Search in sources :

Example 1 with CachedInstanceBinder

use of io.trino.sql.gen.CachedInstanceBinder in project trino by trinodb.

the class RowToRowCast method generateRowCast.

private static Class<?> generateRowCast(Type fromType, Type toType, FunctionDependencies functionDependencies) {
    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(UTF_8)).asBytes();
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(Joiner.on("$").join("RowCast", BaseEncoding.base16().encode(md5Suffix))), type(Object.class));
    Parameter session = arg("session", ConnectorSession.class);
    Parameter row = arg("row", Block.class);
    MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "castRow", type(Block.class), session, row);
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    Variable wasNull = scope.declareVariable(boolean.class, "wasNull");
    Variable blockBuilder = scope.createTempVariable(BlockBuilder.class);
    Variable singleRowBlockWriter = scope.createTempVariable(BlockBuilder.class);
    body.append(wasNull.set(constantBoolean(false)));
    CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(definition, binder);
    // create the row block builder
    body.append(blockBuilder.set(constantType(binder, toType).invoke("createBlockBuilder", BlockBuilder.class, constantNull(BlockBuilderStatus.class), constantInt(1))));
    body.append(singleRowBlockWriter.set(blockBuilder.invoke("beginBlockEntry", BlockBuilder.class)));
    // loop through to append member blocks
    for (int i = 0; i < toTypes.size(); i++) {
        Type fromElementType = fromTypes.get(i);
        Type toElementType = toTypes.get(i);
        Type currentFromType = fromElementType;
        if (currentFromType.equals(UNKNOWN)) {
            body.append(singleRowBlockWriter.invoke("appendNull", BlockBuilder.class).pop());
            continue;
        }
        MethodHandle castMethod = getNullSafeCast(functionDependencies, fromElementType, toElementType);
        MethodHandle writeMethod = getNullSafeWrite(toElementType);
        MethodHandle castAndWrite = collectArguments(writeMethod, 1, castMethod);
        body.append(invokeDynamic(BOOTSTRAP_METHOD, ImmutableList.of(binder.bind(castAndWrite).getBindingId()), "castAndWriteField", castAndWrite.type(), singleRowBlockWriter, scope.getVariable("session"), row, constantInt(i)));
    }
    // call blockBuilder.closeEntry() and return the single row block
    body.append(blockBuilder.invoke("closeEntry", BlockBuilder.class).pop());
    body.append(constantType(binder, toType).invoke("getObject", Object.class, blockBuilder.cast(Block.class), constantInt(0)).cast(Block.class).ret());
    // 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(io.airlift.bytecode.Variable) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) ClassDefinition(io.airlift.bytecode.ClassDefinition) TypeVariableConstraint(io.trino.metadata.TypeVariableConstraint) SqlTypeBytecodeExpression.constantType(io.trino.sql.gen.SqlTypeBytecodeExpression.constantType) Type(io.trino.spi.type.Type) MethodType.methodType(java.lang.invoke.MethodType.methodType) CachedInstanceBinder(io.trino.sql.gen.CachedInstanceBinder) Scope(io.airlift.bytecode.Scope) MethodDefinition(io.airlift.bytecode.MethodDefinition) CallSiteBinder(io.trino.sql.gen.CallSiteBinder) Parameter(io.airlift.bytecode.Parameter) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) Block(io.trino.spi.block.Block) BlockBuilderStatus(io.trino.spi.block.BlockBuilderStatus) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

BytecodeBlock (io.airlift.bytecode.BytecodeBlock)1 ClassDefinition (io.airlift.bytecode.ClassDefinition)1 MethodDefinition (io.airlift.bytecode.MethodDefinition)1 Parameter (io.airlift.bytecode.Parameter)1 Scope (io.airlift.bytecode.Scope)1 Variable (io.airlift.bytecode.Variable)1 TypeVariableConstraint (io.trino.metadata.TypeVariableConstraint)1 Block (io.trino.spi.block.Block)1 BlockBuilderStatus (io.trino.spi.block.BlockBuilderStatus)1 Type (io.trino.spi.type.Type)1 CachedInstanceBinder (io.trino.sql.gen.CachedInstanceBinder)1 CallSiteBinder (io.trino.sql.gen.CallSiteBinder)1 SqlTypeBytecodeExpression.constantType (io.trino.sql.gen.SqlTypeBytecodeExpression.constantType)1 MethodHandle (java.lang.invoke.MethodHandle)1 MethodType.methodType (java.lang.invoke.MethodType.methodType)1