Search in sources :

Example 1 with TypeParameter

use of io.prestosql.spi.function.TypeParameter in project hetu-core by openlookeng.

the class ArrayPositionFunction method arrayPosition.

@TypeParameter("T")
@SqlType(StandardTypes.BIGINT)
public static long arrayPosition(@TypeParameter("T") Type type, @OperatorDependency(operator = EQUAL, returnType = StandardTypes.BOOLEAN, argumentTypes = { "T", "T" }) MethodHandle equalMethodHandle, @SqlType("array(T)") Block array, @SqlType("T") Slice element) {
    int size = array.getPositionCount();
    for (int i = 0; i < size; i++) {
        if (!array.isNull(i)) {
            Slice arrayValue = type.getSlice(array, i);
            try {
                Boolean result = (Boolean) equalMethodHandle.invokeExact(arrayValue, element);
                checkNotIndeterminate(result);
                if (result) {
                    // result is 1-based (instead of 0)
                    return i + 1;
                }
            } catch (Throwable t) {
                throw internalError(t);
            }
        }
    }
    return 0;
}
Also used : Slice(io.airlift.slice.Slice) TypeParameter(io.prestosql.spi.function.TypeParameter) SqlType(io.prestosql.spi.function.SqlType)

Example 2 with TypeParameter

use of io.prestosql.spi.function.TypeParameter in project hetu-core by openlookeng.

the class ArrayReverseFunction method reverse.

@TypeParameter("E")
@SqlType("array(E)")
public Block reverse(@TypeParameter("E") Type type, @SqlType("array(E)") Block block) {
    int arrayLength = block.getPositionCount();
    if (arrayLength < 2) {
        return block;
    }
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
    for (int i = arrayLength - 1; i >= 0; i--) {
        type.appendTo(block, i, blockBuilder);
    }
    pageBuilder.declarePositions(arrayLength);
    return blockBuilder.getRegion(blockBuilder.getPositionCount() - arrayLength, arrayLength);
}
Also used : BlockBuilder(io.prestosql.spi.block.BlockBuilder) TypeParameter(io.prestosql.spi.function.TypeParameter) SqlType(io.prestosql.spi.function.SqlType)

Example 3 with TypeParameter

use of io.prestosql.spi.function.TypeParameter in project hetu-core by openlookeng.

the class ArrayShuffleFunction method shuffle.

@TypeParameter("E")
@SqlType("array(E)")
public Block shuffle(@TypeParameter("E") Type type, @SqlType("array(E)") Block block) {
    int length = block.getPositionCount();
    if (positions.length < length) {
        positions = new int[length];
    }
    for (int i = 0; i < length; i++) {
        positions[i] = i;
    }
    // Randomly swap a pair of positions
    for (int i = length - 1; i > 0; i--) {
        int index = ThreadLocalRandom.current().nextInt(i + 1);
        int swap = positions[i];
        positions[i] = positions[index];
        positions[index] = swap;
    }
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
    for (int i = 0; i < length; i++) {
        type.appendTo(block, positions[i], blockBuilder);
    }
    pageBuilder.declarePositions(length);
    return blockBuilder.getRegion(blockBuilder.getPositionCount() - length, length);
}
Also used : BlockBuilder(io.prestosql.spi.block.BlockBuilder) TypeParameter(io.prestosql.spi.function.TypeParameter) SqlType(io.prestosql.spi.function.SqlType)

Example 4 with TypeParameter

use of io.prestosql.spi.function.TypeParameter in project hetu-core by openlookeng.

the class ArrayIntersectFunction method intersect.

