Search in sources :

Example 1 with TypeParameter

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

the class ArrayToArrayCast method filterObject.

@TypeParameter("F")
@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = Object.class)
@SqlType("array(T)")
public static Block filterObject(@TypeParameter("T") Type resultType, @CastDependency(fromType = "F", toType = "T", convention = @Convention(arguments = BLOCK_POSITION, result = NULLABLE_RETURN, session = true)) MethodHandle cast, ConnectorSession session, @SqlType("array(F)") Block array) throws Throwable {
    int positionCount = array.getPositionCount();
    BlockBuilder resultBuilder = resultType.createBlockBuilder(null, positionCount);
    for (int position = 0; position < positionCount; position++) {
        if (!array.isNull(position)) {
            Object value = (Object) cast.invoke(session, array, position);
            if (value != null) {
                resultType.writeObject(resultBuilder, value);
                continue;
            }
        }
        resultBuilder.appendNull();
    }
    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 2 with TypeParameter

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

the class ArrayToArrayCast method filterBoolean.

@TypeParameter("F")
@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = boolean.class)
@SqlType("array(T)")
public static Block filterBoolean(@TypeParameter("T") Type resultType, @CastDependency(fromType = "F", toType = "T", convention = @Convention(arguments = BLOCK_POSITION, result = NULLABLE_RETURN, session = true)) MethodHandle cast, ConnectorSession session, @SqlType("array(F)") Block array) throws Throwable {
    int positionCount = array.getPositionCount();
    BlockBuilder resultBuilder = resultType.createBlockBuilder(null, positionCount);
    for (int position = 0; position < positionCount; position++) {
        if (!array.isNull(position)) {
            Boolean value = (Boolean) cast.invokeExact(session, array, position);
            if (value != null) {
                resultType.writeBoolean(resultBuilder, value);
                continue;
            }
        }
        resultBuilder.appendNull();
    }
    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 3 with TypeParameter

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

the class ArrayToArrayCast method filterDouble.

@TypeParameter("F")
@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = double.class)
@SqlType("array(T)")
public static Block filterDouble(@TypeParameter("T") Type resultType, @CastDependency(fromType = "F", toType = "T", convention = @Convention(arguments = BLOCK_POSITION, result = NULLABLE_RETURN, session = true)) MethodHandle cast, ConnectorSession session, @SqlType("array(F)") Block array) throws Throwable {
    int positionCount = array.getPositionCount();
    BlockBuilder resultBuilder = resultType.createBlockBuilder(null, positionCount);
    for (int position = 0; position < positionCount; position++) {
        if (!array.isNull(position)) {
            Double value = (Double) cast.invokeExact(session, array, position);
            if (value != null) {
                resultType.writeDouble(resultBuilder, value);
                continue;
            }
        }
        resultBuilder.appendNull();
    }
    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 4 with TypeParameter

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

the class ArrayUnionFunction method union.

@TypeParameter("E")
@SqlType("array(E)")
public static Block union(@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 leftArrayCount = leftArray.getPositionCount();
    int rightArrayCount = rightArray.getPositionCount();
    BlockBuilder distinctElementBlockBuilder = type.createBlockBuilder(null, leftArrayCount + rightArrayCount);
    TypedSet typedSet = createEqualityTypedSet(type, elementEqual, elementHashCode, distinctElementBlockBuilder, leftArrayCount + rightArrayCount, "array_union");
    for (int i = 0; i < leftArray.getPositionCount(); i++) {
        typedSet.add(leftArray, i);
    }
    for (int i = 0; i < rightArray.getPositionCount(); i++) {
        typedSet.add(rightArray, i);
    }
    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 5 with TypeParameter

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

the class ArrayFilterFunction method filterDouble.

@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = double.class)
@SqlType("array(T)")
public static Block filterDouble(@TypeParameter("T") Type elementType, @SqlType("array(T)") Block arrayBlock, @SqlType("function(T, boolean)") DoubleToBooleanFunction function) {
    int positionCount = arrayBlock.getPositionCount();
    BlockBuilder resultBuilder = elementType.createBlockBuilder(null, positionCount);
    for (int position = 0; position < positionCount; position++) {
        Double input = null;
        if (!arrayBlock.isNull(position)) {
            input = elementType.getDouble(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)

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