Skip to content

Commit 57d6dba

Browse files
author
Nikita Glukhov
committed
Simplify and move to jsonb_utils.c clone_parse_state()
1 parent c7734c7 commit 57d6dba

File tree

3 files changed

+34
-39
lines changed

3 files changed

+34
-39
lines changed

src/backend/utils/adt/jsonb.c

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ static void datum_to_jsonb(Datum val, bool is_null, JsonbInState *result,
6565
bool key_scalar);
6666
static void add_jsonb(Datum val, bool is_null, JsonbInState *result,
6767
Oid val_type, bool key_scalar);
68-
static JsonbParseState *clone_parse_state(JsonbParseState *state);
6968
static char *JsonbToCStringWorker(StringInfo out, JsonbContainer *in, int estimated_len, bool indent);
7069
static void add_indent(StringInfo out, bool indent, int level);
7170

@@ -1519,44 +1518,11 @@ jsonb_object_two_arg(PG_FUNCTION_ARGS)
15191518
PG_RETURN_POINTER(JsonbValueToJsonb(result.res));
15201519
}
15211520

1522-
15231521
/*
1524-
* shallow clone of a parse state, suitable for use in aggregate
1525-
* final functions that will only append to the values rather than
1526-
* change them.
1522+
* jsonb_agg aggregate function
15271523
*/
1528-
static JsonbParseState *
1529-
clone_parse_state(JsonbParseState *state)
1530-
{
1531-
JsonbParseState *result,
1532-
*icursor,
1533-
*ocursor;
1534-
1535-
if (state == NULL)
1536-
return NULL;
1537-
1538-
result = palloc(sizeof(JsonbParseState));
1539-
icursor = state;
1540-
ocursor = result;
1541-
for (;;)
1542-
{
1543-
ocursor->contVal = icursor->contVal;
1544-
ocursor->size = icursor->size;
1545-
ocursor->unique_keys = icursor->unique_keys;
1546-
ocursor->skip_nulls = icursor->skip_nulls;
1547-
icursor = icursor->next;
1548-
if (icursor == NULL)
1549-
break;
1550-
ocursor->next = palloc(sizeof(JsonbParseState));
1551-
ocursor = ocursor->next;
1552-
}
1553-
ocursor->next = NULL;
1554-
1555-
return result;
1556-
}
1557-
1558-
static Datum
1559-
jsonb_agg_transfn_worker(FunctionCallInfo fcinfo, bool absent_on_null)
1524+
Datum
1525+
jsonb_agg_transfn(PG_FUNCTION_ARGS)
15601526
{
15611527
MemoryContext oldcontext,
15621528
aggcontext;
@@ -1716,7 +1682,7 @@ jsonb_agg_finalfn(PG_FUNCTION_ARGS)
17161682
* values, just add the final array end marker.
17171683
*/
17181684

1719-
result.parseState = clone_parse_state(arg->res->parseState);
1685+
result.parseState = JsonbParseStateClone(arg->res->parseState);
17201686

17211687
result.res = pushJsonbValue(&result.parseState,
17221688
WJB_END_ARRAY, NULL);
@@ -2006,7 +1972,7 @@ jsonb_object_agg_finalfn(PG_FUNCTION_ARGS)
20061972
* marker.
20071973
*/
20081974

2009-
result.parseState = clone_parse_state(arg->res->parseState);
1975+
result.parseState = JsonbParseStateClone(arg->res->parseState);
20101976

20111977
result.res = pushJsonbValue(&result.parseState,
20121978
WJB_END_OBJECT, NULL);

src/backend/utils/adt/jsonb_util.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,34 @@ fillJsonbValue(JsonbContainer *container, int index,
547547
}
548548
}
549549

550+
/*
551+
* shallow clone of a parse state, suitable for use in aggregate
552+
* final functions that will only append to the values rather than
553+
* change them.
554+
*/
555+
JsonbParseState *
556+
JsonbParseStateClone(JsonbParseState *state)
557+
{
558+
JsonbParseState *result,
559+
*icursor,
560+
*ocursor,
561+
**pocursor = &result;
562+
563+
for (icursor = state; icursor; icursor = icursor->next)
564+
{
565+
*pocursor = ocursor = palloc(sizeof(JsonbParseState));
566+
ocursor->contVal = icursor->contVal;
567+
ocursor->size = icursor->size;
568+
ocursor->unique_keys = icursor->unique_keys;
569+
ocursor->skip_nulls = icursor->skip_nulls;
570+
pocursor = &ocursor->next;
571+
}
572+
573+
*pocursor = NULL;
574+
575+
return result;
576+
}
577+
550578
/*
551579
* Push JsonbValue into JsonbParseState.
552580
*

src/include/utils/jsonb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ extern JsonbValue *getIthJsonbValueFromContainer(JsonbContainer *sheader,
405405
uint32 i);
406406
extern JsonbValue *pushJsonbValue(JsonbParseState **pstate,
407407
JsonbIteratorToken seq, JsonbValue *jbval);
408+
extern JsonbParseState *JsonbParseStateClone(JsonbParseState *state);
408409
extern JsonbIterator *JsonbIteratorInit(JsonbContainer *container);
409410
extern JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val,
410411
bool skipNested);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy