-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutionEngine.py
More file actions
189 lines (176 loc) · 8.01 KB
/
executionEngine.py
File metadata and controls
189 lines (176 loc) · 8.01 KB
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
import math
from typing import Any
import physicalQueryTools
import index
def execute(physPlan: dict[str, Any], indexes: dict[str, Any], relations: dict[str, Any]) -> tuple[int, list[Any], list[str]]:
# Scan Operation
if (physPlan.get("op") == "scan"):
schema = [f"{physPlan.get('relation')}.{attr}" for attr in relations.get("relations").get(physPlan.get("relation")).get("schema")]
io = 0
outputTuples = []
for page in relations.get("relations").get(physPlan.get("relation")).get("pages"):
# read in the page.
io = io + 1
for tuple in page:
# get each tuple
outputTuples.append(tuple)
return io, outputTuples, schema
# Index Scan Operation
elif (physPlan.get("op") == "indexScan"):
schema = [f"{physPlan.get('relation')}.{attr}" for attr in relations.get("relations").get(physPlan.get("relation")).get("schema")]
# parsing predicate
predicate = physPlan.get("predicate")
subPredicates = []
currentPredicate = []
for token in predicate:
if token == "AND":
subPredicates.append(currentPredicate)
currentPredicate.clear()
else:
currentPredicate.append(token)
if (len(currentPredicate) == 3):
subPredicates.append(currentPredicate)
# find Index
indexToUse = None
predicateWithIndex = None
for subPredicate in subPredicates:
atributesInSubPredicate = set(subPredicate).intersection(physicalQueryTools.allAttributes)
indexesInSubpredicate = atributesInSubPredicate.intersection(set(indexes.keys()))
if (len(indexesInSubpredicate) > 0):
indexToUse = iter(indexesInSubpredicate).__next__()
predicateWithIndex: list[str] = subPredicate
break
searchKey = predicateWithIndex.copy()
searchKey.remove(indexToUse)
searchKey.remove("=")
searchKey = searchKey[0]
# get tuples matching the key
io, tuples = index.indexLookup(indexToUse, searchKey, subPredicates, relations.get("relations").get(indexToUse.split(".")[0]))
return io, tuples, schema
elif (physPlan.get("op") == "filter"):
subtotalIO, tuples, schema = execute(physPlan.get("child"), indexes, relations)
predicate = physPlan.get("predicate")
survivingTuples = []
# getSubPredicates
subPredicates = []
currentPredicate = []
for token in predicate:
if token == "AND":
subPredicates.append(currentPredicate)
currentPredicate.clear()
else:
currentPredicate.append(token)
if (len(currentPredicate) == 3):
subPredicates.append(currentPredicate)
# filter the tuples by all predicates.
for t in tuples:
predicateFail = False
for predicate in subPredicates:
lhs = predicate[0]
rhs = predicate[2]
# if lhs is an attr get the attr
if (lhs in physicalQueryTools.allAttributes):
lhs = lhs.split(".")
lhs = t[relations.get("relations").get(lhs[0]).get("schema").index(lhs[1])]
# if rhs is an attr get the attr
if (rhs in physicalQueryTools.allAttributes):
rhs = rhs.split(".")
rhs = t[relations.get("relations").get(rhs[0]).get("schema").index(rhs[1])]
# predicate fails and tuple is not added.
if (lhs != rhs):
predicateFail = True
break
if (not predicateFail):
# io = io + 1
survivingTuples.append(t)
# io = math.ceil(io/2.0)
return subtotalIO, survivingTuples, schema
elif (physPlan.get("op") == "project"):
subtotalIO, tuples, schema = execute(physPlan.get("child"), indexes, relations)
projAttrs = physPlan.get("attrs")
keepTupleIndexes = []
newSchema = []
# find the indexes to keep
for attr in schema:
if (attr in projAttrs):
keepTupleIndexes.append(schema.index(attr))
newSchema.append(attr)
# construct new tuple.
newTuples = []
for t in tuples:
newT = []
for i in keepTupleIndexes:
newT.append(t[i])
newTuples.append(newT)
# convert to pages
return subtotalIO, newTuples, newSchema
elif (physPlan.get("op") == "hashedIndexJoin"):
subtotalIOL, tuplesL, schemaL = execute(physPlan.get("left"), indexes, relations)
io = subtotalIOL
# subtotalIOR, tuplesR, schemaR = execute(physPlan.get("right"), indexes, relations)
condition = physPlan.get("condition")
conditionIndexL = schemaL.index(condition[0])
indexToUse = condition[2]
schemaR = [x.strip("'") for x in relations.get("relations").get(indexToUse.split(".")[0]).get("schema")]
conditionIndexR = schemaR.index(condition[2].split(".")[1])
schemaR.pop(conditionIndexR)
newSchema = schemaL+schemaR
# IO cost is B(R)+T(R)*lookupCost
newTuples = []
pagesAccessed = set()
for tL in tuplesL:
lookupIO, tuplesR = index.indexLookup(indexToUse, tL[conditionIndexL], [], relations.get("relations").get(indexToUse.split(".")[0]), checkPredicates=False, pagesAccessed=pagesAccessed)
io = io + lookupIO
for tR in tuplesR:
# if (tL[conditionIndexL] == tR[conditionIndexR]):
newTuple = tL+tR[:conditionIndexR] + tR[conditionIndexR+1:]
newTuples.append(newTuple)
return io, newTuples, newSchema
elif (physPlan.get("op") == "blockNestedLoopJoin"):
io = 0
subtotalIOL, tuplesL, schemaL = execute(physPlan.get("left"), indexes, relations)
subtotalIOR, tuplesR, schemaR = execute(physPlan.get("right"), indexes, relations)
condition = physPlan.get("condition")
conditionIndexL = schemaL.index(condition[0])
conditionIndexR = schemaR.index(condition[2])
schemaR.pop(conditionIndexR)
newSchema = schemaL+schemaR
# IO cost is B(R)+B(R)*B(S)
pageSize = relations.get("page_size")
# io = math.ceil(len(tuplesL)/pageSize) + math.ceil(len(tuplesL)/pageSize)*math.ceil(len(tuplesR)/pageSize)
io = subtotalIOL + len(tuplesL)*subtotalIOR
newTuples = []
for tL in tuplesL:
for tR in tuplesR:
if (tL[conditionIndexL] == tR[conditionIndexR]):
newTuple = tL+tR[:conditionIndexR] + tR[conditionIndexR+1:]
newTuples.append(newTuple)
# return subtotalIOL+subtotalIOR+io, newTuples, newSchema
return io, newTuples, newSchema
elif (physPlan.get("op") == "hashJoin"):
subtotalIOL, tuplesL, schemaL = execute(physPlan.get("left"), indexes, relations)
subtotalIOR, tuplesR, schemaR = execute(physPlan.get("right"), indexes, relations)
condition = physPlan.get("condition")
conditionIndexL = schemaL.index(condition[0])
conditionIndexR = schemaR.index(condition[2])
schemaR.pop(conditionIndexR)
newSchema = schemaL+schemaR
# IO B(R) + B(S)
io = subtotalIOR + subtotalIOL
# Build phase
hashTable = {}
for tL in tuplesL:
key = tL[conditionIndexL]
if (hashTable.get(key) != None):
hashTable[key].append(tL)
else:
hashTable[key] = [tL]
# Probe Phase
newTuples = []
for tR in tuplesR:
key = tR[conditionIndexR]
if (hashTable.get(key) != None):
for tL in hashTable.get(key):
newTuple = tL+tR[:conditionIndexR] + tR[conditionIndexR+1:]
newTuples.append(newTuple)
return io, newTuples, newSchema