提交 f2584f9a authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Use AggregateDataCollecting for DISTINCT COUNT

上级 021c29e1
......@@ -369,7 +369,7 @@ public class Aggregate extends AbstractAggregate {
@Override
protected Object createAggregateData() {
return AggregateData.create(type);
return AggregateData.create(type, distinct);
}
@Override
......@@ -417,6 +417,12 @@ public class Aggregate extends AbstractAggregate {
data = (AggregateData) createAggregateData();
}
switch (type) {
case COUNT: {
if (!distinct) {
return data.getValue(session.getDatabase(), dataType, distinct);
}
return ValueLong.get(((AggregateDataCollecting) data).getCount());
}
case GROUP_CONCAT: {
Value[] array = ((AggregateDataCollecting) data).getArray();
if (array == null) {
......
......@@ -18,20 +18,24 @@ abstract class AggregateData {
* Create an AggregateData object of the correct sub-type.
*
* @param aggregateType the type of the aggregate operation
* @param distinct if the calculation should be distinct
* @return the aggregate data object of the specified type
*/
static AggregateData create(AggregateType aggregateType) {
static AggregateData create(AggregateType aggregateType, boolean distinct) {
switch (aggregateType) {
case SELECTIVITY:
return new AggregateDataSelectivity();
case GROUP_CONCAT:
case ARRAY_AGG:
case MEDIAN:
return new AggregateDataCollecting();
break;
case COUNT_ALL:
return new AggregateDataCountAll();
case COUNT:
if (!distinct) {
return new AggregateDataCount();
}
break;
case HISTOGRAM:
return new AggregateDataHistogram();
case MODE:
......@@ -41,6 +45,7 @@ abstract class AggregateData {
default:
return new AggregateDataDefault(aggregateType);
}
return new AggregateDataCollecting();
}
/**
......
......@@ -43,6 +43,15 @@ class AggregateDataCollecting extends AggregateData {
return null;
}
/**
* Returns the count of values.
*
* @return the count of values
*/
int getCount() {
return values != null ? values.size() : 0;
}
/**
* Returns array with values or {@code null}.
*
......
......@@ -6,7 +6,6 @@
package org.h2.expression.aggregate;
import org.h2.engine.Database;
import org.h2.util.ValueHashMap;
import org.h2.value.Value;
import org.h2.value.ValueLong;
import org.h2.value.ValueNull;
......@@ -16,7 +15,6 @@ import org.h2.value.ValueNull;
*/
class AggregateDataCount extends AggregateData {
private long count;
private ValueHashMap<AggregateDataCount> distinctValues;
@Override
void add(Database database, int dataType, boolean distinct, Value v) {
......@@ -24,23 +22,10 @@ class AggregateDataCount extends AggregateData {
return;
}
count++;
if (distinct) {
if (distinctValues == null) {
distinctValues = ValueHashMap.newInstance();
}
distinctValues.put(v, this);
}
}
@Override
Value getValue(Database database, int dataType, boolean distinct) {
if (distinct) {
if (distinctValues != null) {
count = distinctValues.size();
} else {
count = 0;
}
}
return ValueLong.get(count).convertTo(dataType);
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论