Skip to content

[CALCITE-7930] Correlated sub-query with ROLLUP or GROUPING SETS loses the () group when the sub-query input is empty - #5405

Merged
mihaibudiu merged 1 commit into
apache:mainfrom
mihaibudiu:issue7930
Nov 12, 2026
Merged

mihaibudiu merged 1 commit into
apache:mainfrom
mihaibudiu:issue7930

Conversation

@mihaibudiu

@mihaibudiu mihaibudiu commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Jira Link

CALCITE-7930

I am not thrilled by this solution, because it uses a switch statement over the aggregates to decide their behavior. So it's not extensible to adding new aggregate functions without changing the decorrelator. A solution that does not look at the aggregate can be implemented, but generates an extra join in some plans.

The same bug exists in both decorrelators.

…s the () group when the sub-query input is empty
@sonarqubecloud

Copy link
Copy Markdown

@silundong silundong left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I first looked at these test cases, I thought TopDownGeneralDecorrelator should theoretically handle them correctly. Given an execution plan like this:

LogicalAggregate(group=[{}], EXPR$0=[COUNT()])
  LogicalAggregate(group=[{0}], groups=[[{0}, {}]], EXPR$0=[COUNT()])

it would introduce a left join for the second Aggregate, as ensured by the following condition in unnestInternal:

if (aggregate.hasEmptyGroup()
    && (!allowEmptyOutputFromRewrite || hasCountFunction)) {
  ...
}

However, after printing the execution plan, I found that TopDownGeneralDecorrelator actually receives the following:

LogicalAggregate(group=[{}], EXPR$0=[COUNT()])
    LogicalAggregate(group=[{0}], groups=[[{0}, {}]])

The second Aggregate does not contain a COUNT function, so the condition above was not satisfied.

@mihaibudiu

Copy link
Copy Markdown
Contributor Author

I read your comment as an explanation of the previous implementation, and not as something that needs to be addressed. I plan to merge this.

@mihaibudiu mihaibudiu added the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label Nov 12, 2026
@mihaibudiu
mihaibudiu merged commit 64b903c into apache:main Nov 12, 2026
16 checks passed
@mihaibudiu
mihaibudiu deleted the issue7930 branch November 12, 2026 02:41
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.

2 participants