Skip to content

Commit

Permalink
feat(query): pretty format for explain
Browse files Browse the repository at this point in the history
Signed-off-by: Enwei Jiao <enwei.jiao@zilliz.com>
  • Loading branch information
jiaoew1991 committed Jul 12, 2022
1 parent d338929 commit f8d22ad
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion query/src/sql/planner/format/display_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Plan {
match self {
Plan::Query {
s_expr, metadata, ..
} => s_expr.to_format_tree(metadata).format_indent(),
} => s_expr.to_format_tree(metadata).format_pretty(),
Plan::Explain { kind, plan } => {
let result = plan.format_indent()?;
Ok(format!("{:?}:\n{}", kind, result))
Expand Down
21 changes: 21 additions & 0 deletions query/src/sql/planner/format/indent_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,25 @@ where T: Display
}
Ok(())
}

/// format TreeNode in a pretty way
pub fn format_pretty(&self) -> Result<String> {
let mut buf = String::new();
self.format_pretty_impl(0, false, &mut buf)?;
Ok(buf)
}

fn format_pretty_impl(&self, indent: usize, is_last: bool, f: &mut String) -> Result<()> {
if indent == 0 {
writeln!(f, "{}", &self.payload).unwrap()
} else if is_last {
writeln!(f, "{}└── {}", "│ ".repeat(indent - 1), &self.payload).unwrap()
} else {
writeln!(f, "{}├── {}", "│ ".repeat(indent - 1), &self.payload).unwrap()
}
for (i, child) in self.children.iter().enumerate() {
child.format_pretty_impl(indent + 1, i == self.children.len() - 1, f)?;
}
Ok(())
}
}
7 changes: 7 additions & 0 deletions query/tests/it/sql/planner/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,11 @@ fn test_format() {
Scan: catalog.database.table
"#;
assert_eq!(result.as_str(), expect);
let pretty_result = tree.format_pretty().unwrap();
let pretty_expect = r#"HashJoin: INNER, build keys: [plus(col1, 123)], probe keys: [col2], join filters: []
├── Filter: [true]
│ └── Scan: catalog.database.table
└── Scan: catalog.database.table
"#;
assert_eq!(pretty_result.as_str(), pretty_expect);
}

0 comments on commit f8d22ad

Please sign in to comment.