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

taosdata / TDengine / #4725

08 Sep 2025 08:43AM UTC coverage: 59.112% (+0.003%) from 59.109%
#4725

push

travis-ci

web-flow
Merge pull request #32881 from taosdata/enh/add-new-windows-ci

fix(ci): update workflow reference to use new Windows CI YAML

135818 of 292179 branches covered (46.48%)

Branch coverage included in aggregate %.

204660 of 283811 relevant lines covered (72.11%)

23953203.49 hits per line

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

80.24
/source/dnode/vnode/src/tsdb/tsdbMergeTree.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 "tsdb.h"
17
#include "tsdbFSet2.h"
18
#include "tsdbMerge.h"
19
#include "tsdbReadUtil.h"
20
#include "tsdbSttFileRW.h"
21
#include "tsdbUtil2.h"
22

23
static void tLDataIterClose2(SLDataIter *pIter);
24

25
// SLDataIter =================================================
26
int32_t tCreateSttBlockLoadInfo(STSchema *pSchema, int16_t *colList, int32_t numOfCols, SSttBlockLoadInfo **pInfo) {
10,386,401✔
27
  *pInfo = NULL;
10,386,401✔
28

29
  SSttBlockLoadInfo *pLoadInfo = taosMemoryCalloc(1, sizeof(SSttBlockLoadInfo));
10,386,401!
30
  if (pLoadInfo == NULL) {
10,392,727!
31
    return terrno;
×
32
  }
33

34
  pLoadInfo->blockData[0].sttBlockIndex = -1;
10,392,727✔
35
  pLoadInfo->blockData[1].sttBlockIndex = -1;
10,392,727✔
36

37
  pLoadInfo->currentLoadBlockIndex = 1;
10,392,727✔
38

39
  int32_t code = tBlockDataCreate(&pLoadInfo->blockData[0].data);
10,392,727✔
40
  if (code) {
10,394,609!
41
    taosMemoryFreeClear(pLoadInfo);
×
42
    return code;
×
43
  }
44

45
  code = tBlockDataCreate(&pLoadInfo->blockData[1].data);
10,394,609✔
46
  if (code) {
10,395,533!
47
    taosMemoryFreeClear(pLoadInfo);
×
48
    return code;
×
49
  }
50

51
  pLoadInfo->aSttBlk = taosArrayInit(4, sizeof(SSttBlk));
10,395,533✔
52
  if (pLoadInfo->aSttBlk == NULL) {
10,393,543✔
53
    taosMemoryFreeClear(pLoadInfo);
1,149!
54
    return terrno;
1,149✔
55
  }
56

57
  pLoadInfo->pSchema = pSchema;
10,392,394✔
58
  pLoadInfo->colIds = colList;
10,392,394✔
59
  pLoadInfo->numOfCols = numOfCols;
10,392,394✔
60

61
  *pInfo = pLoadInfo;
10,392,394✔
62
  return code;
10,392,394✔
63
}
64

65
static void freeItem(void *pValue) {
47,558,084✔
66
  SValue *p = (SValue *)pValue;
47,558,084✔
67
  if (IS_VAR_DATA_TYPE(p->type) || p->type == TSDB_DATA_TYPE_DECIMAL) {
47,558,084!
68
    taosMemoryFree(p->pData);
154,111!
69
  }
70
}
47,562,070✔
71

72
void destroySttBlockLoadInfo(SSttBlockLoadInfo *pLoadInfo) {
10,400,162✔
73
  if (pLoadInfo == NULL) {
10,400,162!
74
    return;
×
75
  }
76

77
  pLoadInfo->currentLoadBlockIndex = 1;
10,400,162✔
78

79
  SBlockDataInfo *pInfo = &pLoadInfo->blockData[0];
10,400,162✔
80
  tBlockDataDestroy(&pInfo->data);
10,400,162✔
81
  pInfo->sttBlockIndex = -1;
10,399,894✔
82
  pInfo->pin = false;
10,399,894✔
83

84
  pInfo = &pLoadInfo->blockData[1];
10,399,894✔
85
  tBlockDataDestroy(&pInfo->data);
10,399,894✔
86
  pInfo->sttBlockIndex = -1;
10,399,772✔
87
  pInfo->pin = false;
10,399,772✔
88

89
  taosArrayDestroy(pLoadInfo->info.pUid);
10,399,772✔
90
  taosArrayDestroyEx(pLoadInfo->info.pFirstKey, freeItem);
10,399,657✔
91
  taosArrayDestroyEx(pLoadInfo->info.pLastKey, freeItem);
10,399,578✔
92
  taosArrayDestroy(pLoadInfo->info.pCount);
10,399,545✔
93
  taosArrayDestroy(pLoadInfo->info.pFirstTs);
10,399,555✔
94
  taosArrayDestroy(pLoadInfo->info.pLastTs);
10,399,513✔
95

96
  pLoadInfo->info.pUid = NULL;
10,399,776✔
97
  pLoadInfo->info.pFirstKey = NULL;
10,399,776✔
98
  pLoadInfo->info.pLastKey = NULL;
10,399,776✔
99
  pLoadInfo->info.pCount = NULL;
10,399,776✔
100
  pLoadInfo->info.pFirstTs = NULL;
10,399,776✔
101
  pLoadInfo->info.pLastTs = NULL;
10,399,776✔
102

103
  taosArrayDestroy(pLoadInfo->aSttBlk);
10,399,776✔
104
  taosMemoryFree(pLoadInfo);
10,400,020!
105
}
106

107
void destroyLDataIter(SLDataIter *pIter) {
10,395,841✔
108
  tLDataIterClose2(pIter);
10,395,841✔
109
  destroySttBlockLoadInfo(pIter->pBlockLoadInfo);
10,400,426✔
110
  taosMemoryFree(pIter);
10,400,228!
111
}
10,400,492✔
112

113
void destroySttBlockReader(SArray *pLDataIterArray, SSttBlockLoadCostInfo *pLoadCost) {
20,759,633✔
114
  if (pLDataIterArray == NULL) {
20,759,633✔
115
    return;
44,480✔
116
  }
117

118
  int32_t numOfLevel = taosArrayGetSize(pLDataIterArray);
20,715,153✔
119
  for (int32_t i = 0; i < numOfLevel; ++i) {
31,094,297✔
120
    SArray *pList = taosArrayGetP(pLDataIterArray, i);
10,385,784✔
121
    for (int32_t j = 0; j < taosArrayGetSize(pList); ++j) {
20,785,250✔
122
      SLDataIter *pIter = taosArrayGetP(pList, j);
10,396,172✔
123
      if (pIter->pBlockLoadInfo != NULL) {
10,395,408!
124
        SSttBlockLoadCostInfo *pCost = &pIter->pBlockLoadInfo->cost;
10,395,515✔
125
        if (pLoadCost != NULL) {
10,395,515✔
126
          pLoadCost->loadBlocks += pCost->loadBlocks;
10,393,787✔
127
          pLoadCost->loadStatisBlocks += pCost->loadStatisBlocks;
10,393,787✔
128
          pLoadCost->blockElapsedTime += pCost->blockElapsedTime;
10,393,787✔
129
          pLoadCost->statisElapsedTime += pCost->statisElapsedTime;
10,393,787✔
130
        }
131
      }
132

133
      destroyLDataIter(pIter);
10,395,408✔
134
    }
135

136
    taosArrayDestroy(pList);
10,382,023✔
137
  }
138

139
  taosArrayDestroy(pLDataIterArray);
20,708,513✔
140
}
141

