Search in sources :

Example 21 with TypeParameter

use of io.trino.spi.function.TypeParameter in project trino by trinodb.

the class ArrayExceptFunction method except.

@TypeParameter("E")
@SqlType("array(E)")
public static Block except(@TypeParameter("E") Type type, @OperatorDependency(operator = EQUAL, argumentTypes = { "E", "E" }, convention = @Convention(arguments = { BLOCK_POSITION, BLOCK_POSITION }, result = NULLABLE_RETURN)) BlockPositionEqual elementEqual, @OperatorDependency(operator = HASH_CODE, argumentTypes = "E", convention = @Convention(arguments = BLOCK_POSITION, result = FAIL_ON_NULL)) BlockPositionHashCode elementHashCode, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") Block rightArray) {
    int leftPositionCount = leftArray.getPositionCount();
    int rightPositionCount = rightArray.getPositionCount();
    if (leftPositionCount == 0 || rightPositionCount == 0) {
        return leftArray;
    }
    TypedSet typedSet = createEqualityTypedSet(type, elementEqual, elementHashCode, leftPositionCount, "array_except");
    BlockBuilder distinctElementBlockBuilder = type.createBlockBuilder(null, leftPositionCount);
    for (int i = 0; i < rightPositionCount; i++) {
        typedSet.add(rightArray, i);
    }
    for (int i = 0; i < leftPositionCount; i++) {
        if (typedSet.add(leftArray, i)) {
            type.appendTo(leftArray, i, distinctElementBlockBuilder);
        }
    }
    return distinctElementBlockBuilder.build();
}
Also used : TypedSet.createEqualityTypedSet(io.trino.operator.aggregation.TypedSet.createEqualityTypedSet) TypedSet(io.trino.operator.aggregation.TypedSet) BlockBuilder(io.trino.spi.block.BlockBuilder) TypeParameter(io.trino.spi.function.TypeParameter) SqlType(io.trino.spi.function.SqlType)

Example 22 with TypeParameter

use of io.trino.spi.function.TypeParameter in project trino by trinodb.

the class ArrayFilterFunction method filterLong.

@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = long.class)
@SqlType("array(T)")
public static Block filterLong(@TypeParameter("T") Type elementType, @SqlType("array(T)") Block arrayBlock, @SqlType("function(T, boolean)") LongToBooleanFunction function) {
    int positionCount = arrayBlock.getPositionCount();
    BlockBuilder resultBuilder = elementType.createBlockBuilder(null, positionCount);
    for (int position = 0; position < positionCount; position++) {
        Long input = null;
        if (!arrayBlock.isNull(position)) {
            input = elementType.getLong(arrayBlock, position);
        }
        Boolean keep = function.apply(input);
        if (TRUE.equals(keep)) {
            elementType.appendTo(arrayBlock, position, resultBuilder);
        }
    }
    return resultBuilder.build();
}
Also used : BlockBuilder(io.trino.spi.block.BlockBuilder) TypeParameterSpecialization(io.trino.spi.function.TypeParameterSpecialization) TypeParameter(io.trino.spi.function.TypeParameter) SqlType(io.trino.spi.function.SqlType)

Example 23 with TypeParameter

use of io.trino.spi.function.TypeParameter in project trino by trinodb.

the class ArrayIntersectFunction method intersect.

@TypeParameter("E")
@SqlType("array(E)")
public Block intersect(@TypeParameter("E") Type type, @OperatorDependency(operator = IS_DISTINCT_FROM, argumentTypes = { "E", "E" }, convention = @Convention(arguments = { BLOCK_POSITION, BLOCK_POSITION }, result = FAIL_ON_NULL)) BlockPositionIsDistinctFrom elementIsDistinctFrom, @OperatorDependency(operator = HASH_CODE, argumentTypes = "E", convention = @Convention(arguments = BLOCK_POSITION, result = FAIL_ON_NULL)) BlockPositionHashCode elementHashCode, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") Block rightArray) {
    if (leftArray.getPositionCount() < rightArray.getPositionCount()) {
        Block tempArray = leftArray;
        leftArray = rightArray;
        rightArray = tempArray;
    }
    int leftPositionCount = leftArray.getPositionCount();
    int rightPositionCount = rightArray.getPositionCount();
    if (rightPositionCount == 0) {
        return rightArray;
    }
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    TypedSet rightTypedSet = createDistinctTypedSet(type, elementIsDistinctFrom, elementHashCode, rightPositionCount, "array_intersect");
    for (int i = 0; i < rightPositionCount; i++) {
        rightTypedSet.add(rightArray, i);
    }
    BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
    // The intersected set can have at most rightPositionCount elements
    TypedSet intersectTypedSet = createDistinctTypedSet(type, elementIsDistinctFrom, elementHashCode, blockBuilder, rightPositionCount, "array_intersect");
    for (int i = 0; i < leftPositionCount; i++) {
        if (rightTypedSet.contains(leftArray, i)) {
            intersectTypedSet.add(leftArray, i);
        }
    }
    pageBuilder.declarePositions(intersectTypedSet.size());
    return blockBuilder.getRegion(blockBuilder.getPositionCount() - intersectTypedSet.size(), intersectTypedSet.size());
}
Also used : Block(io.trino.spi.block.Block) TypedSet.createDistinctTypedSet(io.trino.operator.aggregation.TypedSet.createDistinctTypedSet) TypedSet(io.trino.operator.aggregation.TypedSet) BlockBuilder(io.trino.spi.block.BlockBuilder) TypeParameter(io.trino.spi.function.TypeParameter) SqlType(io.trino.spi.function.SqlType)

