Top Description Inners Fields Constructors Methods
org.h2.command.query

public Class Select

extends Query
Class Inheritance
Static Imports
org.h2.expression.Expression.WITHOUT_PARENTHESES, org.h2.util.HasSQL.ADD_PLAN_INFORMATION, .HasSQL.DEFAULT_SQL_FLAGS

This class represents a simple SELECT statement. For each select statement, visibleColumnCount <= distinctColumnCount <= expressionCount. The expression list count could include ORDER BY and GROUP BY expressions that are not in the select list. The call sequence is init(), mapColumns() if it's a subquery, prepare().
Authors
Thomas Mueller, Joel Turkel (Group sorted query)

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
Select.LazyResultGroupSorted

Lazy execution for a group sorted query.

private class
Select.LazyResultQueryFlat

Lazy execution for a flat query.

private abstract class
Select.LazyResultSelect

Lazy execution for this select.

private static enum

Field Summary

Modifier and TypeField and Description
private Expression
condition

WHERE condition.

private double
private Expression[]
distinctExpressions

DISTINCT ON(...) expressions.

private int[]
private final ArrayList<TableFilter>
private ForUpdate
private ArrayList<Expression>
private int[]
pack-priv boolean[]
groupByExpression

Whether a column in the expression list is part of a group-by.

pack-priv SelectGroups
groupData

Grouped data for aggregates.

pack-priv int[]
groupIndex

The indexes of the group-by columns.

private Expression
having

HAVING condition.

private int
private int
private boolean
private boolean
isExplicitTable

Whether this SELECT is an explicit table (TABLE tableName).

pack-priv boolean
isGroupQuery

This flag is set when SELECT statement contains (non-window) aggregate functions, GROUP BY clause or HAVING clause.

private boolean
private boolean
private boolean
private boolean
private Select
parentSelect

Parent select for selects in table filters.

private Expression
qualify

QUALIFY condition.

private int
private final ArrayList<TableFilter>
pack-priv TableFilter
topTableFilter

The main (top) table filter.

private HashMap<String, Window>
Inherited from org.h2.command.query.Query:
checkInitdistinctexpressionArrayexpressionsfetchExprfetchPercentisPreparedoffsetExprorderListrandomAccessResultresultColumnCountsortvisibleColumnCountwithTies

Constructor Summary

AccessConstructor and Description
public
Select(SessionLocal session, Select parentSelect)

Method Summary

Modifier and TypeMethod and Description
public void
addCondition(Expression
the condition to add
cond
)

Add a condition to the list of conditions.

private int
addExpandedColumn(TableFilter filter, int index, HashMap<Column, ExpressionColumn> except, String schema, String alias, Column c)

public void
addGlobalCondition(Parameter
the parameter
param
,
int
the column index (0 meaning the first column)
columnId
,
int
the comparison type
comparisonType
)

Implements abstract org.h2.command.query.Query.addGlobalCondition.

Add a condition to the query.

private static Expression
addGlobalCondition(Expression condition, Expression additional)

public void
addTableFilter(TableFilter
the table to add
filter
,
boolean
if the table can be the first table in the query plan
isTop
)

Add a table to the query.

public boolean

Returns:

true if a new definition was added, false if old definition was replaced
addWindow
(String
name
name
,
Window
window definition
window
)

Adds a named window definition.

public boolean
allowGlobalConditions()

Implements abstract org.h2.command.query.Query.allowGlobalConditions.

Check whether adding condition to the query is allowed.

private Value[]
constructGroupResultRow(Value[] keyValues, int columnCount)

private static boolean
pack-priv Value[]

Returns:

the row
createGroupSortedRow
(Value[]
the key values
keyValues
,
int
the number of columns
columnCount
)

Create a row with the current values, for queries with group-sort.

private LocalResult
private void
private void
private int
expandColumnList(TableFilter filter, int index, boolean forAlias, HashMap<Column, ExpressionColumn> except)

