spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [spark] cloud-fan commented on a change in pull request #25747: [SPARK-29039][SQL] centralize the catalog and table lookup logic
Date Tue, 17 Sep 2019 18:39:07 GMT
cloud-fan commented on a change in pull request #25747: [SPARK-29039][SQL] centralize the catalog
and table lookup logic
URL: https://github.com/apache/spark/pull/25747#discussion_r325325157
 
 

 ##########
 File path: sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveCatalogs.scala
 ##########
 @@ -0,0 +1,280 @@
+/*
+ * 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.catalyst.analysis
+
+import scala.collection.mutable
+
+import org.apache.spark.sql.{AnalysisException, SaveMode}
+import org.apache.spark.sql.catalyst.TableIdentifier
+import org.apache.spark.sql.catalyst.catalog.{BucketSpec, CatalogTable, CatalogTableType,
CatalogUtils}
+import org.apache.spark.sql.catalyst.plans.logical.{CreateTableAsSelect, CreateV2Table, DropTable,
LogicalPlan, ReplaceTable, ReplaceTableAsSelect, ShowNamespaces, ShowTables}
+import org.apache.spark.sql.catalyst.plans.logical.sql.{CreateTableAsSelectStatement, CreateTableStatement,
DropTableStatement, DropViewStatement, ReplaceTableAsSelectStatement, ReplaceTableStatement,
ShowNamespacesStatement, ShowTablesStatement}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.{CatalogManager, CatalogPlugin, Identifier,
LookupCatalog}
+import org.apache.spark.sql.connector.expressions.Transform
+import org.apache.spark.sql.execution.command.{DropTableCommand, ShowTablesCommand}
+import org.apache.spark.sql.execution.datasources.{CreateTable, DataSource}
+import org.apache.spark.sql.execution.datasources.v2.FileDataSourceV2
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types.StructType
+
+/**
+ * Resolves catalogs from the multi-part identifiers in DDL/DML commands.
+ *
+ * For each SQL statement, this rule has 2 different code paths for the session catalog and
other
+ * catalogs.
+ */
+class ResolveCatalogs(val catalogManager: CatalogManager, conf: SQLConf)
+  extends Rule[LogicalPlan] with LookupCatalog {
+  import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._
+
+  override def apply(plan: LogicalPlan): LogicalPlan = plan.resolveOperatorsUp {
+    // For CREATE TABLE [AS SELECT], we should use the v1 command if the catalog is resolved
to the
+    // session catalog and the table provider is not v2.
+    case c: CreateTableStatement =>
+      c.tableName match {
+        case CatalogAndRestNameParts(Right(sessionCatalog), _) if !isV2Provider(c.provider)
=>
+          val tableDesc = buildCatalogTable(c.tableName.toV1Identifier, c.tableSchema,
+            c.partitioning, c.bucketSpec, c.properties, c.provider, c.options, c.location,
+            c.comment, c.ifNotExists)
+          val mode = if (c.ifNotExists) SaveMode.Ignore else SaveMode.ErrorIfExists
+          CreateTable(tableDesc, mode, None)
+
+        case _ =>
+          val (catalog, ident) = getCatalogAndIdent(c.tableName)
+          CreateV2Table(
+            catalog.asTableCatalog,
+            ident,
+            c.tableSchema,
+            // convert the bucket spec and add it as a transform
+            c.partitioning ++ c.bucketSpec.map(_.asTransform),
+            convertTableProperties(c.properties, c.options, c.location, c.comment, c.provider),
+            ignoreIfExists = c.ifNotExists)
+      }
+
+    case c: CreateTableAsSelectStatement =>
+      c.tableName match {
+        case CatalogAndRestNameParts(Right(sessionCatalog), _) if !isV2Provider(c.provider)
=>
+          val tableDesc = buildCatalogTable(c.tableName.toV1Identifier, new StructType,
+            c.partitioning, c.bucketSpec, c.properties, c.provider, c.options, c.location,
+            c.comment, c.ifNotExists)
+          val mode = if (c.ifNotExists) SaveMode.Ignore else SaveMode.ErrorIfExists
+          CreateTable(tableDesc, mode, Some(c.asSelect))
+
+        case _ =>
+          val (catalog, ident) = getCatalogAndIdent(c.tableName)
+          CreateTableAsSelect(
+            catalog.asTableCatalog,
+            ident,
+            // convert the bucket spec and add it as a transform
+            c.partitioning ++ c.bucketSpec.map(_.asTransform),
+            c.asSelect,
+            convertTableProperties(c.properties, c.options, c.location, c.comment, c.provider),
+            writeOptions = c.options.filterKeys(_ != "path"),
+            ignoreIfExists = c.ifNotExists)
+      }
+
+    // For REPLACE TABLE [AS SELECT], we should fail if the catalog is resolved to the
+    // session catalog and the table provider is not v2.
+    case c: ReplaceTableStatement =>
+      c.tableName match {
+        case CatalogAndRestNameParts(Right(sessionCatalog), _) if !isV2Provider(c.provider)
=>
+          throw new AnalysisException("REPLACE TABLE is only supported with v2 tables.")
+
+        case _ =>
+          val (catalog, ident) = getCatalogAndIdent(c.tableName)
+          ReplaceTable(
+            catalog.asTableCatalog,
+            ident,
+            c.tableSchema,
+            // convert the bucket spec and add it as a transform
+            c.partitioning ++ c.bucketSpec.map(_.asTransform),
+            convertTableProperties(c.properties, c.options, c.location, c.comment, c.provider),
+            orCreate = c.orCreate)
+      }
+
+    case c: ReplaceTableAsSelectStatement =>
+      c.tableName match {
+        case CatalogAndRestNameParts(Right(sessionCatalog), _) if !isV2Provider(c.provider)
=>
+          throw new AnalysisException("REPLACE TABLE AS SELECT is only supported with v2
tables.")
+
+        case _ =>
+          val (catalog, ident) = getCatalogAndIdent(c.tableName)
+          ReplaceTableAsSelect(
+            catalog.asTableCatalog,
+            ident,
+            // convert the bucket spec and add it as a transform
+            c.partitioning ++ c.bucketSpec.map(_.asTransform),
+            c.asSelect,
+            convertTableProperties(c.properties, c.options, c.location, c.comment, c.provider),
+            writeOptions = c.options.filterKeys(_ != "path"),
+            orCreate = c.orCreate)
+      }
+
+    case DropTableStatement(
+         CatalogAndRestNameParts(Left(catalog), restNameParts), ifExists, purge) =>
+      DropTable(catalog.asTableCatalog, restNameParts.toIdentifier, ifExists)
+
+    case d @ DropTableStatement(
+         CatalogAndRestNameParts(Right(sessionCatalog), restNameParts), ifExists, purge)
=>
+      DropTableCommand(d.tableName.toV1Identifier, ifExists, isView = false, purge = purge)
+
+    case DropViewStatement(
+         CatalogAndRestNameParts(Left(catalog), restNameParts), _) =>
+      throw new AnalysisException(
+        s"Can not specify catalog `${catalog.name}` for view ${restNameParts.quoted} " +
+          s"because view support in catalog has not been implemented yet")
+
+    case DropViewStatement(
+         CatalogAndRestNameParts(Right(sessionCatalog), restNameParts), ifExists) =>
+      DropTableCommand(restNameParts.toV1Identifier, ifExists, isView = true, purge = false)
+
+    case ShowNamespacesStatement(
+         Some(CatalogAndRestNameParts(Left(catalog), restNameParts)), pattern) =>
+      val namespace = if (restNameParts.isEmpty) None else Some(restNameParts)
+      ShowNamespaces(catalog.asNamespaceCatalog, namespace, pattern)
+
+    case ShowNamespacesStatement(
+         Some(CatalogAndRestNameParts(Right(sessionCatalog), restNameParts)), pattern) =>
+      throw new AnalysisException(
+        "SHOW NAMESPACES is not supported with the session catalog.")
+
+    case ShowNamespacesStatement(None, pattern) =>
+      if (defaultCatalog.isDefined) {
+        ShowNamespaces(defaultCatalog.get.asNamespaceCatalog, None, pattern)
+      } else {
+        throw new AnalysisException(
+          "SHOW NAMESPACES is not supported with the session catalog.")
+      }
+
+    case ShowTablesStatement(
+         Some(CatalogAndRestNameParts(Left(catalog), restNameParts)), pattern) =>
+      ShowTables(catalog.asTableCatalog, restNameParts, pattern)
+
+    case ShowTablesStatement(
+         Some(CatalogAndRestNameParts(Right(sessionCatalog), restNameParts)), pattern) =>
+      if (restNameParts.length != 1) {
+        throw new AnalysisException(
+          s"The database name is not valid: ${restNameParts.quoted}")
+      }
+      ShowTablesCommand(Some(restNameParts.head), pattern)
+
+    case ShowTablesStatement(None, pattern) =>
+      if (defaultCatalog.isDefined) {
+        ShowTables(defaultCatalog.get.asTableCatalog, catalogManager.currentNamespace, pattern)
+      } else {
+        ShowTablesCommand(None, pattern)
+      }
+  }
+
+  private def convertTableProperties(
 
 Review comment:
   This method and below methods are moved from the old `DataSourceResolution`.

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


With regards,
Apache Git Services

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


Mime
View raw message