private AggregateWindowFunction(InternalAggregationFunction function, List<Integer> argumentChannels) { this.argumentChannels = ImmutableList.copyOf(argumentChannels); this.accumulatorFactory = function.bind(createArgs(function), Optional.empty()); }
private static List<Integer> createArgs(InternalAggregationFunction function) { ImmutableList.Builder<Integer> list = ImmutableList.builder(); for (int i = 0; i < function.getParameterTypes().size(); i++) { list.add(i); } return list.build(); } }
private static InternalAggregationFunction generateAggregation(Type type) { DynamicClassLoader classLoader = new DynamicClassLoader(ChecksumAggregationFunction.class.getClassLoader()); List<Type> inputTypes = ImmutableList.of(type); AggregationMetadata metadata = new AggregationMetadata( generateAggregationName(NAME, type.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createInputParameterMetadata(type), INPUT_FUNCTION.bindTo(type), COMBINE_FUNCTION, OUTPUT_FUNCTION, ImmutableList.of(new AccumulatorStateDescriptor( NullableLongState.class, StateCompiler.generateStateSerializer(NullableLongState.class, classLoader), StateCompiler.generateStateFactory(NullableLongState.class, classLoader))), VARBINARY); GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader); return new InternalAggregationFunction(NAME, inputTypes, ImmutableList.of(BIGINT), VARBINARY, true, false, factory); }
@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"); }
new TypeRegistry(), null); assertEquals(specialized.getFinalType(), DoubleType.DOUBLE); assertTrue(specialized.getParameterTypes().equals(ImmutableList.of(DoubleType.DOUBLE))); assertTrue(specialized.isDecomposable()); assertEquals(specialized.name(), "partially_fixed_type_parameter_injection");
public Parts createPartialAggregations(SymbolAllocator symbolAllocator, FunctionRegistry functionRegistry) { 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(); Signature signature = originalAggregation.getSignature(); InternalAggregationFunction function = functionRegistry.getAggregateFunctionImplementation(signature); Symbol partialSymbol = symbolAllocator.newSymbol(signature.getName(), function.getIntermediateType()); mappings.put(entry.getKey(), partialSymbol); partialAggregation.put(partialSymbol, new Aggregation(originalAggregation.getCall(), signature, originalAggregation.getMask())); finalAggregation.put(entry.getKey(), new Aggregation( new FunctionCall(QualifiedName.of(signature.getName()), ImmutableList.of(partialSymbol.toSymbolReference())), signature, Optional.empty())); } groupingSymbols.forEach(symbol -> mappings.put(symbol, symbol)); return new Parts( new StatisticAggregations(partialAggregation.build(), groupingSymbols), new StatisticAggregations(finalAggregation.build(), groupingSymbols), mappings.build()); }
@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 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"); }
new TypeRegistry(), null); assertEquals(specialized.getFinalType(), DoubleType.DOUBLE); assertTrue(specialized.getParameterTypes().equals(ImmutableList.of(DoubleType.DOUBLE))); assertTrue(specialized.isDecomposable()); assertEquals(specialized.name(), "partially_fixed_type_parameter_injection");
public Parts createPartialAggregations(SymbolAllocator symbolAllocator, FunctionRegistry functionRegistry) { 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(); Signature signature = originalAggregation.getSignature(); InternalAggregationFunction function = functionRegistry.getAggregateFunctionImplementation(signature); Symbol partialSymbol = symbolAllocator.newSymbol(signature.getName(), function.getIntermediateType()); mappings.put(entry.getKey(), partialSymbol); partialAggregation.put(partialSymbol, new Aggregation(originalAggregation.getCall(), signature, originalAggregation.getMask())); finalAggregation.put(entry.getKey(), new Aggregation( new FunctionCall(QualifiedName.of(signature.getName()), ImmutableList.of(partialSymbol.toSymbolReference())), signature, Optional.empty())); } groupingSymbols.forEach(symbol -> mappings.put(symbol, symbol)); return new Parts( new StatisticAggregations(partialAggregation.build(), groupingSymbols), new StatisticAggregations(finalAggregation.build(), groupingSymbols), mappings.build()); }
@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)); }
private AggregateWindowFunction(InternalAggregationFunction function, List<Integer> argumentChannels) { this.argumentChannels = ImmutableList.copyOf(argumentChannels); this.accumulatorFactory = function.bind(createArgs(function), Optional.empty()); }
@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"); }
new TypeRegistry(), null); assertEquals(specialized.getFinalType(), DoubleType.DOUBLE); assertTrue(specialized.getParameterTypes().equals(ImmutableList.of(DoubleType.DOUBLE))); assertTrue(specialized.isDecomposable()); assertEquals(specialized.name(), "simple_generic_implementations");
private static List<Integer> createArgs(InternalAggregationFunction function) { ImmutableList.Builder<Integer> list = ImmutableList.builder(); for (int i = 0; i < function.getParameterTypes().size(); i++) { list.add(i); } return list.build(); } }
private static InternalAggregationFunction generateAggregation(Type type) { DynamicClassLoader classLoader = new DynamicClassLoader(ChecksumAggregationFunction.class.getClassLoader()); List<Type> inputTypes = ImmutableList.of(type); AggregationMetadata metadata = new AggregationMetadata( generateAggregationName(NAME, type.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createInputParameterMetadata(type), INPUT_FUNCTION.bindTo(type), COMBINE_FUNCTION, OUTPUT_FUNCTION, ImmutableList.of(new AccumulatorStateDescriptor( NullableLongState.class, StateCompiler.generateStateSerializer(NullableLongState.class, classLoader), StateCompiler.generateStateFactory(NullableLongState.class, classLoader))), VARBINARY); GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader); return new InternalAggregationFunction(NAME, inputTypes, ImmutableList.of(BIGINT), VARBINARY, true, false, factory); }
Signature signature = originalAggregation.getSignature(); InternalAggregationFunction function = functionRegistry.getAggregateFunctionImplementation(signature); Symbol intermediateSymbol = context.getSymbolAllocator().newSymbol(signature.getName(), function.getIntermediateType());
@Override protected List<? extends OperatorFactory> createOperatorFactories() { OperatorFactory tableScanOperator = createTableScanOperator(0, new PlanNodeId("test"), "orders", "orderkey"); InternalAggregationFunction countFunction = localQueryRunner.getMetadata().getFunctionRegistry().getAggregateFunctionImplementation( new Signature("count", AGGREGATE, BIGINT.getTypeSignature())); AggregationOperatorFactory aggregationOperator = new AggregationOperatorFactory(1, new PlanNodeId("test"), Step.SINGLE, ImmutableList.of(countFunction.bind(ImmutableList.of(0), Optional.empty())), false); return ImmutableList.of(tableScanOperator, aggregationOperator); }
@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"); }
new TypeRegistry(), null); assertEquals(specialized.getFinalType(), DoubleType.DOUBLE); assertTrue(specialized.getParameterTypes().equals(ImmutableList.of(DoubleType.DOUBLE))); assertTrue(specialized.isDecomposable()); assertEquals(specialized.name(), "simple_generic_implementations");