dianfu commented on code in PR #28987:
URL: https://github.com/apache/flink/pull/28987#discussion_r3878861612


##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/internal/TableImpl.java:
##########
@@ -950,6 +958,18 @@ private TableImpl createTable(QueryOperation operation) {
         return new TableImpl(tableEnvironment, operation, 
operationTreeBuilder, lookupResolver);
     }
 
+    private Table projectAfterAggregation(

Review Comment:
   It's only used in `GroupedTableImpl` and `AggregatedTableImpl`. 
`WindowGroupedTableImpl.select()`. So  `WindowAggregatedTableImpl.select()` 
still project the original expressions directly over a window aggregate, so a 
query such as:
   
   `table.window(...).groupBy($("w"), 
$("a").isGreater(10)).select($("a").isGreater(10), $("b").count())`
   
   still fails. Should we also consider window aggregate paths?



##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/operations/utils/OperationTreeBuilder.java:
##########
@@ -242,13 +242,22 @@ public QueryOperation dropColumns(List<Expression> 
fieldLists, QueryOperation ch
         return project(finalFields, child, false);
     }
 
+    public List<Expression> expandExpressions(List<Expression> expressions, 
QueryOperation child) {
+        return getResolver(child).resolveExpanding(expressions);
+    }
+
     public QueryOperation aggregate(
             List<Expression> groupingExpressions,
             List<Expression> aggregates,
             QueryOperation child) {
 
-        ExpressionResolver resolver = getAggResolver(child, 
groupingExpressions);
-        List<ResolvedExpression> resolvedGroupings = 
resolver.resolve(groupingExpressions);
+        // Computed grouping expressions need stable names for subsequent 
projections.
+        List<Expression> namedGroupingExpressions =
+                addAliasToTheCallInAggregate(

Review Comment:
   `addAliasToTheCallInAggregate` only reserves input field names and 
previously generated aliases. For example, `groupBy($("a").as("TMP_0"), 
$("b").upperCase())` assigns `TMP_0` to both grouping expressions because the 
explicit alias is not added to `usedFieldNames`, causing the following 
projection to fail with `Ambiguous column name: TMP_0`.
   
   The explicit alias can also appear after the computed expression, so 
updating the set during the same traversal is insufficient. Please pre-collect 
all explicit expression names before generating temporary aliases and add a 
regression test. The same helper is reused by the aggregate, window aggregate, 
and table aggregate paths, which have the same latent issue.
   
   This is not an issue introduced by this PR, however, worth addressing~



##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/operations/utils/OperationTreeBuilder.java:
##########
@@ -242,13 +242,22 @@ public QueryOperation dropColumns(List<Expression> 
fieldLists, QueryOperation ch
         return project(finalFields, child, false);
     }
 
+    public List<Expression> expandExpressions(List<Expression> expressions, 
QueryOperation child) {

Review Comment:
   Could we encapsulate the workflow in an aggregation-specific 
`OperationTreeBuilder` method instead of exposing expression expansion as a 
standalone operation?
   
   `TableImpl` now needs to coordinate `expand grouping expressions -> build 
the aggregate and assign temporary names -> map grouping expressions to 
aggregate output fields -> project`. These steps form one invariant, especially 
because the final rewrite relies on positional alignment between the expanded 
groupings and the aggregate output schema.
   
   A method such as `aggregateAndProject(groupings, aggregates, projections, 
child)` would keep this aggregation-specific logic inside 
`OperationTreeBuilder` while leaving the generic `project()` context-free.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to