Skip to content

Commit a6fab6b

Browse files
chenkovskyalamb
andauthored
feat: support complex expr for prepared statement argument (#18383)
## Which issue does this PR close? ## Rationale for this change complex expr is not supported in prepared statement argument. ## What changes are included in this PR? simplify arguments of prepared statement first. ## Are these changes tested? UT ## Are there any user-facing changes? No --------- Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
1 parent 4978243 commit a6fab6b

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

datafusion/core/src/execution/context/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,15 @@ pub use datafusion_execution::config::SessionConfig;
7676
use datafusion_execution::registry::SerializerRegistry;
7777
pub use datafusion_execution::TaskContext;
7878
pub use datafusion_expr::execution_props::ExecutionProps;
79+
use datafusion_expr::simplify::SimplifyContext;
7980
use datafusion_expr::{
8081
expr_rewriter::FunctionRewrite,
8182
logical_plan::{DdlStatement, Statement},
8283
planner::ExprPlanner,
8384
Expr, UserDefinedLogicalNode, WindowUDF,
8485
};
8586
use datafusion_optimizer::analyzer::type_coercion::TypeCoercion;
87+
use datafusion_optimizer::simplify_expressions::ExprSimplifier;
8688
use datafusion_optimizer::Analyzer;
8789
use datafusion_optimizer::{AnalyzerRule, OptimizerRule};
8890
use datafusion_session::SessionStore;
@@ -1269,14 +1271,18 @@ impl SessionContext {
12691271
exec_datafusion_err!("Prepared statement '{}' does not exist", name)
12701272
})?;
12711273

1274+
let state = self.state.read();
1275+
let context = SimplifyContext::new(state.execution_props());
1276+
let simplifier = ExprSimplifier::new(context);
1277+
12721278
// Only allow literals as parameters for now.
12731279
let mut params: Vec<ScalarAndMetadata> = parameters
12741280
.into_iter()
1275-
.map(|e| match e {
1281+
.map(|e| match simplifier.simplify(e)? {
12761282
Expr::Literal(scalar, metadata) => {
12771283
Ok(ScalarAndMetadata::new(scalar, metadata))
12781284
}
1279-
_ => not_impl_err!("Unsupported parameter type: {}", e),
1285+
e => not_impl_err!("Unsupported parameter type: {e}"),
12801286
})
12811287
.collect::<Result<_>>()?;
12821288

datafusion/sqllogictest/test_files/prepare.slt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,11 @@ EXECUTE my_plan6(20.0);
204204
statement error Cast error: Cannot cast string 'foo' to value of Int32 type
205205
EXECUTE my_plan6('foo');
206206

207-
# TODO: support non-literal expressions
208-
statement error Unsupported parameter type
209-
EXECUTE my_plan6(10 + 20);
207+
# support non-literal expressions
208+
query II
209+
EXECUTE my_plan6(10 + 10);
210+
----
211+
1 20
210212

211213
statement ok
212214
DEALLOCATE my_plan6;

0 commit comments

Comments
 (0)