aokolnychyi commented on code in PR #40734:
URL: https://github.com/apache/spark/pull/40734#discussion_r1162264468
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Strategy.scala:
##########
@@ -99,16 +100,6 @@ class DataSourceV2Strategy(session: SparkSession) extends
Strategy with Predicat
session.sharedState.cacheManager.uncacheQuery(session, v2Relation, cascade
= true)
}
- private def makeQualifiedDBObjectPath(location: String): String = {
Review Comment:
Exposing this logic in the companion object to be used for staging tables as
well.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2StageTables.scala:
##########
@@ -0,0 +1,80 @@
+/*
+ * 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 scala.collection.JavaConverters._
+
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.catalyst.analysis.{NoSuchTableException,
ResolvedIdentifier}
+import org.apache.spark.sql.catalyst.plans.logical.{CreateTableAsSelect,
LogicalPlan, NoopCommand, ReplaceTableAsSelect, TableSpec}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.util.CharVarcharUtils
+import org.apache.spark.sql.connector.catalog.{CatalogV2Implicits,
CatalogV2Util, Column, StagingTableCatalog}
+import org.apache.spark.sql.errors.QueryCompilationErrors
+import org.apache.spark.sql.types.StructType
+
+object V2StageTables extends Rule[LogicalPlan] {
+
+ import CatalogV2Implicits._
+
+ override def apply(plan: LogicalPlan): LogicalPlan = plan transformDown {
+ case ctas @ CreateTableAsSelect(ResolvedIdentifier(catalog:
StagingTableCatalog, ident),
+ partitioning, query, tableSpec, _, ifNotExists, _, None, None) =>
+ if (catalog.tableExists(ident)) {
+ if (ifNotExists) {
+ NoopCommand("CREATE TABLE ... AS SELECT",
ident.asMultipartIdentifier)
+ } else {
+ throw QueryCompilationErrors.tableAlreadyExistsError(ident)
+ }
+ } else {
+ val columns = toColumns(query.schema)
+ val properties = toProperties(tableSpec)
+ val table = catalog.stageCreate(ident, columns, partitioning.toArray,
properties.asJava)
+ ctas.copy(table = Some(table))
+ }
+
+ case rtas @ ReplaceTableAsSelect(ResolvedIdentifier(catalog:
StagingTableCatalog, ident),
+ partitioning, query, tableSpec, _, orCreate, _, None, None) =>
+ val columns = toColumns(query.schema)
+ val properties = toProperties(tableSpec)
+ val table = if (orCreate) {
+ catalog.stageCreateOrReplace(ident, columns, partitioning.toArray,
properties.asJava)
+ } else if (catalog.tableExists(ident)) {
+ try {
+ catalog.stageReplace(ident, columns, partitioning.toArray,
properties.asJava)
+ } catch {
+ case e: NoSuchTableException =>
+ throw QueryCompilationErrors.cannotReplaceMissingTableError(ident,
Some(e))
+ }
+ } else {
+ throw QueryCompilationErrors.cannotReplaceMissingTableError(ident)
+ }
+ rtas.copy(table = Some(table))
+ }
+
+ private def toColumns(schema: StructType): Array[Column] = {
+ val rawSchema = CharVarcharUtils.getRawSchema(schema, conf)
+ CatalogV2Util.structTypeToV2Columns(rawSchema.asNullable)
+ }
+
+ private def toProperties(tableSpec: TableSpec): Map[String, String] = {
+ val session = SparkSession.active
Review Comment:
The idea to depend on `SparkSession` in the optimizer is questionable.
However, that's what's required to reuse the existing logic to qualify the
location. To be honest, I am not sure Spark should qualify the location for all
catalogs. I understand this is needed for internal sources but it feels a bit
odd to qualify the location based on internal warehouse location within Spark
for all catalogs. Other catalogs may have totally different rules for handling
locations. Getting more feedback on this point would be great!
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]