Search in sources :

Example 6 with FunctionRegistry

use of com.facebook.presto.metadata.FunctionRegistry in project presto by prestodb.

the class FunctionCallCodeGenerator method generateExpression.

@Override
public BytecodeNode generateExpression(Signature signature, BytecodeGeneratorContext context, Type returnType, List<RowExpression> arguments) {
    FunctionRegistry registry = context.getRegistry();
    ScalarFunctionImplementation function = registry.getScalarFunctionImplementation(signature);
    List<BytecodeNode> argumentsBytecode = new ArrayList<>();
    for (RowExpression argument : arguments) {
        argumentsBytecode.add(context.generate(argument));
    }
    return context.generateCall(signature.getName(), function, argumentsBytecode);
}
Also used : ScalarFunctionImplementation(com.facebook.presto.operator.scalar.ScalarFunctionImplementation) FunctionRegistry(com.facebook.presto.metadata.FunctionRegistry) ArrayList(java.util.ArrayList) RowExpression(com.facebook.presto.sql.relational.RowExpression) BytecodeNode(com.facebook.presto.bytecode.BytecodeNode)

Example 7 with FunctionRegistry

use of com.facebook.presto.metadata.FunctionRegistry in project presto by prestodb.

the class AbstractGreatestLeast method specialize.

@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) {
    Type type = boundVariables.getTypeVariable("E");
    checkArgument(type.isOrderable(), "Type must be orderable");
    MethodHandle compareMethod = functionRegistry.getScalarFunctionImplementation(internalOperator(operatorType, BOOLEAN, ImmutableList.of(type, type))).getMethodHandle();
    List<Class<?>> javaTypes = IntStream.range(0, arity).mapToObj(i -> type.getJavaType()).collect(toImmutableList());
    Class<?> clazz = generate(javaTypes, type, compareMethod);
    MethodHandle methodHandle = methodHandle(clazz, getSignature().getName(), javaTypes.toArray(new Class<?>[javaTypes.size()]));
    List<Boolean> nullableParameters = ImmutableList.copyOf(Collections.nCopies(javaTypes.size(), false));
    return new ScalarFunctionImplementation(false, nullableParameters, methodHandle, isDeterministic());
}
Also used : IntStream(java.util.stream.IntStream) MethodHandle(java.lang.invoke.MethodHandle) TypeManager(com.facebook.presto.spi.type.TypeManager) PRIVATE(com.facebook.presto.bytecode.Access.PRIVATE) PrestoException(com.facebook.presto.spi.PrestoException) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) BytecodeUtils.invoke(com.facebook.presto.sql.gen.BytecodeUtils.invoke) FunctionKind(com.facebook.presto.metadata.FunctionKind) Access.a(com.facebook.presto.bytecode.Access.a) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) BOOLEAN(com.facebook.presto.spi.type.BooleanType.BOOLEAN) ParameterizedType.type(com.facebook.presto.bytecode.ParameterizedType.type) Reflection.methodHandle(com.facebook.presto.util.Reflection.methodHandle) Type(com.facebook.presto.spi.type.Type) Objects.requireNonNull(java.util.Objects.requireNonNull) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) INVALID_FUNCTION_ARGUMENT(com.facebook.presto.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT) StandardTypes(com.facebook.presto.spi.type.StandardTypes) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode) IfStatement(com.facebook.presto.bytecode.control.IfStatement) PUBLIC(com.facebook.presto.bytecode.Access.PUBLIC) Variable(com.facebook.presto.bytecode.Variable) BoundVariables(com.facebook.presto.metadata.BoundVariables) Signature.orderableTypeParameter(com.facebook.presto.metadata.Signature.orderableTypeParameter) Parameter(com.facebook.presto.bytecode.Parameter) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) FunctionRegistry(com.facebook.presto.metadata.FunctionRegistry) Signature(com.facebook.presto.metadata.Signature) Signature.internalOperator(com.facebook.presto.metadata.Signature.internalOperator) DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) List(java.util.List) CompilerUtils(com.facebook.presto.bytecode.CompilerUtils) FINAL(com.facebook.presto.bytecode.Access.FINAL) STATIC(com.facebook.presto.bytecode.Access.STATIC) Scope(com.facebook.presto.bytecode.Scope) OperatorType(com.facebook.presto.spi.function.OperatorType) CompilerUtils.defineClass(com.facebook.presto.bytecode.CompilerUtils.defineClass) CallSiteBinder(com.facebook.presto.sql.gen.CallSiteBinder) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) Collections(java.util.Collections) Parameter.arg(com.facebook.presto.bytecode.Parameter.arg) Type(com.facebook.presto.spi.type.Type) OperatorType(com.facebook.presto.spi.function.OperatorType) CompilerUtils.defineClass(com.facebook.presto.bytecode.CompilerUtils.defineClass) MethodHandle(java.lang.invoke.MethodHandle)

Example 8 with FunctionRegistry

use of com.facebook.presto.metadata.FunctionRegistry in project presto by prestodb.

the class BindableAggregationFunction method specialize.

