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

taosdata / TDengine / #3525

10 Nov 2024 03:50AM UTC coverage: 60.818% (-0.08%) from 60.898%
#3525

push

travis-ci

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

merge: from main to 3.0 branch

118634 of 249004 branches covered (47.64%)

Branch coverage included in aggregate %.

136 of 169 new or added lines in 23 files covered. (80.47%)

542 existing lines in 129 files now uncovered.

199071 of 273386 relevant lines covered (72.82%)

15691647.46 hits per line

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

74.11
/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) {
5,016,594✔
28
  int32_t num = numOfTables / pBuf->numPerBucket;
5,016,594✔
29
  int32_t remainder = numOfTables % pBuf->numPerBucket;
5,016,594✔
30

31
  if (pBuf->pData == NULL) {
5,016,594!
32
    pBuf->pData = taosArrayInit(num + 1, POINTER_BYTES);
5,023,122✔
33
    if (pBuf->pData == NULL) {
5,033,642!
34
      return terrno;
×
35
    }
36
  }
37

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

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

50
  if (remainder > 0) {
5,032,121✔
51
    char* p = taosMemoryCalloc(remainder, sizeof(STableBlockScanInfo));
4,916,605✔
52
    if (p == NULL) {
4,922,849!
53
      return terrno;
×
54
    }
55
    void* px = taosArrayPush(pBuf->pData, &p);
4,922,849✔
56
    if (px == NULL) {
4,924,386!
57
      return terrno;
×
58
    }
59
  }
60

61
  pBuf->numOfTables = numOfTables;
5,039,902✔
62

63
  return TSDB_CODE_SUCCESS;
5,039,902✔
64
}
65

