def new_binop(lexpr, rexpr, op): return pgast.Expr( kind=pgast.ExprKind.OP, name=op, lexpr=lexpr, rexpr=rexpr )
def new_unop(op, expr): return pgast.Expr(kind=pgast.ExprKind.OP, name=op, rexpr=expr)
def compile_UnaryOp(expr: irast.Base, *, ctx: context.CompilerContextLevel) -> pgast.Base: with ctx.new() as subctx: subctx.expr_exposed = False operand = dispatch.compile(expr.expr, ctx=subctx) return pgast.Expr(name=expr.op, rexpr=operand, kind=pgast.ExprKind.OP)
def compile_OperatorCall( expr: irast.OperatorCall, *, ctx: context.CompilerContextLevel) -> pgast.Expr: if expr.typemod is ql_ft.TypeModifier.SET_OF: raise RuntimeError( f'set returning operator {expr.func_shortname!r} is not supported ' f'in simple expressions') args = [dispatch.compile(a, ctx=ctx) for a in expr.args] if expr.operator_kind is ql_ft.OperatorKind.INFIX: lexpr, rexpr = args elif expr.operator_kind is ql_ft.OperatorKind.PREFIX: rexpr = args[0] lexpr = None elif expr.operator_kind is ql_ft.OperatorKind.POSTFIX: lexpr = args[0] rexpr = None else: raise RuntimeError(f'unexpected operator kind: {expr.operator_kind!r}') if expr.sql_operator: sql_oper = expr.sql_operator[0] if len(expr.sql_operator) > 1: # Explicit operand types given in FROM SQL OPERATOR if lexpr is not None: lexpr = pgast.TypeCast( arg=lexpr, type_name=pgast.TypeName( name=(expr.sql_operator[1],) ) ) if rexpr is not None: rexpr = pgast.TypeCast( arg=rexpr, type_name=pgast.TypeName( name=(expr.sql_operator[2],) ) ) else: sql_oper = common.get_backend_operator_name(expr.func_shortname)[1] result = pgast.Expr( kind=pgast.ExprKind.OP, name=sql_oper, lexpr=lexpr, rexpr=rexpr, ) if expr.force_return_cast: # The underlying operator has a return value type # different from that of the EdgeQL operator declaration, # so we need to make an explicit cast here. result = pgast.TypeCast( arg=result, type_name=pgast.TypeName( name=pg_types.pg_type_from_object( ctx.env.schema, expr.stype) ) ) return result