From ead63fe34690f0fc81031e50234e5801072e2bc7 Mon Sep 17 00:00:00 2001 From: chaotian Date: Thu, 30 Nov 2023 10:15:48 +0800 Subject: [PATCH v3] Printing const-folder expression in ruleutils.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previous coding in get_const_expr() has handled with negative number, opexpr plus number is an exception. (ex. -1 could be a negative const and also could be a opexpr ‘-’ plus number 1). So we needed to hacking up in get_rule_sortgroupclause() for Opexpr cases and pre-calculate opexpr plus number to real const by expression_planner(). There are two operators we need to worried about namely in4um and numeric_uminus, and we also defined two macros Int4NegOperator and NumericNegOperator in pg_operator.dat. --- src/backend/utils/adt/ruleutils.c | 25 +++++++++++++++++++++++++ src/include/catalog/pg_operator.dat | 4 ++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index ed7f40f053..6788e63192 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -6317,6 +6317,31 @@ get_rule_sortgroupclause(Index ref, List *tlist, bool force_colno, } else if (expr && IsA(expr, Const)) get_const_expr((Const *) expr, context, 1); + else if (expr && IsA(expr, OpExpr)) + { + OpExpr *opexpr = (OpExpr *)expr; + List *args = opexpr->args; + + /* + * Const-folder the expression(opexpr plus const) to negative const, + * mainly there are two operators we need to worried about, namely + * in4um(Int4NegOperator) and numeric_uminus(NumericNegOperator), + * and get_const_expr() is responsible for end up getting labeled + * with a typecast. + */ + if (list_length(args) == 1) + { + Node *arg = (Node *) linitial(args); + Oid opno = opexpr->opno; + + if ((opno == Int4NegOperator || + opno == NumericNegOperator) && + IsA(arg, Const)) + expr = (Node *)expression_planner((Expr *)expr); + } + + get_rule_expr(expr, context, true); + } else if (!expr || IsA(expr, Var)) get_rule_expr(expr, context, true); else diff --git a/src/include/catalog/pg_operator.dat b/src/include/catalog/pg_operator.dat index b2cdea66c4..88e6825e58 100644 --- a/src/include/catalog/pg_operator.dat +++ b/src/include/catalog/pg_operator.dat @@ -575,7 +575,7 @@ { oid => '557', descr => 'subtract', oprname => '-', oprleft => 'int4', oprright => 'int2', oprresult => 'int4', oprcode => 'int42mi' }, -{ oid => '558', descr => 'negate', +{ oid => '558', oid_symbol => 'Int4NegOperator', descr => 'negate', oprname => '-', oprkind => 'l', oprleft => '0', oprright => 'int4', oprresult => 'int4', oprcode => 'int4um' }, { oid => '559', descr => 'negate', @@ -2064,7 +2064,7 @@ oprjoin => 'icnlikejoinsel' }, # NUMERIC type - OID's 1700-1799 -{ oid => '1751', descr => 'negate', +{ oid => '1751', oid_symbol => 'NumericNegOperator', descr => 'negate', oprname => '-', oprkind => 'l', oprleft => '0', oprright => 'numeric', oprresult => 'numeric', oprcode => 'numeric_uminus' }, { oid => '1752', descr => 'equal', -- 2.39.2 (Apple Git-143)