Repository: reata/sqllineage

Issue:
Support Create Table Like Statement
```
drop table  if exists tab_a;
create table if not exists tab_a like tab_b;
```
drop before create, the result is tab_a exists after the above statements;


If we switch the order:
```
create table if not exists tab_a like tab_b;
drop table  if exists tab_a;
```
Although the above statements make no sense, still, we should output result as tab_a does not exist.

Under current circumstances, both cases will be that tab_a does not exists since tab_a is identified as temp table. This is related to #23 

Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/reata/sqllineage/pull/42?src=pr&el=h1) Report
> Merging [#42](https://codecov.io/gh/reata/sqllineage/pull/42?src=pr&el=desc) into [master](https://codecov.io/gh/reata/sqllineage/commit/b8eaecc85b279dc18f9464b9e2bf6b2164b63211&el=desc) will **increase** coverage by `0.78%`.
> The diff coverage is `100.00%`.

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

```diff
@@            Coverage Diff             @@
##           master      #42      +/-   ##
==========================================
+ Coverage   78.84%   79.62%   +0.78%     
==========================================
  Files           1        1              
  Lines         104      108       +4     
==========================================
+ Hits           82       86       +4     
  Misses         22       22              
```


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

------

[Continue to review full report at Codecov](https://codecov.io/gh/reata/sqllineage/pull/42?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/42?src=pr&el=footer). Last update [b8eaecc...b86a485](https://codecov.io/gh/reata/sqllineage/pull/42?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 a46e388..1d9a021 100644
--- a/sqllineage/core.py
+++ b/sqllineage/core.py
@@ -4,7 +4,7 @@ import sys
 from typing import List, Set
 
 import sqlparse
-from sqlparse.sql import Comment, Function, Identifier, Parenthesis, Statement, TokenList
+from sqlparse.sql import Comment, Comparison, Function, Identifier, Parenthesis, Statement, TokenList
 from sqlparse.tokens import Keyword, Token
 
 SOURCE_TABLE_TOKENS = (r'FROM',
@@ -95,11 +95,17 @@ Target Tables:
                     # referring https://github.com/andialbrecht/sqlparse/issues/483 for further information
                     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
+                elif isinstance(sub_token, Comparison):
+                    # create table tab1 like tab2, tab1 like tab2 will be parsed as Comparison
+                    # referring https://github.com/andialbrecht/sqlparse/issues/543 for further information
+                    assert isinstance(sub_token.left, Identifier)
+                    assert isinstance(sub_token.right, Identifier)
+                    self._target_tables.add(sub_token.left.get_real_name())
+                    self._source_tables.add(sub_token.right.get_real_name())
                 else:
                     assert isinstance(sub_token, Identifier)
                     self._target_tables.add(sub_token.get_real_name())
-                    target_table_token_flag = False
+                target_table_token_flag = False
             elif temp_table_token_flag:
                 if self.__token_negligible_before_tablename(sub_token):
                     continue


Test patch:
diff --git a/tests/test_others.py b/tests/test_others.py
index eabb6d4..a9405c8 100644
--- a/tests/test_others.py
+++ b/tests/test_others.py
@@ -14,6 +14,14 @@ def test_create_if_not_exist():
     helper("CREATE TABLE IF NOT EXISTS tab1 (col1 STRING)", None, {"tab1"})
 
 
+def test_create_as():
+    helper("CREATE TABLE tab1 AS SELECT * FROM tab2", {"tab2"}, {"tab1"})
+
+
+def test_create_like():
+    helper("CREATE TABLE tab1 LIKE tab2", {"tab2"}, {"tab1"})
+
+
 def test_create_after_drop():
     helper("DROP TABLE IF EXISTS tab1; CREATE TABLE IF NOT EXISTS tab1 (col1 STRING)", None, {"tab1"})