rionmonster commented on code in PR #30217:
URL: https://github.com/apache/flink/pull/30217#discussion_r4754864438


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/join/stream/AbstractStreamingJoinOperator.java:
##########
@@ -126,88 +123,38 @@ public void close() throws Exception {
         }
     }
 
-    /**
-     * Returns the stored record that the given record replaces, or null if 
there is none. An upsert
-     * input updates a record by sending only the new version, without 
retracting the old one.
-     *
-     * <p>The lookup only happens if it can change the result.
-     */
-    protected @Nullable RowData replacedRecord(
-            RowData record, JoinRecordStateView stateView, boolean isLeft, 
boolean otherIsOuter)
-            throws Exception {
-        return needsReplacedRecord(isLeft, otherIsOuter) ? 
stateView.getRecord(record) : null;
+    /** Creates the state view of the records of one input, with a count of 
matches if outer. */
+    protected JoinRecordStateView createRecordStateView(boolean isLeft, 
boolean isOuter) {
+        final String stateName = isLeft ? LEFT_RECORDS_STATE_NAME : 
RIGHT_RECORDS_STATE_NAME;
+        final JoinInputSideSpec inputSideSpec = isLeft ? leftInputSideSpec : 
rightInputSideSpec;
+        final InternalTypeInfo<RowData> recordType = isLeft ? leftType : 
rightType;
+        final long retentionTime = isLeft ? leftStateRetentionTime : 
rightStateRetentionTime;
+        return isOuter
+                ? OuterJoinRecordStateViews.create(
+                        getRuntimeContext(), stateName, inputSideSpec, 
recordType, retentionTime)
+                : JoinRecordStateViews.create(
+                        getRuntimeContext(), stateName, inputSideSpec, 
recordType, retentionTime);

Review Comment:
   It looks like `StreamingSemiAntiJoinOperator#open` is still constructing 
these views the same way (e.g. same state names, specs, retention, etc.). 
Should we try and identify any of the other operators that might be able to 
take advantage of it? 
   
   Totally okay if that doesn't make sense as well, since I think the per-step 
methods being introduced here make the joins _much_ easier to reason about (and 
worth the change).



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/join/stream/StreamingJoinLogic.java:
##########
@@ -0,0 +1,439 @@
+/*
+ * 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;
+
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.util.RowDataUtil;
+import org.apache.flink.table.data.utils.JoinedRowData;
+import 
org.apache.flink.table.runtime.operators.join.JoinConditionWithNullFilters;
+import 
org.apache.flink.table.runtime.operators.join.stream.AbstractStreamingJoinOperator.OuterRecord;
+import 
org.apache.flink.table.runtime.operators.join.stream.state.JoinRecordStateView;
+import 
org.apache.flink.table.runtime.operators.join.stream.state.OuterJoinRecordStateView;
+import org.apache.flink.table.runtime.typeutils.InternalTypeInfo;
+import org.apache.flink.types.RowKind;
+import org.apache.flink.util.Collector;
+
+import javax.annotation.Nullable;
+
+import java.util.Iterator;
+
+import static 
org.apache.flink.table.runtime.operators.join.stream.AbstractStreamingJoinOperator.iterator;
+import static 
org.apache.flink.table.runtime.operators.join.stream.AbstractStreamingJoinOperator.matches;
+
+/**
+ * The join logic of {@link StreamingJoinOperator}: joins a record of one 
input with the records
+ * stored for the other input. The state views are passed with each record, so 
an operator decides
+ * which state each input is stored in.
+ *
+ * <p>An accumulate record (+I or +U) is joined with the matching records of 
the other side and
+ * stored. A retract record (-U or -D) is removed from the state, and the 
joined rows it produced
+ * are retracted. A record of an outer side is joined with nulls while it has 
no match (null
+ * padding). Each stored record of an outer side counts its matches 
(associations), so that its null
+ * padding is retracted when it gains its first match and sent again when it 
loses its last one.
+ *
+ * <p>Joined rows keep the RowKind of the input record where the outer sides 
allow it, and are sent
+ * as +I and -D otherwise, which is equivalent for this join. Sending -U and 
+U instead may need
+ * some refactoring, see FLINK-17337 and {@code
+ * FlinkChangelogModeInferenceProgram.SatisfyModifyKindSetTraitVisitor}.
+ */
+final class StreamingJoinLogic {
+
+    private final JoinConditionWithNullFilters joinCondition;
+    private final Collector<RowData> collector;
+    private final JoinInputLookups lookups;
+    // whether the join condition has a non-equi part, e.g. A JOIN B ON A.k = 
B.k AND A.v > B.v
+    private final boolean hasNonEquiCondition;

Review Comment:
   Minor nit: Noticed that `JoinInputLookups#hasNonEquiCondition` seems to also 
hold this same flag as a field. Since we have `lookups` already defined, do we 
need this one (since we could just reference it via 
`lookups.hasNotEquiCondition()` in the `accumulate()` and `isNewMatch()` 
functions?
   
   I agree that having this flag _reads_ much more cleanly, just want to ensure 
we aren't paying any type of cost related to it.



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/join/stream/JoinInputLookups.java:
##########
@@ -0,0 +1,158 @@
+/*
+ * 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;
+
+import org.apache.flink.table.connector.ChangelogMode;
+import org.apache.flink.table.data.RowData;
+import 
org.apache.flink.table.runtime.operators.join.stream.state.JoinRecordStateView;
+import 
org.apache.flink.table.runtime.operators.join.stream.utils.JoinInputSideSpec;
+import org.apache.flink.table.runtime.util.RuntimeChangelogMode;
+import org.apache.flink.types.RowKind;
+
+import javax.annotation.Nullable;
+
+import java.io.Serializable;
+
+/**
+ * Decides when a streaming join looks up stored records of the input a record 
comes from, and looks
+ * them up. These two lookups are the only reads of an input's own records: 
the record an upsert
+ * replaces, and the full record of a DELETE that may contain only the unique 
key.
+ */
+final class JoinInputLookups implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    private final JoinInputSideSpec leftInputSideSpec;
+    private final JoinInputSideSpec rightInputSideSpec;
+
+    // whether the input may update a record without retracting it first 
(upsert), so that the
+    // record it replaces has to be looked up in state
+    private final boolean leftIsUpsertOrUnknown;
+    private final boolean rightIsUpsertOrUnknown;
+    // whether a DELETE of the input may contain only the unique key, so that 
the full record has
+    // to be looked up in state to evaluate a non-equi condition
+    private final boolean leftHasKeyOnlyDeletesOrUnknown;
+    private final boolean rightHasKeyOnlyDeletesOrUnknown;
+    // whether the join condition has a non-equi part, e.g. A JOIN B ON A.k = 
B.k AND A.v > B.v
+    private final boolean hasNonEquiCondition;
+
+    JoinInputLookups(
+            JoinInputSideSpec leftInputSideSpec,
+            JoinInputSideSpec rightInputSideSpec,
+            @Nullable RuntimeChangelogMode leftInputChangelogMode,
+            @Nullable RuntimeChangelogMode rightInputChangelogMode,
+            boolean hasNonEquiCondition) {
+        this.leftInputSideSpec = leftInputSideSpec;
+        this.rightInputSideSpec = rightInputSideSpec;
+        this.leftIsUpsertOrUnknown = isUpsertOrUnknown(leftInputChangelogMode);
+        this.rightIsUpsertOrUnknown = 
isUpsertOrUnknown(rightInputChangelogMode);
+        this.leftHasKeyOnlyDeletesOrUnknown = 
hasKeyOnlyDeletesOrUnknown(leftInputChangelogMode);
+        this.rightHasKeyOnlyDeletesOrUnknown = 
hasKeyOnlyDeletesOrUnknown(rightInputChangelogMode);
+        this.hasNonEquiCondition = hasNonEquiCondition;
+    }
+
+    /**
+     * Returns the stored record that the given record replaces, or null if 
there is none. An upsert
+     * input updates a record by sending only the new version, without 
retracting the old one.
+     *
+     * <p>The lookup only happens if it can change the result.
+     */
+    @Nullable
+    RowData replacedRecord(
+            RowData record, JoinRecordStateView stateView, boolean isLeft, 
boolean otherIsOuter)
+            throws Exception {
+        return needsReplacedRecord(isLeft, otherIsOuter) ? 
stateView.getRecord(record) : null;
+    }
+
+    /**
+     * Returns whether looking up the record that an input record replaces can 
change the result.
+     */
+    boolean needsReplacedRecord(boolean isLeft, boolean otherIsOuter) {
+        final boolean inputIsUpsertOrUnknown =
+                isLeft ? leftIsUpsertOrUnknown : rightIsUpsertOrUnknown;
+        // without a unique key, an equal record is a second record and not an 
update
+        final boolean mayReplace = inputIsUpsertOrUnknown && 
hasUniqueKey(isLeft);
+        // with at most one record per join key, the count of the other side 
is right without it
+        final boolean mayCountTwice = otherIsOuter && 
!joinKeyContainsUniqueKey(isLeft);
+        final boolean mayMatchDifferently = hasNonEquiCondition;
+        return mayReplace && (mayCountTwice || mayMatchDifferently);
+    }

Review Comment:
   I noticed that we didn't have any explicit tests that verify this directly 
(e.g. determining if replacement is required). Is it worth introducing a small 
pair of tests to exercise these helpers like `needsReplacedRecord` or 
`needsStoredRecordOfDelete`?
   
   The existing coverage for joins and test suites that exist are _probably_ 
enough since this is just a refactoring with no new logic being introduced, but 
wanted to call it out.



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/join/stream/StreamingSemiAntiJoinOperator.java:
##########


Review Comment:
   Similar to the previous comment, is there a way that we could leverage the 
same `AbstractStreamingJoinOperator#createRecordStateView` helper function here 
(if anything just to ensure that we have multiple callers taking advantage of 
it, if possible). 



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