Search in sources :

Example 1 with LogicalSchema

use of org.apache.pig.newplan.logical.relational.LogicalSchema 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 LogicalSchema

use of org.apache.pig.newplan.logical.relational.LogicalSchema in project metacat by Netflix.

the class PigTypeConverter method toMetacatType.

/**
     * {@inheritDoc}.
     */
@Override
public Type toMetacatType(@Nonnull @NonNull final String pigType) {
    try {
        final LogicalSchema schema = Utils.parseSchema(pigType);
        final LogicalSchema.LogicalFieldSchema field = schema.getField(0);
        return toCanonicalType(field);
    } catch (Exception e) {
        throw new IllegalArgumentException(String.format("Invalid type signature: '%s'", pigType));
    }
}
Also used : LogicalSchema(org.apache.pig.newplan.logical.relational.LogicalSchema) FrontendException(org.apache.pig.impl.logicalLayer.FrontendException)

Aggregations

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