Skip to content

Commit

Permalink
Feature duckdb#1272: Windowed Quantile Tree
Browse files Browse the repository at this point in the history
Use the new thread-safe Build method to parallelise tree construction.
We can call it on every row because it has a quick exit
and it is thread-safe.

This is the only custom window init function that
allocates a large data structure, so the PR effecively
parallelises the construction step of custom windowed aggregates,
and shows how to parallelise any future complex aggregates
without any API changes.
  • Loading branch information
hawkfish committed Aug 22, 2024
1 parent 9ad037f commit c14b942
Showing 1 changed file with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ struct QuantileSortTree : public MergeSortTree<IDX, IDX> {
explicit QuantileSortTree(Elements &&lowest_level) {
BaseTree::Allocate(lowest_level.size());
BaseTree::LowestLevel() = std::move(lowest_level);
BaseTree::Build();
}

template <class INPUT_TYPE>
Expand Down Expand Up @@ -289,9 +288,12 @@ struct QuantileSortTree : public MergeSortTree<IDX, IDX> {

template <typename INPUT_TYPE, typename RESULT_TYPE, bool DISCRETE>
RESULT_TYPE WindowScalar(const INPUT_TYPE *data, const SubFrames &frames, const idx_t n, Vector &result,
const QuantileValue &q) const {
const QuantileValue &q) {
D_ASSERT(n > 0);

// Thread safe and idempotent.
BaseTree::Build();

// Find the interpolated indicies within the frame
Interpolator<DISCRETE> interp(q, n, false);
const auto lo_data = SelectNth(frames, interp.FRN);
Expand All @@ -308,9 +310,12 @@ struct QuantileSortTree : public MergeSortTree<IDX, IDX> {

template <typename INPUT_TYPE, typename CHILD_TYPE, bool DISCRETE>
void WindowList(const INPUT_TYPE *data, const SubFrames &frames, const idx_t n, Vector &list, const idx_t lidx,
const QuantileBindData &bind_data) const {
const QuantileBindData &bind_data) {
D_ASSERT(n > 0);

// Thread safe and idempotent.
BaseTree::Build();

// Result is a constant LIST<CHILD_TYPE> with a fixed length
auto ldata = FlatVector::GetData<list_entry_t>(list);
auto &lentry = ldata[lidx];
Expand Down

0 comments on commit c14b942

Please sign in to comment.