alamb commented on code in PR #25746:
URL: https://github.com/apache/datafusion/pull/25746#discussion_r4158324838
##########
datafusion/sqllogictest/test_files/order.slt:
##########
@@ -974,9 +977,60 @@ logical_plan
03)----TableScan: aggregate_test_100 projection=[c11, c12]
physical_plan
01)SortPreservingMergeExec: [log_c12_base_c11@0 DESC NULLS LAST]
-02)--ProjectionExec: expr=[log(c11@0, c12@1) as log_c12_base_c11]
-03)----RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1,
maintains_sort_order=true
-04)------DataSourceExec: 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]],
file_type=csv, has_header=true
+02)--SortExec: expr=[log_c12_base_c11@0 DESC NULLS LAST],
preserve_partitioning=[true]
+03)----ProjectionExec: expr=[log(c11@0, c12@1) as log_c12_base_c11]
+04)------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1,
maintains_sort_order=true
+05)--------DataSourceExec: 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]],
file_type=csv, has_header=true
+
+# log with a base in (0, 1) is decreasing, so the sort must not be removed
+# even though the input is sorted by x
+query TT
+EXPLAIN SELECT log(0.5, x) AS l FROM (SELECT x FROM (VALUES (1.0), (2.0),
(4.0), (8.0)) AS t(x) ORDER BY x LIMIT 10) ORDER BY l ASC;
+----
+logical_plan
+01)Sort: l ASC NULLS LAST
+02)--Projection: log(Float64(0.5), t.x) AS l
+03)----Sort: t.x ASC NULLS LAST, fetch=10
+04)------SubqueryAlias: t
+05)--------Projection: column1 AS x
+06)----------Values: (Float64(1)), (Float64(2)), (Float64(4)), (Float64(8))
+physical_plan
+01)SortExec: expr=[l@0 ASC NULLS LAST], preserve_partitioning=[false]
+02)--ProjectionExec: expr=[log(0.5, column1@0) as l]
+03)----SortExec: TopK(fetch=10), expr=[column1@0 ASC NULLS LAST],
preserve_partitioning=[false]
+04)------DataSourceExec: partitions=1, partition_sizes=[1]
+
+query R
+SELECT log(0.5, x) AS l FROM (SELECT x FROM (VALUES (1.0), (2.0), (4.0),
(8.0)) AS t(x) ORDER BY x LIMIT 10) ORDER BY l ASC;
+----
+-3
+-2
+-1
+0
+
+# log with a base > 1 is increasing, so the sort can still be removed
+query TT
+EXPLAIN SELECT log(2.0, x) AS l FROM (SELECT x FROM (VALUES (1.0), (2.0),
(4.0), (8.0)) AS t(x) ORDER BY x LIMIT 10) ORDER BY l ASC;
+----
+logical_plan
+01)Sort: l ASC NULLS LAST
+02)--Projection: log(Float64(2), t.x) AS l
+03)----Sort: t.x ASC NULLS LAST, fetch=10
+04)------SubqueryAlias: t
+05)--------Projection: column1 AS x
+06)----------Values: (Float64(1)), (Float64(2)), (Float64(4)), (Float64(8))
+physical_plan
+01)ProjectionExec: expr=[log(2, column1@0) as l]
+02)--SortExec: TopK(fetch=10), expr=[column1@0 ASC NULLS LAST],
preserve_partitioning=[false]
Review Comment:
the comment says "so the sort can be removed" but I see there is a sort in
this plan 🤔
##########
datafusion/sqllogictest/test_files/order.slt:
##########
@@ -974,9 +977,60 @@ logical_plan
03)----TableScan: aggregate_test_100 projection=[c11, c12]
physical_plan
01)SortPreservingMergeExec: [log_c12_base_c11@0 DESC NULLS LAST]
-02)--ProjectionExec: expr=[log(c11@0, c12@1) as log_c12_base_c11]
-03)----RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1,
maintains_sort_order=true
-04)------DataSourceExec: 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]],
file_type=csv, has_header=true
+02)--SortExec: expr=[log_c12_base_c11@0 DESC NULLS LAST],
preserve_partitioning=[true]
+03)----ProjectionExec: expr=[log(c11@0, c12@1) as log_c12_base_c11]
+04)------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1,
maintains_sort_order=true
+05)--------DataSourceExec: 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]],
file_type=csv, has_header=true
+
+# log with a base in (0, 1) is decreasing, so the sort must not be removed
+# even though the input is sorted by x
+query TT
+EXPLAIN SELECT log(0.5, x) AS l FROM (SELECT x FROM (VALUES (1.0), (2.0),
(4.0), (8.0)) AS t(x) ORDER BY x LIMIT 10) ORDER BY l ASC;
+----
+logical_plan
+01)Sort: l ASC NULLS LAST
+02)--Projection: log(Float64(0.5), t.x) AS l
+03)----Sort: t.x ASC NULLS LAST, fetch=10
+04)------SubqueryAlias: t
+05)--------Projection: column1 AS x
+06)----------Values: (Float64(1)), (Float64(2)), (Float64(4)), (Float64(8))
+physical_plan
+01)SortExec: expr=[l@0 ASC NULLS LAST], preserve_partitioning=[false]
+02)--ProjectionExec: expr=[log(0.5, column1@0) as l]
+03)----SortExec: TopK(fetch=10), expr=[column1@0 ASC NULLS LAST],
preserve_partitioning=[false]
+04)------DataSourceExec: partitions=1, partition_sizes=[1]
+
+query R
+SELECT log(0.5, x) AS l FROM (SELECT x FROM (VALUES (1.0), (2.0), (4.0),
(8.0)) AS t(x) ORDER BY x LIMIT 10) ORDER BY l ASC;
+----
+-3
+-2
+-1
+0
+
+# log with a base > 1 is increasing, so the sort can still be removed
+query TT
+EXPLAIN SELECT log(2.0, x) AS l FROM (SELECT x FROM (VALUES (1.0), (2.0),
(4.0), (8.0)) AS t(x) ORDER BY x LIMIT 10) ORDER BY l ASC;
+----
+logical_plan
+01)Sort: l ASC NULLS LAST
+02)--Projection: log(Float64(2), t.x) AS l
+03)----Sort: t.x ASC NULLS LAST, fetch=10
+04)------SubqueryAlias: t
+05)--------Projection: column1 AS x
+06)----------Values: (Float64(1)), (Float64(2)), (Float64(4)), (Float64(8))
+physical_plan
+01)ProjectionExec: expr=[log(2, column1@0) as l]
+02)--SortExec: TopK(fetch=10), expr=[column1@0 ASC NULLS LAST],
preserve_partitioning=[false]
Review Comment:
maybe it should say "the second sort can be removed"
##########
datafusion/functions/src/math/log.rs:
##########
@@ -813,22 +840,50 @@ mod tests {
assert_eq!(results, expected);
// Test with different `nulls_first`
- let base_order = ExprProperties::new_unknown().with_order(
- SortProperties::Ordered(SortOptions {
+ let base_order = ExprProperties::new_unknown()
+ .with_range(gt_one_range.clone())
+ .with_order(SortProperties::Ordered(SortOptions {
descending: true,
nulls_first: true,
- }),
- );
- let num_order = ExprProperties::new_unknown().with_order(
- SortProperties::Ordered(SortOptions {
+ }));
+ let num_order = ExprProperties::new_unknown()
+ .with_range(gt_one_range.clone())
+ .with_order(SortProperties::Ordered(SortOptions {
descending: false,
nulls_first: false,
- }),
- );
+ }));
assert_eq!(
log.output_ordering(&[base_order, num_order]).unwrap(),
SortProperties::Unordered
);
+
+ // Test base in (0, 1), e.g. base = 0.5:
Review Comment:
I am not sure these tests add additional coverage over the end to end slt
tests, but they don't hurt either
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]