Yaoxuan Wu created FLINK-41755:
----------------------------------
Summary: Batch planning fails with CyclicMetadataException, or
does not terminate, for GROUP BY (f(k), k) over GROUP BY k
Key: FLINK-41755
URL: https://issues.apache.org/jira/browse/FLINK-41755
Project: Flink
Issue Type: Bug
Components: Table SQL / Planner
Affects Versions: 2.3.0, 2.0.1
Reporter: Yaoxuan Wu
In batch mode, a query with an aggregate grouped by (f(k), k) on top of an
aggregate grouped by k fails during planning with
org.apache.calcite.rel.metadata.CyclicMetadataException. The rule named in the
error differs per query and per version: FlinkCalcMergeRule,
FlinkLogicalCalcConverter, AggregateReduceGroupingRule,
FlinkSemiAntiJoinProjectTransposeRule, ProjectToCalcRule, or
AggregateProjectPullUpConstantsRule on master.
{code:sql}
-- t(k INT): (2), (1), (-1) s(k INT): (2), (1), (5)
-- (e.g. tEnv.fromValues / fromElements; any source)
-- q1
SELECT x, MAX(k) FROM (
SELECT a, k, AVG(a) AS x FROM (SELECT k, AVG(k) AS a FROM t GROUP BY k) GROUP
BY a, k
) WHERE x IS NOT NULL GROUP BY x;
-- q2
WITH n AS (SELECT k, a, SUM(DISTINCT k) AS v FROM (
SELECT k, MAX(k) AS a FROM (SELECT k FROM t INTERSECT SELECT k
FROM s) GROUP BY k) GROUP BY k, a)
SELECT SUM(o.v) FROM n o WHERE o.k IN (SELECT i.a FROM n i WHERE i.v = o.a AND
i.a = o.v);
-- q3 = q1 without the WHERE: plans on 2.0 - 2.3, fails on master
SELECT x, MAX(k) FROM (
SELECT a, k, AVG(a) AS x FROM (SELECT k, AVG(k) AS a FROM t GROUP BY k) GROUP
BY a, k
) GROUP BY x;
{code}
{noformat}
java.lang.RuntimeException: Error while applying rule FlinkCalcMergeRule, args
[rel#37463:LogicalCalc.NONE.any.[](input=RelSubset#2179, ...)]
at
org.apache.calcite.plan.volcano.VolcanoRuleCall.onMatch(VolcanoRuleCall.java:250)
...
Caused by: org.apache.calcite.rel.metadata.CyclicMetadataException
at
org.apache.calcite.rel.metadata.RelMetadataQuery.getRowCount(RelMetadataQuery.java:259)
at
org.apache.flink.table.planner.plan.metadata.FlinkRelMdRowCount.getRowCountOfAgg(FlinkRelMdRowCount.scala:119)
at
org.apache.flink.table.planner.plan.metadata.FlinkRelMdRowCount.getRowCount(FlinkRelMdRowCount.scala:95)
at
org.apache.calcite.rel.metadata.RelMetadataQuery.getRowCount(RelMetadataQuery.java:259)
at
org.apache.flink.table.planner.plan.metadata.FlinkRelMdRowCount.getRowCount(FlinkRelMdRowCount.scala:411)
at
org.apache.calcite.rel.metadata.RelMetadataQuery.getRowCount(RelMetadataQuery.java:259)
at
org.apache.flink.table.planner.plan.metadata.FlinkRelMdRowCount.getRowCountOfAgg(FlinkRelMdRowCount.scala:119)
... (repeats)
at
org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalAggregate.computeSelfCost(FlinkLogicalAggregate.scala:84)
{noformat}
Expected: the queries run (q3 returns (-1, -1), (1, 1), (2, 2) on 2.3). Actual:
planning fails after about a minute.
||query||2.0.1||2.2.1||2.3.0||master (2.4-SNAPSHOT)||
|q1|CyclicMetadataException
(FlinkCalcMergeRule)|same|same|CyclicMetadataException
(AggregateProjectPullUpConstantsRule)|
|q2|CyclicMetadataException
(FlinkLogicalCalcConverter)|same|same|CyclicMetadataException
(AggregateProjectPullUpConstantsRule)|
|q3|ok|ok|ok|CyclicMetadataException (AggregateProjectPullUpConstantsRule)|
* The failure depends on small details. With SUM / MAX / COUNT instead of AVG,
q1 plans (and returns correct results).
* With a filesystem (json) or datagen source instead of fromElements, q1 does
not fail on 2.3.0 but never finishes planning: killed after 15 minutes, still
in the logical VolcanoPlanner phase (FlinkAggregateProjectMergeRule,
ProjectMergeRule, FlinkCalcMergeRule, ProjectCalcMergeRule firing; most time in
RelSubset.contains), with the memo still growing (about 390 RelSets / 5.6k
LogicalAggregates after 1 min, 1100 RelSets / 35k LogicalAggregates after 14
min). On master the same query fails after 3 s with the CyclicMetadataException
above.
* Other queries of the same shape fail with a UniqueGroups cycle in
AggregateReduceGroupingRule, or with a RowCount cycle in
FlinkSemiAntiJoinProjectTransposeRule / ProjectToCalcRule. On master one of
them fails with a StackOverflowError in
FlinkAggregateJoinTransposeRule.containsSnapshot instead.
Analysis (partly unverified):
* The metadata recursion goes Aggregate -> input RelSubset ->
{{FlinkRelMdRowCount.getRowCount(RelSubset)}}, which uses
{{Util.first(subset.getBest, subset.getOriginal)}} -> Aggregate -> the same
RelSubset. So the Volcano memo holds a rel whose input subset leads back to the
rel's own set.
* The outer aggregate's group key contains the unique key of its input (the
inner group key), so it is redundant. My guess is that it is registered as
equivalent to its input, the planner merges the two sets, and an aggregate ends
up with its own set as input. Every metadata handler, or rule helper, that
follows getBest / getOriginal then loops.
* FLINK-24192 also mentions CyclicMetadataException, but there it only appears
in a plan dump of an infinite-cost failure, for a different query shape.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)