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

taosdata / TDengine / #4488

12 Jul 2025 07:47AM UTC coverage: 62.207% (-0.7%) from 62.948%
#4488

push

travis-ci

web-flow
docs: update stream docs (#31822)

157961 of 324087 branches covered (48.74%)

Branch coverage included in aggregate %.

244465 of 322830 relevant lines covered (75.73%)

6561668.76 hits per line

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

79.15
/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) {
2,905,073✔
27
  *pInfo = NULL;
2,905,073✔
28

29
  SSttBlockLoadInfo *pLoadInfo = taosMemoryCalloc(1, sizeof(SSttBlockLoadInfo));
2,905,073!
30
  if (pLoadInfo == NULL) {
2,909,407!
31
    return terrno;
×
32
  }
33

34
  pLoadInfo->blockData[0].sttBlockIndex = -1;
2,909,407✔
35
  pLoadInfo->blockData[1].sttBlockIndex = -1;
2,909,407✔
36

37
  pLoadInfo->currentLoadBlockIndex = 1;
2,909,407✔
38

39
  int32_t code = tBlockDataCreate(&pLoadInfo->blockData[0].data);
2,909,407✔
40
  if (code) {
2,908,705!
41
    taosMemoryFreeClear(pLoadInfo);
×
42
    return code;
×
43
  }
44

45
  code = tBlockDataCreate(&pLoadInfo->blockData[1].data);
2,908,705✔
46
  if (code) {
2,906,880!
47
    taosMemoryFreeClear(pLoadInfo);
×
48
    return code;
×
49
  }
50

51
  pLoadInfo->aSttBlk = taosArrayInit(4, sizeof(SSttBlk));
2,906,880✔
52
  if (pLoadInfo->aSttBlk == NULL) {
2,908,803✔
53
    taosMemoryFreeClear(pLoadInfo);
433!
54
    return terrno;
433✔
55
  }
56

57
  pLoadInfo->pSchema = pSchema;
2,908,370✔
58
  pLoadInfo->colIds = colList;
2,908,370✔
59
  pLoadInfo->numOfCols = numOfCols;
2,908,370✔
60

61
  *pInfo = pLoadInfo;
2,908,370✔
62
  return code;
2,908,370✔
63
}
64

65
static void freeItem(void *pValue) {
29,503,007✔
66
  SValue *p = (SValue *)pValue;
29,503,007✔
67
  if (IS_VAR_DATA_TYPE(p->type) || p->type == TSDB_DATA_TYPE_DECIMAL) {
29,503,007!
68
    taosMemoryFree(p->pData);
360,724!
69
  }
70
}
29,509,697✔
71

72
void destroySttBlockLoadInfo(SSttBlockLoadInfo *pLoadInfo) {
2,911,592✔
73
  if (pLoadInfo == NULL) {
2,911,592!
74
    return;
×
75
  }
76

77
  pLoadInfo->currentLoadBlockIndex = 1;
2,911,592✔
78

79
  SBlockDataInfo *pInfo = &pLoadInfo->blockData[0];
2,911,592✔
80
  tBlockDataDestroy(&pInfo->data);
2,911,592✔
81
  pInfo->sttBlockIndex = -1;
2,910,736✔
82
  pInfo->pin = false;
2,910,736✔
83

84
  pInfo = &pLoadInfo->blockData[1];
2,910,736✔
85
  tBlockDataDestroy(&pInfo->data);
2,910,736✔
86
  pInfo->sttBlockIndex = -1;
2,910,513✔
87
  pInfo->pin = false;
2,910,513✔
88

89
  taosArrayDestroy(pLoadInfo->info.pUid);
2,910,513✔
90
  taosArrayDestroyEx(pLoadInfo->info.pFirstKey, freeItem);
2,910,253✔
91
  taosArrayDestroyEx(pLoadInfo->info.pLastKey, freeItem);
2,910,561✔
92
  taosArrayDestroy(pLoadInfo->info.pCount);
2,910,674✔
93
  taosArrayDestroy(pLoadInfo->info.pFirstTs);
2,910,449✔
94
  taosArrayDestroy(pLoadInfo->info.pLastTs);
2,910,554✔
95

96
  pLoadInfo->info.pUid = NULL;
2,910,579✔
97
  pLoadInfo->info.pFirstKey = NULL;
2,910,579✔
98
  pLoadInfo->info.pLastKey = NULL;
2,910,579✔
99
  pLoadInfo->info.pCount = NULL;
2,910,579✔
100
  pLoadInfo->info.pFirstTs = NULL;
2,910,579✔
101
  pLoadInfo->info.pLastTs = NULL;
2,910,579✔
102

103
  taosArrayDestroy(pLoadInfo->aSttBlk);
2,910,579✔
104
  taosMemoryFree(pLoadInfo);
2,910,652!
105
}
106

107
void destroyLDataIter(SLDataIter *pIter) {
2,906,846✔
108
  tLDataIterClose2(pIter);
2,906,846✔
109
  destroySttBlockLoadInfo(pIter->pBlockLoadInfo);
2,911,855✔
110
  taosMemoryFree(pIter);
2,911,262!
111
}
2,911,829✔
112

