Java Code Examples for io.prestosql.spi.block.Block

The following examples show how to use io.prestosql.spi.block.Block. These examples are extracted from open source projects. 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 Project: presto   Source File: AvroColumnDecoder.java    License: Apache License 2.0 6 votes vote down vote up
private static Block serializeList(BlockBuilder parentBlockBuilder, Object value, Type type, String columnName)
{
    if (value == null) {
        checkState(parentBlockBuilder != null, "parentBlockBuilder is null");
        parentBlockBuilder.appendNull();
        return null;
    }
    List<?> list = (List<?>) value;
    List<Type> typeParameters = type.getTypeParameters();
    Type elementType = typeParameters.get(0);

    BlockBuilder blockBuilder = elementType.createBlockBuilder(null, list.size());
    for (Object element : list) {
        serializeObject(blockBuilder, element, elementType, columnName);
    }
    if (parentBlockBuilder != null) {
        type.writeObject(parentBlockBuilder, blockBuilder.build());
        return null;
    }
    return blockBuilder.build();
}
 
Example 2
Source Project: presto   Source File: TestMapUnnester.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testMisaligned()
{
    int[] unnestedLengths = {1, 2, 0, 0};
    int[] requiredOutputCounts = {1, 3, 0, 1};

    Slice[][][] elements = column(
            array(toSlices("0.0.0", "0.0.1")),
            array(toSlices("1.0.0", "1.0.1"), toSlices("1.1.0", null)),
            null,
            null);

    Block[] blocks = testMapUnnester(requiredOutputCounts, unnestedLengths, elements);

    // Check final state. Misalignment has occurred.
    assertEquals(blocks.length, 2);
    // Misaligned, does not have a null entry.
    assertFalse(blocks[0] instanceof DictionaryBlock);
    // Misaligned, but has a null entry elements[1][1][1]
    assertTrue(blocks[1] instanceof DictionaryBlock);
}
 
Example 3
Source Project: presto   Source File: TestStateCompiler.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testPrimitiveByteSerialization()
{
    AccumulatorStateFactory<ByteState> factory = StateCompiler.generateStateFactory(ByteState.class);
    AccumulatorStateSerializer<ByteState> serializer = StateCompiler.generateStateSerializer(ByteState.class);
    ByteState state = factory.createSingleState();
    ByteState deserializedState = factory.createSingleState();

    state.setByte((byte) 3);

    BlockBuilder builder = TINYINT.createBlockBuilder(null, 1);
    serializer.serialize(state, builder);

    Block block = builder.build();
    serializer.deserialize(block, 0, deserializedState);
    assertEquals(deserializedState.getByte(), state.getByte());
}
 
Example 4
Source Project: presto   Source File: BenchmarkArrayFilter.java    License: Apache License 2.0 6 votes vote down vote up
private static Block createChannel(int positionCount, int arraySize, ArrayType arrayType)
{
    BlockBuilder blockBuilder = arrayType.createBlockBuilder(null, positionCount);
    for (int position = 0; position < positionCount; position++) {
        BlockBuilder entryBuilder = blockBuilder.beginBlockEntry();
        for (int i = 0; i < arraySize; i++) {
            if (arrayType.getElementType().getJavaType() == long.class) {
                arrayType.getElementType().writeLong(entryBuilder, ThreadLocalRandom.current().nextLong());
            }
            else {
                throw new UnsupportedOperationException();
            }
        }
        blockBuilder.closeEntry();
    }
    return blockBuilder.build();
}
 
Example 5
Source Project: presto   Source File: TestTinyintType.java    License: Apache License 2.0 6 votes vote down vote up
public static Block createTestBlock()
{
    BlockBuilder blockBuilder = TINYINT.createBlockBuilder(null, 15);
    TINYINT.writeLong(blockBuilder, 111);
    TINYINT.writeLong(blockBuilder, 111);
    TINYINT.writeLong(blockBuilder, 111);
    TINYINT.writeLong(blockBuilder, 22);
    TINYINT.writeLong(blockBuilder, 22);
    TINYINT.writeLong(blockBuilder, 22);
    TINYINT.writeLong(blockBuilder, 22);
    TINYINT.writeLong(blockBuilder, 22);
    TINYINT.writeLong(blockBuilder, 33);
    TINYINT.writeLong(blockBuilder, 33);
    TINYINT.writeLong(blockBuilder, 44);
    return blockBuilder.build();
}
 
