You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "Jiashu-Hu (via GitHub)" <gi...@apache.org> on 2025/03/21 20:48:59 UTC
[PR] Documentation: Plan custom expressions [datafusion]
Jiashu-Hu opened a new pull request, #15353:
URL: https://github.com/apache/datafusion/pull/15353
## 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 #15267](https://github.com/apache/datafusion/issues/15267) .
## 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.
-->
This PR adds documentation for using the `ExprPlanner` API to plan custom expressions, as requested in #15267. Clear documentation with an example will help users extend DataFusion to support custom operators like PostgreSQL's `->`.
## What changes are included in this PR?
- Added a new section `## Custom Expression Planning` to document
- Included an introduction explaining what `ExprPlanner` does and why it’s useful.
- Provided a step-by-step example showing how to implement and register an `ExprPlanner` to make the `->` operator concatenate strings (e.g., `'foo'->'bar'` outputs `foobar`).
- Linked to relevant API docs (`ExprPlanner` and `FunctionRegistry`) for further reading.
<!--
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?
yes
<!--
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?
Yes, since this is an documentation chang
<!--
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] Documentation: Plan custom expressions [datafusion]
Posted by "Jiashu-Hu (via GitHub)" <gi...@apache.org>.
Jiashu-Hu commented on code in PR #15353:
URL: https://github.com/apache/datafusion/pull/15353#discussion_r2010728423
##########
docs/source/library-user-guide/adding-udfs.md:
##########
@@ -1160,6 +1160,89 @@ async fn main() -> Result<()> {
// +---+
```
+## Custom Expression Planning
+
+DataFusion provides native support for a limited set of SQL operators by default. For operators not natively defined, developers can extend DataFusion's functionality by implementing custom expression planning. This extensibility is a core feature of DataFusion, allowing it to be customized for particular workloads and requirements.
+
+### Implementing Custom Expression Planning
+
+To extend DataFusion with support for custom operators not natively available, you need to:
+
+1. Implement the `ExprPlanner` trait: This allows you to define custom logic for planning expressions that DataFusion doesn't natively recognize. The trait provides the necessary interface to translate logical expressions into physical execution plans.
+
+ For a detailed documentation please see: [Trait ExprPlanner](https://docs.rs/datafusion/latest/datafusion/logical_expr/planner/trait.ExprPlanner.html)
+
+2. Register your custom planner: Integrate your implementation with DataFusion's `SessionContext` to ensure your custom planning logic is invoked during the query optimization and execution planning phase.
+
+ For a detailed documentation please see: [fn register_expr_planner](https://docs.rs/datafusion/latest/datafusion/execution/trait.FunctionRegistry.html#method.register_expr_planner)
+
+See example below:
+
+```rust
+# use arrow::array::RecordBatch;
+# use std::sync::Arc;
+
+# use datafusion::common::{assert_batches_eq, DFSchema};
+# use datafusion::error::Result;
+# use datafusion::execution::FunctionRegistry;
+# use datafusion::logical_expr::Operator;
+# use datafusion::prelude::*;
+# use datafusion::sql::sqlparser::ast::BinaryOperator;
+# use datafusion_common::ScalarValue;
+# use datafusion_expr::expr::Alias;
+# use datafusion_expr::planner::{ExprPlanner, PlannerResult, RawBinaryExpr};
+# use datafusion_expr::BinaryExpr;
+
+# #[derive(Debug)]
+# // Define the custom planner
+# struct MyCustomPlanner;
+
+// Implement ExprPlanner for cutom operator logic
+impl ExprPlanner for MyCustomPlanner {
+ fn plan_binary_op(
+ &self,
+ expr: RawBinaryExpr,
+ _schema: &DFSchema,
+ ) -> Result<PlannerResult<RawBinaryExpr>> {
+ match &expr.op {
+ // Map `->` to string concatenation
+ BinaryOperator::Arrow => {
+ // Rewrite `->` as a string concatenation operation
+ // - `left` and `right` are the operands (e.g., 'hello' and 'world')
+ // - `Operator::StringConcat` tells DataFusion to concatenate them
+ Ok(PlannerResult::Planned(Expr::BinaryExpr(BinaryExpr {
+ left: Box::new(expr.left.clone()),
+ right: Box::new(expr.right.clone()),
+ op: Operator::StringConcat,
+ })))
+ }
+ _ => Ok(PlannerResult::Original(expr)),
+ }
+ }
+}
+
+use datafusion::execution::context::SessionContext;
+use datafusion::arrow::util::pretty;
+
+#[tokio::main]
+async fn main() -> Result<()> {
+ let config = SessionConfig::new().set_str("datafusion.sql_parser.dialect", "postgres");
+ let mut ctx = SessionContext::new_with_config(config);
+ ctx.register_expr_planner(Arc::new(MyCustomPlanner))?;
+ let results = ctx.sql("select 'foo'->'bar';").await?.collect().await?;
+
+ pretty::print_batches(&results)?;
Review Comment:
Hi alamb, Thank you very much for your review! Yes, absolutely, I've updated it with an `assert_batches_eq!` in the latest commit.
--
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] Documentation: Plan custom expressions [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #15353:
URL: https://github.com/apache/datafusion/pull/15353#discussion_r2008792240
##########
docs/source/library-user-guide/adding-udfs.md:
##########
@@ -1160,6 +1160,89 @@ async fn main() -> Result<()> {
// +---+
```
+## Custom Expression Planning
+
+DataFusion provides native support for a limited set of SQL operators by default. For operators not natively defined, developers can extend DataFusion's functionality by implementing custom expression planning. This extensibility is a core feature of DataFusion, allowing it to be customized for particular workloads and requirements.
+
+### Implementing Custom Expression Planning
+
+To extend DataFusion with support for custom operators not natively available, you need to:
+
+1. Implement the `ExprPlanner` trait: This allows you to define custom logic for planning expressions that DataFusion doesn't natively recognize. The trait provides the necessary interface to translate logical expressions into physical execution plans.
+
+ For a detailed documentation please see: [Trait ExprPlanner](https://docs.rs/datafusion/latest/datafusion/logical_expr/planner/trait.ExprPlanner.html)
+
+2. Register your custom planner: Integrate your implementation with DataFusion's `SessionContext` to ensure your custom planning logic is invoked during the query optimization and execution planning phase.
+
+ For a detailed documentation please see: [fn register_expr_planner](https://docs.rs/datafusion/latest/datafusion/execution/trait.FunctionRegistry.html#method.register_expr_planner)
Review Comment:
```suggestion
For a detailed documentation see: [fn register_expr_planner](https://docs.rs/datafusion/latest/datafusion/execution/trait.FunctionRegistry.html#method.register_expr_planner)
```
##########
docs/source/library-user-guide/adding-udfs.md:
##########
@@ -1160,6 +1160,89 @@ async fn main() -> Result<()> {
// +---+
```
+## Custom Expression Planning
+
+DataFusion provides native support for a limited set of SQL operators by default. For operators not natively defined, developers can extend DataFusion's functionality by implementing custom expression planning. This extensibility is a core feature of DataFusion, allowing it to be customized for particular workloads and requirements.
+
+### Implementing Custom Expression Planning
+
+To extend DataFusion with support for custom operators not natively available, you need to:
+
+1. Implement the `ExprPlanner` trait: This allows you to define custom logic for planning expressions that DataFusion doesn't natively recognize. The trait provides the necessary interface to translate logical expressions into physical execution plans.
+
+ For a detailed documentation please see: [Trait ExprPlanner](https://docs.rs/datafusion/latest/datafusion/logical_expr/planner/trait.ExprPlanner.html)
+
+2. Register your custom planner: Integrate your implementation with DataFusion's `SessionContext` to ensure your custom planning logic is invoked during the query optimization and execution planning phase.
+
+ For a detailed documentation please see: [fn register_expr_planner](https://docs.rs/datafusion/latest/datafusion/execution/trait.FunctionRegistry.html#method.register_expr_planner)
+
+See example below:
+
+```rust
+# use arrow::array::RecordBatch;
+# use std::sync::Arc;
+
+# use datafusion::common::{assert_batches_eq, DFSchema};
+# use datafusion::error::Result;
+# use datafusion::execution::FunctionRegistry;
+# use datafusion::logical_expr::Operator;
+# use datafusion::prelude::*;
+# use datafusion::sql::sqlparser::ast::BinaryOperator;
+# use datafusion_common::ScalarValue;
+# use datafusion_expr::expr::Alias;
+# use datafusion_expr::planner::{ExprPlanner, PlannerResult, RawBinaryExpr};
+# use datafusion_expr::BinaryExpr;
+
+# #[derive(Debug)]
+# // Define the custom planner
+# struct MyCustomPlanner;
+
+// Implement ExprPlanner for cutom operator logic
+impl ExprPlanner for MyCustomPlanner {
+ fn plan_binary_op(
+ &self,
+ expr: RawBinaryExpr,
+ _schema: &DFSchema,
+ ) -> Result<PlannerResult<RawBinaryExpr>> {
+ match &expr.op {
+ // Map `->` to string concatenation
+ BinaryOperator::Arrow => {
+ // Rewrite `->` as a string concatenation operation
+ // - `left` and `right` are the operands (e.g., 'hello' and 'world')
+ // - `Operator::StringConcat` tells DataFusion to concatenate them
+ Ok(PlannerResult::Planned(Expr::BinaryExpr(BinaryExpr {
+ left: Box::new(expr.left.clone()),
+ right: Box::new(expr.right.clone()),
+ op: Operator::StringConcat,
+ })))
+ }
+ _ => Ok(PlannerResult::Original(expr)),
+ }
+ }
+}
+
+use datafusion::execution::context::SessionContext;
+use datafusion::arrow::util::pretty;
+
+#[tokio::main]
+async fn main() -> Result<()> {
+ let config = SessionConfig::new().set_str("datafusion.sql_parser.dialect", "postgres");
+ let mut ctx = SessionContext::new_with_config(config);
+ ctx.register_expr_planner(Arc::new(MyCustomPlanner))?;
+ let results = ctx.sql("select 'foo'->'bar';").await?.collect().await?;
+
+ pretty::print_batches(&results)?;
Review Comment:
Could you please change this to use `assert_batches_eq! so the actual output is in the test and it is tested in CI
##########
docs/source/library-user-guide/adding-udfs.md:
##########
@@ -1160,6 +1160,89 @@ async fn main() -> Result<()> {
// +---+
```
+## Custom Expression Planning
+
+DataFusion provides native support for a limited set of SQL operators by default. For operators not natively defined, developers can extend DataFusion's functionality by implementing custom expression planning. This extensibility is a core feature of DataFusion, allowing it to be customized for particular workloads and requirements.
+
+### Implementing Custom Expression Planning
+
+To extend DataFusion with support for custom operators not natively available, you need to:
+
+1. Implement the `ExprPlanner` trait: This allows you to define custom logic for planning expressions that DataFusion doesn't natively recognize. The trait provides the necessary interface to translate logical expressions into physical execution plans.
+
+ For a detailed documentation please see: [Trait ExprPlanner](https://docs.rs/datafusion/latest/datafusion/logical_expr/planner/trait.ExprPlanner.html)
Review Comment:
```suggestion
For detailed documentation please see: [Trait ExprPlanner](https://docs.rs/datafusion/latest/datafusion/logical_expr/planner/trait.ExprPlanner.html)
```
##########
docs/source/library-user-guide/adding-udfs.md:
##########
@@ -1160,6 +1160,89 @@ async fn main() -> Result<()> {
// +---+
```
+## Custom Expression Planning
+
+DataFusion provides native support for a limited set of SQL operators by default. For operators not natively defined, developers can extend DataFusion's functionality by implementing custom expression planning. This extensibility is a core feature of DataFusion, allowing it to be customized for particular workloads and requirements.
Review Comment:
Here are some wordsmithing suggestions:
```suggestion
DataFusion provides native support for common SQL operators by default such as `+`, `-`, `||`. However it does not provide support for other operators such as `@>`. To override DataFusion's default handling or support unsupported operators, developers can extend DataFusion by implementing custom expression planning, a core feature of DataFusion
```
##########
docs/source/library-user-guide/adding-udfs.md:
##########
@@ -1160,6 +1160,89 @@ async fn main() -> Result<()> {
// +---+
```
+## Custom Expression Planning
+
+DataFusion provides native support for a limited set of SQL operators by default. For operators not natively defined, developers can extend DataFusion's functionality by implementing custom expression planning. This extensibility is a core feature of DataFusion, allowing it to be customized for particular workloads and requirements.
+
+### Implementing Custom Expression Planning
+
+To extend DataFusion with support for custom operators not natively available, you need to:
+
+1. Implement the `ExprPlanner` trait: This allows you to define custom logic for planning expressions that DataFusion doesn't natively recognize. The trait provides the necessary interface to translate logical expressions into physical execution plans.
Review Comment:
I think ExprPlanner actually converts the SQL AST to `Expr`s:
```suggestion
1. Implement the `ExprPlanner` trait: This allows you to define custom logic for planning expressions that DataFusion doesn't natively recognize. The trait provides the necessary interface to translate SQL AST nodes into logical `Expr`.
```
##########
docs/source/library-user-guide/adding-udfs.md:
##########
@@ -1160,6 +1160,89 @@ async fn main() -> Result<()> {
// +---+
```
+## Custom Expression Planning
+
+DataFusion provides native support for a limited set of SQL operators by default. For operators not natively defined, developers can extend DataFusion's functionality by implementing custom expression planning. This extensibility is a core feature of DataFusion, allowing it to be customized for particular workloads and requirements.
+
+### Implementing Custom Expression Planning
+
+To extend DataFusion with support for custom operators not natively available, you need to:
+
+1. Implement the `ExprPlanner` trait: This allows you to define custom logic for planning expressions that DataFusion doesn't natively recognize. The trait provides the necessary interface to translate logical expressions into physical execution plans.
+
+ For a detailed documentation please see: [Trait ExprPlanner](https://docs.rs/datafusion/latest/datafusion/logical_expr/planner/trait.ExprPlanner.html)
+
+2. Register your custom planner: Integrate your implementation with DataFusion's `SessionContext` to ensure your custom planning logic is invoked during the query optimization and execution planning phase.
+
+ For a detailed documentation please see: [fn register_expr_planner](https://docs.rs/datafusion/latest/datafusion/execution/trait.FunctionRegistry.html#method.register_expr_planner)
+
+See example below:
+
+```rust
+# use arrow::array::RecordBatch;
+# use std::sync::Arc;
+
+# use datafusion::common::{assert_batches_eq, DFSchema};
+# use datafusion::error::Result;
+# use datafusion::execution::FunctionRegistry;
+# use datafusion::logical_expr::Operator;
+# use datafusion::prelude::*;
+# use datafusion::sql::sqlparser::ast::BinaryOperator;
+# use datafusion_common::ScalarValue;
+# use datafusion_expr::expr::Alias;
+# use datafusion_expr::planner::{ExprPlanner, PlannerResult, RawBinaryExpr};
+# use datafusion_expr::BinaryExpr;
+
+# #[derive(Debug)]
+# // Define the custom planner
+# struct MyCustomPlanner;
+
+// Implement ExprPlanner for cutom operator logic
Review Comment:
```suggestion
// Implement ExprPlanner to add support for the `->` custom 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-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] Documentation: Plan custom expressions [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #15353:
URL: https://github.com/apache/datafusion/pull/15353
--
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