Search in sources :

Example 21 with VarcharType

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

the class OrcType method toOrcType.

private static List<OrcType> toOrcType(int nextFieldTypeIndex, Type type) {
    if (BOOLEAN.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.BOOLEAN));
    }
    if (TINYINT.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.BYTE));
    }
    if (SMALLINT.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.SHORT));
    }
    if (INTEGER.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.INT));
    }
    if (BIGINT.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.LONG));
    }
    if (DOUBLE.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.DOUBLE));
    }
    if (REAL.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.FLOAT));
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        if (varcharType.isUnbounded()) {
            return ImmutableList.of(new OrcType(OrcTypeKind.STRING));
        }
        return ImmutableList.of(new OrcType(OrcTypeKind.VARCHAR, varcharType.getBoundedLength()));
    }
    if (type instanceof CharType) {
        return ImmutableList.of(new OrcType(OrcTypeKind.CHAR, ((CharType) type).getLength()));
    }
    if (VARBINARY.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.BINARY));
    }
    if (DATE.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.DATE));
    }
    if (TIMESTAMP_MILLIS.equals(type) || TIMESTAMP_MICROS.equals(type) || TIMESTAMP_NANOS.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.TIMESTAMP));
    }
    if (TIMESTAMP_TZ_MILLIS.equals(type) || TIMESTAMP_TZ_MICROS.equals(type) || TIMESTAMP_TZ_NANOS.equals(type)) {
        return ImmutableList.of(new OrcType(OrcTypeKind.TIMESTAMP_INSTANT));
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return ImmutableList.of(new OrcType(OrcTypeKind.DECIMAL, decimalType.getPrecision(), decimalType.getScale()));
    }
    if (type instanceof ArrayType) {
        return createOrcArrayType(nextFieldTypeIndex, type.getTypeParameters().get(0));
    }
    if (type instanceof MapType) {
        return createOrcMapType(nextFieldTypeIndex, type.getTypeParameters().get(0), type.getTypeParameters().get(1));
    }
    if (type instanceof RowType) {
        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));
        }
        List<Type> fieldTypes = type.getTypeParameters();
        return createOrcRowType(nextFieldTypeIndex, fieldNames, fieldTypes);
    }
    throw new TrinoException(NOT_SUPPORTED, format("Unsupported Hive type: %s", type));
}
Also used : VarcharType(io.trino.spi.type.VarcharType) ArrayList(java.util.ArrayList) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) Type(io.trino.spi.type.Type) VarcharType(io.trino.spi.type.VarcharType) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) CharType(io.trino.spi.type.CharType) DecimalType(io.trino.spi.type.DecimalType) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) DecimalType(io.trino.spi.type.DecimalType) TrinoException(io.trino.spi.TrinoException) CharType(io.trino.spi.type.CharType)

Example 22 with VarcharType

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

the class ImplementMinMax method rewrite.

@Override
public Optional<JdbcExpression> rewrite(AggregateFunction aggregateFunction, Captures captures, RewriteContext<String> context) {
    Variable argument = captures.get(ARGUMENT);
    JdbcColumnHandle columnHandle = (JdbcColumnHandle) context.getAssignment(argument.getName());
    verify(columnHandle.getColumnType().equals(aggregateFunction.getOutputType()));
    // Remote database is case insensitive or sorts values differently from Trino
    if (!isRemoteCollationSensitive && (columnHandle.getColumnType() instanceof CharType || columnHandle.getColumnType() instanceof VarcharType)) {
        return Optional.empty();
    }
    return Optional.of(new JdbcExpression(format("%s(%s)", aggregateFunction.getFunctionName(), context.rewriteExpression(argument).orElseThrow()), columnHandle.getJdbcTypeHandle()));
}
Also used : Variable(io.trino.spi.expression.Variable) VarcharType(io.trino.spi.type.VarcharType) JdbcColumnHandle(io.trino.plugin.jdbc.JdbcColumnHandle) JdbcExpression(io.trino.plugin.jdbc.JdbcExpression) CharType(io.trino.spi.type.CharType)

Example 23 with VarcharType

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

the class BlackHolePageSourceProvider method createZeroBlock.

private Block createZeroBlock(Type type, int rowsCount, Slice constantSlice) {
    checkArgument(isSupportedType(type), "Unsupported type [%s]", type);
    Slice slice;
    // do not exceed varchar limit
    if (type instanceof VarcharType && !((VarcharType) type).isUnbounded()) {
        slice = constantSlice.slice(0, Math.min(((VarcharType) type).getBoundedLength(), constantSlice.length()));
    } else {
        slice = constantSlice;
    }
    BlockBuilder builder;
    if (type instanceof FixedWidthType) {
        builder = type.createBlockBuilder(null, rowsCount);
    } else {
        builder = type.createBlockBuilder(null, rowsCount, slice.length());
    }
    for (int i = 0; i < rowsCount; i++) {
        Class<?> javaType = type.getJavaType();
        if (javaType == boolean.class) {
            type.writeBoolean(builder, false);
        } else if (javaType == long.class) {
            type.writeLong(builder, 0);
        } else if (javaType == double.class) {
            type.writeDouble(builder, 0.0);
        } else if (javaType == Slice.class) {
            requireNonNull(slice, "slice is null");
            type.writeSlice(builder, slice, 0, slice.length());
        } else if (isLongDecimal(type)) {
            type.writeObject(builder, Int128.ZERO);
        } else {
            throw new UnsupportedOperationException("Unknown javaType: " + javaType.getName());
        }
    }
    return builder.build();
}
Also used : VarcharType(io.trino.spi.type.VarcharType) Slice(io.airlift.slice.Slice) BlockBuilder(io.trino.spi.block.BlockBuilder) FixedWidthType(io.trino.spi.type.FixedWidthType)

