Search in sources :

Example 91 with Parameter

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

the class JoinCompiler method generateRowEqualsRowMethod.

private static void generateRowEqualsRowMethod(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, List<Type> joinChannelTypes) {
    Parameter leftPosition = arg("leftPosition", int.class);
    Parameter leftPage = arg("leftPage", Page.class);
    Parameter rightPosition = arg("rightPosition", int.class);
    Parameter rightPage = arg("rightPage", Page.class);
    MethodDefinition rowEqualsRowMethod = classDefinition.declareMethod(a(PUBLIC), "rowEqualsRow", type(boolean.class), leftPosition, leftPage, rightPosition, rightPage);
    for (int index = 0; index < joinChannelTypes.size(); index++) {
        BytecodeExpression type = constantType(callSiteBinder, joinChannelTypes.get(index));
        BytecodeExpression leftBlock = leftPage.invoke("getBlock", Block.class, constantInt(index));
        BytecodeExpression rightBlock = rightPage.invoke("getBlock", Block.class, constantInt(index));
        LabelNode checkNextField = new LabelNode("checkNextField");
        rowEqualsRowMethod.getBody().append(typeEquals(type, leftBlock, leftPosition, rightBlock, rightPosition)).ifTrueGoto(checkNextField).push(false).retBoolean().visitLabel(checkNextField);
    }
    rowEqualsRowMethod.getBody().push(true).retInt();
}
Also used : LabelNode(com.facebook.presto.bytecode.instruction.LabelNode) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) Parameter(com.facebook.presto.bytecode.Parameter) BytecodeExpression(com.facebook.presto.bytecode.expression.BytecodeExpression)

Example 92 with Parameter

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

the class StateCompiler method generateField.

private static void generateField(ClassDefinition definition, MethodDefinition constructor, StateField stateField) {
    FieldDefinition field = definition.declareField(a(PRIVATE), UPPER_CAMEL.to(LOWER_CAMEL, stateField.getName()) + "Value", stateField.getType());
    // Generate getter
    MethodDefinition getter = definition.declareMethod(a(PUBLIC), stateField.getGetterName(), type(stateField.getType()));
    getter.getBody().append(getter.getThis().getField(field).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().setField(field, value)).ret();
    constructor.getBody().append(constructor.getThis().setField(field, stateField.initialValueExpression()));
}
Also used : MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) FieldDefinition(com.facebook.presto.bytecode.FieldDefinition) Parameter(com.facebook.presto.bytecode.Parameter)

Example 93 with Parameter

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

the class StateCompiler method generateSerialize.

private static <T> void generateSerialize(ClassDefinition definition, CallSiteBinder binder, Class<T> clazz, List<StateField> fields) {
    Parameter state = arg("state", Object.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(com.facebook.presto.bytecode.control.IfStatement) Variable(com.facebook.presto.bytecode.Variable) Scope(com.facebook.presto.bytecode.Scope) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) Parameter(com.facebook.presto.bytecode.Parameter) Method(java.lang.reflect.Method) SqlTypeBytecodeExpression(com.facebook.presto.sql.gen.SqlTypeBytecodeExpression) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 94 with Parameter

use of com.facebook.presto.bytecode.Parameter 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)

Aggregations

Parameter (com.facebook.presto.bytecode.Parameter)94 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)91 Variable (com.facebook.presto.bytecode.Variable)73 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)70 Scope (com.facebook.presto.bytecode.Scope)52 IfStatement (com.facebook.presto.bytecode.control.IfStatement)42 BytecodeExpression (com.facebook.presto.bytecode.expression.BytecodeExpression)36 ImmutableList (com.google.common.collect.ImmutableList)24 ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)21 BytecodeNode (com.facebook.presto.bytecode.BytecodeNode)17 ForLoop (com.facebook.presto.bytecode.control.ForLoop)15 Block (com.facebook.presto.spi.block.Block)15 CallSiteBinder (com.facebook.presto.bytecode.CallSiteBinder)13 FieldDefinition (com.facebook.presto.bytecode.FieldDefinition)13 List (java.util.List)13 LabelNode (com.facebook.presto.bytecode.instruction.LabelNode)12 Block (com.facebook.presto.common.block.Block)12 ImmutableMap (com.google.common.collect.ImmutableMap)12 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)11 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)11