Search in sources :

Example 11 with CallSiteBinder

use of io.trino.sql.gen.CallSiteBinder 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

ClassDefinition (io.airlift.bytecode.ClassDefinition)11 CallSiteBinder (io.trino.sql.gen.CallSiteBinder)11 MethodDefinition (io.airlift.bytecode.MethodDefinition)8 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)7 Parameter (io.airlift.bytecode.Parameter)7 Scope (io.airlift.bytecode.Scope)7 Variable (io.airlift.bytecode.Variable)7 IfStatement (io.airlift.bytecode.control.IfStatement)6 Block (io.trino.spi.block.Block)6 Signature.typeVariable (io.trino.metadata.Signature.typeVariable)5 BytecodeNode (io.airlift.bytecode.BytecodeNode)4 DynamicClassLoader (io.airlift.bytecode.DynamicClassLoader)4 ForLoop (io.airlift.bytecode.control.ForLoop)4 VariableInstruction.incrementVariable (io.airlift.bytecode.instruction.VariableInstruction.incrementVariable)4 ImmutableList (com.google.common.collect.ImmutableList)3 BytecodeExpression (io.airlift.bytecode.expression.BytecodeExpression)3 BlockBuilder (io.trino.spi.block.BlockBuilder)3 Type (io.trino.spi.type.Type)3 SqlTypeBytecodeExpression (io.trino.sql.gen.SqlTypeBytecodeExpression)3 MethodHandle (java.lang.invoke.MethodHandle)3