113
void destroySttBlockReader(SArray *pLDataIterArray, SSttBlockLoadCostInfo *pLoadCost) {
4,397,246✔
114
  if (pLDataIterArray == NULL) {
4,397,246✔
115
    return;
63,444✔
116
  }
117

118
  int32_t numOfLevel = taosArrayGetSize(pLDataIterArray);
4,333,802✔
119
  for (int32_t i = 0; i < numOfLevel; ++i) {
7,244,158✔
120
    SArray *pList = taosArrayGetP(pLDataIterArray, i);
2,906,322✔
121
    for (int32_t j = 0; j < taosArrayGetSize(pList); ++j) {
5,817,830✔
122
      SLDataIter *pIter = taosArrayGetP(pList, j);
2,906,863✔
123
      if (pIter->pBlockLoadInfo != NULL) {
2,906,334!
124
        SSttBlockLoadCostInfo *pCost = &pIter->pBlockLoadInfo->cost;
2,906,564✔
125
        if (pLoadCost != NULL) {
2,906,564✔
126
          pLoadCost->loadBlocks += pCost->loadBlocks;
2,903,839✔
127
          pLoadCost->loadStatisBlocks += pCost->loadStatisBlocks;
2,903,839✔
128
          pLoadCost->blockElapsedTime += pCost->blockElapsedTime;
2,903,839✔
129
          pLoadCost->statisElapsedTime += pCost->statisElapsedTime;
2,903,839✔
130
        }
131
      }
132

133
      destroyLDataIter(pIter);
2,906,334✔
134
    }
135

136
    taosArrayDestroy(pList);
2,904,704✔
137
  }
138

139
  taosArrayDestroy(pLDataIterArray);
4,337,836✔
140
}
141

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

149
  pLoadInfo->currentLoadBlockIndex = nextSlotIndex;
1,576,793✔
150
}
1,576,793✔
151

152
static int32_t loadLastBlock(SLDataIter *pIter, const char *idStr, SBlockData **pResBlock) {
750,401,669✔
153
  if (pResBlock != NULL) {
750,401,669!
154
    *pResBlock = NULL;
750,786,724✔
155
  }
156

157
  int32_t            code = 0;
750,401,669✔
158
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
750,401,669✔
159

160
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
750,401,669✔
161
    if (pInfo->currentLoadBlockIndex != 0) {
416,817,895✔
162
      tsdbDebug("current load index is set to 0, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
671✔
163
                ", load data, %s",
164
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
165
      pInfo->currentLoadBlockIndex = 0;
671✔
166
    }
167

168
    *pResBlock = &pInfo->blockData[0].data;
416,817,895✔
169
    return code;
416,817,895✔
170
  }
171

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

180
    *pResBlock = &pInfo->blockData[1].data;
357,187,317✔
181
    return code;
357,187,317✔
182
  }
183

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

188
  updateBlockLoadSlot(pInfo);
1,577,496✔
189
  int64_t st = taosGetTimestampUs();
1,578,125✔
190

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

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

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

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

215
  *pResBlock = &pInfo->blockData[pInfo->currentLoadBlockIndex].data;
1,579,236✔
216
  return code;
1,579,236✔
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;
2,147,271✔
223
  int32_t step = backward ? 1 : -1;
2,147,271✔
224
  while (i >= 0 && i < num && uid >= pBlockList[i].minUid && uid <= pBlockList[i].maxUid) {
4,344,049!
225
    i += step;
2,196,778✔
226
  }
227
  return i - step;
2,147,271✔
228
}
229

