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
public class Aggregate extends AbstractAggregate implements ExpressionWithFlags
Implements the integrated aggregate functions, such as COUNT, MAX, SUM.
-
-
Field Summary
Fields Modifier and Type Field Description private static intADDITIONAL_AVG_SCALEThe additional precision and scale in decimal digits for an AVG aggregate function.private static intADDITIONAL_SUM_PRECISIONThe additional result precision in decimal digits for a SUM aggregate function.private static java.util.HashMap<java.lang.String,AggregateType>AGGREGATESprivate AggregateTypeaggregateTypeprivate java.lang.ObjectextraArgumentsprivate intflagsprivate java.util.ArrayList<QueryOrderBy>orderByListprivate SortOrderorderBySort-
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
Constructors Constructor Description Aggregate(AggregateType aggregateType, Expression[] args, Select select, boolean distinct)Create a new aggregate object.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static voidaddAggregate(java.lang.String name, AggregateType type)private static Valuecollect(SessionLocal session, AggregateDataCollecting c, AggregateData d)protected java.lang.ObjectcreateAggregateData()Create aggregate data object specific to the subclass.ValuegetAggregatedValue(SessionLocal session, java.lang.Object aggregateData)Returns aggregated value.AggregateTypegetAggregateType()Returns the type of this aggregate.static AggregateTypegetAggregateType(java.lang.String name)Get the aggregate type for this name, or -1 if no aggregate has been found.private static TypeInfogetAvgType(TypeInfo type)intgetCost()Estimate the cost to process the expression.private static ValuegetCumeDist(Value[] ordered, Value arg, java.util.Comparator<Value> sort)java.lang.ObjectgetExtraArguments()Returns the additional arguments.intgetFlags()Returns the flags.private ValuegetHistogram(SessionLocal session, AggregateData data)private ValuegetHypotheticalSet(SessionLocal session, AggregateData data)private ValuegetListagg(SessionLocal session, AggregateData data)private java.lang.StringBuildergetListaggError(Value[] array, java.lang.String separator)private java.lang.StringgetListaggItem(Value v)private java.lang.StringBuildergetListaggTruncate(Value[] array, java.lang.String separator, java.lang.String filter, boolean withoutCount)private IndexgetMinMaxColumnIndex()private ValuegetMode(SessionLocal session, AggregateData data)protected intgetNumExpressions()Returns the number of expressions, excluding OVER clause.private ValuegetRank(Value[] ordered, Value arg, java.util.Comparator<Value> sort)private ValuegetSecondValue(SessionLocal session, Value[] remembered)SelectgetSelect()Returns the select statement.private java.lang.StringBuildergetSQLArrayAggregate(java.lang.StringBuilder builder, int sqlFlags)private java.lang.StringBuildergetSQLJsonArrayAggregate(java.lang.StringBuilder builder, int sqlFlags)private java.lang.StringBuildergetSQLJsonObjectAggregate(java.lang.StringBuilder builder, int sqlFlags)private java.lang.StringBuildergetSQLListagg(java.lang.StringBuilder builder, int sqlFlags)private static TypeInfogetSumType(TypeInfo type)java.lang.StringBuildergetUnenclosedSQL(java.lang.StringBuilder builder, int sqlFlags)Get the SQL statement of this expression.ValuegetValue(SessionLocal session)Return the resulting value for the current row.private ValuegetValueQuick(SessionLocal session)booleanisDistinct()Returns if distinct is used.booleanisEverything(ExpressionVisitor visitor)Check if this expression and all sub-expressions can fulfill a criteria.voidmapColumnsAnalysis(ColumnResolver resolver, int level, int innerState)Map the columns of the resolver to expression columns.Expressionoptimize(SessionLocal session)Try to optimize the expression.protected 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.voidsetExtraArguments(java.lang.Object extraArguments)Sets the additional arguments.voidsetFlags(int flags)Set the flags for this expression.voidsetOrderByList(java.util.ArrayList<QueryOrderBy> orderByList)Set the order for ARRAY_AGG() or GROUP_CONCAT() aggregate.private voidsortWithOrderBy(Value[] array)protected voidupdateAggregate(SessionLocal session, java.lang.Object aggregateData)Updates an aggregate value.private ValueupdateCollecting(SessionLocal session, Value v, Value[] remembered)private voidupdateData(SessionLocal session, AggregateData data, Value v, Value[] remembered)protected voidupdateFromExpressions(SessionLocal session, java.lang.Object aggregateData, Value[] array)Updates the provided aggregate data from the remembered expressions.protected voidupdateGroupAggregates(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 Detail
-
ADDITIONAL_SUM_PRECISION
private static final int ADDITIONAL_SUM_PRECISION
The additional result precision in decimal digits for a SUM aggregate function.- See Also:
- Constant Field Values
-
ADDITIONAL_AVG_SCALE
private static final int ADDITIONAL_AVG_SCALE
The additional precision and scale in decimal digits for an AVG aggregate function.- See Also:
- Constant Field Values
-
AGGREGATES
private static final java.util.HashMap<java.lang.String,AggregateType> AGGREGATES
-
aggregateType
private final AggregateType aggregateType
-
orderByList
private java.util.ArrayList<QueryOrderBy> orderByList
-
orderBySort
private SortOrder orderBySort
-
extraArguments
private java.lang.Object extraArguments
-
flags
private int flags
-
-
Constructor Detail
-
Aggregate
public Aggregate(AggregateType aggregateType, Expression[] args, Select select, boolean distinct)
Create a new aggregate object.- Parameters:
aggregateType- the aggregate typeargs- the aggregated expressionsselect- the select statementdistinct- if distinct is used
-
-
Method Detail
-
addAggregate
private static void addAggregate(java.lang.String name, AggregateType type)
-
getAggregateType
public static AggregateType getAggregateType(java.lang.String name)
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
public void setOrderByList(java.util.ArrayList<QueryOrderBy> orderByList)
Set the order for ARRAY_AGG() or GROUP_CONCAT() aggregate.- Parameters:
orderByList- the order by list
-
getAggregateType
public AggregateType getAggregateType()
Returns the type of this aggregate.- Returns:
- the type of this aggregate
-
setExtraArguments
public void setExtraArguments(java.lang.Object extraArguments)
Sets the additional arguments.- Parameters:
extraArguments- the additional arguments
-
getExtraArguments
public java.lang.Object getExtraArguments()
Returns the additional arguments.- Returns:
- the additional arguments
-
setFlags
public void setFlags(int flags)
Description copied from interface:ExpressionWithFlagsSet the flags for this expression.- Specified by:
setFlagsin interfaceExpressionWithFlags- Parameters:
flags- the flags to set
-
getFlags
public int getFlags()
Description copied from interface:ExpressionWithFlagsReturns the flags.- Specified by:
getFlagsin interfaceExpressionWithFlags- Returns:
- the flags
-
sortWithOrderBy
private void sortWithOrderBy(Value[] array)
-
updateAggregate
protected void updateAggregate(SessionLocal session, java.lang.Object aggregateData)
Description copied from class:AbstractAggregateUpdates an aggregate value.- Specified by:
updateAggregatein classAbstractAggregate- Parameters:
session- the sessionaggregateData- aggregate data
-
updateData
private void updateData(SessionLocal session, AggregateData data, Value v, Value[] remembered)
-
getSecondValue
private Value getSecondValue(SessionLocal session, Value[] remembered)
-
updateGroupAggregates
protected void updateGroupAggregates(SessionLocal session, int stage)
Description copied from class:DataAnalysisOperationInvoked when processing group stage of grouped window queries to update arguments of this aggregate.- Overrides:
updateGroupAggregatesin classAbstractAggregate- Parameters:
session- the sessionstage- select stage
-
updateCollecting
private Value updateCollecting(SessionLocal session, Value v, Value[] remembered)
-
getNumExpressions
protected int getNumExpressions()
Description copied from class:DataAnalysisOperationReturns the number of expressions, excluding OVER clause.- Specified by:
getNumExpressionsin classDataAnalysisOperation- Returns:
- the number of expressions
-
rememberExpressions
protected void rememberExpressions(SessionLocal session, Value[] array)
Description copied from class:DataAnalysisOperationStores current values of expressions into the specified array.- Specified by:
rememberExpressionsin classDataAnalysisOperation- Parameters:
session- the sessionarray- array to store values of expressions
-
updateFromExpressions
protected void updateFromExpressions(SessionLocal session, java.lang.Object aggregateData, Value[] array)
Description copied from class:AbstractAggregateUpdates the provided aggregate data from the remembered expressions.- Specified by:
updateFromExpressionsin classAbstractAggregate- Parameters:
session- the sessionaggregateData- aggregate dataarray- values of expressions
-
createAggregateData
protected java.lang.Object createAggregateData()
Description copied from class:DataAnalysisOperationCreate aggregate data object specific to the subclass.- Specified by:
createAggregateDatain classDataAnalysisOperation- Returns:
- aggregate-specific data object.
-
getValue
public Value getValue(SessionLocal session)
Description copied from class:ExpressionReturn the resulting value for the current row.- Overrides:
getValuein classDataAnalysisOperation- Parameters:
session- the session- Returns:
- the result
-
getValueQuick
private Value getValueQuick(SessionLocal session)
-
getAggregatedValue
public Value getAggregatedValue(SessionLocal session, java.lang.Object aggregateData)
Description copied from class:DataAnalysisOperationReturns aggregated value.- Specified by:
getAggregatedValuein classDataAnalysisOperation- Parameters:
session- the sessionaggregateData- the aggregate data- Returns:
- aggregated value.
-
collect
private static Value collect(SessionLocal session, AggregateDataCollecting c, AggregateData d)
-
getHypotheticalSet
private Value getHypotheticalSet(SessionLocal session, AggregateData data)
-
getCumeDist
private static Value getCumeDist(Value[] ordered, Value arg, java.util.Comparator<Value> sort)
-
getListagg
private Value getListagg(SessionLocal session, AggregateData data)
-
getListaggError
private java.lang.StringBuilder getListaggError(Value[] array, java.lang.String separator)
-
getListaggTruncate
private java.lang.StringBuilder getListaggTruncate(Value[] array, java.lang.String separator, java.lang.String filter, boolean withoutCount)
-
getListaggItem
private java.lang.String getListaggItem(Value v)
-
getHistogram
private Value getHistogram(SessionLocal session, AggregateData data)
-
getMode
private Value getMode(SessionLocal session, AggregateData data)
-
mapColumnsAnalysis
public void mapColumnsAnalysis(ColumnResolver resolver, int level, int innerState)
Description copied from class:DataAnalysisOperationMap the columns of the resolver to expression columns.- Overrides:
mapColumnsAnalysisin classAbstractAggregate- Parameters:
resolver- the column resolverlevel- the subquery nesting levelinnerState- one of the Expression MAP_IN_* values
-
optimize
public Expression optimize(SessionLocal session)
Description copied from class:ExpressionTry to optimize the expression.- Overrides:
optimizein classAbstractAggregate- Parameters:
session- the session- Returns:
- the optimized expression
-
setEvaluatable
public void setEvaluatable(TableFilter tableFilter, boolean b)
Description copied from class:ExpressionTell the expression columns whether the table filter can return values now. This is used when optimizing the query.- Overrides:
setEvaluatablein classAbstractAggregate- Parameters:
tableFilter- the table filterb- true if the table filter can return value
-
getUnenclosedSQL
public java.lang.StringBuilder getUnenclosedSQL(java.lang.StringBuilder builder, int sqlFlags)Description copied from class:ExpressionGet 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:
getUnenclosedSQLin classExpression- Parameters:
builder- string buildersqlFlags- formatting flags- Returns:
- the specified string builder
-
getSQLArrayAggregate
private java.lang.StringBuilder getSQLArrayAggregate(java.lang.StringBuilder builder, int sqlFlags)
-
getSQLListagg
private java.lang.StringBuilder getSQLListagg(java.lang.StringBuilder builder, int sqlFlags)
-
getSQLJsonObjectAggregate
private java.lang.StringBuilder getSQLJsonObjectAggregate(java.lang.StringBuilder builder, int sqlFlags)
-
getSQLJsonArrayAggregate
private java.lang.StringBuilder getSQLJsonArrayAggregate(java.lang.StringBuilder builder, int sqlFlags)
-
getMinMaxColumnIndex
private Index getMinMaxColumnIndex()
-
isEverything
public boolean isEverything(ExpressionVisitor visitor)
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.- Overrides:
isEverythingin classDataAnalysisOperation- Parameters:
visitor- the visitor- Returns:
- if the criteria can be fulfilled
-
getCost
public int getCost()
Description copied from class:ExpressionEstimate the cost to process the expression. Used when optimizing the query, to calculate the query plan with the lowest estimated cost.- Specified by:
getCostin classExpression- Returns:
- the estimated cost
-
getSelect
public Select getSelect()
Returns the select statement.- Returns:
- the select statement
-
isDistinct
public boolean isDistinct()
Returns if distinct is used.- Returns:
- if distinct is used
-
-