@TypeParameter("E")
@SqlType("array(E)")
public Block intersect(@TypeParameter("E") Type type, @OperatorDependency(operator = IS_DISTINCT_FROM, returnType = BOOLEAN, argumentTypes = { "E", "E" }) MethodHandle elementIsDistinctFrom, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") Block rightArray) {
    Block inputLeftArray = leftArray;
    Block inputRightArray = rightArray;
    if (inputLeftArray.getPositionCount() < inputRightArray.getPositionCount()) {
        Block tempArray = inputLeftArray;
        inputLeftArray = inputRightArray;
        inputRightArray = tempArray;
    }
    int leftPositionCount = inputLeftArray.getPositionCount();
    int rightPositionCount = inputRightArray.getPositionCount();
    if (rightPositionCount == 0) {
        return inputRightArray;
    }
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    TypedSet rightTypedSet = new TypedSet(type, elementIsDistinctFrom, rightPositionCount, "array_intersect");
    for (int i = 0; i < rightPositionCount; i++) {
        rightTypedSet.add(inputRightArray, i);
    }
    BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
    // The intersected set can have at most rightPositionCount elements
    TypedSet intersectTypedSet = new TypedSet(type, Optional.of(elementIsDistinctFrom), blockBuilder, rightPositionCount, "array_intersect");
    for (int i = 0; i < leftPositionCount; i++) {
        if (rightTypedSet.contains(inputLeftArray, i)) {
            intersectTypedSet.add(inputLeftArray, i);
        }
    }
    pageBuilder.declarePositions(intersectTypedSet.size());
    return blockBuilder.getRegion(blockBuilder.getPositionCount() - intersectTypedSet.size(), intersectTypedSet.size());
}
Also used : Block(io.prestosql.spi.block.Block) TypedSet(io.prestosql.operator.aggregation.TypedSet) BlockBuilder(io.prestosql.spi.block.BlockBuilder) TypeParameter(io.prestosql.spi.function.TypeParameter) SqlType(io.prestosql.spi.function.SqlType)

Example 5 with TypeParameter

use of io.prestosql.spi.function.TypeParameter in project hetu-core by openlookeng.

the class ArrayCombinationsFunction method combinations.

@TypeParameter("T")
@SqlType("array(array(T))")
public static Block combinations(@TypeParameter("T") Type elementType, @SqlType("array(T)") Block array, @SqlType(INTEGER) long n) {
    int arrayLength = array.getPositionCount();
    int combinationLength = toIntExact(n);
    checkCondition(combinationLength >= 0, INVALID_FUNCTION_ARGUMENT, "combination size must not be negative: %s", combinationLength);
    checkCondition(combinationLength <= MAX_COMBINATION_LENGTH, INVALID_FUNCTION_ARGUMENT, "combination size must not exceed %s: %s", MAX_COMBINATION_LENGTH, combinationLength);
    ArrayType arrayType = new ArrayType(elementType);
    if (combinationLength > arrayLength) {
        return arrayType.createBlockBuilder(new PageBuilderStatus().createBlockBuilderStatus(), 0).build();
    }
    int combinationCount = combinationCount(arrayLength, combinationLength);
    checkCondition(combinationCount * (long) combinationLength <= MAX_RESULT_ELEMENTS, INVALID_FUNCTION_ARGUMENT, "combinations exceed max size");
    int[] ids = new int[combinationCount * combinationLength];
    int idsPosition = 0;
    int[] combination = firstCombination(arrayLength, combinationLength);
    do {
        arraycopy(combination, 0, ids, idsPosition, combinationLength);
        idsPosition += combinationLength;
    } while (nextCombination(combination, combinationLength));
    verify(idsPosition == ids.length, "idsPosition != ids.length, %s and %s respectively", idsPosition, ids.length);
    int[] offsets = new int[combinationCount + 1];
    setAll(offsets, i -> i * combinationLength);
    return ArrayBlock.fromElementBlock(combinationCount, Optional.empty(), offsets, new DictionaryBlock(array, ids));
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) PageBuilderStatus(io.prestosql.spi.block.PageBuilderStatus) DictionaryBlock(io.prestosql.spi.block.DictionaryBlock) TypeParameter(io.prestosql.spi.function.TypeParameter) SqlType(io.prestosql.spi.function.SqlType)

Aggregations

TypeParameter (io.prestosql.spi.function.TypeParameter)24 SqlType (io.prestosql.spi.function.SqlType)20 BlockBuilder (io.prestosql.spi.block.BlockBuilder)18 TypedSet (io.prestosql.operator.aggregation.TypedSet)6 TypeParameterSpecialization (io.prestosql.spi.function.TypeParameterSpecialization)5 Block (io.prestosql.spi.block.Block)4 HyperLogLog (io.airlift.stats.cardinality.HyperLogLog)3 PrestoException (io.prestosql.spi.PrestoException)3 InputFunction (io.prestosql.spi.function.InputFunction)3 ArrayType (io.prestosql.spi.type.ArrayType)3 RowType (io.prestosql.spi.type.RowType)3 Type (io.prestosql.spi.type.Type)3 Slice (io.airlift.slice.Slice)2 SqlNullable (io.prestosql.spi.function.SqlNullable)2 MapType (io.prestosql.spi.type.MapType)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 DictionaryBlock (io.prestosql.spi.block.DictionaryBlock)1 PageBuilderStatus (io.prestosql.spi.block.PageBuilderStatus)1 OperatorType (io.prestosql.spi.function.OperatorType)1