-
Notifications
You must be signed in to change notification settings - Fork 539
/
registry.py
637 lines (495 loc) · 18 KB
/
registry.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
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
import functools
import operator
from typing import Any, Dict
import sqlalchemy as sa
import sqlalchemy.sql as sql
import ibis
import ibis.common.exceptions as com
import ibis.expr.analysis as L
import ibis.expr.datatypes as dt
import ibis.expr.operations as ops
import ibis.expr.types as ir
import ibis.expr.window as W
from .database import AlchemyTable
from .geospatial import geospatial_supported
def variance_reduction(func_name):
suffix = {'sample': 'samp', 'pop': 'pop'}
def variance_compiler(t, expr):
arg, how, where = expr.op().args
if arg.type().equals(dt.boolean):
arg = arg.cast('int32')
func = getattr(
sa.func, '{}_{}'.format(func_name, suffix.get(how, 'samp'))
)
if where is not None:
arg = where.ifelse(arg, None)
return func(t.translate(arg))
return variance_compiler
def infix_op(infix_sym):
def formatter(t, expr):
op = expr.op()
left, right = op.args
left_arg = t.translate(left)
right_arg = t.translate(right)
return left_arg.op(infix_sym)(right_arg)
return formatter
def fixed_arity(sa_func, arity):
if isinstance(sa_func, str):
sa_func = getattr(sa.func, sa_func)
def formatter(t, expr):
if arity != len(expr.op().args):
raise com.IbisError('incorrect number of args')
return _varargs_call(sa_func, t, expr.op().args)
return formatter
def _varargs_call(sa_func, t, args):
trans_args = []
for raw_arg in args:
arg = t.translate(raw_arg)
try:
arg = arg.scalar_subquery()
except AttributeError:
pass
trans_args.append(arg)
return sa_func(*trans_args)
def varargs(sa_func):
def formatter(t, expr):
return _varargs_call(sa_func, t, expr.op().arg)
return formatter
def get_sqla_table(ctx, table):
if ctx.has_ref(table, parent_contexts=True):
ctx_level = ctx
sa_table = ctx_level.get_ref(table)
while sa_table is None and ctx_level.parent is not ctx_level:
ctx_level = ctx_level.parent
sa_table = ctx_level.get_ref(table)
else:
op = table.op()
if isinstance(op, AlchemyTable):
sa_table = op.sqla_table
else:
sa_table = ctx.get_compiled_expr(table)
return sa_table
def get_col_or_deferred_col(sa_table, colname):
"""
Get a `ColumnExpr`, or create a "deferred" column.
This is to handle the case when selecting a column from a join, which
happens when a join expression is cached during join traversal
We'd like to avoid generating a subquery just for selection but in
sqlalchemy the Join object is not selectable. However, at this point
know that the column can be referred to unambiguously
Later the expression is assembled into
`sa.select([sa.column(colname)]).select_from(table_set)` (roughly)
where `table_set` is `sa_table` above.
"""
try:
return sa_table.exported_columns[colname]
except KeyError:
return sa.column(colname)
def _table_column(t, expr):
op = expr.op()
ctx = t.context
table = op.table
sa_table = get_sqla_table(ctx, table)
out_expr = get_col_or_deferred_col(sa_table, op.name)
# If the column does not originate from the table set in the current SELECT
# context, we should format as a subquery
if t.permit_subquery and ctx.is_foreign_expr(table):
return sa.select([out_expr])
return out_expr
def _table_array_view(t, expr):
ctx = t.context
table = ctx.get_compiled_expr(expr.op().table)
return table
def _exists_subquery(t, expr):
from .query_builder import AlchemyCompiler
op = expr.op()
ctx = t.context
filtered = op.foreign_table.filter(op.predicates).projection(
[ir.literal(1).name(ir.core.unnamed)]
)
sub_ctx = ctx.subcontext()
clause = AlchemyCompiler.to_sql(filtered, sub_ctx, exists=True)
if isinstance(op, ops.NotExistsSubquery):
clause = sa.not_(clause)
return clause
def _cast(t, expr):
op = expr.op()
arg, target_type = op.args
sa_arg = t.translate(arg)
sa_type = t.get_sqla_type(target_type)
if isinstance(arg, ir.CategoryValue) and target_type == 'int32':
return sa_arg
else:
return sa.cast(sa_arg, sa_type)
def _contains(t, expr):
op = expr.op()
left, right = (t.translate(arg) for arg in op.args)
return left.in_(right)
def _not_contains(t, expr):
return sa.not_(_contains(t, expr))
def reduction(sa_func):
def formatter(t, expr):
op = expr.op()
if op.where is not None:
arg = t.translate(op.where.ifelse(op.arg, ibis.NA))
else:
arg = t.translate(op.arg)
return sa_func(arg)
return formatter
def _group_concat(t, expr):
op = expr.op()
sep = t.translate(op.sep)
if op.where is not None:
arg = t.translate(op.where.ifelse(op.arg, ibis.NA))
else:
arg = t.translate(op.arg)
return sa.func.group_concat(arg, sep)
def _alias(t, expr):
# just compile the underlying argument because the naming is handled
# by the translator for the top level expression
op = expr.op()
return t.translate(op.arg)
def _literal(t, expr):
dtype = expr.type()
value = expr.op().value
if isinstance(dtype, dt.Set):
return list(map(sa.literal, value))
return sa.literal(value)
def _value_list(t, expr):
return [t.translate(x) for x in expr.op().values]
def _is_null(t, expr):
arg = t.translate(expr.op().args[0])
return arg.is_(sa.null())
def _not_null(t, expr):
arg = t.translate(expr.op().args[0])
return arg.isnot(sa.null())
def _round(t, expr):
op = expr.op()
arg, digits = op.args
sa_arg = t.translate(arg)
f = sa.func.round
if digits is not None:
sa_digits = t.translate(digits)
return f(sa_arg, sa_digits)
else:
return f(sa_arg)
def _floor_divide(t, expr):
left, right = map(t.translate, expr.op().args)
return sa.func.floor(left / right)
def _count_distinct(t, expr):
arg, where = expr.op().args
if where is not None:
sa_arg = t.translate(where.ifelse(arg, None))
else:
sa_arg = t.translate(arg)
return sa.func.count(sa_arg.distinct())
def _simple_case(t, expr):
op = expr.op()
cases = [op.base == case for case in op.cases]
return _translate_case(t, cases, op.results, op.default)
def _searched_case(t, expr):
op = expr.op()
return _translate_case(t, op.cases, op.results, op.default)
def _translate_case(t, cases, results, default):
case_args = [t.translate(arg) for arg in cases]
result_args = [t.translate(arg) for arg in results]
whens = zip(case_args, result_args)
default = t.translate(default)
return sa.case(list(whens), else_=default)
def _negate(t, expr):
op = expr.op()
(arg,) = map(t.translate, op.args)
return sa.not_(arg) if isinstance(expr, ir.BooleanValue) else -arg
def unary(sa_func):
return fixed_arity(sa_func, 1)
def _string_like(method_name, t, expr):
op = expr.op()
method = getattr(t.translate(op.arg), method_name)
return method(t.translate(op.pattern), escape=op.escape)
def _startswith(t, expr):
arg, start = expr.op().args
return t.translate(arg).startswith(t.translate(start))
def _endswith(t, expr):
arg, start = expr.op().args
return t.translate(arg).endswith(t.translate(start))
_cumulative_to_reduction = {
ops.CumulativeSum: ops.Sum,
ops.CumulativeMin: ops.Min,
ops.CumulativeMax: ops.Max,
ops.CumulativeMean: ops.Mean,
ops.CumulativeAny: ops.Any,
ops.CumulativeAll: ops.All,
}
def _cumulative_to_window(translator, expr, window):
win = W.cumulative_window()
win = win.group_by(window._group_by).order_by(window._order_by)
op = expr.op()
klass = _cumulative_to_reduction[type(op)]
new_op = klass(*op.args)
new_expr = new_op.to_expr()
if expr.has_name():
new_expr = new_expr.name(expr.get_name())
if type(new_op) in translator._rewrites:
new_expr = translator._rewrites[type(new_op)](new_expr)
return L.windowize_function(new_expr, win)
def _window(t, expr):
op = expr.op()
arg, window = op.args
reduction = t.translate(arg)
window_op = arg.op()
_require_order_by = (
ops.DenseRank,
ops.MinRank,
ops.NTile,
ops.PercentRank,
)
if isinstance(window_op, ops.CumulativeOp):
arg = _cumulative_to_window(t, arg, window)
return t.translate(arg)
if window.max_lookback is not None:
raise NotImplementedError(
'Rows with max lookback is not implemented '
'for SQLAlchemy-based backends.'
)
# Some analytic functions need to have the expression of interest in
# the ORDER BY part of the window clause
if isinstance(window_op, _require_order_by) and not window._order_by:
order_by = t.translate(window_op.args[0])
else:
order_by = list(map(t.translate, window._order_by))
partition_by = list(map(t.translate, window._group_by))
frame_clause_not_allowed = (
ops.Lag,
ops.Lead,
ops.DenseRank,
ops.MinRank,
ops.NTile,
ops.PercentRank,
ops.RowNumber,
)
how = {'range': 'range_'}.get(window.how, window.how)
preceding = window.preceding
additional_params = (
{}
if isinstance(window_op, frame_clause_not_allowed)
else {
how: (
-preceding if preceding is not None else preceding,
window.following,
)
}
)
result = reduction.over(
partition_by=partition_by, order_by=order_by, **additional_params
)
if isinstance(
window_op, (ops.RowNumber, ops.DenseRank, ops.MinRank, ops.NTile)
):
return result - 1
else:
return result
def _lag(t, expr):
arg, offset, default = expr.op().args
if default is not None:
raise NotImplementedError()
sa_arg = t.translate(arg)
sa_offset = t.translate(offset) if offset is not None else 1
return sa.func.lag(sa_arg, sa_offset)
def _lead(t, expr):
arg, offset, default = expr.op().args
if default is not None:
raise NotImplementedError()
sa_arg = t.translate(arg)
sa_offset = t.translate(offset) if offset is not None else 1
return sa.func.lead(sa_arg, sa_offset)
def _ntile(t, expr):
op = expr.op()
args = op.args
arg, buckets = map(t.translate, args)
return sa.func.ntile(buckets)
def _sort_key(t, expr):
# We need to define this for window functions that have an order by
by, ascending = expr.op().args
sort_direction = sa.asc if ascending else sa.desc
return sort_direction(t.translate(by))
def _string_join(t, expr):
sep, elements = expr.op().args
return sa.func.concat_ws(t.translate(sep), *map(t.translate, elements))
sqlalchemy_operation_registry: Dict[Any, Any] = {
ops.Alias: _alias,
ops.And: fixed_arity(sql.and_, 2),
ops.Or: fixed_arity(sql.or_, 2),
ops.Not: unary(sa.not_),
ops.Abs: unary(sa.func.abs),
ops.Cast: _cast,
ops.Coalesce: varargs(sa.func.coalesce),
ops.NullIf: fixed_arity(sa.func.nullif, 2),
ops.Contains: _contains,
ops.NotContains: _not_contains,
ops.Count: reduction(sa.func.count),
ops.Sum: reduction(sa.func.sum),
ops.Mean: reduction(sa.func.avg),
ops.Min: reduction(sa.func.min),
ops.Max: reduction(sa.func.max),
ops.CountDistinct: _count_distinct,
ops.GroupConcat: _group_concat,
ops.Between: fixed_arity(sa.between, 3),
ops.IsNull: _is_null,
ops.NotNull: _not_null,
ops.Negate: _negate,
ops.Round: _round,
ops.TypeOf: unary(sa.func.typeof),
ops.Literal: _literal,
ops.ValueList: _value_list,
ops.NullLiteral: lambda *_: sa.null(),
ops.SimpleCase: _simple_case,
ops.SearchedCase: _searched_case,
ops.TableColumn: _table_column,
ops.TableArrayView: _table_array_view,
ops.ExistsSubquery: _exists_subquery,
ops.NotExistsSubquery: _exists_subquery,
# miscellaneous varargs
ops.Least: varargs(sa.func.least),
ops.Greatest: varargs(sa.func.greatest),
# string
ops.LPad: fixed_arity(sa.func.lpad, 3),
ops.RPad: fixed_arity(sa.func.rpad, 3),
ops.Strip: unary(sa.func.trim),
ops.LStrip: unary(sa.func.ltrim),
ops.RStrip: unary(sa.func.rtrim),
ops.Repeat: fixed_arity(sa.func.repeat, 2),
ops.Reverse: unary(sa.func.reverse),
ops.StrRight: fixed_arity(sa.func.right, 2),
ops.Lowercase: unary(sa.func.lower),
ops.Uppercase: unary(sa.func.upper),
ops.StringAscii: unary(sa.func.ascii),
ops.StringLength: unary(sa.func.length),
ops.StringJoin: _string_join,
ops.StringReplace: fixed_arity(sa.func.replace, 3),
ops.StringSQLLike: functools.partial(_string_like, "like"),
ops.StringSQLILike: functools.partial(_string_like, "ilike"),
ops.StartsWith: _startswith,
ops.EndsWith: _endswith,
ops.StringConcat: varargs(sa.func.concat),
# math
ops.Ln: unary(sa.func.ln),
ops.Exp: unary(sa.func.exp),
ops.Sign: unary(sa.func.sign),
ops.Sqrt: unary(sa.func.sqrt),
ops.Ceil: unary(sa.func.ceil),
ops.Floor: unary(sa.func.floor),
ops.Power: fixed_arity(sa.func.pow, 2),
ops.FloorDivide: _floor_divide,
# other
ops.SortKey: _sort_key,
ops.Date: unary(lambda arg: sa.cast(arg, sa.DATE)),
ops.DateFromYMD: fixed_arity(sa.func.date, 3),
ops.TimeFromHMS: fixed_arity(sa.func.time, 3),
ops.TimestampFromYMDHMS: lambda t, expr: sa.func.make_timestamp(
*map(t.translate, expr.op().args[:6]) # ignore timezone
),
}
# TODO: unit tests for each of these
_binary_ops = {
# Binary arithmetic
ops.Add: operator.add,
ops.Subtract: operator.sub,
ops.Multiply: operator.mul,
# XXX `ops.Divide` is overwritten in `translator.py` with a custom
# function `_true_divide`, but for some reason both are required
ops.Divide: operator.truediv,
ops.Modulus: operator.mod,
# Comparisons
ops.Equals: operator.eq,
ops.NotEquals: operator.ne,
ops.Less: operator.lt,
ops.LessEqual: operator.le,
ops.Greater: operator.gt,
ops.GreaterEqual: operator.ge,
ops.IdenticalTo: lambda x, y: x.op('IS NOT DISTINCT FROM')(y),
# Boolean comparisons
# TODO
}
sqlalchemy_window_functions_registry = {
ops.Lag: _lag,
ops.Lead: _lead,
ops.NTile: _ntile,
ops.FirstValue: unary(sa.func.first_value),
ops.LastValue: unary(sa.func.last_value),
ops.RowNumber: fixed_arity(lambda: sa.func.row_number(), 0),
ops.DenseRank: unary(lambda arg: sa.func.dense_rank()),
ops.MinRank: unary(lambda arg: sa.func.rank()),
ops.PercentRank: unary(lambda arg: sa.func.percent_rank()),
ops.WindowOp: _window,
ops.CumulativeOp: _window,
ops.CumulativeMax: unary(sa.func.max),
ops.CumulativeMin: unary(sa.func.min),
ops.CumulativeSum: unary(sa.func.sum),
ops.CumulativeMean: unary(sa.func.avg),
}
if geospatial_supported:
_geospatial_functions = {
ops.GeoArea: unary(sa.func.ST_Area),
ops.GeoAsBinary: unary(sa.func.ST_AsBinary),
ops.GeoAsEWKB: unary(sa.func.ST_AsEWKB),
ops.GeoAsEWKT: unary(sa.func.ST_AsEWKT),
ops.GeoAsText: unary(sa.func.ST_AsText),
ops.GeoAzimuth: fixed_arity(sa.func.ST_Azimuth, 2),
ops.GeoBuffer: fixed_arity(sa.func.ST_Buffer, 2),
ops.GeoCentroid: unary(sa.func.ST_Centroid),
ops.GeoContains: fixed_arity(sa.func.ST_Contains, 2),
ops.GeoContainsProperly: fixed_arity(sa.func.ST_Contains, 2),
ops.GeoCovers: fixed_arity(sa.func.ST_Covers, 2),
ops.GeoCoveredBy: fixed_arity(sa.func.ST_CoveredBy, 2),
ops.GeoCrosses: fixed_arity(sa.func.ST_Crosses, 2),
ops.GeoDFullyWithin: fixed_arity(sa.func.ST_DFullyWithin, 3),
ops.GeoDifference: fixed_arity(sa.func.ST_Difference, 2),
ops.GeoDisjoint: fixed_arity(sa.func.ST_Disjoint, 2),
ops.GeoDistance: fixed_arity(sa.func.ST_Distance, 2),
ops.GeoDWithin: fixed_arity(sa.func.ST_DWithin, 3),
ops.GeoEndPoint: unary(sa.func.ST_EndPoint),
ops.GeoEnvelope: unary(sa.func.ST_Envelope),
ops.GeoEquals: fixed_arity(sa.func.ST_Equals, 2),
ops.GeoGeometryN: fixed_arity(sa.func.ST_GeometryN, 2),
ops.GeoGeometryType: unary(sa.func.ST_GeometryType),
ops.GeoIntersection: fixed_arity(sa.func.ST_Intersection, 2),
ops.GeoIntersects: fixed_arity(sa.func.ST_Intersects, 2),
ops.GeoIsValid: unary(sa.func.ST_IsValid),
ops.GeoLineLocatePoint: fixed_arity(sa.func.ST_LineLocatePoint, 2),
ops.GeoLineMerge: unary(sa.func.ST_LineMerge),
ops.GeoLineSubstring: fixed_arity(sa.func.ST_LineSubstring, 3),
ops.GeoLength: unary(sa.func.ST_Length),
ops.GeoNPoints: unary(sa.func.ST_NPoints),
ops.GeoOrderingEquals: fixed_arity(sa.func.ST_OrderingEquals, 2),
ops.GeoOverlaps: fixed_arity(sa.func.ST_Overlaps, 2),
ops.GeoPerimeter: unary(sa.func.ST_Perimeter),
ops.GeoSimplify: fixed_arity(sa.func.ST_Simplify, 3),
ops.GeoSRID: unary(sa.func.ST_SRID),
ops.GeoSetSRID: fixed_arity(sa.func.ST_SetSRID, 2),
ops.GeoStartPoint: unary(sa.func.ST_StartPoint),
ops.GeoTouches: fixed_arity(sa.func.ST_Touches, 2),
ops.GeoTransform: fixed_arity(sa.func.ST_Transform, 2),
ops.GeoUnaryUnion: unary(sa.func.ST_Union),
ops.GeoUnion: fixed_arity(sa.func.ST_Union, 2),
ops.GeoWithin: fixed_arity(sa.func.ST_Within, 2),
ops.GeoX: unary(sa.func.ST_X),
ops.GeoY: unary(sa.func.ST_Y),
# Missing Geospatial ops:
# ST_AsGML
# ST_AsGeoJSON
# ST_AsKML
# ST_AsRaster
# ST_AsSVG
# ST_AsTWKB
# ST_Distance_Sphere
# ST_Dump
# ST_DumpPoints
# ST_GeogFromText
# ST_GeomFromEWKB
# ST_GeomFromEWKT
# ST_GeomFromText
}
else:
_geospatial_functions = {}
for _k, _v in _binary_ops.items():
sqlalchemy_operation_registry[_k] = fixed_arity(_v, 2)