Search in sources :

Example 16 with FieldDefinition

use of com.facebook.presto.bytecode.FieldDefinition in project presto by prestodb.

the class StateCompiler method generateSingleStateClass.

private static <T> Class<? extends T> generateSingleStateClass(Class<T> clazz, Map<String, Type> fieldTypes, DynamicClassLoader classLoader) {
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName("Single" + clazz.getSimpleName()), type(Object.class), type(clazz));
    // Store class size in static field
    FieldDefinition classSize = definition.declareField(a(PRIVATE, STATIC, FINAL), "CLASS_SIZE", long.class);
    definition.getClassInitializer().getBody().comment("CLASS_SIZE = ClassLayout.parseClass(%s.class).instanceSize()", definition.getName()).push(definition.getType()).invokeStatic(ClassLayout.class, "parseClass", ClassLayout.class, Class.class).invokeVirtual(ClassLayout.class, "instanceSize", int.class).intToLong().putStaticField(classSize);
    // Add getter for class size
    definition.declareMethod(a(PUBLIC), "getEstimatedSize", type(long.class)).getBody().getStaticField(classSize).retLong();
    // Generate constructor
    MethodDefinition constructor = definition.declareConstructor(a(PUBLIC));
    constructor.getBody().append(constructor.getThis()).invokeConstructor(Object.class);
    // Generate fields
    List<StateField> fields = enumerateFields(clazz, fieldTypes);
    for (StateField field : fields) {
        generateField(definition, constructor, field);
    }
    constructor.getBody().ret();
    return defineClass(definition, clazz, classLoader);
}
Also used : MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) FieldDefinition(com.facebook.presto.bytecode.FieldDefinition) ClassLayout(org.openjdk.jol.info.ClassLayout) BytecodeExpressions.constantClass(com.facebook.presto.bytecode.expression.BytecodeExpressions.constantClass) CompilerUtils.defineClass(com.facebook.presto.bytecode.CompilerUtils.defineClass) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition)

Example 17 with FieldDefinition

use of com.facebook.presto.bytecode.FieldDefinition in project presto by prestodb.

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(com.facebook.presto.bytecode.Scope) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) FieldDefinition(com.facebook.presto.bytecode.FieldDefinition) Parameter(com.facebook.presto.bytecode.Parameter)

Example 18 with FieldDefinition

use of com.facebook.presto.bytecode.FieldDefinition in project presto by prestodb.

the class AccumulatorCompiler method generateAccumulatorClass.

private static <T> Class<? extends T> generateAccumulatorClass(Class<T> accumulatorInterface, AggregationMetadata metadata, DynamicClassLoader classLoader) {
    boolean grouped = accumulatorInterface == GroupedAccumulator.class;
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(metadata.getName() + accumulatorInterface.getSimpleName()), type(Object.class), type(accumulatorInterface));
    CallSiteBinder callSiteBinder = new CallSiteBinder();
    AccumulatorStateSerializer<?> stateSerializer = metadata.getStateSerializer();
    AccumulatorStateFactory<?> stateFactory = metadata.getStateFactory();
    FieldDefinition stateSerializerField = definition.declareField(a(PRIVATE, FINAL), "stateSerializer", AccumulatorStateSerializer.class);
    FieldDefinition stateFactoryField = definition.declareField(a(PRIVATE, FINAL), "stateFactory", AccumulatorStateFactory.class);
    FieldDefinition inputChannelsField = definition.declareField(a(PRIVATE, FINAL), "inputChannels", type(List.class, Integer.class));
    FieldDefinition maskChannelField = definition.declareField(a(PRIVATE, FINAL), "maskChannel", type(Optional.class, Integer.class));
    FieldDefinition stateField = definition.declareField(a(PRIVATE, FINAL), "state", grouped ? stateFactory.getGroupedStateClass() : stateFactory.getSingleStateClass());
    // Generate constructor
    generateConstructor(definition, stateSerializerField, stateFactoryField, inputChannelsField, maskChannelField, stateField, grouped);
    // Generate methods
    generateAddInput(definition, stateField, inputChannelsField, maskChannelField, metadata.getInputMetadata(), metadata.getInputFunction(), callSiteBinder, grouped);
    generateGetEstimatedSize(definition, stateField);
    generateGetIntermediateType(definition, callSiteBinder, stateSerializer.getSerializedType());
    generateGetFinalType(definition, callSiteBinder, metadata.getOutputType());
    generateAddIntermediateAsCombine(definition, stateField, stateSerializerField, stateFactoryField, metadata.getCombineFunction(), stateFactory.getSingleStateClass(), callSiteBinder, grouped);
    if (grouped) {
        generateGroupedEvaluateIntermediate(definition, stateSerializerField, stateField);
    } else {
        generateEvaluateIntermediate(definition, stateSerializerField, stateField);
    }
    if (grouped) {
        generateGroupedEvaluateFinal(definition, stateField, metadata.getOutputFunction(), callSiteBinder);
    } else {
        generateEvaluateFinal(definition, stateField, metadata.getOutputFunction(), callSiteBinder);
    }
    return defineClass(definition, accumulatorInterface, callSiteBinder.getBindings(), classLoader);
}
Also used : Optional(java.util.Optional) CallSiteBinder(com.facebook.presto.sql.gen.CallSiteBinder) FieldDefinition(com.facebook.presto.bytecode.FieldDefinition) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition)

Aggregations

FieldDefinition (com.facebook.presto.bytecode.FieldDefinition)18 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)11 Parameter (com.facebook.presto.bytecode.Parameter)7 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)5 ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)5 Variable (com.facebook.presto.bytecode.Variable)4 Block (com.facebook.presto.spi.block.Block)4 ArrayList (java.util.ArrayList)4 BytecodeNode (com.facebook.presto.bytecode.BytecodeNode)3 CallExpression (com.facebook.presto.sql.relational.CallExpression)3 LambdaDefinitionExpression (com.facebook.presto.sql.relational.LambdaDefinitionExpression)3 RowExpression (com.facebook.presto.sql.relational.RowExpression)3 VerifyException (com.google.common.base.VerifyException)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Scope (com.facebook.presto.bytecode.Scope)2 BytecodeExpressions.constantClass (com.facebook.presto.bytecode.expression.BytecodeExpressions.constantClass)2 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 CompilerUtils.defineClass (com.facebook.presto.bytecode.CompilerUtils.defineClass)1 IfStatement (com.facebook.presto.bytecode.control.IfStatement)1