-
-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathcommon.py
More file actions
260 lines (161 loc) · 5.84 KB
/
Copy pathcommon.py
File metadata and controls
260 lines (161 loc) · 5.84 KB
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
from typing import Type, Union
from sqlalchemy import types
from sqlalchemy.sql.functions import Function
from sqlalchemy.sql.type_api import to_instance
class ClickHouseTypeEngine(types.TypeEngine):
def compile(self, dialect=None):
from clickhouse_sqlalchemy.drivers.base import clickhouse_dialect
return super(ClickHouseTypeEngine, self).compile(
dialect=clickhouse_dialect
)
class String(types.String, ClickHouseTypeEngine):
pass
class Int(types.Integer, ClickHouseTypeEngine):
pass
class Float(types.Float, ClickHouseTypeEngine):
pass
class Boolean(types.Boolean, ClickHouseTypeEngine):
pass
class JSON(types.JSON, ClickHouseTypeEngine):
__visit_name__ = 'json'
class Array(ClickHouseTypeEngine):
__visit_name__ = 'array'
hashable = False
def __init__(self, item_type):
self.item_type = item_type
self.item_type_impl = to_instance(item_type)
super(Array, self).__init__()
def __repr__(self):
nested_type_str = \
f'{self.item_type_impl.__module__}.{self.item_type_impl!r}'
return f'Array({nested_type_str})'
@property
def python_type(self):
return list
def literal_processor(self, dialect):
item_processor = self.item_type_impl.literal_processor(dialect)
def process(value):
processed_value = []
for x in value:
if item_processor:
x = item_processor(x)
processed_value.append(x)
return '[' + ', '.join(processed_value) + ']'
return process
class Nullable(ClickHouseTypeEngine):
__visit_name__ = 'nullable'
def __init__(self, nested_type):
self.nested_type = to_instance(nested_type)
super(Nullable, self).__init__()
class UUID(String):
__visit_name__ = 'uuid'
class LowCardinality(ClickHouseTypeEngine):
__visit_name__ = 'lowcardinality'
def __init__(self, nested_type):
self.nested_type = to_instance(nested_type)
super(LowCardinality, self).__init__()
def __repr__(self):
nested_type_str = f'{self.nested_type.__module__}.{self.nested_type!r}'
return f'LowCardinality({nested_type_str})'
class Int8(Int):
__visit_name__ = 'int8'
class UInt8(Int):
__visit_name__ = 'uint8'
class Int16(Int):
__visit_name__ = 'int16'
class UInt16(Int):
__visit_name__ = 'uint16'
class Int32(Int):
__visit_name__ = 'int32'
class UInt32(Int):
__visit_name__ = 'uint32'
class Int64(Int):
__visit_name__ = 'int64'
class UInt64(Int):
__visit_name__ = 'uint64'
class Int128(Int):
__visit_name__ = 'int128'
class UInt128(Int):
__visit_name__ = 'uint128'
class Int256(Int):
__visit_name__ = 'int256'
class UInt256(Int):
__visit_name__ = 'uint256'
class BFloat16(Float):
__visit_name__ = 'bfloat16'
class Float32(Float):
__visit_name__ = 'float32'
class Float64(Float):
__visit_name__ = 'float64'
class Date(types.Date, ClickHouseTypeEngine):
__visit_name__ = 'date'
class Date32(types.Date, ClickHouseTypeEngine):
__visit_name__ = 'date32'
class DateTime(types.DateTime, ClickHouseTypeEngine):
__visit_name__ = 'datetime'
def __init__(self, timezone=None):
super(DateTime, self).__init__()
self.timezone = timezone
class DateTime64(DateTime, ClickHouseTypeEngine):
__visit_name__ = 'datetime64'
def __init__(self, precision=3, timezone=None):
self.precision = precision
super(DateTime64, self).__init__(timezone=timezone)
class Enum(types.Enum, ClickHouseTypeEngine):
__visit_name__ = 'enum'
native_enum = True
def __init__(self, *enums, **kw):
if not enums:
enums = kw.get('_enums', ()) # passed as keyword
super(Enum, self).__init__(*enums, **kw, convert_unicode=False)
class Enum8(Enum):
__visit_name__ = 'enum8'
class Enum16(Enum):
__visit_name__ = 'enum16'
class Decimal(types.Numeric, ClickHouseTypeEngine):
__visit_name__ = 'numeric'
class Tuple(ClickHouseTypeEngine):
__visit_name__ = 'tuple'
def __init__(self, *nested_types):
self.nested_types = nested_types
super(Tuple, self).__init__()
class Map(ClickHouseTypeEngine):
__visit_name__ = 'map'
def __init__(self, key_type, value_type):
self.key_type = key_type
self.value_type = value_type
super(Map, self).__init__()
class AggregateFunction(ClickHouseTypeEngine):
__visit_name__ = 'aggregatefunction'
def __init__(
self,
agg_func: Union[Function, str],
*nested_types: Union[Type[ClickHouseTypeEngine], ClickHouseTypeEngine],
):
self.agg_func = agg_func
self.nested_types = [to_instance(val) for val in nested_types]
super(AggregateFunction, self).__init__()
def __repr__(self) -> str:
type_strs = [f'{val.__module__}.{val!r}' for val in self.nested_types]
if isinstance(self.agg_func, str):
agg_str = self.agg_func
else:
agg_str = f'sa.func.{self.agg_func}'
return f"AggregateFunction({agg_str}, {', '.join(type_strs)})"
class SimpleAggregateFunction(ClickHouseTypeEngine):
__visit_name__ = 'simpleaggregatefunction'
def __init__(
self,
agg_func: Union[Function, str],
*nested_types: Union[Type[ClickHouseTypeEngine], ClickHouseTypeEngine],
):
self.agg_func = agg_func
self.nested_types = [to_instance(val) for val in nested_types]
super(SimpleAggregateFunction, self).__init__()
def __repr__(self) -> str:
type_strs = [f'{val.__module__}.{val!r}' for val in self.nested_types]
if isinstance(self.agg_func, str):
agg_str = self.agg_func
else:
agg_str = f'sa.func.{self.agg_func}'
return f"SimpleAggregateFunction({agg_str}, {', '.join(type_strs)})"