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