public class LogicalPlan
extends java.lang.Object
A LogicalPlan consits of a collection of table scan nodes, join nodes, filter nodes, a select list, and a group by field. LogicalPlans can only represent queries with one aggregation field and one group by field.
LogicalPlans can be converted to physical (optimized) plans using
the physicalPlan(simpledb.TransactionId, java.util.Map<java.lang.String, simpledb.TableStats>, boolean)
method, which uses the
JoinOptimizer
to order joins optimally and to select the
best implementations for joins.
Modifier and Type | Field and Description |
---|---|
private java.lang.String |
aggField |
private java.lang.String |
aggOp |
private java.util.Vector<LogicalFilterNode> |
filters |
private java.lang.String |
groupByField |
private boolean |
hasAgg |
private boolean |
hasOrderBy |
private java.util.Vector<LogicalJoinNode> |
joins |
private boolean |
oByAsc |
private java.lang.String |
oByField |
private java.lang.String |
query |
private java.util.Vector<LogicalSelectListNode> |
selectList |
private java.util.HashMap<java.lang.String,DbIterator> |
subplanMap |
private java.util.HashMap<java.lang.String,java.lang.Integer> |
tableMap |
private java.util.Vector<LogicalScanNode> |
tables |
Constructor and Description |
---|
LogicalPlan()
Constructor -- generate an empty logical plan
|
Modifier and Type | Method and Description |
---|---|
void |
addAggregate(java.lang.String op,
java.lang.String afield,
java.lang.String gfield)
Add an aggregate over the field with the specified grouping to
the query.
|
void |
addFilter(java.lang.String field,
Predicate.Op p,
java.lang.String constantValue)
Add a new filter to the logical plan
|
void |
addJoin(java.lang.String joinField1,
DbIterator joinField2,
Predicate.Op pred)
Add a join between a field and a subquery.
|
void |
addJoin(java.lang.String joinField1,
java.lang.String joinField2,
Predicate.Op pred)
Add a join between two fields of two different tables.
|
void |
addOrderBy(java.lang.String field,
boolean asc)
Add an ORDER BY expression in the specified order on the specified field.
|
void |
addProjectField(java.lang.String fname,
java.lang.String aggOp)
Add a specified field/aggregate combination to the select list of the query.
|
void |
addScan(int table,
java.lang.String name)
Add a scan to the plan.
|
(package private) java.lang.String |
disambiguateName(java.lang.String name)
Given a name of a field, try to figure out what table it belongs to by looking
through all of the tables added via
addScan(int, java.lang.String) . |
(package private) static Aggregator.Op |
getAggOp(java.lang.String s)
Convert the aggregate operator name s into an Aggregator.op operation.
|
java.lang.String |
getQuery()
Get the query text associated with this plan via
setQuery(java.lang.String) . |
java.util.HashMap<java.lang.String,java.lang.Integer> |
getTableAliasToIdMapping() |
java.lang.Integer |
getTableId(java.lang.String alias)
Given a table alias, return id of the table object (this id can be supplied to
Catalog.getDbFile(int) ). |
static void |
main(java.lang.String[] argv) |
DbIterator |
physicalPlan(TransactionId t,
java.util.Map<java.lang.String,TableStats> baseTableStats,
boolean explain)
Convert this LogicalPlan into a physicalPlan represented by a
DbIterator . |
void |
setQuery(java.lang.String query)
Set the text of the query representing this logical plan.
|
private java.util.Vector<LogicalJoinNode> joins
private java.util.Vector<LogicalScanNode> tables
private java.util.Vector<LogicalFilterNode> filters
private java.util.HashMap<java.lang.String,DbIterator> subplanMap
private java.util.HashMap<java.lang.String,java.lang.Integer> tableMap
private java.util.Vector<LogicalSelectListNode> selectList
private java.lang.String groupByField
private boolean hasAgg
private java.lang.String aggOp
private java.lang.String aggField
private boolean oByAsc
private boolean hasOrderBy
private java.lang.String oByField
private java.lang.String query
public void setQuery(java.lang.String query)
query
- the text of the query associated with this planpublic java.lang.String getQuery()
setQuery(java.lang.String)
.public java.lang.Integer getTableId(java.lang.String alias)
Catalog.getDbFile(int)
).
Aliases are added as base tables are added via addScan(int, java.lang.String)
.alias
- the table alias to return a table id forpublic java.util.HashMap<java.lang.String,java.lang.Integer> getTableAliasToIdMapping()
public void addFilter(java.lang.String field, Predicate.Op p, java.lang.String constantValue) throws ParsingException
field
- The name of the over which the filter applies;
this can be a fully qualified field (tablename.field or
alias.field), or can be a unique field name without a
tablename qualifier. If it is an ambiguous name, it will
throw a ParsingExceptionp
- The predicate for the filterconstantValue
- the constant to compare the predicate
against; if field is an integer field, this should be a
String representing an integerParsingException
- if field is not in one of the tables
added via addScan(int, java.lang.String)
or if field is ambiguous (e.g., two
tables contain a field named field.)public void addJoin(java.lang.String joinField1, java.lang.String joinField2, Predicate.Op pred) throws ParsingException
joinField1
- The name of the first join field; this can
be a fully qualified name (e.g., tableName.field or
alias.field) or may be an unqualified unique field name. If
the name is ambiguous or unknown, a ParsingException will be
thrown.joinField2
- The name of the second join fieldpred
- The join predicateParsingException
- if either of the fields is ambiguous,
or is not in one of the tables added via addScan(int, java.lang.String)
public void addJoin(java.lang.String joinField1, DbIterator joinField2, Predicate.Op pred) throws ParsingException
joinField1
- The name of the first join field; this can
be a fully qualified name (e.g., tableName.field or
alias.field) or may be an unqualified unique field name. If
the name is ambiguous or unknown, a ParsingException will be
thrown.joinField2
- the subquery to join with -- the join field
of the subquery is the first field in the result set of the querypred
- The join predicate.ParsingException
- if either of the fields is ambiguous,
or is not in one of the tables added via addScan(int, java.lang.String)
public void addScan(int table, java.lang.String name)
table
- the id of the table accessed by the plan (can be resolved to a DbFile using Catalog.getDbFile(int)
name
- the alias of the table in the planpublic void addProjectField(java.lang.String fname, java.lang.String aggOp) throws ParsingException
fname
- the field to add to the outputaggOp
- the aggregate operation over the field.ParsingException
public void addAggregate(java.lang.String op, java.lang.String afield, java.lang.String gfield) throws ParsingException
op
- the aggregation operatorafield
- the field to aggregate overgfield
- the field to group byParsingException
public void addOrderBy(java.lang.String field, boolean asc) throws ParsingException
field
- the field to order byasc
- true if should be ordered in ascending order, false for descending orderParsingException
java.lang.String disambiguateName(java.lang.String name) throws ParsingException
addScan(int, java.lang.String)
.ParsingException
- if the field cannot be found in any of the tables, or if the
field is ambiguous (appears in multiple tables)static Aggregator.Op getAggOp(java.lang.String s) throws ParsingException
ParsingException
- if s is not a valid operator namepublic DbIterator physicalPlan(TransactionId t, java.util.Map<java.lang.String,TableStats> baseTableStats, boolean explain) throws ParsingException
DbIterator
. Attempts to
find the optimal plan by using JoinOptimizer.orderJoins(java.util.HashMap<java.lang.String, simpledb.TableStats>, java.util.HashMap<java.lang.String, java.lang.Double>, boolean)
to order the joins in the plan.t
- The transaction that the returned DbIterator will run as a part ofbaseTableStats
- a HashMap providing a TableStats
object for each table used in the LogicalPlan. This should
have one entry for each table referenced by the plan, not one
entry for each table alias (so a table t aliases as t1 and
t2 would have just one entry with key 't' in this HashMap).explain
- flag indicating whether output visualizing the physical
query plan should be given.ParsingException
- if the logical plan is not validpublic static void main(java.lang.String[] argv)