Skip to content

Commit 1476682

Browse files
kumarUjjawalJefffrey
authored andcommitted
minor: refactor with assert_or_internal_err!() in datafusion/functions (apache#18700)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes apache#123` indicates that this PR will close issue apache#123. --> - Part of apache#18613 ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. --> --------- Co-authored-by: Jeffrey Vo <[email protected]>
1 parent 2c3d6a6 commit 1476682

6 files changed

Lines changed: 42 additions & 31 deletions

File tree

‎datafusion/functions/src/core/greatest.rs‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ use arrow::buffer::BooleanBuffer;
2121
use arrow::compute::kernels::cmp;
2222
use arrow::compute::SortOptions;
2323
use arrow::datatypes::DataType;
24-
use datafusion_common::{internal_err, Result, ScalarValue};
24+
use datafusion_common::{
25+
assert_eq_or_internal_err, DataFusionError, Result, ScalarValue,
26+
};
2527
use datafusion_doc::Documentation;
2628
use datafusion_expr::{ColumnarValue, ScalarFunctionArgs};
2729
use datafusion_expr::{ScalarUDFImpl, Signature, Volatility};
@@ -113,11 +115,11 @@ impl GreatestLeastOperator for GreatestFunc {
113115

114116
let cmp = make_comparator(lhs, rhs, SORT_OPTIONS)?;
115117

116-
if lhs.len() != rhs.len() {
117-
return internal_err!(
118-
"All arrays should have the same length for greatest comparison"
119-
);
120-
}
118+
assert_eq_or_internal_err!(
119+
lhs.len(),
120+
rhs.len(),
121+
"All arrays should have the same length for greatest comparison"
122+
);
121123

122124
let values = BooleanBuffer::collect_bool(lhs.len(), |i| cmp(i, i).is_ge());
123125

‎datafusion/functions/src/core/greatest_least_utils.rs‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
use arrow::array::{Array, ArrayRef, BooleanArray};
1919
use arrow::compute::kernels::zip::zip;
2020
use arrow::datatypes::DataType;
21-
use datafusion_common::{internal_err, plan_err, Result, ScalarValue};
21+
use datafusion_common::{
22+
assert_or_internal_err, plan_err, DataFusionError, Result, ScalarValue,
23+
};
2224
use datafusion_expr_common::columnar_value::ColumnarValue;
2325
use datafusion_expr_common::type_coercion::binary::type_union_resolution;
2426
use std::sync::Arc;
@@ -50,12 +52,11 @@ fn keep_array<Op: GreatestLeastOperator>(
5052
pub(super) fn execute_conditional<Op: GreatestLeastOperator>(
5153
args: &[ColumnarValue],
5254
) -> Result<ColumnarValue> {
53-
if args.is_empty() {
54-
return internal_err!(
55-
"{} was called with no arguments. It requires at least 1.",
56-
Op::NAME
57-
);
58-
}
55+
assert_or_internal_err!(
56+
!args.is_empty(),
57+
"{} was called with no arguments. It requires at least 1.",
58+
Op::NAME
59+
);
5960

6061
// Some engines (e.g. SQL Server) allow greatest/least with single arg, it's a noop
6162
if args.len() == 1 {

‎datafusion/functions/src/core/least.rs‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ use arrow::buffer::BooleanBuffer;
2121
use arrow::compute::kernels::cmp;
2222
use arrow::compute::SortOptions;
2323
use arrow::datatypes::DataType;
24-
use datafusion_common::{internal_err, Result, ScalarValue};
24+
use datafusion_common::{
25+
assert_eq_or_internal_err, DataFusionError, Result, ScalarValue,
26+
};
2527
use datafusion_doc::Documentation;
2628
use datafusion_expr::{ColumnarValue, ScalarFunctionArgs};
2729
use datafusion_expr::{ScalarUDFImpl, Signature, Volatility};
@@ -126,11 +128,11 @@ impl GreatestLeastOperator for LeastFunc {
126128

127129
let cmp = make_comparator(lhs, rhs, SORT_OPTIONS)?;
128130

129-
if lhs.len() != rhs.len() {
130-
return internal_err!(
131-
"All arrays should have the same length for least comparison"
132-
);
133-
}
131+
assert_eq_or_internal_err!(
132+
lhs.len(),
133+
rhs.len(),
134+
"All arrays should have the same length for least comparison"
135+
);
134136

135137
let values = BooleanBuffer::collect_bool(lhs.len(), |i| cmp(i, i).is_le());
136138

‎datafusion/functions/src/math/pi.rs‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::any::Any;
1919

2020
use arrow::datatypes::DataType;
2121
use arrow::datatypes::DataType::Float64;
22-
use datafusion_common::{internal_err, Result, ScalarValue};
22+
use datafusion_common::{assert_or_internal_err, DataFusionError, Result, ScalarValue};
2323
use datafusion_expr::sort_properties::{ExprProperties, SortProperties};
2424
use datafusion_expr::{
2525
ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
@@ -69,9 +69,11 @@ impl ScalarUDFImpl for PiFunc {
6969
}
7070

7171
fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
72-
if !args.args.is_empty() {
73-
return internal_err!("{} function does not accept arguments", self.name());
74-
}
72+
assert_or_internal_err!(
73+
args.args.is_empty(),
74+
"{} function does not accept arguments",
75+
self.name()
76+
);
7577
Ok(ColumnarValue::Scalar(ScalarValue::Float64(Some(
7678
std::f64::consts::PI,
7779
))))

‎datafusion/functions/src/math/random.rs‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use arrow::datatypes::DataType;
2323
use arrow::datatypes::DataType::Float64;
2424
use rand::{rng, Rng};
2525

26-
use datafusion_common::{internal_err, Result};
26+
use datafusion_common::{assert_or_internal_err, DataFusionError, Result};
2727
use datafusion_expr::{ColumnarValue, ScalarFunctionArgs};
2828
use datafusion_expr::{Documentation, ScalarUDFImpl, Signature, Volatility};
2929
use datafusion_macros::user_doc;
@@ -79,9 +79,11 @@ impl ScalarUDFImpl for RandomFunc {
7979
}
8080

8181
fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
82-
if !args.args.is_empty() {
83-
return internal_err!("{} function does not accept arguments", self.name());
84-
}
82+
assert_or_internal_err!(
83+
args.args.is_empty(),
84+
"{} function does not accept arguments",
85+
self.name()
86+
);
8587
let mut rng = rng();
8688
let mut values = vec![0.0; args.number_rows];
8789
// Equivalent to set each element with rng.random_range(0.0..1.0), but more efficient

‎datafusion/functions/src/string/uuid.rs‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use arrow::datatypes::DataType::Utf8;
2424
use rand::Rng;
2525
use uuid::Uuid;
2626

27-
use datafusion_common::{internal_err, Result};
27+
use datafusion_common::{assert_or_internal_err, DataFusionError, Result};
2828
use datafusion_expr::{ColumnarValue, Documentation, Volatility};
2929
use datafusion_expr::{ScalarFunctionArgs, ScalarUDFImpl, Signature};
3030
use datafusion_macros::user_doc;
@@ -81,9 +81,11 @@ impl ScalarUDFImpl for UuidFunc {
8181
/// Prints random (v4) uuid values per row
8282
/// uuid() = 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'
8383
fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
84-
if !args.args.is_empty() {
85-
return internal_err!("{} function does not accept arguments", self.name());
86-
}
84+
assert_or_internal_err!(
85+
args.args.is_empty(),
86+
"{} function does not accept arguments",
87+
self.name()
88+
);
8789

8890
// Generate random u128 values
8991
let mut rng = rand::rng();

0 commit comments

Comments
 (0)