Search in sources :

Example 6 with TypeSignatureParameter

use of io.trino.spi.type.TypeSignatureParameter in project trino by trinodb.

the class BigQueryResultPageSource method writeBlock.

private void writeBlock(BlockBuilder output, Type type, Object value) {
    if (type instanceof ArrayType && value instanceof List<?>) {
        BlockBuilder builder = output.beginBlockEntry();
        for (Object element : (List<?>) value) {
            appendTo(type.getTypeParameters().get(0), element, builder);
        }
        output.closeEntry();
        return;
    }
    if (type instanceof RowType && value instanceof GenericRecord) {
        GenericRecord record = (GenericRecord) value;
        BlockBuilder builder = output.beginBlockEntry();
        List<String> fieldNames = new ArrayList<>();
        for (int i = 0; i < type.getTypeSignature().getParameters().size(); i++) {
            TypeSignatureParameter parameter = type.getTypeSignature().getParameters().get(i);
            fieldNames.add(parameter.getNamedTypeSignature().getName().orElse("field" + i));
        }
        checkState(fieldNames.size() == type.getTypeParameters().size(), "fieldName doesn't match with type size : %s", type);
        for (int index = 0; index < type.getTypeParameters().size(); index++) {
            appendTo(type.getTypeParameters().get(index), record.get(fieldNames.get(index)), builder);
        }
        output.closeEntry();
        return;
    }
    throw new TrinoException(GENERIC_INTERNAL_ERROR, "Unhandled type for Block: " + type.getTypeSignature());
}
Also used : ArrayList(java.util.ArrayList) RowType(io.trino.spi.type.RowType) ArrayType(io.trino.spi.type.ArrayType) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) TrinoException(io.trino.spi.TrinoException) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) GenericRecord(org.apache.avro.generic.GenericRecord) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 7 with TypeSignatureParameter

use of io.trino.spi.type.TypeSignatureParameter in project trino by trinodb.

the class DeltaHiveTypeTranslator method translate.

// Copy from HiveTypeTranslator with a custom mapping for TimestampWithTimeZone
public static TypeInfo translate(Type type) {
    requireNonNull(type, "type is null");
    if (BOOLEAN.equals(type)) {
        return HIVE_BOOLEAN.getTypeInfo();
    }
    if (BIGINT.equals(type)) {
        return HIVE_LONG.getTypeInfo();
    }
    if (INTEGER.equals(type)) {
        return HIVE_INT.getTypeInfo();
    }
    if (SMALLINT.equals(type)) {
        return HIVE_SHORT.getTypeInfo();
    }
    if (TINYINT.equals(type)) {
        return HIVE_BYTE.getTypeInfo();
    }
    if (REAL.equals(type)) {
        return HIVE_FLOAT.getTypeInfo();
    }
    if (DOUBLE.equals(type)) {
        return HIVE_DOUBLE.getTypeInfo();
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        if (varcharType.isUnbounded()) {
            return HIVE_STRING.getTypeInfo();
        }
        if (varcharType.getBoundedLength() <= HiveVarchar.MAX_VARCHAR_LENGTH) {
            return getVarcharTypeInfo(varcharType.getBoundedLength());
        }
        throw new TrinoException(NOT_SUPPORTED, format("Unsupported Hive type: %s. Supported VARCHAR types: VARCHAR(<=%d), VARCHAR.", type, HiveVarchar.MAX_VARCHAR_LENGTH));
    }
    if (type instanceof CharType) {
        CharType charType = (CharType) type;
        int charLength = charType.getLength();
        if (charLength <= HiveChar.MAX_CHAR_LENGTH) {
            return getCharTypeInfo(charLength);
        }
        throw new TrinoException(NOT_SUPPORTED, format("Unsupported Hive type: %s. Supported CHAR types: CHAR(<=%d).", type, HiveChar.MAX_CHAR_LENGTH));
    }
    if (VARBINARY.equals(type)) {
        return HIVE_BINARY.getTypeInfo();
    }
    if (DATE.equals(type)) {
        return HIVE_DATE.getTypeInfo();
    }
    if (type instanceof TimestampWithTimeZoneType) {
        verify(((TimestampWithTimeZoneType) type).getPrecision() == 3, "Unsupported type: %s", type);
        return HIVE_TIMESTAMP.getTypeInfo();
    }
    if (type instanceof TimestampType) {
        verify(((TimestampType) type).getPrecision() == 3, "Unsupported type: %s", type);
        return HIVE_TIMESTAMP.getTypeInfo();
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale());
    }
    if (isArrayType(type)) {
        TypeInfo elementType = translate(type.getTypeParameters().get(0));
        return getListTypeInfo(elementType);
    }
    if (isMapType(type)) {
        TypeInfo keyType = translate(type.getTypeParameters().get(0));
        TypeInfo valueType = translate(type.getTypeParameters().get(1));
        return getMapTypeInfo(keyType, valueType);
    }
    if (isRowType(type)) {
        ImmutableList.Builder<String> fieldNames = ImmutableList.builder();
        for (TypeSignatureParameter parameter : type.getTypeSignature().getParameters()) {
            if (!parameter.isNamedTypeSignature()) {
                throw new IllegalArgumentException(format("Expected all parameters to be named type, but got %s", parameter));
            }
            NamedTypeSignature namedTypeSignature = parameter.getNamedTypeSignature();
            if (namedTypeSignature.getName().isEmpty()) {
                throw new TrinoException(NOT_SUPPORTED, format("Anonymous row type is not supported in Hive. Please give each field a name: %s", type));
            }
            fieldNames.add(namedTypeSignature.getName().get());
        }
        return getStructTypeInfo(fieldNames.build(), type.getTypeParameters().stream().map(DeltaHiveTypeTranslator::translate).collect(toImmutableList()));
    }
    throw new TrinoException(NOT_SUPPORTED, format("Unsupported Delta Lake type: %s", type));
}
Also used : VarcharType(io.trino.spi.type.VarcharType) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TypeInfoFactory.getCharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getCharTypeInfo) TypeInfoFactory.getStructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getStructTypeInfo) TypeInfoFactory.getVarcharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getVarcharTypeInfo) TypeInfoFactory.getMapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getMapTypeInfo) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) TypeInfoFactory.getListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getListTypeInfo) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) TrinoException(io.trino.spi.TrinoException) TimestampType(io.trino.spi.type.TimestampType) DecimalType(io.trino.spi.type.DecimalType) CharType(io.trino.spi.type.CharType)

