Package edu.caltech.nanodb.queryast
Class SelectClause
java.lang.Object
edu.caltech.nanodb.queryast.SelectClause
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
FieldsModifier and TypeFieldDescriptionprivate HashMap<ColumnName, SelectClause> 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 booleanThis flag indicates whether the SELECT expression should generate duplicate rows, or whether it should simply produce distinct or unique rows.private FromClauseThis field holds a hierarchy of one or more base and derived relations that produce the rows considered by this SELECT clause.private SchemaWhen preparing SQL commands for execution, this value is filled in with the schema that this query's FROM clause produces.private List<Expression> This collection holds zero or more entries specifying GROUP BY values.private ExpressionIf a HAVING expression is specified, this field will refer to the expression to be evaluated.private intThe maximum number of rows that may be returned by the SELECT clause.private static org.apache.logging.log4j.LoggerA logging object for reporting anything interesting that happens.private intThe offset of the first row to return from the SELECT clause.private List<OrderByExpression> This collection holds zero or more entries specifying ORDER BY values.private SelectClauseThe parent of this select-clause, if it is a nested subquery;nullotherwise.private SchemaWhen preparing SQL commands for execution, this value is filled in with the schema that this SELECT clause produces.private List<SelectValue> The specification of values to be produced by the SELECT clause.private ExpressionIf a WHERE expression is specified, this field will refer to the expression to be evaluated. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddGroupByExpr(Expression groupExpr) voidaddOrderByExpr(OrderByExpression orderByExpr) voidaddSelectValue(SelectValue selectValue) Adds a specification to the set of values produced by this SELECT clause.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.Retrieves the from clause for this select clause.Returns the schema of the data produced by the FROM clause of this query.intgetLimit()If a LIMIT clause is specified, this method returns the specified limit; otherwise, the default of 0 is returned.intIf an OFFSET clause is specified, this method returns the specified offset; otherwise, the default of 0 is returned.Returns the parent of this query if it is a subquery;nullotherwise.Returns the schema for this select clause, ornullif thecomputeSchema(edu.caltech.nanodb.storage.TableManager, edu.caltech.nanodb.queryast.SelectClause)method hasn't yet been called on this clause.Retrieves the select values for this select clause.Retrieves the where clause from this from clause.booleanReturns true if the select clause is correlated with some enclosing query, or false otherwise.booleanReturns true if the select clause's results are to be distinct, or false if the clause's results are not distinct.booleanprivate voidresolveColumnRef(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 voidresolveExpressionRefs(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.voidsetDistinct(boolean distinct) Mark the select clause's results as being distinct or not distinct.voidsetFromClause(FromClause fromClause) Sets the hierarchy of base and derived relations that produce the rows considered by this SELECT clause.voidsetHavingExpr(Expression havingExpr) voidsetLimit(int limit) Set the upper limit of how many rows should be produced by this query.voidsetOffset(int offset) Set the starting offset for the rows that should be produced by this query.voidsetWhereExpr(Expression whereExpr) Sets the expression for the WHERE clause.toString()
-
Field Details
-
logger
private static org.apache.logging.log4j.Logger loggerA logging object for reporting anything interesting that happens. -
parentSelect
The parent of this select-clause, if it is a nested subquery;nullotherwise. -
distinct
private boolean distinctThis flag indicates whether the SELECT expression should generate duplicate rows, or whether it should simply produce distinct or unique rows. -
selectValues
The specification of values to be produced by the SELECT clause. These expressions comprise the Generalized Project operation portion of the command. -
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
If a WHERE expression is specified, this field will refer to the expression to be evaluated. -
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
If a HAVING expression is specified, this field will refer to the expression to be evaluated. -
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 limitThe maximum number of rows that may be returned by the SELECT clause. The default value of 0 means "no limit". -
offset
private int offsetThe 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
When preparing SQL commands for execution, this value is filled in with the schema that this SELECT clause produces. -
fromSchema
When preparing SQL commands for execution, this value is filled in with the schema that this query's FROM clause produces.
-
-
Constructor Details
-
SelectClause
public SelectClause()
-
-
Method Details
-
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
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
Retrieves the select values for this select clause.- Returns:
- the select values
-
isTrivialProject
public boolean isTrivialProject() -
getParentSelect
Returns the parent of this query if it is a subquery;nullotherwise.- Returns:
- the parent of this query if it is a subquery;
nullotherwise.
-
setFromClause
Sets the hierarchy of base and derived relations that produce the rows considered by this SELECT clause. -
getFromClause
Retrieves the from clause for this select clause. This can benullif the query doesn't specify a FROM clause.- Returns:
- the from clause
-
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
Sets the expression for the WHERE clause. Anullvalue indicates that the SELECT clause has no WHERE condition. -
getWhereExpr
Retrieves the where clause from this from clause.- Returns:
- the where exprssion
-
addGroupByExpr
-
getGroupByExprs
-
setHavingExpr
-
getHavingExpr
-
addOrderByExpr
-
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."
-
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, ornullif 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
Returns the schema for this select clause, ornullif 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(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(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
-