-
Notifications
You must be signed in to change notification settings - Fork 390
/
pathmerger.py
854 lines (671 loc) · 27.6 KB
/
pathmerger.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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
##
# Copyright (c) 2016 MagicStack Inc.
# All rights reserved.
#
# See LICENSE for details.
##
import collections
from edgedb.lang.common import ast
from edgedb.lang.common import debug
from edgedb.lang.common.algos import boolean
from edgedb.lang.common import markup # NOQA
from edgedb.lang.ir import ast as irast
from edgedb.lang.ir import utils as irutils
def fixup_refs(refs, newref):
irast.Base.fixup_refs(refs, newref)
def is_weak_op(context, op):
return irutils.is_weak_op(op) or context.current.location != 'generator'
def flatten_and_unify_path_combination(
context, expr, deep=False, merge_filters=False, memo=None):
# Flatten nested disjunctions and conjunctions since
# they are associative.
#
assert isinstance(expr, irast.PathCombination)
irutils.flatten_path_combination(expr)
if deep:
newpaths = set()
for path in expr.paths:
path = PathMerger.run(path, context=context, memo=memo)
newpaths.add(path)
expr = expr.__class__(paths=frozenset(newpaths))
unify_paths(
context, expr.paths, mode=expr.__class__, merge_filters=merge_filters)
expr.paths = frozenset(p for p in expr.paths)
return expr
@debug.debug
def unify_paths(context, paths, mode, reverse=True, merge_filters=False):
mypaths = set(paths)
result = None
while mypaths and not result:
result = irutils.extract_paths(mypaths.pop(), reverse=reverse)
if result is not None:
paths_to_merge = set()
for path in mypaths:
path2 = irutils.extract_paths(path, reverse=reverse)
if path2 is not None and path2 is not result:
paths_to_merge.add(path2)
if paths_to_merge:
"""LOG [path.merge] PREPARING TO UNIFY
markup.dump([result] + list(paths_to_merge))
"""
result = _unify_paths(
context, result, paths_to_merge, mode, merge_filters)
"""LOG [path.merge] UNIFICATION RESULT
markup.dump(result)
"""
return result
nest = 0
@debug.debug
def _unify_paths(context, result, paths, mode, merge_filters=False):
global nest
mypaths = set(paths)
while mypaths:
path = mypaths.pop()
if result is path:
continue
if issubclass(mode, irast.Disjunction):
"""LOG [path.merge] ADDING
print(' ' * nest, 'ADDING', result, path,
getattr(result, 'id', '??'),
getattr(path, 'id', '??'), merge_filters)
nest += 2
markup.dump(result)
markup.dump(path)
"""
result = add_paths(
context, result, path, merge_filters=merge_filters)
assert result
"""LOG [path.merge] ADDITION RESULT
nest -= 2
markup.dump(result)
"""
else:
"""LOG [path.merge] INTERSECTING
print(' ' * nest, result, path,
getattr(result, 'id', '??'),
getattr(path, 'id', '??'), merge_filters)
nest += 2
"""
result = intersect_paths(
context, result, path, merge_filters=merge_filters)
assert result
"""LOG [path.merge] INTERSECTION RESULT
markup.dump(result)
nest -= 2
"""
return result
def miniterms_from_conjunctions(paths):
variables = collections.OrderedDict()
terms = []
for path in paths:
term = 0
if isinstance(path, irast.Conjunction):
for subpath in path.paths:
if subpath not in variables:
variables[subpath] = len(variables)
term += 1 << variables[subpath]
elif isinstance(path, irast.EntityLink):
if path not in variables:
variables[path] = len(variables)
term += 1 << variables[path]
terms.append(term)
return list(variables), boolean.ints_to_terms(*terms)
def conjunctions_from_miniterms(terms, variables):
paths = set()
for term in terms:
conjpaths = [variables[i] for i, bit in enumerate(term) if bit]
if len(conjpaths) > 1:
paths.add(irast.Conjunction(paths=frozenset(conjpaths)))
else:
paths.add(conjpaths[0])
return paths
def minimize_disjunction(paths):
variables, miniterms = miniterms_from_conjunctions(paths)
minimized = boolean.minimize(miniterms)
paths = conjunctions_from_miniterms(minimized, variables)
result = irast.Disjunction(paths=frozenset(paths))
return result
def add_sets(context, left, right, merge_filters=False):
if left is right:
return left
if merge_filters:
if (isinstance(merge_filters, ast.ops.Operator) and
is_weak_op(context, merge_filters)):
merge_op = ast.ops.OR
else:
merge_op = ast.ops.AND
match = match_prefixes(left, right, ignore_filters=merge_filters)
if match:
if isinstance(left, irast.EntityLink):
left_link = left
left = left.target
else:
left_link = left.rlink
if isinstance(right, irast.EntityLink):
right_link = right
right = right.target
else:
right_link = right.rlink
if left_link:
fixup_refs([right_link], left_link)
if merge_filters and right_link.propfilter:
left_link.propfilter = irutils.extend_binop(
left_link.propfilter,
right_link.propfilter,
op=merge_op)
left_link.proprefs.update(right_link.proprefs)
left_link.users.update(right_link.users)
if right_link.target:
left_link.target = right_link.target
if left and right:
fixup_refs([right], left)
if merge_filters and right.filter:
left.filter = irutils.extend_binop(
left.filter, right.filter, op=ast.ops.AND)
if merge_filters:
paths_left = set()
for dpath in right.disjunction.paths:
if isinstance(dpath,
(irast.EntitySet, irast.EntityLink)):
merged = intersect_paths(context, left.conjunction,
dpath, merge_filters)
if merged is not left.conjunction:
paths_left.add(dpath)
else:
paths_left.add(dpath)
right.disjunction = irast.Disjunction(
paths=frozenset(paths_left))
left.disjunction = add_paths(
context, left.disjunction, right.disjunction, merge_filters)
if merge_filters and merge_op == ast.ops.OR:
left.disjunction.fixed = True
left.atomrefs.update(right.atomrefs)
left.users.update(right.users)
left.joins.update(right.joins)
left.joins.discard(left)
if left.origin is None and right.origin is not None:
left.origin = right.origin
if right.concept.issubclass(left.concept):
left.concept = right.concept
if merge_filters:
left.conjunction = intersect_paths(
context, left.conjunction, right.conjunction,
merge_filters)
# If greedy disjunction merging is requested, we must
# also try to merge disjunctions.
paths = frozenset(left.conjunction.paths) | frozenset(
left.disjunction.paths)
unify_paths(
context,
paths,
mode=irast.Conjunction,
reverse=False,
merge_filters=merge_filters)
left.disjunction.paths = \
left.disjunction.paths - left.conjunction.paths
else:
conjunction = add_paths(
context, left.conjunction, right.conjunction,
merge_filters)
if conjunction.paths:
left.disjunction.update(conjunction)
left.conjunction.paths = frozenset()
if isinstance(left, irast.EntitySet):
return left
elif isinstance(right, irast.EntitySet):
return right
else:
return left_link
else:
result = irast.Disjunction(paths=frozenset((left, right)))
return result
def add_to_disjunction(context, disjunction, path, merge_filters):
# Other operand is a disjunction -- look for path we can merge with,
# if not found, append to disjunction.
for dpath in disjunction.paths:
if isinstance(dpath, (irast.EntityLink, irast.EntitySet)):
merge = add_sets(context, dpath, path, merge_filters)
if merge is dpath:
break
else:
disjunction.update(path)
return disjunction
def add_to_conjunction(context, conjunction, path, merge_filters):
result = None
if merge_filters:
for cpath in conjunction.paths:
if isinstance(cpath, (irast.EntityLink, irast.EntitySet)):
merge = add_sets(context, cpath, path, merge_filters)
if merge is cpath:
result = conjunction
break
if not result:
result = irast.Disjunction(paths=frozenset({conjunction, path}))
return result
def add_disjunctions(context, left, right, merge_filters=False):
result = irast.Disjunction()
result.update(left)
result.update(right)
if len(result.paths) > 1:
unify_paths(
context,
result.paths,
mode=result.__class__,
reverse=False,
merge_filters=merge_filters)
result.paths = frozenset(p for p in result.paths)
return result
def add_conjunction_to_disjunction(context, disjunction, conjunction):
if disjunction.paths and conjunction.paths:
return irast.Disjunction(
paths=frozenset({disjunction, conjunction}))
elif disjunction.paths:
return disjunction
elif conjunction.paths:
return irast.Disjunction(paths=frozenset({conjunction}))
else:
return irast.Disjunction()
def add_conjunctions(context, left, right):
paths = frozenset(p for p in (left, right) if p.paths)
return irast.Disjunction(paths=paths)
def add_paths(context, left, right, merge_filters=False):
if isinstance(left, (irast.EntityLink, irast.EntitySet)):
if isinstance(right, (irast.EntityLink, irast.EntitySet)):
# Both operands are sets -- simply merge them
result = add_sets(context, left, right, merge_filters)
elif isinstance(right, irast.Disjunction):
result = add_to_disjunction(context, right, left, merge_filters)
elif isinstance(right, irast.Conjunction):
result = add_to_conjunction(context, right, left, merge_filters)
elif isinstance(left, irast.Disjunction):
if isinstance(right, (irast.EntityLink, irast.EntitySet)):
result = add_to_disjunction(context, left, right, merge_filters)
elif isinstance(right, irast.Disjunction):
result = add_disjunctions(context, left, right, merge_filters)
elif isinstance(right, irast.Conjunction):
result = add_conjunction_to_disjunction(context, left, right)
elif isinstance(left, irast.Conjunction):
if isinstance(right, (irast.EntityLink, irast.EntitySet)):
result = add_to_conjunction(context, left, right, merge_filters)
elif isinstance(right, irast.Disjunction):
result = add_conjunction_to_disjunction(context, right, left)
elif isinstance(right, irast.Conjunction):
result = add_conjunctions(context, left, right)
else:
assert False, 'unexpected nodes "{!r}", "{!r}"'.format(left, right)
return result
def intersect_sets(context, left, right, merge_filters=False):
if left is right:
return left
match = match_prefixes(left, right, ignore_filters=True)
if match:
if isinstance(left, irast.EntityLink):
left_set = left.target
right_set = right.target
left_link = left
right_link = right
else:
left_set = left
right_set = right
left_link = left.rlink
right_link = right.rlink
if left_link:
fixup_refs([right_link], left_link)
if right_link.propfilter:
left_link.propfilter = irutils.extend_binop(
left_link.propfilter,
right_link.propfilter,
op=ast.ops.AND)
left_link.proprefs.update(right_link.proprefs)
left_link.users.update(right_link.users)
if right_link.target:
left_link.target = right_link.target
if right_set and left_set:
fixup_refs([right_set], left_set)
if right_set.filter:
left_set.filter = irutils.extend_binop(
left_set.filter, right_set.filter, op=ast.ops.AND)
left_set.conjunction = intersect_paths(
context, left_set.conjunction, right_set.conjunction,
merge_filters)
left_set.atomrefs.update(right_set.atomrefs)
left_set.users.update(right_set.users)
left_set.joins.update(right_set.joins)
left_set.joins.discard(left_set)
if left_set.origin is None and right_set.origin is not None:
left_set.origin = right_set.origin
if right_set.concept.issubclass(left_set.concept):
left_set.concept = right_set.concept
disjunction = intersect_paths(
context, left_set.disjunction, right_set.disjunction,
merge_filters)
left_set.disjunction = irast.Disjunction()
if isinstance(disjunction, irast.Disjunction):
unify_paths(
context,
left_set.conjunction.paths | disjunction.paths,
mode=irast.Conjunction,
reverse=False,
merge_filters=merge_filters)
left_set.disjunction = disjunction
if len(left_set.disjunction.paths) == 1:
first_disj = next(iter(left_set.disjunction.paths))
if isinstance(first_disj, irast.Conjunction):
left_set.conjunction = first_disj
left_set.disjunction = irast.Disjunction()
elif disjunction.paths:
left_set.conjunction = intersect_paths(
context, left_set.conjunction, disjunction, merge_filters)
irutils.flatten_path_combination(left_set.conjunction)
if len(left_set.conjunction.paths) == 1:
first_conj = next(iter(left_set.conjunction.paths))
if isinstance(first_conj, irast.Disjunction):
left_set.disjunction = first_conj
left_set.conjunction = irast.Conjunction()
if isinstance(left, irast.EntitySet):
return left
elif isinstance(right, irast.EntitySet):
return right
else:
return left_link
else:
result = irast.Conjunction(paths=frozenset({left, right}))
return result
def intersect_with_disjunction(context, disjunction, path):
result = irast.Conjunction(paths=frozenset((disjunction, path)))
return result
def intersect_with_conjunction(context, conjunction, path):
# Other operand is a disjunction -- look for path we can merge with,
# if not found, append to conjunction.
for cpath in conjunction.paths:
if isinstance(cpath, (irast.EntityLink, irast.EntitySet)):
merge = intersect_sets(context, cpath, path)
if merge is cpath:
break
else:
conjunction = irast.Conjunction(
paths=frozenset(conjunction.paths | {path}))
return conjunction
def intersect_conjunctions(context, left, right, merge_filters=False):
result = irast.Conjunction(paths=left.paths)
result.update(right)
if len(result.paths) > 1:
irutils.flatten_path_combination(result)
unify_paths(
context,
result.paths,
mode=result.__class__,
reverse=False,
merge_filters=merge_filters)
result.paths = frozenset(p for p in result.paths)
return result
def intersect_disjunctions(context, left, right):
"""Produce a conjunction of two disjunctions."""
if left.paths and right.paths:
# (a | b) & (c | d) --> a & c | a & d | b & c | b & d
# We unroll the expression since it is highly probable that
# the resulting conjunctions will merge and we'll get a simpler
# expression which is we further attempt to minimize using boolean
# minimizer.
#
paths = set()
for l in left.paths:
for r in right.paths:
paths.add(intersect_paths(context, l, r))
result = minimize_disjunction(paths)
return result
else:
# Degenerate case
if not left.paths:
paths = right.paths
fixed = right.fixed
elif not right.paths:
paths = left.paths
fixed = left.fixed
if len(paths) <= 1 and not fixed:
return irast.Conjunction(paths=frozenset(paths))
else:
return irast.Disjunction(paths=frozenset(paths), fixed=fixed)
def intersect_disjunction_with_conjunction(context, disjunction, conjunction):
if disjunction.paths and conjunction.paths:
return irast.Disjunction(paths=frozenset(
{disjunction, conjunction}))
elif conjunction.paths:
return conjunction
elif disjunction.paths:
return irast.Conjunction(paths=frozenset({disjunction}))
else:
return irast.Conjunction()
def intersect_paths(context, left, right, merge_filters=False):
if isinstance(left, (irast.EntityLink, irast.EntitySet)):
if isinstance(right, (irast.EntityLink, irast.EntitySet)):
# Both operands are sets -- simply merge them
result = intersect_sets(context, left, right, merge_filters)
elif isinstance(right, irast.Disjunction):
result = intersect_with_disjunction(context, right, left)
elif isinstance(right, irast.Conjunction):
result = intersect_with_conjunction(context, right, left)
elif isinstance(left, irast.Disjunction):
if isinstance(right, (irast.EntityLink, irast.EntitySet)):
result = intersect_with_disjunction(context, left, right)
elif isinstance(right, irast.Disjunction):
result = intersect_disjunctions(context, left, right)
elif isinstance(right, irast.Conjunction):
result = intersect_disjunction_with_conjunction(
context, left, right)
elif isinstance(left, irast.Conjunction):
if isinstance(right, (irast.EntityLink, irast.EntitySet)):
result = intersect_with_conjunction(context, left, right)
elif isinstance(right, irast.Disjunction):
result = intersect_disjunction_with_conjunction(
context, right, left)
elif isinstance(right, irast.Conjunction):
result = intersect_conjunctions(
context, left, right, merge_filters)
return result
@debug.debug
def match_prefixes(our, other, ignore_filters):
result = None
if isinstance(our, irast.EntityLink):
link = our
our_node = our.target
if our_node is None:
our_id = irutils.LinearPath(our.source.id)
our_id.add(link.link_class, link.direction, None)
our_node = our.source
else:
our_id = our_node.id
else:
link = None
our_node = our
our_id = our.id
if isinstance(other, irast.EntityLink):
other_link = other
other_node = other.target
if other_node is None:
other_node = other.source
other_id = irutils.LinearPath(other.source.id)
other_id.add(other_link.link_class, other_link.direction, None)
else:
other_id = other_node.id
else:
other_link = None
other_node = other
other_id = other.id
if our_id[-1] is None and other_id[-1] is not None:
other_id = irutils.LinearPath(other_id)
other_id[-1] = None
if other_id[-1] is None and our_id[-1] is not None:
our_id = irutils.LinearPath(our_id)
our_id[-1] = None
ok = (
(our_node is None and other_node is None)
or (our_node is not None and other_node is not None
and (our_id == other_id
and our_node.pathvar == other_node.pathvar
and (ignore_filters
or (not our_node.filter
and not other_node.filter
and not our_node.conjunction.paths
and not other_node.conjunction.paths))))
and (not link or (link.link_class == other_link.link_class
and link.direction == other_link.direction))
)
"""LOG [path.merge] MATCH PREFIXES
print(' ' * nest, our, other, ignore_filters)
print(' ' * nest, ' PATHS: ', our_id)
print(' ' * nest, ' *** ', other_id)
print(' ' * nest, 'PATHVARS: ',
our_node.pathvar if our_node is not None else None)
print(' ' * nest, ' *** ',
other_node.pathvar if other_node is not None else None)
print(' ' * nest, ' LINK: ', link.link_class if link else None)
print(' ' * nest, ' *** ',
other_link.link_class if other_link else None)
print(' ' * nest, ' DIR: ', link.direction if link else None)
print(' ' * nest, ' *** ',
other_link.direction if other_link else None)
print(' ' * nest, ' EQ: ', ok)
"""
if ok:
if other_link:
result = other_link
else:
result = other_node
"""LOG [path.merge] MATCH PREFIXES RESULT
print(' ' * nest, ' ----> ', result)
"""
return result
class PathMerger(ast.NodeTransformer):
def visit_AtomicRefExpr(self, expr):
if self._context.current.location == 'generator' and expr.inline:
expr.ref.filter = irutils.extend_binop(expr.ref.filter, expr.expr)
self.visit(expr.ref)
arefs = ast.find_children(
expr, lambda i: isinstance(i, irast.AtomicRefSimple))
for aref in arefs:
self.visit(aref)
expr = irast.InlineFilter(expr=expr.ref.filter, ref=expr.ref)
else:
self.visit(expr.expr)
return expr
def visit_LinkPropRefExpr(self, expr):
if self._context.current.location == 'generator' and expr.inline:
prefs = ast.find_children(
expr.expr,
lambda i: (isinstance(i, irast.LinkPropRefSimple)
and i.ref == expr.ref))
expr.ref.proprefs.update(prefs)
expr.ref.propfilter = irutils.extend_binop(expr.ref.propfilter,
expr.expr)
if expr.ref.target:
self.visit(expr.ref.target)
else:
self.visit(expr.ref.source)
expr = irast.InlinePropFilter(
expr=expr.ref.propfilter, ref=expr.ref)
else:
self.visit(expr.expr)
return expr
def visit_BinOp(self, expr):
left = self.visit(expr.left)
right = self.visit(expr.right)
weak_op = is_weak_op(self._context, expr.op)
if weak_op:
combination = irast.Disjunction
else:
combination = irast.Conjunction
paths = set()
for operand in (left, right):
if isinstance(operand,
(irast.InlineFilter, irast.AtomicRefSimple)):
paths.add(operand.ref)
else:
paths.add(operand)
e = combination(paths=frozenset(paths))
merge_filters = \
self._context.current.location != 'generator' or weak_op
if merge_filters:
merge_filters = expr.op
flatten_and_unify_path_combination(
self._context, e, deep=False, merge_filters=merge_filters,
memo=self._memo)
if len(e.paths) > 1:
expr = irast.BinOp(
left=left,
op=expr.op,
right=right,
aggregates=expr.aggregates)
else:
expr = next(iter(e.paths))
return expr
def visit_PathCombination(self, expr):
return flatten_and_unify_path_combination(
self._context, expr, deep=True, memo=self._memo)
def visit_AtomicRefSimple(self, expr):
expr.ref.atomrefs.add(expr)
return expr
def visit_LinkPropRefSimple(self, expr):
expr.ref.proprefs.add(expr)
return expr
def visit_FunctionCall(self, expr):
args = []
for arg in expr.args:
args.append(self.visit(arg))
for sortexpr in expr.agg_sort:
self.visit(sortexpr.expr)
if expr.agg_filter:
self.visit(expr.agg_filter)
for partition_expr in expr.partition:
self.visit(partition_expr)
if (len(args) > 1 or expr.agg_sort or expr.agg_filter or
expr.partition):
# Make sure that function args are properly merged against
# each other. This is simply a matter of unification of the
# conjunction of paths generated by function argument
# expressions.
#
paths = []
for arg in args:
path = irutils.extract_paths(arg, reverse=True)
if path:
paths.append(path)
for sortexpr in expr.agg_sort:
path = irutils.extract_paths(sortexpr, reverse=True)
if path:
paths.append(path)
if expr.agg_filter:
paths.append(
irutils.extract_paths(
expr.agg_filter, reverse=True))
for partition_expr in expr.partition:
path = irutils.extract_paths(partition_expr, reverse=True)
if path:
paths.append(path)
e = irast.Conjunction(paths=frozenset(paths))
flatten_and_unify_path_combination(self._context, e,
memo=self._memo)
expr = expr.__class__(
name=expr.name,
args=args,
aggregates=expr.aggregates,
kwargs=expr.kwargs,
agg_sort=expr.agg_sort,
agg_filter=expr.agg_filter,
window=expr.window,
partition=expr.partition)
return expr
def visit_Sequence(self, expr):
elements = []
for element in expr.elements:
elements.append(self.visit(element))
unify_paths(self._context, paths=elements, mode=irast.Disjunction)
return expr.__class__(
elements=elements, is_array=expr.is_array)
def visit_Record(self, expr):
elements = []
for element in expr.elements:
elements.append(self.visit(element))
unify_paths(self._context, paths=elements, mode=irast.Disjunction)
return expr.__class__(
elements=elements, concept=expr.concept, rlink=expr.rlink)