mirror of https://github.com/apache/superset.git
parent
51090c3f1b
commit
14b9298ef7
|
@ -318,11 +318,6 @@ class ParsedQuery:
|
||||||
|
|
||||||
table_name_preceding_token = False
|
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:
|
for item in token.tokens:
|
||||||
if item.is_group and (
|
if item.is_group and (
|
||||||
not self._is_identifier(item) or isinstance(item.tokens[0], Parenthesis)
|
not self._is_identifier(item) or isinstance(item.tokens[0], Parenthesis)
|
||||||
|
@ -336,25 +331,16 @@ class ParsedQuery:
|
||||||
table_name_preceding_token = True
|
table_name_preceding_token = True
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# If we haven't processed any identifiers it means the table name is a
|
if item.ttype in Keyword:
|
||||||
# reserved keyword (eg, "table_name") and we shouldn't skip it.
|
|
||||||
if item.ttype in Keyword and has_processed_identifier:
|
|
||||||
table_name_preceding_token = False
|
table_name_preceding_token = False
|
||||||
continue
|
continue
|
||||||
if table_name_preceding_token:
|
if table_name_preceding_token:
|
||||||
if isinstance(item, Identifier):
|
if isinstance(item, Identifier):
|
||||||
self._process_tokenlist(item)
|
self._process_tokenlist(item)
|
||||||
has_processed_identifier = True
|
|
||||||
elif isinstance(item, IdentifierList):
|
elif isinstance(item, IdentifierList):
|
||||||
for token2 in item.get_identifiers():
|
for token2 in item.get_identifiers():
|
||||||
if isinstance(token2, TokenList):
|
if isinstance(token2, TokenList):
|
||||||
self._process_tokenlist(token2)
|
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):
|
elif isinstance(item, IdentifierList):
|
||||||
if any(not self._is_identifier(token2) for token2 in item.tokens):
|
if any(not self._is_identifier(token2) for token2 in item.tokens):
|
||||||
self._extract_from_token(item)
|
self._extract_from_token(item)
|
||||||
|
|
|
@ -45,11 +45,11 @@ def test_table() -> None:
|
||||||
|
|
||||||
Special characters in the table, schema, or catalog name should be escaped correctly.
|
Special characters in the table, schema, or catalog name should be escaped correctly.
|
||||||
"""
|
"""
|
||||||
assert str(Table("table_name")) == "table_name"
|
assert str(Table("tbname")) == "tbname"
|
||||||
assert str(Table("table_name", "schema_name")) == "schema_name.table_name"
|
assert str(Table("tbname", "schemaname")) == "schemaname.tbname"
|
||||||
assert (
|
assert (
|
||||||
str(Table("table_name", "schema_name", "catalog_name"))
|
str(Table("tbname", "schemaname", "catalogname"))
|
||||||
== "catalog_name.schema_name.table_name"
|
== "catalogname.schemaname.tbname"
|
||||||
)
|
)
|
||||||
assert (
|
assert (
|
||||||
str(Table("table.name", "schema/name", "catalog\nname"))
|
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:
|
def test_extract_tables_complex() -> None:
|
||||||
"""
|
"""
|
||||||
Test a few complex queries.
|
Test a few complex queries.
|
||||||
|
|
Loading…
Reference in New Issue