Skip to content

Commit 3feeaa8

Browse files
committed
fix for nulls_first
1 parent 22552df commit 3feeaa8

2 files changed

Lines changed: 28 additions & 10 deletions

File tree

‎datafusion/functions/src/math/log.rs‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ impl ScalarUDFImpl for LogFunc {
8989
(input[0].sort_properties, input[1].sort_properties)
9090
};
9191
match (num_sort_properties, base_sort_properties) {
92-
(first @ SortProperties::Ordered(value), SortProperties::Ordered(base))
93-
if !value.descending && base.descending
94-
|| value.descending && !base.descending =>
92+
(first @ SortProperties::Ordered(num), SortProperties::Ordered(base))
93+
if num.descending != base.descending
94+
&& num.nulls_first == base.nulls_first =>
9595
{
9696
Ok(first)
9797
}
@@ -370,7 +370,7 @@ mod tests {
370370
assert_eq!(result, order.sort_properties);
371371
}
372372

373-
// Test log(base, num)
373+
// Test log(base, num), where `nulls_first` is the same
374374
let mut results = Vec::with_capacity(orders.len() * orders.len());
375375
for base_order in orders.iter() {
376376
for num_order in orders.iter().cloned() {
@@ -430,5 +430,23 @@ mod tests {
430430
SortProperties::Singleton,
431431
];
432432
assert_eq!(results, expected);
433+
434+
// Test with different `nulls_first`
435+
let base_order = ExprProperties::new_unknown().with_order(
436+
SortProperties::Ordered(SortOptions {
437+
descending: true,
438+
nulls_first: true,
439+
}),
440+
);
441+
let num_order = ExprProperties::new_unknown().with_order(
442+
SortProperties::Ordered(SortOptions {
443+
descending: false,
444+
nulls_first: false,
445+
}),
446+
);
447+
assert_eq!(
448+
log.output_ordering(&[base_order, num_order]).unwrap(),
449+
SortProperties::Unordered
450+
);
433451
}
434452
}

‎datafusion/sqllogictest/test_files/order.slt‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ CREATE EXTERNAL TABLE aggregate_test_100 (
519519
)
520520
STORED AS CSV
521521
WITH ORDER(c11)
522-
WITH ORDER(c12 DESC)
522+
WITH ORDER(c12 DESC NULLS LAST)
523523
LOCATION '../../testing/data/csv/aggregate_test_100.csv'
524524
OPTIONS ('format.has_header' 'true');
525525

@@ -566,22 +566,22 @@ physical_plan
566566
01)SortPreservingMergeExec: [log_c11_base_c12@0 ASC NULLS LAST]
567567
02)--ProjectionExec: expr=[log(c12@1, CAST(c11@0 AS Float64)) as log_c11_base_c12]
568568
03)----RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
569-
04)------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100.csv]]}, projection=[c11, c12], output_orderings=[[c11@0 ASC NULLS LAST], [c12@1 DESC]], has_header=true
569+
04)------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100.csv]]}, projection=[c11, c12], output_orderings=[[c11@0 ASC NULLS LAST], [c12@1 DESC NULLS LAST]], has_header=true
570570

571571
query TT
572572
EXPLAIN SELECT LOG(c11, c12) as log_c12_base_c11
573573
FROM aggregate_test_100
574-
ORDER BY log_c12_base_c11 DESC;
574+
ORDER BY log_c12_base_c11 DESC NULLS LAST;
575575
----
576576
logical_plan
577-
01)Sort: log_c12_base_c11 DESC NULLS FIRST
577+
01)Sort: log_c12_base_c11 DESC NULLS LAST
578578
02)--Projection: log(CAST(aggregate_test_100.c11 AS Float64), aggregate_test_100.c12) AS log_c12_base_c11
579579
03)----TableScan: aggregate_test_100 projection=[c11, c12]
580580
physical_plan
581-
01)SortPreservingMergeExec: [log_c12_base_c11@0 DESC]
581+
01)SortPreservingMergeExec: [log_c12_base_c11@0 DESC NULLS LAST]
582582
02)--ProjectionExec: expr=[log(CAST(c11@0 AS Float64), c12@1) as log_c12_base_c11]
583583
03)----RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
584-
04)------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100.csv]]}, projection=[c11, c12], output_orderings=[[c11@0 ASC NULLS LAST], [c12@1 DESC]], has_header=true
584+
04)------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100.csv]]}, projection=[c11, c12], output_orderings=[[c11@0 ASC NULLS LAST], [c12@1 DESC NULLS LAST]], has_header=true
585585

586586
statement ok
587587
drop table aggregate_test_100;

0 commit comments

Comments
 (0)