Example 6
Source Project: presto   Source File: ArrayConcatFunction.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, Metadata metadata)
{
    if (arity < 2) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "There must be two or more arguments to " + FUNCTION_NAME);
    }

    Type elementType = boundVariables.getTypeVariable("E");

    VarArgsToArrayAdapterGenerator.MethodHandleAndConstructor methodHandleAndConstructor = generateVarArgsToArrayAdapter(
            Block.class,
            Block.class,
            arity,
            METHOD_HANDLE.bindTo(elementType),
            USER_STATE_FACTORY.bindTo(elementType));

    return new ScalarFunctionImplementation(
            false,
            nCopies(arity, valueTypeArgumentProperty(RETURN_NULL_ON_NULL)),
            methodHandleAndConstructor.getMethodHandle(),
            Optional.of(methodHandleAndConstructor.getConstructor()));
}
 
Example 7
Source Project: presto   Source File: ArrayMinMaxUtils.java    License: Apache License 2.0 6 votes vote down vote up
@UsedByGeneratedCode
public static Slice sliceArrayMinMax(MethodHandle compareMethodHandle, Type elementType, Block block)
{
    try {
        if (block.getPositionCount() == 0) {
            return null;
        }

        Slice selectedValue = elementType.getSlice(block, 0);
        for (int i = 0; i < block.getPositionCount(); i++) {
            if (block.isNull(i)) {
                return null;
            }
            Slice value = elementType.getSlice(block, i);
            if ((boolean) compareMethodHandle.invokeExact(value, selectedValue)) {
                selectedValue = value;
            }
        }

        return selectedValue;
    }
    catch (Throwable t) {
        throw internalError(t);
    }
}
 
Example 8
Source Project: presto   Source File: LongEncoding.java    License: 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 9
Source Project: presto   Source File: PageBuilder.java    License: Apache License 2.0 6 votes vote down vote up
public Page build()
{
    if (blockBuilders.length == 0) {
        return new Page(declaredPositions);
    }

    Block[] blocks = new Block[blockBuilders.length];
    for (int i = 0; i < blocks.length; i++) {
        blocks[i] = blockBuilders[i].build();
        if (blocks[i].getPositionCount() != declaredPositions) {
            throw new IllegalStateException(format("Declared positions (%s) does not match block %s's number of entries (%s)", declaredPositions, i, blocks[i].getPositionCount()));
        }
    }

    return Page.wrapBlocksWithoutCopy(declaredPositions, blocks);
}
 
Example 10
Source Project: presto   Source File: StreamingAggregationOperator.java    License: Apache License 2.0 6 votes vote down vote up
private void evaluateAndFlushGroup(Page page, int position)
{
    pageBuilder.declarePosition();
    for (int i = 0; i < groupByTypes.size(); i++) {
        Block block = page.getBlock(groupByChannels[i]);
        Type type = groupByTypes.get(i);
        type.appendTo(block, position, pageBuilder.getBlockBuilder(i));
    }
    int offset = groupByTypes.size();
    for (int i = 0; i < aggregates.size(); i++) {
        aggregates.get(i).evaluate(pageBuilder.getBlockBuilder(offset + i));
    }

    if (pageBuilder.isFull()) {
        outputPages.add(pageBuilder.build());
        pageBuilder.reset();
    }

    aggregates = setupAggregates(step, accumulatorFactories);
}
 
Example 11
Source Project: presto   Source File: BenchmarkArraySort.java    License: Apache License 2.0 6 votes vote down vote up
@Setup
public void setup()
{
    Metadata metadata = createTestMetadataManager();
    metadata.addFunctions(extractFunctions(BenchmarkArraySort.class));
    ExpressionCompiler compiler = new ExpressionCompiler(metadata, new PageFunctionCompiler(metadata, 0));
    ImmutableList.Builder<RowExpression> projectionsBuilder = ImmutableList.builder();
    Block[] blocks = new Block[TYPES.size()];
    for (int i = 0; i < TYPES.size(); i++) {
        Type elementType = TYPES.get(i);
        ArrayType arrayType = new ArrayType(elementType);
        projectionsBuilder.add(new CallExpression(
                metadata.resolveFunction(QualifiedName.of(name), fromTypes(arrayType)),
                arrayType,
                ImmutableList.of(field(i, arrayType))));
        blocks[i] = createChannel(POSITIONS, ARRAY_SIZE, arrayType);
    }

    ImmutableList<RowExpression> projections = projectionsBuilder.build();
    pageProcessor = compiler.compilePageProcessor(Optional.empty(), projections).get();
    page = new Page(blocks);
}
 
Example 12
Source Project: presto   Source File: DictionaryBuilder.java    License: Apache License 2.0 6 votes vote down vote up
public int putIfAbsent(Block block, int position)
{
    requireNonNull(block, "block must not be null");

    if (block.isNull(position)) {
        containsNullElement = true;
        return NULL_POSITION;
    }

    int blockPosition;
    long hashPosition = getHashPositionOfElement(block, position);
    if (blockPositionByHash.get(hashPosition) != EMPTY_SLOT) {
        blockPosition = blockPositionByHash.get(hashPosition);
    }
    else {
        blockPosition = addNewElement(hashPosition, block, position);
    }
    verify(blockPosition != NULL_POSITION);
    return blockPosition;
}
 
Example 13
public static void input(BlockComparator comparator, Type valueType, Type keyType, MinMaxByNState state, Block value, Block key, int blockIndex, long n)
{
    TypedKeyValueHeap heap = state.getTypedKeyValueHeap();
    if (heap == null) {
        if (n <= 0) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "third argument of max_by/min_by must be a positive integer");
        }
        checkCondition(n <= MAX_NUMBER_OF_VALUES, INVALID_FUNCTION_ARGUMENT, "third argument of max_by/min_by must be less than or equal to %s; found %s", MAX_NUMBER_OF_VALUES, n);
        heap = new TypedKeyValueHeap(comparator, keyType, valueType, toIntExact(n));
        state.setTypedKeyValueHeap(heap);
    }

    long startSize = heap.getEstimatedSize();
    if (!key.isNull(blockIndex)) {
        heap.add(key, value, blockIndex);
    }
    state.addMemoryUsage(heap.getEstimatedSize() - startSize);
}
 
Example 14
Source Project: presto   Source File: TestColumnarArray.java    License: Apache License 2.0 6 votes vote down vote up
private static <T> void verifyBlock(Block block, T[] expectedValues)
{
    assertBlock(block, expectedValues);

    assertColumnarArray(block, expectedValues);
    assertDictionaryBlock(block, expectedValues);
    assertRunLengthEncodedBlock(block, expectedValues);

    int offset = 1;
    int length = expectedValues.length - 2;
    Block blockRegion = block.getRegion(offset, length);
    T[] expectedValuesRegion = Arrays.copyOfRange(expectedValues, offset, offset + length);

    assertBlock(blockRegion, expectedValuesRegion);

    assertColumnarArray(blockRegion, expectedValuesRegion);
    assertDictionaryBlock(blockRegion, expectedValuesRegion);
    assertRunLengthEncodedBlock(blockRegion, expectedValuesRegion);
}
 
Example 15
Source Project: presto   Source File: BlockAssertions.java    License: Apache License 2.0 6 votes vote down vote up
public static Block createStringArraysBlock(Iterable<? extends Iterable<String>> values)
{
    ArrayType arrayType = new ArrayType(VARCHAR);
    BlockBuilder builder = arrayType.createBlockBuilder(null, 100);

    for (Iterable<String> value : values) {
        if (value == null) {
            builder.appendNull();
        }
        else {
            arrayType.writeObject(builder, createStringsBlock(value));
        }
    }

    return builder.build();
}
 
Example 16
Source Project: presto   Source File: PagesIndex.java    License: Apache License 2.0 6 votes vote down vote up
public void compact()
{
    if (eagerCompact) {
        return;
    }
    for (int channel = 0; channel < types.size(); channel++) {
        ObjectArrayList<Block> blocks = channels[channel];
        for (int i = nextBlockToCompact; i < blocks.size(); i++) {
            Block block = blocks.get(i);

            // Copy the block to compact its size
            Block compactedBlock = block.copyRegion(0, block.getPositionCount());
            blocks.set(i, compactedBlock);
            pagesMemorySize -= block.getRetainedSizeInBytes();
            pagesMemorySize += compactedBlock.getRetainedSizeInBytes();
        }
    }
    nextBlockToCompact = channels[0].size();
    estimatedSize = calculateEstimatedSize();
}
 
