Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: consistent lineage results #661

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions sqllineage/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,8 @@ def _to_src_col(
)
else:
source_columns.add(_to_src_col(src_col, Table(qualifier)))

# this function returns the best guess when resolving wildcards.
# we sort the resolved columns so results are at least consistent.
source_columns = sorted(source_columns, key=lambda x: str(x))
return source_columns
4 changes: 2 additions & 2 deletions sqllineage/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def intermediate_tables(self) -> List[Table]:
@lazy_method
def get_column_lineage(
self, exclude_path_ending_in_subquery=True, exclude_subquery_columns=False
) -> List[Tuple[Column, Column]]:
) -> List[Tuple[Column, ...]]:
"""
a list of column tuple :class:`sqllineage.models.Column`
"""
Expand All @@ -163,7 +163,7 @@ def get_column_lineage(
self._sql_holder.get_column_lineage(
exclude_path_ending_in_subquery, exclude_subquery_columns
),
key=lambda x: (str(x[-1]), str(x[0])),
key=lambda x: "".join([str(i) for i in reversed(x)]),
)

def print_column_lineage(self) -> None:
Expand Down
50 changes: 50 additions & 0 deletions tests/sql/column/test_consistency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import pytest

from sqllineage.core.metadata_provider import MetaDataProvider
from sqllineage.utils.entities import ColumnQualifierTuple
from ...helpers import assert_column_lineage_equal, generate_metadata_providers


providers = generate_metadata_providers(
{
"database_a.table_a": ["col_a", "col_b", "col_c"],
}
)


@pytest.mark.parametrize("provider", providers)
def test_ouput_consistency(provider: MetaDataProvider):
sql = """CREATE TABLE database_b.table_c
AS (
SELECT
*,
1 AS event_time
FROM (
SELECT
table_b.col_b AS col_a
FROM database_b.table_b AS table_b
JOIN database_a.table_a AS table_d
) AS base
)
"""
assert_column_lineage_equal(
sql,
[
(
ColumnQualifierTuple("col_b", "database_a.table_a"),
ColumnQualifierTuple("col_b", "database_b.table_c"),
),
(
ColumnQualifierTuple("col_c", "database_a.table_a"),
ColumnQualifierTuple("col_c", "database_b.table_c"),
),
(
ColumnQualifierTuple("col_b", "database_b.table_b"),
ColumnQualifierTuple("col_a", "database_b.table_c"),
),
],
dialect="athena",
test_sqlparse=False,
test_sqlfluff=True,
metadata_provider=provider,
)