public void
fireBeforeSelectTriggers()

Implements abstract org.h2.command.query.Query.fireBeforeSelectTriggers.

Call the before triggers on all tables.

private void
gatherGroup(int columnCount, int stage)

public Expression
public double
getCost()

Implements abstract org.h2.command.query.Query.getCost.

Calculate the cost to execute this query.

private static void
getFilterSQL(StringBuilder builder, String sql, Expression[] exprList, Expression condition, int conditionIndex, int sqlFlags)

private static void
getFilterSQL(StringBuilder builder, String sql, Expression condition, int sqlFlags)

public ForUpdate
getForUpdate()

Overrides org.h2.command.query.Query.getForUpdate.

Returns FOR UPDATE clause, if any.

public ArrayList<Expression>
public SelectGroups

Returns:

the grouped data
getGroupDataIfCurrent
(boolean
is this a window function
window
)

Get the group data if there is currently a group-by active.

private Index
public Expression
public Expression
getIfSingleRow()

Overrides org.h2.command.query.Query.getIfSingleRow.

If this query is determined as a single-row query, returns a replacement expression.

private List<IndexSort>

Returns:

possible index-sorting operations, or null if unavailable
getIndexSorts
()

Returns possible index-sorting operations (better first) if they exist.

public Select

Returns:

parent select, or null
getParentSelect
()

Returns parent select, or null.

private static boolean
getPlanFromFilter(StringBuilder builder, int sqlFlags, TableFilter f, boolean isJoin)

public StringBuilder
getPlanSQL(StringBuilder
string builder
builder
,
int
formatting flags
sqlFlags
)

Overrides org.h2.command.Prepared.getPlanSQL.

Appends the SQL statement with the execution plan.

public Expression
public SortOrder
public HashSet<Table>
getTables()

Implements abstract org.h2.command.query.Query.getTables.

Get all tables that are involved in this query.

public ArrayList<TableFilter>
public TableFilter
public Window

Returns:

the window with specified name, or null
getWindow
(String
name of the window
name
)

Returns a window with specified name, or null.

public void
init()

Implements abstract org.h2.command.query.Query.init.

Initialize the query.

private void
initGroupData(int columnCount)

public boolean
public boolean
pack-priv boolean
pack-priv boolean
public boolean
isConstantQuery()

Overrides org.h2.command.query.Query.isConstantQuery.

Check if this query will always return the same value and has no side effects.

public boolean
isEverything(ExpressionVisitor
the visitor
visitor
)

Implements abstract org.h2.command.query.Query.isEverything.

Check if this expression and all sub-expressions can fulfill a criteria.

public boolean

Returns:

whether this query is a group query.
isGroupQuery
()

Checks if this query is a group query.

private boolean
isGroupSortedIndex(TableFilter tableFilter, Index index)

public boolean

Returns:

true if window stage of group window query is performed
isGroupWindowStage2
()

Checks if window stage of group window query is performed.

private boolean
private boolean
public boolean

Returns:

true if a direct lookup is possible
isQuickAggregateQuery
()

Check if this is an aggregate query with direct lookup, for example a query of the type SELECT COUNT(*) FROM TEST or SELECT MAX(ID) FROM TEST.

public boolean
isUnion()

Implements abstract org.h2.command.query.Query.isUnion.

Check if this is a UNION query.

public boolean

Returns:

whether this query contains window functions
isWindowQuery
()

Checks if this query contains window functions.

public void
mapColumns(ColumnResolver
the resolver
resolver
,
int
the subquery level (0 is the top level query, 1 is the first subquery level)
level
,
boolean
whether this method was called from the outer query
outer
)

Implements abstract org.h2.command.query.Query.mapColumns.

Map the columns to the given column resolver.

private void
mapCondition(int index)

private int
mergeGroupByExpressions(Database db, int index, ArrayList<String> expressionSQL, boolean scanPrevious)

