You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "gene-bordegaray (via GitHub)" <gi...@apache.org> on 2026/03/12 10:49:18 UTC
[PR] [WIP] feat: add generic runtime binding for physical expressions [datafusion]
gene-bordegaray opened a new pull request, #20901:
URL: https://github.com/apache/datafusion/pull/20901
## Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
-->
- Closes #.
## Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
-->
## What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
-->
## Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
-->
## Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
<!--
If there are any breaking changes to public APIs, please add the `api change` label.
-->
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: add generic runtime binding for physical expressions [datafusion]
Posted by "gene-bordegaray (via GitHub)" <gi...@apache.org>.
gene-bordegaray commented on PR #20901:
URL: https://github.com/apache/datafusion/pull/20901#issuecomment-4047241948
cc: @adriangb
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: add generic runtime binding for physical expressions [datafusion]
Posted by "adriangb (via GitHub)" <gi...@apache.org>.
adriangb commented on PR #20901:
URL: https://github.com/apache/datafusion/pull/20901#issuecomment-4130289020
> A lot of the reservations I have with the way this PR and #20416 are proposing to extend dynamic filters in functionality arise from the fact that the blast radius of these contributions is pretty big, as they need to rely on extending very central pillars of DataFusion like `PhysicalExpr` for getting the job done, when the actual functionality is scoped to just dynamic filters.
>
> IMHO, If we had a way of contributing functionality to dynamic filters without necessarily touching the very core of DataFusion, the flow of these contributions would be much smoother, and I could imagine how representing dynamic filters, not as `PhysicalExpr` implementations, but as first class `DynamicFilter` structs, has good chances of delivering such vision.
Makes sense. I wonder what the alternative looks like though? If we make them their own thing, do we need to duplicate or break all of the APIs (filter pushdown, remapping child column physical indices... basically anything that touches `PhysicalExpr`)?
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: add generic runtime binding for physical expressions [datafusion]
Posted by "gabotechs (via GitHub)" <gi...@apache.org>.
gabotechs commented on PR #20901:
URL: https://github.com/apache/datafusion/pull/20901#issuecomment-4129375485
I think a lot of the limitations and the weirdness that this PR, https://github.com/apache/datafusion/pull/20331 and https://github.com/apache/datafusion/pull/20416 show steam from the fact that we need "force" dynamic filters to behave as normal `dyn PhysicalExpr`.
At the moment of introducing dynamic filters in this project, implementing `DynamicFilterPhysicalExpr` as a non-special case of `PhysicalExpr` was a good decision, but I wonder if a good evolution would be to treat dynamic filters as "first class citizens", and promote them to `DynamicFilter`, with no chains to `PhysicalExpr` (besides the fact that they **produce** PhysicalExprs instead of **being** PhysicalExprs).
One thing I noticed is that, by modeling dynamic filters as yet another case of `PhysicalExpr`, several needs unrelated to other physical expressions appear, which results in several new methods in the `PhsysicalExpr` trait that are only useful for the dynamic filters case, and are left unimplemented in every other case:
- [PhysicalExpr::snapshot](https://github.com/apache/datafusion/blob/9b726bcf288e7779b855ead5b869b0a69aba5c92/datafusion/physical-expr-common/src/physical_expr.rs#L394-L394)
- [PhysicalExpr::snapshot_generation](https://github.com/apache/datafusion/blob/9b726bcf288e7779b855ead5b869b0a69aba5c92/datafusion/physical-expr-common/src/physical_expr.rs#L410-L410)
Sometimes, they directly cannot be treated as normal `PhysicalExpr`, needing to hold references to the specific `DynamicFilterPhysicalExpr` implementation, rather than a generic `PhysicalExpr`:
- [AggrDynFilter.filter](https://github.com/apache/datafusion/blob/9b726bcf288e7779b855ead5b869b0a69aba5c92/datafusion/physical-plan/src/aggregates/mod.rs#L554-L554)
- [HashJoinExecDynamicFilter.filter](https://github.com/apache/datafusion/blob/9b726bcf288e7779b855ead5b869b0a69aba5c92/datafusion/physical-plan/src/joins/hash_join/exec.rs#L766-L766)
- [TopKDynamicFilters.filter](https://github.com/apache/datafusion/blob/9b726bcf288e7779b855ead5b869b0a69aba5c92/datafusion/physical-plan/src/topk/mod.rs#L145-L145)
A consequence of this, is that further contributions to extend dynamic filters in functionality need to pass through adding new methods to `PhysicalExpr` that are only implemented for `DynamicFilterPhysicalExpr`, and ignored in every other case:
- [PhysicalExpr::as_dedupable](https://github.com/jayshrivastava/datafusion/blob/js/dedupe-dynamic-filter-inner-state/datafusion/physical-expr-common/src/physical_expr.rs#L457-L457) from https://github.com/apache/datafusion/pull/20416
- [PhysicalExpr::bind_runtime](https://github.com/gene-bordegaray/datafusion/blob/10a36f01c16ac9a4a19f116ee9885b3893c2d88a/datafusion/physical-expr-common/src/physical_expr.rs#L433)
Or add contributions in other parts of the project that claim to be "general purpose", but in practice they are only useful for dynamic filters:
- [PhysicalExprNode.internal_id](https://github.com/jayshrivastava/datafusion/blob/js/dedupe-dynamic-filter-inner-state/datafusion/proto/proto/datafusion.proto#L891-L891)
- [DeduplicatingSerializer](https://github.com/apache/datafusion/blob/9b726bcf288e7779b855ead5b869b0a69aba5c92/datafusion/proto/src/physical_plan/mod.rs#L3842-L3842) (not sure about this one, I get the impression that only dynamic filters are actually deduped, as no other expression is referenced more than once in a plan, but maybe I'm wrong)
Doing some research, Trino represents dynamic filters as first class citizens, with a dedicated interface:
- [DynamicFilter](https://github.com/trinodb/trino/blob/442557460b901f3b0cc37312c1d5a485b5c38bae/core/trino-spi/src/main/java/io/trino/spi/connector/DynamicFilter.java#L21)
And Trino connectors are expected to interact with dynamic filters by directly passing them as separate arguments
- [ConnectorSplitManager#getSplits()](https://github.com/trinodb/trino/blob/442557460b901f3b0cc37312c1d5a485b5c38bae/core/trino-spi/src/main/java/io/trino/spi/connector/ConnectorSplitManager.java#L24)
A lot of the reservations I have with the way this PR and https://github.com/apache/datafusion/pull/20416 are proposing to extend dynamic filters in functionality arise from the fact that the blast radius of these contributions is pretty big, as they need to rely on extending very central pillars of DataFusion like `PhysicalExpr` for getting the job done, when the actual functionality is scoped to just dynamic filters.
IMHO, If we had a way of contributing functionality to dynamic filters without necessarily touching the very core of DataFusion, the flow of these contributions would be much smoother, and I could imagine how representing dynamic filters, not as `PhysicalExpr` implementations, but as first class `DynamicFilter` structs, has good chances of delivering such vision.
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: add generic runtime binding for physical expressions [datafusion]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #20901:
URL: https://github.com/apache/datafusion/pull/20901#issuecomment-4539164449
Thank you for your contribution. Unfortunately, this pull request is stale because it has been open 60 days with no activity. Please remove the stale label or comment or this will be closed in 7 days.
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: add generic runtime binding for physical expressions [datafusion]
Posted by "LiaCastaneda (via GitHub)" <gi...@apache.org>.
LiaCastaneda commented on code in PR #20901:
URL: https://github.com/apache/datafusion/pull/20901#discussion_r2989423299
##########
datafusion/physical-expr-common/src/physical_expr.rs:
##########
@@ -416,6 +416,28 @@ pub trait PhysicalExpr: Any + Send + Sync + Display + Debug + DynEq + DynHash {
0
}
+ /// Bind runtime-specific data into this expression, if needed.
+ ///
+ /// This hook lets an expression replace itself with a runtime-bound version using the given
+ /// `context` (e.g. binding a per-partition view).
+ ///
+ /// Binding is single-pass over the existing tree. If this method returns a replacement
+ /// expression that itself contains additional bindable nodes, those newly introduced nodes are
+ /// not rebound in the same call.
+ ///
+ /// You should not call this method directly as it does not handle recursion. Instead use
+ /// [`bind_runtime_physical_expr`] to handle recursion and bind the full expression tree.
+ ///
+ /// Note for implementers: this method should not handle recursion.
+ /// Recursion is handled in [`bind_runtime_physical_expr`].
+ fn bind_runtime(
Review Comment:
iiuc the idea is to call this function in `open()` or `execute()` right? where we have the partitioning information.
Do we want to call this fucntion only when we know we are preserving partitioning?
##########
datafusion/physical-expr-common/src/physical_expr.rs:
##########
@@ -856,4 +1028,96 @@ mod test {
&BooleanArray::from(vec![true; 5]),
);
}
+
+ #[test]
+ fn test_bind_runtime_physical_expr_no_transform() {
+ let ctx = RuntimeBindContext {
+ target_key: "right",
+ bound_name: "bound",
+ };
+
+ // TestExpr does not override bind_runtime, so traversal is a no-op.
+ let default_expr: Arc<dyn PhysicalExpr> = Arc::new(TestExpr {});
+ let transformed =
+ super::bind_runtime_physical_expr_opt(Arc::clone(&default_expr), &ctx)
+ .unwrap();
+
+ assert!(!transformed.transformed);
+ assert!(Arc::ptr_eq(&default_expr, &transformed.data));
+
+ // Context mismatch returns no transform even for bindable nodes.
+ let bindable_expr: Arc<dyn PhysicalExpr> = Arc::new(RuntimeBindTestExpr::new(
+ "left",
+ BindBehavior::Match("left"),
+ vec![],
+ ));
+ let transformed =
+ super::bind_runtime_physical_expr_opt(Arc::clone(&bindable_expr), &ctx)
+ .unwrap();
+
+ assert!(!transformed.transformed);
+ assert!(Arc::ptr_eq(&bindable_expr, &transformed.data));
+ }
+
+ #[test]
+ fn test_bind_runtime_physical_expr_recurses() {
+ // Only the right child matches target_key and should be rewritten.
+ let left: Arc<dyn PhysicalExpr> = Arc::new(RuntimeBindTestExpr::new(
+ "left",
+ BindBehavior::Match("left"),
+ vec![],
+ ));
+ let right: Arc<dyn PhysicalExpr> = Arc::new(RuntimeBindTestExpr::new(
+ "right",
+ BindBehavior::Match("right"),
+ vec![],
+ ));
+ let root: Arc<dyn PhysicalExpr> = Arc::new(RuntimeBindTestExpr::new(
+ "root",
+ BindBehavior::None,
+ vec![Arc::clone(&left), Arc::clone(&right)],
+ ));
+ let ctx = RuntimeBindContext {
+ target_key: "right",
+ bound_name: "right_bound",
+ };
+
+ let transformed = super::bind_runtime_physical_expr_opt(root, &ctx).unwrap();
+ assert!(transformed.transformed);
+
+ let root = transformed
+ .data
+ .as_any()
+ .downcast_ref::<RuntimeBindTestExpr>()
+ .expect("root should be RuntimeBindTestExpr");
+ let left = root.children[0]
+ .as_any()
+ .downcast_ref::<RuntimeBindTestExpr>()
+ .expect("left should be RuntimeBindTestExpr");
+ let right = root.children[1]
+ .as_any()
+ .downcast_ref::<RuntimeBindTestExpr>()
+ .expect("right should be RuntimeBindTestExpr");
+
+ assert_eq!(left.name, "left");
+ assert_eq!(right.name, "right_bound");
+ assert_eq!(right.bind_behavior, BindBehavior::None);
Review Comment:
nit: I think it's worth adding here a small comment mentioning why we expect these values, it's a bit hard to follow just by looking at the assert expressions.
##########
datafusion/physical-expr-common/src/physical_expr.rs:
##########
@@ -618,6 +640,46 @@ pub fn snapshot_physical_expr_opt(
})
}
+/// Bind runtime-specific data into the given `PhysicalExpr`.
+///
+/// See the documentation of [`PhysicalExpr::bind_runtime`] for more details.
+///
+/// Runtime binding is applied once over the current expression tree.
+///
+/// # Returns
+///
+/// Returns a runtime-bound expression if any node required binding,
+/// otherwise returns the original expression.
+pub fn bind_runtime_physical_expr(
+ expr: Arc<dyn PhysicalExpr>,
+ context: &(dyn Any + Send + Sync),
+) -> Result<Arc<dyn PhysicalExpr>> {
+ bind_runtime_physical_expr_opt(expr, context).data()
+}
+
+/// Bind runtime-specific data into the given `PhysicalExpr`.
+///
+/// See the documentation of [`PhysicalExpr::bind_runtime`] for more details.
+///
+/// Runtime binding is applied once over the current expression tree.
+///
+/// # Returns
+///
+/// Returns a [`Transformed`] indicating whether any runtime binding happened,
+/// along with the resulting expression.
+pub fn bind_runtime_physical_expr_opt(
+ expr: Arc<dyn PhysicalExpr>,
+ context: &(dyn Any + Send + Sync),
+) -> Result<Transformed<Arc<dyn PhysicalExpr>>> {
+ expr.transform_up(|e| {
+ if let Some(bound) = e.bind_runtime(context)? {
+ Ok(Transformed::yes(bound))
Review Comment:
This looks very similar to the [snapshot_physical_expr_opt](https://github.com/apache/datafusion/blob/6e0dde0890ef967183f5fa828195a44cbf99b870/datafusion/physical-expr-common/src/physical_expr.rs#L609) implementation for `DynamicFilterPhysicalExpr`, no? I also notice that `snapshot()` is only implemented in `DynamicFilterPhysicalExpr`. Unless I'm misunderstanding something, it seems like these two methods do similar things, but `bind_runtime` is the more general version since it can carry context?
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: add generic runtime binding for physical expressions [datafusion]
Posted by "gene-bordegaray (via GitHub)" <gi...@apache.org>.
gene-bordegaray commented on PR #20901:
URL: https://github.com/apache/datafusion/pull/20901#issuecomment-4132903663
> IMHO, If we had a way of contributing functionality to dynamic filters without necessarily touching the very core of DataFusion, the flow of these contributions would be much smoother, and I could imagine how representing dynamic filters, not as `PhysicalExpr` implementations, but as first class `DynamicFilter` structs, has good chances of delivering such vision.
Trino's representation of dynamic filters is much more explicit which avoids a lot of one off behavior for PhysicalExpr.
To Adrian's question if we were to explore a new design how long would we have to support the PhysicalExpr approach?
I don't know the exact scenario but like it would get hairy if users are using the public API for a PhysicalExpr in one run filter and a implicitly uses a first class DynamicFilter in another.
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: add generic runtime binding for physical expressions [datafusion]
Posted by "gene-bordegaray (via GitHub)" <gi...@apache.org>.
gene-bordegaray commented on code in PR #20901:
URL: https://github.com/apache/datafusion/pull/20901#discussion_r2989874655
##########
datafusion/physical-expr-common/src/physical_expr.rs:
##########
@@ -618,6 +640,46 @@ pub fn snapshot_physical_expr_opt(
})
}
+/// Bind runtime-specific data into the given `PhysicalExpr`.
+///
+/// See the documentation of [`PhysicalExpr::bind_runtime`] for more details.
+///
+/// Runtime binding is applied once over the current expression tree.
+///
+/// # Returns
+///
+/// Returns a runtime-bound expression if any node required binding,
+/// otherwise returns the original expression.
+pub fn bind_runtime_physical_expr(
+ expr: Arc<dyn PhysicalExpr>,
+ context: &(dyn Any + Send + Sync),
+) -> Result<Arc<dyn PhysicalExpr>> {
+ bind_runtime_physical_expr_opt(expr, context).data()
+}
+
+/// Bind runtime-specific data into the given `PhysicalExpr`.
+///
+/// See the documentation of [`PhysicalExpr::bind_runtime`] for more details.
+///
+/// Runtime binding is applied once over the current expression tree.
+///
+/// # Returns
+///
+/// Returns a [`Transformed`] indicating whether any runtime binding happened,
+/// along with the resulting expression.
+pub fn bind_runtime_physical_expr_opt(
+ expr: Arc<dyn PhysicalExpr>,
+ context: &(dyn Any + Send + Sync),
+) -> Result<Transformed<Arc<dyn PhysicalExpr>>> {
+ expr.transform_up(|e| {
+ if let Some(bound) = e.bind_runtime(context)? {
+ Ok(Transformed::yes(bound))
Review Comment:
the main difference is snapshot is responsible for capturing the dynamic state into a replacement expression while bind_runtime is it inject execution specific characteristics into n expression.
I was previously using snapshot to do this work but was mentioned that it felt like we were shoe-horning partitioning into dynamic expressions thus a cleaner API with separate concerns would be a better apporach
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: add generic runtime binding for physical expressions [datafusion]
Posted by "gene-bordegaray (via GitHub)" <gi...@apache.org>.
gene-bordegaray commented on code in PR #20901:
URL: https://github.com/apache/datafusion/pull/20901#discussion_r2989902471
##########
datafusion/physical-expr-common/src/physical_expr.rs:
##########
@@ -416,6 +416,28 @@ pub trait PhysicalExpr: Any + Send + Sync + Display + Debug + DynEq + DynHash {
0
}
+ /// Bind runtime-specific data into this expression, if needed.
+ ///
+ /// This hook lets an expression replace itself with a runtime-bound version using the given
+ /// `context` (e.g. binding a per-partition view).
+ ///
+ /// Binding is single-pass over the existing tree. If this method returns a replacement
+ /// expression that itself contains additional bindable nodes, those newly introduced nodes are
+ /// not rebound in the same call.
+ ///
+ /// You should not call this method directly as it does not handle recursion. Instead use
+ /// [`bind_runtime_physical_expr`] to handle recursion and bind the full expression tree.
+ ///
+ /// Note for implementers: this method should not handle recursion.
+ /// Recursion is handled in [`bind_runtime_physical_expr`].
+ fn bind_runtime(
Review Comment:
yes the intended call site is in `open()` since you know the runtime context there for partitioning, but I added that this is generic because this function is not limited to partition number, it could be anything you may want to put in an expression at runtime.
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: add generic runtime binding for physical expressions [datafusion]
Posted by "gene-bordegaray (via GitHub)" <gi...@apache.org>.
gene-bordegaray closed pull request #20901: feat: add generic runtime binding for physical expressions
URL: https://github.com/apache/datafusion/pull/20901
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org