Skip to content

Commit e22d231

Browse files
authored
Raise a plan error on union if column count is not the same between plans. (#13117)
1 parent 5db2740 commit e22d231

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

datafusion/expr/src/logical_plan/builder.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1482,6 +1482,15 @@ pub fn validate_unique_names<'a>(
14821482
/// [`TypeCoercionRewriter::coerce_union`]: https://docs.rs/datafusion-optimizer/latest/datafusion_optimizer/analyzer/type_coercion/struct.TypeCoercionRewriter.html#method.coerce_union
14831483
/// [`coerce_union_schema`]: https://docs.rs/datafusion-optimizer/latest/datafusion_optimizer/analyzer/type_coercion/fn.coerce_union_schema.html
14841484
pub fn union(left_plan: LogicalPlan, right_plan: LogicalPlan) -> Result<LogicalPlan> {
1485+
if left_plan.schema().fields().len() != right_plan.schema().fields().len() {
1486+
return plan_err!(
1487+
"UNION queries have different number of columns: \
1488+
left has {} columns whereas right has {} columns",
1489+
left_plan.schema().fields().len(),
1490+
right_plan.schema().fields().len()
1491+
);
1492+
}
1493+
14851494
// Temporarily use the schema from the left input and later rely on the analyzer to
14861495
// coerce the two schemas into a common one.
14871496

datafusion/sqllogictest/test_files/type_coercion.slt

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ CREATE TABLE orders(
103103
);
104104

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

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

113113
# union_with_different_column_names()

0 commit comments

Comments
 (0)