-
Notifications
You must be signed in to change notification settings - Fork 28.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-49119][SQL] Fix the inconsistency of syntax
show columns
bet…
…ween v1 and v2 ### What changes were proposed in this pull request? The pr aims to - fix the `inconsistency` of syntax `show columns` between `v1` and `v2`. - assign a name `SHOW_COLUMNS_WITH_CONFLICT_NAMESPACE` to the error condition `_LEGACY_ERROR_TEMP_1057`. - unify v1 and v2 `SHOW COLUMNS ...` tests. - move some UT related to `SHOW COLUMNS` from `DDLSuite` to `command/ShowColumnsSuiteBase` or `v1/ShowColumnsSuiteBase`. - move some UT related to `SHOW COLUMNS` from `DDLParserSuite` and `ErrorParserSuite` to `ShowColumnsParserSuite`. ### Why are the changes needed? In `AstBuilder`, we have `a comment` that explains as follows: https://github.com/apache/spark/blob/2a752105091ef95f994526b15bae2159657c8ed0/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala#L5054-L5055 However, in our v2 of the syntax `show columns` implementation, we `did not` perform the above checks, as shown below: ``` withNamespaceAndTable("ns", "tbl") { t => sql(s"CREATE TABLE $t (col1 int, col2 string) $defaultUsing") sql(s"SHOW COLUMNS IN $t IN ns1") } ``` - Before (inconsistent, v1 will fail, but v2 will success) v1: ``` [SHOW_COLUMNS_WITH_CONFLICT_NAMESPACE] SHOW COLUMNS with conflicting namespace: `ns1` != `ns`. ``` v2: ``` Execute successfully. ``` #### so, we should fix it. - After (consistent, v1 & v2 all will fail) v1: ``` [SHOW_COLUMNS_WITH_CONFLICT_NAMESPACE] SHOW COLUMNS with conflicting namespace: `ns1` != `ns`. ``` v2: ``` [SHOW_COLUMNS_WITH_CONFLICT_NAMESPACE] SHOW COLUMNS with conflicting namespace: `ns1` != `ns`. ``` ### Does this PR introduce _any_ user-facing change? Yes, for v2 tables, in syntax `SHOW COLUMNS {FROM | IN} {tableName} {FROM | IN} {namespace}`, if the namespace (`second parameter`) is different from the namespace of the table(`first parameter`), the command will succeed without any awareness before this PR, after this PR, it will report an error. ### How was this patch tested? Add new UT. ### Was this patch authored or co-authored using generative AI tooling? No. Closes #47628 from panbingkun/SPARK-49119. Lead-authored-by: panbingkun <[email protected]> Co-authored-by: Kent Yao <[email protected]> Signed-off-by: yangjie01 <[email protected]>
- Loading branch information
1 parent
0602020
commit 53c1f31
Showing
16 changed files
with
297 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowColumnsParserSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.sql.execution.command | ||
|
||
import org.apache.spark.sql.catalyst.analysis.{AnalysisTest, UnresolvedTableOrView} | ||
import org.apache.spark.sql.catalyst.parser.CatalystSqlParser.parsePlan | ||
import org.apache.spark.sql.catalyst.plans.logical.ShowColumns | ||
|
||
class ShowColumnsParserSuite extends AnalysisTest { | ||
|
||
test("show columns") { | ||
comparePlans( | ||
parsePlan("SHOW COLUMNS IN a.b.c"), | ||
ShowColumns( | ||
UnresolvedTableOrView(Seq("a", "b", "c"), "SHOW COLUMNS", allowTempView = true), | ||
None)) | ||
comparePlans( | ||
parsePlan("SHOW COLUMNS FROM a.b.c"), | ||
ShowColumns( | ||
UnresolvedTableOrView(Seq("a", "b", "c"), "SHOW COLUMNS", allowTempView = true), | ||
None)) | ||
comparePlans( | ||
parsePlan("SHOW COLUMNS IN a.b.c FROM a.b"), | ||
ShowColumns(UnresolvedTableOrView(Seq("a", "b", "c"), "SHOW COLUMNS", allowTempView = true), | ||
Some(Seq("a", "b")))) | ||
comparePlans( | ||
parsePlan("SHOW COLUMNS FROM a.b.c IN a.b"), | ||
ShowColumns(UnresolvedTableOrView(Seq("a", "b", "c"), "SHOW COLUMNS", allowTempView = true), | ||
Some(Seq("a", "b")))) | ||
} | ||
|
||
test("illegal characters in unquoted identifier") { | ||
checkError( | ||
exception = parseException(parsePlan)("SHOW COLUMNS IN t FROM test-db"), | ||
errorClass = "INVALID_IDENTIFIER", | ||
sqlState = "42602", | ||
parameters = Map("ident" -> "test-db") | ||
) | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowColumnsSuiteBase.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.sql.execution.command | ||
|
||
import java.util.Locale | ||
|
||
import org.apache.spark.sql.{AnalysisException, QueryTest, Row} | ||
import org.apache.spark.sql.internal.SQLConf | ||
|
||
/** | ||
* This base suite contains unified tests for the `SHOW COLUMNS ...` command that | ||
* check V1 and V2 table catalogs. The tests that cannot run for all supported catalogs are | ||
* located in more specific test suites: | ||
* | ||
* - V2 table catalog tests: | ||
* `org.apache.spark.sql.execution.command.v2.ShowColumnsSuite` | ||
* - V1 table catalog tests: | ||
* `org.apache.spark.sql.execution.command.v1.ShowColumnsSuiteBase` | ||
* - V1 In-Memory catalog: | ||
* `org.apache.spark.sql.execution.command.v1.ShowColumnsSuite` | ||
* - V1 Hive External catalog: | ||
* `org.apache.spark.sql.hive.execution.command.ShowColumnsSuite` | ||
*/ | ||
trait ShowColumnsSuiteBase extends QueryTest with DDLCommandTestUtils { | ||
override val command = "SHOW COLUMNS ..." | ||
|
||
test("basic test") { | ||
withNamespaceAndTable("ns", "tbl") { t => | ||
sql(s"CREATE TABLE $t(col1 int, col2 string) $defaultUsing") | ||
val expected = Seq(Row("col1"), Row("col2")) | ||
checkAnswer(sql(s"SHOW COLUMNS FROM $t IN ns"), expected) | ||
checkAnswer(sql(s"SHOW COLUMNS IN $t FROM ns"), expected) | ||
checkAnswer(sql(s"SHOW COLUMNS IN $t"), expected) | ||
} | ||
} | ||
|
||
test("negative test - the table does not exist") { | ||
withNamespaceAndTable("ns", "tbl") { t => | ||
sql(s"CREATE TABLE $t(col1 int, col2 string) $defaultUsing") | ||
|
||
checkError( | ||
exception = intercept[AnalysisException] { | ||
sql(s"SHOW COLUMNS IN tbl IN ns1") | ||
}, | ||
errorClass = "TABLE_OR_VIEW_NOT_FOUND", | ||
parameters = Map("relationName" -> "`ns1`.`tbl`"), | ||
context = ExpectedContext(fragment = "tbl", start = 16, stop = 18) | ||
) | ||
} | ||
} | ||
|
||
test("the namespace of the table conflicts with the specified namespace") { | ||
withNamespaceAndTable("ns", "tbl") { t => | ||
sql(s"CREATE TABLE $t(col1 int, col2 string) $defaultUsing") | ||
|
||
val sqlText1 = s"SHOW COLUMNS IN $t IN ns1" | ||
val sqlText2 = s"SHOW COLUMNS IN $t FROM ${"ns".toUpperCase(Locale.ROOT)}" | ||
|
||
checkError( | ||
exception = intercept[AnalysisException] { | ||
sql(sqlText1) | ||
}, | ||
errorClass = "SHOW_COLUMNS_WITH_CONFLICT_NAMESPACE", | ||
parameters = Map( | ||
"namespaceA" -> s"`ns1`", | ||
"namespaceB" -> s"`ns`" | ||
) | ||
) | ||
// When case sensitivity is true, the user supplied namespace name in table identifier | ||
// should match the supplied namespace name in case-sensitive way. | ||
withSQLConf(SQLConf.CASE_SENSITIVE.key -> "true") { | ||
checkError( | ||
exception = intercept[AnalysisException] { | ||
sql(sqlText2) | ||
}, | ||
errorClass = "SHOW_COLUMNS_WITH_CONFLICT_NAMESPACE", | ||
parameters = Map( | ||
"namespaceA" -> s"`${"ns".toUpperCase(Locale.ROOT)}`", | ||
"namespaceB" -> "`ns`" | ||
) | ||
) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.