230
static int32_t binarySearchForStartBlock(SSttBlk *pBlockList, int32_t num, uint64_t uid, int32_t backward) {
4,644,761✔
231
  int32_t midPos = -1;
4,644,761✔
232
  if (num <= 0) {
4,644,761✔
233
    return -1;
1,774,425✔
234
  }
235

236
  int32_t firstPos = 0;
2,870,336✔
237
  int32_t lastPos = num - 1;
2,870,336✔
238

239
  // find the first position which is bigger than the key
240
  if ((uid > pBlockList[lastPos].maxUid) || (uid < pBlockList[firstPos].minUid)) {
2,870,336✔
241
    return -1;
722,932✔
242
  }
243

244
  while (1) {
288,968✔
245
    if (uid >= pBlockList[firstPos].minUid && uid <= pBlockList[firstPos].maxUid) {
2,436,372✔
246
      return findEarliestIndex(firstPos, uid, pBlockList, num, backward);
2,027,145✔
247
    }
248

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

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

256
    if (uid < pBlockList[midPos].minUid) {
409,094✔
257
      lastPos = midPos - 1;
133,480✔
258
    } else if (uid > pBlockList[midPos].maxUid) {
275,614✔
259
      firstPos = midPos + 1;
155,488✔
260
    } else {
261
      return findEarliestIndex(midPos, uid, pBlockList, num, backward);
120,126✔
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;
2,138,932✔
269
  int32_t step = backward ? 1 : -1;
2,138,932✔
270
  while (i >= 0 && i < num && uid == uidList[i]) {
75,727,151!
271
    i += step;
73,588,219✔
272
  }
273
  return i - step;
2,138,932✔
274
}
275

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

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

285
  while (1) {
1,181,376✔
286
    if (uid == uidList[firstPos]) {
3,351,600✔
287
      return findEarliestRow(firstPos, uid, uidList, num, backward);
1,439,309✔
288
    }
289

290
    if (uid > uidList[lastPos] || uid < uidList[firstPos]) {
1,912,291✔
291
      return -1;
31,292✔
292
    }
293

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

297
    if (uid < uidList[midPos]) {
1,880,999✔
298
      lastPos = midPos - 1;
422,789✔
299
    } else if (uid > uidList[midPos]) {
1,458,210✔
300
      firstPos = midPos + 1;
758,587✔
301
    } else {
302
      return findEarliestRow(midPos, uid, uidList, num, backward);
699,623✔
303
    }
304
  }
305
}
306

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

315
  SSttBlk *pStart = &pArray->data[0];
2,905,467✔
316
  SSttBlk *pEnd = &pArray->data[TARRAY2_SIZE(pArray) - 1];
2,905,467✔
317

318
  // all identical
319
  if (pStart->suid == pEnd->suid) {
2,905,467✔
320
    if (pStart->suid != suid) {  // no qualified stt block existed
2,386,524✔
321
      taosArrayClear(pBlockLoadInfo->aSttBlk);
928,500✔
322
      pIter->iSttBlk = -1;
928,044✔
323
      return TSDB_CODE_SUCCESS;
928,044✔
324
    } else {  // all blocks are qualified
325
      taosArrayClear(pBlockLoadInfo->aSttBlk);
1,458,024✔
326
      px = taosArrayAddBatch(pBlockLoadInfo->aSttBlk, pArray->data, pArray->size);
1,458,346✔
327
      if (px == NULL) {
1,456,780!
328
        return terrno;
×
329
      }
330
    }
331
  } else {
332
    SArray *pTmp = taosArrayInit(TARRAY2_SIZE(pArray), sizeof(SSttBlk));
518,943✔
333
    if (pTmp == NULL) {
522,286!
334
      return terrno;
×
335
    }
336

337
    for (int32_t i = 0; i < TARRAY2_SIZE(pArray); ++i) {
1,749,788✔
338
      SSttBlk *p = &pArray->data[i];
1,477,748✔
339
      if (p->suid < suid) {
1,477,748✔
340
        continue;
841,434✔
341
      }
342

343
      if (p->suid == suid) {
636,314✔
344
        void *px = taosArrayPush(pTmp, p);
386,181✔
345
        if (px == NULL) {
386,181!
346
          code = terrno;
×
347
          break;
×
348
        }
349
      } else if (p->suid > suid) {
250,127!
350
        break;
250,256✔
351
      }
352
    }
353

354
    taosArrayDestroy(pBlockLoadInfo->aSttBlk);
522,296✔
355
    pBlockLoadInfo->aSttBlk = pTmp;
522,183✔
356
  }
357

358
  return code;
1,978,963✔
359
}
360

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

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

373
  return TSDB_CODE_SUCCESS;
895,705✔
374
}
375

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

384
  int32_t numOfBlocks = TARRAY2_SIZE(pStatisBlkArray);
2,905,018✔
385
  if (numOfBlocks <= 0) {
2,905,018✔
386
    return code;
64✔
387
  }
388

389
  while ((startIndex < numOfBlocks) && (pStatisBlkArray->data[startIndex].maxTbid.suid < suid)) {
3,455,570✔
390
    ++startIndex;
550,616✔
391
  }
392

393
  if (startIndex >= numOfBlocks || pStatisBlkArray->data[startIndex].minTbid.suid > suid) {
2,904,954✔
394
    return 0;
1,059,884✔
395
  }
396

397
  int32_t endIndex = startIndex;
1,845,070✔
398
  while (endIndex < numOfBlocks && pStatisBlkArray->data[endIndex].minTbid.suid <= suid) {
3,698,026✔
399
    ++endIndex;
1,852,956✔
400
  }
401

402
  int32_t num = endIndex - startIndex;
1,845,070✔
403
  pBlockLoadInfo->cost.loadStatisBlocks += num;
1,845,070✔
404

405
  STbStatisBlock block;
406
  code = tStatisBlockInit(&block);
1,845,070✔
407
  QUERY_CHECK_CODE(code, lino, _end);
1,845,054!
408

409
  int64_t st = taosGetTimestampUs();
1,845,017✔
410

411
  for (int32_t k = startIndex; k < endIndex; ++k) {
3,684,388✔
412
    code = tsdbSttFileReadStatisBlock(pSttFileReader, &pStatisBlkArray->data[k], &block);
1,850,981✔
413
    QUERY_CHECK_CODE(code, lino, _end);
1,852,749!
414

415
    int32_t i = 0;
1,852,749✔
416
    int32_t rows = block.numOfRecords;
1,852,749✔
417
    while (i < rows && ((int64_t *)block.suids.data)[i] != suid) {
2,553,055✔
418
      ++i;
700,306✔
419
    }
420

421
    // existed
422
    if (i < rows) {
1,852,749✔
423
      SSttTableRowsInfo *pInfo = &pBlockLoadInfo->info;
1,806,416✔
424

425
      if (pInfo->pUid == NULL) {
1,806,416✔
426
        pInfo->pUid = taosArrayInit(rows, sizeof(int64_t));
1,798,519✔
427
        pInfo->pFirstTs = taosArrayInit(rows, sizeof(int64_t));
1,798,924✔
428
        pInfo->pLastTs = taosArrayInit(rows, sizeof(int64_t));
1,799,147✔
429
        pInfo->pCount = taosArrayInit(rows, sizeof(int64_t));
1,799,157✔
430

431
        pInfo->pFirstKey = taosArrayInit(rows, sizeof(SValue));
1,799,010✔
432
        pInfo->pLastKey = taosArrayInit(rows, sizeof(SValue));
1,799,076✔
433

434
        if (pInfo->pUid == NULL || pInfo->pFirstTs == NULL || pInfo->pLastTs == NULL || pInfo->pCount == NULL ||
1,799,121!
435
            pInfo->pFirstKey == NULL || pInfo->pLastKey == NULL) {
1,799,125!
436
          code = terrno;
238✔
437
          goto _end;
×
438
        }
439
      }
440

441
      if (pStatisBlkArray->data[k].maxTbid.suid == suid) {
1,806,780✔
442
        int32_t size = rows - i;
1,716,631✔
443
        int32_t offset = i * sizeof(int64_t);
1,716,631✔
444

445
        px = taosArrayAddBatch(pInfo->pUid, tBufferGetDataAt(&block.uids, offset), size);
1,716,631✔
446
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,716,413!
447

448
        px = taosArrayAddBatch(pInfo->pFirstTs, tBufferGetDataAt(&block.firstKeyTimestamps, offset), size);
1,716,413✔
449
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,716,112!
450

451
        px = taosArrayAddBatch(pInfo->pLastTs, tBufferGetDataAt(&block.lastKeyTimestamps, offset), size);
1,716,112✔
452
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,715,753!
453

454
        px = taosArrayAddBatch(pInfo->pCount, tBufferGetDataAt(&block.counts, offset), size);
1,715,753✔
455
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,715,440✔
456

457
        if (block.numOfPKs > 0) {
1,715,271✔
458
          SValue vFirst = {0}, vLast = {0};
202,054✔
459
          for (int32_t f = i; f < rows; ++f) {
605,371✔
460
            code = tValueColumnGet(&block.firstKeyPKs[0], f, &vFirst);
403,040✔
461
            TSDB_CHECK_CODE(code, lino, _end);
403,146!
462

463
            code = tValueDupPayload(&vFirst);
403,146✔
464
            TSDB_CHECK_CODE(code, lino, _end);
403,225!
465

466
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
403,225✔
467
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
403,244!
468

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

473
            code = tValueDupPayload(&vLast);
403,288✔
474
            TSDB_CHECK_CODE(code, lino, _end);
403,319!
475

476
            px = taosArrayPush(pInfo->pLastKey, &vLast);
403,319✔
477
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
403,317!
478
          }
479
        } else {
480
          SValue vFirst = {0};
1,513,217✔
481
          for (int32_t j = 0; j < size; ++j) {
15,063,720✔
482
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
13,563,160✔
483
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
13,547,330!
484

485
            px = taosArrayPush(pInfo->pLastKey, &vFirst);
13,547,330✔
486
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
13,550,503!
487
          }
488
        }
489
      } else {
490
        STbStatisRecord record = {0};
90,149✔
491
        while (i < rows) {
219,331✔
492
          code = tStatisBlockGet(&block, i, &record);
219,325✔
493
          TSDB_CHECK_CODE(code, lino, _end);
219,112!
494

495
          if (record.suid != suid) {
219,112✔
496
            break;
90,141✔
497
          }
498

499
          px = taosArrayPush(pInfo->pUid, &record.uid);
128,971✔
500
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
129,046!
501

502
          px = taosArrayPush(pInfo->pCount, &record.count);
129,046✔
503
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
128,987!
504

505
          px = taosArrayPush(pInfo->pFirstTs, &record.firstKey.ts);
128,987✔
506
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
128,926!
507

508
          px = taosArrayPush(pInfo->pLastTs, &record.lastKey.ts);
128,926✔
509
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
128,878!
510

511
          if (record.firstKey.numOfPKs > 0) {
128,878✔
512
            SValue s = record.firstKey.pks[0];
44,989✔
513
            code = tValueDupPayload(&s);
44,989✔
514
            TSDB_CHECK_CODE(code, lino, _end);
44,997!
515

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

519
            s = record.lastKey.pks[0];
44,997✔
520
            code = tValueDupPayload(&s);
44,997✔
521
            TSDB_CHECK_CODE(code, lino, _end);
45,000!
522

523
            px = taosArrayPush(pInfo->pLastKey, &s);
45,000✔
524
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
45,002!
525
          } else {
526
            SValue v = {0};
83,889✔
527
            px = taosArrayPush(pInfo->pFirstKey, &v);
83,889✔
528
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
84,213!
529

530
            px = taosArrayPush(pInfo->pLastKey, &v);
84,213✔
531
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
84,180!
532
          }
533

534
          i += 1;
129,182✔
535
        }
536
      }
537
    }
538
  }
