mingmwang commented on code in PR #3855: URL: https://github.com/apache/arrow-datafusion/pull/3855#discussion_r998927646
########## datafusion/core/src/physical_optimizer/enforcement.rs: ########## @@ -0,0 +1,858 @@ +// 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. + +//! Enforcement optimizer rules are used to make sure the plan's Distribution and Ordering +//! requirements are met by inserting necessary [[RepartitionExec]] and [[SortExec]]. +//! +use crate::error::Result; +use crate::physical_optimizer::utils::transform_up; +use crate::physical_optimizer::PhysicalOptimizerRule; +use crate::physical_plan::coalesce_partitions::CoalescePartitionsExec; +use crate::physical_plan::repartition::RepartitionExec; +use crate::physical_plan::sorts::sort::SortExec; +use crate::physical_plan::{with_new_children_if_necessary, Distribution, ExecutionPlan}; +use crate::prelude::SessionConfig; +use datafusion_physical_expr::expressions::Column; +use datafusion_physical_expr::{ + normalize_sort_expr_with_equivalence_properties, PhysicalSortExpr, +}; +use std::sync::Arc; + +/// BasicEnforcement rule, it ensures the Distribution and Ordering requirements are met +/// in the strictest way. It might add additional [[RepartitionExec]] to the plan tree +/// and give a non-optimal plan, but it can avoid the possible data skew in joins +/// +/// For example for a HashJoin with keys(a, b, c), the required Distribution(a, b, c) can be satisfied by +/// several alternative partitioning ways: [(a, b, c), (a, b), (a, c), (b, c), (a), (b), (c), ( )]. +/// +/// This rule only chooses the exactly match and satisfies the Distribution(a, b, c) by a HashPartition(a, b, c). +#[derive(Default)] +pub struct BasicEnforcement {} Review Comment: In some Cascade style optimizers, they have the enforcement rules which do the similar things. So I just name the rule to `BasicEnforcement`, and this is for Phase1. In the following PRs, I will also try to implement a more dynamic enforcement rule which will consider different alternative partitioning options and choose a best plan which will have the least number of RepartitionExec as the goal. Following are some papers I read recently, I will try to refer to them and implement the `DynamicEnforcement` rule. Incorporating Partitioning and Parallel Plans into the SCOPE Optimizer http://www.cs.albany.edu/~jhh/courses/readings/zhou10.pdf Another recent paper, see the **EXCHANGE PLACEMENT** sections. https://vldb.org/pvldb/vol15/p936-rajan.pdf And we can have an optimizer conf option like 'perfer_join_keys_eactly_match', if this option is set to `true`, DataFusion physical optimizer will run `BasicEnforcement`, otherwise run `DynamicEnforcement`. -- 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]
