Skip to content

Commit 518ae77

Browse files
authored
Fix: parse python model column types with dialect (#3633)
1 parent 209130c commit 518ae77

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

sqlmesh/core/model/decorator.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ def __init__(self, name: t.Optional[str] = None, is_sql: bool = False, **kwargs:
7171
column_name: (
7272
column_type
7373
if isinstance(column_type, exp.DataType)
74-
else exp.DataType.build(str(column_type))
74+
else exp.DataType.build(
75+
str(column_type), dialect=self.kwargs.get("dialect", self._dialect)
76+
)
7577
)
7678
for column_name, column_type in self.kwargs.pop("columns", {}).items()
7779
}

tests/core/test_model.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import pytest
1111
from pytest_mock.plugin import MockerFixture
1212
from sqlglot import exp, parse_one
13+
from sqlglot.errors import ParseError
1314
from sqlglot.schema import MappingSchema
1415
from sqlmesh.cli.example_project import init_example_project
1516

@@ -5310,6 +5311,27 @@ def test(context, **kwargs):
53105311
assert m.time_column.column.sql() == '"Y"'
53115312
assert m.time_column.format == "%Y-%m-%d"
53125313

5314+
# column type parseable by default dialect: no error
5315+
model._dialect = "clickhouse"
5316+
5317+
@model("good", columns={'"COL"': "DateTime64(9)"})
5318+
def a_model(context):
5319+
pass
5320+
5321+
# column type not parseable by default dialect and no explicit dialect: error
5322+
model._dialect = "snowflake"
5323+
5324+
with pytest.raises(ParseError, match="No expression was parsed from 'DateTime64\(9\)'"):
5325+
5326+
@model("bad", columns={'"COL"': "DateTime64(9)"})
5327+
def a_model(context):
5328+
pass
5329+
5330+
# column type not parseable by default dialect and explicit dialect specified: no error
5331+
@model("good", columns={'"COL"': "DateTime64(9)"}, dialect="clickhouse")
5332+
def a_model(context):
5333+
pass
5334+
53135335
model._dialect = None
53145336

53155337

0 commit comments

Comments
 (0)