OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TargetExprBuilder.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019 OmniSci, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
23 #include "TargetExprBuilder.h"
24 
25 #include "CodeGenerator.h"
26 #include "Execute.h"
27 #include "GroupByAndAggregate.h"
28 #include "MaxwellCodegenPatch.h"
30 #include "Shared/Logger.h"
31 
32 #define LL_CONTEXT executor->cgen_state_->context_
33 #define LL_BUILDER executor->cgen_state_->ir_builder_
34 #define LL_BOOL(v) executor->ll_bool(v)
35 #define LL_INT(v) executor->cgen_state_->llInt(v)
36 #define LL_FP(v) executor->cgen_state_->llFp(v)
37 #define ROW_FUNC executor->cgen_state_->row_func_
38 
39 namespace {
40 
41 std::vector<std::string> agg_fn_base_names(const TargetInfo& target_info) {
42  const auto& chosen_type = get_compact_type(target_info);
43  if (!target_info.is_agg || target_info.agg_kind == kSAMPLE) {
44  if (chosen_type.is_geometry()) {
45  return std::vector<std::string>(2 * chosen_type.get_physical_coord_cols(),
46  "agg_id");
47  }
48  if (chosen_type.is_varlen()) {
49  return {"agg_id", "agg_id"};
50  }
51  return {"agg_id"};
52  }
53  switch (target_info.agg_kind) {
54  case kAVG:
55  return {"agg_sum", "agg_count"};
56  case kCOUNT:
57  return {target_info.is_distinct ? "agg_count_distinct" : "agg_count"};
58  case kMAX:
59  return {"agg_max"};
60  case kMIN:
61  return {"agg_min"};
62  case kSUM:
63  return {"agg_sum"};
65  return {"agg_approximate_count_distinct"};
66  case kSAMPLE:
67  return {"agg_id"};
68  default:
69  UNREACHABLE() << "Unrecognized agg kind: " << std::to_string(target_info.agg_kind);
70  }
71  return {};
72 }
73 
75  return query_mem_desc.getQueryDescriptionType() == QueryDescriptionType::Projection &&
76  query_mem_desc.didOutputColumnar();
77 }
78 
79 } // namespace
80 
82  GroupByAndAggregate* group_by_and_agg,
83  Executor* executor,
85  const CompilationOptions& co,
86  const std::tuple<llvm::Value*, llvm::Value*>& agg_out_ptr_w_idx_in,
87  const std::vector<llvm::Value*>& agg_out_vec,
88  llvm::Value* output_buffer_byte_stream,
89  llvm::Value* out_row_idx,
90  GroupByAndAggregate::DiamondCodegen& diamond_codegen,
91  GroupByAndAggregate::DiamondCodegen* sample_cfg) const {
92  CHECK(group_by_and_agg);
93  CHECK(executor);
94 
95  auto agg_out_ptr_w_idx = agg_out_ptr_w_idx_in;
96  const auto arg_expr = agg_arg(target_expr);
97 
98  const auto agg_fn_names = agg_fn_base_names(target_info);
99  const auto window_func = dynamic_cast<const Analyzer::WindowFunction*>(target_expr);
101  auto target_lvs =
102  window_func
103  ? std::vector<llvm::Value*>{executor->codegenWindowFunction(target_idx, co)}
104  : group_by_and_agg->codegenAggArg(target_expr, co);
105  const auto window_row_ptr = window_func
106  ? group_by_and_agg->codegenWindowRowPointer(
107  window_func, query_mem_desc, co, diamond_codegen)
108  : nullptr;
109  if (window_row_ptr) {
110  agg_out_ptr_w_idx =
111  std::make_tuple(window_row_ptr, std::get<1>(agg_out_ptr_w_idx_in));
112  if (window_function_is_aggregate(window_func->getKind())) {
113  out_row_idx = window_row_ptr;
114  }
115  }
116 
117  llvm::Value* str_target_lv{nullptr};
118  const bool target_has_geo = target_info.is_agg ? target_info.agg_arg_type.is_geometry()
120  if (target_lvs.size() == 3 && !target_has_geo) {
121  // none encoding string, pop the packed pointer + length since
122  // it's only useful for IS NULL checks and assumed to be only
123  // two components (pointer and length) for the purpose of projection
124  str_target_lv = target_lvs.front();
125  target_lvs.erase(target_lvs.begin());
126  }
128  // Geo cols are expanded to the physical coord cols. Each physical coord col is an
129  // array. Ensure that the target values generated match the number of agg
130  // functions before continuing
131  if (target_lvs.size() < agg_fn_names.size()) {
132  CHECK_EQ(target_lvs.size(), agg_fn_names.size() / 2);
133  std::vector<llvm::Value*> new_target_lvs;
134  new_target_lvs.reserve(agg_fn_names.size());
135  for (const auto& target_lv : target_lvs) {
136  new_target_lvs.push_back(target_lv);
137  new_target_lvs.push_back(target_lv);
138  }
139  target_lvs = new_target_lvs;
140  }
141  }
142  if (target_lvs.size() < agg_fn_names.size()) {
143  CHECK_EQ(size_t(1), target_lvs.size());
144  CHECK_EQ(size_t(2), agg_fn_names.size());
145  for (size_t i = 1; i < agg_fn_names.size(); ++i) {
146  target_lvs.push_back(target_lvs.front());
147  }
148  } else {
149  if (target_has_geo) {
150  if (!target_info.is_agg) {
151  CHECK_EQ(static_cast<size_t>(2 * target_info.sql_type.get_physical_coord_cols()),
152  target_lvs.size());
153  CHECK_EQ(agg_fn_names.size(), target_lvs.size());
154  }
155  } else {
156  CHECK(str_target_lv || (agg_fn_names.size() == target_lvs.size()));
157  CHECK(target_lvs.size() == 1 || target_lvs.size() == 2);
158  }
159  }
160 
161  int32_t slot_index = base_slot_index;
162  CHECK_GE(slot_index, 0);
163  CHECK(is_group_by || static_cast<size_t>(slot_index) < agg_out_vec.size());
164 
165  uint32_t col_off{0};
166  const bool is_simple_count =
168  if (co.device_type_ == ExecutorDeviceType::GPU && query_mem_desc.threadsShareMemory() &&
169  is_simple_count && (!arg_expr || arg_expr->get_type_info().get_notnull())) {
170  CHECK_EQ(size_t(1), agg_fn_names.size());
171  const auto chosen_bytes = query_mem_desc.getPaddedSlotWidthBytes(slot_index);
172  llvm::Value* agg_col_ptr{nullptr};
173  if (is_group_by) {
174  if (query_mem_desc.didOutputColumnar()) {
175  col_off = query_mem_desc.getColOffInBytes(slot_index);
176  CHECK_EQ(size_t(0), col_off % chosen_bytes);
177  col_off /= chosen_bytes;
178  CHECK(std::get<1>(agg_out_ptr_w_idx));
179  auto offset =
180  LL_BUILDER.CreateAdd(std::get<1>(agg_out_ptr_w_idx), LL_INT(col_off));
181  agg_col_ptr = LL_BUILDER.CreateGEP(
182  LL_BUILDER.CreateBitCast(
183  std::get<0>(agg_out_ptr_w_idx),
184  llvm::PointerType::get(get_int_type((chosen_bytes << 3), LL_CONTEXT), 0)),
185  offset);
186  } else {
187  col_off = query_mem_desc.getColOnlyOffInBytes(slot_index);
188  CHECK_EQ(size_t(0), col_off % chosen_bytes);
189  col_off /= chosen_bytes;
190  agg_col_ptr = LL_BUILDER.CreateGEP(
191  LL_BUILDER.CreateBitCast(
192  std::get<0>(agg_out_ptr_w_idx),
193  llvm::PointerType::get(get_int_type((chosen_bytes << 3), LL_CONTEXT), 0)),
194  LL_INT(col_off));
195  }
196  }
197 
198  if (chosen_bytes != sizeof(int32_t)) {
199  CHECK_EQ(8, chosen_bytes);
200  if (g_bigint_count) {
201  const auto acc_i64 = LL_BUILDER.CreateBitCast(
202  is_group_by ? agg_col_ptr : agg_out_vec[slot_index],
203  llvm::PointerType::get(get_int_type(64, LL_CONTEXT), 0));
204  LL_BUILDER.CreateAtomicRMW(llvm::AtomicRMWInst::Add,
205  acc_i64,
206  LL_INT(int64_t(1)),
207  llvm::AtomicOrdering::Monotonic);
208  } else {
209  const auto acc_i32 = LL_BUILDER.CreateBitCast(
210  is_group_by ? agg_col_ptr : agg_out_vec[slot_index],
211  llvm::PointerType::get(get_int_type(32, LL_CONTEXT), 0));
212  LL_BUILDER.CreateAtomicRMW(llvm::AtomicRMWInst::Add,
213  acc_i32,
214  LL_INT(1),
215  llvm::AtomicOrdering::Monotonic);
216  }
217  } else {
218  const auto acc_i32 = (is_group_by ? agg_col_ptr : agg_out_vec[slot_index]);
219  if (query_mem_desc.getGpuMemSharing() ==
221  // Atomic operation on address space level 3 (Shared):
222  const auto shared_acc_i32 = LL_BUILDER.CreatePointerCast(
223  acc_i32, llvm::Type::getInt32PtrTy(LL_CONTEXT, 3));
224  LL_BUILDER.CreateAtomicRMW(llvm::AtomicRMWInst::Add,
225  shared_acc_i32,
226  LL_INT(1),
227  llvm::AtomicOrdering::Monotonic);
228  } else {
229  LL_BUILDER.CreateAtomicRMW(llvm::AtomicRMWInst::Add,
230  acc_i32,
231  LL_INT(1),
232  llvm::AtomicOrdering::Monotonic);
233  }
234  }
235  return;
236  }
237 
238  size_t target_lv_idx = 0;
239  const bool lazy_fetched{executor->plan_state_->isLazyFetchColumn(target_expr)};
240 
241  CodeGenerator code_generator(executor);
242  for (const auto& agg_base_name : agg_fn_names) {
243  if (target_info.is_distinct && arg_expr->get_type_info().is_array()) {
244  CHECK_EQ(static_cast<size_t>(query_mem_desc.getLogicalSlotWidthBytes(slot_index)),
245  sizeof(int64_t));
246  // TODO(miyu): check if buffer may be columnar here
247  CHECK(!query_mem_desc.didOutputColumnar());
248  const auto& elem_ti = arg_expr->get_type_info().get_elem_type();
249  if (is_group_by) {
250  col_off = query_mem_desc.getColOnlyOffInBytes(slot_index);
251  CHECK_EQ(size_t(0), col_off % sizeof(int64_t));
252  col_off /= sizeof(int64_t);
253  }
254  executor->cgen_state_->emitExternalCall(
255  "agg_count_distinct_array_" + numeric_type_name(elem_ti),
256  llvm::Type::getVoidTy(LL_CONTEXT),
257  {is_group_by
258  ? LL_BUILDER.CreateGEP(std::get<0>(agg_out_ptr_w_idx), LL_INT(col_off))
259  : agg_out_vec[slot_index],
260  target_lvs[target_lv_idx],
261  code_generator.posArg(arg_expr),
262  elem_ti.is_fp()
263  ? static_cast<llvm::Value*>(executor->cgen_state_->inlineFpNull(elem_ti))
264  : static_cast<llvm::Value*>(
265  executor->cgen_state_->inlineIntNull(elem_ti))});
266  ++slot_index;
267  ++target_lv_idx;
268  continue;
269  }
270 
271  llvm::Value* agg_col_ptr{nullptr};
272  const auto chosen_bytes =
273  static_cast<size_t>(query_mem_desc.getPaddedSlotWidthBytes(slot_index));
274  const auto& chosen_type = get_compact_type(target_info);
275  const auto& arg_type =
276  ((arg_expr && arg_expr->get_type_info().get_type() != kNULLT) &&
280  const bool is_fp_arg =
281  !lazy_fetched && arg_type.get_type() != kNULLT && arg_type.is_fp();
282  if (is_group_by) {
283  agg_col_ptr = group_by_and_agg->codegenAggColumnPtr(output_buffer_byte_stream,
284  out_row_idx,
285  agg_out_ptr_w_idx,
286  query_mem_desc,
287  chosen_bytes,
288  slot_index,
289  target_idx);
290  CHECK(agg_col_ptr);
291  agg_col_ptr->setName("agg_col_ptr");
292  }
293 
294  const bool float_argument_input = takes_float_argument(target_info);
295  const bool is_count_in_avg = target_info.agg_kind == kAVG && target_lv_idx == 1;
296  // The count component of an average should never be compacted.
297  const auto agg_chosen_bytes =
298  float_argument_input && !is_count_in_avg ? sizeof(float) : chosen_bytes;
299  if (float_argument_input) {
300  CHECK_GE(chosen_bytes, sizeof(float));
301  }
302 
303  auto target_lv = target_lvs[target_lv_idx];
304  const auto needs_unnest_double_patch = group_by_and_agg->needsUnnestDoublePatch(
305  target_lv, agg_base_name, query_mem_desc.threadsShareMemory(), co);
306  const auto need_skip_null = !needs_unnest_double_patch && target_info.skip_null_val;
307  if (!needs_unnest_double_patch) {
308  if (need_skip_null && !is_agg_domain_range_equivalent(target_info.agg_kind)) {
309  target_lv = group_by_and_agg->convertNullIfAny(arg_type, target_info, target_lv);
310  } else if (is_fp_arg) {
311  target_lv = executor->castToFP(target_lv);
312  }
313  if (!dynamic_cast<const Analyzer::AggExpr*>(target_expr) || arg_expr) {
314  target_lv =
315  executor->cgen_state_->castToTypeIn(target_lv, (agg_chosen_bytes << 3));
316  }
317  }
318 
319  std::vector<llvm::Value*> agg_args{
320  executor->castToIntPtrTyIn((is_group_by ? agg_col_ptr : agg_out_vec[slot_index]),
321  (agg_chosen_bytes << 3)),
322  (is_simple_count && !arg_expr)
323  ? (agg_chosen_bytes == sizeof(int32_t) ? LL_INT(int32_t(0))
324  : LL_INT(int64_t(0)))
325  : (is_simple_count && arg_expr && str_target_lv ? str_target_lv : target_lv)};
326  if (query_mem_desc.isLogicalSizedColumnsAllowed()) {
327  if (is_simple_count && arg_expr && str_target_lv) {
328  agg_args[1] =
329  agg_chosen_bytes == sizeof(int32_t) ? LL_INT(int32_t(0)) : LL_INT(int64_t(0));
330  }
331  }
332  std::string agg_fname{agg_base_name};
333  if (is_fp_arg) {
334  if (!lazy_fetched) {
335  if (agg_chosen_bytes == sizeof(float)) {
336  CHECK_EQ(arg_type.get_type(), kFLOAT);
337  agg_fname += "_float";
338  } else {
339  CHECK_EQ(agg_chosen_bytes, sizeof(double));
340  agg_fname += "_double";
341  }
342  }
343  } else if (agg_chosen_bytes == sizeof(int32_t)) {
344  agg_fname += "_int32";
345  } else if (agg_chosen_bytes == sizeof(int16_t) &&
346  query_mem_desc.didOutputColumnar()) {
347  agg_fname += "_int16";
348  } else if (agg_chosen_bytes == sizeof(int8_t) && query_mem_desc.didOutputColumnar()) {
349  agg_fname += "_int8";
350  }
351 
353  CHECK_EQ(agg_chosen_bytes, sizeof(int64_t));
354  CHECK(!chosen_type.is_fp());
355  group_by_and_agg->codegenCountDistinct(
356  target_idx, target_expr, agg_args, query_mem_desc, co.device_type_);
357  } else {
358  const auto& arg_ti = target_info.agg_arg_type;
359  if (need_skip_null && !arg_ti.is_geometry()) {
360  agg_fname += "_skip_val";
361  llvm::Value* null_in_lv{nullptr};
362  if (arg_ti.is_fp()) {
363  null_in_lv =
364  static_cast<llvm::Value*>(executor->cgen_state_->inlineFpNull(arg_ti));
365  } else {
366  null_in_lv = static_cast<llvm::Value*>(executor->cgen_state_->inlineIntNull(
368  ? arg_ti
369  : target_info.sql_type));
370  }
371  CHECK(null_in_lv);
372  auto null_lv =
373  executor->cgen_state_->castToTypeIn(null_in_lv, (agg_chosen_bytes << 3));
374  agg_args.push_back(null_lv);
375  }
376  if (!target_info.is_distinct) {
378  query_mem_desc.threadsShareMemory()) {
379  agg_fname += "_shared";
380  if (needs_unnest_double_patch) {
381  agg_fname = patch_agg_fname(agg_fname);
382  }
383  }
384  group_by_and_agg->emitCall(agg_fname, agg_args);
385  }
386  }
387  if (window_func && window_function_requires_peer_handling(window_func)) {
388  const auto window_func_context =
390  const auto pending_outputs =
391  LL_INT(window_func_context->aggregateStatePendingOutputs());
392  executor->cgen_state_->emitExternalCall("add_window_pending_output",
393  llvm::Type::getVoidTy(LL_CONTEXT),
394  {agg_args.front(), pending_outputs});
395  const auto& window_func_ti = window_func->get_type_info();
396  std::string apply_window_pending_outputs_name = "apply_window_pending_outputs";
397  switch (window_func_ti.get_type()) {
398  case kFLOAT: {
399  apply_window_pending_outputs_name += "_float";
400  if (query_mem_desc.didOutputColumnar()) {
401  apply_window_pending_outputs_name += "_columnar";
402  }
403  break;
404  }
405  case kDOUBLE: {
406  apply_window_pending_outputs_name += "_double";
407  break;
408  }
409  default: {
410  apply_window_pending_outputs_name += "_int";
411  if (query_mem_desc.didOutputColumnar()) {
412  apply_window_pending_outputs_name +=
413  std::to_string(window_func_ti.get_size() * 8);
414  } else {
415  apply_window_pending_outputs_name += "64";
416  }
417  break;
418  }
419  }
420  const auto partition_end =
421  LL_INT(reinterpret_cast<int64_t>(window_func_context->partitionEnd()));
422  executor->cgen_state_->emitExternalCall(apply_window_pending_outputs_name,
423  llvm::Type::getVoidTy(LL_CONTEXT),
424  {pending_outputs,
425  target_lvs.front(),
426  partition_end,
427  code_generator.posArg(nullptr)});
428  }
429 
430  ++slot_index;
431  ++target_lv_idx;
432  }
433 }
434 
436  const Executor* executor,
437  const CompilationOptions& co) {
438  if (query_mem_desc.getPaddedSlotWidthBytes(slot_index_counter) == 0) {
439  CHECK(!dynamic_cast<const Analyzer::AggExpr*>(target_expr));
440  ++slot_index_counter;
441  ++target_index_counter;
442  return;
443  }
444  if (dynamic_cast<const Analyzer::UOper*>(target_expr) &&
445  static_cast<const Analyzer::UOper*>(target_expr)->get_optype() == kUNNEST) {
446  throw std::runtime_error("UNNEST not supported in the projection list yet.");
447  }
448  if ((executor->plan_state_->isLazyFetchColumn(target_expr) || !is_group_by) &&
449  (static_cast<size_t>(query_mem_desc.getPaddedSlotWidthBytes(slot_index_counter)) <
450  sizeof(int64_t)) &&
451  !is_columnar_projection(query_mem_desc)) {
452  // TODO(miyu): enable different byte width in the layout w/o padding
454  }
455 
456  auto target_info = get_target_info(target_expr, g_bigint_count);
457  auto arg_expr = agg_arg(target_expr);
458  if (arg_expr) {
459  if (target_info.agg_kind == kSAMPLE) {
460  target_info.skip_null_val = false;
461  } else if (query_mem_desc.getQueryDescriptionType() ==
463  !arg_expr->get_type_info().is_varlen()) {
464  // TODO: COUNT is currently not null-aware for varlen types. Need to add proper code
465  // generation for handling varlen nulls.
466  target_info.skip_null_val = true;
467  } else if (constrained_not_null(arg_expr, ra_exe_unit.quals)) {
468  target_info.skip_null_val = false;
469  }
470  }
471 
472  if (!(query_mem_desc.getQueryDescriptionType() ==
476  sample_exprs_to_codegen.emplace_back(target_expr,
477  target_info,
478  slot_index_counter,
479  target_index_counter++,
480  is_group_by);
481  } else {
482  target_exprs_to_codegen.emplace_back(target_expr,
483  target_info,
484  slot_index_counter,
485  target_index_counter++,
486  is_group_by);
487  }
488 
489  const auto agg_fn_names = agg_fn_base_names(target_info);
490  slot_index_counter += agg_fn_names.size();
491 }
492 
493 namespace {
494 
496  const QueryMemoryDescriptor& query_mem_desc) {
497  const bool is_group_by{query_mem_desc.isGroupBy()};
498  if (target_info.agg_kind == kSAMPLE && target_info.sql_type.is_string() &&
499  target_info.sql_type.get_compression() != kENCODING_NONE) {
500  return get_agg_initial_val(target_info.agg_kind,
501  target_info.sql_type,
502  is_group_by,
503  query_mem_desc.getCompactByteWidth());
504  }
505  return 0;
506 }
507 
508 } // namespace
509 
511  GroupByAndAggregate* group_by_and_agg,
512  Executor* executor,
513  const QueryMemoryDescriptor& query_mem_desc,
514  const CompilationOptions& co,
515  const std::tuple<llvm::Value*, llvm::Value*>& agg_out_ptr_w_idx,
516  const std::vector<llvm::Value*>& agg_out_vec,
517  llvm::Value* output_buffer_byte_stream,
518  llvm::Value* out_row_idx,
519  GroupByAndAggregate::DiamondCodegen& diamond_codegen) const {
520  CHECK(group_by_and_agg);
521  CHECK(executor);
522 
523  for (const auto& target_expr_codegen : target_exprs_to_codegen) {
524  target_expr_codegen.codegen(group_by_and_agg,
525  executor,
526  query_mem_desc,
527  co,
528  agg_out_ptr_w_idx,
529  agg_out_vec,
530  output_buffer_byte_stream,
531  out_row_idx,
532  diamond_codegen);
533  }
534  if (!sample_exprs_to_codegen.empty()) {
535  codegenSampleExpressions(group_by_and_agg,
536  executor,
537  query_mem_desc,
538  co,
539  agg_out_ptr_w_idx,
540  agg_out_vec,
541  output_buffer_byte_stream,
542  out_row_idx,
543  diamond_codegen);
544  }
545 }
546 
548  GroupByAndAggregate* group_by_and_agg,
549  Executor* executor,
550  const QueryMemoryDescriptor& query_mem_desc,
551  const CompilationOptions& co,
552  const std::tuple<llvm::Value*, llvm::Value*>& agg_out_ptr_w_idx,
553  const std::vector<llvm::Value*>& agg_out_vec,
554  llvm::Value* output_buffer_byte_stream,
555  llvm::Value* out_row_idx,
556  GroupByAndAggregate::DiamondCodegen& diamond_codegen) const {
557  CHECK(!sample_exprs_to_codegen.empty());
559  if (sample_exprs_to_codegen.size() == 1 &&
560  !sample_exprs_to_codegen.front().target_info.sql_type.is_varlen()) {
561  codegenSingleSlotSampleExpression(group_by_and_agg,
562  executor,
563  query_mem_desc,
564  co,
565  agg_out_ptr_w_idx,
566  agg_out_vec,
567  output_buffer_byte_stream,
568  out_row_idx,
569  diamond_codegen);
570  } else {
571  codegenMultiSlotSampleExpressions(group_by_and_agg,
572  executor,
573  query_mem_desc,
574  co,
575  agg_out_ptr_w_idx,
576  agg_out_vec,
577  output_buffer_byte_stream,
578  out_row_idx,
579  diamond_codegen);
580  }
581 }
582 
584  GroupByAndAggregate* group_by_and_agg,
585  Executor* executor,
586  const QueryMemoryDescriptor& query_mem_desc,
587  const CompilationOptions& co,
588  const std::tuple<llvm::Value*, llvm::Value*>& agg_out_ptr_w_idx,
589  const std::vector<llvm::Value*>& agg_out_vec,
590  llvm::Value* output_buffer_byte_stream,
591  llvm::Value* out_row_idx,
592  GroupByAndAggregate::DiamondCodegen& diamond_codegen) const {
593  CHECK_EQ(size_t(1), sample_exprs_to_codegen.size());
594  CHECK(!sample_exprs_to_codegen.front().target_info.sql_type.is_varlen());
596  // no need for the atomic if we only have one SAMPLE target
597  sample_exprs_to_codegen.front().codegen(group_by_and_agg,
598  executor,
599  query_mem_desc,
600  co,
601  agg_out_ptr_w_idx,
602  agg_out_vec,
603  output_buffer_byte_stream,
604  out_row_idx,
605  diamond_codegen);
606 }
607 
609  GroupByAndAggregate* group_by_and_agg,
610  Executor* executor,
611  const QueryMemoryDescriptor& query_mem_desc,
612  const CompilationOptions& co,
613  const std::tuple<llvm::Value*, llvm::Value*>& agg_out_ptr_w_idx,
614  const std::vector<llvm::Value*>& agg_out_vec,
615  llvm::Value* output_buffer_byte_stream,
616  llvm::Value* out_row_idx,
617  GroupByAndAggregate::DiamondCodegen& diamond_codegen) const {
618  CHECK(sample_exprs_to_codegen.size() > 1 ||
619  sample_exprs_to_codegen.front().target_info.sql_type.is_varlen());
621  const auto& first_sample_expr = sample_exprs_to_codegen.front();
622  auto target_lvs = group_by_and_agg->codegenAggArg(first_sample_expr.target_expr, co);
623  CHECK_GE(target_lvs.size(), size_t(1));
624 
625  const auto init_val =
626  get_initial_agg_val(first_sample_expr.target_info, query_mem_desc);
627 
628  llvm::Value* agg_col_ptr{nullptr};
629  if (is_group_by) {
630  const auto agg_column_size_bytes =
631  query_mem_desc.isLogicalSizedColumnsAllowed() &&
632  !first_sample_expr.target_info.sql_type.is_varlen()
633  ? first_sample_expr.target_info.sql_type.get_size()
634  : sizeof(int64_t);
635  agg_col_ptr = group_by_and_agg->codegenAggColumnPtr(output_buffer_byte_stream,
636  out_row_idx,
637  agg_out_ptr_w_idx,
638  query_mem_desc,
639  agg_column_size_bytes,
640  first_sample_expr.base_slot_index,
641  first_sample_expr.target_idx);
642  } else {
643  CHECK_LT(static_cast<size_t>(first_sample_expr.base_slot_index), agg_out_vec.size());
644  agg_col_ptr =
645  executor->castToIntPtrTyIn(agg_out_vec[first_sample_expr.base_slot_index], 64);
646  }
647 
648  auto sample_cas_lv = codegenSlotEmptyKey(agg_col_ptr, target_lvs, executor, init_val);
649 
651  sample_cas_lv, executor, false, "sample_valcheck", &diamond_codegen, false);
652 
653  for (const auto& target_expr_codegen : sample_exprs_to_codegen) {
654  target_expr_codegen.codegen(group_by_and_agg,
655  executor,
656  query_mem_desc,
657  co,
658  agg_out_ptr_w_idx,
659  agg_out_vec,
660  output_buffer_byte_stream,
661  out_row_idx,
662  diamond_codegen,
663  &sample_cfg);
664  }
665 }
666 
668  llvm::Value* agg_col_ptr,
669  std::vector<llvm::Value*>& target_lvs,
670  Executor* executor,
671  const int64_t init_val) const {
672  const auto& first_sample_expr = sample_exprs_to_codegen.front();
673  const auto first_sample_slot_bytes =
674  first_sample_expr.target_info.sql_type.is_varlen()
675  ? sizeof(int64_t)
676  : first_sample_expr.target_info.sql_type.get_size();
677  llvm::Value* target_lv_casted{nullptr};
678  // deciding whether proper casting is required for the first sample's slot:
679  if (first_sample_expr.target_info.sql_type.is_varlen()) {
680  target_lv_casted =
681  LL_BUILDER.CreatePtrToInt(target_lvs.front(), llvm::Type::getInt64Ty(LL_CONTEXT));
682  } else if (first_sample_expr.target_info.sql_type.is_fp()) {
683  // Initialization value for SAMPLE on a float column should be 0
684  CHECK_EQ(init_val, 0);
685  if (query_mem_desc.isLogicalSizedColumnsAllowed()) {
686  target_lv_casted = executor->cgen_state_->ir_builder_.CreateFPToSI(
687  target_lvs.front(),
688  first_sample_slot_bytes == sizeof(float) ? llvm::Type::getInt32Ty(LL_CONTEXT)
689  : llvm::Type::getInt64Ty(LL_CONTEXT));
690  } else {
691  target_lv_casted = executor->cgen_state_->ir_builder_.CreateFPToSI(
692  target_lvs.front(), llvm::Type::getInt64Ty(LL_CONTEXT));
693  }
694  } else if (first_sample_slot_bytes != sizeof(int64_t) &&
695  !query_mem_desc.isLogicalSizedColumnsAllowed()) {
696  target_lv_casted =
697  executor->cgen_state_->ir_builder_.CreateCast(llvm::Instruction::CastOps::SExt,
698  target_lvs.front(),
699  llvm::Type::getInt64Ty(LL_CONTEXT));
700  } else {
701  target_lv_casted = target_lvs.front();
702  }
703 
704  std::string slot_empty_cas_func_name("slotEmptyKeyCAS");
705  llvm::Value* init_val_lv{LL_INT(init_val)};
706  if (query_mem_desc.isLogicalSizedColumnsAllowed() &&
707  !first_sample_expr.target_info.sql_type.is_varlen()) {
708  // add proper suffix to the function name:
709  switch (first_sample_slot_bytes) {
710  case 1:
711  slot_empty_cas_func_name += "_int8";
712  break;
713  case 2:
714  slot_empty_cas_func_name += "_int16";
715  break;
716  case 4:
717  slot_empty_cas_func_name += "_int32";
718  break;
719  case 8:
720  break;
721  default:
722  UNREACHABLE() << "Invalid slot size for slotEmptyKeyCAS function.";
723  break;
724  }
725  if (first_sample_slot_bytes != sizeof(int64_t)) {
726  init_val_lv = llvm::ConstantInt::get(
727  get_int_type(first_sample_slot_bytes * 8, LL_CONTEXT), init_val);
728  }
729  }
730 
731  auto sample_cas_lv = executor->cgen_state_->emitExternalCall(
732  slot_empty_cas_func_name,
733  llvm::Type::getInt1Ty(executor->cgen_state_->context_),
734  {agg_col_ptr, target_lv_casted, init_val_lv});
735  return sample_cas_lv;
736 }
#define LL_BUILDER
const Analyzer::Expr * agg_arg(const Analyzer::Expr *expr)
#define CHECK_EQ(x, y)
Definition: Logger.h:198
bool constrained_not_null(const Analyzer::Expr *expr, const std::list< std::shared_ptr< Analyzer::Expr >> &quals)
std::vector< std::string > agg_fn_base_names(const TargetInfo &target_info)
llvm::Value * codegenAggColumnPtr(llvm::Value *output_buffer_byte_stream, llvm::Value *out_row_idx, const std::tuple< llvm::Value *, llvm::Value * > &agg_out_ptr_w_idx, const QueryMemoryDescriptor &query_mem_desc, const size_t chosen_bytes, const size_t agg_out_off, const size_t target_idx)
: returns the pointer to where the aggregation should be stored.
void codegenSampleExpressions(GroupByAndAggregate *group_by_and_agg, Executor *executor, const QueryMemoryDescriptor &query_mem_desc, const CompilationOptions &co, const std::tuple< llvm::Value *, llvm::Value * > &agg_out_ptr_w_idx, const std::vector< llvm::Value * > &agg_out_vec, llvm::Value *output_buffer_byte_stream, llvm::Value *out_row_idx, GroupByAndAggregate::DiamondCodegen &diamond_codegen) const
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:334
bool isLogicalSizedColumnsAllowed() const
SQLTypeInfo sql_type
Definition: TargetInfo.h:42
TargetInfo get_target_info(const PointerType target_expr, const bool bigint_count)
Definition: TargetInfo.h:65
llvm::Value * posArg(const Analyzer::Expr *) const
Definition: ColumnIR.cpp:503
#define UNREACHABLE()
Definition: Logger.h:234
#define CHECK_GE(x, y)
Definition: Logger.h:203
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
int64_t get_agg_initial_val(const SQLAgg agg, const SQLTypeInfo &ti, const bool enable_compaction, const unsigned min_byte_width_to_compact)
bool needsUnnestDoublePatch(llvm::Value *val_ptr, const std::string &agg_base_name, const bool threads_share_memory, const CompilationOptions &co) const
bool takes_float_argument(const TargetInfo &target_info)
Definition: TargetInfo.h:120
bool skip_null_val
Definition: TargetInfo.h:44
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
std::string to_string(char const *&&v)
SQLTypeInfo agg_arg_type
Definition: TargetInfo.h:43
llvm::Value * codegenSlotEmptyKey(llvm::Value *agg_col_ptr, std::vector< llvm::Value * > &target_lvs, Executor *executor, const int64_t init_val) const
std::string patch_agg_fname(const std::string &agg_name)
Helpers for codegen of target expressions.
void codegenSingleSlotSampleExpression(GroupByAndAggregate *group_by_and_agg, Executor *executor, const QueryMemoryDescriptor &query_mem_desc, const CompilationOptions &co, const std::tuple< llvm::Value *, llvm::Value * > &agg_out_ptr_w_idx, const std::vector< llvm::Value * > &agg_out_vec, llvm::Value *output_buffer_byte_stream, llvm::Value *out_row_idx, GroupByAndAggregate::DiamondCodegen &diamond_codegen) const
size_t getColOnlyOffInBytes(const size_t col_idx) const
Definition: sqldefs.h:71
const SQLTypeInfo get_compact_type(const TargetInfo &target)
void codegen(GroupByAndAggregate *group_by_and_agg, Executor *executor, const QueryMemoryDescriptor &query_mem_desc, const CompilationOptions &co, const std::tuple< llvm::Value *, llvm::Value * > &agg_out_ptr_w_idx, const std::vector< llvm::Value * > &agg_out_vec, llvm::Value *output_buffer_byte_stream, llvm::Value *out_row_idx, GroupByAndAggregate::DiamondCodegen &diamond_codegen, GroupByAndAggregate::DiamondCodegen *sample_cfg=nullptr) const
bool is_agg
Definition: TargetInfo.h:40
CHECK(cgen_state)
void codegen(GroupByAndAggregate *group_by_and_agg, Executor *executor, const QueryMemoryDescriptor &query_mem_desc, const CompilationOptions &co, const std::tuple< llvm::Value *, llvm::Value * > &agg_out_ptr_w_idx, const std::vector< llvm::Value * > &agg_out_vec, llvm::Value *output_buffer_byte_stream, llvm::Value *out_row_idx, GroupByAndAggregate::DiamondCodegen &diamond_codegen) const
#define LL_INT(v)
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:326
llvm::Value * convertNullIfAny(const SQLTypeInfo &arg_type, const TargetInfo &agg_info, llvm::Value *target)
int get_physical_coord_cols() const
Definition: sqltypes.h:362
bool g_bigint_count
Definition: sqldefs.h:71
#define LL_CONTEXT
bool is_distinct_target(const TargetInfo &target_info)
Definition: TargetInfo.h:116
void codegenCountDistinct(const size_t target_idx, const Analyzer::Expr *target_expr, std::vector< llvm::Value * > &agg_args, const QueryMemoryDescriptor &, const ExecutorDeviceType)
const int8_t getPaddedSlotWidthBytes(const size_t slot_idx) const
SQLAgg agg_kind
Definition: TargetInfo.h:41
bool is_geometry() const
Definition: sqltypes.h:489
ExecutorDeviceType device_type_
QueryDescriptionType getQueryDescriptionType() const
bool window_function_is_aggregate(const SqlWindowFunctionKind kind)
Definition: WindowContext.h:42
#define CHECK_LT(x, y)
Definition: Logger.h:200
static void resetWindowFunctionContext()
const Analyzer::Expr * target_expr
Definition: sqldefs.h:71
std::vector< llvm::Value * > codegenAggArg(const Analyzer::Expr *target_expr, const CompilationOptions &co)
llvm::Value * codegenWindowRowPointer(const Analyzer::WindowFunction *window_func, const QueryMemoryDescriptor &query_mem_desc, const CompilationOptions &co, DiamondCodegen &diamond_codegen)
bool window_function_requires_peer_handling(const Analyzer::WindowFunction *window_func)
void codegenMultiSlotSampleExpressions(GroupByAndAggregate *group_by_and_agg, Executor *executor, const QueryMemoryDescriptor &query_mem_desc, const CompilationOptions &co, const std::tuple< llvm::Value *, llvm::Value * > &agg_out_ptr_w_idx, const std::vector< llvm::Value * > &agg_out_vec, llvm::Value *output_buffer_byte_stream, llvm::Value *out_row_idx, GroupByAndAggregate::DiamondCodegen &diamond_codegen) const
bool is_string() const
Definition: sqltypes.h:477
int64_t get_initial_agg_val(const TargetInfo &target_info, const QueryMemoryDescriptor &query_mem_desc)
GroupByMemSharing getGpuMemSharing() const
std::string numeric_type_name(const SQLTypeInfo &ti)
Definition: Execute.h:160
bool is_distinct
Definition: TargetInfo.h:45
void operator()(const Analyzer::Expr *target_expr, const Executor *executor, const CompilationOptions &co)
Definition: sqldefs.h:71
const int8_t getLogicalSlotWidthBytes(const size_t slot_idx) const
Definition: sqldefs.h:71
size_t getColOffInBytes(const size_t col_idx) const
bool is_columnar_projection(const QueryMemoryDescriptor &query_mem_desc)
static WindowFunctionContext * getActiveWindowFunctionContext()
bool is_agg_domain_range_equivalent(const SQLAgg &agg_kind)
Definition: TargetInfo.h:52