|
| 1 | +""" |
| 2 | +Overridden version of the pushdown_projections.py file from sqlglot. |
| 3 | +""" |
| 4 | + |
| 5 | +from collections import defaultdict |
| 6 | + |
| 7 | +from sqlglot import exp |
| 8 | +from sqlglot.optimizer.pushdown_projections import SELECT_ALL, _remove_unused_selections |
| 9 | +from sqlglot.optimizer.scope import Scope, traverse_scope |
| 10 | +from sqlglot.schema import ensure_schema |
| 11 | + |
| 12 | +# ruff: noqa |
| 13 | +# mypy: ignore-errors |
| 14 | +# ruff & mypy should not try to typecheck or verify any of this |
| 15 | + |
| 16 | + |
| 17 | +def pushdown_projections(expression, schema=None, remove_unused_selections=True): |
| 18 | + """ |
| 19 | + Rewrite sqlglot AST to remove unused columns projections. |
| 20 | +
|
| 21 | + Example: |
| 22 | + >>> import sqlglot |
| 23 | + >>> sql = "SELECT y.a AS a FROM (SELECT x.a AS a, x.b AS b FROM x) AS y" |
| 24 | + >>> expression = sqlglot.parse_one(sql) |
| 25 | + >>> pushdown_projections(expression).sql() |
| 26 | + 'SELECT y.a AS a FROM (SELECT x.a AS a FROM x) AS y' |
| 27 | +
|
| 28 | + Args: |
| 29 | + expression (sqlglot.Expression): expression to optimize |
| 30 | + remove_unused_selections (bool): remove selects that are unused |
| 31 | + Returns: |
| 32 | + sqlglot.Expression: optimized expression |
| 33 | + """ |
| 34 | + # Map of Scope to all columns being selected by outer queries. |
| 35 | + schema = ensure_schema(schema) |
| 36 | + source_column_alias_count = {} |
| 37 | + referenced_columns = defaultdict(set) |
| 38 | + |
| 39 | + # We build the scope tree (which is traversed in DFS postorder), then iterate |
| 40 | + # over the result in reverse order. This should ensure that the set of selected |
| 41 | + # columns for a particular scope are completely build by the time we get to it. |
| 42 | + for scope in reversed(traverse_scope(expression)): |
| 43 | + parent_selections = referenced_columns.get(scope, {SELECT_ALL}) |
| 44 | + alias_count = source_column_alias_count.get(scope, 0) |
| 45 | + |
| 46 | + # We can't remove columns SELECT DISTINCT nor UNION DISTINCT. |
| 47 | + # PyDough Change: also include ANY set op |
| 48 | + if scope.expression.args.get("distinct") or isinstance( |
| 49 | + scope.expression, exp.SetOperation |
| 50 | + ): |
| 51 | + parent_selections = {SELECT_ALL} |
| 52 | + |
| 53 | + if isinstance(scope.expression, exp.SetOperation): |
| 54 | + left, right = scope.union_scopes |
| 55 | + referenced_columns[left] = parent_selections |
| 56 | + |
| 57 | + if any(select.is_star for select in right.expression.selects): |
| 58 | + referenced_columns[right] = parent_selections |
| 59 | + elif not any(select.is_star for select in left.expression.selects): |
| 60 | + if scope.expression.args.get("by_name"): |
| 61 | + referenced_columns[right] = referenced_columns[left] |
| 62 | + else: |
| 63 | + referenced_columns[right] = [ |
| 64 | + right.expression.selects[i].alias_or_name |
| 65 | + for i, select in enumerate(left.expression.selects) |
| 66 | + if SELECT_ALL in parent_selections |
| 67 | + or select.alias_or_name in parent_selections |
| 68 | + ] |
| 69 | + |
| 70 | + if isinstance(scope.expression, exp.Select): |
| 71 | + if remove_unused_selections: |
| 72 | + _remove_unused_selections(scope, parent_selections, schema, alias_count) |
| 73 | + |
| 74 | + if scope.expression.is_star: |
| 75 | + continue |
| 76 | + |
| 77 | + # Group columns by source name |
| 78 | + selects = defaultdict(set) |
| 79 | + for col in scope.columns: |
| 80 | + table_name = col.table |
| 81 | + col_name = col.name |
| 82 | + selects[table_name].add(col_name) |
| 83 | + |
| 84 | + # Push the selected columns down to the next scope |
| 85 | + for name, (node, source) in scope.selected_sources.items(): |
| 86 | + if isinstance(source, Scope): |
| 87 | + columns = ( |
| 88 | + {SELECT_ALL} if scope.pivots else selects.get(name) or set() |
| 89 | + ) |
| 90 | + referenced_columns[source].update(columns) |
| 91 | + |
| 92 | + column_aliases = node.alias_column_names |
| 93 | + if column_aliases: |
| 94 | + source_column_alias_count[source] = len(column_aliases) |
| 95 | + |
| 96 | + return expression |
0 commit comments