| 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.AbstractList; |
| 9 |
import java.util.Arrays; |
| 10 |
|
| 11 |
import org.h2.engine.SessionLocal; |
| 12 |
import org.h2.expression.Expression; |
| 13 |
import org.h2.expression.ExpressionColumn; |
| 14 |
import org.h2.expression.ExpressionVisitor; |
| 15 |
import org.h2.expression.Parameter; |
| 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.ValueArray; |
| 23 |
import org.h2.value.ValueBoolean; |
| 24 |
import org.h2.value.ValueNull; |
| 25 |
|
| 26 |
/** |
| 27 |
* Quantified comparison predicate with array. |
| 28 |
*/ |
| 29 |
public class ConditionInArray extends Condition { |
| 30 |
|
| 31 |
private static final class ParameterList extends AbstractList<Expression> { |
| 32 |
private final Parameter parameter; |
| 33 |
|
| 34 |
ParameterList(Parameter parameter) { |
| 35 |
this.parameter = parameter; |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
public Expression get(int index) { |
| 40 |
Value value = parameter.getParamValue(); |
| 41 |
if (value instanceof ValueArray) { |
| 42 |
return ValueExpression.get(((ValueArray) value).getList()[index]); |
| 43 |
} |
| 44 |
if (index != 0) { |
| 45 |
throw new IndexOutOfBoundsException(); |
| 46 |
} |
| 47 |
return ValueExpression.get(value); |
| 48 |
} |
| 49 |
|
| 50 |
@Override |
| 51 |
public int size() { |
| 52 |
if (!parameter.isValueSet()) { |
| 53 |
return 0; |
| 54 |
} |
| 55 |
Value value = parameter.getParamValue(); |
| 56 |
if (value instanceof ValueArray) { |
| 57 |
return ((ValueArray) value).getList().length; |
| 58 |
} |
| 59 |
return 1; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
private Expression left; |
| 64 |
private final boolean whenOperand; |
| 65 |
private Expression right; |
| 66 |
private final boolean all; |
| 67 |
private final int compareType; |
| 68 |
|
| 69 |
public ConditionInArray(Expression left, boolean whenOperand, Expression right, boolean all, int compareType) { |
| 70 |
this.left = left; |
| 71 |
this.whenOperand = whenOperand; |
| 72 |
this.right = right; |
| 73 |
this.all = all; |
| 74 |
this.compareType = compareType; |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public Value getValue(SessionLocal session) { |
| 79 |
return getValue(session, left.getValue(session)); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public boolean getWhenValue(SessionLocal session, Value left) { |
| 84 |
if (!whenOperand) { |
| 85 |
return super.getWhenValue(session, left); |
| 86 |
} |
| 87 |
return getValue(session, left).isTrue(); |
| 88 |
} |
| 89 |
|
| 90 |
private Value getValue(SessionLocal session, Value left) { |
| 91 |
Value r = right.getValue(session); |
| 92 |
if (r == ValueNull.INSTANCE) { |
| 93 |
return ValueNull.INSTANCE; |
| 94 |
} |
| 95 |
Value[] array = r.convertToAnyArray(session).getList(); |
| 96 |
if (array.length == 0) { |
| 97 |
return ValueBoolean.get(all); |
| 98 |
} |
| 99 |
if ((compareType & ~1) == Comparison.EQUAL_NULL_SAFE) { |
| 100 |
return getNullSafeValueSlow(session, array, left); |
| 101 |
} |
| 102 |
if (left.containsNull()) { |
| 103 |
return ValueNull.INSTANCE; |
| 104 |
} |
| 105 |
return getValueSlow(session, array, left); |
| 106 |
} |
| 107 |
|
| 108 |
private Value getValueSlow(SessionLocal session, Value[] array, Value l) { |
| 109 |
// this only returns the correct result if the array has at least one |
| 110 |
// element, and if l is not null |
| 111 |
boolean hasNull = false; |
| 112 |
ValueBoolean searched = ValueBoolean.get(!all); |
| 113 |
for (Value v : array) { |
| 114 |
Value cmp = Comparison.compare(session, l, v, compareType); |
| 115 |
if (cmp == ValueNull.INSTANCE) { |
| 116 |
hasNull = true; |
| 117 |
} else if (cmp == searched) { |
| 118 |
return ValueBoolean.get(!all); |
| 119 |
} |
| 120 |
} |
| 121 |
if (hasNull) { |
| 122 |
return ValueNull.INSTANCE; |
| 123 |
} |
| 124 |
return ValueBoolean.get(all); |
| 125 |
} |
| 126 |
|
| 127 |
private Value getNullSafeValueSlow(SessionLocal session, Value[] array, Value l) { |
| 128 |
boolean searched = all == (compareType == Comparison.NOT_EQUAL_NULL_SAFE); |
| 129 |
for (Value v : array) { |
| 130 |
if (session.areEqual(l, v) == searched) { |
| 131 |
return ValueBoolean.get(!all); |
| 132 |
} |
| 133 |
} |
| 134 |
return ValueBoolean.get(all); |
| 135 |
} |
| 136 |
|
| 137 |
@Override |
| 138 |
public boolean isWhenConditionOperand() { |
| 139 |
return whenOperand; |
| 140 |
} |
| 141 |
|
| 142 |
@Override |
| 143 |
public Expression getNotIfPossible(SessionLocal session) { |
| 144 |
if (whenOperand) { |
| 145 |
return null; |
| 146 |
} |
| 147 |
return new ConditionInArray(left, false, right, !all, Comparison.getNotCompareType(compareType)); |
| 148 |
} |
| 149 |
|
| 150 |
@Override |
| 151 |
public void mapColumns(ColumnResolver resolver, int level, int state) { |
| 152 |
left.mapColumns(resolver, level, state); |
| 153 |
right.mapColumns(resolver, level, state); |
| 154 |
} |
| 155 |
|
| 156 |
@Override |
| 157 |
public Expression optimize(SessionLocal session) { |
| 158 |
right = right.optimize(session); |
| 159 |
left = left.optimize(session); |
| 160 |
if (!whenOperand && left.isConstant() && right.isConstant()) { |
| 161 |
return ValueExpression.getBoolean(getValue(session)); |
| 162 |
} |
| 163 |
return this; |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public void createIndexConditions(SessionLocal session, TableFilter filter) { |
| 168 |
if (whenOperand || all || compareType != Comparison.EQUAL || !(left instanceof ExpressionColumn)) { |
| 169 |
return; |
| 170 |
} |
| 171 |
ExpressionColumn l = (ExpressionColumn) left; |
| 172 |
if (filter != l.getTableFilter()) { |
| 173 |
return; |
| 174 |
} |
| 175 |
if (right instanceof Parameter) { |
| 176 |
filter.addIndexCondition(IndexCondition.getInList(l, new ParameterList((Parameter) right))); |
| 177 |
} else if (right.isConstant()) { |
| 178 |
Value r = right.getValue(null); |
| 179 |
if (r instanceof ValueArray) { |
| 180 |
Value[] values = ((ValueArray) r).getList(); |
| 181 |
int count = values.length; |
| 182 |
if (count == 0) { |
| 183 |
filter.addIndexCondition(IndexCondition.get(Comparison.FALSE, l, ValueExpression.FALSE)); |
| 184 |
} else { |
| 185 |
TypeInfo colType = l.getType(), type = colType; |
| 186 |
for (int i = 0; i < count; i++) { |
| 187 |
type = TypeInfo.getHigherType(type, values[i].getType()); |
| 188 |
} |
| 189 |
if (TypeInfo.haveSameOrdering(colType, type)) { |
| 190 |
Expression[] valueList = new Expression[count]; |
| 191 |
for (int i = 0; i < count; i++) { |
| 192 |
valueList[i] = ValueExpression.get(values[i]); |
| 193 |
} |
| 194 |
filter.addIndexCondition(IndexCondition.getInList(l, Arrays.asList(valueList))); |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
} else { |
| 199 |
ExpressionVisitor visitor = ExpressionVisitor.getNotFromResolverVisitor(filter); |
| 200 |
if (right.isEverything(visitor)) { |
| 201 |
TypeInfo arrayType = right.getType(); |
| 202 |
if (arrayType.getValueType() == Value.ARRAY) { |
| 203 |
TypeInfo colType = l.getType(); |
| 204 |
if (TypeInfo.haveSameOrdering(colType, |
| 205 |
TypeInfo.getHigherType(colType, (TypeInfo) arrayType.getExtTypeInfo()))) { |
| 206 |
filter.addIndexCondition(IndexCondition.getInArray(l, right)); |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
@Override |
| 214 |
public void setEvaluatable(TableFilter tableFilter, boolean value) { |
| 215 |
left.setEvaluatable(tableFilter, value); |
| 216 |
right.setEvaluatable(tableFilter, value); |
| 217 |
} |
| 218 |
|
| 219 |
@Override |
| 220 |
public boolean needParentheses() { |
| 221 |
return true; |
| 222 |
} |
| 223 |
|
| 224 |
@Override |
| 225 |
public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) { |
| 226 |
return getWhenSQL(left.getSQL(builder, sqlFlags, AUTO_PARENTHESES), sqlFlags); |
| 227 |
} |
| 228 |
|
| 229 |
@Override |
| 230 |
public StringBuilder getWhenSQL(StringBuilder builder, int sqlFlags) { |
| 231 |
return right.getSQL( |
| 232 |
builder.append(' ').append(Comparison.COMPARE_TYPES[compareType]).append(all ? " ALL(" : " ANY("), |
| 233 |
sqlFlags).append(')'); |
| 234 |
} |
| 235 |
|
| 236 |
@Override |
| 237 |
public void updateAggregate(SessionLocal session, int stage) { |
| 238 |
left.updateAggregate(session, stage); |
| 239 |
right.updateAggregate(session, stage); |
| 240 |
} |
| 241 |
|
| 242 |
@Override |
| 243 |
public boolean isEverything(ExpressionVisitor visitor) { |
| 244 |
return left.isEverything(visitor) && right.isEverything(visitor); |
| 245 |
} |
| 246 |
|
| 247 |
@Override |
| 248 |
public int getCost() { |
| 249 |
return left.getCost() + right.getCost() + 10; |
| 250 |
} |
| 251 |
|
| 252 |
} |