Skip to content

feat: allow EXPLAIN on multi-statement SQL beginning with SET #20106

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 5 commits into
base: master
Choose a base branch
from

Conversation

jasonmp85
Copy link

What does this PR do?

SQL strings containing multiple statements cause problems with the existing explain logic. This change adds a regex to strip an arbitrary number of SET commands from the head of all incoming SQL, such that the eventual EXPLAIN will be performed on just the trailing SQL.

This doesn't fully address all multi-statement SQL strings: in particular, clients might send e.g. both a SELECT and an UPDATE in the same string. As before, this would be passed as-is to the EXPLAIN machinery.

Motivation

DBMON-2626

Review checklist (to be filled by reviewers)

  • Feature or bugfix MUST have appropriate tests (unit, integration, e2e)
  • Add the qa/skip-qa label if the PR doesn't need to be tested during QA.
  • If you need to backport this PR to another branch, you can add the backport/<branch-name> label to the PR and it will automatically open a backport PR once this one is merged

Copy link

codecov bot commented Apr 18, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.66%. Comparing base (5cdecdf) to head (0e89836).

Additional details and impacted files
Flag Coverage Δ
activemq ?
cassandra ?
hive ?
hivemq ?
hudi ?
ignite ?
jboss_wildfly ?
kafka ?
postgres 93.27% <100.00%> (+18.30%) ⬆️
presto ?
solr ?

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jasonmp85 jasonmp85 force-pushed the fix-explain-with-set branch 2 times, most recently from 431c4d2 to 4dabdf0 Compare April 21, 2025 15:30
@jasonmp85 jasonmp85 force-pushed the fix-explain-with-set branch from 4dabdf0 to 77d7577 Compare April 22, 2025 19:33
@jasonmp85 jasonmp85 requested a review from justiniso April 22, 2025 20:08
SQL strings containing multiple statements cause problems with the ex-
isting explain logic. This change adds a regex to strip an arbitrary
number of `SET` commands from the head of all incoming SQL, such that
the eventual `EXPLAIN` will be performed on just the trailing SQL.

This doesn't fully address all multi-statement SQL strings: in part-
icular clients might send e.g. both a SELECT and an UPDATE in the same
string. As before, this would be passed as-is to the EXPLAIN machinery.

Refs: DBMON-2626
@jasonmp85 jasonmp85 force-pushed the fix-explain-with-set branch from 77d7577 to f37b43b Compare April 22, 2025 20:27
Copy link
Contributor

@lu-zhengda lu-zhengda left a comment

Choose a reason for hiding this comment

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

Trim leading SET statement would make it pass _can_explain_statement. Can you add an integration test to make sure explain will work with multi statements?
If it's not feasible to do it with integration test and you tested it manually, can you add a screenshot instead?

@@ -748,6 +748,8 @@ def _run_and_track_explain(self, dbname, statement, obfuscated_statement, query_

@tracked_method(agent_check_getter=agent_check_getter)
def _run_explain_safe(self, dbname, statement, obfuscated_statement, query_signature):
obfuscated_statement = trim_leading_set_stmts(obfuscated_statement)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we consider only run trim_leading_set_stmts if the obfuscated_statement starts with SET?

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.

4 participants