Search in sources :

Example 1 with NULLABLE_RETURN

use of io.trino.spi.function.InvocationConvention.InvocationReturnConvention.NULLABLE_RETURN in project trino by trinodb.

the class RowType method getEqualOperatorMethodHandles.

private static List<OperatorMethodHandle> getEqualOperatorMethodHandles(TypeOperators typeOperators, List<Field> fields) {
    boolean comparable = fields.stream().allMatch(field -> field.getType().isComparable());
    if (!comparable) {
        return emptyList();
    }
    // for large rows, use a generic loop with a megamorphic call site
    if (fields.size() > MEGAMORPHIC_FIELD_COUNT) {
        List<MethodHandle> equalOperators = fields.stream().map(field -> typeOperators.getEqualOperator(field.getType(), simpleConvention(NULLABLE_RETURN, BLOCK_POSITION, BLOCK_POSITION))).collect(toUnmodifiableList());
        return singletonList(new OperatorMethodHandle(EQUAL_CONVENTION, EQUAL.bindTo(equalOperators)));
    }
    // (Block, Block):Boolean
    MethodHandle equal = dropArguments(constant(Boolean.class, TRUE), 0, Block.class, Block.class);
    for (int fieldId = 0; fieldId < fields.size(); fieldId++) {
        Field field = fields.get(fieldId);
        // (Block, Block, int, MethodHandle, Block, Block):Boolean
        equal = collectArguments(CHAIN_EQUAL, 0, equal);
        // field equal
        MethodHandle fieldEqualOperator = typeOperators.getEqualOperator(field.getType(), simpleConvention(NULLABLE_RETURN, BLOCK_POSITION, BLOCK_POSITION));
        // (Block, Block, Block, Block):Boolean
        equal = insertArguments(equal, 2, fieldId, fieldEqualOperator);
        // (Block, Block):Boolean
        equal = permuteArguments(equal, methodType(Boolean.class, Block.class, Block.class), 0, 1, 0, 1);
    }
    return singletonList(new OperatorMethodHandle(EQUAL_CONVENTION, equal));
}
Also used : MethodHandle(java.lang.invoke.MethodHandle) Arrays(java.util.Arrays) BLOCK_POSITION(io.trino.spi.function.InvocationConvention.InvocationArgumentConvention.BLOCK_POSITION) MethodHandles.dropArguments(java.lang.invoke.MethodHandles.dropArguments) ROW(io.trino.spi.type.StandardTypes.ROW) FAIL_ON_NULL(io.trino.spi.function.InvocationConvention.InvocationReturnConvention.FAIL_ON_NULL) BiFunction(java.util.function.BiFunction) MethodHandles.insertArguments(java.lang.invoke.MethodHandles.insertArguments) InvocationConvention.simpleConvention(io.trino.spi.function.InvocationConvention.simpleConvention) Function(java.util.function.Function) MethodHandles.lookup(java.lang.invoke.MethodHandles.lookup) OperatorMethodHandle(io.trino.spi.function.OperatorMethodHandle) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) Collectors.toUnmodifiableList(java.util.stream.Collectors.toUnmodifiableList) Lookup(java.lang.invoke.MethodHandles.Lookup) Block(io.trino.spi.block.Block) Objects.requireNonNull(java.util.Objects.requireNonNull) NULL_HASH_CODE(io.trino.spi.type.TypeUtils.NULL_HASH_CODE) NULLABLE_RETURN(io.trino.spi.function.InvocationConvention.InvocationReturnConvention.NULLABLE_RETURN) MethodType.methodType(java.lang.invoke.MethodType.methodType) FALSE(java.lang.Boolean.FALSE) MethodHandles.constant(java.lang.invoke.MethodHandles.constant) BlockBuilderStatus(io.trino.spi.block.BlockBuilderStatus) Collections.emptyList(java.util.Collections.emptyList) BOXED_NULLABLE(io.trino.spi.function.InvocationConvention.InvocationArgumentConvention.BOXED_NULLABLE) MethodHandles.permuteArguments(java.lang.invoke.MethodHandles.permuteArguments) TrinoException(io.trino.spi.TrinoException) ConnectorSession(io.trino.spi.connector.ConnectorSession) List(java.util.List) InvocationConvention(io.trino.spi.function.InvocationConvention) MethodHandles.collectArguments(java.lang.invoke.MethodHandles.collectArguments) StandardErrorCode(io.trino.spi.StandardErrorCode) Optional(java.util.Optional) BlockBuilder(io.trino.spi.block.BlockBuilder) RowBlockBuilder(io.trino.spi.block.RowBlockBuilder) Collections(java.util.Collections) TRUE(java.lang.Boolean.TRUE) NEVER_NULL(io.trino.spi.function.InvocationConvention.InvocationArgumentConvention.NEVER_NULL) OperatorMethodHandle(io.trino.spi.function.OperatorMethodHandle) MethodHandle(java.lang.invoke.MethodHandle) OperatorMethodHandle(io.trino.spi.function.OperatorMethodHandle)

Example 2 with NULLABLE_RETURN

use of io.trino.spi.function.InvocationConvention.InvocationReturnConvention.NULLABLE_RETURN in project trino by trinodb.

the class AbstractGreatestLeast method specialize.

@Override
public ScalarFunctionImplementation specialize(BoundSignature boundSignature, FunctionDependencies functionDependencies) {
    Type type = boundSignature.getReturnType();
    checkArgument(type.isOrderable(), "Type must be orderable");
    MethodHandle compareMethod = getMinMaxCompare(functionDependencies, type, simpleConvention(FAIL_ON_NULL, NEVER_NULL, NEVER_NULL), min);
    List<Class<?>> javaTypes = IntStream.range(0, boundSignature.getArity()).mapToObj(i -> wrap(type.getJavaType())).collect(toImmutableList());
    Class<?> clazz = generate(javaTypes, compareMethod);
    MethodHandle methodHandle = methodHandle(clazz, getFunctionMetadata().getSignature().getName(), javaTypes.toArray(new Class<?>[0]));
    return new ChoicesScalarFunctionImplementation(boundSignature, NULLABLE_RETURN, nCopies(javaTypes.size(), BOXED_NULLABLE), methodHandle);
}
Also used : FunctionDependencies(io.trino.metadata.FunctionDependencies) SCALAR(io.trino.metadata.FunctionKind.SCALAR) FAIL_ON_NULL(io.trino.spi.function.InvocationConvention.InvocationReturnConvention.FAIL_ON_NULL) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) FunctionNullability(io.trino.metadata.FunctionNullability) Scope(io.airlift.bytecode.Scope) DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) InvocationConvention.simpleConvention(io.trino.spi.function.InvocationConvention.simpleConvention) Access.a(io.airlift.bytecode.Access.a) BOOTSTRAP_METHOD(io.trino.sql.gen.Bootstrap.BOOTSTRAP_METHOD) Parameter.arg(io.airlift.bytecode.Parameter.arg) NOT_SUPPORTED(io.trino.spi.StandardErrorCode.NOT_SUPPORTED) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) CompilerUtils.makeClassName(io.trino.util.CompilerUtils.makeClassName) MinMaxCompare.getMinMaxCompare(io.trino.util.MinMaxCompare.getMinMaxCompare) BytecodeExpressions.constantNull(io.airlift.bytecode.expression.BytecodeExpressions.constantNull) FunctionMetadata(io.trino.metadata.FunctionMetadata) TypeSignature(io.trino.spi.type.TypeSignature) MethodDefinition(io.airlift.bytecode.MethodDefinition) FunctionDependencyDeclaration(io.trino.metadata.FunctionDependencyDeclaration) Collections.nCopies(java.util.Collections.nCopies) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) BytecodeExpression(io.airlift.bytecode.expression.BytecodeExpression) Collectors.joining(java.util.stream.Collectors.joining) CompilerUtils.defineClass(io.trino.util.CompilerUtils.defineClass) Signature.orderableTypeParameter(io.trino.metadata.Signature.orderableTypeParameter) List(java.util.List) PRIVATE(io.airlift.bytecode.Access.PRIVATE) BytecodeExpressions.or(io.airlift.bytecode.expression.BytecodeExpressions.or) Failures.checkCondition(io.trino.util.Failures.checkCondition) NEVER_NULL(io.trino.spi.function.InvocationConvention.InvocationArgumentConvention.NEVER_NULL) ClassDefinition(io.airlift.bytecode.ClassDefinition) IntStream(java.util.stream.IntStream) ParameterizedType.type(io.airlift.bytecode.ParameterizedType.type) Variable(io.airlift.bytecode.Variable) MethodHandle(java.lang.invoke.MethodHandle) Type(io.trino.spi.type.Type) Parameter(io.airlift.bytecode.Parameter) ImmutableList(com.google.common.collect.ImmutableList) LabelNode(io.airlift.bytecode.instruction.LabelNode) Signature(io.trino.metadata.Signature) NULLABLE_RETURN(io.trino.spi.function.InvocationConvention.InvocationReturnConvention.NULLABLE_RETURN) FINAL(io.airlift.bytecode.Access.FINAL) STATIC(io.airlift.bytecode.Access.STATIC) MethodType.methodType(java.lang.invoke.MethodType.methodType) BytecodeExpressions.invokeDynamic(io.airlift.bytecode.expression.BytecodeExpressions.invokeDynamic) BOXED_NULLABLE(io.trino.spi.function.InvocationConvention.InvocationArgumentConvention.BOXED_NULLABLE) SqlScalarFunction(io.trino.metadata.SqlScalarFunction) IfStatement(io.airlift.bytecode.control.IfStatement) CallSiteBinder(io.trino.sql.gen.CallSiteBinder) PUBLIC(io.airlift.bytecode.Access.PUBLIC) MinMaxCompare.getMinMaxCompareFunctionDependencies(io.trino.util.MinMaxCompare.getMinMaxCompareFunctionDependencies) BoundSignature(io.trino.metadata.BoundSignature) BytecodeExpressions.isNull(io.airlift.bytecode.expression.BytecodeExpressions.isNull) Primitives.wrap(com.google.common.primitives.Primitives.wrap) Reflection.methodHandle(io.trino.util.Reflection.methodHandle) Type(io.trino.spi.type.Type) MethodType.methodType(java.lang.invoke.MethodType.methodType) CompilerUtils.defineClass(io.trino.util.CompilerUtils.defineClass) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

