Class FromClause
- java.lang.Object
-
- edu.caltech.nanodb.queryast.FromClause
-
public class FromClause extends java.lang.Object
This class represents a hierarchy of one or more base and derived relations that produce the rows considered by SELECT clauses.The "join condition type" specified in the
condType
field indicates the way that the join condition was specified in the original SQL expression. This value may benull
if the join specified no condition, which would occur with these SQL expressions:- SELECT * FROM t1, t2 WHERE t1.a = t2.a;
- SELECT * FROM t1 JOIN t2 WHERE t1.a = t2.a;
If condType is set to JoinConditionType.JOIN_ON_EXPR then the join condition was specified in an ON clause, such as:
- SELECT * FROM t1 JOIN t2 ON t1.a = t2.a;
getOnExpression()
method.Finally, if the condType is either JoinConditionType.JOIN_USING or JoinConditionType.NATURAL_JOIN then the join condition was implicitly specified via a SQL USING clause, or via a natural join, such as:
- SELECT * FROM t1 JOIN t2 USING (a);
- SELECT * FROM t1 NATURAL JOIN t2;
computeSchema(edu.caltech.nanodb.storage.TableManager)
method has been called (which is indirectly invoked by theSelectCommand
andInsertCommand
classes before planning and executing a query), and is available via thegetComputedJoinExpr()
method.- See Also:
SelectClause
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
FromClause.ClauseType
This enumeration specifies the overall type of the FROM clause.static class
FromClause.JoinConditionType
For FROM clauses that contain join expressions, this enumeration specifies the kind of join-condition for each join expression.
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String
aliasName
If this FROM clause is a base table or a derived table, then this field stores the alias for the table.private FromClause.ClauseType
clauseType
The kind of FROM clause this object represents, either a base table, a derived table (a nested SELECT), or a join expression.private Expression
computedJoinExpr
For FROM clauses that either directly or indirectly specify a join condition, this value is filled in with the join condition to apply, when thecomputeSchema(edu.caltech.nanodb.storage.TableManager)
method is called.private Schema
computedSchema
This value is filled in with the schema of the data that this FROM clause produces, when thecomputeSchema(edu.caltech.nanodb.storage.TableManager)
method is called.private java.util.ArrayList<SelectValue>
computedSelectValues
In cases of NATURAL joins or joins with a USING clause, this value is filled in withSelectValue
objects that can be used to perform a suitable project to eliminate duplicate columns.private FromClause.JoinConditionType
condType
If this object represents a join expression, this field specifies the type of condition used in the join; either a natural join, a join with an ON expression, or a join with a USING clause.private SelectClause
derivedTable
If the FROM clause specifies a nested SELECT command with an alias, this field holds the nested query.private Expression
joinOnExpr
The expression used for joining tables in this FROM clause.private JoinType
joinType
If this object represents a join expression, this field specifies the type of join to use, whether it is an inner join, some kind of outer join, or a cross join.private java.util.List<java.lang.String>
joinUsingNames
private FromClause
leftChild
If this FROM clause is a join expression then this references the left child of the join.private static org.apache.logging.log4j.Logger
logger
A logging object for reporting anything interesting that happens.private FromClause
rightChild
If this FROM clause is a join expression then this references the right child of the join.private FunctionCall
tableFunction
If the FROM clause is a table-returning function then this field holds the name of the table-function and its arguments.private java.lang.String
tableName
If the FROM clause specifies a base table then this field holds the table or function's name.
-
Constructor Summary
Constructors Constructor Description FromClause(FunctionCall tableFunction, java.lang.String aliasName)
FromClause(FromClause left, FromClause right, JoinType type)
Construct a new FROM clause of typeFromClause.ClauseType.JOIN_EXPR
.FromClause(SelectClause derivedTable, java.lang.String aliasName)
Construct a new FROM clause of typeFromClause.ClauseType.SELECT_SUBQUERY
.FromClause(java.lang.String tableName, java.lang.String aliasName)
Construct a new FROM clause of typeFromClause.ClauseType.BASE_TABLE
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addUsingName(java.lang.String name)
For join conditions of the form USING (col1, col2, ...), this method allows the column names to be added to this from clause.private Schema
buildJoinSchema(java.lang.String context, Schema leftSchema, Schema rightSchema, java.util.Set<java.lang.String> commonCols)
This helper function handles a NATURAL join or a join with a USING clause, determining what columns to use in the join, computing the join predicate to use, and computing the schema of the join result.private void
checkJoinColumn(java.lang.String context, java.lang.String side, Schema schema, java.lang.String colName)
Schema
computeSchema(TableManager tableManager)
This function prepares the from-clause for use by the planner to generate an actual execution plan from the clause.FromClause.ClauseType
getClauseType()
Returns the kind of FROM clause this object represents.Expression
getComputedJoinExpr()
java.util.ArrayList<SelectValue>
getComputedSelectValues()
FromClause.JoinConditionType
getConditionType()
Returns the join condition type.JoinType
getJoinType()
Returns the join type of a join clause.FromClause
getLeftChild()
If the from-clause is a join expression, this method returns the left child-clause.Expression
getOnExpression()
Returns the join predicate if this from clause specifies an ON clause.java.lang.String
getResultName()
Returns the name of the relation that the results should go into.FromClause
getRightChild()
If the from-clause is a join expression, this method returns the right child-clause.Schema
getSchema()
SelectClause
getSelectClause()
Returns the select clause for a derived table.java.lang.String
getTableName()
Returns the table name.java.util.List<java.lang.String>
getUsingNames()
For join conditions of the form USING (col1, col2, ...), this method returns the list of column names that were specified.boolean
hasOuterJoinOnLeft()
boolean
hasOuterJoinOnRight()
boolean
isBaseTable()
Returns true if this from clause specifies a base table, as opposed to a derived table or a join expression.boolean
isDerivedTable()
Returns true if this from clause specifies a derived table, as opposed to a base table or a join expression.boolean
isJoinExpr()
Returns true if this from clause specifies a join expression, as opposed to a base table or a join expression.boolean
isOuterJoin()
Returns true if this join clause is an outer join, false otherwise.boolean
isRenamed()
void
setConditionType(FromClause.JoinConditionType type)
Sets the join condition type.void
setOnExpression(Expression expr)
Sets the join predicate if this from clause specifies an ON clause.java.lang.String
toString()
-
-
-
Field Detail
-
logger
private static org.apache.logging.log4j.Logger logger
A logging object for reporting anything interesting that happens.
-
clauseType
private FromClause.ClauseType clauseType
The kind of FROM clause this object represents, either a base table, a derived table (a nested SELECT), or a join expression.
-
tableName
private java.lang.String tableName
If the FROM clause specifies a base table then this field holds the table or function's name. This field will only be set to a non-null
value ifclauseType
is set toFromClause.ClauseType.BASE_TABLE
.
-
tableFunction
private FunctionCall tableFunction
If the FROM clause is a table-returning function then this field holds the name of the table-function and its arguments. This field will only be set to a non-null
value ifclauseType
is set toFromClause.ClauseType.TABLE_FUNCTION
.
-
derivedTable
private SelectClause derivedTable
If the FROM clause specifies a nested SELECT command with an alias, this field holds the nested query. This field will only be set to a non-null
value ifclauseType
is set toFromClause.ClauseType.SELECT_SUBQUERY
.
-
aliasName
private java.lang.String aliasName
If this FROM clause is a base table or a derived table, then this field stores the alias for the table. The alias is optional for base tables, but it is required for derived tables.
-
leftChild
private FromClause leftChild
If this FROM clause is a join expression then this references the left child of the join. This field will only be set to a non-null
value ifclauseType
is set toFromClause.ClauseType.JOIN_EXPR
.
-
rightChild
private FromClause rightChild
If this FROM clause is a join expression then this references the right child of the join. This field will only be set to a non-null
value ifclauseType
is set toFromClause.ClauseType.JOIN_EXPR
.
-
joinType
private JoinType joinType
If this object represents a join expression, this field specifies the type of join to use, whether it is an inner join, some kind of outer join, or a cross join.
-
condType
private FromClause.JoinConditionType condType
If this object represents a join expression, this field specifies the type of condition used in the join; either a natural join, a join with an ON expression, or a join with a USING clause.
-
joinUsingNames
private java.util.List<java.lang.String> joinUsingNames
-
joinOnExpr
private Expression joinOnExpr
The expression used for joining tables in this FROM clause.
-
computedSchema
private Schema computedSchema
This value is filled in with the schema of the data that this FROM clause produces, when thecomputeSchema(edu.caltech.nanodb.storage.TableManager)
method is called. This is done when a SQL statement is being prepared for execution, and is typically kicked off by theSelectClause.computeSchema(edu.caltech.nanodb.storage.TableManager, edu.caltech.nanodb.queryast.SelectClause)
method, which in turn prepares the from-clause objects.
-
computedJoinExpr
private Expression computedJoinExpr
For FROM clauses that either directly or indirectly specify a join condition, this value is filled in with the join condition to apply, when thecomputeSchema(edu.caltech.nanodb.storage.TableManager)
method is called. This is done when a SQL statement is being prepared for execution, and is typically kicked off by theSelectClause.computeSchema(edu.caltech.nanodb.storage.TableManager, edu.caltech.nanodb.queryast.SelectClause)
method, which in turn prepares the from-clause objects.
-
computedSelectValues
private java.util.ArrayList<SelectValue> computedSelectValues
In cases of NATURAL joins or joins with a USING clause, this value is filled in withSelectValue
objects that can be used to perform a suitable project to eliminate duplicate columns. If no duplicate columns are to be eliminated, this value will be null. This value is populated when thecomputeSchema(edu.caltech.nanodb.storage.TableManager)
method is called, and is typically kicked off by theSelectClause.computeSchema(edu.caltech.nanodb.storage.TableManager, edu.caltech.nanodb.queryast.SelectClause)
method, which in turn prepares the from-clause objects.
-
-
Constructor Detail
-
FromClause
public FromClause(java.lang.String tableName, java.lang.String aliasName)
Construct a new FROM clause of typeFromClause.ClauseType.BASE_TABLE
. The alias name may be null in the case of base-tables.- Parameters:
tableName
- the name of the table being selected fromaliasName
- an optional alias used to refer to the table in the rest of the query
-
FromClause
public FromClause(FunctionCall tableFunction, java.lang.String aliasName)
-
FromClause
public FromClause(SelectClause derivedTable, java.lang.String aliasName)
Construct a new FROM clause of typeFromClause.ClauseType.SELECT_SUBQUERY
. The alias name must be specified for derived tables.- Parameters:
derivedTable
- the subquery that is referenced by this from-clausealiasName
- the relation-name given for this subquery- Throws:
java.lang.NullPointerException
- if either derivedTable or aliasName is null
-
FromClause
public FromClause(FromClause left, FromClause right, JoinType type)
Construct a new FROM clause of typeFromClause.ClauseType.JOIN_EXPR
. The two sub-relations being joined are specified as FromClause instances.- Parameters:
left
- the left relation being joinedright
- the right relation being joinedtype
- the kind of join being performed- Throws:
java.lang.NullPointerException
- if either sub-relation is null, or if the join-type is null.
-
-
Method Detail
-
getClauseType
public FromClause.ClauseType getClauseType()
Returns the kind of FROM clause this object represents.- Returns:
- the clause-type. This value will never be null.
-
isBaseTable
public boolean isBaseTable()
Returns true if this from clause specifies a base table, as opposed to a derived table or a join expression.- Returns:
- true if this clause specifies a base table.
-
isDerivedTable
public boolean isDerivedTable()
Returns true if this from clause specifies a derived table, as opposed to a base table or a join expression.- Returns:
true
if this clause is a derived table specified as a SELECT statement.
-
isJoinExpr
public boolean isJoinExpr()
Returns true if this from clause specifies a join expression, as opposed to a base table or a join expression.- Returns:
true
if this clause is a join expression.
-
getTableName
public java.lang.String getTableName()
Returns the table name. This value will be null if this is a derived table or a join expression.- Returns:
- the name of the table.
- Throws:
java.lang.IllegalStateException
- if the from-clause's type is not aFromClause.ClauseType.BASE_TABLE
.
-
getSelectClause
public SelectClause getSelectClause()
Returns the select clause for a derived table. This value will be null if this is not derived.- Returns:
- the derived select clause
- Throws:
java.lang.IllegalStateException
- if the from-clause's type is not aFromClause.ClauseType.SELECT_SUBQUERY
.
-
getLeftChild
public FromClause getLeftChild()
If the from-clause is a join expression, this method returns the left child-clause.- Returns:
- the left child-clause of this join clause
- Throws:
java.lang.IllegalStateException
- if this from-clause is not a join expression.
-
getRightChild
public FromClause getRightChild()
If the from-clause is a join expression, this method returns the right child-clause.- Returns:
- the right child-clause of this join clause
- Throws:
java.lang.IllegalStateException
- if this from-clause is not a join expression.
-
getResultName
public java.lang.String getResultName()
Returns the name of the relation that the results should go into. In the case where an alias exists, this returns the alias. If an alias does not exist, this must return the table name for the base table.- Returns:
- the result relation name
- Throws:
java.lang.IllegalStateException
- if the clause-type is a join expression, since that kind of from-clause doesn't have a result name.
-
isRenamed
public boolean isRenamed()
-
getJoinType
public JoinType getJoinType()
Returns the join type of a join clause.- Returns:
- the join type
- Throws:
java.lang.IllegalStateException
- if the clause-type is not a join clause. Only join clauses have a join type.
-
isOuterJoin
public boolean isOuterJoin()
Returns true if this join clause is an outer join, false otherwise.- Returns:
- true if this join clause is an outer join, false otherwise.
- Throws:
java.lang.IllegalStateException
- if the clause-type is not a join clause. Only join clauses have a join type.
-
hasOuterJoinOnLeft
public boolean hasOuterJoinOnLeft()
-
hasOuterJoinOnRight
public boolean hasOuterJoinOnRight()
-
getConditionType
public FromClause.JoinConditionType getConditionType()
Returns the join condition type. This value will be null if there is only 1 table in the outer array of from clauses.- Returns:
- the join condition type
- Throws:
java.lang.IllegalStateException
- if the clause-type is not a join clause. Only join clauses have a join-condition type.
-
setConditionType
public void setConditionType(FromClause.JoinConditionType type)
Sets the join condition type.- Parameters:
type
- the join condition type- Throws:
java.lang.IllegalStateException
- if the clause-type is not a join clause. Only join clauses have a join-condition type.
-
getUsingNames
public java.util.List<java.lang.String> getUsingNames()
For join conditions of the form USING (col1, col2, ...), this method returns the list of column names that were specified. If this join expression does not use a USING expression then this method returns null.- Returns:
- the list of column names specified in the USING clause
- Throws:
java.lang.IllegalStateException
- if the clause-type is not a join clause. Only join clauses have a join condition.
-
addUsingName
public void addUsingName(java.lang.String name)
For join conditions of the form USING (col1, col2, ...), this method allows the column names to be added to this from clause. This method is used by the parser to construct the from clause as it is being parsed.- Parameters:
name
- the name of the column to use in the join operation- Throws:
java.lang.IllegalStateException
- if the clause-type is not a join clause. Only join clauses have a join condition.
-
getOnExpression
public Expression getOnExpression()
Returns the join predicate if this from clause specifies an ON clause. Returns null if there is no ON clause.- Returns:
- the expression specified in the ON clause
- Throws:
java.lang.IllegalStateException
- if the clause-type is not a join clause. Only join clauses have a join condition.
-
setOnExpression
public void setOnExpression(Expression expr)
Sets the join predicate if this from clause specifies an ON clause. Returns null if there is no ON clause.- Parameters:
expr
- the expression specified in the ON clause- Throws:
java.lang.IllegalStateException
- if the clause-type is not a join clause. Only join clauses have a join condition.
-
computeSchema
public Schema computeSchema(TableManager tableManager)
This function prepares the from-clause for use by the planner to generate an actual execution plan from the clause. This includes several important tasks:- The schema of the from-clause's output is determined.
- If the from-clause is a join, and the join specifies NATURAL, USING (col, ...), or an ON clause, the join condition is determined
Since a from-clause can be a SQL subquery or a join expression, it should be evident that the method will recursively prepare its children as well.
This method really shouldn't be called directly. Instead, the enclosing
SelectClause
will calls this method when the clause'sSelectClause.computeSchema(edu.caltech.nanodb.storage.TableManager, edu.caltech.nanodb.queryast.SelectClause)
method is invoked.- Returns:
- the schema of the from-clause's output
-
buildJoinSchema
private Schema buildJoinSchema(java.lang.String context, Schema leftSchema, Schema rightSchema, java.util.Set<java.lang.String> commonCols)
This helper function handles a NATURAL join or a join with a USING clause, determining what columns to use in the join, computing the join predicate to use, and computing the schema of the join result.
The schema of the result follows the SQL standard:
- Common columns first, in the order specified, if any.
- Columns that appear only in the left table, if any.
- Columns that appear only in the right table, if any.
Of course, any of these sets of columns could be empty.
- Parameters:
context
- a string used in logging messages and exceptions to indicate the context in which this method was called.leftSchema
- the schema of the table on the left side of the joinrightSchema
- the schema of the table on the right side of the joincommonCols
- the set of common columns to use- Returns:
- the schema of the result, as produced by the join operation.
-
checkJoinColumn
private void checkJoinColumn(java.lang.String context, java.lang.String side, Schema schema, java.lang.String colName)
-
getSchema
public Schema getSchema()
-
getComputedJoinExpr
public Expression getComputedJoinExpr()
-
getComputedSelectValues
public java.util.ArrayList<SelectValue> getComputedSelectValues()
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-