adriangb commented on code in PR #15568: URL: https://github.com/apache/datafusion/pull/15568#discussion_r2035754530
########## datafusion/physical-expr/src/expressions/dynamic_filters.rs: ########## @@ -0,0 +1,442 @@ +// 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::{ + any::Any, + fmt::Display, + hash::Hash, + sync::{Arc, RwLock}, +}; + +use crate::{utils::conjunction, PhysicalExpr}; +use arrow::datatypes::{DataType, Schema}; +use datafusion_common::{ + tree_node::{Transformed, TransformedResult, TreeNode}, + Result, +}; +use datafusion_expr::ColumnarValue; +use datafusion_physical_expr_common::physical_expr::{DynEq, DynHash}; + +/// A source of dynamic runtime filters. +/// +/// Operators can create implementations of this trait that get wrapped +/// in [`DynamicFilterPhysicalExpr`] to be pushed down into and through to scans, joins, etc. +/// +/// For example: +/// - A `HashJoin` operator can use this to provide a filter expression +/// that filters out rows from the right side of the join based on the +/// values from the left side. +/// - A `TopK` operator can use this to provide a filter expression +/// that filters out rows from the input based on the values from the +/// top K rows. +/// +/// Initially this trait is intended to be only for internal use as a way to facilitate +/// building [`DynamicFilterPhysicalExpr`]s in the various operators that will be generating +/// dynamic filters. +/// Because of this we've made it a public trait in a private module so that it is only +/// accessible within the crate. +/// If you would like to use this trait in your own code, please open an issue +/// to discuss the use case and we can consider making it public. +pub trait DynamicFilterSource: + Send + Sync + std::fmt::Debug + DynEq + DynHash + Display + 'static +{ + /// Take a snapshot of the current state of filtering, returning a non-dynamic PhysicalExpr. + /// This is used to e.g. serialize dynamic filters across the wire or to pass them into systems + /// that won't use the `PhysicalExpr` API (e.g. matching on the concrete types of the expressions like `PruningPredicate` does). + /// For example, it is expected that this returns a relatively simple expression such as `col1 > 5` for a TopK operator or + /// `col2 IN (1, 2, ... N)` for a HashJoin operator. + fn snapshot_current_filters(&self) -> Result<Vec<Arc<dyn PhysicalExpr>>>; + + fn as_any(&self) -> &dyn Any; +} + +impl PartialEq for dyn DynamicFilterSource { + fn eq(&self, other: &Self) -> bool { + self.dyn_eq(other.as_any()) + } +} + +impl Eq for dyn DynamicFilterSource {} + +/// A wrapper around a [`DynamicFilterSource`] that allows it to be used as a physical expression. +/// This will call [`DynamicFilterSource::snapshot_current_filters`] to get the current filters for each call to +/// [`PhysicalExpr::evaluate`], [`PhysicalExpr::data_type`], and [`PhysicalExpr::nullable`]. +/// It also implements [`PhysicalExpr::snapshot`] by forwarding the call to [`DynamicFilterSource::snapshot_current_filters`]. +#[derive(Debug)] +pub struct DynamicFilterPhysicalExpr { Review Comment: Yes. This becomes an `Arc<dyn PhysicalExpr>` and is what gets pushed down into the source but holds onto a reference to the operator. -- 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