66
int32_t uidComparFunc(const void* p1, const void* p2) {
11,363,744✔
67
  uint64_t pu1 = *(uint64_t*)p1;
11,363,744✔
68
  uint64_t pu2 = *(uint64_t*)p2;
11,363,744✔
69
  if (pu1 == pu2) {
11,363,744!
70
    return 0;
×
71
  } else {
72
    return (pu1 < pu2) ? -1 : 1;
11,363,744✔
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) {
5,086,875✔
125
  size_t num = taosArrayGetSize(pBuf->pData);
5,086,875✔
126
  for (int32_t i = 0; i < num; ++i) {
10,015,582✔
127
    char** p = taosArrayGet(pBuf->pData, i);
4,928,409✔
128
    if (p != NULL) {
4,928,128!
129
      taosMemoryFree(*p);
4,928,152✔
130
    }
131
  }
132

133
  taosArrayDestroy(pBuf->pData);
5,087,173✔
134
}
5,087,085✔
135

136
int32_t getPosInBlockInfoBuf(SBlockInfoBuf* pBuf, int32_t index, STableBlockScanInfo** pInfo) {
12,912,284✔
137
  *pInfo = NULL;
12,912,284✔
138

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

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

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

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

164
  if (numOfPks > 0) {
56,484,642✔
165
    pKey->pks[0].type = type;
2,105,797✔
166

167
    if (IS_NUMERIC_TYPE(type)) {
2,105,797!
168
      if (asc) {
1,851,847✔
169
        switch (type) {
1,335,381!
170
          case TSDB_DATA_TYPE_BIGINT: {
1,062,206✔
171
            pKey->pks[0].val = INT64_MIN;
1,062,206✔
172
            break;
1,062,206✔
173
          }
174
          case TSDB_DATA_TYPE_INT: {
93,742✔
175
            int32_t min = INT32_MIN;
93,742✔
176
            (void)memcpy(&pKey->pks[0].val, &min, tDataTypes[type].bytes);
93,742✔
177
            break;
93,742✔
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:
179,442✔
190
          case TSDB_DATA_TYPE_USMALLINT:
191
          case TSDB_DATA_TYPE_UINT:
192
          case TSDB_DATA_TYPE_UBIGINT: {
193
            pKey->pks[0].val = 0;
179,442✔
194
            break;
179,442✔
195
          }
UNCOV
196
          default:
×
UNCOV
197
            return TSDB_CODE_INVALID_PARA;
×
198
        }
199
      } else {
200
        switch (type) {
516,466!
201
          case TSDB_DATA_TYPE_BIGINT:
341,497✔
202
            pKey->pks[0].val = INT64_MAX;
341,497✔
203
            break;
341,497✔
204
          case TSDB_DATA_TYPE_INT:
58,738✔
205
            pKey->pks[0].val = INT32_MAX;
58,738✔
206
            break;
58,738✔
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,577✔
214
            pKey->pks[0].val = UINT64_MAX;
57,577✔
215
            break;
57,577✔
216
          case TSDB_DATA_TYPE_UINT:
58,709✔
217
            pKey->pks[0].val = UINT32_MAX;
58,709✔
218
            break;
58,709✔
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);
253,950✔
231
      pKey->pks[0].nData = 0;
254,008✔
232

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

237
      if (!asc) {
271,615✔
238
        pKey->numOfPKs = 2;
113,486✔
239
      }
240
    }
241
  }
242

243
  return TSDB_CODE_SUCCESS;
56,502,371✔
244
}
245

246
void clearRowKey(SRowKey* pKey) {
56,597,363✔
247
  if (pKey == NULL || pKey->numOfPKs == 0 || (!IS_VAR_DATA_TYPE(pKey->pks[0].type))) {
56,597,363!
248
    return;
56,345,508✔
249
  }
250
  taosMemoryFreeClear(pKey->pks[0].pData);
251,855!
251
}
252

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

260
  SRowKey* pRowKey = &pScanInfo->lastProcKey;
12,900,276✔
261
  if (asc) {
12,900,276✔
262
    int64_t skey = pReader->info.window.skey;
10,541,790✔
263
    int64_t ts = (skey > INT64_MIN) ? (skey - 1) : skey;
10,541,790!
264

265
    code = initRowKey(pRowKey, ts, numOfPks, type, bytes, asc);
10,541,790✔
266
    if (code != TSDB_CODE_SUCCESS) {
10,538,194!
267
      return code;
×
268
    }
269

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

278
    code = initRowKey(pRowKey, ts, numOfPks, type, bytes, asc);
2,358,486✔
279
    if (code != TSDB_CODE_SUCCESS) {
2,370,222!
280
      return code;
×
281
    }
282

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

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

294
  code = initRowKey(&pScanInfo->sttRange.ekey, INT64_MIN, numOfPks, type, bytes, asc);
12,895,683✔
295
  return code;
12,899,574✔
296
}
297

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

304
  pScanInfo->cleanSttBlocks = false;
12,902,572✔
305
  pScanInfo->sttBlockReturned = false;
12,902,572✔
306

307
  int32_t code = initLastProcKey(pScanInfo, pReader);
12,902,572✔
308
  if (code != TSDB_CODE_SUCCESS) {
12,900,601!
309
    return code;
×
310
  }
311

312
  pScanInfo->sttKeyInfo.status = STT_FILE_READER_UNINIT;
12,900,601✔
313
  code = tSimpleHashPut(pTableMap, &pScanInfo->uid, sizeof(uint64_t), &pScanInfo, POINTER_BYTES);
12,900,601✔
314
  if (code != TSDB_CODE_SUCCESS) {
12,914,067!
315
    return code;
×
316
  }
317

318
  tsdbTrace("%p check table uid:%" PRId64 " from lastKey:%" PRId64 " %s", pReader, pScanInfo->uid,
12,914,067✔
319
            pScanInfo->lastProcKey.ts, pReader->idStr);
320
  return code;
12,914,685✔
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,
5,018,695✔
325
                                STableUidList* pUidList, int32_t numOfTables, SSHashObj** pHashObj) {
326
  int32_t code = 0;
5,018,695✔
327
  *pHashObj = NULL;
5,018,695✔
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));
5,018,695✔
332
  if (pTableMap == NULL) {
5,034,204!
333
    return terrno;
×
334
  }
335

336
  int64_t st = taosGetTimestampUs();
5,027,566✔
337
  code = initBlockScanInfoBuf(pBuf, numOfTables);
5,027,566✔
338
  if (code != TSDB_CODE_SUCCESS) {
5,031,999!
339
    tSimpleHashCleanup(pTableMap);
×
340
    return code;
×
341
  }
342

343
  pUidList->tableUidList = taosMemoryMalloc(numOfTables * sizeof(uint64_t));
5,031,999✔
344
  if (pUidList->tableUidList == NULL) {
5,033,842✔
345
    tSimpleHashCleanup(pTableMap);
5,029✔
346
    return terrno;
×
347
  }
348

349
  pUidList->currentIndex = 0;
5,028,813✔
350

351
  for (int32_t j = 0; j < numOfTables; ++j) {
17,924,864✔
352
    pUidList->tableUidList[j] = idList[j].uid;
12,896,378✔
353

354
    STableBlockScanInfo* pScanInfo = NULL;
12,896,378✔
355
    code = getPosInBlockInfoBuf(pBuf, j, &pScanInfo);
12,896,378✔
356
    if (code != TSDB_CODE_SUCCESS) {
12,889,474!
357
      break;
×
358
    }
359

360
    code = initTableBlockScanInfo(pScanInfo, idList[j].uid, pTableMap, pTsdbReader);
12,889,474✔
361
    if (code != TSDB_CODE_SUCCESS) {
12,896,051!
362
      break;
×
363
    }
364
  }
365

366
  taosSort(pUidList->tableUidList, numOfTables, sizeof(uint64_t), uidComparFunc);
5,028,486✔
367

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

373
  *pHashObj = pTableMap;
5,034,604✔
374
  return code;
5,034,604✔
375
}
376

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

