spark-test commented on code in PR #60549:
URL: https://github.com/apache/spark/pull/60549#discussion_r4593783051
##########
sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2DataFrameSuite.scala:
##########
@@ -1141,6 +1141,38 @@ class DataSourceV2DataFrameSuite
}
}
+ test("SPARK-60451: default values and generated columns on a special
datetime value") {
+ // Evaluating a cast does not give the value of a special value such as
'today' or 'epoch'. As
+ // for `current_date()`, these columns have no V2 expression, so writes
read their SQL text.
+ val t = "testcat.ns.t"
+ Seq(true, false).foreach { ansi =>
+ withSQLConf(SQLConf.ANSI_ENABLED.key -> ansi.toString) {
+ withTable(t) {
+ sql(s"""CREATE TABLE $t (
+ | id INT,
+ | d DATE DEFAULT CAST('today' AS DATE),
+ | e DATE DEFAULT CAST('epoch' AS DATE),
+ | g DATE GENERATED ALWAYS AS (CAST('today' AS DATE))
+ |) USING foo""".stripMargin)
+ val cols = catalog("testcat").loadTable(Identifier.of(Array("ns"),
"t")).columns()
+ assert(cols(1).defaultValue().getExpression == null, ansi)
+ assert(cols(2).defaultValue().getExpression == null, ansi)
+ assert(cols(3).columnGenerationExpression().getExpression == null,
ansi)
+ sql(s"INSERT INTO $t (id) VALUES (1)")
+ // The date can change between the INSERT and the SELECT.
+ checkAnswer(
+ sql(s"SELECT datediff(current_date(), d) IN (0, 1), e, " +
+ s"datediff(current_date(), g) IN (0, 1) FROM $t"),
+ Row(true, java.sql.Date.valueOf("1970-01-01"), true))
+ // ALTER TABLE builds the V2 expression of a new default value the
same way.
+ sql(s"ALTER TABLE $t ALTER COLUMN e SET DEFAULT CAST('tomorrow' AS
DATE)")
+ val altered =
catalog("testcat").loadTable(Identifier.of(Array("ns"), "t")).columns()
+ assert(altered(2).defaultValue().getExpression == null, ansi)
Review Comment:
**Non-blocking (P2):** `testcat` is an `InMemoryTableCatalog`, whose
`alterTable` rebuilds every column that a `ColumnChange` names from the altered
schema (`InMemoryTableCatalog.alteredColumns` calls
`CatalogV2Util.structTypeToV2Columns`). For a column with a default,
`structFieldToV2Column` builds `new ColumnDefaultValue(sql, existsDefault)`,
whose expression is always null. So after the reload,
`altered(2).defaultValue().getExpression` is null whatever `toV2CurrentDefault`
handed to the catalog. Going by the base branch's code, with ANSI mode off the
ALTER passes `LiteralValue(null, DATE)` and this assertion still holds. Only an
exception from the ALTER statement itself can fail it, so nothing here checks
that a default set by ALTER COLUMN has no V2 expression. The CREATE assertions
above are effective, because `createTable` keeps the columns it is given.
The existing test `alter table default value expression should have a cast`
checks this contract on the `UpdateColumnDefaultValue` change of the executed
`AlterTableExec` instead. For example:
```suggestion
// ALTER TABLE builds the V2 expression of a new default value the
same way. The test
// catalog drops it when it rebuilds an altered column, so check
the change it receives.
val alterExec = executeAndKeepPhysicalPlan[AlterTableExec] {
sql(s"ALTER TABLE $t ALTER COLUMN e SET DEFAULT CAST('tomorrow'
AS DATE)")
}
checkDefaultValues(
alterExec.changes.map(_.asInstanceOf[UpdateColumnDefaultValue]).toArray,
Array(new DefaultValue("CAST('tomorrow' AS DATE)", null)))
```
**Verification:**
- **Regression:** The ALTER COLUMN ... SET DEFAULT CAST('tomorrow' AS DATE)
check fails whenever the default value passed to the catalog carries a V2
expression, such as the NULL literal the base branch builds with ANSI mode off
or a date fixed at the time of the ALTER TABLE, and passes with this PR, where
that default has no V2 expression.
- **Behavior:** In both ANSI modes, ALTER COLUMN ... SET DEFAULT with a
special datetime value succeeds and passes the catalog a default value that
keeps its original SQL text and has no V2 expression.
##########
sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2DataFrameSuite.scala:
##########
@@ -1141,6 +1141,38 @@ class DataSourceV2DataFrameSuite
}
}
+ test("SPARK-60451: default values and generated columns on a special
datetime value") {
+ // Evaluating a cast does not give the value of a special value such as
'today' or 'epoch'. As
+ // for `current_date()`, these columns have no V2 expression, so writes
read their SQL text.
Review Comment:
**Nit (P3):** Starting a sentence with "As for `current_date()`" reads as
"regarding `current_date()`", so the comment first seems to be about
`current_date()` itself, while it means that these columns behave like
`current_date()`. "As with", or "Like" as in the new comment in
`V2ExpressionBuilder`, says that. The new test in `CheckConstraintSuite.scala`
opens with the same wording (lines 668-669).
```suggestion
// with `current_date()`, these columns have no V2 expression, so writes
read their SQL text.
```
**Verification:**
- **Inspection:** The comments of the two new SPARK-60451 tests compare
these definitions with `current_date()` using 'As with' or 'Like', and no 'As
for `current_date()`' wording remains in them.
--
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]