提交 92596a70 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Do not pass distinct flag to methods of AggregateData

上级 c5cb7339
......@@ -192,6 +192,9 @@ public class Aggregate extends AbstractAggregate {
*/
public Aggregate(AggregateType type, Expression on, Select select, boolean distinct) {
super(select, distinct);
if (distinct && type == AggregateType.COUNT_ALL) {
throw DbException.throwInternalError();
}
this.type = type;
this.on = on;
}
......@@ -303,7 +306,7 @@ public class Aggregate extends AbstractAggregate {
v = updateCollecting(session, v, remembered);
}
}
data.add(session.getDatabase(), dataType, distinct, v);
data.add(session.getDatabase(), dataType, v);
}
@Override
......@@ -437,9 +440,9 @@ public class Aggregate extends AbstractAggregate {
AggregateDataDefault d = new AggregateDataDefault(type);
Database db = session.getDatabase();
for (Value v : c) {
d.add(db, dataType, false, v);
d.add(db, dataType, v);
}
return d.getValue(db, dataType, false);
return d.getValue(db, dataType);
}
break;
case GROUP_CONCAT: {
......@@ -500,7 +503,7 @@ public class Aggregate extends AbstractAggregate {
default:
// Avoid compiler warning
}
return data.getValue(session.getDatabase(), dataType, distinct);
return data.getValue(session.getDatabase(), dataType);
}
@Override
......
......@@ -53,9 +53,9 @@ abstract class AggregateData {
}
break;
case SELECTIVITY:
return new AggregateDataSelectivity();
return new AggregateDataSelectivity(distinct);
case HISTOGRAM:
return new AggregateDataHistogram();
return new AggregateDataHistogram(distinct);
case MODE:
return new AggregateDataMode();
case ENVELOPE:
......@@ -63,7 +63,7 @@ abstract class AggregateData {
default:
throw DbException.throwInternalError("type=" + aggregateType);
}
return new AggregateDataCollecting();
return new AggregateDataCollecting(distinct);
}
/**
......@@ -71,18 +71,16 @@ abstract class AggregateData {
*
* @param database the database
* @param dataType the datatype of the computed result
* @param distinct if the calculation should be distinct
* @param v the value
*/
abstract void add(Database database, int dataType, boolean distinct, Value v);
abstract void add(Database database, int dataType, Value v);
/**
* Get the aggregate result.
*
* @param database the database
* @param dataType the datatype of the computed result
* @param distinct if distinct is used
* @return the value
*/
abstract Value getValue(Database database, int dataType, boolean distinct);
abstract Value getValue(Database database, int dataType);
}
......@@ -17,20 +17,31 @@ import org.h2.value.ValueNull;
/**
* Data stored while calculating an aggregate that needs collecting of all
* values.
* values or a distinct aggregate.
*
* <p>
* NULL values are not collected. {@link #getValue(Database, int, boolean)}
* NULL values are not collected. {@link #getValue(Database, int)}
* method returns {@code null}. Use {@link #getArray()} for instances of this
* class instead.
* </p>
*/
class AggregateDataCollecting extends AggregateData implements Iterable<Value> {
private final boolean distinct;
Collection<Value> values;
/**
* Creates new instance of data for collecting aggregates.
*
* @param distinct if distinct is used
*/
AggregateDataCollecting(boolean distinct) {
this.distinct = distinct;
}
@Override
void add(Database database, int dataType, boolean distinct, Value v) {
void add(Database database, int dataType, Value v) {
if (v == ValueNull.INSTANCE) {
return;
}
......@@ -42,7 +53,7 @@ class AggregateDataCollecting extends AggregateData implements Iterable<Value> {
}
@Override
Value getValue(Database database, int dataType, boolean distinct) {
Value getValue(Database database, int dataType) {
return null;
}
......
......@@ -17,15 +17,14 @@ class AggregateDataCount extends AggregateData {
private long count;
@Override
void add(Database database, int dataType, boolean distinct, Value v) {
if (v == ValueNull.INSTANCE) {
return;
void add(Database database, int dataType, Value v) {
if (v != ValueNull.INSTANCE) {
count++;
}
count++;
}
@Override
Value getValue(Database database, int dataType, boolean distinct) {
Value getValue(Database database, int dataType) {
return ValueLong.get(count).convertTo(dataType);
}
......
......@@ -6,7 +6,6 @@
package org.h2.expression.aggregate;
import org.h2.engine.Database;
import org.h2.message.DbException;
import org.h2.value.Value;
import org.h2.value.ValueLong;
......@@ -14,21 +13,16 @@ import org.h2.value.ValueLong;
* Data stored while calculating a COUNT(*) aggregate.
*/
class AggregateDataCountAll extends AggregateData {
private long count;
@Override
void add(Database database, int dataType, boolean distinct, Value v) {
if (distinct) {
throw DbException.throwInternalError();
}
void add(Database database, int dataType, Value v) {
count++;
}
@Override
Value getValue(Database database, int dataType, boolean distinct) {
if (distinct) {
throw DbException.throwInternalError();
}
Value getValue(Database database, int dataType) {
return ValueLong.get(count).convertTo(dataType);
}
......
......@@ -33,7 +33,7 @@ class AggregateDataDefault extends AggregateData {
}
@Override
void add(Database database, int dataType, boolean distinct, Value v) {
void add(Database database, int dataType, Value v) {
if (v == ValueNull.INSTANCE) {
return;
}
......@@ -119,7 +119,7 @@ class AggregateDataDefault extends AggregateData {
}
@Override
Value getValue(Database database, int dataType, boolean distinct) {
Value getValue(Database database, int dataType) {
Value v = null;
switch (aggregateType) {
case SUM:
......
......@@ -57,7 +57,7 @@ class AggregateDataEnvelope extends AggregateData {
}
@Override
void add(Database database, int dataType, boolean distinct, Value v) {
void add(Database database, int dataType, Value v) {
if (v == ValueNull.INSTANCE) {
return;
}
......@@ -65,7 +65,7 @@ class AggregateDataEnvelope extends AggregateData {
}
@Override
Value getValue(Database database, int dataType, boolean distinct) {
Value getValue(Database database, int dataType) {
return ValueGeometry.fromEnvelope(envelope);
}
......
......@@ -22,10 +22,21 @@ import org.h2.value.ValueLong;
*/
class AggregateDataHistogram extends AggregateData {
private final boolean distinct;
private ValueHashMap<LongDataCounter> distinctValues;
/**
* Creates new instance of data for HISTOGRAM aggregate.
*
* @param distinct if distinct is used
*/
AggregateDataHistogram(boolean distinct) {
this.distinct = distinct;
}
@Override
void add(Database database, int dataType, boolean distinct, Value v) {
void add(Database database, int dataType, Value v) {
if (distinctValues == null) {
distinctValues = ValueHashMap.newInstance();
}
......@@ -41,7 +52,7 @@ class AggregateDataHistogram extends AggregateData {
}
@Override
Value getValue(Database database, int dataType, boolean distinct) {
Value getValue(Database database, int dataType) {
if (distinctValues == null) {
return ValueArray.get(new Value[0]).convertTo(dataType);
}
......
......@@ -20,7 +20,7 @@ class AggregateDataMode extends AggregateData {
private ValueHashMap<LongDataCounter> distinctValues;
@Override
void add(Database database, int dataType, boolean distinct, Value v) {
void add(Database database, int dataType, Value v) {
if (v == ValueNull.INSTANCE) {
return;
}
......@@ -36,7 +36,7 @@ class AggregateDataMode extends AggregateData {
}
@Override
Value getValue(Database database, int dataType, boolean distinct) {
Value getValue(Database database, int dataType) {
Value v = ValueNull.INSTANCE;
if (distinctValues != null) {
long count = 0L;
......
......@@ -15,12 +15,24 @@ import org.h2.value.ValueInt;
* Data stored while calculating a SELECTIVITY aggregate.
*/
class AggregateDataSelectivity extends AggregateData {
private final boolean distinct;
private long count;
private IntIntHashMap distinctHashes;
private double m2;
/**
* Creates new instance of data for SELECTIVITY aggregate.
*
* @param distinct if distinct is used
*/
AggregateDataSelectivity(boolean distinct) {
this.distinct = distinct;
}
@Override
void add(Database database, int dataType, boolean distinct, Value v) {
void add(Database database, int dataType, Value v) {
count++;
if (distinctHashes == null) {
distinctHashes = new IntIntHashMap();
......@@ -36,7 +48,7 @@ class AggregateDataSelectivity extends AggregateData {
}
@Override
Value getValue(Database database, int dataType, boolean distinct) {
Value getValue(Database database, int dataType) {
if (distinct) {
count = 0;
}
......@@ -53,4 +65,5 @@ class AggregateDataSelectivity extends AggregateData {
v = ValueInt.get(s);
return v.convertTo(dataType);
}
}
......@@ -209,7 +209,7 @@ public class JavaAggregate extends AbstractAggregate {
arg = arg.convertTo(argTypes[i]);
argValues[i] = arg;
}
data.add(session.getDatabase(), dataType, true, args.length == 1 ? arg : ValueArray.get(argValues));
data.add(session.getDatabase(), dataType, args.length == 1 ? arg : ValueArray.get(argValues));
} else {
Aggregate agg = (Aggregate) aggregateData;
Object[] argValues = new Object[args.length];
......@@ -254,7 +254,7 @@ public class JavaAggregate extends AbstractAggregate {
@Override
protected Object createAggregateData() {
return distinct ? new AggregateDataCollecting() : getInstance();
return distinct ? new AggregateDataCollecting(true) : getInstance();
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论