Search in sources :

Example 1 with Signature

use of io.trino.metadata.Signature in project trino by trinodb.

the class AbstractGreatestLeast method generate.

private Class<?> generate(List<Class<?>> javaTypes, MethodHandle compareMethod) {
    Signature signature = getFunctionMetadata().getSignature();
    checkCondition(javaTypes.size() <= 127, NOT_SUPPORTED, "Too many arguments for function call %s()", signature.getName());
    String javaTypeName = javaTypes.stream().map(Class::getSimpleName).collect(joining());
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(javaTypeName + "$" + signature.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), signature.getName(), type(wrap(javaTypes.get(0))), parameters);
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    CallSiteBinder binder = new CallSiteBinder();
    Variable value = scope.declareVariable(wrap(javaTypes.get(0)), "value");
    BytecodeExpression nullValue = constantNull(wrap(javaTypes.get(0)));
    body.append(value.set(nullValue));
    LabelNode done = new LabelNode("done");
    compareMethod = compareMethod.asType(methodType(boolean.class, compareMethod.type().wrap().parameterList()));
    for (int i = 0; i < javaTypes.size(); i++) {
        Parameter parameter = parameters.get(i);
        BytecodeExpression invokeCompare = invokeDynamic(BOOTSTRAP_METHOD, ImmutableList.of(binder.bind(compareMethod).getBindingId()), "compare", boolean.class, parameter, value);
        body.append(new IfStatement().condition(isNull(parameter)).ifTrue(new BytecodeBlock().append(value.set(nullValue)).gotoLabel(done)));
        body.append(new IfStatement().condition(or(isNull(value), invokeCompare)).ifTrue(value.set(parameter)));
    }
    body.visitLabel(done);
    body.append(value.ret());
    return defineClass(definition, Object.class, binder.getBindings(), new DynamicClassLoader(getClass().getClassLoader()));
}
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) LabelNode(io.airlift.bytecode.instruction.LabelNode) DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) Variable(io.airlift.bytecode.Variable) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) ClassDefinition(io.airlift.bytecode.ClassDefinition) IfStatement(io.airlift.bytecode.control.IfStatement) Scope(io.airlift.bytecode.Scope) MethodDefinition(io.airlift.bytecode.MethodDefinition) TypeSignature(io.trino.spi.type.TypeSignature) Signature(io.trino.metadata.Signature) BoundSignature(io.trino.metadata.BoundSignature) CallSiteBinder(io.trino.sql.gen.CallSiteBinder) Signature.orderableTypeParameter(io.trino.metadata.Signature.orderableTypeParameter) Parameter(io.airlift.bytecode.Parameter) BytecodeExpression(io.airlift.bytecode.expression.BytecodeExpression)

Example 2 with Signature

use of io.trino.metadata.Signature in project trino by trinodb.

the class ParametricAggregation method findMatchingImplementation.

private AggregationImplementation findMatchingImplementation(BoundSignature boundSignature) {
    Signature signature = boundSignature.toSignature();
    Optional<AggregationImplementation> foundImplementation = Optional.empty();
    if (implementations.getExactImplementations().containsKey(signature)) {
        foundImplementation = Optional.of(implementations.getExactImplementations().get(signature));
    } else {
        for (AggregationImplementation candidate : implementations.getGenericImplementations()) {
            if (candidate.areTypesAssignable(boundSignature)) {
                if (foundImplementation.isPresent()) {
                    throw new TrinoException(AMBIGUOUS_FUNCTION_CALL, format("Ambiguous function call (%s) for %s", boundSignature, getFunctionMetadata().getSignature()));
                }
                foundImplementation = Optional.of(candidate);
            }
        }
    }
    if (foundImplementation.isEmpty()) {
        throw new TrinoException(FUNCTION_IMPLEMENTATION_MISSING, format("Unsupported type parameters (%s) for %s", boundSignature, getFunctionMetadata().getSignature()));
    }
    return foundImplementation.get();
}
Also used : Signature(io.trino.metadata.Signature) BoundSignature(io.trino.metadata.BoundSignature) TrinoException(io.trino.spi.TrinoException)

Example 3 with Signature

use of io.trino.metadata.Signature in project trino by trinodb.

the class DecimalOperators method decimalSubtractOperator.

