use of io.airlift.bytecode.DynamicClassLoader in project hetu-core by openlookeng.
the class MultimapAggregationFunction method generateAggregation.
private InternalAggregationFunction generateAggregation(Type keyType, Type valueType, Type outputType) {
DynamicClassLoader classLoader = new DynamicClassLoader(MultimapAggregationFunction.class.getClassLoader());
List<Type> inputTypes = ImmutableList.of(keyType, valueType);
MultimapAggregationStateSerializer stateSerializer = new MultimapAggregationStateSerializer(keyType, valueType);
Type intermediateType = stateSerializer.getSerializedType();
AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(NAME, outputType.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createInputParameterMetadata(keyType, valueType), INPUT_FUNCTION, COMBINE_FUNCTION, OUTPUT_FUNCTION.bindTo(keyType).bindTo(valueType), ImmutableList.of(new AccumulatorStateDescriptor(MultimapAggregationState.class, stateSerializer, new MultimapAggregationStateFactory(keyType, valueType, groupMode))), outputType);
GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
return new InternalAggregationFunction(NAME, inputTypes, ImmutableList.of(intermediateType), outputType, true, true, factory);
}
use of io.airlift.bytecode.DynamicClassLoader in project hetu-core by openlookeng.
the class AbstractMinMaxAggregationFunction method generateAggregation.
protected InternalAggregationFunction generateAggregation(Type type, MethodHandle compareMethodHandle) {
DynamicClassLoader classLoader = new DynamicClassLoader(AbstractMinMaxAggregationFunction.class.getClassLoader());
List<Type> inputTypes = ImmutableList.of(type);
MethodHandle inputFunction;
MethodHandle combineFunction;
MethodHandle outputFunction;
Class<? extends AccumulatorState> stateInterface;
AccumulatorStateSerializer<?> stateSerializer;
if (type.getJavaType() == long.class) {
stateInterface = NullableLongState.class;
stateSerializer = StateCompiler.generateStateSerializer(stateInterface, classLoader);
inputFunction = LONG_INPUT_FUNCTION.bindTo(compareMethodHandle);
combineFunction = LONG_COMBINE_FUNCTION.bindTo(compareMethodHandle);
outputFunction = LONG_OUTPUT_FUNCTION.bindTo(type);
} else if (type.getJavaType() == double.class) {
stateInterface = NullableDoubleState.class;
stateSerializer = StateCompiler.generateStateSerializer(stateInterface, classLoader);
inputFunction = DOUBLE_INPUT_FUNCTION.bindTo(compareMethodHandle);
combineFunction = DOUBLE_COMBINE_FUNCTION.bindTo(compareMethodHandle);
outputFunction = DOUBLE_OUTPUT_FUNCTION.bindTo(type);
} else if (type.getJavaType() == boolean.class) {
stateInterface = NullableBooleanState.class;
stateSerializer = StateCompiler.generateStateSerializer(stateInterface, classLoader);
inputFunction = BOOLEAN_INPUT_FUNCTION.bindTo(compareMethodHandle);
combineFunction = BOOLEAN_COMBINE_FUNCTION.bindTo(compareMethodHandle);
outputFunction = BOOLEAN_OUTPUT_FUNCTION.bindTo(type);
} else {
// native container type is Slice or Block
stateInterface = BlockPositionState.class;
stateSerializer = new BlockPositionStateSerializer(type);
inputFunction = min ? BLOCK_POSITION_MIN_INPUT_FUNCTION.bindTo(type) : BLOCK_POSITION_MAX_INPUT_FUNCTION.bindTo(type);
combineFunction = min ? BLOCK_POSITION_MIN_COMBINE_FUNCTION.bindTo(type) : BLOCK_POSITION_MAX_COMBINE_FUNCTION.bindTo(type);
outputFunction = BLOCK_POSITION_OUTPUT_FUNCTION.bindTo(type);
}
AccumulatorStateFactory<?> stateFactory = StateCompiler.generateStateFactory(stateInterface, classLoader);
Type intermediateType = stateSerializer.getSerializedType();
AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(getSignature().getNameSuffix(), type.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createParameterMetadata(type), inputFunction, combineFunction, outputFunction, ImmutableList.of(new AccumulatorStateDescriptor(stateInterface, stateSerializer, stateFactory)), type);
GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
return new InternalAggregationFunction(getSignature().getNameSuffix(), inputTypes, ImmutableList.of(intermediateType), type, true, false, factory);
}
use of io.airlift.bytecode.DynamicClassLoader in project hetu-core by openlookeng.
the class AbstractMinMaxNAggregationFunction method generateAggregation.
protected InternalAggregationFunction generateAggregation(Type type) {
DynamicClassLoader classLoader = new DynamicClassLoader(AbstractMinMaxNAggregationFunction.class.getClassLoader());
BlockComparator comparator = typeToComparator.apply(type);
List<Type> inputTypes = ImmutableList.of(type, BIGINT);
MinMaxNStateSerializer stateSerializer = new MinMaxNStateSerializer(comparator, type);
Type intermediateType = stateSerializer.getSerializedType();
ArrayType outputType = new ArrayType(type);
List<ParameterMetadata> inputParameterMetadata = ImmutableList.of(new ParameterMetadata(STATE), new ParameterMetadata(BLOCK_INPUT_CHANNEL, type), new ParameterMetadata(INPUT_CHANNEL, BIGINT), new ParameterMetadata(BLOCK_INDEX));
AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(getSignature().getNameSuffix(), type.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), inputParameterMetadata, INPUT_FUNCTION.bindTo(comparator).bindTo(type), COMBINE_FUNCTION, OUTPUT_FUNCTION.bindTo(outputType), ImmutableList.of(new AccumulatorStateDescriptor(MinMaxNState.class, stateSerializer, new MinMaxNStateFactory())), outputType);
GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
return new InternalAggregationFunction(getSignature().getNameSuffix(), inputTypes, ImmutableList.of(intermediateType), outputType, true, false, factory);
}
use of io.airlift.bytecode.DynamicClassLoader in project hetu-core by openlookeng.
the class TestStateCompiler method testComplexStateEstimatedSize.
@Test(invocationCount = 100, successPercentage = 90)
public void testComplexStateEstimatedSize() {
Map<String, Type> fieldMap = ImmutableMap.of("Block", new ArrayType(BIGINT), "AnotherBlock", mapType(BIGINT, VARCHAR));
AccumulatorStateFactory<TestComplexState> factory = StateCompiler.generateStateFactory(TestComplexState.class, fieldMap, new DynamicClassLoader(TestComplexState.class.getClassLoader()));
TestComplexState groupedState = factory.createGroupedState();
long initialRetainedSize = getComplexStateRetainedSize(groupedState);
assertEquals(groupedState.getEstimatedSize(), initialRetainedSize);
// BlockBigArray or SliceBigArray has an internal map that can grow in size when getting more blocks
// need to handle the map overhead separately
initialRetainedSize -= getReferenceCountMapOverhead(groupedState);
for (int i = 0; i < 1000; i++) {
long retainedSize = 0;
((GroupedAccumulatorState) groupedState).setGroupId(i);
groupedState.setBoolean(true);
groupedState.setLong(1);
groupedState.setDouble(2.0);
groupedState.setByte((byte) 3);
groupedState.setInt(4);
Slice slice = utf8Slice("test");
retainedSize += slice.getRetainedSize();
groupedState.setSlice(slice);
slice = wrappedDoubleArray(1.0, 2.0, 3.0);
retainedSize += slice.getRetainedSize();
groupedState.setAnotherSlice(slice);
groupedState.setYetAnotherSlice(null);
Block array = createLongsBlock(45);
retainedSize += array.getRetainedSizeInBytes();
groupedState.setBlock(array);
BlockBuilder mapBlockBuilder = mapType(BIGINT, VARCHAR).createBlockBuilder(null, 1);
BlockBuilder singleMapBlockWriter = mapBlockBuilder.beginBlockEntry();
BIGINT.writeLong(singleMapBlockWriter, 123L);
VARCHAR.writeSlice(singleMapBlockWriter, utf8Slice("testBlock"));
mapBlockBuilder.closeEntry();
Block map = mapBlockBuilder.build();
retainedSize += map.getRetainedSizeInBytes();
groupedState.setAnotherBlock(map);
assertEquals(groupedState.getEstimatedSize(), initialRetainedSize + retainedSize * (i + 1) + getReferenceCountMapOverhead(groupedState));
}
for (int i = 0; i < 1000; i++) {
long retainedSize = 0;
((GroupedAccumulatorState) groupedState).setGroupId(i);
groupedState.setBoolean(true);
groupedState.setLong(1);
groupedState.setDouble(2.0);
groupedState.setByte((byte) 3);
groupedState.setInt(4);
Slice slice = utf8Slice("test");
retainedSize += slice.getRetainedSize();
groupedState.setSlice(slice);
slice = wrappedDoubleArray(1.0, 2.0, 3.0);
retainedSize += slice.getRetainedSize();
groupedState.setAnotherSlice(slice);
groupedState.setYetAnotherSlice(null);
Block array = createLongsBlock(45);
retainedSize += array.getRetainedSizeInBytes();
groupedState.setBlock(array);
BlockBuilder mapBlockBuilder = mapType(BIGINT, VARCHAR).createBlockBuilder(null, 1);
BlockBuilder singleMapBlockWriter = mapBlockBuilder.beginBlockEntry();
BIGINT.writeLong(singleMapBlockWriter, 123L);
VARCHAR.writeSlice(singleMapBlockWriter, utf8Slice("testBlock"));
mapBlockBuilder.closeEntry();
Block map = mapBlockBuilder.build();
retainedSize += map.getRetainedSizeInBytes();
groupedState.setAnotherBlock(map);
assertEquals(groupedState.getEstimatedSize(), initialRetainedSize + retainedSize * 1000 + getReferenceCountMapOverhead(groupedState));
}
}
use of io.airlift.bytecode.DynamicClassLoader in project hetu-core by openlookeng.
the class AbstractGreatestLeast method generate.
private Class<?> generate(List<Class<?>> javaTypes, Type type, MethodHandle compareMethod) {
checkCondition(javaTypes.size() <= 127, NOT_SUPPORTED, "Too many arguments for function call %s()", getSignature().getName());
String javaTypeName = javaTypes.stream().map(Class::getSimpleName).collect(joining());
ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(javaTypeName + "$" + getSignature().getName()), type(Object.class));
definition.declareDefaultConstructor(a(PRIVATE));
List<Parameter> parameters = IntStream.range(0, javaTypes.size()).mapToObj(i -> arg("arg" + i, javaTypes.get(i))).collect(toImmutableList());
MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), getSignature().getNameSuffix(), type(javaTypes.get(0)), parameters);
Scope scope = method.getScope();
BytecodeBlock body = method.getBody();
CallSiteBinder binder = new CallSiteBinder();
if (type.getTypeSignature().getBase().equals(StandardTypes.DOUBLE)) {
for (Parameter parameter : parameters) {
body.append(parameter);
body.append(invoke(binder.bind(CHECK_NOT_NAN.bindTo(getSignature().getName().toString())), "checkNotNaN"));
}
}
Variable value = scope.declareVariable(javaTypes.get(0), "value");
body.append(value.set(parameters.get(0)));
for (int i = 1; i < javaTypes.size(); i++) {
body.append(new IfStatement().condition(new BytecodeBlock().append(parameters.get(i)).append(value).append(invoke(binder.bind(compareMethod), "compare"))).ifTrue(value.set(parameters.get(i))));
}
body.append(value.ret());
return defineClass(definition, Object.class, binder.getBindings(), new DynamicClassLoader(getClass().getClassLoader()));
}
Aggregations