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

taosdata / TDengine / #3523

06 Nov 2024 02:29AM UTC coverage: 55.861% (-2.4%) from 58.216%
#3523

push

travis-ci

web-flow
Merge pull request #28551 from taosdata/feat/TS-5215-2

test(blob): testing & fixes for blob

106075 of 245834 branches covered (43.15%)

Branch coverage included in aggregate %.

0 of 15 new or added lines in 2 files covered. (0.0%)

17003 existing lines in 254 files now uncovered.

181910 of 269703 relevant lines covered (67.45%)

1527639.59 hits per line

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

69.18
/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) {
131,355✔
28
  int32_t num = numOfTables / pBuf->numPerBucket;
131,355✔
29
  int32_t remainder = numOfTables % pBuf->numPerBucket;
131,355✔
30

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

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

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

50
  if (remainder > 0) {
131,425✔
51
    char* p = taosMemoryCalloc(remainder, sizeof(STableBlockScanInfo));
117,963✔
52
    if (p == NULL) {
117,985✔
53
      return terrno;
6✔
54
    }
55
    void* px = taosArrayPush(pBuf->pData, &p);
117,979✔
56
    if (px == NULL) {
117,985!
57
      return terrno;
×
58
    }
59
  }
60

61
  pBuf->numOfTables = numOfTables;
131,447✔
62

63
  return TSDB_CODE_SUCCESS;
131,447✔
64
}
65

66
int32_t uidComparFunc(const void* p1, const void* p2) {
1,914,640✔
67
  uint64_t pu1 = *(uint64_t*)p1;
1,914,640✔
68
  uint64_t pu2 = *(uint64_t*)p2;
1,914,640✔
69
  if (pu1 == pu2) {
1,914,640✔
70
    return 0;
78,845✔
71
  } else {
72
    return (pu1 < pu2) ? -1 : 1;
1,835,795✔
73
  }
74
}
75

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

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

87
  int32_t num = (numOfTables - pBuf->numOfTables) / pBuf->numPerBucket;
7✔
88
  int32_t remainder = (numOfTables - pBuf->numOfTables) % pBuf->numPerBucket;
7✔
89
  if (pBuf->pData == NULL) {
7!
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) {
7!
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) {
7!
109
    char* p = taosMemoryCalloc(remainder, sizeof(STableBlockScanInfo));
7✔
110
    if (p == NULL) {
7!
111
      return terrno;
×
112
    }
113
    void* px = taosArrayPush(pBuf->pData, &p);
7✔
114
    if (px == NULL) {
7!
115
      return terrno;
×
116
    }
117
  }
118

119
  pBuf->numOfTables = numOfTables;
7✔
120

121
  return TSDB_CODE_SUCCESS;
7✔
122
}
123

124
void clearBlockScanInfoBuf(SBlockInfoBuf* pBuf) {
131,580✔
125
  size_t num = taosArrayGetSize(pBuf->pData);
131,580✔
126
  for (int32_t i = 0; i < num; ++i) {
249,868✔
127
    char** p = taosArrayGet(pBuf->pData, i);
118,270✔
128
    if (p != NULL) {
118,261!
129
      taosMemoryFree(*p);
118,264✔
130
    }
131
  }
132

133
  taosArrayDestroy(pBuf->pData);
131,598✔
134
}
131,607✔
135

136
int32_t getPosInBlockInfoBuf(SBlockInfoBuf* pBuf, int32_t index, STableBlockScanInfo** pInfo) {
592,236✔
137
  *pInfo = NULL;
592,236✔
138

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

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

149
int32_t getTableBlockScanInfo(SSHashObj* pTableMap, uint64_t uid, STableBlockScanInfo** pInfo, const char* id) {
5,535✔
150
  *pInfo = *(STableBlockScanInfo**)tSimpleHashGet(pTableMap, &uid, sizeof(uid));
5,535✔
151
  if (pInfo == NULL) {
5,535!
152
    int32_t size = tSimpleHashGetSize(pTableMap);
×
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;
5,535✔
158
}
159

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

164
  if (numOfPks > 0) {
2,486,064✔
165
    pKey->pks[0].type = type;
1,308,979✔
166

167
    if (IS_NUMERIC_TYPE(type)) {
1,308,979!
168
      if (asc) {
1,308,964✔
169
        switch (type) {
1,308,914!
170
          case TSDB_DATA_TYPE_BIGINT: {
1,308,642✔
171
            pKey->pks[0].val = INT64_MIN;
1,308,642✔
172
            break;
1,308,642✔
173
          }
174
          case TSDB_DATA_TYPE_INT: {
272✔
175
            int32_t min = INT32_MIN;
272✔
176
            (void)memcpy(&pKey->pks[0].val, &min, tDataTypes[type].bytes);
272✔
177
            break;
272✔
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:
×
190
          case TSDB_DATA_TYPE_USMALLINT:
191
          case TSDB_DATA_TYPE_UINT:
192
          case TSDB_DATA_TYPE_UBIGINT: {
193
            pKey->pks[0].val = 0;
×
194
            break;
×
195
          }
196
          default:
×
197
            return TSDB_CODE_INVALID_PARA;
×
198
        }
199
      } else {
200
        switch (type) {
50!
UNCOV
201
          case TSDB_DATA_TYPE_BIGINT:
×
UNCOV
202
            pKey->pks[0].val = INT64_MAX;
×
UNCOV
203
            break;
×
204
          case TSDB_DATA_TYPE_INT:
50✔
205
            pKey->pks[0].val = INT32_MAX;
50✔
206
            break;
50✔
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:
×
214
            pKey->pks[0].val = UINT64_MAX;
×
215
            break;
×
216
          case TSDB_DATA_TYPE_UINT:
×
217
            pKey->pks[0].val = UINT32_MAX;
×
218
            break;
×
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:
×
226
            return TSDB_CODE_INVALID_PARA;
×
227
        }
228
      }
229
    } else {
230
      pKey->pks[0].pData = taosMemoryCalloc(1, len);
15✔
231
      pKey->pks[0].nData = 0;
15✔
232

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

237
      if (!asc) {
135!
238
        pKey->numOfPKs = 2;
×
239
      }
240
    }
241
  }
242

243
  return TSDB_CODE_SUCCESS;
2,486,184✔
244
}
245

246
void clearRowKey(SRowKey* pKey) {
2,484,579✔
247
  if (pKey == NULL || pKey->numOfPKs == 0 || (!IS_VAR_DATA_TYPE(pKey->pks[0].type))) {
2,484,579!
248
    return;
2,484,617✔
249
  }
250
  taosMemoryFreeClear(pKey->pks[0].pData);
×
251
}
252

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

260
  SRowKey* pRowKey = &pScanInfo->lastProcKey;
592,168✔
261
  if (asc) {
592,168✔
262
    int64_t skey = pReader->info.window.skey;
556,757✔
263
    int64_t ts = (skey > INT64_MIN) ? (skey - 1) : skey;
556,757!
264

265
    code = initRowKey(pRowKey, ts, numOfPks, type, bytes, asc);
556,757✔
266
    if (code != TSDB_CODE_SUCCESS) {
556,696!
267
      return code;
×
268
    }
269

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

278
    code = initRowKey(pRowKey, ts, numOfPks, type, bytes, asc);
35,411✔
279
    if (code != TSDB_CODE_SUCCESS) {
35,459!
280
      return code;
×
281
    }
282

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

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

294
  code = initRowKey(&pScanInfo->sttRange.ekey, INT64_MIN, numOfPks, type, bytes, asc);
592,053✔
295
  return code;
592,074✔
296
}
297

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

304
  pScanInfo->cleanSttBlocks = false;
592,194✔
305
  pScanInfo->sttBlockReturned = false;
592,194✔
306

307
  int32_t code = initLastProcKey(pScanInfo, pReader);
592,194✔
308
  if (code != TSDB_CODE_SUCCESS) {
592,079!
309
    return code;
×
310
  }
311

312
  pScanInfo->sttKeyInfo.status = STT_FILE_READER_UNINIT;
592,079✔
313
  code = tSimpleHashPut(pTableMap, &pScanInfo->uid, sizeof(uint64_t), &pScanInfo, POINTER_BYTES);
592,079✔
314
  if (code != TSDB_CODE_SUCCESS) {
592,254✔
315
    return code;
47✔
316
  }
317

318
  tsdbTrace("%p check table uid:%" PRId64 " from lastKey:%" PRId64 " %s", pReader, pScanInfo->uid,
592,207!
319
            pScanInfo->lastProcKey.ts, pReader->idStr);
320
  return code;
592,207✔
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,
131,284✔
325
                                STableUidList* pUidList, int32_t numOfTables, SSHashObj** pHashObj) {
326
  int32_t code = 0;
131,284✔
327
  *pHashObj = NULL;
131,284✔
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));
131,284✔
332
  if (pTableMap == NULL) {
131,461✔
333
    return terrno;
14✔
334
  }
