private static void declareBlockVariables(RowExpression expression, Parameter page, Scope scope, BytecodeBlock body) { for (int channel : getInputChannels(expression)) { scope.declareVariable("block_" + channel, body, page.invoke("getBlock", Block.class, constantInt(channel))); } }
Variable map = methodDefinition.getScope().declareVariable( "map", methodDefinition.getBody(),
Variable wasNullVariable = scope.declareVariable("wasNull", body, constantFalse()); RowExpressionCompiler compiler = new RowExpressionCompiler( callSiteBinder, compiledLambdaMap); Variable result = scope.declareVariable(boolean.class, "result"); body.append(compiler.compile(filter, scope, Optional.empty()))
Variable value = scope.declareVariable(javaTypes.get(0), "value");
BytecodeBlock body = method.getBody(); Variable blockBuilderVariable = scope.declareVariable(BlockBuilder.class, "blockBuilder"); CallSiteBinder binder = new CallSiteBinder();
BytecodeBlock body = method.getBody(); Variable length = scope.declareVariable(int.class, "length"); body.append(length.set(constantInt(0))); Variable result = scope.declareVariable(Slice.class, "result"); body.append(result.set(invokeStatic(Slices.class, "allocate", Slice.class, length))); Variable position = scope.declareVariable(int.class, "position"); body.append(position.set(constantInt(0)));
scope.declareVariable("wasNull", body, constantFalse()); RowExpressionCompiler compiler = new RowExpressionCompiler( callSiteBinder,
BytecodeBlock body = method.getBody(); Variable positionCount = scope.declareVariable("positionCount", body, page.invoke("getPositionCount", int.class)); .ifTrue(thisVariable.setField(selectedPositionsField, newArray(type(boolean[].class), positionCount)))); Variable selectedPositions = scope.declareVariable("selectedPositions", body, thisVariable.getField(selectedPositionsField)); Variable position = scope.declareVariable(int.class, "position"); body.append(new ForLoop() .initialize(position.set(constantInt(0)))
private static void generateEvaluateIntermediate(ClassDefinition definition, List<StateFieldAndDescriptor> stateFieldAndDescriptors) { Parameter out = arg("out", BlockBuilder.class); MethodDefinition method = definition.declareMethod( a(PUBLIC), "evaluateIntermediate", type(void.class), 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(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(stateSerializer.invoke("serialize", void.class, state.cast(Object.class), rowBuilder)); } body.append(out.invoke("closeEntry", BlockBuilder.class).pop()) .ret(); } }
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(); } }
private static void generateHashRowMethod(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, List<Type> joinChannelTypes) { Parameter position = arg("position", int.class); Parameter page = arg("blocks", Page.class); MethodDefinition hashRowMethod = classDefinition.declareMethod(a(PUBLIC), "hashRow", type(long.class), position, page); Variable resultVariable = hashRowMethod.getScope().declareVariable(long.class, "result"); hashRowMethod.getBody().push(0L).putVariable(resultVariable); for (int index = 0; index < joinChannelTypes.size(); index++) { BytecodeExpression type = constantType(callSiteBinder, joinChannelTypes.get(index)); BytecodeExpression block = page.invoke("getBlock", Block.class, constantInt(index)); hashRowMethod .getBody() .getVariable(resultVariable) .push(31L) .append(OpCode.LMUL) .append(typeHashCode(type, block, position)) .append(OpCode.LADD) .putVariable(resultVariable); } hashRowMethod .getBody() .getVariable(resultVariable) .retLong(); }
BytecodeBlock body = method.getBody(); Variable wasNull = scope.declareVariable(boolean.class, "wasNull"); body.append(wasNull.set(constantBoolean(false)));
private static void generateGetEstimatedSize(ClassDefinition definition, List<FieldDefinition> stateFields) { MethodDefinition method = definition.declareMethod(a(PUBLIC), "getEstimatedSize", type(long.class)); Variable estimatedSize = method.getScope().declareVariable(long.class, "estimatedSize"); method.getBody().append(estimatedSize.set(constantLong(0L))); for (FieldDefinition stateField : stateFields) { method.getBody() .append(estimatedSize.set( BytecodeExpressions.add( estimatedSize, method.getThis().getField(stateField).invoke("getEstimatedSize", long.class)))); } method.getBody().append(estimatedSize.ret()); }
private static CompiledLambda defineLambdaMethod( RowExpressionCompiler innerExpressionCompiler, ClassDefinition classDefinition, String methodName, List<Parameter> inputParameters, LambdaDefinitionExpression lambda) { checkCondition(inputParameters.size() <= 254, NOT_SUPPORTED, "Too many arguments for lambda expression"); Class<?> returnType = Primitives.wrap(lambda.getBody().getType().getJavaType()); MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), methodName, type(returnType), inputParameters); Scope scope = method.getScope(); Variable wasNull = scope.declareVariable(boolean.class, "wasNull"); BytecodeNode compiledBody = innerExpressionCompiler.compile(lambda.getBody(), scope, Optional.empty()); method.getBody() .putVariable(wasNull, false) .append(compiledBody) .append(boxPrimitiveIfNecessary(scope, returnType)) .ret(returnType); Handle lambdaAsmHandle = new Handle( Opcodes.H_INVOKEVIRTUAL, method.getThis().getType().getClassName(), method.getName(), method.getMethodDescriptor(), false); return new CompiledLambda( lambdaAsmHandle, method.getReturnType(), method.getParameterTypes()); }
private void generateProjectMethod( ClassDefinition classDefinition, CallSiteBinder callSiteBinder, CachedInstanceBinder cachedInstanceBinder, Map<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap, String methodName, RowExpression projection) { Parameter session = arg("session", ConnectorSession.class); Parameter cursor = arg("cursor", RecordCursor.class); Parameter output = arg("output", BlockBuilder.class); MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), methodName, type(void.class), session, cursor, output); method.comment("Projection: %s", projection.toString()); Scope scope = method.getScope(); Variable wasNullVariable = scope.declareVariable(type(boolean.class), "wasNull"); RowExpressionCompiler compiler = new RowExpressionCompiler( callSiteBinder, cachedInstanceBinder, fieldReferenceCompiler(cursor), metadata.getFunctionRegistry(), compiledLambdaMap); method.getBody() .comment("boolean wasNull = false;") .putVariable(wasNullVariable, false) .comment("evaluate projection: " + projection.toString()) .append(compiler.compile(projection, scope, Optional.of(output))) .ret(); }
Variable wasNullVariable = scope.declareVariable(type(boolean.class), "wasNull");
Scope scope = method.getScope(); Variable position = scope.declareVariable(int.class, "position");
BytecodeBlock body = getEstimatedSize.getBody(); Variable size = getEstimatedSize.getScope().declareVariable(long.class, "size");
private static BytecodeBlock generateBlockNonNullPositionForLoop(Scope scope, Variable positionVariable, BytecodeBlock loopBody) { Variable rowsVariable = scope.declareVariable(int.class, "rows"); Variable blockVariable = scope.getVariable("block"); BytecodeBlock block = new BytecodeBlock() .append(blockVariable) .invokeInterface(Block.class, "getPositionCount", int.class) .putVariable(rowsVariable); IfStatement ifStatement = new IfStatement("if(!block.isNull(position))") .condition(new BytecodeBlock() .append(blockVariable) .append(positionVariable) .invokeInterface(Block.class, "isNull", boolean.class, int.class)) .ifFalse(loopBody); block.append(new ForLoop() .initialize(positionVariable.set(constantInt(0))) .condition(new BytecodeBlock() .append(positionVariable) .append(rowsVariable) .invokeStatic(CompilerOperations.class, "lessThan", boolean.class, int.class, int.class)) .update(new BytecodeBlock().incrementVariable(positionVariable, (byte) 1)) .body(ifStatement)); return block; }
Variable completedPositionsVariable = scope.declareVariable(int.class, "completedPositions"); Variable finishedVariable = scope.declareVariable(boolean.class, "finished");