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::dict_fsst::DictFSSTCompressionState Struct Reference
Inheritance diagram for duckdb::dict_fsst::DictFSSTCompressionState:
Collaboration diagram for duckdb::dict_fsst::DictFSSTCompressionState:

Public Member Functions

 DictFSSTCompressionState (ColumnDataCheckpointData &checkpoint_data_p, unique_ptr< DictFSSTAnalyzeState > &&state)
void CreateEmptySegment ()
idx_t Finalize ()
bool AllUnique () const
void FlushEncodingBuffer ()
idx_t CalculateRequiredSpace () const
DictionaryAppendState TryEncode ()
bool CompressInternal (UnifiedVectorFormat &vector_format, const string_t &str, bool is_null, EncodedInput &encoded_input, const idx_t i, idx_t count, bool fail_on_no_space)
void Compress (Vector &scan_vector, idx_t count)
void FinalizeCompress ()
void Flush (bool final)
Public Member Functions inherited from duckdb::CompressionState
 CompressionState (const CompressionInfo &info)
template<class TARGET>
TARGET & Cast ()
template<class TARGET>
const TARGET & Cast () const

Public Attributes

ColumnDataCheckpointData & checkpoint_data
const CompressionFunction & function
unique_ptr< ColumnSegment > current_segment
BufferHandle current_handle
idx_t dictionary_offset = 0
 Offset at which to write the next dictionary string.
idx_t string_lengths_space = 0
vector< uint32_t > string_lengths
uint32_t dict_count = 0
bitpacking_width_t string_lengths_width = 0
bitpacking_width_t real_string_lengths_width = 0
 For DICT_FSST we delay encoding of new entries, which means.
idx_t dictionary_indices_space = 0
vector< uint32_t > dictionary_indices
bitpacking_width_t dictionary_indices_width = 0
PrimitiveDictionary< string_t > current_string_map
 string -> dictionary_index (for lookups)
vector< string_t > dictionary_encoding_buffer
 strings added to the dictionary waiting to be encoded
idx_t to_encode_string_sum = 0
StringHeap uncompressed_dictionary_copy
 for DICT_FSST we store uncompressed strings in the 'current_string_map', this owns that memory
unsafe_unique_array< unsigned char > encoding_buffer = nullptr
 This is used for FSST_ONLY, to store the memory of the encoded input.
idx_t encoding_buffer_size = 0
idx_t tuple_count = 0
unique_ptr< DictFSSTAnalyzeState > analyze
idx_t symbol_table_size = DConstants::INVALID_INDEX
idx_t total_tuple_count = 0
 How many values have we compressed so far?
Public Attributes inherited from duckdb::CompressionState
CompressionInfo info

Private Attributes

void * encoder = nullptr
unsafe_unique_array< unsigned char > fsst_serialized_symbol_table
DictionaryAppendState append_state = DictionaryAppendState::REGULAR

Constructor & Destructor Documentation

◆ DictFSSTCompressionState()

duckdb::dict_fsst::DictFSSTCompressionState::DictFSSTCompressionState ( ColumnDataCheckpointData & checkpoint_data_p,
unique_ptr< DictFSSTAnalyzeState > && state )
398273 : CompressionState(analyze_p->info), checkpoint_data(checkpoint_data_p),
398274 function(checkpoint_data.GetCompressionFunction(CompressionType::COMPRESSION_DICT_FSST)),
398276 info.GetBlockManager().buffer_manager.GetBufferAllocator(),
398277 MinValue(analyze_p.get()->total_count, info.GetBlockSize()) / 2, // maximum_size_p (amount of elements)
398278 1 // maximum_target_capacity_p (byte capacity)
398279 ),
398280 analyze(std::move(analyze_p)) {
398281 CreateEmptySegment();
398282}
PrimitiveDictionary< string_t > current_string_map
string -> dictionary_index (for lookups)
Definition duckdb.cpp:398233

◆ ~DictFSSTCompressionState()

duckdb::dict_fsst::DictFSSTCompressionState::~DictFSSTCompressionState ( )
override
398284 {
398285 if (encoder) {
398286 auto fsst_encoder = reinterpret_cast<duckdb_fsst_encoder_t *>(encoder);
398287 duckdb_fsst_destroy(fsst_encoder);
398288 }
398289}

Member Function Documentation

◆ CreateEmptySegment()

void duckdb::dict_fsst::DictFSSTCompressionState::CreateEmptySegment ( )
398497 {
398498 auto &db = checkpoint_data.GetDatabase();
398499 auto &type = checkpoint_data.GetType();
398500
398501 auto compressed_segment =
398502 ColumnSegment::CreateTransientSegment(db, function, type, info.GetBlockSize(), info.GetBlockManager());
398503 current_segment = std::move(compressed_segment);
398504
398505 // Reset the pointers into the current segment.
398506 auto &buffer_manager = BufferManager::GetBufferManager(checkpoint_data.GetDatabase());
398507 current_handle = buffer_manager.Pin(current_segment->block);
398508
398509 append_state = DictionaryAppendState::REGULAR;
398510 string_lengths_width = 0;
398512 dictionary_indices_width = 0;
398513 string_lengths_space = 0;
398514 D_ASSERT(dictionary_indices.empty());
398515 dictionary_indices_space = 0;
398516 tuple_count = 0;
398517 D_ASSERT(string_lengths.empty());
398518 string_lengths.push_back(0);
398519 dict_count = 1;
398520 D_ASSERT(current_string_map.GetSize() == 0);
398521 symbol_table_size = DConstants::INVALID_INDEX;
398522
398523 dictionary_offset = 0;
398524}
db
static constexpr const idx_t INVALID_INDEX
The value used to signify an invalid index entry.
Definition duckdb.hpp:1117
bitpacking_width_t real_string_lengths_width
For DICT_FSST we delay encoding of new entries, which means.
Definition duckdb.cpp:398226
idx_t dictionary_offset
Offset at which to write the next dictionary string.
Definition duckdb.cpp:398217

◆ Finalize()

idx_t duckdb::dict_fsst::DictFSSTCompressionState::Finalize ( )
398312 {
398313 const bool is_fsst_encoded = IsEncoded(append_state);
398314
398315// calculate sizes
398316#ifdef DEBUG
398317 {
398318 auto dictionary_indices_space = BitpackingPrimitives::GetRequiredSize(tuple_count, dictionary_indices_width);
398319 D_ASSERT(dictionary_indices_space == this->dictionary_indices_space);
398320
398321 auto string_lengths_space = BitpackingPrimitives::GetRequiredSize(dict_count, string_lengths_width);
398322 D_ASSERT(string_lengths_space == this->string_lengths_space);
398323
398324 if (append_state == DictionaryAppendState::ENCODED_ALL_UNIQUE) {
398325 D_ASSERT(dictionary_indices_space == 0);
398326 }
398327 }
398328#endif
398329
398330 if (!is_fsst_encoded) {
398331 symbol_table_size = 0;
398332 }
398333
398334 D_ASSERT(to_encode_string_sum == 0);
398335 D_ASSERT(symbol_table_size != DConstants::INVALID_INDEX);
398336
398337 idx_t required_space = 0;
398338 required_space += sizeof(dict_fsst_compression_header_t);
398339 required_space = AlignValue<idx_t>(required_space);
398340 required_space += dictionary_offset;
398341 required_space = AlignValue<idx_t>(required_space);
398342 if (is_fsst_encoded) {
398343 required_space += symbol_table_size;
398344 required_space = AlignValue<idx_t>(required_space);
398345 }
398346 required_space += string_lengths_space;
398347 required_space = AlignValue<idx_t>(required_space);
398348 required_space += dictionary_indices_space;
398349
398350 D_ASSERT(info.GetBlockSize() >= required_space);
398351
398352 // calculate ptr and offsets
398353 auto base_ptr = current_handle.Ptr();
398354 auto header_ptr = reinterpret_cast<dict_fsst_compression_header_t *>(base_ptr);
398356 auto symbol_table_dest = AlignValue<idx_t>(dictionary_dest + dictionary_offset);
398357 auto string_lengths_dest = AlignValue<idx_t>(symbol_table_dest + symbol_table_size);
398358 auto dictionary_indices_dest = AlignValue<idx_t>(string_lengths_dest + string_lengths_space);
398359
398360 header_ptr->mode = ConvertToMode(append_state);
398361 header_ptr->symbol_table_size = NumericCast<uint32_t>(symbol_table_size);
398362 header_ptr->dict_size = NumericCast<uint32_t>(dictionary_offset);
398363 header_ptr->dict_count = NumericCast<uint32_t>(dict_count);
398364 header_ptr->dictionary_indices_width = dictionary_indices_width;
398365 header_ptr->string_lengths_width = string_lengths_width;
398366
398367 // Write the symbol table
398368 if (is_fsst_encoded) {
398369 D_ASSERT(symbol_table_size != DConstants::INVALID_INDEX);
398370 memcpy(base_ptr + symbol_table_dest, fsst_serialized_symbol_table.get(), symbol_table_size);
398371 }
398372
398373 // Write the string lengths of the dictionary
398374 BitpackingPrimitives::PackBuffer<uint32_t, false>(base_ptr + string_lengths_dest, string_lengths.data(), dict_count,
398375 string_lengths_width);
398376 // Write the dictionary indices (selection vector)
398377 BitpackingPrimitives::PackBuffer<sel_t, false>(base_ptr + dictionary_indices_dest,
398378 (sel_t *)(dictionary_indices.data()), tuple_count,
398379 dictionary_indices_width);
398380
398381#ifdef DEBUG
398382 if (append_state != DictionaryAppendState::ENCODED_ALL_UNIQUE) {
398383 auto expected_bitwidth = BitpackingPrimitives::MinimumBitWidth(dict_count - 1);
398384 D_ASSERT(dictionary_indices_width == expected_bitwidth);
398385 }
398386#endif
398387 D_ASSERT(base_ptr + required_space == base_ptr + dictionary_indices_dest + dictionary_indices_space);
398388 D_ASSERT((uint64_t)*max_element(std::begin(dictionary_indices), std::end(dictionary_indices)) == dict_count - 1);
398389 return required_space;
398390}
uint32_t sel_t
Type used for the selection vector.
Definition duckdb.hpp:251
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
static T AlignValue(T n)
Align value (ceiling) (not for pointer types).
Definition duckdb.hpp:2126
static constexpr uint16_t DICTIONARY_HEADER_SIZE
Dictionary header size at the beginning of the string segment (offset + length).
Definition duckdb.cpp:397835

