LuciferYang opened a new pull request, #60549:
URL: https://github.com/apache/spark/pull/60549

   ### What changes were proposed in this pull request?
   
   `V2ExpressionBuilder` folds any expression whose 
`contextIndependentFoldable` is true. `Cast.contextIndependentFoldable` looks 
only at the types, so it is true for a cast of a special datetime string such 
as 'today', 'now' or 'epoch' to DATE or TIMESTAMP_NTZ. Only the optimizer rule 
`SpecialDatetimeValues` gives these strings their values. Evaluating the cast 
treats them as malformed.
   
   This PR makes such a cast not context independent. It also gives every cast 
that `SpecialDatetimeValues` replaces, including one to TIMESTAMP, no V2 form, 
as `current_date()` has none: under ANSI mode the builder turned it into a V2 
cast of the string. A CHECK constraint on such a cast is then stored without a 
predicate, and a DEFAULT value or generation expression without a V2 
expression, so Spark reads their SQL text.
   
   ### Why are the changes needed?
   
   On a Data Source V2 catalog, since SPARK-52875 (4.1.0):
   
   ```sql
   CREATE TABLE t (id INT, d DATE, CONSTRAINT c CHECK (d <= CAST('today' AS 
DATE)));
   -- ANSI on: fails with CAST_INVALID_INPUT
   -- ANSI off: stores the predicate d <= NULL, so every row passes
   CREATE TABLE t (id INT, d DATE DEFAULT CAST('today' AS DATE));
   -- ANSI on: fails with CAST_INVALID_INPUT
   -- ANSI off: INSERT INTO t(id) VALUES (1) writes NULL
   ```
   
   `CHECK (d <= 'today')`, REPLACE TABLE, `ALTER COLUMN ... SET DEFAULT` and 
`GENERATED ALWAYS AS (CAST('today' AS DATE))` behave the same.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. On a Data Source V2 catalog, such a CHECK constraint, DEFAULT value or 
generated column is accepted under ANSI mode, and each write computes its 
value. `Check.predicate()` and the expressions of the `ColumnDefaultValue` and 
the `GenerationExpression` are null for them, also for a cast to TIMESTAMP, so 
a connector reads their SQL text.
   
   ### How was this patch tested?
   
   New tests:
   
   - `CastSuiteBase`: casts of special values to DATE and TIMESTAMP_NTZ, also 
padded, in another case or built by `concat`, are not context independent. 
Other strings still are.
   - `CheckConstraintSuite`: CREATE and REPLACE TABLE with `CAST('today' AS 
DATE)`, `'today'`, `CAST('epoch' AS DATE)` or `CAST('now' AS TIMESTAMP)` store 
no predicate, accept a row on the bound and reject one past it, with ANSI on 
and off.
   - `DataSourceV2DataFrameSuite`: DEFAULT `CAST('today' AS DATE)` and 
`CAST('epoch' AS DATE)` and a generated column `CAST('today' AS DATE)` have no 
V2 expression, and a write fills them with the current date and 1970-01-01, 
with ANSI on and off. A default set by `ALTER COLUMN` has no V2 expression 
either.
   
   Each of them fails without this change.
   
   ### 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