use of com.netflix.metacat.common.type.RowType in project metacat by Netflix.
the class PigTypeConverter method fromCanonicalTypeToPigSchema.
private LogicalSchema.LogicalFieldSchema fromCanonicalTypeToPigSchema(final String alias, final Type canonicalType) {
if (PigTypeMapping.getCANONICAL_TO_PIG().containsKey(canonicalType)) {
return new LogicalSchema.LogicalFieldSchema(alias, null, PigTypeMapping.getCANONICAL_TO_PIG().get(canonicalType));
} else if (canonicalType instanceof DecimalType) {
return new LogicalSchema.LogicalFieldSchema(alias, null, DataType.DOUBLE);
} else if (canonicalType instanceof VarcharType || canonicalType instanceof CharType) {
return new LogicalSchema.LogicalFieldSchema(alias, null, DataType.CHARARRAY);
} else if (canonicalType instanceof MapType) {
final MapType mapType = (MapType) canonicalType;
LogicalSchema schema = null;
if (((MapType) canonicalType).getValueType() != null && !BaseType.UNKNOWN.equals(mapType.getValueType())) {
schema = new LogicalSchema();
schema.addField(fromCanonicalTypeToPigSchema(null, mapType.getValueType()));
}
return new LogicalSchema.LogicalFieldSchema(alias, schema, DataType.MAP);
} else if (canonicalType instanceof ArrayType) {
final ArrayType arrayType = (ArrayType) canonicalType;
final LogicalSchema schema = new LogicalSchema();
Type elementType = arrayType.getElementType();
if (elementType != null) {
if (!(elementType instanceof RowType)) {
elementType = RowType.createRowType(Lists.newArrayList(elementType), ImmutableList.of(NAME_ARRAY_ELEMENT));
}
schema.addField(fromCanonicalTypeToPigSchema(null, elementType));
}
return new LogicalSchema.LogicalFieldSchema(alias, schema, DataType.BAG);
} else if (canonicalType instanceof RowType) {
final LogicalSchema schema = new LogicalSchema();
for (RowType.RowField rowField : ((RowType) canonicalType).getFields()) {
schema.addField(fromCanonicalTypeToPigSchema(rowField.getName() != null ? rowField.getName() : alias, rowField.getType()));
}
return new LogicalSchema.LogicalFieldSchema(alias, schema, DataType.TUPLE);
}
throw new IllegalArgumentException(String.format("Invalid for Pig converter: '%s'", canonicalType));
}
use of com.netflix.metacat.common.type.RowType in project metacat by Netflix.
the class HiveTypeConverter method fromMetacatType.
@Override
public String fromMetacatType(final Type type) {
if (HiveTypeMapping.getCANONICAL_TO_HIVE().containsKey(type)) {
return HiveTypeMapping.getCANONICAL_TO_HIVE().get(type);
}
if (type instanceof DecimalType | type instanceof CharType | type instanceof VarcharType) {
return type.getDisplayName();
} else if (type.getTypeSignature().getBase().equals(TypeEnum.MAP)) {
final MapType mapType = (MapType) type;
return "map<" + fromMetacatType(mapType.getKeyType()) + "," + fromMetacatType(mapType.getValueType()) + ">";
} else if (type.getTypeSignature().getBase().equals(TypeEnum.ROW)) {
final RowType rowType = (RowType) type;
final String typeString = rowType.getFields().stream().map(this::rowFieldToString).collect(Collectors.joining(","));
return "struct<" + typeString + ">";
} else if (type.getTypeSignature().getBase().equals(TypeEnum.ARRAY)) {
final String typeString = ((ParametricType) type).getParameters().stream().map(this::fromMetacatType).collect(Collectors.joining(","));
return "array<" + typeString + ">";
}
return null;
}
use of com.netflix.metacat.common.type.RowType in project metacat by Netflix.
the class CassandraTypeConverter method fromMetacatType.
/**
* {@inheritDoc}
*/
@Override
public String fromMetacatType(@Nonnull @NonNull final Type type) {
switch(type.getTypeSignature().getBase()) {
case ARRAY:
if (!(type instanceof ArrayType)) {
throw new IllegalArgumentException("Expected an ArrayType and got " + type.getClass());
}
final ArrayType arrayType = (ArrayType) type;
return "list<" + this.getElementTypeString(arrayType.getElementType()) + ">";
case BIGINT:
return "bigint";
case BOOLEAN:
return "boolean";
case CHAR:
// TODO: Should we make this unsupported?
return "text";
case DATE:
return "date";
case DECIMAL:
return "decimal";
case DOUBLE:
return "double";
case FLOAT:
return "float";
case INT:
return "int";
case INTERVAL_DAY_TO_SECOND:
throw new UnsupportedOperationException("Cassandra doesn't support intervals.");
case INTERVAL_YEAR_TO_MONTH:
throw new UnsupportedOperationException("Cassandra doesn't support intervals.");
case JSON:
throw new UnsupportedOperationException("Cassandra doesn't support JSON natively.");
case MAP:
if (!(type instanceof MapType)) {
throw new IllegalArgumentException("Was expecting MapType instead it is " + type.getClass());
}
final MapType mapType = (MapType) type;
final Type keyType = mapType.getKeyType();
final Type valueType = mapType.getValueType();
return "map<" + this.getElementTypeString(keyType) + ", " + this.getElementTypeString(valueType) + ">";
case ROW:
if (!(type instanceof RowType)) {
throw new IllegalArgumentException("Was expecting RowType instead it is " + type.getClass());
}
final RowType rowType = (RowType) type;
final StringBuilder tupleBuilder = new StringBuilder();
tupleBuilder.append("tuple<");
// Tuple fields don't need to be frozen
boolean putComma = false;
for (final RowType.RowField field : rowType.getFields()) {
if (putComma) {
tupleBuilder.append(", ");
} else {
putComma = true;
}
tupleBuilder.append(this.fromMetacatType(field.getType()));
}
tupleBuilder.append(">");
return tupleBuilder.toString();
case SMALLINT:
return "smallint";
case STRING:
return "text";
case TIME:
return "time";
case TIME_WITH_TIME_ZONE:
throw new UnsupportedOperationException("Cassandra doesn't support time with timezone");
case TIMESTAMP:
return "timestamp";
case TIMESTAMP_WITH_TIME_ZONE:
throw new UnsupportedOperationException("Cassandra doesn't support time with timezone");
case TINYINT:
return "tinyint";
case UNKNOWN:
throw new UnsupportedOperationException("Cassandra doesn't support an unknown type");
case VARBINARY:
return "blob";
case VARCHAR:
return "text";
default:
throw new IllegalArgumentException("Unknown type: " + type.getTypeSignature().getBase());
}
}
use of com.netflix.metacat.common.type.RowType in project metacat by Netflix.
the class CassandraTypeConverter method toMetacatType.
/**
* {@inheritDoc}
*/
@Override
public Type toMetacatType(@Nonnull @NonNull final String type) {
final Matcher matcher = TYPE_PATTERN.matcher(type.toLowerCase());
// TODO: Escape case from recursion may be needed to avoid potential infinite
if (matcher.matches()) {
final String cqlType = matcher.group(TYPE_GROUP);
switch(cqlType) {
case "ascii":
return BaseType.STRING;
case "bigint":
return BaseType.BIGINT;
case "blob":
return VarbinaryType.createVarbinaryType(Integer.MAX_VALUE);
case "boolean":
return BaseType.BOOLEAN;
case "counter":
return BaseType.BIGINT;
case "date":
return BaseType.DATE;
case "decimal":
return DecimalType.createDecimalType();
case "double":
return BaseType.DOUBLE;
case "float":
return BaseType.FLOAT;
case "frozen":
return this.toMetacatType(matcher.group(PARAM_GROUP));
case "int":
return BaseType.INT;
case "list":
// The possible null for the PARAM_GROUP should be handled on recursive call throwing exception
return new ArrayType(this.toMetacatType(matcher.group(PARAM_GROUP)));
case "map":
final Matcher mapMatcher = MAP_PARAM_PATTERN.matcher(matcher.group(PARAM_GROUP));
if (mapMatcher.matches()) {
return new MapType(this.toMetacatType(mapMatcher.group(MAP_KEY_GROUP)), this.toMetacatType(mapMatcher.group(MAP_VALUE_GROUP)));
} else {
throw new IllegalArgumentException("Unable to parse map params " + matcher.group(PARAM_GROUP));
}
case "smallint":
return BaseType.SMALLINT;
case "text":
return BaseType.STRING;
case "time":
return BaseType.TIME;
case "timestamp":
return BaseType.TIMESTAMP;
case "tinyint":
return BaseType.TINYINT;
case "tuple":
if (matcher.group(PARAM_GROUP) == null) {
throw new IllegalArgumentException("Empty tuple param group. Unable to parse");
}
final Matcher tupleMatcher = TUPLE_PARAM_PATTERN.matcher(matcher.group(PARAM_GROUP));
final ImmutableList.Builder<RowType.RowField> tupleFields = ImmutableList.builder();
int rowFieldNumber = 0;
while (tupleMatcher.find()) {
tupleFields.add(new RowType.RowField(this.toMetacatType(tupleMatcher.group(TUPLE_GROUP)), "field" + rowFieldNumber++));
}
return new RowType(tupleFields.build());
case "varchar":
return BaseType.STRING;
case "varint":
return BaseType.INT;
case "inet":
case "set":
case "timeuuid":
case "uuid":
default:
log.info("Currently unsupported type {}, returning Unknown type", cqlType);
return BaseType.UNKNOWN;
}
} else {
throw new IllegalArgumentException("Unable to parse CQL type " + type);
}
}
Aggregations