dongjoon-hyun opened a new pull request, #60647: URL: https://github.com/apache/spark/pull/60647
### What changes were proposed in this pull request? This PR fixes `CHECK_CONSTRAINT_VIOLATION` failures on rows that a DSv2 write does not write. A CHECK constraint is enforced by a `Filter(CheckInvariant(...))` that the analyzer adds on top of the query of a write. The check of a generated column value that a write provides uses `CheckInvariant` too. `CheckInvariant` throws when its predicate is false, but the optimizer moved the filter below operators that remove rows, so the check also saw rows that are never written: - `PushPredicateThroughJoin` pushed it below inner, semi and anti joins, including the anti join of a `MERGE` with a single `WHEN NOT MATCHED` clause. - `CombineFilters` merged it into the filter below it, and `RewritePredicateSubquery` then put it below the semi join of an `IN` or `EXISTS` subquery. - `PushPredicateThroughNonJoin` pushed it below a non-outer `explode`. This PR makes two changes. 1. `CheckInvariant` overrides `throwable` to `true`, as `RaiseError` does since SPARK-58627, so that the rules above keep the filter above these operators. 2. `RewritePredicateSubquery` keeps throwable conjuncts above the joins that it rewrites `IN` and `EXISTS` subqueries to. The scan pushdown of a DSv2 source (`V2ScanRelationPushDown`) merges adjacent filters regardless of `throwable`, so the check can still reach this rule in the same filter as the subquery. This also applies to other throwable predicates such as `raise_error`. ### Why are the changes needed? A write fails on a row that it does not write. For example, in a DSv2 catalog that supports CHECK constraints: ```sql CREATE TABLE t (id INT, value INT, CONSTRAINT positive_id CHECK (id > 0)); CREATE TABLE s (id INT, value INT); INSERT INTO s VALUES (-1, 10), (1, 20); -- Only (1, 20) is written, but both fail with CHECK_CONSTRAINT_VIOLATION for id -1. INSERT INTO t SELECT s.id, s.value FROM s JOIN VALUES (20) AS v(value) ON s.value = v.value; INSERT INTO t SELECT id, value FROM s WHERE value IN (SELECT * FROM VALUES (20)); ``` In the following `MERGE`, the source row `(-1, 10)` matches the target row `(5, 10)`, so only `(1, 20)` is inserted. It fails in the same way, while the same `MERGE` with two `WHEN NOT MATCHED` clauses succeeds. ```sql -- t contains (5, 10). MERGE INTO t USING s ON t.value = s.value WHEN NOT MATCHED THEN INSERT (id, value) VALUES (s.id, s.value); ``` ### Does this PR introduce _any_ user-facing change? Yes, as a bug fix. Writes to a table with CHECK constraints, or with generated columns whose values the write provides, no longer fail on rows that a join, a subquery or an `explode` removes. CHECK constraints have been enforced since Apache Spark 4.1.0 ([v4.1.0](https://github.com/apache/spark/releases/tag/v4.1.0), 2025-12-11) by SPARK-51902. In addition, a `raise_error` predicate written together with an `IN` or `EXISTS` subquery is no longer evaluated on rows that the subquery removes. ### How was this patch tested? Pass the CIs with the newly added test cases. - `CheckConstraintSuite`: an `INSERT` with a join, an `INSERT` with an `IN` subquery, and a `MERGE` with a single `WHEN NOT MATCHED` clause. They fail without this PR. - `FilterPushdownSuite`: a CHECK constraint filter stays above inner and left anti joins. - `RewriteSubquerySuite`: a CHECK constraint stays above the semi join of an `IN` subquery. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Opus 5.5 -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
