Skip to content

Commit

Permalink
fix: handle CTEs with comments on is_select (apache#16769)
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida authored and Emmanuel Bavoux committed Nov 14, 2021
1 parent cdf92df commit 7965101
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
10 changes: 7 additions & 3 deletions superset/sql_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,17 @@ def limit(self) -> Optional[int]:
return self._limit

def is_select(self) -> bool:
return self._parsed[0].get_type() == "SELECT"
# make sure we strip comments; prevents a bug with coments in the CTE
parsed = sqlparse.parse(self.strip_comments())
return parsed[0].get_type() == "SELECT"

def is_valid_ctas(self) -> bool:
return self._parsed[-1].get_type() == "SELECT"
parsed = sqlparse.parse(self.strip_comments())
return parsed[-1].get_type() == "SELECT"

def is_valid_cvas(self) -> bool:
return len(self._parsed) == 1 and self._parsed[0].get_type() == "SELECT"
parsed = sqlparse.parse(self.strip_comments())
return len(parsed) == 1 and parsed[0].get_type() == "SELECT"

def is_explain(self) -> bool:
# Remove comments
Expand Down
46 changes: 46 additions & 0 deletions tests/unit_tests/sql_parse_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from superset.sql_parse import ParsedQuery


def test_cte_with_comments():
sql = ParsedQuery(
"""WITH blah AS
(SELECT * FROM core_dev.manager_team),
blah2 AS
(SELECT * FROM core_dev.manager_workspace)
SELECT * FROM blah
INNER JOIN blah2 ON blah2.team_id = blah.team_id"""
)
assert sql.is_select()

sql = ParsedQuery(
"""WITH blah AS
/*blahblahbalh*/
(SELECT * FROM core_dev.manager_team),
--blahblahbalh
blah2 AS
(SELECT * FROM core_dev.manager_workspace)
SELECT * FROM blah
INNER JOIN blah2 ON blah2.team_id = blah.team_id"""
)
assert sql.is_select()

0 comments on commit 7965101

Please sign in to comment.