| 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.Constants; |
| 9 |
import org.h2.engine.SessionLocal; |
| 10 |
import org.h2.expression.IntervalOperation.IntervalOpType; |
| 11 |
import org.h2.expression.function.DateTimeFunction; |
| 12 |
import org.h2.message.DbException; |
| 13 |
import org.h2.value.DataType; |
| 14 |
import org.h2.value.TypeInfo; |
| 15 |
import org.h2.value.Value; |
| 16 |
import org.h2.value.ValueInteger; |
| 17 |
import org.h2.value.ValueNull; |
| 18 |
import org.h2.value.ValueNumeric; |
| 19 |
|
| 20 |
/** |
| 21 |
* A mathematical expression, or string concatenation. |
| 22 |
*/ |
| 23 |
public class BinaryOperation extends Operation2 { |
| 24 |
|
| 25 |
public enum OpType { |
| 26 |
/** |
| 27 |
* This operation represents an addition as in 1 + 2. |
| 28 |
*/ |
| 29 |
PLUS, |
| 30 |
|
| 31 |
/** |
| 32 |
* This operation represents a subtraction as in 2 - 1. |
| 33 |
*/ |
| 34 |
MINUS, |
| 35 |
|
| 36 |
/** |
| 37 |
* This operation represents a multiplication as in 2 * 3. |
| 38 |
*/ |
| 39 |
MULTIPLY, |
| 40 |
|
| 41 |
/** |
| 42 |
* This operation represents a division as in 4 / 2. |
| 43 |
*/ |
| 44 |
DIVIDE |
| 45 |
} |
| 46 |
|
| 47 |
private OpType opType; |
| 48 |
private TypeInfo forcedType; |
| 49 |
private boolean convertRight = true; |
| 50 |
|
| 51 |
public BinaryOperation(OpType opType, Expression left, Expression right) { |
| 52 |
super(left, right); |
| 53 |
this.opType = opType; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Sets a forced data type of a datetime minus datetime operation. |
| 58 |
* |
| 59 |
* @param forcedType the forced data type |
| 60 |
*/ |
| 61 |
public void setForcedType(TypeInfo forcedType) { |
| 62 |
if (opType != OpType.MINUS) { |
| 63 |
throw getUnexpectedForcedTypeException(); |
| 64 |
} |
| 65 |
this.forcedType = forcedType; |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public boolean needParentheses() { |
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) { |
| 75 |
// don't remove the space, otherwise it might end up some thing like |
| 76 |
// --1 which is a line remark |
| 77 |
left.getSQL(builder, sqlFlags, AUTO_PARENTHESES).append(' ').append(getOperationToken()).append(' '); |
| 78 |
return right.getSQL(builder, sqlFlags, AUTO_PARENTHESES); |
| 79 |
} |
| 80 |
|
| 81 |
private String getOperationToken() { |
| 82 |
switch (opType) { |
| 83 |
case PLUS: |
| 84 |
return "+"; |
| 85 |
case MINUS: |
| 86 |
return "-"; |
| 87 |
case MULTIPLY: |
| 88 |
return "*"; |
| 89 |
case DIVIDE: |
| 90 |
return "/"; |
| 91 |
default: |
| 92 |
throw DbException.getInternalError("opType=" + opType); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public Value getValue(SessionLocal session) { |
| 98 |
Value l = left.getValue(session).convertTo(type, session); |
| 99 |
Value r = right.getValue(session); |
| 100 |
if (convertRight) { |
| 101 |
r = r.convertTo(type, session); |
| 102 |
} |
| 103 |
switch (opType) { |
| 104 |
case PLUS: |
| 105 |
if (l == ValueNull.INSTANCE || r == ValueNull.INSTANCE) { |
| 106 |
return ValueNull.INSTANCE; |
| 107 |
} |
| 108 |
return l.add(r); |
| 109 |
case MINUS: |
| 110 |
if (l == ValueNull.INSTANCE || r == ValueNull.INSTANCE) { |
| 111 |
return ValueNull.INSTANCE; |
| 112 |
} |
| 113 |
return l.subtract(r); |
| 114 |
case MULTIPLY: |
| 115 |
if (l == ValueNull.INSTANCE || r == ValueNull.INSTANCE) { |
| 116 |
return ValueNull.INSTANCE; |
| 117 |
} |
| 118 |
return l.multiply(r); |
| 119 |
case DIVIDE: |
| 120 |
if (l == ValueNull.INSTANCE || r == ValueNull.INSTANCE) { |
| 121 |
return ValueNull.INSTANCE; |
| 122 |
} |
| 123 |
return l.divide(r, type); |
| 124 |
default: |
| 125 |
throw DbException.getInternalError("type=" + opType); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public Expression optimize(SessionLocal session) { |
| 131 |
left = left.optimize(session); |
| 132 |
right = right.optimize(session); |
| 133 |
TypeInfo leftType = left.getType(), rightType = right.getType(); |
| 134 |
int l = leftType.getValueType(), r = rightType.getValueType(); |
| 135 |
if ((l == Value.NULL && r == Value.NULL) || (l == Value.UNKNOWN && r == Value.UNKNOWN)) { |
| 136 |
// (? + ?) - use decimal by default (the most safe data type) or |
| 137 |
// string when text concatenation with + is enabled |
| 138 |
if (opType == OpType.PLUS && session.getDatabase().getMode().allowPlusForStringConcat) { |
| 139 |
return new ConcatenationOperation(left, right).optimize(session); |
| 140 |
} else { |
| 141 |
type = TypeInfo.TYPE_NUMERIC_FLOATING_POINT; |
| 142 |
} |
| 143 |
} else if (DataType.isIntervalType(l) || DataType.isIntervalType(r)) { |
| 144 |
if (forcedType != null) { |
| 145 |
throw getUnexpectedForcedTypeException(); |
| 146 |
} |
| 147 |
return optimizeInterval(l, r); |
| 148 |
} else if (DataType.isDateTimeType(l) || DataType.isDateTimeType(r)) { |
| 149 |
return optimizeDateTime(session, l, r); |
| 150 |
} else if (forcedType != null) { |
| 151 |
throw getUnexpectedForcedTypeException(); |
| 152 |
} else { |
| 153 |
int dataType = Value.getHigherOrder(l, r); |
| 154 |
if (dataType == Value.NUMERIC) { |
| 155 |
optimizeNumeric(leftType, rightType); |
| 156 |
} else if (dataType == Value.DECFLOAT) { |
| 157 |
optimizeDecfloat(leftType, rightType); |
| 158 |
} else if (dataType == Value.ENUM) { |
| 159 |
type = TypeInfo.TYPE_INTEGER; |
| 160 |
} else if (DataType.isCharacterStringType(dataType) |
| 161 |
&& opType == OpType.PLUS && session.getDatabase().getMode().allowPlusForStringConcat) { |
| 162 |
return new ConcatenationOperation(left, right).optimize(session); |
| 163 |
} else { |
| 164 |
type = TypeInfo.getTypeInfo(dataType); |
| 165 |
} |
| 166 |
} |
| 167 |
if (left.isConstant() && right.isConstant()) { |
| 168 |
return ValueExpression.get(getValue(session)); |
| 169 |
} |
| 170 |
return this; |
| 171 |
} |
| 172 |
|
| 173 |
private void optimizeNumeric(TypeInfo leftType, TypeInfo rightType) { |
| 174 |
leftType = leftType.toNumericType(); |
| 175 |
rightType = rightType.toNumericType(); |
| 176 |
long leftPrecision = leftType.getPrecision(), rightPrecision = rightType.getPrecision(); |
| 177 |
int leftScale = leftType.getScale(), rightScale = rightType.getScale(); |
| 178 |
long precision; |
| 179 |
int scale; |
| 180 |
switch (opType) { |
| 181 |
case PLUS: |
| 182 |
case MINUS: |
| 183 |
// Precision is implementation-defined. |
| 184 |
// Scale must be max(leftScale, rightScale). |
| 185 |
// Choose the largest scale and adjust the precision of other |
| 186 |
// argument. |
| 187 |
if (leftScale < rightScale) { |
| 188 |
leftPrecision += rightScale - leftScale; |
| 189 |
scale = rightScale; |
| 190 |
} else { |
| 191 |
rightPrecision += leftScale - rightScale; |
| 192 |
scale = leftScale; |
| 193 |
} |
| 194 |
// Add one extra digit to the largest precision. |
| 195 |
precision = Math.max(leftPrecision, rightPrecision) + 1; |
| 196 |
break; |
| 197 |
case MULTIPLY: |
| 198 |
// Precision is implementation-defined. |
| 199 |
// Scale must be leftScale + rightScale. |
| 200 |
// Use sum of precisions. |
| 201 |
precision = leftPrecision + rightPrecision; |
| 202 |
scale = leftScale + rightScale; |
| 203 |
break; |
| 204 |
case DIVIDE: { |
| 205 |
// Precision and scale are implementation-defined. |
| 206 |
long scaleAsLong = leftScale - rightScale + rightPrecision * 2; |
| 207 |
if (scaleAsLong >= ValueNumeric.MAXIMUM_SCALE) { |
| 208 |
scale = ValueNumeric.MAXIMUM_SCALE; |
| 209 |
} else if (scaleAsLong <= 0) { |
| 210 |
scale = 0; |
| 211 |
} else { |
| 212 |
scale = (int) scaleAsLong; |
| 213 |
} |
| 214 |
// Divider can be effectively multiplied by no more than |
| 215 |
// 10^rightScale, so add rightScale to its precision and adjust the |
| 216 |
// result to the changes in scale. |
| 217 |
precision = leftPrecision + rightScale - leftScale + scale; |
| 218 |
// If precision is too large, reduce it together with scale |
| 219 |
if (precision > Constants.MAX_NUMERIC_PRECISION) { |
| 220 |
long sub = Math.min(precision - Constants.MAX_NUMERIC_PRECISION, scale); |
| 221 |
precision -= sub; |
| 222 |
scale -= sub; |
| 223 |
} |
| 224 |
break; |
| 225 |
} |
| 226 |
default: |
| 227 |
throw DbException.getInternalError("type=" + opType); |
| 228 |
} |
| 229 |
type = TypeInfo.getTypeInfo(Value.NUMERIC, precision, scale, null); |
| 230 |
} |
| 231 |
|
| 232 |
private void optimizeDecfloat(TypeInfo leftType, TypeInfo rightType) { |
| 233 |
leftType = leftType.toDecfloatType(); |
| 234 |
rightType = rightType.toDecfloatType(); |
| 235 |
long leftPrecision = leftType.getPrecision(), rightPrecision = rightType.getPrecision(); |
| 236 |
long precision; |
| 237 |
switch (opType) { |
| 238 |
case PLUS: |
| 239 |
case MINUS: |
| 240 |
case DIVIDE: |
| 241 |
// Add one extra digit to the largest precision. |
| 242 |
precision = Math.max(leftPrecision, rightPrecision) + 1; |
| 243 |
break; |
| 244 |
case MULTIPLY: |
| 245 |
// Use sum of precisions. |
| 246 |
precision = leftPrecision + rightPrecision; |
| 247 |
break; |
| 248 |
default: |
| 249 |
throw DbException.getInternalError("type=" + opType); |
| 250 |
} |
| 251 |
type = TypeInfo.getTypeInfo(Value.DECFLOAT, precision, 0, null); |
| 252 |
} |
| 253 |
|
| 254 |
private Expression optimizeInterval(int l, int r) { |
| 255 |
boolean lInterval = false, lNumeric = false, lDateTime = false; |
| 256 |
if (DataType.isIntervalType(l)) { |
| 257 |
lInterval = true; |
| 258 |
} else if (DataType.isNumericType(l)) { |
| 259 |
lNumeric = true; |
| 260 |
} else if (DataType.isDateTimeType(l)) { |
| 261 |
lDateTime = true; |
| 262 |
} else { |
| 263 |
throw getUnsupported(l, r); |
| 264 |
} |
| 265 |
boolean rInterval = false, rNumeric = false, rDateTime = false; |
| 266 |
if (DataType.isIntervalType(r)) { |
| 267 |
rInterval = true; |
| 268 |
} else if (DataType.isNumericType(r)) { |
| 269 |
rNumeric = true; |
| 270 |
} else if (DataType.isDateTimeType(r)) { |
| 271 |
rDateTime = true; |
| 272 |
} else { |
| 273 |
throw getUnsupported(l, r); |
| 274 |
} |
| 275 |
switch (opType) { |
| 276 |
case PLUS: |
| 277 |
if (lInterval && rInterval) { |
| 278 |
if (DataType.isYearMonthIntervalType(l) == DataType.isYearMonthIntervalType(r)) { |
| 279 |
return new IntervalOperation(IntervalOpType.INTERVAL_PLUS_INTERVAL, left, right); |
| 280 |
} |
| 281 |
} else if (lInterval && rDateTime) { |
| 282 |
if (r == Value.TIME && DataType.isYearMonthIntervalType(l)) { |
| 283 |
break; |
| 284 |
} |
| 285 |
return new IntervalOperation(IntervalOpType.DATETIME_PLUS_INTERVAL, right, left); |
| 286 |
} else if (lDateTime && rInterval) { |
| 287 |
if (l == Value.TIME && DataType.isYearMonthIntervalType(r)) { |
| 288 |
break; |
| 289 |
} |
| 290 |
return new IntervalOperation(IntervalOpType.DATETIME_PLUS_INTERVAL, left, right); |
| 291 |
} |
| 292 |
break; |
| 293 |
case MINUS: |
| 294 |
if (lInterval && rInterval) { |
| 295 |
if (DataType.isYearMonthIntervalType(l) == DataType.isYearMonthIntervalType(r)) { |
| 296 |
return new IntervalOperation(IntervalOpType.INTERVAL_MINUS_INTERVAL, left, right); |
| 297 |
} |
| 298 |
} else if (lDateTime && rInterval) { |
| 299 |
if (l == Value.TIME && DataType.isYearMonthIntervalType(r)) { |
| 300 |
break; |
| 301 |
} |
| 302 |
return new IntervalOperation(IntervalOpType.DATETIME_MINUS_INTERVAL, left, right); |
| 303 |
} |
| 304 |
break; |
| 305 |
case MULTIPLY: |
| 306 |
if (lInterval && rNumeric) { |
| 307 |
return new IntervalOperation(IntervalOpType.INTERVAL_MULTIPLY_NUMERIC, left, right); |
| 308 |
} else if (lNumeric && rInterval) { |
| 309 |
return new IntervalOperation(IntervalOpType.INTERVAL_MULTIPLY_NUMERIC, right, left); |
| 310 |
} |
| 311 |
break; |
| 312 |
case DIVIDE: |
| 313 |
if (lInterval) { |
| 314 |
if (rNumeric) { |
| 315 |
return new IntervalOperation(IntervalOpType.INTERVAL_DIVIDE_NUMERIC, left, right); |
| 316 |
} else if (rInterval && DataType.isYearMonthIntervalType(l) == DataType.isYearMonthIntervalType(r)) { |
| 317 |
// Non-standard |
| 318 |
return new IntervalOperation(IntervalOpType.INTERVAL_DIVIDE_INTERVAL, left, right); |
| 319 |
} |
| 320 |
} |
| 321 |
break; |
| 322 |
default: |
| 323 |
} |
| 324 |
throw getUnsupported(l, r); |
| 325 |
} |
| 326 |
|
| 327 |
private Expression optimizeDateTime(SessionLocal session, int l, int r) { |
| 328 |
switch (opType) { |
| 329 |
case PLUS: { |
| 330 |
if (DataType.isDateTimeType(l)) { |
| 331 |
if (DataType.isDateTimeType(r)) { |
| 332 |
if (l > r) { |
| 333 |
swap(); |
| 334 |
int t = l; |
| 335 |
l = r; |
| 336 |
r = t; |
| 337 |
} |
| 338 |
return new CompatibilityDatePlusTimeOperation(right, left).optimize(session); |
| 339 |
} |
| 340 |
swap(); |
| 341 |
int t = l; |
| 342 |
l = r; |
| 343 |
r = t; |
| 344 |
} |
| 345 |
switch (l) { |
| 346 |
case Value.INTEGER: |
| 347 |
// Oracle date add |
| 348 |
return new DateTimeFunction(DateTimeFunction.DATEADD, DateTimeFunction.DAY, left, right) |
| 349 |
.optimize(session); |
| 350 |
case Value.NUMERIC: |
| 351 |
case Value.REAL: |
| 352 |
case Value.DOUBLE: |
| 353 |
case Value.DECFLOAT: |
| 354 |
// Oracle date add |
| 355 |
return new DateTimeFunction(DateTimeFunction.DATEADD, DateTimeFunction.SECOND, |
| 356 |
new BinaryOperation(OpType.MULTIPLY, ValueExpression.get(ValueInteger.get(60 * 60 * 24)), |
| 357 |
left), right).optimize(session); |
| 358 |
} |
| 359 |
break; |
| 360 |
} |
| 361 |
case MINUS: |
| 362 |
switch (l) { |
| 363 |
case Value.DATE: |
| 364 |
case Value.TIMESTAMP: |
| 365 |
case Value.TIMESTAMP_TZ: |
| 366 |
switch (r) { |
| 367 |
case Value.INTEGER: { |
| 368 |
if (forcedType != null) { |
| 369 |
throw getUnexpectedForcedTypeException(); |
| 370 |
} |
| 371 |
// Oracle date subtract |
| 372 |
return new DateTimeFunction(DateTimeFunction.DATEADD, DateTimeFunction.DAY, |
| 373 |
new UnaryOperation(right), left).optimize(session); |
| 374 |
} |
| 375 |
case Value.NUMERIC: |
| 376 |
case Value.REAL: |
| 377 |
case Value.DOUBLE: |
| 378 |
case Value.DECFLOAT: { |
| 379 |
if (forcedType != null) { |
| 380 |
throw getUnexpectedForcedTypeException(); |
| 381 |
} |
| 382 |
// Oracle date subtract |
| 383 |
return new DateTimeFunction(DateTimeFunction.DATEADD, DateTimeFunction.SECOND, |
| 384 |
new BinaryOperation(OpType.MULTIPLY, ValueExpression.get(ValueInteger.get(-60 * 60 * 24)), |
| 385 |
right), left).optimize(session); |
| 386 |
} |
| 387 |
case Value.TIME: |
| 388 |
case Value.TIME_TZ: |
| 389 |
case Value.DATE: |
| 390 |
case Value.TIMESTAMP: |
| 391 |
case Value.TIMESTAMP_TZ: |
| 392 |
return new IntervalOperation(IntervalOpType.DATETIME_MINUS_DATETIME, left, right, forcedType); |
| 393 |
} |
| 394 |
break; |
| 395 |
case Value.TIME: |
| 396 |
case Value.TIME_TZ: |
| 397 |
if (DataType.isDateTimeType(r)) { |
| 398 |
return new IntervalOperation(IntervalOpType.DATETIME_MINUS_DATETIME, left, right, forcedType); |
| 399 |
} |
| 400 |
break; |
| 401 |
} |
| 402 |
break; |
| 403 |
case MULTIPLY: |
| 404 |
if (l == Value.TIME) { |
| 405 |
type = TypeInfo.TYPE_TIME; |
| 406 |
convertRight = false; |
| 407 |
return this; |
| 408 |
} else if (r == Value.TIME) { |
| 409 |
swap(); |
| 410 |
type = TypeInfo.TYPE_TIME; |
| 411 |
convertRight = false; |
| 412 |
return this; |
| 413 |
} |
| 414 |
break; |
| 415 |
case DIVIDE: |
| 416 |
if (l == Value.TIME) { |
| 417 |
type = TypeInfo.TYPE_TIME; |
| 418 |
convertRight = false; |
| 419 |
return this; |
| 420 |
} |
| 421 |
break; |
| 422 |
default: |
| 423 |
} |
| 424 |
throw getUnsupported(l, r); |
| 425 |
} |
| 426 |
|
| 427 |
private DbException getUnsupported(int l, int r) { |
| 428 |
return DbException.getUnsupportedException( |
| 429 |
Value.getTypeName(l) + ' ' + getOperationToken() + ' ' + Value.getTypeName(r)); |
| 430 |
} |
| 431 |
|
| 432 |
private DbException getUnexpectedForcedTypeException() { |
| 433 |
StringBuilder builder = getUnenclosedSQL(new StringBuilder(), TRACE_SQL_FLAGS); |
| 434 |
int index = builder.length(); |
| 435 |
return DbException.getSyntaxError( |
| 436 |
IntervalOperation.getForcedTypeSQL(builder.append(' '), forcedType).toString(), index, ""); |
| 437 |
} |
| 438 |
|
| 439 |
private void swap() { |
| 440 |
Expression temp = left; |
| 441 |
left = right; |
| 442 |
right = temp; |
| 443 |
} |
| 444 |
|
| 445 |
/** |
| 446 |
* Returns the type of this binary operation. |
| 447 |
* |
| 448 |
* @return the type of this binary operation |
| 449 |
*/ |
| 450 |
public OpType getOperationType() { |
| 451 |
return opType; |
| 452 |
} |
| 453 |
|
| 454 |
} |