fix output of json_agg(), remove unused json_agg_* prototypes etc.
authorTomas Vondra <[email protected]>
Fri, 20 Jan 2017 23:55:23 +0000 (00:55 +0100)
committerTomas Vondra <[email protected]>
Fri, 20 Jan 2017 23:55:23 +0000 (00:55 +0100)
json_agg_transfn() was relying on json_agg_collectfn() to generate
the initial '[' for the output, but that only worked with the old
XL-specific implementation. With the new implementation adopted from
upstream, this lead to output like this:

    {"x":1},{"x":2}]

Fixed by removing the ifdef in json_agg_transfn(). While doing that,
I've noticed the json_agg_collectfn() is still in json.c, although
it's not needed anymore, and similarly for json_agg_in/out prototypes.

src/backend/utils/adt/json.c
src/include/utils/json.h

index 32e41f5e8dcd68a18faa778be734e2d232440b3a..f7044186035a3352652c57d189ec719ebbf48f6a 100644 (file)
@@ -1902,13 +1902,7 @@ json_agg_transfn(PG_FUNCTION_ARGS)
                state->str = makeStringInfo();
                MemoryContextSwitchTo(oldcontext);
 
-#ifndef XCP
-               /* 
-                * Do not add the start marker. It will be done by the
-                * json_agg_collectfn on receiving the first result
-                */
                appendStringInfoChar(state->str, '[');
-#endif
                json_categorize_type(arg_type, &state->val_category,
                                                         &state->val_output_func);
        }
@@ -1947,66 +1941,6 @@ json_agg_transfn(PG_FUNCTION_ARGS)
        PG_RETURN_POINTER(state);
 }
 
-#ifdef XCP
-/*
- * json_agg collection function
- */
-Datum
-json_agg_collectfn(PG_FUNCTION_ARGS)
-{
-       MemoryContext aggcontext,
-                               oldcontext;
-       JsonAggState *collectstate;
-       JsonAggState *transstate;
-
-       if (!AggCheckCallContext(fcinfo, &aggcontext))
-       {
-               /* cannot be called directly because of internal-type argument */
-               elog(ERROR, "json_agg_collectfn called in non-aggregate context");
-       }
-
-       if (PG_ARGISNULL(0))
-       {
-               /*
-                * Make this state object in a context where it will persist for the
-                * duration of the aggregate call.  MemoryContextSwitchTo is only
-                * needed the first time, as the StringInfo routines make sure they
-                * use the right context to enlarge the object if necessary.
-                */
-               oldcontext = MemoryContextSwitchTo(aggcontext);
-               collectstate = (JsonAggState *) palloc(sizeof(JsonAggState));
-               collectstate->str = makeStringInfo();
-               MemoryContextSwitchTo(oldcontext);
-
-               appendStringInfoChar(collectstate->str, '[');
-       }
-       else
-       {
-               collectstate = (JsonAggState *) PG_GETARG_POINTER(0);
-               if (!PG_ARGISNULL(1))
-                       appendStringInfoString(collectstate->str, ", ");
-       }
-
-       /* fast path for NULLs */
-       if (PG_ARGISNULL(1))
-               PG_RETURN_POINTER(collectstate);
-
-       transstate = (JsonAggState *) PG_GETARG_POINTER(1);
-
-       /* add some whitespace if structured type and not first item */
-       if (!PG_ARGISNULL(0) &&
-               (transstate->val_category == JSONTYPE_ARRAY ||
-                transstate->val_category == JSONTYPE_COMPOSITE))
-       {
-               appendStringInfoString(collectstate->str, "\n ");
-       }
-
-       appendStringInfoString(collectstate->str, transstate->str->data);
-
-       PG_RETURN_POINTER(collectstate);
-}
-#endif
-
 /*
  * json_agg final function
  */
@@ -2014,11 +1948,9 @@ Datum
 json_agg_finalfn(PG_FUNCTION_ARGS)
 {
        JsonAggState *state;
-       MemoryContext aggcontext;
 
        /* cannot be called directly because of internal-type argument */
-       if (!AggCheckCallContext(fcinfo, &aggcontext))
-               elog(ERROR, "aggregate function called in non-aggregate context");
+       Assert(AggCheckCallContext(fcinfo, NULL));
 
        state = PG_ARGISNULL(0) ?
                NULL :
@@ -2132,11 +2064,9 @@ Datum
 json_object_agg_finalfn(PG_FUNCTION_ARGS)
 {
        JsonAggState *state;
-       MemoryContext aggcontext;
 
        /* cannot be called directly because of internal-type argument */
-       if (!AggCheckCallContext(fcinfo, &aggcontext))
-               elog(ERROR, "aggregate function called in non-aggregate context");
+       Assert(AggCheckCallContext(fcinfo, NULL));
 
        state = PG_ARGISNULL(0) ? NULL : (JsonAggState *) PG_GETARG_POINTER(0);
 
index 5720d419234d173973037be4cfdfa747b4e7abe6..9dd9dff2f637e018be708187bbcd22513ad176a4 100644 (file)
@@ -22,10 +22,6 @@ extern Datum json_in(PG_FUNCTION_ARGS);
 extern Datum json_out(PG_FUNCTION_ARGS);
 extern Datum json_recv(PG_FUNCTION_ARGS);
 extern Datum json_send(PG_FUNCTION_ARGS);
-#ifdef XCP
-extern Datum json_agg_state_in(PG_FUNCTION_ARGS);
-extern Datum json_agg_state_out(PG_FUNCTION_ARGS);
-#endif
 extern Datum array_to_json(PG_FUNCTION_ARGS);
 extern Datum array_to_json_pretty(PG_FUNCTION_ARGS);
 extern Datum row_to_json(PG_FUNCTION_ARGS);
@@ -33,9 +29,6 @@ extern Datum row_to_json_pretty(PG_FUNCTION_ARGS);
 extern Datum to_json(PG_FUNCTION_ARGS);
 
 extern Datum json_agg_transfn(PG_FUNCTION_ARGS);
-#ifdef XCP
-extern Datum json_agg_collectfn(PG_FUNCTION_ARGS);
-#endif
 extern Datum json_agg_finalfn(PG_FUNCTION_ARGS);
 
 extern Datum json_object_agg_finalfn(PG_FUNCTION_ARGS);