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

taosdata / TDengine / #4221

30 May 2025 10:15AM UTC coverage: 63.059% (+0.4%) from 62.611%
#4221

push

travis-ci

web-flow
Merge pull request #31256 from taosdata/new_test_3.0

TD-35134, TD-35153, TD-35152 ,TD-35082

157757 of 319076 branches covered (49.44%)

Branch coverage included in aggregate %.

243807 of 317735 relevant lines covered (76.73%)

17224616.85 hits per line

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

80.33
/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,528,097✔
27
  *pInfo = NULL;
7,528,097✔
28

29
  SSttBlockLoadInfo *pLoadInfo = taosMemoryCalloc(1, sizeof(SSttBlockLoadInfo));
7,528,097!
30
  if (pLoadInfo == NULL) {
7,532,449!
31
    return terrno;
×
32
  }
33

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

37
  pLoadInfo->currentLoadBlockIndex = 1;
7,532,449✔
38

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

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

51
  pLoadInfo->aSttBlk = taosArrayInit(4, sizeof(SSttBlk));
7,531,112✔
52
  if (pLoadInfo->aSttBlk == NULL) {
7,532,752✔
53
    taosMemoryFreeClear(pLoadInfo);
664!
54
    return terrno;
664✔
55
  }
56

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

61
  *pInfo = pLoadInfo;
7,532,088✔
62
  return code;
7,532,088✔
63
}
64

65
static void freeItem(void *pValue) {
38,426,631✔
66
  SValue *p = (SValue *)pValue;
38,426,631✔
67
  if (IS_VAR_DATA_TYPE(p->type) || p->type == TSDB_DATA_TYPE_DECIMAL) {
38,426,631!
68
    taosMemoryFree(p->pData);
410,095!
69
  }
70
}
38,441,605✔
71

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

77
  pLoadInfo->currentLoadBlockIndex = 1;
7,537,819✔
78

79
  SBlockDataInfo *pInfo = &pLoadInfo->blockData[0];
7,537,819✔
80
  tBlockDataDestroy(&pInfo->data);
7,537,819✔
81
  pInfo->sttBlockIndex = -1;
7,536,665✔
82
  pInfo->pin = false;
7,536,665✔
83

84
  pInfo = &pLoadInfo->blockData[1];
7,536,665✔
85
  tBlockDataDestroy(&pInfo->data);
7,536,665✔
86
  pInfo->sttBlockIndex = -1;
7,536,453✔
87
  pInfo->pin = false;
7,536,453✔
88

89
  taosArrayDestroy(pLoadInfo->info.pUid);
7,536,453✔
90
  taosArrayDestroyEx(pLoadInfo->info.pFirstKey, freeItem);
7,536,408✔
91
  taosArrayDestroyEx(pLoadInfo->info.pLastKey, freeItem);
7,536,280✔
92
  taosArrayDestroy(pLoadInfo->info.pCount);
7,536,503✔
93
  taosArrayDestroy(pLoadInfo->info.pFirstTs);
7,536,505✔
94
  taosArrayDestroy(pLoadInfo->info.pLastTs);
7,536,466✔
95

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

103
  taosArrayDestroy(pLoadInfo->aSttBlk);
7,536,686✔
104
  taosMemoryFree(pLoadInfo);
7,537,243!
105
}
106

107
void destroyLDataIter(SLDataIter *pIter) {
7,530,923✔
108
  tLDataIterClose2(pIter);
7,530,923✔
109
  destroySttBlockLoadInfo(pIter->pBlockLoadInfo);
7,538,137✔
110
  taosMemoryFree(pIter);
7,537,588!
111
}
7,538,076✔
112

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

118
  int32_t numOfLevel = taosArrayGetSize(pLDataIterArray);
13,305,465✔
119
  for (int32_t i = 0; i < numOfLevel; ++i) {
20,839,339✔
120
    SArray *pList = taosArrayGetP(pLDataIterArray, i);
7,530,625✔
121
    for (int32_t j = 0; j < taosArrayGetSize(pList); ++j) {
15,066,938✔
122
      SLDataIter *pIter = taosArrayGetP(pList, j);
7,532,151✔
123
      if (pIter->pBlockLoadInfo != NULL) {
7,530,722!
124
        SSttBlockLoadCostInfo *pCost = &pIter->pBlockLoadInfo->cost;
7,530,904✔
125
        if (pLoadCost != NULL) {
7,530,904✔
126
          pLoadCost->loadBlocks += pCost->loadBlocks;
7,524,182✔
127
          pLoadCost->loadStatisBlocks += pCost->loadStatisBlocks;
7,524,182✔
128
          pLoadCost->blockElapsedTime += pCost->blockElapsedTime;
7,524,182✔
129
          pLoadCost->statisElapsedTime += pCost->statisElapsedTime;
7,524,182✔
130
        }
131
      }
132

133
      destroyLDataIter(pIter);
7,530,722✔
134
    }
135

136
    taosArrayDestroy(pList);
7,527,458✔
137
  }
