Source

htsql-charset-option / src / htsql / tr / assembler.py

Full commit
  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
#
# Copyright (c) 2006-2010, Prometheus Research, LLC
# Authors: Clark C. Evans <cce@clarkevans.com>,
#          Kirill Simonov <xi@resolvent.net>
#


"""
:mod:`htsql.tr.assembler`
=========================

This module implements the assemble adapter.
"""


from ..adapter import Adapter, adapts, find_adapters
from .code import (Code, Space, ScalarSpace, FreeTableSpace, JoinedTableSpace,
                   ScreenSpace, OrderedSpace, RelativeSpace, Expression, Unit,
                   ColumnUnit, AggregateUnit, SegmentCode, QueryCode)
from .term import (TableTerm, ScalarTerm, FilterTerm, JoinTerm,
                   CorrelationTerm, ProjectionTerm, OrderingTerm, WrapperTerm,
                   SegmentTerm, QueryTerm, ParallelTie, SeriesTie,
                   LEFT, RIGHT, FORWARD)


class Assembler(object):

    def assemble(self, code, *args):
        assemble = Assemble(code, self)
        return assemble.assemble(*args)

    def inject(self, code, term):
        assemble = Assemble(code, self)
        return assemble.inject(term)


class Assemble(Adapter):

    adapts(Code, Assembler)

    def __init__(self, code, assembler):
        self.code = code
        self.assembler = assembler

    def assemble(self):
        raise NotImplementedError()

    def inject(self, term):
        raise NotImplementedError()


class AssembleSpace(Assemble):

    adapts(Space, Assembler)

    def __init__(self, space, assembler):
        self.space = space
        self.assembler = assembler

    def assemble(self, baseline):
        raise NotImplementedError()


class AssembleExpression(Assemble):

    adapts(Expression, Assembler)

    def inject(self, term):
        for unit in self.code.get_units():
            term = self.assembler.inject(unit, term)
        return term


class AssembleUnit(AssembleExpression):

    adapts(Unit, Assembler)

    def inject(self, term):
        raise NotImplementedError()


class AssembleScalar(AssembleSpace):

    adapts(ScalarSpace, Assembler)

    def assemble(self, baseline):
        assert baseline == self.space
        routes = {}
        routes[self.space] = []
        return ScalarTerm(self.space, baseline, routes, self.space.mark)

    def inject(self, term):
        if self.space in term.routes:
            return term
        axis = term.baseline
        while axis.parent != self.space:
            axis = axis.parent
        term = self.assembler.inject(axis, term)
        assert self.space not in term.routes
        left = term
        right_routes = {}
        right_routes[self.space] = []
        right = ScalarTerm(self.space, self.space,
                           right_routes, self.space.mark)
        tie = SeriesTie(axis, is_reverse=True)
        routes = {}
        for key in left.routes:
            routes[key] = [LEFT] + left.routes[key]
        routes[self.space] = [RIGHT]
        return JoinTerm(left, right, [tie], True, left.space, left.baseline,
                        routes, left.mark)


class AssembleFreeTable(AssembleSpace):

    adapts(FreeTableSpace, Assembler)

    def assemble(self, baseline):
        backbone = self.space.axes()
        if baseline == backbone:
            routes = {}
            routes[self.space] = []
            routes[backbone] = []
            return TableTerm(self.space.table, self.space, baseline,
                             routes, self.space.mark)
        left = self.assembler.assemble(self.space.parent, baseline)
        right_routes = {}
        right_routes[self.space] = []
        right_routes[backbone] = []
        right = TableTerm(self.space.table, self.space, backbone,
                          right_routes, self.space.mark)
        routes = {}
        for key in left.routes:
            routes[key] = [LEFT] + left.routes[key]
        routes[self.space] = [RIGHT]
        routes[backbone] = [RIGHT]
        tie = SeriesTie(self.space)
        return JoinTerm(left, right, [tie], True, self.space, baseline,
                        routes, self.space.mark)

    def inject(self, term):
        if self.space in term.routes:
            return term
        assert term.space.spans(self.space)
        space = self.space.inflate(term.space)
        if self.space != space:
            term = self.assembler.inject(space, term)
            routes = term.routes.copy()
            routes[self.space] = routes[space]
            return term.clone(routes=routes)
        backbone = self.space.axes()
        term_backbone = term.space.axes()
        assert term_backbone.concludes(backbone)
        if self.space == backbone:
            axis = term_backbone
            while axis.parent != self.space:
                axis = axis.parent
            left = self.assembler.inject(axis, term)
            assert self.space not in left.routes
            right_routes = {}
            right_routes[self.space] = []
            right = TableTerm(self.space.table, self.space, self.space,
                              right_routes, self.space.mark)
            tie = SeriesTie(axis, is_reverse=True)
            routes = {}
            for key in left.routes:
                routes[key] = [LEFT] + left.routes[key]
            routes[self.space] = [RIGHT]
            return JoinTerm(left, right, [tie], True, left.space,
                            left.baseline, routes, left.mark)
        left = term
        left = self.assembler.inject(self.space.parent, left)
        left = self.assembler.inject(backbone, left)
        assert self.space not in left.routes
        right_routes = {}
        right_routes[self.space] = []
        right_routes[backbone] = []
        right = TableTerm(self.space.table, self.space, backbone,
                          right_routes, self.space.mark)
        backbone_tie = ParallelTie(backbone)
        parent_tie = SeriesTie(self.space)
        routes = {}
        for key in left.routes:
            routes[key] = [LEFT] + left.routes[key]
        routes[self.space] = [RIGHT]
        return JoinTerm(left, right, [backbone_tie, parent_tie], False,
                        left.space, left.baseline, routes, left.mark)


