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

public Class Aggregate

extends AbstractAggregate
implements ExpressionWithFlags
Class Inheritance
All Implemented Interfaces
org.h2.expression.ExpressionWithFlags
Imports
java.io.ByteArrayOutputStream, java.math.BigDecimal, java.util.ArrayList, .Arrays, .Comparator, .HashMap, .Iterator, .LinkedHashMap, .Map.Entry, .TreeMap, org.h2.api.ErrorCode, org.h2.command.query.QueryOrderBy, .Select, org.h2.engine.Constants, .Database, .SessionLocal, org.h2.expression.Expression, .ExpressionColumn, .ExpressionVisitor, .ExpressionWithFlags, .ValueExpression, org.h2.expression.aggregate.AggregateDataCollecting.NullCollectionMode, org.h2.expression.analysis.Window, org.h2.expression.function.BitFunction, .JsonConstructorFunction, org.h2.index.Cursor, .Index, org.h2.message.DbException, org.h2.mvstore.db.MVSpatialIndex, org.h2.result.SearchRow, .SortOrder, org.h2.table.Column, .ColumnResolver, .Table, .TableFilter, org.h2.util.StringUtils, org.h2.util.json.JsonConstructorUtils, org.h2.value.CompareMode, .DataType, .ExtTypeInfoRow, .TypeInfo, .Value, .ValueArray, .ValueBigint, .ValueBoolean, .ValueDouble, .ValueInteger, .ValueInterval, .ValueJson, .ValueNull, .ValueNumeric, .ValueRow, .ValueVarchar

Implements the integrated aggregate functions, such as COUNT, MAX, SUM.

Field Summary

Modifier and TypeField and Description
private static final int
ADDITIONAL_AVG_SCALE

The additional precision and scale in decimal digits for an AVG aggregate function.

private static final int
ADDITIONAL_SUM_PRECISION

The additional result precision in decimal digits for a SUM aggregate function.

private static final HashMap<String, AggregateType>
private final AggregateType
private Object
private int
private ArrayList<QueryOrderBy>
private SortOrder
Inherited from org.h2.expression.aggregate.AbstractAggregate:
argsdistinctfilterConditiontype

Constructor Summary

AccessConstructor and Description
public
Aggregate(AggregateType
the aggregate type
aggregateType
,
Expression[]
the aggregated expressions
args
,
Select
the select statement
select
,
boolean
if distinct is used
distinct
)

Create a new aggregate object.

Method Summary

Modifier and TypeMethod and Description
private static void
private static Value
protected Object
createAggregateData()

Implements abstract org.h2.expression.analysis.DataAnalysisOperation.createAggregateData.

Create aggregate data object specific to the subclass.

public Value
getAggregatedValue(SessionLocal
the session
session
,
Object
the aggregate data
aggregateData
)

Implements abstract org.h2.expression.analysis.DataAnalysisOperation.getAggregatedValue.

Returns aggregated value.

public static AggregateType

Returns:

null if no aggregate function has been found, or the aggregate type
getAggregateType
(String
the aggregate function name
name
)

Get the aggregate type for this name, or -1 if no aggregate has been found.

public AggregateType

Returns:

the type of this aggregate
getAggregateType
()

Returns the type of this aggregate.

private static TypeInfo
public int
getCost()

Implements abstract org.h2.expression.Expression.getCost.

Estimate the cost to process the expression.

private static Value
getCumeDist(Value[] ordered, Value arg, Comparator<Value> sort)

public Object

Returns:

the additional arguments
getExtraArguments
()

Returns the additional arguments.

public int
private Value
private Value
private Value
private StringBuilder
getListaggError(Value[] array, String separator)

private String
private StringBuilder
getListaggTruncate(Value[] array, String separator, String filter, boolean withoutCount)

private Index
private Value
protected int
getNumExpressions()

Implements abstract org.h2.expression.analysis.DataAnalysisOperation.getNumExpressions.

Returns the number of expressions, excluding OVER clause.

private Value
getRank(Value[] ordered, Value arg, Comparator<Value> sort)

private Value
getSecondValue(SessionLocal session, Value[] remembered)

public Select

Returns:

the select statement
getSelect
()

