Repository: reata/sqllineage
Pull request #433 (2023-08-20 11:34:50)

fix: exception for parenthesized where clause
Cloese #426 

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/reata/sqllineage/pull/433?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#433](https://app.codecov.io/gh/reata/sqllineage/pull/433?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (b32c7da) into [master](https://app.codecov.io/gh/reata/sqllineage/commit/fe916f4b4ed5e14558e0cebda2857c3a10bd0475?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (fe916f4) will **increase** coverage by `0.00%`.
> The diff coverage is `100.00%`.

```diff
@@           Coverage Diff           @@
##           master     #433   +/-   ##
=======================================
  Coverage   99.54%   99.54%           
=======================================
  Files          41       41           
  Lines        1991     1992    +1     
=======================================
+ Hits         1982     1983    +1     
  Misses          9        9           
```


| [Files Changed](https://app.codecov.io/gh/reata/sqllineage/pull/433?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [sqllineage/core/parser/sqlfluff/utils.py](https://app.codecov.io/gh/reata/sqllineage/pull/433?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3FsbGluZWFnZS9jb3JlL3BhcnNlci9zcWxmbHVmZi91dGlscy5weQ==) | `100.00% <100.00%> (ø)` | |



Patch:
diff --git a/sqllineage/core/parser/sqlfluff/utils.py b/sqllineage/core/parser/sqlfluff/utils.py
index 8957494..bb46f24 100644
--- a/sqllineage/core/parser/sqlfluff/utils.py
+++ b/sqllineage/core/parser/sqlfluff/utils.py
@@ -169,22 +169,29 @@ def list_subqueries(segment: BaseSegment) -> List[SubQueryTuple]:
                 )
             ]
     elif segment.type == "where_clause":
-        expression_segments = segment.get_child("expression").segments or []
-        bracketed_segments = [s for s in expression_segments if s.type == "bracketed"]
-        if bracketed_segments and is_subquery(bracketed_segments[0]):
-            return [
-                SubQueryTuple(extract_innermost_bracketed(bracketed_segments[0]), None)
-            ]
+        bracketeds = []
+        if expression := segment.get_child("expression"):
+            bracketeds = expression.get_children("bracketed")
+        elif bracketed_where := segment.get_child("bracketed"):
+            if expression := bracketed_where.get_child("expression"):
+                bracketeds = expression.get_children("bracketed")
+        subquery = [
+            SubQueryTuple(extract_innermost_bracketed(bracketed), None)
+            for bracketed in bracketeds
+            if is_subquery(bracketed)
+        ]
     elif segment.type in ["from_clause", "from_expression"]:
         if from_expression_element := find_from_expression_element(segment):
-            subquery += list_subqueries(from_expression_element)
+            subquery = list_subqueries(from_expression_element)
         for join_clause in list_join_clause(segment):
             if from_expression_element := find_from_expression_element(join_clause):
                 subquery += list_subqueries(from_expression_element)
     elif is_set_expression(segment):
-        for s in list_child_segments(segment, check_bracketed=True):
-            if s.type == "bracketed" or s.type == "select_statement":
-                subquery.append(SubQueryTuple(s, None))
+        subquery = [
+            SubQueryTuple(s, None)
+            for s in list_child_segments(segment, check_bracketed=True)
+            if s.type in ("bracketed", "select_statement")
+        ]
     return subquery
 
 


Test patch:
diff --git a/tests/sql/table/test_select.py b/tests/sql/table/test_select.py
index c800e0c..2a0b90c 100644
--- a/tests/sql/table/test_select.py
+++ b/tests/sql/table/test_select.py
@@ -49,6 +49,12 @@ def test_select_with_where():
     )
 
 
+def test_select_with_parenthesized_where():
+    assert_table_lineage_equal(
+        "SELECT * FROM tab1 WHERE (col1 > val1 AND col2 = 'val2')", {"tab1"}
+    )
+
+
 def test_select_with_comment():
     assert_table_lineage_equal("SELECT -- comment1\n col1 FROM tab1", {"tab1"})