jayzhan211 commented on code in PR #27319:
URL: https://github.com/apache/datafusion/pull/27319#discussion_r4720226673
##########
datafusion/sqllogictest/test_files/equivalence_properties.slt:
##########
@@ -375,6 +375,93 @@ physical_plan
05)--------RepartitionExec: partitioning=RoundRobinBatch(2),
input_partitions=1, maintains_sort_order=true
06)----------StreamingTableExec: partition_sizes=1, projection=[a, b, c],
infinite_source=true, output_ordering=[a@0 ASC NULLS LAST, b@1 ASC NULLS LAST,
c@2 ASC NULLS LAST]
+# A partitioned mark join on a struct field, under an aggregate grouped by the
+# same field. The join output is already hash partitioned on the field, so the
+# aggregate needs no repartition. Projection pushdown then moves the field
+# access into the scan and drops the join key from the join output. The join
+# must still know its partitioning through the equivalent field access, or the
+# plan fails the sanity check.
+statement ok
+set datafusion.execution.target_partitions = 2;
+
+statement ok
+set datafusion.optimizer.hash_join_single_partition_threshold_rows = 0;
+
+statement ok
+COPY (SELECT named_struct('k', 'a') AS s)
+TO 'test_files/scratch/equivalence_properties/mark_join/1.parquet';
+
+statement ok
+COPY (SELECT named_struct('k', 'b') AS s)
+TO 'test_files/scratch/equivalence_properties/mark_join/2.parquet';
+
+statement ok
+CREATE EXTERNAL TABLE mark_join STORED AS PARQUET
+LOCATION 'test_files/scratch/equivalence_properties/mark_join/';
+
+query T rowsort
+SELECT s['k'] FROM mark_join
+WHERE s['k'] = 'a' OR s['k'] IN (SELECT s['k'] FROM mark_join)
+GROUP BY 1;
+----
+a
+b
+
+query TT
+EXPLAIN SELECT s['k'] FROM mark_join
+WHERE s['k'] = 'a' OR s['k'] IN (SELECT s['k'] FROM mark_join)
+GROUP BY 1;
+----
+physical_plan
+01)ProjectionExec: expr=[__datafusion_extracted_2@0 as mark_join.s[k]]
+02)--AggregateExec: mode=SinglePartitioned, gby=[__datafusion_extracted_2@0 as
__datafusion_extracted_2], aggr=[]
+03)----FilterExec: __datafusion_extracted_3@1 = a OR mark@0,
projection=[__datafusion_extracted_2@2]
+04)------ProjectionExec: expr=[mark@0 as mark, get_field(s@1, k) as
__datafusion_extracted_3, get_field(s@1, k) as __datafusion_extracted_2]
+05)--------HashJoinExec: mode=Partitioned, join_type=LeftMark,
on=[(__datafusion_extracted_4@0, mark_join.s[k]@0)], projection=[mark@2, s@1]
+06)----------RepartitionExec: partitioning=Hash([__datafusion_extracted_4@0],
2), input_partitions=2
+07)------------DataSourceExec: file_groups={2 groups:
[[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/equivalence_properties/mark_join/1.parquet],
[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/equivalence_properties/mark_join/2.parquet]]},
projection=[get_field(s@0, k) as __datafusion_extracted_4, s],
file_type=parquet
+08)----------RepartitionExec: partitioning=Hash([mark_join.s[k]@0], 2),
input_partitions=2
+09)------------DataSourceExec: file_groups={2 groups:
[[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/equivalence_properties/mark_join/1.parquet],
[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/equivalence_properties/mark_join/2.parquet]]},
projection=[get_field(s@0, k) as mark_join.s[k]], file_type=parquet,
predicate=DynamicFilter [ empty ], dynamic_rg_pruning=eligible
+
+# The same plan with a join key computed from the field. The join can only
+# project its partitioning if the dropped column inside the key also projects
+# through the equivalent field access.
+query T rowsort
+SELECT s['k'] || 'z' FROM mark_join
+WHERE s['k'] = 'a' OR s['k'] || 'z' IN (SELECT s['k'] || 'z' FROM mark_join)
+GROUP BY 1;
+----
+az
+bz
+
+# A join key that uses the dropped column twice.
+query T rowsort
+SELECT s['k'] || s['k'] FROM mark_join
+WHERE s['k'] = 'a' OR s['k'] || s['k'] IN (SELECT s['k'] || s['k'] FROM
mark_join)
+GROUP BY 1;
+----
+aa
+bb
+
+# A union of two such joins, which InterleaveExec combines by partitioning.
+query T rowsort
+SELECT k FROM (
+ SELECT s['k'] AS k FROM mark_join
+ WHERE s['k'] = 'a' OR s['k'] IN (SELECT s['k'] FROM mark_join)
+ UNION ALL
+ SELECT s['k'] FROM mark_join
+ WHERE s['k'] = 'b' OR s['k'] IN (SELECT s['k'] FROM mark_join)
+) GROUP BY k;
+----
+a
+b
+
+statement ok
+DROP TABLE mark_join;
+
+statement ok
+RESET datafusion.optimizer.hash_join_single_partition_threshold_rows;
Review Comment:
@cetra3 the fallback also changes the ordering path. With `x = s + 1`, a
projection that drops `x` now keeps the ordering as `s + 1`, so `ORDER BY s +
1` no longer needs a `SortExec` (main keeps it). That's correct, but no test
fails if it breaks, and a mistake there returns rows in the wrong order. Could
you add this case? It passes here and fails on main with the extra `SortExec`:
```suggestion
RESET datafusion.optimizer.hash_join_single_partition_threshold_rows;
# x = s + 1 keeps the ordering on x through a projection that drops x, as
# s + 1, so ORDER BY s + 1 needs no sort.
query I
COPY (VALUES (2, 1), (3, 2), (5, 4), (6, 0), (7, 6))
TO 'test_files/scratch/equivalence_properties/shifted.csv'
OPTIONS ('format.has_header' 'true');
----
5
statement ok
CREATE EXTERNAL TABLE shifted (x BIGINT, s BIGINT)
STORED AS CSV
WITH ORDER (x)
LOCATION 'test_files/scratch/equivalence_properties/shifted.csv'
OPTIONS ('format.has_header' 'true');
query I
SELECT s FROM (SELECT x, s FROM shifted WHERE x = s + 1) ORDER BY s + 1;
----
1
2
4
6
query TT
EXPLAIN SELECT s FROM (SELECT x, s FROM shifted WHERE x = s + 1) ORDER BY s
+ 1;
----
physical_plan
01)SortPreservingMergeExec: [s@0 + 1 ASC NULLS LAST]
02)--FilterExec: x@0 = s@1 + 1, projection=[s@1]
03)----RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1,
maintains_sort_order=true
04)------DataSourceExec: file_groups={1 group:
[[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/equivalence_properties/shifted.csv]]},
projection=[x, s], output_ordering=[x@0 ASC NULLS LAST], file_type=csv,
has_header=true
statement ok
DROP TABLE shifted;
```
--
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]