539

540
_end:
1,833,407✔
541
  tStatisBlockDestroy(&block);
1,833,407✔
542
  if (code != 0) {
1,844,829!
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;
1,844,418✔
546
    pBlockLoadInfo->cost.statisElapsedTime += el;
1,844,418✔
547

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

553
static int32_t doLoadSttFilesBlk(SSttBlockLoadInfo *pBlockLoadInfo, SLDataIter *pIter, int64_t suid,
2,906,602✔
554
                                 _load_tomb_fn loadTombFn, void *pReader1, const char *idStr) {
555
  int64_t st = taosGetTimestampUs();
2,906,244✔
556

557
  const TSttBlkArray *pSttBlkArray = NULL;
2,906,244✔
558
  pBlockLoadInfo->sttBlockLoaded = true;
2,906,244✔
559

560
  // load the stt block info for each stt-block
561
  int32_t code = tsdbSttFileReadSttBlk(pIter->pReader, &pSttBlkArray);
2,906,244✔
562
  if (code != TSDB_CODE_SUCCESS) {
2,908,154!
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);
2,908,154✔
569
  if (code != TSDB_CODE_SUCCESS) {
2,905,174!
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;
2,905,174✔
576
  code = tsdbSttFileReadStatisBlk(pIter->pReader, (const TStatisBlkArray **)&pStatisBlkArray);
2,905,174✔
577
  if (code != TSDB_CODE_SUCCESS) {
2,907,389!
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);
2,907,389✔
584
  if (code != TSDB_CODE_SUCCESS) {
2,907,858!
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);
2,907,858✔
590

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

596
static int32_t uidComparFn(const void *p1, const void *p2) {
4,428,944✔
597
  const uint64_t *pFirst = p1;
4,428,944✔
598
  const uint64_t *pVal = p2;
4,428,944✔
599

600
  if (*pFirst == *pVal) {
4,428,944✔
601
    return 0;
2,121,202✔
602
  } else {
603
    return *pFirst < *pVal ? -1 : 1;
2,307,742✔
604
  }
605
}
606

607
static void setSttInfoForCurrentTable(SSttBlockLoadInfo *pLoadInfo, uint64_t uid, SSttKeyRange *pRange,
4,650,201✔
608
                                      int64_t *numOfRows) {
609
  if (pRange == NULL || taosArrayGetSize(pLoadInfo->info.pUid) == 0) {
4,650,201!
610
    return;
1,775,498✔
611
  }
612

613
  int32_t index = taosArraySearchIdx(pLoadInfo->info.pUid, &uid, uidComparFn, TD_EQ);
2,872,497✔
614
  if (index >= 0) {
2,874,349✔
615
    pRange->skey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pFirstTs, index);
2,121,211✔
616
    pRange->ekey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pLastTs, index);
2,120,687✔
617

618
    *numOfRows += *(int64_t *)taosArrayGet(pLoadInfo->info.pCount, index);
2,120,390✔
619

620
    if (pRange->skey.numOfPKs > 0) {
2,120,217✔
621
      memcpy(&pRange->skey.pks[0], taosArrayGet(pLoadInfo->info.pFirstKey, index), sizeof(SValue));
413,023✔
622
      memcpy(&pRange->ekey.pks[0], taosArrayGet(pLoadInfo->info.pLastKey, index), sizeof(SValue));
412,979✔
623
    }
624
  }
625
}
626

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

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

640
  pIter->pStartRowKey = pConf->pCurRowKey;
4,651,853✔
641
  pIter->pReader = pSttFileReader;
4,651,853✔
642
  pIter->pBlockLoadInfo = pBlockLoadInfo;
4,651,853✔
643

644
  // open stt file failed, ignore and continue
645
  if (pIter->pReader == NULL) {
4,651,853!
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) {
4,651,853✔
653
    code = doLoadSttFilesBlk(pBlockLoadInfo, pIter, pConf->suid, pConf->loadTombFn, pConf->pReader, idStr);
2,908,208✔
654
    if (code != TSDB_CODE_SUCCESS) {
2,907,556!
655
      return code;
×
656
    }
657
  }
658

659
  setSttInfoForCurrentTable(pBlockLoadInfo, pConf->uid, pKeyRange, numOfRows);
4,651,201✔
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);
4,647,330✔
664
  pIter->iSttBlk = binarySearchForStartBlock(pBlockLoadInfo->aSttBlk->pData, size, pConf->uid, backward);
4,646,883✔
665
  if (pIter->iSttBlk != -1) {
4,644,293✔
666
    pIter->pSttBlk = taosArrayGet(pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
2,151,858✔
667
    pIter->iRow = (pIter->backward) ? pIter->pSttBlk->nRow : -1;
2,151,756✔
668

669
    if ((!backward) && ((pConf->strictTimeRange && pIter->pSttBlk->minKey >= pIter->timeWindow.ekey) ||
2,151,756!
670
                        (!pConf->strictTimeRange && pIter->pSttBlk->minKey > pIter->timeWindow.ekey))) {
1,926,471!
671
      pIter->pSttBlk = NULL;
931✔
672
    }
673

674
    if (backward && ((pConf->strictTimeRange && pIter->pSttBlk->maxKey <= pIter->timeWindow.skey) ||
2,151,756!
675
                     (!pConf->strictTimeRange && pIter->pSttBlk->maxKey < pIter->timeWindow.skey))) {
225,053!
676
      pIter->pSttBlk = NULL;
366✔
677
      pIter->ignoreEarlierTs = true;
366✔
678
    }
679
  }
680

681
  return code;
4,644,191✔
682
}
683

684
void tLDataIterClose2(SLDataIter *pIter) {
2,906,480✔
685
  tsdbSttFileReaderClose(&pIter->pReader);
2,906,480✔
686
  pIter->pReader = NULL;
2,911,880✔
687
}
2,911,880✔
688

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

693
  pIter->iSttBlk += step;
2,208,385✔
694

695
  int32_t index = -1;
2,208,385✔
696
  size_t  size = pIter->pBlockLoadInfo->aSttBlk->size;
2,208,385✔
697
  for (int32_t i = pIter->iSttBlk; i < size && i >= 0; i += step) {
2,211,116!
698
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, i);
263,843✔
699
    if ((!pIter->backward) && p->minUid > pIter->uid) {
263,823✔
700
      break;
152,148✔
701
    }
702

703
    if (pIter->backward && p->maxUid < pIter->uid) {
111,675✔
704
      break;
12,573✔
705
    }
706

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

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

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

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

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

735
  pIter->pSttBlk = NULL;
2,208,365✔
736
  if (index != -1) {
2,208,365✔
737
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, index);
95,413✔
738

739
    pIter->iSttBlk = index;
95,414✔
740
    pIter->pSttBlk = (SSttBlk *)taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
95,414✔
741
    tsdbDebug("try next stt-file block:%d from %d, trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
95,413✔
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);
2,112,952✔
746
  }
747
}
2,208,369✔
748

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

755
  int32_t code = loadLastBlock(pIter, idStr, &pData);
389,870,191✔
756
  if (code) {
382,010,948!
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) {
382,010,948✔
763
    i = binarySearchForStartRowIndex((uint64_t *)pData->aUid, pData->nRow, pIter->uid, pIter->backward);
2,171,298✔
764
    if (i == -1) {
2,172,188!
765
      tsdbDebug("failed to find the data in pBlockData, uid:%" PRIu64 " , %s", pIter->uid, idStr);
×
766
      pIter->iRow = -1;
31,343✔
767
      return code;
31,343✔
768
    }
769
  }
770

