szehon-ho commented on code in PR #60368:
URL: https://github.com/apache/spark/pull/60368#discussion_r4569606442
##########
docs/sql-migration-guide.md:
##########
@@ -89,7 +89,8 @@ license: |
- Since Spark 4.4, the Spark Thrift Server logs a warning at startup when
`hive.server2.enable.doAs` is `true` and `hive.server2.authentication` verifies
user identities (any recognized type other than `NONE`/`NOSASL`), because
executor-side data access still runs as the server's own service identity
rather than the connecting user (SPARK-5159). Set
`spark.sql.hive.thriftServer.allowIneffectiveDoAs` to `true` to acknowledge the
limitation and silence the warning. Spark 5.0 is expected to refuse to start on
this configuration unless that option is set.
- Since Spark 4.4, `UPDATE` and `MERGE` on a Data Source V2 table fail with
`DATATYPE_MISMATCH.INVALID_ROW_LEVEL_OPERATION_ASSIGNMENTS` when they assign to
a metadata column of the table. This includes `UPDATE SET *` and `INSERT *` in
`MERGE WITH SCHEMA EVOLUTION` when the source has a column with the name of a
metadata column of the target table. Previously, these assignments passed
analysis and were usually ignored. To restore the previous behavior, set
`spark.sql.legacy.ignoreMetadataColumnAssignments` to `true`.
- Since Spark 4.4, `CREATE TABLE`, `REPLACE TABLE`, and `ALTER TABLE ... ADD
CONSTRAINT` on a Data Source V2 table fail with
`USER_DEFINED_FUNCTION_IN_CHECK_CONSTRAINT` when the condition of a CHECK
constraint calls a user-defined scalar function, including a Data Source V2
function. `ALTER TABLE ... ADD CONSTRAINT` keeps failing with
`UNSUPPORTED_SQL_UDF_USAGE` on a SQL function. Previously, a write could
resolve the function name in its own session, so that it enforced another rule
or failed (SPARK-60777). To run these statements, change the condition to call
only built-in functions. Existing constraints that call a user-defined function
are not rejected.
-- Since Spark 4.4, a write to a Data Source V2 table parses and analyzes the
stored text of a generation expression with ANSI mode on and the default values
of the SQL configs that change how a text reads, such as
`spark.sql.parser.escapedStringLiterals`, regardless of the session of the
write. Only the following still follow that session: the session time zone,
case sensitivity, `spark.sql.timestampType`,
`spark.sql.decimalOperations.allowPrecisionLoss`, the configs that a view also
takes from the session that reads it, the functions found through `SET PATH`,
and the configs that an expression reads during evaluation, such as
`spark.sql.legacy.timeParserPolicy`. Any other config that the parsing or the
analysis reads takes its default value, also one that a function reads while it
is built, such as `spark.sql.legacy.negativeIndexInArrayInsert`. A text that
does not resolve under these settings, such as `flag = 1` on a `BOOLEAN`
column, is parsed with the ANSI mode of that session a
nd analyzed under that session, and a text that does not parse under them is
parsed under that session. Previously, the session of the write decided how the
text read, so a write could silently store a wrong value (SPARK-60757). As a
result, with `spark.sql.ansi.enabled=false`, also for a table created with that
setting, a write can fail with an error such as `CAST_INVALID_INPUT` where
Spark 4.3 stored `NULL`, and an implicit cast can change the stored value. The
validation of a provided value compares it with the value that the write would
store, so a value that only casts to it now fails with
`CHECK_CONSTRAINT_VIOLATION`, also with ANSI mode on. For example, for `b
STRING GENERATED ALWAYS AS (coalesce(s, 1))` with `s = '007'`, which stores
`7`, a provided `07` fails. A table created with a non-default value of a
config such as `spark.sql.parser.escapedStringLiterals` also gets the default
reading of its text on every write, without an error. A table CHECK constraint
is still read
under the session that writes, so a constraint that repeats the text of a
generation expression can fail a write whose session sets such a config. A row
that Spark 4.3 stored differently can fail with such an error or with
`CHECK_CONSTRAINT_VIOLATION` when a later write provides it again, such as an
`INSERT ... SELECT` from the same table. A `DELETE` does not check the
generated columns of the rows that it carries. To fix such a table, create a
new table with a text that reads as intended under these settings, and copy the
other columns into it with `INSERT ... SELECT`. To read the text under the
session of the write, as Spark 4.3 does, set
`spark.sql.legacy.useCurrentConfigsForGeneratedColumn` to `true`.
+- Since Spark 4.4, a write to a Data Source V2 table with the
`GENERATE_COLUMN_VALUES_ON_WRITE` capability uses a modeled connector
generation expression when Spark can convert it, for both auto-filling missing
generated values and validating provided values. Only when the modeled
expression is absent or cannot be converted does Spark read the SQL text. A
converted modeled expression is analyzed with ANSI mode on and keeps the
explicit and implicit casts stored by the connector. For example, for `b STRING
GENERATED ALWAYS AS (coalesce(s, 1))` with `s = '007'`, a convertible modeled
expression stored by `CREATE TABLE` with ANSI mode off generates `'007'`; one
stored with ANSI mode on generates `'7'`. Analysis or evaluation errors in a
converted modeled expression fail the write without falling back to SQL text or
the writing session's type coercion. Previously, auto-fill used only SQL text.
Validation already preferred modeled expressions, but analyzed them under the
writing session
and compared against the expression before its cast to the column type
(SPARK-61495).
Review Comment:
Updated in 9a94b407d81. The migration guide now explicitly states that no
config restores SQL-text-first evaluation of a convertible model. It recommends
explicit casts and typed literals to encode the intended result, such as
`coalesce(s, '1')` for string semantics. Added coverage for an ANSI-created
model used by a non-ANSI writer under both settings of the legacy flag:
auto-fill stores `'7'`, a supplied `'7'` passes, and `'007'` fails validation.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/StoredExpressionText.scala:
##########
@@ -97,24 +103,53 @@ object StoredExpressionText {
inSession: => E,
input: Seq[Attribute],
analyzer: LogicalPlan => LogicalPlan): E = {
+ val analyzed = try {
+ expressionFromPlan(expr, analyzePlan(expr, input, analyzer,
conf(SQLConf.get)))
+ } catch {
+ case _: AnalysisException => None
+ }
+ analyzed.getOrElse(inSession)
+ }
+
+ /**
+ * Analyzes a modeled expression under fixed ANSI settings and reports
errors without fallback.
+ */
+ def analyzeStrict[E <: Expression](
+ expr: E,
+ input: Seq[Attribute],
+ analyzer: LogicalPlan => LogicalPlan): E = {
+ val plan = analyzePlan(expr, input, analyzer, fixedConf(SQLConf.get))
+ // Check under the session's representation settings, including
first-class CHAR/VARCHAR.
+ SimpleAnalyzer.checkAnalysis(plan)
Review Comment:
Fixed in 9a94b407d81. Strict modeled-analysis failures now report
`INVALID_GENERATED_COLUMN_EXPRESSION` with the table, generated column and
stored expression. The original `AnalysisException` is retained as the cause,
including its error condition and diagnostic. Tests cover auto-fill and
supplied-value validation for unresolved references and invalid type
combinations, plus a table without a catalog. Analysis errors still fail
without SQL or session fallback.
##########
sql/catalyst/src/main/java/org/apache/spark/sql/connector/catalog/GenerationExpression.java:
##########
@@ -26,10 +26,11 @@
* Connectors can define generated columns using either a SQL string (Spark
SQL dialect) or a more
* portable connector {@link Expression expression}, whose semantics follow
the ANSI SQL standard.
* <p>
- * Spark computes a generated value on a write only from the SQL string, so a
column without one is
- * not auto-filled. To check a provided value, Spark parses the SQL string if
one is provided, also
- * when an expression is provided. Otherwise, Spark converts the given
expression to its internal
- * representation.
+ * To compute a missing generated value or check a provided value, Spark first
attempts to convert
+ * the connector expression to its internal representation. If the expression
is absent or cannot
+ * be converted, Spark falls back to parsing the SQL string, if provided.
Review Comment:
Updated in 9a94b407d81. Both `GenerationExpression` and
`Column.generationExpression()` Javadocs now explain that Spark takes the SQL
fallback from the legacy getter when the modern generation-expression object
has no SQL string.
##########
sql/core/src/test/scala/org/apache/spark/sql/connector/GeneratedColumnWriteSuite.scala:
##########
@@ -347,6 +364,402 @@ class GeneratedColumnWriteSuite extends QueryTest with
DatasourceV2SQLBase {
}
}
+ gridTest("modeled generation expression auto-fill")(
Review Comment:
Updated in 9a94b407d81. All tests added or renamed by this PR now use the
`SPARK-61495:` prefix, including the converter and write-analysis tests and the
new folded-constant regressions. All 607 tests and the Scala/Java style checks
passed locally.
--
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]