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::UncompressedStringStorage Struct Reference

Static Public Member Functions

static unique_ptr< AnalyzeState > StringInitAnalyze (ColumnData &col_data, PhysicalType type)
static bool StringAnalyze (AnalyzeState &state_p, Vector &input, idx_t count)
static idx_t StringFinalAnalyze (AnalyzeState &state_p)
static unique_ptr< SegmentScanState > StringInitScan (const QueryContext &context, ColumnSegment &segment)
static void StringScanPartial (ColumnSegment &segment, ColumnScanState &state, idx_t scan_count, Vector &result, idx_t result_offset)
static void StringScan (ColumnSegment &segment, ColumnScanState &state, idx_t scan_count, Vector &result)
static void Select (ColumnSegment &segment, ColumnScanState &state, idx_t vector_count, Vector &result, const SelectionVector &sel, idx_t sel_count)
static void StringFetchRow (ColumnSegment &segment, ColumnFetchState &state, row_t row_id, Vector &result, idx_t result_idx)
static unique_ptr< CompressedSegmentState > StringInitSegment (ColumnSegment &segment, block_id_t block_id, optional_ptr< ColumnSegmentState > segment_state)
static unique_ptr< CompressionAppendState > StringInitAppend (ColumnSegment &segment)
static idx_t StringAppend (CompressionAppendState &append_state, ColumnSegment &segment, SegmentStatistics &stats, UnifiedVectorFormat &data, idx_t offset, idx_t count)
static idx_t StringAppendBase (ColumnSegment &segment, SegmentStatistics &stats, UnifiedVectorFormat &data, idx_t offset, idx_t count)
static idx_t StringAppendBase (BufferHandle &handle, ColumnSegment &segment, SegmentStatistics &stats, UnifiedVectorFormat &data, idx_t offset, idx_t count)
static void StringRevertAppend (ColumnSegment &segment, idx_t new_count)
static idx_t FinalizeAppend (ColumnSegment &segment, SegmentStatistics &stats)
static void UpdateStringStats (SegmentStatistics &stats, const string_t &new_value)
static void SetDictionary (ColumnSegment &segment, BufferHandle &handle, StringDictionaryContainer dict)
static StringDictionaryContainer GetDictionary (ColumnSegment &segment, BufferHandle &handle)
static uint32_t GetDictionaryEnd (ColumnSegment &segment, BufferHandle &handle)
static idx_t RemainingSpace (ColumnSegment &segment, BufferHandle &handle)
static void WriteString (ColumnSegment &segment, string_t string, block_id_t &result_block, int32_t &result_offset)
static void WriteStringMemory (ColumnSegment &segment, string_t string, block_id_t &result_block, int32_t &result_offset)
static string_t ReadOverflowString (ColumnSegment &segment, Vector &result, block_id_t block, int32_t offset)
static string_t ReadString (data_ptr_t target, int32_t offset, uint32_t string_length)
static string_t ReadStringWithLength (data_ptr_t target, int32_t offset)
static void WriteStringMarker (data_ptr_t target, block_id_t block_id, int32_t offset)
static void ReadStringMarker (data_ptr_t target, block_id_t &block_id, int32_t &offset)
static string_t FetchStringFromDict (ColumnSegment &segment, uint32_t dict_end_offset, Vector &result, data_ptr_t base_ptr, int32_t dict_offset, uint32_t string_length)
static unique_ptr< ColumnSegmentState > SerializeState (ColumnSegment &segment)
static unique_ptr< ColumnSegmentState > DeserializeState (Deserializer &deserializer)
static void VisitBlockIds (const ColumnSegment &segment, BlockIdVisitor &visitor)

Static Public Attributes

static constexpr uint16_t DICTIONARY_HEADER_SIZE = sizeof(uint32_t) + sizeof(uint32_t)
 Dictionary header size at the beginning of the string segment (offset + length).
static constexpr uint16_t BIG_STRING_MARKER = (uint16_t)-1
 Marker used in length field to indicate the presence of a big string.
static constexpr idx_t BIG_STRING_MARKER_BASE_SIZE = sizeof(block_id_t) + sizeof(int32_t)
 Base size of big string marker (block id + offset).
static constexpr idx_t BIG_STRING_MARKER_SIZE = BIG_STRING_MARKER_BASE_SIZE
 The marker size of the big string.

Member Function Documentation

◆ StringInitAnalyze()

unique_ptr< AnalyzeState > duckdb::UncompressedStringStorage::StringInitAnalyze ( ColumnData & col_data,
PhysicalType type )
static
405656 {
405657 CompressionInfo info(col_data.GetBlockManager());
405658 return make_uniq<StringAnalyzeState>(info);
405659}

◆ StringAnalyze()

bool duckdb::UncompressedStringStorage::StringAnalyze ( AnalyzeState & state_p,
Vector & input,
idx_t count )
static
405661 {
405662 auto &state = state_p.Cast<StringAnalyzeState>();
405663 UnifiedVectorFormat vdata;
405664 input.ToUnifiedFormat(count, vdata);
405665
405666 state.count += count;
405667 auto data = UnifiedVectorFormat::GetData<string_t>(vdata);
405668 for (idx_t i = 0; i < count; i++) {
405669 auto idx = vdata.sel->get_index(i);
405670 if (vdata.validity.RowIsValid(idx)) {
405671 auto string_size = data[idx].GetSize();
405672 state.total_string_size += string_size;
405673 if (string_size >= StringUncompressed::GetStringBlockLimit(state.info.GetBlockSize())) {
405674 state.overflow_strings++;
405675 }
405676 }
405677 }
405678 return true;
405679}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ StringFinalAnalyze()

idx_t duckdb::UncompressedStringStorage::StringFinalAnalyze ( AnalyzeState & state_p)
static
405681 {
405682 auto &state = state_p.Cast<StringAnalyzeState>();
405683 return state.count * sizeof(int32_t) + state.total_string_size + state.overflow_strings * BIG_STRING_MARKER_SIZE;
405684}
::int32_t int32_t
static constexpr idx_t BIG_STRING_MARKER_SIZE
The marker size of the big string.
Definition duckdb.cpp:75219

◆ StringInitScan()

unique_ptr< SegmentScanState > duckdb::UncompressedStringStorage::StringInitScan ( const QueryContext & context,
ColumnSegment & segment )
static
405703 {
405704 auto result = make_uniq<StringScanState>();
405705 auto &buffer_manager = BufferManager::GetBufferManager(segment.db);
405706 result->handle = buffer_manager.Pin(segment.block);
405707 return std::move(result);
405708}

◆ StringScanPartial()

void duckdb::UncompressedStringStorage::StringScanPartial ( ColumnSegment & segment,
ColumnScanState & state,
idx_t scan_count,
Vector & result,
idx_t result_offset )
static
405714 {
405715 // clear any previously locked buffers and get the primary buffer handle
405716 auto &scan_state = state.scan_state->Cast<StringScanState>();
405717 auto start = state.GetPositionInSegment();
405718
405719 auto baseptr = scan_state.handle.Ptr() + segment.GetBlockOffset();
405720 auto dict_end = GetDictionaryEnd(segment, scan_state.handle);
405721 auto base_data = reinterpret_cast<int32_t *>(baseptr + DICTIONARY_HEADER_SIZE);
405722 auto result_data = FlatVector::GetData<string_t>(result);
405723
405724 int32_t previous_offset = start > 0 ? base_data[start - 1] : 0;
405725
405726 for (idx_t i = 0; i < scan_count; i++) {
405727 // std::abs used since offsets can be negative to indicate big strings
405728 auto current_offset = base_data[start + i];
405729 auto string_length = UnsafeNumericCast<uint32_t>(std::abs(current_offset) - std::abs(previous_offset));
405730 result_data[result_offset + i] =
405731 FetchStringFromDict(segment, dict_end, result, baseptr, current_offset, string_length);
405732 previous_offset = base_data[start + i];
405733 }
405734}
static constexpr uint16_t DICTIONARY_HEADER_SIZE
Dictionary header size at the beginning of the string segment (offset + length).
Definition duckdb.cpp:75213

◆ StringScan()

void duckdb::UncompressedStringStorage::StringScan ( ColumnSegment & segment,
ColumnScanState & state,
idx_t scan_count,
Vector & result )
static
405737 {
405738 StringScanPartial(segment, state, scan_count, result, 0);
405739}

◆ Select()

