fix: keep RENAME TABLE clauses as separate identifiers (issue779)
RENAME TABLE a TO b, c TO d grouped the target of the first clause and the source of the second into a single IdentifierList, because the comma between the two clauses was treated as an identifier-list separator. Detect the rename layout by the TO keywords flanking both sides of the comma and leave such a comma ungrouped, so each renamed table stays a separate Identifier. Genuine target lists such as GRANT ... TO a, b are left untouched.
This commit is contained in:
1 parent
95c0c262cf
commit
241869a841
2 files changed
+46
-1
No files matched your search
@@ -414,6 +414,30 @@ def test_issue562_tzcasts():
|
||||
'SELECT f(HOUR\n from bar AT TIME ZONE \'UTC\')\nfrom foo'
|
||||
|
||||
|
||||
def test_issue779_rename_table_pairs():
|
||||
# The comma in ``RENAME TABLE a TO b, c TO d`` separates independent
|
||||
# rename clauses. The target of one clause and the source of the next
|
||||
# must stay separate Identifiers and not be merged into an IdentifierList.
|
||||
p = sqlparse.parse('RENAME TABLE a TO b, c TO d')[0]
|
||||
idents = [t for t in p.tokens if isinstance(t, sql.Identifier)]
|
||||
assert [t.value for t in idents] == ['a', 'b', 'c', 'd']
|
||||
assert not [t for t in p.tokens if isinstance(t, sql.IdentifierList)]
|
||||
comma = p.token_next_by(m=(T.Punctuation, ','))[1]
|
||||
assert comma is not None and comma.parent is p
|
||||
|
||||
# Backtick-quoted identifiers from the original report behave the same.
|
||||
p = sqlparse.parse(
|
||||
'rename table `db`.`test1` to test2, `db`.`test3` to test4')[0]
|
||||
idents = [t for t in p.tokens if isinstance(t, sql.Identifier)]
|
||||
assert [t.value for t in idents] == [
|
||||
'`db`.`test1`', 'test2', '`db`.`test3`', 'test4']
|
||||
assert not [t for t in p.tokens if isinstance(t, sql.IdentifierList)]
|
||||
|
||||
# A genuine ``TO`` target list (e.g. GRANT ... TO a, b) must still group.
|
||||
p = sqlparse.parse('GRANT SELECT ON t TO a, b')[0]
|
||||
assert p.token_next_by(i=sql.IdentifierList)[1] is not None
|
||||
|
||||
|
||||
def test_as_in_parentheses_indents():
|
||||
# did raise NoneType has no attribute is_group in _process_parentheses
|
||||
formatted = sqlparse.format('(as foo)', reindent=True)
|
||||
|
||||
Reference in new issue
Block a user