Post

将mysql查询语句转化为查询树

任选下列查询语句中的三条,将其转化为对应的查询执行树,并且根据设计的查询优化算法,对生成的查询执行树进行优化。              
SELECT [ ENAME = ’Mary’ & DNAME = ’Research’ ] ( EMPLOYEE JOIN DEPARTMENT )                  
PROJECTION [ BDATE ] ( SELECT [ ENAME = ’John’ & DNAME = ’ Research’ ] ( EMPLOYEE JOIN DEPARTMENT) )                   
SELECT [ ESSN = ’01’ ] (  PROJECTION [ ESSN, PNAME ] ( WORKS_ON JOIN PROJECT ) )                  
PROJECTION [ ENAME ] ( SELECT [ SALARY < 3000 ] ( EMPLOYEE JOIN SELECT [ PNO = ’P1’ ] ( WORKS_ON JOIN PROJECT ) )                  
PROJECTION [ DNAME, SALARY ] ( AVG [ SALARY ] ( SELECT [ DNAME = ’ Research’ ] ( EMPLOYEE  JOIN  DEPART MENT) )                 
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
from pptree import *


def parser(sentence):
    '''
    将输入的数据库查询序列解析成表tables的list,选择条件selects的list,投影projections的list
    假设以上三个list中的元素按list的角标对应
    以及绘制查询树并优化
    :param sentence:sql查询语句
    :return: tables,selects,projections 三个list组成的list
    return example:
    [['EMPLOYEE', 'DEPARTMENT'], [['ENAME', '=', 'Mary'], ['DNAME', '=', 'ENAME']], []]
    '''
    temp = sentence.replace("[", "").replace("]", "").replace("(", "").replace(")", "").replace("", "").replace(",",
                                                                                                                 "").split(
        " ")
    temp_0 = []
    for i in range(len(temp)):
        if temp[i] != "":
            temp_0.append(temp[i])
    tables = []
    for i in range(len(temp_0)):
        if temp_0[i] == "JOIN":
            tables.append(temp_0[i - 1])
            tables.append(temp_0[i + 1])
    temp_1 = temp_0[0: i - 2]
    # sequence = 1 表示select在projection之前,为0则相反,为-1表示没有projection
    sequence = -2
    selects = []
    projections = []
    if not temp_1.__contains__("PROJECTION"):
        sequence = -1
    and_flag = 0
    for i in range(len(temp_1)):
        if (temp_1[i] == "&"):
            selects.append([temp_1[i - 3], temp_1[i - 2], temp_1[i - 1]])
            selects.append([temp_1[i + 1], temp_1[i + 2], temp_1[i - 3]])
            and_flag = 1
    if and_flag == 0:
        for i in range(len(temp_1)):
            if (temp_1[i] == "SELECT"):
                selects.append([temp_1[i + 1], temp_1[i + 2], temp_1[i + 3]])
    for i in range(len(temp_1)):
        if (temp_1[i] == "PROJECTION"):
            if i + 1 <= len(temp_1):
                if temp_1[i + 2] == "SELECT":
                    projections.append(temp_1[i + 1])
                    sequence = 0
                else:
                    projections.append(temp_1[i + 1])
                    projections.append(temp_1[i + 2])
                    sequence = 1
    print("lexer       : " + str(temp_0))
    print("tables      : " + str(tables))
    print("selects     : " + str(selects))
    print("projections : " + str(projections))
    # print(sequence)

    amend = 1
    if sequence == -1:
        root_1 = "SELECT " + "".join(selects[0])
        if len(selects) == 2:
            root_1 = root_1 + " " + "".join(selects[1])
        paint_root = Node(root_1)
        paint_join = Node("JOIN", paint_root)
        for i in range(len(tables)):
            Node(str(tables[i]), paint_join)
        print("原始查询树 :")
        print_tree(paint_root, horizontal=False)

        children = []
        paint_root = Node("JOIN")
        for i in range(len(selects)):
            children.append(Node("SELECT " + "".join(selects[i]), paint_root))
            Node(str(tables[i]), children[i])
        print("改进查询树(%d) :" % (amend))
        amend = amend + 1
        print_tree(paint_root, horizontal=False)

    if sequence == 0:
        root_1 = "PROJECTION " + "".join(projections[0])
        if len(projections) == 2:
            root_1 = root_1 + " " + "".join(selects[1])
        paint_root = Node(root_1)
        select_1 = "SELECT " + "".join(selects[0])
        if len(selects) == 2:
            select_1 = select_1 + " " + "".join(selects[1])
        paint_select = Node(select_1, paint_root)
        paint_join = Node("JOIN", paint_select)
        for i in range(len(tables)):
            Node(str(tables[i]), paint_join)
        print("原始查询树 :")
        print_tree(paint_root, horizontal=False)

        root_1 = "PROJECTION " + "".join(projections[0])
        if len(projections) == 2:
            root_1 = root_1 + " " + "".join(selects[1])
        paint_root = Node(root_1)
        select_1 = "SELECT " + "".join(selects[0])
        paint_select_1 = Node(select_1, paint_root)
        if len(selects) == 2:
            select_2 = "SELECT " + " " + "".join(selects[1])
            paint_select_2 = Node(select_2, paint_select_1)
            paint_join = Node("JOIN", paint_select_2)
            for i in range(len(tables)):
                Node(str(tables[i]), paint_join)
        else:
            paint_join = Node("JOIN", paint_select_1)
            for i in range(len(tables)):
                Node(str(tables[i]), paint_join)
        print("改进查询树(%d) :" % (amend))
        amend = amend + 1
        print_tree(paint_root, horizontal=False)

        root_1 = "PROJECTION " + "".join(projections[0])
        if len(projections) == 2:
            root_1 = root_1 + " " + "".join(selects[1])
        paint_root = Node(root_1)
        paint_join = Node("JOIN", paint_root)
        join_children = []
        for i in range(len(selects)):
            join_children.append(Node("SELECT " + "".join(selects[i]), paint_join))
            Node(str(tables[i]), join_children[i])
        print("改进查询树(%d) :" % (amend))
        amend = amend + 1
        print_tree(paint_root, horizontal=False)

        root_1 = "PROJECTION " + "".join(projections[0])
        if len(projections) == 2:
            root_1 = root_1 + " " + "".join(selects[1])
        paint_root = Node(root_1)
        paint_join = Node("JOIN", paint_root)
        join_children = []
        join_children.append(Node(root_1 + " DNO", paint_join))
        Node(str(tables[0]), Node("SELECT " + "".join(selects[0]), join_children[0]))
        join_children.append(Node("PROJECTION DNO", paint_join))
        Node(str(tables[1]), Node("SELECT " + "".join(selects[1]), join_children[1]))
        print("改进查询树(%d) :" % (amend))
        print("假设BDATE为EMPLOYEE的属性,假设DNO为EMPLOYEE和DEPARTMENT的共同属性,代表部门编号")
        amend = amend + 1
        print_tree(paint_root, horizontal=False)

    if sequence == 1:
        root_1 = "SELECT " + "".join(selects[0])
        if len(selects) == 2:
            root_1 = root_1 + " " + "".join(selects[1])
        paint_root = Node(root_1)
        str_projections = "PROJECTION " + "".join(projections[0])
        if len(projections) == 2:
            str_projections = str_projections + " " + "".join(projections[1])
        paint_projection = Node(str_projections, paint_root)
        paint_join = Node("JOIN", paint_projection)
        for i in range(len(tables)):
            Node(str(tables[i]), paint_join)
        print("原始查询树 :")
        print_tree(paint_root, horizontal=False)

        str_projections = "PROJECTION " + "".join(projections[0])
        if len(projections) == 2:
            str_projections = str_projections + " " + "".join(projections[1])
        paint_root = Node(str_projections)
        str_select = "SELECT " + "".join(selects[0])
        if len(selects) == 2:
            str_select = str_select + " " + "".join(selects[1])
        paint_select = Node(str_select, paint_root)
        paint_join = Node("JOIN", paint_select)
        for i in range(len(tables)):
            Node(str(tables[i]), paint_join)
        print("改进查询树(%d) :" % (amend))
        amend = amend + 1
        print_tree(paint_root, horizontal=False)

        str_projections = "PROJECTION " + "".join(projections[0])
        if len(projections) == 2:
            str_projections = str_projections + " " + "".join(projections[1])
        paint_root = Node(str_projections)
        paint_join = Node("JOIN", paint_root)
        str_select = "SELECT " + "".join(selects[0])
        if len(selects) == 2:
            str_select = str_select + " " + "".join(selects[1])
        Node(str(tables[0]), Node(str_select, paint_join))
        Node(str(tables[1]), paint_join)
        print("改进查询树(%d) :" % (amend))
        print("假设ESSN是WORKS_ON的属性")
        amend = amend + 1
        print_tree(paint_root, horizontal=False)

        str_projections = "PROJECTION " + "".join(projections[0])
        if len(projections) == 2:
            str_projections = str_projections + " " + "".join(projections[1])
        paint_root = Node(str_projections)
        paint_join = Node("JOIN", paint_root)
        str_select = "SELECT " + "".join(selects[0])
        paint_projection = []
        for i in range(len(projections)):
            paint_projection.append(Node("PROJECTION " + str(projections[i]) + " PNO ", paint_join))
        Node(str(tables[0]), Node(str_select, paint_projection[0]))
        Node(str(tables[1]), paint_projection[1])
        print("改进查询树(%d) :" % (amend))
        print("假设ESSN是WORKS_ON的属性,假设PNO为WORKS_ON和PROJECT的共同属性")
        amend = amend + 1
        print_tree(paint_root, horizontal=False)
    print("")
    return [tables, selects, projections]


if __name__ == '__main__':
    assignments = []
    assignments.append("SELECT [ ENAME = ’Mary’ & DNAME = ’Research’ ] ( EMPLOYEE JOIN DEPARTMENT )")
    # 假设BDATE为 EMPLOYEE的属性,假设DNO为EMPLOYEE和DEPARTMENT的共同属性,代表部门编号
    assignments.append(
        "PROJECTION [ BDATE ] ( SELECT [ ENAME = ’John’ & DNAME = ’ Research’ ] ( EMPLOYEE JOIN DEPARTMENT) )")
    # 假设ESSN是WORKS_ON的属性,假设PNO为WORKS_ON和PROJECT的共同属性
    assignments.append("SELECT [ ESSN = ’01’ ] (  PROJECTION [ ESSN, PNAME ] ( WORKS_ON JOIN PROJECT ) )")
    for i in range(3):
        parser(assignments[i])

This post is licensed under CC BY 4.0 by the author.