forked from Mause/duckdb_engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatatypes.py
284 lines (210 loc) · 6.44 KB
/
datatypes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"""
See https://duckdb.org/docs/sql/data_types/numeric for more information
Also
```sql
select * from duckdb_types where type_category = 'NUMERIC';
```
"""
import typing
from typing import Any, Callable, Dict, Optional, Type
import duckdb
from packaging.version import Version
from sqlalchemy import exc
from sqlalchemy.dialects.postgresql.base import PGIdentifierPreparer, PGTypeCompiler
from sqlalchemy.engine import Dialect
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql import sqltypes, type_api
from sqlalchemy.sql.type_api import TypeEngine
from sqlalchemy.types import BigInteger, Integer, SmallInteger, String
# INTEGER INT4, INT, SIGNED -2147483648 2147483647
# SMALLINT INT2, SHORT -32768 32767
# BIGINT INT8, LONG -9223372036854775808 9223372036854775807
(BigInteger, SmallInteger) # pure reexport
duckdb_version = duckdb.__version__
IS_GT_1 = Version(duckdb_version) > Version("1.0.0")
class UInt64(Integer):
pass
class UInt32(Integer):
pass
class UInt16(Integer):
"AKA USMALLINT"
class UInt8(Integer):
pass
class UTinyInteger(Integer):
"AKA UInt1"
name = "UTinyInt"
# UTINYINT - 0 255
class TinyInteger(Integer):
"AKA Int1"
name = "TinyInt"
# TINYINT INT1 -128 127
class USmallInteger(Integer):
name = "usmallint"
"AKA UInt2"
# USMALLINT - 0 65535
min = 0
max = 2**15
class UBigInteger(Integer):
name = "UBigInt"
min = 0
max = 18446744073709551615
class HugeInteger(Integer):
name = "HugeInt"
# HUGEINT -170141183460469231731687303715884105727* 170141183460469231731687303715884105727
class UHugeInteger(Integer):
name = "UHugeInt"
class UInteger(Integer):
# UINTEGER - 0 4294967295
pass
if IS_GT_1:
class VarInt(Integer):
pass
def compile_uint(element: Integer, compiler: PGTypeCompiler, **kw: Any) -> str:
return getattr(element, "name", type(element).__name__)
types = [
subclass
for subclass in Integer.__subclasses__()
if subclass.__module__ == UInt64.__module__
]
assert types
TV = typing.Union[Type[TypeEngine], TypeEngine]
class Struct(TypeEngine):
"""
Represents a STRUCT type in DuckDB
```python
from duckdb_engine.datatypes import Struct
from sqlalchemy import Table, Column, String
Table(
'hello',
Column('name', Struct({'first': String, 'last': String})
)
```
:param fields: only optional due to limitations with how much type information DuckDB returns to us in the description field
"""
__visit_name__ = "struct"
def __init__(self, fields: Optional[Dict[str, TV]] = None):
self.fields = fields
class Map(TypeEngine):
"""
Represents a MAP type in DuckDB
```python
from duckdb_engine.datatypes import Map
from sqlalchemy import Table, Column, String
Table(
'hello',
Column('name', Map(String, String)
)
```
"""
__visit_name__ = "map"
key_type: TV
value_type: TV
def __init__(self, key_type: TV, value_type: TV):
self.key_type = key_type
self.value_type = value_type
def bind_processor(
self, dialect: Dialect
) -> Optional[Callable[[Optional[dict]], Optional[dict]]]:
return lambda value: (
{"key": list(value), "value": list(value.values())} if value else None
)
def result_processor(
self, dialect: Dialect, coltype: str
) -> Optional[Callable[[Optional[dict]], Optional[dict]]]:
if IS_GT_1:
return lambda value: value
else:
return (
lambda value: dict(zip(value["key"], value["value"])) if value else {}
)
class Union(TypeEngine):
"""
Represents a UNION type in DuckDB
```python
from duckdb_engine.datatypes import Union
from sqlalchemy import Table, Column, String
Table(
'hello',
Column('name', Union({"name": String, "age": String})
)
```
"""
__visit_name__ = "union"
fields: Dict[str, TV]
def __init__(self, fields: Dict[str, TV]):
self.fields = fields
ISCHEMA_NAMES = {
"hugeint": HugeInteger,
"uhugeint": UHugeInteger,
"tinyint": TinyInteger,
"utinyint": UTinyInteger,
"int8": BigInteger,
"int4": Integer,
"int2": SmallInteger,
"timetz": sqltypes.TIME,
"timestamptz": sqltypes.TIMESTAMP,
"float4": sqltypes.FLOAT,
"float8": sqltypes.FLOAT,
"usmallint": USmallInteger,
"uinteger": UInteger,
"ubigint": UBigInteger,
"timestamp_s": sqltypes.TIMESTAMP,
"timestamp_ms": sqltypes.TIMESTAMP,
"timestamp_ns": sqltypes.TIMESTAMP,
"enum": sqltypes.Enum,
"bool": sqltypes.BOOLEAN,
"varchar": String,
}
if IS_GT_1:
ISCHEMA_NAMES["varint"] = VarInt
def register_extension_types() -> None:
for subclass in types:
compiles(subclass, "duckdb")(compile_uint)
@compiles(Struct, "duckdb") # type: ignore[misc]
def visit_struct(
instance: Struct,
compiler: PGTypeCompiler,
identifier_preparer: PGIdentifierPreparer,
**kw: Any,
) -> str:
return "STRUCT" + struct_or_union(instance, compiler, identifier_preparer, **kw)
@compiles(Union, "duckdb") # type: ignore[misc]
def visit_union(
instance: Union,
compiler: PGTypeCompiler,
identifier_preparer: PGIdentifierPreparer,
**kw: Any,
) -> str:
return "UNION" + struct_or_union(instance, compiler, identifier_preparer, **kw)
def struct_or_union(
instance: typing.Union[Union, Struct],
compiler: PGTypeCompiler,
identifier_preparer: PGIdentifierPreparer,
**kw: Any,
) -> str:
fields = instance.fields
if fields is None:
raise exc.CompileError(f"DuckDB {repr(instance)} type requires fields")
return "({})".format(
", ".join(
"{} {}".format(
identifier_preparer.quote_identifier(key),
process_type(
value, compiler, identifier_preparer=identifier_preparer, **kw
),
)
for key, value in fields.items()
)
)
def process_type(
value: typing.Union[TypeEngine, Type[TypeEngine]],
compiler: PGTypeCompiler,
**kw: Any,
) -> str:
return compiler.process(type_api.to_instance(value), **kw)
@compiles(Map, "duckdb") # type: ignore[misc]
def visit_map(instance: Map, compiler: PGTypeCompiler, **kw: Any) -> str:
return "MAP({}, {})".format(
process_type(instance.key_type, compiler, **kw),
process_type(instance.value_type, compiler, **kw),
)