Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::TupleDataAllocator Class Reference
Collaboration diagram for duckdb::TupleDataAllocator:

Public Member Functions

 TupleDataAllocator (BufferManager &buffer_manager, shared_ptr< TupleDataLayout > layout_ptr, MemoryTag tag, shared_ptr< ArenaAllocator > stl_allocator)
 TupleDataAllocator (TupleDataAllocator &allocator)
BufferManager & GetBufferManager ()
 Get the buffer manager.
Allocator & GetAllocator ()
 Get the buffer allocator.
ArenaAllocator & GetStlAllocator ()
 Get the STL allocator.
shared_ptr< TupleDataLayout > GetLayoutPtr () const
 Get the layout.
const TupleDataLayout & GetLayout () const
idx_t RowBlockCount () const
 Number of row blocks.
idx_t HeapBlockCount () const
 Number of heap blocks.
void SetPartitionIndex (idx_t index)
 Sets the partition index of this tuple data allocator.
void Build (TupleDataSegment &segment, TupleDataPinState &pin_state, TupleDataChunkState &chunk_state, const idx_t append_offset, const idx_t append_count)
 Builds out the chunks for next append, given the metadata in the append state.
bool BuildFastPath (TupleDataSegment &segment, TupleDataPinState &pin_state, TupleDataChunkState &chunk_state, const idx_t append_offset, const idx_t append_count)
void InitializeChunkState (TupleDataSegment &segment, TupleDataPinState &pin_state, TupleDataChunkState &chunk_state, idx_t chunk_idx, bool init_heap, optional_ptr< SortKeyPayloadState > sort_key_payload_state=nullptr)
 Initializes a chunk, making its pointers valid.
void ReleaseOrStoreHandles (TupleDataPinState &state, TupleDataSegment &segment, TupleDataChunk &chunk, bool release_heap)
 Releases or stores any handles in the management state that are no longer required.
void ReleaseOrStoreHandles (TupleDataPinState &state, TupleDataSegment &segment)
 Releases or stores ALL handles in the management state.
void SetDestroyBufferUponUnpin ()
 Sets 'can_destroy' to true for all blocks so they aren't added to the eviction queue.
void DestroyRowBlocks (idx_t row_block_begin, idx_t row_block_end)
 Destroy the blocks between the given indices.
void DestroyHeapBlocks (idx_t heap_block_begin, idx_t heap_block_end)

Static Public Member Functions

static void RecomputeHeapPointers (Vector &old_heap_ptrs, const SelectionVector &old_heap_sel, const data_ptr_t row_locations[], Vector &new_heap_ptrs, const idx_t offset, const idx_t count, const TupleDataLayout &layout, const idx_t base_col_offset)
static void FindHeapPointers (TupleDataChunkState &chunk_state, SelectionVector &not_found, idx_t &not_found_count, const TupleDataLayout &layout, const idx_t base_col_offset)

Private Member Functions

unsafe_arena_ptr< TupleDataChunkPart > BuildChunkPart (TupleDataSegment &segment, TupleDataPinState &pin_state, TupleDataChunkState &chunk_state, const idx_t append_offset, const idx_t append_count, TupleDataChunk &chunk)
 Builds out a single part (grabs the lock).
void InitializeChunkStateInternal (TupleDataPinState &pin_state, TupleDataChunkState &chunk_state, idx_t offset, bool recompute, bool init_heap_pointers, bool init_heap_sizes, unsafe_vector< reference< TupleDataChunkPart > > &parts, optional_ptr< SortKeyPayloadState > sort_key_payload_state=nullptr)
 Internal function for InitializeChunkState.
void CreateRowBlock (TupleDataSegment &segment)
 Create a row/heap block, extend the pinned handles in the segment accordingly.
void CreateHeapBlock (TupleDataSegment &segment, idx_t size)
BufferHandle & PinRowBlock (TupleDataPinState &state, const TupleDataChunkPart &part)
 Pins the given row block.
BufferHandle & PinHeapBlock (TupleDataPinState &state, const TupleDataChunkPart &part)
 Pins the given heap block.
data_ptr_t GetRowPointer (TupleDataPinState &state, const TupleDataChunkPart &part)
 Gets the pointer to the rows for the given chunk part.
data_ptr_t GetBaseHeapPointer (TupleDataPinState &state, const TupleDataChunkPart &part)
 Gets the base pointer to the heap for the given chunk part.

Static Private Member Functions

static void ReleaseOrStoreHandlesInternal (TupleDataSegment &segment, unsafe_arena_vector< BufferHandle > &pinned_row_handles, buffer_handle_map_t &handles, const ContinuousIdSet &block_ids, unsafe_arena_vector< TupleDataBlock > &blocks, TupleDataPinProperties properties)
 Internal function for ReleaseOrStoreHandles.

Private Attributes

shared_ptr< ArenaAllocator > stl_allocator
 Shared allocator for STL allocations.
BufferManager & buffer_manager
 The buffer manager.
shared_ptr< TupleDataLayout > layout_ptr
 The layout of the data.
const TupleDataLayout & layout
const MemoryTag tag
 Memory tag (for keeping track what the allocated memory belongs to).
optional_idx partition_index
 Partition index (optional, if partitioned).
unsafe_arena_vector< TupleDataBlock > row_blocks
 Blocks storing the fixed-size rows.
unsafe_arena_vector< TupleDataBlock > heap_blocks
 Blocks storing the variable-size data of the fixed-size rows (e.g., string, list).

Constructor & Destructor Documentation

◆ TupleDataAllocator() [1/2]

duckdb::TupleDataAllocator::TupleDataAllocator ( BufferManager & buffer_manager,
shared_ptr< TupleDataLayout > layout_ptr,
MemoryTag tag,
shared_ptr< ArenaAllocator > stl_allocator )
124851 : stl_allocator(std::move(stl_allocator_p)), buffer_manager(buffer_manager), layout_ptr(std::move(layout_ptr_p)),
124853}
unsafe_arena_vector< TupleDataBlock > row_blocks
Blocks storing the fixed-size rows.
Definition duckdb.cpp:57537
unsafe_arena_vector< TupleDataBlock > heap_blocks
Blocks storing the variable-size data of the fixed-size rows (e.g., string, list).
Definition duckdb.cpp:57539
BufferManager & buffer_manager
The buffer manager.
Definition duckdb.cpp:57528
const MemoryTag tag
Memory tag (for keeping track what the allocated memory belongs to).
Definition duckdb.cpp:57533
shared_ptr< ArenaAllocator > stl_allocator
Shared allocator for STL allocations.
Definition duckdb.cpp:57526
shared_ptr< TupleDataLayout > layout_ptr
The layout of the data.
Definition duckdb.cpp:57530

◆ TupleDataAllocator() [2/2]

duckdb::TupleDataAllocator::TupleDataAllocator ( TupleDataAllocator & allocator)
124856 : TupleDataAllocator(allocator.buffer_manager, allocator.layout_ptr, allocator.tag, allocator.stl_allocator) {
124857}

◆ ~TupleDataAllocator()

duckdb::TupleDataAllocator::~TupleDataAllocator ( )
124891 {
124893}
void SetDestroyBufferUponUnpin()
Sets 'can_destroy' to true for all blocks so they aren't added to the eviction queue.
Definition duckdb.cpp:124859

Member Function Documentation

◆ GetBufferManager()

BufferManager & duckdb::TupleDataAllocator::GetBufferManager ( )

Get the buffer manager.

124895 {
124896 return buffer_manager;
124897}

◆ GetAllocator()

Allocator & duckdb::TupleDataAllocator::GetAllocator ( )

Get the buffer allocator.

124899 {
124900 return buffer_manager.GetBufferAllocator();
124901}

◆ GetStlAllocator()

ArenaAllocator & duckdb::TupleDataAllocator::GetStlAllocator ( )

Get the STL allocator.

124903 {
124904 return *stl_allocator;
124905}

◆ GetLayoutPtr()

shared_ptr< TupleDataLayout > duckdb::TupleDataAllocator::GetLayoutPtr ( ) const

Get the layout.

124907 {
124908 return layout_ptr;
124909}

◆ GetLayout()

const TupleDataLayout & duckdb::TupleDataAllocator::GetLayout ( ) const
124911 {
124912 return layout;
124913}

◆ RowBlockCount()

idx_t duckdb::TupleDataAllocator::RowBlockCount ( ) const

Number of row blocks.

124915 {
124916 return row_blocks.size();
124917}

◆ HeapBlockCount()

idx_t duckdb::TupleDataAllocator::HeapBlockCount ( ) const

Number of heap blocks.

124919 {
124920 return heap_blocks.size();
124921}

◆ SetPartitionIndex()

void duckdb::TupleDataAllocator::SetPartitionIndex ( idx_t index)

Sets the partition index of this tuple data allocator.

124923 {
124924 D_ASSERT(!partition_index.IsValid());
124925 D_ASSERT(row_blocks.empty() && heap_blocks.empty());
124927}
optional_idx partition_index
Partition index (optional, if partitioned).
Definition duckdb.cpp:57535
index

◆ Build()

void duckdb::TupleDataAllocator::Build ( TupleDataSegment & segment,
TupleDataPinState & pin_state,
TupleDataChunkState & chunk_state,
const idx_t append_offset,
const idx_t append_count )

Builds out the chunks for next append, given the metadata in the append state.

