-
Notifications
You must be signed in to change notification settings - Fork 28.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-32512][SQL] add alter table add/drop partition command for dat…
…asourcev2 ### What changes were proposed in this pull request? This patch is trying to add `AlterTableAddPartitionExec` and `AlterTableDropPartitionExec` with the new table partition API, defined in #28617. ### Does this PR introduce _any_ user-facing change? Yes. User can use `alter table add partition` or `alter table drop partition` to create/drop partition in V2Table. ### How was this patch tested? Run suites and fix old tests. Closes #29339 from stczwd/SPARK-32512-new. Lead-authored-by: stczwd <qcsd2011@163.com> Co-authored-by: Jacky Lee <qcsd2011@163.com> Co-authored-by: Jackey Lee <qcsd2011@163.com> Signed-off-by: Wenchen Fan <wenchen@databricks.com>
- Loading branch information
1 parent
8760032
commit 1eb236b
Showing
19 changed files
with
670 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolvePartitionSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* 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.AnalysisException | ||
import org.apache.spark.sql.catalyst.InternalRow | ||
import org.apache.spark.sql.catalyst.catalog.CatalogTypes.TablePartitionSpec | ||
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.types._ | ||
|
||
/** | ||
* Resolve [[UnresolvedPartitionSpec]] to [[ResolvedPartitionSpec]] in partition related commands. | ||
*/ | ||
object ResolvePartitionSpec extends Rule[LogicalPlan] { | ||
|
||
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())) | ||
} | ||
|
||
private def resolvePartitionSpecs( | ||
partSpecs: Seq[PartitionSpec], partSchema: StructType): Seq[ResolvedPartitionSpec] = | ||
partSpecs.map { | ||
case unresolvedPartSpec: UnresolvedPartitionSpec => | ||
ResolvedPartitionSpec( | ||
convertToPartIdent(unresolvedPartSpec.spec, partSchema), unresolvedPartSpec.location) | ||
case resolvedPartitionSpec: ResolvedPartitionSpec => | ||
resolvedPartitionSpec | ||
} | ||
|
||
private def convertToPartIdent( | ||
partSpec: TablePartitionSpec, partSchema: StructType): InternalRow = { | ||
val conflictKeys = partSpec.keys.toSeq.diff(partSchema.map(_.name)) | ||
if (conflictKeys.nonEmpty) { | ||
throw new AnalysisException(s"Partition key ${conflictKeys.mkString(",")} not exists") | ||
} | ||
|
||
val partValues = partSchema.map { part => | ||
val partValue = partSpec.get(part.name).orNull | ||
if (partValue == null) { | ||
null | ||
} else { | ||
// TODO: Support other datatypes, such as DateType | ||
part.dataType match { | ||
case _: ByteType => | ||
partValue.toByte | ||
case _: ShortType => | ||
partValue.toShort | ||
case _: IntegerType => | ||
partValue.toInt | ||
case _: LongType => | ||
partValue.toLong | ||
case _: FloatType => | ||
partValue.toFloat | ||
case _: DoubleType => | ||
partValue.toDouble | ||
case _: StringType => | ||
partValue | ||
case _ => | ||
throw new AnalysisException( | ||
s"Type ${part.dataType.typeName} is not supported for partition.") | ||
} | ||
} | ||
} | ||
InternalRow.fromSeq(partValues) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.