Skip to content

Commit e5a4fe9

Browse files
committed
simplify repeated code in tests
1 parent 3de55d6 commit e5a4fe9

File tree

2 files changed

+17
-41
lines changed

2 files changed

+17
-41
lines changed

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

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,13 @@ trait AlterTableTests extends SharedSparkSession with QueryErrorsBase {
15911591
}
15921592
}
15931593

1594+
private def checkConflictSpecialColNameResult(table: String): Unit = {
1595+
val result = sql(s"SELECT * FROM $table").collect()
1596+
assert(result.length == 1)
1597+
assert(!result(0).getBoolean(0))
1598+
assert(result(0).get(1) != null)
1599+
}
1600+
15941601
test("Add column with special column name default value conflicting with column name") {
15951602
withSQLConf(SQLConf.DEFAULT_COLUMN_ALLOWED_PROVIDERS.key -> s"$v2Format, ") {
15961603
val t = fullTableName("table_name")
@@ -1599,40 +1606,28 @@ trait AlterTableTests extends SharedSparkSession with QueryErrorsBase {
15991606
sql(s"CREATE TABLE $t (i boolean) USING $v2Format")
16001607
sql(s"ALTER TABLE $t ADD COLUMN s timestamp DEFAULT current_timestamp")
16011608
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)
1609+
checkConflictSpecialColNameResult(t)
16061610
}
16071611
// There is a default value with special column name 'current_user' but in uppercase.
16081612
withTable(t) {
16091613
sql(s"CREATE TABLE $t (i boolean) USING $v2Format")
16101614
sql(s"ALTER TABLE $t ADD COLUMN s string DEFAULT CURRENT_USER")
16111615
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+
checkConflictSpecialColNameResult(t)
16161617
}
16171618
// There is a default value with special column name same as current column name
16181619
withTable(t) {
16191620
sql(s"CREATE TABLE $t (b boolean) USING $v2Format")
16201621
sql(s"ALTER TABLE $t ADD COLUMN current_timestamp timestamp DEFAULT current_timestamp")
16211622
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)
1623+
checkConflictSpecialColNameResult(t)
16261624
}
16271625
// There is a default value with special column name same as another column name
16281626
withTable(t) {
16291627
sql(s"CREATE TABLE $t (current_date boolean) USING $v2Format")
16301628
sql(s"ALTER TABLE $t ADD COLUMN s date DEFAULT current_date")
16311629
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)
1630+
checkConflictSpecialColNameResult(t)
16361631
}
16371632
}
16381633
}
@@ -1645,40 +1640,28 @@ trait AlterTableTests extends SharedSparkSession with QueryErrorsBase {
16451640
sql(s"CREATE TABLE $t (i boolean, s timestamp) USING $v2Format")
16461641
sql(s"ALTER TABLE $t ALTER COLUMN s SET DEFAULT current_timestamp")
16471642
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)
1643+
checkConflictSpecialColNameResult(t)
16521644
}
16531645
// There is a default value with special column name 'current_user' but in uppercase.
16541646
withTable(t) {
16551647
sql(s"CREATE TABLE $t (i boolean, s string) USING $v2Format")
16561648
sql(s"ALTER TABLE $t ALTER COLUMN s SET DEFAULT CURRENT_USER")
16571649
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)
1650+
checkConflictSpecialColNameResult(t)
16621651
}
16631652
// There is a default value with special column name same as current column name
16641653
withTable(t) {
1665-
sql(s"CREATE TABLE $t (current_timestamp timestamp, b boolean) USING $v2Format")
1654+
sql(s"CREATE TABLE $t (b boolean, current_timestamp timestamp) USING $v2Format")
16661655
sql(s"ALTER TABLE $t ALTER COLUMN current_timestamp SET DEFAULT current_timestamp")
16671656
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))
1657+
checkConflictSpecialColNameResult(t)
16721658
}
16731659
// There is a default value with special column name same as another column name
16741660
withTable(t) {
16751661
sql(s"CREATE TABLE $t (current_date boolean, s date) USING $v2Format")
16761662
sql(s"ALTER TABLE $t ALTER COLUMN s SET DEFAULT current_date")
16771663
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)
1664+
checkConflictSpecialColNameResult(t)
16821665
}
16831666
}
16841667
}

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3887,14 +3887,7 @@ class DataSourceV2SQLSuiteV1Filter
38873887
val result = sql(s"SELECT * FROM $t").collect()
38883888
assert(result.length == 1)
38893889
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)
3890+
Seq(1 to 8: _*).foreach(i => assert(result(0).get(i) != null))
38983891
}
38993892
}
39003893

0 commit comments

Comments
 (0)