fix: ParsedQuery subselect edge case (#13602)

This commit is contained in:
Erik Ritter 2021-03-12 14:54:02 -08:00 committed by GitHub
parent 4fc41e1cce
commit 06d6d7f8e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 3 deletions

View File

@ -21,7 +21,14 @@ from typing import List, Optional, Set
from urllib import parse
import sqlparse
from sqlparse.sql import Identifier, IdentifierList, remove_quotes, Token, TokenList
from sqlparse.sql import (
Identifier,
IdentifierList,
Parenthesis,
remove_quotes,
Token,
TokenList,
)
from sqlparse.tokens import Keyword, Name, Punctuation, String, Whitespace
from sqlparse.utils import imt
@ -278,7 +285,9 @@ class ParsedQuery:
table_name_preceding_token = False
for item in token.tokens:
if item.is_group and not self._is_identifier(item):
if item.is_group and (
not self._is_identifier(item) or isinstance(item.tokens[0], Parenthesis)
):
self._extract_from_token(item)
if item.ttype in Keyword and (
@ -291,7 +300,6 @@ class ParsedQuery:
if item.ttype in Keyword:
table_name_preceding_token = False
continue
if table_name_preceding_token:
if isinstance(item, Identifier):
self._process_tokenlist(item)

View File

@ -158,6 +158,13 @@ class TestSupersetSqlParse(unittest.TestCase):
query = "SELECT f1, (SELECT count(1) FROM t2) FROM t1"
self.assertEqual({Table("t1"), Table("t2")}, self.extract_tables(query))
query = "SELECT f1, (SELECT count(1) FROM t2) as f2 FROM t1"
self.assertEqual({Table("t1"), Table("t2")}, self.extract_tables(query))
def test_parentheses(self):
query = "SELECT f1, (x + y) AS f2 FROM t1"
self.assertEqual({Table("t1")}, self.extract_tables(query))
def test_union(self):
query = "SELECT * FROM t1 UNION SELECT * FROM t2"
self.assertEqual({Table("t1"), Table("t2")}, self.extract_tables(query))