Yaoxuan Wu created FLINK-41681:
----------------------------------

             Summary: Correlated IN sub-query over an aggregate fails with 
IndexOutOfBoundsException in RelDecorrelator.isFieldNotNullRecursive (master 
regression since the Calcite 1.40 upgrade)
                 Key: FLINK-41681
                 URL: https://issues.apache.org/jira/browse/FLINK-41681
             Project: Flink
          Issue Type: Bug
          Components: Table SQL / Planner
    Affects Versions: 2.4.0
            Reporter: Yaoxuan Wu


On master, a correlated IN sub-query whose input is an aggregate fails during 
decorrelation. 2.3.0 and earlier plan the same query.

{code:sql}
-- s(k INT): (-2), (0)
CREATE TEMPORARY VIEW g AS SELECT k, AVG(k) AS m FROM s GROUP BY k;

SELECT o.k, o.m IN (SELECT i.m FROM g i WHERE i.m = o.m) AS d FROM g o;
-- expected: (-2, TRUE), (0, TRUE)    (2.0.1, 2.2.1, 2.3.0)
-- actual on master:
{code}

{noformat}
java.lang.IndexOutOfBoundsException: index out of range: 0
        at org.apache.calcite.util.ImmutableBitSet.nth(ImmutableBitSet.java:903)
        at 
org.apache.calcite.util.ImmutableBitSet$2.get(ImmutableBitSet.java:681)
        at 
org.apache.calcite.sql2rel.RelDecorrelator.isFieldNotNullRecursive(RelDecorrelator.java:3556)
        ...
        at 
org.apache.calcite.sql2rel.RelDecorrelator.isFieldNotNull(RelDecorrelator.java:3537)
        at 
org.apache.calcite.sql2rel.RelDecorrelator.decorrelateRel(RelDecorrelator.java:1703)
{noformat}

* Fails on master (2.4-SNAPSHOT, commits a47e4bf of 2026-10-06 and 2f01434 of 
2026-11-19), batch and streaming. Works on 2.0.1, 2.2.1 and 2.3.0.

Cause:
* {{isFieldNotNull}} / {{isFieldNotNullRecursive}} came into Flink's copy of 
{{RelDecorrelator}} with the Calcite 1.40 upgrade (FLINK-39828, from 
CALCITE-6962).
* The {{Aggregate}} branch of {{isFieldNotNullRecursive}} checks {{index >= 
groupSet.size()}}. {{ImmutableBitSet.size()}} is the capacity of the bit set 
(64), not the number of group keys, so for an aggregate-call field the check 
passes and {{groupSet.asList().get(index)}} throws.
* Fixed in Calcite by CALCITE-7574 (1.43.0): the check uses 
{{agg.getGroupCount()}}.

Possible fix: use {{index >= agg.getGroupCount()}} in Flink's 
{{org.apache.calcite.sql2rel.RelDecorrelator}}, as in CALCITE-7574.




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

Reply via email to