private static SqlScalarFunction decimalSubtractOperator() {
    TypeSignature decimalLeftSignature = new TypeSignature("decimal", typeVariable("a_precision"), typeVariable("a_scale"));
    TypeSignature decimalRightSignature = new TypeSignature("decimal", typeVariable("b_precision"), typeVariable("b_scale"));
    TypeSignature decimalResultSignature = new TypeSignature("decimal", typeVariable("r_precision"), typeVariable("r_scale"));
    Signature signature = Signature.builder().operatorType(SUBTRACT).longVariableConstraints(longVariableExpression("r_precision", "min(38, max(a_precision - a_scale, b_precision - b_scale) + max(a_scale, b_scale) + 1)"), longVariableExpression("r_scale", "max(a_scale, b_scale)")).argumentTypes(decimalLeftSignature, decimalRightSignature).returnType(decimalResultSignature).build();
    return new PolymorphicScalarFunctionBuilder(DecimalOperators.class).signature(signature).deterministic(true).choice(choice -> choice.implementation(methodsGroup -> methodsGroup.methods("subtractShortShortShort").withExtraParameters(DecimalOperators::calculateShortRescaleParameters)).implementation(methodsGroup -> methodsGroup.methods("subtractShortShortLong", "subtractLongLongLong", "subtractShortLongLong", "subtractLongShortLong").withExtraParameters(DecimalOperators::calculateLongRescaleParameters))).build();
}
Also used : MODULUS(io.trino.spi.function.OperatorType.MODULUS) PolymorphicScalarFunctionBuilder(io.trino.metadata.PolymorphicScalarFunctionBuilder) Int128Math.add(io.trino.spi.type.Int128Math.add) Int128Math.subtract(io.trino.spi.type.Int128Math.subtract) TypeSignatureParameter.typeVariable(io.trino.spi.type.TypeSignatureParameter.typeVariable) Int128Math.remainder(io.trino.spi.type.Int128Math.remainder) LiteralParameters(io.trino.spi.function.LiteralParameters) Int128Math.multiply(io.trino.spi.type.Int128Math.multiply) Math.abs(java.lang.Math.abs) SUBTRACT(io.trino.spi.function.OperatorType.SUBTRACT) Int128Math.divideRoundUp(io.trino.spi.type.Int128Math.divideRoundUp) ImmutableList(com.google.common.collect.ImmutableList) SignatureBuilder(io.trino.metadata.SignatureBuilder) SqlType(io.trino.spi.function.SqlType) Objects.requireNonNull(java.util.Objects.requireNonNull) Signature.longVariableExpression(io.trino.metadata.Signature.longVariableExpression) Math.toIntExact(java.lang.Math.toIntExact) Signature(io.trino.metadata.Signature) NUMERIC_VALUE_OUT_OF_RANGE(io.trino.spi.StandardErrorCode.NUMERIC_VALUE_OUT_OF_RANGE) TypeSignature(io.trino.spi.type.TypeSignature) Long.signum(java.lang.Long.signum) Int128(io.trino.spi.type.Int128) NEGATION(io.trino.spi.function.OperatorType.NEGATION) Decimals.longTenToNth(io.trino.spi.type.Decimals.longTenToNth) ScalarOperator(io.trino.spi.function.ScalarOperator) DIVIDE(io.trino.spi.function.OperatorType.DIVIDE) TrinoException(io.trino.spi.TrinoException) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode) SqlScalarFunction(io.trino.metadata.SqlScalarFunction) DIVISION_BY_ZERO(io.trino.spi.StandardErrorCode.DIVISION_BY_ZERO) SpecializeContext(io.trino.metadata.PolymorphicScalarFunctionBuilder.SpecializeContext) Int128Math.rescale(io.trino.spi.type.Int128Math.rescale) Integer.max(java.lang.Integer.max) List(java.util.List) Int128Math.negateExact(io.trino.spi.type.Int128Math.negateExact) ADD(io.trino.spi.function.OperatorType.ADD) MULTIPLY(io.trino.spi.function.OperatorType.MULTIPLY) Decimals(io.trino.spi.type.Decimals) DecimalType(io.trino.spi.type.DecimalType) TypeSignature(io.trino.spi.type.TypeSignature) Signature(io.trino.metadata.Signature) TypeSignature(io.trino.spi.type.TypeSignature) PolymorphicScalarFunctionBuilder(io.trino.metadata.PolymorphicScalarFunctionBuilder)

Example 4 with Signature

use of io.trino.metadata.Signature in project trino by trinodb.

the class DecimalOperators method decimalAddOperator.

