From eb07a5164b93d2d331fd8db6b8a89653b2f247ae Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Thu, 20 Nov 2025 09:32:57 +0200 Subject: [PATCH] extensions: Improve error handling in percentile extension Eliminate unwrap() where we can, replace with expect() where we can't. --- extensions/percentile/src/lib.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/extensions/percentile/src/lib.rs b/extensions/percentile/src/lib.rs index 2086ea46f..ae4521df1 100644 --- a/extensions/percentile/src/lib.rs +++ b/extensions/percentile/src/lib.rs @@ -25,7 +25,7 @@ impl AggFunc for Median { } let mut sorted = state; - sorted.sort_by(|a, b| a.partial_cmp(b).unwrap()); + sorted.sort_by(|a, b| a.total_cmp(b)); let len = sorted.len(); if len % 2 == 1 { @@ -82,8 +82,8 @@ impl AggFunc for Percentile { return Ok(Value::from_float(values[0])); } - let p = p_value.unwrap(); - values.sort_by(|a, b| a.partial_cmp(b).unwrap()); + let p = p_value.ok_or("percentile value must be provided")?; + values.sort_by(|a, b| a.total_cmp(b)); let n = values.len() as f64; let index = p * (n - 1.0) / 100.0; let lower = index.floor() as usize; @@ -144,8 +144,8 @@ impl AggFunc for PercentileCont { return Ok(Value::from_float(values[0])); } - let p = p_value.unwrap(); - values.sort_by(|a, b| a.partial_cmp(b).unwrap()); + let p = p_value.ok_or("percentile value must be provided")?; + values.sort_by(|a, b| a.total_cmp(b)); let n = values.len() as f64; let index = p * (n - 1.0); let lower = index.floor() as usize; @@ -184,8 +184,8 @@ impl AggFunc for PercentileDisc { return Err(err); } - let p = p_value.unwrap(); - values.sort_by(|a, b| a.partial_cmp(b).unwrap()); + let p = p_value.ok_or("percentile value must be provided")?; + values.sort_by(|a, b| a.total_cmp(b)); let n = values.len() as f64; let index = (p * (n - 1.0)).floor() as usize; Ok(Value::from_float(values[index]))