Java Code Examples for io.prestosql.spi.block.Block#getPositionCount()

The following examples show how to use io.prestosql.spi.block.Block#getPositionCount() . 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: AbstractTestBlock.java    From presto with Apache License 2.0 6 votes vote down vote up
private void assertBlockSize(Block block)
{
    // Asserting on `block` is not very effective because most blocks passed to this method is compact.
    // Therefore, we split the `block` into two and assert again.
    long expectedBlockSize = copyBlockViaBlockSerde(block).getSizeInBytes();
    assertEquals(block.getSizeInBytes(), expectedBlockSize);
    assertEquals(block.getRegionSizeInBytes(0, block.getPositionCount()), expectedBlockSize);

    List<Block> splitBlock = splitBlock(block, 2);
    Block firstHalf = splitBlock.get(0);
    long expectedFirstHalfSize = copyBlockViaBlockSerde(firstHalf).getSizeInBytes();
    assertEquals(firstHalf.getSizeInBytes(), expectedFirstHalfSize);
    assertEquals(block.getRegionSizeInBytes(0, firstHalf.getPositionCount()), expectedFirstHalfSize);
    Block secondHalf = splitBlock.get(1);
    long expectedSecondHalfSize = copyBlockViaBlockSerde(secondHalf).getSizeInBytes();
    assertEquals(secondHalf.getSizeInBytes(), expectedSecondHalfSize);
    assertEquals(block.getRegionSizeInBytes(firstHalf.getPositionCount(), secondHalf.getPositionCount()), expectedSecondHalfSize);

    boolean[] positions = new boolean[block.getPositionCount()];
    fill(positions, 0, firstHalf.getPositionCount(), true);
    assertEquals(block.getPositionsSizeInBytes(positions), expectedFirstHalfSize);
    fill(positions, true);
    assertEquals(block.getPositionsSizeInBytes(positions), expectedBlockSize);
    fill(positions, 0, firstHalf.getPositionCount(), false);
    assertEquals(block.getPositionsSizeInBytes(positions), expectedSecondHalfSize);
}
 
Example 2
Source File: FloatEncoding.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public void encodeColumn(Block block, SliceOutput output, EncodeOutput encodeOutput)
{
    for (int position = 0; position < block.getPositionCount(); position++) {
        if (block.isNull(position)) {
            output.writeBytes(nullSequence);
        }
        else {
            float value = Float.intBitsToFloat((int) type.getLong(block, position));
            buffer.setLength(0);
            buffer.append(value);
            for (int index = 0; index < buffer.length(); index++) {
                output.writeByte(buffer.charAt(index));
            }
        }
        encodeOutput.closeEntry();
    }
}
 
Example 3
Source File: StructuralTestUtil.java    From presto with Apache License 2.0 6 votes vote down vote up
public static boolean mapBlocksEqual(Type keyType, Type valueType, Block block1, Block block2)
{
    if (block1.getPositionCount() != block2.getPositionCount()) {
        return false;
    }
    for (int i = 0; i < block1.getPositionCount(); i += 2) {
        if (block1.isNull(i) != block2.isNull(i) || block1.isNull(i + 1) != block2.isNull(i + 1)) {
            return false;
        }
        if (!block1.isNull(i) && !keyType.equalTo(block1, i, block2, i)) {
            return false;
        }
        if (!block1.isNull(i + 1) && !valueType.equalTo(block1, i + 1, block2, i + 1)) {
            return false;
        }
    }
    return true;
}
 
Example 4
Source File: ArrayType.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public boolean equalTo(Block leftBlock, int leftPosition, Block rightBlock, int rightPosition)
{
    Block leftArray = leftBlock.getObject(leftPosition, Block.class);
    Block rightArray = rightBlock.getObject(rightPosition, Block.class);

    if (leftArray.getPositionCount() != rightArray.getPositionCount()) {
        return false;
    }

    for (int i = 0; i < leftArray.getPositionCount(); i++) {
        checkElementNotNull(leftArray.isNull(i), ARRAY_NULL_ELEMENT_MSG);
        checkElementNotNull(rightArray.isNull(i), ARRAY_NULL_ELEMENT_MSG);
        if (!elementType.equalTo(leftArray, i, rightArray, i)) {
            return false;
        }
    }

    return true;
}
 
Example 5
Source File: LongEncoding.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public void encodeColumn(Block block, SliceOutput output, EncodeOutput encodeOutput)
{
    for (int position = 0; position < block.getPositionCount(); position++) {
        if (block.isNull(position)) {
            output.writeBytes(nullSequence);
        }
        else {
            long value = type.getLong(block, position);
            buffer.setLength(0);
            buffer.append(value);
            for (int index = 0; index < buffer.length(); index++) {
                output.writeByte(buffer.charAt(index));
            }
        }
        encodeOutput.closeEntry();
    }
}
 
Example 6
Source File: FloatColumnWriter.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public void writeBlock(Block block)
{
    checkState(!closed);
    checkArgument(block.getPositionCount() > 0, "Block is empty");

    // record nulls
    for (int position = 0; position < block.getPositionCount(); position++) {
        presentStream.writeBoolean(!block.isNull(position));
    }

    // record values
    for (int position = 0; position < block.getPositionCount(); position++) {
        if (!block.isNull(position)) {
            int intBits = (int) type.getLong(block, position);
            float value = intBitsToFloat(intBits);
            dataStream.writeFloat(value);
            statisticsBuilder.addValue(value);
        }
    }
}
 
Example 7
Source File: BinaryEncoding.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public void encodeColumn(Block block, SliceOutput output, EncodeOutput encodeOutput)
{
    for (int position = 0; position < block.getPositionCount(); position++) {
        if (block.isNull(position)) {
            output.writeBytes(nullSequence);
        }
        else {
            Slice slice = type.getSlice(block, position);
            byte[] data = slice.getBytes();
            slice = Slices.wrappedBuffer(base64Encoder.encode(data));
            output.writeBytes(slice);
        }
        encodeOutput.closeEntry();
    }
}
 
Example 8
Source File: ListEncoding.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public void encodeValueInto(int depth, Block block, int position, SliceOutput output)
        throws RcFileCorruptionException
{
    byte separator = getSeparator(depth);

    Block list = block.getObject(position, Block.class);
    for (int elementIndex = 0; elementIndex < list.getPositionCount(); elementIndex++) {
        if (elementIndex > 0) {
            output.writeByte(separator);
        }
        if (list.isNull(elementIndex)) {
            output.writeBytes(nullSequence);
        }
        else {
            elementEncoding.encodeValueInto(depth + 1, list, elementIndex, output);
        }
    }
}
 
Example 9
Source File: AbstractTestBlock.java    From presto with Apache License 2.0 5 votes vote down vote up
protected static void assertEstimatedDataSizeForStats(BlockBuilder blockBuilder, Slice[] expectedSliceValues)
{
    Block block = blockBuilder.build();
    assertEquals(block.getPositionCount(), expectedSliceValues.length);
    for (int i = 0; i < block.getPositionCount(); i++) {
        int expectedSize = expectedSliceValues[i] == null ? 0 : expectedSliceValues[i].length();
        assertEquals(blockBuilder.getEstimatedDataSizeForStats(i), expectedSize);
        assertEquals(block.getEstimatedDataSizeForStats(i), expectedSize);
    }

    BlockBuilder nullValueBlockBuilder = blockBuilder.newBlockBuilderLike(null).appendNull();
    assertEquals(nullValueBlockBuilder.getEstimatedDataSizeForStats(0), 0);
    assertEquals(nullValueBlockBuilder.build().getEstimatedDataSizeForStats(0), 0);
}
 
Example 10
Source File: TestDictionaryAwarePageFilter.java    From presto with Apache License 2.0 5 votes vote down vote up
private static void testFilter(DictionaryAwarePageFilter filter, Block block, boolean filterRange)
{
    IntSet actualSelectedPositions = toSet(filter.filter(null, new Page(block)));

    block = block.getLoadedBlock();

    IntSet expectedSelectedPositions = new IntArraySet(block.getPositionCount());
    for (int position = 0; position < block.getPositionCount(); position++) {
        if (isSelected(filterRange, block.getLong(position, 0))) {
            expectedSelectedPositions.add(position);
        }
    }
    assertEquals(actualSelectedPositions, expectedSelectedPositions);
}
 
Example 11
Source File: PartitionTransforms.java    From presto with Apache License 2.0 5 votes vote down vote up
private static Block truncateVarchar(Block block, int max)
{
    BlockBuilder builder = VARCHAR.createBlockBuilder(null, block.getPositionCount());
    for (int position = 0; position < block.getPositionCount(); position++) {
        if (block.isNull(position)) {
            builder.appendNull();
            continue;
        }
        Slice value = VARCHAR.getSlice(block, position);
        value = truncateVarchar(value, max);
        VARCHAR.writeSlice(builder, value);
    }
    return builder.build();
}
 
Example 12
Source File: ArrayMinMaxUtils.java    From presto with Apache License 2.0 5 votes vote down vote up
@UsedByGeneratedCode
public static Double doubleArrayMinMax(MethodHandle compareMethodHandle, Type elementType, Block block)
{
    try {
        if (block.getPositionCount() == 0) {
            return null;
        }

        boolean containNull = false;
        double selectedValue = elementType.getDouble(block, 0);
        for (int i = 0; i < block.getPositionCount(); i++) {
            if (block.isNull(i)) {
                containNull = true;
            }
            double value = elementType.getDouble(block, i);
            if ((boolean) compareMethodHandle.invokeExact(value, selectedValue)) {
                selectedValue = value;
            }
            else if (isNaN(value)) {
                return NaN;
            }
        }

        return containNull ? null : selectedValue;
    }
    catch (Throwable t) {
        throw internalError(t);
    }
}
 