381
  while ((p = tSimpleHashIterate(pTableMap, p, &iter)) != NULL) {
49,387✔
382
    STableBlockScanInfo* pInfo = *(STableBlockScanInfo**)p;
24,711✔
383

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

388
    if (pInfo->iter.iter != NULL) {
24,711✔
389
      pInfo->iter.iter = tsdbTbDataIterDestroy(pInfo->iter.iter);
4,232✔
390
    }
391

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

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

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

410
  if (p->iter.iter != NULL) {
12,923,717✔
411
    p->iter.iter = tsdbTbDataIterDestroy(p->iter.iter);
4,560,883✔
412
  }
413

414
  if (p->iiter.iter != NULL) {
12,925,574✔
415
    p->iiter.iter = tsdbTbDataIterDestroy(p->iiter.iter);
25,915✔
416
  }
417

418
  taosArrayDestroy(p->delSkyline);
12,926,518✔
419
  p->delSkyline = NULL;
12,923,141✔
420
  taosArrayDestroy(p->pBlockList);
12,923,141✔
421
  p->pBlockList = NULL;
12,923,019✔
422
  taosArrayDestroy(p->pBlockIdxList);
12,923,019✔
423
  p->pBlockIdxList = NULL;
12,922,622✔
424
  taosArrayDestroy(p->pMemDelData);
12,922,622✔
425
  p->pMemDelData = NULL;
12,929,371✔
426
  taosArrayDestroy(p->pFileDelData);
12,929,371✔
427
  p->pFileDelData = NULL;
12,928,620✔
428

429
  clearRowKey(&p->lastProcKey);
12,928,620✔
430
  clearRowKey(&p->sttRange.skey);
12,927,411✔
431
  clearRowKey(&p->sttRange.ekey);
12,926,295✔
432
  clearRowKey(&p->sttKeyInfo.nextProcKey);
12,924,945✔
433
}
12,923,392✔
434

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

439
  while ((p = tSimpleHashIterate(pTableMap, p, &iter)) != NULL) {
17,938,272✔
440
    clearBlockScanInfo(*(STableBlockScanInfo**)p);
12,904,204✔
441
  }
442

443
  tSimpleHashCleanup(pTableMap);
5,030,798✔
444
}
5,035,662✔
445

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

459
void cleanupInfoForNextFileset(SSHashObj* pTableMap) {
7,006,028✔
460
  STableBlockScanInfo** p = NULL;
7,006,028✔
461

462
  int32_t iter = 0;
7,006,028✔
463
  while ((p = tSimpleHashIterate(pTableMap, p, &iter)) != NULL) {
21,732,419✔
464
    doCleanupInfoForNextFileset(*p);
14,728,985✔
465
  }
466
}
7,001,260✔
467

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

475
  pIter->pReader = pReader;
