Skip to content

[CALCITE-7574] RelDecorrelator.isFieldNotNullRecursive throws IndexOutOfBoundsException when decorrelating correlated scalar subquery with Aggregate#4981

Merged
mihaibudiu merged 1 commit into
apache:mainfrom
libe8013:CALCITE-7574
Jun 2, 2026

Conversation

@libe8013
Copy link
Copy Markdown

@libe8013 libe8013 commented Jun 1, 2026

Jira Link

CALCITE-7574

Changes Proposed

In RelDecorrelator.isFieldNotNullRecursive, the Aggregate branch used ImmutableBitSet.size() for bounds checking, which returns the bitset capacity (words.length * 64,
typically ≥ 64) rather than the actual number of group keys. When a field index corresponds to an aggregate result field (not a group field), the check was too lenient, causing
groupSet.asList().get(index) to call nth(index) which throws IndexOutOfBoundsException.

Changed groupSet.size() to agg.getGroupCount() (equivalent to groupSet.cardinality()), which correctly returns the number of group keys. Aggregate result fields now
correctly return false instead of throwing an exception.

Bug introduced by [CALCITE-6962].

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

@xiedeyantu
Copy link
Copy Markdown
Member

The commit message should not contain "Fix", we also should keep the message same as PR title and JIRA title.

@libe8013 libe8013 changed the title [CALCITE-7574] RelDecorrelator throws IndexOutOfBoundsException when decorrelating scalar subquery with Aggregate RelDecorrelator.isFieldNotNullRecursive throws IndexOutOfBoundsException when decorrelating correlated scalar subquery with Aggregate Jun 1, 2026
@libe8013
Copy link
Copy Markdown
Author

libe8013 commented Jun 1, 2026

@xiedeyantu Updated as requested. Commit message, PR title, and JIRA summary are now aligned. Also renamed the Fix section to Solution in the description for consistency. Thanks!

@xiedeyantu
Copy link
Copy Markdown
Member

The commit message and PR title should using "[CALCITE-7574] xxx" at the beginning.

@libe8013
Copy link
Copy Markdown
Author

libe8013 commented Jun 1, 2026

@xiedeyantu Updated. JIRA summary, PR title, and commit message now all use the [CALCITE-7574] prefix as requested. Thanks for clarifying!

@xiedeyantu xiedeyantu changed the title RelDecorrelator.isFieldNotNullRecursive throws IndexOutOfBoundsException when decorrelating correlated scalar subquery with Aggregate [CALCITE-7574] RelDecorrelator.isFieldNotNullRecursive throws IndexOutOfBoundsException when decorrelating correlated scalar subquery with Aggregate Jun 1, 2026
@mihaibudiu mihaibudiu added the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label Jun 1, 2026
@mihaibudiu
Copy link
Copy Markdown
Contributor

Since there are 2 approvals already I think you can squash the commits for merging

…tOfBoundsException when decorrelating correlated scalar subquery with Aggregate

Root Cause:
In isFieldNotNullRecursive, the Aggregate branch used ImmutableBitSet.size()
for bounds checking. size() returns the bitset capacity, not the number of
group keys. Changed to agg.getGroupCount() which correctly returns the
actual number of group keys.
@libe8013
Copy link
Copy Markdown
Author

libe8013 commented Jun 2, 2026

@mihaibudiu Squashed the commits into a single clean commit as requested. Ready for merge. Thanks for the review!

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Jun 2, 2026

@mihaibudiu mihaibudiu merged commit 41d88b4 into apache:main Jun 2, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

LGTM-will-merge-soon Overall PR looks OK. Only minor things left.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants