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(); } }
List<FieldDefinition> joinChannelFields) Parameter leftBlockIndex = arg("leftBlockIndex", int.class); Parameter leftBlockPosition = arg("leftBlockPosition", int.class); Parameter rightPosition = arg("rightPosition", int.class); Parameter page = arg("page", Page.class); Parameter rightChannels = arg("rightChannels", int[].class); .cast(Block.class); BytecodeExpression rightBlock = page.invoke("getBlock", Block.class, rightChannels.getElement(index)); body.append(new IfStatement() .condition(typeEquals(type, leftBlock, leftBlockPosition, rightBlock, rightPosition))
parameterListBuilder.add(arg("userState", VarArgsToArrayAdapterState.class)); for (int i = 0; i < argsLength; i++) { parameterListBuilder.add(arg("input_" + i, javaType)); BytecodeBlock body = methodDefinition.getBody(); BytecodeExpression userState = parameterList.get(0).getField("userState", Object.class); BytecodeExpression args = parameterList.get(0).getField("args", Object.class).cast(javaArrayType); for (int i = 0; i < argsLength; i++) { body.append(args.setElement(i, parameterList.get(i + 1)));
private void generateCombineMethod(ClassDefinition definition, CallSiteBinder binder, MethodHandle compareMethod, Type keyType, Type valueType, Class<?> stateClass) { Parameter state = arg("state", stateClass); Parameter otherState = arg("otherState", stateClass); MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "combine", type(void.class), state, otherState); Class<?> keyJavaType = keyType.getJavaType(); BytecodeBlock ifBlock = new BytecodeBlock() .append(state.invoke("setFirst", void.class, otherState.invoke("getFirst", keyJavaType))) .append(state.invoke("setFirstNull", void.class, otherState.invoke("isFirstNull", boolean.class))) .append(state.invoke("setSecondNull", void.class, otherState.invoke("isSecondNull", boolean.class))); if (valueType.getJavaType().isPrimitive()) { ifBlock.append(state.invoke("setSecond", void.class, otherState.invoke("getSecond", valueType.getJavaType()))); } else { ifBlock.append(new BytecodeBlock() .append(state.invoke("setSecondBlock", void.class, otherState.invoke("getSecondBlock", Block.class))) .append(state.invoke("setSecondPosition", void.class, otherState.invoke("getSecondPosition", int.class)))); } method.getBody() .append(new IfStatement() .condition(or( state.invoke("isFirstNull", boolean.class), and( not(otherState.invoke("isFirstNull", boolean.class)), loadConstant(binder, compareMethod, MethodHandle.class).invoke("invokeExact", boolean.class, otherState.invoke("getFirst", keyJavaType), state.invoke("getFirst", keyJavaType))))) .ifTrue(ifBlock)) .ret(); }
private static void generateGroupedEvaluateFinal( ClassDefinition definition, List<FieldDefinition> stateFields, MethodHandle outputFunction, CallSiteBinder callSiteBinder) { Parameter groupId = arg("groupId", int.class); Parameter out = arg("out", BlockBuilder.class); MethodDefinition method = definition.declareMethod(a(PUBLIC), "evaluateFinal", type(void.class), groupId, out); BytecodeBlock body = method.getBody(); Variable thisVariable = method.getThis(); List<BytecodeExpression> states = new ArrayList<>(); for (FieldDefinition stateField : stateFields) { BytecodeExpression state = thisVariable.getField(stateField); body.append(state.invoke("setGroupId", void.class, groupId.cast(long.class))); states.add(state); } body.comment("output(state_0, state_1, ..., out)"); states.forEach(body::append); body.append(out); body.append(invoke(callSiteBinder.bind(outputFunction), "output")); body.ret(); }
private static List<Parameter> toBlockParameters(List<Integer> inputChannels) { ImmutableList.Builder<Parameter> parameters = ImmutableList.builder(); for (int channel : inputChannels) { parameters.add(arg("block_" + channel, Block.class)); } return parameters.build(); }
/** * Defines the generics bridge method with untyped args to the type specific write method. */ private void defineWriteBridgeMethod() { Parameter struct = arg("struct", Object.class); Parameter protocol = arg("protocol", TProtocolWriter.class); MethodDefinition method = new MethodDefinition( classDefinition, a(PUBLIC, BRIDGE, SYNTHETIC), "write", null, struct, protocol) .addException(Exception.class); method.getBody().append(method.getThis() .invoke("write", type(void.class), ImmutableList.of(structType, protocol.getType()), ImmutableList.of(struct.cast(structType), protocol)) .ret()); classDefinition.addMethod(method); }
private static MethodDefinition defineSetAndGetMethod(Class<?> aClass) { Parameter arr = arg("arr", type(aClass)); Parameter index = arg("index", type(int.class)); Class<?> componentType = aClass.getComponentType(); Parameter value = arg("value", type(componentType)); MethodDefinition methodDefinition = classDefinition.declareMethod(a(PUBLIC, STATIC), "setAndGetMethod_" + componentType.getSimpleName(), type(componentType), arr, index, value); methodDefinition.getBody() .append(arr.setElement(index, value)) .append(arr.getElement(index).ret()); return methodDefinition; } }
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))); } }
private void generateOutputMethod(ClassDefinition definition, CallSiteBinder binder, Type valueType, Class<?> stateClass) { Parameter state = arg("state", stateClass); Parameter out = arg("out", BlockBuilder.class); MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "output", type(void.class), state, out); IfStatement ifStatement = new IfStatement() .condition(or(state.invoke("isFirstNull", boolean.class), state.invoke("isSecondNull", boolean.class))) .ifTrue(new BytecodeBlock().append(out.invoke("appendNull", BlockBuilder.class)).pop()); SqlTypeBytecodeExpression valueSqlType = constantType(binder, valueType); BytecodeExpression getValueExpression; if (valueType.getJavaType().isPrimitive()) { getValueExpression = state.invoke("getSecond", valueType.getJavaType()); } else { getValueExpression = valueSqlType.getValue(state.invoke("getSecondBlock", Block.class), state.invoke("getSecondPosition", int.class)); } ifStatement.ifFalse(valueSqlType.writeValue(out, getValueExpression)); method.getBody().append(ifStatement).ret(); } }
private static MethodDefinition declareAddIntermediate(ClassDefinition definition, boolean grouped) { ImmutableList.Builder<Parameter> parameters = ImmutableList.builder(); if (grouped) { parameters.add(arg("groupIdsBlock", GroupByIdBlock.class)); } parameters.add(arg("block", Block.class)); return definition.declareMethod( a(PUBLIC), "addIntermediate", type(void.class), parameters.build()); }
for (int i = 0; i < javaTypes.size(); i++) { Class<?> javaType = javaTypes.get(i); parameterListBuilder.add(arg("input_" + i, javaType)); invokeStatic(Maps.class, "newHashMapWithExpectedSize", HashMap.class, constantInt(javaTypes.size()))); for (int i = 0; i < javaTypes.size(); i++) { body.append(map.invoke("put", Object.class, constantString(names.get(i)).cast(Object.class), parameterList.get(i).cast(Object.class)));
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))); } }
Parameter block = arg("block", Block.class); Parameter index = arg("index", int.class); Parameter state = arg("state", Object.class); MethodDefinition method = definition.declareMethod(a(PUBLIC), "deserialize", type(void.class), block, index, state); BytecodeBlock deserializerBody = method.getBody(); if (!field.isPrimitiveType()) { deserializerBody.append(new IfStatement() .condition(block.invoke("isNull", boolean.class, index)) .ifTrue(state.cast(setter.getDeclaringClass()).invoke(setter, constantNull(field.getType()))) .ifFalse(state.cast(setter.getDeclaringClass()).invoke(setter, constantType(binder, field.getSqlType()).getValue(block, index)))); state.cast(setter.getDeclaringClass()).invoke( setter, constantType(binder, field.getSqlType()).getValue(block, index).cast(field.getType()))); deserializerBody.append(row.set(block.invoke("getObject", Object.class, index, constantClass(Block.class)).cast(Block.class))); int position = 0; for (StateField field : fields) { deserializerBody.append(new IfStatement() .condition(row.invoke("isNull", boolean.class, constantInt(position))) .ifTrue(state.cast(setter.getDeclaringClass()).invoke(setter, constantNull(field.getType()))) .ifFalse(state.cast(setter.getDeclaringClass()).invoke(setter, constantType(binder, field.getSqlType()).getValue(row, constantInt(position))))); state.cast(setter.getDeclaringClass()).invoke( setter, constantType(binder, field.getSqlType()).getValue(row, constantInt(position)).cast(field.getType())));
private void generateInputMethod(ClassDefinition definition, CallSiteBinder binder, MethodHandle compareMethod, Type keyType, Type valueType, Class<?> stateClass) Parameter state = arg("state", stateClass); Parameter value = arg("value", Block.class); Parameter key = arg("key", Block.class); Parameter position = arg("position", int.class); MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "input", type(void.class), state, value, key, position); SqlTypeBytecodeExpression keySqlType = constantType(binder, keyType); .append(state.invoke("setFirst", void.class, keySqlType.getValue(key, position))) .append(state.invoke("setFirstNull", void.class, constantBoolean(false))) .append(state.invoke("setSecondNull", void.class, value.invoke("isNull", boolean.class, position))); BytecodeNode setValueNode; if (valueType.getJavaType().isPrimitive()) { SqlTypeBytecodeExpression valueSqlType = constantType(binder, valueType); setValueNode = state.invoke("setSecond", void.class, valueSqlType.getValue(value, position)); .append(state.invoke("setSecondBlock", void.class, value)) .append(state.invoke("setSecondPosition", void.class, position)); .condition(value.invoke("isNull", boolean.class, position)) .ifFalse(setValueNode)); state.invoke("isFirstNull", boolean.class), and( not(key.invoke("isNull", boolean.class, position)), loadConstant(binder, compareMethod, MethodHandle.class).invoke("invokeExact", boolean.class, keySqlType.getValue(key, position), state.invoke("getFirst", keyType.getJavaType()))))) .ifTrue(ifBlock)) .ret();
ImmutableMap.Builder<String, ParameterAndType> parameterMapBuilder = ImmutableMap.builder(); parameters.add(arg("session", ConnectorSession.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));
List<FieldDefinition> joinChannelFields) Parameter leftBlockIndex = arg("leftBlockIndex", int.class); Parameter leftBlockPosition = arg("leftBlockPosition", int.class); Parameter rightPosition = arg("rightPosition", int.class); Parameter page = arg("page", Page.class); Parameter rightChannels = arg("rightChannels", int[].class); .invoke("get", Object.class, leftBlockIndex) .cast(Block.class); BytecodeExpression rightBlock = page.invoke("getBlock", Block.class, rightChannels.getElement(index)); Type type = joinChannelTypes.get(index);
private static void generateGroupedEvaluateFinal( ClassDefinition definition, List<FieldDefinition> stateFields, MethodHandle outputFunction, CallSiteBinder callSiteBinder) { Parameter groupId = arg("groupId", int.class); Parameter out = arg("out", BlockBuilder.class); MethodDefinition method = definition.declareMethod(a(PUBLIC), "evaluateFinal", type(void.class), groupId, out); BytecodeBlock body = method.getBody(); Variable thisVariable = method.getThis(); List<BytecodeExpression> states = new ArrayList<>(); for (FieldDefinition stateField : stateFields) { BytecodeExpression state = thisVariable.getField(stateField); body.append(state.invoke("setGroupId", void.class, groupId.cast(long.class))); states.add(state); } body.comment("output(state_0, state_1, ..., out)"); states.forEach(body::append); body.append(out); body.append(invoke(callSiteBinder.bind(outputFunction), "output")); body.ret(); }
parameterListBuilder.add(arg("userState", VarArgsToArrayAdapterState.class)); for (int i = 0; i < argsLength; i++) { parameterListBuilder.add(arg("input_" + i, javaType)); BytecodeBlock body = methodDefinition.getBody(); BytecodeExpression userState = parameterList.get(0).getField("userState", Object.class); BytecodeExpression args = parameterList.get(0).getField("args", Object.class).cast(javaArrayType); for (int i = 0; i < argsLength; i++) { body.append(args.setElement(i, parameterList.get(i + 1)));