Skip to content

Commit 43399cf

Browse files
author
Nikita Glukhov
committed
Simplify and move to jsonb_utils.c clone_parse_state()
1 parent 1edd785 commit 43399cf

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
@@ -85,7 +85,6 @@ static void datum_to_jsonb(Datum val, bool is_null, JsonbInState *result,
8585
bool key_scalar);
8686
static void add_jsonb(Datum val, bool is_null, JsonbInState *result,
8787
Oid val_type, bool key_scalar);
88-
static JsonbParseState *clone_parse_state(JsonbParseState *state);
8988
static char *JsonbToCStringWorker(StringInfo out, JsonbContainer *in, int estimated_len, bool indent);
9089
static void add_indent(StringInfo out, bool indent, int level);
9190

@@ -1519,40 +1518,6 @@ jsonb_object_two_arg(PG_FUNCTION_ARGS)
15191518
}
15201519

15211520

1522-
/*
1523-
* shallow clone of a parse state, suitable for use in aggregate
1524-
* final functions that will only append to the values rather than
1525-
* change them.
1526-
*/
1527-
static JsonbParseState *
1528-
clone_parse_state(JsonbParseState *state)
1529-
{
1530-
JsonbParseState *result,
1531-
*icursor,
1532-
*ocursor;
1533-
1534-
if (state == NULL)
1535-
return NULL;
1536-
1537-
result = palloc(sizeof(JsonbParseState));
1538-
icursor = state;
1539-
ocursor = result;
1540-
for (;;)
1541-
{
1542-
ocursor->contVal = icursor->contVal;
1543-
ocursor->size = icursor->size;
1544-
icursor = icursor->next;
1545-
if (icursor == NULL)
1546-
break;
1547-
ocursor->next = palloc(sizeof(JsonbParseState));
1548-
ocursor = ocursor->next;
1549-
}
1550-
ocursor->next = NULL;
1551-
1552-
return result;
1553-
}
1554-
1555-
15561521
/*
15571522
* jsonb_agg aggregate function
15581523
*/
@@ -1696,7 +1661,7 @@ jsonb_agg_finalfn(PG_FUNCTION_ARGS)
16961661
* values, just add the final array end marker.
16971662
*/
16981663

1699-
result.parseState = clone_parse_state(arg->res->parseState);
1664+
result.parseState = JsonbParseStateClone(arg->res->parseState);
17001665

17011666
result.res = pushJsonbValue(&result.parseState,
17021667
WJB_END_ARRAY, NULL);
@@ -1928,7 +1893,7 @@ jsonb_object_agg_finalfn(PG_FUNCTION_ARGS)
19281893
* marker.
19291894
*/
19301895

1931-
result.parseState = clone_parse_state(arg->res->parseState);
1896+
result.parseState = JsonbParseStateClone(arg->res->parseState);
19321897

19331898
result.res = pushJsonbValue(&result.parseState,
19341899
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
@@ -496,6 +496,32 @@ fillJsonbValue(JsonbContainer *container, int index,
496496
}
497497
}
498498

499+
/*
500+
* shallow clone of a parse state, suitable for use in aggregate
501+
* final functions that will only append to the values rather than
502+
* change them.
503+
*/
504+
JsonbParseState *
505+
JsonbParseStateClone(JsonbParseState *state)
506+
{
507+
JsonbParseState *result,
508+
*icursor,
509+
*ocursor,
510+
**pocursor = &result;
511+
512+
for (icursor = state; icursor; icursor = icursor->next)
513+
{
514+
*pocursor = ocursor = palloc(sizeof(JsonbParseState));
515+
ocursor->contVal = icursor->contVal;
516+
ocursor->size = icursor->size;
517+
pocursor = &ocursor->next;
518+
}
519+
520+
*pocursor = NULL;
521+
522+
return result;
523+
}
524+
499525
/*
500526
* Push JsonbValue into JsonbParseState.
501527
*

src/include/utils/jsonb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ extern JsonbValue *getIthJsonbValueFromContainer(JsonbContainer *sheader,
361361
uint32 i);
362362
extern JsonbValue *pushJsonbValue(JsonbParseState **pstate,
363363
JsonbIteratorToken seq, JsonbValue *jbVal);
364+
extern JsonbParseState *JsonbParseStateClone(JsonbParseState *state);
364365
extern JsonbIterator *JsonbIteratorInit(JsonbContainer *container);
365366
extern JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val,
366367
bool skipNested);

0 commit comments

Comments
 (0)