Ejemplo n.º 1
0
def aggregate_array(state):
    # If we are in a nested list search (e.g: Call(args=[Call(args=[Name()])]))
    # we can't directly use `ORDER BY @index` since the EdgeDB can't quite infer
    # which @index are we talking about.
    if len(state.parents) >= 1:
        path = IR.attribute(
            IR.typed(IR.name(_COMPILER_WORKAROUND_FOR_TARGET), state.match),
            state.pointer,
        )
        body = IR.loop(
            IR.name(_COMPILER_WORKAROUND_FOR_TARGET),
            state.parents[-1].compute_path(allow_missing=True),
            IR.select(path, order=IR.property("index")),
        )
    else:
        body = IR.select(state.compute_path(), order=IR.property("index"))

    return IR.call("array_agg", [body])
Ejemplo n.º 2
0
def serialize_sequence(sequence, context):
    ir_set = IR.set([serialize(value, context) for value in sequence])

    if all(isinstance(item, _BASIC_SET_TYPES) for item in sequence):
        # {1, 2, 3} / {<ast::op>'Add', <ast::op>'Sub', ...}
        return ir_set
    else:
        # Inserting a sequence of AST objects would require special
        # attention to calculate the index property.
        target = IR.name("item")
        scope = IR.namespace({"items": ir_set})
        loop = IR.loop(
            target,
            IR.call("enumerate", [IR.name("items")]),
            IR.select(
                IR.attribute(target, 1),
                selections=[
                    IR.assign(IR.property("index"), IR.attribute(target, 0))
                ],
            ),
        )
        return IR.add_namespace(scope, loop)