JingsongLi 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_r300531386
########## File path: flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/plan/rules/logical/RewriteMinusAllRule.scala ########## @@ -0,0 +1,117 @@ +/* + * 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 +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.{Minus, RelFactories} +import org.apache.calcite.sql.`type`.SqlTypeName.BIGINT +import org.apache.calcite.util.Util + +import scala.collection.JavaConversions._ + +/** + * Replaces logical [[Minus]] operator using a combination of union all, aggregate + * and table function. + * + * Original Query : + * {{{ + * SELECT c1 FROM ut1 EXCEPT ALL SELECT c1 FROM ut2 + * }}} + * + * Rewritten Query: + * {{{ + * SELECT c1 + * FROM ( + * SELECT c1, sum_val + * FROM ( + * SELECT c1, sum(vcol) AS sum_val + * FROM ( + * SELECT c1, 1L as vcol FROM ut1 + * UNION ALL + * SELECT c1, -1L as vcol FROM ut2 + * ) AS union_all + * GROUP BY union_all.c1 + * ) + * WHERE sum_val > 0 + * ) + * LATERAL TABLE(replicate_row(sum_val, c1)) AS T(c1) + * }}} + */ +class RewriteMinusAllRule extends RelOptRule( + operand(classOf[Minus], any), + RelFactories.LOGICAL_BUILDER, + "RewriteMinusAllRule") { + + override def matches(call: RelOptRuleCall): Boolean = { + val minus: Minus = call.rel(0) + minus.all && minus.getInputs.size() == 2 + } + + override def onMatch(call: RelOptRuleCall): Unit = { + val minus: Minus = call.rel(0) + val left = minus.getInput(0) + val right = minus.getInput(1) + + val fields = Util.range(minus.getRowType.getFieldCount) + + // 1. add vcol to left rel node + val leftBuilder = call.builder + val leftWithAddedVirtualCols = leftBuilder + .push(left) + .project(leftBuilder.fields(fields) ++ + Seq(leftBuilder.alias(leftBuilder.cast(leftBuilder.literal(1L), BIGINT), "vcol"))) Review comment: name it `vcol_marker`, there is no appropriate name to describe it. ---------------------------------------------------------------- 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
