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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/constraints.scala:
##########
@@ -194,6 +244,88 @@ case class CheckConstraint(
   }
 }
 
+object CheckConstraint {
+  /**
+   * Whether `e`, with its `RuntimeReplaceable`s replaced, reads the current 
time, date, catalog,
+   * user and the like, which the optimizer replaces with their values. The 
CURRENT_LIKE pattern
+   * stands in for `ReplaceCurrentLike`, which needs a `CatalogManager`, and 
for the functions
+   * `ComputeCurrentTime` replaces. `ComputeCurrentTime` is still applied for 
the casts from TIME
+   * to a timestamp it rewrites, which carry no CURRENT_LIKE bit.
+   */
+  private def readsCurrentContext(e: Expression): Boolean = {
+    val replaced = ReplaceExpressions.replace(e)
+    replaced.containsPattern(CURRENT_LIKE) ||
+      (ComputeCurrentTime.applyForExpression(replaced) ne replaced) ||
+      replaced.exists {
+        case c: Cast => castReadsCurrentDate(c)
+        case _ => false
+      }
+  }
+
+  /**
+   * Whether `cast` reads the current date: `SpecialDatetimeValues` replaces 
it with the value of
+   * 'now', 'today', 'tomorrow' or 'yesterday', or it casts a string with only 
a time part to a
+   * timestamp with local time zone, which completes it with the current date. 
Only a cast of a
+   * constant string at the top level counts, not one into an array, map or 
struct of timestamps,
+   * from a VARIANT, or of a string that only the optimizer makes constant.
+   */
+  private def castReadsCurrentDate(cast: Cast): Boolean = {
+    val completesTime = cast.dataType match {
+      case TimestampType | _: TimestampLTZNanosType => true
+      case _ => false
+    }
+    (completesTime || SpecialDatetimeValues.rewrites(cast.dataType)) &&
+      SpecialDatetimeValues.constantString(cast).exists { s =>
+        SpecialDatetimeValues.readsCurrentDate(s, cast.dataType) ||
+          (completesTime && DateTimeUtils.isTimeOnlyTimestamp(s))
+      }
+  }
+
+  /**
+   * Whether a write with the default parser settings reads `condition` as 
this session does, which
+   * `StoredExpressionText` decides for a generation expression too, so ANSI 
mode, the timestamp
+   * type and the session time zone are those of this session. A `${...}` 
placeholder counts as
+   * read differently, since a write with `spark.sql.variable.substitute` may 
replace it.
+   */
+  private def readsAsOnWrite(condition: String): Boolean = 
!condition.contains("${") && {
+    val conf = SQLConf.get.clone()
+    // Under its legacy config, `StoredExpressionText` would compare the 
session with itself.
+    conf.setConf(SQLConf.USE_CURRENT_SQL_CONFIGS_FOR_GENERATED_COLUMN, false)
+    
SQLConf.withExistingConf(conf)(StoredExpressionText.readsAsInSession(condition))
+  }
+
+  /**
+   * Whether a write that parses the condition resolves `e` differently: `e` 
reads a variable, which
+   * the write looks up in its own session, a column whose type is or contains 
CHAR, which the
+   * write reads without the CHAR type that pads the literals compared with 
it, or a constant that
+   * may depend on the session time zone and reads no current value, such as a 
typed `TIMESTAMP`
+   * literal or `'2024-01-01 00:00:00'` cast to a timestamp, which the write 
computes in its own
+   * time zone. Apart from a cast, which counts only if it needs the time 
zone, a foldable
+   * expression of a timestamp type that reads no current value counts whether 
or not it depends on
+   * the time zone, such as `timestamp_seconds(0)`, and then keeps its 
predicate as before. A
+   * user-defined function is rejected by `failIfCallsUserDefinedFunction`.
+   */
+  private def resolvesDifferentlyOnWrite(e: Expression): Boolean = {
+    e.containsPattern(VARIABLE_REFERENCE) ||
+      e.references.exists { a =>
+        val rawType = CharVarcharUtils.getRawType(a.metadata)
+        rawType.exists(_.existsRecursively(_.isInstanceOf[CharType]))
+      } ||
+      ReplaceExpressions.replace(e).exists {

Review Comment:
   Done in f818b5c28eb: `nodesReadingCurrentContext` finds the nodes that read 
the current context in one bottom-up pass over the replaced condition, and both 
`resolvesDifferentlyOnWrite` and `withPredicateDecision` look them up. The 
replacement also runs once now. Instead of applying `ComputeCurrentTime` to 
each subtree, a node counts if its subtree has the CURRENT_LIKE pattern or a 
cast from TIME to a timestamp, the two kinds of node that the rule rewrites. 
The test for the current time now also covers a cast from TIME to 
`TIMESTAMP_NTZ`.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/constraints.scala:
##########
@@ -194,6 +244,88 @@ case class CheckConstraint(
   }
 }
 
+object CheckConstraint {
+  /**
+   * Whether `e`, with its `RuntimeReplaceable`s replaced, reads the current 
time, date, catalog,
+   * user and the like, which the optimizer replaces with their values. The 
CURRENT_LIKE pattern
+   * stands in for `ReplaceCurrentLike`, which needs a `CatalogManager`, and 
for the functions
+   * `ComputeCurrentTime` replaces. `ComputeCurrentTime` is still applied for 
the casts from TIME
+   * to a timestamp it rewrites, which carry no CURRENT_LIKE bit.
+   */
+  private def readsCurrentContext(e: Expression): Boolean = {
+    val replaced = ReplaceExpressions.replace(e)
+    replaced.containsPattern(CURRENT_LIKE) ||
+      (ComputeCurrentTime.applyForExpression(replaced) ne replaced) ||
+      replaced.exists {
+        case c: Cast => castReadsCurrentDate(c)
+        case _ => false
+      }
+  }
+
+  /**
+   * Whether `cast` reads the current date: `SpecialDatetimeValues` replaces 
it with the value of
+   * 'now', 'today', 'tomorrow' or 'yesterday', or it casts a string with only 
a time part to a
+   * timestamp with local time zone, which completes it with the current date. 
Only a cast of a
+   * constant string at the top level counts, not one into an array, map or 
struct of timestamps,
+   * from a VARIANT, or of a string that only the optimizer makes constant.
+   */
+  private def castReadsCurrentDate(cast: Cast): Boolean = {
+    val completesTime = cast.dataType match {
+      case TimestampType | _: TimestampLTZNanosType => true

Review Comment:
   Done in f818b5c28eb: both places use `TimestampFamily.isLtz`, and the two 
type imports are gone.



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