142
// choose the unpinned slot to load next data block
143
static void updateBlockLoadSlot(SSttBlockLoadInfo *pLoadInfo) {
7,370,631✔
144
  int32_t nextSlotIndex = pLoadInfo->currentLoadBlockIndex ^ 1;
7,370,631✔
145
  if (pLoadInfo->blockData[nextSlotIndex].pin) {
7,370,631!
146
    nextSlotIndex = nextSlotIndex ^ 1;
×
147
  }
148

149
  pLoadInfo->currentLoadBlockIndex = nextSlotIndex;
7,370,631✔
150
}
7,370,631✔
151

152
static int32_t loadLastBlock(SLDataIter *pIter, const char *idStr, SBlockData **pResBlock) {
1,194,541,830✔
153
  if (pResBlock != NULL) {
1,194,541,830!
154
    *pResBlock = NULL;
1,195,059,804✔
155
  }
156

157
  int32_t            code = 0;
1,194,541,830✔
158
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
1,194,541,830✔
159

160
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
1,194,541,830✔
161
    if (pInfo->currentLoadBlockIndex != 0) {
902,428,763✔
162
      tsdbDebug("current load index is set to 0, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
362!
163
                ", load data, %s",
164
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
165
      pInfo->currentLoadBlockIndex = 0;
362✔
166
    }
167

168
    *pResBlock = &pInfo->blockData[0].data;
902,428,763✔
169
    return code;
902,428,763✔
170
  }
171

172
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
292,113,067!
173
    if (pInfo->currentLoadBlockIndex != 1) {
321,272,494✔
174
      tsdbDebug("current load index is set to 1, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
349!
175
                ", load data, %s",
176
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
177
      pInfo->currentLoadBlockIndex = 1;
349✔
178
    }
179

180
    *pResBlock = &pInfo->blockData[1].data;
321,272,494✔
181
    return code;
321,272,494✔
182
  }
183

184
  if (pIter->pSttBlk == NULL || pInfo->pSchema == NULL) {
×
185
    return code;
×
186
  }
187

188
  updateBlockLoadSlot(pInfo);
7,371,236✔
189
  int64_t st = taosGetTimestampUs();
7,373,268✔
190

191
  SBlockData *pBlock = &pInfo->blockData[pInfo->currentLoadBlockIndex].data;
7,373,268✔
192
  code = tsdbSttFileReadBlockDataByColumn(pIter->pReader, pIter->pSttBlk, pBlock, pInfo->pSchema, &pInfo->colIds[1],
7,373,268✔
193
                                          pInfo->numOfCols - 1);
7,373,268✔
194
  if (code != TSDB_CODE_SUCCESS) {
7,373,902!
195
    return code;
×
196
  }
197

198
  double el = (taosGetTimestampUs() - st) / 1000.0;
7,374,125✔
199
  pInfo->cost.blockElapsedTime += el;
7,374,125✔
200
  pInfo->cost.loadBlocks += 1;
7,374,125✔
201

202
  tsdbDebug("read stt block, total load:%" PRId64 ", trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
7,374,125✔
203
            ", last block index:%d, entry:%d, rows:%d, uidRange:%" PRId64 "-%" PRId64 " tsRange:%" PRId64 "-%" PRId64
204
            " %p, elapsed time:%.2f ms, %s",
205
            pInfo->cost.loadBlocks, pIter->uid, pIter->cid, pIter->iSttBlk, pInfo->currentLoadBlockIndex, pBlock->nRow,
206
            pIter->pSttBlk->minUid, pIter->pSttBlk->maxUid, pIter->pSttBlk->minKey, pIter->pSttBlk->maxKey, pBlock, el,
207
            idStr);
208

209
  pInfo->blockData[pInfo->currentLoadBlockIndex].sttBlockIndex = pIter->iSttBlk;
7,374,261✔
210
  pIter->iRow = (pIter->backward) ? pInfo->blockData[pInfo->currentLoadBlockIndex].data.nRow : -1;
7,374,261✔
211

212
  tsdbDebug("last block index list:%d, %d, rowIndex:%d %s", pInfo->blockData[0].sttBlockIndex,
7,374,261✔
213
            pInfo->blockData[1].sttBlockIndex, pIter->iRow, idStr);
214

215
  *pResBlock = &pInfo->blockData[pInfo->currentLoadBlockIndex].data;
7,374,247✔
216
  return code;
7,374,247✔
217
}
218

219
// find the earliest block that contains the required records
220
static FORCE_INLINE int32_t findEarliestIndex(int32_t index, uint64_t uid, const SSttBlk *pBlockList, int32_t num,
221
                                              int32_t backward) {
222
  int32_t i = index;
8,476,312✔
223
  int32_t step = backward ? 1 : -1;
8,476,312✔
224
  while (i >= 0 && i < num && uid >= pBlockList[i].minUid && uid <= pBlockList[i].maxUid) {
17,002,921!
225
    i += step;
8,526,609✔
226
  }
227
  return i - step;
8,476,312✔
228
}
229

230
static int32_t binarySearchForStartBlock(SSttBlk *pBlockList, int32_t num, uint64_t uid, int32_t backward) {
23,797,737✔
231
  int32_t midPos = -1;
23,797,737✔
232
  if (num <= 0) {
23,797,737✔
233
    return -1;
4,943,686✔
234
  }
235

236
  int32_t firstPos = 0;
18,854,051✔
237
  int32_t lastPos = num - 1;
18,854,051✔
238

239
  // find the first position which is bigger than the key
240
  if ((uid > pBlockList[lastPos].maxUid) || (uid < pBlockList[firstPos].minUid)) {
18,854,051✔
241
    return -1;
10,377,628✔
242
  }
243

244
  while (1) {
426,580✔
245
    if (uid >= pBlockList[firstPos].minUid && uid <= pBlockList[firstPos].maxUid) {
8,903,003✔
246
      return findEarliestIndex(firstPos, uid, pBlockList, num, backward);
8,373,920✔
247
    }
248

249
    if (uid > pBlockList[lastPos].maxUid || uid < pBlockList[firstPos].minUid) {
529,083✔
250
      return -1;
111✔
251
    }
252

253
    int32_t numOfRows = lastPos - firstPos + 1;
528,972✔
254
    midPos = (numOfRows >> 1u) + firstPos;
528,972✔
255

256
    if (uid < pBlockList[midPos].minUid) {
528,972✔
257
      lastPos = midPos - 1;
205,944✔
258
    } else if (uid > pBlockList[midPos].maxUid) {
323,028✔
259
      firstPos = midPos + 1;
220,636✔
260
    } else {
261
      return findEarliestIndex(midPos, uid, pBlockList, num, backward);
102,392✔
262
    }
263
  }
264
}
265

266
static FORCE_INLINE int32_t findEarliestRow(int32_t index, uint64_t uid, const uint64_t *uidList, int32_t num,
267
                                            int32_t backward) {
268
  int32_t i = index;
8,378,746✔
269
  int32_t step = backward ? 1 : -1;
8,378,746✔
270
  while (i >= 0 && i < num && uid == uidList[i]) {
119,656,098!
271
    i += step;
111,277,352✔
272
  }
273
  return i - step;
8,378,746✔
274
}
275

276
static int32_t binarySearchForStartRowIndex(uint64_t *uidList, int32_t num, uint64_t uid, int32_t backward) {
8,440,440✔
277
  int32_t firstPos = 0;
8,440,440✔
278
  int32_t lastPos = num - 1;
8,440,440✔
279

280
  // find the first position which is bigger than the key
281
  if ((uid > uidList[lastPos]) || (uid < uidList[firstPos])) {
8,440,440!
282
    return -1;
×
283
  }
284

285
  while (1) {
1,328,677✔
286
    if (uid == uidList[firstPos]) {
9,769,117✔
287
      return findEarliestRow(firstPos, uid, uidList, num, backward);
7,199,702✔
288
    }
289

290
    if (uid > uidList[lastPos] || uid < uidList[firstPos]) {
2,569,415✔
291
      return -1;
61,694✔
292
    }
293

294
    int32_t numOfRows = lastPos - firstPos + 1;
2,507,721✔
295
    int32_t midPos = (numOfRows >> 1u) + firstPos;
2,507,721✔
296

297
    if (uid < uidList[midPos]) {
2,507,721✔
298
      lastPos = midPos - 1;
516,972✔
299
    } else if (uid > uidList[midPos]) {
1,990,749✔
300
      firstPos = midPos + 1;
811,705✔
301
    } else {
302
      return findEarliestRow(midPos, uid, uidList, num, backward);
1,179,044✔
303
    }
304
  }
305
}
306

307
static int32_t extractSttBlockInfo(SLDataIter *pIter, const TSttBlkArray *pArray, SSttBlockLoadInfo *pBlockLoadInfo,
10,375,163✔
308
                                   uint64_t suid) {
309
  void   *px = NULL;
10,375,163✔
310
  int32_t code = TSDB_CODE_SUCCESS;
10,375,163✔
311
  if (TARRAY2_SIZE(pArray) <= 0) {
10,375,163✔
312
    return code;
20,479✔
313
  }
314

315
  SSttBlk *pStart = &pArray->data[0];
10,354,684✔
316
  SSttBlk *pEnd = &pArray->data[TARRAY2_SIZE(pArray) - 1];
10,354,684✔
317

318
  // all identical
319
  if (pStart->suid == pEnd->suid) {
10,354,684✔
320
    if (pStart->suid != suid) {  // no qualified stt block existed
3,819,739✔
321
      taosArrayClear(pBlockLoadInfo->aSttBlk);
1,713,968✔
322
      pIter->iSttBlk = -1;
1,713,734✔
323
      return TSDB_CODE_SUCCESS;
1,713,734✔
324
    } else {  // all blocks are qualified
325
      taosArrayClear(pBlockLoadInfo->aSttBlk);
2,105,771✔
326
      px = taosArrayAddBatch(pBlockLoadInfo->aSttBlk, pArray->data, pArray->size);
2,105,875✔
327
      if (px == NULL) {
2,105,231!
328
        return terrno;
×
329
      }
330
    }
331
  } else {
332
    SArray *pTmp = taosArrayInit(TARRAY2_SIZE(pArray), sizeof(SSttBlk));
6,534,945✔
333
    if (pTmp == NULL) {
6,542,777!
334
      return terrno;
×
335
    }
336

337
    for (int32_t i = 0; i < TARRAY2_SIZE(pArray); ++i) {
23,473,827✔
338
      SSttBlk *p = &pArray->data[i];
19,060,356✔
339
      if (p->suid < suid) {
19,060,356✔
340
        continue;
11,235,500✔
341
      }
342

343
      if (p->suid == suid) {
7,824,856✔
344
        void *px = taosArrayPush(pTmp, p);
5,696,115✔
345
        if (px == NULL) {
5,696,115!
346
          code = terrno;
×
347
          break;
×
348
        }
349
      } else if (p->suid > suid) {
2,128,572!
350
        break;
2,129,244✔
351
      }
352
    }
353

354
    taosArrayDestroy(pBlockLoadInfo->aSttBlk);
6,542,715✔
355
    pBlockLoadInfo->aSttBlk = pTmp;
6,543,752✔
356
  }
357

358
  return code;
8,648,983✔
359
}
360

361
static int32_t tValueDupPayload(SValue *pVal) {
2,961,223✔
362
  if (IS_VAR_DATA_TYPE(pVal->type) || pVal->type == TSDB_DATA_TYPE_DECIMAL) {
2,961,223!
363
    char *p = (char *)pVal->pData;
157,857✔
364
    char *pBuf = taosMemoryMalloc(pVal->nData);
157,857!
365
    if (pBuf == NULL) {
157,974!
366
      return terrno;
×
367
    }
368

369
    memcpy(pBuf, p, pVal->nData);
157,974✔
370
    pVal->pData = (uint8_t *)pBuf;
157,974✔
371
  }
372

373
  return TSDB_CODE_SUCCESS;
2,961,340✔
374
}
375

