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

taosdata / TDengine / #4832

31 Oct 2025 03:37AM UTC coverage: 58.59% (-0.2%) from 58.771%
#4832

push

travis-ci

SallyHuo-TAOS
Merge remote-tracking branch 'origin/cover/3.0' into cover/3.0

# Conflicts:
#	test/ci/run.sh

149363 of 324176 branches covered (46.07%)

Branch coverage included in aggregate %.

198471 of 269498 relevant lines covered (73.64%)

239014265.14 hits per line

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

80.72
/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) {
1,222,159,089✔
27
  *pInfo = NULL;
1,222,159,089✔
28

29
  SSttBlockLoadInfo *pLoadInfo = taosMemoryCalloc(1, sizeof(SSttBlockLoadInfo));
1,222,503,096✔
30
  if (pLoadInfo == NULL) {
1,221,863,739!
31
    return terrno;
×
32
  }
33

34
  pLoadInfo->blockData[0].sttBlockIndex = -1;
1,221,863,739✔
35
  pLoadInfo->blockData[1].sttBlockIndex = -1;
1,221,914,509✔
36

37
  pLoadInfo->currentLoadBlockIndex = 1;
1,221,974,624✔
38

39
  int32_t code = tBlockDataCreate(&pLoadInfo->blockData[0].data);
1,222,177,710✔
40
  if (code) {
1,222,491,163!
41
    taosMemoryFreeClear(pLoadInfo);
×
42
    return code;
×
43
  }
44

45
  code = tBlockDataCreate(&pLoadInfo->blockData[1].data);
1,222,491,163✔
46
  if (code) {
1,222,456,616!
47
    taosMemoryFreeClear(pLoadInfo);
×
48
    return code;
×
49
  }
50

51
  pLoadInfo->aSttBlk = taosArrayInit(4, sizeof(SSttBlk));
1,222,456,616✔
52
  if (pLoadInfo->aSttBlk == NULL) {
1,222,683,717!
53
    taosMemoryFreeClear(pLoadInfo);
×
54
    return terrno;
×
55
  }
56

57
  pLoadInfo->pSchema = pSchema;
1,222,709,718✔
58
  pLoadInfo->colIds = colList;
1,222,799,875✔
59
  pLoadInfo->numOfCols = numOfCols;
1,222,759,048✔
60

61
  *pInfo = pLoadInfo;
1,222,784,205✔
62
  return code;
1,222,684,217✔
63
}
64

65
static void freeItem(void *pValue) {
2,147,483,647✔
66
  SValue *p = (SValue *)pValue;
2,147,483,647✔
67
  if (IS_VAR_DATA_TYPE(p->type) || p->type == TSDB_DATA_TYPE_DECIMAL) {
2,147,483,647!
68
    taosMemoryFree(p->pData);
183,783,981!
69
  }
70
}
2,147,483,647✔
71

72
void destroySttBlockLoadInfo(SSttBlockLoadInfo *pLoadInfo) {
1,222,331,340✔
73
  if (pLoadInfo == NULL) {
1,222,331,340!
74
    return;
×
75
  }
76

77
  pLoadInfo->currentLoadBlockIndex = 1;
1,222,331,340✔
78

79
  SBlockDataInfo *pInfo = &pLoadInfo->blockData[0];
1,222,737,999✔
80
  tBlockDataDestroy(&pInfo->data);
1,222,566,894✔
81
  pInfo->sttBlockIndex = -1;
1,222,516,891✔
82
  pInfo->pin = false;
1,222,397,457✔
83

84
  pInfo = &pLoadInfo->blockData[1];
1,222,592,157✔
85
  tBlockDataDestroy(&pInfo->data);
1,222,730,131✔
86
  pInfo->sttBlockIndex = -1;
1,222,807,112✔
87
  pInfo->pin = false;
1,222,665,276✔
88

89
  taosArrayDestroy(pLoadInfo->info.pUid);
1,222,881,819✔
90
  taosArrayDestroyEx(pLoadInfo->info.pFirstKey, freeItem);
1,222,651,484✔
91
  taosArrayDestroyEx(pLoadInfo->info.pLastKey, freeItem);
1,222,443,810✔
92
  taosArrayDestroy(pLoadInfo->info.pCount);
1,222,816,313✔
93
  taosArrayDestroy(pLoadInfo->info.pFirstTs);
1,222,798,514✔
94
  taosArrayDestroy(pLoadInfo->info.pLastTs);
1,222,769,450✔
95

96
  pLoadInfo->info.pUid = NULL;
1,222,766,552✔
97
  pLoadInfo->info.pFirstKey = NULL;
1,222,785,651✔
98
  pLoadInfo->info.pLastKey = NULL;
1,222,615,202✔
99
  pLoadInfo->info.pCount = NULL;
1,222,870,513✔
100
  pLoadInfo->info.pFirstTs = NULL;
1,222,718,365✔
101
  pLoadInfo->info.pLastTs = NULL;
1,222,780,502✔
102

103
  taosArrayDestroy(pLoadInfo->aSttBlk);
1,222,563,693✔
104
  taosMemoryFree(pLoadInfo);
1,222,772,849✔
105
}
106

107
void destroyLDataIter(SLDataIter *pIter) {
1,222,765,319✔
108
  tLDataIterClose2(pIter);
1,222,765,319✔
109
  destroySttBlockLoadInfo(pIter->pBlockLoadInfo);
1,222,584,133✔
110
  taosMemoryFree(pIter);
1,222,711,812✔
111
}
1,222,537,982✔
112

