| 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.command.query; |
| 7 |
|
| 8 |
import java.util.ArrayList; |
| 9 |
import java.util.Arrays; |
| 10 |
import java.util.HashMap; |
| 11 |
import java.util.Iterator; |
| 12 |
import java.util.Map; |
| 13 |
import java.util.Map.Entry; |
| 14 |
import java.util.TreeMap; |
| 15 |
|
| 16 |
import org.h2.engine.SessionLocal; |
| 17 |
import org.h2.expression.Expression; |
| 18 |
import org.h2.expression.analysis.DataAnalysisOperation; |
| 19 |
import org.h2.expression.analysis.PartitionData; |
| 20 |
import org.h2.value.Value; |
| 21 |
import org.h2.value.ValueRow; |
| 22 |
|
| 23 |
/** |
| 24 |
* Grouped data for aggregates. |
| 25 |
* |
| 26 |
* <p> |
| 27 |
* Call sequence: |
| 28 |
* </p> |
| 29 |
* <ul> |
| 30 |
* <li>{@link #reset()}.</li> |
| 31 |
* <li>For each source row {@link #nextSource()} should be invoked.</li> |
| 32 |
* <li>{@link #done()}.</li> |
| 33 |
* <li>{@link #next()} is invoked inside a loop until it returns null.</li> |
| 34 |
* </ul> |
| 35 |
* <p> |
| 36 |
* Call sequence for lazy group sorted result: |
| 37 |
* </p> |
| 38 |
* <ul> |
| 39 |
* <li>{@link #resetLazy()} (not required before the first execution).</li> |
| 40 |
* <li>For each source group {@link #nextLazyGroup()} should be invoked.</li> |
| 41 |
* <li>For each source row {@link #nextLazyRow()} should be invoked. Each group |
| 42 |
* can have one or more rows.</li> |
| 43 |
* </ul> |
| 44 |
*/ |
| 45 |
public abstract class SelectGroups { |
| 46 |
|
| 47 |
private static final class Grouped extends SelectGroups { |
| 48 |
|
| 49 |
private final int[] groupIndex; |
| 50 |
|
| 51 |
/** |
| 52 |
* Map of group-by key to group-by expression data e.g. AggregateData |
| 53 |
*/ |
| 54 |
private TreeMap<ValueRow, Object[]> groupByData; |
| 55 |
|
| 56 |
/** |
| 57 |
* Key into groupByData that produces currentGroupByExprData. Not used |
| 58 |
* in lazy mode. |
| 59 |
*/ |
| 60 |
private ValueRow currentGroupsKey; |
| 61 |
|
| 62 |
/** |
| 63 |
* Cursor for {@link #next()} method. |
| 64 |
*/ |
| 65 |
private Iterator<Entry<ValueRow, Object[]>> cursor; |
| 66 |
|
| 67 |
Grouped(SessionLocal session, ArrayList<Expression> expressions, int[] groupIndex) { |
| 68 |
super(session, expressions); |
| 69 |
this.groupIndex = groupIndex; |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
public void reset() { |
| 74 |
super.reset(); |
| 75 |
groupByData = new TreeMap<>(session.getDatabase().getCompareMode()); |
| 76 |
currentGroupsKey = null; |
| 77 |
cursor = null; |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public void nextSource() { |
| 82 |
if (groupIndex == null) { |
| 83 |
currentGroupsKey = ValueRow.EMPTY; |
| 84 |
} else { |
| 85 |
Value[] keyValues = new Value[groupIndex.length]; |
| 86 |
// update group |
| 87 |
for (int i = 0; i < groupIndex.length; i++) { |
| 88 |
int idx = groupIndex[i]; |
| 89 |
Expression expr = expressions.get(idx); |
| 90 |
keyValues[i] = expr.getValue(session); |
| 91 |
} |
| 92 |
currentGroupsKey = ValueRow.get(keyValues); |
| 93 |
} |
| 94 |
Object[] values = groupByData.get(currentGroupsKey); |
| 95 |
if (values == null) { |
| 96 |
values = createRow(); |
| 97 |
groupByData.put(currentGroupsKey, values); |
| 98 |
} |
| 99 |
currentGroupByExprData = values; |
| 100 |
currentGroupRowId++; |
| 101 |
} |
| 102 |
|
| 103 |
@Override |
| 104 |
void updateCurrentGroupExprData() { |
| 105 |
// this can be null in lazy mode |
| 106 |
if (currentGroupsKey != null) { |
| 107 |
// since we changed the size of the array, update the object in |
| 108 |
// the groups map |
| 109 |
groupByData.put(currentGroupsKey, currentGroupByExprData); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public void done() { |
| 115 |
super.done(); |
| 116 |
if (groupIndex == null && groupByData.size() == 0) { |
| 117 |
groupByData.put(ValueRow.EMPTY, createRow()); |
| 118 |
} |
| 119 |
cursor = groupByData.entrySet().iterator(); |
| 120 |
} |
| 121 |
|
| 122 |
@Override |
| 123 |
public ValueRow next() { |
| 124 |
if (cursor.hasNext()) { |
| 125 |
Map.Entry<ValueRow, Object[]> entry = cursor.next(); |
| 126 |
currentGroupByExprData = entry.getValue(); |
| 127 |
currentGroupRowId++; |
| 128 |
return entry.getKey(); |
| 129 |
} |
| 130 |
return null; |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public void remove() { |
| 135 |
cursor.remove(); |
| 136 |
currentGroupByExprData = null; |
| 137 |
currentGroupRowId--; |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public void resetLazy() { |
| 142 |
super.resetLazy(); |
| 143 |
currentGroupsKey = null; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
private static final class Plain extends SelectGroups { |
| 148 |
|
| 149 |
private ArrayList<Object[]> rows; |
| 150 |
|
| 151 |
/** |
| 152 |
* Cursor for {@link #next()} method. |
| 153 |
*/ |
| 154 |
private Iterator<Object[]> cursor; |
| 155 |
|
| 156 |
Plain(SessionLocal session, ArrayList<Expression> expressions) { |
| 157 |
super(session, expressions); |
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
public void reset() { |
| 162 |
super.reset(); |
| 163 |
rows = new ArrayList<>(); |
| 164 |
cursor = null; |
| 165 |
} |
| 166 |
|
| 167 |
@Override |
| 168 |
public void nextSource() { |
| 169 |
Object[] values = createRow(); |
| 170 |
rows.add(values); |
| 171 |
currentGroupByExprData = values; |
| 172 |
currentGroupRowId++; |
| 173 |
} |
| 174 |
|
| 175 |
@Override |
| 176 |
void updateCurrentGroupExprData() { |
| 177 |
rows.set(rows.size() - 1, currentGroupByExprData); |
| 178 |
} |
| 179 |
|
| 180 |
@Override |
| 181 |
public void done() { |
| 182 |
super.done(); |
| 183 |
cursor = rows.iterator(); |
| 184 |
} |
| 185 |
|
| 186 |
@Override |
| 187 |
public ValueRow next() { |
| 188 |
if (cursor.hasNext()) { |
| 189 |
currentGroupByExprData = cursor.next(); |
| 190 |
currentGroupRowId++; |
| 191 |
return ValueRow.EMPTY; |
| 192 |
} |
| 193 |
return null; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* The database session. |
| 199 |
*/ |
| 200 |
final SessionLocal session; |
| 201 |
|
| 202 |
/** |
| 203 |
* The query's column list, including invisible expressions such as order by expressions. |
| 204 |
*/ |
| 205 |
final ArrayList<Expression> expressions; |
| 206 |
|
| 207 |
/** |
| 208 |
* The array of current group-by expression data e.g. AggregateData. |
| 209 |
*/ |
| 210 |
Object[] currentGroupByExprData; |
| 211 |
|
| 212 |
/** |
| 213 |
* Maps an expression object to an index, to use in accessing the Object[] |
| 214 |
* pointed to by groupByData. |
| 215 |
*/ |
| 216 |
private final HashMap<Expression, Integer> exprToIndexInGroupByData = new HashMap<>(); |
| 217 |
|
| 218 |
/** |
| 219 |
* Maps an window expression object to its data. |
| 220 |
*/ |
| 221 |
private final HashMap<DataAnalysisOperation, PartitionData> windowData = new HashMap<>(); |
| 222 |
|
| 223 |
/** |
| 224 |
* Maps an partitioned window expression object to its data. |
| 225 |
*/ |
| 226 |
private final HashMap<DataAnalysisOperation, TreeMap<Value, PartitionData>> windowPartitionData = new HashMap<>(); |
| 227 |
|
| 228 |
/** |
| 229 |
* The id of the current group. |
| 230 |
*/ |
| 231 |
int currentGroupRowId; |
| 232 |
|
| 233 |
/** |
| 234 |
* Creates new instance of grouped data. |
| 235 |
* |
| 236 |
* @param session |
| 237 |
* the session |
| 238 |
* @param expressions |
| 239 |
* the expressions |
| 240 |
* @param isGroupQuery |
| 241 |
* is this query is a group query |
| 242 |
* @param groupIndex |
| 243 |
* the indexes of group expressions, or null |
| 244 |
* @return new instance of the grouped data. |
| 245 |
*/ |
| 246 |
public static SelectGroups getInstance(SessionLocal session, ArrayList<Expression> expressions, |
| 247 |
boolean isGroupQuery, int[] groupIndex) { |
| 248 |
return isGroupQuery ? new Grouped(session, expressions, groupIndex) : new Plain(session, expressions); |
| 249 |
} |
| 250 |
|
| 251 |
SelectGroups(SessionLocal session, ArrayList<Expression> expressions) { |
| 252 |
this.session = session; |
| 253 |
this.expressions = expressions; |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Is there currently a group-by active. |
| 258 |
* |
| 259 |
* @return {@code true} if there is currently a group-by active, |
| 260 |
* otherwise returns {@code false}. |
| 261 |
*/ |
| 262 |
public boolean isCurrentGroup() { |
| 263 |
return currentGroupByExprData != null; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Get the group-by data for the current group and the passed in expression. |
| 268 |
* |
| 269 |
* @param expr |
| 270 |
* expression |
| 271 |
* @return expression data or null |
| 272 |
*/ |
| 273 |
public final Object getCurrentGroupExprData(Expression expr) { |
| 274 |
Integer index = exprToIndexInGroupByData.get(expr); |
| 275 |
if (index == null) { |
| 276 |
return null; |
| 277 |
} |
| 278 |
return currentGroupByExprData[index]; |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Set the group-by data for the current group and the passed in expression. |
| 283 |
* |
| 284 |
* @param expr |
| 285 |
* expression |
| 286 |
* @param obj |
| 287 |
* expression data to set |
| 288 |
*/ |
| 289 |
public final void setCurrentGroupExprData(Expression expr, Object obj) { |
| 290 |
Integer index = exprToIndexInGroupByData.get(expr); |
| 291 |
if (index != null) { |
| 292 |
assert currentGroupByExprData[index] == null; |
| 293 |
currentGroupByExprData[index] = obj; |
| 294 |
return; |
| 295 |
} |
| 296 |
index = exprToIndexInGroupByData.size(); |
| 297 |
exprToIndexInGroupByData.put(expr, index); |
| 298 |
if (index >= currentGroupByExprData.length) { |
| 299 |
currentGroupByExprData = Arrays.copyOf(currentGroupByExprData, currentGroupByExprData.length * 2); |
| 300 |
updateCurrentGroupExprData(); |
| 301 |
} |
| 302 |
currentGroupByExprData[index] = obj; |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* Creates new object arrays to holds group-by data. |
| 307 |
* |
| 308 |
* @return new object array to holds group-by data. |
| 309 |
*/ |
| 310 |
final Object[] createRow() { |
| 311 |
return new Object[Math.max(exprToIndexInGroupByData.size(), expressions.size())]; |
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* Get the window data for the specified expression. |
| 316 |
* |
| 317 |
* @param expr |
| 318 |
* expression |
| 319 |
* @param partitionKey |
| 320 |
* a key of partition |
| 321 |
* @return expression data or null |
| 322 |
*/ |
| 323 |
public final PartitionData getWindowExprData(DataAnalysisOperation expr, Value partitionKey) { |
| 324 |
if (partitionKey == null) { |
| 325 |
return windowData.get(expr); |
| 326 |
} else { |
| 327 |
TreeMap<Value, PartitionData> map = windowPartitionData.get(expr); |
| 328 |
return map != null ? map.get(partitionKey) : null; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Set the window data for the specified expression. |
| 334 |
* |
| 335 |
* @param expr |
| 336 |
* expression |
| 337 |
* @param partitionKey |
| 338 |
* a key of partition |
| 339 |
* @param obj |
| 340 |
* window expression data to set |
| 341 |
*/ |
| 342 |
public final void setWindowExprData(DataAnalysisOperation expr, Value partitionKey, PartitionData obj) { |
| 343 |
if (partitionKey == null) { |
| 344 |
Object old = windowData.put(expr, obj); |
| 345 |
assert old == null; |
| 346 |
} else { |
| 347 |
TreeMap<Value, PartitionData> map = windowPartitionData.get(expr); |
| 348 |
if (map == null) { |
| 349 |
map = new TreeMap<>(session.getDatabase().getCompareMode()); |
| 350 |
windowPartitionData.put(expr, map); |
| 351 |
} |
| 352 |
map.put(partitionKey, obj); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* Update group-by data specified by implementation. |
| 358 |
*/ |
| 359 |
abstract void updateCurrentGroupExprData(); |
| 360 |
|
| 361 |
/** |
| 362 |
* Returns identity of the current row. Used by aggregates to check whether |
| 363 |
* they already processed this row or not. |
| 364 |
* |
| 365 |
* @return identity of the current row |
| 366 |
*/ |
| 367 |
public int getCurrentGroupRowId() { |
| 368 |
return currentGroupRowId; |
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
* Resets this group data for reuse. |
| 373 |
*/ |
| 374 |
public void reset() { |
| 375 |
currentGroupByExprData = null; |
| 376 |
exprToIndexInGroupByData.clear(); |
| 377 |
windowData.clear(); |
| 378 |
windowPartitionData.clear(); |
| 379 |
currentGroupRowId = 0; |
| 380 |
} |
| 381 |
|
| 382 |
/** |
| 383 |
* Invoked for each source row to evaluate group key and setup all necessary |
| 384 |
* data for aggregates. |
| 385 |
*/ |
| 386 |
public abstract void nextSource(); |
| 387 |
|
| 388 |
/** |
| 389 |
* Invoked after all source rows are evaluated. |
| 390 |
*/ |
| 391 |
public void done() { |
| 392 |
currentGroupRowId = 0; |
| 393 |
} |
| 394 |
|
| 395 |
/** |
| 396 |
* Returns the key of the next group. |
| 397 |
* |
| 398 |
* @return the key of the next group, or null |
| 399 |
*/ |
| 400 |
public abstract ValueRow next(); |
| 401 |
|
| 402 |
/** |
| 403 |
* Removes the data for the current key. |
| 404 |
* |
| 405 |
* @see #next() |
| 406 |
*/ |
| 407 |
public void remove() { |
| 408 |
throw new UnsupportedOperationException(); |
| 409 |
} |
| 410 |
|
| 411 |
/** |
| 412 |
* Resets this group data for reuse in lazy mode. |
| 413 |
*/ |
| 414 |
public void resetLazy() { |
| 415 |
currentGroupByExprData = null; |
| 416 |
currentGroupRowId = 0; |
| 417 |
} |
| 418 |
|
| 419 |
/** |
| 420 |
* Moves group data to the next group in lazy mode. |
| 421 |
*/ |
| 422 |
public void nextLazyGroup() { |
| 423 |
currentGroupByExprData = new Object[Math.max(exprToIndexInGroupByData.size(), expressions.size())]; |
| 424 |
} |
| 425 |
|
| 426 |
/** |
| 427 |
* Moves group data to the next row in lazy mode. |
| 428 |
*/ |
| 429 |
public void nextLazyRow() { |
| 430 |
currentGroupRowId++; |
| 431 |
} |
| 432 |
|
| 433 |
} |