libe8013 opened a new pull request, #4981:
URL: https://github.com/apache/calcite/pull/4981

   ## Jira Link
   
     [CALCITE-7574](https://issues.apache.org/jira/browse/CALCITE-7574)
   
     ## Changes Proposed
   
     Fix `IndexOutOfBoundsException` in 
`RelDecorrelator.isFieldNotNullRecursive` when decorrelating a correlated 
scalar subquery containing an Aggregate.
   
     ### Root Cause
   
     In `isFieldNotNullRecursive`, the Aggregate branch used 
`ImmutableBitSet.size()` for bounds checking:
   
     ```java
     if (index >= groupSet.size()) {  // BUG
   
     ImmutableBitSet.size() returns the bitset capacity (words.length * 64, 
typically ≥ 64), not the number of group keys. When a field index corresponds 
to an aggregate result field
      (not a group field), the check was too lenient, allowing 
groupSet.asList().get(index) to call nth(index) which throws 
IndexOutOfBoundsException because the bitset has fewer set
      bits than size() suggests.
   
     Fix
   
     Changed groupSet.size() to agg.getGroupCount() (equivalent to 
groupSet.cardinality()), which correctly returns the actual number of group 
keys:
   
     if (index >= agg.getGroupCount()) {  // FIXED
   
     Bug introduced by
   
     [CALCITE-6962] which added the isFieldNotNull/isFieldNotNullRecursive 
methods.
   
     Reproduction
   
     SELECT t1.deptno, t1.total_sal,
       (SELECT COUNT(DISTINCT t2.total_sal) + 1
        FROM (SELECT deptno, SUM(sal) AS total_sal
              FROM emp WHERE deptno IS NOT NULL
              GROUP BY deptno) t2
        WHERE t2.deptno = t1.deptno
          AND t2.total_sal > t1.total_sal) AS rank_sal
     FROM (SELECT deptno, SUM(sal) AS total_sal
           FROM emp WHERE deptno IS NOT NULL
           GROUP BY deptno) t1
     ORDER BY t1.deptno, t1.total_sal DESC
   
     Testing
   
     - Added testDecorrelateScalarSubQueryWithAggregate() in RelDecorrelatorTest
     - All 26 existing RelDecorrelatorTest tests pass with no regression


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to