OmniSciDB  2e3a973ef4
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 >
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 71 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().

74  {
75  if (i == 0) {
76  return false;
77  }
78  return comparator(index[i - 1], index[i]);
79 }
+ 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 252 of file WindowContext.cpp.

Referenced by WindowFunctionContext::computePartition().

254  {
255  const auto first_value_idx = original_indices[output_for_partition_buff[0]];
256  std::fill(output_for_partition_buff,
257  output_for_partition_buff + partition_size,
258  first_value_idx);
259 }
+ Here is the caller graph for this function:

◆ apply_lag_to_partition()

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

Referenced by WindowFunctionContext::computePartition().

232  {
233  std::vector<int64_t> lag_sorted_indices(partition_size, -1);
234  for (int64_t idx = 0; idx < static_cast<int64_t>(partition_size); ++idx) {
235  int64_t lag_idx = idx - lag;
236  if (lag_idx < 0 || lag_idx >= static_cast<int64_t>(partition_size)) {
237  continue;
238  }
239  lag_sorted_indices[idx] = sorted_indices[lag_idx];
240  }
241  std::vector<int64_t> lag_original_indices(partition_size);
242  for (size_t k = 0; k < partition_size; ++k) {
243  const auto lag_index = lag_sorted_indices[k];
244  lag_original_indices[sorted_indices[k]] =
245  lag_index != -1 ? original_indices[lag_index] : -1;
246  }
247  std::copy(lag_original_indices.begin(), lag_original_indices.end(), sorted_indices);
248 }
+ 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 263 of file WindowContext.cpp.

Referenced by WindowFunctionContext::computePartition().

265  {
266  std::copy(
267  original_indices, original_indices + partition_size, output_for_partition_buff);
268 }
+ 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 216 of file WindowContext.cpp.

Referenced by WindowFunctionContext::computePartition().

218  {
219  std::vector<int64_t> new_output_for_partition_buff(partition_size);
220  for (size_t i = 0; i < partition_size; ++i) {
221  new_output_for_partition_buff[i] = original_indices[output_for_partition_buff[i]];
222  }
223  std::copy(new_output_for_partition_buff.begin(),
224  new_output_for_partition_buff.end(),
225  output_for_partition_buff);
226 }
+ 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 293 of file WindowContext.cpp.

References pos_is_set().

296  {
297  if (!pos_is_set(bitset, pos)) {
298  return;
299  }
300  auto& pending_output_slots = *reinterpret_cast<std::vector<void*>*>(handle);
301  for (auto pending_output_slot : pending_output_slots) {
302  *reinterpret_cast<T*>(pending_output_slot) = value;
303  }
304  pending_output_slots.clear();
305 }
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 553 of file WindowContext.cpp.

References SQLTypeInfo::get_type(), kFLOAT, and null_val_bit_pattern().

558  {
559  const auto values = reinterpret_cast<const T*>(order_column_buffer);
560  const auto lhs_val = values[partition_indices[lhs]];
561  const auto rhs_val = values[partition_indices[rhs]];
562  const auto null_bit_pattern = null_val_bit_pattern(ti, ti.get_type() == kFLOAT);
563  const auto lhs_bit_pattern =
564  *reinterpret_cast<const NullPatternType*>(may_alias_ptr(&lhs_val));
565  const auto rhs_bit_pattern =
566  *reinterpret_cast<const NullPatternType*>(may_alias_ptr(&rhs_val));
567  if (lhs_bit_pattern == null_bit_pattern && rhs_bit_pattern == null_bit_pattern) {
568  return false;
569  }
570  if (lhs_bit_pattern == null_bit_pattern && rhs_bit_pattern != null_bit_pattern) {
571  return nulls_first;
572  }
573  if (rhs_bit_pattern == null_bit_pattern && lhs_bit_pattern != null_bit_pattern) {
574  return !nulls_first;
575  }
576  return lhs_val < rhs_val;
577 }
int64_t null_val_bit_pattern(const SQLTypeInfo &ti, const bool float_argument_input)
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:259
+ 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 173 of file WindowContext.cpp.

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

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

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

◆ get_lag_or_lead_argument()

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

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

