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

   Thanks. This is how I plan to do it.
   
   1. SQL
   The condition is added only to the `DO UPDATE` part of the current 
PostgreSQL statement:
   INSERT ... ON CONFLICT (...) DO UPDATE SET ... WHERE (<condition>)
   
   2. Condition syntax
   - `:column` means the incoming row. It becomes `EXCLUDED."column"`
   - `target.column` means the stored row.
   So `:update_time > target.update_time` becomes `EXCLUDED."update_time" > 
target."update_time"`.
   
   The sink will not copy the user's text into the SQL. It will read each part, 
check it, and build the SQL itself. The first version allows only:
   - `:column` and `target.column`
   - =, <>, >, <, <=, >=
   - AND, OR, NOT, IS NULL, IS NOT NULL, and brackets
   
   An unknown column or any other text stops the job at startup.
   
   3. Results
   - Missing key: the row is inserted
   - Condition is true: the row is updated
   - Condition is false or NULL: nothing happens. This is not an error, there 
is no retry, and nothing is inserted.
   
   4. Same key twice in one batch
   Today the sink keeps only the last row for each key. With this option, it 
will keep all rows and apply them in the order they arrive. 
   
   If `reWriteBatchedInserts=true`, keeping all rows is not enough. The driver 
merges them into one multi row INSERT, and PostgreSQL fails with `ON CONFLICT 
DO UPDATE command cannot affect row a second time`. So if a row's key is 
already in the pending batch, the sink first executes that batch, then adds the 
row to a new one. This way one statement never has the same key twice.
   
   Example: the stored `update_time` is 100
   - Input Order: older -> newer (200, 300) : updated to 200, then updated to 
300. Final value is 300
   - Input Order: newer -> older (300, 200): updated to 300, then skipped. 
Final value is 300
   
   I checked this with a small JDBC prototype on PostgreSQL 15.1. It worked 
with `reWriteBatchedInserts` both true and false.
   
   5. Scope
   - PostgreSQL only. Other databases can be separate work.
   - The option needs generate_sink_sql = true, primary_keys and enable_upsert 
= true. Otherwise the job stops at startup.
   - DELETE rows work as today. The condition does not protect against an old 
delete.
   - Without the option, nothing changes.
   
   6. Tests and docs
   PostgreSQL container tests for insert, update, false, NULL and duplicate 
keys in both orders. Docs in English and Chinese.
   
   Does this look okay before I start the PR ?
    


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