zhongyujiang commented on code in PR #5531:
URL: https://github.com/apache/paimon/pull/5531#discussion_r2062628159


##########
paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/SparkV2Write.scala:
##########
@@ -0,0 +1,180 @@
+/*
+ * 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.paimon.spark.source
+
+import org.apache.paimon.CoreOptions
+import org.apache.paimon.spark.{SparkInternalRowWrapper, SparkUtils, 
SparkWriteRequirement}
+import org.apache.paimon.table.FileStoreTable
+import org.apache.paimon.table.sink.{BatchTableWrite, BatchWriteBuilder, 
CommitMessage, CommitMessageSerializer}
+
+import org.apache.spark.internal.Logging
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.connector.distributions.Distribution
+import org.apache.spark.sql.connector.expressions.SortOrder
+import org.apache.spark.sql.connector.write._
+import org.apache.spark.sql.types.StructType
+
+import java.io.{IOException, UncheckedIOException}
+
+import scala.jdk.CollectionConverters._
+import scala.util.{Failure, Success, Try}
+
+class SparkV2Write(
+    storeTable: FileStoreTable,
+    overwriteDynamic: Boolean,
+    overwritePartitions: Map[String, String],
+    writeSchema: StructType
+) extends Write
+  with RequiresDistributionAndOrdering
+  with Logging {
+
+  assert(
+    !(overwriteDynamic && overwritePartitions != null),
+    "Cannot overwrite dynamically and by filter both")
+
+  private val table =
+    storeTable.copy(
+      Map(CoreOptions.DYNAMIC_PARTITION_OVERWRITE.key -> 
overwriteDynamic.toString).asJava)
+
+  private val batchWriteBuilder = {
+    val builder = table.newBatchWriteBuilder()
+    Option(overwritePartitions).foreach(partitions => 
builder.withOverwrite(partitions.asJava))
+    builder
+  }
+
+  private val writeRequirement = SparkWriteRequirement(table)
+
+  override def requiredDistribution(): Distribution = {
+    val distribution = writeRequirement.distribution
+    log.info(s"Requesting $distribution as write distribution for table 
${table.name()}")
+    distribution
+  }
+
+  override def requiredOrdering(): Array[SortOrder] = {
+    val ordering = writeRequirement.ordering
+    log.info(s"Requesting ${ordering.mkString(",")} as write ordering for 
table ${table.name()}")
+    ordering
+  }
+
+  override def toBatch: BatchWrite = new PaimonBatchWrite
+
+  override def toString: String =
+    if (overwriteDynamic)
+      s"PaimonWrite(table=${table.fullName()}, overwriteDynamic=true)"
+    else
+      s"PaimonWrite(table=${table.fullName()}, 
overwritePartitions=$overwritePartitions)"
+
+  private class PaimonBatchWrite extends BatchWrite {
+    override def createBatchWriterFactory(info: PhysicalWriteInfo): 
DataWriterFactory =
+      WriterFactory(writeSchema, batchWriteBuilder)
+
+    override def useCommitCoordinator(): Boolean = false
+
+    override def commit(messages: Array[WriterCommitMessage]): Unit = {
+      log.info(s"Committing to table ${table.name()}")
+      val batchTableCommit = batchWriteBuilder.newCommit()
+
+      val commitMessages =
+        messages.filter(_ != 
null).map(_.asInstanceOf[TaskCommit].commitMessages).flatten.toList

Review Comment:
   Fixed.



-- 
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]

Reply via email to