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

taosdata / TDengine / #3592

24 Jan 2025 08:57AM UTC coverage: 63.546% (-0.1%) from 63.688%
#3592

push

travis-ci

web-flow
Merge pull request #29638 from taosdata/docs/TS-5846-3.0

enh: TDengine modify taosBenchmark new query rule cases and add doc

141205 of 285630 branches covered (49.44%)

Branch coverage included in aggregate %.

220039 of 282844 relevant lines covered (77.8%)

18746126.97 hits per line

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

78.47
/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) {
5,317,966✔
27
  *pInfo = NULL;
5,317,966✔
28

29
  SSttBlockLoadInfo *pLoadInfo = taosMemoryCalloc(1, sizeof(SSttBlockLoadInfo));
5,317,966!
30
  if (pLoadInfo == NULL) {
5,322,582!
31
    return terrno;
×
32
  }
33

34
  pLoadInfo->blockData[0].sttBlockIndex = -1;
5,322,582✔
35
  pLoadInfo->blockData[1].sttBlockIndex = -1;
5,322,582✔
36

37
  pLoadInfo->currentLoadBlockIndex = 1;
5,322,582✔
38

39
  int32_t code = tBlockDataCreate(&pLoadInfo->blockData[0].data);
5,322,582✔
40
  if (code) {
5,323,784!
41
    taosMemoryFreeClear(pLoadInfo);
×
42
    return code;
×
43
  }
44

45
  code = tBlockDataCreate(&pLoadInfo->blockData[1].data);
5,323,784✔
46
  if (code) {
5,324,409!
47
    taosMemoryFreeClear(pLoadInfo);
×
48
    return code;
×
49
  }
50

51
  pLoadInfo->aSttBlk = taosArrayInit(4, sizeof(SSttBlk));
5,324,409✔
52
  if (pLoadInfo->aSttBlk == NULL) {
5,323,966✔
53
    taosMemoryFreeClear(pLoadInfo);
827!
54
    return terrno;
827✔
55
  }
56

57
  pLoadInfo->pSchema = pSchema;
5,323,139✔
58
  pLoadInfo->colIds = colList;
5,323,139✔
59
  pLoadInfo->numOfCols = numOfCols;
5,323,139✔
60

61
  *pInfo = pLoadInfo;
5,323,139✔
62
  return code;
5,323,139✔
63
}
64

65
static void freeItem(void *pValue) {
35,453,083✔
66
  SValue *p = (SValue *)pValue;
35,453,083✔
67
  if (IS_VAR_DATA_TYPE(p->type)) {
35,453,083!
68
    taosMemoryFree(p->pData);
×
69
  }
70
}
35,460,086✔
71

72
void destroySttBlockLoadInfo(SSttBlockLoadInfo *pLoadInfo) {
5,326,580✔
73
  if (pLoadInfo == NULL) {
5,326,580!
74
    return;
×
75
  }
76

77
  pLoadInfo->currentLoadBlockIndex = 1;
5,326,580✔
78

79
  SBlockDataInfo *pInfo = &pLoadInfo->blockData[0];
5,326,580✔
80
  tBlockDataDestroy(&pInfo->data);
5,326,580✔
81
  pInfo->sttBlockIndex = -1;
5,326,602✔
82
  pInfo->pin = false;
5,326,602✔
83

84
  pInfo = &pLoadInfo->blockData[1];
5,326,602✔
85
  tBlockDataDestroy(&pInfo->data);
5,326,602✔
86
  pInfo->sttBlockIndex = -1;
5,326,565✔
87
  pInfo->pin = false;
5,326,565✔
88

89
  taosArrayDestroy(pLoadInfo->info.pUid);
5,326,565✔
90
  taosArrayDestroyEx(pLoadInfo->info.pFirstKey, freeItem);
5,326,461✔
91
  taosArrayDestroyEx(pLoadInfo->info.pLastKey, freeItem);
5,326,453✔
92
  taosArrayDestroy(pLoadInfo->info.pCount);
5,326,555✔
93
  taosArrayDestroy(pLoadInfo->info.pFirstTs);
5,326,513✔
94
  taosArrayDestroy(pLoadInfo->info.pLastTs);
5,326,505✔
95

96
  pLoadInfo->info.pUid = NULL;
5,326,564✔
97
  pLoadInfo->info.pFirstKey = NULL;
5,326,564✔
98
  pLoadInfo->info.pLastKey = NULL;
5,326,564✔
99
  pLoadInfo->info.pCount = NULL;
5,326,564✔
100
  pLoadInfo->info.pFirstTs = NULL;
5,326,564✔
101
  pLoadInfo->info.pLastTs = NULL;
5,326,564✔
102

103
  taosArrayDestroy(pLoadInfo->aSttBlk);
5,326,564✔
104
  taosMemoryFree(pLoadInfo);
5,326,638!
105
}
106

107
void destroyLDataIter(SLDataIter *pIter) {
5,325,199✔
108
  tLDataIterClose2(pIter);
5,325,199✔
109
  destroySttBlockLoadInfo(pIter->pBlockLoadInfo);
5,326,743✔
110
  taosMemoryFree(pIter);
5,326,599!
111
}
5,326,787✔
112

113
void destroySttBlockReader(SArray *pLDataIterArray, SSttBlockLoadCostInfo *pLoadCost) {
10,896,006✔
114
  if (pLDataIterArray == NULL) {
10,896,006✔
115
    return;
59,227✔
116
  }
117

118
  int32_t numOfLevel = taosArrayGetSize(pLDataIterArray);
10,836,779✔
119
  for (int32_t i = 0; i < numOfLevel; ++i) {
16,157,840✔
120
    SArray *pList = taosArrayGetP(pLDataIterArray, i);
5,319,982✔
121
    for (int32_t j = 0; j < taosArrayGetSize(pList); ++j) {
10,646,656✔
122
      SLDataIter *pIter = taosArrayGetP(pList, j);
5,325,502✔
123
      if (pIter->pBlockLoadInfo != NULL) {
5,324,992!
124
        SSttBlockLoadCostInfo *pCost = &pIter->pBlockLoadInfo->cost;
5,325,107✔
125
        if (pLoadCost != NULL) {
5,325,107!
126
          pLoadCost->loadBlocks += pCost->loadBlocks;
5,325,175✔
127
          pLoadCost->loadStatisBlocks += pCost->loadStatisBlocks;
5,325,175✔
128
          pLoadCost->blockElapsedTime += pCost->blockElapsedTime;
5,325,175✔
129
          pLoadCost->statisElapsedTime += pCost->statisElapsedTime;
5,325,175✔
130
        }
131
      }
132

133
      destroyLDataIter(pIter);
5,324,992✔
134
    }
135

136
    taosArrayDestroy(pList);
5,319,499✔
137
  }
138

139
  taosArrayDestroy(pLDataIterArray);
10,837,858✔
140
}
141

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

149
  pLoadInfo->currentLoadBlockIndex = nextSlotIndex;
3,999,111✔
150
}
3,999,111✔
151

152
static int32_t loadLastBlock(SLDataIter *pIter, const char *idStr, SBlockData **pResBlock) {
926,150,358✔
153
  if (pResBlock != NULL) {
926,150,358!
154
    *pResBlock = NULL;
926,544,223✔
155
  }
156

157
  int32_t            code = 0;
926,150,358✔
158
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
926,150,358✔
159

160
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
926,150,358✔
161
    if (pInfo->currentLoadBlockIndex != 0) {
636,501,523✔
162
      tsdbDebug("current load index is set to 0, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
748✔
163
                ", load data, %s",
164
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
165
      pInfo->currentLoadBlockIndex = 0;
748✔
166
    }
167

168
    *pResBlock = &pInfo->blockData[0].data;
636,501,523✔
169
    return code;
636,501,523✔
170
  }
171

172
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
289,648,835!
173
    if (pInfo->currentLoadBlockIndex != 1) {
306,323,790✔
174
      tsdbDebug("current load index is set to 1, block index:%d, fileVer:%" PRId64 ", due to uid:%" PRIu64
740✔
175
                ", load data, %s",
176
                pIter->iSttBlk, pIter->cid, pIter->uid, idStr);
177
      pInfo->currentLoadBlockIndex = 1;
740✔
178
    }
179

180
    *pResBlock = &pInfo->blockData[1].data;
306,323,790✔
181
    return code;
306,323,790✔
182
  }
183

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

188
  updateBlockLoadSlot(pInfo);
3,999,714✔
189
  int64_t st = taosGetTimestampUs();
4,001,570✔
190

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

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

202
  tsdbDebug("read stt block, total load:%" PRId64 ", trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
4,003,281✔
203
            ", last block index:%d, entry:%d, rows:%d, uidRange:%" PRId64 "-%" PRId64 " tsRange:%" PRId64 "-%" PRId64
204
            " %p, elapsed time:%.2f ms, %s",
205
            pInfo->cost.loadBlocks, pIter->uid, pIter->cid, pIter->iSttBlk, pInfo->currentLoadBlockIndex, pBlock->nRow,
206
            pIter->pSttBlk->minUid, pIter->pSttBlk->maxUid, pIter->pSttBlk->minKey, pIter->pSttBlk->maxKey, pBlock, el,
207
            idStr);
208

209
  pInfo->blockData[pInfo->currentLoadBlockIndex].sttBlockIndex = pIter->iSttBlk;
4,003,195✔
210
  pIter->iRow = (pIter->backward) ? pInfo->blockData[pInfo->currentLoadBlockIndex].data.nRow : -1;
4,003,195✔
211

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

215
  *pResBlock = &pInfo->blockData[pInfo->currentLoadBlockIndex].data;
4,003,103✔
216
  return code;
4,003,103✔
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;
4,867,561✔
223
  int32_t step = backward ? 1 : -1;
4,867,561✔
224
  while (i >= 0 && i < num && uid >= pBlockList[i].minUid && uid <= pBlockList[i].maxUid) {
9,782,115!
225
    i += step;
4,914,554✔
226
  }
227
  return i - step;
4,867,561✔
228
}
229