Example 24 with TypeParameter

use of io.trino.spi.function.TypeParameter in project trino by trinodb.

the class ArrayDistinctFunction method distinct.

@TypeParameter("E")
@SqlType("array(E)")
public Block distinct(@TypeParameter("E") Type type, @OperatorDependency(operator = IS_DISTINCT_FROM, argumentTypes = { "E", "E" }, convention = @Convention(arguments = { BLOCK_POSITION, BLOCK_POSITION }, result = FAIL_ON_NULL)) BlockPositionIsDistinctFrom elementIsDistinctFrom, @OperatorDependency(operator = HASH_CODE, argumentTypes = "E", convention = @Convention(arguments = BLOCK_POSITION, result = FAIL_ON_NULL)) BlockPositionHashCode elementHashCode, @SqlType("array(E)") Block array) {
    if (array.getPositionCount() < 2) {
        return array;
    }
    if (array.getPositionCount() == 2) {
        boolean distinct = elementIsDistinctFrom.isDistinctFrom(array, 0, array, 1);
        if (distinct) {
            return array;
        }
        return array.getSingleValueBlock(0);
    }
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    BlockBuilder distinctElementsBlockBuilder = pageBuilder.getBlockBuilder(0);
    TypedSet distinctElements = createDistinctTypedSet(type, elementIsDistinctFrom, elementHashCode, distinctElementsBlockBuilder, array.getPositionCount(), "array_distinct");
    for (int i = 0; i < array.getPositionCount(); i++) {
        distinctElements.add(array, i);
    }
    pageBuilder.declarePositions(distinctElements.size());
    return distinctElementsBlockBuilder.getRegion(distinctElementsBlockBuilder.getPositionCount() - distinctElements.size(), distinctElements.size());
}
Also used : TypedSet.createDistinctTypedSet(io.trino.operator.aggregation.TypedSet.createDistinctTypedSet) TypedSet(io.trino.operator.aggregation.TypedSet) BlockBuilder(io.trino.spi.block.BlockBuilder) TypeParameter(io.trino.spi.function.TypeParameter) SqlType(io.trino.spi.function.SqlType)

Example 25 with TypeParameter

use of io.trino.spi.function.TypeParameter in project trino by trinodb.

the class ApproximateCountDistinctAggregation method input.

@InputFunction
@TypeParameter("T")
public static void input(@OperatorDependency(operator = XX_HASH_64, argumentTypes = "T", convention = @Convention(arguments = NEVER_NULL, result = FAIL_ON_NULL)) MethodHandle methodHandle, @AggregationState HyperLogLogState state, @SqlType("T") long value, @SqlType(StandardTypes.DOUBLE) double maxStandardError) {
    HyperLogLog hll = getOrCreateHyperLogLog(state, maxStandardError);
    state.addMemoryUsage(-hll.estimatedInMemorySize());
    long hash;
    try {
        hash = (long) methodHandle.invokeExact(value);
    } catch (Throwable t) {
        throw internalError(t);
    }
    hll.addHash(hash);
    state.addMemoryUsage(hll.estimatedInMemorySize());
}
Also used : HyperLogLog(io.airlift.stats.cardinality.HyperLogLog) TypeParameter(io.trino.spi.function.TypeParameter) InputFunction(io.trino.spi.function.InputFunction)

Aggregations

TypeParameter (io.trino.spi.function.TypeParameter)28 SqlType (io.trino.spi.function.SqlType)22 BlockBuilder (io.trino.spi.block.BlockBuilder)20 TypeParameterSpecialization (io.trino.spi.function.TypeParameterSpecialization)9 HyperLogLog (io.airlift.stats.cardinality.HyperLogLog)6 TypedSet (io.trino.operator.aggregation.TypedSet)6 InputFunction (io.trino.spi.function.InputFunction)6 TypedSet.createEqualityTypedSet (io.trino.operator.aggregation.TypedSet.createEqualityTypedSet)4 TrinoException (io.trino.spi.TrinoException)3 Block (io.trino.spi.block.Block)3 SqlNullable (io.trino.spi.function.SqlNullable)3 ArrayType (io.trino.spi.type.ArrayType)3 RowType (io.trino.spi.type.RowType)3 Type (io.trino.spi.type.Type)3 TypedSet.createDistinctTypedSet (io.trino.operator.aggregation.TypedSet.createDistinctTypedSet)2 MapType (io.trino.spi.type.MapType)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 HashMultimap (com.google.common.collect.HashMultimap)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1