Repository navigation
fix: do not treat a UNIQUE constraint with a nullable column as unique when checking orderings - #27620
Conversation
…e when checking orderings
| /// 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 { |
There was a problem hiding this comment.
I suggest to make it a internal-only public API: https://datafusion.apache.org/contributor-guide/api-health.html#datafusion-internal-public-apis
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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
…UNIQUE NULL semantics
asolimando
left a comment
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
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_constraintsandsatisfied_by_constraints_orderingdecide 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: SQLUNIQUEallows 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]withUNIQUE (a, b)andbnullable.bis in the middle of the ordering, so the requirement[a, b, c, v]is considered satisfied and theSortExecis removed. The two rows(1, NULL, 0, 2)and(1, NULL, 0, 1)tie on[a, b, c], so only the sort onvcan order them.What changes are included in this PR?
Constraint::implies_unique_sort_prefix(internal use only):truefor a PRIMARY KEY, and for a UNIQUE constraint only if none of its key columns is nullable. The doc ofConstraint::Uniquenow states that key columns may be NULL.satisfied_by_constraintsandsatisfied_by_constraints_orderingcall it once per constraint, before the orderings are checked, instead of checking nullability by position in each ordering.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?
sqllogictestcase inorder.slt(tableunique_nullable) checks the query result and that theSortExecstays in the plan. Onmain, the query returns the rows in the wrong order and the plan has noSortExec. No existing test changes.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_prefixindatafusion-commonis 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.