IgnatiusPang commented on code in PR #25746:
URL: https://github.com/apache/datafusion/pull/25746#discussion_r4172079701
##########
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:
Good catch, the wording was ambiguous. The SortExec: TopK(fetch=10) that's
left comes from the inner ORDER BY x LIMIT 10. It's there on purpose so the
projection gets input that's already sorted by x. The sort that gets removed is
the outer ORDER BY l. Compare it with the log(0.5, x) case above, where that
outer SortExec: expr=[l@0 ASC NULLS LAST] is still there. I've reworded both
comments to say which sort they mean.
--
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]