xudong963 commented on a change in pull request #1135:
URL: https://github.com/apache/arrow-datafusion/pull/1135#discussion_r743633877



##########
File path: datafusion/src/sql/planner.rs
##########
@@ -191,23 +191,32 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
                 left,
                 right,
                 all,
-            } => match (op, all) {
+            } => {
+                let left_plan = self.set_expr_to_plan(left.as_ref(), None, 
ctes)?;
+                let right_plan = self.set_expr_to_plan(right.as_ref(), None, 
ctes)?;
+                match (op, all) {
                 (SetOperator::Union, true) => {
-                    let left_plan = self.set_expr_to_plan(left.as_ref(), None, 
ctes)?;
-                    let right_plan = self.set_expr_to_plan(right.as_ref(), 
None, ctes)?;
                     union_with_alias(left_plan, right_plan, alias)
                 }
                 (SetOperator::Union, false) => {
-                    let left_plan = self.set_expr_to_plan(left.as_ref(), None, 
ctes)?;
-                    let right_plan = self.set_expr_to_plan(right.as_ref(), 
None, ctes)?;
                     let union_plan = union_with_alias(left_plan, right_plan, 
alias)?;
                     LogicalPlanBuilder::from(union_plan).distinct()?.build()
                 }
+                (SetOperator::Intersect, true) => {
+                    let join_keys = 
left_plan.schema().fields().iter().zip(right_plan.schema().fields().iter()).map(|(left_field,
 right_field)| ((Column::from_name(left_field.name())), 
(Column::from_name(right_field.name())))).unzip();
+                    LogicalPlanBuilder::from(left_plan).join(&right_plan, 
JoinType::Semi, join_keys, true)?.build()
+                }
+                (SetOperator::Intersect, false) => {
+                    let distinct_left_plan = 
LogicalPlanBuilder::from(left_plan).distinct()?.build()?;
+                    let join_keys = 
distinct_left_plan.schema().fields().iter().zip(right_plan.schema().fields().iter()).map(|(left_field,
 right_field)| ((Column::from_name(left_field.name())), 
(Column::from_name(right_field.name())))).unzip();
+                    
LogicalPlanBuilder::from(distinct_left_plan).join(&right_plan, JoinType::Semi, 
join_keys, true)?.build()

Review comment:
       Good caught!




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to