| 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.condition; |
| 7 |
|
| 8 |
import java.util.ArrayList; |
| 9 |
import java.util.TreeSet; |
| 10 |
|
| 11 |
import org.h2.engine.SessionLocal; |
| 12 |
import org.h2.expression.Expression; |
| 13 |
import org.h2.expression.ExpressionColumn; |
| 14 |
import org.h2.expression.ExpressionList; |
| 15 |
import org.h2.expression.ExpressionVisitor; |
| 16 |
import org.h2.expression.ValueExpression; |
| 17 |
import org.h2.index.IndexCondition; |
| 18 |
import org.h2.table.ColumnResolver; |
| 19 |
import org.h2.table.TableFilter; |
| 20 |
import org.h2.value.TypeInfo; |
| 21 |
import org.h2.value.Value; |
| 22 |
import org.h2.value.ValueBoolean; |
| 23 |
import org.h2.value.ValueNull; |
| 24 |
import org.h2.value.ValueRow; |
| 25 |
|
| 26 |
/** |
| 27 |
* Used for optimised IN(...) queries where the contents of the IN list are all |
| 28 |
* constant and of the same type. |
| 29 |
*/ |
| 30 |
public final class ConditionInConstantSet extends Condition { |
| 31 |
|
| 32 |
private Expression left; |
| 33 |
private final boolean not; |
| 34 |
private final boolean whenOperand; |
| 35 |
private final ArrayList<Expression> valueList; |
| 36 |
// HashSet cannot be used here, because we need to compare values of |
| 37 |
// different type or scale properly. |
| 38 |
private final TreeSet<Value> valueSet; |
| 39 |
private boolean hasNull; |
| 40 |
private final TypeInfo type; |
| 41 |
|
| 42 |
/** |
| 43 |
* Create a new IN(..) condition. |
| 44 |
* |
| 45 |
* @param session the session |
| 46 |
* @param left |
| 47 |
* the expression before IN. Cannot have {@link Value#UNKNOWN} |
| 48 |
* data type and {@link Value#ENUM} type is also supported only |
| 49 |
* for {@link ExpressionColumn}. |
| 50 |
* @param not whether the result should be negated |
| 51 |
* @param whenOperand whether this is a when operand |
| 52 |
* @param valueList |
| 53 |
* the value list (at least two elements); all values must be |
| 54 |
* comparable with left value |
| 55 |
*/ |
| 56 |
ConditionInConstantSet(SessionLocal session, Expression left, boolean not, boolean whenOperand, |
| 57 |
ArrayList<Expression> valueList) { |
| 58 |
this.left = left; |
| 59 |
this.not = not; |
| 60 |
this.whenOperand = whenOperand; |
| 61 |
this.valueList = valueList; |
| 62 |
this.valueSet = new TreeSet<>(session.getDatabase().getCompareMode()); |
| 63 |
TypeInfo type = left.getType(); |
| 64 |
for (Expression expression : valueList) { |
| 65 |
type = TypeInfo.getHigherType(type, expression.getType()); |
| 66 |
} |
| 67 |
this.type = type; |
| 68 |
for (Expression expression : valueList) { |
| 69 |
add(expression.getValue(session), session); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
private void add(Value v, SessionLocal session) { |
| 74 |
if ((v = v.convertTo(type, session)).containsNull()) { |
| 75 |
hasNull = true; |
| 76 |
} else { |
| 77 |
valueSet.add(v); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public Value getValue(SessionLocal session) { |
| 83 |
return getValue(left.getValue(session), session); |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public boolean getWhenValue(SessionLocal session, Value left) { |
| 88 |
if (!whenOperand) { |
| 89 |
return super.getWhenValue(session, left); |
| 90 |
} |
| 91 |
return getValue(left, session).isTrue(); |
| 92 |
} |
| 93 |
|
| 94 |
private Value getValue(Value left, SessionLocal session) { |
| 95 |
if ((left = left.convertTo(type, session)).containsNull()) { |
| 96 |
return ValueNull.INSTANCE; |
| 97 |
} |
| 98 |
boolean result = valueSet.contains(left); |
| 99 |
if (!result && hasNull) { |
| 100 |
return ValueNull.INSTANCE; |
| 101 |
} |
| 102 |
return ValueBoolean.get(not ^ result); |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public boolean isWhenConditionOperand() { |
| 107 |
return whenOperand; |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public void mapColumns(ColumnResolver resolver, int level, int state) { |
| 112 |
left.mapColumns(resolver, level, state); |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public Expression optimize(SessionLocal session) { |
| 117 |
left = left.optimize(session); |
| 118 |
return this; |
| 119 |
} |
| 120 |
|
| 121 |
@Override |
| 122 |
public Expression getNotIfPossible(SessionLocal session) { |
| 123 |
if (whenOperand) { |
| 124 |
return null; |
| 125 |
} |
| 126 |
return new ConditionInConstantSet(session, left, !not, false, valueList); |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public void createIndexConditions(SessionLocal session, TableFilter filter) { |
| 131 |
if (not || whenOperand || !session.getDatabase().getSettings().optimizeInList) { |
| 132 |
return; |
| 133 |
} |
| 134 |
if (left instanceof ExpressionColumn) { |
| 135 |
ExpressionColumn l = (ExpressionColumn) left; |
| 136 |
if (filter == l.getTableFilter()) { |
| 137 |
createIndexConditions(filter, l, valueList, type); |
| 138 |
} |
| 139 |
} else if (left instanceof ExpressionList) { |
| 140 |
ExpressionList list = (ExpressionList) left; |
| 141 |
if (!list.isArray()) { |
| 142 |
createIndexConditions(filter, list); |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
private void createIndexConditions(TableFilter filter, ExpressionList list) { |
| 148 |
int c = list.getSubexpressionCount(); |
| 149 |
for (int i = 0; i < c; i++) { |
| 150 |
Expression e = list.getSubexpression(i); |
| 151 |
if (e instanceof ExpressionColumn) { |
| 152 |
ExpressionColumn l = (ExpressionColumn) e; |
| 153 |
if (filter == l.getTableFilter()) { |
| 154 |
ArrayList<Expression> subList = new ArrayList<>(valueList.size()); |
| 155 |
for (Expression row : valueList) { |
| 156 |
if (row instanceof ExpressionList) { |
| 157 |
ExpressionList r = (ExpressionList) row; |
| 158 |
if (r.isArray() || r.getSubexpressionCount() != c) { |
| 159 |
return; |
| 160 |
} |
| 161 |
subList.add(r.getSubexpression(i)); |
| 162 |
} else if (row instanceof ValueExpression) { |
| 163 |
Value v = row.getValue(null); |
| 164 |
if (v.getValueType() != Value.ROW) { |
| 165 |
return; |
| 166 |
} |
| 167 |
Value[] values = ((ValueRow) v).getList(); |
| 168 |
if (c != values.length) { |
| 169 |
return; |
| 170 |
} |
| 171 |
subList.add(ValueExpression.get(values[i])); |
| 172 |
} else { |
| 173 |
return; |
| 174 |
} |
| 175 |
} |
| 176 |
TypeInfo type = l.getType(); |
| 177 |
for (Expression expression : subList) { |
| 178 |
type = TypeInfo.getHigherType(type, expression.getType()); |
| 179 |
} |
| 180 |
createIndexConditions(filter, l, subList, type); |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
private static void createIndexConditions(TableFilter filter, ExpressionColumn l, ArrayList<Expression> valueList, |
| 187 |
TypeInfo type) { |
| 188 |
TypeInfo colType = l.getType(); |
| 189 |
if (TypeInfo.haveSameOrdering(colType, TypeInfo.getHigherType(colType, type))) { |
| 190 |
filter.addIndexCondition(IndexCondition.getInList(l, valueList)); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
@Override |
| 195 |
public void setEvaluatable(TableFilter tableFilter, boolean b) { |
| 196 |
left.setEvaluatable(tableFilter, b); |
| 197 |
} |
| 198 |
|
| 199 |
@Override |
| 200 |
public boolean needParentheses() { |
| 201 |
return true; |
| 202 |
} |
| 203 |
|
| 204 |
@Override |
| 205 |
public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) { |
| 206 |
return getWhenSQL(left.getSQL(builder, sqlFlags, AUTO_PARENTHESES), sqlFlags); |
| 207 |
} |
| 208 |
|
| 209 |
@Override |
| 210 |
public StringBuilder getWhenSQL(StringBuilder builder, int sqlFlags) { |
| 211 |
if (not) { |
| 212 |
builder.append(" NOT"); |
| 213 |
} |
| 214 |
return writeExpressions(builder.append(" IN("), valueList, sqlFlags).append(')'); |
| 215 |
} |
| 216 |
|
| 217 |
@Override |
| 218 |
public void updateAggregate(SessionLocal session, int stage) { |
| 219 |
left.updateAggregate(session, stage); |
| 220 |
} |
| 221 |
|
| 222 |
@Override |
| 223 |
public boolean isEverything(ExpressionVisitor visitor) { |
| 224 |
return left.isEverything(visitor); |
| 225 |
} |
| 226 |
|
| 227 |
@Override |
| 228 |
public int getCost() { |
| 229 |
return left.getCost(); |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Add an additional element if possible. Example: given two conditions |
| 234 |
* A IN(1, 2) OR A=3, the constant 3 is added: A IN(1, 2, 3). |
| 235 |
* |
| 236 |
* @param session the session |
| 237 |
* @param other the second condition |
| 238 |
* @return null if the condition was not added, or the new condition |
| 239 |
*/ |
| 240 |
Expression getAdditional(SessionLocal session, Comparison other) { |
| 241 |
if (!not && !whenOperand && left.isEverything(ExpressionVisitor.DETERMINISTIC_VISITOR)) { |
| 242 |
Expression add = other.getIfEquals(left); |
| 243 |
if (add != null) { |
| 244 |
if (add.isConstant()) { |
| 245 |
ArrayList<Expression> list = new ArrayList<>(valueList.size() + 1); |
| 246 |
list.addAll(valueList); |
| 247 |
list.add(add); |
| 248 |
return new ConditionInConstantSet(session, left, false, false, list); |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
return null; |
| 253 |
} |
| 254 |
|
| 255 |
@Override |
| 256 |
public int getSubexpressionCount() { |
| 257 |
return 1 + valueList.size(); |
| 258 |
} |
| 259 |
|
| 260 |
@Override |
| 261 |
public Expression getSubexpression(int index) { |
| 262 |
if (index == 0) { |
| 263 |
return left; |
| 264 |
} else if (index > 0 && index <= valueList.size()) { |
| 265 |
return valueList.get(index - 1); |
| 266 |
} |
| 267 |
throw new IndexOutOfBoundsException(); |
| 268 |
} |
| 269 |
|
| 270 |
} |