Skip to content

ES|QL: enable EXPLAIN (snapshot only) #129526

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

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

Conversation

luigidellaquila
Copy link
Contributor

@luigidellaquila luigidellaquila commented Jun 17, 2025

Re-enabling a very basic EXPLAIN command.
It's in snapshot mode only for now, and it will be like that for a while probably.
The main purpose for now is to help development, but it could be refined and promoted to production eventually.

For now it's not a real command, in particular it doesn't do any compute, so it cannot be chained with further commands.
We could make it a real command, with evaluators and all the rest, but now the planning logic is very coupled into EsqlSession, so if we want EXPLAIN to be really representative of what happens in there, we'll have to keep it in there. Or refactor how planning works.

The syntax is EXPLAIN ( <query> ) and produces information about parsed query, optimized logical plan and optimized physical plan

eg.

explain ( row a = 1 )
{
    "took": 281,
    "is_partial": false,
    "documents_found": 0,
    "values_loaded": 0,
    "columns": [
        {
            "name": "role",
            "type": "keyword"
        },
        {
            "name": "type",
            "type": "keyword"
        },
        {
            "name": "plan",
            "type": "keyword"
        }
    ],
    "values": [
        [
            "coordinator",
            "parsedPlan",
            "Row[[1[INTEGER] AS a#2]]"
        ],
        [
            "coordinator",
            "optimizedLogicalPlan",
            "Limit[1000[INTEGER],false]\n\\_LocalRelation[[a{r}#2],[IntVectorBlock[vector=ConstantIntVector[positions=1, value=1]]]]"
        ],
        [
            "coordinator",
            "optimizedPhysicalPlan",
            "LimitExec[1000[INTEGER],4]\n\\_LocalSourceExec[[a{r}#2],[IntVectorBlock[vector=ConstantIntVector[positions=1, value=1]]]]"
        ]
    ]
}

I'm using round parentheses (and not square brackets, as in the original syntax) just because FORK is already using them for subqueries, but it's something we can change.

| rowCommand
| showCommand
// in development
| {this.isDevVersion()}? timeSeriesCommand
| {this.isDevVersion()}? explainCommand
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moving it to dev mode. It didn't work before, so there is really no reason to have it in the GA grammar

;

subqueryExpression
: OPENING_BRACKET query CLOSING_BRACKET
: LP query RP
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Aligned with FORK

@luigidellaquila luigidellaquila added :Analytics/ES|QL AKA ESQL v8.19.0 auto-backport Automatically create backport pull requests when merged >non-issue labels Jun 18, 2025
@luigidellaquila luigidellaquila marked this pull request as ready for review June 18, 2025 09:22
@elasticsearchmachine elasticsearchmachine added the Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) label Jun 18, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-analytical-engine (Team:Analytics)

Copy link
Contributor

@idegtiarenko idegtiarenko left a comment

Choose a reason for hiding this comment

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

👍 for making int snapshot only until that is properly finalized

Copy link
Contributor

@ivancea ivancea left a comment

Choose a reason for hiding this comment

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

LGTM. And surely, dev mode suits it better than GA

@@ -23,6 +23,9 @@ FROM_COMMA : COMMA -> type(COMMA);
FROM_ASSIGN : ASSIGN -> type(ASSIGN);
METADATA : 'metadata';

// we need this for EXPLAIN
FROM_RP : RP -> type(RP), popMode;
Copy link
Contributor

Choose a reason for hiding this comment

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

I see this RP is missing from some modes:

Now, JOIN technically would fail anyway, as the "ON" token changes to expression mode, so the question is if we want it to fail at lexer or parser level (?).
My guess is that a query like EXPLAIN (FROM index | LOOKUP JOIN lookup) would fail in different ways depending on this, but I'm not sure of which error we usually prefer

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My actual concern is that every time we add a new command we'll have to do all this push/pop dance, but that's a broader topic...
About the error message, I'm not sure either

Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if we add an EXPLAIN inside an EXPLAIN? I fear it could fail in the logical-to-physical conversion with a weird error. I would add a test of that here too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Today it's forbidden on purpose

@@ -207,6 +235,17 @@ public void executeOptimizedPlan(
ActionListener<Result> listener
) {
PhysicalPlan physicalPlan = logicalPlanToPhysicalPlan(optimizedPlan, request);
if (explainMode) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we pass these vars (explainMode, and the plan strings) as parameters instead? If I'm not wrong, calling session.execute() twice will leak those vars and break the second query. We're not doing that I think, but probably worth avoiding it (?)

@ivancea
Copy link
Contributor

ivancea commented Jun 18, 2025

Just saw that the Explain class has some old commented code. I would also remove it and clean it up a bit, as it looks ancient.
Also, it has an output(), which looks wrong now. It's probably better to throw an exception there as it's unused now

@luigidellaquila luigidellaquila removed auto-backport Automatically create backport pull requests when merged v8.19.0 labels Jun 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Analytics/ES|QL AKA ESQL >non-issue Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) v9.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants