mingmwang commented on code in PR #5322: URL: https://github.com/apache/arrow-datafusion/pull/5322#discussion_r1116776024
########## datafusion/core/src/physical_plan/joins/hash_join_utils.rs: ########## @@ -0,0 +1,598 @@ +// 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. + +//! This file contains common subroutines for regular and symmetric hash join +//! related functionality, used both in join calculations and optimization rules. + +use std::collections::HashMap; +use std::sync::Arc; +use std::usize; + +use arrow::datatypes::SchemaRef; + +use datafusion_common::DataFusionError; +use datafusion_physical_expr::expressions::Column; +use datafusion_physical_expr::intervals::Interval; +use datafusion_physical_expr::rewrite::TreeNodeRewritable; +use datafusion_physical_expr::{PhysicalExpr, PhysicalSortExpr}; + +use crate::common::Result; +use crate::physical_plan::joins::utils::{JoinFilter, JoinSide}; + +fn check_filter_expr_contains_sort_information( + expr: &Arc<dyn PhysicalExpr>, + reference: &Arc<dyn PhysicalExpr>, +) -> bool { + expr.eq(reference) + || expr + .children() + .iter() + .any(|e| check_filter_expr_contains_sort_information(e, reference)) +} + +fn collect_columns_recursive(expr: &Arc<dyn PhysicalExpr>, columns: &mut Vec<Column>) { + if let Some(column) = expr.as_any().downcast_ref::<Column>() { + if !columns.iter().any(|c| c.eq(column)) { + columns.push(column.clone()) + } + } + expr.children() + .iter() + .for_each(|e| collect_columns_recursive(e, columns)) +} + +fn collect_columns(expr: &Arc<dyn PhysicalExpr>) -> Vec<Column> { + let mut columns = vec![]; + collect_columns_recursive(expr, &mut columns); + columns +} + +/// Create a one to one mapping from main columns to filter columns using +/// filter column indices. A column index looks like: +/// ```text +/// ColumnIndex { +/// index: 0, // field index in main schema +/// side: JoinSide::Left, // child side +/// } +/// ``` +pub fn map_origin_col_to_filter_col( + filter: &JoinFilter, + schema: &SchemaRef, + side: &JoinSide, +) -> Result<HashMap<Column, Column>> { + let filter_schema = filter.schema(); + let mut col_to_col_map: HashMap<Column, Column> = HashMap::new(); + for (filter_schema_index, index) in filter.column_indices().iter().enumerate() { + if index.side.eq(side) { + // Get the main field from column index: + let main_field = schema.field(index.index); + // Create a column expression: + let main_col = Column::new_with_schema(main_field.name(), schema.as_ref())?; + // Since the order of by filter.column_indices() is the same with + // that of intermediate schema fields, we can get the column directly. + let filter_field = filter_schema.field(filter_schema_index); + let filter_col = Column::new(filter_field.name(), filter_schema_index); + // Insert mapping: + col_to_col_map.insert(main_col, filter_col); + } + } + Ok(col_to_col_map) +} + +/// This function analyzes [PhysicalSortExpr] graphs with respect to monotonicity +/// (sorting) properties. This is necessary since monotonically increasing and/or +/// decreasing expressions are required when using join filter expressions for +/// data pruning purposes. +/// +/// The method works as follows: +/// 1. Maps the original columns to the filter columns using the `map_origin_col_to_filter_col` function. +/// 2. Collects all columns in the sort expression using the `PhysicalExprColumnCollector` visitor. +/// 3. Checks if all columns are included in the `column_mapping_information` map. +/// 4. If all columns are included, the sort expression is converted into a filter expression using the `transform_up` and `convert_filter_columns` functions. +/// 5. Searches the converted filter expression in the filter expression using the `check_filter_expr_contains_sort_information`. +/// 6. If an exact match is encountered, returns the converted filter expression as `Some(Arc<dyn PhysicalExpr>)`. +/// 7. If all columns are not included or the exact match is not encountered, returns `None`. +/// +/// Examples: +/// Consider the filter expression "a + b > c + 10 AND a + b < c + 100". +/// 1. If the expression "a@ + d@" is sorted, it will not be accepted since the "d@" column is not part of the filter. +/// 2. If the expression "d@" is sorted, it will not be accepted since the "d@" column is not part of the filter. +/// 3. If the expression "a@ + b@ + c@" is sorted, all columns are represented in the filter expression. However, +/// there is no exact match, so this expression does not indicate pruning. +pub fn convert_sort_expr_with_filter_schema( + side: &JoinSide, + filter: &JoinFilter, + schema: &SchemaRef, + sort_expr: &PhysicalSortExpr, +) -> Result<Option<Arc<dyn PhysicalExpr>>> { Review Comment: I think you can also put `EquivalenceProperties` into the consideration, you can do this in the following PR. -- 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]
