SEPURI-SAI-KRISHNA opened a new issue, #13384: URL: https://github.com/apache/seatunnel/issues/13384
### Search before asking - [X] I had searched in the [issues](https://github.com/apache/seatunnel/issues?q=is%3Aissue+label%3A%22Bug%22) and found no similar issues. No open PR changes how the SQL transform or a sink handles `UPDATE_BEFORE` (I checked the open PRs that touch `ZetaSQLEngine`, `ZetaSQLFilter`, `CalciteSQLEngine` and `BufferReducedBatchStatementExecutor`). ### What happened A `Sql` transform with a `WHERE` clause on a CDC stream leaves a row in the target table after the row stops matching the condition. The job finishes without an error. The `WHERE` clause judges each row on its own and keeps its row kind (`ZetaSQLEngine.transformBySQL`; `CalciteSQLEngine.execute` does the same). When an update moves a row out of the condition, its `UPDATE_BEFORE` (old values, matches) passes and its `UPDATE_AFTER` (new values, does not match) is dropped. Several sinks write `INSERT` and `UPDATE_AFTER` as an upsert and skip `UPDATE_BEFORE`, so nothing removes the row: Jdbc (`BufferReducedBatchStatementExecutor`), Elasticsearch, ClickHouse (when it reduces rows by key), Kudu, MongoDB. Sinks that write `UPDATE_BEFORE` as a delete (StarRocks, Doris, Redis, Hudi) are not affected. A row that moves into the condition is handled correctly, since its `UPDATE_AFTER` is written as an upsert. Reproduced on Zeta (local mode, dev 7029c0969a) with the config below and a SQLite table `t (id INTEGER PRIMARY KEY, status TEXT)`. Row 1 goes from `active` to `inactive`, row 3 from `inactive` to `active`: | `query` | Rows in `t` after the job | |---|---| | `select id, status from dual` | `(1, inactive)`, `(2, active)`, `(3, active)` | | `select id, status from dual where status = 'active'` | `(1, active)`, `(2, active)`, `(3, active)` | With the `WHERE`, row 1 should be gone; instead it keeps its old value. Two ways to fix it, and I would like to agree on one before writing the PR: 1. In the SQL transform: when the `WHERE` clause rejects an `UPDATE_AFTER`, emit the row as a `DELETE` instead of dropping it. This works for every sink and also when the source sends an incomplete `UPDATE_BEFORE`. It can send a delete for a key the target never had, which costs a statement and needs the DELETE privilege on the target (#13035 keeps write only Jdbc users working). 2. In each sink: write an `UPDATE_BEFORE` that no `UPDATE_AFTER` of the same key follows as a delete. This keeps write only users unaffected when nothing is filtered, but has to be done sink by sink, and an `UPDATE_BEFORE` at the end of one batch would need to wait for the next. I would go with option 1. Which do you prefer? I'm claiming this one: I will write the fix, with unit tests and an E2E for both SQL engines, once we agree on the approach. ### SeaTunnel Version dev (7029c0969a) ### SeaTunnel Config ```conf env { parallelism = 1 job.mode = "BATCH" } source { FakeSource { plugin_output = "cdc" schema { fields { id = int, status = string } primaryKey { name = "pk", columnNames = [id] } } rows = [ { kind = INSERT, fields = [1, "active"] }, { kind = INSERT, fields = [2, "active"] }, { kind = INSERT, fields = [3, "inactive"] }, { kind = UPDATE_BEFORE, fields = [1, "active"] }, { kind = UPDATE_AFTER, fields = [1, "inactive"] }, { kind = UPDATE_BEFORE, fields = [3, "inactive"] }, { kind = UPDATE_AFTER, fields = [3, "active"] } ] } } transform { Sql { plugin_input = "cdc" plugin_output = "out" query = "select id, status from dual where status = 'active'" } } sink { Jdbc { plugin_input = "out" url = "jdbc:sqlite:/tmp/t.db" driver = "org.sqlite.JDBC" database = "main" table = "t" generate_sink_sql = true primary_keys = ["id"] schema_save_mode = "IGNORE" data_save_mode = "APPEND_DATA" } } ``` ### Running Command ```shell ./bin/seatunnel.sh --config cdc_where.conf -m local ``` ### Error Exception ```log No exception. The job ends with exit code 0. ``` ### Zeta or Flink or Spark Version Zeta (dev). The SQL transform code is shared by every engine. ### Java or Scala Version JDK 17 ### Screenshots _No response_ ### Are you willing to submit PR? - [X] Yes I am willing to submit a PR! ### Code of Conduct - [X] I agree to follow this project's [Code of Conduct](https://www.apache.org/foundation/policies/conduct) -- 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]
