Yang Jie created SPARK-60452:
--------------------------------

             Summary: CHECK constraints freeze typed literals with special 
datetime values such as DATE 'today'
                 Key: SPARK-60452
                 URL: https://issues.apache.org/jira/browse/SPARK-60452
             Project: Spark
          Issue Type: Bug
          Components: SQL
    Affects Versions: 5.0.0
            Reporter: Yang Jie


The parser converts a typed literal with a special value, such as DATE 'today' 
or TIMESTAMP 'yesterday', into a plain literal 
(AstBuilder.visitTypeConstructor). A CHECK constraint stores its condition text 
as predicateSql, so the text still says 'today', but the analyzed condition 
holds only the value at the time of the DDL. SPARK-60032 stores no predicate 
for an ALTER TABLE CHECK condition that reads the current time, and it cannot 
see this case:

{noformat}
ALTER TABLE t ADD CONSTRAINT c CHECK (d <= DATE 'today');
-- the stored predicate compares d with the date of the ALTER TABLE
-- the next day:
INSERT INTO t VALUES (1, current_date());  -- CHECK_CONSTRAINT_VIOLATION on d 
<= DATE 'today'
{noformat}

The result also depends on the rest of the condition. CHECK (created >= 
TIMESTAMP 'yesterday' AND created <= current_timestamp()) is stored without a 
predicate because of current_timestamp(), so writes re-parse the typed literal, 
while CHECK (created >= TIMESTAMP 'yesterday') alone stays frozen.

The analyzed tree cannot tell these apart, so a fix needs the parser to record 
that the condition has such a literal, for example in visitCheckConstraint.




--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

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

Reply via email to