| 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.index; |
| 7 |
|
| 8 |
import java.util.ArrayList; |
| 9 |
|
| 10 |
import org.h2.engine.SessionLocal; |
| 11 |
import org.h2.expression.condition.Comparison; |
| 12 |
import org.h2.message.DbException; |
| 13 |
import org.h2.result.ResultInterface; |
| 14 |
import org.h2.result.Row; |
| 15 |
import org.h2.result.SearchRow; |
| 16 |
import org.h2.result.SortOrder; |
| 17 |
import org.h2.table.Column; |
| 18 |
import org.h2.table.IndexColumn; |
| 19 |
import org.h2.table.Table; |
| 20 |
import org.h2.value.Value; |
| 21 |
import org.h2.value.ValueGeometry; |
| 22 |
import org.h2.value.ValueNull; |
| 23 |
|
| 24 |
/** |
| 25 |
* The filter used to walk through an index. This class supports IN(..) |
| 26 |
* and IN(SELECT ...) optimizations. |
| 27 |
* |
| 28 |
* @author Thomas Mueller |
| 29 |
* @author Noel Grandin |
| 30 |
* @author Nicolas Fortin, Atelier SIG, IRSTV FR CNRS 24888 |
| 31 |
*/ |
| 32 |
public class IndexCursor implements Cursor { |
| 33 |
|
| 34 |
private SessionLocal session; |
| 35 |
private Index index; |
| 36 |
private Table table; |
| 37 |
private IndexColumn[] indexColumns; |
| 38 |
private boolean alwaysFalse; |
| 39 |
|
| 40 |
private SearchRow start, end, intersects; |
| 41 |
private Cursor cursor; |
| 42 |
private Column inColumn; |
| 43 |
private int inListIndex; |
| 44 |
private Value[] inList; |
| 45 |
private ResultInterface inResult; |
| 46 |
|
| 47 |
public IndexCursor() { |
| 48 |
} |
| 49 |
|
| 50 |
public void setIndex(Index index) { |
| 51 |
this.index = index; |
| 52 |
this.table = index.getTable(); |
| 53 |
Column[] columns = table.getColumns(); |
| 54 |
indexColumns = new IndexColumn[columns.length]; |
| 55 |
IndexColumn[] idxCols = index.getIndexColumns(); |
| 56 |
if (idxCols != null) { |
| 57 |
for (int i = 0, len = columns.length; i < len; i++) { |
| 58 |
int idx = index.getColumnIndex(columns[i]); |
| 59 |
if (idx >= 0) { |
| 60 |
indexColumns[i] = idxCols[idx]; |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Prepare this index cursor to make a lookup in index. |
| 68 |
* |
| 69 |
* @param s Session. |
| 70 |
* @param indexConditions Index conditions. |
| 71 |
*/ |
| 72 |
public void prepare(SessionLocal s, ArrayList<IndexCondition> indexConditions) { |
| 73 |
session = s; |
| 74 |
alwaysFalse = false; |
| 75 |
start = end = null; |
| 76 |
inList = null; |
| 77 |
inColumn = null; |
| 78 |
inResult = null; |
| 79 |
intersects = null; |
| 80 |
for (IndexCondition condition : indexConditions) { |
| 81 |
if (condition.isAlwaysFalse()) { |
| 82 |
alwaysFalse = true; |
| 83 |
break; |
| 84 |
} |
| 85 |
// If index can perform only full table scan do not try to use it for regular |
| 86 |
// lookups, each such lookup will perform an own table scan. |
| 87 |
if (index.isFindUsingFullTableScan()) { |
| 88 |
continue; |
| 89 |
} |
| 90 |
Column column = condition.getColumn(); |
| 91 |
switch (condition.getCompareType()) { |
| 92 |
case Comparison.IN_LIST: |
| 93 |
case Comparison.IN_ARRAY: |
| 94 |
if (start == null && end == null) { |
| 95 |
if (canUseIndexForIn(column)) { |
| 96 |
this.inColumn = column; |
| 97 |
inList = condition.getCurrentValueList(s); |
| 98 |
inListIndex = 0; |
| 99 |
} |
| 100 |
} |
| 101 |
break; |
| 102 |
case Comparison.IN_QUERY: |
| 103 |
if (start == null && end == null) { |
| 104 |
if (canUseIndexForIn(column)) { |
| 105 |
this.inColumn = column; |
| 106 |
inResult = condition.getCurrentResult(); |
| 107 |
} |
| 108 |
} |
| 109 |
break; |
| 110 |
default: |
| 111 |
Value v = condition.getCurrentValue(s); |
| 112 |
boolean isStart = condition.isStart(); |
| 113 |
boolean isEnd = condition.isEnd(); |
| 114 |
boolean isIntersects = condition.isSpatialIntersects(); |
| 115 |
int columnId = column.getColumnId(); |
| 116 |
if (columnId != SearchRow.ROWID_INDEX) { |
| 117 |
IndexColumn idxCol = indexColumns[columnId]; |
| 118 |
if (idxCol != null && (idxCol.sortType & SortOrder.DESCENDING) != 0) { |
| 119 |
// if the index column is sorted the other way, we swap |
| 120 |
// end and start NULLS_FIRST / NULLS_LAST is not a |
| 121 |
// problem, as nulls never match anyway |
| 122 |
boolean temp = isStart; |
| 123 |
isStart = isEnd; |
| 124 |
isEnd = temp; |
| 125 |
} |
| 126 |
} |
| 127 |
if (isStart) { |
| 128 |
start = getSearchRow(start, columnId, v, true); |
| 129 |
} |
| 130 |
if (isEnd) { |
| 131 |
end = getSearchRow(end, columnId, v, false); |
| 132 |
} |
| 133 |
if (isIntersects) { |
| 134 |
intersects = getSpatialSearchRow(intersects, columnId, v); |
| 135 |
} |
| 136 |
// An X=? condition will produce less rows than |
| 137 |
// an X IN(..) condition, unless the X IN condition can use the index. |
| 138 |
if ((isStart || isEnd) && !canUseIndexFor(inColumn)) { |
| 139 |
inColumn = null; |
| 140 |
inList = null; |
| 141 |
inResult = null; |
| 142 |
} |
| 143 |
break; |
| 144 |
} |
| 145 |
} |
| 146 |
if (inColumn != null) { |
| 147 |
start = table.getTemplateRow(); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Re-evaluate the start and end values of the index search for rows. |
| 153 |
* |
| 154 |
* @param s the session |
| 155 |
* @param indexConditions the index conditions |
| 156 |
*/ |
| 157 |
public void find(SessionLocal s, ArrayList<IndexCondition> indexConditions) { |
| 158 |
prepare(s, indexConditions); |
| 159 |
if (inColumn != null) { |
| 160 |
return; |
| 161 |
} |
| 162 |
if (!alwaysFalse) { |
| 163 |
if (intersects != null && index instanceof SpatialIndex) { |
| 164 |
cursor = ((SpatialIndex) index).findByGeometry(session, start, end, intersects); |
| 165 |
} else if (index != null) { |
| 166 |
cursor = index.find(session, start, end); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
private boolean canUseIndexForIn(Column column) { |
| 172 |
if (inColumn != null) { |
| 173 |
// only one IN(..) condition can be used at the same time |
| 174 |
return false; |
| 175 |
} |
| 176 |
return canUseIndexFor(column); |
| 177 |
} |
| 178 |
|
| 179 |
private boolean canUseIndexFor(Column column) { |
| 180 |
// The first column of the index must match this column, |
| 181 |
// or it must be a VIEW index (where the column is null). |
| 182 |
// Multiple IN conditions with views are not supported, see |
| 183 |
// IndexCondition.getMask. |
| 184 |
IndexColumn[] cols = index.getIndexColumns(); |
| 185 |
if (cols == null) { |
| 186 |
return true; |
| 187 |
} |
| 188 |
IndexColumn idxCol = cols[0]; |
| 189 |
return idxCol == null || idxCol.column == column; |
| 190 |
} |
| 191 |
|
| 192 |
private SearchRow getSpatialSearchRow(SearchRow row, int columnId, Value v) { |
| 193 |
if (row == null) { |
| 194 |
row = table.getTemplateRow(); |
| 195 |
} else if (row.getValue(columnId) != null) { |
| 196 |
// if an object needs to overlap with both a and b, |
| 197 |
// then it needs to overlap with the union of a and b |
| 198 |
// (not the intersection) |
| 199 |
ValueGeometry vg = row.getValue(columnId).convertToGeometry(null); |
| 200 |
v = v.convertToGeometry(null).getEnvelopeUnion(vg); |
| 201 |
} |
| 202 |
if (columnId == SearchRow.ROWID_INDEX) { |
| 203 |
row.setKey(v == ValueNull.INSTANCE ? Long.MIN_VALUE : v.getLong()); |
| 204 |
} else { |
| 205 |
row.setValue(columnId, v); |
| 206 |
} |
| 207 |
return row; |
| 208 |
} |
| 209 |
|
| 210 |
private SearchRow getSearchRow(SearchRow row, int columnId, Value v, boolean max) { |
| 211 |
if (row == null) { |
| 212 |
row = table.getTemplateRow(); |
| 213 |
} else { |
| 214 |
v = getMax(row.getValue(columnId), v, max); |
| 215 |
} |
| 216 |
if (columnId == SearchRow.ROWID_INDEX) { |
| 217 |
row.setKey(v == ValueNull.INSTANCE ? Long.MIN_VALUE : v.getLong()); |
| 218 |
} else { |
| 219 |
row.setValue(columnId, v); |
| 220 |
} |
| 221 |
return row; |
| 222 |
} |
| 223 |
|
| 224 |
private Value getMax(Value a, Value b, boolean bigger) { |
| 225 |
if (a == null) { |
| 226 |
return b; |
| 227 |
} else if (b == null) { |
| 228 |
return a; |
| 229 |
} |
| 230 |
// IS NULL must be checked later |
| 231 |
if (a == ValueNull.INSTANCE) { |
| 232 |
return b; |
| 233 |
} else if (b == ValueNull.INSTANCE) { |
| 234 |
return a; |
| 235 |
} |
| 236 |
int comp = session.compare(a, b); |
| 237 |
if (comp == 0) { |
| 238 |
return a; |
| 239 |
} |
| 240 |
return (comp > 0) == bigger ? a : b; |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Check if the result is empty for sure. |
| 245 |
* |
| 246 |
* @return true if it is |
| 247 |
*/ |
| 248 |
public boolean isAlwaysFalse() { |
| 249 |
return alwaysFalse; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Get start search row. |
| 254 |
* |
| 255 |
* @return search row |
| 256 |
*/ |
| 257 |
public SearchRow getStart() { |
| 258 |
return start; |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Get end search row. |
| 263 |
* |
| 264 |
* @return search row |
| 265 |
*/ |
| 266 |
public SearchRow getEnd() { |
| 267 |
return end; |
| 268 |
} |
| 269 |
|
| 270 |
@Override |
| 271 |
public Row get() { |
| 272 |
if (cursor == null) { |
| 273 |
return null; |
| 274 |
} |
| 275 |
return cursor.get(); |
| 276 |
} |
| 277 |
|
| 278 |
@Override |
| 279 |
public SearchRow getSearchRow() { |
| 280 |
return cursor.getSearchRow(); |
| 281 |
} |
| 282 |
|
| 283 |
@Override |
| 284 |
public boolean next() { |
| 285 |
while (true) { |
| 286 |
if (cursor == null) { |
| 287 |
nextCursor(); |
| 288 |
if (cursor == null) { |
| 289 |
return false; |
| 290 |
} |
| 291 |
} |
| 292 |
if (cursor.next()) { |
| 293 |
return true; |
| 294 |
} |
| 295 |
cursor = null; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
private void nextCursor() { |
| 300 |
if (inList != null) { |
| 301 |
while (inListIndex < inList.length) { |
| 302 |
Value v = inList[inListIndex++]; |
| 303 |
if (v != ValueNull.INSTANCE) { |
| 304 |
find(v); |
| 305 |
break; |
| 306 |
} |
| 307 |
} |
| 308 |
} else if (inResult != null) { |
| 309 |
while (inResult.next()) { |
| 310 |
Value v = inResult.currentRow()[0]; |
| 311 |
if (v != ValueNull.INSTANCE) { |
| 312 |
find(v); |
| 313 |
break; |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
private void find(Value v) { |
| 320 |
v = inColumn.convert(session, v); |
| 321 |
int id = inColumn.getColumnId(); |
| 322 |
start.setValue(id, v); |
| 323 |
cursor = index.find(session, start, start); |
| 324 |
} |
| 325 |
|
| 326 |
@Override |
| 327 |
public boolean previous() { |
| 328 |
throw DbException.getInternalError(toString()); |
| 329 |
} |
| 330 |
|
| 331 |
} |