◆ AllUnique()

bool duckdb::dict_fsst::DictFSSTCompressionState::AllUnique ( ) const

1 is added for NULL always

398915 {
398917 return string_lengths.size() - 1 == tuple_count;
398918}
Here is the caller graph for this function:

◆ FlushEncodingBuffer()

void duckdb::dict_fsst::DictFSSTCompressionState::FlushEncodingBuffer ( )

The highest value that still requires the current string lengths width

398409 {
398410 if (dictionary_encoding_buffer.empty()) {
398411 D_ASSERT(to_encode_string_sum == 0);
398412 return;
398413 }
398414
398415 vector<size_t> fsst_string_sizes;
398416 vector<unsigned char *> fsst_string_ptrs;
398417
398418 data_ptr_t dictionary_start =
398419 AlignPointer<sizeof(void *)>(current_handle.Ptr() + sizeof(dict_fsst_compression_header_t));
398420 D_ASSERT(dictionary_encoding_buffer.size() == dict_count - string_lengths.size());
398421 auto string_count = dictionary_encoding_buffer.size();
398422 idx_t sum = 0;
398423 for (auto &to_encode : dictionary_encoding_buffer) {
398424 auto str_len = to_encode.GetSize();
398425 sum += str_len * 2;
398426 fsst_string_sizes.push_back(str_len);
398427 fsst_string_ptrs.push_back((unsigned char *)to_encode.GetData()); // NOLINT
398428 }
398429 (void)sum;
398430 D_ASSERT(sum + 7 == to_encode_string_sum);
398431
398432 auto compressed_ptrs = vector<unsigned char *>(string_count, nullptr);
398433 auto compressed_sizes = vector<size_t>(string_count, 0);
398434
398435 // Compress the dictionary, straight to the segment
398436 idx_t taken_space = 0;
398437 taken_space += AlignValue<idx_t>(sizeof(dict_fsst_compression_header_t));
398438 taken_space += symbol_table_size;
398439 taken_space = AlignValue<idx_t>(taken_space);
398440 taken_space += string_lengths_space;
398441 taken_space = AlignValue<idx_t>(taken_space);
398442 taken_space += dictionary_indices_space;
398443 taken_space = AlignValue<idx_t>(taken_space);
398444 taken_space += dictionary_offset;
398445 D_ASSERT(taken_space < info.GetBlockSize());
398446
398447 auto fsst_encoder = reinterpret_cast<duckdb_fsst_encoder_t *>(encoder);
398448 auto res =
398449 duckdb_fsst_compress(fsst_encoder, string_count, fsst_string_sizes.data(), fsst_string_ptrs.data(),
398450 info.GetBlockSize() - taken_space, (unsigned char *)dictionary_start + dictionary_offset,
398451 compressed_sizes.data(), compressed_ptrs.data());
398452 if (res != string_count) {
398453 throw FatalException("Somehow we did not have enough room in the segment to store the encoded strings");
398454 }
398455 string_lengths_width = real_string_lengths_width;
398457 uint32_t biggest_strlen = (1 << string_lengths_width) - 1;
398458 D_ASSERT(BitpackingPrimitives::MinimumBitWidth(biggest_strlen) == string_lengths_width);
398459 idx_t compressed_sum = 0;
398460 for (idx_t i = 0; i < string_count; i++) {
398461 auto str_len = UnsafeNumericCast<uint32_t>(compressed_sizes[i]);
398462 if (str_len >= biggest_strlen) {
398463 biggest_strlen = str_len;
398464 }
398465 compressed_sum += str_len;
398466 string_lengths.push_back(str_len);
398467 dictionary_offset += str_len;
398468 }
398469 (void)compressed_sum;
398470 D_ASSERT(compressed_sum <= to_encode_string_sum);
398471 if (biggest_strlen >= NumericCast<uint32_t>(1 << string_lengths_width)) {
398472 string_lengths_width = BitpackingPrimitives::MinimumBitWidth(biggest_strlen);
398473 }
398474 real_string_lengths_width = string_lengths_width;
398475 auto new_string_lengths_space = BitpackingPrimitives::GetRequiredSize(dict_count, string_lengths_width);
398476 D_ASSERT(new_string_lengths_space <= string_lengths_space);
398477 string_lengths_space = new_string_lengths_space;
398478
398479 idx_t required_space = 0;
398480 required_space += sizeof(dict_fsst_compression_header_t);
398481 required_space = AlignValue<idx_t>(required_space);
398482 required_space += dictionary_offset;
398483 required_space = AlignValue<idx_t>(required_space);
398484 required_space += symbol_table_size;
398485 required_space = AlignValue<idx_t>(required_space);
398486 required_space += string_lengths_space;
398487 required_space = AlignValue<idx_t>(required_space);
398488 required_space += dictionary_indices_space;
398489
398490 D_ASSERT(required_space <= info.GetBlockSize());
398491
398492 D_ASSERT(string_lengths_space != 0);
398493 to_encode_string_sum = 0;
398495}
Scalar sum(InputArray src)
::uint32_t uint32_t
vector< string_t > dictionary_encoding_buffer
strings added to the dictionary waiting to be encoded
Definition duckdb.cpp:398235
Here is the call graph for this function:
Here is the caller graph for this function:

