Search in sources :

Example 11 with NamedTypeSignature

use of com.facebook.presto.common.type.NamedTypeSignature in project presto by prestodb.

the class DeltaTypeUtils method convertDeltaDataTypePrestoDataType.

/**
 * Convert given Delta data type to Presto data type signature.
 *
 * @param tableName  Used in error messages when an unsupported data type is encountered.
 * @param columnName Used in error messages when an unsupported data type is encountered.
 * @param deltaType  Data type to convert
 * @return
 */
public static TypeSignature convertDeltaDataTypePrestoDataType(SchemaTableName tableName, String columnName, DataType deltaType) {
    checkArgument(deltaType != null);
    if (deltaType instanceof StructType) {
        StructType deltaStructType = (StructType) deltaType;
        ImmutableList.Builder<TypeSignatureParameter> typeSignatureBuilder = ImmutableList.builder();
        Arrays.stream(deltaStructType.getFields()).forEach(field -> {
            String rowFieldName = field.getName().toLowerCase(Locale.US);
            TypeSignature rowFieldType = convertDeltaDataTypePrestoDataType(tableName, columnName + "." + field.getName(), field.getDataType());
            typeSignatureBuilder.add(TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName(rowFieldName, false)), rowFieldType)));
        });
        return new TypeSignature(StandardTypes.ROW, typeSignatureBuilder.build());
    } else if (deltaType instanceof ArrayType) {
        ArrayType deltaArrayType = (ArrayType) deltaType;
        TypeSignature elementType = convertDeltaDataTypePrestoDataType(tableName, columnName, deltaArrayType.getElementType());
        return new TypeSignature(ARRAY, ImmutableList.of(TypeSignatureParameter.of(elementType)));
    } else if (deltaType instanceof MapType) {
        MapType deltaMapType = (MapType) deltaType;
        TypeSignature keyType = convertDeltaDataTypePrestoDataType(tableName, columnName, deltaMapType.getKeyType());
        TypeSignature valueType = convertDeltaDataTypePrestoDataType(tableName, columnName, deltaMapType.getValueType());
        return new TypeSignature(MAP, ImmutableList.of(TypeSignatureParameter.of(keyType), TypeSignatureParameter.of(valueType)));
    }
    return convertDeltaPrimitiveTypeToPrestoPrimitiveType(tableName, columnName, deltaType).getTypeSignature();
}
Also used : ArrayType(io.delta.standalone.types.ArrayType) TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) StructType(io.delta.standalone.types.StructType) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) ImmutableList(com.google.common.collect.ImmutableList) RowFieldName(com.facebook.presto.common.type.RowFieldName) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) MapType(io.delta.standalone.types.MapType)

Example 12 with NamedTypeSignature

use of com.facebook.presto.common.type.NamedTypeSignature in project presto by prestodb.

the class HiveUtil method translateHiveUnsupportedTypeSignatureForTemporaryTable.

