22
33import typing as t
44
5+ import agate
6+ from dbt_common .clients import agate_helper
7+ from sqlglot import exp
8+
59from sqlmesh .core .model import Model , SeedKind , create_seed_model
610from sqlmesh .dbt .basemodel import BaseModelConfig
711
@@ -18,11 +22,51 @@ class SeedConfig(BaseModelConfig):
1822 General propreties, General configs, and For seeds sections.
1923 """
2024
25+ delimiter : str = ","
26+
2127 def to_sqlmesh (self , context : DbtContext ) -> Model :
2228 """Converts the dbt seed into a SQLMesh model."""
29+ seed_path = self .path .absolute ().as_posix ()
30+ kwargs = self .sqlmesh_model_kwargs (context )
31+ if kwargs .get ("columns" ) is None :
32+ agate_table = agate_helper .from_csv (seed_path , [], delimiter = self .delimiter )
33+ kwargs ["columns" ] = {
34+ name : AGATE_TYPE_MAPPING [tpe .__class__ ]
35+ for name , tpe in zip (agate_table .column_names , agate_table .column_types )
36+ }
37+
2338 return create_seed_model (
2439 self .canonical_name (context ),
25- SeedKind (path = self . path . absolute (). as_posix () ),
40+ SeedKind (path = seed_path ),
2641 dialect = context .dialect ,
27- ** self . sqlmesh_model_kwargs ( context ) ,
42+ ** kwargs ,
2843 )
44+
45+
46+ class Integer (agate .data_types .DataType ):
47+ def cast (self , d : str ) -> t .Optional [int ]:
48+ if d is None :
49+ return d
50+ try :
51+ return int (d )
52+ except ValueError :
53+ raise agate .exceptions .CastError ('Can not parse value "%s" as Integer.' % d )
54+
55+ def jsonify (self , d : str ) -> str :
56+ return d
57+
58+
59+ # The dbt version has a bug in which they check whether the type of the input value
60+ # is int, while the input value is actually always a string.
61+ agate_helper .Integer = Integer # type: ignore
62+
63+
64+ AGATE_TYPE_MAPPING = {
65+ agate_helper .Integer : exp .DataType .build ("int" ),
66+ agate_helper .Number : exp .DataType .build ("double" ),
67+ agate_helper .ISODateTime : exp .DataType .build ("datetime" ),
68+ agate .Date : exp .DataType .build ("date" ),
69+ agate .DateTime : exp .DataType .build ("datetime" ),
70+ agate .Boolean : exp .DataType .build ("boolean" ),
71+ agate .Text : exp .DataType .build ("text" ),
72+ }
0 commit comments