Search in sources :

Example 6 with Signature

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

the class DecimalOperators method decimalDivideOperator.

private static SqlScalarFunction decimalDivideOperator() {
    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"));
    // we extend target precision by b_scale. This is upper bound on how much division result will grow.
    // pessimistic case is a / 0.0000001
    // if scale of divisor is greater than scale of dividend we extend scale further as we
    // want result scale to be maximum of scales of divisor and dividend.
    Signature signature = Signature.builder().operatorType(DIVIDE).longVariableConstraints(longVariableExpression("r_precision", "min(38, a_precision + b_scale + max(b_scale - a_scale, 0))"), 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("divideShortShortShort", "divideShortLongShort", "divideLongShortShort", "divideShortShortLong", "divideLongLongLong", "divideShortLongLong", "divideLongShortLong").withExtraParameters(DecimalOperators::divideRescaleFactor))).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 7 with Signature

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

the class AggregationFromAnnotationsParser method parseFunctionDefinitions.

public static List<ParametricAggregation> parseFunctionDefinitions(Class<?> aggregationDefinition) {
    AggregationFunction aggregationAnnotation = aggregationDefinition.getAnnotation(AggregationFunction.class);
    requireNonNull(aggregationAnnotation, "aggregationAnnotation is null");
    ImmutableList.Builder<ParametricAggregation> functions = ImmutableList.builder();
    // There must be a single state class and combine function
    Class<? extends AccumulatorState> stateClass = getStateClass(aggregationDefinition);
    Optional<Method> combineFunction = getCombineFunction(aggregationDefinition, stateClass);
    // Each output function defines a new aggregation function
    for (Method outputFunction : getOutputFunctions(aggregationDefinition, stateClass)) {
        AggregationHeader header = parseHeader(aggregationDefinition, outputFunction);
        if (header.isDecomposable()) {
            checkArgument(combineFunction.isPresent(), "Decomposable method %s does not have a combine method", header.getName());
        } else if (combineFunction.isPresent()) {
            log.warn("Aggregation function %s is not decomposable, but has combine method", header.getName());
        }
        // Input functions can have either an exact signature, or generic/calculate signature
        List<AggregationImplementation> exactImplementations = new ArrayList<>();
        List<AggregationImplementation> nonExactImplementations = new ArrayList<>();
        for (Method inputFunction : getInputFunctions(aggregationDefinition, stateClass)) {
            Optional<Method> removeInputFunction = getRemoveInputFunction(aggregationDefinition, inputFunction);
            AggregationImplementation implementation = parseImplementation(aggregationDefinition, header.getName(), inputFunction, removeInputFunction, outputFunction, combineFunction.filter(function -> header.isDecomposable()));
            if (isGenericOrCalculated(implementation.getSignature())) {
                exactImplementations.add(implementation);
            } else {
                nonExactImplementations.add(implementation);
            }
        }
        // register a set functions for the canonical name, and each alias
        functions.addAll(buildFunctions(header.getName(), header, stateClass, exactImplementations, nonExactImplementations));
        for (String alias : getAliases(aggregationDefinition.getAnnotation(AggregationFunction.class), outputFunction)) {
            functions.addAll(buildFunctions(alias, header, stateClass, exactImplementations, nonExactImplementations));
        }
    }
    return functions.build();
}
Also used : Arrays(java.util.Arrays) Logger(io.airlift.log.Logger) AggregationFunction(io.trino.spi.function.AggregationFunction) OutputFunction(io.trino.spi.function.OutputFunction) ArrayList(java.util.ArrayList) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) RemoveInputFunction(io.trino.spi.function.RemoveInputFunction) CombineFunction(io.trino.spi.function.CombineFunction) Objects.requireNonNull(java.util.Objects.requireNonNull) Parser.parseImplementation(io.trino.operator.aggregation.AggregationImplementation.Parser.parseImplementation) Signature(io.trino.metadata.Signature) Method(java.lang.reflect.Method) TypeSignature(io.trino.spi.type.TypeSignature) ImmutableSet(com.google.common.collect.ImmutableSet) ParametricFunctionHelpers.signatureWithName(io.trino.operator.ParametricFunctionHelpers.signatureWithName) ParametricImplementationsGroup(io.trino.operator.ParametricImplementationsGroup) MoreCollectors(com.google.common.collect.MoreCollectors) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) AccumulatorState(io.trino.spi.function.AccumulatorState) List(java.util.List) Strings.emptyToNull(com.google.common.base.Strings.emptyToNull) FunctionsParserHelper(io.trino.operator.annotations.FunctionsParserHelper) Optional(java.util.Optional) InputFunction(io.trino.spi.function.InputFunction) FunctionsParserHelper.parseDescription(io.trino.operator.annotations.FunctionsParserHelper.parseDescription) AnnotatedElement(java.lang.reflect.AnnotatedElement) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) AggregationFunction(io.trino.spi.function.AggregationFunction)