138

139
  taosArrayDestroy(pLDataIterArray);
13,308,714✔
140
}
141

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

149
  pLoadInfo->currentLoadBlockIndex = nextSlotIndex;
4,710,826✔
150
}
4,710,826✔
151

152
static int32_t loadLastBlock(SLDataIter *pIter, const char *idStr, SBlockData **pResBlock) {
933,587,002✔
153
  if (pResBlock != NULL) {
933,587,002!
154
    *pResBlock = NULL;
933,856,037✔
155
  }
156

157
  int32_t            code = 0;
933,587,002✔
158
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
933,587,002✔
159

160
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
933,587,002✔
161
    if (pInfo->currentLoadBlockIndex != 0) {
635,282,225✔
162
      tsdbDebug("current load index is set to 0, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
713✔
163
                ", load data, %s",
164
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
165
      pInfo->currentLoadBlockIndex = 0;
713✔
166
    }
167

168
    *pResBlock = &pInfo->blockData[0].data;
635,282,225✔
169
    return code;
635,282,225✔
170
  }
171

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

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

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

188
  updateBlockLoadSlot(pInfo);
4,711,183✔
189
  int64_t st = taosGetTimestampUs();
4,712,302✔
190

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

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

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

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

215
  *pResBlock = &pInfo->blockData[pInfo->currentLoadBlockIndex].data;
4,712,914✔
216
  return code;
4,712,914✔
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,437,055✔
223
  int32_t step = backward ? 1 : -1;
5,437,055✔
224
  while (i >= 0 && i < num && uid >= pBlockList[i].minUid && uid <= pBlockList[i].maxUid) {
10,917,754!
225
    i += step;
5,480,699✔
226
  }
227
  return i - step;
5,437,055✔
228
}
229

230
static int32_t binarySearchForStartBlock(SSttBlk *pBlockList, int32_t num, uint64_t uid, int32_t backward) {
15,498,003✔
231
  int32_t midPos = -1;
15,498,003✔
232
  if (num <= 0) {
15,498,003✔
233
    return -1;
4,061,758✔
234
  }
235

236
  int32_t firstPos = 0;
11,436,245✔
237
  int32_t lastPos = num - 1;
11,436,245✔
238

239
  // find the first position which is bigger than the key
240
  if ((uid > pBlockList[lastPos].maxUid) || (uid < pBlockList[firstPos].minUid)) {
11,436,245✔
241
    return -1;
5,998,929✔
242
  }
243

244
  while (1) {
464,523✔
245
    if (uid >= pBlockList[firstPos].minUid && uid <= pBlockList[firstPos].maxUid) {
5,901,839✔
246
      return findEarliestIndex(firstPos, uid, pBlockList, num, backward);
5,315,553✔
247
    }
248

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

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

256
    if (uid < pBlockList[midPos].minUid) {
586,025✔
257
      lastPos = midPos - 1;
230,850✔
258
    } else if (uid > pBlockList[midPos].maxUid) {
355,175✔
259
      firstPos = midPos + 1;
233,673✔
260
    } else {
261
      return findEarliestIndex(midPos, uid, pBlockList, num, backward);
121,502✔
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,320,347✔
269
  int32_t step = backward ? 1 : -1;
5,320,347✔
270
  while (i >= 0 && i < num && uid == uidList[i]) {
91,128,816!
271
    i += step;
85,808,469✔
272
  }
273
  return i - step;
5,320,347✔
274
}
275

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

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

285
  while (1) {
1,443,964✔
286
    if (uid == uidList[firstPos]) {
6,825,192✔
287
      return findEarliestRow(firstPos, uid, uidList, num, backward);
4,541,066✔
288
    }
289

290
    if (uid > uidList[lastPos] || uid < uidList[firstPos]) {
2,284,126✔
291
      return -1;
60,881✔
292
    }
293

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

297
    if (uid < uidList[midPos]) {
2,223,245✔
298
      lastPos = midPos - 1;
576,356✔
299
    } else if (uid > uidList[midPos]) {
1,646,889✔
300
      firstPos = midPos + 1;
867,608✔
301
    } else {
302
      return findEarliestRow(midPos, uid, uidList, num, backward);
779,281✔
303
    }
304
  }
305
}
306

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

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

318
  // all identical
319
  if (pStart->suid == pEnd->suid) {
7,512,665✔
320
    if (pStart->suid != suid) {  // no qualified stt block existed
3,398,740✔
321
      taosArrayClear(pBlockLoadInfo->aSttBlk);
1,705,237✔
322
      pIter->iSttBlk = -1;
1,705,001✔
323
      return TSDB_CODE_SUCCESS;
1,705,001✔
324
    } else {  // all blocks are qualified
325
      taosArrayClear(pBlockLoadInfo->aSttBlk);
1,693,503✔
326
      px = taosArrayAddBatch(pBlockLoadInfo->aSttBlk, pArray->data, pArray->size);
1,694,471✔
327
      if (px == NULL) {
1,693,631!
328
        return terrno;
×
329
      }
330
    }
331
  } else {
332
    SArray *pTmp = taosArrayInit(TARRAY2_SIZE(pArray), sizeof(SSttBlk));
4,113,925✔
333
    if (pTmp == NULL) {
4,119,803!
334
      return terrno;
×
335
    }
336

337
    for (int32_t i = 0; i < TARRAY2_SIZE(pArray); ++i) {
12,628,284✔
338
      SSttBlk *p = &pArray->data[i];
11,541,508✔
339
      if (p->suid < suid) {
11,541,508✔
340
        continue;
5,128,430✔
341
      }
342

343
      if (p->suid == suid) {
6,413,078✔
344
        void *px = taosArrayPush(pTmp, p);
3,381,148✔
345
        if (px == NULL) {
3,381,148!
346
          code = terrno;
×
347
          break;
×
348
        }
349
      } else if (p->suid > suid) {
3,031,641!
350
        break;
3,032,767✔
351
      }
352
    }
353

354
    taosArrayDestroy(pBlockLoadInfo->aSttBlk);
4,119,543✔
355
    pBlockLoadInfo->aSttBlk = pTmp;
4,120,320✔
356
  }
357

358
  return code;
5,813,951✔
359
}
360

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

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

