* addTargetToSortList * If the given targetlist entry isn't already in the SortGroupClause * list, add it to the end of the list, using the given sort ordering * info. * * Returns the updated SortGroupClause list. */
| 3572 | * Returns the updated SortGroupClause list. |
| 3573 | */ |
| 3574 | List * |
| 3575 | addTargetToSortList(ParseState *pstate, TargetEntry *tle, |
| 3576 | List *sortlist, List *targetlist, SortBy *sortby) |
| 3577 | { |
| 3578 | Oid restype = exprType((Node *) tle->expr); |
| 3579 | Oid sortop; |
| 3580 | Oid eqop; |
| 3581 | bool hashable; |
| 3582 | bool reverse; |
| 3583 | int location; |
| 3584 | ParseCallbackState pcbstate; |
| 3585 | |
| 3586 | /* if tlist item is an UNKNOWN literal, change it to TEXT */ |
| 3587 | if (restype == UNKNOWNOID) |
| 3588 | { |
| 3589 | tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr, |
| 3590 | restype, TEXTOID, -1, |
| 3591 | COERCION_IMPLICIT, |
| 3592 | COERCE_IMPLICIT_CAST, |
| 3593 | exprLocation((Node *) tle->expr)); |
| 3594 | restype = TEXTOID; |
| 3595 | } |
| 3596 | |
| 3597 | /* |
| 3598 | * Rather than clutter the API of get_sort_group_operators and the other |
| 3599 | * functions we're about to use, make use of error context callback to |
| 3600 | * mark any error reports with a parse position. We point to the operator |
| 3601 | * location if present, else to the expression being sorted. (NB: use the |
| 3602 | * original untransformed expression here; the TLE entry might well point |
| 3603 | * at a duplicate expression in the regular SELECT list.) |
| 3604 | */ |
| 3605 | location = sortby->location; |
| 3606 | if (location < 0) |
| 3607 | location = exprLocation(sortby->node); |
| 3608 | setup_parser_errposition_callback(&pcbstate, pstate, location); |
| 3609 | |
| 3610 | /* determine the sortop, eqop, and directionality */ |
| 3611 | switch (sortby->sortby_dir) |
| 3612 | { |
| 3613 | case SORTBY_DEFAULT: |
| 3614 | case SORTBY_ASC: |
| 3615 | get_sort_group_operators(restype, |
| 3616 | true, true, false, |
| 3617 | &sortop, &eqop, NULL, |
| 3618 | &hashable); |
| 3619 | reverse = false; |
| 3620 | break; |
| 3621 | case SORTBY_DESC: |
| 3622 | get_sort_group_operators(restype, |
| 3623 | false, true, true, |
| 3624 | NULL, &eqop, &sortop, |
| 3625 | &hashable); |
| 3626 | reverse = true; |
| 3627 | break; |
| 3628 | case SORTBY_USING: |
| 3629 | Assert(sortby->useOp != NIL); |
| 3630 | sortop = compatible_oper_opid(sortby->useOp, |
| 3631 | restype, |
no test coverage detected