OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SpeculativeTopNMap Class Reference

#include <SpeculativeTopN.h>

Public Member Functions

 SpeculativeTopNMap ()
 
 SpeculativeTopNMap (const ResultSet &rows, const std::vector< Analyzer::Expr * > &target_exprs, const size_t truncate_n)
 
void reduce (SpeculativeTopNMap &that)
 
std::shared_ptr< ResultSetasRows (const RelAlgExecutionUnit &ra_exe_unit, std::shared_ptr< RowSetMemoryOwner > row_set_mem_owner, const QueryMemoryDescriptor &query_mem_desc, const Executor *executor, const size_t top_n, const bool desc) const
 

Private Attributes

std::unordered_map< int64_t,
SpeculativeTopNVal
map_
 
size_t unknown_
 

Detailed Description

Definition at line 57 of file SpeculativeTopN.h.

Constructor & Destructor Documentation

SpeculativeTopNMap::SpeculativeTopNMap ( )

Definition at line 23 of file SpeculativeTopN.cpp.

23 : unknown_(0) {}
SpeculativeTopNMap::SpeculativeTopNMap ( const ResultSet rows,
const std::vector< Analyzer::Expr * > &  target_exprs,
const size_t  truncate_n 
)

Definition at line 25 of file SpeculativeTopN.cpp.

References CHECK, CHECK_EQ, map_, and unknown_.

28  : unknown_(0) {
29  CHECK_EQ(rows.colCount(), target_exprs.size());
30  const bool count_first = dynamic_cast<const Analyzer::AggExpr*>(target_exprs[0]);
31  for (size_t i = 0; i < truncate_n + 1; ++i) {
32  const auto crt_row = rows.getNextRow(false, false);
33  if (crt_row.empty()) {
34  break;
35  }
36  int64_t key{0};
37  size_t val{0};
38  CHECK_EQ(rows.colCount(), crt_row.size());
39  {
40  auto scalar_r = boost::get<ScalarTargetValue>(&crt_row[0]);
41  CHECK(scalar_r);
42  auto p = boost::get<int64_t>(scalar_r);
43  CHECK(p);
44  if (count_first) {
45  val = *p;
46  } else {
47  key = *p;
48  }
49  }
50  {
51  auto scalar_r = boost::get<ScalarTargetValue>(&crt_row[1]);
52  CHECK(scalar_r);
53  auto p = boost::get<int64_t>(scalar_r);
54  CHECK(p);
55  if (count_first) {
56  key = *p;
57  } else {
58  val = *p;
59  }
60  }
61  if (i < truncate_n) {
62  const auto it_ok = map_.emplace(key, SpeculativeTopNVal{val, false});
63  CHECK(it_ok.second);
64  } else {
65  unknown_ = val;
66  }
67  }
68 }
#define CHECK_EQ(x, y)
Definition: Logger.h:301
tuple rows
Definition: report.py:114
#define CHECK(condition)
Definition: Logger.h:291
std::unordered_map< int64_t, SpeculativeTopNVal > map_

Member Function Documentation

std::shared_ptr< ResultSet > SpeculativeTopNMap::asRows ( const RelAlgExecutionUnit ra_exe_unit,
std::shared_ptr< RowSetMemoryOwner row_set_mem_owner,
const QueryMemoryDescriptor query_mem_desc,
const Executor executor,
const size_t  top_n,
const bool  desc 
) const

Definition at line 92 of file SpeculativeTopN.cpp.

References CHECK_EQ, CPU, heavyai::GroupByBaselineHash, map_, query_mem_desc, gpu_enabled::sort(), gpu_enabled::swap(), RelAlgExecutionUnit::target_exprs, target_exprs_to_infos(), and SpeculativeTopNEntry::val.

Referenced by Executor::reduceSpeculativeTopN().

