OmniSciDB  085a039ca4
 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 129 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().

132  {
133  if (i == 0) {
134  return false;
135  }
136  return comparator(index[i - 1], index[i]);
137 }

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

References gpu_enabled::fill().

Referenced by WindowFunctionContext::computePartitionBuffer().

313  {
314  const auto first_value_idx = original_indices[output_for_partition_buff[0]];
315  std::fill(output_for_partition_buff,
316  output_for_partition_buff + partition_size,
317  first_value_idx);
318 }
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 288 of file WindowContext.cpp.

References gpu_enabled::copy().

Referenced by WindowFunctionContext::computePartitionBuffer().

291  {
292  std::vector<int64_t> lag_sorted_indices(partition_size, -1);
293  for (int64_t idx = 0; idx < static_cast<int64_t>(partition_size); ++idx) {
294  int64_t lag_idx = idx - lag;
295  if (lag_idx < 0 || lag_idx >= static_cast<int64_t>(partition_size)) {
296  continue;
297  }
298  lag_sorted_indices[idx] = sorted_indices[lag_idx];
299  }
300  std::vector<int64_t> lag_original_indices(partition_size);
301  for (size_t k = 0; k < partition_size; ++k) {
302  const auto lag_index = lag_sorted_indices[k];
303  lag_original_indices[sorted_indices[k]] =
304  lag_index != -1 ? original_indices[lag_index] : -1;
305  }
306  std::copy(lag_original_indices.begin(), lag_original_indices.end(), sorted_indices);
307 }
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 322 of file WindowContext.cpp.

References gpu_enabled::copy().

Referenced by WindowFunctionContext::computePartitionBuffer().

324  {
325  std::copy(
326  original_indices, original_indices + partition_size, output_for_partition_buff);
327 }
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 274 of file WindowContext.cpp.

References gpu_enabled::copy().

Referenced by WindowFunctionContext::computePartitionBuffer().

277  {
278  std::vector<int64_t> new_output_for_partition_buff(partition_size);
279  for (size_t i = 0; i < partition_size; ++i) {
280  new_output_for_partition_buff[i] = original_indices[output_for_partition_buff[i]];
281  }
282  std::copy(new_output_for_partition_buff.begin(),
283  new_output_for_partition_buff.end(),
284  output_for_partition_buff);
285 }
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 352 of file WindowContext.cpp.

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

355  {
356  if (!pos_is_set(bitset, pos)) {
357  return;
358  }
359  auto& pending_output_slots = *reinterpret_cast<std::vector<void*>*>(handle);
360  for (auto pending_output_slot : pending_output_slots) {
361  *reinterpret_cast<T*>(pending_output_slot) = value;
362  }
363  pending_output_slots.clear();
364 }
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 745 of file WindowContext.cpp.

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

751  {
752  const auto values = reinterpret_cast<const T*>(order_column_buffer);
753  const auto lhs_val = values[partition_indices[lhs]];
754  const auto rhs_val = values[partition_indices[rhs]];
755  const auto null_bit_pattern = null_val_bit_pattern(ti, ti.get_type() == kFLOAT);
756  const auto lhs_bit_pattern =
757  *reinterpret_cast<const NullPatternType*>(may_alias_ptr(&lhs_val));
758  const auto rhs_bit_pattern =
759  *reinterpret_cast<const NullPatternType*>(may_alias_ptr(&rhs_val));
760  if (lhs_bit_pattern == null_bit_pattern && rhs_bit_pattern == null_bit_pattern) {
762  }
763  if (lhs_bit_pattern == null_bit_pattern && rhs_bit_pattern != null_bit_pattern) {
766  }
767  if (rhs_bit_pattern == null_bit_pattern && lhs_bit_pattern != null_bit_pattern) {
770  }
771  if (lhs_val < rhs_val) {
773  }
774  if (lhs_val > rhs_val) {
776  }
778 }
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 231 of file WindowContext.cpp.

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

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

231  {
232  const auto lag_constant = dynamic_cast<const Analyzer::Constant*>(expr);
233  if (!lag_constant) {
234  throw std::runtime_error("LAG with non-constant lag argument not supported yet");
235  }
236  const auto& lag_ti = lag_constant->get_type_info();
237  switch (lag_ti.get_type()) {
238  case kSMALLINT: {
239  return lag_constant->get_constval().smallintval;
240  }
241  case kINT: {
242  return lag_constant->get_constval().intval;
243  }
244  case kBIGINT: {
245  return lag_constant->get_constval().bigintval;
246  }
247  default: {
248  LOG(FATAL) << "Invalid type for the lag argument";
249  }
250  }
251  return 0;
252 }
#define LOG(tag)
Definition: Logger.h:217
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 255 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().

