This is an automated email from the ASF dual-hosted git repository.

alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new 5b6b404b9f Add a `version()` UDF (#12429)
5b6b404b9f is described below

commit 5b6b404b9fbccb8584e8c5d2f1c366f3de736f31
Author: Samuel Colvin <[email protected]>
AuthorDate: Thu Sep 12 11:29:16 2024 +0100

    Add a `version()` UDF (#12429)
    
    * Add a "version()" UDF
    
    * add "Apache"
    
    * fix rustdoc
    
    * add scalar_functions.md entry
---
 datafusion/functions/src/core/mod.rs           |  3 +
 datafusion/functions/src/core/version.rs       | 99 ++++++++++++++++++++++++++
 docs/source/user-guide/sql/scalar_functions.md | 20 ++++++
 3 files changed, 122 insertions(+)

diff --git a/datafusion/functions/src/core/mod.rs 
b/datafusion/functions/src/core/mod.rs
index af340930ea..1c69f9c9b2 100644
--- a/datafusion/functions/src/core/mod.rs
+++ b/datafusion/functions/src/core/mod.rs
@@ -31,6 +31,7 @@ pub mod nvl;
 pub mod nvl2;
 pub mod planner;
 pub mod r#struct;
+pub mod version;
 
 // create UDFs
 make_udf_function!(arrow_cast::ArrowCastFunc, ARROW_CAST, arrow_cast);
@@ -42,6 +43,7 @@ make_udf_function!(r#struct::StructFunc, STRUCT, r#struct);
 make_udf_function!(named_struct::NamedStructFunc, NAMED_STRUCT, named_struct);
 make_udf_function!(getfield::GetFieldFunc, GET_FIELD, get_field);
 make_udf_function!(coalesce::CoalesceFunc, COALESCE, coalesce);
+make_udf_function!(version::VersionFunc, VERSION, version);
 
 pub mod expr_fn {
     use datafusion_expr::{Expr, Literal};
@@ -104,5 +106,6 @@ pub fn functions() -> Vec<Arc<ScalarUDF>> {
         // calls to `get_field`
         get_field(),
         coalesce(),
+        version(),
     ]
 }
diff --git a/datafusion/functions/src/core/version.rs 
b/datafusion/functions/src/core/version.rs
new file mode 100644
index 0000000000..212349e689
--- /dev/null
+++ b/datafusion/functions/src/core/version.rs
@@ -0,0 +1,99 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! [`VersionFunc`]: Implementation of the `version` function.
+
+use std::any::Any;
+
+use arrow::datatypes::DataType;
+use datafusion_common::{not_impl_err, plan_err, Result, ScalarValue};
+use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility};
+
+#[derive(Debug)]
+pub struct VersionFunc {
+    signature: Signature,
+}
+
+impl Default for VersionFunc {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl VersionFunc {
+    pub fn new() -> Self {
+        Self {
+            signature: Signature::exact(vec![], Volatility::Immutable),
+        }
+    }
+}
+
+impl ScalarUDFImpl for VersionFunc {
+    fn as_any(&self) -> &dyn Any {
+        self
+    }
+
+    fn name(&self) -> &str {
+        "version"
+    }
+
+    fn signature(&self) -> &Signature {
+        &self.signature
+    }
+
+    fn return_type(&self, args: &[DataType]) -> Result<DataType> {
+        if args.is_empty() {
+            Ok(DataType::Utf8)
+        } else {
+            plan_err!("version expects no arguments")
+        }
+    }
+
+    fn invoke(&self, _: &[ColumnarValue]) -> Result<ColumnarValue> {
+        not_impl_err!("version does not take any arguments")
+    }
+
+    fn invoke_no_args(&self, _: usize) -> Result<ColumnarValue> {
+        // TODO it would be great to add rust version and arrow version,
+        // but that requires a `build.rs` script and/or adding a version const 
to arrow-rs
+        let version = format!(
+            "Apache DataFusion {}, {} on {}",
+            env!("CARGO_PKG_VERSION"),
+            std::env::consts::ARCH,
+            std::env::consts::OS,
+        );
+        Ok(ColumnarValue::Scalar(ScalarValue::Utf8(Some(version))))
+    }
+}
+
+#[cfg(test)]
+mod test {
+    use super::*;
+    use datafusion_expr::ScalarUDF;
+
+    #[tokio::test]
+    async fn test_version_udf() {
+        let version_udf = ScalarUDF::from(VersionFunc::new());
+        let version = version_udf.invoke_no_args(0).unwrap();
+
+        if let ColumnarValue::Scalar(ScalarValue::Utf8(Some(version))) = 
version {
+            assert!(version.starts_with("Apache DataFusion"));
+        } else {
+            panic!("Expected version string");
+        }
+    }
+}
diff --git a/docs/source/user-guide/sql/scalar_functions.md 
b/docs/source/user-guide/sql/scalar_functions.md
index e08524dcd3..14852935dc 100644
--- a/docs/source/user-guide/sql/scalar_functions.md
+++ b/docs/source/user-guide/sql/scalar_functions.md
@@ -3917,6 +3917,7 @@ sha512(expression)
 
 - [arrow_cast](#arrow_cast)
 - [arrow_typeof](#arrow_typeof)
+- [version](#version)
 
 ### `arrow_cast`
 
@@ -3975,3 +3976,22 @@ arrow_typeof(expression)
 +---------------------------+------------------------+
 1 row in set. Query took 0.001 seconds.
 ```
+
+### `version`
+
+Returns the version of DataFusion.
+
+```
+version()
+```
+
+#### Example
+
+```
+> select version();
++--------------------------------------------+
+| version()                                  |
++--------------------------------------------+
+| Apache DataFusion 41.0.0, aarch64 on macos |
++--------------------------------------------+
+```


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to