Skip to content

Commit 3a49a2f

Browse files
authored
fix: Handle EXECUTE without statement name (#22204)
## Which issue does this PR close? - Closes #22197. ## Rationale for this change `EXEC()` and `EXECUTE()` can parse as `Statement::Execute` without a statement name. The planner previously unwrapped that optional name and panicked on user-supplied SQL. ## What changes are included in this PR? - Return a planning error when `EXECUTE` has no statement name. - Add sqllogictest coverage for the malformed `EXEC` / `EXECUTE` forms from the issue. ## Are these changes tested? - `cargo test -p datafusion-sqllogictest --test sqllogictests -- prepare` - `cargo fmt --all` - `cargo clippy --all-targets --all-features -- -D warnings` ## Are there any user-facing changes? Malformed `EXEC` / `EXECUTE` statements without a name now return a planning error instead of panicking.
1 parent 1ab146a commit 3a49a2f

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

datafusion/sql/src/statement.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,14 +886,18 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
886886
"Execute statement with DEFAULT is not supported"
887887
);
888888
}
889+
let name = name.ok_or_else(|| {
890+
plan_datafusion_err!("EXECUTE statement requires a name")
891+
})?;
892+
889893
let empty_schema = DFSchema::empty();
890894
let parameters = parameters
891895
.into_iter()
892896
.map(|expr| self.sql_to_expr(expr, &empty_schema, planner_context))
893897
.collect::<Result<Vec<Expr>>>()?;
894898

895899
Ok(LogicalPlan::Statement(PlanStatement::Execute(Execute {
896-
name: object_name_to_string(&name.unwrap()),
900+
name: object_name_to_string(&name),
897901
parameters,
898902
})))
899903
}

datafusion/sqllogictest/test_files/prepare.slt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,25 @@ PREPARE my_plan(INT, INT) AS SELECT 1 + $1;
5353
statement error SQL error: ParserError
5454
PREPARE my_plan(INT) AS SELECT id, age FROM person WHERE age is $1;
5555

56+
# EXEC/EXECUTE with parentheses but no statement name must error instead of panic
57+
statement error EXECUTE statement requires a name
58+
EXEC();
59+
60+
statement error EXECUTE statement requires a name
61+
EXEC('');
62+
63+
statement error EXECUTE statement requires a name
64+
EXEC('any-string');
65+
66+
statement error EXECUTE statement requires a name
67+
EXEC(unknown_column);
68+
69+
statement error EXECUTE statement requires a name
70+
EXECUTE();
71+
72+
statement error EXECUTE statement requires a name
73+
EXEC ('a');
74+
5675
# #######################
5776
# Test prepare and execute statements
5877

0 commit comments

Comments
 (0)