Skip to content

Commit 5b6b404

Browse files
authored
Add a version() UDF (#12429)
* Add a "version()" UDF * add "Apache" * fix rustdoc * add scalar_functions.md entry
1 parent f7efd2d commit 5b6b404

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

datafusion/functions/src/core/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub mod nvl;
3131
pub mod nvl2;
3232
pub mod planner;
3333
pub mod r#struct;
34+
pub mod version;
3435

3536
// create UDFs
3637
make_udf_function!(arrow_cast::ArrowCastFunc, ARROW_CAST, arrow_cast);
@@ -42,6 +43,7 @@ make_udf_function!(r#struct::StructFunc, STRUCT, r#struct);
4243
make_udf_function!(named_struct::NamedStructFunc, NAMED_STRUCT, named_struct);
4344
make_udf_function!(getfield::GetFieldFunc, GET_FIELD, get_field);
4445
make_udf_function!(coalesce::CoalesceFunc, COALESCE, coalesce);
46+
make_udf_function!(version::VersionFunc, VERSION, version);
4547

4648
pub mod expr_fn {
4749
use datafusion_expr::{Expr, Literal};
@@ -104,5 +106,6 @@ pub fn functions() -> Vec<Arc<ScalarUDF>> {
104106
// calls to `get_field`
105107
get_field(),
106108
coalesce(),
109+
version(),
107110
]
108111
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
//! [`VersionFunc`]: Implementation of the `version` function.
19+
20+
use std::any::Any;
21+
22+
use arrow::datatypes::DataType;
23+
use datafusion_common::{not_impl_err, plan_err, Result, ScalarValue};
24+
use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility};
25+
26+
#[derive(Debug)]
27+
pub struct VersionFunc {
28+
signature: Signature,
29+
}
30+
31+
impl Default for VersionFunc {
32+
fn default() -> Self {
33+
Self::new()
34+
}
35+
}
36+
37+
impl VersionFunc {
38+
pub fn new() -> Self {
39+
Self {
40+
signature: Signature::exact(vec![], Volatility::Immutable),
41+
}
42+
}
43+
}
44+
45+
impl ScalarUDFImpl for VersionFunc {
46+
fn as_any(&self) -> &dyn Any {
47+
self
48+
}
49+
50+
fn name(&self) -> &str {
51+
"version"
52+
}
53+
54+
fn signature(&self) -> &Signature {
55+
&self.signature
56+
}
57+
58+
fn return_type(&self, args: &[DataType]) -> Result<DataType> {
59+
if args.is_empty() {
60+
Ok(DataType::Utf8)
61+
} else {
62+
plan_err!("version expects no arguments")
63+
}
64+
}
65+
66+
fn invoke(&self, _: &[ColumnarValue]) -> Result<ColumnarValue> {
67+
not_impl_err!("version does not take any arguments")
68+
}
69+
70+
fn invoke_no_args(&self, _: usize) -> Result<ColumnarValue> {
71+
// TODO it would be great to add rust version and arrow version,
72+
// but that requires a `build.rs` script and/or adding a version const to arrow-rs
73+
let version = format!(
74+
"Apache DataFusion {}, {} on {}",
75+
env!("CARGO_PKG_VERSION"),
76+
std::env::consts::ARCH,
77+
std::env::consts::OS,
78+
);
79+
Ok(ColumnarValue::Scalar(ScalarValue::Utf8(Some(version))))
80+
}
81+
}
82+
83+
#[cfg(test)]
84+
mod test {
85+
use super::*;
86+
use datafusion_expr::ScalarUDF;
87+
88+
#[tokio::test]
89+
async fn test_version_udf() {
90+
let version_udf = ScalarUDF::from(VersionFunc::new());
91+
let version = version_udf.invoke_no_args(0).unwrap();
92+
93+
if let ColumnarValue::Scalar(ScalarValue::Utf8(Some(version))) = version {
94+
assert!(version.starts_with("Apache DataFusion"));
95+
} else {
96+
panic!("Expected version string");
97+
}
98+
}
99+
}

docs/source/user-guide/sql/scalar_functions.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3917,6 +3917,7 @@ sha512(expression)
39173917

39183918
- [arrow_cast](#arrow_cast)
39193919
- [arrow_typeof](#arrow_typeof)
3920+
- [version](#version)
39203921

39213922
### `arrow_cast`
39223923

@@ -3975,3 +3976,22 @@ arrow_typeof(expression)
39753976
+---------------------------+------------------------+
39763977
1 row in set. Query took 0.001 seconds.
39773978
```
3979+
3980+
### `version`
3981+
3982+
Returns the version of DataFusion.
3983+
3984+
```
3985+
version()
3986+
```
3987+
3988+
#### Example
3989+
3990+
```
3991+
> select version();
3992+
+--------------------------------------------+
3993+
| version() |
3994+
+--------------------------------------------+
3995+
| Apache DataFusion 41.0.0, aarch64 on macos |
3996+
+--------------------------------------------+
3997+
```

0 commit comments

Comments
 (0)