OmniSciDB  06b3bd477c
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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)
 
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

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:

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

Referenced by WindowFunctionContext::computePartition().

253  {
254  const auto first_value_idx = original_indices[output_for_partition_buff[0]];
255  std::fill(output_for_partition_buff,
256  output_for_partition_buff + partition_size,
257  first_value_idx);
258 }

+ Here is the caller graph for this function:

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

Referenced by WindowFunctionContext::computePartition().

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

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

Referenced by WindowFunctionContext::computePartition().

264  {
265  std::copy(
266  original_indices, original_indices + partition_size, output_for_partition_buff);
267 }

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

Referenced by WindowFunctionContext::computePartition().

217  {
218  std::vector<int64_t> new_output_for_partition_buff(partition_size);
219  for (size_t i = 0; i < partition_size; ++i) {
220  new_output_for_partition_buff[i] = original_indices[output_for_partition_buff[i]];
221  }
222  std::copy(new_output_for_partition_buff.begin(),
223  new_output_for_partition_buff.end(),
224  output_for_partition_buff);
225 }

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

References pos_is_set().

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

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

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

+ Here is the caller graph for this function:

ssize_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 ssize_t lag_or_lead = get_int_constant_from_expr(args[1].get());
206  return window_func->getKind() == SqlWindowFunctionKind::LAG ? lag_or_lead
207  : -lag_or_lead;
208  }
209  CHECK_EQ(args.size(), size_t(1));
210  return window_func->getKind() == SqlWindowFunctionKind::LAG ? 1 : -1;
211 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
SqlWindowFunctionKind getKind() const
Definition: Analyzer.h:1448
CHECK(cgen_state)
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs() const
Definition: Analyzer.h:1450
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 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:

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:

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:

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

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

Referenced by WindowFunctionContext::computePartition().

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

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:

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:

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

References inline_fixed_encoding_null_val().

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

285  {
286  return (reinterpret_cast<const int8_t*>(bitset))[pos >> 3] & (1 << (pos & 7));
287 }

+ Here is the caller graph for this function:

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: