Package org.h2.expression.aggregate
Class Aggregate
java.lang.Object
org.h2.expression.Expression
org.h2.expression.analysis.DataAnalysisOperation
org.h2.expression.aggregate.AbstractAggregate
org.h2.expression.aggregate.Aggregate
- All Implemented Interfaces:
ExpressionWithFlags
,HasSQL
,Typed
Implements the integrated aggregate functions, such as COUNT, MAX, SUM.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
The additional precision and scale in decimal digits for an AVG aggregate function.private static final int
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
Fields inherited from class org.h2.expression.aggregate.AbstractAggregate
args, distinct, filterCondition, type
Fields inherited from class org.h2.expression.analysis.DataAnalysisOperation
over, overOrderBySort, select, STAGE_GROUP, STAGE_RESET, STAGE_WINDOW
Fields inherited from class org.h2.expression.Expression
AUTO_PARENTHESES, MAP_IN_AGGREGATE, MAP_IN_WINDOW, MAP_INITIAL, WITH_PARENTHESES, WITHOUT_PARENTHESES
Fields 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
ConstructorsConstructorDescriptionAggregate
(AggregateType aggregateType, Expression[] args, Select select, boolean distinct) Create a new aggregate object. -
Method Summary
Modifier and TypeMethodDescriptionprivate static void
addAggregate
(String name, AggregateType type) private static Value
collect
(SessionLocal session, AggregateDataCollecting c, AggregateData d) protected Object
Create aggregate data object specific to the subclass.getAggregatedValue
(SessionLocal session, Object aggregateData) Returns aggregated value.Returns the type of this aggregate.static AggregateType
getAggregateType
(String name) Get the aggregate type for this name, or -1 if no aggregate has been found.private static TypeInfo
getAvgType
(TypeInfo type) int
getCost()
Estimate the cost to process the expression.private static Value
getCumeDist
(Value[] ordered, Value arg, Comparator<Value> sort) Returns the additional arguments.int
getFlags()
Returns the flags.private Value
getHistogram
(SessionLocal session, AggregateData data) private Value
getHypotheticalSet
(SessionLocal session, AggregateData data) private Value
getListagg
(SessionLocal session, AggregateData data) private StringBuilder
getListaggError
(Value[] array, String separator) private String
private StringBuilder
getListaggTruncate
(Value[] array, String separator, String filter, boolean withoutCount) private Index
private Value
getMode
(SessionLocal session, AggregateData data) protected int
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) Returns the select statement.private StringBuilder
getSQLArrayAggregate
(StringBuilder builder, int sqlFlags) private StringBuilder
getSQLJsonArrayAggregate
(StringBuilder builder, int sqlFlags) private StringBuilder
getSQLJsonObjectAggregate
(StringBuilder builder, int sqlFlags) private StringBuilder
getSQLListagg
(StringBuilder builder, int sqlFlags) private static TypeInfo
getSumType
(TypeInfo type) getUnenclosedSQL
(StringBuilder builder, int sqlFlags) Get the SQL statement of this expression.getValue
(SessionLocal session) Return the resulting value for the current row.private Value
getValueQuick
(SessionLocal session) boolean
Returns if distinct is used.boolean
isEverything
(ExpressionVisitor visitor) Check if this expression and all sub-expressions can fulfill a criteria.void
mapColumnsAnalysis
(ColumnResolver resolver, int level, int innerState) Map the columns of the resolver to expression columns.optimize
(SessionLocal session) Try to optimize the expression.protected void
rememberExpressions
(SessionLocal 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
setExtraArguments
(Object extraArguments) Sets the additional arguments.void
setFlags
(int flags) Set the flags for this expression.void
setOrderByList
(ArrayList<QueryOrderBy> orderByList) Set the order for ARRAY_AGG() or GROUP_CONCAT() aggregate.private void
sortWithOrderBy
(Value[] array) protected void
updateAggregate
(SessionLocal session, Object aggregateData) 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 session, Object aggregateData, Value[] array) Updates the provided aggregate data from the remembered expressions.protected void
updateGroupAggregates
(SessionLocal session, int stage) Invoked when processing group stage of grouped window queries to update arguments of this aggregate.Methods inherited from class org.h2.expression.aggregate.AbstractAggregate
appendTailConditions, getOrderedResultLoop, getSubexpression, getSubexpressionCount, getType, isAggregate, setFilterCondition, updateAggregate
Methods inherited from class org.h2.expression.analysis.DataAnalysisOperation
createOrder, getGroupData, getOverOrderBySort, getWindowData, mapColumns, setOverCondition, updateAggregate, updateOrderedAggregate
Methods inherited from class org.h2.expression.Expression
addFilterConditions, createIndexConditions, getAlias, getBooleanValue, getColumnName, getColumnNameForView, getEnclosedSQL, getNonAliasExpression, getNotIfPossible, getNullable, getSchemaName, getSQL, getSQL, getSQL, getSQL, getTableAlias, getTableName, getWhenSQL, getWhenValue, isConstant, isIdentity, isNullConstant, isValueSet, isWhenConditionOperand, needParentheses, optimizeCondition, toString, writeExpressions, writeExpressions
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.h2.util.HasSQL
getTraceSQL
-
Field Details
-
ADDITIONAL_SUM_PRECISION
private static final int ADDITIONAL_SUM_PRECISIONThe additional result precision in decimal digits for a SUM aggregate function.- See Also:
-
ADDITIONAL_AVG_SCALE
private static final int ADDITIONAL_AVG_SCALEThe additional precision and scale in decimal digits for an AVG aggregate function.- See Also:
-
AGGREGATES
-
aggregateType
-
orderByList
-
orderBySort
-
extraArguments
-
flags
private int flags
-
-
Constructor Details
-
Aggregate
Create a new aggregate object.- Parameters:
aggregateType
- the aggregate typeargs
- the aggregated expressionsselect
- the select statementdistinct
- if distinct is used
-
-
Method Details
-
addAggregate
-
getAggregateType
Get the aggregate type for this name, or -1 if no aggregate has been found.- Parameters:
name
- the aggregate function name- Returns:
- null if no aggregate function has been found, or the aggregate type
-
setOrderByList
Set the order for ARRAY_AGG() or GROUP_CONCAT() aggregate.- Parameters:
orderByList
- the order by list
-
getAggregateType
Returns the type of this aggregate.- Returns:
- the type of this aggregate
-
setExtraArguments
Sets the additional arguments.- Parameters:
extraArguments
- the additional arguments
-
getExtraArguments
Returns the additional arguments.- Returns:
- the additional arguments
-
setFlags
public void setFlags(int flags) Description copied from interface:ExpressionWithFlags
Set the flags for this expression.- Specified by:
setFlags
in interfaceExpressionWithFlags
- Parameters:
flags
- the flags to set
-
getFlags
public int getFlags()Description copied from interface:ExpressionWithFlags
Returns the flags.- Specified by:
getFlags
in interfaceExpressionWithFlags
- Returns:
- the flags
-
sortWithOrderBy
-
updateAggregate
Description copied from class:AbstractAggregate
Updates an aggregate value.- Specified by:
updateAggregate
in classAbstractAggregate
- Parameters:
session
- the sessionaggregateData
- aggregate data
-
updateData
-
getSecondValue
-
updateGroupAggregates
Description copied from class:DataAnalysisOperation
Invoked when processing group stage of grouped window queries to update arguments of this aggregate.- Overrides:
updateGroupAggregates
in classAbstractAggregate
- Parameters:
session
- the sessionstage
- select stage
-
updateCollecting
-
getNumExpressions
protected int getNumExpressions()Description copied from class:DataAnalysisOperation
Returns the number of expressions, excluding OVER clause.- Specified by:
getNumExpressions
in classDataAnalysisOperation
- Returns:
- the number of expressions
-
rememberExpressions
Description copied from class:DataAnalysisOperation
Stores current values of expressions into the specified array.- Specified by:
rememberExpressions
in classDataAnalysisOperation
- Parameters:
session
- the sessionarray
- array to store values of expressions
-
updateFromExpressions
Description copied from class:AbstractAggregate
Updates the provided aggregate data from the remembered expressions.- Specified by:
updateFromExpressions
in classAbstractAggregate
- Parameters:
session
- the sessionaggregateData
- aggregate dataarray
- values of expressions
-
createAggregateData
Description copied from class:DataAnalysisOperation
Create aggregate data object specific to the subclass.- Specified by:
createAggregateData
in classDataAnalysisOperation
- Returns:
- aggregate-specific data object.
-
getValue
Description copied from class:Expression
Return the resulting value for the current row.- Overrides:
getValue
in classDataAnalysisOperation
- Parameters:
session
- the session- Returns:
- the result
-
getValueQuick
-
getAggregatedValue
Description copied from class:DataAnalysisOperation
Returns aggregated value.- Specified by:
getAggregatedValue
in classDataAnalysisOperation
- Parameters:
session
- the sessionaggregateData
- the aggregate data- Returns:
- aggregated value.
-
collect
-
getHypotheticalSet
-
getRank
-
getCumeDist
-
getListagg
-
getListaggError
-
getListaggTruncate
private StringBuilder getListaggTruncate(Value[] array, String separator, String filter, boolean withoutCount) -
getListaggItem
-
getHistogram
-
getMode
-
mapColumnsAnalysis
Description copied from class:DataAnalysisOperation
Map the columns of the resolver to expression columns.- Overrides:
mapColumnsAnalysis
in classAbstractAggregate
- Parameters:
resolver
- the column resolverlevel
- the subquery nesting levelinnerState
- one of the Expression MAP_IN_* values
-
optimize
Description copied from class:Expression
Try to optimize the expression.- Overrides:
optimize
in classAbstractAggregate
- Parameters:
session
- the session- Returns:
- the optimized expression
-
getSumType
-
getAvgType
-
setEvaluatable
Description copied from class:Expression
Tell the expression columns whether the table filter can return values now. This is used when optimizing the query.- Overrides:
setEvaluatable
in classAbstractAggregate
- Parameters:
tableFilter
- the table filterb
- true if the table filter can return value
-
getUnenclosedSQL
Description copied from class:Expression
Get the SQL statement of this expression. This may not always be the original SQL statement, especially after optimization. Enclosing '(' and ')' are never appended.- Specified by:
getUnenclosedSQL
in classExpression
- Parameters:
builder
- string buildersqlFlags
- formatting flags- Returns:
- the specified string builder
-
getSQLArrayAggregate
-
getSQLListagg
-
getSQLJsonObjectAggregate
-
getSQLJsonArrayAggregate
-
getMinMaxColumnIndex
-
isEverything
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.- Overrides:
isEverything
in classDataAnalysisOperation
- Parameters:
visitor
- the visitor- Returns:
- if the criteria can be fulfilled
-
getCost
public int getCost()Description copied from class:Expression
Estimate the cost to process the expression. Used when optimizing the query, to calculate the query plan with the lowest estimated cost.- Specified by:
getCost
in classExpression
- Returns:
- the estimated cost
-
getSelect
Returns the select statement.- Returns:
- the select statement
-
isDistinct
public boolean isDistinct()Returns if distinct is used.- Returns:
- if distinct is used
-