LuciferYang commented on code in PR #59545:
URL: https://github.com/apache/spark/pull/59545#discussion_r4505531484
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/constraints.scala:
##########
@@ -151,8 +155,43 @@ case class CheckConstraint(
}
}
+ override def stringArgs: Iterator[Any] =
+ Iterator(child, condition, userProvidedName, tableName,
userProvidedCharacteristic)
+
+ /**
+ * Decides, once ALTER TABLE has analyzed the condition, whether to store
the constraint without
+ * a predicate (`omitPredicate`).
+ *
+ * A write parses `condition` again, with its own settings. It cannot bind a
parameter marker
+ * that the legacy parameter substitution leaves there, resolves an
IDENTIFIER clause in its own
+ * session, and does not resolve a column qualified by the table name the
way ALTER TABLE does.
+ * It also resolves a name such as `current_date` or `user` to the function
or to a column,
+ * depending on its case sensitivity, its reserved keywords and the columns
of the table at that
+ * time. None of them can be told apart after analysis, so the parser sets
`keepPredicate` for
+ * them, and for any name with more than one part, since it cannot tell a
table qualifier from a
+ * struct column. Such a condition, or one that
`resolvesDifferentlyOnWrite`, keeps the predicate
+ * built from the optimized plan, as before. So does one that calls a
user-defined function,
+ * which the analysis rejects next and which is not evaluated here.
+ */
+ def markAsAnalyzed(): CheckConstraint = copy(omitPredicate =
Review Comment:
Renamed to `withPredicateDecision` in aa6fde9b003. Its doc says that
`AddCheckConstraint.storeAnalyzedQuery` calls it at the end of the analysis,
the comment above `toV2Constraint` says that only `AddCheckConstraint` decides,
and the doc of `KeepAnalyzedQuery` says that `storeAnalyzedQuery` may also take
from the analyzed plan what the command needs later, and that `NormalizePlan`
calls it too.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/finishAnalysis.scala:
##########
@@ -316,14 +317,49 @@ object SpecialDatetimeValues extends Rule[LogicalPlan] {
expression.transformWithPruning(_.containsPattern(CAST))(specialDatetimeRewrite)
}
+ /** 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 a value that reads
the current date, that
+ * is, the value of 'now', 'today', 'tomorrow' or 'yesterday' rather than of
'epoch'.
+ */
+ private[sql] def readsCurrentDate(s: UTF8String, dt: DataType): Boolean =
+ rewrites(dt) && DateTimeUtils.isCurrentSpecialValue(s.toString)
+
+ /**
+ * The constant string that `cast` casts, or None if it casts something else
or evaluating the
+ * string fails. Unlike the rule, this does not throw.
+ */
+ private[sql] def constantString(cast: Cast): Option[UTF8String] = cast.child
match {
+ case e if isConstantString(e) =>
+ try {
+ Option(e.eval().asInstanceOf[UTF8String])
+ } catch {
+ case NonFatal(_) => None
+ }
+ case _ => None
+ }
+
private val specialDatetimeRewrite: PartialFunction[Expression, Expression]
= {
- case cast @ Cast(e, dt @ (DateType | TimestampType | TimestampNTZType), _,
_)
- if e.foldable && e.dataType.isInstanceOf[StringType] =>
+ case cast @ FoldableStringCast(e, dt) =>
Option(e.eval())
.flatMap(s => conv(dt)(s.toString, cast.zoneId))
.map(Literal(_, dt))
.getOrElse(cast)
}
+
+ private def isConstantString(e: Expression): Boolean =
+ e.foldable && e.dataType.isInstanceOf[StringType]
+
+ /** A cast of a foldable string to a type in `conv`. */
+ private object FoldableStringCast {
Review Comment:
Done in aa6fde9b003: the rewrite uses a guard with `rewrites` and the
extractor is 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]