Search in sources :

Example 1 with ArrayMapBytecodeExpression

use of io.prestosql.sql.gen.ArrayMapBytecodeExpression in project hetu-core by openlookeng.

the class ArrayToArrayCast method generateArrayCast.

private static Class<?> generateArrayCast(TypeManager typeManager, FunctionMetadata elementCastFunctionMetadata, BuiltInScalarFunctionImplementation elementCast) {
    CallSiteBinder binder = new CallSiteBinder();
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(Joiner.on("$").join("ArrayCast", elementCastFunctionMetadata.getArgumentTypes().get(0), elementCastFunctionMetadata.getReturnType())), type(Object.class));
    Parameter session = arg("session", ConnectorSession.class);
    Parameter value = arg("value", Block.class);
    MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "castArray", type(Block.class), session, value);
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    Variable wasNull = scope.declareVariable(boolean.class, "wasNull");
    body.append(wasNull.set(constantBoolean(false)));
    // cast map elements
    Type fromElementType = typeManager.getType(elementCastFunctionMetadata.getArgumentTypes().get(0));
    Type toElementType = typeManager.getType(elementCastFunctionMetadata.getReturnType());
    CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(definition, binder);
    ArrayMapBytecodeExpression newArray = ArrayGeneratorUtils.map(scope, cachedInstanceBinder, fromElementType, toElementType, value, elementCastFunctionMetadata.getName().getObjectName(), elementCast);
    // return the block
    body.append(newArray.ret());
    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(), ArrayToArrayCast.class.getClassLoader());
}
Also used : ArrayMapBytecodeExpression(io.prestosql.sql.gen.ArrayMapBytecodeExpression) Variable(io.airlift.bytecode.Variable) Signature.typeVariable(io.prestosql.spi.function.Signature.typeVariable) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) ClassDefinition(io.airlift.bytecode.ClassDefinition) Type(io.prestosql.spi.type.Type) CastType(io.prestosql.metadata.CastType) CachedInstanceBinder(io.prestosql.sql.gen.CachedInstanceBinder) Scope(io.airlift.bytecode.Scope) MethodDefinition(io.airlift.bytecode.MethodDefinition) CallSiteBinder(io.prestosql.sql.gen.CallSiteBinder) Parameter(io.airlift.bytecode.Parameter) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) Block(io.prestosql.spi.block.Block)

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 CastType (io.prestosql.metadata.CastType)1 Block (io.prestosql.spi.block.Block)1 Signature.typeVariable (io.prestosql.spi.function.Signature.typeVariable)1 Type (io.prestosql.spi.type.Type)1 ArrayMapBytecodeExpression (io.prestosql.sql.gen.ArrayMapBytecodeExpression)1 CachedInstanceBinder (io.prestosql.sql.gen.CachedInstanceBinder)1 CallSiteBinder (io.prestosql.sql.gen.CallSiteBinder)1