Yaoxuan Wu created FLINK-41752:
----------------------------------

             Summary: GROUP BY ROLLUP / CUBE / GROUPING SETS over two 
projections of the same column fails in FlinkAggregateProjectMergeRule: 
IllegalArgumentException "must be sorted"
                 Key: FLINK-41752
                 URL: https://issues.apache.org/jira/browse/FLINK-41752
             Project: Flink
          Issue Type: Bug
          Components: Table SQL / Planner
    Affects Versions: 2.3.0
            Reporter: Yaoxuan Wu


A ROLLUP / CUBE / GROUPING SETS aggregate whose grouping columns are two 
projections of the same input column fails during optimization (batch and 
streaming, default configuration).

{code:python}
from pyflink.table import EnvironmentSettings, TableEnvironment, DataTypes as D

t = TableEnvironment.create(EnvironmentSettings.in_batch_mode())
t.create_temporary_view('t', t.from_elements([(1, 10), (2, 20)],
    D.ROW([D.FIELD('k', D.INT()), D.FIELD('v', D.INT())])))

q = "SELECT a, b, MAX(v) FROM (SELECT k AS a, {} AS b, v FROM t) GROUP BY 
ROLLUP(a, b)"
print(sorted((tuple(r) for r in t.execute_sql(q.format('ABS(k)')).collect()), 
key=str))
# [(1, 1, 10), (1, None, 10), (2, 2, 20), (2, None, 20), (None, None, 20)]
print(sorted((tuple(r) for r in t.execute_sql(q.format('k')).collect()), 
key=str))
# expected: [(1, 1, 10), (1, None, 10), (2, 2, 20), (2, None, 20), (None, None, 
20)]
# actual:   IllegalArgumentException: must be sorted: [{0}, {0}, {}]
{code}

{noformat}
java.lang.RuntimeException: Error while applying rule 
FlinkAggregateProjectMergeRule, args 
[rel#..:LogicalAggregate(input=..,group={0, 1},groups=[{0, 1}, {0}, 
{}],EXPR$2=MAX($2)), ...]
Caused by: java.lang.IllegalArgumentException: must be sorted: [{0}, {0}, {}]
        at 
org.apache.calcite.rel.core.Aggregate$Group.induce(Aggregate.java:494)
        at 
org.apache.calcite.rel.core.Aggregate.getGroupType(Aggregate.java:481)
        at 
org.apache.calcite.rel.core.Aggregate.explainTerms(Aggregate.java:335)
        ...
        at 
org.apache.calcite.plan.volcano.VolcanoRuleCall.transformTo(VolcanoRuleCall.java:144)
        at 
org.apache.flink.table.planner.plan.rules.logical.FlinkAggregateProjectMergeRule.onMatch(FlinkAggregateProjectMergeRule.java:77)
{noformat}

* {{GROUPING SETS ((a, b), (b))}} fails with {{must be sorted: [{0}, {0}]}}, 
{{CUBE(a, b)}} with {{must be sorted: [{0}, {0}, {0}, {}]}}.
* The Table API form fails the same way ({{add_columns(col('k').alias('k2'))}}, 
then {{GROUP BY ROLLUP(k, k2)}}).
* With a NOT NULL key column the same queries fail with a different message in 
batch mode: {{Type mismatch}} in {{VolcanoPlanner.register}}, after the merged 
Aggregate gets an AUXILIARY_GROUP.
* {{COUNT(DISTINCT ...)}} also reaches it without an explicit ROLLUP, because 
the distinct aggregate is expanded into grouping sets: {{SELECT a, b, 
COUNT(DISTINCT b) FROM (SELECT k AS a, k AS b FROM t) GROUP BY a, b}}.
* A plain {{GROUP BY a, b}} over the same projection works.
* Reproduces on 2.0.1, 2.2.1, 2.3.0 and master (2.4-SNAPSHOT, 2f01434, 
2026-11-19).

Cause:
* {{FlinkAggregateProjectMergeRule.newApply}} maps the grouping keys through 
the Project ({{a -> $0}}, {{b -> $0}}) and permutes the grouping sets with that 
map.
* When two grouping keys map to the same input field, different grouping sets 
become equal ({{[{0, 1}, {0}, {}]}} -> {{[{0}, {0}, {}]}}), and the new 
Aggregate is built with duplicate grouping sets, which 
{{Aggregate.Group.induce}} rejects.
* Merging is not valid there anyway: the duplicated set must produce its rows 
twice.

Calcite fixed the same problem in its own {{AggregateProjectMergeRule}} in 
CALCITE-7320 (1.42.0): the rule does not fire when two permuted grouping sets 
are equal. Flink's copy of the rule needs the same check; a Calcite upgrade 
alone does not help because Flink registers its own rule.




--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to