Search in sources :

Example 46 with FieldDefinition

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

the class JoinFilterFunctionCompiler method generateMethods.

private void generateMethods(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, RowExpression filter, int leftBlocksSize) {
    CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(classDefinition, callSiteBinder);
    FieldDefinition sessionField = classDefinition.declareField(a(PRIVATE, FINAL), "session", ConnectorSession.class);
    Map<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap = generateMethodsForLambda(classDefinition, callSiteBinder, cachedInstanceBinder, filter);
    generateFilterMethod(classDefinition, callSiteBinder, cachedInstanceBinder, compiledLambdaMap, filter, leftBlocksSize, sessionField);
    generateConstructor(classDefinition, sessionField, cachedInstanceBinder);
}
Also used : CompiledLambda(io.trino.sql.gen.LambdaBytecodeGenerator.CompiledLambda) FieldDefinition(io.airlift.bytecode.FieldDefinition) LambdaDefinitionExpression(io.trino.sql.relational.LambdaDefinitionExpression)

Example 47 with FieldDefinition

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

the class LambdaBytecodeGenerator method compileLambdaProvider.

public static Class<? extends Supplier<Object>> compileLambdaProvider(LambdaDefinitionExpression lambdaExpression, FunctionManager functionManager, Class<?> lambdaInterface) {
    ClassDefinition lambdaProviderClassDefinition = new ClassDefinition(a(PUBLIC, Access.FINAL), makeClassName("LambdaProvider"), type(Object.class), type(Supplier.class, Object.class));
    FieldDefinition sessionField = lambdaProviderClassDefinition.declareField(a(PRIVATE), "session", ConnectorSession.class);
    CallSiteBinder callSiteBinder = new CallSiteBinder();
    CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(lambdaProviderClassDefinition, callSiteBinder);
    Map<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap = generateMethodsForLambda(lambdaProviderClassDefinition, callSiteBinder, cachedInstanceBinder, lambdaExpression, functionManager);
    MethodDefinition method = lambdaProviderClassDefinition.declareMethod(a(PUBLIC), "get", type(Object.class), ImmutableList.of());
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    scope.declareVariable("wasNull", body, constantFalse());
    scope.declareVariable("session", body, method.getThis().getField(sessionField));
    RowExpressionCompiler rowExpressionCompiler = new RowExpressionCompiler(callSiteBinder, cachedInstanceBinder, variableReferenceCompiler(ImmutableMap.of()), functionManager, compiledLambdaMap);
    BytecodeGeneratorContext generatorContext = new BytecodeGeneratorContext(rowExpressionCompiler, scope, callSiteBinder, cachedInstanceBinder, functionManager);
    body.append(generateLambda(generatorContext, ImmutableList.of(), compiledLambdaMap.get(lambdaExpression), lambdaInterface)).retObject();
    // constructor
    Parameter sessionParameter = arg("session", ConnectorSession.class);
    MethodDefinition constructorDefinition = lambdaProviderClassDefinition.declareConstructor(a(PUBLIC), sessionParameter);
    BytecodeBlock constructorBody = constructorDefinition.getBody();
    Variable constructorThisVariable = constructorDefinition.getThis();
    constructorBody.comment("super();").append(constructorThisVariable).invokeConstructor(Object.class).append(constructorThisVariable.setField(sessionField, sessionParameter));
    cachedInstanceBinder.generateInitializations(constructorThisVariable, constructorBody);
    constructorBody.ret();
    // noinspection unchecked
    return (Class<? extends Supplier<Object>>) defineClass(lambdaProviderClassDefinition, Supplier.class, callSiteBinder.getBindings(), AccumulatorCompiler.class.getClassLoader());
}
Also used : Variable(io.airlift.bytecode.Variable) FieldDefinition(io.airlift.bytecode.FieldDefinition) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) ClassDefinition(io.airlift.bytecode.ClassDefinition) Scope(io.airlift.bytecode.Scope) MethodDefinition(io.airlift.bytecode.MethodDefinition) Parameter(io.airlift.bytecode.Parameter) Supplier(java.util.function.Supplier) CompilerUtils.defineClass(io.trino.util.CompilerUtils.defineClass) LambdaDefinitionExpression(io.trino.sql.relational.LambdaDefinitionExpression)

Example 48 with FieldDefinition

use of io.airlift.bytecode.FieldDefinition 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)

Example 49 with FieldDefinition

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

the class StateCompiler method generateCopy.