◆ CalculateRequiredSpace()

idx_t duckdb::dict_fsst::DictFSSTCompressionState::CalculateRequiredSpace ( ) const
398392 {
398393 idx_t required_space = 0;
398394 required_space += sizeof(dict_fsst_compression_header_t);
398395 required_space = AlignValue<idx_t>(required_space);
398396 required_space += dictionary_offset + to_encode_string_sum;
398397 required_space = AlignValue<idx_t>(required_space);
398398 if (IsEncoded(append_state)) {
398399 required_space += symbol_table_size;
398400 required_space = AlignValue<idx_t>(required_space);
398401 }
398402 required_space += string_lengths_space;
398403 required_space = AlignValue<idx_t>(required_space);
398404 required_space += dictionary_indices_space;
398405
398406 return required_space;
398407}

◆ TryEncode()

DictionaryAppendState duckdb::dict_fsst::DictFSSTCompressionState::TryEncode ( )

We were appending normally, the segment is full

Check that the compressed dictionary can fit on the block

We omit the selection buffer in this mode, setting the width to 0 makes the RequiredSpace result not include the selection buffer space.

398920 {
398922
398923 if (dictionary_offset < DICTIONARY_ENCODE_THRESHOLD) {
398924 return DictionaryAppendState::NOT_ENCODED;
398925 }
398926
398927 DictionaryAppendState new_state;
398928 if (!analyze->contains_nulls && AllUnique()) {
398929 new_state = DictionaryAppendState::ENCODED_ALL_UNIQUE;
398930 } else {
398931 new_state = DictionaryAppendState::ENCODED;
398932 }
398933
398934 vector<size_t> fsst_string_sizes;
398935 vector<unsigned char *> fsst_string_ptrs;
398936
398937 uint32_t offset = 0;
398938 data_ptr_t dictionary_start =
398939 AlignPointer<sizeof(void *)>(current_handle.Ptr() + sizeof(dict_fsst_compression_header_t));
398940 D_ASSERT(dictionary_offset > string_t::INLINE_BYTES && dictionary_offset <= string_t::MAX_STRING_SIZE);
398941 auto dict_copy = uncompressed_dictionary_copy.EmptyString(dictionary_offset);
398942 memcpy((void *)dict_copy.GetData(), (void *)dictionary_start, dictionary_offset);
398943 auto uncompressed_start = dict_copy.GetData();
398944 // Skip index 0, that's reserved for NULL
398945 for (idx_t i = 1; i < string_lengths.size(); i++) {
398946 auto length = string_lengths[i];
398947 auto start = uncompressed_start + offset;
398948 fsst_string_sizes.push_back(length);
398949 fsst_string_ptrs.push_back((unsigned char *)start); // NOLINT
398950 offset += length;
398951 }
398952 D_ASSERT(offset == dictionary_offset);
398953
398954 // Create the encoder
398955 auto string_count = string_lengths.size() - 1;
398956 encoder = reinterpret_cast<void *>(
398957 duckdb_fsst_create(string_count, fsst_string_sizes.data(), fsst_string_ptrs.data(), 0));
398958 auto fsst_encoder = reinterpret_cast<duckdb_fsst_encoder_t *>(encoder);
398959
398960 auto compressed_ptrs = vector<unsigned char *>(string_count, nullptr);
398961 auto compressed_sizes = vector<size_t>(string_count, 0);
398962
398963 // Compress the dictionary, straight to the segment
398964 auto res = duckdb_fsst_compress(fsst_encoder, string_count, fsst_string_sizes.data(), fsst_string_ptrs.data(),
398965 dictionary_offset, (unsigned char *)dictionary_start, compressed_sizes.data(),
398966 compressed_ptrs.data());
398967
398968 bool can_use_encoding = true;
398969 idx_t new_size = 0;
398970 bitpacking_width_t new_string_lengths_width;
398971 idx_t new_string_lengths_space;
398973 do {
398974 if (res != string_count) {
398975 can_use_encoding = false;
398976 break;
398977 }
398978 uint32_t max_length = 0;
398979 for (idx_t i = 0; i < string_count; i++) {
398980 auto str_len = UnsafeNumericCast<uint32_t>(compressed_sizes[i]);
398981 new_size += str_len;
398982 if (str_len > max_length) {
398983 max_length = str_len;
398984 }
398985 }
398986 if (new_size + DICTIONARY_ENCODE_THRESHOLD > dictionary_offset) {
398987 can_use_encoding = false;
398988 break;
398989 }
398990
398991 // Export the symbol table, so we get an accurate measurement of the size
398992 if (!fsst_serialized_symbol_table) {
398993 fsst_serialized_symbol_table =
398994 make_unsafe_uniq_array_uninitialized<unsigned char>(sizeof(duckdb_fsst_decoder_t));
398995 }
398996 symbol_table_size = duckdb_fsst_export(fsst_encoder, fsst_serialized_symbol_table.get());
398997
398998 new_string_lengths_width = BitpackingPrimitives::MinimumBitWidth(max_length);
398999 new_string_lengths_space = BitpackingPrimitives::GetRequiredSize(dict_count, new_string_lengths_width);
399000
399001 idx_t required_space = 0;
399002 required_space += sizeof(dict_fsst_compression_header_t);
399003 required_space = AlignValue<idx_t>(required_space);
399004 required_space += new_size;
399005 required_space = AlignValue<idx_t>(required_space);
399006 required_space += symbol_table_size;
399007 required_space = AlignValue<idx_t>(required_space);
399008 required_space += new_string_lengths_space;
399009 required_space = AlignValue<idx_t>(required_space);
399010 required_space += dictionary_indices_space;
399011
399012 if (required_space > info.GetBlockSize()) {
399013 can_use_encoding = false;
399014 break;
399015 }
399016
399017 } while (false);
399018
399019 if (!can_use_encoding) {
399020 // The dictionary does not compress well enough to use FSST
399021 // continue filling the remaining bytes without encoding
399022
399023 // We compressed directly to the segment, in the hopes this would fit and decrease in size
399024 // which it sadly didn't, so now we need to undo a bunch of things
399025
399026 memcpy(dictionary_start, dict_copy.GetData(), dictionary_offset);
399028 duckdb_fsst_destroy(fsst_encoder);
399029 encoder = nullptr;
399030 symbol_table_size = DConstants::INVALID_INDEX;
399031 return DictionaryAppendState::NOT_ENCODED;
399032 }
399033
399034 if (new_state == DictionaryAppendState::ENCODED_ALL_UNIQUE) {
399037 dictionary_indices_width = 0;
399038 dictionary_indices_space = 0;
399039 }
399040
399041#ifdef DEBUG
399042 auto temp_decoder = alloca(sizeof(duckdb_fsst_decoder_t));
399043 duckdb_fsst_import((duckdb_fsst_decoder_t *)temp_decoder, fsst_serialized_symbol_table.get());
399044
399045 vector<unsigned char> decompress_buffer;
399046#endif
399047
399048 // Rewrite the dictionary
399049 current_string_map.Clear();
399050 if (new_state == DictionaryAppendState::ENCODED) {
399051 offset = 0;
399052 auto uncompressed_dictionary_ptr = dict_copy.GetData();
399053 for (idx_t i = 0; i < string_count; i++) {
399054 auto size = UnsafeNumericCast<uint32_t>(compressed_sizes[i]);
399055 // Skip index 0, reserved for NULL
399056 uint32_t dictionary_index = UnsafeNumericCast<uint32_t>(i + 1);
399057 auto uncompressed_str_len = string_lengths[dictionary_index];
399058
399059 string_t dictionary_string(uncompressed_dictionary_ptr + offset, uncompressed_str_len);
399060 current_string_map.Insert(dictionary_string);
399061
399062#ifdef DEBUG
399064 decompress_buffer.resize(uncompressed_str_len + 1 + 100);
399065 FSSTPrimitives::DecompressValue((void *)temp_decoder, (const char *)compressed_ptrs[i],
399066 (idx_t)compressed_sizes[i], decompress_buffer);
399067
399068 string_t decompressed_string((const char *)decompress_buffer.data(), uncompressed_str_len);
399069 D_ASSERT(decompressed_string == dictionary_string);
399070#endif
399071
399072 string_lengths[dictionary_index] = size;
399073 offset += uncompressed_str_len;
399074 }
399075 } else {
399076 D_ASSERT(new_state == DictionaryAppendState::ENCODED_ALL_UNIQUE);
399077 for (idx_t i = 0; i < string_count; i++) {
399078 auto &start = compressed_ptrs[i];
399079 auto size = UnsafeNumericCast<uint32_t>(compressed_sizes[i]);
399080 // Skip index 0, reserved for NULL
399081 uint32_t dictionary_index = UnsafeNumericCast<uint32_t>(i + 1);
399082 string_lengths[dictionary_index] = size;
399083 string_t dictionary_string((const char *)start, UnsafeNumericCast<uint32_t>(size)); // NOLINT
399084
399085 current_string_map.Insert(dictionary_string);
399086 }
399087 }
399088 dictionary_offset = new_size;
399089 string_lengths_width = new_string_lengths_width;
399090 string_lengths_space = new_string_lengths_space;
399091 real_string_lengths_width = string_lengths_width;
399092 return new_state;
399093}
DictionaryAppendState
Definition duckdb.cpp:397825
GOpaque< Size > size(const GMat &src)
bool AllUnique() const
Definition duckdb.cpp:398915
StringHeap uncompressed_dictionary_copy
for DICT_FSST we store uncompressed strings in the 'current_string_map', this owns that memory
Definition duckdb.cpp:398238
Here is the call graph for this function:

