Skip to content

Commit 7228de5

Browse files
kumarUjjawalJefffrey
authored andcommitted
minor: refactor with assert_or_internal_err!() in datafusion/functions-aggregate (apache#18716)
## 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 fc16bd8 commit 7228de5

4 files changed

Lines changed: 35 additions & 33 deletions

File tree

‎datafusion/functions-aggregate/src/array_agg.rs‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ use datafusion_common::cast::as_list_array;
3232
use datafusion_common::utils::{
3333
compare_rows, get_row_at_idx, take_function_args, SingleRowListArrayBuilder,
3434
};
35-
use datafusion_common::{exec_err, internal_err, Result, ScalarValue};
35+
use datafusion_common::{
36+
assert_eq_or_internal_err, exec_err, DataFusionError, Result, ScalarValue,
37+
};
3638
use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs};
3739
use datafusion_expr::utils::format_state_name;
3840
use datafusion_expr::{
@@ -319,9 +321,7 @@ impl Accumulator for ArrayAggAccumulator {
319321
return Ok(());
320322
}
321323

322-
if values.len() != 1 {
323-
return internal_err!("expects single batch");
324-
}
324+
assert_eq_or_internal_err!(values.len(), 1, "expects single batch");
325325

326326
let val = &values[0];
327327
let nulls = if self.ignore_nulls {
@@ -349,9 +349,7 @@ impl Accumulator for ArrayAggAccumulator {
349349
return Ok(());
350350
}
351351

352-
if states.len() != 1 {
353-
return internal_err!("expects single state");
354-
}
352+
assert_eq_or_internal_err!(states.len(), 1, "expects single state");
355353

356354
let list_arr = as_list_array(&states[0])?;
357355

@@ -472,9 +470,7 @@ impl Accumulator for DistinctArrayAggAccumulator {
472470
return Ok(());
473471
}
474472

475-
if states.len() != 1 {
476-
return internal_err!("expects single state");
477-
}
473+
assert_eq_or_internal_err!(states.len(), 1, "expects single state");
478474

479475
states[0]
480476
.as_list::<i32>()

‎datafusion/functions-aggregate/src/median.rs‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ use arrow::datatypes::{
4040
};
4141

4242
use datafusion_common::{
43-
internal_datafusion_err, internal_err, DataFusionError, Result, ScalarValue,
43+
assert_eq_or_internal_err, internal_datafusion_err, DataFusionError, Result,
44+
ScalarValue,
4445
};
4546
use datafusion_expr::function::StateFieldsArgs;
4647
use datafusion_expr::{
@@ -189,12 +190,12 @@ impl AggregateUDFImpl for Median {
189190
args: AccumulatorArgs,
190191
) -> Result<Box<dyn GroupsAccumulator>> {
191192
let num_args = args.exprs.len();
192-
if num_args != 1 {
193-
return internal_err!(
194-
"median should only have 1 arg, but found num args:{}",
195-
args.exprs.len()
196-
);
197-
}
193+
assert_eq_or_internal_err!(
194+
num_args,
195+
1,
196+
"median should only have 1 arg, but found num args:{}",
197+
num_args
198+
);
198199

199200
let dt = args.expr_fields[0].data_type().clone();
200201

‎datafusion/functions-aggregate/src/nth_value.rs‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ use arrow::array::{new_empty_array, ArrayRef, AsArray, StructArray};
2727
use arrow::datatypes::{DataType, Field, FieldRef, Fields};
2828

2929
use datafusion_common::utils::{get_row_at_idx, SingleRowListArrayBuilder};
30-
use datafusion_common::{exec_err, internal_err, not_impl_err, Result, ScalarValue};
30+
use datafusion_common::{
31+
assert_or_internal_err, exec_err, not_impl_err, DataFusionError, Result, ScalarValue,
32+
};
3133
use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs};
3234
use datafusion_expr::utils::format_state_name;
3335
use datafusion_expr::{
@@ -206,10 +208,11 @@ impl TrivialNthValueAccumulator {
206208
/// Create a new order-insensitive NTH_VALUE accumulator based on the given
207209
/// item data type.
208210
pub fn try_new(n: i64, datatype: &DataType) -> Result<Self> {
209-
if n == 0 {
210-
// n cannot be 0
211-
return internal_err!("Nth value indices are 1 based. 0 is invalid index");
212-
}
211+
// n cannot be 0
212+
assert_or_internal_err!(
213+
n != 0,
214+
"Nth value indices are 1 based. 0 is invalid index"
215+
);
213216
Ok(Self {
214217
n,
215218
values: VecDeque::new(),
@@ -339,10 +342,11 @@ impl NthValueAccumulator {
339342
ordering_dtypes: &[DataType],
340343
ordering_req: LexOrdering,
341344
) -> Result<Self> {
342-
if n == 0 {
343-
// n cannot be 0
344-
return internal_err!("Nth value indices are 1 based. 0 is invalid index");
345-
}
345+
// n cannot be 0
346+
assert_or_internal_err!(
347+
n != 0,
348+
"Nth value indices are 1 based. 0 is invalid index"
349+
);
346350
let mut datatypes = vec![datatype.clone()];
347351
datatypes.extend(ordering_dtypes.iter().cloned());
348352
Ok(Self {

‎datafusion/functions-aggregate/src/percentile_cont.rs‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ use arrow::{
3434
use arrow::array::ArrowNativeTypeOp;
3535

3636
use datafusion_common::{
37-
internal_datafusion_err, internal_err, plan_err, DataFusionError, Result, ScalarValue,
37+
assert_eq_or_internal_err, internal_datafusion_err, plan_err, DataFusionError,
38+
Result, ScalarValue,
3839
};
3940
use datafusion_expr::expr::{AggregateFunction, Sort};
4041
use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs};
@@ -303,12 +304,12 @@ impl AggregateUDFImpl for PercentileCont {
303304
args: AccumulatorArgs,
304305
) -> Result<Box<dyn GroupsAccumulator>> {
305306
let num_args = args.exprs.len();
306-
if num_args != 2 {
307-
return internal_err!(
308-
"percentile_cont should have 2 args, but found num args:{}",
309-
args.exprs.len()
310-
);
311-
}
307+
assert_eq_or_internal_err!(
308+
num_args,
309+
2,
310+
"percentile_cont should have 2 args, but found num args:{}",
311+
num_args
312+
);
312313

313314
let percentile = validate_percentile_expr(&args.exprs[1], "PERCENTILE_CONT")?;
314315

0 commit comments

Comments
 (0)