26 const std::vector<Analyzer::Expr*>& target_exprs,
27 const size_t truncate_n)
29 CHECK_EQ(rows.colCount(), target_exprs.size());
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()) {
38 CHECK_EQ(rows.colCount(), crt_row.size());
40 auto scalar_r = boost::get<ScalarTargetValue>(&crt_row[0]);
42 auto p = boost::get<int64_t>(scalar_r);
51 auto scalar_r = boost::get<ScalarTargetValue>(&crt_row[1]);
53 auto p = boost::get<int64_t>(scalar_r);
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);
84 for (
const auto& kv : that.
map_) {
85 const auto it_ok = map_.emplace(
89 unknown_ += that.unknown_;
94 std::shared_ptr<RowSetMemoryOwner> row_set_mem_owner,
96 const Executor* executor,
98 const bool desc)
const {
99 std::vector<SpeculativeTopNEntry> vec;
100 for (
const auto& kv :
map_) {
104 std::sort(vec.begin(), vec.end(), std::greater<SpeculativeTopNEntry>());
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) {
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});
129 auto rs = std::make_shared<ResultSet>(
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 =
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;
160 std::lock_guard<std::mutex> lock(
mutex_);
162 CHECK(!(*e.first == *expr) || e.second != desc);
164 blacklist_.emplace_back(expr, desc);
168 const bool desc)
const {
169 std::lock_guard<std::mutex> lock(
mutex_);
171 if (*e.first == *expr && e.second == desc) {
196 for (
const auto target_expr : ra_exe_unit.
target_exprs) {
198 if (agg_expr && !shared::is_any<kCOUNT, kCOUNT_IF>(agg_expr->get_aggtype())) {
std::vector< Analyzer::Expr * > target_exprs
void reduce(SpeculativeTopNMap &that)
bool contains(const std::shared_ptr< Analyzer::Expr > expr, const bool desc) const
bool use_speculative_top_n(const RelAlgExecutionUnit &ra_exe_unit, const QueryMemoryDescriptor &query_mem_desc)
DEVICE void sort(ARGS &&...args)
std::optional< size_t > limit
Speculative top N algorithm.
std::shared_ptr< ResultSet > 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
std::vector< std::pair< std::shared_ptr< Analyzer::Expr >, bool > > blacklist_
void add(const std::shared_ptr< Analyzer::Expr > expr, const bool desc)
std::vector< TargetInfo > target_exprs_to_infos(const std::vector< Analyzer::Expr * > &targets, const QueryMemoryDescriptor &query_mem_desc)
Basic constructors and methods of the row set interface.
std::unordered_map< int64_t, SpeculativeTopNVal > map_
DEVICE void swap(ARGS &&...args)