forked from pingcap/tiflash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDMFileBlockInputStream.h
More file actions
285 lines (243 loc) · 9.36 KB
/
DMFileBlockInputStream.h
File metadata and controls
285 lines (243 loc) · 9.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2023 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <Common/Exception.h>
#include <Common/config.h> // For ENABLE_CLARA
#include <Interpreters/Context_fwd.h>
#include <Interpreters/Settings.h>
#include <Storages/DeltaMerge/DeltaMergeDefines.h>
#include <Storages/DeltaMerge/File/ColumnCache.h>
#include <Storages/DeltaMerge/File/ColumnCacheLongTerm_fwd.h>
#include <Storages/DeltaMerge/File/DMFileReader.h>
#include <Storages/DeltaMerge/Index/VectorIndex/Stream/Ctx_fwd.h>
#include <Storages/DeltaMerge/RowKeyRange.h>
#include <Storages/DeltaMerge/ScanContext_fwd.h>
#include <Storages/DeltaMerge/SkippableBlockInputStream.h>
#if ENABLE_CLARA
#include <Storages/DeltaMerge/Index/FullTextIndex/Stream/Ctx_fwd.h>
#endif
namespace DB::DM
{
inline static constexpr size_t DMFILE_READ_ROWS_THRESHOLD = DEFAULT_MERGE_BLOCK_SIZE * 3;
class DMFileBlockInputStream : public SkippableBlockInputStream
{
public:
explicit DMFileBlockInputStream(DMFileReader && reader_, bool enable_data_sharing_)
: reader(std::move(reader_))
, enable_data_sharing(enable_data_sharing_)
{
if (enable_data_sharing)
{
DMFileReaderPool::instance().add(reader);
}
}
~DMFileBlockInputStream() override
{
if (enable_data_sharing)
{
DMFileReaderPool::instance().del(reader);
}
}
String getName() const override { return "DMFile"; }
Block getHeader() const override { return reader.getHeader(); }
bool getSkippedRows(size_t & skip_rows) override { return reader.getSkippedRows(skip_rows); }
size_t skipNextBlock() override { return reader.skipNextBlock(); }
Block read() override { return reader.read(); }
Block readWithFilter(const IColumn::Filter & filter) override { return reader.readWithFilter(filter); }
private:
friend class tests::DMFileMetaV2Test;
DMFileReader reader;
const bool enable_data_sharing;
};
using DMFileBlockInputStreamPtr = std::shared_ptr<DMFileBlockInputStream>;
class DMFileBlockInputStreamBuilder
{
public:
// Construct a builder by `context`.
// It implicitly set the params by
// - mark cache and min-max-index cache from global context
// - current settings from this context
// - current read limiter form this context
// - current file provider from this context
explicit DMFileBlockInputStreamBuilder(const Context & context);
// Build the final stream ptr. LocalIndex will take effect.
// Empty `rowkey_ranges` means not filter by rowkey
SkippableBlockInputStreamPtr build(
const DMFilePtr & dmfile,
const ColumnDefines & read_columns,
const RowKeyRanges & rowkey_ranges,
const ScanContextPtr & scan_context);
// **** filters **** //
// Only set enable_handle_clean_read_ param to true when
// in normal mode (is_fast_scan_ == false):
// 1. There is no delta.
// 2. You don't need pk, version and delete_tag columns
// in fast scan mode (is_fast_scan_ == true):
// 1. You don't need pk columns
// If you have no idea what it means, then simply set it to false.
// Only set enable_del_clean_read_ param to true when you don't need del columns in fast scan.
// `max_data_version_` is the MVCC filter version for reading. Used by clean read check
DMFileBlockInputStreamBuilder & enableCleanRead(
bool enable_handle_clean_read_,
bool is_fast_scan_,
bool enable_del_clean_read_,
UInt64 max_data_version_)
{
enable_handle_clean_read = enable_handle_clean_read_;
enable_del_clean_read = enable_del_clean_read_;
is_fast_scan = is_fast_scan_;
max_data_version = max_data_version_;
return *this;
}
DMFileBlockInputStreamBuilder & setVecIndexQuery(const VectorIndexStreamCtxPtr & ctx)
{
vec_index_ctx = ctx;
return *this;
}
#if ENABLE_CLARA
DMFileBlockInputStreamBuilder & setFtsIndexQuery(const FullTextIndexStreamCtxPtr & ctx)
{
fts_index_ctx = ctx;
return *this;
}
#endif
DMFileBlockInputStreamBuilder & setReadPacks(const IdSetPtr & read_packs_)
{
read_packs = read_packs_;
RUNTIME_CHECK_MSG(
read_packs == nullptr || pack_filter == nullptr,
"pack_filter is not nullptr when setting read_packs");
return *this;
}
DMFileBlockInputStreamBuilder & setColumnCache(const ColumnCachePtr & column_cache_)
{
// note that `enable_column_cache` is controlled by Settings (see `setFromSettings`)
column_cache = column_cache_;
return *this;
}
DMFileBlockInputStreamBuilder & onlyReadOnePackEveryTime()
{
read_one_pack_every_time = true;
return *this;
}
DMFileBlockInputStreamBuilder & setRowsThreshold(size_t rows_threshold_per_read_)
{
rows_threshold_per_read = rows_threshold_per_read_;
return *this;
}
DMFileBlockInputStreamBuilder & setTracingID(const String & tracing_id_)
{
tracing_id = tracing_id_;
return *this;
}
DMFileBlockInputStreamBuilder & setReadTag(ReadTag read_tag_)
{
read_tag = read_tag_;
return *this;
}
DMFileBlockInputStreamBuilder & setDMFilePackFilterResult(const DMFilePackFilterResultPtr & pack_filter_)
{
pack_filter = pack_filter_;
RUNTIME_CHECK_MSG(
pack_filter == nullptr || read_packs == nullptr,
"read_packs is not nullptr when setting pack_filter");
return *this;
}
/**
* @note To really enable the long term cache, you also need to ensure
* ColumnCacheLongTerm is initialized in the global context.
*/
DMFileBlockInputStreamBuilder & enableColumnCacheLongTerm(ColumnID pk_col_id_)
{
pk_col_id = pk_col_id_;
return *this;
}
private:
DMFileBlockInputStreamPtr buildNoLocalIndex(
const DMFilePtr & dmfile,
const ColumnDefines & read_columns,
const RowKeyRanges & rowkey_ranges,
const ScanContextPtr & scan_context);
/// The returned stream should be plugged into a VectorIndexInputStream. Plug to somewhere else may not work.
SkippableBlockInputStreamPtr buildForVectorIndex(
const DMFilePtr & dmfile,
const ColumnDefines & read_columns,
const RowKeyRanges & rowkey_ranges,
const ScanContextPtr & scan_context);
#if ENABLE_CLARA
/// The returned stream should be plugged into a FullTextIndexInputStream. Plug to somewhere else will not work.
SkippableBlockInputStreamPtr buildForFullTextIndex(
const DMFilePtr & dmfile,
const ColumnDefines & read_columns,
const RowKeyRanges & rowkey_ranges,
const ScanContextPtr & scan_context);
#endif
private:
// These methods are called by the ctor
DMFileBlockInputStreamBuilder & setFromSettings(const Settings & settings);
DMFileBlockInputStreamBuilder & setCaches(
const MarkCachePtr & mark_cache_,
const MinMaxIndexCachePtr & index_cache_,
const ColumnCacheLongTermPtr & column_cache_long_term_)
{
mark_cache = mark_cache_;
index_cache = index_cache_;
column_cache_long_term = column_cache_long_term_;
return *this;
}
private:
FileProviderPtr file_provider;
// clean read
bool enable_handle_clean_read = false;
bool is_fast_scan = false;
bool enable_del_clean_read = false;
UInt64 max_data_version = std::numeric_limits<UInt64>::max();
// packs filter (filter by pack index)
IdSetPtr read_packs;
MarkCachePtr mark_cache;
MinMaxIndexCachePtr index_cache;
// column cache
bool enable_column_cache = false;
ColumnCachePtr column_cache;
ReadLimiterPtr read_limiter;
size_t max_read_buffer_size{};
size_t rows_threshold_per_read = DMFILE_READ_ROWS_THRESHOLD;
bool read_one_pack_every_time = false;
size_t max_sharing_column_bytes_for_all = 0;
String tracing_id;
ReadTag read_tag = ReadTag::Internal;
DMFilePackFilterResultPtr pack_filter;
/// If set, will *try* to build a VectorIndexDMFileInputStream
/// instead of a normal DMFileBlockInputStream.
VectorIndexStreamCtxPtr vec_index_ctx = nullptr;
#if ENABLE_CLARA
FullTextIndexStreamCtxPtr fts_index_ctx = nullptr;
#endif
// Note: column_cache_long_term is currently only filled when performing Vector Search.
ColumnCacheLongTermPtr column_cache_long_term = nullptr;
ColumnID pk_col_id = 0;
};
/**
* Create a simple stream that read all blocks on default.
* Only read one pack every time.
* @param context Database context.
* @param file DMFile pointer.
* @param cols The columns to read. Empty means read all columns.
* @return A shared pointer of an input stream
*/
SkippableBlockInputStreamPtr createSimpleBlockInputStream(
const DB::Context & context,
const DMFilePtr & file,
ColumnDefines cols = {});
} // namespace DB::DM