Search in sources :

Example 11 with LambdaDefinitionExpression

use of com.facebook.presto.spi.relation.LambdaDefinitionExpression in project presto by prestodb.

the class RowExpressionPredicateCompiler method generatePredicateMethod.

private MethodDefinition generatePredicateMethod(SqlFunctionProperties sqlFunctionProperties, Map<SqlFunctionId, SqlInvokedFunction> sessionFunctions, ClassDefinition classDefinition, CallSiteBinder callSiteBinder, CachedInstanceBinder cachedInstanceBinder, RowExpression predicate) {
    Map<LambdaDefinitionExpression, LambdaBytecodeGenerator.CompiledLambda> compiledLambdaMap = generateMethodsForLambda(classDefinition, callSiteBinder, cachedInstanceBinder, predicate, metadata, sqlFunctionProperties, sessionFunctions);
    Parameter properties = arg("properties", SqlFunctionProperties.class);
    Parameter page = arg("page", Page.class);
    Parameter position = arg("position", int.class);
    MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), "evaluate", type(boolean.class), ImmutableList.<Parameter>builder().add(properties).add(page).add(position).build());
    method.comment("Predicate: %s", predicate.toString());
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    declareBlockVariables(predicate, page, scope, body);
    Variable wasNullVariable = scope.declareVariable("wasNull", body, constantFalse());
    RowExpressionCompiler compiler = new RowExpressionCompiler(classDefinition, callSiteBinder, cachedInstanceBinder, fieldReferenceCompiler(callSiteBinder), metadata, sqlFunctionProperties, sessionFunctions, compiledLambdaMap);
    Variable result = scope.declareVariable(boolean.class, "result");
    body.append(compiler.compile(predicate, scope, Optional.empty())).putVariable(result).append(and(not(wasNullVariable), result).ret());
    return method;
}
Also used : Variable(com.facebook.presto.bytecode.Variable) Scope(com.facebook.presto.bytecode.Scope) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) Parameter(com.facebook.presto.bytecode.Parameter) LambdaDefinitionExpression(com.facebook.presto.spi.relation.LambdaDefinitionExpression)

Example 12 with LambdaDefinitionExpression

use of com.facebook.presto.spi.relation.LambdaDefinitionExpression 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 13 with LambdaDefinitionExpression

use of com.facebook.presto.spi.relation.LambdaDefinitionExpression in project presto by prestodb.

the class LambdaBytecodeGenerator method variableReferenceCompiler.