373
  return TSDB_CODE_SUCCESS;
3,080,188✔
374
}
375

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

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

389
  while ((startIndex < numOfBlocks) && (pStatisBlkArray->data[startIndex].maxTbid.suid < suid)) {
8,616,766✔
390
    ++startIndex;
1,102,179✔
391
  }
392

393
  if (startIndex >= numOfBlocks || pStatisBlkArray->data[startIndex].minTbid.suid > suid) {
7,514,587✔
394
    return 0;
1,872,053✔
395
  }
396

397
  int32_t endIndex = startIndex;
5,642,534✔
398
  while (endIndex < numOfBlocks && pStatisBlkArray->data[endIndex].minTbid.suid <= suid) {
11,295,907✔
399
    ++endIndex;
5,653,373✔
400
  }
401

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

405
  STbStatisBlock block;
406
  code = tStatisBlockInit(&block);
5,642,534✔
407
  QUERY_CHECK_CODE(code, lino, _end);
5,642,154!
408

409
  int64_t st = taosGetTimestampUs();
5,641,757✔
410

411
  for (int32_t k = startIndex; k < endIndex; ++k) {
11,281,178✔
412
    code = tsdbSttFileReadStatisBlock(pSttFileReader, &pStatisBlkArray->data[k], &block);
5,649,655✔
413
    QUERY_CHECK_CODE(code, lino, _end);
5,653,033!
414

415
    int32_t i = 0;
5,653,033✔
416
    int32_t rows = block.numOfRecords;
5,653,033✔
417
    while (i < rows && ((int64_t *)block.suids.data)[i] != suid) {
11,341,030✔
418
      ++i;
5,687,997✔
419
    }
420

421
    // existed
422
    if (i < rows) {
5,653,033✔
423
      SSttTableRowsInfo *pInfo = &pBlockLoadInfo->info;
5,017,411✔
424

425
      if (pInfo->pUid == NULL) {
5,017,411✔
426
        pInfo->pUid = taosArrayInit(rows, sizeof(int64_t));
5,006,546✔
427
        pInfo->pFirstTs = taosArrayInit(rows, sizeof(int64_t));
5,006,733✔
428
        pInfo->pLastTs = taosArrayInit(rows, sizeof(int64_t));
5,006,877✔
429
        pInfo->pCount = taosArrayInit(rows, sizeof(int64_t));
5,006,910✔
430

431
        pInfo->pFirstKey = taosArrayInit(rows, sizeof(SValue));
5,006,807✔
432
        pInfo->pLastKey = taosArrayInit(rows, sizeof(SValue));
5,006,887✔
433

434
        if (pInfo->pUid == NULL || pInfo->pFirstTs == NULL || pInfo->pLastTs == NULL || pInfo->pCount == NULL ||
5,006,882!
435
            pInfo->pFirstKey == NULL || pInfo->pLastKey == NULL) {
5,006,891✔
436
          code = terrno;
512✔
437
          goto _end;
×
438
        }
439
      }
440

441
      if (pStatisBlkArray->data[k].maxTbid.suid == suid) {
5,017,235✔
442
        int32_t size = rows - i;
2,754,418✔
443
        int32_t offset = i * sizeof(int64_t);
2,754,418✔
444

445
        px = taosArrayAddBatch(pInfo->pUid, tBufferGetDataAt(&block.uids, offset), size);
2,754,418✔
446
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
2,754,356!
447

448
        px = taosArrayAddBatch(pInfo->pFirstTs, tBufferGetDataAt(&block.firstKeyTimestamps, offset), size);
2,754,356✔
449
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
2,754,132!
450

451
        px = taosArrayAddBatch(pInfo->pLastTs, tBufferGetDataAt(&block.lastKeyTimestamps, offset), size);
2,754,132✔
452
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
2,753,651!
453

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

457
        if (block.numOfPKs > 0) {
2,753,396✔
458
          SValue vFirst = {0}, vLast = {0};
260,716✔
459
          for (int32_t f = i; f < rows; ++f) {
1,744,464✔
460
            code = tValueColumnGet(&block.firstKeyPKs[0], f, &vFirst);
1,483,427✔
461
            TSDB_CHECK_CODE(code, lino, _end);
1,483,504!
462

463
            code = tValueDupPayload(&vFirst);
1,483,504✔
464
            TSDB_CHECK_CODE(code, lino, _end);
1,483,597!
465

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

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

473
            code = tValueDupPayload(&vLast);
1,483,729✔
474
            TSDB_CHECK_CODE(code, lino, _end);
1,483,632!
475

476
            px = taosArrayPush(pInfo->pLastKey, &vLast);
1,483,632✔
477
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,483,748!
478
          }
479
        } else {
480
          SValue vFirst = {0};
2,492,680✔
481
          for (int32_t j = 0; j < size; ++j) {
17,203,231✔
482
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
14,723,399✔
483
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
14,713,468!
484

485
            px = taosArrayPush(pInfo->pLastKey, &vFirst);
14,713,468✔
486
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
14,710,551!
487
          }
488
        }
489
      } else {
490
        STbStatisRecord record = {0};
2,262,817✔
491
        while (i < rows) {
4,585,677✔
492
          code = tStatisBlockGet(&block, i, &record);
4,585,561✔
493
          TSDB_CHECK_CODE(code, lino, _end);
4,585,441!
494

495
          if (record.suid != suid) {
4,585,441✔
496
            break;
2,262,814✔
497
          }
498

499
          px = taosArrayPush(pInfo->pUid, &record.uid);
2,322,627✔
500
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
2,322,739!
501

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

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

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

511
          if (record.firstKey.numOfPKs > 0) {
2,322,733✔
512
            SValue s = record.firstKey.pks[0];
56,892✔
513
            code = tValueDupPayload(&s);
56,892✔
514
            TSDB_CHECK_CODE(code, lino, _end);
56,900!
515

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

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

523
            px = taosArrayPush(pInfo->pLastKey, &s);
56,902✔
524
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
56,903!
525
          } else {
526
            SValue v = {0};
2,265,841✔
527
            px = taosArrayPush(pInfo->pFirstKey, &v);
2,265,841✔
528
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
2,266,009!
529

530
            px = taosArrayPush(pInfo->pLastKey, &v);
2,266,009✔
531
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
2,265,957!
532
          }
533

534
          i += 1;
2,322,860✔
535
        }
536
      }
537
    }
538
  }
