Apache DataFusion SQL Query Engine
minor: refactor with `assert_or_internal_err!()` in `datafusion/expr-common` (#18702)
Kumar Ujjawal1b1cd0d1e6f3
diff --git a/datafusion/expr-common/src/columnar_value.rs b/datafusion/expr-common/src/columnar_value.rsindex f7bbd41120..d508164b6b 100644--- a/datafusion/expr-common/src/columnar_value.rs+++ b/datafusion/expr-common/src/columnar_value.rs@@ -247,7 +247,8 @@ impl ColumnarValue {Some(array_len)} else {return internal_err!(- "Arguments has mixed length. Expected length: {array_len}, found length: {}", a.len()+ "Arguments has mixed length. Expected length: {array_len}, found length: {}",+ a.len());}}diff --git a/datafusion/expr-common/src/interval_arithmetic.rs b/datafusion/expr-common/src/interval_arithmetic.rsindex 942f3d48c6..9ea9d9275f 100644--- a/datafusion/expr-common/src/interval_arithmetic.rs+++ b/datafusion/expr-common/src/interval_arithmetic.rs@@ -31,7 +31,10 @@ use arrow::datatypes::{MIN_DECIMAL128_FOR_EACH_PRECISION, MIN_DECIMAL256_FOR_EACH_PRECISION,};use datafusion_common::rounding::{alter_fp_rounding_mode, next_down, next_up};-use datafusion_common::{internal_err, Result, ScalarValue};+use datafusion_common::{+ assert_eq_or_internal_err, assert_or_internal_err, internal_err, DataFusionError,+ Result, ScalarValue,+};macro_rules! get_extreme_value {($extreme:ident, $value:expr) => {@@ -266,24 +269,23 @@ impl Interval {/// - Floating-point endpoints with `NaN`, `INF`, or `NEG_INF` are converted/// to `NULL`s.pub fn try_new(lower: ScalarValue, upper: ScalarValue) -> Result<Self> {- if lower.data_type() != upper.data_type() {- return internal_err!("Endpoints of an Interval should have the same type");- }+ assert_eq_or_internal_err!(+ lower.data_type(),+ upper.data_type(),+ "Endpoints of an Interval should have the same type"+ );let interval = Self::new(lower, upper);- if interval.lower.is_null()- || interval.upper.is_null()- || interval.lower <= interval.upper- {- Ok(interval)- } else {- internal_err!(- "Interval's lower bound {} is greater than the upper bound {}",- interval.lower,- interval.upper- )- }+ assert_or_internal_err!(+ interval.lower.is_null()+ || interval.upper.is_null()+ || interval.lower <= interval.upper,+ "Interval's lower bound {} is greater than the upper bound {}",+ interval.lower,+ interval.upper+ );+ Ok(interval)}/// Only for internal usage. Responsible for standardizing booleans and@@ -454,15 +456,16 @@ impl Interval {/// to an error.pub fn gt<T: Borrow<Self>>(&self, other: T) -> Result<Self> {let rhs = other.borrow();- if self.data_type().ne(&rhs.data_type()) {- internal_err!(- "Only intervals with the same data type are comparable, lhs:{}, rhs:{}",- self.data_type(),- rhs.data_type()- )- } else if !(self.upper.is_null() || rhs.lower.is_null())- && self.upper <= rhs.lower- {+ let lhs_type = self.data_type();+ let rhs_type = rhs.data_type();+ assert_eq_or_internal_err!(+ lhs_type,+ rhs_type,+ "Only intervals with the same data type are comparable, lhs:{}, rhs:{}",+ self.data_type(),+ rhs.data_type()+ );+ if !(self.upper.is_null() || rhs.lower.is_null()) && self.upper <= rhs.lower {// Values in this interval are certainly less than or equal to// those in the given interval.Ok(Self::CERTAINLY_FALSE)@@ -487,15 +490,16 @@ impl Interval {/// to an error.pub fn gt_eq<T: Borrow<Self>>(&self, other: T) -> Result<Self> {let rhs = other.borrow();- if self.data_type().ne(&rhs.data_type()) {- internal_err!(- "Only intervals with the same data type are comparable, lhs:{}, rhs:{}",- self.data_type(),- rhs.data_type()- )- } else if !(self.lower.is_null() || rhs.upper.is_null())- && self.lower >= rhs.upper- {+ let lhs_type = self.data_type();+ let rhs_type = rhs.data_type();+ assert_eq_or_internal_err!(+ lhs_type,+ rhs_type,+ "Only intervals with the same data type are comparable, lhs:{}, rhs:{}",+ self.data_type(),+ rhs.data_type()+ );+ if !(self.lower.is_null() || rhs.upper.is_null()) && self.lower >= rhs.upper {// Values in this interval are certainly greater than or equal to// those in the given interval.Ok(Self::CERTAINLY_TRUE)@@ -542,16 +546,17 @@ impl Interval {/// to an error.pub fn equal<T: Borrow<Self>>(&self, other: T) -> Result<Self> {let rhs = other.borrow();- if BinaryTypeCoercer::new(&self.data_type(), &Operator::Eq, &rhs.data_type())- .get_result_type()- .is_err()- {- internal_err!(- "Interval data types must be compatible for equality checks, lhs:{}, rhs:{}",- self.data_type(),- rhs.data_type()- )- } else if !self.lower.is_null()+ let types_compatible =+ BinaryTypeCoercer::new(&self.data_type(), &Operator::Eq, &rhs.data_type())+ .get_result_type()+ .is_ok();+ assert_or_internal_err!(+ types_compatible,+ "Interval data types must be compatible for equality checks, lhs:{}, rhs:{}",+ self.data_type(),+ rhs.data_type()+ );+ if !self.lower.is_null()&& (self.lower == self.upper)&& (rhs.lower == rhs.upper)&& (self.lower == rhs.lower)@@ -616,9 +621,12 @@ impl Interval {/// Compute the logical negation of this (boolean) interval.pub fn not(&self) -> Result<Self> {- if self.data_type().ne(&DataType::Boolean) {- internal_err!("Cannot apply logical negation to a non-boolean interval")- } else if self == &Self::CERTAINLY_TRUE {+ assert_eq_or_internal_err!(+ self.data_type(),+ DataType::Boolean,+ "Cannot apply logical negation to a non-boolean interval"+ );+ if self == &Self::CERTAINLY_TRUE {Ok(Self::CERTAINLY_FALSE)} else if self == &Self::CERTAINLY_FALSE {Ok(Self::CERTAINLY_TRUE)@@ -635,13 +643,15 @@ impl Interval {/// to an error.pub fn intersect<T: Borrow<Self>>(&self, other: T) -> Result<Option<Self>> {let rhs = other.borrow();- if self.data_type().ne(&rhs.data_type()) {- return internal_err!(- "Only intervals with the same data type are intersectable, lhs:{}, rhs:{}",- self.data_type(),- rhs.data_type()- );- };+ let lhs_type = self.data_type();+ let rhs_type = rhs.data_type();+ assert_eq_or_internal_err!(+ lhs_type,+ rhs_type,+ "Only intervals with the same data type are intersectable, lhs:{}, rhs:{}",+ self.data_type(),+ rhs.data_type()+ );// If it is evident that the result is an empty interval, short-circuit// and directly return `None`.@@ -670,13 +680,15 @@ impl Interval {/// to an error.pub fn union<T: Borrow<Self>>(&self, other: T) -> Result<Self> {let rhs = other.borrow();- if self.data_type().ne(&rhs.data_type()) {- return internal_err!(- "Cannot calculate the union of intervals with different data types, lhs:{}, rhs:{}",- self.data_type(),- rhs.data_type()- );- };+ let lhs_type = self.data_type();+ let rhs_type = rhs.data_type();+ assert_eq_or_internal_err!(+ lhs_type,+ rhs_type,+ "Cannot calculate the union of intervals with different data types, lhs:{}, rhs:{}",+ self.data_type(),+ rhs.data_type()+ );let lower = if self.lower.is_null()|| (!rhs.lower.is_null() && self.lower <= rhs.lower)@@ -706,27 +718,28 @@ impl Interval {pub fn contains_value<T: Borrow<ScalarValue>>(&self, other: T) -> Result<bool> {let rhs = other.borrow();- let (lhs_lower, lhs_upper, rhs) = if self.data_type().eq(&rhs.data_type()) {- (&self.lower, &self.upper, rhs)- } else if let Some(common_type) =- comparison_coercion_numeric(&self.data_type(), &rhs.data_type())- {- (- &self.lower.cast_to(&common_type)?,- &self.upper.cast_to(&common_type)?,- &rhs.cast_to(&common_type)?,- )+ let (lhs_lower, lhs_upper, rhs_value) = if self.data_type().eq(&rhs.data_type()) {+ (self.lower.clone(), self.upper.clone(), rhs.clone())} else {- return internal_err!(+ let maybe_common_type =+ comparison_coercion_numeric(&self.data_type(), &rhs.data_type());+ assert_or_internal_err!(+ maybe_common_type.is_some(),"Data types must be compatible for containment checks, lhs:{}, rhs:{}",self.data_type(),rhs.data_type());+ let common_type = maybe_common_type.expect("checked for Some");+ (+ self.lower.cast_to(&common_type)?,+ self.upper.cast_to(&common_type)?,+ rhs.cast_to(&common_type)?,+ )};// We only check the upper bound for a `None` value because `None`// values are less than `Some` values according to Rust.- Ok(lhs_lower <= rhs && (lhs_upper.is_null() || rhs <= lhs_upper))+ Ok(lhs_lower <= rhs_value && (lhs_upper.is_null() || rhs_value <= lhs_upper))}/// Decide if this interval is a superset of, overlaps with, or@@ -738,13 +751,15 @@ impl Interval {/// to an error.pub fn contains<T: Borrow<Self>>(&self, other: T) -> Result<Self> {let rhs = other.borrow();- if self.data_type().ne(&rhs.data_type()) {- return internal_err!(- "Interval data types must match for containment checks, lhs:{}, rhs:{}",- self.data_type(),- rhs.data_type()- );- };+ let lhs_type = self.data_type();+ let rhs_type = rhs.data_type();+ assert_eq_or_internal_err!(+ lhs_type,+ rhs_type,+ "Interval data types must match for containment checks, lhs:{}, rhs:{}",+ self.data_type(),+ rhs.data_type()+ );match self.intersect(rhs)? {Some(intersection) => {@@ -813,15 +828,15 @@ impl Interval {/// to an error.pub fn mul<T: Borrow<Self>>(&self, other: T) -> Result<Self> {let rhs = other.borrow();- let dt = if self.data_type().eq(&rhs.data_type()) {- self.data_type()- } else {- return internal_err!(- "Intervals must have the same data type for multiplication, lhs:{}, rhs:{}",- self.data_type(),- rhs.data_type()- );- };+ let dt = self.data_type();+ let rhs_type = rhs.data_type();+ assert_eq_or_internal_err!(+ dt.clone(),+ rhs_type.clone(),+ "Intervals must have the same data type for multiplication, lhs:{}, rhs:{}",+ dt.clone(),+ rhs_type.clone()+ );let zero = ScalarValue::new_zero(&dt)?;@@ -856,15 +871,15 @@ impl Interval {/// zero should result in an interval set, not the universal set.pub fn div<T: Borrow<Self>>(&self, other: T) -> Result<Self> {let rhs = other.borrow();- let dt = if self.data_type().eq(&rhs.data_type()) {- self.data_type()- } else {- return internal_err!(- "Intervals must have the same data type for division, lhs:{}, rhs:{}",- self.data_type(),- rhs.data_type()- );- };+ let dt = self.data_type();+ let rhs_type = rhs.data_type();+ assert_eq_or_internal_err!(+ dt.clone(),+ rhs_type.clone(),+ "Intervals must have the same data type for division, lhs:{}, rhs:{}",+ dt.clone(),+ rhs_type.clone()+ );let zero = ScalarValue::new_zero(&dt)?;// We want 0 to be approachable from both negative and positive sides.@@ -1317,13 +1332,15 @@ pub fn satisfy_greater(right: &Interval,strict: bool,) -> Result<Option<(Interval, Interval)>> {- if left.data_type().ne(&right.data_type()) {- return internal_err!(- "Intervals must have the same data type, lhs:{}, rhs:{}",- left.data_type(),- right.data_type()- );- }+ let lhs_type = left.data_type();+ let rhs_type = right.data_type();+ assert_eq_or_internal_err!(+ lhs_type.clone(),+ rhs_type.clone(),+ "Intervals must have the same data type, lhs:{}, rhs:{}",+ lhs_type,+ rhs_type+ );if !left.upper.is_null() && left.upper <= right.lower {if !strict && left.upper == right.lower {@@ -1811,13 +1828,12 @@ impl NullableInterval {fn not(&self) -> Result<Self> {match self {Self::Null { datatype } => {- if datatype == &DataType::Boolean {- Ok(Self::UNKNOWN)- } else {- internal_err!(- "Cannot apply logical negation to a non-boolean interval"- )- }+ assert_eq_or_internal_err!(+ datatype,+ &DataType::Boolean,+ "Cannot apply logical negation to a non-boolean interval"+ );+ Ok(Self::UNKNOWN)}Self::MaybeNull { values } => Ok(Self::MaybeNull {values: values.not()?,diff --git a/datafusion/expr-common/src/statistics.rs b/datafusion/expr-common/src/statistics.rsindex 5c5e397e74..c5c3355983 100644--- a/datafusion/expr-common/src/statistics.rs+++ b/datafusion/expr-common/src/statistics.rs@@ -24,7 +24,10 @@ use crate::type_coercion::binary::binary_numeric_coercion;use arrow::array::ArrowNativeTypeOp;use arrow::datatypes::DataType;use datafusion_common::rounding::alter_fp_rounding_mode;-use datafusion_common::{internal_err, not_impl_err, Result, ScalarValue};+use datafusion_common::{+ assert_eq_or_internal_err, assert_ne_or_internal_err, assert_or_internal_err,+ internal_err, not_impl_err, DataFusionError, Result, ScalarValue,+};/// This object defines probabilistic distributions that encode uncertain/// information about a single, scalar value. Currently, we support five core@@ -275,11 +278,11 @@ pub struct GenericDistribution {impl UniformDistribution {fn try_new(interval: Interval) -> Result<Self> {- if interval.data_type().eq(&DataType::Boolean) {- return internal_err!(- "Construction of a boolean `Uniform` distribution is prohibited, create a `Bernoulli` distribution instead."- );- }+ assert_ne_or_internal_err!(+ interval.data_type(),+ DataType::Boolean,+ "Construction of a boolean `Uniform` distribution is prohibited, create a `Bernoulli` distribution instead."+ );Ok(Self { interval })}@@ -337,21 +340,29 @@ impl ExponentialDistribution {positive_tail: bool,) -> Result<Self> {let dt = rate.data_type();- if offset.data_type() != dt {- internal_err!("Rate and offset must have the same data type")- } else if offset.is_null() {- internal_err!("Offset of an `ExponentialDistribution` cannot be null")- } else if rate.is_null() {- internal_err!("Rate of an `ExponentialDistribution` cannot be null")- } else if rate.le(&ScalarValue::new_zero(&dt)?) {- internal_err!("Rate of an `ExponentialDistribution` must be positive")- } else {- Ok(Self {- rate,- offset,- positive_tail,- })- }+ assert_eq_or_internal_err!(+ offset.data_type(),+ dt,+ "Rate and offset must have the same data type"+ );+ assert_or_internal_err!(+ !offset.is_null(),+ "Offset of an `ExponentialDistribution` cannot be null"+ );+ assert_or_internal_err!(+ !rate.is_null(),+ "Rate of an `ExponentialDistribution` cannot be null"+ );+ let zero = ScalarValue::new_zero(&dt)?;+ assert_or_internal_err!(+ !rate.le(&zero),+ "Rate of an `ExponentialDistribution` must be positive"+ );+ Ok(Self {+ rate,+ offset,+ positive_tail,+ })}pub fn data_type(&self) -> DataType {@@ -412,15 +423,21 @@ impl ExponentialDistribution {impl GaussianDistribution {fn try_new(mean: ScalarValue, variance: ScalarValue) -> Result<Self> {let dt = mean.data_type();- if variance.data_type() != dt {- internal_err!("Mean and variance must have the same data type")- } else if variance.is_null() {- internal_err!("Variance of a `GaussianDistribution` cannot be null")- } else if variance.lt(&ScalarValue::new_zero(&dt)?) {- internal_err!("Variance of a `GaussianDistribution` must be positive")- } else {- Ok(Self { mean, variance })- }+ assert_eq_or_internal_err!(+ variance.data_type(),+ dt,+ "Mean and variance must have the same data type"+ );+ assert_or_internal_err!(+ !variance.is_null(),+ "Variance of a `GaussianDistribution` cannot be null"+ );+ let zero = ScalarValue::new_zero(&dt)?;+ assert_or_internal_err!(+ !variance.lt(&zero),+ "Variance of a `GaussianDistribution` must be positive"+ );+ Ok(Self { mean, variance })}pub fn data_type(&self) -> DataType {@@ -447,19 +464,16 @@ impl GaussianDistribution {impl BernoulliDistribution {fn try_new(p: ScalarValue) -> Result<Self> {if p.is_null() {- Ok(Self { p })- } else {- let dt = p.data_type();- let zero = ScalarValue::new_zero(&dt)?;- let one = ScalarValue::new_one(&dt)?;- if p.ge(&zero) && p.le(&one) {- Ok(Self { p })- } else {- internal_err!(- "Success probability of a `BernoulliDistribution` must be in [0, 1]"- )- }+ return Ok(Self { p });}+ let dt = p.data_type();+ let zero = ScalarValue::new_zero(&dt)?;+ let one = ScalarValue::new_one(&dt)?;+ assert_or_internal_err!(+ p.ge(&zero) && p.le(&one),+ "Success probability of a `BernoulliDistribution` must be in [0, 1]"+ );+ Ok(Self { p })}pub fn data_type(&self) -> DataType {@@ -521,11 +535,11 @@ impl GenericDistribution {variance: ScalarValue,range: Interval,) -> Result<Self> {- if range.data_type().eq(&DataType::Boolean) {- return internal_err!(- "Construction of a boolean `Generic` distribution is prohibited, create a `Bernoulli` distribution instead."- );- }+ assert_ne_or_internal_err!(+ range.data_type(),+ DataType::Boolean,+ "Construction of a boolean `Generic` distribution is prohibited, create a `Bernoulli` distribution instead."+ );let validate_location = |m: &ScalarValue| -> Result<bool> {// Checks whether the given location estimate is within the range.@@ -536,20 +550,24 @@ impl GenericDistribution {}};- if !validate_location(&mean)?- || !validate_location(&median)?- || (!variance.is_null()- && variance.lt(&ScalarValue::new_zero(&variance.data_type())?))- {- internal_err!("Tried to construct an invalid `GenericDistribution` instance")+ let locations_valid = validate_location(&mean)? && validate_location(&median)?;+ let variance_non_negative = if variance.is_null() {+ true} else {- Ok(Self {- mean,- median,- variance,- range,- })- }+ let zero = ScalarValue::new_zero(&variance.data_type())?;+ !variance.lt(&zero)+ };+ assert_or_internal_err!(+ locations_valid && variance_non_negative,+ "Tried to construct an invalid `GenericDistribution` instance"+ );++ Ok(Self {+ mean,+ median,+ variance,+ range,+ })}pub fn data_type(&self) -> DataType {