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

Use AggregateDataCollecting for DISTINCT COUNT

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