Skip to content

fix: do not treat a UNIQUE constraint with a nullable column as unique when checking orderings - #27620

Merged
2010YOUY01 merged 3 commits into
apache:mainfrom
asolimando:asolimando/fix-unique-nullable-ordering
Nov 25, 2026
Merged

2010YOUY01 merged 3 commits into
apache:mainfrom
asolimando:asolimando/fix-unique-nullable-ordering

Conversation

@asolimando

@asolimando asolimando commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Which issue does this PR close?

Rationale for this change

A query on a table with a UNIQUE constraint on a nullable column and a declared sort order (WITH ORDER) can return rows in the wrong order. See the issue for the reproducer.

EquivalenceProperties::satisfied_by_constraints and satisfied_by_constraints_ordering decide whether a PRIMARY KEY or UNIQUE constraint lets the input ordering satisfy a sort requirement, so that the sort can be removed. For a UNIQUE constraint, they accept a nullable key column when it is neither the first nor the last column of the ordering. This is not correct at any position: SQL UNIQUE allows several rows with NULL in a key column, so these rows are not unique on the key.

In the issue, the input is ordered by [a, b, c] with UNIQUE (a, b) and b nullable. b is in the middle of the ordering, so the requirement [a, b, c, v] is considered satisfied and the SortExec is removed. The two rows (1, NULL, 0, 2) and (1, NULL, 0, 1) tie on [a, b, c], so only the sort on v can order them.

What changes are included in this PR?

  • New Constraint::implies_unique_sort_prefix (internal use only): true for a PRIMARY KEY, and for a UNIQUE constraint only if none of its key columns is nullable. The doc of Constraint::Unique now states that key columns may be NULL.
  • satisfied_by_constraints and satisfied_by_constraints_ordering call it once per constraint, before the orderings are checked, instead of checking nullability by position in each ordering.
  • A new private helper ordering_contains_columns, used by both functions, checks that all key columns are in an ordering. It replaces the map of column positions and nullability that was built for each ordering.

What is the testing strategy for this PR?

  • A new sqllogictest case in order.slt (table unique_nullable) checks the query result and that the SortExec stays in the plan. On main, the query returns the rows in the wrong order and the plan has no SortExec. No existing test changes.
  • A unit test for Constraint::implies_unique_sort_prefix.

Are there any user-facing changes?

Queries on tables with a UNIQUE constraint on a nullable column and a declared sort order return correctly sorted results. No public API changes: the new Constraint::implies_unique_sort_prefix in datafusion-common is internal only (#[doc(hidden)]).


Disclaimer: I used AI to assist in the code generation, I have manually reviewed the output and it matches my intention and understanding.

@asolimando
asolimando marked this pull request as ready for review November 20, 2026 19:37
@github-actions github-actions Bot added physical-expr Changes to the physical-expr crates sqllogictest SQL Logic Tests (.slt) common Related to common crate labels Nov 20, 2026

@2010YOUY01 2010YOUY01 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Makes sense. Thank you!

/// values are treated as equal, as when sorting: always for a primary key,
/// and for a unique key only if none of its columns is nullable in
/// `schema`. See [`FunctionalDependence::is_valid_across_nulls`].
pub fn is_valid_across_nulls(&self, schema: &Schema) -> bool {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That's true, I have added #[doc(hidden)] plus the internal-use section, thanks!

/// values are treated as equal, as when sorting: always for a primary key,
/// and for a unique key only if none of its columns is nullable in
/// `schema`. See [`FunctionalDependence::is_valid_across_nulls`].
pub fn is_valid_across_nulls(&self, schema: &Schema) -> bool {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Optional nit: To make this function more redable, perhaps we can rename it to does_constraint_imply_unique_order_prefix and add a comment with an example:

Given UNIQUE(a, b), where both a and b are non-null, an existing sort prefix (a, b) uniquely identifies each row, making subsequent sort keys redundant.

It might also help to clarify in the Constraint enum documentation that SQL UNIQUE constraints generally allow NULLs. For example, the rows (1, NULL) and (1, NULL) can coexist without violating UNIQUE(a, b).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks, I have adopted the suggest with a slight change (renamed it to implies_unique_sort_prefix as it is already a method on Constraint), added your example to its doc, and documented on Constraint::Unique that key columns may be NULL

…ue-nullable-ordering

# Conflicts:
#	datafusion/sqllogictest/test_files/order.slt

@asolimando asolimando left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks a lot @2010YOUY01, I have applied both suggestions and updated the PR description accordingly, please take a look again when you have time!

/// values are treated as equal, as when sorting: always for a primary key,
/// and for a unique key only if none of its columns is nullable in
/// `schema`. See [`FunctionalDependence::is_valid_across_nulls`].
pub fn is_valid_across_nulls(&self, schema: &Schema) -> bool {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That's true, I have added #[doc(hidden)] plus the internal-use section, thanks!

/// values are treated as equal, as when sorting: always for a primary key,
/// and for a unique key only if none of its columns is nullable in
/// `schema`. See [`FunctionalDependence::is_valid_across_nulls`].
pub fn is_valid_across_nulls(&self, schema: &Schema) -> bool {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks, I have adopted the suggest with a slight change (renamed it to implies_unique_sort_prefix as it is already a method on Constraint), added your example to its doc, and documented on Constraint::Unique that key columns may be NULL

@2010YOUY01
2010YOUY01 added this pull request to the merge queue Nov 25, 2026
Merged via the queue into apache:main with commit 73043ac Nov 25, 2026
42 checks passed
@asolimando
asolimando deleted the asolimando/fix-unique-nullable-ordering branch November 25, 2026 20:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

common Related to common crate physical-expr Changes to the physical-expr crates sqllogictest SQL Logic Tests (.slt) v57.0.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong results: ORDER BY returns unsorted rows for a table with a UNIQUE constraint on a nullable column and WITH ORDER

2 participants