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 datafusion::arrow::datatypes::DataType;
16use datafusion_common::ScalarValue;
17use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, Signature, Volatility};
18use session::context::Channel;
19
20use crate::function::{Function, find_function_context};
21use crate::system::define_nullary_udf;
22
23define_nullary_udf!(VersionFunction);
24
25impl Function for VersionFunction {
26    fn name(&self) -> &str {
27        "version"
28    }
29
30    fn return_type(&self, _: &[DataType]) -> datafusion_common::Result<DataType> {
31        Ok(DataType::Utf8View)
32    }
33
34    fn signature(&self) -> &Signature {
35        &self.signature
36    }
37
38    fn invoke_with_args(
39        &self,
40        args: ScalarFunctionArgs,
41    ) -> datafusion_common::Result<ColumnarValue> {
42        let func_ctx = find_function_context(&args)?;
43        let version = match func_ctx.query_ctx.channel() {
44            Channel::Mysql => {
45                format!(
46                    "{}-greptimedb-{}",
47                    std::env::var("GREPTIMEDB_MYSQL_SERVER_VERSION")
48                        .unwrap_or_else(|_| "8.4.2".to_string()),
49                    common_version::version()
50                )
51            }
52            Channel::Postgres => {
53                format!("16.3-greptimedb-{}", common_version::version())
54            }
55            _ => common_version::version().to_string(),
56        };
57        Ok(ColumnarValue::Scalar(ScalarValue::Utf8View(Some(version))))
58    }
59}