common_function/scalars/
math.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
15mod clamp;
16mod modulo;
17mod pow;
18mod rate;
19
20use std::fmt;
21use std::sync::Arc;
22
23pub use clamp::ClampFunction;
24use common_query::error::{GeneralDataFusionSnafu, Result};
25use common_query::prelude::Signature;
26use datafusion::error::DataFusionError;
27use datafusion::logical_expr::Volatility;
28use datatypes::prelude::ConcreteDataType;
29use datatypes::vectors::VectorRef;
30pub use pow::PowFunction;
31pub use rate::RateFunction;
32use snafu::ResultExt;
33
34use crate::function::{Function, FunctionContext};
35use crate::function_registry::FunctionRegistry;
36use crate::scalars::math::modulo::ModuloFunction;
37
38pub(crate) struct MathFunction;
39
40impl MathFunction {
41    pub fn register(registry: &FunctionRegistry) {
42        registry.register(Arc::new(ModuloFunction));
43        registry.register(Arc::new(PowFunction));
44        registry.register(Arc::new(RateFunction));
45        registry.register(Arc::new(RangeFunction));
46        registry.register(Arc::new(ClampFunction));
47    }
48}
49
50/// `RangeFunction` will never be used as a normal function,
51/// just for datafusion to generate logical plan for RangeSelect
52#[derive(Clone, Debug, Default)]
53struct RangeFunction;
54
55impl fmt::Display for RangeFunction {
56    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
57        write!(f, "RANGE_FN")
58    }
59}
60
61impl Function for RangeFunction {
62    fn name(&self) -> &str {
63        "range_fn"
64    }
65
66    // The first argument to range_fn is the expression to be evaluated
67    fn return_type(&self, input_types: &[ConcreteDataType]) -> Result<ConcreteDataType> {
68        input_types
69            .first()
70            .cloned()
71            .ok_or(DataFusionError::Internal(
72                "No expr found in range_fn".into(),
73            ))
74            .context(GeneralDataFusionSnafu)
75    }
76
77    /// `range_fn` will never been used. As long as a legal signature is returned, the specific content of the signature does not matter.
78    /// In fact, the arguments loaded by `range_fn` are very complicated, and it is difficult to use `Signature` to describe
79    fn signature(&self) -> Signature {
80        Signature::variadic_any(Volatility::Immutable)
81    }
82
83    fn eval(&self, _func_ctx: &FunctionContext, _columns: &[VectorRef]) -> Result<VectorRef> {
84        Err(DataFusionError::Internal(
85            "range_fn just a empty function used in range select, It should not be eval!".into(),
86        ))
87        .context(GeneralDataFusionSnafu)
88    }
89}