230
static int32_t binarySearchForStartBlock(SSttBlk *pBlockList, int32_t num, uint64_t uid, int32_t backward) {
11,687,834✔
231
  int32_t midPos = -1;
11,687,834✔
232
  if (num <= 0) {
11,687,834✔
233
    return -1;
1,991,322✔
234
  }
235

236
  int32_t firstPos = 0;
9,696,512✔
237
  int32_t lastPos = num - 1;
9,696,512✔
238

239
  // find the first position which is bigger than the key
240
  if ((uid > pBlockList[lastPos].maxUid) || (uid < pBlockList[firstPos].minUid)) {
9,696,512✔
241
    return -1;
4,828,671✔
242
  }
243

244
  while (1) {
554,034✔
245
    if (uid >= pBlockList[firstPos].minUid && uid <= pBlockList[firstPos].maxUid) {
5,421,875✔
246
      return findEarliestIndex(firstPos, uid, pBlockList, num, backward);
4,731,206✔
247
    }
248

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

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

256
    if (uid < pBlockList[midPos].minUid) {
690,389✔
257
      lastPos = midPos - 1;
282,010✔
258
    } else if (uid > pBlockList[midPos].maxUid) {
408,379✔
259
      firstPos = midPos + 1;
272,024✔
260
    } else {
261
      return findEarliestIndex(midPos, uid, pBlockList, num, backward);
136,355✔
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;
4,810,357✔
269
  int32_t step = backward ? 1 : -1;
4,810,357✔
270
  while (i >= 0 && i < num && uid == uidList[i]) {
102,533,875!
271
    i += step;
97,723,518✔
272
  }
273
  return i - step;
4,810,357✔
274
}
275

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

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

285
  while (1) {
1,443,507✔
286
    if (uid == uidList[firstPos]) {
6,336,722✔
287
      return findEarliestRow(firstPos, uid, uidList, num, backward);
3,894,155✔
288
    }
289

290
    if (uid > uidList[lastPos] || uid < uidList[firstPos]) {
2,442,567✔
291
      return -1;
82,858✔
292
    }
293

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

297
    if (uid < uidList[midPos]) {
2,359,709✔
298
      lastPos = midPos - 1;
625,198✔
299
    } else if (uid > uidList[midPos]) {
1,734,511✔
300
      firstPos = midPos + 1;
818,309✔
301
    } else {
302
      return findEarliestRow(midPos, uid, uidList, num, backward);
916,202✔
303
    }
304
  }
305
}
306

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

315
  SSttBlk *pStart = &pArray->data[0];
5,303,139✔
316
  SSttBlk *pEnd = &pArray->data[TARRAY2_SIZE(pArray) - 1];
5,303,139✔
317

318
  // all identical
319
  if (pStart->suid == pEnd->suid) {
5,303,139✔
320
    if (pStart->suid != suid) {  // no qualified stt block existed
2,345,628✔
321
      taosArrayClear(pBlockLoadInfo->aSttBlk);
847,735✔
322
      pIter->iSttBlk = -1;
847,693✔
323
      return TSDB_CODE_SUCCESS;
847,693✔
324
    } else {  // all blocks are qualified
325
      taosArrayClear(pBlockLoadInfo->aSttBlk);
1,497,893✔
326
      px = taosArrayAddBatch(pBlockLoadInfo->aSttBlk, pArray->data, pArray->size);
1,497,794✔
327
      if (px == NULL) {
1,497,347!
328
        return terrno;
×
329
      }
330
    }
331
  } else {
332
    SArray *pTmp = taosArrayInit(TARRAY2_SIZE(pArray), sizeof(SSttBlk));
2,957,511✔
333
    if (pTmp == NULL) {
2,962,564!
334
      return terrno;
×
335
    }
336

337
    for (int32_t i = 0; i < TARRAY2_SIZE(pArray); ++i) {
10,618,119✔
338
      SSttBlk *p = &pArray->data[i];
8,636,153✔
339
      if (p->suid < suid) {
8,636,153✔
340
        continue;
4,906,114✔
341
      }
342

343
      if (p->suid == suid) {
3,730,039✔
344
        void *px = taosArrayPush(pTmp, p);
2,749,715✔
345
        if (px == NULL) {
2,749,715!
346
          code = terrno;
×
347
          break;
×
348
        }
349
      } else if (p->suid > suid) {
979,841!
350
        break;
980,194✔
351
      }
352
    }
353

354
    taosArrayDestroy(pBlockLoadInfo->aSttBlk);
2,962,160✔
355
    pBlockLoadInfo->aSttBlk = pTmp;
2,963,235✔
356
  }
357

358
  return code;
4,460,582✔
359
}
360

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

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

373
  return TSDB_CODE_SUCCESS;
2,048,104✔
374
}
375

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

384
  int32_t numOfBlocks = TARRAY2_SIZE(pStatisBlkArray);
5,315,103✔
385
  if (numOfBlocks <= 0) {
5,315,103✔
386
    return code;
10,158✔
387
  }
388

389
  while ((startIndex < numOfBlocks) && (pStatisBlkArray->data[startIndex].maxTbid.suid < suid)) {
5,717,108✔
390
    ++startIndex;
412,163✔
391
  }
392

393
  if (startIndex >= numOfBlocks || pStatisBlkArray->data[startIndex].minTbid.suid > suid) {
5,304,945✔
394
    return 0;
922,244✔
395
  }
396

397
  int32_t endIndex = startIndex;
4,382,701✔
398
  while (endIndex < numOfBlocks && pStatisBlkArray->data[endIndex].minTbid.suid <= suid) {
8,768,265✔
399
    ++endIndex;
4,385,564✔
400
  }
401

402
  int32_t num = endIndex - startIndex;
4,382,701✔
403
  pBlockLoadInfo->cost.loadStatisBlocks += num;
4,382,701✔
404

405
  STbStatisBlock block;
406
  code = tStatisBlockInit(&block);
4,382,701✔
407
  QUERY_CHECK_CODE(code, lino, _end);
4,382,403!
408

