示例#1
0
def compile_ConfigSet(
    expr: qlast.ConfigSet, *,
    ctx: context.ContextLevel,
) -> irast.Set:

    info = _validate_op(expr, ctx=ctx)
    param_val = setgen.ensure_set(
        dispatch.compile(expr.expr, ctx=ctx), ctx=ctx)

    try:
        ireval.evaluate(param_val, schema=ctx.env.schema)
    except ireval.UnsupportedExpressionError as e:
        raise errors.QueryError(
            f'non-constant expression in CONFIGURE {expr.scope} SET',
            context=expr.expr.context
        ) from e

    config_set = irast.ConfigSet(
        name=info.param_name,
        cardinality=info.cardinality,
        scope=expr.scope,
        requires_restart=info.requires_restart,
        backend_setting=info.backend_setting,
        context=expr.context,
        expr=param_val,
    )
    return setgen.ensure_set(config_set, ctx=ctx)
示例#2
0
文件: config.py 项目: syyunn/edgedb
def compile_ConfigSet(
    expr: qlast.ConfigSet, *,
    ctx: context.ContextLevel,
) -> irast.ConfigSet:

    info = _validate_op(expr, ctx=ctx)
    param_val = dispatch.compile(expr.expr, ctx=ctx)

    try:
        ireval.evaluate(param_val, schema=ctx.env.schema)
    except ireval.UnsupportedExpressionError as e:
        level = 'SYSTEM' if expr.system else 'SESSION'
        raise errors.QueryError(
            f'non-constant expression in CONFIGURE {level} SET',
            context=expr.expr.context
        ) from e

    return irast.ConfigSet(
        name=info.param_name,
        cardinality=info.cardinality,
        system=expr.system,
        requires_restart=info.requires_restart,
        backend_setting=info.backend_setting,
        context=expr.context,
        expr=param_val,
    )
示例#3
0
def compile_UnaryOp(expr: qlast.Base, *,
                    ctx: context.ContextLevel) -> irast.Set:

    result = func.compile_operator(expr,
                                   op_name=expr.op,
                                   qlargs=[expr.operand],
                                   ctx=ctx)

    try:
        result = ireval.evaluate(result, schema=ctx.env.schema)
    except ireval.UnsupportedExpressionError:
        pass

    return setgen.ensure_set(result, ctx=ctx)
示例#4
0
def try_fold_binop(opcall: irast.OperatorCall, *,
                   ctx: context.ContextLevel) -> typing.Optional[irast.Set]:
    try:
        const = ireval.evaluate(opcall, schema=ctx.env.schema)
    except ireval.UnsupportedExpressionError:
        anyreal = ctx.env.schema.get('std::anyreal')

        if (opcall.func_shortname in ('std::+', 'std::*')
                and opcall.operator_kind is ft.OperatorKind.INFIX and all(
                    setgen.get_set_type(a.expr, ctx=ctx).issubclass(
                        ctx.env.schema, anyreal) for a in opcall.args)):
            return try_fold_associative_binop(opcall, ctx=ctx)
    else:
        return setgen.ensure_set(const, ctx=ctx)
示例#5
0
文件: expr.py 项目: yew1eb/edgedb
def try_fold_associative_binop(
        opcall: irast.OperatorCall, *,
        ctx: context.ContextLevel) -> typing.Optional[irast.Set]:

    # Let's check if we have (CONST + (OTHER_CONST + X))
    # tree, which can be optimized to ((CONST + OTHER_CONST) + X)

    op = opcall.func_shortname
    my_const = opcall.args[0].expr
    other_binop = opcall.args[1].expr
    folded = None

    if isinstance(other_binop.expr, irast.BaseConstant):
        my_const, other_binop = other_binop, my_const

    if (isinstance(my_const.expr, irast.BaseConstant)
            and isinstance(other_binop.expr, irast.OperatorCall)
            and other_binop.expr.func_shortname == op
            and other_binop.expr.operator_kind is ft.OperatorKind.INFIX):

        other_const = other_binop.expr.args[0].expr
        other_binop_node = other_binop.expr.args[1].expr

        if isinstance(other_binop_node.expr, irast.BaseConstant):
            other_binop_node, other_const = \
                other_const, other_binop_node

        if isinstance(other_const.expr, irast.BaseConstant):
            try:
                new_const = ireval.evaluate(
                    irast.OperatorCall(
                        args=[
                            irast.CallArg(expr=other_const, ),
                            irast.CallArg(expr=my_const, ),
                        ],
                        func_module_id=opcall.func_module_id,
                        func_shortname=op,
                        func_polymorphic=opcall.func_polymorphic,
                        func_sql_function=opcall.func_sql_function,
                        sql_operator=opcall.sql_operator,
                        force_return_cast=opcall.force_return_cast,
                        operator_kind=opcall.operator_kind,
                        params_typemods=opcall.params_typemods,
                        context=opcall.context,
                        typeref=opcall.typeref,
                        typemod=opcall.typemod,
                    ),
                    schema=ctx.env.schema,
                )
            except ireval.UnsupportedExpressionError:
                pass
            else:
                folded_binop = irast.OperatorCall(
                    args=[
                        irast.CallArg(expr=setgen.ensure_set(new_const,
                                                             ctx=ctx), ),
                        irast.CallArg(expr=other_binop_node, ),
                    ],
                    func_module_id=opcall.func_module_id,
                    func_shortname=op,
                    func_polymorphic=opcall.func_polymorphic,
                    func_sql_function=opcall.func_sql_function,
                    sql_operator=opcall.sql_operator,
                    force_return_cast=opcall.force_return_cast,
                    operator_kind=opcall.operator_kind,
                    params_typemods=opcall.params_typemods,
                    context=opcall.context,
                    typeref=opcall.typeref,
                    typemod=opcall.typemod,
                )

                folded = setgen.ensure_set(folded_binop, ctx=ctx)

    return folded