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

The following examples show how to use io.prestosql.spi.block.BlockBuilder#appendStructure() . 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: TestArrayBlock.java    From presto with Apache License 2.0 6 votes vote down vote up
private static BlockBuilder createBlockBuilderWithValues(long[][][] expectedValues)
{
    BlockBuilder blockBuilder = new ArrayBlockBuilder(new ArrayBlockBuilder(BIGINT, null, 100, 100), null, 100);
    for (long[][] expectedValue : expectedValues) {
        if (expectedValue == null) {
            blockBuilder.appendNull();
        }
        else {
            BlockBuilder intermediateBlockBuilder = new ArrayBlockBuilder(BIGINT, null, 100, 100);
            for (int j = 0; j < expectedValue.length; j++) {
                if (expectedValue[j] == null) {
                    intermediateBlockBuilder.appendNull();
                }
                else {
                    BlockBuilder innerMostBlockBuilder = BIGINT.createBlockBuilder(null, expectedValue.length);
                    for (long v : expectedValue[j]) {
                        BIGINT.writeLong(innerMostBlockBuilder, v);
                    }
                    intermediateBlockBuilder.appendStructure(innerMostBlockBuilder.build());
                }
            }
            blockBuilder.appendStructure(intermediateBlockBuilder.build());
        }
    }
    return blockBuilder;
}
 
Example 2
Source File: TestArrayBlock.java    From presto with Apache License 2.0 6 votes vote down vote up
private static BlockBuilder writeValues(long[][] expectedValues, BlockBuilder blockBuilder)
{
    for (long[] expectedValue : expectedValues) {
        if (expectedValue == null) {
            blockBuilder.appendNull();
        }
        else {
            BlockBuilder elementBlockBuilder = BIGINT.createBlockBuilder(null, expectedValue.length);
            for (long v : expectedValue) {
                BIGINT.writeLong(elementBlockBuilder, v);
            }
            blockBuilder.appendStructure(elementBlockBuilder);
        }
    }
    return blockBuilder;
}
 
Example 3
Source File: TestArrayBlock.java    From presto with Apache License 2.0 6 votes vote down vote up
private static BlockBuilder createBlockBuilderWithValues(Slice[][] expectedValues)
{
    BlockBuilder blockBuilder = new ArrayBlockBuilder(VARCHAR, null, 100, 100);
    for (Slice[] expectedValue : expectedValues) {
        if (expectedValue == null) {
            blockBuilder.appendNull();
        }
        else {
            BlockBuilder elementBlockBuilder = VARCHAR.createBlockBuilder(null, expectedValue.length);
            for (Slice v : expectedValue) {
                VARCHAR.writeSlice(elementBlockBuilder, v);
            }
            blockBuilder.appendStructure(elementBlockBuilder.build());
        }
    }
    return blockBuilder;
}
 
Example 4
Source File: TestColumnarArray.java    From presto with Apache License 2.0 6 votes vote down vote up
public static BlockBuilder createBlockBuilderWithValues(Slice[][] expectedValues)
{
    BlockBuilder blockBuilder = new ArrayBlockBuilder(VARCHAR, null, 100, 100);
    for (Slice[] expectedValue : expectedValues) {
        if (expectedValue == null) {
            blockBuilder.appendNull();
        }
        else {
            BlockBuilder elementBlockBuilder = VARCHAR.createBlockBuilder(null, expectedValue.length);
            for (Slice v : expectedValue) {
                if (v == null) {
                    elementBlockBuilder.appendNull();
                }
                else {
                    VARCHAR.writeSlice(elementBlockBuilder, v);
                }
            }
            blockBuilder.appendStructure(elementBlockBuilder.build());
        }
    }
    return blockBuilder;
}
 
Example 5
Source File: RowHashCodeOperator.java    From presto with Apache License 2.0 5 votes vote down vote up
@UsedByGeneratedCode
public static long hash(Type rowType, Block block)
{
    BlockBuilder blockBuilder = rowType.createBlockBuilder(null, 1);
    blockBuilder.appendStructure(block);
    return rowType.hash(blockBuilder.build(), 0);
}
 
Example 6
Source File: BenchmarkUnnestOperator.java    From presto with Apache License 2.0 5 votes vote down vote up
private Block produceArrayBlock(ArrayType arrayType, int entries, double primitiveNullsRatio, double rowNullsRatio)
{
    BlockBuilder builder = arrayType.createBlockBuilder(null, 100);
    Type elementType = arrayType.getElementType();

    for (int i = 0; i < entries; i++) {
        int arrayLength = minNestedCardinality + random.nextInt(maxNestedCardinality - minNestedCardinality);
        builder.appendStructure(produceBlock(elementType, arrayLength, primitiveNullsRatio, rowNullsRatio));
    }
    return builder.build();
}
 