409
  int64_t st = taosGetTimestampUs();
4,382,288✔
410

411
  for (int32_t k = startIndex; k < endIndex; ++k) {
8,755,060✔
412
    code = tsdbSttFileReadStatisBlock(pSttFileReader, &pStatisBlkArray->data[k], &block);
4,381,687✔
413
    QUERY_CHECK_CODE(code, lino, _end);
4,385,581!
414

415
    int32_t i = 0;
4,385,581✔
416
    int32_t rows = block.numOfRecords;
4,385,581✔
417
    while (i < rows && ((int64_t *)block.suids.data)[i] != suid) {
9,396,651✔
418
      ++i;
5,011,070✔
419
    }
420

421
    // existed
422
    if (i < rows) {
4,385,581✔
423
      SSttTableRowsInfo *pInfo = &pBlockLoadInfo->info;
4,205,191✔
424

425
      if (pInfo->pUid == NULL) {
4,205,191✔
426
        pInfo->pUid = taosArrayInit(rows, sizeof(int64_t));
4,202,388✔
427
        pInfo->pFirstTs = taosArrayInit(rows, sizeof(int64_t));
4,202,480✔
428
        pInfo->pLastTs = taosArrayInit(rows, sizeof(int64_t));
4,202,773✔
429
        pInfo->pCount = taosArrayInit(rows, sizeof(int64_t));
4,202,781✔
430

431
        pInfo->pFirstKey = taosArrayInit(rows, sizeof(SValue));
4,202,476✔
432
        pInfo->pLastKey = taosArrayInit(rows, sizeof(SValue));
4,202,686✔
433

434
        if (pInfo->pUid == NULL || pInfo->pFirstTs == NULL || pInfo->pLastTs == NULL || pInfo->pCount == NULL ||
4,202,672!
435
            pInfo->pFirstKey == NULL || pInfo->pLastKey == NULL) {
4,202,734✔
436
          code = terrno;
315✔
437
          goto _end;
×
438
        }
439
      }
440

441
      if (pStatisBlkArray->data[k].maxTbid.suid == suid) {
4,205,160✔
442
        int32_t size = rows - i;
3,408,915✔
443
        int32_t offset = i * sizeof(int64_t);
3,408,915✔
444

445
        px = taosArrayAddBatch(pInfo->pUid, tBufferGetDataAt(&block.uids, offset), size);
3,408,915✔
446
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
3,408,260!
447

448
        px = taosArrayAddBatch(pInfo->pFirstTs, tBufferGetDataAt(&block.firstKeyTimestamps, offset), size);
3,408,260✔
449
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
3,408,163!
450

451
        px = taosArrayAddBatch(pInfo->pLastTs, tBufferGetDataAt(&block.lastKeyTimestamps, offset), size);
3,408,163✔
452
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
3,407,388!
453

454
        px = taosArrayAddBatch(pInfo->pCount, tBufferGetDataAt(&block.counts, offset), size);
3,407,388✔
455
        TSDB_CHECK_NULL(px, code, lino, _end, terrno);
3,407,775✔
456

457
        if (block.numOfPKs > 0) {
3,406,984✔
458
          SValue vFirst = {0}, vLast = {0};
14,479✔
459
          for (int32_t f = i; f < rows; ++f) {
1,038,614✔
460
            code = tValueColumnGet(&block.firstKeyPKs[0], f, &vFirst);
1,024,131✔
461
            TSDB_CHECK_CODE(code, lino, _end);
1,024,164!
462

463
            code = tValueDupPayload(&vFirst);
1,024,164✔
464
            TSDB_CHECK_CODE(code, lino, _end);
1,024,189!
465

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

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

473
            code = tValueDupPayload(&vLast);
1,024,150✔
474
            TSDB_CHECK_CODE(code, lino, _end);
1,024,165!
475

476
            px = taosArrayPush(pInfo->pLastKey, &vLast);
1,024,165✔
477
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
1,024,135!
478
          }
479
        } else {
480
          SValue vFirst = {0};
3,392,505✔
481
          for (int32_t j = 0; j < size; ++j) {
18,791,526✔
482
            px = taosArrayPush(pInfo->pFirstKey, &vFirst);
15,409,893✔
483
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
15,398,067!
484

485
            px = taosArrayPush(pInfo->pLastKey, &vFirst);
15,398,067✔
486
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
15,399,021!
487
          }
488
        }
489
      } else {
490
        STbStatisRecord record = {0};
796,245✔
491
        while (i < rows) {
1,628,526✔
492
          code = tStatisBlockGet(&block, i, &record);
1,628,513✔
493
          TSDB_CHECK_CODE(code, lino, _end);
1,628,468!
494

495
          if (record.suid != suid) {
1,628,468✔
496
            break;
796,253✔
497
          }
498

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

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

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

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

511
          if (record.firstKey.numOfPKs > 0) {
832,245!
512
            SValue s = record.firstKey.pks[0];
×
513
            code = tValueDupPayload(&s);
×
514
            TSDB_CHECK_CODE(code, lino, _end);
×
515

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

519
            s = record.lastKey.pks[0];
×
520
            code = tValueDupPayload(&s);
×
521
            TSDB_CHECK_CODE(code, lino, _end);
×
522

523
            px = taosArrayPush(pInfo->pLastKey, &s);
×
524
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
×
525
          } else {
526
            SValue v = {0};
832,245✔
527
            px = taosArrayPush(pInfo->pFirstKey, &v);
832,245✔
528
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
832,289!
529

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

534
          i += 1;
832,281✔
535
        }
536
      }
537
    }
538
  }
539

540
_end:
4,373,373✔
541
  tStatisBlockDestroy(&block);
4,373,373✔
542
  if (code != 0) {
4,382,309!
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;
4,382,560✔
546
    pBlockLoadInfo->cost.statisElapsedTime += el;
4,382,560✔
547

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

553
static int32_t doLoadSttFilesBlk(SSttBlockLoadInfo *pBlockLoadInfo, SLDataIter *pIter, int64_t suid,
5,314,654✔
554
                                 _load_tomb_fn loadTombFn, void *pReader1, const char *idStr) {
555
  int64_t st = taosGetTimestampUs();
5,315,949✔
556

557
  const TSttBlkArray *pSttBlkArray = NULL;
5,315,949✔
558
  pBlockLoadInfo->sttBlockLoaded = true;
5,315,949✔
559

560
  // load the stt block info for each stt-block
561
  int32_t code = tsdbSttFileReadSttBlk(pIter->pReader, &pSttBlkArray);
5,315,949✔
562
  if (code != TSDB_CODE_SUCCESS) {
5,315,102!
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);
5,315,102✔
569
  if (code != TSDB_CODE_SUCCESS) {
5,318,194!
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;
5,318,194✔
576
  code = tsdbSttFileReadStatisBlk(pIter->pReader, (const TStatisBlkArray **)&pStatisBlkArray);
5,318,194✔
577
  if (code != TSDB_CODE_SUCCESS) {
5,318,226!
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);
5,318,226✔
584
  if (code != TSDB_CODE_SUCCESS) {
5,318,019!
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);
5,318,019✔
590

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

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

600
  if (*pFirst == *pVal) {
12,837,097✔
601
    return 0;
4,789,608✔
602
  } else {
603
    return *pFirst < *pVal ? -1 : 1;
8,047,489✔
604
  }
605
}
606

607
static void setSttInfoForCurrentTable(SSttBlockLoadInfo *pLoadInfo, uint64_t uid, SSttKeyRange *pRange,
11,691,527✔
608
                                      int64_t *numOfRows) {
609
  if (pRange == NULL || taosArrayGetSize(pLoadInfo->info.pUid) == 0) {
11,691,527!
610
    return;
1,991,388✔
611
  }
612

613
  int32_t index = taosArraySearchIdx(pLoadInfo->info.pUid, &uid, uidComparFn, TD_EQ);
9,699,206✔
614
  if (index >= 0) {
9,702,006✔
615
    pRange->skey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pFirstTs, index);
4,790,419✔
616
    pRange->ekey.ts = *(int64_t *)taosArrayGet(pLoadInfo->info.pLastTs, index);
4,787,257✔
617

618
    *numOfRows += *(int64_t *)taosArrayGet(pLoadInfo->info.pCount, index);
4,788,282✔
619

620
    if (pRange->skey.numOfPKs > 0) {
4,788,267✔
621
      memcpy(&pRange->skey.pks[0], taosArrayGet(pLoadInfo->info.pFirstKey, index), sizeof(SValue));
12,274✔
622
      memcpy(&pRange->ekey.pks[0], taosArrayGet(pLoadInfo->info.pLastKey, index), sizeof(SValue));
12,274✔
623
    }
624
  }
625
}
626

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

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

640
  pIter->pStartRowKey = pConf->pCurRowKey;
11,691,547✔
641
  pIter->pReader = pSttFileReader;
11,691,547✔
642
  pIter->pBlockLoadInfo = pBlockLoadInfo;
11,691,547✔
643

644
  // open stt file failed, ignore and continue
645
  if (pIter->pReader == NULL) {
11,691,547!
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) {
11,691,547✔
653
    code = doLoadSttFilesBlk(pBlockLoadInfo, pIter, pConf->suid, pConf->loadTombFn, pConf->pReader, idStr);
5,316,799✔
654
    if (code != TSDB_CODE_SUCCESS) {
5,317,354!
655
      return code;
×
656
    }
657
  }
658

659
  setSttInfoForCurrentTable(pBlockLoadInfo, pConf->uid, pKeyRange, numOfRows);
11,692,102✔
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);
11,689,680✔
664
  pIter->iSttBlk = binarySearchForStartBlock(pBlockLoadInfo->aSttBlk->pData, size, pConf->uid, backward);
11,688,346✔
665
  if (pIter->iSttBlk != -1) {
11,689,853✔
666
    pIter->pSttBlk = taosArrayGet(pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
4,872,470✔
667
    pIter->iRow = (pIter->backward) ? pIter->pSttBlk->nRow : -1;
4,870,948✔
668

669
    if ((!backward) && ((pConf->strictTimeRange && pIter->pSttBlk->minKey >= pIter->timeWindow.ekey) ||
4,870,948!
670
                        (!pConf->strictTimeRange && pIter->pSttBlk->minKey > pIter->timeWindow.ekey))) {
4,150,238!
671
      pIter->pSttBlk = NULL;
331✔
672
    }
673

674
    if (backward && ((pConf->strictTimeRange && pIter->pSttBlk->maxKey <= pIter->timeWindow.skey) ||
4,870,948!
675
                     (!pConf->strictTimeRange && pIter->pSttBlk->maxKey < pIter->timeWindow.skey))) {
721,027!
676
      pIter->pSttBlk = NULL;
655✔
677
      pIter->ignoreEarlierTs = true;
655✔
678
    }
679
  }
680

681
  return code;
11,688,331✔
682
}
683

684
void tLDataIterClose2(SLDataIter *pIter) {
5,324,980✔
685
  tsdbSttFileReaderClose(&pIter->pReader);
5,324,980✔
686
  pIter->pReader = NULL;
5,326,761✔
687
}
5,326,761✔
688

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

693
  pIter->iSttBlk += step;
4,932,435✔
694

695
  int32_t index = -1;
4,932,435✔
696
  size_t  size = pIter->pBlockLoadInfo->aSttBlk->size;
4,932,435✔
697
  for (int32_t i = pIter->iSttBlk; i < size && i >= 0; i += step) {
4,947,130!
698
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, i);
321,515✔
699
    if ((!pIter->backward) && p->minUid > pIter->uid) {
321,499✔
700
      break;
203,654✔
701
    }
702

703
    if (pIter->backward && p->maxUid < pIter->uid) {
117,845✔
704
      break;
17,945✔
705
    }
706

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

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

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

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

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

735
  pIter->pSttBlk = NULL;
4,932,419✔
736
  if (index != -1) {
4,932,419✔
737
    SSttBlk *p = taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, index);
84,060✔
738

739
    pIter->iSttBlk = index;
84,060✔
740
    pIter->pSttBlk = (SSttBlk *)taosArrayGet(pIter->pBlockLoadInfo->aSttBlk, pIter->iSttBlk);
84,060✔
741
    tsdbDebug("try next stt-file block:%d from %d, trigger by uid:%" PRIu64 ", stt-fileVer:%" PRId64
84,059✔
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);
4,848,359✔
746
  }
