diff --git a/datafusion/sql/src/statement.rs b/datafusion/sql/src/statement.rs index 8c94610f7764..30396453c197 100644 --- a/datafusion/sql/src/statement.rs +++ b/datafusion/sql/src/statement.rs @@ -2435,17 +2435,23 @@ impl SqlToRel<'_, S> { span: _, }) = val { - let name = - name.replace('$', "").parse::().map_err(|_| { - plan_datafusion_err!("Can't parse placeholder: {name}") - })? - 1; + let index = match name[1..].parse::().map_err(|_| { + plan_datafusion_err!("Can't parse placeholder: {name}") + })? { + 0 => { + return plan_err!( + "Invalid placeholder, zero is not a valid index: {name}" + ); + } + index => index - 1, + }; let field = fields.get(idx).ok_or_else(|| { plan_datafusion_err!( "Placeholder ${} refers to a non existent column", idx + 1 ) })?; - let _ = prepare_param_data_types.insert(name, Arc::clone(field)); + let _ = prepare_param_data_types.insert(index, Arc::clone(field)); } } } diff --git a/datafusion/sqllogictest/test_files/insert_values_placeholders.slt b/datafusion/sqllogictest/test_files/insert_values_placeholders.slt new file mode 100644 index 000000000000..a9cc0ba28934 --- /dev/null +++ b/datafusion/sqllogictest/test_files/insert_values_placeholders.slt @@ -0,0 +1,26 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +########## +## INSERT VALUES placeholder tests +########## + +statement ok +CREATE TABLE placeholder_zero_insert(x BIGINT NULL); + +query error DataFusion error: Error during planning: Invalid placeholder, zero is not a valid index: \$0 +EXPLAIN INSERT INTO placeholder_zero_insert VALUES ($0);