Example 8 with TypeSignatureParameter

use of io.trino.spi.type.TypeSignatureParameter in project trino by trinodb.

the class RowToJsonCast method specialize.

@Override
protected ScalarFunctionImplementation specialize(BoundSignature boundSignature) {
    Type type = boundSignature.getArgumentType(0);
    checkCondition(canCastToJson(type), INVALID_CAST_ARGUMENT, "Cannot cast %s to JSON", type);
    List<Type> fieldTypes = type.getTypeParameters();
    List<JsonGeneratorWriter> fieldWriters = new ArrayList<>(fieldTypes.size());
    MethodHandle methodHandle;
    if (legacyRowToJson) {
        for (Type fieldType : fieldTypes) {
            fieldWriters.add(createJsonGeneratorWriter(fieldType, true));
        }
        methodHandle = LEGACY_METHOD_HANDLE.bindTo(fieldWriters);
    } else {
        List<TypeSignatureParameter> typeSignatureParameters = type.getTypeSignature().getParameters();
        List<String> fieldNames = new ArrayList<>(fieldTypes.size());
        for (int i = 0; i < fieldTypes.size(); i++) {
            fieldNames.add(typeSignatureParameters.get(i).getNamedTypeSignature().getName().orElse(""));
            fieldWriters.add(createJsonGeneratorWriter(fieldTypes.get(i), false));
        }
        methodHandle = METHOD_HANDLE.bindTo(fieldNames).bindTo(fieldWriters);
    }
    return new ChoicesScalarFunctionImplementation(boundSignature, FAIL_ON_NULL, ImmutableList.of(NEVER_NULL), methodHandle);
}
Also used : JsonGeneratorWriter(io.trino.util.JsonUtil.JsonGeneratorWriter) JsonGeneratorWriter.createJsonGeneratorWriter(io.trino.util.JsonUtil.JsonGeneratorWriter.createJsonGeneratorWriter) Type(io.trino.spi.type.Type) OperatorType(io.trino.spi.function.OperatorType) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) ArrayList(java.util.ArrayList) TypeVariableConstraint(io.trino.metadata.TypeVariableConstraint) MethodHandle(java.lang.invoke.MethodHandle)

Example 9 with TypeSignatureParameter

use of io.trino.spi.type.TypeSignatureParameter in project trino by trinodb.

the class OrcTester method rowType.

private static Type rowType(Type... fieldTypes) {
    ImmutableList.Builder<TypeSignatureParameter> typeSignatureParameters = ImmutableList.builder();
    for (int i = 0; i < fieldTypes.length; i++) {
        String filedName = "field_" + i;
        Type fieldType = fieldTypes[i];
        typeSignatureParameters.add(TypeSignatureParameter.namedTypeParameter(new NamedTypeSignature(Optional.of(new RowFieldName(filedName)), fieldType.getTypeSignature())));
    }
    return TESTING_TYPE_MANAGER.getParameterizedType(StandardTypes.ROW, typeSignatureParameters.build());
}
Also used : TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) OrcType(io.trino.orc.metadata.OrcType) MapType(io.trino.spi.type.MapType) VarbinaryType(io.trino.spi.type.VarbinaryType) CharType(io.trino.spi.type.CharType) RowType(io.trino.spi.type.RowType) ArrayType(io.trino.spi.type.ArrayType) DecimalType(io.trino.spi.type.DecimalType) Type(io.trino.spi.type.Type) VarcharType(io.trino.spi.type.VarcharType) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) RowFieldName(io.trino.spi.type.RowFieldName) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature)