124973 {
124974 D_ASSERT(this == segment.allocator.get());
124975 auto &chunks = segment.chunks;
124976 if (!chunks.empty()) {
124977 ReleaseOrStoreHandles(pin_state, segment, *chunks.back(), true);
124978 }
124979
124980 if (!BuildFastPath(segment, pin_state, chunk_state, append_offset, append_count)) {
124981 // Build the chunk parts for the incoming data
124982 chunk_state.chunk_part_indices.clear();
124983 idx_t offset = 0;
124984 while (offset != append_count) {
124985 if (chunks.empty() || chunks.back()->count == STANDARD_VECTOR_SIZE) {
124986 chunks.push_back(stl_allocator->MakeUnsafePtr<TupleDataChunk>(*stl_allocator->Make<mutex>()));
124987 }
124988 auto &chunk = *chunks.back();
124989
124990 // Build the next part
124991 auto next = MinValue<idx_t>(append_count - offset, STANDARD_VECTOR_SIZE - chunk.count);
124992 auto &chunk_part = chunk.AddPart(
124993 segment, BuildChunkPart(segment, pin_state, chunk_state, append_offset + offset, next, chunk));
124994 next = chunk_part.count;
124995
124996 segment.count += next;
124997 segment.data_size += chunk_part.count * layout.GetRowWidth();
124998 if (!layout.AllConstant()) {
124999 segment.data_size += chunk_part.total_heap_size;
125000 }
125001
125002 if (layout.HasDestructor()) {
125003 const auto base_row_ptr = GetRowPointer(pin_state, chunk_part);
125004 for (auto &aggr_idx : layout.GetAggregateDestructorIndices()) {
125005 const auto aggr_offset = layout.GetOffsets()[layout.ColumnCount() + aggr_idx];
125006 auto &aggr_fun = layout.GetAggregates()[aggr_idx];
125007 for (idx_t i = 0; i < next; i++) {
125008 duckdb::FastMemset(base_row_ptr + i * layout.GetRowWidth() + aggr_offset, '\0',
125009 aggr_fun.payload_size);
125010 }
125011 }
125012 }
125013
125014 offset += next;
125015 chunk_state.chunk_part_indices.emplace_back(chunks.size() - 1, chunk.part_ids.End() - 1);
125016 }
125017
125018 // Now initialize the pointers to write the data to
125019 chunk_state.chunk_parts.clear();
125020 for (const auto &indices : chunk_state.chunk_part_indices) {
125021 chunk_state.chunk_parts.emplace_back(*segment.chunk_parts[indices.second]);
125022 }
125023 InitializeChunkStateInternal(pin_state, chunk_state, append_offset, false, true, false,
125024 chunk_state.chunk_parts);
125025
125026 // To reduce metadata, we try to merge chunk parts where possible
125027 // Due to the way chunk parts are constructed, only the last part of the first chunk is eligible for merging
125028 segment.chunks[chunk_state.chunk_part_indices[0].first]->MergeLastChunkPart(segment);
125029 }
125030
125031 segment.Verify();
125032}
unsafe_arena_ptr< TupleDataChunkPart > BuildChunkPart(TupleDataSegment &segment, TupleDataPinState &pin_state, TupleDataChunkState &chunk_state, const idx_t append_offset, const idx_t append_count, TupleDataChunk &chunk)
Builds out a single part (grabs the lock).
Definition duckdb.cpp:125035
void ReleaseOrStoreHandles(TupleDataPinState &state, TupleDataSegment &segment, TupleDataChunk &chunk, bool release_heap)
Releases or stores any handles in the management state that are no longer required.
Definition duckdb.cpp:125540
data_ptr_t GetRowPointer(TupleDataPinState &state, const TupleDataChunkPart &part)
Gets the pointer to the rows for the given chunk part.
Definition duckdb.cpp:125636
void InitializeChunkStateInternal(TupleDataPinState &pin_state, TupleDataChunkState &chunk_state, idx_t offset, bool recompute, bool init_heap_pointers, bool init_heap_sizes, unsafe_vector< reference< TupleDataChunkPart > > &parts, optional_ptr< SortKeyPayloadState > sort_key_payload_state=nullptr)
Internal function for InitializeChunkState.
Definition duckdb.cpp:125210
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
Here is the call graph for this function:

◆ BuildFastPath()

bool duckdb::TupleDataAllocator::BuildFastPath ( TupleDataSegment & segment,
TupleDataPinState & pin_state,
TupleDataChunkState & chunk_state,
const idx_t append_offset,
const idx_t append_count )
124931 {
124932 if (!layout.AllConstant() || layout.HasDestructor()) {
124933 return false;
124934 }
124935
124936 auto &chunks = segment.chunks;
124937 if (chunks.empty()) {
124938 return false;
124939 }
124940
124941 auto &chunk = *chunks.back();
124942 if (chunk.count + append_count > STANDARD_VECTOR_SIZE) {
124943 return false;
124944 }
124945
124946 auto &part = *segment.chunk_parts[chunk.part_ids.End() - 1];
124947 auto &row_block = row_blocks[part.row_block_index];
124948
124949 const auto row_width = layout.GetRowWidth();
124950 const auto added_size = append_count * row_width;
124951 if (row_block.size + added_size > row_block.capacity) {
124952 return false;
124953 }
124954
124955 // We can do the fast path append!
124956 auto row_locations = FlatVector::GetData<data_ptr_t>(chunk_state.row_locations);
124957 const auto base_row_ptr = GetRowPointer(pin_state, part) + part.count * row_width;
124958 for (idx_t i = 0; i < append_count; i++) {
124959 row_locations[append_offset + i] = base_row_ptr + i * row_width;
124960 }
124961
124962 // Increment counts and sizes
124963 chunk.count += append_count;
124964 part.count += append_count;
124965 segment.count += append_count;
124966 row_block.size += added_size;
124967 segment.data_size += added_size;
124968
124969 return true;
124970}

◆ InitializeChunkState()

void duckdb::TupleDataAllocator::InitializeChunkState ( TupleDataSegment & segment,
TupleDataPinState & pin_state,
TupleDataChunkState & chunk_state,
idx_t chunk_idx,
bool init_heap,
optional_ptr< SortKeyPayloadState > sort_key_payload_state = nullptr )

Initializes a chunk, making its pointers valid.

125127 {
125128 D_ASSERT(this == segment.allocator.get());
125129 D_ASSERT(chunk_idx < segment.ChunkCount());
125130 auto &chunk = *segment.chunks[chunk_idx];
125131
125132 // Release or store any handles that are no longer required:
125133 // We can't release the heap here if the current chunk's heap_block_ids is empty, because if we are iterating with
125134 // PinProperties::DESTROY_AFTER_DONE, we might destroy a heap block that is needed by a later chunk, e.g.,
125135 // when chunk 0 needs heap block 0, chunk 1 does not need any heap blocks, and chunk 2 needs heap block 0 again
125136 ReleaseOrStoreHandles(pin_state, segment, chunk, !chunk.heap_block_ids.Empty());
125137
125138 chunk_state.chunk_parts.clear();
125139 for (auto part_id = chunk.part_ids.Start(); part_id < chunk.part_ids.End(); part_id++) {
125140 chunk_state.chunk_parts.emplace_back(*segment.chunk_parts[part_id]);
125141 }
125142
125143 InitializeChunkStateInternal(pin_state, chunk_state, 0, true, init_heap, init_heap, chunk_state.chunk_parts,
125144 sort_key_payload_state);
125145
125146 chunk_state.chunk_lock = &chunk.lock.get();
125147}
Here is the call graph for this function:

◆ RecomputeHeapPointers()

void duckdb::TupleDataAllocator::RecomputeHeapPointers ( Vector & old_heap_ptrs,
const SelectionVector & old_heap_sel,
const data_ptr_t row_locations[],
Vector & new_heap_ptrs,
const idx_t offset,
const idx_t count,
const TupleDataLayout & layout,
const idx_t base_col_offset )
static
125351 {
125352 if (layout.IsSortKeyLayout()) {
125353 switch (layout.GetSortKeyType()) {
125354 case SortKeyType::NO_PAYLOAD_VARIABLE_32:
125355 SortKeyRecomputeHeapPointers<SortKeyType::NO_PAYLOAD_VARIABLE_32>(
125356 old_heap_ptrs, old_heap_sel, row_locations, new_heap_ptrs, offset, count);
125357 break;
125358 case SortKeyType::PAYLOAD_VARIABLE_32:
125359 SortKeyRecomputeHeapPointers<SortKeyType::PAYLOAD_VARIABLE_32>(old_heap_ptrs, old_heap_sel, row_locations,
125360 new_heap_ptrs, offset, count);
125361 break;
125362 default:
125363 throw NotImplementedException("SortKeyRecomputeHeapPointers for %s",
125364 EnumUtil::ToString(layout.GetSortKeyType()));
125365 }
125366 return;
125367 }
125368
125369 const auto old_heap_locations = FlatVector::GetData<data_ptr_t>(old_heap_ptrs);
125370
125371 UnifiedVectorFormat new_heap_data;
125372 new_heap_ptrs.ToUnifiedFormat(offset + count, new_heap_data);
125373 const auto new_heap_locations = UnifiedVectorFormat::GetData<data_ptr_t>(new_heap_data);
125374 const auto new_heap_sel = *new_heap_data.sel;
125375
125376 const auto all_valid = layout.AllValid();
125377 const auto column_count = layout.ColumnCount();
125378
125379 for (const auto &col_idx : layout.GetVariableColumns()) {
125380 const auto &col_offset = layout.GetOffsets()[col_idx];
125381
125382 // Precompute mask indexes
125383 idx_t entry_idx;
125384 idx_t idx_in_entry;
125385 ValidityBytes::GetEntryIndex(col_idx, entry_idx, idx_in_entry);
125386
125387 const auto &type = layout.GetTypes()[col_idx];
125388 switch (type.InternalType()) {
125389 case PhysicalType::VARCHAR: {
125390 for (idx_t i = 0; i < count; i++) {
125391 const auto idx = offset + i;
125392 const auto &row_location = row_locations[idx] + base_col_offset;
125393 const auto valid =
125394 all_valid ||
125395 ValidityBytes::RowIsValid(
125396 ValidityBytes(row_location, column_count).GetValidityEntryUnsafe(entry_idx), idx_in_entry);
125397 if (!valid) {
125398 continue;
125399 }
125400
125401 const auto &old_heap_ptr = old_heap_locations[old_heap_sel.get_index(idx)];
125402 const auto &new_heap_ptr = new_heap_locations[new_heap_sel.get_index(idx)];
125403
125404 const auto string_location = row_location + col_offset;
125405 if (Load<uint32_t>(string_location) > string_t::INLINE_LENGTH) {
125406 const auto string_ptr_location = string_location + string_t::HEADER_SIZE;
125407 const auto string_ptr = Load<data_ptr_t>(string_ptr_location);
125408 const auto diff = string_ptr - old_heap_ptr;
125409 D_ASSERT(diff >= 0);
125410 Store<data_ptr_t>(new_heap_ptr + diff, string_ptr_location);
125411 }
125412 }
125413 VerifyStrings(layout, type.id(), row_locations, col_idx, base_col_offset, col_offset, offset, count);
125414 break;
125415 }
125416 case PhysicalType::LIST:
125417 case PhysicalType::ARRAY: {
125418 for (idx_t i = 0; i < count; i++) {
125419 const auto idx = offset + i;
125420 const auto &row_location = row_locations[idx] + base_col_offset;
125421 const auto valid =
125422 all_valid ||
125423 ValidityBytes::RowIsValid(
125424 ValidityBytes(row_location, column_count).GetValidityEntryUnsafe(entry_idx), idx_in_entry);
125425 if (!valid) {
125426 continue;
125427 }
125428
125429 const auto &old_heap_ptr = old_heap_locations[old_heap_sel.get_index(idx)];
125430 const auto &new_heap_ptr = new_heap_locations[new_heap_sel.get_index(idx)];
125431
125432 const auto &list_ptr_location = row_location + col_offset;
125433 const auto list_ptr = Load<data_ptr_t>(list_ptr_location);
125434 const auto diff = list_ptr - old_heap_ptr;
125435 D_ASSERT(diff >= 0);
125436 Store<data_ptr_t>(new_heap_ptr + diff, list_ptr_location);
125437 }
125438 break;
125439 }
125440 case PhysicalType::STRUCT: {
125441 const auto &struct_layout = layout.GetStructLayout(col_idx);
125442 if (!struct_layout.AllConstant()) {
125443 RecomputeHeapPointers(old_heap_ptrs, old_heap_sel, row_locations, new_heap_ptrs, offset, count,
125444 struct_layout, base_col_offset + col_offset);
125445 }
125446 break;
125447 }
125448 default:
125449 break;
125450 }
125451 }
125452}
@ LIST
A list of some logical data type.
Definition duckdb.hpp:2813
@ STRUCT
Struct of logical types.
Definition duckdb.hpp:2816
@ VARCHAR
DuckDB Extensions.
Definition duckdb.hpp:2847

◆ FindHeapPointers()

void duckdb::TupleDataAllocator::FindHeapPointers ( TupleDataChunkState & chunk_state,
SelectionVector & not_found,
idx_t & not_found_count,
const TupleDataLayout & layout,
const idx_t base_col_offset )
static
125455 {
125456 D_ASSERT(!layout.AllConstant());
125457 const auto row_locations = FlatVector::GetData<data_ptr_t>(chunk_state.row_locations);
125458 const auto heap_locations = FlatVector::GetData<data_ptr_t>(chunk_state.heap_locations);
125459
125460 const auto all_valid = layout.AllValid();
125461 const auto column_count = layout.ColumnCount();
125462
125463 for (const auto &col_idx : layout.GetVariableColumns()) {
125464 if (not_found_count == 0) {
125465 return;
125466 }
125467 const auto &col_offset = layout.GetOffsets()[col_idx];
125468
125469 // Precompute mask indexes
125470 idx_t entry_idx;
125471 idx_t idx_in_entry;
125472 ValidityBytes::GetEntryIndex(col_idx, entry_idx, idx_in_entry);
125473
125474 idx_t next_not_found_count = 0;
125475 const auto &type = layout.GetTypes()[col_idx];
125476 switch (type.InternalType()) {
125477 case PhysicalType::VARCHAR: {
125478 for (idx_t i = 0; i < not_found_count; i++) {
125479 const auto idx = not_found.get_index(i);
125480 const auto &row_location = row_locations[idx] + base_col_offset;
125481 D_ASSERT(FlatVector::GetData<idx_t>(chunk_state.heap_sizes)[idx] != 0);
125482
125483 // We always serialize a NullValue<string_t>, which isn't inlined if this build flag is enabled
125484 // So we need to grab the pointer from here even if the string is NULL
125485#ifndef DUCKDB_DEBUG_NO_INLINE
125486 const auto valid =
125487 all_valid ||
125488 ValidityBytes::RowIsValid(
125489 ValidityBytes(row_location, column_count).GetValidityEntryUnsafe(entry_idx), idx_in_entry);
125490 if (valid) {
125491#endif
125492 const auto string_location = row_location + col_offset;
125493 if (Load<uint32_t>(string_location) > string_t::INLINE_LENGTH) {
125494 const auto string_ptr_location = string_location + string_t::HEADER_SIZE;
125495 heap_locations[idx] = Load<data_ptr_t>(string_ptr_location);
125496 continue;
125497 }
125498#ifndef DUCKDB_DEBUG_NO_INLINE
125499 }
125500#endif
125501 not_found.set_index(next_not_found_count++, idx);
125502 }
125503 not_found_count = next_not_found_count;
125504 break;
125505 }
125506 case PhysicalType::LIST:
125507 case PhysicalType::ARRAY: {
125508 for (idx_t i = 0; i < not_found_count; i++) {
125509 const auto idx = not_found.get_index(i);
125510 const auto &row_location = row_locations[idx] + base_col_offset;
125511 D_ASSERT(FlatVector::GetData<idx_t>(chunk_state.heap_sizes)[idx] != 0);
125512
125513 const auto valid =
125514 all_valid ||
125515 ValidityBytes::RowIsValid(
125516 ValidityBytes(row_location, column_count).GetValidityEntryUnsafe(entry_idx), idx_in_entry);
125517 if (valid) {
125518 const auto &list_ptr_location = row_location + col_offset;
125519 heap_locations[idx] = Load<data_ptr_t>(list_ptr_location);
125520 continue;
125521 }
125522 not_found.set_index(next_not_found_count++, idx);
125523 }
125524 not_found_count = next_not_found_count;
125525 break;
125526 }
125527 case PhysicalType::STRUCT: {
125528 const auto &struct_layout = layout.GetStructLayout(col_idx);
125529 if (!struct_layout.AllConstant()) {
125530 FindHeapPointers(chunk_state, not_found, not_found_count, struct_layout, base_col_offset + col_offset);
125531 }
125532 break;
125533 }
125534 default:
125535 break;
125536 }
125537 }
125538}

◆ ReleaseOrStoreHandles() [1/2]

void duckdb::TupleDataAllocator::ReleaseOrStoreHandles ( TupleDataPinState & state,
TupleDataSegment & segment,
TupleDataChunk & chunk,
bool release_heap )

Releases or stores any handles in the management state that are no longer required.

125541 {
125542 D_ASSERT(this == segment.allocator.get());
125543 ReleaseOrStoreHandlesInternal(segment, segment.pinned_row_handles, pin_state.row_handles, chunk.row_block_ids,
125544 row_blocks, pin_state.properties);
125545 if (!layout.AllConstant() && release_heap) {
125546 ReleaseOrStoreHandlesInternal(segment, segment.pinned_heap_handles, pin_state.heap_handles,
125547 chunk.heap_block_ids, heap_blocks, pin_state.properties);
125548 }
125549}
static void ReleaseOrStoreHandlesInternal(TupleDataSegment &segment, unsafe_arena_vector< BufferHandle > &pinned_row_handles, buffer_handle_map_t &handles, const ContinuousIdSet &block_ids, unsafe_arena_vector< TupleDataBlock > &blocks, TupleDataPinProperties properties)
Internal function for ReleaseOrStoreHandles.
Definition duckdb.cpp:125557
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ReleaseOrStoreHandles() [2/2]

void duckdb::TupleDataAllocator::ReleaseOrStoreHandles ( TupleDataPinState & state,
TupleDataSegment & segment )

Releases or stores ALL handles in the management state.

125551 {
125552 mutex dummy_chunk_mutex;
125553 static TupleDataChunk DUMMY_CHUNK(dummy_chunk_mutex);
125554 ReleaseOrStoreHandles(pin_state, segment, DUMMY_CHUNK, true);
125555}
Here is the call graph for this function:

◆ SetDestroyBufferUponUnpin()

void duckdb::TupleDataAllocator::SetDestroyBufferUponUnpin ( )

Sets 'can_destroy' to true for all blocks so they aren't added to the eviction queue.

124859 {
124860 DestroyRowBlocks(0, row_blocks.size());
124861 if (!layout.AllConstant()) {
124862 DestroyHeapBlocks(0, heap_blocks.size());
124863 }
124864}
void DestroyRowBlocks(idx_t row_block_begin, idx_t row_block_end)
Destroy the blocks between the given indices.
Definition duckdb.cpp:124866
Here is the call graph for this function:

◆ DestroyRowBlocks()

void duckdb::TupleDataAllocator::DestroyRowBlocks ( idx_t row_block_begin,
idx_t row_block_end )

Destroy the blocks between the given indices.

124866 {
124867 if (row_block_begin == row_block_end) {
124868 return;
124869 }
124870 for (idx_t block_idx = row_block_begin; block_idx < row_block_end; block_idx++) {
124871 auto &block = row_blocks[block_idx];
124872 if (block.handle) {
124873 block.handle->GetMemory().SetDestroyBufferUpon(DestroyBufferUpon::UNPIN);
124874 }
124875 }
124876}
@ UNPIN
Destroy the data buffer upon eviction to storage (destroy instead of evict).
Definition duckdb.hpp:45581
Here is the caller graph for this function:

◆ DestroyHeapBlocks()

void duckdb::TupleDataAllocator::DestroyHeapBlocks ( idx_t heap_block_begin,
idx_t heap_block_end )
124878 {
124879 D_ASSERT(!layout.AllConstant());
124880 if (heap_block_begin == heap_block_end) {
124881 return;
124882 }
124883 for (idx_t block_idx = heap_block_begin; block_idx < heap_block_end; block_idx++) {
124884 auto &block = heap_blocks[block_idx];
124885 if (block.handle) {
124886 block.handle->GetMemory().SetDestroyBufferUpon(DestroyBufferUpon::UNPIN);
124887 }
124888 }
124889}

◆ BuildChunkPart()

unsafe_arena_ptr< TupleDataChunkPart > duckdb::TupleDataAllocator::BuildChunkPart ( TupleDataSegment & segment,
TupleDataPinState & pin_state,
TupleDataChunkState & chunk_state,
const idx_t append_offset,
const idx_t append_count,
TupleDataChunk & chunk )
private

Builds out a single part (grabs the lock).

125037 {
125038 D_ASSERT(append_count != 0);
125039 auto result_ptr = stl_allocator->MakeUnsafePtr<TupleDataChunkPart>(chunk.lock.get());
125040 auto &result = *result_ptr;
125041 const auto block_size = buffer_manager.GetBlockSize();
125042
125043 // Allocate row block (if needed)
125044 if (row_blocks.empty() || row_blocks.back().RemainingCapacity() < layout.GetRowWidth()) {
125045 CreateRowBlock(segment);
125046 if (partition_index.IsValid()) { // Set the eviction queue index logarithmically using RadixBits
125047 row_blocks.back().handle->GetMemory().SetEvictionQueueIndex(
125048 RadixPartitioning::RadixBits(partition_index.GetIndex()));
125049 }
125050 }
125051 result.row_block_index = NumericCast<uint32_t>(row_blocks.size() - 1);
125052 auto &row_block = row_blocks[result.row_block_index];
125053 result.row_block_offset = NumericCast<uint32_t>(row_block.size);
125054
125055 // Set count (might be reduced later when checking heap space)
125056 result.count = NumericCast<uint32_t>(MinValue(row_block.RemainingCapacity(layout.GetRowWidth()), append_count));
125057 if (!layout.AllConstant()) {
125058 const auto heap_sizes = FlatVector::GetData<idx_t>(chunk_state.heap_sizes);
125059
125060 // Compute total heap size first
125061 idx_t total_heap_size = 0;
125062 for (idx_t i = 0; i < result.count; i++) {
125063 const auto &heap_size = heap_sizes[append_offset + i];
125064 total_heap_size += heap_size;
125065 }
125066
125067 if (total_heap_size == 0) {
125068 result.SetHeapEmpty();
125069 } else {
125070 idx_t heap_remaining;
125071 if (!heap_blocks.empty() && heap_blocks.back().RemainingCapacity() >= heap_sizes[append_offset]) {
125072 // We have enough room for the current entry
125073 heap_remaining = heap_blocks.back().RemainingCapacity();
125074 } else {
125075 // We need to allocate a new block
125076 heap_remaining = MaxValue<idx_t>(block_size, heap_sizes[append_offset]);
125077 }
125078
125079 if (total_heap_size <= heap_remaining) {
125080 // Everything fits
125081 result.total_heap_size = total_heap_size;
125082 } else {
125083 // Not everything fits - determine how many we can read next
125084 result.total_heap_size = 0;
125085 for (idx_t i = 0; i < result.count; i++) {
125086 const auto &heap_size = heap_sizes[append_offset + i];
125087 if (result.total_heap_size + heap_size > heap_remaining) {
125088 result.count = NumericCast<uint32_t>(i);
125089 break;
125090 }
125091 result.total_heap_size += heap_size;
125092 }
125093 }
125094
125095 if (result.total_heap_size == 0) {
125096 result.SetHeapEmpty();
125097 } else {
125098 // Allocate heap block (if needed)
125099 if (heap_blocks.empty() || heap_blocks.back().RemainingCapacity() < heap_sizes[append_offset]) {
125100 const auto size = MaxValue<idx_t>(block_size, heap_sizes[append_offset]);
125101 CreateHeapBlock(segment, size);
125102 if (partition_index.IsValid()) { // Set the eviction queue index logarithmically using RadixBits
125103 heap_blocks.back().handle->GetMemory().SetEvictionQueueIndex(
125104 RadixPartitioning::RadixBits(partition_index.GetIndex()));
125105 }
125106 }
125107 result.heap_block_index = NumericCast<uint32_t>(heap_blocks.size() - 1);
125108 auto &heap_block = heap_blocks[result.heap_block_index];
125109 result.heap_block_offset = NumericCast<uint32_t>(heap_block.size);
125110
125111 // Mark this portion of the heap block as filled and set the pointer
125112 heap_block.size += result.total_heap_size;
125113 result.base_heap_ptr = GetBaseHeapPointer(pin_state, result);
125114 }
125115 }
125116 }
125117 D_ASSERT(result.count != 0 && result.count <= STANDARD_VECTOR_SIZE);
125118
125119 // Mark this portion of the row block as filled
125120 row_block.size += result.count * layout.GetRowWidth();
125121
125122 return result_ptr;
125123}
void CreateRowBlock(TupleDataSegment &segment)
Create a row/heap block, extend the pinned handles in the segment accordingly.
Definition duckdb.cpp:125598
data_ptr_t GetBaseHeapPointer(TupleDataPinState &state, const TupleDataChunkPart &part)
Gets the base pointer to the heap for the given chunk part.
Definition duckdb.cpp:125640
GOpaque< Size > size(const GMat &src)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ InitializeChunkStateInternal()

void duckdb::TupleDataAllocator::InitializeChunkStateInternal ( TupleDataPinState & pin_state,
TupleDataChunkState & chunk_state,
idx_t offset,
bool recompute,
bool init_heap_pointers,
bool init_heap_sizes,
unsafe_vector< reference< TupleDataChunkPart > > & parts,
optional_ptr< SortKeyPayloadState > sort_key_payload_state = nullptr )
private

Internal function for InitializeChunkState.

125214 {
125215 const auto row_locations = FlatVector::GetData<data_ptr_t>(chunk_state.row_locations);
125216 const auto heap_sizes = FlatVector::GetData<idx_t>(chunk_state.heap_sizes);
125217 const auto heap_locations = FlatVector::GetData<data_ptr_t>(chunk_state.heap_locations);
125218
125219 for (auto &part_ref : parts) {
125220 auto &part = part_ref.get();
125221 const auto next = part.count;
125222
125223 // Set up row locations for the scan
125224 const auto row_width = layout.GetRowWidth();
125225 const auto base_row_ptr = GetRowPointer(pin_state, part);
125226 for (idx_t i = 0; i < next; i++) {
125227 row_locations[offset + i] = base_row_ptr + i * row_width;
125228 }
125229
125230 if (sort_key_payload_state) {
125231 D_ASSERT(!layout.IsSortKeyLayout()); // This must be the payload collection
125232 lock_guard<mutex> guard(part.lock);
125233 SortKeySetPayload(row_locations, offset, next, *sort_key_payload_state);
125234 }
125235
125236 if (layout.AllConstant()) { // Can't have a heap
125237 offset += next;
125238 continue;
125239 }
125240
125241 if (part.total_heap_size == 0) {
125242 if (init_heap_sizes) { // No heap, but we need the heap sizes
125243 InitializeHeapSizes(row_locations, heap_sizes, offset, next, part, layout.GetHeapSizeOffset());
125244 }
125245 offset += next;
125246 continue;
125247 }
125248
125249 // Check if heap block has changed - re-compute the pointers within each row if so
125250 if (recompute && pin_state.properties != TupleDataPinProperties::ALREADY_PINNED) {
125251 const auto new_base_heap_ptr = GetBaseHeapPointer(pin_state, part);
125252 if (part.base_heap_ptr != new_base_heap_ptr) {
125253 lock_guard<mutex> guard(part.lock);
125254 const auto old_base_heap_ptr = part.base_heap_ptr;
125255 if (old_base_heap_ptr != new_base_heap_ptr) {
125256 Vector old_heap_ptrs(
125257 Value::POINTER(CastPointerToValue(old_base_heap_ptr + part.heap_block_offset)));
125258 Vector new_heap_ptrs(
125259 Value::POINTER(CastPointerToValue(new_base_heap_ptr + part.heap_block_offset)));
125260 RecomputeHeapPointers(old_heap_ptrs, *ConstantVector::ZeroSelectionVector(), row_locations,
125261 new_heap_ptrs, offset, next, layout, 0);
125262 part.base_heap_ptr = new_base_heap_ptr;
125263 }
125264 }
125265 }
125266
125267 if (init_heap_sizes) {
125268 InitializeHeapSizes(row_locations, heap_sizes, offset, next, part, layout.GetHeapSizeOffset());
125269 }
125270
125271 if (init_heap_pointers) {
125272 // Set the pointers where the heap data will be written (if needed)
125273 heap_locations[offset] = part.base_heap_ptr + part.heap_block_offset;
125274 for (idx_t i = 1; i < next; i++) {
125275 auto idx = offset + i;
125276 heap_locations[idx] = heap_locations[idx - 1] + heap_sizes[idx - 1];
125277 }
125278 }
125279
125280 offset += next;
125281 }
125282 D_ASSERT(offset <= STANDARD_VECTOR_SIZE);
125283}
static DUCKDB_API Value POINTER(uintptr_t value)
Create a pointer Value from a specified value.
Definition duckdb.cpp:132331
@ ALREADY_PINNED
Assumes all blocks are already pinned (for reading only).
Definition duckdb.cpp:56605
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ReleaseOrStoreHandlesInternal()

