use of com.facebook.presto.spi.function.Description in project presto by prestodb.
the class StringFunctions method replace.
@Description("greedily replaces occurrences of a pattern with a string")
@ScalarFunction
@LiteralParameters({ "x", "y", "z", "u" })
@Constraint(variable = "u", expression = "min(2147483647, x + z * (x + 1))")
@SqlType("varchar(u)")
public static Slice replace(@SqlType("varchar(x)") Slice str, @SqlType("varchar(y)") Slice search, @SqlType("varchar(z)") Slice replace) {
// Empty search?
if (search.length() == 0) {
// With empty `search` we insert `replace` in front of every character and at the end
int resultLength;
try {
resultLength = Math.addExact(Math.multiplyExact(countCodePoints(str) + 1, replace.length()), str.length());
} catch (ArithmeticException e) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "inputs to \"replace\" function are too large: when \"search\" parameter is empty, length of \"string\" times length of \"replace\" must not exceed " + Integer.MAX_VALUE);
}
Slice buffer = Slices.allocate(resultLength);
// Always start with replace
buffer.setBytes(0, replace);
int indexBuffer = replace.length();
// After every code point insert `replace`
int index = 0;
while (index < str.length()) {
int codePointLength = lengthOfCodePointSafe(str, index);
// Append current code point
buffer.setBytes(indexBuffer, str, index, codePointLength);
indexBuffer += codePointLength;
// Append `replace`
buffer.setBytes(indexBuffer, replace);
indexBuffer += replace.length();
// Advance pointer to current code point
index += codePointLength;
}
return buffer;
}
// Allocate a reasonable buffer
Slice buffer = Slices.allocate(str.length());
int index = 0;
int indexBuffer = 0;
while (index < str.length()) {
int matchIndex = str.indexOf(search, index);
// Found a match?
if (matchIndex < 0) {
// No match found so copy the rest of string
int bytesToCopy = str.length() - index;
buffer = Slices.ensureSize(buffer, indexBuffer + bytesToCopy);
buffer.setBytes(indexBuffer, str, index, bytesToCopy);
indexBuffer += bytesToCopy;
break;
}
int bytesToCopy = matchIndex - index;
buffer = Slices.ensureSize(buffer, indexBuffer + bytesToCopy + replace.length());
// Non empty match?
if (bytesToCopy > 0) {
buffer.setBytes(indexBuffer, str, index, bytesToCopy);
indexBuffer += bytesToCopy;
}
// Non empty replace?
if (replace.length() > 0) {
buffer.setBytes(indexBuffer, replace);
indexBuffer += replace.length();
}
// Continue searching after match
index = matchIndex + search.length();
}
return buffer.slice(0, indexBuffer);
}
use of com.facebook.presto.spi.function.Description in project presto by prestodb.
the class AggregationCompiler method getDescription.
private static String getDescription(AnnotatedElement base, AnnotatedElement override) {
Description description = override.getAnnotation(Description.class);
if (description != null) {
return description.value();
}
description = base.getAnnotation(Description.class);
return (description == null) ? null : description.value();
}
use of com.facebook.presto.spi.function.Description in project presto by prestodb.
the class TDigestFunctions method destructureTDigest.
@ScalarFunction(value = "destructure_tdigest", visibility = EXPERIMENTAL)
@Description("Return the raw TDigest, including arrays of centroid means and weights, as well as min, max, sum, count, and compression factor.")
@SqlType("row(centroid_means array(double), centroid_weights array(integer), compression double, min double, max double, sum double, count bigint)")
public static Block destructureTDigest(@SqlType("tdigest(double)") Slice input) {
TDigest tDigest = createTDigest(input);
BlockBuilder blockBuilder = TDIGEST_CENTROIDS_ROW_TYPE.createBlockBuilder(null, 1);
BlockBuilder rowBuilder = blockBuilder.beginBlockEntry();
// Centroid means / weights
BlockBuilder meansBuilder = DOUBLE.createBlockBuilder(null, tDigest.centroidCount());
BlockBuilder weightsBuilder = INTEGER.createBlockBuilder(null, tDigest.centroidCount());
for (Centroid centroid : tDigest.centroids()) {
int weight = (int) centroid.getWeight();
DOUBLE.writeDouble(meansBuilder, centroid.getMean());
INTEGER.writeLong(weightsBuilder, weight);
}
rowBuilder.appendStructure(meansBuilder);
rowBuilder.appendStructure(weightsBuilder);
// Compression, min, max, sum, count
DOUBLE.writeDouble(rowBuilder, tDigest.getCompressionFactor());
DOUBLE.writeDouble(rowBuilder, tDigest.getMin());
DOUBLE.writeDouble(rowBuilder, tDigest.getMax());
DOUBLE.writeDouble(rowBuilder, tDigest.getSum());
BIGINT.writeLong(rowBuilder, (long) tDigest.getSize());
blockBuilder.closeEntry();
return TDIGEST_CENTROIDS_ROW_TYPE.getObject(blockBuilder, blockBuilder.getPositionCount() - 1);
}
use of com.facebook.presto.spi.function.Description in project presto by prestodb.
the class UrlFunctions method urlExtractParameter.
@SqlNullable
@Description("extract query parameter from url")
@ScalarFunction
@LiteralParameters({ "x", "y" })
@SqlType("varchar(x)")
public static Slice urlExtractParameter(@SqlType("varchar(x)") Slice url, @SqlType("varchar(y)") Slice parameterName) {
URI uri = parseUrl(url);
if ((uri == null) || (uri.getQuery() == null)) {
return null;
}
Slice query = slice(uri.getQuery());
String parameter = parameterName.toStringUtf8();
Iterable<String> queryArgs = QUERY_SPLITTER.split(query.toStringUtf8());
for (String queryArg : queryArgs) {
Iterator<String> arg = ARG_SPLITTER.split(queryArg).iterator();
if (arg.next().equals(parameter)) {
if (arg.hasNext()) {
return utf8Slice(arg.next());
}
// first matched key is empty
return Slices.EMPTY_SLICE;
}
}
// no key matched
return null;
}
use of com.facebook.presto.spi.function.Description in project presto by prestodb.
the class CodegenScalarFromAnnotationsParser method createSqlScalarFunction.
private static SqlScalarFunction createSqlScalarFunction(Method method) {
CodegenScalarFunction codegenScalarFunction = method.getAnnotation(CodegenScalarFunction.class);
Signature signature = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, codegenScalarFunction.value()), FunctionKind.SCALAR, Arrays.stream(method.getAnnotationsByType(TypeParameter.class)).map(t -> withVariadicBound(t.value(), t.boundedBy().isEmpty() ? null : t.boundedBy())).collect(toImmutableList()), ImmutableList.of(), parseTypeSignature(method.getAnnotation(SqlType.class).value()), Arrays.stream(method.getParameters()).map(p -> parseTypeSignature(p.getAnnotation(SqlType.class).value())).collect(toImmutableList()), false);
return new SqlScalarFunction(signature) {
@Override
public BuiltInScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, FunctionAndTypeManager functionAndTypeManager) {
Signature boundSignature = applyBoundVariables(signature, boundVariables, arity);
MethodHandle handle;
try {
handle = (MethodHandle) method.invoke(null, boundSignature.getArgumentTypes().stream().map(t -> functionAndTypeManager.getType(t)).toArray());
} catch (Exception e) {
throw new PrestoException(FUNCTION_IMPLEMENTATION_ERROR, format("Method %s does not return valid MethodHandle", method), e);
}
return new BuiltInScalarFunctionImplementation(method.getAnnotation(SqlNullable.class) != null, getArgumentProperties(method), handle, Optional.empty());
}
@Override
public SqlFunctionVisibility getVisibility() {
return codegenScalarFunction.visibility();
}
@Override
public boolean isDeterministic() {
return codegenScalarFunction.deterministic();
}
@Override
public String getDescription() {
Description description = method.getAnnotation(Description.class);
return description == null ? "" : description.value();
}
@Override
public boolean isCalledOnNullInput() {
return codegenScalarFunction.calledOnNullInput();
}
};
}
Aggregations