fix: revert #17654 to fix subselect table name parsing (#18017)

This commit is contained in:
Erik Ritter 2022-01-12 21:28:23 -08:00 committed by GitHub
parent 51090c3f1b
commit 14b9298ef7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 35 deletions

View File

@ -318,11 +318,6 @@ class ParsedQuery:
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)
@ -336,25 +331,16 @@ class ParsedQuery:
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)

View File

@ -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"))
@ -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.