cloud-fan commented on a change in pull request #25651: [SPARK-28948][SQL] 
Support passing all Table metadata in TableProvider
URL: https://github.com/apache/spark/pull/25651#discussion_r328442663
 
 

 ##########
 File path: 
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/CatalogExtensionForTableProvider.scala
 ##########
 @@ -0,0 +1,98 @@
+/*
+ * 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.datasources.v2
+
+import java.util
+
+import scala.util.control.NonFatal
+
+import org.apache.spark.sql.AnalysisException
+import org.apache.spark.sql.connector.catalog.{DelegatingCatalogExtension, 
Identifier, SupportsSpecifiedSchemaPartitioning, Table}
+import org.apache.spark.sql.connector.expressions.Transform
+import org.apache.spark.sql.execution.datasources.DataSource
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types.StructType
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+class CatalogExtensionForTableProvider extends DelegatingCatalogExtension {
+
+  private val conf = SQLConf.get
+
+  override def loadTable(ident: Identifier): Table = {
+    val table = super.loadTable(ident)
+    tryResolveTableProvider(table)
+  }
+
+  override def createTable(
+      ident: Identifier,
+      schema: StructType,
+      partitions: Array[Transform],
+      properties: util.Map[String, String]): Table = {
+    val provider = properties.getOrDefault("provider", 
conf.defaultDataSourceName)
+    val maybeProvider = DataSource.lookupDataSourceV2(provider, conf)
+    val (actualSchema, actualPartitioning) = if (maybeProvider.isDefined && 
schema.isEmpty) {
+      // A sanity check. The parser should guarantee it.
+      assert(partitions.isEmpty)
+      // If `CREATE TABLE ... USING` does not specify table metadata, get the 
table metadata from
+      // data source first.
+      val table = maybeProvider.get.getTable(new 
CaseInsensitiveStringMap(properties))
+      table.schema() -> table.partitioning()
+    } else {
+      schema -> partitions
+    }
+    super.createTable(ident, actualSchema, actualPartitioning, properties)
+    // call `loadTable` to make sure the schema/partitioning specified in 
`CREATE TABLE ... USING`
+    // matches the actual data schema/partitioning. If error happens during 
table loading, drop
+    // the table.
+    try {
+      loadTable(ident)
+    } catch {
+      case NonFatal(e) =>
+        dropTable(ident)
+        throw e
+    }
+  }
+
+  private def tryResolveTableProvider(table: Table): Table = {
+    val providerName = table.properties().get("provider")
+    assert(providerName != null)
+    DataSource.lookupDataSourceV2(providerName, conf).map {
+      // TODO: support file source v2 in CREATE TABLE USING.
+      case _: FileDataSourceV2 => table
 
 Review comment:
   As you already found out in 
https://github.com/apache/spark/pull/25651#discussion_r328336988
   
   File source v2 can't take the partitioning from metastore because the 
`TableProvider` API was incompleted before. I don't want to fix file source v2 
in this PR, so I simply ignore it here.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to