From f10d022e11dcd1015db1a74ce9f4198ebdcb7f40 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 16 Oct 2023 13:37:39 +0200 Subject: Merging upstream version 18.13.0. Signed-off-by: Daniel Baumann --- sqlglot/optimizer/pushdown_projections.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'sqlglot/optimizer/pushdown_projections.py') diff --git a/sqlglot/optimizer/pushdown_projections.py b/sqlglot/optimizer/pushdown_projections.py index b51601f..4bc3bd2 100644 --- a/sqlglot/optimizer/pushdown_projections.py +++ b/sqlglot/optimizer/pushdown_projections.py @@ -9,7 +9,9 @@ from sqlglot.schema import ensure_schema SELECT_ALL = object() # Selection to use if selection list is empty -DEFAULT_SELECTION = lambda: alias("1", "_") +DEFAULT_SELECTION = lambda is_agg: alias( + exp.Max(this=exp.Literal.number(1)) if is_agg else "1", "_" +) def pushdown_projections(expression, schema=None, remove_unused_selections=True): @@ -98,6 +100,7 @@ def _remove_unused_selections(scope, parent_selections, schema, alias_count): new_selections = [] removed = False star = False + is_agg = False select_all = SELECT_ALL in parent_selections @@ -112,6 +115,9 @@ def _remove_unused_selections(scope, parent_selections, schema, alias_count): star = True removed = True + if not is_agg and selection.find(exp.AggFunc): + is_agg = True + if star: resolver = Resolver(scope, schema) names = {s.alias_or_name for s in new_selections} @@ -124,7 +130,7 @@ def _remove_unused_selections(scope, parent_selections, schema, alias_count): # If there are no remaining selections, just select a single constant if not new_selections: - new_selections.append(DEFAULT_SELECTION()) + new_selections.append(DEFAULT_SELECTION(is_agg)) scope.expression.select(*new_selections, append=False, copy=False) -- cgit v1.2.3