• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #3526

10 Nov 2024 03:50AM UTC coverage: 60.225% (-0.6%) from 60.818%
#3526

push

travis-ci

web-flow
Merge pull request #28709 from taosdata/main

merge: from main to 3.0 branch

117031 of 249004 branches covered (47.0%)

Branch coverage included in aggregate %.

130 of 169 new or added lines in 23 files covered. (76.92%)

4149 existing lines in 176 files now uncovered.

197577 of 273386 relevant lines covered (72.27%)

5840219.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.03
/source/dnode/vnode/src/tsdb/tsdbReadUtil.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "tsdbReadUtil.h"
17
#include "tsdb.h"
18
#include "tsdbDataFileRW.h"
19
#include "tsdbFS2.h"
20
#include "tsdbMerge.h"
21
#include "tsdbUtil2.h"
22
#include "tsimplehash.h"
23

24
static bool overlapWithDelSkylineWithoutVer(STableBlockScanInfo* pBlockScanInfo, const SBrinRecord* pRecord,
25
                                            int32_t order);
26

27
static int32_t initBlockScanInfoBuf(SBlockInfoBuf* pBuf, int32_t numOfTables) {
1,387,277✔
28
  int32_t num = numOfTables / pBuf->numPerBucket;
1,387,277✔
29
  int32_t remainder = numOfTables % pBuf->numPerBucket;
1,387,277✔
30

31
  if (pBuf->pData == NULL) {
1,387,277!
32
    pBuf->pData = taosArrayInit(num + 1, POINTER_BYTES);
1,387,942✔
33
    if (pBuf->pData == NULL) {
1,389,107!
34
      return terrno;
×
35
    }
36
  }
37

38
  for (int32_t i = 0; i < num; ++i) {
1,388,896✔
39
    char* p = taosMemoryCalloc(pBuf->numPerBucket, sizeof(STableBlockScanInfo));
8✔
40
    if (p == NULL) {
8!
41
      return terrno;
×
42
    }
43

44
    void* px = taosArrayPush(pBuf->pData, &p);
8✔
45
    if (px == NULL) {
8!
46
      return terrno;
×
47
    }
48
  }
49

50
  if (remainder > 0) {
1,388,888✔
51
    char* p = taosMemoryCalloc(remainder, sizeof(STableBlockScanInfo));
1,344,254✔
52
    if (p == NULL) {
1,344,742!
53
      return terrno;
×
54
    }
55
    void* px = taosArrayPush(pBuf->pData, &p);
1,344,742✔
56
    if (px == NULL) {
1,345,099!
57
      return terrno;
×
58
    }
59
  }
60

61
  pBuf->numOfTables = numOfTables;
1,389,733✔
62

63
  return TSDB_CODE_SUCCESS;
1,389,733✔
64
}
65

66
int32_t uidComparFunc(const void* p1, const void* p2) {
4,070,185✔
67
  uint64_t pu1 = *(uint64_t*)p1;
4,070,185✔
68
  uint64_t pu2 = *(uint64_t*)p2;
4,070,185✔
69
  if (pu1 == pu2) {
4,070,185!
70
    return 0;
×
71
  } else {
72
    return (pu1 < pu2) ? -1 : 1;
4,070,185✔
73
  }
74
}
75

76
int32_t ensureBlockScanInfoBuf(SBlockInfoBuf* pBuf, int32_t numOfTables) {
6✔
77
  if (numOfTables <= pBuf->numOfTables) {
6!
78
    return TSDB_CODE_SUCCESS;
×
79
  }
80

81
  if (pBuf->numOfTables > 0) {
6!
82
    STableBlockScanInfo** p = (STableBlockScanInfo**)taosArrayPop(pBuf->pData);
6✔
83
    taosMemoryFree(*p);
6✔
84
    pBuf->numOfTables /= pBuf->numPerBucket;
6✔
85
  }
86

87
  int32_t num = (numOfTables - pBuf->numOfTables) / pBuf->numPerBucket;
6✔
88
  int32_t remainder = (numOfTables - pBuf->numOfTables) % pBuf->numPerBucket;
6✔
89
  if (pBuf->pData == NULL) {
6!
90
    pBuf->pData = taosArrayInit(num + 1, POINTER_BYTES);
×
91
    if (pBuf->pData == NULL) {
×
92
      return terrno;
×
93
    }
94
  }
95

96
  for (int32_t i = 0; i < num; ++i) {
6!
97
    char* p = taosMemoryCalloc(pBuf->numPerBucket, sizeof(STableBlockScanInfo));
×
98
    if (p == NULL) {
×
99
      return terrno;
×
100
    }
101

102
    void* px = taosArrayPush(pBuf->pData, &p);
×
103
    if (px == NULL) {
×
104
      return terrno;
×
105
    }
106
  }
107

108
  if (remainder > 0) {
6!
109
    char* p = taosMemoryCalloc(remainder, sizeof(STableBlockScanInfo));
6✔
110
    if (p == NULL) {
6!
111
      return terrno;
×
112
    }
113
    void* px = taosArrayPush(pBuf->pData, &p);
6✔
114
    if (px == NULL) {
6!
115
      return terrno;
×
116
    }
117
  }
118

119
  pBuf->numOfTables = numOfTables;
6✔
120

121
  return TSDB_CODE_SUCCESS;
6✔
122
}
123

124
void clearBlockScanInfoBuf(SBlockInfoBuf* pBuf) {
1,439,137✔
125
  size_t num = taosArrayGetSize(pBuf->pData);
1,439,137✔
126
  for (int32_t i = 0; i < num; ++i) {
2,784,764✔
127
    char** p = taosArrayGet(pBuf->pData, i);
1,345,536✔
128
    if (p != NULL) {
1,345,508!
129
      taosMemoryFree(*p);
1,345,515✔
130
    }
131
  }
132

133
  taosArrayDestroy(pBuf->pData);
1,439,228✔
134
}
1,439,208✔
135

136
int32_t getPosInBlockInfoBuf(SBlockInfoBuf* pBuf, int32_t index, STableBlockScanInfo** pInfo) {
3,718,865✔
137
  *pInfo = NULL;
3,718,865✔
138

139
  int32_t bucketIndex = index / pBuf->numPerBucket;
3,718,865✔
140
  char**  pBucket = taosArrayGet(pBuf->pData, bucketIndex);
3,718,865✔
141
  if (pBucket == NULL) {
3,718,106!
142
    return TSDB_CODE_NOT_FOUND;
×
143
  }
144

145
  *pInfo = (STableBlockScanInfo*)((*pBucket) + (index % pBuf->numPerBucket) * sizeof(STableBlockScanInfo));
3,718,106✔
146
  return TSDB_CODE_SUCCESS;
3,718,106✔
147
}
148

149
int32_t getTableBlockScanInfo(SSHashObj* pTableMap, uint64_t uid, STableBlockScanInfo** pInfo, const char* id) {
1,232,413✔
150
  *pInfo = *(STableBlockScanInfo**)tSimpleHashGet(pTableMap, &uid, sizeof(uid));
1,232,413✔
151
  if (pInfo == NULL) {
1,232,573✔
152
    int32_t size = tSimpleHashGetSize(pTableMap);
171✔
153
    tsdbError("failed to locate the uid:%" PRIu64 " in query table uid list, total tables:%d, %s", uid, size, id);
×
154
    return TSDB_CODE_INVALID_PARA;
×
155
  }
156

157
  return TSDB_CODE_SUCCESS;
1,232,402✔
158
}
159

160
int32_t initRowKey(SRowKey* pKey, int64_t ts, int32_t numOfPks, int32_t type, int32_t len, bool asc) {
16,201,117✔
161
  pKey->numOfPKs = numOfPks;
16,201,117✔
162
  pKey->ts = ts;
16,201,117✔
163

164
  if (numOfPks > 0) {
16,201,117✔
165
    pKey->pks[0].type = type;
843,271✔
166

167
    if (IS_NUMERIC_TYPE(type)) {
843,271!
168
      if (asc) {
591,532✔
169
        switch (type) {
360,715!
170
          case TSDB_DATA_TYPE_BIGINT: {
88,427✔
171
            pKey->pks[0].val = INT64_MIN;
88,427✔
172
            break;
88,427✔
173
          }
174
          case TSDB_DATA_TYPE_INT: {
93,578✔
175
            int32_t min = INT32_MIN;
93,578✔
176
            (void)memcpy(&pKey->pks[0].val, &min, tDataTypes[type].bytes);
93,578✔
177
            break;
93,578✔
178
          }
179
          case TSDB_DATA_TYPE_SMALLINT: {
×
180
            int16_t min = INT16_MIN;
×
181
            (void)memcpy(&pKey->pks[0].val, &min, tDataTypes[type].bytes);
×
182
            break;
×
183
          }
184
          case TSDB_DATA_TYPE_TINYINT: {
×
185
            int8_t min = INT8_MIN;
×
186
            (void)memcpy(&pKey->pks[0].val, &min, tDataTypes[type].bytes);
×
187
            break;
×
188
          }
189
          case TSDB_DATA_TYPE_UTINYINT:
178,694✔
190
          case TSDB_DATA_TYPE_USMALLINT:
191
          case TSDB_DATA_TYPE_UINT:
192
          case TSDB_DATA_TYPE_UBIGINT: {
193
            pKey->pks[0].val = 0;
178,694✔
194
            break;
178,694✔
195
          }
196
          default:
16✔
197
            return TSDB_CODE_INVALID_PARA;
16✔
198
        }
199
      } else {
200
        switch (type) {
230,817!
201
          case TSDB_DATA_TYPE_BIGINT:
57,011✔
202
            pKey->pks[0].val = INT64_MAX;
57,011✔
203
            break;
57,011✔
204
          case TSDB_DATA_TYPE_INT:
58,131✔
205
            pKey->pks[0].val = INT32_MAX;
58,131✔
206
            break;
58,131✔
207
          case TSDB_DATA_TYPE_SMALLINT:
×
208
            pKey->pks[0].val = INT16_MAX;
×
209
            break;
×
210
          case TSDB_DATA_TYPE_TINYINT:
×
211
            pKey->pks[0].val = INT8_MAX;
×
212
            break;
×
213
          case TSDB_DATA_TYPE_UBIGINT:
57,182✔
214
            pKey->pks[0].val = UINT64_MAX;
57,182✔
215
            break;
57,182✔
216
          case TSDB_DATA_TYPE_UINT:
58,486✔
217
            pKey->pks[0].val = UINT32_MAX;
58,486✔
218
            break;
58,486✔
219
          case TSDB_DATA_TYPE_USMALLINT:
×
220
            pKey->pks[0].val = UINT16_MAX;
×
221
            break;
×
222
          case TSDB_DATA_TYPE_UTINYINT:
×
223
            pKey->pks[0].val = UINT8_MAX;
×
224
            break;
×
225
          default:
7✔
226
            return TSDB_CODE_INVALID_PARA;
7✔
227
        }
228
      }
229
    } else {
230
      pKey->pks[0].pData = taosMemoryCalloc(1, len);
251,739✔
231
      pKey->pks[0].nData = 0;
251,790✔
232

233
      if (pKey->pks[0].pData == NULL) {
251,790!
234
        return terrno;
×
235
      }
236

237
      if (!asc) {
253,919✔
238
        pKey->numOfPKs = 2;
111,251✔
239
      }
240
    }
241
  }
242

243
  return TSDB_CODE_SUCCESS;
16,203,274✔
244
}
245

246
void clearRowKey(SRowKey* pKey) {
16,224,427✔
247
  if (pKey == NULL || pKey->numOfPKs == 0 || (!IS_VAR_DATA_TYPE(pKey->pks[0].type))) {
16,224,427!
248
    return;
15,972,937✔
249
  }
250
  taosMemoryFreeClear(pKey->pks[0].pData);
251,490!
251
}
252

253
static int32_t initLastProcKey(STableBlockScanInfo* pScanInfo, STsdbReader* pReader) {
3,715,190✔
254
  int32_t code = 0;
3,715,190✔
255
  int32_t numOfPks = pReader->suppInfo.numOfPks;
3,715,190✔
256
  bool    asc = ASCENDING_TRAVERSE(pReader->info.order);
3,715,190✔
257
  int8_t  type = pReader->suppInfo.pk.type;
3,715,190✔
258
  int32_t bytes = pReader->suppInfo.pk.bytes;
3,715,190✔
259

260
  SRowKey* pRowKey = &pScanInfo->lastProcKey;
3,715,190✔
261
  if (asc) {
3,715,190✔
262
    int64_t skey = pReader->info.window.skey;
3,498,288✔
263
    int64_t ts = (skey > INT64_MIN) ? (skey - 1) : skey;
3,498,288!
264

265
    code = initRowKey(pRowKey, ts, numOfPks, type, bytes, asc);
3,498,288✔
266
    if (code != TSDB_CODE_SUCCESS) {
3,497,959!
267
      return code;
×
268
    }
269

270
    code = initRowKey(&pScanInfo->sttKeyInfo.nextProcKey, skey, numOfPks, type, bytes, asc);
3,497,959✔
271
    if (code != TSDB_CODE_SUCCESS) {
3,496,519!
272
      return code;
×
273
    }
274
  } else {
275
    int64_t ekey = pReader->info.window.ekey;
216,902✔
276
    int64_t ts = (ekey < INT64_MAX) ? (ekey + 1) : ekey;
216,902✔
277

278
    code = initRowKey(pRowKey, ts, numOfPks, type, bytes, asc);
216,902✔
279
    if (code != TSDB_CODE_SUCCESS) {
218,809!
280
      return code;
×
281
    }
282

283
    code = initRowKey(&pScanInfo->sttKeyInfo.nextProcKey, ekey, numOfPks, type, bytes, asc);
218,809✔
284
    if (code != TSDB_CODE_SUCCESS) {
218,580!
285
      return code;
×
286
    }
287
  }
288

289
  code = initRowKey(&pScanInfo->sttRange.skey, INT64_MAX, numOfPks, type, bytes, asc);
3,715,099✔
290
  if (code != TSDB_CODE_SUCCESS) {
3,711,989!
291
    return code;
×
292
  }
293

294
  code = initRowKey(&pScanInfo->sttRange.ekey, INT64_MIN, numOfPks, type, bytes, asc);
3,711,989✔
295
  return code;
3,712,063✔
296
}
297

298
int32_t initTableBlockScanInfo(STableBlockScanInfo* pScanInfo, uint64_t uid, SSHashObj* pTableMap,
3,717,087✔
299
                               STsdbReader* pReader) {
300
  pScanInfo->uid = uid;
3,717,087✔
301
  INIT_KEYRANGE(&pScanInfo->sttRange);
3,717,087✔
302
  INIT_TIMEWINDOW(&pScanInfo->filesetWindow);
3,717,087✔
303

304
  pScanInfo->cleanSttBlocks = false;
3,717,087✔
305
  pScanInfo->sttBlockReturned = false;
3,717,087✔
306

307
  int32_t code = initLastProcKey(pScanInfo, pReader);
3,717,087✔
308
  if (code != TSDB_CODE_SUCCESS) {
3,712,175!
309
    return code;
×
310
  }
311

312
  pScanInfo->sttKeyInfo.status = STT_FILE_READER_UNINIT;
3,712,175✔
313
  code = tSimpleHashPut(pTableMap, &pScanInfo->uid, sizeof(uint64_t), &pScanInfo, POINTER_BYTES);
3,712,175✔
314
  if (code != TSDB_CODE_SUCCESS) {
3,719,811!
315
    return code;
×
316
  }
317

318
  tsdbTrace("%p check table uid:%" PRId64 " from lastKey:%" PRId64 " %s", pReader, pScanInfo->uid,
3,719,811✔
319
            pScanInfo->lastProcKey.ts, pReader->idStr);
320
  return code;
3,719,861✔
321
}
322

