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

fix: Allow "EXPLAIN" queries when "Allow DML" setting is False #11348

Merged
merged 9 commits into from
Oct 21, 2020

Conversation

hughhhh
Copy link
Member

@hughhhh hughhhh commented Oct 20, 2020

SUMMARY

Fixing is_explain function in sql_parse to properly catch EXPLAIN statements. sqlparse out of the box doesn't handle comments well, so we have to parse the query ourselves.

Closes #8827

Before

Screen Shot 2020-10-20 at 6 08 19 PM

After

Screen Shot 2020-10-20 at 6 08 47 PM

TEST PLAN

  1. Goto Sqllab

  2. Run the following query:

EXPLAIN SELECT * FROM logs
  1. See results populate

ADDITIONAL INFORMATION

  • Has associated issue:
  • Changes UI
  • Requires DB Migration.
  • Confirm DB Migration upgrade and downgrade tested.
  • Introduces new feature or API
  • Removes existing feature or API

@codecov-io
Copy link

codecov-io commented Oct 20, 2020

Codecov Report

Merging #11348 into master will decrease coverage by 4.67%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #11348      +/-   ##
==========================================
- Coverage   65.75%   61.08%   -4.68%     
==========================================
  Files         838      838              
  Lines       39714    39714              
  Branches     3613     3613              
==========================================
- Hits        26115    24260    -1855     
- Misses      13498    15273    +1775     
- Partials      101      181      +80     
Flag Coverage Δ
#cypress ?
#javascript 62.60% <ø> (ø)
#python 60.18% <100.00%> (-0.73%) ⬇️

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

Impacted Files Coverage Δ
superset/sql_parse.py 99.32% <100.00%> (+0.01%) ⬆️
superset-frontend/src/SqlLab/App.jsx 0.00% <0.00%> (-100.00%) ⬇️
superset-frontend/src/explore/App.jsx 0.00% <0.00%> (-100.00%) ⬇️
superset-frontend/src/dashboard/App.jsx 0.00% <0.00%> (-100.00%) ⬇️
superset-frontend/src/explore/index.jsx 0.00% <0.00%> (-100.00%) ⬇️
superset-frontend/src/dashboard/index.jsx 0.00% <0.00%> (-100.00%) ⬇️
superset-frontend/src/setup/setupColors.js 0.00% <0.00%> (-100.00%) ⬇️
superset-frontend/src/chart/ChartContainer.jsx 0.00% <0.00%> (-100.00%) ⬇️
superset-frontend/src/setup/setupFormatters.js 0.00% <0.00%> (-100.00%) ⬇️
superset-frontend/src/explore/reducers/index.js 0.00% <0.00%> (-100.00%) ⬇️
... and 178 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7369039...f7f8f83. Read the comment docs.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Please format your PR title to match: ^(build|chore|ci|docs|feat|fix|perf|refactor|style|test|other)((.+))?:\s.+!

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Please format your PR title to match: ^(build|chore|ci|docs|feat|fix|perf|refactor|style|test|other)((.+))?:\s.+!

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Please format your PR title to match: ^(build|chore|ci|docs|feat|fix|perf|refactor|style|test|other)((.+))?:\s.+!

@mistercrunch mistercrunch changed the title [WIP] Fix Allow "EXPLAIN" queries when "Allow DML" setting is False fix: Allow "EXPLAIN" queries when "Allow DML" setting is False Oct 20, 2020
@@ -111,7 +111,11 @@ def is_select(self) -> bool:
return self._parsed[0].get_type() == "SELECT"

def is_explain(self) -> bool:
return self.stripped().upper().startswith("EXPLAIN")
# Parsing SQL statement for EXPLAIN and filtering out comments
Copy link
Member

Choose a reason for hiding this comment

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

Thinking about whether the right logic here is first(), any() or all() and I think it's either

    1. it works only if the SQL is a single statement, and raises otherwise
    1. all of the statements are EXPLAIN (which is not typical)

@github-actions github-actions bot dismissed stale reviews from themself October 20, 2020 18:42

All good!

@pull-request-size pull-request-size bot added size/M and removed size/S labels Oct 21, 2020
@hughhhh hughhhh marked this pull request as ready for review October 21, 2020 01:10
@hughhhh hughhhh requested a review from betodealmeida October 21, 2020 01:12
Comment on lines 122 to 125
if statements_without_comments[0].startswith("EXPLAIN"):
return True

return False
Copy link
Member

Choose a reason for hiding this comment

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

You can simplify this:

return statements_without_comments[0].startswith("EXPLAIN")

statement.strip(" \t\n;")
for statement in self.stripped().upper().splitlines()
if not statement.startswith("--")
]
Copy link
Member

Choose a reason for hiding this comment

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

This will miss some edge cases like:

-- This is a comment
 -- this is another comment but with a space in the front
EXPLAIN SELECT * FROM TABLE

You need to lstrip() each line as well before testing if it startswith():

        statements_without_comments = [
            statement.strip(" \t\n;")
            for statement in self.stripped().upper().splitlines()
            if not statement.lstrip().startswith("--")
        ]

Copy link
Member

Choose a reason for hiding this comment

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

I find it simpler to just use regular expression:

    queries = re.sub(r'\s*--.*', '', queries)
    queries = [x.strip(' \t\n\r') for x in queries.split(';')]
    queries = [x for x in queries if x]  # query must be non-empty

if statement.upper().startswith("EXPLAIN"):
return True
# Remove comments
statements_without_comments = [statement.strip(" \t\n;") for statement in self.stripped().upper().splitlines() if not statement.startswith("--")]
Copy link
Member

Choose a reason for hiding this comment

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

sqlparse.format offers a strip_comments option that will also take care of /*this*/ and probably more.

@hughhhh hughhhh requested a review from betodealmeida October 21, 2020 16:47
Co-authored-by: Beto Dealmeida <roberto@dealmeida.net>
@hughhhh hughhhh merged commit dfbcbcc into master Oct 21, 2020
@hughhhh hughhhh deleted the hugh/so-495 branch October 21, 2020 19:59
@mistercrunch
Copy link
Member

WAIT A MINUTE! DARK THEME!?!?!?
danceparty

auxten pushed a commit to auxten/incubator-superset that referenced this pull request Nov 20, 2020
@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 1.0.0 labels Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels size/M 🚢 1.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Permit "EXPLAIN" with "Allow DML" unchecked
5 participants