113
void destroySttBlockReader(SArray *pLDataIterArray, SSttBlockLoadCostInfo *pLoadCost) {
2,147,483,647✔
114
  if (pLDataIterArray == NULL) {
2,147,483,647✔
115
    return;
37,570,227✔
116
  }
117

118
  int32_t numOfLevel = taosArrayGetSize(pLDataIterArray);
2,147,483,647✔
119
  for (int32_t i = 0; i < numOfLevel; ++i) {
2,147,483,647✔
120
    SArray *pList = taosArrayGetP(pLDataIterArray, i);
1,215,454,460✔
121
    for (int32_t j = 0; j < taosArrayGetSize(pList); ++j) {
2,147,483,647✔
122
      SLDataIter *pIter = taosArrayGetP(pList, j);
1,222,949,161✔
123
      if (pIter->pBlockLoadInfo != NULL) {
1,222,912,927!
124
        SSttBlockLoadCostInfo *pCost = &pIter->pBlockLoadInfo->cost;
1,223,001,526✔
125
        if (pLoadCost != NULL) {
1,222,989,585✔
126
          pLoadCost->loadBlocks += pCost->loadBlocks;
1,222,590,125✔
127
          pLoadCost->loadStatisBlocks += pCost->loadStatisBlocks;
1,222,631,722✔
128
          pLoadCost->blockElapsedTime += pCost->blockElapsedTime;
1,222,621,432✔
129
          pLoadCost->statisElapsedTime += pCost->statisElapsedTime;
1,222,570,351✔
130
        }
131
      }
132

133
      destroyLDataIter(pIter);
1,222,983,846✔
134
    }
135

136
    taosArrayDestroy(pList);
1,215,225,564✔
137
  }
138

139
  taosArrayDestroy(pLDataIterArray);
2,147,483,647✔
140
}
141

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

149
  pLoadInfo->currentLoadBlockIndex = nextSlotIndex;
655,757,145✔
150
}
655,686,775✔
151

152
static int32_t loadLastBlock(SLDataIter *pIter, const char *idStr, SBlockData **pResBlock) {
2,147,483,647✔
153
  if (pResBlock != NULL) {
2,147,483,647!
154
    *pResBlock = NULL;
2,147,483,647✔
155
  }
156

157
  int32_t            code = 0;
2,147,483,647✔
158
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
2,147,483,647✔
159

160
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
2,147,483,647✔
161
    if (pInfo->currentLoadBlockIndex != 0) {
2,147,483,647✔
162
      tsdbDebug("current load index is set to 0, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
356,352!
163
                ", load data, %s",
164
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
165
      pInfo->currentLoadBlockIndex = 0;
356,352✔
166
    }
167

168
    *pResBlock = &pInfo->blockData[0].data;
2,147,483,647✔
169
    return code;
2,147,483,647✔
170
  }
171

172
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
2,147,483,647✔
173
    if (pInfo->currentLoadBlockIndex != 1) {
2,147,483,647✔
174
      tsdbDebug("current load index is set to 1, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
330,070!
175
                ", load data, %s",
176
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
177
      pInfo->currentLoadBlockIndex = 1;
330,070✔
178
    }
179

180
    *pResBlock = &pInfo->blockData[1].data;
2,147,483,647✔
181
    return code;
2,147,483,647✔
182
  }
183

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

188
  updateBlockLoadSlot(pInfo);
655,739,722✔
189
  int64_t st = taosGetTimestampUs();
656,033,849✔
190

191
  SBlockData *pBlock = &pInfo->blockData[pInfo->currentLoadBlockIndex].data;
656,033,849✔
192
  code = tsdbSttFileReadBlockDataByColumn(pIter->pReader, pIter->pSttBlk, pBlock, pInfo->pSchema, &pInfo->colIds[1],
655,890,253✔
193
                                          pInfo->numOfCols - 1);
655,792,088✔
194
  if (code != TSDB_CODE_SUCCESS) {
655,879,834!
195
    return code;
×
196
  }
197

198
  double el = (taosGetTimestampUs() - st) / 1000.0;
655,880,671✔
199
  pInfo->cost.blockElapsedTime += el;
655,880,671✔
200
  pInfo->cost.loadBlocks += 1;
655,890,630✔
201

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

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

215
  *pResBlock = &pInfo->blockData[pInfo->currentLoadBlockIndex].data;
656,001,286✔
216
  return code;
655,747,478✔
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;
1,048,080,768✔
223
  int32_t step = backward ? 1 : -1;
1,048,080,768✔
224
  while (i >= 0 && i < num && uid >= pBlockList[i].minUid && uid <= pBlockList[i].maxUid) {
2,109,633,202!
225
    i += step;
1,061,552,434✔
226
  }
227
  return i - step;
1,047,940,689✔
228
}
229

230
static int32_t binarySearchForStartBlock(SSttBlk *pBlockList, int32_t num, uint64_t uid, int32_t backward) {
2,147,483,647✔
231
  int32_t midPos = -1;
2,147,483,647✔
232
  if (num <= 0) {
2,147,483,647✔
233
    return -1;
820,142,287✔
234
  }
235

236
  int32_t firstPos = 0;
1,350,833,503✔
237
  int32_t lastPos = num - 1;
1,350,833,503✔
238

239
  // find the first position which is bigger than the key
240
  if ((uid > pBlockList[lastPos].maxUid) || (uid < pBlockList[firstPos].minUid)) {
1,350,833,503✔
241
    return -1;
303,100,196✔
242
  }
243

244
  while (1) {
85,845,667✔
245
    if (uid >= pBlockList[firstPos].minUid && uid <= pBlockList[firstPos].maxUid) {
1,133,895,940✔
246
      return findEarliestIndex(firstPos, uid, pBlockList, num, backward);
1,003,170,683✔
247
    }
248

249
    if (uid > pBlockList[lastPos].maxUid || uid < pBlockList[firstPos].minUid) {
130,733,575✔
250
      return -1;
108,160✔
251
    }
252

253
    int32_t numOfRows = lastPos - firstPos + 1;
130,628,350✔
254
    midPos = (numOfRows >> 1u) + firstPos;
130,628,350✔
255

256
    if (uid < pBlockList[midPos].minUid) {
130,628,350✔
257
      lastPos = midPos - 1;
40,269,785✔
258
    } else if (uid > pBlockList[midPos].maxUid) {
90,351,193✔
259
      firstPos = midPos + 1;
45,575,882✔
260
    } else {
261
      return findEarliestIndex(midPos, uid, pBlockList, num, backward);
44,770,006✔
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;
1,055,514,812✔
269
  int32_t step = backward ? 1 : -1;
1,055,514,812✔
270
  while (i >= 0 && i < num && uid == uidList[i]) {
2,147,483,647✔
271
    i += step;
2,147,483,647✔
272
  }
273
  return i - step;
1,055,564,272✔
274
}
275

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

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

285
  while (1) {
657,593,237✔
286
    if (uid == uidList[firstPos]) {
1,715,132,065✔
287
      return findEarliestRow(firstPos, uid, uidList, num, backward);
607,865,478✔
288
    }
289

290
    if (uid > uidList[lastPos] || uid < uidList[firstPos]) {
1,106,994,760✔
291
      return -1;
1,848,720✔
292
    }
293

294
    int32_t numOfRows = lastPos - firstPos + 1;
1,105,216,193✔
295
    int32_t midPos = (numOfRows >> 1u) + firstPos;
1,105,216,193✔
296

297
    if (uid < uidList[midPos]) {
1,105,216,193✔
298
      lastPos = midPos - 1;
224,322,585✔
299
    } else if (uid > uidList[midPos]) {
880,961,553✔
300
      firstPos = midPos + 1;
433,270,652✔
301
    } else {
302
      return findEarliestRow(midPos, uid, uidList, num, backward);
447,698,794✔
303
    }
304
  }
305
}
306

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

315
  SSttBlk *pStart = &pArray->data[0];
1,219,775,959✔
316
  SSttBlk *pEnd = &pArray->data[TARRAY2_SIZE(pArray) - 1];
1,219,650,921✔
317

318
  // all identical
319
  if (pStart->suid == pEnd->suid) {
1,219,849,749✔
320
    if (pStart->suid != suid) {  // no qualified stt block existed
955,361,996✔
321
      taosArrayClear(pBlockLoadInfo->aSttBlk);
361,132,943✔
322
      pIter->iSttBlk = -1;
361,083,959✔
323
      return TSDB_CODE_SUCCESS;
361,145,800✔
324
    } else {  // all blocks are qualified
325
      taosArrayClear(pBlockLoadInfo->aSttBlk);
593,994,697✔
326
      px = taosArrayAddBatch(pBlockLoadInfo->aSttBlk, pArray->data, pArray->size);
594,178,784✔
327
      if (px == NULL) {
594,292,318!
328
        return terrno;
×
329
      }
330
    }
331
  } else {
332
    SArray *pTmp = taosArrayInit(TARRAY2_SIZE(pArray), sizeof(SSttBlk));
264,433,980✔
333
    if (pTmp == NULL) {
264,507,280!
334
      return terrno;
×
335
    }
336

337
    for (int32_t i = 0; i < TARRAY2_SIZE(pArray); ++i) {
964,847,728✔
338
      SSttBlk *p = &pArray->data[i];
829,083,301✔
339
      if (p->suid < suid) {
829,028,755✔
340
        continue;
492,528,946✔
341
      }
342

343
      if (p->suid == suid) {
336,581,424✔
344
        void *px = taosArrayPush(pTmp, p);
207,811,502✔
345
        if (px == NULL) {
207,811,502!
346
          code = terrno;
×
347
          break;
×
348
        }
349
      } else if (p->suid > suid) {
128,772,487!
350
        break;
128,777,986✔
351
      }
352
    }
353

354
    taosArrayDestroy(pBlockLoadInfo->aSttBlk);
264,484,122✔
355
    pBlockLoadInfo->aSttBlk = pTmp;
264,514,471✔
356
  }
357

358
  return code;
858,660,559✔
359
}
360

361
static int32_t tValueDupPayload(SValue *pVal) {
449,246,872✔
362
  if (IS_VAR_DATA_TYPE(pVal->type) || pVal->type == TSDB_DATA_TYPE_DECIMAL) {
449,246,872!
363
    char *p = (char *)pVal->pData;
183,766,454✔
364
    char *pBuf = taosMemoryMalloc(pVal->nData);
183,765,438!
365
    if (pBuf == NULL) {
183,769,093!
366
      return terrno;
×
367
    }
368

369
    memcpy(pBuf, p, pVal->nData);
183,769,093!
370
    pVal->pData = (uint8_t *)pBuf;
183,769,216✔
371
  }
372

373
  return TSDB_CODE_SUCCESS;
449,270,735✔
374
}
375

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

384
  int32_t numOfBlocks = TARRAY2_SIZE(pStatisBlkArray);
1,222,449,009✔
385
  if (numOfBlocks <= 0) {
1,222,417,276✔
386
    return code;
2,817,923✔
387
  }
388

389
  while ((startIndex < numOfBlocks) && (pStatisBlkArray->data[startIndex].maxTbid.suid < suid)) {
1,430,369,677✔
390
    ++startIndex;
210,770,324✔
391
  }
392

393
  if (startIndex >= numOfBlocks || pStatisBlkArray->data[startIndex].minTbid.suid > suid) {
1,219,739,748✔
394
    return 0;
418,913,698✔
395
  }
396

397
  int32_t endIndex = startIndex;
800,779,323✔
398
  while (endIndex < numOfBlocks && pStatisBlkArray->data[endIndex].minTbid.suid <= suid) {
1,612,337,725✔
399
    ++endIndex;
811,558,402✔
400
  }
401

402
  int32_t num = endIndex - startIndex;
800,742,426✔
403
  pBlockLoadInfo->cost.loadStatisBlocks += num;
800,742,426✔
404

405
  STbStatisBlock block;
800,872,796✔
406
  code = tStatisBlockInit(&block);
800,928,984✔
407
  QUERY_CHECK_CODE(code, lino, _end);
801,068,696!
408

409
  int64_t st = taosGetTimestampUs();
801,001,184✔
410

