Search in sources :

Example 1 with ArrayMapBytecodeExpression

use of com.facebook.presto.sql.gen.ArrayMapBytecodeExpression in project presto by prestodb.

the class ArrayToArrayCast method generateArrayCast.

private static Class<?> generateArrayCast(TypeManager typeManager, Signature elementCastSignature, ScalarFunctionImplementation elementCast) {
    CallSiteBinder binder = new CallSiteBinder();
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), CompilerUtils.makeClassName(Joiner.on("$").join("ArrayCast", elementCastSignature.getArgumentTypes().get(0), elementCastSignature.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(elementCastSignature.getArgumentTypes().get(0));
    Type toElementType = typeManager.getType(elementCastSignature.getReturnType());
    CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(definition, binder);
    ArrayMapBytecodeExpression newArray = ArrayGeneratorUtils.map(scope, cachedInstanceBinder, fromElementType, toElementType, value, elementCastSignature.getName(), 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(com.facebook.presto.sql.gen.ArrayMapBytecodeExpression) Signature.typeVariable(com.facebook.presto.metadata.Signature.typeVariable) Variable(com.facebook.presto.bytecode.Variable) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) Type(com.facebook.presto.spi.type.Type) 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) Parameter(com.facebook.presto.bytecode.Parameter) Block(com.facebook.presto.spi.block.Block) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock)

Aggregations

BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)1 ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)1 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)1 Parameter (com.facebook.presto.bytecode.Parameter)1 Scope (com.facebook.presto.bytecode.Scope)1 Variable (com.facebook.presto.bytecode.Variable)1 Signature.typeVariable (com.facebook.presto.metadata.Signature.typeVariable)1 Block (com.facebook.presto.spi.block.Block)1 Type (com.facebook.presto.spi.type.Type)1 ArrayMapBytecodeExpression (com.facebook.presto.sql.gen.ArrayMapBytecodeExpression)1 CachedInstanceBinder (com.facebook.presto.sql.gen.CachedInstanceBinder)1 CallSiteBinder (com.facebook.presto.sql.gen.CallSiteBinder)1