Search in sources :

Example 1 with AccumulatorStateSerializer

use of io.trino.spi.function.AccumulatorStateSerializer in project trino by trinodb.

the class StateCompiler method getSerializedType.

public static Type getSerializedType(Class<?> clazz, Map<String, Type> fieldTypes) {
    AccumulatorStateMetadata metadata = getMetadataAnnotation(clazz);
    if (metadata != null && metadata.stateSerializerClass() != AccumulatorStateSerializer.class) {
        try {
            AccumulatorStateSerializer<?> stateSerializer = (AccumulatorStateSerializer<?>) metadata.stateSerializerClass().getConstructor().newInstance();
            return stateSerializer.getSerializedType();
        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
    List<StateField> fields = enumerateFields(clazz, fieldTypes);
    return getSerializedType(fields);
}
Also used : AccumulatorStateMetadata(io.trino.spi.function.AccumulatorStateMetadata) AccumulatorStateSerializer(io.trino.spi.function.AccumulatorStateSerializer) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with AccumulatorStateSerializer

use of io.trino.spi.function.AccumulatorStateSerializer in project trino by trinodb.

the class StateCompiler method generateStateSerializer.

public static <T extends AccumulatorState> AccumulatorStateSerializer<T> generateStateSerializer(Class<T> clazz, Map<String, Type> fieldTypes) {
    AccumulatorStateMetadata metadata = getMetadataAnnotation(clazz);
    if (metadata != null && metadata.stateSerializerClass() != AccumulatorStateSerializer.class) {
        try {
            // noinspection unchecked
            return (AccumulatorStateSerializer<T>) metadata.stateSerializerClass().getConstructor().newInstance();
        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            throw new RuntimeException(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);
    // grouped aggregation state fields use engine classes, so generated class must be able to see both plugin and system classes
    DynamicClassLoader classLoader = new DynamicClassLoader(clazz.getClassLoader(), StateCompiler.class.getClassLoader());
    Class<?> serializerClass = defineClass(definition, AccumulatorStateSerializer.class, callSiteBinder.getBindings(), classLoader);
    try {
        // noinspection unchecked
        return (AccumulatorStateSerializer<T>) serializerClass.getConstructor().newInstance();
    } catch (ReflectiveOperationException e) {
        throw new RuntimeException(e);
    }
}
Also used : DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) AccumulatorStateMetadata(io.trino.spi.function.AccumulatorStateMetadata) AccumulatorStateSerializer(io.trino.spi.function.AccumulatorStateSerializer) ClassDefinition(io.airlift.bytecode.ClassDefinition) InvocationTargetException(java.lang.reflect.InvocationTargetException) CallSiteBinder(io.trino.sql.gen.CallSiteBinder)

Aggregations

AccumulatorStateMetadata (io.trino.spi.function.AccumulatorStateMetadata)2 AccumulatorStateSerializer (io.trino.spi.function.AccumulatorStateSerializer)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ClassDefinition (io.airlift.bytecode.ClassDefinition)1 DynamicClassLoader (io.airlift.bytecode.DynamicClassLoader)1 CallSiteBinder (io.trino.sql.gen.CallSiteBinder)1