OmniSciDB  fe05a0c208
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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;
78  // virtual void getFragmentsForQuery(QueryInfo &queryInfo, const void *predicate = 0);
80 
88  void insertData(InsertData& insert_data_struct) override;
89 
90  void insertDataNoCheckpoint(InsertData& insert_data_struct) override;
91 
92  void dropFragmentsToSize(const size_t maxRows) override;
93 
95  const int fragment_id,
96  const std::shared_ptr<ChunkMetadata> metadata) override;
97 
98  void updateChunkStats(const ColumnDescriptor* cd,
99  std::unordered_map</*fragment_id*/ int, ChunkStats>& stats_map,
100  std::optional<Data_Namespace::MemoryLevel> memory_level) override;
101 
102  FragmentInfo* getFragmentInfo(const int fragment_id) const override;
103 
107  inline int getFragmenterId() override { return chunkKeyPrefix_.back(); }
108  inline std::vector<int> getChunkKeyPrefix() const { return chunkKeyPrefix_; }
112  inline std::string getFragmenterType() override { return fragmenterType_; }
113  size_t getNumRows() override { return numTuples_; }
114  void setNumRows(const size_t numTuples) override { numTuples_ = numTuples; }
115 
116  std::optional<ChunkUpdateStats> updateColumn(
117  const Catalog_Namespace::Catalog* catalog,
118  const TableDescriptor* td,
119  const ColumnDescriptor* cd,
120  const int fragment_id,
121  const std::vector<uint64_t>& frag_offsets,
122  const std::vector<ScalarTargetValue>& rhs_values,
123  const SQLTypeInfo& rhs_type,
124  const Data_Namespace::MemoryLevel memory_level,
125  UpdelRoll& updel_roll) override;
126 
127  void updateColumns(const Catalog_Namespace::Catalog* catalog,
128  const TableDescriptor* td,
129  const int fragmentId,
130  const std::vector<TargetMetaInfo> sourceMetaInfo,
131  const std::vector<const ColumnDescriptor*> columnDescriptors,
132  const RowDataProvider& sourceDataProvider,
133  const size_t indexOffFragmentOffsetColumn,
134  const Data_Namespace::MemoryLevel memoryLevel,
135  UpdelRoll& updelRoll,
136  Executor* executor) override;
137 
138  void updateColumn(const Catalog_Namespace::Catalog* catalog,
139  const TableDescriptor* td,
140  const ColumnDescriptor* cd,
141  const int fragment_id,
142  const std::vector<uint64_t>& frag_offsets,
143  const ScalarTargetValue& rhs_value,
144  const SQLTypeInfo& rhs_type,
145  const Data_Namespace::MemoryLevel memory_level,
146  UpdelRoll& updel_roll) override;
147 
148  void updateColumnMetadata(const ColumnDescriptor* cd,
149  FragmentInfo& fragment,
150  std::shared_ptr<Chunk_NS::Chunk> chunk,
151  const UpdateValuesStats& update_values_stats,
152  const SQLTypeInfo& rhs_type,
153  UpdelRoll& updel_roll) override;
154 
155  void updateMetadata(const Catalog_Namespace::Catalog* catalog,
156  const MetaDataKey& key,
157  UpdelRoll& updel_roll) override;
158 
159  void compactRows(const Catalog_Namespace::Catalog* catalog,
160  const TableDescriptor* td,
161  const int fragment_id,
162  const std::vector<uint64_t>& frag_offsets,
163  const Data_Namespace::MemoryLevel memory_level,
164  UpdelRoll& updel_roll) override;
165 
166  const std::vector<uint64_t> getVacuumOffsets(
167  const std::shared_ptr<Chunk_NS::Chunk>& chunk) override;
168 
170  const FragmentInfo& fragment,
171  const Data_Namespace::MemoryLevel memory_level);
172 
173  void dropColumns(const std::vector<int>& columnIds) override;
174 
175  bool hasDeletedRows(const int delete_column_id) override;
176 
177  protected:
178  std::vector<int> chunkKeyPrefix_;
179  std::map<int, Chunk_NS::Chunk>
181  std::deque<std::unique_ptr<FragmentInfo>>
183  // int currentInsertBufferFragmentId_;
186  const int physicalTableId_;
187  const int shard_;
189  size_t pageSize_; /* Page size in bytes of each page making up a given chunk - passed to
190  BufferMgr in createChunk() */
191  size_t numTuples_;
194  size_t maxRows_;
195  std::string fragmenterType_;
197  fragmentInfoMutex_; // to prevent read-write conflicts for fragmentInfoVec_
199  insertMutex_; // to prevent race conditions on insert - only one insert statement
200  // should be going to a table at a time
205  std::unordered_map<int, size_t> varLenColInfo_;
206  std::shared_ptr<std::mutex> mutex_access_inmem_states;
207 
218  void deleteFragments(const std::vector<int>& dropFragIds);
219 
221  void getChunkMetadata();
222 
223  void lockInsertCheckpointData(const InsertData& insertDataStruct);
224  void insertDataImpl(InsertData& insert_data);
225  void addColumns(const InsertData& insertDataStruct);
226 
229  // FIX-ME: Temporary lock; needs removing.
230  mutable std::mutex temp_mutex_;
231 
232  FragmentInfo& getFragmentInfoFromId(const int fragment_id);
233 
234  auto vacuum_fixlen_rows(const FragmentInfo& fragment,
235  const std::shared_ptr<Chunk_NS::Chunk>& chunk,
236  const std::vector<uint64_t>& frag_offsets);
237  auto vacuum_varlen_rows(const FragmentInfo& fragment,
238  const std::shared_ptr<Chunk_NS::Chunk>& chunk,
239  const std::vector<uint64_t>& frag_offsets);
240 
241  private:
242  bool isAddingNewColumns(const InsertData& insert_data) const;
243  void dropFragmentsToSizeNoInsertLock(const size_t max_rows);
244 };
245 
246 } // 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 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:102
TableInfo getFragmentsForQuery() override
returns (inside QueryInfo) object all ids and row sizes of fragments
InsertOrderFragmenter & operator=(const InsertOrderFragmenter &)
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.
#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:77
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
void lockInsertCheckpointData(const InsertData &insertDataStruct)
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_
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:59
void insertDataNoCheckpoint(InsertData &insert_data_struct) override
Given data wrapped in an InsertData struct, inserts it into the correct partitions No locks and check...
Executor(const ExecutorId id, 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:144
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