Search in sources :

Example 6 with TypeParameterSpecialization

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

the class ArrayFilterFunction method filterBoolean.

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

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

the class FunctionsParserHelper method getDeclaredSpecializedTypeParameters.

public static Map<String, Class<?>> getDeclaredSpecializedTypeParameters(Method method, Set<TypeParameter> typeParameters) {
    Map<String, Class<?>> specializedTypeParameters = new HashMap<>();
    TypeParameterSpecialization[] typeParameterSpecializations = method.getAnnotationsByType(TypeParameterSpecialization.class);
    ImmutableSet<String> typeParameterNames = typeParameters.stream().map(TypeParameter::value).collect(toImmutableSet());
    for (TypeParameterSpecialization specialization : typeParameterSpecializations) {
        checkArgument(typeParameterNames.contains(specialization.name()), "%s does not match any declared type parameters (%s) [%s]", specialization.name(), typeParameters, method);
        Class<?> existingSpecialization = specializedTypeParameters.get(specialization.name());
        checkArgument(existingSpecialization == null || existingSpecialization.equals(specialization.nativeContainerType()), "%s has conflicting specializations %s and %s [%s]", specialization.name(), existingSpecialization, specialization.nativeContainerType(), method);
        specializedTypeParameters.put(specialization.name(), specialization.nativeContainerType());
    }
    return specializedTypeParameters;
}
Also used : TypeParameterSpecialization(io.trino.spi.function.TypeParameterSpecialization) HashMap(java.util.HashMap)

Example 8 with TypeParameterSpecialization

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

the class ArrayToArrayCast method filterLong.

@TypeParameter("F")
@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = long.class)
@SqlType("array(T)")
public static Block filterLong(@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)) {
            Long value = (Long) cast.invokeExact(session, array, position);
            if (value != null) {
                resultType.writeLong(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 9 with TypeParameterSpecialization

use of io.trino.spi.function.TypeParameterSpecialization 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)

Aggregations

TypeParameterSpecialization (io.trino.spi.function.TypeParameterSpecialization)9 BlockBuilder (io.trino.spi.block.BlockBuilder)8 SqlType (io.trino.spi.function.SqlType)8 TypeParameter (io.trino.spi.function.TypeParameter)8 HashMap (java.util.HashMap)1