snuyanzin commented on code in PR #24420: URL: https://github.com/apache/flink/pull/24420#discussion_r1730414834
########## flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/JoinConditionTypeCoerceRule.java: ########## @@ -0,0 +1,178 @@ +/* + * 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.planner.plan.rules.logical; + +import org.apache.flink.table.api.TableException; +import org.apache.flink.table.planner.plan.utils.FlinkRexUtil; + +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.plan.RelOptUtil; +import org.apache.calcite.plan.RelRule; +import org.apache.calcite.rel.core.Join; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rel.type.RelDataTypeFactory; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexCall; +import org.apache.calcite.rex.RexInputRef; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.sql.SqlKind; +import org.apache.calcite.sql.type.SqlTypeUtil; +import org.apache.calcite.tools.RelBuilder; +import org.immutables.value.Value; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +/** + * Planner rule that coerces the both sides of EQUALS(`=`) operator in Join condition to the same + * type while sans nullability. + * + * <p>For most cases, we already did the type coercion during type validation by implicit type + * coercion or during sqlNode to relNode conversion, this rule just does a rechecking to ensure a + * strongly uniform equals type, so that during a HashJoin shuffle we can have the same hashcode of + * the same value. + */ +@Value.Enclosing +public class JoinConditionTypeCoerceRule + extends RelRule<JoinConditionTypeCoerceRule.JoinConditionTypeCoerceRuleConfig> { + + public static final JoinConditionTypeCoerceRule INSTANCE = + JoinConditionTypeCoerceRule.JoinConditionTypeCoerceRuleConfig.DEFAULT.toRule(); + + private JoinConditionTypeCoerceRule(JoinConditionTypeCoerceRuleConfig config) { + super(config); + } + + @Override + public boolean matches(RelOptRuleCall call) { + Join join = call.rel(0); + if (join.getCondition().isAlwaysTrue()) { + return false; + } + RelDataTypeFactory typeFactory = call.builder().getTypeFactory(); + return hasEqualsRefsOfDifferentTypes(typeFactory, join.getCondition()); + } + + @Override + public void onMatch(RelOptRuleCall call) { + Join join = call.rel(0); + RelBuilder builder = call.builder(); + RexBuilder rexBuilder = builder.getRexBuilder(); + RelDataTypeFactory typeFactory = builder.getTypeFactory(); + + List<RexNode> joinFilters = RelOptUtil.conjunctions(join.getCondition()); + List<RexNode> newJoinFilters = + joinFilters.stream() + .map( + filter -> { + if (filter instanceof RexCall) { + RexCall c = (RexCall) filter; + if (c.getKind().equals(SqlKind.EQUALS)) { Review Comment: ```suggestion if (c.getKind() == SqlKind.EQUALS) { ``` it's better to follow same approach in code e.g. below it is compared with `==` which is preferred for enums -- 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: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org