fhueske commented on a change in pull request #6792: [FLINK-10474][table] Don't translate IN/NOT_IN to JOIN with VALUES URL: https://github.com/apache/flink/pull/6792#discussion_r224801305
########## File path: flink-libraries/flink-table/src/main/scala/org/apache/flink/table/plan/rules/common/ConvertToNotInOrInRule.scala ########## @@ -0,0 +1,179 @@ +/* + * 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.common + +import org.apache.calcite.plan.RelOptRule.{any, operand} +import org.apache.calcite.plan.{RelOptRule, RelOptRuleCall, RelOptUtil} +import org.apache.calcite.rel.core.Filter +import org.apache.calcite.rex.{RexCall, RexLiteral, RexNode} +import org.apache.calcite.sql.SqlBinaryOperator +import org.apache.calcite.sql.fun.SqlStdOperatorTable.{IN, NOT_IN, EQUALS, NOT_EQUALS, AND, OR} +import org.apache.calcite.tools.RelBuilder + +import scala.collection.JavaConversions._ +import scala.collection.mutable + +/** + * Rule for converting a cascade of predicates to [[IN]] or [[NOT_IN]]. + * + * For example, convert predicate: (x = 1 OR x = 2 OR x = 3) AND y = 4 to + * predicate: x IN (1, 2, 3) AND y = 4. + * + * @param fromOperator The fromOperator, for example, when convert to [[IN]], fromOperator is + * [[EQUALS]]. We convert a cascade of [[EQUALS]] to [[IN]]. + * @param connectOperator The connect operator to connect the fromOperator. + * @param composedOperator The composed operator that may contains sub [[IN]] or [[NOT_IN]]. + * @param toOperator The toOperator, for example, when convert to [[IN]], toOperator is + * [[IN]]. We convert a cascade of [[EQUALS]] to [[IN]]. + * @param description The description of the rule. + */ +class ConvertToNotInOrInRule( + fromOperator: SqlBinaryOperator, + connectOperator: SqlBinaryOperator, + composedOperator: SqlBinaryOperator, + toOperator: SqlBinaryOperator, + description: String) + extends RelOptRule( + operand(classOf[Filter], any), + description) { + + override def onMatch(call: RelOptRuleCall): Unit = { + val filter: Filter = call.rel(0) + convertToNotInOrIn(call.builder(), filter.getCondition) match { + case Some(newRex) => + call.transformTo(filter.copy( + filter.getTraitSet, + filter.getInput, + newRex)) + + case None => // do nothing + } + } + + /** + * Returns a condition decomposed by [[AND]] or [[OR]]. + */ + def decomposedBy(rex: RexNode, operator: SqlBinaryOperator): Seq[RexNode] = { Review comment: can be `private` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on 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
