Java Code Examples for io.prestosql.spi.block.BlockBuilder#appendNull()

The following examples show how to use io.prestosql.spi.block.BlockBuilder#appendNull() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TimestampEncoding.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public Block decodeColumn(ColumnData columnData)
{
    int size = columnData.rowCount();
    BlockBuilder builder = type.createBlockBuilder(null, size);

    Slice slice = columnData.getSlice();
    for (int i = 0; i < size; i++) {
        int length = columnData.getLength(i);
        if (length != 0) {
            int offset = columnData.getOffset(i);
            long millis = getTimestamp(slice, offset);
            type.writeLong(builder, millis);
        }
        else {
            builder.appendNull();
        }
    }
    return builder.build();
}
 
Example 2
Source File: PartitionTransforms.java    From presto with Apache License 2.0 5 votes vote down vote up
private static Block truncateBigint(Block block, int width)
{
    BlockBuilder builder = BIGINT.createFixedSizeBlockBuilder(block.getPositionCount());
    for (int position = 0; position < block.getPositionCount(); position++) {
        if (block.isNull(position)) {
            builder.appendNull();
            continue;
        }
        long value = BIGINT.getLong(block, position);
        value -= ((value % width) + width) % width;
        BIGINT.writeLong(builder, value);
    }
    return builder.build();
}
 
Example 3
Source File: LongColumnReader.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
protected void readValue(BlockBuilder blockBuilder, Type type)
{
    if (definitionLevel == columnDescriptor.getMaxDefinitionLevel()) {
        type.writeLong(blockBuilder, valuesReader.readLong());
    }
    else if (isValueNull()) {
        blockBuilder.appendNull();
    }
}
 
Example 4
Source File: BooleanColumnReader.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
protected void readValue(BlockBuilder blockBuilder, Type type)
{
    if (definitionLevel == columnDescriptor.getMaxDefinitionLevel()) {
        type.writeBoolean(blockBuilder, valuesReader.readBoolean());
    }
    else if (isValueNull()) {
        blockBuilder.appendNull();
    }
}
 
Example 5
Source File: TestReaderProjectionsAdapter.java    From presto with Apache License 2.0 5 votes vote down vote up
private static Block createLongArrayBlock(List<Object> data)
{
    BlockBuilder builder = BIGINT.createBlockBuilder(null, data.size());
    for (int i = 0; i < data.size(); i++) {
        Long value = (Long) data.get(i);
        if (value == null) {
            builder.appendNull();
        }
        else {
            builder.writeLong(value);
        }
    }
    return builder.build();
}
 
Example 6
Source File: GeometryUnionAgg.java    From presto with Apache License 2.0 5 votes vote down vote up
@OutputFunction(GEOMETRY_TYPE_NAME)
public static void output(@AggregationState GeometryState state, BlockBuilder out)
{
    if (state.getGeometry() == null) {
        out.appendNull();
    }
    else {
        GEOMETRY.writeSlice(out, GeometrySerde.serialize(state.getGeometry()));
    }
}
 
Example 7
Source File: LongSumAggregation.java    From presto with Apache License 2.0 5 votes vote down vote up
@OutputFunction(StandardTypes.BIGINT)
public static void output(@AggregationState LongLongState state, BlockBuilder out)
{
    if (state.getFirst() == 0) {
        out.appendNull();
    }
    else {
        BigintType.BIGINT.writeLong(out, state.getSecond());
    }
}
 
Example 8
Source File: ArrayAggregationStateSerializer.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ArrayAggregationState state, BlockBuilder out)
{
    if (state.isEmpty()) {
        out.appendNull();
    }
    else {
        BlockBuilder entryBuilder = out.beginBlockEntry();
        state.forEach((block, position) -> elementType.appendTo(block, position, entryBuilder));
        out.closeEntry();
    }
}
 
Example 9
Source File: LongTimestampWithTimeZoneType.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public void appendTo(Block block, int position, BlockBuilder blockBuilder)
{
    if (block.isNull(position)) {
        blockBuilder.appendNull();
    }
    else {
        blockBuilder.writeLong(getPackedEpochMillis(block, position));
        blockBuilder.writeInt(getFraction(block, position));
        blockBuilder.closeEntry();
    }
}
 
Example 10
Source File: JsonUtil.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public void append(JsonParser parser, BlockBuilder blockBuilder)
        throws IOException
{
    Boolean result = currentTokenAsBoolean(parser);
    if (result == null) {
        blockBuilder.appendNull();
    }
    else {
        BOOLEAN.writeBoolean(blockBuilder, result);
    }
}
 
