Skip to content

Commit 2e3adad

Browse files
brkyvzcloud-fan
authored andcommitted
[SPARK-31061][SQL] Provide ability to alter the provider of a table
This PR adds functionality to HiveExternalCatalog to be able to change the provider of a table. This is useful for catalogs in Spark 3.0 to be able to use alterTable to change the provider of a table as part of an atomic REPLACE TABLE function. No Unit tests Closes apache#27822 from brkyvz/externalCat. Authored-by: Burak Yavuz <[email protected]> Signed-off-by: Wenchen Fan <[email protected]>
1 parent 6468d6f commit 2e3adad

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveExternalCatalog.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,15 @@ private[spark] class HiveExternalCatalog(conf: SparkConf, hadoopConf: Configurat
634634
k.startsWith(DATASOURCE_PREFIX) || k.startsWith(STATISTICS_PREFIX) ||
635635
k.startsWith(CREATED_SPARK_VERSION)
636636
}
637-
val newTableProps = propsFromOldTable ++ tableDefinition.properties + partitionProviderProp
637+
val newFormatIfExists = tableDefinition.provider.flatMap { p =>
638+
if (DDLUtils.isDatasourceTable(tableDefinition)) {
639+
Some(DATASOURCE_PROVIDER -> p)
640+
} else {
641+
None
642+
}
643+
}
644+
val newTableProps =
645+
propsFromOldTable ++ tableDefinition.properties + partitionProviderProp ++ newFormatIfExists
638646

639647
// // Add old table's owner if we need to restore
640648
val owner = Option(tableDefinition.owner).filter(_.nonEmpty).getOrElse(oldTableDef.owner)

sql/hive/src/test/scala/org/apache/spark/sql/hive/HiveExternalCatalogSuite.scala

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.apache.spark.sql.hive
1919

20+
import java.net.URI
21+
2022
import org.apache.hadoop.conf.Configuration
2123

2224
import org.apache.spark.SparkConf
@@ -178,4 +180,42 @@ class HiveExternalCatalogSuite extends ExternalCatalogSuite {
178180
assertThrows[QueryExecutionException](client.runSqlHive(
179181
"INSERT overwrite directory \"fs://localhost/tmp\" select 1 as a"))
180182
}
183+
184+
test("SPARK-31061: alterTable should be able to change table provider") {
185+
val catalog = newBasicCatalog()
186+
val parquetTable = CatalogTable(
187+
identifier = TableIdentifier("parq_tbl", Some("db1")),
188+
tableType = CatalogTableType.MANAGED,
189+
storage = storageFormat.copy(locationUri = Some(new URI("file:/some/path"))),
190+
schema = new StructType().add("col1", "int").add("col2", "string"),
191+
provider = Some("parquet"))
192+
catalog.createTable(parquetTable, ignoreIfExists = false)
193+
194+
val rawTable = externalCatalog.getTable("db1", "parq_tbl")
195+
assert(rawTable.provider === Some("parquet"))
196+
197+
val fooTable = parquetTable.copy(provider = Some("foo"))
198+
catalog.alterTable(fooTable)
199+
val alteredTable = externalCatalog.getTable("db1", "parq_tbl")
200+
assert(alteredTable.provider === Some("foo"))
201+
}
202+
203+
test("SPARK-31061: alterTable should be able to change table provider from hive") {
204+
val catalog = newBasicCatalog()
205+
val hiveTable = CatalogTable(
206+
identifier = TableIdentifier("parq_tbl", Some("db1")),
207+
tableType = CatalogTableType.MANAGED,
208+
storage = storageFormat,
209+
schema = new StructType().add("col1", "int").add("col2", "string"),
210+
provider = Some("hive"))
211+
catalog.createTable(hiveTable, ignoreIfExists = false)
212+
213+
val rawTable = externalCatalog.getTable("db1", "parq_tbl")
214+
assert(rawTable.provider === Some("hive"))
215+
216+
val fooTable = rawTable.copy(provider = Some("foo"))
217+
catalog.alterTable(fooTable)
218+
val alteredTable = externalCatalog.getTable("db1", "parq_tbl")
219+
assert(alteredTable.provider === Some("foo"))
220+
}
181221
}

0 commit comments

Comments
 (0)