Example 7
Source File: TestingUnnesterUtil.java    From presto with Apache License 2.0 5 votes vote down vote up
public static Block createArrayBlock(Slice[][] values)
{
    BlockBuilder blockBuilder = new ArrayBlockBuilder(VARCHAR, null, 100, 100);
    for (Slice[] expectedValue : values) {
        if (expectedValue == null) {
            blockBuilder.appendNull();
        }
        else {
            Block elementBlock = createSimpleBlock(expectedValue);
            blockBuilder.appendStructure(elementBlock);
        }
    }
    return blockBuilder.build();
}
 
Example 8
Source File: TestingUnnesterUtil.java    From presto with Apache License 2.0 5 votes vote down vote up
public static Block createArrayBlockOfRowBlocks(Slice[][][] elements, RowType rowType)
{
    BlockBuilder arrayBlockBuilder = new ArrayBlockBuilder(rowType, null, 100, 100);
    for (int i = 0; i < elements.length; i++) {
        if (elements[i] == null) {
            arrayBlockBuilder.appendNull();
        }
        else {
            Slice[][] expectedValues = elements[i];
            BlockBuilder elementBlockBuilder = rowType.createBlockBuilder(null, elements[i].length);
            for (Slice[] expectedValue : expectedValues) {
                if (expectedValue == null) {
                    elementBlockBuilder.appendNull();
                }
                else {
                    BlockBuilder entryBuilder = elementBlockBuilder.beginBlockEntry();
                    for (Slice v : expectedValue) {
                        if (v == null) {
                            entryBuilder.appendNull();
                        }
                        else {
                            VARCHAR.writeSlice(entryBuilder, v);
                        }
                    }
                    elementBlockBuilder.closeEntry();
                }
            }
            arrayBlockBuilder.appendStructure(elementBlockBuilder.build());
        }
    }
    return arrayBlockBuilder.build();
}
 
Example 9
Source File: TestBlockBuilder.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test
public void testNewBlockBuilderLike()
{
    ArrayType longArrayType = new ArrayType(BIGINT);
    ArrayType arrayType = new ArrayType(longArrayType);
    List<Type> channels = ImmutableList.of(BIGINT, VARCHAR, arrayType);
    PageBuilder pageBuilder = new PageBuilder(channels);
    BlockBuilder bigintBlockBuilder = pageBuilder.getBlockBuilder(0);
    BlockBuilder varcharBlockBuilder = pageBuilder.getBlockBuilder(1);
    BlockBuilder arrayBlockBuilder = pageBuilder.getBlockBuilder(2);

    for (int i = 0; i < 100; i++) {
        BIGINT.writeLong(bigintBlockBuilder, i);
        VARCHAR.writeSlice(varcharBlockBuilder, Slices.utf8Slice("test" + i));
        Block longArrayBlock = new ArrayType(BIGINT)
                .createBlockBuilder(null, 1)
                .appendStructure(BIGINT.createBlockBuilder(null, 2).writeLong(i).closeEntry().writeLong(i * 2).closeEntry().build());
        arrayBlockBuilder.appendStructure(longArrayBlock);
        pageBuilder.declarePosition();
    }

    PageBuilder newPageBuilder = pageBuilder.newPageBuilderLike();
    for (int i = 0; i < channels.size(); i++) {
        assertEquals(newPageBuilder.getType(i), pageBuilder.getType(i));
        // we should get new block builder instances
        assertNotEquals(pageBuilder.getBlockBuilder(i), newPageBuilder.getBlockBuilder(i));
        assertEquals(newPageBuilder.getBlockBuilder(i).getPositionCount(), 0);
        assertTrue(newPageBuilder.getBlockBuilder(i).getRetainedSizeInBytes() < pageBuilder.getBlockBuilder(i).getRetainedSizeInBytes());
    }
}
 
Example 10
Source File: AbstractTestBlock.java    From presto with Apache License 2.0 5 votes vote down vote up
private static Block copyBlockViaWriteStructure(Block block, Supplier<BlockBuilder> newBlockBuilder)
{
    BlockBuilder blockBuilder = newBlockBuilder.get();
    for (int i = 0; i < block.getPositionCount(); i++) {
        if (block.isNull(i)) {
            blockBuilder.appendNull();
        }
        else {
            blockBuilder.appendStructure(block.getObject(i, Block.class));
        }
    }
    return blockBuilder.build();
}
 
