Search in sources :

Example 6 with AggregationFunctionMetadata

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

the class TestAnnotationEngineForAggregates method testMultiOutputAggregationParse.

@Test
public void testMultiOutputAggregationParse() {
    Signature expectedSignature1 = new Signature("multi_output_aggregate_1", DoubleType.DOUBLE.getTypeSignature(), ImmutableList.of(DoubleType.DOUBLE.getTypeSignature()));
    Signature expectedSignature2 = new Signature("multi_output_aggregate_2", DoubleType.DOUBLE.getTypeSignature(), ImmutableList.of(DoubleType.DOUBLE.getTypeSignature()));
    List<ParametricAggregation> aggregations = parseFunctionDefinitions(MultiOutputAggregationFunction.class);
    assertEquals(aggregations.size(), 2);
    ParametricAggregation aggregation1 = aggregations.stream().filter(aggregate -> aggregate.getFunctionMetadata().getSignature().getName().equals("multi_output_aggregate_1")).collect(toImmutableList()).get(0);
    assertEquals(aggregation1.getFunctionMetadata().getSignature(), expectedSignature1);
    assertEquals(aggregation1.getFunctionMetadata().getDescription(), "Simple multi output function aggregate specialized description");
    ParametricAggregation aggregation2 = aggregations.stream().filter(aggregate -> aggregate.getFunctionMetadata().getSignature().getName().equals("multi_output_aggregate_2")).collect(toImmutableList()).get(0);
    assertEquals(aggregation2.getFunctionMetadata().getSignature(), expectedSignature2);
    assertEquals(aggregation2.getFunctionMetadata().getDescription(), "Simple multi output function aggregate generic description");
    ParametricImplementationsGroup<AggregationImplementation> implementations1 = aggregation1.getImplementations();
    assertImplementationCount(implementations1, 1, 0, 0);
    ParametricImplementationsGroup<AggregationImplementation> implementations2 = aggregation2.getImplementations();
    assertImplementationCount(implementations2, 1, 0, 0);
    AggregationImplementation implementation = getOnlyElement(implementations1.getExactImplementations().values());
    assertEquals(implementation.getDefinitionClass(), MultiOutputAggregationFunction.class);
    assertDependencyCount(implementation, 0, 0, 0);
    assertFalse(implementation.hasSpecializedTypeParameters());
    assertEquals(implementation.getInputParameterKinds(), ImmutableList.of(STATE, INPUT_CHANNEL));
    BoundSignature boundSignature = new BoundSignature(aggregation1.getFunctionMetadata().getSignature().getName(), DoubleType.DOUBLE, ImmutableList.of(DoubleType.DOUBLE));
    AggregationFunctionMetadata aggregationMetadata = aggregation1.getAggregationMetadata();
    assertFalse(aggregationMetadata.isOrderSensitive());
    assertFalse(aggregationMetadata.getIntermediateTypes().isEmpty());
    aggregation1.specialize(boundSignature, NO_FUNCTION_DEPENDENCIES);
}
Also used : AggregationImplementation(io.trino.operator.aggregation.AggregationImplementation) 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 7 with AggregationFunctionMetadata

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

the class StatisticAggregations method createPartialAggregations.

public Parts createPartialAggregations(SymbolAllocator symbolAllocator, PlannerContext plannerContext) {
    ImmutableMap.Builder<Symbol, Aggregation> partialAggregation = ImmutableMap.builder();
    ImmutableMap.Builder<Symbol, Aggregation> finalAggregation = ImmutableMap.builder();
    ImmutableMap.Builder<Symbol, Symbol> mappings = ImmutableMap.builder();
    for (Map.Entry<Symbol, Aggregation> entry : aggregations.entrySet()) {
        Aggregation originalAggregation = entry.getValue();
        ResolvedFunction resolvedFunction = originalAggregation.getResolvedFunction();
        AggregationFunctionMetadata functionMetadata = plannerContext.getMetadata().getAggregationFunctionMetadata(resolvedFunction);
        List<Type> intermediateTypes = functionMetadata.getIntermediateTypes().stream().map(plannerContext.getTypeManager()::getType).collect(toImmutableList());
        Type intermediateType = intermediateTypes.size() == 1 ? intermediateTypes.get(0) : RowType.anonymous(intermediateTypes);
        Symbol partialSymbol = symbolAllocator.newSymbol(resolvedFunction.getSignature().getName(), intermediateType);
        mappings.put(entry.getKey(), partialSymbol);
        partialAggregation.put(partialSymbol, new Aggregation(resolvedFunction, originalAggregation.getArguments(), originalAggregation.isDistinct(), originalAggregation.getFilter(), originalAggregation.getOrderingScheme(), originalAggregation.getMask()));
        finalAggregation.put(entry.getKey(), new Aggregation(resolvedFunction, ImmutableList.of(partialSymbol.toSymbolReference()), false, Optional.empty(), Optional.empty(), Optional.empty()));
    }
    groupingSymbols.forEach(symbol -> mappings.put(symbol, symbol));
    return new Parts(new StatisticAggregations(partialAggregation.buildOrThrow(), groupingSymbols), new StatisticAggregations(finalAggregation.buildOrThrow(), groupingSymbols), mappings.buildOrThrow());
}
Also used : Symbol(io.trino.sql.planner.Symbol) ResolvedFunction(io.trino.metadata.ResolvedFunction) ImmutableMap(com.google.common.collect.ImmutableMap) AggregationFunctionMetadata(io.trino.metadata.AggregationFunctionMetadata) Aggregation(io.trino.sql.planner.plan.AggregationNode.Aggregation) RowType(io.trino.spi.type.RowType) Type(io.trino.spi.type.Type) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Example 8 with AggregationFunctionMetadata

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

