Skip to content

Commit 7007f25

Browse files
authored
fix: cast to varchar in non-validating dialect (#420)
1 parent e72aa11 commit 7007f25

8 files changed

+40
-9
lines changed

sqllineage/core/parser/sqlparse/models.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def _extract_source_columns(token: Token) -> List[ColumnQualifierTuple]:
152152
elif isinstance(token, Identifier):
153153
real_name = token.get_real_name()
154154
# ignore function dtypes that don't need to check for extract column
155-
FUNC_DTYPE = ["decimal", "numeric"]
155+
FUNC_DTYPE = ["decimal", "numeric", "varchar"]
156156
has_function = any(
157157
isinstance(t, Function) and t.get_real_name() not in FUNC_DTYPE
158158
for t in token.tokens

tests/sql/column/test_column_select_cast.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ def test_cast_with_comparison():
4949
)
5050

5151

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

6264

63-
@pytest.mark.parametrize("dtype", ["string", "timestamp", "date", "decimal(18, 0)"])
65+
@pytest.mark.parametrize(
66+
"dtype", ["string", "timestamp", "date", "decimal(18, 0)", "varchar(255)"]
67+
)
6468
def test_nested_cast_to_data_type(dtype: str):
6569
sql = f"""INSERT INTO tab1
6670
SELECT cast(cast(col1 AS {dtype}) AS {dtype}) AS col1
@@ -78,7 +82,9 @@ def test_nested_cast_to_data_type(dtype: str):
7882
)
7983

8084

81-
@pytest.mark.parametrize("dtype", ["string", "timestamp", "date", "decimal(18, 0)"])
85+
@pytest.mark.parametrize(
86+
"dtype", ["string", "timestamp", "date", "decimal(18, 0)", "varchar(255)"]
87+
)
8288
def test_cast_to_data_type_with_case_when(dtype: str):
8389
sql = f"""INSERT INTO tab1
8490
SELECT cast(case when col1 > 0 then col2 else col3 end as {dtype}) AS col1

tests/sql/table/sqlfluff_only/test_dialect_specific.py renamed to tests/sql/sqlfluff_only/test_dialect_specific.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22

33
from sqllineage.utils.entities import ColumnQualifierTuple
4-
from ....helpers import assert_column_lineage_equal, assert_table_lineage_equal
4+
from ...helpers import assert_column_lineage_equal, assert_table_lineage_equal
55

66

77
@pytest.mark.parametrize("dialect", ["tsql"])

tests/sql/table/sqlfluff_only/test_dml_specify_column.py renamed to tests/sql/sqlfluff_only/test_dml_specify_column.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
from sqllineage.utils.entities import ColumnQualifierTuple
6-
from ....helpers import assert_column_lineage_equal, assert_table_lineage_equal
6+
from ...helpers import assert_column_lineage_equal, assert_table_lineage_equal
77

88

99
def test_view_with_subquery_custom_columns():

tests/sql/table/sqlfluff_only/test_keyword_as_identifier.py renamed to tests/sql/sqlfluff_only/test_keyword_as_identifier.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
from sqllineage.utils.entities import ColumnQualifierTuple
7-
from ....helpers import assert_column_lineage_equal, assert_table_lineage_equal
7+
from ...helpers import assert_column_lineage_equal, assert_table_lineage_equal
88

99

1010
def test_non_reserved_keyword_as_source():
@@ -71,3 +71,28 @@ def test_current_timestamp():
7171
],
7272
test_sqlparse=False,
7373
)
74+
75+
76+
def test_coalesce_with_whitespace():
77+
"""
78+
coalesce is a keyword since ANSI SQL-2023
79+
usually it's parsed as a function. however, when whitespace followed which is valid syntax,
80+
sqlparse cannot produce the correct AST
81+
"""
82+
sql = """INSERT INTO tab1
83+
SELECT coalesce (col1, col2) as col3
84+
FROM tab2"""
85+
assert_column_lineage_equal(
86+
sql,
87+
[
88+
(
89+
ColumnQualifierTuple("col1", "tab2"),
90+
ColumnQualifierTuple("col3", "tab1"),
91+
),
92+
(
93+
ColumnQualifierTuple("col2", "tab2"),
94+
ColumnQualifierTuple("col3", "tab1"),
95+
),
96+
],
97+
test_sqlparse=False,
98+
)

tests/sql/table/sqlfluff_only/test_parenthesized_expression.py renamed to tests/sql/sqlfluff_only/test_parenthesized_expression.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from sqllineage.utils.entities import ColumnQualifierTuple
2-
from ....helpers import assert_column_lineage_equal
2+
from ...helpers import assert_column_lineage_equal
33

44

55
def test_subquery_expression_without_source_table():

tests/sql/table/sqlfluff_only/test_parenthesized_query.py renamed to tests/sql/sqlfluff_only/test_parenthesized_query.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66

77
from sqllineage.utils.entities import ColumnQualifierTuple
8-
from ....helpers import assert_column_lineage_equal, assert_table_lineage_equal
8+
from ...helpers import assert_column_lineage_equal, assert_table_lineage_equal
99

1010

1111
def test_insert_into_qualified_table_with_parenthesized_query():

0 commit comments

Comments
 (0)