Repository: reata/sqllineage

Issue:
drop table parsed as target table
```
$ sqllineage -e "DROP TABLE IF EXISTS tab1"
Statements(#): 1
Source Tables:
    
Target Tables:
    tab1
```

expect:
When a table is in target_tables, dropping table result in a removal from target_tables. Otherwise  the DROP DML doesn't affect the result.

Discussion:

Patch:
diff --git a/sqllineage/core.py b/sqllineage/core.py
index fbf17f2..74c178d 100644
--- a/sqllineage/core.py
+++ b/sqllineage/core.py
@@ -4,7 +4,7 @@ from typing import List, Set
 
 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')
@@ -20,7 +20,10 @@ class LineageParser(object):
         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


Test patch:
diff --git a/tests/test_others.py b/tests/test_others.py
index a6f8472..6fc57c9 100644
--- a/tests/test_others.py
+++ b/tests/test_others.py
@@ -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"})