376
static int32_t loadSttStatisticsBlockData(SSttFileReader *pSttFileReader, SSttBlockLoadInfo *pBlockLoadInfo,
10,377,149✔
377
                                          TStatisBlkArray *pStatisBlkArray, uint64_t suid, const char *id) {
378
  int32_t code = TSDB_CODE_SUCCESS;
10,377,149✔
379
  int32_t lino = 0;
10,377,149✔
380
  void   *px = NULL;
10,377,149✔
381
  int32_t startIndex = 0;
10,377,149✔
382
  int32_t ret = 0;
10,377,149✔
383

384
  int32_t numOfBlocks = TARRAY2_SIZE(pStatisBlkArray);
10,377,149✔
385
  if (numOfBlocks <= 0) {
10,377,149✔
386
    return code;
20,479✔
387
  }
388

389
  while ((startIndex < numOfBlocks) && (pStatisBlkArray->data[startIndex].maxTbid.suid < suid)) {
12,188,202✔
390
    ++startIndex;
1,831,532✔
391
  }
392

393
  if (startIndex >= numOfBlocks || pStatisBlkArray->data[startIndex].minTbid.suid > suid) {
10,356,670✔
394
    return 0;
2,456,743✔
395
  }
396

397
  int32_t endIndex = startIndex;
7,899,927✔
398
  while (endIndex < numOfBlocks && pStatisBlkArray->data[endIndex].minTbid.suid <= suid) {
15,810,238✔
399
    ++endIndex;
7,910,311✔
400
  }
401

402
  int32_t num = endIndex - startIndex;
7,899,927✔
403
  pBlockLoadInfo->cost.loadStatisBlocks += num;
7,899,927✔
404

405
  STbStatisBlock block;
406
  code = tStatisBlockInit(&block);
7,899,927✔
407
  QUERY_CHECK_CODE(code, lino, _end);
7,899,858!
408

409
  int64_t st = taosGetTimestampUs();
7,899,665✔
410

411
  for (int32_t k = startIndex; k < endIndex; ++k) {
15,783,149✔
412
    code = tsdbSttFileReadStatisBlock(pSttFileReader, &pStatisBlkArray->data[k], &block);
7,906,024✔
413
    QUERY_CHECK_CODE(code, lino, _end);
7,909,896!
414

415
    int32_t i = 0;
7,909,896✔
416
    int32_t rows = block.numOfRecords;
7,909,896✔
417
    while (i < rows && ((int64_t *)block.suids.data)[i] != suid) {
18,579,566✔
418
      ++i;
10,669,670✔
419
    }
420

421
    // existed
422
    if (i < rows) {
7,909,896✔
423
      SSttTableRowsInfo *pInfo = &pBlockLoadInfo->info;
7,732,373✔
424

425
      if (pInfo->pUid == NULL) {
7,732,373✔
426
        pInfo->pUid = taosArrayInit(rows, sizeof(int64_t));
7,722,229✔
427
        pInfo->pFirstTs = taosArrayInit(rows, sizeof(int64_t));
7,722,299✔
428
        pInfo->pLastTs = taosArrayInit(rows, sizeof(int64_t));
7,722,689✔
429
        pInfo->pCount = taosArrayInit(rows, sizeof(int64_t));
7,722,760✔
430

431
        pInfo->pFirstKey = taosArrayInit(rows, sizeof(SValue));
7,722,430✔
432
        pInfo->pLastKey = taosArrayInit(rows, sizeof(SValue));
7,722,611✔
433

434
        if (pInfo->pUid == NULL || pInfo->pFirstTs == NULL || pInfo->pLastTs == NULL || pInfo->pCount == NULL ||
7,722,694!
435
            pInfo->pFirstKey == NULL || pInfo->pLastKey == NULL) {
7,722,755!
436
          code = terrno;
×
437
          goto _end;
×
438
        }
439
      }
440

441
      if (pStatisBlkArray->data[k].maxTbid.suid == suid) {
7,732,977✔
442
        int32_t size = rows - i;
5,814,393✔
443
        int32_t offset = i * sizeof(int64_t);
5,814,393✔
444

445
        px = taosArrayAddBatch(pInfo->pUid, tBufferGetDataAt(&block.uids, offset), size);
5,814,393✔
446
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
5,814,326!
447

448
        px = taosArrayAddBatch(pInfo->pFirstTs, tBufferGetDataAt(&block.firstKeyTimestamps, offset), size);
5,814,326✔
449
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
5,814,074!
450

451
        px = taosArrayAddBatch(pInfo->pLastTs, tBufferGetDataAt(&block.lastKeyTimestamps, offset), size);
5,814,074✔
452
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
5,813,728!
453

454
        px = taosArrayAddBatch(pInfo->pCount, tBufferGetDataAt(&block.counts, offset), size);
5,813,728✔
455
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
5,813,795✔
456

457
        if (block.numOfPKs > 0) {
5,813,435✔
458
          SValue vFirst = {0}, vLast = {0};
102,881✔
459
          for (int32_t f = i; f < rows; ++f) {
1,550,613✔
460
            code = tValueColumnGet(&block.firstKeyPKs[0], f, &vFirst);
1,447,667✔
461
            TSDB_CHECK_CODE(code, lino, _end);
1,447,701!
462

463
            code = tValueDupPayload(&vFirst);
1,447,701✔
464
            TSDB_CHECK_CODE(code, lino, _end);
1,447,755!
465

466
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
1,447,755✔
467
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,447,706!
468

469
            // todo add api to clone the original data
470
            code = tValueColumnGet(&block.lastKeyPKs[0], f, &vLast);
1,447,706✔
471
            TSDB_CHECK_CODE(code, lino, _end);
1,447,761!
472

473
            code = tValueDupPayload(&vLast);
1,447,761✔
474
            TSDB_CHECK_CODE(code, lino, _end);
1,447,779!
475

476
            px = taosArrayPush(pInfo->pLastKey, &vLast);
1,447,779✔
477
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,447,732!
478
          }
479
        } else {
480
          SValue vFirst = {0};
5,710,554✔
481
          for (int32_t j = 0; j < size; ++j) {
25,016,549✔
482
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
19,332,153✔
483
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
19,317,207!
484

485
            px = taosArrayPush(pInfo->pLastKey, &vFirst);
19,317,207✔
486
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
19,305,995!
487
          }
488
        }
489
      } else {
490
        STbStatisRecord record = {0};
1,918,584✔
491
        while (i < rows) {
4,465,748✔
492
          code = tStatisBlockGet(&block, i, &record);
4,465,718✔
493
          TSDB_CHECK_CODE(code, lino, _end);
4,465,580!
494

495
          if (record.suid != suid) {
4,465,580✔
496
            break;
1,918,589✔
497
          }
498

499
          px = taosArrayPush(pInfo->pUid, &record.uid);
2,546,991✔
500
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
2,547,135!
501

502
          px = taosArrayPush(pInfo->pCount, &record.count);
2,547,135✔
503
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
2,547,108!
504

505
          px = taosArrayPush(pInfo->pFirstTs, &record.firstKey.ts);
2,547,108✔
506
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
2,547,119!
507

508
          px = taosArrayPush(pInfo->pLastTs, &record.lastKey.ts);
2,547,119✔
509
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
2,547,105!
510

511
          if (record.firstKey.numOfPKs > 0) {
2,547,105✔
512
            SValue s = record.firstKey.pks[0];
33,204✔
513
            code = tValueDupPayload(&s);
33,204✔
514
            TSDB_CHECK_CODE(code, lino, _end);
33,209!
515

516
            px = taosArrayPush(pInfo->pFirstKey, &s);
33,209✔
517
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
33,209!
518

519
            s = record.lastKey.pks[0];
33,209✔
520
            code = tValueDupPayload(&s);
33,209✔
521
            TSDB_CHECK_CODE(code, lino, _end);
33,212!
522

523
            px = taosArrayPush(pInfo->pLastKey, &s);
33,212✔
524
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
33,212!
525
          } else {
526
            SValue v = {0};
2,513,901✔
527
            px = taosArrayPush(pInfo->pFirstKey, &v);
2,513,901✔
528
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
2,513,941!
529

530
            px = taosArrayPush(pInfo->pLastKey, &v);
2,513,941✔
531
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
2,513,952!
532
          }
533

534
          i += 1;
2,547,164✔
535
        }
536
      }
537
    }
538
  }
539

540
_end:
7,877,125✔
541
  tStatisBlockDestroy(&block);
7,877,125✔
542
  if (code != 0) {
7,900,585!
543
    tsdbError("%s error happens at:%s line number: %d, code:%s", id, __func__, lino, tstrerror(code));
×
544
  } else {
545
    double el = (taosGetTimestampUs() - st) / 1000.0;
7,900,716✔
546
    pBlockLoadInfo->cost.statisElapsedTime += el;
7,900,716✔
547

548
    tsdbDebug("%s load %d statis blocks into buf, elapsed time:%.2fms", id, num, el);
7,900,716✔
549
  }
550
  return code;
7,900,937✔
551
}
552

553
static int32_t doLoadSttFilesBlk(SSttBlockLoadInfo *pBlockLoadInfo, SLDataIter *pIter, int64_t suid,
10,376,501✔
554
                                 _load_tomb_fn loadTombFn, void *pReader1, const char *idStr) {
555
  int64_t st = taosGetTimestampUs();
10,382,203✔
556

557
  const TSttBlkArray *pSttBlkArray = NULL;
10,382,203✔
558
  pBlockLoadInfo->sttBlockLoaded = true;
10,382,203✔
559

560
  // load the stt block info for each stt-block
561
  int32_t code = tsdbSttFileReadSttBlk(pIter->pReader, &pSttBlkArray);
10,382,203✔
562
  if (code != TSDB_CODE_SUCCESS) {
10,377,954!
563
    tsdbError("load stt blk failed, code:%s, %s", tstrerror(code), idStr);
×
564
    return code;
×
565
  }
566

567
  // load the stt block info for each stt file block
568
  code = extractSttBlockInfo(pIter, pSttBlkArray, pBlockLoadInfo, suid);
10,377,954✔
569
  if (code != TSDB_CODE_SUCCESS) {
10,382,301!
570
    tsdbError("load stt block info failed, code:%s, %s", tstrerror(code), idStr);
×
571
    return code;
×
572
  }
573

574
  // load stt statistics block for all stt-blocks, to decide if the data of queried table exists in current stt file
575
  TStatisBlkArray *pStatisBlkArray = NULL;
10,382,301✔
576
  code = tsdbSttFileReadStatisBlk(pIter->pReader, (const TStatisBlkArray **)&pStatisBlkArray);
10,382,301✔
577
  if (code != TSDB_CODE_SUCCESS) {
10,381,074!
578
    tsdbError("failed to load stt block statistics, code:%s, %s", tstrerror(code), idStr);
×
579
    return code;
×
580
  }
581

582
  // load statistics block for all tables in current stt file
583
  code = loadSttStatisticsBlockData(pIter->pReader, pIter->pBlockLoadInfo, pStatisBlkArray, suid, idStr);
10,381,074✔
584
  if (code != TSDB_CODE_SUCCESS) {
10,384,623!
585
    tsdbError("failed to load stt statistics block data, code:%s, %s", tstrerror(code), idStr);
×
586
    return code;
×
587
  }
588

589
  code = loadTombFn(pReader1, pIter->pReader, pIter->pBlockLoadInfo);
10,384,623✔
590

591
  double el = (taosGetTimestampUs() - st) / 1000.0;
10,383,929✔
592
  tsdbDebug("load the stt file blk info completed, elapsed time:%.2fms, %s", el, idStr);
10,383,929✔
593
  return code;
10,383,925✔
594
}
595

596
static int32_t uidComparFn(const void *p1, const void *p2) {
22,426,884✔
597
  const uint64_t *pFirst = p1;
22,426,884✔
598
  const uint64_t *pVal = p2;
22,426,884✔
599

600
  if (*pFirst == *pVal) {
22,426,884✔
601
    return 0;
8,430,733✔
602
  } else {
603
    return *pFirst < *pVal ? -1 : 1;
13,996,151✔
604
  }
605
}
606

607
static void setSttInfoForCurrentTable(SSttBlockLoadInfo *pLoadInfo, uint64_t uid, SSttKeyRange *pRange,
23,815,104✔
608
                                      int64_t *numOfRows) {
609
  if (pRange == NULL || taosArrayGetSize(pLoadInfo->info.pUid) == 0) {
23,815,104!
610
    return;
4,944,172✔
611
  }
612

613
  int32_t index = taosArraySearchIdx(pLoadInfo->info.pUid, &uid, uidComparFn, TD_EQ);
18,861,899✔
614
  if (index >= 0) {
18,870,580✔
615
    pRange->skey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pFirstTs, index);
8,431,754✔
616
    pRange->ekey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pLastTs, index);
8,427,961✔
617

618
    *numOfRows += *(int64_t *)taosArrayGet(pLoadInfo->info.pCount, index);
8,428,389✔
619

620
    if (pRange->skey.numOfPKs > 0) {
8,427,760✔
621
      memcpy(&pRange->skey.pks[0], taosArrayGet(pLoadInfo->info.pFirstKey, index), sizeof(SValue));
175,653✔
622
      memcpy(&pRange->ekey.pks[0], taosArrayGet(pLoadInfo->info.pLastKey, index), sizeof(SValue));
175,623✔
623
    }
624
  }
625
}
626

627
int32_t tLDataIterOpen2(SLDataIter *pIter, SSttFileReader *pSttFileReader, int32_t cid, int8_t backward,
23,810,628✔
628
                        SMergeTreeConf *pConf, SSttBlockLoadInfo *pBlockLoadInfo, SSttKeyRange *pKeyRange,
629
                        int64_t *numOfRows, const char *idStr) {
630
  int32_t code = TSDB_CODE_SUCCESS;
23,810,628✔
631

632
  pIter->uid = pConf->uid;
23,810,628✔
633
  pIter->cid = cid;
23,810,628✔
634
  pIter->backward = backward;
23,810,628✔
635
  pIter->verRange.minVer = pConf->verRange.minVer;
23,810,628✔
636
  pIter->verRange.maxVer = pConf->verRange.maxVer;
23,810,628✔
637
  pIter->timeWindow.skey = pConf->timewindow.skey;
23,810,628✔
638
  pIter->timeWindow.ekey = pConf->timewindow.ekey;
23,810,628✔
639

640
  pIter->pStartRowKey = pConf->pCurRowKey;
23,810,628✔
641
  pIter->pReader = pSttFileReader;
23,810,628✔
642
  pIter->pBlockLoadInfo = pBlockLoadInfo;
23,810,628✔
643

644
  // open stt file failed, ignore and continue
645
  if (pIter->pReader == NULL) {
23,810,628!
646
    tsdbError("stt file reader is null, %s", idStr);
×
647
    pIter->pSttBlk = NULL;
×
648
    pIter->iSttBlk = -1;
×
649
    return TSDB_CODE_SUCCESS;
×
650
  }
651

652
  if (!pBlockLoadInfo->sttBlockLoaded) {
23,810,628✔
653
    code = doLoadSttFilesBlk(pBlockLoadInfo, pIter, pConf->suid, pConf->loadTombFn, pConf->pReader, idStr);
10,380,151✔
654
    if (code != TSDB_CODE_SUCCESS) {
10,383,627!
655
      return code;
×
656
    }
657
  }
658

659
  setSttInfoForCurrentTable(pBlockLoadInfo, pConf->uid, pKeyRange, numOfRows);
23,814,104✔
660

661
  // find the start block, actually we could load the position to avoid repeatly searching for the start position when
662
  // the skey is updated.
663
  size_t size = taosArrayGetSize(pBlockLoadInfo->aSttBlk);
23,801,794✔
664
  pIter->iSttBlk = binarySearchForStartBlock(pBlockLoadInfo->aSttBlk->pData, size, pConf->uid, backward);
23,797,535✔
665
  if (pIter->iSttBlk != -1) {
23,800,849✔
666
    pIter->pSttBlk = taosArrayGet(pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
8,492,426✔
667
    pIter->iRow = (pIter->backward) ? pIter->pSttBlk->nRow : -1;
8,489,702✔
668

669
    if ((!backward) && ((pConf->strictTimeRange && pIter->pSttBlk->minKey >= pIter->timeWindow.ekey) ||
8,489,702!
670
                        (!pConf->strictTimeRange && pIter->pSttBlk->minKey > pIter->timeWindow.ekey))) {
7,461,339!
671
      pIter->pSttBlk = NULL;
2,894✔
672
    }
673

674
    if (backward && ((pConf->strictTimeRange && pIter->pSttBlk->maxKey <= pIter->timeWindow.skey) ||
8,489,702!
675
                     (!pConf->strictTimeRange && pIter->pSttBlk->maxKey < pIter->timeWindow.skey))) {
1,029,157!
676
      pIter->pSttBlk = NULL;
385✔
677
      pIter->ignoreEarlierTs = true;
385✔
678
    }
679
  }
680

681
  return code;
23,798,125✔
682
}
683

684
void tLDataIterClose2(SLDataIter *pIter) {
10,396,707✔
685
  tsdbSttFileReaderClose(&pIter->pReader);
10,396,707✔
686
  pIter->pReader = NULL;
10,400,439✔
687
}
10,400,439✔
688

689
void tLDataIterNextBlock(SLDataIter *pIter, const char *idStr) {
8,547,608✔
690
  int32_t step = pIter->backward ? -1 : 1;
8,547,608✔
691
  int32_t oldIndex = pIter->iSttBlk;
8,547,608✔
692

693
  pIter->iSttBlk += step;
8,547,608✔
694

695
  int32_t index = -1;
8,547,608✔
696
  size_t  size = pIter->pBlockLoadInfo->aSttBlk->size;
8,547,608✔
697
  for (int32_t i = pIter->iSttBlk; i < size && i >= 0; i += step) {
8,549,438!
698
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, i);
285,186✔
699
    if ((!pIter->backward) && p->minUid > pIter->uid) {
285,187✔
700
      break;
182,833✔
701
    }
702

703
    if (pIter->backward && p->maxUid < pIter->uid) {
102,354✔
704
      break;
12,240✔
705
    }
706

707
    // check uid firstly
708
    if (p->minUid <= pIter->uid && p->maxUid >= pIter->uid) {
90,114!
709
      if ((!pIter->backward) && p->minKey > pIter->timeWindow.ekey) {
90,129✔
710
        break;
630✔
711
      }
712

713
      if (pIter->backward && p->maxKey < pIter->timeWindow.skey) {
89,499✔
714
        break;
2✔
715
      }
716

717
      // check time range secondly
718
      if (p->minKey <= pIter->timeWindow.ekey && p->maxKey >= pIter->timeWindow.skey) {
89,497✔
719
        if ((!pIter->backward) && p->minVer > pIter->verRange.maxVer) {
87,652!
720
          break;
×
721
        }
722

723
        if (pIter->backward && p->maxVer < pIter->verRange.minVer) {
87,652!
724
          break;
×
725
        }
726

727
        if (p->minVer <= pIter->verRange.maxVer && p->maxVer >= pIter->verRange.minVer) {
87,652!
728
          index = i;
87,652✔
729
          break;
87,652✔
730
        }
731
      }
732
    }
733
  }
734

735
  pIter->pSttBlk = NULL;
8,547,609✔
736
  if (index != -1) {
8,547,609✔
737
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, index);
87,652✔
738

739
    pIter->iSttBlk = index;
87,651✔
740
    pIter->pSttBlk = (SSttBlk *)taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
87,651✔
741
    tsdbDebug("try next stt-file block:%d from %d, trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
87,652✔
742
              ", uidRange:%" PRId64 "-%" PRId64 " %s",
743
              pIter->iSttBlk, oldIndex, pIter->uid, pIter->cid, p->minUid, p->maxUid, idStr);
744
  } else {
745
    tsdbDebug("no more last block qualified, uid:%" PRIu64 ", stt-file block:%d, %s", pIter->uid, oldIndex, idStr);
8,459,957✔
746
  }
