22 #include <tbb/parallel_for.h>
23 #include <tbb/task_arena.h>
30 return (stop - start) / step;
35 return step.numStepsBetween(start, stop);
38 template <
typename T,
typename K>
46 [&](
const tbb::blocked_range<int64_t>& r) {
47 const int64_t start_out_idx = r.begin();
48 const int64_t end_out_idx = r.end();
49 for (int64_t out_idx = start_out_idx; out_idx != end_out_idx;
51 series_output[out_idx] = start + (step * out_idx);
57 template <
typename T,
typename K>
63 const int64_t MAX_ROWS{1L << 30};
64 const int64_t PARALLEL_THRESHOLD{10000L};
72 if (num_rows > MAX_ROWS) {
73 return mgr.ERROR_MESSAGE(
74 "Invocation of generate_series would result in " +
std::to_string(num_rows) +
75 " rows, which exceeds the max limit of " +
std::to_string(MAX_ROWS) +
" rows.");
79 if (num_rows > PARALLEL_THRESHOLD) {
84 for (int64_t out_idx = 0; out_idx != num_rows; ++out_idx) {
85 series_output[out_idx] = start + (step * out_idx);
96 mgr, start, stop, static_cast<int64_t>(1), series_output);
104 constexpr
char alphanum_lookup_table[] =
106 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
107 "abcdefghijklmnopqrstuvwxyz";
108 constexpr
size_t char_mod =
sizeof(alphanum_lookup_table) - 1;
109 std::uniform_int_distribution<int32_t> rand_distribution(0, char_mod);
112 tmp_s.reserve(str_len);
113 for (
int i = 0; i < str_len; ++i) {
114 tmp_s += alphanum_lookup_table[rand_distribution(generator)];
121 #pragma comment(linker "/INCLUDE:generate_random_strings__cpu_")
126 const int64_t num_strings,
127 const int64_t string_length,
134 constexpr int64_t max_strings{10000000L};
135 constexpr int64_t max_str_len{10000L};
136 if (num_strings > max_strings) {
137 return mgr.ERROR_MESSAGE(
138 "generate_random_strings: num_strings must be between 0 and 10,000,000.");
140 if (string_length > max_str_len) {
141 return mgr.ERROR_MESSAGE(
142 "generate_random_strings: string_length must be between 1 and 10,000.");
144 if (num_strings == 0L) {
150 constexpr int64_t target_strings_per_thread{5000};
152 std::thread::hardware_concurrency(), num_strings, target_strings_per_thread);
153 std::vector<std::mt19937> per_thread_rand_generators;
154 per_thread_rand_generators.reserve(thread_info.
num_threads);
155 for (int64_t thread_idx = 0; thread_idx < thread_info.
num_threads; ++thread_idx) {
156 const uint64_t seed = std::chrono::duration_cast<std::chrono::nanoseconds>(
157 std::chrono::system_clock::now().time_since_epoch())
160 per_thread_rand_generators.emplace_back(seed);
162 std::vector<std::string> rand_strings(num_strings);
163 tbb::task_arena limited_arena(thread_info.
num_threads);
164 limited_arena.execute([&] {
168 [&](
const tbb::blocked_range<int64_t>& r) {
169 const int64_t tbb_thread_idx = tbb::this_task_arena::current_thread_index();
170 const int64_t start_out_idx = r.begin();
171 const int64_t end_out_idx = r.end();
172 for (int64_t out_idx = start_out_idx; out_idx != end_out_idx; ++out_idx) {
173 rand_strings[out_idx] =
174 gen_random_str(per_thread_rand_generators[tbb_thread_idx], string_length);
177 tbb::simple_partitioner());
179 const std::vector<int32_t> rand_string_ids =
180 output_strings.string_dict_proxy_->getOrAddTransientBulk(rand_strings);
181 for (int64_t row_idx = 0; row_idx < num_strings; row_idx++) {
182 output_id[row_idx] = row_idx;
183 output_strings[row_idx] = rand_string_ids[row_idx];
void set_output_row_size(int64_t num_rows)
int64_t num_elems_per_thread
EXTENSION_NOINLINE_HOST int32_t generate_random_strings__cpu_(TableFunctionManager &mgr, const int64_t num_strings, const int64_t string_length, Column< int64_t > &output_id, Column< TextEncodingDict > &output_strings)
FORCE_INLINE T __attribute__((__may_alias__))*may_alias_ptr(T *ptr)
#define EXTENSION_NOINLINE_HOST
void parallel_for(const blocked_range< Int > &range, const Body &body, const Partitioner &p=Partitioner())
int64_t numStepsBetween(int64_t start, int64_t stop, int64_t step)
#define DEBUG_TIMER(name)
HOST std::string gen_random_str(std::mt19937 &generator, const int64_t str_len)
NEVER_INLINE HOST int32_t generate_series__cpu_template(TableFunctionManager &mgr, const T start, const T stop, const K step, Column< T > &series_output)
int32_t generate_series_parallel(const T start, const T stop, const K step, Column< T > &series_output)