7,008,826✔
476
  pIter->pBrinBlockList = pList;
7,008,826✔
477
}
7,008,826✔
478

479
int32_t getNextBrinRecord(SBrinRecordIter* pIter, SBrinRecord** pRecord) {
9,995,249✔
480
  *pRecord = NULL;
9,995,249✔
481

482
  if (pIter->blockIndex == -1 || (pIter->recordIndex + 1) >= pIter->block.numOfRecords) {
9,995,249✔
483
    pIter->blockIndex += 1;
7,124,509✔
484
    if (pIter->blockIndex >= taosArrayGetSize(pIter->pBrinBlockList)) {
7,124,509✔
485
      return TSDB_CODE_SUCCESS;
6,995,882✔
486
    }
487

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

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

500
    pIter->recordIndex = -1;
129,803✔
501
  }
502

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

507
  return code;
3,000,147✔
508
}
509

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

518
  for (int32_t i = 0; i < pSup->numOfTables; ++i) {
580,337✔
519
    SBlockOrderWrapper* pBlockInfo = pSup->pDataBlockInfo[i];
460,254✔
520
    taosMemoryFreeClear(pBlockInfo);
460,254✔
521
  }
522

523
  taosMemoryFreeClear(pSup->pDataBlockInfo);
120,083!
524
}
120,110✔
525

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

536
  return TSDB_CODE_SUCCESS;
120,157✔
537
}
538

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

543
  SBlockOrderSupporter* pSupporter = (SBlockOrderSupporter*)param;
5,759,257✔
544

545
  int32_t leftTableBlockIndex = pSupporter->indexPerTable[leftIndex];
5,759,257✔
546
  int32_t rightTableBlockIndex = pSupporter->indexPerTable[rightIndex];
5,759,257✔
547

548
  if (leftTableBlockIndex > pSupporter->numOfBlocksPerTable[leftIndex]) {
5,759,257✔
549
    /* left block is empty */
550
    return 1;
1,307,432✔
551
  } else if (rightTableBlockIndex > pSupporter->numOfBlocksPerTable[rightIndex]) {
4,451,825✔
552
    /* right block is empty */
553
    return -1;
340,042✔
554
  }
555

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

559
  return pLeftBlock->offset > pRightBlock->offset ? 1 : -1;
4,111,783✔
560
}
561

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

576
  SRowKey* pFirstKey = &record->firstKey.key;
