Debian Codegraph @

src/main/org/h2/expression/aggregate/AggregateDataDistinctWithCounts.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.expression.aggregate;
7
8 import java.util.TreeMap;
9 import org.h2.engine.SessionLocal;
10 import org.h2.value.Value;
11 import org.h2.value.ValueNull;
12
13 /**
14 * Data stored while calculating an aggregate that needs distinct values with
15 * their counts.
16 */
17 final class AggregateDataDistinctWithCounts extends AggregateData {
18
19 private final boolean ignoreNulls;
20
21 private final int maxDistinctCount;
22
23 private TreeMap<Value, LongDataCounter> values;
24
25 /**
26 * Creates new instance of data for aggregate that needs distinct values
27 * with their counts.
28 *
29 * @param ignoreNulls
30 * whether NULL values should be ignored
31 * @param maxDistinctCount
32 * maximum count of distinct values to collect
33 */
34 AggregateDataDistinctWithCounts(boolean ignoreNulls, int maxDistinctCount) {
35 this.ignoreNulls = ignoreNulls;
36 this.maxDistinctCount = maxDistinctCount;
37 }
38
39 @Override
40 void add(SessionLocal session, Value v) {
41 if (ignoreNulls && v == ValueNull.INSTANCE) {
42 return;
43 }
44 if (values == null) {
45 values = new TreeMap<>(session.getDatabase().getCompareMode());
46 }
47 LongDataCounter a = values.get(v);
48 if (a == null) {
49 if (values.size() >= maxDistinctCount) {
50 return;
51 }
52 a = new LongDataCounter();
53 values.put(v, a);
54 }
55 a.count++;
56 }
57
58 @Override
59 Value getValue(SessionLocal session) {
60 return null;
61 }
62
63 /**
64 * Returns map with values and their counts.
65 *
66 * @return map with values and their counts
67 */
68 TreeMap<Value, LongDataCounter> getValues() {
69 return values;
70 }
71
72 }