Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BEAM-8630] Add logical types, make public #10785

Merged
merged 1 commit into from
Feb 6, 2020
Merged
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 @@ -29,6 +29,8 @@
import java.util.List;
import java.util.stream.Collectors;
import org.apache.beam.sdk.annotations.Internal;
import org.apache.beam.sdk.extensions.sql.impl.utils.CalciteUtils.DateType;
import org.apache.beam.sdk.extensions.sql.impl.utils.CalciteUtils.TimeType;
import org.apache.beam.sdk.schemas.Schema;
import org.apache.beam.sdk.schemas.Schema.Field;
import org.apache.beam.sdk.schemas.Schema.FieldType;
Expand Down Expand Up @@ -68,6 +70,16 @@ public static Type beamFieldTypeToZetaSqlType(FieldType fieldType) {
return createZetaSqlArrayTypeFromBeamElementFieldType(fieldType.getCollectionElementType());
case ROW:
return createZetaSqlStructTypeFromBeamSchema(fieldType.getRowSchema());
case LOGICAL_TYPE:
switch (fieldType.getLogicalType().getIdentifier()) {
case DateType.IDENTIFIER:
return TypeFactory.createSimpleType(TypeKind.TYPE_DATE);
case TimeType.IDENTIFIER:
return TypeFactory.createSimpleType(TypeKind.TYPE_TIME);
default:
throw new IllegalArgumentException(
"Unsupported Beam logical type: " + fieldType.getLogicalType().getIdentifier());
}
default:
throw new UnsupportedOperationException(
"Unsupported Beam fieldType: " + fieldType.getTypeName());
Expand All @@ -79,7 +91,7 @@ private static ArrayType createZetaSqlArrayTypeFromBeamElementFieldType(
return TypeFactory.createArrayType(beamFieldTypeToZetaSqlType(elementFieldType));
}

private static StructType createZetaSqlStructTypeFromBeamSchema(Schema schema) {
public static StructType createZetaSqlStructTypeFromBeamSchema(Schema schema) {
return TypeFactory.createStructType(
schema.getFields().stream()
.map(ZetaSqlUtils::beamFieldToZetaSqlStructField)
Expand Down Expand Up @@ -139,7 +151,7 @@ private static Value javaListToZetaSqlArrayValue(List<Object> elements, FieldTyp
createZetaSqlArrayTypeFromBeamElementFieldType(elementType), values);
}

private static Value beamRowToZetaSqlStructValue(Row row, Schema schema) {
public static Value beamRowToZetaSqlStructValue(Row row, Schema schema) {
List<Value> values = new ArrayList<>(row.getFieldCount());

for (int i = 0; i < row.getFieldCount(); i++) {
Expand Down