use of com.facebook.presto.spi.function.AccumulatorStateSerializer in project presto by prestodb.
the class BindableAggregationFunction method specialize.
@Override
public InternalAggregationFunction specialize(BoundVariables variables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) {
// bind variables
Signature boundSignature = applyBoundVariables(getSignature(), variables, arity);
List<Type> inputTypes = boundSignature.getArgumentTypes().stream().map(x -> typeManager.getType(x)).collect(toImmutableList());
Type outputType = typeManager.getType(boundSignature.getReturnType());
AggregationFunction aggregationAnnotation = definitionClass.getAnnotation(AggregationFunction.class);
requireNonNull(aggregationAnnotation, "aggregationAnnotation is null");
DynamicClassLoader classLoader = new DynamicClassLoader(definitionClass.getClassLoader(), getClass().getClassLoader());
AggregationMetadata metadata;
AccumulatorStateSerializer<?> stateSerializer = StateCompiler.generateStateSerializer(stateClass, classLoader);
Type intermediateType = stateSerializer.getSerializedType();
Method combineFunction = AggregationCompiler.getCombineFunction(definitionClass, stateClass);
AccumulatorStateFactory<?> stateFactory = StateCompiler.generateStateFactory(stateClass, classLoader);
try {
MethodHandle inputHandle = lookup().unreflect(inputFunction);
MethodHandle combineHandle = lookup().unreflect(combineFunction);
MethodHandle outputHandle = outputFunction == null ? null : lookup().unreflect(outputFunction);
metadata = new AggregationMetadata(generateAggregationName(getSignature().getName(), outputType.getTypeSignature(), signaturesFromTypes(inputTypes)), getParameterMetadata(inputFunction, inputTypes), inputHandle, combineHandle, outputHandle, stateClass, stateSerializer, stateFactory, outputType);
} catch (IllegalAccessException e) {
throw Throwables.propagate(e);
}
AccumulatorFactoryBinder factory = new LazyAccumulatorFactoryBinder(metadata, classLoader);
return new InternalAggregationFunction(getSignature().getName(), inputTypes, intermediateType, outputType, decomposable, factory);
}
use of com.facebook.presto.spi.function.AccumulatorStateSerializer in project presto by prestodb.
the class StateCompiler method generateStateSerializer.
public static <T> AccumulatorStateSerializer<T> generateStateSerializer(Class<T> clazz, Map<String, Type> fieldTypes, DynamicClassLoader classLoader) {
AccumulatorStateMetadata metadata = getMetadataAnnotation(clazz);
if (metadata != null && metadata.stateSerializerClass() != void.class) {
try {
return (AccumulatorStateSerializer<T>) metadata.stateSerializerClass().getConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw Throwables.propagate(e);
}
}
ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(clazz.getSimpleName() + "Serializer"), type(Object.class), type(AccumulatorStateSerializer.class));
CallSiteBinder callSiteBinder = new CallSiteBinder();
// Generate constructor
definition.declareDefaultConstructor(a(PUBLIC));
List<StateField> fields = enumerateFields(clazz, fieldTypes);
generateGetSerializedType(definition, fields, callSiteBinder);
generateSerialize(definition, callSiteBinder, clazz, fields);
generateDeserialize(definition, callSiteBinder, clazz, fields);
Class<? extends AccumulatorStateSerializer> serializerClass = defineClass(definition, AccumulatorStateSerializer.class, callSiteBinder.getBindings(), classLoader);
try {
return (AccumulatorStateSerializer<T>) serializerClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw Throwables.propagate(e);
}
}
Aggregations