Search in sources :

Example 1 with MapType

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

Example 2 with MapType

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

the class PigTypeConverter method toCanonicalMapType.

private Type toCanonicalMapType(final LogicalSchema.LogicalFieldSchema field) {
    final Type key = BaseType.STRING;
    Type value = BaseType.UNKNOWN;
    if (null != field.schema && !TypeUtils.isNullOrEmpty(field.schema.getFields())) {
        value = toCanonicalType(field.schema.getFields().get(0));
    }
    return new MapType(key, value);
}
Also used : DecimalType(com.netflix.metacat.common.type.DecimalType) ArrayType(com.netflix.metacat.common.type.ArrayType) Type(com.netflix.metacat.common.type.Type) CharType(com.netflix.metacat.common.type.CharType) VarcharType(com.netflix.metacat.common.type.VarcharType) LocalExecType(org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.LocalExecType) DataType(org.apache.pig.data.DataType) BaseType(com.netflix.metacat.common.type.BaseType) RowType(com.netflix.metacat.common.type.RowType) MapType(com.netflix.metacat.common.type.MapType) MapType(com.netflix.metacat.common.type.MapType)

Example 3 with MapType

use of com.netflix.metacat.common.type.MapType 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.getDisplayName()));
}
Also used : ArrayType(com.netflix.metacat.common.type.ArrayType) DecimalType(com.netflix.metacat.common.type.DecimalType) ArrayType(com.netflix.metacat.common.type.ArrayType) Type(com.netflix.metacat.common.type.Type) CharType(com.netflix.metacat.common.type.CharType) VarcharType(com.netflix.metacat.common.type.VarcharType) LocalExecType(org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.LocalExecType) DataType(org.apache.pig.data.DataType) BaseType(com.netflix.metacat.common.type.BaseType) 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 4 with MapType

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

use of com.netflix.metacat.common.type.MapType 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)6 RowType (com.netflix.metacat.common.type.RowType)6 DecimalType (com.netflix.metacat.common.type.DecimalType)5 ArrayType (com.netflix.metacat.common.type.ArrayType)4 CharType (com.netflix.metacat.common.type.CharType)4 Type (com.netflix.metacat.common.type.Type)4 VarcharType (com.netflix.metacat.common.type.VarcharType)4 BaseType (com.netflix.metacat.common.type.BaseType)3 ParametricType (com.netflix.metacat.common.type.ParametricType)2 VarbinaryType (com.netflix.metacat.common.type.VarbinaryType)2 LocalExecType (org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.LocalExecType)2 DataType (org.apache.pig.data.DataType)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 ImmutableList (com.google.common.collect.ImmutableList)1 MetacatJsonLocator (com.netflix.metacat.common.json.MetacatJsonLocator)1 TypeEnum (com.netflix.metacat.common.type.TypeEnum)1 Matcher (java.util.regex.Matcher)1