335

336
  int64_t st = taosGetTimestampUs();
131,393✔
337
  code = initBlockScanInfoBuf(pBuf, numOfTables);
131,393✔
338
  if (code != TSDB_CODE_SUCCESS) {
131,424✔
339
    tSimpleHashCleanup(pTableMap);
15✔
340
    return code;
15✔
341
  }
342

343
  pUidList->tableUidList = taosMemoryMalloc(numOfTables * sizeof(uint64_t));
131,409✔
344
  if (pUidList->tableUidList == NULL) {
131,422✔
345
    tSimpleHashCleanup(pTableMap);
17✔
346
    return terrno;
5✔
347
  }
348

349
  pUidList->currentIndex = 0;
131,405✔
350

351
  for (int32_t j = 0; j < numOfTables; ++j) {
723,255✔
352
    pUidList->tableUidList[j] = idList[j].uid;
591,886✔
353

354
    STableBlockScanInfo* pScanInfo = NULL;
591,886✔
355
    code = getPosInBlockInfoBuf(pBuf, j, &pScanInfo);
591,886✔
356
    if (code != TSDB_CODE_SUCCESS) {
591,864!
357
      break;
47✔
358
    }
359

360
    code = initTableBlockScanInfo(pScanInfo, idList[j].uid, pTableMap, pTsdbReader);
591,864✔
361
    if (code != TSDB_CODE_SUCCESS) {
591,897✔
362
      break;
47✔
363
    }
364
  }
365

366
  taosSort(pUidList->tableUidList, numOfTables, sizeof(uint64_t), uidComparFunc);
131,416✔
367

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

373
  *pHashObj = pTableMap;
131,412✔
374
  return code;
131,412✔
375
}
376

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

381
  while ((p = tSimpleHashIterate(pTableMap, p, &iter)) != NULL) {
845✔
382
    STableBlockScanInfo* pInfo = *(STableBlockScanInfo**)p;
433✔
383

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

388
    if (pInfo->iter.iter != NULL) {
433✔
389
      pInfo->iter.iter = tsdbTbDataIterDestroy(pInfo->iter.iter);
62✔
390
    }
391

392
    if (pInfo->iiter.iter != NULL) {
433!
393
      pInfo->iiter.iter = tsdbTbDataIterDestroy(pInfo->iiter.iter);
×
394
    }
395

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

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

410
  if (p->iter.iter != NULL) {
591,939✔
411
    p->iter.iter = tsdbTbDataIterDestroy(p->iter.iter);
160,747✔
412
  }
413

414
  if (p->iiter.iter != NULL) {
592,060✔
415
    p->iiter.iter = tsdbTbDataIterDestroy(p->iiter.iter);
269✔
416
  }
417

418
  taosArrayDestroy(p->delSkyline);
592,064✔
419
  p->delSkyline = NULL;
591,964✔
420
  taosArrayDestroy(p->pBlockList);
591,964✔
421
  p->pBlockList = NULL;
591,957✔
422
  taosArrayDestroy(p->pBlockIdxList);
591,957✔
423
  p->pBlockIdxList = NULL;
591,940✔
424
  taosArrayDestroy(p->pMemDelData);
591,940✔
425
  p->pMemDelData = NULL;
592,083✔
426
  taosArrayDestroy(p->pFileDelData);
592,083✔
427
  p->pFileDelData = NULL;
592,066✔
428

429
  clearRowKey(&p->lastProcKey);
592,066✔
430
  clearRowKey(&p->sttRange.skey);
592,038✔
431
  clearRowKey(&p->sttRange.ekey);
592,020✔
432
  clearRowKey(&p->sttKeyInfo.nextProcKey);
591,991✔
433
}
591,939✔
434

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

439
  while ((p = tSimpleHashIterate(pTableMap, p, &iter)) != NULL) {
722,965✔
440
    clearBlockScanInfo(*(STableBlockScanInfo**)p);
591,589✔
441
  }
442

443
  tSimpleHashCleanup(pTableMap);
131,206✔
444
}
131,410✔
445

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

459
void cleanupInfoForNextFileset(SSHashObj* pTableMap) {
52,509✔
460
  STableBlockScanInfo** p = NULL;
52,509✔
461

462
  int32_t iter = 0;
52,509✔
463
  while ((p = tSimpleHashIterate(pTableMap, p, &iter)) != NULL) {
389,267✔
464
    doCleanupInfoForNextFileset(*p);
336,769✔
465
  }
466
}
52,471✔
467

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

475
  pIter->pReader = pReader;
52,513✔
476
  pIter->pBrinBlockList = pList;
52,513✔
477
}
52,513✔
478

479
int32_t getNextBrinRecord(SBrinRecordIter* pIter, SBrinRecord** pRecord) {
54,098✔
480
  *pRecord = NULL;
54,098✔
481

482
  if (pIter->blockIndex == -1 || (pIter->recordIndex + 1) >= pIter->block.numOfRecords) {
54,098✔
483
    pIter->blockIndex += 1;
52,815✔
484
    if (pIter->blockIndex >= taosArrayGetSize(pIter->pBrinBlockList)) {
52,815✔
485
      return TSDB_CODE_SUCCESS;
51,987✔
486
    }
487

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

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

500
    pIter->recordIndex = -1;
833✔
501
  }
502

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

507
  return code;
2,116✔
508
}
509

510
void clearBrinBlockIter(SBrinRecordIter* pIter) { tBrinBlockDestroy(&pIter->block); }
52,507✔
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) {
635✔
515
  taosMemoryFreeClear(pSup->numOfBlocksPerTable);
635!
516
  taosMemoryFreeClear(pSup->indexPerTable);
635!
517

518
  for (int32_t i = 0; i < pSup->numOfTables; ++i) {
1,334✔
519
    SBlockOrderWrapper* pBlockInfo = pSup->pDataBlockInfo[i];
699✔
520
    taosMemoryFreeClear(pBlockInfo);
699!
521
  }
522

523
  taosMemoryFreeClear(pSup->pDataBlockInfo);
635!
524
}
635✔
525

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

536
  return TSDB_CODE_SUCCESS;
635✔
537
}
538

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

543
  SBlockOrderSupporter* pSupporter = (SBlockOrderSupporter*)param;
402✔
544

545
  int32_t leftTableBlockIndex = pSupporter->indexPerTable[leftIndex];
402✔
546
  int32_t rightTableBlockIndex = pSupporter->indexPerTable[rightIndex];
402✔
547

548
  if (leftTableBlockIndex > pSupporter->numOfBlocksPerTable[leftIndex]) {
402✔
549
    /* left block is empty */
550
    return 1;
164✔
551
  } else if (rightTableBlockIndex > pSupporter->numOfBlocksPerTable[rightIndex]) {
238✔
552
    /* right block is empty */
553
    return -1;
64✔
554
  }
555

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

559
  return pLeftBlock->offset > pRightBlock->offset ? 1 : -1;
174✔
560
}
561

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

576
  SRowKey* pFirstKey = &record->firstKey.key;
765✔
577
  if (pFirstKey->numOfPKs > 0) {
765!
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;
765✔
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) {
53,388✔
610
  pIter->index = -1;
53,388✔
611
  pIter->numOfBlocks = 0;
53,388✔
612

613
  if (needFree) {
53,388✔
614
    taosArrayClearEx(pIter->blockList, freePkItem);
2✔
615
  } else {
616
    taosArrayClear(pIter->blockList);
53,386✔
617
  }
618
}
53,407✔
619

620
void cleanupDataBlockIterator(SDataBlockIter* pIter, bool needFree) {
131,495✔
621
  pIter->index = -1;
131,495✔
622
  pIter->numOfBlocks = 0;
131,495✔
623
  if (needFree) {
131,495✔
624
    taosArrayDestroyEx(pIter->blockList, freePkItem);
4✔
625
  } else {
626
    taosArrayDestroy(pIter->blockList);
131,491✔
627
  }
628
}
131,575✔
629

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

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

636
  pBlockIter->numOfBlocks = numOfBlocks;
635✔
637

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

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

647
  int32_t cnt = 0;
635✔
648

649
  for (int32_t i = 0; i < numOfTables; ++i) {
1,334✔
650
    STableBlockScanInfo* pTableScanInfo = taosArrayGetP(pTableList, i);
699✔
651

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

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

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

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

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

674
    sup.numOfTables += 1;
699✔
675
  }
676

