Skip to content

Apache DataFusion SQL Query Engine

Change the DataFusion explain plans to make it clearer in the predicate/filter (#2063)

Yang Jiang257d03093912
diff --git a/datafusion/src/logical_plan/plan.rs b/datafusion/src/logical_plan/plan.rs
index 5fc91b6e1a..3acb69ecc1 100644
--- a/datafusion/src/logical_plan/plan.rs
+++ b/datafusion/src/logical_plan/plan.rs
@@ -20,6 +20,7 @@
use super::display::{GraphvizVisitor, IndentVisitor};
use super::expr::{Column, Expr};
use super::extension::UserDefinedLogicalNode;
+use crate::datasource::datasource::TableProviderFilterPushDown;
use crate::datasource::TableProvider;
use crate::error::DataFusionError;
use crate::logical_plan::dfschema::DFSchemaRef;
@@ -310,6 +311,7 @@ pub struct Join {
/// If null_equals_null is true, null == null else null != null
pub null_equals_null: bool,
}
+
/// A LogicalPlan represents the different types of relational
/// operators (such as Projection, Filter, etc) and can be created by
/// the SQL query planner and the DataFrame API.
@@ -900,6 +902,7 @@ impl LogicalPlan {
}
LogicalPlan::TableScan(TableScan {
+ ref source,
ref table_name,
ref projection,
ref filters,
@@ -913,7 +916,39 @@ impl LogicalPlan {
)?;
if !filters.is_empty() {
- write!(f, ", filters={:?}", filters)?;
+ let mut full_filter = vec![];
+ let mut partial_filter = vec![];
+ let mut unsupported_filters = vec![];
+
+ filters.iter().for_each(|x| {
+ if let Ok(t) = source.supports_filter_pushdown(x) {
+ match t {
+ TableProviderFilterPushDown::Exact => {
+ full_filter.push(x)
+ }
+ TableProviderFilterPushDown::Inexact => {
+ partial_filter.push(x)
+ }
+ TableProviderFilterPushDown::Unsupported => {
+ unsupported_filters.push(x)
+ }
+ }
+ }
+ });
+
+ if !full_filter.is_empty() {
+ write!(f, ", full_filters={:?}", full_filter)?;
+ };
+ if !partial_filter.is_empty() {
+ write!(f, ", partial_filters={:?}", partial_filter)?;
+ }
+ if !unsupported_filters.is_empty() {
+ write!(
+ f,
+ ", unsupported_filters={:?}",
+ unsupported_filters
+ )?;
+ }
}
if let Some(n) = limit {
@@ -1217,11 +1252,11 @@ mod tests {
}
/// Tests for the Visitor trait and walking logical plan nodes
-
#[derive(Debug, Default)]
struct OkVisitor {
strings: Vec<String>,
}
+
impl PlanVisitor for OkVisitor {
type Error = String;
@@ -1271,7 +1306,7 @@ mod tests {
"pre_visit TableScan",
"post_visit TableScan",
"post_visit Filter",
- "post_visit Projection"
+ "post_visit Projection",
]
);
}
@@ -1281,6 +1316,7 @@ mod tests {
struct OptionalCounter {
val: Option<usize>,
}
+
impl OptionalCounter {
fn new(val: usize) -> Self {
Self { val: Some(val) }
@@ -1344,7 +1380,7 @@ mod tests {
assert_eq!(
visitor.inner.strings,
- vec!["pre_visit Projection", "pre_visit Filter",]
+ vec!["pre_visit Projection", "pre_visit Filter"]
);
}
@@ -1422,7 +1458,7 @@ mod tests {
assert_eq!(
visitor.inner.strings,
- vec!["pre_visit Projection", "pre_visit Filter",]
+ vec!["pre_visit Projection", "pre_visit Filter"]
);
}
diff --git a/datafusion/src/optimizer/filter_push_down.rs b/datafusion/src/optimizer/filter_push_down.rs
index 795e7a2938..30a7ee9732 100644
--- a/datafusion/src/optimizer/filter_push_down.rs
+++ b/datafusion/src/optimizer/filter_push_down.rs
@@ -1431,7 +1431,7 @@ mod tests {
let plan = table_scan_with_pushdown_provider(TableProviderFilterPushDown::Exact)?;
let expected = "\
- TableScan: test projection=None, filters=[#a = Int64(1)]";
+ TableScan: test projection=None, full_filters=[#a = Int64(1)]";
assert_optimized_plan_eq(&plan, expected);
Ok(())
}
@@ -1443,7 +1443,7 @@ mod tests {
let expected = "\
Filter: #a = Int64(1)\
- \n TableScan: test projection=None, filters=[#a = Int64(1)]";
+ \n TableScan: test projection=None, partial_filters=[#a = Int64(1)]";
assert_optimized_plan_eq(&plan, expected);
Ok(())
}
@@ -1457,7 +1457,7 @@ mod tests {
let expected = "\
Filter: #a = Int64(1)\
- \n TableScan: test projection=None, filters=[#a = Int64(1)]";
+ \n TableScan: test projection=None, partial_filters=[#a = Int64(1)]";
// Optimizing the same plan multiple times should produce the same plan
// each time.
@@ -1501,7 +1501,7 @@ mod tests {
let expected ="Projection: #a, #b\
\n Filter: #a = Int64(10) AND #b > Int64(11)\
- \n TableScan: test projection=Some([0]), filters=[#a = Int64(10), #b > Int64(11)]";
+ \n TableScan: test projection=Some([0]), partial_filters=[#a = Int64(10), #b > Int64(11)]";
assert_optimized_plan_eq(&plan, expected);
diff --git a/datafusion/tests/sql/explain_analyze.rs b/datafusion/tests/sql/explain_analyze.rs
index 84946261ad..cf984d5018 100644
--- a/datafusion/tests/sql/explain_analyze.rs
+++ b/datafusion/tests/sql/explain_analyze.rs
@@ -260,7 +260,7 @@ async fn csv_explain_plans() {
"Explain [plan_type:Utf8, plan:Utf8]",
" Projection: #aggregate_test_100.c1 [c1:Utf8]",
" Filter: #aggregate_test_100.c2 > Int64(10) [c1:Utf8, c2:Int32]",
- " TableScan: aggregate_test_100 projection=Some([0, 1]), filters=[#aggregate_test_100.c2 > Int64(10)] [c1:Utf8, c2:Int32]",
+ " TableScan: aggregate_test_100 projection=Some([0, 1]), partial_filters=[#aggregate_test_100.c2 > Int64(10)] [c1:Utf8, c2:Int32]",
];
let formatted = plan.display_indent_schema().to_string();
let actual: Vec<&str> = formatted.trim().lines().collect();
@@ -275,7 +275,7 @@ async fn csv_explain_plans() {
"Explain",
" Projection: #aggregate_test_100.c1",
" Filter: #aggregate_test_100.c2 > Int64(10)",
- " TableScan: aggregate_test_100 projection=Some([0, 1]), filters=[#aggregate_test_100.c2 > Int64(10)]",
+ " TableScan: aggregate_test_100 projection=Some([0, 1]), partial_filters=[#aggregate_test_100.c2 > Int64(10)]",
];
let formatted = plan.display_indent().to_string();
let actual: Vec<&str> = formatted.trim().lines().collect();
@@ -297,7 +297,7 @@ async fn csv_explain_plans() {
" 2 -> 3 [arrowhead=none, arrowtail=normal, dir=back]",
" 4[shape=box label=\"Filter: #aggregate_test_100.c2 > Int64(10)\"]",
" 3 -> 4 [arrowhead=none, arrowtail=normal, dir=back]",
- " 5[shape=box label=\"TableScan: aggregate_test_100 projection=Some([0, 1]), filters=[#aggregate_test_100.c2 > Int64(10)]\"]",
+ " 5[shape=box label=\"TableScan: aggregate_test_100 projection=Some([0, 1]), partial_filters=[#aggregate_test_100.c2 > Int64(10)]\"]",
" 4 -> 5 [arrowhead=none, arrowtail=normal, dir=back]",
" }",
" subgraph cluster_6",
@@ -308,7 +308,7 @@ async fn csv_explain_plans() {
" 7 -> 8 [arrowhead=none, arrowtail=normal, dir=back]",
" 9[shape=box label=\"Filter: #aggregate_test_100.c2 > Int64(10)\\nSchema: [c1:Utf8, c2:Int32]\"]",
" 8 -> 9 [arrowhead=none, arrowtail=normal, dir=back]",
- " 10[shape=box label=\"TableScan: aggregate_test_100 projection=Some([0, 1]), filters=[#aggregate_test_100.c2 > Int64(10)]\\nSchema: [c1:Utf8, c2:Int32]\"]",
+ " 10[shape=box label=\"TableScan: aggregate_test_100 projection=Some([0, 1]), partial_filters=[#aggregate_test_100.c2 > Int64(10)]\\nSchema: [c1:Utf8, c2:Int32]\"]",
" 9 -> 10 [arrowhead=none, arrowtail=normal, dir=back]",
" }",
"}",
@@ -458,7 +458,7 @@ async fn csv_explain_verbose_plans() {
"Explain [plan_type:Utf8, plan:Utf8]",
" Projection: #aggregate_test_100.c1 [c1:Utf8]",
" Filter: #aggregate_test_100.c2 > Int64(10) [c1:Utf8, c2:Int32]",
- " TableScan: aggregate_test_100 projection=Some([0, 1]), filters=[#aggregate_test_100.c2 > Int64(10)] [c1:Utf8, c2:Int32]",
+ " TableScan: aggregate_test_100 projection=Some([0, 1]), partial_filters=[#aggregate_test_100.c2 > Int64(10)] [c1:Utf8, c2:Int32]",
];
let formatted = plan.display_indent_schema().to_string();
let actual: Vec<&str> = formatted.trim().lines().collect();
@@ -473,7 +473,7 @@ async fn csv_explain_verbose_plans() {
"Explain",
" Projection: #aggregate_test_100.c1",
" Filter: #aggregate_test_100.c2 > Int64(10)",
- " TableScan: aggregate_test_100 projection=Some([0, 1]), filters=[#aggregate_test_100.c2 > Int64(10)]",
+ " TableScan: aggregate_test_100 projection=Some([0, 1]), partial_filters=[#aggregate_test_100.c2 > Int64(10)]",
];
let formatted = plan.display_indent().to_string();
let actual: Vec<&str> = formatted.trim().lines().collect();
@@ -495,7 +495,7 @@ async fn csv_explain_verbose_plans() {
" 2 -> 3 [arrowhead=none, arrowtail=normal, dir=back]",
" 4[shape=box label=\"Filter: #aggregate_test_100.c2 > Int64(10)\"]",
" 3 -> 4 [arrowhead=none, arrowtail=normal, dir=back]",
- " 5[shape=box label=\"TableScan: aggregate_test_100 projection=Some([0, 1]), filters=[#aggregate_test_100.c2 > Int64(10)]\"]",
+ " 5[shape=box label=\"TableScan: aggregate_test_100 projection=Some([0, 1]), partial_filters=[#aggregate_test_100.c2 > Int64(10)]\"]",
" 4 -> 5 [arrowhead=none, arrowtail=normal, dir=back]",
" }",
" subgraph cluster_6",
@@ -506,7 +506,7 @@ async fn csv_explain_verbose_plans() {
" 7 -> 8 [arrowhead=none, arrowtail=normal, dir=back]",
" 9[shape=box label=\"Filter: #aggregate_test_100.c2 > Int64(10)\\nSchema: [c1:Utf8, c2:Int32]\"]",
" 8 -> 9 [arrowhead=none, arrowtail=normal, dir=back]",
- " 10[shape=box label=\"TableScan: aggregate_test_100 projection=Some([0, 1]), filters=[#aggregate_test_100.c2 > Int64(10)]\\nSchema: [c1:Utf8, c2:Int32]\"]",
+ " 10[shape=box label=\"TableScan: aggregate_test_100 projection=Some([0, 1]), partial_filters=[#aggregate_test_100.c2 > Int64(10)]\\nSchema: [c1:Utf8, c2:Int32]\"]",
" 9 -> 10 [arrowhead=none, arrowtail=normal, dir=back]",
" }",
"}",
@@ -621,9 +621,9 @@ order by
\n Inner Join: #customer.c_custkey = #orders.o_custkey\
\n TableScan: customer projection=Some([0, 1, 2, 3, 4, 5, 7])\
\n Filter: #orders.o_orderdate >= Date32(\"8674\") AND #orders.o_orderdate < Date32(\"8766\")\
- \n TableScan: orders projection=Some([0, 1, 4]), filters=[#orders.o_orderdate >= Date32(\"8674\"), #orders.o_orderdate < Date32(\"8766\")]\
+ \n TableScan: orders projection=Some([0, 1, 4]), partial_filters=[#orders.o_orderdate >= Date32(\"8674\"), #orders.o_orderdate < Date32(\"8766\")]\
\n Filter: #lineitem.l_returnflag = Utf8(\"R\")\
- \n TableScan: lineitem projection=Some([0, 5, 6, 8]), filters=[#lineitem.l_returnflag = Utf8(\"R\")]\
+ \n TableScan: lineitem projection=Some([0, 5, 6, 8]), partial_filters=[#lineitem.l_returnflag = Utf8(\"R\")]\
\n TableScan: nation projection=Some([0, 1])";
assert_eq!(format!("{:?}", plan.unwrap()), expected);
@@ -744,7 +744,7 @@ async fn csv_explain() {
"logical_plan",
"Projection: #aggregate_test_100.c1\
\n Filter: #aggregate_test_100.c2 > Int64(10)\
- \n TableScan: aggregate_test_100 projection=Some([0, 1]), filters=[#aggregate_test_100.c2 > Int64(10)]"
+ \n TableScan: aggregate_test_100 projection=Some([0, 1]), partial_filters=[#aggregate_test_100.c2 > Int64(10)]"
],
vec!["physical_plan",
"ProjectionExec: expr=[c1@0 as c1]\