This is an automated email from the ASF dual-hosted git repository.

wombatukun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hudi.git


The following commit(s) were added to refs/heads/master by this push:
     new 4e11dd21c2a [HUDI-8298] refactor SparkDatasetTestUtils#serializeRow 
(#12620)
4e11dd21c2a is described below

commit 4e11dd21c2a147fc994c966426c35265d73e6711
Author: karthick-de-25 <[email protected]>
AuthorDate: Thu Jan 23 09:07:34 2025 +0530

    [HUDI-8298] refactor SparkDatasetTestUtils#serializeRow (#12620)
---
 .../hudi/testutils/SparkDatasetTestUtils.java       | 18 +++++-------------
 .../hudi/testutils/KeyGeneratorTestUtilities.java   | 21 ++-------------------
 2 files changed, 7 insertions(+), 32 deletions(-)

diff --git 
a/hudi-spark-datasource/hudi-spark-common/src/test/java/org/apache/hudi/testutils/SparkDatasetTestUtils.java
 
b/hudi-spark-datasource/hudi-spark-common/src/test/java/org/apache/hudi/testutils/SparkDatasetTestUtils.java
index f9289b30cd7..fd5c27acdc3 100644
--- 
a/hudi-spark-datasource/hudi-spark-common/src/test/java/org/apache/hudi/testutils/SparkDatasetTestUtils.java
+++ 
b/hudi-spark-datasource/hudi-spark-common/src/test/java/org/apache/hudi/testutils/SparkDatasetTestUtils.java
@@ -29,7 +29,6 @@ import org.apache.hudi.config.HoodieIndexConfig;
 import org.apache.hudi.config.HoodieWriteConfig;
 import org.apache.hudi.index.HoodieIndex;
 
-import org.apache.spark.package$;
 import org.apache.spark.sql.Dataset;
 import org.apache.spark.sql.Row;
 import org.apache.spark.sql.SQLContext;
@@ -237,17 +236,10 @@ public class SparkDatasetTestUtils {
         .withBulkInsertParallelism(2);
   }
 
-  private static InternalRow serializeRow(ExpressionEncoder encoder, Row row)
-      throws InvocationTargetException, IllegalAccessException, 
NoSuchMethodException, ClassNotFoundException {
-    // TODO [HUDI-8298] only spark 3 needed
-    if (package$.MODULE$.SPARK_VERSION().startsWith("2.")) {
-      Method spark2method = encoder.getClass().getMethod("toRow", 
Object.class);
-      return (InternalRow) spark2method.invoke(encoder, row);
-    } else {
-      Class<?> serializerClass = 
Class.forName("org.apache.spark.sql.catalyst.encoders.ExpressionEncoder$Serializer");
-      Object serializer = 
encoder.getClass().getMethod("createSerializer").invoke(encoder);
-      Method aboveSpark2method = serializerClass.getMethod("apply", 
Object.class);
-      return (InternalRow) aboveSpark2method.invoke(serializer, row);
-    }
+  public static InternalRow serializeRow(ExpressionEncoder encoder, Row row)
+      throws InvocationTargetException, IllegalAccessException, 
NoSuchMethodException {
+    Object serializer = encoder.createSerializer();
+    Method applyMethod = serializer.getClass().getMethod("apply", 
Object.class);
+    return (InternalRow) applyMethod.invoke(serializer, row);
   }
 }
diff --git 
a/hudi-spark-datasource/hudi-spark/src/test/java/org/apache/hudi/testutils/KeyGeneratorTestUtilities.java
 
b/hudi-spark-datasource/hudi-spark/src/test/java/org/apache/hudi/testutils/KeyGeneratorTestUtilities.java
index 416d96ef5e9..0738269e8d3 100644
--- 
a/hudi-spark-datasource/hudi-spark/src/test/java/org/apache/hudi/testutils/KeyGeneratorTestUtilities.java
+++ 
b/hudi-spark-datasource/hudi-spark/src/test/java/org/apache/hudi/testutils/KeyGeneratorTestUtilities.java
@@ -24,7 +24,6 @@ import org.apache.hudi.SparkAdapterSupport$;
 import org.apache.avro.Schema;
 import org.apache.avro.generic.GenericData;
 import org.apache.avro.generic.GenericRecord;
-import org.apache.spark.package$;
 import org.apache.spark.sql.Row;
 import org.apache.spark.sql.catalyst.InternalRow;
 import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder;
@@ -32,7 +31,6 @@ import 
org.apache.spark.sql.catalyst.expressions.GenericRowWithSchema;
 import org.apache.spark.sql.types.StructType;
 
 import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
 
 import scala.Function1;
 
@@ -101,21 +99,6 @@ public class KeyGeneratorTestUtilities {
   }
 
   public static InternalRow getInternalRow(Row row, ExpressionEncoder<Row> 
encoder) throws ClassNotFoundException, InvocationTargetException, 
IllegalAccessException, NoSuchMethodException {
-    return serializeRow(encoder, row);
+    return SparkDatasetTestUtils.serializeRow(encoder, row);
   }
-
-  private static InternalRow serializeRow(ExpressionEncoder encoder, Row row)
-      throws InvocationTargetException, IllegalAccessException, 
NoSuchMethodException, ClassNotFoundException {
-    // TODO [HUDI-8298] only spark 3 needed
-    if (package$.MODULE$.SPARK_VERSION().startsWith("2.")) {
-      Method spark2method = encoder.getClass().getMethod("toRow", 
Object.class);
-      return (InternalRow) spark2method.invoke(encoder, row);
-    } else {
-      Class<?> serializerClass = 
Class.forName("org.apache.spark.sql.catalyst.encoders.ExpressionEncoder$Serializer");
-      Object serializer = 
encoder.getClass().getMethod("createSerializer").invoke(encoder);
-      Method aboveSpark2method = serializerClass.getMethod("apply", 
Object.class);
-      return (InternalRow) aboveSpark2method.invoke(serializer, row);
-    }
-  }
-
-}
+}
\ No newline at end of file

Reply via email to