void duckdb::UncompressedStringStorage::Select ( ColumnSegment & segment,
ColumnScanState & state,
idx_t vector_count,
Vector & result,
const SelectionVector & sel,
idx_t sel_count )
static
405745 {
405746 // clear any previously locked buffers and get the primary buffer handle
405747 auto &scan_state = state.scan_state->Cast<StringScanState>();
405748 auto start = state.GetPositionInSegment();
405749
405750 auto baseptr = scan_state.handle.Ptr() + segment.GetBlockOffset();
405751 auto dict_end = GetDictionaryEnd(segment, scan_state.handle);
405752 auto base_data = reinterpret_cast<int32_t *>(baseptr + DICTIONARY_HEADER_SIZE);
405753 auto result_data = FlatVector::GetData<string_t>(result);
405754
405755 for (idx_t i = 0; i < sel_count; i++) {
405756 idx_t index = start + sel.get_index(i);
405757 auto current_offset = base_data[index];
405758 auto prev_offset = index > 0 ? base_data[index - 1] : 0;
405759 auto string_length = UnsafeNumericCast<uint32_t>(std::abs(current_offset) - std::abs(prev_offset));
405760 result_data[i] = FetchStringFromDict(segment, dict_end, result, baseptr, current_offset, string_length);
405761 }
405762}
index

◆ StringFetchRow()

void duckdb::UncompressedStringStorage::StringFetchRow ( ColumnSegment & segment,
ColumnFetchState & state,
row_t row_id,
Vector & result,
idx_t result_idx )
static
405784 {
405785 // fetch a single row from the string segment
405786 // first pin the main buffer if it is not already pinned
405787 auto &handle = state.GetOrInsertHandle(segment);
405788
405789 auto baseptr = handle.Ptr() + segment.GetBlockOffset();
405790 auto dict_end = GetDictionaryEnd(segment, handle);
405791 auto base_data = reinterpret_cast<int32_t *>(baseptr + DICTIONARY_HEADER_SIZE);
405792 auto result_data = FlatVector::GetData<string_t>(result);
405793
405794 auto dict_offset = base_data[row_id];
405795 uint32_t string_length;
405796 if (DUCKDB_UNLIKELY(row_id == 0LL)) {
405797 // edge case where this is the first string in the dict
405798 string_length = NumericCast<uint32_t>(std::abs(dict_offset));
405799 } else {
405800 string_length = NumericCast<uint32_t>(std::abs(dict_offset) - std::abs(base_data[row_id - 1]));
405801 }
405802 result_data[result_idx] = FetchStringFromDict(segment, dict_end, result, baseptr, dict_offset, string_length);
405803}
::uint32_t uint32_t

◆ StringInitSegment()

unique_ptr< CompressedSegmentState > duckdb::UncompressedStringStorage::StringInitSegment ( ColumnSegment & segment,
block_id_t block_id,
optional_ptr< ColumnSegmentState > segment_state )
static
405821 {
405822 auto &buffer_manager = BufferManager::GetBufferManager(segment.db);
405823 if (block_id == INVALID_BLOCK) {
405824 auto handle = buffer_manager.Pin(segment.block);
405825 StringDictionaryContainer dictionary;
405826 dictionary.size = 0;
405827 dictionary.end = UnsafeNumericCast<uint32_t>(segment.SegmentSize());
405828 SetDictionary(segment, handle, dictionary);
405829 }
405830 auto result = make_uniq<UncompressedStringSegmentState>();
405831 if (segment_state) {
405832 auto &serialized_state = segment_state->Cast<SerializedStringSegmentState>();
405833 result->on_disk_blocks = std::move(serialized_state.blocks);
405834 }
405835 return std::move(result);
405836}

◆ StringInitAppend()

unique_ptr< CompressionAppendState > duckdb::UncompressedStringStorage::StringInitAppend ( ColumnSegment & segment)
inlinestatic
75237 {
75238 auto &buffer_manager = BufferManager::GetBufferManager(segment.db);
75239 // This block was initialized in StringInitSegment
75240 auto handle = buffer_manager.Pin(segment.block);
75241 return make_uniq<CompressionAppendState>(std::move(handle));
75242 }

◆ StringAppend()

idx_t duckdb::UncompressedStringStorage::StringAppend ( CompressionAppendState & append_state,
ColumnSegment & segment,
SegmentStatistics & stats,
UnifiedVectorFormat & data,
idx_t offset,
idx_t count )
inlinestatic
75245 {
75246 return StringAppendBase(append_state.handle, segment, stats, data, offset, count);
75247 }

◆ StringAppendBase() [1/2]

idx_t duckdb::UncompressedStringStorage::StringAppendBase ( ColumnSegment & segment,
SegmentStatistics & stats,
UnifiedVectorFormat & data,
idx_t offset,
idx_t count )
inlinestatic
75250 {
75251 auto &buffer_manager = BufferManager::GetBufferManager(segment.db);
75252 auto handle = buffer_manager.Pin(segment.block);
75253 return StringAppendBase(handle, segment, stats, data, offset, count);
75254 }

◆ StringAppendBase() [2/2]

idx_t duckdb::UncompressedStringStorage::StringAppendBase ( BufferHandle & handle,
ColumnSegment & segment,
SegmentStatistics & stats,
UnifiedVectorFormat & data,
idx_t offset,
idx_t count )
inlinestatic
75257 {
75258 D_ASSERT(segment.GetBlockOffset() == 0);
75259 auto handle_ptr = handle.Ptr();
75260 auto source_data = UnifiedVectorFormat::GetData<string_t>(data);
75261 auto result_data = reinterpret_cast<int32_t *>(handle_ptr + DICTIONARY_HEADER_SIZE);
75262 auto dictionary_size = reinterpret_cast<uint32_t *>(handle_ptr);
75263 auto dictionary_end = reinterpret_cast<uint32_t *>(handle_ptr + sizeof(uint32_t));
75264
75265 idx_t remaining_space = RemainingSpace(segment, handle);
75266 auto base_count = segment.count.load();
75267 for (idx_t i = 0; i < count; i++) {
75268 auto source_idx = data.sel->get_index(offset + i);
75269 auto target_idx = base_count + i;
75270 if (remaining_space < sizeof(int32_t)) {
75271 // string index does not fit in the block at all
75272 segment.count += i;
75273 return i;
75274 }
75275 remaining_space -= sizeof(int32_t);
75276 const bool is_null = !data.validity.RowIsValid(source_idx);
75277 if (is_null) {
75278 stats.statistics.SetHasNullFast();
75279 // null value is stored as a copy of the last value, this is done to be able to efficiently do the
75280 // string_length calculation
75281 if (target_idx > 0) {
75282 result_data[target_idx] = result_data[target_idx - 1];
75283 } else {
75284 result_data[target_idx] = 0;
75285 }
75286 continue;
75287 }
75288 auto end = handle.Ptr() + *dictionary_end;
75289
75290#ifdef DEBUG
75291 GetDictionary(segment, handle).Verify(segment.GetBlockSize());
75292#endif
75293 // Unknown string, continue
75294 // non-null value, check if we can fit it within the block
75295 idx_t string_length = source_data[source_idx].GetSize();
75296
75297 // determine whether or not we have space in the block for this string
75298 bool use_overflow_block = false;
75299 idx_t required_space = string_length;
75300 if (DUCKDB_UNLIKELY(required_space >= StringUncompressed::GetStringBlockLimit(segment.GetBlockSize()))) {
75301 // string exceeds block limit, store in overflow block and only write a marker here
75302 required_space = BIG_STRING_MARKER_SIZE;
75303 use_overflow_block = true;
75304 }
75305 if (DUCKDB_UNLIKELY(required_space > remaining_space)) {
75306 // no space remaining: return how many tuples we ended up writing
75307 segment.count += i;
75308 return i;
75309 }
75310
75311 // we have space: write the string
75312 UpdateStringStats(stats, source_data[source_idx]);
75313
75314 if (DUCKDB_UNLIKELY(use_overflow_block)) {
75315 // write to overflow blocks
75316 block_id_t block;
75317 int32_t current_offset;
75318 // write the string into the current string block
75319 WriteString(segment, source_data[source_idx], block, current_offset);
75320 *dictionary_size += BIG_STRING_MARKER_SIZE;
75321 remaining_space -= BIG_STRING_MARKER_SIZE;
75322 auto dict_pos = end - *dictionary_size;
75323
75324 // write a big string marker into the dictionary
75325 WriteStringMarker(dict_pos, block, current_offset);
75326
75327 // place the dictionary offset into the set of vectors
75328 // note: for overflow strings we write negative value
75329
75330 // dictionary_size is an uint32_t value, so we can cast up.
75331 D_ASSERT(NumericCast<idx_t>(*dictionary_size) <= segment.GetBlockSize());
75332 result_data[target_idx] = -NumericCast<int32_t>((*dictionary_size));
75333 } else {
75334 // string fits in block, append to dictionary and increment dictionary position
75335 D_ASSERT(string_length < NumericLimits<uint16_t>::Maximum());
75336 *dictionary_size += required_space;
75337 remaining_space -= required_space;
75338 auto dict_pos = end - *dictionary_size;
75339 // now write the actual string data into the dictionary
75340 memcpy(dict_pos, source_data[source_idx].GetData(), string_length);
75341
75342 // dictionary_size is an uint32_t value, so we can cast up.
75343 D_ASSERT(NumericCast<idx_t>(*dictionary_size) <= segment.GetBlockSize());
75344 // Place the dictionary offset into the set of vectors.
75345 result_data[target_idx] = NumericCast<int32_t>(*dictionary_size);
75346 }
75347 D_ASSERT(RemainingSpace(segment, handle) <= segment.GetBlockSize());
75348#ifdef DEBUG
75349 GetDictionary(segment, handle).Verify(segment.GetBlockSize());
75350#endif
75351 }
75352 segment.count += count;
75353 return count;
75354 }

