Sachin-nsk commented on issue #13407:
URL: https://github.com/apache/seatunnel/issues/13407#issuecomment-7134765191

   Hi, I would like to work on this.
   
   I tested the current behaviour on dev with PostgreSQL: a replayed older row 
overwrites a newer stored row. Also, when the same key appears twice in one 
batch, the sink keeps only the last row that arrived, so the newer one can be 
dropped before any SQL runs. The predicate therefore needed to be applied per 
row, in order.
   
   My plan:
   1. PostgreSQL only,  using one statement: INSERT ... ON CONFLICT (pk) DO 
UPDATE ... WHERE (<update_condition_sql>). This inserts missing rows and skips 
the update when the condition is false or NULL.
   2. Behaviour is unchanged when the option is not set.
   3. Unit tests, a PostgreSQL container test, and docs.
   4. Other databases can be handled separately.
   
   SQL example: (PostgreSQL 15.1) . The table is test (id int primary key, name 
text, update_time biginit)
   Stored row is (1, 'current', 100)
   
   1. Older row: skipped, the stored row stays (1, 'current', 100)
   --INSERT 0 0
   INSERT INTO test AS target (id, name, update_time) VALUES (1, 'old', 50)
   ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name, update_time = 
EXCLUDED.update_time
   WHERE (50 > target.update_time);
   
   2. newer row: updated, the row becomes (1, 'new', 200)
   -- INSERT 0 1
   INSERT INTO test AS target (id, name, update_time) VALUES (1, 'new', 200)
   ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name, update_time = 
EXCLUDED.update_time
   WHERE (200 > target.update_time);
   
   3. missing key: inserted as (2, 'other', 10)
   -- INSERT 0 1
   INSERT INTO test AS target (id, name, update_time) VALUES (2, 'other', 10)
   ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name, update_time = 
EXCLUDED.update_time
   WHERE (10 > target.update_time);
   
   4. NULL update_time: skipped, the stored row is unchanged
   --INSERT 0 0
   INSERT INTO test AS target (id, name, update_time) VALUES (1, 'nullrow', 
NULL)
   ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name, update_time = 
EXCLUDED.update_time
   WHERE (NULL::bigint > target.update_time);
   
   
   One question: PostgreSQL rejects the example : update_time > update_time as 
ambiguous. Would a fixed alias like :update_time > target.update_time be 
acceptable ?


-- 
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]

Reply via email to