Skip to content

Commit 928032f

Browse files
askoaaskoa
andauthored
Fail if field lengths are not same in INTERSECT and EXPECT (#3674)
* fail if field lengths are not same in INTERSECT and EXPECT * incorporate PR comment * move test per PR comment Co-authored-by: askoa <askoa@local>
1 parent 6d2b417 commit 928032f

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

datafusion/expr/src/logical_plan/builder.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,16 @@ impl LogicalPlanBuilder {
781781
join_type: JoinType,
782782
is_all: bool,
783783
) -> Result<LogicalPlan> {
784+
let left_len = left_plan.schema().fields().len();
785+
let right_len = right_plan.schema().fields().len();
786+
787+
if left_len != right_len {
788+
return Err(DataFusionError::Plan(format!(
789+
"INTERSECT/EXCEPT query must have the same number of columns. Left is {} and right is {}.",
790+
left_len, right_len
791+
)));
792+
}
793+
784794
let join_keys = left_plan
785795
.schema()
786796
.fields()
@@ -1417,4 +1427,21 @@ mod tests {
14171427
]);
14181428
table_scan(Some(name), &schema, None)?.build()
14191429
}
1430+
1431+
#[test]
1432+
fn plan_builder_intersect_different_num_columns_error() -> Result<()> {
1433+
let plan1 = table_scan(None, &employee_schema(), Some(vec![3]))?;
1434+
1435+
let plan2 = table_scan(None, &employee_schema(), Some(vec![3, 4]))?;
1436+
1437+
let expected = "Error during planning: INTERSECT/EXCEPT query must have the same number of columns. \
1438+
Left is 1 and right is 2.";
1439+
let err_msg1 =
1440+
LogicalPlanBuilder::intersect(plan1.build()?, plan2.build()?, true)
1441+
.unwrap_err();
1442+
1443+
assert_eq!(err_msg1.to_string(), expected);
1444+
1445+
Ok(())
1446+
}
14201447
}

0 commit comments

Comments
 (0)