Skip to content

Commit

Permalink
fix: cast to varchar in non-validating dialect (#420)
Browse files Browse the repository at this point in the history
  • Loading branch information
reata authored Jul 30, 2023
1 parent e72aa11 commit 7007f25
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 9 deletions.
2 changes: 1 addition & 1 deletion sqllineage/core/parser/sqlparse/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def _extract_source_columns(token: Token) -> List[ColumnQualifierTuple]:
elif isinstance(token, Identifier):
real_name = token.get_real_name()
# ignore function dtypes that don't need to check for extract column
FUNC_DTYPE = ["decimal", "numeric"]
FUNC_DTYPE = ["decimal", "numeric", "varchar"]
has_function = any(
isinstance(t, Function) and t.get_real_name() not in FUNC_DTYPE
for t in token.tokens
Expand Down
12 changes: 9 additions & 3 deletions tests/sql/column/test_column_select_cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ def test_cast_with_comparison():
)


@pytest.mark.parametrize("dtype", ["string", "timestamp", "date", "decimal(18, 0)"])
@pytest.mark.parametrize(
"dtype", ["string", "timestamp", "date", "decimal(18, 0)", "varchar(255)"]
)
def test_cast_to_data_type(dtype: str):
sql = f"""INSERT INTO tab1
SELECT cast(col1 as {dtype}) AS col1
Expand All @@ -60,7 +62,9 @@ def test_cast_to_data_type(dtype: str):
)


@pytest.mark.parametrize("dtype", ["string", "timestamp", "date", "decimal(18, 0)"])
@pytest.mark.parametrize(
"dtype", ["string", "timestamp", "date", "decimal(18, 0)", "varchar(255)"]
)
def test_nested_cast_to_data_type(dtype: str):
sql = f"""INSERT INTO tab1
SELECT cast(cast(col1 AS {dtype}) AS {dtype}) AS col1
Expand All @@ -78,7 +82,9 @@ def test_nested_cast_to_data_type(dtype: str):
)


@pytest.mark.parametrize("dtype", ["string", "timestamp", "date", "decimal(18, 0)"])
@pytest.mark.parametrize(
"dtype", ["string", "timestamp", "date", "decimal(18, 0)", "varchar(255)"]
)
def test_cast_to_data_type_with_case_when(dtype: str):
sql = f"""INSERT INTO tab1
SELECT cast(case when col1 > 0 then col2 else col3 end as {dtype}) AS col1
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from sqllineage.utils.entities import ColumnQualifierTuple
from ....helpers import assert_column_lineage_equal, assert_table_lineage_equal
from ...helpers import assert_column_lineage_equal, assert_table_lineage_equal


@pytest.mark.parametrize("dialect", ["tsql"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

from sqllineage.utils.entities import ColumnQualifierTuple
from ....helpers import assert_column_lineage_equal, assert_table_lineage_equal
from ...helpers import assert_column_lineage_equal, assert_table_lineage_equal


def test_view_with_subquery_custom_columns():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


from sqllineage.utils.entities import ColumnQualifierTuple
from ....helpers import assert_column_lineage_equal, assert_table_lineage_equal
from ...helpers import assert_column_lineage_equal, assert_table_lineage_equal


def test_non_reserved_keyword_as_source():
Expand Down Expand Up @@ -71,3 +71,28 @@ def test_current_timestamp():
],
test_sqlparse=False,
)


def test_coalesce_with_whitespace():
"""
coalesce is a keyword since ANSI SQL-2023
usually it's parsed as a function. however, when whitespace followed which is valid syntax,
sqlparse cannot produce the correct AST
"""
sql = """INSERT INTO tab1
SELECT coalesce (col1, col2) as col3
FROM tab2"""
assert_column_lineage_equal(
sql,
[
(
ColumnQualifierTuple("col1", "tab2"),
ColumnQualifierTuple("col3", "tab1"),
),
(
ColumnQualifierTuple("col2", "tab2"),
ColumnQualifierTuple("col3", "tab1"),
),
],
test_sqlparse=False,
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from sqllineage.utils.entities import ColumnQualifierTuple
from ....helpers import assert_column_lineage_equal
from ...helpers import assert_column_lineage_equal


def test_subquery_expression_without_source_table():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""

from sqllineage.utils.entities import ColumnQualifierTuple
from ....helpers import assert_column_lineage_equal, assert_table_lineage_equal
from ...helpers import assert_column_lineage_equal, assert_table_lineage_equal


def test_insert_into_qualified_table_with_parenthesized_query():
Expand Down

0 comments on commit 7007f25

Please sign in to comment.