Skip to content

Commit

Permalink
fix: revert #17654 to fix subselect table name parsing (#18017)
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Ritter authored Jan 13, 2022
1 parent 51090c3 commit 14b9298
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 35 deletions.
16 changes: 1 addition & 15 deletions superset/sql_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,6 @@ def _extract_from_token(self, token: Token) -> None:

table_name_preceding_token = False

# If the table name is a reserved word (eg, "table_name") it won't be returned. We
# fix this by ensuring that at least one identifier is returned after the FROM
# before stopping on a keyword.
has_processed_identifier = False

for item in token.tokens:
if item.is_group and (
not self._is_identifier(item) or isinstance(item.tokens[0], Parenthesis)
Expand All @@ -336,25 +331,16 @@ def _extract_from_token(self, token: Token) -> None:
table_name_preceding_token = True
continue

# If we haven't processed any identifiers it means the table name is a
# reserved keyword (eg, "table_name") and we shouldn't skip it.
if item.ttype in Keyword and has_processed_identifier:
if item.ttype in Keyword:
table_name_preceding_token = False
continue
if table_name_preceding_token:
if isinstance(item, Identifier):
self._process_tokenlist(item)
has_processed_identifier = True
elif isinstance(item, IdentifierList):
for token2 in item.get_identifiers():
if isinstance(token2, TokenList):
self._process_tokenlist(token2)
has_processed_identifier = True
elif item.ttype in Keyword:
# convert into an identifier
fixed = Identifier([Token(Name, item.value)])
self._process_tokenlist(fixed)
has_processed_identifier = True
elif isinstance(item, IdentifierList):
if any(not self._is_identifier(token2) for token2 in item.tokens):
self._extract_from_token(item)
Expand Down
24 changes: 4 additions & 20 deletions tests/unit_tests/sql_parse_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ def test_table() -> None:
Special characters in the table, schema, or catalog name should be escaped correctly.
"""
assert str(Table("table_name")) == "table_name"
assert str(Table("table_name", "schema_name")) == "schema_name.table_name"
assert str(Table("tbname")) == "tbname"
assert str(Table("tbname", "schemaname")) == "schemaname.tbname"
assert (
str(Table("table_name", "schema_name", "catalog_name"))
== "catalog_name.schema_name.table_name"
str(Table("tbname", "schemaname", "catalogname"))
== "catalogname.schemaname.tbname"
)
assert (
str(Table("table.name", "schema/name", "catalog\nname"))
Expand Down Expand Up @@ -537,22 +537,6 @@ def test_extract_tables_multistatement() -> None:
}


def test_extract_tables_keyword() -> None:
"""
Test that table names that are keywords work as expected.
If the table name is a ``sqlparse`` reserved keyword (eg, "table_name") the parser
needs extra logic to identify it.
"""
assert extract_tables("SELECT * FROM table_name") == {Table("table_name")}
assert extract_tables("SELECT * FROM table_name AS foo") == {Table("table_name")}

# these 3 are considered keywords
assert extract_tables("SELECT * FROM catalog_name.schema_name.table_name") == {
Table("table_name", "schema_name", "catalog_name")
}


def test_extract_tables_complex() -> None:
"""
Test a few complex queries.
Expand Down

0 comments on commit 14b9298

Please sign in to comment.