[SPARK-57379][SQL] Block temporary variables in CHECK constraint expressions#56472
Open
yadavay-amzn wants to merge 1 commit into
Open
[SPARK-57379][SQL] Block temporary variables in CHECK constraint expressions#56472yadavay-amzn wants to merge 1 commit into
yadavay-amzn wants to merge 1 commit into
Conversation
0bc7741 to
59df33b
Compare
…essions Uses the existing INVALID_TEMP_OBJ_REFERENCE error class (sqlState 42K0F) which is purpose-built for 'persistent object references temp object' scenarios. Detection: containsPattern(VARIABLE_REFERENCE) in both CREATE/REPLACE TABLE (ResolveTableSpec) and ALTER TABLE ADD CONSTRAINT (CheckAnalysis) paths. Error helper: notAllowedToCreateCheckConstraintReferencingTempVarError passes obj=CHECK CONSTRAINT, objName=constraint name (or empty if inline), tempObj=VARIABLE, tempObjName=variable's originalNameParts.
59df33b to
d21db83
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
Block references to temporary (session) variables in CHECK constraint expressions, on both the
CREATE/REPLACE TABLEpath and theALTER TABLE ... ADD CONSTRAINTpath. A CHECK constraint that references a session variable is now rejected at analysis time withINVALID_TEMP_OBJ_REFERENCE.The check mirrors the structural placement of the existing non-deterministic-constraint validation in both the
CREATE/REPLACE TABLE(ResolveTableSpec) andALTER TABLE ... ADD CONSTRAINT(CheckAnalysis) paths. It usesINVALID_TEMP_OBJ_REFERENCE— the canonical error class already raised when a persistent view references a temporary variable — rather than the generated-column-specific error used by SPARK-57360. Detection scans the constraint expression for anyVariableReferencenode, so nested references (e.g. inside aCAST) are caught.Why are the changes needed?
A CHECK constraint is persisted with the table, but a temporary/session variable is session-scoped and will not exist in other sessions, making the persisted constraint invalid. Today both paths silently accept it:
Spark already blocks the analogous case for persistent views via
INVALID_TEMP_OBJ_REFERENCE; this extends the same protection to CHECK constraints.Does this PR introduce any user-facing change?
Yes. Creating or altering a table with a CHECK constraint that references a temporary variable now fails with
INVALID_TEMP_OBJ_REFERENCEinstead of silently persisting an invalid constraint. No previously-valid constraint is affected.How was this patch tested?
New tests in
CheckConstraintSuitecoveringCREATE TABLE,REPLACE TABLE, andALTER TABLE ADD CONSTRAINT, including a nested reference (CHECK (i > CAST(my_var AS BIGINT))) to confirm detection is recursive, a qualified variable name (session.my_var), and a happy-path guard (a non-variable constraint still succeeds). CTAS/RTAS cannot carry CHECK constraints (the parser rejects them), so they need no coverage. Verified the tests fail without the fix.Was this patch authored or co-authored using generative AI tooling?
Yes.