|
| 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 | +} |
0 commit comments