OmniSciDB  04ee39c94c
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)
 
ssize_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 ssize_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 >
bool 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 >
bool 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

◆ advance_current_rank()

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 67 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().

70  {
71  if (i == 0) {
72  return false;
73  }
74  return comparator(index[i - 1], index[i]);
75 }
+ Here is the caller graph for this function:

◆ apply_first_value_to_partition()

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 247 of file WindowContext.cpp.

Referenced by WindowFunctionContext::computePartition().

249  {
250  const auto first_value_idx = original_indices[output_for_partition_buff[0]];
251  std::fill(output_for_partition_buff,
252  output_for_partition_buff + partition_size,
253  first_value_idx);
254 }
+ Here is the caller graph for this function:

◆ apply_lag_to_partition()

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

Definition at line 224 of file WindowContext.cpp.

Referenced by WindowFunctionContext::computePartition().

227  {
228  std::vector<int64_t> lag_sorted_indices(partition_size, -1);
229  for (ssize_t idx = 0; idx < static_cast<ssize_t>(partition_size); ++idx) {
230  ssize_t lag_idx = idx - lag;
231  if (lag_idx < 0 || lag_idx >= static_cast<ssize_t>(partition_size)) {
232  continue;
233  }
234  lag_sorted_indices[idx] = sorted_indices[lag_idx];
235  }
236  std::vector<int64_t> lag_original_indices(partition_size);
237  for (size_t k = 0; k < partition_size; ++k) {
238  const auto lag_index = lag_sorted_indices[k];
239  lag_original_indices[sorted_indices[k]] =
240  lag_index != -1 ? original_indices[lag_index] : -1;
241  }
242  std::copy(lag_original_indices.begin(), lag_original_indices.end(), sorted_indices);
243 }
+ Here is the caller graph for this function:

◆ apply_last_value_to_partition()

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 258 of file WindowContext.cpp.

Referenced by WindowFunctionContext::computePartition().

260  {
261  std::copy(
262  original_indices, original_indices + partition_size, output_for_partition_buff);
263 }
+ Here is the caller graph for this function:

◆ apply_permutation_to_partition()

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 211 of file WindowContext.cpp.

Referenced by WindowFunctionContext::computePartition().

213  {
214  std::vector<int64_t> new_output_for_partition_buff(partition_size);
215  for (size_t i = 0; i < partition_size; ++i) {
216  new_output_for_partition_buff[i] = original_indices[output_for_partition_buff[i]];
217  }
218  std::copy(new_output_for_partition_buff.begin(),
219  new_output_for_partition_buff.end(),
220  output_for_partition_buff);
221 }
+ Here is the caller graph for this function:

◆ apply_window_pending_outputs_int()

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 288 of file WindowContext.cpp.

References run-benchmark::handle, and pos_is_set().

291  {
292  if (!pos_is_set(bitset, pos)) {
293  return;
294  }
295  auto& pending_output_slots = *reinterpret_cast<std::vector<void*>*>(handle);
296  for (auto pending_output_slot : pending_output_slots) {
297  *reinterpret_cast<T*>(pending_output_slot) = value;
298  }
299  pending_output_slots.clear();
300 }
bool pos_is_set(const int64_t bitset, const int64_t pos)
+ Here is the call graph for this function:

◆ fp_comparator()

template<class T , class NullPatternType >
bool 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 548 of file WindowContext.cpp.

References SQLTypeInfoCore< TYPE_FACET_PACK >::get_type(), kFLOAT, and null_val_bit_pattern().

553  {
554  const auto values = reinterpret_cast<const T*>(order_column_buffer);
555  const auto lhs_val = values[partition_indices[lhs]];
556  const auto rhs_val = values[partition_indices[rhs]];
557  const auto null_bit_pattern = null_val_bit_pattern(ti, ti.get_type() == kFLOAT);
558  const auto lhs_bit_pattern =
559  *reinterpret_cast<const NullPatternType*>(may_alias_ptr(&lhs_val));
560  const auto rhs_bit_pattern =
561  *reinterpret_cast<const NullPatternType*>(may_alias_ptr(&rhs_val));
562  if (lhs_bit_pattern == null_bit_pattern && rhs_bit_pattern == null_bit_pattern) {
563  return false;
564  }
565  if (lhs_bit_pattern == null_bit_pattern && rhs_bit_pattern != null_bit_pattern) {
566  return nulls_first;
567  }
568  if (rhs_bit_pattern == null_bit_pattern && lhs_bit_pattern != null_bit_pattern) {
569  return !nulls_first;
570  }
571  return lhs_val < rhs_val;
572 }
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:323
int64_t null_val_bit_pattern(const SQLTypeInfo &ti, const bool float_argument_input)
+ Here is the call graph for this function:

◆ get_int_constant_from_expr()

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

Definition at line 169 of file WindowContext.cpp.

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

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

169  {
170  const auto lag_constant = dynamic_cast<const Analyzer::Constant*>(expr);
171  if (!lag_constant) {
172  throw std::runtime_error("LAG with non-constant lag argument not supported yet");
173  }
174  const auto& lag_ti = lag_constant->get_type_info();
175  switch (lag_ti.get_type()) {
176  case kSMALLINT: {
177  return lag_constant->get_constval().smallintval;
178  }
179  case kINT: {
180  return lag_constant->get_constval().intval;
181  }
182  case kBIGINT: {
183  return lag_constant->get_constval().bigintval;
184  }
185  default: {
186  LOG(FATAL) << "Invalid type for the lag argument";
187  }
188  }
189  return 0;
190 }
#define LOG(tag)
Definition: Logger.h:182
Definition: sqltypes.h:47
+ Here is the caller graph for this function:

◆ get_lag_or_lead_argument()

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

Definition at line 193 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::computePartition().

193  {
194  CHECK(window_func->getKind() == SqlWindowFunctionKind::LAG ||
195  window_func->getKind() == SqlWindowFunctionKind::LEAD);
196  const auto& args = window_func->getArgs();
197  if (args.size() == 3) {
198  throw std::runtime_error("LAG with default not supported yet");
199  }
200  if (args.size() == 2) {
201  const ssize_t lag_or_lead = get_int_constant_from_expr(args[1].get());
202  return window_func->getKind() == SqlWindowFunctionKind::LAG ? lag_or_lead
203  : -lag_or_lead;
204  }
205  CHECK_EQ(args.size(), size_t(1));
206  return 1;
207 }
#define CHECK_EQ(x, y)
Definition: Logger.h:195
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs() const
Definition: Analyzer.h:1341
#define CHECK(condition)
Definition: Logger.h:187
size_t get_int_constant_from_expr(const Analyzer::Expr *expr)
SqlWindowFunctionKind getKind() const
Definition: Analyzer.h:1339
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ index_to_cume_dist()

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 127 of file WindowContext.cpp.

References advance_current_rank().

Referenced by WindowFunctionContext::computePartition().

130  {
131  std::vector<double> cume_dist(index_size);
132  size_t start_peer_group = 0;
133  while (start_peer_group < index_size) {
134  size_t end_peer_group = start_peer_group + 1;
135  while (end_peer_group < index_size &&
136  !advance_current_rank(comparator, index, end_peer_group)) {
137  ++end_peer_group;
138  }
139  for (size_t i = start_peer_group; i < end_peer_group; ++i) {
140  cume_dist[index[i]] = static_cast<double>(end_peer_group) / index_size;
141  }
142  start_peer_group = end_peer_group;
143  }
144  return cume_dist;
145 }
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:

◆ index_to_dense_rank()

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 94 of file WindowContext.cpp.

References advance_current_rank().

Referenced by WindowFunctionContext::computePartition().

97  {
98  std::vector<int64_t> dense_rank(index_size);
99  size_t crt_rank = 1;
100  for (size_t i = 0; i < index_size; ++i) {
101  if (advance_current_rank(comparator, index, i)) {
102  ++crt_rank;
103  }
104  dense_rank[index[i]] = crt_rank;
105  }
106  return dense_rank;
107 }
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:

◆ index_to_ntile()

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 148 of file WindowContext.cpp.

Referenced by WindowFunctionContext::computePartition().

150  {
151  std::vector<int64_t> row_numbers(index_size);
152  if (!n) {
153  throw std::runtime_error("NTILE argument cannot be zero");
154  }
155  const size_t tile_size = (index_size + n - 1) / n;
156  for (size_t i = 0; i < index_size; ++i) {
157  row_numbers[index[i]] = i / tile_size + 1;
158  }
159  return row_numbers;
160 }
+ Here is the caller graph for this function:

◆ index_to_partition_end()

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 265 of file WindowContext.cpp.

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

Referenced by WindowFunctionContext::computePartition().

270  {
271  int64_t partition_end_handle = reinterpret_cast<int64_t>(partition_end);
272  for (size_t i = 0; i < index_size; ++i) {
273  if (advance_current_rank(comparator, index, i)) {
274  agg_count_distinct_bitmap(&partition_end_handle, off + i - 1, 0);
275  }
276  }
277  CHECK(index_size);
278  agg_count_distinct_bitmap(&partition_end_handle, off + index_size - 1, 0);
279 }
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)
#define CHECK(condition)
Definition: Logger.h:187
ALWAYS_INLINE void agg_count_distinct_bitmap(int64_t *agg, const int64_t val, const int64_t min_val)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ index_to_percent_rank()

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 110 of file WindowContext.cpp.

References advance_current_rank().

Referenced by WindowFunctionContext::computePartition().

113  {
114  std::vector<double> percent_rank(index_size);
115  size_t crt_rank = 1;
116  for (size_t i = 0; i < index_size; ++i) {
117  if (advance_current_rank(comparator, index, i)) {
118  crt_rank = i + 1;
119  }
120  percent_rank[index[i]] =
121  index_size == 1 ? 0 : static_cast<double>(crt_rank - 1) / (index_size - 1);
122  }
123  return percent_rank;
124 }
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:

◆ index_to_rank()

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 78 of file WindowContext.cpp.

References advance_current_rank().

Referenced by WindowFunctionContext::computePartition().

81  {
82  std::vector<int64_t> rank(index_size);
83  size_t crt_rank = 1;
84  for (size_t i = 0; i < index_size; ++i) {
85  if (advance_current_rank(comparator, index, i)) {
86  crt_rank = i + 1;
87  }
88  rank[index[i]] = crt_rank;
89  }
90  return rank;
91 }
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:

◆ index_to_row_number()

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

Definition at line 57 of file WindowContext.cpp.

Referenced by WindowFunctionContext::computePartition().

57  {
58  std::vector<int64_t> row_numbers(index_size);
59  for (size_t i = 0; i < index_size; ++i) {
60  row_numbers[index[i]] = i + 1;
61  }
62  return row_numbers;
63 }
+ Here is the caller graph for this function:

◆ integer_comparator()

template<class T >
bool 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 525 of file WindowContext.cpp.

References inline_fixed_encoding_null_val().

530  {
531  const auto values = reinterpret_cast<const T*>(order_column_buffer);
532  const auto lhs_val = values[partition_indices[lhs]];
533  const auto rhs_val = values[partition_indices[rhs]];
534  const auto null_val = inline_fixed_encoding_null_val(ti);
535  if (lhs_val == null_val && rhs_val == null_val) {
536  return false;
537  }
538  if (lhs_val == null_val && rhs_val != null_val) {
539  return nulls_first;
540  }
541  if (rhs_val == null_val && lhs_val != null_val) {
542  return !nulls_first;
543  }
544  return lhs_val < rhs_val;
545 }
int64_t inline_fixed_encoding_null_val(const SQL_TYPE_INFO &ti)
+ Here is the call graph for this function:

◆ pos_is_set()

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

Definition at line 281 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().

281  {
282  return (reinterpret_cast<const int8_t*>(bitset))[pos >> 3] & (1 << (pos & 7));
283 }
+ Here is the caller graph for this function:

◆ window_function_buffer_element_size()

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

Definition at line 164 of file WindowContext.cpp.

Referenced by WindowFunctionContext::compute().

164  {
165  return 8;
166 }
+ Here is the caller graph for this function: