Skip to content
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

[EPIC] Complete SQL EXPLAIN Tree Rendering #14914

Open
1 of 36 tasks
irenjj opened this issue Feb 27, 2025 · 9 comments
Open
1 of 36 tasks

[EPIC] Complete SQL EXPLAIN Tree Rendering #14914

irenjj opened this issue Feb 27, 2025 · 9 comments
Labels
enhancement New feature or request

Comments

@irenjj
Copy link
Contributor

irenjj commented Feb 27, 2025

Is your feature request related to a problem or challenge?

#14677 introduces basic tree rendering for SQL EXPLAIN output but requires follow-up work to improve completeness, test coverage, and code quality. Below are the key tasks and enhancements needed:

Here is an example of running the new explains:

set datafusion.explain.format = 'tree';
create table foo(x int, y int) as values (1,2), (3,4);
explain select * from foo where x = 4;

The output looks like

+---------------+------------------------------------+
| plan_type     | plan                               |
+---------------+------------------------------------+
| logical_plan  | Filter: foo.x = Int32(4)           |
|               |   TableScan: foo projection=[x, y] |
| physical_plan | ┌───────────────────────────┐      |
|               | │    CoalesceBatchesExec    │      |
|               | └─────────────┬─────────────┘      |
|               | ┌─────────────┴─────────────┐      |
|               | │         FilterExec        │      |
|               | └─────────────┬─────────────┘      |
|               | ┌─────────────┴─────────────┐      |
|               | │       DataSourceExec      │      |
|               | │    --------------------   │      |
|               | │    partition_sizes: [1]   │      |
|               | │       partitions: 1       │      |
|               | └───────────────────────────┘      |
|               |                                    |
+---------------+------------------------------------+

Items to complete

Information Completion for all ExecutionPlans

The current implementation lacks detailed information for many physical plans.

Add rendering logic for missing operators (In every physical plan's fmt_as function).

New Features:

Process / Documentation

  • Document the tree explain in the user documentation
  • Add upgrade guide in 47 for TreeRender in DisplayFormatType`
  • Use tree explain by default

Test Coverage Expansion

  • Add tests for plans with long metrics (e.g., partition_sizes: [20, 20, 20, 30, ...]) to validate line-wrapping and truncation logic.
  • Tests for TPCH queries (e.g., Q5, Q9) to test rendering of deeply nested plans.
  • Validate alignment and indentation for multi-level operators.

Code Cleanup & Improvements

TODOs

Make variables (e.g., indentation size, truncation thresholds) configurable:

// TODO: Make these variables configurable.  
Dead Code Removal

Remove or utilize the unused Coordinate struct:

#[allow(dead_code)]  
pub struct Coordinate { ... }  
// TODO: It's never used.  
@irenjj irenjj added the enhancement New feature or request label Feb 27, 2025
@alamb alamb changed the title Tracking Issue: Enhance SQL EXPLAIN Tree Rendering [EPIC] Complete SQL EXPLAIN Tree Rendering Feb 28, 2025
@alamb
Copy link
Contributor

alamb commented Mar 4, 2025

I made a first PR to show how to add tree explain

Once we get that one in I plan to file a bunch of other tickets for the remaining ExplainPlan nodes

@milenkovicm
Copy link
Contributor

This looks great! Would it be possible to extend this with svg output? It would be great for ballista ui

@alamb
Copy link
Contributor

alamb commented Mar 4, 2025

This looks great! Would it be possible to extend this with svg output? It would be great for ballista ui

There is already a graphviz version (that kind of looks crappy).

Since the output is text, perhaps there is some way to render text as a svg 🤔

@waynexia
Copy link
Member

waynexia commented Mar 4, 2025

For the setting side, how about using the existing EXPLAIN [ANALYZE] FORMAT <format> grammar? sqlparser can parse and generate a AnalyzeFormat, which the planner can use. But our current implementation just drops this format field:

Statement::Explain {
verbose,
statement,
analyze,
format: _,
describe_alias: _,
..
} => {
self.explain_to_plan(verbose, analyze, DFStatement::Statement(statement))
}

We can also try to support GRAPHVIZ format in this way, it should be easier to be rendered to svg.

@alamb
Copy link
Contributor

alamb commented Mar 5, 2025

For the setting side, how about using the existing EXPLAIN [ANALYZE] FORMAT <format> grammar? sqlparser can parse and generate a AnalyzeFormat, which the planner can use. But our current implementation just drops this format field:

datafusion/datafusion/sql/src/statement.rs

Lines 213 to 222 in ed517ef

Statement::Explain {
verbose,
statement,
analyze,
format: _,
describe_alias: _,
..
} => {
self.explain_to_plan(verbose, analyze, DFStatement::Statement(statement))
}
We can also try to support GRAPHVIZ format in this way, it should be easier to be rendered to svg.

This is a great idea @waynexia

It seems like AnalyzeFormat currently only supports Text, JSON, and GRAPHVIZ 🤔

https://docs.rs/sqlparser/latest/sqlparser/ast/enum.AnalyzeFormat.html

It would be a good way to show graphviz plans

@alamb
Copy link
Contributor

alamb commented Mar 5, 2025

For the setting side, how about using the existing EXPLAIN [ANALYZE] FORMAT grammar? sqlparser can parse and generate a AnalyzeFormat, which the planner can use. But our current implementation just drops this format field:

I filed the following ticket for this

I also filed a ticket to avoid printing logical plans when in tree mode

@alamb
Copy link
Contributor

alamb commented Mar 5, 2025

Here is another PR to improve DataSource:

@milenkovicm
Copy link
Contributor

milenkovicm commented Mar 6, 2025

This looks great! Would it be possible to extend this with svg output? It would be great for ballista ui

There is already a graphviz version (that kind of looks crappy).

Since the output is text, perhaps there is some way to render text as a svg 🤔

ballista uses graphviz-rust which generates those sub-optimal images (and needs local graph viz installation). spark uses

a javascript framework which works much better for them. Maybe we should keep dot representation if we already have it

@alamb
Copy link
Contributor

alamb commented Mar 6, 2025

This looks great! Would it be possible to extend this with svg output? It would be great for ballista ui

There is already a graphviz version (that kind of looks crappy).
Since the output is text, perhaps there is some way to render text as a svg 🤔

ballista uses graphviz-rust which generates those sub-optimal images (and needs local graph viz installation). spark uses

a javascript framework which works much better for them. Maybe we should keep dot representation if we already have it

I think that if we just made it easier to use the existing formats, that would be a big win.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants