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 @@ -247,6 +247,13 @@ message LogicalTypes {
// e.g. -1.5s at precision 6 is {seconds: -2, subseconds: 500000}.
TIMESTAMP = 9 [(org.apache.beam.model.pipeline.v1.beam_urn) =
"beam:logical_type:timestamp:v1"];

// A URN for Time type
// - Representation type: INT64
// - A time without a timezone, represented by the number of
// nanoseconds since midnight.
TIME = 10 [(org.apache.beam.model.pipeline.v1.beam_urn) =
"beam:logical_type:time:v1"];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.apache.beam.sdk.schemas.logicaltypes.MicrosInstant;
import org.apache.beam.sdk.schemas.logicaltypes.PythonCallable;
import org.apache.beam.sdk.schemas.logicaltypes.SchemaLogicalType;
import org.apache.beam.sdk.schemas.logicaltypes.Time;
import org.apache.beam.sdk.schemas.logicaltypes.Timestamp;
import org.apache.beam.sdk.schemas.logicaltypes.UnknownLogicalType;
import org.apache.beam.sdk.schemas.logicaltypes.VariableBytes;
Expand Down Expand Up @@ -116,6 +117,7 @@ private static String getLogicalTypeUrn(String identifier) {
.put(FixedString.IDENTIFIER, FixedString.class)
.put(VariableString.IDENTIFIER, VariableString.class)
.put(Date.IDENTIFIER, Date.class)
.put(Time.IDENTIFIER, Time.class)
.put(Timestamp.IDENTIFIER, Timestamp.class)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
package org.apache.beam.sdk.schemas.logicaltypes;

import java.time.LocalTime;
import org.apache.beam.model.pipeline.v1.RunnerApi;
import org.apache.beam.model.pipeline.v1.SchemaApi;
import org.apache.beam.sdk.schemas.Schema;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* A time without a time-zone.
Expand All @@ -30,23 +33,20 @@
* of time in nanoseconds.
*/
public class Time implements Schema.LogicalType<LocalTime, Long> {
public static final String IDENTIFIER = "beam:logical_type:time:v1";
public static final String IDENTIFIER =
SchemaApi.LogicalTypes.Enum.TIME
.getValueDescriptor()
.getOptions()
.getExtension(RunnerApi.beamUrn);

@Override
public String getIdentifier() {
return IDENTIFIER;
}

// unused
@Override
public Schema.FieldType getArgumentType() {
return Schema.FieldType.STRING;
}

// unused
@Override
public String getArgument() {
return "";
public Schema.@Nullable FieldType getArgumentType() {
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -41,6 +42,7 @@
import org.apache.beam.model.pipeline.v1.SchemaApi.LogicalType;
import org.apache.beam.sdk.schemas.Schema.Field;
import org.apache.beam.sdk.schemas.Schema.FieldType;
import org.apache.beam.sdk.schemas.logicaltypes.Date;
import org.apache.beam.sdk.schemas.logicaltypes.DateTime;
import org.apache.beam.sdk.schemas.logicaltypes.FixedBytes;
import org.apache.beam.sdk.schemas.logicaltypes.FixedPrecisionNumeric;
Expand All @@ -51,6 +53,7 @@
import org.apache.beam.sdk.schemas.logicaltypes.PythonCallable;
import org.apache.beam.sdk.schemas.logicaltypes.SchemaLogicalType;
import org.apache.beam.sdk.schemas.logicaltypes.SqlTypes;
import org.apache.beam.sdk.schemas.logicaltypes.Time;
import org.apache.beam.sdk.schemas.logicaltypes.Timestamp;
import org.apache.beam.sdk.schemas.logicaltypes.UnknownLogicalType;
import org.apache.beam.sdk.schemas.logicaltypes.VariableBytes;
Expand Down Expand Up @@ -145,6 +148,7 @@ public static Iterable<Schema> data() {
.add(Schema.of(Field.of("fixed_bytes", FieldType.logicalType(FixedBytes.of(24)))))
.add(Schema.of(Field.of("micros_instant", FieldType.logicalType(new MicrosInstant()))))
.add(Schema.of(Field.of("date", FieldType.logicalType(SqlTypes.DATE))))
.add(Schema.of(Field.of("time", FieldType.logicalType(SqlTypes.TIME))))
.add(Schema.of(Field.of("python_callable", FieldType.logicalType(new PythonCallable()))))
.add(
Schema.of(
Expand Down Expand Up @@ -392,6 +396,7 @@ public static Iterable<Row> data() {
.add(simpleRow(FieldType.logicalType(new PortableNullArgLogicalType()), "str"))
.add(simpleRow(FieldType.logicalType(new DateTime()), LocalDateTime.of(2000, 1, 3, 3, 1)))
.add(simpleRow(FieldType.logicalType(SqlTypes.DATE), LocalDate.of(2000, 1, 3)))
.add(simpleRow(FieldType.logicalType(SqlTypes.TIME), LocalTime.of(3, 1, 2, 3000)))
.add(simpleNullRow(FieldType.STRING))
.add(simpleNullRow(FieldType.INT32))
.add(simpleNullRow(FieldType.map(FieldType.STRING, FieldType.INT32)))
Expand Down Expand Up @@ -444,6 +449,41 @@ public void typeInfoNotSet() {
}
}

/**
* A portable logical type has to be recoverable from its URN alone, because that is all a schema
* coming from another SDK carries. {@link LogicalTypesTest#testLogicalTypeFromToProtoCorrectly}
* cannot check this: it branches on {@code STANDARD_LOGICAL_TYPES} itself, so it passes whether
* or not the type is registered there.
*/
@RunWith(JUnit4.class)
public static class PortableLogicalTypeFromUrnTest {

@Test
public void timeIsRecoveredFromItsUrnAlone() {
FieldType fieldType = FieldType.logicalType(SqlTypes.TIME);

// serializeLogicalType = false, so the proto carries the URN and no Java payload
SchemaApi.FieldType proto = SchemaTranslation.fieldTypeToProto(fieldType, false, false);
assertThat(proto.getLogicalType().getUrn(), equalTo("beam:logical_type:time:v1"));
assertThat(proto.getLogicalType().getPayload().size(), equalTo(0));

Schema.FieldType translated = SchemaTranslation.fieldTypeFromProto(proto);
assertThat(translated.getLogicalType().getClass(), equalTo(Time.class));
assertThat(translated.getLogicalType().getBaseType(), equalTo(FieldType.INT64));
}

@Test
public void dateIsRecoveredFromItsUrnAlone() {
FieldType fieldType = FieldType.logicalType(SqlTypes.DATE);

SchemaApi.FieldType proto = SchemaTranslation.fieldTypeToProto(fieldType, false, false);
assertThat(proto.getLogicalType().getUrn(), equalTo("beam:logical_type:date:v1"));

Schema.FieldType translated = SchemaTranslation.fieldTypeFromProto(proto);
assertThat(translated.getLogicalType().getClass(), equalTo(Date.class));
}
}

/** Test schema translation of logical types. */
@RunWith(Parameterized.class)
public static class LogicalTypesTest {
Expand Down
Loading