Skip to content

Commit fc16bd8

Browse files
kumarUjjawallogan-keede
authored andcommitted
minor: refactor with assert_or_internal_err!() in datafusion/expr-common (apache#18702)
## 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. -->
1 parent 1476682 commit fc16bd8

3 files changed

Lines changed: 208 additions & 173 deletions

File tree

‎datafusion/expr-common/src/columnar_value.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ impl ColumnarValue {
247247
Some(array_len)
248248
} else {
249249
return internal_err!(
250-
"Arguments has mixed length. Expected length: {array_len}, found length: {}", a.len()
250+
"Arguments has mixed length. Expected length: {array_len}, found length: {}",
251+
a.len()
251252
);
252253
}
253254
}

‎datafusion/expr-common/src/interval_arithmetic.rs‎

Lines changed: 128 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ use arrow::datatypes::{
3131
MIN_DECIMAL128_FOR_EACH_PRECISION, MIN_DECIMAL256_FOR_EACH_PRECISION,
3232
};
3333
use datafusion_common::rounding::{alter_fp_rounding_mode, next_down, next_up};
34-
use datafusion_common::{internal_err, Result, ScalarValue};
34+
use datafusion_common::{
35+
assert_eq_or_internal_err, assert_or_internal_err, internal_err, DataFusionError,
36+
Result, ScalarValue,
37+
};
3538

