Search in sources :

Example 1 with FieldDefinition

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

the class StateCompiler method generateGroupedStateClass.

private static <T> Class<? extends T> generateGroupedStateClass(Class<T> clazz, Map<String, Type> fieldTypes, DynamicClassLoader classLoader) {
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName("Grouped" + clazz.getSimpleName()), type(AbstractGroupedAccumulatorState.class), type(clazz), type(GroupedAccumulator.class));
    List<StateField> fields = enumerateFields(clazz, fieldTypes);
    // Create constructor
    MethodDefinition constructor = definition.declareConstructor(a(PUBLIC));
    constructor.getBody().append(constructor.getThis()).invokeConstructor(AbstractGroupedAccumulatorState.class);
    // Create ensureCapacity
    MethodDefinition ensureCapacity = definition.declareMethod(a(PUBLIC), "ensureCapacity", type(void.class), arg("size", long.class));
    // Generate fields, constructor, and ensureCapacity
    List<FieldDefinition> fieldDefinitions = new ArrayList<>();
    for (StateField field : fields) {
        fieldDefinitions.add(generateGroupedField(definition, constructor, ensureCapacity, field));
    }
    constructor.getBody().ret();
    ensureCapacity.getBody().ret();
    // Generate getEstimatedSize
    MethodDefinition getEstimatedSize = definition.declareMethod(a(PUBLIC), "getEstimatedSize", type(long.class));
    BytecodeBlock body = getEstimatedSize.getBody();
    Variable size = getEstimatedSize.getScope().declareVariable(long.class, "size");
    // initialize size to 0L
    body.append(size.set(constantLong(0)));
    // add field to size
    for (FieldDefinition field : fieldDefinitions) {
        body.append(size.set(add(size, getEstimatedSize.getThis().getField(field).invoke("sizeOf", long.class))));
    }
    // return size
    body.append(size.ret());
    return defineClass(definition, clazz, classLoader);
}
Also used : Variable(com.facebook.presto.bytecode.Variable) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) FieldDefinition(com.facebook.presto.bytecode.FieldDefinition) ArrayList(java.util.ArrayList) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) GroupedAccumulator(com.facebook.presto.operator.aggregation.GroupedAccumulator)

Example 2 with FieldDefinition

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

the class InvokeFunctionBytecodeExpression method invokeFunction.

public static BytecodeExpression invokeFunction(Scope scope, CachedInstanceBinder cachedInstanceBinder, String name, ScalarFunctionImplementation function, List<BytecodeExpression> parameters) {
    requireNonNull(scope, "scope is null");
    requireNonNull(function, "function is null");
    Binding binding = cachedInstanceBinder.getCallSiteBinder().bind(function.getMethodHandle());
    Optional<BytecodeNode> instance = Optional.empty();
    if (function.getInstanceFactory().isPresent()) {
        FieldDefinition field = cachedInstanceBinder.getCachedInstance(function.getInstanceFactory().get());
        instance = Optional.of(scope.getThis().getField(field));
    }
    return new InvokeFunctionBytecodeExpression(scope, binding, name, function, instance, parameters);
}
Also used : FieldDefinition(com.facebook.presto.bytecode.FieldDefinition) BytecodeNode(com.facebook.presto.bytecode.BytecodeNode)

Example 3 with FieldDefinition

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

the class JoinFilterFunctionCompiler method generateMethodsForLambdaAndTry.

private PreGeneratedExpressions generateMethodsForLambdaAndTry(ClassDefinition containerClassDefinition, CallSiteBinder callSiteBinder, CachedInstanceBinder cachedInstanceBinder, int leftBlocksSize, RowExpression filter) {
    Set<RowExpression> lambdaAndTryExpressions = ImmutableSet.copyOf(extractLambdaAndTryExpressions(filter));
    ImmutableMap.Builder<CallExpression, MethodDefinition> tryMethodMap = ImmutableMap.builder();
    ImmutableMap.Builder<LambdaDefinitionExpression, FieldDefinition> lambdaFieldMap = ImmutableMap.builder();
    int counter = 0;
    for (RowExpression expression : lambdaAndTryExpressions) {
        if (expression instanceof CallExpression) {
            CallExpression tryExpression = (CallExpression) expression;
            verify(!Signatures.TRY.equals(tryExpression.getSignature().getName()));
            Parameter session = arg("session", ConnectorSession.class);
            Parameter leftPosition = arg("leftPosition", int.class);
            Parameter leftBlocks = arg("leftBlocks", Block[].class);
            Parameter rightPosition = arg("rightPosition", int.class);
            Parameter rightBlocks = arg("rightBlocks", Block[].class);
            BytecodeExpressionVisitor innerExpressionVisitor = new BytecodeExpressionVisitor(callSiteBinder, cachedInstanceBinder, fieldReferenceCompiler(callSiteBinder, leftPosition, leftBlocks, rightPosition, rightBlocks, leftBlocksSize), metadata.getFunctionRegistry(), new PreGeneratedExpressions(tryMethodMap.build(), lambdaFieldMap.build()));
            List<Parameter> inputParameters = ImmutableList.<Parameter>builder().add(session).add(leftPosition).add(leftBlocks).add(rightPosition).add(rightBlocks).build();
            MethodDefinition tryMethod = defineTryMethod(innerExpressionVisitor, containerClassDefinition, "try_" + counter, inputParameters, Primitives.wrap(tryExpression.getType().getJavaType()), tryExpression, callSiteBinder);
            tryMethodMap.put(tryExpression, tryMethod);
        } else if (expression instanceof LambdaDefinitionExpression) {
            LambdaDefinitionExpression lambdaExpression = (LambdaDefinitionExpression) expression;
            PreGeneratedExpressions preGeneratedExpressions = new PreGeneratedExpressions(tryMethodMap.build(), lambdaFieldMap.build());
            FieldDefinition methodHandleField = LambdaBytecodeGenerator.preGenerateLambdaExpression(lambdaExpression, "lambda_" + counter, containerClassDefinition, preGeneratedExpressions, callSiteBinder, cachedInstanceBinder, metadata.getFunctionRegistry());
            lambdaFieldMap.put(lambdaExpression, methodHandleField);
        } else {
            throw new VerifyException(format("unexpected expression: %s", expression.toString()));
        }
        counter++;
    }
    return new PreGeneratedExpressions(tryMethodMap.build(), lambdaFieldMap.build());
}
Also used : FieldDefinition(com.facebook.presto.bytecode.FieldDefinition) RowExpression(com.facebook.presto.sql.relational.RowExpression) ImmutableMap(com.google.common.collect.ImmutableMap) VerifyException(com.google.common.base.VerifyException) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) Parameter(com.facebook.presto.bytecode.Parameter) Block(com.facebook.presto.spi.block.Block) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) CallExpression(com.facebook.presto.sql.relational.CallExpression) LambdaDefinitionExpression(com.facebook.presto.sql.relational.LambdaDefinitionExpression)

Example 4 with FieldDefinition

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

the class JoinProbeCompiler method generateCurrentRowContainsNull.

private static void generateCurrentRowContainsNull(ClassDefinition classDefinition, List<FieldDefinition> probeBlockFields, FieldDefinition positionField) {
    MethodDefinition method = classDefinition.declareMethod(a(PRIVATE), "currentRowContainsNull", type(boolean.class));
    Variable thisVariable = method.getThis();
    for (FieldDefinition probeBlockField : probeBlockFields) {
        LabelNode checkNextField = new LabelNode("checkNextField");
        method.getBody().append(thisVariable.getField(probeBlockField).invoke("isNull", boolean.class, thisVariable.getField(positionField))).ifFalseGoto(checkNextField).push(true).retBoolean().visitLabel(checkNextField);
    }
    method.getBody().push(false).retInt();
}
Also used : LabelNode(com.facebook.presto.bytecode.instruction.LabelNode) Variable(com.facebook.presto.bytecode.Variable) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) FieldDefinition(com.facebook.presto.bytecode.FieldDefinition)

Example 5 with FieldDefinition

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

the class CachedInstanceBinder method getCachedInstance.

public FieldDefinition getCachedInstance(MethodHandle methodHandle) {
    FieldDefinition field = classDefinition.declareField(a(PRIVATE, FINAL), "__cachedInstance" + nextId, methodHandle.type().returnType());
    initializers.put(field, methodHandle);
    nextId++;
    return field;
}
Also used : FieldDefinition(com.facebook.presto.bytecode.FieldDefinition)

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