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