Debian Codegraph @

src/main/org/h2/expression/aggregate/AggregateDataCollecting.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.ArrayList;
9 import java.util.Collection;
10 import java.util.Collections;
11 import java.util.Comparator;
12 import java.util.Iterator;
13 import java.util.TreeSet;
14
15 import org.h2.api.ErrorCode;
16 import org.h2.engine.SessionLocal;
17 import org.h2.message.DbException;
18 import org.h2.value.Value;
19 import org.h2.value.ValueNull;
20 import org.h2.value.ValueRow;
21
22 /**
23 * Data stored while calculating an aggregate that needs collecting of all
24 * values or a distinct aggregate.
25 *
26 * <p>
27 * NULL values are not collected. {@link #getValue(SessionLocal)} method
28 * returns {@code null}. Use {@link #getArray()} for instances of this class
29 * instead.
30 * </p>
31 */
32 final class AggregateDataCollecting extends AggregateData implements Iterable<Value> {
33
34 /**
35 * NULL values collection mode.
36 */
37 enum NullCollectionMode {
38
39 /**
40 * Rows with NULL value are completely ignored.
41 */
42 IGNORED,
43
44 /**
45 * Rows with NULL values are processed causing the result to be not
46 * NULL, but NULL values aren't collected.
47 */
48 EXCLUDED,
49
50 /**
51 * Rows with NULL values are aggregated just like rows with any other
52 * values, should also be used when NULL values aren't passed to
53 * {@linkplain AggregateDataCollecting}.
54 */
55 USED_OR_IMPOSSIBLE;
56
57 }
58
59 private final boolean distinct;
60
61 private final boolean orderedWithOrder;
62
63 private final NullCollectionMode nullCollectionMode;
64
65 Collection<Value> values;
66
67 private Value shared;
68
69 /**
70 * Creates new instance of data for collecting aggregates.
71 *
72 * @param distinct
73 * if distinct is used
74 * @param orderedWithOrder
75 * if aggregate is an ordered aggregate with ORDER BY clause
76 * @param nullCollectionMode
77 * NULL values collection mode
78 */
79 AggregateDataCollecting(boolean distinct, boolean orderedWithOrder, NullCollectionMode nullCollectionMode) {
80 this.distinct = distinct;
81 this.orderedWithOrder = orderedWithOrder;
82 this.nullCollectionMode = nullCollectionMode;
83 }
84
85 @Override
86 void add(SessionLocal session, Value v) {
87 if (nullCollectionMode == NullCollectionMode.IGNORED && isNull(v)) {
88 return;
89 }
90 Collection<Value> c = values;
91 if (c == null) {
92 if (distinct) {
93 Comparator<Value> comparator = session.getDatabase().getCompareMode();
94 if (orderedWithOrder) {
95 comparator = Comparator.comparing(t -> ((ValueRow) t).getList()[0], comparator);
96 }
97 c = new TreeSet<>(comparator);
98 } else {
99 c = new ArrayList<>();
100 }
101 values = c;
102 }
103 if (nullCollectionMode == NullCollectionMode.EXCLUDED && isNull(v)) {
104 return;
105 }
106 c.add(v);
107 }
108
109 private boolean isNull(Value v) {
110 return (orderedWithOrder ? ((ValueRow) v).getList()[0] : v) == ValueNull.INSTANCE;
111 }
112
113 @Override
114 Value getValue(SessionLocal session) {
115 return null;
116 }
117
118 /**
119 * Returns the count of values.
120 *
121 * @return the count of values
122 */
123 int getCount() {
124 return values != null ? values.size() : 0;
125 }
126
127 /**
128 * Returns array with values or {@code null}.
129 *
130 * @return array with values or {@code null}
131 */
132 Value[] getArray() {
133 Collection<Value> values = this.values;
134 if (values == null) {
135 return null;
136 }
137 return values.toArray(Value.EMPTY_VALUES);
138 }
139
140 @Override
141 public Iterator<Value> iterator() {
142 return values != null ? values.iterator() : Collections.emptyIterator();
143 }
144
145 /**
146 * Sets value of a shared argument.
147 *
148 * @param shared the shared value
149 */
150 void setSharedArgument(Value shared) {
151 if (this.shared == null) {
152 this.shared = shared;
153 } else if (!this.shared.equals(shared)) {
154 throw DbException.get(ErrorCode.INVALID_VALUE_2, "Inverse distribution function argument",
155 this.shared.getTraceSQL() + "<>" + shared.getTraceSQL());
156 }
157 }
158
159 /**
160 * Returns value of a shared argument.
161 *
162 * @return value of a shared argument
163 */
164 Value getSharedArgument() {
165 return shared;
166 }
167
168 }