Repository: reata/sqllineage
Pull request #413 (2023-07-22 03:45:25)

Resolve test_create_using_serde with sqlfluff
Resolve test_create_using_serde in #347 

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/reata/sqllineage/pull/413?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#413](https://app.codecov.io/gh/reata/sqllineage/pull/413?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (ea3b9ad) into [master](https://app.codecov.io/gh/reata/sqllineage/commit/1cbd35ef083bb1a9d086c6a60a9187a36da91d35?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (1cbd35e) will **not change** coverage.
> The diff coverage is `100.00%`.

```diff
@@           Coverage Diff           @@
##           master     #413   +/-   ##
=======================================
  Coverage   99.40%   99.40%           
=======================================
  Files          42       42           
  Lines        2013     2013           
=======================================
  Hits         2001     2001           
  Misses         12       12           
```


| [Impacted Files](https://app.codecov.io/gh/reata/sqllineage/pull/413?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [sqllineage/core/models.py](https://app.codecov.io/gh/reata/sqllineage/pull/413?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3FsbGluZWFnZS9jb3JlL21vZGVscy5weQ==) | `100.00% <100.00%> (ø)` | |
| [sqllineage/core/parser/sqlparse/handlers/cte.py](https://app.codecov.io/gh/reata/sqllineage/pull/413?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3FsbGluZWFnZS9jb3JlL3BhcnNlci9zcWxwYXJzZS9oYW5kbGVycy9jdGUucHk=) | `100.00% <100.00%> (ø)` | |



Patch:
diff --git a/sqllineage/core/models.py b/sqllineage/core/models.py
index c5eb25f..41c0d34 100644
--- a/sqllineage/core/models.py
+++ b/sqllineage/core/models.py
@@ -189,7 +189,7 @@ class Column:
 
         def _to_src_col(
             name: str, parent: Optional[Union[Path, Table, SubQuery]] = None
-        ):
+        ) -> Column:
             col = Column(name)
             if parent:
                 col.parent = parent
@@ -204,12 +204,12 @@ class Column:
                         source_columns.add(_to_src_col(src_col, table))
                 else:
                     # select unqualified column
-                    src_col = _to_src_col(src_col, None)
+                    source = _to_src_col(src_col, None)
                     for table in set(alias_mapping.values()):
                         # in case of only one table, we get the right answer
                         # in case of multiple tables, a bunch of possible tables are set
-                        src_col.parent = table
-                    source_columns.add(src_col)
+                        source.parent = table
+                    source_columns.add(source)
             else:
                 if alias_mapping.get(qualifier):
                     source_columns.add(
diff --git a/sqllineage/core/parser/sqlparse/handlers/cte.py b/sqllineage/core/parser/sqlparse/handlers/cte.py
index 22785cc..11efc27 100644
--- a/sqllineage/core/parser/sqlparse/handlers/cte.py
+++ b/sqllineage/core/parser/sqlparse/handlers/cte.py
@@ -28,6 +28,6 @@ class CTEHandler(NextTokenBaseHandler):
             cte = []
         for token in cte:
             sublist = list(token.get_sublists())
-            if sublist:
+            if sublist and not (isinstance(sublist[0], Function)):
                 # CTE: tbl AS (SELECT 1), tbl is alias and (SELECT 1) is subquery Parenthesis
                 holder.add_cte(SqlParseSubQuery.of(sublist[0], token.get_real_name()))


Test patch:
diff --git a/tests/test_columns.py b/tests/test_columns.py
index ebe7e21..1ef36d5 100644
--- a/tests/test_columns.py
+++ b/tests/test_columns.py
@@ -94,6 +94,9 @@ FROM tab2"""
             (ColumnQualifierTuple("*", "tab2"), ColumnQualifierTuple("cnt", "tab1")),
         ],
     )
+
+
+def test_select_column_using_cast():
     sql = """INSERT INTO tab1
 SELECT cast(col1 AS timestamp)
 FROM tab2"""
diff --git a/tests/test_others_dialect_specific.py b/tests/test_others_dialect_specific.py
index 1d8c00e..322f804 100644
--- a/tests/test_others_dialect_specific.py
+++ b/tests/test_others_dialect_specific.py
@@ -40,20 +40,19 @@ FROM foo""",
     )
 
 
-def test_create_using_serde():
+@pytest.mark.parametrize("dialect", ["databricks", "hive", "sparksql"])
+def test_create_using_serde(dialect: str):
     """
     https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-RowFormats&SerDe
     here with is not an indicator for CTE
-    FIXME: sqlfluff hive dialect doesn't support parsing this yet
     """
-    # Check
-    #
+    # use raw sql string to avoid Python recognize some character as escaping
     assert_table_lineage_equal(
-        """CREATE TABLE apachelog (
+        r"""CREATE TABLE apachelog (
   host STRING,
   identity STRING,
-  user STRING,
-  time STRING,
+  `user` STRING,
+  `time` STRING,
   request STRING,
   status STRING,
   size STRING,
@@ -66,7 +65,7 @@ WITH SERDEPROPERTIES (
 STORED AS TEXTFILE""",  # noqa
         None,
         {"apachelog"},
-        test_sqlfluff=False,
+        dialect=dialect,
     )