Skip to content

Commit a8761a6

Browse files
author
Sean Kenneth Doherty
authored
Guard insert placeholder zero (apache#22299)
## Which issue does this PR close? - Fixes apache#22224. ## Rationale for this change `EXPLAIN INSERT INTO t VALUES ($0)` currently panics while inferring parameter types for INSERT VALUES. Other SQL paths already reject `$0` as an invalid placeholder index, and the INSERT inference path should do the same instead of subtracting one from zero. ## What changes are included in this PR? - Adds a zero-index guard before INSERT VALUES placeholder numbers are converted to parameter vector positions. - Aligns the INSERT VALUES error with the existing invalid-placeholder planning error. - Adds a standalone SQL logic regression for `EXPLAIN INSERT INTO ... VALUES ($0)`. ## Are these changes tested? Yes. - `cargo fmt --check --all` - `cargo test -p datafusion-sqllogictest --test sqllogictests -- insert_values_placeholders` - `cargo test -p datafusion-sql --test sql_integration test_insert_schema_errors` -> 6 passed - `git diff --check origin/main...HEAD` - `git diff --cached --check` ## Are there any user-facing changes? Yes. `INSERT ... VALUES ($0)` now returns a planning error instead of panicking. ## Scope note The change is limited to INSERT VALUES placeholder type inference and the new regression coverage. I did not find any broader placeholder semantics that needed to change.
1 parent 305b6fa commit a8761a6

2 files changed

Lines changed: 37 additions & 5 deletions

File tree

‎datafusion/sql/src/statement.rs‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2485,17 +2485,23 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
24852485
span: _,
24862486
}) = val
24872487
{
2488-
let name =
2489-
name.replace('$', "").parse::<usize>().map_err(|_| {
2490-
plan_datafusion_err!("Can't parse placeholder: {name}")
2491-
})? - 1;
2488+
let index = match name[1..].parse::<usize>().map_err(|_| {
2489+
plan_datafusion_err!("Can't parse placeholder: {name}")
2490+
})? {
2491+
0 => {
2492+
return plan_err!(
2493+
"Invalid placeholder, zero is not a valid index: {name}"
2494+
);
2495+
}
2496+
index => index - 1,
2497+
};
24922498
let field = fields.get(idx).ok_or_else(|| {
24932499
plan_datafusion_err!(
24942500
"Placeholder ${} refers to a non existent column",
24952501
idx + 1
24962502
)
24972503
})?;
2498-
let _ = prepare_param_data_types.insert(name, Arc::clone(field));
2504+
let _ = prepare_param_data_types.insert(index, Arc::clone(field));
24992505
}
25002506
}
25012507
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
##########
19+
## INSERT VALUES placeholder tests
20+
##########
21+
22+
statement ok
23+
CREATE TABLE placeholder_zero_insert(x BIGINT NULL);
24+
25+
query error DataFusion error: Error during planning: Invalid placeholder, zero is not a valid index: \$0
26+
EXPLAIN INSERT INTO placeholder_zero_insert VALUES ($0);

0 commit comments

Comments
 (0)