Github user viirya commented on a diff in the pull request: https://github.com/apache/spark/pull/21886#discussion_r205575498 --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala --- @@ -1407,6 +1408,87 @@ object ReplaceExceptWithAntiJoin extends Rule[LogicalPlan] { } } +/** + * Replaces logical [[Intersect]] operator using a combination of Union, Aggregate + * and Generate operator. + * + * Input Query : + * {{{ + * SELECT c1 FROM ut1 INTERSECT ALL SELECT c1 FROM ut2 + * }}} + * + * Rewritten Query: + * {{{ + * SELECT c1 + * FROM ( + * SELECT replicate_row(min_count, c1) AS (min_count, c1) + * FROM ( + * SELECT c1, + * vcol1_cnt, + * vcol2_cnt, + * IF (vcol1_cnt > vcol1_cnt, vcol2_cnt, vcol1_cnt) AS min_count + * FROM ( + * SELECT c1, count(vcol1) as vcol1_cnt, count(vcol2) as vcol2_cnt + * FROM ( + * SELECT c1, true as vcol1, null as vcol2 FROM ut1 + * UNION ALL + * SELECT c1, null as vcol1, true as vcol2 FROM ut2 --- End diff -- Based on the implementation, I think this should be: ``` SELECT true as vcol1, null as vcol2, c1 FROM ut1 UNION ALL SELECT null as vcol1, true as vcol2, c1 FROM ut2 ```
--- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org