411
  for (int32_t k = startIndex; k < endIndex; ++k) {
1,612,716,682✔
412
    code = tsdbSttFileReadStatisBlock(pSttFileReader, &pStatisBlkArray->data[k], &block);
811,470,136✔
413
    QUERY_CHECK_CODE(code, lino, _end);
811,734,384!
414

415
    int32_t i = 0;
811,734,384✔
416
    int32_t rows = block.numOfRecords;
811,734,384✔
417
    while (i < rows && ((int64_t *)block.suids.data)[i] != suid) {
1,274,042,433✔
418
      ++i;
462,308,049✔
419
    }
420

421
    // existed
422
    if (i < rows) {
811,782,421✔
423
      SSttTableRowsInfo *pInfo = &pBlockLoadInfo->info;
782,631,957✔
424

425
      if (pInfo->pUid == NULL) {
782,593,567✔
426
        pInfo->pUid = taosArrayInit(rows, sizeof(int64_t));
771,874,215✔
427
        pInfo->pFirstTs = taosArrayInit(rows, sizeof(int64_t));
771,857,602✔
428
        pInfo->pLastTs = taosArrayInit(rows, sizeof(int64_t));
771,904,338✔
429
        pInfo->pCount = taosArrayInit(rows, sizeof(int64_t));
771,871,058✔
430

431
        pInfo->pFirstKey = taosArrayInit(rows, sizeof(SValue));
771,916,137✔
432
        pInfo->pLastKey = taosArrayInit(rows, sizeof(SValue));
771,766,362✔
433

434
        if (pInfo->pUid == NULL || pInfo->pFirstTs == NULL || pInfo->pLastTs == NULL || pInfo->pCount == NULL ||
771,776,159!
435
            pInfo->pFirstKey == NULL || pInfo->pLastKey == NULL) {
771,904,233!
436
          code = terrno;
79,788✔
437
          goto _end;
×
438
        }
439
      }
440

441
      if (pStatisBlkArray->data[k].maxTbid.suid == suid) {
782,574,016✔
442
        int32_t size = rows - i;
729,435,894✔
443
        int32_t offset = i * sizeof(int64_t);
729,435,894✔
444

445
        px = taosArrayAddBatch(pInfo->pUid, tBufferGetDataAt(&block.uids, offset), size);
729,435,894✔
446
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
729,328,286!
447

448
        px = taosArrayAddBatch(pInfo->pFirstTs, tBufferGetDataAt(&block.firstKeyTimestamps, offset), size);
729,328,286✔
449
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
729,421,249!
450

451
        px = taosArrayAddBatch(pInfo->pLastTs, tBufferGetDataAt(&block.lastKeyTimestamps, offset), size);
729,421,249✔
452
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
729,540,887!
453

454
        px = taosArrayAddBatch(pInfo->pCount, tBufferGetDataAt(&block.counts, offset), size);
729,540,887✔
455
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
729,181,387!
456

457
        if (block.numOfPKs > 0) {
729,181,387✔
458
          SValue vFirst = {0}, vLast = {0};
84,436,813✔
459
          for (int32_t f = i; f < rows; ++f) {
267,607,404✔
460
            code = tValueColumnGet(&block.firstKeyPKs[0], f, &vFirst);
183,157,347✔
461
            TSDB_CHECK_CODE(code, lino, _end);
183,143,212!
462

463
            code = tValueDupPayload(&vFirst);
183,143,212✔
464
            TSDB_CHECK_CODE(code, lino, _end);
183,161,189!
465

466
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
183,161,189✔
467
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
183,184,507!
468

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

473
            code = tValueDupPayload(&vLast);
183,145,643✔
474
            TSDB_CHECK_CODE(code, lino, _end);
183,181,449!
475

476
            px = taosArrayPush(pInfo->pLastKey, &vLast);
183,181,449✔
477
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
183,187,758!
478
          }
479
        } else {
480
          SValue vFirst = {0};
644,744,574✔
481
          for (int32_t j = 0; j < size; ++j) {
2,147,483,647✔
482
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
2,147,483,647✔
483
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
2,147,483,647!
484

485
            px = taosArrayPush(pInfo->pLastKey, &vFirst);
2,147,483,647✔
486
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
2,147,483,647!
487
          }
488
        }
489
      } else {
490
        STbStatisRecord record = {0};
53,145,416✔
491
        while (i < rows) {
130,769,102✔
492
          code = tStatisBlockGet(&block, i, &record);
130,773,600✔
493
          TSDB_CHECK_CODE(code, lino, _end);
130,766,914!
494

495
          if (record.suid != suid) {
130,766,914✔
496
            break;
53,153,505✔
497
          }
498

499
          px = taosArrayPush(pInfo->pUid, &record.uid);
77,613,409✔
500
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
77,636,320!
501

502
          px = taosArrayPush(pInfo->pCount, &record.count);
77,636,320✔
503
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
77,651,513!
504

505
          px = taosArrayPush(pInfo->pFirstTs, &record.firstKey.ts);
77,651,513✔
506
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
77,650,079!
507

508
          px = taosArrayPush(pInfo->pLastTs, &record.lastKey.ts);
77,650,079✔
509
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
77,647,168!
510

511
          if (record.firstKey.numOfPKs > 0) {
77,647,168✔
512
            SValue s = record.firstKey.pks[0];
41,467,798✔
513
            code = tValueDupPayload(&s);
41,468,859✔
514
            TSDB_CHECK_CODE(code, lino, _end);
41,469,405!
515

516
            px = taosArrayPush(pInfo->pFirstKey, &s);
41,469,405✔
517
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
41,469,936!
518

519
            s = record.lastKey.pks[0];
41,469,936✔
520
            code = tValueDupPayload(&s);
41,469,936✔
521
            TSDB_CHECK_CODE(code, lino, _end);
41,468,357!
522

523
            px = taosArrayPush(pInfo->pLastKey, &s);
41,468,357✔
524
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
41,469,406!
525
          } else {
526
            SValue v = {0};
36,179,370✔
527
            px = taosArrayPush(pInfo->pFirstKey, &v);
36,167,211✔
528
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
36,176,136!
529

530
            px = taosArrayPush(pInfo->pLastKey, &v);
36,176,136✔
531
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
36,173,777!
532
          }
533

534
          i += 1;
77,623,442✔
535
        }
536
      }
537
    }
538
  }
539

540
_end:
801,381,745✔
541
  tStatisBlockDestroy(&block);
801,029,827✔
542
  if (code != 0) {
800,917,228!
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;
801,046,005✔
546
    pBlockLoadInfo->cost.statisElapsedTime += el;
801,046,005✔
547

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

553
static int32_t doLoadSttFilesBlk(SSttBlockLoadInfo *pBlockLoadInfo, SLDataIter *pIter, int64_t suid,
1,222,236,900✔
554
                                 _load_tomb_fn loadTombFn, void *pReader1, const char *idStr) {
555
  int64_t st = taosGetTimestampUs();
1,222,680,964✔
556

557
  const TSttBlkArray *pSttBlkArray = NULL;
1,222,680,964✔
558
  pBlockLoadInfo->sttBlockLoaded = true;
1,222,439,802✔
559

560
  // load the stt block info for each stt-block
561
  int32_t code = tsdbSttFileReadSttBlk(pIter->pReader, &pSttBlkArray);
1,222,719,180✔
562
  if (code != TSDB_CODE_SUCCESS) {
1,222,473,571!
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);
1,222,473,571✔
569
  if (code != TSDB_CODE_SUCCESS) {
1,222,383,994!
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;
1,222,383,994✔
576
  code = tsdbSttFileReadStatisBlk(pIter->pReader, (const TStatisBlkArray **)&pStatisBlkArray);
1,222,384,366✔
577
  if (code != TSDB_CODE_SUCCESS) {
1,222,587,651!
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);
1,222,587,651✔
584
  if (code != TSDB_CODE_SUCCESS) {
1,222,311,783!
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);
1,222,311,783✔
590

591
  double el = (taosGetTimestampUs() - st) / 1000.0;
1,222,719,684✔
592
  tsdbDebug("load the stt file blk info completed, elapsed time:%.2fms, %s", el, idStr);
1,222,719,684✔
593
  return code;
1,222,655,711✔
594
}
595

596
static int32_t uidComparFn(const void *p1, const void *p2) {
2,147,483,647✔
597
  const uint64_t *pFirst = p1;
2,147,483,647✔
598
  const uint64_t *pVal = p2;
2,147,483,647✔
599

600
  if (*pFirst == *pVal) {
2,147,483,647✔
601
    return 0;
1,046,306,125✔
602
  } else {
603
    return *pFirst < *pVal ? -1 : 1;
1,325,166,155✔
604
  }
605
}
606

607
static void setSttInfoForCurrentTable(SSttBlockLoadInfo *pLoadInfo, uint64_t uid, SSttKeyRange *pRange,
2,147,483,647✔
608
                                      int64_t *numOfRows) {
609
  if (pRange == NULL || taosArrayGetSize(pLoadInfo->info.pUid) == 0) {
2,147,483,647!
610
    return;
820,237,317✔
611
  }
612

613
  int32_t index = taosArraySearchIdx(pLoadInfo->info.pUid, &uid, uidComparFn, TD_EQ);
1,351,171,442✔
614
  if (index >= 0) {
1,351,046,674✔
615
    pRange->skey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pFirstTs, index);
1,046,220,328✔
616
    pRange->ekey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pLastTs, index);
1,046,272,763✔
617

618
    *numOfRows += *(int64_t *)taosArrayGet(pLoadInfo->info.pCount, index);
1,046,348,767✔
619

620
    if (pRange->skey.numOfPKs > 0) {
1,046,262,337✔
621
      memcpy(&pRange->skey.pks[0], taosArrayGet(pLoadInfo->info.pFirstKey, index), sizeof(SValue));
187,167,913!
622
      memcpy(&pRange->ekey.pks[0], taosArrayGet(pLoadInfo->info.pLastKey, index), sizeof(SValue));
187,171,191!
623
    }
624
  }
625
}
626

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

632
  pIter->uid = pConf->uid;
2,147,483,647✔
633
  pIter->cid = cid;
2,147,483,647✔
634
  pIter->backward = backward;
2,147,483,647✔
635
  pIter->verRange.minVer = pConf->verRange.minVer;
2,147,483,647✔
636
  pIter->verRange.maxVer = pConf->verRange.maxVer;
2,147,483,647✔
637
  pIter->timeWindow.skey = pConf->timewindow.skey;
2,147,483,647✔
638
  pIter->timeWindow.ekey = pConf->timewindow.ekey;
2,147,483,647✔
639

640
  pIter->pStartRowKey = pConf->pCurRowKey;
2,147,483,647✔
641
  pIter->pReader = pSttFileReader;
2,147,483,647✔
642
  pIter->pBlockLoadInfo = pBlockLoadInfo;
2,147,483,647✔
643

644
  // open stt file failed, ignore and continue
645
  if (pIter->pReader == NULL) {
2,147,483,647!
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) {
2,147,483,647✔
653
    code = doLoadSttFilesBlk(pBlockLoadInfo, pIter, pConf->suid, pConf->loadTombFn, pConf->pReader, idStr);
1,222,157,964✔
654
    if (code != TSDB_CODE_SUCCESS) {
1,222,665,557!
655
      return code;
×
656
    }
657
  }
658

659
  setSttInfoForCurrentTable(pBlockLoadInfo, pConf->uid, pKeyRange, numOfRows);
2,147,483,647✔
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);
2,147,483,647✔
664
  pIter->iSttBlk = binarySearchForStartBlock(pBlockLoadInfo->aSttBlk->pData, size, pConf->uid, backward);
2,147,483,647✔
665
  if (pIter->iSttBlk != -1) {
2,147,483,647✔
666
    pIter->pSttBlk = taosArrayGet(pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
1,048,066,230✔
667
    pIter->iRow = (pIter->backward) ? pIter->pSttBlk->nRow : -1;
1,048,123,230✔
668

669
    if ((!backward) && ((pConf->strictTimeRange && pIter->pSttBlk->minKey >= pIter->timeWindow.ekey) ||
1,048,133,849!
670
                        (!pConf->strictTimeRange && pIter->pSttBlk->minKey > pIter->timeWindow.ekey))) {
990,214,319✔
671
      pIter->pSttBlk = NULL;
970,134✔
672
    }
673

674
    if (backward && ((pConf->strictTimeRange && pIter->pSttBlk->maxKey <= pIter->timeWindow.skey) ||
1,047,895,967!
675
                     (!pConf->strictTimeRange && pIter->pSttBlk->maxKey < pIter->timeWindow.skey))) {
57,893,467!
676
      pIter->pSttBlk = NULL;
600,891✔
677
      pIter->ignoreEarlierTs = true;
455,772✔
678
    }
679
  }
680

681
  return code;
2,147,483,647✔
682
}
683

684
void tLDataIterClose2(SLDataIter *pIter) {
1,222,702,309✔
685
  tsdbSttFileReaderClose(&pIter->pReader);
1,222,702,309✔
686
  pIter->pReader = NULL;
1,222,654,760✔
687
}
1,222,637,049✔
688

689
void tLDataIterNextBlock(SLDataIter *pIter, const char *idStr) {
1,069,972,283✔
690
  int32_t step = pIter->backward ? -1 : 1;
1,069,972,283✔
691
  int32_t oldIndex = pIter->iSttBlk;
1,070,115,301✔
692

693
  pIter->iSttBlk += step;
1,070,193,305✔
694

695
  int32_t index = -1;
1,070,146,139✔
696
  size_t  size = pIter->pBlockLoadInfo->aSttBlk->size;
1,070,146,139✔
697
  for (int32_t i = pIter->iSttBlk; i < size && i >= 0; i += step) {
1,071,876,735!
698
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, i);
121,211,825✔
699
    if ((!pIter->backward) && p->minUid > pIter->uid) {
121,219,660✔
700
      break;
68,359,189✔
701
    }
702

703
    if (pIter->backward && p->maxUid < pIter->uid) {
52,887,668✔
704
      break;
4,184,464✔
705
    }
706

707
    // check uid firstly
708
    if (p->minUid <= pIter->uid && p->maxUid >= pIter->uid) {
48,681,430!
709
      if ((!pIter->backward) && p->minKey > pIter->timeWindow.ekey) {
48,681,516✔
710
        break;
492,957✔
711
      }
712

713
      if (pIter->backward && p->maxKey < pIter->timeWindow.skey) {
48,187,585✔
714
        break;
1,706✔
715
      }
716

717
      // check time range secondly
718
      if (p->minKey <= pIter->timeWindow.ekey && p->maxKey >= pIter->timeWindow.skey) {
48,185,879✔
719
        if ((!pIter->backward) && p->minVer > pIter->verRange.maxVer) {
46,409,072!
720
          break;
×
721
        }
722

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

727
        if (p->minVer <= pIter->verRange.maxVer && p->maxVer >= pIter->verRange.minVer) {
46,409,072✔
728
          index = i;
46,407,596✔
729
          break;
46,407,596✔
730
        }
731
      }
732
    }
733
  }
734

735
  pIter->pSttBlk = NULL;
1,070,204,921✔
736
  if (index != -1) {
1,069,950,622✔
737
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, index);
46,407,596✔
738

739
    pIter->iSttBlk = index;
46,408,154✔
740
    pIter->pSttBlk = (SSttBlk *)taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
46,408,240✔
741
    tsdbDebug("try next stt-file block:%d from %d, trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
46,408,098✔
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);
1,023,543,026✔
746
  }
747
}
1,069,954,338✔
748

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

755
  int32_t code = loadLastBlock(pIter, idStr, &pData);
2,147,483,647✔
756
  if (code) {
2,147,483,647!
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) {
2,147,483,647✔
763
    i = binarySearchForStartRowIndex((uint64_t *)pData->aUid, pData->nRow, pIter->uid, pIter->backward);
1,057,513,036✔
764
    if (i == -1) {
1,057,144,765✔
765
      tsdbDebug("failed to find the data in pBlockData, uid:%" PRIu64 " , %s", pIter->uid, idStr);
1,848,720!
766
      pIter->iRow = -1;
1,848,720✔
767
      return code;
1,848,720✔
768
    }
769
  }
770

