use of com.facebook.presto.spi.function.FunctionHandle in project presto by prestodb.
the class TestTypeValidator method testInvalidWindowFunctionSignature.
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "type of variable 'sum(_[0-9]+)?' is expected to be double, but the actual type is bigint")
public void testInvalidWindowFunctionSignature() {
VariableReferenceExpression windowVariable = variableAllocator.newVariable("sum", DOUBLE);
// should be DOUBLE
FunctionHandle functionHandle = FUNCTION_MANAGER.lookupFunction("sum", fromTypes(BIGINT));
WindowNode.Frame frame = new WindowNode.Frame(RANGE, UNBOUNDED_PRECEDING, Optional.empty(), UNBOUNDED_FOLLOWING, Optional.empty(), Optional.empty(), Optional.empty());
WindowNode.Function function = new WindowNode.Function(call("sum", functionHandle, BIGINT, variableC), frame, false);
WindowNode.Specification specification = new WindowNode.Specification(ImmutableList.of(), Optional.empty());
PlanNode node = new WindowNode(Optional.empty(), newId(), baseTableScan, specification, ImmutableMap.of(windowVariable, function), Optional.empty(), ImmutableSet.of(), 0);
assertTypesValid(node);
}
use of com.facebook.presto.spi.function.FunctionHandle in project presto by prestodb.
the class TestMapUnionAggregation method testSimpleWithDuplicates.
@Test
public void testSimpleWithDuplicates() {
MapType mapType = mapType(DOUBLE, VARCHAR);
FunctionHandle functionHandle = FUNCTION_AND_TYPE_MANAGER.lookupFunction(NAME, fromTypes(mapType));
InternalAggregationFunction aggFunc = FUNCTION_AND_TYPE_MANAGER.getAggregateFunctionImplementation(functionHandle);
assertAggregation(aggFunc, ImmutableMap.of(23.0, "aaa", 33.0, "bbb", 43.0, "ccc", 53.0, "ddd", 13.0, "eee"), arrayBlockOf(mapType, mapBlockOf(DOUBLE, VARCHAR, ImmutableMap.of(23.0, "aaa", 33.0, "bbb", 53.0, "ddd")), mapBlockOf(DOUBLE, VARCHAR, ImmutableMap.of(43.0, "ccc", 53.0, "ddd", 13.0, "eee"))));
mapType = mapType(DOUBLE, BIGINT);
functionHandle = FUNCTION_AND_TYPE_MANAGER.lookupFunction(NAME, fromTypes(mapType));
aggFunc = FUNCTION_AND_TYPE_MANAGER.getAggregateFunctionImplementation(functionHandle);
assertAggregation(aggFunc, ImmutableMap.of(1.0, 99L, 2.0, 99L, 3.0, 99L, 4.0, 44L), arrayBlockOf(mapType, mapBlockOf(DOUBLE, BIGINT, ImmutableMap.of(1.0, 99L, 2.0, 99L, 3.0, 99L)), mapBlockOf(DOUBLE, BIGINT, ImmutableMap.of(1.0, 44L, 2.0, 44L, 4.0, 44L))));
mapType = mapType(BOOLEAN, BIGINT);
functionHandle = FUNCTION_AND_TYPE_MANAGER.lookupFunction(NAME, fromTypes(mapType));
aggFunc = FUNCTION_AND_TYPE_MANAGER.getAggregateFunctionImplementation(functionHandle);
assertAggregation(aggFunc, ImmutableMap.of(false, 12L, true, 13L), arrayBlockOf(mapType, mapBlockOf(BOOLEAN, BIGINT, ImmutableMap.of(false, 12L)), mapBlockOf(BOOLEAN, BIGINT, ImmutableMap.of(true, 13L, false, 33L))));
}
use of com.facebook.presto.spi.function.FunctionHandle in project presto by prestodb.
the class RowIndeterminateOperator method generateIndeterminate.
private static Class<?> generateIndeterminate(Type type, FunctionAndTypeManager functionAndTypeManager) {
CallSiteBinder binder = new CallSiteBinder();
ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName("RowIndeterminateOperator"), type(Object.class));
Parameter value = arg("value", type.getJavaType());
Parameter isNull = arg("isNull", boolean.class);
MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "indeterminate", type(boolean.class), value, isNull);
Scope scope = method.getScope();
BytecodeBlock body = method.getBody();
Variable wasNull = scope.declareVariable(boolean.class, "wasNull");
body.append(wasNull.set(constantFalse()));
CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(definition, binder);
LabelNode end = new LabelNode("end");
List<Type> fieldTypes = type.getTypeParameters();
boolean hasUnknownFields = fieldTypes.stream().anyMatch(fieldType -> fieldType.equals(UNKNOWN));
body.append(new IfStatement("if isNull is true...").condition(isNull).ifTrue(new BytecodeBlock().push(true).gotoLabel(end)));
if (hasUnknownFields) {
// if the current field type is UNKNOWN which means this field is null, directly return true
body.push(true).gotoLabel(end);
} else {
for (int i = 0; i < fieldTypes.size(); i++) {
IfStatement ifNullField = new IfStatement("if the field is null...");
ifNullField.condition(value.invoke("isNull", boolean.class, constantInt(i))).ifTrue(new BytecodeBlock().push(true).gotoLabel(end));
FunctionHandle functionHandle = functionAndTypeManager.resolveOperator(INDETERMINATE, fromTypes(fieldTypes.get(i)));
JavaScalarFunctionImplementation function = functionAndTypeManager.getJavaScalarFunctionImplementation(functionHandle);
BytecodeExpression element = constantType(binder, fieldTypes.get(i)).getValue(value, constantInt(i));
ifNullField.ifFalse(new IfStatement("if the field is not null but indeterminate...").condition(invokeFunction(scope, cachedInstanceBinder, functionAndTypeManager.getFunctionMetadata(functionHandle).getName().getObjectName(), function, element)).ifTrue(new BytecodeBlock().push(true).gotoLabel(end)));
body.append(ifNullField);
}
// if none of the fields is indeterminate, then push false
body.push(false);
}
body.visitLabel(end).retBoolean();
// create constructor
MethodDefinition constructorDefinition = definition.declareConstructor(a(PUBLIC));
BytecodeBlock constructorBody = constructorDefinition.getBody();
Variable thisVariable = constructorDefinition.getThis();
constructorBody.comment("super();").append(thisVariable).invokeConstructor(Object.class);
cachedInstanceBinder.generateInitializations(thisVariable, constructorBody);
constructorBody.ret();
return defineClass(definition, Object.class, binder.getBindings(), RowIndeterminateOperator.class.getClassLoader());
}
use of com.facebook.presto.spi.function.FunctionHandle in project presto by prestodb.
the class RowComparisonOperator method getMethodHandles.
protected List<MethodHandle> getMethodHandles(RowType type, FunctionAndTypeManager functionAndTypeManager, OperatorType operatorType) {
ImmutableList.Builder<MethodHandle> argumentMethods = ImmutableList.builder();
for (Type parameterType : type.getTypeParameters()) {
FunctionHandle operatorHandle = functionAndTypeManager.resolveOperator(operatorType, fromTypes(parameterType, parameterType));
argumentMethods.add(functionAndTypeManager.getJavaScalarFunctionImplementation(operatorHandle).getMethodHandle());
}
return argumentMethods.build();
}
use of com.facebook.presto.spi.function.FunctionHandle in project presto by prestodb.
the class RowDistinctFromOperator method specialize.
@Override
public BuiltInScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, FunctionAndTypeManager functionAndTypeManager) {
ImmutableList.Builder<MethodHandle> argumentMethods = ImmutableList.builder();
Type type = boundVariables.getTypeVariable("T");
for (Type parameterType : type.getTypeParameters()) {
FunctionHandle operatorHandle = functionAndTypeManager.resolveOperator(IS_DISTINCT_FROM, fromTypes(parameterType, parameterType));
FunctionInvoker functionInvoker = functionAndTypeManager.getFunctionInvokerProvider().createFunctionInvoker(operatorHandle, Optional.of(new InvocationConvention(ImmutableList.of(NULL_FLAG, NULL_FLAG), InvocationConvention.InvocationReturnConvention.FAIL_ON_NULL, false)));
argumentMethods.add(functionInvoker.methodHandle());
}
return new BuiltInScalarFunctionImplementation(ImmutableList.of(new ScalarFunctionImplementationChoice(false, ImmutableList.of(valueTypeArgumentProperty(USE_NULL_FLAG), valueTypeArgumentProperty(USE_NULL_FLAG)), ReturnPlaceConvention.STACK, METHOD_HANDLE_NULL_FLAG.bindTo(type).bindTo(argumentMethods.build()), Optional.empty()), new ScalarFunctionImplementationChoice(false, ImmutableList.of(valueTypeArgumentProperty(BLOCK_AND_POSITION), valueTypeArgumentProperty(BLOCK_AND_POSITION)), ReturnPlaceConvention.STACK, METHOD_HANDLE_BLOCK_POSITION.bindTo(type).bindTo(argumentMethods.build()), Optional.empty())));
}
Aggregations