747
}
4,932,421✔
748

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

755
  int32_t code = loadLastBlock(pIter, idStr, &pData);
474,281,922✔
756
  if (code) {
467,766,703!
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) {
467,766,703✔
763
    i = binarySearchForStartRowIndex((uint64_t *)pData->aUid, pData->nRow, pIter->uid, pIter->backward);
4,893,863✔
764
    if (i == -1) {
4,894,581!
765
      tsdbDebug("failed to find the data in pBlockData, uid:%" PRIu64 " , %s", pIter->uid, idStr);
×
766
      pIter->iRow = -1;
82,969✔
767
      return code;
82,969✔
768
    }
769
  }
770

771
  for (; i < pData->nRow && i >= 0; i += step) {
484,817,132!
772
    if (pData->aUid != NULL) {
482,720,116✔
773
      if (!pIter->backward) {
480,914,505✔
774
        if (pData->aUid[i] > pIter->uid) {
441,126,493✔
775
          break;
1,010,449✔
776
        }
777
      } else {
778
        if (pData->aUid[i] < pIter->uid) {
39,788,012✔
779
          break;
97,181✔
780
        }
781
      }
782
    }
783

784
    int64_t ts = pData->aTSKEY[i];
481,612,486✔
785
    if (!pIter->backward) {               // asc
481,612,486✔
786
      if (ts > pIter->timeWindow.ekey) {  // no more data
441,384,082✔
787
        break;
245,089✔
788
      } else {
789
        if (ts < pIter->timeWindow.skey) {
441,138,993✔
790
          continue;
6,643,030✔
791
        }
792

793
        if (ts == pIter->timeWindow.skey && pIter->pStartRowKey->numOfPKs > 0) {
434,495,963✔
794
          SRowKey key;
795
          tColRowGetKey(pData, i, &key);
100,004!
796
          int32_t ret = pkCompEx(&key, pIter->pStartRowKey);
100,004✔
797
          if (ret < 0) {
100,004!
798
            continue;
×
799
          }
800
        }
801
      }
802
    } else {
803
      if (ts < pIter->timeWindow.skey) {
40,228,404✔
804
        break;
209,070✔
805
      } else {
806
        if (ts > pIter->timeWindow.ekey) {
40,019,334✔
807
          continue;
9,990,311✔
808
        }
809

810
        if (ts == pIter->timeWindow.ekey && pIter->pStartRowKey->numOfPKs > 0) {
30,029,023!
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];
464,524,986✔
822
    if (ver < pIter->verRange.minVer) {
464,524,986!
823
      continue;
×
824
    }
825

826
    // todo opt handle desc case
827
    if (ver > pIter->verRange.maxVer) {
464,524,986✔
828
      continue;
24✔
829
    }
830

831
    hasVal = true;
464,524,962✔
832
    break;
464,524,962✔
833
  }
834

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

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

846
  *hasNext = false;
476,675,047✔
847

848
  // no qualified last file block in current file, no need to fetch row
849
  if (pIter->pSttBlk == NULL) {
476,675,047✔
850
    return code;
6,820,959✔
851
  }
852

853
  code = loadLastBlock(pIter, idStr, &pBlockData);
469,854,088✔
854
  if (pBlockData == NULL || code != TSDB_CODE_SUCCESS) {
474,239,161!
855
    lino = __LINE__;
×
856
    goto _exit;
×
857
  }
858

859
  pIter->iRow += step;
474,587,933✔
860

861
  while (1) {
83,572✔
862
    bool skipBlock = false;
474,671,505✔
863
    code = findNextValidRow(pIter, idStr);
474,671,505✔
864
    TSDB_CHECK_CODE(code, lino, _exit);
468,224,234!
865

866
    if (pIter->pBlockLoadInfo->checkRemainingRow) {
468,224,234!
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) {
468,224,234!
890
      tLDataIterNextBlock(pIter, idStr);
4,821,855✔
891
      if (pIter->pSttBlk == NULL) {  // no more data
4,931,894✔
892
        goto _exit;
4,848,323✔
893
      }
894
    } else {
895
      break;
896
    }
897

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

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

910
  pIter->rInfo.suid = pBlockData->suid;
463,402,379✔
911
  pIter->rInfo.uid = pBlockData->uid;
463,402,379✔
912
  pIter->rInfo.row = tsdbRowFromBlockData(pBlockData, pIter->iRow);
463,402,379✔
913

914
_exit:
467,901,929✔
915
  if (code) {
467,901,929!
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);
467,966,284!
920
  return code;
467,966,284✔
921
}
922

923
// SMergeTree =================================================
924
static FORCE_INLINE int32_t tLDataIterCmprFn(const SRBTreeNode *p1, const SRBTreeNode *p2) {
72,545,867✔
925
  SLDataIter *pIter1 = (SLDataIter *)(((uint8_t *)p1) - offsetof(SLDataIter, node));
72,898,506✔
926
  SLDataIter *pIter2 = (SLDataIter *)(((uint8_t *)p2) - offsetof(SLDataIter, node));
72,898,506✔
927

928
  SRowKey rkey1 = {0}, rkey2 = {0};
72,898,506✔
929
  tRowGetKeyEx(&pIter1->rInfo.row, &rkey1);
72,898,506!
930
  tRowGetKeyEx(&pIter2->rInfo.row, &rkey2);
73,425,906!
931

932
  int32_t ret = tRowKeyCompare(&rkey1, &rkey2);
73,027,412✔
933
  if (ret < 0) {
73,038,835✔
934
    return -1;
50,917,725✔
935
  } else if (ret > 0) {
22,121,110✔
936
    return 1;
9,111,455✔
937
  } else {
938
    int64_t ver1 = TSDBROW_VERSION(&pIter1->rInfo.row);
13,009,655!
939
    int64_t ver2 = TSDBROW_VERSION(&pIter2->rInfo.row);
13,009,655!
940

941
    if (ver1 < ver2) {
13,009,655✔
942
      return -1;
5,253,057✔
943
    } else if (ver1 > ver2) {
7,756,598!
944
      return 1;
8,299,295✔
945
    } else {
946
      return 0;
×
947
    }
948
  }
949
}
950

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

955
int32_t tMergeTreeOpen2(SMergeTree *pMTree, SMergeTreeConf *pConf, SSttDataInfoForTable *pSttDataInfo) {
12,062,451✔
956
  int32_t code = TSDB_CODE_SUCCESS;
12,062,451✔
957

958
  pMTree->pIter = NULL;
12,062,451✔
959
  pMTree->backward = pConf->backward;
12,062,451✔
960
  pMTree->idStr = pConf->idstr;
12,062,451✔
961
  int32_t lino = 0;
12,062,451✔
962

963
  if (!pMTree->backward) {  // asc
12,062,451✔
964
    tRBTreeCreate(&pMTree->rbt, tLDataIterCmprFn);
10,195,371✔
965
  } else {  // desc
966
    tRBTreeCreate(&pMTree->rbt, tLDataIterDescCmprFn);
1,867,080✔
967
  }
968

969
  pMTree->ignoreEarlierTs = false;
12,064,880✔
970

971
  // no data exists, go to end
972
  int32_t numOfLevels = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->size;
12,064,880✔
973
  if (numOfLevels == 0) {
12,064,880✔
974
    goto _end;
605,456✔
975
  }
976

977
  code = adjustSttDataIters(pConf->pSttFileBlockIterArray, pConf->pCurrentFileset);
11,459,424✔
978
  if (code) {
11,458,071!
979
    goto _end;
×
980
  }
981

982
  for (int32_t j = 0; j < numOfLevels; ++j) {
23,114,595✔
983
    SSttLvl *pSttLevel = ((STFileSet *)pConf->pCurrentFileset)->lvlArr->data[j];
11,651,180✔
984
    SArray  *pList = taosArrayGetP(pConf->pSttFileBlockIterArray, j);
11,651,180✔
985

986
    for (int32_t i = 0; i < TARRAY2_SIZE(pSttLevel->fobjArr); ++i) {  // open all last file
23,341,763✔
987
      SLDataIter *pIter = taosArrayGetP(pList, i);
11,685,239✔
988

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

998
        code = tsdbSttFileReaderOpen(pSttLevel->fobjArr->data[i]->fname, &conf, &pSttFileReader);
5,312,808✔
999
        if (code != TSDB_CODE_SUCCESS) {
5,314,347!
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) {
11,684,218✔
1006
        code = tCreateSttBlockLoadInfo(pConf->pSchema, pConf->pCols, pConf->numOfCols, &pLoadInfo);
5,316,395✔
1007
        if (code != TSDB_CODE_SUCCESS) {
5,316,245!
1008
          goto _end;
×
1009
        }
1010
      }
1011

1012
      memset(pIter, 0, sizeof(SLDataIter));
11,684,068✔
1013

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

1018
      code = tLDataIterOpen2(pIter, pSttFileReader, cid, pMTree->backward, pConf, pLoadInfo, &range, &numOfRows,
11,684,068✔
1019
                             pMTree->idStr);
1020
      if (code != TSDB_CODE_SUCCESS) {
11,688,552!
1021
        goto _end;
×
1022
      }
1023

1024
      bool hasVal = NULL;
11,688,552✔
1025
      code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
11,688,552✔
1026
      if (code) {
11,693,018!
1027
        goto _end;
×
1028
      }
1029

1030
      if (hasVal) {
11,693,018✔
1031
        tMergeTreeAddIter(pMTree, pIter);
4,786,951✔
1032

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

1038
          pSttDataInfo->numOfRows += numOfRows;
4,785,728✔
1039
        }
1040
      } else {
1041
        if (!pMTree->ignoreEarlierTs) {
6,906,067!
1042
          pMTree->ignoreEarlierTs = pIter->ignoreEarlierTs;
6,906,117✔
1043
        }
1044
      }
1045
    }
1046
  }
1047

1048
  return code;
11,463,415✔
1049

1050
_end:
605,456✔
1051
  tMergeTreeClose(pMTree);
605,456✔
1052
  return code;
605,395✔
1053
}
1054

1055
void tMergeTreeAddIter(SMergeTree *pMTree, SLDataIter *pIter) {
4,786,358✔
1056
  SRBTreeNode *node = tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pIter);
4,786,358✔
1057
}
4,786,565✔
1058

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

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

1064
  if (pInfo->blockData[0].sttBlockIndex == pIter->iSttBlk) {
265,933,765✔
1065
    pInfo->blockData[0].pin = true;
151,079,049✔
1066
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
151,079,049✔
1067
    return;
151,093,892✔
1068
  }
1069

1070
  if (pInfo->blockData[1].sttBlockIndex == pIter->iSttBlk) {
114,854,716!
1071
    pInfo->blockData[1].pin = true;
121,013,003✔
1072
    tsdbTrace("pin stt-block, blockIndex:%d, stt-fileVer:%" PRId64 " %s", pIter->iSttBlk, pIter->cid, id);
121,013,003✔
1073
    return;
121,017,182✔
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) {
259,879,734✔
1080
  SSttBlockLoadInfo *pInfo = pIter->pBlockLoadInfo;
259,879,734✔
1081
  if (pInfo->blockData[0].pin) {
259,879,734✔
1082
    pInfo->blockData[0].pin = false;
149,086,718✔
1083
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[0].sttBlockIndex, pIter->cid, id);
149,086,718✔
1084
    return;
149,090,748✔
1085
  }
1086

1087
  if (pInfo->blockData[1].pin) {
110,793,016!
1088
    pInfo->blockData[1].pin = false;
118,928,126✔
1089
    tsdbTrace("unpin stt-block:%d, stt-fileVer:%" PRId64 " %s", pInfo->blockData[1].sttBlockIndex, pIter->cid, id);
118,928,126✔
1090
    return;
118,933,617✔
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) {
265,927,555✔
1097
  if (pMTree->pIter == NULL) {
265,927,555!
1098
    return;
×
1099
  }
1100

1101
  SLDataIter *pIter = pMTree->pIter;
265,927,555✔
1102
  pMTree->pPinnedBlockIter = pIter;
265,927,555✔
1103
  tLDataIterPinSttBlock(pIter, pMTree->idStr);
265,927,555✔
1104
}
1105

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

