Package org.h2.expression.analysis
Class DataAnalysisOperation
java.lang.Object
org.h2.expression.Expression
org.h2.expression.analysis.DataAnalysisOperation
- Direct Known Subclasses:
AbstractAggregate,WindowFunction
A base class for data analysis operations such as aggregates and window
functions.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected WindowOVER clauseprotected SortOrderSort order for OVERprotected final SelectSELECTstatic final intGroup stage, used for explicit or implicit GROUP BY operation.static final intReset stage.static final intWindow processing stage.Fields inherited from class org.h2.expression.Expression
AUTO_PARENTHESES, MAP_IN_AGGREGATE, MAP_IN_WINDOW, MAP_INITIAL, WITH_PARENTHESES, WITHOUT_PARENTHESESFields inherited from interface org.h2.util.HasSQL
ADD_PLAN_INFORMATION, DEFAULT_SQL_FLAGS, NO_CASTS, QUOTE_ONLY_WHEN_REQUIRED, REPLACE_LOBS_FOR_TRACE, TRACE_SQL_FLAGS -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected StringBuilderappendTailConditions(StringBuilder builder, int sqlFlags, boolean forceOrderBy) Used to create SQL for the OVER and FILTER clauses.protected abstract ObjectCreate aggregate data object specific to the subclass.protected static SortOrdercreateOrder(SessionLocal session, ArrayList<QueryOrderBy> orderBy, int offset) Create sort order.protected abstract ValuegetAggregatedValue(SessionLocal session, Object aggregateData) Returns aggregated value.protected ObjectgetGroupData(SelectGroups groupData, boolean ifExists) Get the aggregate group data object from the collector object.protected abstract intReturns the number of expressions, excluding OVER clause.protected abstract voidgetOrderedResultLoop(SessionLocal session, HashMap<Integer, Value> result, ArrayList<Value[]> ordered, int rowIdColumn) Returns result of this window function or window aggregate.Returns the OVER condition.protected SortOrderReturns the sort order for OVER clause.getValue(SessionLocal session) Return the resulting value for the current row.protected ObjectgetWindowData(SessionLocal session, SelectGroups groupData, boolean forOrderBy) Get the aggregate data for a window clause.abstract booleanChecks whether this expression is an aggregate function.booleanisEverything(ExpressionVisitor visitor) Check if this expression and all sub-expressions can fulfill a criteria.final voidmapColumns(ColumnResolver resolver, int level, int state) Map the columns of the resolver to expression columns.protected voidmapColumnsAnalysis(ColumnResolver resolver, int level, int innerState) Map the columns of the resolver to expression columns.optimize(SessionLocal session) Try to optimize the expression.protected abstract voidrememberExpressions(SessionLocal session, Value[] array) Stores current values of expressions into the specified array.voidsetEvaluatable(TableFilter tableFilter, boolean b) Tell the expression columns whether the table filter can return values now.voidsetOverCondition(Window over) Sets the OVER condition.final voidupdateAggregate(SessionLocal session, int stage) Update an aggregate value.protected abstract voidupdateAggregate(SessionLocal session, SelectGroups groupData, int groupRowId) Update a row of an aggregate.protected voidupdateGroupAggregates(SessionLocal session, int stage) Invoked when processing group stage of grouped window queries to update arguments of this aggregate.protected voidupdateOrderedAggregate(SessionLocal session, SelectGroups groupData, int groupRowId, ArrayList<QueryOrderBy> orderBy) Update a row of an ordered aggregate.Methods inherited from class org.h2.expression.Expression
addFilterConditions, createIndexConditions, getAlias, getBooleanValue, getColumnName, getColumnNameForView, getCost, getEnclosedSQL, getNonAliasExpression, getNotIfPossible, getNullable, getSchemaName, getSQL, getSQL, getSQL, getSQL, getSubexpression, getSubexpressionCount, getTableAlias, getTableName, getType, getTypeIfStaticallyKnown, getUnenclosedSQL, getWhenSQL, getWhenValue, isConstant, isIdentity, isNullConstant, isValueSet, isWhenConditionOperand, needParentheses, optimizeCondition, toString, writeExpressions, writeExpressionsMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.h2.util.HasSQL
getTraceSQL
-
Field Details
-
STAGE_RESET
public static final int STAGE_RESETReset stage. Used to reset internal data to its initial state.- See Also:
-
STAGE_GROUP
public static final int STAGE_GROUPGroup stage, used for explicit or implicit GROUP BY operation.- See Also:
-
STAGE_WINDOW
public static final int STAGE_WINDOWWindow processing stage.- See Also:
-
select
SELECT -
over
OVER clause -
overOrderBySort
Sort order for OVER
-
-
Constructor Details
-
DataAnalysisOperation
-
-
Method Details
-
createOrder
protected static SortOrder createOrder(SessionLocal session, ArrayList<QueryOrderBy> orderBy, int offset) Create sort order.- Parameters:
session- database sessionorderBy- array of order by expressionsoffset- index offset- Returns:
- the SortOrder
-
getOverCondition
Returns the OVER condition.- Returns:
- the OVER condition
-
setOverCondition
Sets the OVER condition.- Parameters:
over- OVER condition
-
isAggregate
public abstract boolean isAggregate()Checks whether this expression is an aggregate function.- Returns:
- true if this is an aggregate function (including aggregates with OVER clause), false if this is a window function
-
getOverOrderBySort
Returns the sort order for OVER clause.- Returns:
- the sort order for OVER clause
-
mapColumns
Description copied from class:ExpressionMap the columns of the resolver to expression columns.- Specified by:
mapColumnsin classExpression- Parameters:
resolver- the column resolverlevel- the subquery nesting levelstate- current state for nesting checks, initial value isExpression.MAP_INITIAL
-
mapColumnsAnalysis
Map the columns of the resolver to expression columns.- Parameters:
resolver- the column resolverlevel- the subquery nesting levelinnerState- one of the Expression MAP_IN_* values
-
optimize
Description copied from class:ExpressionTry to optimize the expression.- Specified by:
optimizein classExpression- Parameters:
session- the session- Returns:
- the optimized expression
-
setEvaluatable
Description copied from class:ExpressionTell the expression columns whether the table filter can return values now. This is used when optimizing the query.- Specified by:
setEvaluatablein classExpression- Parameters:
tableFilter- the table filterb- true if the table filter can return value
-
updateAggregate
Description copied from class:ExpressionUpdate an aggregate value. This method is called at statement execution time. It is usually called once for each row, but if the expression is used multiple times (for example in the column list, and as part of the HAVING expression) it is called multiple times - the row counter needs to be used to make sure the internal state is only updated once.- Specified by:
updateAggregatein classExpression- Parameters:
session- the sessionstage- select stage
-
updateAggregate
protected abstract void updateAggregate(SessionLocal session, SelectGroups groupData, int groupRowId) Update a row of an aggregate.- Parameters:
session- the database sessiongroupData- data for the aggregate groupgroupRowId- row id of group
-
updateGroupAggregates
Invoked when processing group stage of grouped window queries to update arguments of this aggregate.- Parameters:
session- the sessionstage- select stage
-
getNumExpressions
protected abstract int getNumExpressions()Returns the number of expressions, excluding OVER clause.- Returns:
- the number of expressions
-
rememberExpressions
Stores current values of expressions into the specified array.- Parameters:
session- the sessionarray- array to store values of expressions
-
getWindowData
Get the aggregate data for a window clause.- Parameters:
session- database sessiongroupData- aggregate group dataforOrderBy- true if this is for ORDER BY- Returns:
- the aggregate data object, specific to each kind of aggregate.
-
getGroupData
Get the aggregate group data object from the collector object.- Parameters:
groupData- the collector objectifExists- if true, return null if object not found, if false, return new object if nothing found- Returns:
- group data object
-
createAggregateData
Create aggregate data object specific to the subclass.- Returns:
- aggregate-specific data object.
-
isEverything
Description copied from class:ExpressionCheck if this expression and all sub-expressions can fulfill a criteria. If any part returns false, the result is false.- Specified by:
isEverythingin classExpression- Parameters:
visitor- the visitor- Returns:
- if the criteria can be fulfilled
-
getValue
Description copied from class:ExpressionReturn the resulting value for the current row.- Specified by:
getValuein classExpression- Parameters:
session- the session- Returns:
- the result
-
getAggregatedValue
Returns aggregated value.- Parameters:
session- the sessionaggregateData- the aggregate data- Returns:
- aggregated value.
-
updateOrderedAggregate
protected void updateOrderedAggregate(SessionLocal session, SelectGroups groupData, int groupRowId, ArrayList<QueryOrderBy> orderBy) Update a row of an ordered aggregate.- Parameters:
session- the database sessiongroupData- data for the aggregate groupgroupRowId- row id of grouporderBy- list of order by expressions
-
getOrderedResultLoop
protected abstract void getOrderedResultLoop(SessionLocal session, HashMap<Integer, Value> result, ArrayList<Value[]> ordered, int rowIdColumn) Returns result of this window function or window aggregate. This method may not be called on window aggregate without window order clause.- Parameters:
session- the sessionresult- the map to append result toordered- ordered datarowIdColumn- the index of row id value
-
appendTailConditions
protected StringBuilder appendTailConditions(StringBuilder builder, int sqlFlags, boolean forceOrderBy) Used to create SQL for the OVER and FILTER clauses.- Parameters:
builder- string buildersqlFlags- formatting flagsforceOrderBy- whether synthetic ORDER BY clause should be generated when it is missing- Returns:
- the builder object
-