private static SqlScalarFunction decimalAddOperator() {
    TypeSignature decimalLeftSignature = new TypeSignature("decimal", typeVariable("a_precision"), typeVariable("a_scale"));
    TypeSignature decimalRightSignature = new TypeSignature("decimal", typeVariable("b_precision"), typeVariable("b_scale"));
    TypeSignature decimalResultSignature = new TypeSignature("decimal", typeVariable("r_precision"), typeVariable("r_scale"));
    Signature signature = Signature.builder().operatorType(ADD).longVariableConstraints(longVariableExpression("r_precision", "min(38, max(a_precision - a_scale, b_precision - b_scale) + max(a_scale, b_scale) + 1)"), longVariableExpression("r_scale", "max(a_scale, b_scale)")).argumentTypes(decimalLeftSignature, decimalRightSignature).returnType(decimalResultSignature).build();
    return new PolymorphicScalarFunctionBuilder(DecimalOperators.class).signature(signature).deterministic(true).choice(choice -> choice.implementation(methodsGroup -> methodsGroup.methods("addShortShortShort").withExtraParameters(DecimalOperators::calculateShortRescaleParameters)).implementation(methodsGroup -> methodsGroup.methods("addShortShortLong", "addLongLongLong", "addShortLongLong", "addLongShortLong").withExtraParameters(DecimalOperators::calculateLongRescaleParameters))).build();
}
Also used : MODULUS(io.trino.spi.function.OperatorType.MODULUS) PolymorphicScalarFunctionBuilder(io.trino.metadata.PolymorphicScalarFunctionBuilder) Int128Math.add(io.trino.spi.type.Int128Math.add) Int128Math.subtract(io.trino.spi.type.Int128Math.subtract) TypeSignatureParameter.typeVariable(io.trino.spi.type.TypeSignatureParameter.typeVariable) Int128Math.remainder(io.trino.spi.type.Int128Math.remainder) LiteralParameters(io.trino.spi.function.LiteralParameters) Int128Math.multiply(io.trino.spi.type.Int128Math.multiply) Math.abs(java.lang.Math.abs) SUBTRACT(io.trino.spi.function.OperatorType.SUBTRACT) Int128Math.divideRoundUp(io.trino.spi.type.Int128Math.divideRoundUp) ImmutableList(com.google.common.collect.ImmutableList) SignatureBuilder(io.trino.metadata.SignatureBuilder) SqlType(io.trino.spi.function.SqlType) Objects.requireNonNull(java.util.Objects.requireNonNull) Signature.longVariableExpression(io.trino.metadata.Signature.longVariableExpression) Math.toIntExact(java.lang.Math.toIntExact) Signature(io.trino.metadata.Signature) NUMERIC_VALUE_OUT_OF_RANGE(io.trino.spi.StandardErrorCode.NUMERIC_VALUE_OUT_OF_RANGE) TypeSignature(io.trino.spi.type.TypeSignature) Long.signum(java.lang.Long.signum) Int128(io.trino.spi.type.Int128) NEGATION(io.trino.spi.function.OperatorType.NEGATION) Decimals.longTenToNth(io.trino.spi.type.Decimals.longTenToNth) ScalarOperator(io.trino.spi.function.ScalarOperator) DIVIDE(io.trino.spi.function.OperatorType.DIVIDE) TrinoException(io.trino.spi.TrinoException) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode) SqlScalarFunction(io.trino.metadata.SqlScalarFunction) DIVISION_BY_ZERO(io.trino.spi.StandardErrorCode.DIVISION_BY_ZERO) SpecializeContext(io.trino.metadata.PolymorphicScalarFunctionBuilder.SpecializeContext) Int128Math.rescale(io.trino.spi.type.Int128Math.rescale) Integer.max(java.lang.Integer.max) List(java.util.List) Int128Math.negateExact(io.trino.spi.type.Int128Math.negateExact) ADD(io.trino.spi.function.OperatorType.ADD) MULTIPLY(io.trino.spi.function.OperatorType.MULTIPLY) Decimals(io.trino.spi.type.Decimals) DecimalType(io.trino.spi.type.DecimalType) TypeSignature(io.trino.spi.type.TypeSignature) Signature(io.trino.metadata.Signature) TypeSignature(io.trino.spi.type.TypeSignature) PolymorphicScalarFunctionBuilder(io.trino.metadata.PolymorphicScalarFunctionBuilder)

Example 5 with Signature

use of io.trino.metadata.Signature in project trino by trinodb.

the class DecimalOperators method decimalMultiplyOperator.

