use of com.facebook.presto.spi.function.FunctionKind.SCALAR in project presto by prestodb.
the class TestPolymorphicScalarFunction method testSetsHiddenToTrueForOperators.
@Test
public void testSetsHiddenToTrueForOperators() {
Signature signature = SignatureBuilder.builder().operatorType(ADD).kind(SCALAR).returnType(parseTypeSignature("varchar(x)", ImmutableSet.of("x"))).argumentTypes(parseTypeSignature("varchar(x)", ImmutableSet.of("x"))).build();
SqlScalarFunction function = SqlScalarFunction.builder(TestMethods.class, ADD).signature(signature).deterministic(true).choice(choice -> choice.implementation(methodsGroup -> methodsGroup.methods("varcharToVarchar"))).build();
BuiltInScalarFunctionImplementation functionImplementation = function.specialize(BOUND_VARIABLES, 1, FUNCTION_AND_TYPE_MANAGER);
}
use of com.facebook.presto.spi.function.FunctionKind.SCALAR in project presto by prestodb.
the class HiveScalarFunctionInvoker method createFunctionInvoker.
public static HiveScalarFunctionInvoker createFunctionInvoker(Class<?> cls, QualifiedObjectName name, List<TypeSignature> arguments, TypeManager typeManager) {
final List<Type> argumentTypes = arguments.stream().map(typeManager::getType).collect(Collectors.toList());
try {
// Step 1: Create function instance
final GenericUDF udf = createGenericUDF(name, cls);
// Step 2: Initialize function
ObjectInspector[] inputInspectors = argumentTypes.stream().map(argumentType -> ObjectInspectors.create(argumentType, typeManager)).toArray(ObjectInspector[]::new);
ObjectInspector resultInspector = udf.initialize(inputInspectors);
// Step 3: Create invoker
Type resultType = PrestoTypes.fromObjectInspector(resultInspector, typeManager);
ObjectInputDecoder[] argumentDecoders = argumentTypes.stream().map(argumentsType -> createDecoder(argumentsType, typeManager)).toArray(ObjectInputDecoder[]::new);
ObjectEncoder resultEncoder = createEncoder(resultType, resultInspector);
Signature signature = new Signature(name, SCALAR, resultType.getTypeSignature(), arguments);
// Step 4: Create ThreadLocal GenericUDF
final ThreadLocal<GenericUDF> genericUDFSupplier = ThreadLocal.withInitial(() -> {
try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(cls.getClassLoader())) {
GenericUDF ret = createGenericUDF(name, cls);
ret.initialize(inputInspectors);
return ret;
} catch (Exception e) {
throw initializationError(e);
}
});
return new HiveScalarFunctionInvoker(signature, genericUDFSupplier::get, argumentDecoders, resultEncoder);
} catch (Exception e) {
throw initializationError(e);
}
}
use of com.facebook.presto.spi.function.FunctionKind.SCALAR in project presto by prestodb.
the class DecimalOperators method decimalDivideOperator.
private static SqlScalarFunction decimalDivideOperator() {
TypeSignature decimalLeftSignature = parseTypeSignature("decimal(a_precision, a_scale)", ImmutableSet.of("a_precision", "a_scale"));
TypeSignature decimalRightSignature = parseTypeSignature("decimal(b_precision, b_scale)", ImmutableSet.of("b_precision", "b_scale"));
TypeSignature decimalResultSignature = parseTypeSignature("decimal(r_precision, r_scale)", ImmutableSet.of("r_precision", "r_scale"));
// we extend target precision by b_scale. This is upper bound on how much division result will grow.
// pessimistic case is a / 0.0000001
// if scale of divisor is greater than scale of dividend we extend scale further as we
// want result scale to be maximum of scales of divisor and dividend.
Signature signature = SignatureBuilder.builder().kind(SCALAR).operatorType(DIVIDE).longVariableConstraints(longVariableExpression("r_precision", "min(38, a_precision + b_scale + max(b_scale - a_scale, 0))"), longVariableExpression("r_scale", "max(a_scale, b_scale)")).argumentTypes(decimalLeftSignature, decimalRightSignature).returnType(decimalResultSignature).build();
return SqlScalarFunction.builder(DecimalOperators.class, DIVIDE).signature(signature).deterministic(true).choice(choice -> choice.implementation(methodsGroup -> methodsGroup.methods("divideShortShortShort", "divideShortLongShort", "divideLongShortShort", "divideShortShortLong", "divideLongLongLong", "divideShortLongLong", "divideLongShortLong").withExtraParameters(DecimalOperators::divideRescaleFactor))).build();
}
use of com.facebook.presto.spi.function.FunctionKind.SCALAR in project presto by prestodb.
the class DecimalOperators method decimalSubtractOperator.
private static SqlScalarFunction decimalSubtractOperator() {
TypeSignature decimalLeftSignature = parseTypeSignature("decimal(a_precision, a_scale)", ImmutableSet.of("a_precision", "a_scale"));
TypeSignature decimalRightSignature = parseTypeSignature("decimal(b_precision, b_scale)", ImmutableSet.of("b_precision", "b_scale"));
TypeSignature decimalResultSignature = parseTypeSignature("decimal(r_precision, r_scale)", ImmutableSet.of("r_precision", "r_scale"));
Signature signature = SignatureBuilder.builder().kind(SCALAR).operatorType(SUBTRACT).longVariableConstraints(longVariableExpression("r_precision", "min(38, max(a_precision - a_scale, b_precision - b_scale) + max(a_scale, b_scale) + 1)"), longVariableExpression("r_scale", "max(a_scale, b_scale)")).argumentTypes(decimalLeftSignature, decimalRightSignature).returnType(decimalResultSignature).build();
return SqlScalarFunction.builder(DecimalOperators.class, SUBTRACT).signature(signature).deterministic(true).choice(choice -> choice.implementation(methodsGroup -> methodsGroup.methods("subtractShortShortShort").withExtraParameters(DecimalOperators::calculateShortRescaleParameters)).implementation(methodsGroup -> methodsGroup.methods("subtractShortShortLong", "subtractLongLongLong", "subtractShortLongLong", "subtractLongShortLong").withExtraParameters(DecimalOperators::calculateLongRescaleParameters))).build();
}
use of com.facebook.presto.spi.function.FunctionKind.SCALAR in project presto by prestodb.
the class DecimalOperators method decimalAddOperator.
private static SqlScalarFunction decimalAddOperator() {
TypeSignature decimalLeftSignature = parseTypeSignature("decimal(a_precision, a_scale)", ImmutableSet.of("a_precision", "a_scale"));
TypeSignature decimalRightSignature = parseTypeSignature("decimal(b_precision, b_scale)", ImmutableSet.of("b_precision", "b_scale"));
TypeSignature decimalResultSignature = parseTypeSignature("decimal(r_precision, r_scale)", ImmutableSet.of("r_precision", "r_scale"));
Signature signature = SignatureBuilder.builder().kind(SCALAR).operatorType(ADD).longVariableConstraints(longVariableExpression("r_precision", "min(38, max(a_precision - a_scale, b_precision - b_scale) + max(a_scale, b_scale) + 1)"), longVariableExpression("r_scale", "max(a_scale, b_scale)")).argumentTypes(decimalLeftSignature, decimalRightSignature).returnType(decimalResultSignature).build();
return SqlScalarFunction.builder(DecimalOperators.class, ADD).signature(signature).deterministic(true).choice(choice -> choice.implementation(methodsGroup -> methodsGroup.methods("addShortShortShort").withExtraParameters(DecimalOperators::calculateShortRescaleParameters)).implementation(methodsGroup -> methodsGroup.methods("addShortShortLong", "addLongLongLong", "addShortLongLong", "addLongShortLong").withExtraParameters(DecimalOperators::calculateLongRescaleParameters))).build();
}
Aggregations