747
}
8,547,609✔
748

749
static int32_t findNextValidRow(SLDataIter *pIter, const char *idStr) {
610,468,911✔
750
  bool        hasVal = false;
610,468,911✔
751
  int32_t     step = pIter->backward ? -1 : 1;
610,468,911✔
752
  int32_t     i = pIter->iRow;
610,468,911✔
753
  SBlockData *pData = NULL;
610,468,911✔
754

755
  int32_t code = loadLastBlock(pIter, idStr, &pData);
610,468,911✔
756
  if (code) {
607,530,932!
757
    tsdbError("failed to load stt block, code:%s, %s", tstrerror(code), idStr);
×
758
    return code;
×
759
  }
760

761
  // mostly we only need to find the start position for a given table
762
  if ((((i == 0) && (!pIter->backward)) || (i == pData->nRow - 1 && pIter->backward)) && pData->aUid != NULL) {
607,530,932✔
763
    i = binarySearchForStartRowIndex((uint64_t *)pData->aUid, pData->nRow, pIter->uid, pIter->backward);
8,440,904✔
764
    if (i == -1) {
8,441,027!
765
      tsdbDebug("failed to find the data in pBlockData, uid:%" PRIu64 " , %s", pIter->uid, idStr);
×
766
      pIter->iRow = -1;
61,744✔
767
      return code;
61,744✔
768
    }
769
  }
770

771
  for (; i < pData->nRow && i >= 0; i += step) {
629,976,529!
772
    if (pData->aUid != NULL) {
625,403,924✔
773
      if (!pIter->backward) {
620,921,501✔
774
        if (pData->aUid[i] > pIter->uid) {
576,444,141✔
775
          break;
1,306,074✔
776
        }
777
      } else {
778
        if (pData->aUid[i] < pIter->uid) {
44,477,360✔
779
          break;
43,402✔
780
        }
781
      }
782
    }
783

784
    int64_t ts = pData->aTSKEY[i];
624,054,448✔
785
    if (!pIter->backward) {               // asc
624,054,448✔
786
      if (ts > pIter->timeWindow.ekey) {  // no more data
578,964,169✔
787
        break;
639,128✔
788
      } else {
789
        if (ts < pIter->timeWindow.skey) {
578,325,041✔
790
          continue;
10,120,149✔
791
        }
792

793
        if (ts == pIter->timeWindow.skey && pIter->pStartRowKey->numOfPKs > 0) {
568,204,892✔
794
          SRowKey key;
795
          tColRowGetKey(pData, i, &key);
252!
796
          int32_t ret = pkCompEx(&key, pIter->pStartRowKey);
252✔
797
          if (ret < 0) {
252!
798
            continue;
×
799
          }
800
        }
801
      }
802
    } else {
803
      if (ts < pIter->timeWindow.skey) {
45,090,279✔
804
        break;
371,026✔
805
      } else {
806
        if (ts > pIter->timeWindow.ekey) {
44,719,253✔
807
          continue;
11,580,241✔
808
        }
809

810
        if (ts == pIter->timeWindow.ekey && pIter->pStartRowKey->numOfPKs > 0) {
33,139,012✔
811
          SRowKey key;
812
          tColRowGetKey(pData, i, &key);
4!
813
          int32_t ret = pkCompEx(&key, pIter->pStartRowKey);
4✔
814
          if (ret > 0) {
4!
815
            continue;
×
816
          }
817
        }
818
      }
819
    }
820

821
    int64_t ver = pData->aVersion[i];
601,343,904✔
822
    if (ver < pIter->verRange.minVer) {
601,343,904!
823
      continue;
×
824
    }
825

826
    // todo opt handle desc case
827
    if (ver > pIter->verRange.maxVer) {
601,343,904!
828
      continue;
×
829
    }
830

831
    hasVal = true;
601,343,904✔
832
    break;
601,343,904✔
833
  }
834

835
  pIter->iRow = (hasVal) ? i : -1;
608,276,139✔
836
  return code;
608,276,139✔
837
}
838

