Search in sources :

Example 71 with Database

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);
}
Also used : Value(org.h2.value.Value) CompareMode(org.h2.value.CompareMode)

Example 72 with Database

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);
}
Also used : IntIntHashMap(org.h2.util.IntIntHashMap)

Example 73 with Database

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);
}
Also used : Value(org.h2.value.Value)

Example 74 with Database

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());
}
Also used : Value(org.h2.value.Value) DbException(org.h2.message.DbException)

Example 75 with Database

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);
}
Also used : Value(org.h2.value.Value)

Aggregations

Database (org.h2.engine.Database)79 SQLException (java.sql.SQLException)45 PreparedStatement (java.sql.PreparedStatement)38 DbException (org.h2.message.DbException)37 ResultSet (java.sql.ResultSet)34 Statement (java.sql.Statement)32 SimpleResultSet (org.h2.tools.SimpleResultSet)32 Connection (java.sql.Connection)27 Table (org.h2.table.Table)25 Value (org.h2.value.Value)25 Column (org.h2.table.Column)22 IOException (java.io.IOException)19 Constraint (org.h2.constraint.Constraint)18 Expression (org.h2.expression.Expression)17 ExpressionColumn (org.h2.expression.ExpressionColumn)17 ValueString (org.h2.value.ValueString)15 ValueExpression (org.h2.expression.ValueExpression)14 Session (org.h2.engine.Session)13 JdbcConnection (org.h2.jdbc.JdbcConnection)13 Schema (org.h2.schema.Schema)13