197  {
198  CHECK(window_func->getKind() == SqlWindowFunctionKind::LAG ||
199  window_func->getKind() == SqlWindowFunctionKind::LEAD);
200  const auto& args = window_func->getArgs();
201  if (args.size() == 3) {
202  throw std::runtime_error("LAG with default not supported yet");
203  }
204  if (args.size() == 2) {
205  const int64_t lag_or_lead =
206  static_cast<int64_t>(get_int_constant_from_expr(args[1].get()));
207  return window_func->getKind() == SqlWindowFunctionKind::LAG ? lag_or_lead
208  : -lag_or_lead;
209  }
210  CHECK_EQ(args.size(), size_t(1));
211  return window_func->getKind() == SqlWindowFunctionKind::LAG ? 1 : -1;
212 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs() const
Definition: Analyzer.h:1449
#define CHECK(condition)
Definition: Logger.h:197
size_t get_int_constant_from_expr(const Analyzer::Expr *expr)
SqlWindowFunctionKind getKind() const
Definition: Analyzer.h:1447
+ 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 131 of file WindowContext.cpp.

References advance_current_rank().

Referenced by WindowFunctionContext::computePartition().

134  {
135  std::vector<double> cume_dist(index_size);
136  size_t start_peer_group = 0;
137  while (start_peer_group < index_size) {
138  size_t end_peer_group = start_peer_group + 1;
139  while (end_peer_group < index_size &&
140  !advance_current_rank(comparator, index, end_peer_group)) {
141  ++end_peer_group;
142  }
143  for (size_t i = start_peer_group; i < end_peer_group; ++i) {
144  cume_dist[index[i]] = static_cast<double>(end_peer_group) / index_size;
145  }
146  start_peer_group = end_peer_group;
147  }
148  return cume_dist;
149 }
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 98 of file WindowContext.cpp.

References advance_current_rank().

Referenced by WindowFunctionContext::computePartition().

101  {
102  std::vector<int64_t> dense_rank(index_size);
103  size_t crt_rank = 1;
104  for (size_t i = 0; i < index_size; ++i) {
105  if (advance_current_rank(comparator, index, i)) {
106  ++crt_rank;
107  }
108  dense_rank[index[i]] = crt_rank;
109  }
110  return dense_rank;
111 }
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 152 of file WindowContext.cpp.

Referenced by WindowFunctionContext::computePartition().

154  {
155  std::vector<int64_t> row_numbers(index_size);
156  if (!n) {
157  throw std::runtime_error("NTILE argument cannot be zero");
158  }
159  const size_t tile_size = (index_size + n - 1) / n;
160  for (size_t i = 0; i < index_size; ++i) {
161  row_numbers[index[i]] = i / tile_size + 1;
162  }
163  return row_numbers;
164 }
+ 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 270 of file WindowContext.cpp.

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

Referenced by WindowFunctionContext::computePartition().

275  {
276  int64_t partition_end_handle = reinterpret_cast<int64_t>(partition_end);
277  for (size_t i = 0; i < index_size; ++i) {
278  if (advance_current_rank(comparator, index, i)) {
279  agg_count_distinct_bitmap(&partition_end_handle, off + i - 1, 0);
280  }
281  }
282  CHECK(index_size);
283  agg_count_distinct_bitmap(&partition_end_handle, off + index_size - 1, 0);
284 }
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:197
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 114 of file WindowContext.cpp.

References advance_current_rank().

Referenced by WindowFunctionContext::computePartition().

117  {
118  std::vector<double> percent_rank(index_size);
119  size_t crt_rank = 1;
120  for (size_t i = 0; i < index_size; ++i) {
121  if (advance_current_rank(comparator, index, i)) {
122  crt_rank = i + 1;
123  }
124  percent_rank[index[i]] =
125  index_size == 1 ? 0 : static_cast<double>(crt_rank - 1) / (index_size - 1);
126  }
127  return percent_rank;
128 }
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 82 of file WindowContext.cpp.

References advance_current_rank().

Referenced by WindowFunctionContext::computePartition().

85  {
86  std::vector<int64_t> rank(index_size);
87  size_t crt_rank = 1;
88  for (size_t i = 0; i < index_size; ++i) {
89  if (advance_current_rank(comparator, index, i)) {
90  crt_rank = i + 1;
91  }
92  rank[index[i]] = crt_rank;
93  }
94  return rank;
95 }
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 61 of file WindowContext.cpp.

Referenced by WindowFunctionContext::computePartition().

61  {
62  std::vector<int64_t> row_numbers(index_size);
63  for (size_t i = 0; i < index_size; ++i) {
64  row_numbers[index[i]] = i + 1;
65  }
66  return row_numbers;
67 }
+ 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 530 of file WindowContext.cpp.

References inline_fixed_encoding_null_val().

535  {
536  const auto values = reinterpret_cast<const T*>(order_column_buffer);
537  const auto lhs_val = values[partition_indices[lhs]];
538  const auto rhs_val = values[partition_indices[rhs]];
539  const auto null_val = inline_fixed_encoding_null_val(ti);
540  if (lhs_val == null_val && rhs_val == null_val) {
541  return false;
542  }
543  if (lhs_val == null_val && rhs_val != null_val) {
544  return nulls_first;
545  }
546  if (rhs_val == null_val && lhs_val != null_val) {
547  return !nulls_first;
548  }
549  return lhs_val < rhs_val;
550 }
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 286 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().

286  {
287  return (reinterpret_cast<const int8_t*>(bitset))[pos >> 3] & (1 << (pos & 7));
288 }
+ 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 168 of file WindowContext.cpp.

Referenced by WindowFunctionContext::compute().

168  {
169  return 8;
170 }
+ Here is the caller graph for this function: