257886 {
257887 WindowAggregatorLocalState::Finalize(context, gastate, collection);
257888
257889
257890 auto &gstate = gastate.Cast<WindowSegmentTreeGlobalState>();
257891 auto cursor = make_uniq<WindowCursor>(*collection, gastate.aggregator.child_idx);
257892 const auto leaf_count = collection->size();
257893 auto &filter_mask = gstate.filter_mask;
257894 WindowSegmentTreePart gtstate(
allocator, gastate.aggr, std::move(
cursor), filter_mask);
257895
257896 auto &levels_flat_native = gstate.levels_flat_native;
257897 const auto &levels_flat_start = gstate.levels_flat_start;
257898
257899 for (;;) {
257900 const idx_t level_current = gstate.build_level.load();
257901 if (level_current >= levels_flat_start.size()) {
257902 break;
257903 }
257904
257905
257906 const auto level_size =
257907 (level_current == 0 ? leaf_count : levels_flat_start[level_current] - levels_flat_start[level_current - 1]);
257908 if (level_size <= 1) {
257909 break;
257910 }
257911 const idx_t build_count = (level_size + gstate.TREE_FANOUT - 1) / gstate.TREE_FANOUT;
257912
257913
257914 const idx_t build_idx = (*gstate.build_started).
at(level_current)++;
257915 if (build_idx >= build_count) {
257916
257917
257918 while (level_current == gstate.build_level.load()) {
257919 std::this_thread::sleep_for(std::chrono::milliseconds(1));
257920 }
257921 continue;
257922 }
257923
257924
257925 const idx_t pos = build_idx * gstate.TREE_FANOUT;
257926 const idx_t levels_flat_offset = levels_flat_start[level_current] + build_idx;
257927 auto state_ptr = levels_flat_native.GetStatePtr(levels_flat_offset);
257928 gtstate.WindowSegmentValue(gstate, level_current, pos, MinValue(level_size, pos + gstate.TREE_FANOUT),
257929 state_ptr);
257930 gtstate.FlushStates(level_current > 0);
257931
257932
257933 const idx_t build_complete = ++(*gstate.build_completed).
at(level_current);
257934 if (build_complete == build_count) {
257935 gstate.build_level++;
257936 continue;
257937 }
257938 }
257939}
ArenaAllocator allocator
Global allocator.
Definition duckdb.cpp:164091
unique_ptr< WindowCursor > cursor
The state used for reading the collection.
Definition duckdb.cpp:164094
T & at(int idx, std::vector< T > &items)
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237