[ 
https://issues.apache.org/jira/browse/FLINK-41811?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Gustavo de Morais updated FLINK-41811:
--------------------------------------
    Description: 
A set-semantic PTF only sees the upsert keys that survive the exchange on its 
PARTITION BY columns ({{TableSemantics#upsertKeyColumns}}). A unique key that 
does not contain the PARTITION BY columns is dropped, although it still 
identifies a row within each partition. A PTF that keeps several rows per 
partition and receives retractions then has to use the full input row to find 
the row a retraction removes. That breaks as soon as a column differs between a 
row and its retraction, e.g. {{PROCTIME()}} (materialized below the PTF), 
{{NOW()}}, or metadata like a Kafka offset. The table argument also keeps 
columns the query never reads. Example with {{DEDUPLICATE}}:

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

We should expose the unique keys of the input (computed below the exchange) in 
{{TableSemantics}} and store them in the compiled plan, so a PTF can identify a 
row by key. Plans compiled before the change carry no keys and keep today's 
behavior.

  was:
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.

        Summary: Expose the unique keys of the input to process table functions 
 (was: PTFs on retract input cannot reliably match a retraction to the row it 
removes)

> Expose the unique keys of the input to process table functions
> --------------------------------------------------------------
>
>                 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
>            Assignee: Gustavo de Morais
>            Priority: Major
>
> A set-semantic PTF only sees the upsert keys that survive the exchange on its 
> PARTITION BY columns ({{TableSemantics#upsertKeyColumns}}). A unique key that 
> does not contain the PARTITION BY columns is dropped, although it still 
> identifies a row within each partition. A PTF that keeps several rows per 
> partition and receives retractions then has to use the full input row to find 
> the row a retraction removes. That breaks as soon as a column differs between 
> a row and its retraction, e.g. {{PROCTIME()}} (materialized below the PTF), 
> {{NOW()}}, or metadata like a Kafka offset. The table argument also keeps 
> columns the query never reads. Example with {{DEDUPLICATE}}:
> {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.
> We should expose the unique keys of the input (computed below the exchange) 
> in {{TableSemantics}} and store them in the compiled plan, so a PTF can 
> identify a row by key. Plans compiled before the change carry no keys and 
> keep today's behavior.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to