seokyun-ha-toss commented on code in PR #15283:
URL: https://github.com/apache/iceberg/pull/15283#discussion_r3021061365
##########
kafka-connect/kafka-connect/src/main/java/org/apache/iceberg/connect/data/RecordConverter.java:
##########
@@ -464,6 +479,171 @@ protected Temporal convertTimestampValue(Object value,
TimestampType type) {
return convertLocalDateTime(value);
}
+ protected Variant convertVariantValue(Object value) {
+ if (value instanceof ByteBuffer) {
+ return Variant.from((ByteBuffer) value);
+ }
+
+ Set<String> fieldNames = Sets.newHashSet();
+ collectFieldNames(value, fieldNames);
+ List<String> allFieldNames =
fieldNames.stream().sorted().collect(Collectors.toList());
+ VariantMetadata metadata = Variants.metadata(allFieldNames);
+ VariantValue variantValue = objectToVariantValue(value, metadata);
+ return Variant.of(metadata, variantValue);
+ }
+
+ /**
+ * Collects all field names (map keys) from the entire object tree into the
given set. Used to
+ * build a single VariantMetadata for the whole Variant (required for nested
maps).
+ */
+ private static void collectFieldNames(Object value, Set<String> names) {
+ if (value == null) {
+ return;
+ }
+ if (value instanceof Collection) {
+ for (Object element : (Collection<?>) value) {
+ collectFieldNames(element, names);
+ }
+ return;
+ }
+ if (value instanceof Map) {
+ Map<?, ?> map = (Map<?, ?>) value;
+ for (Map.Entry<?, ?> entry : map.entrySet()) {
+ Object key = entry.getKey();
+ if (key != null && key instanceof String) {
+ names.add((String) key);
+ collectFieldNames(entry.getValue(), names);
+ }
+ }
+ return;
+ }
+ if (value instanceof Struct) {
+ Struct struct = (Struct) value;
+ for (Field field : struct.schema().fields()) {
+ names.add(field.name());
+ collectFieldNames(struct.get(field), names);
+ }
+ }
+ }
+
+ /**
+ * 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();
+ }
+ VariantValue primitive = primitiveToVariantValue(value);
+ if (primitive != null) {
+ return primitive;
+ }
+ 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(
+ (key, val) -> {
+ if (key != null && key instanceof String) {
+ object.put((String) key, objectToVariantValue(val, metadata));
+ }
+ });
+ return object;
+ }
+ if (value instanceof Struct) {
+ Struct struct = (Struct) value;
+ ShreddedObject object = Variants.object(metadata);
+ for (Field field : struct.schema().fields()) {
+ object.put(field.name(), objectToVariantValue(struct.get(field),
metadata));
+ }
+ return object;
+ }
+ throw new IllegalArgumentException("Cannot convert to variant: " +
value.getClass().getName());
+ }
+
+ /**
+ * Converts a primitive or primitive-like value to VariantValue; returns
null if not supported.
+ */
+ private static VariantValue primitiveToVariantValue(Object value) {
+ if (value instanceof Boolean) {
+ return Variants.of((Boolean) value);
+ }
+ if (value instanceof Instant) {
+ return Variants.ofTimestamptz(DateTimeUtil.microsFromInstant((Instant)
value));
+ }
+ if (value instanceof OffsetDateTime) {
+ return
Variants.ofTimestamptz(DateTimeUtil.microsFromTimestamptz((OffsetDateTime)
value));
+ }
+ if (value instanceof ZonedDateTime) {
+ return Variants.ofTimestamptz(
+ DateTimeUtil.microsFromTimestamptz(((ZonedDateTime)
value).toOffsetDateTime()));
+ }
+ if (value instanceof LocalDateTime) {
+ return
Variants.ofTimestampntz(DateTimeUtil.microsFromTimestamp((LocalDateTime)
value));
+ }
+ if (value instanceof LocalDate) {
+ return Variants.ofDate(DateTimeUtil.daysFromDate((LocalDate) value));
+ }
+ if (value instanceof LocalTime) {
+ return Variants.ofTime(DateTimeUtil.microsFromTime((LocalTime) value));
+ }
+ if (value instanceof Date) {
+ int days = (int) (((Date) value).getTime() / 1000 / 60 / 60 / 24);
Review Comment:
Wow, really appreciate it @brandonstanleyappfolio , Thanks! I'll take a
look!!
Sadly, I don't have much time to work on in these days.... I'll resume this
work as soon as possible. Thanks for guys! 🙏
--
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]