private static void generateCopy(ClassDefinition definition, List<StateField> fields, List<FieldDefinition> fieldDefinitions) {
    MethodDefinition copy = definition.declareMethod(a(PUBLIC), "copy", type(AccumulatorState.class));
    Variable thisVariable = copy.getThis();
    BytecodeBlock body = copy.getBody();
    List<BytecodeExpression> fieldCopyExpressions = new ArrayList<>();
    for (int i = 0; i < fields.size(); i++) {
        Optional<BytecodeExpression> fieldCopy = copyField(thisVariable, fieldDefinitions.get(i), fields.get(i).getType());
        if (fieldCopy.isEmpty()) {
            body.append(newInstance(UnsupportedOperationException.class, constantString(format("copy not supported for %s (cannot copy field of type %s)", definition.getName(), fields.get(i).getType())))).throwObject();
            return;
        }
        fieldCopyExpressions.add(fieldCopy.get());
    }
    Variable instanceCopy = copy.getScope().declareVariable(definition.getType(), "instanceCopy");
    body.append(instanceCopy.set(newInstance(definition.getType())));
    for (int i = 0; i < fieldDefinitions.size(); i++) {
        FieldDefinition fieldDefinition = fieldDefinitions.get(i);
        Class<?> type = fields.get(i).getType();
        if (type == long.class || type == double.class || type == boolean.class || type == byte.class || type == int.class) {
            body.append(instanceCopy.setField(fieldDefinition, fieldCopyExpressions.get(i)));
        } else {
            body.append(new IfStatement("if field value is null").condition(isNull(thisVariable.getField(fieldDefinition))).ifTrue(instanceCopy.setField(fieldDefinition, thisVariable.getField(fieldDefinition))).ifFalse(instanceCopy.setField(fieldDefinition, fieldCopyExpressions.get(i))));
        }
    }
    copy.getBody().append(instanceCopy.ret());
}
Also used : IfStatement(io.airlift.bytecode.control.IfStatement) AccumulatorState(io.trino.spi.function.AccumulatorState) Variable(io.airlift.bytecode.Variable) MethodDefinition(io.airlift.bytecode.MethodDefinition) FieldDefinition(io.airlift.bytecode.FieldDefinition) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) ArrayList(java.util.ArrayList) BytecodeExpression(io.airlift.bytecode.expression.BytecodeExpression) SqlTypeBytecodeExpression(io.trino.sql.gen.SqlTypeBytecodeExpression)

Example 50 with FieldDefinition

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

the class StateCompiler method generateInstanceSize.

private static FieldDefinition generateInstanceSize(ClassDefinition definition) {
    // Store instance size in static field
    FieldDefinition instanceSize = definition.declareField(a(PRIVATE, STATIC, FINAL), "INSTANCE_SIZE", long.class);
    definition.getClassInitializer().getBody().comment("INSTANCE_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(instanceSize);
    return instanceSize;
}
Also used : FieldDefinition(io.airlift.bytecode.FieldDefinition) ClassLayout(org.openjdk.jol.info.ClassLayout) BytecodeExpressions.constantClass(io.airlift.bytecode.expression.BytecodeExpressions.constantClass) CompilerUtils.defineClass(io.trino.util.CompilerUtils.defineClass)

Aggregations

FieldDefinition (io.airlift.bytecode.FieldDefinition)50 MethodDefinition (io.airlift.bytecode.MethodDefinition)27 Variable (io.airlift.bytecode.Variable)25 Parameter (io.airlift.bytecode.Parameter)20 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)19 ArrayList (java.util.ArrayList)17 ClassDefinition (io.airlift.bytecode.ClassDefinition)15 BytecodeExpression (io.airlift.bytecode.expression.BytecodeExpression)14 Scope (io.airlift.bytecode.Scope)6 IfStatement (io.airlift.bytecode.control.IfStatement)5 ImmutableList (com.google.common.collect.ImmutableList)4 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)4 LambdaDefinitionExpression (io.prestosql.spi.relation.LambdaDefinitionExpression)4 AccumulatorState (io.trino.spi.function.AccumulatorState)4 LambdaDefinitionExpression (io.trino.sql.relational.LambdaDefinitionExpression)4 List (java.util.List)4 CompiledLambda (io.prestosql.sql.gen.LambdaBytecodeGenerator.CompiledLambda)3 GroupedAccumulatorState (io.trino.spi.function.GroupedAccumulatorState)3 CompiledLambda (io.trino.sql.gen.LambdaBytecodeGenerator.CompiledLambda)3 CompilerUtils.defineClass (io.trino.util.CompilerUtils.defineClass)3