Search in sources :

Example 6 with Variable

use of io.airlift.bytecode.Variable in project hetu-core by openlookeng.

the class AccumulatorCompiler method generateGroupedEvaluateIntermediate.

private static void generateGroupedEvaluateIntermediate(ClassDefinition definition, List<StateFieldAndDescriptor> stateFieldAndDescriptors) {
    Parameter groupId = arg("groupId", int.class);
    Parameter out = arg("out", BlockBuilder.class);
    MethodDefinition method = definition.declareMethod(a(PUBLIC), "evaluateIntermediate", type(void.class), groupId, out);
    Variable thisVariable = method.getThis();
    BytecodeBlock body = method.getBody();
    if (stateFieldAndDescriptors.size() == 1) {
        BytecodeExpression stateSerializer = thisVariable.getField(getOnlyElement(stateFieldAndDescriptors).getStateSerializerField());
        BytecodeExpression state = thisVariable.getField(getOnlyElement(stateFieldAndDescriptors).getStateField());
        body.append(state.invoke("setGroupId", void.class, groupId.cast(long.class))).append(stateSerializer.invoke("serialize", void.class, state.cast(Object.class), out)).ret();
    } else {
        Variable rowBuilder = method.getScope().declareVariable(BlockBuilder.class, "rowBuilder");
        body.append(rowBuilder.set(out.invoke("beginBlockEntry", BlockBuilder.class)));
        for (int i = 0; i < stateFieldAndDescriptors.size(); i++) {
            BytecodeExpression stateSerializer = thisVariable.getField(stateFieldAndDescriptors.get(i).getStateSerializerField());
            BytecodeExpression state = thisVariable.getField(stateFieldAndDescriptors.get(i).getStateField());
            body.append(state.invoke("setGroupId", void.class, groupId.cast(long.class))).append(stateSerializer.invoke("serialize", void.class, state.cast(Object.class), rowBuilder));
        }
        body.append(out.invoke("closeEntry", BlockBuilder.class).pop()).ret();
    }
}
Also used : Variable(io.airlift.bytecode.Variable) MethodDefinition(io.airlift.bytecode.MethodDefinition) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) Parameter(io.airlift.bytecode.Parameter) BytecodeExpression(io.airlift.bytecode.expression.BytecodeExpression) BlockBuilder(io.prestosql.spi.block.BlockBuilder)

Example 7 with Variable

use of io.airlift.bytecode.Variable in project hetu-core by openlookeng.

the class NullIfCodeGenerator method generateExpression.

@Override
public BytecodeNode generateExpression(FunctionHandle functionHandle, BytecodeGeneratorContext generatorContext, Type returnType, List<RowExpression> arguments) {
    Scope scope = generatorContext.getScope();
    RowExpression first = arguments.get(0);
    RowExpression second = arguments.get(1);
    LabelNode notMatch = new LabelNode("notMatch");
    // push first arg on the stack
    Variable firstValue = scope.createTempVariable(first.getType().getJavaType());
    BytecodeBlock block = new BytecodeBlock().comment("check if first arg is null").append(generatorContext.generate(first)).append(ifWasNullPopAndGoto(scope, notMatch, void.class)).dup(first.getType().getJavaType()).putVariable(firstValue);
    Type firstType = first.getType();
    Type secondType = second.getType();
    // if (equal(cast(first as <common type>), cast(second as <common type>))
    FunctionAndTypeManager functionAndTypeManager = generatorContext.getFunctionManager();
    FunctionHandle equalFunction = functionAndTypeManager.resolveOperatorFunctionHandle(EQUAL, TypeSignatureProvider.fromTypes(firstType, secondType));
    FunctionMetadata equalFunctionMetadata = functionAndTypeManager.getFunctionMetadata(equalFunction);
    BuiltInScalarFunctionImplementation equalsFunction = generatorContext.getFunctionManager().getBuiltInScalarFunctionImplementation(equalFunction);
    BytecodeNode equalsCall = generatorContext.generateCall(EQUAL.name(), equalsFunction, ImmutableList.of(cast(generatorContext, firstValue, firstType, equalFunctionMetadata.getArgumentTypes().get(0)), cast(generatorContext, generatorContext.generate(second, Optional.empty()), secondType, equalFunctionMetadata.getArgumentTypes().get(1))));
    BytecodeBlock conditionBlock = new BytecodeBlock().append(equalsCall).append(BytecodeUtils.ifWasNullClearPopAndGoto(scope, notMatch, void.class, boolean.class));
    // if first and second are equal, return null
    BytecodeBlock trueBlock = new BytecodeBlock().append(generatorContext.wasNull().set(constantTrue())).pop(first.getType().getJavaType()).pushJavaDefault(first.getType().getJavaType());
    // else return first (which is still on the stack
    block.append(new IfStatement().condition(conditionBlock).ifTrue(trueBlock).ifFalse(notMatch));
    return block;
}
Also used : LabelNode(io.airlift.bytecode.instruction.LabelNode) FunctionMetadata(io.prestosql.spi.function.FunctionMetadata) IfStatement(io.airlift.bytecode.control.IfStatement) CastType(io.prestosql.metadata.CastType) Type(io.prestosql.spi.type.Type) Variable(io.airlift.bytecode.Variable) Scope(io.airlift.bytecode.Scope) FunctionAndTypeManager(io.prestosql.metadata.FunctionAndTypeManager) BuiltInScalarFunctionImplementation(io.prestosql.spi.function.BuiltInScalarFunctionImplementation) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) RowExpression(io.prestosql.spi.relation.RowExpression) BytecodeNode(io.airlift.bytecode.BytecodeNode) FunctionHandle(io.prestosql.spi.function.FunctionHandle)

