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> {
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");
}
}
}