◆ StringRevertAppend()

void duckdb::UncompressedStringStorage::StringRevertAppend ( ColumnSegment & segment,
idx_t new_count )
inlinestatic
75356 {
75357 if (new_count >= segment.count) {
75358 return;
75359 }
75360 // we need to decrement the dictionary size by all of the strings we are erasing
75361 auto &buffer_manager = BufferManager::GetBufferManager(segment.db);
75362 auto handle = buffer_manager.Pin(segment.block);
75363 auto handle_ptr = handle.Ptr();
75364 auto result_data = reinterpret_cast<int32_t *>(handle_ptr + DICTIONARY_HEADER_SIZE);
75365 auto dictionary_size = reinterpret_cast<uint32_t *>(handle_ptr);
75366 uint32_t new_dictionary_size;
75367 if (new_count == 0) {
75368 new_dictionary_size = 0;
75369 } else {
75370 auto entry_offset = result_data[new_count - 1];
75371 if (entry_offset < 0) {
75372 // overflow strings store the dict offset negatively - invert size
75373 new_dictionary_size = -entry_offset;
75374 } else {
75375 new_dictionary_size = entry_offset;
75376 }
75377 }
75378 *dictionary_size = new_dictionary_size;
75379 }

◆ FinalizeAppend()

idx_t duckdb::UncompressedStringStorage::FinalizeAppend ( ColumnSegment & segment,
SegmentStatistics & stats )
static
405838 {
405839 auto &buffer_manager = BufferManager::GetBufferManager(segment.db);
405840 auto handle = buffer_manager.Pin(segment.block);
405841 auto dict = GetDictionary(segment, handle);
405842 D_ASSERT(dict.end == segment.SegmentSize());
405843 // compute the total size required to store this segment
405844 auto offset_size = DICTIONARY_HEADER_SIZE + segment.count * sizeof(int32_t);
405845 auto total_size = offset_size + dict.size;
405846
405847 CompressionInfo info(segment.block->GetBlockManager());
405848 if (total_size >= info.GetCompactionFlushLimit()) {
405849 // the block is full enough, don't bother moving around the dictionary
405850 return segment.SegmentSize();
405851 }
405852
405853 // the block has space left: figure out how much space we can save
405854 auto move_amount = segment.SegmentSize() - total_size;
405855 // move the dictionary so it lines up exactly with the offsets
405856 auto dataptr = handle.Ptr();
405857 memmove(dataptr + offset_size, dataptr + dict.end - dict.size, dict.size);
405858 dict.end -= move_amount;
405859 D_ASSERT(dict.end == total_size);
405860 // write the new dictionary (with the updated "end")
405861 SetDictionary(segment, handle, dict);
405862 return total_size;
405863}

