Skip to content

Commit

Permalink
refactor: add more error info when array is empty (#5560)
Browse files Browse the repository at this point in the history
* refactor: add more error info when array is empty

* make code more concise

* add tests and refine the error report
  • Loading branch information
Weijun-H authored Mar 14, 2023
1 parent 3b5a9af commit 26eb406
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions datafusion/core/tests/sqllogictests/test_files/aggregate.slt
Original file line number Diff line number Diff line change
Expand Up @@ -1284,3 +1284,9 @@ NULL 2

statement ok
drop table the_nulls;

query error DataFusion error: Execution error: median requires at least one non-null value
select median(a) from (select 1 as a where 1=0);

query error DataFusion error: Execution error: aggregate function needs at least one non-null element
select approx_median(a) from (select 1 as a where 1=0);
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,11 @@ impl Accumulator for ApproxPercentileAccumulator {
}

fn evaluate(&self) -> Result<ScalarValue> {
if self.digest.count() == 0.0 {
return Err(DataFusionError::Execution(
"aggregate function needs at least one non-null element".to_string(),
));
}
let q = self.digest.estimate_quantile(self.percentile);

// These acceptable return types MUST match the validation in
Expand Down
6 changes: 6 additions & 0 deletions datafusion/physical-expr/src/aggregate/median.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ impl Accumulator for MedianAccumulator {
}

fn evaluate(&self) -> Result<ScalarValue> {
if !self.all_values.iter().any(|v| !v.is_null()) {
return Err(DataFusionError::Execution(
"median requires at least one non-null value".into(),
));
}

// Create an array of all the non null values and find the
// sorted indexes
let array = ScalarValue::iter_to_array(
Expand Down

0 comments on commit 26eb406

Please sign in to comment.