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

taosdata / TDengine / #4308

14 Jun 2025 02:06PM UTC coverage: 62.454% (-0.3%) from 62.777%
#4308

push

travis-ci

web-flow
fix: taosdump windows pthread_mutex_unlock crash(3.0) (#31357)

* fix: windows pthread_mutex_unlock crash

* enh: sync from main fix taosdump crash windows

* fix: restore .github action branch to main

153985 of 315105 branches covered (48.87%)

Branch coverage included in aggregate %.

238120 of 312727 relevant lines covered (76.14%)

6462519.65 hits per line

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

80.27
/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) {
3,849,411✔
27
  *pInfo = NULL;
3,849,411✔
28

29
  SSttBlockLoadInfo *pLoadInfo = taosMemoryCalloc(1, sizeof(SSttBlockLoadInfo));
3,849,411!
30
  if (pLoadInfo == NULL) {
3,852,317!
31
    return terrno;
×
32
  }
33

34
  pLoadInfo->blockData[0].sttBlockIndex = -1;
3,852,317✔
35
  pLoadInfo->blockData[1].sttBlockIndex = -1;
3,852,317✔
36

37
  pLoadInfo->currentLoadBlockIndex = 1;
3,852,317✔
38

39
  int32_t code = tBlockDataCreate(&pLoadInfo->blockData[0].data);
3,852,317✔
40
  if (code) {
3,850,195!
41
    taosMemoryFreeClear(pLoadInfo);
×
42
    return code;
×
43
  }
44

45
  code = tBlockDataCreate(&pLoadInfo->blockData[1].data);
3,850,195✔
46
  if (code) {
3,848,858!
47
    taosMemoryFreeClear(pLoadInfo);
×
48
    return code;
×
49
  }
50

51
  pLoadInfo->aSttBlk = taosArrayInit(4, sizeof(SSttBlk));
3,848,858✔
52
  if (pLoadInfo->aSttBlk == NULL) {
3,851,941✔
53
    taosMemoryFreeClear(pLoadInfo);
745!
54
    return terrno;
745✔
55
  }
56

57
  pLoadInfo->pSchema = pSchema;
3,851,196✔
58
  pLoadInfo->colIds = colList;
3,851,196✔
59
  pLoadInfo->numOfCols = numOfCols;
3,851,196✔
60

61
  *pInfo = pLoadInfo;
3,851,196✔
62
  return code;
3,851,196✔
63
}
64

65
static void freeItem(void *pValue) {
30,461,950✔
66
  SValue *p = (SValue *)pValue;
30,461,950✔
67
  if (IS_VAR_DATA_TYPE(p->type) || p->type == TSDB_DATA_TYPE_DECIMAL) {
30,461,950!
68
    taosMemoryFree(p->pData);
793,960!
69
  }
70
}
30,478,761✔
71

72
void destroySttBlockLoadInfo(SSttBlockLoadInfo *pLoadInfo) {
3,857,348✔
73
  if (pLoadInfo == NULL) {
3,857,348!
74
    return;
×
75
  }
76

77
  pLoadInfo->currentLoadBlockIndex = 1;
3,857,348✔
78

79
  SBlockDataInfo *pInfo = &pLoadInfo->blockData[0];
3,857,348✔
80
  tBlockDataDestroy(&pInfo->data);
3,857,348✔
81
  pInfo->sttBlockIndex = -1;
3,855,722✔
82
  pInfo->pin = false;
3,855,722✔
83

84
  pInfo = &pLoadInfo->blockData[1];
3,855,722✔
85
  tBlockDataDestroy(&pInfo->data);
3,855,722✔
86
  pInfo->sttBlockIndex = -1;
3,855,414✔
87
  pInfo->pin = false;
3,855,414✔
88

89
  taosArrayDestroy(pLoadInfo->info.pUid);
3,855,414✔
90
  taosArrayDestroyEx(pLoadInfo->info.pFirstKey, freeItem);
3,855,106✔
91
  taosArrayDestroyEx(pLoadInfo->info.pLastKey, freeItem);
3,855,203✔
92
  taosArrayDestroy(pLoadInfo->info.pCount);
3,855,331✔
93
  taosArrayDestroy(pLoadInfo->info.pFirstTs);
3,855,192✔
94
  taosArrayDestroy(pLoadInfo->info.pLastTs);
3,854,770✔
95

96
  pLoadInfo->info.pUid = NULL;
3,855,276✔
97
  pLoadInfo->info.pFirstKey = NULL;
3,855,276✔
98
  pLoadInfo->info.pLastKey = NULL;
3,855,276✔
99
  pLoadInfo->info.pCount = NULL;
3,855,276✔
100
  pLoadInfo->info.pFirstTs = NULL;
3,855,276✔
101
  pLoadInfo->info.pLastTs = NULL;
3,855,276✔
102

103
  taosArrayDestroy(pLoadInfo->aSttBlk);
3,855,276✔
104
  taosMemoryFree(pLoadInfo);
3,856,151!
105
}
106