677
  if (numOfBlocks != cnt && sup.numOfTables != numOfTables) {
635!
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) {
635✔
684
    STableBlockScanInfo* pTableScanInfo = taosArrayGetP(pTableList, 0);
608✔
685
    for (int32_t i = 0; i < numOfBlocks; ++i) {
1,219✔
686
      STableDataBlockIdx tableDataBlockIdx = {.globalIndex = i};
611✔
687
      void*              px = taosArrayPush(pTableScanInfo->pBlockIdxList, &tableDataBlockIdx);
611✔
688
      if (px == NULL) {
611!
689
        return terrno;
×
690
      }
691
    }
692

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

698
    taosArrayDestroy(pTableScanInfo->pBlockList);
608✔
699
    pTableScanInfo->pBlockList = NULL;
608✔
700

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

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

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

713
  SMultiwayMergeTreeInfo* pTree = NULL;
27✔
714

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

721
  int32_t numOfTotal = 0;
27✔
722
  while (numOfTotal < cnt) {
181✔
723
    int32_t pos = tMergeTreeGetChosenIndex(pTree);
154✔
724
    int32_t index = sup.indexPerTable[pos]++;
154✔
725

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

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

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

739
    px = taosArrayPush(pTableScanInfo->pBlockIdxList, &tableDataBlockIdx);
154✔
740
    if (px == NULL) {
154!
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]) {
154✔
746
      sup.indexPerTable[pos] = sup.numOfBlocksPerTable[pos] + 1;
91✔
747
    }
748

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

756
  for (int32_t i = 0; i < numOfTables; ++i) {
118✔
757
    STableBlockScanInfo* pTableScanInfo = taosArrayGetP(pTableList, i);
91✔
758
    taosArrayDestroy(pTableScanInfo->pBlockList);
91✔
759
    pTableScanInfo->pBlockList = NULL;
91✔
760
  }
761

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

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

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

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

780
  pBlockIter->index += step;
128✔
781
  return true;
128✔
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,
1,880✔
793
                                ETombBlkCheckEnum* pRet) {
794
  int32_t     code = 0;
1,880✔
795
  STombRecord record = {0};
1,880✔
796

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

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

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

812
  for (int32_t k = 0; k < pBlock->numOfRecords; ++k) {
7,757✔
813
    code = tTombBlockGet(pBlock, k, &record);
7,066✔
814
    if (code != TSDB_CODE_SUCCESS) {
7,066!
815
      *pRet = BLK_CHECK_QUIT;
×
816
      return code;
×
817
    }
818

819
    if (record.suid < pReader->info.suid) {
7,066✔
820
      continue;
1,266✔
821
    }
822

823
    if (record.suid > pReader->info.suid) {
5,800✔
824
      *pRet = BLK_CHECK_QUIT;
471✔
825
      return TSDB_CODE_SUCCESS;
471✔
826
    }
827

828
    if (uid < record.uid) {
5,329✔
829
      while ((*j) < numOfTables && pReader->status.uidList.tableUidList[*j] < record.uid) {
4,528✔
830
        (*j) += 1;
2,331✔
831
      }
832

833
      if ((*j) >= numOfTables) {
2,197✔
834
        *pRet = BLK_CHECK_QUIT;
718✔
835
        return TSDB_CODE_SUCCESS;
718✔
836
      }
837

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

844
      if (pScanInfo->pFileDelData == NULL) {
1,479✔
845
        pScanInfo->pFileDelData = taosArrayInit(4, sizeof(SDelData));
324✔
846
        if (pScanInfo->pFileDelData == NULL) {
325✔
847
          return terrno;
1✔
848
        }
849
      }
850
    }
851

852
    if (record.uid < uid) {
4,611✔
853
      continue;
980✔
854
    }
855

856
    if (!(record.suid == pReader->info.suid && uid == record.uid)) {
3,631!
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) {
3,631!
862
      SDelData delData = {.version = record.version, .sKey = record.skey, .eKey = record.ekey};
3,631✔
863
      void*    px = taosArrayPush(pScanInfo->pFileDelData, &delData);
3,631✔
864
      if (px == NULL) {
3,631!
865
        return terrno;
×
866
      }
867
    }
868
  }
869

870
  *pRet = BLK_CHECK_CONTINUE;
691✔
871
  return TSDB_CODE_SUCCESS;
691✔
872
}
873

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

881
  int32_t i = 0, j = 0;
