Skip to content

Commit

Permalink
fix: DROP table DDL with different logic (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
reata committed Aug 11, 2019
1 parent 95cc52f commit 245a835
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
7 changes: 5 additions & 2 deletions sqllineage/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import sqlparse
from sqlparse.sql import Function, Identifier, Parenthesis, Statement, TokenList
from sqlparse.tokens import Keyword, Token
from sqlparse.tokens import DDL, Keyword, Token

SOURCE_TABLE_TOKENS = ('FROM', 'JOIN', 'INNER JOIN', 'LEFT JOIN', 'RIGHT JOIN', 'LEFT OUTER JOIN', 'RIGHT OUTER JOIN',
'FULL OUTER JOIN', 'CROSS JOIN')
Expand All @@ -20,7 +20,10 @@ def __init__(self, sql: str, encoding=None):
self._target_tables = set()
self._stmt = sqlparse.parse(sql, self._encoding)
for stmt in self._stmt:
self._extract_from_token(stmt)
if stmt.token_first().ttype == DDL and stmt.token_first().normalized == "DROP":
self._target_tables -= {t.get_real_name() for t in stmt.tokens if isinstance(t, Identifier)}
else:
self._extract_from_token(stmt)
self._tmp_tables = self._source_tables.intersection(self._target_tables)
self._source_tables -= self._tmp_tables
self._target_tables -= self._tmp_tables
Expand Down
20 changes: 20 additions & 0 deletions tests/test_others.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ def test_use():
helper("USE db1")


def test_create():
helper("CREATE TABLE tab1 (col1 STRING)", None, {"tab1"})


def test_create_if_not_exist():
helper("CREATE TABLE IF NOT EXISTS tab1 (col1 STRING)", None, {"tab1"})


def test_create_after_drop():
helper("DROP TABLE IF EXISTS tab1; CREATE TABLE IF NOT EXISTS tab1 (col1 STRING)", None, {"tab1"})


def test_drop():
helper("DROP TABLE IF EXISTS tab1", None, None)


def test_drop_after_create():
helper("CREATE TABLE IF NOT EXISTS tab1 (col1 STRING);DROP TABLE IF EXISTS tab1", None, None)


def test_create_select():
helper("CREATE TABLE tab1 SELECT * FROM tab2", {"tab2"}, {"tab1"})

Expand Down

0 comments on commit 245a835

Please sign in to comment.