pnowojski commented on code in PR #30217:
URL: https://github.com/apache/flink/pull/30217#discussion_r4756246478
##########
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:
Done, `StreamingSemiAntiJoinOperator#open` uses it now, with the same views
as before. The other join operators can't: mini-batch inherits
`StreamingJoinOperator#open`, and multi-join and async-state use other state
views.
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/join/stream/StreamingSemiAntiJoinOperator.java:
##########
Review Comment:
Done, see above.
##########
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:
Added `JoinInputLookupsTest` for both guards. It also covers the first
commit: without the unique-key check, it fails.
##########
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:
No real cost, but it was a duplicate, as was the now unread field in
`AbstractStreamingJoinOperator`. Removed both.
--
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]