gustavodemorais commented on code in PR #29809:
URL: https://github.com/apache/flink/pull/29809#discussion_r4683485373


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/join/snapshot/JoinKeyBuildStateView.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.snapshot;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.api.common.functions.RuntimeContext;
+import org.apache.flink.api.common.state.ValueState;
+import org.apache.flink.api.common.state.ValueStateDescriptor;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.runtime.typeutils.InternalTypeInfo;
+import org.apache.flink.types.RowKind;
+
+/**
+ * {@link BuildStateView} for a build side whose upsert key is identical to 
the join key. The build
+ * table of a join key holds at most one row, so it is kept in a {@link 
ValueState}.
+ *
+ * <p>Insertions and updates ({@code +I}/{@code +U}) overwrite the row, 
deletes ({@code -D}),
+ * including partial deletes that carry only the key columns, clear it. 
Update-befores ({@code -U})
+ * are ignored because the following {@code +U} overwrites the row.
+ */
+@Internal
+public class JoinKeyBuildStateView implements BuildStateView {
+
+    @VisibleForTesting static final String BUILD_ROW_STATE_NAME = "build-row";
+
+    /** The committed build row of the current key. */
+    private final ValueState<RowData> buildRowState;
+
+    public JoinKeyBuildStateView(RuntimeContext ctx, InternalTypeInfo<RowData> 
buildType) {
+        buildRowState = ctx.getState(new 
ValueStateDescriptor<>(BUILD_ROW_STATE_NAME, buildType));
+    }
+
+    @Override
+    public boolean apply(RowData change) throws Exception {
+        final RowKind changeType = change.getRowKind();
+        if (changeType == RowKind.UPDATE_BEFORE) {

Review Comment:
   My earlier suggestion to ignore `-U` here was wrong: when the build side 
stays retract, a `-U` can be the only signal that a row is gone. Could we treat 
`-U` like `-D` (remove by key) in both keyed views (same in 
`UpsertBuildStateView`)? On the upsert path no `-U` reaches the operator, so it 
costs nothing.
   
   Example: a retract source with PK `bk`, read through `WHERE bv > 10`:
   
   ```
   source:       +I(a, 20)  -U(a, 20)  +U(a, 5)
   after WHERE:  +I(a, 20)  -U(a, 20)              <- a no longer matches
   state today:  a -> 20                           <- -U ignored
   probe a:      joins with 20, expected no match
   ```



##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/stream/sql/join/LateralSnapshotJoinTest.java:
##########
@@ -278,7 +282,10 @@ void testBuildRowtimeIsNotForwarded() {
     }
 
     @Test
-    void testInnerJoinWithUpsertBuildSourceMaterializesRetractions() {
+    void testInnerJoinWithUpsertBuildSourceSkipsChangelogNormalize() throws 
Exception {

Review Comment:
   The new plan tests all use `I,UA,D` sources with full deletes. Could we add 
the two new paths: a retract source with a PK containing the join key 
(`I,UB,UA,D`, planned as DropUpdateBefore + keyed layout, plus the filter case 
from the `-U` comment), and an upsert source with key-only deletes 
(`'source.produces-delete-by-key' = 'true'`), which is the upsert-kafka shape 
and the one the DELETE_BY_KEY branch is for?



##########
flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/optimize/program/FlinkChangelogModeInferenceProgram.scala:
##########
@@ -782,12 +782,20 @@ class FlinkChangelogModeInferenceProgram extends 
FlinkOptimizeProgram[StreamOpti
 
         case lateralSnapshotJoin: StreamPhysicalLateralSnapshotJoin =>
           // Probe (left) is required to be append-only.
-          // Build (right) side requires BEFORE_AND_AFTER for updates.
           val left = 
lateralSnapshotJoin.getLeft.asInstanceOf[StreamPhysicalRel]
           val right = 
lateralSnapshotJoin.getRight.asInstanceOf[StreamPhysicalRel]
           val newLeftOption = this.visit(left, UpdateKindTrait.NONE)
           val rightInputModifyKindSet = getModifyKindSet(right)
-          val newRightOption = this.visit(right, 
beforeAfterOrNone(rightInputModifyKindSet))
+          // The upsert-keyed layout applies updates by key, so 
ONLY_UPDATE_AFTER suffices. The
+          // multi-set layout matches by full row and needs BEFORE_AND_AFTER.
+          val newRightOption =
+            if (lateralSnapshotJoin.getBuildUpsertKey.isPresent) {

Review Comment:
   Could we reuse the existing `visitChildAttemptUpsert` (generalized to 
`onlyAfterOrNone`/`beforeAfterOrNone`) here, and add a matching delete-by-key 
helper for the delete visitor? That's the same "ask for upsert, fall back to 
retract" pattern the PTF path, the join and the multi-join already use. The 
fallback is only safe once the keyed layouts handle `-U` by key (see the 
comment on `JoinKeyBuildStateView`).



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/join/snapshot/LateralSnapshotJoinOperator.java:
##########
@@ -80,15 +80,15 @@
  *
  * <ul>
  *   <li>Build-side (input2 / right) changes are handled the same way in both 
phases: they are
- *       buffered in {@code buildChangeBuffer} and applied lazily to a per-key 
multi-set in {@code
- *       buildTableState} on the next per-key access (build- or probe-side) 
once the build-side
- *       watermark has advanced past the buffer's tag, or at the flip. The 
buffer is keyed by the
- *       build-side row-time attribute ({@code buildRowtimeIndex}) and applied 
in row-time order
- *       once the build watermark reaches it; changes sharing a row-time are 
applied in arrival
- *       order, which mirrors the upstream emit order (a retraction directly 
precedes its
+ *       buffered in {@code buildChangeBuffer} and applied lazily to a per-key 
build table (see
+ *       {@link BuildStateView}) on the next per-key access (build- or 
probe-side) once the
+ *       build-side watermark has advanced past the buffer's tag, or at the 
flip. The buffer is
+ *       keyed by the build-side row-time attribute ({@code 
buildRowtimeIndex}) and applied in
+ *       row-time order once the build watermark reaches it; changes sharing a 
row-time are applied
+ *       in arrival order, which mirrors the upstream emit order (a retraction 
directly precedes its
  *       accumulation). Gating application on the row-time preserves atomic 
update visibility across
  *       {@code -U}/{@code +U} pairs in JOIN phase. Accumulations whose join 
key can never match are
- *       not stored.
+ *       not stored. Layouts keyed by an upsert key apply changes on arrival 
instead.

Review Comment:
   Could we update `joins.md` (en and zh) for the keyed layouts? `on_time` says 
it orders build changes, but keyed layouts apply them on arrival.



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