LuciferYang commented on code in PR #59545:
URL: https://github.com/apache/spark/pull/59545#discussion_r4505531039
##########
sql/catalyst/src/main/java/org/apache/spark/sql/connector/catalog/constraints/Check.java:
##########
@@ -75,8 +75,10 @@ public String predicateSql() {
/**
* Returns the search condition as a {@link Predicate}, or {@code null} if
the condition cannot be
- * expressed using supported predicate expressions. Use {@link
#predicateSql()} for the canonical
- * SQL representation, which is always present.
+ * expressed using supported predicate expressions. Spark may also leave it
{@code null} for a
Review Comment:
Added in aa6fde9b003, after the other CHECK constraint entries. It covers
the values checked at each write, the writer's session settings, copied rows of
a row-level operation with your 30-day example, the conditions that keep their
predicate, existing constraints, and the violation message.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/v2AlterTableCommands.scala:
##########
@@ -386,8 +386,10 @@ case class AddCheckConstraint(
analyzedQuery: Option[LogicalPlan] = None) extends UnaryCommand with
KeepAnalyzedQuery {
// The optimizer replaces the filtered scan with an empty relation if no row
can violate the
- // check, so keep the analyzed child to find the table when planning.
- override def storeAnalyzedQuery(): Command = copy(analyzedQuery =
Some(child))
+ // check, so keep the analyzed child to find the table when planning. The
constraint decides
+ // here, with its condition analyzed, whether to store a predicate.
+ override def storeAnalyzedQuery(): Command = copy(
Review Comment:
Done in aa6fde9b003 as you suggested: `storeAnalyzedQuery` decides only when
`analyzedQuery` is empty and the constraint is resolved, so the decision is
made once, at the end of the analysis. `CheckConstraintParseSuite` now compares
a parsed `CHECK (CAST(d AS TIMESTAMP) > TIMESTAMP'2020-01-01 00:00:00')`, which
failed with `UnresolvedException` before, and `CheckConstraintSuite` checks
that normalizing the optimized plan keeps `omitPredicate`.
##########
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] =
Review Comment:
Done both in aa6fde9b003: the parser sets `keepPredicate` only in
`visitAddTableConstraint`, and `stringArgs` prints each flag when it is set, so
the plan string of a constraint without them is unchanged.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala:
##########
@@ -7189,11 +7189,27 @@ class AstBuilder extends DataTypeAstBuilder
override def visitCheckConstraint(ctx: CheckConstraintContext):
CheckConstraint =
withOrigin(ctx) {
val condition = getOriginalText(ctx.expr)
+ val child = expression(ctx.booleanExpression())
CheckConstraint(
- child = expression(ctx.booleanExpression()),
- condition = condition)
+ child = child,
+ condition = condition,
+ keepPredicate = child.containsAnyPattern(PARAMETER,
UNRESOLVED_IDENTIFIER) ||
+ hasCurrentLikeKeyword(ctx.booleanExpression()) ||
+ child.exists {
+ case u: UnresolvedAttribute =>
+ u.nameParts.length > 1 ||
LiteralFunctionResolution.isLiteralFunction(u.nameParts)
+ case _ => false
+ })
}
+ /**
+ * Whether `tree` has a keyword such as `current_date` or `user` without
parentheses, which parses
+ * as a column name or as the function depending on
`spark.sql.ansi.enforceReservedKeywords`.
+ */
+ private def hasCurrentLikeKeyword(tree: ParseTree): Boolean = tree match {
Review Comment:
Done in aa6fde9b003 with your cases, and `hasCurrentLikeKeyword` is gone. A
lambda variable with more than one part counts too, as an `UnresolvedAttribute`
does: the parse-tree walk caught `current_user.y` in a lambda body, and the
same check covers `tbl.id` there. Since the check reads only the expression,
`visitAddTableConstraint` sets the flag, which is item 3. A new parse test
covers these and the backquoted `user` in a lambda body.
--
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]