Skip to content

Commit be6efbc

Browse files
suxiaogang223Dandandanalamb
authored
[feat]:fast check has column (#5328)
* impl has_column * replace is_err by has_column * add comment * avoid str allocate in Err, error should not be returned * Update datafusion/common/src/dfschema.rs Co-authored-by: Daniël Heres <[email protected]> * Update datafusion/common/src/dfschema.rs Co-authored-by: Daniël Heres <[email protected]> * format * avoid the collect() * fix one more is_ok --------- Co-authored-by: Daniël Heres <[email protected]> Co-authored-by: Andrew Lamb <[email protected]>
1 parent 1455b02 commit be6efbc

File tree

5 files changed

+34
-16
lines changed

5 files changed

+34
-16
lines changed

datafusion/common/src/dfschema.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,27 @@ impl DFSchema {
331331
}
332332
}
333333

334+
/// Find if the field exists with the given name
335+
pub fn has_column_with_unqualified_name(&self, name: &str) -> bool {
336+
self.fields().iter().any(|field| field.name() == name)
337+
}
338+
339+
/// Find if the field exists with the given qualified name
340+
pub fn has_column_with_qualified_name(&self, qualifier: &str, name: &str) -> bool {
341+
self.fields().iter().any(|field| {
342+
field.qualifier().map(|q| q.eq(qualifier)).unwrap_or(false)
343+
&& field.name() == name
344+
})
345+
}
346+
347+
/// Find if the field exists with the given qualified column
348+
pub fn has_column(&self, column: &Column) -> bool {
349+
match &column.relation {
350+
Some(r) => self.has_column_with_qualified_name(r, &column.name),
351+
None => self.has_column_with_unqualified_name(&column.name),
352+
}
353+
}
354+
334355
/// Check to see if unqualified field names matches field names in Arrow schema
335356
pub fn matches_arrow_schema(&self, arrow_schema: &Schema) -> bool {
336357
self.fields

datafusion/expr/src/logical_plan/builder.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,7 @@ impl LogicalPlanBuilder {
377377
input,
378378
mut expr,
379379
schema: _,
380-
}) if missing_cols
381-
.iter()
382-
.all(|c| input.schema().field_from_column(c).is_ok()) =>
383-
{
380+
}) if missing_cols.iter().all(|c| input.schema().has_column(c)) => {
384381
let mut missing_exprs = missing_cols
385382
.iter()
386383
.map(|c| normalize_col(Expr::Column(c.clone()), &input))
@@ -723,13 +720,13 @@ impl LogicalPlanBuilder {
723720
let mut join_on: Vec<(Expr, Expr)> = vec![];
724721
let mut filters: Option<Expr> = None;
725722
for (l, r) in &on {
726-
if self.plan.schema().field_from_column(l).is_ok()
727-
&& right.schema().field_from_column(r).is_ok()
723+
if self.plan.schema().has_column(l)
724+
&& right.schema().has_column(r)
728725
&& can_hash(self.plan.schema().field_from_column(l)?.data_type())
729726
{
730727
join_on.push((Expr::Column(l.clone()), Expr::Column(r.clone())));
731-
} else if self.plan.schema().field_from_column(r).is_ok()
732-
&& right.schema().field_from_column(l).is_ok()
728+
} else if self.plan.schema().has_column(l)
729+
&& right.schema().has_column(r)
733730
&& can_hash(self.plan.schema().field_from_column(r)?.data_type())
734731
{
735732
join_on.push((Expr::Column(r.clone()), Expr::Column(l.clone())));

datafusion/optimizer/src/decorrelate_where_exists.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ fn optimize_exists(
172172
let using_cols: Vec<Column> = expr
173173
.to_columns()?
174174
.into_iter()
175-
.filter(|col| input_schema.field_from_column(col).is_ok())
175+
.filter(|col| input_schema.has_column(col))
176176
.collect::<_>();
177177

178178
cols.extend(using_cols);

datafusion/optimizer/src/decorrelate_where_in.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ fn optimize_where_in(
166166
let using_cols: Vec<Column> = expr
167167
.to_columns()?
168168
.into_iter()
169-
.filter(|col| input_schema.field_from_column(col).is_ok())
169+
.filter(|col| input_schema.has_column(col))
170170
.collect::<_>();
171171

172172
cols.extend(using_cols);

datafusion/optimizer/src/eliminate_outer_join.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ impl OptimizerRule for EliminateOuterJoin {
8484
let mut left_non_nullable = false;
8585
let mut right_non_nullable = false;
8686
for col in non_nullable_cols.iter() {
87-
if join.left.schema().field_from_column(col).is_ok() {
87+
if join.left.schema().has_column(col) {
8888
left_non_nullable = true;
8989
}
90-
if join.right.schema().field_from_column(col).is_ok() {
90+
if join.right.schema().has_column(col) {
9191
right_non_nullable = true;
9292
}
9393
}
@@ -251,10 +251,10 @@ fn extract_non_nullable_columns(
251251
{
252252
for left_col in &left_non_nullable_cols {
253253
for right_col in &right_non_nullable_cols {
254-
if (left_schema.field_from_column(left_col).is_ok()
255-
&& left_schema.field_from_column(right_col).is_ok())
256-
|| (right_schema.field_from_column(left_col).is_ok()
257-
&& right_schema.field_from_column(right_col).is_ok())
254+
if (left_schema.has_column(left_col)
255+
&& left_schema.has_column(right_col))
256+
|| (right_schema.has_column(left_col)
257+
&& right_schema.has_column(right_col))
258258
{
259259
non_nullable_cols.push(left_col.clone());
260260
break;

0 commit comments

Comments
 (0)