You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "beetle0915 (via GitHub)" <gi...@apache.org> on 2026/08/18 07:50:59 UTC
[PR] [FLINK-40344][table] Support selecting computed grouping expressions [flink]
beetle0915 opened a new pull request, #28987:
URL: https://github.com/apache/flink/pull/28987
## What is the purpose of the change
This change fixes the selection of computed grouping expressions in the Table API.
Previously, a computed grouping expression such as `a > 10` was materialized as an aggregate output field, but a subsequent projection attempted to resolve the original expression against the aggregate output. Since the original field `a` was no longer available, validation failed.
## Brief change log
- Assign stable output names to computed grouping expressions.
- Replace matching grouping expressions in aggregate projections with references to the corresponding aggregate output fields.
- Unwrap grouping expression aliases when serializing `GROUP BY`.
- Add regression tests for direct reuse and structurally equivalent computed grouping expressions.
- Cover both `groupBy(...).select(...)` and explicit `groupBy(...).aggregate(...).select(...)` paths while ensuring that non-grouping expressions remain rejected.
## Verifying this change
This change added tests in `QueryOperationSqlSerializationTest` covering:
- Selecting the same computed grouping expression.
- Selecting a structurally equivalent computed grouping expression with an alias.
- Selecting a computed grouping expression after an explicit `aggregate()` call.
- Ensuring that a non-grouping expression is still rejected.
## Does this pull request potentially affect one of the following parts:
- Dependencies (does it add or upgrade a dependency): no
- The public API, i.e., is any changed class annotated with `@Public(Evolving)`: no
- The serializers: no
- The runtime per-record code paths (performance sensitive): no
- Anything that affects deployment or recovery: no
- The S3 file system connector: no
## Documentation
- Does this pull request introduce a new feature? no
- If yes, how is the feature documented? not applicable
---
##### Was generative AI tooling used to co-author this PR?
- [X] Yes
Generated-by: OpenAI Codex (GPT-5)
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40344][table] Support selecting computed grouping expressions [flink]
Posted by "dianfu (via GitHub)" <gi...@apache.org>.
dianfu commented on PR #28987:
URL: https://github.com/apache/flink/pull/28987#issuecomment-6122007293
@beetle0915 Hey, what's the status of this PR? :)
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40344][table] Support selecting computed grouping expressions [flink]
Posted by "beetle0915 (via GitHub)" <gi...@apache.org>.
beetle0915 commented on PR #28987:
URL: https://github.com/apache/flink/pull/28987#issuecomment-5329020637
The latest Azure CI run has passed successfully on the current head (cd82e7c). This PR is ready for review. Thanks!
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40344][table] Support selecting computed grouping expressions [flink]
Posted by "beetle0915 (via GitHub)" <gi...@apache.org>.
beetle0915 commented on code in PR #28987:
URL: https://github.com/apache/flink/pull/28987#discussion_r4307718202
##########
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:
Fixed both WindowGroupedTableImpl.select and WindowAggregatedTableImpl.select, with regression coverage for computed grouping expressions and alias collisions.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40344][table] Support selecting computed grouping expressions [flink]
Posted by "beetle0915 (via GitHub)" <gi...@apache.org>.
beetle0915 commented on code in PR #28987:
URL: https://github.com/apache/flink/pull/28987#discussion_r4307716253
##########
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:
Fixed. All explicit names are now reserved before generating aliases, regardless of their order. Added regression tests for both orderings across regular, window, and table aggregations.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40344][table] Support selecting computed grouping expressions [flink]
Posted by "beetle0915 (via GitHub)" <gi...@apache.org>.
beetle0915 commented on PR #28987:
URL: https://github.com/apache/flink/pull/28987#issuecomment-5326510102
@flinkbot run azure
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40344][table] Support selecting computed grouping expressions [flink]
Posted by "beetle0915 (via GitHub)" <gi...@apache.org>.
beetle0915 commented on PR #28987:
URL: https://github.com/apache/flink/pull/28987#issuecomment-5326871829
@flinkbot run azure
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40344][table] Support selecting computed grouping expressions [flink]
Posted by "dianfu (via GitHub)" <gi...@apache.org>.
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40344][table] Support selecting computed grouping expressions [flink]
Posted by "beetle0915 (via GitHub)" <gi...@apache.org>.
beetle0915 commented on PR #28987:
URL: https://github.com/apache/flink/pull/28987#issuecomment-6182766055
I've been swamped these days. I'll get this done over the weekend.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40344][table] Support selecting computed grouping expressions [flink]
Posted by "flinkbot (via GitHub)" <gi...@apache.org>.
flinkbot commented on PR #28987:
URL: https://github.com/apache/flink/pull/28987#issuecomment-5325299737
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "3767c7ecc5df9c9bcdcc2f86d71980209be776d0",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "3767c7ecc5df9c9bcdcc2f86d71980209be776d0",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* 3767c7ecc5df9c9bcdcc2f86d71980209be776d0 UNKNOWN
<details>
<summary>Bot commands</summary>
The @flinkbot bot supports the following commands:
- `@flinkbot run azure` re-run the last Azure build
</details>
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40344][table] Support selecting computed grouping expressions [flink]
Posted by "beetle0915 (via GitHub)" <gi...@apache.org>.
beetle0915 commented on code in PR #28987:
URL: https://github.com/apache/flink/pull/28987#discussion_r4307717034
##########
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:
Updated. The grouping expansion, aggregation, grouping-reference replacement, and projection are now coordinated by aggregateAndProject / windowAggregateAndProject in OperationTreeBuilder. The generic project method remains unchanged.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-40344][table] Support selecting computed grouping expressions [flink]
Posted by "dianfu (via GitHub)" <gi...@apache.org>.
dianfu commented on PR #28987:
URL: https://github.com/apache/flink/pull/28987#issuecomment-6259345435
@beetle0915 Thanks for addressing the previous review comments. After another pass, I feel that the implementation has makes the already complex code significantly harder to understand and maintain.
The original limitation also has a straightforward workaround by materializing the computed grouping expression before aggregation. For example:
Expression key = $("a").isGreater(10);
Table withKey = table.addColumns(key.as("group_key"));
withKey.groupBy($("group_key"))
.select($("group_key"), $("b").count());
Since the issue occurs during query validation, does not cause incorrect runtime results, and can be avoided with the pre-projection above, I am inclined to keep the current behavior and defer this fix for now. We can revisit it later if there is a simpler design that does not add this level of complexity to the aggregation paths.
What do you think?
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org