Skip to content

Commit

Permalink
Raise a plan error on union if column count is not the same between p…
Browse files Browse the repository at this point in the history
…lans. (#13117)
  • Loading branch information
Omega359 authored Oct 28, 2024
1 parent 5db2740 commit e22d231
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 9 additions & 0 deletions datafusion/expr/src/logical_plan/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,15 @@ pub fn validate_unique_names<'a>(
/// [`TypeCoercionRewriter::coerce_union`]: https://docs.rs/datafusion-optimizer/latest/datafusion_optimizer/analyzer/type_coercion/struct.TypeCoercionRewriter.html#method.coerce_union
/// [`coerce_union_schema`]: https://docs.rs/datafusion-optimizer/latest/datafusion_optimizer/analyzer/type_coercion/fn.coerce_union_schema.html
pub fn union(left_plan: LogicalPlan, right_plan: LogicalPlan) -> Result<LogicalPlan> {
if left_plan.schema().fields().len() != right_plan.schema().fields().len() {
return plan_err!(
"UNION queries have different number of columns: \
left has {} columns whereas right has {} columns",
left_plan.schema().fields().len(),
right_plan.schema().fields().len()
);
}

// Temporarily use the schema from the left input and later rely on the analyzer to
// coerce the two schemas into a common one.

Expand Down
4 changes: 2 additions & 2 deletions datafusion/sqllogictest/test_files/type_coercion.slt
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ CREATE TABLE orders(
);

# union_different_num_columns_error() / UNION
query error Error during planning: Union schemas have different number of fields: query 1 has 1 fields whereas query 2 has 2 fields
query error DataFusion error: Error during planning: UNION queries have different number of columns: left has 1 columns whereas right has 2 columns
SELECT order_id FROM orders UNION SELECT customer_id, o_item_id FROM orders

# union_different_num_columns_error() / UNION ALL
query error Error during planning: Union schemas have different number of fields: query 1 has 1 fields whereas query 2 has 2 fields
query error DataFusion error: Error during planning: UNION queries have different number of columns: left has 1 columns whereas right has 2 columns
SELECT order_id FROM orders UNION ALL SELECT customer_id, o_item_id FROM orders

# union_with_different_column_names()
Expand Down

0 comments on commit e22d231

Please sign in to comment.