private void
public void
public void
private double
preparePlan(boolean parse)

private void
processGroupResult(int columnCount, LocalResult result, long offset, boolean quickOffset, boolean withHaving)

private void
queryDistinct(ResultTarget result, long offset, long limitRows, boolean withTies, boolean quickOffset)

private LazyResult
queryFlat(int columnCount, ResultTarget result, long offset, long limitRows, boolean withTies, Select.QuickOffset quickOffset)

private void
queryGroup(int columnCount, LocalResult result, long offset, boolean quickOffset)

private LazyResult
queryGroupSorted(int columnCount, ResultTarget result, long offset, boolean quickOffset)

private void
queryGroupWindow(int columnCount, LocalResult result, long offset, boolean quickOffset)

private void
queryQuick(int columnCount, ResultTarget result, boolean skipResult)

private void
queryWindow(int columnCount, LocalResult result, long offset, boolean quickOffset)

protected ResultInterface
queryWithoutCache(long
the limit as specified in the JDBC method call
maxRows
,
ResultTarget
the target to write results to
target
)

Implements abstract org.h2.command.query.Query.queryWithoutCache.

Execute the query without checking the cache.

private void
readWithLimit(ResultTarget result, long limitRows, boolean withTies, Select.LazyResultQueryFlat lazyResult)

private Value[]

Returns:

the same or the truncated row
rowForResult
(Value[]
the complete row
row
,
int
the number of columns to keep
columnCount
)

Removes HAVING and QUALIFY columns from the row.

public void
setDistinct()

Set the distinct flag.

public void
setDistinct(Expression[]
array of expressions
distinctExpressions
)

Set the DISTINCT ON expressions.

public void
setEvaluatable(TableFilter
the table filter
tableFilter
,
boolean
the new value
b
)

Implements abstract org.h2.command.query.Query.setEvaluatable.

Change the evaluatable flag.

private void
public void
setExplicitTable()

Convert this SELECT to an explicit table (TABLE tableName).

public void
public void
setForUpdate(ForUpdate
the new FOR UPDATE clause
b
)

Implements abstract org.h2.command.query.Query.setForUpdate.

Set the FOR UPDATE clause.

public void
pack-priv void
setGroupData(final SelectGroups groupData)

public void
setGroupQuery()

Set when SELECT statement contains (non-window) aggregate functions, GROUP BY clause or HAVING clause.

public void
public void
public void
setWildcard()

Sets a wildcard expression as in "SELECT * FROM TEST".

public void
setWindowQuery()

Called if this query contains window functions.

private static void
skipOffset(Select.LazyResultSelect lazyResult, long offset, boolean quickOffset)

pack-priv void
updateAgg(int
number of columns
columnCount
,
int
see STAGE_RESET/STAGE_GROUP/STAGE_WINDOW in DataAnalysisOperation
stage
)

Update any aggregate expressions with the query stage.

public void
updateAggregate(SessionLocal
the session
s
,
int
select stage
stage
)

Implements abstract org.h2.command.query.Query.updateAggregate.

Update all aggregate function values.

Inherited from org.h2.command.query.Query:
addParameterappendEndOfQueryToSQLcleanupOrdercollectDependenciesconvertToDistinctdisableCacheexistsfinishResultgetColumnCountgetCostAsExpressiongetExpressionsgetFetchgetMaxDataModificationIdgetOffsetgetOffsetFetchgetOuterQueryScopegetRowDataTypegetTypehasOrderinitExpressioninitOrderisFetchPercentisNeverLazyisQueryisRandomAccessResultisReadOnlyisRetryableisStandardDistinctisTransactionalisWithTiesprepareprepareOrderqueryqueryqueryMetasetDistinctIfPossiblesetFetchsetFetchPercentsetNeverLazysetOffsetsetOrdersetOuterQueryScopesetRandomAccessResultsetWithClausesetWithTiestoTablewriteWithList