cshuo commented on code in PR #14351:
URL: https://github.com/apache/hudi/pull/14351#discussion_r2563730865


##########
hudi-spark-datasource/hudi-spark-common/src/main/scala/org/apache/hudi/cdc/InternalRowToJsonStringConverter.scala:
##########
@@ -36,15 +37,63 @@ class InternalRowToJsonStringConverter(schema: StructType) {
   }
 
   def convert(record: InternalRow): UTF8String = {
-    val map = scala.collection.mutable.Map.empty[String, Any]
+    // Use LinkedHashMap to preserve field order
+    val map = scala.collection.mutable.LinkedHashMap.empty[String, Any]
     schema.zipWithIndex.foreach {
       case (field, idx) =>
-        if (field.dataType.isInstanceOf[StringType]) {
-          map(field.name) = 
Option(record.getUTF8String(idx)).map(_.toString).orNull
-        } else {
-          map(field.name) = record.get(idx, field.dataType)
-        }
+        map(field.name) = convertField(record.get(idx, field.dataType), 
field.dataType)
     }
     UTF8String.fromString(mapper.writeValueAsString(map))
   }
+
+  private def convertField(value: Any, dataType: DataType): Any = {
+    if (value == null) {
+      null
+    } else {
+      dataType match {
+        case StringType => value.toString

Review Comment:
   I think there is only an unit test `TestInternalRowToJsonStringConverter` 
convering the converting logic from internalRow to cdc json string.



##########
hudi-spark-datasource/hudi-spark-common/src/main/scala/org/apache/hudi/cdc/InternalRowToJsonStringConverter.scala:
##########
@@ -36,15 +37,63 @@ class InternalRowToJsonStringConverter(schema: StructType) {
   }
 
   def convert(record: InternalRow): UTF8String = {
-    val map = scala.collection.mutable.Map.empty[String, Any]
+    // Use LinkedHashMap to preserve field order
+    val map = scala.collection.mutable.LinkedHashMap.empty[String, Any]
     schema.zipWithIndex.foreach {
       case (field, idx) =>
-        if (field.dataType.isInstanceOf[StringType]) {
-          map(field.name) = 
Option(record.getUTF8String(idx)).map(_.toString).orNull
-        } else {
-          map(field.name) = record.get(idx, field.dataType)
-        }
+        map(field.name) = convertField(record.get(idx, field.dataType), 
field.dataType)
     }
     UTF8String.fromString(mapper.writeValueAsString(map))
   }
+
+  private def convertField(value: Any, dataType: DataType): Any = {
+    if (value == null) {
+      null
+    } else {
+      dataType match {
+        case StringType => value.toString

Review Comment:
   I think there is only an unit test `TestInternalRowToJsonStringConverter` 
covering the converting logic from internalRow to cdc json string.



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