Example 8 with Signature

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

the class TestAnnotationEngineForAggregates method testNotDecomposableAggregationParse.

@Test
public void testNotDecomposableAggregationParse() {
    Signature expectedSignature = new Signature("custom_decomposable_aggregate", DoubleType.DOUBLE.getTypeSignature(), ImmutableList.of(DoubleType.DOUBLE.getTypeSignature()));
    ParametricAggregation aggregation = getOnlyElement(parseFunctionDefinitions(NotDecomposableAggregationFunction.class));
    assertEquals(aggregation.getFunctionMetadata().getDescription(), "Aggregate with Decomposable=false");
    assertTrue(aggregation.getFunctionMetadata().isDeterministic());
    assertEquals(aggregation.getFunctionMetadata().getSignature(), expectedSignature);
    BoundSignature boundSignature = new BoundSignature(aggregation.getFunctionMetadata().getSignature().getName(), DoubleType.DOUBLE, ImmutableList.of(DoubleType.DOUBLE));
    AggregationFunctionMetadata aggregationMetadata = aggregation.getAggregationMetadata();
    assertFalse(aggregationMetadata.isOrderSensitive());
    assertTrue(aggregationMetadata.getIntermediateTypes().isEmpty());
    aggregation.specialize(boundSignature, NO_FUNCTION_DEPENDENCIES);
}
Also used : TypeSignature(io.trino.spi.type.TypeSignature) Signature(io.trino.metadata.Signature) BoundSignature(io.trino.metadata.BoundSignature) BoundSignature(io.trino.metadata.BoundSignature) ParametricAggregation(io.trino.operator.aggregation.ParametricAggregation) AggregationFunctionMetadata(io.trino.metadata.AggregationFunctionMetadata) Test(org.testng.annotations.Test)

Example 9 with Signature

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

the class TestAnnotationEngineForAggregates method testSimpleExplicitSpecializedAggregationParse.

// TODO this is not yet supported
@Test(enabled = false)
public void testSimpleExplicitSpecializedAggregationParse() {
    Signature expectedSignature = new Signature("explicit_specialized_aggregate", ImmutableList.of(typeVariable("T")), ImmutableList.of(), new TypeSignature("T"), ImmutableList.of(new TypeSignature(ARRAY, TypeSignatureParameter.typeParameter(new TypeSignature("T")))), false);
    ParametricAggregation aggregation = getOnlyElement(parseFunctionDefinitions(ExplicitSpecializedAggregationFunction.class));
    assertEquals(aggregation.getFunctionMetadata().getDescription(), "Simple explicit specialized aggregate");
    assertTrue(aggregation.getFunctionMetadata().isDeterministic());
    assertEquals(aggregation.getFunctionMetadata().getSignature(), expectedSignature);
    ParametricImplementationsGroup<AggregationImplementation> implementations = aggregation.getImplementations();
    assertImplementationCount(implementations, 0, 1, 1);
    AggregationImplementation implementation1 = implementations.getSpecializedImplementations().get(0);
    assertTrue(implementation1.hasSpecializedTypeParameters());
    assertFalse(implementation1.hasSpecializedTypeParameters());
    assertEquals(implementation1.getInputParameterKinds(), ImmutableList.of(STATE, INPUT_CHANNEL));
    AggregationImplementation implementation2 = implementations.getSpecializedImplementations().get(1);
    assertTrue(implementation2.hasSpecializedTypeParameters());
    assertFalse(implementation2.hasSpecializedTypeParameters());
    assertEquals(implementation2.getInputParameterKinds(), ImmutableList.of(STATE, INPUT_CHANNEL));
    BoundSignature boundSignature = new BoundSignature(aggregation.getFunctionMetadata().getSignature().getName(), DoubleType.DOUBLE, ImmutableList.of(new ArrayType(DoubleType.DOUBLE)));
    AggregationFunctionMetadata aggregationMetadata = aggregation.getAggregationMetadata();
    assertFalse(aggregationMetadata.isOrderSensitive());
    assertFalse(aggregationMetadata.getIntermediateTypes().isEmpty());
    aggregation.specialize(boundSignature, NO_FUNCTION_DEPENDENCIES);
}
Also used : AggregationImplementation(io.trino.operator.aggregation.AggregationImplementation) ArrayType(io.trino.spi.type.ArrayType) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) Signature(io.trino.metadata.Signature) BoundSignature(io.trino.metadata.BoundSignature) BoundSignature(io.trino.metadata.BoundSignature) ParametricAggregation(io.trino.operator.aggregation.ParametricAggregation) AggregationFunctionMetadata(io.trino.metadata.AggregationFunctionMetadata) Test(org.testng.annotations.Test)

Example 10 with Signature

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

the class TestAnnotationEngineForAggregates method testSimpleImplicitSpecializedAggregationParse.

// TODO this is not yet supported
@Test(enabled = false)
public void testSimpleImplicitSpecializedAggregationParse() {
    Signature expectedSignature = new Signature("implicit_specialized_aggregate", ImmutableList.of(typeVariable("T")), ImmutableList.of(), new TypeSignature("T"), ImmutableList.of(new TypeSignature(ARRAY, TypeSignatureParameter.typeParameter(new TypeSignature("T"))), new TypeSignature("T")), false);
    ParametricAggregation aggregation = getOnlyElement(parseFunctionDefinitions(ImplicitSpecializedAggregationFunction.class));
    assertEquals(aggregation.getFunctionMetadata().getDescription(), "Simple implicit specialized aggregate");
    assertTrue(aggregation.getFunctionMetadata().isDeterministic());
    assertEquals(aggregation.getFunctionMetadata().getSignature(), expectedSignature);
    ParametricImplementationsGroup<AggregationImplementation> implementations = aggregation.getImplementations();
    assertImplementationCount(implementations, 0, 0, 2);
    AggregationImplementation implementation1 = implementations.getSpecializedImplementations().get(0);
    assertTrue(implementation1.hasSpecializedTypeParameters());
    assertFalse(implementation1.hasSpecializedTypeParameters());
    assertEquals(implementation1.getInputParameterKinds(), ImmutableList.of(STATE, INPUT_CHANNEL, INPUT_CHANNEL));
    AggregationImplementation implementation2 = implementations.getSpecializedImplementations().get(1);
    assertTrue(implementation2.hasSpecializedTypeParameters());
    assertFalse(implementation2.hasSpecializedTypeParameters());
    assertEquals(implementation2.getInputParameterKinds(), ImmutableList.of(STATE, INPUT_CHANNEL, INPUT_CHANNEL));
    BoundSignature boundSignature = new BoundSignature(aggregation.getFunctionMetadata().getSignature().getName(), DoubleType.DOUBLE, ImmutableList.of(new ArrayType(DoubleType.DOUBLE)));
    AggregationFunctionMetadata aggregationMetadata = aggregation.getAggregationMetadata();
    assertFalse(aggregationMetadata.isOrderSensitive());
    assertFalse(aggregationMetadata.getIntermediateTypes().isEmpty());
    aggregation.specialize(boundSignature, NO_FUNCTION_DEPENDENCIES);
}
Also used : AggregationImplementation(io.trino.operator.aggregation.AggregationImplementation) ArrayType(io.trino.spi.type.ArrayType) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) Signature(io.trino.metadata.Signature) BoundSignature(io.trino.metadata.BoundSignature) BoundSignature(io.trino.metadata.BoundSignature) ParametricAggregation(io.trino.operator.aggregation.ParametricAggregation) AggregationFunctionMetadata(io.trino.metadata.AggregationFunctionMetadata) Test(org.testng.annotations.Test)

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