use of io.airlift.bytecode.ClassDefinition in project hetu-core by openlookeng.
the class ConcatFunction method generateConcat.
private static Class<?> generateConcat(TypeSignature type, int arity) {
checkCondition(arity <= 254, NOT_SUPPORTED, "Too many arguments for string concatenation");
ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(type.getBase() + "_concat" + arity + "ScalarFunction"), type(Object.class));
// Generate constructor
definition.declareDefaultConstructor(a(PRIVATE));
// Generate concat()
List<Parameter> parameters = IntStream.range(0, arity).mapToObj(i -> arg("arg" + i, Slice.class)).collect(toImmutableList());
MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "concat", type(Slice.class), parameters);
Scope scope = method.getScope();
BytecodeBlock body = method.getBody();
Variable length = scope.declareVariable(int.class, "length");
body.append(length.set(constantInt(0)));
for (int i = 0; i < arity; ++i) {
body.append(length.set(generateCheckedAdd(length, parameters.get(i).invoke("length", int.class))));
}
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)));
for (int i = 0; i < arity; ++i) {
body.append(result.invoke("setBytes", void.class, position, parameters.get(i)));
body.append(position.set(add(position, parameters.get(i).invoke("length", int.class))));
}
body.getVariable(result).retObject();
return defineClass(definition, Object.class, ImmutableMap.of(), new DynamicClassLoader(ConcatFunction.class.getClassLoader()));
}
use of io.airlift.bytecode.ClassDefinition in project hetu-core by openlookeng.
the class ArrayToArrayCast method generateArrayCast.
private static Class<?> generateArrayCast(TypeManager typeManager, FunctionMetadata elementCastFunctionMetadata, BuiltInScalarFunctionImplementation elementCast) {
CallSiteBinder binder = new CallSiteBinder();
ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(Joiner.on("$").join("ArrayCast", elementCastFunctionMetadata.getArgumentTypes().get(0), elementCastFunctionMetadata.getReturnType())), type(Object.class));
Parameter session = arg("session", ConnectorSession.class);
Parameter value = arg("value", Block.class);
MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "castArray", type(Block.class), session, value);
Scope scope = method.getScope();
BytecodeBlock body = method.getBody();
Variable wasNull = scope.declareVariable(boolean.class, "wasNull");
body.append(wasNull.set(constantBoolean(false)));
// cast map elements
Type fromElementType = typeManager.getType(elementCastFunctionMetadata.getArgumentTypes().get(0));
Type toElementType = typeManager.getType(elementCastFunctionMetadata.getReturnType());
CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(definition, binder);
ArrayMapBytecodeExpression newArray = ArrayGeneratorUtils.map(scope, cachedInstanceBinder, fromElementType, toElementType, value, elementCastFunctionMetadata.getName().getObjectName(), elementCast);
// return the block
body.append(newArray.ret());
MethodDefinition constructorDefinition = definition.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();
return defineClass(definition, Object.class, binder.getBindings(), ArrayToArrayCast.class.getClassLoader());
}
use of io.airlift.bytecode.ClassDefinition in project hetu-core by openlookeng.
the class AccumulatorCompiler method generateAccumulatorClass.
private static <T> Class<? extends T> generateAccumulatorClass(Class<T> accumulatorInterface, AggregationMetadata metadata, DynamicClassLoader classLoader) {
boolean grouped = accumulatorInterface == GroupedAccumulator.class;
ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(metadata.getName() + accumulatorInterface.getSimpleName()), type(Object.class), type(accumulatorInterface));
CallSiteBinder callSiteBinder = new CallSiteBinder();
List<AccumulatorStateDescriptor> stateDescriptors = metadata.getAccumulatorStateDescriptors();
List<StateFieldAndDescriptor> stateFieldAndDescriptors = new ArrayList<>();
for (int i = 0; i < stateDescriptors.size(); i++) {
stateFieldAndDescriptors.add(new StateFieldAndDescriptor(definition.declareField(a(PRIVATE, FINAL), "stateSerializer_" + i, AccumulatorStateSerializer.class), definition.declareField(a(PRIVATE, FINAL), "stateFactory_" + i, AccumulatorStateFactory.class), definition.declareField(a(PRIVATE, FINAL), "state_" + i, grouped ? stateDescriptors.get(i).getFactory().getGroupedStateClass() : stateDescriptors.get(i).getFactory().getSingleStateClass()), stateDescriptors.get(i)));
}
List<FieldDefinition> stateFields = stateFieldAndDescriptors.stream().map(StateFieldAndDescriptor::getStateField).collect(toImmutableList());
int lambdaCount = metadata.getLambdaInterfaces().size();
List<FieldDefinition> lambdaProviderFields = new ArrayList<>(lambdaCount);
for (int i = 0; i < lambdaCount; i++) {
lambdaProviderFields.add(definition.declareField(a(PRIVATE, FINAL), "lambdaProvider_" + i, LambdaProvider.class));
}
FieldDefinition inputChannelsField = definition.declareField(a(PRIVATE, FINAL), "inputChannels", type(List.class, Integer.class));
FieldDefinition maskChannelField = definition.declareField(a(PRIVATE, FINAL), "maskChannel", type(Optional.class, Integer.class));
// Generate constructor
generateConstructor(definition, stateFieldAndDescriptors, inputChannelsField, maskChannelField, lambdaProviderFields, grouped);
// Generate methods
generateAddInput(definition, stateFields, inputChannelsField, maskChannelField, metadata.getValueInputMetadata(), metadata.getLambdaInterfaces(), lambdaProviderFields, metadata.getInputFunction(), callSiteBinder, grouped);
generateAddInputWindowIndex(definition, stateFields, metadata.getValueInputMetadata(), metadata.getLambdaInterfaces(), lambdaProviderFields, metadata.getInputFunction(), callSiteBinder);
generateGetEstimatedSize(definition, stateFields);
generateGetIntermediateType(definition, callSiteBinder, stateDescriptors.stream().map(stateDescriptor -> stateDescriptor.getSerializer().getSerializedType()).collect(toImmutableList()));
generateGetFinalType(definition, callSiteBinder, metadata.getOutputType());
generateAddIntermediateAsCombine(definition, stateFieldAndDescriptors, metadata.getLambdaInterfaces(), lambdaProviderFields, metadata.getCombineFunction(), callSiteBinder, grouped);
if (grouped) {
generateGroupedEvaluateIntermediate(definition, stateFieldAndDescriptors);
} else {
generateEvaluateIntermediate(definition, stateFieldAndDescriptors);
}
if (grouped) {
generateGroupedEvaluateFinal(definition, stateFields, metadata.getOutputFunction(), callSiteBinder);
} else {
generateEvaluateFinal(definition, stateFields, metadata.getOutputFunction(), callSiteBinder);
}
if (grouped) {
generatePrepareFinal(definition);
}
generateCapture(definition);
generateRestore(definition);
return defineClass(definition, accumulatorInterface, callSiteBinder.getBindings(), classLoader);
}
use of io.airlift.bytecode.ClassDefinition in project hetu-core by openlookeng.
the class OrderingCompiler method generatePageWithPositionComparatorClass.
private Class<? extends PageWithPositionComparator> generatePageWithPositionComparatorClass(List<Type> sortTypes, List<Integer> sortChannels, List<SortOrder> sortOrders) {
CallSiteBinder callSiteBinder = new CallSiteBinder();
ClassDefinition classDefinition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName("PageWithPositionComparator"), type(Object.class), type(PageWithPositionComparator.class));
classDefinition.declareDefaultConstructor(a(PUBLIC));
generateMergeSortCompareTo(classDefinition, callSiteBinder, sortTypes, sortChannels, sortOrders);
return defineClass(classDefinition, PageWithPositionComparator.class, callSiteBinder.getBindings(), getClass().getClassLoader());
}
use of io.airlift.bytecode.ClassDefinition in project hetu-core by openlookeng.
the class OrderingCompiler method compilePagesIndexComparator.
private Class<? extends PagesIndexComparator> compilePagesIndexComparator(List<Type> sortTypes, List<Integer> sortChannels, List<SortOrder> sortOrders) {
CallSiteBinder callSiteBinder = new CallSiteBinder();
ClassDefinition classDefinition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName("PagesIndexComparator"), type(Object.class), type(PagesIndexComparator.class));
classDefinition.declareDefaultConstructor(a(PUBLIC));
generatePageIndexCompareTo(classDefinition, callSiteBinder, sortTypes, sortChannels, sortOrders);
return defineClass(classDefinition, PagesIndexComparator.class, callSiteBinder.getBindings(), getClass().getClassLoader());
}
Aggregations