792,983✔
577
  if (pFirstKey->numOfPKs > 0) {
792,983✔
578
    if (IS_NUMERIC_TYPE(pFirstKey->pks[0].type)) {
375,657!
579
      pBlockInfo->firstPk.val = pFirstKey->pks[0].val;
375,657✔
580
      pBlockInfo->lastPk.val = record->lastKey.key.pks[0].val;
375,657✔
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;
792,983✔
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) {
7,138,967✔
610
  pIter->index = -1;
7,138,967✔
611
  pIter->numOfBlocks = 0;
7,138,967✔
612

613
  if (needFree) {
7,138,967✔
614
    taosArrayClearEx(pIter->blockList, freePkItem);
269,933✔
615
  } else {
616
    taosArrayClear(pIter->blockList);
6,869,034✔
617
  }
618
}
7,142,303✔
619

620
void cleanupDataBlockIterator(SDataBlockIter* pIter, bool needFree) {
5,085,512✔
621
  pIter->index = -1;
5,085,512✔
622
  pIter->numOfBlocks = 0;
5,085,512✔
623
  if (needFree) {
5,085,512✔
624
    taosArrayDestroyEx(pIter->blockList, freePkItem);
34,102✔
625
  } else {
626
    taosArrayDestroy(pIter->blockList);
5,051,410✔
627
  }
628
}
5,086,981✔
629

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

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

636
  pBlockIter->numOfBlocks = numOfBlocks;
120,021✔
637

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

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

647
  int32_t cnt = 0;
120,153✔
648

649
  for (int32_t i = 0; i < numOfTables; ++i) {
580,247✔
650
    STableBlockScanInfo* pTableScanInfo = taosArrayGetP(pTableList, i);
460,019✔
651

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

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

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

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

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

674
    sup.numOfTables += 1;
460,094✔
675
  }
676

677
  if (numOfBlocks != cnt && sup.numOfTables != numOfTables) {
120,228!
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) {
120,228✔
684
    STableBlockScanInfo* pTableScanInfo = taosArrayGetP(pTableList, 0);
62,431✔
685
    for (int32_t i = 0; i < numOfBlocks; ++i) {
129,892✔
686
      STableDataBlockIdx tableDataBlockIdx = {.globalIndex = i};
67,477✔
687
      void*              px = taosArrayPush(pTableScanInfo->pBlockIdxList, &tableDataBlockIdx);
67,477✔
688
      if (px == NULL) {
67,495!
689
        return terrno;
×
690
      }
691
    }
692

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

698
    taosArrayDestroy(pTableScanInfo->pBlockList);
62,408✔
699
    pTableScanInfo->pBlockList = NULL;
62,489✔
700

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

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

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

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

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

721
  int32_t numOfTotal = 0;
57,679✔
722
  while (numOfTotal < cnt) {
783,314✔
723
    int32_t pos = tMergeTreeGetChosenIndex(pTree);
725,644✔
724
    int32_t index = sup.indexPerTable[pos]++;
725,644✔
725

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

819
    if (record.suid < pReader->info.suid) {
4,700,227✔
820
      continue;
10,310✔
821
    }
822

823
    if (record.suid > pReader->info.suid) {
4,689,917✔
824
      *pRet = BLK_CHECK_QUIT;
9,938✔
825
      return TSDB_CODE_SUCCESS;
9,938✔
826
    }
827

828
    if (uid < record.uid) {
4,679,979✔
829
      while ((*j) < numOfTables && pReader->status.uidList.tableUidList[*j] < record.uid) {
126,931✔
830
        (*j) += 1;
64,990✔
831
      }
832

833
      if ((*j) >= numOfTables) {
61,941✔
834
        *pRet = BLK_CHECK_QUIT;
29,048✔
835
        return TSDB_CODE_SUCCESS;
29,048✔
836
      }
837

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

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

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

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

870
  *pRet = BLK_CHECK_CONTINUE;
22,791✔
871
  return TSDB_CODE_SUCCESS;
22,791✔
872
}
873

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

881
  int32_t i = 0, j = 0;
6,970,128✔
882
  while (i < pTombBlkArray->size && j < numOfTables) {
7,009,284!
883
    STombBlk* pTombBlk = &pTombBlkArray->data[i];
94,274✔
884
    if (pTombBlk->maxTbid.suid < pReader->info.suid) {
94,274✔
885
      i += 1;
9,294✔
886
      continue;
16,419✔
887
    }
888

889
    if (pTombBlk->minTbid.suid > pReader->info.suid) {
84,980✔
890
      break;
16,133✔
891
    }
892

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

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

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

923
    tTombBlockDestroy(&block);
61,722✔
924
    if (code != TSDB_CODE_SUCCESS || ret == BLK_CHECK_QUIT) {
61,724!
925
      return code;
38,987✔
926
    }
927

928
    i += 1;
22,737✔
929
  }
930

931
  return TSDB_CODE_SUCCESS;
6,931,143✔
932
}
933

934
int32_t loadDataFileTombDataForAll(STsdbReader* pReader) {
9,313,483✔
935
  if (pReader->status.pCurrentFileset == NULL || pReader->status.pCurrentFileset->farr[3] == NULL) {
9,313,483!
936
    return TSDB_CODE_SUCCESS;
9,280,063✔
937
  }
938

939
  const TTombBlkArray* pBlkArray = NULL;
33,420✔
940

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

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

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

956
  return doLoadTombDataFromTombBlk(pBlkArray, pReader, pSttFileReader, false);
6,936,896✔
957
}
958

959
int32_t loadMemTombData(SArray** ppMemDelData, STbData* pMemTbData, STbData* piMemTbData, int64_t ver) {
12,821,647✔
960
  if (*ppMemDelData == NULL) {
12,821,647✔
961
    *ppMemDelData = taosArrayInit(4, sizeof(SDelData));
12,817,892✔
962
    if (*ppMemDelData == NULL) {
12,820,761!
963
      return terrno;
×
964
    }
965
  }
966

967
  SArray* pMemDelData = *ppMemDelData;
12,824,516✔
968

969
  SDelData* p = NULL;
12,824,516✔
970
  if (pMemTbData != NULL) {
12,824,516✔
971
    taosRLockLatch(&pMemTbData->lock);
4,566,214✔
972
    p = pMemTbData->pHead;
4,567,604✔
973
    while (p) {
5,359,290✔
974
      if (p->version <= ver) {
791,695✔
975
        void* px = taosArrayPush(pMemDelData, p);
787,610✔
976
        if (px == NULL) {
787,610!
977
          taosRUnLockLatch(&pMemTbData->lock);
×
978
          return terrno;
×
979
        }
980
      }
981

982
      p = p->pNext;
791,686✔
983
    }
984
    taosRUnLockLatch(&pMemTbData->lock);
4,567,595✔
985
  }
986

987
  if (piMemTbData != NULL) {
12,826,078✔
988
    p = piMemTbData->pHead;
27,233✔
989
    while (p) {
89,431✔
990
      if (p->version <= ver) {
62,198!
991
        void* px = taosArrayPush(pMemDelData, p);
62,198✔
992
        if (px == NULL) {
62,198!
993
          return terrno;
×
994
        }
995
      }
996
      p = p->pNext;
62,198✔
997
    }
998
  }
999

1000
  return TSDB_CODE_SUCCESS;
12,826,078✔
1001
}
1002

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

1010
  if (pNumOfRows != 0) {
5,668!
1011
    *pNumOfRows = 0;
5,669✔
1012
  }
1013

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

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

1023
  if (i >= TARRAY2_SIZE(pStatisBlkArray)) {
5,668!
UNCOV
1024
    return code;
×
1025
  }
1026

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

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

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

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

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

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

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

1065
    uint64_t uid = pUidList[uidIndex];
5,666✔
1066

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

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

1112
int32_t doAdjustValidDataIters(SArray* pLDIterList, int32_t numOfFileObj) {
14,510,459✔
1113
  int32_t size = taosArrayGetSize(pLDIterList);
14,510,459✔
1114

1115
  if (size < numOfFileObj) {
14,509,517✔
1116
    int32_t inc = numOfFileObj - size;
6,940,455✔
1117
    for (int32_t k = 0; k < inc; ++k) {
13,890,230✔
1118
      SLDataIter* pIter = taosMemoryCalloc(1, sizeof(SLDataIter));
6,941,689✔
1119
      if (!pIter) {
6,948,199!
1120
        return terrno;
×
1121
      }
1122
      void* px = taosArrayPush(pLDIterList, &pIter);
6,949,775✔
1123
      if (px == NULL) {
6,949,775!
1124
        taosMemoryFree(pIter);
×
1125
        return terrno;
×
1126
      }
1127
    }
1128
  } else if (size > numOfFileObj) {  // remove unused LDataIter
7,569,062!
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;
14,513,148✔
1138
}
1139

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

1144
  // add the list/iter placeholder
1145
  while (taosArrayGetSize(pSttFileBlockIterArray) < numOfLevels) {
21,250,645✔
1146
    SArray* pList = taosArrayInit(4, POINTER_BYTES);
6,929,915✔
1147
    if (pList == NULL) {
6,946,386!
1148
      return terrno;
×
1149
    }
1150
    void* px = taosArrayPush(pSttFileBlockIterArray, &pList);
6,942,447✔
1151
    if (px == NULL) {
6,942,447!
1152
      return terrno;
×
1153
    }
1154
  }
1155

1156
  for (int32_t j = 0; j < numOfLevels; ++j) {
28,825,480✔
1157
    SSttLvl* pSttLevel = pFileSet->lvlArr->data[j];
14,514,399✔
1158
    SArray*  pList = taosArrayGetP(pSttFileBlockIterArray, j);
14,514,399✔
1159
    code = doAdjustValidDataIters(pList, TARRAY2_SIZE(pSttLevel->fobjArr));
14,511,352✔
1160
    if (code != TSDB_CODE_SUCCESS) {
14,514,929!
1161
      return code;
×
1162
    }
1163
  }
1164

1165
  return TSDB_CODE_SUCCESS;
14,311,081✔
1166
}
1167

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

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

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

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

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

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

1236
  return numOfRows;
5,671✔
1237
}
1238

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

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

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

1253
  // overlap with mem data
1254
  if (pMemIter->hasVal) {
105,683✔
1255
    STbData* pTbData = pMemIter->iter->pTbData;
34,067✔
1256
    if (overlapHelper(p1, pTbData->minKey, pTbData->maxKey)) {
34,067✔
1257
      return true;
33,680✔
1258
    }
1259
  }
1260

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

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

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

1281
  return false;
57,695✔
1282
}
1283

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

