Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 82d5037

Browse files
author
Nikita Glukhov
committed
Add jsonpath wrapItem()
1 parent c527c29 commit 82d5037

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/backend/utils/adt/jsonpath_exec.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ static JsonItem *getScalar(JsonItem *scalar, enum jbvType type);
419419
static JsonItem *getNumber(JsonItem *scalar);
420420
static bool convertJsonDoubleToNumeric(JsonItem *dbl, JsonItem *num);
421421
static JsonbValue *wrapItemsInArray(const JsonValueList *items, bool isJsonb);
422+
static JsonItem *wrapItem(JsonItem *jbv, bool isJsonb);
422423
static text *JsonItemUnquoteText(JsonItem *jsi, bool isJsonb);
423424
static JsonItem *wrapJsonObjectOrArray(JsonItem *js, JsonItem *buf,
424425
bool isJsonb);
@@ -3646,6 +3647,45 @@ convertJsonDoubleToNumeric(JsonItem *dbl, JsonItem *num)
36463647
return true;
36473648
}
36483649

3650+
/*
3651+
* Wrap a non-array SQL/JSON item into an array for applying array subscription
3652+
* path steps in lax mode.
3653+
*/
3654+
static JsonItem *
3655+
wrapItem(JsonItem *jsi, bool isJsonb)
3656+
{
3657+
JsonbParseState *ps = NULL;
3658+
JsonItem jsibuf;
3659+
JsonbValue jbvbuf;
3660+
3661+
switch (JsonbType(jsi))
3662+
{
3663+
case jbvArray:
3664+
/* Simply return an array item. */
3665+
return jsi;
3666+
3667+
case jbvObject:
3668+
/*
3669+
* Need to wrap object into a binary JsonbValue for its unpacking
3670+
* in pushJsonbValue().
3671+
*/
3672+
if (!JsonItemIsBinary(jsi))
3673+
jsi = JsonxWrapInBinary(jsi, &jsibuf, isJsonb);
3674+
break;
3675+
3676+
default:
3677+
/* Ordinary scalars can be pushed directly. */
3678+
break;
3679+
}
3680+
3681+
pushJsonbValue(&ps, WJB_BEGIN_ARRAY, NULL);
3682+
(isJsonb ? pushJsonbValue : pushJsonValue)(&ps, WJB_ELEM,
3683+
JsonItemToJsonbValue(jsi, &jbvbuf));
3684+
jsi = JsonbValueToJsonItem(pushJsonbValue(&ps, WJB_END_ARRAY, NULL), &jsibuf);
3685+
3686+
return JsonxWrapInBinary(jsi, NULL, isJsonb);
3687+
}
3688+
36493689
/* Construct a JSON array from the item list */
36503690
static JsonbValue *
36513691
wrapItemsInArray(const JsonValueList *items, bool isJsonb)

0 commit comments

Comments
 (0)