Search in sources :

Example 1 with FixedWidthType

use of io.trino.spi.type.FixedWidthType 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)

Aggregations

Slice (io.airlift.slice.Slice)1 BlockBuilder (io.trino.spi.block.BlockBuilder)1 FixedWidthType (io.trino.spi.type.FixedWidthType)1 VarcharType (io.trino.spi.type.VarcharType)1