Skip to content

Commit

Permalink
Minor: avoid a clone in type coercion (#11530)
Browse files Browse the repository at this point in the history
* Minor: avoid a clone in type coercion

* Fix test
  • Loading branch information
alamb authored Jul 18, 2024
1 parent 4dd8532 commit 723a595
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
18 changes: 8 additions & 10 deletions datafusion/optimizer/src/analyzer/type_coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl AnalyzerRule for TypeCoercion {
/// Assumes that children have already been optimized
fn analyze_internal(
external_schema: &DFSchema,
mut plan: LogicalPlan,
plan: LogicalPlan,
) -> Result<Transformed<LogicalPlan>> {
// get schema representing all available input fields. This is used for data type
// resolution only, so order does not matter here
Expand All @@ -103,15 +103,13 @@ fn analyze_internal(
// select t2.c2 from t1 where t1.c1 in (select t2.c1 from t2 where t2.c2=t1.c3)
schema.merge(external_schema);

if let LogicalPlan::Filter(filter) = &mut plan {
if let Ok(new_predicate) = filter
.predicate
.clone()
.cast_to(&DataType::Boolean, filter.input.schema())
{
filter.predicate = new_predicate;
}
}
// Coerce filter predicates to boolean (handles `WHERE NULL`)
let plan = if let LogicalPlan::Filter(mut filter) = plan {
filter.predicate = filter.predicate.cast_to(&DataType::Boolean, &schema)?;
LogicalPlan::Filter(filter)
} else {
plan
};

let mut expr_rewrite = TypeCoercionRewriter::new(&schema);

Expand Down
4 changes: 4 additions & 0 deletions datafusion/sqllogictest/test_files/misc.slt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ query I
select 1 where NULL
----

# Where clause does not accept non boolean and has nice error message
query error Cannot create filter with non\-boolean predicate 'Utf8\("foo"\)' returning Utf8
select 1 where 'foo'

query I
select 1 where NULL and 1 = 1
----
Expand Down

0 comments on commit 723a595

Please sign in to comment.