|
47 | 47 | #include "utils/array.h"
|
48 | 48 | #include "utils/builtins.h"
|
49 | 49 | #include "utils/datum.h"
|
| 50 | +#include "utils/json.h" |
| 51 | +#include "utils/jsonb.h" |
50 | 52 | #include "utils/jsonpath.h"
|
51 | 53 | #include "utils/lsyscache.h"
|
52 | 54 | #include "utils/typcache.h"
|
@@ -2460,6 +2462,12 @@ ExecInitExprRec(Expr *node, ExprState *state,
|
2460 | 2462 | {
|
2461 | 2463 | ExecInitExprRec(ctor->func, state, resv, resnull);
|
2462 | 2464 | }
|
| 2465 | + else if ((ctor->type == JSCTOR_JSON_PARSE && !ctor->unique) || |
| 2466 | + ctor->type == JSCTOR_JSON_SERIALIZE) |
| 2467 | + { |
| 2468 | + /* Use the value of the first argument as a result */ |
| 2469 | + ExecInitExprRec(linitial(args), state, resv, resnull); |
| 2470 | + } |
2463 | 2471 | else
|
2464 | 2472 | {
|
2465 | 2473 | scratch.opcode = EEOP_JSON_CONSTRUCTOR;
|
@@ -2492,6 +2500,43 @@ ExecInitExprRec(Expr *node, ExprState *state,
|
2492 | 2500 | argno++;
|
2493 | 2501 | }
|
2494 | 2502 |
|
| 2503 | + /* prepare type cache for datum_to_json[b]() */ |
| 2504 | + if (ctor->type == JSCTOR_JSON_SCALAR) |
| 2505 | + { |
| 2506 | + bool is_jsonb = |
| 2507 | + ctor->returning->format->format_type == JS_FORMAT_JSONB; |
| 2508 | + |
| 2509 | + scratch.d.json_constructor.arg_type_cache = |
| 2510 | + palloc(sizeof(*scratch.d.json_constructor.arg_type_cache) * nargs); |
| 2511 | + |
| 2512 | + for (int i = 0; i < nargs; i++) |
| 2513 | + { |
| 2514 | + int category; |
| 2515 | + Oid outfuncid; |
| 2516 | + Oid typid = scratch.d.json_constructor.arg_types[i]; |
| 2517 | + |
| 2518 | + if (is_jsonb) |
| 2519 | + { |
| 2520 | + JsonbTypeCategory jbcat; |
| 2521 | + |
| 2522 | + jsonb_categorize_type(typid, &jbcat, &outfuncid); |
| 2523 | + |
| 2524 | + category = (int) jbcat; |
| 2525 | + } |
| 2526 | + else |
| 2527 | + { |
| 2528 | + JsonTypeCategory jscat; |
| 2529 | + |
| 2530 | + json_categorize_type(typid, &jscat, &outfuncid); |
| 2531 | + |
| 2532 | + category = (int) jscat; |
| 2533 | + } |
| 2534 | + |
| 2535 | + scratch.d.json_constructor.arg_type_cache[i].outfuncid = outfuncid; |
| 2536 | + scratch.d.json_constructor.arg_type_cache[i].category = category; |
| 2537 | + } |
| 2538 | + } |
| 2539 | + |
2495 | 2540 | ExprEvalPushStep(state, &scratch);
|
2496 | 2541 | }
|
2497 | 2542 |
|
|
0 commit comments