Skip to content

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

Open
1fanwang wants to merge 1 commit into
apache:mainfrom
1fanwang:fix-sorted-global-aggregate
Open

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

Conversation

@1fanwang

Copy link
Copy Markdown

Jira Link

CALCITE-6087

Changes Proposed

An empty global aggregate implemented by EnumerableSortedAggregate returns no row. For example, MAX and COUNT over an empty input should return one row containing NULL and 0, but the result is empty.

Sorted grouping emits one row per observed key, and 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 aggregate before the fix Reproduced an empty result instead of m=null; c=0
Focused regression test 1 passed
Core test suite 16,639 passed, 157 skipped
Raw logs
$ 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.sortedAggRuleOnEmptyInput --no-daemon --console=plain
1 completed, 0 failed, 0 skipped
BUILD SUCCESSFUL

$ JAVA_HOME=$(/usr/libexec/java_home -v 21) ./gradlew :core:test --no-daemon --console=plain
16639 completed, 0 failed, 157 skipped
BUILD SUCCESSFUL

Formatting, Checkstyle, and forbidden-apis checks pass.

…n input is empty

Signed-off-by: 1fanwang <1fannnw@gmail.com>
@sonarqubecloud

Copy link
Copy Markdown

@xuzifu666

Copy link
Copy Markdown
Member

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

.withHook(Hook.PLANNER, (Consumer<RelOptPlanner>) planner -> {
planner.removeRule(EnumerableRules.ENUMERABLE_AGGREGATE_RULE);
planner.addRule(EnumerableRules.ENUMERABLE_SORTED_AGGREGATE_RULE);
planner.addRule(EnumerableRules.ENUMERABLE_AGGREGATE_RULE);

@rubenada rubenada Aug 22, 2026

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.

why adding this rule if it was just removed a few lines above?
UPDATE: ok, I understand, if we don't do it the query is un-implementable. But now I wonder if we can be really sure to be testing the fix here, or maybe it's the standard ENUMERABLE_AGGREGATE_RULE that kicks in....
I wonder if a more suitable test would be testing the without any hook (to confirm it passes with ENUMERABLE_AGGREGATE_RULE; and then testing it with Hook to remove ENUMERABLE_AGGREGATE_RULE, add ENUMERABLE_SORTED_AGGREGATE_RULE and verify that it fails because it cannot be implemented?

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants