Search in sources :

Example 21 with SqlNullable

use of com.facebook.presto.spi.function.SqlNullable in project presto by prestodb.

the class JsonOperators method castToBoolean.

@ScalarOperator(CAST)
@SqlNullable
@SqlType(BOOLEAN)
public static Boolean castToBoolean(@SqlType(JSON) Slice json) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        parser.nextToken();
        Boolean result = currentTokenAsBoolean(parser);
        // check no trailing token
        checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to BOOLEAN");
        return result;
    } catch (IOException | JsonCastException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), BOOLEAN), e);
    }
}
Also used : JsonCastException(com.facebook.presto.util.JsonCastException) PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException) JsonUtil.currentTokenAsBoolean(com.facebook.presto.util.JsonUtil.currentTokenAsBoolean) JsonUtil.createJsonParser(com.facebook.presto.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) SqlNullable(com.facebook.presto.spi.function.SqlNullable) SqlType(com.facebook.presto.spi.function.SqlType)

Example 22 with SqlNullable

use of com.facebook.presto.spi.function.SqlNullable in project presto by prestodb.

the class QuantileDigestFunctions method quantileAtValueBigint.

@ScalarFunction("quantile_at_value")
@Description("Given an input x between min/max values of qdigest, find which quantile is represented by that value")
@SqlType(StandardTypes.DOUBLE)
@SqlNullable
public static Double quantileAtValueBigint(@SqlType("qdigest(bigint)") Slice input, @SqlType(StandardTypes.BIGINT) long value) {
    QuantileDigest digest = new QuantileDigest(input);
    if (digest.getCount() == 0 || value > digest.getMax() || value < digest.getMin()) {
        return null;
    }
    double bucketCount = digest.getHistogram(ImmutableList.of(value)).get(0).getCount();
    return bucketCount / digest.getCount();
}
Also used : QuantileDigest(com.facebook.airlift.stats.QuantileDigest) SqlNullable(com.facebook.presto.spi.function.SqlNullable) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Example 23 with SqlNullable

use of com.facebook.presto.spi.function.SqlNullable in project presto by prestodb.

the class Re2JRegexpReplaceLambdaFunction method regexpReplace.

@LiteralParameters("x")
@SqlType("varchar")
@SqlNullable
public Slice regexpReplace(@SqlType("varchar") Slice source, @SqlType(Re2JRegexpType.NAME) Re2JRegexp pattern, @SqlType("function(array(varchar), varchar(x))") UnaryFunctionInterface replaceFunction) {
    // If there is no match we can simply return the original source without doing copy.
    Matcher matcher = pattern.re2jPattern.matcher(source);
    if (!matcher.find()) {
        return source;
    }
    SliceOutput output = new DynamicSliceOutput(source.length());
    BlockBuilder blockBuilder = VARCHAR.createBlockBuilder(null, 10);
    int groupCount = matcher.groupCount();
    int appendPosition = 0;
    do {
        int start = matcher.start();
        int end = matcher.end();
        // Append the un-matched part
        if (appendPosition < start) {
            output.writeBytes(source, appendPosition, start - appendPosition);
        }
        appendPosition = end;
        // Append the capturing groups to the target block that will be passed to lambda
        for (int i = 1; i <= groupCount; i++) {
            Slice matchedGroupSlice = matcher.group(i);
            if (matchedGroupSlice != null) {
                VARCHAR.writeSlice(blockBuilder, matchedGroupSlice);
            } else {
                blockBuilder.appendNull();
            }
        }
        Block target = blockBuilder.getRegion(blockBuilder.getPositionCount() - groupCount, groupCount);
        // Call the lambda function to replace the block, and append the result to output
        Slice replaced = (Slice) replaceFunction.apply(target);
        if (replaced == null) {
            // replacing a substring with null (unknown) makes the entire string null
            return null;
        }
        output.appendBytes(replaced);
    } while (matcher.find());
    // Append the rest of un-matched
    output.writeBytes(source, appendPosition, source.length() - appendPosition);
    return output.slice();
}
Also used : SliceOutput(io.airlift.slice.SliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) Matcher(com.google.re2j.Matcher) Slice(io.airlift.slice.Slice) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) Block(com.facebook.presto.common.block.Block) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) SqlNullable(com.facebook.presto.spi.function.SqlNullable) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) SqlType(com.facebook.presto.spi.function.SqlType)

