LuciferYang commented on code in PR #59545:
URL: https://github.com/apache/spark/pull/59545#discussion_r4506946501
##########
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:
Done in c0196346f6c: the class overview and the Javadoc of `predicateSql()`
now say that `predicate()` is null in the cases its Javadoc lists, so the three
places agree.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/constraints.scala:
##########
@@ -151,8 +155,48 @@ case class CheckConstraint(
}
}
+ // The two flags are printed only when set, so that the plan string of a
constraint without
+ // them stays as it was.
+ override def stringArgs: Iterator[Any] =
+ Iterator(child, condition, userProvidedName, tableName,
userProvidedCharacteristic) ++
+ Iterator("keepPredicate").filter(_ => keepPredicate) ++
+ Iterator("omitPredicate").filter(_ => omitPredicate)
+
+ /**
+ * Decides from the analyzed condition whether to store the constraint
without a predicate
+ * (`omitPredicate`). `AddCheckConstraint.storeAnalyzedQuery` calls this at
the end of the
+ * analysis of an ALTER TABLE.
+ *
+ * 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 withPredicateDecision(): CheckConstraint = copy(omitPredicate =
Review Comment:
Agreed that these conditions still compare with the values of the ALTER
TABLE. The PR keeps them on their predicate on purpose: a write could read
their other part differently, and for a name with more than one part the parser
cannot tell a table qualifier from a struct column. I opened SPARK-61194 for
them, with the deferred V2 expressions and the narrower multi-part rule you
describe. The deferred expressions add names that connectors see in
`Check.predicate()`, so I'd settle that interface there rather than in this PR.
Casts into nested types and from a VARIANT are not detected, and stay under
"Not covered" in the description. The description now says that such a
condition is stored and checked as before, as the migration guide entry does.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/constraints.scala:
##########
@@ -151,8 +155,48 @@ case class CheckConstraint(
}
}
+ // The two flags are printed only when set, so that the plan string of a
constraint without
+ // them stays as it was.
+ override def stringArgs: Iterator[Any] =
+ Iterator(child, condition, userProvidedName, tableName,
userProvidedCharacteristic) ++
+ Iterator("keepPredicate").filter(_ => keepPredicate) ++
+ Iterator("omitPredicate").filter(_ => omitPredicate)
+
+ /**
+ * Decides from the analyzed condition whether to store the constraint
without a predicate
+ * (`omitPredicate`). `AddCheckConstraint.storeAnalyzedQuery` calls this at
the end of the
+ * analysis of an ALTER TABLE.
+ *
+ * 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 withPredicateDecision(): CheckConstraint = copy(omitPredicate =
+ !keepPredicate &&
StoredExpressionFunctions.findUserDefinedFunctionCall(child).isEmpty &&
+ !CheckConstraint.resolvesDifferentlyOnWrite(child) &&
+ CheckConstraint.readsCurrentContext(child))
+
+ // ALTER TABLE builds the constraint from the optimized plan, where the
current time, date,
+ // catalog, user and the like are replaced by their values at the time of
the ALTER TABLE. A
+ // condition that `withPredicateDecision` finds reading one is stored
without a predicate, so
+ // that writes check `predicateSql`.
+ // The predicate is not built from the analyzed condition, as the default
value of a column is,
Review Comment:
Done in c0196346f6c: the comment now says that ALTER TABLE calls it on the
optimized plan, and that CREATE and REPLACE TABLE call it on the analyzed
condition in `ResolveTableSpec`, where the `CAST('today' AS DATE)` folding
still happens (SPARK-60451).
--
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]