use of com.netflix.metacat.common.type.Type in project metacat by Netflix.
the class PostgreSqlTypeConverter method toMetacatType.
/**
* {@inheritDoc}
*
* @see <a href="https://www.postgresql.org/docs/current/static/datatype.html">PosgreSQL Data Types</a>
*/
@Override
public Type toMetacatType(@Nonnull @NonNull final String type) {
// See: https://www.postgresql.org/docs/current/static/datatype.html
final String lowerType = type.toLowerCase();
// Split up the possible type: TYPE[(size, magnitude)] EXTRA
final String[] splitType = this.splitType(lowerType);
final Type elementType;
switch(splitType[0]) {
case "smallint":
case "int2":
elementType = BaseType.SMALLINT;
break;
case "int":
case "integer":
case "int4":
elementType = BaseType.INT;
break;
case "int8":
case "bigint":
elementType = BaseType.BIGINT;
break;
case "decimal":
case "numeric":
elementType = this.toMetacatDecimalType(splitType);
break;
case "real":
case "float4":
elementType = BaseType.FLOAT;
break;
case "double precision":
case "float8":
elementType = BaseType.DOUBLE;
break;
case "character varying":
case "varchar":
elementType = this.toMetacatVarcharType(splitType);
break;
case "character":
case "char":
elementType = this.toMetacatCharType(splitType);
break;
case "text":
elementType = BaseType.STRING;
break;
case "bytea":
elementType = VarbinaryType.createVarbinaryType(Integer.MAX_VALUE);
break;
case "timestamp":
elementType = this.toMetacatTimestampType(splitType);
break;
case "timestampz":
elementType = BaseType.TIMESTAMP_WITH_TIME_ZONE;
break;
case "date":
elementType = BaseType.DATE;
break;
case "time":
elementType = this.toMetacatTimeType(splitType);
break;
case "timez":
elementType = BaseType.TIME_WITH_TIME_ZONE;
break;
case "boolean":
case "bool":
elementType = BaseType.BOOLEAN;
break;
case "bit":
case "bit varying":
case "varbit":
elementType = this.toMetacatBitType(splitType);
break;
case "json":
elementType = BaseType.JSON;
break;
case "smallserial":
case "serial2":
case "serial":
case "serial4":
case "bigserial":
case "serial8":
case "money":
case "interval":
case "enum":
case "point":
case "line":
case "lseg":
case "box":
case "path":
case "polygon":
case "circle":
case "cidr":
case "inet":
case "macaddr":
case "tsvector":
case "tsquery":
case "uuid":
case "xml":
case "int4range":
case "int8range":
case "numrange":
case "tsrange":
case "tstzrange":
case "daterange":
case "oid":
case "regproc":
case "regprocedure":
case "regoper":
case "regoperator":
case "regclass":
case "regtype":
case "regrole":
case "regnamespace":
case "regconfig":
case "regdictionary":
case "pg_lsn":
case "jsonb":
case "txid_snapshot":
default:
// TODO: Will catch complex types but not sure how to parse beyond that right now, may be recursive?
// https://www.postgresql.org/docs/current/static/rowtypes.html
log.info("Encountered {} type. Returning unknown type", splitType[0]);
return BaseType.UNKNOWN;
}
return this.checkForArray(splitType, elementType);
}
use of com.netflix.metacat.common.type.Type 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.Type in project metacat by Netflix.
the class ConnectorTypeConverter method fromMetacatTypeToJson.
/**
* Converts from Metacat type to JSON format.
* @param type type
* @return Type in JSON format
*/
default JsonNode fromMetacatTypeToJson(Type type) {
final MetacatJsonLocator json = new MetacatJsonLocator();
JsonNode result = null;
final TypeEnum base = type.getTypeSignature().getBase();
if (!base.isParametricType()) {
result = new TextNode(fromMetacatType(type));
} else if (type instanceof DecimalType || type instanceof CharType || type instanceof VarcharType || type instanceof VarbinaryType) {
final ObjectNode node = json.emptyObjectNode();
final String typeText = fromMetacatType(type);
final int index = typeText.indexOf('(');
if (index == -1) {
node.put("type", typeText);
} else {
node.put("type", typeText.substring(0, index));
if (type instanceof DecimalType) {
node.put("precision", ((DecimalType) type).getPrecision());
node.put("scale", ((DecimalType) type).getScale());
} else if (type instanceof CharType) {
node.put("length", ((CharType) type).getLength());
} else if (type instanceof VarcharType) {
node.put("length", ((VarcharType) type).getLength());
} else {
node.put("length", ((VarbinaryType) type).getLength());
}
}
result = node;
} else if (base.equals(TypeEnum.MAP)) {
final MapType mapType = (MapType) type;
final ObjectNode node = json.emptyObjectNode();
node.put("type", TypeEnum.MAP.getType());
node.set("keyType", fromMetacatTypeToJson(mapType.getKeyType()));
node.set("valueType", fromMetacatTypeToJson(mapType.getValueType()));
result = node;
} else if (base.equals(TypeEnum.ROW)) {
final RowType rowType = (RowType) type;
final ObjectNode node = json.emptyObjectNode();
final ArrayNode fieldsNode = node.arrayNode();
rowType.getFields().forEach(f -> {
final ObjectNode fieldNode = json.emptyObjectNode();
fieldNode.put("name", f.getName());
fieldNode.set("type", fromMetacatTypeToJson(f.getType()));
fieldsNode.add(fieldNode);
});
node.put("type", TypeEnum.ROW.getType());
node.set("fields", fieldsNode);
result = node;
} else if (base.equals(TypeEnum.ARRAY)) {
final ObjectNode node = json.emptyObjectNode();
node.put("type", TypeEnum.ARRAY.getType());
((ParametricType) type).getParameters().stream().findFirst().ifPresent(t -> node.set("elementType", fromMetacatTypeToJson(t)));
result = node;
}
return result;
}
use of com.netflix.metacat.common.type.Type in project metacat by Netflix.
the class DozerJsonTypeConverter method convert.
@Override
public Object convert(final Object existingDestinationFieldValue, final Object sourceFieldValue, final Class<?> destinationClass, final Class<?> sourceClass) {
JsonNode result = null;
final Type type = (Type) sourceFieldValue;
final ConnectorTypeConverter typeConverter;
try {
typeConverter = this.typeConverterFactory.get(MetacatContextManager.getContext().getDataTypeContext());
} catch (final Exception e) {
throw new IllegalStateException("Unable to get a type converter", e);
}
try {
result = typeConverter.fromMetacatTypeToJson(type);
} catch (final Exception ignored) {
// TODO: Handle exception.
}
return result;
}
use of com.netflix.metacat.common.type.Type in project metacat by Netflix.
the class S3ConnectorInfoConverter method toType.
/**
* Converts from type string to Metacat type.
* @param type type
* @return Type
*/
public Type toType(final String type) {
Type result = null;
if (isUsePigTypes) {
// Hack for now. We need to correct the type format in Franklin
String typeString = type;
if ("map".equals(type)) {
typeString = "map[]";
}
result = pigTypeConverter.toMetacatType(typeString);
} else {
result = typeManager.getType(TypeSignature.parseTypeSignature(type));
}
return result;
}
Aggregations