OmniSciDB  ca0c39ec8f
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
anonymous_namespace{WindowContext.cpp} Namespace Reference

Functions

std::vector< int64_t > index_to_row_number (const int64_t *index, const size_t index_size)
 
bool advance_current_rank (const std::function< bool(const int64_t lhs, const int64_t rhs)> &comparator, const int64_t *index, const size_t i)
 
std::vector< int64_t > index_to_rank (const int64_t *index, const size_t index_size, const std::function< bool(const int64_t lhs, const int64_t rhs)> &comparator)
 
std::vector< int64_t > index_to_dense_rank (const int64_t *index, const size_t index_size, const std::function< bool(const int64_t lhs, const int64_t rhs)> &comparator)
 
std::vector< double > index_to_percent_rank (const int64_t *index, const size_t index_size, const std::function< bool(const int64_t lhs, const int64_t rhs)> &comparator)
 
std::vector< double > index_to_cume_dist (const int64_t *index, const size_t index_size, const std::function< bool(const int64_t lhs, const int64_t rhs)> &comparator)
 
std::vector< int64_t > index_to_ntile (const int64_t *index, const size_t index_size, const size_t n)
 
size_t window_function_buffer_element_size (const SqlWindowFunctionKind)
 
size_t get_int_constant_from_expr (const Analyzer::Expr *expr)
 
int64_t get_lag_or_lead_argument (const Analyzer::WindowFunction *window_func)
 
void apply_permutation_to_partition (int64_t *output_for_partition_buff, const int32_t *original_indices, const size_t partition_size)
 
void apply_lag_to_partition (const int64_t lag, const int32_t *original_indices, int64_t *sorted_indices, const size_t partition_size)
 
void apply_first_value_to_partition (const int32_t *original_indices, int64_t *output_for_partition_buff, const size_t partition_size)
 
void apply_last_value_to_partition (const int32_t *original_indices, int64_t *output_for_partition_buff, const size_t partition_size)
 
void index_to_partition_end (const int8_t *partition_end, const size_t off, const int64_t *index, const size_t index_size, const std::function< bool(const int64_t lhs, const int64_t rhs)> &comparator)
 
bool pos_is_set (const int64_t bitset, const int64_t pos)
 
template<class T >
void apply_window_pending_outputs_int (const int64_t handle, const int64_t value, const int64_t bitset, const int64_t pos)
 
template<class T >
WindowFunctionContext::WindowComparatorResult integer_comparator (const int8_t *order_column_buffer, const SQLTypeInfo &ti, const int32_t *partition_indices, const int64_t lhs, const int64_t rhs, const bool nulls_first)
 
template<class T , class NullPatternType >
WindowFunctionContext::WindowComparatorResult fp_comparator (const int8_t *order_column_buffer, const SQLTypeInfo &ti, const int32_t *partition_indices, const int64_t lhs, const int64_t rhs, const bool nulls_first)
 

Function Documentation

bool anonymous_namespace{WindowContext.cpp}::advance_current_rank ( const std::function< bool(const int64_t lhs, const int64_t rhs)> &  comparator,
const int64_t *  index,
const size_t  i 
)

Definition at line 204 of file WindowContext.cpp.

Referenced by index_to_cume_dist(), index_to_dense_rank(), index_to_partition_end(), index_to_percent_rank(), and index_to_rank().

207  {
208  if (i == 0) {
209  return false;
210  }
211  return comparator(index[i - 1], index[i]);
212 }

+ Here is the caller graph for this function:

void anonymous_namespace{WindowContext.cpp}::apply_first_value_to_partition ( const int32_t *  original_indices,
int64_t *  output_for_partition_buff,
const size_t  partition_size 
)

Definition at line 386 of file WindowContext.cpp.

References gpu_enabled::fill().

Referenced by WindowFunctionContext::computePartitionBuffer().

388  {
389  const auto first_value_idx = original_indices[output_for_partition_buff[0]];
390  std::fill(output_for_partition_buff,
391  output_for_partition_buff + partition_size,
392  first_value_idx);
393 }
DEVICE void fill(ARGS &&...args)
Definition: gpu_enabled.h:60

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void anonymous_namespace{WindowContext.cpp}::apply_lag_to_partition ( const int64_t  lag,
const int32_t *  original_indices,
int64_t *  sorted_indices,
const size_t  partition_size 
)

Definition at line 363 of file WindowContext.cpp.

References gpu_enabled::copy().

Referenced by WindowFunctionContext::computePartitionBuffer().

366  {
367  std::vector<int64_t> lag_sorted_indices(partition_size, -1);
368  for (int64_t idx = 0; idx < static_cast<int64_t>(partition_size); ++idx) {
369  int64_t lag_idx = idx - lag;
370  if (lag_idx < 0 || lag_idx >= static_cast<int64_t>(partition_size)) {
371  continue;
372  }
373  lag_sorted_indices[idx] = sorted_indices[lag_idx];
374  }
375  std::vector<int64_t> lag_original_indices(partition_size);
376  for (size_t k = 0; k < partition_size; ++k) {
377  const auto lag_index = lag_sorted_indices[k];
378  lag_original_indices[sorted_indices[k]] =
379  lag_index != -1 ? original_indices[lag_index] : -1;
380  }
381  std::copy(lag_original_indices.begin(), lag_original_indices.end(), sorted_indices);
382 }
DEVICE auto copy(ARGS &&...args)
Definition: gpu_enabled.h:51

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void anonymous_namespace{WindowContext.cpp}::apply_last_value_to_partition ( const int32_t *  original_indices,
int64_t *  output_for_partition_buff,
const size_t  partition_size 
)

Definition at line 397 of file WindowContext.cpp.

References gpu_enabled::copy().

Referenced by WindowFunctionContext::computePartitionBuffer().

399  {
400  std::copy(
401  original_indices, original_indices + partition_size, output_for_partition_buff);
402 }
DEVICE auto copy(ARGS &&...args)
Definition: gpu_enabled.h:51

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void anonymous_namespace{WindowContext.cpp}::apply_permutation_to_partition ( int64_t *  output_for_partition_buff,
const int32_t *  original_indices,
const size_t  partition_size 
)

Definition at line 349 of file WindowContext.cpp.

References gpu_enabled::copy().

Referenced by WindowFunctionContext::computePartitionBuffer().

352  {
353  std::vector<int64_t> new_output_for_partition_buff(partition_size);
354  for (size_t i = 0; i < partition_size; ++i) {
355  new_output_for_partition_buff[i] = original_indices[output_for_partition_buff[i]];
356  }
357  std::copy(new_output_for_partition_buff.begin(),
358  new_output_for_partition_buff.end(),
359  output_for_partition_buff);
360 }
DEVICE auto copy(ARGS &&...args)
Definition: gpu_enabled.h:51

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class T >
void anonymous_namespace{WindowContext.cpp}::apply_window_pending_outputs_int ( const int64_t  handle,
const int64_t  value,
const int64_t  bitset,
const int64_t  pos 
)

Definition at line 427 of file WindowContext.cpp.

References pos_is_set(), and heavydb.dtypes::T.

430  {
431  if (!pos_is_set(bitset, pos)) {
432  return;
433  }
434  auto& pending_output_slots = *reinterpret_cast<std::vector<void*>*>(handle);
435  for (auto pending_output_slot : pending_output_slots) {
436  *reinterpret_cast<T*>(pending_output_slot) = value;
437  }
438  pending_output_slots.clear();
439 }
bool pos_is_set(const int64_t bitset, const int64_t pos)

+ Here is the call graph for this function:

template<class T , class NullPatternType >
WindowFunctionContext::WindowComparatorResult anonymous_namespace{WindowContext.cpp}::fp_comparator ( const int8_t *  order_column_buffer,
const SQLTypeInfo ti,
const int32_t *  partition_indices,
const int64_t  lhs,
const int64_t  rhs,
const bool  nulls_first 
)

Definition at line 1049 of file WindowContext.cpp.

References WindowFunctionContext::EQ, SQLTypeInfo::get_type(), WindowFunctionContext::GT, kFLOAT, WindowFunctionContext::LT, null_val_bit_pattern(), and heavydb.dtypes::T.

1055  {
1056  const auto values = reinterpret_cast<const T*>(order_column_buffer);
1057  const auto lhs_val = values[partition_indices[lhs]];
1058  const auto rhs_val = values[partition_indices[rhs]];
1059  const auto null_bit_pattern = null_val_bit_pattern(ti, ti.get_type() == kFLOAT);
1060  const auto lhs_bit_pattern =
1061  *reinterpret_cast<const NullPatternType*>(may_alias_ptr(&lhs_val));
1062  const auto rhs_bit_pattern =
1063  *reinterpret_cast<const NullPatternType*>(may_alias_ptr(&rhs_val));
1064  if (lhs_bit_pattern == null_bit_pattern && rhs_bit_pattern == null_bit_pattern) {
1066  }
1067  if (lhs_bit_pattern == null_bit_pattern && rhs_bit_pattern != null_bit_pattern) {
1070  }
1071  if (rhs_bit_pattern == null_bit_pattern && lhs_bit_pattern != null_bit_pattern) {
1074  }
1075  if (lhs_val < rhs_val) {
1077  }
1078  if (lhs_val > rhs_val) {
1080  }
1082 }
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:379
int64_t null_val_bit_pattern(const SQLTypeInfo &ti, const bool float_argument_input)

+ Here is the call graph for this function:

size_t anonymous_namespace{WindowContext.cpp}::get_int_constant_from_expr ( const Analyzer::Expr expr)

Definition at line 306 of file WindowContext.cpp.

References logger::FATAL, kBIGINT, kINT, kSMALLINT, and LOG.

Referenced by WindowFunctionContext::computePartitionBuffer(), and get_lag_or_lead_argument().

306  {
307  const auto lag_constant = dynamic_cast<const Analyzer::Constant*>(expr);
308  if (!lag_constant) {
309  throw std::runtime_error("LAG with non-constant lag argument not supported yet");
310  }
311  const auto& lag_ti = lag_constant->get_type_info();
312  switch (lag_ti.get_type()) {
313  case kSMALLINT: {
314  return lag_constant->get_constval().smallintval;
315  }
316  case kINT: {
317  return lag_constant->get_constval().intval;
318  }
319  case kBIGINT: {
320  return lag_constant->get_constval().bigintval;
321  }
322  default: {
323  LOG(FATAL) << "Invalid type for the lag argument";
324  }
325  }
326  return 0;
327 }
#define LOG(tag)
Definition: Logger.h:216
Definition: sqltypes.h:60

+ Here is the caller graph for this function:

int64_t anonymous_namespace{WindowContext.cpp}::get_lag_or_lead_argument ( const Analyzer::WindowFunction window_func)

Definition at line 330 of file WindowContext.cpp.

References run_benchmark_import::args, CHECK, CHECK_EQ, get_int_constant_from_expr(), Analyzer::WindowFunction::getArgs(), Analyzer::WindowFunction::getKind(), LAG, and LEAD.

Referenced by WindowFunctionContext::computePartitionBuffer().

330  {
331  CHECK(window_func->getKind() == SqlWindowFunctionKind::LAG ||
332  window_func->getKind() == SqlWindowFunctionKind::LEAD);
333  const auto& args = window_func->getArgs();
334  if (args.size() == 3) {
335  throw std::runtime_error("LAG with default not supported yet");
336  }
337  if (args.size() == 2) {
338  const int64_t lag_or_lead =
339  static_cast<int64_t>(get_int_constant_from_expr(args[1].get()));
340  return window_func->getKind() == SqlWindowFunctionKind::LAG ? lag_or_lead
341  : -lag_or_lead;
342  }
343  CHECK_EQ(args.size(), size_t(1));
344  return window_func->getKind() == SqlWindowFunctionKind::LAG ? 1 : -1;
345 }
#define CHECK_EQ(x, y)
Definition: Logger.h:230
SqlWindowFunctionKind getKind() const
Definition: Analyzer.h:2454
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs() const
Definition: Analyzer.h:2456
#define CHECK(condition)
Definition: Logger.h:222
size_t get_int_constant_from_expr(const Analyzer::Expr *expr)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::vector<double> anonymous_namespace{WindowContext.cpp}::index_to_cume_dist ( const int64_t *  index,
const size_t  index_size,
const std::function< bool(const int64_t lhs, const int64_t rhs)> &  comparator 
)

Definition at line 264 of file WindowContext.cpp.

References advance_current_rank().

Referenced by WindowFunctionContext::computePartitionBuffer().

267  {
268  std::vector<double> cume_dist(index_size);
269  size_t start_peer_group = 0;
270  while (start_peer_group < index_size) {
271  size_t end_peer_group = start_peer_group + 1;
272  while (end_peer_group < index_size &&
273  !advance_current_rank(comparator, index, end_peer_group)) {
274  ++end_peer_group;
275  }
276  for (size_t i = start_peer_group; i < end_peer_group; ++i) {
277  cume_dist[index[i]] = static_cast<double>(end_peer_group) / index_size;
278  }
279  start_peer_group = end_peer_group;
280  }
281  return cume_dist;
282 }
bool advance_current_rank(const std::function< bool(const int64_t lhs, const int64_t rhs)> &comparator, const int64_t *index, const size_t i)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::vector<int64_t> anonymous_namespace{WindowContext.cpp}::index_to_dense_rank ( const int64_t *  index,
const size_t  index_size,
const std::function< bool(const int64_t lhs, const int64_t rhs)> &  comparator 
)

Definition at line 231 of file WindowContext.cpp.

References advance_current_rank().

Referenced by WindowFunctionContext::computePartitionBuffer().

234  {
235  std::vector<int64_t> dense_rank(index_size);
236  size_t crt_rank = 1;
237  for (size_t i = 0; i < index_size; ++i) {
238  if (advance_current_rank(comparator, index, i)) {
239  ++crt_rank;
240  }
241  dense_rank[index[i]] = crt_rank;
242  }
243  return dense_rank;
244 }
bool advance_current_rank(const std::function< bool(const int64_t lhs, const int64_t rhs)> &comparator, const int64_t *index, const size_t i)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::vector<int64_t> anonymous_namespace{WindowContext.cpp}::index_to_ntile ( const int64_t *  index,
const size_t  index_size,
const size_t  n 
)

Definition at line 285 of file WindowContext.cpp.

Referenced by WindowFunctionContext::computePartitionBuffer().

287  {
288  std::vector<int64_t> row_numbers(index_size);
289  if (!n) {
290  throw std::runtime_error("NTILE argument cannot be zero");
291  }
292  const size_t tile_size = (index_size + n - 1) / n;
293  for (size_t i = 0; i < index_size; ++i) {
294  row_numbers[index[i]] = i / tile_size + 1;
295  }
296  return row_numbers;
297 }
constexpr double n
Definition: Utm.h:38

+ Here is the caller graph for this function:

void anonymous_namespace{WindowContext.cpp}::index_to_partition_end ( const int8_t *  partition_end,
const size_t  off,
const int64_t *  index,
const size_t  index_size,
const std::function< bool(const int64_t lhs, const int64_t rhs)> &  comparator 
)

Definition at line 404 of file WindowContext.cpp.

References advance_current_rank(), agg_count_distinct_bitmap(), and CHECK.

Referenced by WindowFunctionContext::computePartitionBuffer().

409  {
410  int64_t partition_end_handle = reinterpret_cast<int64_t>(partition_end);
411  for (size_t i = 0; i < index_size; ++i) {
412  if (advance_current_rank(comparator, index, i)) {
413  agg_count_distinct_bitmap(&partition_end_handle, off + i - 1, 0);
414  }
415  }
416  CHECK(index_size);
417  agg_count_distinct_bitmap(&partition_end_handle, off + index_size - 1, 0);
418 }
bool advance_current_rank(const std::function< bool(const int64_t lhs, const int64_t rhs)> &comparator, const int64_t *index, const size_t i)
RUNTIME_EXPORT ALWAYS_INLINE void agg_count_distinct_bitmap(int64_t *agg, const int64_t val, const int64_t min_val)
#define CHECK(condition)
Definition: Logger.h:222

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::vector<double> anonymous_namespace{WindowContext.cpp}::index_to_percent_rank ( const int64_t *  index,
const size_t  index_size,
const std::function< bool(const int64_t lhs, const int64_t rhs)> &  comparator 
)

Definition at line 247 of file WindowContext.cpp.

References advance_current_rank().

Referenced by WindowFunctionContext::computePartitionBuffer().

250  {
251  std::vector<double> percent_rank(index_size);
252  size_t crt_rank = 1;
253  for (size_t i = 0; i < index_size; ++i) {
254  if (advance_current_rank(comparator, index, i)) {
255  crt_rank = i + 1;
256  }
257  percent_rank[index[i]] =
258  index_size == 1 ? 0 : static_cast<double>(crt_rank - 1) / (index_size - 1);
259  }
260  return percent_rank;
261 }
bool advance_current_rank(const std::function< bool(const int64_t lhs, const int64_t rhs)> &comparator, const int64_t *index, const size_t i)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::vector<int64_t> anonymous_namespace{WindowContext.cpp}::index_to_rank ( const int64_t *  index,
const size_t  index_size,
const std::function< bool(const int64_t lhs, const int64_t rhs)> &  comparator 
)

Definition at line 215 of file WindowContext.cpp.

References advance_current_rank().

Referenced by WindowFunctionContext::computePartitionBuffer().

218  {
219  std::vector<int64_t> rank(index_size);
220  size_t crt_rank = 1;
221  for (size_t i = 0; i < index_size; ++i) {
222  if (advance_current_rank(comparator, index, i)) {
223  crt_rank = i + 1;
224  }
225  rank[index[i]] = crt_rank;
226  }
227  return rank;
228 }
bool advance_current_rank(const std::function< bool(const int64_t lhs, const int64_t rhs)> &comparator, const int64_t *index, const size_t i)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::vector<int64_t> anonymous_namespace{WindowContext.cpp}::index_to_row_number ( const int64_t *  index,
const size_t  index_size 
)

Definition at line 194 of file WindowContext.cpp.

Referenced by WindowFunctionContext::computePartitionBuffer().

194  {
195  std::vector<int64_t> row_numbers(index_size);
196  for (size_t i = 0; i < index_size; ++i) {
197  row_numbers[index[i]] = i + 1;
198  }
199  return row_numbers;
200 }

+ Here is the caller graph for this function:

template<class T >
WindowFunctionContext::WindowComparatorResult anonymous_namespace{WindowContext.cpp}::integer_comparator ( const int8_t *  order_column_buffer,
const SQLTypeInfo ti,
const int32_t *  partition_indices,
const int64_t  lhs,
const int64_t  rhs,
const bool  nulls_first 
)

Definition at line 1017 of file WindowContext.cpp.

References WindowFunctionContext::EQ, WindowFunctionContext::GT, inline_fixed_encoding_null_val(), WindowFunctionContext::LT, and heavydb.dtypes::T.

1023  {
1024  const auto values = reinterpret_cast<const T*>(order_column_buffer);
1025  const auto lhs_val = values[partition_indices[lhs]];
1026  const auto rhs_val = values[partition_indices[rhs]];
1027  const auto null_val = inline_fixed_encoding_null_val(ti);
1028  if (lhs_val == null_val && rhs_val == null_val) {
1030  }
1031  if (lhs_val == null_val && rhs_val != null_val) {
1034  }
1035  if (rhs_val == null_val && lhs_val != null_val) {
1038  }
1039  if (lhs_val < rhs_val) {
1041  }
1042  if (lhs_val > rhs_val) {
1044  }
1046 }
int64_t inline_fixed_encoding_null_val(const SQL_TYPE_INFO &ti)

+ Here is the call graph for this function:

bool anonymous_namespace{WindowContext.cpp}::pos_is_set ( const int64_t  bitset,
const int64_t  pos 
)

Definition at line 420 of file WindowContext.cpp.

Referenced by apply_window_pending_outputs_double(), apply_window_pending_outputs_float(), apply_window_pending_outputs_float_columnar(), and apply_window_pending_outputs_int().

420  {
421  return (reinterpret_cast<const int8_t*>(bitset))[pos >> 3] & (1 << (pos & 7));
422 }

+ Here is the caller graph for this function:

size_t anonymous_namespace{WindowContext.cpp}::window_function_buffer_element_size ( const SqlWindowFunctionKind  )

Definition at line 301 of file WindowContext.cpp.

Referenced by WindowFunctionContext::compute().

301  {
302  return 8;
303 }

+ Here is the caller graph for this function: