forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-48659][SQL][TESTS] Unify v1 and v2 ALTER TABLE .. SET TBLPROPE…
…RTIES tests ### What changes were proposed in this pull request? - Move parser tests from `o.a.s.s.c.p.DDLParserSuite` and `o.a.s.s.e.c.DDLParserSuite` to `AlterTableSetTblPropertiesParserSuite`. - Porting and refactoring DS v1 tests from `DDLSuite` and `HiveDDLSuite` to `v1.AlterTableSetTblPropertiesBase` and to `v1.AlterTableSetTblPropertiesSuite`. - Add a test for DSv2 `ALTER TABLE .. RENAME` to `v2.AlterTableSetTblPropertiesSuite`. ### Why are the changes needed? - To improve test coverage. - Align with other similar tests, eg: `AlterTableRename*` ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? - Add new UT & Update existed UT. - By running new test suites: ``` $ build/sbt -Phive -Phive-thriftserver "test:testOnly *AlterTableSetTblPropertiesSuite" $ build/sbt -Phive -Phive-thriftserver "test:testOnly *AlterTableSetTblPropertiesParserSuite" ``` ### Was this patch authored or co-authored using generative AI tooling? No. Closes apache#47018 from panbingkun/SPARK-48659. Authored-by: panbingkun <[email protected]> Signed-off-by: Wenchen Fan <[email protected]>
- Loading branch information
1 parent
3469ec6
commit cd8bf11
Showing
9 changed files
with
263 additions
and
62 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
55 changes: 55 additions & 0 deletions
55
.../scala/org/apache/spark/sql/execution/command/AlterTableSetTblPropertiesParserSuite.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.SparkThrowable | ||
import org.apache.spark.sql.catalyst.analysis.{AnalysisTest, UnresolvedTable} | ||
import org.apache.spark.sql.catalyst.parser.CatalystSqlParser.parsePlan | ||
import org.apache.spark.sql.catalyst.parser.ParseException | ||
import org.apache.spark.sql.catalyst.plans.logical.SetTableProperties | ||
import org.apache.spark.sql.test.SharedSparkSession | ||
|
||
class AlterTableSetTblPropertiesParserSuite extends AnalysisTest with SharedSparkSession { | ||
|
||
private def parseException(sqlText: String): SparkThrowable = { | ||
intercept[ParseException](sql(sqlText).collect()) | ||
} | ||
|
||
// ALTER TABLE table_name SET TBLPROPERTIES ('comment' = new_comment); | ||
test("alter table: alter table properties") { | ||
val sql1_table = "ALTER TABLE table_name SET TBLPROPERTIES ('test' = 'test', " + | ||
"'comment' = 'new_comment')" | ||
comparePlans( | ||
parsePlan(sql1_table), | ||
SetTableProperties( | ||
UnresolvedTable(Seq("table_name"), "ALTER TABLE ... SET TBLPROPERTIES", true), | ||
Map("test" -> "test", "comment" -> "new_comment"))) | ||
} | ||
|
||
test("alter table - property values must be set") { | ||
val sql = "ALTER TABLE my_tab SET TBLPROPERTIES('key_without_value', 'key_with_value'='x')" | ||
checkError( | ||
exception = parseException(sql), | ||
errorClass = "_LEGACY_ERROR_TEMP_0035", | ||
parameters = Map("message" -> "Values must be specified for key(s): [key_without_value]"), | ||
context = ExpectedContext( | ||
fragment = sql, | ||
start = 0, | ||
stop = 78)) | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...st/scala/org/apache/spark/sql/execution/command/AlterTableSetTblPropertiesSuiteBase.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,69 @@ | ||
/* | ||
* 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.{AnalysisException, QueryTest} | ||
import org.apache.spark.sql.catalyst.TableIdentifier | ||
|
||
/** | ||
* This base suite contains unified tests for the `ALTER TABLE .. SET TBLPROPERTIES` | ||
* 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.AlterTableSetTblPropertiesSuite` | ||
* - V1 table catalog tests: | ||
* `org.apache.spark.sql.execution.command.v1.AlterTableSetTblPropertiesSuiteBase` | ||
* - V1 In-Memory catalog: | ||
* `org.apache.spark.sql.execution.command.v1.AlterTableSetTblPropertiesSuite` | ||
* - V1 Hive External catalog: | ||
* `org.apache.spark.sql.hive.execution.command.AlterTableSetTblPropertiesSuite` | ||
*/ | ||
trait AlterTableSetTblPropertiesSuiteBase extends QueryTest with DDLCommandTestUtils { | ||
override val command = "ALTER TABLE .. SET TBLPROPERTIES" | ||
|
||
def checkTblProps(tableIdent: TableIdentifier, expectedTblProps: Map[String, String]): Unit | ||
|
||
test("alter table set tblproperties") { | ||
withNamespaceAndTable("ns", "tbl") { t => | ||
sql(s"CREATE TABLE $t (col1 int, col2 string, a int, b int) $defaultUsing") | ||
val tableIdent = TableIdentifier("tbl", Some("ns"), Some(catalog)) | ||
checkTblProps(tableIdent, Map.empty[String, String]) | ||
|
||
sql(s"ALTER TABLE $t SET TBLPROPERTIES ('k1' = 'v1', 'k2' = 'v2', 'k3' = 'v3')") | ||
checkTblProps(tableIdent, Map("k1" -> "v1", "k2" -> "v2", "k3" -> "v3")) | ||
|
||
sql(s"USE $catalog.ns") | ||
sql(s"ALTER TABLE tbl SET TBLPROPERTIES ('k1' = 'v1', 'k2' = 'v2', 'k3' = 'v3')") | ||
checkTblProps(tableIdent, Map("k1" -> "v1", "k2" -> "v2", "k3" -> "v3")) | ||
|
||
sql(s"ALTER TABLE $t SET TBLPROPERTIES ('k1' = 'v1', 'k2' = 'v8')") | ||
checkTblProps(tableIdent, Map("k1" -> "v1", "k2" -> "v8", "k3" -> "v3")) | ||
|
||
// table to alter does not exist | ||
checkError( | ||
exception = intercept[AnalysisException] { | ||
sql("ALTER TABLE does_not_exist SET TBLPROPERTIES ('winner' = 'loser')") | ||
}, | ||
errorClass = "TABLE_OR_VIEW_NOT_FOUND", | ||
parameters = Map("relationName" -> "`does_not_exist`"), | ||
context = ExpectedContext(fragment = "does_not_exist", start = 12, stop = 25) | ||
) | ||
} | ||
} | ||
} |
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
62 changes: 62 additions & 0 deletions
62
...est/scala/org/apache/spark/sql/execution/command/v1/AlterTableSetTblPropertiesSuite.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,62 @@ | ||
/* | ||
* 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.v1 | ||
|
||
import org.apache.spark.sql.catalyst.TableIdentifier | ||
import org.apache.spark.sql.execution.command | ||
import org.apache.spark.sql.internal.StaticSQLConf.CATALOG_IMPLEMENTATION | ||
|
||
/** | ||
* This base suite contains unified tests for the `ALTER TABLE .. SET TBLPROPERTIES` | ||
* command that check V1 table catalogs. The tests that cannot run for all V1 catalogs | ||
* are located in more specific test suites: | ||
* | ||
* - V1 In-Memory catalog: | ||
* `org.apache.spark.sql.execution.command.v1.AlterTableSetTblPropertiesSuite` | ||
* - V1 Hive External catalog: | ||
* `org.apache.spark.sql.hive.execution.command.AlterTableSetTblPropertiesSuite` | ||
*/ | ||
trait AlterTableSetTblPropertiesSuiteBase extends command.AlterTableSetTblPropertiesSuiteBase { | ||
|
||
private[sql] lazy val sessionCatalog = spark.sessionState.catalog | ||
|
||
private def isUsingHiveMetastore: Boolean = { | ||
spark.sparkContext.conf.get(CATALOG_IMPLEMENTATION) == "hive" | ||
} | ||
|
||
private def normalizeTblProps(props: Map[String, String]): Map[String, String] = { | ||
props.filterNot(p => Seq("transient_lastDdlTime").contains(p._1)) | ||
} | ||
|
||
private def getTableProperties(tableIdent: TableIdentifier): Map[String, String] = { | ||
sessionCatalog.getTableMetadata(tableIdent).properties | ||
} | ||
|
||
override def checkTblProps(tableIdent: TableIdentifier, | ||
expectedTblProps: Map[String, String]): Unit = { | ||
val actualTblProps = getTableProperties(tableIdent) | ||
if (isUsingHiveMetastore) { | ||
assert(normalizeTblProps(actualTblProps) == expectedTblProps) | ||
} else { | ||
assert(actualTblProps == expectedTblProps) | ||
} | ||
} | ||
} | ||
|
||
class AlterTableSetTblPropertiesSuite | ||
extends AlterTableSetTblPropertiesSuiteBase with CommandSuiteBase |
50 changes: 50 additions & 0 deletions
50
...est/scala/org/apache/spark/sql/execution/command/v2/AlterTableSetTblPropertiesSuite.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,50 @@ | ||
/* | ||
* 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.v2 | ||
|
||
import scala.jdk.CollectionConverters._ | ||
|
||
import org.apache.spark.sql.catalyst.TableIdentifier | ||
import org.apache.spark.sql.connector.catalog.{Identifier, Table} | ||
import org.apache.spark.sql.connector.catalog.CatalogV2Implicits.CatalogHelper | ||
import org.apache.spark.sql.execution.command | ||
|
||
/** | ||
* The class contains tests for the `ALTER TABLE .. SET TBLPROPERTIES` command to | ||
* check V2 table catalogs. | ||
*/ | ||
class AlterTableSetTblPropertiesSuite | ||
extends command.AlterTableSetTblPropertiesSuiteBase with CommandSuiteBase { | ||
|
||
private def normalizeTblProps(props: Map[String, String]): Map[String, String] = { | ||
props.filterNot(p => Seq("provider", "owner").contains(p._1)) | ||
} | ||
|
||
private def getTableMetadata(tableIndent: TableIdentifier): Table = { | ||
val nameParts = tableIndent.nameParts | ||
val v2Catalog = spark.sessionState.catalogManager.catalog(nameParts.head).asTableCatalog | ||
val namespace = nameParts.drop(1).init.toArray | ||
v2Catalog.loadTable(Identifier.of(namespace, nameParts.last)) | ||
} | ||
|
||
override def checkTblProps(tableIdent: TableIdentifier, | ||
expectedTblProps: Map[String, String]): Unit = { | ||
val actualTblProps = getTableMetadata(tableIdent).properties.asScala.toMap | ||
assert(normalizeTblProps(actualTblProps) === expectedTblProps) | ||
} | ||
} |
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
27 changes: 27 additions & 0 deletions
27
...t/scala/org/apache/spark/sql/hive/execution/command/AlterTableSetTblPropertiesSuite.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,27 @@ | ||
/* | ||
* 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.hive.execution.command | ||
|
||
import org.apache.spark.sql.execution.command.v1 | ||
|
||
/** | ||
* The class contains tests for the `ALTER TABLE .. SET TBLPROPERTIES` command to check | ||
* V1 Hive external table catalog. | ||
*/ | ||
class AlterTableSetTblPropertiesSuite | ||
extends v1.AlterTableSetTblPropertiesSuiteBase with CommandSuiteBase |