Skip to content

Apache DataFusion SQL Query Engine

fix: plan UNION of untyped aliased placeholders (#25675)

Adrian Garcia Badaracco87b1f6a13196
diff --git a/datafusion/core/tests/sql/select.rs b/datafusion/core/tests/sql/select.rs
index 96b911e8db..c5c1ae54e7 100644
--- a/datafusion/core/tests/sql/select.rs
+++ b/datafusion/core/tests/sql/select.rs
@@ -319,6 +319,33 @@ async fn test_named_parameter_not_bound() -> Result<()> {
Ok(())
}
+#[tokio::test]
+async fn test_optimize_union_of_aliased_placeholders_before_binding() -> Result<()> {
+ let ctx = SessionContext::new();
+ let state = ctx.state();
+ let plan = state
+ .create_logical_plan("SELECT $1 AS a UNION ALL SELECT $2 AS a")
+ .await?;
+
+ let optimized = state.optimize(&plan)?;
+ let bound = optimized.replace_params_with_values(&ParamValues::List(vec![
+ ScalarValue::from("x").into(),
+ ScalarValue::from("y").into(),
+ ]))?;
+ let batches = ctx.execute_logical_plan(bound).await?.collect().await?;
+
+ assert_snapshot!(batches_to_sort_string(&batches), @r"
+ +---+
+ | a |
+ +---+
+ | x |
+ | y |
+ +---+
+ ");
+
+ Ok(())
+}
+
#[tokio::test]
async fn test_query_parameters_with_metadata() -> Result<()> {
let ctx = SessionContext::new();
diff --git a/datafusion/expr/src/expr_schema.rs b/datafusion/expr/src/expr_schema.rs
index 75b3a60af1..c47b45612a 100644
--- a/datafusion/expr/src/expr_schema.rs
+++ b/datafusion/expr/src/expr_schema.rs
@@ -182,14 +182,9 @@ impl ExprSchemable for Expr {
#[cfg_attr(feature = "recursive_protection", recursive::recursive)]
fn get_type(&self, schema: &dyn ExprSchema) -> Result<DataType> {
match self {
- Expr::Alias(Alias { expr, name, .. }) => match &**expr {
- Expr::Placeholder(Placeholder { field, .. }) => match &field {
- None => schema.data_type(&Column::from_name(name)).cloned(),
- Some(field) => Ok(field.data_type().clone()),
- },
- _ => expr.get_type(schema),
- },
- Expr::Negative(expr) => expr.get_type(schema),
+ Expr::Alias(Alias { expr, .. }) | Expr::Negative(expr) => {
+ expr.get_type(schema)
+ }
Expr::Column(c) => Ok(schema.data_type(c)?.clone()),
Expr::OuterReferenceColumn(field, _) => Ok(field.data_type().clone()),
Expr::ScalarVariable(field, _) => Ok(field.data_type().clone()),
@@ -1286,6 +1281,21 @@ mod tests {
);
}
+ #[test]
+ fn test_untyped_aliased_placeholder_in_empty_schema() {
+ // A UNION arm computes the type of `$1 AS a` against the input of the
+ // projection, which does not have a column `a`.
+ let schema = DFSchema::empty();
+ let expr = Expr::Placeholder(Placeholder::new_with_field("$1".to_string(), None))
+ .alias("a");
+
+ assert_eq!(expr.get_type(&schema).unwrap(), DataType::Null);
+ assert_eq!(
+ expr.to_field(&schema).unwrap().1.data_type(),
+ &DataType::Null
+ );
+ }
+
#[derive(Debug)]
struct MockExprSchema {
field: FieldRef,
diff --git a/datafusion/sqllogictest/test_files/prepare.slt b/datafusion/sqllogictest/test_files/prepare.slt
index 7513889666..108b023ccf 100644
--- a/datafusion/sqllogictest/test_files/prepare.slt
+++ b/datafusion/sqllogictest/test_files/prepare.slt
@@ -472,3 +472,40 @@ EXECUTE my_plan(1, 2)
statement ok
DEALLOCATE my_plan
+
+# Untyped aliased placeholders in UNION arms
+statement ok
+PREPARE my_plan AS SELECT $1 AS a UNION ALL SELECT $2 AS a;
+
+query T rowsort
+EXECUTE my_plan('x', 'y')
+----
+x
+y
+
+statement ok
+DEALLOCATE my_plan
+
+statement ok
+create table keys_t (k1 varchar, k2 varchar) as values ('a0', 'b0'), ('a1', 'b1'), ('a2', 'b2');
+
+statement ok
+PREPARE my_plan AS
+WITH keys AS (
+ SELECT $1 AS k1, $2 AS k2
+ UNION ALL
+ SELECT $3 AS k1, $4 AS k2
+)
+SELECT t.k1 FROM keys_t t JOIN keys ON t.k1 = keys.k1 AND t.k2 = keys.k2;
+
+query T rowsort
+EXECUTE my_plan('a0', 'b0', 'a2', 'b2')
+----
+a0
+a2
+
+statement ok
+DEALLOCATE my_plan
+
+statement ok
+drop table keys_t