| 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 java.util.List; |
| 9 |
|
| 10 |
import org.h2.api.ErrorCode; |
| 11 |
import org.h2.engine.Constants; |
| 12 |
import org.h2.engine.SessionLocal; |
| 13 |
import org.h2.expression.function.NamedExpression; |
| 14 |
import org.h2.message.DbException; |
| 15 |
import org.h2.table.Column; |
| 16 |
import org.h2.table.ColumnResolver; |
| 17 |
import org.h2.table.TableFilter; |
| 18 |
import org.h2.util.HasSQL; |
| 19 |
import org.h2.util.StringUtils; |
| 20 |
import org.h2.value.TypeInfo; |
| 21 |
import org.h2.value.Typed; |
| 22 |
import org.h2.value.Value; |
| 23 |
|
| 24 |
/** |
| 25 |
* An expression is a operation, a value, or a function in a query. |
| 26 |
*/ |
| 27 |
public abstract class Expression implements HasSQL, Typed { |
| 28 |
|
| 29 |
/** |
| 30 |
* Initial state for {@link #mapColumns(ColumnResolver, int, int)}. |
| 31 |
*/ |
| 32 |
public static final int MAP_INITIAL = 0; |
| 33 |
|
| 34 |
/** |
| 35 |
* State for expressions inside a window function for |
| 36 |
* {@link #mapColumns(ColumnResolver, int, int)}. |
| 37 |
*/ |
| 38 |
public static final int MAP_IN_WINDOW = 1; |
| 39 |
|
| 40 |
/** |
| 41 |
* State for expressions inside an aggregate for |
| 42 |
* {@link #mapColumns(ColumnResolver, int, int)}. |
| 43 |
*/ |
| 44 |
public static final int MAP_IN_AGGREGATE = 2; |
| 45 |
|
| 46 |
/** |
| 47 |
* Wrap expression in parentheses only if it can't be safely included into |
| 48 |
* other expressions without them. |
| 49 |
*/ |
| 50 |
public static final int AUTO_PARENTHESES = 0; |
| 51 |
|
| 52 |
/** |
| 53 |
* Wrap expression in parentheses unconditionally. |
| 54 |
*/ |
| 55 |
public static final int WITH_PARENTHESES = 1; |
| 56 |
|
| 57 |
/** |
| 58 |
* Do not wrap expression in parentheses. |
| 59 |
*/ |
| 60 |
public static final int WITHOUT_PARENTHESES = 2; |
| 61 |
|
| 62 |
private boolean addedToFilter; |
| 63 |
|
| 64 |
/** |
| 65 |
* Get the SQL snippet for a list of expressions. |
| 66 |
* |
| 67 |
* @param builder the builder to append the SQL to |
| 68 |
* @param expressions the list of expressions |
| 69 |
* @param sqlFlags formatting flags |
| 70 |
* @return the specified string builder |
| 71 |
*/ |
| 72 |
public static StringBuilder writeExpressions(StringBuilder builder, List<? extends Expression> expressions, |
| 73 |
int sqlFlags) { |
| 74 |
for (int i = 0, length = expressions.size(); i < length; i++) { |
| 75 |
if (i > 0) { |
| 76 |
builder.append(", "); |
| 77 |
} |
| 78 |
expressions.get(i).getUnenclosedSQL(builder, sqlFlags); |
| 79 |
} |
| 80 |
return builder; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Get the SQL snippet for an array of expressions. |
| 85 |
* |
| 86 |
* @param builder the builder to append the SQL to |
| 87 |
* @param expressions the list of expressions |
| 88 |
* @param sqlFlags formatting flags |
| 89 |
* @return the specified string builder |
| 90 |
*/ |
| 91 |
public static StringBuilder writeExpressions(StringBuilder builder, Expression[] expressions, int sqlFlags) { |
| 92 |
for (int i = 0, length = expressions.length; i < length; i++) { |
| 93 |
if (i > 0) { |
| 94 |
builder.append(", "); |
| 95 |
} |
| 96 |
Expression e = expressions[i]; |
| 97 |
if (e == null) { |
| 98 |
builder.append("DEFAULT"); |
| 99 |
} else { |
| 100 |
e.getUnenclosedSQL(builder, sqlFlags); |
| 101 |
} |
| 102 |
} |
| 103 |
return builder; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Return the resulting value for the current row. |
| 108 |
* |
| 109 |
* @param session the session |
| 110 |
* @return the result |
| 111 |
*/ |
| 112 |
public abstract Value getValue(SessionLocal session); |
| 113 |
|
| 114 |
/** |
| 115 |
* Returns the data type. The data type may be unknown before the |
| 116 |
* optimization phase. |
| 117 |
* |
| 118 |
* @return the data type |
| 119 |
*/ |
| 120 |
@Override |
| 121 |
public abstract TypeInfo getType(); |
| 122 |
|
| 123 |
/** |
| 124 |
* Map the columns of the resolver to expression columns. |
| 125 |
* |
| 126 |
* @param resolver the column resolver |
| 127 |
* @param level the subquery nesting level |
| 128 |
* @param state current state for nesting checks, initial value is |
| 129 |
* {@link #MAP_INITIAL} |
| 130 |
*/ |
| 131 |
public abstract void mapColumns(ColumnResolver resolver, int level, int state); |
| 132 |
|
| 133 |
/** |
| 134 |
* Try to optimize the expression. |
| 135 |
* |
| 136 |
* @param session the session |
| 137 |
* @return the optimized expression |
| 138 |
*/ |
| 139 |
public abstract Expression optimize(SessionLocal session); |
| 140 |
|
| 141 |
/** |
| 142 |
* Try to optimize or remove the condition. |
| 143 |
* |
| 144 |
* @param session the session |
| 145 |
* @return the optimized condition, or {@code null} |
| 146 |
*/ |
| 147 |
public final Expression optimizeCondition(SessionLocal session) { |
| 148 |
Expression e = optimize(session); |
| 149 |
if (e.isConstant()) { |
| 150 |
return e.getBooleanValue(session) ? null : ValueExpression.FALSE; |
| 151 |
} |
| 152 |
return e; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Tell the expression columns whether the table filter can return values |
| 157 |
* now. This is used when optimizing the query. |
| 158 |
* |
| 159 |
* @param tableFilter the table filter |
| 160 |
* @param value true if the table filter can return value |
| 161 |
*/ |
| 162 |
public abstract void setEvaluatable(TableFilter tableFilter, boolean value); |
| 163 |
|
| 164 |
@Override |
| 165 |
public final String getSQL(int sqlFlags) { |
| 166 |
return getSQL(new StringBuilder(), sqlFlags, AUTO_PARENTHESES).toString(); |
| 167 |
} |
| 168 |
|
| 169 |
@Override |
| 170 |
public final StringBuilder getSQL(StringBuilder builder, int sqlFlags) { |
| 171 |
return getSQL(builder, sqlFlags, AUTO_PARENTHESES); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Get the SQL statement of this expression. This may not always be the |
| 176 |
* original SQL statement, especially after optimization. |
| 177 |
* |
| 178 |
* @param sqlFlags |
| 179 |
* formatting flags |
| 180 |
* @param parentheses |
| 181 |
* parentheses mode |
| 182 |
* @return the SQL statement |
| 183 |
*/ |
| 184 |
public final String getSQL(int sqlFlags, int parentheses) { |
| 185 |
return getSQL(new StringBuilder(), sqlFlags, parentheses).toString(); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Get the SQL statement of this expression. This may not always be the |
| 190 |
* original SQL statement, especially after optimization. |
| 191 |
* |
| 192 |
* @param builder |
| 193 |
* string builder |
| 194 |
* @param sqlFlags |
| 195 |
* formatting flags |
| 196 |
* @param parentheses |
| 197 |
* parentheses mode |
| 198 |
* @return the specified string builder |
| 199 |
*/ |
| 200 |
public final StringBuilder getSQL(StringBuilder builder, int sqlFlags, int parentheses) { |
| 201 |
return parentheses == WITH_PARENTHESES || parentheses != WITHOUT_PARENTHESES && needParentheses() |
| 202 |
? getUnenclosedSQL(builder.append('('), sqlFlags).append(')') |
| 203 |
: getUnenclosedSQL(builder, sqlFlags); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Returns whether this expressions needs to be wrapped in parentheses when |
| 208 |
* it is used as an argument of other expressions. |
| 209 |
* |
| 210 |
* @return {@code true} if it is |
| 211 |
*/ |
| 212 |
public boolean needParentheses() { |
| 213 |
return false; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Get the SQL statement of this expression. This may not always be the |
| 218 |
* original SQL statement, especially after optimization. Enclosing '(' and |
| 219 |
* ')' are always appended. |
| 220 |
* |
| 221 |
* @param builder |
| 222 |
* string builder |
| 223 |
* @param sqlFlags |
| 224 |
* formatting flags |
| 225 |
* @return the specified string builder |
| 226 |
*/ |
| 227 |
public final StringBuilder getEnclosedSQL(StringBuilder builder, int sqlFlags) { |
| 228 |
return getUnenclosedSQL(builder.append('('), sqlFlags).append(')'); |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Get the SQL statement of this expression. This may not always be the |
| 233 |
* original SQL statement, especially after optimization. Enclosing '(' and |
| 234 |
* ')' are never appended. |
| 235 |
* |
| 236 |
* @param builder |
| 237 |
* string builder |
| 238 |
* @param sqlFlags |
| 239 |
* formatting flags |
| 240 |
* @return the specified string builder |
| 241 |
*/ |
| 242 |
public abstract StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags); |
| 243 |
|
| 244 |
/** |
| 245 |
* Update an aggregate value. This method is called at statement execution |
| 246 |
* time. It is usually called once for each row, but if the expression is |
| 247 |
* used multiple times (for example in the column list, and as part of the |
| 248 |
* HAVING expression) it is called multiple times - the row counter needs to |
| 249 |
* be used to make sure the internal state is only updated once. |
| 250 |
* |
| 251 |
* @param session the session |
| 252 |
* @param stage select stage |
| 253 |
*/ |
| 254 |
public abstract void updateAggregate(SessionLocal session, int stage); |
| 255 |
|
| 256 |
/** |
| 257 |
* Check if this expression and all sub-expressions can fulfill a criteria. |
| 258 |
* If any part returns false, the result is false. |
| 259 |
* |
| 260 |
* @param visitor the visitor |
| 261 |
* @return if the criteria can be fulfilled |
| 262 |
*/ |
| 263 |
public abstract boolean isEverything(ExpressionVisitor visitor); |
| 264 |
|
| 265 |
/** |
| 266 |
* Estimate the cost to process the expression. |
| 267 |
* Used when optimizing the query, to calculate the query plan |
| 268 |
* with the lowest estimated cost. |
| 269 |
* |
| 270 |
* @return the estimated cost |
| 271 |
*/ |
| 272 |
public abstract int getCost(); |
| 273 |
|
| 274 |
/** |
| 275 |
* If it is possible, return the negated expression. This is used |
| 276 |
* to optimize NOT expressions: NOT ID>10 can be converted to |
| 277 |
* ID<=10. Returns null if negating is not possible. |
| 278 |
* |
| 279 |
* @param session the session |
| 280 |
* @return the negated expression, or null |
| 281 |
*/ |
| 282 |
public Expression getNotIfPossible(@SuppressWarnings("unused") SessionLocal session) { |
| 283 |
// by default it is not possible |
| 284 |
return null; |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Check if this expression will always return the same value. |
| 289 |
* |
| 290 |
* @return if the expression is constant |
| 291 |
*/ |
| 292 |
public boolean isConstant() { |
| 293 |
return false; |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Check if this expression will always return the NULL value. |
| 298 |
* |
| 299 |
* @return if the expression is constant NULL value |
| 300 |
*/ |
| 301 |
public boolean isNullConstant() { |
| 302 |
return false; |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* Is the value of a parameter set. |
| 307 |
* |
| 308 |
* @return true if set |
| 309 |
*/ |
| 310 |
public boolean isValueSet() { |
| 311 |
return false; |
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* Check if this is an identity column. |
| 316 |
* |
| 317 |
* @return true if it is an identity column |
| 318 |
*/ |
| 319 |
public boolean isIdentity() { |
| 320 |
return false; |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Get the value in form of a boolean expression. |
| 325 |
* Returns true or false. |
| 326 |
* In this database, everything can be a condition. |
| 327 |
* |
| 328 |
* @param session the session |
| 329 |
* @return the result |
| 330 |
*/ |
| 331 |
public boolean getBooleanValue(SessionLocal session) { |
| 332 |
return getValue(session).isTrue(); |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* Create index conditions if possible and attach them to the table filter. |
| 337 |
* |
| 338 |
* @param session the session |
| 339 |
* @param filter the table filter |
| 340 |
*/ |
| 341 |
@SuppressWarnings("unused") |
| 342 |
public void createIndexConditions(SessionLocal session, TableFilter filter) { |
| 343 |
// default is do nothing |
| 344 |
} |
| 345 |
|
| 346 |
/** |
| 347 |
* Get the column name or alias name of this expression. |
| 348 |
* |
| 349 |
* @param session the session |
| 350 |
* @param columnIndex 0-based column index |
| 351 |
* @return the column name |
| 352 |
*/ |
| 353 |
public String getColumnName(SessionLocal session, int columnIndex) { |
| 354 |
return getAlias(session, columnIndex); |
| 355 |
} |
| 356 |
|
| 357 |
/** |
| 358 |
* Get the schema name, or null |
| 359 |
* |
| 360 |
* @return the schema name |
| 361 |
*/ |
| 362 |
public String getSchemaName() { |
| 363 |
return null; |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* Get the table name, or null |
| 368 |
* |
| 369 |
* @return the table name |
| 370 |
*/ |
| 371 |
public String getTableName() { |
| 372 |
return null; |
| 373 |
} |
| 374 |
|
| 375 |
/** |
| 376 |
* Check whether this expression is a column and can store NULL. |
| 377 |
* |
| 378 |
* @return whether NULL is allowed |
| 379 |
*/ |
| 380 |
public int getNullable() { |
| 381 |
return Column.NULLABLE_UNKNOWN; |
| 382 |
} |
| 383 |
|
| 384 |
/** |
| 385 |
* Get the table alias name or null |
| 386 |
* if this expression does not represent a column. |
| 387 |
* |
| 388 |
* @return the table alias name |
| 389 |
*/ |
| 390 |
public String getTableAlias() { |
| 391 |
return null; |
| 392 |
} |
| 393 |
|
| 394 |
/** |
| 395 |
* Get the alias name of a column or SQL expression |
| 396 |
* if it is not an aliased expression. |
| 397 |
* |
| 398 |
* @param session the session |
| 399 |
* @param columnIndex 0-based column index |
| 400 |
* @return the alias name |
| 401 |
*/ |
| 402 |
public String getAlias(SessionLocal session, int columnIndex) { |
| 403 |
switch (session.getMode().expressionNames) { |
| 404 |
default: { |
| 405 |
String sql = getSQL(QUOTE_ONLY_WHEN_REQUIRED | NO_CASTS, WITHOUT_PARENTHESES); |
| 406 |
if (sql.length() <= Constants.MAX_IDENTIFIER_LENGTH) { |
| 407 |
return sql; |
| 408 |
} |
| 409 |
} |
| 410 |
//$FALL-THROUGH$ |
| 411 |
case C_NUMBER: |
| 412 |
return "C" + (columnIndex + 1); |
| 413 |
case EMPTY: |
| 414 |
return ""; |
| 415 |
case NUMBER: |
| 416 |
return Integer.toString(columnIndex + 1); |
| 417 |
case POSTGRESQL_STYLE: |
| 418 |
if (this instanceof NamedExpression) { |
| 419 |
return StringUtils.toLowerEnglish(((NamedExpression) this).getName()); |
| 420 |
} |
| 421 |
return "?column?"; |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
/** |
| 426 |
* Get the column name of this expression for a view. |
| 427 |
* |
| 428 |
* @param session the session |
| 429 |
* @param columnIndex 0-based column index |
| 430 |
* @return the column name for a view |
| 431 |
*/ |
| 432 |
public String getColumnNameForView(SessionLocal session, int columnIndex) { |
| 433 |
switch (session.getMode().viewExpressionNames) { |
| 434 |
case AS_IS: |
| 435 |
default: |
| 436 |
return getAlias(session, columnIndex); |
| 437 |
case EXCEPTION: |
| 438 |
throw DbException.get(ErrorCode.COLUMN_ALIAS_IS_NOT_SPECIFIED_1, getTraceSQL()); |
| 439 |
case MYSQL_STYLE: { |
| 440 |
String name = getSQL(QUOTE_ONLY_WHEN_REQUIRED | NO_CASTS, WITHOUT_PARENTHESES); |
| 441 |
if (name.length() > 64) { |
| 442 |
name = "Name_exp_" + (columnIndex + 1); |
| 443 |
} |
| 444 |
return name; |
| 445 |
} |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
/** |
| 450 |
* Returns the main expression, skipping aliases. |
| 451 |
* |
| 452 |
* @return the expression |
| 453 |
*/ |
| 454 |
public Expression getNonAliasExpression() { |
| 455 |
return this; |
| 456 |
} |
| 457 |
|
| 458 |
/** |
| 459 |
* Add conditions to a table filter if they can be evaluated. |
| 460 |
* |
| 461 |
* @param filter the table filter |
| 462 |
*/ |
| 463 |
public void addFilterConditions(TableFilter filter) { |
| 464 |
if (!addedToFilter && isEverything(ExpressionVisitor.EVALUATABLE_VISITOR)) { |
| 465 |
filter.addFilterCondition(this, false); |
| 466 |
addedToFilter = true; |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
/** |
| 471 |
* Convert this expression to a String. |
| 472 |
* |
| 473 |
* @return the string representation |
| 474 |
*/ |
| 475 |
@Override |
| 476 |
public String toString() { |
| 477 |
return getTraceSQL(); |
| 478 |
} |
| 479 |
|
| 480 |
/** |
| 481 |
* Returns count of subexpressions. |
| 482 |
* |
| 483 |
* @return count of subexpressions |
| 484 |
*/ |
| 485 |
public int getSubexpressionCount() { |
| 486 |
return 0; |
| 487 |
} |
| 488 |
|
| 489 |
/** |
| 490 |
* Returns subexpression with specified index. |
| 491 |
* |
| 492 |
* @param index 0-based index |
| 493 |
* @return subexpression with specified index, may be null |
| 494 |
* @throws IndexOutOfBoundsException if specified index is not valid |
| 495 |
*/ |
| 496 |
public Expression getSubexpression(int index) { |
| 497 |
throw new IndexOutOfBoundsException(); |
| 498 |
} |
| 499 |
|
| 500 |
/** |
| 501 |
* Return the resulting value of when operand for the current row. |
| 502 |
* |
| 503 |
* @param session |
| 504 |
* the session |
| 505 |
* @param left |
| 506 |
* value on the left side |
| 507 |
* @return the result |
| 508 |
*/ |
| 509 |
public boolean getWhenValue(SessionLocal session, Value left) { |
| 510 |
return session.compareWithNull(left, getValue(session), true) == 0; |
| 511 |
} |
| 512 |
|
| 513 |
/** |
| 514 |
* Appends the SQL statement of this when operand to the specified builder. |
| 515 |
* |
| 516 |
* @param builder |
| 517 |
* string builder |
| 518 |
* @param sqlFlags |
| 519 |
* formatting flags |
| 520 |
* @return the specified string builder |
| 521 |
*/ |
| 522 |
public StringBuilder getWhenSQL(StringBuilder builder, int sqlFlags) { |
| 523 |
return getUnenclosedSQL(builder.append(' '), sqlFlags); |
| 524 |
} |
| 525 |
|
| 526 |
/** |
| 527 |
* Returns whether this expression is a right side of condition in a when |
| 528 |
* operand. |
| 529 |
* |
| 530 |
* @return {@code true} if it is, {@code false} otherwise |
| 531 |
*/ |
| 532 |
public boolean isWhenConditionOperand() { |
| 533 |
return false; |
| 534 |
} |
| 535 |
|
| 536 |
} |