Example 11
Source File: MapType.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public void writeObject(BlockBuilder blockBuilder, Object value)
{
    if (!(value instanceof SingleMapBlock)) {
        throw new IllegalArgumentException("Maps must be represented with SingleMapBlock");
    }
    blockBuilder.appendStructure((Block) value);
}
 
Example 12
Source File: BlockAssertions.java    From presto with Apache License 2.0 4 votes vote down vote up
public static Block createRowBlock(List<Type> fieldTypes, Object[]... rows)
{
    BlockBuilder rowBlockBuilder = new RowBlockBuilder(fieldTypes, null, 1);
    for (Object[] row : rows) {
        if (row == null) {
            rowBlockBuilder.appendNull();
            continue;
        }
        BlockBuilder singleRowBlockWriter = rowBlockBuilder.beginBlockEntry();
        for (Object fieldValue : row) {
            if (fieldValue == null) {
                singleRowBlockWriter.appendNull();
                continue;
            }

            if (fieldValue instanceof String) {
                VARCHAR.writeSlice(singleRowBlockWriter, utf8Slice((String) fieldValue));
            }
            else if (fieldValue instanceof Slice) {
                VARBINARY.writeSlice(singleRowBlockWriter, (Slice) fieldValue);
            }
            else if (fieldValue instanceof Double) {
                DOUBLE.writeDouble(singleRowBlockWriter, ((Double) fieldValue).doubleValue());
            }
            else if (fieldValue instanceof Long) {
                BIGINT.writeLong(singleRowBlockWriter, ((Long) fieldValue).longValue());
            }
            else if (fieldValue instanceof Boolean) {
                BOOLEAN.writeBoolean(singleRowBlockWriter, ((Boolean) fieldValue).booleanValue());
            }
            else if (fieldValue instanceof Block) {
                singleRowBlockWriter.appendStructure((Block) fieldValue);
            }
            else if (fieldValue instanceof Integer) {
                INTEGER.writeLong(singleRowBlockWriter, ((Integer) fieldValue).intValue());
            }
            else {
                throw new IllegalArgumentException();
            }
        }
        rowBlockBuilder.closeEntry();
    }

    return rowBlockBuilder.build();
}
 
Example 13
Source File: ArrayType.java    From presto with Apache License 2.0 4 votes vote down vote up
@Override
public void writeObject(BlockBuilder blockBuilder, Object value)
{
    blockBuilder.appendStructure((Block) value);
}
 
Example 14
Source File: RowType.java    From presto with Apache License 2.0 4 votes vote down vote up
@Override
public void writeObject(BlockBuilder blockBuilder, Object value)
{
    blockBuilder.appendStructure((Block) value);
}
 
Example 15
Source File: TestReaderProjectionsAdapter.java    From presto with Apache License 2.0 4 votes vote down vote up
private static Block createProjectedColumnBlock(Block data, Type finalType, Type blockType, List<Integer> dereferences)
{
    if (dereferences.size() == 0) {
        return data;
    }

    BlockBuilder builder = finalType.createBlockBuilder(null, data.getPositionCount());

    for (int i = 0; i < data.getPositionCount(); i++) {
        Type sourceType = blockType;

        Block currentData = null;
        boolean isNull = data.isNull(i);

        if (!isNull) {
            // Get SingleRowBlock corresponding to element at position i
            currentData = data.getObject(i, Block.class);
        }

        // Apply all dereferences except for the last one, because the type can be different
        for (int j = 0; j < dereferences.size() - 1; j++) {
            if (isNull) {
                // If null element is discovered at any dereferencing step, break
                break;
            }

            checkArgument(sourceType instanceof RowType);
            if (currentData.isNull(dereferences.get(j))) {
                currentData = null;
            }
            else {
                sourceType = ((RowType) sourceType).getFields().get(dereferences.get(j)).getType();
                currentData = currentData.getObject(dereferences.get(j), Block.class);
            }

            isNull = isNull || (currentData == null);
        }

        if (isNull) {
            // Append null if any of the elements in the dereference chain were null
            builder.appendNull();
        }
        else {
            int lastDereference = dereferences.get(dereferences.size() - 1);

            if (currentData.isNull(lastDereference)) {
                // Append null if the last dereference is null
                builder.appendNull();
            }
            else {
                // Append actual values otherwise
                if (finalType.equals(BIGINT)) {
                    Long value = currentData.getLong(lastDereference, 0);
                    builder.writeLong(value);
                }
                else if (finalType instanceof RowType) {
                    Block block = currentData.getObject(lastDereference, Block.class);
                    builder.appendStructure(block);
                }
                else {
                    throw new UnsupportedOperationException();
                }
            }
        }
    }

    return builder.build();
}