Skip to content

Implement tree explain for SortExec #15077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions datafusion/physical-plan/src/sorts/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1005,10 +1005,15 @@ impl DisplayAs for SortExec {
None => write!(f, "SortExec: expr=[{}], preserve_partitioning=[{preserve_partitioning}]", self.expr),
}
}
DisplayFormatType::TreeRender => {
// TODO: collect info
write!(f, "")
}
DisplayFormatType::TreeRender => match self.fetch {
Some(fetch) => {
writeln!(f, "limit={fetch}")?;
writeln!(f, "sort keys=[{}]", self.expr)
}
None => {
writeln!(f, "sort keys=[{}]", self.expr)
}
},
}
}
}
Expand Down Expand Up @@ -1225,13 +1230,9 @@ mod tests {
impl DisplayAs for SortedUnboundedExec {
fn fmt_as(&self, t: DisplayFormatType, f: &mut Formatter) -> fmt::Result {
match t {
DisplayFormatType::Default | DisplayFormatType::Verbose => {
write!(f, "UnboundableExec",).unwrap()
}
DisplayFormatType::TreeRender => {
// TODO: collect info
write!(f, "").unwrap()
}
DisplayFormatType::Default
| DisplayFormatType::Verbose
| DisplayFormatType::TreeRender => write!(f, "UnboundableExec",).unwrap(),
}
Ok(())
}
Expand Down
48 changes: 46 additions & 2 deletions datafusion/sqllogictest/test_files/explain_tree.slt
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,52 @@ physical_plan
17)│ format: arrow │
18)└───────────────────────────┘

# Query for sort.
query TT
explain SELECT * FROM table1 ORDER BY string_col;
----
logical_plan
01)Sort: table1.string_col ASC NULLS LAST
02)--TableScan: table1 projection=[int_col, string_col, bigint_col, date_col]
physical_plan
01)┌───────────────────────────┐
02)│ SortExec │
03)│ -------------------- │
04)│ sort keys: │
05)│ [string_col@1 ASC NULLS │
06)│ LAST] │
07)└─────────────┬─────────────┘
08)┌─────────────┴─────────────┐
09)│ DataSourceExec │
10)│ -------------------- │
11)│ files: 1 │
12)│ format: csv │
13)└───────────────────────────┘

# Query for sort with limit.
query TT
explain SELECT * FROM table1 ORDER BY string_col LIMIT 1;
----
logical_plan
01)Sort: table1.string_col ASC NULLS LAST, fetch=1
02)--TableScan: table1 projection=[int_col, string_col, bigint_col, date_col]
physical_plan
01)┌───────────────────────────┐
02)│ SortExec │
03)│ -------------------- │
04)│ limit: 1 │
05)│ │
06)│ sort keys: │
07)│ [string_col@1 ASC NULLS │
08)│ LAST] │
Comment on lines +577 to +579
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there is some way to avoid the redundancy of sort keys 🤔

For example I think this would be look better (not sure how to do it)

01)┌───────────────────────────┐
02)│          SortExec         │
03)│    --------------------   │
07)│   string_col@1 ASC NULLS  │
08)│            LAST           │
05)│                           │
04)│          limit: 1         │
09)└─────────────┬─────────────┘

Copy link
Contributor Author

@irenjj irenjj Mar 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to handle cases that are not in KV format. Other prs may have similar issues too, maybe we can handle it in the next pr, filed: #15098

09)└─────────────┬─────────────┘
10)┌─────────────┴─────────────┐
11)│ DataSourceExec │
12)│ -------------------- │
13)│ files: 1 │
14)│ format: csv │
15)└───────────────────────────┘

# Query with projection on csv
query TT
explain SELECT int_col, bigint_col, int_col+bigint_col AS sum_col FROM table1;
Expand Down Expand Up @@ -629,8 +675,6 @@ physical_plan
18)│ rows: 1 │
19)└───────────────────────────┘



# Query with projection on json
query TT
explain SELECT int_col, bigint_col, int_col+bigint_col AS sum_col FROM table4;
Expand Down