Skip to content

fix: do not remove DISTINCT based on a nullable UNIQUE constraint#1

Merged
simonvandel merged 1 commit into
simonvandel:push-omqkkpttorszfrom
alamb:distinct-nullable-unique-followup
Jul 16, 2026
Merged

fix: do not remove DISTINCT based on a nullable UNIQUE constraint#1
simonvandel merged 1 commit into
simonvandel:push-omqkkpttorszfrom
alamb:distinct-nullable-unique-followup

Conversation

@alamb

@alamb alamb commented Jul 16, 2026

Copy link
Copy Markdown

Proposed update to

Fixes this bug:

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

Before this PR: returned two NULL rows (three in total)

After this PR: returns 1 null row (two in total)

This was written by claude code and reviewed by me

`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>
@simonvandel
simonvandel merged commit b4719a4 into simonvandel:push-omqkkpttorsz Jul 16, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants