twalthr commented on code in PR #30112:
URL: https://github.com/apache/flink/pull/30112#discussion_r4707757024
##########
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:
```suggestion
In external systems, 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'`.
```
##########
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:
why not using getEnumArgument here?
##########
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:
```suggestion
FROM TUMBLE(TABLE unique_clicks, DESCRIPTOR(rowtime), INTERVAL '10' MINUTES)
```
##########
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.
Review Comment:
beautiful explanation! short, precise and helpful
##########
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:
I could imagine this being a useful CallContextUtils method. Many functions
would like to check for empty descriptors.
##########
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:
use a CTE instead of a view?
##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/nodes/exec/stream/DeduplicateTestPrograms.java:
##########
@@ -122,6 +122,100 @@ public class DeduplicateTestPrograms {
+ "keep => 'last')")
.build();
+ public static final TableTestProgram INSERT_ONLY_OUTPUT =
+ TableTestProgram.of(
+ "deduplicate-insert-only-output",
+ "over insert-only input, keeping the first arrival
emits only inserts, "
+ + "so the result can be written to an
append-only sink, also "
+ + "when dropping redelivered copies")
+ .setupTableSource(
+ SourceTestStep.newBuilder("user_events")
+ .addSchema(USER_EVENTS_SCHEMA)
+ .producedValues(
+ Row.of("u1", "login"),
+ Row.of("u2", "login"),
+ Row.of("u1", "click"))
+ .build())
+ .setupTableSource(
+ SourceTestStep.newBuilder("payments")
+ .addSchema(
+ "payment_id STRING",
+ "amount INT",
+ "ingested_at TIMESTAMP(3)")
+ .producedValues(
+ Row.of(
+ "p1",
+ 50,
+ LocalDateTime.of(1970, 1,
1, 10, 0, 1)),
+ Row.of(
+ "p2",
+ 20,
+ LocalDateTime.of(1970, 1,
1, 10, 0, 2)),
+ Row.of(
+ "p1",
+ 50,
+ LocalDateTime.of(1970, 1,
1, 10, 0, 7)))
+ .build())
+ .setupTableSink(
+ appendOnlySink(
+ "first_arrivals",
+ USER_EVENTS_SCHEMA,
+ "+I[u1, login]",
+ "+I[u2, login]"))
+ .setupTableSink(
+ appendOnlySink(
+ "unique_payments",
+ new String[] {
+ "payment_id STRING",
+ "amount INT",
+ "ingested_at TIMESTAMP(3)"
+ },
+ "+I[p1, 50, 1970-01-01T10:00:01]",
+ "+I[p2, 20, 1970-01-01T10:00:02]"))
+ .runSql(
+ "INSERT INTO first_arrivals SELECT * FROM
DEDUPLICATE("
+ + "input => TABLE user_events PARTITION BY
user_id)")
+ .runSql(
+ "INSERT INTO unique_payments SELECT * FROM
DEDUPLICATE("
+ + "input => TABLE payments PARTITION BY
payment_id, "
+ + "state_ttl => INTERVAL '1' HOUR)")
+ .build();
+
+ public static final TableTestProgram ON_TIME =
+ TableTestProgram.of(
+ "deduplicate-on-time",
+ "with on_time, the first arrival keeps its
rowtime, so an event-time "
+ + "window aggregation can consume the
deduplicated stream")
+ .setupTableSource(
+ SourceTestStep.newBuilder("clicks")
+ .addSchema(
+ "event_id STRING",
+ "ts TIMESTAMP_LTZ(3)",
+ "WATERMARK FOR ts AS ts")
+ .producedValues(
+ Row.of("e1", at("10:01")),
+ Row.of("e2", at("10:02")),
+ Row.of("e1", at("10:03")),
+ Row.of("e3", at("10:12")))
+ .build())
+ .setupTableSink(
+ appendOnlySink(
+ "unique_clicks_per_window",
+ new String[] {"window_start TIMESTAMP(3)",
"events BIGINT"},
+ "+I[1970-01-01T10:00, 2]",
+ "+I[1970-01-01T10:10, 1]"))
+ .setupSql(
+ "CREATE VIEW unique_clicks AS SELECT * FROM
DEDUPLICATE("
+ + "input => TABLE clicks PARTITION BY
event_id, "
+ + "on_time => DESCRIPTOR(ts))")
+ .runSql(
+ "INSERT INTO unique_clicks_per_window "
+ + "SELECT window_start, COUNT(*) FROM
TABLE(TUMBLE("
Review Comment:
```suggestion
+ "SELECT window_start, COUNT(*) FROM
TUMBLE("
```
##########
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:
It currently reads like this can happen in Flink.
--
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]