Example 11
Source File: TestTypedSet.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetElementPositionWithNull()
{
    int elementCount = 100;
    // Set initialTypedSetEntryCount to a small number to trigger rehash()
    int initialTypedSetEntryCount = 10;
    TypedSet typedSet = new TypedSet(BIGINT, initialTypedSetEntryCount, FUNCTION_NAME);
    BlockBuilder blockBuilder = BIGINT.createFixedSizeBlockBuilder(elementCount);
    for (int i = 0; i < elementCount; i++) {
        if (i % 10 == 0) {
            blockBuilder.appendNull();
        }
        else {
            BIGINT.writeLong(blockBuilder, i);
        }
        typedSet.add(blockBuilder, i);
    }

    // The internal elementBlock and hashtable of the typedSet should contain
    // all distinct non-null elements plus one null
    assertEquals(typedSet.size(), elementCount - elementCount / 10 + 1);

    int nullCount = 0;
    for (int j = 0; j < blockBuilder.getPositionCount(); j++) {
        // The null is only added to typedSet once, so the internal elementBlock subscript is shifted by nullCountMinusOne
        if (!blockBuilder.isNull(j)) {
            assertEquals(typedSet.positionOf(blockBuilder, j), j - nullCount + 1);
        }
        else {
            // The first null added to typedSet is at position 0
            assertEquals(typedSet.positionOf(blockBuilder, j), 0);
            nullCount++;
        }
    }
}
 
Example 12
Source File: JsonUtil.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public void append(JsonParser parser, BlockBuilder blockBuilder)
        throws IOException
{
    Long result = currentTokenAsBigint(parser);
    if (result == null) {
        blockBuilder.appendNull();
    }
    else {
        BIGINT.writeLong(blockBuilder, result);
    }
}
 
Example 13
Source File: RepeatFunction.java    From presto with Apache License 2.0 5 votes vote down vote up
private static Block repeatNullValues(BlockBuilder blockBuilder, long count)
{
    for (int i = 0; i < count; i++) {
        blockBuilder.appendNull();
    }
    return blockBuilder.build();
}
 
Example 14
Source File: BlockPositionState.java    From presto with Apache License 2.0 5 votes vote down vote up
static void write(Type type, BlockPositionState state, BlockBuilder out)
{
    if (state.getBlock() == null) {
        out.appendNull();
    }
    else {
        type.appendTo(state.getBlock(), state.getPosition(), out);
    }
}
 
Example 15
Source File: TestReadWrite.java    From presto with Apache License 2.0 5 votes vote down vote up
private static void generateBigintArray(Random random, BlockBuilder parentBuilder)
{
    int numberOfElements = random.nextInt(MAX_ARRAY_GENERATED_LENGTH);
    BlockBuilder builder = parentBuilder.beginBlockEntry();
    for (int i = 0; i < numberOfElements; i++) {
        if (random.nextDouble() < NULL_FRACTION) {
            builder.appendNull();
        }
        else {
            builder.writeLong(random.nextLong());
        }
    }
    parentBuilder.closeEntry();
}
 
Example 16
Source File: TestTypedSet.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetElementPositionWithProvidedNonEmptyBlockBuilder()
{
    int elementCount = 100;
    // Set initialTypedSetEntryCount to a small number to trigger rehash()
    int initialTypedSetEntryCount = 10;

    PageBuilder pageBuilder = new PageBuilder(ImmutableList.of(BIGINT));
    BlockBuilder firstBlockBuilder = pageBuilder.getBlockBuilder(0);

    for (int i = 0; i < elementCount; i++) {
        BIGINT.writeLong(firstBlockBuilder, i);
    }
    pageBuilder.declarePositions(elementCount);

    // The secondBlockBuilder should already have elementCount rows.
    BlockBuilder secondBlockBuilder = pageBuilder.getBlockBuilder(0);

    TypedSet typedSet = new TypedSet(BIGINT, Optional.empty(), secondBlockBuilder, initialTypedSetEntryCount, FUNCTION_NAME);
    BlockBuilder externalBlockBuilder = BIGINT.createFixedSizeBlockBuilder(elementCount);
    for (int i = 0; i < elementCount; i++) {
        if (i % 10 == 0) {
            externalBlockBuilder.appendNull();
        }
        else {
            BIGINT.writeLong(externalBlockBuilder, i);
        }
        typedSet.add(externalBlockBuilder, i);
    }

    assertEquals(typedSet.size(), secondBlockBuilder.getPositionCount() - elementCount);
    assertEquals(typedSet.size(), elementCount - elementCount / 10 + 1);

    for (int i = 0; i < typedSet.size(); i++) {
        int expectedPositionInSecondBlockBuilder = i + elementCount;
        assertEquals(typedSet.positionOf(secondBlockBuilder, expectedPositionInSecondBlockBuilder), expectedPositionInSecondBlockBuilder);
    }
}
 
Example 17
Source File: SliceState.java    From presto with Apache License 2.0 5 votes vote down vote up
static void write(Type type, SliceState state, BlockBuilder out)
{
    if (state.getSlice() == null) {
        out.appendNull();
    }
    else {
        type.writeSlice(out, state.getSlice());
    }
}
 
Example 18
Source File: HiveCoercionRecordCursor.java    From presto with Apache License 2.0 4 votes vote down vote up
private static void rewriteBlock(
        Type fromType,
        Type toType,
        Block block,
        int position,
        BlockBuilder blockBuilder,
        Coercer coercer,
        BridgingRecordCursor bridgingRecordCursor)
{
    Class<?> fromJavaType = fromType.getJavaType();
    if (fromJavaType == long.class) {
        bridgingRecordCursor.setValue(fromType.getLong(block, position));
    }
    else if (fromJavaType == double.class) {
        bridgingRecordCursor.setValue(fromType.getDouble(block, position));
    }
    else if (fromJavaType == boolean.class) {
        bridgingRecordCursor.setValue(fromType.getBoolean(block, position));
    }
    else if (fromJavaType == Slice.class) {
        bridgingRecordCursor.setValue(fromType.getSlice(block, position));
    }
    else if (fromJavaType == Block.class) {
        bridgingRecordCursor.setValue(fromType.getObject(block, position));
    }
    else {
        bridgingRecordCursor.setValue(null);
    }
    coercer.reset();
    Class<?> toJaveType = toType.getJavaType();
    if (coercer.isNull(bridgingRecordCursor, 0)) {
        blockBuilder.appendNull();
    }
    else if (toJaveType == long.class) {
        toType.writeLong(blockBuilder, coercer.getLong(bridgingRecordCursor, 0));
    }
    else if (toJaveType == double.class) {
        toType.writeDouble(blockBuilder, coercer.getDouble(bridgingRecordCursor, 0));
    }
    else if (toJaveType == boolean.class) {
        toType.writeBoolean(blockBuilder, coercer.getBoolean(bridgingRecordCursor, 0));
    }
    else if (toJaveType == Slice.class) {
        toType.writeSlice(blockBuilder, coercer.getSlice(bridgingRecordCursor, 0));
    }
    else if (toJaveType == Block.class) {
        toType.writeObject(blockBuilder, coercer.getObject(bridgingRecordCursor, 0));
    }
    else {
        throw new PrestoException(NOT_SUPPORTED, format("Unsupported coercion from %s to %s", fromType.getDisplayName(), toType.getDisplayName()));
    }
    coercer.reset();
    bridgingRecordCursor.close();
}
 
