Package edu.caltech.nanodb.queryast
Class SelectClause
- java.lang.Object
-
- edu.caltech.nanodb.queryast.SelectClause
-
public class SelectClause extends java.lang.Object
This class represents a single SELECT ... statement or clause. SELECT statements can appear as clauses within other expressions, so the class is written to be used easily within other classes.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.HashMap<ColumnName,SelectClause>
correlatedWith
If this query is correlated with one or more enclosing queries, this will contain column names, and the corresponding references to the enclosing queries.private boolean
distinct
This flag indicates whether the SELECT expression should generate duplicate rows, or whether it should simply produce distinct or unique rows.private FromClause
fromClause
This field holds a hierarchy of one or more base and derived relations that produce the rows considered by this SELECT clause.private Schema
fromSchema
When preparing SQL commands for execution, this value is filled in with the schema that this query's FROM clause produces.private java.util.List<Expression>
groupByExprs
This collection holds zero or more entries specifying GROUP BY values.private Expression
havingExpr
If a HAVING expression is specified, this field will refer to the expression to be evaluated.private int
limit
The maximum number of rows that may be returned by the SELECT clause.private static org.apache.logging.log4j.Logger
logger
A logging object for reporting anything interesting that happens.private int
offset
The offset of the first row to return from the SELECT clause.private java.util.List<OrderByExpression>
orderByExprs
This collection holds zero or more entries specifying ORDER BY values.private SelectClause
parentSelect
The parent of this select-clause, if it is a nested subquery;null
otherwise.private Schema
resultSchema
When preparing SQL commands for execution, this value is filled in with the schema that this SELECT clause produces.private java.util.List<SelectValue>
selectValues
The specification of values to be produced by the SELECT clause.private Expression
whereExpr
If a WHERE expression is specified, this field will refer to the expression to be evaluated.
-
Constructor Summary
Constructors Constructor Description SelectClause()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addGroupByExpr(Expression groupExpr)
void
addOrderByExpr(OrderByExpression orderByExpr)
void
addSelectValue(SelectValue selectValue)
Adds a specification to the set of values produced by this SELECT clause.Schema
computeSchema(TableManager tableManager, SelectClause parentSelect)
This method computes the resulting schema from this query, and in the process it performs various semantic checks as well.java.util.Set<ColumnName>
getCorrelatedColumns()
FromClause
getFromClause()
Retrieves the from clause for this select clause.Schema
getFromSchema()
Returns the schema of the data produced by the FROM clause of this query.java.util.List<Expression>
getGroupByExprs()
Expression
getHavingExpr()
int
getLimit()
If a LIMIT clause is specified, this method returns the specified limit; otherwise, the default of 0 is returned.int
getOffset()
If an OFFSET clause is specified, this method returns the specified offset; otherwise, the default of 0 is returned.java.util.List<OrderByExpression>
getOrderByExprs()
SelectClause
getParentSelect()
Returns the parent of this query if it is a subquery;null
otherwise.Schema
getSchema()
Returns the schema for this select clause, ornull
if thecomputeSchema(edu.caltech.nanodb.storage.TableManager, edu.caltech.nanodb.queryast.SelectClause)
method hasn't yet been called on this clause.java.util.List<SelectValue>
getSelectValues()
Retrieves the select values for this select clause.Expression
getWhereExpr()
Retrieves the where clause from this from clause.boolean
isCorrelated()
Returns true if the select clause is correlated with some enclosing query, or false otherwise.boolean
isDistinct()
Returns true if the select clause's results are to be distinct, or false if the clause's results are not distinct.boolean
isTrivialProject()
private void
resolveColumnRef(java.lang.String desc, Expression expr, ColumnName colName, Schema s, boolean checkParentQueries)
This helper function attempts to resolve a specific column-name against a query's schema.private void
resolveExpressionRefs(java.lang.String desc, Expression expr, Schema s, boolean checkParentQueries)
This helper function goes through the expression and verifies that every symbol-reference corresponds to an actual value produced by the FROM-clause of the SELECT query.void
setDistinct(boolean distinct)
Mark the select clause's results as being distinct or not distinct.void
setFromClause(FromClause fromClause)
Sets the hierarchy of base and derived relations that produce the rows considered by this SELECT clause.void
setHavingExpr(Expression havingExpr)
void
setLimit(int limit)
Set the upper limit of how many rows should be produced by this query.void
setOffset(int offset)
Set the starting offset for the rows that should be produced by this query.void
setWhereExpr(Expression whereExpr)
Sets the expression for the WHERE 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.
-
parentSelect
private SelectClause parentSelect
The parent of this select-clause, if it is a nested subquery;null
otherwise.
-
distinct
private boolean distinct
This flag indicates whether the SELECT expression should generate duplicate rows, or whether it should simply produce distinct or unique rows.
-
selectValues
private java.util.List<SelectValue> selectValues
The specification of values to be produced by the SELECT clause. These expressions comprise the Generalized Project operation portion of the command.
-
fromClause
private FromClause fromClause
This field holds a hierarchy of one or more base and derived relations that produce the rows considered by this SELECT clause. If the SELECT expression has no FROM clause, this field will be null.
-
whereExpr
private Expression whereExpr
If a WHERE expression is specified, this field will refer to the expression to be evaluated.
-
groupByExprs
private java.util.List<Expression> groupByExprs
This collection holds zero or more entries specifying GROUP BY values. If the SELECT expression has no GROUP BY clause, this collection will be empty.
-
havingExpr
private Expression havingExpr
If a HAVING expression is specified, this field will refer to the expression to be evaluated.
-
orderByExprs
private java.util.List<OrderByExpression> orderByExprs
This collection holds zero or more entries specifying ORDER BY values. If the SELECT expression has no ORDER BY clause, this collection will be empty.
-
limit
private int limit
The maximum number of rows that may be returned by the SELECT clause. The default value of 0 means "no limit".
-
offset
private int offset
The offset of the first row to return from the SELECT clause. Earlier rows will be computed but not returned. The default value of 0 means "start with the first row."
-
resultSchema
private Schema resultSchema
When preparing SQL commands for execution, this value is filled in with the schema that this SELECT clause produces.
-
fromSchema
private Schema fromSchema
When preparing SQL commands for execution, this value is filled in with the schema that this query's FROM clause produces.
-
correlatedWith
private java.util.HashMap<ColumnName,SelectClause> correlatedWith
If this query is correlated with one or more enclosing queries, this will contain column names, and the corresponding references to the enclosing queries.- Design Note:
- (Donnie) We need to know both the column names and the corresponding parent-queries that will generate those values, so that we can set up the execution plan properly.
-
-
Method Detail
-
setDistinct
public void setDistinct(boolean distinct)
Mark the select clause's results as being distinct or not distinct. This corresponds to whether the SQL command is "SELECT [ALL] ..." or "SELECT DISTINCT ...".- Parameters:
distinct
- If true, specifies that the results of this select clause are distinct. If false, the results of the clause are not distinct.
-
isDistinct
public boolean isDistinct()
Returns true if the select clause's results are to be distinct, or false if the clause's results are not distinct.
-
addSelectValue
public void addSelectValue(SelectValue selectValue)
Adds a specification to the set of values produced by this SELECT clause. This method is called by the parser as a SELECT command (or subquery) is being parsed.
-
getSelectValues
public java.util.List<SelectValue> getSelectValues()
Retrieves the select values for this select clause.- Returns:
- the select values
-
isTrivialProject
public boolean isTrivialProject()
-
getParentSelect
public SelectClause getParentSelect()
Returns the parent of this query if it is a subquery;null
otherwise.- Returns:
- the parent of this query if it is a subquery;
null
otherwise.
-
setFromClause
public void setFromClause(FromClause fromClause)
Sets the hierarchy of base and derived relations that produce the rows considered by this SELECT clause.
-
getFromClause
public FromClause getFromClause()
Retrieves the from clause for this select clause. This can benull
if the query doesn't specify a FROM clause.- Returns:
- the from clause
-
getFromSchema
public Schema getFromSchema()
Returns the schema of the data produced by the FROM clause of this query. If the query has no FROM clause, this will be an empty schema.- Returns:
- the schema produced by the FROM clause of this query, or an empty schema if the query has no FROM clause.
-
setWhereExpr
public void setWhereExpr(Expression whereExpr)
Sets the expression for the WHERE clause. Anull
value indicates that the SELECT clause has no WHERE condition.
-
getWhereExpr
public Expression getWhereExpr()
Retrieves the where clause from this from clause.- Returns:
- the where exprssion
-
addGroupByExpr
public void addGroupByExpr(Expression groupExpr)
-
getGroupByExprs
public java.util.List<Expression> getGroupByExprs()
-
setHavingExpr
public void setHavingExpr(Expression havingExpr)
-
getHavingExpr
public Expression getHavingExpr()
-
addOrderByExpr
public void addOrderByExpr(OrderByExpression orderByExpr)
-
getOrderByExprs
public java.util.List<OrderByExpression> getOrderByExprs()
-
getLimit
public int getLimit()
If a LIMIT clause is specified, this method returns the specified limit; otherwise, the default of 0 is returned.- Returns:
- the offset specified in the SQL
-
setLimit
public void setLimit(int limit)
Set the upper limit of how many rows should be produced by this query. A value of 0 means "unlimited." Negative values are disallowed and will cause an exception to be thrown.- Parameters:
limit
- a positive number specifying the maximum number of tuples to produce, or 0 to specify "unlimited."
-
getOffset
public int getOffset()
If an OFFSET clause is specified, this method returns the specified offset; otherwise, the default of 0 is returned.- Returns:
- the offset specified in the SQL
-
setOffset
public void setOffset(int offset)
Set the starting offset for the rows that should be produced by this query. A value of 0 means "start with the first row" (in other words, "no offset"). Negative values are disallowed and will cause an exception to be thrown.- Parameters:
offset
- a positive number specifying the number of tuples to skip during query evaluation, or 0 to specify "start at the beginning."
-
isCorrelated
public boolean isCorrelated()
Returns true if the select clause is correlated with some enclosing query, or false otherwise.- Returns:
- true if the select clause is correlated with some enclosing query, or false otherwise.
-
getCorrelatedColumns
public java.util.Set<ColumnName> getCorrelatedColumns()
-
computeSchema
public Schema computeSchema(TableManager tableManager, SelectClause parentSelect) throws SchemaNameException
This method computes the resulting schema from this query, and in the process it performs various semantic checks as well.- Parameters:
tableManager
- the table manager to use for retrieving schema infoparentSelect
- the enclosing SELECT query if this is a nested subquery, ornull
if this is a top-level query- Returns:
- the schema of this select clause's result
- Throws:
SchemaNameException
- if the select clause contains some kind of semantic error involving schemas that are referenced
-
getSchema
public Schema getSchema()
Returns the schema for this select clause, ornull
if thecomputeSchema(edu.caltech.nanodb.storage.TableManager, edu.caltech.nanodb.queryast.SelectClause)
method hasn't yet been called on this clause.- Returns:
- the schema for this select clause
-
resolveExpressionRefs
private void resolveExpressionRefs(java.lang.String desc, Expression expr, Schema s, boolean checkParentQueries) throws SchemaNameException
This helper function goes through the expression and verifies that every symbol-reference corresponds to an actual value produced by the FROM-clause of the SELECT query. Any column-names that don't include a table-name are also updated to include the proper table-name.It's possible to have symbols that don't reference columns in the FROM clause, if a query is correlated with an enclosing query. In these cases, the unresolvable attributes are collected so they can be resolved at the end of the process.
- Parameters:
desc
- A short string describing the context of the expression, since expressions can appear in the SELECT clause, the WHERE clause, the GROUP BY clause, etc.expr
- The expression that will be evaluated.s
- The schema against which the expression will be evaluated.checkParentQueries
- if this is true and the column-name can't be resolved against the current query, any parent queries will also be checked for the column-name. This allows correlated queries to be resolved properly.- Throws:
SchemaNameException
- if an expression-reference cannot be resolved against the specified schema, either because the named column or table doesn't appear in the schema, or if a column name is ambiguous.
-
resolveColumnRef
private void resolveColumnRef(java.lang.String desc, Expression expr, ColumnName colName, Schema s, boolean checkParentQueries) throws SchemaNameException
This helper function attempts to resolve a specific column-name against a query's schema.- Parameters:
desc
- A short string describing the context of the expression, since expressions can appear in the SELECT clause, the WHERE clause, the GROUP BY clause, etc.expr
- The expression that will be evaluated.colName
- The column-name to resolve.s
- The schema against which the expression will be evaluated.checkParentQueries
- if this is true and the column-name can't be resolved against the current query, any parent queries will also be checked for the column-name. This allows correlated queries to be resolved properly.- Throws:
SchemaNameException
- if an expression-reference cannot be resolved against the specified schema, either because the named column or table doesn't appear in the schema, or if a column name is ambiguous.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-