godfreyhe commented on a change in pull request #8898: [FLINK-12936][table-planner-blink] Support intersect all / minus all to blink planner URL: https://github.com/apache/flink/pull/8898#discussion_r300529061
########## File path: flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/plan/rules/logical/RewriteIntersectAllRule.scala ########## @@ -0,0 +1,141 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.table.plan.rules.logical + +import org.apache.flink.table.functions.sql.FlinkSqlOperatorTable.{GREATER_THAN, GREATER_THAN_OR_EQUAL, IF} +import org.apache.flink.table.plan.util.SetOpRewriteUtil.replicateRows + +import org.apache.calcite.plan.RelOptRule.{any, operand} +import org.apache.calcite.plan.{RelOptRule, RelOptRuleCall} +import org.apache.calcite.rel.core.{Intersect, RelFactories} +import org.apache.calcite.sql.`type`.SqlTypeName +import org.apache.calcite.util.Util + +import scala.collection.JavaConversions._ + +/** + * Replaces logical [[Intersect]] operator using a combination of union all, aggregate + * and table function. + * + * Original Query : + * {{{ + * SELECT c1 FROM ut1 INTERSECT ALL SELECT c1 FROM ut2 + * }}} + * + * Rewritten Query: + * {{{ + * SELECT c1 + * FROM ( + * SELECT c1, If (vcol_left_cnt > vcol_right_cnt, vcol_right_cnt, vcol_left_cnt) AS min_count + * FROM ( + * SELECT + * c1, + * count(vcol_left_marker) as vcol_left_cnt, + * count(vcol_right_marker) as vcol_right_cnt + * FROM ( + * SELECT c1, true as vcol_left_marker, null as vcol_right_marker FROM ut1 + * UNION ALL + * SELECT c1, null as vcol_left_marker, true as vcol_right_marker FROM ut2 + * ) AS union_all + * GROUP BY c1 + * ) + * WHERE vcol_left_cnt >= 1 AND vcol_right_cnt >= 1 + * ) + * ) + * LATERAL TABLE(replicate_row(min_count, c1)) AS T(c1) + * }}} + */ +class RewriteIntersectAllRule extends RelOptRule( + operand(classOf[Intersect], any), + RelFactories.LOGICAL_BUILDER, + "RewriteIntersectAllRule") { + + override def matches(call: RelOptRuleCall): Boolean = { + val intersect: Intersect = call.rel(0) + intersect.all && intersect.getInputs.size() == 2 Review comment: add a `TODO` ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