BOXED_NULLABLE (io.trino.spi.function.InvocationConvention.InvocationArgumentConvention.BOXED_NULLABLE)2 NEVER_NULL (io.trino.spi.function.InvocationConvention.InvocationArgumentConvention.NEVER_NULL)2 FAIL_ON_NULL (io.trino.spi.function.InvocationConvention.InvocationReturnConvention.FAIL_ON_NULL)2 NULLABLE_RETURN (io.trino.spi.function.InvocationConvention.InvocationReturnConvention.NULLABLE_RETURN)2 InvocationConvention.simpleConvention (io.trino.spi.function.InvocationConvention.simpleConvention)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 Primitives.wrap (com.google.common.primitives.Primitives.wrap)1 FINAL (io.airlift.bytecode.Access.FINAL)1 PRIVATE (io.airlift.bytecode.Access.PRIVATE)1 PUBLIC (io.airlift.bytecode.Access.PUBLIC)1 STATIC (io.airlift.bytecode.Access.STATIC)1 Access.a (io.airlift.bytecode.Access.a)1 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)1 ClassDefinition (io.airlift.bytecode.ClassDefinition)1 DynamicClassLoader (io.airlift.bytecode.DynamicClassLoader)1 MethodDefinition (io.airlift.bytecode.MethodDefinition)1 Parameter (io.airlift.bytecode.Parameter)1 Parameter.arg (io.airlift.bytecode.Parameter.arg)1