Skip to content

[CALCITE-6089] EnumerableSortedAggregate fails with ClassCastException: class X cannot be cast to class org.apache.calcite.runtime.FlatLists$ComparableList - #5222

Open
1fanwang wants to merge 1 commit into
apache:mainfrom
1fanwang:calcite-6089-sorted-agg-union
Open

Conversation

@1fanwang

Copy link
Copy Markdown
Contributor

Jira Link

CALCITE-6089

Changes Proposed

Counting rows over a UNION ALL failed at runtime when the sorted aggregate was
used:

java.lang.ClassCastException: class java.lang.Integer cannot be cast to class
org.apache.calcite.runtime.FlatLists$ComparableList

The query now returns c=4.

The aggregate has no group key. The rule used to convert it anyway, and
comparing the zero-length keys threw. CALCITE-6087 made the rule decline an
empty group set, so no sorted aggregate is produced, the planner uses the
regular aggregate, and the query succeeds. That fix therefore closed this issue
too, so this PR adds only the regression tests requested when #5202 merged.

Two tests, following the pattern that patch established. One forces the sorted
rule and asserts the planner now rejects the plan; the other runs the query with
the usual rule set and asserts the result.

Testing

Running the suite on this branch, which is current main plus the two new tests:

$ ./gradlew :core:test --tests '*EnumerableSortedAggregateTest'
15 completed, 0 failed, 0 skipped
BUILD SUCCESSFUL

Reverting only the one-line rule change from CALCITE-6087, and keeping the new
tests, reproduces the original failure:

$ git checkout bd65b15fc6^ -- core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableSortedAggregateRule.java
$ ./gradlew :core:test --tests '*EnumerableSortedAggregateTest.sortedAggCountUnion*'
FAILURE sortedAggCountUnionRejectsEmptyGroupSet()
  but: was "java.sql.SQLException: Error while executing SQL ...:
  class java.lang.Integer cannot be cast to class
  org.apache.calcite.runtime.FlatLists$ComparableList"
2 completed, 1 failed

Are there any user-facing changes?

No. Tests only.

@1fanwang 1fanwang changed the title [CALCITE-6089] Add regression tests for groupless sorted aggregate [CALCITE-6089] EnumerableSortedAggregate fails with ClassCastException: class X cannot be cast to class org.apache.calcite.runtime.FlatLists$ComparableList Aug 25, 2026

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

I approved, but please simplify the useless comments.

* EnumerableSortedAggregate fails with ClassCastException: class X cannot be
* cast to class org.apache.calcite.runtime.FlatLists$ComparableList</a>.
*
* <p>The aggregate has no group key, so since

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.

I don't think we care about what used to happen previously. We are not keeping a log of all past bugs.

@mihaibudiu mihaibudiu added the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label Aug 25, 2026
@mihaibudiu

Copy link
Copy Markdown
Contributor

please squash the commits for merging

…n: class X cannot be cast to class org.apache.calcite.runtime.FlatLists$ComparableList

Signed-off-by: 1fanwang <1fannnw@gmail.com>
@1fanwang
1fanwang force-pushed the calcite-6089-sorted-agg-union branch from fd7f2d0 to 6319f71 Compare August 26, 2026 05:03
@sonarqubecloud

Copy link
Copy Markdown

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