private static TypeSignature translateHiveUnsupportedTypeSignatureForTemporaryTable(TypeSignature typeSignature) {
    List<TypeSignatureParameter> parameters = typeSignature.getParameters();
    if (typeSignature.getBase().equals("unknown")) {
        return new TypeSignature(StandardTypes.BOOLEAN);
    }
    if (typeSignature.getBase().equals(StandardTypes.ROW)) {
        ImmutableList.Builder<TypeSignatureParameter> updatedParameters = ImmutableList.builder();
        for (int i = 0; i < parameters.size(); i++) {
            TypeSignatureParameter typeSignatureParameter = parameters.get(i);
            checkArgument(typeSignatureParameter.isNamedTypeSignature(), "unexpected row type signature parameter: %s", typeSignatureParameter);
            NamedTypeSignature namedTypeSignature = typeSignatureParameter.getNamedTypeSignature();
            updatedParameters.add(TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(namedTypeSignature.getFieldName().orElse(new RowFieldName("_field_" + i, false))), translateHiveUnsupportedTypeSignatureForTemporaryTable(namedTypeSignature.getTypeSignature()))));
        }
        return new TypeSignature(StandardTypes.ROW, updatedParameters.build());
    }
    if (!parameters.isEmpty()) {
        ImmutableList.Builder<TypeSignatureParameter> updatedParameters = ImmutableList.builder();
        for (TypeSignatureParameter parameter : parameters) {
            switch(parameter.getKind()) {
                case LONG:
                case VARIABLE:
                    updatedParameters.add(parameter);
                    continue;
                case TYPE:
                    updatedParameters.add(TypeSignatureParameter.of(translateHiveUnsupportedTypeSignatureForTemporaryTable(parameter.getTypeSignature())));
                    break;
                case NAMED_TYPE:
                    NamedTypeSignature namedTypeSignature = parameter.getNamedTypeSignature();
                    updatedParameters.add(TypeSignatureParameter.of(new NamedTypeSignature(namedTypeSignature.getFieldName(), translateHiveUnsupportedTypeSignatureForTemporaryTable(namedTypeSignature.getTypeSignature()))));
                    break;
                default:
                    throw new IllegalArgumentException("Unexpected parameter type: " + parameter.getKind());
            }
        }
        return new TypeSignature(typeSignature.getBase(), updatedParameters.build());
    }
    return typeSignature;
}
Also used : TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) RowFieldName(com.facebook.presto.common.type.RowFieldName) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature)

Example 13 with NamedTypeSignature

use of com.facebook.presto.common.type.NamedTypeSignature in project presto by prestodb.

the class FixJsonDataUtils method fixValue.

/**
 * Force values coming from Jackson to have the expected object type.
 */
private static Object fixValue(TypeSignature signature, Object value) {
    if (value == null) {
        return null;
    }
    if (signature.getBase().equals(ARRAY)) {
        List<Object> fixedValue = new ArrayList<>();
        for (Object object : List.class.cast(value)) {
            fixedValue.add(fixValue(signature.getTypeParametersAsTypeSignatures().get(0), object));
        }
        return fixedValue;
    }
    if (signature.getBase().equals(MAP)) {
        TypeSignature keySignature = signature.getTypeParametersAsTypeSignatures().get(0);
        TypeSignature valueSignature = signature.getTypeParametersAsTypeSignatures().get(1);
        Map<Object, Object> fixedValue = new HashMap<>();
        for (Map.Entry<?, ?> entry : (Set<Map.Entry<?, ?>>) Map.class.cast(value).entrySet()) {
            fixedValue.put(fixValue(keySignature, entry.getKey()), fixValue(valueSignature, entry.getValue()));
        }
        return fixedValue;
    }
    if (signature.getBase().equals(ROW)) {
        Map<String, Object> fixedValue = new LinkedHashMap<>();
        List<Object> listValue = List.class.cast(value);
        checkArgument(listValue.size() == signature.getParameters().size(), "Mismatched data values and row type");
        for (int i = 0; i < listValue.size(); i++) {
            TypeSignatureParameter parameter = signature.getParameters().get(i);
            checkArgument(parameter.getKind() == ParameterKind.NAMED_TYPE, "Unexpected parameter [%s] for row type", parameter);
            NamedTypeSignature namedTypeSignature = parameter.getNamedTypeSignature();
            String key = namedTypeSignature.getName().orElse("field" + i);
            fixedValue.put(key, fixValue(namedTypeSignature.getTypeSignature(), listValue.get(i)));
        }
        return fixedValue;
    }
    if (signature.isVarcharEnum()) {
        return String.class.cast(value);
    }
    if (signature.isBigintEnum()) {
        if (value instanceof String) {
            return Long.parseLong((String) value);
        }
        return ((Number) value).longValue();
    }
    switch(signature.getBase()) {
        case BIGINT:
            if (value instanceof String) {
                return Long.parseLong((String) value);
            }
            return ((Number) value).longValue();
        case INTEGER:
            if (value instanceof String) {
                return Integer.parseInt((String) value);
            }
            return ((Number) value).intValue();
        case SMALLINT:
            if (value instanceof String) {
                return Short.parseShort((String) value);
            }
            return ((Number) value).shortValue();
        case TINYINT:
            if (value instanceof String) {
                return Byte.parseByte((String) value);
            }
            return ((Number) value).byteValue();
        case DOUBLE:
            if (value instanceof String) {
                return Double.parseDouble((String) value);
            }
            return ((Number) value).doubleValue();
        case REAL:
            if (value instanceof String) {
                return Float.parseFloat((String) value);
            }
            return ((Number) value).floatValue();
        case BOOLEAN:
            if (value instanceof String) {
                return Boolean.parseBoolean((String) value);
            }
            return Boolean.class.cast(value);
        case VARCHAR:
        case JSON:
        case TIME:
        case TIME_WITH_TIME_ZONE:
        case TIMESTAMP:
        case TIMESTAMP_WITH_TIME_ZONE:
        case DATE:
        case INTERVAL_YEAR_TO_MONTH:
        case INTERVAL_DAY_TO_SECOND:
        case IPADDRESS:
        case IPPREFIX:
        case DECIMAL:
        case CHAR:
        case GEOMETRY:
            return String.class.cast(value);
        case BING_TILE:
            // they are serialized as json otherwise (value will be a LinkedHashMap).
            return value;
        default:
            // as a plain text and everything else is base64 encoded binary
            if (value instanceof String) {
                return Base64.getDecoder().decode((String) value);
            }
            return value;
    }
}
Also used : Set(java.util.Set) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) LinkedHashMap(java.util.LinkedHashMap) TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 14 with NamedTypeSignature

use of com.facebook.presto.common.type.NamedTypeSignature in project presto by prestodb.

the class TestClientTypeSignature method testJsonRoundTrip.

@Test
public void testJsonRoundTrip() {
    TypeSignature bigint = BIGINT.getTypeSignature();
    assertJsonRoundTrip(new ClientTypeSignature(bigint));
    assertJsonRoundTrip(new ClientTypeSignature("array", ImmutableList.of(new ClientTypeSignatureParameter(TypeSignatureParameter.of(bigint)))));
    assertJsonRoundTrip(new ClientTypeSignature("foo", ImmutableList.of(new ClientTypeSignatureParameter(TypeSignatureParameter.of(42)))));
    assertJsonRoundTrip(new ClientTypeSignature("row", ImmutableList.of(new ClientTypeSignatureParameter(TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName("foo", false)), bigint))), new ClientTypeSignatureParameter(TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName("bar", false)), bigint))))));
}
Also used : TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) RowFieldName(com.facebook.presto.common.type.RowFieldName) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) Test(org.testng.annotations.Test)

Aggregations

NamedTypeSignature (com.facebook.presto.common.type.NamedTypeSignature)14 TypeSignatureParameter (com.facebook.presto.common.type.TypeSignatureParameter)13 RowFieldName (com.facebook.presto.common.type.RowFieldName)10 TypeSignature (com.facebook.presto.common.type.TypeSignature)10 ImmutableList (com.google.common.collect.ImmutableList)9 Type (com.facebook.presto.common.type.Type)6 PrestoException (com.facebook.presto.spi.PrestoException)5 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)5 List (java.util.List)4 Optional (java.util.Optional)4 CharType (com.facebook.presto.common.type.CharType)3 DecimalType (com.facebook.presto.common.type.DecimalType)3 VarcharType (com.facebook.presto.common.type.VarcharType)3 DecimalTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo)3 TypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)3 MapType (com.facebook.presto.common.type.MapType)2 RowType (com.facebook.presto.common.type.RowType)2 StandardTypes (com.facebook.presto.common.type.StandardTypes)2 ROW (com.facebook.presto.common.type.StandardTypes.ROW)2 TypeParameter (com.facebook.presto.common.type.TypeParameter)2