771
  for (; i < pData->nRow && i >= 0; i += step) {
397,676,623!
772
    if (pData->aUid != NULL) {
401,107,316✔
773
      if (!pIter->backward) {
397,033,996✔
774
        if (pData->aUid[i] > pIter->uid) {
378,530,709✔
775
          break;
576,850✔
776
        }
777
      } else {
778
        if (pData->aUid[i] < pIter->uid) {
18,503,287✔
779
          break;
123,062✔
780
        }
781
      }
782
    }
783

784
    int64_t ts = pData->aTSKEY[i];
400,407,404✔
785
    if (!pIter->backward) {               // asc
400,407,404✔
786
      if (ts > pIter->timeWindow.ekey) {  // no more data
381,084,322✔
787
        break;
6,378✔
788
      } else {
789
        if (ts < pIter->timeWindow.skey) {
381,077,944✔
790
          continue;
9,563,123✔
791
        }
792

793
        if (ts == pIter->timeWindow.skey && pIter->pStartRowKey->numOfPKs > 0) {
371,514,821!
794
          SRowKey key;
795
          tColRowGetKey(pData, i, &key);
×
796
          int32_t ret = pkCompEx(&key, pIter->pStartRowKey);
×
797
          if (ret < 0) {
×
798
            continue;
×
799
          }
800
        }
801
      }
802
    } else {
803
      if (ts < pIter->timeWindow.skey) {
19,323,082✔
804
        break;
2,480✔
805
      } else {
806
        if (ts > pIter->timeWindow.ekey) {
19,320,602✔
807
          continue;
5,121,564✔
808
        }
809

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

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

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

831
    hasVal = true;
385,713,859✔
832
    break;
385,713,859✔
833
  }
834

835
  pIter->iRow = (hasVal) ? i : -1;
382,991,936!
836
  return code;
382,991,936✔
837
}
838

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

846
  *hasNext = false;
391,376,166✔
847

848
  // no qualified last file block in current file, no need to fetch row
849
  if (pIter->pSttBlk == NULL) {
391,376,166✔
850
    return code;
2,497,207✔
851
  }
852

853
  code = loadLastBlock(pIter, idStr, &pBlockData);
388,878,959✔
854
  if (pBlockData == NULL || code != TSDB_CODE_SUCCESS) {
389,829,112!
855
    lino = __LINE__;
×
856
    goto _exit;
×
857
  }
858

859
  pIter->iRow += step;
389,996,836✔
860

861
  while (1) {
93,864✔
862
    bool skipBlock = false;
390,090,700✔
863
    code = findNextValidRow(pIter, idStr);
390,090,700✔
864
    TSDB_CHECK_CODE(code, lino, _exit);
388,228,871!
865

866
    if (pIter->pBlockLoadInfo->checkRemainingRow) {
388,228,871!
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) {
388,228,871!
890
      tLDataIterNextBlock(pIter, idStr);
2,150,541✔
891
      if (pIter->pSttBlk == NULL) {  // no more data
2,206,929✔
892
        goto _exit;
2,113,066✔
893
      }
894
    } else {
895
      break;
896
    }
897

898
    if (iBlockL != pIter->iSttBlk) {
93,863!
899
      code = loadLastBlock(pIter, idStr, &pBlockData);
95,413✔
900
      if ((pBlockData == NULL) || (code != 0)) {
95,406!
901
        lino = __LINE__;
×
902
        goto _exit;
×
903
      }
904

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

910
  pIter->rInfo.suid = pBlockData->suid;
386,078,330✔
911
  pIter->rInfo.uid = pBlockData->uid;
386,078,330✔
912
  pIter->rInfo.row = tsdbRowFromBlockData(pBlockData, pIter->iRow);
386,078,330✔
913

914
_exit:
388,023,664✔
915
  if (code) {
388,023,664!
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);
388,194,863!
920
  return code;
388,194,863✔
921
}
922

923
// SMergeTree =================================================
924
static FORCE_INLINE int32_t tLDataIterCmprFn(const SRBTreeNode *p1, const SRBTreeNode *p2) {
97,125,239✔
925
  SLDataIter *pIter1 = (SLDataIter *)(((uint8_t *)p1) - offsetof(SLDataIter, node));
97,494,362✔
926
  SLDataIter *pIter2 = (SLDataIter *)(((uint8_t *)p2) - offsetof(SLDataIter, node));
97,494,362✔
927

928
  SRowKey rkey1 = {0}, rkey2 = {0};
97,494,362✔
929
  tRowGetKeyEx(&pIter1->rInfo.row, &rkey1);
97,494,362!
930
  tRowGetKeyEx(&pIter2->rInfo.row, &rkey2);
98,051,890!
931

932
  int32_t ret = tRowKeyCompare(&rkey1, &rkey2);
97,173,272✔
933
  if (ret < 0) {
97,282,090✔
934
    return -1;
73,770,651✔
935
  } else if (ret > 0) {
23,511,439✔
936
    return 1;
9,331,741✔
937
  } else {
938
    int64_t ver1 = TSDBROW_VERSION(&pIter1->rInfo.row);
14,179,698!
939
    int64_t ver2 = TSDBROW_VERSION(&pIter2->rInfo.row);
14,179,698!
940

941
    if (ver1 < ver2) {
14,179,698✔
942
      return -1;
5,686,991✔
943
    } else if (ver1 > ver2) {
8,492,707!
944
      return 1;
9,068,302✔
945
    } else {
946
      return 0;
×
947
    }
948
  }
949
}
950

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

955
int32_t tMergeTreeOpen2(SMergeTree *pMTree, SMergeTreeConf *pConf, SSttDataInfoForTable *pSttDataInfo) {
4,992,076✔
956
  int32_t code = TSDB_CODE_SUCCESS;
4,992,076✔
957

958
  pMTree->pIter = NULL;
4,992,076✔
959
  pMTree->backward = pConf->backward;
4,992,076✔
960
  pMTree->idStr = pConf->idstr;
4,992,076✔
961
  int32_t lino = 0;
4,992,076✔
962

963
  if (!pMTree->backward) {  // asc
4,992,076✔
964
    tRBTreeCreate(&pMTree->rbt, tLDataIterCmprFn);
4,152,725✔
965
  } else {  // desc
966
    tRBTreeCreate(&pMTree->rbt, tLDataIterDescCmprFn);
839,351✔
967
  }
968

969
  pMTree->ignoreEarlierTs = false;
4,991,570✔
970

971
  // no data exists, go to end
972
  int32_t numOfLevels = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->size;
4,991,570✔
973
  if (numOfLevels == 0) {
4,991,570✔
974
    goto _end;
530,072✔
975
  }
976

977
  code = adjustSttDataIters(pConf->pSttFileBlockIterArray, pConf->pCurrentFileset);
4,461,498✔
978
  if (code) {
4,460,568!
979
    goto _end;
×
980
  }
981

982
  for (int32_t j = 0; j < numOfLevels; ++j) {
9,068,074✔
983
    SSttLvl *pSttLevel = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->data[j];
4,611,015✔
984
    SArray  *pList = taosArrayGetP(pConf->pSttFileBlockIterArray, j);
4,611,015✔
985

986
    for (int32_t i = 0; i < TARRAY2_SIZE(pSttLevel->fobjArr); ++i) {  // open all last file
9,255,243✔
987
      SLDataIter *pIter = taosArrayGetP(pList, i);
4,647,737✔
988

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

998
        code = tsdbSttFileReaderOpen(pSttLevel->fobjArr->data[i]->fname, &conf, &pSttFileReader);
2,907,578✔
999
        if (code != TSDB_CODE_SUCCESS) {
2,905,321!
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) {
4,642,765✔
1006
        code = tCreateSttBlockLoadInfo(pConf->pSchema, pConf->pCols, pConf->numOfCols, &pLoadInfo);
2,906,162✔
1007
        if (code != TSDB_CODE_SUCCESS) {
2,908,294!
1008
          goto _end;
×
1009
        }
1010
      }
1011

1012
      memset(pIter, 0, sizeof(SLDataIter));
4,644,897✔
1013

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

1018
      code = tLDataIterOpen2(pIter, pSttFileReader, cid, pMTree->backward, pConf, pLoadInfo, &range, &numOfRows,
4,644,897✔
1019
                             pMTree->idStr);
1020
      if (code != TSDB_CODE_SUCCESS) {
4,643,536!
1021
        goto _end;
×
1022
      }
1023

1024
      bool hasVal = NULL;
4,643,536✔
1025
      code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
4,643,536✔
1026
      if (code) {
4,644,920!
1027
        goto _end;
×
1028
      }
1029

1030
      if (hasVal) {
4,644,920✔
1031
        tMergeTreeAddIter(pMTree, pIter);
2,110,996✔
1032

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

1038
          pSttDataInfo->numOfRows += numOfRows;
2,109,573✔
1039
        }
1040
      } else {
1041
        if (!pMTree->ignoreEarlierTs) {
2,533,924!
1042
          pMTree->ignoreEarlierTs = pIter->ignoreEarlierTs;
2,535,653✔
1043
        }
1044
      }
1045
    }
1046
  }
1047

1048
  return code;
4,457,059✔
1049

1050
_end:
530,072✔
1051
  tMergeTreeClose(pMTree);
530,072✔
1052
  return code;
530,019✔
1053
}
1054

1055
void tMergeTreeAddIter(SMergeTree *pMTree, SLDataIter *pIter) {
2,110,664✔
1056
  SRBTreeNode *node = tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pIter);
2,110,664✔
1057
}
2,110,482✔
1058

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

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

