| 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 org.h2.engine.SessionLocal; |
| 10 |
import org.h2.expression.Expression; |
| 11 |
import org.h2.expression.ExpressionColumn; |
| 12 |
import org.h2.expression.ExpressionList; |
| 13 |
import org.h2.expression.ExpressionVisitor; |
| 14 |
import org.h2.expression.Parameter; |
| 15 |
import org.h2.expression.TypedValueExpression; |
| 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 |
* An 'in' condition with a list of values, as in WHERE NAME IN(...) |
| 28 |
*/ |
| 29 |
public final class ConditionIn extends Condition { |
| 30 |
|
| 31 |
private Expression left; |
| 32 |
private final boolean not; |
| 33 |
private final boolean whenOperand; |
| 34 |
private final ArrayList<Expression> valueList; |
| 35 |
|
| 36 |
/** |
| 37 |
* Create a new IN(..) condition. |
| 38 |
* |
| 39 |
* @param left the expression before IN |
| 40 |
* @param not whether the result should be negated |
| 41 |
* @param whenOperand whether this is a when operand |
| 42 |
* @param values the value list (at least one element) |
| 43 |
*/ |
| 44 |
public ConditionIn(Expression left, boolean not, boolean whenOperand, ArrayList<Expression> values) { |
| 45 |
this.left = left; |
| 46 |
this.not = not; |
| 47 |
this.whenOperand = whenOperand; |
| 48 |
this.valueList = values; |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
public Value getValue(SessionLocal session) { |
| 53 |
return getValue(session, left.getValue(session)); |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
public boolean getWhenValue(SessionLocal session, Value left) { |
| 58 |
if (!whenOperand) { |
| 59 |
return super.getWhenValue(session, left); |
| 60 |
} |
| 61 |
return getValue(session, left).isTrue(); |
| 62 |
} |
| 63 |
|
| 64 |
private Value getValue(SessionLocal session, Value left) { |
| 65 |
if (left.containsNull()) { |
| 66 |
return ValueNull.INSTANCE; |
| 67 |
} |
| 68 |
boolean hasNull = false; |
| 69 |
for (Expression e : valueList) { |
| 70 |
Value r = e.getValue(session); |
| 71 |
Value cmp = Comparison.compare(session, left, r, Comparison.EQUAL); |
| 72 |
if (cmp == ValueNull.INSTANCE) { |
| 73 |
hasNull = true; |
| 74 |
} else if (cmp == ValueBoolean.TRUE) { |
| 75 |
return ValueBoolean.get(!not); |
| 76 |
} |
| 77 |
} |
| 78 |
if (hasNull) { |
| 79 |
return ValueNull.INSTANCE; |
| 80 |
} |
| 81 |
return ValueBoolean.get(not); |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public boolean isWhenConditionOperand() { |
| 86 |
return whenOperand; |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public void mapColumns(ColumnResolver resolver, int level, int state) { |
| 91 |
left.mapColumns(resolver, level, state); |
| 92 |
for (Expression e : valueList) { |
| 93 |
e.mapColumns(resolver, level, state); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public Expression optimize(SessionLocal session) { |
| 99 |
left = left.optimize(session); |
| 100 |
boolean constant = !whenOperand && left.isConstant(); |
| 101 |
if (constant && left.isNullConstant()) { |
| 102 |
return TypedValueExpression.UNKNOWN; |
| 103 |
} |
| 104 |
boolean allValuesConstant = true; |
| 105 |
boolean allValuesNull = true; |
| 106 |
TypeInfo leftType = left.getType(); |
| 107 |
for (int i = 0, l = valueList.size(); i < l; i++) { |
| 108 |
Expression e = valueList.get(i); |
| 109 |
e = e.optimize(session); |
| 110 |
TypeInfo.checkComparable(leftType, e.getType()); |
| 111 |
if (e.isConstant() && !e.getValue(session).containsNull()) { |
| 112 |
allValuesNull = false; |
| 113 |
} |
| 114 |
if (allValuesConstant && !e.isConstant()) { |
| 115 |
allValuesConstant = false; |
| 116 |
} |
| 117 |
if (left instanceof ExpressionColumn && e instanceof Parameter) { |
| 118 |
((Parameter) e).setColumn(((ExpressionColumn) left).getColumn()); |
| 119 |
} |
| 120 |
valueList.set(i, e); |
| 121 |
} |
| 122 |
return optimize2(session, constant, allValuesConstant, allValuesNull, valueList); |
| 123 |
} |
| 124 |
|
| 125 |
private Expression optimize2(SessionLocal session, boolean constant, boolean allValuesConstant, |
| 126 |
boolean allValuesNull, ArrayList<Expression> values) { |
| 127 |
if (constant && allValuesConstant) { |
| 128 |
return ValueExpression.getBoolean(getValue(session)); |
| 129 |
} |
| 130 |
if (values.size() == 1) { |
| 131 |
return new Comparison(not ? Comparison.NOT_EQUAL : Comparison.EQUAL, left, values.get(0), whenOperand) |
| 132 |
.optimize(session); |
| 133 |
} |
| 134 |
if (allValuesConstant && !allValuesNull) { |
| 135 |
int leftType = left.getType().getValueType(); |
| 136 |
if (leftType == Value.UNKNOWN) { |
| 137 |
return this; |
| 138 |
} |
| 139 |
if (leftType == Value.ENUM && !(left instanceof ExpressionColumn)) { |
| 140 |
return this; |
| 141 |
} |
| 142 |
return new ConditionInConstantSet(session, left, not, whenOperand, values).optimize(session); |
| 143 |
} |
| 144 |
return this; |
| 145 |
} |
| 146 |
|
| 147 |
@Override |
| 148 |
public Expression getNotIfPossible(SessionLocal session) { |
| 149 |
if (whenOperand) { |
| 150 |
return null; |
| 151 |
} |
| 152 |
return new ConditionIn(left, !not, false, valueList); |
| 153 |
} |
| 154 |
|
| 155 |
@Override |
| 156 |
public void createIndexConditions(SessionLocal session, TableFilter filter) { |
| 157 |
if (not || whenOperand || !session.getDatabase().getSettings().optimizeInList) { |
| 158 |
return; |
| 159 |
} |
| 160 |
if (left instanceof ExpressionColumn) { |
| 161 |
ExpressionColumn l = (ExpressionColumn) left; |
| 162 |
if (filter == l.getTableFilter()) { |
| 163 |
createIndexConditions(filter, l, valueList); |
| 164 |
} |
| 165 |
} else if (left instanceof ExpressionList) { |
| 166 |
ExpressionList list = (ExpressionList) left; |
| 167 |
if (!list.isArray()) { |
| 168 |
createIndexConditions(filter, list); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
private void createIndexConditions(TableFilter filter, ExpressionList list) { |
| 174 |
int c = list.getSubexpressionCount(); |
| 175 |
for (int i = 0; i < c; i++) { |
| 176 |
Expression e = list.getSubexpression(i); |
| 177 |
if (e instanceof ExpressionColumn) { |
| 178 |
ExpressionColumn l = (ExpressionColumn) e; |
| 179 |
if (filter == l.getTableFilter()) { |
| 180 |
ArrayList<Expression> subList = new ArrayList<>(valueList.size()); |
| 181 |
for (Expression row : valueList) { |
| 182 |
if (row instanceof ExpressionList) { |
| 183 |
ExpressionList r = (ExpressionList) row; |
| 184 |
if (r.isArray() || r.getSubexpressionCount() != c) { |
| 185 |
return; |
| 186 |
} |
| 187 |
subList.add(r.getSubexpression(i)); |
| 188 |
} else if (row instanceof ValueExpression) { |
| 189 |
Value v = row.getValue(null); |
| 190 |
if (v.getValueType() != Value.ROW) { |
| 191 |
return; |
| 192 |
} |
| 193 |
Value[] values = ((ValueRow) v).getList(); |
| 194 |
if (c != values.length) { |
| 195 |
return; |
| 196 |
} |
| 197 |
subList.add(ValueExpression.get(values[i])); |
| 198 |
} else { |
| 199 |
return; |
| 200 |
} |
| 201 |
} |
| 202 |
createIndexConditions(filter, l, subList); |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
private static void createIndexConditions(TableFilter filter, ExpressionColumn l, // |
| 209 |
ArrayList<Expression> valueList) { |
| 210 |
ExpressionVisitor visitor = ExpressionVisitor.getNotFromResolverVisitor(filter); |
| 211 |
TypeInfo colType = l.getType(); |
| 212 |
for (Expression e : valueList) { |
| 213 |
if (!e.isEverything(visitor) |
| 214 |
|| !TypeInfo.haveSameOrdering(colType, TypeInfo.getHigherType(colType, e.getType()))) { |
| 215 |
return; |
| 216 |
} |
| 217 |
} |
| 218 |
filter.addIndexCondition(IndexCondition.getInList(l, valueList)); |
| 219 |
} |
| 220 |
|
| 221 |
@Override |
| 222 |
public void setEvaluatable(TableFilter tableFilter, boolean b) { |
| 223 |
left.setEvaluatable(tableFilter, b); |
| 224 |
for (Expression e : valueList) { |
| 225 |
e.setEvaluatable(tableFilter, b); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
@Override |
| 230 |
public boolean needParentheses() { |
| 231 |
return true; |
| 232 |
} |
| 233 |
|
| 234 |
@Override |
| 235 |
public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) { |
| 236 |
return getWhenSQL(left.getSQL(builder, sqlFlags, AUTO_PARENTHESES), sqlFlags); |
| 237 |
} |
| 238 |
|
| 239 |
@Override |
| 240 |
public StringBuilder getWhenSQL(StringBuilder builder, int sqlFlags) { |
| 241 |
if (not) { |
| 242 |
builder.append(" NOT"); |
| 243 |
} |
| 244 |
return writeExpressions(builder.append(" IN("), valueList, sqlFlags).append(')'); |
| 245 |
} |
| 246 |
|
| 247 |
@Override |
| 248 |
public void updateAggregate(SessionLocal session, int stage) { |
| 249 |
left.updateAggregate(session, stage); |
| 250 |
for (Expression e : valueList) { |
| 251 |
e.updateAggregate(session, stage); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
@Override |
| 256 |
public boolean isEverything(ExpressionVisitor visitor) { |
| 257 |
if (!left.isEverything(visitor)) { |
| 258 |
return false; |
| 259 |
} |
| 260 |
return areAllValues(visitor); |
| 261 |
} |
| 262 |
|
| 263 |
private boolean areAllValues(ExpressionVisitor visitor) { |
| 264 |
for (Expression e : valueList) { |
| 265 |
if (!e.isEverything(visitor)) { |
| 266 |
return false; |
| 267 |
} |
| 268 |
} |
| 269 |
return true; |
| 270 |
} |
| 271 |
|
| 272 |
@Override |
| 273 |
public int getCost() { |
| 274 |
int cost = left.getCost(); |
| 275 |
for (Expression e : valueList) { |
| 276 |
cost += e.getCost(); |
| 277 |
} |
| 278 |
return cost; |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Add an additional element if possible. Example: given two conditions |
| 283 |
* A IN(1, 2) OR A=3, the constant 3 is added: A IN(1, 2, 3). |
| 284 |
* |
| 285 |
* @param other the second condition |
| 286 |
* @return null if the condition was not added, or the new condition |
| 287 |
*/ |
| 288 |
Expression getAdditional(Comparison other) { |
| 289 |
if (!not && !whenOperand && left.isEverything(ExpressionVisitor.DETERMINISTIC_VISITOR)) { |
| 290 |
Expression add = other.getIfEquals(left); |
| 291 |
if (add != null) { |
| 292 |
ArrayList<Expression> list = new ArrayList<>(valueList.size() + 1); |
| 293 |
list.addAll(valueList); |
| 294 |
list.add(add); |
| 295 |
return new ConditionIn(left, false, false, list); |
| 296 |
} |
| 297 |
} |
| 298 |
return null; |
| 299 |
} |
| 300 |
|
| 301 |
@Override |
| 302 |
public int getSubexpressionCount() { |
| 303 |
return 1 + valueList.size(); |
| 304 |
} |
| 305 |
|
| 306 |
@Override |
| 307 |
public Expression getSubexpression(int index) { |
| 308 |
if (index == 0) { |
| 309 |
return left; |
| 310 |
} else if (index > 0 && index <= valueList.size()) { |
| 311 |
return valueList.get(index - 1); |
| 312 |
} |
| 313 |
throw new IndexOutOfBoundsException(); |
| 314 |
} |
| 315 |
|
| 316 |
} |