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

taosdata / TDengine / #4442

04 Jul 2025 02:10AM UTC coverage: 63.58% (+0.3%) from 63.29%
#4442

push

travis-ci

web-flow
fix:(stmt2) heap buffer overflow (#31607)

160719 of 321690 branches covered (49.96%)

Branch coverage included in aggregate %.

19 of 22 new or added lines in 3 files covered. (86.36%)

222 existing lines in 60 files now uncovered.

247667 of 320626 relevant lines covered (77.24%)

17710656.63 hits per line

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

80.64
/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) {
7,610,681✔
27
  *pInfo = NULL;
7,610,681✔
28

29
  SSttBlockLoadInfo *pLoadInfo = taosMemoryCalloc(1, sizeof(SSttBlockLoadInfo));
7,610,681!
30
  if (pLoadInfo == NULL) {
7,615,830!
31
    return terrno;
×
32
  }
33

34
  pLoadInfo->blockData[0].sttBlockIndex = -1;
7,615,830✔
35
  pLoadInfo->blockData[1].sttBlockIndex = -1;
7,615,830✔
36

37
  pLoadInfo->currentLoadBlockIndex = 1;
7,615,830✔
38

39
  int32_t code = tBlockDataCreate(&pLoadInfo->blockData[0].data);
7,615,830✔
40
  if (code) {
7,614,545!
41
    taosMemoryFreeClear(pLoadInfo);
×
42
    return code;
×
43
  }
44

45
  code = tBlockDataCreate(&pLoadInfo->blockData[1].data);
7,614,545✔
46
  if (code) {
7,613,048!
47
    taosMemoryFreeClear(pLoadInfo);
×
48
    return code;
×
49
  }
50

51
  pLoadInfo->aSttBlk = taosArrayInit(4, sizeof(SSttBlk));
7,613,048✔
52
  if (pLoadInfo->aSttBlk == NULL) {
7,613,616✔
53
    taosMemoryFreeClear(pLoadInfo);
619!
54
    return terrno;
619✔
55
  }
56

57
  pLoadInfo->pSchema = pSchema;
7,612,997✔
58
  pLoadInfo->colIds = colList;
7,612,997✔
59
  pLoadInfo->numOfCols = numOfCols;
7,612,997✔
60

61
  *pInfo = pLoadInfo;
7,612,997✔
62
  return code;
7,612,997✔
63
}
64

65
static void freeItem(void *pValue) {
41,216,559✔
66
  SValue *p = (SValue *)pValue;
41,216,559✔
67
  if (IS_VAR_DATA_TYPE(p->type) || p->type == TSDB_DATA_TYPE_DECIMAL) {
41,216,559!
68
    taosMemoryFree(p->pData);
585,420!
69
  }
70
}
41,224,930✔
71

72
void destroySttBlockLoadInfo(SSttBlockLoadInfo *pLoadInfo) {
7,622,242✔
73
  if (pLoadInfo == NULL) {
7,622,242!
74
    return;
×
75
  }
76

77
  pLoadInfo->currentLoadBlockIndex = 1;
7,622,242✔
78

79
  SBlockDataInfo *pInfo = &pLoadInfo->blockData[0];
7,622,242✔
80
  tBlockDataDestroy(&pInfo->data);
7,622,242✔
81
  pInfo->sttBlockIndex = -1;
7,621,010✔
82
  pInfo->pin = false;
7,621,010✔
83

84
  pInfo = &pLoadInfo->blockData[1];
7,621,010✔
85
  tBlockDataDestroy(&pInfo->data);
7,621,010✔
86
  pInfo->sttBlockIndex = -1;
7,620,581✔
87
  pInfo->pin = false;
7,620,581✔
88

89
  taosArrayDestroy(pLoadInfo->info.pUid);
7,620,581✔
90
  taosArrayDestroyEx(pLoadInfo->info.pFirstKey, freeItem);
7,620,585✔
91
  taosArrayDestroyEx(pLoadInfo->info.pLastKey, freeItem);
7,620,602✔
92
  taosArrayDestroy(pLoadInfo->info.pCount);
7,620,701✔
93
  taosArrayDestroy(pLoadInfo->info.pFirstTs);
7,620,569✔
94
  taosArrayDestroy(pLoadInfo->info.pLastTs);
7,620,474✔
95

96
  pLoadInfo->info.pUid = NULL;
7,620,601✔
97
  pLoadInfo->info.pFirstKey = NULL;
7,620,601✔
98
  pLoadInfo->info.pLastKey = NULL;
7,620,601✔
99
  pLoadInfo->info.pCount = NULL;
7,620,601✔
100
  pLoadInfo->info.pFirstTs = NULL;
7,620,601✔
101
  pLoadInfo->info.pLastTs = NULL;
7,620,601✔
102

103
  taosArrayDestroy(pLoadInfo->aSttBlk);
7,620,601✔
104
  taosMemoryFree(pLoadInfo);
7,621,155!
105
}
106

107
void destroyLDataIter(SLDataIter *pIter) {
7,613,736✔
108
  tLDataIterClose2(pIter);
7,613,736✔
109
  destroySttBlockLoadInfo(pIter->pBlockLoadInfo);
7,622,617✔
110
  taosMemoryFree(pIter);
7,621,862!
111
}
7,622,511✔
112