private static RowExpressionVisitor<BytecodeNode, Scope> variableReferenceCompiler(Map<String, ParameterAndType> parameterMap) {
    return new RowExpressionVisitor<BytecodeNode, Scope>() {

        @Override
        public BytecodeNode visitInputReference(InputReferenceExpression node, Scope scope) {
            throw new UnsupportedOperationException();
        }

        @Override
        public BytecodeNode visitCall(CallExpression call, Scope scope) {
            throw new UnsupportedOperationException();
        }

        @Override
        public BytecodeNode visitConstant(ConstantExpression literal, Scope scope) {
            throw new UnsupportedOperationException();
        }

        @Override
        public BytecodeNode visitLambda(LambdaDefinitionExpression lambda, Scope context) {
            throw new UnsupportedOperationException();
        }

        @Override
        public BytecodeNode visitVariableReference(VariableReferenceExpression reference, Scope context) {
            ParameterAndType parameterAndType = parameterMap.get(reference.getName());
            Parameter parameter = parameterAndType.getParameter();
            Class<?> type = parameterAndType.getType();
            return new BytecodeBlock().append(parameter).append(unboxPrimitiveIfNecessary(context, type));
        }

        @Override
        public BytecodeNode visitSpecialForm(SpecialFormExpression specialForm, Scope context) {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : InputReferenceExpression(com.facebook.presto.spi.relation.InputReferenceExpression) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) Scope(com.facebook.presto.bytecode.Scope) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) RowExpressionVisitor(com.facebook.presto.spi.relation.RowExpressionVisitor) Parameter(com.facebook.presto.bytecode.Parameter) CallExpression(com.facebook.presto.spi.relation.CallExpression) SpecialFormExpression(com.facebook.presto.spi.relation.SpecialFormExpression) LambdaDefinitionExpression(com.facebook.presto.spi.relation.LambdaDefinitionExpression)

Example 14 with LambdaDefinitionExpression

use of com.facebook.presto.spi.relation.LambdaDefinitionExpression in project presto by prestodb.

the class LambdaBytecodeGenerator method compileLambdaProvider.

public static Class<? extends LambdaProvider> compileLambdaProvider(LambdaDefinitionExpression lambdaExpression, Metadata metadata, SqlFunctionProperties sqlFunctionProperties, Map<SqlFunctionId, SqlInvokedFunction> sessionFunctions, Class lambdaInterface) {
    ClassDefinition lambdaProviderClassDefinition = new ClassDefinition(a(PUBLIC, Access.FINAL), makeClassName("LambdaProvider"), type(Object.class), type(LambdaProvider.class));
    FieldDefinition propertiesField = lambdaProviderClassDefinition.declareField(a(PRIVATE), "properties", SqlFunctionProperties.class);
    CallSiteBinder callSiteBinder = new CallSiteBinder();
    CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(lambdaProviderClassDefinition, callSiteBinder);
    Map<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap = generateMethodsForLambda(lambdaProviderClassDefinition, callSiteBinder, cachedInstanceBinder, lambdaExpression, metadata, sqlFunctionProperties, sessionFunctions);
    MethodDefinition method = lambdaProviderClassDefinition.declareMethod(a(PUBLIC), "getLambda", type(Object.class), ImmutableList.of());
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    scope.declareVariable("wasNull", body, constantFalse());
    scope.declareVariable("properties", body, method.getThis().getField(propertiesField));
    RowExpressionCompiler rowExpressionCompiler = new RowExpressionCompiler(lambdaProviderClassDefinition, callSiteBinder, cachedInstanceBinder, variableReferenceCompiler(ImmutableMap.of()), metadata, sqlFunctionProperties, sessionFunctions, compiledLambdaMap);
    BytecodeGeneratorContext generatorContext = new BytecodeGeneratorContext(rowExpressionCompiler, scope, callSiteBinder, cachedInstanceBinder, metadata.getFunctionAndTypeManager());
    body.append(generateLambda(generatorContext, ImmutableList.of(), compiledLambdaMap.get(lambdaExpression), lambdaInterface)).retObject();
    // constructor
    Parameter propertiesParameter = arg("properties", SqlFunctionProperties.class);
    MethodDefinition constructorDefinition = lambdaProviderClassDefinition.declareConstructor(a(PUBLIC), propertiesParameter);
    BytecodeBlock constructorBody = constructorDefinition.getBody();
    Variable constructorThisVariable = constructorDefinition.getThis();
    constructorBody.comment("super();").append(constructorThisVariable).invokeConstructor(Object.class).append(constructorThisVariable.setField(propertiesField, propertiesParameter));
    cachedInstanceBinder.generateInitializations(constructorThisVariable, constructorBody);
    constructorBody.ret();
    return defineClass(lambdaProviderClassDefinition, LambdaProvider.class, callSiteBinder.getBindings(), AccumulatorCompiler.class.getClassLoader());
}
Also used : Variable(com.facebook.presto.bytecode.Variable) FieldDefinition(com.facebook.presto.bytecode.FieldDefinition) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) LambdaProvider(com.facebook.presto.operator.aggregation.LambdaProvider) Scope(com.facebook.presto.bytecode.Scope) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) CallSiteBinder(com.facebook.presto.bytecode.CallSiteBinder) Parameter(com.facebook.presto.bytecode.Parameter) AccumulatorCompiler(com.facebook.presto.operator.aggregation.AccumulatorCompiler) LambdaDefinitionExpression(com.facebook.presto.spi.relation.LambdaDefinitionExpression)

Aggregations

LambdaDefinitionExpression (com.facebook.presto.spi.relation.LambdaDefinitionExpression)14 CallExpression (com.facebook.presto.spi.relation.CallExpression)9 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)8 Scope (com.facebook.presto.bytecode.Scope)8 Parameter (com.facebook.presto.bytecode.Parameter)7 Variable (com.facebook.presto.bytecode.Variable)7 RowExpression (com.facebook.presto.spi.relation.RowExpression)7 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)7 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)6 Metadata (com.facebook.presto.metadata.Metadata)6 ConstantExpression (com.facebook.presto.spi.relation.ConstantExpression)6 ImmutableList (com.google.common.collect.ImmutableList)6 CallSiteBinder (com.facebook.presto.bytecode.CallSiteBinder)5 ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)5 FieldDefinition (com.facebook.presto.bytecode.FieldDefinition)5 Optional (java.util.Optional)5 PRIVATE (com.facebook.presto.bytecode.Access.PRIVATE)4 PUBLIC (com.facebook.presto.bytecode.Access.PUBLIC)4 Access.a (com.facebook.presto.bytecode.Access.a)4 BytecodeNode (com.facebook.presto.bytecode.BytecodeNode)4