org.h2.expression.analysis

## Class DataAnalysisOperation

- java.lang.Object
- - org.h2.expression.Expression
  - - org.h2.expression.analysis.DataAnalysisOperation

Direct Known Subclasses:
  AbstractAggregate, WindowFunction

    public abstract class DataAnalysisOperation
    extends Expression

A base class for data analysis operations such as aggregates and window functions.

### Field Summary

Fields
`protected Window` `over` OVER clause
`protected SortOrder` `overOrderBySort` Sort order for OVER
`protected Select` `select` SELECT
`static int` `STAGE_GROUP` Group stage, used for explicit or implicit GROUP BY operation.
`static int` `STAGE_RESET` Reset stage.
`static int` `STAGE_WINDOW` Window processing stage.

### Fields inherited from class org.h2.expression.Expression

`MAP_IN_AGGREGATE, MAP_IN_WINDOW, MAP_INITIAL`

### Constructor Summary

Constructors
`protected` `DataAnalysisOperation(Select select)`

### Method Summary

Methods
`protected java.lang.StringBuilder` `appendTailConditions(java.lang.StringBuilder builder)` Used to create SQL for the OVER and FILTER clauses.
`protected abstract java.lang.Object` `createAggregateData()` Create aggregate data object specific to the subclass.
`protected static SortOrder` `createOrder(Session session, java.util.ArrayList<SelectOrderBy> orderBy, int offset)` Create sort order.
`protected abstract Value` `getAggregatedValue(Session session, java.lang.Object aggregateData)` Returns aggregated value.
`protected java.lang.Object` `getGroupData(SelectGroups groupData, boolean ifExists)` Get the aggregate group data object from the collector object.
`protected abstract int` `getNumExpressions()` Returns the number of expressions, excluding OVER clause.
`protected abstract void` `getOrderedResultLoop(Session session, java.util.HashMap<java.lang.Integer,Value> result, java.util.ArrayList<Value[]> ordered, int rowIdColumn)` Returns result of this window function or window aggregate.
`protected SortOrder` `getOverOrderBySort()` Returns the sort order for OVER clause.
`Value` `getValue(Session session)` Return the resulting value for the current row.
`protected java.lang.Object` `getWindowData(Session session, SelectGroups groupData, boolean forOrderBy)` Get the aggregate data for a window clause.
`abstract boolean` `isAggregate()` Checks whether this expression is an aggregate function.
`boolean` `isEverything(ExpressionVisitor visitor)` Check if this expression and all sub-expressions can fulfill a criteria.
`void` `mapColumns(ColumnResolver resolver, int level, int state)` Map the columns of the resolver to expression columns.
`protected void` `mapColumnsAnalysis(ColumnResolver resolver, int level, int innerState)` Map the columns of the resolver to expression columns.
`Expression` `optimize(Session session)` Try to optimize the expression.
`protected abstract void` `rememberExpressions(Session session, Value[] array)` Stores current values of expressions into the specified array.
`void` `setEvaluatable(TableFilter tableFilter, boolean b)` Tell the expression columns whether the table filter can return values now.
`void` `setOverCondition(Window over)` Sets the OVER condition.
`void` `updateAggregate(Session session, int stage)` Update an aggregate value.
`protected abstract void` `updateAggregate(Session session, SelectGroups groupData, int groupRowId)` Update a row of an aggregate.
`protected void` `updateGroupAggregates(Session session, int stage)` Invoked when processing group stage of grouped window queries to update arguments of this aggregate.
`protected void` `updateOrderedAggregate(Session session, SelectGroups groupData, int groupRowId, java.util.ArrayList<SelectOrderBy> orderBy)` Update a row of an ordered aggregate.

### Methods inherited from class org.h2.expression.Expression

`addFilterConditions, createIndexConditions, getAlias, getBooleanValue, getColumnName, getCost, getExpressionColumns, getExpressionColumns, getExpressionColumns, getNonAliasExpression, getNotIfPossible, getNullable, getSchemaName, getSQL, getSQL, getSubexpression, getSubexpressionCount, getTableAlias, getTableName, getType, getUnenclosedSQL, isAutoIncrement, isConstant, isGeneratedKey, isValueSet, toString, writeExpressions, writeExpressions`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Field Detail

#### STAGE_RESET

    public static final int STAGE_RESET

Reset stage. Used to reset internal data to its initial state.

See Also:
  Constant Field Values

#### STAGE_GROUP

    public static final int STAGE_GROUP

Group stage, used for explicit or implicit GROUP BY operation.

See Also:
  Constant Field Values

#### STAGE_WINDOW

    public static final int STAGE_WINDOW

Window processing stage.

See Also:
  Constant Field Values

#### select

    protected final Select select

SELECT

#### over

    protected Window over

OVER clause

#### overOrderBySort

    protected SortOrder overOrderBySort

Sort order for OVER

### Constructor Detail

#### DataAnalysisOperation

    protected DataAnalysisOperation(Select select)

### Method Detail

#### createOrder

    protected static SortOrder createOrder(Session session,
                        java.util.ArrayList<SelectOrderBy> orderBy,
                        int offset)

Create sort order.

Parameters:
  `session` - database session
  `orderBy` - array of order by expressions
  `offset` - index offset