98  {
99  std::vector<SpeculativeTopNEntry> vec;
100  for (const auto& kv : map_) {
101  vec.emplace_back(SpeculativeTopNEntry{kv.first, kv.second.val, kv.second.unknown});
102  }
103  if (desc) {
104  std::sort(vec.begin(), vec.end(), std::greater<SpeculativeTopNEntry>());
105  } else {
106  std::sort(vec.begin(), vec.end());
107  }
108  const auto num_rows = std::min(top_n, vec.size());
109  for (size_t i = 0; i < num_rows; ++i) {
110  if (vec[i].unknown) {
111  throw SpeculativeTopNFailed();
112  }
113  }
114  CHECK_EQ(size_t(2), ra_exe_unit.target_exprs.size());
115 
116  // Top N key-value pairs are stored into a new ResultSet with a new
117  // QueryMemoryDescriptor to be passed over. We use row-wise GroupByBaselineHash, as it
118  // is the most flexible layout for dealing with key-value pairs in the storage (for
119  // iterations and reduction).
120  auto query_mem_desc_rs = query_mem_desc;
121  query_mem_desc_rs.setQueryDescriptionType(QueryDescriptionType::GroupByBaselineHash);
122  query_mem_desc_rs.setOutputColumnar(false);
123  query_mem_desc_rs.setEntryCount(num_rows);
124  query_mem_desc_rs.clearSlotInfo();
125  query_mem_desc_rs.addColSlotInfo({std::make_tuple(8, 8)});
126  query_mem_desc_rs.addColSlotInfo({std::make_tuple(8, 8)});
127  query_mem_desc_rs.setAllTargetGroupbyIndices({-1, -1});
128 
129  auto rs = std::make_shared<ResultSet>(
130  target_exprs_to_infos(ra_exe_unit.target_exprs, query_mem_desc_rs),
132  query_mem_desc_rs,
133  row_set_mem_owner,
134  executor->blockSize(),
135  executor->gridSize());
136  auto rs_storage = rs->allocateStorage();
137  auto rs_buff = reinterpret_cast<int64_t*>(rs_storage->getUnderlyingBuffer());
138  const bool count_first =
139  dynamic_cast<const Analyzer::AggExpr*>(ra_exe_unit.target_exprs[0]);
140 
141  // going throug the TopN results, and properly storing them into the GroupByBaselineHash
142  // layout (including the group column (key) and two agg columns (key and value)) to
143  // imitate the regular Group By query's result.
144  for (size_t i = 0; i < num_rows; ++i) {
145  rs_buff[0] = vec[i].key;
146  int64_t col0 = vec[i].key;
147  int64_t col1 = vec[i].val;
148  if (count_first) {
149  std::swap(col0, col1);
150  }
151  rs_buff[1] = col0;
152  rs_buff[2] = col1;
153  rs_buff += 3;
154  }
155  return rs;
156 }
std::vector< Analyzer::Expr * > target_exprs
#define CHECK_EQ(x, y)
Definition: Logger.h:301
DEVICE void sort(ARGS &&...args)
Definition: gpu_enabled.h:105
size_t val
GroupByBaselineHash
Definition: enums.h:58
std::vector< TargetInfo > target_exprs_to_infos(const std::vector< Analyzer::Expr * > &targets, const QueryMemoryDescriptor &query_mem_desc)
std::unordered_map< int64_t, SpeculativeTopNVal > map_
DEVICE void swap(ARGS &&...args)
Definition: gpu_enabled.h:114

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void SpeculativeTopNMap::reduce ( SpeculativeTopNMap that)

Definition at line 70 of file SpeculativeTopN.cpp.

References CHECK, map_, unknown_, and SpeculativeTopNVal::val.

Referenced by Executor::reduceSpeculativeTopN().

70  {
71  for (auto& kv : map_) {
72  auto& this_entry = kv.second;
73  const auto that_it = that.map_.find(kv.first);
74  if (that_it != that.map_.end()) {
75  const auto& that_entry = that_it->second;
76  CHECK(!that_entry.unknown);
77  this_entry.val += that_entry.val;
78  that.map_.erase(that_it);
79  } else {
80  this_entry.val += that.unknown_;
81  this_entry.unknown = that.unknown_;
82  }
83  }
84  for (const auto& kv : that.map_) {
85  const auto it_ok = map_.emplace(
86  kv.first, SpeculativeTopNVal{kv.second.val + unknown_, unknown_ != 0});
87  CHECK(it_ok.second);
88  }
89  unknown_ += that.unknown_;
90 }
#define CHECK(condition)
Definition: Logger.h:291
std::unordered_map< int64_t, SpeculativeTopNVal > map_

+ Here is the caller graph for this function:

Member Data Documentation

std::unordered_map<int64_t, SpeculativeTopNVal> SpeculativeTopNMap::map_
private

Definition at line 75 of file SpeculativeTopN.h.

Referenced by asRows(), reduce(), and SpeculativeTopNMap().

size_t SpeculativeTopNMap::unknown_
private

Definition at line 76 of file SpeculativeTopN.h.

Referenced by reduce(), and SpeculativeTopNMap().


The documentation for this class was generated from the following files: