LCOV - differential code coverage report
Current view: top level - src/backend/parser - parse_collate.c (source / functions) Coverage Total Hit UBC CBC
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 94.7 % 283 268 15 268
Current Date: 2024-04-14 14:21:10 Functions: 100.0 % 10 10 10
Baseline: 16@8cea358b128 Branches: 80.7 % 161 130 31 130
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed (240..) days: 94.7 % 283 268 15 268
Function coverage date bins:
(240..) days: 100.0 % 10 10 10
Branch coverage date bins:
(240..) days: 80.7 % 161 130 31 130

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * parse_collate.c
                                  4                 :                :  *      Routines for assigning collation information.
                                  5                 :                :  *
                                  6                 :                :  * We choose to handle collation analysis in a post-pass over the output
                                  7                 :                :  * of expression parse analysis.  This is because we need more state to
                                  8                 :                :  * perform this processing than is needed in the finished tree.  If we
                                  9                 :                :  * did it on-the-fly while building the tree, all that state would have
                                 10                 :                :  * to be kept in expression node trees permanently.  This way, the extra
                                 11                 :                :  * storage is just local variables in this recursive routine.
                                 12                 :                :  *
                                 13                 :                :  * The info that is actually saved in the finished tree is:
                                 14                 :                :  * 1. The output collation of each expression node, or InvalidOid if it
                                 15                 :                :  * returns a noncollatable data type.  This can also be InvalidOid if the
                                 16                 :                :  * result type is collatable but the collation is indeterminate.
                                 17                 :                :  * 2. The collation to be used in executing each function.  InvalidOid means
                                 18                 :                :  * that there are no collatable inputs or their collation is indeterminate.
                                 19                 :                :  * This value is only stored in node types that might call collation-using
                                 20                 :                :  * functions.
                                 21                 :                :  *
                                 22                 :                :  * You might think we could get away with storing only one collation per
                                 23                 :                :  * node, but the two concepts really need to be kept distinct.  Otherwise
                                 24                 :                :  * it's too confusing when a function produces a collatable output type but
                                 25                 :                :  * has no collatable inputs or produces noncollatable output from collatable
                                 26                 :                :  * inputs.
                                 27                 :                :  *
                                 28                 :                :  * Cases with indeterminate collation might result in an error being thrown
                                 29                 :                :  * at runtime.  If we knew exactly which functions require collation
                                 30                 :                :  * information, we could throw those errors at parse time instead.
                                 31                 :                :  *
                                 32                 :                :  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
                                 33                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                 34                 :                :  *
                                 35                 :                :  *
                                 36                 :                :  * IDENTIFICATION
                                 37                 :                :  *    src/backend/parser/parse_collate.c
                                 38                 :                :  *
                                 39                 :                :  *-------------------------------------------------------------------------
                                 40                 :                :  */
                                 41                 :                : #include "postgres.h"
                                 42                 :                : 
                                 43                 :                : #include "catalog/pg_aggregate.h"
                                 44                 :                : #include "catalog/pg_collation.h"
                                 45                 :                : #include "nodes/makefuncs.h"
                                 46                 :                : #include "nodes/nodeFuncs.h"
                                 47                 :                : #include "parser/parse_collate.h"
                                 48                 :                : #include "utils/lsyscache.h"
                                 49                 :                : 
                                 50                 :                : 
                                 51                 :                : /*
                                 52                 :                :  * Collation strength (the SQL standard calls this "derivation").  Order is
                                 53                 :                :  * chosen to allow comparisons to work usefully.  Note: the standard doesn't
                                 54                 :                :  * seem to distinguish between NONE and CONFLICT.
                                 55                 :                :  */
                                 56                 :                : typedef enum
                                 57                 :                : {
                                 58                 :                :     COLLATE_NONE,               /* expression is of a noncollatable datatype */
                                 59                 :                :     COLLATE_IMPLICIT,           /* collation was derived implicitly */
                                 60                 :                :     COLLATE_CONFLICT,           /* we had a conflict of implicit collations */
                                 61                 :                :     COLLATE_EXPLICIT,           /* collation was derived explicitly */
                                 62                 :                : } CollateStrength;
                                 63                 :                : 
                                 64                 :                : typedef struct
                                 65                 :                : {
                                 66                 :                :     ParseState *pstate;         /* parse state (for error reporting) */
                                 67                 :                :     Oid         collation;      /* OID of current collation, if any */
                                 68                 :                :     CollateStrength strength;   /* strength of current collation choice */
                                 69                 :                :     int         location;       /* location of expr that set collation */
                                 70                 :                :     /* Remaining fields are only valid when strength == COLLATE_CONFLICT */
                                 71                 :                :     Oid         collation2;     /* OID of conflicting collation */
                                 72                 :                :     int         location2;      /* location of expr that set collation2 */
                                 73                 :                : } assign_collations_context;
                                 74                 :                : 
                                 75                 :                : static bool assign_query_collations_walker(Node *node, ParseState *pstate);
                                 76                 :                : static bool assign_collations_walker(Node *node,
                                 77                 :                :                                      assign_collations_context *context);
                                 78                 :                : static void merge_collation_state(Oid collation,
                                 79                 :                :                                   CollateStrength strength,
                                 80                 :                :                                   int location,
                                 81                 :                :                                   Oid collation2,
                                 82                 :                :                                   int location2,
                                 83                 :                :                                   assign_collations_context *context);
                                 84                 :                : static void assign_aggregate_collations(Aggref *aggref,
                                 85                 :                :                                         assign_collations_context *loccontext);
                                 86                 :                : static void assign_ordered_set_collations(Aggref *aggref,
                                 87                 :                :                                           assign_collations_context *loccontext);
                                 88                 :                : static void assign_hypothetical_collations(Aggref *aggref,
                                 89                 :                :                                            assign_collations_context *loccontext);
                                 90                 :                : 
                                 91                 :                : 
                                 92                 :                : /*
                                 93                 :                :  * assign_query_collations()
                                 94                 :                :  *      Mark all expressions in the given Query with collation information.
                                 95                 :                :  *
                                 96                 :                :  * This should be applied to each Query after completion of parse analysis
                                 97                 :                :  * for expressions.  Note that we do not recurse into sub-Queries, since
                                 98                 :                :  * those should have been processed when built.
                                 99                 :                :  */
                                100                 :                : void
 4775 tgl@sss.pgh.pa.us         101                 :CBC      274333 : assign_query_collations(ParseState *pstate, Query *query)
                                102                 :                : {
                                103                 :                :     /*
                                104                 :                :      * We just use query_tree_walker() to visit all the contained expressions.
                                105                 :                :      * We can skip the rangetable and CTE subqueries, though, since RTEs and
                                106                 :                :      * subqueries had better have been processed already (else Vars referring
                                107                 :                :      * to them would not get created with the right collation).
                                108                 :                :      */
                                109                 :         274333 :     (void) query_tree_walker(query,
                                110                 :                :                              assign_query_collations_walker,
                                111                 :                :                              (void *) pstate,
                                112                 :                :                              QTW_IGNORE_RANGE_TABLE |
                                113                 :                :                              QTW_IGNORE_CTE_SUBQUERIES);
                                114                 :         274309 : }
                                115                 :                : 
                                116                 :                : /*
                                117                 :                :  * Walker for assign_query_collations
                                118                 :                :  *
                                119                 :                :  * Each expression found by query_tree_walker is processed independently.
                                120                 :                :  * Note that query_tree_walker may pass us a whole List, such as the
                                121                 :                :  * targetlist, in which case each subexpression must be processed
                                122                 :                :  * independently --- we don't want to bleat if two different targetentries
                                123                 :                :  * have different collations.
                                124                 :                :  */
                                125                 :                : static bool
                                126                 :        3021362 : assign_query_collations_walker(Node *node, ParseState *pstate)
                                127                 :                : {
                                128                 :                :     /* Need do nothing for empty subexpressions */
                                129         [ +  + ]:        3021362 :     if (node == NULL)
                                130                 :        2469548 :         return false;
                                131                 :                : 
                                132                 :                :     /*
                                133                 :                :      * We don't want to recurse into a set-operations tree; it's already been
                                134                 :                :      * fully processed in transformSetOperationStmt.
                                135                 :                :      */
                                136         [ +  + ]:         551814 :     if (IsA(node, SetOperationStmt))
                                137                 :           4140 :         return false;
                                138                 :                : 
                                139         [ +  + ]:         547674 :     if (IsA(node, List))
                                140                 :         267890 :         assign_list_collations(pstate, (List *) node);
                                141                 :                :     else
                                142                 :         279784 :         assign_expr_collations(pstate, node);
                                143                 :                : 
                                144                 :         547650 :     return false;
                                145                 :                : }
                                146                 :                : 
                                147                 :                : /*
                                148                 :                :  * assign_list_collations()
                                149                 :                :  *      Mark all nodes in the list of expressions with collation information.
                                150                 :                :  *
                                151                 :                :  * The list member expressions are processed independently; they do not have
                                152                 :                :  * to share a common collation.
                                153                 :                :  */
                                154                 :                : void
                                155                 :         297390 : assign_list_collations(ParseState *pstate, List *exprs)
                                156                 :                : {
                                157                 :                :     ListCell   *lc;
                                158                 :                : 
                                159   [ +  +  +  +  :         998020 :     foreach(lc, exprs)
                                              +  + ]
                                160                 :                :     {
 4753 bruce@momjian.us          161                 :         700648 :         Node       *node = (Node *) lfirst(lc);
                                162                 :                : 
 4775 tgl@sss.pgh.pa.us         163                 :         700648 :         assign_expr_collations(pstate, node);
                                164                 :                :     }
                                165                 :         297372 : }
                                166                 :                : 
                                167                 :                : /*
                                168                 :                :  * assign_expr_collations()
                                169                 :                :  *      Mark all nodes in the given expression tree with collation information.
                                170                 :                :  *
                                171                 :                :  * This is exported for the benefit of various utility commands that process
                                172                 :                :  * expressions without building a complete Query.  It should be applied after
                                173                 :                :  * calling transformExpr() plus any expression-modifying operations such as
                                174                 :                :  * coerce_to_boolean().
                                175                 :                :  */
                                176                 :                : void
                                177                 :        1067094 : assign_expr_collations(ParseState *pstate, Node *expr)
                                178                 :                : {
                                179                 :                :     assign_collations_context context;
                                180                 :                : 
                                181                 :                :     /* initialize context for tree walk */
                                182                 :        1067094 :     context.pstate = pstate;
                                183                 :        1067094 :     context.collation = InvalidOid;
                                184                 :        1067094 :     context.strength = COLLATE_NONE;
                                185                 :        1067094 :     context.location = -1;
                                186                 :                : 
                                187                 :                :     /* and away we go */
                                188                 :        1067094 :     (void) assign_collations_walker(expr, &context);
                                189                 :        1067067 : }
                                190                 :                : 
                                191                 :                : /*
                                192                 :                :  * select_common_collation()
                                193                 :                :  *      Identify a common collation for a list of expressions.
                                194                 :                :  *
                                195                 :                :  * The expressions should all return the same datatype, else this is not
                                196                 :                :  * terribly meaningful.
                                197                 :                :  *
                                198                 :                :  * none_ok means that it is permitted to return InvalidOid, indicating that
                                199                 :                :  * no common collation could be identified, even for collatable datatypes.
                                200                 :                :  * Otherwise, an error is thrown for conflict of implicit collations.
                                201                 :                :  *
                                202                 :                :  * In theory, none_ok = true reflects the rules of SQL standard clause "Result
                                203                 :                :  * of data type combinations", none_ok = false reflects the rules of clause
                                204                 :                :  * "Collation determination" (in some cases invoked via "Grouping
                                205                 :                :  * operations").
                                206                 :                :  */
                                207                 :                : Oid
                                208                 :          27526 : select_common_collation(ParseState *pstate, List *exprs, bool none_ok)
                                209                 :                : {
                                210                 :                :     assign_collations_context context;
                                211                 :                : 
                                212                 :                :     /* initialize context for tree walk */
                                213                 :          27526 :     context.pstate = pstate;
                                214                 :          27526 :     context.collation = InvalidOid;
                                215                 :          27526 :     context.strength = COLLATE_NONE;
                                216                 :          27526 :     context.location = -1;
                                217                 :                : 
                                218                 :                :     /* and away we go */
                                219                 :          27526 :     (void) assign_collations_walker((Node *) exprs, &context);
                                220                 :                : 
                                221                 :                :     /* deal with collation conflict */
                                222         [ +  + ]:          27526 :     if (context.strength == COLLATE_CONFLICT)
                                223                 :                :     {
                                224         [ +  + ]:             36 :         if (none_ok)
                                225                 :             18 :             return InvalidOid;
                                226         [ +  - ]:             18 :         ereport(ERROR,
                                227                 :                :                 (errcode(ERRCODE_COLLATION_MISMATCH),
                                228                 :                :                  errmsg("collation mismatch between implicit collations \"%s\" and \"%s\"",
                                229                 :                :                         get_collation_name(context.collation),
                                230                 :                :                         get_collation_name(context.collation2)),
                                231                 :                :                  errhint("You can choose the collation by applying the COLLATE clause to one or both expressions."),
                                232                 :                :                  parser_errposition(context.pstate, context.location2)));
                                233                 :                :     }
                                234                 :                : 
                                235                 :                :     /*
                                236                 :                :      * Note: if strength is still COLLATE_NONE, we'll return InvalidOid, but
                                237                 :                :      * that's okay because it must mean none of the expressions returned
                                238                 :                :      * collatable datatypes.
                                239                 :                :      */
                                240                 :          27490 :     return context.collation;
                                241                 :                : }
                                242                 :                : 
                                243                 :                : /*
                                244                 :                :  * assign_collations_walker()
                                245                 :                :  *      Recursive guts of collation processing.
                                246                 :                :  *
                                247                 :                :  * Nodes with no children (eg, Vars, Consts, Params) must have been marked
                                248                 :                :  * when built.  All upper-level nodes are marked here.
                                249                 :                :  *
                                250                 :                :  * Note: if this is invoked directly on a List, it will attempt to infer a
                                251                 :                :  * common collation for all the list members.  In particular, it will throw
                                252                 :                :  * error if there are conflicting explicit collations for different members.
                                253                 :                :  */
                                254                 :                : static bool
                                255                 :        4044817 : assign_collations_walker(Node *node, assign_collations_context *context)
                                256                 :                : {
                                257                 :                :     assign_collations_context loccontext;
                                258                 :                :     Oid         collation;
                                259                 :                :     CollateStrength strength;
                                260                 :                :     int         location;
                                261                 :                : 
                                262                 :                :     /* Need do nothing for empty subexpressions */
                                263         [ +  + ]:        4044817 :     if (node == NULL)
                                264                 :         239914 :         return false;
                                265                 :                : 
                                266                 :                :     /*
                                267                 :                :      * Prepare for recursion.  For most node types, though not all, the first
                                268                 :                :      * thing we do is recurse to process all nodes below this one. Each level
                                269                 :                :      * of the tree has its own local context.
                                270                 :                :      */
                                271                 :        3804903 :     loccontext.pstate = context->pstate;
                                272                 :        3804903 :     loccontext.collation = InvalidOid;
                                273                 :        3804903 :     loccontext.strength = COLLATE_NONE;
                                274                 :        3804903 :     loccontext.location = -1;
                                275                 :                :     /* Set these fields just to suppress uninitialized-value warnings: */
 3765                           276                 :        3804903 :     loccontext.collation2 = InvalidOid;
                                277                 :        3804903 :     loccontext.location2 = -1;
                                278                 :                : 
                                279                 :                :     /*
                                280                 :                :      * Recurse if appropriate, then determine the collation for this node.
                                281                 :                :      *
                                282                 :                :      * Note: the general cases are at the bottom of the switch, after various
                                283                 :                :      * special cases.
                                284                 :                :      */
 4775                           285   [ +  +  +  +  :        3804903 :     switch (nodeTag(node))
                                     +  +  +  +  +  
                                              +  + ]
                                286                 :                :     {
                                287                 :           3839 :         case T_CollateExpr:
                                288                 :                :             {
                                289                 :                :                 /*
                                290                 :                :                  * COLLATE sets an explicitly derived collation, regardless of
                                291                 :                :                  * what the child state is.  But we must recurse to set up
                                292                 :                :                  * collation info below here.
                                293                 :                :                  */
                                294                 :           3839 :                 CollateExpr *expr = (CollateExpr *) node;
                                295                 :                : 
                                296                 :           3839 :                 (void) expression_tree_walker(node,
                                297                 :                :                                               assign_collations_walker,
                                298                 :                :                                               (void *) &loccontext);
                                299                 :                : 
                                300                 :           3839 :                 collation = expr->collOid;
                                301         [ -  + ]:           3839 :                 Assert(OidIsValid(collation));
                                302                 :           3839 :                 strength = COLLATE_EXPLICIT;
                                303                 :           3839 :                 location = expr->location;
                                304                 :                :             }
                                305                 :           3839 :             break;
                                306                 :           7465 :         case T_FieldSelect:
                                307                 :                :             {
                                308                 :                :                 /*
                                309                 :                :                  * For FieldSelect, the result has the field's declared
                                310                 :                :                  * collation, independently of what happened in the arguments.
                                311                 :                :                  * (The immediate argument must be composite and thus not
                                312                 :                :                  * collatable, anyhow.)  The field's collation was already
                                313                 :                :                  * looked up and saved in the node.
                                314                 :                :                  */
                                315                 :           7465 :                 FieldSelect *expr = (FieldSelect *) node;
                                316                 :                : 
                                317                 :                :                 /* ... but first, recurse */
                                318                 :           7465 :                 (void) expression_tree_walker(node,
                                319                 :                :                                               assign_collations_walker,
                                320                 :                :                                               (void *) &loccontext);
                                321                 :                : 
                                322         [ +  + ]:           7465 :                 if (OidIsValid(expr->resultcollid))
                                323                 :                :                 {
                                324                 :                :                     /* Node's result type is collatable. */
                                325                 :                :                     /* Pass up field's collation as an implicit choice. */
                                326                 :           1313 :                     collation = expr->resultcollid;
                                327                 :           1313 :                     strength = COLLATE_IMPLICIT;
                                328                 :           1313 :                     location = exprLocation(node);
                                329                 :                :                 }
                                330                 :                :                 else
                                331                 :                :                 {
                                332                 :                :                     /* Node's result type isn't collatable. */
                                333                 :           6152 :                     collation = InvalidOid;
                                334                 :           6152 :                     strength = COLLATE_NONE;
 2489                           335                 :           6152 :                     location = -1;  /* won't be used */
                                336                 :                :                 }
                                337                 :                :             }
 4775                           338                 :           7465 :             break;
                                339                 :           2037 :         case T_RowExpr:
                                340                 :                :             {
                                341                 :                :                 /*
                                342                 :                :                  * RowExpr is a special case because the subexpressions are
                                343                 :                :                  * independent: we don't want to complain if some of them have
                                344                 :                :                  * incompatible explicit collations.
                                345                 :                :                  */
 4753 bruce@momjian.us          346                 :           2037 :                 RowExpr    *expr = (RowExpr *) node;
                                347                 :                : 
 4775 tgl@sss.pgh.pa.us         348                 :           2037 :                 assign_list_collations(context->pstate, expr->args);
                                349                 :                : 
                                350                 :                :                 /*
                                351                 :                :                  * Since the result is always composite and therefore never
                                352                 :                :                  * has a collation, we can just stop here: this node has no
                                353                 :                :                  * impact on the collation of its parent.
                                354                 :                :                  */
 4753 bruce@momjian.us          355                 :           2037 :                 return false;   /* done */
                                356                 :                :             }
 4775 tgl@sss.pgh.pa.us         357                 :             87 :         case T_RowCompareExpr:
                                358                 :                :             {
                                359                 :                :                 /*
                                360                 :                :                  * For RowCompare, we have to find the common collation of
                                361                 :                :                  * each pair of input columns and build a list.  If we can't
                                362                 :                :                  * find a common collation, we just put InvalidOid into the
                                363                 :                :                  * list, which may or may not cause an error at runtime.
                                364                 :                :                  */
                                365                 :             87 :                 RowCompareExpr *expr = (RowCompareExpr *) node;
                                366                 :             87 :                 List       *colls = NIL;
                                367                 :                :                 ListCell   *l;
                                368                 :                :                 ListCell   *r;
                                369                 :                : 
                                370   [ +  -  +  +  :            288 :                 forboth(l, expr->largs, r, expr->rargs)
                                     +  -  +  +  +  
                                        +  +  -  +  
                                                 + ]
                                371                 :                :                 {
 4753 bruce@momjian.us          372                 :            201 :                     Node       *le = (Node *) lfirst(l);
                                373                 :            201 :                     Node       *re = (Node *) lfirst(r);
                                374                 :                :                     Oid         coll;
                                375                 :                : 
 4775 tgl@sss.pgh.pa.us         376                 :            201 :                     coll = select_common_collation(context->pstate,
                                377                 :            201 :                                                    list_make2(le, re),
                                378                 :                :                                                    true);
                                379                 :            201 :                     colls = lappend_oid(colls, coll);
                                380                 :                :                 }
                                381                 :             87 :                 expr->inputcollids = colls;
                                382                 :                : 
                                383                 :                :                 /*
                                384                 :                :                  * Since the result is always boolean and therefore never has
                                385                 :                :                  * a collation, we can just stop here: this node has no impact
                                386                 :                :                  * on the collation of its parent.
                                387                 :                :                  */
 4753 bruce@momjian.us          388                 :             87 :                 return false;   /* done */
                                389                 :                :             }
 4775 tgl@sss.pgh.pa.us         390                 :          35162 :         case T_CoerceToDomain:
                                391                 :                :             {
                                392                 :                :                 /*
                                393                 :                :                  * If the domain declaration included a non-default COLLATE
                                394                 :                :                  * spec, then use that collation as the output collation of
                                395                 :                :                  * the coercion.  Otherwise allow the input collation to
                                396                 :                :                  * bubble up.  (The input should be of the domain's base type,
                                397                 :                :                  * therefore we don't need to worry about it not being
                                398                 :                :                  * collatable when the domain is.)
                                399                 :                :                  */
                                400                 :          35162 :                 CoerceToDomain *expr = (CoerceToDomain *) node;
 4753 bruce@momjian.us          401                 :          35162 :                 Oid         typcollation = get_typcollation(expr->resulttype);
                                402                 :                : 
                                403                 :                :                 /* ... but first, recurse */
 4775 tgl@sss.pgh.pa.us         404                 :          35162 :                 (void) expression_tree_walker(node,
                                405                 :                :                                               assign_collations_walker,
                                406                 :                :                                               (void *) &loccontext);
                                407                 :                : 
                                408         [ +  + ]:          35162 :                 if (OidIsValid(typcollation))
                                409                 :                :                 {
                                410                 :                :                     /* Node's result type is collatable. */
                                411         [ +  + ]:          28935 :                     if (typcollation == DEFAULT_COLLATION_OID)
                                412                 :                :                     {
                                413                 :                :                         /* Collation state bubbles up from child. */
                                414                 :            267 :                         collation = loccontext.collation;
                                415                 :            267 :                         strength = loccontext.strength;
                                416                 :            267 :                         location = loccontext.location;
                                417                 :                :                     }
                                418                 :                :                     else
                                419                 :                :                     {
                                420                 :                :                         /* Use domain's collation as an implicit choice. */
                                421                 :          28668 :                         collation = typcollation;
                                422                 :          28668 :                         strength = COLLATE_IMPLICIT;
                                423                 :          28668 :                         location = exprLocation(node);
                                424                 :                :                     }
                                425                 :                :                 }
                                426                 :                :                 else
                                427                 :                :                 {
                                428                 :                :                     /* Node's result type isn't collatable. */
                                429                 :           6227 :                     collation = InvalidOid;
                                430                 :           6227 :                     strength = COLLATE_NONE;
 2489                           431                 :           6227 :                     location = -1;  /* won't be used */
                                432                 :                :                 }
                                433                 :                : 
                                434                 :                :                 /*
                                435                 :                :                  * Save the state into the expression node.  We know it
                                436                 :                :                  * doesn't care about input collation.
                                437                 :                :                  */
 4775                           438         [ -  + ]:          35162 :                 if (strength == COLLATE_CONFLICT)
 4775 tgl@sss.pgh.pa.us         439                 :UBC           0 :                     exprSetCollation(node, InvalidOid);
                                440                 :                :                 else
 4775 tgl@sss.pgh.pa.us         441                 :CBC       35162 :                     exprSetCollation(node, collation);
                                442                 :                :             }
                                443                 :          35162 :             break;
                                444                 :         682622 :         case T_TargetEntry:
                                445                 :         682622 :             (void) expression_tree_walker(node,
                                446                 :                :                                           assign_collations_walker,
                                447                 :                :                                           (void *) &loccontext);
                                448                 :                : 
                                449                 :                :             /*
                                450                 :                :              * TargetEntry can have only one child, and should bubble that
                                451                 :                :              * state up to its parent.  We can't use the general-case code
                                452                 :                :              * below because exprType and friends don't work on TargetEntry.
                                453                 :                :              */
                                454                 :         682610 :             collation = loccontext.collation;
                                455                 :         682610 :             strength = loccontext.strength;
                                456                 :         682610 :             location = loccontext.location;
                                457                 :                : 
                                458                 :                :             /*
                                459                 :                :              * Throw error if the collation is indeterminate for a TargetEntry
                                460                 :                :              * that is a sort/group target.  We prefer to do this now, instead
                                461                 :                :              * of leaving the comparison functions to fail at runtime, because
                                462                 :                :              * we can give a syntax error pointer to help locate the problem.
                                463                 :                :              * There are some cases where there might not be a failure, for
                                464                 :                :              * example if the planner chooses to use hash aggregation instead
                                465                 :                :              * of sorting for grouping; but it seems better to predictably
                                466                 :                :              * throw an error.  (Compare transformSetOperationTree, which will
                                467                 :                :              * throw error for indeterminate collation of set-op columns, even
                                468                 :                :              * though the planner might be able to implement the set-op
                                469                 :                :              * without sorting.)
                                470                 :                :              */
 4772                           471         [ +  + ]:         682610 :             if (strength == COLLATE_CONFLICT &&
                                472         [ +  + ]:             15 :                 ((TargetEntry *) node)->ressortgroupref != 0)
                                473         [ +  - ]:              9 :                 ereport(ERROR,
                                474                 :                :                         (errcode(ERRCODE_COLLATION_MISMATCH),
                                475                 :                :                          errmsg("collation mismatch between implicit collations \"%s\" and \"%s\"",
                                476                 :                :                                 get_collation_name(loccontext.collation),
                                477                 :                :                                 get_collation_name(loccontext.collation2)),
                                478                 :                :                          errhint("You can choose the collation by applying the COLLATE clause to one or both expressions."),
                                479                 :                :                          parser_errposition(context->pstate,
                                480                 :                :                                             loccontext.location2)));
 4775                           481                 :         682601 :             break;
 3264 andres@anarazel.de        482                 :         529037 :         case T_InferenceElem:
                                483                 :                :         case T_RangeTblRef:
                                484                 :                :         case T_JoinExpr:
                                485                 :                :         case T_FromExpr:
                                486                 :                :         case T_OnConflictExpr:
                                487                 :                :         case T_SortGroupClause:
                                488                 :                :         case T_MergeAction:
 4775 tgl@sss.pgh.pa.us         489                 :         529037 :             (void) expression_tree_walker(node,
                                490                 :                :                                           assign_collations_walker,
                                491                 :                :                                           (void *) &loccontext);
                                492                 :                : 
                                493                 :                :             /*
                                494                 :                :              * When we're invoked on a query's jointree, we don't need to do
                                495                 :                :              * anything with join nodes except recurse through them to process
                                496                 :                :              * WHERE/ON expressions.  So just stop here.  Likewise, we don't
                                497                 :                :              * need to do anything when invoked on sort/group lists.
                                498                 :                :              */
                                499                 :         529031 :             return false;
                                500                 :          20467 :         case T_Query:
                                501                 :                :             {
                                502                 :                :                 /*
                                503                 :                :                  * We get here when we're invoked on the Query belonging to a
                                504                 :                :                  * SubLink.  Act as though the Query returns its first output
                                505                 :                :                  * column, which indeed is what it does for EXPR_SUBLINK and
                                506                 :                :                  * ARRAY_SUBLINK cases.  In the cases where the SubLink
                                507                 :                :                  * returns boolean, this info will be ignored.  Special case:
                                508                 :                :                  * in EXISTS, the Query might return no columns, in which case
                                509                 :                :                  * we need do nothing.
                                510                 :                :                  *
                                511                 :                :                  * We needn't recurse, since the Query is already processed.
                                512                 :                :                  */
                                513                 :          20467 :                 Query      *qtree = (Query *) node;
                                514                 :                :                 TargetEntry *tent;
                                515                 :                : 
 3786                           516         [ +  + ]:          20467 :                 if (qtree->targetList == NIL)
                                517                 :              3 :                     return false;
 2561                           518                 :          20464 :                 tent = linitial_node(TargetEntry, qtree->targetList);
 3786                           519         [ -  + ]:          20464 :                 if (tent->resjunk)
 3786 tgl@sss.pgh.pa.us         520                 :UBC           0 :                     return false;
                                521                 :                : 
 4775 tgl@sss.pgh.pa.us         522                 :CBC       20464 :                 collation = exprCollation((Node *) tent->expr);
                                523                 :                :                 /* collation doesn't change if it's converted to array */
                                524                 :          20464 :                 strength = COLLATE_IMPLICIT;
                                525                 :          20464 :                 location = exprLocation((Node *) tent->expr);
                                526                 :                :             }
                                527                 :          20464 :             break;
                                528                 :          54547 :         case T_List:
                                529                 :          54547 :             (void) expression_tree_walker(node,
                                530                 :                :                                           assign_collations_walker,
                                531                 :                :                                           (void *) &loccontext);
                                532                 :                : 
                                533                 :                :             /*
                                534                 :                :              * When processing a list, collation state just bubbles up from
                                535                 :                :              * the list elements.
                                536                 :                :              */
                                537                 :          54547 :             collation = loccontext.collation;
                                538                 :          54547 :             strength = loccontext.strength;
                                539                 :          54547 :             location = loccontext.location;
                                540                 :          54547 :             break;
                                541                 :                : 
                                542                 :        1636692 :         case T_Var:
                                543                 :                :         case T_Const:
                                544                 :                :         case T_Param:
                                545                 :                :         case T_CoerceToDomainValue:
                                546                 :                :         case T_CaseTestExpr:
                                547                 :                :         case T_SetToDefault:
                                548                 :                :         case T_CurrentOfExpr:
                                549                 :                : 
                                550                 :                :             /*
                                551                 :                :              * General case for childless expression nodes.  These should
                                552                 :                :              * already have a collation assigned; it is not this function's
                                553                 :                :              * responsibility to look into the catalogs for base-case
                                554                 :                :              * information.
                                555                 :                :              */
                                556                 :        1636692 :             collation = exprCollation(node);
                                557                 :                : 
                                558                 :                :             /*
                                559                 :                :              * Note: in most cases, there will be an assigned collation
                                560                 :                :              * whenever type_is_collatable(exprType(node)); but an exception
                                561                 :                :              * occurs for a Var referencing a subquery output column for which
                                562                 :                :              * a unique collation was not determinable.  That may lead to a
                                563                 :                :              * runtime failure if a collation-sensitive function is applied to
                                564                 :                :              * the Var.
                                565                 :                :              */
                                566                 :                : 
                                567         [ +  + ]:        1636692 :             if (OidIsValid(collation))
                                568                 :         357672 :                 strength = COLLATE_IMPLICIT;
                                569                 :                :             else
                                570                 :        1279020 :                 strength = COLLATE_NONE;
                                571                 :        1636692 :             location = exprLocation(node);
                                572                 :        1636692 :             break;
                                573                 :                : 
                                574                 :         832948 :         default:
                                575                 :                :             {
                                576                 :                :                 /*
                                577                 :                :                  * General case for most expression nodes with children. First
                                578                 :                :                  * recurse, then figure out what to assign to this node.
                                579                 :                :                  */
                                580                 :                :                 Oid         typcollation;
                                581                 :                : 
                                582                 :                :                 /*
                                583                 :                :                  * For most node types, we want to treat all the child
                                584                 :                :                  * expressions alike; but there are a few exceptions, hence
                                585                 :                :                  * this inner switch.
                                586                 :                :                  */
 4006                           587   [ +  +  +  +  :         832948 :                 switch (nodeTag(node))
                                                 + ]
                                588                 :                :                 {
                                589                 :          21721 :                     case T_Aggref:
                                590                 :                :                         {
                                591                 :                :                             /*
                                592                 :                :                              * Aggref is messy enough that we give it its own
                                593                 :                :                              * function, in fact three of them.  The FILTER
                                594                 :                :                              * clause is independent of the rest of the
                                595                 :                :                              * aggregate, however, so it can be processed
                                596                 :                :                              * separately.
                                597                 :                :                              */
                                598                 :          21721 :                             Aggref     *aggref = (Aggref *) node;
                                599                 :                : 
 3765                           600   [ +  +  +  - ]:          21721 :                             switch (aggref->aggkind)
                                601                 :                :                             {
                                602                 :          21568 :                                 case AGGKIND_NORMAL:
                                603                 :          21568 :                                     assign_aggregate_collations(aggref,
                                604                 :                :                                                                 &loccontext);
                                605                 :          21562 :                                     break;
                                606                 :             90 :                                 case AGGKIND_ORDERED_SET:
                                607                 :             90 :                                     assign_ordered_set_collations(aggref,
                                608                 :                :                                                                   &loccontext);
                                609                 :             90 :                                     break;
                                610                 :             63 :                                 case AGGKIND_HYPOTHETICAL:
                                611                 :             63 :                                     assign_hypothetical_collations(aggref,
                                612                 :                :                                                                    &loccontext);
                                613                 :             60 :                                     break;
 3765 tgl@sss.pgh.pa.us         614                 :UBC           0 :                                 default:
                                615         [ #  # ]:              0 :                                     elog(ERROR, "unrecognized aggkind: %d",
                                616                 :                :                                          (int) aggref->aggkind);
                                617                 :                :                             }
                                618                 :                : 
 3925 noah@leadboat.com         619                 :CBC       21712 :                             assign_expr_collations(context->pstate,
 2489 tgl@sss.pgh.pa.us         620                 :          21712 :                                                    (Node *) aggref->aggfilter);
                                621                 :                :                         }
 3925 noah@leadboat.com         622                 :          21712 :                         break;
                                623                 :           1640 :                     case T_WindowFunc:
                                624                 :                :                         {
                                625                 :                :                             /*
                                626                 :                :                              * WindowFunc requires special processing only for
                                627                 :                :                              * its aggfilter clause, as for aggregates.
                                628                 :                :                              */
                                629                 :           1640 :                             WindowFunc *wfunc = (WindowFunc *) node;
                                630                 :                : 
                                631                 :           1640 :                             (void) assign_collations_walker((Node *) wfunc->args,
                                632                 :                :                                                             &loccontext);
                                633                 :                : 
                                634                 :           1640 :                             assign_expr_collations(context->pstate,
                                635                 :           1640 :                                                    (Node *) wfunc->aggfilter);
                                636                 :                :                         }
 4006 tgl@sss.pgh.pa.us         637                 :           1640 :                         break;
                                638                 :          39171 :                     case T_CaseExpr:
                                639                 :                :                         {
                                640                 :                :                             /*
                                641                 :                :                              * CaseExpr is a special case because we do not
                                642                 :                :                              * want to recurse into the test expression (if
                                643                 :                :                              * any).  It was already marked with collations
                                644                 :                :                              * during transformCaseExpr, and furthermore its
                                645                 :                :                              * collation is not relevant to the result of the
                                646                 :                :                              * CASE --- only the output expressions are.
                                647                 :                :                              */
                                648                 :          39171 :                             CaseExpr   *expr = (CaseExpr *) node;
                                649                 :                :                             ListCell   *lc;
                                650                 :                : 
                                651   [ +  -  +  +  :         108361 :                             foreach(lc, expr->args)
                                              +  + ]
                                652                 :                :                             {
 2561                           653                 :          69190 :                                 CaseWhen   *when = lfirst_node(CaseWhen, lc);
                                654                 :                : 
                                655                 :                :                                 /*
                                656                 :                :                                  * The condition expressions mustn't affect
                                657                 :                :                                  * the CASE's result collation either; but
                                658                 :                :                                  * since they are known to yield boolean, it's
                                659                 :                :                                  * safe to recurse directly on them --- they
                                660                 :                :                                  * won't change loccontext.
                                661                 :                :                                  */
 4006                           662                 :          69190 :                                 (void) assign_collations_walker((Node *) when->expr,
                                663                 :                :                                                                 &loccontext);
                                664                 :          69190 :                                 (void) assign_collations_walker((Node *) when->result,
                                665                 :                :                                                                 &loccontext);
                                666                 :                :                             }
                                667                 :          39171 :                             (void) assign_collations_walker((Node *) expr->defresult,
                                668                 :                :                                                             &loccontext);
                                669                 :                :                         }
                                670                 :          39171 :                         break;
 1222                           671                 :           5741 :                     case T_SubscriptingRef:
                                672                 :                :                         {
                                673                 :                :                             /*
                                674                 :                :                              * The subscripts are treated as independent
                                675                 :                :                              * expressions not contributing to the node's
                                676                 :                :                              * collation.  Only the container, and the source
                                677                 :                :                              * expression if any, contribute.  (This models
                                678                 :                :                              * the old behavior, in which the subscripts could
                                679                 :                :                              * be counted on to be integers and thus not
                                680                 :                :                              * contribute anything.)
                                681                 :                :                              */
                                682                 :           5741 :                             SubscriptingRef *sbsref = (SubscriptingRef *) node;
                                683                 :                : 
                                684                 :           5741 :                             assign_expr_collations(context->pstate,
                                685                 :           5741 :                                                    (Node *) sbsref->refupperindexpr);
                                686                 :           5741 :                             assign_expr_collations(context->pstate,
                                687                 :           5741 :                                                    (Node *) sbsref->reflowerindexpr);
                                688                 :           5741 :                             (void) assign_collations_walker((Node *) sbsref->refexpr,
                                689                 :                :                                                             &loccontext);
                                690                 :           5741 :                             (void) assign_collations_walker((Node *) sbsref->refassgnexpr,
                                691                 :                :                                                             &loccontext);
                                692                 :                :                         }
                                693                 :           5741 :                         break;
 4006                           694                 :         764675 :                     default:
                                695                 :                : 
                                696                 :                :                         /*
                                697                 :                :                          * Normal case: all child expressions contribute
                                698                 :                :                          * equally to loccontext.
                                699                 :                :                          */
                                700                 :         764675 :                         (void) expression_tree_walker(node,
                                701                 :                :                                                       assign_collations_walker,
                                702                 :                :                                                       (void *) &loccontext);
                                703                 :         764666 :                         break;
                                704                 :                :                 }
                                705                 :                : 
                                706                 :                :                 /*
                                707                 :                :                  * Now figure out what collation to assign to this node.
                                708                 :                :                  */
 4775                           709                 :         832930 :                 typcollation = get_typcollation(exprType(node));
                                710         [ +  + ]:         832930 :                 if (OidIsValid(typcollation))
                                711                 :                :                 {
                                712                 :                :                     /* Node's result is collatable; what about its input? */
                                713         [ +  + ]:         128272 :                     if (loccontext.strength > COLLATE_NONE)
                                714                 :                :                     {
                                715                 :                :                         /* Collation state bubbles up from children. */
                                716                 :          97232 :                         collation = loccontext.collation;
                                717                 :          97232 :                         strength = loccontext.strength;
                                718                 :          97232 :                         location = loccontext.location;
                                719                 :                :                     }
                                720                 :                :                     else
                                721                 :                :                     {
                                722                 :                :                         /*
                                723                 :                :                          * Collatable output produced without any collatable
                                724                 :                :                          * input.  Use the type's collation (which is usually
                                725                 :                :                          * DEFAULT_COLLATION_OID, but might be different for a
                                726                 :                :                          * domain).
                                727                 :                :                          */
                                728                 :          31040 :                         collation = typcollation;
                                729                 :          31040 :                         strength = COLLATE_IMPLICIT;
                                730                 :          31040 :                         location = exprLocation(node);
                                731                 :                :                     }
                                732                 :                :                 }
                                733                 :                :                 else
                                734                 :                :                 {
                                735                 :                :                     /* Node's result type isn't collatable. */
                                736                 :         704658 :                     collation = InvalidOid;
                                737                 :         704658 :                     strength = COLLATE_NONE;
 2489                           738                 :         704658 :                     location = -1;  /* won't be used */
                                739                 :                :                 }
                                740                 :                : 
                                741                 :                :                 /*
                                742                 :                :                  * Save the result collation into the expression node. If the
                                743                 :                :                  * state is COLLATE_CONFLICT, we'll set the collation to
                                744                 :                :                  * InvalidOid, which might result in an error at runtime.
                                745                 :                :                  */
 4775                           746         [ +  + ]:         832930 :                 if (strength == COLLATE_CONFLICT)
                                747                 :             15 :                     exprSetCollation(node, InvalidOid);
                                748                 :                :                 else
                                749                 :         832915 :                     exprSetCollation(node, collation);
                                750                 :                : 
                                751                 :                :                 /*
                                752                 :                :                  * Likewise save the input collation, which is the one that
                                753                 :                :                  * any function called by this node should use.
                                754                 :                :                  */
                                755         [ +  + ]:         832930 :                 if (loccontext.strength == COLLATE_CONFLICT)
                                756                 :             42 :                     exprSetInputCollation(node, InvalidOid);
                                757                 :                :                 else
                                758                 :         832888 :                     exprSetInputCollation(node, loccontext.collation);
                                759                 :                :             }
                                760                 :         832930 :             break;
                                761                 :                :     }
                                762                 :                : 
                                763                 :                :     /*
                                764                 :                :      * Now, merge my information into my parent's state.
                                765                 :                :      */
 3765                           766                 :        3273700 :     merge_collation_state(collation,
                                767                 :                :                           strength,
                                768                 :                :                           location,
                                769                 :                :                           loccontext.collation2,
                                770                 :                :                           loccontext.location2,
                                771                 :                :                           context);
                                772                 :                : 
                                773                 :        3273688 :     return false;
                                774                 :                : }
                                775                 :                : 
                                776                 :                : /*
                                777                 :                :  * Merge collation state of a subexpression into the context for its parent.
                                778                 :                :  */
                                779                 :                : static void
                                780                 :        3273700 : merge_collation_state(Oid collation,
                                781                 :                :                       CollateStrength strength,
                                782                 :                :                       int location,
                                783                 :                :                       Oid collation2,
                                784                 :                :                       int location2,
                                785                 :                :                       assign_collations_context *context)
                                786                 :                : {
                                787                 :                :     /*
                                788                 :                :      * If the collation strength for this node is different from what's
                                789                 :                :      * already in *context, then this node either dominates or is dominated by
                                790                 :                :      * earlier siblings.
                                791                 :                :      */
 4775                           792         [ +  + ]:        3273700 :     if (strength > context->strength)
                                793                 :                :     {
                                794                 :                :         /* Override previous parent state */
                                795                 :         675956 :         context->collation = collation;
                                796                 :         675956 :         context->strength = strength;
                                797                 :         675956 :         context->location = location;
                                798                 :                :         /* Bubble up error info if applicable */
                                799         [ +  + ]:         675956 :         if (strength == COLLATE_CONFLICT)
                                800                 :                :         {
 3765                           801                 :             57 :             context->collation2 = collation2;
                                802                 :             57 :             context->location2 = location2;
                                803                 :                :         }
                                804                 :                :     }
 4775                           805         [ +  + ]:        2597744 :     else if (strength == context->strength)
                                806                 :                :     {
                                807                 :                :         /* Merge, or detect error if there's a collation conflict */
                                808   [ +  +  -  +  :        2549978 :         switch (strength)
                                                 - ]
                                809                 :                :         {
                                810                 :        2447737 :             case COLLATE_NONE:
                                811                 :                :                 /* Nothing + nothing is still nothing */
                                812                 :        2447737 :                 break;
                                813                 :         102199 :             case COLLATE_IMPLICIT:
                                814         [ +  + ]:         102199 :                 if (collation != context->collation)
                                815                 :                :                 {
                                816                 :                :                     /*
                                817                 :                :                      * Non-default implicit collation always beats default.
                                818                 :                :                      */
                                819         [ +  + ]:          22504 :                     if (context->collation == DEFAULT_COLLATION_OID)
                                820                 :                :                     {
                                821                 :                :                         /* Override previous parent state */
                                822                 :           6126 :                         context->collation = collation;
                                823                 :           6126 :                         context->strength = strength;
                                824                 :           6126 :                         context->location = location;
                                825                 :                :                     }
                                826         [ +  + ]:          16378 :                     else if (collation != DEFAULT_COLLATION_OID)
                                827                 :                :                     {
                                828                 :                :                         /*
                                829                 :                :                          * Oops, we have a conflict.  We cannot throw error
                                830                 :                :                          * here, since the conflict could be resolved by a
                                831                 :                :                          * later sibling CollateExpr, or the parent might not
                                832                 :                :                          * care about collation anyway.  Return enough info to
                                833                 :                :                          * throw the error later, if needed.
                                834                 :                :                          */
                                835                 :             78 :                         context->strength = COLLATE_CONFLICT;
                                836                 :             78 :                         context->collation2 = collation;
                                837                 :             78 :                         context->location2 = location;
                                838                 :                :                     }
                                839                 :                :                 }
                                840                 :         102199 :                 break;
 4775 tgl@sss.pgh.pa.us         841                 :UBC           0 :             case COLLATE_CONFLICT:
                                842                 :                :                 /* We're still conflicted ... */
                                843                 :              0 :                 break;
 4775 tgl@sss.pgh.pa.us         844                 :CBC          42 :             case COLLATE_EXPLICIT:
                                845         [ +  + ]:             42 :                 if (collation != context->collation)
                                846                 :                :                 {
                                847                 :                :                     /*
                                848                 :                :                      * Oops, we have a conflict of explicit COLLATE clauses.
                                849                 :                :                      * Here we choose to throw error immediately; that is what
                                850                 :                :                      * the SQL standard says to do, and there's no good reason
                                851                 :                :                      * to be less strict.
                                852                 :                :                      */
                                853         [ +  - ]:             12 :                     ereport(ERROR,
                                854                 :                :                             (errcode(ERRCODE_COLLATION_MISMATCH),
                                855                 :                :                              errmsg("collation mismatch between explicit collations \"%s\" and \"%s\"",
                                856                 :                :                                     get_collation_name(context->collation),
                                857                 :                :                                     get_collation_name(collation)),
                                858                 :                :                              parser_errposition(context->pstate, location)));
                                859                 :                :                 }
                                860                 :             30 :                 break;
                                861                 :                :         }
                                862                 :                :     }
 3765                           863                 :        3273688 : }
                                864                 :                : 
                                865                 :                : /*
                                866                 :                :  * Aggref is a special case because expressions used only for ordering
                                867                 :                :  * shouldn't be taken to conflict with each other or with regular args,
                                868                 :                :  * indeed shouldn't affect the aggregate's result collation at all.
                                869                 :                :  * We handle this by applying assign_expr_collations() to them rather than
                                870                 :                :  * passing down our loccontext.
                                871                 :                :  *
                                872                 :                :  * Note that we recurse to each TargetEntry, not directly to its contained
                                873                 :                :  * expression, so that the case above for T_TargetEntry will complain if we
                                874                 :                :  * can't resolve a collation for an ORDER BY item (whether or not it is also
                                875                 :                :  * a normal aggregate arg).
                                876                 :                :  *
                                877                 :                :  * We need not recurse into the aggorder or aggdistinct lists, because those
                                878                 :                :  * contain only SortGroupClause nodes which we need not process.
                                879                 :                :  */
                                880                 :                : static void
                                881                 :          21568 : assign_aggregate_collations(Aggref *aggref,
                                882                 :                :                             assign_collations_context *loccontext)
                                883                 :                : {
                                884                 :                :     ListCell   *lc;
                                885                 :                : 
                                886                 :                :     /* Plain aggregates have no direct args */
                                887         [ -  + ]:          21568 :     Assert(aggref->aggdirectargs == NIL);
                                888                 :                : 
                                889                 :                :     /* Process aggregated args, holding resjunk ones at arm's length */
                                890   [ +  +  +  +  :          39883 :     foreach(lc, aggref->args)
                                              +  + ]
                                891                 :                :     {
 2561                           892                 :          18321 :         TargetEntry *tle = lfirst_node(TargetEntry, lc);
                                893                 :                : 
 3765                           894         [ +  + ]:          18321 :         if (tle->resjunk)
                                895                 :            532 :             assign_expr_collations(loccontext->pstate, (Node *) tle);
                                896                 :                :         else
                                897                 :          17789 :             (void) assign_collations_walker((Node *) tle, loccontext);
                                898                 :                :     }
                                899                 :          21562 : }
                                900                 :                : 
                                901                 :                : /*
                                902                 :                :  * For ordered-set aggregates, it's somewhat unclear how best to proceed.
                                903                 :                :  * The spec-defined inverse distribution functions have only one sort column
                                904                 :                :  * and don't return collatable types, but this is clearly too restrictive in
                                905                 :                :  * the general case.  Our solution is to consider that the aggregate's direct
                                906                 :                :  * arguments contribute normally to determination of the aggregate's own
                                907                 :                :  * collation, while aggregated arguments contribute only when the aggregate
                                908                 :                :  * is designed to have exactly one aggregated argument (i.e., it has a single
                                909                 :                :  * aggregated argument and is non-variadic).  If it can have more than one
                                910                 :                :  * aggregated argument, we process the aggregated arguments as independent
                                911                 :                :  * sort columns.  This avoids throwing error for something like
                                912                 :                :  *      agg(...) within group (order by x collate "foo", y collate "bar")
                                913                 :                :  * while also guaranteeing that variadic aggregates don't change in behavior
                                914                 :                :  * depending on how many sort columns a particular call happens to have.
                                915                 :                :  *
                                916                 :                :  * Otherwise this is much like the plain-aggregate case.
                                917                 :                :  */
                                918                 :                : static void
                                919                 :             90 : assign_ordered_set_collations(Aggref *aggref,
                                920                 :                :                               assign_collations_context *loccontext)
                                921                 :                : {
                                922                 :                :     bool        merge_sort_collations;
                                923                 :                :     ListCell   *lc;
                                924                 :                : 
                                925                 :                :     /* Merge sort collations to parent only if there can be only one */
                                926   [ +  -  +  - ]:            180 :     merge_sort_collations = (list_length(aggref->args) == 1 &&
 2489                           927                 :             90 :                              get_func_variadictype(aggref->aggfnoid) == InvalidOid);
                                928                 :                : 
                                929                 :                :     /* Direct args, if any, are normal children of the Aggref node */
 3765                           930                 :             90 :     (void) assign_collations_walker((Node *) aggref->aggdirectargs,
                                931                 :                :                                     loccontext);
                                932                 :                : 
                                933                 :                :     /* Process aggregated args appropriately */
                                934   [ +  -  +  +  :            180 :     foreach(lc, aggref->args)
                                              +  + ]
                                935                 :                :     {
 2561                           936                 :             90 :         TargetEntry *tle = lfirst_node(TargetEntry, lc);
                                937                 :                : 
 3765                           938         [ +  - ]:             90 :         if (merge_sort_collations)
                                939                 :             90 :             (void) assign_collations_walker((Node *) tle, loccontext);
                                940                 :                :         else
 3765 tgl@sss.pgh.pa.us         941                 :UBC           0 :             assign_expr_collations(loccontext->pstate, (Node *) tle);
                                942                 :                :     }
 3765 tgl@sss.pgh.pa.us         943                 :CBC          90 : }
                                944                 :                : 
                                945                 :                : /*
                                946                 :                :  * Hypothetical-set aggregates are even more special: per spec, we need to
                                947                 :                :  * unify the collations of each pair of hypothetical and aggregated args.
                                948                 :                :  * And we need to force the choice of collation down into the sort column
                                949                 :                :  * to ensure that the sort happens with the chosen collation.  Other than
                                950                 :                :  * that, the behavior is like regular ordered-set aggregates.  Note that
                                951                 :                :  * hypothetical direct arguments contribute to the aggregate collation
                                952                 :                :  * only when their partner aggregated arguments do.
                                953                 :                :  */
                                954                 :                : static void
                                955                 :             63 : assign_hypothetical_collations(Aggref *aggref,
                                956                 :                :                                assign_collations_context *loccontext)
                                957                 :                : {
                                958                 :             63 :     ListCell   *h_cell = list_head(aggref->aggdirectargs);
                                959                 :             63 :     ListCell   *s_cell = list_head(aggref->args);
                                960                 :                :     bool        merge_sort_collations;
                                961                 :                :     int         extra_args;
                                962                 :                : 
                                963                 :                :     /* Merge sort collations to parent only if there can be only one */
                                964   [ +  +  -  + ]:            111 :     merge_sort_collations = (list_length(aggref->args) == 1 &&
 2489                           965                 :             48 :                              get_func_variadictype(aggref->aggfnoid) == InvalidOid);
                                966                 :                : 
                                967                 :                :     /* Process any non-hypothetical direct args */
 3765                           968                 :             63 :     extra_args = list_length(aggref->aggdirectargs) - list_length(aggref->args);
                                969         [ -  + ]:             63 :     Assert(extra_args >= 0);
                                970         [ -  + ]:             63 :     while (extra_args-- > 0)
                                971                 :                :     {
 3765 tgl@sss.pgh.pa.us         972                 :UBC           0 :         (void) assign_collations_walker((Node *) lfirst(h_cell), loccontext);
 1735                           973                 :              0 :         h_cell = lnext(aggref->aggdirectargs, h_cell);
                                974                 :                :     }
                                975                 :                : 
                                976                 :                :     /* Scan hypothetical args and aggregated args in parallel */
 3765 tgl@sss.pgh.pa.us         977   [ +  +  +  - ]:CBC         150 :     while (h_cell && s_cell)
                                978                 :                :     {
                                979                 :             90 :         Node       *h_arg = (Node *) lfirst(h_cell);
                                980                 :             90 :         TargetEntry *s_tle = (TargetEntry *) lfirst(s_cell);
                                981                 :                :         assign_collations_context paircontext;
                                982                 :                : 
                                983                 :                :         /*
                                984                 :                :          * Assign collations internally in this pair of expressions, then
                                985                 :                :          * choose a common collation for them.  This should match
                                986                 :                :          * select_common_collation(), but we can't use that function as-is
                                987                 :                :          * because we need access to the whole collation state so we can
                                988                 :                :          * bubble it up to the aggregate function's level.
                                989                 :                :          */
                                990                 :             90 :         paircontext.pstate = loccontext->pstate;
                                991                 :             90 :         paircontext.collation = InvalidOid;
                                992                 :             90 :         paircontext.strength = COLLATE_NONE;
                                993                 :             90 :         paircontext.location = -1;
                                994                 :                :         /* Set these fields just to suppress uninitialized-value warnings: */
                                995                 :             90 :         paircontext.collation2 = InvalidOid;
                                996                 :             90 :         paircontext.location2 = -1;
                                997                 :                : 
                                998                 :             90 :         (void) assign_collations_walker(h_arg, &paircontext);
                                999                 :             90 :         (void) assign_collations_walker((Node *) s_tle->expr, &paircontext);
                               1000                 :                : 
                               1001                 :                :         /* deal with collation conflict */
                               1002         [ -  + ]:             87 :         if (paircontext.strength == COLLATE_CONFLICT)
 3765 tgl@sss.pgh.pa.us        1003         [ #  # ]:UBC           0 :             ereport(ERROR,
                               1004                 :                :                     (errcode(ERRCODE_COLLATION_MISMATCH),
                               1005                 :                :                      errmsg("collation mismatch between implicit collations \"%s\" and \"%s\"",
                               1006                 :                :                             get_collation_name(paircontext.collation),
                               1007                 :                :                             get_collation_name(paircontext.collation2)),
                               1008                 :                :                      errhint("You can choose the collation by applying the COLLATE clause to one or both expressions."),
                               1009                 :                :                      parser_errposition(paircontext.pstate,
                               1010                 :                :                                         paircontext.location2)));
                               1011                 :                : 
                               1012                 :                :         /*
                               1013                 :                :          * At this point paircontext.collation can be InvalidOid only if the
                               1014                 :                :          * type is not collatable; no need to do anything in that case.  If we
                               1015                 :                :          * do have to change the sort column's collation, do it by inserting a
                               1016                 :                :          * RelabelType node into the sort column TLE.
                               1017                 :                :          *
                               1018                 :                :          * XXX This is pretty grotty for a couple of reasons:
                               1019                 :                :          * assign_collations_walker isn't supposed to be changing the
                               1020                 :                :          * expression structure like this, and a parse-time change of
                               1021                 :                :          * collation ought to be signaled by a CollateExpr not a RelabelType
                               1022                 :                :          * (the use of RelabelType for collation marking is supposed to be a
                               1023                 :                :          * planner/executor thing only).  But we have no better alternative.
                               1024                 :                :          * In particular, injecting a CollateExpr could result in the
                               1025                 :                :          * expression being interpreted differently after dump/reload, since
                               1026                 :                :          * we might be effectively promoting an implicit collation to
                               1027                 :                :          * explicit.  This kluge is relying on ruleutils.c not printing a
                               1028                 :                :          * COLLATE clause for a RelabelType, and probably on some other
                               1029                 :                :          * fragile behaviors.
                               1030                 :                :          */
 3765 tgl@sss.pgh.pa.us        1031   [ +  +  -  + ]:CBC         102 :         if (OidIsValid(paircontext.collation) &&
                               1032                 :             15 :             paircontext.collation != exprCollation((Node *) s_tle->expr))
                               1033                 :                :         {
 3765 tgl@sss.pgh.pa.us        1034                 :UBC           0 :             s_tle->expr = (Expr *)
                               1035                 :              0 :                 makeRelabelType(s_tle->expr,
                               1036                 :              0 :                                 exprType((Node *) s_tle->expr),
                               1037                 :              0 :                                 exprTypmod((Node *) s_tle->expr),
                               1038                 :                :                                 paircontext.collation,
                               1039                 :                :                                 COERCE_IMPLICIT_CAST);
                               1040                 :                :         }
                               1041                 :                : 
                               1042                 :                :         /*
                               1043                 :                :          * If appropriate, merge this column's collation state up to the
                               1044                 :                :          * aggregate function.
                               1045                 :                :          */
 3765 tgl@sss.pgh.pa.us        1046         [ -  + ]:CBC          87 :         if (merge_sort_collations)
 3765 tgl@sss.pgh.pa.us        1047                 :UBC           0 :             merge_collation_state(paircontext.collation,
                               1048                 :                :                                   paircontext.strength,
                               1049                 :                :                                   paircontext.location,
                               1050                 :                :                                   paircontext.collation2,
                               1051                 :                :                                   paircontext.location2,
                               1052                 :                :                                   loccontext);
                               1053                 :                : 
 1735 tgl@sss.pgh.pa.us        1054                 :CBC          87 :         h_cell = lnext(aggref->aggdirectargs, h_cell);
                               1055                 :             87 :         s_cell = lnext(aggref->args, s_cell);
                               1056                 :                :     }
 3765                          1057   [ +  -  -  + ]:             60 :     Assert(h_cell == NULL && s_cell == NULL);
 4775                          1058                 :             60 : }
        

Generated by: LCOV version 2.1-beta2-3-g6141622