@@ -102,7 +102,11 @@ def sql(self) -> str:
102102 return "null"
103103 assert self .value
104104
105- data = self .value .base64 if isinstance (self .value , ValueValue_base64 ) else self .value
105+ data = (
106+ self .value .base64
107+ if isinstance (self .value , ValueValue_base64 )
108+ else self .value
109+ )
106110
107111 if not data or len (data ) < 2 :
108112 return ""
@@ -421,7 +425,9 @@ def sql(self) -> str:
421425
422426 return (
423427 "TIME '"
424- + time (hours , minutes , seconds , microsecond = t .microseconds ).strftime ("%H:%M:%S.%f" )
428+ + time (hours , minutes , seconds , microsecond = t .microseconds ).strftime (
429+ "%H:%M:%S.%f"
430+ )
425431 + "'"
426432 )
427433
@@ -449,7 +455,9 @@ def sql(self) -> str:
449455
450456 return (
451457 "TIMETZ '"
452- + time (hours , minutes , seconds , microsecond = t .microseconds ).strftime ("%H:%M:%S.%f" )
458+ + time (hours , minutes , seconds , microsecond = t .microseconds ).strftime (
459+ "%H:%M:%S.%f"
460+ )
453461 + "'"
454462 )
455463
@@ -542,8 +550,13 @@ class Value_timestamp_ns(ValueBase):
542550 def sql (self ) -> str :
543551 if self .is_null :
544552 return "null"
553+ assert self .value
554+ ns_since_epoch = self .value
555+ seconds , nanoseconds = divmod (ns_since_epoch , 10 ** 9 )
545556
546- return f"make_timestamp_ns({ self .value } ::bigint)"
557+ dt = datetime .fromtimestamp (seconds , tz = UTC )
558+ formatted = dt .strftime ("%Y-%m-%dT%H:%M:%S" ) + f".{ nanoseconds :09d} "
559+ return f"TIMESTAMP_NS '{ formatted } '"
547560
548561
549562class ValueType_timestamp_s (BaseModel ):
@@ -841,7 +854,10 @@ def sql(self) -> str:
841854 return (
842855 "STRUCT("
843856 + "," .join (
844- [f'"{ child .first } " { child .second .sql ()} ' for child in self .type_info .child_types ]
857+ [
858+ f'"{ child .first } " { child .second .sql ()} '
859+ for child in self .type_info .child_types
860+ ]
845861 )
846862 + ")"
847863 )
@@ -861,7 +877,10 @@ def sql(self) -> str:
861877 return (
862878 "{"
863879 + "," .join (
864- [f"'{ name } ':" + value .sql () for name , value in zip (names , values , strict = True )]
880+ [
881+ f"'{ name } ':" + value .sql ()
882+ for name , value in zip (names , values , strict = True )
883+ ]
865884 )
866885 + "}"
867886 )
0 commit comments