common_function/scalars/
aggregate.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! # Deprecate Warning:
16//!
17//! This module is deprecated and will be removed in the future.
18//! All UDAF implementation here are not maintained and should
19//! not be used before they are refactored into the `src/aggr`
20//! version.
21
22use std::sync::Arc;
23
24use common_query::logical_plan::AggregateFunctionCreatorRef;
25
26use crate::function_registry::FunctionRegistry;
27use crate::scalars::vector::product::VectorProductCreator;
28use crate::scalars::vector::sum::VectorSumCreator;
29
30/// A function creates `AggregateFunctionCreator`.
31/// "Aggregator" *is* AggregatorFunction. Since the later one is long, we named an short alias for it.
32/// The two names might be used interchangeably.
33type AggregatorCreatorFunction = Arc<dyn Fn() -> AggregateFunctionCreatorRef + Send + Sync>;
34
35/// `AggregateFunctionMeta` dynamically creates AggregateFunctionCreator.
36#[derive(Clone)]
37pub struct AggregateFunctionMeta {
38    name: String,
39    args_count: u8,
40    creator: AggregatorCreatorFunction,
41}
42
43pub type AggregateFunctionMetaRef = Arc<AggregateFunctionMeta>;
44
45impl AggregateFunctionMeta {
46    pub fn new(name: &str, args_count: u8, creator: AggregatorCreatorFunction) -> Self {
47        Self {
48            name: name.to_string(),
49            args_count,
50            creator,
51        }
52    }
53
54    pub fn name(&self) -> String {
55        self.name.to_string()
56    }
57
58    pub fn args_count(&self) -> u8 {
59        self.args_count
60    }
61
62    pub fn create(&self) -> AggregateFunctionCreatorRef {
63        (self.creator)()
64    }
65}
66
67pub(crate) struct AggregateFunctions;
68
69impl AggregateFunctions {
70    pub fn register(registry: &FunctionRegistry) {
71        registry.register_aggregate_function(Arc::new(AggregateFunctionMeta::new(
72            "vec_sum",
73            1,
74            Arc::new(|| Arc::new(VectorSumCreator::default())),
75        )));
76        registry.register_aggregate_function(Arc::new(AggregateFunctionMeta::new(
77            "vec_product",
78            1,
79            Arc::new(|| Arc::new(VectorProductCreator::default())),
80        )));
81
82        #[cfg(feature = "geo")]
83        registry.register_aggregate_function(Arc::new(AggregateFunctionMeta::new(
84            "json_encode_path",
85            3,
86            Arc::new(|| Arc::new(super::geo::encoding::JsonPathEncodeFunctionCreator::default())),
87        )));
88    }
89}