21 #include <tbb/parallel_for.h>
22 #include <tbb/task_arena.h>
24 #include <boost/filesystem/operations.hpp>
25 #include <boost/filesystem/path.hpp>
26 #include <boost/sort/spreadsort/string_sort.hpp>
30 #include <string_view>
32 #include <type_traits>
39 #include <sys/fcntl.h>
59 auto fd =
heavyai::open(path, O_RDWR | O_CREAT | (recover ? O_APPEND : O_TRUNC), 0644);
63 auto err = std::string(
"Dictionary path ") + std::string(path) +
64 std::string(
" does not exist.");
81 if (in == 0 || (in > (UINT32_MAX))) {
90 for (
size_t i = 0; i < str.size(); ++i) {
91 str_hash = str_hash * 997 + str[i];
101 const int64_t num_elems,
102 const int64_t target_elems_per_thread) {
104 std::min(std::max(max_thread_count, int64_t(1)),
105 ((num_elems + target_elems_per_thread - 1) / target_elems_per_thread));
119 const std::string& folder,
122 const bool materializeHashes,
123 size_t initial_capacity)
124 : dict_key_(dict_key)
127 , string_id_string_dict_hash_table_(initial_capacity, INVALID_STR_ID)
128 , hash_cache_(initial_capacity)
130 , materialize_hashes_(materializeHashes)
133 , offset_map_(nullptr)
134 , payload_map_(nullptr)
135 , offset_file_size_(0)
136 , payload_file_size_(0)
137 , payload_file_off_(0)
138 , strings_cache_(nullptr) {
139 if (!isTemp && folder.empty()) {
144 CHECK_EQ(
size_t(0), (initial_capacity & (initial_capacity - 1)));
146 boost::filesystem::path storage_path(folder);
147 offsets_path_ = (storage_path / boost::filesystem::path(
"DictOffsets")).
string();
148 const auto payload_path =
149 (storage_path / boost::filesystem::path(
"DictPayload")).
string();
155 bool storage_is_empty =
false;
157 storage_is_empty =
true;
173 const uint64_t str_count =
178 const uint64_t max_entries =
180 round_up_p2(std::max(initial_capacity, static_cast<size_t>(1))));
184 std::vector<string_dict_hash_t> new_hash_cache(max_entries / 2);
189 if (str_count == 0) {
193 unsigned string_id = 0;
196 uint32_t thread_inits = 0;
197 const auto thread_count = std::thread::hardware_concurrency();
198 const uint32_t items_per_thread = std::max<uint32_t>(
199 2000, std::min<uint32_t>(200000, (str_count / thread_count) + 1));
200 std::vector<std::future<std::vector<std::pair<string_dict_hash_t, unsigned int>>>>
202 for (string_id = 0; string_id < str_count; string_id += items_per_thread) {
205 std::vector<std::pair<string_dict_hash_t, unsigned int>> hashVec;
206 for (uint32_t curr_id = string_id;
207 curr_id < string_id + items_per_thread && curr_id < str_count;
210 if (recovered.canary) {
214 std::string_view temp(recovered.c_str_ptr, recovered.size);
215 hashVec.emplace_back(std::make_pair(
hash_string(temp), temp.size()));
221 if (thread_inits % thread_count == 0) {
226 if (dictionary_futures.size() != 0) {
229 VLOG(1) <<
"Opened string dictionary " << folder <<
" # Strings: " <<
str_count_
241 std::unordered_map<std::string, int32_t>
map_;
244 void operator()(std::string
const& str, int32_t
const string_id)
override {
245 auto const emplaced = map_.emplace(str, string_id);
246 CHECK(emplaced.second) <<
"str(" << str <<
") string_id(" << string_id <<
')';
248 void operator()(std::string_view
const, int32_t
const string_id)
override {
249 UNREACHABLE() <<
"MapMaker must be called with a std::string.";
251 std::unordered_map<std::string, int32_t>
moveMap() {
return std::move(map_); }
258 constexpr
size_t big_gen =
static_cast<size_t>(std::numeric_limits<size_t>::max());
261 return [map{map_maker.moveMap()}](std::string
const& str) {
262 auto const itr = map.find(str);
274 CHECK_LE(n, static_cast<size_t>(std::numeric_limits<int32_t>::max()) + 1);
275 for (
unsigned id = 0;
id <
n; ++id) {
280 serial_callback(str,
id);
283 size_t const n = std::min(static_cast<size_t>(generation),
str_count_);
284 CHECK_LE(n, static_cast<size_t>(std::numeric_limits<int32_t>::max()) + 1);
286 for (
unsigned id = 0;
id <
n; ++id) {
293 std::vector<std::future<std::vector<std::pair<string_dict_hash_t, unsigned int>>>>&
294 dictionary_futures) {
295 for (
auto& dictionary_future : dictionary_futures) {
296 dictionary_future.wait();
297 const auto hashVec = dictionary_future.get();
298 for (
const auto& hash : hashVec) {
299 const uint32_t bucket =
309 dictionary_futures.clear();
325 if (storage_slots == 0) {
330 int64_t min_bound = 0;
331 int64_t max_bound = storage_slots - 1;
333 while (min_bound <= max_bound) {
334 guess = (max_bound + min_bound) / 2;
336 if (getStringFromStorage(guess).canary) {
337 max_bound = guess - 1;
339 min_bound = guess + 1;
342 CHECK_GE(guess + (min_bound > guess ? 1 : 0), 0);
343 return guess + (min_bound > guess ? 1 : 0);
348 : dict_key_(dict_key)
349 , folder_(
"DB_" + std::
to_string(dict_key.db_id) +
"_DICT_" +
351 , strings_cache_(nullptr)
382 std::vector<int32_t> string_ids;
383 client_->get_or_add_bulk(string_ids, std::vector<std::string>{str});
384 CHECK_EQ(
size_t(1), string_ids.size());
385 return string_ids.front();
387 return getOrAddImpl(str);
394 std::ostringstream oss;
395 oss <<
"The text encoded column using dictionary " << dict_key
396 <<
" has exceeded it's limit of " <<
sizeof(
T) * 8 <<
" bits ("
397 << static_cast<size_t>(max_valid_int_value<T>() + 1) <<
" unique values) "
398 <<
"while attempting to add the new string '" << str <<
"'. ";
407 oss <<
"To load more data, please re-create the table with "
408 <<
"this column as type TEXT ENCODING DICT(" <<
sizeof(
T) * 2 * 8 <<
") ";
409 if (
sizeof(
T) == 1) {
410 oss <<
"or TEXT ENCODING DICT(32) ";
412 oss <<
"and reload your data.";
416 oss <<
"Currently dictionary-encoded text columns support a maximum of "
418 <<
" strings. Consider recreating the table with "
419 <<
"this column as type TEXT ENCODING NONE and reloading your data.";
422 throw std::runtime_error(oss.str());
427 std::ostringstream oss;
428 oss <<
"The string '" << str <<
" could not be inserted into the dictionary "
429 << dict_key <<
" because it exceeded the maximum allowable "
431 << str.size() <<
" characters).";
433 throw std::runtime_error(oss.str());
438 template <
class String>
440 const std::vector<std::vector<String>>& string_array_vec,
441 std::vector<std::vector<int32_t>>& ids_array_vec) {
447 ids_array_vec.resize(string_array_vec.size());
448 for (
size_t i = 0; i < string_array_vec.size(); i++) {
449 auto& strings = string_array_vec[i];
450 auto& ids = ids_array_vec[i];
451 ids.resize(strings.size());
457 const std::vector<std::vector<std::string>>& string_array_vec,
458 std::vector<std::vector<int32_t>>& ids_array_vec);
461 const std::vector<std::vector<std::string_view>>& string_array_vec,
462 std::vector<std::vector<int32_t>>& ids_array_vec);
469 template <
class String>
471 std::vector<string_dict_hash_t>& hashes)
const
473 CHECK_EQ(string_vec.size(), hashes.size());
476 [&string_vec, &hashes](
const tbb::blocked_range<size_t>& r) {
477 for (
size_t curr_id = r.begin(); curr_id != r.end(); ++curr_id) {
478 if (string_vec[curr_id].empty()) {
481 hashes[curr_id] =
hash_string(string_vec[curr_id]);
486 template <
class T,
class String>
488 T* encoded_vec)
const {
489 return getBulk(string_vec, encoded_vec, -1L );
493 uint8_t* encoded_vec)
const;
495 uint16_t* encoded_vec)
const;
497 int32_t* encoded_vec)
const;
499 template <
class T,
class String>
502 const int64_t generation)
const {
503 constexpr int64_t target_strings_per_thread{1000};
504 const int64_t num_lookup_strings = string_vec.size();
505 if (num_lookup_strings == 0) {
510 std::thread::hardware_concurrency(), num_lookup_strings, target_strings_per_thread);
514 std::vector<size_t> num_strings_not_found_per_thread(thread_info.
num_threads, 0UL);
517 const int64_t num_dict_strings = generation >= 0 ? generation :
storageEntryCount();
518 const bool dictionary_is_empty = (num_dict_strings == 0);
519 if (dictionary_is_empty) {
521 [&](
const tbb::blocked_range<int64_t>& r) {
522 const int64_t start_idx = r.begin();
523 const int64_t end_idx = r.end();
524 for (int64_t string_idx = start_idx; string_idx < end_idx;
529 return num_lookup_strings;
534 tbb::task_arena limited_arena(thread_info.
num_threads);
535 limited_arena.execute([&] {
538 tbb::blocked_range<int64_t>(
540 [&](
const tbb::blocked_range<int64_t>& r) {
541 const int64_t start_idx = r.begin();
542 const int64_t end_idx = r.end();
543 size_t num_strings_not_found = 0;
544 for (int64_t string_idx = start_idx; string_idx != end_idx; ++string_idx) {
545 const auto& input_string = string_vec[string_idx];
546 if (input_string.empty()) {
547 encoded_vec[string_idx] = inline_int_null_value<T>();
559 string_id >= num_dict_strings) {
561 num_strings_not_found++;
564 encoded_vec[string_idx] = string_id;
566 const size_t tbb_thread_idx = tbb::this_task_arena::current_thread_index();
567 num_strings_not_found_per_thread[tbb_thread_idx] = num_strings_not_found;
569 tbb::simple_partitioner());
572 size_t num_strings_not_found = 0;
573 for (int64_t thread_idx = 0; thread_idx < thread_info.num_threads; ++thread_idx) {
574 num_strings_not_found += num_strings_not_found_per_thread[thread_idx];
576 return num_strings_not_found;
580 uint8_t* encoded_vec,
581 const int64_t generation)
const;
583 uint16_t* encoded_vec,
584 const int64_t generation)
const;
586 int32_t* encoded_vec,
587 const int64_t generation)
const;
589 template <
class T,
class String>
591 T* output_string_ids) {
601 for (
const auto& input_string : input_strings) {
602 if (input_string.empty()) {
603 output_string_ids[idx++] = inline_int_null_value<T>();
609 uint32_t hash_bucket =
617 if (
str_count_ > static_cast<size_t>(max_valid_int_value<T>())) {
618 throw_encoding_error<T>(input_string,
dict_key_);
622 <<
") of Dictionary encoded Strings reached for this column, offset path "
636 const int32_t string_id =
static_cast<int32_t
>(
str_count_);
638 output_string_ids[idx++] = string_id;
641 const size_t num_strings_added =
str_count_ - initial_str_count;
642 if (num_strings_added > 0) {
647 template <
class T,
class String>
649 T* output_string_ids) {
652 std::vector<string_dict_hash_t> input_strings_hashes(input_strings.size());
656 size_t shadow_str_count =
658 const size_t storage_high_water_mark = shadow_str_count;
659 std::vector<size_t> string_memory_ids;
660 size_t sum_new_string_lengths = 0;
661 string_memory_ids.reserve(input_strings.size());
662 size_t input_string_idx{0};
663 for (
const auto& input_string : input_strings) {
665 if (input_string.empty()) {
666 output_string_ids[input_string_idx++] = inline_int_null_value<T>();
675 storage_high_water_mark,
678 input_strings_hashes);
683 const uint32_t hash_bucket =
687 storage_high_water_mark,
695 output_string_ids[input_string_idx++] =
701 if (shadow_str_count > static_cast<size_t>(max_valid_int_value<T>())) {
702 throw_encoding_error<T>(input_string,
dict_key_);
705 <<
"Maximum number (" << shadow_str_count
706 <<
") of Dictionary encoded Strings reached for this column, offset path "
710 string_memory_ids.push_back(input_string_idx);
711 sum_new_string_lengths += input_string.size();
713 static_cast<int32_t
>(shadow_str_count);
717 output_string_ids[input_string_idx++] = shadow_str_count++;
720 const size_t num_strings_added = shadow_str_count -
str_count_;
721 str_count_ = shadow_str_count;
722 if (num_strings_added > 0) {
727 uint8_t* encoded_vec);
729 uint16_t* encoded_vec);
731 int32_t* encoded_vec);
734 const std::vector<std::string_view>& string_vec,
735 uint8_t* encoded_vec);
737 const std::vector<std::string_view>& string_vec,
738 uint16_t* encoded_vec);
740 const std::vector<std::string_view>& string_vec,
741 int32_t* encoded_vec);
743 template <
class String>
747 if constexpr (std::is_same_v<std::string, std::decay_t<String>>) {
750 return client_->get(std::string(str));
761 auto str_id = string_id_string_dict_hash_table_[computeBucket(
762 hash, sv, string_id_string_dict_hash_table_)];
770 client_->get_string(ret, string_id);
777 CHECK_LT(string_id, static_cast<int32_t>(str_count_));
778 return getStringChecked(string_id);
783 std::shared_lock<std::shared_mutex>
read_lock(rw_mutex_);
786 CHECK_LT(string_id, static_cast<int32_t>(str_count_));
787 return getStringBytesChecked(string_id);
793 return client_->storage_entry_count();
801 const std::string& pattern,
803 const bool is_simple,
807 str.c_str(), str.size(), pattern.c_str(), pattern.size())
814 str.c_str(), str.size(), pattern.c_str(), pattern.size())
826 const bool is_simple,
828 const size_t generation)
const {
831 return client_->get_like(pattern, icase, is_simple, escape, generation);
833 const auto cache_key = std::make_tuple(pattern, icase, is_simple, escape);
838 std::vector<int32_t>
result;
839 std::vector<std::thread> workers;
842 std::vector<std::vector<int32_t>> worker_results(worker_count);
844 for (
int worker_idx = 0; worker_idx < worker_count; ++worker_idx) {
845 workers.emplace_back([&worker_results,
854 for (
size_t string_id = worker_idx; string_id < generation;
855 string_id += worker_count) {
857 if (
is_like(str, pattern, icase, is_simple, escape)) {
858 worker_results[worker_idx].push_back(string_id);
863 for (
auto& worker : workers) {
866 for (
const auto& worker_result : worker_results) {
867 result.insert(result.end(), worker_result.begin(), worker_result.end());
870 const auto it_ok =
like_cache_.insert(std::make_pair(cache_key, result));
878 std::string comp_operator,
880 std::vector<int32_t>
result;
885 auto eq_id = eq_id_itr->second;
886 if (comp_operator ==
"=") {
887 result.push_back(eq_id);
889 for (int32_t idx = 0; idx <= cur_size; idx++) {
893 result.push_back(idx);
897 std::vector<std::thread> workers;
900 std::vector<std::vector<int32_t>> worker_results(worker_count);
902 for (
int worker_idx = 0; worker_idx < worker_count; ++worker_idx) {
903 workers.emplace_back(
904 [&worker_results, &pattern, generation, worker_idx, worker_count,
this]() {
905 for (
size_t string_id = worker_idx; string_id < generation;
906 string_id += worker_count) {
908 if (str == pattern) {
909 worker_results[worker_idx].push_back(string_id);
914 for (
auto& worker : workers) {
917 for (
const auto& worker_result : worker_results) {
918 result.insert(result.end(), worker_result.begin(), worker_result.end());
920 if (result.size() > 0) {
921 const auto it_ok =
equal_cache_.insert(std::make_pair(pattern, result[0]));
925 if (comp_operator ==
"<>") {
926 for (int32_t idx = 0; idx <= cur_size; idx++) {
930 result.push_back(idx);
938 const std::string& comp_operator,
939 const size_t generation) {
942 return client_->get_compare(pattern, comp_operator, generation);
944 std::vector<int32_t> ret;
949 if (comp_operator ==
"=" || comp_operator ==
"<>") {
950 return getEquals(pattern, comp_operator, generation);
958 cache_index = std::make_shared<StringDictionary::compare_cache_value_t>();
963 [
this](decltype(
sorted_cache)::value_type
const&
a, decltype(pattern)& b) {
965 return string_lt(a_str.c_str_ptr, a_str.size, b.c_str(), b.size());
970 cache_index->diff = 1;
974 cache_str.c_str_ptr, cache_str.size, pattern.c_str(), pattern.size())) {
975 cache_index->index = cache_itr -
sorted_cache.begin() - 1;
976 cache_index->diff = 1;
979 cache_index->diff = 0;
1000 if (comp_operator ==
"<") {
1001 size_t idx = cache_index->index;
1002 if (cache_index->diff) {
1003 idx = cache_index->index + 1;
1004 if (cache_index->index == 0 && cache_index->diff > 0) {
1005 idx = cache_index->index;
1008 for (
size_t i = 0; i < idx; i++) {
1019 }
else if (comp_operator ==
"<=") {
1020 size_t idx = cache_index->index + 1;
1021 if (cache_index == 0 && cache_index->diff > 0) {
1022 idx = cache_index->index;
1024 for (
size_t i = 0; i < idx; i++) {
1033 }
else if (comp_operator ==
">") {
1034 size_t idx = cache_index->index + 1;
1035 if (cache_index->index == 0 && cache_index->diff > 0) {
1036 idx = cache_index->index;
1048 }
else if (comp_operator ==
">=") {
1049 size_t idx = cache_index->index;
1050 if (cache_index->diff) {
1051 idx = cache_index->index + 1;
1052 if (cache_index->index == 0 && cache_index->diff > 0) {
1053 idx = cache_index->index;
1059 }
else if (comp_operator ==
"=") {
1060 if (!cache_index->diff) {
1066 }
else if (comp_operator ==
"<>") {
1067 if (!cache_index->diff) {
1068 size_t idx = cache_index->index;
1069 for (
size_t i = 0; i < idx; i++) {
1083 std::runtime_error(
"Unsupported string comparison operator");
1091 const std::string& pattern,
1092 const char escape) {
1093 return regexp_like(str.c_str(), str.size(), pattern.c_str(), pattern.size(), escape);
1100 const size_t generation)
const {
1103 return client_->get_regexp_like(pattern, escape, generation);
1105 const auto cache_key = std::make_pair(pattern, escape);
1110 std::vector<int32_t>
result;
1111 std::vector<std::thread> workers;
1114 std::vector<std::vector<int32_t>> worker_results(worker_count);
1116 for (
int worker_idx = 0; worker_idx < worker_count; ++worker_idx) {
1117 workers.emplace_back([&worker_results,
1124 for (
size_t string_id = worker_idx; string_id < generation;
1125 string_id += worker_count) {
1128 worker_results[worker_idx].push_back(string_id);
1133 for (
auto& worker : workers) {
1136 for (
const auto& worker_result : worker_results) {
1137 result.insert(result.end(), worker_result.begin(), worker_result.end());
1139 const auto it_ok =
regex_cache_.insert(std::make_pair(cache_key, result));
1140 CHECK(it_ok.second);
1149 throw std::runtime_error(
1150 "copying dictionaries from remote server is not supported yet.");
1159 const bool multithreaded =
str_count_ > 10000;
1160 const auto worker_count =
1161 multithreaded ?
static_cast<size_t>(
cpu_threads()) :
size_t(1);
1163 std::vector<std::vector<std::string>> worker_results(worker_count);
1164 auto copy = [
this](std::vector<std::string>& str_list,
1165 const size_t start_id,
1166 const size_t end_id) {
1168 str_list.reserve(end_id - start_id);
1169 for (
size_t string_id = start_id; string_id < end_id; ++string_id) {
1173 if (multithreaded) {
1174 std::vector<std::future<void>> workers;
1175 const auto stride = (
str_count_ + (worker_count - 1)) / worker_count;
1176 for (
size_t worker_idx = 0, start = 0, end = std::min(start + stride,
str_count_);
1177 worker_idx < worker_count && start <
str_count_;
1178 ++worker_idx, start += stride, end = std::min(start + stride, str_count_)) {
1182 for (
auto& worker : workers) {
1186 CHECK_EQ(worker_results.size(), size_t(1));
1190 for (
const auto& worker_result : worker_results) {
1192 strings_cache_->end(), worker_result.begin(), worker_result.end());
1198 return string_id_string_dict_hash_table_.size() <= num_strings * 2;
1209 new_str_ids[bucket] = i;
1217 new_str_ids[bucket] = i;
1223 template <
class String>
1225 const size_t str_count,
1227 const size_t storage_high_water_mark,
1228 const std::vector<String>& input_strings,
1229 const std::vector<size_t>& string_memory_ids,
1230 const std::vector<string_dict_hash_t>& input_strings_hashes) noexcept {
1231 std::vector<int32_t> new_str_ids(string_id_string_dict_hash_table_.size() * 2,
1233 if (materialize_hashes_) {
1234 for (
size_t i = 0; i != str_count; ++i) {
1236 const uint32_t bucket = computeUniqueBucketWithHash(hash, new_str_ids);
1237 new_str_ids[bucket] = i;
1239 hash_cache_.resize(hash_cache_.size() * 2);
1241 for (
size_t storage_idx = 0; storage_idx != storage_high_water_mark; ++storage_idx) {
1242 const auto storage_string = getStringChecked(storage_idx);
1244 const uint32_t bucket = computeUniqueBucketWithHash(hash, new_str_ids);
1245 new_str_ids[bucket] = storage_idx;
1247 for (
size_t memory_idx = 0; memory_idx != string_memory_ids.size(); ++memory_idx) {
1248 const size_t string_memory_id = string_memory_ids[memory_idx];
1249 const uint32_t bucket = computeUniqueBucketWithHash(
1250 input_strings_hashes[string_memory_id], new_str_ids);
1251 new_str_ids[bucket] = storage_high_water_mark + memory_idx;
1254 string_id_string_dict_hash_table_.swap(new_str_ids);
1259 if (str.size() == 0) {
1260 return inline_int_null_value<int32_t>();
1262 CHECK(str.size() <= MAX_STRLEN);
1265 std::shared_lock<std::shared_mutex>
read_lock(rw_mutex_);
1266 const uint32_t bucket = computeBucket(hash, str, string_id_string_dict_hash_table_);
1267 if (string_id_string_dict_hash_table_[bucket] != INVALID_STR_ID) {
1268 return string_id_string_dict_hash_table_[bucket];
1271 std::lock_guard<std::shared_mutex>
write_lock(rw_mutex_);
1272 if (fillRateIsHigh(str_count_)) {
1274 increaseHashTableCapacity();
1278 const uint32_t bucket = computeBucket(hash, str, string_id_string_dict_hash_table_);
1279 if (string_id_string_dict_hash_table_[bucket] == INVALID_STR_ID) {
1281 <<
"Maximum number (" << str_count_
1282 <<
") of Dictionary encoded Strings reached for this column, offset path "
1285 appendToStorage(str);
1286 string_id_string_dict_hash_table_[bucket] =
static_cast<int32_t
>(str_count_);
1287 if (materialize_hashes_) {
1288 hash_cache_[str_count_] = hash;
1291 invalidateInvertedIndex();
1293 return string_id_string_dict_hash_table_[bucket];
1297 const auto str_canary = getStringFromStorage(string_id);
1298 CHECK(!str_canary.canary);
1299 return std::string(str_canary.c_str_ptr, str_canary.size);
1303 const int string_id)
const noexcept {
1304 const auto str_canary = getStringFromStorage(string_id);
1305 CHECK(!str_canary.canary);
1306 return std::make_pair(str_canary.c_str_ptr, str_canary.size);
1309 template <
class String>
1312 const String& input_string,
1313 const std::vector<int32_t>& string_id_string_dict_hash_table)
const noexcept {
1314 const size_t string_dict_hash_table_size = string_id_string_dict_hash_table.size();
1315 uint32_t bucket = hash & (string_dict_hash_table_size - 1);
1317 const int32_t candidate_string_id = string_id_string_dict_hash_table[bucket];
1318 if (candidate_string_id ==
1322 if ((materialize_hashes_ && hash == hash_cache_[candidate_string_id]) ||
1323 !materialize_hashes_) {
1324 const auto candidate_string = getStringFromStorageFast(candidate_string_id);
1325 if (input_string.size() == candidate_string.size() &&
1326 !memcmp(input_string.data(), candidate_string.data(), input_string.size())) {
1332 if (++bucket == string_dict_hash_table_size) {
1339 template <
class String>
1342 const String& input_string,
1343 const std::vector<int32_t>& string_id_string_dict_hash_table,
1344 const size_t storage_high_water_mark,
1345 const std::vector<String>& input_strings,
1346 const std::vector<size_t>& string_memory_ids)
const noexcept {
1347 uint32_t bucket = input_string_hash & (string_id_string_dict_hash_table.size() - 1);
1349 const int32_t candidate_string_id = string_id_string_dict_hash_table[bucket];
1350 if (candidate_string_id ==
1354 if (!materialize_hashes_ || (input_string_hash == hash_cache_[candidate_string_id])) {
1355 if (candidate_string_id > 0 &&
1356 static_cast<size_t>(candidate_string_id) >= storage_high_water_mark) {
1359 size_t memory_offset =
1360 static_cast<size_t>(candidate_string_id - storage_high_water_mark);
1361 const String candidate_string = input_strings[string_memory_ids[memory_offset]];
1362 if (input_string.size() == candidate_string.size() &&
1363 !memcmp(input_string.data(), candidate_string.data(), input_string.size())) {
1369 const auto candidate_storage_string =
1370 getStringFromStorageFast(candidate_string_id);
1371 if (input_string.size() == candidate_storage_string.size() &&
1372 !memcmp(input_string.data(),
1373 candidate_storage_string.data(),
1374 input_string.size())) {
1382 if (++bucket == string_id_string_dict_hash_table.size()) {
1391 const std::vector<int32_t>& string_id_string_dict_hash_table) noexcept {
1392 const size_t string_dict_hash_table_size = string_id_string_dict_hash_table.size();
1393 uint32_t bucket = hash & (string_dict_hash_table_size - 1);
1395 if (string_id_string_dict_hash_table[bucket] ==
1401 if (++bucket == string_dict_hash_table_size) {
1409 const size_t write_length) {
1411 const size_t min_capacity_needed =
1428 const size_t write_length) {
1431 const size_t min_capacity_needed =
1447 template <
class String>
1450 checkAndConditionallyIncreasePayloadCapacity(str.size());
1451 memcpy(payload_map_ + payload_file_off_, str.data(), str.size());
1455 payload_file_off_ += str.size();
1457 checkAndConditionallyIncreaseOffsetCapacity(
sizeof(str_meta));
1458 memcpy(offset_map_ + str_count_, &str_meta,
sizeof(str_meta));
1461 template <
class String>
1463 const std::vector<String>& input_strings,
1464 const std::vector<size_t>& string_memory_ids,
1465 const size_t sum_new_strings_lengths) noexcept {
1466 const size_t num_strings = string_memory_ids.size();
1468 checkAndConditionallyIncreasePayloadCapacity(sum_new_strings_lengths);
1469 checkAndConditionallyIncreaseOffsetCapacity(
sizeof(
StringIdxEntry) * num_strings);
1471 for (
size_t i = 0; i < num_strings; ++i) {
1472 const size_t string_idx = string_memory_ids[i];
1473 const String str = input_strings[string_idx];
1474 const size_t str_size(str.size());
1475 memcpy(payload_map_ + payload_file_off_, str.data(), str_size);
1476 StringIdxEntry str_meta{
static_cast<uint64_t
>(payload_file_off_), str_size};
1477 payload_file_off_ += str_size;
1478 memcpy(offset_map_ + str_count_ + i, &str_meta,
sizeof(str_meta));
1485 return {payload_map_ + str_meta->
off, str_meta->
size};
1489 const int string_id)
const noexcept {
1496 if (str_meta->
size == 0xffff) {
1498 return {
nullptr, 0,
true};
1500 return {payload_map_ + str_meta->
off, str_meta->
size,
false};
1505 payload_file_size_ += addStorageCapacity(payload_fd_, min_capacity_requested);
1507 payload_map_ =
static_cast<char*
>(
1508 addMemoryCapacity(payload_map_, payload_file_size_, min_capacity_requested));
1514 offset_file_size_ += addStorageCapacity(offset_fd_, min_capacity_requested);
1517 addMemoryCapacity(offset_map_, offset_file_size_, min_capacity_requested));
1523 const size_t min_capacity_requested) noexcept {
1524 const size_t canary_buff_size_to_add =
1528 if (canary_buffer_size < canary_buff_size_to_add) {
1529 CANARY_BUFFER =
static_cast<char*
>(realloc(CANARY_BUFFER, canary_buff_size_to_add));
1530 canary_buffer_size = canary_buff_size_to_add;
1531 CHECK(CANARY_BUFFER);
1532 memset(CANARY_BUFFER, 0xff, canary_buff_size_to_add);
1535 CHECK_NE(lseek(fd, 0, SEEK_END), -1);
1536 const auto write_return =
write(fd, CANARY_BUFFER, canary_buff_size_to_add);
1537 CHECK(write_return > 0 &&
1538 (static_cast<size_t>(write_return) == canary_buff_size_to_add));
1539 return canary_buff_size_to_add;
1544 const size_t min_capacity_requested) noexcept {
1545 const size_t canary_buff_size_to_add =
1548 if (canary_buffer_size < canary_buff_size_to_add) {
1550 reinterpret_cast<char*
>(realloc(CANARY_BUFFER, canary_buff_size_to_add));
1551 canary_buffer_size = canary_buff_size_to_add;
1552 CHECK(CANARY_BUFFER);
1553 memset(CANARY_BUFFER, 0xff, canary_buff_size_to_add);
1555 void* new_addr = realloc(addr, mem_size + canary_buff_size_to_add);
1557 void* write_addr =
reinterpret_cast<void*
>(
static_cast<char*
>(new_addr) + mem_size);
1558 CHECK(memcpy(write_addr, CANARY_BUFFER, canary_buff_size_to_add));
1559 mem_size += canary_buff_size_to_add;
1600 return static_cast<bool>(
client_);
1606 std::vector<int32_t> temp_sorted_cache;
1607 for (
size_t i = cur_cache_size; i <
str_count_; i++) {
1608 temp_sorted_cache.push_back(i);
1620 std::sort(cache.begin(), cache.end(), [
this](int32_t
a, int32_t b) {
1623 return string_lt(a_str.c_str_ptr, a_str.size, b_str.c_str_ptr, b_str.size);
1629 std::vector<int32_t> updated_cache(temp_sorted_cache.size() +
sorted_cache.size());
1630 size_t t_idx = 0, s_idx = 0, idx = 0;
1631 for (; t_idx < temp_sorted_cache.size() && s_idx <
sorted_cache.size(); idx++) {
1634 const auto insert_from_temp_cache =
1635 string_lt(t_string.c_str_ptr, t_string.size, s_string.c_str_ptr, s_string.size);
1636 if (insert_from_temp_cache) {
1637 updated_cache[idx] = temp_sorted_cache[t_idx++];
1642 while (t_idx < temp_sorted_cache.size()) {
1643 updated_cache[idx++] = temp_sorted_cache[t_idx++];
1652 std::vector<int32_t>& dest_ids,
1654 const std::vector<int32_t>& source_ids,
1656 const std::vector<std::string const*>& transient_string_vec) {
1657 std::vector<std::string> strings;
1659 for (
const int32_t source_id : source_ids) {
1660 if (source_id == std::numeric_limits<int32_t>::min()) {
1661 strings.emplace_back(
"");
1662 }
else if (source_id < 0) {
1664 CHECK_LT(string_index, transient_string_vec.size()) <<
"source_id=" << source_id;
1665 strings.emplace_back(*transient_string_vec[string_index]);
1667 strings.push_back(source_dict->
getString(source_id));
1671 dest_ids.resize(strings.size());
1676 std::vector<std::vector<int32_t>>& dest_array_ids,
1678 const std::vector<std::vector<int32_t>>& source_array_ids,
1680 dest_array_ids.resize(source_array_ids.size());
1682 std::atomic<size_t> row_idx{0};
1683 auto processor = [&row_idx, &dest_array_ids, dest_dict, &source_array_ids, source_dict](
1686 auto row = row_idx.fetch_add(1);
1688 if (row >= dest_array_ids.size()) {
1691 const auto& source_ids = source_array_ids[row];
1692 auto& dest_ids = dest_array_ids[row];
1697 const int num_worker_threads = std::thread::hardware_concurrency();
1699 if (source_array_ids.size() / num_worker_threads > 10) {
1700 std::vector<std::future<void>> worker_threads;
1701 for (
int i = 0; i < num_worker_threads; ++i) {
1705 for (
auto& child : worker_threads) {
1708 for (
auto& child : worker_threads) {
1717 const size_t generation)
const {
1730 CHECK_LE(num_strings, std::numeric_limits<int32_t>::max());
1732 std::vector<std::string_view> string_views(num_strings);
1734 if (num_strings == 0) {
1735 return string_views;
1737 constexpr int64_t tbb_parallel_threshold{1000};
1738 if (num_strings < tbb_parallel_threshold) {
1740 for (int32_t string_idx = 0; string_idx < num_strings; ++string_idx) {
1744 constexpr int64_t target_strings_per_thread{1000};
1746 std::thread::hardware_concurrency(), num_strings, target_strings_per_thread);
1750 tbb::task_arena limited_arena(thread_info.
num_threads);
1752 limited_arena.execute([&] {
1754 tbb::blocked_range<int64_t>(
1756 [&](
const tbb::blocked_range<int64_t>& r) {
1758 const int32_t start_idx = r.begin();
1759 const int32_t end_idx = r.end();
1760 for (int32_t string_idx = start_idx; string_idx != end_idx; ++string_idx) {
1764 tbb::simple_partitioner());
1767 return string_views;
1775 const std::shared_ptr<StringDictionary> dest_dict,
1779 const size_t num_dest_strings = dest_dict->storageEntryCount();
1780 std::vector<int32_t> translated_ids(num_source_strings);
1783 translated_ids.data(),
1788 dest_transient_lookup_callback,
1790 return translated_ids;
1795 std::shared_lock<std::shared_mutex>& source_read_lock,
1796 std::shared_lock<std::shared_mutex>& dest_read_lock) {
1797 const bool dicts_are_same = (source_dict_key == dest_dict_key);
1798 const bool source_dict_is_locked_first = (source_dict_key < dest_dict_key);
1799 if (dicts_are_same) {
1801 dest_read_lock.lock();
1802 }
else if (source_dict_is_locked_first) {
1803 source_read_lock.lock();
1804 dest_read_lock.lock();
1806 dest_read_lock.lock();
1807 source_read_lock.lock();
1813 int32_t* translated_ids,
1814 const int64_t source_generation,
1815 const int64_t dest_generation,
1816 const bool dest_has_transients,
1818 const std::vector<StringOps_Namespace::StringOpInfo>& string_op_infos)
const {
1822 const int64_t num_source_strings = source_generation;
1823 const int64_t num_dest_strings = dest_generation;
1826 if (num_source_strings == 0L) {
1835 throw std::runtime_error(
1836 "Cannot translate between a local source and remote destination dictionary.");
1841 std::shared_lock<std::shared_mutex> source_read_lock(
rw_mutex_, std::defer_lock);
1842 std::shared_lock<std::shared_mutex> dest_read_lock(dest_dict->
rw_mutex_,
1854 const bool dest_dictionary_is_empty = (num_dest_strings == 0);
1856 constexpr int64_t target_strings_per_thread{1000};
1858 std::thread::hardware_concurrency(), num_source_strings, target_strings_per_thread);
1867 const StringOps_Namespace::StringOps string_ops(string_op_infos);
1868 const bool has_string_ops = string_ops.size();
1870 tbb::task_arena limited_arena(thread_info.
num_threads);
1871 std::vector<size_t> num_strings_not_translated_per_thread(thread_info.
num_threads, 0UL);
1872 constexpr
bool short_circuit_empty_dictionary_translations{
false};
1873 limited_arena.execute([&] {
1875 if (short_circuit_empty_dictionary_translations && dest_dictionary_is_empty) {
1877 tbb::blocked_range<int32_t>(
1881 [&](
const tbb::blocked_range<int32_t>& r) {
1882 const int32_t start_idx = r.begin();
1883 const int32_t end_idx = r.end();
1884 for (int32_t string_idx = start_idx; string_idx != end_idx; ++string_idx) {
1888 tbb::simple_partitioner());
1889 num_strings_not_translated_per_thread[0] += num_source_strings;
1898 tbb::blocked_range<int32_t>(
1902 [&](
const tbb::blocked_range<int32_t>& r) {
1903 const int32_t start_idx = r.begin();
1904 const int32_t end_idx = r.end();
1905 size_t num_strings_not_translated = 0;
1906 std::string string_ops_storage;
1908 for (int32_t source_string_id = start_idx; source_string_id != end_idx;
1909 ++source_string_id) {
1910 const std::string_view source_str =
1915 if (source_str.empty()) {
1916 translated_ids[source_string_id] = inline_int_null_value<int32_t>();
1930 const auto translated_string_id =
1932 translated_ids[source_string_id] = translated_string_id;
1935 translated_string_id >= num_dest_strings) {
1936 if (dest_has_transients) {
1937 num_strings_not_translated +=
1938 dest_transient_lookup_callback(source_str, source_string_id);
1940 num_strings_not_translated++;
1945 const size_t tbb_thread_idx = tbb::this_task_arena::current_thread_index();
1946 num_strings_not_translated_per_thread[tbb_thread_idx] +=
1947 num_strings_not_translated;
1949 tbb::simple_partitioner());
1952 size_t total_num_strings_not_translated = 0;
1953 for (int64_t thread_idx = 0; thread_idx < thread_info.num_threads; ++thread_idx) {
1954 total_num_strings_not_translated += num_strings_not_translated_per_thread[thread_idx];
1956 return total_num_strings_not_translated;
1960 Datum* translated_ids,
1961 const int64_t source_generation,
1962 const std::vector<StringOps_Namespace::StringOpInfo>& string_op_infos)
const {
1965 CHECK_GT(string_op_infos.size(), 0UL);
1966 CHECK(!string_op_infos.back().getReturnType().is_string());
1967 const int64_t num_source_strings = source_generation;
1970 if (num_source_strings == 0L) {
1979 std::shared_lock<std::shared_mutex> source_read_lock(
rw_mutex_);
1988 constexpr int64_t target_strings_per_thread{1000};
1990 std::thread::hardware_concurrency(), num_source_strings, target_strings_per_thread);
1999 const StringOps_Namespace::StringOps string_ops(string_op_infos);
2002 tbb::task_arena limited_arena(thread_info.
num_threads);
2010 tbb::blocked_range<int32_t>(
2012 [&](
const tbb::blocked_range<int32_t>& r) {
2013 const int32_t start_idx = r.begin();
2014 const int32_t end_idx = r.end();
2015 for (int32_t source_string_id = start_idx; source_string_id != end_idx;
2016 ++source_string_id) {
2017 const std::string source_str =
2019 translated_ids[source_string_id] = string_ops.numericEval(source_str);
2027 const std::vector<int32_t>& source_ids,
2029 const int32_t dest_generation) {
2032 dict_server_host, {temp_dict_key.
db_id, temp_dict_key.
dict_id},
false);
void throw_string_too_long_error(std::string_view str, const shared::StringDictKey &dict_key)
StringIdxEntry * offset_map_
void translate_string_ids(std::vector< int32_t > &dest_ids, const DictRef dest_dict_ref, const std::vector< int32_t > &source_ids, const DictRef source_dict_ref, const int32_t dest_generation)
size_t payload_file_size_
bool isClient() const noexcept
void increaseHashTableCapacity() noexcept
void checkAndConditionallyIncreasePayloadCapacity(const size_t write_length)
string_dict_hash_t hash_string(const std::string_view &str)
size_t addStorageCapacity(int fd, const size_t min_capacity_requested=0) noexcept
std::vector< int32_t > getRegexpLike(const std::string &pattern, const char escape, const size_t generation) const
void operator()(std::string_view const, int32_t const string_id) override
size_t getBulk(const std::vector< String > &string_vec, T *encoded_vec) const
int64_t num_elems_per_thread
client_no_timeout_(new StringDictionaryClient(host,{dict_key.db_id, dict_key.dict_id}, false))
heavyai::shared_lock< heavyai::shared_mutex > read_lock
std::vector< int32_t > buildDictionaryTranslationMap(const std::shared_ptr< StringDictionary > dest_dict, StringLookupCallback const &dest_transient_lookup_callback) const
RUNTIME_EXPORT DEVICE bool string_eq(const char *lhs, const int32_t lhs_len, const char *rhs, const int32_t rhs_len)
std::vector< std::string > copyStrings() const
void buildDictionaryNumericTranslationMap(Datum *translated_ids, const int64_t source_generation, const std::vector< StringOps_Namespace::StringOpInfo > &string_op_infos) const
const shared::StringDictKey & getDictKey() const noexcept
std::pair< char *, size_t > getStringBytesChecked(const int string_id) const noexcept
size_t storageEntryCount() const
StringDictionary(const shared::StringDictKey &dict_key, const std::string &folder, const bool isTemp, const bool recover, const bool materializeHashes=false, size_t initial_capacity=256)
void addOffsetCapacity(const size_t min_capacity_requested=0) noexcept
uint32_t computeBucketFromStorageAndMemory(const string_dict_hash_t input_string_hash, const String &input_string, const std::vector< int32_t > &string_id_string_dict_hash_table, const size_t storage_high_water_mark, const std::vector< String > &input_strings, const std::vector< size_t > &string_memory_ids) const noexcept
std::string getStringChecked(const int string_id) const noexcept
std::vector< string_dict_hash_t > hash_cache_
DictionaryCache< std::string, compare_cache_value_t > compare_cache_
DEVICE void sort(ARGS &&...args)
bool fillRateIsHigh(const size_t num_strings) const noexcept
void * addMemoryCapacity(void *addr, size_t &mem_size, const size_t min_capacity_requested=0) noexcept
Constants for Builtin SQL Types supported by HEAVY.AI.
std::string offsets_path_
heavyai::unique_lock< heavyai::shared_mutex > write_lock
static void populate_string_ids(std::vector< int32_t > &dest_ids, StringDictionary *dest_dict, const std::vector< int32_t > &source_ids, const StringDictionary *source_dict, const std::vector< std::string const * > &transient_string_vec={})
Populates provided dest_ids vector with string ids corresponding to given source strings.
std::unordered_map< std::string, int32_t > map_
std::shared_mutex rw_mutex_
std::string_view getStringFromStorageFast(const int string_id) const noexcept
int32_t getOrAdd(const std::string &str) noexcept
std::map< std::pair< std::string, char >, std::vector< int32_t > > regex_cache_
std::unique_ptr< StringDictionaryClient > client_
std::string getStringUnlocked(int32_t string_id) const noexcept
size_t write(FILE *f, const size_t offset, const size_t size, const int8_t *buf)
Writes the specified number of bytes to the offset position in file f from buf.
RUNTIME_EXPORT DEVICE bool string_lt(const char *lhs, const int32_t lhs_len, const char *rhs, const int32_t rhs_len)
int32_t getIdOfString(const String &) const
bool is_regexp_like(const std::string &str, const std::string &pattern, const char escape)
static constexpr size_t MAX_STRCOUNT
std::vector< int32_t > getEquals(std::string pattern, std::string comp_operator, size_t generation)
uint32_t computeBucket(const string_dict_hash_t hash, const String &input_string, const std::vector< int32_t > &string_id_string_dict_hash_table) const noexcept
future< Result > async(Fn &&fn, Args &&...args)
static constexpr int32_t INVALID_STR_ID
std::shared_ptr< std::vector< std::string > > strings_cache_
std::vector< int32_t > getCompare(const std::string &pattern, const std::string &comp_operator, const size_t generation)
DEVICE auto copy(ARGS &&...args)
int open(const char *path, int flags, int mode)
RUNTIME_EXPORT DEVICE bool string_like(const char *str, const int32_t str_len, const char *pattern, const int32_t pat_len, const char escape_char)
void appendToStorageBulk(const std::vector< String > &input_strings, const std::vector< size_t > &string_memory_ids, const size_t sum_new_strings_lengths) noexcept
RUNTIME_EXPORT DEVICE bool string_like_simple(const char *str, const int32_t str_len, const char *pattern, const int32_t pat_len)
void addPayloadCapacity(const size_t min_capacity_requested=0) noexcept
bool is_like(const std::string &str, const std::string &pattern, const bool icase, const bool is_simple, const char escape)
std::map< std::string, int32_t > equal_cache_
void order_translation_locks(const shared::StringDictKey &source_dict_key, const shared::StringDictKey &dest_dict_key, std::shared_lock< std::shared_mutex > &source_read_lock, std::shared_lock< std::shared_mutex > &dest_read_lock)
uint32_t computeUniqueBucketWithHash(const string_dict_hash_t hash, const std::vector< int32_t > &string_id_string_dict_hash_table) noexcept
void getOrAddBulkArray(const std::vector< std::vector< String >> &string_array_vec, std::vector< std::vector< int32_t >> &ids_array_vec)
Functions to support the LIKE and ILIKE operator in SQL. Only single-byte character set is supported ...
int32_t getUnlocked(const std::string_view sv) const noexcept
void appendToStorage(const String str) noexcept
int checked_open(const char *path, const bool recover)
void getOrAddBulk(const std::vector< String > &string_vec, T *encoded_vec)
const shared::StringDictKey dict_key_
std::unordered_map< std::string, int32_t > moveMap()
std::pair< char *, size_t > getStringBytes(int32_t string_id) const noexcept
void processDictionaryFutures(std::vector< std::future< std::vector< std::pair< string_dict_hash_t, unsigned int >>>> &dictionary_futures)
void translate_string_ids(std::vector< int32_t > &dest_ids, const LeafHostInfo &dict_server_host, const shared::StringDictKey &dest_dict_key, const std::vector< int32_t > &source_ids, const shared::StringDictKey &source_dict_key, const int32_t dest_generation)
const int SYSTEM_PAGE_SIZE
bool checkpoint() noexcept
void throw_encoding_error(std::string_view str, const shared::StringDictKey &dict_key)
std::vector< int32_t > getLike(const std::string &pattern, const bool icase, const bool is_simple, const char escape, const size_t generation) const
void mergeSortedCache(std::vector< int32_t > &temp_sorted_cache)
DEVICE auto lower_bound(ARGS &&...args)
void eachStringSerially(int64_t const generation, StringCallback &) const
RUNTIME_EXPORT DEVICE bool string_ilike_simple(const char *str, const int32_t str_len, const char *pattern, const int32_t pat_len)
size_t getNumStringsFromStorage(const size_t storage_slots) const noexcept
void update_leaf(const LeafHostInfo &host_info)
int64_t num_elems_per_thread
std::string getString(int32_t string_id) const
void hashStrings(const std::vector< String > &string_vec, std::vector< string_dict_hash_t > &hashes) const noexcept
std::vector< std::string_view > getStringViews() const
int msync(void *addr, size_t length, bool async)
std::unique_ptr< StringDictionaryClient > client_no_timeout_
void parallel_for(const blocked_range< Int > &range, const Body &body, const Partitioner &p=Partitioner())
void increaseHashTableCapacityFromStorageAndMemory(const size_t str_count, const size_t storage_high_water_mark, const std::vector< String > &input_strings, const std::vector< size_t > &string_memory_ids, const std::vector< string_dict_hash_t > &input_strings_hashes) noexcept
std::function< int32_t(std::string const &)> makeLambdaStringToId() const
void checkAndConditionallyIncreaseOffsetCapacity(const size_t write_length)
static void populate_string_array_ids(std::vector< std::vector< int32_t >> &dest_array_ids, StringDictionary *dest_dict, const std::vector< std::vector< int32_t >> &source_array_ids, const StringDictionary *source_dict)
void invalidateInvertedIndex() noexcept
uint32_t string_dict_hash_t
#define DEBUG_TIMER(name)
void checked_munmap(void *addr, size_t length)
const uint64_t round_up_p2(const uint64_t num)
std::vector< int32_t > string_id_string_dict_hash_table_
ThreadInfo(const int64_t max_thread_count, const int64_t num_elems, const int64_t target_elems_per_thread)
void sortCache(std::vector< int32_t > &cache)
static constexpr size_t MAX_STRLEN
void getOrAddBulkParallel(const std::vector< String > &string_vec, T *encoded_vec)
std::function< bool(std::string_view, int32_t string_id)> StringLookupCallback
std::map< std::tuple< std::string, bool, bool, char >, std::vector< int32_t > > like_cache_
bool g_enable_stringdict_parallel
PayloadString getStringFromStorage(const int string_id) const noexcept
RUNTIME_EXPORT DEVICE bool regexp_like(const char *str, const int32_t str_len, const char *pattern, const int32_t pat_len, const char escape_char)
size_t file_size(const int fd)
static unsigned transientIdToIndex(int32_t const id)
void operator()(std::string const &str, int32_t const string_id) override
DEVICE void swap(ARGS &&...args)
std::vector< int32_t > sorted_cache
int32_t getOrAddImpl(const std::string_view &str) noexcept
~StringDictionary() noexcept
void * checked_mmap(const int fd, const size_t sz)
RUNTIME_EXPORT DEVICE bool string_ilike(const char *str, const int32_t str_len, const char *pattern, const int32_t pat_len, const char escape_char)