Package edu.caltech.nanodb.queryeval
Class CostBasedJoinPlanner
- java.lang.Object
-
- edu.caltech.nanodb.queryeval.CostBasedJoinPlanner
-
- All Implemented Interfaces:
Planner
public class CostBasedJoinPlanner extends java.lang.Object implements Planner
This planner implementation uses dynamic programming to devise an optimal join strategy for the query. As always, queries are optimized in units of SELECT-FROM-WHERE subqueries; optimizations don't currently span multiple subqueries.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
CostBasedJoinPlanner.JoinComponent
This helper class is used to keep track of one "join component" in the dynamic programming algorithm.
-
Field Summary
Fields Modifier and Type Field Description private static org.apache.logging.log4j.Logger
logger
A logging object for reporting anything interesting that happens.protected StorageManager
storageManager
The storage manager used during query planning.
-
Constructor Summary
Constructors Constructor Description CostBasedJoinPlanner()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
collectDetails(FromClause fromClause, java.util.HashSet<Expression> conjuncts, java.util.ArrayList<FromClause> leafFromClauses)
This helper method pulls the essential details for join optimization out of a FROM clause.private java.util.ArrayList<CostBasedJoinPlanner.JoinComponent>
generateLeafJoinComponents(java.util.Collection<FromClause> leafFromClauses, java.util.Collection<Expression> conjuncts)
This helper method performs the first step of the dynamic programming process to generate an optimal join plan, by generating a plan for every leaf from-clause identified from analyzing the query.private CostBasedJoinPlanner.JoinComponent
generateOptimalJoin(java.util.ArrayList<CostBasedJoinPlanner.JoinComponent> leafComponents, java.util.Set<Expression> conjuncts)
This helper method builds up a full join-plan using a dynamic programming approach.private CostBasedJoinPlanner.JoinComponent
makeJoinPlan(FromClause fromClause, java.util.Collection<Expression> extraConjuncts)
Given the top-levelFromClause
for a SELECT-FROM-WHERE block, this helper generates an optimal join plan for theFromClause
.private PlanNode
makeLeafPlan(FromClause fromClause, java.util.Collection<Expression> conjuncts, java.util.HashSet<Expression> leafConjuncts)
Constructs a plan tree for evaluating the specified from-clause.PlanNode
makePlan(SelectClause selClause, java.util.List<SelectClause> enclosingSelects)
Returns the root of a plan tree suitable for executing the specified query.SelectNode
makeSimpleSelect(java.lang.String tableName, Expression predicate, java.util.List<SelectClause> enclosingSelects)
Constructs a simple select plan that reads directly from a table, with an optional predicate for selecting rows.void
setStorageManager(StorageManager storageManager)
Sets the server to be used during query planning.
-
-
-
Field Detail
-
logger
private static org.apache.logging.log4j.Logger logger
A logging object for reporting anything interesting that happens.
-
storageManager
protected StorageManager storageManager
The storage manager used during query planning.
-
-
Method Detail
-
setStorageManager
public void setStorageManager(StorageManager storageManager)
Sets the server to be used during query planning.- Specified by:
setStorageManager
in interfacePlanner
- Parameters:
storageManager
- the storage manager instance for the planner implementation to use
-
makePlan
public PlanNode makePlan(SelectClause selClause, java.util.List<SelectClause> enclosingSelects)
Returns the root of a plan tree suitable for executing the specified query.- Specified by:
makePlan
in interfacePlanner
- Parameters:
selClause
- an object describing the query to be performedenclosingSelects
- a list of enclosing queries, ifselClause
is a nested subquery. This is allowed to be an empty list, ornull
, if the query is a top-level query. IfselClause
is a nested subquery,enclosingSelects[0]
is the outermost enclosing query, thenenclosingSelects[1]
is enclosed byenclosingSelects[0]
, and so forth. The most tightly enclosing query is the last one inenclosingSelects
.- Returns:
- a plan tree for executing the specified query
-
makeJoinPlan
private CostBasedJoinPlanner.JoinComponent makeJoinPlan(FromClause fromClause, java.util.Collection<Expression> extraConjuncts)
Given the top-levelFromClause
for a SELECT-FROM-WHERE block, this helper generates an optimal join plan for theFromClause
.- Parameters:
fromClause
- the top-levelFromClause
of a SELECT-FROM-WHERE block.extraConjuncts
- any extra conjuncts (e.g. from the WHERE clause, or HAVING clause)- Returns:
- a
JoinComponent
object that represents the optimal plan corresponding to the FROM-clause
-
collectDetails
private void collectDetails(FromClause fromClause, java.util.HashSet<Expression> conjuncts, java.util.ArrayList<FromClause> leafFromClauses)
This helper method pulls the essential details for join optimization out of a FROM clause. TODO: FILL IN DETAILS.- Parameters:
fromClause
- the from-clause to collect details fromconjuncts
- the collection to add all conjuncts toleafFromClauses
- the collection to add all leaf from-clauses to
-
generateLeafJoinComponents
private java.util.ArrayList<CostBasedJoinPlanner.JoinComponent> generateLeafJoinComponents(java.util.Collection<FromClause> leafFromClauses, java.util.Collection<Expression> conjuncts)
This helper method performs the first step of the dynamic programming process to generate an optimal join plan, by generating a plan for every leaf from-clause identified from analyzing the query. Leaf plans are usually very simple; they are built either from base-tables or SELECT subqueries. The most complex detail is that any conjuncts in the query that can be evaluated solely against a particular leaf plan-node will be associated with the plan node. This is a heuristic that usually produces good plans (and certainly will for the current state of the database), but could easily interfere with indexes or other plan optimizations.- Parameters:
leafFromClauses
- the collection of from-clauses found in the queryconjuncts
- the collection of conjuncts that can be applied at this level- Returns:
- a collection of
CostBasedJoinPlanner.JoinComponent
object containing the plans and other details for each leaf from-clause
-
makeLeafPlan
private PlanNode makeLeafPlan(FromClause fromClause, java.util.Collection<Expression> conjuncts, java.util.HashSet<Expression> leafConjuncts)
Constructs a plan tree for evaluating the specified from-clause. TODO: COMPLETE THE DOCUMENTATION- Parameters:
fromClause
- the select nodes that need to be joined.conjuncts
- additional conjuncts that can be applied when constructing the from-clause plan.leafConjuncts
- this is an output-parameter. Any conjuncts applied in this plan from the conjuncts collection should be added to this out-param.- Returns:
- a plan tree for evaluating the specified from-clause
- Throws:
java.lang.IllegalArgumentException
- if the specified from-clause is a join expression that isn't an outer join, or has some other unrecognized type.
-
generateOptimalJoin
private CostBasedJoinPlanner.JoinComponent generateOptimalJoin(java.util.ArrayList<CostBasedJoinPlanner.JoinComponent> leafComponents, java.util.Set<Expression> conjuncts)
This helper method builds up a full join-plan using a dynamic programming approach. The implementation maintains a collection of optimal intermediate plans that join n of the leaf nodes, each with its own associated cost, and then uses that collection to generate a new collection of optimal intermediate plans that join n+1 of the leaf nodes. This process completes when all leaf plans are joined together; there will be one plan, and it will be the optimal join plan (as far as our limited estimates can determine, anyway).- Parameters:
leafComponents
- the collection of leaf join-components, generated by thegenerateLeafJoinComponents(java.util.Collection<edu.caltech.nanodb.queryast.FromClause>, java.util.Collection<edu.caltech.nanodb.expressions.Expression>)
method.conjuncts
- the collection of all conjuncts found in the query- Returns:
- a single
CostBasedJoinPlanner.JoinComponent
object that joins all leaf components together in an optimal way.
-
makeSimpleSelect
public SelectNode makeSimpleSelect(java.lang.String tableName, Expression predicate, java.util.List<SelectClause> enclosingSelects)
Constructs a simple select plan that reads directly from a table, with an optional predicate for selecting rows.While this method can be used for building up larger SELECT queries, the returned plan is also suitable for use in UPDATE and DELETE command evaluation. In these cases, the plan must only generate tuples of type
PageTuple
, so that the command can modify or delete the actual tuple in the file's page data.- Specified by:
makeSimpleSelect
in interfacePlanner
- Parameters:
tableName
- The name of the table that is being selected from.predicate
- An optional selection predicate, ornull
if no filtering is desired.enclosingSelects
- a list of enclosing queries, ifselClause
is a nested subquery. This is allowed to be an empty list, ornull
, if the query is a top-level query. IfselClause
is a nested subquery,enclosingSelects[0]
is the outermost enclosing query, thenenclosingSelects[1]
is enclosed byenclosingSelects[0]
, and so forth. The most tightly enclosing query is the last one inenclosingSelects
.- Returns:
- A new plan-node for evaluating the select operation.
-
-