quwin commented on code in PR #26261: URL: https://github.com/apache/datafusion/pull/26261#discussion_r4267083950
########## datafusion/sqllogictest/test_files/sort_merge_join_sort_options.slt: ########## @@ -0,0 +1,105 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + + +########## +## The sort options of a SortMergeJoin follow the ordering both inputs declare on the join keys, +## so no SortExec is needed on top of inputs that are already sorted that way. +########## + +statement ok +set datafusion.optimizer.prefer_hash_join = false; + +statement ok +set datafusion.execution.target_partitions = 2; + +statement ok +COPY (SELECT * FROM (VALUES (3, 'c', 30), (2, 'b', 20), (1, 'a', 10)) AS t(k1, k2, v) ORDER BY k1 DESC NULLS LAST, k2 ASC NULLS FIRST) +TO 'test_files/scratch/sort_merge_join_sort_options/left.parquet'; + +statement ok +COPY (SELECT * FROM (VALUES (3, 'c', 300), (2, 'b', 200), (4, 'd', 400)) AS t(k1, k2, w) ORDER BY k1 DESC NULLS LAST, k2 ASC NULLS FIRST) +TO 'test_files/scratch/sort_merge_join_sort_options/right.parquet'; + +# both inputs sorted `k1 DESC NULLS LAST, k2 ASC NULLS FIRST`: the join adopts these per key options +statement ok +CREATE EXTERNAL TABLE l (k1 INT, k2 VARCHAR, v INT) STORED AS PARQUET +WITH ORDER (k1 DESC NULLS LAST, k2 ASC NULLS FIRST) +LOCATION 'test_files/scratch/sort_merge_join_sort_options/left.parquet'; + +statement ok +CREATE EXTERNAL TABLE r (k1 INT, k2 VARCHAR, w INT) STORED AS PARQUET +WITH ORDER (k1 DESC NULLS LAST, k2 ASC NULLS FIRST) +LOCATION 'test_files/scratch/sort_merge_join_sort_options/right.parquet'; + +query TT +EXPLAIN SELECT l.k1, l.k2, v, w FROM l JOIN r ON l.k1 = r.k1 AND l.k2 = r.k2 +---- +logical_plan +01)Projection: l.k1, l.k2, l.v, r.w +02)--Inner Join: l.k1 = r.k1, l.k2 = r.k2 +03)----TableScan: l projection=[k1, k2, v] +04)----TableScan: r projection=[k1, k2, w] +physical_plan +01)ProjectionExec: expr=[k1@0 as k1, k2@1 as k2, v@2 as v, w@5 as w] +02)--SortMergeJoinExec: join_type=Inner, on=[(k1@0, k1@0), (k2@1, k2@1)] +03)----DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_merge_join_sort_options/left.parquet]]}, projection=[k1, k2, v], output_ordering=[k1@0 DESC NULLS LAST, k2@1 ASC], file_type=parquet +04)----DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_merge_join_sort_options/right.parquet]]}, projection=[k1, k2, w], output_ordering=[k1@0 DESC NULLS LAST, k2@1 ASC], file_type=parquet Review Comment: This expected plan seems to be stale. The focused SQL logic run fails the `EXPLAIN`: DataFusion puts the projection on `SortMergeJoinExec`, rather than emitting a separate `ProjectionExec`. ```suggestion 01)SortMergeJoinExec: join_type=Inner, on=[(k1@0, k1@0), (k2@1, k2@1)], projection=[k1@0, k2@1, v@2, w@5] 02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_merge_join_sort_options/left.parquet]]}, projection=[k1, k2, v], output_ordering=[k1@0 DESC NULLS LAST, k2@1 ASC], file_type=parquet 03)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/sort_merge_join_sort_options/right.parquet]]}, projection=[k1, k2, w], output_ordering=[k1@0 DESC NULLS LAST, k2@1 ASC], file_type=parquet ``` ########## datafusion/sqllogictest/test_files/sort_merge_join.slt: ########## @@ -1029,6 +1029,35 @@ DROP TABLE c; statement ok set datafusion.execution.target_partitions = 4; +# prefer_hash_join = false also selects a sort merge join with a single target partition +statement ok +set datafusion.execution.target_partitions = 1; + +query TT +EXPLAIN SELECT t1.a, t1.b, t2.b FROM t1 JOIN t2 ON t1.a = t2.a +---- +logical_plan +01)Projection: t1.a, t1.b, t2.b +02)--Inner Join: t1.a = t2.a +03)----TableScan: t1 projection=[a, b] +04)----TableScan: t2 projection=[a, b] +physical_plan +01)ProjectionExec: expr=[a@0 as a, b@1 as b, b@3 as b] +02)--SortMergeJoinExec: join_type=Inner, on=[(a@0, a@0)] +03)----SortExec: expr=[a@0 ASC], preserve_partitioning=[false] +04)------DataSourceExec: partitions=1, partition_sizes=[1] +05)----SortExec: expr=[a@0 ASC], preserve_partitioning=[false] +06)------DataSourceExec: partitions=1, partition_sizes=[1] Review Comment: This single-partition `EXPLAIN` snapshot seems to fail. The actual plan puts ```projection=[a@0, b@1, b@3]``` on `SortMergeJoinExec`, rather than emitting the expected `ProjectionExec`. ```suggestion 01)SortMergeJoinExec: join_type=Inner, on=[(a@0, a@0)], projection=[a@0, b@1, b@3] 02)--SortExec: expr=[a@0 ASC], preserve_partitioning=[false] 03)----DataSourceExec: partitions=1, partition_sizes=[1] 04)--SortExec: expr=[a@0 ASC], preserve_partitioning=[false] 05)----DataSourceExec: partitions=1, partition_sizes=[1] ``` -- 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]
