dongjoon-hyun opened a new pull request, #60042:
URL: https://github.com/apache/spark/pull/60042

   ### What changes were proposed in this pull request?
   
   This PR fixes `ALTER TABLE ... ADD CONSTRAINT ... CHECK` on a Data Source V2 
table, which fails with `INTERNAL_ERROR` when the optimizer can prove that no 
existing row violates the condition. Examples are `CHECK (true)`, `CHECK (1 = 
1)`, `CHECK (id > 0 OR true)`, `CHECK (null)`, and `CHECK (id IS NOT NULL)` on 
a `NOT NULL` column.
   
   `AddCheckConstraint` validates the existing rows through its child, 
`Filter(NOT condition, table)`. When the condition always holds, the optimizer 
folds this filter to `false` and replaces the whole child with an empty 
`LocalRelation`. `DataSourceV2Strategy` looked for the target table only in the 
optimized child (`PhysicalOperation(_, _, DataSourceV2ScanRelation)`), so no 
strategy could plan the command.
   
   ```
   AddCheckConstraint checkconstraint(true, id IS NOT NULL, c1, tbl, ...)
   +- LocalRelation <empty>, [id#48, data#49]
   ```
   
   This PR:
   - Makes `AddCheckConstraint` extend `KeepAnalyzedQuery`, so that it stores 
its analyzed child in a non-child field at the end of analysis, as the V2 write 
commands do.
   - Makes `DataSourceV2Strategy` take the table from the optimized child as 
before, and from the analyzed query when the optimizer removed the scan. 
Validation still runs on the optimized child, which returns no rows, and then 
the constraint is added.
   
   ### Why are the changes needed?
   
   A condition that always holds cannot be violated by the existing rows, so 
the statement should succeed and store the constraint. Instead, it fails as 
follows.
   
   ```
   [INTERNAL_ERROR] The Spark SQL phase planning failed with an internal error. 
...
   Caused by: java.lang.AssertionError: assertion failed: No plan for 
AddCheckConstraint ...
   ```
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. `ALTER TABLE ... ADD CONSTRAINT ... CHECK` with a condition that always 
holds now adds the constraint instead of failing with `INTERNAL_ERROR`. This 
bug exists since [v4.1.0](https://github.com/apache/spark/releases/tag/v4.1.0) 
(2025-12-11), which started validating existing rows when adding a CHECK 
constraint.
   
   ### How was this patch tested?
   
   Pass the CIs with the newly added test case in `CheckConstraintSuite`. The 
test fails without this fix with the `INTERNAL_ERROR` above.
   
   ### 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]

Reply via email to