771
  for (; i < pData->nRow && i >= 0; i += step) {
2,147,483,647✔
772
    if (pData->aUid != NULL) {
2,147,483,647✔
773
      if (!pIter->backward) {
2,147,483,647✔
774
        if (pData->aUid[i] > pIter->uid) {
2,147,483,647✔
775
          break;
421,777,398✔
776
        }
777
      } else {
778
        if (pData->aUid[i] < pIter->uid) {
2,147,483,647✔
779
          break;
37,808,582✔
780
        }
781
      }
782
    }
783

784
    int64_t ts = pData->aTSKEY[i];
2,147,483,647✔
785
    if (!pIter->backward) {               // asc
2,147,483,647✔
786
      if (ts > pIter->timeWindow.ekey) {  // no more data
2,147,483,647✔
787
        break;
4,578,271✔
788
      } else {
789
        if (ts < pIter->timeWindow.skey) {
2,147,483,647✔
790
          continue;
2,147,483,647✔
791
        }
792

793
        if (ts == pIter->timeWindow.skey && pIter->pStartRowKey->numOfPKs > 0) {
2,147,483,647✔
794
          SRowKey key;
382,788✔
795
          tColRowGetKey(pData, i, &key);
382,788!
796
          int32_t ret = pkCompEx(&key, pIter->pStartRowKey);
382,788✔
797
          if (ret < 0) {
382,788!
798
            continue;
×
799
          }
800
        }
801
      }
802
    } else {
803
      if (ts < pIter->timeWindow.skey) {
2,147,483,647✔
804
        break;
2,838,916✔
805
      } else {
806
        if (ts > pIter->timeWindow.ekey) {
2,147,483,647✔
807
          continue;
2,147,483,647✔
808
        }
809

810
        if (ts == pIter->timeWindow.ekey && pIter->pStartRowKey->numOfPKs > 0) {
2,147,483,647!
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];
2,147,483,647✔
822
    if (ver < pIter->verRange.minVer) {
2,147,483,647!
823
      continue;
×
824
    }
825

826
    // todo opt handle desc case
827
    if (ver > pIter->verRange.maxVer) {
2,147,483,647✔
828
      continue;
219,762✔
829
    }
830

831
    hasVal = true;
2,147,483,647✔
832
    break;
2,147,483,647✔
833
  }
834

835
  pIter->iRow = (hasVal) ? i : -1;
2,147,483,647✔
836
  return code;
2,147,483,647✔
837
}
838

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

846
  *hasNext = false;
2,147,483,647✔
847

848
  // no qualified last file block in current file, no need to fetch row
849
  if (pIter->pSttBlk == NULL) {
2,147,483,647✔
850
    return code;
1,124,773,929✔
851
  }
852

853
  code = loadLastBlock(pIter, idStr, &pBlockData);
2,147,483,647✔
854
  if (pBlockData == NULL || code != TSDB_CODE_SUCCESS) {
2,147,483,647✔
855
    lino = __LINE__;
260,502✔
856
    goto _exit;
260,502✔
857
  }
858

859
  pIter->iRow += step;
2,147,483,647✔
860

861
  while (1) {
46,408,470✔
862
    bool skipBlock = false;
2,147,483,647✔
863
    code = findNextValidRow(pIter, idStr);
2,147,483,647✔
864
    TSDB_CHECK_CODE(code, lino, _exit);
2,147,483,647!
865

866
    if (pIter->pBlockLoadInfo->checkRemainingRow) {
2,147,483,647!
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) {
2,147,483,647!
890
      tLDataIterNextBlock(pIter, idStr);
1,242,415,646✔
891
      if (pIter->pSttBlk == NULL) {  // no more data
1,069,921,184✔
892
        goto _exit;
1,023,719,391✔
893
      }
894
    } else {
895
      break;
896
    }
897

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

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

910
  pIter->rInfo.suid = pBlockData->suid;
2,147,483,647✔
911
  pIter->rInfo.uid = pBlockData->uid;
2,147,483,647✔
912
  pIter->rInfo.row = tsdbRowFromBlockData(pBlockData, pIter->iRow);
2,147,483,647✔
913

914
_exit:
2,147,483,647✔
915
  if (code) {
2,147,483,647!
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);
2,147,483,647✔
920
  return code;
2,147,483,647✔
921
}
922

923
// SMergeTree =================================================
924
static FORCE_INLINE int32_t tLDataIterCmprFn(const SRBTreeNode *p1, const SRBTreeNode *p2) {
2,147,483,647✔
925
  SLDataIter *pIter1 = (SLDataIter *)(((uint8_t *)p1) - offsetof(SLDataIter, node));
2,147,483,647✔
926
  SLDataIter *pIter2 = (SLDataIter *)(((uint8_t *)p2) - offsetof(SLDataIter, node));
2,147,483,647✔
927

928
  SRowKey rkey1 = {0}, rkey2 = {0};
2,147,483,647✔
929
  tRowGetKeyEx(&pIter1->rInfo.row, &rkey1);
2,147,483,647!
930
  tRowGetKeyEx(&pIter2->rInfo.row, &rkey2);
2,147,483,647!
931

932
  int32_t ret = tRowKeyCompare(&rkey1, &rkey2);
2,147,483,647✔
933
  if (ret < 0) {
2,147,483,647✔
934
    return -1;
2,147,483,647✔
935
  } else if (ret > 0) {
2,147,483,647✔
936
    return 1;
2,147,483,647✔
937
  } else {
938
    int64_t ver1 = TSDBROW_VERSION(&pIter1->rInfo.row);
2,147,483,647!
939
    int64_t ver2 = TSDBROW_VERSION(&pIter2->rInfo.row);
2,147,483,647!
940

941
    if (ver1 < ver2) {
2,147,483,647✔
942
      return -1;
2,147,483,647✔
943
    } else if (ver1 > ver2) {
2,147,483,647!
944
      return 1;
2,147,483,647✔
945
    } else {
946
      return 0;
×
947
    }
948
  }
949
}
950

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

955
int32_t tMergeTreeOpen2(SMergeTree *pMTree, SMergeTreeConf *pConf, SSttDataInfoForTable *pSttDataInfo) {
2,147,483,647✔
956
  int32_t code = TSDB_CODE_SUCCESS;
2,147,483,647✔
957

958
  pMTree->pIter = NULL;
2,147,483,647✔
959
  pMTree->backward = pConf->backward;
2,147,483,647✔
960
  pMTree->idStr = pConf->idstr;
2,147,483,647✔
961
  int32_t lino = 0;
2,147,483,647✔
962

963
  if (!pMTree->backward) {  // asc
2,147,483,647✔
964
    tRBTreeCreate(&pMTree->rbt, tLDataIterCmprFn);
2,147,483,647✔
965
  } else {  // desc
966
    tRBTreeCreate(&pMTree->rbt, tLDataIterDescCmprFn);
134,506,856✔
967
  }
968

969
  pMTree->ignoreEarlierTs = false;
2,147,483,647✔
970

971
  // no data exists, go to end
972
  int32_t numOfLevels = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->size;
2,147,483,647✔
973
  if (numOfLevels == 0) {
2,147,483,647✔
974
    goto _end;
522,742,662✔
975
  }
976

977
  code = adjustSttDataIters(pConf->pSttFileBlockIterArray, pConf->pCurrentFileset);
2,006,342,902✔
978
  if (code) {
2,005,813,318!
979
    goto _end;
×
980
  }
981

982
  for (int32_t j = 0; j < numOfLevels; ++j) {
2,147,483,647✔
983
    SSttLvl *pSttLevel = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->data[j];
2,147,483,647✔
984
    SArray  *pList = taosArrayGetP(pConf->pSttFileBlockIterArray, j);
2,147,483,647✔
985

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

989
      SSttFileReader    *pSttFileReader = pIter->pReader;
2,147,483,647✔
990
      SSttBlockLoadInfo *pLoadInfo = pIter->pBlockLoadInfo;
2,147,483,647✔
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) {
2,147,483,647✔
995
        SSttFileReaderConfig conf = {.tsdb = pConf->pTsdb, .szPage = pConf->pTsdb->pVnode->config.tsdbPageSize};
1,222,768,031✔
996
        conf.file[0] = *pSttLevel->fobjArr->data[i]->f;
1,222,863,429✔
997

998
        code = tsdbSttFileReaderOpen(pSttLevel->fobjArr->data[i]->fname, &conf, &pSttFileReader);
1,222,921,878✔
999
        if (code != TSDB_CODE_SUCCESS) {
1,222,118,631!
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) {
2,147,483,647✔
1006
        code = tCreateSttBlockLoadInfo(pConf->pSchema, pConf->pCols, pConf->numOfCols, &pLoadInfo);
1,222,648,587✔
1007
        if (code != TSDB_CODE_SUCCESS) {
1,222,608,504!
1008
          goto _end;
×
1009
        }
1010
      }
1011

1012
      memset(pIter, 0, sizeof(SLDataIter));
2,147,483,647!
1013

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

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

1024
      bool hasVal = NULL;
2,147,483,647✔
1025
      code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
2,147,483,647✔
1026
      if (code) {
2,147,483,647!
1027
        goto _end;
×
1028
      }
1029

1030
      if (hasVal) {
2,147,483,647✔
1031
        tMergeTreeAddIter(pMTree, pIter);
1,037,950,521✔
1032

1033
        // let's record the time window for current table of uid in the stt files
1034
        if (pSttDataInfo != NULL && numOfRows > 0) {
1,037,900,908!
1035
          void *px = taosArrayPush(pSttDataInfo->pKeyRangeList, &range);
1,037,996,826✔
1036
          QUERY_CHECK_NULL(px, code, lino, _end, terrno);
1,038,041,602!
1037

1038
          pSttDataInfo->numOfRows += numOfRows;
1,038,041,602✔
1039
        }
1040
      } else {
1041
        if (!pMTree->ignoreEarlierTs) {
1,132,929,983✔
1042
          pMTree->ignoreEarlierTs = pIter->ignoreEarlierTs;
1,133,069,176!
1043
        }
1044
      }
1045
    }
1046
  }
1047

1048
  return code;
2,006,354,160✔
1049

1050
_end:
522,642,971✔
1051
  tMergeTreeClose(pMTree);
522,642,971✔
1052
  return code;
522,663,485✔
1053
}
1054

