Search in sources :

Example 1 with LambdaDefinitionExpression

use of io.prestosql.spi.relation.LambdaDefinitionExpression in project hetu-core by openlookeng.

the class PageFunctionCompiler method generateFilterMethod.

private MethodDefinition generateFilterMethod(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, CachedInstanceBinder cachedInstanceBinder, Map<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap, RowExpression filter) {
    Parameter session = arg("session", ConnectorSession.class);
    Parameter page = arg("page", Page.class);
    Parameter position = arg("position", int.class);
    MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), "filter", type(boolean.class), ImmutableList.<Parameter>builder().add(session).add(page).add(position).build());
    method.comment("Filter: %s", filter.toString());
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    declareBlockVariables(filter, page, scope, body);
    Variable wasNullVariable = scope.declareVariable("wasNull", body, constantFalse());
    RowExpressionCompiler compiler = new RowExpressionCompiler(callSiteBinder, cachedInstanceBinder, fieldReferenceCompiler(callSiteBinder), metadata, compiledLambdaMap, new ClassContext(classDefinition, scope, (newFilterName, rowExpressionCompiler, generator, subFilter, callerScope) -> {
        MethodDefinition newMethod = classDefinition.declareMethod(a(PUBLIC), newFilterName, type(boolean.class), ImmutableList.<Parameter>builder().add(session).add(page).add(position).build());
        newMethod.comment("Filter: %s", subFilter.toString());
        Scope innerScope = newMethod.getScope();
        BytecodeBlock innerBody = newMethod.getBody();
        declareBlockVariables(subFilter, page, innerScope, innerBody);
        Variable wasNullVariable1 = innerScope.declareVariable("wasNull", innerBody, constantFalse());
        Variable result1 = innerScope.declareVariable(boolean.class, "result");
        BytecodeGeneratorContext generatorContext = new BytecodeGeneratorContext(rowExpressionCompiler, innerScope, callSiteBinder, cachedInstanceBinder, metadata.getFunctionAndTypeManager());
        innerBody.append(generator.generateExpression(null, generatorContext, subFilter.getType(), subFilter.getArguments())).putVariable(result1).append(and(not(wasNullVariable1), result1).ret());
        /* Call the sub-method: Important use caller scope to pass parameters to new function */
        BytecodeBlock block = new BytecodeBlock().comment("INVOKE").setDescription("INVOKE " + newFilterName);
        block.append(callerScope.getThis()).getVariable(session).getVariable(page).getVariable(position).invokeVirtual(classDefinition.getType(), newFilterName, type(boolean.class), type(ConnectorSession.class), type(Page.class), type(int.class));
        return block;
    }));
    Variable result = scope.declareVariable(boolean.class, "result");
    body.append(compiler.compile(filter, scope)).putVariable(result).append(and(not(wasNullVariable), result).ret());
    return method;
}
Also used : PageFilter(io.prestosql.operator.project.PageFilter) BytecodeExpressions.newArray(io.airlift.bytecode.expression.BytecodeExpressions.newArray) LoadingCache(com.google.common.cache.LoadingCache) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) BytecodeUtils.invoke(io.prestosql.sql.gen.BytecodeUtils.invoke) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) BytecodeNode(io.airlift.bytecode.BytecodeNode) Scope(io.airlift.bytecode.Scope) CompilerUtils.makeClassName(io.prestosql.util.CompilerUtils.makeClassName) Access.a(io.airlift.bytecode.Access.a) BytecodeExpressions.constantFalse(io.airlift.bytecode.expression.BytecodeExpressions.constantFalse) Parameter.arg(io.airlift.bytecode.Parameter.arg) BytecodeExpressions.add(io.airlift.bytecode.expression.BytecodeExpressions.add) ConnectorSession(io.prestosql.spi.connector.ConnectorSession) Map(java.util.Map) PageProjection(io.prestosql.operator.project.PageProjection) BytecodeExpressions.constantNull(io.airlift.bytecode.expression.BytecodeExpressions.constantNull) MethodDefinition(io.airlift.bytecode.MethodDefinition) PrestoException(io.prestosql.spi.PrestoException) ImmutableSet(com.google.common.collect.ImmutableSet) RowExpressionDeterminismEvaluator(io.prestosql.sql.relational.RowExpressionDeterminismEvaluator) InputPageProjection(io.prestosql.operator.project.InputPageProjection) ImmutableMap(com.google.common.collect.ImmutableMap) ConstantPageProjection(io.prestosql.operator.project.ConstantPageProjection) BlockBuilder(io.prestosql.spi.block.BlockBuilder) CompiledLambda(io.prestosql.sql.gen.LambdaBytecodeGenerator.CompiledLambda) Set(java.util.Set) Metadata(io.prestosql.metadata.Metadata) CacheLoader(com.google.common.cache.CacheLoader) BytecodeExpressions.constantBoolean(io.airlift.bytecode.expression.BytecodeExpressions.constantBoolean) List(java.util.List) PRIVATE(io.airlift.bytecode.Access.PRIVATE) LambdaDefinitionExpression(io.prestosql.spi.relation.LambdaDefinitionExpression) SelectedPositions(io.prestosql.operator.project.SelectedPositions) Optional(java.util.Optional) CacheBuilder(com.google.common.cache.CacheBuilder) LambdaExpressionExtractor.extractLambdaExpressions(io.prestosql.sql.gen.LambdaExpressionExtractor.extractLambdaExpressions) Work(io.prestosql.operator.Work) Reflection.constructorMethodHandle(io.prestosql.spi.util.Reflection.constructorMethodHandle) ClassDefinition(io.airlift.bytecode.ClassDefinition) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) ParameterizedType.type(io.airlift.bytecode.ParameterizedType.type) Variable(io.airlift.bytecode.Variable) Nested(org.weakref.jmx.Nested) ParameterizedType(io.airlift.bytecode.ParameterizedType) BytecodeExpressions.not(io.airlift.bytecode.expression.BytecodeExpressions.not) RowExpressionVisitor(io.prestosql.spi.relation.RowExpressionVisitor) BytecodeExpressions.constantInt(io.airlift.bytecode.expression.BytecodeExpressions.constantInt) InputChannels(io.prestosql.operator.project.InputChannels) GeneratedPageProjection(io.prestosql.operator.project.GeneratedPageProjection) Supplier(java.util.function.Supplier) Parameter(io.airlift.bytecode.Parameter) TreeSet(java.util.TreeSet) Inject(javax.inject.Inject) ImmutableList(com.google.common.collect.ImmutableList) Managed(org.weakref.jmx.Managed) CompilerUtils.defineClass(io.prestosql.util.CompilerUtils.defineClass) Objects.requireNonNull(java.util.Objects.requireNonNull) BytecodeExpressions.and(io.airlift.bytecode.expression.BytecodeExpressions.and) BytecodeExpressions.lessThan(io.airlift.bytecode.expression.BytecodeExpressions.lessThan) Block(io.prestosql.spi.block.Block) Nullable(javax.annotation.Nullable) FINAL(io.airlift.bytecode.Access.FINAL) BytecodeExpressions.invokeStatic(io.airlift.bytecode.expression.BytecodeExpressions.invokeStatic) BytecodeUtils.generateWrite(io.prestosql.sql.gen.BytecodeUtils.generateWrite) ForLoop(io.airlift.bytecode.control.ForLoop) CompilerConfig(io.prestosql.sql.planner.CompilerConfig) Page(io.prestosql.spi.Page) Expressions(io.prestosql.sql.relational.Expressions) PageFieldsToInputParametersRewriter.rewritePageFieldsToInputParameters(io.prestosql.operator.project.PageFieldsToInputParametersRewriter.rewritePageFieldsToInputParameters) IfStatement(io.airlift.bytecode.control.IfStatement) Consumer(java.util.function.Consumer) PUBLIC(io.airlift.bytecode.Access.PUBLIC) InputReferenceExpression(io.prestosql.spi.relation.InputReferenceExpression) RowExpression(io.prestosql.spi.relation.RowExpression) COMPILER_ERROR(io.prestosql.spi.StandardErrorCode.COMPILER_ERROR) FieldDefinition(io.airlift.bytecode.FieldDefinition) PageFieldsToInputParametersRewriter(io.prestosql.operator.project.PageFieldsToInputParametersRewriter) Variable(io.airlift.bytecode.Variable) Scope(io.airlift.bytecode.Scope) MethodDefinition(io.airlift.bytecode.MethodDefinition) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) Parameter(io.airlift.bytecode.Parameter)