1288
  int32_t ret = tRowKeyCompare(&px1->skey, &px2->skey);
68,421✔
1289
  return ret;
68,460✔
1290
}
1291

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

1296
  int32_t num = taosArrayGetSize(pKeyRangeList);
274,594✔
1297
  if (num == 0) {
274,693✔
1298
    return false;
168,395✔
1299
  }
1300

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

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

1311
  for (int32_t i = 0; i < num - 1; ++i) {
57,688✔
1312
    SSttKeyRange* p1 = taosArrayGet(pKeyRangeList, i);
24,871✔
1313
    SSttKeyRange* p2 = taosArrayGet(pKeyRangeList, i + 1);
24,872✔
1314
    if (p1 == NULL || p2 == NULL) {
24,853!
1315
      return false;
24,838✔
1316
    }
1317

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

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

1329
  return true;
32,817✔
1330
}
1331

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

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

1342
    if (p->ts >= pRecord->firstKey.key.ts && p->ts <= pRecord->lastKey.key.ts) {
107,922✔
1343
      if (p->version >= pRecord->minVer) {
25,703✔
1344
        return true;
24,091✔
1345
      }
1346
    } else if (p->ts < pRecord->firstKey.key.ts) {  // p->ts < pBlock->minKey.ts
82,219✔
1347
      if (p->version >= pRecord->minVer) {
77,480✔
1348
        if (i < num - 1) {
23,252!
1349
          TSDBKEY* pnext = taosArrayGet(pBlockScanInfo->delSkyline, i + 1);
23,252✔
1350
          if (pnext == NULL) {
23,252!
1351
            return false;
×
1352
          }
1353

1354
          if (pnext->ts >= pRecord->firstKey.key.ts) {
23,252✔
1355
            return true;
8,138✔
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;
4,739✔
1365
    }
1366
  }
1367

1368
  return false;
4✔
1369
}
1370

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

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

1381
    if (p->ts >= pRecord->firstKey.key.ts && p->ts <= pRecord->lastKey.key.ts) {
8,076!
1382
      return true;
8,076✔
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) {
788,464✔
1403
  if (pBlockScanInfo->delSkyline == NULL || (taosArrayGetSize(pBlockScanInfo->delSkyline) == 0)) {
788,464✔
1404
    return false;
741,563✔
1405
  }
1406

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

1414
  if (pRecord->firstKey.key.ts > pLast->ts || pRecord->lastKey.key.ts < pFirst->ts) {
47,052✔
1415
    return false;
10,080✔
1416
  }
1417

1418
  // version is not overlap
1419
  if (ASCENDING_TRAVERSE(order)) {
36,972✔
1420
    return doCheckDatablockOverlap(pBlockScanInfo, pRecord, pBlockScanInfo->fileDelIndex);
27,428✔
1421
  } else {
1422
    int32_t index = pBlockScanInfo->fileDelIndex;
9,544✔
1423
    while (1) {
29,608✔
1424
      TSDBKEY* p = taosArrayGet(pBlockScanInfo->delSkyline, index);
39,152✔
1425
      if (p == NULL) {
39,152!
1426
        return false;
×
1427
      }
1428

1429
      if (p->ts > pRecord->firstKey.key.ts && index > 0) {
39,152✔
1430
        index -= 1;
29,608✔
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) {
9,544!
1433
          index -= 1;
24✔
1434
        }
1435
        break;
9,544✔
1436
      }
1437
    }
1438

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

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

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

1455
  if (pRecord->firstKey.key.ts > pLast->ts || pRecord->lastKey.key.ts < pFirst->ts) {
8,457!
1456
    return false;
381✔
1457
  }
1458

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