Skip to content

Apache DataFusion SQL Query Engine

Fix lost filters and projections in ParquetExec, CSVExec etc (#2077)

Yang Jiang3d319158d90f
diff --git a/datafusion/src/physical_optimizer/pruning.rs b/datafusion/src/physical_optimizer/pruning.rs
index 77902c761a..67b7476e55 100644
--- a/datafusion/src/physical_optimizer/pruning.rs
+++ b/datafusion/src/physical_optimizer/pruning.rs
@@ -210,6 +210,11 @@ impl PruningPredicate {
pub fn logical_expr(&self) -> &Expr {
&self.logical_expr
}
+
+ /// Returns a reference to the predicate expr
+ pub fn predicate_expr(&self) -> &Arc<dyn PhysicalExpr> {
+ &self.predicate_expr
+ }
}
/// Handles creating references to the min/max statistics
diff --git a/datafusion/src/physical_optimizer/repartition.rs b/datafusion/src/physical_optimizer/repartition.rs
index c3556c394e..b0dbc77135 100644
--- a/datafusion/src/physical_optimizer/repartition.rs
+++ b/datafusion/src/physical_optimizer/repartition.rs
@@ -365,7 +365,7 @@ mod tests {
"HashAggregateExec: mode=Final, gby=[], aggr=[]",
"HashAggregateExec: mode=Partial, gby=[], aggr=[]",
"RepartitionExec: partitioning=RoundRobinBatch(10)",
- "ParquetExec: limit=None, partitions=[x]",
+ "ParquetExec: limit=None, partitions=[x], projection=[c1]",
];
assert_optimized!(expected, plan);
@@ -381,7 +381,7 @@ mod tests {
"HashAggregateExec: mode=Partial, gby=[], aggr=[]",
"FilterExec: c1@0",
"RepartitionExec: partitioning=RoundRobinBatch(10)",
- "ParquetExec: limit=None, partitions=[x]",
+ "ParquetExec: limit=None, partitions=[x], projection=[c1]",
];
assert_optimized!(expected, plan);
@@ -398,7 +398,7 @@ mod tests {
"FilterExec: c1@0",
// nothing sorts the data, so the local limit doesn't require sorted data either
"RepartitionExec: partitioning=RoundRobinBatch(10)",
- "ParquetExec: limit=None, partitions=[x]",
+ "ParquetExec: limit=None, partitions=[x], projection=[c1]",
];
assert_optimized!(expected, plan);
@@ -414,7 +414,7 @@ mod tests {
"LocalLimitExec: limit=100",
// data is sorted so can't repartition here
"SortExec: [c1@0 ASC]",
- "ParquetExec: limit=None, partitions=[x]",
+ "ParquetExec: limit=None, partitions=[x], projection=[c1]",
];
assert_optimized!(expected, plan);
@@ -432,7 +432,7 @@ mod tests {
// data is sorted so can't repartition here even though
// filter would benefit from parallelism, the answers might be wrong
"SortExec: [c1@0 ASC]",
- "ParquetExec: limit=None, partitions=[x]",
+ "ParquetExec: limit=None, partitions=[x], projection=[c1]",
];
assert_optimized!(expected, plan);
@@ -455,7 +455,7 @@ mod tests {
"GlobalLimitExec: limit=100",
"LocalLimitExec: limit=100",
// Expect no repartition to happen for local limit
- "ParquetExec: limit=None, partitions=[x]",
+ "ParquetExec: limit=None, partitions=[x], projection=[c1]",
];
assert_optimized!(expected, plan);
@@ -471,11 +471,11 @@ mod tests {
let expected = &[
"UnionExec",
// Expect no repartition of ParquetExec
- "ParquetExec: limit=None, partitions=[x]",
- "ParquetExec: limit=None, partitions=[x]",
- "ParquetExec: limit=None, partitions=[x]",
- "ParquetExec: limit=None, partitions=[x]",
- "ParquetExec: limit=None, partitions=[x]",
+ "ParquetExec: limit=None, partitions=[x], projection=[c1]",
+ "ParquetExec: limit=None, partitions=[x], projection=[c1]",
+ "ParquetExec: limit=None, partitions=[x], projection=[c1]",
+ "ParquetExec: limit=None, partitions=[x], projection=[c1]",
+ "ParquetExec: limit=None, partitions=[x], projection=[c1]",
];
assert_optimized!(expected, plan);
@@ -489,7 +489,7 @@ mod tests {
let expected = &[
"SortPreservingMergeExec: [c1@0 ASC]",
// Expect no repartition of SortPreservingMergeExec
- "ParquetExec: limit=None, partitions=[x]",
+ "ParquetExec: limit=None, partitions=[x], projection=[c1]",
];
assert_optimized!(expected, plan);
@@ -505,7 +505,7 @@ mod tests {
// Expect no repartition of SortPreservingMergeExec
// even though there is a projection exec between it
"ProjectionExec: expr=[c1@0 as c1]",
- "ParquetExec: limit=None, partitions=[x]",
+ "ParquetExec: limit=None, partitions=[x], projection=[c1]",
];
assert_optimized!(expected, plan);
@@ -522,7 +522,7 @@ mod tests {
"SortExec: [c1@0 ASC]",
"ProjectionExec: expr=[c1@0 as c1]",
"RepartitionExec: partitioning=RoundRobinBatch(10)",
- "ParquetExec: limit=None, partitions=[x]",
+ "ParquetExec: limit=None, partitions=[x], projection=[c1]",
];
assert_optimized!(expected, plan);
@@ -539,7 +539,7 @@ mod tests {
"SortExec: [c1@0 ASC]",
"FilterExec: c1@0",
"RepartitionExec: partitioning=RoundRobinBatch(10)",
- "ParquetExec: limit=None, partitions=[x]",
+ "ParquetExec: limit=None, partitions=[x], projection=[c1]",
];
assert_optimized!(expected, plan);
@@ -560,7 +560,7 @@ mod tests {
"FilterExec: c1@0",
// repartition is lowest down
"RepartitionExec: partitioning=RoundRobinBatch(10)",
- "ParquetExec: limit=None, partitions=[x]",
+ "ParquetExec: limit=None, partitions=[x], projection=[c1]",
];
assert_optimized!(expected, plan);
diff --git a/datafusion/src/physical_plan/file_format/csv.rs b/datafusion/src/physical_plan/file_format/csv.rs
index 10872a7d15..f124e70eb8 100644
--- a/datafusion/src/physical_plan/file_format/csv.rs
+++ b/datafusion/src/physical_plan/file_format/csv.rs
@@ -165,10 +165,11 @@ impl ExecutionPlan for CsvExec {
DisplayFormatType::Default => {
write!(
f,
- "CsvExec: files={}, has_header={}, limit={:?}",
+ "CsvExec: files={}, has_header={}, limit={:?}, projection={}",
super::FileGroupsDisplay(&self.base_config.file_groups),
self.has_header,
self.base_config.limit,
+ super::ProjectSchemaDisplay(&self.projected_schema),
)
}
}
diff --git a/datafusion/src/physical_plan/file_format/mod.rs b/datafusion/src/physical_plan/file_format/mod.rs
index 176f94a451..8e150e8e23 100644
--- a/datafusion/src/physical_plan/file_format/mod.rs
+++ b/datafusion/src/physical_plan/file_format/mod.rs
@@ -171,6 +171,22 @@ impl<'a> Display for FileGroupsDisplay<'a> {
}
}
+/// A wrapper to customize partitioned file display
+#[derive(Debug)]
+struct ProjectSchemaDisplay<'a>(&'a SchemaRef);
+
+impl<'a> Display for ProjectSchemaDisplay<'a> {
+ fn fmt(&self, f: &mut Formatter) -> FmtResult {
+ let parts: Vec<_> = self
+ .0
+ .fields()
+ .iter()
+ .map(|x| x.name().to_owned())
+ .collect::<Vec<String>>();
+ write!(f, "[{}]", parts.join(", "))
+ }
+}
+
/// A utility which can adapt file-level record batches to a table schema which may have a schema
/// obtained from merging multiple file-level schemas.
///
diff --git a/datafusion/src/physical_plan/file_format/parquet.rs b/datafusion/src/physical_plan/file_format/parquet.rs
index 42766993e6..cda6727125 100644
--- a/datafusion/src/physical_plan/file_format/parquet.rs
+++ b/datafusion/src/physical_plan/file_format/parquet.rs
@@ -274,12 +274,24 @@ impl ExecutionPlan for ParquetExec {
) -> std::fmt::Result {
match t {
DisplayFormatType::Default => {
- write!(
- f,
- "ParquetExec: limit={:?}, partitions={}",
- self.base_config.limit,
- super::FileGroupsDisplay(&self.base_config.file_groups)
- )
+ if let Some(pre) = &self.pruning_predicate {
+ write!(
+ f,
+ "ParquetExec: limit={:?}, partitions={}, predicate={}, projection={}",
+ self.base_config.limit,
+ super::FileGroupsDisplay(&self.base_config.file_groups),
+ pre.predicate_expr(),
+ super::ProjectSchemaDisplay(&self.projected_schema),
+ )
+ } else {
+ write!(
+ f,
+ "ParquetExec: limit={:?}, partitions={}, projection={}",
+ self.base_config.limit,
+ super::FileGroupsDisplay(&self.base_config.file_groups),
+ super::ProjectSchemaDisplay(&self.projected_schema),
+ )
+ }
}
}
}
diff --git a/datafusion/src/physical_plan/mod.rs b/datafusion/src/physical_plan/mod.rs
index 5e86db02b0..f14ba3fdcf 100644
--- a/datafusion/src/physical_plan/mod.rs
+++ b/datafusion/src/physical_plan/mod.rs
@@ -291,7 +291,7 @@ pub trait ExecutionPlan: Debug + Send + Sync {
/// \n CoalesceBatchesExec: target_batch_size=4096\
/// \n FilterExec: a@0 < 5\
/// \n RepartitionExec: partitioning=RoundRobinBatch(3)\
-/// \n CsvExec: files=[tests/example.csv], has_header=true, limit=None",
+/// \n CsvExec: files=[tests/example.csv], has_header=true, limit=None, projection=[a]",
/// plan_string.trim());
/// }
/// ```
diff --git a/datafusion/tests/sql/explain_analyze.rs b/datafusion/tests/sql/explain_analyze.rs
index cf984d5018..6ed8507ec8 100644
--- a/datafusion/tests/sql/explain_analyze.rs
+++ b/datafusion/tests/sql/explain_analyze.rs
@@ -658,7 +658,7 @@ async fn test_physical_plan_display_indent() {
" CoalesceBatchesExec: target_batch_size=4096",
" FilterExec: c12@1 < CAST(10 AS Float64)",
" RepartitionExec: partitioning=RoundRobinBatch(3)",
- " CsvExec: files=[ARROW_TEST_DATA/csv/aggregate_test_100.csv], has_header=true, limit=None",
+ " CsvExec: files=[ARROW_TEST_DATA/csv/aggregate_test_100.csv], has_header=true, limit=None, projection=[c1, c12]",
];
let data_path = datafusion::test_util::arrow_test_data();
@@ -703,13 +703,13 @@ async fn test_physical_plan_display_indent_multi_children() {
" ProjectionExec: expr=[c1@0 as c1]",
" ProjectionExec: expr=[c1@0 as c1]",
" RepartitionExec: partitioning=RoundRobinBatch(3)",
- " CsvExec: files=[ARROW_TEST_DATA/csv/aggregate_test_100.csv], has_header=true, limit=None",
+ " CsvExec: files=[ARROW_TEST_DATA/csv/aggregate_test_100.csv], has_header=true, limit=None, projection=[c1]",
" CoalesceBatchesExec: target_batch_size=4096",
" RepartitionExec: partitioning=Hash([Column { name: \"c2\", index: 0 }], 3)",
" ProjectionExec: expr=[c2@0 as c2]",
" ProjectionExec: expr=[c1@0 as c2]",
" RepartitionExec: partitioning=RoundRobinBatch(3)",
- " CsvExec: files=[ARROW_TEST_DATA/csv/aggregate_test_100.csv], has_header=true, limit=None",
+ " CsvExec: files=[ARROW_TEST_DATA/csv/aggregate_test_100.csv], has_header=true, limit=None, projection=[c1]",
];
let data_path = datafusion::test_util::arrow_test_data();
@@ -751,7 +751,7 @@ async fn csv_explain() {
\n CoalesceBatchesExec: target_batch_size=4096\
\n FilterExec: CAST(c2@1 AS Int64) > 10\
\n RepartitionExec: partitioning=RoundRobinBatch(NUM_CORES)\
- \n CsvExec: files=[ARROW_TEST_DATA/csv/aggregate_test_100.csv], has_header=true, limit=None\
+ \n CsvExec: files=[ARROW_TEST_DATA/csv/aggregate_test_100.csv], has_header=true, limit=None, projection=[c1, c2]\
\n"
]];
assert_eq!(expected, actual);
diff --git a/datafusion/tests/user_defined_plan.rs b/datafusion/tests/user_defined_plan.rs
index 30b4229259..3b41a28e07 100644
--- a/datafusion/tests/user_defined_plan.rs
+++ b/datafusion/tests/user_defined_plan.rs
@@ -215,7 +215,7 @@ async fn topk_query() -> Result<()> {
async fn topk_plan() -> Result<()> {
let mut ctx = setup_table(make_topk_context()).await?;
- let expected = vec![
+ let mut expected = vec![
"| logical_plan after topk | TopK: k=3 |",
"| | Projection: #sales.customer_id, #sales.revenue |",
"| | TableScan: sales projection=Some([0, 1]) |",
@@ -225,7 +225,9 @@ async fn topk_plan() -> Result<()> {
let actual_output = exec_sql(&mut ctx, &explain_query).await?;
// normalize newlines (output on windows uses \r\n)
- let actual_output = actual_output.replace("\r\n", "\n");
+ let mut actual_output = actual_output.replace("\r\n", "\n");
+ actual_output.retain(|x| !x.is_ascii_whitespace());
+ expected.retain(|x| !x.is_ascii_whitespace());
assert!(
actual_output.contains(&expected),