Example 2 with LambdaDefinitionExpression

use of io.prestosql.spi.relation.LambdaDefinitionExpression in project hetu-core by openlookeng.

the class JoinFilterFunctionCompiler method generateMethodsForLambda.

private Map<LambdaDefinitionExpression, CompiledLambda> generateMethodsForLambda(ClassDefinition containerClassDefinition, CallSiteBinder callSiteBinder, CachedInstanceBinder cachedInstanceBinder, int leftBlocksSize, RowExpression filter) {
    Set<LambdaDefinitionExpression> lambdaExpressions = ImmutableSet.copyOf(extractLambdaExpressions(filter));
    ImmutableMap.Builder<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap = ImmutableMap.builder();
    int counter = 0;
    for (LambdaDefinitionExpression lambdaExpression : lambdaExpressions) {
        CompiledLambda compiledLambda = LambdaBytecodeGenerator.preGenerateLambdaExpression(lambdaExpression, "lambda_" + counter, containerClassDefinition, compiledLambdaMap.build(), callSiteBinder, cachedInstanceBinder, metadata);
        compiledLambdaMap.put(lambdaExpression, compiledLambda);
        counter++;
    }
    return compiledLambdaMap.build();
}
Also used : CompiledLambda(io.prestosql.sql.gen.LambdaBytecodeGenerator.CompiledLambda) ImmutableMap(com.google.common.collect.ImmutableMap) LambdaDefinitionExpression(io.prestosql.spi.relation.LambdaDefinitionExpression)

Example 3 with LambdaDefinitionExpression

use of io.prestosql.spi.relation.LambdaDefinitionExpression in project hetu-core by openlookeng.

the class LambdaBytecodeGenerator method compileLambdaProvider.

public static Class<? extends LambdaProvider> compileLambdaProvider(LambdaDefinitionExpression lambdaExpression, Metadata metadata, Class<?> lambdaInterface) {
    ClassDefinition lambdaProviderClassDefinition = new ClassDefinition(a(PUBLIC, Access.FINAL), makeClassName("LambdaProvider"), type(Object.class), type(LambdaProvider.class));
    FieldDefinition sessionField = lambdaProviderClassDefinition.declareField(a(PRIVATE), "session", ConnectorSession.class);
    CallSiteBinder callSiteBinder = new CallSiteBinder();
    CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(lambdaProviderClassDefinition, callSiteBinder);
    Map<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap = generateMethodsForLambda(lambdaProviderClassDefinition, callSiteBinder, cachedInstanceBinder, lambdaExpression, metadata);
    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("session", body, method.getThis().getField(sessionField));
    RowExpressionCompiler rowExpressionCompiler = new RowExpressionCompiler(callSiteBinder, cachedInstanceBinder, variableReferenceCompiler(ImmutableMap.of()), metadata, compiledLambdaMap);
    BytecodeGeneratorContext generatorContext = new BytecodeGeneratorContext(rowExpressionCompiler, scope, callSiteBinder, cachedInstanceBinder, metadata.getFunctionAndTypeManager());
    body.append(generateLambda(generatorContext, ImmutableList.of(), compiledLambdaMap.get(lambdaExpression), lambdaInterface)).retObject();
    // constructor
    Parameter sessionParameter = arg("session", ConnectorSession.class);
    MethodDefinition constructorDefinition = lambdaProviderClassDefinition.declareConstructor(a(PUBLIC), sessionParameter);
    BytecodeBlock constructorBody = constructorDefinition.getBody();
    Variable constructorThisVariable = constructorDefinition.getThis();
    constructorBody.comment("super();").append(constructorThisVariable).invokeConstructor(Object.class).append(constructorThisVariable.setField(sessionField, sessionParameter));
    cachedInstanceBinder.generateInitializations(constructorThisVariable, constructorBody);
    constructorBody.ret();
    return defineClass(lambdaProviderClassDefinition, LambdaProvider.class, callSiteBinder.getBindings(), AccumulatorCompiler.class.getClassLoader());
}
Also used : Variable(io.airlift.bytecode.Variable) FieldDefinition(io.airlift.bytecode.FieldDefinition) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) ClassDefinition(io.airlift.bytecode.ClassDefinition) LambdaProvider(io.prestosql.operator.aggregation.LambdaProvider) Scope(io.airlift.bytecode.Scope) MethodDefinition(io.airlift.bytecode.MethodDefinition) Parameter(io.airlift.bytecode.Parameter) AccumulatorCompiler(io.prestosql.operator.aggregation.AccumulatorCompiler) LambdaDefinitionExpression(io.prestosql.spi.relation.LambdaDefinitionExpression)

Example 4 with LambdaDefinitionExpression

use of io.prestosql.spi.relation.LambdaDefinitionExpression in project hetu-core by openlookeng.

the class CursorProcessorCompiler method generateMethods.

