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_r328626270
 
 

 ##########
 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()
 
 Review comment:
   There are 2 "table" in this context:
   1. the table entry in Spark metastore
   2. the table returned by `TableProvider`
   
   For example, `CREATE TABLE abc USING jdbc OPTIONS(table='xyz')`, `abc` is 
the table entry in Spark metastore, `xyz` is the table returned by 
`TableProvider`. The table entry in Spark metastore is simply a link to the 
table returned by `TableProvider`. So the CREATE TABLE here is not to create a 
table in JDBC, but to create a table in Spark metastore that links to the JDBC 
table.

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to