/src/postgres/src/backend/parser/parse_jsontable.c
Line | Count | Source |
1 | | /*------------------------------------------------------------------------- |
2 | | * |
3 | | * parse_jsontable.c |
4 | | * parsing of JSON_TABLE |
5 | | * |
6 | | * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group |
7 | | * Portions Copyright (c) 1994, Regents of the University of California |
8 | | * |
9 | | * |
10 | | * IDENTIFICATION |
11 | | * src/backend/parser/parse_jsontable.c |
12 | | * |
13 | | *------------------------------------------------------------------------- |
14 | | */ |
15 | | |
16 | | #include "postgres.h" |
17 | | |
18 | | #include "catalog/pg_type.h" |
19 | | #include "nodes/makefuncs.h" |
20 | | #include "nodes/nodeFuncs.h" |
21 | | #include "optimizer/optimizer.h" |
22 | | #include "parser/parse_clause.h" |
23 | | #include "parser/parse_collate.h" |
24 | | #include "parser/parse_expr.h" |
25 | | #include "parser/parse_relation.h" |
26 | | #include "parser/parse_type.h" |
27 | | #include "utils/fmgrprotos.h" |
28 | | #include "utils/json.h" |
29 | | #include "utils/lsyscache.h" |
30 | | |
31 | | /* Context for transformJsonTableColumns() */ |
32 | | typedef struct JsonTableParseContext |
33 | | { |
34 | | ParseState *pstate; |
35 | | JsonTable *jt; |
36 | | TableFunc *tf; |
37 | | List *pathNames; /* list of all path and columns names */ |
38 | | int pathNameId; /* path name id counter */ |
39 | | } JsonTableParseContext; |
40 | | |
41 | | static JsonTablePlan *transformJsonTableColumns(JsonTableParseContext *cxt, |
42 | | JsonTablePlanSpec *planspec, |
43 | | List *columns, |
44 | | List *passingArgs, |
45 | | JsonTablePathSpec *pathspec); |
46 | | static JsonTablePlan *transformJsonTableNestedColumns(JsonTableParseContext *cxt, |
47 | | JsonTablePlanSpec *plan, |
48 | | List *passingArgs, |
49 | | List *columns); |
50 | | static JsonFuncExpr *transformJsonTableColumn(JsonTableColumn *jtc, |
51 | | Node *contextItemExpr, |
52 | | List *passingArgs); |
53 | | static bool isCompositeType(Oid typid); |
54 | | static JsonTablePlan *makeJsonTablePathScan(JsonTableParseContext *cxt, |
55 | | JsonTablePathSpec *pathspec, |
56 | | JsonTablePlanSpec *planspec, |
57 | | bool errorOnError, |
58 | | int colMin, int colMax, |
59 | | JsonTablePlan *childplan); |
60 | | static void CheckDuplicateColumnOrPathNames(JsonTableParseContext *cxt, |
61 | | List *columns); |
62 | | static bool LookupPathOrColumnName(JsonTableParseContext *cxt, char *name); |
63 | | static char *generateJsonTablePathName(JsonTableParseContext *cxt); |
64 | | static void validateJsonTableChildPlan(JsonTableParseContext *cxt, |
65 | | JsonTablePlanSpec *plan, |
66 | | List *columns); |
67 | | static JsonTablePlan *makeJsonTableSiblingJoin(bool cross, |
68 | | JsonTablePlan *lplan, |
69 | | JsonTablePlan *rplan); |
70 | | static void |
71 | | appendJsonTableColumns(JsonTableParseContext *cxt, List *columns, List *passingArgs); |
72 | | |
73 | | /* |
74 | | * transformJsonTable - |
75 | | * Transform a raw JsonTable into TableFunc |
76 | | * |
77 | | * Mainly, this transforms the JSON_TABLE() document-generating expression |
78 | | * (jt->context_item) and the column-generating expressions (jt->columns) to |
79 | | * populate TableFunc.docexpr and TableFunc.colvalexprs, respectively. Also, |
80 | | * the PASSING values (jt->passing) are transformed and added into |
81 | | * TableFunc.passingvalexprs. |
82 | | */ |
83 | | ParseNamespaceItem * |
84 | | transformJsonTable(ParseState *pstate, JsonTable *jt) |
85 | 0 | { |
86 | 0 | TableFunc *tf; |
87 | 0 | JsonFuncExpr *jfe; |
88 | 0 | JsonExpr *je; |
89 | 0 | JsonTablePlanSpec *plan = jt->planspec; |
90 | 0 | JsonTablePathSpec *rootPathSpec = jt->pathspec; |
91 | 0 | bool is_lateral; |
92 | 0 | JsonTableParseContext cxt = {pstate}; |
93 | |
|
94 | 0 | Assert(IsA(rootPathSpec->string, A_Const) && |
95 | 0 | castNode(A_Const, rootPathSpec->string)->val.node.type == T_String); |
96 | |
|
97 | 0 | if (jt->on_error && |
98 | 0 | jt->on_error->btype != JSON_BEHAVIOR_ERROR && |
99 | 0 | jt->on_error->btype != JSON_BEHAVIOR_EMPTY && |
100 | 0 | jt->on_error->btype != JSON_BEHAVIOR_EMPTY_ARRAY) |
101 | 0 | ereport(ERROR, |
102 | 0 | errcode(ERRCODE_SYNTAX_ERROR), |
103 | 0 | errmsg("invalid %s behavior", "ON ERROR"), |
104 | 0 | errdetail("Only EMPTY [ ARRAY ] or ERROR is allowed in the top-level ON ERROR clause."), |
105 | 0 | parser_errposition(pstate, jt->on_error->location)); |
106 | | |
107 | 0 | cxt.pathNameId = 0; |
108 | | |
109 | | /* |
110 | | * Collect the user-supplied path and column names, checking that they are |
111 | | * distinct. If the row pattern path has an explicit name, it shares this |
112 | | * namespace, so seed the list with it. |
113 | | */ |
114 | 0 | if (rootPathSpec->name != NULL) |
115 | 0 | cxt.pathNames = list_make1(rootPathSpec->name); |
116 | 0 | CheckDuplicateColumnOrPathNames(&cxt, jt->columns); |
117 | | |
118 | | /* |
119 | | * Generate a name for the row pattern path if it was not given one. Path |
120 | | * names are optional for every path, including when a PLAN clause is |
121 | | * present; a specific PLAN() can only reference named paths, so an |
122 | | * unnamed path that the plan must mention is caught later as a path name |
123 | | * mismatch or a path not covered by the plan. We generate the name only |
124 | | * after collecting the user-supplied names above, so that it cannot |
125 | | * collide with any of them. |
126 | | */ |
127 | 0 | if (rootPathSpec->name == NULL) |
128 | 0 | rootPathSpec->name = generateJsonTablePathName(&cxt); |
129 | | |
130 | | /* |
131 | | * We make lateral_only names of this level visible, whether or not the |
132 | | * RangeTableFunc is explicitly marked LATERAL. This is needed for SQL |
133 | | * spec compliance and seems useful on convenience grounds for all |
134 | | * functions in FROM. |
135 | | * |
136 | | * (LATERAL can't nest within a single pstate level, so we don't need |
137 | | * save/restore logic here.) |
138 | | */ |
139 | 0 | Assert(!pstate->p_lateral_active); |
140 | 0 | pstate->p_lateral_active = true; |
141 | |
|
142 | 0 | tf = makeNode(TableFunc); |
143 | 0 | tf->functype = TFT_JSON_TABLE; |
144 | | |
145 | | /* |
146 | | * Transform JsonFuncExpr representing the top JSON_TABLE context_item and |
147 | | * pathspec into a dummy JSON_TABLE_OP JsonExpr. |
148 | | * |
149 | | * The raw PASSING arguments are transformed here and again for each |
150 | | * column that has a path (see transformJsonTableColumn()). Since |
151 | | * transformExpr() modifies some raw nodes in place, such as replacing a |
152 | | * SubLink's subselect with its Query, each transformation must be given |
153 | | * its own copy, so that jt->passing stays untouched. |
154 | | */ |
155 | 0 | jfe = makeNode(JsonFuncExpr); |
156 | 0 | jfe->op = JSON_TABLE_OP; |
157 | 0 | jfe->context_item = jt->context_item; |
158 | 0 | jfe->pathspec = (Node *) rootPathSpec->string; |
159 | 0 | jfe->passing = copyObject(jt->passing); |
160 | 0 | jfe->on_empty = NULL; |
161 | 0 | jfe->on_error = jt->on_error; |
162 | 0 | jfe->location = jt->location; |
163 | 0 | tf->docexpr = transformExpr(pstate, (Node *) jfe, EXPR_KIND_FROM_FUNCTION); |
164 | | |
165 | | /* |
166 | | * Assign collations now, because assign_query_collations() doesn't look |
167 | | * into the range table. This must happen before the PASSING arguments |
168 | | * are copied into tf->passingvalexprs below, so that the copies carry the |
169 | | * collations too. |
170 | | */ |
171 | 0 | assign_expr_collations(pstate, tf->docexpr); |
172 | | |
173 | | /* |
174 | | * Create a JsonTablePlan that will generate row pattern that becomes |
175 | | * source data for JSON path expressions in jt->columns. This also adds |
176 | | * the columns' transformed JsonExpr nodes into tf->colvalexprs. |
177 | | */ |
178 | 0 | cxt.jt = jt; |
179 | 0 | cxt.tf = tf; |
180 | 0 | tf->plan = (Node *) transformJsonTableColumns(&cxt, plan, jt->columns, |
181 | 0 | jt->passing, |
182 | 0 | rootPathSpec); |
183 | | |
184 | | /* |
185 | | * Copy the transformed PASSING arguments into the TableFunc node, because |
186 | | * they are evaluated separately from the JsonExpr that we just put in |
187 | | * TableFunc.docexpr. JsonExpr.passing_values is still kept around for |
188 | | * get_json_table(). |
189 | | */ |
190 | 0 | je = (JsonExpr *) tf->docexpr; |
191 | 0 | tf->passingvalexprs = copyObject(je->passing_values); |
192 | |
|
193 | 0 | tf->ordinalitycol = -1; /* undefine ordinality column number */ |
194 | 0 | tf->location = jt->location; |
195 | |
|
196 | 0 | pstate->p_lateral_active = false; |
197 | | |
198 | | /* |
199 | | * Mark the RTE as LATERAL if the user said LATERAL explicitly, or if |
200 | | * there are any lateral cross-references in it. |
201 | | */ |
202 | 0 | is_lateral = jt->lateral || contain_vars_of_level((Node *) tf, 0); |
203 | |
|
204 | 0 | return addRangeTableEntryForTableFunc(pstate, |
205 | 0 | tf, jt->alias, is_lateral, true); |
206 | 0 | } |
207 | | |
208 | | /* |
209 | | * Check if a column / path name is duplicated in the given shared list of |
210 | | * names. |
211 | | */ |
212 | | static void |
213 | | CheckDuplicateColumnOrPathNames(JsonTableParseContext *cxt, |
214 | | List *columns) |
215 | 0 | { |
216 | 0 | ListCell *lc1; |
217 | |
|
218 | 0 | foreach(lc1, columns) |
219 | 0 | { |
220 | 0 | JsonTableColumn *jtc = castNode(JsonTableColumn, lfirst(lc1)); |
221 | |
|
222 | 0 | if (jtc->coltype == JTC_NESTED) |
223 | 0 | { |
224 | 0 | if (jtc->pathspec->name) |
225 | 0 | { |
226 | 0 | if (LookupPathOrColumnName(cxt, jtc->pathspec->name)) |
227 | 0 | ereport(ERROR, |
228 | 0 | errcode(ERRCODE_DUPLICATE_ALIAS), |
229 | 0 | errmsg("duplicate JSON_TABLE column or path name: %s", |
230 | 0 | jtc->pathspec->name), |
231 | 0 | parser_errposition(cxt->pstate, |
232 | 0 | jtc->pathspec->name_location)); |
233 | 0 | cxt->pathNames = lappend(cxt->pathNames, jtc->pathspec->name); |
234 | 0 | } |
235 | | |
236 | 0 | CheckDuplicateColumnOrPathNames(cxt, jtc->columns); |
237 | 0 | } |
238 | 0 | else |
239 | 0 | { |
240 | 0 | if (LookupPathOrColumnName(cxt, jtc->name)) |
241 | 0 | ereport(ERROR, |
242 | 0 | errcode(ERRCODE_DUPLICATE_ALIAS), |
243 | 0 | errmsg("duplicate JSON_TABLE column or path name: %s", |
244 | 0 | jtc->name), |
245 | 0 | parser_errposition(cxt->pstate, jtc->location)); |
246 | 0 | cxt->pathNames = lappend(cxt->pathNames, jtc->name); |
247 | 0 | } |
248 | 0 | } |
249 | 0 | } |
250 | | |
251 | | /* |
252 | | * Lookup a column/path name in the given name list, returning true if already |
253 | | * there. |
254 | | */ |
255 | | static bool |
256 | | LookupPathOrColumnName(JsonTableParseContext *cxt, char *name) |
257 | 0 | { |
258 | 0 | ListCell *lc; |
259 | |
|
260 | 0 | foreach(lc, cxt->pathNames) |
261 | 0 | { |
262 | 0 | if (strcmp(name, (const char *) lfirst(lc)) == 0) |
263 | 0 | return true; |
264 | 0 | } |
265 | | |
266 | 0 | return false; |
267 | 0 | } |
268 | | |
269 | | /* Generate a new unique JSON_TABLE path name. */ |
270 | | static char * |
271 | | generateJsonTablePathName(JsonTableParseContext *cxt) |
272 | 0 | { |
273 | 0 | char namebuf[32]; |
274 | 0 | char *name; |
275 | | |
276 | | /* |
277 | | * Bump the counter until we produce a name that is not already used as a |
278 | | * path or column name. Otherwise a generated name could coincide with a |
279 | | * user-supplied one, which would confuse PLAN clause matching and could |
280 | | * silently drop a column. |
281 | | */ |
282 | 0 | do |
283 | 0 | { |
284 | 0 | snprintf(namebuf, sizeof(namebuf), "json_table_path_%d", |
285 | 0 | cxt->pathNameId++); |
286 | 0 | } while (LookupPathOrColumnName(cxt, namebuf)); |
287 | |
|
288 | 0 | name = pstrdup(namebuf); |
289 | 0 | cxt->pathNames = lappend(cxt->pathNames, name); |
290 | |
|
291 | 0 | return name; |
292 | 0 | } |
293 | | |
294 | | /* |
295 | | * Create a JsonTablePlan that will supply the source row for 'columns' |
296 | | * using 'pathspec' and append the columns' transformed JsonExpr nodes and |
297 | | * their type/collation information to cxt->tf. |
298 | | */ |
299 | | static JsonTablePlan * |
300 | | transformJsonTableColumns(JsonTableParseContext *cxt, |
301 | | JsonTablePlanSpec *planspec, |
302 | | List *columns, |
303 | | List *passingArgs, |
304 | | JsonTablePathSpec *pathspec) |
305 | 0 | { |
306 | 0 | JsonTable *jt = cxt->jt; |
307 | 0 | TableFunc *tf = cxt->tf; |
308 | 0 | JsonTablePathScan *scan; |
309 | 0 | JsonTablePlanSpec *childPlanSpec; |
310 | 0 | bool defaultPlan = planspec == NULL || |
311 | 0 | planspec->plan_type == JSTP_DEFAULT; |
312 | 0 | bool errorOnError = jt->on_error && |
313 | 0 | jt->on_error->btype == JSON_BEHAVIOR_ERROR; |
314 | 0 | int colMin, |
315 | 0 | colMax; |
316 | 0 | JsonTablePlan *childplan = NULL; |
317 | | |
318 | | /* Start of column range */ |
319 | 0 | colMin = list_length(tf->colvalexprs); |
320 | |
|
321 | 0 | if (defaultPlan) |
322 | 0 | childPlanSpec = planspec; |
323 | 0 | else |
324 | 0 | { |
325 | | /* validate parent and child plans */ |
326 | 0 | JsonTablePlanSpec *parentPlanSpec; |
327 | |
|
328 | 0 | if (planspec->plan_type == JSTP_JOINED) |
329 | 0 | { |
330 | 0 | if (planspec->join_type != JSTP_JOIN_INNER && |
331 | 0 | planspec->join_type != JSTP_JOIN_OUTER) |
332 | 0 | ereport(ERROR, |
333 | 0 | (errcode(ERRCODE_SYNTAX_ERROR), |
334 | 0 | errmsg("invalid JSON_TABLE plan clause"), |
335 | 0 | errdetail("Expected INNER or OUTER."), |
336 | 0 | parser_errposition(cxt->pstate, planspec->location))); |
337 | | |
338 | 0 | parentPlanSpec = planspec->plan1; |
339 | 0 | childPlanSpec = planspec->plan2; |
340 | |
|
341 | 0 | Assert(parentPlanSpec->plan_type != JSTP_JOINED); |
342 | 0 | Assert(parentPlanSpec->pathname); |
343 | 0 | } |
344 | 0 | else |
345 | 0 | { |
346 | 0 | parentPlanSpec = planspec; |
347 | 0 | childPlanSpec = NULL; |
348 | 0 | } |
349 | | |
350 | 0 | if (strcmp(parentPlanSpec->pathname, pathspec->name) != 0) |
351 | 0 | ereport(ERROR, |
352 | 0 | (errcode(ERRCODE_SYNTAX_ERROR), |
353 | 0 | errmsg("invalid JSON_TABLE plan"), |
354 | 0 | errdetail("PATH name mismatch: expected %s but %s is given.", |
355 | 0 | pathspec->name, parentPlanSpec->pathname), |
356 | 0 | parser_errposition(cxt->pstate, planspec->location))); |
357 | | |
358 | 0 | validateJsonTableChildPlan(cxt, childPlanSpec, columns); |
359 | 0 | } |
360 | | |
361 | 0 | appendJsonTableColumns(cxt, columns, passingArgs); |
362 | | |
363 | | /* End of column range. */ |
364 | 0 | if (list_length(tf->colvalexprs) == colMin) |
365 | 0 | { |
366 | | /* No columns in this Scan beside the nested ones. */ |
367 | 0 | colMax = colMin = -1; |
368 | 0 | } |
369 | 0 | else |
370 | 0 | colMax = list_length(tf->colvalexprs) - 1; |
371 | |
|
372 | 0 | if (childPlanSpec || defaultPlan) |
373 | 0 | { |
374 | | /* transform recursively nested columns */ |
375 | 0 | childplan = transformJsonTableNestedColumns(cxt, childPlanSpec, |
376 | 0 | columns, passingArgs); |
377 | 0 | } |
378 | | |
379 | | /* transform only non-nested columns */ |
380 | 0 | scan = (JsonTablePathScan *) makeJsonTablePathScan(cxt, pathspec, |
381 | 0 | planspec, |
382 | 0 | errorOnError, |
383 | 0 | colMin, |
384 | 0 | colMax, |
385 | 0 | childplan); |
386 | |
|
387 | 0 | return (JsonTablePlan *) scan; |
388 | 0 | } |
389 | | |
390 | | /* Append transformed non-nested JSON_TABLE columns to the TableFunc node */ |
391 | | static void |
392 | | appendJsonTableColumns(JsonTableParseContext *cxt, List *columns, List *passingArgs) |
393 | 0 | { |
394 | 0 | ListCell *col; |
395 | 0 | ParseState *pstate = cxt->pstate; |
396 | 0 | TableFunc *tf = cxt->tf; |
397 | 0 | bool ordinality_found = false; |
398 | 0 | Oid contextItemTypid = exprType(tf->docexpr); |
399 | |
|
400 | 0 | foreach(col, columns) |
401 | 0 | { |
402 | 0 | JsonTableColumn *rawc = castNode(JsonTableColumn, lfirst(col)); |
403 | 0 | Oid typid; |
404 | 0 | int32 typmod; |
405 | 0 | Oid typcoll = InvalidOid; |
406 | 0 | Node *colexpr; |
407 | |
|
408 | 0 | if (rawc->name) |
409 | 0 | tf->colnames = lappend(tf->colnames, |
410 | 0 | makeString(pstrdup(rawc->name))); |
411 | | |
412 | | /* |
413 | | * Determine the type and typmod for the new column. FOR ORDINALITY |
414 | | * columns are INTEGER by standard; the others are user-specified. |
415 | | */ |
416 | 0 | switch (rawc->coltype) |
417 | 0 | { |
418 | 0 | case JTC_FOR_ORDINALITY: |
419 | 0 | if (ordinality_found) |
420 | 0 | ereport(ERROR, |
421 | 0 | (errcode(ERRCODE_SYNTAX_ERROR), |
422 | 0 | errmsg("only one FOR ORDINALITY column is allowed"), |
423 | 0 | parser_errposition(pstate, rawc->location))); |
424 | 0 | ordinality_found = true; |
425 | 0 | colexpr = NULL; |
426 | 0 | typid = INT4OID; |
427 | 0 | typmod = -1; |
428 | 0 | break; |
429 | | |
430 | 0 | case JTC_REGULAR: |
431 | 0 | typenameTypeIdAndMod(pstate, rawc->typeName, &typid, &typmod); |
432 | | |
433 | | /* |
434 | | * Use JTC_FORMATTED so as to use JSON_QUERY for this column |
435 | | * if the specified type is one that's better handled using |
436 | | * JSON_QUERY() or if non-default WRAPPER or QUOTES behavior |
437 | | * is specified. |
438 | | */ |
439 | 0 | if (isCompositeType(typid) || |
440 | 0 | rawc->quotes != JS_QUOTES_UNSPEC || |
441 | 0 | rawc->wrapper != JSW_UNSPEC) |
442 | 0 | rawc->coltype = JTC_FORMATTED; |
443 | |
|
444 | 0 | pg_fallthrough; |
445 | 0 | case JTC_FORMATTED: |
446 | 0 | case JTC_EXISTS: |
447 | 0 | { |
448 | 0 | JsonFuncExpr *jfe; |
449 | 0 | CaseTestExpr *param = makeNode(CaseTestExpr); |
450 | |
|
451 | 0 | param->collation = InvalidOid; |
452 | 0 | param->typeId = contextItemTypid; |
453 | 0 | param->typeMod = -1; |
454 | |
|
455 | 0 | jfe = transformJsonTableColumn(rawc, (Node *) param, |
456 | 0 | passingArgs); |
457 | |
|
458 | 0 | colexpr = transformExpr(pstate, (Node *) jfe, |
459 | 0 | EXPR_KIND_FROM_FUNCTION); |
460 | 0 | assign_expr_collations(pstate, colexpr); |
461 | |
|
462 | 0 | typid = exprType(colexpr); |
463 | 0 | typmod = exprTypmod(colexpr); |
464 | 0 | typcoll = exprCollation(colexpr); |
465 | 0 | break; |
466 | 0 | } |
467 | | |
468 | 0 | case JTC_NESTED: |
469 | 0 | continue; |
470 | | |
471 | 0 | default: |
472 | 0 | elog(ERROR, "unknown JSON_TABLE column type: %d", (int) rawc->coltype); |
473 | 0 | break; |
474 | 0 | } |
475 | | |
476 | 0 | tf->coltypes = lappend_oid(tf->coltypes, typid); |
477 | 0 | tf->coltypmods = lappend_int(tf->coltypmods, typmod); |
478 | 0 | tf->colcollations = lappend_oid(tf->colcollations, typcoll); |
479 | 0 | tf->colvalexprs = lappend(tf->colvalexprs, colexpr); |
480 | 0 | } |
481 | 0 | } |
482 | | |
483 | | /* |
484 | | * Check if the type is "composite" for the purpose of checking whether to use |
485 | | * JSON_VALUE() or JSON_QUERY() for a given JsonTableColumn. |
486 | | */ |
487 | | static bool |
488 | | isCompositeType(Oid typid) |
489 | 0 | { |
490 | 0 | char typtype = get_typtype(typid); |
491 | |
|
492 | 0 | return typid == JSONOID || |
493 | 0 | typid == JSONBOID || |
494 | 0 | typid == RECORDOID || |
495 | 0 | type_is_array(typid) || |
496 | 0 | typtype == TYPTYPE_COMPOSITE || |
497 | | /* domain over one of the above? */ |
498 | 0 | (typtype == TYPTYPE_DOMAIN && |
499 | 0 | isCompositeType(getBaseType(typid))); |
500 | 0 | } |
501 | | |
502 | | /* |
503 | | * Transform JSON_TABLE column definition into a JsonFuncExpr |
504 | | * This turns: |
505 | | * - regular column into JSON_VALUE() |
506 | | * - FORMAT JSON column into JSON_QUERY() |
507 | | * - EXISTS column into JSON_EXISTS() |
508 | | */ |
509 | | static JsonFuncExpr * |
510 | | transformJsonTableColumn(JsonTableColumn *jtc, Node *contextItemExpr, |
511 | | List *passingArgs) |
512 | 0 | { |
513 | 0 | Node *pathspec; |
514 | 0 | JsonFuncExpr *jfexpr = makeNode(JsonFuncExpr); |
515 | |
|
516 | 0 | if (jtc->coltype == JTC_REGULAR) |
517 | 0 | jfexpr->op = JSON_VALUE_OP; |
518 | 0 | else if (jtc->coltype == JTC_EXISTS) |
519 | 0 | jfexpr->op = JSON_EXISTS_OP; |
520 | 0 | else |
521 | 0 | jfexpr->op = JSON_QUERY_OP; |
522 | | |
523 | | /* Pass the column name so any runtime JsonExpr errors can print it. */ |
524 | 0 | Assert(jtc->name != NULL); |
525 | 0 | jfexpr->column_name = pstrdup(jtc->name); |
526 | |
|
527 | 0 | jfexpr->context_item = makeJsonValueExpr((Expr *) contextItemExpr, NULL, |
528 | 0 | makeJsonFormat(JS_FORMAT_DEFAULT, |
529 | 0 | JS_ENC_DEFAULT, |
530 | 0 | -1)); |
531 | 0 | if (jtc->pathspec) |
532 | 0 | pathspec = (Node *) jtc->pathspec->string; |
533 | 0 | else |
534 | 0 | { |
535 | | /* Construct default path as '$."column_name"' */ |
536 | 0 | StringInfoData path; |
537 | |
|
538 | 0 | initStringInfo(&path); |
539 | |
|
540 | 0 | appendStringInfoString(&path, "$."); |
541 | 0 | escape_json(&path, jtc->name); |
542 | |
|
543 | 0 | pathspec = makeStringConst(path.data, -1); |
544 | 0 | } |
545 | 0 | jfexpr->pathspec = pathspec; |
546 | | /* Each column gets its own copy; see transformJsonTable(). */ |
547 | 0 | jfexpr->passing = copyObject(passingArgs); |
548 | 0 | jfexpr->output = makeNode(JsonOutput); |
549 | 0 | jfexpr->output->typeName = jtc->typeName; |
550 | 0 | jfexpr->output->returning = makeNode(JsonReturning); |
551 | 0 | jfexpr->output->returning->format = jtc->format; |
552 | 0 | jfexpr->on_empty = jtc->on_empty; |
553 | 0 | jfexpr->on_error = jtc->on_error; |
554 | 0 | jfexpr->quotes = jtc->quotes; |
555 | 0 | jfexpr->wrapper = jtc->wrapper; |
556 | 0 | jfexpr->location = jtc->location; |
557 | |
|
558 | 0 | return jfexpr; |
559 | 0 | } |
560 | | |
561 | | static JsonTableColumn * |
562 | | findNestedJsonTableColumn(List *columns, const char *pathname) |
563 | 0 | { |
564 | 0 | ListCell *lc; |
565 | |
|
566 | 0 | foreach(lc, columns) |
567 | 0 | { |
568 | 0 | JsonTableColumn *jtc = castNode(JsonTableColumn, lfirst(lc)); |
569 | |
|
570 | 0 | if (jtc->coltype == JTC_NESTED && |
571 | 0 | jtc->pathspec->name && |
572 | 0 | !strcmp(jtc->pathspec->name, pathname)) |
573 | 0 | return jtc; |
574 | 0 | } |
575 | | |
576 | 0 | return NULL; |
577 | 0 | } |
578 | | |
579 | | /* |
580 | | * Recursively transform nested columns and create child plan(s) that will be |
581 | | * used to evaluate their row patterns. |
582 | | * |
583 | | * Default plan is transformed into a cross/union join of its nested columns. |
584 | | * Simple and outer/inner plans are transformed into a JsonTablePlan by |
585 | | * finding and transforming corresponding nested column. |
586 | | * Sibling plans are recursively transformed into a JsonTableSiblingJoin. |
587 | | */ |
588 | | static JsonTablePlan * |
589 | | transformJsonTableNestedColumns(JsonTableParseContext *cxt, |
590 | | JsonTablePlanSpec *planspec, |
591 | | List *columns, |
592 | | List *passingArgs) |
593 | 0 | { |
594 | 0 | JsonTableColumn *jtc = NULL; |
595 | |
|
596 | 0 | if (!planspec || planspec->plan_type == JSTP_DEFAULT) |
597 | 0 | { |
598 | | /* unspecified or default plan */ |
599 | 0 | JsonTablePlan *plan = NULL; |
600 | 0 | ListCell *lc; |
601 | 0 | bool cross = planspec && (planspec->join_type & JSTP_JOIN_CROSS); |
602 | | |
603 | | /* |
604 | | * If there are multiple NESTED COLUMNS clauses in 'columns', their |
605 | | * respective plans will be combined using a "sibling join" plan, |
606 | | * which effectively does a UNION of the sets of rows coming from each |
607 | | * nested plan. |
608 | | */ |
609 | 0 | foreach(lc, columns) |
610 | 0 | { |
611 | 0 | JsonTableColumn *col = castNode(JsonTableColumn, lfirst(lc)); |
612 | 0 | JsonTablePlan *nested; |
613 | |
|
614 | 0 | if (col->coltype != JTC_NESTED) |
615 | 0 | continue; |
616 | | |
617 | 0 | if (col->pathspec->name == NULL) |
618 | 0 | { |
619 | 0 | col->pathspec->name = generateJsonTablePathName(cxt); |
620 | 0 | } |
621 | |
|
622 | 0 | nested = transformJsonTableColumns(cxt, planspec, col->columns, |
623 | 0 | passingArgs, |
624 | 0 | col->pathspec); |
625 | | |
626 | | /* Join nested plan with previous sibling nested plans. */ |
627 | 0 | if (plan) |
628 | 0 | plan = makeJsonTableSiblingJoin(cross, plan, nested); |
629 | 0 | else |
630 | 0 | plan = nested; |
631 | 0 | } |
632 | |
|
633 | 0 | return plan; |
634 | 0 | } |
635 | 0 | else if (planspec->plan_type == JSTP_SIMPLE) |
636 | 0 | { |
637 | 0 | jtc = findNestedJsonTableColumn(columns, planspec->pathname); |
638 | 0 | } |
639 | 0 | else if (planspec->plan_type == JSTP_JOINED) |
640 | 0 | { |
641 | 0 | if (planspec->join_type == JSTP_JOIN_INNER || |
642 | 0 | planspec->join_type == JSTP_JOIN_OUTER) |
643 | 0 | { |
644 | 0 | Assert(planspec->plan1->plan_type == JSTP_SIMPLE); |
645 | 0 | jtc = findNestedJsonTableColumn(columns, planspec->plan1->pathname); |
646 | 0 | } |
647 | 0 | else |
648 | 0 | { |
649 | 0 | JsonTablePlan *lplan = transformJsonTableNestedColumns(cxt, |
650 | 0 | planspec->plan1, |
651 | 0 | columns, |
652 | 0 | passingArgs); |
653 | 0 | JsonTablePlan *rplan = transformJsonTableNestedColumns(cxt, |
654 | 0 | planspec->plan2, |
655 | 0 | columns, |
656 | 0 | passingArgs); |
657 | |
|
658 | 0 | return makeJsonTableSiblingJoin(planspec->join_type == JSTP_JOIN_CROSS, |
659 | 0 | lplan, rplan); |
660 | 0 | } |
661 | 0 | } |
662 | 0 | else |
663 | 0 | elog(ERROR, "invalid JSON_TABLE plan type %d", planspec->plan_type); |
664 | | |
665 | | /* |
666 | | * The plan's path names were already matched one-to-one against the |
667 | | * nested columns by validateJsonTableChildPlan(), so a nested column with |
668 | | * this path name must exist. |
669 | | */ |
670 | 0 | Assert(jtc != NULL); |
671 | |
|
672 | 0 | return transformJsonTableColumns(cxt, planspec, jtc->columns, |
673 | 0 | passingArgs, |
674 | 0 | jtc->pathspec); |
675 | 0 | } |
676 | | |
677 | | /* |
678 | | * Create transformed JSON_TABLE parent plan node by appending all non-nested |
679 | | * columns to the TableFunc node and remembering their indices in the |
680 | | * colvalexprs list. |
681 | | * |
682 | | * colMin and colMax give the range of columns computed by this scan in the |
683 | | * global flat list of column expressions that will be passed to the |
684 | | * JSON_TABLE's TableFunc. Both are -1 when all of columns are nested and |
685 | | * thus computed by 'childplan'. |
686 | | */ |
687 | | static JsonTablePlan * |
688 | | makeJsonTablePathScan(JsonTableParseContext *cxt, JsonTablePathSpec *pathspec, |
689 | | JsonTablePlanSpec *planspec, |
690 | | bool errorOnError, |
691 | | int colMin, int colMax, |
692 | | JsonTablePlan *childplan) |
693 | 0 | { |
694 | 0 | JsonTablePathScan *scan = makeNode(JsonTablePathScan); |
695 | 0 | char *pathstring; |
696 | 0 | Const *value; |
697 | |
|
698 | 0 | Assert(IsA(pathspec->string, A_Const)); |
699 | 0 | pathstring = castNode(A_Const, pathspec->string)->val.sval.sval; |
700 | 0 | value = makeConst(JSONPATHOID, -1, InvalidOid, -1, |
701 | 0 | DirectFunctionCall1(jsonpath_in, |
702 | 0 | CStringGetDatum(pathstring)), |
703 | 0 | false, false); |
704 | |
|
705 | 0 | scan->plan.type = T_JsonTablePathScan; |
706 | 0 | scan->path = makeJsonTablePath(value, pathspec->name); |
707 | 0 | scan->errorOnError = errorOnError; |
708 | |
|
709 | 0 | scan->child = childplan; |
710 | |
|
711 | 0 | scan->colMin = colMin; |
712 | 0 | scan->colMax = colMax; |
713 | |
|
714 | 0 | if (scan->child) |
715 | 0 | scan->outerJoin = planspec == NULL || |
716 | 0 | (planspec->join_type & JSTP_JOIN_OUTER); |
717 | | /* else: default plan case, no children found */ |
718 | |
|
719 | 0 | return (JsonTablePlan *) scan; |
720 | 0 | } |
721 | | |
722 | | /* |
723 | | * Create a JsonTablePlan that will perform a join of the rows coming from |
724 | | * 'lplan' and 'rplan'. |
725 | | * |
726 | | * The default way of "joining" the rows is to perform a UNION between the |
727 | | * sets of rows from 'lplan' and 'rplan'. |
728 | | */ |
729 | | static JsonTablePlan * |
730 | | makeJsonTableSiblingJoin(bool cross, JsonTablePlan *lplan, JsonTablePlan *rplan) |
731 | 0 | { |
732 | 0 | JsonTableSiblingJoin *join = makeNode(JsonTableSiblingJoin); |
733 | |
|
734 | 0 | join->plan.type = T_JsonTableSiblingJoin; |
735 | 0 | join->lplan = lplan; |
736 | 0 | join->rplan = rplan; |
737 | 0 | join->cross = cross; |
738 | |
|
739 | 0 | return (JsonTablePlan *) join; |
740 | 0 | } |
741 | | |
742 | | /* Collect sibling path names from plan to the specified list. */ |
743 | | static void |
744 | | collectSiblingPathsInJsonTablePlan(JsonTablePlanSpec *plan, List **paths) |
745 | 0 | { |
746 | 0 | if (plan->plan_type == JSTP_SIMPLE) |
747 | 0 | *paths = lappend(*paths, plan->pathname); |
748 | 0 | else if (plan->plan_type == JSTP_JOINED) |
749 | 0 | { |
750 | 0 | if (plan->join_type == JSTP_JOIN_INNER || |
751 | 0 | plan->join_type == JSTP_JOIN_OUTER) |
752 | 0 | { |
753 | 0 | Assert(plan->plan1->plan_type == JSTP_SIMPLE); |
754 | 0 | *paths = lappend(*paths, plan->plan1->pathname); |
755 | 0 | } |
756 | 0 | else if (plan->join_type == JSTP_JOIN_CROSS || |
757 | 0 | plan->join_type == JSTP_JOIN_UNION) |
758 | 0 | { |
759 | 0 | collectSiblingPathsInJsonTablePlan(plan->plan1, paths); |
760 | 0 | collectSiblingPathsInJsonTablePlan(plan->plan2, paths); |
761 | 0 | } |
762 | 0 | else |
763 | 0 | elog(ERROR, "invalid JSON_TABLE join type %d", |
764 | 0 | plan->join_type); |
765 | 0 | } |
766 | 0 | } |
767 | | |
768 | | /* |
769 | | * Validate child JSON_TABLE plan by checking that: |
770 | | * - all nested columns have path names specified |
771 | | * - all nested columns have corresponding node in the sibling plan |
772 | | * - plan does not contain duplicate or extra nodes |
773 | | */ |
774 | | static void |
775 | | validateJsonTableChildPlan(JsonTableParseContext *cxt, JsonTablePlanSpec *plan, |
776 | | List *columns) |
777 | 0 | { |
778 | 0 | ParseState *pstate = cxt->pstate; |
779 | 0 | ListCell *lc1; |
780 | 0 | List *siblings = NIL; |
781 | 0 | int nchildren = 0; |
782 | |
|
783 | 0 | if (plan) |
784 | 0 | collectSiblingPathsInJsonTablePlan(plan, &siblings); |
785 | |
|
786 | 0 | foreach(lc1, columns) |
787 | 0 | { |
788 | 0 | JsonTableColumn *jtc = castNode(JsonTableColumn, lfirst(lc1)); |
789 | |
|
790 | 0 | if (jtc->coltype == JTC_NESTED) |
791 | 0 | { |
792 | 0 | ListCell *lc2; |
793 | 0 | bool found = false; |
794 | | |
795 | | /* |
796 | | * A NESTED path need not be named; generate a name if it was left |
797 | | * unnamed. A generated name cannot be referenced by a specific |
798 | | * PLAN(), so such a path is reported just below as a nested path |
799 | | * not covered by the plan. |
800 | | */ |
801 | 0 | if (jtc->pathspec->name == NULL) |
802 | 0 | jtc->pathspec->name = generateJsonTablePathName(cxt); |
803 | | |
804 | | /* find nested path name in the list of sibling path names */ |
805 | 0 | foreach(lc2, siblings) |
806 | 0 | { |
807 | 0 | if ((found = !strcmp(jtc->pathspec->name, lfirst(lc2)))) |
808 | 0 | break; |
809 | 0 | } |
810 | |
|
811 | 0 | if (!found) |
812 | 0 | ereport(ERROR, |
813 | 0 | errcode(ERRCODE_SYNTAX_ERROR), |
814 | 0 | errmsg("invalid JSON_TABLE specification"), |
815 | 0 | errdetail("PLAN clause for nested path %s was not found.", |
816 | 0 | jtc->pathspec->name), |
817 | 0 | parser_errposition(pstate, jtc->location)); |
818 | | |
819 | 0 | nchildren++; |
820 | 0 | } |
821 | 0 | } |
822 | | |
823 | 0 | if (list_length(siblings) > nchildren) |
824 | 0 | ereport(ERROR, |
825 | 0 | errcode(ERRCODE_SYNTAX_ERROR), |
826 | 0 | errmsg("invalid JSON_TABLE plan clause"), |
827 | 0 | errdetail("PLAN clause contains some extra or duplicate sibling nodes."), |
828 | 0 | parser_errposition(pstate, plan ? plan->location : -1)); |
829 | 0 | } |