Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoinOptimizer.java
More file actions
553 lines (482 loc) · 20.2 KB
/
Copy pathJoinOptimizer.java
File metadata and controls
553 lines (482 loc) · 20.2 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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package simpledb;
import java.util.*;
import javax.swing.*;
import javax.swing.tree.*;
/**
* The JoinOptimizer class is responsible for ordering a series of joins
* optimally, and for selecting the best instantiation of a join for a given
* logical plan.
*/
public class JoinOptimizer {
LogicalPlan p;
Vector<LogicalJoinNode> joins;
/**
* Constructor
*
* @param p
* the logical plan being optimized
* @param joins
* the list of joins being performed
*/
public JoinOptimizer(LogicalPlan p, Vector<LogicalJoinNode> joins) {
this.p = p;
this.joins = joins;
}
/**
* Return best iterator for computing a given logical join, given the
* specified statistics, and the provided left and right subplans. Note that
* there is insufficient information to determine which plan should be the
* inner/outer here -- because DbIterator's don't provide any cardinality
* estimates, and stats only has information about the base tables. For this
* reason, the plan1
*
* @param lj
* The join being considered
* @param plan1
* The left join node's child
* @param plan2
* The right join node's child
*/
public static DbIterator instantiateJoin(LogicalJoinNode lj,
DbIterator plan1, DbIterator plan2) throws ParsingException {
int t1id = 0, t2id = 0;
DbIterator j;
try {
t1id = plan1.getTupleDesc().fieldNameToIndex(lj.f1QuantifiedName);
} catch (NoSuchElementException e) {
throw new ParsingException("Unknown field " + lj.f1QuantifiedName);
}
if (lj instanceof LogicalSubplanJoinNode) {
t2id = 0;
} else {
try {
t2id = plan2.getTupleDesc().fieldNameToIndex(
lj.f2QuantifiedName);
} catch (NoSuchElementException e) {
throw new ParsingException("Unknown field "
+ lj.f2QuantifiedName);
}
}
JoinPredicate p = new JoinPredicate(t1id, lj.p, t2id);
j = new Join(p,plan1,plan2);
return j;
}
/**
* Estimate the cost of a join.
*
* The cost of the join should be calculated based on the join algorithm (or
* algorithms) that you implemented for Lab 2. It should be a function of
* the amount of data that must be read over the course of the query, as
* well as the number of CPU opertions performed by your join. Assume that
* the cost of a single predicate application is roughly 1.
*
*
* @param j
* A LogicalJoinNode representing the join operation being
* performed.
* @param card1
* Estimated cardinality of the left-hand side of the query
* @param card2
* Estimated cardinality of the right-hand side of the query
* @param cost1
* Estimated cost of one full scan of the table on the left-hand
* side of the query
* @param cost2
* Estimated cost of one full scan of the table on the right-hand
* side of the query
* @return An estimate of the cost of this query, in terms of cost1 and
* cost2
*/
public double estimateJoinCost(LogicalJoinNode j, int card1, int card2,
double cost1, double cost2) {
if (j instanceof LogicalSubplanJoinNode) {
// A LogicalSubplanJoinNode represents a subquery.
// You do not need to implement proper support for these for Lab 5.
return card1 + cost1 + cost2;
} else {
// Insert your code here.
// HINT: You may need to use the variable "j" if you implemented
// a join algorithm that's more complicated than a basic
// nested-loops join.
return -1.0;
}
}
/**
* Estimate the cardinality of a join. The cardinality of a join is the
* number of tuples produced by the join.
*
* @param j
* A LogicalJoinNode representing the join operation being
* performed.
* @param card1
* Cardinality of the left-hand table in the join
* @param card2
* Cardinality of the right-hand table in the join
* @param t1pkey
* Is the left-hand table a primary-key table?
* @param t2pkey
* Is the right-hand table a primary-key table?
* @param stats
* The table stats, referenced by table names, not alias
* @return The cardinality of the join
*/
public int estimateJoinCardinality(LogicalJoinNode j, int card1, int card2,
boolean t1pkey, boolean t2pkey, Map<String, TableStats> stats) {
if (j instanceof LogicalSubplanJoinNode) {
// A LogicalSubplanJoinNode represents a subquery.
// You do not need to implement proper support for these for Lab 5.
return card1;
} else {
return estimateTableJoinCardinality(j.p, j.t1Alias, j.t2Alias,
j.f1PureName, j.f2PureName, card1, card2, t1pkey, t2pkey,
stats, p.getTableAliasToIdMapping());
}
}
/**
* Estimate the join cardinality of two tables.
* */
public static int estimateTableJoinCardinality(Predicate.Op joinOp,
String table1Alias, String table2Alias, String field1PureName,
String field2PureName, int card1, int card2, boolean t1pkey,
boolean t2pkey, Map<String, TableStats> stats,
Map<String, Integer> tableAliasToId) {
int card = 1;
// some code goes here
return card <= 0 ? 1 : card;
}
/**
* Helper method to enumerate all of the subsets of a given size of a
* specified vector.
*
* @param v
* The vector whose subsets are desired
* @param size
* The size of the subsets of interest
* @return a set of all subsets of the specified size
*/
@SuppressWarnings("unchecked")
public <T> Set<Set<T>> enumerateSubsets(Vector<T> v, int size) {
Set<Set<T>> els = new HashSet<Set<T>>();
els.add(new HashSet<T>());
// Iterator<Set> it;
// long start = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
Set<Set<T>> newels = new HashSet<Set<T>>();
for (Set<T> s : els) {
for (T t : v) {
Set<T> news = (Set<T>) (((HashSet<T>) s).clone());
if (news.add(t))
newels.add(news);
}
}
els = newels;
}
return els;
}
/**
* Compute a logical, reasonably efficient join on the specified tables. See
* PS4 for hints on how this should be implemented.
*
* @param stats
* Statistics for each table involved in the join, referenced by
* base table names, not alias
* @param filterSelectivities
* Selectivities of the filter predicates on each table in the
* join, referenced by table alias (if no alias, the base table
* name)
* @param explain
* Indicates whether your code should explain its query plan or
* simply execute it
* @return A Vector<LogicalJoinNode> that stores joins in the left-deep
* order in which they should be executed.
* @throws ParsingException
* when stats or filter selectivities is missing a table in the
* join, or or when another internal error occurs
*/
public Vector<LogicalJoinNode> orderJoins(
HashMap<String, TableStats> stats,
HashMap<String, Double> filterSelectivities, boolean explain)
throws ParsingException {
//Not necessary for labs 1--3
// some code goes here
//Replace the following
return joins;
}
// ===================== Private Methods =================================
/**
* This is a helper method that computes the cost and cardinality of joining
* joinToRemove to joinSet (joinSet should contain joinToRemove), given that
* all of the subsets of size joinSet.size() - 1 have already been computed
* and stored in PlanCache pc.
*
* @param stats
* table stats for all of the tables, referenced by table names
* rather than alias (see {@link #orderJoins})
* @param filterSelectivities
* the selectivities of the filters over each of the tables
* (where tables are indentified by their alias or name if no
* alias is given)
* @param joinToRemove
* the join to remove from joinSet
* @param joinSet
* the set of joins being considered
* @param bestCostSoFar
* the best way to join joinSet so far (minimum of previous
* invocations of computeCostAndCardOfSubplan for this joinSet,
* from returned CostCard)
* @param pc
* the PlanCache for this join; should have subplans for all
* plans of size joinSet.size()-1
* @return A {@link CostCard} objects desribing the cost, cardinality,
* optimal subplan
* @throws ParsingException
* when stats, filterSelectivities, or pc object is missing
* tables involved in join
*/
@SuppressWarnings("unchecked")
private CostCard computeCostAndCardOfSubplan(
HashMap<String, TableStats> stats,
HashMap<String, Double> filterSelectivities,
LogicalJoinNode joinToRemove, Set<LogicalJoinNode> joinSet,
double bestCostSoFar, PlanCache pc) throws ParsingException {
LogicalJoinNode j = joinToRemove;
Vector<LogicalJoinNode> prevBest;
if (this.p.getTableId(j.t1Alias) == null)
throw new ParsingException("Unknown table " + j.t1Alias);
if (this.p.getTableId(j.t2Alias) == null)
throw new ParsingException("Unknown table " + j.t2Alias);
String table1Name = Database.getCatalog().getTableName(
this.p.getTableId(j.t1Alias));
String table2Name = Database.getCatalog().getTableName(
this.p.getTableId(j.t2Alias));
String table1Alias = j.t1Alias;
String table2Alias = j.t2Alias;
Set<LogicalJoinNode> news = (Set<LogicalJoinNode>) ((HashSet<LogicalJoinNode>) joinSet)
.clone();
news.remove(j);
double t1cost, t2cost;
int t1card, t2card;
boolean leftPkey, rightPkey;
if (news.isEmpty()) { // base case -- both are base relations
prevBest = new Vector<LogicalJoinNode>();
t1cost = stats.get(table1Name).estimateScanCost();
t1card = stats.get(table1Name).estimateTableCardinality(
filterSelectivities.get(j.t1Alias));
leftPkey = isPkey(j.t1Alias, j.f1PureName);
t2cost = table2Alias == null ? 0 : stats.get(table2Name)
.estimateScanCost();
t2card = table2Alias == null ? 0 : stats.get(table2Name)
.estimateTableCardinality(
filterSelectivities.get(j.t2Alias));
rightPkey = table2Alias == null ? false : isPkey(table2Alias,
j.f2PureName);
} else {
// news is not empty -- figure best way to join j to news
prevBest = pc.getOrder(news);
// possible that we have not cached an answer, if subset
// includes a cross product
if (prevBest == null) {
return null;
}
double prevBestCost = pc.getCost(news);
int bestCard = pc.getCard(news);
// estimate cost of right subtree
if (doesJoin(prevBest, table1Alias)) { // j.t1 is in prevBest
t1cost = prevBestCost; // left side just has cost of whatever
// left
// subtree is
t1card = bestCard;
leftPkey = hasPkey(prevBest);
t2cost = j.t2Alias == null ? 0 : stats.get(table2Name)
.estimateScanCost();
t2card = j.t2Alias == null ? 0 : stats.get(table2Name)
.estimateTableCardinality(
filterSelectivities.get(j.t2Alias));
rightPkey = j.t2Alias == null ? false : isPkey(j.t2Alias,
j.f2PureName);
} else if (doesJoin(prevBest, j.t2Alias)) { // j.t2 is in prevbest
// (both
// shouldn't be)
t2cost = prevBestCost; // left side just has cost of whatever
// left
// subtree is
t2card = bestCard;
rightPkey = hasPkey(prevBest);
t1cost = stats.get(table1Name).estimateScanCost();
t1card = stats.get(table1Name).estimateTableCardinality(
filterSelectivities.get(j.t1Alias));
leftPkey = isPkey(j.t1Alias, j.f1PureName);
} else {
// don't consider this plan if one of j.t1 or j.t2
// isn't a table joined in prevBest (cross product)
return null;
}
}
// case where prevbest is left
double cost1 = estimateJoinCost(j, t1card, t2card, t1cost, t2cost);
LogicalJoinNode j2 = j.swapInnerOuter();
double cost2 = estimateJoinCost(j2, t2card, t1card, t2cost, t1cost);
if (cost2 < cost1) {
boolean tmp;
j = j2;
cost1 = cost2;
tmp = rightPkey;
rightPkey = leftPkey;
leftPkey = tmp;
}
if (cost1 >= bestCostSoFar)
return null;
CostCard cc = new CostCard();
cc.card = estimateJoinCardinality(j, t1card, t2card, leftPkey,
rightPkey, stats);
cc.cost = cost1;
cc.plan = (Vector<LogicalJoinNode>) prevBest.clone();
cc.plan.addElement(j); // prevbest is left -- add new join to end
return cc;
}
/**
* Return true if the specified table is in the list of joins, false
* otherwise
*/
private boolean doesJoin(Vector<LogicalJoinNode> joinlist, String table) {
for (LogicalJoinNode j : joinlist) {
if (j.t1Alias.equals(table)
|| (j.t2Alias != null && j.t2Alias.equals(table)))
return true;
}
return false;
}
/**
* Return true if field is a primary key of the specified table, false
* otherwise
*
* @param tableAlias
* The alias of the table in the query
* @param field
* The pure name of the field
*/
private boolean isPkey(String tableAlias, String field) {
int tid1 = p.getTableId(tableAlias);
String pkey1 = Database.getCatalog().getPrimaryKey(tid1);
return pkey1.equals(field);
}
/**
* Return true if a primary key field is joined by one of the joins in
* joinlist
*/
private boolean hasPkey(Vector<LogicalJoinNode> joinlist) {
for (LogicalJoinNode j : joinlist) {
if (isPkey(j.t1Alias, j.f1PureName)
|| (j.t2Alias != null && isPkey(j.t2Alias, j.f2PureName)))
return true;
}
return false;
}
/**
* Helper function to display a Swing window with a tree representation of
* the specified list of joins. See {@link #orderJoins}, which may want to
* call this when the analyze flag is true.
*
* @param js
* the join plan to visualize
* @param pc
* the PlanCache accumulated whild building the optimal plan
* @param stats
* table statistics for base tables
* @param selectivities
* the selectivities of the filters over each of the tables
* (where tables are indentified by their alias or name if no
* alias is given)
*/
private void printJoins(Vector<LogicalJoinNode> js, PlanCache pc,
HashMap<String, TableStats> stats,
HashMap<String, Double> selectivities) {
JFrame f = new JFrame("Join Plan for " + p.getQuery());
// Set the default close operation for the window,
// or else the program won't exit when clicking close button
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
f.setVisible(true);
f.setSize(300, 500);
HashMap<String, DefaultMutableTreeNode> m = new HashMap<String, DefaultMutableTreeNode>();
// int numTabs = 0;
// int k;
DefaultMutableTreeNode root = null, treetop = null;
HashSet<LogicalJoinNode> pathSoFar = new HashSet<LogicalJoinNode>();
boolean neither;
System.out.println(js);
for (LogicalJoinNode j : js) {
pathSoFar.add(j);
System.out.println("PATH SO FAR = " + pathSoFar);
String table1Name = Database.getCatalog().getTableName(
this.p.getTableId(j.t1Alias));
String table2Name = Database.getCatalog().getTableName(
this.p.getTableId(j.t2Alias));
// Double c = pc.getCost(pathSoFar);
neither = true;
root = new DefaultMutableTreeNode("Join " + j + " (Cost ="
+ pc.getCost(pathSoFar) + ", card = "
+ pc.getCard(pathSoFar) + ")");
DefaultMutableTreeNode n = m.get(j.t1Alias);
if (n == null) { // never seen this table before
n = new DefaultMutableTreeNode(j.t1Alias
+ " (Cost = "
+ stats.get(table1Name).estimateScanCost()
+ ", card = "
+ stats.get(table1Name).estimateTableCardinality(
selectivities.get(j.t1Alias)) + ")");
root.add(n);
} else {
// make left child root n
root.add(n);
neither = false;
}
m.put(j.t1Alias, root);
n = m.get(j.t2Alias);
if (n == null) { // never seen this table before
n = new DefaultMutableTreeNode(
j.t2Alias == null ? "Subplan"
: (j.t2Alias
+ " (Cost = "
+ stats.get(table2Name)
.estimateScanCost()
+ ", card = "
+ stats.get(table2Name)
.estimateTableCardinality(
selectivities
.get(j.t2Alias)) + ")"));
root.add(n);
} else {
// make right child root n
root.add(n);
neither = false;
}
m.put(j.t2Alias, root);
// unless this table doesn't join with other tables,
// all tables are accessed from root
if (!neither) {
for (String key : m.keySet()) {
m.put(key, root);
}
}
treetop = root;
}
JTree tree = new JTree(treetop);
JScrollPane treeView = new JScrollPane(tree);
tree.setShowsRootHandles(true);
// Set the icon for leaf nodes.
ImageIcon leafIcon = new ImageIcon("join.jpg");
DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
renderer.setOpenIcon(leafIcon);
renderer.setClosedIcon(leafIcon);
tree.setCellRenderer(renderer);
f.setSize(300, 500);
f.add(treeView);
for (int i = 0; i < tree.getRowCount(); i++) {
tree.expandRow(i);
}
if (js.size() == 0) {
f.add(new JLabel("No joins in plan."));
}
f.pack();
}
}
You can’t perform that action at this time.
