Sachin-nsk opened a new pull request, #13480:
URL: https://github.com/apache/seatunnel/pull/13480
Yes — this is a contribution to Apache SeaTunnel.
### Purpose of this pull request
Close #13407
The JDBC sink upsert always overwrites the stored row, so a replay or repair
job can replace newer data written by another job. This adds an opt-in
`update_condition_sql` for PostgreSQL: an existing row is updated only when the
condition is true.
```hocon
update_condition_sql = ":update_time > target.update_time"
```
```sql
INSERT INTO ... AS target (...) VALUES (...)
ON CONFLICT ("id") DO UPDATE SET ... WHERE (EXCLUDED."update_time" >
target."update_time")
```
**Behaviour**
- A missing key is inserted. A false or NULL condition keeps the stored row
(no error, no retry, no insert).
- `:column` is the incoming row, `target.column` the stored row. Only
columns written by the sink, `= <> < <= > >=`, `AND`, `OR`, `NOT`, `IS [NOT]
NULL` and parentheses are accepted. Anything else fails at job validation. The
condition is parsed into a tree and the SQL is built from it; the user text is
never passed to the database.
- With the option, every change is applied in arrival order instead of
keeping the last change per key. The pending batch is executed before a key
repeats, so it also works with `reWriteBatchedInserts=true`. A retry replays
the whole buffer in order.
- Requires a `jdbc:postgresql:` URL with `PostgresDialect`,
`generate_sink_sql = true`, primary keys and `enable_upsert = true`; not usable
with `query`, `use_copy_statement` or `support_upsert_by_insert_only`.
`postgresLow`, dialects extending the PostgreSQL one, and Greenplum/HighGo URLs
are rejected.
**Limits**
- `DELETE` rows are not guarded by the condition.
- A server reached through a `jdbc:postgresql:` URL is treated as PostgreSQL.
- Concurrent writers were tested at the default `READ COMMITTED` isolation
only.
### Does this PR introduce _any_ user-facing change?
Yes, a new optional sink option `update_condition_sql`. Without it the
behaviour is unchanged. Documented in `docs/en` and `docs/zh`
(`connectors/sink/Jdbc.md`).
### How was this patch tested?
- Unit tests: parser, ordered executor (new classes); generated SQL,
validation and sink factory (existing classes).
- `JdbcPostgresIT` (4 new cases): both input orders, missing keys,
true/false/NULL, DELETE, composite key, `reWriteBatchedInserts` on and off,
option absent, invalid condition, two concurrent writers.
- Local results (Windows): connector-jdbc 1314 tests, 0 failures (10
skipped) on JDK 11 and 17; unit tests of the 8 CDC connectors depending on it
pass on JDK 11 and 17; `JdbcPostgresIT` 15/15, `JdbcPostgresTimestampIT` and
`JdbcSinkCDCChangelogIT` pass on Zeta. Only comments and blank lines changed
after the e2e and JDK 17 runs.
- Manual repair job (JDBC source to JDBC sink, `TIMESTAMP` column): newer
rows kept, older rows updated, missing rows inserted, and a second run changed
nothing.
### Check list
* [ ] New Jar binary package: N/A, no new dependencies
* [x] Documentation updated
* [ ] `incompatible-changes.md`: N/A, no behaviour change without the option
* [ ] New connector files (plugin mapping, dist, label, plugin_config): N/A,
existing connector
--
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]