Search in sources :

Example 1 with TypedSet

use of io.prestosql.operator.aggregation.TypedSet in project hetu-core by openlookeng.

the class MapConcatFunction method mapConcat.

@UsedByGeneratedCode
public static Block mapConcat(MapType mapType, Object state, Block[] maps) {
    int entries = 0;
    int lastMapIndex = maps.length - 1;
    int firstMapIndex = lastMapIndex;
    for (int i = 0; i < maps.length; i++) {
        entries += maps[i].getPositionCount();
        if (maps[i].getPositionCount() > 0) {
            lastMapIndex = i;
            firstMapIndex = min(firstMapIndex, i);
        }
    }
    if (lastMapIndex == firstMapIndex) {
        return maps[lastMapIndex];
    }
    PageBuilder pageBuilder = (PageBuilder) state;
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    // TODO: we should move TypedSet into user state as well
    Type keyType = mapType.getKeyType();
    Type valueType = mapType.getValueType();
    TypedSet typedSet = new TypedSet(keyType, entries / 2, FUNCTION_NAME);
    BlockBuilder mapBlockBuilder = pageBuilder.getBlockBuilder(0);
    BlockBuilder blockBuilder = mapBlockBuilder.beginBlockEntry();
    // the last map
    Block map = maps[lastMapIndex];
    for (int i = 0; i < map.getPositionCount(); i += 2) {
        typedSet.add(map, i);
        keyType.appendTo(map, i, blockBuilder);
        valueType.appendTo(map, i + 1, blockBuilder);
    }
    // the map between the last and the first
    for (int idx = lastMapIndex - 1; idx > firstMapIndex; idx--) {
        map = maps[idx];
        for (int i = 0; i < map.getPositionCount(); i += 2) {
            if (!typedSet.contains(map, i)) {
                typedSet.add(map, i);
                keyType.appendTo(map, i, blockBuilder);
                valueType.appendTo(map, i + 1, blockBuilder);
            }
        }
    }
    // the first map
    map = maps[firstMapIndex];
    for (int i = 0; i < map.getPositionCount(); i += 2) {
        if (!typedSet.contains(map, i)) {
            keyType.appendTo(map, i, blockBuilder);
            valueType.appendTo(map, i + 1, blockBuilder);
        }
    }
    mapBlockBuilder.closeEntry();
    pageBuilder.declarePosition();
    return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
Also used : MapType(io.prestosql.spi.type.MapType) Type(io.prestosql.spi.type.Type) TypedSet(io.prestosql.operator.aggregation.TypedSet) Block(io.prestosql.spi.block.Block) PageBuilder(io.prestosql.spi.PageBuilder) BlockBuilder(io.prestosql.spi.block.BlockBuilder) UsedByGeneratedCode(io.prestosql.spi.annotation.UsedByGeneratedCode)

Example 2 with TypedSet

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

use of io.prestosql.operator.aggregation.TypedSet in project hetu-core by openlookeng.

the class BenchmarkArrayDistinct method oldArrayDistinct.

@ScalarFunction
@SqlType("array(varchar)")
public static Block oldArrayDistinct(@SqlType("array(varchar)") Block array) {
    if (array.getPositionCount() == 0) {
        return array;
    }
    TypedSet typedSet = new TypedSet(VARCHAR, array.getPositionCount(), "old_array_distinct");
    BlockBuilder distinctElementBlockBuilder = VARCHAR.createBlockBuilder(null, array.getPositionCount());
    for (int i = 0; i < array.getPositionCount(); i++) {
        if (!typedSet.contains(array, i)) {
            typedSet.add(array, i);
            VARCHAR.appendTo(array, i, distinctElementBlockBuilder);
        }
    }
    return distinctElementBlockBuilder.build();
}
Also used : TypedSet(io.prestosql.operator.aggregation.TypedSet) BlockBuilder(io.prestosql.spi.block.BlockBuilder) ScalarFunction(io.prestosql.spi.function.ScalarFunction) SqlType(io.prestosql.spi.function.SqlType)

Example 4 with TypedSet

use of io.prestosql.operator.aggregation.TypedSet in project hetu-core by openlookeng.

the class ArrayDistinctFunction method distinct.

@TypeParameter("E")
@SqlType("array(E)")
public Block distinct(@TypeParameter("E") Type type, @OperatorDependency(operator = IS_DISTINCT_FROM, returnType = BOOLEAN, argumentTypes = { "E", "E" }) MethodHandle elementIsDistinctFrom, @SqlType("array(E)") Block array) {
    if (array.getPositionCount() < 2) {
        return array;
    }
    if (array.getPositionCount() == 2) {
        boolean firstValueNull = array.isNull(0);
        Object firstValue = firstValueNull ? defaultValue(type.getJavaType()) : readNativeValue(type, array, 0);
        boolean secondValueNull = array.isNull(1);
        Object secondValue = secondValueNull ? defaultValue(type.getJavaType()) : readNativeValue(type, array, 1);
        boolean distinct;
        try {
            distinct = (boolean) elementIsDistinctFrom.invoke(firstValue, firstValueNull, secondValue, secondValueNull);
        } catch (Throwable t) {
            throw internalError(t);
        }
        if (distinct) {
            return array;
        }
        return array.getSingleValueBlock(0);
    }
    TypedSet typedSet = new TypedSet(type, elementIsDistinctFrom, array.getPositionCount(), "array_distinct");
    int distinctCount = 0;
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    BlockBuilder distinctElementBlockBuilder = pageBuilder.getBlockBuilder(0);
    for (int i = 0; i < array.getPositionCount(); i++) {
        if (!typedSet.contains(array, i)) {
            typedSet.add(array, i);
            distinctCount++;
            type.appendTo(array, i, distinctElementBlockBuilder);
        }
    }
    pageBuilder.declarePositions(distinctCount);
    return distinctElementBlockBuilder.getRegion(distinctElementBlockBuilder.getPositionCount() - distinctCount, distinctCount);
}
Also used : 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 TypedSet

use of io.prestosql.operator.aggregation.TypedSet in project hetu-core by openlookeng.

the class MathFunctions method mapDotProduct.

private static double mapDotProduct(Block leftMap, Block rightMap) {
    TypedSet rightMapKeys = new TypedSet(VARCHAR, rightMap.getPositionCount(), "cosine_similarity");
    for (int i = 0; i < rightMap.getPositionCount(); i += 2) {
        rightMapKeys.add(rightMap, i);
    }
    double result = 0.0;
    for (int i = 0; i < leftMap.getPositionCount(); i += 2) {
        int position = rightMapKeys.positionOf(leftMap, i);
        if (position != -1) {
            result += DOUBLE.getDouble(leftMap, i + 1) * DOUBLE.getDouble(rightMap, 2 * position + 1);
        }
    }
    return result;
}
Also used : TypedSet(io.prestosql.operator.aggregation.TypedSet) Constraint(io.prestosql.type.Constraint)

Aggregations

TypedSet (io.prestosql.operator.aggregation.TypedSet)11 BlockBuilder (io.prestosql.spi.block.BlockBuilder)10 SqlType (io.prestosql.spi.function.SqlType)7 TypeParameter (io.prestosql.spi.function.TypeParameter)6 Block (io.prestosql.spi.block.Block)5 Type (io.prestosql.spi.type.Type)5 PrestoException (io.prestosql.spi.PrestoException)3 MapType (io.prestosql.spi.type.MapType)3 UsedByGeneratedCode (io.prestosql.spi.annotation.UsedByGeneratedCode)2 SqlNullable (io.prestosql.spi.function.SqlNullable)2 ArrayType (io.prestosql.spi.type.ArrayType)2 RowType (io.prestosql.spi.type.RowType)2 ObjectBigArray (io.prestosql.array.ObjectBigArray)1 CastType (io.prestosql.metadata.CastType)1 PageBuilder (io.prestosql.spi.PageBuilder)1 ScalarFunction (io.prestosql.spi.function.ScalarFunction)1 Constraint (io.prestosql.type.Constraint)1 MethodType.methodType (java.lang.invoke.MethodType.methodType)1