gustavodemorais commented on code in PR #29653: URL: https://github.com/apache/flink/pull/29653#discussion_r4524000930
########## flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/metadata/FlinkRelMdPredicates.java: ########## @@ -0,0 +1,130 @@ +/* + * 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.metadata; + +import org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalCalc; +import org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalMultiJoin; + +import org.apache.calcite.plan.RelOptPredicateList; +import org.apache.calcite.plan.RelOptUtil; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.Join; +import org.apache.calcite.rel.core.JoinRelType; +import org.apache.calcite.rel.core.Project; +import org.apache.calcite.rel.metadata.BuiltInMetadata; +import org.apache.calcite.rel.metadata.ReflectiveRelMetadataProvider; +import org.apache.calcite.rel.metadata.RelMdPredicates; +import org.apache.calcite.rel.metadata.RelMetadataProvider; +import org.apache.calcite.rel.metadata.RelMetadataQuery; +import org.apache.calcite.rex.RexInputRef; +import org.apache.calcite.rex.RexLocalRef; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexPermuteInputsShuttle; +import org.apache.calcite.rex.RexProgram; +import org.apache.calcite.rex.RexUtil; +import org.apache.calcite.util.ImmutableBitSet; +import org.apache.calcite.util.mapping.Mapping; +import org.apache.calcite.util.mapping.MappingType; +import org.apache.calcite.util.mapping.Mappings; + +import java.util.ArrayList; +import java.util.List; + +/** + * A metadata handler for {@link BuiltInMetadata.Predicates}, which adds {@link StreamPhysicalCalc} + * and {@link StreamPhysicalMultiJoin} to {@link RelMdPredicates}. Other Calcs are left out so that + * the predicates seen by logical rules, e.g. ReduceExpressionsRule, don't change. + */ +public class FlinkRelMdPredicates extends RelMdPredicates { Review Comment: nice to have: a `FlinkRelMdPredicatesTest` like the other `FlinkRelMd*` handlers have? ########## flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/utils/UpsertKeyUtil.java: ########## @@ -63,4 +78,97 @@ public static Optional<int[]> smallestKey(@Nullable Set<ImmutableBitSet> upsertK upsertKeys.stream().map(ImmutableBitSet::toArray).collect(Collectors.toList()); return Optional.of(UpsertKeyUtils.smallestKey(asArrays)); } + + /** + * Returns the upsert keys of the given node, plus each key whose columns outside {@code + * columns} can all be replaced by columns inside that are equal to them on every row, e.g. + * through an inner equi-join condition. For example, for {@code t1 JOIN t2 ON a1 = a2} and + * columns {a1, b2}, the upsert key {a2, b2} adds {a1, b2}. + * + * @return the upsert keys and the aligned ones, or null if the upsert keys are unknown + */ + public static @Nullable Set<ImmutableBitSet> getUpsertKeysAlignedTo( Review Comment: nit: `getUpsertKeysAlignedTo` was a bit hard to parse for me at the call sites. I actually think `getUpsertKeysUsingEqualColumns` is a bit clearer. Let me know if you have other suggestions ########## flink-table/flink-table-planner/src/test/scala/org/apache/flink/table/planner/plan/stream/sql/join/JoinTest.scala: ########## @@ -815,4 +815,421 @@ class JoinTest extends TableTestBase { "Key: 'table.exec.mini-batch.size' , default: -1 (fallback keys: []) must be > 0.") .isInstanceOf[IllegalArgumentException] } + + private def createUpsertSource( + name: String, + columns: String, + primaryKey: String, + deleteByKey: Boolean = false): Unit = + util.tableEnv.executeSql(s""" + |create table $name ( + | $columns, + | primary key ($primaryKey) not enforced + |) with ( + | 'connector' = 'values', + | 'changelog-mode' = 'I,UA,D', + | 'source.produces-delete-by-key' = '$deleteByKey' + |) + |""".stripMargin) + + private def createUpsertSink( + columns: String, + primaryKey: String, + deleteByKey: Boolean = false): Unit = + util.tableEnv.executeSql(s""" + |create table sink ( + | $columns, + | primary key ($primaryKey) not enforced + |) with ( + | 'connector' = 'values', + | 'sink-insert-only' = 'false', + | 'sink.supports-delete-by-key' = '$deleteByKey' + |) + |""".stripMargin) + + @Test + def testJoinUpsertKeyEquivalenceInnerJoinBasic(): Unit = { Review Comment: nice to have: a FLOAT/DOUBLE (or MAP) negative case, where the materializer has to stay. -- 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]
