* Finish parse analysis of a SubscriptingRef expression for a jsonb. * * Transform the subscript expressions, coerce them to text, * and determine the result type of the SubscriptingRef node. */
| 43 | * and determine the result type of the SubscriptingRef node. |
| 44 | */ |
| 45 | static void |
| 46 | jsonb_subscript_transform(SubscriptingRef *sbsref, |
| 47 | List *indirection, |
| 48 | ParseState *pstate, |
| 49 | bool isSlice, |
| 50 | bool isAssignment) |
| 51 | { |
| 52 | List *upperIndexpr = NIL; |
| 53 | ListCell *idx; |
| 54 | |
| 55 | /* |
| 56 | * Transform and convert the subscript expressions. Jsonb subscripting |
| 57 | * does not support slices, look only and the upper index. |
| 58 | */ |
| 59 | foreach(idx, indirection) |
| 60 | { |
| 61 | A_Indices *ai = lfirst_node(A_Indices, idx); |
| 62 | Node *subExpr; |
| 63 | |
| 64 | if (isSlice) |
| 65 | { |
| 66 | Node *expr = ai->uidx ? ai->uidx : ai->lidx; |
| 67 | |
| 68 | ereport(ERROR, |
| 69 | (errcode(ERRCODE_DATATYPE_MISMATCH), |
| 70 | errmsg("jsonb subscript does not support slices"), |
| 71 | parser_errposition(pstate, exprLocation(expr)))); |
| 72 | } |
| 73 | |
| 74 | if (ai->uidx) |
| 75 | { |
| 76 | Oid subExprType = InvalidOid, |
| 77 | targetType = UNKNOWNOID; |
| 78 | |
| 79 | subExpr = transformExpr(pstate, ai->uidx, pstate->p_expr_kind); |
| 80 | subExprType = exprType(subExpr); |
| 81 | |
| 82 | if (subExprType != UNKNOWNOID) |
| 83 | { |
| 84 | Oid targets[2] = {INT4OID, TEXTOID}; |
| 85 | |
| 86 | /* |
| 87 | * Jsonb can handle multiple subscript types, but cases when a |
| 88 | * subscript could be coerced to multiple target types must be |
| 89 | * avoided, similar to overloaded functions. It could be |
| 90 | * possibly extend with jsonpath in the future. |
| 91 | */ |
| 92 | for (int i = 0; i < 2; i++) |
| 93 | { |
| 94 | if (can_coerce_type(1, &subExprType, &targets[i], COERCION_IMPLICIT)) |
| 95 | { |
| 96 | /* |
| 97 | * One type has already succeeded, it means there are |
| 98 | * two coercion targets possible, failure. |
| 99 | */ |
| 100 | if (targetType != UNKNOWNOID) |
| 101 | ereport(ERROR, |
| 102 | (errcode(ERRCODE_DATATYPE_MISMATCH), |
nothing calls this directly
no test coverage detected