Skip to content

Commit

Permalink
optimize test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
wayneguow committed May 24, 2024
1 parent f1952d4 commit 82fe09d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -432,20 +432,21 @@ trait AlterTableTests extends SharedSparkSession with QueryErrorsBase {
withTable(t) {
sql(s"CREATE TABLE $t (id int) USING $v2Format")

val sqlText = s"ALTER TABLE $t ADD COLUMN point.z double"
checkError(
exception = intercept[AnalysisException] {
sql(s"ALTER TABLE $t ADD COLUMN point.z double")
sql(sqlText)
},
errorClass = "UNRESOLVED_COLUMN.WITH_SUGGESTION_AND_TABLE",
sqlState = "42703",
parameters = Map(
"objectName" -> "`point`",
"tableName" -> toSQLId(t),
"tableName" -> toSQLId(prependCatalogName(t)),
"proposal" -> "`id`"
),
context = ExpectedContext(
fragment = s"ALTER TABLE $t ADD COLUMN point.z double",
start = 0,
fragment = "point.z double",
start = 24 + t.length,
stop = 37 + t.length)
)
}
Expand Down Expand Up @@ -778,7 +779,7 @@ trait AlterTableTests extends SharedSparkSession with QueryErrorsBase {
sqlState = "42703",
parameters = Map(
"objectName" -> "`data`",
"tableName" -> toSQLId(t),
"tableName" -> toSQLId(prependCatalogName(t)),
"proposal" -> "`id`"
),
context = ExpectedContext(
Expand All @@ -803,7 +804,7 @@ trait AlterTableTests extends SharedSparkSession with QueryErrorsBase {
sqlState = "42703",
parameters = Map(
"objectName" -> "`point`.`x`",
"tableName" -> toSQLId(t),
"tableName" -> toSQLId(prependCatalogName(t)),
"proposal" -> "`id`"
),
context = ExpectedContext(
Expand Down Expand Up @@ -884,7 +885,7 @@ trait AlterTableTests extends SharedSparkSession with QueryErrorsBase {
sqlState = "42703",
parameters = Map(
"objectName" -> "`non_exist`",
"tableName" -> toSQLId(t),
"tableName" -> toSQLId(prependCatalogName(t)),
"proposal" -> "`a`, `point`, `b`"
),
context = ExpectedContext(
Expand Down Expand Up @@ -919,8 +920,8 @@ trait AlterTableTests extends SharedSparkSession with QueryErrorsBase {
errorClass = "UNRESOLVED_COLUMN.WITH_SUGGESTION_AND_TABLE",
sqlState = "42703",
parameters = Map(
"objectName" -> "`non_exist`",
"tableName" -> toSQLId(t),
"objectName" -> "`point`.`non_exist`",
"tableName" -> toSQLId(prependCatalogName(t)),
"proposal" -> "`a`, `point`, `b`"
),
context = ExpectedContext(
Expand Down Expand Up @@ -1019,7 +1020,7 @@ trait AlterTableTests extends SharedSparkSession with QueryErrorsBase {
sqlState = "42703",
parameters = Map(
"objectName" -> "`data`",
"tableName" -> toSQLId(t),
"tableName" -> toSQLId(prependCatalogName(t)),
"proposal" -> "`id`"
),
context = ExpectedContext(
Expand All @@ -1044,7 +1045,7 @@ trait AlterTableTests extends SharedSparkSession with QueryErrorsBase {
sqlState = "42703",
parameters = Map(
"objectName" -> "`point`.`x`",
"tableName" -> toSQLId(t),
"tableName" -> toSQLId(prependCatalogName(t)),
"proposal" -> "`id`"
),
context = ExpectedContext(
Expand Down Expand Up @@ -1152,7 +1153,7 @@ trait AlterTableTests extends SharedSparkSession with QueryErrorsBase {
sqlState = "42703",
parameters = Map(
"objectName" -> "`data`",
"tableName" -> toSQLId(t),
"tableName" -> toSQLId(prependCatalogName(t)),
"proposal" -> "`id`"
),
context = ExpectedContext(
Expand All @@ -1177,7 +1178,7 @@ trait AlterTableTests extends SharedSparkSession with QueryErrorsBase {
sqlState = "42703",
parameters = Map(
"objectName" -> "`point`.`x`",
"tableName" -> toSQLId(t),
"tableName" -> toSQLId(prependCatalogName(t)),
"proposal" -> "`id`"
),
context = ExpectedContext(
Expand Down Expand Up @@ -1336,7 +1337,7 @@ trait AlterTableTests extends SharedSparkSession with QueryErrorsBase {
sqlState = "42703",
parameters = Map(
"objectName" -> "`data`",
"tableName" -> toSQLId(t),
"tableName" -> toSQLId(prependCatalogName(t)),
"proposal" -> "`id`"
),
context = ExpectedContext(
Expand Down Expand Up @@ -1366,13 +1367,13 @@ trait AlterTableTests extends SharedSparkSession with QueryErrorsBase {
sqlState = "42703",
parameters = Map(
"objectName" -> "`point`.`x`",
"tableName" -> toSQLId(t),
"tableName" -> toSQLId(prependCatalogName(t)),
"proposal" -> "`id`"
),
context = ExpectedContext(
fragment = sqlText,
start = 0,
stop = 31 + toSQLId(t).length)
stop = 31 + t.length)
)

// with if exists it should pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class V2CommandsCaseSensitivitySuite
with QueryErrorsBase {

import CreateTablePartitioningValidationSuite._
import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._

private val table = ResolvedTable(
catalog,
Expand Down Expand Up @@ -150,14 +149,28 @@ class V2CommandsCaseSensitivitySuite
test("AlterTable: add column - nested") {
Seq("POINT.Z", "poInt.z", "poInt.Z").foreach { ref =>
val field = ref.split("\\.")
alterTableTest(
AddColumns(
table,
Seq(QualifiedColType(
Some(UnresolvedFieldName(field.init.toImmutableArraySeq)),
field.last, LongType, true, None, None, None))),
Seq("Missing field " + field.head)
)
val alter = AddColumns(
table,
Seq(QualifiedColType(
Some(UnresolvedFieldName(field.init.toImmutableArraySeq)),
field.last, LongType, true, None, None, None)))

Seq(true, false).foreach { caseSensitive =>
if (caseSensitive) {
assertAnalysisErrorClass(
inputPlan = alter,
expectedErrorClass = "UNRESOLVED_COLUMN.WITH_SUGGESTION_AND_TABLE",
expectedMessageParameters = Map(
"objectName" -> s"`${field.head}`",
"tableName" -> toSQLId(table.name),
"proposal" -> "`id`, `data`, `point`"
),
caseSensitive = caseSensitive
)
} else {
assertAnalysisSuccess(alter, caseSensitive)
}
}
}
}

Expand Down Expand Up @@ -328,18 +341,21 @@ class V2CommandsCaseSensitivitySuite
test("AlterTable: drop column resolution") {
Seq(Array("ID"), Array("point", "X"), Array("POINT", "X"), Array("POINT", "x")).foreach { ref =>
Seq(true, false).foreach { ifExists =>
val expectedErrors = if (ifExists) {
Seq.empty[String]
} else {
Seq("Missing field " + ref.quoted)
}
val alter = DropColumns(table, Seq(UnresolvedFieldName(ref.toImmutableArraySeq)), ifExists)
if (ifExists) {
// using IF EXISTS will silence all errors for missing columns
assertAnalysisSuccess(alter, caseSensitive = true)
assertAnalysisSuccess(alter, caseSensitive = false)
} else {
alterTableTest(alter, expectedErrors, expectErrorOnCaseSensitive = true)
alterTableTest(
alter = alter,
expectedErrorClass = "UNRESOLVED_COLUMN.WITH_SUGGESTION_AND_TABLE",
expectedMessageParameters = Map(
"objectName" -> s"${toSQLId(ref.toImmutableArraySeq)}",
"tableName" -> toSQLId(table.name),
"proposal" -> "`id`, `data`, `point`"
),
expectErrorOnCaseSensitive = true)
}
}
}
Expand All @@ -348,9 +364,14 @@ class V2CommandsCaseSensitivitySuite
test("AlterTable: rename column resolution") {
Seq(Array("ID"), Array("point", "X"), Array("POINT", "X"), Array("POINT", "x")).foreach { ref =>
alterTableTest(
RenameColumn(table, UnresolvedFieldName(ref.toImmutableArraySeq), "newName"),
Seq("Missing field " + ref.quoted)
)
alter = RenameColumn(table, UnresolvedFieldName(ref.toImmutableArraySeq), "newName"),
expectedErrorClass = "UNRESOLVED_COLUMN.WITH_SUGGESTION_AND_TABLE",
expectedMessageParameters = Map(
"objectName" -> s"${toSQLId(ref.toImmutableArraySeq)}",
"tableName" -> toSQLId(table.name),
"proposal" -> "`id`, `data`, `point`"
),
expectErrorOnCaseSensitive = true)
}
}

Expand All @@ -359,8 +380,13 @@ class V2CommandsCaseSensitivitySuite
alterTableTest(
AlterColumn(table, UnresolvedFieldName(ref.toImmutableArraySeq),
None, Some(true), None, None, None),
Seq("Missing field " + ref.quoted)
)
expectedErrorClass = "UNRESOLVED_COLUMN.WITH_SUGGESTION_AND_TABLE",
expectedMessageParameters = Map(
"objectName" -> s"${toSQLId(ref.toImmutableArraySeq)}",
"tableName" -> toSQLId(table.name),
"proposal" -> "`id`, `data`, `point`"
),
expectErrorOnCaseSensitive = true)
}
}

Expand All @@ -369,8 +395,13 @@ class V2CommandsCaseSensitivitySuite
alterTableTest(
AlterColumn(table, UnresolvedFieldName(ref.toImmutableArraySeq),
Some(StringType), None, None, None, None),
Seq("Missing field " + ref.quoted)
)
expectedErrorClass = "UNRESOLVED_COLUMN.WITH_SUGGESTION_AND_TABLE",
expectedMessageParameters = Map(
"objectName" -> s"${toSQLId(ref.toImmutableArraySeq)}",
"tableName" -> toSQLId(table.name),
"proposal" -> "`id`, `data`, `point`"
),
expectErrorOnCaseSensitive = true)
}
}

Expand All @@ -379,8 +410,13 @@ class V2CommandsCaseSensitivitySuite
alterTableTest(
AlterColumn(table, UnresolvedFieldName(ref.toImmutableArraySeq),
None, None, Some("comment"), None, None),
Seq("Missing field " + ref.quoted)
)
expectedErrorClass = "UNRESOLVED_COLUMN.WITH_SUGGESTION_AND_TABLE",
expectedMessageParameters = Map(
"objectName" -> s"${toSQLId(ref.toImmutableArraySeq)}",
"tableName" -> toSQLId(table.name),
"proposal" -> "`id`, `data`, `point`"
),
expectErrorOnCaseSensitive = true)
}
}

Expand All @@ -397,13 +433,15 @@ class V2CommandsCaseSensitivitySuite

private def alterTableTest(
alter: => AlterTableCommand,
error: Seq[String],
expectedErrorClass: String,
expectedMessageParameters: Map[String, String],
expectErrorOnCaseSensitive: Boolean = true): Unit = {
Seq(true, false).foreach { caseSensitive =>
withSQLConf(SQLConf.CASE_SENSITIVE.key -> caseSensitive.toString) {
val expectError = if (expectErrorOnCaseSensitive) caseSensitive else !caseSensitive
if (expectError) {
assertAnalysisError(alter, error, caseSensitive)
assertAnalysisErrorClass(
alter, expectedErrorClass, expectedMessageParameters, caseSensitive = caseSensitive)
} else {
assertAnalysisSuccess(alter, caseSensitive)
}
Expand Down

0 comments on commit 82fe09d

Please sign in to comment.