Example 19
Source File: MongoPageSource.java    From presto with Apache License 2.0 4 votes vote down vote up
private void appendTo(Type type, Object value, BlockBuilder output)
{
    if (value == null) {
        output.appendNull();
        return;
    }

    Class<?> javaType = type.getJavaType();
    try {
        if (javaType == boolean.class) {
            type.writeBoolean(output, (Boolean) value);
        }
        else if (javaType == long.class) {
            if (type.equals(BIGINT)) {
                type.writeLong(output, ((Number) value).longValue());
            }
            else if (type.equals(INTEGER)) {
                type.writeLong(output, ((Number) value).intValue());
            }
            else if (type.equals(SMALLINT)) {
                type.writeLong(output, Shorts.checkedCast(((Number) value).longValue()));
            }
            else if (type.equals(TINYINT)) {
                type.writeLong(output, SignedBytes.checkedCast(((Number) value).longValue()));
            }
            else if (type.equals(REAL)) {
                //noinspection NumericCastThatLosesPrecision
                type.writeLong(output, floatToIntBits(((float) ((Number) value).doubleValue())));
            }
            else if (type instanceof DecimalType) {
                type.writeLong(output, encodeShortScaledValue(((Decimal128) value).bigDecimalValue(), ((DecimalType) type).getScale()));
            }
            else if (type.equals(DATE)) {
                long utcMillis = ((Date) value).getTime();
                type.writeLong(output, TimeUnit.MILLISECONDS.toDays(utcMillis));
            }
            else if (type.equals(TIME)) {
                type.writeLong(output, UTC_CHRONOLOGY.millisOfDay().get(((Date) value).getTime()));
            }
            else if (type.equals(TIMESTAMP)) {
                // TODO provide correct TIMESTAMP mapping, and respecting session.isLegacyTimestamp()
                type.writeLong(output, ((Date) value).getTime());
            }
            else if (type.equals(TIMESTAMP_WITH_TIME_ZONE)) {
                type.writeLong(output, packDateTimeWithZone(((Date) value).getTime(), UTC_KEY));
            }
            else {
                throw new PrestoException(GENERIC_INTERNAL_ERROR, "Unhandled type for " + javaType.getSimpleName() + ":" + type.getTypeSignature());
            }
        }
        else if (javaType == double.class) {
            type.writeDouble(output, ((Number) value).doubleValue());
        }
        else if (javaType == Slice.class) {
            writeSlice(output, type, value);
        }
        else if (javaType == Block.class) {
            writeBlock(output, type, value);
        }
        else {
            throw new PrestoException(GENERIC_INTERNAL_ERROR, "Unhandled type for " + javaType.getSimpleName() + ":" + type.getTypeSignature());
        }
    }
    catch (ClassCastException ignore) {
        // TODO remove (fail clearly), or hide behind a toggle
        // returns null instead of raising exception
        output.appendNull();
    }
}
 
Example 20
Source File: JoniRegexpReplaceLambdaFunction.java    From presto with Apache License 2.0 4 votes vote down vote up
@LiteralParameters("x")
@SqlType("varchar")
@SqlNullable
public Slice regexpReplace(
        @SqlType("varchar") Slice source,
        @SqlType(JoniRegexpType.NAME) JoniRegexp pattern,
        @SqlType("function(array(varchar), varchar(x))") UnaryFunctionInterface replaceFunction)
{
    // If there is no match we can simply return the original source without doing copy.
    Matcher matcher = pattern.matcher(source.getBytes());
    if (matcher.search(0, source.length(), Option.DEFAULT) == -1) {
        return source;
    }

    SliceOutput output = new DynamicSliceOutput(source.length());

    // Prepare a BlockBuilder that will be used to create the target block
    // that will be passed to the lambda function.
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);

    int groupCount = pattern.regex().numberOfCaptures();
    int appendPosition = 0;
    int nextStart;

    do {
        // nextStart is the same as the last appendPosition, unless the last match was zero-width.
        if (matcher.getEnd() == matcher.getBegin()) {
            if (matcher.getBegin() < source.length()) {
                nextStart = matcher.getEnd() + lengthOfCodePointFromStartByte(source.getByte(matcher.getBegin()));
            }
            else {
                // last match is empty and we matched end of source, move past the source length to terminate the loop
                nextStart = matcher.getEnd() + 1;
            }
        }
        else {
            nextStart = matcher.getEnd();
        }

        // Append the un-matched part
        Slice unmatched = source.slice(appendPosition, matcher.getBegin() - appendPosition);
        appendPosition = matcher.getEnd();
        output.appendBytes(unmatched);

        // Append the capturing groups to the target block that will be passed to lambda
        Region matchedRegion = matcher.getEagerRegion();
        for (int i = 1; i <= groupCount; i++) {
            // Add to the block builder if the matched region is not null. In Joni null is represented as [-1, -1]
            if (matchedRegion.beg[i] >= 0 && matchedRegion.end[i] >= 0) {
                VARCHAR.writeSlice(blockBuilder, source, matchedRegion.beg[i], matchedRegion.end[i] - matchedRegion.beg[i]);
            }
            else {
                blockBuilder.appendNull();
            }
        }
        pageBuilder.declarePositions(groupCount);
        Block target = blockBuilder.getRegion(blockBuilder.getPositionCount() - groupCount, groupCount);

        // Call the lambda function to replace the block, and append the result to output
        Slice replaced = (Slice) replaceFunction.apply(target);
        if (replaced == null) {
            // replacing a substring with null (unknown) makes the entire string null
            return null;
        }
        output.appendBytes(replaced);
    }
    while (matcher.search(nextStart, source.length(), Option.DEFAULT) != -1);

    // Append the last un-matched part
    output.writeBytes(source, appendPosition, source.length() - appendPosition);
    return output.slice();
}