[
https://issues.apache.org/jira/browse/FLINK-41762?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Yaoxuan Wu updated FLINK-41762:
-------------------------------
Labels: (was: correctness)
> IndexOutOfBoundsException in FlinkRelMdDistinctRowCount for Expand when a
> ROLLUP / CUBE / GROUPING SETS query aggregates one of its grouping columns
> ----------------------------------------------------------------------------------------------------------------------------------------------------
>
> Key: FLINK-41762
> URL: https://issues.apache.org/jira/browse/FLINK-41762
> Project: Flink
> Issue Type: Bug
> Components: Table SQL / Planner
> Affects Versions: 2.3.0
> Reporter: Yaoxuan Wu
> Priority: Major
>
> A ROLLUP / CUBE / GROUPING SETS query that aggregates one of its own grouping
> columns fails while it is optimized as soon as a parent operator makes the
> planner ask for the aggregate's row count (DISTINCT, a join, INTERSECT /
> EXCEPT). 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('j', D.INT()), D.FIELD('n', D.INT())])))
> q = "SELECT {} x FROM (SELECT n, SUM(n) AS x FROM t GROUP BY GROUPING SETS
> ((n), ()))"
> print(sorted(tuple(r) for r in t.execute_sql(q.format('')).collect()))
> # [(10,), (20,), (30,)]
> print(sorted(tuple(r) for r in t.execute_sql(q.format('DISTINCT')).collect()))
> # expected: [(10,), (20,), (30,)]
> # actual: IndexOutOfBoundsException: 2 (RelMdUtil.splitCols)
> {code}
> {noformat}
> java.lang.RuntimeException: Error while applying rule
> FlinkLogicalCalcConverter(in:NONE,out:LOGICAL), args [rel#..:LogicalCalc(...)]
> Caused by: java.lang.IndexOutOfBoundsException: 2
> at scala.collection.mutable.ArrayBuffer.apply(ArrayBuffer.scala:49)
> at
> org.apache.calcite.rel.metadata.RelMdUtil.splitCols(RelMdUtil.java:589)
> at
> org.apache.flink.table.planner.plan.metadata.FlinkRelMdDistinctRowCount.getDistinctRowCount(FlinkRelMdDistinctRowCount.scala:152)
> -- Calc
> ...
> at
> org.apache.flink.table.planner.plan.metadata.FlinkRelMdDistinctRowCount.getDistinctRowCount(FlinkRelMdDistinctRowCount.scala:242)
> -- Expand
> ...
> at
> org.apache.flink.table.planner.plan.metadata.FlinkRelMdDistinctRowCount.getDistinctRowCountOfAggregate(FlinkRelMdDistinctRowCount.scala:345)
> {noformat}
> * Same with ROLLUP / CUBE, with MAX / AVG of a grouping column, and with a
> join or {{INTERSECT ALL}} as the parent instead of DISTINCT.
> * {{SUM}} of a column that is not a grouping column works, and so does
> {{COUNT(n)}}.
> * Reproduces on 2.0.1, 2.2.1, 2.3.0 and master (2.4-SNAPSHOT, 2f01434,
> 2026-11-19).
> Cause:
> * {{DecomposeGroupingSetsRule}} rewrites the grouping-sets aggregate to an
> Aggregate over an Expand. When an aggregate argument is also a grouping
> column, the Expand appends a copy of that column, so the Expand is wider than
> its input.
> * {{getDistinctRowCountOfAggregate}} asks for the NDV of the argument column
> ({{ImmutableBitSet.of(argList)}}), i.e. of that appended column.
> * In {{getDistinctRowCount(rel: Expand, ...)}} a group key that does not
> contain the expand-id column goes to the {{else}} branch, which passes the
> key unchanged to the Expand's input:
> {code}
> } else {
> mq.getDistinctRowCount(rel.getInput, groupKey, newPredicate)
> }
> {code}
> * That column index does not exist in the input, and the Calc / Aggregate
> handler below fails ({{RelMdUtil.splitCols}}, or
> {{FlinkRelMdUtil.setAggChildKeys}} in some plans with an aggregate below the
> Expand).
> A possible fix: map the keys through the Expand projections in the {{else}}
> branch too ({{project.get(key)}}, as the branch with the expand id does).
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)