emkornfield commented on code in PR #15283:
URL: https://github.com/apache/iceberg/pull/15283#discussion_r2790113172
##########
kafka-connect/kafka-connect/src/main/java/org/apache/iceberg/connect/data/RecordConverter.java:
##########
@@ -464,6 +474,119 @@ protected Temporal convertTimestampValue(Object value,
TimestampType type) {
return convertLocalDateTime(value);
}
+ protected Variant convertVariantValue(Object value) {
+ if (value instanceof Variant) {
+ return (Variant) value;
+ } else if (value instanceof ByteBuffer) {
+ return Variant.from((ByteBuffer) value);
+ }
+
+ List<String> allFieldNames =
+
collectFieldNames(value).stream().distinct().sorted().collect(Collectors.toList());
+ VariantMetadata metadata =
+ allFieldNames.isEmpty() ? Variants.emptyMetadata() :
Variants.metadata(allFieldNames);
+ VariantValue variantValue = objectToVariantValue(value, metadata);
+ return Variant.of(metadata, variantValue);
+ }
+
+ /**
+ * Collects all field names (map keys) from the entire object tree. Used to
build a single
+ * VariantMetadata for the whole Variant (required for nested maps).
+ */
+ private static List<String> collectFieldNames(Object value) {
+ if (value == null) {
+ return Lists.newArrayList();
+ }
+ if (value instanceof Map) {
+ Map<?, ?> map = (Map<?, ?>) value;
+ List<String> names = Lists.newArrayList();
+ map.keySet().stream().map(Object::toString).forEach(names::add);
+ for (Object v : map.values()) {
+ names.addAll(collectFieldNames(v));
+ }
+ return names;
+ }
+ if (value instanceof Collection) {
+ List<String> names = Lists.newArrayList();
+ for (Object element : (Collection<?>) value) {
+ names.addAll(collectFieldNames(element));
+ }
+ return names;
+ }
+ return Lists.newArrayList();
+ }
+
+ /**
+ * Recursively converts a Java object to a VariantValue using the given
shared metadata for all
+ * nested maps. Handles primitives, List (array), and Map (object); map keys
become field names.
+ */
+ private static VariantValue objectToVariantValue(Object value,
VariantMetadata metadata) {
+ if (value == null) {
+ return Variants.ofNull();
+ }
+ if (value instanceof Boolean) {
+ return Variants.of((Boolean) value);
+ }
+ if (value instanceof Number) {
+ return numberToVariantValue((Number) value);
+ }
+ if (value instanceof String) {
+ return Variants.of((String) value);
+ }
+ if (value instanceof ByteBuffer) {
+ return Variants.of((ByteBuffer) value);
+ }
+ if (value instanceof byte[]) {
+ return Variants.of(ByteBuffer.wrap((byte[]) value));
+ }
+ if (value instanceof BigDecimal) {
+ return Variants.of((BigDecimal) value);
+ }
+ if (value instanceof UUID) {
+ return Variants.ofUUID((UUID) value);
+ }
+ if (value instanceof Collection) {
+ ValueArray array = Variants.array();
+ for (Object element : (Collection<?>) value) {
+ array.add(objectToVariantValue(element, metadata));
+ }
+ return array;
+ }
+ if (value instanceof Map) {
+ Map<?, ?> map = (Map<?, ?>) value;
+ ShreddedObject object = Variants.object(metadata);
+ map.forEach((k, v) -> object.put(k.toString(), objectToVariantValue(v,
metadata)));
+ return object;
+ }
+ throw new IllegalArgumentException("Cannot convert to variant: " +
value.getClass().getName());
+ }
+
+ private static VariantValue numberToVariantValue(Number value) {
+ if (value instanceof Integer) {
+ return Variants.of((Integer) value);
+ }
+ if (value instanceof Long) {
+ return Variants.of((Long) value);
+ }
+ if (value instanceof Float) {
+ return Variants.of((Float) value);
+ }
+ if (value instanceof Double) {
+ return Variants.of((Double) value);
+ }
+ if (value instanceof Byte) {
+ return Variants.of((Byte) value);
+ }
+ if (value instanceof Short) {
+ return Variants.of((Short) value);
+ }
+ Number num = value;
+ if (num.doubleValue() == num.longValue()) {
+ return Variants.of(num.longValue());
+ }
+ return Variants.of(num.doubleValue());
Review Comment:
Or we should convert it to BigDecimal.
--
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]