| 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 |
import java.util.Arrays; |
| 10 |
import java.util.List; |
| 11 |
import java.util.TreeSet; |
| 12 |
|
| 13 |
import org.h2.command.query.Query; |
| 14 |
import org.h2.engine.SessionLocal; |
| 15 |
import org.h2.expression.Expression; |
| 16 |
import org.h2.expression.ExpressionColumn; |
| 17 |
import org.h2.expression.ExpressionVisitor; |
| 18 |
import org.h2.expression.condition.Comparison; |
| 19 |
import org.h2.message.DbException; |
| 20 |
import org.h2.result.ResultInterface; |
| 21 |
import org.h2.table.Column; |
| 22 |
import org.h2.table.TableType; |
| 23 |
import org.h2.value.Value; |
| 24 |
import org.h2.value.ValueArray; |
| 25 |
|
| 26 |
/** |
| 27 |
* A index condition object is made for each condition that can potentially use |
| 28 |
* an index. This class does not extend expression, but in general there is one |
| 29 |
* expression that maps to each index condition. |
| 30 |
* |
| 31 |
* @author Thomas Mueller |
| 32 |
* @author Noel Grandin |
| 33 |
* @author Nicolas Fortin, Atelier SIG, IRSTV FR CNRS 24888 |
| 34 |
*/ |
| 35 |
public class IndexCondition { |
| 36 |
|
| 37 |
/** |
| 38 |
* A bit of a search mask meaning 'equal'. |
| 39 |
*/ |
| 40 |
public static final int EQUALITY = 1; |
| 41 |
|
| 42 |
/** |
| 43 |
* A bit of a search mask meaning 'larger or equal'. |
| 44 |
*/ |
| 45 |
public static final int START = 2; |
| 46 |
|
| 47 |
/** |
| 48 |
* A bit of a search mask meaning 'smaller or equal'. |
| 49 |
*/ |
| 50 |
public static final int END = 4; |
| 51 |
|
| 52 |
/** |
| 53 |
* A search mask meaning 'between'. |
| 54 |
*/ |
| 55 |
public static final int RANGE = START | END; |
| 56 |
|
| 57 |
/** |
| 58 |
* A bit of a search mask meaning 'the condition is always false'. |
| 59 |
*/ |
| 60 |
public static final int ALWAYS_FALSE = 8; |
| 61 |
|
| 62 |
/** |
| 63 |
* A bit of a search mask meaning 'spatial intersection'. |
| 64 |
*/ |
| 65 |
public static final int SPATIAL_INTERSECTS = 16; |
| 66 |
|
| 67 |
private final Column column; |
| 68 |
/** |
| 69 |
* see constants in {@link Comparison} |
| 70 |
*/ |
| 71 |
private final int compareType; |
| 72 |
|
| 73 |
private final Expression expression; |
| 74 |
private List<Expression> expressionList; |
| 75 |
private Query expressionQuery; |
| 76 |
|
| 77 |
/** |
| 78 |
* @param compareType the comparison type, see constants in |
| 79 |
* {@link Comparison} |
| 80 |
*/ |
| 81 |
private IndexCondition(int compareType, ExpressionColumn column, |
| 82 |
Expression expression) { |
| 83 |
this.compareType = compareType; |
| 84 |
this.column = column == null ? null : column.getColumn(); |
| 85 |
this.expression = expression; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Create an index condition with the given parameters. |
| 90 |
* |
| 91 |
* @param compareType the comparison type, see constants in |
| 92 |
* {@link Comparison} |
| 93 |
* @param column the column |
| 94 |
* @param expression the expression |
| 95 |
* @return the index condition |
| 96 |
*/ |
| 97 |
public static IndexCondition get(int compareType, ExpressionColumn column, |
| 98 |
Expression expression) { |
| 99 |
return new IndexCondition(compareType, column, expression); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Create an index condition with the compare type IN_LIST and with the |
| 104 |
* given parameters. |
| 105 |
* |
| 106 |
* @param column the column |
| 107 |
* @param list the expression list |
| 108 |
* @return the index condition |
| 109 |
*/ |
| 110 |
public static IndexCondition getInList(ExpressionColumn column, |
| 111 |
List<Expression> list) { |
| 112 |
IndexCondition cond = new IndexCondition(Comparison.IN_LIST, column, |
| 113 |
null); |
| 114 |
cond.expressionList = list; |
| 115 |
return cond; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Create an index condition with the compare type IN_ARRAY and with the |
| 120 |
* given parameters. |
| 121 |
* |
| 122 |
* @param column the column |
| 123 |
* @param array the array |
| 124 |
* @return the index condition |
| 125 |
*/ |
| 126 |
public static IndexCondition getInArray(ExpressionColumn column, Expression array) { |
| 127 |
return new IndexCondition(Comparison.IN_ARRAY, column, array); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Create an index condition with the compare type IN_QUERY and with the |
| 132 |
* given parameters. |
| 133 |
* |
| 134 |
* @param column the column |
| 135 |
* @param query the select statement |
| 136 |
* @return the index condition |
| 137 |
*/ |
| 138 |
public static IndexCondition getInQuery(ExpressionColumn column, Query query) { |
| 139 |
assert query.isRandomAccessResult(); |
| 140 |
IndexCondition cond = new IndexCondition(Comparison.IN_QUERY, column, null); |
| 141 |
cond.expressionQuery = query; |
| 142 |
return cond; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Get the current value of the expression. |
| 147 |
* |
| 148 |
* @param session the session |
| 149 |
* @return the value |
| 150 |
*/ |
| 151 |
public Value getCurrentValue(SessionLocal session) { |
| 152 |
return expression.getValue(session); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Get the current value list of the expression. The value list is of the |
| 157 |
* same type as the column, distinct, and sorted. |
| 158 |
* |
| 159 |
* @param session the session |
| 160 |
* @return the value list |
| 161 |
*/ |
| 162 |
public Value[] getCurrentValueList(SessionLocal session) { |
| 163 |
TreeSet<Value> valueSet = new TreeSet<>(session.getDatabase().getCompareMode()); |
| 164 |
if (compareType == Comparison.IN_LIST) { |
| 165 |
for (Expression e : expressionList) { |
| 166 |
Value v = e.getValue(session); |
| 167 |
v = column.convert(session, v); |
| 168 |
valueSet.add(v); |
| 169 |
} |
| 170 |
} else if (compareType == Comparison.IN_ARRAY) { |
| 171 |
Value v = expression.getValue(session); |
| 172 |
if (v instanceof ValueArray) { |
| 173 |
for (Value e : ((ValueArray) v).getList()) { |
| 174 |
valueSet.add(e); |
| 175 |
} |
| 176 |
} |
| 177 |
} else { |
| 178 |
throw DbException.getInternalError("compareType = " + compareType); |
| 179 |
} |
| 180 |
Value[] array = valueSet.toArray(new Value[valueSet.size()]); |
| 181 |
Arrays.sort(array, session.getDatabase().getCompareMode()); |
| 182 |
return array; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Get the current result of the expression. The rows may not be of the same |
| 187 |
* type, therefore the rows may not be unique. |
| 188 |
* |
| 189 |
* @return the result |
| 190 |
*/ |
| 191 |
public ResultInterface getCurrentResult() { |
| 192 |
return expressionQuery.query(0); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Get the SQL snippet of this comparison. |
| 197 |
* |
| 198 |
* @param sqlFlags formatting flags |
| 199 |
* @return the SQL snippet |
| 200 |
*/ |
| 201 |
public String getSQL(int sqlFlags) { |
| 202 |
if (compareType == Comparison.FALSE) { |
| 203 |
return "FALSE"; |
| 204 |
} |
| 205 |
StringBuilder builder = new StringBuilder(); |
| 206 |
column.getSQL(builder, sqlFlags); |
| 207 |
switch (compareType) { |
| 208 |
case Comparison.EQUAL: |
| 209 |
builder.append(" = "); |
| 210 |
break; |
| 211 |
case Comparison.EQUAL_NULL_SAFE: |
| 212 |
builder.append(expression.isNullConstant() |
| 213 |
|| column.getType().getValueType() == Value.BOOLEAN && expression.isConstant() // |
| 214 |
? " IS " |
| 215 |
: " IS NOT DISTINCT FROM "); |
| 216 |
break; |
| 217 |
case Comparison.BIGGER_EQUAL: |
| 218 |
builder.append(" >= "); |
| 219 |
break; |
| 220 |
case Comparison.BIGGER: |
| 221 |
builder.append(" > "); |
| 222 |
break; |
| 223 |
case Comparison.SMALLER_EQUAL: |
| 224 |
builder.append(" <= "); |
| 225 |
break; |
| 226 |
case Comparison.SMALLER: |
| 227 |
builder.append(" < "); |
| 228 |
break; |
| 229 |
case Comparison.IN_LIST: |
| 230 |
Expression.writeExpressions(builder.append(" IN("), expressionList, sqlFlags).append(')'); |
| 231 |
break; |
| 232 |
case Comparison.IN_ARRAY: |
| 233 |
return expression.getSQL(builder.append(" = ANY("), sqlFlags, Expression.AUTO_PARENTHESES).append(')') |
| 234 |
.toString(); |
| 235 |
case Comparison.IN_QUERY: |
| 236 |
builder.append(" IN("); |
| 237 |
builder.append(expressionQuery.getPlanSQL(sqlFlags)); |
| 238 |
builder.append(')'); |
| 239 |
break; |
| 240 |
case Comparison.SPATIAL_INTERSECTS: |
| 241 |
builder.append(" && "); |
| 242 |
break; |
| 243 |
default: |
| 244 |
throw DbException.getInternalError("type=" + compareType); |
| 245 |
} |
| 246 |
if (expression != null) { |
| 247 |
expression.getSQL(builder, sqlFlags, Expression.AUTO_PARENTHESES); |
| 248 |
} |
| 249 |
return builder.toString(); |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Get the comparison bit mask. |
| 254 |
* |
| 255 |
* @param indexConditions all index conditions |
| 256 |
* @return the mask |
| 257 |
*/ |
| 258 |
public int getMask(ArrayList<IndexCondition> indexConditions) { |
| 259 |
switch (compareType) { |
| 260 |
case Comparison.FALSE: |
| 261 |
return ALWAYS_FALSE; |
| 262 |
case Comparison.EQUAL: |
| 263 |
case Comparison.EQUAL_NULL_SAFE: |
| 264 |
return EQUALITY; |
| 265 |
case Comparison.IN_LIST: |
| 266 |
case Comparison.IN_ARRAY: |
| 267 |
case Comparison.IN_QUERY: |
| 268 |
if (indexConditions.size() > 1) { |
| 269 |
if (TableType.TABLE != column.getTable().getTableType()) { |
| 270 |
// if combined with other conditions, |
| 271 |
// IN(..) can only be used for regular tables |
| 272 |
// test case: |
| 273 |
// create table test(a int, b int, primary key(id, name)); |
| 274 |
// create unique index c on test(b, a); |
| 275 |
// insert into test values(1, 10), (2, 20); |
| 276 |
// select * from (select * from test) |
| 277 |
// where a=1 and b in(10, 20); |
| 278 |
return 0; |
| 279 |
} |
| 280 |
} |
| 281 |
return EQUALITY; |
| 282 |
case Comparison.BIGGER_EQUAL: |
| 283 |
case Comparison.BIGGER: |
| 284 |
return START; |
| 285 |
case Comparison.SMALLER_EQUAL: |
| 286 |
case Comparison.SMALLER: |
| 287 |
return END; |
| 288 |
case Comparison.SPATIAL_INTERSECTS: |
| 289 |
return SPATIAL_INTERSECTS; |
| 290 |
default: |
| 291 |
throw DbException.getInternalError("type=" + compareType); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Check if the result is always false. |
| 297 |
* |
| 298 |
* @return true if the result will always be false |
| 299 |
*/ |
| 300 |
public boolean isAlwaysFalse() { |
| 301 |
return compareType == Comparison.FALSE; |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Check if this index condition is of the type column larger or equal to |
| 306 |
* value. |
| 307 |
* |
| 308 |
* @return true if this is a start condition |
| 309 |
*/ |
| 310 |
public boolean isStart() { |
| 311 |
switch (compareType) { |
| 312 |
case Comparison.EQUAL: |
| 313 |
case Comparison.EQUAL_NULL_SAFE: |
| 314 |
case Comparison.BIGGER_EQUAL: |
| 315 |
case Comparison.BIGGER: |
| 316 |
return true; |
| 317 |
default: |
| 318 |
return false; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* Check if this index condition is of the type column smaller or equal to |
| 324 |
* value. |
| 325 |
* |
| 326 |
* @return true if this is a end condition |
| 327 |
*/ |
| 328 |
public boolean isEnd() { |
| 329 |
switch (compareType) { |
| 330 |
case Comparison.EQUAL: |
| 331 |
case Comparison.EQUAL_NULL_SAFE: |
| 332 |
case Comparison.SMALLER_EQUAL: |
| 333 |
case Comparison.SMALLER: |
| 334 |
return true; |
| 335 |
default: |
| 336 |
return false; |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Check if this index condition is of the type spatial column intersects |
| 342 |
* value. |
| 343 |
* |
| 344 |
* @return true if this is a spatial intersects condition |
| 345 |
*/ |
| 346 |
public boolean isSpatialIntersects() { |
| 347 |
switch (compareType) { |
| 348 |
case Comparison.SPATIAL_INTERSECTS: |
| 349 |
return true; |
| 350 |
default: |
| 351 |
return false; |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
public int getCompareType() { |
| 356 |
return compareType; |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* Get the referenced column. |
| 361 |
* |
| 362 |
* @return the column |
| 363 |
*/ |
| 364 |
public Column getColumn() { |
| 365 |
return column; |
| 366 |
} |
| 367 |
|
| 368 |
/** |
| 369 |
* Get expression. |
| 370 |
* |
| 371 |
* @return Expression. |
| 372 |
*/ |
| 373 |
public Expression getExpression() { |
| 374 |
return expression; |
| 375 |
} |
| 376 |
|
| 377 |
/** |
| 378 |
* Get expression list. |
| 379 |
* |
| 380 |
* @return Expression list. |
| 381 |
*/ |
| 382 |
public List<Expression> getExpressionList() { |
| 383 |
return expressionList; |
| 384 |
} |
| 385 |
|
| 386 |
/** |
| 387 |
* Get expression query. |
| 388 |
* |
| 389 |
* @return Expression query. |
| 390 |
*/ |
| 391 |
public Query getExpressionQuery() { |
| 392 |
return expressionQuery; |
| 393 |
} |
| 394 |
|
| 395 |
/** |
| 396 |
* Check if the expression can be evaluated. |
| 397 |
* |
| 398 |
* @return true if it can be evaluated |
| 399 |
*/ |
| 400 |
public boolean isEvaluatable() { |
| 401 |
if (expression != null) { |
| 402 |
return expression |
| 403 |
.isEverything(ExpressionVisitor.EVALUATABLE_VISITOR); |
| 404 |
} |
| 405 |
if (expressionList != null) { |
| 406 |
for (Expression e : expressionList) { |
| 407 |
if (!e.isEverything(ExpressionVisitor.EVALUATABLE_VISITOR)) { |
| 408 |
return false; |
| 409 |
} |
| 410 |
} |
| 411 |
return true; |
| 412 |
} |
| 413 |
return expressionQuery |
| 414 |
.isEverything(ExpressionVisitor.EVALUATABLE_VISITOR); |
| 415 |
} |
| 416 |
|
| 417 |
@Override |
| 418 |
public String toString() { |
| 419 |
StringBuilder builder = new StringBuilder("column=").append(column).append(", compareType="); |
| 420 |
return compareTypeToString(builder, compareType) |
| 421 |
.append(", expression=").append(expression) |
| 422 |
.append(", expressionList=").append(expressionList) |
| 423 |
.append(", expressionQuery=").append(expressionQuery).toString(); |
| 424 |
} |
| 425 |
|
| 426 |
private static StringBuilder compareTypeToString(StringBuilder builder, int i) { |
| 427 |
boolean f = false; |
| 428 |
if ((i & EQUALITY) == EQUALITY) { |
| 429 |
f = true; |
| 430 |
builder.append("EQUALITY"); |
| 431 |
} |
| 432 |
if ((i & START) == START) { |
| 433 |
if (f) { |
| 434 |
builder.append(", "); |
| 435 |
} |
| 436 |
f = true; |
| 437 |
builder.append("START"); |
| 438 |
} |
| 439 |
if ((i & END) == END) { |
| 440 |
if (f) { |
| 441 |
builder.append(", "); |
| 442 |
} |
| 443 |
f = true; |
| 444 |
builder.append("END"); |
| 445 |
} |
| 446 |
if ((i & ALWAYS_FALSE) == ALWAYS_FALSE) { |
| 447 |
if (f) { |
| 448 |
builder.append(", "); |
| 449 |
} |
| 450 |
f = true; |
| 451 |
builder.append("ALWAYS_FALSE"); |
| 452 |
} |
| 453 |
if ((i & SPATIAL_INTERSECTS) == SPATIAL_INTERSECTS) { |
| 454 |
if (f) { |
| 455 |
builder.append(", "); |
| 456 |
} |
| 457 |
builder.append("SPATIAL_INTERSECTS"); |
| 458 |
} |
| 459 |
return builder; |
| 460 |
} |
| 461 |
|
| 462 |
} |