OmniSciDB  16c4e035a1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
InsertOrderFragmenter.h
Go to the documentation of this file.
1 /*
2  * Copyright 2020 OmniSci, 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 
22 #pragma once
23 
24 #include <map>
25 #include <mutex>
26 #include <unordered_map>
27 #include <unordered_set>
28 #include <vector>
29 
30 #include "DataMgr/Chunk/Chunk.h"
31 #include "DataMgr/MemoryLevel.h"
32 #include "FragmentDefaultValues.h"
36 #include "Shared/types.h"
37 
38 class Executor;
39 
40 namespace Data_Namespace {
41 class DataMgr;
42 }
43 
44 namespace Fragmenter_Namespace {
45 
54  public:
56 
58  const std::vector<int> chunkKeyPrefix,
59  std::vector<Chunk_NS::Chunk>& chunkVec,
60  Data_Namespace::DataMgr* dataMgr,
62  const int physicalTableId,
63  const int shard,
64  const size_t maxFragmentRows = DEFAULT_FRAGMENT_ROWS,
65  const size_t maxChunkSize = DEFAULT_MAX_CHUNK_SIZE,
66  const size_t pageSize = DEFAULT_PAGE_SIZE /*default 1MB*/,
67  const size_t maxRows = DEFAULT_MAX_ROWS,
69  const bool uses_foreign_storage = false);
70 
71  ~InsertOrderFragmenter() override;
72 
77  size_t getNumFragments() override;
78 
85  // virtual void getFragmentsForQuery(QueryInfo &queryInfo, const void *predicate = 0);
87 
95  void insertData(InsertData& insert_data_struct) override;
96 
97  void insertChunks(const InsertChunks& insert_chunk) override;
98 
99  void insertDataNoCheckpoint(InsertData& insert_data_struct) override;
100 
101  void insertChunksNoCheckpoint(const InsertChunks& insert_chunk) override;
102 
103  void dropFragmentsToSize(const size_t maxRows) override;
104 
106  const int fragment_id,
107  const std::shared_ptr<ChunkMetadata> metadata) override;
108 
109  void updateChunkStats(const ColumnDescriptor* cd,
110  std::unordered_map</*fragment_id*/ int, ChunkStats>& stats_map,
111  std::optional<Data_Namespace::MemoryLevel> memory_level) override;
112 
113  FragmentInfo* getFragmentInfo(const int fragment_id) const override;
114 
118  inline int getFragmenterId() override { return chunkKeyPrefix_.back(); }
119  inline std::vector<int> getChunkKeyPrefix() const { return chunkKeyPrefix_; }
123  inline std::string getFragmenterType() override { return fragmenterType_; }
124  size_t getNumRows() override { return numTuples_; }
125  void setNumRows(const size_t numTuples) override { numTuples_ = numTuples; }
126 
127  std::optional<ChunkUpdateStats> updateColumn(
128  const Catalog_Namespace::Catalog* catalog,
129  const TableDescriptor* td,
130  const ColumnDescriptor* cd,
131  const int fragment_id,
132  const std::vector<uint64_t>& frag_offsets,
133  const std::vector<ScalarTargetValue>& rhs_values,
134  const SQLTypeInfo& rhs_type,
135  const Data_Namespace::MemoryLevel memory_level,
136  UpdelRoll& updel_roll) override;
137 
138  void updateColumns(const Catalog_Namespace::Catalog* catalog,
139  const TableDescriptor* td,
140  const int fragmentId,
141  const std::vector<TargetMetaInfo> sourceMetaInfo,
142  const std::vector<const ColumnDescriptor*> columnDescriptors,
143  const RowDataProvider& sourceDataProvider,
144  const size_t indexOffFragmentOffsetColumn,
145  const Data_Namespace::MemoryLevel memoryLevel,
146  UpdelRoll& updelRoll,
147  Executor* executor) override;
148 
149  void updateColumn(const Catalog_Namespace::Catalog* catalog,
150  const TableDescriptor* td,
151  const ColumnDescriptor* cd,
152  const int fragment_id,
153  const std::vector<uint64_t>& frag_offsets,
154  const ScalarTargetValue& rhs_value,
155  const SQLTypeInfo& rhs_type,
156  const Data_Namespace::MemoryLevel memory_level,
157  UpdelRoll& updel_roll) override;
158 
159  void updateColumnMetadata(const ColumnDescriptor* cd,
160  FragmentInfo& fragment,
161  std::shared_ptr<Chunk_NS::Chunk> chunk,
162  const UpdateValuesStats& update_values_stats,
163  const SQLTypeInfo& rhs_type,
164  UpdelRoll& updel_roll) override;
165 
166  void updateMetadata(const Catalog_Namespace::Catalog* catalog,
167  const MetaDataKey& key,
168  UpdelRoll& updel_roll) override;
169 
170  void compactRows(const Catalog_Namespace::Catalog* catalog,
171  const TableDescriptor* td,
172  const int fragment_id,
173  const std::vector<uint64_t>& frag_offsets,
174  const Data_Namespace::MemoryLevel memory_level,
175  UpdelRoll& updel_roll) override;
176 
177  const std::vector<uint64_t> getVacuumOffsets(
178  const std::shared_ptr<Chunk_NS::Chunk>& chunk) override;
179 
181  const FragmentInfo& fragment,
182  const Data_Namespace::MemoryLevel memory_level);
183 
184  void dropColumns(const std::vector<int>& columnIds) override;
185 
186  bool hasDeletedRows(const int delete_column_id) override;
187 
188  void resetSizesFromFragments() override;
189 
190  protected:
191  std::vector<int> chunkKeyPrefix_;
192  std::map<int, Chunk_NS::Chunk>
194  std::deque<std::unique_ptr<FragmentInfo>>
196  // int currentInsertBufferFragmentId_;
199  const int physicalTableId_;
200  const int shard_;
202  size_t pageSize_; /* Page size in bytes of each page making up a given chunk - passed to
203  BufferMgr in createChunk() */
204  size_t numTuples_;
207  size_t maxRows_;
208  std::string fragmenterType_;
210  fragmentInfoMutex_; // to prevent read-write conflicts for fragmentInfoVec_
212  insertMutex_; // to prevent race conditions on insert - only one insert statement
213  // should be going to a table at a time
218  std::unordered_map<int, size_t> varLenColInfo_;
219  std::shared_ptr<std::mutex> mutex_access_inmem_states;
220 
231  void deleteFragments(const std::vector<int>& dropFragIds);
232 
234  void getChunkMetadata();
235 
236  void lockInsertCheckpointData(const InsertData& insertDataStruct);
237  void insertDataImpl(InsertData& insert_data);
238  void insertChunksImpl(const InsertChunks& insert_chunk);
239  void addColumns(const InsertData& insertDataStruct);
240 
243  // FIX-ME: Temporary lock; needs removing.
244  mutable std::mutex temp_mutex_;
245 
246  FragmentInfo& getFragmentInfoFromId(const int fragment_id);
247 
248  auto vacuum_fixlen_rows(const FragmentInfo& fragment,
249  const std::shared_ptr<Chunk_NS::Chunk>& chunk,
250  const std::vector<uint64_t>& frag_offsets);
251  auto vacuum_varlen_rows(const FragmentInfo& fragment,
252  const std::shared_ptr<Chunk_NS::Chunk>& chunk,
253  const std::vector<uint64_t>& frag_offsets);
254 
255  private:
256  bool isAddingNewColumns(const InsertData& insert_data) const;
257  void dropFragmentsToSizeNoInsertLock(const size_t max_rows);
259  void insertChunksIntoFragment(const InsertChunks& insert_chunks,
260  const std::optional<int> delete_column_id,
261  FragmentInfo* current_fragment,
262  const size_t num_rows_to_insert,
263  size_t& num_rows_inserted,
264  size_t& num_rows_left,
265  const size_t start_fragment);
266 };
267 
268 } // namespace Fragmenter_Namespace
void dropFragmentsToSizeNoInsertLock(const size_t max_rows)
void updateMetadata(const Catalog_Namespace::Catalog *catalog, const MetaDataKey &key, UpdelRoll &updel_roll) override
void insertChunksImpl(const InsertChunks &insert_chunk)
void dropColumns(const std::vector< int > &columnIds) override
FragmentInfo & getFragmentInfoFromId(const int fragment_id)
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:114
TableInfo getFragmentsForQuery() override
returns (inside QueryInfo) object all ids and row sizes of fragments
InsertOrderFragmenter & operator=(const InsertOrderFragmenter &)
void insertChunksIntoFragment(const InsertChunks &insert_chunks, const std::optional< int > delete_column_id, FragmentInfo *current_fragment, const size_t num_rows_to_insert, size_t &num_rows_inserted, size_t &num_rows_left, const size_t start_fragment)
std::pair< const TableDescriptor *, Fragmenter_Namespace::FragmentInfo * > MetaDataKey
Definition: UpdelRoll.h:40
std::deque< std::unique_ptr< FragmentInfo > > fragmentInfoVec_
void dropFragmentsToSize(const size_t maxRows) override
Will truncate table to less than maxRows by dropping fragments.
InsertOrderFragmenter(const std::vector< int > chunkKeyPrefix, std::vector< Chunk_NS::Chunk > &chunkVec, Data_Namespace::DataMgr *dataMgr, Catalog_Namespace::Catalog *catalog, const int physicalTableId, const int shard, const size_t maxFragmentRows=DEFAULT_FRAGMENT_ROWS, const size_t maxChunkSize=DEFAULT_MAX_CHUNK_SIZE, const size_t pageSize=DEFAULT_PAGE_SIZE, const size_t maxRows=DEFAULT_MAX_ROWS, const Data_Namespace::MemoryLevel defaultInsertLevel=Data_Namespace::DISK_LEVEL, const bool uses_foreign_storage=false)
std::shared_ptr< std::mutex > mutex_access_inmem_states
#define DEFAULT_MAX_CHUNK_SIZE
std::optional< ChunkUpdateStats > updateColumn(const Catalog_Namespace::Catalog *catalog, const TableDescriptor *td, const ColumnDescriptor *cd, const int fragment_id, const std::vector< uint64_t > &frag_offsets, const std::vector< ScalarTargetValue > &rhs_values, const SQLTypeInfo &rhs_type, const Data_Namespace::MemoryLevel memory_level, UpdelRoll &updel_roll) override
void updateColumns(const Catalog_Namespace::Catalog *catalog, const TableDescriptor *td, const int fragmentId, const std::vector< TargetMetaInfo > sourceMetaInfo, const std::vector< const ColumnDescriptor * > columnDescriptors, const RowDataProvider &sourceDataProvider, const size_t indexOffFragmentOffsetColumn, const Data_Namespace::MemoryLevel memoryLevel, UpdelRoll &updelRoll, Executor *executor) override
The InsertOrderFragmenter is a child class of AbstractFragmenter, and fragments data in insert order...
auto vacuum_fixlen_rows(const FragmentInfo &fragment, const std::shared_ptr< Chunk_NS::Chunk > &chunk, const std::vector< uint64_t > &frag_offsets)
void insertData(InsertData &insert_data_struct) override
appends data onto the most recently occuring fragment, creating a new one if necessary ...
void updateColumnChunkMetadata(const ColumnDescriptor *cd, const int fragment_id, const std::shared_ptr< ChunkMetadata > metadata) override
Updates the metadata for a column chunk.
void insertChunks(const InsertChunks &insert_chunk) override
Insert chunks into minimal number of fragments.
#define DEFAULT_MAX_ROWS
std::string getFragmenterType() override
get fragmenter&#39;s type (as string
const std::vector< uint64_t > getVacuumOffsets(const std::shared_ptr< Chunk_NS::Chunk > &chunk) override
std::shared_timed_mutex mapd_shared_mutex
void addColumns(const InsertData &insertDataStruct)
Used by Fragmenter classes to store info about each fragment - the fragment id and number of tuples(r...
Definition: Fragmenter.h:84
void deleteFragments(const std::vector< int > &dropFragIds)
void setNumRows(const size_t numTuples) override
specifies the content in-memory of a row in the column metadata table
FragmentInfo * createNewFragment(const Data_Namespace::MemoryLevel memory_level=Data_Namespace::DISK_LEVEL)
creates new fragment, calling createChunk() method of BufferMgr to make a new chunk for each column o...
#define DEFAULT_PAGE_SIZE
Executor(const ExecutorId id, Data_Namespace::DataMgr *data_mgr, const size_t block_size_x, const size_t grid_size_x, const size_t max_gpu_slab_size, const std::string &debug_dir, const std::string &debug_file)
Definition: Execute.cpp:158
void lockInsertCheckpointData(const InsertData &insertDataStruct)
void insertChunksNoCheckpoint(const InsertChunks &insert_chunk) override
Insert chunks into minimal number of fragments; no locks or checkpoints taken.
void compactRows(const Catalog_Namespace::Catalog *catalog, const TableDescriptor *td, const int fragment_id, const std::vector< uint64_t > &frag_offsets, const Data_Namespace::MemoryLevel memory_level, UpdelRoll &updel_roll) override
FragmentInfo * getFragmentInfo(const int fragment_id) const override
Retrieve the fragment info object for an individual fragment for editing.
#define DEFAULT_FRAGMENT_ROWS
void updateChunkStats(const ColumnDescriptor *cd, std::unordered_map< int, ChunkStats > &stats_map, std::optional< Data_Namespace::MemoryLevel > memory_level) override
Update chunk stats.
auto getChunksForAllColumns(const TableDescriptor *td, const FragmentInfo &fragment, const Data_Namespace::MemoryLevel memory_level)
bool isAddingNewColumns(const InsertData &insert_data) const
std::unordered_map< int, size_t > varLenColInfo_
size_t getNumFragments() override
returns the number of fragments in a table
void updateColumnMetadata(const ColumnDescriptor *cd, FragmentInfo &fragment, std::shared_ptr< Chunk_NS::Chunk > chunk, const UpdateValuesStats &update_values_stats, const SQLTypeInfo &rhs_type, UpdelRoll &updel_roll) override
The data to be inserted using the fragment manager.
Definition: Fragmenter.h:66
void insertDataNoCheckpoint(InsertData &insert_data_struct) override
Given data wrapped in an InsertData struct, inserts it into the correct partitions No locks and check...
int getFragmenterId() override
get fragmenter&#39;s id
std::map< int, Chunk_NS::Chunk > columnMap_
auto vacuum_varlen_rows(const FragmentInfo &fragment, const std::shared_ptr< Chunk_NS::Chunk > &chunk, const std::vector< uint64_t > &frag_offsets)
bool hasDeletedRows(const int delete_column_id) override
Iterates through chunk metadata to return whether any rows have been deleted.
boost::variant< int64_t, double, float, NullableString > ScalarTargetValue
Definition: TargetValue.h:156