use of com.facebook.presto.spi.function.Description in project presto by prestodb.
the class VarbinaryFunctions method reverse.
@Description("Reverse a given varbinary")
@ScalarFunction("reverse")
@SqlType(StandardTypes.VARBINARY)
public static Slice reverse(@SqlType("varbinary") Slice inputSlice) {
if (inputSlice.length() == 0) {
return EMPTY_SLICE;
}
int length = inputSlice.length();
Slice reverse = Slices.allocate(length);
for (int i = 0; i < length; i++) {
reverse.setByte(i, inputSlice.getByte((length - 1) - i));
}
return reverse;
}
use of com.facebook.presto.spi.function.Description in project presto by prestodb.
the class SqlInvokedScalarFromAnnotationsParser method parseFunctionDefinition.
public static List<SqlInvokedFunction> parseFunctionDefinition(Class<?> clazz) {
checkArgument(clazz.isAnnotationPresent(SqlInvokedScalarFunction.class), "Class is not annotated with SqlInvokedScalarFunction: %s", clazz.getName());
SqlInvokedScalarFunction header = clazz.getAnnotation(SqlInvokedScalarFunction.class);
Optional<String> description = Optional.ofNullable(clazz.getAnnotation(Description.class)).map(Description::value);
return findScalarsInFunctionDefinitionClass(clazz).stream().map(method -> createSqlInvokedFunctions(method, Optional.of(header), description)).flatMap(List::stream).collect(toImmutableList());
}
use of com.facebook.presto.spi.function.Description in project presto by prestodb.
the class SqlInvokedScalarFromAnnotationsParser method createSqlInvokedFunctions.
private static List<SqlInvokedFunction> createSqlInvokedFunctions(Method method, Optional<SqlInvokedScalarFunction> header, Optional<String> description) {
SqlInvokedScalarFunction functionHeader = header.orElse(method.getAnnotation(SqlInvokedScalarFunction.class));
String functionDescription = description.orElse(method.isAnnotationPresent(Description.class) ? method.getAnnotation(Description.class).value() : "");
TypeSignature returnType = parseTypeSignature(method.getAnnotation(SqlType.class).value());
// Parameter
checkCondition(!method.isAnnotationPresent(SqlParameter.class) || !method.isAnnotationPresent(SqlParameters.class), FUNCTION_IMPLEMENTATION_ERROR, "Function-defining method [%s] is annotated with both @SqlParameter and @SqlParameters", method);
List<Parameter> parameters;
if (method.isAnnotationPresent(SqlParameter.class)) {
parameters = ImmutableList.of(getParameterFromAnnotation(method.getAnnotation(SqlParameter.class)));
} else if (method.isAnnotationPresent(SqlParameters.class)) {
parameters = stream(method.getAnnotation(SqlParameters.class).value()).map(SqlInvokedScalarFromAnnotationsParser::getParameterFromAnnotation).collect(toImmutableList());
} else {
parameters = ImmutableList.of();
}
// Routine characteristics
RoutineCharacteristics routineCharacteristics = RoutineCharacteristics.builder().setLanguage(RoutineCharacteristics.Language.SQL).setDeterminism(functionHeader.deterministic() ? DETERMINISTIC : NOT_DETERMINISTIC).setNullCallClause(functionHeader.calledOnNullInput() ? CALLED_ON_NULL_INPUT : RETURNS_NULL_ON_NULL_INPUT).build();
String body;
try {
body = (String) method.invoke(null);
} catch (ReflectiveOperationException e) {
throw new PrestoException(FUNCTION_IMPLEMENTATION_ERROR, format("Failed to get function body for method [%s]", method), e);
}
return Stream.concat(Stream.of(functionHeader.value()), stream(functionHeader.alias())).map(name -> new SqlInvokedFunction(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, name), parameters, returnType, functionDescription, routineCharacteristics, body, notVersioned())).collect(toImmutableList());
}
use of com.facebook.presto.spi.function.Description in project presto by prestodb.
the class MathFunctions method inverseWeibullCdf.
@Description("Inverse of Weibull cdf given a, b parameters and probability")
@ScalarFunction
@SqlType(StandardTypes.DOUBLE)
public static double inverseWeibullCdf(@SqlType(StandardTypes.DOUBLE) double a, @SqlType(StandardTypes.DOUBLE) double b, @SqlType(StandardTypes.DOUBLE) double p) {
checkCondition(p >= 0 && p <= 1, INVALID_FUNCTION_ARGUMENT, "p must be in the interval [0, 1]");
checkCondition(a > 0, INVALID_FUNCTION_ARGUMENT, "a must be greater than 0");
checkCondition(b > 0, INVALID_FUNCTION_ARGUMENT, "b must be greater than 0");
WeibullDistribution distribution = new WeibullDistribution(null, a, b, WeibullDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
return distribution.inverseCumulativeProbability(p);
}
use of com.facebook.presto.spi.function.Description in project presto by prestodb.
the class MathFunctions method chiSquaredCdf.
@Description("ChiSquared cdf given the df parameter and value")
@ScalarFunction
@SqlType(StandardTypes.DOUBLE)
public static double chiSquaredCdf(@SqlType(StandardTypes.DOUBLE) double df, @SqlType(StandardTypes.DOUBLE) double value) {
checkCondition(value >= 0, INVALID_FUNCTION_ARGUMENT, "value must non-negative");
checkCondition(df > 0, INVALID_FUNCTION_ARGUMENT, "df must be greater than 0");
ChiSquaredDistribution distribution = new ChiSquaredDistribution(null, df, ChiSquaredDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
return distribution.cumulativeProbability(value);
}
Aggregations