kosiew commented on code in PR #27081:
URL: https://github.com/apache/datafusion/pull/27081#discussion_r4752408863


##########
datafusion/physical-plan/src/joins/cross_join.rs:
##########
@@ -91,23 +91,11 @@ pub struct CrossJoinExec {
 impl CrossJoinExec {
     /// Create a new [CrossJoinExec].
     pub fn new(left: Arc<dyn ExecutionPlan>, right: Arc<dyn ExecutionPlan>) -> 
Self {
-        // left then right
-        let (all_columns, metadata) = {
-            let left_schema = left.schema();
-            let right_schema = right.schema();
-            let left_fields = left_schema.fields().iter();
-            let right_fields = right_schema.fields().iter();
-
-            let mut metadata = left_schema.metadata().clone();
-            metadata.extend(right_schema.metadata().clone());
-
-            (
-                left_fields.chain(right_fields).cloned().collect::<Fields>(),
-                metadata,
-            )
-        };
-
-        let schema = 
Arc::new(Schema::new(all_columns).with_metadata(metadata));
+        // Use the shared helper (inner join) so metadata merges the same way 
as
+        // the logical plan; merging it here independently let schemas diverge.
+        let (schema, _) =
+            build_join_schema(&left.schema(), &right.schema(), 
&JoinType::Inner);

Review Comment:
   Could we document the left-biased metadata precedence in the constructor 
docs? This is an observable behavior change for previously successful unswapped 
cross joins.



##########
datafusion/physical-plan/src/joins/cross_join.rs:
##########
@@ -706,9 +694,39 @@ mod tests {
         TestMemoryExec, assert_join_metrics, build_table_i32, 
build_table_scan_i32,
     };
 
+    use arrow::datatypes::{DataType, Field};
     use datafusion_common::{assert_contains, 
test_util::batches_to_sort_string};
     use datafusion_execution::runtime_env::RuntimeEnvBuilder;
     use insta::assert_snapshot;
+    use std::collections::HashMap;
+
+    // On a conflicting schema-metadata key, the cross join keeps the LEFT 
value,
+    // matching `build_join_schema` and the logical plan (mirrors the 
per-join-type
+    // metadata tests #16221 added in joins/utils.rs and the logical 
builder.rs).
+    #[test]
+    fn cross_join_schema_metadata_is_left_biased() {
+        let input = |field: &str, meta_value: &str| {
+            let schema = Arc::new(
+                Schema::new(vec![Field::new(field, DataType::Int32, false)])
+                    .with_metadata(HashMap::from([(
+                        String::from("metadata_key"),
+                        String::from(meta_value),
+                    )])),
+            );
+            TestMemoryExec::try_new_exec(&[vec![]], schema, None).unwrap()
+        };
+
+        let join =
+            CrossJoinExec::new(input("a", "left value"), input("b", "right 
value"));
+
+        assert_eq!(

Review Comment:
   Consider adding a unique metadata key to each input and asserting the full 
output map to verify that non-conflicting metadata from both inputs is 
preserved.



##########
datafusion/sqllogictest/test_files/metadata.slt:
##########
@@ -693,5 +693,27 @@ drop table table_with_metadata;
 # Test that metadata on conflicting values raises an error.
 # The larger_table has 10 values, smaller_tables 1 value and the fields of 
each table
 # have conflicting metadata, same key different values See test:context.rs 
register_conflicting_metadata_tables
-statement error DataFusion error: PhysicalOptimizer rule 'join_selection' 
failed\. Schema mismatch\.\ncaused by\nInternal error: Schema metadata 
mismatch: Expected original metadata: \{"metadata_key": "right"\}, got 
metadata: \{"metadata_key": "left"\}
+statement error DataFusion error: PhysicalOptimizer rule 'join_selection' 
failed\. Schema mismatch\.\ncaused by\nInternal error: Schema metadata 
mismatch: Expected original metadata: \{"metadata_key": "left"\}, got metadata: 
\{"metadata_key": "right"\}

Review Comment:
   Could we rename the test heading to 'Reject a cross-join input swap that 
changes schema-level metadata'? This more accurately describes the behavior 
being tested.



-- 
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]

Reply via email to