@Override
public InternalAggregationFunction specialize(BoundVariables variables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) {
    // bind variables
    Signature boundSignature = applyBoundVariables(getSignature(), variables, arity);
    List<Type> inputTypes = boundSignature.getArgumentTypes().stream().map(x -> typeManager.getType(x)).collect(toImmutableList());
    Type outputType = typeManager.getType(boundSignature.getReturnType());
    AggregationFunction aggregationAnnotation = definitionClass.getAnnotation(AggregationFunction.class);
    requireNonNull(aggregationAnnotation, "aggregationAnnotation is null");
    DynamicClassLoader classLoader = new DynamicClassLoader(definitionClass.getClassLoader(), getClass().getClassLoader());
    AggregationMetadata metadata;
    AccumulatorStateSerializer<?> stateSerializer = StateCompiler.generateStateSerializer(stateClass, classLoader);
    Type intermediateType = stateSerializer.getSerializedType();
    Method combineFunction = AggregationCompiler.getCombineFunction(definitionClass, stateClass);
    AccumulatorStateFactory<?> stateFactory = StateCompiler.generateStateFactory(stateClass, classLoader);
    try {
        MethodHandle inputHandle = lookup().unreflect(inputFunction);
        MethodHandle combineHandle = lookup().unreflect(combineFunction);
        MethodHandle outputHandle = outputFunction == null ? null : lookup().unreflect(outputFunction);
        metadata = new AggregationMetadata(generateAggregationName(getSignature().getName(), outputType.getTypeSignature(), signaturesFromTypes(inputTypes)), getParameterMetadata(inputFunction, inputTypes), inputHandle, combineHandle, outputHandle, stateClass, stateSerializer, stateFactory, outputType);
    } catch (IllegalAccessException e) {
        throw Throwables.propagate(e);
    }
    AccumulatorFactoryBinder factory = new LazyAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(getSignature().getName(), inputTypes, intermediateType, outputType, decomposable, factory);
}
Also used : TypeSignature(com.facebook.presto.spi.type.TypeSignature) MethodHandle(java.lang.invoke.MethodHandle) Arrays(java.util.Arrays) AggregationCompiler.isParameterBlock(com.facebook.presto.operator.aggregation.AggregationCompiler.isParameterBlock) TypeManager(com.facebook.presto.spi.type.TypeManager) AggregationUtils.generateAggregationName(com.facebook.presto.operator.aggregation.AggregationUtils.generateAggregationName) MethodHandles.lookup(java.lang.invoke.MethodHandles.lookup) ParameterMetadata(com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) AggregationState(com.facebook.presto.spi.function.AggregationState) AggregationFunction(com.facebook.presto.spi.function.AggregationFunction) Type(com.facebook.presto.spi.type.Type) Objects.requireNonNull(java.util.Objects.requireNonNull) SqlAggregationFunction(com.facebook.presto.metadata.SqlAggregationFunction) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) Method(java.lang.reflect.Method) Nullable(javax.annotation.Nullable) ParameterMetadata.fromSqlType(com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.fromSqlType) AggregationCompiler.isParameterNullable(com.facebook.presto.operator.aggregation.AggregationCompiler.isParameterNullable) SignatureBinder.applyBoundVariables(com.facebook.presto.metadata.SignatureBinder.applyBoundVariables) BoundVariables(com.facebook.presto.metadata.BoundVariables) FunctionRegistry(com.facebook.presto.metadata.FunctionRegistry) Signature(com.facebook.presto.metadata.Signature) Throwables(com.google.common.base.Throwables) DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) BLOCK_INDEX(com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.ParameterType.BLOCK_INDEX) List(java.util.List) AccumulatorStateSerializer(com.facebook.presto.spi.function.AccumulatorStateSerializer) Annotation(java.lang.annotation.Annotation) AccumulatorStateFactory(com.facebook.presto.spi.function.AccumulatorStateFactory) StateCompiler(com.facebook.presto.operator.aggregation.state.StateCompiler) SqlType(com.facebook.presto.spi.function.SqlType) STATE(com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.ParameterType.STATE) DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) Method(java.lang.reflect.Method) AggregationFunction(com.facebook.presto.spi.function.AggregationFunction) SqlAggregationFunction(com.facebook.presto.metadata.SqlAggregationFunction) Type(com.facebook.presto.spi.type.Type) ParameterMetadata.fromSqlType(com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.fromSqlType) SqlType(com.facebook.presto.spi.function.SqlType) TypeSignature(com.facebook.presto.spi.type.TypeSignature) Signature(com.facebook.presto.metadata.Signature) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

FunctionRegistry (com.facebook.presto.metadata.FunctionRegistry)8 Signature (com.facebook.presto.metadata.Signature)6 TypeSignature.parseTypeSignature (com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)5 BlockEncodingManager (com.facebook.presto.block.BlockEncodingManager)4 Type (com.facebook.presto.spi.type.Type)4 FeaturesConfig (com.facebook.presto.sql.analyzer.FeaturesConfig)4 RowExpression (com.facebook.presto.sql.relational.RowExpression)4 Test (org.testng.annotations.Test)4 BoundVariables (com.facebook.presto.metadata.BoundVariables)3 TypeManager (com.facebook.presto.spi.type.TypeManager)3 CallExpression (com.facebook.presto.sql.relational.CallExpression)3 ConstantExpression (com.facebook.presto.sql.relational.ConstantExpression)3 ExpressionOptimizer (com.facebook.presto.sql.relational.optimizer.ExpressionOptimizer)3 TypeRegistry (com.facebook.presto.type.TypeRegistry)3 ImmutableCollectors.toImmutableList (com.facebook.presto.util.ImmutableCollectors.toImmutableList)3 ImmutableList (com.google.common.collect.ImmutableList)3 MethodHandle (java.lang.invoke.MethodHandle)3 List (java.util.List)3 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)2 DynamicClassLoader (com.facebook.presto.bytecode.DynamicClassLoader)2