107
void destroyLDataIter(SLDataIter *pIter) {
3,850,122✔
108
  tLDataIterClose2(pIter);
3,850,122✔
109
  destroySttBlockLoadInfo(pIter->pBlockLoadInfo);
3,857,728✔
110
  taosMemoryFree(pIter);
3,857,097!
111
}
3,858,024✔
112

113
void destroySttBlockReader(SArray *pLDataIterArray, SSttBlockLoadCostInfo *pLoadCost) {
5,311,340✔
114
  if (pLDataIterArray == NULL) {
5,311,340✔
115
    return;
63,395✔
116
  }
117

118
  int32_t numOfLevel = taosArrayGetSize(pLDataIterArray);
5,247,945✔
119
  for (int32_t i = 0; i < numOfLevel; ++i) {
9,104,046✔
120
    SArray *pList = taosArrayGetP(pLDataIterArray, i);
3,850,606✔
121
    for (int32_t j = 0; j < taosArrayGetSize(pList); ++j) {
7,707,305✔
122
      SLDataIter *pIter = taosArrayGetP(pList, j);
3,850,364✔
123
      if (pIter->pBlockLoadInfo != NULL) {
3,850,032!
124
        SSttBlockLoadCostInfo *pCost = &pIter->pBlockLoadInfo->cost;
3,850,324✔
125
        if (pLoadCost != NULL) {
3,850,324✔
126
          pLoadCost->loadBlocks += pCost->loadBlocks;
3,847,286✔
127
          pLoadCost->loadStatisBlocks += pCost->loadStatisBlocks;
3,847,286✔
128
          pLoadCost->blockElapsedTime += pCost->blockElapsedTime;
3,847,286✔
129
          pLoadCost->statisElapsedTime += pCost->statisElapsedTime;
3,847,286✔
130
        }
131
      }
132

133
      destroyLDataIter(pIter);
3,850,032✔
134
    }
135

136
    taosArrayDestroy(pList);
3,846,948✔
137
  }
138

139
  taosArrayDestroy(pLDataIterArray);
5,253,440✔
140
}
141

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

149
  pLoadInfo->currentLoadBlockIndex = nextSlotIndex;
1,738,838✔
150
}
1,738,838✔
151

152
static int32_t loadLastBlock(SLDataIter *pIter, const char *idStr, SBlockData **pResBlock) {
645,182,549✔
153
  if (pResBlock != NULL) {
645,182,549!
154
    *pResBlock = NULL;
645,408,203✔
155
  }
156

157
  int32_t            code = 0;
645,182,549✔
158
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
645,182,549✔
159

160
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
645,182,549✔
161
    if (pInfo->currentLoadBlockIndex != 0) {
365,554,414✔
162
      tsdbDebug("current load index is set to 0, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
608✔
163
                ", load data, %s",
164
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
165
      pInfo->currentLoadBlockIndex = 0;
608✔
166
    }
167

168
    *pResBlock = &pInfo->blockData[0].data;
365,554,414✔
169
    return code;
365,554,414✔
170
  }
171

172
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
279,628,135!
173
    if (pInfo->currentLoadBlockIndex != 1) {
300,573,107✔
174
      tsdbDebug("current load index is set to 1, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
652✔
175
                ", load data, %s",
176
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
177
      pInfo->currentLoadBlockIndex = 1;
652✔
178
    }
179

180
    *pResBlock = &pInfo->blockData[1].data;
300,573,107✔
181
    return code;
300,573,107✔
182
  }
183

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

188
  updateBlockLoadSlot(pInfo);
1,739,380✔
189
  int64_t st = taosGetTimestampUs();
1,740,223✔
190

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

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

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

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

215
  *pResBlock = &pInfo->blockData[pInfo->currentLoadBlockIndex].data;
1,740,698✔
216
  return code;
1,740,698✔
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,559,105✔
223
  int32_t step = backward ? 1 : -1;
2,559,105✔
224
  while (i >= 0 && i < num && uid >= pBlockList[i].minUid && uid <= pBlockList[i].maxUid) {
5,160,287!
225
    i += step;
2,601,182✔
226
  }
227
  return i - step;
2,559,105✔
228
}
229

230
static int32_t binarySearchForStartBlock(SSttBlk *pBlockList, int32_t num, uint64_t uid, int32_t backward) {
6,715,894✔
231
  int32_t midPos = -1;
6,715,894✔
232
  if (num <= 0) {
6,715,894✔
233
    return -1;
3,415,792✔
234
  }
235

236
  int32_t firstPos = 0;
3,300,102✔
237
  int32_t lastPos = num - 1;
3,300,102✔
238

239
  // find the first position which is bigger than the key
240
  if ((uid > pBlockList[lastPos].maxUid) || (uid < pBlockList[firstPos].minUid)) {
3,300,102✔
241
    return -1;
740,829✔
242
  }
243

244
  while (1) {
228,094✔
245
    if (uid >= pBlockList[firstPos].minUid && uid <= pBlockList[firstPos].maxUid) {
2,787,367✔
246
      return findEarliestIndex(firstPos, uid, pBlockList, num, backward);
2,459,914✔
247
    }
248

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

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

256
    if (uid < pBlockList[midPos].minUid) {
327,285✔
257
      lastPos = midPos - 1;
104,253✔
258
    } else if (uid > pBlockList[midPos].maxUid) {
223,032✔
259
      firstPos = midPos + 1;
123,841✔
260
    } else {
261
      return findEarliestIndex(midPos, uid, pBlockList, num, backward);
99,191✔
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,529,509✔
269
  int32_t step = backward ? 1 : -1;
2,529,509✔
270
  while (i >= 0 && i < num && uid == uidList[i]) {
65,695,909!
271
    i += step;
63,166,400✔
272
  }
273
  return i - step;
2,529,509✔
274
}
275

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

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

285
  while (1) {
1,177,739✔
286
    if (uid == uidList[firstPos]) {
3,722,595✔
287
      return findEarliestRow(firstPos, uid, uidList, num, backward);
1,640,021✔
288
    }
289

290
    if (uid > uidList[lastPos] || uid < uidList[firstPos]) {
2,082,574✔
291
      return -1;
15,347✔
292
    }
293

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

297
    if (uid < uidList[midPos]) {
2,067,227✔
298
      lastPos = midPos - 1;
401,808✔
299
    } else if (uid > uidList[midPos]) {
1,665,419✔
300
      firstPos = midPos + 1;
775,931✔
301
    } else {
302
      return findEarliestRow(midPos, uid, uidList, num, backward);
889,488✔
303
    }
304
  }
305
}
306

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

315
  SSttBlk *pStart = &pArray->data[0];
3,846,635✔
316
  SSttBlk *pEnd = &pArray->data[TARRAY2_SIZE(pArray) - 1];
3,846,635✔
317

318
  // all identical
319
  if (pStart->suid == pEnd->suid) {
3,846,635✔
320
    if (pStart->suid != suid) {  // no qualified stt block existed
3,015,931✔
321
      taosArrayClear(pBlockLoadInfo->aSttBlk);
1,457,644✔
322
      pIter->iSttBlk = -1;
1,456,956✔
323
      return TSDB_CODE_SUCCESS;
1,456,956✔
324
    } else {  // all blocks are qualified
325
      taosArrayClear(pBlockLoadInfo->aSttBlk);
1,558,287✔
326
      px = taosArrayAddBatch(pBlockLoadInfo->aSttBlk, pArray->data, pArray->size);
1,559,997✔
327
      if (px == NULL) {
1,558,963!
328
        return terrno;
×
329
      }
330
    }
331
  } else {
332
    SArray *pTmp = taosArrayInit(TARRAY2_SIZE(pArray), sizeof(SSttBlk));
830,704✔
333
    if (pTmp == NULL) {
836,955!
334
      return terrno;
×
335
    }
336

337
    for (int32_t i = 0; i < TARRAY2_SIZE(pArray); ++i) {
2,602,493✔
338
      SSttBlk *p = &pArray->data[i];
2,214,511✔
339
      if (p->suid < suid) {
2,214,511✔
340
        continue;
1,249,282✔
341
      }
342

343
      if (p->suid == suid) {
965,229✔
344
        void *px = taosArrayPush(pTmp, p);
516,360✔
345
        if (px == NULL) {
516,360!
346
          code = terrno;
×
347
          break;
×
348
        }
349
      } else if (p->suid > suid) {
448,834!
350
        break;
448,955✔
351
      }
352
    }
353

354
    taosArrayDestroy(pBlockLoadInfo->aSttBlk);
836,937✔
355
    pBlockLoadInfo->aSttBlk = pTmp;
836,778✔
356
  }
357

358
  return code;
2,395,741✔
359
}
360

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

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

373
  return TSDB_CODE_SUCCESS;
1,993,441✔
374
}
375

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

384
  int32_t numOfBlocks = TARRAY2_SIZE(pStatisBlkArray);
3,846,456✔
385
  if (numOfBlocks <= 0) {
3,846,456✔
386
    return code;
265✔
387
  }
388

389
  while ((startIndex < numOfBlocks) && (pStatisBlkArray->data[startIndex].maxTbid.suid < suid)) {
4,981,599✔
390
    ++startIndex;
1,135,408✔
391
  }
392

393
  if (startIndex >= numOfBlocks || pStatisBlkArray->data[startIndex].minTbid.suid > suid) {
3,846,191✔
394
    return 0;
1,738,105✔
395
  }
396

