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