◆ UpdateStringStats()

void duckdb::UncompressedStringStorage::UpdateStringStats ( SegmentStatistics & stats,
const string_t & new_value )
inlinestatic
75384 {
75385 stats.statistics.SetHasNoNullFast();
75386 if (stats.statistics.GetStatsType() == StatisticsType::GEOMETRY_STATS) {
75387 GeometryStats::Update(stats.statistics, new_value);
75388 } else {
75389 StringStats::Update(stats.statistics, new_value);
75390 }
75391 }
static DUCKDB_API void Update(BaseStatistics &stats, const string_t &value)
Definition duckdb.cpp:424643

◆ SetDictionary()

void duckdb::UncompressedStringStorage::SetDictionary ( ColumnSegment & segment,
BufferHandle & handle,
StringDictionaryContainer dict )
static
405913 {
405914 auto startptr = handle.Ptr() + segment.GetBlockOffset();
405915 Store<uint32_t>(container.size, startptr);
405916 Store<uint32_t>(container.end, startptr + sizeof(uint32_t));
405917}

◆ GetDictionary()

StringDictionaryContainer duckdb::UncompressedStringStorage::GetDictionary ( ColumnSegment & segment,
BufferHandle & handle )
static
405919 {
405920 auto startptr = handle.Ptr() + segment.GetBlockOffset();
405921 StringDictionaryContainer container;
405922 container.size = Load<uint32_t>(startptr);
405923 container.end = Load<uint32_t>(startptr + sizeof(uint32_t));
405924 return container;
405925}

◆ GetDictionaryEnd()

uint32_t duckdb::UncompressedStringStorage::GetDictionaryEnd ( ColumnSegment & segment,
BufferHandle & handle )
static
405927 {
405928 auto startptr = handle.Ptr() + segment.GetBlockOffset();
405929 return Load<uint32_t>(startptr + sizeof(uint32_t));
405930}

◆ RemainingSpace()

idx_t duckdb::UncompressedStringStorage::RemainingSpace ( ColumnSegment & segment,
BufferHandle & handle )
static
405932 {
405933 auto dictionary = GetDictionary(segment, handle);
405934 D_ASSERT(dictionary.end == segment.SegmentSize());
405935 idx_t used_space = dictionary.size + segment.count * sizeof(int32_t) + DICTIONARY_HEADER_SIZE;
405936 D_ASSERT(segment.SegmentSize() >= used_space);
405937 return segment.SegmentSize() - used_space;
405938}

◆ WriteString()

void duckdb::UncompressedStringStorage::WriteString ( ColumnSegment & segment,
string_t string,
block_id_t & result_block,
int32_t & result_offset )
static
405941 {
405942 auto &state = segment.GetSegmentState()->Cast<UncompressedStringSegmentState>();
405943 if (state.overflow_writer) {
405944 // overflow writer is set: write string there
405945 state.overflow_writer->WriteString(state, string, result_block, result_offset);
405946 } else {
405947 // default overflow behavior: use in-memory buffer to store the overflow string
405948 WriteStringMemory(segment, string, result_block, result_offset);
405949 }
405950}

◆ WriteStringMemory()

void duckdb::UncompressedStringStorage::WriteStringMemory ( ColumnSegment & segment,
string_t string,
block_id_t & result_block,
int32_t & result_offset )
static
405953 {
405954 auto total_length = UnsafeNumericCast<uint32_t>(string.GetSize() + sizeof(uint32_t));
405955 shared_ptr<BlockHandle> block;
405956 BufferHandle handle;
405957
405958 auto &buffer_manager = BufferManager::GetBufferManager(segment.db);
405959 auto &state = segment.GetSegmentState()->Cast<UncompressedStringSegmentState>();
405960 // check if the string fits in the current block
405961 if (!state.head || state.head->offset + total_length >= state.head->size) {
405962 // string does not fit, allocate space for it
405963 // create a new string block
405964 auto alloc_size = MaxValue<idx_t>(total_length, segment.GetBlockSize());
405965 auto new_block = make_uniq<StringBlock>();
405966 new_block->offset = 0;
405967 new_block->size = alloc_size;
405968 // allocate an in-memory buffer for it
405969 handle = buffer_manager.Allocate(MemoryTag::OVERFLOW_STRINGS, alloc_size, false);
405970 block = handle.GetBlockHandle();
405971 state.InsertOverflowBlock(block->BlockId(), reference<StringBlock>(*new_block));
405972 new_block->block = std::move(block);
405973 new_block->next = std::move(state.head);
405974 state.head = std::move(new_block);
405975 } else {
405976 // string fits, copy it into the current block
405977 handle = buffer_manager.Pin(state.head->block);
405978 }
405979
405980 result_block = state.head->block->BlockId();
405981 result_offset = UnsafeNumericCast<int32_t>(state.head->offset);
405982
405983 // copy the string and the length there
405984 auto ptr = handle.Ptr() + state.head->offset;
405985 Store<uint32_t>(UnsafeNumericCast<uint32_t>(string.GetSize()), ptr);
405986 ptr += sizeof(uint32_t);
405987 memcpy(ptr, string.GetData(), string.GetSize());
405988 state.head->offset += total_length;
405989}

