Skip to content

Commit

Permalink
[SPARK-48926][SQL][TESTS] Use checkError method to optimize excepti…
Browse files Browse the repository at this point in the history
…on check logic related to `UNRESOLVED_COLUMN` error classes

### What changes were proposed in this pull request?

This PR aims to use `checkError` method to optimize exception check logic related to `UNRESOLVED_COLUMN` error classes

### Why are the changes needed?

Unify error classes check way to `checkError` method.

### Does this PR introduce _any_ user-facing change?

No.

### How was this patch tested?

Pass related test cases.

### Was this patch authored or co-authored using generative AI tooling?

No.

Closes #47389 from wayneguow/op_un_col.

Authored-by: Wei Guo <[email protected]>
Signed-off-by: Hyukjin Kwon <[email protected]>
  • Loading branch information
wayneguow authored and HyukjinKwon committed Jul 18, 2024
1 parent 8b18914 commit f26eeb0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,17 @@ class LateralColumnAliasSuite extends LateralColumnAliasSuiteBase {
| FROM (SELECT 1 AS id1, id1 + 1 AS id2)) > 5
|""".stripMargin
withLCAOff {
assert(intercept[AnalysisException] { sql(query2) }
.getErrorClass == "UNRESOLVED_COLUMN.WITHOUT_SUGGESTION")
checkError(
exception = intercept[AnalysisException] {
sql(query2)
},
errorClass = "UNRESOLVED_COLUMN.WITHOUT_SUGGESTION",
sqlState = "42703",
parameters = Map("objectName" -> s"`id1`"),
context = ExpectedContext(
fragment = "id1",
start = 73,
stop = 75))
}
withLCAOn { checkAnswer(sql(query2), Seq.empty) }

Expand Down Expand Up @@ -783,18 +792,44 @@ class LateralColumnAliasSuite extends LateralColumnAliasSuiteBase {
}

test("Attribute cannot be resolved by LCA remain unresolved") {
assert(intercept[AnalysisException] {
sql(s"SELECT dept AS d, d AS new_dept, new_dep + 1 AS newer_dept FROM $testTable")
}.getErrorClass == "UNRESOLVED_COLUMN.WITH_SUGGESTION")

assert(intercept[AnalysisException] {
sql(s"SELECT count(name) AS cnt, cnt + 1, count(unresovled) FROM $testTable GROUP BY dept")
}.getErrorClass == "UNRESOLVED_COLUMN.WITH_SUGGESTION")

assert(intercept[AnalysisException] {
sql(s"SELECT * FROM range(1, 7) WHERE (" +
s"SELECT id2 FROM (SELECT 1 AS id, other_id + 1 AS id2)) > 5")
}.getErrorClass == "UNRESOLVED_COLUMN.WITHOUT_SUGGESTION")
checkError(
exception = intercept[AnalysisException] {
sql(s"SELECT dept AS d, d AS new_dept, new_dep + 1 AS newer_dept FROM $testTable")
},
errorClass = "UNRESOLVED_COLUMN.WITH_SUGGESTION",
sqlState = "42703",
parameters = Map("objectName" -> s"`new_dep`",
"proposal" -> "`dept`, `name`, `bonus`, `salary`, `properties`"),
context = ExpectedContext(
fragment = "new_dep",
start = 33,
stop = 39))

checkError(
exception = intercept[AnalysisException] {
sql(s"SELECT count(name) AS cnt, cnt + 1, count(unresovled) FROM $testTable GROUP BY dept")
},
errorClass = "UNRESOLVED_COLUMN.WITH_SUGGESTION",
sqlState = "42703",
parameters = Map("objectName" -> s"`unresovled`",
"proposal" -> "`name`, `bonus`, `dept`, `properties`, `salary`"),
context = ExpectedContext(
fragment = "unresovled",
start = 42,
stop = 51))

checkError(
exception = intercept[AnalysisException] {
sql(s"SELECT * FROM range(1, 7) WHERE (" +
s"SELECT id2 FROM (SELECT 1 AS id, other_id + 1 AS id2)) > 5")
},
errorClass = "UNRESOLVED_COLUMN.WITHOUT_SUGGESTION",
sqlState = "42703",
parameters = Map("objectName" -> s"`other_id`"),
context = ExpectedContext(
fragment = "other_id",
start = 66,
stop = 73))
}

test("Pushed-down aggregateExpressions should have no duplicates") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,30 @@ class SqlScriptingInterpreterSuite extends QueryTest with SharedSparkSession {
}

test("session vars - var out of scope") {
val varName: String = "testVarName"
val e = intercept[AnalysisException] {
val sqlScript =
"""
s"""
|BEGIN
| BEGIN
| DECLARE testVarName = 1;
| SELECT testVarName;
| DECLARE $varName = 1;
| SELECT $varName;
| END;
| SELECT testVarName;
| SELECT $varName;
|END
|""".stripMargin
verifySqlScriptResult(sqlScript, Seq.empty)
}
assert(e.getErrorClass === "UNRESOLVED_COLUMN.WITHOUT_SUGGESTION")
assert(e.getMessage.contains("testVarName"))
checkError(
exception = e,
errorClass = "UNRESOLVED_COLUMN.WITHOUT_SUGGESTION",
sqlState = "42703",
parameters = Map("objectName" -> s"`$varName`"),
context = ExpectedContext(
fragment = s"$varName",
start = 79,
stop = 89)
)
}

test("session vars - drop var statement") {
Expand Down

0 comments on commit f26eeb0

Please sign in to comment.