Top Description Fields Constructors Methods
org.h2.expression.analysis

public abstract Class DataAnalysisOperation

extends Expression
Class Inheritance
Known Direct Subclasses
org.h2.expression.analysis.WindowFunction, org.h2.expression.aggregate.AbstractAggregate
Imports
java.util.ArrayList, .HashMap, org.h2.api.ErrorCode, org.h2.command.query.QueryOrderBy, .Select, .SelectGroups, org.h2.engine.SessionLocal, org.h2.expression.Expression, .ExpressionVisitor, org.h2.message.DbException, org.h2.result.SortOrder, org.h2.table.ColumnResolver, .TableFilter, org.h2.value.Value, .ValueInteger

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

Field Summary

Modifier and TypeField and Description
private int
private int
protected Window
over

OVER clause

protected SortOrder
overOrderBySort

Sort order for OVER

protected final Select
select

SELECT

public static final int
STAGE_GROUP

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

public static final int
STAGE_RESET

Reset stage.

public static final int
STAGE_WINDOW

Window processing stage.

Inherited from org.h2.expression.Expression:
AUTO_PARENTHESESMAP_IN_AGGREGATEMAP_IN_WINDOWMAP_INITIALWITH_PARENTHESESWITHOUT_PARENTHESES

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Modifier and TypeMethod and Description
protected StringBuilder

Returns:

the builder object
appendTailConditions
(StringBuilder
string builder
builder
,
int
formatting flags
sqlFlags
,
boolean
whether synthetic ORDER BY clause should be generated when it is missing
forceOrderBy
)

Used to create SQL for the OVER and FILTER clauses.

private void
checkOrderBy(WindowFrameUnits units, int orderBySize)

protected abstract Object

Returns:

aggregate-specific data object.
createAggregateData
()

Create aggregate data object specific to the subclass.

protected static SortOrder

Returns:

the SortOrder
createOrder
(SessionLocal
database session
session
,
ArrayList<QueryOrderBy>
array of order by expressions
orderBy
,
int
index offset
offset
)

Create sort order.

protected abstract Value

Returns:

aggregated value.
getAggregatedValue
(SessionLocal
the session
session
,
Object
the aggregate data
aggregateData
)

Returns aggregated value.

protected Object

Returns:

group data object
getGroupData
(SelectGroups
the collector object
groupData
,
boolean
if true, return null if object not found, if false, return new object if nothing found
ifExists
)

Get the aggregate group data object from the collector object.

protected abstract int

Returns:

the number of expressions
getNumExpressions
()

Returns the number of expressions, excluding OVER clause.

private int

Returns:

the number of window frame expressions
getNumFrameExpressions
()

Returns the number of window frame expressions.

private Value
getOrderedResult(SessionLocal session, SelectGroups groupData, PartitionData partition, Object data)

protected abstract void
getOrderedResultLoop(SessionLocal
the session
session
,
HashMap<Integer, Value>
the map to append result to
result
,
ArrayList<Value[]>
ordered data
ordered
,
int
the index of row id value
rowIdColumn
)

Returns result of this window function or window aggregate.

public Window

Returns:

the OVER condition
getOverCondition
()

Returns the OVER condition.

protected SortOrder

Returns:

the sort order for OVER clause
getOverOrderBySort
()

Returns the sort order for OVER clause.

public Value
getValue(SessionLocal
the session
session
)

Implements abstract org.h2.expression.Expression.getValue.

Return the resulting value for the current row.

protected Object

Returns:

the aggregate data object, specific to each kind of aggregate.
getWindowData
(SessionLocal
database session
session
,
SelectGroups
aggregate group data
groupData
,
boolean
true if this is for ORDER BY
forOrderBy
)

Get the aggregate data for a window clause.

private Value

Returns:

result of this function
getWindowResult
(SessionLocal
the session
session
,
SelectGroups
the group data
groupData
)

Returns result of this window function or window aggregate.

public abstract boolean

Returns:

true if this is an aggregate function (including aggregates with OVER clause), false if this is a window function
isAggregate
()

Checks whether this expression is an aggregate function.

public boolean
isEverything(ExpressionVisitor
the visitor
visitor
)

Implements abstract org.h2.expression.Expression.isEverything.

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

public final void
mapColumns(ColumnResolver
the column resolver
resolver
,
int
the subquery nesting level
level
,
int
current state for nesting checks, initial value is MAP_INITIAL
state
)

Implements abstract org.h2.expression.Expression.mapColumns.

Map the columns of the resolver to expression columns.

protected void
mapColumnsAnalysis(ColumnResolver
the column resolver
resolver
,
int
the subquery nesting level
level
,
int
one of the Expression MAP_IN_* values
innerState
)

Map the columns of the resolver to expression columns.

public Expression
optimize(SessionLocal
the session
session
)

Implements abstract org.h2.expression.Expression.optimize.

Try to optimize the expression.

protected abstract void
rememberExpressions(SessionLocal
the session
session
,
Value[]
array to store values of expressions
array
)

Stores current values of expressions into the specified array.

public void
setEvaluatable(TableFilter
the table filter
tableFilter
,
boolean
true if the table filter can return value
b
)

Implements abstract org.h2.expression.Expression.setEvaluatable.

Tell the expression columns whether the table filter can return values now.

public void
setOverCondition(Window
OVER condition
over
)

Sets the OVER condition.

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

Implements abstract org.h2.expression.Expression.updateAggregate.

Update an aggregate value.

protected abstract void
updateAggregate(SessionLocal
the database session
session
,
SelectGroups
data for the aggregate group
groupData
,
int
row id of group
groupRowId
)

Update a row of an aggregate.

protected void
updateGroupAggregates(SessionLocal
the session
session
,
int
select stage
stage
)

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

protected void
updateOrderedAggregate(SessionLocal
the database session
session
,
SelectGroups
data for the aggregate group
groupData
,
int
row id of group
groupRowId
,
ArrayList<QueryOrderBy>
list of order by expressions
orderBy
)

Update a row of an ordered aggregate.

Inherited from org.h2.expression.Expression:
addFilterConditionscreateIndexConditionsgetAliasgetBooleanValuegetColumnNamegetColumnNameForViewgetCostgetEnclosedSQLgetNonAliasExpressiongetNotIfPossiblegetNullablegetSchemaNamegetSQLgetSQLgetSQLgetSQLgetSubexpressiongetSubexpressionCountgetTableAliasgetTableNamegetTypegetTypeIfStaticallyKnowngetUnenclosedSQLgetWhenSQLgetWhenValueisConstantisIdentityisNullConstantisValueSetisWhenConditionOperandneedParenthesesoptimizeConditiontoStringwriteExpressionswriteExpressions

Field Detail

lastGroupRowIdback to summary
private int lastGroupRowId
numFrameExpressionsback to summary
private int numFrameExpressions
overback to summary
protected Window over

OVER clause

overOrderBySortback to summary
protected SortOrder overOrderBySort

Sort order for OVER

selectback to summary
protected final Select select

SELECT

STAGE_GROUPback to summary
public static final int STAGE_GROUP

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

STAGE_RESETback to summary
public static final int STAGE_RESET

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

STAGE_WINDOWback to summary
public static final int STAGE_WINDOW

Window processing stage.

Constructor Detail

DataAnalysisOperationback to summary
protected DataAnalysisOperation(Select select)

Method Detail

appendTailConditionsback to summary
protected StringBuilder appendTailConditions(StringBuilder builder, int sqlFlags, boolean forceOrderBy)

Used to create SQL for the OVER and FILTER clauses.

Parameters
builder:StringBuilder

string builder

sqlFlags:int

formatting flags

forceOrderBy:boolean

whether synthetic ORDER BY clause should be generated when it is missing

Returns:StringBuilder

the builder object

checkOrderByback to summary
private void checkOrderBy(WindowFrameUnits units, int orderBySize)
createAggregateDataback to summary
protected abstract Object createAggregateData()

Create aggregate data object specific to the subclass.

Returns:Object

aggregate-specific data object.

createOrderback to summary
protected static SortOrder createOrder(SessionLocal session, ArrayList<QueryOrderBy> orderBy, int offset)

Create sort order.

Parameters
session:SessionLocal

database session

orderBy:ArrayList<QueryOrderBy>

array of order by expressions

offset:int

index offset

Returns:SortOrder

the SortOrder

getAggregatedValueback to summary
protected abstract Value getAggregatedValue(SessionLocal session, Object aggregateData)

Returns aggregated value.

Parameters
session:SessionLocal

the session

aggregateData:Object

the aggregate data

Returns:Value

aggregated value.

getGroupDataback to summary
protected Object getGroupData(SelectGroups groupData, boolean ifExists)

Get the aggregate group data object from the collector object.

Parameters
groupData:SelectGroups

the collector object

ifExists:boolean

if true, return null if object not found, if false, return new object if nothing found

Returns:Object

group data object

getNumExpressionsback to summary
protected abstract int getNumExpressions()

Returns the number of expressions, excluding OVER clause.

Returns:int

the number of expressions

getNumFrameExpressionsback to summary
private int getNumFrameExpressions()

Returns the number of window frame expressions.

Returns:int

the number of window frame expressions

getOrderedResultback to summary
private Value getOrderedResult(SessionLocal session, SelectGroups groupData, PartitionData partition, Object data)
getOrderedResultLoopback to summary
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:SessionLocal

the session

result:HashMap<Integer, Value>

the map to append result to

ordered:ArrayList<Value[]>

ordered data

rowIdColumn:int

the index of row id value

getOverConditionback to summary
public Window getOverCondition()

Returns the OVER condition.

Returns:Window

the OVER condition

getOverOrderBySortback to summary
protected SortOrder getOverOrderBySort()

Returns the sort order for OVER clause.

Returns:SortOrder

the sort order for OVER clause

getValueback to summary
public Value getValue(SessionLocal session)

Implements abstract org.h2.expression.Expression.getValue.

Doc from org.h2.expression.Expression.getValue.

Return the resulting value for the current row.

Parameters
session:SessionLocal

the session

Returns:Value

the result

Annotations
@Override
getWindowDataback to summary
protected Object getWindowData(SessionLocal session, SelectGroups groupData, boolean forOrderBy)

Get the aggregate data for a window clause.

Parameters
session:SessionLocal

database session

groupData:SelectGroups

aggregate group data

forOrderBy:boolean

true if this is for ORDER BY

Returns:Object

the aggregate data object, specific to each kind of aggregate.

getWindowResultback to summary
private Value getWindowResult(SessionLocal session, SelectGroups groupData)

Returns result of this window function or window aggregate. This method is not used for plain aggregates.

Parameters
session:SessionLocal

the session

groupData:SelectGroups

the group data

Returns:Value

result of this function

isAggregateback to summary
public abstract boolean isAggregate()

Checks whether this expression is an aggregate function.

Returns:boolean

true if this is an aggregate function (including aggregates with OVER clause), false if this is a window function

isEverythingback to summary
public boolean isEverything(ExpressionVisitor visitor)

Implements abstract org.h2.expression.Expression.isEverything.

Doc from org.h2.expression.Expression.isEverything.

Check if this expression and all sub-expressions can fulfill a criteria. If any part returns false, the result is false.

Parameters
visitor:ExpressionVisitor

the visitor

Returns:boolean

if the criteria can be fulfilled

Annotations
@Override
mapColumnsback to summary
public final void mapColumns(ColumnResolver resolver, int level, int state)

Implements abstract org.h2.expression.Expression.mapColumns.

Doc from org.h2.expression.Expression.mapColumns.

Map the columns of the resolver to expression columns.

Parameters
resolver:ColumnResolver

the column resolver

level:int

the subquery nesting level

state:int

current state for nesting checks, initial value is MAP_INITIAL

Annotations
@Override
mapColumnsAnalysisback to summary
protected void mapColumnsAnalysis(ColumnResolver resolver, int level, int innerState)

Map the columns of the resolver to expression columns.

Parameters
resolver:ColumnResolver

the column resolver

level:int

the subquery nesting level

innerState:int

one of the Expression MAP_IN_* values

optimizeback to summary
public Expression optimize(SessionLocal session)

Implements abstract org.h2.expression.Expression.optimize.

Doc from org.h2.expression.Expression.optimize.

Try to optimize the expression.

Parameters
session:SessionLocal

the session

Returns:Expression

the optimized expression

Annotations
@Override
rememberExpressionsback to summary
protected abstract void rememberExpressions(SessionLocal session, Value[] array)

Stores current values of expressions into the specified array.

Parameters
session:SessionLocal

the session

array:Value[]

array to store values of expressions

setEvaluatableback to summary
public void setEvaluatable(TableFilter tableFilter, boolean b)

Implements abstract org.h2.expression.Expression.setEvaluatable.

Doc from org.h2.expression.Expression.setEvaluatable.

Tell the expression columns whether the table filter can return values now. This is used when optimizing the query.

Parameters
tableFilter:TableFilter

the table filter

b:boolean

true if the table filter can return value

Annotations
@Override
setOverConditionback to summary
public void setOverCondition(Window over)

Sets the OVER condition.

Parameters
over:Window

OVER condition

updateAggregateback to summary
public final void updateAggregate(SessionLocal session, int stage)

Implements abstract org.h2.expression.Expression.updateAggregate.

Doc from org.h2.expression.Expression.updateAggregate.

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.

Parameters
session:SessionLocal

the session

stage:int

select stage

Annotations
@Override
updateAggregateback to summary
protected abstract void updateAggregate(SessionLocal session, SelectGroups groupData, int groupRowId)

Update a row of an aggregate.

Parameters
session:SessionLocal

the database session

groupData:SelectGroups

data for the aggregate group

groupRowId:int

row id of group

updateGroupAggregatesback to summary
protected void updateGroupAggregates(SessionLocal session, int stage)

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

Parameters
session:SessionLocal

the session

stage:int

select stage

updateOrderedAggregateback to summary
protected void updateOrderedAggregate(SessionLocal session, SelectGroups groupData, int groupRowId, ArrayList<QueryOrderBy> orderBy)

Update a row of an ordered aggregate.

Parameters
session:SessionLocal

the database session

groupData:SelectGroups

data for the aggregate group

groupRowId:int

row id of group

orderBy:ArrayList<QueryOrderBy>

list of order by expressions