255  {
256  CHECK(window_func->getKind() == SqlWindowFunctionKind::LAG ||
257  window_func->getKind() == SqlWindowFunctionKind::LEAD);
258  const auto& args = window_func->getArgs();
259  if (args.size() == 3) {
260  throw std::runtime_error("LAG with default not supported yet");
261  }
262  if (args.size() == 2) {
263  const int64_t lag_or_lead =
264  static_cast<int64_t>(get_int_constant_from_expr(args[1].get()));
265  return window_func->getKind() == SqlWindowFunctionKind::LAG ? lag_or_lead
266  : -lag_or_lead;
267  }
268  CHECK_EQ(args.size(), size_t(1));
269  return window_func->getKind() == SqlWindowFunctionKind::LAG ? 1 : -1;
270 }
#define CHECK_EQ(x, y)
Definition: Logger.h:231
SqlWindowFunctionKind getKind() const
Definition: Analyzer.h:2261
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs() const
Definition: Analyzer.h:2263
#define CHECK(condition)
Definition: Logger.h:223
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 189 of file WindowContext.cpp.

References advance_current_rank().

Referenced by WindowFunctionContext::computePartitionBuffer().

192  {
193  std::vector<double> cume_dist(index_size);
194  size_t start_peer_group = 0;
195  while (start_peer_group < index_size) {
196  size_t end_peer_group = start_peer_group + 1;
197  while (end_peer_group < index_size &&
198  !advance_current_rank(comparator, index, end_peer_group)) {
199  ++end_peer_group;
200  }
201  for (size_t i = start_peer_group; i < end_peer_group; ++i) {
202  cume_dist[index[i]] = static_cast<double>(end_peer_group) / index_size;
203  }
204  start_peer_group = end_peer_group;
205  }
206  return cume_dist;
207 }
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 156 of file WindowContext.cpp.

References advance_current_rank().

Referenced by WindowFunctionContext::computePartitionBuffer().

159  {
160  std::vector<int64_t> dense_rank(index_size);
161  size_t crt_rank = 1;
162  for (size_t i = 0; i < index_size; ++i) {
163  if (advance_current_rank(comparator, index, i)) {
164  ++crt_rank;
165  }
166  dense_rank[index[i]] = crt_rank;
167  }
168  return dense_rank;
169 }
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 210 of file WindowContext.cpp.

Referenced by WindowFunctionContext::computePartitionBuffer().

212  {
213  std::vector<int64_t> row_numbers(index_size);
214  if (!n) {
215  throw std::runtime_error("NTILE argument cannot be zero");
216  }
217  const size_t tile_size = (index_size + n - 1) / n;
218  for (size_t i = 0; i < index_size; ++i) {
219  row_numbers[index[i]] = i / tile_size + 1;
220  }
221  return row_numbers;
222 }
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 329 of file WindowContext.cpp.

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

Referenced by WindowFunctionContext::computePartitionBuffer().

334  {
335  int64_t partition_end_handle = reinterpret_cast<int64_t>(partition_end);
336  for (size_t i = 0; i < index_size; ++i) {
337  if (advance_current_rank(comparator, index, i)) {
338  agg_count_distinct_bitmap(&partition_end_handle, off + i - 1, 0);
339  }
340  }
341  CHECK(index_size);
342  agg_count_distinct_bitmap(&partition_end_handle, off + index_size - 1, 0);
343 }
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:223

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

References advance_current_rank().

Referenced by WindowFunctionContext::computePartitionBuffer().

175  {
176  std::vector<double> percent_rank(index_size);
177  size_t crt_rank = 1;
178  for (size_t i = 0; i < index_size; ++i) {
179  if (advance_current_rank(comparator, index, i)) {
180  crt_rank = i + 1;
181  }
182  percent_rank[index[i]] =
183  index_size == 1 ? 0 : static_cast<double>(crt_rank - 1) / (index_size - 1);
184  }
185  return percent_rank;
186 }
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 140 of file WindowContext.cpp.

References advance_current_rank().

Referenced by WindowFunctionContext::computePartitionBuffer().

143  {
144  std::vector<int64_t> rank(index_size);
145  size_t crt_rank = 1;
146  for (size_t i = 0; i < index_size; ++i) {
147  if (advance_current_rank(comparator, index, i)) {
148  crt_rank = i + 1;
149  }
150  rank[index[i]] = crt_rank;
151  }
152  return rank;
153 }
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 119 of file WindowContext.cpp.

Referenced by WindowFunctionContext::computePartitionBuffer().

119  {
120  std::vector<int64_t> row_numbers(index_size);
121  for (size_t i = 0; i < index_size; ++i) {
122  row_numbers[index[i]] = i + 1;
123  }
124  return row_numbers;
125 }

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

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

719  {
720  const auto values = reinterpret_cast<const T*>(order_column_buffer);
721  const auto lhs_val = values[partition_indices[lhs]];
722  const auto rhs_val = values[partition_indices[rhs]];
723  const auto null_val = inline_fixed_encoding_null_val(ti);
724  if (lhs_val == null_val && rhs_val == null_val) {
726  }
727  if (lhs_val == null_val && rhs_val != null_val) {
730  }
731  if (rhs_val == null_val && lhs_val != null_val) {
734  }
735  if (lhs_val < rhs_val) {
737  }
738  if (lhs_val > rhs_val) {
740  }
742 }
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 345 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().

345  {
346  return (reinterpret_cast<const int8_t*>(bitset))[pos >> 3] & (1 << (pos & 7));
347 }

+ Here is the caller graph for this function:

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

Definition at line 226 of file WindowContext.cpp.

Referenced by WindowFunctionContext::compute().

226  {
227  return 8;
228 }

+ Here is the caller graph for this function: