mingmwang commented on code in PR #4192:
URL: https://github.com/apache/arrow-datafusion/pull/4192#discussion_r1021089704


##########
datafusion/optimizer/src/propagate_empty_relation.rs:
##########
@@ -0,0 +1,331 @@
+// 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.
+
+use datafusion_common::{DataFusionError, Result};
+use datafusion_expr::logical_plan::LogicalPlan;
+use datafusion_expr::{EmptyRelation, JoinType, Projection};
+use std::sync::Arc;
+
+use crate::{utils, OptimizerConfig, OptimizerRule};
+
+/// Optimization rule that bottom-up to eliminate plan by propagating 
empty_relation.
+#[derive(Default)]
+pub struct PropagateEmptyRelation;
+
+impl PropagateEmptyRelation {
+    #[allow(missing_docs)]
+    pub fn new() -> Self {
+        Self {}
+    }
+}
+
+impl OptimizerRule for PropagateEmptyRelation {
+    fn optimize(
+        &self,
+        plan: &LogicalPlan,
+        optimizer_config: &mut OptimizerConfig,
+    ) -> Result<LogicalPlan> {
+        // optimize child plans first
+        let optimized_children_plan =
+            utils::optimize_children(self, plan, optimizer_config)?;
+        match &optimized_children_plan {
+            LogicalPlan::EmptyRelation(_) => Ok(optimized_children_plan),
+            LogicalPlan::Projection(_)
+            | LogicalPlan::Filter(_)
+            | LogicalPlan::Window(_)
+            | LogicalPlan::Sort(_)
+            | LogicalPlan::SubqueryAlias(_)
+            | LogicalPlan::Repartition(_)
+            | LogicalPlan::Limit(_) => match 
empty_child(&optimized_children_plan)? {
+                Some(empty) => Ok(empty),
+                None => Ok(optimized_children_plan),
+            },
+            LogicalPlan::CrossJoin(_) => {
+                let (left_empty, right_empty) = 
binary_plan_children_is_empty(plan)?;
+                if left_empty || right_empty {
+                    Ok(LogicalPlan::EmptyRelation(EmptyRelation {
+                        produce_one_row: false,
+                        schema: plan.schema().clone(),
+                    }))
+                } else {
+                    Ok(optimized_children_plan)
+                }
+            }
+            LogicalPlan::Join(join) => {
+                // TODO: For Join, more join type need to be careful:
+                // For LeftOuter/LeftSemi/LeftAnti Join, only the left side is 
empty, the Join result is empty.
+                // For LeftSemi Join, if the right side is empty, the Join 
result is empty.
+                // For LeftAnti Join, if the right side is empty, the Join 
result is left side(should exclude null ??).
+                // For RightOuter/RightSemi/RightAnti Join, only the right 
side is empty, the Join result is empty.
+                // For RightSemi Join, if the left side is empty, the Join 
result is empty.
+                // For RightAnti Join, if the left side is empty, the Join 
result is right side(should exclude null ??).
+                // For Full Join, only both sides are empty, the Join result 
is empty.
+                // For LeftOut/Full Join, if the right side is empty, the Join 
can be eliminated with a Projection with left side
+                // columns + right side columns replaced with null values.
+                // For RightOut/Full Join, if the left side is empty, the Join 
can be eliminated with a Projection with right side
+                // columns + left side columns replaced with null values.
+                if join.join_type == JoinType::Inner {
+                    let (left_empty, right_empty) = 
binary_plan_children_is_empty(plan)?;
+                    if left_empty || right_empty {
+                        Ok(LogicalPlan::EmptyRelation(EmptyRelation {
+                            produce_one_row: false,
+                            schema: plan.schema().clone(),
+                        }))
+                    } else {
+                        Ok(optimized_children_plan)
+                    }
+                } else {
+                    Ok(optimized_children_plan)
+                }
+            }
+            LogicalPlan::Union(union) => {
+                let (left_empty, right_empty) = 
binary_plan_children_is_empty(plan)?;
+                if left_empty && right_empty {

Review Comment:
   I'm not sure whether DataFusion's Union is binary or can have more than 2 
inputs.
   Based on the doc, looks like it can accept multiple inputs.
   
   ````
       /// Union multiple inputs
       Union(Union),
   
   ````



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

Reply via email to