SEPURI-SAI-KRISHNA opened a new pull request, #13393:
URL: https://github.com/apache/seatunnel/pull/13393

   Yes, glad to contribute to Apache SeaTunnel.
   
   ### Purpose of this pull request
   
   Closes #13384, with the approach agreed there (option 1: the fix is in the 
SQL transform, no sink changes).
   
   The `WHERE` clause of the `Sql` and `Calcite` transforms judges each row on 
its own and keeps its row kind. When an update moves a row out of the 
condition, its `UPDATE_BEFORE` (old values, matching) passes and its 
`UPDATE_AFTER` (new values, not matching) is dropped. Sinks that write 
`UPDATE_AFTER` as an upsert and skip `UPDATE_BEFORE` (Jdbc with `primary_keys`, 
Elasticsearch, ClickHouse, Kudu, MongoDB) keep the old row, and the job 
finishes without an error.
   
   Rule: when the `WHERE` clause rejects an `UPDATE_AFTER`, the transform emits 
the projected new image as a `DELETE`. Everything else is unchanged:
   
   | Row kind | Matches `WHERE` | Output |
   |---|---|---|
   | any | yes | projected row, same row kind (as before) |
   | `UPDATE_AFTER` | no | projected row as `DELETE` (new) |
   | `INSERT`, `UPDATE_BEFORE`, `DELETE` | no | nothing (as before) |
   
   - `ZetaSQLEngine.transformBySQL`: the rejected row goes through the normal 
`project` and lateral views (which copy the row kind), with `DELETE`.
   - `CalciteSQLEngine`: the `WHERE` clause is compiled into the plan, so 
`init()` also compiles the query without its `WHERE` clause, for a single 
`SELECT` (optionally under `ORDER BY`) without `GROUP BY` or `HAVING`. When the 
main plan returns no row for an `UPDATE_AFTER`, that plan builds the image. 
Other query shapes keep the old behavior.
   - The delete row is the same projection as a normal output row, so it 
carries the key columns the query selects. The docs say to select the sink's 
key.
   - A `WHERE` clause can guard the projection (`select 10 / x ... where x <> 
0`). If the projection of a rejected `UPDATE_AFTER` fails, the row is dropped 
as before and a WARN is logged once per transform (then DEBUG), so such jobs 
keep running.
   
   ### Trade-offs of option 1
   
   Documented in `sql.md`, `calcite.md` and `incompatible-changes.md` (EN, ZH), 
as asked in #13384:
   
   1. A `DELETE` can be sent for a key the target never had: it is also sent 
when the old values did not match either. Jdbc, MongoDB and the other keyed 
sinks treat it as a no-op, but the Kudu sink fails the write on a missing key 
unless `ignore_not_found = true` (its default is `false`, 
`KuduOutputFormat.checkErrors`). The alternative, sending the `DELETE` only 
when the preceding `UPDATE_BEFORE` was kept, avoids it but no longer covers an 
incomplete `UPDATE_BEFORE`.
   2. The sink user needs the `DELETE` privilege on the target table, also for 
a job that never deleted rows before.
   3. `GROUP BY` / `HAVING` queries on Calcite keep dropping a rejected 
`UPDATE_AFTER`, because removing the `WHERE` clause changes what they compute.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. A job that filters a CDC stream with a `WHERE` clause now sends 
`DELETE` rows downstream for rows that leave the condition, and a keyed sink 
removes them instead of keeping the old values. Described in 
`docs/en|zh/transforms/sql.md` (new section "WHERE on a CDC stream"), 
`docs/en|zh/transforms/calcite.md`, and `incompatible-changes.md` (EN, ZH). No 
option, default or API changes.
   
   ### How was this patch tested?
   
   - `SQLTransformTest` (Zeta) and `CalciteSQLEngineTest`, 4 new tests each: 
every row kind through a matching and a non-matching `WHERE`, with exact row 
kinds, values, table id and options; a query without `WHERE` keeps every row 
kind; a `LATERAL VIEW` turns a rejected `UPDATE_AFTER` into one `DELETE` per 
exploded row (Zeta); `ORDER BY` (Calcite); a failing projection drops the row. 
With the dev engines the 4 behavior tests fail and the 2 guard tests pass.
   - Whole `seatunnel-transforms-v2` module on JDK 17: 1965 tests, 0 failures.
   - `JdbcSinkCDCChangelogIT` (existing class, PostgreSQL), new 
`testSqlWhereOnCDCChangelog` and `testCalciteWhereOnCDCChangelog`: FakeSource 
-> `Sql` (or `Calcite`) with `WHERE status = 'active'` -> Jdbc with 
`primary_keys`. Row 1 goes `active -> inactive`, row 3 `inactive -> active`, 
row 2 stays `active`. The final table must be exactly `[(2, active), (3, 
active)]`. Run locally on Zeta:
     - dev engines: both fail, the table is `[(1, active), (2, active), (3, 
active)]`;
     - this PR: the whole `JdbcSinkCDCChangelogIT` passes (3 tests).
   - The two existing CDC recipes with a SQL `WHERE` (`KafkaRecipeIT`, 
`ElasticsearchRecipeIT`) filter on the key and update only matching rows, so 
they are not affected.
   - `spotless:apply` (no changes), `git diff --check`.
   
   ### Check list
   
   * [x] If any new Jar binary package adding in your PR, please add License 
Notice according
     [New License 
Guide](https://github.com/apache/seatunnel/blob/dev/docs/en/developer/new-license.md)
 (no new jar)
   * [x] If necessary, please update the documentation to describe the new 
feature. https://github.com/apache/seatunnel/tree/dev/docs (sql.md, calcite.md, 
EN and ZH)
   * [x] If necessary, please update `incompatible-changes.md` to describe the 
incompatibility caused by this PR. (EN and ZH)
   * [x] If you are contributing the connector code, please check that the 
following files are updated:
     1. Update 
[plugin-mapping.properties](https://github.com/apache/seatunnel/blob/dev/plugin-mapping.properties)
 and add new connector information in it (not a new connector)
     2. Update the pom file of 
[seatunnel-dist](https://github.com/apache/seatunnel/blob/dev/seatunnel-dist/pom.xml)
 (not a new connector)
     3. Add ci label in 
[label-scope-conf](https://github.com/apache/seatunnel/blob/dev/.github/workflows/labeler/label-scope-conf.yml)
 (not a new connector)
     4. Add e2e testcase in 
[seatunnel-e2e](https://github.com/apache/seatunnel/tree/dev/seatunnel-e2e/seatunnel-connector-v2-e2e/)
 (JdbcSinkCDCChangelogIT)
     5. Update connector 
[plugin_config](https://github.com/apache/seatunnel/blob/dev/config/plugin_config)
 (not a new 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]

Reply via email to