Search in sources :

Example 1 with CharType

use of com.netflix.metacat.common.type.CharType 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 CharType

use of com.netflix.metacat.common.type.CharType 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());
    }
}
Also used : ArrayType(com.netflix.metacat.common.type.ArrayType) DecimalType(com.netflix.metacat.common.type.DecimalType) CharType(com.netflix.metacat.common.type.CharType) VarcharType(com.netflix.metacat.common.type.VarcharType) VarbinaryType(com.netflix.metacat.common.type.VarbinaryType) ArrayType(com.netflix.metacat.common.type.ArrayType) BaseType(com.netflix.metacat.common.type.BaseType) Type(com.netflix.metacat.common.type.Type) VarcharType(com.netflix.metacat.common.type.VarcharType) DecimalType(com.netflix.metacat.common.type.DecimalType) CharType(com.netflix.metacat.common.type.CharType)

Example 3 with CharType

use of com.netflix.metacat.common.type.CharType 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;
}
Also used : VarcharType(com.netflix.metacat.common.type.VarcharType) DecimalType(com.netflix.metacat.common.type.DecimalType) RowType(com.netflix.metacat.common.type.RowType) ParametricType(com.netflix.metacat.common.type.ParametricType) CharType(com.netflix.metacat.common.type.CharType) MapType(com.netflix.metacat.common.type.MapType)

Example 4 with CharType

use of com.netflix.metacat.common.type.CharType 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));
}
Also used : ArrayType(com.netflix.metacat.common.type.ArrayType) DecimalType(com.netflix.metacat.common.type.DecimalType) ArrayType(com.netflix.metacat.common.type.ArrayType) BaseType(com.netflix.metacat.common.type.BaseType) Type(com.netflix.metacat.common.type.Type) CharType(com.netflix.metacat.common.type.CharType) VarcharType(com.netflix.metacat.common.type.VarcharType) DataType(org.apache.pig.data.DataType) RowType(com.netflix.metacat.common.type.RowType) MapType(com.netflix.metacat.common.type.MapType) VarcharType(com.netflix.metacat.common.type.VarcharType) DecimalType(com.netflix.metacat.common.type.DecimalType) RowType(com.netflix.metacat.common.type.RowType) LogicalSchema(org.apache.pig.newplan.logical.relational.LogicalSchema) CharType(com.netflix.metacat.common.type.CharType) MapType(com.netflix.metacat.common.type.MapType)

Aggregations

CharType (com.netflix.metacat.common.type.CharType)4 DecimalType (com.netflix.metacat.common.type.DecimalType)4 VarcharType (com.netflix.metacat.common.type.VarcharType)4 ArrayType (com.netflix.metacat.common.type.ArrayType)2 BaseType (com.netflix.metacat.common.type.BaseType)2 MapType (com.netflix.metacat.common.type.MapType)2 RowType (com.netflix.metacat.common.type.RowType)2 Type (com.netflix.metacat.common.type.Type)2 VarbinaryType (com.netflix.metacat.common.type.VarbinaryType)2 ParametricType (com.netflix.metacat.common.type.ParametricType)1 DataType (org.apache.pig.data.DataType)1 LogicalSchema (org.apache.pig.newplan.logical.relational.LogicalSchema)1