Example 10 with TypeSignatureParameter

use of io.trino.spi.type.TypeSignatureParameter in project trino by trinodb.

the class SignatureBinder method extractBoundVariables.

private static void extractBoundVariables(BoundSignature boundSignature, Signature declaredSignature, Map<String, TypeVariableConstraint> typeVariableConstraints, BoundVariables bindings, Type actualType, TypeSignature declaredTypeSignature) {
    // type without nested type parameters
    if (declaredTypeSignature.getParameters().isEmpty()) {
        String typeVariable = declaredTypeSignature.getBase();
        TypeVariableConstraint typeVariableConstraint = typeVariableConstraints.get(typeVariable);
        if (typeVariableConstraint == null) {
            return;
        }
        if (bindings.containsTypeVariable(typeVariable)) {
            Type existingTypeBinding = bindings.getTypeVariable(typeVariable);
            verifyBoundSignature(actualType.equals(existingTypeBinding), boundSignature, declaredSignature);
        } else {
            bindings.setTypeVariable(typeVariable, actualType);
        }
        return;
    }
    verifyBoundSignature(declaredTypeSignature.getBase().equalsIgnoreCase(actualType.getTypeSignature().getBase()), boundSignature, declaredSignature);
    // type with nested literal parameters
    if (isTypeWithLiteralParameters(declaredTypeSignature)) {
        for (int i = 0; i < declaredTypeSignature.getParameters().size(); i++) {
            TypeSignatureParameter typeSignatureParameter = declaredTypeSignature.getParameters().get(i);
            Long actualLongBinding = actualType.getTypeSignature().getParameters().get(i).getLongLiteral();
            if (typeSignatureParameter.getKind() == ParameterKind.VARIABLE) {
                if (bindings.containsLongVariable(typeSignatureParameter.getVariable())) {
                    Long existingLongBinding = bindings.getLongVariable(typeSignatureParameter.getVariable());
                    verifyBoundSignature(actualLongBinding.equals(existingLongBinding), boundSignature, declaredSignature);
                } else {
                    bindings.setLongVariable(typeSignatureParameter.getVariable(), actualLongBinding);
                }
            } else {
                verify(typeSignatureParameter.getKind() == ParameterKind.LONG);
                verifyBoundSignature(actualLongBinding.equals(typeSignatureParameter.getLongLiteral()), boundSignature, declaredSignature);
            }
        }
        return;
    }
    // type with nested type parameters
    List<Type> actualTypeParameters = actualType.getTypeParameters();
    // unknown types are assumed to have unknown nested types
    if (UNKNOWN.equals(actualType)) {
        actualTypeParameters = Collections.nCopies(declaredTypeSignature.getParameters().size(), UNKNOWN);
    }
    verifyBoundSignature(declaredTypeSignature.getParameters().size() == actualTypeParameters.size(), boundSignature, declaredSignature);
    for (int i = 0; i < declaredTypeSignature.getParameters().size(); i++) {
        TypeSignatureParameter typeSignatureParameter = declaredTypeSignature.getParameters().get(i);
        TypeSignature typeSignature = typeSignatureParameter.getTypeSignatureOrNamedTypeSignature().orElseThrow(() -> new UnsupportedOperationException("Types with both type parameters and literal parameters at the same time are not supported"));
        Type actualTypeParameter = actualTypeParameters.get(i);
        extractBoundVariables(boundSignature, declaredSignature, typeVariableConstraints, bindings, actualTypeParameter, typeSignature);
    }
}
Also used : JsonType(io.trino.type.JsonType) Type(io.trino.spi.type.Type) FunctionType(io.trino.type.FunctionType) RowType(io.trino.spi.type.RowType) UnknownType(io.trino.type.UnknownType) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter)

Aggregations

TypeSignatureParameter (io.trino.spi.type.TypeSignatureParameter)17 NamedTypeSignature (io.trino.spi.type.NamedTypeSignature)11 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)9 Type (io.trino.spi.type.Type)9 ImmutableList (com.google.common.collect.ImmutableList)8 TrinoException (io.trino.spi.TrinoException)8 TypeSignature (io.trino.spi.type.TypeSignature)8 RowType (io.trino.spi.type.RowType)6 VarcharType (io.trino.spi.type.VarcharType)6 ArrayList (java.util.ArrayList)6 List (java.util.List)5 CharType (io.trino.spi.type.CharType)4 DecimalType (io.trino.spi.type.DecimalType)4 RowFieldName (io.trino.spi.type.RowFieldName)4 TimestampType (io.trino.spi.type.TimestampType)3 Collectors.toList (java.util.stream.Collectors.toList)3 DecimalTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo)3 TypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)3 TypeInfoFactory.getCharTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getCharTypeInfo)3 TypeInfoFactory.getListTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getListTypeInfo)3