Search in sources :

Example 11 with TypedSet

use of io.trino.operator.aggregation.TypedSet 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 12 with TypedSet

use of io.trino.operator.aggregation.TypedSet 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)

Aggregations

TypedSet (io.trino.operator.aggregation.TypedSet)12 BlockBuilder (io.trino.spi.block.BlockBuilder)10 TypedSet.createEqualityTypedSet (io.trino.operator.aggregation.TypedSet.createEqualityTypedSet)9 SqlType (io.trino.spi.function.SqlType)7 Block (io.trino.spi.block.Block)6 TypeParameter (io.trino.spi.function.TypeParameter)6 Type (io.trino.spi.type.Type)5 MapType (io.trino.spi.type.MapType)4 TrinoException (io.trino.spi.TrinoException)3 TypeSignature.mapType (io.trino.spi.type.TypeSignature.mapType)3 UsedByGeneratedCode (io.trino.annotation.UsedByGeneratedCode)2 TypedSet.createDistinctTypedSet (io.trino.operator.aggregation.TypedSet.createDistinctTypedSet)2 SqlNullable (io.trino.spi.function.SqlNullable)2 ArrayType (io.trino.spi.type.ArrayType)2 RowType (io.trino.spi.type.RowType)2 ObjectBigArray (io.trino.array.ObjectBigArray)1 TypedSet.createUnboundedEqualityTypedSet (io.trino.operator.aggregation.TypedSet.createUnboundedEqualityTypedSet)1 PageBuilder (io.trino.spi.PageBuilder)1 ScalarFunction (io.trino.spi.function.ScalarFunction)1 TypeSignature.arrayType (io.trino.spi.type.TypeSignature.arrayType)1