Returns the select statement.

private StringBuilder
getSQLArrayAggregate(StringBuilder builder, int sqlFlags)

private StringBuilder
getSQLJsonArrayAggregate(StringBuilder builder, int sqlFlags)

private StringBuilder
private StringBuilder
getSQLListagg(StringBuilder builder, int sqlFlags)

private static TypeInfo
public StringBuilder
getUnenclosedSQL(StringBuilder
string builder
builder
,
int
formatting flags
sqlFlags
)

Implements abstract org.h2.expression.Expression.getUnenclosedSQL.

Get the SQL statement of this expression.

public Value
getValue(SessionLocal
the session
session
)

Overrides org.h2.expression.analysis.DataAnalysisOperation.getValue.

Return the resulting value for the current row.

private Value
public boolean

Returns:

if distinct is used
isDistinct
()

Returns if distinct is used.

public boolean
isEverything(ExpressionVisitor
the visitor
visitor
)

Overrides org.h2.expression.analysis.DataAnalysisOperation.isEverything.

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

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

Overrides org.h2.expression.aggregate.AbstractAggregate.mapColumnsAnalysis.

Map the columns of the resolver to expression columns.

public Expression
optimize(SessionLocal
the session
session
)

Overrides org.h2.expression.aggregate.AbstractAggregate.optimize.

Try to optimize the expression.

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

Implements abstract org.h2.expression.analysis.DataAnalysisOperation.rememberExpressions.

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
)

Overrides org.h2.expression.aggregate.AbstractAggregate.setEvaluatable.

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

public void
setExtraArguments(Object
the additional arguments
extraArguments
)

Sets the additional arguments.

public void
setFlags(int
the flags to set
flags
)

Implements org.h2.expression.ExpressionWithFlags.setFlags.

Set the flags for this expression.

public void
setOrderByList(ArrayList<QueryOrderBy>
the order by list
orderByList
)

Set the order for ARRAY_AGG() or GROUP_CONCAT() aggregate.

private void
protected void
updateAggregate(SessionLocal
the session
session
,
Object
aggregate data
aggregateData
)

Implements abstract org.h2.expression.aggregate.AbstractAggregate.updateAggregate.

Updates an aggregate value.

private Value
updateCollecting(SessionLocal session, Value v, Value[] remembered)

private void
updateData(SessionLocal session, AggregateData data, Value v, Value[] remembered)

protected void
updateFromExpressions(SessionLocal
the session
session
,
Object
aggregate data
aggregateData
,
Value[]
values of expressions
array
)

Implements abstract org.h2.expression.aggregate.AbstractAggregate.updateFromExpressions.

Updates the provided aggregate data from the remembered expressions.

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

Overrides org.h2.expression.aggregate.AbstractAggregate.updateGroupAggregates.

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

Inherited from org.h2.expression.aggregate.AbstractAggregate:
appendTailConditionsgetFilterConditiongetOrderedResultLoopgetSubexpressiongetSubexpressionCountgetTypeisAggregatesetFilterConditionupdateAggregate

Field Detail

ADDITIONAL_AVG_SCALEback to summary
private static final int ADDITIONAL_AVG_SCALE

The additional precision and scale in decimal digits for an AVG aggregate function.

ADDITIONAL_SUM_PRECISIONback to summary
private static final int ADDITIONAL_SUM_PRECISION

The additional result precision in decimal digits for a SUM aggregate function.

AGGREGATESback to summary
private static final HashMap<String, AggregateType> AGGREGATES
aggregateTypeback to summary
private final AggregateType aggregateType
extraArgumentsback to summary
private Object extraArguments
flagsback to summary
private int flags
orderByListback to summary
private ArrayList<QueryOrderBy> orderByList
orderBySortback to summary
private SortOrder orderBySort

Constructor Detail

Aggregateback to summary
public Aggregate(AggregateType aggregateType, Expression[] args, Select select, boolean distinct)

Create a new aggregate object.

Parameters
aggregateType:AggregateType

the aggregate type

args:Expression[]

the aggregated expressions

select:Select

the select statement

distinct:boolean

if distinct is used

Method Detail

addAggregateback to summary
private static void addAggregate(String name, AggregateType type)
collectback to summary
private static Value collect(SessionLocal session, AggregateDataCollecting c, AggregateData d)
createAggregateDataback to summary
protected Object createAggregateData()

Implements abstract org.h2.expression.analysis.DataAnalysisOperation.createAggregateData.

Doc from org.h2.expression.analysis.DataAnalysisOperation.createAggregateData.

Create aggregate data object specific to the subclass.

Returns:Object

aggregate-specific data object.

Annotations
@Override
getAggregatedValueback to summary
public Value getAggregatedValue(SessionLocal session, Object aggregateData)

Implements abstract org.h2.expression.analysis.DataAnalysisOperation.getAggregatedValue.

Doc from org.h2.expression.analysis.DataAnalysisOperation.getAggregatedValue.

Returns aggregated value.

Parameters
session:SessionLocal

the session

aggregateData:Object

the aggregate data

Returns:Value

aggregated value.

Annotations
@Override
getAggregateTypeback to summary
public static AggregateType getAggregateType(String name)

Get the aggregate type for this name, or -1 if no aggregate has been found.

Parameters
name:String

the aggregate function name

Returns:AggregateType

null if no aggregate function has been found, or the aggregate type

getAggregateTypeback to summary
public AggregateType getAggregateType()

Returns the type of this aggregate.

Returns:AggregateType

the type of this aggregate

getAvgTypeback to summary
private static TypeInfo getAvgType(TypeInfo type)
getCostback to summary
public int getCost()

Implements abstract org.h2.expression.Expression.getCost.

Doc from org.h2.expression.Expression.getCost.

Estimate the cost to process the expression. Used when optimizing the query, to calculate the query plan with the lowest estimated cost.

Returns:int

the estimated cost

Annotations
@Override
getCumeDistback to summary
private static Value getCumeDist(Value[] ordered, Value arg, Comparator<Value> sort)
getExtraArgumentsback to summary
public Object getExtraArguments()

Returns the additional arguments.

Returns:Object

the additional arguments

getFlagsback to summary
public int getFlags()

Implements org.h2.expression.ExpressionWithFlags.getFlags.

Doc from org.h2.expression.ExpressionWithFlags.getFlags.

Returns the flags.

Returns:int

the flags

Annotations
@Override
getHistogramback to summary
private Value getHistogram(SessionLocal session, AggregateData data)
getHypotheticalSetback to summary
private Value getHypotheticalSet(SessionLocal session, AggregateData data)
getListaggback to summary
private Value getListagg(SessionLocal session, AggregateData data)
getListaggErrorback to summary
private StringBuilder getListaggError(Value[] array, String separator)
getListaggItemback to summary
private String getListaggItem(Value v)
getListaggTruncateback to summary
private StringBuilder getListaggTruncate(Value[] array, String separator, String filter, boolean withoutCount)
getMinMaxColumnIndexback to summary
private Index getMinMaxColumnIndex()
getModeback to summary
private Value getMode(SessionLocal session, AggregateData data)
getNumExpressionsback to summary
protected int getNumExpressions()

Implements abstract org.h2.expression.analysis.DataAnalysisOperation.getNumExpressions.

Doc from org.h2.expression.analysis.DataAnalysisOperation.getNumExpressions.

Returns the number of expressions, excluding OVER clause.

Returns:int

the number of expressions

Annotations
@Override
getRankback to summary
private Value getRank(Value[] ordered, Value arg, Comparator<Value> sort)
getSecondValueback to summary
private Value getSecondValue(SessionLocal session, Value[] remembered)
getSelectback to summary
public Select getSelect()

Returns the select statement.

Returns:Select

the select statement

getSQLArrayAggregateback to summary
private StringBuilder getSQLArrayAggregate(StringBuilder builder, int sqlFlags)
getSQLJsonArrayAggregateback to summary
private StringBuilder getSQLJsonArrayAggregate(StringBuilder builder, int sqlFlags)
getSQLJsonObjectAggregateback to summary
private StringBuilder getSQLJsonObjectAggregate(StringBuilder builder, int sqlFlags)
getSQLListaggback to summary
private StringBuilder getSQLListagg(StringBuilder builder, int sqlFlags)
getSumTypeback to summary
private static TypeInfo getSumType(TypeInfo type)
getUnenclosedSQLback to summary
public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags)

Implements abstract org.h2.expression.Expression.getUnenclosedSQL.

Doc from org.h2.expression.Expression.getUnenclosedSQL.

Get the SQL statement of this expression. This may not always be the original SQL statement, especially after optimization. Enclosing '(' and ')' are never appended.

Parameters
builder:StringBuilder

string builder

sqlFlags:int

formatting flags

Returns:StringBuilder

the specified string builder

Annotations
@Override
getValueback to summary
public Value getValue(SessionLocal session)

Overrides org.h2.expression.analysis.DataAnalysisOperation.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
getValueQuickback to summary
private Value getValueQuick(SessionLocal session)
isDistinctback to summary
public boolean isDistinct()

Returns if distinct is used.

Returns:boolean

if distinct is used

isEverythingback to summary
public boolean isEverything(ExpressionVisitor visitor)

Overrides org.h2.expression.analysis.DataAnalysisOperation.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
mapColumnsAnalysisback to summary
public void mapColumnsAnalysis(ColumnResolver resolver, int level, int innerState)

Overrides org.h2.expression.aggregate.AbstractAggregate.mapColumnsAnalysis.

Doc from org.h2.expression.analysis.DataAnalysisOperation.mapColumnsAnalysis.

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

Annotations
@Override
optimizeback to summary
public Expression optimize(SessionLocal session)

Overrides org.h2.expression.aggregate.AbstractAggregate.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 void rememberExpressions(SessionLocal session, Value[] array)

Implements abstract org.h2.expression.analysis.DataAnalysisOperation.rememberExpressions.

Doc from org.h2.expression.analysis.DataAnalysisOperation.rememberExpressions.

Stores current values of expressions into the specified array.

Parameters
session:SessionLocal

the session

array:Value[]

array to store values of expressions

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

Overrides org.h2.expression.aggregate.AbstractAggregate.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
setExtraArgumentsback to summary
public void setExtraArguments(Object extraArguments)

Sets the additional arguments.

Parameters
extraArguments:Object

the additional arguments

setFlagsback to summary
public void setFlags(int flags)

Implements org.h2.expression.ExpressionWithFlags.setFlags.

Doc from org.h2.expression.ExpressionWithFlags.setFlags.

Set the flags for this expression.

Parameters
flags:int

the flags to set

Annotations
@Override
setOrderByListback to summary
public void setOrderByList(ArrayList<QueryOrderBy> orderByList)

Set the order for ARRAY_AGG() or GROUP_CONCAT() aggregate.

Parameters
orderByList:ArrayList<QueryOrderBy>

the order by list

sortWithOrderByback to summary
private void sortWithOrderBy(Value[] array)
updateAggregateback to summary
protected void updateAggregate(SessionLocal session, Object aggregateData)

Implements abstract org.h2.expression.aggregate.AbstractAggregate.updateAggregate.

Doc from org.h2.expression.aggregate.AbstractAggregate.updateAggregate.

Updates an aggregate value.

Parameters
session:SessionLocal

the session

aggregateData:Object

aggregate data

Annotations
@Override
updateCollectingback to summary
private Value updateCollecting(SessionLocal session, Value v, Value[] remembered)
updateDataback to summary
private void updateData(SessionLocal session, AggregateData data, Value v, Value[] remembered)
updateFromExpressionsback to summary
protected void updateFromExpressions(SessionLocal session, Object aggregateData, Value[] array)

Implements abstract org.h2.expression.aggregate.AbstractAggregate.updateFromExpressions.

Doc from org.h2.expression.aggregate.AbstractAggregate.updateFromExpressions.

Updates the provided aggregate data from the remembered expressions.

Parameters
session:SessionLocal

the session

aggregateData:Object

aggregate data

array:Value[]

values of expressions

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

Overrides org.h2.expression.aggregate.AbstractAggregate.updateGroupAggregates.

Doc from org.h2.expression.analysis.DataAnalysisOperation.updateGroupAggregates.

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

Parameters
session:SessionLocal

the session

stage:int

select stage

Annotations
@Override