MartijnVisser commented on code in PR #29551:
URL: https://github.com/apache/flink/pull/29551#discussion_r4789312631


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/join/stream/StreamingMultiJoinOperator.java:
##########
@@ -902,6 +907,21 @@ private void handleInsertAfterInput(
     private void addRecordToState(RowData input, @Nullable RowData joinKey, 
int inputId)
             throws Exception {
         final boolean isUpsert = isUpsert(input);
+        // A record that can never match isn't stored. Retractions still go to 
state: removing a
+        // record that was never stored is a no-op, and one stored by an 
earlier version is removed.
+        // Records of inputs with updates without UPDATE_BEFORE or deletes by 
key are always
+        // stored, because their next version or delete is completed from 
state.
+        if (isUpsert
+                && !isUpsertInput[inputId]
+                && !hasKeyOnlyDeletes[inputId]
+                && cannotMatch(input, inputId)) {
+            // Storing an upsert replaces the previous version of the record 
with the same unique
+            // key, which may arrive without UPDATE_BEFORE, so that version is 
removed instead.
+            if (inputSpecs.get(inputId).hasUniqueKey()) {

Review Comment:
   Since FLINK-41066, upsert inputs and inputs with key-only deletes are never 
skipped, so I think this removal only matters for older plans. Can you note 
that here and under the limitations?



##########
flink-table/flink-table-runtime/src/test/java/org/apache/flink/table/runtime/operators/join/stream/multijoin/StreamingMultiJoinNullKeyOperatorTest.java:
##########
@@ -0,0 +1,210 @@
+/*
+ * 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.runtime.operators.join.stream.multijoin;
+
+import org.apache.flink.runtime.checkpoint.OperatorSubtaskState;
+import org.apache.flink.runtime.operators.testutils.MockEnvironment;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.runtime.generated.GeneratedJoinCondition;
+import org.apache.flink.table.runtime.generated.JoinCondition;
+import org.apache.flink.table.runtime.operators.join.FlinkJoinType;
+import 
org.apache.flink.table.runtime.operators.join.stream.keyselector.AttributeBasedJoinKeyExtractor.ConditionAttributeRef;
+import org.apache.flink.table.runtime.typeutils.InternalTypeInfo;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.VarCharType;
+import 
org.apache.flink.testutils.junit.extensions.parameterized.ParameterizedTestExtension;
+
+import org.junit.jupiter.api.TestTemplate;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * Tests that the multi-way join doesn't store records with a NULL in a field 
in which a NULL means
+ * that they can never match, and that its output doesn't change.
+ *
+ * <p>SQL: SELECT u.*, o.*, p.* FROM Users u JOIN Orders o ON u.user_id = 
o.user_id AND u.details =
+ * o.details LEFT JOIN Payments p ON o.user_id = p.user_id
+ *
+ * <p>Schema: Users(user_id PRIMARY KEY, name, details), Orders(user_id, 
order_id PRIMARY KEY,
+ * details), Payments(user_id, payment_id PRIMARY KEY, details)
+ */
+@ExtendWith(ParameterizedTestExtension.class)
+class StreamingMultiJoinNullKeyOperatorTest extends 
StreamingMultiJoinOperatorTestBase {
+
+    // The inner join filters user_id and details of Users and Orders, the 
left join filters
+    // user_id of Payments.
+    private static final int[][] NULL_FILTER_FIELDS = {{0, 2}, {0, 2}, {0}};
+
+    private boolean filterNulls = true;
+
+    public StreamingMultiJoinNullKeyOperatorTest(StateBackendMode 
stateBackendMode) {
+        super(
+                stateBackendMode,
+                3,
+                List.of(FlinkJoinType.INNER, FlinkJoinType.INNER, 
FlinkJoinType.LEFT),
+                Arrays.asList(
+                        null,
+                        createAndCondition(createJoinCondition(1, 0), 
createDetailsCondition()),
+                        createJoinCondition(2, 1)),
+                Map.of(
+                        1,
+                        List.of(
+                                new ConditionAttributeRef(0, 0, 1, 0),
+                                new ConditionAttributeRef(0, 2, 1, 2)),
+                        2,
+                        List.of(new ConditionAttributeRef(1, 0, 2, 0))),
+                false);
+    }
+
+    @TestTemplate
+    void testDoesNotStoreRecordsThatCanNeverMatch() throws Exception {
+        insertUser(null, "Gus", "EU");
+        insertUser("1", "Bob", null);
+        insertOrder(null, "order_1", "EU");
+        insertOrder("1", "order_2", null);
+        insertPayment(null, "payment_1", "EU");
+        emitsNothing();
+        assertThat(testHarness.numKeyedStateEntries()).isZero();
+
+        deleteUser(null, "Gus", "EU");
+        deleteUser("1", "Bob", null);
+        deleteOrder(null, "order_1", "EU");
+        deleteOrder("1", "order_2", null);
+        deletePayment(null, "payment_1", "EU");
+        emitsNothing();
+        assertThat(testHarness.numKeyedStateEntries()).isZero();
+    }
+
+    @TestTemplate
+    void testStoresRecordsThatCanMatch() throws Exception {
+        insertUser("1", "Gus", "EU");
+        insertOrder(null, "order_1", "EU");
+        insertOrder("1", "order_2", null);
+        emitsNothing();
+        insertOrder("1", "order_3", "EU");
+        emits(INSERT, "1", "Gus", "EU", "1", "order_3", "EU", null, null, 
null);
+        insertPayment(null, "payment_1", "EU");
+        emitsNothing();
+        insertPayment("1", "payment_2", null);
+        emits(
+                DELETE,
+                r("1", "Gus", "EU", "1", "order_3", "EU", null, null, null),
+                INSERT,
+                r("1", "Gus", "EU", "1", "order_3", "EU", "1", "payment_2", 
null));
+        assertThat(testHarness.numKeyedStateEntries()).isEqualTo(3);
+    }
+
+    @TestTemplate
+    void testUpdateAfterReplacesStoredRecord() throws Exception {
+        // Without UPDATE_BEFORE, as for an input whose unique key contains 
the common join key

Review Comment:
   These tests pass no changelog modes, so they run as a plan from before 
FLINK-41066 and this comment doesn't apply. A case with known modes would cover 
the upsert guard too.



##########
flink-table/flink-table-runtime/src/test/java/org/apache/flink/table/runtime/operators/join/stream/multijoin/StreamingMultiJoinNullKeyOperatorTest.java:
##########
@@ -0,0 +1,210 @@
+/*
+ * 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.runtime.operators.join.stream.multijoin;
+
+import org.apache.flink.runtime.checkpoint.OperatorSubtaskState;
+import org.apache.flink.runtime.operators.testutils.MockEnvironment;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.runtime.generated.GeneratedJoinCondition;
+import org.apache.flink.table.runtime.generated.JoinCondition;
+import org.apache.flink.table.runtime.operators.join.FlinkJoinType;
+import 
org.apache.flink.table.runtime.operators.join.stream.keyselector.AttributeBasedJoinKeyExtractor.ConditionAttributeRef;
+import org.apache.flink.table.runtime.typeutils.InternalTypeInfo;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.VarCharType;
+import 
org.apache.flink.testutils.junit.extensions.parameterized.ParameterizedTestExtension;
+
+import org.junit.jupiter.api.TestTemplate;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * Tests that the multi-way join doesn't store records with a NULL in a field 
in which a NULL means
+ * that they can never match, and that its output doesn't change.
+ *
+ * <p>SQL: SELECT u.*, o.*, p.* FROM Users u JOIN Orders o ON u.user_id = 
o.user_id AND u.details =
+ * o.details LEFT JOIN Payments p ON o.user_id = p.user_id
+ *
+ * <p>Schema: Users(user_id PRIMARY KEY, name, details), Orders(user_id, 
order_id PRIMARY KEY,
+ * details), Payments(user_id, payment_id PRIMARY KEY, details)
+ */
+@ExtendWith(ParameterizedTestExtension.class)
+class StreamingMultiJoinNullKeyOperatorTest extends 
StreamingMultiJoinOperatorTestBase {
+
+    // The inner join filters user_id and details of Users and Orders, the 
left join filters
+    // user_id of Payments.
+    private static final int[][] NULL_FILTER_FIELDS = {{0, 2}, {0, 2}, {0}};
+
+    private boolean filterNulls = true;
+
+    public StreamingMultiJoinNullKeyOperatorTest(StateBackendMode 
stateBackendMode) {
+        super(
+                stateBackendMode,
+                3,
+                List.of(FlinkJoinType.INNER, FlinkJoinType.INNER, 
FlinkJoinType.LEFT),
+                Arrays.asList(
+                        null,
+                        createAndCondition(createJoinCondition(1, 0), 
createDetailsCondition()),
+                        createJoinCondition(2, 1)),
+                Map.of(
+                        1,
+                        List.of(
+                                new ConditionAttributeRef(0, 0, 1, 0),
+                                new ConditionAttributeRef(0, 2, 1, 2)),
+                        2,
+                        List.of(new ConditionAttributeRef(1, 0, 2, 0))),
+                false);
+    }
+
+    @TestTemplate
+    void testDoesNotStoreRecordsThatCanNeverMatch() throws Exception {
+        insertUser(null, "Gus", "EU");
+        insertUser("1", "Bob", null);
+        insertOrder(null, "order_1", "EU");
+        insertOrder("1", "order_2", null);
+        insertPayment(null, "payment_1", "EU");
+        emitsNothing();
+        assertThat(testHarness.numKeyedStateEntries()).isZero();
+
+        deleteUser(null, "Gus", "EU");
+        deleteUser("1", "Bob", null);
+        deleteOrder(null, "order_1", "EU");
+        deleteOrder("1", "order_2", null);
+        deletePayment(null, "payment_1", "EU");
+        emitsNothing();
+        assertThat(testHarness.numKeyedStateEntries()).isZero();
+    }
+
+    @TestTemplate
+    void testStoresRecordsThatCanMatch() throws Exception {
+        insertUser("1", "Gus", "EU");
+        insertOrder(null, "order_1", "EU");
+        insertOrder("1", "order_2", null);
+        emitsNothing();
+        insertOrder("1", "order_3", "EU");
+        emits(INSERT, "1", "Gus", "EU", "1", "order_3", "EU", null, null, 
null);
+        insertPayment(null, "payment_1", "EU");
+        emitsNothing();
+        insertPayment("1", "payment_2", null);
+        emits(
+                DELETE,
+                r("1", "Gus", "EU", "1", "order_3", "EU", null, null, null),
+                INSERT,
+                r("1", "Gus", "EU", "1", "order_3", "EU", "1", "payment_2", 
null));
+        assertThat(testHarness.numKeyedStateEntries()).isEqualTo(3);
+    }
+
+    @TestTemplate
+    void testUpdateAfterReplacesStoredRecord() throws Exception {
+        // Without UPDATE_BEFORE, as for an input whose unique key contains 
the common join key
+        insertUser("1", "Gus", "EU");
+        assertThat(testHarness.numKeyedStateEntries()).isOne();
+        updateAfterUser("1", "Gus", null);
+        emitsNothing();
+        assertThat(testHarness.numKeyedStateEntries()).isZero();
+
+        insertOrder("1", "order_1", "EU");
+        emitsNothing();
+    }
+
+    @TestTemplate
+    void testInsertReplacesStoredRecord() throws Exception {
+        insertUser("1", "Gus", "EU");
+        assertThat(testHarness.numKeyedStateEntries()).isOne();
+        insertUser("1", "Gus", null);
+        emitsNothing();
+        assertThat(testHarness.numKeyedStateEntries()).isZero();
+
+        insertOrder("1", "order_1", "EU");
+        emitsNothing();
+    }
+
+    @TestTemplate
+    void testRetractsRecordStoredByEarlierVersion() throws Exception {

Review Comment:
   If retractions with a NULL are skipped too, this still passes: Orders has a 
unique key, so the previous-version removal deletes the row. Can you add an 
input without a unique key?



-- 
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]

Reply via email to