Skip to content

Incorrect results with unique constraints #23634

Description

@alamb

Is your feature request related to a problem or challenge?

Follow-up to the discussion on #23548 (see #23548 (comment)).

This query gets the wrong answer because a GroupBy is incorrectly removed

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)

Which I verified via duckdb:

(venv) andrewlamb@Andrews-MacBook-Pro-3:~/Software/datafusion$ duckdb
DuckDB v1.5.4 (Variegata)
Enter ".help" for usage hints.
memory D create table t (x int unique);
memory D insert into t values (null);
memory D insert into t values (null);
memory D insert into t values (1);
memory D select * from t;
┌───────┐
│   x   │
│ int32 │
├───────┤
│  NULL │
│  NULL │
│     1 │
└───────┘
memory D select distinct x  from t;
┌───────┐
│   x   │
│ int32 │
├───────┤
│     1 │
│  NULL │
└───────┘
memory D select distinct x  from t order by x nulls last;
┌───────┐
│   x   │
│ int32 │
├───────┤
│     1 │
│  NULL │
└───────┘

Here is a proposed fix targeting this branch for your review

Describe the solution you'd like

Fix the bug

Describe alternatives you've considered

Additional context

Related:

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions