Search in sources :

Example 1 with VarbinaryType

use of com.netflix.metacat.common.type.VarbinaryType in project metacat by Netflix.

the class MySqlTypeConverter method fromMetacatType.

/**
 * {@inheritDoc}
 */
@Override
public String fromMetacatType(@Nonnull @NonNull final Type type) {
    switch(type.getTypeSignature().getBase()) {
        case ARRAY:
            throw new UnsupportedOperationException("MySQL doesn't support array types");
        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;
            final int charLength = charType.getLength();
            if (charLength < MIN_BYTE_LENGTH) {
                throw new IllegalArgumentException("CHAR type must have a length > 0");
            }
            // entire picture.
            if (charLength <= MAX_BYTE_LENGTH) {
                return "CHAR(" + charLength + ")";
            } else {
                return "TEXT";
            }
        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 "DECIMAL(" + decimalType.getPrecision() + ", " + decimalType.getScale() + ")";
        case DOUBLE:
            return "DOUBLE";
        case FLOAT:
            return "FLOAT(24)";
        case INT:
            return "INT";
        case INTERVAL_DAY_TO_SECOND:
            throw new UnsupportedOperationException("MySQL doesn't support interval types");
        case INTERVAL_YEAR_TO_MONTH:
            throw new UnsupportedOperationException("MySQL doesn't support interval types");
        case JSON:
            return "JSON";
        case MAP:
            throw new UnsupportedOperationException("MySQL doesn't support map types");
        case ROW:
            throw new UnsupportedOperationException("MySQL doesn't support row types");
        case SMALLINT:
            return "SMALLINT";
        case STRING:
            return "TEXT";
        case TIME:
        case TIME_WITH_TIME_ZONE:
            return "TIME";
        case TIMESTAMP:
        case TIMESTAMP_WITH_TIME_ZONE:
            return "TIMESTAMP";
        case TINYINT:
            return "TINYINT";
        case UNKNOWN:
            throw new IllegalArgumentException("Can't map an unknown type");
        case VARBINARY:
            if (!(type instanceof VarbinaryType)) {
                throw new IllegalArgumentException("Expected varbinary type but was " + type.getClass().getName());
            }
            final VarbinaryType varbinaryType = (VarbinaryType) type;
            final int binaryLength = varbinaryType.getLength();
            if (binaryLength < MIN_BYTE_LENGTH) {
                throw new IllegalArgumentException("VARBINARY type must have a length > 0");
            }
            // entire picture.
            if (binaryLength <= MAX_BYTE_LENGTH) {
                return "VARBINARY(" + binaryLength + ")";
            } else {
                return "BLOB";
            }
        case VARCHAR:
            if (!(type instanceof VarcharType)) {
                throw new IllegalArgumentException("Expected varchar type but was " + type.getClass().getName());
            }
            final VarcharType varcharType = (VarcharType) type;
            final int varCharLength = varcharType.getLength();
            if (varCharLength < MIN_BYTE_LENGTH) {
                throw new IllegalArgumentException("VARCHAR type must have a length > 0");
            }
            // entire picture.
            if (varCharLength <= MAX_BYTE_LENGTH) {
                return "VARCHAR(" + varCharLength + ")";
            } else {
                return "TEXT";
            }
        default:
            throw new IllegalArgumentException("Unknown type " + type.getTypeSignature().getBase());
    }
}
Also used : VarbinaryType(com.netflix.metacat.common.type.VarbinaryType) VarcharType(com.netflix.metacat.common.type.VarcharType) DecimalType(com.netflix.metacat.common.type.DecimalType) CharType(com.netflix.metacat.common.type.CharType)

Example 2 with VarbinaryType

use of com.netflix.metacat.common.type.VarbinaryType 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;
}
Also used : DecimalType(com.netflix.metacat.common.type.DecimalType) VarbinaryType(com.netflix.metacat.common.type.VarbinaryType) Type(com.netflix.metacat.common.type.Type) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) MetacatJsonLocator(com.netflix.metacat.common.json.MetacatJsonLocator) CharType(com.netflix.metacat.common.type.CharType) VarcharType(com.netflix.metacat.common.type.VarcharType) JsonNode(com.fasterxml.jackson.databind.JsonNode) RowType(com.netflix.metacat.common.type.RowType) TypeEnum(com.netflix.metacat.common.type.TypeEnum) MapType(com.netflix.metacat.common.type.MapType) ParametricType(com.netflix.metacat.common.type.ParametricType) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) VarcharType(com.netflix.metacat.common.type.VarcharType) RowType(com.netflix.metacat.common.type.RowType) JsonNode(com.fasterxml.jackson.databind.JsonNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) MetacatJsonLocator(com.netflix.metacat.common.json.MetacatJsonLocator) MapType(com.netflix.metacat.common.type.MapType) VarbinaryType(com.netflix.metacat.common.type.VarbinaryType) TypeEnum(com.netflix.metacat.common.type.TypeEnum) DecimalType(com.netflix.metacat.common.type.DecimalType) CharType(com.netflix.metacat.common.type.CharType) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Aggregations

CharType (com.netflix.metacat.common.type.CharType)2 DecimalType (com.netflix.metacat.common.type.DecimalType)2 VarbinaryType (com.netflix.metacat.common.type.VarbinaryType)2 VarcharType (com.netflix.metacat.common.type.VarcharType)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 TextNode (com.fasterxml.jackson.databind.node.TextNode)1 MetacatJsonLocator (com.netflix.metacat.common.json.MetacatJsonLocator)1 MapType (com.netflix.metacat.common.type.MapType)1 ParametricType (com.netflix.metacat.common.type.ParametricType)1 RowType (com.netflix.metacat.common.type.RowType)1 Type (com.netflix.metacat.common.type.Type)1 TypeEnum (com.netflix.metacat.common.type.TypeEnum)1