| 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.mvstore.db; |
| 7 |
|
| 8 |
import java.util.Arrays; |
| 9 |
import java.util.BitSet; |
| 10 |
|
| 11 |
import org.h2.engine.Database; |
| 12 |
import org.h2.expression.Expression; |
| 13 |
import org.h2.message.DbException; |
| 14 |
import org.h2.mvstore.Cursor; |
| 15 |
import org.h2.mvstore.MVMap; |
| 16 |
import org.h2.mvstore.MVMap.Builder; |
| 17 |
import org.h2.mvstore.type.DataType; |
| 18 |
import org.h2.mvstore.type.LongDataType; |
| 19 |
import org.h2.result.ResultExternal; |
| 20 |
import org.h2.result.RowFactory.DefaultRowFactory; |
| 21 |
import org.h2.result.SortOrder; |
| 22 |
import org.h2.value.TypeInfo; |
| 23 |
import org.h2.value.Value; |
| 24 |
import org.h2.value.ValueNull; |
| 25 |
import org.h2.value.ValueRow; |
| 26 |
|
| 27 |
/** |
| 28 |
* Sorted temporary result. |
| 29 |
* |
| 30 |
* <p> |
| 31 |
* This result is used for distinct and/or sorted results. |
| 32 |
* </p> |
| 33 |
*/ |
| 34 |
class MVSortedTempResult extends MVTempResult { |
| 35 |
|
| 36 |
/** |
| 37 |
* Whether this result is a standard distinct result. |
| 38 |
*/ |
| 39 |
private final boolean distinct; |
| 40 |
|
| 41 |
/** |
| 42 |
* Distinct indexes for DISTINCT ON results. |
| 43 |
*/ |
| 44 |
private final int[] distinctIndexes; |
| 45 |
|
| 46 |
/** |
| 47 |
* Mapping of indexes of columns to its positions in the store, or {@code null} |
| 48 |
* if columns are not reordered. |
| 49 |
*/ |
| 50 |
private final int[] indexes; |
| 51 |
|
| 52 |
/** |
| 53 |
* Map with rows as keys and counts of duplicate rows as values. If this map is |
| 54 |
* distinct all values are 1. |
| 55 |
*/ |
| 56 |
private final MVMap<ValueRow, Long> map; |
| 57 |
|
| 58 |
/** |
| 59 |
* Optional index. This index is created only if result is distinct and |
| 60 |
* {@code columnCount != distinctColumnCount} or if |
| 61 |
* {@link #contains(Value[])} method is invoked. Only the root result should |
| 62 |
* have an index if required. |
| 63 |
*/ |
| 64 |
private MVMap<ValueRow, Value> index; |
| 65 |
|
| 66 |
/** |
| 67 |
* Used for DISTINCT ON in presence of ORDER BY. |
| 68 |
*/ |
| 69 |
private ValueDataType orderedDistinctOnType; |
| 70 |
|
| 71 |
/** |
| 72 |
* Cursor for the {@link #next()} method. |
| 73 |
*/ |
| 74 |
private Cursor<ValueRow, Long> cursor; |
| 75 |
|
| 76 |
/** |
| 77 |
* Current value for the {@link #next()} method. Used in non-distinct results |
| 78 |
* with duplicate rows. |
| 79 |
*/ |
| 80 |
private Value[] current; |
| 81 |
|
| 82 |
/** |
| 83 |
* Count of remaining duplicate rows for the {@link #next()} method. Used in |
| 84 |
* non-distinct results. |
| 85 |
*/ |
| 86 |
private long valueCount; |
| 87 |
|
| 88 |
/** |
| 89 |
* Creates a shallow copy of the result. |
| 90 |
* |
| 91 |
* @param parent |
| 92 |
* parent result |
| 93 |
*/ |
| 94 |
private MVSortedTempResult(MVSortedTempResult parent) { |
| 95 |
super(parent); |
| 96 |
this.distinct = parent.distinct; |
| 97 |
this.distinctIndexes = parent.distinctIndexes; |
| 98 |
this.indexes = parent.indexes; |
| 99 |
this.map = parent.map; |
| 100 |
this.rowCount = parent.rowCount; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Creates a new sorted temporary result. |
| 105 |
* |
| 106 |
* @param database |
| 107 |
* database |
| 108 |
* @param expressions |
| 109 |
* column expressions |
| 110 |
* @param distinct |
| 111 |
* whether this result should be distinct |
| 112 |
* @param distinctIndexes |
| 113 |
* indexes of distinct columns for DISTINCT ON results |
| 114 |
* @param visibleColumnCount |
| 115 |
* count of visible columns |
| 116 |
* @param resultColumnCount |
| 117 |
* the number of columns including visible columns and additional |
| 118 |
* virtual columns for ORDER BY and DISTINCT ON clauses |
| 119 |
* @param sort |
| 120 |
* sort order, or {@code null} if this result does not need any |
| 121 |
* sorting |
| 122 |
*/ |
| 123 |
MVSortedTempResult(Database database, Expression[] expressions, boolean distinct, int[] distinctIndexes, |
| 124 |
int visibleColumnCount, int resultColumnCount, SortOrder sort) { |
| 125 |
super(database, expressions, visibleColumnCount, resultColumnCount); |
| 126 |
this.distinct = distinct; |
| 127 |
this.distinctIndexes = distinctIndexes; |
| 128 |
int[] sortTypes = new int[resultColumnCount]; |
| 129 |
int[] indexes; |
| 130 |
if (sort != null) { |
| 131 |
/* |
| 132 |
* If sorting is specified we need to reorder columns in requested order and set |
| 133 |
* sort types (ASC, DESC etc) for them properly. |
| 134 |
*/ |
| 135 |
indexes = new int[resultColumnCount]; |
| 136 |
int[] colIndex = sort.getQueryColumnIndexes(); |
| 137 |
int len = colIndex.length; |
| 138 |
// This set is used to remember columns that are already included |
| 139 |
BitSet used = new BitSet(); |
| 140 |
for (int i = 0; i < len; i++) { |
| 141 |
int idx = colIndex[i]; |
| 142 |
assert !used.get(idx); |
| 143 |
used.set(idx); |
| 144 |
indexes[i] = idx; |
| 145 |
sortTypes[i] = sort.getSortTypes()[i]; |
| 146 |
} |
| 147 |
/* |
| 148 |
* Because this result may have more columns than specified in sorting we need |
| 149 |
* to add all remaining columns to the mapping of columns. A default sorting |
| 150 |
* order (ASC / 0) will be used for them. |
| 151 |
*/ |
| 152 |
int idx = 0; |
| 153 |
for (int i = len; i < resultColumnCount; i++) { |
| 154 |
idx = used.nextClearBit(idx); |
| 155 |
indexes[i] = idx; |
| 156 |
idx++; |
| 157 |
} |
| 158 |
/* |
| 159 |
* Sometimes columns may be not reordered. Because reordering of columns |
| 160 |
* slightly slows down other methods we check whether columns are really |
| 161 |
* reordered or have the same order. |
| 162 |
*/ |
| 163 |
sameOrder: { |
| 164 |
for (int i = 0; i < resultColumnCount; i++) { |
| 165 |
if (indexes[i] != i) { |
| 166 |
// Columns are reordered |
| 167 |
break sameOrder; |
| 168 |
} |
| 169 |
} |
| 170 |
/* |
| 171 |
* Columns are not reordered, set this field to null to disable reordering in |
| 172 |
* other methods. |
| 173 |
*/ |
| 174 |
indexes = null; |
| 175 |
} |
| 176 |
} else { |
| 177 |
// Columns are not reordered if sort order is not specified |
| 178 |
indexes = null; |
| 179 |
} |
| 180 |
this.indexes = indexes; |
| 181 |
ValueDataType keyType = new ValueDataType(database, SortOrder.addNullOrdering(database, sortTypes)); |
| 182 |
if (indexes != null) { |
| 183 |
int l = indexes.length; |
| 184 |
TypeInfo[] types = new TypeInfo[l]; |
| 185 |
for (int i = 0; i < l; i++) { |
| 186 |
types[i] = expressions[indexes[i]].getType(); |
| 187 |
} |
| 188 |
keyType.setRowFactory(DefaultRowFactory.INSTANCE.createRowFactory(database, database.getCompareMode(), |
| 189 |
database, types, null, false)); |
| 190 |
} else { |
| 191 |
keyType.setRowFactory(DefaultRowFactory.INSTANCE.createRowFactory(database, database.getCompareMode(), |
| 192 |
database, expressions, null, false)); |
| 193 |
} |
| 194 |
Builder<ValueRow, Long> builder = new MVMap.Builder<ValueRow, Long>().keyType(keyType) |
| 195 |
.valueType(LongDataType.INSTANCE); |
| 196 |
map = store.openMap("tmp", builder); |
| 197 |
if (distinct && resultColumnCount != visibleColumnCount || distinctIndexes != null) { |
| 198 |
int count; |
| 199 |
TypeInfo[] types; |
| 200 |
if (distinctIndexes != null) { |
| 201 |
count = distinctIndexes.length; |
| 202 |
types = new TypeInfo[count]; |
| 203 |
for (int i = 0; i < count; i++) { |
| 204 |
types[i] = expressions[distinctIndexes[i]].getType(); |
| 205 |
} |
| 206 |
} else { |
| 207 |
count = visibleColumnCount; |
| 208 |
types = new TypeInfo[count]; |
| 209 |
for (int i = 0; i < count; i++) { |
| 210 |
types[i] = expressions[i].getType(); |
| 211 |
} |
| 212 |
} |
| 213 |
ValueDataType distinctType = new ValueDataType(database, new int[count]); |
| 214 |
distinctType.setRowFactory(DefaultRowFactory.INSTANCE.createRowFactory(database, database.getCompareMode(), |
| 215 |
database, types, null, false)); |
| 216 |
DataType<Value> distinctValueType; |
| 217 |
if (distinctIndexes != null && sort != null) { |
| 218 |
distinctValueType = orderedDistinctOnType = keyType; |
| 219 |
} else { |
| 220 |
distinctValueType = NullValueDataType.INSTANCE; |
| 221 |
} |
| 222 |
Builder<ValueRow, Value> indexBuilder = new MVMap.Builder<ValueRow, Value>().keyType(distinctType) |
| 223 |
.valueType(distinctValueType); |
| 224 |
index = store.openMap("idx", indexBuilder); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
@Override |
| 229 |
public int addRow(Value[] values) { |
| 230 |
assert parent == null; |
| 231 |
ValueRow key = getKey(values); |
| 232 |
if (distinct || distinctIndexes != null) { |
| 233 |
if (distinctIndexes != null) { |
| 234 |
int cnt = distinctIndexes.length; |
| 235 |
Value[] newValues = new Value[cnt]; |
| 236 |
for (int i = 0; i < cnt; i++) { |
| 237 |
newValues[i] = values[distinctIndexes[i]]; |
| 238 |
} |
| 239 |
ValueRow distinctRow = ValueRow.get(newValues); |
| 240 |
if (orderedDistinctOnType == null) { |
| 241 |
if (index.putIfAbsent(distinctRow, ValueNull.INSTANCE) != null) { |
| 242 |
return rowCount; |
| 243 |
} |
| 244 |
} else { |
| 245 |
ValueRow previous = (ValueRow) index.get(distinctRow); |
| 246 |
if (previous == null) { |
| 247 |
index.put(distinctRow, key); |
| 248 |
} else if (orderedDistinctOnType.compare(previous, key) > 0) { |
| 249 |
map.remove(previous); |
| 250 |
rowCount--; |
| 251 |
index.put(distinctRow, key); |
| 252 |
} else { |
| 253 |
return rowCount; |
| 254 |
} |
| 255 |
} |
| 256 |
} else if (visibleColumnCount != resultColumnCount) { |
| 257 |
ValueRow distinctRow = ValueRow.get(Arrays.copyOf(values, visibleColumnCount)); |
| 258 |
if (index.putIfAbsent(distinctRow, ValueNull.INSTANCE) != null) { |
| 259 |
return rowCount; |
| 260 |
} |
| 261 |
} |
| 262 |
// Add a row and increment the counter only if row does not exist |
| 263 |
if (map.putIfAbsent(key, 1L) == null) { |
| 264 |
rowCount++; |
| 265 |
} |
| 266 |
} else { |
| 267 |
// Try to set counter to 1 first if such row does not exist yet |
| 268 |
Long old = map.putIfAbsent(key, 1L); |
| 269 |
if (old != null) { |
| 270 |
// This rows is already in the map, increment its own counter |
| 271 |
map.put(key, old + 1); |
| 272 |
} |
| 273 |
rowCount++; |
| 274 |
} |
| 275 |
return rowCount; |
| 276 |
} |
| 277 |
|
| 278 |
@Override |
| 279 |
public boolean contains(Value[] values) { |
| 280 |
// Only parent result maintains the index |
| 281 |
if (parent != null) { |
| 282 |
return parent.contains(values); |
| 283 |
} |
| 284 |
assert distinct; |
| 285 |
if (visibleColumnCount != resultColumnCount) { |
| 286 |
return index.containsKey(ValueRow.get(values)); |
| 287 |
} |
| 288 |
return map.containsKey(getKey(values)); |
| 289 |
} |
| 290 |
|
| 291 |
@Override |
| 292 |
public synchronized ResultExternal createShallowCopy() { |
| 293 |
if (parent != null) { |
| 294 |
return parent.createShallowCopy(); |
| 295 |
} |
| 296 |
if (closed) { |
| 297 |
return null; |
| 298 |
} |
| 299 |
childCount++; |
| 300 |
return new MVSortedTempResult(this); |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* Reorder values if required and convert them into {@link ValueRow}. |
| 305 |
* |
| 306 |
* @param values |
| 307 |
* values |
| 308 |
* @return ValueRow for maps |
| 309 |
*/ |
| 310 |
private ValueRow getKey(Value[] values) { |
| 311 |
if (indexes != null) { |
| 312 |
Value[] r = new Value[indexes.length]; |
| 313 |
for (int i = 0; i < indexes.length; i++) { |
| 314 |
r[i] = values[indexes[i]]; |
| 315 |
} |
| 316 |
values = r; |
| 317 |
} |
| 318 |
return ValueRow.get(values); |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Reorder values back if required. |
| 323 |
* |
| 324 |
* @param key |
| 325 |
* reordered values |
| 326 |
* @return original values |
| 327 |
*/ |
| 328 |
private Value[] getValue(Value[] key) { |
| 329 |
if (indexes != null) { |
| 330 |
Value[] r = new Value[indexes.length]; |
| 331 |
for (int i = 0; i < indexes.length; i++) { |
| 332 |
r[indexes[i]] = key[i]; |
| 333 |
} |
| 334 |
key = r; |
| 335 |
} |
| 336 |
return key; |
| 337 |
} |
| 338 |
|
| 339 |
@Override |
| 340 |
public Value[] next() { |
| 341 |
if (cursor == null) { |
| 342 |
cursor = map.cursor(null); |
| 343 |
current = null; |
| 344 |
valueCount = 0L; |
| 345 |
} |
| 346 |
// If we have multiple rows with the same values return them all |
| 347 |
if (--valueCount > 0) { |
| 348 |
/* |
| 349 |
* Underflow in valueCount is hypothetically possible after a lot of invocations |
| 350 |
* (not really possible in practice), but current will be null anyway. |
| 351 |
*/ |
| 352 |
return current; |
| 353 |
} |
| 354 |
if (!cursor.hasNext()) { |
| 355 |
// Set current to null to be sure |
| 356 |
current = null; |
| 357 |
return null; |
| 358 |
} |
| 359 |
// Read the next row |
| 360 |
current = getValue(cursor.next().getList()); |
| 361 |
/* |
| 362 |
* If valueCount is greater than 1 that is possible for non-distinct results the |
| 363 |
* following invocations of next() will use this.current and this.valueCount. |
| 364 |
*/ |
| 365 |
valueCount = cursor.getValue(); |
| 366 |
return current; |
| 367 |
} |
| 368 |
|
| 369 |
@Override |
| 370 |
public int removeRow(Value[] values) { |
| 371 |
assert parent == null && distinct; |
| 372 |
if (visibleColumnCount != resultColumnCount) { |
| 373 |
throw DbException.getUnsupportedException("removeRow()"); |
| 374 |
} |
| 375 |
// If an entry was removed decrement the counter |
| 376 |
if (map.remove(getKey(values)) != null) { |
| 377 |
rowCount--; |
| 378 |
} |
| 379 |
return rowCount; |
| 380 |
} |
| 381 |
|
| 382 |
@Override |
| 383 |
public void reset() { |
| 384 |
cursor = null; |
| 385 |
current = null; |
| 386 |
valueCount = 0L; |
| 387 |
} |
| 388 |
|
| 389 |
} |