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


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/ptf/Deduplicate.java:
##########
@@ -46,18 +63,43 @@ public Deduplicate() {
     @Override
     public UserDefinedFunction specialize(final SpecializedContext context) {
         final CallContext callContext = context.getCallContext();
+        final TableSemantics tableSemantics =
+                CallContextUtils.getTableSemanticsOrThrow(callContext, 
ARG_INPUT);
+        final ChangelogMode inputMode =
+                
tableSemantics.changelogMode().orElseThrow(IllegalStateException::new);
+        final ChangelogMode outputMode =
+                
callContext.getOutputChangelogMode().orElseThrow(IllegalStateException::new);
+
+        final boolean isInsertOnly = inputMode.containsOnly(RowKind.INSERT);
         final boolean isKeepFirst = DeduplicateDefinition.keep(callContext) == 
Keep.FIRST;
+        final boolean hasSequenceBy =
+                
DeduplicateDefinition.sequenceByOutputIndices(callContext).length > 0;
+        final boolean hasOneRowPerKey = isPartitionByUpsertKey(tableSemantics);
+        final boolean needsUpdateBefore =
+                outputMode.contains(RowKind.UPDATE_BEFORE)
+                        && !inputMode.contains(RowKind.UPDATE_BEFORE);
+        final boolean needsFullDeletes = inputMode.keyOnlyDeletes() && 
!outputMode.keyOnlyDeletes();
 
-        if (isKeepFirst) {
-            final boolean hasSequenceBy =
-                    
DeduplicateDefinition.sequenceByOutputIndices(callContext).length > 0;
-            if (!hasSequenceBy) {
-                return new DeduplicateEmitOnceFunction(context);
-            } else {
-                return new DeduplicateCurrentRowFunction(context);
-            }
-        } else {
-            return new DeduplicateCurrentRowFunction(context);
+        if (isInsertOnly) {
+            return isKeepFirst && !hasSequenceBy
+                    ? new DeduplicateAppendEmitOnceFunction(context)
+                    : new DeduplicateAppendReplaceFunction(context);
         }
+        if (hasOneRowPerKey) {
+            return needsUpdateBefore || needsFullDeletes
+                    ? new DeduplicateUpsertNormalizeFunction(context)
+                    : new DeduplicateUpsertForwardFunction(context);
+        }
+        return new DeduplicateRetractFunction(context);
+    }
+
+    /** Returns whether the PARTITION BY columns contain an upsert key of the 
input. */
+    private static boolean isPartitionByUpsertKey(final TableSemantics 
tableSemantics) {

Review Comment:
   When the input's upsert key contains the `PARTITION BY` columns, 
`upsertKeyColumns()` already has it, but `DeduplicateRetractFunction` still 
uses the whole row as the state key. Could we use the key instead?
   
   Example: `page_counts` = `SELECT user_id, page, COUNT(*) AS cnt FROM clicks 
GROUP BY user_id, page`, with key `(user_id, page)`, then `DEDUPLICATE(input => 
TABLE page_counts PARTITION BY user_id)`.
   
   `liveRows` of `u1`:
   
   | | map key | map value |
   |---|---|---|
   | Today | `(u1, A, 3)`, the whole row | count `1`, arrival position |
   | With the key | `(u1, A)` | `(u1, A, 3)`, arrival position |
   
   - The map key gets smaller.
   - No count needed, since a key has at most one live row.
   
   Compiled plans already store the key, so it's easiest to do before the 
release.



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