/
expr.py
753 lines (593 loc) · 26.2 KB
/
expr.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
#
# This source file is part of the EdgeDB open source project.
#
# Copyright 2008-present MagicStack Inc. and the EdgeDB authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""EdgeQL non-statement expression compilation functions."""
import typing
from edb.lang.common import ast
from edb.lang.common import parsing
from edb.lang.ir import ast as irast
from edb.lang.ir import utils as irutils
from edb.lang.schema import basetypes as s_basetypes
from edb.lang.schema import objtypes as s_objtypes
from edb.lang.schema import pointers as s_pointers
from edb.lang.schema import types as s_types
from edb.lang.schema import utils as s_utils
from edb.lang.edgeql import ast as qlast
from edb.lang.edgeql import errors
from . import astutils
from . import context
from . import dispatch
from . import pathctx
from . import setgen
from . import schemactx
from . import typegen
from . import viewgen
from . import func # NOQA
@dispatch.compile.register(qlast.Path)
def compile_Path(
expr: qlast.Base, *, ctx: context.ContextLevel) -> irast.Set:
return setgen.compile_path(expr, ctx=ctx)
@dispatch.compile.register(qlast.BinOp)
def compile_BinOp(
expr: qlast.Base, *, ctx: context.ContextLevel) -> irast.Set:
try_folding = True
if isinstance(expr.op, qlast.SetOperator):
op_node = compile_set_op(expr, ctx=ctx)
try_folding = False
elif isinstance(expr.op, qlast.EquivalenceOperator):
op_node = compile_equivalence_op(expr, ctx=ctx)
elif isinstance(expr.op, ast.ops.MembershipOperator):
op_node = compile_membership_op(expr, ctx=ctx)
try_folding = False
else:
left = dispatch.compile(expr.left, ctx=ctx)
right = dispatch.compile(expr.right, ctx=ctx)
op_node = irast.BinOp(left=left, right=right, op=expr.op)
if try_folding:
folded = try_fold_binop(op_node, ctx=ctx)
if folded is not None:
return folded
return setgen.ensure_set(op_node, ctx=ctx)
@dispatch.compile.register(qlast.IsOp)
def compile_IsOp(
expr: qlast.Base, *, ctx: context.ContextLevel) -> irast.Set:
op_node = compile_type_check_op(expr, ctx=ctx)
folded = try_fold_binop(op_node, ctx=ctx)
if folded is not None:
return folded
return setgen.ensure_set(op_node, ctx=ctx)
@dispatch.compile.register(qlast.Parameter)
def compile_Parameter(
expr: qlast.Base, *, ctx: context.ContextLevel) -> irast.Set:
pt = ctx.arguments.get(expr.name)
if pt is not None and not isinstance(pt, s_types.Type):
pt = s_basetypes.normalize_type(pt, ctx.schema)
return setgen.ensure_set(irast.Parameter(type=pt, name=expr.name), ctx=ctx)
@dispatch.compile.register(qlast.DetachedExpr)
def compile_DetachedExpr(
expr: qlast.DetachedExpr, *, ctx: context.ContextLevel):
with ctx.detached() as subctx:
return dispatch.compile(expr.expr, ctx=subctx)
@dispatch.compile.register(qlast.Set)
def compile_Set(
expr: qlast.Base, *, ctx: context.ContextLevel) -> irast.Base:
if expr.elements:
if len(expr.elements) == 1:
# From the scope perspective, single-element set
# literals are equivalent to a binary UNION with
# an empty set, not to the element.
with ctx.newscope(fenced=True) as scopectx:
ir_set = dispatch.compile(expr.elements[0], ctx=scopectx)
return setgen.scoped_set(ir_set, ctx=scopectx)
else:
elements = flatten_set(expr)
# a set literal is just sugar for a UNION
op = qlast.UNION
bigunion = qlast.BinOp(
left=elements[0],
right=elements[1],
op=op
)
for el in elements[2:]:
bigunion = qlast.BinOp(
left=bigunion,
right=el,
op=op
)
return dispatch.compile(bigunion, ctx=ctx)
else:
return irutils.new_empty_set(ctx.schema, alias=ctx.aliases.get('e'))
@dispatch.compile.register(qlast.Constant)
def compile_Constant(
expr: qlast.Base, *, ctx: context.ContextLevel) -> irast.Base:
ct = s_basetypes.normalize_type(expr.value.__class__, ctx.schema)
return setgen.generated_set(
irast.Constant(value=expr.value, type=ct), ctx=ctx)
@dispatch.compile.register(qlast.EmptyCollection)
def compile_EmptyCollection(
expr: qlast.Base, *, ctx: context.ContextLevel) -> irast.Base:
raise errors.EdgeQLError(
f'could not determine type of empty array',
context=expr.context)
@dispatch.compile.register(qlast.TupleElement)
def compile_TupleElement(
expr: qlast.Base, *, ctx: context.ContextLevel) -> irast.Base:
name = expr.name.name
if expr.name.module:
name = f'{expr.name.module}::{name}'
val = setgen.ensure_set(dispatch.compile(expr.val, ctx=ctx), ctx=ctx)
element = irast.TupleElement(
name=name,
val=val,
)
return element
@dispatch.compile.register(qlast.NamedTuple)
def compile_NamedTuple(
expr: qlast.Base, *, ctx: context.ContextLevel) -> irast.Base:
elements = [dispatch.compile(e, ctx=ctx) for e in expr.elements]
return setgen.generated_set(
irast.Tuple(elements=elements, named=True), ctx=ctx)
@dispatch.compile.register(qlast.Tuple)
def compile_Tuple(
expr: qlast.Base, *, ctx: context.ContextLevel) -> irast.Base:
elements = []
for i, el in enumerate(expr.elements):
element = irast.TupleElement(
name=str(i),
val=dispatch.compile(el, ctx=ctx)
)
elements.append(element)
return setgen.generated_set(irast.Tuple(elements=elements), ctx=ctx)
@dispatch.compile.register(qlast.Array)
def compile_Array(
expr: qlast.Base, *, ctx: context.ContextLevel) -> irast.Base:
elements = [dispatch.compile(e, ctx=ctx) for e in expr.elements]
# check that none of the elements are themselves arrays
for el, expr_el in zip(elements, expr.elements):
if isinstance(irutils.infer_type(el, ctx.schema), s_types.Array):
raise errors.EdgeQLError(
f'nested arrays are not supported',
context=expr_el.context)
return setgen.generated_set(irast.Array(elements=elements), ctx=ctx)
@dispatch.compile.register(qlast.IfElse)
def compile_IfElse(
expr: qlast.IfElse, *, ctx: context.ContextLevel) -> irast.Base:
condition = setgen.ensure_set(
dispatch.compile(expr.condition, ctx=ctx), ctx=ctx)
ql_if_expr = expr.if_expr
ql_else_expr = expr.else_expr
with ctx.newscope(fenced=True) as scopectx:
if_expr = dispatch.compile(ql_if_expr, ctx=scopectx)
with ctx.newscope(fenced=True) as scopectx:
else_expr = dispatch.compile(ql_else_expr, ctx=scopectx)
if_expr_type = irutils.infer_type(if_expr, ctx.schema)
else_expr_type = irutils.infer_type(else_expr, ctx.schema)
result = s_utils.get_class_nearest_common_ancestor(
[if_expr_type, else_expr_type])
if result is None:
raise errors.EdgeQLError(
'if/else clauses must be of related types, got: {}/{}'.format(
if_expr_type.name, else_expr_type.name),
context=expr.context)
return setgen.generated_set(
irast.IfElseExpr(
if_expr=if_expr, else_expr=else_expr, condition=condition),
ctx=ctx
)
@dispatch.compile.register(qlast.UnaryOp)
def compile_UnaryOp(
expr: qlast.Base, *, ctx: context.ContextLevel) -> irast.Set:
if expr.op == qlast.DISTINCT:
return compile_distinct_op(expr, ctx=ctx)
operand = dispatch.compile(expr.operand, ctx=ctx)
if astutils.is_exists_expr_set(operand):
operand.expr.negated = not operand.expr.negated
return operand
unop = irast.UnaryOp(expr=operand, op=expr.op)
result_type = irutils.infer_type(unop, ctx.schema)
real_t = ctx.schema.get('std::anyreal')
if (isinstance(operand.expr, irast.Constant) and
result_type.issubclass(real_t)):
# Fold the operation to constant if possible
if expr.op == ast.ops.UMINUS:
return setgen.ensure_set(
irast.Constant(value=-operand.expr.value, type=result_type),
ctx=ctx)
elif expr.op == ast.ops.UPLUS:
return operand
return setgen.generated_set(unop, ctx=ctx)
@dispatch.compile.register(qlast.ExistsPredicate)
def compile_ExistsPredicate(
expr: qlast.Base, *, ctx: context.ContextLevel) -> irast.Base:
with ctx.new() as exctx:
with exctx.newscope(fenced=True) as opctx:
operand = setgen.scoped_set(
dispatch.compile(expr.expr, ctx=opctx), ctx=opctx)
return setgen.generated_set(
irast.ExistPred(expr=operand), ctx=exctx)
@dispatch.compile.register(qlast.Coalesce)
def compile_Coalesce(
expr: qlast.Base, *, ctx: context.ContextLevel) -> irast.Base:
if all(isinstance(a, qlast.Set) and not a.elements for a in expr.args):
return irutils.new_empty_set(ctx.schema, alias=ctx.aliases.get('e'))
with ctx.newscope() as newctx:
leftmost_arg = larg = setgen.ensure_set(
dispatch.compile(expr.args[0], ctx=newctx), ctx=newctx)
for rarg_ql in expr.args[1:]:
with newctx.new() as nestedscopectx:
with nestedscopectx.newscope(fenced=True) as fencectx:
rarg = setgen.scoped_set(
dispatch.compile(rarg_ql, ctx=fencectx), ctx=fencectx)
coalesce = irast.Coalesce(left=larg, right=rarg)
larg = setgen.generated_set(coalesce, ctx=nestedscopectx)
# Make sure any empty set types are properly resolved
# before entering them into the scope tree.
irutils.infer_type(larg, schema=ctx.schema)
pathctx.register_set_in_scope(leftmost_arg, ctx=ctx)
pathctx.mark_path_as_optional(leftmost_arg.path_id, ctx=ctx)
return larg
@dispatch.compile.register(qlast.TypeCast)
def compile_TypeCast(
expr: qlast.Base, *, ctx: context.ContextLevel) -> irast.Base:
target_typeref = typegen.ql_typeref_to_ir_typeref(expr.type, ctx=ctx)
if (isinstance(expr.expr, qlast.EmptyCollection) and
target_typeref.maintype == 'array'):
ir_expr = irast.Array()
else:
with ctx.new() as subctx:
# We use "exposed" mode in case this is a type of a cast
# that wants view shapes, e.g. a std::json cast. We do
# this wholesale to support tuple and array casts without
# having to analyze the target type (which is cumbersome
# in QL AST).
subctx.expr_exposed = True
ir_expr = dispatch.compile(expr.expr, ctx=subctx)
return setgen.ensure_set(
_cast_expr(expr.type, ir_expr, ctx=ctx,
source_context=expr.expr.context),
ctx=ctx
)
def _cast_expr(
ql_type: qlast.TypeName, ir_expr: irast.Base, *,
source_context: parsing.ParserContext,
ctx: context.ContextLevel) -> irast.Base:
try:
orig_type = irutils.infer_type(ir_expr, ctx.schema)
except errors.EdgeQLError:
# It is possible that the source expression is unresolved
# if the expr is an empty set (or a coalesce of empty sets).
orig_type = None
new_type = typegen.ql_typeref_to_type(ql_type, ctx=ctx)
new_typeref = typegen.ql_typeref_to_ir_typeref(ql_type, ctx=ctx)
json_t = ctx.schema.get('std::json')
if isinstance(orig_type, s_types.Tuple):
if new_type.issubclass(json_t):
# Casting to std::json involves casting each tuple
# element and also keeping the cast around the whole tuple.
# This is to trigger the downstream logic of casting
# objects (in elements of the tuple).
elements = []
for i, n in enumerate(orig_type.element_types):
val = setgen.generated_set(
irast.TupleIndirection(
expr=ir_expr,
name=n
),
ctx=ctx
)
val.path_id = irutils.tuple_indirection_path_id(
ir_expr.path_id, n, orig_type.element_types[n])
val_type = irutils.infer_type(val, ctx.schema)
# Element cast
val = _cast_expr(ql_type, val, ctx=ctx,
source_context=source_context)
elements.append(irast.TupleElement(name=n, val=val))
new_tuple = setgen.ensure_set(
irast.Tuple(named=orig_type.named, elements=elements), ctx=ctx)
return setgen.ensure_set(
irast.TypeCast(expr=new_tuple, type=new_typeref), ctx=ctx)
else:
# For tuple-to-tuple casts we generate a new tuple
# to simplify things on sqlgen side.
if not isinstance(new_type, s_types.Tuple):
raise errors.EdgeQLError(
f'cannot cast tuple to {new_type.name}',
context=source_context)
if len(orig_type.element_types) != len(new_type.element_types):
raise errors.EdgeQLError(
f'cannot cast to {new_type.name}: '
f'number of elements is not the same',
context=source_context)
new_names = list(new_type.element_types)
elements = []
for i, n in enumerate(orig_type.element_types):
val = setgen.generated_set(
irast.TupleIndirection(
expr=ir_expr,
name=n
),
ctx=ctx
)
val.path_id = irutils.tuple_indirection_path_id(
ir_expr.path_id, n, orig_type.element_types[n])
val_type = irutils.infer_type(val, ctx.schema)
new_el_name = new_names[i]
if val_type != new_type.element_types[new_el_name]:
# Element cast
val = _cast_expr(ql_type.subtypes[i], val, ctx=ctx,
source_context=source_context)
elements.append(irast.TupleElement(name=new_el_name, val=val))
return irast.Tuple(named=new_type.named, elements=elements)
elif isinstance(ir_expr, irast.EmptySet):
# For the common case of casting an empty set, we simply
# generate a new EmptySet node of the requested type.
return irutils.new_empty_set(ctx.schema, scls=new_type,
alias=ir_expr.path_id.target.name.name)
elif (isinstance(ir_expr, irast.Set) and
isinstance(ir_expr.expr, irast.Array)):
if new_type.issubclass(json_t):
el_type = ql_type
elif not isinstance(new_type, s_types.Array):
raise errors.EdgeQLError(
f'cannot cast array to {new_type.name}',
context=source_context)
else:
el_type = ql_type.subtypes[0]
casted_els = []
for el in ir_expr.expr.elements:
el = _cast_expr(el_type, el, ctx=ctx,
source_context=source_context)
casted_els.append(el)
ir_expr.expr = irast.Array(elements=casted_els)
return setgen.ensure_set(
irast.TypeCast(expr=ir_expr, type=new_typeref), ctx=ctx)
else:
if new_type.issubclass(json_t) and ir_expr.path_id.is_objtype_path():
# JSON casts of objects are special: we want the full shape
# and not just an identity.
viewgen.compile_view_shapes(ir_expr, ctx=ctx)
return setgen.ensure_set(
irast.TypeCast(expr=ir_expr, type=new_typeref), ctx=ctx)
@dispatch.compile.register(qlast.TypeFilter)
def compile_TypeFilter(
expr: qlast.Base, *, ctx: context.ContextLevel) -> irast.Base:
# Expr[IS Type] expressions.
with ctx.new() as scopectx:
arg = setgen.ensure_set(
dispatch.compile(expr.expr, ctx=scopectx),
ctx=scopectx)
arg_type = irutils.infer_type(arg, ctx.schema)
if not isinstance(arg_type, s_objtypes.ObjectType):
raise errors.EdgeQLError(
f'invalid type filter operand: {arg_type.name} '
f'is not an object type',
context=expr.expr.context)
typ = schemactx.get_schema_type(expr.type.maintype, ctx=ctx)
if not isinstance(typ, s_objtypes.ObjectType):
raise errors.EdgeQLError(
f'invalid type filter operand: {typ.name} is not an object type',
context=expr.type.context)
return setgen.class_indirection_set(arg, typ, optional=False, ctx=ctx)
@dispatch.compile.register(qlast.Indirection)
def compile_Indirection(
expr: qlast.Base, *, ctx: context.ContextLevel) -> irast.Base:
node = dispatch.compile(expr.arg, ctx=ctx)
int_type = schemactx.get_schema_type('std::int64', ctx=ctx)
for indirection_el in expr.indirection:
if isinstance(indirection_el, qlast.Index):
idx = dispatch.compile(indirection_el.index, ctx=ctx)
idx.context = indirection_el.index.context
node = irast.IndexIndirection(expr=node, index=idx,
context=expr.context)
elif isinstance(indirection_el, qlast.Slice):
if indirection_el.start:
start = dispatch.compile(indirection_el.start, ctx=ctx)
else:
start = irast.Constant(value=None, type=int_type)
if indirection_el.stop:
stop = dispatch.compile(indirection_el.stop, ctx=ctx)
else:
stop = irast.Constant(value=None, type=int_type)
node = irast.SliceIndirection(
expr=node, start=start, stop=stop)
else:
raise ValueError('unexpected indirection node: '
'{!r}'.format(indirection_el))
return setgen.ensure_set(node, ctx=ctx)
def try_fold_arithmetic_binop(
op: ast.ops.Operator, left: irast.Set, right: irast.Set, *,
ctx: context.ContextLevel) -> typing.Optional[irast.Set]:
"""Try folding an arithmetic expr into a constant."""
schema = ctx.schema
real_t = schema.get('std::anyreal')
float_t = schema.get('std::anyfloat')
int_t = schema.get('std::anyint')
left_type = irutils.infer_type(left, schema)
right_type = irutils.infer_type(right, schema)
if not left_type.issubclass(real_t) or not right_type.issubclass(real_t):
return
result_type = left_type
if right_type.issubclass(float_t):
result_type = right_type
left = left.expr
right = right.expr
if op == ast.ops.ADD:
value = left.value + right.value
elif op == ast.ops.SUB:
value = left.value - right.value
elif op == ast.ops.MUL:
value = left.value * right.value
elif op == ast.ops.DIV:
if left_type.issubclass(int_t) and right_type.issubclass(int_t):
value = left.value // right.value
else:
value = left.value / right.value
elif op == ast.ops.POW:
value = left.value ** right.value
elif op == ast.ops.MOD:
value = left.value % right.value
else:
value = None
if value is not None:
return setgen.ensure_set(
irast.Constant(value=value, type=result_type), ctx=ctx)
def try_fold_comparison_binop(
op: ast.ops.Operator, left: irast.Set, right: irast.Set, *,
ctx: context.ContextLevel) -> typing.Optional[irast.Set]:
"""Try folding a comparison expr into a constant."""
left = left.expr
right = right.expr
if op == ast.ops.EQ:
value = left.value == right.value
elif op == ast.ops.NE:
value = left.value != right.value
elif op == ast.ops.GT:
value = left.value > right.value
elif op == ast.ops.GE:
value = left.value >= right.value
elif op == ast.ops.LT:
value = left.value < right.value
elif op == ast.ops.LE:
value = left.value <= right.value
else:
value = None
if value is not None:
return setgen.ensure_set(
irast.Constant(value=value, type=ctx.schema.get('std::bool')),
ctx=ctx)
def try_fold_binop(
binop: irast.BinOp, *,
ctx: context.ContextLevel) -> typing.Optional[irast.Set]:
"""Try folding a binary operator expression."""
schema = ctx.schema
real_t = schema.get('std::anyreal')
result_type = irutils.infer_type(binop, schema)
folded = None
left = binop.left
right = binop.right
op = binop.op
if (isinstance(left.expr, irast.Constant) and
isinstance(right.expr, irast.Constant)):
# Left and right nodes are constants.
if isinstance(op, ast.ops.ComparisonOperator):
folded = try_fold_comparison_binop(op, left, right, ctx=ctx)
elif result_type.issubclass(real_t):
folded = try_fold_arithmetic_binop(op, left, right, ctx=ctx)
elif op in {ast.ops.ADD, ast.ops.MUL}:
# Let's check if we have (CONST + (OTHER_CONST + X))
# tree, which can be optimized to ((CONST + OTHER_CONST) + X)
my_const = left
other_binop = right
if isinstance(right.expr, irast.Constant):
my_const, other_binop = other_binop, my_const
if (isinstance(my_const.expr, irast.Constant) and
isinstance(other_binop.expr, irast.BinOp) and
other_binop.expr.op == op):
other_const = other_binop.expr.left
other_binop_node = other_binop.expr.right
if isinstance(other_binop_node.expr, irast.Constant):
other_binop_node, other_const = \
other_const, other_binop_node
if isinstance(other_const.expr, irast.Constant):
new_const = try_fold_arithmetic_binop(
op, other_const, my_const, ctx=ctx)
if new_const is not None:
folded_binop = irast.BinOp(
left=new_const,
right=other_binop_node,
op=op)
folded = setgen.ensure_set(folded_binop, ctx=ctx)
return folded
def compile_type_check_op(
expr: qlast.IsOp, *, ctx: context.ContextLevel) -> irast.TypeCheckOp:
# <Expr> IS <TypeExpr>
left = dispatch.compile(expr.left, ctx=ctx)
ltype = irutils.infer_type(left, ctx.schema)
left = setgen.ptr_step_set(
left, source=ltype, ptr_name=('std', '__type__'),
direction=s_pointers.PointerDirection.Outbound,
source_context=expr.context, ctx=ctx)
pathctx.register_set_in_scope(left, ctx=ctx)
right = typegen.ql_typeref_to_ir_typeref(expr.right, ctx=ctx)
return irast.TypeCheckOp(left=left, right=right, op=expr.op)
def _compile_set_op(
expr: qlast.BinOp, *, ctx: context.ContextLevel) -> irast.Set:
with ctx.newscope(fenced=True) as scopectx:
left = setgen.scoped_set(
dispatch.compile(expr.left, ctx=scopectx),
ctx=scopectx)
with ctx.newscope(fenced=True) as scopectx:
right = setgen.scoped_set(
dispatch.compile(expr.right, ctx=scopectx),
ctx=scopectx)
return setgen.ensure_set(
irast.SetOp(left=left, right=right, op=expr.op), ctx=ctx)
def compile_set_op(
expr: qlast.BinOp, *, ctx: context.ContextLevel) -> irast.Set:
# UNION
return _compile_set_op(expr, ctx=ctx)
def compile_distinct_op(
expr: qlast.UnaryOp, *, ctx: context.ContextLevel) -> irast.DistinctOp:
# DISTINCT(SET OF any A) -> SET OF any
with ctx.newscope(fenced=True) as scopectx:
operand = setgen.scoped_set(
dispatch.compile(expr.operand, ctx=scopectx), ctx=scopectx)
return setgen.generated_set(irast.DistinctOp(expr=operand), ctx=ctx)
def compile_equivalence_op(
expr: qlast.BinOp, *,
ctx: context.ContextLevel) -> irast.EquivalenceOp:
# A ?= B ≣ EQUIV(OPTIONAL any A, OPTIONAL any B) -> std::bool
# Definition:
# | {a = b | ∀ (a, b) ∈ A ⨯ B}, iff A != ∅ ∧ B != ∅
# | {True}, iff A = B = ∅
# | {False}, iff A != ∅ ∧ B = ∅
# | {False}, iff A = ∅ ∧ B != ∅
#
# A ?!= B ≣ NEQUIV(OPTIONAL any A, OPTIONAL any B) -> std::bool
# Definition:
# | {a != b | ∀ (a, b) ∈ A ⨯ B}, iff A != ∅ ∧ B != ∅
# | {False}, iff A = B = ∅
# | {True}, iff A != ∅ ∧ B = ∅
# | {True}, iff A = ∅ ∧ B != ∅
left = setgen.ensure_set(dispatch.compile(expr.left, ctx=ctx), ctx=ctx)
right = setgen.ensure_set(dispatch.compile(expr.right, ctx=ctx), ctx=ctx)
result = irast.EquivalenceOp(left=left, right=right, op=expr.op)
# Make sure any empty set types are properly resolved
# before entering them into the scope tree.
irutils.infer_type(result, schema=ctx.schema)
pathctx.register_set_in_scope(left, ctx=ctx)
pathctx.mark_path_as_optional(left.path_id, ctx=ctx)
pathctx.register_set_in_scope(right, ctx=ctx)
pathctx.mark_path_as_optional(right.path_id, ctx=ctx)
return result
def compile_membership_op(
expr: qlast.BinOp, *, ctx: context.ContextLevel) -> irast.Base:
left = dispatch.compile(expr.left, ctx=ctx)
with ctx.newscope(fenced=True) as scopectx:
# [NOT] IN is an aggregate, so we need to put a scope fence.
right = setgen.scoped_set(
dispatch.compile(expr.right, ctx=scopectx), ctx=scopectx)
op_node = irast.BinOp(left=left, right=right, op=expr.op)
return setgen.generated_set(op_node, ctx=ctx)
def flatten_set(expr: qlast.Set) -> typing.List[qlast.Expr]:
elements = []
for el in expr.elements:
if isinstance(el, qlast.Set):
elements.extend(flatten_set(el))
else:
elements.append(el)
return elements