Skip to content

Commit

Permalink
refactor: add cardinality to CteScan explain (#14310)
Browse files Browse the repository at this point in the history
chore: add cardinality to CteScan explain
  • Loading branch information
xudong963 authored Jan 12, 2024
1 parent 225375d commit 17421b6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
22 changes: 10 additions & 12 deletions src/query/sql/src/executor/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down Expand Up @@ -386,13 +380,17 @@ fn table_scan_to_format_tree(
}

fn cte_scan_to_format_tree(plan: &CteScan) -> Result<FormatTreeNode<String>> {
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +29,7 @@ pub struct CteScan {
pub cte_idx: (IndexType, IndexType),
pub output_schema: DataSchemaRef,
pub offsets: Vec<IndexType>,
pub stat: PlanStatsInfo,
}

impl CteScan {
Expand Down Expand Up @@ -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,
}))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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
8 changes: 6 additions & 2 deletions tests/sqllogictests/suites/tpch/queries.test
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 17421b6

Please sign in to comment.