Search in sources :

Example 6 with SqlType

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

the class ArrayUnionFunction method bigintUnion.

@SqlType("array(bigint)")
public static Block bigintUnion(@SqlType("array(bigint)") Block leftArray, @SqlType("array(bigint)") Block rightArray) {
    int leftArrayCount = leftArray.getPositionCount();
    int rightArrayCount = rightArray.getPositionCount();
    LongSet set = new LongOpenHashSet(leftArrayCount + rightArrayCount);
    BlockBuilder distinctElementBlockBuilder = BIGINT.createBlockBuilder(null, leftArrayCount + rightArrayCount);
    AtomicBoolean containsNull = new AtomicBoolean(false);
    appendBigintArray(leftArray, containsNull, set, distinctElementBlockBuilder);
    appendBigintArray(rightArray, containsNull, set, distinctElementBlockBuilder);
    return distinctElementBlockBuilder.build();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LongSet(it.unimi.dsi.fastutil.longs.LongSet) LongOpenHashSet(it.unimi.dsi.fastutil.longs.LongOpenHashSet) BlockBuilder(io.prestosql.spi.block.BlockBuilder) SqlType(io.prestosql.spi.function.SqlType)

Example 7 with SqlType

use of io.prestosql.spi.function.SqlType 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 8 with SqlType

use of io.prestosql.spi.function.SqlType 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 9 with SqlType

use of io.prestosql.spi.function.SqlType 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 10 with SqlType

use of io.prestosql.spi.function.SqlType 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)

Aggregations

SqlType (io.prestosql.spi.function.SqlType)138 ScalarFunction (io.prestosql.spi.function.ScalarFunction)96 Description (io.prestosql.spi.function.Description)76 SqlNullable (io.prestosql.spi.function.SqlNullable)52 BlockBuilder (io.prestosql.spi.block.BlockBuilder)42 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)40 PrestoException (io.prestosql.spi.PrestoException)27 Slice (io.airlift.slice.Slice)25 LiteralParameters (io.prestosql.spi.function.LiteralParameters)24 TypeParameter (io.prestosql.spi.function.TypeParameter)20 ScalarOperator (io.prestosql.spi.function.ScalarOperator)16 Constraint (io.prestosql.type.Constraint)16 JsonParser (com.fasterxml.jackson.core.JsonParser)15 JsonUtil.createJsonParser (io.prestosql.util.JsonUtil.createJsonParser)15 IOException (java.io.IOException)15 Point (com.esri.core.geometry.Point)14 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)14 MultiPoint (com.esri.core.geometry.MultiPoint)12 Block (io.prestosql.spi.block.Block)9 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)8