3434from sqlmesh .core .model .seed import CsvSeedReader , Seed , create_seed
3535from sqlmesh .core .renderer import ExpressionRenderer , QueryRenderer
3636from sqlmesh .utils import str_to_bool
37- from sqlmesh .utils .date import TimeLike , make_inclusive , to_datetime
37+ from sqlmesh .utils .date import TimeLike , make_inclusive , to_datetime , to_ds , to_ts
3838from sqlmesh .utils .errors import ConfigError , SQLMeshError , raise_config_error
3939from sqlmesh .utils .hashing import hash_data
4040from sqlmesh .utils .jinja import JinjaMacroRegistry , extract_macro_references
@@ -514,9 +514,6 @@ def convert_to_time_column(
514514 ) -> exp .Expression :
515515 """Convert a TimeLike object to the same time format and type as the model's time column."""
516516 if self .time_column :
517- if self .time_column .format :
518- time = to_datetime (time ).strftime (self .time_column .format )
519-
520517 if columns_to_types is None :
521518 columns_to_types = self .columns_to_types_or_raise
522519
@@ -526,12 +523,18 @@ def convert_to_time_column(
526523 )
527524
528525 time_column_type = columns_to_types [self .time_column .column ]
526+
527+ if time_column_type .is_type (exp .DataType .Type .DATE ):
528+ return exp .cast (exp .Literal .string (to_ds (time )), to = "date" )
529+ if time_column_type .this in exp .DataType .TEMPORAL_TYPES :
530+ return exp .cast (exp .Literal .string (to_ts (time )), to = time_column_type .this )
531+
532+ if self .time_column .format :
533+ time = to_datetime (time ).strftime (self .time_column .format )
529534 if time_column_type .this in exp .DataType .TEXT_TYPES :
530535 return exp .Literal .string (time )
531- elif time_column_type .this in exp .DataType .NUMERIC_TYPES :
536+ if time_column_type .this in exp .DataType .NUMERIC_TYPES :
532537 return exp .Literal .number (time )
533- elif time_column_type .this in exp .DataType .TEMPORAL_TYPES :
534- return exp .cast (exp .Literal .string (time ), time_column_type )
535538 return exp .convert (time )
536539
537540 def update_schema (
0 commit comments