539

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

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

553
static int32_t doLoadSttFilesBlk(SSttBlockLoadInfo *pBlockLoadInfo, SLDataIter *pIter, int64_t suid,
7,524,354✔
554
                                 _load_tomb_fn loadTombFn, void *pReader1, const char *idStr) {
555
  int64_t st = taosGetTimestampUs();
7,523,734✔
556

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

560
  // load the stt block info for each stt-block
561
  int32_t code = tsdbSttFileReadSttBlk(pIter->pReader, &pSttBlkArray);
7,523,734✔
562
  if (code != TSDB_CODE_SUCCESS) {
7,524,755!
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,524,755✔
569
  if (code != TSDB_CODE_SUCCESS) {
7,526,601!
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,526,601✔
576
  code = tsdbSttFileReadStatisBlk(pIter->pReader, (const TStatisBlkArray **)&pStatisBlkArray);
7,526,601✔
577
  if (code != TSDB_CODE_SUCCESS) {
7,525,840!
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,525,840✔
584
  if (code != TSDB_CODE_SUCCESS) {
7,527,155!
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,527,155✔
590

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

596
static int32_t uidComparFn(const void *p1, const void *p2) {
13,632,668✔
597
  const uint64_t *pFirst = p1;
13,632,668✔
598
  const uint64_t *pVal = p2;
13,632,668✔
599

600
  if (*pFirst == *pVal) {
13,632,668✔
601
    return 0;
5,384,909✔
602
  } else {
603
    return *pFirst < *pVal ? -1 : 1;
8,247,759✔
604
  }
605
}
606

607
static void setSttInfoForCurrentTable(SSttBlockLoadInfo *pLoadInfo, uint64_t uid, SSttKeyRange *pRange,
15,505,532✔
608
                                      int64_t *numOfRows) {
609
  if (pRange == NULL || taosArrayGetSize(pLoadInfo->info.pUid) == 0) {
15,505,532!
610
    return;
4,062,761✔
611
  }
612

613
  int32_t index = taosArraySearchIdx(pLoadInfo->info.pUid, &uid, uidComparFn, TD_EQ);
11,440,953✔
614
  if (index >= 0) {
11,444,766✔
615
    pRange->skey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pFirstTs, index);
5,385,303✔
616
    pRange->ekey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pLastTs, index);
5,383,883✔
617

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

620
    if (pRange->skey.numOfPKs > 0) {
5,383,724✔
621
      memcpy(&pRange->skey.pks[0], taosArrayGet(pLoadInfo->info.pFirstKey, index), sizeof(SValue));
504,652✔
622
      memcpy(&pRange->ekey.pks[0], taosArrayGet(pLoadInfo->info.pLastKey, index), sizeof(SValue));
504,542✔
623
    }
624
  }
625
}
626

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

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

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

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

659
  setSttInfoForCurrentTable(pBlockLoadInfo, pConf->uid, pKeyRange, numOfRows);
15,504,997✔
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,500,443✔
664
  pIter->iSttBlk = binarySearchForStartBlock(pBlockLoadInfo->aSttBlk->pData, size, pConf->uid, backward);
15,499,122✔
665
  if (pIter->iSttBlk != -1) {
15,497,786✔
666
    pIter->pSttBlk = taosArrayGet(pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
5,445,338✔
667
    pIter->iRow = (pIter->backward) ? pIter->pSttBlk->nRow : -1;
5,444,691✔
668

669
    if ((!backward) && ((pConf->strictTimeRange && pIter->pSttBlk->minKey >= pIter->timeWindow.ekey) ||
5,444,691!
670
                        (!pConf->strictTimeRange && pIter->pSttBlk->minKey > pIter->timeWindow.ekey))) {
4,793,360✔
671
      pIter->pSttBlk = NULL;
1,492✔
672
    }
673

674
    if (backward && ((pConf->strictTimeRange && pIter->pSttBlk->maxKey <= pIter->timeWindow.skey) ||
5,444,691!
675
                     (!pConf->strictTimeRange && pIter->pSttBlk->maxKey < pIter->timeWindow.skey))) {
651,525!
676
      pIter->pSttBlk = NULL;
475✔
677
      pIter->ignoreEarlierTs = true;
475✔
678
    }
679
  }
680

681
  return code;
15,497,139✔
682
}
683

684
void tLDataIterClose2(SLDataIter *pIter) {
7,530,631✔
685
  tsdbSttFileReaderClose(&pIter->pReader);
7,530,631✔
686
  pIter->pReader = NULL;
7,538,176✔
687
}
7,538,176✔
688

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

693
  pIter->iSttBlk += step;
5,492,549✔
694

695
  int32_t index = -1;
5,492,549✔
696
  size_t  size = pIter->pBlockLoadInfo->aSttBlk->size;
5,492,549✔
697
  for (int32_t i = pIter->iSttBlk; i < size && i >= 0; i += step) {
5,494,670!
698
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, i);
295,852✔
699
    if ((!pIter->backward) && p->minUid > pIter->uid) {
295,834✔
700
      break;
186,982✔
701
    }
702

703
    if (pIter->backward && p->maxUid < pIter->uid) {
108,852✔
704
      break;
21,487✔
705
    }
706

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

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

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

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

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

735
  pIter->pSttBlk = NULL;
5,492,531✔
736
  if (index != -1) {
5,492,531✔
737
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, index);
84,710✔
738

739
    pIter->iSttBlk = index;
84,710✔
740
    pIter->pSttBlk = (SSttBlk *)taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
84,710✔
741
    tsdbDebug("try next stt-file block:%d from %d, trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
84,710✔
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,407,821✔
746
  }
747
}
5,492,533✔
748

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

755
  int32_t code = loadLastBlock(pIter, idStr, &pData);
476,468,192✔
756
  if (code) {
472,483,099!
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) {
472,483,099✔
763
    i = binarySearchForStartRowIndex((uint64_t *)pData->aUid, pData->nRow, pIter->uid, pIter->backward);
5,381,652✔
764
    if (i == -1) {
5,382,069!
765
      tsdbDebug("failed to find the data in pBlockData, uid:%" PRIu64 " , %s", pIter->uid, idStr);
×
766
      pIter->iRow = -1;
61,027✔
767
      return code;
61,027✔
768
    }
769
  }
770

771
  for (; i < pData->nRow && i >= 0; i += step) {
485,622,200!
772
    if (pData->aUid != NULL) {
482,957,751✔
773
      if (!pIter->backward) {
479,222,443✔
774
        if (pData->aUid[i] > pIter->uid) {
447,158,313✔
775
          break;
764,248✔
776
        }
777
      } else {
778
        if (pData->aUid[i] < pIter->uid) {
32,064,130✔
779
          break;
127,715✔
780
        }
781
      }
782
    }
783

784
    int64_t ts = pData->aTSKEY[i];
482,065,788✔
785
    if (!pIter->backward) {               // asc
482,065,788✔
786
      if (ts > pIter->timeWindow.ekey) {  // no more data
449,608,063✔
787
        break;
196,186✔
788
      } else {
789
        if (ts < pIter->timeWindow.skey) {
449,411,877✔
790
          continue;
8,474,273✔
791
        }
792

793
        if (ts == pIter->timeWindow.skey && pIter->pStartRowKey->numOfPKs > 0) {
440,937,604✔
794
          SRowKey key;
795
          tColRowGetKey(pData, i, &key);
312!
796
          int32_t ret = pkCompEx(&key, pIter->pStartRowKey);
312✔
797
          if (ret < 0) {
312!
798
            continue;
×
799
          }
800
        }
801
      }
802
    } else {
803
      if (ts < pIter->timeWindow.skey) {
32,457,725✔
804
        break;
89,923✔
805
      } else {
806
        if (ts > pIter->timeWindow.ekey) {
32,367,802✔
807
          continue;
4,318,834✔
808
        }
809

810
        if (ts == pIter->timeWindow.ekey && pIter->pStartRowKey->numOfPKs > 0) {
28,048,968!
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];
468,986,572✔
822
    if (ver < pIter->verRange.minVer) {
468,986,572!
823
      continue;
×
824
    }
825

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

831
    hasVal = true;
468,986,572✔
832
    break;
468,986,572✔
833
  }
834

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

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

846
  *hasNext = false;
485,084,447✔
847

848
  // no qualified last file block in current file, no need to fetch row
849
  if (pIter->pSttBlk == NULL) {
485,084,447✔
850
    return code;
10,062,345✔
851
  }
852

853
  code = loadLastBlock(pIter, idStr, &pBlockData);
475,022,102✔
854
  if (pBlockData == NULL || code != TSDB_CODE_SUCCESS) {
476,224,523!
855
    lino = __LINE__;
×
856
    goto _exit;
×
857
  }
858

859
  pIter->iRow += step;
476,844,426✔
860

861
  while (1) {
84,003✔
862
    bool skipBlock = false;
476,928,429✔
863
    code = findNextValidRow(pIter, idStr);
476,928,429✔
864
    TSDB_CHECK_CODE(code, lino, _exit);
473,251,525!
865

866
    if (pIter->pBlockLoadInfo->checkRemainingRow) {
473,251,525!
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) {
473,251,525!
890
      tLDataIterNextBlock(pIter, idStr);
5,425,343✔
891
      if (pIter->pSttBlk == NULL) {  // no more data
5,491,805✔
892
        goto _exit;
5,407,793✔
893
      }
894
    } else {
895
      break;
896
    }
897

898
    if (iBlockL != pIter->iSttBlk) {
84,012!
899
      code = loadLastBlock(pIter, idStr, &pBlockData);
84,710✔
900
      if ((pBlockData == NULL) || (code != 0)) {
84,687!
901
        lino = __LINE__;
×
902
        goto _exit;
×
903
      }
904

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

910
  pIter->rInfo.suid = pBlockData->suid;
467,826,182✔
911
  pIter->rInfo.uid = pBlockData->uid;
467,826,182✔
912
  pIter->rInfo.row = tsdbRowFromBlockData(pBlockData, pIter->iRow);
467,826,182✔
913

914
_exit:
472,614,058✔
915
  if (code) {
472,614,058!
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);
472,913,880!
920
  return code;
472,913,880✔
921
}
922

923
// SMergeTree =================================================
924
static FORCE_INLINE int32_t tLDataIterCmprFn(const SRBTreeNode *p1, const SRBTreeNode *p2) {
65,880,100✔
925
  SLDataIter *pIter1 = (SLDataIter *)(((uint8_t *)p1) - offsetof(SLDataIter, node));
66,041,808✔
926
  SLDataIter *pIter2 = (SLDataIter *)(((uint8_t *)p2) - offsetof(SLDataIter, node));
66,041,808✔
927

928
  SRowKey rkey1 = {0}, rkey2 = {0};
66,041,808✔
929
  tRowGetKeyEx(&pIter1->rInfo.row, &rkey1);
66,041,808!
930
  tRowGetKeyEx(&pIter2->rInfo.row, &rkey2);
66,258,376!
931

932
  int32_t ret = tRowKeyCompare(&rkey1, &rkey2);
66,136,298✔
933
  if (ret < 0) {
66,087,968✔
934
    return -1;
38,022,673✔
935
  } else if (ret > 0) {
28,065,295✔
936
    return 1;
11,122,135✔
937
  } else {
938
    int64_t ver1 = TSDBROW_VERSION(&pIter1->rInfo.row);
16,943,160!
939
    int64_t ver2 = TSDBROW_VERSION(&pIter2->rInfo.row);
16,943,160!
940

941
    if (ver1 < ver2) {
16,943,160✔
942
      return -1;
6,800,704✔
943
    } else if (ver1 > ver2) {
10,142,456!
944
      return 1;
10,634,715✔
945
    } else {
946
      return 0;
×
947
    }
948
  }
949
}
950

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

955
int32_t tMergeTreeOpen2(SMergeTree *pMTree, SMergeTreeConf *pConf, SSttDataInfoForTable *pSttDataInfo) {
15,892,761✔
956
  int32_t code = TSDB_CODE_SUCCESS;
15,892,761✔
957

958
  pMTree->pIter = NULL;
15,892,761✔
959
  pMTree->backward = pConf->backward;
15,892,761✔
960
  pMTree->idStr = pConf->idstr;
15,892,761✔
961
  int32_t lino = 0;
15,892,761✔
962

963
  if (!pMTree->backward) {  // asc
15,892,761✔
964
    tRBTreeCreate(&pMTree->rbt, tLDataIterCmprFn);
13,253,643✔
965
  } else {  // desc
966
    tRBTreeCreate(&pMTree->rbt, tLDataIterDescCmprFn);
2,639,118✔
967
  }
968

969
  pMTree->ignoreEarlierTs = false;
15,893,579✔
970

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

977
  code = adjustSttDataIters(pConf->pSttFileBlockIterArray, pConf->pCurrentFileset);
15,285,966✔
978
  if (code) {
15,279,783!
979
    goto _end;
×
980
  }
981

982
  for (int32_t j = 0; j < numOfLevels; ++j) {
30,762,730✔
983
    SSttLvl *pSttLevel = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->data[j];
15,476,609✔
984
    SArray  *pList = taosArrayGetP(pConf->pSttFileBlockIterArray, j);
15,476,609✔
985

986
    for (int32_t i = 0; i < TARRAY2_SIZE(pSttLevel->fobjArr); ++i) {  // open all last file
30,971,472✔
987
      SLDataIter *pIter = taosArrayGetP(pList, i);
15,488,525✔
988

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

998
        code = tsdbSttFileReaderOpen(pSttLevel->fobjArr->data[i]->fname, &conf, &pSttFileReader);
7,517,148✔
999
        if (code != TSDB_CODE_SUCCESS) {
7,522,370!
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,481,212✔
1006
        code = tCreateSttBlockLoadInfo(pConf->pSchema, pConf->pCols, pConf->numOfCols, &pLoadInfo);
7,526,458✔
1007
        if (code != TSDB_CODE_SUCCESS) {
7,525,449!
1008
          goto _end;
×
1009
        }
1010
      }
1011

1012
      memset(pIter, 0, sizeof(SLDataIter));
15,480,203✔
1013

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

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

1024
      bool hasVal = NULL;
15,497,429✔
1025
      code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
15,497,429✔
1026
      if (code) {
15,503,076!
1027
        goto _end;
×
1028
      }
1029

1030
      if (hasVal) {
15,503,076✔
1031
        tMergeTreeAddIter(pMTree, pIter);
5,373,331✔
1032

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

1038
          pSttDataInfo->numOfRows += numOfRows;
5,371,556✔
1039
        }
1040
      } else {
1041
        if (!pMTree->ignoreEarlierTs) {
10,129,745!
1042
          pMTree->ignoreEarlierTs = pIter->ignoreEarlierTs;
10,132,032✔
1043
        }
1044
      }
1045
    }
1046
  }
1047

1048
  return code;
15,286,121✔
1049

1050
_end:
607,613✔
1051
  tMergeTreeClose(pMTree);
607,613✔
1052
  return code;
607,524✔
1053
}
1054

1055
void tMergeTreeAddIter(SMergeTree *pMTree, SLDataIter *pIter) {
5,372,211✔
1056
  SRBTreeNode *node = tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pIter);
5,372,211✔
1057
}
5,373,034✔
1058

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

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

1064
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
310,260,163✔
1065
    pInfo->blockData[0].pin = true;
182,243,372✔
1066
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
182,243,372✔
1067
    return;
182,261,190✔
1068
  }
1069

1070
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
128,016,791!
1071
    pInfo->blockData[1].pin = true;
135,737,792✔
1072
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
135,737,792✔
1073
    return;
135,603,056✔
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) {
305,533,670✔
1080
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
305,533,670✔
1081
  if (pInfo->blockData[0].pin) {
305,533,670✔
1082
    pInfo->blockData[0].pin = false;
180,499,758✔
1083
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[0].sttBlockIndex, pIter->cid, id);
180,499,758✔
1084
    return;
180,521,115✔
1085
  }
1086

1087
  if (pInfo->blockData[1].pin) {
125,033,912!
1088
    pInfo->blockData[1].pin = false;
134,355,835✔
1089
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[1].sttBlockIndex, pIter->cid, id);
134,355,835✔
1090
    return;
134,368,577✔
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) {
310,216,127✔
1097
  if (pMTree->pIter == NULL) {
310,216,127!
1098
    return;
×
1099
  }
1100

1101
  SLDataIter *pIter = pMTree->pIter;
310,216,127✔
1102
  pMTree->pPinnedBlockIter = pIter;
310,216,127✔
1103
  tLDataIterPinSttBlock(pIter, pMTree->idStr);
310,216,127✔
1104
}
1105

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