the class TestAnnotationEngineForAggregates method testSimpleGenericAggregationFunctionParse.

@Test
public void testSimpleGenericAggregationFunctionParse() {
    Signature expectedSignature = new Signature("simple_generic_implementations", ImmutableList.of(typeVariable("T")), ImmutableList.of(), new TypeSignature("T"), ImmutableList.of(new TypeSignature("T")), false);
    ParametricAggregation aggregation = getOnlyElement(parseFunctionDefinitions(GenericAggregationFunction.class));
    assertEquals(aggregation.getFunctionMetadata().getDescription(), "Simple aggregate with two generic implementations");
    assertTrue(aggregation.getFunctionMetadata().isDeterministic());
    assertEquals(aggregation.getFunctionMetadata().getSignature(), expectedSignature);
    assertEquals(aggregation.getStateClass(), NullableLongState.class);
    ParametricImplementationsGroup<AggregationImplementation> implementations = aggregation.getImplementations();
    assertImplementationCount(implementations, 0, 0, 2);
    AggregationImplementation implementationDouble = implementations.getGenericImplementations().stream().filter(impl -> impl.getInputFunction().type().equals(methodType(void.class, NullableLongState.class, double.class))).collect(toImmutableList()).get(0);
    assertEquals(implementationDouble.getDefinitionClass(), GenericAggregationFunction.class);
    assertDependencyCount(implementationDouble, 0, 0, 0);
    assertFalse(implementationDouble.hasSpecializedTypeParameters());
    assertEquals(implementationDouble.getInputParameterKinds(), ImmutableList.of(STATE, INPUT_CHANNEL));
    AggregationImplementation implementationLong = implementations.getGenericImplementations().stream().filter(impl -> impl.getInputFunction().type().equals(methodType(void.class, NullableLongState.class, long.class))).collect(toImmutableList()).get(0);
    assertEquals(implementationLong.getDefinitionClass(), GenericAggregationFunction.class);
    assertDependencyCount(implementationLong, 0, 0, 0);
    assertFalse(implementationLong.hasSpecializedTypeParameters());
    assertEquals(implementationLong.getInputParameterKinds(), ImmutableList.of(STATE, INPUT_CHANNEL));
    BoundSignature boundSignature = new BoundSignature(aggregation.getFunctionMetadata().getSignature().getName(), DoubleType.DOUBLE, ImmutableList.of(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) 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 9 with AggregationFunctionMetadata

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

the class TestAnnotationEngineForAggregates method testLongConstraintAggregateFunctionParse.

@Test
public void testLongConstraintAggregateFunctionParse() {
    Signature expectedSignature = new Signature("parametric_aggregate_long_constraint", ImmutableList.of(), ImmutableList.of(new LongVariableConstraint("z", "x + y")), new TypeSignature("varchar", TypeSignatureParameter.typeVariable("z")), ImmutableList.of(new TypeSignature("varchar", TypeSignatureParameter.typeVariable("x")), new TypeSignature("varchar", TypeSignatureParameter.typeVariable("y"))), false);
    ParametricAggregation aggregation = getOnlyElement(parseFunctionDefinitions(LongConstraintAggregateFunction.class));
    assertEquals(aggregation.getFunctionMetadata().getDescription(), "Parametric aggregate with parametric type returned");
    assertTrue(aggregation.getFunctionMetadata().isDeterministic());
    assertEquals(aggregation.getFunctionMetadata().getSignature(), expectedSignature);
    ParametricImplementationsGroup<AggregationImplementation> implementations = aggregation.getImplementations();
    assertEquals(implementations.getGenericImplementations().size(), 1);
    AggregationImplementation implementation = implementations.getGenericImplementations().get(0);
    assertEquals(implementation.getDefinitionClass(), LongConstraintAggregateFunction.class);
    assertDependencyCount(implementation, 0, 0, 0);
    assertFalse(implementation.hasSpecializedTypeParameters());
    assertEquals(implementation.getInputParameterKinds(), ImmutableList.of(STATE, INPUT_CHANNEL, INPUT_CHANNEL));
    BoundSignature boundSignature = new BoundSignature(aggregation.getFunctionMetadata().getSignature().getName(), createVarcharType(30), ImmutableList.of(createVarcharType(17), createVarcharType(13)));
    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) TypeSignature(io.trino.spi.type.TypeSignature) LongVariableConstraint(io.trino.metadata.LongVariableConstraint) 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 AggregationFunctionMetadata

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

the class TestAnnotationEngineForAggregates method testInjectLiteralAggregateParse.

@Test
public void testInjectLiteralAggregateParse() {
    Signature expectedSignature = new Signature("inject_literal_aggregate", new TypeSignature("varchar", TypeSignatureParameter.typeVariable("x")), ImmutableList.of(new TypeSignature("varchar", TypeSignatureParameter.typeVariable("x"))));
    ParametricAggregation aggregation = getOnlyElement(parseFunctionDefinitions(InjectLiteralAggregateFunction.class));
    assertEquals(aggregation.getFunctionMetadata().getDescription(), "Simple aggregate with type literal");
    assertTrue(aggregation.getFunctionMetadata().isDeterministic());
    assertEquals(aggregation.getFunctionMetadata().getSignature(), expectedSignature);
    ParametricImplementationsGroup<AggregationImplementation> implementations = aggregation.getImplementations();
    assertEquals(implementations.getGenericImplementations().size(), 1);
    AggregationImplementation implementation = implementations.getGenericImplementations().get(0);
    assertEquals(implementation.getDefinitionClass(), InjectLiteralAggregateFunction.class);
    assertDependencyCount(implementation, 1, 1, 1);
    assertTrue(implementation.getInputDependencies().get(0) instanceof LiteralImplementationDependency);
    assertTrue(implementation.getCombineDependencies().get(0) instanceof LiteralImplementationDependency);
    assertTrue(implementation.getOutputDependencies().get(0) instanceof LiteralImplementationDependency);
    assertFalse(implementation.hasSpecializedTypeParameters());
    assertEquals(implementation.getInputParameterKinds(), ImmutableList.of(STATE, INPUT_CHANNEL));
    BoundSignature boundSignature = new BoundSignature(aggregation.getFunctionMetadata().getSignature().getName(), createVarcharType(17), ImmutableList.of(createVarcharType(17)));
    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) TypeSignature(io.trino.spi.type.TypeSignature) LiteralImplementationDependency(io.trino.operator.annotations.LiteralImplementationDependency) 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

AggregationFunctionMetadata (io.trino.metadata.AggregationFunctionMetadata)13 BoundSignature (io.trino.metadata.BoundSignature)10 ParametricAggregation (io.trino.operator.aggregation.ParametricAggregation)10 Test (org.testng.annotations.Test)10 Signature (io.trino.metadata.Signature)9 AggregationImplementation (io.trino.operator.aggregation.AggregationImplementation)9 TypeSignature (io.trino.spi.type.TypeSignature)9 ResolvedFunction (io.trino.metadata.ResolvedFunction)3 ArrayType (io.trino.spi.type.ArrayType)2 RowType (io.trino.spi.type.RowType)2 Type (io.trino.spi.type.Type)2 Symbol (io.trino.sql.planner.Symbol)2 Map (java.util.Map)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 SystemSessionProperties.preferPartialAggregation (io.trino.SystemSessionProperties.preferPartialAggregation)1 FunctionBinding (io.trino.metadata.FunctionBinding)1 FunctionDependencies (io.trino.metadata.FunctionDependencies)1 FunctionMetadata (io.trino.metadata.FunctionMetadata)1 LongVariableConstraint (io.trino.metadata.LongVariableConstraint)1 LiteralImplementationDependency (io.trino.operator.annotations.LiteralImplementationDependency)1