Search in sources :

Example 41 with BytecodeBlock

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

the class RowToRowCast method generateRowCast.

private static Class<?> generateRowCast(Type fromType, Type toType, FunctionRegistry functionRegistry) {
    List<Type> toTypes = toType.getTypeParameters();
    List<Type> fromTypes = fromType.getTypeParameters();
    CallSiteBinder binder = new CallSiteBinder();
    // Embed the MD5 hash code of input and output types into the generated class name instead of the raw type names,
    // which could prevent the class name from hitting the length limitation and invalid characters.
    byte[] md5Suffix = Hashing.md5().hashBytes((fromType + "$" + toType).getBytes()).asBytes();
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), CompilerUtils.makeClassName(Joiner.on("$").join("RowCast", BaseEncoding.base16().encode(md5Suffix))), type(Object.class));
    Parameter session = arg("session", ConnectorSession.class);
    Parameter value = arg("value", Block.class);
    MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "castRow", type(Block.class), session, value);
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    Variable wasNull = scope.declareVariable(boolean.class, "wasNull");
    Variable blockBuilder = scope.createTempVariable(BlockBuilder.class);
    body.append(wasNull.set(constantBoolean(false)));
    CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(definition, binder);
    // create the interleave block builder
    body.newObject(InterleavedBlockBuilder.class).dup().append(constantType(binder, toType).invoke("getTypeParameters", List.class)).append(newInstance(BlockBuilderStatus.class)).append(constantInt(toTypes.size())).invokeConstructor(InterleavedBlockBuilder.class, List.class, BlockBuilderStatus.class, int.class).putVariable(blockBuilder);
    // loop through to append member blocks
    for (int i = 0; i < toTypes.size(); i++) {
        Signature signature = internalOperator(CAST.name(), toTypes.get(i).getTypeSignature(), ImmutableList.of(fromTypes.get(i).getTypeSignature()));
        ScalarFunctionImplementation function = functionRegistry.getScalarFunctionImplementation(signature);
        Type currentFromType = fromTypes.get(i);
        if (currentFromType.equals(UNKNOWN)) {
            body.append(blockBuilder.invoke("appendNull", BlockBuilder.class).pop());
            continue;
        }
        BytecodeExpression fromElement = constantType(binder, currentFromType).getValue(value, constantInt(i));
        BytecodeExpression toElement = invokeFunction(scope, cachedInstanceBinder, signature.getName(), function, fromElement);
        IfStatement ifElementNull = new IfStatement("if the element in the row type is null...");
        ifElementNull.condition(value.invoke("isNull", boolean.class, constantInt(i))).ifTrue(blockBuilder.invoke("appendNull", BlockBuilder.class).pop()).ifFalse(constantType(binder, toTypes.get(i)).writeValue(blockBuilder, toElement));
        body.append(ifElementNull);
    }
    // call blockBuilder.build()
    body.append(blockBuilder.invoke("build", Block.class)).retObject();
    // create constructor
    MethodDefinition constructorDefinition = definition.declareConstructor(a(PUBLIC));
    BytecodeBlock constructorBody = constructorDefinition.getBody();
    Variable thisVariable = constructorDefinition.getThis();
    constructorBody.comment("super();").append(thisVariable).invokeConstructor(Object.class);
    cachedInstanceBinder.generateInitializations(thisVariable, constructorBody);
    constructorBody.ret();
    return defineClass(definition, Object.class, binder.getBindings(), RowToRowCast.class.getClassLoader());
}
Also used : Variable(com.facebook.presto.bytecode.Variable) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) IfStatement(com.facebook.presto.bytecode.control.IfStatement) Type(com.facebook.presto.spi.type.Type) SqlTypeBytecodeExpression.constantType(com.facebook.presto.sql.gen.SqlTypeBytecodeExpression.constantType) CachedInstanceBinder(com.facebook.presto.sql.gen.CachedInstanceBinder) Scope(com.facebook.presto.bytecode.Scope) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) CallSiteBinder(com.facebook.presto.sql.gen.CallSiteBinder) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) Parameter(com.facebook.presto.bytecode.Parameter) Block(com.facebook.presto.spi.block.Block) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) BytecodeExpression(com.facebook.presto.bytecode.expression.BytecodeExpression) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder)

Example 42 with BytecodeBlock

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

the class IfStatement method accept.

