Search in sources :

Example 11 with DataType

use of org.h2.value.DataType in project h2database by h2database.

the class Aggregate method getValue.

@Override
public Value getValue(Session session) {
    if (select.isQuickAggregateQuery()) {
        switch(type) {
            case COUNT:
            case COUNT_ALL:
                Table table = select.getTopTableFilter().getTable();
                return ValueLong.get(table.getRowCount(session));
            case MIN:
            case MAX:
                {
                    boolean first = type == AggregateType.MIN;
                    Index index = getMinMaxColumnIndex();
                    int sortType = index.getIndexColumns()[0].sortType;
                    if ((sortType & SortOrder.DESCENDING) != 0) {
                        first = !first;
                    }
                    Cursor cursor = index.findFirstOrLast(session, first);
                    SearchRow row = cursor.getSearchRow();
                    Value v;
                    if (row == null) {
                        v = ValueNull.INSTANCE;
                    } else {
                        v = row.getValue(index.getColumns()[0].getColumnId());
                    }
                    return v;
                }
            case MEDIAN:
                {
                    return AggregateDataMedian.getResultFromIndex(session, on, dataType);
                }
            default:
                DbException.throwInternalError("type=" + type);
        }
    }
    HashMap<Expression, Object> group = select.getCurrentGroup();
    if (group == null) {
        throw DbException.get(ErrorCode.INVALID_USE_OF_AGGREGATE_FUNCTION_1, getSQL());
    }
    AggregateData data = (AggregateData) group.get(this);
    if (data == null) {
        data = AggregateData.create(type);
    }
    if (type == AggregateType.GROUP_CONCAT) {
        Value[] array = ((AggregateDataCollecting) data).getArray();
        if (array == null) {
            return ValueNull.INSTANCE;
        }
        if (orderByList != null || distinct) {
            sortWithOrderBy(array);
        }
        StatementBuilder buff = new StatementBuilder();
        String sep = groupConcatSeparator == null ? "," : groupConcatSeparator.getValue(session).getString();
        for (Value val : array) {
            String s;
            if (val.getType() == Value.ARRAY) {
                s = ((ValueArray) val).getList()[0].getString();
            } else {
                s = val.getString();
            }
            if (s == null) {
                continue;
            }
            if (sep != null) {
                buff.appendExceptFirst(sep);
            }
            buff.append(s);
        }
        return ValueString.get(buff.toString());
    } else if (type == AggregateType.ARRAY_AGG) {
        Value[] array = ((AggregateDataCollecting) data).getArray();
        if (array == null) {
            return ValueNull.INSTANCE;
        }
        if (orderByList != null || distinct) {
            sortWithOrderBy(array);
        }
        if (orderByList != null) {
            for (int i = 0; i < array.length; i++) {
                array[i] = ((ValueArray) array[i]).getList()[0];
            }
        }
        return ValueArray.get(array);
    }
    return data.getValue(session.getDatabase(), dataType, distinct);
}
Also used : Table(org.h2.table.Table) Index(org.h2.index.Index) ValueString(org.h2.value.ValueString) Cursor(org.h2.index.Cursor) StatementBuilder(org.h2.util.StatementBuilder) Value(org.h2.value.Value) SearchRow(org.h2.result.SearchRow)

Example 12 with DataType

use of org.h2.value.DataType 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)

Example 13 with DataType

use of org.h2.value.DataType in project h2database by h2database.

the class AggregateDataHistogram method getValue.

@Override
Value getValue(Database database, int dataType, boolean distinct) {
    if (distinct) {
        count = 0;
        groupDistinct(database, dataType);
    }
    ValueArray[] values = new ValueArray[distinctValues.size()];
    int i = 0;
    for (Value dv : distinctValues.keys()) {
        AggregateDataHistogram d = distinctValues.get(dv);
        values[i] = ValueArray.get(new Value[] { dv, ValueLong.get(d.count) });
        i++;
    }
    final CompareMode compareMode = database.getCompareMode();
    Arrays.sort(values, new Comparator<ValueArray>() {

        @Override
        public int compare(ValueArray v1, ValueArray v2) {
            Value a1 = v1.getList()[0];
            Value a2 = v2.getList()[0];
            return a1.compareTo(a2, compareMode);
        }
    });
    Value v = ValueArray.get(values);
    return v.convertTo(dataType);
}
Also used : Value(org.h2.value.Value) CompareMode(org.h2.value.CompareMode) ValueArray(org.h2.value.ValueArray)

Example 14 with DataType

use of org.h2.value.DataType in project h2database by h2database.

the class TransactionStore method openMap.

/**
 * Open the map with the given name.
 *
 * @param <K> the key type
 * @param name the map name
 * @param keyType the key type
 * @param valueType the value type
 * @return the map
 */
synchronized <K> MVMap<K, VersionedValue> openMap(String name, DataType keyType, DataType valueType) {
    if (keyType == null) {
        keyType = new ObjectDataType();
    }
    if (valueType == null) {
        valueType = new ObjectDataType();
    }
    VersionedValueType vt = new VersionedValueType(valueType);
    MVMap<K, VersionedValue> map;
    MVMap.Builder<K, VersionedValue> builder = new MVMap.Builder<K, VersionedValue>().keyType(keyType).valueType(vt);
    map = store.openMap(name, builder);
    @SuppressWarnings("unchecked") MVMap<Object, VersionedValue> m = (MVMap<Object, VersionedValue>) map;
    maps.put(map.getId(), m);
    return map;
}
Also used : MVMap(org.h2.mvstore.MVMap) ObjectDataType(org.h2.mvstore.type.ObjectDataType)

Example 15 with DataType

use of org.h2.value.DataType in project h2database by h2database.

the class Page method setValue.

/**
 * Replace the value at an index in this page.
 *
 * @param index the index
 * @param value the new value
 * @return the old value
 */
public Object setValue(int index, Object value) {
    Object old = values[index];
    // this is slightly slower:
    // values = Arrays.copyOf(values, values.length);
    values = values.clone();
    DataType valueType = map.getValueType();
    if (isPersistent()) {
        addMemory(valueType.getMemory(value) - valueType.getMemory(old));
    }
    values[index] = value;
    return old;
}
Also used : DataType(org.h2.mvstore.type.DataType)

Aggregations

Value (org.h2.value.Value)16 ValueString (org.h2.value.ValueString)13 DataType (org.h2.value.DataType)12 Expression (org.h2.expression.Expression)6 Column (org.h2.table.Column)6 Constraint (org.h2.constraint.Constraint)5 UserDataType (org.h2.engine.UserDataType)5 UserAggregate (org.h2.engine.UserAggregate)4 Index (org.h2.index.Index)4 Table (org.h2.table.Table)4 ArrayList (java.util.ArrayList)3 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)3 Mode (org.h2.engine.Mode)3 ValueExpression (org.h2.expression.ValueExpression)3 SearchRow (org.h2.result.SearchRow)3 Schema (org.h2.schema.Schema)3 SimpleResultSet (org.h2.tools.SimpleResultSet)3 StatementBuilder (org.h2.util.StatementBuilder)3 CompareMode (org.h2.value.CompareMode)3 IOException (java.io.IOException)2