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


##########
flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/optimize/program/FlinkChangelogModeInferenceProgram.scala:
##########
@@ -1674,8 +1679,108 @@ class FlinkChangelogModeInferenceProgram extends 
FlinkOptimizeProgram[StreamOpti
     }
 
     /**
-     * Removes the ChangelogNormalize and the exchange that only exists for 
it. A filter condition
-     * pushed into the ChangelogNormalize is kept as a Calc, so it still 
filters the rows.
+     * Provides the input of a function that ignores retractions (see
+     * [[StaticArgumentTrait.IGNORE_RETRACTIONS]]).
+     *
+     * Such a function never needs UPDATE_BEFORE or DELETE rows, so a
+     * [[StreamPhysicalChangelogFilter]] that keeps only INSERT and 
UPDATE_AFTER rows drops them in
+     * front of it. This drop is placed as low as possible, so that the 
operators between it and the
+     * function also see only INSERT and UPDATE_AFTER rows:
+     * {{{
+     * Source -> ChangelogFilter -> Union -> Calc -> PTF
+     * Source -> ChangelogNormalize -> Join -> ChangelogFilter -> Calc -> PTF
+     * }}}
+     *
+     * The drop moves below operators that handle each row on its own (see 
[[canPushDropBelow]]) and
+     * stops at the first other operator:
+     *   - A source gets the drop on top of it.
+     *   - A ChangelogNormalize is replaced by the drop, unless it deduplicates
+     *     (table.exec.source.cdc-events-duplicate). A filter pushed into it 
stays as a Calc above
+     *     the drop.
+     *   - A DropUpdateBefore is replaced by the drop.
+     *   - A stateful operator, like a join or an aggregate, keeps its own 
requirements on its
+     *     inputs, and the drop goes on top of it.
+     *
+     * This saves the state of a ChangelogNormalize in front of the function, 
and the input needs no
+     * upsert key. Filters on non-key columns, metadata columns and key-only 
deletes don't need a
+     * ChangelogNormalize either.
+     */
+    private def dropRetractions(rel: StreamPhysicalRel): 
Option[StreamPhysicalRel] = rel match {
+      case stateless if canPushDropBelow(stateless) =>
+        val newChildren =
+          stateless.getInputs.map(child => 
dropRetractions(child.asInstanceOf[StreamPhysicalRel]))
+        val allChildrenConverted = newChildren.forall(_.isDefined)
+        if (allChildrenConverted) {
+          Some(copyWithoutRetractions(stateless, newChildren.flatten.toList))
+        } else {
+          None
+        }
+
+      case normalize: StreamPhysicalChangelogNormalize if 
!deduplicatesChanges(normalize) =>

Review Comment:
   Makes sense. Then I think a semantic test with a repeated upsert below a 
non-key filter should pin it; the new `TO_APPEND` programs also pass on master. 
The `IGNORE_RETRACTIONS` docs could mention it too.



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