Skip to content

Make EXPLAIN PLAN queries honor the query context set via SET statements#17974

Merged
abhishekrb19 merged 3 commits intomasterfrom
explain_plan_set_statement
May 2, 2025
Merged

Make EXPLAIN PLAN queries honor the query context set via SET statements#17974
abhishekrb19 merged 3 commits intomasterfrom
explain_plan_set_statement

Conversation

@abhishekrb19
Copy link
Copy Markdown
Contributor

This is a follow-up patch to #17894 that adds support for honoring the query context for EXPLAIN PLAN queries via SET statements.

Description:

  • EXPLAIN PLAN queries were ignoring the query context parameters from SET statements.
  • This was because the planner config was eagerly built before the SET statements from the SQL query were processed and the query context extracted in processStatementList.
  • With this patch, processStatementList overrides the planner config in the end. In order to do this override, the field is made non-final (similar to other non-final mutable members in PlannerContext).
  • Adds a couple of tests for explain plan on select and insert queries.

This PR has:

  • been self-reviewed.
  • added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.
  • added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage is met.
  • been tested in a test Druid cluster.

- EXPLAIN PLAN queries were ignoring the query context parameters
from the SET statement.
- This was because the planner config was eagerly built before the
set statement list from the sql query is processed and the query
context is extracted from it.
- This patch now overrides the planner config in processStatementList.
In order to do this override, the field is made non-final (similar to
other non-final mutable members in PlannerContext).
Copy link
Copy Markdown
Member

@clintropolis clintropolis left a comment

Choose a reason for hiding this comment

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

good find, thanks for fixing 👍

Comment on lines +562 to +569
/**
* Add additional query context parameters to planner config, overriding any existing values.
*/
public void addAllToPlannerConfig(Map<String, Object> toAdd)
{
this.plannerConfig = this.plannerConfig.withOverrides(toAdd);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How about just adding this logic to initializeContextFields or addAllToQueryContext instead of a new method? It seems like callers would always want to call both methods, so I don't see a good reason to keep them separate since it would make it easier to make mistakes.

To go with that, I think we could also adjust the PlannerContext constructor to no longer take the PlannerConfig argument since we can get it from the PlannerToolbox, and we don't need the Preconditions.checkNotNull that is currently in the constructor because PlannerToolbox also has that check, making the one in this constructor redundant. This would mean that PlannerContext.create would no longer need to also be calling withOverrides on the context before passing to the constructor

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, makes sense to add it to initializeContextFields. I've also renamed this private method to initializeContextFieldsAndPlannerConfig

@abhishekrb19
Copy link
Copy Markdown
Contributor Author

Thanks for the quick review, @clintropolis

@abhishekrb19 abhishekrb19 merged commit 8bd4835 into master May 2, 2025
74 checks passed
@abhishekrb19 abhishekrb19 deleted the explain_plan_set_statement branch May 2, 2025 23:28
@capistrant capistrant added this to the 34.0.0 milestone Jul 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants