[ 
https://issues.apache.org/jira/browse/SPARK-60032?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Yang Jie updated SPARK-60032:
-----------------------------
    Description: 
For ALTER TABLE ... ADD CONSTRAINT ... CHECK, DataSourceV2Strategy calls 
CheckConstraint.toV2Constraint on the optimized plan, so the V2 predicate 
stored in the catalog is built from the optimized expression. Writes resolve 
the stored predicate() before predicateSql 
(ResolveTableConstraints.buildCatalystExpression), so whatever the optimizer 
did at ALTER time is what later writes enforce.

For example, ComputeCurrentTime folds current_timestamp() into a literal:

ALTER TABLE t ADD CONSTRAINT vt CHECK (ts <= current_timestamp());
-- the stored predicate is ts <= <ALTER time literal>
INSERT INTO t VALUES (1, 'a', current_timestamp());
-- fails with CHECK_CONSTRAINT_VIOLATION, although the message shows ts <= 
current_timestamp()

CREATE TABLE is not affected, because ResolveTableSpec builds its predicate 
during analysis. DefaultValueExpression and GeneratedColumnExpression avoid the 
same problem by building their V2 form from analyzedChild 
(AnalysisAwareExpression). Building the CHECK predicate from the analyzed 
expression would do the same here.

Raised in https://github.com/apache/spark/pull/59217#discussion_r4199939321


> ALTER TABLE ADD CONSTRAINT builds the stored CHECK predicate from the 
> optimized expression, freezing current_timestamp()
> ------------------------------------------------------------------------------------------------------------------------
>
>                 Key: SPARK-60032
>                 URL: https://issues.apache.org/jira/browse/SPARK-60032
>             Project: Spark
>          Issue Type: Bug
>          Components: SQL
>    Affects Versions: 5.0.0
>            Reporter: Yang Jie
>            Priority: Major
>
> For ALTER TABLE ... ADD CONSTRAINT ... CHECK, DataSourceV2Strategy calls 
> CheckConstraint.toV2Constraint on the optimized plan, so the V2 predicate 
> stored in the catalog is built from the optimized expression. Writes resolve 
> the stored predicate() before predicateSql 
> (ResolveTableConstraints.buildCatalystExpression), so whatever the optimizer 
> did at ALTER time is what later writes enforce.
> For example, ComputeCurrentTime folds current_timestamp() into a literal:
> ALTER TABLE t ADD CONSTRAINT vt CHECK (ts <= current_timestamp());
> -- the stored predicate is ts <= <ALTER time literal>
> INSERT INTO t VALUES (1, 'a', current_timestamp());
> -- fails with CHECK_CONSTRAINT_VIOLATION, although the message shows ts <= 
> current_timestamp()
> CREATE TABLE is not affected, because ResolveTableSpec builds its predicate 
> during analysis. DefaultValueExpression and GeneratedColumnExpression avoid 
> the same problem by building their V2 form from analyzedChild 
> (AnalysisAwareExpression). Building the CHECK predicate from the analyzed 
> expression would do the same here.
> Raised in https://github.com/apache/spark/pull/59217#discussion_r4199939321



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to