class AssembleJoinedTable(AssembleSpace):

    adapts(JoinedTableSpace, Assembler)

    def assemble(self, baseline):
        backbone = self.space.axes()
        if baseline == backbone:
            routes = {}
            routes[self.space] = []
            routes[backbone] = []
            return TableTerm(self.space.table, self.space, baseline,
                             routes, self.space.mark)
        term = self.assembler.assemble(self.space.parent, baseline)
        assert self.space not in term.routes
        if backbone in term.routes:
            if term.space.conforms(self.space):
                routes = term.routes.copy()
                routes[self.space] = routes[backbone]
                term = term.clone(space=self.space, routes=routes)
                return term
            assert term.space.dominates(self.space)
        left = term
        right_routes = {}
        right_routes[self.space] = []
        right_routes[backbone] = []
        right = TableTerm(self.space.table, self.space, backbone,
                          right_routes, self.space.mark)
        routes = {}
        for key in left.routes:
            routes[key] = [LEFT]+left.routes[key]
        routes[self.space] = [RIGHT]
        routes[backbone] = [RIGHT]
        tie = SeriesTie(self.space)
        return JoinTerm(left, right, [tie], True, self.space, baseline,
                        routes, self.space.mark)

    def inject(self, term):
        if self.space in term.routes:
            return term
        assert term.space.spans(self.space)
        space = self.space.inflate(term.space)
        if self.space != space:
            term = self.assembler.inject(space, term)
            routes = term.routes.copy()
            routes[self.space] = routes[space]
            return term.clone(routes=routes)
        backbone = self.space.axes()
        term_backbone = term.space.axes()
        if term_backbone.concludes(self.space):
            axis = term_backbone
            while axis.parent != self.space:
                axis = axis.parent
            left = self.assembler.inject(axis, term)
            assert self.space not in left.routes
            right_routes = {}
            right_routes[self.space] = []
            right = TableTerm(self.space.table, self.space, self.space,
                              right_routes, self.space.mark)
            tie = SeriesTie(axis, is_reverse=True)
            routes = {}
            for key in left.routes:
                routes[key] = [LEFT] + left.routes[key]
            routes[self.space] = [RIGHT]
            return JoinTerm(left, right, [tie], True, left.space,
                            left.baseline, routes, left.mark)
        left = term
        left = self.assembler.inject(self.space.parent, left)
        if not self.space.is_contracting:
            left = self.assembler.inject(backbone, left)
        assert self.space not in left.routes
        right_routes = {}
        right_routes[self.space] = []
        right_routes[backbone] = []
        right = TableTerm(self.space.table, self.space, backbone,
                          right_routes, self.space.mark)
        ties = []
        if not self.space.is_contracting:
            backbone_tie = ParallelTie(backbone)
            ties.append(backbone_tie)
        parent_tie = SeriesTie(self.space)
        ties.append(parent_tie)
        is_inner = True
        space = self.space
        while not term_backbone.concludes(space):
            if not space.is_expanding:
                is_inner = False
                break
            space = space.parent
        routes = {}
        for key in left.routes:
            routes[key] = [LEFT] + left.routes[key]
        routes[self.space] = [RIGHT]
        return JoinTerm(left, right, ties, is_inner,
                        left.space, left.baseline, routes, left.mark)


