Skip to content

[CALCITE-6087] EnumerableSortedAggregate returns incorrect result when input is empty - #5202

Merged
rubenada merged 1 commit into
apache:mainfrom
1fanwang:fix-sorted-global-aggregate
Aug 25, 2026
Merged

[CALCITE-6087] EnumerableSortedAggregate returns incorrect result when input is empty#5202
rubenada merged 1 commit into
apache:mainfrom
1fanwang:fix-sorted-global-aggregate

Conversation

@1fanwang

@1fanwang 1fanwang commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Jira Link

CALCITE-6087

Changes Proposed

When a query applies MAX or COUNT to empty input, a sorted global aggregate returns no row. SQL requires one row containing NULL and 0.

Sorted grouping emits one row per observed key, but a global aggregate has no grouping key. The sorted aggregate rule now declines an empty group set, so EnumerableAggregate handles it through its zero-key singleton path. Grouped sorted aggregates are unchanged.

Testing

Scenario Result
Forced sorted execution before the fix Empty result instead of m=null; c=0
Standard aggregate after the fix m=null; c=0
Sorted-only planning after the fix Expected planning failure
Raw logs

With the pre-fix parent version of EnumerableSortedAggregateRule:

$ JAVA_HOME=$(/usr/libexec/java_home -v 21) ./gradlew :core:test --tests org.apache.calcite.test.enumerable.EnumerableSortedAggregateTest.sortedAggOnEmptyInput --no-daemon --console=plain
Expected: "m=null; c=0"
     but: was ""
1 test completed, 1 failed
BUILD FAILED

$ JAVA_HOME=$(/usr/libexec/java_home -v 21) ./gradlew :core:test --tests 'org.apache.calcite.test.enumerable.EnumerableSortedAggregateTest.aggOnEmptyInput' --tests 'org.apache.calcite.test.enumerable.EnumerableSortedAggregateTest.sortedAggRuleRejectsEmptyGroupSet' --no-daemon --console=plain --rerun-tasks
Expected: a string containing "There are not enough rules to produce a node with desired properties"
     but: was "org.opentest4j.AssertionFailedError: expected exception but none was thrown"
2 tests completed, 1 failed
BUILD FAILED

With this PR, the standard-path assertion observed m=null; c=0:

$ JAVA_HOME=$(/usr/libexec/java_home -v 21) ./gradlew :core:test --tests 'org.apache.calcite.test.enumerable.EnumerableSortedAggregateTest.aggOnEmptyInput' --tests 'org.apache.calcite.test.enumerable.EnumerableSortedAggregateTest.sortedAggRuleRejectsEmptyGroupSet' --no-daemon --console=plain --rerun-tasks
2 completed, 0 failed, 0 skipped
BUILD SUCCESSFUL

$ JAVA_HOME=$(/usr/libexec/java_home -v 21) ./gradlew :core:test --tests 'org.apache.calcite.test.enumerable.EnumerableSortedAggregateTest' --no-daemon --console=plain
13 completed, 0 failed, 0 skipped
BUILD SUCCESSFUL

@xuzifu666

Copy link
Copy Markdown
Member

Thank you for the contribution! Please take a look to jira discussion.

@rubenada

Copy link
Copy Markdown
Contributor

Thanks for the contribution @1fanwang !
@xuzifu666 I opened this ticket long time ago, I had a suspicion about a probably fix (implemented in this PR), I asked the contributors that worked on the EnumerableSortedAggregate for their opinion about the fix, but got no answer.
I think we can move forward if we are indeed correcting a bug.

@1fanwang 1fanwang changed the title [CALCITE-6087] EnumerableSortedAggregate returns incorrect result when input is empty [CALCITE-6087] Fix empty global EnumerableSortedAggregate results Aug 23, 2026
@1fanwang

Copy link
Copy Markdown
Contributor Author

Thank you for the contribution! Please take a look to jira discussion.

thanks for the pointer @xuzifu666
I think the direction from CALCITE-6087 is to reject empty groupSets in EnumerableSortedAggregateRule

done in d3b829a

@rubenada rubenada changed the title [CALCITE-6087] Fix empty global EnumerableSortedAggregate results [CALCITE-6087] EnumerableSortedAggregate returns incorrect result when input is empty Aug 24, 2026
@rubenada

Copy link
Copy Markdown
Contributor

Thanks @1fanwang , LGTM. I just left some minor comment.
Could you please also squash commits into a single one (keeping [CALCITE-6087] EnumerableSortedAggregate returns incorrect result when input is empty as final commit message)

@1fanwang
1fanwang force-pushed the fix-sorted-global-aggregate branch 2 times, most recently from ec5bec9 to 35d86fd Compare August 24, 2026 16:04
…n input is empty

Signed-off-by: 1fanwang <1fannnw@gmail.com>
@1fanwang
1fanwang force-pushed the fix-sorted-global-aggregate branch from 35d86fd to 3d2df15 Compare August 24, 2026 18:48
@1fanwang
1fanwang requested a review from rubenada August 24, 2026 19:13
@sonarqubecloud

Copy link
Copy Markdown

@rubenada rubenada added the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label Aug 24, 2026
@rubenada
rubenada merged commit bd65b15 into apache:main Aug 25, 2026
20 of 35 checks passed
@rubenada

Copy link
Copy Markdown
Contributor

Thanks for the patch @1fanwang !
If you want to continue contributing on this area, there's a related ticket https://issues.apache.org/jira/browse/CALCITE-6089 which may have been also fixed by the current patch. It'd be needed to verify it (and add the appropriate tests, similar to the ones here to prevent any regression in the future).

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.

3 participants