Example 24 with SqlNullable

use of com.facebook.presto.spi.function.SqlNullable in project presto by prestodb.

the class KHyperLogLogFunctions method mergeKhll.

@ScalarFunction
@SqlType(KHyperLogLogType.NAME)
@SqlNullable
public static Slice mergeKhll(@SqlType("array(KHyperLogLog)") Block block) {
    if (block.getPositionCount() == 0) {
        return null;
    }
    KHyperLogLog merged = null;
    int firstNonNullIndex = 0;
    while (firstNonNullIndex < block.getPositionCount() && block.isNull(firstNonNullIndex)) {
        firstNonNullIndex++;
    }
    if (firstNonNullIndex == block.getPositionCount()) {
        return null;
    }
    Slice initialSlice = block.getSlice(firstNonNullIndex, 0, block.getSliceLength(firstNonNullIndex));
    merged = KHyperLogLog.newInstance(initialSlice);
    for (int i = firstNonNullIndex; i < block.getPositionCount(); i++) {
        Slice currentSlice = block.getSlice(i, 0, block.getSliceLength(i));
        if (!block.isNull(i)) {
            merged = KHyperLogLog.merge(merged, KHyperLogLog.newInstance(currentSlice));
        }
    }
    return merged.serialize();
}
Also used : Slice(io.airlift.slice.Slice) SqlNullable(com.facebook.presto.spi.function.SqlNullable) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) SqlType(com.facebook.presto.spi.function.SqlType)

Example 25 with SqlNullable

use of com.facebook.presto.spi.function.SqlNullable in project presto by prestodb.

the class ArrayAnyMatchFunction method anyMatchSlice.

@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = Slice.class)
@SqlType(StandardTypes.BOOLEAN)
@SqlNullable
public static Boolean anyMatchSlice(@TypeParameter("T") Type elementType, @SqlType("array(T)") Block arrayBlock, @SqlType("function(T, boolean)") SliceToBooleanFunction function) {
    boolean hasNullResult = false;
    int positionCount = arrayBlock.getPositionCount();
    for (int i = 0; i < positionCount; i++) {
        Slice element = null;
        if (!arrayBlock.isNull(i)) {
            element = elementType.getSlice(arrayBlock, i);
        }
        Boolean match = function.apply(element);
        if (TRUE.equals(match)) {
            return true;
        }
        if (match == null) {
            hasNullResult = true;
        }
    }
    if (hasNullResult) {
        return null;
    }
    return false;
}
Also used : Slice(io.airlift.slice.Slice) TypeParameterSpecialization(com.facebook.presto.spi.function.TypeParameterSpecialization) SqlNullable(com.facebook.presto.spi.function.SqlNullable) TypeParameter(com.facebook.presto.spi.function.TypeParameter) SqlType(com.facebook.presto.spi.function.SqlType)

Aggregations

SqlNullable (com.facebook.presto.spi.function.SqlNullable)61 SqlType (com.facebook.presto.spi.function.SqlType)61 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)44 Description (com.facebook.presto.spi.function.Description)37 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)29 PrestoException (com.facebook.presto.spi.PrestoException)17 OGCGeometry.createFromEsriGeometry (com.esri.core.geometry.ogc.OGCGeometry.createFromEsriGeometry)15 GeometryUtils.jsonFromJtsGeometry (com.facebook.presto.geospatial.GeometryUtils.jsonFromJtsGeometry)15 GeometryUtils.wktFromJtsGeometry (com.facebook.presto.geospatial.GeometryUtils.wktFromJtsGeometry)15 Geometry (org.locationtech.jts.geom.Geometry)15 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)14 JsonParser (com.fasterxml.jackson.core.JsonParser)14 IOException (java.io.IOException)14 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)9 ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)8 JsonCastException (com.facebook.presto.util.JsonCastException)8 Slice (io.airlift.slice.Slice)8 Point (com.esri.core.geometry.Point)7 LiteralParameters (com.facebook.presto.spi.function.LiteralParameters)7 TypeParameter (com.facebook.presto.spi.function.TypeParameter)7