Skip to content

Commit 7a9b2c1

Browse files
author
Nikita Glukhov
committed
Simplify and move to jsonb_utils.c clone_parse_state()
1 parent 4a58b94 commit 7a9b2c1

File tree

3 files changed

+29
-37
lines changed

3 files changed

+29
-37
lines changed

src/backend/utils/adt/jsonb.c

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ static void datum_to_jsonb(Datum val, bool is_null, JsonbInState *result,
8686
bool key_scalar);
8787
static void add_jsonb(Datum val, bool is_null, JsonbInState *result,
8888
Oid val_type, bool key_scalar);
89-
static JsonbParseState *clone_parse_state(JsonbParseState *state);
9089
static char *JsonbToCStringWorker(StringInfo out, JsonbContainer *in, int estimated_len, bool indent);
9190
static void add_indent(StringInfo out, bool indent, int level);
9291

@@ -1465,40 +1464,6 @@ jsonb_object_two_arg(PG_FUNCTION_ARGS)
14651464
}
14661465

14671466

1468-
/*
1469-
* shallow clone of a parse state, suitable for use in aggregate
1470-
* final functions that will only append to the values rather than
1471-
* change them.
1472-
*/
1473-
static JsonbParseState *
1474-
clone_parse_state(JsonbParseState *state)
1475-
{
1476-
JsonbParseState *result,
1477-
*icursor,
1478-
*ocursor;
1479-
1480-
if (state == NULL)
1481-
return NULL;
1482-
1483-
result = palloc(sizeof(JsonbParseState));
1484-
icursor = state;
1485-
ocursor = result;
1486-
for (;;)
1487-
{
1488-
ocursor->contVal = icursor->contVal;
1489-
ocursor->size = icursor->size;
1490-
icursor = icursor->next;
1491-
if (icursor == NULL)
1492-
break;
1493-
ocursor->next = palloc(sizeof(JsonbParseState));
1494-
ocursor = ocursor->next;
1495-
}
1496-
ocursor->next = NULL;
1497-
1498-
return result;
1499-
}
1500-
1501-
15021467
/*
15031468
* jsonb_agg aggregate function
15041469
*/
@@ -1642,7 +1607,7 @@ jsonb_agg_finalfn(PG_FUNCTION_ARGS)
16421607
* values, just add the final array end marker.
16431608
*/
16441609

1645-
result.parseState = clone_parse_state(arg->res->parseState);
1610+
result.parseState = JsonbParseStateClone(arg->res->parseState);
16461611

16471612
result.res = pushJsonbValue(&result.parseState,
16481613
WJB_END_ARRAY, NULL);
@@ -1874,7 +1839,7 @@ jsonb_object_agg_finalfn(PG_FUNCTION_ARGS)
18741839
* marker.
18751840
*/
18761841

1877-
result.parseState = clone_parse_state(arg->res->parseState);
1842+
result.parseState = JsonbParseStateClone(arg->res->parseState);
18781843

18791844
result.res = pushJsonbValue(&result.parseState,
18801845
WJB_END_OBJECT, NULL);

src/backend/utils/adt/jsonb_util.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,32 @@ fillJsonbValue(JsonbContainer *container, int index,
538538
}
539539
}
540540

541+
/*
542+
* shallow clone of a parse state, suitable for use in aggregate
543+
* final functions that will only append to the values rather than
544+
* change them.
545+
*/
546+
JsonbParseState *
547+
JsonbParseStateClone(JsonbParseState *state)
548+
{
549+
JsonbParseState *result,
550+
*icursor,
551+
*ocursor,
552+
**pocursor = &result;
553+
554+
for (icursor = state; icursor; icursor = icursor->next)
555+
{
556+
*pocursor = ocursor = palloc(sizeof(JsonbParseState));
557+
ocursor->contVal = icursor->contVal;
558+
ocursor->size = icursor->size;
559+
pocursor = &ocursor->next;
560+
}
561+
562+
*pocursor = NULL;
563+
564+
return result;
565+
}
566+
541567
/*
542568
* Push JsonbValue into JsonbParseState.
543569
*

src/include/utils/jsonb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ extern JsonbValue *getIthJsonbValueFromContainer(JsonbContainer *sheader,
387387
uint32 i);
388388
extern JsonbValue *pushJsonbValue(JsonbParseState **pstate,
389389
JsonbIteratorToken seq, JsonbValue *jbval);
390+
extern JsonbParseState *JsonbParseStateClone(JsonbParseState *state);
390391
extern JsonbIterator *JsonbIteratorInit(JsonbContainer *container);
391392
extern JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val,
392393
bool skipNested);

0 commit comments

Comments
 (0)