Search in sources :

Example 1 with RowType

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));
}
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)

Example 2 with RowType

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;
}
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 3 with RowType

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

Example 4 with RowType

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);
    }
}
Also used : ArrayType(com.netflix.metacat.common.type.ArrayType) Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) RowType(com.netflix.metacat.common.type.RowType) MapType(com.netflix.metacat.common.type.MapType)

Aggregations

MapType (com.netflix.metacat.common.type.MapType)4 RowType (com.netflix.metacat.common.type.RowType)4 ArrayType (com.netflix.metacat.common.type.ArrayType)3 DecimalType (com.netflix.metacat.common.type.DecimalType)3 BaseType (com.netflix.metacat.common.type.BaseType)2 CharType (com.netflix.metacat.common.type.CharType)2 Type (com.netflix.metacat.common.type.Type)2 VarcharType (com.netflix.metacat.common.type.VarcharType)2 ImmutableList (com.google.common.collect.ImmutableList)1 ParametricType (com.netflix.metacat.common.type.ParametricType)1 VarbinaryType (com.netflix.metacat.common.type.VarbinaryType)1 Matcher (java.util.regex.Matcher)1 DataType (org.apache.pig.data.DataType)1 LogicalSchema (org.apache.pig.newplan.logical.relational.LogicalSchema)1