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

Support EXPLAIN ... FORMAT <indent | tree | json | graphviz > ... #15166

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

alamb
Copy link
Contributor

@alamb alamb commented Mar 11, 2025

Which issue does this PR close?

Rationale for this change

I would like to be able to see more of the great explain plan code that DataFusion already has (see #15021 for details)

What changes are included in this PR?

Changes:

  • Parser support EXPLAIN VERBOSE FORMAT ...
  • Support indent mode
  • Support tree mode
  • Support pgjson mode
  • Support graphviz mode
  • Update documentation
  • Add sqllogictests

Here is an example

> explain format text select * from values(1) t(x);
Invalid or Unsupported Configuration: Invalid explain format: text
> explain format indent select * from values(1) t(x);
+---------------+-----------------------------------------------------+
| plan_type     | plan                                                |
+---------------+-----------------------------------------------------+
| logical_plan  | SubqueryAlias: t                                    |
|               |   Projection: column1 AS x                          |
|               |     Values: (Int64(1))                              |
| physical_plan | ProjectionExec: expr=[column1@0 as x]               |
|               |   DataSourceExec: partitions=1, partition_sizes=[1] |
|               |                                                     |
+---------------+-----------------------------------------------------+
2 row(s) fetched.
Elapsed 0.013 seconds.

> explain format tree select * from values(1) t(x);
+---------------+-------------------------------+
| plan_type     | plan                          |
+---------------+-------------------------------+
| physical_plan | ┌───────────────────────────┐ |
|               | │       ProjectionExec      │ |
|               | │    --------------------   │ |
|               | │        x: column1@0       │ |
|               | └─────────────┬─────────────┘ |
|               | ┌─────────────┴─────────────┐ |
|               | │       DataSourceExec      │ |
|               | │    --------------------   │ |
|               | │         bytes: 128        │ |
|               | │       format: memory      │ |
|               | │          rows: 1          │ |
|               | └───────────────────────────┘ |
|               |                               |
+---------------+-------------------------------+
1 row(s) fetched.
Elapsed 0.011 seconds.

Are these changes tested?

Are there any user-facing changes?

@github-actions github-actions bot added sql SQL Planner logical-expr Logical plan and expressions core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) labels Mar 11, 2025
@alamb alamb force-pushed the alamb/explan_format branch from d2e0d63 to aa53939 Compare March 12, 2025 15:54
@alamb alamb force-pushed the alamb/explan_format branch from aa53939 to b23e3cd Compare March 12, 2025 16:37
@github-actions github-actions bot added the documentation Improvements or additions to documentation label Mar 12, 2025
@alamb alamb changed the title WIP: Support EXPLAIN ... FORMAT <indent | tree | json | graphviz > ... Support EXPLAIN ... FORMAT <indent | tree | json | graphviz > ... Mar 12, 2025
@alamb alamb force-pushed the alamb/explan_format branch from b23e3cd to 187f38e Compare March 12, 2025 16:48
describe_alias: _,
..
} => {
self.explain_to_plan(verbose, analyze, DFStatement::Statement(statement))
let format = format.map(|format| format.to_string());
Copy link
Member

Choose a reason for hiding this comment

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

Not very related to this PR, will we change sqlparser part to use a String instead of enum like here? (I'd prefer so, as it gives us more flexibility)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that would be a fine idea to propose upstream in sqlparser

I am not aware of any plans at the moment


<pre>
EXPLAIN [ANALYZE] [VERBOSE] statement
EXPLAIN [ANALYZE] [VERBOSE] [FORMAT format] statement
Copy link
Member

Choose a reason for hiding this comment

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

Maybe also state here that using VERBOSE and FORMAT together is not supported (yet?) like ANALYZE and FORMAT below

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will do so

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to clarify in 398cf1824

@alamb alamb force-pushed the alamb/explan_format branch from 187f38e to c965e8e Compare March 12, 2025 18:57
@alamb alamb marked this pull request as ready for review March 12, 2025 18:58
@@ -79,21 +78,6 @@ pub enum DisplayFormatType {
TreeRender,
}

impl FromStr for DisplayFormatType {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is superceded by the ExplainFormat

@@ -434,3 +434,135 @@ drop table t1;

statement ok
drop table t2;

## Tests for explain format
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here are the tests showing how this works

@alamb
Copy link
Contributor Author

alamb commented Mar 12, 2025

FYI @irenjj

@irenjj
Copy link
Contributor

irenjj commented Mar 13, 2025

Thanks @alamb !
I did a quick local run. It looks like datafusion.explain.format and explain ... format [type] work together to determine the final output of EXPLAIN. datafusion.explain.format sets the default, while explain ... format [type] determines the final output. The explain ... format [type] option has a higher priority. Maybe we can mention this in explain.md.

@irenjj
Copy link
Contributor

irenjj commented Mar 13, 2025

I'll take a look at others later today.❤️

Comment on lines 1759 to 1763
displayable(optimized_plan.as_ref()).to_stringified(
e.verbose,
FinalPhysicalPlan,
DisplayFormatType::TreeRender,
),
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we can add a new function to handle tree explain like display_pg_json, then the third parameter is no longer needed as I found the values passed in below are all default values.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is a good idea -- I will try it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did it in a83ec4e and i think it looks much nicer ❤️

@irenjj
Copy link
Contributor

irenjj commented Mar 13, 2025

Thanks @alamb , others LGTM.

@alamb
Copy link
Contributor Author

alamb commented Mar 13, 2025

Thanks @alamb ! I did a quick local run. It looks like datafusion.explain.format and explain ... format [type] work together to determine the final output of EXPLAIN. datafusion.explain.format sets the default, while explain ... format [type] determines the final output. The explain ... format [type] option has a higher priority. Maybe we can mention this in explain.md.

This is a good idea -- thank you. I did it in 31f402d

Copy link
Contributor

@irenjj irenjj left a comment

Choose a reason for hiding this comment

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

LGTM, thanks again! @alamb and @waynexia

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Core DataFusion crate documentation Improvements or additions to documentation logical-expr Logical plan and expressions sql SQL Planner sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support different EXPLAIN formats via SQL Graphviz PhysicalPlan
3 participants