@Override
public void generateMethods(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, RowExpression filter, List<RowExpression> projections) {
    CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(classDefinition, callSiteBinder);
    generateProcessMethod(classDefinition, projections.size());
    Map<LambdaDefinitionExpression, CompiledLambda> filterCompiledLambdaMap = generateMethodsForLambda(classDefinition, callSiteBinder, cachedInstanceBinder, filter, "filter");
    generateFilterMethod(classDefinition, callSiteBinder, cachedInstanceBinder, filterCompiledLambdaMap, filter);
    for (int i = 0; i < projections.size(); i++) {
        String methodName = "project_" + i;
        Map<LambdaDefinitionExpression, CompiledLambda> projectCompiledLambdaMap = generateMethodsForLambda(classDefinition, callSiteBinder, cachedInstanceBinder, projections.get(i), methodName);
        generateProjectMethod(classDefinition, callSiteBinder, cachedInstanceBinder, projectCompiledLambdaMap, methodName, projections.get(i));
    }
    MethodDefinition constructorDefinition = classDefinition.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();
}
Also used : CompiledLambda(io.prestosql.sql.gen.LambdaBytecodeGenerator.CompiledLambda) Variable(io.airlift.bytecode.Variable) MethodDefinition(io.airlift.bytecode.MethodDefinition) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) LambdaDefinitionExpression(io.prestosql.spi.relation.LambdaDefinitionExpression)

Example 5 with LambdaDefinitionExpression

use of io.prestosql.spi.relation.LambdaDefinitionExpression in project hetu-core by openlookeng.

the class CursorProcessorCompiler method fieldReferenceCompiler.

private static RowExpressionVisitor<BytecodeNode, Scope> fieldReferenceCompiler(Variable cursorVariable) {
    return new RowExpressionVisitor<BytecodeNode, Scope>() {

        @Override
        public BytecodeNode visitInputReference(InputReferenceExpression node, Scope scope) {
            int field = node.getField();
            Type type = node.getType();
            Variable wasNullVariable = scope.getVariable("wasNull");
            Class<?> javaType = type.getJavaType();
            if (!javaType.isPrimitive() && javaType != Slice.class) {
                javaType = Object.class;
            }
            IfStatement ifStatement = new IfStatement();
            ifStatement.condition().setDescription(String.format(Locale.ROOT, "cursor.get%s(%d)", type, field)).getVariable(cursorVariable).push(field).invokeInterface(RecordCursor.class, "isNull", boolean.class, int.class);
            ifStatement.ifTrue().putVariable(wasNullVariable, true).pushJavaDefault(javaType);
            ifStatement.ifFalse().getVariable(cursorVariable).push(field).invokeInterface(RecordCursor.class, "get" + Primitives.wrap(javaType).getSimpleName(), javaType, int.class);
            return ifStatement;
        }

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

        @Override
        public BytecodeNode visitSpecialForm(SpecialForm specialForm, Scope context) {
            throw new UnsupportedOperationException("not yet implemented");
        }

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

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

        @Override
        public BytecodeNode visitVariableReference(VariableReferenceExpression reference, Scope context) {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : InputReferenceExpression(io.prestosql.spi.relation.InputReferenceExpression) Variable(io.airlift.bytecode.Variable) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) IfStatement(io.airlift.bytecode.control.IfStatement) Type(io.prestosql.spi.type.Type) Scope(io.airlift.bytecode.Scope) Slice(io.airlift.slice.Slice) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) RowExpressionVisitor(io.prestosql.spi.relation.RowExpressionVisitor) CallExpression(io.prestosql.spi.relation.CallExpression) SpecialForm(io.prestosql.spi.relation.SpecialForm) LambdaDefinitionExpression(io.prestosql.spi.relation.LambdaDefinitionExpression)

Aggregations

LambdaDefinitionExpression (io.prestosql.spi.relation.LambdaDefinitionExpression)14 CompiledLambda (io.prestosql.sql.gen.LambdaBytecodeGenerator.CompiledLambda)10 Variable (io.airlift.bytecode.Variable)7 ImmutableMap (com.google.common.collect.ImmutableMap)6 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)6 ClassDefinition (io.airlift.bytecode.ClassDefinition)5 FieldDefinition (io.airlift.bytecode.FieldDefinition)5 MethodDefinition (io.airlift.bytecode.MethodDefinition)5 Parameter (io.airlift.bytecode.Parameter)5 Scope (io.airlift.bytecode.Scope)5 ConstantExpression (io.prestosql.spi.relation.ConstantExpression)4 InputReferenceExpression (io.prestosql.spi.relation.InputReferenceExpression)4 RowExpressionVisitor (io.prestosql.spi.relation.RowExpressionVisitor)4 IfStatement (io.airlift.bytecode.control.IfStatement)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 PUBLIC (io.airlift.bytecode.Access.PUBLIC)2 Access.a (io.airlift.bytecode.Access.a)2 BytecodeNode (io.airlift.bytecode.BytecodeNode)2 Parameter.arg (io.airlift.bytecode.Parameter.arg)2