From 17421b63173cefb9cdc2b69514fa638aa20639d0 Mon Sep 17 00:00:00 2001 From: "xudong.w" Date: Fri, 12 Jan 2024 15:25:19 +0800 Subject: [PATCH] refactor: add cardinality to CteScan explain (#14310) chore: add cardinality to CteScan explain --- src/query/sql/src/executor/format.rs | 22 +++++++++---------- .../physical_plans/physical_cte_scan.rs | 7 ++++++ .../standalone/explain/materialized_cte.test | 15 ++++++++----- tests/sqllogictests/suites/tpch/queries.test | 8 +++++-- 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/query/sql/src/executor/format.rs b/src/query/sql/src/executor/format.rs index 1fe3d1869b55..56bb49360ba9 100644 --- a/src/query/sql/src/executor/format.rs +++ b/src/query/sql/src/executor/format.rs @@ -127,13 +127,7 @@ impl PhysicalPlan { children, )) } - PhysicalPlan::CteScan(cte_scan) => Ok(FormatTreeNode::with_children( - format!( - "CteScan: {}, sub index: {}", - cte_scan.cte_idx.0, cte_scan.cte_idx.1 - ), - vec![], - )), + PhysicalPlan::CteScan(cte_scan) => cte_scan_to_format_tree(cte_scan), PhysicalPlan::MaterializedCte(materialized_cte) => { let left_child = materialized_cte.left.format_join(metadata)?; let right_child = materialized_cte.right.format_join(metadata)?; @@ -386,13 +380,17 @@ fn table_scan_to_format_tree( } fn cte_scan_to_format_tree(plan: &CteScan) -> Result> { - let cte_idx = FormatTreeNode::new(format!( + let mut children = vec![FormatTreeNode::new(format!( "CTE index: {}, sub index: {}", plan.cte_idx.0, plan.cte_idx.1 - )); - Ok(FormatTreeNode::with_children("CTEScan".to_string(), vec![ - cte_idx, - ])) + ))]; + let items = plan_stats_info_to_format_tree(&plan.stat); + children.extend(items); + + Ok(FormatTreeNode::with_children( + "CTEScan".to_string(), + children, + )) } fn constant_table_scan_to_format_tree( diff --git a/src/query/sql/src/executor/physical_plans/physical_cte_scan.rs b/src/query/sql/src/executor/physical_plans/physical_cte_scan.rs index 23c02128ea7c..62c5305f9b74 100644 --- a/src/query/sql/src/executor/physical_plans/physical_cte_scan.rs +++ b/src/query/sql/src/executor/physical_plans/physical_cte_scan.rs @@ -16,6 +16,7 @@ use databend_common_exception::Result; use databend_common_expression::DataSchemaRef; use databend_common_expression::DataSchemaRefExt; +use crate::executor::explain::PlanStatsInfo; use crate::executor::PhysicalPlan; use crate::executor::PhysicalPlanBuilder; use crate::ColumnSet; @@ -28,6 +29,7 @@ pub struct CteScan { pub cte_idx: (IndexType, IndexType), pub output_schema: DataSchemaRef, pub offsets: Vec, + pub stat: PlanStatsInfo, } impl CteScan { @@ -62,12 +64,17 @@ impl PhysicalPlanBuilder { } } + let plan_stat = PlanStatsInfo { + estimated_rows: cte_scan.stat.cardinality, + }; + // 2. Build physical plan. Ok(PhysicalPlan::CteScan(CteScan { plan_id: self.next_plan_id(), cte_idx: cte_scan.cte_idx, output_schema: DataSchemaRefExt::create(pruned_fields), offsets: pruned_offsets, + stat: plan_stat, })) } } diff --git a/tests/sqllogictests/suites/mode/standalone/explain/materialized_cte.test b/tests/sqllogictests/suites/mode/standalone/explain/materialized_cte.test index b96f4c87f8ac..9405b55fd14b 100644 --- a/tests/sqllogictests/suites/mode/standalone/explain/materialized_cte.test +++ b/tests/sqllogictests/suites/mode/standalone/explain/materialized_cte.test @@ -21,9 +21,11 @@ MaterializedCTE ├── filters: [] ├── estimated rows: 100.00 ├── CTEScan(Build) - │ └── CTE index: 0, sub index: 2 + │ ├── CTE index: 0, sub index: 2 + │ └── estimated rows: 10.00 └── CTEScan(Probe) - └── CTE index: 0, sub index: 1 + ├── CTE index: 0, sub index: 1 + └── estimated rows: 10.00 query T explain with t1 as materialized (select number as a from numbers(10)), t2 as materialized (select a as b from t1) select t1.a from t1 join t2 on t1.a = t2.b; @@ -42,7 +44,8 @@ MaterializedCTE └── MaterializedCTE ├── output columns: [numbers.number (#0)] ├── CTEScan - │ └── CTE index: 0, sub index: 2 + │ ├── CTE index: 0, sub index: 2 + │ └── estimated rows: 10.00 └── HashJoin ├── output columns: [numbers.number (#0)] ├── join type: INNER @@ -51,6 +54,8 @@ MaterializedCTE ├── filters: [] ├── estimated rows: 100.00 ├── CTEScan(Build) - │ └── CTE index: 1, sub index: 1 + │ ├── CTE index: 1, sub index: 1 + │ └── estimated rows: 10.00 └── CTEScan(Probe) - └── CTE index: 0, sub index: 1 + ├── CTE index: 0, sub index: 1 + └── estimated rows: 10.00 diff --git a/tests/sqllogictests/suites/tpch/queries.test b/tests/sqllogictests/suites/tpch/queries.test index dc3547d0a091..2a024721a891 100644 --- a/tests/sqllogictests/suites/tpch/queries.test +++ b/tests/sqllogictests/suites/tpch/queries.test @@ -1850,11 +1850,15 @@ MaterializedCte: 0 └── Right └── HashJoin: INNER ├── Build - │ └── CteScan: 0, sub index: 2 + │ └── CTEScan + │ ├── CTE index: 0, sub index: 2 + │ └── estimated rows: 81.00 └── Probe └── HashJoin: INNER ├── Build - │ └── CteScan: 0, sub index: 1 + │ └── CTEScan + │ ├── CTE index: 0, sub index: 1 + │ └── estimated rows: 81.00 └── Probe └── Scan: default.tpch_test.supplier (read rows: 1000)