49,048✔
882
  while (i < pTombBlkArray->size && j < numOfTables) {
50,975!
883
    STombBlk* pTombBlk = &pTombBlkArray->data[i];
3,874✔
884
    if (pTombBlk->maxTbid.suid < pReader->info.suid) {
3,874✔
885
      i += 1;
1,003✔
886
      continue;
1,236✔
887
    }
888

889
    if (pTombBlk->minTbid.suid > pReader->info.suid) {
2,871✔
890
      break;
757✔
891
    }
892

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

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

906
    STombBlock block = {0};
1,881✔
907
    code = isFile ? tsdbDataFileReadTombBlock(pFileReader, &pTombBlkArray->data[i], &block)
823✔
908
                  : tsdbSttFileReadTombBlock(pFileReader, &pTombBlkArray->data[i], &block);
1,881✔
909
    if (code != TSDB_CODE_SUCCESS) {
1,881✔
910
      return code;
1✔
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;
1,880✔
921
    code = doCheckTombBlock(&block, pReader, numOfTables, &j, &ret);
1,880✔
922

923
    tTombBlockDestroy(&block);
1,880✔
924
    if (code != TSDB_CODE_SUCCESS || ret == BLK_CHECK_QUIT) {
1,880!
925
      return code;
1,189✔
926
    }
927

928
    i += 1;
691✔
929
  }
930

931
  return TSDB_CODE_SUCCESS;
47,858✔
932
}
933

934
int32_t loadDataFileTombDataForAll(STsdbReader* pReader) {
74,202✔
935
  if (pReader->status.pCurrentFileset == NULL || pReader->status.pCurrentFileset->farr[3] == NULL) {
74,202✔
936
    return TSDB_CODE_SUCCESS;
72,893✔
937
  }
938

939
  const TTombBlkArray* pBlkArray = NULL;
1,309✔
940

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

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

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

956
  return doLoadTombDataFromTombBlk(pBlkArray, pReader, pSttFileReader, false);
47,741✔
957
}
958

959
int32_t loadMemTombData(SArray** ppMemDelData, STbData* pMemTbData, STbData* piMemTbData, int64_t ver) {
392,438✔
960
  if (*ppMemDelData == NULL) {
392,438!
961
    *ppMemDelData = taosArrayInit(4, sizeof(SDelData));
392,439✔
962
    if (*ppMemDelData == NULL) {
392,522✔
963
      return terrno;
60✔
964
    }
965
  }
966

967
  SArray* pMemDelData = *ppMemDelData;
392,461✔
968

969
  SDelData* p = NULL;
392,461✔
970
  if (pMemTbData != NULL) {
392,461✔
971
    taosRLockLatch(&pMemTbData->lock);
160,965✔
972
    p = pMemTbData->pHead;
160,970✔
973
    while (p) {
185,627✔
974
      if (p->version <= ver) {
24,657✔
975
        void* px = taosArrayPush(pMemDelData, p);
22,912✔
976
        if (px == NULL) {
22,912!
977
          taosRUnLockLatch(&pMemTbData->lock);
×
978
          return terrno;
×
979
        }
980
      }
981

982
      p = p->pNext;
24,657✔
983
    }
984
    taosRUnLockLatch(&pMemTbData->lock);
160,970✔
985
  }
986

987
  if (piMemTbData != NULL) {
392,509✔
988
    p = piMemTbData->pHead;
273✔
989
    while (p) {
414✔
990
      if (p->version <= ver) {
141!
991
        void* px = taosArrayPush(pMemDelData, p);
141✔
992
        if (px == NULL) {
141!
993
          return terrno;
×
994
        }
995
      }
996
      p = p->pNext;
141✔
997
    }
998
  }
999

1000
  return TSDB_CODE_SUCCESS;
392,509✔
1001
}
1002

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

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

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

1018
  int32_t i = 0;
4,922✔
1019
  while ((i < TARRAY2_SIZE(pStatisBlkArray)) && (pStatisBlkArray->data[i].maxTbid.suid < suid)) {
6,877✔
1020
    ++i;
1,955✔
1021
  }
1022

1023
  if (i >= TARRAY2_SIZE(pStatisBlkArray)) {
4,922✔
1024
    return code;
1,955✔
1025
  }
1026

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

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

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

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

1042
  int32_t index = 0;
2,970✔
1043
  while (index < pStatisBlock->numOfRecords && ((int64_t*)pStatisBlock->suids.data)[index] < suid) {
5,936!
1044
    ++index;
2,966✔
1045
  }
1046

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

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

1065
    uint64_t uid = pUidList[uidIndex];
4,961✔
1066

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

1082
  tStatisBlockDestroy(pStatisBlock);
2,969✔
1083
  taosMemoryFreeClear(pStatisBlock);
2,969!
1084
  *pNumOfRows = num;
2,969✔
1085
  return code;
