Skip to content

Commit df2763e

Browse files
kumarUjjawallogan-keede
authored andcommitted
minor: refactor with assert_or_internal_err!() in datafusion/physical-optimizer (apache#18732)
## 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 9df2a60 commit df2763e

3 files changed

Lines changed: 43 additions & 41 deletions

File tree

‎datafusion/physical-optimizer/src/coalesce_batches.rs‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ use crate::PhysicalOptimizerRule;
2323
use std::sync::Arc;
2424

2525
use datafusion_common::error::Result;
26-
use datafusion_common::{config::ConfigOptions, internal_err};
26+
use datafusion_common::{
27+
assert_eq_or_internal_err, config::ConfigOptions, DataFusionError,
28+
};
2729
use datafusion_physical_expr::Partitioning;
2830
use datafusion_physical_plan::{
2931
async_func::AsyncFuncExec, coalesce_batches::CoalesceBatchesExec,
@@ -76,11 +78,11 @@ impl PhysicalOptimizerRule for CoalesceBatches {
7678
} else if let Some(async_exec) = plan_any.downcast_ref::<AsyncFuncExec>() {
7779
// Coalesce inputs to async functions to reduce number of async function invocations
7880
let children = async_exec.children();
79-
if children.len() != 1 {
80-
return internal_err!(
81-
"Expected AsyncFuncExec to have exactly one child"
82-
);
83-
}
81+
assert_eq_or_internal_err!(
82+
children.len(),
83+
1,
84+
"Expected AsyncFuncExec to have exactly one child"
85+
);
8486

8587
let coalesce_exec = Arc::new(CoalesceBatchesExec::new(
8688
Arc::clone(children[0]),

‎datafusion/physical-optimizer/src/enforce_sorting/replace_with_order_preserving_variants.rs‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::utils::{
2727

2828
use datafusion_common::config::ConfigOptions;
2929
use datafusion_common::tree_node::Transformed;
30-
use datafusion_common::{internal_err, Result};
30+
use datafusion_common::{assert_or_internal_err, DataFusionError, Result};
3131
use datafusion_physical_plan::coalesce_partitions::CoalescePartitionsExec;
3232
use datafusion_physical_plan::execution_plan::EmissionType;
3333
use datafusion_physical_plan::repartition::RepartitionExec;
@@ -139,16 +139,21 @@ pub fn plan_with_order_preserving_variants(
139139
if let Some(ordering) = child.output_ordering() {
140140
let mut fetch = fetch;
141141
if let Some(coalesce_fetch) = sort_input.plan.fetch() {
142-
if let Some(sort_fetch) = fetch {
143-
if coalesce_fetch < sort_fetch {
144-
return internal_err!(
145-
"CoalescePartitionsExec fetch [{:?}] should be greater than or equal to SortExec fetch [{:?}]", coalesce_fetch, sort_fetch
142+
fetch = match fetch {
143+
Some(sort_fetch) => {
144+
assert_or_internal_err!(
145+
coalesce_fetch >= sort_fetch,
146+
"CoalescePartitionsExec fetch [{:?}] should be greater than or equal to SortExec fetch [{:?}]",
147+
coalesce_fetch,
148+
sort_fetch
146149
);
150+
Some(sort_fetch)
147151
}
148-
} else {
149-
// If the sort node does not have a fetch, we need to keep the coalesce node's fetch.
150-
fetch = Some(coalesce_fetch);
151-
}
152+
None => {
153+
// If the sort node does not have a fetch, we need to keep the coalesce node's fetch.
154+
Some(coalesce_fetch)
155+
}
156+
};
152157
};
153158
// When the input of a `CoalescePartitionsExec` has an ordering,
154159
// replace it with a `SortPreservingMergeExec` if appropriate:

‎datafusion/physical-optimizer/src/filter_pushdown.rs‎

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ use std::sync::Arc;
3636
use crate::PhysicalOptimizerRule;
3737

3838
use datafusion_common::tree_node::{TreeNode, TreeNodeRecursion};
39-
use datafusion_common::{config::ConfigOptions, internal_err, Result};
39+
use datafusion_common::{
40+
assert_eq_or_internal_err, config::ConfigOptions, DataFusionError, Result,
41+
};
4042
use datafusion_physical_expr::PhysicalExpr;
4143
use datafusion_physical_expr_common::physical_expr::is_volatile;
4244
use datafusion_physical_plan::filter_pushdown::{
@@ -461,22 +463,18 @@ fn push_down_filters(
461463

462464
let filter_description_parent_filters = filter_description.parent_filters();
463465
let filter_description_self_filters = filter_description.self_filters();
464-
if filter_description_parent_filters.len() != children.len() {
465-
return internal_err!(
466-
"Filter pushdown expected FilterDescription to have parent filters for {}, but got {} for node {}",
467-
children.len(),
468-
filter_description_parent_filters.len(),
469-
node.name()
470-
);
471-
}
472-
if filter_description_self_filters.len() != children.len() {
473-
return internal_err!(
474-
"Filter pushdown expected FilterDescription to have self filters for {}, but got {} for node {}",
475-
children.len(),
476-
filter_description_self_filters.len(),
477-
node.name()
478-
);
479-
}
466+
assert_eq_or_internal_err!(
467+
filter_description_parent_filters.len(),
468+
children.len(),
469+
"Filter pushdown expected parent filters count to match number of children for node {}",
470+
node.name()
471+
);
472+
assert_eq_or_internal_err!(
473+
filter_description_self_filters.len(),
474+
children.len(),
475+
"Filter pushdown expected self filters count to match number of children for node {}",
476+
node.name()
477+
);
480478

481479
for (child_idx, (child, parent_filters, self_filters)) in izip!(
482480
children,
@@ -525,15 +523,12 @@ fn push_down_filters(
525523
// from our parents and filters that the current node injected. We need to de-entangle
526524
// this since we do need to distinguish between them.
527525
let mut all_filters = result.filters.into_iter().collect_vec();
528-
if all_filters.len() != num_self_filters + num_parent_filters {
529-
return internal_err!(
530-
"Filter pushdown did not return the expected number of filters: expected {} self filters and {} parent filters, but got {}. Likely culprit is {}",
531-
num_self_filters,
532-
num_parent_filters,
533-
all_filters.len(),
534-
child.name()
535-
);
536-
}
526+
assert_eq_or_internal_err!(
527+
all_filters.len(),
528+
num_self_filters + num_parent_filters,
529+
"Filter pushdown did not return the expected number of filters from {}",
530+
child.name()
531+
);
537532
let parent_filters = all_filters
538533
.split_off(num_self_filters)
539534
.into_iter()

0 commit comments

Comments
 (0)