public static List<SqlAggregationFunction> createFunctionByAnnotations(Class<?> aggregationDefinition) { return ImmutableList.of(AggregationFromAnnotationsParser.parseFunctionDefinition(aggregationDefinition)); }
@Test public void testNotAnnotatedAggregateStateAggregationParse() { ParametricAggregation aggregation = parseFunctionDefinition(NotAnnotatedAggregateStateAggregationFunction.class); AggregationImplementation implementation = getOnlyElement(aggregation.getImplementations().getExactImplementations().values()); List<AggregationMetadata.ParameterMetadata.ParameterType> expectedMetadataTypes = ImmutableList.of(AggregationMetadata.ParameterMetadata.ParameterType.STATE, AggregationMetadata.ParameterMetadata.ParameterType.INPUT_CHANNEL); assertTrue(implementation.getInputParameterMetadataTypes().equals(expectedMetadataTypes)); InternalAggregationFunction specialized = aggregation.specialize(BoundVariables.builder().build(), 1, new TypeRegistry(), null); assertEquals(specialized.getFinalType(), DoubleType.DOUBLE); assertTrue(specialized.isDecomposable()); assertEquals(specialized.name(), "no_aggregation_state_aggregate"); }
@Test public void testStateOnDifferentThanFirstPositionAggregationParse() { Signature expectedSignature = new Signature( "simple_exact_aggregate_aggregation_state_moved", FunctionKind.AGGREGATE, DoubleType.DOUBLE.getTypeSignature(), ImmutableList.of(DoubleType.DOUBLE.getTypeSignature())); ParametricAggregation aggregation = parseFunctionDefinition(StateOnDifferentThanFirstPositionAggregationFunction.class); assertEquals(aggregation.getSignature(), expectedSignature); AggregationImplementation implementation = getOnlyElement(aggregation.getImplementations().getExactImplementations().values()); assertEquals(implementation.getDefinitionClass(), StateOnDifferentThanFirstPositionAggregationFunction.class); List<AggregationMetadata.ParameterMetadata.ParameterType> expectedMetadataTypes = ImmutableList.of(AggregationMetadata.ParameterMetadata.ParameterType.INPUT_CHANNEL, AggregationMetadata.ParameterMetadata.ParameterType.STATE); assertTrue(implementation.getInputParameterMetadataTypes().equals(expectedMetadataTypes)); }
@Test public void testCustomStateSerializerAggregationParse() { ParametricAggregation aggregation = parseFunctionDefinition(CustomStateSerializerAggregationFunction.class); AggregationImplementation implementation = getOnlyElement(aggregation.getImplementations().getExactImplementations().values()); assertTrue(implementation.getStateSerializerFactory().isPresent()); InternalAggregationFunction specialized = aggregation.specialize(BoundVariables.builder().build(), 1, new TypeRegistry(), null); AccumulatorStateSerializer<?> createdSerializer = getOnlyElement(((LazyAccumulatorFactoryBinder) specialized.getAccumulatorFactoryBinder()) .getGenericAccumulatorFactoryBinder().getStateDescriptors()).getSerializer(); Class<?> serializerFactory = implementation.getStateSerializerFactory().get().type().returnType(); assertTrue(serializerFactory.isInstance(createdSerializer)); }
@Test public void testFixedTypeParameterInjectionAggregateFunctionParse() { Signature expectedSignature = new Signature( "fixed_type_parameter_injection", FunctionKind.AGGREGATE, ImmutableList.of(), ImmutableList.of(), DoubleType.DOUBLE.getTypeSignature(), ImmutableList.of(DoubleType.DOUBLE.getTypeSignature()), false); ParametricAggregation aggregation = parseFunctionDefinition(FixedTypeParameterInjectionAggregateFunction.class); assertEquals(aggregation.getDescription(), "Simple aggregate with fixed parameter type injected"); assertTrue(aggregation.isDeterministic()); assertEquals(aggregation.getSignature(), expectedSignature); ParametricImplementationsGroup<AggregationImplementation> implementations = aggregation.getImplementations(); assertImplementationCount(implementations, 1, 0, 0); AggregationImplementation implementationDouble = implementations.getExactImplementations().get(expectedSignature); assertFalse(implementationDouble.getStateSerializerFactory().isPresent()); assertEquals(implementationDouble.getDefinitionClass(), FixedTypeParameterInjectionAggregateFunction.class); assertDependencyCount(implementationDouble, 1, 1, 1); assertFalse(implementationDouble.hasSpecializedTypeParameters()); List<AggregationMetadata.ParameterMetadata.ParameterType> expectedMetadataTypes = ImmutableList.of(AggregationMetadata.ParameterMetadata.ParameterType.STATE, AggregationMetadata.ParameterMetadata.ParameterType.INPUT_CHANNEL); assertTrue(implementationDouble.getInputParameterMetadataTypes().equals(expectedMetadataTypes)); assertEquals(implementationDouble.getStateClass(), NullableDoubleState.class); }
@Test public void testNotDecomposableAggregationParse() { Signature expectedSignature = new Signature( "custom_decomposable_aggregate", FunctionKind.AGGREGATE, DoubleType.DOUBLE.getTypeSignature(), ImmutableList.of(DoubleType.DOUBLE.getTypeSignature())); ParametricAggregation aggregation = parseFunctionDefinition(NotDecomposableAggregationFunction.class); assertEquals(aggregation.getDescription(), "Aggregate with Decomposable=false"); assertTrue(aggregation.isDeterministic()); assertEquals(aggregation.getSignature(), expectedSignature); InternalAggregationFunction specialized = aggregation.specialize(BoundVariables.builder().build(), 1, new TypeRegistry(), null); assertEquals(specialized.getFinalType(), DoubleType.DOUBLE); assertFalse(specialized.isDecomposable()); assertEquals(specialized.name(), "custom_decomposable_aggregate"); }
@Test public void testSimpleExactAggregationParse() { Signature expectedSignature = new Signature( "simple_exact_aggregate", FunctionKind.AGGREGATE, DoubleType.DOUBLE.getTypeSignature(), ImmutableList.of(DoubleType.DOUBLE.getTypeSignature())); ParametricAggregation aggregation = parseFunctionDefinition(ExactAggregationFunction.class); assertEquals(aggregation.getDescription(), "Simple exact aggregate description"); assertTrue(aggregation.isDeterministic()); assertEquals(aggregation.getSignature(), expectedSignature); ParametricImplementationsGroup<AggregationImplementation> implementations = aggregation.getImplementations(); assertImplementationCount(implementations, 1, 0, 0); AggregationImplementation implementation = getOnlyElement(implementations.getExactImplementations().values()); assertFalse(implementation.getStateSerializerFactory().isPresent()); assertEquals(implementation.getDefinitionClass(), ExactAggregationFunction.class); assertDependencyCount(implementation, 0, 0, 0); assertFalse(implementation.hasSpecializedTypeParameters()); List<AggregationMetadata.ParameterMetadata.ParameterType> expectedMetadataTypes = ImmutableList.of(AggregationMetadata.ParameterMetadata.ParameterType.STATE, AggregationMetadata.ParameterMetadata.ParameterType.INPUT_CHANNEL); assertTrue(implementation.getInputParameterMetadataTypes().equals(expectedMetadataTypes)); InternalAggregationFunction specialized = aggregation.specialize(BoundVariables.builder().build(), 1, new TypeRegistry(), null); assertEquals(specialized.getFinalType(), DoubleType.DOUBLE); assertTrue(specialized.isDecomposable()); assertEquals(specialized.name(), "simple_exact_aggregate"); }
@Test public void testSimpleBlockInputAggregationParse() { Signature expectedSignature = new Signature( "block_input_aggregate", FunctionKind.AGGREGATE, DoubleType.DOUBLE.getTypeSignature(), ImmutableList.of(DoubleType.DOUBLE.getTypeSignature())); ParametricAggregation aggregation = parseFunctionDefinition(BlockInputAggregationFunction.class); assertEquals(aggregation.getDescription(), "Simple aggregate with @BlockPosition usage"); assertTrue(aggregation.isDeterministic()); assertEquals(aggregation.getSignature(), expectedSignature); ParametricImplementationsGroup<AggregationImplementation> implementations = aggregation.getImplementations(); assertImplementationCount(implementations, 1, 0, 0); AggregationImplementation implementation = getOnlyElement(implementations.getExactImplementations().values()); assertFalse(implementation.getStateSerializerFactory().isPresent()); assertEquals(implementation.getDefinitionClass(), BlockInputAggregationFunction.class); assertDependencyCount(implementation, 0, 0, 0); assertFalse(implementation.hasSpecializedTypeParameters()); List<AggregationMetadata.ParameterMetadata.ParameterType> expectedMetadataTypes = ImmutableList.of(AggregationMetadata.ParameterMetadata.ParameterType.STATE, AggregationMetadata.ParameterMetadata.ParameterType.BLOCK_INPUT_CHANNEL, AggregationMetadata.ParameterMetadata.ParameterType.BLOCK_INDEX); assertEquals(implementation.getInputParameterMetadataTypes(), expectedMetadataTypes); InternalAggregationFunction specialized = aggregation.specialize(BoundVariables.builder().build(), 1, new TypeRegistry(), null); assertEquals(specialized.getFinalType(), DoubleType.DOUBLE); assertTrue(specialized.isDecomposable()); assertEquals(specialized.name(), "block_input_aggregate"); }
ImmutableList.of(DoubleType.DOUBLE.getTypeSignature())); ParametricAggregation aggregation = parseFunctionDefinition(InjectOperatorAggregateFunction.class); assertEquals(aggregation.getDescription(), "Simple aggregate with operator injected"); assertTrue(aggregation.isDeterministic());
ImmutableList.of(parseTypeSignature("varchar(x)", ImmutableSet.of("x")))); ParametricAggregation aggregation = parseFunctionDefinition(InjectLiteralAggregateFunction.class); assertEquals(aggregation.getDescription(), "Simple aggregate with type literal"); assertTrue(aggregation.isDeterministic());
false); ParametricAggregation aggregation = parseFunctionDefinition(GenericAggregationFunction.class); assertEquals(aggregation.getDescription(), "Simple aggregate with two generic implementations"); assertTrue(aggregation.isDeterministic());
false); ParametricAggregation aggregation = parseFunctionDefinition(InjectTypeAggregateFunction.class); assertEquals(aggregation.getDescription(), "Simple aggregate with type injected"); assertTrue(aggregation.isDeterministic());
false); ParametricAggregation aggregation = parseFunctionDefinition(PartiallyFixedTypeParameterInjectionAggregateFunction.class); assertEquals(aggregation.getDescription(), "Simple aggregate with fixed parameter type injected"); assertTrue(aggregation.isDeterministic());
public void testSimpleExplicitSpecializedAggregationParse() { Signature expectedSignature = new Signature( "explicit_specialized_aggregate", FunctionKind.AGGREGATE, ImmutableList.of(typeVariable("T")), ImmutableList.of(), parseTypeSignature("T"), ImmutableList.of(new TypeSignature(ARRAY, TypeSignatureParameter.of(parseTypeSignature("T")))), false); ParametricAggregation aggregation = parseFunctionDefinition(ExplicitSpecializedAggregationFunction.class); assertEquals(aggregation.getDescription(), "Simple explicit specialized aggregate"); assertTrue(aggregation.isDeterministic()); assertEquals(aggregation.getSignature(), expectedSignature); ParametricImplementationsGroup<AggregationImplementation> implementations = aggregation.getImplementations(); assertImplementationCount(implementations, 0, 1, 1); AggregationImplementation implementation1 = implementations.getSpecializedImplementations().get(0); assertTrue(implementation1.hasSpecializedTypeParameters()); assertFalse(implementation1.hasSpecializedTypeParameters()); List<AggregationMetadata.ParameterMetadata.ParameterType> expectedMetadataTypes = ImmutableList.of(AggregationMetadata.ParameterMetadata.ParameterType.STATE, AggregationMetadata.ParameterMetadata.ParameterType.INPUT_CHANNEL); assertTrue(implementation1.getInputParameterMetadataTypes().equals(expectedMetadataTypes)); AggregationImplementation implementation2 = implementations.getSpecializedImplementations().get(1); assertTrue(implementation2.hasSpecializedTypeParameters()); assertFalse(implementation2.hasSpecializedTypeParameters()); assertTrue(implementation2.getInputParameterMetadataTypes().equals(expectedMetadataTypes)); InternalAggregationFunction specialized = aggregation.specialize(BoundVariables.builder().setTypeVariable("T", DoubleType.DOUBLE).build(), 1, new TypeRegistry(), null); assertEquals(specialized.getFinalType(), DoubleType.DOUBLE); assertTrue(specialized.isDecomposable()); assertEquals(specialized.name(), "implicit_specialized_aggregate"); }
public void testSimpleImplicitSpecializedAggregationParse() { Signature expectedSignature = new Signature( "implicit_specialized_aggregate", FunctionKind.AGGREGATE, ImmutableList.of(typeVariable("T")), ImmutableList.of(), parseTypeSignature("T"), ImmutableList.of(new TypeSignature(ARRAY, TypeSignatureParameter.of(parseTypeSignature("T"))), parseTypeSignature("T")), false); ParametricAggregation aggregation = parseFunctionDefinition(ImplicitSpecializedAggregationFunction.class); assertEquals(aggregation.getDescription(), "Simple implicit specialized aggregate"); assertTrue(aggregation.isDeterministic()); assertEquals(aggregation.getSignature(), expectedSignature); ParametricImplementationsGroup<AggregationImplementation> implementations = aggregation.getImplementations(); assertImplementationCount(implementations, 0, 0, 2); AggregationImplementation implementation1 = implementations.getSpecializedImplementations().get(0); assertTrue(implementation1.hasSpecializedTypeParameters()); assertFalse(implementation1.hasSpecializedTypeParameters()); List<AggregationMetadata.ParameterMetadata.ParameterType> expectedMetadataTypes = ImmutableList.of(AggregationMetadata.ParameterMetadata.ParameterType.STATE, AggregationMetadata.ParameterMetadata.ParameterType.INPUT_CHANNEL, AggregationMetadata.ParameterMetadata.ParameterType.INPUT_CHANNEL); assertTrue(implementation1.getInputParameterMetadataTypes().equals(expectedMetadataTypes)); AggregationImplementation implementation2 = implementations.getSpecializedImplementations().get(1); assertTrue(implementation2.hasSpecializedTypeParameters()); assertFalse(implementation2.hasSpecializedTypeParameters()); assertTrue(implementation2.getInputParameterMetadataTypes().equals(expectedMetadataTypes)); InternalAggregationFunction specialized = aggregation.specialize(BoundVariables.builder().setTypeVariable("T", DoubleType.DOUBLE).build(), 1, new TypeRegistry(), null); assertEquals(specialized.getFinalType(), DoubleType.DOUBLE); assertTrue(specialized.isDecomposable()); assertEquals(specialized.name(), "implicit_specialized_aggregate"); }
false); ParametricAggregation aggregation = parseFunctionDefinition(LongConstraintAggregateFunction.class); assertEquals(aggregation.getDescription(), "Parametric aggregate with parametric type returned"); assertTrue(aggregation.isDeterministic());