Gustavo de Morais created FLINK-41811:
-----------------------------------------
Summary: PTFs on retract input cannot reliably match a retraction
to the row it removes
Key: FLINK-41811
URL: https://issues.apache.org/jira/browse/FLINK-41811
Project: Flink
Issue Type: Improvement
Components: Table SQL / API, Table SQL / Planner
Reporter: Gustavo de Morais
A set-semantic PTF with {{SUPPORT_UPDATES}} receives -U/-D rows when the
PARTITION BY columns are not an upsert key of the input. A PTF that keeps
several rows per partition has to find the row a retraction removes, and today
it can only use the full input row for that:
* Unique keys of the input are not exposed. {{TableSemantics#upsertKeyColumns}}
only contains keys that survive the exchange on the PARTITION BY columns, so a
key that does not contain them is dropped.
* The table argument keeps every column, also the ones the query never reads,
and {{PROCTIME()}} is materialized below the PTF.
* The non-deterministic update check only requires the PARTITION BY columns to
be deterministic, unless the argument declares {{REQUIRE_UPDATE_BEFORE}}, which
forces -U on every input.
So a column that differs between a row and its retraction ({{PROCTIME()}},
{{NOW()}}, metadata like a Kafka offset) makes the retraction miss, and the
removed row stays. Example with {{DEDUPLICATE}}, which matches by the full row:
{code:sql}
CREATE TABLE accounts (
customer_id STRING, account_id STRING, balance INT, pt AS PROCTIME(),
PRIMARY KEY (account_id) NOT ENFORCED
) WITH ('connector' = 'values', 'changelog-mode' = 'I,UB,UA,D');
SELECT customer_id, account_id, balance
FROM DEDUPLICATE(input => TABLE accounts PARTITION BY customer_id, keep =>
'last');
{code}
Input +I(c1, a1, 10), -U(c1, a1, 10), +U(c1, a1, 15), -D(c1, a1, 15) gives
+I[c1, a1, 10], -U[c1, a1, 10], +U[c1, a1, 15]: c1 keeps a1 after it was
deleted. {{ROW_NUMBER() = 1}} on the same table is correct, since the planner
prunes unread columns below the Rank and keeps proctime lazy.
Possible changes, both compatible with existing compiled plans:
* Expose the unique keys of the input, including keys that do not contain the
PARTITION BY columns, so a PTF can identify a row by key.
* Let a PTF declare that it reads retractions when they are delivered, without
forcing -U, so that the non-deterministic update check requires all input
columns to be deterministic.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)