andygrove commented on a change in pull request #8709:
URL: https://github.com/apache/arrow/pull/8709#discussion_r527735046



##########
File path: rust/datafusion/src/physical_plan/hash_utils.rs
##########
@@ -0,0 +1,150 @@
+// 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.
+
+//! Functionality used both on logical and physical plans
+
+use crate::error::{DataFusionError, Result};
+use arrow::datatypes::{Field, Schema};
+use std::collections::HashSet;
+
+/// All valid types of joins.
+#[derive(Clone, Debug)]
+pub enum JoinType {
+    /// Inner join
+    Inner,
+}
+
+/// The on clause of the join, as vector of (left, right) columns.
+pub type JoinOn<'a> = [(&'a str, &'a str)];
+
+/// Checks whether the schemas "left" and "right" and columns "on" represent a 
valid join.
+/// They are valid whenever their columns' intersection equals the set `on`
+pub fn check_join_is_valid(left: &Schema, right: &Schema, on: &JoinOn) -> 
Result<()> {
+    let left: HashSet<String> = left.fields().iter().map(|f| 
f.name().clone()).collect();
+    let right: HashSet<String> =
+        right.fields().iter().map(|f| f.name().clone()).collect();
+
+    check_join_set_is_valid(&left, &right, on)
+}
+
+/// Checks whether the sets left, right and on compose a valid join.
+/// They are valid whenever their intersection equals the set `on`
+fn check_join_set_is_valid(
+    left: &HashSet<String>,
+    right: &HashSet<String>,
+    on: &JoinOn,
+) -> Result<()> {
+    if on.len() == 0 {
+        return Err(DataFusionError::Plan(
+            "The 'on' clause of a join cannot be empty".to_string(),
+        ));
+    }
+    let on_left = &on.iter().map(|on| 
on.0.to_string()).collect::<HashSet<_>>();
+    let left_missing = left.difference(on_left).collect::<HashSet<_>>();
+
+    let on_right = &on.iter().map(|on| 
on.1.to_string()).collect::<HashSet<_>>();
+    let right_missing = right.difference(on_right).collect::<HashSet<_>>();
+
+    if (left_missing.len() > 0) | (right_missing.len() > 0) {
+        return Err(DataFusionError::Plan(format!(
+                "The left or right side of the join does not have columns 
\"on\": \nMissing on the left: {:?}\nMissing on the right: {:?}",
+                left_missing,
+                right_missing,
+            )));
+    };
+    Ok(())
+}
+
+/// Creates a schema for a join operation.
+/// The fields from the left side are first
+pub fn build_join_schema(
+    left: &Schema,
+    right: &Schema,
+    on: &JoinOn,
+    join_type: &JoinType,
+) -> Schema {
+    let fields: Vec<Field> = match join_type {
+        JoinType::Inner => {
+            let on_left = &on.iter().map(|on| 
on.0.to_string()).collect::<HashSet<_>>();
+            let on_right = &on.iter().map(|on| 
on.1.to_string()).collect::<HashSet<_>>();
+
+            // inner: all fields are there
+            let left_fields =
+                left.fields().iter().filter(|f| !on_left.contains(f.name()));
+
+            let right_fields = right
+                .fields()
+                .iter()
+                .filter(|f| !on_right.contains(f.name()));
+
+            // left then right
+            left_fields.chain(right_fields).map(|f| f.clone()).collect()
+        }
+    };
+    Schema::new(fields)

Review comment:
       @jorgecarleitao I think the join keys are being dropped from the schema?




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

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to