Search in sources :

Example 76 with IfStatement

use of com.facebook.presto.bytecode.control.IfStatement in project presto by prestodb.

the class JoinCompiler method generateHashPositionMethod.

private static void generateHashPositionMethod(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, List<Type> joinChannelTypes, List<FieldDefinition> joinChannelFields, FieldDefinition hashChannelField) {
    Parameter blockIndex = arg("blockIndex", int.class);
    Parameter blockPosition = arg("blockPosition", int.class);
    MethodDefinition hashPositionMethod = classDefinition.declareMethod(a(PUBLIC), "hashPosition", type(long.class), blockIndex, blockPosition);
    Variable thisVariable = hashPositionMethod.getThis();
    BytecodeExpression hashChannel = thisVariable.getField(hashChannelField);
    BytecodeExpression bigintType = constantType(callSiteBinder, BigintType.BIGINT);
    IfStatement ifStatement = new IfStatement();
    ifStatement.condition(notEqual(hashChannel, constantNull(hashChannelField.getType())));
    ifStatement.ifTrue(bigintType.invoke("getLong", long.class, hashChannel.invoke("get", Object.class, blockIndex).cast(Block.class), blockPosition).ret());
    hashPositionMethod.getBody().append(ifStatement);
    Variable resultVariable = hashPositionMethod.getScope().declareVariable(long.class, "result");
    hashPositionMethod.getBody().push(0L).putVariable(resultVariable);
    for (int index = 0; index < joinChannelTypes.size(); index++) {
        BytecodeExpression type = constantType(callSiteBinder, joinChannelTypes.get(index));
        BytecodeExpression block = hashPositionMethod.getThis().getField(joinChannelFields.get(index)).invoke("get", Object.class, blockIndex).cast(Block.class);
        hashPositionMethod.getBody().getVariable(resultVariable).push(31L).append(OpCode.LMUL).append(typeHashCode(type, block, blockPosition)).append(OpCode.LADD).putVariable(resultVariable);
    }
    hashPositionMethod.getBody().getVariable(resultVariable).retLong();
}
Also used : IfStatement(com.facebook.presto.bytecode.control.IfStatement) Variable(com.facebook.presto.bytecode.Variable) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) Parameter(com.facebook.presto.bytecode.Parameter) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) Block(com.facebook.presto.common.block.Block) BytecodeExpression(com.facebook.presto.bytecode.expression.BytecodeExpression)

Example 77 with IfStatement

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

Aggregations

IfStatement (com.facebook.presto.bytecode.control.IfStatement)77 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)72 Variable (com.facebook.presto.bytecode.Variable)65 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)45 Scope (com.facebook.presto.bytecode.Scope)43 Parameter (com.facebook.presto.bytecode.Parameter)41 BytecodeNode (com.facebook.presto.bytecode.BytecodeNode)23 ForLoop (com.facebook.presto.bytecode.control.ForLoop)19 BytecodeExpression (com.facebook.presto.bytecode.expression.BytecodeExpression)19 LabelNode (com.facebook.presto.bytecode.instruction.LabelNode)19 ImmutableList (com.google.common.collect.ImmutableList)18 Block (com.facebook.presto.spi.block.Block)15 Block (com.facebook.presto.common.block.Block)13 ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)10 Type (com.facebook.presto.common.type.Type)10 DictionaryBlock (com.facebook.presto.spi.block.DictionaryBlock)10 LazyBlock (com.facebook.presto.spi.block.LazyBlock)10 RunLengthEncodedBlock (com.facebook.presto.spi.block.RunLengthEncodedBlock)10 CallSiteBinder (com.facebook.presto.bytecode.CallSiteBinder)9 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)9