LCOV - code coverage report
Current view: top level - src/backend/parser - parse_expr.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 880 1198 73.5 %
Date: 2019-11-13 23:06:49 Functions: 37 39 94.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * parse_expr.c
       4             :  *    handle expressions in parser
       5             :  *
       6             :  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/parser/parse_expr.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             :  */
      15             : 
      16             : #include "postgres.h"
      17             : 
      18             : #include "catalog/pg_type.h"
      19             : #include "commands/dbcommands.h"
      20             : #include "miscadmin.h"
      21             : #include "nodes/makefuncs.h"
      22             : #include "nodes/nodeFuncs.h"
      23             : #include "optimizer/optimizer.h"
      24             : #include "parser/analyze.h"
      25             : #include "parser/parse_agg.h"
      26             : #include "parser/parse_clause.h"
      27             : #include "parser/parse_coerce.h"
      28             : #include "parser/parse_collate.h"
      29             : #include "parser/parse_expr.h"
      30             : #include "parser/parse_func.h"
      31             : #include "parser/parse_oper.h"
      32             : #include "parser/parse_relation.h"
      33             : #include "parser/parse_target.h"
      34             : #include "parser/parse_type.h"
      35             : #include "utils/builtins.h"
      36             : #include "utils/date.h"
      37             : #include "utils/lsyscache.h"
      38             : #include "utils/timestamp.h"
      39             : #include "utils/xml.h"
      40             : 
      41             : /* GUC parameters */
      42             : bool        operator_precedence_warning = false;
      43             : bool        Transform_null_equals = false;
      44             : 
      45             : /*
      46             :  * Node-type groups for operator precedence warnings
      47             :  * We use zero for everything not otherwise classified
      48             :  */
      49             : #define PREC_GROUP_POSTFIX_IS   1   /* postfix IS tests (NullTest, etc) */
      50             : #define PREC_GROUP_INFIX_IS     2   /* infix IS (IS DISTINCT FROM, etc) */
      51             : #define PREC_GROUP_LESS         3   /* < > */
      52             : #define PREC_GROUP_EQUAL        4   /* = */
      53             : #define PREC_GROUP_LESS_EQUAL   5   /* <= >= <> */
      54             : #define PREC_GROUP_LIKE         6   /* LIKE ILIKE SIMILAR */
      55             : #define PREC_GROUP_BETWEEN      7   /* BETWEEN */
      56             : #define PREC_GROUP_IN           8   /* IN */
      57             : #define PREC_GROUP_NOT_LIKE     9   /* NOT LIKE/ILIKE/SIMILAR */
      58             : #define PREC_GROUP_NOT_BETWEEN  10  /* NOT BETWEEN */
      59             : #define PREC_GROUP_NOT_IN       11  /* NOT IN */
      60             : #define PREC_GROUP_POSTFIX_OP   12  /* generic postfix operators */
      61             : #define PREC_GROUP_INFIX_OP     13  /* generic infix operators */
      62             : #define PREC_GROUP_PREFIX_OP    14  /* generic prefix operators */
      63             : 
      64             : /*
      65             :  * Map precedence groupings to old precedence ordering
      66             :  *
      67             :  * Old precedence order:
      68             :  * 1. NOT
      69             :  * 2. =
      70             :  * 3. < >
      71             :  * 4. LIKE ILIKE SIMILAR
      72             :  * 5. BETWEEN
      73             :  * 6. IN
      74             :  * 7. generic postfix Op
      75             :  * 8. generic Op, including <= => <>
      76             :  * 9. generic prefix Op
      77             :  * 10. IS tests (NullTest, BooleanTest, etc)
      78             :  *
      79             :  * NOT BETWEEN etc map to BETWEEN etc when considered as being on the left,
      80             :  * but to NOT when considered as being on the right, because of the buggy
      81             :  * precedence handling of those productions in the old grammar.
      82             :  */
      83             : static const int oldprecedence_l[] = {
      84             :     0, 10, 10, 3, 2, 8, 4, 5, 6, 4, 5, 6, 7, 8, 9
      85             : };
      86             : static const int oldprecedence_r[] = {
      87             :     0, 10, 10, 3, 2, 8, 4, 5, 6, 1, 1, 1, 7, 8, 9
      88             : };
      89             : 
      90             : static Node *transformExprRecurse(ParseState *pstate, Node *expr);
      91             : static Node *transformParamRef(ParseState *pstate, ParamRef *pref);
      92             : static Node *transformAExprOp(ParseState *pstate, A_Expr *a);
      93             : static Node *transformAExprOpAny(ParseState *pstate, A_Expr *a);
      94             : static Node *transformAExprOpAll(ParseState *pstate, A_Expr *a);
      95             : static Node *transformAExprDistinct(ParseState *pstate, A_Expr *a);
      96             : static Node *transformAExprNullIf(ParseState *pstate, A_Expr *a);
      97             : static Node *transformAExprOf(ParseState *pstate, A_Expr *a);
      98             : static Node *transformAExprIn(ParseState *pstate, A_Expr *a);
      99             : static Node *transformAExprBetween(ParseState *pstate, A_Expr *a);
     100             : static Node *transformBoolExpr(ParseState *pstate, BoolExpr *a);
     101             : static Node *transformFuncCall(ParseState *pstate, FuncCall *fn);
     102             : static Node *transformMultiAssignRef(ParseState *pstate, MultiAssignRef *maref);
     103             : static Node *transformCaseExpr(ParseState *pstate, CaseExpr *c);
     104             : static Node *transformSubLink(ParseState *pstate, SubLink *sublink);
     105             : static Node *transformArrayExpr(ParseState *pstate, A_ArrayExpr *a,
     106             :                                 Oid array_type, Oid element_type, int32 typmod);
     107             : static Node *transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault);
     108             : static Node *transformCoalesceExpr(ParseState *pstate, CoalesceExpr *c);
     109             : static Node *transformMinMaxExpr(ParseState *pstate, MinMaxExpr *m);
     110             : static Node *transformSQLValueFunction(ParseState *pstate,
     111             :                                        SQLValueFunction *svf);
     112             : static Node *transformXmlExpr(ParseState *pstate, XmlExpr *x);
     113             : static Node *transformXmlSerialize(ParseState *pstate, XmlSerialize *xs);
     114             : static Node *transformBooleanTest(ParseState *pstate, BooleanTest *b);
     115             : static Node *transformCurrentOfExpr(ParseState *pstate, CurrentOfExpr *cexpr);
     116             : static Node *transformColumnRef(ParseState *pstate, ColumnRef *cref);
     117             : static Node *transformWholeRowRef(ParseState *pstate, RangeTblEntry *rte,
     118             :                                   int location);
     119             : static Node *transformIndirection(ParseState *pstate, A_Indirection *ind);
     120             : static Node *transformTypeCast(ParseState *pstate, TypeCast *tc);
     121             : static Node *transformCollateClause(ParseState *pstate, CollateClause *c);
     122             : static Node *make_row_comparison_op(ParseState *pstate, List *opname,
     123             :                                     List *largs, List *rargs, int location);
     124             : static Node *make_row_distinct_op(ParseState *pstate, List *opname,
     125             :                                   RowExpr *lrow, RowExpr *rrow, int location);
     126             : static Expr *make_distinct_op(ParseState *pstate, List *opname,
     127             :                               Node *ltree, Node *rtree, int location);
     128             : static Node *make_nulltest_from_distinct(ParseState *pstate,
     129             :                                          A_Expr *distincta, Node *arg);
     130             : static int  operator_precedence_group(Node *node, const char **nodename);
     131             : static void emit_precedence_warnings(ParseState *pstate,
     132             :                                      int opgroup, const char *opname,
     133             :                                      Node *lchild, Node *rchild,
     134             :                                      int location);
     135             : 
     136             : 
     137             : /*
     138             :  * transformExpr -
     139             :  *    Analyze and transform expressions. Type checking and type casting is
     140             :  *    done here.  This processing converts the raw grammar output into
     141             :  *    expression trees with fully determined semantics.
     142             :  */
     143             : Node *
     144     1687042 : transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
     145             : {
     146             :     Node       *result;
     147             :     ParseExprKind sv_expr_kind;
     148             : 
     149             :     /* Save and restore identity of expression type we're parsing */
     150             :     Assert(exprKind != EXPR_KIND_NONE);
     151     1687042 :     sv_expr_kind = pstate->p_expr_kind;
     152     1687042 :     pstate->p_expr_kind = exprKind;
     153             : 
     154     1687042 :     result = transformExprRecurse(pstate, expr);
     155             : 
     156     1684938 :     pstate->p_expr_kind = sv_expr_kind;
     157             : 
     158     1684938 :     return result;
     159             : }
     160             : 
     161             : static Node *
     162     4450974 : transformExprRecurse(ParseState *pstate, Node *expr)
     163             : {
     164             :     Node       *result;
     165             : 
     166     4450974 :     if (expr == NULL)
     167       55376 :         return NULL;
     168             : 
     169             :     /* Guard against stack overflow due to overly complex expressions */
     170     4395598 :     check_stack_depth();
     171             : 
     172     4395598 :     switch (nodeTag(expr))
     173             :     {
     174             :         case T_ColumnRef:
     175     1712274 :             result = transformColumnRef(pstate, (ColumnRef *) expr);
     176     1711908 :             break;
     177             : 
     178             :         case T_ParamRef:
     179       42488 :             result = transformParamRef(pstate, (ParamRef *) expr);
     180       42484 :             break;
     181             : 
     182             :         case T_A_Const:
     183             :             {
     184      979848 :                 A_Const    *con = (A_Const *) expr;
     185      979848 :                 Value      *val = &con->val;
     186             : 
     187      979848 :                 result = (Node *) make_const(pstate, val, con->location);
     188      979848 :                 break;
     189             :             }
     190             : 
     191             :         case T_A_Indirection:
     192       20426 :             result = transformIndirection(pstate, (A_Indirection *) expr);
     193       20402 :             break;
     194             : 
     195             :         case T_A_ArrayExpr:
     196        2602 :             result = transformArrayExpr(pstate, (A_ArrayExpr *) expr,
     197             :                                         InvalidOid, InvalidOid, -1);
     198        2598 :             break;
     199             : 
     200             :         case T_TypeCast:
     201      361764 :             result = transformTypeCast(pstate, (TypeCast *) expr);
     202      360620 :             break;
     203             : 
     204             :         case T_CollateClause:
     205        2568 :             result = transformCollateClause(pstate, (CollateClause *) expr);
     206        2564 :             break;
     207             : 
     208             :         case T_A_Expr:
     209             :             {
     210      538020 :                 A_Expr     *a = (A_Expr *) expr;
     211             : 
     212      538020 :                 switch (a->kind)
     213             :                 {
     214             :                     case AEXPR_OP:
     215      494054 :                         result = transformAExprOp(pstate, a);
     216      493856 :                         break;
     217             :                     case AEXPR_OP_ANY:
     218       11754 :                         result = transformAExprOpAny(pstate, a);
     219       11746 :                         break;
     220             :                     case AEXPR_OP_ALL:
     221         106 :                         result = transformAExprOpAll(pstate, a);
     222         106 :                         break;
     223             :                     case AEXPR_DISTINCT:
     224             :                     case AEXPR_NOT_DISTINCT:
     225         494 :                         result = transformAExprDistinct(pstate, a);
     226         494 :                         break;
     227             :                     case AEXPR_NULLIF:
     228         378 :                         result = transformAExprNullIf(pstate, a);
     229         378 :                         break;
     230             :                     case AEXPR_OF:
     231          28 :                         result = transformAExprOf(pstate, a);
     232          28 :                         break;
     233             :                     case AEXPR_IN:
     234       28986 :                         result = transformAExprIn(pstate, a);
     235       28982 :                         break;
     236             :                     case AEXPR_LIKE:
     237             :                     case AEXPR_ILIKE:
     238             :                     case AEXPR_SIMILAR:
     239             :                         /* we can transform these just like AEXPR_OP */
     240        1846 :                         result = transformAExprOp(pstate, a);
     241        1842 :                         break;
     242             :                     case AEXPR_BETWEEN:
     243             :                     case AEXPR_NOT_BETWEEN:
     244             :                     case AEXPR_BETWEEN_SYM:
     245             :                     case AEXPR_NOT_BETWEEN_SYM:
     246         374 :                         result = transformAExprBetween(pstate, a);
     247         374 :                         break;
     248             :                     case AEXPR_PAREN:
     249           0 :                         result = transformExprRecurse(pstate, a->lexpr);
     250           0 :                         break;
     251             :                     default:
     252           0 :                         elog(ERROR, "unrecognized A_Expr kind: %d", a->kind);
     253             :                         result = NULL;  /* keep compiler quiet */
     254             :                         break;
     255             :                 }
     256      537806 :                 break;
     257             :             }
     258             : 
     259             :         case T_BoolExpr:
     260      150454 :             result = transformBoolExpr(pstate, (BoolExpr *) expr);
     261      150440 :             break;
     262             : 
     263             :         case T_FuncCall:
     264      388692 :             result = transformFuncCall(pstate, (FuncCall *) expr);
     265      388210 :             break;
     266             : 
     267             :         case T_MultiAssignRef:
     268         172 :             result = transformMultiAssignRef(pstate, (MultiAssignRef *) expr);
     269         168 :             break;
     270             : 
     271             :         case T_GroupingFunc:
     272         176 :             result = transformGroupingFunc(pstate, (GroupingFunc *) expr);
     273         176 :             break;
     274             : 
     275             :         case T_NamedArgExpr:
     276             :             {
     277         772 :                 NamedArgExpr *na = (NamedArgExpr *) expr;
     278             : 
     279         772 :                 na->arg = (Expr *) transformExprRecurse(pstate, (Node *) na->arg);
     280         772 :                 result = expr;
     281         772 :                 break;
     282             :             }
     283             : 
     284             :         case T_SubLink:
     285       61422 :             result = transformSubLink(pstate, (SubLink *) expr);
     286       61374 :             break;
     287             : 
     288             :         case T_CaseExpr:
     289       61924 :             result = transformCaseExpr(pstate, (CaseExpr *) expr);
     290       61920 :             break;
     291             : 
     292             :         case T_RowExpr:
     293        6032 :             result = transformRowExpr(pstate, (RowExpr *) expr, false);
     294        6032 :             break;
     295             : 
     296             :         case T_CoalesceExpr:
     297       17226 :             result = transformCoalesceExpr(pstate, (CoalesceExpr *) expr);
     298       17222 :             break;
     299             : 
     300             :         case T_MinMaxExpr:
     301         102 :             result = transformMinMaxExpr(pstate, (MinMaxExpr *) expr);
     302         102 :             break;
     303             : 
     304             :         case T_SQLValueFunction:
     305        2320 :             result = transformSQLValueFunction(pstate,
     306             :                                                (SQLValueFunction *) expr);
     307        2320 :             break;
     308             : 
     309             :         case T_XmlExpr:
     310         398 :             result = transformXmlExpr(pstate, (XmlExpr *) expr);
     311         378 :             break;
     312             : 
     313             :         case T_XmlSerialize:
     314          24 :             result = transformXmlSerialize(pstate, (XmlSerialize *) expr);
     315          24 :             break;
     316             : 
     317             :         case T_NullTest:
     318             :             {
     319       15762 :                 NullTest   *n = (NullTest *) expr;
     320             : 
     321       15762 :                 if (operator_precedence_warning)
     322           0 :                     emit_precedence_warnings(pstate, PREC_GROUP_POSTFIX_IS, "IS",
     323           0 :                                              (Node *) n->arg, NULL,
     324             :                                              n->location);
     325             : 
     326       15762 :                 n->arg = (Expr *) transformExprRecurse(pstate, (Node *) n->arg);
     327             :                 /* the argument can be any type, so don't coerce it */
     328       15762 :                 n->argisrow = type_is_rowtype(exprType((Node *) n->arg));
     329       15762 :                 result = expr;
     330       15762 :                 break;
     331             :             }
     332             : 
     333             :         case T_BooleanTest:
     334         130 :             result = transformBooleanTest(pstate, (BooleanTest *) expr);
     335         130 :             break;
     336             : 
     337             :         case T_CurrentOfExpr:
     338         158 :             result = transformCurrentOfExpr(pstate, (CurrentOfExpr *) expr);
     339         158 :             break;
     340             : 
     341             :             /*
     342             :              * In all places where DEFAULT is legal, the caller should have
     343             :              * processed it rather than passing it to transformExpr().
     344             :              */
     345             :         case T_SetToDefault:
     346           0 :             ereport(ERROR,
     347             :                     (errcode(ERRCODE_SYNTAX_ERROR),
     348             :                      errmsg("DEFAULT is not allowed in this context"),
     349             :                      parser_errposition(pstate,
     350             :                                         ((SetToDefault *) expr)->location)));
     351             :             break;
     352             : 
     353             :             /*
     354             :              * CaseTestExpr doesn't require any processing; it is only
     355             :              * injected into parse trees in a fully-formed state.
     356             :              *
     357             :              * Ordinarily we should not see a Var here, but it is convenient
     358             :              * for transformJoinUsingClause() to create untransformed operator
     359             :              * trees containing already-transformed Vars.  The best
     360             :              * alternative would be to deconstruct and reconstruct column
     361             :              * references, which seems expensively pointless.  So allow it.
     362             :              */
     363             :         case T_CaseTestExpr:
     364             :         case T_Var:
     365             :             {
     366       29844 :                 result = (Node *) expr;
     367       29844 :                 break;
     368             :             }
     369             : 
     370             :         default:
     371             :             /* should not reach here */
     372           0 :             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
     373             :             result = NULL;      /* keep compiler quiet */
     374             :             break;
     375             :     }
     376             : 
     377     4393262 :     return result;
     378             : }
     379             : 
     380             : /*
     381             :  * helper routine for delivering "column does not exist" error message
     382             :  *
     383             :  * (Usually we don't have to work this hard, but the general case of field
     384             :  * selection from an arbitrary node needs it.)
     385             :  */
     386             : static void
     387          24 : unknown_attribute(ParseState *pstate, Node *relref, const char *attname,
     388             :                   int location)
     389             : {
     390             :     RangeTblEntry *rte;
     391             : 
     392          32 :     if (IsA(relref, Var) &&
     393           8 :         ((Var *) relref)->varattno == InvalidAttrNumber)
     394             :     {
     395             :         /* Reference the RTE by alias not by actual table name */
     396           0 :         rte = GetRTEByRangeTablePosn(pstate,
     397           0 :                                      ((Var *) relref)->varno,
     398           0 :                                      ((Var *) relref)->varlevelsup);
     399           0 :         ereport(ERROR,
     400             :                 (errcode(ERRCODE_UNDEFINED_COLUMN),
     401             :                  errmsg("column %s.%s does not exist",
     402             :                         rte->eref->aliasname, attname),
     403             :                  parser_errposition(pstate, location)));
     404             :     }
     405             :     else
     406             :     {
     407             :         /* Have to do it by reference to the type of the expression */
     408          24 :         Oid         relTypeId = exprType(relref);
     409             : 
     410          24 :         if (ISCOMPLEX(relTypeId))
     411          12 :             ereport(ERROR,
     412             :                     (errcode(ERRCODE_UNDEFINED_COLUMN),
     413             :                      errmsg("column \"%s\" not found in data type %s",
     414             :                             attname, format_type_be(relTypeId)),
     415             :                      parser_errposition(pstate, location)));
     416          12 :         else if (relTypeId == RECORDOID)
     417          12 :             ereport(ERROR,
     418             :                     (errcode(ERRCODE_UNDEFINED_COLUMN),
     419             :                      errmsg("could not identify column \"%s\" in record data type",
     420             :                             attname),
     421             :                      parser_errposition(pstate, location)));
     422             :         else
     423           0 :             ereport(ERROR,
     424             :                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
     425             :                      errmsg("column notation .%s applied to type %s, "
     426             :                             "which is not a composite type",
     427             :                             attname, format_type_be(relTypeId)),
     428             :                      parser_errposition(pstate, location)));
     429             :     }
     430             : }
     431             : 
     432             : static Node *
     433       20426 : transformIndirection(ParseState *pstate, A_Indirection *ind)
     434             : {
     435       20426 :     Node       *last_srf = pstate->p_last_srf;
     436       20426 :     Node       *result = transformExprRecurse(pstate, ind->arg);
     437       20426 :     List       *subscripts = NIL;
     438       20426 :     int         location = exprLocation(result);
     439             :     ListCell   *i;
     440             : 
     441             :     /*
     442             :      * We have to split any field-selection operations apart from
     443             :      * subscripting.  Adjacent A_Indices nodes have to be treated as a single
     444             :      * multidimensional subscript operation.
     445             :      */
     446       38112 :     foreach(i, ind->indirection)
     447             :     {
     448       17710 :         Node       *n = lfirst(i);
     449             : 
     450       17710 :         if (IsA(n, A_Indices))
     451        6502 :             subscripts = lappend(subscripts, n);
     452       11208 :         else if (IsA(n, A_Star))
     453             :         {
     454           0 :             ereport(ERROR,
     455             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     456             :                      errmsg("row expansion via \"*\" is not supported here"),
     457             :                      parser_errposition(pstate, location)));
     458             :         }
     459             :         else
     460             :         {
     461             :             Node       *newresult;
     462             : 
     463             :             Assert(IsA(n, String));
     464             : 
     465             :             /* process subscripts before this field selection */
     466       11208 :             if (subscripts)
     467          88 :                 result = (Node *) transformContainerSubscripts(pstate,
     468             :                                                                result,
     469             :                                                                exprType(result),
     470             :                                                                InvalidOid,
     471             :                                                                exprTypmod(result),
     472             :                                                                subscripts,
     473             :                                                                NULL);
     474       11208 :             subscripts = NIL;
     475             : 
     476       22416 :             newresult = ParseFuncOrColumn(pstate,
     477       11208 :                                           list_make1(n),
     478       11208 :                                           list_make1(result),
     479             :                                           last_srf,
     480             :                                           NULL,
     481             :                                           false,
     482             :                                           location);
     483       11208 :             if (newresult == NULL)
     484          24 :                 unknown_attribute(pstate, result, strVal(n), location);
     485       11184 :             result = newresult;
     486             :         }
     487             :     }
     488             :     /* process trailing subscripts, if any */
     489       20402 :     if (subscripts)
     490        6266 :         result = (Node *) transformContainerSubscripts(pstate,
     491             :                                                        result,
     492             :                                                        exprType(result),
     493             :                                                        InvalidOid,
     494             :                                                        exprTypmod(result),
     495             :                                                        subscripts,
     496             :                                                        NULL);
     497             : 
     498       20402 :     return result;
     499             : }
     500             : 
     501             : /*
     502             :  * Transform a ColumnRef.
     503             :  *
     504             :  * If you find yourself changing this code, see also ExpandColumnRefStar.
     505             :  */
     506             : static Node *
     507     1712274 : transformColumnRef(ParseState *pstate, ColumnRef *cref)
     508             : {
     509     1712274 :     Node       *node = NULL;
     510     1712274 :     char       *nspname = NULL;
     511     1712274 :     char       *relname = NULL;
     512     1712274 :     char       *colname = NULL;
     513             :     RangeTblEntry *rte;
     514             :     int         levels_up;
     515             :     enum
     516             :     {
     517             :         CRERR_NO_COLUMN,
     518             :         CRERR_NO_RTE,
     519             :         CRERR_WRONG_DB,
     520             :         CRERR_TOO_MANY
     521     1712274 :     }           crerr = CRERR_NO_COLUMN;
     522             :     const char *err;
     523             : 
     524             :     /*
     525             :      * Check to see if the column reference is in an invalid place within the
     526             :      * query.  We allow column references in most places, except in default
     527             :      * expressions and partition bound expressions.
     528             :      */
     529     1712274 :     err = NULL;
     530     1712274 :     switch (pstate->p_expr_kind)
     531             :     {
     532             :         case EXPR_KIND_NONE:
     533             :             Assert(false);      /* can't happen */
     534           0 :             break;
     535             :         case EXPR_KIND_OTHER:
     536             :         case EXPR_KIND_JOIN_ON:
     537             :         case EXPR_KIND_JOIN_USING:
     538             :         case EXPR_KIND_FROM_SUBSELECT:
     539             :         case EXPR_KIND_FROM_FUNCTION:
     540             :         case EXPR_KIND_WHERE:
     541             :         case EXPR_KIND_POLICY:
     542             :         case EXPR_KIND_HAVING:
     543             :         case EXPR_KIND_FILTER:
     544             :         case EXPR_KIND_WINDOW_PARTITION:
     545             :         case EXPR_KIND_WINDOW_ORDER:
     546             :         case EXPR_KIND_WINDOW_FRAME_RANGE:
     547             :         case EXPR_KIND_WINDOW_FRAME_ROWS:
     548             :         case EXPR_KIND_WINDOW_FRAME_GROUPS:
     549             :         case EXPR_KIND_SELECT_TARGET:
     550             :         case EXPR_KIND_INSERT_TARGET:
     551             :         case EXPR_KIND_UPDATE_SOURCE:
     552             :         case EXPR_KIND_UPDATE_TARGET:
     553             :         case EXPR_KIND_GROUP_BY:
     554             :         case EXPR_KIND_ORDER_BY:
     555             :         case EXPR_KIND_DISTINCT_ON:
     556             :         case EXPR_KIND_LIMIT:
     557             :         case EXPR_KIND_OFFSET:
     558             :         case EXPR_KIND_RETURNING:
     559             :         case EXPR_KIND_VALUES:
     560             :         case EXPR_KIND_VALUES_SINGLE:
     561             :         case EXPR_KIND_CHECK_CONSTRAINT:
     562             :         case EXPR_KIND_DOMAIN_CHECK:
     563             :         case EXPR_KIND_FUNCTION_DEFAULT:
     564             :         case EXPR_KIND_INDEX_EXPRESSION:
     565             :         case EXPR_KIND_INDEX_PREDICATE:
     566             :         case EXPR_KIND_ALTER_COL_TRANSFORM:
     567             :         case EXPR_KIND_EXECUTE_PARAMETER:
     568             :         case EXPR_KIND_TRIGGER_WHEN:
     569             :         case EXPR_KIND_PARTITION_EXPRESSION:
     570             :         case EXPR_KIND_CALL_ARGUMENT:
     571             :         case EXPR_KIND_COPY_WHERE:
     572             :         case EXPR_KIND_GENERATED_COLUMN:
     573             :             /* okay */
     574     1712218 :             break;
     575             : 
     576             :         case EXPR_KIND_COLUMN_DEFAULT:
     577          16 :             err = _("cannot use column reference in DEFAULT expression");
     578          16 :             break;
     579             :         case EXPR_KIND_PARTITION_BOUND:
     580          40 :             err = _("cannot use column reference in partition bound expression");
     581          40 :             break;
     582             : 
     583             :             /*
     584             :              * There is intentionally no default: case here, so that the
     585             :              * compiler will warn if we add a new ParseExprKind without
     586             :              * extending this switch.  If we do see an unrecognized value at
     587             :              * runtime, the behavior will be the same as for EXPR_KIND_OTHER,
     588             :              * which is sane anyway.
     589             :              */
     590             :     }
     591     1712274 :     if (err)
     592          56 :         ereport(ERROR,
     593             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     594             :                  errmsg_internal("%s", err),
     595             :                  parser_errposition(pstate, cref->location)));
     596             : 
     597             :     /*
     598             :      * Give the PreParseColumnRefHook, if any, first shot.  If it returns
     599             :      * non-null then that's all, folks.
     600             :      */
     601     1712218 :     if (pstate->p_pre_columnref_hook != NULL)
     602             :     {
     603       17130 :         node = pstate->p_pre_columnref_hook(pstate, cref);
     604       17130 :         if (node != NULL)
     605         990 :             return node;
     606             :     }
     607             : 
     608             :     /*----------
     609             :      * The allowed syntaxes are:
     610             :      *
     611             :      * A        First try to resolve as unqualified column name;
     612             :      *          if no luck, try to resolve as unqualified table name (A.*).
     613             :      * A.B      A is an unqualified table name; B is either a
     614             :      *          column or function name (trying column name first).
     615             :      * A.B.C    schema A, table B, col or func name C.
     616             :      * A.B.C.D  catalog A, schema B, table C, col or func D.
     617             :      * A.*      A is an unqualified table name; means whole-row value.
     618             :      * A.B.*    whole-row value of table B in schema A.
     619             :      * A.B.C.*  whole-row value of table C in schema B in catalog A.
     620             :      *
     621             :      * We do not need to cope with bare "*"; that will only be accepted by
     622             :      * the grammar at the top level of a SELECT list, and transformTargetList
     623             :      * will take care of it before it ever gets here.  Also, "A.*" etc will
     624             :      * be expanded by transformTargetList if they appear at SELECT top level,
     625             :      * so here we are only going to see them as function or operator inputs.
     626             :      *
     627             :      * Currently, if a catalog name is given then it must equal the current
     628             :      * database name; we check it here and then discard it.
     629             :      *----------
     630             :      */
     631     1711228 :     switch (list_length(cref->fields))
     632             :     {
     633             :         case 1:
     634             :             {
     635      583034 :                 Node       *field1 = (Node *) linitial(cref->fields);
     636             : 
     637             :                 Assert(IsA(field1, String));
     638      583034 :                 colname = strVal(field1);
     639             : 
     640             :                 /* Try to identify as an unqualified column */
     641      583034 :                 node = colNameToVar(pstate, colname, false, cref->location);
     642             : 
     643      582998 :                 if (node == NULL)
     644             :                 {
     645             :                     /*
     646             :                      * Not known as a column of any range-table entry.
     647             :                      *
     648             :                      * Try to find the name as a relation.  Note that only
     649             :                      * relations already entered into the rangetable will be
     650             :                      * recognized.
     651             :                      *
     652             :                      * This is a hack for backwards compatibility with
     653             :                      * PostQUEL-inspired syntax.  The preferred form now is
     654             :                      * "rel.*".
     655             :                      */
     656       32494 :                     rte = refnameRangeTblEntry(pstate, NULL, colname,
     657             :                                                cref->location,
     658             :                                                &levels_up);
     659       32494 :                     if (rte)
     660       19170 :                         node = transformWholeRowRef(pstate, rte,
     661             :                                                     cref->location);
     662             :                 }
     663      582998 :                 break;
     664             :             }
     665             :         case 2:
     666             :             {
     667     1128162 :                 Node       *field1 = (Node *) linitial(cref->fields);
     668     1128162 :                 Node       *field2 = (Node *) lsecond(cref->fields);
     669             : 
     670             :                 Assert(IsA(field1, String));
     671     1128162 :                 relname = strVal(field1);
     672             : 
     673             :                 /* Locate the referenced RTE */
     674     1128162 :                 rte = refnameRangeTblEntry(pstate, nspname, relname,
     675             :                                            cref->location,
     676             :                                            &levels_up);
     677     1128146 :                 if (rte == NULL)
     678             :                 {
     679        2334 :                     crerr = CRERR_NO_RTE;
     680        2334 :                     break;
     681             :                 }
     682             : 
     683             :                 /* Whole-row reference? */
     684     1125812 :                 if (IsA(field2, A_Star))
     685             :                 {
     686         486 :                     node = transformWholeRowRef(pstate, rte, cref->location);
     687         486 :                     break;
     688             :                 }
     689             : 
     690             :                 Assert(IsA(field2, String));
     691     1125326 :                 colname = strVal(field2);
     692             : 
     693             :                 /* Try to identify as a column of the RTE */
     694     1125326 :                 node = scanRTEForColumn(pstate, rte, colname, cref->location,
     695             :                                         0, NULL);
     696     1125326 :                 if (node == NULL)
     697             :                 {
     698             :                     /* Try it as a function call on the whole row */
     699         104 :                     node = transformWholeRowRef(pstate, rte, cref->location);
     700         312 :                     node = ParseFuncOrColumn(pstate,
     701         104 :                                              list_make1(makeString(colname)),
     702         104 :                                              list_make1(node),
     703             :                                              pstate->p_last_srf,
     704             :                                              NULL,
     705             :                                              false,
     706             :                                              cref->location);
     707             :                 }
     708     1125326 :                 break;
     709             :             }
     710             :         case 3:
     711             :             {
     712          32 :                 Node       *field1 = (Node *) linitial(cref->fields);
     713          32 :                 Node       *field2 = (Node *) lsecond(cref->fields);
     714          32 :                 Node       *field3 = (Node *) lthird(cref->fields);
     715             : 
     716             :                 Assert(IsA(field1, String));
     717          32 :                 nspname = strVal(field1);
     718             :                 Assert(IsA(field2, String));
     719          32 :                 relname = strVal(field2);
     720             : 
     721             :                 /* Locate the referenced RTE */
     722          32 :                 rte = refnameRangeTblEntry(pstate, nspname, relname,
     723             :                                            cref->location,
     724             :                                            &levels_up);
     725          32 :                 if (rte == NULL)
     726             :                 {
     727          28 :                     crerr = CRERR_NO_RTE;
     728          28 :                     break;
     729             :                 }
     730             : 
     731             :                 /* Whole-row reference? */
     732           4 :                 if (IsA(field3, A_Star))
     733             :                 {
     734           0 :                     node = transformWholeRowRef(pstate, rte, cref->location);
     735           0 :                     break;
     736             :                 }
     737             : 
     738             :                 Assert(IsA(field3, String));
     739           4 :                 colname = strVal(field3);
     740             : 
     741             :                 /* Try to identify as a column of the RTE */
     742           4 :                 node = scanRTEForColumn(pstate, rte, colname, cref->location,
     743             :                                         0, NULL);
     744           4 :                 if (node == NULL)
     745             :                 {
     746             :                     /* Try it as a function call on the whole row */
     747           0 :                     node = transformWholeRowRef(pstate, rte, cref->location);
     748           0 :                     node = ParseFuncOrColumn(pstate,
     749           0 :                                              list_make1(makeString(colname)),
     750           0 :                                              list_make1(node),
     751             :                                              pstate->p_last_srf,
     752             :                                              NULL,
     753             :                                              false,
     754             :                                              cref->location);
     755             :                 }
     756           4 :                 break;
     757             :             }
     758             :         case 4:
     759             :             {
     760           0 :                 Node       *field1 = (Node *) linitial(cref->fields);
     761           0 :                 Node       *field2 = (Node *) lsecond(cref->fields);
     762           0 :                 Node       *field3 = (Node *) lthird(cref->fields);
     763           0 :                 Node       *field4 = (Node *) lfourth(cref->fields);
     764             :                 char       *catname;
     765             : 
     766             :                 Assert(IsA(field1, String));
     767           0 :                 catname = strVal(field1);
     768             :                 Assert(IsA(field2, String));
     769           0 :                 nspname = strVal(field2);
     770             :                 Assert(IsA(field3, String));
     771           0 :                 relname = strVal(field3);
     772             : 
     773             :                 /*
     774             :                  * We check the catalog name and then ignore it.
     775             :                  */
     776           0 :                 if (strcmp(catname, get_database_name(MyDatabaseId)) != 0)
     777             :                 {
     778           0 :                     crerr = CRERR_WRONG_DB;
     779           0 :                     break;
     780             :                 }
     781             : 
     782             :                 /* Locate the referenced RTE */
     783           0 :                 rte = refnameRangeTblEntry(pstate, nspname, relname,
     784             :                                            cref->location,
     785             :                                            &levels_up);
     786           0 :                 if (rte == NULL)
     787             :                 {
     788           0 :                     crerr = CRERR_NO_RTE;
     789           0 :                     break;
     790             :                 }
     791             : 
     792             :                 /* Whole-row reference? */
     793           0 :                 if (IsA(field4, A_Star))
     794             :                 {
     795           0 :                     node = transformWholeRowRef(pstate, rte, cref->location);
     796           0 :                     break;
     797             :                 }
     798             : 
     799             :                 Assert(IsA(field4, String));
     800           0 :                 colname = strVal(field4);
     801             : 
     802             :                 /* Try to identify as a column of the RTE */
     803           0 :                 node = scanRTEForColumn(pstate, rte, colname, cref->location,
     804             :                                         0, NULL);
     805           0 :                 if (node == NULL)
     806             :                 {
     807             :                     /* Try it as a function call on the whole row */
     808           0 :                     node = transformWholeRowRef(pstate, rte, cref->location);
     809           0 :                     node = ParseFuncOrColumn(pstate,
     810           0 :                                              list_make1(makeString(colname)),
     811           0 :                                              list_make1(node),
     812             :                                              pstate->p_last_srf,
     813             :                                              NULL,
     814             :                                              false,
     815             :                                              cref->location);
     816             :                 }
     817           0 :                 break;
     818             :             }
     819             :         default:
     820           0 :             crerr = CRERR_TOO_MANY; /* too many dotted names */
     821           0 :             break;
     822             :     }
     823             : 
     824             :     /*
     825             :      * Now give the PostParseColumnRefHook, if any, a chance.  We pass the
     826             :      * translation-so-far so that it can throw an error if it wishes in the
     827             :      * case that it has a conflicting interpretation of the ColumnRef. (If it
     828             :      * just translates anyway, we'll throw an error, because we can't undo
     829             :      * whatever effects the preceding steps may have had on the pstate.) If it
     830             :      * returns NULL, use the standard translation, or throw a suitable error
     831             :      * if there is none.
     832             :      */
     833     1711176 :     if (pstate->p_post_columnref_hook != NULL)
     834             :     {
     835             :         Node       *hookresult;
     836             : 
     837       38524 :         hookresult = pstate->p_post_columnref_hook(pstate, cref, node);
     838       38512 :         if (node == NULL)
     839       15484 :             node = hookresult;
     840       23028 :         else if (hookresult != NULL)
     841           0 :             ereport(ERROR,
     842             :                     (errcode(ERRCODE_AMBIGUOUS_COLUMN),
     843             :                      errmsg("column reference \"%s\" is ambiguous",
     844             :                             NameListToString(cref->fields)),
     845             :                      parser_errposition(pstate, cref->location)));
     846             :     }
     847             : 
     848             :     /*
     849             :      * Throw error if no translation found.
     850             :      */
     851     1711164 :     if (node == NULL)
     852             :     {
     853         246 :         switch (crerr)
     854             :         {
     855             :             case CRERR_NO_COLUMN:
     856         202 :                 errorMissingColumn(pstate, relname, colname, cref->location);
     857             :                 break;
     858             :             case CRERR_NO_RTE:
     859          44 :                 errorMissingRTE(pstate, makeRangeVar(nspname, relname,
     860             :                                                      cref->location));
     861             :                 break;
     862             :             case CRERR_WRONG_DB:
     863           0 :                 ereport(ERROR,
     864             :                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     865             :                          errmsg("cross-database references are not implemented: %s",
     866             :                                 NameListToString(cref->fields)),
     867             :                          parser_errposition(pstate, cref->location)));
     868             :                 break;
     869             :             case CRERR_TOO_MANY:
     870           0 :                 ereport(ERROR,
     871             :                         (errcode(ERRCODE_SYNTAX_ERROR),
     872             :                          errmsg("improper qualified name (too many dotted names): %s",
     873             :                                 NameListToString(cref->fields)),
     874             :                          parser_errposition(pstate, cref->location)));
     875             :                 break;
     876             :         }
     877             :     }
     878             : 
     879     1710918 :     return node;
     880             : }
     881             : 
     882             : static Node *
     883       42488 : transformParamRef(ParseState *pstate, ParamRef *pref)
     884             : {
     885             :     Node       *result;
     886             : 
     887             :     /*
     888             :      * The core parser knows nothing about Params.  If a hook is supplied,
     889             :      * call it.  If not, or if the hook returns NULL, throw a generic error.
     890             :      */
     891       42488 :     if (pstate->p_paramref_hook != NULL)
     892       42488 :         result = pstate->p_paramref_hook(pstate, pref);
     893             :     else
     894           0 :         result = NULL;
     895             : 
     896       42488 :     if (result == NULL)
     897           4 :         ereport(ERROR,
     898             :                 (errcode(ERRCODE_UNDEFINED_PARAMETER),
     899             :                  errmsg("there is no parameter $%d", pref->number),
     900             :                  parser_errposition(pstate, pref->location)));
     901             : 
     902       42484 :     return result;
     903             : }
     904             : 
     905             : /* Test whether an a_expr is a plain NULL constant or not */
     906             : static bool
     907         968 : exprIsNullConstant(Node *arg)
     908             : {
     909         968 :     if (arg && IsA(arg, A_Const))
     910             :     {
     911          68 :         A_Const    *con = (A_Const *) arg;
     912             : 
     913          68 :         if (con->val.type == T_Null)
     914          20 :             return true;
     915             :     }
     916         948 :     return false;
     917             : }
     918             : 
     919             : static Node *
     920      495900 : transformAExprOp(ParseState *pstate, A_Expr *a)
     921             : {
     922      495900 :     Node       *lexpr = a->lexpr;
     923      495900 :     Node       *rexpr = a->rexpr;
     924             :     Node       *result;
     925             : 
     926      495900 :     if (operator_precedence_warning)
     927             :     {
     928             :         int         opgroup;
     929             :         const char *opname;
     930             : 
     931           0 :         opgroup = operator_precedence_group((Node *) a, &opname);
     932           0 :         if (opgroup > 0)
     933           0 :             emit_precedence_warnings(pstate, opgroup, opname,
     934             :                                      lexpr, rexpr,
     935             :                                      a->location);
     936             : 
     937             :         /* Look through AEXPR_PAREN nodes so they don't affect tests below */
     938           0 :         while (lexpr && IsA(lexpr, A_Expr) &&
     939           0 :                ((A_Expr *) lexpr)->kind == AEXPR_PAREN)
     940           0 :             lexpr = ((A_Expr *) lexpr)->lexpr;
     941           0 :         while (rexpr && IsA(rexpr, A_Expr) &&
     942           0 :                ((A_Expr *) rexpr)->kind == AEXPR_PAREN)
     943           0 :             rexpr = ((A_Expr *) rexpr)->lexpr;
     944             :     }
     945             : 
     946             :     /*
     947             :      * Special-case "foo = NULL" and "NULL = foo" for compatibility with
     948             :      * standards-broken products (like Microsoft's).  Turn these into IS NULL
     949             :      * exprs. (If either side is a CaseTestExpr, then the expression was
     950             :      * generated internally from a CASE-WHEN expression, and
     951             :      * transform_null_equals does not apply.)
     952             :      */
     953      495900 :     if (Transform_null_equals &&
     954           0 :         list_length(a->name) == 1 &&
     955           0 :         strcmp(strVal(linitial(a->name)), "=") == 0 &&
     956           0 :         (exprIsNullConstant(lexpr) || exprIsNullConstant(rexpr)) &&
     957           0 :         (!IsA(lexpr, CaseTestExpr) &&!IsA(rexpr, CaseTestExpr)))
     958           0 :     {
     959           0 :         NullTest   *n = makeNode(NullTest);
     960             : 
     961           0 :         n->nulltesttype = IS_NULL;
     962           0 :         n->location = a->location;
     963             : 
     964           0 :         if (exprIsNullConstant(lexpr))
     965           0 :             n->arg = (Expr *) rexpr;
     966             :         else
     967           0 :             n->arg = (Expr *) lexpr;
     968             : 
     969           0 :         result = transformExprRecurse(pstate, (Node *) n);
     970             :     }
     971      495900 :     else if (lexpr && IsA(lexpr, RowExpr) &&
     972        1738 :              rexpr && IsA(rexpr, SubLink) &&
     973           0 :              ((SubLink *) rexpr)->subLinkType == EXPR_SUBLINK)
     974           0 :     {
     975             :         /*
     976             :          * Convert "row op subselect" into a ROWCOMPARE sublink. Formerly the
     977             :          * grammar did this, but now that a row construct is allowed anywhere
     978             :          * in expressions, it's easier to do it here.
     979             :          */
     980           0 :         SubLink    *s = (SubLink *) rexpr;
     981             : 
     982           0 :         s->subLinkType = ROWCOMPARE_SUBLINK;
     983           0 :         s->testexpr = lexpr;
     984           0 :         s->operName = a->name;
     985           0 :         s->location = a->location;
     986           0 :         result = transformExprRecurse(pstate, (Node *) s);
     987             :     }
     988      495900 :     else if (lexpr && IsA(lexpr, RowExpr) &&
     989        1738 :              rexpr && IsA(rexpr, RowExpr))
     990             :     {
     991             :         /* ROW() op ROW() is handled specially */
     992        1738 :         lexpr = transformExprRecurse(pstate, lexpr);
     993        1738 :         rexpr = transformExprRecurse(pstate, rexpr);
     994             : 
     995        1738 :         result = make_row_comparison_op(pstate,
     996             :                                         a->name,
     997             :                                         castNode(RowExpr, lexpr)->args,
     998             :                                         castNode(RowExpr, rexpr)->args,
     999             :                                         a->location);
    1000             :     }
    1001             :     else
    1002             :     {
    1003             :         /* Ordinary scalar operator */
    1004      494162 :         Node       *last_srf = pstate->p_last_srf;
    1005             : 
    1006      494162 :         lexpr = transformExprRecurse(pstate, lexpr);
    1007      494054 :         rexpr = transformExprRecurse(pstate, rexpr);
    1008             : 
    1009      494000 :         result = (Node *) make_op(pstate,
    1010             :                                   a->name,
    1011             :                                   lexpr,
    1012             :                                   rexpr,
    1013             :                                   last_srf,
    1014             :                                   a->location);
    1015             :     }
    1016             : 
    1017      495698 :     return result;
    1018             : }
    1019             : 
    1020             : static Node *
    1021       11754 : transformAExprOpAny(ParseState *pstate, A_Expr *a)
    1022             : {
    1023       11754 :     Node       *lexpr = a->lexpr;
    1024       11754 :     Node       *rexpr = a->rexpr;
    1025             : 
    1026       11754 :     if (operator_precedence_warning)
    1027           0 :         emit_precedence_warnings(pstate, PREC_GROUP_POSTFIX_OP,
    1028           0 :                                  strVal(llast(a->name)),
    1029             :                                  lexpr, NULL,
    1030             :                                  a->location);
    1031             : 
    1032       11754 :     lexpr = transformExprRecurse(pstate, lexpr);
    1033       11754 :     rexpr = transformExprRecurse(pstate, rexpr);
    1034             : 
    1035       11754 :     return (Node *) make_scalar_array_op(pstate,
    1036             :                                          a->name,
    1037             :                                          true,
    1038             :                                          lexpr,
    1039             :                                          rexpr,
    1040             :                                          a->location);
    1041             : }
    1042             : 
    1043             : static Node *
    1044         106 : transformAExprOpAll(ParseState *pstate, A_Expr *a)
    1045             : {
    1046         106 :     Node       *lexpr = a->lexpr;
    1047         106 :     Node       *rexpr = a->rexpr;
    1048             : 
    1049         106 :     if (operator_precedence_warning)
    1050           0 :         emit_precedence_warnings(pstate, PREC_GROUP_POSTFIX_OP,
    1051           0 :                                  strVal(llast(a->name)),
    1052             :                                  lexpr, NULL,
    1053             :                                  a->location);
    1054             : 
    1055         106 :     lexpr = transformExprRecurse(pstate, lexpr);
    1056         106 :     rexpr = transformExprRecurse(pstate, rexpr);
    1057             : 
    1058         106 :     return (Node *) make_scalar_array_op(pstate,
    1059             :                                          a->name,
    1060             :                                          false,
    1061             :                                          lexpr,
    1062             :                                          rexpr,
    1063             :                                          a->location);
    1064             : }
    1065             : 
    1066             : static Node *
    1067         494 : transformAExprDistinct(ParseState *pstate, A_Expr *a)
    1068             : {
    1069         494 :     Node       *lexpr = a->lexpr;
    1070         494 :     Node       *rexpr = a->rexpr;
    1071             :     Node       *result;
    1072             : 
    1073         494 :     if (operator_precedence_warning)
    1074           0 :         emit_precedence_warnings(pstate, PREC_GROUP_INFIX_IS, "IS",
    1075             :                                  lexpr, rexpr,
    1076             :                                  a->location);
    1077             : 
    1078             :     /*
    1079             :      * If either input is an undecorated NULL literal, transform to a NullTest
    1080             :      * on the other input. That's simpler to process than a full DistinctExpr,
    1081             :      * and it avoids needing to require that the datatype have an = operator.
    1082             :      */
    1083         494 :     if (exprIsNullConstant(rexpr))
    1084          20 :         return make_nulltest_from_distinct(pstate, a, lexpr);
    1085         474 :     if (exprIsNullConstant(lexpr))
    1086           0 :         return make_nulltest_from_distinct(pstate, a, rexpr);
    1087             : 
    1088         474 :     lexpr = transformExprRecurse(pstate, lexpr);
    1089         474 :     rexpr = transformExprRecurse(pstate, rexpr);
    1090             : 
    1091         474 :     if (lexpr && IsA(lexpr, RowExpr) &&
    1092           4 :         rexpr && IsA(rexpr, RowExpr))
    1093             :     {
    1094             :         /* ROW() op ROW() is handled specially */
    1095           4 :         result = make_row_distinct_op(pstate, a->name,
    1096             :                                       (RowExpr *) lexpr,
    1097             :                                       (RowExpr *) rexpr,
    1098             :                                       a->location);
    1099             :     }
    1100             :     else
    1101             :     {
    1102             :         /* Ordinary scalar operator */
    1103         470 :         result = (Node *) make_distinct_op(pstate,
    1104             :                                            a->name,
    1105             :                                            lexpr,
    1106             :                                            rexpr,
    1107             :                                            a->location);
    1108             :     }
    1109             : 
    1110             :     /*
    1111             :      * If it's NOT DISTINCT, we first build a DistinctExpr and then stick a
    1112             :      * NOT on top.
    1113             :      */
    1114         474 :     if (a->kind == AEXPR_NOT_DISTINCT)
    1115          56 :         result = (Node *) makeBoolExpr(NOT_EXPR,
    1116          28 :                                        list_make1(result),
    1117             :                                        a->location);
    1118             : 
    1119         474 :     return result;
    1120             : }
    1121             : 
    1122             : static Node *
    1123         378 : transformAExprNullIf(ParseState *pstate, A_Expr *a)
    1124             : {
    1125         378 :     Node       *lexpr = transformExprRecurse(pstate, a->lexpr);
    1126         378 :     Node       *rexpr = transformExprRecurse(pstate, a->rexpr);
    1127             :     OpExpr     *result;
    1128             : 
    1129         378 :     result = (OpExpr *) make_op(pstate,
    1130             :                                 a->name,
    1131             :                                 lexpr,
    1132             :                                 rexpr,
    1133             :                                 pstate->p_last_srf,
    1134             :                                 a->location);
    1135             : 
    1136             :     /*
    1137             :      * The comparison operator itself should yield boolean ...
    1138             :      */
    1139         378 :     if (result->opresulttype != BOOLOID)
    1140           0 :         ereport(ERROR,
    1141             :                 (errcode(ERRCODE_DATATYPE_MISMATCH),
    1142             :                  errmsg("NULLIF requires = operator to yield boolean"),
    1143             :                  parser_errposition(pstate, a->location)));
    1144         378 :     if (result->opretset)
    1145           0 :         ereport(ERROR,
    1146             :                 (errcode(ERRCODE_DATATYPE_MISMATCH),
    1147             :         /* translator: %s is name of a SQL construct, eg NULLIF */
    1148             :                  errmsg("%s must not return a set", "NULLIF"),
    1149             :                  parser_errposition(pstate, a->location)));
    1150             : 
    1151             :     /*
    1152             :      * ... but the NullIfExpr will yield the first operand's type.
    1153             :      */
    1154         378 :     result->opresulttype = exprType((Node *) linitial(result->args));
    1155             : 
    1156             :     /*
    1157             :      * We rely on NullIfExpr and OpExpr being the same struct
    1158             :      */
    1159         378 :     NodeSetTag(result, T_NullIfExpr);
    1160             : 
    1161         378 :     return (Node *) result;
    1162             : }
    1163             : 
    1164             : /*
    1165             :  * Checking an expression for match to a list of type names. Will result
    1166             :  * in a boolean constant node.
    1167             :  */
    1168             : static Node *
    1169          28 : transformAExprOf(ParseState *pstate, A_Expr *a)
    1170             : {
    1171          28 :     Node       *lexpr = a->lexpr;
    1172             :     Const      *result;
    1173             :     ListCell   *telem;
    1174             :     Oid         ltype,
    1175             :                 rtype;
    1176          28 :     bool        matched = false;
    1177             : 
    1178          28 :     if (operator_precedence_warning)
    1179           0 :         emit_precedence_warnings(pstate, PREC_GROUP_POSTFIX_IS, "IS",
    1180             :                                  lexpr, NULL,
    1181             :                                  a->location);
    1182             : 
    1183          28 :     lexpr = transformExprRecurse(pstate, lexpr);
    1184             : 
    1185          28 :     ltype = exprType(lexpr);
    1186          32 :     foreach(telem, (List *) a->rexpr)
    1187             :     {
    1188          28 :         rtype = typenameTypeId(pstate, lfirst(telem));
    1189          28 :         matched = (rtype == ltype);
    1190          28 :         if (matched)
    1191          24 :             break;
    1192             :     }
    1193             : 
    1194             :     /*
    1195             :      * We have two forms: equals or not equals. Flip the sense of the result
    1196             :      * for not equals.
    1197             :      */
    1198          28 :     if (strcmp(strVal(linitial(a->name)), "<>") == 0)
    1199           0 :         matched = (!matched);
    1200             : 
    1201          28 :     result = (Const *) makeBoolConst(matched, false);
    1202             : 
    1203             :     /* Make the result have the original input's parse location */
    1204          28 :     result->location = exprLocation((Node *) a);
    1205             : 
    1206          28 :     return (Node *) result;
    1207             : }
    1208             : 
    1209             : static Node *
    1210       28986 : transformAExprIn(ParseState *pstate, A_Expr *a)
    1211             : {
    1212       28986 :     Node       *result = NULL;
    1213             :     Node       *lexpr;
    1214             :     List       *rexprs;
    1215             :     List       *rvars;
    1216             :     List       *rnonvars;
    1217             :     bool        useOr;
    1218             :     ListCell   *l;
    1219             : 
    1220             :     /*
    1221             :      * If the operator is <>, combine with AND not OR.
    1222             :      */
    1223       28986 :     if (strcmp(strVal(linitial(a->name)), "<>") == 0)
    1224        2850 :         useOr = false;
    1225             :     else
    1226       26136 :         useOr = true;
    1227             : 
    1228       28986 :     if (operator_precedence_warning)
    1229           0 :         emit_precedence_warnings(pstate,
    1230             :                                  useOr ? PREC_GROUP_IN : PREC_GROUP_NOT_IN,
    1231             :                                  "IN",
    1232             :                                  a->lexpr, NULL,
    1233             :                                  a->location);
    1234             : 
    1235             :     /*
    1236             :      * We try to generate a ScalarArrayOpExpr from IN/NOT IN, but this is only
    1237             :      * possible if there is a suitable array type available.  If not, we fall
    1238             :      * back to a boolean condition tree with multiple copies of the lefthand
    1239             :      * expression.  Also, any IN-list items that contain Vars are handled as
    1240             :      * separate boolean conditions, because that gives the planner more scope
    1241             :      * for optimization on such clauses.
    1242             :      *
    1243             :      * First step: transform all the inputs, and detect whether any contain
    1244             :      * Vars.
    1245             :      */
    1246       28986 :     lexpr = transformExprRecurse(pstate, a->lexpr);
    1247       28986 :     rexprs = rvars = rnonvars = NIL;
    1248      103624 :     foreach(l, (List *) a->rexpr)
    1249             :     {
    1250       74642 :         Node       *rexpr = transformExprRecurse(pstate, lfirst(l));
    1251             : 
    1252       74638 :         rexprs = lappend(rexprs, rexpr);
    1253       74638 :         if (contain_vars_of_level(rexpr, 0))
    1254           0 :             rvars = lappend(rvars, rexpr);
    1255             :         else
    1256       74638 :             rnonvars = lappend(rnonvars, rexpr);
    1257             :     }
    1258             : 
    1259             :     /*
    1260             :      * ScalarArrayOpExpr is only going to be useful if there's more than one
    1261             :      * non-Var righthand item.
    1262             :      */
    1263       28982 :     if (list_length(rnonvars) > 1)
    1264             :     {
    1265             :         List       *allexprs;
    1266             :         Oid         scalar_type;
    1267             :         Oid         array_type;
    1268             : 
    1269             :         /*
    1270             :          * Try to select a common type for the array elements.  Note that
    1271             :          * since the LHS' type is first in the list, it will be preferred when
    1272             :          * there is doubt (eg, when all the RHS items are unknown literals).
    1273             :          *
    1274             :          * Note: use list_concat here not lcons, to avoid damaging rnonvars.
    1275             :          */
    1276       24976 :         allexprs = list_concat(list_make1(lexpr), rnonvars);
    1277       24976 :         scalar_type = select_common_type(pstate, allexprs, NULL, NULL);
    1278             : 
    1279             :         /*
    1280             :          * Do we have an array type to use?  Aside from the case where there
    1281             :          * isn't one, we don't risk using ScalarArrayOpExpr when the common
    1282             :          * type is RECORD, because the RowExpr comparison logic below can cope
    1283             :          * with some cases of non-identical row types.
    1284             :          */
    1285       24976 :         if (OidIsValid(scalar_type) && scalar_type != RECORDOID)
    1286       24964 :             array_type = get_array_type(scalar_type);
    1287             :         else
    1288          12 :             array_type = InvalidOid;
    1289       24976 :         if (array_type != InvalidOid)
    1290             :         {
    1291             :             /*
    1292             :              * OK: coerce all the right-hand non-Var inputs to the common type
    1293             :              * and build an ArrayExpr for them.
    1294             :              */
    1295             :             List       *aexprs;
    1296             :             ArrayExpr  *newa;
    1297             : 
    1298       24956 :             aexprs = NIL;
    1299       95548 :             foreach(l, rnonvars)
    1300             :             {
    1301       70592 :                 Node       *rexpr = (Node *) lfirst(l);
    1302             : 
    1303       70592 :                 rexpr = coerce_to_common_type(pstate, rexpr,
    1304             :                                               scalar_type,
    1305             :                                               "IN");
    1306       70592 :                 aexprs = lappend(aexprs, rexpr);
    1307             :             }
    1308       24956 :             newa = makeNode(ArrayExpr);
    1309       24956 :             newa->array_typeid = array_type;
    1310             :             /* array_collid will be set by parse_collate.c */
    1311       24956 :             newa->element_typeid = scalar_type;
    1312       24956 :             newa->elements = aexprs;
    1313       24956 :             newa->multidims = false;
    1314       24956 :             newa->location = -1;
    1315             : 
    1316       24956 :             result = (Node *) make_scalar_array_op(pstate,
    1317             :                                                    a->name,
    1318             :                                                    useOr,
    1319             :                                                    lexpr,
    1320             :                                                    (Node *) newa,
    1321             :                                                    a->location);
    1322             : 
    1323             :             /* Consider only the Vars (if any) in the loop below */
    1324       24956 :             rexprs = rvars;
    1325             :         }
    1326             :     }
    1327             : 
    1328             :     /*
    1329             :      * Must do it the hard way, ie, with a boolean expression tree.
    1330             :      */
    1331       33028 :     foreach(l, rexprs)
    1332             :     {
    1333        4046 :         Node       *rexpr = (Node *) lfirst(l);
    1334             :         Node       *cmp;
    1335             : 
    1336        4070 :         if (IsA(lexpr, RowExpr) &&
    1337          24 :             IsA(rexpr, RowExpr))
    1338             :         {
    1339             :             /* ROW() op ROW() is handled specially */
    1340          48 :             cmp = make_row_comparison_op(pstate,
    1341             :                                          a->name,
    1342          24 :                                          copyObject(((RowExpr *) lexpr)->args),
    1343             :                                          ((RowExpr *) rexpr)->args,
    1344             :                                          a->location);
    1345             :         }
    1346             :         else
    1347             :         {
    1348             :             /* Ordinary scalar operator */
    1349        8044 :             cmp = (Node *) make_op(pstate,
    1350             :                                    a->name,
    1351        4022 :                                    copyObject(lexpr),
    1352             :                                    rexpr,
    1353             :                                    pstate->p_last_srf,
    1354             :                                    a->location);
    1355             :         }
    1356             : 
    1357        4046 :         cmp = coerce_to_boolean(pstate, cmp, "IN");
    1358        4046 :         if (result == NULL)
    1359        4026 :             result = cmp;
    1360             :         else
    1361          40 :             result = (Node *) makeBoolExpr(useOr ? OR_EXPR : AND_EXPR,
    1362          20 :                                            list_make2(result, cmp),
    1363             :                                            a->location);
    1364             :     }
    1365             : 
    1366       28982 :     return result;
    1367             : }
    1368             : 
    1369             : static Node *
    1370         374 : transformAExprBetween(ParseState *pstate, A_Expr *a)
    1371             : {
    1372             :     Node       *aexpr;
    1373             :     Node       *bexpr;
    1374             :     Node       *cexpr;
    1375             :     Node       *result;
    1376             :     Node       *sub1;
    1377             :     Node       *sub2;
    1378             :     List       *args;
    1379             : 
    1380             :     /* Deconstruct A_Expr into three subexprs */
    1381         374 :     aexpr = a->lexpr;
    1382         374 :     args = castNode(List, a->rexpr);
    1383             :     Assert(list_length(args) == 2);
    1384         374 :     bexpr = (Node *) linitial(args);
    1385         374 :     cexpr = (Node *) lsecond(args);
    1386             : 
    1387         374 :     if (operator_precedence_warning)
    1388             :     {
    1389             :         int         opgroup;
    1390             :         const char *opname;
    1391             : 
    1392           0 :         opgroup = operator_precedence_group((Node *) a, &opname);
    1393           0 :         emit_precedence_warnings(pstate, opgroup, opname,
    1394             :                                  aexpr, cexpr,
    1395             :                                  a->location);
    1396             :         /* We can ignore bexpr thanks to syntactic restrictions */
    1397             :         /* Wrap subexpressions to prevent extra warnings */
    1398           0 :         aexpr = (Node *) makeA_Expr(AEXPR_PAREN, NIL, aexpr, NULL, -1);
    1399           0 :         bexpr = (Node *) makeA_Expr(AEXPR_PAREN, NIL, bexpr, NULL, -1);
    1400           0 :         cexpr = (Node *) makeA_Expr(AEXPR_PAREN, NIL, cexpr, NULL, -1);
    1401             :     }
    1402             : 
    1403             :     /*
    1404             :      * Build the equivalent comparison expression.  Make copies of
    1405             :      * multiply-referenced subexpressions for safety.  (XXX this is really
    1406             :      * wrong since it results in multiple runtime evaluations of what may be
    1407             :      * volatile expressions ...)
    1408             :      *
    1409             :      * Ideally we would not use hard-wired operators here but instead use
    1410             :      * opclasses.  However, mixed data types and other issues make this
    1411             :      * difficult:
    1412             :      * http://archives.postgresql.org/pgsql-hackers/2008-08/msg01142.php
    1413             :      */
    1414         374 :     switch (a->kind)
    1415             :     {
    1416             :         case AEXPR_BETWEEN:
    1417         346 :             args = list_make2(makeSimpleA_Expr(AEXPR_OP, ">=",
    1418             :                                                aexpr, bexpr,
    1419             :                                                a->location),
    1420             :                               makeSimpleA_Expr(AEXPR_OP, "<=",
    1421             :                                                copyObject(aexpr), cexpr,
    1422             :                                                a->location));
    1423         346 :             result = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
    1424         346 :             break;
    1425             :         case AEXPR_NOT_BETWEEN:
    1426          12 :             args = list_make2(makeSimpleA_Expr(AEXPR_OP, "<",
    1427             :                                                aexpr, bexpr,
    1428             :                                                a->location),
    1429             :                               makeSimpleA_Expr(AEXPR_OP, ">",
    1430             :                                                copyObject(aexpr), cexpr,
    1431             :                                                a->location));
    1432          12 :             result = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
    1433          12 :             break;
    1434             :         case AEXPR_BETWEEN_SYM:
    1435           8 :             args = list_make2(makeSimpleA_Expr(AEXPR_OP, ">=",
    1436             :                                                aexpr, bexpr,
    1437             :                                                a->location),
    1438             :                               makeSimpleA_Expr(AEXPR_OP, "<=",
    1439             :                                                copyObject(aexpr), cexpr,
    1440             :                                                a->location));
    1441           8 :             sub1 = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
    1442           8 :             args = list_make2(makeSimpleA_Expr(AEXPR_OP, ">=",
    1443             :                                                copyObject(aexpr), copyObject(cexpr),
    1444             :                                                a->location),
    1445             :                               makeSimpleA_Expr(AEXPR_OP, "<=",
    1446             :                                                copyObject(aexpr), copyObject(bexpr),
    1447             :                                                a->location));
    1448           8 :             sub2 = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
    1449           8 :             args = list_make2(sub1, sub2);
    1450           8 :             result = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
    1451           8 :             break;
    1452             :         case AEXPR_NOT_BETWEEN_SYM:
    1453           8 :             args = list_make2(makeSimpleA_Expr(AEXPR_OP, "<",
    1454             :                                                aexpr, bexpr,
    1455             :                                                a->location),
    1456             :                               makeSimpleA_Expr(AEXPR_OP, ">",
    1457             :                                                copyObject(aexpr), cexpr,
    1458             :                                                a->location));
    1459           8 :             sub1 = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
    1460           8 :             args = list_make2(makeSimpleA_Expr(AEXPR_OP, "<",
    1461             :                                                copyObject(aexpr), copyObject(cexpr),
    1462             :                                                a->location),
    1463             :                               makeSimpleA_Expr(AEXPR_OP, ">",
    1464             :                                                copyObject(aexpr), copyObject(bexpr),
    1465             :                                                a->location));
    1466           8 :             sub2 = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
    1467           8 :             args = list_make2(sub1, sub2);
    1468           8 :             result = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
    1469           8 :             break;
    1470             :         default:
    1471           0 :             elog(ERROR, "unrecognized A_Expr kind: %d", a->kind);
    1472             :             result = NULL;      /* keep compiler quiet */
    1473             :             break;
    1474             :     }
    1475             : 
    1476         374 :     return transformExprRecurse(pstate, result);
    1477             : }
    1478             : 
    1479             : static Node *
    1480      150454 : transformBoolExpr(ParseState *pstate, BoolExpr *a)
    1481             : {
    1482      150454 :     List       *args = NIL;
    1483             :     const char *opname;
    1484             :     ListCell   *lc;
    1485             : 
    1486      150454 :     switch (a->boolop)
    1487             :     {
    1488             :         case AND_EXPR:
    1489       99484 :             opname = "AND";
    1490       99484 :             break;
    1491             :         case OR_EXPR:
    1492       22768 :             opname = "OR";
    1493       22768 :             break;
    1494             :         case NOT_EXPR:
    1495       28202 :             opname = "NOT";
    1496       28202 :             break;
    1497             :         default:
    1498           0 :             elog(ERROR, "unrecognized boolop: %d", (int) a->boolop);
    1499             :             opname = NULL;      /* keep compiler quiet */
    1500             :             break;
    1501             :     }
    1502             : 
    1503      516730 :     foreach(lc, a->args)
    1504             :     {
    1505      366290 :         Node       *arg = (Node *) lfirst(lc);
    1506             : 
    1507      366290 :         arg = transformExprRecurse(pstate, arg);
    1508      366276 :         arg = coerce_to_boolean(pstate, arg, opname);
    1509      366276 :         args = lappend(args, arg);
    1510             :     }
    1511             : 
    1512      150440 :     return (Node *) makeBoolExpr(a->boolop, args, a->location);
    1513             : }
    1514             : 
    1515             : static Node *
    1516      388692 : transformFuncCall(ParseState *pstate, FuncCall *fn)
    1517             : {
    1518      388692 :     Node       *last_srf = pstate->p_last_srf;
    1519             :     List       *targs;
    1520             :     ListCell   *args;
    1521             : 
    1522             :     /* Transform the list of arguments ... */
    1523      388692 :     targs = NIL;
    1524      890274 :     foreach(args, fn->args)
    1525             :     {
    1526      501622 :         targs = lappend(targs, transformExprRecurse(pstate,
    1527      501622 :                                                     (Node *) lfirst(args)));
    1528             :     }
    1529             : 
    1530             :     /*
    1531             :      * When WITHIN GROUP is used, we treat its ORDER BY expressions as
    1532             :      * additional arguments to the function, for purposes of function lookup
    1533             :      * and argument type coercion.  So, transform each such expression and add
    1534             :      * them to the targs list.  We don't explicitly mark where each argument
    1535             :      * came from, but ParseFuncOrColumn can tell what's what by reference to
    1536             :      * list_length(fn->agg_order).
    1537             :      */
    1538      388652 :     if (fn->agg_within_group)
    1539             :     {
    1540             :         Assert(fn->agg_order != NIL);
    1541         418 :         foreach(args, fn->agg_order)
    1542             :         {
    1543         222 :             SortBy     *arg = (SortBy *) lfirst(args);
    1544             : 
    1545         222 :             targs = lappend(targs, transformExpr(pstate, arg->node,
    1546             :                                                  EXPR_KIND_ORDER_BY));
    1547             :         }
    1548             :     }
    1549             : 
    1550             :     /* ... and hand off to ParseFuncOrColumn */
    1551      388652 :     return ParseFuncOrColumn(pstate,
    1552             :                              fn->funcname,
    1553             :                              targs,
    1554             :                              last_srf,
    1555             :                              fn,
    1556             :                              false,
    1557             :                              fn->location);
    1558             : }
    1559             : 
    1560             : static Node *
    1561         172 : transformMultiAssignRef(ParseState *pstate, MultiAssignRef *maref)
    1562             : {
    1563             :     SubLink    *sublink;
    1564             :     RowExpr    *rexpr;
    1565             :     Query      *qtree;
    1566             :     TargetEntry *tle;
    1567             : 
    1568             :     /* We should only see this in first-stage processing of UPDATE tlists */
    1569             :     Assert(pstate->p_expr_kind == EXPR_KIND_UPDATE_SOURCE);
    1570             : 
    1571             :     /* We only need to transform the source if this is the first column */
    1572         172 :     if (maref->colno == 1)
    1573             :     {
    1574             :         /*
    1575             :          * For now, we only allow EXPR SubLinks and RowExprs as the source of
    1576             :          * an UPDATE multiassignment.  This is sufficient to cover interesting
    1577             :          * cases; at worst, someone would have to write (SELECT * FROM expr)
    1578             :          * to expand a composite-returning expression of another form.
    1579             :          */
    1580         136 :         if (IsA(maref->source, SubLink) &&
    1581          52 :             ((SubLink *) maref->source)->subLinkType == EXPR_SUBLINK)
    1582             :         {
    1583             :             /* Relabel it as a MULTIEXPR_SUBLINK */
    1584          52 :             sublink = (SubLink *) maref->source;
    1585          52 :             sublink->subLinkType = MULTIEXPR_SUBLINK;
    1586             :             /* And transform it */
    1587          52 :             sublink = (SubLink *) transformExprRecurse(pstate,
    1588             :                                                        (Node *) sublink);
    1589             : 
    1590          52 :             qtree = castNode(Query, sublink->subselect);
    1591             : 
    1592             :             /* Check subquery returns required number of columns */
    1593          52 :             if (count_nonjunk_tlist_entries(qtree->targetList) != maref->ncolumns)
    1594           0 :                 ereport(ERROR,
    1595             :                         (errcode(ERRCODE_SYNTAX_ERROR),
    1596             :                          errmsg("number of columns does not match number of values"),
    1597             :                          parser_errposition(pstate, sublink->location)));
    1598             : 
    1599             :             /*
    1600             :              * Build a resjunk tlist item containing the MULTIEXPR SubLink,
    1601             :              * and add it to pstate->p_multiassign_exprs, whence it will later
    1602             :              * get appended to the completed targetlist.  We needn't worry
    1603             :              * about selecting a resno for it; transformUpdateStmt will do
    1604             :              * that.
    1605             :              */
    1606          52 :             tle = makeTargetEntry((Expr *) sublink, 0, NULL, true);
    1607          52 :             pstate->p_multiassign_exprs = lappend(pstate->p_multiassign_exprs,
    1608             :                                                   tle);
    1609             : 
    1610             :             /*
    1611             :              * Assign a unique-within-this-targetlist ID to the MULTIEXPR
    1612             :              * SubLink.  We can just use its position in the
    1613             :              * p_multiassign_exprs list.
    1614             :              */
    1615          52 :             sublink->subLinkId = list_length(pstate->p_multiassign_exprs);
    1616             :         }
    1617          32 :         else if (IsA(maref->source, RowExpr))
    1618             :         {
    1619             :             /* Transform the RowExpr, allowing SetToDefault items */
    1620          28 :             rexpr = (RowExpr *) transformRowExpr(pstate,
    1621          28 :                                                  (RowExpr *) maref->source,
    1622             :                                                  true);
    1623             : 
    1624             :             /* Check it returns required number of columns */
    1625          28 :             if (list_length(rexpr->args) != maref->ncolumns)
    1626           0 :                 ereport(ERROR,
    1627             :                         (errcode(ERRCODE_SYNTAX_ERROR),
    1628             :                          errmsg("number of columns does not match number of values"),
    1629             :                          parser_errposition(pstate, rexpr->location)));
    1630             : 
    1631             :             /*
    1632             :              * Temporarily append it to p_multiassign_exprs, so we can get it
    1633             :              * back when we come back here for additional columns.
    1634             :              */
    1635          28 :             tle = makeTargetEntry((Expr *) rexpr, 0, NULL, true);
    1636          28 :             pstate->p_multiassign_exprs = lappend(pstate->p_multiassign_exprs,
    1637             :                                                   tle);
    1638             :         }
    1639             :         else
    1640           4 :             ereport(ERROR,
    1641             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1642             :                      errmsg("source for a multiple-column UPDATE item must be a sub-SELECT or ROW() expression"),
    1643             :                      parser_errposition(pstate, exprLocation(maref->source))));
    1644             :     }
    1645             :     else
    1646             :     {
    1647             :         /*
    1648             :          * Second or later column in a multiassignment.  Re-fetch the
    1649             :          * transformed SubLink or RowExpr, which we assume is still the last
    1650             :          * entry in p_multiassign_exprs.
    1651             :          */
    1652             :         Assert(pstate->p_multiassign_exprs != NIL);
    1653          88 :         tle = (TargetEntry *) llast(pstate->p_multiassign_exprs);
    1654             :     }
    1655             : 
    1656             :     /*
    1657             :      * Emit the appropriate output expression for the current column
    1658             :      */
    1659         168 :     if (IsA(tle->expr, SubLink))
    1660             :     {
    1661             :         Param      *param;
    1662             : 
    1663         104 :         sublink = (SubLink *) tle->expr;
    1664             :         Assert(sublink->subLinkType == MULTIEXPR_SUBLINK);
    1665         104 :         qtree = castNode(Query, sublink->subselect);
    1666             : 
    1667             :         /* Build a Param representing the current subquery output column */
    1668         104 :         tle = (TargetEntry *) list_nth(qtree->targetList, maref->colno - 1);
    1669             :         Assert(!tle->resjunk);
    1670             : 
    1671         104 :         param = makeNode(Param);
    1672         104 :         param->paramkind = PARAM_MULTIEXPR;
    1673         104 :         param->paramid = (sublink->subLinkId << 16) | maref->colno;
    1674         104 :         param->paramtype = exprType((Node *) tle->expr);
    1675         104 :         param->paramtypmod = exprTypmod((Node *) tle->expr);
    1676         104 :         param->paramcollid = exprCollation((Node *) tle->expr);
    1677         104 :         param->location = exprLocation((Node *) tle->expr);
    1678             : 
    1679         104 :         return (Node *) param;
    1680             :     }
    1681             : 
    1682          64 :     if (IsA(tle->expr, RowExpr))
    1683             :     {
    1684             :         Node       *result;
    1685             : 
    1686          64 :         rexpr = (RowExpr *) tle->expr;
    1687             : 
    1688             :         /* Just extract and return the next element of the RowExpr */
    1689          64 :         result = (Node *) list_nth(rexpr->args, maref->colno - 1);
    1690             : 
    1691             :         /*
    1692             :          * If we're at the last column, delete the RowExpr from
    1693             :          * p_multiassign_exprs; we don't need it anymore, and don't want it in
    1694             :          * the finished UPDATE tlist.
    1695             :          */
    1696          64 :         if (maref->colno == maref->ncolumns)
    1697          28 :             pstate->p_multiassign_exprs =
    1698          28 :                 list_delete_ptr(pstate->p_multiassign_exprs, tle);
    1699             : 
    1700          64 :         return result;
    1701             :     }
    1702             : 
    1703           0 :     elog(ERROR, "unexpected expr type in multiassign list");
    1704             :     return NULL;                /* keep compiler quiet */
    1705             : }
    1706             : 
    1707             : static Node *
    1708       61924 : transformCaseExpr(ParseState *pstate, CaseExpr *c)
    1709             : {
    1710       61924 :     CaseExpr   *newc = makeNode(CaseExpr);
    1711       61924 :     Node       *last_srf = pstate->p_last_srf;
    1712             :     Node       *arg;
    1713             :     CaseTestExpr *placeholder;
    1714             :     List       *newargs;
    1715             :     List       *resultexprs;
    1716             :     ListCell   *l;
    1717             :     Node       *defresult;
    1718             :     Oid         ptype;
    1719             : 
    1720             :     /* transform the test expression, if any */
    1721       61924 :     arg = transformExprRecurse(pstate, (Node *) c->arg);
    1722             : 
    1723             :     /* generate placeholder for test expression */
    1724       61924 :     if (arg)
    1725             :     {
    1726             :         /*
    1727             :          * If test expression is an untyped literal, force it to text. We have
    1728             :          * to do something now because we won't be able to do this coercion on
    1729             :          * the placeholder.  This is not as flexible as what was done in 7.4
    1730             :          * and before, but it's good enough to handle the sort of silly coding
    1731             :          * commonly seen.
    1732             :          */
    1733        7140 :         if (exprType(arg) == UNKNOWNOID)
    1734           4 :             arg = coerce_to_common_type(pstate, arg, TEXTOID, "CASE");
    1735             : 
    1736             :         /*
    1737             :          * Run collation assignment on the test expression so that we know
    1738             :          * what collation to mark the placeholder with.  In principle we could
    1739             :          * leave it to parse_collate.c to do that later, but propagating the
    1740             :          * result to the CaseTestExpr would be unnecessarily complicated.
    1741             :          */
    1742        7140 :         assign_expr_collations(pstate, arg);
    1743             : 
    1744        7140 :         placeholder = makeNode(CaseTestExpr);
    1745        7140 :         placeholder->typeId = exprType(arg);
    1746        7140 :         placeholder->typeMod = exprTypmod(arg);
    1747        7140 :         placeholder->collation = exprCollation(arg);
    1748             :     }
    1749             :     else
    1750       54784 :         placeholder = NULL;
    1751             : 
    1752       61924 :     newc->arg = (Expr *) arg;
    1753             : 
    1754             :     /* transform the list of arguments */
    1755       61924 :     newargs = NIL;
    1756       61924 :     resultexprs = NIL;
    1757      162968 :     foreach(l, c->args)
    1758             :     {
    1759      101044 :         CaseWhen   *w = lfirst_node(CaseWhen, l);
    1760      101044 :         CaseWhen   *neww = makeNode(CaseWhen);
    1761             :         Node       *warg;
    1762             : 
    1763      101044 :         warg = (Node *) w->expr;
    1764      101044 :         if (placeholder)
    1765             :         {
    1766             :             /* shorthand form was specified, so expand... */
    1767       28308 :             warg = (Node *) makeSimpleA_Expr(AEXPR_OP, "=",
    1768             :                                              (Node *) placeholder,
    1769             :                                              warg,
    1770             :                                              w->location);
    1771             :         }
    1772      101044 :         neww->expr = (Expr *) transformExprRecurse(pstate, warg);
    1773             : 
    1774      101044 :         neww->expr = (Expr *) coerce_to_boolean(pstate,
    1775      101044 :                                                 (Node *) neww->expr,
    1776             :                                                 "CASE/WHEN");
    1777             : 
    1778      101044 :         warg = (Node *) w->result;
    1779      101044 :         neww->result = (Expr *) transformExprRecurse(pstate, warg);
    1780      101044 :         neww->location = w->location;
    1781             : 
    1782      101044 :         newargs = lappend(newargs, neww);
    1783      101044 :         resultexprs = lappend(resultexprs, neww->result);
    1784             :     }
    1785             : 
    1786       61924 :     newc->args = newargs;
    1787             : 
    1788             :     /* transform the default clause */
    1789       61924 :     defresult = (Node *) c->defresult;
    1790       61924 :     if (defresult == NULL)
    1791             :     {
    1792       12006 :         A_Const    *n = makeNode(A_Const);
    1793             : 
    1794       12006 :         n->val.type = T_Null;
    1795       12006 :         n->location = -1;
    1796       12006 :         defresult = (Node *) n;
    1797             :     }
    1798       61924 :     newc->defresult = (Expr *) transformExprRecurse(pstate, defresult);
    1799             : 
    1800             :     /*
    1801             :      * Note: default result is considered the most significant type in
    1802             :      * determining preferred type. This is how the code worked before, but it
    1803             :      * seems a little bogus to me --- tgl
    1804             :      */
    1805       61924 :     resultexprs = lcons(newc->defresult, resultexprs);
    1806             : 
    1807       61924 :     ptype = select_common_type(pstate, resultexprs, "CASE", NULL);
    1808             :     Assert(OidIsValid(ptype));
    1809       61924 :     newc->casetype = ptype;
    1810             :     /* casecollid will be set by parse_collate.c */
    1811             : 
    1812             :     /* Convert default result clause, if necessary */
    1813       61924 :     newc->defresult = (Expr *)
    1814       61924 :         coerce_to_common_type(pstate,
    1815       61924 :                               (Node *) newc->defresult,
    1816             :                               ptype,
    1817             :                               "CASE/ELSE");
    1818             : 
    1819             :     /* Convert when-clause results, if necessary */
    1820      162968 :     foreach(l, newc->args)
    1821             :     {
    1822      101044 :         CaseWhen   *w = (CaseWhen *) lfirst(l);
    1823             : 
    1824      101044 :         w->result = (Expr *)
    1825      101044 :             coerce_to_common_type(pstate,
    1826      101044 :                                   (Node *) w->result,
    1827             :                                   ptype,
    1828             :                                   "CASE/WHEN");
    1829             :     }
    1830             : 
    1831             :     /* if any subexpression contained a SRF, complain */
    1832       61924 :     if (pstate->p_last_srf != last_srf)
    1833           4 :         ereport(ERROR,
    1834             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1835             :         /* translator: %s is name of a SQL construct, eg GROUP BY */
    1836             :                  errmsg("set-returning functions are not allowed in %s",
    1837             :                         "CASE"),
    1838             :                  errhint("You might be able to move the set-returning function into a LATERAL FROM item."),
    1839             :                  parser_errposition(pstate,
    1840             :                                     exprLocation(pstate->p_last_srf))));
    1841             : 
    1842       61920 :     newc->location = c->location;
    1843             : 
    1844       61920 :     return (Node *) newc;
    1845             : }
    1846             : 
    1847             : static Node *
    1848       61422 : transformSubLink(ParseState *pstate, SubLink *sublink)
    1849             : {
    1850       61422 :     Node       *result = (Node *) sublink;
    1851             :     Query      *qtree;
    1852             :     const char *err;
    1853             : 
    1854             :     /*
    1855             :      * Check to see if the sublink is in an invalid place within the query. We
    1856             :      * allow sublinks everywhere in SELECT/INSERT/UPDATE/DELETE, but generally
    1857             :      * not in utility statements.
    1858             :      */
    1859       61422 :     err = NULL;
    1860       61422 :     switch (pstate->p_expr_kind)
    1861             :     {
    1862             :         case EXPR_KIND_NONE:
    1863             :             Assert(false);      /* can't happen */
    1864           0 :             break;
    1865             :         case EXPR_KIND_OTHER:
    1866             :             /* Accept sublink here; caller must throw error if wanted */
    1867           0 :             break;
    1868             :         case EXPR_KIND_JOIN_ON:
    1869             :         case EXPR_KIND_JOIN_USING:
    1870             :         case EXPR_KIND_FROM_SUBSELECT:
    1871             :         case EXPR_KIND_FROM_FUNCTION:
    1872             :         case EXPR_KIND_WHERE:
    1873             :         case EXPR_KIND_POLICY:
    1874             :         case EXPR_KIND_HAVING:
    1875             :         case EXPR_KIND_FILTER:
    1876             :         case EXPR_KIND_WINDOW_PARTITION:
    1877             :         case EXPR_KIND_WINDOW_ORDER:
    1878             :         case EXPR_KIND_WINDOW_FRAME_RANGE:
    1879             :         case EXPR_KIND_WINDOW_FRAME_ROWS:
    1880             :         case EXPR_KIND_WINDOW_FRAME_GROUPS:
    1881             :         case EXPR_KIND_SELECT_TARGET:
    1882             :         case EXPR_KIND_INSERT_TARGET:
    1883             :         case EXPR_KIND_UPDATE_SOURCE:
    1884             :         case EXPR_KIND_UPDATE_TARGET:
    1885             :         case EXPR_KIND_GROUP_BY:
    1886             :         case EXPR_KIND_ORDER_BY:
    1887             :         case EXPR_KIND_DISTINCT_ON:
    1888             :         case EXPR_KIND_LIMIT:
    1889             :         case EXPR_KIND_OFFSET:
    1890             :         case EXPR_KIND_RETURNING:
    1891             :         case EXPR_KIND_VALUES:
    1892             :         case EXPR_KIND_VALUES_SINGLE:
    1893             :             /* okay */
    1894       61398 :             break;
    1895             :         case EXPR_KIND_CHECK_CONSTRAINT:
    1896             :         case EXPR_KIND_DOMAIN_CHECK:
    1897           0 :             err = _("cannot use subquery in check constraint");
    1898           0 :             break;
    1899             :         case EXPR_KIND_COLUMN_DEFAULT:
    1900             :         case EXPR_KIND_FUNCTION_DEFAULT:
    1901           4 :             err = _("cannot use subquery in DEFAULT expression");
    1902           4 :             break;
    1903             :         case EXPR_KIND_INDEX_EXPRESSION:
    1904           0 :             err = _("cannot use subquery in index expression");
    1905           0 :             break;
    1906             :         case EXPR_KIND_INDEX_PREDICATE:
    1907           0 :             err = _("cannot use subquery in index predicate");
    1908           0 :             break;
    1909             :         case EXPR_KIND_ALTER_COL_TRANSFORM:
    1910           0 :             err = _("cannot use subquery in transform expression");
    1911           0 :             break;
    1912             :         case EXPR_KIND_EXECUTE_PARAMETER:
    1913           0 :             err = _("cannot use subquery in EXECUTE parameter");
    1914           0 :             break;
    1915             :         case EXPR_KIND_TRIGGER_WHEN:
    1916           0 :             err = _("cannot use subquery in trigger WHEN condition");
    1917           0 :             break;
    1918             :         case EXPR_KIND_PARTITION_BOUND:
    1919           8 :             err = _("cannot use subquery in partition bound");
    1920           8 :             break;
    1921             :         case EXPR_KIND_PARTITION_EXPRESSION:
    1922           4 :             err = _("cannot use subquery in partition key expression");
    1923           4 :             break;
    1924             :         case EXPR_KIND_CALL_ARGUMENT:
    1925           0 :             err = _("cannot use subquery in CALL argument");
    1926           0 :             break;
    1927             :         case EXPR_KIND_COPY_WHERE:
    1928           4 :             err = _("cannot use subquery in COPY FROM WHERE condition");
    1929           4 :             break;
    1930             :         case EXPR_KIND_GENERATED_COLUMN:
    1931           4 :             err = _("cannot use subquery in column generation expression");
    1932           4 :             break;
    1933             : 
    1934             :             /*
    1935             :              * There is intentionally no default: case here, so that the
    1936             :              * compiler will warn if we add a new ParseExprKind without
    1937             :              * extending this switch.  If we do see an unrecognized value at
    1938             :              * runtime, the behavior will be the same as for EXPR_KIND_OTHER,
    1939             :              * which is sane anyway.
    1940             :              */
    1941             :     }
    1942       61422 :     if (err)
    1943          24 :         ereport(ERROR,
    1944             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1945             :                  errmsg_internal("%s", err),
    1946             :                  parser_errposition(pstate, sublink->location)));
    1947             : 
    1948       61398 :     pstate->p_hasSubLinks = true;
    1949             : 
    1950             :     /*
    1951             :      * OK, let's transform the sub-SELECT.
    1952             :      */
    1953       61398 :     qtree = parse_sub_analyze(sublink->subselect, pstate, NULL, false, true);
    1954             : 
    1955             :     /*
    1956             :      * Check that we got a SELECT.  Anything else should be impossible given
    1957             :      * restrictions of the grammar, but check anyway.
    1958             :      */
    1959      122756 :     if (!IsA(qtree, Query) ||
    1960       61378 :         qtree->commandType != CMD_SELECT)
    1961           0 :         elog(ERROR, "unexpected non-SELECT command in SubLink");
    1962             : 
    1963       61378 :     sublink->subselect = (Node *) qtree;
    1964             : 
    1965       61378 :     if (sublink->subLinkType == EXISTS_SUBLINK)
    1966             :     {
    1967             :         /*
    1968             :          * EXISTS needs no test expression or combining operator. These fields
    1969             :          * should be null already, but make sure.
    1970             :          */
    1971       10612 :         sublink->testexpr = NULL;
    1972       10612 :         sublink->operName = NIL;
    1973             :     }
    1974       69142 :     else if (sublink->subLinkType == EXPR_SUBLINK ||
    1975       18376 :              sublink->subLinkType == ARRAY_SUBLINK)
    1976             :     {
    1977             :         /*
    1978             :          * Make sure the subselect delivers a single column (ignoring resjunk
    1979             :          * targets).
    1980             :          */
    1981       46454 :         if (count_nonjunk_tlist_entries(qtree->targetList) != 1)
    1982           0 :             ereport(ERROR,
    1983             :                     (errcode(ERRCODE_SYNTAX_ERROR),
    1984             :                      errmsg("subquery must return only one column"),
    1985             :                      parser_errposition(pstate, sublink->location)));
    1986             : 
    1987             :         /*
    1988             :          * EXPR and ARRAY need no test expression or combining operator. These
    1989             :          * fields should be null already, but make sure.
    1990             :          */
    1991       46454 :         sublink->testexpr = NULL;
    1992       46454 :         sublink->operName = NIL;
    1993             :     }
    1994        4312 :     else if (sublink->subLinkType == MULTIEXPR_SUBLINK)
    1995             :     {
    1996             :         /* Same as EXPR case, except no restriction on number of columns */
    1997          52 :         sublink->testexpr = NULL;
    1998          52 :         sublink->operName = NIL;
    1999             :     }
    2000             :     else
    2001             :     {
    2002             :         /* ALL, ANY, or ROWCOMPARE: generate row-comparing expression */
    2003             :         Node       *lefthand;
    2004             :         List       *left_list;
    2005             :         List       *right_list;
    2006             :         ListCell   *l;
    2007             : 
    2008        4260 :         if (operator_precedence_warning)
    2009             :         {
    2010           0 :             if (sublink->operName == NIL)
    2011           0 :                 emit_precedence_warnings(pstate, PREC_GROUP_IN, "IN",
    2012             :                                          sublink->testexpr, NULL,
    2013             :                                          sublink->location);
    2014             :             else
    2015           0 :                 emit_precedence_warnings(pstate, PREC_GROUP_POSTFIX_OP,
    2016           0 :                                          strVal(llast(sublink->operName)),
    2017             :                                          sublink->testexpr, NULL,
    2018             :                                          sublink->location);
    2019             :         }
    2020             : 
    2021             :         /*
    2022             :          * If the source was "x IN (select)", convert to "x = ANY (select)".
    2023             :          */
    2024        4260 :         if (sublink->operName == NIL)
    2025        4194 :             sublink->operName = list_make1(makeString("="));
    2026             : 
    2027             :         /*
    2028             :          * Transform lefthand expression, and convert to a list
    2029             :          */
    2030        4260 :         lefthand = transformExprRecurse(pstate, sublink->testexpr);
    2031        4260 :         if (lefthand && IsA(lefthand, RowExpr))
    2032         382 :             left_list = ((RowExpr *) lefthand)->args;
    2033             :         else
    2034        3878 :             left_list = list_make1(lefthand);
    2035             : 
    2036             :         /*
    2037             :          * Build a list of PARAM_SUBLINK nodes representing the output columns
    2038             :          * of the subquery.
    2039             :          */
    2040        4260 :         right_list = NIL;
    2041        9554 :         foreach(l, qtree->targetList)
    2042             :         {
    2043        5294 :             TargetEntry *tent = (TargetEntry *) lfirst(l);
    2044             :             Param      *param;
    2045             : 
    2046        5294 :             if (tent->resjunk)
    2047           8 :                 continue;
    2048             : 
    2049        5286 :             param = makeNode(Param);
    2050        5286 :             param->paramkind = PARAM_SUBLINK;
    2051        5286 :             param->paramid = tent->resno;
    2052        5286 :             param->paramtype = exprType((Node *) tent->expr);
    2053        5286 :             param->paramtypmod = exprTypmod((Node *) tent->expr);
    2054        5286 :             param->paramcollid = exprCollation((Node *) tent->expr);
    2055        5286 :             param->location = -1;
    2056             : 
    2057        5286 :             right_list = lappend(right_list, param);
    2058             :         }
    2059             : 
    2060             :         /*
    2061             :          * We could rely on make_row_comparison_op to complain if the list
    2062             :          * lengths differ, but we prefer to generate a more specific error
    2063             :          * message.
    2064             :          */
    2065        4260 :         if (list_length(left_list) < list_length(right_list))
    2066           0 :             ereport(ERROR,
    2067             :                     (errcode(ERRCODE_SYNTAX_ERROR),
    2068             :                      errmsg("subquery has too many columns"),
    2069             :                      parser_errposition(pstate, sublink->location)));
    2070        4260 :         if (list_length(left_list) > list_length(right_list))
    2071           0 :             ereport(ERROR,
    2072             :                     (errcode(ERRCODE_SYNTAX_ERROR),
    2073             :                      errmsg("subquery has too few columns"),
    2074             :                      parser_errposition(pstate, sublink->location)));
    2075             : 
    2076             :         /*
    2077             :          * Identify the combining operator(s) and generate a suitable
    2078             :          * row-comparison expression.
    2079             :          */
    2080        4260 :         sublink->testexpr = make_row_comparison_op(pstate,
    2081             :                                                    sublink->operName,
    2082             :                                                    left_list,
    2083             :                                                    right_list,
    2084             :                                                    sublink->location);
    2085             :     }
    2086             : 
    2087       61374 :     return result;
    2088             : }
    2089             : 
    2090             : /*
    2091             :  * transformArrayExpr
    2092             :  *
    2093             :  * If the caller specifies the target type, the resulting array will
    2094             :  * be of exactly that type.  Otherwise we try to infer a common type
    2095             :  * for the elements using select_common_type().
    2096             :  */
    2097             : static Node *
    2098        3570 : transformArrayExpr(ParseState *pstate, A_ArrayExpr *a,
    2099             :                    Oid array_type, Oid element_type, int32 typmod)
    2100             : {
    2101        3570 :     ArrayExpr  *newa = makeNode(ArrayExpr);
    2102        3570 :     List       *newelems = NIL;
    2103        3570 :     List       *newcoercedelems = NIL;
    2104             :     ListCell   *element;
    2105             :     Oid         coerce_type;
    2106             :     bool        coerce_hard;
    2107             : 
    2108             :     /*
    2109             :      * Transform the element expressions
    2110             :      *
    2111             :      * Assume that the array is one-dimensional unless we find an array-type
    2112             :      * element expression.
    2113             :      */
    2114        3570 :     newa->multidims = false;
    2115       12296 :     foreach(element, a->elements)
    2116             :     {
    2117        8726 :         Node       *e = (Node *) lfirst(element);
    2118             :         Node       *newe;
    2119             : 
    2120             :         /* Look through AEXPR_PAREN nodes so they don't affect test below */
    2121       17546 :         while (e && IsA(e, A_Expr) &&
    2122          94 :                ((A_Expr *) e)->kind == AEXPR_PAREN)
    2123           0 :             e = ((A_Expr *) e)->lexpr;
    2124             : 
    2125             :         /*
    2126             :          * If an element is itself an A_ArrayExpr, recurse directly so that we
    2127             :          * can pass down any target type we were given.
    2128             :          */
    2129        8726 :         if (IsA(e, A_ArrayExpr))
    2130             :         {
    2131         620 :             newe = transformArrayExpr(pstate,
    2132             :                                       (A_ArrayExpr *) e,
    2133             :                                       array_type,
    2134             :                                       element_type,
    2135             :                                       typmod);
    2136             :             /* we certainly have an array here */
    2137             :             Assert(array_type == InvalidOid || array_type == exprType(newe));
    2138         620 :             newa->multidims = true;
    2139             :         }
    2140             :         else
    2141             :         {
    2142        8106 :             newe = transformExprRecurse(pstate, e);
    2143             : 
    2144             :             /*
    2145             :              * Check for sub-array expressions, if we haven't already found
    2146             :              * one.
    2147             :              */
    2148        8106 :             if (!newa->multidims && type_is_array(exprType(newe)))
    2149           4 :                 newa->multidims = true;
    2150             :         }
    2151             : 
    2152        8726 :         newelems = lappend(newelems, newe);
    2153             :     }
    2154             : 
    2155             :     /*
    2156             :      * Select a target type for the elements.
    2157             :      *
    2158             :      * If we haven't been given a target array type, we must try to deduce a
    2159             :      * common type based on the types of the individual elements present.
    2160             :      */
    2161        3570 :     if (OidIsValid(array_type))
    2162             :     {
    2163             :         /* Caller must ensure array_type matches element_type */
    2164             :         Assert(OidIsValid(element_type));
    2165         444 :         coerce_type = (newa->multidims ? array_type : element_type);
    2166         444 :         coerce_hard = true;
    2167             :     }
    2168             :     else
    2169             :     {
    2170             :         /* Can't handle an empty array without a target type */
    2171        3126 :         if (newelems == NIL)
    2172           4 :             ereport(ERROR,
    2173             :                     (errcode(ERRCODE_INDETERMINATE_DATATYPE),
    2174             :                      errmsg("cannot determine type of empty array"),
    2175             :                      errhint("Explicitly cast to the desired type, "
    2176             :                              "for example ARRAY[]::integer[]."),
    2177             :                      parser_errposition(pstate, a->location)));
    2178             : 
    2179             :         /* Select a common type for the elements */
    2180        3122 :         coerce_type = select_common_type(pstate, newelems, "ARRAY", NULL);
    2181             : 
    2182        3122 :         if (newa->multidims)
    2183             :         {
    2184         294 :             array_type = coerce_type;
    2185         294 :             element_type = get_element_type(array_type);
    2186         294 :             if (!OidIsValid(element_type))
    2187           0 :                 ereport(ERROR,
    2188             :                         (errcode(ERRCODE_UNDEFINED_OBJECT),
    2189             :                          errmsg("could not find element type for data type %s",
    2190             :                                 format_type_be(array_type)),
    2191             :                          parser_errposition(pstate, a->location)));
    2192             :         }
    2193             :         else
    2194             :         {
    2195        2828 :             element_type = coerce_type;
    2196        2828 :             array_type = get_array_type(element_type);
    2197        2828 :             if (!OidIsValid(array_type))
    2198           0 :                 ereport(ERROR,
    2199             :                         (errcode(ERRCODE_UNDEFINED_OBJECT),
    2200             :                          errmsg("could not find array type for data type %s",
    2201             :                                 format_type_be(element_type)),
    2202             :                          parser_errposition(pstate, a->location)));
    2203             :         }
    2204        3122 :         coerce_hard = false;
    2205             :     }
    2206             : 
    2207             :     /*
    2208             :      * Coerce elements to target type
    2209             :      *
    2210             :      * If the array has been explicitly cast, then the elements are in turn
    2211             :      * explicitly coerced.
    2212             :      *
    2213             :      * If the array's type was merely derived from the common type of its
    2214             :      * elements, then the elements are implicitly coerced to the common type.
    2215             :      * This is consistent with other uses of select_common_type().
    2216             :      */
    2217       12292 :     foreach(element, newelems)
    2218             :     {
    2219        8726 :         Node       *e = (Node *) lfirst(element);
    2220             :         Node       *newe;
    2221             : 
    2222        8726 :         if (coerce_hard)
    2223             :         {
    2224        1112 :             newe = coerce_to_target_type(pstate, e,
    2225             :                                          exprType(e),
    2226             :                                          coerce_type,
    2227             :                                          typmod,
    2228             :                                          COERCION_EXPLICIT,
    2229             :                                          COERCE_EXPLICIT_CAST,
    2230             :                                          -1);
    2231        1112 :             if (newe == NULL)
    2232           0 :                 ereport(ERROR,
    2233             :                         (errcode(ERRCODE_CANNOT_COERCE),
    2234             :                          errmsg("cannot cast type %s to %s",
    2235             :                                 format_type_be(exprType(e)),
    2236             :                                 format_type_be(coerce_type)),
    2237             :                          parser_errposition(pstate, exprLocation(e))));
    2238             :         }
    2239             :         else
    2240        7614 :             newe = coerce_to_common_type(pstate, e,
    2241             :                                          coerce_type,
    2242             :                                          "ARRAY");
    2243        8726 :         newcoercedelems = lappend(newcoercedelems, newe);
    2244             :     }
    2245             : 
    2246        3566 :     newa->array_typeid = array_type;
    2247             :     /* array_collid will be set by parse_collate.c */
    2248        3566 :     newa->element_typeid = element_type;
    2249        3566 :     newa->elements = newcoercedelems;
    2250        3566 :     newa->location = a->location;
    2251             : 
    2252        3566 :     return (Node *) newa;
    2253             : }
    2254             : 
    2255             : static Node *
    2256        6060 : transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault)
    2257             : {
    2258             :     RowExpr    *newr;
    2259             :     char        fname[16];
    2260             :     int         fnum;
    2261             : 
    2262        6060 :     newr = makeNode(RowExpr);
    2263             : 
    2264             :     /* Transform the field expressions */
    2265        6060 :     newr->args = transformExpressionList(pstate, r->args,
    2266             :                                          pstate->p_expr_kind, allowDefault);
    2267             : 
    2268             :     /* Barring later casting, we consider the type RECORD */
    2269        6060 :     newr->row_typeid = RECORDOID;
    2270        6060 :     newr->row_format = COERCE_IMPLICIT_CAST;
    2271             : 
    2272             :     /* ROW() has anonymous columns, so invent some field names */
    2273        6060 :     newr->colnames = NIL;
    2274       20706 :     for (fnum = 1; fnum <= list_length(newr->args); fnum++)
    2275             :     {
    2276       14646 :         snprintf(fname, sizeof(fname), "f%d", fnum);
    2277       14646 :         newr->colnames = lappend(newr->colnames, makeString(pstrdup(fname)));
    2278             :     }
    2279             : 
    2280        6060 :     newr->location = r->location;
    2281             : 
    2282        6060 :     return (Node *) newr;
    2283             : }
    2284             : 
    2285             : static Node *
    2286       17226 : transformCoalesceExpr(ParseState *pstate, CoalesceExpr *c)
    2287             : {
    2288       17226 :     CoalesceExpr *newc = makeNode(CoalesceExpr);
    2289       17226 :     Node       *last_srf = pstate->p_last_srf;
    2290       17226 :     List       *newargs = NIL;
    2291       17226 :     List       *newcoercedargs = NIL;
    2292             :     ListCell   *args;
    2293             : 
    2294       51670 :     foreach(args, c->args)
    2295             :     {
    2296       34444 :         Node       *e = (Node *) lfirst(args);
    2297             :         Node       *newe;
    2298             : 
    2299       34444 :         newe = transformExprRecurse(pstate, e);
    2300       34444 :         newargs = lappend(newargs, newe);
    2301             :     }
    2302             : 
    2303       17226 :     newc->coalescetype = select_common_type(pstate, newargs, "COALESCE", NULL);
    2304             :     /* coalescecollid will be set by parse_collate.c */
    2305             : 
    2306             :     /* Convert arguments if necessary */
    2307       51670 :     foreach(args, newargs)
    2308             :     {
    2309       34444 :         Node       *e = (Node *) lfirst(args);
    2310             :         Node       *newe;
    2311             : 
    2312       34444 :         newe = coerce_to_common_type(pstate, e,
    2313             :                                      newc->coalescetype,
    2314             :                                      "COALESCE");
    2315       34444 :         newcoercedargs = lappend(newcoercedargs, newe);
    2316             :     }
    2317             : 
    2318             :     /* if any subexpression contained a SRF, complain */
    2319       17226 :     if (pstate->p_last_srf != last_srf)
    2320           4 :         ereport(ERROR,
    2321             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    2322             :         /* translator: %s is name of a SQL construct, eg GROUP BY */
    2323             :                  errmsg("set-returning functions are not allowed in %s",
    2324             :                         "COALESCE"),
    2325             :                  errhint("You might be able to move the set-returning function into a LATERAL FROM item."),
    2326             :                  parser_errposition(pstate,
    2327             :                                     exprLocation(pstate->p_last_srf))));
    2328             : 
    2329       17222 :     newc->args = newcoercedargs;
    2330       17222 :     newc->location = c->location;
    2331       17222 :     return (Node *) newc;
    2332             : }
    2333             : 
    2334             : static Node *
    2335         102 : transformMinMaxExpr(ParseState *pstate, MinMaxExpr *m)
    2336             : {
    2337         102 :     MinMaxExpr *newm = makeNode(MinMaxExpr);
    2338         102 :     List       *newargs = NIL;
    2339         102 :     List       *newcoercedargs = NIL;
    2340         102 :     const char *funcname = (m->op == IS_GREATEST) ? "GREATEST" : "LEAST";
    2341             :     ListCell   *args;
    2342             : 
    2343         102 :     newm->op = m->op;
    2344         342 :     foreach(args, m->args)
    2345             :     {
    2346         240 :         Node       *e = (Node *) lfirst(args);
    2347             :         Node       *newe;
    2348             : 
    2349         240 :         newe = transformExprRecurse(pstate, e);
    2350         240 :         newargs = lappend(newargs, newe);
    2351             :     }
    2352             : 
    2353         102 :     newm->minmaxtype = select_common_type(pstate, newargs, funcname, NULL);
    2354             :     /* minmaxcollid and inputcollid will be set by parse_collate.c */
    2355             : 
    2356             :     /* Convert arguments if necessary */
    2357         342 :     foreach(args, newargs)
    2358             :     {
    2359         240 :         Node       *e = (Node *) lfirst(args);
    2360             :         Node       *newe;
    2361             : 
    2362         240 :         newe = coerce_to_common_type(pstate, e,
    2363             :                                      newm->minmaxtype,
    2364             :                                      funcname);
    2365         240 :         newcoercedargs = lappend(newcoercedargs, newe);
    2366             :     }
    2367             : 
    2368         102 :     newm->args = newcoercedargs;
    2369         102 :     newm->location = m->location;
    2370         102 :     return (Node *) newm;
    2371             : }
    2372             : 
    2373             : static Node *
    2374        2320 : transformSQLValueFunction(ParseState *pstate, SQLValueFunction *svf)
    2375             : {
    2376             :     /*
    2377             :      * All we need to do is insert the correct result type and (where needed)
    2378             :      * validate the typmod, so we just modify the node in-place.
    2379             :      */
    2380        2320 :     switch (svf->op)
    2381             :     {
    2382             :         case SVFOP_CURRENT_DATE:
    2383          30 :             svf->type = DATEOID;
    2384          30 :             break;
    2385             :         case SVFOP_CURRENT_TIME:
    2386           4 :             svf->type = TIMETZOID;
    2387           4 :             break;
    2388             :         case SVFOP_CURRENT_TIME_N:
    2389           4 :             svf->type = TIMETZOID;
    2390           4 :             svf->typmod = anytime_typmod_check(true, svf->typmod);
    2391           4 :             break;
    2392             :         case SVFOP_CURRENT_TIMESTAMP:
    2393         248 :             svf->type = TIMESTAMPTZOID;
    2394         248 :             break;
    2395             :         case SVFOP_CURRENT_TIMESTAMP_N:
    2396         326 :             svf->type = TIMESTAMPTZOID;
    2397         326 :             svf->typmod = anytimestamp_typmod_check(true, svf->typmod);
    2398         326 :             break;
    2399             :         case SVFOP_LOCALTIME:
    2400           4 :             svf->type = TIMEOID;
    2401           4 :             break;
    2402             :         case SVFOP_LOCALTIME_N:
    2403           4 :             svf->type = TIMEOID;
    2404           4 :             svf->typmod = anytime_typmod_check(false, svf->typmod);
    2405           4 :             break;
    2406             :         case SVFOP_LOCALTIMESTAMP:
    2407          12 :             svf->type = TIMESTAMPOID;
    2408          12 :             break;
    2409             :         case SVFOP_LOCALTIMESTAMP_N:
    2410           4 :             svf->type = TIMESTAMPOID;
    2411           4 :             svf->typmod = anytimestamp_typmod_check(false, svf->typmod);
    2412           4 :             break;
    2413             :         case SVFOP_CURRENT_ROLE:
    2414             :         case SVFOP_CURRENT_USER:
    2415             :         case SVFOP_USER:
    2416             :         case SVFOP_SESSION_USER:
    2417             :         case SVFOP_CURRENT_CATALOG:
    2418             :         case SVFOP_CURRENT_SCHEMA:
    2419        1684 :             svf->type = NAMEOID;
    2420        1684 :             break;
    2421             :     }
    2422             : 
    2423        2320 :     return (Node *) svf;
    2424             : }
    2425             : 
    2426             : static Node *
    2427         398 : transformXmlExpr(ParseState *pstate, XmlExpr *x)
    2428             : {
    2429             :     XmlExpr    *newx;
    2430             :     ListCell   *lc;
    2431             :     int         i;
    2432             : 
    2433         398 :     if (operator_precedence_warning && x->op == IS_DOCUMENT)
    2434           0 :         emit_precedence_warnings(pstate, PREC_GROUP_POSTFIX_IS, "IS",
    2435           0 :                                  (Node *) linitial(x->args), NULL,
    2436             :                                  x->location);
    2437             : 
    2438         398 :     newx = makeNode(XmlExpr);
    2439         398 :     newx->op = x->op;
    2440         398 :     if (x->name)
    2441         174 :         newx->name = map_sql_identifier_to_xml_name(x->name, false, false);
    2442             :     else
    2443         224 :         newx->name = NULL;
    2444         398 :     newx->xmloption = x->xmloption;
    2445         398 :     newx->type = XMLOID;     /* this just marks the node as transformed */
    2446         398 :     newx->typmod = -1;
    2447         398 :     newx->location = x->location;
    2448             : 
    2449             :     /*
    2450             :      * gram.y built the named args as a list of ResTarget.  Transform each,
    2451             :      * and break the names out as a separate list.
    2452             :      */
    2453         398 :     newx->named_args = NIL;
    2454         398 :     newx->arg_names = NIL;
    2455             : 
    2456         548 :     foreach(lc, x->named_args)
    2457             :     {
    2458         158 :         ResTarget  *r = lfirst_node(ResTarget, lc);
    2459             :         Node       *expr;
    2460             :         char       *argname;
    2461             : 
    2462         158 :         expr = transformExprRecurse(pstate, r->val);
    2463             : 
    2464         158 :         if (r->name)
    2465          74 :             argname = map_sql_identifier_to_xml_name(r->name, false, false);
    2466          84 :         else if (IsA(r->val, ColumnRef))
    2467          80 :             argname = map_sql_identifier_to_xml_name(FigureColname(r->val),
    2468             :                                                      true, false);
    2469             :         else
    2470             :         {
    2471           4 :             ereport(ERROR,
    2472             :                     (errcode(ERRCODE_SYNTAX_ERROR),
    2473             :                      x->op == IS_XMLELEMENT
    2474             :                      ? errmsg("unnamed XML attribute value must be a column reference")
    2475             :                      : errmsg("unnamed XML element value must be a column reference"),
    2476             :                      parser_errposition(pstate, r->location)));
    2477             :             argname = NULL;     /* keep compiler quiet */
    2478             :         }
    2479             : 
    2480             :         /* reject duplicate argnames in XMLELEMENT only */
    2481         154 :         if (x->op == IS_XMLELEMENT)
    2482             :         {
    2483             :             ListCell   *lc2;
    2484             : 
    2485          82 :             foreach(lc2, newx->arg_names)
    2486             :             {
    2487          26 :                 if (strcmp(argname, strVal(lfirst(lc2))) == 0)
    2488           4 :                     ereport(ERROR,
    2489             :                             (errcode(ERRCODE_SYNTAX_ERROR),
    2490             :                              errmsg("XML attribute name \"%s\" appears more than once",
    2491             :                                     argname),
    2492             :                              parser_errposition(pstate, r->location)));
    2493             :             }
    2494             :         }
    2495             : 
    2496         150 :         newx->named_args = lappend(newx->named_args, expr);
    2497         150 :         newx->arg_names = lappend(newx->arg_names, makeString(argname));
    2498             :     }
    2499             : 
    2500             :     /* The other arguments are of varying types depending on the function */
    2501         390 :     newx->args = NIL;
    2502         390 :     i = 0;
    2503         938 :     foreach(lc, x->args)
    2504             :     {
    2505         560 :         Node       *e = (Node *) lfirst(lc);
    2506             :         Node       *newe;
    2507             : 
    2508         560 :         newe = transformExprRecurse(pstate, e);
    2509         560 :         switch (x->op)
    2510             :         {
    2511             :             case IS_XMLCONCAT:
    2512          88 :                 newe = coerce_to_specific_type(pstate, newe, XMLOID,
    2513             :                                                "XMLCONCAT");
    2514          80 :                 break;
    2515             :             case IS_XMLELEMENT:
    2516             :                 /* no coercion necessary */
    2517          92 :                 break;
    2518             :             case IS_XMLFOREST:
    2519           0 :                 newe = coerce_to_specific_type(pstate, newe, XMLOID,
    2520             :                                                "XMLFOREST");
    2521           0 :                 break;
    2522             :             case IS_XMLPARSE:
    2523         188 :                 if (i == 0)
    2524          94 :                     newe = coerce_to_specific_type(pstate, newe, TEXTOID,
    2525             :                                                    "XMLPARSE");
    2526             :                 else
    2527          94 :                     newe = coerce_to_boolean(pstate, newe, "XMLPARSE");
    2528         188 :                 break;
    2529             :             case IS_XMLPI:
    2530          34 :                 newe = coerce_to_specific_type(pstate, newe, TEXTOID,
    2531             :                                                "XMLPI");
    2532          34 :                 break;
    2533             :             case IS_XMLROOT:
    2534         138 :                 if (i == 0)
    2535          46 :                     newe = coerce_to_specific_type(pstate, newe, XMLOID,
    2536             :                                                    "XMLROOT");
    2537          92 :                 else if (i == 1)
    2538          46 :                     newe = coerce_to_specific_type(pstate, newe, TEXTOID,
    2539             :                                                    "XMLROOT");
    2540             :                 else
    2541          46 :                     newe = coerce_to_specific_type(pstate, newe, INT4OID,
    2542             :                                                    "XMLROOT");
    2543         138 :                 break;
    2544             :             case IS_XMLSERIALIZE:
    2545             :                 /* not handled here */
    2546             :                 Assert(false);
    2547           0 :                 break;
    2548             :             case IS_DOCUMENT:
    2549          20 :                 newe = coerce_to_specific_type(pstate, newe, XMLOID,
    2550             :                                                "IS DOCUMENT");
    2551          16 :                 break;
    2552             :         }
    2553         548 :         newx->args = lappend(newx->args, newe);
    2554         548 :         i++;
    2555             :     }
    2556             : 
    2557         378 :     return (Node *) newx;
    2558             : }
    2559             : 
    2560             : static Node *
    2561          24 : transformXmlSerialize(ParseState *pstate, XmlSerialize *xs)
    2562             : {
    2563             :     Node       *result;
    2564             :     XmlExpr    *xexpr;
    2565             :     Oid         targetType;
    2566             :     int32       targetTypmod;
    2567             : 
    2568          24 :     xexpr = makeNode(XmlExpr);
    2569          24 :     xexpr->op = IS_XMLSERIALIZE;
    2570          24 :     xexpr->args = list_make1(coerce_to_specific_type(pstate,
    2571             :                                                      transformExprRecurse(pstate, xs->expr),
    2572             :                                                      XMLOID,
    2573             :                                                      "XMLSERIALIZE"));
    2574             : 
    2575          24 :     typenameTypeIdAndMod(pstate, xs->typeName, &targetType, &targetTypmod);
    2576             : 
    2577          24 :     xexpr->xmloption = xs->xmloption;
    2578          24 :     xexpr->location = xs->location;
    2579             :     /* We actually only need these to be able to parse back the expression. */
    2580          24 :     xexpr->type = targetType;
    2581          24 :     xexpr->typmod = targetTypmod;
    2582             : 
    2583             :     /*
    2584             :      * The actual target type is determined this way.  SQL allows char and
    2585             :      * varchar as target types.  We allow anything that can be cast implicitly
    2586             :      * from text.  This way, user-defined text-like data types automatically
    2587             :      * fit in.
    2588             :      */
    2589          24 :     result = coerce_to_target_type(pstate, (Node *) xexpr,
    2590             :                                    TEXTOID, targetType, targetTypmod,
    2591             :                                    COERCION_IMPLICIT,
    2592             :                                    COERCE_IMPLICIT_CAST,
    2593             :                                    -1);
    2594          24 :     if (result == NULL)
    2595           0 :         ereport(ERROR,
    2596             :                 (errcode(ERRCODE_CANNOT_COERCE),
    2597             :                  errmsg("cannot cast XMLSERIALIZE result to %s",
    2598             :                         format_type_be(targetType)),
    2599             :                  parser_errposition(pstate, xexpr->location)));
    2600          24 :     return result;
    2601             : }
    2602             : 
    2603             : static Node *
    2604         130 : transformBooleanTest(ParseState *pstate, BooleanTest *b)
    2605             : {
    2606             :     const char *clausename;
    2607             : 
    2608         130 :     if (operator_precedence_warning)
    2609           0 :         emit_precedence_warnings(pstate, PREC_GROUP_POSTFIX_IS, "IS",
    2610           0 :                                  (Node *) b->arg, NULL,
    2611             :                                  b->location);
    2612             : 
    2613         130 :     switch (b->booltesttype)
    2614             :     {
    2615             :         case IS_TRUE:
    2616          28 :             clausename = "IS TRUE";
    2617          28 :             break;
    2618             :         case IS_NOT_TRUE:
    2619          34 :             clausename = "IS NOT TRUE";
    2620          34 :             break;
    2621             :         case IS_FALSE:
    2622          28 :             clausename = "IS FALSE";
    2623          28 :             break;
    2624             :         case IS_NOT_FALSE:
    2625          20 :             clausename = "IS NOT FALSE";
    2626          20 :             break;
    2627             :         case IS_UNKNOWN:
    2628          12 :             clausename = "IS UNKNOWN";
    2629          12 :             break;
    2630             :         case IS_NOT_UNKNOWN:
    2631           8 :             clausename = "IS NOT UNKNOWN";
    2632           8 :             break;
    2633             :         default:
    2634           0 :             elog(ERROR, "unrecognized booltesttype: %d",
    2635             :                  (int) b->booltesttype);
    2636             :             clausename = NULL;  /* keep compiler quiet */
    2637             :     }
    2638             : 
    2639         130 :     b->arg = (Expr *) transformExprRecurse(pstate, (Node *) b->arg);
    2640             : 
    2641         130 :     b->arg = (Expr *) coerce_to_boolean(pstate,
    2642         130 :                                         (Node *) b->arg,
    2643             :                                         clausename);
    2644             : 
    2645         130 :     return (Node *) b;
    2646             : }
    2647             : 
    2648             : static Node *
    2649         158 : transformCurrentOfExpr(ParseState *pstate, CurrentOfExpr *cexpr)
    2650             : {
    2651             :     int         sublevels_up;
    2652             : 
    2653             :     /* CURRENT OF can only appear at top level of UPDATE/DELETE */
    2654             :     Assert(pstate->p_target_rangetblentry != NULL);
    2655         158 :     cexpr->cvarno = RTERangeTablePosn(pstate,
    2656             :                                       pstate->p_target_rangetblentry,
    2657             :                                       &sublevels_up);
    2658             :     Assert(sublevels_up == 0);
    2659             : 
    2660             :     /*
    2661             :      * Check to see if the cursor name matches a parameter of type REFCURSOR.
    2662             :      * If so, replace the raw name reference with a parameter reference. (This
    2663             :      * is a hack for the convenience of plpgsql.)
    2664             :      */
    2665         158 :     if (cexpr->cursor_name != NULL) /* in case already transformed */
    2666             :     {
    2667         158 :         ColumnRef  *cref = makeNode(ColumnRef);
    2668         158 :         Node       *node = NULL;
    2669             : 
    2670             :         /* Build an unqualified ColumnRef with the given name */
    2671         158 :         cref->fields = list_make1(makeString(cexpr->cursor_name));
    2672         158 :         cref->location = -1;
    2673             : 
    2674             :         /* See if there is a translation available from a parser hook */
    2675         158 :         if (pstate->p_pre_columnref_hook != NULL)
    2676           8 :             node = pstate->p_pre_columnref_hook(pstate, cref);
    2677         158 :         if (node == NULL && pstate->p_post_columnref_hook != NULL)
    2678           8 :             node = pstate->p_post_columnref_hook(pstate, cref, NULL);
    2679             : 
    2680             :         /*
    2681             :          * XXX Should we throw an error if we get a translation that isn't a
    2682             :          * refcursor Param?  For now it seems best to silently ignore false
    2683             :          * matches.
    2684             :          */
    2685         158 :         if (node != NULL && IsA(node, Param))
    2686             :         {
    2687           8 :             Param      *p = (Param *) node;
    2688             : 
    2689          16 :             if (p->paramkind == PARAM_EXTERN &&
    2690           8 :                 p->paramtype == REFCURSOROID)
    2691             :             {
    2692             :                 /* Matches, so convert CURRENT OF to a param reference */
    2693           8 :                 cexpr->cursor_name = NULL;
    2694           8 :                 cexpr->cursor_param = p->paramid;
    2695             :             }
    2696             :         }
    2697             :     }
    2698             : 
    2699         158 :     return (Node *) cexpr;
    2700             : }
    2701             : 
    2702             : /*
    2703             :  * Construct a whole-row reference to represent the notation "relation.*".
    2704             :  */
    2705             : static Node *
    2706       19760 : transformWholeRowRef(ParseState *pstate, RangeTblEntry *rte, int location)
    2707             : {
    2708             :     Var        *result;
    2709             :     int         vnum;
    2710             :     int         sublevels_up;
    2711             : 
    2712             :     /* Find the RTE's rangetable location */
    2713       19760 :     vnum = RTERangeTablePosn(pstate, rte, &sublevels_up);
    2714             : 
    2715             :     /*
    2716             :      * Build the appropriate referencing node.  Note that if the RTE is a
    2717             :      * function returning scalar, we create just a plain reference to the
    2718             :      * function value, not a composite containing a single column.  This is
    2719             :      * pretty inconsistent at first sight, but it's what we've done
    2720             :      * historically.  One argument for it is that "rel" and "rel.*" mean the
    2721             :      * same thing for composite relations, so why not for scalar functions...
    2722             :      */
    2723       19760 :     result = makeWholeRowVar(rte, vnum, sublevels_up, true);
    2724             : 
    2725             :     /* location is not filled in by makeWholeRowVar */
    2726       19760 :     result->location = location;
    2727             : 
    2728             :     /* mark relation as requiring whole-row SELECT access */
    2729       19760 :     markVarForSelectPriv(pstate, result, rte);
    2730             : 
    2731       19760 :     return (Node *) result;
    2732             : }
    2733             : 
    2734             : /*
    2735             :  * Handle an explicit CAST construct.
    2736             :  *
    2737             :  * Transform the argument, look up the type name, and apply any necessary
    2738             :  * coercion function(s).
    2739             :  */
    2740             : static Node *
    2741      361764 : transformTypeCast(ParseState *pstate, TypeCast *tc)
    2742             : {
    2743             :     Node       *result;
    2744      361764 :     Node       *arg = tc->arg;
    2745             :     Node       *expr;
    2746             :     Oid         inputType;
    2747             :     Oid         targetType;
    2748             :     int32       targetTypmod;
    2749             :     int         location;
    2750             : 
    2751             :     /* Look up the type name first */
    2752      361764 :     typenameTypeIdAndMod(pstate, tc->typeName, &targetType, &targetTypmod);
    2753             : 
    2754             :     /*
    2755             :      * Look through any AEXPR_PAREN nodes that may have been inserted thanks
    2756             :      * to operator_precedence_warning.  Otherwise, ARRAY[]::foo[] behaves
    2757             :      * differently from (ARRAY[])::foo[].
    2758             :      */
    2759      725830 :     while (arg && IsA(arg, A_Expr) &&
    2760        2302 :            ((A_Expr *) arg)->kind == AEXPR_PAREN)
    2761           0 :         arg = ((A_Expr *) arg)->lexpr;
    2762             : 
    2763             :     /*
    2764             :      * If the subject of the typecast is an ARRAY[] construct and the target
    2765             :      * type is an array type, we invoke transformArrayExpr() directly so that
    2766             :      * we can pass down the type information.  This avoids some cases where
    2767             :      * transformArrayExpr() might not infer the correct type.  Otherwise, just
    2768             :      * transform the argument normally.
    2769             :      */
    2770      361764 :     if (IsA(arg, A_ArrayExpr))
    2771             :     {
    2772             :         Oid         targetBaseType;
    2773             :         int32       targetBaseTypmod;
    2774             :         Oid         elementType;
    2775             : 
    2776             :         /*
    2777             :          * If target is a domain over array, work with the base array type
    2778             :          * here.  Below, we'll cast the array type to the domain.  In the
    2779             :          * usual case that the target is not a domain, the remaining steps
    2780             :          * will be a no-op.
    2781             :          */
    2782         358 :         targetBaseTypmod = targetTypmod;
    2783         358 :         targetBaseType = getBaseTypeAndTypmod(targetType, &targetBaseTypmod);
    2784         358 :         elementType = get_element_type(targetBaseType);
    2785         358 :         if (OidIsValid(elementType))
    2786             :         {
    2787         348 :             expr = transformArrayExpr(pstate,
    2788             :                                       (A_ArrayExpr *) arg,
    2789             :                                       targetBaseType,
    2790             :                                       elementType,
    2791             :                                       targetBaseTypmod);
    2792             :         }
    2793             :         else
    2794          10 :             expr = transformExprRecurse(pstate, arg);
    2795             :     }
    2796             :     else
    2797      361406 :         expr = transformExprRecurse(pstate, arg);
    2798             : 
    2799      361752 :     inputType = exprType(expr);
    2800      361752 :     if (inputType == InvalidOid)
    2801           0 :         return expr;            /* do nothing if NULL input */
    2802             : 
    2803             :     /*
    2804             :      * Location of the coercion is preferentially the location of the :: or
    2805             :      * CAST symbol, but if there is none then use the location of the type
    2806             :      * name (this can happen in TypeName 'string' syntax, for instance).
    2807             :      */
    2808      361752 :     location = tc->location;
    2809      361752 :     if (location < 0)
    2810       31546 :         location = tc->typeName->location;
    2811             : 
    2812      361752 :     result = coerce_to_target_type(pstate, expr, inputType,
    2813             :                                    targetType, targetTypmod,
    2814             :                                    COERCION_EXPLICIT,
    2815             :                                    COERCE_EXPLICIT_CAST,
    2816             :                                    location);
    2817      360636 :     if (result == NULL)
    2818          16 :         ereport(ERROR,
    2819             :                 (errcode(ERRCODE_CANNOT_COERCE),
    2820             :                  errmsg("cannot cast type %s to %s",
    2821             :                         format_type_be(inputType),
    2822             :                         format_type_be(targetType)),
    2823             :                  parser_coercion_errposition(pstate, location, expr)));
    2824             : 
    2825      360620 :     return result;
    2826             : }
    2827             : 
    2828             : /*
    2829             :  * Handle an explicit COLLATE clause.
    2830             :  *
    2831             :  * Transform the argument, and look up the collation name.
    2832             :  */
    2833             : static Node *
    2834        2568 : transformCollateClause(ParseState *pstate, CollateClause *c)
    2835             : {
    2836             :     CollateExpr *newc;
    2837             :     Oid         argtype;
    2838             : 
    2839        2568 :     newc = makeNode(CollateExpr);
    2840        2568 :     newc->arg = (Expr *) transformExprRecurse(pstate, c->arg);
    2841             : 
    2842        2568 :     argtype = exprType((Node *) newc->arg);
    2843             : 
    2844             :     /*
    2845             :      * The unknown type is not collatable, but coerce_type() takes care of it
    2846             :      * separately, so we'll let it go here.
    2847             :      */
    2848        2568 :     if (!type_is_collatable(argtype) && argtype != UNKNOWNOID)
    2849           4 :         ereport(ERROR,
    2850             :                 (errcode(ERRCODE_DATATYPE_MISMATCH),
    2851             :                  errmsg("collations are not supported by type %s",
    2852             :                         format_type_be(argtype)),
    2853             :                  parser_errposition(pstate, c->location)));
    2854             : 
    2855        2564 :     newc->collOid = LookupCollation(pstate, c->collname, c->location);
    2856        2564 :     newc->location = c->location;
    2857             : 
    2858        2564 :     return (Node *) newc;
    2859             : }
    2860             : 
    2861             : /*
    2862             :  * Transform a "row compare-op row" construct
    2863             :  *
    2864             :  * The inputs are lists of already-transformed expressions.
    2865             :  * As with coerce_type, pstate may be NULL if no special unknown-Param
    2866             :  * processing is wanted.
    2867             :  *
    2868             :  * The output may be a single OpExpr, an AND or OR combination of OpExprs,
    2869             :  * or a RowCompareExpr.  In all cases it is guaranteed to return boolean.
    2870             :  * The AND, OR, and RowCompareExpr cases further imply things about the
    2871             :  * behavior of the operators (ie, they behave as =, <>, or < <= > >=).
    2872             :  */
    2873             : static Node *
    2874        6022 : make_row_comparison_op(ParseState *pstate, List *opname,
    2875             :                        List *largs, List *rargs, int location)
    2876             : {
    2877             :     RowCompareExpr *rcexpr;
    2878             :     RowCompareType rctype;
    2879             :     List       *opexprs;
    2880             :     List       *opnos;
    2881             :     List       *opfamilies;
    2882             :     ListCell   *l,
    2883             :                *r;
    2884             :     List      **opinfo_lists;
    2885             :     Bitmapset  *strats;
    2886             :     int         nopers;
    2887             :     int         i;
    2888             : 
    2889        6022 :     nopers = list_length(largs);
    2890        6022 :     if (nopers != list_length(rargs))
    2891           0 :         ereport(ERROR,
    2892             :                 (errcode(ERRCODE_SYNTAX_ERROR),
    2893             :                  errmsg("unequal number of entries in row expressions"),
    2894             :                  parser_errposition(pstate, location)));
    2895             : 
    2896             :     /*
    2897             :      * We can't compare zero-length rows because there is no principled basis
    2898             :      * for figuring out what the operator is.
    2899             :      */
    2900        6022 :     if (nopers == 0)
    2901           4 :         ereport(ERROR,
    2902             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    2903             :                  errmsg("cannot compare rows of zero length"),
    2904             :                  parser_errposition(pstate, location)));
    2905             : 
    2906             :     /*
    2907             :      * Identify all the pairwise operators, using make_op so that behavior is
    2908             :      * the same as in the simple scalar case.
    2909             :      */
    2910        6018 :     opexprs = NIL;
    2911       14860 :     forboth(l, largs, r, rargs)
    2912             :     {
    2913        8846 :         Node       *larg = (Node *) lfirst(l);
    2914        8846 :         Node       *rarg = (Node *) lfirst(r);
    2915             :         OpExpr     *cmp;
    2916             : 
    2917        8846 :         cmp = castNode(OpExpr, make_op(pstate, opname, larg, rarg,
    2918             :                                        pstate->p_last_srf, location));
    2919             : 
    2920             :         /*
    2921             :          * We don't use coerce_to_boolean here because we insist on the
    2922             :          * operator yielding boolean directly, not via coercion.  If it
    2923             :          * doesn't yield bool it won't be in any index opfamilies...
    2924             :          */
    2925        8842 :         if (cmp->opresulttype != BOOLOID)
    2926           0 :             ereport(ERROR,
    2927             :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
    2928             :                      errmsg("row comparison operator must yield type boolean, "
    2929             :                             "not type %s",
    2930             :                             format_type_be(cmp->opresulttype)),
    2931             :                      parser_errposition(pstate, location)));
    2932        8842 :         if (expression_returns_set((Node *) cmp))
    2933           0 :             ereport(ERROR,
    2934             :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
    2935             :                      errmsg("row comparison operator must not return a set"),
    2936             :                      parser_errposition(pstate, location)));
    2937        8842 :         opexprs = lappend(opexprs, cmp);
    2938             :     }
    2939             : 
    2940             :     /*
    2941             :      * If rows are length 1, just return the single operator.  In this case we
    2942             :      * don't insist on identifying btree semantics for the operator (but we
    2943             :      * still require it to return boolean).
    2944             :      */
    2945        6014 :     if (nopers == 1)
    2946        3878 :         return (Node *) linitial(opexprs);
    2947             : 
    2948             :     /*
    2949             :      * Now we must determine which row comparison semantics (= <> < <= > >=)
    2950             :      * apply to this set of operators.  We look for btree opfamilies
    2951             :      * containing the operators, and see which interpretations (strategy
    2952             :      * numbers) exist for each operator.
    2953             :      */
    2954        2136 :     opinfo_lists = (List **) palloc(nopers * sizeof(List *));
    2955        2136 :     strats = NULL;
    2956        2136 :     i = 0;
    2957        7100 :     foreach(l, opexprs)
    2958             :     {
    2959        4964 :         Oid         opno = ((OpExpr *) lfirst(l))->opno;
    2960             :         Bitmapset  *this_strats;
    2961             :         ListCell   *j;
    2962             : 
    2963        4964 :         opinfo_lists[i] = get_op_btree_interpretation(opno);
    2964             : 
    2965             :         /*
    2966             :          * convert strategy numbers into a Bitmapset to make the intersection
    2967             :          * calculation easy.
    2968             :          */
    2969        4964 :         this_strats = NULL;
    2970       10282 :         foreach(j, opinfo_lists[i])
    2971             :         {
    2972        5318 :             OpBtreeInterpretation *opinfo = lfirst(j);
    2973             : 
    2974        5318 :             this_strats = bms_add_member(this_strats, opinfo->strategy);
    2975             :         }
    2976        4964 :         if (i == 0)
    2977        2136 :             strats = this_strats;
    2978             :         else
    2979        2828 :             strats = bms_int_members(strats, this_strats);
    2980        4964 :         i++;
    2981             :     }
    2982             : 
    2983             :     /*
    2984             :      * If there are multiple common interpretations, we may use any one of
    2985             :      * them ... this coding arbitrarily picks the lowest btree strategy
    2986             :      * number.
    2987             :      */
    2988        2136 :     i = bms_first_member(strats);
    2989        2136 :     if (i < 0)
    2990             :     {
    2991             :         /* No common interpretation, so fail */
    2992           4 :         ereport(ERROR,
    2993             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    2994             :                  errmsg("could not determine interpretation of row comparison operator %s",
    2995             :                         strVal(llast(opname))),
    2996             :                  errhint("Row comparison operators must be associated with btree operator families."),
    2997             :                  parser_errposition(pstate, location)));
    2998             :     }
    2999        2132 :     rctype = (RowCompareType) i;
    3000             : 
    3001             :     /*
    3002             :      * For = and <> cases, we just combine the pairwise operators with AND or
    3003             :      * OR respectively.
    3004             :      */
    3005        2132 :     if (rctype == ROWCOMPARE_EQ)
    3006         736 :         return (Node *) makeBoolExpr(AND_EXPR, opexprs, location);
    3007        1396 :     if (rctype == ROWCOMPARE_NE)
    3008        1292 :         return (Node *) makeBoolExpr(OR_EXPR, opexprs, location);
    3009             : 
    3010             :     /*
    3011             :      * Otherwise we need to choose exactly which opfamily to associate with
    3012             :      * each operator.
    3013             :      */
    3014         104 :     opfamilies = NIL;
    3015         348 :     for (i = 0; i < nopers; i++)
    3016             :     {
    3017         244 :         Oid         opfamily = InvalidOid;
    3018             :         ListCell   *j;
    3019             : 
    3020         244 :         foreach(j, opinfo_lists[i])
    3021             :         {
    3022         244 :             OpBtreeInterpretation *opinfo = lfirst(j);
    3023             : 
    3024         244 :             if (opinfo->strategy == rctype)
    3025             :             {
    3026         244 :                 opfamily = opinfo->opfamily_id;
    3027         244 :                 break;
    3028             :             }
    3029             :         }
    3030         244 :         if (OidIsValid(opfamily))
    3031         244 :             opfamilies = lappend_oid(opfamilies, opfamily);
    3032             :         else                    /* should not happen */
    3033           0 :             ereport(ERROR,
    3034             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3035             :                      errmsg("could not determine interpretation of row comparison operator %s",
    3036             :                             strVal(llast(opname))),
    3037             :                      errdetail("There are multiple equally-plausible candidates."),
    3038             :                      parser_errposition(pstate, location)));
    3039             :     }
    3040             : 
    3041             :     /*
    3042             :      * Now deconstruct the OpExprs and create a RowCompareExpr.
    3043             :      *
    3044             :      * Note: can't just reuse the passed largs/rargs lists, because of
    3045             :      * possibility that make_op inserted coercion operations.
    3046             :      */
    3047         104 :     opnos = NIL;
    3048         104 :     largs = NIL;
    3049         104 :     rargs = NIL;
    3050         348 :     foreach(l, opexprs)
    3051             :     {
    3052         244 :         OpExpr     *cmp = (OpExpr *) lfirst(l);
    3053             : 
    3054         244 :         opnos = lappend_oid(opnos, cmp->opno);
    3055         244 :         largs = lappend(largs, linitial(cmp->args));
    3056         244 :         rargs = lappend(rargs, lsecond(cmp->args));
    3057             :     }
    3058             : 
    3059         104 :     rcexpr = makeNode(RowCompareExpr);
    3060         104 :     rcexpr->rctype = rctype;
    3061         104 :     rcexpr->opnos = opnos;
    3062         104 :     rcexpr->opfamilies = opfamilies;
    3063         104 :     rcexpr->inputcollids = NIL; /* assign_expr_collations will fix this */
    3064         104 :     rcexpr->largs = largs;
    3065         104 :     rcexpr->rargs = rargs;
    3066             : 
    3067         104 :     return (Node *) rcexpr;
    3068             : }
    3069             : 
    3070             : /*
    3071             :  * Transform a "row IS DISTINCT FROM row" construct
    3072             :  *
    3073             :  * The input RowExprs are already transformed
    3074             :  */
    3075             : static Node *
    3076           4 : make_row_distinct_op(ParseState *pstate, List *opname,
    3077             :                      RowExpr *lrow, RowExpr *rrow,
    3078             :                      int location)
    3079             : {
    3080           4 :     Node       *result = NULL;
    3081           4 :     List       *largs = lrow->args;
    3082           4 :     List       *rargs = rrow->args;
    3083             :     ListCell   *l,
    3084             :                *r;
    3085             : 
    3086           4 :     if (list_length(largs) != list_length(rargs))
    3087           0 :         ereport(ERROR,
    3088             :                 (errcode(ERRCODE_SYNTAX_ERROR),
    3089             :                  errmsg("unequal number of entries in row expressions"),
    3090             :                  parser_errposition(pstate, location)));
    3091             : 
    3092          16 :     forboth(l, largs, r, rargs)
    3093             :     {
    3094          12 :         Node       *larg = (Node *) lfirst(l);
    3095          12 :         Node       *rarg = (Node *) lfirst(r);
    3096             :         Node       *cmp;
    3097             : 
    3098          12 :         cmp = (Node *) make_distinct_op(pstate, opname, larg, rarg, location);
    3099          12 :         if (result == NULL)
    3100           4 :             result = cmp;
    3101             :         else
    3102           8 :             result = (Node *) makeBoolExpr(OR_EXPR,
    3103           8 :                                            list_make2(result, cmp),
    3104             :                                            location);
    3105             :     }
    3106             : 
    3107           4 :     if (result == NULL)
    3108             :     {
    3109             :         /* zero-length rows?  Generate constant FALSE */
    3110           0 :         result = makeBoolConst(false, false);
    3111             :     }
    3112             : 
    3113           4 :     return result;
    3114             : }
    3115             : 
    3116             : /*
    3117             :  * make the node for an IS DISTINCT FROM operator
    3118             :  */
    3119             : static Expr *
    3120         482 : make_distinct_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree,
    3121             :                  int location)
    3122             : {
    3123             :     Expr       *result;
    3124             : 
    3125         482 :     result = make_op(pstate, opname, ltree, rtree,
    3126             :                      pstate->p_last_srf, location);
    3127         482 :     if (((OpExpr *) result)->opresulttype != BOOLOID)
    3128           0 :         ereport(ERROR,
    3129             :                 (errcode(ERRCODE_DATATYPE_MISMATCH),
    3130             :                  errmsg("IS DISTINCT FROM requires = operator to yield boolean"),
    3131             :                  parser_errposition(pstate, location)));
    3132         482 :     if (((OpExpr *) result)->opretset)
    3133           0 :         ereport(ERROR,
    3134             :                 (errcode(ERRCODE_DATATYPE_MISMATCH),
    3135             :         /* translator: %s is name of a SQL construct, eg NULLIF */
    3136             :                  errmsg("%s must not return a set", "IS DISTINCT FROM"),
    3137             :                  parser_errposition(pstate, location)));
    3138             : 
    3139             :     /*
    3140             :      * We rely on DistinctExpr and OpExpr being same struct
    3141             :      */
    3142         482 :     NodeSetTag(result, T_DistinctExpr);
    3143             : 
    3144         482 :     return result;
    3145             : }
    3146             : 
    3147             : /*
    3148             :  * Produce a NullTest node from an IS [NOT] DISTINCT FROM NULL construct
    3149             :  *
    3150             :  * "arg" is the untransformed other argument
    3151             :  */
    3152             : static Node *
    3153          20 : make_nulltest_from_distinct(ParseState *pstate, A_Expr *distincta, Node *arg)
    3154             : {
    3155          20 :     NullTest   *nt = makeNode(NullTest);
    3156             : 
    3157          20 :     nt->arg = (Expr *) transformExprRecurse(pstate, arg);
    3158             :     /* the argument can be any type, so don't coerce it */
    3159          20 :     if (distincta->kind == AEXPR_NOT_DISTINCT)
    3160           8 :         nt->nulltesttype = IS_NULL;
    3161             :     else
    3162          12 :         nt->nulltesttype = IS_NOT_NULL;
    3163             :     /* argisrow = false is correct whether or not arg is composite */
    3164          20 :     nt->argisrow = false;
    3165          20 :     nt->location = distincta->location;
    3166          20 :     return (Node *) nt;
    3167             : }
    3168             : 
    3169             : /*
    3170             :  * Identify node's group for operator precedence warnings
    3171             :  *
    3172             :  * For items in nonzero groups, also return a suitable node name into *nodename
    3173             :  *
    3174             :  * Note: group zero is used for nodes that are higher or lower precedence
    3175             :  * than everything that changed precedence; we need never issue warnings
    3176             :  * related to such nodes.
    3177             :  */
    3178             : static int
    3179           0 : operator_precedence_group(Node *node, const char **nodename)
    3180             : {
    3181           0 :     int         group = 0;
    3182             : 
    3183           0 :     *nodename = NULL;
    3184           0 :     if (node == NULL)
    3185           0 :         return 0;
    3186             : 
    3187           0 :     if (IsA(node, A_Expr))
    3188             :     {
    3189           0 :         A_Expr     *aexpr = (A_Expr *) node;
    3190             : 
    3191           0 :         if (aexpr->kind == AEXPR_OP &&
    3192           0 :             aexpr->lexpr != NULL &&
    3193           0 :             aexpr->rexpr != NULL)
    3194             :         {
    3195             :             /* binary operator */
    3196           0 :             if (list_length(aexpr->name) == 1)
    3197             :             {
    3198           0 :                 *nodename = strVal(linitial(aexpr->name));
    3199             :                 /* Ignore if op was always higher priority than IS-tests */
    3200           0 :                 if (strcmp(*nodename, "+") == 0 ||
    3201           0 :                     strcmp(*nodename, "-") == 0 ||
    3202           0 :                     strcmp(*nodename, "*") == 0 ||
    3203           0 :                     strcmp(*nodename, "/") == 0 ||
    3204           0 :                     strcmp(*nodename, "%") == 0 ||
    3205           0 :                     strcmp(*nodename, "^") == 0)
    3206           0 :                     group = 0;
    3207           0 :                 else if (strcmp(*nodename, "<") == 0 ||
    3208           0 :                          strcmp(*nodename, ">") == 0)
    3209           0 :                     group = PREC_GROUP_LESS;
    3210           0 :                 else if (strcmp(*nodename, "=") == 0)
    3211           0 :                     group = PREC_GROUP_EQUAL;
    3212           0 :                 else if (strcmp(*nodename, "<=") == 0 ||
    3213           0 :                          strcmp(*nodename, ">=") == 0 ||
    3214           0 :                          strcmp(*nodename, "<>") == 0)
    3215           0 :                     group = PREC_GROUP_LESS_EQUAL;
    3216             :                 else
    3217           0 :                     group = PREC_GROUP_INFIX_OP;
    3218             :             }
    3219             :             else
    3220             :             {
    3221             :                 /* schema-qualified operator syntax */
    3222           0 :                 *nodename = "OPERATOR()";
    3223           0 :                 group = PREC_GROUP_INFIX_OP;
    3224             :             }
    3225             :         }
    3226           0 :         else if (aexpr->kind == AEXPR_OP &&
    3227           0 :                  aexpr->lexpr == NULL &&
    3228           0 :                  aexpr->rexpr != NULL)
    3229             :         {
    3230             :             /* prefix operator */
    3231           0 :             if (list_length(aexpr->name) == 1)
    3232             :             {
    3233           0 :                 *nodename = strVal(linitial(aexpr->name));
    3234             :                 /* Ignore if op was always higher priority than IS-tests */
    3235           0 :                 if (strcmp(*nodename, "+") == 0 ||
    3236           0 :                     strcmp(*nodename, "-") == 0)
    3237           0 :                     group = 0;
    3238             :                 else
    3239           0 :                     group = PREC_GROUP_PREFIX_OP;
    3240             :             }
    3241             :             else
    3242             :             {
    3243             :                 /* schema-qualified operator syntax */
    3244           0 :                 *nodename = "OPERATOR()";
    3245           0 :                 group = PREC_GROUP_PREFIX_OP;
    3246             :             }
    3247             :         }
    3248           0 :         else if (aexpr->kind == AEXPR_OP &&
    3249           0 :                  aexpr->lexpr != NULL &&
    3250           0 :                  aexpr->rexpr == NULL)
    3251             :         {
    3252             :             /* postfix operator */
    3253           0 :             if (list_length(aexpr->name) == 1)
    3254             :             {
    3255           0 :                 *nodename = strVal(linitial(aexpr->name));
    3256           0 :                 group = PREC_GROUP_POSTFIX_OP;
    3257             :             }
    3258             :             else
    3259             :             {
    3260             :                 /* schema-qualified operator syntax */
    3261           0 :                 *nodename = "OPERATOR()";
    3262           0 :                 group = PREC_GROUP_POSTFIX_OP;
    3263             :             }
    3264             :         }
    3265           0 :         else if (aexpr->kind == AEXPR_OP_ANY ||
    3266           0 :                  aexpr->kind == AEXPR_OP_ALL)
    3267             :         {
    3268           0 :             *nodename = strVal(llast(aexpr->name));
    3269           0 :             group = PREC_GROUP_POSTFIX_OP;
    3270             :         }
    3271           0 :         else if (aexpr->kind == AEXPR_DISTINCT ||
    3272           0 :                  aexpr->kind == AEXPR_NOT_DISTINCT)
    3273             :         {
    3274           0 :             *nodename = "IS";
    3275           0 :             group = PREC_GROUP_INFIX_IS;
    3276             :         }
    3277           0 :         else if (aexpr->kind == AEXPR_OF)
    3278             :         {
    3279           0 :             *nodename = "IS";
    3280           0 :             group = PREC_GROUP_POSTFIX_IS;
    3281             :         }
    3282           0 :         else if (aexpr->kind == AEXPR_IN)
    3283             :         {
    3284           0 :             *nodename = "IN";
    3285           0 :             if (strcmp(strVal(linitial(aexpr->name)), "=") == 0)
    3286           0 :                 group = PREC_GROUP_IN;
    3287             :             else
    3288           0 :                 group = PREC_GROUP_NOT_IN;
    3289             :         }
    3290           0 :         else if (aexpr->kind == AEXPR_LIKE)
    3291             :         {
    3292           0 :             *nodename = "LIKE";
    3293           0 :             if (strcmp(strVal(linitial(aexpr->name)), "~~") == 0)
    3294           0 :                 group = PREC_GROUP_LIKE;
    3295             :             else
    3296           0 :                 group = PREC_GROUP_NOT_LIKE;
    3297             :         }
    3298           0 :         else if (aexpr->kind == AEXPR_ILIKE)
    3299             :         {
    3300           0 :             *nodename = "ILIKE";
    3301           0 :             if (strcmp(strVal(linitial(aexpr->name)), "~~*") == 0)
    3302           0 :                 group = PREC_GROUP_LIKE;
    3303             :             else
    3304           0 :                 group = PREC_GROUP_NOT_LIKE;
    3305             :         }
    3306           0 :         else if (aexpr->kind == AEXPR_SIMILAR)
    3307             :         {
    3308           0 :             *nodename = "SIMILAR";
    3309           0 :             if (strcmp(strVal(linitial(aexpr->name)), "~") == 0)
    3310           0 :                 group = PREC_GROUP_LIKE;
    3311             :             else
    3312           0 :                 group = PREC_GROUP_NOT_LIKE;
    3313             :         }
    3314           0 :         else if (aexpr->kind == AEXPR_BETWEEN ||
    3315           0 :                  aexpr->kind == AEXPR_BETWEEN_SYM)
    3316             :         {
    3317             :             Assert(list_length(aexpr->name) == 1);
    3318           0 :             *nodename = strVal(linitial(aexpr->name));
    3319           0 :             group = PREC_GROUP_BETWEEN;
    3320             :         }
    3321           0 :         else if (aexpr->kind == AEXPR_NOT_BETWEEN ||
    3322           0 :                  aexpr->kind == AEXPR_NOT_BETWEEN_SYM)
    3323             :         {
    3324             :             Assert(list_length(aexpr->name) == 1);
    3325           0 :             *nodename = strVal(linitial(aexpr->name));
    3326           0 :             group = PREC_GROUP_NOT_BETWEEN;
    3327             :         }
    3328             :     }
    3329           0 :     else if (IsA(node, NullTest) ||
    3330           0 :              IsA(node, BooleanTest))
    3331             :     {
    3332           0 :         *nodename = "IS";
    3333           0 :         group = PREC_GROUP_POSTFIX_IS;
    3334             :     }
    3335           0 :     else if (IsA(node, XmlExpr))
    3336             :     {
    3337           0 :         XmlExpr    *x = (XmlExpr *) node;
    3338             : 
    3339           0 :         if (x->op == IS_DOCUMENT)
    3340             :         {
    3341           0 :             *nodename = "IS";
    3342           0 :             group = PREC_GROUP_POSTFIX_IS;
    3343             :         }
    3344             :     }
    3345           0 :     else if (IsA(node, SubLink))
    3346             :     {
    3347           0 :         SubLink    *s = (SubLink *) node;
    3348             : 
    3349           0 :         if (s->subLinkType == ANY_SUBLINK ||
    3350           0 :             s->subLinkType == ALL_SUBLINK)
    3351             :         {
    3352           0 :             if (s->operName == NIL)
    3353             :             {
    3354           0 :                 *nodename = "IN";
    3355           0 :                 group = PREC_GROUP_IN;
    3356             :             }
    3357             :             else
    3358             :             {
    3359           0 :                 *nodename = strVal(llast(s->operName));
    3360           0 :                 group = PREC_GROUP_POSTFIX_OP;
    3361             :             }
    3362             :         }
    3363             :     }
    3364           0 :     else if (IsA(node, BoolExpr))
    3365             :     {
    3366             :         /*
    3367             :          * Must dig into NOTs to see if it's IS NOT DOCUMENT or NOT IN.  This
    3368             :          * opens us to possibly misrecognizing, eg, NOT (x IS DOCUMENT) as a
    3369             :          * problematic construct.  We can tell the difference by checking
    3370             :          * whether the parse locations of the two nodes are identical.
    3371             :          *
    3372             :          * Note that when we are comparing the child node to its own children,
    3373             :          * we will not know that it was a NOT.  Fortunately, that doesn't
    3374             :          * matter for these cases.
    3375             :          */
    3376           0 :         BoolExpr   *b = (BoolExpr *) node;
    3377             : 
    3378           0 :         if (b->boolop == NOT_EXPR)
    3379             :         {
    3380           0 :             Node       *child = (Node *) linitial(b->args);
    3381             : 
    3382           0 :             if (IsA(child, XmlExpr))
    3383             :             {
    3384           0 :                 XmlExpr    *x = (XmlExpr *) child;
    3385             : 
    3386           0 :                 if (x->op == IS_DOCUMENT &&
    3387           0 :                     x->location == b->location)
    3388             :                 {
    3389           0 :                     *nodename = "IS";
    3390           0 :                     group = PREC_GROUP_POSTFIX_IS;
    3391             :                 }
    3392             :             }
    3393           0 :             else if (IsA(child, SubLink))
    3394             :             {
    3395           0 :                 SubLink    *s = (SubLink *) child;
    3396             : 
    3397           0 :                 if (s->subLinkType == ANY_SUBLINK && s->operName == NIL &&
    3398           0 :                     s->location == b->location)
    3399             :                 {
    3400           0 :                     *nodename = "IN";
    3401           0 :                     group = PREC_GROUP_NOT_IN;
    3402             :                 }
    3403             :             }
    3404             :         }
    3405             :     }
    3406           0 :     return group;
    3407             : }
    3408             : 
    3409             : /*
    3410             :  * helper routine for delivering 9.4-to-9.5 operator precedence warnings
    3411             :  *
    3412             :  * opgroup/opname/location represent some parent node
    3413             :  * lchild, rchild are its left and right children (either could be NULL)
    3414             :  *
    3415             :  * This should be called before transforming the child nodes, since if a
    3416             :  * precedence-driven parsing change has occurred in a query that used to work,
    3417             :  * it's quite possible that we'll get a semantic failure while analyzing the
    3418             :  * child expression.  We want to produce the warning before that happens.
    3419             :  * In any case, operator_precedence_group() expects untransformed input.
    3420             :  */
    3421             : static void
    3422           0 : emit_precedence_warnings(ParseState *pstate,
    3423             :                          int opgroup, const char *opname,
    3424             :                          Node *lchild, Node *rchild,
    3425             :                          int location)
    3426             : {
    3427             :     int         cgroup;
    3428             :     const char *copname;
    3429             : 
    3430             :     Assert(opgroup > 0);
    3431             : 
    3432             :     /*
    3433             :      * Complain if left child, which should be same or higher precedence
    3434             :      * according to current rules, used to be lower precedence.
    3435             :      *
    3436             :      * Exception to precedence rules: if left child is IN or NOT IN or a
    3437             :      * postfix operator, the grouping is syntactically forced regardless of
    3438             :      * precedence.
    3439             :      */
    3440           0 :     cgroup = operator_precedence_group(lchild, &copname);
    3441           0 :     if (cgroup > 0)
    3442             :     {
    3443           0 :         if (oldprecedence_l[cgroup] < oldprecedence_r[opgroup] &&
    3444           0 :             cgroup != PREC_GROUP_IN &&
    3445           0 :             cgroup != PREC_GROUP_NOT_IN &&
    3446           0 :             cgroup != PREC_GROUP_POSTFIX_OP &&
    3447             :             cgroup != PREC_GROUP_POSTFIX_IS)
    3448           0 :             ereport(WARNING,
    3449             :                     (errmsg("operator precedence change: %s is now lower precedence than %s",
    3450             :                             opname, copname),
    3451             :                      parser_errposition(pstate, location)));
    3452             :     }
    3453             : 
    3454             :     /*
    3455             :      * Complain if right child, which should be higher precedence according to
    3456             :      * current rules, used to be same or lower precedence.
    3457             :      *
    3458             :      * Exception to precedence rules: if right child is a prefix operator, the
    3459             :      * grouping is syntactically forced regardless of precedence.
    3460             :      */
    3461           0 :     cgroup = operator_precedence_group(rchild, &copname);
    3462           0 :     if (cgroup > 0)
    3463             :     {
    3464           0 :         if (oldprecedence_r[cgroup] <= oldprecedence_l[opgroup] &&
    3465             :             cgroup != PREC_GROUP_PREFIX_OP)
    3466           0 :             ereport(WARNING,
    3467             :                     (errmsg("operator precedence change: %s is now lower precedence than %s",
    3468             :                             opname, copname),
    3469             :                      parser_errposition(pstate, location)));
    3470             :     }
    3471           0 : }
    3472             : 
    3473             : /*
    3474             :  * Produce a string identifying an expression by kind.
    3475             :  *
    3476             :  * Note: when practical, use a simple SQL keyword for the result.  If that
    3477             :  * doesn't work well, check call sites to see whether custom error message
    3478             :  * strings are required.
    3479             :  */
    3480             : const char *
    3481          40 : ParseExprKindName(ParseExprKind exprKind)
    3482             : {
    3483          40 :     switch (exprKind)
    3484             :     {
    3485             :         case EXPR_KIND_NONE:
    3486           0 :             return "invalid expression context";
    3487             :         case EXPR_KIND_OTHER:
    3488           0 :             return "extension expression";
    3489             :         case EXPR_KIND_JOIN_ON:
    3490           0 :             return "JOIN/ON";
    3491             :         case EXPR_KIND_JOIN_USING:
    3492           0 :             return "JOIN/USING";
    3493             :         case EXPR_KIND_FROM_SUBSELECT:
    3494           0 :             return "sub-SELECT in FROM";
    3495             :         case EXPR_KIND_FROM_FUNCTION:
    3496           0 :             return "function in FROM";
    3497             :         case EXPR_KIND_WHERE:
    3498          12 :             return "WHERE";
    3499             :         case EXPR_KIND_POLICY:
    3500           0 :             return "POLICY";
    3501             :         case EXPR_KIND_HAVING:
    3502           0 :             return "HAVING";
    3503             :         case EXPR_KIND_FILTER:
    3504           0 :             return "FILTER";
    3505             :         case EXPR_KIND_WINDOW_PARTITION:
    3506           0 :             return "window PARTITION BY";
    3507             :         case EXPR_KIND_WINDOW_ORDER:
    3508           0 :             return "window ORDER BY";
    3509             :         case EXPR_KIND_WINDOW_FRAME_RANGE:
    3510           0 :             return "window RANGE";
    3511             :         case EXPR_KIND_WINDOW_FRAME_ROWS:
    3512           0 :             return "window ROWS";
    3513             :         case EXPR_KIND_WINDOW_FRAME_GROUPS:
    3514           0 :             return "window GROUPS";
    3515             :         case EXPR_KIND_SELECT_TARGET:
    3516           0 :             return "SELECT";
    3517             :         case EXPR_KIND_INSERT_TARGET:
    3518           0 :             return "INSERT";
    3519             :         case EXPR_KIND_UPDATE_SOURCE:
    3520             :         case EXPR_KIND_UPDATE_TARGET:
    3521           4 :             return "UPDATE";
    3522             :         case EXPR_KIND_GROUP_BY:
    3523           8 :             return "GROUP BY";
    3524             :         case EXPR_KIND_ORDER_BY:
    3525           0 :             return "ORDER BY";
    3526             :         case EXPR_KIND_DISTINCT_ON:
    3527           0 :             return "DISTINCT ON";
    3528             :         case EXPR_KIND_LIMIT:
    3529           4 :             return "LIMIT";
    3530             :         case EXPR_KIND_OFFSET:
    3531           0 :             return "OFFSET";
    3532             :         case EXPR_KIND_RETURNING:
    3533           8 :             return "RETURNING";
    3534             :         case EXPR_KIND_VALUES:
    3535             :         case EXPR_KIND_VALUES_SINGLE:
    3536           4 :             return "VALUES";
    3537             :         case EXPR_KIND_CHECK_CONSTRAINT:
    3538             :         case EXPR_KIND_DOMAIN_CHECK:
    3539           0 :             return "CHECK";
    3540             :         case EXPR_KIND_COLUMN_DEFAULT:
    3541             :         case EXPR_KIND_FUNCTION_DEFAULT:
    3542           0 :             return "DEFAULT";
    3543             :         case EXPR_KIND_INDEX_EXPRESSION:
    3544           0 :             return "index expression";
    3545             :         case EXPR_KIND_INDEX_PREDICATE:
    3546           0 :             return "index predicate";
    3547             :         case EXPR_KIND_ALTER_COL_TRANSFORM:
    3548           0 :             return "USING";
    3549             :         case EXPR_KIND_EXECUTE_PARAMETER:
    3550           0 :             return "EXECUTE";
    3551             :         case EXPR_KIND_TRIGGER_WHEN:
    3552           0 :             return "WHEN";
    3553             :         case EXPR_KIND_PARTITION_BOUND:
    3554           0 :             return "partition bound";
    3555             :         case EXPR_KIND_PARTITION_EXPRESSION:
    3556           0 :             return "PARTITION BY";
    3557             :         case EXPR_KIND_CALL_ARGUMENT:
    3558           0 :             return "CALL";
    3559             :         case EXPR_KIND_COPY_WHERE:
    3560           0 :             return "WHERE";
    3561             :         case EXPR_KIND_GENERATED_COLUMN:
    3562           0 :             return "GENERATED AS";
    3563             : 
    3564             :             /*
    3565             :              * There is intentionally no default: case here, so that the
    3566             :              * compiler will warn if we add a new ParseExprKind without
    3567             :              * extending this switch.  If we do see an unrecognized value at
    3568             :              * runtime, we'll fall through to the "unrecognized" return.
    3569             :              */
    3570             :     }
    3571           0 :     return "unrecognized expression kind";
    3572             : }

Generated by: LCOV version 1.13