Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@
* LogicalTypes.Date <-----> LogicalType(DATE)
* <------ LogicalType(urn="beam:logical_type:date:v1")
* LogicalTypes.TimestampMillis <-----> DATETIME
* LogicalTypes.TimestampMicros ------> Long
* LogicalTypes.TimestampMicros <------ LogicalType(urn="beam:logical_type:micros_instant:v1")
* LogicalTypes.Decimal <-----> DECIMAL
* </pre>
*
Expand Down Expand Up @@ -1179,6 +1181,9 @@ private static org.apache.avro.Schema getFieldSchema(
baseType = LogicalTypes.date().addToSchema(org.apache.avro.Schema.create(Type.INT));
} else if ("TIME".equals(identifier)) {
baseType = LogicalTypes.timeMillis().addToSchema(org.apache.avro.Schema.create(Type.INT));
} else if (SqlTypes.TIMESTAMP.getIdentifier().equals(identifier)) {
baseType =
LogicalTypes.timestampMicros().addToSchema(org.apache.avro.Schema.create(Type.LONG));
} else {
throw new RuntimeException(
"Unhandled logical type " + checkNotNull(fieldType.getLogicalType()).getIdentifier());
Expand Down Expand Up @@ -1315,6 +1320,10 @@ private static org.apache.avro.Schema getFieldSchema(
return ((java.time.LocalDate) value).toEpochDay();
} else if ("TIME".equals(identifier)) {
return (int) ((Instant) value).getMillis();
} else if (SqlTypes.TIMESTAMP.getIdentifier().equals(identifier)) {
java.time.Instant instant = (java.time.Instant) value;
return TimeUnit.SECONDS.toMicros(instant.getEpochSecond())
+ TimeUnit.NANOSECONDS.toMicros(instant.getNano());
} else {
throw new RuntimeException("Unhandled logical type " + identifier);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.apache.avro.Conversions;
import org.apache.avro.LogicalType;
import org.apache.avro.LogicalTypes;
Expand Down Expand Up @@ -1038,6 +1039,39 @@ public void testAvroBytesToRowAndRowToAvroBytesFunctions() {
assertEquals(row, deserializedRow);
}

@Test
public void testBeamTimestampLogicalTypeToAvro() {
// Tests special handling for Beam's MicrosInstant logical type
// Only one way (Beam to Avro)

Schema beamSchema =
Schema.builder().addLogicalTypeField("timestampMicrosLT", SqlTypes.TIMESTAMP).build();
List<org.apache.avro.Schema.Field> fields = Lists.newArrayList();
fields.add(
new org.apache.avro.Schema.Field(
"timestampMicrosLT",
LogicalTypes.timestampMicros().addToSchema(org.apache.avro.Schema.create(Type.LONG)),
"",
(Object) null));
org.apache.avro.Schema avroSchema =
org.apache.avro.Schema.createRecord("topLevelRecord", null, null, false, fields);

assertEquals(avroSchema, AvroUtils.toAvroSchema(beamSchema));

java.time.Instant instant =
java.time.Instant.ofEpochMilli(DATE_TIME.getMillis()).plusNanos(123000);
Row beamRow = Row.withSchema(beamSchema).addValue(instant).build();
GenericRecord avroRecord =
new GenericRecordBuilder(avroSchema)
.set(
"timestampMicrosLT",
TimeUnit.SECONDS.toMicros(instant.getEpochSecond())
+ TimeUnit.NANOSECONDS.toMicros(instant.getNano()))
.build();

assertEquals(avroRecord, AvroUtils.toGenericRecord(beamRow));
}

@Test
public void testNullSchemas() {
assertEquals(
Expand Down
Loading