| 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.analysis; |
| 7 |
|
| 8 |
import org.h2.engine.SessionLocal; |
| 9 |
import org.h2.expression.Expression; |
| 10 |
import org.h2.table.ColumnResolver; |
| 11 |
|
| 12 |
/** |
| 13 |
* Window frame bound. |
| 14 |
*/ |
| 15 |
public class WindowFrameBound { |
| 16 |
|
| 17 |
private final WindowFrameBoundType type; |
| 18 |
|
| 19 |
private Expression value; |
| 20 |
|
| 21 |
private boolean isVariable; |
| 22 |
|
| 23 |
private int expressionIndex = -1; |
| 24 |
|
| 25 |
/** |
| 26 |
* Creates new instance of window frame bound. |
| 27 |
* |
| 28 |
* @param type |
| 29 |
* bound type |
| 30 |
* @param value |
| 31 |
* bound value, if any |
| 32 |
*/ |
| 33 |
public WindowFrameBound(WindowFrameBoundType type, Expression value) { |
| 34 |
this.type = type; |
| 35 |
if (type == WindowFrameBoundType.PRECEDING || type == WindowFrameBoundType.FOLLOWING) { |
| 36 |
this.value = value; |
| 37 |
} else { |
| 38 |
this.value = null; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Returns the type |
| 44 |
* |
| 45 |
* @return the type |
| 46 |
*/ |
| 47 |
public WindowFrameBoundType getType() { |
| 48 |
return type; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Returns the value. |
| 53 |
* |
| 54 |
* @return the value |
| 55 |
*/ |
| 56 |
public Expression getValue() { |
| 57 |
return value; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Returns whether bound is defined as n PRECEDING or n FOLLOWING. |
| 62 |
* |
| 63 |
* @return whether bound is defined as n PRECEDING or n FOLLOWING |
| 64 |
*/ |
| 65 |
public boolean isParameterized() { |
| 66 |
return type == WindowFrameBoundType.PRECEDING || type == WindowFrameBoundType.FOLLOWING; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Returns whether bound is defined with a variable. This method may be used |
| 71 |
* only after {@link #optimize(SessionLocal)} invocation. |
| 72 |
* |
| 73 |
* @return whether bound is defined with a variable |
| 74 |
*/ |
| 75 |
public boolean isVariable() { |
| 76 |
return isVariable; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Returns the index of preserved expression. |
| 81 |
* |
| 82 |
* @return the index of preserved expression, or -1 |
| 83 |
*/ |
| 84 |
public int getExpressionIndex() { |
| 85 |
return expressionIndex; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Sets the index of preserved expression. |
| 90 |
* |
| 91 |
* @param expressionIndex |
| 92 |
* the index to set |
| 93 |
*/ |
| 94 |
void setExpressionIndex(int expressionIndex) { |
| 95 |
this.expressionIndex = expressionIndex; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Map the columns of the resolver to expression columns. |
| 100 |
* |
| 101 |
* @param resolver |
| 102 |
* the column resolver |
| 103 |
* @param level |
| 104 |
* the subquery nesting level |
| 105 |
* @param state |
| 106 |
* current state for nesting checks |
| 107 |
*/ |
| 108 |
void mapColumns(ColumnResolver resolver, int level, int state) { |
| 109 |
if (value != null) { |
| 110 |
value.mapColumns(resolver, level, state); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Try to optimize bound expression. |
| 116 |
* |
| 117 |
* @param session |
| 118 |
* the session |
| 119 |
*/ |
| 120 |
void optimize(SessionLocal session) { |
| 121 |
if (value != null) { |
| 122 |
value = value.optimize(session); |
| 123 |
if (!value.isConstant()) { |
| 124 |
isVariable = true; |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Update an aggregate value. |
| 131 |
* |
| 132 |
* @param session |
| 133 |
* the session |
| 134 |
* @param stage |
| 135 |
* select stage |
| 136 |
* @see Expression#updateAggregate(SessionLocal, int) |
| 137 |
*/ |
| 138 |
void updateAggregate(SessionLocal session, int stage) { |
| 139 |
if (value != null) { |
| 140 |
value.updateAggregate(session, stage); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Appends SQL representation to the specified builder. |
| 146 |
* |
| 147 |
* @param builder |
| 148 |
* string builder |
| 149 |
* @param following |
| 150 |
* if false return SQL for starting clause, if true return SQL |
| 151 |
* for following clause |
| 152 |
* @param sqlFlags |
| 153 |
* formatting flags |
| 154 |
* @return the specified string builder |
| 155 |
* @see Expression#getSQL(StringBuilder, int, int) |
| 156 |
*/ |
| 157 |
public StringBuilder getSQL(StringBuilder builder, boolean following, int sqlFlags) { |
| 158 |
if (type == WindowFrameBoundType.PRECEDING || type == WindowFrameBoundType.FOLLOWING) { |
| 159 |
value.getUnenclosedSQL(builder, sqlFlags).append(' '); |
| 160 |
} |
| 161 |
return builder.append(type.getSQL()); |
| 162 |
} |
| 163 |
|
| 164 |
} |