1111
  SLDataIter *pIter = pMTree->pPinnedBlockIter;
305,623,185✔
1112
  pMTree->pPinnedBlockIter = NULL;
305,623,185✔
1113
  tLDataIterUnpinSttBlock(pIter, pMTree->idStr);
305,623,185✔
1114
}
1115

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

1122
  *pHasNext = false;
483,834,202✔
1123
  if (pMTree->pIter) {
483,834,202✔
1124
    SLDataIter *pIter = pMTree->pIter;
468,619,180✔
1125
    bool        hasVal = false;
468,619,180✔
1126
    code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
468,619,180✔
1127
    if (!hasVal || (code != 0)) {
467,097,284✔
1128
      if (code == TSDB_CODE_FILE_CORRUPTED) {
5,335,909!
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,335,909✔
1133
    }
1134

1135
    // compare with min in RB Tree
1136
    pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
467,097,284✔
1137
    if (pMTree->pIter && pIter) {
467,097,284✔
1138
      int32_t c = pMTree->rbt.cmprFn(&pMTree->pIter->node, &pIter->node);
37,743,571✔
1139
      if (c > 0) {
37,609,650✔
1140
        (void)tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
5,805,335✔
1141
        pMTree->pIter = NULL;
6,711,409✔
1142
      } else if (!c) {
31,804,315!
1143
        return TSDB_CODE_INTERNAL_ERROR;
×
1144
      }
1145
    }
1146
  }
1147

1148
  if (pMTree->pIter == NULL) {
483,084,459✔
1149
    pMTree->pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
27,899,358✔
1150
    if (pMTree->pIter) {
27,899,358✔
1151
      tRBTreeDrop(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
12,050,655✔
1152
    }
1153
  }
1154

1155
  *pHasNext = (pMTree->pIter != NULL);
483,566,151✔
1156
  return code;
483,566,151✔
1157
}
1158

1159
void tMergeTreeClose(SMergeTree *pMTree) {
26,806,952✔
1160
  pMTree->pIter = NULL;
26,806,952✔
1161
  pMTree->pPinnedBlockIter = NULL;
26,806,952✔
1162
}
26,806,952✔
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