voonhous commented on code in PR #18773:
URL: https://github.com/apache/hudi/pull/18773#discussion_r3267565408
##########
hudi-common/src/main/java/org/apache/hudi/avro/HoodieAvroUtils.java:
##########
@@ -700,6 +721,96 @@ public static Object
convertValueForAvroLogicalTypes(Schema fieldSchema, Object
return fieldValue;
}
+ // The extract* helpers below accept either the Avro 1.11.x primitive form
(Integer / Long) or the
+ // Avro 1.12 java.time form, and return the canonical primitive that Avro
1.11.x would have produced
+ // for the same underlying bytes. See the javadoc on
convertValueForAvroLogicalTypes for context.
+
+ private static long extractEpochDay(Object fieldValue) {
+ if (fieldValue instanceof LocalDate) {
+ return ((LocalDate) fieldValue).toEpochDay();
+ }
+ if (fieldValue instanceof Number) {
+ return ((Number) fieldValue).longValue();
+ }
+ return Long.parseLong(fieldValue.toString());
+ }
+
+ private static long extractEpochMillis(Object fieldValue) {
+ if (fieldValue instanceof Instant) {
+ return ((Instant) fieldValue).toEpochMilli();
+ }
+ if (fieldValue instanceof Number) {
+ return ((Number) fieldValue).longValue();
+ }
+ return Long.parseLong(fieldValue.toString());
+ }
+
+ private static long extractEpochMicros(Object fieldValue) {
+ if (fieldValue instanceof Instant) {
+ Instant instant = (Instant) fieldValue;
+ return Math.addExact(Math.multiplyExact(instant.getEpochSecond(),
1_000_000L), instant.getNano() / 1000L);
+ }
+ if (fieldValue instanceof Number) {
+ return ((Number) fieldValue).longValue();
+ }
+ return Long.parseLong(fieldValue.toString());
+ }
+
+ private static long extractLocalEpochMillis(Object fieldValue) {
+ if (fieldValue instanceof LocalDateTime) {
+ return ((LocalDateTime)
fieldValue).toInstant(ZoneOffset.UTC).toEpochMilli();
+ }
+ if (fieldValue instanceof Number) {
+ return ((Number) fieldValue).longValue();
+ }
+ return Long.parseLong(fieldValue.toString());
+ }
+
+ private static long extractLocalEpochMicros(Object fieldValue) {
+ if (fieldValue instanceof LocalDateTime) {
+ Instant instant = ((LocalDateTime) fieldValue).toInstant(ZoneOffset.UTC);
+ return Math.addExact(Math.multiplyExact(instant.getEpochSecond(),
1_000_000L), instant.getNano() / 1000L);
+ }
+ if (fieldValue instanceof Number) {
+ return ((Number) fieldValue).longValue();
+ }
+ return Long.parseLong(fieldValue.toString());
+ }
+
+ /**
+ * If {@code schema} carries a date / timestamp logical type and {@code
value} is in the Avro 1.12
+ * java.time form, normalize it back to the Avro 1.11.x primitive form
({@link Integer} for date,
+ * {@link Long} for timestamp-*/local-timestamp-*). Used at the entry of
schema-evolution rewrite
+ * paths whose legacy code does unguarded {@code (Integer)} / {@code (Long)}
casts on field
+ * values. For non-logical-type schemas, primitive inputs, or null, this is
a no-op. The on-disk
+ * byte format is unaffected — this is purely about in-memory Java type.
+ */
+ private static Object normalizeToAvro1_11PrimitiveForLogicalType(Object
value, Schema schema) {
+ if (value == null || schema == null) {
+ return value;
+ }
+ org.apache.avro.LogicalType lt = schema.getLogicalType();
Review Comment:
Possible to import this instead of using FQN?
Wonder if there's a checkstyle check for this.
--
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]