dongjoon-hyun commented on code in PR #59545:
URL: https://github.com/apache/spark/pull/59545#discussion_r4534054408


##########
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:
   Nit: `exists` calls `readsCurrentContext` on every foldable candidate, and 
each call runs `ReplaceExpressions.replace`, 
`ComputeCurrentTime.applyForExpression` and the constant-string evaluation of 
`castReadsCurrentDate` again on a subtree that is already replaced. A foldable 
node that reads the current time returns `false` here, so `exists` goes on into 
its children and repeats the same work for each of them. Then 
`withPredicateDecision` runs `readsCurrentContext(child)` over the whole 
condition once more.
   
   Conditions are small, so this only costs analysis time. Could the replaced 
tree be computed once, for example by finding the nodes that read the current 
context in one bottom-up pass and reusing the result here and in 
`withPredicateDecision`?



##########
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:
   Nit: `TimestampFamily.isLtz` (TimestampFamily.scala:43) already classifies 
`TimestampType` and `TimestampLTZNanosType` as the LTZ family, and this match 
and the one at L320 repeat it by hand. Could both use it, e.g. 
`TimestampFamily.isLtz(cast.dataType)` here and 
`x.dataType.existsRecursively(TimestampFamily.isLtz)` at L320? Then a new LTZ 
variant is covered in one place, and the `TimestampLTZNanosType` and 
`TimestampType` imports can go.



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