◆ CompressInternal()

bool duckdb::dict_fsst::DictFSSTCompressionState::CompressInternal ( UnifiedVectorFormat & vector_format,
const string_t & str,
bool is_null,
EncodedInput & encoded_input,
const idx_t i,
idx_t count,
bool fail_on_no_space )

In GetRequiredSize we will round up to ALGORITHM_GROUP_SIZE anyways

This string does not exist in the dictionary, add it

This string does not exist in the dictionary, add it

Not in the dictionary, add it

The string doesn't fit, there are no strings left in the encoding buffer that could potentially

Not in the dictionary, add it

398740 {
398741 auto strings = UnifiedVectorFormat::GetData<string_t>(vector_format);
398743
398745 // so we can avoid recalculating for every tuple
398746 const bool recalculate_indices_space =
398747 append_state == DictionaryAppendState::ENCODED_ALL_UNIQUE
398748 ? false
398749 : ((tuple_count % BitpackingPrimitives::BITPACKING_ALGORITHM_GROUP_SIZE) == 0);
398750
398751 if (append_state == DictionaryAppendState::ENCODED_ALL_UNIQUE || is_null) {
398752 lookup = 0;
398753 } else {
398754 auto it = current_string_map.Lookup(str);
398755 lookup = it.IsEmpty() ? DConstants::INVALID_INDEX : it.index + 1;
398756 }
398757
398758 switch (append_state) {
398759 case DictionaryAppendState::NOT_ENCODED:
398760 case DictionaryAppendState::REGULAR: {
398761 if (append_state == DictionaryAppendState::REGULAR) {
398762 if (lookup != DConstants::INVALID_INDEX) {
398763 return AddLookup<DictionaryAppendState::REGULAR>(*this, lookup, recalculate_indices_space,
398764 fail_on_no_space);
398765 } else {
398767 return AddToDictionary<DictionaryAppendState::REGULAR>(*this, str, recalculate_indices_space,
398768 fail_on_no_space);
398769 }
398770 } else {
398771 if (lookup != DConstants::INVALID_INDEX) {
398772 return AddLookup<DictionaryAppendState::NOT_ENCODED>(*this, lookup, recalculate_indices_space,
398773 fail_on_no_space);
398774 } else {
398776 return AddToDictionary<DictionaryAppendState::NOT_ENCODED>(*this, str, recalculate_indices_space,
398777 fail_on_no_space);
398778 }
398779 }
398780 }
398781 case DictionaryAppendState::ENCODED: {
398782 // Don't encode the input, the 'current_string_map' is not encoded.
398783 // encoding of the dictionary is done lazily
398784 // we optimize for the case where the strings are *already* in the dictionary
398785
398786 bool fits;
398787 if (lookup != DConstants::INVALID_INDEX) {
398788 fits =
398789 AddLookup<DictionaryAppendState::ENCODED>(*this, lookup, recalculate_indices_space, fail_on_no_space);
398790 } else {
398792 fits = AddToDictionary<DictionaryAppendState::ENCODED>(*this, str, recalculate_indices_space,
398793 fail_on_no_space);
398794 }
398795 if (fits) {
398796 return fits;
398797 }
398798 if (dictionary_encoding_buffer.empty()) {
398800 // reduce the space used enough to store this string.
398801 return false;
398802 }
398803
398804 // We lazily encode the new entries, if we're full but have entries in the buffer
398805 // we flush these and try again to see if the size went down enough
398807 if (lookup != DConstants::INVALID_INDEX) {
398808 return AddLookup<DictionaryAppendState::ENCODED>(*this, lookup, recalculate_indices_space,
398809 fail_on_no_space);
398810 } else {
398812 return AddToDictionary<DictionaryAppendState::ENCODED>(*this, str, recalculate_indices_space,
398813 fail_on_no_space);
398814 }
398815 }
398816 case DictionaryAppendState::ENCODED_ALL_UNIQUE: {
398817 // Encode the input upfront, the 'current_string_map' is also encoded.
398818 // no lookups are performed, everything is added.
398819
398820#ifdef DEBUG
398821 auto temp_decoder = alloca(sizeof(duckdb_fsst_decoder_t));
398822 duckdb_fsst_import(reinterpret_cast<duckdb_fsst_decoder_t *>(temp_decoder), fsst_serialized_symbol_table.get());
398823 vector<unsigned char> decompress_buffer;
398824#endif
398825
398826 if (encoded_input.data.empty()) {
398827 encoded_input.offset = i;
398828 vector<unsigned char *> input_string_ptrs;
398829 vector<size_t> input_string_lengths;
398830 idx_t total_size = 0;
398831 for (idx_t j = i; j < count; j++) {
398832 auto index = vector_format.sel->get_index(j);
398833#ifdef DEBUG
398835 D_ASSERT(vector_format.validity.RowIsValid(index));
398836#endif
398837 auto &to_encode = strings[index];
398838 input_string_ptrs.push_back((unsigned char *)to_encode.GetData()); // NOLINT
398839 input_string_lengths.push_back(to_encode.GetSize());
398840 total_size += to_encode.GetSize();
398841 }
398842
398843 size_t output_buffer_size = 7 + (2 * total_size); // size as specified in fsst.h
398844 auto compressed_ptrs = vector<unsigned char *>(input_string_lengths.size(), nullptr);
398845 auto compressed_sizes = vector<size_t>(input_string_lengths.size(), 0);
398846 if (output_buffer_size > encoding_buffer_size) {
398847 encoding_buffer = make_unsafe_uniq_array_uninitialized<unsigned char>(output_buffer_size);
398848 encoding_buffer_size = output_buffer_size;
398849 }
398850
398851 // FIXME: can we compress directly to the segment? that would save a copy
398852 // I think yes?
398853 // We can give the segment as destination, and limit the size
398854 // it will tell us when it can't fit everything
398855 // worst case we can just check if the rest of the metadata fits when we remove the last string that it was
398856 // able to encode I believe 'duckdb_fsst_compress' tells us how many of the input strings it was able to
398857 // compress We can work backwards from there to see how many strings actually fit (probably worst case ret-1
398858 // ??)
398859 auto fsst_encoder = reinterpret_cast<duckdb_fsst_encoder_t *>(encoder);
398860 auto res = duckdb_fsst_compress(fsst_encoder, input_string_lengths.size(), input_string_lengths.data(),
398861 input_string_ptrs.data(), output_buffer_size, encoding_buffer.get(),
398862 compressed_sizes.data(), compressed_ptrs.data());
398863 if (res != input_string_lengths.size()) {
398864 throw FatalException("FSST compression failed to compress all input strings");
398865 }
398866
398867 for (idx_t j = 0; j < input_string_lengths.size(); j++) {
398868 uint32_t size = UnsafeNumericCast<uint32_t>(compressed_sizes[j]);
398869 string_t encoded_string((const char *)compressed_ptrs[j], size); // NOLINT;
398870
398871#ifdef DEBUG
398873 auto &uncompressed_str = strings[encoded_input.offset + j];
398874 decompress_buffer.resize(uncompressed_str.GetSize() + 1 + 100);
398875 auto decoded_std_string = FSSTPrimitives::DecompressValue(
398876 (void *)temp_decoder, reinterpret_cast<const char *>(compressed_ptrs[j]),
398877 (idx_t)compressed_sizes[j], decompress_buffer);
398878
398879 D_ASSERT(decoded_std_string.size() == uncompressed_str.GetSize());
398880 string_t decompressed_string(reinterpret_cast<const char *>(decompress_buffer.data()),
398881 UnsafeNumericCast<uint32_t>(uncompressed_str.GetSize()));
398882 D_ASSERT(decompressed_string == uncompressed_str);
398883#endif
398884
398885 encoded_input.data.push_back(encoded_string);
398886 }
398887 }
398888
398889#ifdef DEBUG
398891 for (idx_t j = encoded_input.offset; j < count; j++) {
398892 auto &uncompressed_string = strings[j];
398893 auto &compressed_string = encoded_input.data[j - encoded_input.offset];
398894
398895 decompress_buffer.resize(uncompressed_string.GetSize() + 1 + 100);
398896 auto decoded_std_string =
398897 FSSTPrimitives::DecompressValue((void *)temp_decoder, (const char *)compressed_string.GetData(),
398898 compressed_string.GetSize(), decompress_buffer);
398899
398900 D_ASSERT(decoded_std_string.size() == uncompressed_string.GetSize());
398901 string_t decompressed_string(reinterpret_cast<const char *>(decompress_buffer.data()),
398902 UnsafeNumericCast<uint32_t>(uncompressed_string.GetSize()));
398903 D_ASSERT(decompressed_string == uncompressed_string);
398904 }
398905
398906#endif
398907 auto &string = encoded_input.data[i - encoded_input.offset];
398908 return AddToDictionary<DictionaryAppendState::ENCODED_ALL_UNIQUE>(*this, string, recalculate_indices_space,
398909 fail_on_no_space);
398910 }
398911 };
398912 throw InternalException("Unreachable");
398913}
index
void FlushEncodingBuffer()
Definition duckdb.cpp:398409
unsafe_unique_array< unsigned char > encoding_buffer
This is used for FSST_ONLY, to store the memory of the encoded input.
Definition duckdb.cpp:398241
Here is the call graph for this function:

