xiedeyantu commented on code in PR #4277: URL: https://github.com/apache/calcite/pull/4277#discussion_r2039475865
########## core/src/main/java/org/apache/calcite/rel/rules/JoinConditionExpandIsNotDistinctFromRule.java: ########## @@ -0,0 +1,159 @@ +/* + * 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.calcite.rel.rules; + +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.plan.RelRule; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.Join; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexCall; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexShuttle; +import org.apache.calcite.rex.RexUtil; +import org.apache.calcite.sql.fun.SqlStdOperatorTable; +import org.apache.calcite.sql.type.ArraySqlType; +import org.apache.calcite.sql.type.SqlTypeName; +import org.apache.calcite.tools.RelBuilder; + +import com.google.common.collect.ImmutableList; + +import org.immutables.value.Value; + +/** + * Planner rule that replaces {@code IS NOT DISTINCT FROM} + * in a {@link Join} condition with logically equivalent operations. + * + * @see SqlStdOperatorTable#IS_NOT_DISTINCT_FROM + * @see RelBuilder#isNotDistinctFrom + */ [email protected] +public final class JoinConditionExpandIsNotDistinctFromRule + extends RelRule<JoinConditionExpandIsNotDistinctFromRule.Config> + implements TransformationRule { + + /** Creates a JoinConditionExpandIsNotDistinctFromRule. */ + JoinConditionExpandIsNotDistinctFromRule(Config config) { + super(config); + } + + //~ Methods ---------------------------------------------------------------- + + @Override public void onMatch(RelOptRuleCall call) { + Join join = call.rel(0); + RexNode oldJoinCond = join.getCondition(); + + RexCall found = + RexUtil.findOperatorCall(SqlStdOperatorTable.IS_NOT_DISTINCT_FROM, + oldJoinCond); + + if (found == null) { + // no longer contains isNotDistinctFromOperator + return; + } + + for (RexNode op : found.getOperands()) { + if (op.getType().getSqlTypeName() == SqlTypeName.MAP) { + // map type cannot be compared + return; + } + } + + RemoveIsNotDistinctFromRexShuttle rewriteShuttle = + new RemoveIsNotDistinctFromRexShuttle( + join.getCluster().getRexBuilder()); + + RelNode newJoin = join.accept(rewriteShuttle); + assert newJoin instanceof Join; + if (((Join) newJoin).getCondition().equals(join.getCondition())) { + return; + } + call.transformTo(newJoin); + } + + //~ Inner Classes ---------------------------------------------------------- + + /** The RexShuttle use for converting 'x IS NOT DISTINCT FROM y' to + * '(COALESCE(x, {zero}) = COALESCE(y, {zero})) + * AND ((x IS NULL) = (y IS NULL))'. */ + private static class RemoveIsNotDistinctFromRexShuttle extends RexShuttle { + final RexBuilder rexBuilder; + + RemoveIsNotDistinctFromRexShuttle( + RexBuilder rexBuilder) { + this.rexBuilder = rexBuilder; + } + + @Override public RexNode visitCall(RexCall call) { + RexNode newCall = super.visitCall(call); + + if (call.getOperator() + == SqlStdOperatorTable.IS_NOT_DISTINCT_FROM) { + RexCall tmpCall = (RexCall) newCall; + + RexNode operand0 = tmpCall.operands.get(0); + RexNode operand1 = tmpCall.operands.get(1); + + RexNode operand0Zero = makeZeroLiteral(rexBuilder, operand0); + RexNode operand1Zero = makeZeroLiteral(rexBuilder, operand1); + + RexNode coalesceCondition = + rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, + rexBuilder.makeCall(SqlStdOperatorTable.COALESCE, + operand0, operand0Zero), + rexBuilder.makeCall(SqlStdOperatorTable.COALESCE, + operand1, operand1Zero)); + + RexNode isNullCondition = + rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, + rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, + operand0), + rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, + operand1)); + + newCall = + rexBuilder.makeCall(SqlStdOperatorTable.AND, + coalesceCondition, isNullCondition); + } + return newCall; + } + } + + private static RexNode makeZeroLiteral(RexBuilder rexBuilder, RexNode operand) { + if (operand.getType().getSqlTypeName() == SqlTypeName.ARRAY) { + ArraySqlType arrayType = (ArraySqlType) operand.getType(); + RelDataType elementType = arrayType.getComponentType(); + RexNode elementZero = rexBuilder.makeZeroLiteral(elementType); Review Comment: @mihaibudiu I have used the new method `makeZeroRexNode` to replace the `makeZeroLiteral`. Could please help me to have a look again? -- 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]
