stczwd commented on a change in pull request #29339:
URL: https://github.com/apache/spark/pull/29339#discussion_r520238163
##########
File path:
sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala
##########
@@ -2066,13 +2069,68 @@ class DataSourceV2SQLSuite
}
test("ALTER TABLE ADD PARTITION") {
- val t = "testcat.ns1.ns2.tbl"
+ val t = "testpart.ns1.ns2.tbl"
withTable(t) {
spark.sql(s"CREATE TABLE $t (id bigint, data string) USING foo
PARTITIONED BY (id)")
- val e = intercept[AnalysisException] {
- sql(s"ALTER TABLE $t ADD PARTITION (id=1) LOCATION 'loc'")
- }
- assert(e.message.contains("ALTER TABLE ADD PARTITION is only supported
with v1 tables"))
+ spark.sql(s"ALTER TABLE $t ADD PARTITION (id=1) LOCATION 'loc'")
+
+ val partTable = catalog("testpart").asTableCatalog
+ .loadTable(Identifier.of(Array("ns1", "ns2"),
"tbl")).asInstanceOf[InMemoryPartitionTable]
+ assert(partTable.partitionExists(InternalRow.fromSeq(Seq(1))))
+
+ val partMetadata =
partTable.loadPartitionMetadata(InternalRow.fromSeq(Seq(1)))
+ assert(partMetadata.containsKey("location"))
+ assert(partMetadata.get("location") == "loc")
+
+ partTable.clearPartitions()
Review comment:
done
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala
##########
@@ -3457,10 +3457,12 @@ class AstBuilder(conf: SQLConf) extends
SqlBaseBaseVisitor[AnyRef] with Logging
if (ctx.VIEW != null) {
operationNotAllowed("ALTER VIEW ... DROP PARTITION", ctx)
}
- AlterTableDropPartitionStatement(
- visitMultipartIdentifier(ctx.multipartIdentifier),
- ctx.partitionSpec.asScala.map(visitNonOptionalPartitionSpec).toSeq,
- ifExists = ctx.EXISTS != null,
+ val partSpecs =
ctx.partitionSpec.asScala.map(visitNonOptionalPartitionSpec)
+ .map(spec => UnresolvedPartitionSpec(spec))
+ AlterTableDropPartition(
+ UnresolvedTableOrView(visitMultipartIdentifier(ctx.multipartIdentifier)),
Review comment:
done
##########
File path:
sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveSessionCatalog.scala
##########
@@ -514,11 +521,20 @@ class ResolveSessionCatalog(
from,
to)
- case AlterTableDropPartitionStatement(tbl, specs, ifExists, purge,
retainData) =>
- val v1TableName = parseV1Table(tbl, "ALTER TABLE DROP PARTITION")
+ case AlterTableDropPartition(
+ r @ ResolvedTable(_, _, _: V1Table), specs, ifExists, purge,
retainData)
+ if isSessionCatalog(r.catalog) =>
AlterTableDropPartitionCommand(
- v1TableName.asTableIdentifier,
- specs,
+ r.identifier.asTableIdentifier,
+ specs.asUnresolvedPartitionSpecs.map(_.spec),
+ ifExists,
+ purge,
+ retainData)
+
+ case AlterTableDropPartition(r: ResolvedView, specs, ifExists, purge,
retainData) =>
Review comment:
ok
##########
File path:
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/AlterTableAddPartitionExec.scala
##########
@@ -0,0 +1,65 @@
+/*
+ * 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.catalyst.InternalRow
+import
org.apache.spark.sql.catalyst.analysis.{PartitionsAlreadyExistException,
ResolvedPartitionSpec}
+import org.apache.spark.sql.catalyst.expressions.Attribute
+import
org.apache.spark.sql.connector.catalog.{SupportsAtomicPartitionManagement,
SupportsPartitionManagement}
+
+/**
+ * Physical plan node for adding partitions of table.
+ */
+case class AlterTableAddPartitionExec(
+ table: SupportsPartitionManagement,
+ partSpecs: Seq[ResolvedPartitionSpec],
+ ignoreIfExists: Boolean) extends V2CommandExec {
+ import DataSourceV2Implicits._
+
+ override def output: Seq[Attribute] = Seq.empty
+
+ override protected def run(): Seq[InternalRow] = {
+ val (existsParts, notExistsParts) =
+ partSpecs.partition(p => table.partitionExists(p.spec))
+
+ if (existsParts.nonEmpty && !ignoreIfExists) {
+ throw new PartitionsAlreadyExistException(
+ table.name(), existsParts.map(_.spec), table.partitionSchema())
+ }
+
+ notExistsParts match {
+ case Seq() => // Nothing will be done
+ case Seq(partitionSpec) =>
+ val partProp = partitionSpec.location.map(loc => "location" ->
loc).toMap
+ table.createPartition(partitionSpec.spec, partProp.asJava)
+ case Seq(_ *) if table.isInstanceOf[SupportsAtomicPartitionManagement] =>
Review comment:
ok
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolvePartitionSpec.scala
##########
@@ -0,0 +1,51 @@
+/*
+ * 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 org.apache.spark.sql.catalyst.plans.logical.{AlterTableAddPartition,
AlterTableDropPartition, LogicalPlan}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.SupportsPartitionManagement
+import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Implicits
+import org.apache.spark.sql.types.StructType
+
+/**
+ * Analyze PartitionSpecs in datasource v2 commands.
+ */
+object ResolvePartitionSpec extends Rule[LogicalPlan] {
+ import DataSourceV2Implicits._
+
+ def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+ case r @ AlterTableAddPartition(
+ ResolvedTable(_, _, table: SupportsPartitionManagement), partSpecs, _)
=>
+ r.copy(parts = resolvePartitionSpecs(partSpecs, table.partitionSchema()))
+
+ case r @ AlterTableDropPartition(
+ ResolvedTable(_, _, table: SupportsPartitionManagement), partSpecs, _,
_, _) =>
+ r.copy(parts = resolvePartitionSpecs(partSpecs, table.partitionSchema()))
+ }
+
+ def resolvePartitionSpecs(
+ partSpecs: Seq[PartitionSpec], partSchema: StructType):
Seq[ResolvedPartitionSpec] =
+ partSpecs.map {
+ case unresolvedPartSpec: UnresolvedPartitionSpec =>
+ ResolvedPartitionSpec(
+ unresolvedPartSpec.spec.asPartitionIdentifier(partSchema),
unresolvedPartSpec.location)
Review comment:
sure
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/v2Commands.scala
##########
@@ -589,3 +589,37 @@ case class AnalyzeColumn(
"mutually exclusive. Only one of them should be specified.")
override def children: Seq[LogicalPlan] = child :: Nil
}
+
+/**
+ * The logical plan of the ALTER TABLE ADD PARTITION command.
+ *
+ * The syntax of this command is:
+ * {{{
+ * ALTER TABLE table ADD [IF NOT EXISTS]
+ * PARTITION spec1 [LOCATION 'loc1'][, PARTITION spec2
[LOCATION 'loc2'], ...];
+ * }}}
+ */
+case class AlterTableAddPartition(
+ child: LogicalPlan,
+ parts: Seq[PartitionSpec],
+ ifNotExists: Boolean) extends Command {
+ override def children: Seq[LogicalPlan] = child :: Nil
Review comment:
good
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/v2Commands.scala
##########
@@ -589,3 +589,37 @@ case class AnalyzeColumn(
"mutually exclusive. Only one of them should be specified.")
override def children: Seq[LogicalPlan] = child :: Nil
}
+
+/**
+ * The logical plan of the ALTER TABLE ADD PARTITION command.
+ *
+ * The syntax of this command is:
+ * {{{
+ * ALTER TABLE table ADD [IF NOT EXISTS]
+ * PARTITION spec1 [LOCATION 'loc1'][, PARTITION spec2
[LOCATION 'loc2'], ...];
+ * }}}
+ */
+case class AlterTableAddPartition(
+ child: LogicalPlan,
+ parts: Seq[PartitionSpec],
+ ifNotExists: Boolean) extends Command {
+ override def children: Seq[LogicalPlan] = child :: Nil
+}
+
+/**
+ * The logical plan of the ALTER TABLE DROP PARTITION command.
+ * This may remove the data and metadata for this partition.
+ *
+ * The syntax of this command is:
+ * {{{
+ * ALTER TABLE table DROP [IF EXISTS] PARTITION spec1[, PARTITION spec2,
...];
+ * }}}
+ */
+case class AlterTableDropPartition(
+ child: LogicalPlan,
+ parts: Seq[PartitionSpec],
Review comment:
done
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/v2ResolutionPlans.scala
##########
@@ -53,6 +55,12 @@ case class UnresolvedTableOrView(
override def output: Seq[Attribute] = Nil
}
+trait PartitionSpec
Review comment:
Perfect.
##########
File path:
sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveSessionCatalog.scala
##########
@@ -500,11 +501,17 @@ class ResolveSessionCatalog(
v1TableName.asTableIdentifier,
"ALTER TABLE RECOVER PARTITIONS")
- case AlterTableAddPartitionStatement(tbl, partitionSpecsAndLocs,
ifNotExists) =>
- val v1TableName = parseV1Table(tbl, "ALTER TABLE ADD PARTITION")
+ case AlterTableAddPartition(r @ ResolvedTable(_, _, _: V1Table),
partSpecsAndLocs, ifNotExists)
+ if isSessionCatalog(r.catalog) =>
AlterTableAddPartitionCommand(
- v1TableName.asTableIdentifier,
- partitionSpecsAndLocs,
+ r.identifier.asTableIdentifier,
+ partSpecsAndLocs.asUnresolvedPartitionSpecs.map(spec => (spec.spec,
spec.location)),
+ ifNotExists)
+
+ case AlterTableAddPartition(r: ResolvedView, partSpecsAndLocs,
ifNotExists) =>
Review comment:
yes
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala
##########
@@ -3415,10 +3415,10 @@ class AstBuilder(conf: SQLConf) extends
SqlBaseBaseVisitor[AnyRef] with Logging
val specsAndLocs = ctx.partitionSpecLocation.asScala.map { splCtx =>
val spec = visitNonOptionalPartitionSpec(splCtx.partitionSpec)
val location = Option(splCtx.locationSpec).map(visitLocationSpec)
- spec -> location
+ UnresolvedPartitionSpec(spec, location)
}
- AlterTableAddPartitionStatement(
- visitMultipartIdentifier(ctx.multipartIdentifier),
+ AlterTableAddPartition(
+ UnresolvedTableOrView(visitMultipartIdentifier(ctx.multipartIdentifier)),
Review comment:
ok
##########
File path:
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/AlterTableDropPartitionExec.scala
##########
@@ -0,0 +1,57 @@
+/*
+ * 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 org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.analysis.{NoSuchPartitionsException,
ResolvedPartitionSpec}
+import org.apache.spark.sql.catalyst.expressions.Attribute
+import
org.apache.spark.sql.connector.catalog.{SupportsAtomicPartitionManagement,
SupportsPartitionManagement}
+
+/**
+ * Physical plan node for dropping partitions of table.
+ */
+case class AlterTableDropPartitionExec(
+ table: SupportsPartitionManagement,
+ partSpecs: Seq[ResolvedPartitionSpec],
+ ignoreIfNotExists: Boolean) extends V2CommandExec {
+ import DataSourceV2Implicits._
+
+ override def output: Seq[Attribute] = Seq.empty
+
+ override protected def run(): Seq[InternalRow] = {
+ val (existsPartIdents, notExistsPartIdents) =
+ partSpecs.map(_.spec).partition(table.partitionExists)
+
+ if (notExistsPartIdents.nonEmpty && !ignoreIfNotExists) {
+ throw new NoSuchPartitionsException(
+ table.name(), notExistsPartIdents, table.partitionSchema())
+ }
+
+ existsPartIdents match {
+ case Seq() => // Nothing will be done
+ case Seq(partIdent) =>
+ table.dropPartition(partIdent)
+ case Seq(_ *) if table.isInstanceOf[SupportsAtomicPartitionManagement] =>
Review comment:
ok
##########
File path:
sql/core/src/test/scala/org/apache/spark/sql/connector/DatasourceV2SQLBase.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.connector
+
+import org.apache.spark.sql.{DataFrame, SaveMode}
+import org.apache.spark.sql.connector.catalog.{CatalogPlugin,
CatalogV2Implicits, Identifier, Table}
+import org.apache.spark.sql.internal.SQLConf.V2_SESSION_CATALOG_IMPLEMENTATION
+import org.apache.spark.util.Utils
+
+class DatasourceV2SQLBase
+ extends InsertIntoTests(supportsDynamicOverwrite = true, includeSQLOnlyTests
= true)
+ with AlterTableTests {
+
+ import CatalogV2Implicits._
+
+ protected val v2Source = classOf[FakeV2Provider].getName
+ override protected val v2Format = v2Source
+ override protected val catalogAndNamespace = "testcat.ns1.ns2."
+ protected val defaultUser: String = Utils.getCurrentUserName()
Review comment:
ok
##########
File path:
sql/core/src/test/scala/org/apache/spark/sql/connector/DatasourceV2SQLBase.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.connector
+
+import org.apache.spark.sql.{DataFrame, SaveMode}
+import org.apache.spark.sql.connector.catalog.{CatalogPlugin,
CatalogV2Implicits, Identifier, Table}
+import org.apache.spark.sql.internal.SQLConf.V2_SESSION_CATALOG_IMPLEMENTATION
+import org.apache.spark.util.Utils
+
+class DatasourceV2SQLBase
+ extends InsertIntoTests(supportsDynamicOverwrite = true, includeSQLOnlyTests
= true)
+ with AlterTableTests {
Review comment:
done
##########
File path:
sql/core/src/test/scala/org/apache/spark/sql/connector/DatasourceV2SQLBase.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.connector
+
+import org.apache.spark.sql.{DataFrame, SaveMode}
+import org.apache.spark.sql.connector.catalog.{CatalogPlugin,
CatalogV2Implicits, Identifier, Table}
+import org.apache.spark.sql.internal.SQLConf.V2_SESSION_CATALOG_IMPLEMENTATION
+import org.apache.spark.util.Utils
+
+class DatasourceV2SQLBase
Review comment:
done
----------------------------------------------------------------
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]