1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.expression; |
7 | |
8 | import org.h2.engine.Database; |
9 | import org.h2.util.ValueHashMap; |
10 | import org.h2.value.Value; |
11 | import org.h2.value.ValueLong; |
12 | import org.h2.value.ValueNull; |
13 | |
14 | /** |
15 | * Data stored while calculating an aggregate. |
16 | */ |
17 | class AggregateDataCount extends AggregateData { |
18 | private long count; |
19 | private ValueHashMap<AggregateDataCount> distinctValues; |
20 | |
21 | @Override |
22 | void add(Database database, int dataType, boolean distinct, Value v) { |
23 | if (v == ValueNull.INSTANCE) { |
24 | return; |
25 | } |
26 | count++; |
27 | if (distinct) { |
28 | if (distinctValues == null) { |
29 | distinctValues = ValueHashMap.newInstance(); |
30 | } |
31 | distinctValues.put(v, this); |
32 | return; |
33 | } |
34 | } |
35 | |
36 | @Override |
37 | Value getValue(Database database, int dataType, boolean distinct) { |
38 | if (distinct) { |
39 | if (distinctValues != null) { |
40 | count = distinctValues.size(); |
41 | } else { |
42 | count = 0; |
43 | } |
44 | } |
45 | Value v = ValueLong.get(count); |
46 | return v.convertTo(dataType); |
47 | } |
48 | |
49 | } |