323
// NOTE: speedup the whole processing by preparing the buffer for STableBlockScanInfo in batch model
324
int32_t createDataBlockScanInfo(STsdbReader* pTsdbReader, SBlockInfoBuf* pBuf, const STableKeyInfo* idList,
1,387,094✔
325
                                STableUidList* pUidList, int32_t numOfTables, SSHashObj** pHashObj) {
326
  int32_t code = 0;
1,387,094✔
327
  *pHashObj = NULL;
1,387,094✔
328

329
  // allocate buffer in order to load data blocks from file
330
  // todo use simple hash instead, optimize the memory consumption
331
  SSHashObj* pTableMap = tSimpleHashInit(numOfTables, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
1,387,094✔
332
  if (pTableMap == NULL) {
1,389,045!
333
    return terrno;
×
334
  }
335

336
  int64_t st = taosGetTimestampUs();
1,388,524✔
337
  code = initBlockScanInfoBuf(pBuf, numOfTables);
1,388,524✔
338
  if (code != TSDB_CODE_SUCCESS) {
1,388,799!
339
    tSimpleHashCleanup(pTableMap);
×
340
    return code;
×
341
  }
342

343
  pUidList->tableUidList = taosMemoryMalloc(numOfTables * sizeof(uint64_t));
1,388,799✔
344
  if (pUidList->tableUidList == NULL) {
1,389,011✔
345
    tSimpleHashCleanup(pTableMap);
760✔
346
    return terrno;
×
347
  }
348

349
  pUidList->currentIndex = 0;
1,388,251✔
350

351
  for (int32_t j = 0; j < numOfTables; ++j) {
5,088,333✔
352
    pUidList->tableUidList[j] = idList[j].uid;
3,700,138✔
353

354
    STableBlockScanInfo* pScanInfo = NULL;
3,700,138✔
355
    code = getPosInBlockInfoBuf(pBuf, j, &pScanInfo);
3,700,138✔
356
    if (code != TSDB_CODE_SUCCESS) {
3,698,428!
357
      break;
×
358
    }
359

360
    code = initTableBlockScanInfo(pScanInfo, idList[j].uid, pTableMap, pTsdbReader);
3,698,428✔
361
    if (code != TSDB_CODE_SUCCESS) {
3,700,082!
362
      break;
×
363
    }
364
  }
365

366
  taosSort(pUidList->tableUidList, numOfTables, sizeof(uint64_t), uidComparFunc);
1,388,195✔
367

368
  pTsdbReader->cost.createScanInfoList = (taosGetTimestampUs() - st) / 1000.0;
1,388,759✔
369
  tsdbDebug("%p create %d tables scan-info, size:%.2f Kb, elapsed time:%.2f ms, %s", pTsdbReader, numOfTables,
1,388,759✔
370
            (sizeof(STableBlockScanInfo) * numOfTables) / 1024.0, pTsdbReader->cost.createScanInfoList,
371
            pTsdbReader->idStr);
372

373
  *pHashObj = pTableMap;
1,389,043✔
374
  return code;
1,389,043✔
375
}
376

377
void resetAllDataBlockScanInfo(SSHashObj* pTableMap, int64_t ts, int32_t step) {
24,423✔
378
  void*   p = NULL;
24,423✔
379
  int32_t iter = 0;
24,423✔
380

381
  while ((p = tSimpleHashIterate(pTableMap, p, &iter)) != NULL) {
48,879✔
382
    STableBlockScanInfo* pInfo = *(STableBlockScanInfo**)p;
24,456✔
383

384
    pInfo->iterInit = false;
24,456✔
385
    pInfo->iter.hasVal = false;
24,456✔
386
    pInfo->iiter.hasVal = false;
24,456✔
387

388
    if (pInfo->iter.iter != NULL) {
24,456✔
389
      pInfo->iter.iter = tsdbTbDataIterDestroy(pInfo->iter.iter);
3,933✔
390
    }
391

392
    if (pInfo->iiter.iter != NULL) {
24,456✔
393
      pInfo->iiter.iter = tsdbTbDataIterDestroy(pInfo->iiter.iter);
18✔
394
    }
395

396
    taosArrayDestroy(pInfo->delSkyline);
24,456✔
397
    pInfo->delSkyline = NULL;
24,456✔
398
    pInfo->lastProcKey.ts = ts;
24,456✔
399
    // todo check the nextProcKey info
400
    pInfo->sttKeyInfo.nextProcKey.ts = ts + step;
24,456✔
401
  }
402
}
24,415✔
403

404
void clearBlockScanInfo(STableBlockScanInfo* p) {
3,721,212✔
405
  p->iterInit = false;
3,721,212✔
406
  p->iter.hasVal = false;
3,721,212✔
407
  p->iiter.hasVal = false;
3,721,212✔
408
  p->sttKeyInfo.status = STT_FILE_READER_UNINIT;
3,721,212✔
409

410
  if (p->iter.iter != NULL) {
3,721,212✔
411
    p->iter.iter = tsdbTbDataIterDestroy(p->iter.iter);
1,582,081✔
412
  }
413

414
  if (p->iiter.iter != NULL) {
3,722,111✔
415
    p->iiter.iter = tsdbTbDataIterDestroy(p->iiter.iter);
17,318✔
416
  }
417

418
  taosArrayDestroy(p->delSkyline);
3,723,143✔
419
  p->delSkyline = NULL;
3,721,503✔
420
  taosArrayDestroy(p->pBlockList);
3,721,503✔
421
  p->pBlockList = NULL;
3,721,152✔
422
  taosArrayDestroy(p->pBlockIdxList);
3,721,152✔
423
  p->pBlockIdxList = NULL;
3,720,897✔
424
  taosArrayDestroy(p->pMemDelData);
3,720,897✔
425
  p->pMemDelData = NULL;
3,723,360✔
426
  taosArrayDestroy(p->pFileDelData);
3,723,360✔
427
  p->pFileDelData = NULL;
3,723,019✔
428

429
  clearRowKey(&p->lastProcKey);
3,723,019✔
430
  clearRowKey(&p->sttRange.skey);
3,722,666✔
431
  clearRowKey(&p->sttRange.ekey);
3,722,290✔
432
  clearRowKey(&p->sttKeyInfo.nextProcKey);
3,721,731✔
433
}
3,721,092✔
434

435
void destroyAllBlockScanInfo(SSHashObj* pTableMap) {
1,389,118✔
436
  void*   p = NULL;
1,389,118✔
437
  int32_t iter = 0;
1,389,118✔
438

439
  while ((p = tSimpleHashIterate(pTableMap, p, &iter)) != NULL) {
5,090,399✔
440
    clearBlockScanInfo(*(STableBlockScanInfo**)p);
3,701,307✔
441
  }
442

443
  tSimpleHashCleanup(pTableMap);
1,388,556✔
444
}
1,389,223✔
445

446
static void doCleanupInfoForNextFileset(STableBlockScanInfo* pScanInfo) {
5,509,551✔
447
  // reset the index in last block when handing a new file
448
  taosArrayClear(pScanInfo->pBlockList);
5,509,551✔
449
  taosArrayClear(pScanInfo->pBlockIdxList);
5,510,034✔
450
  taosArrayClear(pScanInfo->pFileDelData);  // del data from each file set
5,509,840✔
451
  pScanInfo->cleanSttBlocks = false;
5,509,815✔
452
  pScanInfo->numOfRowsInStt = 0;
5,509,815✔
453
  pScanInfo->sttBlockReturned = false;
5,509,815✔
454
  INIT_KEYRANGE(&pScanInfo->sttRange);
5,509,815✔
455
  INIT_TIMEWINDOW(&pScanInfo->filesetWindow);
5,509,815✔
456
  pScanInfo->sttKeyInfo.status = STT_FILE_READER_UNINIT;
5,509,815✔
457
}
5,509,815✔
458

459
void cleanupInfoForNextFileset(SSHashObj* pTableMap) {
3,054,241✔
460
  STableBlockScanInfo** p = NULL;
3,054,241✔
461

462
  int32_t iter = 0;
3,054,241✔
463
  while ((p = tSimpleHashIterate(pTableMap, p, &iter)) != NULL) {
8,563,004✔
464
    doCleanupInfoForNextFileset(*p);
5,510,938✔
465
  }
466
}
3,048,773✔
467

468
// brin records iterator
469
void initBrinRecordIter(SBrinRecordIter* pIter, SDataFileReader* pReader, SArray* pList) {
3,053,981✔
470
  (void)memset(&pIter->block, 0, sizeof(SBrinBlock));
3,053,981✔
471
  (void)memset(&pIter->record, 0, sizeof(SBrinRecord));
3,053,981✔
472
  pIter->blockIndex = -1;
3,053,981✔
473
  pIter->recordIndex = -1;
3,053,981✔
474

475
  pIter->pReader = pReader;
3,053,981✔
476
  pIter->pBrinBlockList = pList;
3,053,981✔
477
}
3,053,981✔
478

479
int32_t getNextBrinRecord(SBrinRecordIter* pIter, SBrinRecord** pRecord) {
3,930,559✔
480
  *pRecord = NULL;
3,930,559✔
481

482
  if (pIter->blockIndex == -1 || (pIter->recordIndex + 1) >= pIter->block.numOfRecords) {
3,930,559✔
483
    pIter->blockIndex += 1;
3,168,576✔
484
    if (pIter->blockIndex >= taosArrayGetSize(pIter->pBrinBlockList)) {
3,168,576✔
485
      return TSDB_CODE_SUCCESS;
3,050,865✔
486
    }
487

488
    pIter->pCurrentBlk = taosArrayGet(pIter->pBrinBlockList, pIter->blockIndex);
118,282✔
489
    if (pIter->pCurrentBlk == NULL) {
118,352!
490
      return TSDB_CODE_INVALID_PARA;
×
491
    }
492

493
    tBrinBlockClear(&pIter->block);
118,352✔
494
    int32_t code = tsdbDataFileReadBrinBlock(pIter->pReader, pIter->pCurrentBlk, &pIter->block);
118,485✔
495
    if (code != TSDB_CODE_SUCCESS) {
118,478!
496
      tsdbError("failed to read brinBlock from file, code:%s", tstrerror(code));
×
497
      return code;
×
498
    }
499

500
    pIter->recordIndex = -1;
118,478✔
501
  }
502

503
  pIter->recordIndex += 1;
880,461✔
504
  int32_t code = tBrinBlockGet(&pIter->block, pIter->recordIndex, &pIter->record);
880,461✔
505
  *pRecord = &pIter->record;
880,337✔
506

507
  return code;
880,337✔
508
}
509

510
void clearBrinBlockIter(SBrinRecordIter* pIter) { tBrinBlockDestroy(&pIter->block); }
3,054,111✔
511

512
// initialize the file block access order
513
//  sort the file blocks according to the offset of each data block in the files
514
static void cleanupBlockOrderSupporter(SBlockOrderSupporter* pSup) {
115,085✔
515
  taosMemoryFreeClear(pSup->numOfBlocksPerTable);
115,085!
516
  taosMemoryFreeClear(pSup->indexPerTable);
115,128!
517

518
  for (int32_t i = 0; i < pSup->numOfTables; ++i) {
426,947✔
519
    SBlockOrderWrapper* pBlockInfo = pSup->pDataBlockInfo[i];
311,841✔
520
    taosMemoryFreeClear(pBlockInfo);
311,841✔
521
  }
522

523
  taosMemoryFreeClear(pSup->pDataBlockInfo);
115,106!
524
}
115,107✔
525

526
static int32_t initBlockOrderSupporter(SBlockOrderSupporter* pSup, int32_t numOfTables) {
114,975✔
527
  pSup->pDataBlockInfo = taosMemoryCalloc(1, POINTER_BYTES * numOfTables);
114,975✔
528
  pSup->indexPerTable = taosMemoryCalloc(1, sizeof(int32_t) * numOfTables);
115,084✔
529
  pSup->numOfBlocksPerTable = taosMemoryCalloc(1, sizeof(int32_t) * numOfTables);
115,134✔
530
  pSup->numOfTables = 0;
115,126✔
531
  if (pSup->pDataBlockInfo == NULL || pSup->indexPerTable == NULL || pSup->numOfBlocksPerTable == NULL) {
115,126!
532
    cleanupBlockOrderSupporter(pSup);
2✔
533
    return terrno;
×
534
  }
535

536
  return TSDB_CODE_SUCCESS;
115,124✔
537
}
538

539
static int32_t fileDataBlockOrderCompar(const void* pLeft, const void* pRight, void* param) {
1,178,205✔
540
  int32_t leftIndex = *(int32_t*)pLeft;
1,178,205✔
541
  int32_t rightIndex = *(int32_t*)pRight;
1,178,205✔
542

543
  SBlockOrderSupporter* pSupporter = (SBlockOrderSupporter*)param;
1,178,205✔
544

545
  int32_t leftTableBlockIndex = pSupporter->indexPerTable[leftIndex];
1,178,205✔
546
  int32_t rightTableBlockIndex = pSupporter->indexPerTable[rightIndex];
1,178,205✔
547

548
  if (leftTableBlockIndex > pSupporter->numOfBlocksPerTable[leftIndex]) {
1,178,205✔
549
    /* left block is empty */
550
    return 1;
477,850✔
551
  } else if (rightTableBlockIndex > pSupporter->numOfBlocksPerTable[rightIndex]) {
700,355✔
552
    /* right block is empty */
553
    return -1;
196,589✔
554
  }
555

556
  SBlockOrderWrapper* pLeftBlock = &pSupporter->pDataBlockInfo[leftIndex][leftTableBlockIndex];
503,766✔
557
  SBlockOrderWrapper* pRightBlock = &pSupporter->pDataBlockInfo[rightIndex][rightTableBlockIndex];
503,766✔
558

559
  return pLeftBlock->offset > pRightBlock->offset ? 1 : -1;
503,766✔
560
}
561

562
int32_t recordToBlockInfo(SFileDataBlockInfo* pBlockInfo, SBrinRecord* record) {
400,502✔
563
  pBlockInfo->uid = record->uid;
400,502✔
564
  pBlockInfo->firstKey = record->firstKey.key.ts;
400,502✔
565
  pBlockInfo->lastKey = record->lastKey.key.ts;
400,502✔
566
  pBlockInfo->minVer = record->minVer;
400,502✔
567
  pBlockInfo->maxVer = record->maxVer;
400,502✔
568
  pBlockInfo->blockOffset = record->blockOffset;
400,502✔
569
  pBlockInfo->smaOffset = record->smaOffset;
400,502✔
570
  pBlockInfo->blockSize = record->blockSize;
400,502✔
571
  pBlockInfo->blockKeySize = record->blockKeySize;
400,502✔
572
  pBlockInfo->smaSize = record->smaSize;
400,502✔
573
  pBlockInfo->numRow = record->numRow;
400,502✔
574
  pBlockInfo->count = record->count;
400,502✔
575

576
  SRowKey* pFirstKey = &record->firstKey.key;
400,502✔
577
  if (pFirstKey->numOfPKs > 0) {
400,502!
UNCOV
578
    if (IS_NUMERIC_TYPE(pFirstKey->pks[0].type)) {
×
UNCOV
579
      pBlockInfo->firstPk.val = pFirstKey->pks[0].val;
×
UNCOV
580
      pBlockInfo->lastPk.val = record->lastKey.key.pks[0].val;
×
581
    } else {
582
      char* p = taosMemoryCalloc(1, pFirstKey->pks[0].nData + VARSTR_HEADER_SIZE);
×
583
      if (p == NULL) {
×
584
        return terrno;
×
585
      }
586
      memcpy(varDataVal(p), pFirstKey->pks[0].pData, pFirstKey->pks[0].nData);
×
587
      varDataSetLen(p, pFirstKey->pks[0].nData);
×
588
      pBlockInfo->firstPk.pData = (uint8_t*)p;
×
589

590
      int32_t keyLen = record->lastKey.key.pks[0].nData;
×
591
      p = taosMemoryCalloc(1, keyLen + VARSTR_HEADER_SIZE);
×
592
      if (p == NULL) {
×
593
        return terrno;
×
594
      }
595
      memcpy(varDataVal(p), record->lastKey.key.pks[0].pData, keyLen);
×
596
      varDataSetLen(p, keyLen);
×
597
      pBlockInfo->lastPk.pData = (uint8_t*)p;
×
598
    }
599
  }
600
  return TSDB_CODE_SUCCESS;
400,502✔
601
}
602

603
static void freePkItem(void* pItem) {
×
604
  SFileDataBlockInfo* p = pItem;
×
605
  taosMemoryFreeClear(p->firstPk.pData);
×
606
  taosMemoryFreeClear(p->lastPk.pData);
×
607
}
×
608

609
void clearDataBlockIterator(SDataBlockIter* pIter, bool needFree) {
3,186,775✔
610
  pIter->index = -1;
3,186,775✔
611
  pIter->numOfBlocks = 0;
3,186,775✔
612

613
  if (needFree) {
3,186,775✔
614
    taosArrayClearEx(pIter->blockList, freePkItem);
309,194✔
615
  } else {
616
    taosArrayClear(pIter->blockList);
2,877,581✔
617
  }
618
}
3,187,705✔
619

620
void cleanupDataBlockIterator(SDataBlockIter* pIter, bool needFree) {
1,438,984✔
621
  pIter->index = -1;
1,438,984✔
622
  pIter->numOfBlocks = 0;
1,438,984✔
623
  if (needFree) {
1,438,984✔
624
    taosArrayDestroyEx(pIter->blockList, freePkItem);
33,853✔
625
  } else {
626
    taosArrayDestroy(pIter->blockList);
1,405,131✔
627
  }
628
}
1,439,188✔
629

630
int32_t initBlockIterator(STsdbReader* pReader, SDataBlockIter* pBlockIter, int32_t numOfBlocks, SArray* pTableList) {
114,947✔
631
  bool asc = ASCENDING_TRAVERSE(pReader->info.order);
114,947✔
632

633
  SBlockOrderSupporter sup = {0};
114,947✔
634
  clearDataBlockIterator(pBlockIter, shouldFreePkBuf(&pReader->suppInfo));
114,947✔
635

636
  pBlockIter->numOfBlocks = numOfBlocks;
115,017✔
637

638
  // access data blocks according to the offset of each block in asc/desc order.
639
  int32_t numOfTables = taosArrayGetSize(pTableList);
115,017✔
640

641
  int64_t st = taosGetTimestampUs();
115,027✔
642
  int32_t code = initBlockOrderSupporter(&sup, numOfTables);
115,027✔
643
  if (code != TSDB_CODE_SUCCESS) {
115,121!
644
    return code;
×
645
  }
646

647
  int32_t cnt = 0;
115,121✔
648

649
  for (int32_t i = 0; i < numOfTables; ++i) {
426,799✔
650
    STableBlockScanInfo* pTableScanInfo = taosArrayGetP(pTableList, i);
311,675✔
651

652
    size_t num = taosArrayGetSize(pTableScanInfo->pBlockList);
311,596✔
653
    sup.numOfBlocksPerTable[sup.numOfTables] = num;
311,603✔
654

655
    char* buf = taosMemoryMalloc(sizeof(SBlockOrderWrapper) * num);
311,603✔
656
    if (buf == NULL) {
311,684!
657
      cleanupBlockOrderSupporter(&sup);
×
658
      return terrno;
×
659
    }
660

661
    sup.pDataBlockInfo[sup.numOfTables] = (SBlockOrderWrapper*)buf;
311,684✔
662

663
    for (int32_t k = 0; k < num; ++k) {
712,841✔
664
      SFileDataBlockInfo* pBlockInfo = taosArrayGet(pTableScanInfo->pBlockList, k);
401,163✔
665
      if (pBlockInfo == NULL) {
401,157!
666
        return TSDB_CODE_INVALID_PARA;
×
667
      }
668

669
      sup.pDataBlockInfo[sup.numOfTables][k] =
401,157✔
670
          (SBlockOrderWrapper){.uid = pTableScanInfo->uid, .offset = pBlockInfo->blockOffset, .pInfo = pTableScanInfo};
401,157✔
671
      cnt++;
401,157✔
672
    }
673

674
    sup.numOfTables += 1;
311,678✔
675
  }
676

677
  if (numOfBlocks != cnt && sup.numOfTables != numOfTables) {
115,124!
678
    cleanupBlockOrderSupporter(&sup);
×
679
    return TSDB_CODE_INVALID_PARA;
×
680
  }
681

682
  // since there is only one table qualified, blocks are not sorted
683
  if (sup.numOfTables == 1) {
115,124✔
684
    STableBlockScanInfo* pTableScanInfo = taosArrayGetP(pTableList, 0);
57,592✔
685
    for (int32_t i = 0; i < numOfBlocks; ++i) {
120,915✔
686
      STableDataBlockIdx tableDataBlockIdx = {.globalIndex = i};
63,324✔
687
      void*              px = taosArrayPush(pTableScanInfo->pBlockIdxList, &tableDataBlockIdx);
63,324✔
688
      if (px == NULL) {
63,338!
689
        return terrno;
×
690
      }
691
    }
692

693
    void* p = taosArrayAddAll(pBlockIter->blockList, pTableScanInfo->pBlockList);
57,591✔
694
    if (p == NULL) {
57,570!
695
      return TSDB_CODE_OUT_OF_MEMORY;
×
696
    }
697

698
    taosArrayDestroy(pTableScanInfo->pBlockList);
57,570✔
699
    pTableScanInfo->pBlockList = NULL;
57,659✔
700

701
    int64_t et = taosGetTimestampUs();
57,601✔
702
    tsdbDebug("%p create blocks info struct completed for one table, %d blocks not sorted, elapsed time:%.2f ms %s",
57,601✔
703
              pReader, numOfBlocks, (et - st) / 1000.0, pReader->idStr);
704

705
    pBlockIter->index = asc ? 0 : (numOfBlocks - 1);
57,626✔
706
    cleanupBlockOrderSupporter(&sup);
57,626✔
707
    return TSDB_CODE_SUCCESS;
57,664✔
708
  }
709

710
  tsdbDebug("%p create data blocks info struct completed, %d blocks in %d tables %s", pReader, cnt, sup.numOfTables,
57,532✔
711
            pReader->idStr);
712

713
  SMultiwayMergeTreeInfo* pTree = NULL;
57,532✔
714

715
  uint8_t ret = tMergeTreeCreate(&pTree, sup.numOfTables, &sup, fileDataBlockOrderCompar);
57,532✔
716
  if (ret != TSDB_CODE_SUCCESS) {
57,480!
717
    cleanupBlockOrderSupporter(&sup);
×
718
    return TSDB_CODE_OUT_OF_MEMORY;
×
719
  }
720

721
  int32_t numOfTotal = 0;
57,480✔
722
  while (numOfTotal < cnt) {
394,420✔
723
    int32_t pos = tMergeTreeGetChosenIndex(pTree);
336,951✔
724
    int32_t index = sup.indexPerTable[pos]++;
336,951✔
725

726
    SFileDataBlockInfo* pBlockInfo = taosArrayGet(sup.pDataBlockInfo[pos][index].pInfo->pBlockList, index);
336,951✔
727
    if (pBlockInfo == NULL) {
336,960!
728
      return TSDB_CODE_INVALID_PARA;
×
729
    }
730

731
    void* px = taosArrayPush(pBlockIter->blockList, pBlockInfo);
336,960✔
732
    if (px == NULL) {
336,874!
733
      return terrno;
×
734
    }
735

736
    STableBlockScanInfo* pTableScanInfo = sup.pDataBlockInfo[pos][index].pInfo;
336,874✔
737
    STableDataBlockIdx   tableDataBlockIdx = {.globalIndex = numOfTotal};
336,874✔
738

739
    px = taosArrayPush(pTableScanInfo->pBlockIdxList, &tableDataBlockIdx);
336,874✔
740
    if (px == NULL) {
336,637!
741
      return terrno;
×
742
    }
743

744
    // set data block index overflow, in order to disable the offset comparator
745
    if (sup.indexPerTable[pos] >= sup.numOfBlocksPerTable[pos]) {
336,637✔
746
      sup.indexPerTable[pos] = sup.numOfBlocksPerTable[pos] + 1;
254,018✔
747
    }
748

749
    numOfTotal += 1;
336,637✔
750
    code = tMergeTreeAdjust(pTree, tMergeTreeGetAdjustIndex(pTree));
336,637✔
751
    if (TSDB_CODE_SUCCESS != code) {
336,940!
752
      return code;
×
753
    }
754
  }
755

756
  for (int32_t i = 0; i < numOfTables; ++i) {
311,631✔
757
    STableBlockScanInfo* pTableScanInfo = taosArrayGetP(pTableList, i);
254,155✔
758
    taosArrayDestroy(pTableScanInfo->pBlockList);
254,145✔
759
    pTableScanInfo->pBlockList = NULL;
254,162✔
760
  }
761

762
  int64_t et = taosGetTimestampUs();
57,479✔
763
  tsdbDebug("%p %d data blocks access order completed, elapsed time:%.2f ms %s", pReader, numOfBlocks,
57,479✔
764
            (et - st) / 1000.0, pReader->idStr);
765
  cleanupBlockOrderSupporter(&sup);
57,479✔
766
  taosMemoryFree(pTree);
57,478✔
767

768
  pBlockIter->index = asc ? 0 : (numOfBlocks - 1);
57,479✔
769
  return TSDB_CODE_SUCCESS;
57,479✔
770
}
771

772
bool blockIteratorNext(SDataBlockIter* pBlockIter, const char* idStr) {
401,091✔
773
  bool asc = ASCENDING_TRAVERSE(pBlockIter->order);
401,091✔
774

775
  int32_t step = asc ? 1 : -1;
401,091✔
776
  if ((pBlockIter->index >= pBlockIter->numOfBlocks - 1 && asc) || (pBlockIter->index <= 0 && (!asc))) {
401,091✔
777
    return false;
115,019✔
778
  }
779

780
  pBlockIter->index += step;
286,072✔
781
  return true;
286,072✔
782
}
783

784
typedef enum {
785
  BLK_CHECK_CONTINUE = 0x1,
786
  BLK_CHECK_QUIT = 0x2,
787
} ETombBlkCheckEnum;
788

789
static int32_t loadNextStatisticsBlock(SSttFileReader* pSttFileReader, STbStatisBlock* pStatisBlock,
790
                                       const TStatisBlkArray* pStatisBlkArray, int32_t numOfRows, int32_t* i,
791
                                       int32_t* j);
792
static int32_t doCheckTombBlock(STombBlock* pBlock, STsdbReader* pReader, int32_t numOfTables, int32_t* j,
30,091✔
793
                                ETombBlkCheckEnum* pRet) {
794
  int32_t     code = 0;
30,091✔
795
  STombRecord record = {0};
30,091✔
796

797
  uint64_t             uid = pReader->status.uidList.tableUidList[*j];
30,091✔
798
  STableBlockScanInfo* pScanInfo = NULL;
30,091✔
799

800
  code = getTableBlockScanInfo(pReader->status.pTableMap, uid, &pScanInfo, pReader->idStr);
30,091✔
801
  if (code != TSDB_CODE_SUCCESS) {
30,091!
802
    return code;
×
803
  }
804

805
  if (pScanInfo->pFileDelData == NULL) {
30,091✔
806
    pScanInfo->pFileDelData = taosArrayInit(4, sizeof(SDelData));
12,882✔
807
    if (pScanInfo->pFileDelData == NULL) {
12,882!
808
      return terrno;
×
809
    }
810
  }
811

812
  for (int32_t k = 0; k < pBlock->numOfRecords; ++k) {
3,005,371✔
813
    code = tTombBlockGet(pBlock, k, &record);
2,988,080✔
814
    if (code != TSDB_CODE_SUCCESS) {
2,988,011!
815
      *pRet = BLK_CHECK_QUIT;
×
816
      return code;
×
817
    }
818

819
    if (record.suid < pReader->info.suid) {
2,988,011!
UNCOV
820
      continue;
×
821
    }
822

823
    if (record.suid > pReader->info.suid) {
2,988,011✔
824
      *pRet = BLK_CHECK_QUIT;
6,050✔
825
      return TSDB_CODE_SUCCESS;
6,050✔
826
    }
827

828
    if (uid < record.uid) {
2,981,961✔
829
      while ((*j) < numOfTables && pReader->status.uidList.tableUidList[*j] < record.uid) {
40,732✔
830
        (*j) += 1;
21,418✔
831
      }
832

833
      if ((*j) >= numOfTables) {
19,314✔
834
        *pRet = BLK_CHECK_QUIT;
6,778✔
835
        return TSDB_CODE_SUCCESS;
6,778✔
836
      }
837

838
      uid = pReader->status.uidList.tableUidList[*j];
12,536✔
839
      code = getTableBlockScanInfo(pReader->status.pTableMap, uid, &pScanInfo, pReader->idStr);
12,536✔
840
      if (code != TSDB_CODE_SUCCESS) {
12,536!
841
        return code;
×
842
      }
843

844
      if (pScanInfo->pFileDelData == NULL) {
12,536✔
845
        pScanInfo->pFileDelData = taosArrayInit(4, sizeof(SDelData));
6,817✔
846
        if (pScanInfo->pFileDelData == NULL) {
6,817!
847
          return terrno;
×
848
        }
849
      }
850
    }
851

852
    if (record.uid < uid) {
2,975,272✔
853
      continue;
2,602,151✔
854
    }
855

856
    if (!(record.suid == pReader->info.suid && uid == record.uid)) {
373,121!
857
      tsdbError("tsdb reader failed at: %s:%d", __func__, __LINE__);
×
858
      return TSDB_CODE_INTERNAL_ERROR;
×
859
    }
860

861
    if (record.version <= pReader->info.verRange.maxVer) {
373,121!
862
      SDelData delData = {.version = record.version, .sKey = record.skey, .eKey = record.ekey};
373,121✔
863
      void*    px = taosArrayPush(pScanInfo->pFileDelData, &delData);
373,121✔
864
      if (px == NULL) {
373,129!
865
        return terrno;
×
866
      }
867
    }
868
  }
869

870
  *pRet = BLK_CHECK_CONTINUE;
17,291✔
871
  return TSDB_CODE_SUCCESS;
17,291✔
872
}
873

874
// load tomb data API
875
static int32_t doLoadTombDataFromTombBlk(const TTombBlkArray* pTombBlkArray, STsdbReader* pReader, void* pFileReader,
3,004,268✔
876
                                         bool isFile) {
877
  int32_t        code = 0;
3,004,268✔
878
  STableUidList* pList = &pReader->status.uidList;
3,004,268✔
879
  int32_t        numOfTables = tSimpleHashGetSize(pReader->status.pTableMap);
3,004,268✔
880

881
  int32_t i = 0, j = 0;
3,005,280✔
882
  while (i < pTombBlkArray->size && j < numOfTables) {
3,031,327!
883
    STombBlk* pTombBlk = &pTombBlkArray->data[i];
44,457✔
884
    if (pTombBlk->maxTbid.suid < pReader->info.suid) {
44,457✔
885
      i += 1;
4,835✔
886
      continue;
8,784✔
887
    }
888

889
    if (pTombBlk->minTbid.suid > pReader->info.suid) {
39,622✔
890
      break;
5,582✔
891
    }
892

893
    if (!(pTombBlk->minTbid.suid <= pReader->info.suid && pTombBlk->maxTbid.suid >= pReader->info.suid)) {
35,414!
894
      tsdbError("tsdb reader failed at: %s:%d", __func__, __LINE__);
×
895
      return TSDB_CODE_INTERNAL_ERROR;
12,828✔
896
    }
897
    if (pTombBlk->maxTbid.suid == pReader->info.suid && pTombBlk->maxTbid.uid < pList->tableUidList[0]) {
35,414✔
898
      i += 1;
3,949✔
899
      continue;
3,949✔
900
    }
901

902
    if (pTombBlk->minTbid.suid == pReader->info.suid && pTombBlk->minTbid.uid > pList->tableUidList[numOfTables - 1]) {
31,465!
903
      break;
1,374✔
904
    }
905

906
    STombBlock block = {0};
30,091✔
907
    code = isFile ? tsdbDataFileReadTombBlock(pFileReader, &pTombBlkArray->data[i], &block)
18,260✔
908
                  : tsdbSttFileReadTombBlock(pFileReader, &pTombBlkArray->data[i], &block);
30,091✔
909
    if (code != TSDB_CODE_SUCCESS) {
30,091!
910
      return code;
×
911
    }
912

913
    //    uint64_t uid = pReader->status.uidList.tableUidList[j];
914

915
    //    STableBlockScanInfo* pScanInfo = getTableBlockScanInfo(pReader->status.pTableMap, uid, pReader->idStr);
916
    //    if (pScanInfo->pFileDelData == NULL) {
917
    //      pScanInfo->pFileDelData = taosArrayInit(4, sizeof(SDelData));
918
    //    }
919

920
    ETombBlkCheckEnum ret = 0;
30,091✔
921
    code = doCheckTombBlock(&block, pReader, numOfTables, &j, &ret);
30,091✔
922

923
    tTombBlockDestroy(&block);
30,091✔
924
    if (code != TSDB_CODE_SUCCESS || ret == BLK_CHECK_QUIT) {
30,091!
925
      return code;
12,828✔
926
    }
927

928
    i += 1;
17,263✔
929
  }
930

931
  return TSDB_CODE_SUCCESS;
2,992,452✔
932
}
933

934
int32_t loadDataFileTombDataForAll(STsdbReader* pReader) {
3,350,982✔
935
  if (pReader->status.pCurrentFileset == NULL || pReader->status.pCurrentFileset->farr[3] == NULL) {
3,350,982!
936
    return TSDB_CODE_SUCCESS;
3,330,009✔
937
  }
938

939
  const TTombBlkArray* pBlkArray = NULL;
20,973✔
940

941
  int32_t code = tsdbDataFileReadTombBlk(pReader->pFileReader, &pBlkArray);
20,973✔
942
  if (code != TSDB_CODE_SUCCESS) {
20,973!
943
    return code;
×
944
  }
945

946
  return doLoadTombDataFromTombBlk(pBlkArray, pReader, pReader->pFileReader, true);
20,973✔
947
}
948

949
int32_t loadSttTombDataForAll(STsdbReader* pReader, SSttFileReader* pSttFileReader, SSttBlockLoadInfo* pLoadInfo) {
2,984,614✔
950
  const TTombBlkArray* pBlkArray = NULL;
2,984,614✔
951
  int32_t              code = tsdbSttFileReadTombBlk(pSttFileReader, &pBlkArray);
2,984,614✔
952
  if (code != TSDB_CODE_SUCCESS) {
2,984,361!
953
    return code;
×
954
  }
955

956
  return doLoadTombDataFromTombBlk(pBlkArray, pReader, pSttFileReader, false);
2,984,361✔
957
}
958

959
int32_t loadMemTombData(SArray** ppMemDelData, STbData* pMemTbData, STbData* piMemTbData, int64_t ver) {
3,648,086✔
960
  if (*ppMemDelData == NULL) {
3,648,086✔
961
    *ppMemDelData = taosArrayInit(4, sizeof(SDelData));
3,644,015✔
962
    if (*ppMemDelData == NULL) {
3,646,559!
963
      return terrno;
×
964
    }
965
  }
966

967
  SArray* pMemDelData = *ppMemDelData;
3,650,630✔
968

969
  SDelData* p = NULL;
3,650,630✔
970
  if (pMemTbData != NULL) {
3,650,630✔
971
    taosRLockLatch(&pMemTbData->lock);
1,586,746✔
972
    p = pMemTbData->pHead;
1,587,386✔
973
    while (p) {
1,837,861✔
974
      if (p->version <= ver) {
250,478✔
975
        void* px = taosArrayPush(pMemDelData, p);
248,215✔
976
        if (px == NULL) {
248,215!
977
          taosRUnLockLatch(&pMemTbData->lock);
×
978
          return terrno;
×
979
        }
980
      }
981

982
      p = p->pNext;
250,475✔
983
    }
984
    taosRUnLockLatch(&pMemTbData->lock);
1,587,383✔
985
  }
986

987
  if (piMemTbData != NULL) {
3,651,304✔
988
    p = piMemTbData->pHead;
18,842✔
989
    while (p) {
25,572✔
990
      if (p->version <= ver) {
6,730!
991
        void* px = taosArrayPush(pMemDelData, p);
6,730✔
992
        if (px == NULL) {
6,730!
993
          return terrno;
×
994
        }
995
      }
996
      p = p->pNext;
6,730✔
997
    }
998
  }
999

1000
  return TSDB_CODE_SUCCESS;
3,651,304✔
1001
}
1002

1003
int32_t getNumOfRowsInSttBlock(SSttFileReader* pSttFileReader, SSttBlockLoadInfo* pBlockLoadInfo,
4✔
1004
                               TStatisBlkArray* pStatisBlkArray, uint64_t suid, const uint64_t* pUidList,
1005
                               int32_t numOfTables, int32_t* pNumOfRows) {
1006
  int32_t num = 0;
4✔
1007
  int32_t code = 0;
4✔
1008
  int32_t lino = 0;
4✔
1009

1010
  if (pNumOfRows != 0) {
4!
1011
    *pNumOfRows = 0;
4✔
1012
  }
1013

1014
  if (TARRAY2_SIZE(pStatisBlkArray) <= 0) {
4!
1015
    return code;
×
1016
  }
1017

1018
  int32_t i = 0;
4✔
1019
  while ((i < TARRAY2_SIZE(pStatisBlkArray)) && (pStatisBlkArray->data[i].maxTbid.suid < suid)) {
4!
1020
    ++i;
×
1021
  }
1022

1023
  if (i >= TARRAY2_SIZE(pStatisBlkArray)) {
4!
1024
    return code;
×
1025
  }
1026

1027
  SStatisBlk*     p = &pStatisBlkArray->data[i];
4✔
1028
  STbStatisBlock* pStatisBlock = taosMemoryCalloc(1, sizeof(STbStatisBlock));
4✔
1029
  TSDB_CHECK_NULL(pStatisBlock, code, lino, _err, terrno);
4!
1030

1031
  code = tStatisBlockInit(pStatisBlock);
4✔
1032
  TSDB_CHECK_CODE(code, lino, _err);
4!
1033

1034
  int64_t st = taosGetTimestampMs();
4✔
1035
  code = tsdbSttFileReadStatisBlock(pSttFileReader, p, pStatisBlock);
4✔
1036
  TSDB_CHECK_CODE(code, lino, _err);
4!
1037

1038
  double el = (taosGetTimestampMs() - st) / 1000.0;
4✔
1039
  pBlockLoadInfo->cost.loadStatisBlocks += 1;
4✔
1040
  pBlockLoadInfo->cost.statisElapsedTime += el;
4✔
1041

1042
  int32_t index = 0;
4✔
1043
  while (index < pStatisBlock->numOfRecords && ((int64_t*)pStatisBlock->suids.data)[index] < suid) {
4!
UNCOV
1044
    ++index;
×
1045
  }
1046

1047
  if (index >= pStatisBlock->numOfRecords) {
4!
1048
    tStatisBlockDestroy(pStatisBlock);
×
1049
    taosMemoryFreeClear(pStatisBlock);
×
1050
    *pNumOfRows = num;
×
1051
    return code;
×
1052
  }
1053

1054
  int32_t j = index;
4✔
1055
  int32_t uidIndex = 0;
4✔
1056
  while (i < TARRAY2_SIZE(pStatisBlkArray) && uidIndex < numOfTables) {
7!
1057
    p = &pStatisBlkArray->data[i];
4✔
1058
    if (p->minTbid.suid > suid) {
4✔
1059
      tStatisBlockDestroy(pStatisBlock);
1✔
1060
      taosMemoryFreeClear(pStatisBlock);
1!
1061
      *pNumOfRows = num;
1✔
1062
      return code;
1✔
1063
    }
1064

1065
    uint64_t uid = pUidList[uidIndex];
3✔
1066

1067
    if (((int64_t*)pStatisBlock->uids.data)[j] == uid) {
3!
1068
      num += ((int64_t*)pStatisBlock->counts.data)[j];
3✔
1069
      uidIndex += 1;
3✔
1070
      j += 1;
3✔
1071
      code = loadNextStatisticsBlock(pSttFileReader, pStatisBlock, pStatisBlkArray, pStatisBlock->numOfRecords, &i, &j);
3✔
1072
      TSDB_CHECK_CODE(code, lino, _err);
3!
UNCOV
1073
    } else if (((int64_t*)pStatisBlock->uids.data)[j] < uid) {
×
UNCOV
1074
      j += 1;
×
UNCOV
1075
      code = loadNextStatisticsBlock(pSttFileReader, pStatisBlock, pStatisBlkArray, pStatisBlock->numOfRecords, &i, &j);
×
UNCOV
1076
      TSDB_CHECK_CODE(code, lino, _err);
×
1077
    } else {
1078
      uidIndex += 1;
×
1079
    }
1080
  }
1081

1082
  tStatisBlockDestroy(pStatisBlock);
3✔
1083
  taosMemoryFreeClear(pStatisBlock);
3!
1084
  *pNumOfRows = num;
3✔
1085
  return code;
3✔
1086

1087
_err:
×
1088
  tsdbError("%p failed to get number of rows in stt block, %s at line:%d code:%s", pSttFileReader, __func__, lino,
×
1089
            tstrerror(code));
1090
  return code;
×
1091
}
1092

1093
// load next stt statistics block
1094
static int32_t loadNextStatisticsBlock(SSttFileReader* pSttFileReader, STbStatisBlock* pStatisBlock,
3✔
1095
                                       const TStatisBlkArray* pStatisBlkArray, int32_t numOfRows, int32_t* i,
1096
                                       int32_t* j) {
1097
  if ((*j) >= numOfRows) {
3!
1098
    (*i) += 1;
3✔
1099
    (*j) = 0;
3✔
1100
    if ((*i) < TARRAY2_SIZE(pStatisBlkArray)) {
3!
1101
      int32_t code = tsdbSttFileReadStatisBlock(pSttFileReader, &pStatisBlkArray->data[(*i)], pStatisBlock);
×
1102
      if (code != 0) {
×
1103
        tsdbError("%p failed to read statisBlock, code:%s", pSttFileReader, tstrerror(code));
×
1104
        return code;
×
1105
      }
1106
    }
1107
  }
1108

1109
  return 0;
3✔
1110
}
1111

1112
int32_t doAdjustValidDataIters(SArray* pLDIterList, int32_t numOfFileObj) {
5,342,906✔
1113
  int32_t size = taosArrayGetSize(pLDIterList);
5,342,906✔
1114

1115
  if (size < numOfFileObj) {
5,341,900✔
1116
    int32_t inc = numOfFileObj - size;
2,986,360✔
1117
    for (int32_t k = 0; k < inc; ++k) {
5,976,862✔
1118
      SLDataIter* pIter = taosMemoryCalloc(1, sizeof(SLDataIter));
2,986,097✔
1119
      if (!pIter) {
2,990,713!
1120
        return terrno;
×
1121
      }
1122
      void* px = taosArrayPush(pLDIterList, &pIter);
2,990,502✔
1123
      if (px == NULL) {
2,990,502!
1124
        taosMemoryFree(pIter);
×
1125
        return terrno;
×
1126
      }
1127
    }
1128
  } else if (size > numOfFileObj) {  // remove unused LDataIter
2,355,540!
1129
    int32_t inc = size - numOfFileObj;
×
1130

1131
    for (int i = 0; i < inc; ++i) {
×
1132
      SLDataIter* pIter = taosArrayPop(pLDIterList);
×
1133
      destroyLDataIter(pIter);
×
1134
    }
1135
  }
1136

1137
  return TSDB_CODE_SUCCESS;
5,344,686✔
1138
}
1139

1140
int32_t adjustSttDataIters(SArray* pSttFileBlockIterArray, STFileSet* pFileSet) {
5,183,960✔
1141
  int32_t numOfLevels = pFileSet->lvlArr->size;
5,183,960✔
1142
  int32_t code = 0;
5,183,960✔
1143

1144
  // add the list/iter placeholder
1145
  while (taosArrayGetSize(pSttFileBlockIterArray) < numOfLevels) {
8,171,790✔
1146
    SArray* pList = taosArrayInit(4, POINTER_BYTES);
2,978,983✔
1147
    if (pList == NULL) {
2,990,482!
1148
      return terrno;
×
1149
    }
1150
    void* px = taosArrayPush(pSttFileBlockIterArray, &pList);
2,987,830✔
1151
    if (px == NULL) {
2,987,830!
1152
      return terrno;
×
1153
    }
1154
  }
1155

1156
  for (int32_t j = 0; j < numOfLevels; ++j) {
10,529,531✔
1157
    SSttLvl* pSttLevel = pFileSet->lvlArr->data[j];
5,345,761✔
1158
    SArray*  pList = taosArrayGetP(pSttFileBlockIterArray, j);
5,345,761✔
1159
    code = doAdjustValidDataIters(pList, TARRAY2_SIZE(pSttLevel->fobjArr));
5,343,263✔
1160
    if (code != TSDB_CODE_SUCCESS) {
5,345,420!
1161
      return code;
×
1162
    }
1163
  }
1164

1165
  return TSDB_CODE_SUCCESS;
5,183,770✔
1166
}
1167

1168
int32_t tsdbGetRowsInSttFiles(STFileSet* pFileSet, SArray* pSttFileBlockIterArray, STsdb* pTsdb, SMergeTreeConf* pConf,
4✔
1169
                              const char* pstr) {
1170
  int32_t numOfRows = 0;
4✔
1171
  int32_t code = 0;
4✔
1172

1173
  // no data exists, go to end
1174
  int32_t numOfLevels = pFileSet->lvlArr->size;
4✔
1175
  if (numOfLevels == 0) {
4!
1176
    return numOfRows;
×
1177
  }
1178

1179
  // add the list/iter placeholder
1180
  code = adjustSttDataIters(pSttFileBlockIterArray, pFileSet);
4✔
1181
  if (code != TSDB_CODE_SUCCESS) {
4!
1182
    return numOfRows;
×
1183
  }
1184

1185
  for (int32_t j = 0; j < numOfLevels; ++j) {
8✔
1186
    SSttLvl* pSttLevel = pFileSet->lvlArr->data[j];
4✔
1187
    SArray*  pList = taosArrayGetP(pSttFileBlockIterArray, j);
4✔
1188

1189
    for (int32_t i = 0; i < taosArrayGetSize(pList); ++i) {  // open all last file
8✔
1190
      SLDataIter* pIter = taosArrayGetP(pList, i);
4✔
1191

1192
      // open stt file reader if not opened yet
1193
      // if failed to open this stt file, ignore the error and try next one
1194
      if (pIter->pReader == NULL) {
4!
1195
        SSttFileReaderConfig conf = {.tsdb = pTsdb, .szPage = pTsdb->pVnode->config.tsdbPageSize};
4✔
1196
        conf.file[0] = *pSttLevel->fobjArr->data[i]->f;
4✔
1197

1198
        const char* pName = pSttLevel->fobjArr->data[i]->fname;
4✔
1199
        code = tsdbSttFileReaderOpen(pName, &conf, &pIter->pReader);
4✔
1200
        if (code != TSDB_CODE_SUCCESS) {
4!
1201
          tsdbError("open stt file reader error. file:%s, code %s, %s", pName, tstrerror(code), pstr);
×
1202
          continue;
×
1203
        }
1204
      }
1205

1206
      if (pIter->pBlockLoadInfo == NULL) {
4!
1207
        code = tCreateSttBlockLoadInfo(pConf->pSchema, pConf->pCols, pConf->numOfCols, &pIter->pBlockLoadInfo);
4✔
1208
        if (code != TSDB_CODE_SUCCESS) {
4!
1209
          tsdbError("failed to create block load info, code: out of memory, %s", pstr);
×
1210
          continue;
×
1211
        }
1212
      }
1213

1214
      // load stt blocks statis for all stt-blocks, to decide if the data of queried table exists in current stt file
1215
      TStatisBlkArray* pStatisBlkArray = NULL;
4✔
1216
      code = tsdbSttFileReadStatisBlk(pIter->pReader, (const TStatisBlkArray**)&pStatisBlkArray);
4✔
1217
      if (code != TSDB_CODE_SUCCESS) {
4!
1218
        tsdbError("failed to load stt block statistics, code:%s, %s", tstrerror(code), pstr);
×
1219
        continue;
×
1220
      }
1221

1222
      // extract rows from each stt file one-by-one
1223
      STsdbReader* pReader = pConf->pReader;
4✔
1224
      int32_t      numOfTables = tSimpleHashGetSize(pReader->status.pTableMap);
4✔
1225
      uint64_t*    pUidList = pReader->status.uidList.tableUidList;
4✔
1226
      int32_t      n = 0;
4✔
1227
      code = getNumOfRowsInSttBlock(pIter->pReader, pIter->pBlockLoadInfo, pStatisBlkArray, pConf->suid, pUidList,
4✔
1228
                                    numOfTables, &n);
1229
      numOfRows += n;
4✔
1230
      if (code) {
4!
1231
        tsdbError("%s failed to get rows in stt blocks, code:%s", pstr, tstrerror(code));
×
1232
      }
1233
    }
1234
  }
1235

1236
  return numOfRows;
4✔
1237
}
1238

1239
static bool overlapHelper(const STimeWindow* pLeft, TSKEY minKey, TSKEY maxKey) {
39,222✔
1240
  return (pLeft->ekey >= minKey) && (pLeft->skey <= maxKey);
39,222✔
1241
}
1242

1243
static bool overlapWithTimeWindow(STimeWindow* p1, STimeWindow* pQueryWindow, STableBlockScanInfo* pBlockScanInfo,
90,401✔
1244
                                  int32_t order) {
1245
  // overlap with query window
1246
  if (!(p1->skey >= pQueryWindow->skey && p1->ekey <= pQueryWindow->ekey)) {
90,401✔
1247
    return true;
551✔
1248
  }
1249

1250
  SIterInfo* pMemIter = &pBlockScanInfo->iter;
89,850✔
1251
  SIterInfo* pIMemIter = &pBlockScanInfo->iiter;
89,850✔
1252

1253
  // overlap with mem data
1254
  if (pMemIter->hasVal) {
89,850✔
1255
    STbData* pTbData = pMemIter->iter->pTbData;
32,614✔
1256
    if (overlapHelper(p1, pTbData->minKey, pTbData->maxKey)) {
32,614✔
1257
      return true;
32,481✔
1258
    }
1259
  }
1260

1261
  // overlap with imem data
1262
  if (pIMemIter->hasVal) {
57,378✔
1263
    STbData* pITbData = pIMemIter->iter->pTbData;
1,160✔
1264
    if (overlapHelper(p1, pITbData->minKey, pITbData->maxKey)) {
1,160!
1265
      return true;
1,160✔
1266
    }
1267
  }
1268

1269
  // overlap with data file block
1270
  STimeWindow* pFileWin = &pBlockScanInfo->filesetWindow;
56,218✔
1271
  if ((taosArrayGetSize(pBlockScanInfo->pBlockIdxList) > 0) && overlapHelper(p1, pFileWin->skey, pFileWin->ekey)) {
56,218✔
1272
    return true;
5,451✔
1273
  }
1274

1275
  // overlap with deletion skyline
1276
  SBrinRecord record = {.firstKey = p1->skey, .lastKey = p1->ekey};
50,763✔
1277
  if (overlapWithDelSkylineWithoutVer(pBlockScanInfo, &record, order)) {
50,763✔
1278
    return true;
71✔
1279
  }
1280

1281
  return false;
50,725✔
1282
}
1283

1284
static int32_t sortUidComparFn(const void* p1, const void* p2) {
66,836✔
1285
  const SSttKeyRange* px1 = p1;
66,836✔
1286
  const SSttKeyRange* px2 = p2;
66,836✔
1287

1288
  int32_t ret = tRowKeyCompare(&px1->skey, &px2->skey);
66,836✔
1289
  return ret;
66,906✔
1290
}
1291

1292
bool isCleanSttBlock(SArray* pKeyRangeList, STimeWindow* pQueryWindow, STableBlockScanInfo* pScanInfo, int32_t order) {
161,457✔
1293
  // check if it overlap with del skyline
1294
  taosArraySort(pKeyRangeList, sortUidComparFn);
161,457✔
1295

1296
  int32_t num = taosArrayGetSize(pKeyRangeList);
161,527✔
1297
  if (num == 0) {
161,610✔
1298
    return false;
71,174✔
1299
  }
1300

1301
  SSttKeyRange* pRange = taosArrayGet(pKeyRangeList, 0);
90,436✔
1302
  if (pRange == NULL) {
90,409!
1303
    return false;
×
1304
  }
1305

1306
  STimeWindow w = {.skey = pRange->skey.ts, .ekey = pRange->ekey.ts};
90,409✔
1307
  if (overlapWithTimeWindow(&w, pQueryWindow, pScanInfo, order)) {
90,409✔
1308
    return false;
39,760✔
1309
  }
1310

1311
  for (int32_t i = 0; i < num - 1; ++i) {
50,714✔
1312
    SSttKeyRange* p1 = taosArrayGet(pKeyRangeList, i);
24,934✔
1313
    SSttKeyRange* p2 = taosArrayGet(pKeyRangeList, i + 1);
24,929✔
1314
    if (p1 == NULL || p2 == NULL) {
24,916!
1315
      return false;
24,915✔
1316
    }
1317

1318
    if (p1->ekey.ts >= p2->skey.ts) {
24,916✔
1319
      return false;
24,915✔
1320
    }
1321

1322
    STimeWindow w2 = {.skey = p2->skey.ts, .ekey = p2->ekey.ts};
1✔
1323
    bool        overlap = overlapWithTimeWindow(&w2, pQueryWindow, pScanInfo, order);
1✔
1324
    if (overlap) {
4!
1325
      return false;
×
1326
    }
1327
  }
1328

1329
  return true;
25,780✔
1330
}
1331

1332
static bool doCheckDatablockOverlap(STableBlockScanInfo* pBlockScanInfo, const SBrinRecord* pRecord,
23,845✔
1333
                                    int32_t startIndex) {
1334
  size_t num = taosArrayGetSize(pBlockScanInfo->delSkyline);
23,845✔
1335

1336
  for (int32_t i = startIndex; i < num; i += 1) {
92,592✔
1337
    TSDBKEY* p = taosArrayGet(pBlockScanInfo->delSkyline, i);
92,578✔
1338
    if (p == NULL) {
92,578!
1339
      return false;
×
1340
    }
1341

1342
    if (p->ts >= pRecord->firstKey.key.ts && p->ts <= pRecord->lastKey.key.ts) {
92,578✔
1343
      if (p->version >= pRecord->minVer) {
17,745✔
1344
        return true;
15,853✔
1345
      }
1346
    } else if (p->ts < pRecord->firstKey.key.ts) {  // p->ts < pBlock->minKey.ts
74,833✔
1347
      if (p->version >= pRecord->minVer) {
69,079✔
1348
        if (i < num - 1) {
16,234!
1349
          TSDBKEY* pnext = taosArrayGet(pBlockScanInfo->delSkyline, i + 1);
16,234✔
1350
          if (pnext == NULL) {
16,234!
1351
            return false;
×
1352
          }
1353

1354
          if (pnext->ts >= pRecord->firstKey.key.ts) {
16,234✔
1355
            return true;
2,224✔
1356
          }
1357
        } else {  // it must be the last point
1358
          if (!(p->version == 0)) {
×
1359
            tsdbError("unexpected version:%" PRId64, p->version);
×
1360
          }
1361
        }
1362
      }
1363
    } else {  // (p->ts > pBlock->maxKey.ts) {
1364
      return false;
5,754✔
1365
    }
1366
  }
1367

1368
  return false;
14✔
1369
}
1370

1371
static bool doCheckDatablockOverlapWithoutVersion(STableBlockScanInfo* pBlockScanInfo, const SBrinRecord* pRecord,
71✔
1372
                                                  int32_t startIndex) {
1373
  size_t num = taosArrayGetSize(pBlockScanInfo->delSkyline);
71✔
1374

1375
  for (int32_t i = startIndex; i < num; i += 1) {
71!
1376
    TSDBKEY* p = taosArrayGet(pBlockScanInfo->delSkyline, i);
71✔
1377
    if (p == NULL) {
71!
1378
      return false;
×
1379
    }
1380

1381
    if (p->ts >= pRecord->firstKey.key.ts && p->ts <= pRecord->lastKey.key.ts) {
71!
1382
      return true;
71✔
1383
    } else if (p->ts < pRecord->firstKey.key.ts) {  // p->ts < pBlock->minKey.ts
×
1384
      if (i < num - 1) {
×
1385
        TSDBKEY* pnext = taosArrayGet(pBlockScanInfo->delSkyline, i + 1);
×
1386
        if (pnext == NULL) {
×
1387
          return false;
×
1388
        }
1389

1390
        if (pnext->ts >= pRecord->firstKey.key.ts) {
×
1391
          return true;
×
1392
        }
1393
      }
1394
    } else {  // (p->ts > pBlock->maxKey.ts) {
1395
      return false;
×
1396
    }
1397
  }
1398

1399
  return false;
×
1400
}
1401

1402
bool overlapWithDelSkyline(STableBlockScanInfo* pBlockScanInfo, const SBrinRecord* pRecord, int32_t order) {
425,759✔
1403
  if (pBlockScanInfo->delSkyline == NULL || (taosArrayGetSize(pBlockScanInfo->delSkyline) == 0)) {
425,759✔
1404
    return false;
400,657✔
1405
  }
1406

1407
  // ts is not overlap
1408
  TSDBKEY* pFirst = taosArrayGet(pBlockScanInfo->delSkyline, 0);
25,102✔
1409
  TSDBKEY* pLast = taosArrayGetLast(pBlockScanInfo->delSkyline);
25,220✔
1410
  if (pFirst == NULL || pLast == NULL) {
25,220!
1411
    return false;
×
1412
  }
1413

1414
  if (pRecord->firstKey.key.ts > pLast->ts || pRecord->lastKey.key.ts < pFirst->ts) {
25,220✔
1415
    return false;
1,375✔
1416
  }
1417

1418
  // version is not overlap
1419
  if (ASCENDING_TRAVERSE(order)) {
23,845✔
1420
    return doCheckDatablockOverlap(pBlockScanInfo, pRecord, pBlockScanInfo->fileDelIndex);
18,337✔
1421
  } else {
1422
    int32_t index = pBlockScanInfo->fileDelIndex;
5,508✔
1423
    while (1) {
26,918✔
1424
      TSDBKEY* p = taosArrayGet(pBlockScanInfo->delSkyline, index);
32,426✔
1425
      if (p == NULL) {
32,426!
1426
        return false;
×
1427
      }
1428

1429
      if (p->ts > pRecord->firstKey.key.ts && index > 0) {
32,426✔
1430
        index -= 1;
26,918✔
1431
      } else {  // find the first point that is smaller than the minKey.ts of dataBlock.
1432
        if (p->ts == pRecord->firstKey.key.ts && p->version < pRecord->maxVer && index > 0) {
5,508!
1433
          index -= 1;
24✔
1434
        }
1435
        break;
5,508✔
1436
      }
1437
    }
1438

1439
    return doCheckDatablockOverlap(pBlockScanInfo, pRecord, index);
5,508✔
1440
  }
1441
}
1442

1443
bool overlapWithDelSkylineWithoutVer(STableBlockScanInfo* pBlockScanInfo, const SBrinRecord* pRecord, int32_t order) {
50,770✔
1444
  if (pBlockScanInfo->delSkyline == NULL || (taosArrayGetSize(pBlockScanInfo->delSkyline) == 0)) {
50,770✔
1445
    return false;
50,718✔
1446
  }
1447

1448
  // ts is not overlap
1449
  TSDBKEY* pFirst = taosArrayGet(pBlockScanInfo->delSkyline, 0);
52✔
1450
  TSDBKEY* pLast = taosArrayGetLast(pBlockScanInfo->delSkyline);
71✔
1451
  if (pFirst == NULL || pLast == NULL) {
71!
1452
    return false;
×
1453
  }
1454

1455
  if (pRecord->firstKey.key.ts > pLast->ts || pRecord->lastKey.key.ts < pFirst->ts) {
71!
UNCOV
1456
    return false;
×
1457
  }
1458

1459
  // version is not overlap
1460
  if (ASCENDING_TRAVERSE(order)) {
71!
1461
    return doCheckDatablockOverlapWithoutVersion(pBlockScanInfo, pRecord, pBlockScanInfo->fileDelIndex);
71✔
1462
  } else {
1463
    int32_t index = pBlockScanInfo->fileDelIndex;
×
1464
    while (1) {
×
1465
      TSDBKEY* p = taosArrayGet(pBlockScanInfo->delSkyline, index);
×
1466
      if (p == NULL) {
×
1467
        return false;
×
1468
      }
1469

1470
      if (p->ts > pRecord->firstKey.key.ts && index > 0) {
×
1471
        index -= 1;
×
1472
      } else {  // find the first point that is smaller than the minKey.ts of dataBlock.
1473
        if (p->ts == pRecord->firstKey.key.ts && index > 0) {
×
1474
          index -= 1;
×
1475
        }
1476
        break;
×
1477
      }
1478
    }
1479

1480
    return doCheckDatablockOverlapWithoutVersion(pBlockScanInfo, pRecord, index);
×
1481
  }
1482
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc