Martijn Visser created FLINK-41671:
--------------------------------------

             Summary: ON CONFLICT DO ERROR reports false conflicts and DO 
NOTHING keeps stale rows when an update moves between join subtasks
                 Key: FLINK-41671
                 URL: https://issues.apache.org/jira/browse/FLINK-41671
             Project: Flink
          Issue Type: Bug
          Components: Table SQL / Planner
    Affects Versions: 2.3.0
            Reporter: Martijn Visser


{{WatermarkTimestampAssigner}} stamps each record with its own subtask's 
combined watermark when it arrives. When one input change retracts a row on one 
join subtask and inserts its replacement on another, the two can get different 
stamps, and {{WatermarkCompactingSinkMaterializer}} compacts the old and new 
row without the retraction. DO ERROR then fails on a key that never has two 
rows: the left side has one row per from_account and the join adds at most one 
match. DO NOTHING keeps the stale row and drops the new one, so the result 
stays wrong after end of input. The script below fails within two seconds at 
parallelism 4. It finishes at parallelism 1, and without ON CONFLICT.

{code:sql}
SET 'parallelism.default' = '4';
SET 'table.dml-sync' = 'true';

CREATE TABLE transactions (
  id BIGINT,
  from_account AS CAST(MOD(id, 10) AS INT),
  to_account AS CAST(MOD(id * 7 + id / 10, 10) AS INT),
  amount AS CAST(id + 1 AS DOUBLE),
  ts AS TIMESTAMPADD(MILLISECOND, CAST(id * 100 AS INT), TIMESTAMP '2021-01-01 
00:00:00.000'),
  WATERMARK FOR ts AS ts - INTERVAL '5' SECOND
) WITH (
  'connector' = 'datagen',
  'fields.id.kind' = 'sequence',
  'fields.id.start' = '0',
  'fields.id.end' = '19999',
  'rows-per-second' = '5000'
);

CREATE TABLE sink (
  from_account INT,
  id BIGINT,
  to_account INT,
  credits DOUBLE,
  PRIMARY KEY (from_account) NOT ENFORCED
) WITH ('connector' = 'blackhole');

INSERT INTO sink
SELECT l.from_account, l.id, l.to_account, c.credits
FROM (
  SELECT id, from_account, to_account
  FROM (
    SELECT id, from_account, to_account,
      ROW_NUMBER() OVER (PARTITION BY from_account ORDER BY ts DESC, id DESC) 
AS rn
    FROM transactions)
  WHERE rn = 1) AS l
JOIN (
  SELECT to_account AS account, SUM(amount) AS credits
  FROM transactions
  GROUP BY to_account) AS c
ON l.to_account = c.account
ON CONFLICT DO ERROR;
{code}

Fails with: Primary key constraint violation: multiple distinct records with 
the same primary key detected. Conflicting key: [from_account=4].



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

Reply via email to