MCPcopy Create free account
hub / github.com/apache/cloudberry / jsonb_subscript_transform

Function jsonb_subscript_transform

src/backend/utils/adt/jsonbsubs.c:45–165  ·  view source on GitHub ↗

* 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. */

Source from the content-addressed store, hash-verified

43 * and determine the result type of the SubscriptingRef node.
44 */
45static void
46jsonb_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),

Callers

nothing calls this directly

Calls 12

parser_errpositionFunction · 0.85
exprLocationFunction · 0.85
transformExprFunction · 0.85
exprTypeFunction · 0.85
can_coerce_typeFunction · 0.85
format_type_beFunction · 0.85
coerce_typeFunction · 0.85
lappendFunction · 0.85
foreachFunction · 0.50
errcodeFunction · 0.50
errmsgFunction · 0.50
errhintFunction · 0.50

Tested by

no test coverage detected