839
int32_t tLDataIterNextRow(SLDataIter *pIter, const char *idStr, bool *hasNext) {
625,060,870✔
840
  int32_t     step = pIter->backward ? -1 : 1;
625,060,870✔
841
  int32_t     code = 0;
625,060,870✔
842
  int32_t     iBlockL = pIter->iSttBlk;
625,060,870✔
843
  SBlockData *pBlockData = NULL;
625,060,870✔
844
  int32_t     lino = 0;
625,060,870✔
845

846
  *hasNext = false;
625,060,870✔
847

848
  // no qualified last file block in current file, no need to fetch row
849
  if (pIter->pSttBlk == NULL) {
625,060,870✔
850
    return code;
15,323,894✔
851
  }
852

853
  code = loadLastBlock(pIter, idStr, &pBlockData);
609,736,976✔
854
  if (pBlockData == NULL || code != TSDB_CODE_SUCCESS) {
609,507,519!
855
    lino = __LINE__;
×
856
    goto _exit;
×
857
  }
858

859
  pIter->iRow += step;
610,281,173✔
860

861
  while (1) {
86,897✔
862
    bool skipBlock = false;
610,368,070✔
863
    code = findNextValidRow(pIter, idStr);
610,368,070✔
864
    TSDB_CHECK_CODE(code, lino, _exit);
608,522,956!
865

866
    if (pIter->pBlockLoadInfo->checkRemainingRow) {
608,522,956!
867
      skipBlock = true;
×
868
      int16_t *aCols = pIter->pBlockLoadInfo->colIds;
×
869
      int      nCols = pIter->pBlockLoadInfo->numOfCols;
×
870
      bool     isLast = pIter->pBlockLoadInfo->isLast;
×
871
      for (int inputColIndex = 0; inputColIndex < nCols; ++inputColIndex) {
×
872
        for (int colIndex = 0; colIndex < pBlockData->nColData; ++colIndex) {
×
873
          SColData *pColData = &pBlockData->aColData[colIndex];
×
874
          int16_t   cid = pColData->cid;
×
875

876
          if (cid == aCols[inputColIndex]) {
×
877
            if (isLast && (pColData->flag & HAS_VALUE)) {
×
878
              skipBlock = false;
×
879
              break;
×
880
            } else if (pColData->flag & (HAS_VALUE | HAS_NULL)) {
×
881
              skipBlock = false;
×
882
              break;
×
883
            }
884
          }
885
        }
886
      }
887
    }
888

889
    if (skipBlock || pIter->iRow >= pBlockData->nRow || pIter->iRow < 0) {
608,522,956!
890
      tLDataIterNextBlock(pIter, idStr);
8,452,480✔
891
      if (pIter->pSttBlk == NULL) {  // no more data
8,546,954✔
892
        goto _exit;
8,460,064✔
893
      }
894
    } else {
895
      break;
896
    }
897

898
    if (iBlockL != pIter->iSttBlk) {
86,890!
899
      code = loadLastBlock(pIter, idStr, &pBlockData);
87,652✔
900
      if ((pBlockData == NULL) || (code != 0)) {
87,657!
901
        lino = __LINE__;
×
902
        goto _exit;
×
903
      }
904

905
      // set start row index
906
      pIter->iRow = pIter->backward ? pBlockData->nRow - 1 : 0;
87,659✔
907
    }
908
  }
909

910
  pIter->rInfo.suid = pBlockData->suid;
600,070,476✔
911
  pIter->rInfo.uid = pBlockData->uid;
600,070,476✔
912
  pIter->rInfo.row = tsdbRowFromBlockData(pBlockData, pIter->iRow);
600,070,476✔
913

914
_exit:
607,756,884✔
915
  if (code) {
607,756,884!
916
    tsdbError("failed to exec stt-file nextIter, lino:%d, code:%s, %s", lino, tstrerror(code), idStr);
×
917
  }
918

919
  *hasNext = (code == TSDB_CODE_SUCCESS) && (pIter->pSttBlk != NULL) && (pBlockData != NULL);
608,325,525!
920
  return code;
608,325,525✔
921
}
922

923
// SMergeTree =================================================
924
static FORCE_INLINE int32_t tLDataIterCmprFn(const SRBTreeNode *p1, const SRBTreeNode *p2) {
56,363,517✔
925
  SLDataIter *pIter1 = (SLDataIter *)(((uint8_t *)p1) - offsetof(SLDataIter, node));
56,635,382✔
926
  SLDataIter *pIter2 = (SLDataIter *)(((uint8_t *)p2) - offsetof(SLDataIter, node));
56,635,382✔
927

928
  SRowKey rkey1 = {0}, rkey2 = {0};
56,635,382✔
929
  tRowGetKeyEx(&pIter1->rInfo.row, &rkey1);
56,635,382!
930
  tRowGetKeyEx(&pIter2->rInfo.row, &rkey2);
56,793,222!
931

932
  int32_t ret = tRowKeyCompare(&rkey1, &rkey2);
56,369,096✔
933
  if (ret < 0) {
56,349,727✔
934
    return -1;
36,525,627✔
935
  } else if (ret > 0) {
19,824,100✔
936
    return 1;
8,139,091✔
937
  } else {
938
    int64_t ver1 = TSDBROW_VERSION(&pIter1->rInfo.row);
11,685,009!
939
    int64_t ver2 = TSDBROW_VERSION(&pIter2->rInfo.row);
11,685,009!
940

941
    if (ver1 < ver2) {
11,685,009✔
942
      return -1;
4,728,787✔
943
    } else if (ver1 > ver2) {
6,956,222!
944
      return 1;
7,436,606✔
945
    } else {
946
      return 0;
×
947
    }
948
  }
949
}
950

951
static FORCE_INLINE int32_t tLDataIterDescCmprFn(const SRBTreeNode *p1, const SRBTreeNode *p2) {
271,865!
952
  return -1 * tLDataIterCmprFn(p1, p2);
271,365✔
953
}
954

955
int32_t tMergeTreeOpen2(SMergeTree *pMTree, SMergeTreeConf *pConf, SSttDataInfoForTable *pSttDataInfo) {
24,181,895✔
956
  int32_t code = TSDB_CODE_SUCCESS;
24,181,895✔
957

958
  pMTree->pIter = NULL;
24,181,895✔
959
  pMTree->backward = pConf->backward;
24,181,895✔
960
  pMTree->idStr = pConf->idstr;
24,181,895✔
961
  int32_t lino = 0;
24,181,895✔
962

963
  if (!pMTree->backward) {  // asc
24,181,895✔
964
    tRBTreeCreate(&pMTree->rbt, tLDataIterCmprFn);
20,465,788✔
965
  } else {  // desc
966
    tRBTreeCreate(&pMTree->rbt, tLDataIterDescCmprFn);
3,716,107✔
967
  }
968

969
  pMTree->ignoreEarlierTs = false;
24,188,581✔
970

971
  // no data exists, go to end
972
  int32_t numOfLevels = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->size;
24,188,581✔
973
  if (numOfLevels == 0) {
24,188,581✔
974
    goto _end;
608,715✔
975
  }
976

977
  code = adjustSttDataIters(pConf->pSttFileBlockIterArray, pConf->pCurrentFileset);
23,579,866✔
978
  if (code) {
23,572,149!
979
    goto _end;
×
980
  }
981

982
  for (int32_t j = 0; j < numOfLevels; ++j) {
47,325,768✔
983
    SSttLvl *pSttLevel = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->data[j];
23,744,572✔
984
    SArray  *pList = taosArrayGetP(pConf->pSttFileBlockIterArray, j);
23,744,572✔
985

986
    for (int32_t i = 0; i < TARRAY2_SIZE(pSttLevel->fobjArr); ++i) {  // open all last file
47,547,484✔
987
      SLDataIter *pIter = taosArrayGetP(pList, i);
23,793,865✔
988

989
      SSttFileReader    *pSttFileReader = pIter->pReader;
23,789,625✔
990
      SSttBlockLoadInfo *pLoadInfo = pIter->pBlockLoadInfo;
23,789,625✔
991

992
      // open stt file reader if not opened yet
993
      // if failed to open this stt file, ignore the error and try next one
994
      if (pSttFileReader == NULL) {
23,789,625✔
995
        SSttFileReaderConfig conf = {.tsdb = pConf->pTsdb, .szPage = pConf->pTsdb->pVnode->config.tsdbPageSize};
10,376,632✔
996
        conf.file[0] = *pSttLevel->fobjArr->data[i]->f;
10,376,632✔
997

998
        code = tsdbSttFileReaderOpen(pSttLevel->fobjArr->data[i]->fname, &conf, &pSttFileReader);
10,376,632✔
999
        if (code != TSDB_CODE_SUCCESS) {
10,377,894!
1000
          tsdbError("open stt file reader error. file name %s, code %s, %s", pSttLevel->fobjArr->data[i]->fname,
×
1001
                    tstrerror(code), pMTree->idStr);
1002
        }
1003
      }
1004

1005
      if (pLoadInfo == NULL) {
23,790,758✔
1006
        code = tCreateSttBlockLoadInfo(pConf->pSchema, pConf->pCols, pConf->numOfCols, &pLoadInfo);
10,379,557✔
1007
        if (code != TSDB_CODE_SUCCESS) {
10,378,889!
1008
          goto _end;
×
1009
        }
1010
      }
1011

1012
      memset(pIter, 0, sizeof(SLDataIter));
23,790,090✔
1013

1014
      SSttKeyRange range = {.skey.numOfPKs = pConf->pCurRowKey->numOfPKs, .ekey.numOfPKs = pConf->pCurRowKey->numOfPKs};
23,790,090✔
1015
      int64_t      numOfRows = 0;
23,790,090✔
1016
      int64_t      cid = pSttLevel->fobjArr->data[i]->f->cid;
23,790,090✔
1017

1018
      code = tLDataIterOpen2(pIter, pSttFileReader, cid, pMTree->backward, pConf, pLoadInfo, &range, &numOfRows,
23,790,090✔
1019
                             pMTree->idStr);
1020
      if (code != TSDB_CODE_SUCCESS) {
23,798,661!
1021
        goto _end;
×
1022
      }
1023

1024
      bool hasVal = NULL;
23,798,661✔
1025
      code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
23,798,661✔
1026
      if (code) {
23,810,464!
1027
        goto _end;
×
1028
      }
1029

1030
      if (hasVal) {
23,810,464✔
1031
        tMergeTreeAddIter(pMTree, pIter);
8,416,797✔
1032

1033
        // let's record the time window for current table of uid in the stt files
1034
        if (pSttDataInfo != NULL && numOfRows > 0) {
8,415,998!
1035
          void *px = taosArrayPush(pSttDataInfo->pKeyRangeList, &range);
8,416,198✔
1036
          QUERY_CHECK_NULL(px, code, lino, _end, terrno);
8,415,237!
1037

1038
          pSttDataInfo->numOfRows += numOfRows;
8,415,237✔
1039
        }
1040
      } else {
1041
        if (!pMTree->ignoreEarlierTs) {
15,393,667!
1042
          pMTree->ignoreEarlierTs = pIter->ignoreEarlierTs;
15,395,947✔
1043
        }
1044
      }
1045
    }
1046
  }
1047

1048
  return code;
23,581,196✔
1049

1050
_end:
608,715✔
1051
  tMergeTreeClose(pMTree);
608,715✔
1052
  return code;
608,502✔
1053
}
1054

