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


##########
docs/content/docs/sql/reference/queries/deduplication.md:
##########
@@ -102,7 +102,9 @@ SELECT * FROM DEDUPLICATE(
 
 The output columns are the `PARTITION BY` columns, in the order listed, 
followed by the remaining input columns. Time attributes are emitted as regular 
timestamps.

Review Comment:
   If `on_time` stays, I think Syntax and Parameters need it too, and this line 
contradicts the `rowtime` column it appends.



##########
docs/content/docs/sql/reference/queries/deduplication.md:
##########
@@ -169,6 +171,67 @@ SELECT * FROM DEDUPLICATE(
 
 `(c1, Hamburg, 110)` is dropped because its `lsn` is smaller than that of the 
kept row.
 
+#### Deduplicate exact matches
+
+At-least-once delivery, retries, and reprocessing after a failure can write 
the same row more than once. To drop these copies, partition by the columns 
that identify a row and keep the default `keep => 'first'`.
+
+Here, a payment is delivered twice. Both copies share the `payment_id`, but 
each has its own `ingested_at`:
+
+| `payments` (`payment_id`, `amount`, `ingested_at`) |
+|:---------------------------------------------------|
+| `(p1, 50, 10:00:01)`                               |
+| `(p2, 20, 10:00:02)`                               |
+| `(p1, 50, 10:00:07)`                               |
+
+```sql
+INSERT INTO unique_payments
+SELECT * FROM DEDUPLICATE(
+  input => TABLE payments PARTITION BY payment_id,
+  state_ttl => INTERVAL '1' HOUR)
+```
+
+| Result                 |
+|:-----------------------|
+| `(p1, 50, 10:00:01)`   |
+| `(p2, 20, 10:00:02)`   |
+
+The second `p1` is dropped. Partition by the columns that are equal in every 
copy, here `payment_id`, and leave out columns that differ between copies, such 
as `ingested_at`.
+
+This query:
+
+- **Only emits inserts.** The first row of a key is emitted as soon as it 
arrives and never updated, so `unique_payments` can be an append-only table, 
and operations that require an insert-only input can consume the result.
+- **Does not depend on watermarks.** No row waits for a watermark or is 
dropped as late.
+- **Keeps one entry per key in state.** Set `state_ttl` to how long copies can 
keep arriving, for example the time a source needs to recover from a failure. A 
copy that arrives after its key expired is emitted again.
+
+#### Keep event time
+
+Event-time operations such as window aggregations need a time attribute. As 
with other PTFs, pass `on_time` to keep one: the output gets a `rowtime` column 
with the time of each row. This works when the result only contains inserts, 
for example with `keep => 'first'` and no `sequence_by`:

Review Comment:
   FLIP-622 leaves event time to DEDUPLICATE_WINDOW. Locally, a first e1 that 
arrived late kept its rowtime and the TUMBLE dropped it, while its on-time copy 
was already deduplicated, so e1 was in no window. Is that intended?



##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/definitions/DeduplicateDefinition.java:
##########
@@ -143,6 +142,9 @@ private static Optional<DataType> inferOutputType(final 
CallContext callContext)
     }
 
     private static ChangelogMode inferChangelogMode(final ChangelogContext 
changelogContext) {
+        if (emitsOnlyInserts(changelogContext)) {

Review Comment:
   The deduplicate-first-arrival-restore plan still has outputChangelogMode 
INSERT, UPDATE_AFTER here. FLINK-41470 isn't released yet, so I think 
regenerating it makes sense, then the restore test covers what 2.5.0 compiles.



##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/definitions/DeduplicateDefinition.java:
##########
@@ -167,6 +169,26 @@ private static Optional<String> validate(final CallContext 
callContext) {
                 .or(() -> CallContextUtils.validateStateTtl(callContext, 
ARG_STATE_TTL));
     }
 
+    /**
+     * Over insert-only input, the kept row of a key never changes when the 
first arrival is kept.
+     * The function then only emits insertions, so its changelog mode can be 
insert-only.
+     */
+    private static boolean emitsOnlyInserts(final ChangelogContext context) {
+        if 
(!context.getTableChangelogMode(ARG_INPUT).containsOnly(RowKind.INSERT)) {
+            return false;
+        }
+        final boolean keepsFirst =
+                keep(context.getArgumentValue(ARG_KEEP, String.class)) == 
Keep.FIRST;
+        final boolean hasSequenceBy =
+                context.getArgumentValue(ARG_SEQUENCE_BY, 
ColumnList.class).isPresent();

Review Comment:
   `sequence_by => DESCRIPTOR()` passes validation and runs 
`DeduplicateEmitOnceFunction`, but is declared updating here. I think rejecting 
an empty descriptor in `validateSequenceBy` is the simpler fix.



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