3639
macro_rules! get_extreme_value {
3740
($extreme:ident, $value:expr) => {
@@ -266,24 +269,23 @@ impl Interval {
266269
/// - Floating-point endpoints with `NaN`, `INF`, or `NEG_INF` are converted
267270
/// to `NULL`s.
268271
pub fn try_new(lower: ScalarValue, upper: ScalarValue) -> Result<Self> {
269-
if lower.data_type() != upper.data_type() {
270-
return internal_err!("Endpoints of an Interval should have the same type");
271-
}
272+
assert_eq_or_internal_err!(
273+
lower.data_type(),
274+
upper.data_type(),
275+
"Endpoints of an Interval should have the same type"
276+
);
272277

273278
let interval = Self::new(lower, upper);
274279

275-
if interval.lower.is_null()
276-
|| interval.upper.is_null()
277-
|| interval.lower <= interval.upper
278-
{
279-
Ok(interval)
280-
} else {
281-
internal_err!(
282-
"Interval's lower bound {} is greater than the upper bound {}",
283-
interval.lower,
284-
interval.upper
285-
)
286-
}
280+
assert_or_internal_err!(
281+
interval.lower.is_null()
282+
|| interval.upper.is_null()
283+
|| interval.lower <= interval.upper,
284+
"Interval's lower bound {} is greater than the upper bound {}",
285+
interval.lower,
286+
interval.upper
287+
);
288+
Ok(interval)
287289
}
288290

289291
/// Only for internal usage. Responsible for standardizing booleans and
@@ -454,15 +456,16 @@ impl Interval {
454456
/// to an error.
455457
pub fn gt<T: Borrow<Self>>(&self, other: T) -> Result<Self> {
456458
let rhs = other.borrow();
457-
if self.data_type().ne(&rhs.data_type()) {
458-
internal_err!(
459-
"Only intervals with the same data type are comparable, lhs:{}, rhs:{}",
460-
self.data_type(),
461-
rhs.data_type()
462-
)
463-
} else if !(self.upper.is_null() || rhs.lower.is_null())
464-
&& self.upper <= rhs.lower
465-
{
459+
let lhs_type = self.data_type();
460+
let rhs_type = rhs.data_type();
461+
assert_eq_or_internal_err!(
462+
lhs_type,
463+
rhs_type,
464+
"Only intervals with the same data type are comparable, lhs:{}, rhs:{}",
465+
self.data_type(),
466+
rhs.data_type()
467+
);
468+
if !(self.upper.is_null() || rhs.lower.is_null()) && self.upper <= rhs.lower {
466469
// Values in this interval are certainly less than or equal to
467470
// those in the given interval.
468471
Ok(Self::CERTAINLY_FALSE)
@@ -487,15 +490,16 @@ impl Interval {
487490
/// to an error.
488491
pub fn gt_eq<T: Borrow<Self>>(&self, other: T) -> Result<Self> {
489492
let rhs = other.borrow();
490-
if self.data_type().ne(&rhs.data_type()) {
491-
internal_err!(
492-
"Only intervals with the same data type are comparable, lhs:{}, rhs:{}",
493-
self.data_type(),
494-
rhs.data_type()
495-
)
496-
} else if !(self.lower.is_null() || rhs.upper.is_null())
497-
&& self.lower >= rhs.upper
498-
{
493+
let lhs_type = self.data_type();
494+
let rhs_type = rhs.data_type();
495+
assert_eq_or_internal_err!(
496+
lhs_type,
497+
rhs_type,
498+
"Only intervals with the same data type are comparable, lhs:{}, rhs:{}",
499+
self.data_type(),
500+
rhs.data_type()
501+
);
502+
if !(self.lower.is_null() || rhs.upper.is_null()) && self.lower >= rhs.upper {
499503
// Values in this interval are certainly greater than or equal to
500504
// those in the given interval.
501505
Ok(Self::CERTAINLY_TRUE)
@@ -542,16 +546,17 @@ impl Interval {
542546
/// to an error.
543547
pub fn equal<T: Borrow<Self>>(&self, other: T) -> Result<Self> {
544548
let rhs = other.borrow();
545-
if BinaryTypeCoercer::new(&self.data_type(), &Operator::Eq, &rhs.data_type())
546-
.get_result_type()
547-
.is_err()
548-
{
549-
internal_err!(
550-
"Interval data types must be compatible for equality checks, lhs:{}, rhs:{}",
551-
self.data_type(),
552-
rhs.data_type()
553-
)
554-
} else if !self.lower.is_null()
549+
let types_compatible =
550+
BinaryTypeCoercer::new(&self.data_type(), &Operator::Eq, &rhs.data_type())
551+
.get_result_type()
552+
.is_ok();
553+
assert_or_internal_err!(
554+
types_compatible,
555+
"Interval data types must be compatible for equality checks, lhs:{}, rhs:{}",
556+
self.data_type(),
557+
rhs.data_type()
558+
);
559+
if !self.lower.is_null()
555560
&& (self.lower == self.upper)
556561
&& (rhs.lower == rhs.upper)
557562
&& (self.lower == rhs.lower)
@@ -616,9 +621,12 @@ impl Interval {
616621

617622
/// Compute the logical negation of this (boolean) interval.
618623
pub fn not(&self) -> Result<Self> {
619-
if self.data_type().ne(&DataType::Boolean) {
620-
internal_err!("Cannot apply logical negation to a non-boolean interval")
621-
} else if self == &Self::CERTAINLY_TRUE {
624+
assert_eq_or_internal_err!(
625+
self.data_type(),
626+
DataType::Boolean,
627+
"Cannot apply logical negation to a non-boolean interval"
628+
);
629+
if self == &Self::CERTAINLY_TRUE {
622630
Ok(Self::CERTAINLY_FALSE)
623631
} else if self == &Self::CERTAINLY_FALSE {
624632
Ok(Self::CERTAINLY_TRUE)
@@ -635,13 +643,15 @@ impl Interval {
635643
/// to an error.
636644
pub fn intersect<T: Borrow<Self>>(&self, other: T) -> Result<Option<Self>> {
637645
let rhs = other.borrow();
638-
if self.data_type().ne(&rhs.data_type()) {
639-
return internal_err!(
640-
"Only intervals with the same data type are intersectable, lhs:{}, rhs:{}",
641-
self.data_type(),
642-
rhs.data_type()
643-
);
644-
};
646+
let lhs_type = self.data_type();
647+
let rhs_type = rhs.data_type();
648+
assert_eq_or_internal_err!(
649+
lhs_type,
650+
rhs_type,
651+
"Only intervals with the same data type are intersectable, lhs:{}, rhs:{}",
652+
self.data_type(),
653+
rhs.data_type()
654+
);
645655

646656
// If it is evident that the result is an empty interval, short-circuit
647657
// and directly return `None`.
@@ -670,13 +680,15 @@ impl Interval {
670680
/// to an error.
671681
pub fn union<T: Borrow<Self>>(&self, other: T) -> Result<Self> {
672682
let rhs = other.borrow();
673-
if self.data_type().ne(&rhs.data_type()) {
674-
return internal_err!(
675-
"Cannot calculate the union of intervals with different data types, lhs:{}, rhs:{}",
676-
self.data_type(),
677-
rhs.data_type()
678-
);
679-
};
683+
let lhs_type = self.data_type();
684+
let rhs_type = rhs.data_type();
685+
assert_eq_or_internal_err!(
686+
lhs_type,
687+
rhs_type,
688+
"Cannot calculate the union of intervals with different data types, lhs:{}, rhs:{}",
689+
self.data_type(),
690+
rhs.data_type()
691+
);
680692

681693
let lower = if self.lower.is_null()
682694
|| (!rhs.lower.is_null() && self.lower <= rhs.lower)
@@ -706,27 +718,28 @@ impl Interval {
706718
pub fn contains_value<T: Borrow<ScalarValue>>(&self, other: T) -> Result<bool> {
707719
let rhs = other.borrow();
708720

709-
let (lhs_lower, lhs_upper, rhs) = if self.data_type().eq(&rhs.data_type()) {
710-
(&self.lower, &self.upper, rhs)
711-
} else if let Some(common_type) =
712-
comparison_coercion_numeric(&self.data_type(), &rhs.data_type())
713-
{
714-
(
715-
&self.lower.cast_to(&common_type)?,
716-
&self.upper.cast_to(&common_type)?,
717-
&rhs.cast_to(&common_type)?,
718-
)
721+
let (lhs_lower, lhs_upper, rhs_value) = if self.data_type().eq(&rhs.data_type()) {
722+
(self.lower.clone(), self.upper.clone(), rhs.clone())
719723
} else {
720-
return internal_err!(
724+
let maybe_common_type =
725+
comparison_coercion_numeric(&self.data_type(), &rhs.data_type());
726+
assert_or_internal_err!(
727+
maybe_common_type.is_some(),
721728
"Data types must be compatible for containment checks, lhs:{}, rhs:{}",
722729
self.data_type(),
723730
rhs.data_type()
724731
);
732+
let common_type = maybe_common_type.expect("checked for Some");
733+
(
734+
self.lower.cast_to(&common_type)?,
735+
self.upper.cast_to(&common_type)?,
736+
rhs.cast_to(&common_type)?,
737+
)
725738
};
726739

727740
// We only check the upper bound for a `None` value because `None`
728741
// values are less than `Some` values according to Rust.
729-
Ok(lhs_lower <= rhs && (lhs_upper.is_null() || rhs <= lhs_upper))
742+
Ok(lhs_lower <= rhs_value && (lhs_upper.is_null() || rhs_value <= lhs_upper))
730743
}
731744

732745
/// Decide if this interval is a superset of, overlaps with, or
@@ -738,13 +751,15 @@ impl Interval {
738751
/// to an error.
739752
pub fn contains<T: Borrow<Self>>(&self, other: T) -> Result<Self> {
740753
let rhs = other.borrow();
741-
if self.data_type().ne(&rhs.data_type()) {
742-
return internal_err!(
743-
"Interval data types must match for containment checks, lhs:{}, rhs:{}",
744-
self.data_type(),
745-
rhs.data_type()
746-
);
747-
};
754+
let lhs_type = self.data_type();
755+
let rhs_type = rhs.data_type();
756+
assert_eq_or_internal_err!(
757+
lhs_type,
758+
rhs_type,
759+
"Interval data types must match for containment checks, lhs:{}, rhs:{}",
760+
self.data_type(),
761+
rhs.data_type()
762+
);
748763

749764
match self.intersect(rhs)? {
750765
Some(intersection) => {
@@ -813,15 +828,15 @@ impl Interval {
813828
/// to an error.
814829
pub fn mul<T: Borrow<Self>>(&self, other: T) -> Result<Self> {
815830
let rhs = other.borrow();
816-
let dt = if self.data_type().eq(&rhs.data_type()) {
817-
self.data_type()
818-
} else {
819-
return internal_err!(
820-
"Intervals must have the same data type for multiplication, lhs:{}, rhs:{}",
821-
self.data_type(),
822-
rhs.data_type()
823-
);
824-
};
831+
let dt = self.data_type();
832+
let rhs_type = rhs.data_type();
833+
assert_eq_or_internal_err!(
834+
dt.clone(),
835+
rhs_type.clone(),
836+
"Intervals must have the same data type for multiplication, lhs:{}, rhs:{}",
837+
dt.clone(),
838+
rhs_type.clone()
839+
);
825840

826841
let zero = ScalarValue::new_zero(&dt)?;
827842

@@ -856,15 +871,15 @@ impl Interval {
856871
/// zero should result in an interval set, not the universal set.
857872
pub fn div<T: Borrow<Self>>(&self, other: T) -> Result<Self> {
858873
let rhs = other.borrow();
859-
let dt = if self.data_type().eq(&rhs.data_type()) {
860-
self.data_type()
861-
} else {
862-
return internal_err!(
863-
"Intervals must have the same data type for division, lhs:{}, rhs:{}",
864-
self.data_type(),
865-
rhs.data_type()
866-
);
867-
};
874+
let dt = self.data_type();
875+
let rhs_type = rhs.data_type();
876+
assert_eq_or_internal_err!(
877+
dt.clone(),
878+
rhs_type.clone(),
879+
"Intervals must have the same data type for division, lhs:{}, rhs:{}",
880+
dt.clone(),
881+
rhs_type.clone()
882+
);
868883

869884
let zero = ScalarValue::new_zero(&dt)?;
870885
// We want 0 to be approachable from both negative and positive sides.
@@ -1317,13 +1332,15 @@ pub fn satisfy_greater(
13171332
right: &Interval,
13181333
strict: bool,
13191334
) -> Result<Option<(Interval, Interval)>> {
1320-
if left.data_type().ne(&right.data_type()) {
1321-
return internal_err!(
1322-
"Intervals must have the same data type, lhs:{}, rhs:{}",
1323-
left.data_type(),
1324-
right.data_type()
1325-
);
1326-
}
1335+
let lhs_type = left.data_type();
1336+
let rhs_type = right.data_type();
1337+
assert_eq_or_internal_err!(
1338+
lhs_type.clone(),
1339+
rhs_type.clone(),
1340+
"Intervals must have the same data type, lhs:{}, rhs:{}",
1341+
lhs_type,
1342+
rhs_type
1343+
);
13271344

13281345
if !left.upper.is_null() && left.upper <= right.lower {
13291346
if !strict && left.upper == right.lower {
@@ -1811,13 +1828,12 @@ impl NullableInterval {
18111828
fn not(&self) -> Result<Self> {
18121829
match self {
18131830
Self::Null { datatype } => {
1814-
if datatype == &DataType::Boolean {
1815-
Ok(Self::UNKNOWN)
1816-
} else {
1817-
internal_err!(
1818-
"Cannot apply logical negation to a non-boolean interval"
1819-
)
1820-
}
1831+
assert_eq_or_internal_err!(
1832+
datatype,
1833+
&DataType::Boolean,
1834+
"Cannot apply logical negation to a non-boolean interval"
1835+
);
1836+
Ok(Self::UNKNOWN)
18211837
}
18221838
Self::MaybeNull { values } => Ok(Self::MaybeNull {
18231839
values: values.not()?,

0 commit comments

Comments
 (0)