2,969✔
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,
2,969✔
1095
                                       const TStatisBlkArray* pStatisBlkArray, int32_t numOfRows, int32_t* i,
1096
                                       int32_t* j) {
1097
  if ((*j) >= numOfRows) {
2,969✔
1098
    (*i) += 1;
1,973✔
1099
    (*j) = 0;
1,973✔
1100
    if ((*i) < TARRAY2_SIZE(pStatisBlkArray)) {
1,973!
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;
2,969✔
1110
}
1111

1112
int32_t doAdjustValidDataIters(SArray* pLDIterList, int32_t numOfFileObj) {
207,739✔
1113
  int32_t size = taosArrayGetSize(pLDIterList);
207,739✔
1114

1115
  if (size < numOfFileObj) {
207,741✔
1116
    int32_t inc = numOfFileObj - size;
52,610✔
1117
    for (int32_t k = 0; k < inc; ++k) {
105,347✔
1118
      SLDataIter* pIter = taosMemoryCalloc(1, sizeof(SLDataIter));
52,735✔
1119
      if (!pIter) {
52,738✔
1120
        return terrno;
3✔
1121
      }
1122
      void* px = taosArrayPush(pLDIterList, &pIter);
52,737✔
1123
      if (px == NULL) {
52,737!
1124
        taosMemoryFree(pIter);
×
1125
        return terrno;
×
1126
      }
1127
    }
1128
  } else if (size > numOfFileObj) {  // remove unused LDataIter
155,131!
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;
207,741✔
1138
}
1139

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

1144
  // add the list/iter placeholder
1145
  while (taosArrayGetSize(pSttFileBlockIterArray) < numOfLevels) {
259,622✔
1146
    SArray* pList = taosArrayInit(4, POINTER_BYTES);
52,576✔
1147
    if (pList == NULL) {
52,615✔
1148
      return terrno;
2✔
1149
    }
1150
    void* px = taosArrayPush(pSttFileBlockIterArray, &pList);
52,611✔
1151
    if (px == NULL) {
52,611!
1152
      return terrno;
×
1153
    }
1154
  }
1155

1156
  for (int32_t j = 0; j < numOfLevels; ++j) {
414,769✔
1157
    SSttLvl* pSttLevel = pFileSet->lvlArr->data[j];
207,742✔
1158
    SArray*  pList = taosArrayGetP(pSttFileBlockIterArray, j);
207,742✔
1159
    code = doAdjustValidDataIters(pList, TARRAY2_SIZE(pSttLevel->fobjArr));
207,741✔
1160
    if (code != TSDB_CODE_SUCCESS) {
207,744✔
1161
      return code;
3✔
1162
    }
1163
  }
1164

1165
  return TSDB_CODE_SUCCESS;
207,027✔
1166
}
1167

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

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

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

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

1189
    for (int32_t i = 0; i < taosArrayGetSize(pList); ++i) {  // open all last file
9,849✔
1190
      SLDataIter* pIter = taosArrayGetP(pList, i);
4,920✔
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,924!
1195
        SSttFileReaderConfig conf = {.tsdb = pTsdb, .szPage = pTsdb->pVnode->config.tsdbPageSize};
4,925✔
1196
        conf.file[0] = *pSttLevel->fobjArr->data[i]->f;
4,925✔
1197

1198
        const char* pName = pSttLevel->fobjArr->data[i]->fname;
4,925✔
1199
        code = tsdbSttFileReaderOpen(pName, &conf, &pIter->pReader);
4,925✔
1200
        if (code != TSDB_CODE_SUCCESS) {
4,920!
UNCOV
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,919!
1207
        code = tCreateSttBlockLoadInfo(pConf->pSchema, pConf->pCols, pConf->numOfCols, &pIter->pBlockLoadInfo);
4,920✔
1208
        if (code != TSDB_CODE_SUCCESS) {
4,924!
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,923✔
1216
      code = tsdbSttFileReadStatisBlk(pIter->pReader, (const TStatisBlkArray**)&pStatisBlkArray);
4,923✔
1217
      if (code != TSDB_CODE_SUCCESS) {
4,924!
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,924✔
1224
      int32_t      numOfTables = tSimpleHashGetSize(pReader->status.pTableMap);
4,924✔
1225
      uint64_t*    pUidList = pReader->status.uidList.tableUidList;
4,922✔
1226
      int32_t      n = 0;
4,922✔
1227
      code = getNumOfRowsInSttBlock(pIter->pReader, pIter->pBlockLoadInfo, pStatisBlkArray, pConf->suid, pUidList,
4,922✔
1228
                                    numOfTables, &n);
1229
      numOfRows += n;
4,924✔
1230
      if (code) {
4,924!
1231
        tsdbError("%s failed to get rows in stt blocks, code:%s", pstr, tstrerror(code));
×
1232
      }
1233
    }
1234
  }
1235

1236
  return numOfRows;
4,924✔
1237
}
1238

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

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

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

1253
  // overlap with mem data
1254
  if (pMemIter->hasVal) {
26,910✔
1255
    STbData* pTbData = pMemIter->iter->pTbData;
1,053✔
1256
    if (overlapHelper(p1, pTbData->minKey, pTbData->maxKey)) {
1,053✔
1257
      return true;
658✔
1258
    }
1259
  }
1260

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

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

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

1281
  return false;
26,250✔
1282
}
1283

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

UNCOV
1288
  int32_t ret = tRowKeyCompare(&px1->skey, &px2->skey);
×
UNCOV
1289
  return ret;
×
1290
}
1291

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

1296
  int32_t num = taosArrayGetSize(pKeyRangeList);
83,886✔
1297
  if (num == 0) {
83,888✔
1298
    return false;
56,977✔
1299
  }
1300

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

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

1311
  for (int32_t i = 0; i < num - 1; ++i) {
26,250!
UNCOV
1312
    SSttKeyRange* p1 = taosArrayGet(pKeyRangeList, i);
×
UNCOV
1313
    SSttKeyRange* p2 = taosArrayGet(pKeyRangeList, i + 1);
×
UNCOV
1314
    if (p1 == NULL || p2 == NULL) {
×
1315
      return false;
×
1316
    }
1317

UNCOV
1318
    if (p1->ekey.ts >= p2->skey.ts) {
×
1319
      return false;
×
1320
    }
1321

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

1329
  return true;
26,250✔
1330
}
1331

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

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

1342
    if (p->ts >= pRecord->firstKey.key.ts && p->ts <= pRecord->lastKey.key.ts) {
1,130!
1343
      if (p->version >= pRecord->minVer) {
518!
1344
        return true;
518✔
1345
      }
1346
    } else if (p->ts < pRecord->firstKey.key.ts) {  // p->ts < pBlock->minKey.ts
612!
1347
      if (p->version >= pRecord->minVer) {
612!
1348
        if (i < num - 1) {
612!
1349
          TSDBKEY* pnext = taosArrayGet(pBlockScanInfo->delSkyline, i + 1);
612✔
1350
          if (pnext == NULL) {
612!
1351
            return false;
×
1352
          }
1353

1354
          if (pnext->ts >= pRecord->firstKey.key.ts) {
612!
1355
            return true;
612✔
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;
×
1365
    }
1366
  }
1367

1368
  return false;
×
1369
}
1370

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

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

UNCOV
1381
    if (p->ts >= pRecord->firstKey.key.ts && p->ts <= pRecord->lastKey.key.ts) {
×
UNCOV
1382
      return true;
×
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) {
1,326✔
1403
  if (pBlockScanInfo->delSkyline == NULL || (taosArrayGetSize(pBlockScanInfo->delSkyline) == 0)) {
1,326!
1404
    return false;
194✔
1405
  }
1406

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

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

1418
  // version is not overlap
1419
  if (ASCENDING_TRAVERSE(order)) {
1,130✔
1420
    return doCheckDatablockOverlap(pBlockScanInfo, pRecord, pBlockScanInfo->fileDelIndex);
646✔
1421
  } else {
1422
    int32_t index = pBlockScanInfo->fileDelIndex;
484✔
1423
    while (1) {
456✔
1424
      TSDBKEY* p = taosArrayGet(pBlockScanInfo->delSkyline, index);
940✔
1425
      if (p == NULL) {
940!
1426
        return false;
×
1427
      }
1428

1429
      if (p->ts > pRecord->firstKey.key.ts && index > 0) {
940!
1430
        index -= 1;
456✔
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) {
484!
1433
          index -= 1;
28✔
1434
        }
1435
        break;
484✔
1436
      }
1437
    }
1438

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

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

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

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

1459
  // version is not overlap
UNCOV
1460
  if (ASCENDING_TRAVERSE(order)) {
×
UNCOV
1461
    return doCheckDatablockOverlapWithoutVersion(pBlockScanInfo, pRecord, pBlockScanInfo->fileDelIndex);
×
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