OmniSciDB  6686921089
 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 >
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

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

108  {
109  if (i == 0) {
110  return false;
111  }
112  return comparator(index[i - 1], index[i]);
113 }

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

References gpu_enabled::fill().

Referenced by WindowFunctionContext::computePartition().

289  {
290  const auto first_value_idx = original_indices[output_for_partition_buff[0]];
291  std::fill(output_for_partition_buff,
292  output_for_partition_buff + partition_size,
293  first_value_idx);
294 }
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 264 of file WindowContext.cpp.

References gpu_enabled::copy().

Referenced by WindowFunctionContext::computePartition().

267  {
268  std::vector<int64_t> lag_sorted_indices(partition_size, -1);
269  for (int64_t idx = 0; idx < static_cast<int64_t>(partition_size); ++idx) {
270  int64_t lag_idx = idx - lag;
271  if (lag_idx < 0 || lag_idx >= static_cast<int64_t>(partition_size)) {
272  continue;
273  }
274  lag_sorted_indices[idx] = sorted_indices[lag_idx];
275  }
276  std::vector<int64_t> lag_original_indices(partition_size);
277  for (size_t k = 0; k < partition_size; ++k) {
278  const auto lag_index = lag_sorted_indices[k];
279  lag_original_indices[sorted_indices[k]] =
280  lag_index != -1 ? original_indices[lag_index] : -1;
281  }
282  std::copy(lag_original_indices.begin(), lag_original_indices.end(), sorted_indices);
283 }
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 298 of file WindowContext.cpp.

References gpu_enabled::copy().

Referenced by WindowFunctionContext::computePartition().

300  {
301  std::copy(
302  original_indices, original_indices + partition_size, output_for_partition_buff);
303 }
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 250 of file WindowContext.cpp.

References gpu_enabled::copy(), and i.

Referenced by WindowFunctionContext::computePartition().

253  {
254  std::vector<int64_t> new_output_for_partition_buff(partition_size);
255  for (size_t i = 0; i < partition_size; ++i) {
256  new_output_for_partition_buff[i] = original_indices[output_for_partition_buff[i]];
257  }
258  std::copy(new_output_for_partition_buff.begin(),
259  new_output_for_partition_buff.end(),
260  output_for_partition_buff);
261 }
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 328 of file WindowContext.cpp.

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

331  {
332  if (!pos_is_set(bitset, pos)) {
333  return;
334  }
335  auto& pending_output_slots = *reinterpret_cast<std::vector<void*>*>(handle);
336  for (auto pending_output_slot : pending_output_slots) {
337  *reinterpret_cast<T*>(pending_output_slot) = value;
338  }
339  pending_output_slots.clear();
340 }
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 >
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 584 of file WindowContext.cpp.

References SQLTypeInfo::get_type(), kFLOAT, null_val_bit_pattern(), and omnisci.dtypes::T.

589  {
590  const auto values = reinterpret_cast<const T*>(order_column_buffer);
591  const auto lhs_val = values[partition_indices[lhs]];
592  const auto rhs_val = values[partition_indices[rhs]];
593  const auto null_bit_pattern = null_val_bit_pattern(ti, ti.get_type() == kFLOAT);
594  const auto lhs_bit_pattern =
595  *reinterpret_cast<const NullPatternType*>(may_alias_ptr(&lhs_val));
596  const auto rhs_bit_pattern =
597  *reinterpret_cast<const NullPatternType*>(may_alias_ptr(&rhs_val));
598  if (lhs_bit_pattern == null_bit_pattern && rhs_bit_pattern == null_bit_pattern) {
599  return false;
600  }
601  if (lhs_bit_pattern == null_bit_pattern && rhs_bit_pattern != null_bit_pattern) {
602  return nulls_first;
603  }
604  if (rhs_bit_pattern == null_bit_pattern && lhs_bit_pattern != null_bit_pattern) {
605  return !nulls_first;
606  }
607  return lhs_val < rhs_val;
608 }
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:329
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 207 of file WindowContext.cpp.

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

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

207  {
208  const auto lag_constant = dynamic_cast<const Analyzer::Constant*>(expr);
209  if (!lag_constant) {
210  throw std::runtime_error("LAG with non-constant lag argument not supported yet");
211  }
212  const auto& lag_ti = lag_constant->get_type_info();
213  switch (lag_ti.get_type()) {
214  case kSMALLINT: {
215  return lag_constant->get_constval().smallintval;
216  }
217  case kINT: {
218  return lag_constant->get_constval().intval;
219  }
220  case kBIGINT: {
221  return lag_constant->get_constval().bigintval;
222  }
223  default: {
224  LOG(FATAL) << "Invalid type for the lag argument";
225  }
226  }
227  return 0;
228 }
#define LOG(tag)
Definition: Logger.h:203
Definition: sqltypes.h:45

+ 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 231 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().

