Skip to content

refactor: update SqlPlan for more cleaner variants#7966

Open
sunng87 wants to merge 4 commits intoGreptimeTeam:mainfrom
sunng87:refactor/smaller-plan
Open

refactor: update SqlPlan for more cleaner variants#7966
sunng87 wants to merge 4 commits intoGreptimeTeam:mainfrom
sunng87:refactor/smaller-plan

Conversation

@sunng87
Copy link
Copy Markdown
Member

@sunng87 sunng87 commented Apr 14, 2026

I hereby agree to the terms of the GreptimeDB CLA.

Refer to a related PR or issue link (optional)

What's changed and what's your intention?

Our SqlPlan has been unclear with its members:

  • query string
  • Option<LogicalPlan>
  • Option<Statement>
  • Option<Schema>

in this patch, I refactored it into several fixed variants:

  • Empty query
  • Short-cut query: not processed by query engine
  • LogicalPlan: processed by datafusion query engine
  • Statement: processed by our own handler, eg CREATE TABLE/SHOW TABLES

In this case, we won't store 3 forms (string, parsed statement, plan) of every query.

PR Checklist

Please convert it to a draft if some of the following conditions are not met.

  • I have written the necessary rustdoc comments.
  • I have added the necessary unit tests and integration tests.
  • This PR requires documentation updates.
  • API changes are backward compatible.
  • Schema or data changes are backward compatible.

@sunng87 sunng87 requested review from a team, discord9, evenyag and waynexia as code owners April 14, 2026 13:39
@sunng87 sunng87 requested a review from shuiyisong April 14, 2026 13:39
@github-actions github-actions bot added the docs-not-required This change does not impact docs. label Apr 14, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the query execution and planning logic to rely more on LogicalPlan and the original query string rather than the parsed Statement AST. Key changes include redefining SqlPlan as an enum to handle different query types (Empty, Shortcut, Plan, Statement), updating the SqlQueryHandler trait to accept a query string instead of an optional statement, and implementing is_readonly_plan to determine read-only status directly from the logical plan. Additionally, the DescribeResult struct was simplified by removing the redundant schema field, and support for enterprise-specific 'show' statements was added to the Postgres handler. I have no feedback to provide as there were no review comments.

@sunng87 sunng87 requested a review from MichaelScofield April 15, 2026 03:19
&self,
stmt: Option<Statement>,
plan: LogicalPlan,
query: String,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Seems this query can be derived from plan inside this function and only when needed? So that you don't have to do let query = logical_plan.display_indent().to_string(); outside every time, which is costly.

Copy link
Copy Markdown
Member Author

@sunng87 sunng87 Apr 16, 2026

Choose a reason for hiding this comment

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

In the generated plan we lost the original query string, the string from logical_plan.display_indent().to_string() is actually the explain view of plan, instead of the original one.

The only case we need to use logical_plan.display_indent().to_string() is for the grpc query interface when no query string is provided. Actually I think that interface is not in use.

Comment thread src/servers/src/mysql/handler.rs Outdated
Comment thread src/servers/src/mysql/handler.rs Outdated
Comment thread src/servers/src/lib.rs
/// Hardcoded SQL shortcuts
Shortcut(String),
/// Datafusion parsed execution plan with the original query string
Plan(LogicalPlan, String),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The original query string can be calculated from LogicalPlan, is it needed to store it here as well?

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

Labels

docs-not-required This change does not impact docs. size/M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants