Skip to content

fix: Do not mark GROUP BY derived unique keys as nullable#23636

Open
alamb wants to merge 3 commits into
apache:mainfrom
alamb:fd-groupby-key-not-nullable
Open

fix: Do not mark GROUP BY derived unique keys as nullable#23636
alamb wants to merge 3 commits into
apache:mainfrom
alamb:fd-groupby-key-not-nullable

Conversation

@alamb

@alamb alamb commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

This query is wrong

CREATE TABLE t_uniq (x INT UNIQUE) AS VALUES (NULL), (NULL), (1);
SELECT DISTINCT x FROM t_uniq ORDER BY x NULLS LAST;
-- returns: NULL, NULL, 1   (should be: 1, NULL)

What changes are included in this PR?

Fix bug by correctly computing and consuming FunctionalDependencies

Are these changes tested?

Yes, new sqllogictest cases in group_by.slt cover the wrong-results query, the plan for the nullable UNIQUE case (the DISTINCT is planned as an Aggregate), and that DISTINCT over a PRIMARY KEY is still removed.

Are there any user-facing changes?

No API changes. SELECT DISTINCT over a table with a nullable UNIQUE constraint now returns correct results (at the cost of keeping the aggregation in the plan).

@github-actions github-actions Bot added the common Related to common crate label Jul 16, 2026
@codecov-commenter

codecov-commenter commented Jul 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@95de385). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #23636   +/-   ##
=======================================
  Coverage        ?   80.65%           
=======================================
  Files           ?     1086           
  Lines           ?   366106           
  Branches        ?   366106           
=======================================
  Hits            ?   295279           
  Misses          ?    53234           
  Partials        ?    17593           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

alamb and others added 2 commits July 16, 2026 07:58
GROUP BY treats NULLs as equal, so every key combination -- NULL
included -- occurs in exactly one output row, like a primary key.
Marking the dependency nullable (multiple NULL keys may coexist)
was overly conservative.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
`ReplaceDistinctWithAggregate` removes `Distinct::All` when a
`Dependency::Single` functional dependence covers all input fields.
However a nullable UNIQUE constraint also produces a `Single`-mode
dependence, and SQL UNIQUE permits multiple NULL keys. DISTINCT treats
NULLs as equal, so it must still collapse those rows:

    CREATE TABLE t (x INT UNIQUE) AS VALUES (NULL), (NULL), (1);
    SELECT DISTINCT x FROM t;

returned two NULL rows.

Only remove the DISTINCT when the dependence is non-nullable or none of
its source fields are actually nullable, mirroring the guards already
used by `EliminateJoin` and `Filter::is_scalar`.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@alamb
alamb force-pushed the fd-groupby-key-not-nullable branch from 8b4bc1b to 2b62f29 Compare July 16, 2026 12:05
@github-actions github-actions Bot added optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) labels Jul 16, 2026
@alamb
alamb marked this pull request as ready for review July 16, 2026 14:14
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

common Related to common crate optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect results with unique constraints

2 participants