Skip to content

Commit c89b10f

Browse files
authored
Minor: Improvements to type coercion rule (#3379)
* Clean up code in type_coercion.rs * fix: improve error message
1 parent 08a85db commit c89b10f

2 files changed

Lines changed: 14 additions & 33 deletions

File tree

datafusion/optimizer/src/type_coercion.rs

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
//! Optimizer rule for type validation and coercion
1919
2020
use crate::{OptimizerConfig, OptimizerRule};
21-
use arrow::datatypes::DataType;
2221
use datafusion_common::{DFSchema, DFSchemaRef, Result};
2322
use datafusion_expr::binary_rule::coerce_types;
2423
use datafusion_expr::expr_rewriter::{ExprRewritable, ExprRewriter, RewriteRecursion};
@@ -86,37 +85,18 @@ impl ExprRewriter for TypeCoercionRewriter {
8685
}
8786

8887
fn mutate(&mut self, expr: Expr) -> Result<Expr> {
89-
match &expr {
88+
match expr {
9089
Expr::BinaryExpr { left, op, right } => {
9190
let left_type = left.get_type(&self.schema)?;
9291
let right_type = right.get_type(&self.schema)?;
93-
match right_type {
94-
DataType::Interval(_) => {
95-
// we don't want to cast intervals because that breaks
96-
// the logic in the physical planner
97-
Ok(expr)
98-
}
99-
_ => {
100-
let coerced_type = coerce_types(&left_type, op, &right_type)?;
101-
let left = left.clone().cast_to(&coerced_type, &self.schema)?;
102-
let right = right.clone().cast_to(&coerced_type, &self.schema)?;
103-
match (&left, &right) {
104-
(Expr::Cast { .. }, _) | (_, Expr::Cast { .. }) => {
105-
Ok(Expr::BinaryExpr {
106-
left: Box::new(left),
107-
op: *op,
108-
right: Box::new(right),
109-
})
110-
}
111-
_ => {
112-
// no cast was added so we return the original expression
113-
Ok(expr)
114-
}
115-
}
116-
}
117-
}
92+
let coerced_type = coerce_types(&left_type, &op, &right_type)?;
93+
Ok(Expr::BinaryExpr {
94+
left: Box::new(left.cast_to(&coerced_type, &self.schema)?),
95+
op,
96+
right: Box::new(right.cast_to(&coerced_type, &self.schema)?),
97+
})
11898
}
119-
_ => Ok(expr),
99+
expr => Ok(expr),
120100
}
121101
}
122102
}

datafusion/physical-expr/src/planner.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ pub fn create_physical_expr(
4848
execution_props: &ExecutionProps,
4949
) -> Result<Arc<dyn PhysicalExpr>> {
5050
if input_schema.fields.len() != input_dfschema.fields().len() {
51-
return Err(DataFusionError::Internal(
52-
"create_physical_expr passed Arrow schema and DataFusion \
53-
schema with different number of fields"
54-
.to_string(),
55-
));
51+
return Err(DataFusionError::Internal(format!(
52+
"create_physical_expr expected same number of fields, got \
53+
got Arrow schema with {} and DataFusion schema with {}",
54+
input_schema.fields.len(),
55+
input_dfschema.fields().len()
56+
)));
5657
}
5758
match e {
5859
Expr::Alias(expr, ..) => Ok(create_physical_expr(

0 commit comments

Comments
 (0)