Example 8 with Variable

use of io.airlift.bytecode.Variable in project hetu-core by openlookeng.

the class OrderingCompiler method generatePageIndexCompareTo.

private static void generatePageIndexCompareTo(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, List<Type> sortTypes, List<Integer> sortChannels, List<SortOrder> sortOrders) {
    Parameter pagesIndex = arg("pagesIndex", PagesIndex.class);
    Parameter leftPosition = arg("leftPosition", int.class);
    Parameter rightPosition = arg("rightPosition", int.class);
    MethodDefinition compareToMethod = classDefinition.declareMethod(a(PUBLIC), "compareTo", type(int.class), pagesIndex, leftPosition, rightPosition);
    Scope scope = compareToMethod.getScope();
    Variable valueAddresses = scope.declareVariable(LongArrayList.class, "valueAddresses");
    compareToMethod.getBody().comment("LongArrayList valueAddresses = pagesIndex.valueAddresses").append(valueAddresses.set(pagesIndex.invoke("getValueAddresses", LongArrayList.class)));
    Variable leftPageAddress = scope.declareVariable(long.class, "leftPageAddress");
    compareToMethod.getBody().comment("long leftPageAddress = valueAddresses.getLong(leftPosition)").append(leftPageAddress.set(valueAddresses.invoke("getLong", long.class, leftPosition)));
    Variable leftBlockIndex = scope.declareVariable(int.class, "leftBlockIndex");
    compareToMethod.getBody().comment("int leftBlockIndex = decodeSliceIndex(leftPageAddress)").append(leftBlockIndex.set(invokeStatic(SyntheticAddress.class, "decodeSliceIndex", int.class, leftPageAddress)));
    Variable leftBlockPosition = scope.declareVariable(int.class, "leftBlockPosition");
    compareToMethod.getBody().comment("int leftBlockPosition = decodePosition(leftPageAddress)").append(leftBlockPosition.set(invokeStatic(SyntheticAddress.class, "decodePosition", int.class, leftPageAddress)));
    Variable rightPageAddress = scope.declareVariable(long.class, "rightPageAddress");
    compareToMethod.getBody().comment("long rightPageAddress = valueAddresses.getLong(rightPosition);").append(rightPageAddress.set(valueAddresses.invoke("getLong", long.class, rightPosition)));
    Variable rightBlockIndex = scope.declareVariable(int.class, "rightBlockIndex");
    compareToMethod.getBody().comment("int rightBlockIndex = decodeSliceIndex(rightPageAddress)").append(rightBlockIndex.set(invokeStatic(SyntheticAddress.class, "decodeSliceIndex", int.class, rightPageAddress)));
    Variable rightBlockPosition = scope.declareVariable(int.class, "rightBlockPosition");
    compareToMethod.getBody().comment("int rightBlockPosition = decodePosition(rightPageAddress)").append(rightBlockPosition.set(invokeStatic(SyntheticAddress.class, "decodePosition", int.class, rightPageAddress)));
    for (int i = 0; i < sortChannels.size(); i++) {
        int sortChannel = sortChannels.get(i);
        SortOrder sortOrder = sortOrders.get(i);
        BytecodeBlock block = new BytecodeBlock().setDescription("compare channel " + sortChannel + " " + sortOrder);
        Type sortType = sortTypes.get(i);
        BytecodeExpression leftBlock = pagesIndex.invoke("getChannel", ObjectArrayList.class, constantInt(sortChannel)).invoke("get", Object.class, leftBlockIndex).cast(Block.class);
        BytecodeExpression rightBlock = pagesIndex.invoke("getChannel", ObjectArrayList.class, constantInt(sortChannel)).invoke("get", Object.class, rightBlockIndex).cast(Block.class);
        block.append(getStatic(SortOrder.class, sortOrder.name()).invoke("compareBlockValue", int.class, ImmutableList.of(Type.class, Block.class, int.class, Block.class, int.class), constantType(callSiteBinder, sortType), leftBlock, leftBlockPosition, rightBlock, rightBlockPosition));
        LabelNode equal = new LabelNode("equal");
        block.comment("if (compare != 0) return compare").dup().ifZeroGoto(equal).retInt().visitLabel(equal).pop(int.class);
        compareToMethod.getBody().append(block);
    }
    // values are equal
    compareToMethod.getBody().push(0).retInt();
}
Also used : LabelNode(io.airlift.bytecode.instruction.LabelNode) SqlTypeBytecodeExpression.constantType(io.prestosql.sql.gen.SqlTypeBytecodeExpression.constantType) Type(io.prestosql.spi.type.Type) 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) SortOrder(io.prestosql.spi.block.SortOrder) BytecodeExpression(io.airlift.bytecode.expression.BytecodeExpression)

