| 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 java.util.ArrayList; |
| 9 |
import java.util.Iterator; |
| 10 |
import java.util.ListIterator; |
| 11 |
|
| 12 |
import org.h2.api.ErrorCode; |
| 13 |
import org.h2.command.query.QueryOrderBy; |
| 14 |
import org.h2.command.query.Select; |
| 15 |
import org.h2.engine.SessionLocal; |
| 16 |
import org.h2.expression.Expression; |
| 17 |
import org.h2.message.DbException; |
| 18 |
import org.h2.result.SortOrder; |
| 19 |
import org.h2.table.ColumnResolver; |
| 20 |
import org.h2.table.TableFilter; |
| 21 |
import org.h2.util.HasSQL; |
| 22 |
import org.h2.value.Value; |
| 23 |
import org.h2.value.ValueRow; |
| 24 |
|
| 25 |
/** |
| 26 |
* Window clause. |
| 27 |
*/ |
| 28 |
public final class Window { |
| 29 |
|
| 30 |
private ArrayList<Expression> partitionBy; |
| 31 |
|
| 32 |
private ArrayList<QueryOrderBy> orderBy; |
| 33 |
|
| 34 |
private WindowFrame frame; |
| 35 |
|
| 36 |
private String parent; |
| 37 |
|
| 38 |
/** |
| 39 |
* Appends ORDER BY clause to the specified builder. |
| 40 |
* |
| 41 |
* @param builder |
| 42 |
* string builder |
| 43 |
* @param orderBy |
| 44 |
* ORDER BY clause, or null |
| 45 |
* @param sqlFlags |
| 46 |
* formatting flags |
| 47 |
* @param forceOrderBy |
| 48 |
* whether synthetic ORDER BY clause should be generated when it |
| 49 |
* is missing |
| 50 |
*/ |
| 51 |
public static void appendOrderBy(StringBuilder builder, ArrayList<QueryOrderBy> orderBy, int sqlFlags, |
| 52 |
boolean forceOrderBy) { |
| 53 |
if (orderBy != null && !orderBy.isEmpty()) { |
| 54 |
appendOrderByStart(builder); |
| 55 |
for (int i = 0; i < orderBy.size(); i++) { |
| 56 |
QueryOrderBy o = orderBy.get(i); |
| 57 |
if (i > 0) { |
| 58 |
builder.append(", "); |
| 59 |
} |
| 60 |
o.expression.getUnenclosedSQL(builder, sqlFlags); |
| 61 |
SortOrder.typeToString(builder, o.sortType); |
| 62 |
} |
| 63 |
} else if (forceOrderBy) { |
| 64 |
appendOrderByStart(builder); |
| 65 |
builder.append("NULL"); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
private static void appendOrderByStart(StringBuilder builder) { |
| 70 |
if (builder.charAt(builder.length() - 1) != '(') { |
| 71 |
builder.append(' '); |
| 72 |
} |
| 73 |
builder.append("ORDER BY "); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Creates a new instance of window clause. |
| 78 |
* |
| 79 |
* @param parent |
| 80 |
* name of the parent window |
| 81 |
* @param partitionBy |
| 82 |
* PARTITION BY clause, or null |
| 83 |
* @param orderBy |
| 84 |
* ORDER BY clause, or null |
| 85 |
* @param frame |
| 86 |
* window frame clause, or null |
| 87 |
*/ |
| 88 |
public Window(String parent, ArrayList<Expression> partitionBy, ArrayList<QueryOrderBy> orderBy, |
| 89 |
WindowFrame frame) { |
| 90 |
this.parent = parent; |
| 91 |
this.partitionBy = partitionBy; |
| 92 |
this.orderBy = orderBy; |
| 93 |
this.frame = frame; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Map the columns of the resolver to expression columns. |
| 98 |
* |
| 99 |
* @param resolver |
| 100 |
* the column resolver |
| 101 |
* @param level |
| 102 |
* the subquery nesting level |
| 103 |
* @see Expression#mapColumns(ColumnResolver, int, int) |
| 104 |
*/ |
| 105 |
public void mapColumns(ColumnResolver resolver, int level) { |
| 106 |
resolveWindows(resolver); |
| 107 |
if (partitionBy != null) { |
| 108 |
for (Expression e : partitionBy) { |
| 109 |
e.mapColumns(resolver, level, Expression.MAP_IN_WINDOW); |
| 110 |
} |
| 111 |
} |
| 112 |
if (orderBy != null) { |
| 113 |
for (QueryOrderBy o : orderBy) { |
| 114 |
o.expression.mapColumns(resolver, level, Expression.MAP_IN_WINDOW); |
| 115 |
} |
| 116 |
} |
| 117 |
if (frame != null) { |
| 118 |
frame.mapColumns(resolver, level, Expression.MAP_IN_WINDOW); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
private void resolveWindows(ColumnResolver resolver) { |
| 123 |
if (parent != null) { |
| 124 |
Select select = resolver.getSelect(); |
| 125 |
Window p; |
| 126 |
while ((p = select.getWindow(parent)) == null) { |
| 127 |
select = select.getParentSelect(); |
| 128 |
if (select == null) { |
| 129 |
throw DbException.get(ErrorCode.WINDOW_NOT_FOUND_1, parent); |
| 130 |
} |
| 131 |
} |
| 132 |
p.resolveWindows(resolver); |
| 133 |
if (partitionBy == null) { |
| 134 |
partitionBy = p.partitionBy; |
| 135 |
} |
| 136 |
if (orderBy == null) { |
| 137 |
orderBy = p.orderBy; |
| 138 |
} |
| 139 |
if (frame == null) { |
| 140 |
frame = p.frame; |
| 141 |
} |
| 142 |
parent = null; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Try to optimize the window conditions. |
| 148 |
* |
| 149 |
* @param session |
| 150 |
* the session |
| 151 |
*/ |
| 152 |
public void optimize(SessionLocal session) { |
| 153 |
if (partitionBy != null) { |
| 154 |
for (ListIterator<Expression> i = partitionBy.listIterator(); i.hasNext();) { |
| 155 |
Expression e = i.next().optimize(session); |
| 156 |
if (e.isConstant()) { |
| 157 |
i.remove(); |
| 158 |
} else { |
| 159 |
i.set(e); |
| 160 |
} |
| 161 |
} |
| 162 |
if (partitionBy.isEmpty()) { |
| 163 |
partitionBy = null; |
| 164 |
} |
| 165 |
} |
| 166 |
if (orderBy != null) { |
| 167 |
for (Iterator<QueryOrderBy> i = orderBy.iterator(); i.hasNext();) { |
| 168 |
QueryOrderBy o = i.next(); |
| 169 |
Expression e = o.expression.optimize(session); |
| 170 |
if (e.isConstant()) { |
| 171 |
i.remove(); |
| 172 |
} else { |
| 173 |
o.expression = e; |
| 174 |
} |
| 175 |
} |
| 176 |
if (orderBy.isEmpty()) { |
| 177 |
orderBy = null; |
| 178 |
} |
| 179 |
} |
| 180 |
if (frame != null) { |
| 181 |
frame.optimize(session); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Tell the expression columns whether the table filter can return values |
| 187 |
* now. This is used when optimizing the query. |
| 188 |
* |
| 189 |
* @param tableFilter |
| 190 |
* the table filter |
| 191 |
* @param value |
| 192 |
* true if the table filter can return value |
| 193 |
* @see Expression#setEvaluatable(TableFilter, boolean) |
| 194 |
*/ |
| 195 |
public void setEvaluatable(TableFilter tableFilter, boolean value) { |
| 196 |
if (partitionBy != null) { |
| 197 |
for (Expression e : partitionBy) { |
| 198 |
e.setEvaluatable(tableFilter, value); |
| 199 |
} |
| 200 |
} |
| 201 |
if (orderBy != null) { |
| 202 |
for (QueryOrderBy o : orderBy) { |
| 203 |
o.expression.setEvaluatable(tableFilter, value); |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Returns ORDER BY clause. |
| 210 |
* |
| 211 |
* @return ORDER BY clause, or null |
| 212 |
*/ |
| 213 |
public ArrayList<QueryOrderBy> getOrderBy() { |
| 214 |
return orderBy; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Returns window frame, or null. |
| 219 |
* |
| 220 |
* @return window frame, or null |
| 221 |
*/ |
| 222 |
public WindowFrame getWindowFrame() { |
| 223 |
return frame; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Returns {@code true} if window ordering clause is specified or ROWS unit |
| 228 |
* is used. |
| 229 |
* |
| 230 |
* @return {@code true} if window ordering clause is specified or ROWS unit |
| 231 |
* is used |
| 232 |
*/ |
| 233 |
public boolean isOrdered() { |
| 234 |
if (orderBy != null) { |
| 235 |
return true; |
| 236 |
} |
| 237 |
if (frame != null && frame.getUnits() == WindowFrameUnits.ROWS) { |
| 238 |
if (frame.getStarting().getType() == WindowFrameBoundType.UNBOUNDED_PRECEDING) { |
| 239 |
WindowFrameBound following = frame.getFollowing(); |
| 240 |
if (following != null && following.getType() == WindowFrameBoundType.UNBOUNDED_FOLLOWING) { |
| 241 |
return false; |
| 242 |
} |
| 243 |
} |
| 244 |
return true; |
| 245 |
} |
| 246 |
return false; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Returns the key for the current group. |
| 251 |
* |
| 252 |
* @param session |
| 253 |
* session |
| 254 |
* @return key for the current group, or null |
| 255 |
*/ |
| 256 |
public Value getCurrentKey(SessionLocal session) { |
| 257 |
if (partitionBy == null) { |
| 258 |
return null; |
| 259 |
} |
| 260 |
int len = partitionBy.size(); |
| 261 |
if (len == 1) { |
| 262 |
return partitionBy.get(0).getValue(session); |
| 263 |
} else { |
| 264 |
Value[] keyValues = new Value[len]; |
| 265 |
// update group |
| 266 |
for (int i = 0; i < len; i++) { |
| 267 |
Expression expr = partitionBy.get(i); |
| 268 |
keyValues[i] = expr.getValue(session); |
| 269 |
} |
| 270 |
return ValueRow.get(keyValues); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Appends SQL representation to the specified builder. |
| 276 |
* |
| 277 |
* @param builder |
| 278 |
* string builder |
| 279 |
* @param sqlFlags |
| 280 |
* formatting flags |
| 281 |
* @param forceOrderBy |
| 282 |
* whether synthetic ORDER BY clause should be generated when it |
| 283 |
* is missing |
| 284 |
* @return the specified string builder |
| 285 |
* @see Expression#getSQL(StringBuilder, int, int) |
| 286 |
*/ |
| 287 |
public StringBuilder getSQL(StringBuilder builder, int sqlFlags, boolean forceOrderBy) { |
| 288 |
builder.append("OVER ("); |
| 289 |
if (partitionBy != null) { |
| 290 |
builder.append("PARTITION BY "); |
| 291 |
for (int i = 0; i < partitionBy.size(); i++) { |
| 292 |
if (i > 0) { |
| 293 |
builder.append(", "); |
| 294 |
} |
| 295 |
partitionBy.get(i).getUnenclosedSQL(builder, sqlFlags); |
| 296 |
} |
| 297 |
} |
| 298 |
appendOrderBy(builder, orderBy, sqlFlags, forceOrderBy); |
| 299 |
if (frame != null) { |
| 300 |
if (builder.charAt(builder.length() - 1) != '(') { |
| 301 |
builder.append(' '); |
| 302 |
} |
| 303 |
frame.getSQL(builder, sqlFlags); |
| 304 |
} |
| 305 |
return builder.append(')'); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Update an aggregate value. |
| 310 |
* |
| 311 |
* @param session |
| 312 |
* the session |
| 313 |
* @param stage |
| 314 |
* select stage |
| 315 |
* @see Expression#updateAggregate(SessionLocal, int) |
| 316 |
*/ |
| 317 |
public void updateAggregate(SessionLocal session, int stage) { |
| 318 |
if (partitionBy != null) { |
| 319 |
for (Expression expr : partitionBy) { |
| 320 |
expr.updateAggregate(session, stage); |
| 321 |
} |
| 322 |
} |
| 323 |
if (orderBy != null) { |
| 324 |
for (QueryOrderBy o : orderBy) { |
| 325 |
o.expression.updateAggregate(session, stage); |
| 326 |
} |
| 327 |
} |
| 328 |
if (frame != null) { |
| 329 |
frame.updateAggregate(session, stage); |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
@Override |
| 334 |
public String toString() { |
| 335 |
return getSQL(new StringBuilder(), HasSQL.TRACE_SQL_FLAGS, false).toString(); |
| 336 |
} |
| 337 |
|
| 338 |
} |