Skip to content

Support different EXPLAIN formats via SQL #15021

Closed

Description

@alamb

Is your feature request related to a problem or challenge?

Suggested by @waynexia #14914 (comment)

Currently, DataFusion supports EXPLAIN and EXPLAIN ANALYZE as documented here: https://datafusion.apache.org/user-guide/sql/explain.html#id1

Both those commands produce a text based explain format:

> explain select * from values (1), (2);
+---------------+---------------------------------------------------+
| plan_type     | plan                                              |
+---------------+---------------------------------------------------+
| logical_plan  | Values: (Int64(1)), (Int64(2))                    |
| physical_plan | DataSourceExec: partitions=1, partition_sizes=[1] |
|               |                                                   |
+---------------+---------------------------------------------------+
2 row(s) fetched.
Elapsed 0.031 seconds.

DataFusion has at least three other text based ways to generate explain plans:

Also, @milenkovicm suggested on #14914 (comment) that getting an SVG version would also be useful

As of now it is not possible to see those formats via SQL (you need to use the API or set a config option)

Describe the solution you'd like

I would like to be able to access the different explain formats via SQL

Describe alternatives you've considered

@waynexia suggests (and I agree) on #14914 (comment)

That we follow the EXPLAIN [ANALYZE] FORMAT <format> grammar?:

EXPLAIN [ANALYZE] FORMAT <format>

So this would look something like

-- show explain plan in default text format
EXPLAIN  SELECT * from foo
-- show explain plan in tree format
EXPLAIN FORMAT tree SELECT * from foo
-- show explain plan in json format
EXPLAIN FORMAT json SELECT * from foo
-- ...

@waynexia suggests on #14914 (comment)

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

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

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

Additional context

No response

Activity

  1. milenkovicm commented on Mar 5, 2025

    @milenkovicm
    Contributor

    getting json output may be sufficient, we could derive SVG from it if needed

  2. waynexia commented on Mar 6, 2025

    @waynexia
    Member

    I can implement the JSON part

  3. self-assigned this
    on Mar 6, 2025
  4. alamb commented on Mar 6, 2025

    @alamb
    ContributorAuthor

    I was thinking about implementing this feature

    I think it would be a relatively straightforward implementation:

    1. Extend the DataFusion explain statement to have a format field too https://docs.rs/datafusion/latest/datafusion/sql/parser/struct.ExplainStatement.html
    2. Extend DFParser to parse FORMAT ...

    Then we would just need to update the code in planning:

    DFStatement::Explain(ExplainStatement {

  5. milenkovicm commented on Mar 6, 2025

    @milenkovicm
    Contributor

    as mentioned in #14914 (comment)

    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

  6. alamb commented on Mar 11, 2025

    @alamb
    ContributorAuthor

    I so want this feature (and I would like an excuse to write some code) I am going to take a shot at doing so

  7. assigned and unassigned on Mar 11, 2025
  8. alamb commented on Mar 12, 2025

    @alamb
    ContributorAuthor
  9. waynexia commented on Mar 12, 2025

    @waynexia
    Member

    Thank you @alamb, and apologize for postponing this so long 🙈 (excuse: keeping receiving unscheduled prioritiiii things from @killme2008

  10. alamb commented on Mar 12, 2025

    @alamb
    ContributorAuthor

    Thank you @alamb, and apologize for postponing this so long 🙈 (excuse: keeping receiving unscheduled prioritiiii things from @killme2008

    LOL I totally understand. I happened to have some time and wanted some excuse to write some code :)

    (Thanks for the review!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions