Search in sources :

Example 16 with Scope

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

the class AbstractGreatestLeast method generate.

private Class<?> generate(List<Class<?>> javaTypes, Type type, MethodHandle compareMethod) {
    String javaTypeName = javaTypes.stream().map(Class::getSimpleName).collect(joining());
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), CompilerUtils.makeClassName(javaTypeName + "$" + getSignature().getName()), type(Object.class));
    definition.declareDefaultConstructor(a(PRIVATE));
    List<Parameter> parameters = IntStream.range(0, javaTypes.size()).mapToObj(i -> arg("arg" + i, javaTypes.get(i))).collect(toImmutableList());
    MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), getSignature().getName(), type(javaTypes.get(0)), parameters);
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    CallSiteBinder binder = new CallSiteBinder();
    if (type.getTypeSignature().getBase().equals(StandardTypes.DOUBLE)) {
        for (Parameter parameter : parameters) {
            body.append(parameter);
            body.append(invoke(binder.bind(CHECK_NOT_NAN.bindTo(getSignature().getName())), "checkNotNaN"));
        }
    }
    Variable value = scope.declareVariable(javaTypes.get(0), "value");
    body.append(value.set(parameters.get(0)));
    for (int i = 1; i < javaTypes.size(); i++) {
        body.append(new IfStatement().condition(new BytecodeBlock().append(parameters.get(i)).append(value).append(invoke(binder.bind(compareMethod), "compare"))).ifTrue(value.set(parameters.get(i))));
    }
    body.append(value.ret());
    return defineClass(definition, Object.class, binder.getBindings(), new DynamicClassLoader(getClass().getClassLoader()));
}
Also used : IntStream(java.util.stream.IntStream) MethodHandle(java.lang.invoke.MethodHandle) TypeManager(com.facebook.presto.spi.type.TypeManager) PRIVATE(com.facebook.presto.bytecode.Access.PRIVATE) PrestoException(com.facebook.presto.spi.PrestoException) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) BytecodeUtils.invoke(com.facebook.presto.sql.gen.BytecodeUtils.invoke) FunctionKind(com.facebook.presto.metadata.FunctionKind) Access.a(com.facebook.presto.bytecode.Access.a) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) BOOLEAN(com.facebook.presto.spi.type.BooleanType.BOOLEAN) ParameterizedType.type(com.facebook.presto.bytecode.ParameterizedType.type) Reflection.methodHandle(com.facebook.presto.util.Reflection.methodHandle) Type(com.facebook.presto.spi.type.Type) Objects.requireNonNull(java.util.Objects.requireNonNull) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) INVALID_FUNCTION_ARGUMENT(com.facebook.presto.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT) StandardTypes(com.facebook.presto.spi.type.StandardTypes) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode) IfStatement(com.facebook.presto.bytecode.control.IfStatement) PUBLIC(com.facebook.presto.bytecode.Access.PUBLIC) Variable(com.facebook.presto.bytecode.Variable) BoundVariables(com.facebook.presto.metadata.BoundVariables) Signature.orderableTypeParameter(com.facebook.presto.metadata.Signature.orderableTypeParameter) Parameter(com.facebook.presto.bytecode.Parameter) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) FunctionRegistry(com.facebook.presto.metadata.FunctionRegistry) Signature(com.facebook.presto.metadata.Signature) Signature.internalOperator(com.facebook.presto.metadata.Signature.internalOperator) DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) List(java.util.List) CompilerUtils(com.facebook.presto.bytecode.CompilerUtils) FINAL(com.facebook.presto.bytecode.Access.FINAL) STATIC(com.facebook.presto.bytecode.Access.STATIC) Scope(com.facebook.presto.bytecode.Scope) OperatorType(com.facebook.presto.spi.function.OperatorType) CompilerUtils.defineClass(com.facebook.presto.bytecode.CompilerUtils.defineClass) CallSiteBinder(com.facebook.presto.sql.gen.CallSiteBinder) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) Collections(java.util.Collections) Parameter.arg(com.facebook.presto.bytecode.Parameter.arg) DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) Variable(com.facebook.presto.bytecode.Variable) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) IfStatement(com.facebook.presto.bytecode.control.IfStatement) Scope(com.facebook.presto.bytecode.Scope) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) CallSiteBinder(com.facebook.presto.sql.gen.CallSiteBinder) Signature.orderableTypeParameter(com.facebook.presto.metadata.Signature.orderableTypeParameter) Parameter(com.facebook.presto.bytecode.Parameter)

Example 17 with Scope

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

Example 18 with Scope

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

the class ArrayConstructor method generateArrayConstructor.

private static Class<?> generateArrayConstructor(List<Class<?>> stackTypes, Type elementType) {
    List<String> stackTypeNames = stackTypes.stream().map(Class::getSimpleName).collect(toImmutableList());
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), CompilerUtils.makeClassName(Joiner.on("").join(stackTypeNames) + "ArrayConstructor"), type(Object.class));
    // Generate constructor
    definition.declareDefaultConstructor(a(PRIVATE));
    // Generate arrayConstructor()
    ImmutableList.Builder<Parameter> parameters = ImmutableList.builder();
    for (int i = 0; i < stackTypes.size(); i++) {
        Class<?> stackType = stackTypes.get(i);
        parameters.add(arg("arg" + i, stackType));
    }
    MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "arrayConstructor", type(Block.class), parameters.build());
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    Variable blockBuilderVariable = scope.declareVariable(BlockBuilder.class, "blockBuilder");
    CallSiteBinder binder = new CallSiteBinder();
    BytecodeExpression createBlockBuilder = blockBuilderVariable.set(constantType(binder, elementType).invoke("createBlockBuilder", BlockBuilder.class, newInstance(BlockBuilderStatus.class), constantInt(stackTypes.size())));
    body.append(createBlockBuilder);
    for (int i = 0; i < stackTypes.size(); i++) {
        if (elementType.getJavaType() == void.class) {
            body.append(blockBuilderVariable.invoke("appendNull", BlockBuilder.class));
        } else {
            Variable argument = scope.getVariable("arg" + i);
            IfStatement ifStatement = new IfStatement().condition(equal(argument, constantNull(stackTypes.get(i)))).ifTrue(blockBuilderVariable.invoke("appendNull", BlockBuilder.class).pop()).ifFalse(constantType(binder, elementType).writeValue(blockBuilderVariable, argument.cast(elementType.getJavaType())));
            body.append(ifStatement);
        }
    }
    body.append(blockBuilderVariable.invoke("build", Block.class).ret());
    return defineClass(definition, Object.class, binder.getBindings(), new DynamicClassLoader(ArrayConstructor.class.getClassLoader()));
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) Signature.typeVariable(com.facebook.presto.metadata.Signature.typeVariable) Variable(com.facebook.presto.bytecode.Variable) ImmutableList(com.google.common.collect.ImmutableList) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) IfStatement(com.facebook.presto.bytecode.control.IfStatement) 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) BytecodeExpression(com.facebook.presto.bytecode.expression.BytecodeExpression) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder)

Example 19 with Scope

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

the class StateCompiler method generateDeserialize.

private static <T> void generateDeserialize(ClassDefinition definition, CallSiteBinder binder, Class<T> clazz, List<StateField> fields) {
    Parameter block = arg("block", Block.class);
    Parameter index = arg("index", int.class);
    Parameter state = arg("state", Object.class);
    MethodDefinition method = definition.declareMethod(a(PUBLIC), "deserialize", type(void.class), block, index, state);
    BytecodeBlock deserializerBody = method.getBody();
    Scope scope = method.getScope();
    if (fields.size() == 1) {
        StateField field = fields.get(0);
        Method setter = getSetter(clazz, field);
        if (!field.isPrimitiveType()) {
            deserializerBody.append(new IfStatement().condition(block.invoke("isNull", boolean.class, index)).ifTrue(state.cast(setter.getDeclaringClass()).invoke(setter, constantNull(field.getType()))).ifFalse(state.cast(setter.getDeclaringClass()).invoke(setter, constantType(binder, field.getSqlType()).getValue(block, index))));
        } else {
            // For primitive type, we need to cast here because we serialize byte fields with TINYINT (whose java type is long).
            deserializerBody.append(state.cast(setter.getDeclaringClass()).invoke(setter, constantType(binder, field.getSqlType()).getValue(block, index).cast(field.getType())));
        }
    } else {
        Variable row = scope.declareVariable(Block.class, "row");
        deserializerBody.append(row.set(block.invoke("getObject", Object.class, index, constantClass(Block.class)).cast(Block.class)));
        int position = 0;
        for (StateField field : fields) {
            Method setter = getSetter(clazz, field);
            if (!field.isPrimitiveType()) {
                deserializerBody.append(new IfStatement().condition(row.invoke("isNull", boolean.class, constantInt(position))).ifTrue(state.cast(setter.getDeclaringClass()).invoke(setter, constantNull(field.getType()))).ifFalse(state.cast(setter.getDeclaringClass()).invoke(setter, constantType(binder, field.getSqlType()).getValue(row, constantInt(position)))));
            } else {
                // For primitive type, we need to cast here because we serialize byte fields with TINYINT (whose java type is long).
                deserializerBody.append(state.cast(setter.getDeclaringClass()).invoke(setter, constantType(binder, field.getSqlType()).getValue(row, constantInt(position)).cast(field.getType())));
            }
            position++;
        }
    }
    deserializerBody.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)

Example 20 with Scope

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

Aggregations

Scope (com.facebook.presto.bytecode.Scope)37 Variable (com.facebook.presto.bytecode.Variable)33 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)32 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)29 Parameter (com.facebook.presto.bytecode.Parameter)28 IfStatement (com.facebook.presto.bytecode.control.IfStatement)24 Block (com.facebook.presto.spi.block.Block)16 BytecodeNode (com.facebook.presto.bytecode.BytecodeNode)11 ImmutableList (com.google.common.collect.ImmutableList)11 Type (com.facebook.presto.spi.type.Type)9 ForLoop (com.facebook.presto.bytecode.control.ForLoop)8 BytecodeExpression (com.facebook.presto.bytecode.expression.BytecodeExpression)8 DictionaryBlock (com.facebook.presto.spi.block.DictionaryBlock)8 LazyBlock (com.facebook.presto.spi.block.LazyBlock)8 RunLengthEncodedBlock (com.facebook.presto.spi.block.RunLengthEncodedBlock)8 ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)7 LabelNode (com.facebook.presto.bytecode.instruction.LabelNode)7 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)7 CallSiteBinder (com.facebook.presto.sql.gen.CallSiteBinder)6 List (java.util.List)6