OSDN Git Service

Added test for unknown JSON field names.
[pgstoreplans/pg_store_plans.git] / json2sql.pl
1 #! /usr/bin/perl
2
3 print <<'EOS';
4 \echo ###### Prepare for the test
5 \set QUIET
6 SET client_min_messages = 'error';
7 CREATE EXTENSION IF NOT EXISTS pg_store_plans;
8 DROP TABLE IF EXISTS plans;
9 CREATE TABLE plans (id int, title text, lplan text, splan text);
10 SET client_min_messages = 'notice';
11 \set ECHO none
12
13 \echo ###### insert original JSON plans
14 INSERT INTO plans (VALUES
15 EOS
16
17 $plan_no = 0;
18 $title = "###### Plan $plan_no: all properties";
19 setplan0();
20 print "($plan_no, \'$title\',\n";
21 print " $escape'$plan')";
22
23 $plan_no++;
24 $state = 0;
25 while(<>) {
26         chomp;
27         if ($state == 0) {
28                 next if (!/^###### (.*$)/);
29                 $title = "###### Plan $plan_no: $1";
30                 $state = 1;
31         } elsif ($state == 1) {
32                 if (/[}\]:,]/) {
33                         die("??? : $_");
34                 }
35                 next if (!/^   { *\+$/);
36                 $plan = $_;
37                 $plan =~ s/^   (.*[^ ]) *\+$/$1\n/;
38                 $state = 2;
39         } elsif ($state == 2) {
40                 if (/^   } *\+$/) {
41                         $state = 3;
42                 }
43                 $l = $_;
44                 $l =~ s/^   (.*[^ ]) *\+$/$1/;
45                 $plan .= "$l";
46                 $plan .= "\n" if ($state == 2);
47         } elsif ($state == 3) {
48                 $escape = "";
49                 if ($plan =~ /'/ || $plan =~ /\\\"/) {
50                         $escape = "E";
51                 }
52                 # Add escape char for '''
53                 $plan =~ s/'/\\'/g;
54                 # Add escape char for '\"'
55                 $plan =~ s/\\\"/\\\\\"/g;
56
57                 # Remove "Total Runtime" line.
58                 $plan =~ s/,\n *"Total Runtime":.*\n/\n/;
59                 
60                 print ",\n($plan_no, \'$title\',\n";
61                 print " $escape'$plan')";
62                 $plan_no++;
63                 $state = 0;
64         }
65 }
66
67 print <<'EOS';
68 );
69
70 \pset pager
71 \set ECHO queries
72
73 \echo ###### set shortened JSON
74 UPDATE plans SET splan = pg_store_plans_shorten(lplan);
75
76 \echo ###### JSON properties round-trip test
77 SELECT id FROM plans
78         where pg_store_plans_jsonplan(splan) <> lplan;
79
80 \pset format unaligned
81 \pset tuples_only on
82 \pset recordsep '\n\n=======\n'
83 \echo  ###### format conversion tests
84 SELECT '### '||'yaml-short       '||title||E'\n'||
85   pg_store_plans_yamlplan(splan)
86   FROM plans WHERE id BETWEEN 1 AND 3 or id = 1 ORDER BY id;
87 \echo  ################## 
88 SELECT '### '||'xml-short        '||title||E'\n'||
89   pg_store_plans_xmlplan(splan)
90   FROM plans WHERE id BETWEEN 4 AND 6 or id = 1 ORDER BY id;
91
92 \echo  ###### text format output test
93 SELECT '### '||'TEXT-short       '||title||E'\n'||
94   pg_store_plans_textplan(splan)
95   FROM plans ORDER BY id;
96
97 \echo  ###### long-json-as-a-source test
98 SELECT '### '||'yaml-long JSON   '||title||E'\n'||
99   pg_store_plans_yamlplan(lplan)
100   FROM plans WHERE id = 1 ORDER BY id;
101 \echo  ################## 
102 SELECT '### '||'xml-long JSON    '||title||E'\n'||
103   pg_store_plans_xmlplan(lplan)
104   FROM plans WHERE id = 1 ORDER BY id;
105 \echo  ################## 
106 SELECT '### '||'text-long JSON   '||title||E'\n'||
107   pg_store_plans_xmlplan(lplan)
108   FROM plans WHERE id = 1 ORDER BY id;
109
110 \echo  ###### chopped-source test
111 SELECT '### '||'inflate-chopped  '||title||E'\n'||
112   pg_store_plans_jsonplan(substring(splan from 1 for char_length(splan) / 3))
113   FROM plans WHERE id BETWEEN 16 AND 18 ORDER BY id;
114 \echo  ################## 
115 SELECT '### '||'yaml-chopped     '||title||E'\n'||
116   pg_store_plans_yamlplan(substring(splan from 1 for char_length(splan) / 3))
117   FROM plans WHERE id BETWEEN 19 AND 21 ORDER BY id;
118 \echo  ################## 
119 SELECT '### '||'xml-chopped      '||title||E'\n'||
120   pg_store_plans_xmlplan(substring(splan from 1 for char_length(splan) / 3))
121   FROM plans WHERE id BETWEEN 22 AND 24 ORDER BY id;
122 \echo  ################## 
123 SELECT '### '||'text-chopped     '||title||E'\n'||
124   pg_store_plans_textplan(substring(splan from 1 for char_length(splan) / 3))
125   FROM plans WHERE id BETWEEN 25 AND 27 ORDER BY id;
126
127 \echo ###### shorten test
128 SELECT '### '||'shorten          '||title||E'\n'||
129   pg_store_plans_shorten(lplan)
130   FROM plans WHERE id = 0 ORDER BY id;
131 \echo ###### normalize test
132 SELECT '### '||'normalize        '||title||E'\n'||
133   pg_store_plans_normalize(lplan)
134   FROM plans WHERE id BETWEEN 1 AND 3 ORDER BY id;
135
136 EOS
137
138 sub setplan0 {
139         $plan = << 'EOS';
140 {
141   "Plan": 0,
142   "Plans": 0,
143   "Node Type": "Result",
144   "Node Type": "ModifyTable",
145   "Node Type": "Append",
146   "Node Type": "Merge Append",
147   "Node Type": "Recursive Union",
148   "Node Type": "BitmapAnd",
149   "Node Type": "BitmapOr",
150   "Node Type": "Seq Scan",
151   "Node Type": "Index Scan",
152   "Node Type": "Index Only Scan",
153   "Node Type": "Bitmap Index Scan",
154   "Node Type": "Bitmap Heap Scan",
155   "Node Type": "Tid Scan",
156   "Node Type": "Subquery Scan",
157   "Node Type": "Function Scan",
158   "Node Type": "Values Scan",
159   "Node Type": "CTE Scan",
160   "Node Type": "Workable Scan",
161   "Node Type": "Foreign Scan",
162   "Node Type": "Nested Loop",
163   "Node Type": "Merge Join",
164   "Node Type": "Hash Join",
165   "Node Type": "Materialize",
166   "Node Type": "Sort",
167   "Node Type": "Group",
168   "Node Type": "Aggregate",
169   "Node Type": "WindowAgg",
170   "Node Type": "Unique",
171   "Node Type": "Hash",
172   "Node Type": "SetOp",
173   "Node Type": "LockRows",
174   "Node Type": "Limit",
175   "Parent Relationship": "Outer",
176   "Parent Relationship": "Inner",
177   "Parent Relationship": "Subquery",
178   "Parent Relationship": "Member",
179   "Parent Relationship": "InitPlan",
180   "Parent Relationship": "SubPlan",
181   "Scan Direction": "Backward",
182   "Scan Direction": "NoMovement",
183   "Scan Direction": "Forward",
184   "Index Name": 0,
185   "Relation Name": 0,
186   "Function Name": 0,
187   "CTE Name": 0,
188   "Schema": 0,
189   "Alias": 0,
190   "Output": "[]",
191   "Merge Cond": "a",
192   "Strategy": "Plain",
193   "Strategy": "Sorted",
194   "Strategy": "Hashed",
195   "Join Type": "Inner",
196   "Join Type": "Left",
197   "Join Type": "Full",
198   "Join Type": "Right",
199   "Join Type": "Semi",
200   "Join Type": "Anti",
201   "Command": "Intersect",
202   "Command": "Intersect All",
203   "Command": "Except",
204   "Command": "Except All",
205   "Sort Method": "top-N heapsort",
206   "Sort Method": "quicksort",
207   "Sort Method": "external sort",
208   "Sort Method": "external merge",
209   "Sort Method": "still in progress",
210   "Sort Key": "a",
211   "Filter": "a",
212   "Join Filter": "a",
213   "Hash Cond": "a",
214   "Index Cond": "a",
215   "TID Cond": "a",
216   "Recheck Cond": "a",
217   "Operation": "Insert",
218   "Operation": "Delete",
219   "Operation": "Update",
220   "Subplan Name": "a",
221   "Triggers": 0,
222   "Trigger": 0,
223   "Trigger Name": 0,
224   "Relation": 0,
225   "Constraint Name": 0,
226   "Function Call": 0,
227   "Startup Cost": 0,
228   "Total Cost": 0,
229   "Plan Rows": 0,
230   "Plan Width": 0,
231   "Actual Startup Time": 0,
232   "Actual Total Time": 0,
233   "Actual Rows": 0,
234   "Actual Loops": 0,
235   "Heap Fetches": 0,
236   "Shared Hit Blocks": 0,
237   "Shared Read Blocks": 0,
238   "Shared Dirtied Blocks": 0,
239   "Shared Written Blocks": 0,
240   "Local Hit Blocks": 0,
241   "Local Read Blocks": 0,
242   "Local Dirtied Blocks": 0,
243   "Local Written Blocks": 0,
244   "Temp Read Blocks": 0,
245   "Temp Written Blocks": 0,
246   "I/O Read Time": 0,
247   "I/O Write Time": 0,
248   "Sort Space Used": 0,
249   "Sort Space Type": "Disk",
250   "Sort Space Type": "Memory",
251   "Peak Memory Usage": 0,
252   "Original Hash Batches": 0,
253   "Hash Batches": 0,
254   "Hash Buckets": 0,
255   "Rows Removed by Filter": 0,
256   "Rows Removed by Index Recheck": 0,
257   "Time": 0,
258   "Calls": 0,
259   "Unknown Key": "Unknown Value"
260 }
261 EOS
262         chop $plan;
263 }
264