Example 17
Source Project: presto   Source File: ArrayJoin.java    License: Apache License 2.0 5 votes vote down vote up
@UsedByGeneratedCode
public static Slice arrayJoin(
        MethodHandle castFunction,
        Object state,
        ConnectorSession session,
        Block arrayBlock,
        Slice delimiter)
{
    return arrayJoin(castFunction, state, session, arrayBlock, delimiter, null);
}
 
Example 18
Source Project: presto   Source File: TestUuidType.java    License: Apache License 2.0 5 votes vote down vote up
public static Block createTestBlock()
{
    BlockBuilder blockBuilder = UUID.createBlockBuilder(null, 1);
    for (int i = 0; i < 10; i++) {
        String uuid = "6b5f5b65-67e4-43b0-8ee3-586cd49f58a" + i;
        UUID.writeSlice(blockBuilder, castFromVarcharToUuid(utf8Slice(uuid)));
    }
    return blockBuilder.build();
}
 
Example 19
Source Project: presto   Source File: AbstractMinMaxNAggregationFunction.java    License: Apache License 2.0 5 votes vote down vote up
public static void input(BlockComparator comparator, Type type, MinMaxNState state, Block block, long n, int blockIndex)
{
    TypedHeap heap = state.getTypedHeap();
    if (heap == null) {
        if (n <= 0) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "second argument of max_n/min_n must be positive");
        }
        checkCondition(n <= MAX_NUMBER_OF_VALUES, INVALID_FUNCTION_ARGUMENT, "second argument of max_n/min_n must be less than or equal to %s; found %s", MAX_NUMBER_OF_VALUES, n);
        heap = new TypedHeap(comparator, type, toIntExact(n));
        state.setTypedHeap(heap);
    }
    long startSize = heap.getEstimatedSize();
    heap.add(block, blockIndex);
    state.addMemoryUsage(heap.getEstimatedSize() - startSize);
}
 
Example 20
Source Project: presto   Source File: SimplePagesHashStrategy.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean positionEqualsRow(int leftBlockIndex, int leftPosition, int rightPosition, Page page, int[] rightHashChannels)
{
    for (int i = 0; i < hashChannels.size(); i++) {
        int hashChannel = hashChannels.get(i);
        Type type = types.get(hashChannel);
        Block leftBlock = channels.get(hashChannel).get(leftBlockIndex);
        Block rightBlock = page.getBlock(rightHashChannels[i]);
        if (!TypeUtils.positionEqualsPosition(type, leftBlock, leftPosition, rightBlock, rightPosition)) {
            return false;
        }
    }
    return true;
}
 
Example 21
Source Project: presto   Source File: RealType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Object getObjectValue(ConnectorSession session, Block block, int position)
{
    if (block.isNull(position)) {
        return null;
    }
    return intBitsToFloat(block.getInt(position, 0));
}
 
Example 22
Source Project: presto   Source File: BigintValueWriter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void write(Block block)
{
    for (int i = 0; i < block.getPositionCount(); i++) {
        if (!block.isNull(i)) {
            long value = type.getLong(block, i);
            getValueWriter().writeLong(value);
            getStatistics().updateStats(value);
        }
    }
}
 
Example 23
Source Project: presto   Source File: TestTinyintVarcharMapType.java    License: Apache License 2.0 5 votes vote down vote up
public static Block createTestBlock(Type mapType)
{
    BlockBuilder blockBuilder = mapType.createBlockBuilder(null, 2);
    mapType.writeObject(blockBuilder, mapBlockOf(TINYINT, VARCHAR, ImmutableMap.of(1, "hi")));
    mapType.writeObject(blockBuilder, mapBlockOf(TINYINT, VARCHAR, ImmutableMap.of(1, "2", 2, "hello")));
    return blockBuilder.build();
}
 
Example 24
Source Project: presto   Source File: LearnLibSvmRegressorAggregation.java    License: Apache License 2.0 5 votes vote down vote up
@InputFunction
public static void input(
        @AggregationState LearnState state,
        @SqlType(DOUBLE) double label,
        @SqlType("map(bigint,double)") Block features,
        @SqlType(VARCHAR) Slice parameters)
{
    state.getLabels().add(label);
    FeatureVector featureVector = ModelUtils.toFeatures(features);
    state.addMemoryUsage(featureVector.getEstimatedSize());
    state.getFeatureVectors().add(featureVector);
    state.setParameters(parameters);
}
 
