From 52562cba57a5044552bcaf11dd3eed0753f2325d Mon Sep 17 00:00:00 2001 From: Tishj Date: Thu, 11 Jan 2024 10:04:50 +0100 Subject: [PATCH 1/3] verify that the chunks are initialized if the count reaches CHUNK_CAPACITY, this already happens when count reaches LIST_CAPACITY, but LIST_CAPACITY is hardcoded to 16, CHUNK_CAPACITY could be smaller --- src/function/aggregate/sorted_aggregate_function.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/function/aggregate/sorted_aggregate_function.cpp b/src/function/aggregate/sorted_aggregate_function.cpp index afcbd42d82e..3775821c431 100644 --- a/src/function/aggregate/sorted_aggregate_function.cpp +++ b/src/function/aggregate/sorted_aggregate_function.cpp @@ -189,6 +189,7 @@ struct SortedAggregateState { } if (count > CHUNK_CAPACITY && !ordering) { + InitializeChunks(order_bind); InitializeCollections(order_bind); FlushChunks(order_bind); } From ed0b892dc12df957ab5ca385b858dcfa41940481 Mon Sep 17 00:00:00 2001 From: Tishj Date: Thu, 11 Jan 2024 10:16:15 +0100 Subject: [PATCH 2/3] fix issue where lists are not being flushed --- src/function/aggregate/sorted_aggregate_function.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/function/aggregate/sorted_aggregate_function.cpp b/src/function/aggregate/sorted_aggregate_function.cpp index 3775821c431..32f9b4ef748 100644 --- a/src/function/aggregate/sorted_aggregate_function.cpp +++ b/src/function/aggregate/sorted_aggregate_function.cpp @@ -189,7 +189,9 @@ struct SortedAggregateState { } if (count > CHUNK_CAPACITY && !ordering) { - InitializeChunks(order_bind); + if (CHUNK_CAPACITY < LIST_CAPACITY) { + FlushLinkedLists(order_bind); + } InitializeCollections(order_bind); FlushChunks(order_bind); } From 0996c36dc785f90b7437bdaad65a56563879ccd6 Mon Sep 17 00:00:00 2001 From: Tishj Date: Fri, 19 Jan 2024 16:57:32 +0100 Subject: [PATCH 3/3] cap LIST_CAPACITY to STANDARD_VECTOR_SIZE, so it's never bigger --- src/function/aggregate/sorted_aggregate_function.cpp | 5 +---- src/include/duckdb/common/helper.hpp | 8 ++++---- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/function/aggregate/sorted_aggregate_function.cpp b/src/function/aggregate/sorted_aggregate_function.cpp index 32f9b4ef748..e140bb9c444 100644 --- a/src/function/aggregate/sorted_aggregate_function.cpp +++ b/src/function/aggregate/sorted_aggregate_function.cpp @@ -102,8 +102,8 @@ struct SortedAggregateState { using LinkedChunkFunctions = vector; //! Capacities of the various levels of buffering - static const idx_t LIST_CAPACITY = 16; static const idx_t CHUNK_CAPACITY = STANDARD_VECTOR_SIZE; + static const idx_t LIST_CAPACITY = MinValue(16, CHUNK_CAPACITY); SortedAggregateState() : count(0), nsel(0), offset(0) { } @@ -189,9 +189,6 @@ struct SortedAggregateState { } if (count > CHUNK_CAPACITY && !ordering) { - if (CHUNK_CAPACITY < LIST_CAPACITY) { - FlushLinkedLists(order_bind); - } InitializeCollections(order_bind); FlushChunks(order_bind); } diff --git a/src/include/duckdb/common/helper.hpp b/src/include/duckdb/common/helper.hpp index 944ad7a24bb..cb30b3ee1a3 100644 --- a/src/include/duckdb/common/helper.hpp +++ b/src/include/duckdb/common/helper.hpp @@ -146,13 +146,13 @@ static duckdb::unique_ptr make_unique(_Args&&... __args) { } template -T MaxValue(T a, T b) { - return a > b ? a : b; +constexpr T MaxValue(T a, T b) { + return a > b ? a : b; } template -T MinValue(T a, T b) { - return a < b ? a : b; +constexpr T MinValue(T a, T b) { + return a < b ? a : b; } template