Skip to content

Commit 3fdaf39

Browse files
kumarUjjawaladriangb
authored andcommitted
minor: refactor with assert_or_internal_err!() in datafusion/physical-expr-common (apache#18735)
## 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: Adrian Garcia Badaracco <[email protected]>
1 parent 17f7284 commit 3fdaf39

2 files changed

Lines changed: 30 additions & 30 deletions

File tree

‎datafusion/physical-expr-common/src/datum.rs‎

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use arrow::compute::kernels::cmp::{
2424
use arrow::compute::{ilike, like, nilike, nlike, SortOptions};
2525
use arrow::error::ArrowError;
2626
use datafusion_common::DataFusionError;
27-
use datafusion_common::{arrow_datafusion_err, internal_err};
27+
use datafusion_common::{arrow_datafusion_err, assert_or_internal_err, internal_err};
2828
use datafusion_common::{Result, ScalarValue};
2929
use datafusion_expr_common::columnar_value::ColumnarValue;
3030
use datafusion_expr_common::operator::Operator;
@@ -99,29 +99,24 @@ pub fn apply_cmp_for_nested(
9999
let left_data_type = lhs.data_type();
100100
let right_data_type = rhs.data_type();
101101

102-
if matches!(
103-
op,
104-
Operator::Eq
105-
| Operator::NotEq
106-
| Operator::Lt
107-
| Operator::Gt
108-
| Operator::LtEq
109-
| Operator::GtEq
110-
| Operator::IsDistinctFrom
111-
| Operator::IsNotDistinctFrom
112-
) && left_data_type.equals_datatype(&right_data_type)
113-
{
114-
apply(lhs, rhs, |l, r| {
115-
Ok(Arc::new(compare_op_for_nested(op, l, r)?))
116-
})
117-
} else {
118-
internal_err!(
119-
"invalid operator or data type mismatch for nested data, op {} left {}, right {}",
102+
assert_or_internal_err!(
103+
matches!(
120104
op,
121-
left_data_type,
122-
right_data_type
123-
)
124-
}
105+
Operator::Eq
106+
| Operator::NotEq
107+
| Operator::Lt
108+
| Operator::Gt
109+
| Operator::LtEq
110+
| Operator::GtEq
111+
| Operator::IsDistinctFrom
112+
| Operator::IsNotDistinctFrom
113+
) && left_data_type.equals_datatype(&right_data_type),
114+
"invalid operator or data type mismatch for nested data, op {op} left {left_data_type}, right {right_data_type}",
115+
);
116+
117+
apply(lhs, rhs, |l, r| {
118+
Ok(Arc::new(compare_op_for_nested(op, l, r)?))
119+
})
125120
}
126121

127122
/// Compare with eq with either nested or non-nested
@@ -148,9 +143,7 @@ pub fn compare_op_for_nested(
148143
let l_len = l.len();
149144
let r_len = r.len();
150145

151-
if l_len != r_len && !is_l_scalar && !is_r_scalar {
152-
return internal_err!("len mismatch");
153-
}
146+
assert_or_internal_err!(l_len == r_len || is_l_scalar || is_r_scalar, "len mismatch");
154147

155148
let len = match is_l_scalar {
156149
true => r_len,

‎datafusion/physical-expr-common/src/physical_expr.rs‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ use arrow::record_batch::RecordBatch;
3030
use datafusion_common::tree_node::{
3131
Transformed, TransformedResult, TreeNode, TreeNodeRecursion,
3232
};
33-
use datafusion_common::{exec_err, internal_err, not_impl_err, Result, ScalarValue};
33+
use datafusion_common::{
34+
assert_eq_or_internal_err, exec_err, not_impl_err, DataFusionError, Result,
35+
ScalarValue,
36+
};
3437
use datafusion_expr_common::columnar_value::ColumnarValue;
3538
use datafusion_expr_common::interval_arithmetic::Interval;
3639
use datafusion_expr_common::sort_properties::ExprProperties;
@@ -453,9 +456,13 @@ pub fn with_new_children_if_necessary(
453456
children: Vec<Arc<dyn PhysicalExpr>>,
454457
) -> Result<Arc<dyn PhysicalExpr>> {
455458
let old_children = expr.children();
456-
if children.len() != old_children.len() {
457-
internal_err!("PhysicalExpr: Wrong number of children")
458-
} else if children.is_empty()
459+
assert_eq_or_internal_err!(
460+
children.len(),
461+
old_children.len(),
462+
"PhysicalExpr: Wrong number of children"
463+
);
464+
465+
if children.is_empty()
459466
|| children
460467
.iter()
461468
.zip(old_children.iter())

0 commit comments

Comments
 (0)