Skip to content

Commit ad8f55e

Browse files
committed
Add more tests
1 parent da190b3 commit ad8f55e

File tree

4 files changed

+198
-4
lines changed

4 files changed

+198
-4
lines changed

sql/core/src/test/scala/org/apache/spark/sql/connector/AlterTableTests.scala

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,4 +1590,96 @@ trait AlterTableTests extends SharedSparkSession with QueryErrorsBase {
15901590
Row(1, Map(Row(20, null) -> Row("d", null)), "sales")))
15911591
}
15921592
}
1593+
1594+
test("Add column with special column name default value conflicting with column name") {
1595+
withSQLConf(SQLConf.DEFAULT_COLUMN_ALLOWED_PROVIDERS.key -> s"$v2Format, ") {
1596+
val t = fullTableName("table_name")
1597+
// There is a default value that is a special column name 'current_timestamp'.
1598+
withTable(t) {
1599+
sql(s"CREATE TABLE $t (i boolean) USING $v2Format")
1600+
sql(s"ALTER TABLE $t ADD COLUMN s timestamp DEFAULT current_timestamp")
1601+
sql(s"INSERT INTO $t(i) VALUES(false)")
1602+
val result = sql(s"SELECT * FROM $t").collect()
1603+
assert(result.length == 1)
1604+
assert(!result(0).getBoolean(0))
1605+
assert(result(0).getTimestamp(1) != null)
1606+
}
1607+
// There is a default value with special column name 'current_user' but in uppercase.
1608+
withTable(t) {
1609+
sql(s"CREATE TABLE $t (i boolean) USING $v2Format")
1610+
sql(s"ALTER TABLE $t ADD COLUMN s string DEFAULT CURRENT_USER")
1611+
sql(s"INSERT INTO $t(i) VALUES(false)")
1612+
val result = sql(s"SELECT * FROM $t").collect()
1613+
assert(result.length == 1)
1614+
assert(!result(0).getBoolean(0))
1615+
assert(result(0).getString(1) != null)
1616+
}
1617+
// There is a default value with special column name same as current column name
1618+
withTable(t) {
1619+
sql(s"CREATE TABLE $t (b boolean) USING $v2Format")
1620+
sql(s"ALTER TABLE $t ADD COLUMN current_timestamp timestamp DEFAULT current_timestamp")
1621+
sql(s"INSERT INTO $t(b) VALUES(false)")
1622+
val result = sql(s"SELECT * FROM $t").collect()
1623+
assert(result.length == 1)
1624+
assert(!result(0).getBoolean(0))
1625+
assert(result(0).getTimestamp(1) != null)
1626+
}
1627+
// There is a default value with special column name same as another column name
1628+
withTable(t) {
1629+
sql(s"CREATE TABLE $t (current_date boolean) USING $v2Format")
1630+
sql(s"ALTER TABLE $t ADD COLUMN s date DEFAULT current_date")
1631+
sql(s"INSERT INTO $t(current_date) VALUES(false)")
1632+
val result = sql(s"SELECT * FROM $t").collect()
1633+
assert(result.length == 1)
1634+
assert(!result(0).getBoolean(0))
1635+
assert(result(0).getDate(1) != null)
1636+
}
1637+
}
1638+
}
1639+
1640+
test("Set default value for existing column conflicting with special column names") {
1641+
withSQLConf(SQLConf.DEFAULT_COLUMN_ALLOWED_PROVIDERS.key -> s"$v2Format, ") {
1642+
val t = fullTableName("table_name")
1643+
// There is a default value that is a special column name 'current_timestamp'.
1644+
withTable(t) {
1645+
sql(s"CREATE TABLE $t (i boolean, s timestamp) USING $v2Format")
1646+
sql(s"ALTER TABLE $t ALTER COLUMN s SET DEFAULT current_timestamp")
1647+
sql(s"INSERT INTO $t(i) VALUES(false)")
1648+
val result = sql(s"SELECT * FROM $t").collect()
1649+
assert(result.length == 1)
1650+
assert(!result(0).getBoolean(0))
1651+
assert(result(0).getTimestamp(1) != null)
1652+
}
1653+
// There is a default value with special column name 'current_user' but in uppercase.
1654+
withTable(t) {
1655+
sql(s"CREATE TABLE $t (i boolean, s string) USING $v2Format")
1656+
sql(s"ALTER TABLE $t ALTER COLUMN s SET DEFAULT CURRENT_USER")
1657+
sql(s"INSERT INTO $t(i) VALUES(false)")
1658+
val result = sql(s"SELECT * FROM $t").collect()
1659+
assert(result.length == 1)
1660+
assert(!result(0).getBoolean(0))
1661+
assert(result(0).getString(1) != null)
1662+
}
1663+
// There is a default value with special column name same as current column name
1664+
withTable(t) {
1665+
sql(s"CREATE TABLE $t (current_timestamp timestamp, b boolean) USING $v2Format")
1666+
sql(s"ALTER TABLE $t ALTER COLUMN current_timestamp SET DEFAULT current_timestamp")
1667+
sql(s"INSERT INTO $t(b) VALUES(false)")
1668+
val result = sql(s"SELECT * FROM $t").collect()
1669+
assert(result.length == 1)
1670+
assert(result(0).getTimestamp(0) != null)
1671+
assert(!result(0).getBoolean(1))
1672+
}
1673+
// There is a default value with special column name same as another column name
1674+
withTable(t) {
1675+
sql(s"CREATE TABLE $t (current_date boolean, s date) USING $v2Format")
1676+
sql(s"ALTER TABLE $t ALTER COLUMN s SET DEFAULT current_date")
1677+
sql(s"INSERT INTO $t(current_date) VALUES(false)")
1678+
val result = sql(s"SELECT * FROM $t").collect()
1679+
assert(result.length == 1)
1680+
assert(!result(0).getBoolean(0))
1681+
assert(result(0).getDate(1) != null)
1682+
}
1683+
}
1684+
}
15931685
}

sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3870,14 +3870,51 @@ class DataSourceV2SQLSuiteV1Filter
38703870
}
38713871
}
38723872

3873-
test("test default value conflicting with special column name") {
3873+
test("test default value special column name conflicting with real column name") {
38743874
val t = "testcat.ns.t"
38753875
withTable("t") {
38763876
sql(s"""CREATE table $t (
38773877
c1 STRING,
3878-
c2 TIMESTAMP,
3879-
c3 BOOLEAN DEFAULT FALSE,
3880-
current_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP)""")
3878+
current_date DATE DEFAULT CURRENT_DATE,
3879+
current_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
3880+
current_time time DEFAULT CURRENT_TIME,
3881+
current_user STRING DEFAULT CURRENT_USER,
3882+
session_user STRING DEFAULT SESSION_USER,
3883+
user STRING DEFAULT USER,
3884+
current_database STRING DEFAULT CURRENT_DATABASE(),
3885+
current_catalog STRING DEFAULT CURRENT_CATALOG())""")
3886+
sql(s"INSERT INTO $t (c1) VALUES ('a')")
3887+
val result = sql(s"SELECT * FROM $t").collect()
3888+
assert(result.length == 1)
3889+
assert(result(0).getString(0) == "a")
3890+
assert(result(0).get(1) != null)
3891+
assert(result(0).get(2) != null)
3892+
assert(result(0).get(3) != null)
3893+
assert(result(0).get(4) != null)
3894+
assert(result(0).get(5) != null)
3895+
assert(result(0).get(6) != null)
3896+
assert(result(0).get(7) != null)
3897+
assert(result(0).get(8) != null)
3898+
}
3899+
}
3900+
3901+
test("test default value should not refer to real column") {
3902+
val t = "testcat.ns.t"
3903+
withTable("t") {
3904+
checkError(
3905+
exception = intercept[AnalysisException] {
3906+
sql(s"""CREATE table $t (
3907+
c1 STRING,
3908+
current_timestamp TIMESTAMP DEFAULT c1)""")
3909+
},
3910+
condition = "INVALID_DEFAULT_VALUE.UNRESOLVED_EXPRESSION",
3911+
parameters = Map(
3912+
"statement" -> "CREATE TABLE",
3913+
"colName" -> "`current_timestamp`",
3914+
"defaultValue" -> "c1"
3915+
),
3916+
sqlState = Some("42623")
3917+
)
38813918
}
38823919
}
38833920

sql/core/src/test/scala/org/apache/spark/sql/connector/UpdateTableSuiteBase.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,33 @@ abstract class UpdateTableSuiteBase extends RowLevelOperationSuiteBase {
660660
Row(1, 42, "hr") :: Row(2, 2, "software") :: Row(3, 42, "hr") :: Nil)
661661
}
662662

663+
test("update with current_timestamp default value using DEFAULT keyword") {
664+
sql(s"""CREATE TABLE $tableNameAsString
665+
| (pk INT NOT NULL, current_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP)""".stripMargin)
666+
append("pk INT NOT NULL, current_timestamp TIMESTAMP",
667+
"""{ "pk": 1, "i": false, "current_timestamp": "2023-01-01 10:00:00" }
668+
|{ "pk": 2, "i": true, "current_timestamp": "2023-01-01 11:00:00" }
669+
|""".stripMargin)
670+
671+
val initialResult = sql(s"SELECT * FROM $tableNameAsString").collect()
672+
assert(initialResult.length == 2)
673+
val initialTimestamp1 = initialResult(0).getTimestamp(1)
674+
val initialTimestamp2 = initialResult(1).getTimestamp(1)
675+
676+
sql(s"UPDATE $tableNameAsString SET current_timestamp = DEFAULT WHERE pk = 1")
677+
678+
val updatedResult = sql(s"SELECT * FROM $tableNameAsString").collect()
679+
assert(updatedResult.length == 2)
680+
681+
val updatedRow = updatedResult.find(_.getInt(0) == 1).get
682+
val unchangedRow = updatedResult.find(_.getInt(0) == 2).get
683+
684+
// The timestamp should be different (newer) after the update for pk=1
685+
assert(updatedRow.getTimestamp(1).getTime > initialTimestamp1.getTime)
686+
// The timestamp should remain unchanged for pk=2
687+
assert(unchangedRow.getTimestamp(1).getTime == initialTimestamp2.getTime)
688+
}
689+
663690
test("update char/varchar columns") {
664691
createTable("pk INT NOT NULL, s STRUCT<n_c: CHAR(3), n_vc: VARCHAR(5)>, dep STRING")
665692

sql/core/src/test/scala/org/apache/spark/sql/sources/InsertSuite.scala

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,44 @@ class InsertSuite extends DataSourceTest with SharedSparkSession {
10971097
sql("insert into t select false, default")
10981098
checkAnswer(spark.table("t"), Row(false, 42L))
10991099
}
1100+
// There is a default value that is a special column name 'current_timestamp'.
1101+
withTable("t") {
1102+
sql("create table t(i boolean, s timestamp default current_timestamp) using parquet")
1103+
sql("insert into t(i) values(false)")
1104+
val result = spark.table("t").collect()
1105+
assert(result.length == 1)
1106+
assert(!result(0).getBoolean(0))
1107+
assert(result(0).getTimestamp(1) != null)
1108+
}
1109+
// There is a default value with special column name 'current_user' but in uppercase.
1110+
withTable("t") {
1111+
sql("create table t(i boolean, s string default CURRENT_USER) using parquet")
1112+
sql("insert into t(i) values(false)")
1113+
val result = spark.table("t").collect()
1114+
assert(result.length == 1)
1115+
assert(!result(0).getBoolean(0))
1116+
assert(result(0).getString(1) != null)
1117+
}
1118+
// There is a default value with special column name same as current column name
1119+
withTable("t") {
1120+
sql("create table t(current_timestamp timestamp default current_timestamp, b boolean) " +
1121+
"using parquet")
1122+
sql("insert into t(b) values(false)")
1123+
val result = spark.table("t").collect()
1124+
assert(result.length == 1)
1125+
assert(result(0).getTimestamp(0) != null)
1126+
assert(!result(0).getBoolean(1))
1127+
}
1128+
// There is a default value with special column name same as another column name
1129+
withTable("t") {
1130+
sql("create table t(current_date boolean, s date default current_date) " +
1131+
"using parquet")
1132+
sql("insert into t(current_date) values(false)")
1133+
val result = spark.table("t").collect()
1134+
assert(result.length == 1)
1135+
assert(!result(0).getBoolean(0))
1136+
assert(result(0).getDate(1) != null)
1137+
}
11001138
// There is a complex query plan in the SELECT query in the INSERT INTO statement.
11011139
withTable("t") {
11021140
sql("create table t(i boolean default false, s bigint default 42) using parquet")

0 commit comments

Comments
 (0)