fix: handle CTEs with comments on is_select (#16769)

This commit is contained in:
Beto Dealmeida 2021-09-21 16:23:54 -07:00 committed by GitHub
parent 493e804320
commit 48a61bacc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 3 deletions

View File

@ -131,13 +131,17 @@ class ParsedQuery:
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

View File

@ -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()