11import typing as t
22from textwrap import dedent
33
4+ import dateutil
45import sqlalchemy as sa
56from attr import Factory
67from attrs import define
@@ -74,6 +75,7 @@ class TypeMap:
7475 # CrateDB can not store `DATE` types, so converge to `TIMESTAMP`.
7576 DataType .NAIVE_DATE : sa .TIMESTAMP (),
7677 DataType .NAIVE_DATETIME : sa .TIMESTAMP (),
78+ DataType .NAIVE_TIME : sa .TIMESTAMP (),
7779 DataType .UTC_DATETIME : sa .TIMESTAMP (),
7880 # TODO: The parameters are coming from the API, here `input_fivetran.json`.
7981 # How to loop them into this type resolution machinery?
@@ -82,7 +84,6 @@ class TypeMap:
8284 DataType .STRING : sa .String (),
8385 DataType .JSON : ObjectTypeImpl (),
8486 DataType .XML : sa .String (),
85- DataType .NAIVE_TIME : sa .TIMESTAMP (),
8687 }
8788
8889 cratedb_map = {
@@ -109,6 +110,8 @@ class TypeMap:
109110 # sa.DateTime: DataType.NAIVE_DATETIME,
110111 sa .DateTime : DataType .UTC_DATETIME ,
111112 sa .DATETIME : DataType .UTC_DATETIME ,
113+ sa .Time : DataType .NAIVE_TIME ,
114+ sa .TIME : DataType .NAIVE_TIME ,
112115 sa .Numeric : DataType .DECIMAL ,
113116 sa .DECIMAL : DataType .DECIMAL ,
114117 sa .LargeBinary : DataType .BINARY ,
@@ -156,6 +159,27 @@ def replace_values(cls, record):
156159 record .pop (rm )
157160
158161
162+ class CrateDBKnowledge :
163+ """
164+ Manage special knowledge about CrateDB.
165+
166+ CrateDB can't store values of the `TIME` type, so we selected to store it as `DATETIME`
167+ This routine converges the value.
168+ """
169+
170+ @classmethod
171+ def replace_values (cls , request , record ):
172+ for column in request .table .columns :
173+ if column .type == common_pb2 .DataType .NAIVE_TIME and column .name in record :
174+ obj = dateutil .parser .parse (record [column .name ])
175+ obj = obj .replace (year = 1970 , month = 1 , day = 1 )
176+ # Calculate milliseconds since midnight (timezone-independent).
177+ ms = (
178+ obj .hour * 3600 + obj .minute * 60 + obj .second
179+ ) * 1000 + obj .microsecond // 1000
180+ record [column .name ] = str (ms )
181+
182+
159183@define
160184class TableInfo :
161185 """
0 commit comments