forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowTransform.cpp
More file actions
1727 lines (1503 loc) · 62.4 KB
/
Copy pathWindowTransform.cpp
File metadata and controls
1727 lines (1503 loc) · 62.4 KB
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
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <Processors/Transforms/WindowTransform.h>
#include <AggregateFunctions/AggregateFunctionFactory.h>
#include <Common/Arena.h>
#include <Common/FieldVisitorsAccurateComparison.h>
#include <base/arithmeticOverflow.h>
#include <Columns/ColumnConst.h>
#include <DataTypes/DataTypesNumber.h>
#include <DataTypes/getLeastSupertype.h>
#include <Interpreters/ExpressionActions.h>
#include <Interpreters/convertFieldToType.h>
namespace DB
{
struct Settings;
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
extern const int NOT_IMPLEMENTED;
}
// Interface for true window functions. It's not much of an interface, they just
// accept the guts of WindowTransform and do 'something'. Given a small number of
// true window functions, and the fact that the WindowTransform internals are
// pretty much well defined in domain terms (e.g. frame boundaries), this is
// somewhat acceptable.
class IWindowFunction
{
public:
virtual ~IWindowFunction() = default;
// Must insert the result for current_row.
virtual void windowInsertResultInto(const WindowTransform * transform,
size_t function_index) = 0;
};
// Compares ORDER BY column values at given rows to find the boundaries of frame:
// [compared] with [reference] +/- offset. Return value is -1/0/+1, like in
// sorting predicates -- -1 means [compared] is less than [reference] +/- offset.
template <typename ColumnType>
static int compareValuesWithOffset(const IColumn * _compared_column,
size_t compared_row, const IColumn * _reference_column,
size_t reference_row,
const Field & _offset,
bool offset_is_preceding)
{
// Casting the columns to the known type here makes it faster, probably
// because the getData call can be devirtualized.
const auto * compared_column = assert_cast<const ColumnType *>(
_compared_column);
const auto * reference_column = assert_cast<const ColumnType *>(
_reference_column);
// Note that the storage type of offset returned by get<> is different, so
// we need to specify the type explicitly.
const typename ColumnType::ValueType offset
= _offset.get<typename ColumnType::ValueType>();
assert(offset >= 0);
const auto compared_value_data = compared_column->getDataAt(compared_row);
assert(compared_value_data.size == sizeof(typename ColumnType::ValueType));
auto compared_value = unalignedLoad<typename ColumnType::ValueType>(
compared_value_data.data);
const auto reference_value_data = reference_column->getDataAt(reference_row);
assert(reference_value_data.size == sizeof(typename ColumnType::ValueType));
auto reference_value = unalignedLoad<typename ColumnType::ValueType>(
reference_value_data.data);
bool is_overflow;
if (offset_is_preceding)
is_overflow = common::subOverflow(reference_value, offset, reference_value);
else
is_overflow = common::addOverflow(reference_value, offset, reference_value);
// fmt::print(stderr,
// "compared [{}] = {}, old ref {}, shifted ref [{}] = {}, offset {} preceding {} overflow {} to negative {}\n",
// compared_row, toString(compared_value),
// // fmt doesn't like char8_t.
// static_cast<Int64>(unalignedLoad<typename ColumnType::ValueType>(reference_value_data.data)),
// reference_row, toString(reference_value),
// toString(offset), offset_is_preceding,
// is_overflow, offset_is_preceding);
if (is_overflow)
{
if (offset_is_preceding)
{
// Overflow to the negative, [compared] must be greater.
// We know that because offset is >= 0.
return 1;
}
else
{
// Overflow to the positive, [compared] must be less.
return -1;
}
}
else
{
// No overflow, compare normally.
return compared_value < reference_value ? -1
: compared_value == reference_value ? 0 : 1;
}
}
// A specialization of compareValuesWithOffset for floats.
template <typename ColumnType>
static int compareValuesWithOffsetFloat(const IColumn * _compared_column,
size_t compared_row, const IColumn * _reference_column,
size_t reference_row,
const Field & _offset,
bool offset_is_preceding)
{
// Casting the columns to the known type here makes it faster, probably
// because the getData call can be devirtualized.
const auto * compared_column = assert_cast<const ColumnType *>(
_compared_column);
const auto * reference_column = assert_cast<const ColumnType *>(
_reference_column);
const auto offset = _offset.get<typename ColumnType::ValueType>();
assert(offset >= 0);
const auto compared_value_data = compared_column->getDataAt(compared_row);
assert(compared_value_data.size == sizeof(typename ColumnType::ValueType));
auto compared_value = unalignedLoad<typename ColumnType::ValueType>(
compared_value_data.data);
const auto reference_value_data = reference_column->getDataAt(reference_row);
assert(reference_value_data.size == sizeof(typename ColumnType::ValueType));
auto reference_value = unalignedLoad<typename ColumnType::ValueType>(
reference_value_data.data);
// Floats overflow to Inf and the comparison will work normally, so we don't
// have to do anything.
if (offset_is_preceding)
{
reference_value -= offset;
}
else
{
reference_value += offset;
}
const auto result = compared_value < reference_value ? -1
: compared_value == reference_value ? 0 : 1;
// fmt::print(stderr, "compared {}, offset {}, reference {}, result {}\n",
// compared_value, offset, reference_value, result);
return result;
}
// Helper macros to dispatch on type of the ORDER BY column
#define APPLY_FOR_ONE_TYPE(FUNCTION, TYPE) \
else if (typeid_cast<const TYPE *>(column)) \
{ \
/* clang-tidy you're dumb, I can't put FUNCTION in braces here. */ \
compare_values_with_offset = FUNCTION<TYPE>; /* NOLINT */ \
}
#define APPLY_FOR_TYPES(FUNCTION) \
if (false) /* NOLINT */ \
{ \
/* Do nothing, a starter condition. */ \
} \
APPLY_FOR_ONE_TYPE(FUNCTION, ColumnVector<UInt8>) \
APPLY_FOR_ONE_TYPE(FUNCTION, ColumnVector<UInt16>) \
APPLY_FOR_ONE_TYPE(FUNCTION, ColumnVector<UInt32>) \
APPLY_FOR_ONE_TYPE(FUNCTION, ColumnVector<UInt64>) \
\
APPLY_FOR_ONE_TYPE(FUNCTION, ColumnVector<Int8>) \
APPLY_FOR_ONE_TYPE(FUNCTION, ColumnVector<Int16>) \
APPLY_FOR_ONE_TYPE(FUNCTION, ColumnVector<Int32>) \
APPLY_FOR_ONE_TYPE(FUNCTION, ColumnVector<Int64>) \
APPLY_FOR_ONE_TYPE(FUNCTION, ColumnVector<Int128>) \
\
APPLY_FOR_ONE_TYPE(FUNCTION##Float, ColumnVector<Float32>) \
APPLY_FOR_ONE_TYPE(FUNCTION##Float, ColumnVector<Float64>) \
\
else \
{ \
throw Exception(ErrorCodes::NOT_IMPLEMENTED, \
"The RANGE OFFSET frame for '{}' ORDER BY column is not implemented", \
demangle(typeid(*column).name())); \
}
WindowTransform::WindowTransform(const Block & input_header_,
const Block & output_header_,
const WindowDescription & window_description_,
const std::vector<WindowFunctionDescription> & functions)
: IProcessor({input_header_}, {output_header_})
, input(inputs.front())
, output(outputs.front())
, input_header(input_header_)
, window_description(window_description_)
{
// Materialize all columns in header, because we materialize all columns
// in chunks and it's convenient if they match.
auto input_columns = input_header.getColumns();
for (auto & column : input_columns)
{
column = std::move(column)->convertToFullColumnIfConst();
}
input_header.setColumns(std::move(input_columns));
// Initialize window function workspaces.
workspaces.reserve(functions.size());
for (const auto & f : functions)
{
WindowFunctionWorkspace workspace;
workspace.aggregate_function = f.aggregate_function;
const auto & aggregate_function = workspace.aggregate_function;
if (!arena && aggregate_function->allocatesMemoryInArena())
{
arena = std::make_unique<Arena>();
}
workspace.argument_column_indices.reserve(f.argument_names.size());
for (const auto & argument_name : f.argument_names)
{
workspace.argument_column_indices.push_back(
input_header.getPositionByName(argument_name));
}
workspace.argument_columns.assign(f.argument_names.size(), nullptr);
/// Currently we have slightly wrong mixup of the interfaces of Window and Aggregate functions.
workspace.window_function_impl = dynamic_cast<IWindowFunction *>(const_cast<IAggregateFunction *>(aggregate_function.get()));
if (!workspace.window_function_impl)
{
workspace.aggregate_function_state.reset(
aggregate_function->sizeOfData(),
aggregate_function->alignOfData());
aggregate_function->create(workspace.aggregate_function_state.data());
}
workspaces.push_back(std::move(workspace));
}
partition_by_indices.reserve(window_description.partition_by.size());
for (const auto & column : window_description.partition_by)
{
partition_by_indices.push_back(
input_header.getPositionByName(column.column_name));
}
order_by_indices.reserve(window_description.order_by.size());
for (const auto & column : window_description.order_by)
{
order_by_indices.push_back(
input_header.getPositionByName(column.column_name));
}
// Choose a row comparison function for RANGE OFFSET frame based on the
// type of the ORDER BY column.
if (window_description.frame.type == WindowFrame::FrameType::Range
&& (window_description.frame.begin_type
== WindowFrame::BoundaryType::Offset
|| window_description.frame.end_type
== WindowFrame::BoundaryType::Offset))
{
assert(order_by_indices.size() == 1);
const auto & entry = input_header.getByPosition(order_by_indices[0]);
const IColumn * column = entry.column.get();
APPLY_FOR_TYPES(compareValuesWithOffset)
// Convert the offsets to the ORDER BY column type. We can't just check
// that the type matches, because e.g. the int literals are always
// (U)Int64, but the column might be Int8 and so on.
if (window_description.frame.begin_type
== WindowFrame::BoundaryType::Offset)
{
window_description.frame.begin_offset = convertFieldToTypeOrThrow(
window_description.frame.begin_offset,
*entry.type);
if (applyVisitor(FieldVisitorAccurateLess{},
window_description.frame.begin_offset, Field(0)))
{
throw Exception(ErrorCodes::BAD_ARGUMENTS,
"Window frame start offset must be nonnegative, {} given",
window_description.frame.begin_offset);
}
}
if (window_description.frame.end_type
== WindowFrame::BoundaryType::Offset)
{
window_description.frame.end_offset = convertFieldToTypeOrThrow(
window_description.frame.end_offset,
*entry.type);
if (applyVisitor(FieldVisitorAccurateLess{},
window_description.frame.end_offset, Field(0)))
{
throw Exception(ErrorCodes::BAD_ARGUMENTS,
"Window frame start offset must be nonnegative, {} given",
window_description.frame.end_offset);
}
}
}
}
WindowTransform::~WindowTransform()
{
// Some states may be not created yet if the creation failed.
for (auto & ws : workspaces)
{
if (!ws.window_function_impl)
{
ws.aggregate_function->destroy(
ws.aggregate_function_state.data());
}
}
}
void WindowTransform::advancePartitionEnd()
{
if (partition_ended)
{
return;
}
const RowNumber end = blocksEnd();
// fmt::print(stderr, "end {}, partition_end {}\n", end, partition_end);
// If we're at the total end of data, we must end the partition. This is one
// of the few places in calculations where we need special handling for end
// of data, other places will work as usual based on
// `partition_ended` = true, because end of data is logically the same as
// any other end of partition.
// We must check this first, because other calculations might not be valid
// when we're at the end of data.
if (input_is_finished)
{
partition_ended = true;
// We receive empty chunk at the end of data, so the partition_end must
// be already at the end of data.
assert(partition_end == end);
return;
}
// If we got to the end of the block already, but we are going to get more
// input data, wait for it.
if (partition_end == end)
{
return;
}
// We process one block at a time, but we can process each block many times,
// if it contains multiple partitions. The `partition_end` is a
// past-the-end pointer, so it must be already in the "next" block we haven't
// processed yet. This is also the last block we have.
// The exception to this rule is end of data, for which we checked above.
assert(end.block == partition_end.block + 1);
// Try to advance the partition end pointer.
const size_t partition_by_columns = partition_by_indices.size();
if (partition_by_columns == 0)
{
// No PARTITION BY. All input is one partition, which will end when the
// input ends.
partition_end = end;
return;
}
// Check for partition end.
// The partition ends when the PARTITION BY columns change. We need
// some reference columns for comparison. We might have already
// dropped the blocks where the partition starts, but any other row in the
// partition will do. We can't use frame_start or frame_end or current_row (the next row
// for which we are calculating the window functions), because they all might be
// past the end of the partition. prev_frame_start is suitable, because it
// is a pointer to the first row of the previous frame that must have been
// valid, or to the first row of the partition, and we make sure not to drop
// its block.
assert(partition_start <= prev_frame_start);
// The frame start should be inside the prospective partition, except the
// case when it still has no rows.
assert(prev_frame_start < partition_end || partition_start == partition_end);
assert(first_block_number <= prev_frame_start.block);
const auto block_rows = blockRowsNumber(partition_end);
for (; partition_end.row < block_rows; ++partition_end.row)
{
// fmt::print(stderr, "compare reference '{}' to compared '{}'\n",
// prev_frame_start, partition_end);
size_t i = 0;
for (; i < partition_by_columns; i++)
{
const auto * reference_column
= inputAt(prev_frame_start)[partition_by_indices[i]].get();
const auto * compared_column
= inputAt(partition_end)[partition_by_indices[i]].get();
// fmt::print(stderr, "reference '{}', compared '{}'\n",
// (*reference_column)[prev_frame_start.row],
// (*compared_column)[partition_end.row]);
if (compared_column->compareAt(partition_end.row,
prev_frame_start.row, *reference_column,
1 /* nan_direction_hint */) != 0)
{
break;
}
}
if (i < partition_by_columns)
{
partition_ended = true;
return;
}
}
// Went until the end of block, go to the next.
assert(partition_end.row == block_rows);
++partition_end.block;
partition_end.row = 0;
// Went until the end of data and didn't find the new partition.
assert(!partition_ended && partition_end == blocksEnd());
}
auto WindowTransform::moveRowNumberNoCheck(const RowNumber & _x, int64_t offset) const
{
RowNumber x = _x;
if (offset > 0)
{
for (;;)
{
assertValid(x);
assert(offset >= 0);
const auto block_rows = blockRowsNumber(x);
x.row += offset;
if (x.row >= block_rows)
{
offset = x.row - block_rows;
x.row = 0;
x.block++;
if (x == blocksEnd())
{
break;
}
}
else
{
offset = 0;
break;
}
}
}
else if (offset < 0)
{
for (;;)
{
assertValid(x);
assert(offset <= 0);
// abs(offset) is less than INT64_MAX, as checked in the parser, so
// this negation should always work.
assert(offset >= -INT64_MAX);
if (x.row >= static_cast<uint64_t>(-offset))
{
x.row -= -offset;
offset = 0;
break;
}
// Move to the first row in current block. Note that the offset is
// negative.
offset += x.row;
x.row = 0;
// Move to the last row of the previous block, if we are not at the
// first one. Offset also is incremented by one, because we pass over
// the first row of this block.
if (x.block == first_block_number)
{
break;
}
--x.block;
offset += 1;
x.row = blockRowsNumber(x) - 1;
}
}
return std::tuple{x, offset};
}
auto WindowTransform::moveRowNumber(const RowNumber & _x, int64_t offset) const
{
auto [x, o] = moveRowNumberNoCheck(_x, offset);
#ifndef NDEBUG
// Check that it was reversible.
auto [xx, oo] = moveRowNumberNoCheck(x, -(offset - o));
// fmt::print(stderr, "{} -> {}, result {}, {}, new offset {}, twice {}, {}\n",
// _x, offset, x, o, -(offset - o), xx, oo);
assert(xx == _x);
assert(oo == 0);
#endif
return std::tuple{x, o};
}
void WindowTransform::advanceFrameStartRowsOffset()
{
// Just recalculate it each time by walking blocks.
const auto [moved_row, offset_left] = moveRowNumber(current_row,
window_description.frame.begin_offset.get<UInt64>()
* (window_description.frame.begin_preceding ? -1 : 1));
frame_start = moved_row;
assertValid(frame_start);
// fmt::print(stderr, "frame start {} left {} partition start {}\n",
// frame_start, offset_left, partition_start);
if (frame_start <= partition_start)
{
// Got to the beginning of partition and can't go further back.
frame_start = partition_start;
frame_started = true;
return;
}
if (partition_end <= frame_start)
{
// A FOLLOWING frame start ran into the end of partition.
frame_start = partition_end;
frame_started = partition_ended;
return;
}
// Handled the equality case above. Now the frame start is inside the
// partition, if we walked all the offset, it's final.
assert(partition_start < frame_start);
frame_started = offset_left == 0;
// If we ran into the start of data (offset left is negative), we won't be
// able to make progress. Should have handled this case above.
assert(offset_left >= 0);
}
void WindowTransform::advanceFrameStartRangeOffset()
{
// See the comment for advanceFrameEndRangeOffset().
const int direction = window_description.order_by[0].direction;
const bool preceding = window_description.frame.begin_preceding
== (direction > 0);
const auto * reference_column
= inputAt(current_row)[order_by_indices[0]].get();
for (; frame_start < partition_end; advanceRowNumber(frame_start))
{
// The first frame value is [current_row] with offset, so we advance
// while [frames_start] < [current_row] with offset.
const auto * compared_column
= inputAt(frame_start)[order_by_indices[0]].get();
if (compare_values_with_offset(compared_column, frame_start.row,
reference_column, current_row.row,
window_description.frame.begin_offset,
preceding)
* direction >= 0)
{
frame_started = true;
return;
}
}
frame_started = partition_ended;
}
void WindowTransform::advanceFrameStart()
{
if (frame_started)
{
return;
}
const auto frame_start_before = frame_start;
switch (window_description.frame.begin_type)
{
case WindowFrame::BoundaryType::Unbounded:
// UNBOUNDED PRECEDING, just mark it valid. It is initialized when
// the new partition starts.
frame_started = true;
break;
case WindowFrame::BoundaryType::Current:
// CURRENT ROW differs between frame types only in how the peer
// groups are accounted.
assert(partition_start <= peer_group_start);
assert(peer_group_start < partition_end);
assert(peer_group_start <= current_row);
frame_start = peer_group_start;
frame_started = true;
break;
case WindowFrame::BoundaryType::Offset:
switch (window_description.frame.type)
{
case WindowFrame::FrameType::Rows:
advanceFrameStartRowsOffset();
break;
case WindowFrame::FrameType::Range:
advanceFrameStartRangeOffset();
break;
default:
throw Exception(ErrorCodes::NOT_IMPLEMENTED,
"Frame start type '{}' for frame '{}' is not implemented",
window_description.frame.begin_type,
window_description.frame.type);
}
break;
}
assert(frame_start_before <= frame_start);
if (frame_start == frame_start_before)
{
// If the frame start didn't move, this means we validated that the frame
// starts at the point we reached earlier but were unable to validate.
// This probably only happens in degenerate cases where the frame start
// is further than the end of partition, and the partition ends at the
// last row of the block, but we can only tell for sure after a new
// block arrives. We still have to update the state of aggregate
// functions when the frame start becomes valid, so we continue.
assert(frame_started);
}
assert(partition_start <= frame_start);
assert(frame_start <= partition_end);
if (partition_ended && frame_start == partition_end)
{
// Check that if the start of frame (e.g. FOLLOWING) runs into the end
// of partition, it is marked as valid -- we can't advance it any
// further.
assert(frame_started);
}
}
bool WindowTransform::arePeers(const RowNumber & x, const RowNumber & y) const
{
if (x == y)
{
// For convenience, a row is always its own peer.
return true;
}
if (window_description.frame.type == WindowFrame::FrameType::Rows)
{
// For ROWS frame, row is only peers with itself (checked above);
return false;
}
// For RANGE and GROUPS frames, rows that compare equal w/ORDER BY are peers.
assert(window_description.frame.type == WindowFrame::FrameType::Range);
const size_t n = order_by_indices.size();
if (n == 0)
{
// No ORDER BY, so all rows are peers.
return true;
}
size_t i = 0;
for (; i < n; i++)
{
const auto * column_x = inputAt(x)[order_by_indices[i]].get();
const auto * column_y = inputAt(y)[order_by_indices[i]].get();
if (column_x->compareAt(x.row, y.row, *column_y,
1 /* nan_direction_hint */) != 0)
{
return false;
}
}
return true;
}
void WindowTransform::advanceFrameEndCurrentRow()
{
// fmt::print(stderr, "starting from frame_end {}\n", frame_end);
// We only process one block here, and frame_end must be already in it: if
// we didn't find the end in the previous block, frame_end is now the first
// row of the current block. We need this knowledge to write a simpler loop
// (only loop over rows and not over blocks), that should hopefully be more
// efficient.
// partition_end is either in this new block or past-the-end.
assert(frame_end.block == partition_end.block
|| frame_end.block + 1 == partition_end.block);
if (frame_end == partition_end)
{
// The case when we get a new block and find out that the partition has
// ended.
assert(partition_ended);
frame_ended = partition_ended;
return;
}
// We advance until the partition end. It's either in the current block or
// in the next one, which is also the past-the-end block. Figure out how
// many rows we have to process.
uint64_t rows_end;
if (partition_end.row == 0)
{
assert(partition_end == blocksEnd());
rows_end = blockRowsNumber(frame_end);
}
else
{
assert(frame_end.block == partition_end.block);
rows_end = partition_end.row;
}
// Equality would mean "no data to process", for which we checked above.
assert(frame_end.row < rows_end);
// fmt::print(stderr, "first row {} last {}\n", frame_end.row, rows_end);
// Advance frame_end while it is still peers with the current row.
for (; frame_end.row < rows_end; ++frame_end.row)
{
if (!arePeers(current_row, frame_end))
{
// fmt::print(stderr, "{} and {} don't match\n", reference, frame_end);
frame_ended = true;
return;
}
}
// Might have gotten to the end of the current block, have to properly
// update the row number.
if (frame_end.row == blockRowsNumber(frame_end))
{
++frame_end.block;
frame_end.row = 0;
}
// Got to the end of partition (frame ended as well then) or end of data.
assert(frame_end == partition_end);
frame_ended = partition_ended;
}
void WindowTransform::advanceFrameEndUnbounded()
{
// The UNBOUNDED FOLLOWING frame ends when the partition ends.
frame_end = partition_end;
frame_ended = partition_ended;
}
void WindowTransform::advanceFrameEndRowsOffset()
{
// Walk the specified offset from the current row. The "+1" is needed
// because the frame_end is a past-the-end pointer.
const auto [moved_row, offset_left] = moveRowNumber(current_row,
window_description.frame.end_offset.get<UInt64>()
* (window_description.frame.end_preceding ? -1 : 1)
+ 1);
if (partition_end <= moved_row)
{
// Clamp to the end of partition. It might not have ended yet, in which
// case wait for more data.
frame_end = partition_end;
frame_ended = partition_ended;
return;
}
if (moved_row <= partition_start)
{
// Clamp to the start of partition.
frame_end = partition_start;
frame_ended = true;
return;
}
// Frame end inside partition, if we walked all the offset, it's final.
frame_end = moved_row;
frame_ended = offset_left == 0;
// If we ran into the start of data (offset left is negative), we won't be
// able to make progress. Should have handled this case above.
assert(offset_left >= 0);
}
void WindowTransform::advanceFrameEndRangeOffset()
{
// PRECEDING/FOLLOWING change direction for DESC order.
// See CD 9075-2:201?(E) 7.14 <window clause> p. 429.
const int direction = window_description.order_by[0].direction;
const bool preceding = window_description.frame.end_preceding
== (direction > 0);
const auto * reference_column
= inputAt(current_row)[order_by_indices[0]].get();
for (; frame_end < partition_end; advanceRowNumber(frame_end))
{
// The last frame value is current_row with offset, and we need a
// past-the-end pointer, so we advance while
// [frame_end] <= [current_row] with offset.
const auto * compared_column
= inputAt(frame_end)[order_by_indices[0]].get();
if (compare_values_with_offset(compared_column, frame_end.row,
reference_column, current_row.row,
window_description.frame.end_offset,
preceding)
* direction > 0)
{
frame_ended = true;
return;
}
}
frame_ended = partition_ended;
}
void WindowTransform::advanceFrameEnd()
{
// No reason for this function to be called again after it succeeded.
assert(!frame_ended);
const auto frame_end_before = frame_end;
switch (window_description.frame.end_type)
{
case WindowFrame::BoundaryType::Current:
advanceFrameEndCurrentRow();
break;
case WindowFrame::BoundaryType::Unbounded:
advanceFrameEndUnbounded();
break;
case WindowFrame::BoundaryType::Offset:
switch (window_description.frame.type)
{
case WindowFrame::FrameType::Rows:
advanceFrameEndRowsOffset();
break;
case WindowFrame::FrameType::Range:
advanceFrameEndRangeOffset();
break;
default:
throw Exception(ErrorCodes::NOT_IMPLEMENTED,
"The frame end type '{}' is not implemented",
window_description.frame.end_type);
}
break;
}
// fmt::print(stderr, "frame_end {} -> {}\n", frame_end_before, frame_end);
// We might not have advanced the frame end if we found out we reached the
// end of input or the partition, or if we still don't know the frame start.
if (frame_end_before == frame_end)
{
return;
}
}
// Update the aggregation states after the frame has changed.
void WindowTransform::updateAggregationState()
{
// fmt::print(stderr, "update agg states [{}, {}) -> [{}, {})\n",
// prev_frame_start, prev_frame_end, frame_start, frame_end);
// Assert that the frame boundaries are known, have proper order wrt each
// other, and have not gone back wrt the previous frame.
assert(frame_started);
assert(frame_ended);
assert(frame_start <= frame_end);
assert(prev_frame_start <= prev_frame_end);
assert(prev_frame_start <= frame_start);
assert(prev_frame_end <= frame_end);
assert(partition_start <= frame_start);
assert(frame_end <= partition_end);
// We might have to reset aggregation state and/or add some rows to it.
// Figure out what to do.
bool reset_aggregation = false;
RowNumber rows_to_add_start;
RowNumber rows_to_add_end;
if (frame_start == prev_frame_start)
{
// The frame start didn't change, add the tail rows.
reset_aggregation = false;
rows_to_add_start = prev_frame_end;
rows_to_add_end = frame_end;
}
else
{
// The frame start changed, reset the state and aggregate over the
// entire frame. This can be made per-function after we learn to
// subtract rows from some types of aggregation states, but for now we
// always have to reset when the frame start changes.
reset_aggregation = true;
rows_to_add_start = frame_start;
rows_to_add_end = frame_end;
}
for (auto & ws : workspaces)
{
if (ws.window_function_impl)
{
// No need to do anything for true window functions.
continue;
}
const auto * a = ws.aggregate_function.get();
auto * buf = ws.aggregate_function_state.data();
if (reset_aggregation)
{
// fmt::print(stderr, "(2) reset aggregation\n");
a->destroy(buf);
a->create(buf);
}
// To achieve better performance, we will have to loop over blocks and
// rows manually, instead of using advanceRowNumber().
// For this purpose, the past-the-end block can be different than the
// block of the past-the-end row (it's usually the next block).
const auto past_the_end_block = rows_to_add_end.row == 0
? rows_to_add_end.block
: rows_to_add_end.block + 1;
for (auto block_number = rows_to_add_start.block;
block_number < past_the_end_block;
++block_number)
{
auto & block = blockAt(block_number);
if (ws.cached_block_number != block_number)
{
for (size_t i = 0; i < ws.argument_column_indices.size(); ++i)
{
ws.argument_columns[i] = block.input_columns[
ws.argument_column_indices[i]].get();
}
ws.cached_block_number = block_number;
}
// First and last blocks may be processed partially, and other blocks
// are processed in full.
const auto first_row = block_number == rows_to_add_start.block
? rows_to_add_start.row : 0;
const auto past_the_end_row = block_number == rows_to_add_end.block
? rows_to_add_end.row : block.rows;
// We should add an addBatch analog that can accept a starting offset.
// For now, add the values one by one.
auto * columns = ws.argument_columns.data();
// Removing arena.get() from the loop makes it faster somehow...
auto * arena_ptr = arena.get();
for (auto row = first_row; row < past_the_end_row; ++row)
{
a->add(buf, columns, row, arena_ptr);
}
}
}
prev_frame_start = frame_start;
prev_frame_end = frame_end;
}
void WindowTransform::writeOutCurrentRow()
{
assert(current_row < partition_end);
assert(current_row.block >= first_block_number);
const auto & block = blockAt(current_row);
for (size_t wi = 0; wi < workspaces.size(); ++wi)
{
auto & ws = workspaces[wi];
if (ws.window_function_impl)
{
ws.window_function_impl->windowInsertResultInto(this, wi);
}
else
{
IColumn * result_column = block.output_columns[wi].get();
const auto * a = ws.aggregate_function.get();
auto * buf = ws.aggregate_function_state.data();
// FIXME does it also allocate the result on the arena?
// We'll have to pass it out with blocks then...
a->insertResultInto(buf, *result_column, arena.get());
}
}
// fmt::print(stderr, "wrote out aggregation state for current row '{}'\n",
// current_row);
}