Skip to content

Commit

Permalink
feat: support MySQL RENAME TABLE statement
Browse files Browse the repository at this point in the history
  • Loading branch information
reata committed Aug 27, 2022
1 parent c497068 commit d05584c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
24 changes: 20 additions & 4 deletions sqllineage/core/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ def analyze(self, stmt: Statement) -> StatementLineageHolder:
holder = StatementLineageHolder()
elif stmt.get_type() == "DROP":
holder = self._extract_from_ddl_drop(stmt)
elif stmt.get_type() == "ALTER":
elif (
stmt.get_type() == "ALTER"
or stmt.token_first(skip_cm=True).normalized == "RENAME"
):
holder = self._extract_from_ddl_alter(stmt)
else:
# DML parsing logic also applies to CREATE DDL
Expand All @@ -68,10 +71,23 @@ def _extract_from_ddl_drop(cls, stmt: Statement) -> StatementLineageHolder:
@classmethod
def _extract_from_ddl_alter(cls, stmt: Statement) -> StatementLineageHolder:
holder = StatementLineageHolder()
tables = [Table.of(t) for t in stmt.tokens if isinstance(t, Identifier)]
tables = []
for t in stmt.tokens:
if isinstance(t, Identifier):
tables.append(Table.of(t))
elif isinstance(t, IdentifierList):
for identifier in t.get_identifiers():
tables.append(Table.of(identifier))
keywords = [t for t in stmt.tokens if t.is_keyword]
if any(k.normalized == "RENAME" for k in keywords) and len(tables) == 2:
holder.add_rename(tables[0], tables[1])
if any(k.normalized == "RENAME" for k in keywords):
if stmt.get_type() == "ALTER" and len(tables) == 2:
holder.add_rename(tables[0], tables[1])
elif (
stmt.token_first(skip_cm=True).normalized == "RENAME"
and len(tables) % 2 == 0
):
for i in range(0, len(tables), 2):
holder.add_rename(tables[i], tables[i + 1])
if any(k.normalized == "EXCHANGE" for k in keywords) and len(tables) == 2:
holder.add_write(tables[0])
holder.add_read(tables[1])
Expand Down
12 changes: 12 additions & 0 deletions tests/test_others.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,18 @@ def test_alter_table_rename():
assert_table_lineage_equal("alter table tab1 rename to tab2;", None, None)


def test_rename_table():
"""
This syntax is MySQL specific:
https://dev.mysql.com/doc/refman/8.0/en/rename-table.html
"""
assert_table_lineage_equal("rename table tab1 to tab2", None, None)


def test_rename_tables():
assert_table_lineage_equal("rename table tab1 to tab2, tab3 to tab4", None, None)


def test_alter_table_exchange_partition():
"""
See https://cwiki.apache.org/confluence/display/Hive/Exchange+Partition for language manual
Expand Down

0 comments on commit d05584c

Please sign in to comment.