Search in sources :

Example 6 with SqlType

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

the class ArrayDistinctFunction method bigintDistinct.

@SqlType("array(bigint)")
public Block bigintDistinct(@SqlType("array(bigint)") Block array) {
    if (array.getPositionCount() == 0) {
        return array;
    }
    boolean containsNull = false;
    LongSet set = new LongOpenHashSet(array.getPositionCount());
    int distinctCount = 0;
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    BlockBuilder distinctElementBlockBuilder = pageBuilder.getBlockBuilder(0);
    for (int i = 0; i < array.getPositionCount(); i++) {
        if (array.isNull(i)) {
            if (!containsNull) {
                containsNull = true;
                distinctElementBlockBuilder.appendNull();
                distinctCount++;
            }
            continue;
        }
        long value = BIGINT.getLong(array, i);
        if (!set.contains(value)) {
            set.add(value);
            distinctCount++;
            BIGINT.appendTo(array, i, distinctElementBlockBuilder);
        }
    }
    pageBuilder.declarePositions(distinctCount);
    return distinctElementBlockBuilder.getRegion(distinctElementBlockBuilder.getPositionCount() - distinctCount, distinctCount);
}
Also used : LongSet(it.unimi.dsi.fastutil.longs.LongSet) LongOpenHashSet(it.unimi.dsi.fastutil.longs.LongOpenHashSet) BlockBuilder(io.trino.spi.block.BlockBuilder) SqlType(io.trino.spi.function.SqlType)

Example 7 with SqlType

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

Example 8 with SqlType

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

the class ArrayFilterFunction method filterObject.

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

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

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

the class SplitToMapFunction method splitToMap.

@SqlType("map(varchar,varchar)")
public Block splitToMap(@TypeParameter("map(varchar,varchar)") Type mapType, @SqlType(StandardTypes.VARCHAR) Slice string, @SqlType(StandardTypes.VARCHAR) Slice entryDelimiter, @SqlType(StandardTypes.VARCHAR) Slice keyValueDelimiter) {
    checkCondition(entryDelimiter.length() > 0, INVALID_FUNCTION_ARGUMENT, "entryDelimiter is empty");
    checkCondition(keyValueDelimiter.length() > 0, INVALID_FUNCTION_ARGUMENT, "keyValueDelimiter is empty");
    checkCondition(!entryDelimiter.equals(keyValueDelimiter), INVALID_FUNCTION_ARGUMENT, "entryDelimiter and keyValueDelimiter must not be the same");
    Map<Slice, Slice> map = new HashMap<>();
    int entryStart = 0;
    while (entryStart < string.length()) {
        // Extract key-value pair based on current index
        // then add the pair if it can be split by keyValueDelimiter
        Slice keyValuePair;
        int entryEnd = string.indexOf(entryDelimiter, entryStart);
        if (entryEnd >= 0) {
            keyValuePair = string.slice(entryStart, entryEnd - entryStart);
        } else {
            // The rest of the string is the last possible pair.
            keyValuePair = string.slice(entryStart, string.length() - entryStart);
        }
        int keyEnd = keyValuePair.indexOf(keyValueDelimiter);
        if (keyEnd >= 0) {
            int valueStart = keyEnd + keyValueDelimiter.length();
            Slice key = keyValuePair.slice(0, keyEnd);
            Slice value = keyValuePair.slice(valueStart, keyValuePair.length() - valueStart);
            if (value.indexOf(keyValueDelimiter) >= 0) {
                throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Key-value delimiter must appear exactly once in each entry. Bad input: '" + keyValuePair.toStringUtf8() + "'");
            }
            if (map.containsKey(key)) {
                throw new TrinoException(INVALID_FUNCTION_ARGUMENT, format("Duplicate keys (%s) are not allowed", key.toStringUtf8()));
            }
            map.put(key, value);
        } else {
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Key-value delimiter must appear exactly once in each entry. Bad input: '" + keyValuePair.toStringUtf8() + "'");
        }
        if (entryEnd < 0) {
            // No more pairs to add
            break;
        }
        // Next possible pair is placed next to the current entryDelimiter
        entryStart = entryEnd + entryDelimiter.length();
    }
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
    BlockBuilder singleMapBlockBuilder = blockBuilder.beginBlockEntry();
    for (Map.Entry<Slice, Slice> entry : map.entrySet()) {
        VARCHAR.writeSlice(singleMapBlockBuilder, entry.getKey());
        VARCHAR.writeSlice(singleMapBlockBuilder, entry.getValue());
    }
    blockBuilder.closeEntry();
    pageBuilder.declarePosition();
    return (Block) mapType.getObject(blockBuilder, blockBuilder.getPositionCount() - 1);
}
Also used : HashMap(java.util.HashMap) Slice(io.airlift.slice.Slice) TrinoException(io.trino.spi.TrinoException) Block(io.trino.spi.block.Block) HashMap(java.util.HashMap) Map(java.util.Map) BlockBuilder(io.trino.spi.block.BlockBuilder) 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