Skip to content

Apache DataFusion SQL Query Engine

fix: change placeholder errors from Internal to Plan (#9745)

Adam Curtisb1f377465a50
diff --git a/datafusion/common/src/error.rs b/datafusion/common/src/error.rs
index d1e47b4734..cafab6d334 100644
--- a/datafusion/common/src/error.rs
+++ b/datafusion/common/src/error.rs
@@ -601,6 +601,7 @@ pub use config_err as _config_err;
pub use internal_datafusion_err as _internal_datafusion_err;
pub use internal_err as _internal_err;
pub use not_impl_err as _not_impl_err;
+pub use plan_datafusion_err as _plan_datafusion_err;
pub use plan_err as _plan_err;
pub use schema_err as _schema_err;
diff --git a/datafusion/common/src/param_value.rs b/datafusion/common/src/param_value.rs
index c614098713..8d61bad97b 100644
--- a/datafusion/common/src/param_value.rs
+++ b/datafusion/common/src/param_value.rs
@@ -15,8 +15,8 @@
// specific language governing permissions and limitations
// under the License.
-use crate::error::_plan_err;
-use crate::{DataFusionError, Result, ScalarValue};
+use crate::error::{_plan_datafusion_err, _plan_err};
+use crate::{Result, ScalarValue};
use arrow_schema::DataType;
use std::collections::HashMap;
@@ -75,16 +75,12 @@ impl ParamValues {
let idx = id[1..]
.parse::<usize>()
.map_err(|e| {
- DataFusionError::Internal(format!(
- "Failed to parse placeholder id: {e}"
- ))
+ _plan_datafusion_err!("Failed to parse placeholder id: {e}")
})?
.checked_sub(1);
// value at the idx-th position in param_values should be the value for the placeholder
let value = idx.and_then(|idx| list.get(idx)).ok_or_else(|| {
- DataFusionError::Internal(format!(
- "No value found for placeholder with id {id}"
- ))
+ _plan_datafusion_err!("No value found for placeholder with id {id}")
})?;
Ok(value.clone())
}
@@ -93,9 +89,7 @@ impl ParamValues {
let name = &id[1..];
// value at the name position in param_values should be the value for the placeholder
let value = map.get(name).ok_or_else(|| {
- DataFusionError::Internal(format!(
- "No value found for placeholder with name {id}"
- ))
+ _plan_datafusion_err!("No value found for placeholder with name {id}")
})?;
Ok(value.clone())
}
diff --git a/datafusion/sql/tests/sql_integration.rs b/datafusion/sql/tests/sql_integration.rs
index c738a2bd75..448a9c5420 100644
--- a/datafusion/sql/tests/sql_integration.rs
+++ b/datafusion/sql/tests/sql_integration.rs
@@ -24,7 +24,8 @@ use arrow_schema::TimeUnit::Nanosecond;
use arrow_schema::*;
use datafusion_common::config::ConfigOptions;
use datafusion_common::{
- plan_err, DFSchema, DataFusionError, ParamValues, Result, ScalarValue, TableReference,
+ assert_contains, plan_err, DFSchema, DataFusionError, ParamValues, Result,
+ ScalarValue, TableReference,
};
use datafusion_expr::{
logical_plan::{LogicalPlan, Prepare},
@@ -4364,6 +4365,40 @@ fn test_prepare_statement_to_plan_value_list() {
prepare_stmt_replace_params_quick_test(plan, param_values, expected_plan);
}
+#[test]
+fn test_prepare_statement_unknown_list_param() {
+ let sql = "SELECT id from person where id = $2";
+ let plan = logical_plan(sql).unwrap();
+ let param_values = ParamValues::List(vec![]);
+ let err = plan.replace_params_with_values(&param_values).unwrap_err();
+ assert_contains!(
+ err.to_string(),
+ "Error during planning: No value found for placeholder with id $2"
+ );
+}
+
+#[test]
+fn test_prepare_statement_unknown_hash_param() {
+ let sql = "SELECT id from person where id = $bar";
+ let plan = logical_plan(sql).unwrap();
+ let param_values = ParamValues::Map(HashMap::new());
+ let err = plan.replace_params_with_values(&param_values).unwrap_err();
+ assert_contains!(
+ err.to_string(),
+ "Error during planning: No value found for placeholder with name $bar"
+ );
+}
+
+#[test]
+fn test_prepare_statement_bad_list_idx() {
+ let sql = "SELECT id from person where id = $foo";
+ let plan = logical_plan(sql).unwrap();
+ let param_values = ParamValues::List(vec![]);
+
+ let err = plan.replace_params_with_values(&param_values).unwrap_err();
+ assert_contains!(err.to_string(), "Error during planning: Failed to parse placeholder id: invalid digit found in string");
+}
+
#[test]
fn test_table_alias() {
let sql = "select * from (\