Search in sources :

Example 11 with SqlFunctionProperties

use of com.facebook.presto.common.function.SqlFunctionProperties in project presto by prestodb.

the class LambdaBytecodeGenerator method generateMethodsForLambda.

private static Map<LambdaDefinitionExpression, CompiledLambda> generateMethodsForLambda(ClassDefinition containerClassDefinition, CallSiteBinder callSiteBinder, CachedInstanceBinder cachedInstanceBinder, List<RowExpression> expressions, Metadata metadata, SqlFunctionProperties sqlFunctionProperties, Map<SqlFunctionId, SqlInvokedFunction> sessionFunctions, String methodNamePrefix, Set<LambdaDefinitionExpression> existingCompiledLambdas) {
    Set<LambdaDefinitionExpression> lambdaExpressions = expressions.stream().map(LambdaExpressionExtractor::extractLambdaExpressions).flatMap(List::stream).filter(lambda -> !existingCompiledLambdas.contains(lambda)).collect(toImmutableSet());
    ImmutableMap.Builder<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap = ImmutableMap.builder();
    int counter = existingCompiledLambdas.size();
    for (LambdaDefinitionExpression lambdaExpression : lambdaExpressions) {
        CompiledLambda compiledLambda = LambdaBytecodeGenerator.preGenerateLambdaExpression(lambdaExpression, methodNamePrefix + "lambda_" + counter, containerClassDefinition, compiledLambdaMap.build(), callSiteBinder, cachedInstanceBinder, metadata, sqlFunctionProperties, sessionFunctions);
        compiledLambdaMap.put(lambdaExpression, compiledLambda);
        counter++;
    }
    return compiledLambdaMap.build();
}
Also used : BytecodeExpression(com.facebook.presto.bytecode.expression.BytecodeExpression) Arrays(java.util.Arrays) LambdaProvider(com.facebook.presto.operator.aggregation.LambdaProvider) RowExpressionVisitor(com.facebook.presto.spi.relation.RowExpressionVisitor) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) BytecodeExpressions.invokeDynamic(com.facebook.presto.bytecode.expression.BytecodeExpressions.invokeDynamic) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) Type(org.objectweb.asm.Type) Access.a(com.facebook.presto.bytecode.Access.a) CallSiteBinder(com.facebook.presto.bytecode.CallSiteBinder) ParameterizedType.type(com.facebook.presto.bytecode.ParameterizedType.type) BytecodeUtils.boxPrimitiveIfNecessary(com.facebook.presto.sql.gen.BytecodeUtils.boxPrimitiveIfNecessary) Map(java.util.Map) CompilerUtils.makeClassName(com.facebook.presto.util.CompilerUtils.makeClassName) CallExpression(com.facebook.presto.spi.relation.CallExpression) Method(java.lang.reflect.Method) SpecialFormExpression(com.facebook.presto.spi.relation.SpecialFormExpression) Variable(com.facebook.presto.bytecode.Variable) ImmutableSet(com.google.common.collect.ImmutableSet) Parameter(com.facebook.presto.bytecode.Parameter) ImmutableMap(com.google.common.collect.ImmutableMap) Type.getType(org.objectweb.asm.Type.getType) SqlFunctionProperties(com.facebook.presto.common.function.SqlFunctionProperties) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) LambdaDefinitionExpression(com.facebook.presto.spi.relation.LambdaDefinitionExpression) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) List(java.util.List) Scope(com.facebook.presto.bytecode.Scope) NOT_SUPPORTED(com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED) Type.getMethodType(org.objectweb.asm.Type.getMethodType) Optional(java.util.Optional) AccumulatorCompiler(com.facebook.presto.operator.aggregation.AccumulatorCompiler) InputReferenceExpression(com.facebook.presto.spi.relation.InputReferenceExpression) Parameter.arg(com.facebook.presto.bytecode.Parameter.arg) PRIVATE(com.facebook.presto.bytecode.Access.PRIVATE) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) BytecodeExpressions.constantFalse(com.facebook.presto.bytecode.expression.BytecodeExpressions.constantFalse) LAMBDA_CAPTURE_METHOD(com.facebook.presto.sql.gen.LambdaCapture.LAMBDA_CAPTURE_METHOD) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) Failures.checkCondition(com.facebook.presto.util.Failures.checkCondition) RowExpression(com.facebook.presto.spi.relation.RowExpression) VerifyException(com.google.common.base.VerifyException) PUBLIC(com.facebook.presto.bytecode.Access.PUBLIC) Opcodes(org.objectweb.asm.Opcodes) Access(com.facebook.presto.bytecode.Access) CompilerUtils.defineClass(com.facebook.presto.util.CompilerUtils.defineClass) Primitives(com.google.common.primitives.Primitives) Handle(org.objectweb.asm.Handle) BytecodeNode(com.facebook.presto.bytecode.BytecodeNode) SqlFunctionId(com.facebook.presto.spi.function.SqlFunctionId) FieldDefinition(com.facebook.presto.bytecode.FieldDefinition) BytecodeUtils.unboxPrimitiveIfNecessary(com.facebook.presto.sql.gen.BytecodeUtils.unboxPrimitiveIfNecessary) COMPILER_ERROR(com.facebook.presto.spi.StandardErrorCode.COMPILER_ERROR) ParameterizedType(com.facebook.presto.bytecode.ParameterizedType) Metadata(com.facebook.presto.metadata.Metadata) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ImmutableMap(com.google.common.collect.ImmutableMap) LambdaDefinitionExpression(com.facebook.presto.spi.relation.LambdaDefinitionExpression)

