Edit on GitHub

# sqlglot.optimizer.isolate_table_selects

View Source

     1from sqlglot import alias, exp
     2from sqlglot.errors import OptimizeError
     3from sqlglot.optimizer.scope import traverse_scope
     4from sqlglot.schema import ensure_schema
     5
     6
     7def isolate_table_selects(expression, schema=None):
     8    schema = ensure_schema(schema)
     9
    10    for scope in traverse_scope(expression):
    11        if len(scope.selected_sources) == 1:
    12            continue
    13
    14        for _, source in scope.selected_sources.values():
    15            if not isinstance(source, exp.Table) or not schema.column_names(source):
    16                continue
    17
    18            if not source.alias:
    19                raise OptimizeError("Tables require an alias. Run qualify_tables optimization.")
    20
    21            source.replace(
    22                exp.select("*")
    23                .from_(
    24                    alias(source, source.alias_or_name, table=True),
    25                    copy=False,
    26                )
    27                .subquery(source.alias, copy=False)
    28            )
    29
    30    return expression

def isolate_table_selects(expression, schema=None): View Source

     8def isolate_table_selects(expression, schema=None):
     9    schema = ensure_schema(schema)
    10
    11    for scope in traverse_scope(expression):
    12        if len(scope.selected_sources) == 1:
    13            continue
    14
    15        for _, source in scope.selected_sources.values():
    16            if not isinstance(source, exp.Table) or not schema.column_names(source):
    17                continue
    18
    19            if not source.alias:
    20                raise OptimizeError("Tables require an alias. Run qualify_tables optimization.")
    21
    22            source.replace(
    23                exp.select("*")
    24                .from_(
    25                    alias(source, source.alias_or_name, table=True),
    26                    copy=False,
    27                )
    28                .subquery(source.alias, copy=False)
    29            )
    30
    31    return expression