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


##########
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
+
+      try {
+        val start = System.currentTimeMillis()
+        batchTableCommit.commit(commitMessages.asJava)
+        log.info(s"Committed in ${System.currentTimeMillis() - start} ms")
+      } finally {
+        batchTableCommit.close()
+      }
+    }
+
+    override def abort(messages: Array[WriterCommitMessage]): Unit = {
+      // TODO clean uncommitted files
+    }
+  }
+}
+
+private case class WriterFactory(writeSchema: StructType, batchWriteBuilder: 
BatchWriteBuilder)
+  extends DataWriterFactory {
+
+  override def createWriter(partitionId: Int, taskId: Long): 
DataWriter[InternalRow] = {
+    val batchTableWrite = batchWriteBuilder.newWrite()
+    new PaimonDataWriter(batchTableWrite, writeSchema)
+  }
+}
+
+private class PaimonDataWriter(batchTableWrite: BatchTableWrite, writeSchema: 
StructType)
+  extends DataWriter[InternalRow] {
+
+  private val ioManager = SparkUtils.createIOManager()
+  batchTableWrite.withIOManager(ioManager)
+
+  override def write(record: InternalRow): Unit = {
+    val wrappedRow = new SparkInternalRowWrapper(record, -1, writeSchema, 
writeSchema.fields.length)
+    batchTableWrite.write(wrappedRow)
+  }
+
+  override def commit(): WriterCommitMessage = {
+    try {
+      val commitMessages = batchTableWrite.prepareCommit().asScala.toList
+      TaskCommit(commitMessages)
+    } finally {
+      close()
+    }
+  }
+
+  override def abort(): Unit = close()
+
+  override def close(): Unit = {
+    try {
+      batchTableWrite.close()
+      ioManager.close()
+    } catch {
+      case e: Exception => throw new RuntimeException(e)
+    }
+  }
+}
+
+private case class TaskCommit(commitMessagesList: List[CommitMessage]) extends 
WriterCommitMessage {
+  private val serializedMessageList: List[Array[Byte]] = {
+    Option(commitMessagesList).filter(_.nonEmpty) match {
+      case Some(messages) =>
+        val serializer = new CommitMessageSerializer()
+        messages.map {
+          msg =>
+            Try(serializer.serialize(msg)) match {
+              case Success(serializedBytes) => serializedBytes
+              case Failure(e: IOException) => throw new UncheckedIOException(e)
+              case Failure(e) => throw e
+            }
+        }
+      case None => Nil
+    }
+  }
+
+  def commitMessages(): List[CommitMessage] = {
+    val deserializer = new CommitMessageSerializer()
+    serializedMessageList.map {

Review Comment:
   `CommitMessage` has already implemented the `Serializable` interface, so 
this piece of code can indeed be confusing. Added a comment in the code for 
explanation to facilitate the review.



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