-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquery.cpp
More file actions
430 lines (373 loc) · 13 KB
/
Copy pathquery.cpp
File metadata and controls
430 lines (373 loc) · 13 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// Copyright 2026 Davide Faconti
// SPDX-License-Identifier: MPL-2.0
#include "pj_datastore/query.hpp"
#include <algorithm>
#include <cassert>
#include <cmath>
#include <limits>
#include <utility>
namespace PJ {
namespace {
[[nodiscard]] Range<Timestamp> normalized(Range<Timestamp> range) {
if (range.max < range.min) {
std::swap(range.min, range.max);
}
return range;
}
[[nodiscard]] bool isBoolColumn(const TopicChunk& chunk, std::size_t column_index) {
return column_index < chunk.columns.size() && chunk.columns[column_index].descriptor &&
chunk.columns[column_index].descriptor->logical_type == PrimitiveType::kBool;
}
[[nodiscard]] std::optional<double> readSeriesValue(
const TopicChunk& chunk, std::size_t column_index, std::size_t row) {
if (column_index >= chunk.columns.size() || row >= chunk.stats.row_count || chunk.isNull(column_index, row)) {
return std::nullopt;
}
if (isBoolColumn(chunk, column_index)) {
return chunk.readBool(column_index, row) ? 1.0 : 0.0;
}
return chunk.readNumericAsDouble(column_index, row);
}
[[nodiscard]] SeriesSample makeSeriesSample(const TopicChunk& chunk, std::size_t column_index, std::size_t row) {
const auto value = readSeriesValue(chunk, column_index, row);
assert(value.has_value());
return SeriesSample{chunk.readTimestamp(row), *value, &chunk, row};
}
[[nodiscard]] Range<Timestamp> allTime() {
return Range<Timestamp>{
.min = std::numeric_limits<Timestamp>::min(),
.max = std::numeric_limits<Timestamp>::max(),
};
}
} // namespace
// ===========================================================================
// RangeCursor
// ===========================================================================
RangeCursor::RangeCursor(const std::deque<TopicChunk>& chunks, Timestamp t_min, Timestamp t_max)
: chunks_(&chunks), t_min_(t_min), t_max_(t_max) {
findFirstValid();
}
bool RangeCursor::valid() const noexcept {
return chunk_index_ < chunks_->size();
}
SampleRow RangeCursor::current() const {
assert(valid());
const auto& chunk = (*chunks_)[chunk_index_];
return SampleRow{chunk.readTimestamp(row_index_), &chunk, row_index_};
}
void RangeCursor::advance() {
assert(valid());
const auto& chunk = (*chunks_)[chunk_index_];
++row_index_;
if (row_index_ >= chunk.stats.row_count) {
++chunk_index_;
row_index_ = 0;
}
skipToValid();
}
void RangeCursor::forEach(std::function<void(const SampleRow&)> callback) {
while (valid()) {
callback(current());
advance();
}
}
void RangeCursor::forEachChunk(std::function<void(const ChunkRowRange&)> callback) {
while (chunk_index_ < chunks_->size()) {
const auto& chunk = (*chunks_)[chunk_index_];
// Skip chunks entirely before our range
if (chunk.stats.t_max < t_min_) {
++chunk_index_;
continue;
}
// Stop if chunk is entirely after our range
if (chunk.stats.t_min > t_max_) {
break;
}
// Find first valid row in this chunk (>= t_min_)
std::size_t first = row_index_;
while (first < chunk.stats.row_count && chunk.readTimestamp(first) < t_min_) {
++first;
}
// Find one-past-last valid row in this chunk (<= t_max_)
std::size_t end = first;
while (end < chunk.stats.row_count && chunk.readTimestamp(end) <= t_max_) {
++end;
}
if (first < end) {
callback(ChunkRowRange{&chunk, first, end});
}
// Move to next chunk
++chunk_index_;
row_index_ = 0;
}
// Mark cursor exhausted
chunk_index_ = chunks_->size();
}
void RangeCursor::findFirstValid() {
const auto& chunks = *chunks_;
// First chunk that could contain a row in range, i.e. whose t_max >= t_min_.
// Committed chunks are non-empty and time-ordered (each chunk's t_min >= the
// previous chunk's t_max), so t_max is non-decreasing across the deque and we
// can binary-search it.
const auto chunk_it = std::lower_bound(
chunks.begin(), chunks.end(), t_min_,
[](const TopicChunk& chunk, Timestamp value) { return chunk.stats.t_max < value; });
if (chunk_it == chunks.end()) {
// All data is strictly before t_min_.
chunk_index_ = chunks.size();
row_index_ = 0;
return;
}
chunk_index_ = static_cast<std::size_t>(chunk_it - chunks.begin());
// First row with timestamp >= t_min_ within that chunk. Such a row exists
// because t_max (the chunk's last timestamp) >= t_min_.
const TopicChunk& chunk = *chunk_it;
const auto ts_begin = chunk.timestamps.begin();
const auto ts_end = ts_begin + static_cast<std::ptrdiff_t>(chunk.stats.row_count);
const auto row_it = std::lower_bound(ts_begin, ts_end, t_min_);
row_index_ = static_cast<std::size_t>(row_it - ts_begin);
// If the first row at or after t_min_ is already past t_max_, nothing in the
// deque falls inside [t_min_, t_max_].
if (row_it == ts_end || *row_it > t_max_) {
chunk_index_ = chunks.size();
row_index_ = 0;
}
}
void RangeCursor::skipToValid() {
if (!valid()) {
return;
}
const auto& chunk = (*chunks_)[chunk_index_];
Timestamp ts = chunk.readTimestamp(row_index_);
if (ts > t_max_) {
// Past the end of the query range
chunk_index_ = chunks_->size();
return;
}
// ts >= t_min_ is guaranteed by how we advance through sorted data
}
// ===========================================================================
// latest_at
// ===========================================================================
std::optional<SampleRow> latestAt(const std::deque<TopicChunk>& chunks, Timestamp t) {
// Last chunk that can contain a row at or before t, i.e. the latest chunk
// whose t_min <= t. Committed chunks are non-empty and have non-decreasing
// t_min, so upper_bound finds the first chunk strictly after t; the chunk
// before it is the answer. (At a shared boundary timestamp this selects the
// later chunk, matching the previous reverse-scan behaviour.)
const auto after = std::upper_bound(chunks.begin(), chunks.end(), t, [](Timestamp value, const TopicChunk& chunk) {
return value < chunk.stats.t_min;
});
if (after == chunks.begin()) {
// Empty deque, or every chunk starts strictly after t.
return std::nullopt;
}
const TopicChunk& chunk = *(after - 1);
// Last row with timestamp <= t within that chunk. Such a row exists because
// the chunk's first timestamp (t_min) is <= t.
const auto ts_begin = chunk.timestamps.begin();
const auto ts_end = ts_begin + static_cast<std::ptrdiff_t>(chunk.stats.row_count);
const auto row_after = std::upper_bound(ts_begin, ts_end, t);
if (row_after == ts_begin) {
return std::nullopt; // unreachable for committed chunks (row 0 ts == t_min <= t)
}
const std::size_t row = static_cast<std::size_t>((row_after - 1) - ts_begin);
return SampleRow{chunk.readTimestamp(row), &chunk, row};
}
// ===========================================================================
// range_query
// ===========================================================================
RangeCursor rangeQuery(const std::deque<TopicChunk>& chunks, Timestamp t_min, Timestamp t_max) {
return RangeCursor(chunks, t_min, t_max);
}
// ===========================================================================
// SeriesCursor
// ===========================================================================
SeriesCursor::SeriesCursor(const std::deque<TopicChunk>& chunks, std::size_t column_index, Range<Timestamp> time_range)
: chunks_(&chunks), column_index_(column_index), time_range_(normalized(time_range)) {
skipToSample();
}
bool SeriesCursor::valid() const noexcept {
return chunk_index_ < chunks_->size();
}
SeriesSample SeriesCursor::current() const {
assert(valid());
return makeSeriesSample((*chunks_)[chunk_index_], column_index_, row_index_);
}
void SeriesCursor::advance() {
assert(valid());
++row_index_;
skipToSample();
}
void SeriesCursor::forEach(std::function<void(const SeriesSample&)> callback) {
while (valid()) {
callback(current());
advance();
}
}
void SeriesCursor::skipToSample() {
while (chunk_index_ < chunks_->size()) {
const auto& chunk = (*chunks_)[chunk_index_];
if (chunk.stats.row_count == 0 || chunk.stats.t_max < time_range_.min || column_index_ >= chunk.columns.size()) {
++chunk_index_;
row_index_ = 0;
continue;
}
if (chunk.stats.t_min > time_range_.max) {
chunk_index_ = chunks_->size();
return;
}
while (row_index_ < chunk.stats.row_count) {
const Timestamp ts = chunk.readTimestamp(row_index_);
if (ts < time_range_.min) {
++row_index_;
continue;
}
if (ts > time_range_.max) {
chunk_index_ = chunks_->size();
return;
}
if (readSeriesValue(chunk, column_index_, row_index_).has_value()) {
return;
}
++row_index_;
}
++chunk_index_;
row_index_ = 0;
}
}
// ===========================================================================
// SeriesReader
// ===========================================================================
SeriesReader::SeriesReader(const std::deque<TopicChunk>& chunks, std::size_t column_index)
: chunks_(&chunks), column_index_(column_index) {}
std::size_t SeriesReader::size() const {
std::size_t count = 0;
for (const TopicChunk& chunk : *chunks_) {
if (column_index_ >= chunk.columns.size()) {
continue;
}
for (std::size_t row = 0; row < chunk.stats.row_count; ++row) {
if (readSeriesValue(chunk, column_index_, row).has_value()) {
++count;
}
}
}
return count;
}
bool SeriesReader::empty() const {
return size() == 0;
}
std::optional<SeriesSample> SeriesReader::sampleAt(std::size_t index) const {
std::size_t series_index = 0;
for (const TopicChunk& chunk : *chunks_) {
if (column_index_ >= chunk.columns.size()) {
continue;
}
for (std::size_t row = 0; row < chunk.stats.row_count; ++row) {
if (!readSeriesValue(chunk, column_index_, row).has_value()) {
continue;
}
if (series_index == index) {
return makeSeriesSample(chunk, column_index_, row);
}
++series_index;
}
}
return std::nullopt;
}
std::optional<std::size_t> SeriesReader::indexAtOrBeforeTime(Timestamp t) const {
std::optional<std::size_t> latest;
std::size_t series_index = 0;
for (const TopicChunk& chunk : *chunks_) {
if (chunk.stats.row_count == 0 || column_index_ >= chunk.columns.size()) {
continue;
}
if (chunk.stats.t_min > t) {
break;
}
for (std::size_t row = 0; row < chunk.stats.row_count; ++row) {
const Timestamp ts = chunk.readTimestamp(row);
if (ts > t) {
return latest;
}
if (readSeriesValue(chunk, column_index_, row).has_value()) {
latest = series_index;
++series_index;
}
}
}
return latest;
}
std::optional<std::size_t> SeriesReader::indexAtOrAfterTime(Timestamp t) const {
std::size_t series_index = 0;
for (const TopicChunk& chunk : *chunks_) {
if (chunk.stats.row_count == 0 || column_index_ >= chunk.columns.size()) {
continue;
}
if (chunk.stats.t_max < t) {
for (std::size_t row = 0; row < chunk.stats.row_count; ++row) {
if (readSeriesValue(chunk, column_index_, row).has_value()) {
++series_index;
}
}
continue;
}
for (std::size_t row = 0; row < chunk.stats.row_count; ++row) {
if (!readSeriesValue(chunk, column_index_, row).has_value()) {
continue;
}
if (chunk.readTimestamp(row) >= t) {
return series_index;
}
++series_index;
}
}
return std::nullopt;
}
std::optional<SeriesSample> SeriesReader::sampleAtOrBeforeTime(Timestamp t) const {
const auto index = indexAtOrBeforeTime(t);
return index.has_value() ? sampleAt(*index) : std::nullopt;
}
std::optional<SeriesSample> SeriesReader::sampleAtOrAfterTime(Timestamp t) const {
const auto index = indexAtOrAfterTime(t);
return index.has_value() ? sampleAt(*index) : std::nullopt;
}
SeriesCursor SeriesReader::samples(Range<Timestamp> time_range) const {
return SeriesCursor(*chunks_, column_index_, time_range);
}
std::optional<SeriesBounds> SeriesReader::bounds() const {
return bounds(allTime());
}
std::optional<SeriesBounds> SeriesReader::bounds(Range<Timestamp> time_range) const {
SeriesBounds result;
bool found_time = false;
bool found_value = false;
auto cursor = samples(time_range);
cursor.forEach([&](const SeriesSample& sample) {
if (!found_time) {
result.time.min = sample.timestamp;
result.time.max = sample.timestamp;
found_time = true;
} else {
result.time.min = std::min(result.time.min, sample.timestamp);
result.time.max = std::max(result.time.max, sample.timestamp);
}
if (std::isfinite(sample.value)) {
if (!found_value) {
result.value.min = sample.value;
result.value.max = sample.value;
found_value = true;
} else {
result.value.min = std::min(result.value.min, sample.value);
result.value.max = std::max(result.value.max, sample.value);
}
}
++result.sample_count;
});
if (!found_time || !found_value) {
return std::nullopt;
}
return result;
}
} // namespace PJ