@Override
public void accept(MethodVisitor visitor, MethodGenerationContext generationContext) {
    checkState(!condition.isEmpty(), "IfStatement does not have a condition set");
    checkState(!ifTrue.isEmpty() || !ifFalse.isEmpty(), "IfStatement does not have a true or false block set");
    BytecodeBlock block = new BytecodeBlock();
    // if !condition goto false;
    block.append(new BytecodeBlock().setDescription("condition").append(condition));
    block.ifFalseGoto(falseLabel);
    if (!ifTrue.isEmpty()) {
        block.append(new BytecodeBlock().setDescription("ifTrue").append(ifTrue));
    }
    if (!ifFalse.isEmpty()) {
        // close true case by skipping to end
        block.gotoLabel(outLabel);
        block.visitLabel(falseLabel);
        block.append(new BytecodeBlock().setDescription("ifFalse").append(ifFalse));
        block.visitLabel(outLabel);
    } else {
        block.visitLabel(falseLabel);
    }
    block.accept(visitor, generationContext);
}
Also used : BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock)

Example 43 with BytecodeBlock

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

the class TryCatch method accept.

@Override
public void accept(MethodVisitor visitor, MethodGenerationContext generationContext) {
    LabelNode tryStart = new LabelNode("tryStart");
    LabelNode tryEnd = new LabelNode("tryEnd");
    LabelNode handler = new LabelNode("handler");
    LabelNode done = new LabelNode("done");
    BytecodeBlock block = new BytecodeBlock();
    // try block
    block.visitLabel(tryStart).append(tryNode).visitLabel(tryEnd).gotoLabel(done);
    // handler block
    block.visitLabel(handler).append(catchNode);
    // all done
    block.visitLabel(done);
    block.accept(visitor, generationContext);
    visitor.visitTryCatchBlock(tryStart.getLabel(), tryEnd.getLabel(), handler.getLabel(), exceptionName);
}
Also used : LabelNode(com.facebook.presto.bytecode.instruction.LabelNode) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock)

Example 44 with BytecodeBlock

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

the class WhileLoop method accept.

@Override
public void accept(MethodVisitor visitor, MethodGenerationContext generationContext) {
    checkState(!condition.isEmpty(), "WhileLoop does not have a condition set");
    BytecodeBlock block = new BytecodeBlock().visitLabel(continueLabel).append(condition).ifZeroGoto(endLabel).append(body).gotoLabel(continueLabel).visitLabel(endLabel);
    block.accept(visitor, generationContext);
}
Also used : BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock)

Example 45 with BytecodeBlock

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

the class AndBytecodeExpression method getBytecode.

@Override
public BytecodeNode getBytecode(MethodGenerationContext generationContext) {
    LabelNode falseLabel = new LabelNode("false");
    LabelNode endLabel = new LabelNode("end");
    return new BytecodeBlock().append(left).ifFalseGoto(falseLabel).append(right).ifFalseGoto(falseLabel).push(true).gotoLabel(endLabel).visitLabel(falseLabel).push(false).visitLabel(endLabel);
}
Also used : LabelNode(com.facebook.presto.bytecode.instruction.LabelNode) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock)

Aggregations

BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)82 Variable (com.facebook.presto.bytecode.Variable)57 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)43 IfStatement (com.facebook.presto.bytecode.control.IfStatement)42 Parameter (com.facebook.presto.bytecode.Parameter)38 Scope (com.facebook.presto.bytecode.Scope)33 Block (com.facebook.presto.spi.block.Block)23 BytecodeExpression (com.facebook.presto.bytecode.expression.BytecodeExpression)18 LabelNode (com.facebook.presto.bytecode.instruction.LabelNode)18 BytecodeNode (com.facebook.presto.bytecode.BytecodeNode)17 ImmutableList (com.google.common.collect.ImmutableList)16 ForLoop (com.facebook.presto.bytecode.control.ForLoop)12 Type (com.facebook.presto.spi.type.Type)12 ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)11 DictionaryBlock (com.facebook.presto.spi.block.DictionaryBlock)11 LazyBlock (com.facebook.presto.spi.block.LazyBlock)11 RunLengthEncodedBlock (com.facebook.presto.spi.block.RunLengthEncodedBlock)11 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)10 List (java.util.List)9 RowExpression (com.facebook.presto.sql.relational.RowExpression)7