◆ ReadOverflowString()

string_t duckdb::UncompressedStringStorage::ReadOverflowString ( ColumnSegment & segment,
Vector & result,
block_id_t block,
int32_t offset )
static
405992 {
405993 auto &buffer_manager = segment.block->GetMemory().GetBufferManager();
405994 auto &state = segment.GetSegmentState()->Cast<UncompressedStringSegmentState>();
405995
405996 D_ASSERT(block != INVALID_BLOCK);
405997 D_ASSERT(offset < NumericCast<int32_t>(segment.GetBlockSize()));
405998
405999 if (block < MAXIMUM_BLOCK) {
406000 // read the overflow string from disk
406001 // pin the initial handle and read the length
406002 auto block_handle = state.GetHandle(segment.block->GetBlockManager(), block);
406003 auto handle = buffer_manager.Pin(block_handle);
406004
406005 // read header
406006 uint32_t length = Load<uint32_t>(handle.Ptr() + offset);
406007 uint32_t remaining = length;
406008 offset += sizeof(uint32_t);
406009
406010 BufferHandle target_handle;
406011 string_t overflow_string;
406012 data_ptr_t target_ptr;
406013 bool allocate_block = length >= segment.GetBlockSize();
406014 if (allocate_block) {
406015 // overflow string is bigger than a block - allocate a temporary buffer for it
406016 target_handle = buffer_manager.Allocate(MemoryTag::OVERFLOW_STRINGS, length);
406017 target_ptr = target_handle.Ptr();
406018 } else {
406019 // overflow string is smaller than a block - add it to the vector directly
406020 overflow_string = StringVector::EmptyString(result, length);
406021 target_ptr = data_ptr_cast(overflow_string.GetDataWriteable());
406022 }
406023
406024 // now append the string to the single buffer
406025 while (remaining > 0) {
406026 idx_t to_write = MinValue<idx_t>(remaining, segment.GetBlockSize() - sizeof(block_id_t) -
406027 UnsafeNumericCast<idx_t>(offset));
406028 memcpy(target_ptr, handle.Ptr() + offset, to_write);
406029 remaining -= to_write;
406030 offset += UnsafeNumericCast<int32_t>(to_write);
406031 target_ptr += to_write;
406032 if (remaining > 0) {
406033 // read the next block
406034 block_id_t next_block = Load<block_id_t>(handle.Ptr() + offset);
406035 block_handle = state.GetHandle(segment.block->GetBlockManager(), next_block);
406036 handle = buffer_manager.Pin(block_handle);
406037 offset = 0;
406038 }
406039 }
406040 if (allocate_block) {
406041 auto final_buffer = target_handle.Ptr();
406042 StringVector::AddHandle(result, std::move(target_handle));
406043 return ReadString(final_buffer, 0, length);
406044 } else {
406045 overflow_string.Finalize();
406046 return overflow_string;
406047 }
406048 }
406049
406050 // read the overflow string from memory
406051 // first pin the handle, if it is not pinned yet
406052 auto string_block = state.FindOverflowBlock(block);
406053 auto handle = buffer_manager.Pin(string_block.get().block);
406054 auto final_buffer = handle.Ptr();
406055 StringVector::AddHandle(result, std::move(handle));
406056 return ReadStringWithLength(final_buffer, offset);
406057}
static DUCKDB_API void AddHandle(Vector &vector, BufferHandle handle)
Adds a reference to a handle that stores strings of this vector.
Definition duckdb.cpp:137951
static DUCKDB_API string_t EmptyString(Vector &vector, idx_t len)
Definition duckdb.cpp:137942

