Skip to content

Commit e4182d4

Browse files
authored
Run cargo-fmt on datafusion-functions/core (#9367)
* Run cargo-fmt on datafusion-functions/core * fmt nvl2
1 parent d6717f8 commit e4182d4

File tree

6 files changed

+66
-35
lines changed

6 files changed

+66
-35
lines changed

datafusion/functions/src/core/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,3 @@ export_functions!(
3232
(nvl, arg_1 arg_2, "returns value2 if value1 is NULL; otherwise it returns value1"),
3333
(nvl2, arg_1 arg_2 arg_3, "Returns value2 if value1 is not NULL; otherwise, it returns value3.")
3434
);
35-

datafusion/functions/src/core/nullif.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ static SUPPORTED_NULLIF_TYPES: &[DataType] = &[
5252
DataType::LargeUtf8,
5353
];
5454

55-
5655
impl NullIfFunc {
5756
pub fn new() -> Self {
5857
Self {
59-
signature:
60-
Signature::uniform(2, SUPPORTED_NULLIF_TYPES.to_vec(),
61-
Volatility::Immutable,
62-
)
58+
signature: Signature::uniform(
59+
2,
60+
SUPPORTED_NULLIF_TYPES.to_vec(),
61+
Volatility::Immutable,
62+
),
6363
}
6464
}
6565
}
@@ -78,18 +78,20 @@ impl ScalarUDFImpl for NullIfFunc {
7878

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

8790
fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
8891
nullif_func(args)
8992
}
9093
}
9194

92-
9395
/// Implements NULLIF(expr1, expr2)
9496
/// Args: 0 - left expr is any array
9597
/// 1 - if the left is equal to this expr2, then the result is NULL, otherwise left value is passed.

datafusion/functions/src/core/nvl.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
use arrow::array::Array;
19+
use arrow::compute::is_not_null;
20+
use arrow::compute::kernels::zip::zip;
1821
use arrow::datatypes::DataType;
1922
use datafusion_common::{internal_err, Result};
2023
use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility};
21-
use arrow::compute::kernels::zip::zip;
22-
use arrow::compute::is_not_null;
23-
use arrow::array::Array;
2424

2525
#[derive(Debug)]
2626
pub(super) struct NVLFunc {
@@ -50,8 +50,9 @@ static SUPPORTED_NVL_TYPES: &[DataType] = &[
5050
impl NVLFunc {
5151
pub fn new() -> Self {
5252
Self {
53-
signature:
54-
Signature::uniform(2, SUPPORTED_NVL_TYPES.to_vec(),
53+
signature: Signature::uniform(
54+
2,
55+
SUPPORTED_NVL_TYPES.to_vec(),
5556
Volatility::Immutable,
5657
),
5758
aliases: vec![String::from("ifnull")],
@@ -195,8 +196,11 @@ mod tests {
195196
let result = nvl_func(&[a, lit_array])?;
196197
let result = result.into_array(0).expect("Failed to convert to array");
197198

198-
let expected =
199-
Arc::new(BooleanArray::from(vec![Some(true), Some(false), Some(false)])) as ArrayRef;
199+
let expected = Arc::new(BooleanArray::from(vec![
200+
Some(true),
201+
Some(false),
202+
Some(false),
203+
])) as ArrayRef;
200204

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

253257
let result_null = nvl_func(&[a_null, b_null])?;
254-
let result_null = result_null.into_array(1).expect("Failed to convert to array");
258+
let result_null = result_null
259+
.into_array(1)
260+
.expect("Failed to convert to array");
255261

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

datafusion/functions/src/core/nvl2.rs

+11-15
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
use arrow::array::Array;
19+
use arrow::compute::is_not_null;
20+
use arrow::compute::kernels::zip::zip;
1821
use arrow::datatypes::DataType;
1922
use datafusion_common::{internal_err, plan_datafusion_err, Result};
2023
use datafusion_expr::{utils, ColumnarValue, ScalarUDFImpl, Signature, Volatility};
21-
use arrow::compute::kernels::zip::zip;
22-
use arrow::compute::is_not_null;
23-
use arrow::array::Array;
2424

2525
#[derive(Debug)]
2626
pub(super) struct NVL2Func {
@@ -30,10 +30,7 @@ pub(super) struct NVL2Func {
3030
impl NVL2Func {
3131
pub fn new() -> Self {
3232
Self {
33-
signature:
34-
Signature::variadic_equal(
35-
Volatility::Immutable,
36-
),
33+
signature: Signature::variadic_equal(Volatility::Immutable),
3734
}
3835
}
3936
}
@@ -87,14 +84,13 @@ fn nvl2_func(args: &[ColumnarValue]) -> Result<ColumnarValue> {
8784
}
8885
}
8986
if is_array {
90-
let args = args.iter().map(|arg| match arg {
91-
ColumnarValue::Scalar(scalar) => {
92-
scalar.to_array_of_size(len)
93-
}
94-
ColumnarValue::Array(array) => {
95-
Ok(array.clone())
96-
}
97-
}).collect::<Result<Vec<_>>>()?;
87+
let args = args
88+
.iter()
89+
.map(|arg| match arg {
90+
ColumnarValue::Scalar(scalar) => scalar.to_array_of_size(len),
91+
ColumnarValue::Array(array) => Ok(array.clone()),
92+
})
93+
.collect::<Result<Vec<_>>>()?;
9894
let to_apply = is_not_null(&args[0])?;
9995
let value = zip(&to_apply, &args[1], &args[2])?;
10096
Ok(ColumnarValue::Array(value))

datafusion/functions/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ use log::debug;
8484
#[macro_use]
8585
pub mod macros;
8686

87-
make_package!(core, "core_expressions", "Core datafusion expressions");
87+
/// Core datafusion expressions
88+
/// Enabled via feature flag `core_expressions`
89+
#[cfg(feature = "core_expressions")]
90+
pub mod core;
91+
make_stub_package!(core, "core_expressions");
8892

8993
make_package!(
9094
encoding,

datafusion/functions/src/macros.rs

+24
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,30 @@ macro_rules! make_package {
122122
};
123123
}
124124

125+
/// Macro creates a sub module if the feature is not enabled
126+
///
127+
/// The rationale for providing stub functions is to help users to configure datafusion
128+
/// properly (so they get an error telling them why a function is not available)
129+
/// instead of getting a cryptic "no function found" message at runtime.
130+
131+
macro_rules! make_stub_package {
132+
($name:ident, $feature:literal) => {
133+
#[cfg(not(feature = $feature))]
134+
#[doc = concat!("Disabled. Enable via feature flag `", $feature, "`")]
135+
pub mod $name {
136+
use datafusion_expr::ScalarUDF;
137+
use log::debug;
138+
use std::sync::Arc;
139+
140+
/// Returns an empty list of functions when the feature is not enabled
141+
pub fn functions() -> Vec<Arc<ScalarUDF>> {
142+
debug!("{} functions disabled", stringify!($name));
143+
vec![]
144+
}
145+
}
146+
};
147+
}
148+
125149
/// Invokes a function on each element of an array and returns the result as a new array
126150
///
127151
/// $ARG: ArrayRef

0 commit comments

Comments
 (0)