OmniSciDB  04ee39c94c
ChunkAccessorTable.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018 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 /*
18  * @file ChunkAccessorTable.cpp
19  * @author Simon Eves <simon.eves@mapd.com>
20  */
21 
22 #include "ChunkAccessorTable.h"
23 
25  const TableDescriptor* td,
26  const std::vector<std::string>& columnNames) {
27  ChunkAccessorTable table;
28 
29  // get fragments
30  const auto tableInfo = td->fragmenter->getFragmentsForQuery();
31  if (tableInfo.fragments.size() == 0) {
32  throw std::runtime_error("No fragments in table '" + td->tableName + "'");
33  }
34 
35  // for each fragment...
36  for (const auto& fragment : tableInfo.fragments) {
37  // add a table entry for it
38  table.emplace_back();
39  std::get<0>(table.back()) = 0;
40 
41  // for each column...
42  bool isFirstColumn = true;
43  for (const auto& columnName : columnNames) {
44  // get column descriptor
45  const auto cd = cat.getMetadataForColumn(td->tableId, columnName);
46  if (!cd) {
47  throw std::runtime_error("Failed to find physical column '" + columnName + "'");
48  }
49 
50  // find the chunk
51  ChunkKey chunkKey{
52  cat.getCurrentDB().dbId, td->tableId, cd->columnId, fragment.fragmentId};
53  auto chunkMetaIt = fragment.getChunkMetadataMap().find(cd->columnId);
54  if (chunkMetaIt == fragment.getChunkMetadataMap().end()) {
55  throw std::runtime_error("Failed to find the chunk for column: " +
56  cd->columnName + " in table: " + td->tableName +
57  ". The column was likely deleted via a table truncate.");
58  }
59 
60  // get the chunk
61  std::shared_ptr<Chunk_NS::Chunk> chunk =
63  &cat.getDataMgr(),
64  chunkKey,
66  0,
67  chunkMetaIt->second.numBytes,
68  chunkMetaIt->second.numElements);
69  CHECK(chunk);
70 
71  // the size
72  size_t chunkSize = chunkMetaIt->second.numElements;
73 
74  // and an iterator
75  ChunkIter chunkIt = chunk->begin_iterator(chunkMetaIt->second);
76 
77  // populate table entry
78  if (isFirstColumn) {
79  // store the size
80  std::get<0>(table.back()) = chunkSize;
81  isFirstColumn = false;
82  } else {
83  // all columns chunks must be the same size
84  CHECK(std::get<0>(table.back()) == chunkSize);
85  }
86  std::get<1>(table.back()).push_back(chunk);
87  std::get<2>(table.back()).push_back(chunkIt);
88  }
89  }
90 
91  // prefix-sum the per-fragment counts
92  // these are now "first row of next fragment"
93  size_t sum = 0;
94  for (auto& entry : table) {
95  sum += std::get<0>(entry);
96  std::get<0>(entry) = sum;
97  }
98 
99  // done
100  return table;
101 }
102 
104  size_t rowid,
105  size_t& rowOffset) {
106  rowOffset = 0;
107  for (auto& entry : table) {
108  if (rowid < std::get<0>(entry)) {
109  return std::get<2>(entry);
110  }
111  rowOffset = std::get<0>(entry);
112  }
113  CHECK(false);
114  static ChunkIterVector emptyChunkIterVector;
115  return emptyChunkIterVector;
116 }
ChunkAccessorTable getChunkAccessorTable(const Catalog_Namespace::Catalog &cat, const TableDescriptor *td, const std::vector< std::string > &columnNames)
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:81
std::string tableName
Data_Namespace::DataMgr & getDataMgr() const
Definition: Catalog.h:177
const ColumnDescriptor * getMetadataForColumn(int tableId, const std::string &colName) const
std::vector< std::tuple< size_t, std::vector< std::shared_ptr< Chunk_NS::Chunk > >, ChunkIterVector > > ChunkAccessorTable
ChunkIterVector & getChunkItersAndRowOffset(ChunkAccessorTable &table, size_t rowid, size_t &rowOffset)
virtual TableInfo getFragmentsForQuery()=0
Should get the partitions(fragments) where at least one tuple could satisfy the (optional) provided p...
static std::shared_ptr< Chunk > getChunk(const ColumnDescriptor *cd, DataMgr *data_mgr, const ChunkKey &key, const MemoryLevel mem_level, const int deviceId, const size_t num_bytes, const size_t num_elems)
Definition: Chunk.cpp:28
const DBMetadata & getCurrentDB() const
Definition: Catalog.h:176
std::vector< ChunkIter > ChunkIterVector
#define CHECK(condition)
Definition: Logger.h:187
std::vector< int > ChunkKey
Definition: types.h:35
specifies the content in-memory of a row in the table metadata table
Fragmenter_Namespace::AbstractFragmenter * fragmenter