[GitHub] [hudi] wecharyu commented on a diff in pull request #9408: [HUDI-6671] Support 'alter table add partition' sql

2023-08-16 Thread via GitHub


wecharyu commented on code in PR #9408:
URL: https://github.com/apache/hudi/pull/9408#discussion_r1296644197


##
hudi-spark-datasource/hudi-spark-common/src/main/scala/org/apache/spark/sql/hudi/command/AlterHoodieTableAddPartitionCommand.scala:
##
@@ -0,0 +1,88 @@
+/*
+ * 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.hudi.command
+
+import org.apache.hadoop.fs.Path
+import org.apache.hudi.common.fs.FSUtils
+import org.apache.hudi.common.model.HoodiePartitionMetadata
+import org.apache.hudi.common.table.timeline.HoodieActiveTimeline
+import org.apache.spark.sql.{AnalysisException, Row, SparkSession}
+import org.apache.spark.sql.catalyst.TableIdentifier
+import org.apache.spark.sql.catalyst.catalog.CatalogTypes.TablePartitionSpec
+import org.apache.spark.sql.catalyst.catalog.{CatalogTablePartition, 
HoodieCatalogTable}
+import org.apache.spark.sql.execution.command.DDLUtils
+import org.apache.spark.sql.hudi.HoodieSqlCommonUtils.{makePartitionPath, 
normalizePartitionSpec}
+
+case class AlterHoodieTableAddPartitionCommand(
+   tableIdentifier: TableIdentifier,
+   partitionSpecsAndLocs: Seq[(TablePartitionSpec, Option[String])],
+   ifNotExists: Boolean)
+  extends HoodieLeafRunnableCommand {
+
+  override def run(sparkSession: SparkSession): Seq[Row] = {
+val fullTableName = s"${tableIdentifier.database}.${tableIdentifier.table}"
+logInfo(s"start execute alter table add partition command for 
$fullTableName")
+
+val hoodieCatalogTable = HoodieCatalogTable(sparkSession, tableIdentifier)
+
+if (!hoodieCatalogTable.isPartitionedTable) {
+  throw new AnalysisException(s"$fullTableName is a non-partitioned table 
that is not allowed to add partition")
+}
+
+val catalog = sparkSession.sessionState.catalog
+val table = hoodieCatalogTable.table
+DDLUtils.verifyAlterTableType(catalog, table, isView = false)
+
+val normalizedSpecs: Seq[Map[String, String]] = partitionSpecsAndLocs.map 
{ case (spec, location) =>
+  if (location.isDefined) {
+throw new AnalysisException(s"Hoodie table does not support specify 
partition location explicitly")
+  }
+  normalizePartitionSpec(
+spec,
+hoodieCatalogTable.partitionFields,
+hoodieCatalogTable.tableName,
+sparkSession.sessionState.conf.resolver)
+}
+
+val basePath = new Path(hoodieCatalogTable.tableLocation)
+val fileSystem = hoodieCatalogTable.metaClient.getFs
+val instantTime = HoodieActiveTimeline.createNewInstantTime
+val format = hoodieCatalogTable.tableConfig.getPartitionMetafileFormat
+val (partitionMetadata, parts) = normalizedSpecs.map { spec =>
+  val partitionPath = makePartitionPath(hoodieCatalogTable, spec)
+  val fullPartitionPath: Path = FSUtils.getPartitionPath(basePath, 
partitionPath)
+  val metadata = if 
(HoodiePartitionMetadata.hasPartitionMetadata(fileSystem, fullPartitionPath)) {
+if (!ifNotExists) {
+  throw new AnalysisException(s"Partition metadata already exists for 
path: $fullPartitionPath")
+}
+None
+  } else Some(new HoodiePartitionMetadata(fileSystem, instantTime, 
basePath, fullPartitionPath, format))
+  (metadata, CatalogTablePartition(spec, table.storage.copy(locationUri = 
Some(fullPartitionPath.toUri
+}.unzip
+partitionMetadata.flatten.foreach(_.trySave(0))
+
+// Sync new partitions in batch, enable ignoreIfExists to avoid sync 
failure.
+val batchSize = 
sparkSession.sparkContext.conf.getInt("spark.sql.addPartitionInBatch.size", 100)
+parts.toIterator.grouped(batchSize).foreach { batch =>

Review Comment:
   It makes sense to add a warning log, otherwise the exception will be 
displayed in client while we succeed add partition folder actually.



-- 
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: commits-unsubscr...@hudi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



[GitHub] [hudi] wecharyu commented on a diff in pull request #9408: [HUDI-6671] Support 'alter table add partition' sql

2023-08-10 Thread via GitHub


wecharyu commented on code in PR #9408:
URL: https://github.com/apache/hudi/pull/9408#discussion_r1290337578


##
hudi-spark-datasource/hudi-spark-common/src/main/scala/org/apache/spark/sql/hudi/command/AlterHoodieTableAddPartitionCommand.scala:
##
@@ -0,0 +1,88 @@
+/*
+ * 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.hudi.command
+
+import org.apache.hadoop.fs.Path
+import org.apache.hudi.common.fs.FSUtils
+import org.apache.hudi.common.model.HoodiePartitionMetadata
+import org.apache.hudi.common.table.timeline.HoodieActiveTimeline
+import org.apache.spark.sql.{AnalysisException, Row, SparkSession}
+import org.apache.spark.sql.catalyst.TableIdentifier
+import org.apache.spark.sql.catalyst.catalog.CatalogTypes.TablePartitionSpec
+import org.apache.spark.sql.catalyst.catalog.{CatalogTablePartition, 
HoodieCatalogTable}
+import org.apache.spark.sql.execution.command.DDLUtils
+import org.apache.spark.sql.hudi.HoodieSqlCommonUtils.{makePartitionPath, 
normalizePartitionSpec}
+
+case class AlterHoodieTableAddPartitionCommand(
+   tableIdentifier: TableIdentifier,
+   partitionSpecsAndLocs: Seq[(TablePartitionSpec, Option[String])],
+   ifNotExists: Boolean)
+  extends HoodieLeafRunnableCommand {
+
+  override def run(sparkSession: SparkSession): Seq[Row] = {
+val fullTableName = s"${tableIdentifier.database}.${tableIdentifier.table}"
+logInfo(s"start execute alter table add partition command for 
$fullTableName")
+

Review Comment:
   Spark analysis will throw exception if table does not exist, so we do not 
need check it here.



-- 
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: commits-unsubscr...@hudi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



[GitHub] [hudi] wecharyu commented on a diff in pull request #9408: [HUDI-6671] Support 'alter table add partition' sql

2023-08-09 Thread via GitHub


wecharyu commented on code in PR #9408:
URL: https://github.com/apache/hudi/pull/9408#discussion_r1289521158


##
hudi-spark-datasource/hudi-spark-common/src/main/scala/org/apache/spark/sql/hudi/HoodieSqlCommonUtils.scala:
##
@@ -330,23 +330,36 @@ object HoodieSqlCommonUtils extends SparkAdapterSupport {
 val allPartitionPaths = hoodieCatalogTable.getPartitionPaths
 val enableHiveStylePartitioning = 
isHiveStyledPartitioning(allPartitionPaths, table)
 val enableEncodeUrl = isUrlEncodeEnabled(allPartitionPaths, table)
-val partitionsToDrop = normalizedSpecs.map { spec =>
-  hoodieCatalogTable.partitionFields.map { partitionColumn =>
-val encodedPartitionValue = if (enableEncodeUrl) {
-  PartitionPathEncodeUtils.escapePathName(spec(partitionColumn))
-} else {
-  spec(partitionColumn)
-}
-if (enableHiveStylePartitioning) {
-  partitionColumn + "=" + encodedPartitionValue
-} else {

Review Comment:
   Not remove it, just reuse the common implementation of `makePartitionPath` 
which will handle hive style and url encode.



-- 
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: commits-unsubscr...@hudi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org