common_function/system/
version.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::sync::Arc;
16use std::{env, fmt};
17
18use common_query::error::Result;
19use common_query::prelude::{Signature, Volatility};
20use datatypes::data_type::ConcreteDataType;
21use datatypes::vectors::{StringVector, VectorRef};
22use session::context::Channel;
23
24use crate::function::{Function, FunctionContext};
25
26#[derive(Clone, Debug, Default)]
27pub(crate) struct VersionFunction;
28
29impl fmt::Display for VersionFunction {
30    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31        write!(f, "VERSION")
32    }
33}
34
35impl Function for VersionFunction {
36    fn name(&self) -> &str {
37        "version"
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 version = match func_ctx.query_ctx.channel() {
50            Channel::Mysql => {
51                format!(
52                    "{}-greptimedb-{}",
53                    std::env::var("GREPTIMEDB_MYSQL_SERVER_VERSION")
54                        .unwrap_or_else(|_| "8.4.2".to_string()),
55                    env!("CARGO_PKG_VERSION")
56                )
57            }
58            Channel::Postgres => {
59                format!("16.3-greptimedb-{}", env!("CARGO_PKG_VERSION"))
60            }
61            _ => env!("CARGO_PKG_VERSION").to_string(),
62        };
63        let result = StringVector::from(vec![version]);
64        Ok(Arc::new(result))
65    }
66}