fix: consider nulls_first when propagating ordered SortProperties - #24206
fix: consider nulls_first when propagating ordered SortProperties#24206Nagato-Yuzuru wants to merge 4 commits into
Conversation
|
The old query plan can lead to this sort error In datafusion-cli Get Order C was silently violated |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #24206 +/- ##
==========================================
- Coverage 81.05% 81.02% -0.03%
==========================================
Files 1106 1106
Lines 382287 384267 +1980
Branches 382287 384267 +1980
==========================================
+ Hits 309851 311364 +1513
- Misses 54121 54560 +439
- Partials 18315 18343 +28 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| OPTIONS ('format.has_header' 'true'); | ||
|
|
||
| query TT | ||
| EXPLAIN SELECT CAST((inc_col>desc_col) as integer) as c from annotated_data_finite_nulls_last order by c; |
There was a problem hiding this comment.
Do we have sqltest like the example in the description covered, like null + numeric -> null, then we expect to see the null first -> unordered
There was a problem hiding this comment.
Thanks for comment. The add case is added.
i found that the + slt would produce an identical plan with and without this patch, and wouldn't guard the change. The add/sub half of the fix is only observable in the unit tests; gt_or_gteq and and_or are the parts reachable end to end.
#23910 made BinaryExpr::arithmetic_sort_properties fall back to Unordered unless it can prove the arithmetic doesn't overflow.
let cannot_overflow = !range.is_unbounded()
&& !unsigned_subtraction_may_underflow(self.op, l_range, r_range, range);
if !wraps_in_domain && (self.fail_on_overflow || cannot_overflow) {
sort_properties
} else {
SortProperties::Unordered
}Here if endpoint null, so range.is_unbounded() holds for any col + col and the result is Unordered regardless of null placement. SortProperties::add's return value is discarded before nulls_first can matter. The SortExec still survives if declaring both columns ASC NULLS FIRST.
Before current patch:
DataFusion CLI v54.1.0
> set datafusion.execution.target_partitions = 1;
0 row(s) fetched.
Elapsed 0.001 seconds.
> COPY (VALUES (NULL, 1), (1, 2), (2, 4), (3, NULL))
TO 'test_files/scratch/order/mixed_null_placement.csv'
OPTIONS ('format.has_header' 'true');
+-------+
| count |
+-------+
| 4 |
+-------+
1 row(s) fetched.
Elapsed 0.002 seconds.
> CREATE EXTERNAL TABLE mixed_null_placement (
a BIGINT,
b BIGINT
)
STORED AS CSV
WITH ORDER (a ASC NULLS FIRST)
WITH ORDER (b ASC NULLS LAST)
LOCATION 'test_files/scratch/order/mixed_null_placement.csv'
OPTIONS ('format.has_header' 'true');
0 row(s) fetched.
Elapsed 0.001 seconds.
> SELECT a, b, a + b AS s FROM mixed_null_placement ORDER BY s ASC NULLS FIRST;
+------+------+------+
| a | b | s |
+------+------+------+
| NULL | 1 | NULL |
| 3 | NULL | NULL |
| 1 | 2 | 3 |
| 2 | 4 | 6 |
+------+------+------+
4 row(s) fetched.
Elapsed 0.003 seconds.
> EXPLAIN SELECT a + b AS s FROM mixed_null_placement ORDER BY s;
+---------------+-------------------------------+
| plan_type | plan |
+---------------+-------------------------------+
| physical_plan | ┌───────────────────────────┐ |
| | │ SortExec │ |
| | │ -------------------- │ |
| | │ s@0 ASC NULLS LAST │ |
| | └─────────────┬─────────────┘ |
| | ┌─────────────┴─────────────┐ |
| | │ DataSourceExec │ |
| | │ -------------------- │ |
| | │ files: 1 │ |
| | │ format: csv │ |
| | └───────────────────────────┘ |
| | |
+---------------+-------------------------------+
1 row(s) fetched.
Elapsed 0.001 seconds.
Which issue does this PR close?
SortPropertiesshould considernulls_first#11596.Rationale for this change
In the original implementation, cases where
null_firstis not same were not correctly handled and were still treated asordered.Example:
asortedASC NULLS FIRST,bsortedASC NULLS LAST:Since
a + bisNULLwherever either input is, the result has nulls at both ends. The previous code returnedOrdered(ASC, nulls_first: true)for this case; it now returnsUnordered.What changes are included in this PR?
Unit Tests were added and issues were fixed. Additional processing was performed on
and_or(see #11596 (comment) )SortProperties::{add, sub, gt_or_gteq, and_or} now propagate an Ordered result only when both operands agree on nulls_first; otherwise they return Unordered.
This is conservative. If one input were known to be non-null the old result could be valid. But SortProperties carries no nullability information, so Unordered is the only sound answer at this layer.
Are these changes tested?
Yes.
Are there any user-facing changes?
No.