gustavodemorais commented on code in PR #30112:
URL: https://github.com/apache/flink/pull/30112#discussion_r4709547334
##########
docs/content.zh/docs/sql/reference/queries/deduplication.md:
##########
@@ -169,6 +173,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'`.
Review Comment:
Applied, thanks.
##########
docs/content.zh/docs/sql/reference/queries/deduplication.md:
##########
@@ -169,6 +173,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 time attributes
+
+PTFs take an optional [`on_time`]({{< ref "docs/dev/table/functions/ptfs"
>}}#time) argument to forward a time attribute: the output gets a `rowtime`
column with the time of each row, which event-time operations such as window
aggregations can use. For `DEDUPLICATE`, this works when the result only
contains inserts, that is with `keep => 'first'` and no `sequence_by`:
+
+| `clicks` (`event_id`, `ts`) |
+|:----------------------------|
+| `(e1, 10:01)` |
+| `(e2, 10:02)` |
+| `(e1, 10:03)` |
+| `(e3, 10:12)` |
+
+```sql
+CREATE VIEW unique_clicks AS
+SELECT * FROM DEDUPLICATE(
+ input => TABLE clicks PARTITION BY event_id,
+ on_time => DESCRIPTOR(ts));
+
+SELECT window_start, COUNT(*) AS events
+FROM TABLE(TUMBLE(TABLE unique_clicks, DESCRIPTOR(rowtime), INTERVAL '10'
MINUTES))
Review Comment:
Done.
##########
docs/content.zh/docs/sql/reference/queries/deduplication.md:
##########
@@ -169,6 +173,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 time attributes
+
+PTFs take an optional [`on_time`]({{< ref "docs/dev/table/functions/ptfs"
>}}#time) argument to forward a time attribute: the output gets a `rowtime`
column with the time of each row, which event-time operations such as window
aggregations can use. For `DEDUPLICATE`, this works when the result only
contains inserts, that is with `keep => 'first'` and no `sequence_by`:
+
+| `clicks` (`event_id`, `ts`) |
+|:----------------------------|
+| `(e1, 10:01)` |
+| `(e2, 10:02)` |
+| `(e1, 10:03)` |
+| `(e3, 10:12)` |
+
+```sql
+CREATE VIEW unique_clicks AS
Review Comment:
Done, switched to a CTE.
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/definitions/DeduplicateDefinition.java:
##########
@@ -97,8 +98,7 @@ public enum Keep {
/** Returns the {@code keep} argument, or {@link Keep#FIRST} when it is
omitted. */
public static Keep keep(final CallContext callContext) {
- return CallContextUtils.getEnumArgument(callContext, ARG_KEEP,
Keep.class)
- .orElse(Keep.FIRST);
+ return keep(callContext.getArgumentValue(ARG_KEEP, String.class));
Review Comment:
Done. The changelog path still uses `toEnum`, because `getEnumArgument`
takes a `CallContext`.
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/definitions/DeduplicateDefinition.java:
##########
@@ -177,6 +200,12 @@ private static List<Field> outputFields(final
TableSemantics tableSemantics) {
private static Optional<String> validateSequenceBy(
final CallContext callContext, final TableSemantics
tableSemantics) {
+ if (callContext
+ .getArgumentValue(ARG_SEQUENCE_BY, ColumnList.class)
+ .filter(columns -> columns.getNames().isEmpty())
Review Comment:
Done, added `CallContextUtils.validateNonEmptyDescriptor`.
--
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]