Example 25
Source Project: presto   Source File: BlockAssertions.java    License: Apache License 2.0 5 votes vote down vote up
public static List<Object> toValues(Type type, Iterable<Block> blocks)
{
    List<Object> values = new ArrayList<>();
    for (Block block : blocks) {
        for (int position = 0; position < block.getPositionCount(); position++) {
            values.add(type.getObjectValue(SESSION, block, position));
        }
    }
    return Collections.unmodifiableList(values);
}
 
Example 26
Source Project: presto   Source File: TestSmallintArrayType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Object getGreaterValue(Object value)
{
    Block block = (Block) value;
    BlockBuilder blockBuilder = SMALLINT.createBlockBuilder(null, block.getPositionCount() + 1);
    for (int i = 0; i < block.getPositionCount(); i++) {
        SMALLINT.appendTo(block, i, blockBuilder);
    }
    SMALLINT.writeLong(blockBuilder, 1L);

    return blockBuilder.build();
}
 
Example 27
Source Project: presto   Source File: SerDeUtils.java    License: Apache License 2.0 5 votes vote down vote up
private static Block serializeStruct(Type type, BlockBuilder builder, Object object, StructObjectInspector inspector)
{
    if (object == null) {
        requireNonNull(builder, "parent builder is null").appendNull();
        return null;
    }

    List<Type> typeParameters = type.getTypeParameters();
    List<? extends StructField> allStructFieldRefs = inspector.getAllStructFieldRefs();
    checkArgument(typeParameters.size() == allStructFieldRefs.size());
    BlockBuilder currentBuilder;

    boolean builderSynthesized = false;
    if (builder == null) {
        builderSynthesized = true;
        builder = type.createBlockBuilder(null, 1);
    }
    currentBuilder = builder.beginBlockEntry();

    for (int i = 0; i < typeParameters.size(); i++) {
        StructField field = allStructFieldRefs.get(i);
        serializeObject(typeParameters.get(i), currentBuilder, inspector.getStructFieldData(object, field), field.getFieldObjectInspector());
    }

    builder.closeEntry();
    if (builderSynthesized) {
        return (Block) type.getObject(builder, 0);
    }
    else {
        return null;
    }
}
 
Example 28
Source Project: presto   Source File: JoniRegexpFunctions.java    License: Apache License 2.0 5 votes vote down vote up
@Description("Group(s) extracted using the given pattern")
@ScalarFunction
@LiteralParameters("x")
@SqlType("array(varchar(x))")
public static Block regexpExtractAll(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) JoniRegexp pattern, @SqlType(StandardTypes.BIGINT) long groupIndex)
{
    Matcher matcher = pattern.matcher(source.getBytes());
    validateGroup(groupIndex, matcher.getEagerRegion());
    BlockBuilder blockBuilder = VARCHAR.createBlockBuilder(null, 32);
    int group = toIntExact(groupIndex);

    int nextStart = 0;
    while (true) {
        int offset = matcher.search(nextStart, source.length(), Option.DEFAULT);
        if (offset == -1) {
            break;
        }
        nextStart = getNextStart(source, matcher);
        Region region = matcher.getEagerRegion();
        int beg = region.beg[group];
        int end = region.end[group];
        if (beg == -1 || end == -1) {
            blockBuilder.appendNull();
        }
        else {
            Slice slice = source.slice(beg, end - beg);
            VARCHAR.writeSlice(blockBuilder, slice);
        }
    }
    return blockBuilder.build();
}
 
Example 29
@Override
protected Block[] getSequenceBlocks(int start, int length)
{
    BlockBuilder blockBuilder = INTERVAL_YEAR_MONTH.createBlockBuilder(null, length);
    for (int i = start; i < start + length; i++) {
        INTERVAL_YEAR_MONTH.writeLong(blockBuilder, i);
    }
    return new Block[] {blockBuilder.build()};
}
 
Example 30
Source Project: presto   Source File: JsonType.java    License: 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 {
        block.writeBytesTo(position, 0, block.getSliceLength(position), blockBuilder);
        blockBuilder.closeEntry();
    }
}