Header And Logo

PostgreSQL
| The world's most advanced open source database.

parse_collate.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * parse_collate.c
00004  *      Routines for assigning collation information.
00005  *
00006  * We choose to handle collation analysis in a post-pass over the output
00007  * of expression parse analysis.  This is because we need more state to
00008  * perform this processing than is needed in the finished tree.  If we
00009  * did it on-the-fly while building the tree, all that state would have
00010  * to be kept in expression node trees permanently.  This way, the extra
00011  * storage is just local variables in this recursive routine.
00012  *
00013  * The info that is actually saved in the finished tree is:
00014  * 1. The output collation of each expression node, or InvalidOid if it
00015  * returns a noncollatable data type.  This can also be InvalidOid if the
00016  * result type is collatable but the collation is indeterminate.
00017  * 2. The collation to be used in executing each function.  InvalidOid means
00018  * that there are no collatable inputs or their collation is indeterminate.
00019  * This value is only stored in node types that might call collation-using
00020  * functions.
00021  *
00022  * You might think we could get away with storing only one collation per
00023  * node, but the two concepts really need to be kept distinct.  Otherwise
00024  * it's too confusing when a function produces a collatable output type but
00025  * has no collatable inputs or produces noncollatable output from collatable
00026  * inputs.
00027  *
00028  * Cases with indeterminate collation might result in an error being thrown
00029  * at runtime.  If we knew exactly which functions require collation
00030  * information, we could throw those errors at parse time instead.
00031  *
00032  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00033  * Portions Copyright (c) 1994, Regents of the University of California
00034  *
00035  *
00036  * IDENTIFICATION
00037  *    src/backend/parser/parse_collate.c
00038  *
00039  *-------------------------------------------------------------------------
00040  */
00041 #include "postgres.h"
00042 
00043 #include "catalog/pg_collation.h"
00044 #include "nodes/nodeFuncs.h"
00045 #include "parser/parse_collate.h"
00046 #include "utils/lsyscache.h"
00047 
00048 
00049 /*
00050  * Collation strength (the SQL standard calls this "derivation").  Order is
00051  * chosen to allow comparisons to work usefully.  Note: the standard doesn't
00052  * seem to distinguish between NONE and CONFLICT.
00053  */
00054 typedef enum
00055 {
00056     COLLATE_NONE,               /* expression is of a noncollatable datatype */
00057     COLLATE_IMPLICIT,           /* collation was derived implicitly */
00058     COLLATE_CONFLICT,           /* we had a conflict of implicit collations */
00059     COLLATE_EXPLICIT            /* collation was derived explicitly */
00060 } CollateStrength;
00061 
00062 typedef struct
00063 {
00064     ParseState *pstate;         /* parse state (for error reporting) */
00065     Oid         collation;      /* OID of current collation, if any */
00066     CollateStrength strength;   /* strength of current collation choice */
00067     int         location;       /* location of expr that set collation */
00068     /* Remaining fields are only valid when strength == COLLATE_CONFLICT */
00069     Oid         collation2;     /* OID of conflicting collation */
00070     int         location2;      /* location of expr that set collation2 */
00071 } assign_collations_context;
00072 
00073 static bool assign_query_collations_walker(Node *node, ParseState *pstate);
00074 static bool assign_collations_walker(Node *node,
00075                          assign_collations_context *context);
00076 
00077 
00078 /*
00079  * assign_query_collations()
00080  *      Mark all expressions in the given Query with collation information.
00081  *
00082  * This should be applied to each Query after completion of parse analysis
00083  * for expressions.  Note that we do not recurse into sub-Queries, since
00084  * those should have been processed when built.
00085  */
00086 void
00087 assign_query_collations(ParseState *pstate, Query *query)
00088 {
00089     /*
00090      * We just use query_tree_walker() to visit all the contained expressions.
00091      * We can skip the rangetable and CTE subqueries, though, since RTEs and
00092      * subqueries had better have been processed already (else Vars referring
00093      * to them would not get created with the right collation).
00094      */
00095     (void) query_tree_walker(query,
00096                              assign_query_collations_walker,
00097                              (void *) pstate,
00098                              QTW_IGNORE_RANGE_TABLE |
00099                              QTW_IGNORE_CTE_SUBQUERIES);
00100 }
00101 
00102 /*
00103  * Walker for assign_query_collations
00104  *
00105  * Each expression found by query_tree_walker is processed independently.
00106  * Note that query_tree_walker may pass us a whole List, such as the
00107  * targetlist, in which case each subexpression must be processed
00108  * independently --- we don't want to bleat if two different targetentries
00109  * have different collations.
00110  */
00111 static bool
00112 assign_query_collations_walker(Node *node, ParseState *pstate)
00113 {
00114     /* Need do nothing for empty subexpressions */
00115     if (node == NULL)
00116         return false;
00117 
00118     /*
00119      * We don't want to recurse into a set-operations tree; it's already been
00120      * fully processed in transformSetOperationStmt.
00121      */
00122     if (IsA(node, SetOperationStmt))
00123         return false;
00124 
00125     if (IsA(node, List))
00126         assign_list_collations(pstate, (List *) node);
00127     else
00128         assign_expr_collations(pstate, node);
00129 
00130     return false;
00131 }
00132 
00133 /*
00134  * assign_list_collations()
00135  *      Mark all nodes in the list of expressions with collation information.
00136  *
00137  * The list member expressions are processed independently; they do not have
00138  * to share a common collation.
00139  */
00140 void
00141 assign_list_collations(ParseState *pstate, List *exprs)
00142 {
00143     ListCell   *lc;
00144 
00145     foreach(lc, exprs)
00146     {
00147         Node       *node = (Node *) lfirst(lc);
00148 
00149         assign_expr_collations(pstate, node);
00150     }
00151 }
00152 
00153 /*
00154  * assign_expr_collations()
00155  *      Mark all nodes in the given expression tree with collation information.
00156  *
00157  * This is exported for the benefit of various utility commands that process
00158  * expressions without building a complete Query.  It should be applied after
00159  * calling transformExpr() plus any expression-modifying operations such as
00160  * coerce_to_boolean().
00161  */
00162 void
00163 assign_expr_collations(ParseState *pstate, Node *expr)
00164 {
00165     assign_collations_context context;
00166 
00167     /* initialize context for tree walk */
00168     context.pstate = pstate;
00169     context.collation = InvalidOid;
00170     context.strength = COLLATE_NONE;
00171     context.location = -1;
00172 
00173     /* and away we go */
00174     (void) assign_collations_walker(expr, &context);
00175 }
00176 
00177 /*
00178  * select_common_collation()
00179  *      Identify a common collation for a list of expressions.
00180  *
00181  * The expressions should all return the same datatype, else this is not
00182  * terribly meaningful.
00183  *
00184  * none_ok means that it is permitted to return InvalidOid, indicating that
00185  * no common collation could be identified, even for collatable datatypes.
00186  * Otherwise, an error is thrown for conflict of implicit collations.
00187  *
00188  * In theory, none_ok = true reflects the rules of SQL standard clause "Result
00189  * of data type combinations", none_ok = false reflects the rules of clause
00190  * "Collation determination" (in some cases invoked via "Grouping
00191  * operations").
00192  */
00193 Oid
00194 select_common_collation(ParseState *pstate, List *exprs, bool none_ok)
00195 {
00196     assign_collations_context context;
00197 
00198     /* initialize context for tree walk */
00199     context.pstate = pstate;
00200     context.collation = InvalidOid;
00201     context.strength = COLLATE_NONE;
00202     context.location = -1;
00203 
00204     /* and away we go */
00205     (void) assign_collations_walker((Node *) exprs, &context);
00206 
00207     /* deal with collation conflict */
00208     if (context.strength == COLLATE_CONFLICT)
00209     {
00210         if (none_ok)
00211             return InvalidOid;
00212         ereport(ERROR,
00213                 (errcode(ERRCODE_COLLATION_MISMATCH),
00214                  errmsg("collation mismatch between implicit collations \"%s\" and \"%s\"",
00215                         get_collation_name(context.collation),
00216                         get_collation_name(context.collation2)),
00217                  errhint("You can choose the collation by applying the COLLATE clause to one or both expressions."),
00218                  parser_errposition(context.pstate, context.location2)));
00219     }
00220 
00221     /*
00222      * Note: if strength is still COLLATE_NONE, we'll return InvalidOid, but
00223      * that's okay because it must mean none of the expressions returned
00224      * collatable datatypes.
00225      */
00226     return context.collation;
00227 }
00228 
00229 /*
00230  * assign_collations_walker()
00231  *      Recursive guts of collation processing.
00232  *
00233  * Nodes with no children (eg, Vars, Consts, Params) must have been marked
00234  * when built.  All upper-level nodes are marked here.
00235  *
00236  * Note: if this is invoked directly on a List, it will attempt to infer a
00237  * common collation for all the list members.  In particular, it will throw
00238  * error if there are conflicting explicit collations for different members.
00239  */
00240 static bool
00241 assign_collations_walker(Node *node, assign_collations_context *context)
00242 {
00243     assign_collations_context loccontext;
00244     Oid         collation;
00245     CollateStrength strength;
00246     int         location;
00247 
00248     /* Need do nothing for empty subexpressions */
00249     if (node == NULL)
00250         return false;
00251 
00252     /*
00253      * Prepare for recursion.  For most node types, though not all, the first
00254      * thing we do is recurse to process all nodes below this one. Each level
00255      * of the tree has its own local context.
00256      */
00257     loccontext.pstate = context->pstate;
00258     loccontext.collation = InvalidOid;
00259     loccontext.strength = COLLATE_NONE;
00260     loccontext.location = -1;
00261 
00262     /*
00263      * Recurse if appropriate, then determine the collation for this node.
00264      *
00265      * Note: the general cases are at the bottom of the switch, after various
00266      * special cases.
00267      */
00268     switch (nodeTag(node))
00269     {
00270         case T_CollateExpr:
00271             {
00272                 /*
00273                  * COLLATE sets an explicitly derived collation, regardless of
00274                  * what the child state is.  But we must recurse to set up
00275                  * collation info below here.
00276                  */
00277                 CollateExpr *expr = (CollateExpr *) node;
00278 
00279                 (void) expression_tree_walker(node,
00280                                               assign_collations_walker,
00281                                               (void *) &loccontext);
00282 
00283                 collation = expr->collOid;
00284                 Assert(OidIsValid(collation));
00285                 strength = COLLATE_EXPLICIT;
00286                 location = expr->location;
00287             }
00288             break;
00289         case T_FieldSelect:
00290             {
00291                 /*
00292                  * For FieldSelect, the result has the field's declared
00293                  * collation, independently of what happened in the arguments.
00294                  * (The immediate argument must be composite and thus not
00295                  * collatable, anyhow.)  The field's collation was already
00296                  * looked up and saved in the node.
00297                  */
00298                 FieldSelect *expr = (FieldSelect *) node;
00299 
00300                 /* ... but first, recurse */
00301                 (void) expression_tree_walker(node,
00302                                               assign_collations_walker,
00303                                               (void *) &loccontext);
00304 
00305                 if (OidIsValid(expr->resultcollid))
00306                 {
00307                     /* Node's result type is collatable. */
00308                     /* Pass up field's collation as an implicit choice. */
00309                     collation = expr->resultcollid;
00310                     strength = COLLATE_IMPLICIT;
00311                     location = exprLocation(node);
00312                 }
00313                 else
00314                 {
00315                     /* Node's result type isn't collatable. */
00316                     collation = InvalidOid;
00317                     strength = COLLATE_NONE;
00318                     location = -1;      /* won't be used */
00319                 }
00320             }
00321             break;
00322         case T_RowExpr:
00323             {
00324                 /*
00325                  * RowExpr is a special case because the subexpressions are
00326                  * independent: we don't want to complain if some of them have
00327                  * incompatible explicit collations.
00328                  */
00329                 RowExpr    *expr = (RowExpr *) node;
00330 
00331                 assign_list_collations(context->pstate, expr->args);
00332 
00333                 /*
00334                  * Since the result is always composite and therefore never
00335                  * has a collation, we can just stop here: this node has no
00336                  * impact on the collation of its parent.
00337                  */
00338                 return false;   /* done */
00339             }
00340         case T_RowCompareExpr:
00341             {
00342                 /*
00343                  * For RowCompare, we have to find the common collation of
00344                  * each pair of input columns and build a list.  If we can't
00345                  * find a common collation, we just put InvalidOid into the
00346                  * list, which may or may not cause an error at runtime.
00347                  */
00348                 RowCompareExpr *expr = (RowCompareExpr *) node;
00349                 List       *colls = NIL;
00350                 ListCell   *l;
00351                 ListCell   *r;
00352 
00353                 forboth(l, expr->largs, r, expr->rargs)
00354                 {
00355                     Node       *le = (Node *) lfirst(l);
00356                     Node       *re = (Node *) lfirst(r);
00357                     Oid         coll;
00358 
00359                     coll = select_common_collation(context->pstate,
00360                                                    list_make2(le, re),
00361                                                    true);
00362                     colls = lappend_oid(colls, coll);
00363                 }
00364                 expr->inputcollids = colls;
00365 
00366                 /*
00367                  * Since the result is always boolean and therefore never has
00368                  * a collation, we can just stop here: this node has no impact
00369                  * on the collation of its parent.
00370                  */
00371                 return false;   /* done */
00372             }
00373         case T_CoerceToDomain:
00374             {
00375                 /*
00376                  * If the domain declaration included a non-default COLLATE
00377                  * spec, then use that collation as the output collation of
00378                  * the coercion.  Otherwise allow the input collation to
00379                  * bubble up.  (The input should be of the domain's base type,
00380                  * therefore we don't need to worry about it not being
00381                  * collatable when the domain is.)
00382                  */
00383                 CoerceToDomain *expr = (CoerceToDomain *) node;
00384                 Oid         typcollation = get_typcollation(expr->resulttype);
00385 
00386                 /* ... but first, recurse */
00387                 (void) expression_tree_walker(node,
00388                                               assign_collations_walker,
00389                                               (void *) &loccontext);
00390 
00391                 if (OidIsValid(typcollation))
00392                 {
00393                     /* Node's result type is collatable. */
00394                     if (typcollation == DEFAULT_COLLATION_OID)
00395                     {
00396                         /* Collation state bubbles up from child. */
00397                         collation = loccontext.collation;
00398                         strength = loccontext.strength;
00399                         location = loccontext.location;
00400                     }
00401                     else
00402                     {
00403                         /* Use domain's collation as an implicit choice. */
00404                         collation = typcollation;
00405                         strength = COLLATE_IMPLICIT;
00406                         location = exprLocation(node);
00407                     }
00408                 }
00409                 else
00410                 {
00411                     /* Node's result type isn't collatable. */
00412                     collation = InvalidOid;
00413                     strength = COLLATE_NONE;
00414                     location = -1;      /* won't be used */
00415                 }
00416 
00417                 /*
00418                  * Save the state into the expression node.  We know it
00419                  * doesn't care about input collation.
00420                  */
00421                 if (strength == COLLATE_CONFLICT)
00422                     exprSetCollation(node, InvalidOid);
00423                 else
00424                     exprSetCollation(node, collation);
00425             }
00426             break;
00427         case T_TargetEntry:
00428             (void) expression_tree_walker(node,
00429                                           assign_collations_walker,
00430                                           (void *) &loccontext);
00431 
00432             /*
00433              * TargetEntry can have only one child, and should bubble that
00434              * state up to its parent.  We can't use the general-case code
00435              * below because exprType and friends don't work on TargetEntry.
00436              */
00437             collation = loccontext.collation;
00438             strength = loccontext.strength;
00439             location = loccontext.location;
00440 
00441             /*
00442              * Throw error if the collation is indeterminate for a TargetEntry
00443              * that is a sort/group target.  We prefer to do this now, instead
00444              * of leaving the comparison functions to fail at runtime, because
00445              * we can give a syntax error pointer to help locate the problem.
00446              * There are some cases where there might not be a failure, for
00447              * example if the planner chooses to use hash aggregation instead
00448              * of sorting for grouping; but it seems better to predictably
00449              * throw an error.  (Compare transformSetOperationTree, which will
00450              * throw error for indeterminate collation of set-op columns, even
00451              * though the planner might be able to implement the set-op
00452              * without sorting.)
00453              */
00454             if (strength == COLLATE_CONFLICT &&
00455                 ((TargetEntry *) node)->ressortgroupref != 0)
00456                 ereport(ERROR,
00457                         (errcode(ERRCODE_COLLATION_MISMATCH),
00458                          errmsg("collation mismatch between implicit collations \"%s\" and \"%s\"",
00459                                 get_collation_name(loccontext.collation),
00460                                 get_collation_name(loccontext.collation2)),
00461                          errhint("You can choose the collation by applying the COLLATE clause to one or both expressions."),
00462                          parser_errposition(context->pstate,
00463                                             loccontext.location2)));
00464             break;
00465         case T_RangeTblRef:
00466         case T_JoinExpr:
00467         case T_FromExpr:
00468         case T_SortGroupClause:
00469             (void) expression_tree_walker(node,
00470                                           assign_collations_walker,
00471                                           (void *) &loccontext);
00472 
00473             /*
00474              * When we're invoked on a query's jointree, we don't need to do
00475              * anything with join nodes except recurse through them to process
00476              * WHERE/ON expressions.  So just stop here.  Likewise, we don't
00477              * need to do anything when invoked on sort/group lists.
00478              */
00479             return false;
00480         case T_Query:
00481             {
00482                 /*
00483                  * We get here when we're invoked on the Query belonging to a
00484                  * SubLink.  Act as though the Query returns its first output
00485                  * column, which indeed is what it does for EXPR_SUBLINK and
00486                  * ARRAY_SUBLINK cases.  In the cases where the SubLink
00487                  * returns boolean, this info will be ignored.
00488                  *
00489                  * We needn't recurse, since the Query is already processed.
00490                  */
00491                 Query      *qtree = (Query *) node;
00492                 TargetEntry *tent;
00493 
00494                 tent = (TargetEntry *) linitial(qtree->targetList);
00495                 Assert(IsA(tent, TargetEntry));
00496                 Assert(!tent->resjunk);
00497                 collation = exprCollation((Node *) tent->expr);
00498                 /* collation doesn't change if it's converted to array */
00499                 strength = COLLATE_IMPLICIT;
00500                 location = exprLocation((Node *) tent->expr);
00501             }
00502             break;
00503         case T_List:
00504             (void) expression_tree_walker(node,
00505                                           assign_collations_walker,
00506                                           (void *) &loccontext);
00507 
00508             /*
00509              * When processing a list, collation state just bubbles up from
00510              * the list elements.
00511              */
00512             collation = loccontext.collation;
00513             strength = loccontext.strength;
00514             location = loccontext.location;
00515             break;
00516 
00517         case T_Var:
00518         case T_Const:
00519         case T_Param:
00520         case T_CoerceToDomainValue:
00521         case T_CaseTestExpr:
00522         case T_SetToDefault:
00523         case T_CurrentOfExpr:
00524 
00525             /*
00526              * General case for childless expression nodes.  These should
00527              * already have a collation assigned; it is not this function's
00528              * responsibility to look into the catalogs for base-case
00529              * information.
00530              */
00531             collation = exprCollation(node);
00532 
00533             /*
00534              * Note: in most cases, there will be an assigned collation
00535              * whenever type_is_collatable(exprType(node)); but an exception
00536              * occurs for a Var referencing a subquery output column for which
00537              * a unique collation was not determinable.  That may lead to a
00538              * runtime failure if a collation-sensitive function is applied to
00539              * the Var.
00540              */
00541 
00542             if (OidIsValid(collation))
00543                 strength = COLLATE_IMPLICIT;
00544             else
00545                 strength = COLLATE_NONE;
00546             location = exprLocation(node);
00547             break;
00548 
00549         default:
00550             {
00551                 /*
00552                  * General case for most expression nodes with children. First
00553                  * recurse, then figure out what to assign to this node.
00554                  */
00555                 Oid         typcollation;
00556 
00557                 /*
00558                  * For most node types, we want to treat all the child
00559                  * expressions alike; but there are a few exceptions, hence
00560                  * this inner switch.
00561                  */
00562                 switch (nodeTag(node))
00563                 {
00564                     case T_Aggref:
00565                         {
00566                             /*
00567                              * Aggref is a special case because expressions
00568                              * used only for ordering shouldn't be taken to
00569                              * conflict with each other or with regular args.
00570                              * So we apply assign_expr_collations() to them
00571                              * rather than passing down our loccontext.
00572                              *
00573                              * Note that we recurse to each TargetEntry, not
00574                              * directly to its contained expression, so that
00575                              * the case above for T_TargetEntry will apply
00576                              * appropriate checks to agg ORDER BY items.
00577                              *
00578                              * We need not recurse into the aggorder or
00579                              * aggdistinct lists, because those contain only
00580                              * SortGroupClause nodes which we need not
00581                              * process.
00582                              */
00583                             Aggref     *aggref = (Aggref *) node;
00584                             ListCell   *lc;
00585 
00586                             foreach(lc, aggref->args)
00587                             {
00588                                 TargetEntry *tle = (TargetEntry *) lfirst(lc);
00589 
00590                                 Assert(IsA(tle, TargetEntry));
00591                                 if (tle->resjunk)
00592                                     assign_expr_collations(context->pstate,
00593                                                            (Node *) tle);
00594                                 else
00595                                     (void) assign_collations_walker((Node *) tle,
00596                                                                 &loccontext);
00597                             }
00598                         }
00599                         break;
00600                     case T_CaseExpr:
00601                         {
00602                             /*
00603                              * CaseExpr is a special case because we do not
00604                              * want to recurse into the test expression (if
00605                              * any).  It was already marked with collations
00606                              * during transformCaseExpr, and furthermore its
00607                              * collation is not relevant to the result of the
00608                              * CASE --- only the output expressions are.
00609                              */
00610                             CaseExpr   *expr = (CaseExpr *) node;
00611                             ListCell   *lc;
00612 
00613                             foreach(lc, expr->args)
00614                             {
00615                                 CaseWhen   *when = (CaseWhen *) lfirst(lc);
00616 
00617                                 Assert(IsA(when, CaseWhen));
00618 
00619                                 /*
00620                                  * The condition expressions mustn't affect
00621                                  * the CASE's result collation either; but
00622                                  * since they are known to yield boolean, it's
00623                                  * safe to recurse directly on them --- they
00624                                  * won't change loccontext.
00625                                  */
00626                                 (void) assign_collations_walker((Node *) when->expr,
00627                                                                 &loccontext);
00628                                 (void) assign_collations_walker((Node *) when->result,
00629                                                                 &loccontext);
00630                             }
00631                             (void) assign_collations_walker((Node *) expr->defresult,
00632                                                             &loccontext);
00633                         }
00634                         break;
00635                     default:
00636 
00637                         /*
00638                          * Normal case: all child expressions contribute
00639                          * equally to loccontext.
00640                          */
00641                         (void) expression_tree_walker(node,
00642                                                     assign_collations_walker,
00643                                                       (void *) &loccontext);
00644                         break;
00645                 }
00646 
00647                 /*
00648                  * Now figure out what collation to assign to this node.
00649                  */
00650                 typcollation = get_typcollation(exprType(node));
00651                 if (OidIsValid(typcollation))
00652                 {
00653                     /* Node's result is collatable; what about its input? */
00654                     if (loccontext.strength > COLLATE_NONE)
00655                     {
00656                         /* Collation state bubbles up from children. */
00657                         collation = loccontext.collation;
00658                         strength = loccontext.strength;
00659                         location = loccontext.location;
00660                     }
00661                     else
00662                     {
00663                         /*
00664                          * Collatable output produced without any collatable
00665                          * input.  Use the type's collation (which is usually
00666                          * DEFAULT_COLLATION_OID, but might be different for a
00667                          * domain).
00668                          */
00669                         collation = typcollation;
00670                         strength = COLLATE_IMPLICIT;
00671                         location = exprLocation(node);
00672                     }
00673                 }
00674                 else
00675                 {
00676                     /* Node's result type isn't collatable. */
00677                     collation = InvalidOid;
00678                     strength = COLLATE_NONE;
00679                     location = -1;      /* won't be used */
00680                 }
00681 
00682                 /*
00683                  * Save the result collation into the expression node. If the
00684                  * state is COLLATE_CONFLICT, we'll set the collation to
00685                  * InvalidOid, which might result in an error at runtime.
00686                  */
00687                 if (strength == COLLATE_CONFLICT)
00688                     exprSetCollation(node, InvalidOid);
00689                 else
00690                     exprSetCollation(node, collation);
00691 
00692                 /*
00693                  * Likewise save the input collation, which is the one that
00694                  * any function called by this node should use.
00695                  */
00696                 if (loccontext.strength == COLLATE_CONFLICT)
00697                     exprSetInputCollation(node, InvalidOid);
00698                 else
00699                     exprSetInputCollation(node, loccontext.collation);
00700             }
00701             break;
00702     }
00703 
00704     /*
00705      * Now, merge my information into my parent's state.  If the collation
00706      * strength for this node is different from what's already in *context,
00707      * then this node either dominates or is dominated by earlier siblings.
00708      */
00709     if (strength > context->strength)
00710     {
00711         /* Override previous parent state */
00712         context->collation = collation;
00713         context->strength = strength;
00714         context->location = location;
00715         /* Bubble up error info if applicable */
00716         if (strength == COLLATE_CONFLICT)
00717         {
00718             context->collation2 = loccontext.collation2;
00719             context->location2 = loccontext.location2;
00720         }
00721     }
00722     else if (strength == context->strength)
00723     {
00724         /* Merge, or detect error if there's a collation conflict */
00725         switch (strength)
00726         {
00727             case COLLATE_NONE:
00728                 /* Nothing + nothing is still nothing */
00729                 break;
00730             case COLLATE_IMPLICIT:
00731                 if (collation != context->collation)
00732                 {
00733                     /*
00734                      * Non-default implicit collation always beats default.
00735                      */
00736                     if (context->collation == DEFAULT_COLLATION_OID)
00737                     {
00738                         /* Override previous parent state */
00739                         context->collation = collation;
00740                         context->strength = strength;
00741                         context->location = location;
00742                     }
00743                     else if (collation != DEFAULT_COLLATION_OID)
00744                     {
00745                         /*
00746                          * Ooops, we have a conflict.  We cannot throw error
00747                          * here, since the conflict could be resolved by a
00748                          * later sibling CollateExpr, or the parent might not
00749                          * care about collation anyway.  Return enough info to
00750                          * throw the error later, if needed.
00751                          */
00752                         context->strength = COLLATE_CONFLICT;
00753                         context->collation2 = collation;
00754                         context->location2 = location;
00755                     }
00756                 }
00757                 break;
00758             case COLLATE_CONFLICT:
00759                 /* We're still conflicted ... */
00760                 break;
00761             case COLLATE_EXPLICIT:
00762                 if (collation != context->collation)
00763                 {
00764                     /*
00765                      * Ooops, we have a conflict of explicit COLLATE clauses.
00766                      * Here we choose to throw error immediately; that is what
00767                      * the SQL standard says to do, and there's no good reason
00768                      * to be less strict.
00769                      */
00770                     ereport(ERROR,
00771                             (errcode(ERRCODE_COLLATION_MISMATCH),
00772                              errmsg("collation mismatch between explicit collations \"%s\" and \"%s\"",
00773                                     get_collation_name(context->collation),
00774                                     get_collation_name(collation)),
00775                              parser_errposition(context->pstate, location)));
00776                 }
00777                 break;
00778         }
00779     }
00780 
00781     return false;
00782 }