24 #include "../Shared/funcannotations.h"
25 #include "../Utils/ChunkIter.h"
28 #ifdef EXECUTE_INCLUDE
31 const uint64_t row_pos,
32 const uint32_t elem_log_sz) {
40 return ad.is_null ? 0 : ad.length >> elem_log_sz;
44 const uint64_t row_pos,
45 const uint32_t elem_log_sz,
46 const int32_t null_val) {
51 return ad.is_null ? null_val : ad.length >> elem_log_sz;
55 const uint64_t row_pos,
56 const int32_t null_val) {
61 return ad.is_null ? null_val : 1;
65 const uint64_t row_pos) {
74 const uint64_t row_pos) {
83 point_coord_array_size(int8_t* chunk_iter_,
84 const uint64_t row_pos,
85 const uint32_t elem_log_sz) {
93 return ad.is_null ? 0 : ad.length >> elem_log_sz;
97 point_coord_array_size_nullable(int8_t* chunk_iter_,
98 const uint64_t row_pos,
99 const uint32_t elem_log_sz,
100 const int32_t null_val) {
105 return ad.is_null ? null_val : ad.length >> elem_log_sz;
108 #define ARRAY_AT(type) \
109 extern "C" DEVICE RUNTIME_EXPORT type array_at_##type( \
110 int8_t* chunk_iter_, const uint64_t row_pos, const uint32_t elem_idx) { \
111 ChunkIter* chunk_iter = reinterpret_cast<ChunkIter*>(chunk_iter_); \
114 ChunkIter_get_nth(chunk_iter, row_pos, &ad, &is_end); \
115 return reinterpret_cast<type*>(ad.pointer)[elem_idx]; \
127 #define VARLEN_ARRAY_AT(type) \
128 extern "C" DEVICE RUNTIME_EXPORT type varlen_array_at_##type( \
129 int8_t* chunk_iter_, const uint64_t row_pos, const uint32_t elem_idx) { \
130 ChunkIter* chunk_iter = reinterpret_cast<ChunkIter*>(chunk_iter_); \
133 ChunkIter_get_nth_varlen(chunk_iter, row_pos, &ad, &is_end); \
134 return reinterpret_cast<type*>(ad.pointer)[elem_idx]; \
137 VARLEN_ARRAY_AT(int8_t)
138 VARLEN_ARRAY_AT(int16_t)
139 VARLEN_ARRAY_AT(int32_t)
140 VARLEN_ARRAY_AT(int64_t)
141 VARLEN_ARRAY_AT(
float)
142 VARLEN_ARRAY_AT(
double)
144 #undef VARLEN_ARRAY_AT
146 #define VARLEN_NOTNULL_ARRAY_AT(type) \
147 extern "C" DEVICE RUNTIME_EXPORT type varlen_notnull_array_at_##type( \
148 int8_t* chunk_iter_, const uint64_t row_pos, const uint32_t elem_idx) { \
149 ChunkIter* chunk_iter = reinterpret_cast<ChunkIter*>(chunk_iter_); \
152 ChunkIter_get_nth_varlen_notnull(chunk_iter, row_pos, &ad, &is_end); \
153 return reinterpret_cast<type*>(ad.pointer)[elem_idx]; \
156 VARLEN_NOTNULL_ARRAY_AT(int8_t)
157 VARLEN_NOTNULL_ARRAY_AT(int16_t)
158 VARLEN_NOTNULL_ARRAY_AT(int32_t)
159 VARLEN_NOTNULL_ARRAY_AT(int64_t)
160 VARLEN_NOTNULL_ARRAY_AT(
float)
161 VARLEN_NOTNULL_ARRAY_AT(
double)
163 #undef VARLEN_NOTNULL_ARRAY_AT
165 #define ARRAY_ANY(type, needle_type, oper_name, oper) \
166 extern "C" DEVICE RUNTIME_EXPORT bool array_any_##oper_name##_##type##_##needle_type( \
167 int8_t* chunk_iter_, \
168 const uint64_t row_pos, \
169 const needle_type needle, \
170 const type null_val) { \
171 ChunkIter* chunk_iter = reinterpret_cast<ChunkIter*>(chunk_iter_); \
174 ChunkIter_get_nth(chunk_iter, row_pos, &ad, &is_end); \
175 const size_t elem_count = ad.length / sizeof(type); \
176 for (size_t i = 0; i < elem_count; ++i) { \
177 const needle_type val = reinterpret_cast<type*>(ad.pointer)[i]; \
178 if (val != null_val && val oper needle) { \
185 #define ARRAY_ALL(type, needle_type, oper_name, oper) \
186 extern "C" DEVICE RUNTIME_EXPORT bool array_all_##oper_name##_##type##_##needle_type( \
187 int8_t* chunk_iter_, \
188 const uint64_t row_pos, \
189 const needle_type needle, \
190 const type null_val) { \
191 ChunkIter* chunk_iter = reinterpret_cast<ChunkIter*>(chunk_iter_); \
194 ChunkIter_get_nth(chunk_iter, row_pos, &ad, &is_end); \
195 const size_t elem_count = ad.length / sizeof(type); \
196 for (size_t i = 0; i < elem_count; ++i) { \
197 const needle_type val = reinterpret_cast<type*>(ad.pointer)[i]; \
198 if (!(val != null_val && val oper needle)) { \
205 #define ARRAY_ALL_ANY_ALL_TYPES(oper_name, oper, needle_type) \
206 ARRAY_ANY(int8_t, needle_type, oper_name, oper) \
207 ARRAY_ALL(int8_t, needle_type, oper_name, oper) \
208 ARRAY_ANY(int16_t, needle_type, oper_name, oper) \
209 ARRAY_ALL(int16_t, needle_type, oper_name, oper) \
210 ARRAY_ANY(int32_t, needle_type, oper_name, oper) \
211 ARRAY_ALL(int32_t, needle_type, oper_name, oper) \
212 ARRAY_ANY(int64_t, needle_type, oper_name, oper) \
213 ARRAY_ALL(int64_t, needle_type, oper_name, oper) \
214 ARRAY_ANY(float, needle_type, oper_name, oper) \
215 ARRAY_ALL(float, needle_type, oper_name, oper) \
216 ARRAY_ANY(double, needle_type, oper_name, oper) \
217 ARRAY_ALL(double, needle_type, oper_name, oper)
219 ARRAY_ALL_ANY_ALL_TYPES(eq, ==, int8_t)
220 ARRAY_ALL_ANY_ALL_TYPES(ne, !=, int8_t)
221 ARRAY_ALL_ANY_ALL_TYPES(lt, <, int8_t)
222 ARRAY_ALL_ANY_ALL_TYPES(le, <=, int8_t)
223 ARRAY_ALL_ANY_ALL_TYPES(gt, >, int8_t)
224 ARRAY_ALL_ANY_ALL_TYPES(ge, >=, int8_t)
226 ARRAY_ALL_ANY_ALL_TYPES(eq, ==, int16_t)
227 ARRAY_ALL_ANY_ALL_TYPES(ne, !=, int16_t)
228 ARRAY_ALL_ANY_ALL_TYPES(lt, <, int16_t)
229 ARRAY_ALL_ANY_ALL_TYPES(le, <=, int16_t)
230 ARRAY_ALL_ANY_ALL_TYPES(gt, >, int16_t)
231 ARRAY_ALL_ANY_ALL_TYPES(ge, >=, int16_t)
233 ARRAY_ALL_ANY_ALL_TYPES(eq, ==, int32_t)
234 ARRAY_ALL_ANY_ALL_TYPES(ne, !=, int32_t)
235 ARRAY_ALL_ANY_ALL_TYPES(lt, <, int32_t)
236 ARRAY_ALL_ANY_ALL_TYPES(le, <=, int32_t)
237 ARRAY_ALL_ANY_ALL_TYPES(gt, >, int32_t)
238 ARRAY_ALL_ANY_ALL_TYPES(ge, >=, int32_t)
240 ARRAY_ALL_ANY_ALL_TYPES(eq, ==, int64_t)
241 ARRAY_ALL_ANY_ALL_TYPES(ne, !=, int64_t)
242 ARRAY_ALL_ANY_ALL_TYPES(lt, <, int64_t)
243 ARRAY_ALL_ANY_ALL_TYPES(le, <=, int64_t)
244 ARRAY_ALL_ANY_ALL_TYPES(gt, >, int64_t)
245 ARRAY_ALL_ANY_ALL_TYPES(ge, >=, int64_t)
247 ARRAY_ALL_ANY_ALL_TYPES(eq, ==,
float)
248 ARRAY_ALL_ANY_ALL_TYPES(ne, !=,
float)
249 ARRAY_ALL_ANY_ALL_TYPES(lt, <,
float)
250 ARRAY_ALL_ANY_ALL_TYPES(le, <=,
float)
251 ARRAY_ALL_ANY_ALL_TYPES(gt, >,
float)
252 ARRAY_ALL_ANY_ALL_TYPES(ge, >=,
float)
254 ARRAY_ALL_ANY_ALL_TYPES(eq, ==,
double)
255 ARRAY_ALL_ANY_ALL_TYPES(ne, !=,
double)
256 ARRAY_ALL_ANY_ALL_TYPES(lt, <,
double)
257 ARRAY_ALL_ANY_ALL_TYPES(le, <=,
double)
258 ARRAY_ALL_ANY_ALL_TYPES(gt, >,
double)
259 ARRAY_ALL_ANY_ALL_TYPES(ge, >=,
double)
261 #undef ARRAY_ALL_ANY_ALL_TYPES
265 #define ARRAY_AT_CHECKED(type) \
266 extern "C" DEVICE RUNTIME_EXPORT type array_at_##type##_checked( \
267 int8_t* chunk_iter_, \
268 const uint64_t row_pos, \
269 const int64_t elem_idx, \
270 const type null_val) { \
271 if (elem_idx <= 0) { \
274 ChunkIter* chunk_iter = reinterpret_cast<ChunkIter*>(chunk_iter_); \
277 ChunkIter_get_nth(chunk_iter, row_pos, &ad, &is_end); \
278 if (ad.is_null || static_cast<size_t>(elem_idx) > ad.length / sizeof(type)) { \
281 return reinterpret_cast<type*>(ad.pointer)[elem_idx - 1]; \
284 ARRAY_AT_CHECKED(int8_t)
285 ARRAY_AT_CHECKED(int16_t)
286 ARRAY_AT_CHECKED(int32_t)
287 ARRAY_AT_CHECKED(int64_t)
288 ARRAY_AT_CHECKED(
float)
289 ARRAY_AT_CHECKED(
double)
291 #undef ARRAY_AT_CHECKED
294 int64_t element_size) {
296 int8_t* varlen_buffer =
297 reinterpret_cast<int8_t*
>(
checked_malloc((element_count + 1) * element_size));
298 return varlen_buffer;
305 fast_fixlen_array_size(int8_t* chunk_iter_,
const uint32_t elem_log_sz) {
312 const uint64_t row_pos) {
317 auto n =
static_cast<int>(row_pos);
323 determine_fixed_array_len(int8_t* chunk_iter, int64_t valid_len) {
324 return chunk_iter ? valid_len : 0;
328 const uint64_t row_pos) {
358 const double dval{val};
359 return *
reinterpret_cast<const int64_t*
>(may_alias_ptr(&dval));
363 return *
reinterpret_cast<const int64_t*
>(may_alias_ptr(&val));
366 #define COUNT_DISTINCT_ARRAY(type) \
367 extern "C" RUNTIME_EXPORT void agg_count_distinct_array_##type( \
368 int64_t* agg, int8_t* chunk_iter_, const uint64_t row_pos, const type null_val) { \
369 ChunkIter* chunk_iter = reinterpret_cast<ChunkIter*>(chunk_iter_); \
372 ChunkIter_get_nth(chunk_iter, row_pos, &ad, &is_end); \
373 const size_t elem_count{ad.length / sizeof(type)}; \
374 for (size_t i = 0; i < elem_count; ++i) { \
375 const auto val = reinterpret_cast<type*>(ad.pointer)[i]; \
376 if (val != null_val) { \
377 reinterpret_cast<CountDistinctSet*>(*agg)->insert(elem_bitcast_##type(val)); \
382 COUNT_DISTINCT_ARRAY(int8_t)
383 COUNT_DISTINCT_ARRAY(int16_t)
384 COUNT_DISTINCT_ARRAY(int32_t)
385 COUNT_DISTINCT_ARRAY(int64_t)
386 COUNT_DISTINCT_ARRAY(
float)
387 COUNT_DISTINCT_ARRAY(
double)
389 #undef COUNT_DISTINCT_ARRAY
391 #include <functional>
392 #include <string_view>
395 const int64_t string_dict_handle);
397 template <
typename T>
398 bool array_any(int8_t*
const chunk_iter_i8,
399 uint64_t
const row_pos,
400 std::string_view
const needle_str,
401 int64_t
const string_dict_handle,
403 std::function<
bool(std::string_view, std::string_view)>
const cmp) {
408 size_t const elem_count = ad.length /
sizeof(
T);
409 for (
size_t i = 0; i < elem_count; ++i) {
410 T const val =
reinterpret_cast<T*
>(ad.pointer)[i];
411 if (val != null_val) {
421 template <
typename T>
422 bool array_all(int8_t*
const chunk_iter_i8,
423 uint64_t
const row_pos,
424 std::string_view
const needle_str,
425 int64_t
const string_dict_handle,
427 std::function<
bool(std::string_view, std::string_view)>
const cmp) {
432 size_t const elem_count = ad.length /
sizeof(
T);
433 for (
size_t i = 0; i < elem_count; ++i) {
434 T const val =
reinterpret_cast<T*
>(ad.pointer)[i];
435 if (val == null_val) {
446 #define ARRAY_STR_ANY(type, oper_name, oper) \
447 extern "C" RUNTIME_EXPORT bool array_any_##oper_name##_str_##type( \
448 int8_t* const chunk_iter_i8, \
449 uint64_t const row_pos, \
450 char const* const needle_ptr, \
451 uint32_t const needle_len, \
452 int64_t const string_dict_handle, \
453 type const null_val) { \
454 return array_any(chunk_iter_i8, \
456 std::string_view{needle_ptr, needle_len}, \
457 string_dict_handle, \
459 std::oper<std::string_view>{}); \
462 #define ARRAY_STR_ALL(type, oper_name, oper) \
463 extern "C" RUNTIME_EXPORT bool array_all_##oper_name##_str_##type( \
464 int8_t* const chunk_iter_i8, \
465 uint64_t const row_pos, \
466 char const* const needle_ptr, \
467 uint32_t const needle_len, \
468 int64_t const string_dict_handle, \
469 type const null_val) { \
470 return array_all(chunk_iter_i8, \
472 std::string_view{needle_ptr, needle_len}, \
473 string_dict_handle, \
475 std::oper<std::string_view>{}); \
478 #define ARRAY_STR_ALL_ANY_ALL_TYPES(oper_name, oper) \
479 ARRAY_STR_ANY(int8_t, oper_name, oper) \
480 ARRAY_STR_ALL(int8_t, oper_name, oper) \
481 ARRAY_STR_ANY(int16_t, oper_name, oper) \
482 ARRAY_STR_ALL(int16_t, oper_name, oper) \
483 ARRAY_STR_ANY(int32_t, oper_name, oper) \
484 ARRAY_STR_ALL(int32_t, oper_name, oper) \
485 ARRAY_STR_ANY(int64_t, oper_name, oper) \
486 ARRAY_STR_ALL(int64_t, oper_name, oper)
488 ARRAY_STR_ALL_ANY_ALL_TYPES(eq, equal_to)
489 ARRAY_STR_ALL_ANY_ALL_TYPES(ne, not_equal_to)
490 ARRAY_STR_ALL_ANY_ALL_TYPES(lt, less)
491 ARRAY_STR_ALL_ANY_ALL_TYPES(le, less_equal)
492 ARRAY_STR_ALL_ANY_ALL_TYPES(gt, greater)
493 ARRAY_STR_ALL_ANY_ALL_TYPES(ge, greater_equal)
495 #undef ARRAY_ALL_ANY_ALL_TYPES
501 #endif // EXECUTE_INCLUDE
DEVICE void ChunkIter_get_nth_point_coords(ChunkIter *it, int n, ArrayDatum *result, bool *is_end)
DEVICE void ChunkIter_get_nth(ChunkIter *it, int n, bool uncompress, VarlenDatum *result, bool *is_end)
std::conditional_t< is_cuda_compiler(), DeviceArrayDatum, HostArrayDatum > ArrayDatum
EXTENSION_NOINLINE int8_t * allocate_varlen_buffer(int64_t element_count, int64_t element_size)
void * checked_malloc(const size_t size)
std::string_view stringView() const
RUNTIME_EXPORT StringView string_decompress(const int32_t string_id, const int64_t string_dict_handle)