forked from h2database/h2database
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WindowFrame.java
877 lines (806 loc) · 30.5 KB
/
WindowFrame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
/*
* Copyright 2004-2021 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (https://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.expression.analysis;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.NoSuchElementException;
import org.h2.api.ErrorCode;
import org.h2.engine.SessionLocal;
import org.h2.expression.BinaryOperation;
import org.h2.expression.BinaryOperation.OpType;
import org.h2.expression.Expression;
import org.h2.expression.ValueExpression;
import org.h2.message.DbException;
import org.h2.result.SortOrder;
import org.h2.table.ColumnResolver;
import org.h2.value.Value;
import org.h2.value.ValueNull;
/**
* Window frame clause.
*/
public final class WindowFrame {
private abstract static class Itr implements Iterator<Value[]> {
final ArrayList<Value[]> orderedRows;
int cursor;
Itr(ArrayList<Value[]> orderedRows) {
this.orderedRows = orderedRows;
}
}
private static class PlainItr extends Itr {
final int endIndex;
PlainItr(ArrayList<Value[]> orderedRows, int startIndex, int endIndex) {
super(orderedRows);
this.endIndex = endIndex;
cursor = startIndex;
}
@Override
public boolean hasNext() {
return cursor <= endIndex;
}
@Override
public Value[] next() {
if (cursor > endIndex) {
throw new NoSuchElementException();
}
return orderedRows.get(cursor++);
}
}
private static class PlainReverseItr extends Itr {
final int startIndex;
PlainReverseItr(ArrayList<Value[]> orderedRows, int startIndex, int endIndex) {
super(orderedRows);
this.startIndex = startIndex;
cursor = endIndex;
}
@Override
public boolean hasNext() {
return cursor >= startIndex;
}
@Override
public Value[] next() {
if (cursor < startIndex) {
throw new NoSuchElementException();
}
return orderedRows.get(cursor--);
}
}
private static class BiItr extends PlainItr {
final int end1, start1;
BiItr(ArrayList<Value[]> orderedRows, int startIndex1, int endIndex1, int startIndex2, int endIndex2) {
super(orderedRows, startIndex1, endIndex2);
end1 = endIndex1;
start1 = startIndex2;
}
@Override
public Value[] next() {
if (cursor > endIndex) {
throw new NoSuchElementException();
}
Value[] r = orderedRows.get(cursor);
cursor = cursor != end1 ? cursor + 1 : start1;
return r;
}
}
private static class BiReverseItr extends PlainReverseItr {
final int end1, start1;
BiReverseItr(ArrayList<Value[]> orderedRows, int startIndex1, int endIndex1, int startIndex2, int endIndex2) {
super(orderedRows, startIndex1, endIndex2);
end1 = endIndex1;
start1 = startIndex2;
}
@Override
public Value[] next() {
if (cursor < startIndex) {
throw new NoSuchElementException();
}
Value[] r = orderedRows.get(cursor);
cursor = cursor != start1 ? cursor - 1 : end1;
return r;
}
}
private static final class TriItr extends BiItr {
private final int end2, start2;
TriItr(ArrayList<Value[]> orderedRows, int startIndex1, int endIndex1, int startIndex2, int endIndex2,
int startIndex3, int endIndex3) {
super(orderedRows, startIndex1, endIndex1, startIndex2, endIndex3);
end2 = endIndex2;
start2 = startIndex3;
}
@Override
public Value[] next() {
if (cursor > endIndex) {
throw new NoSuchElementException();
}
Value[] r = orderedRows.get(cursor);
cursor = cursor != end1 ? cursor != end2 ? cursor + 1 : start2 : start1;
return r;
}
}
private static final class TriReverseItr extends BiReverseItr {
private final int end2, start2;
TriReverseItr(ArrayList<Value[]> orderedRows, int startIndex1, int endIndex1, int startIndex2, int endIndex2,
int startIndex3, int endIndex3) {
super(orderedRows, startIndex1, endIndex1, startIndex2, endIndex3);
end2 = endIndex2;
start2 = startIndex3;
}
@Override
public Value[] next() {
if (cursor < startIndex) {
throw new NoSuchElementException();
}
Value[] r = orderedRows.get(cursor);
cursor = cursor != start1 ? cursor != start2 ? cursor - 1 : end2 : end1;
return r;
}
}
private final WindowFrameUnits units;
private final WindowFrameBound starting;
private final WindowFrameBound following;
private final WindowFrameExclusion exclusion;
/**
* Returns iterator for the specified frame, or default iterator if frame is
* null.
*
* @param over
* window
* @param session
* the session
* @param orderedRows
* ordered rows
* @param sortOrder
* sort order
* @param currentRow
* index of the current row
* @param reverse
* whether iterator should iterate in reverse order
* @return iterator
*/
public static Iterator<Value[]> iterator(Window over, SessionLocal session, ArrayList<Value[]> orderedRows,
SortOrder sortOrder, int currentRow, boolean reverse) {
WindowFrame frame = over.getWindowFrame();
if (frame != null) {
return frame.iterator(session, orderedRows, sortOrder, currentRow, reverse);
}
int endIndex = orderedRows.size() - 1;
return plainIterator(orderedRows, 0,
over.getOrderBy() == null ? endIndex : toGroupEnd(orderedRows, sortOrder, currentRow, endIndex),
reverse);
}
/**
* Returns end index for the specified frame, or default end index if frame
* is null.
*
* @param over
* window
* @param session
* the session
* @param orderedRows
* ordered rows
* @param sortOrder
* sort order
* @param currentRow
* index of the current row
* @return end index
* @throws UnsupportedOperationException
* if over is not null and its exclusion clause is not EXCLUDE
* NO OTHERS
*/
public static int getEndIndex(Window over, SessionLocal session, ArrayList<Value[]> orderedRows,
SortOrder sortOrder, int currentRow) {
WindowFrame frame = over.getWindowFrame();
if (frame != null) {
return frame.getEndIndex(session, orderedRows, sortOrder, currentRow);
}
int endIndex = orderedRows.size() - 1;
return over.getOrderBy() == null ? endIndex : toGroupEnd(orderedRows, sortOrder, currentRow, endIndex);
}
private static Iterator<Value[]> plainIterator(ArrayList<Value[]> orderedRows, int startIndex, int endIndex,
boolean reverse) {
if (endIndex < startIndex) {
return Collections.emptyIterator();
}
return reverse ? new PlainReverseItr(orderedRows, startIndex, endIndex)
: new PlainItr(orderedRows, startIndex, endIndex);
}
private static Iterator<Value[]> biIterator(ArrayList<Value[]> orderedRows, int startIndex1, int endIndex1,
int startIndex2, int endIndex2, boolean reverse) {
return reverse ? new BiReverseItr(orderedRows, startIndex1, endIndex1, startIndex2, endIndex2)
: new BiItr(orderedRows, startIndex1, endIndex1, startIndex2, endIndex2);
}
private static Iterator<Value[]> triIterator(ArrayList<Value[]> orderedRows, int startIndex1, int endIndex1,
int startIndex2, int endIndex2, int startIndex3, int endIndex3, boolean reverse) {
return reverse ? new TriReverseItr(orderedRows, startIndex1, endIndex1, startIndex2, endIndex2, //
startIndex3, endIndex3)
: new TriItr(orderedRows, startIndex1, endIndex1, startIndex2, endIndex2, startIndex3, endIndex3);
}
private static int toGroupStart(ArrayList<Value[]> orderedRows, SortOrder sortOrder, int offset, int minOffset) {
Value[] row = orderedRows.get(offset);
while (offset > minOffset && sortOrder.compare(row, orderedRows.get(offset - 1)) == 0) {
offset--;
}
return offset;
}
private static int toGroupEnd(ArrayList<Value[]> orderedRows, SortOrder sortOrder, int offset, int maxOffset) {
Value[] row = orderedRows.get(offset);
while (offset < maxOffset && sortOrder.compare(row, orderedRows.get(offset + 1)) == 0) {
offset++;
}
return offset;
}
private static int getIntOffset(WindowFrameBound bound, Value[] values, SessionLocal session) {
Value v = bound.isVariable() ? values[bound.getExpressionIndex()] : bound.getValue().getValue(session);
int value;
if (v == ValueNull.INSTANCE || (value = v.getInt()) < 0) {
throw DbException.get(ErrorCode.INVALID_PRECEDING_OR_FOLLOWING_1, v.getTraceSQL());
}
return value;
}
/**
* Appends bound value to the current row and produces row for comparison
* operations.
*
* @param session
* the session
* @param orderedRows
* rows in partition
* @param sortOrder
* the sort order
* @param currentRow
* index of the current row
* @param bound
* window frame bound
* @param add
* false for PRECEDING, true for FOLLOWING
* @return row for comparison operations, or null if result is out of range
* and should be treated as UNLIMITED
*/
private static Value[] getCompareRow(SessionLocal session, ArrayList<Value[]> orderedRows, SortOrder sortOrder,
int currentRow, WindowFrameBound bound, boolean add) {
int sortIndex = sortOrder.getQueryColumnIndexes()[0];
Value[] row = orderedRows.get(currentRow);
Value currentValue = row[sortIndex];
int type = currentValue.getValueType();
Value newValue;
Value range = getValueOffset(bound, orderedRows.get(currentRow), session);
switch (type) {
case Value.NULL:
newValue = ValueNull.INSTANCE;
break;
case Value.TINYINT:
case Value.SMALLINT:
case Value.INTEGER:
case Value.BIGINT:
case Value.NUMERIC:
case Value.REAL:
case Value.DOUBLE:
case Value.DECFLOAT:
case Value.TIME:
case Value.TIME_TZ:
case Value.DATE:
case Value.TIMESTAMP:
case Value.TIMESTAMP_TZ:
case Value.INTERVAL_YEAR:
case Value.INTERVAL_MONTH:
case Value.INTERVAL_DAY:
case Value.INTERVAL_HOUR:
case Value.INTERVAL_MINUTE:
case Value.INTERVAL_SECOND:
case Value.INTERVAL_YEAR_TO_MONTH:
case Value.INTERVAL_DAY_TO_HOUR:
case Value.INTERVAL_DAY_TO_MINUTE:
case Value.INTERVAL_DAY_TO_SECOND:
case Value.INTERVAL_HOUR_TO_MINUTE:
case Value.INTERVAL_HOUR_TO_SECOND:
case Value.INTERVAL_MINUTE_TO_SECOND:
OpType opType = add ^ (sortOrder.getSortTypes()[0] & SortOrder.DESCENDING) != 0 ? OpType.PLUS
: OpType.MINUS;
try {
newValue = new BinaryOperation(opType, ValueExpression.get(currentValue), ValueExpression.get(range))
.optimize(session).getValue(session).convertTo(type);
} catch (DbException ex) {
switch (ex.getErrorCode()) {
case ErrorCode.NUMERIC_VALUE_OUT_OF_RANGE_1:
case ErrorCode.NUMERIC_VALUE_OUT_OF_RANGE_2:
return null;
}
throw ex;
}
break;
default:
throw DbException.getInvalidValueException("unsupported type of sort key for RANGE units",
currentValue.getTraceSQL());
}
Value[] newRow = row.clone();
newRow[sortIndex] = newValue;
return newRow;
}
private static Value getValueOffset(WindowFrameBound bound, Value[] values, SessionLocal session) {
Value value = bound.isVariable() ? values[bound.getExpressionIndex()] : bound.getValue().getValue(session);
if (value == ValueNull.INSTANCE || value.getSignum() < 0) {
throw DbException.get(ErrorCode.INVALID_PRECEDING_OR_FOLLOWING_1, value.getTraceSQL());
}
return value;
}
/**
* Creates new instance of window frame clause.
*
* @param units
* units
* @param starting
* starting clause
* @param following
* following clause
* @param exclusion
* exclusion clause
*/
public WindowFrame(WindowFrameUnits units, WindowFrameBound starting, WindowFrameBound following,
WindowFrameExclusion exclusion) {
this.units = units;
this.starting = starting;
if (following != null && following.getType() == WindowFrameBoundType.CURRENT_ROW) {
following = null;
}
this.following = following;
this.exclusion = exclusion;
}
/**
* Returns the units.
*
* @return the units
*/
public WindowFrameUnits getUnits() {
return units;
}
/**
* Returns the starting clause.
*
* @return the starting clause
*/
public WindowFrameBound getStarting() {
return starting;
}
/**
* Returns the following clause.
*
* @return the following clause, or null
*/
public WindowFrameBound getFollowing() {
return following;
}
/**
* Returns the exclusion clause.
*
* @return the exclusion clause
*/
public WindowFrameExclusion getExclusion() {
return exclusion;
}
/**
* Checks validity of this frame.
*
* @return whether bounds of this frame valid
*/
public boolean isValid() {
WindowFrameBoundType s = starting.getType(),
f = following != null ? following.getType() : WindowFrameBoundType.CURRENT_ROW;
return s != WindowFrameBoundType.UNBOUNDED_FOLLOWING && f != WindowFrameBoundType.UNBOUNDED_PRECEDING
&& s.compareTo(f) <= 0;
}
/**
* Check if bounds of this frame has variable expressions. This method may
* be used only after {@link #optimize(SessionLocal)} invocation.
*
* @return if bounds of this frame has variable expressions
*/
public boolean isVariableBounds() {
if (starting.isVariable()) {
return true;
}
if (following != null && following.isVariable()) {
return true;
}
return false;
}
/**
* Map the columns of the resolver to expression columns.
*
* @param resolver
* the column resolver
* @param level
* the subquery nesting level
* @param state
* current state for nesting checks
*/
void mapColumns(ColumnResolver resolver, int level, int state) {
starting.mapColumns(resolver, level, state);
if (following != null) {
following.mapColumns(resolver, level, state);
}
}
/**
* Try to optimize bound expressions.
*
* @param session
* the session
*/
void optimize(SessionLocal session) {
starting.optimize(session);
if (following != null) {
following.optimize(session);
}
}
/**
* Update an aggregate value.
*
* @param session
* the session
* @param stage
* select stage
* @see Expression#updateAggregate(SessionLocal, int)
*/
void updateAggregate(SessionLocal session, int stage) {
starting.updateAggregate(session, stage);
if (following != null) {
following.updateAggregate(session, stage);
}
}
/**
* Returns iterator.
*
* @param session
* the session
* @param orderedRows
* ordered rows
* @param sortOrder
* sort order
* @param currentRow
* index of the current row
* @param reverse
* whether iterator should iterate in reverse order
* @return iterator
*/
public Iterator<Value[]> iterator(SessionLocal session, ArrayList<Value[]> orderedRows, SortOrder sortOrder,
int currentRow, boolean reverse) {
int startIndex = getIndex(session, orderedRows, sortOrder, currentRow, starting, false);
int endIndex = following != null ? getIndex(session, orderedRows, sortOrder, currentRow, following, true)
: units == WindowFrameUnits.ROWS ? currentRow
: toGroupEnd(orderedRows, sortOrder, currentRow, orderedRows.size() - 1);
if (endIndex < startIndex) {
return Collections.emptyIterator();
}
int size = orderedRows.size();
if (startIndex >= size || endIndex < 0) {
return Collections.emptyIterator();
}
if (startIndex < 0) {
startIndex = 0;
}
if (endIndex >= size) {
endIndex = size - 1;
}
return exclusion != WindowFrameExclusion.EXCLUDE_NO_OTHERS
? complexIterator(orderedRows, sortOrder, currentRow, startIndex, endIndex, reverse)
: plainIterator(orderedRows, startIndex, endIndex, reverse);
}
/**
* Returns start index of this frame,
*
* @param session
* the session
* @param orderedRows
* ordered rows
* @param sortOrder
* sort order
* @param currentRow
* index of the current row
* @return start index
* @throws UnsupportedOperationException
* if exclusion clause is not EXCLUDE NO OTHERS
*/
public int getStartIndex(SessionLocal session, ArrayList<Value[]> orderedRows, SortOrder sortOrder, //
int currentRow) {
if (exclusion != WindowFrameExclusion.EXCLUDE_NO_OTHERS) {
throw new UnsupportedOperationException();
}
int startIndex = getIndex(session, orderedRows, sortOrder, currentRow, starting, false);
if (startIndex < 0) {
startIndex = 0;
}
return startIndex;
}
/**
* Returns end index of this frame,
*
* @param session
* the session
* @param orderedRows
* ordered rows
* @param sortOrder
* sort order
* @param currentRow
* index of the current row
* @return end index
* @throws UnsupportedOperationException
* if exclusion clause is not EXCLUDE NO OTHERS
*/
private int getEndIndex(SessionLocal session, ArrayList<Value[]> orderedRows, SortOrder sortOrder, //
int currentRow) {
if (exclusion != WindowFrameExclusion.EXCLUDE_NO_OTHERS) {
throw new UnsupportedOperationException();
}
int endIndex = following != null ? getIndex(session, orderedRows, sortOrder, currentRow, following, true)
: units == WindowFrameUnits.ROWS ? currentRow
: toGroupEnd(orderedRows, sortOrder, currentRow, orderedRows.size() - 1);
int size = orderedRows.size();
if (endIndex >= size) {
endIndex = size - 1;
}
return endIndex;
}
/**
* Returns starting or ending index of a window frame.
*
* @param session
* the session
* @param orderedRows
* rows in partition
* @param sortOrder
* the sort order
* @param currentRow
* index of the current row
* @param bound
* window frame bound
* @param forFollowing
* false for start index, true for end index
* @return starting or ending index of a window frame (inclusive), can be 0
* or be equal to the number of rows if frame is not limited from
* that side
*/
private int getIndex(SessionLocal session, ArrayList<Value[]> orderedRows, SortOrder sortOrder, int currentRow,
WindowFrameBound bound, boolean forFollowing) {
int size = orderedRows.size();
int last = size - 1;
int index;
switch (bound.getType()) {
case UNBOUNDED_PRECEDING:
index = -1;
break;
case PRECEDING:
switch (units) {
case ROWS: {
int value = getIntOffset(bound, orderedRows.get(currentRow), session);
index = value > currentRow ? -1 : currentRow - value;
break;
}
case GROUPS: {
int value = getIntOffset(bound, orderedRows.get(currentRow), session);
if (!forFollowing) {
index = toGroupStart(orderedRows, sortOrder, currentRow, 0);
while (value > 0 && index > 0) {
value--;
index = toGroupStart(orderedRows, sortOrder, index - 1, 0);
}
if (value > 0) {
index = -1;
}
} else {
if (value == 0) {
index = toGroupEnd(orderedRows, sortOrder, currentRow, last);
} else {
index = currentRow;
while (value > 0 && index >= 0) {
value--;
index = toGroupStart(orderedRows, sortOrder, index, 0) - 1;
}
}
}
break;
}
case RANGE: {
index = currentRow;
Value[] row = getCompareRow(session, orderedRows, sortOrder, index, bound, false);
if (row != null) {
index = Collections.binarySearch(orderedRows, row, sortOrder);
if (index >= 0) {
if (!forFollowing) {
while (index > 0 && sortOrder.compare(row, orderedRows.get(index - 1)) == 0) {
index--;
}
} else {
while (index < last && sortOrder.compare(row, orderedRows.get(index + 1)) == 0) {
index++;
}
}
} else {
index = ~index;
if (!forFollowing) {
if (index == 0) {
index = -1;
}
} else {
index--;
}
}
} else {
index = -1;
}
break;
}
default:
throw DbException.getUnsupportedException("units=" + units);
}
break;
case CURRENT_ROW:
switch (units) {
case ROWS:
index = currentRow;
break;
case GROUPS:
case RANGE:
index = forFollowing ? toGroupEnd(orderedRows, sortOrder, currentRow, last)
: toGroupStart(orderedRows, sortOrder, currentRow, 0);
break;
default:
throw DbException.getUnsupportedException("units=" + units);
}
break;
case FOLLOWING:
switch (units) {
case ROWS: {
int value = getIntOffset(bound, orderedRows.get(currentRow), session);
int rem = last - currentRow;
index = value > rem ? size : currentRow + value;
break;
}
case GROUPS: {
int value = getIntOffset(bound, orderedRows.get(currentRow), session);
if (forFollowing) {
index = toGroupEnd(orderedRows, sortOrder, currentRow, last);
while (value > 0 && index < last) {
value--;
index = toGroupEnd(orderedRows, sortOrder, index + 1, last);
}
if (value > 0) {
index = size;
}
} else {
if (value == 0) {
index = toGroupStart(orderedRows, sortOrder, currentRow, 0);
} else {
index = currentRow;
while (value > 0 && index <= last) {
value--;
index = toGroupEnd(orderedRows, sortOrder, index, last) + 1;
}
}
}
break;
}
case RANGE: {
index = currentRow;
Value[] row = getCompareRow(session, orderedRows, sortOrder, index, bound, true);
if (row != null) {
index = Collections.binarySearch(orderedRows, row, sortOrder);
if (index >= 0) {
if (forFollowing) {
while (index < last && sortOrder.compare(row, orderedRows.get(index + 1)) == 0) {
index++;
}
} else {
while (index > 0 && sortOrder.compare(row, orderedRows.get(index - 1)) == 0) {
index--;
}
}
} else {
index = ~index;
if (forFollowing) {
if (index != size) {
index--;
}
}
}
} else {
index = size;
}
break;
}
default:
throw DbException.getUnsupportedException("units=" + units);
}
break;
case UNBOUNDED_FOLLOWING:
index = size;
break;
default:
throw DbException.getUnsupportedException("window frame bound type=" + bound.getType());
}
return index;
}
private Iterator<Value[]> complexIterator(ArrayList<Value[]> orderedRows, SortOrder sortOrder, int currentRow,
int startIndex, int endIndex, boolean reverse) {
if (exclusion == WindowFrameExclusion.EXCLUDE_CURRENT_ROW) {
if (currentRow < startIndex || currentRow > endIndex) {
// Nothing to exclude
} else if (currentRow == startIndex) {
startIndex++;
} else if (currentRow == endIndex) {
endIndex--;
} else {
return biIterator(orderedRows, startIndex, currentRow - 1, currentRow + 1, endIndex, reverse);
}
} else {
// Do not include previous rows if they are not in the range
int exStart = toGroupStart(orderedRows, sortOrder, currentRow, startIndex);
// Do not include next rows if they are not in the range
int exEnd = toGroupEnd(orderedRows, sortOrder, currentRow, endIndex);
boolean includeCurrentRow = exclusion == WindowFrameExclusion.EXCLUDE_TIES;
if (includeCurrentRow) {
// Simplify exclusion if possible
if (currentRow == exStart) {
exStart++;
includeCurrentRow = false;
} else if (currentRow == exEnd) {
exEnd--;
includeCurrentRow = false;
}
}
if (exStart > exEnd || exEnd < startIndex || exStart > endIndex) {
// Empty range or nothing to exclude
} else if (includeCurrentRow) {
if (startIndex == exStart) {
if (endIndex == exEnd) {
return Collections.singleton(orderedRows.get(currentRow)).iterator();
} else {
return biIterator(orderedRows, currentRow, currentRow, exEnd + 1, endIndex, reverse);
}
} else {
if (endIndex == exEnd) {
return biIterator(orderedRows, startIndex, exStart - 1, currentRow, currentRow, reverse);
} else {
return triIterator(orderedRows, startIndex, exStart - 1, currentRow, currentRow, exEnd + 1,
endIndex, reverse);
}
}
} else {
if (startIndex >= exStart) {
startIndex = exEnd + 1;
} else if (endIndex <= exEnd) {
endIndex = exStart - 1;
} else {
return biIterator(orderedRows, startIndex, exStart - 1, exEnd + 1, endIndex, reverse);
}
}
}
return plainIterator(orderedRows, startIndex, endIndex, reverse);
}
/**
* Append SQL representation to the specified builder.
*
* @param builder
* string builder
* @param formattingFlags
* quote all identifiers
* @return the specified string builder
* @see org.h2.expression.Expression#getSQL(StringBuilder, int, int)
*/
public StringBuilder getSQL(StringBuilder builder, int formattingFlags) {
builder.append(units.getSQL());
if (following == null) {
builder.append(' ');
starting.getSQL(builder, false, formattingFlags);
} else {
builder.append(" BETWEEN ");
starting.getSQL(builder, false, formattingFlags).append(" AND ");
following.getSQL(builder, true, formattingFlags);
}
if (exclusion != WindowFrameExclusion.EXCLUDE_NO_OTHERS) {
builder.append(' ').append(exclusion.getSQL());
}
return builder;
}
}