houqp commented on a change in pull request #1023: URL: https://github.com/apache/arrow-datafusion/pull/1023#discussion_r712339261
########## File path: datafusion/src/physical_plan/join_utils.rs ########## @@ -0,0 +1,212 @@ +// 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. + +//! Join related functionality used both on logical and physical plans + +use crate::error::{DataFusionError, Result}; +use crate::logical_plan::JoinType; +use crate::physical_plan::expressions::Column; +use arrow::datatypes::{Field, Schema}; +use std::collections::HashSet; + +/// The on clause of the join, as vector of (left, right) columns. +pub type JoinOn = Vec<(Column, Column)>; +/// Reference for JoinOn. +pub type JoinOnRef<'a> = &'a [(Column, Column)]; + +/// 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: JoinOnRef) -> Result<()> { + let left: HashSet<Column> = left + .fields() + .iter() + .enumerate() + .map(|(idx, f)| Column::new(f.name(), idx)) + .collect(); + let right: HashSet<Column> = right + .fields() + .iter() + .enumerate() + .map(|(idx, f)| Column::new(f.name(), idx)) + .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<Column>, + right: &HashSet<Column>, + on: &[(Column, Column)], +) -> Result<()> { + let on_left = &on.iter().map(|on| on.0.clone()).collect::<HashSet<_>>(); + let left_missing = on_left.difference(left).collect::<HashSet<_>>(); + + let on_right = &on.iter().map(|on| on.1.clone()).collect::<HashSet<_>>(); + let right_missing = on_right.difference(right).collect::<HashSet<_>>(); + + if !left_missing.is_empty() | !right_missing.is_empty() { + return Err(DataFusionError::Plan(format!( + "The left or right side of the join does not have all columns on \"on\": \nMissing on the left: {:?}\nMissing on the right: {:?}", + left_missing, + right_missing, + ))); + }; + + Ok(()) +} + +/// Used in ColumnIndex to distinguish which side the index is for +#[derive(Debug, Clone)] +pub enum JoinSide { + /// Left side of the join + Left, + /// Right side of the join + Right, +} + +/// Information about the index and placement (left or right) of the columns +#[derive(Debug, Clone)] +pub struct ColumnIndex { + /// Index of the column + pub index: usize, + /// Whether the column is at the left or right side + pub side: JoinSide, +} + +/// Creates a schema for a join operation. +/// The fields from the left side are first +pub fn build_join_schema( + left: &Schema, + right: &Schema, + join_type: &JoinType, +) -> (Schema, Vec<ColumnIndex>) { Review comment: this is the main change, we are now returning column index with schema in this function. -- 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]
