use of com.netflix.metacat.common.type.ArrayType in project metacat by Netflix.
the class PostgreSqlTypeConverter 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 ARRAY type but was " + type.getClass().getName());
}
final ArrayType arrayType = (ArrayType) type;
final String array;
final Type elementType;
// Check for nested arrays
if (arrayType.getElementType() instanceof ArrayType) {
array = MULTI_ARRAY;
elementType = ((ArrayType) arrayType.getElementType()).getElementType();
} else {
array = SINGLE_ARRAY;
elementType = arrayType.getElementType();
}
// Recursively determine the type of the array
return this.fromMetacatType(elementType) + array;
case BIGINT:
return "BIGINT";
case BOOLEAN:
return "BOOLEAN";
case CHAR:
if (!(type instanceof CharType)) {
throw new IllegalArgumentException("Expected CHAR type but was " + type.getClass().getName());
}
final CharType charType = (CharType) type;
return "CHAR(" + charType.getLength() + ")";
case DATE:
return "DATE";
case DECIMAL:
if (!(type instanceof DecimalType)) {
throw new IllegalArgumentException("Expected decimal type but was " + type.getClass().getName());
}
final DecimalType decimalType = (DecimalType) type;
return "NUMERIC(" + decimalType.getPrecision() + ", " + decimalType.getScale() + ")";
case DOUBLE:
return "DOUBLE PRECISION";
case FLOAT:
return "REAL";
case INT:
return "INT";
case INTERVAL_DAY_TO_SECOND:
// TODO: It does but not sure how best to represent now
throw new UnsupportedOperationException("PostgreSQL doesn't support interval types");
case INTERVAL_YEAR_TO_MONTH:
// TODO: It does but not sure how best to represent now
throw new UnsupportedOperationException("PostgreSQL doesn't support interval types");
case JSON:
return "JSON";
case MAP:
throw new UnsupportedOperationException("PostgreSQL doesn't support map types");
case ROW:
// TODO: Well it does but how do we know what the internal type is?
throw new UnsupportedOperationException("PostgreSQL doesn't support row types");
case SMALLINT:
return "SMALLINT";
case STRING:
return "TEXT";
case TIME:
return "TIME";
case TIME_WITH_TIME_ZONE:
return "TIME WITH TIME ZONE";
case TIMESTAMP:
return "TIMESTAMP";
case TIMESTAMP_WITH_TIME_ZONE:
return "TIMESTAMP WITH TIME ZONE";
case TINYINT:
// NOTE: There is no tiny int type in PostgreSQL so using slightly larger SMALLINT
return "SMALLINT";
case UNKNOWN:
throw new IllegalArgumentException("Can't map an unknown type");
case VARBINARY:
return "BYTEA";
case VARCHAR:
if (!(type instanceof VarcharType)) {
throw new IllegalArgumentException("Expected varchar type but was " + type.getClass().getName());
}
final VarcharType varcharType = (VarcharType) type;
// NOTE: PostgreSQL lets you store up to 1GB in a varchar field which is about the same as TEXT
return "CHARACTER VARYING(" + varcharType.getLength() + ")";
default:
throw new IllegalArgumentException("Unknown type " + type.getTypeSignature().getBase());
}
}
use of com.netflix.metacat.common.type.ArrayType in project metacat by Netflix.
the class PigTypeConverter method toCanonicalArrayType.
private Type toCanonicalArrayType(final LogicalSchema.LogicalFieldSchema field) {
final LogicalSchema.LogicalFieldSchema subField = field.schema.getField(0);
final Type elementType;
if (subField.type == DataType.TUPLE && !TypeUtils.isNullOrEmpty(subField.schema.getFields()) && NAME_ARRAY_ELEMENT.equals(subField.schema.getFields().get(0).alias)) {
elementType = toCanonicalType(subField.schema.getFields().get(0));
} else {
elementType = toCanonicalType(subField);
}
return new ArrayType(elementType);
}
use of com.netflix.metacat.common.type.ArrayType 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.ArrayType 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.ArrayType 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