You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by gi...@apache.org on 2026/10/16 14:33:20 UTC
(datafusion) branch main updated: feat!: pass input schema to ExprPlanner::plan_scalar (#26177)
This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git
The following commit(s) were added to refs/heads/main by this push:
new 396ea7c19e feat!: pass input schema to ExprPlanner::plan_scalar (#26177)
396ea7c19e is described below
commit 396ea7c19e7731747b7efebf22f638bff741040c
Author: fys <40...@users.noreply.github.com>
AuthorDate: Fri Oct 16 14:16:25 2026 +0000
feat!: pass input schema to ExprPlanner::plan_scalar (#26177)
## 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
Scalar function planners need access to the input schema to inspect
argument types and field metadata, enabling expression rewrites.
<!--
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.
Please explain the problem you are trying to solve in terms of the
user-visible
behavior, rather than the implementation.
For example, "The code in `foo.rs` doesn't handle nulls" is a symptom of
the
implementation. "COUNT(DISTINCT) returns wrong results when the column
contains
nulls" is the user-visible problem.
-->
## What changes are included in this PR?
Add a `&DFSchema` parameter to `ExprPlanner::plan_scalar` and pass the
input schema during SQL planning.
<!--
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.
-->
## What is the testing strategy for this PR?
Updated `select_scalar_func_with_expr_planner` to verify that the
planner can resolve a column argument’s type using the input schema and
correctly rewrite the function call.
<!--
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
Briefly describe how this PR is tested, and point to the specific tests
you added. For example: 'This new feature is covered by the
`sqllogictest` cases added in `foo.slt`'.
If this PR does not add tests, explain why. For example, if the change
is already covered by existing tests, please mention it.
You should also check the `codecov` bot reply on this PR to confirm the
changed code is exercised.
-->
## Are there any user-facing changes?
Yes. Implementations overriding `ExprPlanner::plan_scalar` must accept
an additional `&DFSchema` parameter. This is a breaking API change.
<!--
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.
-->
---
datafusion/expr/src/planner.rs | 6 +++++-
datafusion/sql/src/expr/function.rs | 2 +-
datafusion/sql/tests/sql_integration.rs | 23 +++++++++++++++--------
3 files changed, 21 insertions(+), 10 deletions(-)
diff --git a/datafusion/expr/src/planner.rs b/datafusion/expr/src/planner.rs
index 31aad67d17..ed13209521 100644
--- a/datafusion/expr/src/planner.rs
+++ b/datafusion/expr/src/planner.rs
@@ -266,7 +266,11 @@ pub trait ExprPlanner: Debug + Send + Sync {
/// Plans scalar functions, such as `ABS(<expr>)`
///
/// Returns the original scalar function if not possible
- fn plan_scalar(&self, expr: RawScalarExpr) -> Result<PlannerResult<RawScalarExpr>> {
+ fn plan_scalar(
+ &self,
+ expr: RawScalarExpr,
+ _schema: &DFSchema,
+ ) -> Result<PlannerResult<RawScalarExpr>> {
Ok(PlannerResult::Original(expr))
}
diff --git a/datafusion/sql/src/expr/function.rs b/datafusion/sql/src/expr/function.rs
index f3e4c79740..1a481dc386 100644
--- a/datafusion/sql/src/expr/function.rs
+++ b/datafusion/sql/src/expr/function.rs
@@ -352,7 +352,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
};
for planner in self.context_provider.get_expr_planners().iter() {
- match planner.plan_scalar(scalar_expr)? {
+ match planner.plan_scalar(scalar_expr, schema)? {
PlannerResult::Planned(expr) => return Ok(expr),
PlannerResult::Original(expr) => scalar_expr = expr,
}
diff --git a/datafusion/sql/tests/sql_integration.rs b/datafusion/sql/tests/sql_integration.rs
index 5a48c52ef5..7ab57a86f9 100644
--- a/datafusion/sql/tests/sql_integration.rs
+++ b/datafusion/sql/tests/sql_integration.rs
@@ -28,10 +28,10 @@ use arrow::datatypes::{TimeUnit::Nanosecond, *};
use common::MockContextProvider;
use datafusion_common::{DFSchema, DataFusionError, Result, assert_contains};
use datafusion_expr::{
- ColumnarValue, CreateIndex, DdlStatement, Expr, HigherOrderFunctionArgs,
- HigherOrderReturnFieldArgs, HigherOrderSignature, HigherOrderUDF, HigherOrderUDFImpl,
- LambdaParametersProgress, ScalarFunctionArgs, ScalarUDF, ScalarUDFImpl, Signature,
- ValueOrLambda, Volatility, col,
+ ColumnarValue, CreateIndex, DdlStatement, Expr, ExprSchemable,
+ HigherOrderFunctionArgs, HigherOrderReturnFieldArgs, HigherOrderSignature,
+ HigherOrderUDF, HigherOrderUDFImpl, LambdaParametersProgress, ScalarFunctionArgs,
+ ScalarUDF, ScalarUDFImpl, Signature, ValueOrLambda, Volatility, col,
expr::{HigherOrderFunction, LambdaVariable, ScalarFunction},
lambda,
logical_plan::LogicalPlan,
@@ -910,10 +910,15 @@ fn select_scalar_func_with_literal_no_relation() {
struct SingleArgumentCoalescePlanner;
impl ExprPlanner for SingleArgumentCoalescePlanner {
- fn plan_scalar(&self, expr: RawScalarExpr) -> Result<PlannerResult<RawScalarExpr>> {
+ fn plan_scalar(
+ &self,
+ expr: RawScalarExpr,
+ schema: &DFSchema,
+ ) -> Result<PlannerResult<RawScalarExpr>> {
if expr.func.name() == "coalesce"
&& let [arg] = expr.args.as_slice()
{
+ assert_eq!(arg.get_type(schema)?, DataType::Int64);
Ok(PlannerResult::Planned(arg.clone()))
} else {
Ok(PlannerResult::Original(expr))
@@ -927,7 +932,7 @@ fn select_scalar_func_with_expr_planner() -> Result<()> {
.with_scalar_function(core_functions::coalesce())
.with_expr_planner(Arc::new(SingleArgumentCoalescePlanner));
let plan = logical_plan_from_state(
- "SELECT coalesce(42)",
+ "SELECT coalesce(t.a) FROM (SELECT 42 AS a) AS t",
&GenericDialect {},
ParserOptions::default(),
state,
@@ -935,8 +940,10 @@ fn select_scalar_func_with_expr_planner() -> Result<()> {
assert_snapshot!(
plan,
@r"
- Projection: Int64(42)
- EmptyRelation: rows=1
+ Projection: t.a
+ SubqueryAlias: t
+ Projection: Int64(42) AS a
+ EmptyRelation: rows=1
"
);
Ok(())
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org