use of org.h2.engine.Database in project h2database by h2database.
the class AggregateDataMedian method getValue.
@Override
Value getValue(Database database, int dataType, boolean distinct) {
Value[] a = getArray();
if (a == null) {
return ValueNull.INSTANCE;
}
final CompareMode mode = database.getCompareMode();
Arrays.sort(a, new Comparator<Value>() {
@Override
public int compare(Value o1, Value o2) {
return o1.compareTo(o2, mode);
}
});
int len = a.length;
int idx = len / 2;
Value v1 = a[idx];
if ((len & 1) == 1) {
return v1.convertTo(dataType);
}
return getMedian(a[idx - 1], v1, dataType, mode);
}
use of org.h2.engine.Database in project h2database by h2database.
the class AggregateDataSelectivity method add.
@Override
void add(Database database, int dataType, boolean distinct, Value v) {
count++;
if (distinctHashes == null) {
distinctHashes = new IntIntHashMap();
}
int size = distinctHashes.size();
if (size > Constants.SELECTIVITY_DISTINCT_COUNT) {
distinctHashes = new IntIntHashMap();
m2 += size;
}
int hash = v.hashCode();
// the value -1 is not supported
distinctHashes.put(hash, 1);
}
use of org.h2.engine.Database in project h2database by h2database.
the class ConditionIn method getValue.
@Override
public Value getValue(Session session) {
Value l = left.getValue(session);
if (l == ValueNull.INSTANCE) {
return l;
}
boolean result = false;
boolean hasNull = false;
for (Expression e : valueList) {
Value r = e.getValue(session);
if (r == ValueNull.INSTANCE) {
hasNull = true;
} else {
r = r.convertTo(l.getType());
result = Comparison.compareNotNull(database, l, r, Comparison.EQUAL);
if (result) {
break;
}
}
}
if (!result && hasNull) {
return ValueNull.INSTANCE;
}
return ValueBoolean.get(result);
}
use of org.h2.engine.Database in project h2database by h2database.
the class PageDataIndex method add.
@Override
public void add(Session session, Row row) {
boolean retry = false;
if (mainIndexColumn != -1) {
row.setKey(row.getValue(mainIndexColumn).getLong());
} else {
if (row.getKey() == 0) {
row.setKey((int) ++lastKey);
retry = true;
}
}
if (tableData.getContainsLargeObject()) {
for (int i = 0, len = row.getColumnCount(); i < len; i++) {
Value v = row.getValue(i);
Value v2 = v.copy(database, getId());
if (v2.isLinkedToTable()) {
session.removeAtCommitStop(v2);
}
if (v != v2) {
row.setValue(i, v2);
}
}
}
// tries are required (specially if there was originally a primary key)
if (trace.isDebugEnabled()) {
trace.debug("{0} add {1}", getName(), row);
}
long add = 0;
while (true) {
try {
addTry(session, row);
break;
} catch (DbException e) {
if (e != fastDuplicateKeyException) {
throw e;
}
if (!retry) {
throw getNewDuplicateKeyException();
}
if (add == 0) {
// in the first re-try add a small random number,
// to avoid collisions after a re-start
row.setKey((long) (row.getKey() + Math.random() * 10_000));
} else {
row.setKey(row.getKey() + add);
}
add++;
} finally {
store.incrementChangeCount();
}
}
lastKey = Math.max(lastKey, row.getKey());
}
use of org.h2.engine.Database in project h2database by h2database.
the class AggregateDataDefault method getValue.
@Override
Value getValue(Database database, int dataType, boolean distinct) {
if (distinct) {
count = 0;
groupDistinct(database, dataType);
}
Value v = null;
switch(aggregateType) {
case SUM:
case MIN:
case MAX:
case BIT_OR:
case BIT_AND:
case BOOL_OR:
case BOOL_AND:
v = value;
break;
case AVG:
if (value != null) {
v = divide(value, count);
}
break;
case STDDEV_POP:
{
if (count < 1) {
return ValueNull.INSTANCE;
}
v = ValueDouble.get(Math.sqrt(m2 / count));
break;
}
case STDDEV_SAMP:
{
if (count < 2) {
return ValueNull.INSTANCE;
}
v = ValueDouble.get(Math.sqrt(m2 / (count - 1)));
break;
}
case VAR_POP:
{
if (count < 1) {
return ValueNull.INSTANCE;
}
v = ValueDouble.get(m2 / count);
break;
}
case VAR_SAMP:
{
if (count < 2) {
return ValueNull.INSTANCE;
}
v = ValueDouble.get(m2 / (count - 1));
break;
}
default:
DbException.throwInternalError("type=" + aggregateType);
}
return v == null ? ValueNull.INSTANCE : v.convertTo(dataType);
}
Aggregations