* Handle an explicit CAST construct. * * Transform the argument, look up the type name, and apply any necessary * coercion function(s). */
| 2742 | * coercion function(s). |
| 2743 | */ |
| 2744 | static Node * |
| 2745 | transformTypeCast(ParseState *pstate, TypeCast *tc) |
| 2746 | { |
| 2747 | Node *result; |
| 2748 | Node *arg = tc->arg; |
| 2749 | Node *expr; |
| 2750 | Oid inputType; |
| 2751 | Oid targetType; |
| 2752 | int32 targetTypmod; |
| 2753 | int location; |
| 2754 | |
| 2755 | /* Look up the type name first */ |
| 2756 | typenameTypeIdAndMod(pstate, tc->typeName, &targetType, &targetTypmod); |
| 2757 | |
| 2758 | /* |
| 2759 | * If the subject of the typecast is an ARRAY[] construct and the target |
| 2760 | * type is an array type, we invoke transformArrayExpr() directly so that |
| 2761 | * we can pass down the type information. This avoids some cases where |
| 2762 | * transformArrayExpr() might not infer the correct type. Otherwise, just |
| 2763 | * transform the argument normally. |
| 2764 | */ |
| 2765 | if (IsA(arg, A_ArrayExpr)) |
| 2766 | { |
| 2767 | Oid targetBaseType; |
| 2768 | int32 targetBaseTypmod; |
| 2769 | Oid elementType; |
| 2770 | |
| 2771 | /* |
| 2772 | * If target is a domain over array, work with the base array type |
| 2773 | * here. Below, we'll cast the array type to the domain. In the |
| 2774 | * usual case that the target is not a domain, the remaining steps |
| 2775 | * will be a no-op. |
| 2776 | */ |
| 2777 | targetBaseTypmod = targetTypmod; |
| 2778 | targetBaseType = getBaseTypeAndTypmod(targetType, &targetBaseTypmod); |
| 2779 | elementType = get_element_type(targetBaseType); |
| 2780 | if (OidIsValid(elementType)) |
| 2781 | { |
| 2782 | expr = transformArrayExpr(pstate, |
| 2783 | (A_ArrayExpr *) arg, |
| 2784 | targetBaseType, |
| 2785 | elementType, |
| 2786 | targetBaseTypmod); |
| 2787 | } |
| 2788 | else |
| 2789 | expr = transformExprRecurse(pstate, arg); |
| 2790 | } |
| 2791 | else |
| 2792 | expr = transformExprRecurse(pstate, arg); |
| 2793 | |
| 2794 | inputType = exprType(expr); |
| 2795 | if (inputType == InvalidOid) |
| 2796 | return expr; /* do nothing if NULL input */ |
| 2797 | |
| 2798 | /* |
| 2799 | * Location of the coercion is preferentially the location of the :: or |
| 2800 | * CAST symbol, but if there is none then use the location of the type |
| 2801 | * name (this can happen in TypeName 'string' syntax, for instance). |
no test coverage detected