Skip to content

Commit 0346cbc

Browse files
committed
chore: Optimize schema rewriter usages
1 parent 51f13d7 commit 0346cbc

1 file changed

Lines changed: 21 additions & 11 deletions

File tree

datafusion/datasource-parquet/src/opener.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -358,17 +358,27 @@ impl FileOpener for ParquetOpener {
358358
// and we can avoid doing any more work on the file (bloom filters, loading the page index, etc.).
359359
// Additionally, if any casts were inserted we can move casts from the column to the literal side:
360360
// `CAST(col AS INT) = 5` can become `col = CAST(5 AS <col type>)`, which can be evaluated statically.
361-
let rewriter = expr_adapter_factory.create(
362-
Arc::clone(&logical_file_schema),
363-
Arc::clone(&physical_file_schema),
364-
)?;
365-
let simplifier = PhysicalExprSimplifier::new(&physical_file_schema);
366-
predicate = predicate
367-
.map(|p| simplifier.simplify(rewriter.rewrite(p)?))
368-
.transpose()?;
369-
// Adapt projections to the physical file schema as well
370-
projection = projection
371-
.try_map_exprs(|p| simplifier.simplify(rewriter.rewrite(p)?))?;
361+
//
362+
// When the schemas are identical and there is no predicate, the
363+
// rewriter is a no-op: column indices already match (partition
364+
// columns are appended after file columns in the table schema),
365+
// types are the same, and there are no missing columns. Skip the
366+
// tree walk entirely in that case.
367+
let needs_rewrite =
368+
predicate.is_some() || logical_file_schema != physical_file_schema;
369+
if needs_rewrite {
370+
let rewriter = expr_adapter_factory.create(
371+
Arc::clone(&logical_file_schema),
372+
Arc::clone(&physical_file_schema),
373+
)?;
374+
let simplifier = PhysicalExprSimplifier::new(&physical_file_schema);
375+
predicate = predicate
376+
.map(|p| simplifier.simplify(rewriter.rewrite(p)?))
377+
.transpose()?;
378+
// Adapt projections to the physical file schema as well
379+
projection = projection
380+
.try_map_exprs(|p| simplifier.simplify(rewriter.rewrite(p)?))?;
381+
}
372382

373383
// Build predicates for this specific file
374384
let (pruning_predicate, page_pruning_predicate) = build_pruning_predicates(

0 commit comments

Comments
 (0)