Example 24 with VarcharType

use of io.trino.spi.type.VarcharType 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 25 with VarcharType

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

the class IcebergUtil method deserializePartitionValue.

public static Object deserializePartitionValue(Type type, String valueString, String name) {
    if (valueString == null) {
        return null;
    }
    try {
        if (type.equals(BOOLEAN)) {
            if (valueString.equalsIgnoreCase("true")) {
                return true;
            }
            if (valueString.equalsIgnoreCase("false")) {
                return false;
            }
            throw new IllegalArgumentException();
        }
        if (type.equals(INTEGER)) {
            return parseLong(valueString);
        }
        if (type.equals(BIGINT)) {
            return parseLong(valueString);
        }
        if (type.equals(REAL)) {
            return (long) floatToRawIntBits(parseFloat(valueString));
        }
        if (type.equals(DOUBLE)) {
            return parseDouble(valueString);
        }
        if (type.equals(DATE)) {
            return parseLong(valueString);
        }
        if (type.equals(TIME_MICROS)) {
            return parseLong(valueString) * PICOSECONDS_PER_MICROSECOND;
        }
        if (type.equals(TIMESTAMP_MICROS)) {
            return parseLong(valueString);
        }
        if (type.equals(TIMESTAMP_TZ_MICROS)) {
            return timestampTzFromMicros(parseLong(valueString));
        }
        if (type instanceof VarcharType) {
            Slice value = utf8Slice(valueString);
            VarcharType varcharType = (VarcharType) type;
            if (!varcharType.isUnbounded() && SliceUtf8.countCodePoints(value) > varcharType.getBoundedLength()) {
                throw new IllegalArgumentException();
            }
            return value;
        }
        if (type.equals(VarbinaryType.VARBINARY)) {
            return Slices.wrappedBuffer(Base64.getDecoder().decode(valueString));
        }
        if (type.equals(UuidType.UUID)) {
            return javaUuidToTrinoUuid(UUID.fromString(valueString));
        }
        if (isShortDecimal(type) || isLongDecimal(type)) {
            DecimalType decimalType = (DecimalType) type;
            BigDecimal decimal = new BigDecimal(valueString);
            decimal = decimal.setScale(decimalType.getScale(), BigDecimal.ROUND_UNNECESSARY);
            if (decimal.precision() > decimalType.getPrecision()) {
                throw new IllegalArgumentException();
            }
            BigInteger unscaledValue = decimal.unscaledValue();
            return isShortDecimal(type) ? unscaledValue.longValue() : Int128.valueOf(unscaledValue);
        }
    } catch (IllegalArgumentException e) {
        throw new TrinoException(ICEBERG_INVALID_PARTITION_VALUE, format("Invalid partition value '%s' for %s partition key: %s", valueString, type.getDisplayName(), name));
    }
    // Iceberg tables don't partition by non-primitive-type columns.
    throw new TrinoException(GENERIC_INTERNAL_ERROR, "Invalid partition type " + type.toString());
}
Also used : VarcharType(io.trino.spi.type.VarcharType) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) DecimalType(io.trino.spi.type.DecimalType) BigInteger(java.math.BigInteger) TrinoException(io.trino.spi.TrinoException) BigDecimal(java.math.BigDecimal)

Aggregations

VarcharType (io.trino.spi.type.VarcharType)83 DecimalType (io.trino.spi.type.DecimalType)50 CharType (io.trino.spi.type.CharType)47 Type (io.trino.spi.type.Type)37 TrinoException (io.trino.spi.TrinoException)35 ArrayType (io.trino.spi.type.ArrayType)29 TimestampType (io.trino.spi.type.TimestampType)23 VarcharType.createUnboundedVarcharType (io.trino.spi.type.VarcharType.createUnboundedVarcharType)23 MapType (io.trino.spi.type.MapType)21 Slice (io.airlift.slice.Slice)20 DecimalType.createDecimalType (io.trino.spi.type.DecimalType.createDecimalType)18 RowType (io.trino.spi.type.RowType)18 ImmutableList (com.google.common.collect.ImmutableList)17 TimeType (io.trino.spi.type.TimeType)15 VarbinaryType (io.trino.spi.type.VarbinaryType)15 Block (io.trino.spi.block.Block)14 TimestampWithTimeZoneType (io.trino.spi.type.TimestampWithTimeZoneType)14 BigDecimal (java.math.BigDecimal)14 List (java.util.List)14 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)12