| 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.api.ErrorCode; |
| 9 |
import org.h2.command.query.Select; |
| 10 |
import org.h2.command.query.SelectGroups; |
| 11 |
import org.h2.command.query.SelectListColumnResolver; |
| 12 |
import org.h2.engine.Database; |
| 13 |
import org.h2.engine.SessionLocal; |
| 14 |
import org.h2.expression.analysis.DataAnalysisOperation; |
| 15 |
import org.h2.expression.condition.Comparison; |
| 16 |
import org.h2.index.IndexCondition; |
| 17 |
import org.h2.message.DbException; |
| 18 |
import org.h2.mode.ModeFunction; |
| 19 |
import org.h2.schema.Constant; |
| 20 |
import org.h2.schema.Schema; |
| 21 |
import org.h2.table.Column; |
| 22 |
import org.h2.table.ColumnResolver; |
| 23 |
import org.h2.table.Table; |
| 24 |
import org.h2.table.TableFilter; |
| 25 |
import org.h2.util.ParserUtil; |
| 26 |
import org.h2.util.StringUtils; |
| 27 |
import org.h2.value.TypeInfo; |
| 28 |
import org.h2.value.Value; |
| 29 |
import org.h2.value.ValueBigint; |
| 30 |
import org.h2.value.ValueBoolean; |
| 31 |
import org.h2.value.ValueDecfloat; |
| 32 |
import org.h2.value.ValueDouble; |
| 33 |
import org.h2.value.ValueInteger; |
| 34 |
import org.h2.value.ValueNumeric; |
| 35 |
import org.h2.value.ValueReal; |
| 36 |
import org.h2.value.ValueSmallint; |
| 37 |
import org.h2.value.ValueTinyint; |
| 38 |
|
| 39 |
/** |
| 40 |
* A column reference expression that represents a column of a table or view. |
| 41 |
*/ |
| 42 |
public final class ExpressionColumn extends Expression { |
| 43 |
|
| 44 |
private final Database database; |
| 45 |
private final String schemaName; |
| 46 |
private final String tableAlias; |
| 47 |
private final String columnName; |
| 48 |
private final boolean rowId; |
| 49 |
private final boolean quotedName; |
| 50 |
private ColumnResolver columnResolver; |
| 51 |
private int queryLevel; |
| 52 |
private Column column; |
| 53 |
|
| 54 |
/** |
| 55 |
* Creates a new column reference for metadata of queries; should not be |
| 56 |
* used as normal expression. |
| 57 |
* |
| 58 |
* @param database |
| 59 |
* the database |
| 60 |
* @param column |
| 61 |
* the column |
| 62 |
*/ |
| 63 |
public ExpressionColumn(Database database, Column column) { |
| 64 |
this.database = database; |
| 65 |
this.column = column; |
| 66 |
columnName = tableAlias = schemaName = null; |
| 67 |
rowId = column.isRowId(); |
| 68 |
quotedName = true; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Creates a new instance of column reference for regular columns as normal |
| 73 |
* expression. |
| 74 |
* |
| 75 |
* @param database |
| 76 |
* the database |
| 77 |
* @param schemaName |
| 78 |
* the schema name, or {@code null} |
| 79 |
* @param tableAlias |
| 80 |
* the table alias name, table name, or {@code null} |
| 81 |
* @param columnName |
| 82 |
* the column name |
| 83 |
*/ |
| 84 |
public ExpressionColumn(Database database, String schemaName, String tableAlias, String columnName) { |
| 85 |
this(database, schemaName, tableAlias, columnName, true); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Creates a new instance of column reference for regular columns as normal |
| 90 |
* expression. |
| 91 |
* |
| 92 |
* @param database |
| 93 |
* the database |
| 94 |
* @param schemaName |
| 95 |
* the schema name, or {@code null} |
| 96 |
* @param tableAlias |
| 97 |
* the table alias name, table name, or {@code null} |
| 98 |
* @param columnName |
| 99 |
* the column name |
| 100 |
* @param quotedName |
| 101 |
* whether name was quoted |
| 102 |
*/ |
| 103 |
public ExpressionColumn(Database database, String schemaName, String tableAlias, String columnName, |
| 104 |
boolean quotedName) { |
| 105 |
this.database = database; |
| 106 |
this.schemaName = schemaName; |
| 107 |
this.tableAlias = tableAlias; |
| 108 |
this.columnName = columnName; |
| 109 |
rowId = false; |
| 110 |
this.quotedName = quotedName; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Creates a new instance of column reference for {@code _ROWID_} column as |
| 115 |
* normal expression. |
| 116 |
* |
| 117 |
* @param database |
| 118 |
* the database |
| 119 |
* @param schemaName |
| 120 |
* the schema name, or {@code null} |
| 121 |
* @param tableAlias |
| 122 |
* the table alias name, table name, or {@code null} |
| 123 |
*/ |
| 124 |
public ExpressionColumn(Database database, String schemaName, String tableAlias) { |
| 125 |
this.database = database; |
| 126 |
this.schemaName = schemaName; |
| 127 |
this.tableAlias = tableAlias; |
| 128 |
columnName = Column.ROWID; |
| 129 |
quotedName = rowId = true; |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) { |
| 134 |
if (schemaName != null) { |
| 135 |
ParserUtil.quoteIdentifier(builder, schemaName, sqlFlags).append('.'); |
| 136 |
} |
| 137 |
if (tableAlias != null) { |
| 138 |
ParserUtil.quoteIdentifier(builder, tableAlias, sqlFlags).append('.'); |
| 139 |
} |
| 140 |
if (column != null) { |
| 141 |
if (columnResolver != null && columnResolver.hasDerivedColumnList()) { |
| 142 |
ParserUtil.quoteIdentifier(builder, columnResolver.getColumnName(column), sqlFlags); |
| 143 |
} else { |
| 144 |
column.getSQL(builder, sqlFlags); |
| 145 |
} |
| 146 |
} else if (rowId) { |
| 147 |
builder.append(columnName); |
| 148 |
} else { |
| 149 |
ParserUtil.quoteIdentifier(builder, columnName, sqlFlags); |
| 150 |
} |
| 151 |
return builder; |
| 152 |
} |
| 153 |
|
| 154 |
public TableFilter getTableFilter() { |
| 155 |
return columnResolver == null ? null : columnResolver.getTableFilter(); |
| 156 |
} |
| 157 |
|
| 158 |
@Override |
| 159 |
public void mapColumns(ColumnResolver resolver, int level, int state) { |
| 160 |
if (tableAlias != null && !database.equalsIdentifiers(tableAlias, resolver.getTableAlias())) { |
| 161 |
return; |
| 162 |
} |
| 163 |
if (schemaName != null && !database.equalsIdentifiers(schemaName, resolver.getSchemaName())) { |
| 164 |
return; |
| 165 |
} |
| 166 |
if (rowId) { |
| 167 |
Column col = resolver.getRowIdColumn(); |
| 168 |
if (col != null) { |
| 169 |
mapColumn(resolver, col, level); |
| 170 |
} |
| 171 |
return; |
| 172 |
} |
| 173 |
Column col = resolver.findColumn(columnName); |
| 174 |
if (col != null) { |
| 175 |
mapColumn(resolver, col, level); |
| 176 |
return; |
| 177 |
} |
| 178 |
Column[] columns = resolver.getSystemColumns(); |
| 179 |
for (int i = 0; columns != null && i < columns.length; i++) { |
| 180 |
col = columns[i]; |
| 181 |
if (database.equalsIdentifiers(columnName, col.getName())) { |
| 182 |
mapColumn(resolver, col, level); |
| 183 |
return; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
private void mapColumn(ColumnResolver resolver, Column col, int level) { |
| 189 |
if (this.columnResolver == null) { |
| 190 |
queryLevel = level; |
| 191 |
column = col; |
| 192 |
this.columnResolver = resolver; |
| 193 |
} else if (queryLevel == level && this.columnResolver != resolver) { |
| 194 |
if (resolver instanceof SelectListColumnResolver) { |
| 195 |
// ignore - already mapped, that's ok |
| 196 |
} else { |
| 197 |
throw DbException.get(ErrorCode.AMBIGUOUS_COLUMN_NAME_1, columnName); |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
@Override |
| 203 |
public Expression optimize(SessionLocal session) { |
| 204 |
if (columnResolver == null) { |
| 205 |
Schema schema = session.getDatabase().findSchema( |
| 206 |
tableAlias == null ? session.getCurrentSchemaName() : tableAlias); |
| 207 |
if (schema != null) { |
| 208 |
Constant constant = schema.findConstant(columnName); |
| 209 |
if (constant != null) { |
| 210 |
return constant.getValue(); |
| 211 |
} |
| 212 |
} |
| 213 |
return optimizeOther(); |
| 214 |
} |
| 215 |
return columnResolver.optimize(this, column); |
| 216 |
} |
| 217 |
|
| 218 |
private Expression optimizeOther() { |
| 219 |
if (tableAlias == null && !quotedName) { |
| 220 |
Expression e = ModeFunction.getCompatibilityDateTimeValueFunction(database, |
| 221 |
StringUtils.toUpperEnglish(columnName), -1); |
| 222 |
if (e != null) { |
| 223 |
return e; |
| 224 |
} |
| 225 |
} |
| 226 |
throw getColumnException(ErrorCode.COLUMN_NOT_FOUND_1); |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Get exception to throw, with column and table info added |
| 231 |
* |
| 232 |
* @param code SQL error code |
| 233 |
* @return DbException |
| 234 |
*/ |
| 235 |
public DbException getColumnException(int code) { |
| 236 |
String name = columnName; |
| 237 |
if (tableAlias != null) { |
| 238 |
if (schemaName != null) { |
| 239 |
name = schemaName + '.' + tableAlias + '.' + name; |
| 240 |
} else { |
| 241 |
name = tableAlias + '.' + name; |
| 242 |
} |
| 243 |
} |
| 244 |
return DbException.get(code, name); |
| 245 |
} |
| 246 |
|
| 247 |
@Override |
| 248 |
public void updateAggregate(SessionLocal session, int stage) { |
| 249 |
Select select = columnResolver.getSelect(); |
| 250 |
if (select == null) { |
| 251 |
throw DbException.get(ErrorCode.MUST_GROUP_BY_COLUMN_1, getTraceSQL()); |
| 252 |
} |
| 253 |
if (stage == DataAnalysisOperation.STAGE_RESET) { |
| 254 |
return; |
| 255 |
} |
| 256 |
SelectGroups groupData = select.getGroupDataIfCurrent(false); |
| 257 |
if (groupData == null) { |
| 258 |
// this is a different level (the enclosing query) |
| 259 |
return; |
| 260 |
} |
| 261 |
Value v = (Value) groupData.getCurrentGroupExprData(this); |
| 262 |
if (v == null) { |
| 263 |
groupData.setCurrentGroupExprData(this, columnResolver.getValue(column)); |
| 264 |
} else if (!select.isGroupWindowStage2()) { |
| 265 |
if (!session.areEqual(columnResolver.getValue(column), v)) { |
| 266 |
throw DbException.get(ErrorCode.MUST_GROUP_BY_COLUMN_1, getTraceSQL()); |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
@Override |
| 272 |
public Value getValue(SessionLocal session) { |
| 273 |
Select select = columnResolver.getSelect(); |
| 274 |
if (select != null) { |
| 275 |
SelectGroups groupData = select.getGroupDataIfCurrent(false); |
| 276 |
if (groupData != null) { |
| 277 |
Value v = (Value) groupData.getCurrentGroupExprData(this); |
| 278 |
if (v != null) { |
| 279 |
return v; |
| 280 |
} |
| 281 |
if (select.isGroupWindowStage2()) { |
| 282 |
throw DbException.get(ErrorCode.MUST_GROUP_BY_COLUMN_1, getTraceSQL()); |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
Value value = columnResolver.getValue(column); |
| 287 |
if (value == null) { |
| 288 |
if (select == null) { |
| 289 |
throw DbException.get(ErrorCode.NULL_NOT_ALLOWED, getTraceSQL()); |
| 290 |
} else { |
| 291 |
throw DbException.get(ErrorCode.MUST_GROUP_BY_COLUMN_1, getTraceSQL()); |
| 292 |
} |
| 293 |
} |
| 294 |
return value; |
| 295 |
} |
| 296 |
|
| 297 |
@Override |
| 298 |
public TypeInfo getType() { |
| 299 |
return column != null ? column.getType() : rowId ? TypeInfo.TYPE_BIGINT : TypeInfo.TYPE_UNKNOWN; |
| 300 |
} |
| 301 |
|
| 302 |
@Override |
| 303 |
public void setEvaluatable(TableFilter tableFilter, boolean b) { |
| 304 |
} |
| 305 |
|
| 306 |
public Column getColumn() { |
| 307 |
return column; |
| 308 |
} |
| 309 |
|
| 310 |
public String getOriginalColumnName() { |
| 311 |
return columnName; |
| 312 |
} |
| 313 |
|
| 314 |
public String getOriginalTableAliasName() { |
| 315 |
return tableAlias; |
| 316 |
} |
| 317 |
|
| 318 |
@Override |
| 319 |
public String getColumnName(SessionLocal session, int columnIndex) { |
| 320 |
if (column != null) { |
| 321 |
if (columnResolver != null) { |
| 322 |
return columnResolver.getColumnName(column); |
| 323 |
} |
| 324 |
return column.getName(); |
| 325 |
} |
| 326 |
return columnName; |
| 327 |
} |
| 328 |
|
| 329 |
@Override |
| 330 |
public String getSchemaName() { |
| 331 |
Table table = column.getTable(); |
| 332 |
return table == null ? null : table.getSchema().getName(); |
| 333 |
} |
| 334 |
|
| 335 |
@Override |
| 336 |
public String getTableName() { |
| 337 |
Table table = column.getTable(); |
| 338 |
return table == null ? null : table.getName(); |
| 339 |
} |
| 340 |
|
| 341 |
@Override |
| 342 |
public String getAlias(SessionLocal session, int columnIndex) { |
| 343 |
if (column != null) { |
| 344 |
if (columnResolver != null) { |
| 345 |
return columnResolver.getColumnName(column); |
| 346 |
} |
| 347 |
return column.getName(); |
| 348 |
} |
| 349 |
if (tableAlias != null) { |
| 350 |
return tableAlias + '.' + columnName; |
| 351 |
} |
| 352 |
return columnName; |
| 353 |
} |
| 354 |
|
| 355 |
@Override |
| 356 |
public String getColumnNameForView(SessionLocal session, int columnIndex) { |
| 357 |
return getAlias(session, columnIndex); |
| 358 |
} |
| 359 |
|
| 360 |
@Override |
| 361 |
public boolean isIdentity() { |
| 362 |
return column.isIdentity(); |
| 363 |
} |
| 364 |
|
| 365 |
@Override |
| 366 |
public int getNullable() { |
| 367 |
return column.isNullable() ? Column.NULLABLE : Column.NOT_NULLABLE; |
| 368 |
} |
| 369 |
|
| 370 |
@Override |
| 371 |
public boolean isEverything(ExpressionVisitor visitor) { |
| 372 |
switch (visitor.getType()) { |
| 373 |
case ExpressionVisitor.OPTIMIZABLE_AGGREGATE: |
| 374 |
return false; |
| 375 |
case ExpressionVisitor.INDEPENDENT: |
| 376 |
return this.queryLevel < visitor.getQueryLevel(); |
| 377 |
case ExpressionVisitor.EVALUATABLE: |
| 378 |
// if this column belongs to a 'higher level' query and is |
| 379 |
// therefore just a parameter |
| 380 |
if (visitor.getQueryLevel() < this.queryLevel) { |
| 381 |
return true; |
| 382 |
} |
| 383 |
if (getTableFilter() == null) { |
| 384 |
return false; |
| 385 |
} |
| 386 |
return getTableFilter().isEvaluatable(); |
| 387 |
case ExpressionVisitor.SET_MAX_DATA_MODIFICATION_ID: |
| 388 |
visitor.addDataModificationId(column.getTable().getMaxDataModificationId()); |
| 389 |
return true; |
| 390 |
case ExpressionVisitor.NOT_FROM_RESOLVER: |
| 391 |
return columnResolver != visitor.getResolver(); |
| 392 |
case ExpressionVisitor.GET_DEPENDENCIES: |
| 393 |
if (column != null) { |
| 394 |
visitor.addDependency(column.getTable()); |
| 395 |
} |
| 396 |
return true; |
| 397 |
case ExpressionVisitor.GET_COLUMNS1: |
| 398 |
if (column == null) { |
| 399 |
throw DbException.get(ErrorCode.COLUMN_NOT_FOUND_1, getTraceSQL()); |
| 400 |
} |
| 401 |
visitor.addColumn1(column); |
| 402 |
return true; |
| 403 |
case ExpressionVisitor.GET_COLUMNS2: |
| 404 |
if (column == null) { |
| 405 |
throw DbException.get(ErrorCode.COLUMN_NOT_FOUND_1, getTraceSQL()); |
| 406 |
} |
| 407 |
visitor.addColumn2(column); |
| 408 |
return true; |
| 409 |
case ExpressionVisitor.DECREMENT_QUERY_LEVEL: { |
| 410 |
if (column == null) { |
| 411 |
throw DbException.get(ErrorCode.COLUMN_NOT_FOUND_1, getTraceSQL()); |
| 412 |
} |
| 413 |
if (visitor.getColumnResolvers().contains(columnResolver)) { |
| 414 |
int decrement = visitor.getQueryLevel(); |
| 415 |
if (decrement > 0) { |
| 416 |
if (queryLevel > 0) { |
| 417 |
queryLevel--; |
| 418 |
return true; |
| 419 |
} |
| 420 |
throw DbException.getInternalError("queryLevel=0"); |
| 421 |
} |
| 422 |
return queryLevel > 0; |
| 423 |
} |
| 424 |
} |
| 425 |
//$FALL-THROUGH$ |
| 426 |
default: |
| 427 |
return true; |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
@Override |
| 432 |
public int getCost() { |
| 433 |
return 2; |
| 434 |
} |
| 435 |
|
| 436 |
@Override |
| 437 |
public void createIndexConditions(SessionLocal session, TableFilter filter) { |
| 438 |
TableFilter tf = getTableFilter(); |
| 439 |
if (filter == tf && column.getType().getValueType() == Value.BOOLEAN) { |
| 440 |
filter.addIndexCondition(IndexCondition.get(Comparison.EQUAL, this, ValueExpression.TRUE)); |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
@Override |
| 445 |
public Expression getNotIfPossible(SessionLocal session) { |
| 446 |
Expression o = optimize(session); |
| 447 |
if (o != this) { |
| 448 |
return o.getNotIfPossible(session); |
| 449 |
} |
| 450 |
Value v; |
| 451 |
switch (column.getType().getValueType()) { |
| 452 |
case Value.BOOLEAN: |
| 453 |
v = ValueBoolean.FALSE; |
| 454 |
break; |
| 455 |
case Value.TINYINT: |
| 456 |
v = ValueTinyint.get((byte) 0); |
| 457 |
break; |
| 458 |
case Value.SMALLINT: |
| 459 |
v = ValueSmallint.get((short) 0); |
| 460 |
break; |
| 461 |
case Value.INTEGER: |
| 462 |
v = ValueInteger.get(0); |
| 463 |
break; |
| 464 |
case Value.BIGINT: |
| 465 |
v = ValueBigint.get(0L); |
| 466 |
break; |
| 467 |
case Value.NUMERIC: |
| 468 |
v = ValueNumeric.ZERO; |
| 469 |
break; |
| 470 |
case Value.REAL: |
| 471 |
v = ValueReal.ZERO; |
| 472 |
break; |
| 473 |
case Value.DOUBLE: |
| 474 |
v = ValueDouble.ZERO; |
| 475 |
break; |
| 476 |
case Value.DECFLOAT: |
| 477 |
v = ValueDecfloat.ZERO; |
| 478 |
break; |
| 479 |
default: |
| 480 |
/* |
| 481 |
* Can be replaced with CAST(column AS BOOLEAN) = FALSE, but this |
| 482 |
* replacement can't be optimized further, so it's better to leave |
| 483 |
* NOT (column) as is. |
| 484 |
*/ |
| 485 |
return null; |
| 486 |
} |
| 487 |
return new Comparison(Comparison.EQUAL, this, ValueExpression.get(v), false); |
| 488 |
} |
| 489 |
|
| 490 |
} |