private static SqlScalarFunction decimalMultiplyOperator() {
    TypeSignature decimalLeftSignature = new TypeSignature("decimal", typeVariable("a_precision"), typeVariable("a_scale"));
    TypeSignature decimalRightSignature = new TypeSignature("decimal", typeVariable("b_precision"), typeVariable("b_scale"));
    TypeSignature decimalResultSignature = new TypeSignature("decimal", typeVariable("r_precision"), typeVariable("r_scale"));
    Signature signature = Signature.builder().operatorType(MULTIPLY).longVariableConstraints(longVariableExpression("r_precision", "min(38, a_precision + b_precision)"), longVariableExpression("r_scale", "a_scale + b_scale")).argumentTypes(decimalLeftSignature, decimalRightSignature).returnType(decimalResultSignature).build();
    return new PolymorphicScalarFunctionBuilder(DecimalOperators.class).signature(signature).deterministic(true).choice(choice -> choice.implementation(methodsGroup -> methodsGroup.methods("multiplyShortShortShort", "multiplyShortShortLong", "multiplyLongLongLong", "multiplyShortLongLong", "multiplyLongShortLong"))).build();
}
Also used : MODULUS(io.trino.spi.function.OperatorType.MODULUS) PolymorphicScalarFunctionBuilder(io.trino.metadata.PolymorphicScalarFunctionBuilder) Int128Math.add(io.trino.spi.type.Int128Math.add) Int128Math.subtract(io.trino.spi.type.Int128Math.subtract) TypeSignatureParameter.typeVariable(io.trino.spi.type.TypeSignatureParameter.typeVariable) Int128Math.remainder(io.trino.spi.type.Int128Math.remainder) LiteralParameters(io.trino.spi.function.LiteralParameters) Int128Math.multiply(io.trino.spi.type.Int128Math.multiply) Math.abs(java.lang.Math.abs) SUBTRACT(io.trino.spi.function.OperatorType.SUBTRACT) Int128Math.divideRoundUp(io.trino.spi.type.Int128Math.divideRoundUp) ImmutableList(com.google.common.collect.ImmutableList) SignatureBuilder(io.trino.metadata.SignatureBuilder) SqlType(io.trino.spi.function.SqlType) Objects.requireNonNull(java.util.Objects.requireNonNull) Signature.longVariableExpression(io.trino.metadata.Signature.longVariableExpression) Math.toIntExact(java.lang.Math.toIntExact) Signature(io.trino.metadata.Signature) NUMERIC_VALUE_OUT_OF_RANGE(io.trino.spi.StandardErrorCode.NUMERIC_VALUE_OUT_OF_RANGE) TypeSignature(io.trino.spi.type.TypeSignature) Long.signum(java.lang.Long.signum) Int128(io.trino.spi.type.Int128) NEGATION(io.trino.spi.function.OperatorType.NEGATION) Decimals.longTenToNth(io.trino.spi.type.Decimals.longTenToNth) ScalarOperator(io.trino.spi.function.ScalarOperator) DIVIDE(io.trino.spi.function.OperatorType.DIVIDE) TrinoException(io.trino.spi.TrinoException) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode) SqlScalarFunction(io.trino.metadata.SqlScalarFunction) DIVISION_BY_ZERO(io.trino.spi.StandardErrorCode.DIVISION_BY_ZERO) SpecializeContext(io.trino.metadata.PolymorphicScalarFunctionBuilder.SpecializeContext) Int128Math.rescale(io.trino.spi.type.Int128Math.rescale) Integer.max(java.lang.Integer.max) List(java.util.List) Int128Math.negateExact(io.trino.spi.type.Int128Math.negateExact) ADD(io.trino.spi.function.OperatorType.ADD) MULTIPLY(io.trino.spi.function.OperatorType.MULTIPLY) Decimals(io.trino.spi.type.Decimals) DecimalType(io.trino.spi.type.DecimalType) TypeSignature(io.trino.spi.type.TypeSignature) Signature(io.trino.metadata.Signature) TypeSignature(io.trino.spi.type.TypeSignature) PolymorphicScalarFunctionBuilder(io.trino.metadata.PolymorphicScalarFunctionBuilder)

Aggregations

Signature (io.trino.metadata.Signature)34 TypeSignature (io.trino.spi.type.TypeSignature)32 BoundSignature (io.trino.metadata.BoundSignature)27 Test (org.testng.annotations.Test)25 SqlScalarFunction (io.trino.metadata.SqlScalarFunction)16 ParametricAggregation (io.trino.operator.aggregation.ParametricAggregation)14 AggregationImplementation (io.trino.operator.aggregation.AggregationImplementation)13 FunctionMetadata (io.trino.metadata.FunctionMetadata)12 ParametricScalar (io.trino.operator.scalar.ParametricScalar)12 AggregationFunctionMetadata (io.trino.metadata.AggregationFunctionMetadata)9 ImmutableList (com.google.common.collect.ImmutableList)8 List (java.util.List)8 TrinoException (io.trino.spi.TrinoException)5 LiteralParameters (io.trino.spi.function.LiteralParameters)5 Objects.requireNonNull (java.util.Objects.requireNonNull)5 UsedByGeneratedCode (io.trino.annotation.UsedByGeneratedCode)4 PolymorphicScalarFunctionBuilder (io.trino.metadata.PolymorphicScalarFunctionBuilder)4 SpecializeContext (io.trino.metadata.PolymorphicScalarFunctionBuilder.SpecializeContext)4 Signature.longVariableExpression (io.trino.metadata.Signature.longVariableExpression)4 SignatureBuilder (io.trino.metadata.SignatureBuilder)4