LuciferYang commented on code in PR #60549:
URL: https://github.com/apache/spark/pull/60549#discussion_r4631253134


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -856,9 +857,17 @@ case class Cast(
   }
 
   override def contextIndependentFoldable: Boolean = {
-    child.contextIndependentFoldable && !Cast.needsTimeZone(child.dataType, 
dataType)
+    child.contextIndependentFoldable && !Cast.needsTimeZone(child.dataType, 
dataType) &&
+      !castsSpecialDatetimeValue
   }
 
+  /**
+   * Whether this casts a special value such as 'today' or 'epoch', which 
`SpecialDatetimeValues`
+   * replaces with its value. Evaluating the cast does not give that value.
+   */
+  private[sql] def castsSpecialDatetimeValue: Boolean = 
SpecialDatetimeValues.rewrites(dataType) &&

Review Comment:
   I tried it: `SpecialDatetimeValues.applyForExpression` before 
`constantFolding`, with only the casts that read the current date excluded. Two 
cases broke:
   
   - A cast to TIMESTAMP or TIMESTAMP_LTZ(p) needs a time zone, so it is never 
context independent and never reaches the folding case. Under ANSI mode, 
`DEFAULT CAST('epoch' AS TIMESTAMP)` became a V2 cast of the string 'epoch' 
again.
   - In the analyzed condition of a CHECK constraint in CREATE TABLE, a cast to 
DATE or TIMESTAMP_NTZ(p) has no time zone, and the rule reads `cast.zoneId`. 
`CHECK (d >= CAST('epoch' AS DATE))` failed with `NoSuchElementException`.
   
   So the cast stays not context independent, and the builder turns a cast of 
'epoch' into its value itself. CREATE TABLE now stores the same predicate as 
ALTER TABLE for DATE, TIMESTAMP and both nanosecond types, and `DEFAULT 
CAST('epoch' AS DATE)` gets a V2 literal. The tests check both.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -856,9 +857,17 @@ case class Cast(
   }
 
   override def contextIndependentFoldable: Boolean = {
-    child.contextIndependentFoldable && !Cast.needsTimeZone(child.dataType, 
dataType)
+    child.contextIndependentFoldable && !Cast.needsTimeZone(child.dataType, 
dataType) &&

Review Comment:
   I changed the title and added both points to the description: what this 
fixes, and that a table created before the fix keeps the predicate or default 
value it stored until the constraint is added again or the default is set 
again. I also added an entry to the migration guide, and added the 
`correctness` label and the affected versions from 4.1.0 to the JIRA.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/finishAnalysis.scala:
##########
@@ -320,13 +320,16 @@ object SpecialDatetimeValues extends Rule[LogicalPlan] {
   /** Whether this rule may replace a cast to `dt`. */
   private[sql] def rewrites(dt: DataType): Boolean = conv.contains(dt)
 
+  /** Whether this rule replaces a cast of `s` to `dt` with the value of a 
special value. */
+  private[sql] def replaces(s: UTF8String, dt: DataType): Boolean =

Review Comment:
   I merged master and resolved the conflict with `conv.lift(dt)`, as you 
described. `CastSuiteBase` now has casts to TIMESTAMP_NTZ(9) and 
TIMESTAMP_NTZ(7), and the CREATE TABLE test has TIMESTAMP_NTZ(9) and 
TIMESTAMP_LTZ(9), for 'today' or 'now' and for 'epoch'. I left TIMESTAMP_LTZ(p) 
out of `CastSuiteBase`: a cast from a string to it needs a time zone, so it is 
not context independent anyway.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -856,9 +857,17 @@ case class Cast(
   }
 
   override def contextIndependentFoldable: Boolean = {
-    child.contextIndependentFoldable && !Cast.needsTimeZone(child.dataType, 
dataType)
+    child.contextIndependentFoldable && !Cast.needsTimeZone(child.dataType, 
dataType) &&
+      !castsSpecialDatetimeValue

Review Comment:
   `castsSpecialDatetimeValue` is now `lazy val specialDatetimeValue`, so each 
`Cast` computes its special value once.
   



-- 
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