◆ Compress()

void duckdb::dict_fsst::DictFSSTCompressionState::Compress ( Vector & scan_vector,
idx_t count )
399095 {
399096 UnifiedVectorFormat vector_format;
399097 scan_vector.ToUnifiedFormat(count, vector_format);
399098 auto strings = UnifiedVectorFormat::GetData<string_t>(vector_format);
399099
399100 EncodedInput encoded_input;
399101 for (idx_t i = 0; i < count; i++) {
399102 auto idx = vector_format.sel->get_index(i);
399103 auto &str = strings[idx];
399104 auto is_null = !vector_format.validity.RowIsValid(idx);
399105 do {
399106 if (CompressInternal(vector_format, str, is_null, encoded_input, i, count, false)) {
399107 break;
399108 }
399109
399110 if (append_state == DictionaryAppendState::REGULAR) {
399111 append_state = TryEncode();
399112 D_ASSERT(append_state != DictionaryAppendState::REGULAR);
399113 if (CompressInternal(vector_format, str, is_null, encoded_input, i, count, false)) {
399114 break;
399115 }
399116 }
399117 Flush(false);
399118 encoded_input.data.clear();
399119 encoded_input.offset = 0;
399120 if (!CompressInternal(vector_format, str, is_null, encoded_input, i, count, true)) {
399121 throw FatalException("Compressing directly after Flush doesn't fit - expected to throw earlier!");
399122 }
399123 } while (false);
399124 if (!is_null) {
399125 UncompressedStringStorage::UpdateStringStats(current_segment->stats, str);
399126 } else {
399127 current_segment->stats.statistics.SetHasNullFast();
399128 }
399129 tuple_count++;
399130 }
399131}
bool CompressInternal(UnifiedVectorFormat &vector_format, const string_t &str, bool is_null, EncodedInput &encoded_input, const idx_t i, idx_t count, bool fail_on_no_space)
Definition duckdb.cpp:398738
void Flush(bool final)
Definition duckdb.cpp:398526
DictionaryAppendState TryEncode()
Definition duckdb.cpp:398920

◆ FinalizeCompress()

void duckdb::dict_fsst::DictFSSTCompressionState::FinalizeCompress ( )
399133 {
399134 Flush(true);
399135}

◆ Flush()

void duckdb::dict_fsst::DictFSSTCompressionState::Flush ( bool final)

This should already be empty at this point, otherwise that means that strings are not encoded / not added to the dictionary

398526 {
398527 if (final) {
398529 }
398530
398531 if (!tuple_count) {
398532 return;
398533 }
398534
398535 current_segment->count = tuple_count;
398536
398537 auto segment_size = Finalize();
398538 auto &state = checkpoint_data.GetCheckpointState();
398539 state.FlushSegment(std::move(current_segment), std::move(current_handle), segment_size);
398540
398541 // Reset the state
398545 D_ASSERT(dictionary_encoding_buffer.empty());
398546 D_ASSERT(to_encode_string_sum == 0);
398547
398548 current_string_map.Clear();
398549 string_lengths.clear();
398550 dictionary_indices.clear();
398551 if (encoder) {
398552 auto fsst_encoder = reinterpret_cast<duckdb_fsst_encoder_t *>(encoder);
398553 duckdb_fsst_destroy(fsst_encoder);
398554 encoder = nullptr;
398555 symbol_table_size = DConstants::INVALID_INDEX;
398556 }
398557 total_tuple_count += tuple_count;
398558
398559 if (!final) {
398560 CreateEmptySegment();
398561 }
398562}
idx_t total_tuple_count
How many values have we compressed so far?
Definition duckdb.cpp:398249
Here is the call graph for this function:

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