提交 57c070e7 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add LongDataCounter and fix NPE and other issues in AggregateDataHistogram

上级 667769c5
...@@ -7,7 +7,7 @@ package org.h2.expression.aggregate; ...@@ -7,7 +7,7 @@ package org.h2.expression.aggregate;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.Map; import java.util.Map.Entry;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.engine.Database; import org.h2.engine.Database;
import org.h2.engine.Mode; import org.h2.engine.Mode;
...@@ -21,37 +21,35 @@ import org.h2.value.ValueLong; ...@@ -21,37 +21,35 @@ import org.h2.value.ValueLong;
* Data stored while calculating a HISTOGRAM aggregate. * Data stored while calculating a HISTOGRAM aggregate.
*/ */
class AggregateDataHistogram extends AggregateData { class AggregateDataHistogram extends AggregateData {
private long count;
private ValueHashMap<AggregateDataHistogram> distinctValues; private ValueHashMap<LongDataCounter> distinctValues;
@Override @Override
void add(Database database, int dataType, boolean distinct, Value v) { void add(Database database, int dataType, boolean distinct, Value v) {
if (distinctValues == null) { if (distinctValues == null) {
distinctValues = ValueHashMap.newInstance(); distinctValues = ValueHashMap.newInstance();
} }
AggregateDataHistogram a = distinctValues.get(v); LongDataCounter a = distinctValues.get(v);
if (a == null) { if (a == null) {
if (distinctValues.size() < Constants.SELECTIVITY_DISTINCT_COUNT) { if (distinctValues.size() >= Constants.SELECTIVITY_DISTINCT_COUNT) {
a = new AggregateDataHistogram(); return;
distinctValues.put(v, a);
} }
a = new LongDataCounter();
distinctValues.put(v, a);
} }
if (a != null) {
a.count++; a.count++;
} }
}
@Override @Override
Value getValue(Database database, int dataType, boolean distinct) { Value getValue(Database database, int dataType, boolean distinct) {
if (distinct) { if (distinctValues == null) {
count = 0; return ValueArray.get(new Value[0]).convertTo(dataType);
groupDistinct(database, dataType);
} }
ValueArray[] values = new ValueArray[distinctValues.size()]; ValueArray[] values = new ValueArray[distinctValues.size()];
int i = 0; int i = 0;
for (Map.Entry<Value,AggregateDataHistogram> entry : distinctValues.entries()) { for (Entry<Value, LongDataCounter> entry : distinctValues.entries()) {
AggregateDataHistogram d = entry.getValue(); LongDataCounter d = entry.getValue();
values[i] = ValueArray.get(new Value[] { entry.getKey(), ValueLong.get(d.count) }); values[i] = ValueArray.get(new Value[] { entry.getKey(), ValueLong.get(distinct ? 1L : d.count) });
i++; i++;
} }
final Mode mode = database.getMode(); final Mode mode = database.getMode();
...@@ -64,18 +62,7 @@ class AggregateDataHistogram extends AggregateData { ...@@ -64,18 +62,7 @@ class AggregateDataHistogram extends AggregateData {
return a1.compareTo(a2, mode, compareMode); return a1.compareTo(a2, mode, compareMode);
} }
}); });
Value v = ValueArray.get(values); return ValueArray.get(values).convertTo(dataType);
return v.convertTo(dataType);
}
private void groupDistinct(Database database, int dataType) {
if (distinctValues == null) {
return;
}
count = 0;
for (Value v : distinctValues.keys()) {
add(database, dataType, false, v);
}
} }
} }
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.expression.aggregate;
/**
* Counter.
*/
final class LongDataCounter {
/**
* The value.
*/
long count;
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论