1055
void tMergeTreeAddIter(SMergeTree *pMTree, SLDataIter *pIter) {
1,037,881,560✔
1056
  SRBTreeNode *node = tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pIter);
1,037,881,560✔
1057
}
1,037,933,155✔
1058

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

1061
static void tLDataIterPinSttBlock(SLDataIter *pIter, const char *id) {
2,147,483,647✔
1062
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
2,147,483,647✔
1063

1064
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
2,147,483,647✔
1065
    pInfo->blockData[0].pin = true;
2,147,483,647✔
1066
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
2,147,483,647✔
1067
    return;
2,147,483,647✔
1068
  }
1069

1070
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
2,147,483,647!
1071
    pInfo->blockData[1].pin = true;
2,147,483,647✔
1072
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
2,147,483,647✔
1073
    return;
2,147,483,647✔
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) {
2,147,483,647✔
1080
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
2,147,483,647✔
1081
  if (pInfo->blockData[0].pin) {
2,147,483,647✔
1082
    pInfo->blockData[0].pin = false;
2,147,483,647✔
1083
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[0].sttBlockIndex, pIter->cid, id);
2,147,483,647✔
1084
    return;
2,147,483,647✔
1085
  }
1086

1087
  if (pInfo->blockData[1].pin) {
2,147,483,647!
1088
    pInfo->blockData[1].pin = false;
2,147,483,647✔
1089
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[1].sttBlockIndex, pIter->cid, id);
2,147,483,647✔
1090
    return;
2,147,483,647✔
1091
  }
1092

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

1096
void tMergeTreePinSttBlock(SMergeTree *pMTree) {
2,147,483,647✔
1097
  if (pMTree->pIter == NULL) {
2,147,483,647!
1098
    return;
×
1099
  }
1100

1101
  SLDataIter *pIter = pMTree->pIter;
2,147,483,647✔
1102
  pMTree->pPinnedBlockIter = pIter;
2,147,483,647✔
1103
  tLDataIterPinSttBlock(pIter, pMTree->idStr);
2,147,483,647✔
1104
}
1105

1106
void tMergeTreeUnpinSttBlock(SMergeTree *pMTree) {
2,147,483,647✔
1107
  if (pMTree->pPinnedBlockIter == NULL) {
2,147,483,647!
1108
    return;
×
1109
  }
1110

1111
  SLDataIter *pIter = pMTree->pPinnedBlockIter;
2,147,483,647✔
1112
  pMTree->pPinnedBlockIter = NULL;
2,147,483,647✔
1113
  tLDataIterUnpinSttBlock(pIter, pMTree->idStr);
2,147,483,647✔
1114
}
1115

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

1122
  *pHasNext = false;
2,147,483,647✔
1123
  while (pMTree->pIter) {
2,147,483,647✔
1124
    SLDataIter *pIter = pMTree->pIter;
2,147,483,647✔
1125
    bool        hasVal = false;
2,147,483,647✔
1126
    code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
2,147,483,647✔
1127
    if (!hasVal || (code != 0)) {
2,147,483,647!
1128
      if (code == TSDB_CODE_FILE_CORRUPTED) {
911,974,130!
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;
911,974,130✔
1133
    }
1134

1135
    // compare with min in RB Tree
1136
    pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
2,147,483,647✔
1137
    if (pMTree->pIter && pIter) {
2,147,483,647✔
1138
      int32_t c = pMTree->rbt.cmprFn(&pMTree->pIter->node, &pIter->node);
2,147,483,647✔
1139
      if (c > 0) {
2,147,483,647✔
1140
        (void)tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
2,147,483,647✔
1141
        pMTree->pIter = NULL;
2,147,483,647✔
1142
      } else if (!c) {
2,147,483,647!
1143
        continue;
×
1144
      }
1145
    }
1146

1147
    break;
2,147,483,647✔
1148
  }
1149

1150
  if (pMTree->pIter == NULL) {
2,147,483,647✔
1151
    pMTree->pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
2,147,483,647✔
1152
    if (pMTree->pIter) {
2,147,483,647✔
1153
      tRBTreeDrop(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
2,147,483,647✔
1154
    }
1155
  }
1156

1157
  *pHasNext = (pMTree->pIter != NULL);
2,147,483,647✔
1158
  return code;
2,147,483,647✔
1159
}
1160

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