1111
  SLDataIter *pIter = pMTree->pPinnedBlockIter;
259,979,047✔
1112
  pMTree->pPinnedBlockIter = NULL;
259,979,047✔
1113
  tLDataIterUnpinSttBlock(pIter, pMTree->idStr);
259,979,047✔
1114
}
1115

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

1122
  *pHasNext = false;
476,346,534✔
1123
  if (pMTree->pIter) {
476,346,534✔
1124
    SLDataIter *pIter = pMTree->pIter;
464,648,096✔
1125
    bool        hasVal = false;
464,648,096✔
1126
    code = tLDataIterNextRow(pIter, pMTree->idStr, &hasVal);
464,648,096✔
1127
    if (!hasVal || (code != 0)) {
462,587,796✔
1128
      if (code == TSDB_CODE_FILE_CORRUPTED) {
4,762,269!
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;
4,762,269✔
1133
    }
1134

1135
    // compare with min in RB Tree
1136
    pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
462,587,796✔
1137
    if (pMTree->pIter && pIter) {
462,587,796✔
1138
      int32_t c = pMTree->rbt.cmprFn(&pMTree->pIter->node, &pIter->node);
50,448,959✔
1139
      if (c > 0) {
50,374,456✔
1140
        (void)tRBTreePut(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
4,100,433✔
1141
        pMTree->pIter = NULL;
5,243,872✔
1142
      } else if (!c) {
46,274,023!
1143
        return TSDB_CODE_INTERNAL_ERROR;
×
1144
      }
1145
    }
1146
  }
1147

1148
  if (pMTree->pIter == NULL) {
475,355,170✔
1149
    pMTree->pIter = (SLDataIter *)tRBTreeMin(&pMTree->rbt);
22,048,441✔
1150
    if (pMTree->pIter) {
22,048,441✔
1151
      tRBTreeDrop(&pMTree->rbt, (SRBTreeNode *)pMTree->pIter);
10,008,653✔
1152
    }
1153
  }
1154

1155
  *pHasNext = (pMTree->pIter != NULL);
475,765,208✔
1156
  return code;
475,765,208✔
1157
}
1158

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