| 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; |
| 7 |
|
| 8 |
import org.h2.engine.SessionLocal; |
| 9 |
import org.h2.table.ColumnResolver; |
| 10 |
import org.h2.table.TableFilter; |
| 11 |
import org.h2.value.TypeInfo; |
| 12 |
import org.h2.value.Value; |
| 13 |
import org.h2.value.ValueNull; |
| 14 |
|
| 15 |
/** |
| 16 |
* A simple case. |
| 17 |
*/ |
| 18 |
public final class SimpleCase extends Expression { |
| 19 |
|
| 20 |
public static final class SimpleWhen { |
| 21 |
|
| 22 |
Expression[] operands; |
| 23 |
|
| 24 |
Expression result; |
| 25 |
|
| 26 |
SimpleWhen next; |
| 27 |
|
| 28 |
public SimpleWhen(Expression operand, Expression result) { |
| 29 |
this(new Expression[] { operand }, result); |
| 30 |
} |
| 31 |
|
| 32 |
public SimpleWhen(Expression[] operands, Expression result) { |
| 33 |
this.operands = operands; |
| 34 |
this.result = result; |
| 35 |
} |
| 36 |
|
| 37 |
public void setWhen(SimpleWhen next) { |
| 38 |
this.next = next; |
| 39 |
} |
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
private Expression operand; |
| 44 |
|
| 45 |
private SimpleWhen when; |
| 46 |
|
| 47 |
private Expression elseResult; |
| 48 |
|
| 49 |
private TypeInfo type; |
| 50 |
|
| 51 |
public SimpleCase(Expression operand, SimpleWhen when, Expression elseResult) { |
| 52 |
this.operand = operand; |
| 53 |
this.when = when; |
| 54 |
this.elseResult = elseResult; |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public Value getValue(SessionLocal session) { |
| 59 |
Value v = operand.getValue(session); |
| 60 |
for (SimpleWhen when = this.when; when != null; when = when.next) { |
| 61 |
for (Expression e : when.operands) { |
| 62 |
if (e.getWhenValue(session, v)) { |
| 63 |
return when.result.getValue(session).convertTo(type, session); |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
if (elseResult != null) { |
| 68 |
return elseResult.getValue(session).convertTo(type, session); |
| 69 |
} |
| 70 |
return ValueNull.INSTANCE; |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public Expression optimize(SessionLocal session) { |
| 75 |
TypeInfo typeInfo = TypeInfo.TYPE_UNKNOWN; |
| 76 |
operand = operand.optimize(session); |
| 77 |
boolean allConst = operand.isConstant(); |
| 78 |
Value v = null; |
| 79 |
if (allConst) { |
| 80 |
v = operand.getValue(session); |
| 81 |
} |
| 82 |
TypeInfo operandType = operand.getType(); |
| 83 |
for (SimpleWhen when = this.when; when != null; when = when.next) { |
| 84 |
Expression[] operands = when.operands; |
| 85 |
for (int i = 0; i < operands.length; i++) { |
| 86 |
Expression e = operands[i].optimize(session); |
| 87 |
if (!e.isWhenConditionOperand()) { |
| 88 |
TypeInfo.checkComparable(operandType, e.getType()); |
| 89 |
} |
| 90 |
if (allConst) { |
| 91 |
if (e.isConstant()) { |
| 92 |
if (e.getWhenValue(session, v)) { |
| 93 |
return when.result.optimize(session); |
| 94 |
} |
| 95 |
} else { |
| 96 |
allConst = false; |
| 97 |
} |
| 98 |
} |
| 99 |
operands[i] = e; |
| 100 |
} |
| 101 |
when.result = when.result.optimize(session); |
| 102 |
typeInfo = combineTypes(typeInfo, when.result); |
| 103 |
} |
| 104 |
if (elseResult != null) { |
| 105 |
elseResult = elseResult.optimize(session); |
| 106 |
if (allConst) { |
| 107 |
return elseResult; |
| 108 |
} |
| 109 |
typeInfo = combineTypes(typeInfo, elseResult); |
| 110 |
} else if (allConst) { |
| 111 |
return ValueExpression.NULL; |
| 112 |
} |
| 113 |
if (typeInfo.getValueType() == Value.UNKNOWN) { |
| 114 |
typeInfo = TypeInfo.TYPE_VARCHAR; |
| 115 |
} |
| 116 |
type = typeInfo; |
| 117 |
return this; |
| 118 |
} |
| 119 |
|
| 120 |
static TypeInfo combineTypes(TypeInfo typeInfo, Expression e) { |
| 121 |
if (!e.isNullConstant()) { |
| 122 |
TypeInfo type = e.getType(); |
| 123 |
int valueType = type.getValueType(); |
| 124 |
if (valueType != Value.UNKNOWN && valueType != Value.NULL) { |
| 125 |
typeInfo = TypeInfo.getHigherType(typeInfo, type); |
| 126 |
} |
| 127 |
} |
| 128 |
return typeInfo; |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) { |
| 133 |
operand.getUnenclosedSQL(builder.append("CASE "), sqlFlags); |
| 134 |
for (SimpleWhen when = this.when; when != null; when = when.next) { |
| 135 |
builder.append(" WHEN"); |
| 136 |
Expression[] operands = when.operands; |
| 137 |
for (int i = 0, len = operands.length; i < len; i++) { |
| 138 |
if (i > 0) { |
| 139 |
builder.append(','); |
| 140 |
} |
| 141 |
operands[i].getWhenSQL(builder, sqlFlags); |
| 142 |
} |
| 143 |
when.result.getUnenclosedSQL(builder.append(" THEN "), sqlFlags); |
| 144 |
} |
| 145 |
if (elseResult != null) { |
| 146 |
elseResult.getUnenclosedSQL(builder.append(" ELSE "), sqlFlags); |
| 147 |
} |
| 148 |
return builder.append(" END"); |
| 149 |
} |
| 150 |
|
| 151 |
@Override |
| 152 |
public TypeInfo getType() { |
| 153 |
return type; |
| 154 |
} |
| 155 |
|
| 156 |
@Override |
| 157 |
public void mapColumns(ColumnResolver resolver, int level, int state) { |
| 158 |
operand.mapColumns(resolver, level, state); |
| 159 |
for (SimpleWhen when = this.when; when != null; when = when.next) { |
| 160 |
for (Expression e : when.operands) { |
| 161 |
e.mapColumns(resolver, level, state); |
| 162 |
} |
| 163 |
when.result.mapColumns(resolver, level, state); |
| 164 |
} |
| 165 |
if (elseResult != null) { |
| 166 |
elseResult.mapColumns(resolver, level, state); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
@Override |
| 171 |
public void setEvaluatable(TableFilter tableFilter, boolean value) { |
| 172 |
operand.setEvaluatable(tableFilter, value); |
| 173 |
for (SimpleWhen when = this.when; when != null; when = when.next) { |
| 174 |
for (Expression e : when.operands) { |
| 175 |
e.setEvaluatable(tableFilter, value); |
| 176 |
} |
| 177 |
when.result.setEvaluatable(tableFilter, value); |
| 178 |
} |
| 179 |
if (elseResult != null) { |
| 180 |
elseResult.setEvaluatable(tableFilter, value); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
@Override |
| 185 |
public void updateAggregate(SessionLocal session, int stage) { |
| 186 |
operand.updateAggregate(session, stage); |
| 187 |
for (SimpleWhen when = this.when; when != null; when = when.next) { |
| 188 |
for (Expression e : when.operands) { |
| 189 |
e.updateAggregate(session, stage); |
| 190 |
} |
| 191 |
when.result.updateAggregate(session, stage); |
| 192 |
} |
| 193 |
if (elseResult != null) { |
| 194 |
elseResult.updateAggregate(session, stage); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
@Override |
| 199 |
public boolean isEverything(ExpressionVisitor visitor) { |
| 200 |
if (!operand.isEverything(visitor)) { |
| 201 |
return false; |
| 202 |
} |
| 203 |
for (SimpleWhen when = this.when; when != null; when = when.next) { |
| 204 |
for (Expression e : when.operands) { |
| 205 |
if (!e.isEverything(visitor)) { |
| 206 |
return false; |
| 207 |
} |
| 208 |
} |
| 209 |
if (!when.result.isEverything(visitor)) { |
| 210 |
return false; |
| 211 |
} |
| 212 |
} |
| 213 |
if (elseResult != null && !elseResult.isEverything(visitor)) { |
| 214 |
return false; |
| 215 |
} |
| 216 |
return true; |
| 217 |
} |
| 218 |
|
| 219 |
@Override |
| 220 |
public int getCost() { |
| 221 |
int cost = 1, resultCost = 0; |
| 222 |
cost += operand.getCost(); |
| 223 |
for (SimpleWhen when = this.when; when != null; when = when.next) { |
| 224 |
for (Expression e : when.operands) { |
| 225 |
cost += e.getCost(); |
| 226 |
} |
| 227 |
resultCost = Math.max(resultCost, when.result.getCost()); |
| 228 |
} |
| 229 |
if (elseResult != null) { |
| 230 |
resultCost = Math.max(resultCost, elseResult.getCost()); |
| 231 |
} |
| 232 |
return cost + resultCost; |
| 233 |
} |
| 234 |
|
| 235 |
@Override |
| 236 |
public int getSubexpressionCount() { |
| 237 |
int count = 1; |
| 238 |
for (SimpleWhen when = this.when; when != null; when = when.next) { |
| 239 |
count += when.operands.length + 1; |
| 240 |
} |
| 241 |
if (elseResult != null) { |
| 242 |
count++; |
| 243 |
} |
| 244 |
return count; |
| 245 |
} |
| 246 |
|
| 247 |
@Override |
| 248 |
public Expression getSubexpression(int index) { |
| 249 |
if (index >= 0) { |
| 250 |
if (index == 0) { |
| 251 |
return operand; |
| 252 |
} |
| 253 |
int ptr = 1; |
| 254 |
for (SimpleWhen when = this.when; when != null; when = when.next) { |
| 255 |
Expression[] operands = when.operands; |
| 256 |
int count = operands.length; |
| 257 |
int offset = index - ptr; |
| 258 |
if (offset < count) { |
| 259 |
return operands[offset]; |
| 260 |
} |
| 261 |
ptr += count; |
| 262 |
if (index == ptr++) { |
| 263 |
return when.result; |
| 264 |
} |
| 265 |
} |
| 266 |
if (elseResult != null && index == ptr) { |
| 267 |
return elseResult; |
| 268 |
} |
| 269 |
} |
| 270 |
throw new IndexOutOfBoundsException(); |
| 271 |
} |
| 272 |
|
| 273 |
} |