Example 9 with Variable

use of io.airlift.bytecode.Variable in project hetu-core by openlookeng.

the class PageFunctionCompiler method generateConstructor.

private static void generateConstructor(ClassDefinition classDefinition, CachedInstanceBinder cachedInstanceBinder, Map<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap, Consumer<MethodDefinition> additionalStatements) {
    MethodDefinition constructorDefinition = classDefinition.declareConstructor(a(PUBLIC));
    BytecodeBlock body = constructorDefinition.getBody();
    Variable thisVariable = constructorDefinition.getThis();
    body.comment("super();").append(thisVariable).invokeConstructor(Object.class);
    additionalStatements.accept(constructorDefinition);
    cachedInstanceBinder.generateInitializations(thisVariable, body);
    body.ret();
}
Also used : Variable(io.airlift.bytecode.Variable) MethodDefinition(io.airlift.bytecode.MethodDefinition) BytecodeBlock(io.airlift.bytecode.BytecodeBlock)

Example 10 with Variable

use of io.airlift.bytecode.Variable 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)

Aggregations

Variable (io.airlift.bytecode.Variable)148 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)114 MethodDefinition (io.airlift.bytecode.MethodDefinition)102 Parameter (io.airlift.bytecode.Parameter)86 IfStatement (io.airlift.bytecode.control.IfStatement)70 Scope (io.airlift.bytecode.Scope)63 BytecodeExpression (io.airlift.bytecode.expression.BytecodeExpression)50 BytecodeNode (io.airlift.bytecode.BytecodeNode)39 LabelNode (io.airlift.bytecode.instruction.LabelNode)32 ClassDefinition (io.airlift.bytecode.ClassDefinition)30 FieldDefinition (io.airlift.bytecode.FieldDefinition)26 ImmutableList (com.google.common.collect.ImmutableList)24 ForLoop (io.airlift.bytecode.control.ForLoop)21 ArrayList (java.util.ArrayList)21 Type (io.trino.spi.type.Type)16 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)15 Block (io.prestosql.spi.block.Block)15 Type (io.prestosql.spi.type.Type)14 List (java.util.List)14 Block (io.trino.spi.block.Block)13