class AssembleScreen(AssembleSpace):

    adapts(ScreenSpace, Assembler)

    def assemble(self, baseline):
        child = self.assembler.assemble(self.space.parent, baseline)
        child = self.assembler.inject(self.space.filter, child)
        assert self.space not in child.routes
        routes = {}
        for key in child.routes:
            routes[key] = [FORWARD] + child.routes[key]
        routes[self.space] = [FORWARD] + child.routes[self.space.parent]
        return FilterTerm(child, self.space.filter, self.space, baseline,
                          routes, self.space.mark)

    def inject(self, term):
        if self.space in term.routes:
            return term
        assert term.space.spans(self.space)
        space = self.space.inflate(term.space)
        if self.space != space:
            term = self.assembler.inject(space, term)
            routes = term.routes.copy()
            routes[self.space] = routes[space]
            return term.clone(routes=routes)
        left = term
        term_backbone = term.space.axes()
        baseline = self.space.axes()
        right = self.assembler.assemble(self.space, baseline)
        ties = []
        if term_backbone.concludes(baseline) or baseline.parent in left.routes:
            axes = baseline
            while axes in right.routes:
                left = self.assembler.inject(axes, left)
                tie = ParallelTie(axes)
                ties.append(tie)
        else:
            space = self.space
            while not space.is_axis:
                space = space.parent
            assert space in right.routes
            left = self.assembler.inject(space.parent, left)
            tie = SeriesTie(space)
        routes = {}
        for key in left.routes:
            routes[key] = [LEFT] + left.routes[key]
        routes[self.space] = [RIGHT] + right.routes[self.space]
        return JoinTerm(left, right, ties, False,
                        left.space, left.baseline, routes, left.mark)


class AssembleOrdered(AssembleSpace):

    adapts(OrderedSpace, Assembler)

    def assemble(self, baseline):
        child = self.assembler.assemble(self.space.parent, baseline)
        for code, dir in self.space.order:
            child = self.assembler.inject(code, child)
        assert self.space not in child.routes
        routes = {}
        for key in child.routes:
            routes[key] = [FORWARD] + child.routes[key]
        routes[self.space] = [FORWARD] + child.routes[self.space.parent]
        return OrderingTerm(child, self.space.order,
                            self.space.limit, self.space.offset,
                            self.space, baseline, routes, self.space.mark)


class AssembleColumn(AssembleUnit):

    adapts(ColumnUnit, Assembler)

    def inject(self, term):
        return self.assembler.inject(self.code.space, term)


class AssembleAggregate(AssembleUnit):

    adapts(AggregateUnit, Assembler)

    def inject(self, term):
        assert term.space.spans(self.code.space)
        assert not term.space.spans(self.code.plural_space)
        assert not self.code.space.spans(self.code.plural_space)
        assert self.code.plural_space.spans(self.code.space)
        with_base_term = (not self.code.space.dominates(term.space))
        if with_base_term:
            base_space = self.code.space
        else:
            base_space = term.space
            left = term
        base_backbone = base_space.axes()
        plural_space = self.code.plural_space.inflate(base_space)
        baseline = plural_space
        while not base_space.concludes(baseline.parent):
            baseline = baseline.parent
        baseline = baseline.axes()
        plural_term = self.assembler.assemble(plural_space, baseline)
        plural_term = self.assembler.inject(self.code.expression, plural_term)
        ties = []
        axes = []
        if (base_backbone.concludes(baseline)
                or baseline.parent in plural_term.routes):
            axis = baseline
            while axis in plural_term.routes:
                axes.append(axis)
                tie = ParallelTie(axis)
                ties.append(tie)
                axis = axis.parent
        else:
            axes.append(baseline)
            tie = SeriesTie(baseline)
            ties.append(tie)
        relative_space = RelativeSpace(baseline.parent,
                                       plural_space, self.code.mark)
        routes = {}
        for axis in axes:
            routes[axis] = [FORWARD] + plural_term.routes[axis]
        routes[self.code] = []
        projected_term = ProjectionTerm(plural_term, ties, relative_space,
                                        baseline.parent, routes,
                                        self.code.mark)
        if with_base_term:
            assert False
        if (base_backbone.concludes(baseline)
                or baseline.parent in plural_term.routes):
            for axis in axes:
                term = self.assembler.inject(axis, term)
        else:
            for axis in axes:
                term = self.assembler.inject(axis.parent, term)
        left = term
        right = projected_term
        routes = {}
        for key in left.routes:
            routes[key] = [LEFT] + left.routes[key]
        routes[self.code] = [RIGHT]
        return JoinTerm(left, right, ties, False,
                        left.space, left.baseline, routes, left.mark)


class AssembleSegment(Assemble):

    adapts(SegmentCode, Assembler)

    def assemble(self):
        child = self.assembler.assemble(self.code.space, self.code.space.scalar)
        child = self.assembler.inject(self.code, child)
        select = self.code.elements
        routes = {}
        for key in child.routes:
            routes[key] = [FORWARD] + child.routes[key]
        return SegmentTerm(child, select, child.space, child.baseline,
                           routes, self.code.mark)

    def inject(self, term):
        for element in self.code.elements:
            for unit in element.get_units():
                term = self.assembler.inject(unit, term)
        return term


class AssembleQuery(Assemble):

    adapts(QueryCode, Assembler)

    def assemble(self):
        segment = None
        if self.code.segment is not None:
            segment = self.assembler.assemble(self.code.segment)
        return QueryTerm(self.code, segment, self.code.mark)


assemble_adapters = find_adapters()