Search in sources :

Example 66 with Scope

use of io.airlift.bytecode.Scope in project trino by trinodb.

the class StateCompiler method generateSerialize.

private static <T> void generateSerialize(ClassDefinition definition, CallSiteBinder binder, Class<T> clazz, List<StateField> fields) {
    Parameter state = arg("state", AccumulatorState.class);
    Parameter out = arg("out", BlockBuilder.class);
    MethodDefinition method = definition.declareMethod(a(PUBLIC), "serialize", type(void.class), state, out);
    Scope scope = method.getScope();
    BytecodeBlock serializerBody = method.getBody();
    if (fields.size() == 0) {
        serializerBody.append(out.invoke("appendNull", BlockBuilder.class).pop());
    } else if (fields.size() == 1) {
        Method getter = getGetter(clazz, getOnlyElement(fields));
        SqlTypeBytecodeExpression sqlType = constantType(binder, getOnlyElement(fields).getSqlType());
        Variable fieldValue = scope.declareVariable(getter.getReturnType(), "value");
        serializerBody.append(fieldValue.set(state.cast(getter.getDeclaringClass()).invoke(getter)));
        if (!getOnlyElement(fields).isPrimitiveType()) {
            serializerBody.append(new IfStatement().condition(equal(fieldValue, constantNull(getter.getReturnType()))).ifTrue(out.invoke("appendNull", BlockBuilder.class).pop()).ifFalse(sqlType.writeValue(out, fieldValue)));
        } else {
            // For primitive type, we need to cast here because we serialize byte fields with TINYINT/INTEGER (whose java type is long).
            serializerBody.append(sqlType.writeValue(out, fieldValue.cast(getOnlyElement(fields).getSqlType().getJavaType())));
        }
    } else if (fields.size() > 1) {
        Variable rowBuilder = scope.declareVariable(BlockBuilder.class, "rowBuilder");
        serializerBody.append(rowBuilder.set(out.invoke("beginBlockEntry", BlockBuilder.class)));
        for (StateField field : fields) {
            Method getter = getGetter(clazz, field);
            SqlTypeBytecodeExpression sqlType = constantType(binder, field.getSqlType());
            Variable fieldValue = scope.createTempVariable(getter.getReturnType());
            serializerBody.append(fieldValue.set(state.cast(getter.getDeclaringClass()).invoke(getter)));
            if (!field.isPrimitiveType()) {
                serializerBody.append(new IfStatement().condition(equal(fieldValue, constantNull(getter.getReturnType()))).ifTrue(rowBuilder.invoke("appendNull", BlockBuilder.class).pop()).ifFalse(sqlType.writeValue(rowBuilder, fieldValue)));
            } else {
                // For primitive type, we need to cast here because we serialize byte fields with TINYINT/INTEGER (whose java type is long).
                serializerBody.append(sqlType.writeValue(rowBuilder, fieldValue.cast(field.getSqlType().getJavaType())));
            }
        }
        serializerBody.append(out.invoke("closeEntry", BlockBuilder.class).pop());
    }
    serializerBody.ret();
}
Also used : IfStatement(io.airlift.bytecode.control.IfStatement) Variable(io.airlift.bytecode.Variable) Scope(io.airlift.bytecode.Scope) MethodDefinition(io.airlift.bytecode.MethodDefinition) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) Parameter(io.airlift.bytecode.Parameter) Method(java.lang.reflect.Method) SqlTypeBytecodeExpression(io.trino.sql.gen.SqlTypeBytecodeExpression) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 67 with Scope

use of io.airlift.bytecode.Scope in project trino by trinodb.

the class StateCompiler method generateGroupedField.

private static FieldDefinition generateGroupedField(ClassDefinition definition, MethodDefinition constructor, MethodDefinition ensureCapacity, StateField stateField) {
    Class<?> bigArrayType = getBigArrayType(stateField.getType());
    FieldDefinition field = definition.declareField(a(PRIVATE), UPPER_CAMEL.to(LOWER_CAMEL, stateField.getName()) + "Values", bigArrayType);
    // Generate getter
    MethodDefinition getter = definition.declareMethod(a(PUBLIC), stateField.getGetterName(), type(stateField.getType()));
    getter.getBody().append(getter.getThis().getField(field).invoke("get", stateField.getType(), getter.getThis().invoke("getGroupId", long.class)).ret());
    // Generate setter
    Parameter value = arg("value", stateField.getType());
    MethodDefinition setter = definition.declareMethod(a(PUBLIC), stateField.getSetterName(), type(void.class), value);
    setter.getBody().append(setter.getThis().getField(field).invoke("set", void.class, setter.getThis().invoke("getGroupId", long.class), value)).ret();
    Scope ensureCapacityScope = ensureCapacity.getScope();
    ensureCapacity.getBody().append(ensureCapacity.getThis().getField(field).invoke("ensureCapacity", void.class, ensureCapacityScope.getVariable("size")));
    // Initialize field in constructor
    constructor.getBody().append(constructor.getThis().setField(field, newInstance(field.getType(), stateField.initialValueExpression())));
    return field;
}
Also used : Scope(io.airlift.bytecode.Scope) MethodDefinition(io.airlift.bytecode.MethodDefinition) FieldDefinition(io.airlift.bytecode.FieldDefinition) Parameter(io.airlift.bytecode.Parameter)

Aggregations

Scope (io.airlift.bytecode.Scope)67 Variable (io.airlift.bytecode.Variable)63 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)56 MethodDefinition (io.airlift.bytecode.MethodDefinition)53 Parameter (io.airlift.bytecode.Parameter)49 IfStatement (io.airlift.bytecode.control.IfStatement)44 BytecodeNode (io.airlift.bytecode.BytecodeNode)21 ClassDefinition (io.airlift.bytecode.ClassDefinition)21 ImmutableList (com.google.common.collect.ImmutableList)17 ForLoop (io.airlift.bytecode.control.ForLoop)15 BytecodeExpression (io.airlift.bytecode.expression.BytecodeExpression)14 LabelNode (io.airlift.bytecode.instruction.LabelNode)14 Type (io.prestosql.spi.type.Type)12 Block (io.prestosql.spi.block.Block)11 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)10 CallSiteBinder (io.prestosql.sql.gen.CallSiteBinder)9 Block (io.trino.spi.block.Block)9 VariableInstruction.incrementVariable (io.airlift.bytecode.instruction.VariableInstruction.incrementVariable)8 BlockBuilder (io.prestosql.spi.block.BlockBuilder)8 Type (io.trino.spi.type.Type)8