397
  int32_t endIndex = startIndex;
2,108,086✔
398
  while (endIndex < numOfBlocks && pStatisBlkArray->data[endIndex].minTbid.suid <= suid) {
4,234,651✔
399
    ++endIndex;
2,126,565✔
400
  }
401

402
  int32_t num = endIndex - startIndex;
2,108,086✔
403
  pBlockLoadInfo->cost.loadStatisBlocks += num;
2,108,086✔
404

405
  STbStatisBlock block;
406
  code = tStatisBlockInit(&block);
2,108,086✔
407
  QUERY_CHECK_CODE(code, lino, _end);
2,107,548!
408

409
  int64_t st = taosGetTimestampUs();
2,107,513✔
410

411
  for (int32_t k = startIndex; k < endIndex; ++k) {
4,218,066✔
412
    code = tsdbSttFileReadStatisBlock(pSttFileReader, &pStatisBlkArray->data[k], &block);
2,124,187✔
413
    QUERY_CHECK_CODE(code, lino, _end);
2,126,655!
414

415
    int32_t i = 0;
2,126,655✔
416
    int32_t rows = block.numOfRecords;
2,126,655✔
417
    while (i < rows && ((int64_t *)block.suids.data)[i] != suid) {
3,064,637✔
418
      ++i;
937,982✔
419
    }
420

421
    // existed
422
    if (i < rows) {
2,126,655✔
423
      SSttTableRowsInfo *pInfo = &pBlockLoadInfo->info;
2,035,923✔
424

425
      if (pInfo->pUid == NULL) {
2,035,923✔
426
        pInfo->pUid = taosArrayInit(rows, sizeof(int64_t));
2,017,423✔
427
        pInfo->pFirstTs = taosArrayInit(rows, sizeof(int64_t));
2,017,636✔
428
        pInfo->pLastTs = taosArrayInit(rows, sizeof(int64_t));
2,017,830✔
429
        pInfo->pCount = taosArrayInit(rows, sizeof(int64_t));
2,017,866✔
430

431
        pInfo->pFirstKey = taosArrayInit(rows, sizeof(SValue));
2,017,874✔
432
        pInfo->pLastKey = taosArrayInit(rows, sizeof(SValue));
2,017,836✔
433

434
        if (pInfo->pUid == NULL || pInfo->pFirstTs == NULL || pInfo->pLastTs == NULL || pInfo->pCount == NULL ||
2,017,845!
435
            pInfo->pFirstKey == NULL || pInfo->pLastKey == NULL) {
2,017,863✔
436
          code = terrno;
207✔
437
          goto _end;
×
438
        }
439
      }
440

441
      if (pStatisBlkArray->data[k].maxTbid.suid == suid) {
2,036,138✔
442
        int32_t size = rows - i;
1,906,000✔
443
        int32_t offset = i * sizeof(int64_t);
1,906,000✔
444

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

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

451
        px = taosArrayAddBatch(pInfo->pLastTs, tBufferGetDataAt(&block.lastKeyTimestamps, offset), size);
1,905,454✔
452
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,904,672!
453

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

457
        if (block.numOfPKs > 0) {
1,904,459✔
458
          SValue vFirst = {0}, vLast = {0};
412,931✔
459
          for (int32_t f = i; f < rows; ++f) {
1,305,025✔
460
            code = tValueColumnGet(&block.firstKeyPKs[0], f, &vFirst);
891,442✔
461
            TSDB_CHECK_CODE(code, lino, _end);
891,562!
462

463
            code = tValueDupPayload(&vFirst);
891,562✔
464
            TSDB_CHECK_CODE(code, lino, _end);
891,784!
465

466
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
891,784✔
467
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
892,037!
468

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

473
            code = tValueDupPayload(&vLast);
891,987✔
474
            TSDB_CHECK_CODE(code, lino, _end);
892,035!
475

476
            px = taosArrayPush(pInfo->pLastKey, &vLast);
892,035✔
477
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
892,094!
478
          }
479
        } else {
480
          SValue vFirst = {0};
1,491,528✔
481
          for (int32_t j = 0; j < size; ++j) {
14,983,051✔
482
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
13,506,963✔
483
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
13,489,887!
484

485
            px = taosArrayPush(pInfo->pLastKey, &vFirst);
13,489,887✔
486
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
13,491,523!
487
          }
488
        }
489
      } else {
490
        STbStatisRecord record = {0};
130,138✔
491
        while (i < rows) {
312,696✔
492
          code = tStatisBlockGet(&block, i, &record);
312,690✔
493
          TSDB_CHECK_CODE(code, lino, _end);
312,547!
494

495
          if (record.suid != suid) {
312,547✔
496
            break;
130,144✔
497
          }
498

499
          px = taosArrayPush(pInfo->pUid, &record.uid);
182,403✔
500
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
182,477!
501

502
          px = taosArrayPush(pInfo->pCount, &record.count);
182,477✔
503
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
182,476!
504

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

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

511
          if (record.firstKey.numOfPKs > 0) {
182,444✔
512
            SValue s = record.firstKey.pks[0];
105,600✔
513
            code = tValueDupPayload(&s);
105,600✔
514
            TSDB_CHECK_CODE(code, lino, _end);
105,609!
515

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

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

523
            px = taosArrayPush(pInfo->pLastKey, &s);
105,614✔
524
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
105,620!
525
          } else {
526
            SValue v = {0};
76,844✔
527
            px = taosArrayPush(pInfo->pFirstKey, &v);
76,844✔
528
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
76,962!
529

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

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

540
_end:
2,093,879✔
541
  tStatisBlockDestroy(&block);
2,093,879✔
542
  if (code != 0) {
2,107,662!
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;
2,107,864✔
546
    pBlockLoadInfo->cost.statisElapsedTime += el;
2,107,864✔
547

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

553
static int32_t doLoadSttFilesBlk(SSttBlockLoadInfo *pBlockLoadInfo, SLDataIter *pIter, int64_t suid,
3,849,164✔
554
                                 _load_tomb_fn loadTombFn, void *pReader1, const char *idStr) {
555
  int64_t st = taosGetTimestampUs();
3,851,761✔
556

557
  const TSttBlkArray *pSttBlkArray = NULL;
3,851,761✔
558
  pBlockLoadInfo->sttBlockLoaded = true;
3,851,761✔
559

560
  // load the stt block info for each stt-block
561
  int32_t code = tsdbSttFileReadSttBlk(pIter->pReader, &pSttBlkArray);
3,851,761✔
562
  if (code != TSDB_CODE_SUCCESS) {
3,852,338!
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);
3,852,338✔
569
  if (code != TSDB_CODE_SUCCESS) {
3,850,726!
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;
3,850,726✔
576
  code = tsdbSttFileReadStatisBlk(pIter->pReader, (const TStatisBlkArray **)&pStatisBlkArray);
3,850,726✔
577
  if (code != TSDB_CODE_SUCCESS) {
3,849,399!
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);
3,849,399✔
584
  if (code != TSDB_CODE_SUCCESS) {
3,849,529!
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);
3,849,529✔
590

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

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

600
  if (*pFirst == *pVal) {
5,165,165✔
601
    return 0;
2,551,475✔
602
  } else {
603
    return *pFirst < *pVal ? -1 : 1;
2,613,690✔
604
  }
605
}
606

607
static void setSttInfoForCurrentTable(SSttBlockLoadInfo *pLoadInfo, uint64_t uid, SSttKeyRange *pRange,
6,723,633✔
608
                                      int64_t *numOfRows) {
609
  if (pRange == NULL || taosArrayGetSize(pLoadInfo->info.pUid) == 0) {
6,723,633!
610
    return;
3,418,100✔
611
  }
612

613
  int32_t index = taosArraySearchIdx(pLoadInfo->info.pUid, &uid, uidComparFn, TD_EQ);
3,303,197✔
614
  if (index >= 0) {
3,307,854✔
615
    pRange->skey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pFirstTs, index);
2,551,772✔
616
    pRange->ekey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pLastTs, index);
2,550,939✔
617

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

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

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

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

640
  pIter->pStartRowKey = pConf->pCurRowKey;
6,726,078✔
641
  pIter->pReader = pSttFileReader;
6,726,078✔
642
  pIter->pBlockLoadInfo = pBlockLoadInfo;
6,726,078✔
643

644
  // open stt file failed, ignore and continue
645
  if (pIter->pReader == NULL) {
6,726,078!
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) {
6,726,078✔
653
    code = doLoadSttFilesBlk(pBlockLoadInfo, pIter, pConf->suid, pConf->loadTombFn, pConf->pReader, idStr);
3,852,001✔
654
    if (code != TSDB_CODE_SUCCESS) {
3,848,662!
655
      return code;
×
656
    }
657
  }
658

659
  setSttInfoForCurrentTable(pBlockLoadInfo, pConf->uid, pKeyRange, numOfRows);
6,722,739✔
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);
6,719,895✔
664
  pIter->iSttBlk = binarySearchForStartBlock(pBlockLoadInfo->aSttBlk->pData, size, pConf->uid, backward);
6,718,016✔
665
  if (pIter->iSttBlk != -1) {
6,714,264✔
666
    pIter->pSttBlk = taosArrayGet(pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
2,566,154✔
667
    pIter->iRow = (pIter->backward) ? pIter->pSttBlk->nRow : -1;
2,566,016✔
668

669
    if ((!backward) && ((pConf->strictTimeRange && pIter->pSttBlk->minKey >= pIter->timeWindow.ekey) ||
2,566,016!
670
                        (!pConf->strictTimeRange && pIter->pSttBlk->minKey > pIter->timeWindow.ekey))) {
2,093,132!
671
      pIter->pSttBlk = NULL;
1,090✔
672
    }
673

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

681
  return code;
6,714,126✔
682
}
683

684
void tLDataIterClose2(SLDataIter *pIter) {
3,849,255✔
685
  tsdbSttFileReaderClose(&pIter->pReader);
3,849,255✔
686
  pIter->pReader = NULL;
3,857,789✔
687
}
3,857,789✔
688

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

693
  pIter->iSttBlk += step;
2,597,874✔
694

695
  int32_t index = -1;
2,597,874✔
696
  size_t  size = pIter->pBlockLoadInfo->aSttBlk->size;
2,597,874✔
697
  for (int32_t i = pIter->iSttBlk; i < size && i >= 0; i += step) {
2,599,541!
698
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, i);
222,677✔
699
    if ((!pIter->backward) && p->minUid > pIter->uid) {
222,669✔
700
      break;
126,857✔
701
    }
702

703
    if (pIter->backward && p->maxUid < pIter->uid) {
95,812✔
704
      break;
14,475✔
705
    }
706

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

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

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

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

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

735
  pIter->pSttBlk = NULL;
2,597,866✔
736
  if (index != -1) {
2,597,866✔
737
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, index);
79,170✔
738

739
    pIter->iSttBlk = index;
79,168✔
740
    pIter->pSttBlk = (SSttBlk *)taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
79,168✔
741
    tsdbDebug("try next stt-file block:%d from %d, trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
79,170✔
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,518,696✔
746
  }
747
}
2,597,866✔
748

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

755
  int32_t code = loadLastBlock(pIter, idStr, &pData);
333,165,842✔
756
  if (code) {
330,325,829!
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) {
330,325,829✔
763
    i = binarySearchForStartRowIndex((uint64_t *)pData->aUid, pData->nRow, pIter->uid, pIter->backward);
2,545,454✔
764
    if (i == -1) {
2,545,611!
765
      tsdbDebug("failed to find the data in pBlockData, uid:%" PRIu64 " , %s", pIter->uid, idStr);
×
766
      pIter->iRow = -1;
15,583✔
767
      return code;
15,583✔
768
    }
769
  }
770

771
  for (; i < pData->nRow && i >= 0; i += step) {
340,441,787!
772
    if (pData->aUid != NULL) {
340,086,186✔
773
      if (!pIter->backward) {
335,352,472✔
774
        if (pData->aUid[i] > pIter->uid) {
317,539,631✔
775
          break;
681,815✔
776
        }
777
      } else {
778
        if (pData->aUid[i] < pIter->uid) {
17,812,841✔
779
          break;
264,531✔
780
        }
781
      }
782
    }
783

784
    int64_t ts = pData->aTSKEY[i];
339,139,840✔
785
    if (!pIter->backward) {               // asc
339,139,840✔
786
      if (ts > pIter->timeWindow.ekey) {  // no more data
321,098,759✔
787
        break;
4,777✔
788
      } else {
789
        if (ts < pIter->timeWindow.skey) {
321,093,982✔
790
          continue;
7,761,313✔
791
        }
792

793
        if (ts == pIter->timeWindow.skey && pIter->pStartRowKey->numOfPKs > 0) {
313,332,669✔
794
          SRowKey key;
795
          tColRowGetKey(pData, i, &key);
252!
796
          int32_t ret = pkCompEx(&key, pIter->pStartRowKey);
252✔
797
          if (ret < 0) {
252!
798
            continue;
×
799
          }
800
        }
801
      }
802
    } else {
803
      if (ts < pIter->timeWindow.skey) {
18,041,081✔
804
        break;
2,474✔
805
      } else {
806
        if (ts > pIter->timeWindow.ekey) {
18,038,607✔
807
          continue;
1,902,995✔
808
        }
809

810
        if (ts == pIter->timeWindow.ekey && pIter->pStartRowKey->numOfPKs > 0) {
16,135,612!
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];
329,468,281✔
822
    if (ver < pIter->verRange.minVer) {
329,468,281!
823
      continue;
×
824
    }
825

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

831
    hasVal = true;
329,468,281✔
832
    break;
329,468,281✔
833
  }
834

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

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

846
  *hasNext = false;
337,537,270✔
847

848
  // no qualified last file block in current file, no need to fetch row
849
  if (pIter->pSttBlk == NULL) {
337,537,270✔
850
    return code;
4,157,112✔
851
  }
852

853
  code = loadLastBlock(pIter, idStr, &pBlockData);
333,380,158✔
854
  if (pBlockData == NULL || code != TSDB_CODE_SUCCESS) {
332,589,415!
855
    lino = __LINE__;
×
856
    goto _exit;
×
857
  }
858

859
  pIter->iRow += step;
333,033,659✔
860

861
  while (1) {
78,508✔
862
    bool skipBlock = false;
333,112,167✔
863
    code = findNextValidRow(pIter, idStr);
333,112,167✔
864
    TSDB_CHECK_CODE(code, lino, _exit);
331,259,136!
865

866
    if (pIter->pBlockLoadInfo->checkRemainingRow) {
331,259,136!
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) {
331,259,136!
890
      tLDataIterNextBlock(pIter, idStr);
2,465,100✔
891
      if (pIter->pSttBlk == NULL) {  // no more data
2,597,257✔
892
        goto _exit;
2,518,750✔
893
      }
894
    } else {
895
      break;
896
    }
897

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

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

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

914
_exit:
330,868,541✔
915
  if (code) {
330,868,541!
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);
331,171,316!
920
  return code;
331,171,316✔
921
}
922

923
// SMergeTree =================================================
924
static FORCE_INLINE int32_t tLDataIterCmprFn(const SRBTreeNode *p1, const SRBTreeNode *p2) {
90,238,783✔
925
  SLDataIter *pIter1 = (SLDataIter *)(((uint8_t *)p1) - offsetof(SLDataIter, node));
90,339,285✔
926
  SLDataIter *pIter2 = (SLDataIter *)(((uint8_t *)p2) - offsetof(SLDataIter, node));
90,339,285✔
927

928
  SRowKey rkey1 = {0}, rkey2 = {0};
90,339,285✔
929
  tRowGetKeyEx(&pIter1->rInfo.row, &rkey1);
90,339,285!
930
  tRowGetKeyEx(&pIter2->rInfo.row, &rkey2);
90,849,658!
931

932
  int32_t ret = tRowKeyCompare(&rkey1, &rkey2);
90,687,020✔
933
  if (ret < 0) {
90,555,088✔
934
    return -1;
46,704,342✔
935
  } else if (ret > 0) {
43,850,746✔
936
    return 1;
17,637,677✔
937
  } else {
938
    int64_t ver1 = TSDBROW_VERSION(&pIter1->rInfo.row);
26,213,069!
939
    int64_t ver2 = TSDBROW_VERSION(&pIter2->rInfo.row);
26,213,069!
940

941
    if (ver1 < ver2) {
26,213,069✔
942
      return -1;
10,414,377✔
943
    } else if (ver1 > ver2) {
15,798,692!
944
      return 1;
17,504,358✔
945
    } else {
946
      return 0;
×
947
    }
948
  }
949
}
950

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

955
int32_t tMergeTreeOpen2(SMergeTree *pMTree, SMergeTreeConf *pConf, SSttDataInfoForTable *pSttDataInfo) {
7,082,628✔
956
  int32_t code = TSDB_CODE_SUCCESS;
7,082,628✔
957

958
  pMTree->pIter = NULL;
7,082,628✔
959
  pMTree->backward = pConf->backward;
7,082,628✔
960
  pMTree->idStr = pConf->idstr;
7,082,628✔
961
  int32_t lino = 0;
7,082,628✔
962

963
  if (!pMTree->backward) {  // asc
7,082,628✔
964
    tRBTreeCreate(&pMTree->rbt, tLDataIterCmprFn);
5,130,224✔
965
  } else {  // desc
966
    tRBTreeCreate(&pMTree->rbt, tLDataIterDescCmprFn);
1,952,404✔
967
  }
968

969
  pMTree->ignoreEarlierTs = false;
7,082,272✔
970

971
  // no data exists, go to end
972
  int32_t numOfLevels = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->size;
7,082,272✔
973
  if (numOfLevels == 0) {
7,082,272✔
974
    goto _end;
535,854✔
975
  }
976

977
  code = adjustSttDataIters(pConf->pSttFileBlockIterArray, pConf->pCurrentFileset);
6,546,418✔
978
  if (code) {
6,542,534!
979
    goto _end;
×
980
  }
981

982
  for (int32_t j = 0; j < numOfLevels; ++j) {
13,245,487✔
983
    SSttLvl *pSttLevel = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->data[j];
6,706,472✔
984
    SArray  *pList = taosArrayGetP(pConf->pSttFileBlockIterArray, j);
6,706,472✔
985

986
    for (int32_t i = 0; i < TARRAY2_SIZE(pSttLevel->fobjArr); ++i) {  // open all last file
13,422,202✔
987
      SLDataIter *pIter = taosArrayGetP(pList, i);
6,719,249✔
988

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

998
        code = tsdbSttFileReaderOpen(pSttLevel->fobjArr->data[i]->fname, &conf, &pSttFileReader);
3,848,528✔
999
        if (code != TSDB_CODE_SUCCESS) {
3,847,402!
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) {
6,715,202✔
1006
        code = tCreateSttBlockLoadInfo(pConf->pSchema, pConf->pCols, pConf->numOfCols, &pLoadInfo);
3,852,601✔
1007
        if (code != TSDB_CODE_SUCCESS) {
3,850,860!
1008
          goto _end;
×
1009
        }
1010
      }
1011

1012
      memset(pIter, 0, sizeof(SLDataIter));
6,713,461✔
1013

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

1018
      code = tLDataIterOpen2(pIter, pSttFileReader, cid, pMTree->backward, pConf, pLoadInfo, &range, &numOfRows,
6,713,461✔
1019
                             pMTree->idStr);
1020
      if (code != TSDB_CODE_SUCCESS) {
6,713,551!
1021
        goto _end;
×
1022
      }
1023

1024
      bool hasVal = NULL;
6,713,551✔
1025
      code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
6,713,551✔
1026
      if (code) {
6,716,608!
1027
        goto _end;
×
1028
      }
1029

1030
      if (hasVal) {
6,716,608✔
1031
        tMergeTreeAddIter(pMTree, pIter);
2,542,509✔
1032

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

1038
          pSttDataInfo->numOfRows += numOfRows;
2,540,814✔
1039
        }
1040
      } else {
1041
        if (!pMTree->ignoreEarlierTs) {
4,174,099!
1042
          pMTree->ignoreEarlierTs = pIter->ignoreEarlierTs;
4,177,879✔
1043
        }
1044
      }
1045
    }
1046
  }
1047

1048
  return code;
6,539,015✔
1049

1050
_end:
535,854✔
1051
  tMergeTreeClose(pMTree);
535,854✔
1052
  return code;
535,728✔
1053
}
1054

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

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

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

1064
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
276,517,623✔
1065
    pInfo->blockData[0].pin = true;
155,189,806✔
1066
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
155,189,806✔
1067
    return;
155,285,332✔
1068
  }
1069

1070
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
121,327,817!
1071
    pInfo->blockData[1].pin = true;
127,110,411✔
1072
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
127,110,411✔
1073
    return;
127,031,741✔
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) {
271,530,934✔
1080
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
271,530,934✔
1081
  if (pInfo->blockData[0].pin) {
271,530,934✔
1082
    pInfo->blockData[0].pin = false;
153,280,443✔
1083
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[0].sttBlockIndex, pIter->cid, id);
153,280,443✔
1084
    return;
153,287,110✔
1085
  }
1086

1087
  if (pInfo->blockData[1].pin) {
118,250,491!
1088
    pInfo->blockData[1].pin = false;
125,829,202✔
1089
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[1].sttBlockIndex, pIter->cid, id);
125,829,202✔
1090
    return;
125,832,187✔
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) {
276,605,910✔
1097
  if (pMTree->pIter == NULL) {
276,605,910!
1098
    return;
×
1099
  }
1100

1101
  SLDataIter *pIter = pMTree->pIter;
276,605,910✔
1102
  pMTree->pPinnedBlockIter = pIter;
276,605,910✔
1103
  tLDataIterPinSttBlock(pIter, pMTree->idStr);
276,605,910✔
1104
}
1105

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

1111
  SLDataIter *pIter = pMTree->pPinnedBlockIter;
271,609,210✔
1112
  pMTree->pPinnedBlockIter = NULL;
271,609,210✔
1113
  tLDataIterUnpinSttBlock(pIter, pMTree->idStr);
271,609,210✔
1114
}
1115

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

1122
  *pHasNext = false;
337,687,525✔
1123
  while (pMTree->pIter) {
337,687,525✔
1124
    SLDataIter *pIter = pMTree->pIter;
330,610,545✔
1125
    bool        hasVal = false;
330,610,545✔
1126
    code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
330,610,545✔
1127
    if (!hasVal || (code != 0)) {
328,348,019✔
1128
      if (code == TSDB_CODE_FILE_CORRUPTED) {
2,495,098!
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,495,098✔
1133
    }
1134

1135
    // compare with min in RB Tree
1136
    pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
328,348,019✔
1137
    if (pMTree->pIter && pIter) {
328,348,019✔
1138
      int32_t c = pMTree->rbt.cmprFn(&pMTree->pIter->node, &pIter->node);
46,670,327✔
1139
      if (c > 0) {
46,298,910✔
1140
        (void)tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
10,399,266✔
1141
        pMTree->pIter = NULL;
10,181,057✔
1142
      } else if (!c) {
35,899,644!
1143
        continue;
×
1144
      }
1145
    }
1146

1147
    break;
327,758,393✔
1148
  }
1149

1150
  if (pMTree->pIter == NULL) {
334,835,373✔
1151
    pMTree->pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
19,692,395✔
1152
    if (pMTree->pIter) {
19,692,395✔
1153
      tRBTreeDrop(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
12,675,602✔
1154
    }
1155
  }
1156

1157
  *pHasNext = (pMTree->pIter != NULL);
334,734,611✔
1158
  return code;
334,734,611✔
1159
}
1160

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