Skip to content

Commit e6378f4

Browse files
Replace panic in datafusion-expr crate (#3341)
1 parent 30fce22 commit e6378f4

File tree

5 files changed

+33
-25
lines changed

5 files changed

+33
-25
lines changed

datafusion/expr/src/binary_rule.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,10 @@ fn coercion_decimal_mathematics_type(
382382
let result_precision = result_scale + (*p1 - *s1).min(*p2 - *s2);
383383
Some(create_decimal_type(result_precision, result_scale))
384384
}
385-
_ => unreachable!(),
385+
_ => None,
386386
}
387387
}
388-
_ => unreachable!(),
388+
_ => None,
389389
}
390390
}
391391

datafusion/expr/src/expr_rewriter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ fn rewrite_sort_col_by_aggs(expr: Expr, plan: &LogicalPlan) -> Result<Expr> {
386386
// The expr is not based on Aggregate plan output. Skip it.
387387
return Ok(expr);
388388
}
389-
let normalized_expr = normalized_expr.unwrap();
389+
let normalized_expr = normalized_expr?;
390390
if let Some(found_agg) = self
391391
.aggr_expr
392392
.iter()

datafusion/expr/src/logical_plan/builder.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -607,12 +607,12 @@ impl LogicalPlanBuilder {
607607
for (l, r) in &on {
608608
if self.plan.schema().field_from_column(l).is_ok()
609609
&& right.schema().field_from_column(r).is_ok()
610-
&& can_hash(self.plan.schema().field_from_column(l).unwrap().data_type())
610+
&& can_hash(self.plan.schema().field_from_column(l)?.data_type())
611611
{
612612
join_on.push((l.clone(), r.clone()));
613613
} else if self.plan.schema().field_from_column(r).is_ok()
614614
&& right.schema().field_from_column(l).is_ok()
615-
&& can_hash(self.plan.schema().field_from_column(r).unwrap().data_type())
615+
&& can_hash(self.plan.schema().field_from_column(r)?.data_type())
616616
{
617617
join_on.push((r.clone(), l.clone()));
618618
} else {
@@ -629,7 +629,9 @@ impl LogicalPlanBuilder {
629629
}
630630
if join_on.is_empty() {
631631
let join = Self::from(self.plan.clone()).cross_join(&right.clone())?;
632-
join.filter(filters.unwrap())
632+
join.filter(filters.ok_or_else(|| {
633+
DataFusionError::Internal("filters should not be None here".to_string())
634+
})?)
633635
} else {
634636
Ok(Self::from(LogicalPlan::Join(Join {
635637
left: Arc::new(self.plan.clone()),
@@ -901,18 +903,16 @@ pub fn union_with_alias(
901903
.map(|p| match p.as_ref() {
902904
LogicalPlan::Projection(Projection {
903905
expr, input, alias, ..
904-
}) => Arc::new(
905-
project_with_column_index_alias(
906-
expr.to_vec(),
907-
input.clone(),
908-
union_schema.clone(),
909-
alias.clone(),
910-
)
911-
.unwrap(),
912-
),
913-
x => Arc::new(x.clone()),
906+
}) => Ok(Arc::new(project_with_column_index_alias(
907+
expr.to_vec(),
908+
input.clone(),
909+
union_schema.clone(),
910+
alias.clone(),
911+
)?)),
912+
x => Ok(Arc::new(x.clone())),
914913
})
915-
.collect::<Vec<_>>();
914+
.into_iter()
915+
.collect::<Result<Vec<_>>>()?;
916916

917917
if inputs.is_empty() {
918918
return Err(DataFusionError::Plan("Empty UNION".to_string()));

datafusion/expr/src/logical_plan/display.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,12 @@ impl<'a, 'b> PlanVisitor for GraphvizVisitor<'a, 'b> {
231231
_plan: &LogicalPlan,
232232
) -> std::result::Result<bool, fmt::Error> {
233233
// always be non-empty as pre_visit always pushes
234-
self.parent_ids.pop().unwrap();
235-
Ok(true)
234+
// So it should always be Ok(true)
235+
let res = self.parent_ids.pop();
236+
match res {
237+
Some(_) => Ok(true),
238+
None => Err(fmt::Error),
239+
}
236240
}
237241
}
238242

datafusion/expr/src/logical_plan/plan.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,10 @@ impl LogicalPlan {
549549
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
550550
let with_schema = false;
551551
let mut visitor = IndentVisitor::new(f, with_schema);
552-
self.0.accept(&mut visitor).unwrap();
553-
Ok(())
552+
match self.0.accept(&mut visitor) {
553+
Ok(_) => Ok(()),
554+
Err(_) => Err(fmt::Error),
555+
}
554556
}
555557
}
556558
Wrapper(self)
@@ -590,8 +592,10 @@ impl LogicalPlan {
590592
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
591593
let with_schema = true;
592594
let mut visitor = IndentVisitor::new(f, with_schema);
593-
self.0.accept(&mut visitor).unwrap();
594-
Ok(())
595+
match self.0.accept(&mut visitor) {
596+
Ok(_) => Ok(()),
597+
Err(_) => Err(fmt::Error),
598+
}
595599
}
596600
}
597601
Wrapper(self)
@@ -641,12 +645,12 @@ impl LogicalPlan {
641645
let mut visitor = GraphvizVisitor::new(f);
642646

643647
visitor.pre_visit_plan("LogicalPlan")?;
644-
self.0.accept(&mut visitor).unwrap();
648+
self.0.accept(&mut visitor).map_err(|_| fmt::Error)?;
645649
visitor.post_visit_plan()?;
646650

647651
visitor.set_with_schema(true);
648652
visitor.pre_visit_plan("Detailed LogicalPlan")?;
649-
self.0.accept(&mut visitor).unwrap();
653+
self.0.accept(&mut visitor).map_err(|_| fmt::Error)?;
650654
visitor.post_visit_plan()?;
651655

652656
writeln!(f, "}}")?;

0 commit comments

Comments
 (0)