1 /*-------------------------------------------------------------------------
3 * pgsp_json.c: Plan handler for JSON/XML/YAML style plans
5 * Copyright (c) 2012-2016, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
8 * pg_store_plan/pgsp_json.c
10 *-------------------------------------------------------------------------
14 #include "miscadmin.h"
15 #include "nodes/nodes.h"
16 #include "nodes/parsenodes.h"
17 #include "nodes/bitmapset.h"
18 #include "parser/scanner.h"
19 #include "parser/gram.h"
20 #include "utils/xml.h"
21 #include "utils/json.h"
22 #include "utils/jsonapi.h"
24 #include "pgsp_json.h"
25 #include "pgsp_json_int.h"
30 void normalize_expr(char *expr, bool preserve_space);
31 static const char *converter_core(word_table *tbl,
32 const char *src, pgsp_parser_mode mode);
34 static void json_objstart(void *state);
35 static void json_objend(void *state);
36 static void json_arrstart(void *state);
37 static void json_arrend(void *state);
38 static void json_ofstart(void *state, char *fname, bool isnull);
39 static void json_aestart(void *state, bool isnull);
40 static void json_scalar(void *state, char *token, JsonTokenType tokentype);
42 static void yaml_objstart(void *state);
43 static void yaml_objend(void *state);
44 static void yaml_arrstart(void *state);
45 static void yaml_arrend(void *state);
46 static void yaml_ofstart(void *state, char *fname, bool isnull);
47 static void yaml_aestart(void *state, bool isnull);
48 static void yaml_scalar(void *state, char *token, JsonTokenType tokentype);
50 static void adjust_wbuf(pgspParserContext *ctx, int len);
51 static char *hyphenate_words(pgspParserContext *ctx, char *src);
52 static void xml_objstart(void *state);
53 static void xml_objend(void *state);
54 static void xml_arrend(void *state);
55 static void xml_ofstart(void *state, char *fname, bool isnull);
56 static void xml_ofend(void *state, char *fname, bool isnull);
57 static void xml_aestart(void *state, bool isnull);
58 static void xml_aeend(void *state, bool isnull);
59 static void xml_scalar(void *state, char *token, JsonTokenType tokentype) ;
61 static void init_json_semaction(JsonSemAction *sem,
62 pgspParserContext *ctx);
64 word_table propfields[] =
66 {P_NodeType, "t" ,"Node Type", NULL, true, conv_nodetype, SETTER(node_type)},
67 {P_RelationShip, "h" ,"Parent Relationship", NULL, true, conv_relasionship, NULL},
68 {P_RelationName, "n" ,"Relation Name", NULL, true, NULL, SETTER(obj_name)},
69 {P_FunctioName, "f" ,"Function Name", NULL, true, NULL, SETTER(obj_name)},
70 {P_IndexName, "i" ,"Index Name", NULL, true, NULL, SETTER(index_name)},
71 {P_CTEName, "c" ,"CTE Name", NULL, true, NULL, SETTER(obj_name)},
72 {P_TrgRelation, "w" ,"Relation", NULL, true, NULL, SETTER(trig_relation)},
73 {P_Schema, "s" ,"Schema", NULL, true, NULL, SETTER(schema_name)},
74 {P_Alias, "a" ,"Alias", NULL, true, NULL, SETTER(alias)},
75 {P_Output, "o" ,"Output", NULL, true, conv_expression, SETTER(output)},
76 {P_ScanDir, "d" ,"Scan Direction", NULL, true, conv_scandir, SETTER(scan_dir)},
77 {P_MergeCond, "m" ,"Merge Cond", NULL, true, conv_expression, SETTER(merge_cond)},
78 {P_Strategy, "g" ,"Strategy", NULL, true, conv_strategy, SETTER(strategy)},
79 {P_JoinType, "j" ,"Join Type", NULL, true, conv_jointype, SETTER(join_type)},
80 {P_SortMethod, "e" ,"Sort Method", NULL, true, conv_sortmethod, SETTER(sort_method)},
81 {P_SortKey, "k" ,"Sort Key", NULL, true, conv_expression, SETTER(sort_key)},
82 {P_Filter, "5" ,"Filter", NULL, true, conv_expression, SETTER(filter)},
83 {P_JoinFilter, "6" ,"Join Filter", NULL, true, conv_expression, SETTER(join_filter)},
84 {P_HashCond, "7" ,"Hash Cond", NULL, true, conv_expression, SETTER(hash_cond)},
85 {P_IndexCond, "8" ,"Index Cond", NULL, true, conv_expression, SETTER(index_cond)},
86 {P_TidCond, "9" ,"TID Cond", NULL, true, conv_expression, SETTER(tid_cond)},
87 {P_RecheckCond, "0" ,"Recheck Cond", NULL, true, conv_expression, SETTER(recheck_cond)},
88 {P_Operation, "!" ,"Operation", NULL, true, conv_operation, SETTER(operation)},
89 {P_SubplanName, "q" ,"Subplan Name", NULL, true, NULL, SETTER(subplan_name)},
90 {P_Command, "b" ,"Command", NULL, true, conv_setsetopcommand,SETTER(setopcommand)},
91 {P_Triggers, "r" ,"Triggers", NULL, true, NULL, NULL},
92 {P_Trigger, "u" ,"Trigger", NULL, true, NULL, SETTER(node_type)},
93 {P_TriggerName, "v" ,"Trigger Name", NULL, true, NULL, SETTER(trig_name)},
94 {P_ConstraintName, "x" ,"Constraint Name", NULL, true, NULL, NULL},
95 {P_Plans, "l" ,"Plans", NULL, true, NULL, NULL},
96 {P_Plan, "p" ,"Plan", NULL, true, NULL, NULL},
97 {P_GroupKey, "-" ,"Group Key", NULL, true, NULL, SETTER(group_key)},
98 {P_GroupSets, "=" ,"Grouping Sets", NULL, true, NULL, NULL},
99 {P_GroupKeys, "\\" ,"Group Keys", NULL, true, NULL, SETTER(group_key)},
100 {P_Parallel, "`" ,"Parallel Aware", NULL, true, NULL, SETTER(parallel_aware)},
101 {P_WorkersPlanned, "{" ,"Workers Planned", NULL, true, NULL, SETTER(workers_planned)},
102 {P_WorkersLaunched, "}" ,"Workers Launched", NULL, true, NULL, SETTER(workers_launched)},
104 /* Values of these properties are ignored on normalization */
105 {P_FunctionCall, "y" ,"Function Call", NULL, false, NULL, SETTER(func_call)},
106 {P_StartupCost, "1" ,"Startup Cost", NULL, false, NULL, SETTER(startup_cost)},
107 {P_TotalCost, "2" ,"Total Cost", NULL, false, NULL, SETTER(total_cost)},
108 {P_PlanRows, "3" ,"Plan Rows", NULL, false, NULL, SETTER(plan_rows)},
109 {P_PlanWidth, "4" ,"Plan Width", NULL, false, NULL, SETTER(plan_width)},
110 {P_ActualStartupTime,"A","Actual Startup Time", NULL, false, NULL, SETTER(actual_startup_time)},
111 {P_ActualTotalTime, "B" ,"Actual Total Time", NULL, false, NULL, SETTER(actual_total_time)},
112 {P_ActualRows, "C" ,"Actual Rows", NULL, false, NULL, SETTER(actual_rows)},
113 {P_ActualLoops, "D" ,"Actual Loops", NULL, false, NULL, SETTER(actual_loops)},
114 {P_HeapFetches, "E" ,"Heap Fetches", NULL, false, NULL, SETTER(heap_fetches)},
115 {P_SharedHitBlks, "F" ,"Shared Hit Blocks", NULL, false, NULL, SETTER(shared_hit_blks)},
116 {P_SharedReadBlks, "G" ,"Shared Read Blocks", NULL, false, NULL, SETTER(shared_read_blks)},
117 {P_SharedDirtiedBlks,"H","Shared Dirtied Blocks",NULL,false, NULL, SETTER(shared_dirtied_blks)},
118 {P_SharedWrittenBlks,"I","Shared Written Blocks",NULL,false, NULL, SETTER(shared_written_blks)},
119 {P_LocalHitBlks, "J" ,"Local Hit Blocks", NULL, false, NULL, SETTER(local_hit_blks)},
120 {P_LocalReadBlks, "K" ,"Local Read Blocks", NULL, false, NULL, SETTER(local_read_blks)},
121 {P_LocalDirtiedBlks,"L" ,"Local Dirtied Blocks",NULL, false, NULL, SETTER(local_dirtied_blks)},
122 {P_LocalWrittenBlks,"M" ,"Local Written Blocks",NULL, false, NULL, SETTER(local_written_blks)},
123 {P_TempReadBlks, "N" ,"Temp Read Blocks", NULL, false, NULL, SETTER(temp_read_blks)},
124 {P_TempWrittenBlks, "O" ,"Temp Written Blocks", NULL, false, NULL, SETTER(temp_written_blks)},
125 {P_IOReadTime, "P" ,"I/O Read Time", NULL, false, NULL, SETTER(io_read_time)},
126 {P_IOWwriteTime, "Q" ,"I/O Write Time", NULL, false, NULL, SETTER(io_write_time)},
127 {P_SortSpaceUsed, "R" ,"Sort Space Used", NULL, false, NULL, SETTER(sort_space_used)},
128 {P_SortSpaceType, "S" ,"Sort Space Type", NULL, false, conv_sortspacetype,SETTER(sort_space_type)},
129 {P_PeakMemoryUsage, "T" ,"Peak Memory Usage", NULL, false, NULL, SETTER(peak_memory_usage)},
130 {P_OrgHashBatches, "U","Original Hash Batches",NULL, false, NULL, SETTER(org_hash_batches)},
131 {P_OrgHashBuckets, "*","Original Hash Buckets",NULL, false, NULL, SETTER(org_hash_buckets)},
132 {P_HashBatches, "V" ,"Hash Batches", NULL, false, NULL, SETTER(hash_batches)},
133 {P_HashBuckets, "W" ,"Hash Buckets", NULL, false, NULL, SETTER(hash_buckets)},
134 {P_RowsFilterRmvd, "X" ,"Rows Removed by Filter",NULL,false,NULL, SETTER(filter_removed)},
135 {P_RowsIdxRchkRmvd, "Y" ,"Rows Removed by Index Recheck",NULL,false, NULL, SETTER(idxrchk_removed)},
136 {P_TrgTime, "Z" ,"Time", NULL, false, NULL, SETTER(trig_time)},
137 {P_TrgCalls, "z" ,"Calls", NULL, false, NULL, SETTER(trig_calls)},
138 {P_PlanTime, "#" ,"Planning Time", NULL, false, NULL, SETTER(plan_time)},
139 {P_ExecTime, "$" ,"Execution Time", NULL, false, NULL, SETTER(exec_time)},
140 {P_ExactHeapBlks, "&" ,"Exact Heap Blocks", NULL, false, NULL, SETTER(exact_heap_blks)},
141 {P_LossyHeapBlks, "(" ,"Lossy Heap Blocks", NULL, false, NULL, SETTER(lossy_heap_blks)},
142 {P_RowsJoinFltRemvd,")" ,"Rows Removed by Join Filter", NULL, false, NULL, SETTER(joinfilt_removed)},
143 {P_TargetTables, "_" ,"Target Tables", NULL, false, NULL, NULL},
144 {P_ConfRes, "%" ,"Conflict Resolution", NULL, false, NULL, SETTER(conflict_resolution)},
145 {P_ConfArbitIdx, "@" ,"Conflict Arbiter Indexes",NULL, false, NULL, SETTER(conflict_arbiter_indexes)},
146 {P_TuplesInserted, "^" ,"Tuples Inserted", NULL, false, NULL, SETTER(tuples_inserted)},
147 {P_ConfTuples, "+" ,"Conflicting Tuples", NULL, false, NULL, SETTER(conflicting_tuples)},
148 {P_SamplingMethod, "" ,"Sampling Method" , NULL, false, NULL, SETTER(sampling_method)},
149 {P_SamplingParams, "" ,"Sampling Parameters" , NULL, false, NULL, SETTER(sampling_params)},
150 {P_RepeatableSeed, "" ,"Repeatable Seed" , NULL, false, NULL, SETTER(repeatable_seed)},
151 {P_Workers, "[" ,"Workers", NULL, false, NULL, NULL},
152 {P_WorkerNumber, "]" ,"Worker Number", NULL, false, NULL, SETTER(worker_number)},
154 {P_Invalid, NULL, NULL, NULL, false, NULL, NULL}
157 word_table nodetypes[] =
159 {T_Result, "a" ,"Result", NULL, false, NULL, NULL},
160 {T_ModifyTable, "b" ,"ModifyTable", NULL, false, NULL, NULL},
161 {T_Append, "c" ,"Append", NULL, false, NULL, NULL},
162 {T_MergeAppend, "d" ,"Merge Append", NULL, false, NULL, NULL},
163 {T_RecursiveUnion,"e" ,"Recursive Union",NULL, false, NULL, NULL},
164 {T_BitmapAnd, "f" ,"BitmapAnd", NULL, false, NULL, NULL},
165 {T_BitmapOr, "g" ,"BitmapOr", NULL, false, NULL, NULL},
166 {T_Scan, "" , "", "", false, NULL, NULL},
167 {T_SeqScan, "h" ,"Seq Scan", NULL, false, NULL, NULL},
168 {T_IndexScan, "i" ,"Index Scan", NULL, false, NULL, NULL},
169 {T_IndexOnlyScan,"j","Index Only Scan",NULL, false, NULL, NULL},
170 {T_BitmapIndexScan,"k" ,"Bitmap Index Scan", NULL, false, NULL, NULL},
171 {T_BitmapHeapScan,"l" ,"Bitmap Heap Scan", NULL ,false, NULL, NULL},
172 {T_TidScan, "m" ,"Tid Scan", NULL, false, NULL, NULL},
173 {T_SubqueryScan,"n" ,"Subquery Scan", NULL, false, NULL, NULL},
174 {T_FunctionScan,"o" ,"Function Scan", NULL, false, NULL, NULL},
175 {T_ValuesScan, "p" ,"Values Scan", NULL, false, NULL, NULL},
176 {T_CteScan, "q" ,"CTE Scan", NULL, false, NULL, NULL},
177 {T_WorkTableScan,"r","WorkTable Scan", NULL, false, NULL, NULL},
178 {T_ForeignScan, "s" , "Foreign Scan", NULL, false, NULL, NULL},
179 {T_Join, "" , "", NULL, false, NULL, NULL},
180 {T_NestLoop, "t" ,"Nested Loop", NULL, false, NULL, NULL},
181 {T_MergeJoin, "u" ,"Merge Join", "Merge", false, NULL, NULL},
182 {T_HashJoin, "v" ,"Hash Join", "Hash", false, NULL, NULL},
183 {T_Material, "w" ,"Materialize", NULL, false, NULL, NULL},
184 {T_Sort, "x" ,"Sort", NULL, false, NULL, NULL},
185 {T_Group, "y" ,"Group", NULL, false, NULL, NULL},
186 {T_Agg, "z" ,"Aggregate", NULL, false, NULL, NULL},
187 {T_WindowAgg, "0" ,"WindowAgg", NULL, false, NULL, NULL},
188 {T_Unique, "1" ,"Unique", NULL, false, NULL, NULL},
189 {T_Hash, "2" ,"Hash", NULL, false, NULL, NULL},
190 {T_SetOp, "3" ,"SetOp", NULL, false, NULL, NULL},
191 {T_LockRows, "4" ,"LockRows", NULL, false, NULL, NULL},
192 {T_Limit, "5" ,"Limit", NULL, false, NULL, NULL},
193 #if PG_VERSION_NUM >= 90600
194 {T_Gather, "6" ,"Gather", NULL, false, NULL, NULL},
196 {T_Invalid, NULL, NULL, NULL, false, NULL, NULL}
199 word_table directions[] =
201 {T_Invalid, "b" ,"Backward", "Backward", false, NULL, NULL},
202 {T_Invalid, "n" ,"NoMovement","", false, NULL, NULL},
203 {T_Invalid, "f" ,"Forward", "", false, NULL, NULL},
204 {T_Invalid, NULL , NULL, NULL, false, NULL, NULL}
207 word_table relationships[] =
209 {T_Invalid, "o" ,"Outer", NULL, false, NULL, NULL},
210 {T_Invalid, "i" ,"Inner", NULL, false, NULL, NULL},
211 {T_Invalid, "s" ,"Subquery", NULL, false, NULL, NULL},
212 {T_Invalid, "m" ,"Member", NULL, false, NULL, NULL},
213 {T_Invalid, "I" ,"InitPlan", NULL, false, NULL, NULL},
214 {T_Invalid, "S" ,"SubPlan", NULL, false, NULL, NULL},
215 {T_Invalid, NULL, NULL, NULL, false, NULL, NULL}
218 word_table strategies[] =
220 {S_Plain, "p" ,"Plain", NULL, false, NULL, NULL},
221 {S_Sorted, "s" ,"Sorted", NULL, false, NULL, NULL},
222 {S_Hashed, "h" ,"Hashed", NULL, false, NULL, NULL},
223 {S_Invalid, NULL, NULL, NULL, false, NULL, NULL}
226 word_table operations[] =
228 {T_Invalid, "i" ,"Insert", NULL, false, NULL, NULL},
229 {T_Invalid, "d" ,"Delete", NULL, false, NULL, NULL},
230 {T_Invalid, "u" ,"Update", NULL, false, NULL, NULL},
231 {T_Invalid, NULL, NULL, NULL, false, NULL, NULL}
234 word_table jointypes[] =
236 {T_Invalid, "i" ,"Inner", NULL, false, NULL, NULL},
237 {T_Invalid, "l" ,"Left", NULL, false, NULL, NULL},
238 {T_Invalid, "f" ,"Full", NULL, false, NULL, NULL},
239 {T_Invalid, "r" ,"Right", NULL, false, NULL, NULL},
240 {T_Invalid, "s" ,"Semi", NULL, false, NULL, NULL},
241 {T_Invalid, "a" ,"Anti", NULL, false, NULL, NULL},
242 {T_Invalid, NULL, NULL, NULL, false, NULL, NULL}
245 word_table setsetopcommands[] =
247 {T_Invalid, "i" ,"Intersect", NULL, false, NULL, NULL},
248 {T_Invalid, "I" ,"Intersect All", NULL, false, NULL, NULL},
249 {T_Invalid, "e" ,"Except", NULL, false, NULL, NULL},
250 {T_Invalid, "E" ,"Except All", NULL, false, NULL, NULL},
251 {T_Invalid, NULL, NULL, NULL, false, NULL, NULL}
254 word_table sortmethods[] =
256 {T_Invalid, "h" ,"top-N heapsort", NULL, false, NULL, NULL},
257 {T_Invalid, "q" ,"quicksort", NULL, false, NULL, NULL},
258 {T_Invalid, "e" ,"external sort", NULL, false, NULL, NULL},
259 {T_Invalid, "E" ,"external merge", NULL, false, NULL, NULL},
260 {T_Invalid, "s" ,"still in progress", NULL, false, NULL, NULL},
261 {T_Invalid, NULL, NULL, NULL, false, NULL, NULL}
264 word_table sortspacetype[] =
266 {T_Invalid, "d" ,"Disk", NULL, false, NULL, NULL},
267 {T_Invalid, "m" ,"Memory",NULL, false, NULL, NULL},
268 {T_Invalid, NULL, NULL, NULL, false, NULL, NULL}
272 search_word_table(word_table *tbl, const char *word, int mode)
277 (mode == PGSP_JSON_SHORTEN || mode == PGSP_JSON_NORMALIZE);
281 * Use simple linear search. We can gain too small portion of the whole
282 * processing time using more 'clever' algorithms like b-tree or tries,
283 * which won't be worth the additional memory, complexity and
284 * initialization cost.
286 for (p = tbl ; p->longname ; p++)
288 if (strcmp(longname ? p->longname: p->shortname, word) == 0)
292 if (p->longname == NULL && mode == PGSP_JSON_TEXTIZE)
294 /* Fallback to long json prop name */
295 for (p = tbl ; p->longname ; p++)
296 if (strcmp(p->longname, word) == 0)
300 return (p->longname ? p : NULL);
305 converter_core(word_table *tbl,
306 const char *src, pgsp_parser_mode mode)
311 p = search_word_table(tbl, src, mode);
318 case PGSP_JSON_SHORTEN:
319 case PGSP_JSON_NORMALIZE:
322 case PGSP_JSON_INFLATE:
323 case PGSP_JSON_YAMLIZE:
324 case PGSP_JSON_XMLIZE:
327 case PGSP_JSON_TEXTIZE:
334 elog(ERROR, "Internal error");
340 conv_nodetype(const char *src, pgsp_parser_mode mode)
342 return converter_core(nodetypes, src, mode);
346 conv_scandir(const char *src, pgsp_parser_mode mode)
348 return converter_core(directions, src, mode);
352 conv_relasionship(const char *src, pgsp_parser_mode mode)
354 return converter_core(relationships, src, mode);
358 conv_strategy(const char *src, pgsp_parser_mode mode)
360 return converter_core(strategies, src, mode);
364 * Look for these operator characters in order to decide whether to strip
365 * whitespaces which are needless from the view of sql syntax in
366 * normalize_expr(). This must be synced with op_chars in scan.l.
368 #define OPCHARS "~!@#^&|`?+-*/%<>="
369 #define IS_WSCHAR(c) ((c) == ' ' || (c) == '\n' || (c) == '\t')
370 #define IS_CONST(tok) (tok == FCONST || tok == SCONST || tok == BCONST || \
371 tok == XCONST || tok == ICONST || tok == NULL_P || \
372 tok == TRUE_P || tok == FALSE_P || \
373 tok == CURRENT_DATE || tok == CURRENT_TIME || \
374 tok == LOCALTIME || tok == LOCALTIMESTAMP)
377 * norm_yylex: core_yylex with replacing some tokens.
380 norm_yylex(char *str, core_YYSTYPE *yylval, YYLTYPE *yylloc, core_yyscan_t yyscanner)
386 tok = core_yylex(yylval, yylloc, yyscanner);
391 * Error might occur during parsing quoted tokens that chopped
392 * halfway. Just ignore the rest of this query even if there might
393 * be other reasons for parsing to fail.
401 * '?' alone is assumed to be an IDENT. If there's a real
402 * operator '?', this should be confused but there's hardly be.
404 if (tok == Op && str[*yylloc] == '?' &&
405 strchr(OPCHARS, str[*yylloc + 1]) == NULL)
409 * Replace tokens with '=' if the operator is consists of two or
410 * more opchars only. Assuming that opchars do not compose a token
411 * with non-opchars, check the first char only is sufficient.
413 if (tok == Op && strchr(OPCHARS, str[*yylloc]) != NULL)
420 * normalize_expr - Normalize statements or expressions.
422 * Mask constants, strip unnecessary whitespaces and upcase keywords. expr is
423 * modified in-place (destructively). If readability is more important than
424 * uniqueness, preserve_space puts one space for one existent whitespace for
428 normalize_expr(char *expr, bool preserve_space)
430 core_yyscan_t yyscanner;
431 core_yy_extra_type yyextra;
440 yyscanner = scanner_init(expr,
446 * The warnings about nonstandard escape strings is already emitted in the
447 * core. Just silence them here.
449 #if PG_VERSION_NUM >= 90500
450 yyextra.escape_string_warning = false;
457 tok = norm_yylex(expr, &yylval, &yylloc, yyscanner);
465 /* Skipping preceding whitespaces */
466 for(i = lastloc ; i < start && IS_WSCHAR(expr[i]) ; i++);
468 /* Searching for trailing whitespace */
469 for(i2 = i; i2 < start && !IS_WSCHAR(expr[i2]) ; i2++);
471 if (lasttok == IDENT)
473 /* Identifiers are copied in case-sensitive manner. */
474 memcpy(wp, expr + i, i2 - i);
479 /* Upcase keywords */
481 for (sp = expr + i ; sp < expr + i2 ; sp++, wp++)
482 *wp = (*sp >= 'a' && *sp <= 'z' ?
483 *sp - ('a' - 'A') : *sp);
487 * Because of destructive writing, wp must not go advance the
489 * Although this function's output does not need any validity as a
490 * statement or an expression, spaces are added where it should be
491 * to keep some extent of sanity. If readability is more important
492 * than uniqueness, preserve_space adds one space for each
493 * existent whitespace.
498 (tok >= IDENT && lasttok >= IDENT &&
499 !IS_CONST(tok) && !IS_CONST(lasttok))))
505 /* Exit on parse error. */
513 * Negative signs before numbers are tokenized separately. And
514 * explicit positive signs won't appear in deparsed expressions.
517 tok = norm_yylex(expr, &yylval, &yylloc, yyscanner);
519 /* Exit on parse error. */
530 tok = norm_yylex(expr, &yylval, &end, yyscanner);
532 /* Exit on parse error. */
540 * Negative values may be surrounded with parens by the
541 * deparser. Mask involving them.
543 if (lasttok == '(' && tok == ')')
545 wp -= (start - lastloc);
550 while (expr[end - 1] == ' ') end--;
566 conv_expression(const char *src, pgsp_parser_mode mode)
568 const char *ret = src;
570 if (mode == PGSP_JSON_NORMALIZE)
572 char *t = pstrdup(src);
573 normalize_expr(t, true);
574 ret = (const char *)t;
580 conv_operation(const char *src, pgsp_parser_mode mode)
582 return converter_core(operations, src, mode);
587 conv_jointype(const char *src, pgsp_parser_mode mode)
589 return converter_core(jointypes, src, mode);
593 conv_setsetopcommand(const char *src, pgsp_parser_mode mode)
595 return converter_core(setsetopcommands, src, mode);
599 conv_sortmethod(const char *src, pgsp_parser_mode mode)
601 return converter_core(sortmethods, src, mode);
605 conv_sortspacetype(const char *src, pgsp_parser_mode mode)
607 return converter_core(sortspacetype, src, mode);
610 /**** Parser callbacks ****/
614 json_objstart(void *state)
616 pgspParserContext *ctx = (pgspParserContext *)state;
618 if (ctx->mode == PGSP_JSON_INFLATE)
620 if (!ctx->fname && ctx->dest->len > 0)
622 appendStringInfoChar(ctx->dest, '\n');
623 appendStringInfoSpaces(ctx->dest, (ctx->level) * INDENT_STEP);
627 appendStringInfoChar(ctx->dest, '{');
630 ctx->first = bms_add_member(ctx->first, ctx->level);
632 if (ctx->mode == PGSP_JSON_INFLATE)
633 appendStringInfoChar(ctx->dest, '\n');
637 json_objend(void *state)
639 pgspParserContext *ctx = (pgspParserContext *)state;
640 if (ctx->mode == PGSP_JSON_INFLATE)
642 if (!bms_is_member(ctx->level, ctx->first))
643 appendStringInfoChar(ctx->dest, '\n');
644 appendStringInfoSpaces(ctx->dest, (ctx->level - 1) * INDENT_STEP);
647 appendStringInfoChar(ctx->dest, '}');
650 ctx->last_elem_is_object = true;
651 ctx->first = bms_del_member(ctx->first, ctx->level);
656 json_arrstart(void *state)
658 pgspParserContext *ctx = (pgspParserContext *)state;
660 if (ctx->current_list == P_GroupKeys)
663 appendStringInfoChar(ctx->dest, '[');
666 ctx->last_elem_is_object = true;
667 ctx->first = bms_add_member(ctx->first, ctx->level);
671 json_arrend(void *state)
673 pgspParserContext *ctx = (pgspParserContext *)state;
675 if (ctx->current_list == P_GroupKeys)
678 if (ctx->mode == PGSP_JSON_INFLATE &&
679 (ctx->current_list == P_GroupKeys ?
680 ctx->wlist_level == 0 : ctx->last_elem_is_object))
682 appendStringInfoChar(ctx->dest, '\n');
683 appendStringInfoSpaces(ctx->dest, (ctx->level - 1) * INDENT_STEP);
686 appendStringInfoChar(ctx->dest, ']');
691 json_ofstart(void *state, char *fname, bool isnull)
694 pgspParserContext *ctx = (pgspParserContext *)state;
698 p = search_word_table(propfields, fname, ctx->mode);
702 (errmsg("JSON parser encoutered unknown field name: \"%s\".", fname),
703 errdetail_log("INPUT: \"%s\"", ctx->org_string)));
706 ctx->remove = (ctx->mode == PGSP_JSON_NORMALIZE &&
707 (!p || !p->normalize_use));
712 if (!bms_is_member(ctx->level, ctx->first))
714 appendStringInfoChar(ctx->dest, ',');
715 if (ctx->mode == PGSP_JSON_INFLATE)
716 appendStringInfoChar(ctx->dest, '\n');
719 ctx->first = bms_del_member(ctx->first, ctx->level);
721 if (ctx->mode == PGSP_JSON_INFLATE)
722 appendStringInfoSpaces(ctx->dest, ctx->level * INDENT_STEP);
725 * We intentionally let some property names not have a short name. Use long
726 * name for the cases.
728 if (!p || !p->longname)
730 else if (ctx->mode == PGSP_JSON_INFLATE ||
731 !(p->shortname && p->shortname[0]))
736 escape_json(ctx->dest, fn);
738 ctx->valconverter = (p ? p->converter : NULL);
740 appendStringInfoChar(ctx->dest, ':');
742 if (ctx->mode == PGSP_JSON_INFLATE)
743 appendStringInfoChar(ctx->dest, ' ');
745 if (p && p->tag == P_GroupKeys)
747 ctx->current_list = p->tag;
748 ctx->list_fname = fname;
749 ctx->wlist_level = 0;
754 json_ofend(void *state, char *fname, bool isnull)
756 pgspParserContext *ctx = (pgspParserContext *)state;
758 if (ctx->list_fname && strcmp(fname, ctx->list_fname) == 0)
760 ctx->list_fname = NULL;
761 ctx->current_list = P_Invalid;
766 json_aestart(void *state, bool isnull)
768 pgspParserContext *ctx = (pgspParserContext *)state;
772 if (ctx->current_list == P_GroupKeys &&
773 ctx->wlist_level == 1)
775 if (!bms_is_member(ctx->level, ctx->first))
776 appendStringInfoChar(ctx->dest, ',');
778 if (ctx->mode == PGSP_JSON_INFLATE)
780 appendStringInfoChar(ctx->dest, '\n');
781 appendStringInfoSpaces(ctx->dest, (ctx->level) * INDENT_STEP);
786 if (!bms_is_member(ctx->level, ctx->first))
788 appendStringInfoChar(ctx->dest, ',');
790 if (ctx->mode == PGSP_JSON_INFLATE &&
791 !ctx->last_elem_is_object)
792 appendStringInfoChar(ctx->dest, ' ');
796 ctx->first = bms_del_member(ctx->first, ctx->level);
800 json_scalar(void *state, char *token, JsonTokenType tokentype)
802 pgspParserContext *ctx = (pgspParserContext *)state;
803 const char *val = token;
808 if (ctx->valconverter)
809 val = ctx->valconverter(token, ctx->mode);
811 if (tokentype == JSON_TOKEN_STRING)
812 escape_json(ctx->dest, val);
814 appendStringInfoString(ctx->dest, val);
815 ctx->last_elem_is_object = false;
821 yaml_objstart(void *state)
823 pgspParserContext *ctx = (pgspParserContext *)state;
827 if (ctx->dest->len > 0)
828 appendStringInfoChar(ctx->dest, '\n');
829 appendStringInfoSpaces(ctx->dest, (ctx->level - 1) * INDENT_STEP);
830 appendStringInfoString(ctx->dest, "- ");
831 appendStringInfoString(ctx->dest, ctx->fname);
832 appendStringInfoString(ctx->dest, ":\n");
833 appendStringInfoSpaces(ctx->dest, (ctx->level + 1) * INDENT_STEP);
838 ctx->first = bms_add_member(ctx->first, ctx->level);
842 yaml_objend(void *state)
844 pgspParserContext *ctx = (pgspParserContext *)state;
847 ctx->last_elem_is_object = true;
848 ctx->first = bms_del_member(ctx->first, ctx->level);
852 yaml_arrstart(void *state)
854 pgspParserContext *ctx = (pgspParserContext *)state;
858 appendStringInfoString(ctx->dest, ctx->fname);
859 appendStringInfoString(ctx->dest, ":");
864 ctx->first = bms_add_member(ctx->first, ctx->level);
868 yaml_arrend(void *state)
870 pgspParserContext *ctx = (pgspParserContext *)state;
874 yaml_ofstart(void *state, char *fname, bool isnull)
877 pgspParserContext *ctx = (pgspParserContext *)state;
880 p = search_word_table(propfields, fname, ctx->mode);
884 (errmsg("Short JSON parser encoutered unknown field name: \"%s\".", fname),
885 errdetail_log("INPUT: \"%s\"", ctx->org_string)));
887 s = (p ? p->longname : fname);
889 if (!bms_is_member(ctx->level, ctx->first))
891 appendStringInfoString(ctx->dest, "\n");
892 appendStringInfoSpaces(ctx->dest, ctx->level * INDENT_STEP);
895 ctx->first = bms_del_member(ctx->first, ctx->level);
897 ctx->valconverter = NULL;
899 ctx->valconverter = (p ? p->converter : NULL);
903 yaml_aestart(void *state, bool isnull)
905 pgspParserContext *ctx = (pgspParserContext *)state;
907 appendStringInfoString(ctx->dest, "\n");
908 bms_del_member(ctx->first, ctx->level);
909 appendStringInfoSpaces(ctx->dest, ctx->level * INDENT_STEP);
910 appendStringInfoString(ctx->dest, "- ");
914 yaml_scalar(void *state, char *token, JsonTokenType tokentype)
916 pgspParserContext *ctx = (pgspParserContext *)state;
920 appendStringInfoString(ctx->dest, ctx->fname);
921 appendStringInfoString(ctx->dest, ": ");
925 json_scalar(state, token, tokentype);
927 ctx->last_elem_is_object = false;
933 xml_objstart(void *state)
935 pgspParserContext *ctx = (pgspParserContext *)state;
938 ctx->first = bms_add_member(ctx->first, ctx->level);
943 xml_objend(void *state)
945 pgspParserContext *ctx = (pgspParserContext *)state;
946 appendStringInfoChar(ctx->dest, '\n');
947 appendStringInfoSpaces(ctx->dest, ctx->level * INDENT_STEP);
950 ctx->first = bms_del_member(ctx->first, ctx->level);
952 ctx->last_elem_is_object = true;
956 xml_arrend(void *state)
958 pgspParserContext *ctx = (pgspParserContext *)state;
960 appendStringInfoChar(ctx->dest, '\n');
961 appendStringInfoSpaces(ctx->dest, (ctx->level + 1) * INDENT_STEP);
965 adjust_wbuf(pgspParserContext *ctx, int len)
969 for (buflen = ctx->wbuflen ; len > buflen ; buflen *= 2);
970 if (buflen > ctx->wbuflen)
972 ctx->wbuf = (char *)palloc(buflen);
973 ctx->wbuflen = buflen;
978 hyphenate_words(pgspParserContext *ctx, char *src)
982 adjust_wbuf(ctx, strlen(src) + 1);
983 strcpy(ctx->wbuf, src);
985 for (p = ctx->wbuf ; *p ; p++)
986 if (*p == ' ') *p = '-';
992 xml_ofstart(void *state, char *fname, bool isnull)
995 pgspParserContext *ctx = (pgspParserContext *)state;
998 p = search_word_table(propfields, fname, ctx->mode);
1002 (errmsg("Short JSON parser encoutered unknown field name: \"%s\".", fname),
1003 errdetail_log("INPUT: \"%s\"", ctx->org_string)));
1005 s = (p ? p->longname : fname);
1008 * save current process context
1009 * There's no problem if P_Plan appears recursively.
1011 if (p && (p->tag == P_Plan || p->tag == P_Triggers))
1012 ctx->section = p->tag;
1014 appendStringInfoChar(ctx->dest, '\n');
1015 appendStringInfoSpaces(ctx->dest, (ctx->level + 1) * INDENT_STEP);
1017 ctx->valconverter = NULL;
1019 appendStringInfoChar(ctx->dest, '<');
1020 appendStringInfoString(ctx->dest, escape_xml(hyphenate_words(ctx, s)));
1021 appendStringInfoChar(ctx->dest, '>');
1022 ctx->valconverter = (p ? p->converter : NULL);
1025 * If the object field name is Plan or Triggers, the value should be an
1026 * array and the items are tagged by other than "Item". "Item"s appear
1027 * only in Output field.
1029 if (p && (p->tag == P_Plans || p->tag == P_Triggers))
1030 ctx->not_item = bms_add_member(ctx->not_item, ctx->level + 1);
1032 ctx->not_item = bms_del_member(ctx->not_item, ctx->level + 1);
1036 xml_ofend(void *state, char *fname, bool isnull)
1038 pgspParserContext *ctx = (pgspParserContext *)state;
1042 p = search_word_table(propfields, fname, ctx->mode);
1043 s = (p ? p->longname : fname);
1045 appendStringInfoString(ctx->dest, "</");
1046 appendStringInfoString(ctx->dest, escape_xml(hyphenate_words(ctx, s)));
1047 appendStringInfoChar(ctx->dest, '>');
1051 xml_aestart(void *state, bool isnull)
1053 pgspParserContext *ctx = (pgspParserContext *)state;
1057 * The "Trigger" in "Triggers", "Plan" in "Plans" and "Item" nodes are
1058 * implicitly represented in JSON format. Restore them for XML format.
1062 if (bms_is_member(ctx->level, ctx->not_item))
1064 if (ctx->section == P_Plan)
1072 appendStringInfoChar(ctx->dest, '\n');
1073 appendStringInfoSpaces(ctx->dest, (ctx->level + 1) * INDENT_STEP);
1074 appendStringInfoString(ctx->dest, tag);
1078 xml_aeend(void *state, bool isnull)
1080 pgspParserContext *ctx = (pgspParserContext *)state;
1084 * The "Plan" in "Plans" or "Item" nodes are implicitly represented in
1085 * JSON format. Restore it for XML format.
1088 if (bms_is_member(ctx->level, ctx->not_item))
1090 if (ctx->section == P_Plan)
1097 appendStringInfoString(ctx->dest, tag);
1102 xml_scalar(void *state, char *token, JsonTokenType tokentype)
1104 pgspParserContext *ctx = (pgspParserContext *)state;
1105 const char *s = token;
1107 if (ctx->valconverter)
1108 s = ctx->valconverter(token, PGSP_JSON_XMLIZE);
1110 if (tokentype == JSON_TOKEN_STRING)
1113 appendStringInfoString(ctx->dest, s);
1114 ctx->last_elem_is_object = false;
1117 /********************************/
1119 init_parser_context(pgspParserContext *ctx, int mode,
1120 char *orgstr, char *buf, int buflen){
1121 memset(ctx, 0, sizeof(*ctx));
1122 ctx->dest = makeStringInfo();
1124 ctx->org_string = orgstr;
1126 ctx->wbuflen = buflen;
1130 * run_pg_parse_json:
1132 * Wrap pg_parse_json in order to restore InterruptHoldoffCount when parse
1135 * Returns true when parse completed. False for unexpected end of string.
1138 run_pg_parse_json(JsonLexContext *lex, JsonSemAction *sem)
1140 MemoryContext ccxt = CurrentMemoryContext;
1141 uint32 saved_IntrHoldoffCount;
1144 * "ereport(ERROR.." occurs on error in pg_parse_json resets
1145 * InterruptHoldoffCount to zero, so we must save the value before calling
1146 * json parser to restore it on parse error. See errfinish().
1148 saved_IntrHoldoffCount = InterruptHoldoffCount;
1152 pg_parse_json(lex, sem);
1159 InterruptHoldoffCount = saved_IntrHoldoffCount;
1161 ecxt = MemoryContextSwitchTo(ccxt);
1162 errdata = CopyErrorData();
1164 if (errdata->sqlerrcode == ERRCODE_INVALID_TEXT_REPRESENTATION)
1171 MemoryContextSwitchTo(ecxt);
1181 init_json_lex_context(JsonLexContext *lex, char *json)
1183 lex->input = lex->token_terminator = lex->line_start = json;
1184 lex->line_number = 1;
1185 lex->input_length = strlen(json);
1186 lex->strval = makeStringInfo();
1190 init_json_semaction(JsonSemAction *sem, pgspParserContext *ctx)
1192 sem->semstate = (void*)ctx;
1193 sem->object_start = json_objstart;
1194 sem->object_end = json_objend;
1195 sem->array_start = json_arrstart;
1196 sem->array_end = json_arrend;
1197 sem->object_field_start = json_ofstart;
1198 sem->object_field_end = json_ofend;
1199 sem->array_element_start= json_aestart;
1200 sem->array_element_end = NULL;
1201 sem->scalar = json_scalar;
1205 pgsp_json_shorten(char *json)
1209 pgspParserContext ctx;
1211 init_json_lex_context(&lex, json);
1212 init_parser_context(&ctx, PGSP_JSON_SHORTEN, json, NULL, 0);
1213 init_json_semaction(&sem, &ctx);
1215 run_pg_parse_json(&lex, &sem);
1217 return ctx.dest->data;
1221 pgsp_json_normalize(char *json)
1225 pgspParserContext ctx;
1227 init_json_lex_context(&lex, json);
1228 init_parser_context(&ctx,PGSP_JSON_NORMALIZE, json, NULL, 0);
1229 init_json_semaction(&sem, &ctx);
1231 run_pg_parse_json(&lex, &sem);
1233 return ctx.dest->data;
1237 pgsp_json_inflate(char *json)
1241 pgspParserContext ctx;
1243 init_json_lex_context(&lex, json);
1244 init_parser_context(&ctx, PGSP_JSON_INFLATE, json, NULL, 0);
1245 init_json_semaction(&sem, &ctx);
1247 if (!run_pg_parse_json(&lex, &sem))
1249 if (ctx.dest->len > 0 &&
1250 ctx.dest->data[ctx.dest->len - 1] != '\n')
1251 appendStringInfoChar(ctx.dest, '\n');
1253 if (ctx.dest->len == 0)
1254 appendStringInfoString(ctx.dest, "<Input was not JSON>");
1256 appendStringInfoString(ctx.dest, "<truncated>");
1259 return ctx.dest->data;
1263 pgsp_json_yamlize(char *json)
1265 pgspParserContext ctx;
1269 init_json_lex_context(&lex, json);
1270 init_parser_context(&ctx, PGSP_JSON_YAMLIZE, json, NULL, 0);
1272 sem.semstate = (void*)&ctx;
1273 sem.object_start = yaml_objstart;
1274 sem.object_end = yaml_objend;
1275 sem.array_start = yaml_arrstart;
1276 sem.array_end = yaml_arrend;
1277 sem.object_field_start = yaml_ofstart;
1278 sem.object_field_end = NULL;
1279 sem.array_element_start= yaml_aestart;
1280 sem.array_element_end = NULL;
1281 sem.scalar = yaml_scalar;
1283 if (!run_pg_parse_json(&lex, &sem))
1285 if (ctx.dest->len > 0 &&
1286 ctx.dest->data[ctx.dest->len - 1] != '\n')
1287 appendStringInfoChar(ctx.dest, '\n');
1289 if (ctx.dest->len == 0)
1290 appendStringInfoString(ctx.dest, "<Input was not JSON>");
1292 appendStringInfoString(ctx.dest, "<truncated>");
1295 return ctx.dest->data;
1299 pgsp_json_xmlize(char *json)
1301 pgspParserContext ctx;
1307 init_json_lex_context(&lex, json);
1308 init_parser_context(&ctx, PGSP_JSON_XMLIZE, json, buf, sizeof(buf));
1310 sem.semstate = (void*)&ctx;
1311 sem.object_start = xml_objstart;
1312 sem.object_end = xml_objend;
1313 sem.array_start = NULL;
1314 sem.array_end = xml_arrend;
1315 sem.object_field_start = xml_ofstart;
1316 sem.object_field_end = xml_ofend;
1317 sem.array_element_start= xml_aestart;
1318 sem.array_element_end = xml_aeend;
1319 sem.scalar = xml_scalar;
1321 appendStringInfo(ctx.dest,
1322 "<explain xmlns=\"http://www.postgresql.org/2009/explain\">\n <Query>");
1323 start_len = ctx.dest->len;
1325 if (!run_pg_parse_json(&lex, &sem))
1327 if (ctx.dest->len > start_len &&
1328 ctx.dest->data[ctx.dest->len - 1] != '\n')
1329 appendStringInfoChar(ctx.dest, '\n');
1331 if (ctx.dest->len == start_len)
1333 resetStringInfo(ctx.dest);
1334 appendStringInfoString(ctx.dest, "<Input was not JSON>");
1337 appendStringInfoString(ctx.dest, "<truncated>");
1340 appendStringInfo(ctx.dest, "</Query>\n</explain>\n");
1342 return ctx.dest->data;