danielcweeks commented on code in PR #15498:
URL: https://github.com/apache/iceberg/pull/15498#discussion_r2921185465


##########
kafka-connect/kafka-connect/src/main/java/org/apache/iceberg/connect/data/RecordConverter.java:
##########
@@ -531,4 +537,62 @@ private String ensureTimestampFormat(String str) {
     }
     return result;
   }
+
+  private Variant tryDeserializeVariant(Object value) {
+    // Try to deserialize ByteBuffer or byte[] as a serialized variant
+    // Returns null if not a valid serialized variant
+    try {
+      if (value instanceof ByteBuffer) {
+        return Variant.from((ByteBuffer) value);
+      } else if (value instanceof byte[]) {
+        return Variant.from(ByteBuffer.wrap((byte[]) value));
+      }
+    } catch (RuntimeException e) {
+      // Not a valid serialized variant, return null to fall back to JSON 
conversion
+    }
+    return null;
+  }
+
+  protected Variant convertVariant(Object value) {
+    try {
+      // If it's already a Variant, return it
+      if (value instanceof Variant) {
+        return (Variant) value;
+      }
+
+      // Try to deserialize if it's a ByteBuffer or byte[]
+      Variant deserialized = tryDeserializeVariant(value);
+      if (deserialized != null) {
+        return deserialized;
+      }
+
+      // Convert the value to JSON representation and wrap in a Variant
+      String jsonString;
+      if (value instanceof String) {
+        jsonString = (String) value;
+      } else if (value instanceof Map || value instanceof List) {
+        jsonString = MAPPER.writeValueAsString(value);
+      } else if (value instanceof Struct) {
+        Struct struct = (Struct) value;
+        byte[] data = config.jsonConverter().fromConnectData(null, 
struct.schema(), struct);
+        jsonString = new String(data, StandardCharsets.UTF_8);
+      } else if (value instanceof Number || value instanceof Boolean) {
+        // For primitives, convert to JSON representation
+        jsonString = value.toString();

Review Comment:
   I don't think we want to treat all primitives this way.  We should handle 
each of the primitive types appropriately and convert to the correct variant 
primitive type.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to