OmniSciDB  bf83d84833
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
InputMetadata.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "InputMetadata.h"
18 #include "Execute.h"
19 
20 #include "../Fragmenter/Fragmenter.h"
21 
22 #include <future>
23 
24 InputTableInfoCache::InputTableInfoCache(Executor* executor) : executor_(executor) {}
25 
26 namespace {
27 
29  const Fragmenter_Namespace::TableInfo& table_info) {
30  Fragmenter_Namespace::TableInfo table_info_copy;
31  table_info_copy.chunkKeyPrefix = table_info.chunkKeyPrefix;
32  table_info_copy.fragments = table_info.fragments;
33  table_info_copy.setPhysicalNumTuples(table_info.getPhysicalNumTuples());
34  return table_info_copy;
35 }
36 
38  const std::vector<const TableDescriptor*>& shard_tables) {
39  size_t total_number_of_tuples{0};
40  Fragmenter_Namespace::TableInfo table_info_all_shards;
41  for (const TableDescriptor* shard_table : shard_tables) {
42  CHECK(shard_table->fragmenter);
43  const auto& shard_metainfo = shard_table->fragmenter->getFragmentsForQuery();
44  total_number_of_tuples += shard_metainfo.getPhysicalNumTuples();
45  table_info_all_shards.fragments.reserve(table_info_all_shards.fragments.size() +
46  shard_metainfo.fragments.size());
47  table_info_all_shards.fragments.insert(table_info_all_shards.fragments.end(),
48  shard_metainfo.fragments.begin(),
49  shard_metainfo.fragments.end());
50  }
51  table_info_all_shards.setPhysicalNumTuples(total_number_of_tuples);
52  return table_info_all_shards;
53 }
54 
55 } // namespace
56 
58  const auto it = cache_.find(table_id);
59  if (it != cache_.end()) {
60  const auto& table_info = it->second;
61  return copy_table_info(table_info);
62  }
63  const auto cat = executor_->getCatalog();
64  CHECK(cat);
65  const auto td = cat->getMetadataForTable(table_id);
66  CHECK(td);
67  const auto shard_tables = cat->getPhysicalTablesDescriptors(td);
68  auto table_info = build_table_info(shard_tables);
69  auto it_ok = cache_.emplace(table_id, copy_table_info(table_info));
70  CHECK(it_ok.second);
71  return copy_table_info(table_info);
72 }
73 
75  decltype(cache_)().swap(cache_);
76 }
77 
78 namespace {
79 
80 bool uses_int_meta(const SQLTypeInfo& col_ti) {
81  return col_ti.is_integer() || col_ti.is_decimal() || col_ti.is_time() ||
82  col_ti.is_boolean() ||
83  (col_ti.is_string() && col_ti.get_compression() == kENCODING_DICT);
84 }
85 
87  std::vector<Fragmenter_Namespace::FragmentInfo> result;
88  if (rows) {
89  result.resize(1);
90  auto& fragment = result.front();
91  fragment.fragmentId = 0;
92  fragment.deviceIds.resize(3);
93  fragment.resultSet = rows.get();
94  fragment.resultSetMutex.reset(new std::mutex());
95  }
97  table_info.fragments = result;
98  return table_info;
99 }
100 
101 void collect_table_infos(std::vector<InputTableInfo>& table_infos,
102  const std::vector<InputDescriptor>& input_descs,
103  Executor* executor) {
104  const auto temporary_tables = executor->getTemporaryTables();
105  const auto cat = executor->getCatalog();
106  CHECK(cat);
107  std::unordered_map<int, size_t> info_cache;
108  for (const auto& input_desc : input_descs) {
109  const auto table_id = input_desc.getTableId();
110  const auto cached_index_it = info_cache.find(table_id);
111  if (cached_index_it != info_cache.end()) {
112  CHECK_LT(cached_index_it->second, table_infos.size());
113  table_infos.push_back(
114  {table_id, copy_table_info(table_infos[cached_index_it->second].info)});
115  continue;
116  }
117  if (input_desc.getSourceType() == InputSourceType::RESULT) {
118  CHECK_LT(table_id, 0);
119  CHECK(temporary_tables);
120  const auto it = temporary_tables->find(table_id);
121  LOG_IF(FATAL, it == temporary_tables->end())
122  << "Failed to find previous query result for node " << -table_id;
123  table_infos.push_back({table_id, synthesize_table_info(it->second)});
124  } else {
125  CHECK(input_desc.getSourceType() == InputSourceType::TABLE);
126  table_infos.push_back({table_id, executor->getTableInfo(table_id)});
127  }
128  CHECK(!table_infos.empty());
129  info_cache.insert(std::make_pair(table_id, table_infos.size() - 1));
130  }
131 }
132 
133 } // namespace
134 
135 ChunkMetadataMap synthesize_metadata(const ResultSet* rows) {
136  rows->moveToBegin();
137  std::vector<std::vector<std::unique_ptr<Encoder>>> dummy_encoders;
138  const size_t worker_count =
140  for (size_t worker_idx = 0; worker_idx < worker_count; ++worker_idx) {
141  dummy_encoders.emplace_back();
142  for (size_t i = 0; i < rows->colCount(); ++i) {
143  const auto& col_ti = rows->getColType(i);
144  dummy_encoders.back().emplace_back(Encoder::Create(nullptr, col_ti));
145  }
146  }
147  const auto do_work = [rows](const std::vector<TargetValue>& crt_row,
148  std::vector<std::unique_ptr<Encoder>>& dummy_encoders) {
149  for (size_t i = 0; i < rows->colCount(); ++i) {
150  const auto& col_ti = rows->getColType(i);
151  const auto& col_val = crt_row[i];
152  const auto scalar_col_val = boost::get<ScalarTargetValue>(&col_val);
153  CHECK(scalar_col_val);
154  if (uses_int_meta(col_ti)) {
155  const auto i64_p = boost::get<int64_t>(scalar_col_val);
156  CHECK(i64_p);
157  dummy_encoders[i]->updateStats(*i64_p, *i64_p == inline_int_null_val(col_ti));
158  } else if (col_ti.is_fp()) {
159  switch (col_ti.get_type()) {
160  case kFLOAT: {
161  const auto float_p = boost::get<float>(scalar_col_val);
162  CHECK(float_p);
163  dummy_encoders[i]->updateStats(*float_p,
164  *float_p == inline_fp_null_val(col_ti));
165  break;
166  }
167  case kDOUBLE: {
168  const auto double_p = boost::get<double>(scalar_col_val);
169  CHECK(double_p);
170  dummy_encoders[i]->updateStats(*double_p,
171  *double_p == inline_fp_null_val(col_ti));
172  break;
173  }
174  default:
175  CHECK(false);
176  }
177  } else {
178  throw std::runtime_error(col_ti.get_type_name() +
179  " is not supported in temporary table.");
180  }
181  }
182  };
184  const size_t worker_count = cpu_threads();
185  std::vector<std::future<void>> compute_stats_threads;
186  const auto entry_count = rows->entryCount();
187  for (size_t i = 0,
188  start_entry = 0,
189  stride = (entry_count + worker_count - 1) / worker_count;
190  i < worker_count && start_entry < entry_count;
191  ++i, start_entry += stride) {
192  const auto end_entry = std::min(start_entry + stride, entry_count);
193  compute_stats_threads.push_back(std::async(
194  std::launch::async,
195  [rows, &do_work, &dummy_encoders](
196  const size_t start, const size_t end, const size_t worker_idx) {
197  for (size_t i = start; i < end; ++i) {
198  const auto crt_row = rows->getRowAtNoTranslations(i);
199  if (!crt_row.empty()) {
200  do_work(crt_row, dummy_encoders[worker_idx]);
201  }
202  }
203  },
204  start_entry,
205  end_entry,
206  i));
207  }
208  for (auto& child : compute_stats_threads) {
209  child.wait();
210  }
211  for (auto& child : compute_stats_threads) {
212  child.get();
213  }
214  } else {
215  while (true) {
216  auto crt_row = rows->getNextRow(false, false);
217  if (crt_row.empty()) {
218  break;
219  }
220  do_work(crt_row, dummy_encoders[0]);
221  }
222  rows->moveToBegin();
223  }
224  ChunkMetadataMap metadata_map;
225  for (size_t worker_idx = 1; worker_idx < worker_count; ++worker_idx) {
226  CHECK_LT(worker_idx, dummy_encoders.size());
227  const auto& worker_encoders = dummy_encoders[worker_idx];
228  for (size_t i = 0; i < rows->colCount(); ++i) {
229  dummy_encoders[0][i]->reduceStats(*worker_encoders[i]);
230  }
231  }
232  for (size_t i = 0; i < rows->colCount(); ++i) {
233  const auto it_ok =
234  metadata_map.emplace(i, dummy_encoders[0][i]->getMetadata(rows->getColType(i)));
235  CHECK(it_ok.second);
236  }
237  return metadata_map;
238 }
239 
240 size_t get_frag_count_of_table(const int table_id, Executor* executor) {
241  const auto temporary_tables = executor->getTemporaryTables();
242  CHECK(temporary_tables);
243  auto it = temporary_tables->find(table_id);
244  if (it != temporary_tables->end()) {
245  CHECK_GE(int(0), table_id);
246  return size_t(1);
247  } else {
248  const auto table_info = executor->getTableInfo(table_id);
249  return table_info.fragments.size();
250  }
251 }
252 
253 std::vector<InputTableInfo> get_table_infos(
254  const std::vector<InputDescriptor>& input_descs,
255  Executor* executor) {
256  std::vector<InputTableInfo> table_infos;
257  collect_table_infos(table_infos, input_descs, executor);
258  return table_infos;
259 }
260 
261 std::vector<InputTableInfo> get_table_infos(const RelAlgExecutionUnit& ra_exe_unit,
262  Executor* executor) {
264  std::vector<InputTableInfo> table_infos;
265  collect_table_infos(table_infos, ra_exe_unit.input_descs, executor);
266  return table_infos;
267 }
268 
273  }
274  return chunkMetadataMap;
275 }
276 
278  std::unique_ptr<std::lock_guard<std::mutex>> lock;
279  if (resultSetMutex) {
280  lock.reset(new std::lock_guard<std::mutex>(*resultSetMutex));
281  }
282  CHECK_EQ(!!resultSet, !!resultSetMutex);
283  if (resultSet && !synthesizedNumTuplesIsValid) {
284  numTuples = resultSet->rowCount();
285  synthesizedNumTuplesIsValid = true;
286  }
287  return numTuples;
288 }
289 
291  if (!fragments.empty() && fragments.front().resultSet) {
292  return fragments.front().getNumTuples();
293  }
294  return numTuples;
295 }
296 
298  if (!fragments.empty() && fragments.front().resultSet) {
299  return fragments.front().resultSet->entryCount();
300  }
301  return numTuples;
302 }
303 
305  if (!fragments.empty() && fragments.front().resultSet) {
306  return fragments.front().resultSet->entryCount();
307  }
308  size_t fragment_num_tupples_upper_bound = 0;
309  for (const auto& fragment : fragments) {
310  fragment_num_tupples_upper_bound =
311  std::max(fragment.getNumTuples(), fragment_num_tupples_upper_bound);
312  }
313  return fragment_num_tupples_upper_bound;
314 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
std::string cat(Ts &&...args)
Fragmenter_Namespace::TableInfo copy_table_info(const Fragmenter_Namespace::TableInfo &table_info)
ChunkMetadataMap synthesize_metadata(const ResultSet *rows)
static Encoder * Create(Data_Namespace::AbstractBuffer *buffer, const SQLTypeInfo sqlType)
Definition: Encoder.cpp:26
Executor * executor_
Definition: InputMetadata.h:48
std::vector< InputDescriptor > input_descs
#define CHECK_GE(x, y)
Definition: Logger.h:210
std::shared_ptr< ResultSet > ResultSetPtr
std::vector< FragmentInfo > fragments
Definition: Fragmenter.h:161
std::vector< int > chunkKeyPrefix
Definition: Fragmenter.h:160
double inline_fp_null_val(const SQL_TYPE_INFO &ti)
bool is_time() const
Definition: sqltypes.h:484
#define LOG_IF(severity, condition)
Definition: Logger.h:287
std::map< int, std::shared_ptr< ChunkMetadata >> ChunkMetadataMap
bool use_parallel_algorithms(const ResultSet &rows)
Definition: ResultSet.cpp:1063
size_t getPhysicalNumTuples() const
Definition: Fragmenter.h:154
Fragmenter_Namespace::TableInfo getTableInfo(const int table_id)
bool uses_int_meta(const SQLTypeInfo &col_ti)
bool is_integer() const
Definition: sqltypes.h:480
#define INJECT_TIMER(DESC)
Definition: measure.h:93
size_t getFragmentNumTuplesUpperBound() const
Fragmenter_Namespace::TableInfo synthesize_table_info(const ResultSetPtr &rows)
const ChunkMetadataMap & getChunkMetadataMap() const
bool is_boolean() const
Definition: sqltypes.h:485
#define CHECK_LT(x, y)
Definition: Logger.h:207
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:319
InputTableInfoCache(Executor *executor)
#define CHECK(condition)
Definition: Logger.h:197
std::vector< InputTableInfo > get_table_infos(const std::vector< InputDescriptor > &input_descs, Executor *executor)
void setPhysicalNumTuples(const size_t physNumTuples)
Definition: Fragmenter.h:156
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
void collect_table_infos(std::vector< InputTableInfo > &table_infos, const std::vector< InputDescriptor > &input_descs, Executor *executor)
bool is_string() const
Definition: sqltypes.h:478
std::unordered_map< int, Fragmenter_Namespace::TableInfo > cache_
Definition: InputMetadata.h:47
size_t get_frag_count_of_table(const int table_id, Executor *executor)
int cpu_threads()
Definition: thread_count.h:24
Fragmenter_Namespace::TableInfo build_table_info(const std::vector< const TableDescriptor * > &shard_tables)
bool is_decimal() const
Definition: sqltypes.h:481
DEVICE void swap(ARGS &&...args)
Definition: gpu_enabled.h:114