231  {
232  CHECK(window_func->getKind() == SqlWindowFunctionKind::LAG ||
233  window_func->getKind() == SqlWindowFunctionKind::LEAD);
234  const auto& args = window_func->getArgs();
235  if (args.size() == 3) {
236  throw std::runtime_error("LAG with default not supported yet");
237  }
238  if (args.size() == 2) {
239  const int64_t lag_or_lead =
240  static_cast<int64_t>(get_int_constant_from_expr(args[1].get()));
241  return window_func->getKind() == SqlWindowFunctionKind::LAG ? lag_or_lead
242  : -lag_or_lead;
243  }
244  CHECK_EQ(args.size(), size_t(1));
245  return window_func->getKind() == SqlWindowFunctionKind::LAG ? 1 : -1;
246 }
#define CHECK_EQ(x, y)
Definition: Logger.h:217
SqlWindowFunctionKind getKind() const
Definition: Analyzer.h:1607
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs() const
Definition: Analyzer.h:1609
#define CHECK(condition)
Definition: Logger.h:209
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 165 of file WindowContext.cpp.

References advance_current_rank(), and i.

Referenced by WindowFunctionContext::computePartition().

168  {
169  std::vector<double> cume_dist(index_size);
170  size_t start_peer_group = 0;
171  while (start_peer_group < index_size) {
172  size_t end_peer_group = start_peer_group + 1;
173  while (end_peer_group < index_size &&
174  !advance_current_rank(comparator, index, end_peer_group)) {
175  ++end_peer_group;
176  }
177  for (size_t i = start_peer_group; i < end_peer_group; ++i) {
178  cume_dist[index[i]] = static_cast<double>(end_peer_group) / index_size;
179  }
180  start_peer_group = end_peer_group;
181  }
182  return cume_dist;
183 }
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 132 of file WindowContext.cpp.

References advance_current_rank(), and i.

Referenced by WindowFunctionContext::computePartition().

135  {
136  std::vector<int64_t> dense_rank(index_size);
137  size_t crt_rank = 1;
138  for (size_t i = 0; i < index_size; ++i) {
139  if (advance_current_rank(comparator, index, i)) {
140  ++crt_rank;
141  }
142  dense_rank[index[i]] = crt_rank;
143  }
144  return dense_rank;
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:

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

References i.

Referenced by WindowFunctionContext::computePartition().

188  {
189  std::vector<int64_t> row_numbers(index_size);
190  if (!n) {
191  throw std::runtime_error("NTILE argument cannot be zero");
192  }
193  const size_t tile_size = (index_size + n - 1) / n;
194  for (size_t i = 0; i < index_size; ++i) {
195  row_numbers[index[i]] = i / tile_size + 1;
196  }
197  return row_numbers;
198 }
constexpr double n
Definition: Utm.h:46

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

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

Referenced by WindowFunctionContext::computePartition().

310  {
311  int64_t partition_end_handle = reinterpret_cast<int64_t>(partition_end);
312  for (size_t i = 0; i < index_size; ++i) {
313  if (advance_current_rank(comparator, index, i)) {
314  agg_count_distinct_bitmap(&partition_end_handle, off + i - 1, 0);
315  }
316  }
317  CHECK(index_size);
318  agg_count_distinct_bitmap(&partition_end_handle, off + index_size - 1, 0);
319 }
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:209
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:

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

References advance_current_rank(), and i.

Referenced by WindowFunctionContext::computePartition().

151  {
152  std::vector<double> percent_rank(index_size);
153  size_t crt_rank = 1;
154  for (size_t i = 0; i < index_size; ++i) {
155  if (advance_current_rank(comparator, index, i)) {
156  crt_rank = i + 1;
157  }
158  percent_rank[index[i]] =
159  index_size == 1 ? 0 : static_cast<double>(crt_rank - 1) / (index_size - 1);
160  }
161  return percent_rank;
162 }
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 116 of file WindowContext.cpp.

References advance_current_rank(), and i.

Referenced by WindowFunctionContext::computePartition().

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

References i.

Referenced by WindowFunctionContext::computePartition().

95  {
96  std::vector<int64_t> row_numbers(index_size);
97  for (size_t i = 0; i < index_size; ++i) {
98  row_numbers[index[i]] = i + 1;
99  }
100  return row_numbers;
101 }

+ Here is the caller graph for this function:

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

References inline_fixed_encoding_null_val(), and omnisci.dtypes::T.

566  {
567  const auto values = reinterpret_cast<const T*>(order_column_buffer);
568  const auto lhs_val = values[partition_indices[lhs]];
569  const auto rhs_val = values[partition_indices[rhs]];
570  const auto null_val = inline_fixed_encoding_null_val(ti);
571  if (lhs_val == null_val && rhs_val == null_val) {
572  return false;
573  }
574  if (lhs_val == null_val && rhs_val != null_val) {
575  return nulls_first;
576  }
577  if (rhs_val == null_val && lhs_val != null_val) {
578  return !nulls_first;
579  }
580  return lhs_val < rhs_val;
581 }
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 321 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().

321  {
322  return (reinterpret_cast<const int8_t*>(bitset))[pos >> 3] & (1 << (pos & 7));
323 }

+ Here is the caller graph for this function:

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

Definition at line 202 of file WindowContext.cpp.

Referenced by WindowFunctionContext::compute().

202  {
203  return 8;
204 }

+ Here is the caller graph for this function: