-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
69 lines (57 loc) · 2.35 KB
/
main.py
File metadata and controls
69 lines (57 loc) · 2.35 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
import argparse
import json
from math import ceil
import queryTools
import physicalQueryTools
import index
import executionEngine
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--r", required=False, default="./inputs/relations.json", help="path to relations json.")
parser.add_argument("--s", required=False, default="./inputs/statistics.json", help="path to statistics json.")
parser.add_argument("--q", required=False, default="./queries/Q1.json", help="path to query json.")
args = parser.parse_args()
print(f"\nrelations from {args.r}")
print(f"statistics from {args.s}")
print(f"query from {args.q}")
# load data
relations = loadFile(args.r)
statistics = loadFile(args.s)
logicalPlan = loadFile(args.q)
print("\nORIGINAL LOGICAL PLAN")
print(json.dumps(logicalPlan, indent=4))
# create indexes
indexes = index.loadIndexes(relations)
# Query Rewrite
logicalPlan = queryTools.rewriteLogicalPlan(logicalPlan, relations, statistics)
# Get Cardonality and Join Order
cardinality, finalLogicalPlan = queryTools.getCardinality(logicalPlan, statistics)
print("\nREWRITTEN LOGICAL PLAN")
print(json.dumps(finalLogicalPlan, indent=4))
print(f"\nFINAL ESTIMATED CARDINALITY: {ceil(cardinality)}")
# Physical Plans and Estimates
physPlans = physicalQueryTools.createPhysicalPlans(finalLogicalPlan, indexes, statistics)
ioEstimates = physicalQueryTools.estimatePhysPlans(physPlans, statistics)
print("\nPHYSICAL PLANS")
print(f"Number of plans: {len(physPlans)}")
for num, physPlan in enumerate(physPlans):
print(f"\nPhysical plan: {num+1}")
print(f"Estimated cost: {ioEstimates[num]}")
print(json.dumps(physPlan, indent=4))
# Choose Physical Plan
indexOfLowest = min(enumerate(ioEstimates), key=lambda x: x[1])[0]
print(f"\nCHOSEN PHYSICAL PLAN: plan number {indexOfLowest+1}")
# Execute Physical Plan
ioCost, outputTuples, schema = executionEngine.execute(physPlans[indexOfLowest], indexes, relations)
print(f"\nACTUAL IO COST: {ioCost}")
print(f"\nOUTPUT TUPLES")
print(f"schema: {schema}")
for tuple in outputTuples:
print(tuple)
def loadFile(directory: str):
file = open(directory)
result = json.load(file)
file.close()
return result
if (__name__ == "__main__"):
main()