1064
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
326,873,882✔
1065
    pInfo->blockData[0].pin = true;
178,148,800✔
1066
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
178,148,800✔
1067
    return;
178,166,410✔
1068
  }
1069

1070
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
148,725,082!
1071
    pInfo->blockData[1].pin = true;
154,673,685✔
1072
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
154,673,685✔
1073
    return;
154,674,107✔
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) {
323,716,671✔
1080
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
323,716,671✔
1081
  if (pInfo->blockData[0].pin) {
323,716,671✔
1082
    pInfo->blockData[0].pin = false;
177,014,076✔
1083
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[0].sttBlockIndex, pIter->cid, id);
177,014,076✔
1084
    return;
176,786,797✔
1085
  }
1086

1087
  if (pInfo->blockData[1].pin) {
146,702,595!
1088
    pInfo->blockData[1].pin = false;
153,009,403✔
1089
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[1].sttBlockIndex, pIter->cid, id);
153,009,403✔
1090
    return;
153,020,371✔
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) {
326,836,807✔
1097
  if (pMTree->pIter == NULL) {
326,836,807!
1098
    return;
×
1099
  }
1100

1101
  SLDataIter *pIter = pMTree->pIter;
326,836,807✔
1102
  pMTree->pPinnedBlockIter = pIter;
326,836,807✔
1103
  tLDataIterPinSttBlock(pIter, pMTree->idStr);
326,836,807✔
1104
}
1105

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

1111
  SLDataIter *pIter = pMTree->pPinnedBlockIter;
323,675,796✔
1112
  pMTree->pPinnedBlockIter = NULL;
323,675,796✔
1113
  tLDataIterUnpinSttBlock(pIter, pMTree->idStr);
323,675,796✔
1114
}
1115

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

1122
  *pHasNext = false;
390,228,264✔
1123
  while (pMTree->pIter) {
390,228,264✔
1124
    SLDataIter *pIter = pMTree->pIter;
385,689,343✔
1125
    bool        hasVal = false;
385,689,343✔
1126
    code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
385,689,343✔
1127
    if (!hasVal || (code != 0)) {
385,686,289✔
1128
      if (code == TSDB_CODE_FILE_CORRUPTED) {
2,072,763!
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;
2,072,763✔
1133
    }
1134

1135
    // compare with min in RB Tree
1136
    pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
385,686,289✔
1137
    if (pMTree->pIter && pIter) {
385,686,289✔
1138
      int32_t c = pMTree->rbt.cmprFn(&pMTree->pIter->node, &pIter->node);
73,831,257✔
1139
      if (c > 0) {
73,320,003✔
1140
        (void)tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
3,894,604✔
1141
        pMTree->pIter = NULL;
5,490,964✔
1142
      } else if (!c) {
69,425,399!
1143
        continue;
×
1144
      }
1145
    }
1146

1147
    break;
386,771,395✔
1148
  }
1149

1150
  if (pMTree->pIter == NULL) {
391,310,316✔
1151
    pMTree->pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
12,513,022✔
1152
    if (pMTree->pIter) {
12,513,022✔
1153
      tRBTreeDrop(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
7,569,958✔
1154
    }
1155
  }
1156

1157
  *pHasNext = (pMTree->pIter != NULL);
391,238,856✔
1158
  return code;
391,238,856✔
1159
}
1160

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