Skip to content

fix: distinct_eliminated is rewritten as distinct_on_group_key - #19142

Merged
zhang2014 merged 9 commits into
databendlabs:mainfrom
KKould:fix/distinct_on_group_key
Dec 24, 2025
Merged

fix: distinct_eliminated is rewritten as distinct_on_group_key#19142
zhang2014 merged 9 commits into
databendlabs:mainfrom
KKould:fix/distinct_on_group_key

Conversation

@KKould

@KKould KKould commented Dec 23, 2025

Copy link
Copy Markdown
Member

I hereby agree to the terms of the CLA available at: https://docs.databend.com/dev/policies/cla/

Summary

The previous distinct_eliminated method would incorrectly convert to count when group_distinct is used and arg0 is a group by key. However, in this case, group_distinct can only be 0 or 1.

Tests

  • Unit Test
  • Logic Test
  • Benchmark Test
  • No Test - Explain why

Type of change

  • Bug Fix (non-breaking change which fixes an issue)
  • New Feature (non-breaking change which adds functionality)
  • Breaking Change (fix or feature that could cause existing functionality not to work as expected)
  • Documentation Update
  • Refactoring
  • Performance Improvement
  • Other (please describe):

This change is Reviewable

@KKould
KKould requested review from Copilot, sundy-li and zhang2014 and removed request for sundy-li and zhang2014 December 23, 2025 07:56
@KKould KKould self-assigned this Dec 23, 2025
@KKould
KKould requested review from sundy-li and zhang2014 December 23, 2025 07:56
@github-actions github-actions Bot added the pr-bugfix this PR patches a bug in codebase label Dec 23, 2025

Copilot AI 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.

Pull request overview

This PR fixes a bug where count(distinct key), uniq(key), and count_distinct(key) on a grouping key were incorrectly converted to count(), which would return the group size instead of the correct value (1 for non-null values, 0 for null values).

  • The optimization now correctly rewrites these aggregate functions to scalar expressions that return 1 for non-null grouping keys and 0 for null grouping keys
  • Added logic test cases to verify the correct behavior for both non-nullable and nullable grouping keys
  • Refactored the scalar item handling to support post-aggregate scalar expressions

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tests/sqllogictests/suites/base/03_common/03_0001_select_aggregator.test Added test cases to verify count(distinct k) returns 1 per group (or 0 for NULL) when k is the grouping key
src/query/sql/src/planner/optimizer/optimizers/operator/aggregate/normalize_aggregate.rs Fixed the optimization logic to rewrite distinct aggregates on grouping keys as conditional expressions (1 or 0) instead of incorrectly converting to count()

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@KKould

KKould commented Dec 23, 2025

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@KKould
KKould marked this pull request as ready for review December 23, 2025 18:35

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@zhang2014
zhang2014 merged commit a3fb83a into databendlabs:main Dec 24, 2025
86 checks passed
everpcpc pushed a commit to everpcpc/databend that referenced this pull request Dec 24, 2025
…endlabs#19142)

* fix: distinct_eliminated is rewritten as distinct_on_group_key

* chore: codefmt

* chore: codefmt

* fix: group by set

* fix: explain for agg

* chore: fix explain_native/aggregate.test

* chore: fix explain agg

* fix: stop collapsing distinct on grouping sets to 0/1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix this PR patches a bug in codebase

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants