Repository: reata/sqllineage

Issue:
subquery mistake alias as table name
```
$ python -m sqllineage.core -e "SELECT col1 FROM (SELECT col2 from tab1) dt"
Statements(#): 1
Source Tables:
    dt
Target Tables:

```

expect source table as tab1

Discussion:
codecov-io: # [Codecov](https://codecov.io/gh/reata/sqllineage/pull/17?src=pr&el=h1) Report
> Merging [#17](https://codecov.io/gh/reata/sqllineage/pull/17?src=pr&el=desc) into [master](https://codecov.io/gh/reata/sqllineage/commit/b613ec49de0b613cfaa74b37282f410659dcbc06?src=pr&el=desc) will **decrease** coverage by `0.59%`.
> The diff coverage is `100%`.

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

```diff
@@            Coverage Diff            @@
##           master      #17     +/-   ##
=========================================
- Coverage   79.38%   78.78%   -0.6%     
=========================================
  Files           1        1             
  Lines          97       99      +2     
=========================================
+ Hits           77       78      +1     
- Misses         20       21      +1
```


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

------

[Continue to review full report at Codecov](https://codecov.io/gh/reata/sqllineage/pull/17?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/17?src=pr&el=footer). Last update [b613ec4...325dece](https://codecov.io/gh/reata/sqllineage/pull/17?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/sqllineage/core.py b/sqllineage/core.py
index 301c818..2f5e0b6 100644
--- a/sqllineage/core.py
+++ b/sqllineage/core.py
@@ -3,7 +3,7 @@ import sys
 from typing import List, Set
 
 import sqlparse
-from sqlparse.sql import Function, Identifier, IdentifierList, Statement, TokenList
+from sqlparse.sql import Function, Identifier, Parenthesis, Statement, TokenList
 from sqlparse.tokens import Keyword, Token, Whitespace
 
 SOURCE_TABLE_TOKENS = ('FROM', 'JOIN', 'INNER JOIN', 'LEFT JOIN', 'RIGHT JOIN', 'LEFT OUTER JOIN', 'RIGHT OUTER JOIN',
@@ -51,11 +51,9 @@ Target Tables:
         return self._target_tables
 
     def _extract_from_token(self, token: Token):
-        if not isinstance(token, TokenList):
-            return
         source_table_token_flag = target_table_token_flag = temp_table_token_flag = False
         for sub_token in token.tokens:
-            if isinstance(token, TokenList) and not isinstance(sub_token, (Identifier, IdentifierList)):
+            if isinstance(sub_token, TokenList):
                 self._extract_from_token(sub_token)
             if sub_token.ttype in Keyword:
                 if sub_token.normalized in SOURCE_TABLE_TOKENS:
@@ -75,7 +73,13 @@ Target Tables:
                     continue
                 else:
                     assert isinstance(sub_token, Identifier)
-                    self._source_tables.add(sub_token.get_real_name())
+                    if isinstance(sub_token.token_first(), 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
+                        pass
+                    else:
+                        self._source_tables.add(sub_token.get_real_name())
                     source_table_token_flag = False
             elif target_table_token_flag:
                 if sub_token.ttype == Whitespace:


Test patch:
diff --git a/tests/test_select.py b/tests/test_select.py
index cea96ef..49fe323 100644
--- a/tests/test_select.py
+++ b/tests/test_select.py
@@ -40,6 +40,10 @@ def test_select_count():
     helper("SELECT COUNT(*) FROM tab1", {"tab1"})
 
 
+def test_select_subquery():
+    helper("SELECT col1 FROM (SELECT col2 FROM tab1) dt", {"tab1"})
+
+
 def test_select_inner_join():
     helper("SELECT * FROM tab1 INNER JOIN tab2", {"tab1", "tab2"})