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);
}
}
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();
}
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();
}
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();
}
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;
}
Aggregations