Search in sources :

Example 11 with DataType

use of org.h2.mvstore.type.DataType 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 12 with DataType

use of org.h2.mvstore.type.DataType in project h2database by h2database.

the class Aggregate method updateAggregate.

@Override
public void updateAggregate(Session session) {
    // TODO aggregates: check nested MIN(MAX(ID)) and so on
    // if (on != null) {
    // on.updateAggregate();
    // }
    HashMap<Expression, Object> group = select.getCurrentGroup();
    if (group == null) {
        // this is a different level (the enclosing query)
        return;
    }
    int groupRowId = select.getCurrentGroupRowId();
    if (lastGroupRowId == groupRowId) {
        // already visited
        return;
    }
    lastGroupRowId = groupRowId;
    AggregateData data = (AggregateData) group.get(this);
    if (data == null) {
        data = AggregateData.create(type);
        group.put(this, data);
    }
    Value v = on == null ? null : on.getValue(session);
    if (type == AggregateType.GROUP_CONCAT) {
        if (v != ValueNull.INSTANCE) {
            v = v.convertTo(Value.STRING);
            if (orderByList != null) {
                int size = orderByList.size();
                Value[] array = new Value[1 + size];
                array[0] = v;
                for (int i = 0; i < size; i++) {
                    SelectOrderBy o = orderByList.get(i);
                    array[i + 1] = o.expression.getValue(session);
                }
                v = ValueArray.get(array);
            }
        }
    }
    if (type == AggregateType.ARRAY_AGG) {
        if (v != ValueNull.INSTANCE) {
            if (orderByList != null) {
                int size = orderByList.size();
                Value[] array = new Value[1 + size];
                array[0] = v;
                for (int i = 0; i < size; i++) {
                    SelectOrderBy o = orderByList.get(i);
                    array[i + 1] = o.expression.getValue(session);
                }
                v = ValueArray.get(array);
            }
        }
    }
    if (filterCondition != null) {
        if (!filterCondition.getBooleanValue(session)) {
            return;
        }
    }
    data.add(session.getDatabase(), dataType, distinct, v);
}
Also used : SelectOrderBy(org.h2.command.dml.SelectOrderBy) Value(org.h2.value.Value)

Example 13 with DataType

use of org.h2.mvstore.type.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 14 with DataType

use of org.h2.mvstore.type.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 15 with DataType

use of org.h2.mvstore.type.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)

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