◆ ReadString()

string_t duckdb::UncompressedStringStorage::ReadString ( data_ptr_t target,
int32_t offset,
uint32_t string_length )
static
406059 {
406060 auto ptr = target + offset;
406061 auto str_ptr = char_ptr_cast(ptr);
406062 return string_t(str_ptr, string_length);
406063}

◆ ReadStringWithLength()

string_t duckdb::UncompressedStringStorage::ReadStringWithLength ( data_ptr_t target,
int32_t offset )
static
406065 {
406066 auto ptr = target + offset;
406067 auto str_length = Load<uint32_t>(ptr);
406068 auto str_ptr = char_ptr_cast(ptr + sizeof(uint32_t));
406069 return string_t(str_ptr, str_length);
406070}

◆ WriteStringMarker()

void duckdb::UncompressedStringStorage::WriteStringMarker ( data_ptr_t target,
block_id_t block_id,
int32_t offset )
static
406072 {
406073 memcpy(target, &block_id, sizeof(block_id_t));
406074 target += sizeof(block_id_t);
406075 memcpy(target, &offset, sizeof(int32_t));
406076}

◆ ReadStringMarker()

void duckdb::UncompressedStringStorage::ReadStringMarker ( data_ptr_t target,
block_id_t & block_id,
int32_t & offset )
static
406078 {
406079 memcpy(&block_id, target, sizeof(block_id_t));
406080 target += sizeof(block_id_t);
406081 memcpy(&offset, target, sizeof(int32_t));
406082}

◆ FetchStringFromDict()

string_t duckdb::UncompressedStringStorage::FetchStringFromDict ( ColumnSegment & segment,
uint32_t dict_end_offset,
Vector & result,
data_ptr_t base_ptr,
int32_t dict_offset,
uint32_t string_length )
inlinestatic
75407 {
75408 D_ASSERT(dict_offset <= NumericCast<int32_t>(segment.GetBlockSize()));
75409 if (DUCKDB_LIKELY(dict_offset >= 0)) {
75410 // regular string - fetch from dictionary
75411 auto dict_end = base_ptr + dict_end_offset;
75412 auto dict_pos = dict_end - dict_offset;
75413
75414 auto str_ptr = char_ptr_cast(dict_pos);
75415 return string_t(str_ptr, string_length);
75416 } else {
75417 // read overflow string
75418 block_id_t block_id;
75419 int32_t offset;
75420 ReadStringMarker(base_ptr + dict_end_offset - AbsValue<int32_t>(dict_offset), block_id, offset);
75421
75422 return ReadOverflowString(segment, result, block_id, offset);
75423 }
75424 }

◆ SerializeState()

unique_ptr< ColumnSegmentState > duckdb::UncompressedStringStorage::SerializeState ( ColumnSegment & segment)
static
405868 {
405869 auto &state = segment.GetSegmentState()->Cast<UncompressedStringSegmentState>();
405870 if (state.on_disk_blocks.empty()) {
405871 // no on-disk blocks - nothing to write
405872 return nullptr;
405873 }
405874 return make_uniq<SerializedStringSegmentState>(state.on_disk_blocks);
405875}

◆ DeserializeState()

unique_ptr< ColumnSegmentState > duckdb::UncompressedStringStorage::DeserializeState ( Deserializer & deserializer)
static
405877 {
405878 auto result = make_uniq<SerializedStringSegmentState>();
405879 deserializer.ReadProperty(1, "overflow_blocks", result->blocks);
405880 return std::move(result);
405881}

◆ VisitBlockIds()

void duckdb::UncompressedStringStorage::VisitBlockIds ( const ColumnSegment & segment,
BlockIdVisitor & visitor )
static
405883 {
405884 auto &state = segment.GetSegmentState()->Cast<UncompressedStringSegmentState>();
405885 for (auto &block_id : state.on_disk_blocks) {
405886 visitor.Visit(block_id);
405887 }
405888}

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