codecov-io: # [Codecov](https://codecov.io/gh/reata/sqllineage/pull/24?src=pr&el=h1) Report
> Merging [#24](https://codecov.io/gh/reata/sqllineage/pull/24?src=pr&el=desc) into [master](https://codecov.io/gh/reata/sqllineage/commit/245a83548482fbf78f90a26cea97a534c61be2e5?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/reata/sqllineage/pull/24/graphs/tree.svg?width=650&token=latkaw2Lx6&height=150&src=pr)](https://codecov.io/gh/reata/sqllineage/pull/24?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master      #24   +/-   ##
=======================================
  Coverage   79.79%   79.79%           
=======================================
  Files           1        1           
  Lines          99       99           
=======================================
  Hits           79       79           
  Misses         20       20
```


| [Impacted Files](https://codecov.io/gh/reata/sqllineage/pull/24?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [sqllineage/core.py](https://codecov.io/gh/reata/sqllineage/pull/24/diff?src=pr&el=tree#diff-c3FsbGluZWFnZS9jb3JlLnB5) | `79.79% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/reata/sqllineage/pull/24?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/reata/sqllineage/pull/24?src=pr&el=footer). Last update [245a835...5fcd262](https://codecov.io/gh/reata/sqllineage/pull/24?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

diff --git a/sqllineage/core.py b/sqllineage/core.py
index 74c178d..2faa914 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 DDL, Keyword, Token
+from sqlparse.tokens import Keyword, Token
 
 SOURCE_TABLE_TOKENS = ('FROM', 'JOIN', 'INNER JOIN', 'LEFT JOIN', 'RIGHT JOIN', 'LEFT OUTER JOIN', 'RIGHT OUTER JOIN',
                        'FULL OUTER JOIN', 'CROSS JOIN')
@@ -18,9 +18,9 @@ class LineageParser(object):
         self._encoding = encoding
         self._source_tables = set()
         self._target_tables = set()
-        self._stmt = sqlparse.parse(sql, self._encoding)
+        self._stmt = sqlparse.parse(sql.strip(), self._encoding)
         for stmt in self._stmt:
-            if stmt.token_first().ttype == DDL and stmt.token_first().normalized == "DROP":
+            if stmt.get_type() == "DROP":
                 self._target_tables -= {t.get_real_name() for t in stmt.tokens if isinstance(t, Identifier)}
             else:
                 self._extract_from_token(stmt)
@@ -76,7 +76,7 @@ Target Tables:
                     continue
                 else:
                     assert isinstance(sub_token, Identifier)
-                    if isinstance(sub_token.token_first(), Parenthesis):
+                    if isinstance(sub_token.token_first(skip_cm=True), Parenthesis):
                         # SELECT col1 FROM (SELECT col2 FROM tab1) dt, the subquery will be parsed as Identifier
                         # and this Identifier's get_real_name method would return alias name dt
                         # referring https://github.com/andialbrecht/sqlparse/issues/218 for further information
@@ -90,8 +90,8 @@ Target Tables:
                 elif isinstance(sub_token, Function):
                     # insert into tab (col1, col2), tab (col1, col2) will be parsed as Function
                     # referring https://github.com/andialbrecht/sqlparse/issues/483 for further information
-                    assert isinstance(sub_token.token_first(), Identifier)
-                    self._target_tables.add(sub_token.token_first().get_real_name())
+                    assert isinstance(sub_token.token_first(skip_cm=True), Identifier)
+                    self._target_tables.add(sub_token.token_first(skip_cm=True).get_real_name())
                     target_table_token_flag = False
                 else:
                     assert isinstance(sub_token, Identifier)

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.
diff --git a/tests/test_others.py b/tests/test_others.py
index 6fc57c9..7e0a287 100644
--- a/tests/test_others.py
+++ b/tests/test_others.py
@@ -22,6 +22,11 @@ def test_drop():
     helper("DROP TABLE IF EXISTS tab1", None, None)
 
 
+def test_drop_with_comment():
+    helper("""--comment
+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)
 
@@ -33,3 +38,8 @@ def test_create_select():
 def test_split_statements():
     sql = "SELECT * FROM tab1; SELECT * FROM tab2;"
     assert len(LineageParser(sql).statements) == 2
+
+
+def test_split_statements_with_heading_and_ending_new_line():
+    sql = "\nSELECT * FROM tab1;\nSELECT * FROM tab2;\n"
+    assert len(LineageParser(sql).statements) == 2

pip install -e .
parse_log_pytest
pytest
pytest flake8
3.7
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
attrs @ file:///croot/attrs_1668696182826/work
certifi @ file:///croot/certifi_1671487769961/work/certifi
flake8==5.0.4
flit_core @ file:///opt/conda/conda-bld/flit-core_1644941570762/work/source/flit_core
importlib-metadata==4.2.0
iniconfig @ file:///home/linux1/recipes/ci/iniconfig_1610983019677/work
mccabe==0.7.0
packaging @ file:///croot/packaging_1671697413597/work
pluggy @ file:///tmp/build/80754af9/pluggy_1648042572264/work
py @ file:///opt/conda/conda-bld/py_1644396412707/work
pycodestyle==2.9.1
pyflakes==2.5.0
pytest==7.1.2
-e git+https://github.com/reata/sqllineage.git@245a83548482fbf78f90a26cea97a534c61be2e5#egg=sqllineage
sqlparse==0.3.1
tomli @ file:///opt/conda/conda-bld/tomli_1657175507142/work
typing_extensions @ file:///croot/typing_extensions_1669924550328/work
zipp @ file:///croot/zipp_1672387121353/work

name: sqllineage
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - attrs=22.1.0=py37h06a4308_0
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2022.12.7=py37h06a4308_0
  - flit-core=3.6.0=pyhd3eb1b0_0
  - importlib_metadata=4.11.3=hd3eb1b0_0
  - iniconfig=1.1.1=pyhd3eb1b0_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - packaging=22.0=py37h06a4308_0
  - pip=22.3.1=py37h06a4308_0
  - pluggy=1.0.0=py37h06a4308_1
  - py=1.11.0=pyhd3eb1b0_0
  - pytest=7.1.2=py37h06a4308_0
  - python=3.7.16=h7a1cb2a_0
  - readline=8.2=h5eee18b_0
  - setuptools=65.6.3=py37h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - tomli=2.0.1=py37h06a4308_0
  - typing_extensions=4.4.0=py37h06a4308_0
  - wheel=0.38.4=py37h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zipp=3.11.0=py37h06a4308_0
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - flake8==5.0.4
      - importlib-metadata==4.2.0
      - mccabe==0.7.0
      - pycodestyle==2.9.1
      - pyflakes==2.5.0
      - sqlparse==0.3.1
prefix: /opt/conda/envs/sqllineage

tests/test_others.py::test_drop_with_comment tests/test_others.py::test_split_statements_with_heading_and_ending_new_line
tests/test_others.py::test_use tests/test_others.py::test_create tests/test_others.py::test_create_if_not_exist tests/test_others.py::test_create_after_drop tests/test_others.py::test_drop tests/test_others.py::test_drop_after_create tests/test_others.py::test_create_select tests/test_others.py::test_split_statements
sqllineage/core.py
sqllineage/core.py