-
-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathsqlcompiler.py
More file actions
524 lines (421 loc) · 16.5 KB
/
Copy pathsqlcompiler.py
File metadata and controls
524 lines (421 loc) · 16.5 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
from sqlalchemy import exc, literal_column
from sqlalchemy.sql import compiler, elements, COLLECT_CARTESIAN_PRODUCTS, \
WARN_LINTING, crud
from sqlalchemy.sql import type_api
from sqlalchemy.util import inspect_getfullargspec
from ... import types
class ClickHouseSQLCompiler(compiler.SQLCompiler):
def visit_select(
self,
select_stmt,
**kwargs,
):
orig_compile_state_factory = select_stmt._compile_state_factory
def f(self, *args, **kwargs):
tmp = orig_compile_state_factory(self, *args, **kwargs)
if hasattr(tmp, 'select_statement'):
# Fix missed attributes
for attr in ["_with_cube", "_with_rollup", "_with_totals", "_final_clause", "_sample_clause", "_limit_by_clause", "_array_join"]:
val = getattr(tmp.select_statement, attr, None)
if val:
setattr(tmp.statement, attr, val)
return tmp
select_stmt._compile_state_factory = f
tmp_select = super().visit_select(select_stmt=select_stmt, **kwargs)
return tmp_select
def visit_mod_binary(self, binary, operator, **kw):
return self.process(binary.left, **kw) + ' %% ' + \
self.process(binary.right, **kw)
def visit_is_not_distinct_from_binary(self, binary, operator, **kw):
"""
Implementation of distinctness comparison in ClickHouse SQL.
A distinctness comparison treats NULL as if it is a (singleton)
value and is what ClickHouse uses for `SELECT DISTINCT` and `GROUP BY`.
Some databases have direct support for a `IS DISTINCT` comparison, but
ClickHouse does not, so we rely on the `hasAny` array function here.
"""
return "hasAny([%s], [%s])" % (
self.process(binary.left, **kw),
self.process(binary.right, **kw),
)
def visit_is_distinct_from_binary(self, binary, operator, **kw):
return "NOT %s" % self.visit_is_not_distinct_from_binary(
binary, operator, **kw
)
def visit_empty_set_expr(self, element_types):
return "SELECT %s WHERE 1!=1" % (
", ".join(
"CAST(NULL AS %s)"
% self.dialect.type_compiler.process(
t if isinstance(t, types.Nullable) else types.Nullable(t)
)
for t in element_types or [types.Int8()]
),
)
def post_process_text(self, text):
return text.replace('%', '%%')
def visit_count_func(self, fn, **kw):
# count accepts zero arguments.
return 'count%s' % self.process(fn.clause_expr, **kw)
def visit_case(self, clause, **kwargs):
text = 'CASE '
if clause.value is not None:
text += clause.value._compiler_dispatch(self, **kwargs) + ' '
for cond, result in clause.whens:
text += 'WHEN ' + cond._compiler_dispatch(
self, **kwargs
) + ' THEN ' + result._compiler_dispatch(
self, **kwargs) + " "
if clause.else_ is not None:
text += 'ELSE ' + clause.else_._compiler_dispatch(
self, **kwargs
) + ' '
text += 'END'
return text
def visit_if__func(self, func, **kw):
return "(%s) ? (%s) : (%s)" % (
self.process(func.clauses.clauses[0], **kw),
self.process(func.clauses.clauses[1], **kw),
self.process(func.clauses.clauses[2], **kw)
)
def limit_by_clause(self, select, **kw):
text = ''
limit_by_clause = select._limit_by_clause
if limit_by_clause:
text += ' LIMIT '
if limit_by_clause.offset is not None:
text += self.process(limit_by_clause.offset, **kw) + ', '
text += self.process(limit_by_clause.limit, **kw)
limit_by_exprs = limit_by_clause.by_clauses._compiler_dispatch(
self, **kw
)
text += ' BY ' + limit_by_exprs
return text
def limit_clause(self, select, **kw):
text = ''
if select._limit_clause is not None:
text += ' \n LIMIT '
if select._offset_clause is not None:
text += self.process(select._offset_clause, **kw) + ', '
text += self.process(select._limit_clause, **kw)
else:
if select._offset_clause is not None:
raise exc.CompileError('OFFSET without LIMIT is not supported')
return text
def visit_lambda(self, lambda_, **kw):
func = lambda_.func
spec = inspect_getfullargspec(func)
if spec.varargs:
raise exc.CompileError('Lambdas with *args are not supported')
try:
# ArgSpec in SA>=1.3.0b2
keywords = spec.keywords
except AttributeError:
# FullArgSpec in SA>=1.3.0b2
keywords = spec.varkw
if keywords:
raise exc.CompileError('Lambdas with **kwargs are not supported')
text = ', '.join(spec.args) + ' -> '
args = [literal_column(arg) for arg in spec.args]
text += self.process(func(*args), **kw)
return text
def visit_extract(self, extract, **kw):
field = self.extract_map.get(extract.field, extract.field)
column = self.process(extract.expr, **kw)
if field == 'year':
return 'toYear(%s)' % column
elif field == 'month':
return 'toMonth(%s)' % column
elif field == 'day':
return 'toDayOfMonth(%s)' % column
else:
return column
def visit_join(self, join, asfrom=False, **kwargs):
text = join.left._compiler_dispatch(self, asfrom=True, **kwargs)
if text[0] == '(' and text[-1] == ')':
text = text[1:-1]
flags = join.full
if not isinstance(flags, dict):
flags = {'full': flags}
# need to make a variable to prevent leaks in some debuggers
join_type = flags.get('type')
if join_type is None:
if flags.get('full'):
join_type = 'FULL OUTER'
elif join.isouter:
join_type = 'LEFT OUTER'
else:
join_type = 'INNER'
else:
join_type = join_type.upper()
if join.isouter and 'INNER' in join_type:
raise exc.CompileError(
"can't compile join with specified "
"INNER type and isouter=True"
)
# isouter=False by default, disable that checking
# elif not join.isouter and 'OUTER' in join.type:
# raise exc.CompileError(
# "can't compile join with specified "
# "OUTER type and isouter=False"
# )
strictness = flags.get('strictness')
if strictness:
join_type = strictness.upper() + ' ' + join_type
distribution = flags.get('distribution')
if distribution:
join_type = distribution.upper() + ' ' + join_type
if join_type is not None:
text += ' ' + join_type.upper() + ' JOIN '
onclause = join.onclause
text += join.right._compiler_dispatch(self, asfrom=True, **kwargs)
if isinstance(onclause, elements.Tuple):
text += ' USING ' + onclause._compiler_dispatch(
self, include_table=False, **kwargs
)
else:
text += ' ON ' + onclause._compiler_dispatch(self, **kwargs)
return text
def visit_array_join(self, array_join, **kwargs):
kwargs['within_columns_clause'] = True
return ' \nARRAY JOIN {columns}'.format(
columns=', '.join(
col._compiler_dispatch(self,
within_label_clause=False,
**kwargs)
for col in array_join.clauses
)
)
def visit_left_array_join(self, array_join, **kwargs):
kwargs['within_columns_clause'] = True
return ' \nLEFT ARRAY JOIN {columns}'.format(
columns=', '.join(
col._compiler_dispatch(self,
within_label_clause=False,
**kwargs)
for col in array_join.clauses
)
)
def visit_label(self,
label,
from_labeled_label=False,
**kw):
if from_labeled_label:
return super(ClickHouseSQLCompiler, self).visit_label(
label,
render_label_as_label=label
)
else:
return super(ClickHouseSQLCompiler, self).visit_label(
label,
**kw
)
def _compose_select_body(
self,
text,
select,
compile_state,
inner_columns,
froms,
byfrom,
toplevel,
kwargs,
):
text += ", ".join(inner_columns)
if self.linting & COLLECT_CARTESIAN_PRODUCTS:
from_linter = compiler.FromLinter({}, set())
warn_linting = self.linting & WARN_LINTING
if toplevel:
self.from_linter = from_linter
else:
from_linter = None
warn_linting = False
if froms:
text += " \nFROM "
if select._hints:
text += ", ".join(
[
f._compiler_dispatch(
self,
asfrom=True,
fromhints=byfrom,
from_linter=from_linter,
**kwargs
)
for f in froms
]
)
else:
text += ", ".join(
[
f._compiler_dispatch(
self,
asfrom=True,
from_linter=from_linter,
**kwargs
)
for f in froms
]
)
else:
text += self.default_from()
if getattr(select, '_array_join', None) is not None:
text += select._array_join._compiler_dispatch(self, **kwargs)
sample_clause = getattr(select, '_sample_clause', None)
if sample_clause is not None:
text += self.sample_clause(select, **kwargs)
final_clause = getattr(select, '_final_clause', None)
if final_clause is not None:
text += self.final_clause()
if select._where_criteria:
t = self._generate_delimited_and_list(
select._where_criteria, from_linter=from_linter, **kwargs
)
if t:
text += " \nWHERE " + t
if warn_linting:
from_linter.warn()
if select._group_by_clauses:
text += self.group_by_clause(select, **kwargs)
if select._having_criteria:
t = self._generate_delimited_and_list(
select._having_criteria, **kwargs
)
if t:
text += " \nHAVING " + t
if select._order_by_clauses:
text += self.order_by_clause(select, **kwargs)
limit_by_clause = getattr(select, '_limit_by_clause', None)
if limit_by_clause is not None:
text += self.limit_by_clause(select, **kwargs)
if select._has_row_limiting_clause:
text += self._row_limit_clause(select, **kwargs)
if select._for_update_arg is not None:
text += self.for_update_clause(select, **kwargs)
return text
def sample_clause(self, select, **kw):
return " \nSAMPLE " + self.process(select._sample_clause, **kw)
def final_clause(self):
return " \nFINAL"
def group_by_clause(self, select, **kw):
text = ""
group_by = select._group_by_clause._compiler_dispatch(
self, **kw)
if group_by:
text = " GROUP BY " + group_by
if getattr(select, '_with_cube', False):
text += " WITH CUBE"
if getattr(select, '_with_rollup', False):
text += " WITH ROLLUP"
if getattr(select, '_with_totals', False):
text += " WITH TOTALS"
return text
def visit_delete(self, delete_stmt, **kw):
if not self.dialect.supports_delete:
raise exc.CompileError(
'ALTER DELETE is not supported by this server version'
)
compile_state = delete_stmt._compile_state_factory(
delete_stmt, self, **kw
)
delete_stmt = compile_state.statement
extra_froms = compile_state._extra_froms
correlate_froms = {delete_stmt.table}.union(extra_froms)
self.stack.append(
{
"correlate_froms": correlate_froms,
"asfrom_froms": correlate_froms,
"selectable": delete_stmt,
}
)
text = "ALTER TABLE "
table_text = self.delete_table_clause(
delete_stmt, delete_stmt.table, extra_froms
)
text += table_text + " DELETE"
if delete_stmt._where_criteria:
t = self._generate_delimited_and_list(
delete_stmt._where_criteria, include_table=False, **kw
)
if t:
text += " WHERE " + t
else:
raise exc.CompileError('WHERE clause is required')
self.stack.pop(-1)
return text
def visit_update(self, update_stmt, **kw):
if not self.dialect.supports_update:
raise exc.CompileError(
'ALTER UPDATE is not supported by this server version'
)
compile_state = update_stmt._compile_state_factory(
update_stmt, self, **kw
)
update_stmt = compile_state.statement
render_extra_froms = []
correlate_froms = {update_stmt.table}
self.stack.append(
{
"correlate_froms": correlate_froms,
"asfrom_froms": correlate_froms,
"selectable": update_stmt,
}
)
text = "ALTER TABLE "
table_text = self.update_tables_clause(
update_stmt, update_stmt.table, render_extra_froms, **kw
)
crud_params = crud._get_crud_params(
self, update_stmt, compile_state, **kw
)
text += table_text
text += " UPDATE "
text += ", ".join(expr + "=" + value for c, expr, value in crud_params)
if update_stmt._where_criteria:
t = self._generate_delimited_and_list(
update_stmt._where_criteria, include_table=False, **kw
)
if t:
text += " WHERE " + t
else:
raise exc.CompileError('WHERE clause is required')
self.stack.pop(-1)
return text
def render_literal_value(self, value, type_):
if isinstance(value, list):
return (
'[' +
', '.join(self.render_literal_value(
x, type_api._resolve_value_to_type(x)
) for x in value) +
']'
)
else:
return super(ClickHouseSQLCompiler, self).render_literal_value(
value, type_
)
def _get_regexp_args(self, binary, kw):
string = self.process(binary.left, **kw)
pattern = self.process(binary.right, **kw)
return string, pattern
def visit_regexp_match_op_binary(self, binary, operator, **kw):
string, pattern = self._get_regexp_args(binary, kw)
return "MATCH(%s, %s)" % (string, pattern)
def visit_not_regexp_match_op_binary(self, binary, operator, **kw):
return "NOT %s" % self.visit_regexp_match_op_binary(
binary,
operator,
**kw
)
def visit_ilike_case_insensitive_operand(self, element, **kw):
return element.element._compiler_dispatch(self, **kw)
def visit_ilike_op_binary(self, binary, operator, **kw):
return "%s ILIKE %s" % (
self.process(binary.left, **kw),
self.process(binary.right, **kw)
)
def visit_not_ilike_op_binary(self, binary, operator, **kw):
return "%s NOT ILIKE %s" % (
self.process(binary.left, **kw),
self.process(binary.right, **kw)
)