gustavodemorais commented on code in PR #30112:
URL: https://github.com/apache/flink/pull/30112#discussion_r4706197421
##########
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:
Yes, `DEDUPLICATE` keeps the first arrival and forwards its time, so a late
first copy stays late. Event-time deduplication is left to
`DEDUPLICATE_WINDOW`; I added a note to the docs.
##########
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:
Added `on_time` to Syntax and Parameters and fixed the output line.
##########
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:
Done, regenerated.
##########
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:
Done, an empty `sequence_by` is now rejected.
--
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]