You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@paimon.apache.org by "Zouxxyy (via GitHub)" <gi...@apache.org> on 2025/03/10 16:03:10 UTC

Re: [PR] [spark] Eliminate the De/serialization process when writing to the ap… [paimon]

Zouxxyy commented on code in PR #5159:
URL: https://github.com/apache/paimon/pull/5159#discussion_r1987487366


##########
paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/SparkTableWrite.scala:
##########
@@ -67,8 +73,12 @@ class SparkTableWrite(writeBuilder: BatchWriteBuilder, rowType: RowType, rowKind
     ioManager.close()
   }
 
-  private def toPaimonRow(row: Row) =
-    new SparkRow(rowType, row, SparkRowUtils.getRowKind(row, rowKindColIdx))
+  private def toPaimonRow(row: InternalRow) =
+    new SparkInternalRowWrapper(

Review Comment:
   We can use like this to reduce the cost of initialization.
   ```
     SparkInternalRowWrapper wrap(row internalRow) {
       this.row = internalRow;
       return this;
     }
   ```



##########
paimon-spark/paimon-spark-common/src/main/java/org/apache/paimon/spark/SparkInternalRowWrapper.java:
##########
@@ -0,0 +1,361 @@
+/*
+ * 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;
+
+import org.apache.paimon.data.BinaryString;
+import org.apache.paimon.data.Decimal;
+import org.apache.paimon.data.InternalArray;
+import org.apache.paimon.data.InternalMap;
+import org.apache.paimon.data.InternalRow;
+import org.apache.paimon.data.Timestamp;
+import org.apache.paimon.data.variant.Variant;
+import org.apache.paimon.spark.util.shim.TypeUtils$;
+import org.apache.paimon.types.RowKind;
+
+import org.apache.spark.sql.catalyst.util.ArrayData;
+import org.apache.spark.sql.catalyst.util.DateTimeUtils;
+import org.apache.spark.sql.catalyst.util.MapData;
+import org.apache.spark.sql.paimon.shims.SparkShimLoader;
+import org.apache.spark.sql.types.ArrayType;
+import org.apache.spark.sql.types.DataType;
+import org.apache.spark.sql.types.MapType;
+import org.apache.spark.sql.types.StructType;
+import org.apache.spark.sql.types.TimestampNTZType;
+import org.apache.spark.sql.types.TimestampType;
+
+import java.math.BigDecimal;
+
+/** Wrapper to fetch value from the spark internal row. */
+public class SparkInternalRowWrapper implements InternalRow {
+
+    private final org.apache.spark.sql.catalyst.InternalRow internalRow;
+    private final RowKind rowKind;
+    private final int length;
+    private final StructType structType;
+
+    public SparkInternalRowWrapper(

Review Comment:
   We can only keep these in `SparkInternalRowWrapper`
   ```
   private org.apache.spark.sql.catalyst.InternalRow internalRow;
   private final StructType structType; // or paimon rowType
   private final rowKindColIdx
   ```
   
   and get `FieldCount` or `RowKind` by static method



##########
paimon-spark/paimon-spark-3.2/src/main/scala/org/apache/paimon/spark/catalyst/Compatibility.scala:
##########
@@ -50,4 +52,8 @@ object Compatibility {
       ansiEnabled: Boolean = SQLConf.get.ansiEnabled): Cast = {
     Cast(child, dataType, timeZoneId, ansiEnabled)
   }
+
+  def callFunction(name: String, args: Seq[Column]): Column = {
+    call_udf(name, args: _*)

Review Comment:
   What is the difference between this and the `call_function` in Spark 3.5+. It seems that new Compatibility have been added for it.



##########
paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/commands/BucketExpression.scala:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.commands
+
+import org.apache.paimon.data.serializer.InternalRowSerializer
+import org.apache.paimon.spark.SparkInternalRowWrapper
+import org.apache.paimon.spark.SparkTypeUtils.toPaimonType
+import org.apache.paimon.table.sink.KeyAndBucketExtractor.{bucket, bucketKeyHashCode}
+import org.apache.paimon.types.{RowKind, RowType}
+
+import org.apache.spark.sql.catalyst.{FunctionIdentifier, InternalRow => SparkInternalRow}
+import org.apache.spark.sql.catalyst.analysis.FunctionRegistry.FunctionBuilder
+import org.apache.spark.sql.catalyst.analysis.FunctionRegistryBase
+import org.apache.spark.sql.catalyst.expressions.{Expression, ExpressionInfo, Literal}
+import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback
+import org.apache.spark.sql.types.{DataType, DataTypes, StructField, StructType}
+
+case class FixedBucketExpression(_children: Seq[Expression])

Review Comment:
   Let's add some comments to the args



##########
paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/commands/PaimonSparkWriter.scala:
##########
@@ -86,33 +91,43 @@ case class PaimonSparkWriter(table: FileStoreTable) {
     }
 
     def writeWithoutBucket(dataFrame: DataFrame): Dataset[Array[Byte]] = {
-      dataFrame.mapPartitions {
-        iter =>
-          {
-            val write = newWrite()
-            try {
-              iter.foreach(row => write.write(row))
-              write.finish()
-            } finally {
-              write.close()
+      val schema = dataFrame.schema
+      dataFrame.queryExecution.toRdd

Review Comment:
   Have you tested the performance difference with this modification for unaware bucket scenario



-- 
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: issues-unsubscribe@paimon.apache.org

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