Example 12 with SqlFunctionProperties

use of com.facebook.presto.common.function.SqlFunctionProperties in project presto by prestodb.

the class LambdaBytecodeGenerator method preGenerateLambdaExpression.

/**
 * @return a MethodHandle field that represents the lambda expression
 */
private static CompiledLambda preGenerateLambdaExpression(LambdaDefinitionExpression lambdaExpression, String methodName, ClassDefinition classDefinition, Map<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap, CallSiteBinder callSiteBinder, CachedInstanceBinder cachedInstanceBinder, Metadata metadata, SqlFunctionProperties sqlFunctionProperties, Map<SqlFunctionId, SqlInvokedFunction> sessionFunctions) {
    ImmutableList.Builder<Parameter> parameters = ImmutableList.builder();
    ImmutableMap.Builder<String, ParameterAndType> parameterMapBuilder = ImmutableMap.builder();
    parameters.add(arg("properties", SqlFunctionProperties.class));
    for (int i = 0; i < lambdaExpression.getArguments().size(); i++) {
        Class<?> type = Primitives.wrap(lambdaExpression.getArgumentTypes().get(i).getJavaType());
        String argumentName = lambdaExpression.getArguments().get(i);
        Parameter arg = arg("lambda_" + argumentName, type);
        parameters.add(arg);
        parameterMapBuilder.put(argumentName, new ParameterAndType(arg, type));
    }
    RowExpressionCompiler innerExpressionCompiler = new RowExpressionCompiler(classDefinition, callSiteBinder, cachedInstanceBinder, variableReferenceCompiler(parameterMapBuilder.build()), metadata, sqlFunctionProperties, sessionFunctions, compiledLambdaMap);
    return defineLambdaMethod(innerExpressionCompiler, classDefinition, methodName, parameters.build(), lambdaExpression);
}
Also used : ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) SqlFunctionProperties(com.facebook.presto.common.function.SqlFunctionProperties) Parameter(com.facebook.presto.bytecode.Parameter) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

SqlFunctionProperties (com.facebook.presto.common.function.SqlFunctionProperties)12 CallSiteBinder (com.facebook.presto.bytecode.CallSiteBinder)6 ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)6 Parameter (com.facebook.presto.bytecode.Parameter)6 PrestoException (com.facebook.presto.spi.PrestoException)6 CallExpression (com.facebook.presto.spi.relation.CallExpression)6 RowExpression (com.facebook.presto.spi.relation.RowExpression)6 SpecialFormExpression (com.facebook.presto.spi.relation.SpecialFormExpression)6 ImmutableList (com.google.common.collect.ImmutableList)6 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)6 PRIVATE (com.facebook.presto.bytecode.Access.PRIVATE)5 PUBLIC (com.facebook.presto.bytecode.Access.PUBLIC)5 Access.a (com.facebook.presto.bytecode.Access.a)5 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)5 BytecodeNode (com.facebook.presto.bytecode.BytecodeNode)5 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)5 Parameter.arg (com.facebook.presto.bytecode.Parameter.arg)5 ParameterizedType.type (com.facebook.presto.bytecode.ParameterizedType.type)5 Scope (com.facebook.presto.bytecode.Scope)5 Variable (com.facebook.presto.bytecode.Variable)5