Skip to content

Commit

Permalink
fix: subquery alias and column qualifier case inconsistency (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
reata committed Dec 26, 2023
1 parent 4ac7fde commit 8b37464
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
10 changes: 9 additions & 1 deletion sqllineage/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,15 @@ def __init__(self, name: str, **kwargs):
"""
self._parent: Set[Union[Path, Table, SubQuery]] = set()
self.raw_name = escape_identifier_name(name)
self.source_columns = kwargs.pop("source_columns", ((self.raw_name, None),))
self.source_columns = (
(
escape_identifier_name(raw_name),
escape_identifier_name(qualifier) if qualifier is not None else None,
)
for raw_name, qualifier in kwargs.pop(
"source_columns", ((self.raw_name, None),)
)
)

def __str__(self):
return (
Expand Down
3 changes: 1 addition & 2 deletions sqllineage/core/parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def end_of_query_cleanup(self, holder: SubQueryLineageHolder) -> None:
if len(holder.write) > 1:
raise SQLLineageException
tgt_tbl = list(holder.write)[0]
for idx in range(len(col_grp)):
tgt_col = col_grp[idx]
for idx, tgt_col in enumerate(col_grp):
tgt_col.parent = tgt_tbl
for src_col in tgt_col.to_source_columns(
self.get_alias_mapping_from_table_group(tbl_grp, holder)
Expand Down
16 changes: 16 additions & 0 deletions tests/sql/column/test_column_select_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,19 @@ def test_comment_after_column_comma_last():
),
],
)


def test_select_column_name_case_insensitive():
sql = """INSERT INTO tab1
SELECT col1,
COL1
FROM tab2"""
assert_column_lineage_equal(
sql,
[
(
ColumnQualifierTuple("col1", "tab2"),
ColumnQualifierTuple("col1", "tab1"),
)
],
)
17 changes: 17 additions & 0 deletions tests/sql/column/test_column_select_from_subquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,20 @@ def test_window_function_in_subquery():
(ColumnQualifierTuple("col2", "tab2"), ColumnQualifierTuple("rn", "tab1")),
],
)


def test_select_column_in_subquery_alias_and_qualifier_case_insensitive():
sql = """INSERT INTO tab1
SELECT DT.col1
FROM (SELECT col1 FROM tab2) dt"""
assert_column_lineage_equal(
sql,
[(ColumnQualifierTuple("col1", "tab2"), ColumnQualifierTuple("col1", "tab1"))],
)
sql = """INSERT INTO tab1
SELECT dt.col1
FROM (SELECT col1 FROM tab2) DT"""
assert_column_lineage_equal(
sql,
[(ColumnQualifierTuple("col1", "tab2"), ColumnQualifierTuple("col1", "tab1"))],
)

0 comments on commit 8b37464

Please sign in to comment.