void duckdb::TupleDataAllocator::ReleaseOrStoreHandlesInternal ( TupleDataSegment & segment,
unsafe_arena_vector< BufferHandle > & pinned_row_handles,
buffer_handle_map_t & handles,
const ContinuousIdSet & block_ids,
unsafe_arena_vector< TupleDataBlock > & blocks,
TupleDataPinProperties properties )
staticprivate

Internal function for ReleaseOrStoreHandles.

125561 {
125562 bool found_handle;
125563 do {
125564 found_handle = false;
125565 for (auto it = handles.begin(); it != handles.end(); it++) {
125566 const auto block_id = it->first;
125567 if (block_ids.Contains(block_id)) {
125568 // still required: do not release
125569 continue;
125570 }
125571 switch (properties) {
125573 lock_guard<mutex> guard(segment.pinned_handles_lock);
125574 D_ASSERT(blocks.size() == pinned_handles.size());
125575 pinned_handles[block_id] = std::move(it->second);
125576 break;
125577 }
125580 break;
125582 // Prevent it from being added to the eviction queue
125583 blocks[block_id].handle->GetMemory().SetDestroyBufferUpon(DestroyBufferUpon::UNPIN);
125584 // Destroy
125585 blocks[block_id].handle.reset();
125586 break;
125587 default:
125588 D_ASSERT(properties == TupleDataPinProperties::INVALID);
125589 throw InternalException("Encountered TupleDataPinProperties::INVALID");
125590 }
125591 handles.erase(it);
125592 found_handle = true;
125593 break;
125594 }
125595 } while (found_handle);
125596}
@ KEEP_EVERYTHING_PINNED
Keeps all passed blocks pinned while scanning/iterating over the chunks (for both reading/writing).
Definition duckdb.cpp:56599
@ DESTROY_AFTER_DONE
Destroys blocks after they are done (for reading only).
Definition duckdb.cpp:56603
@ UNPIN_AFTER_DONE
Unpins blocks after they are done (for both reading/writing).
Definition duckdb.cpp:56601
Here is the caller graph for this function:

◆ CreateRowBlock()

void duckdb::TupleDataAllocator::CreateRowBlock ( TupleDataSegment & segment)
private

Create a row/heap block, extend the pinned handles in the segment accordingly.

125598 {
125599 row_blocks.emplace_back(buffer_manager, tag, buffer_manager.GetBlockSize());
125600 segment.pinned_row_handles.resize(row_blocks.size());
125601}
Here is the caller graph for this function:

◆ CreateHeapBlock()

void duckdb::TupleDataAllocator::CreateHeapBlock ( TupleDataSegment & segment,
idx_t size )
private
125603 {
125604 heap_blocks.emplace_back(buffer_manager, tag, size);
125605 segment.pinned_heap_handles.resize(heap_blocks.size());
125606}

◆ PinRowBlock()

BufferHandle & duckdb::TupleDataAllocator::PinRowBlock ( TupleDataPinState & state,
const TupleDataChunkPart & part )
private

Pins the given row block.

125608 {
125609 const auto &row_block_index = part.row_block_index;
125610 auto it = pin_state.row_handles.find(row_block_index);
125611 if (it == pin_state.row_handles.end()) {
125612 D_ASSERT(row_block_index < row_blocks.size());
125613 auto &row_block = row_blocks[row_block_index];
125614 D_ASSERT(row_block.handle);
125615 D_ASSERT(part.row_block_offset < row_block.size);
125616 D_ASSERT(part.row_block_offset + part.count * layout.GetRowWidth() <= row_block.size);
125617 it = pin_state.row_handles.emplace(row_block_index, buffer_manager.Pin(row_block.handle)).first;
125618 }
125619 return it->second;
125620}
Here is the caller graph for this function:

◆ PinHeapBlock()

BufferHandle & duckdb::TupleDataAllocator::PinHeapBlock ( TupleDataPinState & state,
const TupleDataChunkPart & part )
private

Pins the given heap block.

125622 {
125623 const auto &heap_block_index = part.heap_block_index;
125624 auto it = pin_state.heap_handles.find(heap_block_index);
125625 if (it == pin_state.heap_handles.end()) {
125626 D_ASSERT(heap_block_index < heap_blocks.size());
125627 auto &heap_block = heap_blocks[heap_block_index];
125628 D_ASSERT(heap_block.handle);
125629 D_ASSERT(part.heap_block_offset < heap_block.size);
125630 D_ASSERT(part.heap_block_offset + part.total_heap_size <= heap_block.size);
125631 it = pin_state.heap_handles.emplace(heap_block_index, buffer_manager.Pin(heap_block.handle)).first;
125632 }
125633 return it->second;
125634}
Here is the caller graph for this function:

◆ GetRowPointer()

data_ptr_t duckdb::TupleDataAllocator::GetRowPointer ( TupleDataPinState & state,
const TupleDataChunkPart & part )
private

Gets the pointer to the rows for the given chunk part.

125636 {
125637 return PinRowBlock(pin_state, part).Ptr() + part.row_block_offset;
125638}
data_ptr_t Ptr() const
Returns a pointer to the buffer data. Handle must be valid.
Definition duckdb.hpp:9927
BufferHandle & PinRowBlock(TupleDataPinState &state, const TupleDataChunkPart &part)
Pins the given row block.
Definition duckdb.cpp:125608
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetBaseHeapPointer()

data_ptr_t duckdb::TupleDataAllocator::GetBaseHeapPointer ( TupleDataPinState & state,
const TupleDataChunkPart & part )
private

Gets the base pointer to the heap for the given chunk part.

125640 {
125641 return PinHeapBlock(pin_state, part).Ptr();
125642}
BufferHandle & PinHeapBlock(TupleDataPinState &state, const TupleDataChunkPart &part)
Pins the given heap block.
Definition duckdb.cpp:125622
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following file: