Skip to content

[CALCITE-7558] Lattice.Measure.compareTo() collapses distinct aggregates that share the same name - #5219

Merged
mihaibudiu merged 1 commit into
apache:mainfrom
1fanwang:calcite-7558-measure-ordering
Aug 25, 2026
Merged

[CALCITE-7558] Lattice.Measure.compareTo() collapses distinct aggregates that share the same name#5219
mihaibudiu merged 1 commit into
apache:mainfrom
1fanwang:calcite-7558-measure-ordering

Conversation

@1fanwang

Copy link
Copy Markdown
Contributor

Jira Link

CALCITE-7558

Changes Proposed

A lattice can contain a built-in aggregate and a user-defined aggregate with the same name. Lattice.Measure.compareTo() treated those measures as equal even when SqlOperator.equals() did not, so the builder's TreeSet silently discarded one of them. Current main retains one of three unequal measures in the regression; this branch retains all three.

The natural order still compares arguments, aggregate name, and the distinct flag in the same order as before. Aggregate kind and implementation class now break only ties that previously collapsed unequal measures.

Testing

$ SOURCE=core/src/main/java/org/apache/calcite/materialize/Lattice.java
$ git checkout c46c70cb26 -- "$SOURCE"
$ JAVA_HOME=$(/usr/libexec/java_home -v 21) ./gradlew :core:test \
    --tests 'org.apache.calcite.materialize.LatticeSuggesterTest.testMeasureNaturalOrderingKeepsDistinctAggregatorsWithSameName' \
    --no-daemon --console=plain
Expected: a collection with size <3>
     but: collection size was <1>

$ git checkout a8981422fa -- "$SOURCE"
$ JAVA_HOME=$(/usr/libexec/java_home -v 21) ./gradlew :core:test \
    --tests 'org.apache.calcite.materialize.LatticeSuggesterTest.testMeasureNaturalOrderingKeepsDistinctAggregatorsWithSameName' \
    --no-daemon --console=plain
0.1sec, 1 completed, 0 failed, 0 skipped
BUILD SUCCESSFUL

…tes that share the same name

Two unequal measures can share their argument list, aggregate name, and distinct flag. The builder stores measures in a TreeSet, so the natural-order tie silently drops one aggregate definition.

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

Copy link
Copy Markdown

@1fanwang 1fanwang changed the title [CALCITE-7558] Preserve distinct same-name lattice measures [CALCITE-7558] Lattice.Measure.compareTo() collapses distinct aggregates that share the same name Aug 25, 2026
@mihaibudiu mihaibudiu added the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label Aug 25, 2026
@mihaibudiu
mihaibudiu merged commit 28231ea into apache:main Aug 25, 2026
36 of 51 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.

2 participants