Returns:
  the SortOrder

#### setOverCondition

    public void setOverCondition(Window over)

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

    protected SortOrder getOverOrderBySort()

Returns the sort order for OVER clause.

Returns:
  the sort order for OVER clause

#### mapColumns

    public final void mapColumns(ColumnResolver resolver,
                  int level,
                  int state)

Description copied from class: `Expression`
Map the columns of the resolver to expression columns.

Specified by:
  `mapColumns` in class `Expression`
Parameters:
  `resolver` - the column resolver
  `level` - the subquery nesting level
  `state` - current state for nesting checks, initial value is `Expression.MAP_INITIAL`

#### mapColumnsAnalysis

    protected void mapColumnsAnalysis(ColumnResolver resolver,
                          int level,
                          int innerState)

Map the columns of the resolver to expression columns.

Parameters:
  `resolver` - the column resolver
  `level` - the subquery nesting level
  `innerState` - one of the Expression MAP_IN_* values

#### optimize

    public Expression optimize(Session session)

Description copied from class: `Expression`
Try to optimize the expression.

Specified by:
  `optimize` in class `Expression`
Parameters:
  `session` - the session
Returns:
  the optimized expression

#### setEvaluatable

    public void setEvaluatable(TableFilter tableFilter,
                      boolean b)

Description copied from class: `Expression`
Tell the expression columns whether the table filter can return values now. This is used when optimizing the query.

Specified by:
  `setEvaluatable` in class `Expression`
Parameters:
  `tableFilter` - the table filter
  `b` - true if the table filter can return value

#### updateAggregate

    public final void updateAggregate(Session session,
                       int stage)

Description copied from class: `Expression`
Update 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:
  `updateAggregate` in class `Expression`
Parameters:
  `session` - the session
  `stage` - select stage

#### updateAggregate

    protected abstract void updateAggregate(Session session,
                       SelectGroups groupData,
                       int groupRowId)

Update a row of an aggregate.

Parameters:
  `session` - the database session
  `groupData` - data for the aggregate group
  `groupRowId` - row id of group

#### updateGroupAggregates

    protected void updateGroupAggregates(Session session,
                             int stage)

Invoked when processing group stage of grouped window queries to update arguments of this aggregate.

Parameters:
  `session` - the session
  `stage` - select stage

#### getNumExpressions

    protected abstract int getNumExpressions()

Returns the number of expressions, excluding OVER clause.

Returns:
  the number of expressions

#### rememberExpressions

    protected abstract void rememberExpressions(Session session,
                           Value[] array)

Stores current values of expressions into the specified array.

Parameters:
  `session` - the session
  `array` - array to store values of expressions

#### getWindowData

    protected java.lang.Object getWindowData(Session session,
                                 SelectGroups groupData,
                                 boolean forOrderBy)

Get the aggregate data for a window clause.

Parameters:
  `session` - database session
  `groupData` - aggregate group data
  `forOrderBy` - true if this is for ORDER BY
Returns:
  the aggregate data object, specific to each kind of aggregate.

#### getGroupData

    protected java.lang.Object getGroupData(SelectGroups groupData,
                                boolean ifExists)

Get the aggregate group data object from the collector object.

Parameters:
  `groupData` - the collector object
  `ifExists` - if true, return null if object not found, if false, return new object if nothing found
Returns:
  group data object

#### createAggregateData

    protected abstract java.lang.Object createAggregateData()

Create aggregate data object specific to the subclass.

Returns:
  aggregate-specific data object.

#### isEverything

    public boolean isEverything(ExpressionVisitor visitor)

Description copied from class: `Expression`
Check if this expression and all sub-expressions can fulfill a criteria. If any part returns false, the result is false.

Specified by:
  `isEverything` in class `Expression`
Parameters:
  `visitor` - the visitor
Returns:
  if the criteria can be fulfilled

#### getValue

    public Value getValue(Session session)

Description copied from class: `Expression`
Return the resulting value for the current row.

Specified by:
  `getValue` in class `Expression`
Parameters:
  `session` - the session
Returns:
  the result

#### getAggregatedValue

    protected abstract Value getAggregatedValue(Session session,
                           java.lang.Object aggregateData)

Returns aggregated value.

Parameters:
  `session` - the session
  `aggregateData` - the aggregate data
Returns:
  aggregated value.

#### updateOrderedAggregate

    protected void updateOrderedAggregate(Session session,
                              SelectGroups groupData,
                              int groupRowId,
                              java.util.ArrayList<SelectOrderBy> orderBy)

Update a row of an ordered aggregate.

Parameters:
  `session` - the database session
  `groupData` - data for the aggregate group
  `groupRowId` - row id of group
  `orderBy` - list of order by expressions

#### getOrderedResultLoop

    protected abstract void getOrderedResultLoop(Session session,
                            java.util.HashMap<java.lang.Integer,Value> result,
                            java.util.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 session
  `result` - the map to append result to
  `ordered` - ordered data
  `rowIdColumn` - the index of row id value

#### appendTailConditions

    protected java.lang.StringBuilder appendTailConditions(java.lang.StringBuilder builder)

Used to create SQL for the OVER and FILTER clauses.

Parameters:
  `builder` - string builder
Returns:
  the builder object