Example 13
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 14
Source File: OrcWriteValidation.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public void addBlock(Type type, Block block)
{
    for (int position = 0; position < block.getPositionCount(); position++) {
        if (!block.isNull(position)) {
            rowCount++;
        }
    }
}
 
Example 15
Source File: TestTinyintArrayType.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
protected Object getGreaterValue(Object value)
{
    Block block = (Block) value;
    BlockBuilder blockBuilder = TINYINT.createBlockBuilder(null, block.getPositionCount() + 1);
    for (int i = 0; i < block.getPositionCount(); i++) {
        TINYINT.appendTo(block, i, blockBuilder);
    }
    TINYINT.writeLong(blockBuilder, 1L);

    return blockBuilder.build();
}
 
Example 16
Source File: TestColumnarRow.java    From presto with Apache License 2.0 5 votes vote down vote up
private static <T> void assertRunLengthEncodedBlock(Block block, T[] expectedValues)
{
    for (int position = 0; position < block.getPositionCount(); position++) {
        RunLengthEncodedBlock runLengthEncodedBlock = createTestRleBlock(block, position);
        T[] expectedDictionaryValues = createTestRleExpectedValues(expectedValues, position);

        assertBlock(runLengthEncodedBlock, expectedDictionaryValues);
        assertColumnarRow(runLengthEncodedBlock, expectedDictionaryValues);
    }
}
 
Example 17
Source File: TestMapBlock.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test
public void testEstimatedDataSizeForStats()
{
    Map<String, Long>[] expectedValues = alternatingNullValues(createTestMap(9, 3, 4, 0, 8, 0, 6, 5));
    BlockBuilder blockBuilder = createBlockBuilderWithValues(expectedValues);
    Block block = blockBuilder.build();
    assertEquals(block.getPositionCount(), expectedValues.length);
    for (int i = 0; i < block.getPositionCount(); i++) {
        int expectedSize = getExpectedEstimatedDataSize(expectedValues[i]);
        assertEquals(blockBuilder.getEstimatedDataSizeForStats(i), expectedSize);
        assertEquals(block.getEstimatedDataSizeForStats(i), expectedSize);
    }
}
 
Example 18
Source File: TestPageProcessor.java    From presto with Apache License 2.0 4 votes vote down vote up
private static LazyBlock lazyWrapper(Block block)
{
    return new LazyBlock(block.getPositionCount(), block::getLoadedBlock);
}
 
Example 19
Source File: RcFileTester.java    From presto with Apache License 2.0 4 votes vote down vote up
private static void assertFileContentsNew(
        Type type,
        TempFile tempFile,
        Format format,
        List<?> expectedValues,
        boolean readLastBatchOnly,
        Map<String, String> metadata)
        throws IOException
{
    try (RcFileReader recordReader = createRcFileReader(tempFile, type, format.getVectorEncoding())) {
        assertIndexOf(recordReader, tempFile.getFile());
        assertEquals(recordReader.getMetadata(), ImmutableMap.builder()
                .putAll(metadata)
                .put("hive.io.rcfile.column.number", "1")
                .build());

        Iterator<?> iterator = expectedValues.iterator();
        int totalCount = 0;
        for (int batchSize = recordReader.advance(); batchSize >= 0; batchSize = recordReader.advance()) {
            totalCount += batchSize;
            if (readLastBatchOnly && totalCount == expectedValues.size()) {
                assertEquals(advance(iterator, batchSize), batchSize);
            }
            else {
                Block block = recordReader.readBlock(0);

                List<Object> data = new ArrayList<>(block.getPositionCount());
                for (int position = 0; position < block.getPositionCount(); position++) {
                    data.add(type.getObjectValue(SESSION, block, position));
                }

                for (int i = 0; i < batchSize; i++) {
                    assertTrue(iterator.hasNext());
                    Object expected = iterator.next();

                    Object actual = data.get(i);
                    assertColumnValueEquals(type, actual, expected);
                }
            }
        }
        assertFalse(iterator.hasNext());
        assertEquals(recordReader.getRowsRead(), totalCount);
    }
}
 
Example 20
Source File: TestDictionaryAwarePageProjection.java    From presto with Apache License 2.0 4 votes vote down vote up
private static LazyBlock lazyWrapper(Block block)
{
    return new LazyBlock(block.getPositionCount(), block::getLoadedBlock);
}