OmniSciDB  b24e664e58
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 58 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:198
CHECK(cgen_state)
std::unordered_map< int64_t, SpeculativeTopNVal > map_

+ Here is the call graph for this function:

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, GroupByBaselineHash, map_, num_rows, query_mem_desc, 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  auto query_mem_desc_rs = query_mem_desc;
116  query_mem_desc_rs.setQueryDescriptionType(QueryDescriptionType::GroupByBaselineHash);
117  query_mem_desc_rs.setOutputColumnar(false);
118  query_mem_desc_rs.setEntryCount(num_rows);
119  query_mem_desc_rs.clearSlotInfo();
120  query_mem_desc_rs.addColSlotInfo({std::make_tuple(8, 8)});
121  query_mem_desc_rs.addColSlotInfo({std::make_tuple(8, 8)});
122  auto rs = std::make_shared<ResultSet>(
123  target_exprs_to_infos(ra_exe_unit.target_exprs, query_mem_desc_rs),
125  query_mem_desc_rs,
126  row_set_mem_owner,
127  executor);
128  auto rs_storage = rs->allocateStorage();
129  auto rs_buff = reinterpret_cast<int64_t*>(rs_storage->getUnderlyingBuffer());
130  const bool count_first =
131  dynamic_cast<const Analyzer::AggExpr*>(ra_exe_unit.target_exprs[0]);
132  for (size_t i = 0; i < num_rows; ++i) {
133  rs_buff[0] = vec[i].key;
134  int64_t col0 = vec[i].key;
135  int64_t col1 = vec[i].val;
136  if (count_first) {
137  std::swap(col0, col1);
138  }
139  rs_buff[1] = col0;
140  rs_buff[2] = col1;
141  rs_buff += 3;
142  }
143  return rs;
144 }
std::vector< Analyzer::Expr * > target_exprs
#define CHECK_EQ(x, y)
Definition: Logger.h:198
const int8_t const int64_t * num_rows
size_t val
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_

+ 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 }
CHECK(cgen_state)
std::unordered_map< int64_t, SpeculativeTopNVal > map_

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

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

Definition at line 76 of file SpeculativeTopN.h.

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

size_t SpeculativeTopNMap::unknown_
private

Definition at line 77 of file SpeculativeTopN.h.

Referenced by reduce(), and SpeculativeTopNMap().


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