Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run cargo-fmt on datafusion-functions/core #9367

Merged
merged 6 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion datafusion/functions/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ export_functions!(
(nvl, arg_1 arg_2, "returns value2 if value1 is NULL; otherwise it returns value1"),
(nvl2, arg_1 arg_2 arg_3, "Returns value2 if value1 is not NULL; otherwise, it returns value3.")
);

22 changes: 12 additions & 10 deletions datafusion/functions/src/core/nullif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ static SUPPORTED_NULLIF_TYPES: &[DataType] = &[
DataType::LargeUtf8,
];


impl NullIfFunc {
pub fn new() -> Self {
Self {
signature:
Signature::uniform(2, SUPPORTED_NULLIF_TYPES.to_vec(),
Volatility::Immutable,
)
signature: Signature::uniform(
2,
SUPPORTED_NULLIF_TYPES.to_vec(),
Volatility::Immutable,
),
}
}
}
Expand All @@ -78,18 +78,20 @@ impl ScalarUDFImpl for NullIfFunc {

fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
// NULLIF has two args and they might get coerced, get a preview of this
let coerced_types = datafusion_expr::type_coercion::functions::data_types(arg_types, &self.signature);
coerced_types.map(|typs| typs[0].clone())
.map_err(|e| e.context("Failed to coerce arguments for NULLIF")
)
let coerced_types = datafusion_expr::type_coercion::functions::data_types(
arg_types,
&self.signature,
);
coerced_types
.map(|typs| typs[0].clone())
.map_err(|e| e.context("Failed to coerce arguments for NULLIF"))
}

fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
nullif_func(args)
}
}


/// Implements NULLIF(expr1, expr2)
/// Args: 0 - left expr is any array
/// 1 - if the left is equal to this expr2, then the result is NULL, otherwise left value is passed.
Expand Down
22 changes: 14 additions & 8 deletions datafusion/functions/src/core/nvl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
// specific language governing permissions and limitations
// under the License.

use arrow::array::Array;
use arrow::compute::is_not_null;
use arrow::compute::kernels::zip::zip;
use arrow::datatypes::DataType;
use datafusion_common::{internal_err, Result};
use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility};
use arrow::compute::kernels::zip::zip;
use arrow::compute::is_not_null;
use arrow::array::Array;

#[derive(Debug)]
pub(super) struct NVLFunc {
Expand Down Expand Up @@ -50,8 +50,9 @@ static SUPPORTED_NVL_TYPES: &[DataType] = &[
impl NVLFunc {
pub fn new() -> Self {
Self {
signature:
Signature::uniform(2, SUPPORTED_NVL_TYPES.to_vec(),
signature: Signature::uniform(
2,
SUPPORTED_NVL_TYPES.to_vec(),
Volatility::Immutable,
),
aliases: vec![String::from("ifnull")],
Expand Down Expand Up @@ -195,8 +196,11 @@ mod tests {
let result = nvl_func(&[a, lit_array])?;
let result = result.into_array(0).expect("Failed to convert to array");

let expected =
Arc::new(BooleanArray::from(vec![Some(true), Some(false), Some(false)])) as ArrayRef;
let expected = Arc::new(BooleanArray::from(vec![
Some(true),
Some(false),
Some(false),
])) as ArrayRef;

assert_eq!(expected.as_ref(), result.as_ref());
Ok(())
Expand Down Expand Up @@ -251,7 +255,9 @@ mod tests {
let b_null = ColumnarValue::Scalar(ScalarValue::Int32(Some(2i32)));

let result_null = nvl_func(&[a_null, b_null])?;
let result_null = result_null.into_array(1).expect("Failed to convert to array");
let result_null = result_null
.into_array(1)
.expect("Failed to convert to array");

let expected_null = Arc::new(Int32Array::from(vec![Some(2i32)])) as ArrayRef;

Expand Down
26 changes: 11 additions & 15 deletions datafusion/functions/src/core/nvl2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
// specific language governing permissions and limitations
// under the License.

use arrow::array::Array;
use arrow::compute::is_not_null;
use arrow::compute::kernels::zip::zip;
use arrow::datatypes::DataType;
use datafusion_common::{internal_err, plan_datafusion_err, Result};
use datafusion_expr::{utils, ColumnarValue, ScalarUDFImpl, Signature, Volatility};
use arrow::compute::kernels::zip::zip;
use arrow::compute::is_not_null;
use arrow::array::Array;

#[derive(Debug)]
pub(super) struct NVL2Func {
Expand All @@ -30,10 +30,7 @@ pub(super) struct NVL2Func {
impl NVL2Func {
pub fn new() -> Self {
Self {
signature:
Signature::variadic_equal(
Volatility::Immutable,
),
signature: Signature::variadic_equal(Volatility::Immutable),
}
}
}
Expand Down Expand Up @@ -87,14 +84,13 @@ fn nvl2_func(args: &[ColumnarValue]) -> Result<ColumnarValue> {
}
}
if is_array {
let args = args.iter().map(|arg| match arg {
ColumnarValue::Scalar(scalar) => {
scalar.to_array_of_size(len)
}
ColumnarValue::Array(array) => {
Ok(array.clone())
}
}).collect::<Result<Vec<_>>>()?;
let args = args
.iter()
.map(|arg| match arg {
ColumnarValue::Scalar(scalar) => scalar.to_array_of_size(len),
ColumnarValue::Array(array) => Ok(array.clone()),
})
.collect::<Result<Vec<_>>>()?;
let to_apply = is_not_null(&args[0])?;
let value = zip(&to_apply, &args[1], &args[2])?;
Ok(ColumnarValue::Array(value))
Expand Down
6 changes: 5 additions & 1 deletion datafusion/functions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ use log::debug;
#[macro_use]
pub mod macros;

make_package!(core, "core_expressions", "Core datafusion expressions");
/// Core datafusion expressions
/// Enabled via feature flag `core_expressions`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the actual change. If this PR is merged, I will make follow on PR(s) to apply the same treatment to the other modules in datafusion-functions.

I did only one initially to keep the size of the PR down

#[cfg(feature = "core_expressions")]
pub mod core;
make_stub_package!(core, "core_expressions");

make_package!(
encoding,
Expand Down
24 changes: 24 additions & 0 deletions datafusion/functions/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,30 @@ macro_rules! make_package {
};
}

/// Macro creates a sub module if the feature is not enabled
///
/// The rationale for providing stub functions is to help users to configure datafusion
/// properly (so they get an error telling them why a function is not available)
/// instead of getting a cryptic "no function found" message at runtime.

macro_rules! make_stub_package {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is part of the make_package macro which I think keeps most of the "don't repeat yourself" goal but still allows cargo fmt to run.

Once I port over the other modules to use this pattern, I will remove the make_package macro

($name:ident, $feature:literal) => {
#[cfg(not(feature = $feature))]
#[doc = concat!("Disabled. Enable via feature flag `", $feature, "`")]
pub mod $name {
use datafusion_expr::ScalarUDF;
use log::debug;
use std::sync::Arc;

/// Returns an empty list of functions when the feature is not enabled
pub fn functions() -> Vec<Arc<ScalarUDF>> {
debug!("{} functions disabled", stringify!($name));
vec![]
}
}
};
}

/// Invokes a function on each element of an array and returns the result as a new array
///
/// $ARG: ArrayRef
Expand Down
Loading