Search in sources :

Example 1 with SqlType

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

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

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

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

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

the class ColorFunctions method bar.

@ScalarFunction
@SqlType(StandardTypes.VARCHAR)
public static Slice bar(@SqlType(StandardTypes.DOUBLE) double percent, @SqlType(StandardTypes.BIGINT) long width, @SqlType(ColorType.NAME) long lowColor, @SqlType(ColorType.NAME) long highColor) {
    long count = (int) (percent * width);
    count = Math.min(width, count);
    count = Math.max(0, count);
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < count; i++) {
        float fraction = (float) (i * 1.0 / (width - 1));
        int color = interpolate(fraction, lowColor, highColor);
        builder.append(ansiColorEscape(color)).append('\u2588');
    }
    // reset
    builder.append(ANSI_RESET);
    // pad to force column to be the requested width
    for (long i = count; i < width; ++i) {
        builder.append(' ');
    }
    return utf8Slice(builder.toString());
}
Also used : Constraint(io.trino.type.Constraint) ScalarFunction(io.trino.spi.function.ScalarFunction) SqlType(io.trino.spi.function.SqlType)

Aggregations

SqlType (io.trino.spi.function.SqlType)176 ScalarFunction (io.trino.spi.function.ScalarFunction)107 Description (io.trino.spi.function.Description)90 LiteralParameters (io.trino.spi.function.LiteralParameters)60 SqlNullable (io.trino.spi.function.SqlNullable)56 BlockBuilder (io.trino.spi.block.BlockBuilder)52 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)46 TrinoException (io.trino.spi.TrinoException)37 Constraint (io.trino.type.Constraint)23 Slice (io.airlift.slice.Slice)22 TypeParameter (io.trino.spi.function.TypeParameter)21 Point (com.esri.core.geometry.Point)17 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)17 MultiPoint (com.esri.core.geometry.MultiPoint)15 JsonParser (com.fasterxml.jackson.core.JsonParser)14 JsonUtil.createJsonParser (io.trino.util.JsonUtil.createJsonParser)14 IOException (java.io.IOException)14 ScalarOperator (io.trino.spi.function.ScalarOperator)13 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)11 Matcher (io.airlift.joni.Matcher)9