1055
void tMergeTreeAddIter(SMergeTree *pMTree, SLDataIter *pIter) {
8,415,512✔
1056
  SRBTreeNode *node = tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pIter);
8,415,512✔
1057
}
8,415,944✔
1058

1059
bool tMergeTreeIgnoreEarlierTs(SMergeTree *pMTree) { return pMTree->ignoreEarlierTs; }
×
1060

1061
static void tLDataIterPinSttBlock(SLDataIter *pIter, const char *id) {
299,969,926✔
1062
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
299,969,926✔
1063

1064
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
299,969,926✔
1065
    pInfo->blockData[0].pin = true;
182,047,737✔
1066
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
182,047,737✔
1067
    return;
182,070,401✔
1068
  }
1069

1070
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
117,922,189!
1071
    pInfo->blockData[1].pin = true;
129,151,521✔
1072
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
129,151,521✔
1073
    return;
129,086,176✔
1074
  }
1075

1076
  tsdbError("failed to pin any stt block, sttBlock:%d stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
×
1077
}
1078

1079
static void tLDataIterUnpinSttBlock(SLDataIter *pIter, const char *id) {
294,296,608✔
1080
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
294,296,608✔
1081
  if (pInfo->blockData[0].pin) {
294,296,608✔
1082
    pInfo->blockData[0].pin = false;
179,735,059✔
1083
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[0].sttBlockIndex, pIter->cid, id);
179,735,059✔
1084
    return;
179,755,399✔
1085
  }
1086

1087
  if (pInfo->blockData[1].pin) {
114,561,549!
1088
    pInfo->blockData[1].pin = false;
127,740,947✔
1089
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[1].sttBlockIndex, pIter->cid, id);
127,740,947✔
1090
    return;
127,751,872✔
1091
  }
1092

1093
  tsdbError("failed to unpin any stt block, sttBlock:%d stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
×
1094
}
1095

1096
void tMergeTreePinSttBlock(SMergeTree *pMTree) {
299,984,201✔
1097
  if (pMTree->pIter == NULL) {
299,984,201!
1098
    return;
×
1099
  }
1100

1101
  SLDataIter *pIter = pMTree->pIter;
299,984,201✔
1102
  pMTree->pPinnedBlockIter = pIter;
299,984,201✔
1103
  tLDataIterPinSttBlock(pIter, pMTree->idStr);
299,984,201✔
1104
}
1105

1106
void tMergeTreeUnpinSttBlock(SMergeTree *pMTree) {
294,578,227✔
1107
  if (pMTree->pPinnedBlockIter == NULL) {
294,578,227!
1108
    return;
×
1109
  }
1110

1111
  SLDataIter *pIter = pMTree->pPinnedBlockIter;
294,578,227✔
1112
  pMTree->pPinnedBlockIter = NULL;
294,578,227✔
1113
  tLDataIterUnpinSttBlock(pIter, pMTree->idStr);
294,578,227✔
1114
}
1115

1116
int32_t tMergeTreeNext(SMergeTree *pMTree, bool *pHasNext) {
625,735,357✔
1117
  int32_t code = 0;
625,735,357✔
1118
  if (pHasNext == NULL) {
625,735,357!
1119
    return TSDB_CODE_INVALID_PARA;
×
1120
  }
1121

1122
  *pHasNext = false;
625,735,357✔
1123
  while (pMTree->pIter) {
625,735,357✔
1124
    SLDataIter *pIter = pMTree->pIter;
601,379,211✔
1125
    bool        hasVal = false;
601,379,211✔
1126
    code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
601,379,211✔
1127
    if (!hasVal || (code != 0)) {
599,441,405✔
1128
      if (code == TSDB_CODE_FILE_CORRUPTED) {
8,386,293!
1129
        code = 0;  // suppress the file corrupt error to enable all queries within this cluster can run without failed.
×
1130
      }
1131

1132
      pMTree->pIter = NULL;
8,386,293✔
1133
    }
1134

1135
    // compare with min in RB Tree
1136
    pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
599,441,405✔
1137
    if (pMTree->pIter && pIter) {
599,441,405✔
1138
      int32_t c = pMTree->rbt.cmprFn(&pMTree->pIter->node, &pIter->node);
36,710,772✔
1139
      if (c > 0) {
36,232,295✔
1140
        (void)tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
4,231,534✔
1141
        pMTree->pIter = NULL;
4,668,595✔
1142
      } else if (!c) {
32,000,761!
1143
        continue;
×
1144
      }
1145
    }
1146

1147
    break;
599,399,989✔
1148
  }
1149

1150
  if (pMTree->pIter == NULL) {
623,756,135✔
1151
    pMTree->pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
37,206,262✔
1152
    if (pMTree->pIter) {
37,206,262✔
1153
      tRBTreeDrop(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
13,055,096✔
1154
    }
1155
  }
1156

1157
  *pHasNext = (pMTree->pIter != NULL);
623,454,145✔
1158
  return code;
623,454,145✔
1159
}
1160

1161
void tMergeTreeClose(SMergeTree *pMTree) {
43,011,527✔
1162
  pMTree->pIter = NULL;
43,011,527✔
1163
  pMTree->pPinnedBlockIter = NULL;
43,011,527✔
1164
}
43,011,527✔
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