2010YOUY01 commented on code in PR #15301:
URL: https://github.com/apache/datafusion/pull/15301#discussion_r2024728505


##########
datafusion/physical-optimizer/src/filter_pushdown.rs:
##########
@@ -0,0 +1,190 @@
+// 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 std::sync::Arc;
+
+use datafusion_common::{config::ConfigOptions, Result};
+use datafusion_physical_expr::PhysicalExpr;
+use datafusion_physical_plan::{
+    execution_plan::{ExecutionPlanFilterPushdownResult, FilterPushdownSupport},
+    with_new_children_if_necessary, ExecutionPlan,
+};
+
+use crate::PhysicalOptimizerRule;
+
+#[derive(Clone, Copy, Debug)]
+enum FilterPushdownSupportState {

Review Comment:
   A short doc comment would be helpful.



##########
datafusion/physical-plan/src/sorts/sort.rs:
##########
@@ -1226,12 +1242,23 @@ impl ExecutionPlan for SortExec {
                     context.runtime_env(),
                     &self.metrics_set,
                 )?;
+                let dynamic_filter_source = 
Arc::clone(&self.dynamic_filter_source);
+                let enable_dynamic_filter_pushdown = context
+                    .session_config()
+                    .options()
+                    .optimizer
+                    .enable_dynamic_filter_pushdown;
                 Ok(Box::pin(RecordBatchStreamAdapter::new(
                     self.schema(),
                     futures::stream::once(async move {
                         while let Some(batch) = input.next().await {
                             let batch = batch?;
                             topk.insert_batch(batch)?;
+                            if enable_dynamic_filter_pushdown {

Review Comment:
   I think the code to update the current executor's dynamic filter can be 
extracted to a separate function like `self.maybe_update_dynamic_filter(...)`



##########
datafusion/physical-plan/src/execution_plan.rs:
##########
@@ -467,8 +467,74 @@ pub trait ExecutionPlan: Debug + DisplayAs + Send + Sync {
     ) -> Result<Option<Arc<dyn ExecutionPlan>>> {
         Ok(None)
     }
+
+    /// Returns a set of filters that this operator owns but would like to be 
pushed down.
+    /// For example, a `TopK` operator may produce dynamic filters that 
reference it's currrent state,
+    /// while a `FilterExec` will just hand of the filters it has as is.
+    /// The default implementation returns an empty vector.
+    fn filters_for_pushdown(&self) -> Result<Vec<Arc<dyn PhysicalExpr>>> {
+        Ok(Vec::new())
+    }
+
+    /// After we've attempted to push down filters into this node's children
+    /// this will be called with the result for each filter that this node 
gave in `filters_for_pushdown`.
+    /// The node should update itself to possibly drop filters that were 
pushed down as `Exact`.
+    fn with_filter_pushdown_result(
+        self: Arc<Self>,
+        _pushdown: &[FilterPushdownSupport],
+    ) -> Result<Option<Arc<dyn ExecutionPlan>>> {
+        Ok(None)
+    }
+
+    /// Push down the given filters into this `ExecutionPlan`.
+    /// This is called after `with_filter_pushdown_result`.
+    /// Operators can accept filters from their parents, either as Exact or 
Unsupported.
+    /// If the operator accepts a filter as Exact, it should return a new 
`ExecutionPlan` with the filter applied
+    /// and the parent that generated the filter might not apply it anymore.
+    fn push_down_filters_from_parents(
+        &self,
+        _filters: &[&Arc<dyn PhysicalExpr>],
+    ) -> Result<Option<ExecutionPlanFilterPushdownResult>> {
+        Ok(None)
+    }
+
+    /// Returns `true` if this `ExecutionPlan` allows filter pushdown to flow 
throught it and `false` otherwise.
+    /// Nodes such as aggregations cannot have filters pushed down through 
them, so they return `false`.
+    /// On the other hand nodes such as repartitions can have filters pushed 
down through them, so they return `true`.
+    /// The default implementation returns `false`.
+    fn supports_filter_pushdown(&self) -> bool {
+        false
+    }
 }
 
+#[derive(Debug, Clone, Copy)]
+pub enum FilterPushdownSupport {

Review Comment:
   A short doc comment would be good to add for this struct.



##########
datafusion/physical-plan/src/execution_plan.rs:
##########
@@ -467,8 +467,74 @@ pub trait ExecutionPlan: Debug + DisplayAs + Send + Sync {
     ) -> Result<Option<Arc<dyn ExecutionPlan>>> {
         Ok(None)
     }
+
+    /// Returns a set of filters that this operator owns but would like to be 
pushed down.
+    /// For example, a `TopK` operator may produce dynamic filters that 
reference it's currrent state,
+    /// while a `FilterExec` will just hand of the filters it has as is.
+    /// The default implementation returns an empty vector.

Review Comment:
   There are two things that can be better explained in the comment:
   - For filter expressions, if it's evaluated to true on one entry, should it 
be kept or discarded.
   - Why is a vector of multiple expressions being returned?



##########
datafusion/physical-optimizer/src/filter_pushdown.rs:
##########
@@ -0,0 +1,190 @@
+// 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 std::sync::Arc;
+
+use datafusion_common::{config::ConfigOptions, Result};
+use datafusion_physical_expr::PhysicalExpr;
+use datafusion_physical_plan::{
+    execution_plan::{ExecutionPlanFilterPushdownResult, FilterPushdownSupport},
+    with_new_children_if_necessary, ExecutionPlan,
+};
+
+use crate::PhysicalOptimizerRule;
+
+#[derive(Clone, Copy, Debug)]
+enum FilterPushdownSupportState {
+    ChildExact,
+    ChildInexact,
+    NoChild,
+}
+
+impl FilterPushdownSupportState {
+    fn combine_with_other(
+        &self,
+        other: &FilterPushdownSupport,
+    ) -> FilterPushdownSupportState {
+        match (other, self) {
+            (FilterPushdownSupport::Exact, 
FilterPushdownSupportState::NoChild) => {
+                FilterPushdownSupportState::ChildExact
+            }
+            (FilterPushdownSupport::Exact, 
FilterPushdownSupportState::ChildInexact) => {
+                FilterPushdownSupportState::ChildInexact
+            }
+            (FilterPushdownSupport::Inexact, 
FilterPushdownSupportState::NoChild) => {
+                FilterPushdownSupportState::ChildInexact
+            }
+            (FilterPushdownSupport::Inexact, 
FilterPushdownSupportState::ChildExact) => {
+                FilterPushdownSupportState::ChildInexact
+            }
+            (
+                FilterPushdownSupport::Inexact,
+                FilterPushdownSupportState::ChildInexact,
+            ) => FilterPushdownSupportState::ChildInexact,
+            (FilterPushdownSupport::Exact, 
FilterPushdownSupportState::ChildExact) => {
+                // If both are exact, keep it as exact
+                FilterPushdownSupportState::ChildExact
+            }
+        }
+    }
+}
+
+fn pushdown_filters(

Review Comment:
   It would be great to doc the high-level procedure inside 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: github-unsubscr...@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to