113
void destroySttBlockReader(SArray *pLDataIterArray, SSttBlockLoadCostInfo *pLoadCost) {
13,752,742✔
114
  if (pLDataIterArray == NULL) {
13,752,742✔
115
    return;
65,610✔
116
  }
117

118
  int32_t numOfLevel = taosArrayGetSize(pLDataIterArray);
13,687,132✔
119
  for (int32_t i = 0; i < numOfLevel; ++i) {
21,299,205✔
120
    SArray *pList = taosArrayGetP(pLDataIterArray, i);
7,611,600✔
121
    for (int32_t j = 0; j < taosArrayGetSize(pList); ++j) {
15,232,946✔
122
      SLDataIter *pIter = taosArrayGetP(pList, j);
7,614,154✔
123
      if (pIter->pBlockLoadInfo != NULL) {
7,613,237!
124
        SSttBlockLoadCostInfo *pCost = &pIter->pBlockLoadInfo->cost;
7,613,483✔
125
        if (pLoadCost != NULL) {
7,613,483✔
126
          pLoadCost->loadBlocks += pCost->loadBlocks;
7,605,570✔
127
          pLoadCost->loadStatisBlocks += pCost->loadStatisBlocks;
7,605,570✔
128
          pLoadCost->blockElapsedTime += pCost->blockElapsedTime;
7,605,570✔
129
          pLoadCost->statisElapsedTime += pCost->statisElapsedTime;
7,605,570✔
130
        }
131
      }
132

133
      destroyLDataIter(pIter);
7,613,237✔
134
    }
135

136
    taosArrayDestroy(pList);
7,607,573✔
137
  }
138

139
  taosArrayDestroy(pLDataIterArray);
13,687,605✔
140
}
141

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

149
  pLoadInfo->currentLoadBlockIndex = nextSlotIndex;
4,701,439✔
150
}
4,701,439✔
151

152
static int32_t loadLastBlock(SLDataIter *pIter, const char *idStr, SBlockData **pResBlock) {
1,040,708,796✔
153
  if (pResBlock != NULL) {
1,040,708,796!
154
    *pResBlock = NULL;
1,041,201,162✔
155
  }
156

157
  int32_t            code = 0;
1,040,708,796✔
158
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
1,040,708,796✔
159

160
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
1,040,708,796✔
161
    if (pInfo->currentLoadBlockIndex != 0) {
707,146,105✔
162
      tsdbDebug("current load index is set to 0, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
577✔
163
                ", load data, %s",
164
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
165
      pInfo->currentLoadBlockIndex = 0;
577✔
166
    }
167

168
    *pResBlock = &pInfo->blockData[0].data;
707,146,105✔
169
    return code;
707,146,105✔
170
  }
171

172
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
333,562,691!
173
    if (pInfo->currentLoadBlockIndex != 1) {
352,795,482✔
174
      tsdbDebug("current load index is set to 1, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
477✔
175
                ", load data, %s",
176
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
177
      pInfo->currentLoadBlockIndex = 1;
477✔
178
    }
179

180
    *pResBlock = &pInfo->blockData[1].data;
352,795,482✔
181
    return code;
352,795,482✔
182
  }
183

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

188
  updateBlockLoadSlot(pInfo);
4,702,266✔
189
  int64_t st = taosGetTimestampUs();
4,703,981✔
190

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

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

202
  tsdbDebug("read stt block, total load:%" PRId64 ", trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
4,705,536✔
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;
4,705,372✔
210
  pIter->iRow = (pIter->backward) ? pInfo->blockData[pInfo->currentLoadBlockIndex].data.nRow : -1;
4,705,372✔
211

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

215
  *pResBlock = &pInfo->blockData[pInfo->currentLoadBlockIndex].data;
4,705,498✔
216
  return code;
4,705,498✔
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;
5,953,521✔
223
  int32_t step = backward ? 1 : -1;
5,953,521✔
224
  while (i >= 0 && i < num && uid >= pBlockList[i].minUid && uid <= pBlockList[i].maxUid) {
11,960,225!
225
    i += step;
6,006,704✔
226
  }
227
  return i - step;
5,953,521✔
228
}
229

230
static int32_t binarySearchForStartBlock(SSttBlk *pBlockList, int32_t num, uint64_t uid, int32_t backward) {
15,815,388✔
231
  int32_t midPos = -1;
15,815,388✔
232
  if (num <= 0) {
15,815,388✔
233
    return -1;
5,284,983✔
234
  }
235

236
  int32_t firstPos = 0;
10,530,405✔
237
  int32_t lastPos = num - 1;
10,530,405✔
238

239
  // find the first position which is bigger than the key
240
  if ((uid > pBlockList[lastPos].maxUid) || (uid < pBlockList[firstPos].minUid)) {
10,530,405✔
241
    return -1;
4,576,741✔
242
  }
243

244
  while (1) {
698,974✔
245
    if (uid >= pBlockList[firstPos].minUid && uid <= pBlockList[firstPos].maxUid) {
6,652,638✔
246
      return findEarliestIndex(firstPos, uid, pBlockList, num, backward);
5,815,897✔
247
    }
248

249
    if (uid > pBlockList[lastPos].maxUid || uid < pBlockList[firstPos].minUid) {
836,741!
250
      return -1;
143✔
251
    }
252

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

256
    if (uid < pBlockList[midPos].minUid) {
836,598✔
257
      lastPos = midPos - 1;
339,137✔
258
    } else if (uid > pBlockList[midPos].maxUid) {
497,461✔
259
      firstPos = midPos + 1;
359,837✔
260
    } else {
261
      return findEarliestIndex(midPos, uid, pBlockList, num, backward);
137,624✔
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;
5,817,631✔
269
  int32_t step = backward ? 1 : -1;
5,817,631✔
270
  while (i >= 0 && i < num && uid == uidList[i]) {
112,578,754!
271
    i += step;
106,761,123✔
272
  }
273
  return i - step;
5,817,631✔
274
}
275

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

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

285
  while (1) {
1,973,452✔
286
    if (uid == uidList[firstPos]) {
7,904,592✔
287
      return findEarliestRow(firstPos, uid, uidList, num, backward);
4,496,091✔
288
    }
289

290
    if (uid > uidList[lastPos] || uid < uidList[firstPos]) {
3,408,501✔
291
      return -1;
113,509✔
292
    }
293

294
    int32_t numOfRows = lastPos - firstPos + 1;
3,294,992✔
295
    int32_t midPos = (numOfRows >> 1u) + firstPos;
3,294,992✔
296

297
    if (uid < uidList[midPos]) {
3,294,992✔
298
      lastPos = midPos - 1;
840,958✔
299
    } else if (uid > uidList[midPos]) {
2,454,034✔
300
      firstPos = midPos + 1;
1,132,494✔
301
    } else {
302
      return findEarliestRow(midPos, uid, uidList, num, backward);
1,321,540✔
303
    }
304
  }
305
}
306

307
static int32_t extractSttBlockInfo(SLDataIter *pIter, const TSttBlkArray *pArray, SSttBlockLoadInfo *pBlockLoadInfo,
7,604,598✔
308
                                   uint64_t suid) {
309
  void   *px = NULL;
7,604,598✔
310
  int32_t code = TSDB_CODE_SUCCESS;
7,604,598✔
311
  if (TARRAY2_SIZE(pArray) <= 0) {
7,604,598✔
312
    return code;
9,271✔
313
  }
314

315
  SSttBlk *pStart = &pArray->data[0];
7,595,327✔
316
  SSttBlk *pEnd = &pArray->data[TARRAY2_SIZE(pArray) - 1];
7,595,327✔
317

318
  // all identical
319
  if (pStart->suid == pEnd->suid) {
7,595,327✔
320
    if (pStart->suid != suid) {  // no qualified stt block existed
3,699,008✔
321
      taosArrayClear(pBlockLoadInfo->aSttBlk);
1,872,968✔
322
      pIter->iSttBlk = -1;
1,872,432✔
323
      return TSDB_CODE_SUCCESS;
1,872,432✔
324
    } else {  // all blocks are qualified
325
      taosArrayClear(pBlockLoadInfo->aSttBlk);
1,826,040✔
326
      px = taosArrayAddBatch(pBlockLoadInfo->aSttBlk, pArray->data, pArray->size);
1,827,245✔
327
      if (px == NULL) {
1,826,067!
328
        return terrno;
×
329
      }
330
    }
331
  } else {
332
    SArray *pTmp = taosArrayInit(TARRAY2_SIZE(pArray), sizeof(SSttBlk));
3,896,319✔
333
    if (pTmp == NULL) {
3,903,868!
334
      return terrno;
×
335
    }
336

337
    for (int32_t i = 0; i < TARRAY2_SIZE(pArray); ++i) {
13,716,017✔
338
      SSttBlk *p = &pArray->data[i];
11,128,659✔
339
      if (p->suid < suid) {
11,128,659✔
340
        continue;
6,560,517✔
341
      }
342

343
      if (p->suid == suid) {
4,568,142✔
344
        void *px = taosArrayPush(pTmp, p);
3,252,207✔
345
        if (px == NULL) {
3,252,207!
346
          code = terrno;
×
347
          break;
×
348
        }
349
      } else if (p->suid > suid) {
1,315,915!
350
        break;
1,316,537✔
351
      }
352
    }
353

354
    taosArrayDestroy(pBlockLoadInfo->aSttBlk);
3,903,895✔
355
    pBlockLoadInfo->aSttBlk = pTmp;
3,904,346✔
356
  }
357

358
  return code;
5,730,413✔
359
}
360

361
static int32_t tValueDupPayload(SValue *pVal) {
4,773,983✔
362
  if (IS_VAR_DATA_TYPE(pVal->type) || pVal->type == TSDB_DATA_TYPE_DECIMAL) {
4,773,983!
363
    char *p = (char *)pVal->pData;
592,566✔
364
    char *pBuf = taosMemoryMalloc(pVal->nData);
592,566!
365
    if (pBuf == NULL) {
593,076!
366
      return terrno;
×
367
    }
368

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

373
  return TSDB_CODE_SUCCESS;
4,774,493✔
374
}
375

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

384
  int32_t numOfBlocks = TARRAY2_SIZE(pStatisBlkArray);
7,606,210✔
385
  if (numOfBlocks <= 0) {
7,606,210✔
386
    return code;
9,271✔
387
  }
388

389
  while ((startIndex < numOfBlocks) && (pStatisBlkArray->data[startIndex].maxTbid.suid < suid)) {
9,280,621✔
390
    ++startIndex;
1,683,682✔
391
  }
392

393
  if (startIndex >= numOfBlocks || pStatisBlkArray->data[startIndex].minTbid.suid > suid) {
7,596,939✔
394
    return 0;
2,300,943✔
395
  }
396

397
  int32_t endIndex = startIndex;
5,295,996✔
398
  while (endIndex < numOfBlocks && pStatisBlkArray->data[endIndex].minTbid.suid <= suid) {
10,606,542✔
399
    ++endIndex;
5,310,546✔
400
  }
401

402
  int32_t num = endIndex - startIndex;
5,295,996✔
403
  pBlockLoadInfo->cost.loadStatisBlocks += num;
5,295,996✔
404

405
  STbStatisBlock block;
406
  code = tStatisBlockInit(&block);
5,295,996✔
407
  QUERY_CHECK_CODE(code, lino, _end);
5,295,773!
408

409
  int64_t st = taosGetTimestampUs();
5,295,715✔
410

411
  for (int32_t k = startIndex; k < endIndex; ++k) {
10,588,624✔
412
    code = tsdbSttFileReadStatisBlock(pSttFileReader, &pStatisBlkArray->data[k], &block);
5,306,699✔
413
    QUERY_CHECK_CODE(code, lino, _end);
5,310,607!
414

415
    int32_t i = 0;
5,310,607✔
416
    int32_t rows = block.numOfRecords;
5,310,607✔
417
    while (i < rows && ((int64_t *)block.suids.data)[i] != suid) {
11,320,526✔
418
      ++i;
6,009,919✔
419
    }
420

421
    // existed
422
    if (i < rows) {
5,310,607✔
423
      SSttTableRowsInfo *pInfo = &pBlockLoadInfo->info;
5,027,302✔
424

425
      if (pInfo->pUid == NULL) {
5,027,302✔
426
        pInfo->pUid = taosArrayInit(rows, sizeof(int64_t));
5,013,256✔
427
        pInfo->pFirstTs = taosArrayInit(rows, sizeof(int64_t));
5,013,221✔
428
        pInfo->pLastTs = taosArrayInit(rows, sizeof(int64_t));
5,013,512✔
429
        pInfo->pCount = taosArrayInit(rows, sizeof(int64_t));
5,013,614✔
430

431
        pInfo->pFirstKey = taosArrayInit(rows, sizeof(SValue));
5,013,414✔
432
        pInfo->pLastKey = taosArrayInit(rows, sizeof(SValue));
5,013,434✔
433

434
        if (pInfo->pUid == NULL || pInfo->pFirstTs == NULL || pInfo->pLastTs == NULL || pInfo->pCount == NULL ||
5,013,592!
435
            pInfo->pFirstKey == NULL || pInfo->pLastKey == NULL) {
5,013,629!
UNCOV
436
          code = terrno;
×
437
          goto _end;
×
438
        }
439
      }
440

441
      if (pStatisBlkArray->data[k].maxTbid.suid == suid) {
5,027,681✔
442
        int32_t size = rows - i;
4,085,768✔
443
        int32_t offset = i * sizeof(int64_t);
4,085,768✔
444

445
        px = taosArrayAddBatch(pInfo->pUid, tBufferGetDataAt(&block.uids, offset), size);
4,085,768✔
446
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
4,085,663!
447

448
        px = taosArrayAddBatch(pInfo->pFirstTs, tBufferGetDataAt(&block.firstKeyTimestamps, offset), size);
4,085,663✔
449
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
4,085,314!
450

451
        px = taosArrayAddBatch(pInfo->pLastTs, tBufferGetDataAt(&block.lastKeyTimestamps, offset), size);
4,085,314✔
452
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
4,084,752!
453

454
        px = taosArrayAddBatch(pInfo->pCount, tBufferGetDataAt(&block.counts, offset), size);
4,084,752✔
455
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
4,084,783✔
456

457
        if (block.numOfPKs > 0) {
4,084,410✔
458
          SValue vFirst = {0}, vLast = {0};
319,726✔
459
          for (int32_t f = i; f < rows; ++f) {
2,639,602✔
460
            code = tValueColumnGet(&block.firstKeyPKs[0], f, &vFirst);
2,319,408✔
461
            TSDB_CHECK_CODE(code, lino, _end);
2,319,647!
462

463
            code = tValueDupPayload(&vFirst);
2,319,647✔
464
            TSDB_CHECK_CODE(code, lino, _end);
2,319,841!
465

466
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
2,319,841✔
467
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
2,319,771!
468

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

473
            code = tValueDupPayload(&vLast);
2,319,860✔
474
            TSDB_CHECK_CODE(code, lino, _end);
2,319,893!
475

476
            px = taosArrayPush(pInfo->pLastKey, &vLast);
2,319,893✔
477
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
2,319,876!
478
          }
479
        } else {
480
          SValue vFirst = {0};
3,764,684✔
481
          for (int32_t j = 0; j < size; ++j) {
19,909,532✔
482
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
16,162,053✔
483
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
16,153,371!
484

485
            px = taosArrayPush(pInfo->pLastKey, &vFirst);
16,153,371✔
486
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
16,144,848!
487
          }
488
        }
489
      } else {
490
        STbStatisRecord record = {0};
941,913✔
491
        while (i < rows) {
2,405,942✔
492
          code = tStatisBlockGet(&block, i, &record);
2,405,929✔
493
          TSDB_CHECK_CODE(code, lino, _end);
2,405,785!
494

495
          if (record.suid != suid) {
2,405,785✔
496
            break;
941,918✔
497
          }
498

499
          px = taosArrayPush(pInfo->pUid, &record.uid);
1,463,867✔
500
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,463,972!
501

502
          px = taosArrayPush(pInfo->pCount, &record.count);
1,463,972✔
503
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,463,960!
504

505
          px = taosArrayPush(pInfo->pFirstTs, &record.firstKey.ts);
1,463,960✔
506
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,463,924!
507

508
          px = taosArrayPush(pInfo->pLastTs, &record.lastKey.ts);
1,463,924✔
509
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,463,920!
510

511
          if (record.firstKey.numOfPKs > 0) {
1,463,920✔
512
            SValue s = record.firstKey.pks[0];
68,058✔
513
            code = tValueDupPayload(&s);
68,058✔
514
            TSDB_CHECK_CODE(code, lino, _end);
68,086!
515

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

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

523
            px = taosArrayPush(pInfo->pLastKey, &s);
68,089✔
524
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
68,088!
525
          } else {
526
            SValue v = {0};
1,395,862✔
527
            px = taosArrayPush(pInfo->pFirstKey, &v);
1,395,862✔
528
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,395,951!
529

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

534
          i += 1;
1,464,029✔
535
        }
536
      }
537
    }
538
  }
539

540
_end:
5,281,925✔
541
  tStatisBlockDestroy(&block);
5,281,925✔
542
  if (code != 0) {
5,296,290!
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;
5,296,692✔
546
    pBlockLoadInfo->cost.statisElapsedTime += el;
5,296,692✔
547

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

553
static int32_t doLoadSttFilesBlk(SSttBlockLoadInfo *pBlockLoadInfo, SLDataIter *pIter, int64_t suid,
7,607,478✔
554
                                 _load_tomb_fn loadTombFn, void *pReader1, const char *idStr) {
555
  int64_t st = taosGetTimestampUs();
7,609,885✔
556

557
  const TSttBlkArray *pSttBlkArray = NULL;
7,609,885✔
558
  pBlockLoadInfo->sttBlockLoaded = true;
7,609,885✔
559

560
  // load the stt block info for each stt-block
561
  int32_t code = tsdbSttFileReadSttBlk(pIter->pReader, &pSttBlkArray);
7,609,885✔
562
  if (code != TSDB_CODE_SUCCESS) {
7,608,773!
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);
7,608,773✔
569
  if (code != TSDB_CODE_SUCCESS) {
7,610,189!
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;
7,610,189✔
576
  code = tsdbSttFileReadStatisBlk(pIter->pReader, (const TStatisBlkArray **)&pStatisBlkArray);
7,610,189✔
577
  if (code != TSDB_CODE_SUCCESS) {
7,609,810!
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);
7,609,810✔
584
  if (code != TSDB_CODE_SUCCESS) {
7,611,053!
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);
7,611,053✔
590

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

596
static int32_t uidComparFn(const void *p1, const void *p2) {
14,121,825✔
597
  const uint64_t *pFirst = p1;
14,121,825✔
598
  const uint64_t *pVal = p2;
14,121,825✔
599

600
  if (*pFirst == *pVal) {
14,121,825✔
601
    return 0;
5,855,781✔
602
  } else {
603
    return *pFirst < *pVal ? -1 : 1;
8,266,044✔
604
  }
605
}
606

607
static void setSttInfoForCurrentTable(SSttBlockLoadInfo *pLoadInfo, uint64_t uid, SSttKeyRange *pRange,
15,834,232✔
608
                                      int64_t *numOfRows) {
609
  if (pRange == NULL || taosArrayGetSize(pLoadInfo->info.pUid) == 0) {
15,834,232!
610
    return;
5,288,113✔
611
  }
612

613
  int32_t index = taosArraySearchIdx(pLoadInfo->info.pUid, &uid, uidComparFn, TD_EQ);
10,537,914✔
614
  if (index >= 0) {
10,546,280✔
615
    pRange->skey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pFirstTs, index);
5,856,393✔
616
    pRange->ekey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pLastTs, index);
5,853,297✔
617

618
    *numOfRows += *(int64_t *)taosArrayGet(pLoadInfo->info.pCount, index);
5,853,265✔
619

620
    if (pRange->skey.numOfPKs > 0) {
5,852,726✔
621
      memcpy(&pRange->skey.pks[0], taosArrayGet(pLoadInfo->info.pFirstKey, index), sizeof(SValue));
682,997✔
622
      memcpy(&pRange->ekey.pks[0], taosArrayGet(pLoadInfo->info.pLastKey, index), sizeof(SValue));
682,812✔
623
    }
624
  }
625
}
626

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

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

640
  pIter->pStartRowKey = pConf->pCurRowKey;
15,834,242✔
641
  pIter->pReader = pSttFileReader;
15,834,242✔
642
  pIter->pBlockLoadInfo = pBlockLoadInfo;
15,834,242✔
643

644
  // open stt file failed, ignore and continue
645
  if (pIter->pReader == NULL) {
15,834,242!
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) {
15,834,242✔
653
    code = doLoadSttFilesBlk(pBlockLoadInfo, pIter, pConf->suid, pConf->loadTombFn, pConf->pReader, idStr);
7,610,955✔
654
    if (code != TSDB_CODE_SUCCESS) {
7,609,235!
655
      return code;
×
656
    }
657
  }
658

659
  setSttInfoForCurrentTable(pBlockLoadInfo, pConf->uid, pKeyRange, numOfRows);
15,832,522✔
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);
15,821,425✔
664
  pIter->iSttBlk = binarySearchForStartBlock(pBlockLoadInfo->aSttBlk->pData, size, pConf->uid, backward);
15,816,339✔
665
  if (pIter->iSttBlk != -1) {
15,816,706✔
666
    pIter->pSttBlk = taosArrayGet(pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
5,968,476✔
667
    pIter->iRow = (pIter->backward) ? pIter->pSttBlk->nRow : -1;
5,966,695✔
668

669
    if ((!backward) && ((pConf->strictTimeRange && pIter->pSttBlk->minKey >= pIter->timeWindow.ekey) ||
5,966,695!
670
                        (!pConf->strictTimeRange && pIter->pSttBlk->minKey > pIter->timeWindow.ekey))) {
5,040,909✔
671
      pIter->pSttBlk = NULL;
1,241✔
672
    }
673

674
    if (backward && ((pConf->strictTimeRange && pIter->pSttBlk->maxKey <= pIter->timeWindow.skey) ||
5,966,695!
675
                     (!pConf->strictTimeRange && pIter->pSttBlk->maxKey < pIter->timeWindow.skey))) {
926,233!
676
      pIter->pSttBlk = NULL;
377✔
677
      pIter->ignoreEarlierTs = true;
377✔
678
    }
679
  }
680

681
  return code;
15,814,925✔
682
}
683

684
void tLDataIterClose2(SLDataIter *pIter) {
7,614,135✔
685
  tsdbSttFileReaderClose(&pIter->pReader);
7,614,135✔
686
  pIter->pReader = NULL;
7,622,677✔
687
}
7,622,677✔
688

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

693
  pIter->iSttBlk += step;
6,018,627✔
694

695
  int32_t index = -1;
6,018,627✔
696
  size_t  size = pIter->pBlockLoadInfo->aSttBlk->size;
6,018,627✔
697
  for (int32_t i = pIter->iSttBlk; i < size && i >= 0; i += step) {
6,021,521!
698
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, i);
329,583✔
699
    if ((!pIter->backward) && p->minUid > pIter->uid) {
329,562✔
700
      break;
217,874✔
701
    }
702

703
    if (pIter->backward && p->maxUid < pIter->uid) {
111,688✔
704
      break;
15,124✔
705
    }
706

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

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

717
      // check time range secondly
718
      if (p->minKey <= pIter->timeWindow.ekey && p->maxKey >= pIter->timeWindow.skey) {
95,936✔
719
        if ((!pIter->backward) && p->minVer > pIter->verRange.maxVer) {
93,030✔
720
          break;
392✔
721
        }
722

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

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

735
  pIter->pSttBlk = NULL;
6,018,606✔
736
  if (index != -1) {
6,018,606✔
737
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, index);
92,640✔
738

739
    pIter->iSttBlk = index;
92,636✔
740
    pIter->pSttBlk = (SSttBlk *)taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
92,636✔
741
    tsdbDebug("try next stt-file block:%d from %d, trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
92,636✔
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);
5,925,966✔
746
  }
747
}
6,018,603✔
748

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

755
  int32_t code = loadLastBlock(pIter, idStr, &pData);
533,700,474✔
756
  if (code) {
530,371,531!
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) {
530,371,531✔
763
    i = binarySearchForStartRowIndex((uint64_t *)pData->aUid, pData->nRow, pIter->uid, pIter->backward);
5,931,701✔
764
    if (i == -1) {
5,932,273!
765
      tsdbDebug("failed to find the data in pBlockData, uid:%" PRIu64 " , %s", pIter->uid, idStr);
×
766
      pIter->iRow = -1;
113,650✔
767
      return code;
113,650✔
768
    }
769
  }
770

771
  for (; i < pData->nRow && i >= 0; i += step) {
549,016,881!
772
    if (pData->aUid != NULL) {
547,126,000✔
773
      if (!pIter->backward) {
542,856,176✔
774
        if (pData->aUid[i] > pIter->uid) {
500,317,109✔
775
          break;
1,200,814✔
776
        }
777
      } else {
778
        if (pData->aUid[i] < pIter->uid) {
42,539,067✔
779
          break;
239,127✔
780
        }
781
      }
782
    }
783

784
    int64_t ts = pData->aTSKEY[i];
545,686,059✔
785
    if (!pIter->backward) {               // asc
545,686,059✔
786
      if (ts > pIter->timeWindow.ekey) {  // no more data
502,650,956✔
787
        break;
310,119✔
788
      } else {
789
        if (ts < pIter->timeWindow.skey) {
502,340,837✔
790
          continue;
10,324,232✔
791
        }
792

793
        if (ts == pIter->timeWindow.skey && pIter->pStartRowKey->numOfPKs > 0) {
492,016,605✔
794
          SRowKey key;
795
          tColRowGetKey(pData, i, &key);
258!
796
          int32_t ret = pkCompEx(&key, pIter->pStartRowKey);
258✔
797
          if (ret < 0) {
258!
798
            continue;
×
799
          }
800
        }
801
      }
802
    } else {
803
      if (ts < pIter->timeWindow.skey) {
43,035,103✔
804
        break;
191,506✔
805
      } else {
806
        if (ts > pIter->timeWindow.ekey) {
42,843,597✔
807
          continue;
7,807,148✔
808
        }
809

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

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

826
    // todo opt handle desc case
827
    if (ver > pIter->verRange.maxVer) {
527,053,054✔
828
      continue;
50,492✔
829
    }
830

831
    hasVal = true;
527,002,562✔
832
    break;
527,002,562✔
833
  }
834

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

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

846
  *hasNext = false;
540,742,470✔
847

848
  // no qualified last file block in current file, no need to fetch row
849
  if (pIter->pSttBlk == NULL) {
540,742,470✔
850
    return code;
9,862,514✔
851
  }
852

853
  code = loadLastBlock(pIter, idStr, &pBlockData);
530,879,956✔
854
  if (pBlockData == NULL || code != TSDB_CODE_SUCCESS) {
533,065,261!
855
    lino = __LINE__;
×
856
    goto _exit;
×
857
  }
858

859
  pIter->iRow += step;
533,538,181✔
860

861
  while (1) {
91,432✔
862
    bool skipBlock = false;
533,629,613✔
863
    code = findNextValidRow(pIter, idStr);
533,629,613✔
864
    TSDB_CHECK_CODE(code, lino, _exit);
531,720,127!
865

866
    if (pIter->pBlockLoadInfo->checkRemainingRow) {
531,720,127!
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) {
531,720,127!
890
      tLDataIterNextBlock(pIter, idStr);
5,684,014✔
891
      if (pIter->pSttBlk == NULL) {  // no more data
6,017,527✔
892
        goto _exit;
5,926,099✔
893
      }
894
    } else {
895
      break;
896
    }
897

898
    if (iBlockL != pIter->iSttBlk) {
91,428!
899
      code = loadLastBlock(pIter, idStr, &pBlockData);
92,636✔
900
      if ((pBlockData == NULL) || (code != 0)) {
92,639!
901
        lino = __LINE__;
×
902
        goto _exit;
×
903
      }
904

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

910
  pIter->rInfo.suid = pBlockData->suid;
526,036,113✔
911
  pIter->rInfo.uid = pBlockData->uid;
526,036,113✔
912
  pIter->rInfo.row = tsdbRowFromBlockData(pBlockData, pIter->iRow);
526,036,113✔
913

914
_exit:
531,489,291✔
915
  if (code) {
531,489,291!
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);
531,762,876!
920
  return code;
531,762,876✔
921
}
922

923
// SMergeTree =================================================
924
static FORCE_INLINE int32_t tLDataIterCmprFn(const SRBTreeNode *p1, const SRBTreeNode *p2) {
71,213,713✔
925
  SLDataIter *pIter1 = (SLDataIter *)(((uint8_t *)p1) - offsetof(SLDataIter, node));
71,471,002✔
926
  SLDataIter *pIter2 = (SLDataIter *)(((uint8_t *)p2) - offsetof(SLDataIter, node));
71,471,002✔
927

928
  SRowKey rkey1 = {0}, rkey2 = {0};
71,471,002✔
929
  tRowGetKeyEx(&pIter1->rInfo.row, &rkey1);
71,471,002!
930
  tRowGetKeyEx(&pIter2->rInfo.row, &rkey2);
71,936,986!
931

932
  int32_t ret = tRowKeyCompare(&rkey1, &rkey2);
71,437,033✔
933
  if (ret < 0) {
71,457,389✔
934
    return -1;
50,797,748✔
935
  } else if (ret > 0) {
20,659,641✔
936
    return 1;
8,382,937✔
937
  } else {
938
    int64_t ver1 = TSDBROW_VERSION(&pIter1->rInfo.row);
12,276,704!
939
    int64_t ver2 = TSDBROW_VERSION(&pIter2->rInfo.row);
12,276,704!
940

941
    if (ver1 < ver2) {
12,276,704✔
942
      return -1;
4,971,879✔
943
    } else if (ver1 > ver2) {
7,304,825!
944
      return 1;
7,852,465✔
945
    } else {
946
      return 0;
×
947
    }
948
  }
949
}
950

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

955
int32_t tMergeTreeOpen2(SMergeTree *pMTree, SMergeTreeConf *pConf, SSttDataInfoForTable *pSttDataInfo) {
16,196,784✔
956
  int32_t code = TSDB_CODE_SUCCESS;
16,196,784✔
957

958
  pMTree->pIter = NULL;
16,196,784✔
959
  pMTree->backward = pConf->backward;
16,196,784✔
960
  pMTree->idStr = pConf->idstr;
16,196,784✔
961
  int32_t lino = 0;
16,196,784✔
962

963
  if (!pMTree->backward) {  // asc
16,196,784✔
964
    tRBTreeCreate(&pMTree->rbt, tLDataIterCmprFn);
12,801,892✔
965
  } else {  // desc
966
    tRBTreeCreate(&pMTree->rbt, tLDataIterDescCmprFn);
3,394,892✔
967
  }
968

969
  pMTree->ignoreEarlierTs = false;
16,197,607✔
970

971
  // no data exists, go to end
972
  int32_t numOfLevels = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->size;
16,197,607✔
973
  if (numOfLevels == 0) {
16,197,607✔
974
    goto _end;
631,519✔
975
  }
976

977
  code = adjustSttDataIters(pConf->pSttFileBlockIterArray, pConf->pCurrentFileset);
15,566,088✔
978
  if (code) {
15,561,466!
979
    goto _end;
×
980
  }
981

982
  for (int32_t j = 0; j < numOfLevels; ++j) {
31,351,692✔
983
    SSttLvl *pSttLevel = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->data[j];
15,788,254✔
984
    SArray  *pList = taosArrayGetP(pConf->pSttFileBlockIterArray, j);
15,788,254✔
985

986
    for (int32_t i = 0; i < TARRAY2_SIZE(pSttLevel->fobjArr); ++i) {  // open all last file
31,606,390✔
987
      SLDataIter *pIter = taosArrayGetP(pList, i);
15,816,164✔
988

989
      SSttFileReader    *pSttFileReader = pIter->pReader;
15,810,411✔
990
      SSttBlockLoadInfo *pLoadInfo = pIter->pBlockLoadInfo;
15,810,411✔
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) {
15,810,411✔
995
        SSttFileReaderConfig conf = {.tsdb = pConf->pTsdb, .szPage = pConf->pTsdb->pVnode->config.tsdbPageSize};
7,604,592✔
996
        conf.file[0] = *pSttLevel->fobjArr->data[i]->f;
7,604,592✔
997

998
        code = tsdbSttFileReaderOpen(pSttLevel->fobjArr->data[i]->fname, &conf, &pSttFileReader);
7,604,592✔
999
        if (code != TSDB_CODE_SUCCESS) {
7,607,086!
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) {
15,814,339✔
1006
        code = tCreateSttBlockLoadInfo(pConf->pSchema, pConf->pCols, pConf->numOfCols, &pLoadInfo);
7,612,692✔
1007
        if (code != TSDB_CODE_SUCCESS) {
7,608,116!
1008
          goto _end;
×
1009
        }
1010
      }
1011

1012
      memset(pIter, 0, sizeof(SLDataIter));
15,809,763✔
1013

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

1018
      code = tLDataIterOpen2(pIter, pSttFileReader, cid, pMTree->backward, pConf, pLoadInfo, &range, &numOfRows,
15,809,763✔
1019
                             pMTree->idStr);
1020
      if (code != TSDB_CODE_SUCCESS) {
15,814,901!
1021
        goto _end;
×
1022
      }
1023

1024
      bool hasVal = NULL;
15,814,901✔
1025
      code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
15,814,901✔
1026
      if (code) {
15,823,987!
1027
        goto _end;
×
1028
      }
1029

1030
      if (hasVal) {
15,823,987✔
1031
        tMergeTreeAddIter(pMTree, pIter);
5,843,699✔
1032

1033
        // let's record the time window for current table of uid in the stt files
1034
        if (pSttDataInfo != NULL && numOfRows > 0) {
5,843,193!
1035
          void *px = taosArrayPush(pSttDataInfo->pKeyRangeList, &range);
5,842,676✔
1036
          QUERY_CHECK_NULL(px, code, lino, _end, terrno);
5,841,739!
1037

1038
          pSttDataInfo->numOfRows += numOfRows;
5,841,739✔
1039
        }
1040
      } else {
1041
        if (!pMTree->ignoreEarlierTs) {
9,980,288!
1042
          pMTree->ignoreEarlierTs = pIter->ignoreEarlierTs;
9,984,167✔
1043
        }
1044
      }
1045
    }
1046
  }
1047

1048
  return code;
15,563,438✔
1049

1050
_end:
631,519✔
1051
  tMergeTreeClose(pMTree);
631,519✔
1052
  return code;
631,463✔
1053
}
1054

1055
void tMergeTreeAddIter(SMergeTree *pMTree, SLDataIter *pIter) {
5,842,387✔
1056
  SRBTreeNode *node = tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pIter);
5,842,387✔
1057
}
5,843,065✔
1058

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

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

1064
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
344,283,099✔
1065
    pInfo->blockData[0].pin = true;
199,550,272✔
1066
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
199,550,272✔
1067
    return;
199,584,954✔
1068
  }
1069

1070
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
144,732,827!
1071
    pInfo->blockData[1].pin = true;
149,494,211✔
1072
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
149,494,211✔
1073
    return;
149,502,248✔
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) {
337,656,081✔
1080
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
337,656,081✔
1081
  if (pInfo->blockData[0].pin) {
337,656,081✔
1082
    pInfo->blockData[0].pin = false;
197,003,612✔
1083
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[0].sttBlockIndex, pIter->cid, id);
197,003,612✔
1084
    return;
197,035,671✔
1085
  }
1086

1087
  if (pInfo->blockData[1].pin) {
140,652,469!
1088
    pInfo->blockData[1].pin = false;
147,772,004✔
1089
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[1].sttBlockIndex, pIter->cid, id);
147,772,004✔
1090
    return;
147,786,709✔
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) {
344,290,337✔
1097
  if (pMTree->pIter == NULL) {
344,290,337!
1098
    return;
×
1099
  }
1100

1101
  SLDataIter *pIter = pMTree->pIter;
344,290,337✔
1102
  pMTree->pPinnedBlockIter = pIter;
344,290,337✔
1103
  tLDataIterPinSttBlock(pIter, pMTree->idStr);
344,290,337✔
1104
}
1105

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

1111
  SLDataIter *pIter = pMTree->pPinnedBlockIter;
337,719,985✔
1112
  pMTree->pPinnedBlockIter = NULL;
337,719,985✔
1113
  tLDataIterUnpinSttBlock(pIter, pMTree->idStr);
337,719,985✔
1114
}
1115

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

1122
  *pHasNext = false;
540,989,314✔
1123
  while (pMTree->pIter) {
540,989,314✔
1124
    SLDataIter *pIter = pMTree->pIter;
524,675,284✔
1125
    bool        hasVal = false;
524,675,284✔
1126
    code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
524,675,284✔
1127
    if (!hasVal || (code != 0)) {
525,370,319✔
1128
      if (code == TSDB_CODE_FILE_CORRUPTED) {
5,800,995!
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;
5,800,995✔
1133
    }
1134

1135
    // compare with min in RB Tree
1136
    pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
525,370,319✔
1137
    if (pMTree->pIter && pIter) {
525,370,319✔
1138
      int32_t c = pMTree->rbt.cmprFn(&pMTree->pIter->node, &pIter->node);
50,608,727✔
1139
      if (c > 0) {
50,388,085✔
1140
        (void)tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
4,129,744✔
1141
        pMTree->pIter = NULL;
4,875,050✔
1142
      } else if (!c) {
46,258,341!
1143
        continue;
×
1144
      }
1145
    }
1146

1147
    break;
525,894,983✔
1148
  }
1149

1150
  if (pMTree->pIter == NULL) {
542,209,013✔
1151
    pMTree->pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
26,818,883✔
1152
    if (pMTree->pIter) {
26,818,883✔
1153
      tRBTreeDrop(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
10,679,760✔
1154
    }
1155
  }
1156

1157
  *pHasNext = (pMTree->pIter != NULL);
542,137,486✔
1158
  return code;
542,137,486✔
1159
}
1160

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