common_function/system/
build.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
15use std::fmt;
16use std::sync::Arc;
17
18use common_query::error::Result;
19use common_query::prelude::{Signature, Volatility};
20use datatypes::prelude::*;
21use datatypes::vectors::{StringVector, VectorRef};
22
23use crate::function::{Function, FunctionContext};
24
25/// Generates build information
26#[derive(Clone, Debug, Default)]
27pub struct BuildFunction;
28
29impl fmt::Display for BuildFunction {
30    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31        write!(f, "BUILD")
32    }
33}
34
35impl Function for BuildFunction {
36    fn name(&self) -> &str {
37        "build"
38    }
39
40    fn return_type(&self, _input_types: &[ConcreteDataType]) -> Result<ConcreteDataType> {
41        Ok(ConcreteDataType::string_datatype())
42    }
43
44    fn signature(&self) -> Signature {
45        Signature::nullary(Volatility::Immutable)
46    }
47
48    fn eval(&self, _func_ctx: &FunctionContext, _columns: &[VectorRef]) -> Result<VectorRef> {
49        let build_info = common_version::build_info().to_string();
50        let v = Arc::new(StringVector::from(vec![build_info]));
51        Ok(v)
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use std::sync::Arc;
58
59    use super::*;
60    #[test]
61    fn test_build_function() {
62        let build = BuildFunction;
63        assert_eq!("build", build.name());
64        assert_eq!(
65            ConcreteDataType::string_datatype(),
66            build.return_type(&[]).unwrap()
67        );
68        assert_eq!(build.signature(), Signature::nullary(Volatility::Immutable));
69        let build_info = common_version::build_info().to_string();
70        let vector = build.eval(&FunctionContext::default(), &[]).unwrap();
71        let expect: VectorRef = Arc::new(StringVector::from(vec![build_info]));
72        assert_eq!(expect, vector);
73    }
74}