-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonToText.py
434 lines (377 loc) · 16.6 KB
/
JsonToText.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import json
import argparse
import SwitchCase
import queue
import copy
#import copy
#from __future__ import print_function
#import loggingqueue.Queue
# from gtts import gTTS
#import random, string
#import pygame
#import os
#from nlp import handler
#initialize Switcher
s = SwitchCase.Switcher()
join_list = []
scan_list = []
class Node(object):
def __init__(self, node_type, relation_name, schema, alias, group_key, sort_key, join_type, index_name,
hash_cond, table_filter, index_cond, merge_cond, recheck_cond, join_filter, subplan_name, actual_rows,
actual_time):
self.node_type = node_type
self.children = []
self.relation_name = relation_name
self.schema = schema
self.alias = alias
self.group_key = group_key
self.sort_key = sort_key
self.join_type = join_type
self.index_name = index_name
self.hash_cond = hash_cond
self.table_filter = table_filter
self.index_cond = index_cond
self.merge_cond = merge_cond
self.recheck_cond = recheck_cond
self.join_filter = join_filter
self.subplan_name = subplan_name
self.actual_rows = actual_rows
self.actual_time = actual_time
def add_children(self, child):
self.children.append(child)
def set_output_name(self, output_name):
if "T" == output_name[0] and output_name[1:].isdigit():
self.output_name = int(output_name[1:])
else:
self.output_name = output_name
# print(output_name)
def get_output_name(self):
if str(self.output_name).isdigit():
return "T" + str(self.output_name)
else:
return self.output_name
def set_step(self, step):
self.step = step
# Phase 1
def parse_json(json_file):
q = queue.Queue()
q_node = queue.Queue()
print("Reading from : ", json_file)
json_obj = json.load(open(json_file, 'r'))
#retrieve json as dict type, plan
plan = json_obj[0][0][0]['Plan']
#print("Plan: ", plan)
global qep
qep = None
qep = plan
q.put(plan)
q_node.put(None)
while not q.empty(): #when q is not empty
# print(current_plan)
# print(q.get())
current_plan = q.get()
parent_node = q_node.get()
#print("Current_plan: ", current_plan)
relation_name = schema = alias = group_key = sort_key = join_type = index_name = hash_cond = table_filter \
= index_cond = merge_cond = recheck_cond = join_filter = subplan_name = actual_rows = actual_time = None
if 'Relation Name' in current_plan:
relation_name = current_plan['Relation Name']
if 'Schema' in current_plan:
schema = current_plan['Schema']
if 'Alias' in current_plan:
alias = current_plan['Alias']
if 'Group Key' in current_plan:
group_key = current_plan['Group Key']
if 'Sort Key' in current_plan:
sort_key = current_plan['Sort Key']
if 'Join Type' in current_plan:
join_type = current_plan['Join Type']
if 'Index Name' in current_plan:
index_name = current_plan['Index Name']
if 'Hash Cond' in current_plan:
hash_cond = current_plan['Hash Cond']
if 'Filter' in current_plan:
table_filter = current_plan['Filter']
if 'Index Cond' in current_plan:
index_cond = current_plan['Index Cond']
if 'Merge Cond' in current_plan:
merge_cond = current_plan['Merge Cond']
if 'Recheck Cond' in current_plan:
recheck_cond = current_plan['Recheck Cond']
if 'Join Filter' in current_plan:
join_filter = current_plan['Join Filter']
if 'Actual Rows' in current_plan:
actual_rows = current_plan['Actual Rows']
if 'Actual Total Time' in current_plan:
actual_time = current_plan['Actual Total Time']
if 'Subplan Name' in current_plan:
if "returns" in current_plan['Subplan Name']:
name = current_plan['Subplan Name']
subplan_name = name[name.index("$"):-1]
else:
subplan_name = current_plan['Subplan Name']
#create a node from each node of tree from dictionary
current_node = Node(current_plan['Node Type'], relation_name, schema, alias, group_key, sort_key, join_type,
index_name, hash_cond, table_filter, index_cond, merge_cond, recheck_cond, join_filter,
subplan_name, actual_rows, actual_time)
if "Limit" == current_node.node_type:
current_node.plan_rows = current_plan['Plan Rows']
#find out which table is being scanned, passed the name to to Node.output_name
if "Scan" in current_node.node_type:
# print(current_node.node_type, ' --> ')
# print(current_plan['Node Type'])
if current_node.node_type != 'Bitmap Index Scan':
scan_list.append(current_plan)
if "Index" in current_node.node_type:
if relation_name:
current_node.set_output_name(relation_name + " with index on " + index_name)
elif "Subquery" in current_node.node_type:
current_node.set_output_name(alias)
else:
current_node.set_output_name(relation_name)
if parent_node is not None: #if current_node is not the root
parent_node.add_children(current_node)
else:
head_node = current_node
if 'Plans' in current_plan:
# print('current_plan[\'Plans\']-->', current_plan['Plans'])
# print(current_node.node_type, ' --> ')
# print(len(current_plan['Plans']))
if len(current_plan['Plans']) == 2:
leftSub = (current_plan['Plans'][0])
rightSub = (current_plan['Plans'][1])
join_list.append([current_plan, {'leftTable':leftSub, 'rightTable':rightSub}])
#print(join_list)
for item in current_plan['Plans']:
# push child plans into queue
#print("item --> ", item)
q.put(item)
# push parent for each child into queue
q_node.put(current_node)
return head_node
# Phase 2 (not in use)
# def simplify_graph(node):
# new_node = copy.deepcopy(node)
# new_node.children = []
#
# for child in node.children:
# new_child = simplify_graph(child)
# new_node.add_children(new_child)
# print(new_node.actual_time, ", ", child.actual_time)
# new_node.actual_time -= child.actual_time
#
# if node.node_type in ["Result"]:
# return node.children[0]
#
# return new_node
# Phase 3
def to_text(node, skip=False):
global steps, cur_step, cur_table_name
increment = True
# skip the child if merge it with current node
if node.node_type in ["Unique", "Aggregate"] and len(node.children) == 1 \
and ("Scan" in node.children[0].node_type or node.children[0].node_type == "Sort"):
children_skip = True
elif node.node_type == "Bitmap Heap Scan" and node.children[0].node_type == "Bitmap Index Scan":
children_skip = True
else:
children_skip = False
# recursive
for child in node.children:
if node.node_type == "Aggregate" and len(node.children) > 1 and child.node_type == "Sort":
to_text(child, True)
else:
to_text(child, children_skip)
if node.node_type in ["Hash"] or skip:
return
step = ""
# generate natural language for various QEP operators
if "Join" in node.node_type:
# special preprocessing for joins
if node.join_type == "Semi":
# add the word "Semi" before "Join" into node.node_type
node_type_list = node.node_type.split()
node_type_list.insert(-1, node.join_type)
node.node_type = " ".join(node_type_list)
else:
pass
if "Hash" in node.node_type:
step += " and perform " + node.node_type.lower() + " on "
for i, child in enumerate(node.children):
if child.node_type == "Hash":
child.set_output_name(child.children[0].get_output_name())
hashed_table = child.get_output_name()
if i < len(node.children) - 1:
step += ("table " + child.get_output_name())
else:
step += (" and table " + child.get_output_name())
# combine hash with hash join
step = "hash table " + hashed_table + step + " under condition " + parse_cond("Hash Cond", node.hash_cond, table_subquery_name_pair)
elif "Merge" in node.node_type:
step += "perform " + node.node_type.lower() + " on "
any_sort = False # whether sort is performed on any table
for i, child in enumerate(node.children):
if child.node_type == "Sort":
child.set_output_name(child.children[0].get_output_name())
any_sort = True
if i < len(node.children) - 1:
step += ("table " + child.get_output_name())
else:
step += (" and table " + child.get_output_name())
# combine sort with merge join
if any_sort:
sort_step = "sort "
for child in node.children:
if child.node_type == "Sort":
if i < len(node.children) - 1:
sort_step += ("table " + child.get_output_name())
else:
sort_step += (" and table " + child.get_output_name())
step = sort_step + " and " + step
elif node.node_type == "Bitmap Heap Scan":
# combine bitmap heap scan and bitmap index scan
if "Bitmap Index Scan" in node.children[0].node_type:
node.children[0].set_output_name(node.relation_name)
step = " with index condition " + parse_cond("Recheck Cond", node.recheck_cond, table_subquery_name_pair)
step = "perform bitmap heap scan on table " + node.children[0].get_output_name() + step
elif "Scan" in node.node_type:
if node.node_type == "Seq Scan":
step += "perform sequential scan on table "
else:
step += "perform " + node.node_type.lower() + " on table "
step += node.get_output_name()
# if no table filter, remain original table name
if not node.table_filter:
increment = False
elif node.node_type == "Unique":
# combine unique and sort
if "Sort" in node.children[0].node_type:
node.children[0].set_output_name(node.children[0].children[0].get_output_name())
step = "sort " + node.children[0].get_output_name()
if node.children[0].sort_key:
step += " with attribute " + parse_cond("Sort Key", node.children[0].sort_key, table_subquery_name_pair) + " and "
else:
step += " and "
step += "perform unique on table " + node.children[0].get_output_name()
elif node.node_type == "Aggregate":
for child in node.children:
# combine aggregate and sort
if "Sort" in child.node_type:
child.set_output_name(child.children[0].get_output_name())
step = "sort " + child.get_output_name() + " and "
# combine aggregate with scan
if "Scan" in child.node_type:
if child.node_type == "Seq Scan":
step = "perform sequential scan on " + child.get_output_name() + " and "
else:
step = "perform " + child.node_type.lower() + " on " + child.get_output_name() + " and "
step += "perform aggregate on table " + node.children[0].get_output_name()
if len(node.children) == 2:
step += " and table " + node.children[1].get_output_name()
elif node.node_type == "Sort":
step += "perform sort on table " + node.children[0].get_output_name() + " with attribute " + parse_cond("Sort Key", node.sort_key, table_subquery_name_pair)
elif node.node_type == "Limit":
step += "limit the result from table " + node.children[0].get_output_name() + " to " + str(node.plan_rows) + " record(s)"
else:
step += "perform " + node.node_type.lower() + " on"
# binary operator
if len(node.children) > 1:
for i, child in enumerate(node.children):
print(i, child.node_type)
if i < len(node.children) - 1:
step += (" table " + child.get_output_name()) # + ","
else:
step += (" and table " + child.get_output_name())
# unary operator
else:
step += " table " + node.children[0].get_output_name()
print(node.relation_name)
#print(node.children,node.children[0].get_output_name())
# add conditions
if node.group_key:
#print('node.group_key --> ', node.table_filter, '\ntable_subquery_name_pair --> ', table_subquery_name_pair)
step += " with grouping on attribute " + parse_cond("Group Key", node.group_key, table_subquery_name_pair)
if node.table_filter:
#print('node.table_filter --> ', node.table_filter, '\ntable_subquery_name_pair --> ', table_subquery_name_pair)
step += " and filtering on " + parse_cond("Table Filter", node.table_filter, table_subquery_name_pair)
if node.join_filter:
#print('node.join_filter --> ', node.table_filter, '\ntable_subquery_name_pair --> ', table_subquery_name_pair)
step += " while filtering on " + parse_cond("Join Filter", node.join_filter, table_subquery_name_pair)
# set intermediate (temporary) table name
if increment:
node.set_output_name("T" + str(cur_table_name))
step += " to get intermediate table " + node.get_output_name()
cur_table_name += 1
if node.subplan_name:
table_subquery_name_pair[node.subplan_name] = node.get_output_name()
step = "Step " + str(cur_step) + ", " + step + "."
node.set_step(cur_step)
cur_step += 1
steps.append(step)
# not in use
# def random_word(length):
# letters = string.ascii_lowercase
# return ''.join(random.choice(letters) for _ in range(length))
# Phase 4 (not in use)
# def vocalize(steps):
# logger = logging.getLogger("neuron.vocalizer.vocalize")
# txt = ""
# for step in steps:
# # pronounce the dot sign if it's not period
# step = step.replace(".", " dot ")
# txt += step[:-5] + ". "
#
# random_name = random_word(10) + '.mp3'
# random_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), random_name)
#
# tts = gTTS(text=txt, lang='en')
# logger.debug("Obtained TTS result from Google")
# with open(random_file, 'wb') as f:
# tts.save(f.name)
#
# pygame.mixer.init()
# pygame.mixer.music.load(f.name)
# pygame.mixer.music.play()
def get_text(json_file):
global steps, cur_step, cur_table_name, table_subquery_name_pair
global current_plan_tree
steps = ["The query is executed as follow."]
cur_step = 1
cur_table_name = 1
table_subquery_name_pair = {}
head_node = parse_json(json_file)
#print(head_node)
# handler.current_plan_tree = simplified_graph = simplify_graph(head_node)
#
# to_text(simplified_graph)
to_text(head_node)
if " to get intermediate table" in steps[-1]:
steps[-1] = steps[-1][:steps[-1].index(" to get intermediate table")] + ' to get the final result.'
return steps
def parse_cond(type, qry, tblSubQry):
#still missing on tblSubQry
print(tblSubQry)
joinFilter = s.case(type, qry)
return joinFilter
parser = argparse.ArgumentParser()
parser.add_argument('--json_file', type=str, default='json.txt', help='the json generated file for vocalization')
args = parser.parse_args()
def PlanToText():
steps = get_text(args.json_file)
#steps is in list format, for each step
a = copy.deepcopy(join_list)
b = copy.deepcopy(scan_list)
c = copy.deepcopy(qep)
join_list.clear()
scan_list.clear()
qep.clear()
return steps, c, a, b
# if __name__ == '__main__':
# parser = argparse.ArgumentParser()
# parser.add_argument('--json_file', type=str, default='json.txt', help='the json generated file for vocalization')
# args = parser.parse_args()
# steps = get_text(args.json_file)
# print('scan_list -->', scan_list)
# print('join_list -->', join_list)
# #print(steps)
# # vocalize(steps)