tsdbRead.c 127.8 KB
Newer Older
H
hjxilinx 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

H
Haojun Liao 已提交
16
#include "osDef.h"
H
Hongze Cheng 已提交
17
#include "tsdb.h"
18

H
Hongze Cheng 已提交
19
#define ASCENDING_TRAVERSE(o) (o == TSDB_ORDER_ASC)
H
Hongze Cheng 已提交
20

21 22 23 24 25 26
typedef enum {
  EXTERNAL_ROWS_PREV = 0x1,
  EXTERNAL_ROWS_MAIN = 0x2,
  EXTERNAL_ROWS_NEXT = 0x3,
} EContentData;

27
typedef struct {
dengyihao's avatar
dengyihao 已提交
28
  STbDataIter* iter;
29 30 31 32
  int32_t      index;
  bool         hasVal;
} SIterInfo;

33 34
typedef struct {
  int32_t numOfBlocks;
35
  int32_t numOfLastFiles;
36 37
} SBlockNumber;

H
Haojun Liao 已提交
38
typedef struct STableBlockScanInfo {
dengyihao's avatar
dengyihao 已提交
39 40
  uint64_t  uid;
  TSKEY     lastKey;
H
Hongze Cheng 已提交
41 42 43 44 45 46 47 48
  SMapData  mapData;            // block info (compressed)
  SArray*   pBlockList;         // block data index list
  SIterInfo iter;               // mem buffer skip list iterator
  SIterInfo iiter;              // imem buffer skip list iterator
  SArray*   delSkyline;         // delete info for this table
  int32_t   fileDelIndex;       // file block delete index
  int32_t   lastBlockDelIndex;  // delete index for last block
  bool      iterInit;           // whether to initialize the in-memory skip list iterator or not
H
Haojun Liao 已提交
49 50 51
} STableBlockScanInfo;

typedef struct SBlockOrderWrapper {
dengyihao's avatar
dengyihao 已提交
52
  int64_t uid;
53
  int64_t offset;
H
Haojun Liao 已提交
54
} SBlockOrderWrapper;
H
Hongze Cheng 已提交
55 56

typedef struct SBlockOrderSupporter {
57 58 59 60
  SBlockOrderWrapper** pDataBlockInfo;
  int32_t*             indexPerTable;
  int32_t*             numOfBlocksPerTable;
  int32_t              numOfTables;
H
Hongze Cheng 已提交
61 62 63
} SBlockOrderSupporter;

typedef struct SIOCostSummary {
64 65 66
  int64_t numOfBlocks;
  double  blockLoadTime;
  double  buildmemBlock;
67
  int64_t headFileLoad;
68
  double  headFileLoadTime;
69
  int64_t smaDataLoad;
70
  double  smaLoadTime;
71 72
  int64_t lastBlockLoad;
  double  lastBlockLoadTime;
H
Hongze Cheng 已提交
73 74 75
} SIOCostSummary;

typedef struct SBlockLoadSuppInfo {
76
  SArray*          pColAgg;
77
  SColumnDataAgg   tsColAgg;
C
Cary Xu 已提交
78
  SColumnDataAgg** plist;
79 80
  int16_t*         colIds;    // column ids for loading file block data
  char**           buildBuf;  // build string tmp buffer, todo remove it later after all string format being updated.
H
Hongze Cheng 已提交
81 82
} SBlockLoadSuppInfo;

83
typedef struct SLastBlockReader {
H
Hongze Cheng 已提交
84 85 86 87 88
  STimeWindow        window;
  SVersionRange      verRange;
  int32_t            order;
  uint64_t           uid;
  SMergeTree         mergeTree;
89
  SSttBlockLoadInfo* pInfo;
90 91
} SLastBlockReader;

92
typedef struct SFilesetIter {
H
Hongze Cheng 已提交
93 94 95
  int32_t           numOfFiles;  // number of total files
  int32_t           index;       // current accessed index in the list
  SArray*           pFileList;   // data file list
96
  int32_t           order;
H
Hongze Cheng 已提交
97
  SLastBlockReader* pLastBlockReader;  // last file block reader
98
} SFilesetIter;
H
Haojun Liao 已提交
99 100

typedef struct SFileDataBlockInfo {
101
  // index position in STableBlockScanInfo in order to check whether neighbor block overlaps with it
dengyihao's avatar
dengyihao 已提交
102
  uint64_t uid;
103
  int32_t  tbBlockIdx;
H
Haojun Liao 已提交
104 105 106
} SFileDataBlockInfo;

typedef struct SDataBlockIter {
107
  int32_t   numOfBlocks;
108
  int32_t   index;
H
Hongze Cheng 已提交
109
  SArray*   blockList;  // SArray<SFileDataBlockInfo>
110
  int32_t   order;
H
Hongze Cheng 已提交
111
  SDataBlk  block;  // current SDataBlk data
112
  SHashObj* pTableMap;
H
Haojun Liao 已提交
113 114 115
} SDataBlockIter;

typedef struct SFileBlockDumpInfo {
dengyihao's avatar
dengyihao 已提交
116 117 118 119
  int32_t totalRows;
  int32_t rowIndex;
  int64_t lastKey;
  bool    allDumped;
H
Haojun Liao 已提交
120 121
} SFileBlockDumpInfo;

122
typedef struct SUidOrderCheckInfo {
123 124
  uint64_t* tableUidList;  // access table uid list in uid ascending order list
  int32_t   currentIndex;  // index in table uid list
125 126
} SUidOrderCheckInfo;

H
Haojun Liao 已提交
127
typedef struct SReaderStatus {
128
  bool                 loadFromFile;       // check file stage
129
  bool                 composedDataBlock;  // the returned data block is a composed block or not
130 131
  SHashObj*            pTableMap;          // SHash<STableBlockScanInfo>
  STableBlockScanInfo* pTableIter;         // table iterator used in building in-memory buffer data blocks.
132
  SUidOrderCheckInfo   uidCheckInfo;       // check all table in uid order
133
  SFileBlockDumpInfo   fBlockDumpInfo;
134 135 136 137
  SDFileSet*           pCurrentFileset;  // current opened file set
  SBlockData           fileBlockData;
  SFilesetIter         fileIter;
  SDataBlockIter       blockIter;
H
Haojun Liao 已提交
138 139
} SReaderStatus;

H
Hongze Cheng 已提交
140
struct STsdbReader {
H
Haojun Liao 已提交
141 142 143 144 145 146 147
  STsdb*             pTsdb;
  uint64_t           suid;
  int16_t            order;
  STimeWindow        window;  // the primary query time window that applies to all queries
  SSDataBlock*       pResBlock;
  int32_t            capacity;
  SReaderStatus      status;
148 149
  char*              idStr;  // query info handle, for debug purpose
  int32_t            type;   // query type: 1. retrieve all data blocks, 2. retrieve direct prev|next rows
H
Hongze Cheng 已提交
150
  SBlockLoadSuppInfo suppInfo;
H
Hongze Cheng 已提交
151
  STsdbReadSnap*     pReadSnap;
152
  SIOCostSummary     cost;
153 154
  STSchema*          pSchema;     // the newest version schema
  STSchema*          pMemSchema;  // the previous schema for in-memory data, to avoid load schema too many times
155 156
  SDataFReader*      pFileReader;
  SVersionRange      verRange;
157

158 159
  int32_t      step;
  STsdbReader* innerReader[2];
H
Hongze Cheng 已提交
160
};
H
Hongze Cheng 已提交
161

H
Haojun Liao 已提交
162
static SFileDataBlockInfo* getCurrentBlockInfo(SDataBlockIter* pBlockIter);
163 164
static int      buildDataBlockFromBufImpl(STableBlockScanInfo* pBlockScanInfo, int64_t endKey, int32_t capacity,
                                          STsdbReader* pReader);
165
static TSDBROW* getValidMemRow(SIterInfo* pIter, const SArray* pDelList, STsdbReader* pReader);
166 167
static int32_t  doMergeRowsInFileBlocks(SBlockData* pBlockData, STableBlockScanInfo* pScanInfo, STsdbReader* pReader,
                                        SRowMerger* pMerger);
H
Hongze Cheng 已提交
168 169
static int32_t  doMergeRowsInLastBlock(SLastBlockReader* pLastBlockReader, STableBlockScanInfo* pScanInfo, int64_t ts,
                                       SRowMerger* pMerger);
170
static int32_t  doMergeRowsInBuf(SIterInfo* pIter, uint64_t uid, int64_t ts, SArray* pDelList, SRowMerger* pMerger,
dengyihao's avatar
dengyihao 已提交
171
                                 STsdbReader* pReader);
H
Haojun Liao 已提交
172
static int32_t  doAppendRowFromTSRow(SSDataBlock* pBlock, STsdbReader* pReader, STSRow* pTSRow, uint64_t uid);
173
static int32_t  doAppendRowFromFileBlock(SSDataBlock* pResBlock, STsdbReader* pReader, SBlockData* pBlockData,
H
Hongze Cheng 已提交
174
                                         int32_t rowIndex);
175
static void     setComposedBlockFlag(STsdbReader* pReader, bool composed);
176
static bool     hasBeenDropped(const SArray* pDelList, int32_t* index, TSDBKEY* pKey, int32_t order);
177

H
Hongze Cheng 已提交
178 179 180 181
static int32_t doMergeMemTableMultiRows(TSDBROW* pRow, uint64_t uid, SIterInfo* pIter, SArray* pDelList,
                                        STSRow** pTSRow, STsdbReader* pReader, bool* freeTSRow);
static int32_t doMergeMemIMemRows(TSDBROW* pRow, TSDBROW* piRow, STableBlockScanInfo* pBlockScanInfo,
                                  STsdbReader* pReader, STSRow** pTSRow);
182 183
static int32_t mergeRowsInFileBlocks(SBlockData* pBlockData, STableBlockScanInfo* pBlockScanInfo, int64_t key,
                                     STsdbReader* pReader);
184

dengyihao's avatar
dengyihao 已提交
185 186 187 188
static int32_t initDelSkylineIterator(STableBlockScanInfo* pBlockScanInfo, STsdbReader* pReader, STbData* pMemTbData,
                                      STbData* piMemTbData);
static STsdb*  getTsdbByRetentions(SVnode* pVnode, TSKEY winSKey, SRetention* retentions, const char* idstr,
                                   int8_t* pLevel);
189
static SVersionRange getQueryVerRange(SVnode* pVnode, SQueryTableDataCond* pCond, int8_t level);
H
Hongze Cheng 已提交
190 191 192
static int64_t       getCurrentKeyInLastBlock(SLastBlockReader* pLastBlockReader);
static bool          hasDataInLastBlock(SLastBlockReader* pLastBlockReader);
static int32_t       doBuildDataBlock(STsdbReader* pReader);
H
Haojun Liao 已提交
193

194 195 196
static int32_t setColumnIdSlotList(STsdbReader* pReader, SSDataBlock* pBlock) {
  SBlockLoadSuppInfo* pSupInfo = &pReader->suppInfo;

197
  size_t numOfCols = blockDataGetNumOfCols(pBlock);
198

199
  pSupInfo->colIds = taosMemoryMalloc(numOfCols * sizeof(int16_t));
200
  pSupInfo->buildBuf = taosMemoryCalloc(numOfCols, POINTER_BYTES);
201 202 203
  if (pSupInfo->buildBuf == NULL || pSupInfo->colIds == NULL) {
    taosMemoryFree(pSupInfo->colIds);
    taosMemoryFree(pSupInfo->buildBuf);
H
Haojun Liao 已提交
204 205
    return TSDB_CODE_OUT_OF_MEMORY;
  }
H
Hongze Cheng 已提交
206

H
Haojun Liao 已提交
207 208
  for (int32_t i = 0; i < numOfCols; ++i) {
    SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, i);
209
    pSupInfo->colIds[i] = pCol->info.colId;
210 211 212 213

    if (IS_VAR_DATA_TYPE(pCol->info.type)) {
      pSupInfo->buildBuf[i] = taosMemoryMalloc(pCol->info.bytes);
    }
H
Haojun Liao 已提交
214
  }
H
Hongze Cheng 已提交
215

H
Haojun Liao 已提交
216 217
  return TSDB_CODE_SUCCESS;
}
H
Hongze Cheng 已提交
218

219
static SHashObj* createDataBlockScanInfo(STsdbReader* pTsdbReader, const STableKeyInfo* idList, int32_t numOfTables) {
H
Haojun Liao 已提交
220
  // allocate buffer in order to load data blocks from file
221
  // todo use simple hash instead, optimize the memory consumption
222 223 224
  SHashObj* pTableMap =
      taosHashInit(numOfTables, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
  if (pTableMap == NULL) {
H
Haojun Liao 已提交
225 226 227
    return NULL;
  }

228
  for (int32_t j = 0; j < numOfTables; ++j) {
229
    STableBlockScanInfo info = {.lastKey = 0, .uid = idList[j].uid};
230
    if (ASCENDING_TRAVERSE(pTsdbReader->order)) {
H
Haojun Liao 已提交
231
      int64_t skey = pTsdbReader->window.skey;
H
Hongze Cheng 已提交
232
      info.lastKey = (skey > INT64_MIN) ? (skey - 1) : skey;
wmmhello's avatar
wmmhello 已提交
233
    } else {
H
Haojun Liao 已提交
234
      int64_t ekey = pTsdbReader->window.ekey;
H
Hongze Cheng 已提交
235
      info.lastKey = (ekey < INT64_MAX) ? (ekey + 1) : ekey;
H
Haojun Liao 已提交
236
    }
wmmhello's avatar
wmmhello 已提交
237

238 239 240
    taosHashPut(pTableMap, &info.uid, sizeof(uint64_t), &info, sizeof(info));
    tsdbDebug("%p check table uid:%" PRId64 " from lastKey:%" PRId64 " %s", pTsdbReader, info.uid, info.lastKey,
              pTsdbReader->idStr);
H
Haojun Liao 已提交
241 242
  }

243 244
  tsdbDebug("%p create %d tables scan-info, size:%.2f Kb, %s", pTsdbReader, numOfTables,
            (sizeof(STableBlockScanInfo) * numOfTables) / 1024.0, pTsdbReader->idStr);
245

246
  return pTableMap;
H
Hongze Cheng 已提交
247
}
H
Hongze Cheng 已提交
248

H
Haojun Liao 已提交
249
static void resetDataBlockScanInfo(SHashObj* pTableMap, int64_t ts) {
250 251
  STableBlockScanInfo* p = NULL;

dengyihao's avatar
dengyihao 已提交
252
  while ((p = taosHashIterate(pTableMap, p)) != NULL) {
253 254
    p->iterInit = false;
    p->iiter.hasVal = false;
dengyihao's avatar
dengyihao 已提交
255
    if (p->iter.iter != NULL) {
256
      p->iter.iter = tsdbTbDataIterDestroy(p->iter.iter);
257 258
    }

259
    p->delSkyline = taosArrayDestroy(p->delSkyline);
H
Haojun Liao 已提交
260
    p->lastKey = ts;
261 262 263
  }
}

264 265 266 267 268 269 270 271
static void destroyBlockScanInfo(SHashObj* pTableMap) {
  STableBlockScanInfo* p = NULL;

  while ((p = taosHashIterate(pTableMap, p)) != NULL) {
    p->iterInit = false;
    p->iiter.hasVal = false;

    if (p->iter.iter != NULL) {
272
      p->iter.iter = tsdbTbDataIterDestroy(p->iter.iter);
273 274 275
    }

    if (p->iiter.iter != NULL) {
276
      p->iiter.iter = tsdbTbDataIterDestroy(p->iiter.iter);
277 278
    }

279 280
    p->delSkyline = taosArrayDestroy(p->delSkyline);
    p->pBlockList = taosArrayDestroy(p->pBlockList);
281
    tMapDataClear(&p->mapData);
282 283 284 285 286
  }

  taosHashCleanup(pTableMap);
}

287
static bool isEmptyQueryTimeWindow(STimeWindow* pWindow) {
288 289
  ASSERT(pWindow != NULL);
  return pWindow->skey > pWindow->ekey;
H
Haojun Liao 已提交
290
}
H
Hongze Cheng 已提交
291

292 293 294
// Update the query time window according to the data time to live(TTL) information, in order to avoid to return
// the expired data to client, even it is queried already.
static STimeWindow updateQueryTimeWindow(STsdb* pTsdb, STimeWindow* pWindow) {
dengyihao's avatar
dengyihao 已提交
295
  STsdbKeepCfg* pCfg = &pTsdb->keepCfg;
H
Hongze Cheng 已提交
296

297
  int64_t now = taosGetTimestamp(pCfg->precision);
dengyihao's avatar
dengyihao 已提交
298
  int64_t earilyTs = now - (tsTickPerMin[pCfg->precision] * pCfg->keep2) + 1;  // needs to add one tick
299

dengyihao's avatar
dengyihao 已提交
300
  STimeWindow win = *pWindow;
301 302 303 304 305 306
  if (win.skey < earilyTs) {
    win.skey = earilyTs;
  }

  return win;
}
H
Hongze Cheng 已提交
307

H
Haojun Liao 已提交
308
static void limitOutputBufferSize(const SQueryTableDataCond* pCond, int32_t* capacity) {
H
Haojun Liao 已提交
309 310 311 312 313 314
  int32_t rowLen = 0;
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
    rowLen += pCond->colList[i].bytes;
  }

  // make sure the output SSDataBlock size be less than 2MB.
H
Haojun Liao 已提交
315 316 317
  const int32_t TWOMB = 2 * 1024 * 1024;
  if ((*capacity) * rowLen > TWOMB) {
    (*capacity) = TWOMB / rowLen;
H
Haojun Liao 已提交
318 319 320 321
  }
}

// init file iterator
322
static int32_t initFilesetIterator(SFilesetIter* pIter, SArray* aDFileSet, STsdbReader* pReader) {
H
Hongze Cheng 已提交
323
  size_t numOfFileset = taosArrayGetSize(aDFileSet);
324

325 326
  pIter->index = ASCENDING_TRAVERSE(pReader->order) ? -1 : numOfFileset;
  pIter->order = pReader->order;
H
Hongze Cheng 已提交
327
  pIter->pFileList = aDFileSet;
328
  pIter->numOfFiles = numOfFileset;
H
Haojun Liao 已提交
329

330 331 332 333
  if (pIter->pLastBlockReader == NULL) {
    pIter->pLastBlockReader = taosMemoryCalloc(1, sizeof(struct SLastBlockReader));
    if (pIter->pLastBlockReader == NULL) {
      int32_t code = TSDB_CODE_OUT_OF_MEMORY;
334
      tsdbError("failed to prepare the last block iterator, code:%d %s", tstrerror(code), pReader->idStr);
335 336
      return code;
    }
337 338
  }

339 340 341 342 343 344 345 346
  SLastBlockReader* pLReader = pIter->pLastBlockReader;
  pLReader->order = pReader->order;
  pLReader->window = pReader->window;
  pLReader->verRange = pReader->verRange;

  pLReader->uid = 0;
  tMergeTreeClose(&pLReader->mergeTree);

347
  if (pLReader->pInfo == NULL) {
H
Haojun Liao 已提交
348 349 350 351 352
    pLReader->pInfo = tCreateLastBlockLoadInfo();
    if (pLReader->pInfo == NULL) {
      tsdbDebug("init fileset iterator failed, code:%s %s", tstrerror(terrno), pReader->idStr);
      return terrno;
    }
353 354
  }

355
  tsdbDebug("init fileset iterator, total files:%d %s", pIter->numOfFiles, pReader->idStr);
H
Haojun Liao 已提交
356 357 358
  return TSDB_CODE_SUCCESS;
}

359
static bool filesetIteratorNext(SFilesetIter* pIter, STsdbReader* pReader) {
360 361
  bool    asc = ASCENDING_TRAVERSE(pIter->order);
  int32_t step = asc ? 1 : -1;
362 363 364
  pIter->index += step;

  if ((asc && pIter->index >= pIter->numOfFiles) || ((!asc) && pIter->index < 0)) {
H
Haojun Liao 已提交
365 366 367
    return false;
  }

368 369
  pIter->pLastBlockReader->uid = 0;
  tMergeTreeClose(&pIter->pLastBlockReader->mergeTree);
370
  resetLastBlockLoadInfo(pIter->pLastBlockReader->pInfo);
371

H
Haojun Liao 已提交
372 373
  // check file the time range of coverage
  STimeWindow win = {0};
H
Hongze Cheng 已提交
374

375
  while (1) {
H
Haojun Liao 已提交
376 377 378
    if (pReader->pFileReader != NULL) {
      tsdbDataFReaderClose(&pReader->pFileReader);
    }
379

380
    pReader->status.pCurrentFileset = (SDFileSet*)taosArrayGet(pIter->pFileList, pIter->index);
H
Haojun Liao 已提交
381

382 383 384 385
    int32_t code = tsdbDataFReaderOpen(&pReader->pFileReader, pReader->pTsdb, pReader->status.pCurrentFileset);
    if (code != TSDB_CODE_SUCCESS) {
      goto _err;
    }
H
Haojun Liao 已提交
386

387 388
    pReader->cost.headFileLoad += 1;

389 390 391 392 393 394 395 396 397 398 399 400
    int32_t fid = pReader->status.pCurrentFileset->fid;
    tsdbFidKeyRange(fid, pReader->pTsdb->keepCfg.days, pReader->pTsdb->keepCfg.precision, &win.skey, &win.ekey);

    // current file are no longer overlapped with query time window, ignore remain files
    if ((asc && win.skey > pReader->window.ekey) || (!asc && win.ekey < pReader->window.skey)) {
      tsdbDebug("%p remain files are not qualified for qrange:%" PRId64 "-%" PRId64 ", ignore, %s", pReader,
                pReader->window.skey, pReader->window.ekey, pReader->idStr);
      return false;
    }

    if ((asc && (win.ekey < pReader->window.skey)) || ((!asc) && (win.skey > pReader->window.ekey))) {
      pIter->index += step;
401 402 403
      if ((asc && pIter->index >= pIter->numOfFiles) || ((!asc) && pIter->index < 0)) {
        return false;
      }
404 405
      continue;
    }
C
Cary Xu 已提交
406

407
    tsdbDebug("%p file found fid:%d for qrange:%" PRId64 "-%" PRId64 ", %s", pReader, fid, pReader->window.skey,
408
              pReader->window.ekey, pReader->idStr);
409 410
    return true;
  }
411

412
_err:
H
Haojun Liao 已提交
413 414 415
  return false;
}

416
static void resetDataBlockIterator(SDataBlockIter* pIter, int32_t order) {
417 418
  pIter->order = order;
  pIter->index = -1;
419
  pIter->numOfBlocks = 0;
420 421 422 423 424 425 426
  if (pIter->blockList == NULL) {
    pIter->blockList = taosArrayInit(4, sizeof(SFileDataBlockInfo));
  } else {
    taosArrayClear(pIter->blockList);
  }
}

L
Liu Jicong 已提交
427
static void cleanupDataBlockIterator(SDataBlockIter* pIter) { taosArrayDestroy(pIter->blockList); }
H
Haojun Liao 已提交
428

H
Haojun Liao 已提交
429
static void initReaderStatus(SReaderStatus* pStatus) {
dengyihao's avatar
dengyihao 已提交
430 431
  pStatus->pTableIter = NULL;
  pStatus->loadFromFile = true;
H
Haojun Liao 已提交
432 433
}

434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
static SSDataBlock* createResBlock(SQueryTableDataCond* pCond, int32_t capacity) {
  SSDataBlock* pResBlock = createDataBlock();
  if (pResBlock == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
    SColumnInfoData colInfo = {{0}, 0};
    colInfo.info = pCond->colList[i];
    blockDataAppendColInfo(pResBlock, &colInfo);
  }

  int32_t code = blockDataEnsureCapacity(pResBlock, capacity);
  if (code != TSDB_CODE_SUCCESS) {
    terrno = code;
    taosMemoryFree(pResBlock);
    return NULL;
  }

  return pResBlock;
}

457 458
static int32_t tsdbReaderCreate(SVnode* pVnode, SQueryTableDataCond* pCond, STsdbReader** ppReader, int32_t capacity,
                                const char* idstr) {
H
Haojun Liao 已提交
459
  int32_t      code = 0;
460
  int8_t       level = 0;
H
Haojun Liao 已提交
461
  STsdbReader* pReader = (STsdbReader*)taosMemoryCalloc(1, sizeof(*pReader));
H
Hongze Cheng 已提交
462 463
  if (pReader == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Haojun Liao 已提交
464
    goto _end;
H
Hongze Cheng 已提交
465 466
  }

C
Cary Xu 已提交
467 468 469 470
  if (VND_IS_TSMA(pVnode)) {
    tsdbDebug("vgId:%d, tsma is selected to query", TD_VID(pVnode));
  }

H
Haojun Liao 已提交
471
  initReaderStatus(&pReader->status);
472

L
Liu Jicong 已提交
473
  pReader->pTsdb = getTsdbByRetentions(pVnode, pCond->twindows.skey, pVnode->config.tsdbCfg.retentions, idstr, &level);
dengyihao's avatar
dengyihao 已提交
474 475
  pReader->suid = pCond->suid;
  pReader->order = pCond->order;
476
  pReader->capacity = 4096;
dengyihao's avatar
dengyihao 已提交
477 478
  pReader->idStr = (idstr != NULL) ? strdup(idstr) : NULL;
  pReader->verRange = getQueryVerRange(pVnode, pCond, level);
479
  pReader->type = pCond->type;
480
  pReader->window = updateQueryTimeWindow(pReader->pTsdb, &pCond->twindows);
481

482
  ASSERT(pCond->numOfCols > 0);
H
Hongze Cheng 已提交
483

484
  limitOutputBufferSize(pCond, &pReader->capacity);
485

486 487
  // allocate buffer in order to load data blocks from file
  SBlockLoadSuppInfo* pSup = &pReader->suppInfo;
488
  pSup->pColAgg = taosArrayInit(4, sizeof(SColumnDataAgg));
489
  pSup->plist = taosMemoryCalloc(pCond->numOfCols, POINTER_BYTES);
490
  if (pSup->pColAgg == NULL || pSup->plist == NULL) {
491 492 493
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _end;
  }
H
Haojun Liao 已提交
494

495 496
  pSup->tsColAgg.colId = PRIMARYKEY_TIMESTAMP_COL_ID;

H
Hongze Cheng 已提交
497
  code = tBlockDataCreate(&pReader->status.fileBlockData);
H
Haojun Liao 已提交
498 499 500 501 502
  if (code != TSDB_CODE_SUCCESS) {
    terrno = code;
    goto _end;
  }

503 504 505 506
  pReader->pResBlock = createResBlock(pCond, pReader->capacity);
  if (pReader->pResBlock == NULL) {
    code = terrno;
    goto _end;
H
Hongze Cheng 已提交
507
  }
H
Hongze Cheng 已提交
508

509 510
  setColumnIdSlotList(pReader, pReader->pResBlock);

H
Hongze Cheng 已提交
511 512
  *ppReader = pReader;
  return code;
H
Hongze Cheng 已提交
513

H
Haojun Liao 已提交
514 515
_end:
  tsdbReaderClose(pReader);
H
Hongze Cheng 已提交
516 517 518
  *ppReader = NULL;
  return code;
}
H
Hongze Cheng 已提交
519

H
Haojun Liao 已提交
520
static int32_t doLoadBlockIndex(STsdbReader* pReader, SDataFReader* pFileReader, SArray* pIndexList) {
521
  SArray* aBlockIdx = taosArrayInit(8, sizeof(SBlockIdx));
H
Hongze Cheng 已提交
522

523
  int64_t st = taosGetTimestampUs();
H
Hongze Cheng 已提交
524
  int32_t code = tsdbReadBlockIdx(pFileReader, aBlockIdx);
H
Haojun Liao 已提交
525
  if (code != TSDB_CODE_SUCCESS) {
526
    goto _end;
H
Haojun Liao 已提交
527
  }
H
Hongze Cheng 已提交
528

529 530
  size_t num = taosArrayGetSize(aBlockIdx);
  if (num == 0) {
H
Haojun Liao 已提交
531
    taosArrayDestroy(aBlockIdx);
H
Haojun Liao 已提交
532 533
    return TSDB_CODE_SUCCESS;
  }
H
Hongze Cheng 已提交
534

535 536 537 538
  int64_t et1 = taosGetTimestampUs();

  SBlockIdx* pBlockIdx = NULL;
  for (int32_t i = 0; i < num; ++i) {
539
    pBlockIdx = (SBlockIdx*)taosArrayGet(aBlockIdx, i);
H
Haojun Liao 已提交
540

541
    // uid check
H
Hongze Cheng 已提交
542
    if (pBlockIdx->suid != pReader->suid) {
H
Haojun Liao 已提交
543 544 545 546
      continue;
    }

    // this block belongs to a table that is not queried.
H
Hongze Cheng 已提交
547
    void* p = taosHashGet(pReader->status.pTableMap, &pBlockIdx->uid, sizeof(uint64_t));
H
Haojun Liao 已提交
548 549 550 551 552 553
    if (p == NULL) {
      continue;
    }

    STableBlockScanInfo* pScanInfo = p;
    if (pScanInfo->pBlockList == NULL) {
554
      pScanInfo->pBlockList = taosArrayInit(4, sizeof(int32_t));
H
Haojun Liao 已提交
555 556
    }

H
Hongze Cheng 已提交
557
    taosArrayPush(pIndexList, pBlockIdx);
H
Haojun Liao 已提交
558
  }
H
Hongze Cheng 已提交
559

560
  int64_t et2 = taosGetTimestampUs();
561
  tsdbDebug("load block index for %d tables completed, elapsed time:%.2f ms, set blockIdx:%.2f ms, size:%.2f Kb %s",
562
            (int32_t)num, (et1 - st) / 1000.0, (et2 - et1) / 1000.0, num * sizeof(SBlockIdx) / 1024.0, pReader->idStr);
563 564 565

  pReader->cost.headFileLoadTime += (et1 - st) / 1000.0;

566
_end:
H
Hongze Cheng 已提交
567
  taosArrayDestroy(aBlockIdx);
H
Haojun Liao 已提交
568 569
  return code;
}
H
Hongze Cheng 已提交
570

571
static void cleanupTableScanInfo(SHashObj* pTableMap) {
572
  STableBlockScanInfo* px = NULL;
dengyihao's avatar
dengyihao 已提交
573
  while (1) {
574
    px = taosHashIterate(pTableMap, px);
575 576 577 578
    if (px == NULL) {
      break;
    }

579
    // reset the index in last block when handing a new file
580
    tMapDataClear(&px->mapData);
581 582
    taosArrayClear(px->pBlockList);
  }
583 584
}

585
static int32_t doLoadFileBlock(STsdbReader* pReader, SArray* pIndexList, SBlockNumber* pBlockNum) {
586 587 588 589 590 591
  int32_t numOfQTable = 0;
  size_t  sizeInDisk = 0;
  size_t  numOfTables = taosArrayGetSize(pIndexList);

  int64_t st = taosGetTimestampUs();
  cleanupTableScanInfo(pReader->status.pTableMap);
592

dengyihao's avatar
dengyihao 已提交
593
  for (int32_t i = 0; i < numOfTables; ++i) {
H
Haojun Liao 已提交
594
    SBlockIdx* pBlockIdx = taosArrayGet(pIndexList, i);
H
Hongze Cheng 已提交
595

596
    STableBlockScanInfo* pScanInfo = taosHashGet(pReader->status.pTableMap, &pBlockIdx->uid, sizeof(int64_t));
H
Hongze Cheng 已提交
597

598
    tMapDataReset(&pScanInfo->mapData);
H
Hongze Cheng 已提交
599
    tsdbReadDataBlk(pReader->pFileReader, pBlockIdx, &pScanInfo->mapData);
600

601
    sizeInDisk += pScanInfo->mapData.nData;
602
    for (int32_t j = 0; j < pScanInfo->mapData.nItem; ++j) {
H
Hongze Cheng 已提交
603 604
      SDataBlk block = {0};
      tMapDataGetItemByIdx(&pScanInfo->mapData, j, &block, tGetDataBlk);
H
Hongze Cheng 已提交
605

606
      // 1. time range check
607
      if (block.minKey.ts > pReader->window.ekey || block.maxKey.ts < pReader->window.skey) {
H
Haojun Liao 已提交
608 609
        continue;
      }
H
Hongze Cheng 已提交
610

611
      // 2. version range check
H
Hongze Cheng 已提交
612
      if (block.minVer > pReader->verRange.maxVer || block.maxVer < pReader->verRange.minVer) {
613 614
        continue;
      }
615

616
      void* p = taosArrayPush(pScanInfo->pBlockList, &j);
H
Haojun Liao 已提交
617
      if (p == NULL) {
618
        tMapDataClear(&pScanInfo->mapData);
H
Haojun Liao 已提交
619 620
        return TSDB_CODE_OUT_OF_MEMORY;
      }
621

622
      pBlockNum->numOfBlocks += 1;
H
Haojun Liao 已提交
623
    }
H
Hongze Cheng 已提交
624

H
Haojun Liao 已提交
625
    if (pScanInfo->pBlockList != NULL && taosArrayGetSize(pScanInfo->pBlockList) > 0) {
626 627 628 629
      numOfQTable += 1;
    }
  }

H
Hongze Cheng 已提交
630
  pBlockNum->numOfLastFiles = pReader->pFileReader->pSet->nSttF;
631
  int32_t total = pBlockNum->numOfLastFiles + pBlockNum->numOfBlocks;
632

633
  double el = (taosGetTimestampUs() - st) / 1000.0;
H
Hongze Cheng 已提交
634
  tsdbDebug(
635
      "load block of %d tables completed, blocks:%d in %d tables, last-files:%d, block-info-size:%.2f Kb, elapsed "
636
      "time:%.2f ms %s",
637
      numOfTables, pBlockNum->numOfBlocks, numOfQTable, pBlockNum->numOfLastFiles, sizeInDisk / 1000.0, el,
H
Hongze Cheng 已提交
638
      pReader->idStr);
639

640
  pReader->cost.numOfBlocks += total;
641
  pReader->cost.headFileLoadTime += el;
642

H
Haojun Liao 已提交
643 644
  return TSDB_CODE_SUCCESS;
}
H
Hongze Cheng 已提交
645

646
static void setBlockAllDumped(SFileBlockDumpInfo* pDumpInfo, int64_t maxKey, int32_t order) {
647
  int32_t step = ASCENDING_TRAVERSE(order) ? 1 : -1;
648
  pDumpInfo->allDumped = true;
649
  pDumpInfo->lastKey = maxKey + step;
H
Haojun Liao 已提交
650 651
}

652 653
static void doCopyColVal(SColumnInfoData* pColInfoData, int32_t rowIndex, int32_t colIndex, SColVal* pColVal,
                         SBlockLoadSuppInfo* pSup) {
H
Haojun Liao 已提交
654
  if (IS_VAR_DATA_TYPE(pColVal->type)) {
655
    if (pColVal->isNull || pColVal->isNone) {
H
Haojun Liao 已提交
656 657 658
      colDataAppendNULL(pColInfoData, rowIndex);
    } else {
      varDataSetLen(pSup->buildBuf[colIndex], pColVal->value.nData);
H
Haojun Liao 已提交
659
      ASSERT(pColVal->value.nData <= pColInfoData->info.bytes);
H
Haojun Liao 已提交
660 661 662 663
      memcpy(varDataVal(pSup->buildBuf[colIndex]), pColVal->value.pData, pColVal->value.nData);
      colDataAppend(pColInfoData, rowIndex, pSup->buildBuf[colIndex], false);
    }
  } else {
664
    colDataAppend(pColInfoData, rowIndex, (const char*)&pColVal->value, pColVal->isNull || pColVal->isNone);
H
Haojun Liao 已提交
665
  }
H
Haojun Liao 已提交
666 667
}

668
static SFileDataBlockInfo* getCurrentBlockInfo(SDataBlockIter* pBlockIter) {
669 670
  if (taosArrayGetSize(pBlockIter->blockList) == 0) {
    ASSERT(pBlockIter->numOfBlocks == taosArrayGetSize(pBlockIter->blockList));
671 672
    return NULL;
  }
673 674 675

  SFileDataBlockInfo* pBlockInfo = taosArrayGet(pBlockIter->blockList, pBlockIter->index);
  return pBlockInfo;
676 677
}

H
Hongze Cheng 已提交
678
static SDataBlk* getCurrentBlock(SDataBlockIter* pBlockIter) { return &pBlockIter->block; }
679

680
static int32_t copyBlockDataToSDataBlock(STsdbReader* pReader, STableBlockScanInfo* pBlockScanInfo) {
681
  SReaderStatus*  pStatus = &pReader->status;
682
  SDataBlockIter* pBlockIter = &pStatus->blockIter;
H
Hongze Cheng 已提交
683

684
  SBlockData*         pBlockData = &pStatus->fileBlockData;
H
Haojun Liao 已提交
685
  SFileDataBlockInfo* pFBlock = getCurrentBlockInfo(pBlockIter);
H
Hongze Cheng 已提交
686
  SDataBlk*           pBlock = getCurrentBlock(pBlockIter);
H
Haojun Liao 已提交
687
  SSDataBlock*        pResBlock = pReader->pResBlock;
688
  int32_t             numOfOutputCols = blockDataGetNumOfCols(pResBlock);
H
Haojun Liao 已提交
689

H
Haojun Liao 已提交
690
  SBlockLoadSuppInfo* pSupInfo = &pReader->suppInfo;
691
  SFileBlockDumpInfo* pDumpInfo = &pReader->status.fBlockDumpInfo;
H
Haojun Liao 已提交
692

H
Haojun Liao 已提交
693
  SColVal cv = {0};
694
  int64_t st = taosGetTimestampUs();
695 696
  bool    asc = ASCENDING_TRAVERSE(pReader->order);
  int32_t step = asc ? 1 : -1;
697

698
  int32_t rowIndex = 0;
699 700
  int32_t remain = asc ? (pBlockData->nRow - pDumpInfo->rowIndex) : (pDumpInfo->rowIndex + 1);

701 702 703 704 705 706 707 708
  int32_t endIndex = 0;
  if (remain <= pReader->capacity) {
    endIndex = pBlockData->nRow;
  } else {
    endIndex = pDumpInfo->rowIndex + step * pReader->capacity;
    remain = pReader->capacity;
  }

709
  int32_t          i = 0;
710 711
  SColumnInfoData* pColData = taosArrayGet(pResBlock->pDataBlock, i);
  if (pColData->info.colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
712
    for (int32_t j = pDumpInfo->rowIndex; j < endIndex && j >= 0; j += step) {
713 714 715 716 717
      colDataAppend(pColData, rowIndex++, (const char*)&pBlockData->aTSKEY[j], false);
    }
    i += 1;
  }

718 719 720
  int32_t colIndex = 0;
  int32_t num = taosArrayGetSize(pBlockData->aIdx);
  while (i < numOfOutputCols && colIndex < num) {
721 722 723
    rowIndex = 0;
    pColData = taosArrayGet(pResBlock->pDataBlock, i);

H
Hongze Cheng 已提交
724
    SColData* pData = tBlockDataGetColDataByIdx(pBlockData, colIndex);
725 726 727
    if (pData->cid < pColData->info.colId) {
      colIndex += 1;
    } else if (pData->cid == pColData->info.colId) {
728
      for (int32_t j = pDumpInfo->rowIndex; j < endIndex && j >= 0; j += step) {
729 730
        tColDataGetValue(pData, j, &cv);
        doCopyColVal(pColData, rowIndex++, i, &cv, pSupInfo);
H
Haojun Liao 已提交
731
      }
732
      colIndex += 1;
733
      i += 1;
734
      ASSERT(rowIndex == remain);
735 736
    } else {  // the specified column does not exist in file block, fill with null data
      colDataAppendNNULL(pColData, 0, remain);
737
      i += 1;
H
Haojun Liao 已提交
738
    }
739 740
  }

741
  while (i < numOfOutputCols) {
742 743 744
    pColData = taosArrayGet(pResBlock->pDataBlock, i);
    colDataAppendNNULL(pColData, 0, remain);
    i += 1;
H
Haojun Liao 已提交
745
  }
H
Haojun Liao 已提交
746

747
  pResBlock->info.rows = remain;
748
  pDumpInfo->rowIndex += step * remain;
749

750
  setBlockAllDumped(pDumpInfo, pBlock->maxKey.ts, pReader->order);
H
Haojun Liao 已提交
751

752
  double elapsedTime = (taosGetTimestampUs() - st) / 1000.0;
H
Haojun Liao 已提交
753
  pReader->cost.blockLoadTime += elapsedTime;
H
Haojun Liao 已提交
754

755
  int32_t unDumpedRows = asc ? pBlock->nRow - pDumpInfo->rowIndex : pDumpInfo->rowIndex + 1;
H
Haojun Liao 已提交
756
  tsdbDebug("%p copy file block to sdatablock, global index:%d, table index:%d, brange:%" PRId64 "-%" PRId64
757
            ", rows:%d, remain:%d, minVer:%" PRId64 ", maxVer:%" PRId64 ", elapsed time:%.2f ms, %s",
758
            pReader, pBlockIter->index, pFBlock->tbBlockIdx, pBlock->minKey.ts, pBlock->maxKey.ts, remain, unDumpedRows,
H
Hongze Cheng 已提交
759
            pBlock->minVer, pBlock->maxVer, elapsedTime, pReader->idStr);
760 761 762 763

  return TSDB_CODE_SUCCESS;
}

764
static int32_t doLoadFileBlockData(STsdbReader* pReader, SDataBlockIter* pBlockIter, SBlockData* pBlockData) {
765 766
  int64_t st = taosGetTimestampUs();

767
  SFileDataBlockInfo* pBlockInfo = getCurrentBlockInfo(pBlockIter);
768
  SFileBlockDumpInfo* pDumpInfo = &pReader->status.fBlockDumpInfo;
769
  ASSERT(pBlockInfo != NULL);
770

H
Hongze Cheng 已提交
771 772
  SDataBlk* pBlock = getCurrentBlock(pBlockIter);
  int32_t   code = tsdbReadDataBlock(pReader->pFileReader, pBlock, pBlockData);
773 774 775
  if (code != TSDB_CODE_SUCCESS) {
    tsdbError("%p error occurs in loading file block, global index:%d, table index:%d, brange:%" PRId64 "-%" PRId64
              ", rows:%d, code:%s %s",
776
              pReader, pBlockIter->index, pBlockInfo->tbBlockIdx, pBlock->minKey.ts, pBlock->maxKey.ts, pBlock->nRow,
777 778 779
              tstrerror(code), pReader->idStr);
    return code;
  }
780

781
  double elapsedTime = (taosGetTimestampUs() - st) / 1000.0;
782

783 784 785 786
  tsdbDebug("%p load file block into buffer, global index:%d, index in table block list:%d, brange:%" PRId64 "-%" PRId64
            ", rows:%d, minVer:%" PRId64 ", maxVer:%" PRId64 ", elapsed time:%.2f ms, %s",
            pReader, pBlockIter->index, pBlockInfo->tbBlockIdx, pBlock->minKey.ts, pBlock->maxKey.ts, pBlock->nRow,
            pBlock->minVer, pBlock->maxVer, elapsedTime, pReader->idStr);
787 788 789

  pReader->cost.blockLoadTime += elapsedTime;
  pDumpInfo->allDumped = false;
790

H
Haojun Liao 已提交
791
  return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
792
}
H
Hongze Cheng 已提交
793

H
Haojun Liao 已提交
794 795 796
static void cleanupBlockOrderSupporter(SBlockOrderSupporter* pSup) {
  taosMemoryFreeClear(pSup->numOfBlocksPerTable);
  taosMemoryFreeClear(pSup->indexPerTable);
H
Hongze Cheng 已提交
797

H
Haojun Liao 已提交
798 799 800 801
  for (int32_t i = 0; i < pSup->numOfTables; ++i) {
    SBlockOrderWrapper* pBlockInfo = pSup->pDataBlockInfo[i];
    taosMemoryFreeClear(pBlockInfo);
  }
H
Hongze Cheng 已提交
802

H
Haojun Liao 已提交
803 804
  taosMemoryFreeClear(pSup->pDataBlockInfo);
}
H
Hongze Cheng 已提交
805

H
Haojun Liao 已提交
806 807
static int32_t initBlockOrderSupporter(SBlockOrderSupporter* pSup, int32_t numOfTables) {
  ASSERT(numOfTables >= 1);
H
Hongze Cheng 已提交
808

H
Haojun Liao 已提交
809
  pSup->numOfBlocksPerTable = taosMemoryCalloc(1, sizeof(int32_t) * numOfTables);
810 811
  pSup->indexPerTable = taosMemoryCalloc(1, sizeof(int32_t) * numOfTables);
  pSup->pDataBlockInfo = taosMemoryCalloc(1, POINTER_BYTES * numOfTables);
H
Hongze Cheng 已提交
812

H
Haojun Liao 已提交
813 814 815 816
  if (pSup->numOfBlocksPerTable == NULL || pSup->indexPerTable == NULL || pSup->pDataBlockInfo == NULL) {
    cleanupBlockOrderSupporter(pSup);
    return TSDB_CODE_OUT_OF_MEMORY;
  }
H
Hongze Cheng 已提交
817

H
Haojun Liao 已提交
818 819
  return TSDB_CODE_SUCCESS;
}
H
Hongze Cheng 已提交
820

H
Haojun Liao 已提交
821
static int32_t fileDataBlockOrderCompar(const void* pLeft, const void* pRight, void* param) {
822
  int32_t leftIndex = *(int32_t*)pLeft;
H
Haojun Liao 已提交
823
  int32_t rightIndex = *(int32_t*)pRight;
H
Hongze Cheng 已提交
824

H
Haojun Liao 已提交
825
  SBlockOrderSupporter* pSupporter = (SBlockOrderSupporter*)param;
H
Hongze Cheng 已提交
826

H
Haojun Liao 已提交
827 828
  int32_t leftTableBlockIndex = pSupporter->indexPerTable[leftIndex];
  int32_t rightTableBlockIndex = pSupporter->indexPerTable[rightIndex];
H
Hongze Cheng 已提交
829

H
Haojun Liao 已提交
830 831 832 833 834 835 836
  if (leftTableBlockIndex > pSupporter->numOfBlocksPerTable[leftIndex]) {
    /* left block is empty */
    return 1;
  } else if (rightTableBlockIndex > pSupporter->numOfBlocksPerTable[rightIndex]) {
    /* right block is empty */
    return -1;
  }
H
Hongze Cheng 已提交
837

838
  SBlockOrderWrapper* pLeftBlock = &pSupporter->pDataBlockInfo[leftIndex][leftTableBlockIndex];
H
Haojun Liao 已提交
839
  SBlockOrderWrapper* pRightBlock = &pSupporter->pDataBlockInfo[rightIndex][rightTableBlockIndex];
H
Hongze Cheng 已提交
840

841 842 843 844
  return pLeftBlock->offset > pRightBlock->offset ? 1 : -1;
}

static int32_t doSetCurrentBlock(SDataBlockIter* pBlockIter) {
845 846 847
  SFileDataBlockInfo* pBlockInfo = getCurrentBlockInfo(pBlockIter);
  if (pBlockInfo != NULL) {
    STableBlockScanInfo* pScanInfo = taosHashGet(pBlockIter->pTableMap, &pBlockInfo->uid, sizeof(pBlockInfo->uid));
848
    int32_t*             mapDataIndex = taosArrayGet(pScanInfo->pBlockList, pBlockInfo->tbBlockIdx);
H
Hongze Cheng 已提交
849
    tMapDataGetItemByIdx(&pScanInfo->mapData, *mapDataIndex, &pBlockIter->block, tGetDataBlk);
850
  }
851 852 853 854 855 856

#if 0
  qDebug("check file block, table uid:%"PRIu64" index:%d offset:%"PRId64", ", pScanInfo->uid, *mapDataIndex, pBlockIter->block.aSubBlock[0].offset);
#endif

  return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
857
}
H
Hongze Cheng 已提交
858

859
static int32_t initBlockIterator(STsdbReader* pReader, SDataBlockIter* pBlockIter, int32_t numOfBlocks) {
860
  bool asc = ASCENDING_TRAVERSE(pReader->order);
H
Haojun Liao 已提交
861

862
  pBlockIter->numOfBlocks = numOfBlocks;
863
  taosArrayClear(pBlockIter->blockList);
864
  pBlockIter->pTableMap = pReader->status.pTableMap;
865

866 867
  // access data blocks according to the offset of each block in asc/desc order.
  int32_t numOfTables = (int32_t)taosHashGetSize(pReader->status.pTableMap);
H
Haojun Liao 已提交
868

869
  int64_t st = taosGetTimestampUs();
H
Haojun Liao 已提交
870

871
  SBlockOrderSupporter sup = {0};
872
  int32_t              code = initBlockOrderSupporter(&sup, numOfTables);
873 874 875
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }
H
Haojun Liao 已提交
876

877 878 879 880 881 882 883
  int32_t cnt = 0;
  void*   ptr = NULL;
  while (1) {
    ptr = taosHashIterate(pReader->status.pTableMap, ptr);
    if (ptr == NULL) {
      break;
    }
H
Haojun Liao 已提交
884

885 886 887 888
    STableBlockScanInfo* pTableScanInfo = (STableBlockScanInfo*)ptr;
    if (pTableScanInfo->pBlockList == NULL || taosArrayGetSize(pTableScanInfo->pBlockList) == 0) {
      continue;
    }
H
Haojun Liao 已提交
889

890 891
    size_t num = taosArrayGetSize(pTableScanInfo->pBlockList);
    sup.numOfBlocksPerTable[sup.numOfTables] = num;
H
Haojun Liao 已提交
892

893 894 895 896 897
    char* buf = taosMemoryMalloc(sizeof(SBlockOrderWrapper) * num);
    if (buf == NULL) {
      cleanupBlockOrderSupporter(&sup);
      return TSDB_CODE_TDB_OUT_OF_MEMORY;
    }
H
Haojun Liao 已提交
898

899
    sup.pDataBlockInfo[sup.numOfTables] = (SBlockOrderWrapper*)buf;
H
Hongze Cheng 已提交
900
    SDataBlk block = {0};
901 902
    for (int32_t k = 0; k < num; ++k) {
      SBlockOrderWrapper wrapper = {0};
903 904

      int32_t* mapDataIndex = taosArrayGet(pTableScanInfo->pBlockList, k);
H
Hongze Cheng 已提交
905
      tMapDataGetItemByIdx(&pTableScanInfo->mapData, *mapDataIndex, &block, tGetDataBlk);
906

907
      wrapper.uid = pTableScanInfo->uid;
908
      wrapper.offset = block.aSubBlock[0].offset;
H
Haojun Liao 已提交
909

910 911 912 913 914 915
      sup.pDataBlockInfo[sup.numOfTables][k] = wrapper;
      cnt++;
    }

    sup.numOfTables += 1;
  }
H
Haojun Liao 已提交
916

917
  ASSERT(numOfBlocks == cnt);
H
Haojun Liao 已提交
918

919
  // since there is only one table qualified, blocks are not sorted
920 921
  if (sup.numOfTables == 1) {
    for (int32_t i = 0; i < numOfBlocks; ++i) {
922 923
      SFileDataBlockInfo blockInfo = {.uid = sup.pDataBlockInfo[0][i].uid, .tbBlockIdx = i};
      taosArrayPush(pBlockIter->blockList, &blockInfo);
924
    }
925

926
    int64_t et = taosGetTimestampUs();
927
    tsdbDebug("%p create blocks info struct completed for one table, %d blocks not sorted, elapsed time:%.2f ms %s",
928
              pReader, numOfBlocks, (et - st) / 1000.0, pReader->idStr);
H
Haojun Liao 已提交
929

930
    pBlockIter->index = asc ? 0 : (numOfBlocks - 1);
H
Haojun Liao 已提交
931
    cleanupBlockOrderSupporter(&sup);
932
    doSetCurrentBlock(pBlockIter);
933
    return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
934
  }
H
Haojun Liao 已提交
935

936 937
  tsdbDebug("%p create data blocks info struct completed, %d blocks in %d tables %s", pReader, cnt, sup.numOfTables,
            pReader->idStr);
938

939
  ASSERT(cnt <= numOfBlocks && sup.numOfTables <= numOfTables);
H
Haojun Liao 已提交
940

941 942 943 944 945
  SMultiwayMergeTreeInfo* pTree = NULL;
  uint8_t                 ret = tMergeTreeCreate(&pTree, sup.numOfTables, &sup, fileDataBlockOrderCompar);
  if (ret != TSDB_CODE_SUCCESS) {
    cleanupBlockOrderSupporter(&sup);
    return TSDB_CODE_TDB_OUT_OF_MEMORY;
H
Haojun Liao 已提交
946
  }
H
Haojun Liao 已提交
947

948 949 950 951
  int32_t numOfTotal = 0;
  while (numOfTotal < cnt) {
    int32_t pos = tMergeTreeGetChosenIndex(pTree);
    int32_t index = sup.indexPerTable[pos]++;
H
Haojun Liao 已提交
952

953 954
    SFileDataBlockInfo blockInfo = {.uid = sup.pDataBlockInfo[pos][index].uid, .tbBlockIdx = index};
    taosArrayPush(pBlockIter->blockList, &blockInfo);
H
Haojun Liao 已提交
955

956 957 958 959
    // set data block index overflow, in order to disable the offset comparator
    if (sup.indexPerTable[pos] >= sup.numOfBlocksPerTable[pos]) {
      sup.indexPerTable[pos] = sup.numOfBlocksPerTable[pos] + 1;
    }
H
Haojun Liao 已提交
960

961 962
    numOfTotal += 1;
    tMergeTreeAdjust(pTree, tMergeTreeGetAdjustIndex(pTree));
H
Haojun Liao 已提交
963
  }
H
Haojun Liao 已提交
964

965
  int64_t et = taosGetTimestampUs();
H
Hongze Cheng 已提交
966 967
  tsdbDebug("%p %d data blocks access order completed, elapsed time:%.2f ms %s", pReader, numOfBlocks,
            (et - st) / 1000.0, pReader->idStr);
968 969
  cleanupBlockOrderSupporter(&sup);
  taosMemoryFree(pTree);
H
Haojun Liao 已提交
970

971
  pBlockIter->index = asc ? 0 : (numOfBlocks - 1);
972 973
  doSetCurrentBlock(pBlockIter);

974
  return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
975
}
H
Hongze Cheng 已提交
976

H
Haojun Liao 已提交
977
static bool blockIteratorNext(SDataBlockIter* pBlockIter) {
978 979
  bool asc = ASCENDING_TRAVERSE(pBlockIter->order);

980
  int32_t step = asc ? 1 : -1;
981
  if ((pBlockIter->index >= pBlockIter->numOfBlocks - 1 && asc) || (pBlockIter->index <= 0 && (!asc))) {
982 983 984
    return false;
  }

985
  pBlockIter->index += step;
986 987
  doSetCurrentBlock(pBlockIter);

988 989 990
  return true;
}

991 992 993
/**
 * This is an two rectangles overlap cases.
 */
H
Hongze Cheng 已提交
994
static int32_t dataBlockPartiallyRequired(STimeWindow* pWindow, SVersionRange* pVerRange, SDataBlk* pBlock) {
995 996
  return (pWindow->ekey < pBlock->maxKey.ts && pWindow->ekey >= pBlock->minKey.ts) ||
         (pWindow->skey > pBlock->minKey.ts && pWindow->skey <= pBlock->maxKey.ts) ||
H
Hongze Cheng 已提交
997 998
         (pVerRange->minVer > pBlock->minVer && pVerRange->minVer <= pBlock->maxVer) ||
         (pVerRange->maxVer < pBlock->maxVer && pVerRange->maxVer >= pBlock->minVer);
H
Haojun Liao 已提交
999
}
H
Hongze Cheng 已提交
1000

H
Hongze Cheng 已提交
1001 1002
static SDataBlk* getNeighborBlockOfSameTable(SFileDataBlockInfo* pFBlockInfo, STableBlockScanInfo* pTableBlockScanInfo,
                                             int32_t* nextIndex, int32_t order) {
1003 1004 1005
  bool asc = ASCENDING_TRAVERSE(order);
  if (asc && pFBlockInfo->tbBlockIdx >= taosArrayGetSize(pTableBlockScanInfo->pBlockList) - 1) {
    return NULL;
1006 1007
  }

1008
  if (!asc && pFBlockInfo->tbBlockIdx == 0) {
1009 1010 1011
    return NULL;
  }

1012
  int32_t step = asc ? 1 : -1;
1013
  *nextIndex = pFBlockInfo->tbBlockIdx + step;
1014

H
Hongze Cheng 已提交
1015 1016
  SDataBlk* pBlock = taosMemoryCalloc(1, sizeof(SDataBlk));
  int32_t*  indexInMapdata = taosArrayGet(pTableBlockScanInfo->pBlockList, *nextIndex);
1017

H
Hongze Cheng 已提交
1018
  tMapDataGetItemByIdx(&pTableBlockScanInfo->mapData, *indexInMapdata, pBlock, tGetDataBlk);
1019
  return pBlock;
1020 1021 1022 1023 1024
}

static int32_t findFileBlockInfoIndex(SDataBlockIter* pBlockIter, SFileDataBlockInfo* pFBlockInfo) {
  ASSERT(pBlockIter != NULL && pFBlockInfo != NULL);

1025
  int32_t step = ASCENDING_TRAVERSE(pBlockIter->order) ? 1 : -1;
1026 1027
  int32_t index = pBlockIter->index;

1028
  while (index < pBlockIter->numOfBlocks && index >= 0) {
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
    SFileDataBlockInfo* pFBlock = taosArrayGet(pBlockIter->blockList, index);
    if (pFBlock->uid == pFBlockInfo->uid && pFBlock->tbBlockIdx == pFBlockInfo->tbBlockIdx) {
      return index;
    }

    index += step;
  }

  ASSERT(0);
  return -1;
}

1041
static int32_t setFileBlockActiveInBlockIter(SDataBlockIter* pBlockIter, int32_t index, int32_t step) {
1042
  if (index < 0 || index >= pBlockIter->numOfBlocks) {
1043 1044 1045 1046
    return -1;
  }

  SFileDataBlockInfo fblock = *(SFileDataBlockInfo*)taosArrayGet(pBlockIter->blockList, index);
1047 1048 1049 1050 1051
  pBlockIter->index += step;

  if (index != pBlockIter->index) {
    taosArrayRemove(pBlockIter->blockList, index);
    taosArrayInsert(pBlockIter->blockList, pBlockIter->index, &fblock);
1052

1053 1054 1055
    SFileDataBlockInfo* pBlockInfo = taosArrayGet(pBlockIter->blockList, pBlockIter->index);
    ASSERT(pBlockInfo->uid == fblock.uid && pBlockInfo->tbBlockIdx == fblock.tbBlockIdx);
  }
1056

1057
  doSetCurrentBlock(pBlockIter);
1058 1059 1060
  return TSDB_CODE_SUCCESS;
}

H
Hongze Cheng 已提交
1061
static bool overlapWithNeighborBlock(SDataBlk* pBlock, SDataBlk* pNeighbor, int32_t order) {
1062 1063 1064 1065 1066 1067
  // it is the last block in current file, no chance to overlap with neighbor blocks.
  if (ASCENDING_TRAVERSE(order)) {
    return pBlock->maxKey.ts == pNeighbor->minKey.ts;
  } else {
    return pBlock->minKey.ts == pNeighbor->maxKey.ts;
  }
H
Haojun Liao 已提交
1068
}
H
Hongze Cheng 已提交
1069

H
Hongze Cheng 已提交
1070
static bool bufferDataInFileBlockGap(int32_t order, TSDBKEY key, SDataBlk* pBlock) {
H
Haojun Liao 已提交
1071
  bool ascScan = ASCENDING_TRAVERSE(order);
H
Hongze Cheng 已提交
1072

1073
  return (ascScan && (key.ts != TSKEY_INITIAL_VAL && key.ts <= pBlock->minKey.ts)) ||
1074
         (!ascScan && (key.ts != TSKEY_INITIAL_VAL && key.ts >= pBlock->maxKey.ts));
H
Haojun Liao 已提交
1075
}
H
Hongze Cheng 已提交
1076

H
Hongze Cheng 已提交
1077
static bool keyOverlapFileBlock(TSDBKEY key, SDataBlk* pBlock, SVersionRange* pVerRange) {
H
Hongze Cheng 已提交
1078 1079
  return (key.ts >= pBlock->minKey.ts && key.ts <= pBlock->maxKey.ts) && (pBlock->maxVer >= pVerRange->minVer) &&
         (pBlock->minVer <= pVerRange->maxVer);
H
Haojun Liao 已提交
1080 1081
}

H
Hongze Cheng 已提交
1082
static bool doCheckforDatablockOverlap(STableBlockScanInfo* pBlockScanInfo, const SDataBlk* pBlock) {
1083 1084 1085 1086 1087
  size_t num = taosArrayGetSize(pBlockScanInfo->delSkyline);

  for (int32_t i = pBlockScanInfo->fileDelIndex; i < num; i += 1) {
    TSDBKEY* p = taosArrayGet(pBlockScanInfo->delSkyline, i);
    if (p->ts >= pBlock->minKey.ts && p->ts <= pBlock->maxKey.ts) {
H
Hongze Cheng 已提交
1088
      if (p->version >= pBlock->minVer) {
1089 1090 1091
        return true;
      }
    } else if (p->ts < pBlock->minKey.ts) {  // p->ts < pBlock->minKey.ts
H
Hongze Cheng 已提交
1092
      if (p->version >= pBlock->minVer) {
1093 1094 1095 1096 1097 1098 1099
        if (i < num - 1) {
          TSDBKEY* pnext = taosArrayGet(pBlockScanInfo->delSkyline, i + 1);
          if (i + 1 == num - 1) {  // pnext is the last point
            if (pnext->ts >= pBlock->minKey.ts) {
              return true;
            }
          } else {
H
Hongze Cheng 已提交
1100
            if (pnext->ts >= pBlock->minKey.ts && pnext->version >= pBlock->minVer) {
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
              return true;
            }
          }
        } else {  // it must be the last point
          ASSERT(p->version == 0);
        }
      }
    } else {  // (p->ts > pBlock->maxKey.ts) {
      return false;
    }
  }

  return false;
}

H
Hongze Cheng 已提交
1116
static bool overlapWithDelSkyline(STableBlockScanInfo* pBlockScanInfo, const SDataBlk* pBlock, int32_t order) {
1117 1118 1119 1120
  if (pBlockScanInfo->delSkyline == NULL) {
    return false;
  }

1121
  // ts is not overlap
1122
  TSDBKEY* pFirst = taosArrayGet(pBlockScanInfo->delSkyline, 0);
L
Liu Jicong 已提交
1123
  TSDBKEY* pLast = taosArrayGetLast(pBlockScanInfo->delSkyline);
1124 1125 1126 1127 1128
  if (pBlock->minKey.ts > pLast->ts || pBlock->maxKey.ts < pFirst->ts) {
    return false;
  }

  // version is not overlap
1129 1130 1131 1132
  if (ASCENDING_TRAVERSE(order)) {
    return doCheckforDatablockOverlap(pBlockScanInfo, pBlock);
  } else {
    int32_t index = pBlockScanInfo->fileDelIndex;
1133
    while (1) {
1134 1135 1136 1137 1138
      TSDBKEY* p = taosArrayGet(pBlockScanInfo->delSkyline, index);
      if (p->ts > pBlock->minKey.ts && index > 0) {
        index -= 1;
      } else {  // find the first point that is smaller than the minKey.ts of dataBlock.
        break;
1139 1140 1141
      }
    }

1142 1143
    return doCheckforDatablockOverlap(pBlockScanInfo, pBlock);
  }
1144 1145
}

1146 1147 1148 1149
// 1. the version of all rows should be less than the endVersion
// 2. current block should not overlap with next neighbor block
// 3. current timestamp should not be overlap with each other
// 4. output buffer should be large enough to hold all rows in current block
1150
// 5. delete info should not overlap with current block data
H
Hongze Cheng 已提交
1151
static bool fileBlockShouldLoad(STsdbReader* pReader, SFileDataBlockInfo* pFBlock, SDataBlk* pBlock,
1152
                                STableBlockScanInfo* pScanInfo, TSDBKEY key, SLastBlockReader* pLastBlockReader) {
H
Hongze Cheng 已提交
1153 1154
  int32_t   neighborIndex = 0;
  SDataBlk* pNeighbor = getNeighborBlockOfSameTable(pFBlock, pScanInfo, &neighborIndex, pReader->order);
1155

1156
  // overlap with neighbor
1157 1158 1159
  bool overlapWithNeighbor = false;
  if (pNeighbor) {
    overlapWithNeighbor = overlapWithNeighborBlock(pBlock, pNeighbor, pReader->order);
1160
    taosMemoryFree(pNeighbor);
1161 1162
  }

1163
  // has duplicated ts of different version in this block
L
Liu Jicong 已提交
1164 1165
  bool hasDup = (pBlock->nSubBlock == 1) ? pBlock->hasDup : true;
  bool overlapWithDel = overlapWithDelSkyline(pScanInfo, pBlock, pReader->order);
1166

1167
  // todo here we need to each key in the last files to identify if it is really overlapped with last block
1168
  // todo
1169
  bool overlapWithlastBlock = false;
1170
#if 0
H
Hongze Cheng 已提交
1171
  if (taosArrayGetSize(pLastBlockReader->pSstBlk) > 0 && (pLastBlockReader->currentBlockIndex != -1)) {
H
Hongze Cheng 已提交
1172
    SSttBlk* pSstBlk = taosArrayGet(pLastBlockReader->pSstBlk, pLastBlockReader->currentBlockIndex);
H
Hongze Cheng 已提交
1173
    overlapWithlastBlock = !(pBlock->maxKey.ts < pSstBlk->minKey || pBlock->minKey.ts > pSstBlk->maxKey);
1174
  }
1175
#endif
1176

1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
  bool moreThanOutputCapacity = pBlock->nRow > pReader->capacity;
  bool partiallyRequired = dataBlockPartiallyRequired(&pReader->window, &pReader->verRange, pBlock);
  bool overlapWithKey = keyOverlapFileBlock(key, pBlock, &pReader->verRange);

  bool loadDataBlock = (overlapWithNeighbor || hasDup || partiallyRequired || overlapWithKey ||
                        moreThanOutputCapacity || overlapWithDel || overlapWithlastBlock);

  // log the reason why load the datablock for profile
  if (loadDataBlock) {
    tsdbDebug("%p uid:%" PRIu64
1187
              " need to load the datablock, overlapwithneighborblock:%d, hasDup:%d, partiallyRequired:%d, "
1188 1189 1190 1191 1192 1193
              "overlapWithKey:%d, greaterThanBuf:%d, overlapWithDel:%d, overlapWithlastBlock:%d, %s",
              pReader, pFBlock->uid, overlapWithNeighbor, hasDup, partiallyRequired, overlapWithKey,
              moreThanOutputCapacity, overlapWithDel, overlapWithlastBlock, pReader->idStr);
  }

  return loadDataBlock;
H
Haojun Liao 已提交
1194 1195
}

1196
static int32_t buildDataBlockFromBuf(STsdbReader* pReader, STableBlockScanInfo* pBlockScanInfo, int64_t endKey) {
1197
  if (!(pBlockScanInfo->iiter.hasVal || pBlockScanInfo->iter.hasVal)) {
1198 1199
    return TSDB_CODE_SUCCESS;
  }
H
Haojun Liao 已提交
1200

1201 1202 1203
  SSDataBlock* pBlock = pReader->pResBlock;

  int64_t st = taosGetTimestampUs();
1204
  int32_t code = buildDataBlockFromBufImpl(pBlockScanInfo, endKey, pReader->capacity, pReader);
H
Haojun Liao 已提交
1205

1206
  blockDataUpdateTsWindow(pBlock, 0);
1207
  pBlock->info.uid = pBlockScanInfo->uid;
1208

1209
  setComposedBlockFlag(pReader, true);
1210

1211
  double elapsedTime = (taosGetTimestampUs() - st) / 1000.0;
S
Shengliang Guan 已提交
1212
  tsdbDebug("%p build data block from cache completed, elapsed time:%.2f ms, numOfRows:%d, brange:%" PRId64
1213 1214 1215
            " - %" PRId64 " %s",
            pReader, elapsedTime, pBlock->info.rows, pBlock->info.window.skey, pBlock->info.window.ekey,
            pReader->idStr);
1216 1217

  pReader->cost.buildmemBlock += elapsedTime;
H
Haojun Liao 已提交
1218 1219 1220
  return code;
}

1221 1222
static bool tryCopyDistinctRowFromFileBlock(STsdbReader* pReader, SBlockData* pBlockData, int64_t key,
                                            SFileBlockDumpInfo* pDumpInfo) {
1223 1224 1225 1226 1227
  // opt version
  // 1. it is not a border point
  // 2. the direct next point is not an duplicated timestamp
  if ((pDumpInfo->rowIndex < pDumpInfo->totalRows - 1 && pReader->order == TSDB_ORDER_ASC) ||
      (pDumpInfo->rowIndex > 0 && pReader->order == TSDB_ORDER_DESC)) {
1228
    int32_t step = pReader->order == TSDB_ORDER_ASC ? 1 : -1;
1229 1230

    int64_t nextKey = pBlockData->aTSKEY[pDumpInfo->rowIndex + step];
1231
    if (nextKey != key) {  // merge is not needed
1232
      doAppendRowFromFileBlock(pReader->pResBlock, pReader, pBlockData, pDumpInfo->rowIndex);
1233 1234 1235 1236 1237 1238 1239 1240
      pDumpInfo->rowIndex += step;
      return true;
    }
  }

  return false;
}

1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
static bool nextRowFromLastBlocks(SLastBlockReader* pLastBlockReader, STableBlockScanInfo* pBlockScanInfo) {
  while (1) {
    bool hasVal = tMergeTreeNext(&pLastBlockReader->mergeTree);
    if (!hasVal) {
      return false;
    }

    TSDBROW row = tMergeTreeGetRow(&pLastBlockReader->mergeTree);
    TSDBKEY k = TSDBROW_KEY(&row);
    if (!hasBeenDropped(pBlockScanInfo->delSkyline, &pBlockScanInfo->lastBlockDelIndex, &k, pLastBlockReader->order)) {
      return true;
    }
  }
}

static bool tryCopyDistinctRowFromSttBlock(TSDBROW* fRow, SLastBlockReader* pLastBlockReader,
                                           STableBlockScanInfo* pScanInfo, int64_t ts, STsdbReader* pReader) {
  bool hasVal = nextRowFromLastBlocks(pLastBlockReader, pScanInfo);
  if (hasVal) {
    int64_t next1 = getCurrentKeyInLastBlock(pLastBlockReader);
    if (next1 != ts) {
      doAppendRowFromFileBlock(pReader->pResBlock, pReader, fRow->pBlockData, fRow->iRow);
      return true;
    }
  } else {
    doAppendRowFromFileBlock(pReader->pResBlock, pReader, fRow->pBlockData, fRow->iRow);
    return true;
  }

  return false;
}

H
Haojun Liao 已提交
1273 1274 1275 1276 1277 1278
static FORCE_INLINE STSchema* doGetSchemaForTSRow(int32_t sversion, STsdbReader* pReader, uint64_t uid) {
  // always set the newest schema version in pReader->pSchema
  if (pReader->pSchema == NULL) {
    pReader->pSchema = metaGetTbTSchema(pReader->pTsdb->pVnode->pMeta, uid, -1);
  }

1279
  if (pReader->pSchema && sversion == pReader->pSchema->version) {
H
Haojun Liao 已提交
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
    return pReader->pSchema;
  }

  if (pReader->pMemSchema == NULL) {
    int32_t code =
        metaGetTbTSchemaEx(pReader->pTsdb->pVnode->pMeta, pReader->suid, uid, sversion, &pReader->pMemSchema);
    return pReader->pMemSchema;
  }

  if (pReader->pMemSchema->version == sversion) {
    return pReader->pMemSchema;
  }

  taosMemoryFree(pReader->pMemSchema);
  int32_t code = metaGetTbTSchemaEx(pReader->pTsdb->pVnode->pMeta, pReader->suid, uid, sversion, &pReader->pMemSchema);
  return pReader->pMemSchema;
}

1298
static int32_t doMergeBufAndFileRows(STsdbReader* pReader, STableBlockScanInfo* pBlockScanInfo, TSDBROW* pRow,
1299 1300 1301 1302 1303 1304
                                     SIterInfo* pIter, int64_t key, SLastBlockReader* pLastBlockReader) {
  SRowMerger          merge = {0};
  STSRow*             pTSRow = NULL;
  SBlockData*         pBlockData = &pReader->status.fileBlockData;
  SFileBlockDumpInfo* pDumpInfo = &pReader->status.fBlockDumpInfo;

1305
  int64_t tsLast = INT64_MIN;
1306
  if (hasDataInLastBlock(pLastBlockReader)) {
1307 1308
    tsLast = getCurrentKeyInLastBlock(pLastBlockReader);
  }
1309

H
Hongze Cheng 已提交
1310 1311
  TSDBKEY k = TSDBROW_KEY(pRow);
  TSDBROW fRow = tsdbRowFromBlockData(pBlockData, pDumpInfo->rowIndex);
1312

1313 1314
  int64_t minKey = 0;
  if (pReader->order == TSDB_ORDER_ASC) {
H
Hongze Cheng 已提交
1315
    minKey = INT64_MAX;  // chosen the minimum value
1316
    if (minKey > tsLast && hasDataInLastBlock(pLastBlockReader)) {
1317 1318
      minKey = tsLast;
    }
1319

1320 1321 1322
    if (minKey > k.ts) {
      minKey = k.ts;
    }
1323

1324 1325 1326 1327 1328
    if (minKey > key && pBlockData->nRow > 0) {
      minKey = key;
    }
  } else {
    minKey = INT64_MIN;
1329
    if (minKey < tsLast && hasDataInLastBlock(pLastBlockReader)) {
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
      minKey = tsLast;
    }

    if (minKey < k.ts) {
      minKey = k.ts;
    }

    if (minKey < key && pBlockData->nRow > 0) {
      minKey = key;
    }
1340 1341 1342 1343
  }

  bool init = false;

1344
  // ASC: file block ---> last block -----> imem -----> mem
H
Hongze Cheng 已提交
1345
  // DESC: mem -----> imem -----> last block -----> file block
1346 1347
  if (pReader->order == TSDB_ORDER_ASC) {
    if (minKey == key) {
1348
      init = true;
1349 1350
      tRowMergerInit(&merge, &fRow, pReader->pSchema);
      doMergeRowsInFileBlocks(pBlockData, pBlockScanInfo, pReader, &merge);
1351 1352
    }

1353
    if (minKey == tsLast) {
1354
      TSDBROW fRow1 = tMergeTreeGetRow(&pLastBlockReader->mergeTree);
H
Haojun Liao 已提交
1355 1356 1357
      if (init) {
        tRowMerge(&merge, &fRow1);
      } else {
1358 1359 1360
        init = true;
        tRowMergerInit(&merge, &fRow1, pReader->pSchema);
      }
H
Haojun Liao 已提交
1361
      doMergeRowsInLastBlock(pLastBlockReader, pBlockScanInfo, tsLast, &merge);
1362
    }
1363

1364
    if (minKey == k.ts) {
H
Haojun Liao 已提交
1365 1366 1367
      if (init) {
        tRowMerge(&merge, pRow);
      } else {
1368 1369 1370 1371 1372 1373 1374 1375 1376
        init = true;
        STSchema* pSchema = doGetSchemaForTSRow(TSDBROW_SVERSION(pRow), pReader, pBlockScanInfo->uid);
        tRowMergerInit(&merge, pRow, pSchema);
      }
      doMergeRowsInBuf(pIter, pBlockScanInfo->uid, k.ts, pBlockScanInfo->delSkyline, &merge, pReader);
    }
  } else {
    if (minKey == k.ts) {
      init = true;
1377 1378
      STSchema* pSchema = doGetSchemaForTSRow(TSDBROW_SVERSION(pRow), pReader, pBlockScanInfo->uid);
      tRowMergerInit(&merge, pRow, pSchema);
1379
      doMergeRowsInBuf(pIter, pBlockScanInfo->uid, k.ts, pBlockScanInfo->delSkyline, &merge, pReader);
1380 1381
    }

1382
    if (minKey == tsLast) {
1383
      TSDBROW fRow1 = tMergeTreeGetRow(&pLastBlockReader->mergeTree);
H
Haojun Liao 已提交
1384 1385 1386
      if (init) {
        tRowMerge(&merge, &fRow1);
      } else {
1387 1388 1389
        init = true;
        tRowMergerInit(&merge, &fRow1, pReader->pSchema);
      }
H
Haojun Liao 已提交
1390
      doMergeRowsInLastBlock(pLastBlockReader, pBlockScanInfo, tsLast, &merge);
1391 1392 1393
    }

    if (minKey == key) {
H
Haojun Liao 已提交
1394 1395 1396
      if (init) {
        tRowMerge(&merge, &fRow);
      } else {
1397 1398 1399 1400 1401
        init = true;
        tRowMergerInit(&merge, &fRow, pReader->pSchema);
      }
      doMergeRowsInFileBlocks(pBlockData, pBlockScanInfo, pReader, &merge);
    }
1402 1403
  }

1404 1405 1406 1407 1408
  int32_t code = tRowMergerGetRow(&merge, &pTSRow);
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }

1409 1410 1411 1412 1413 1414 1415
  doAppendRowFromTSRow(pReader->pResBlock, pReader, pTSRow, pBlockScanInfo->uid);

  taosMemoryFree(pTSRow);
  tRowMergerClear(&merge);
  return TSDB_CODE_SUCCESS;
}

1416 1417 1418
static int32_t doMergeFileBlockAndLastBlock(SLastBlockReader* pLastBlockReader, STsdbReader* pReader,
                                            STableBlockScanInfo* pBlockScanInfo, SBlockData* pBlockData,
                                            bool mergeBlockData) {
1419
  SFileBlockDumpInfo* pDumpInfo = &pReader->status.fBlockDumpInfo;
H
Hongze Cheng 已提交
1420
  int64_t             tsLastBlock = getCurrentKeyInLastBlock(pLastBlockReader);
1421 1422 1423

  STSRow*    pTSRow = NULL;
  SRowMerger merge = {0};
1424
  TSDBROW    fRow = tMergeTreeGetRow(&pLastBlockReader->mergeTree);
1425

1426 1427 1428 1429 1430 1431
  // only last block exists
  if ((!mergeBlockData) || (tsLastBlock != pBlockData->aTSKEY[pDumpInfo->rowIndex])) {
    if (tryCopyDistinctRowFromSttBlock(&fRow, pLastBlockReader, pBlockScanInfo, tsLastBlock, pReader)) {
      return TSDB_CODE_SUCCESS;
    } else {
      tRowMergerInit(&merge, &fRow, pReader->pSchema);
1432

1433 1434
      TSDBROW fRow1 = tMergeTreeGetRow(&pLastBlockReader->mergeTree);
      tRowMerge(&merge, &fRow1);
1435
      doMergeRowsInLastBlock(pLastBlockReader, pBlockScanInfo, tsLastBlock, &merge);
1436

1437 1438 1439 1440
      // merge with block data if ts == key
      if (mergeBlockData && (tsLastBlock == pBlockData->aTSKEY[pDumpInfo->rowIndex])) {
        doMergeRowsInFileBlocks(pBlockData, pBlockScanInfo, pReader, &merge);
      }
1441

1442 1443 1444 1445
      int32_t code = tRowMergerGetRow(&merge, &pTSRow);
      if (code != TSDB_CODE_SUCCESS) {
        return code;
      }
1446

1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
      doAppendRowFromTSRow(pReader->pResBlock, pReader, pTSRow, pBlockScanInfo->uid);

      taosMemoryFree(pTSRow);
      tRowMergerClear(&merge);
    }
  } else {  // not merge block data
    tRowMergerInit(&merge, &fRow, pReader->pSchema);
    doMergeRowsInLastBlock(pLastBlockReader, pBlockScanInfo, tsLastBlock, &merge);

    // merge with block data if ts == key
    if (mergeBlockData && (tsLastBlock == pBlockData->aTSKEY[pDumpInfo->rowIndex])) {
      doMergeRowsInFileBlocks(pBlockData, pBlockScanInfo, pReader, &merge);
    }

    int32_t code = tRowMergerGetRow(&merge, &pTSRow);
    if (code != TSDB_CODE_SUCCESS) {
      return code;
    }

    doAppendRowFromTSRow(pReader->pResBlock, pReader, pTSRow, pBlockScanInfo->uid);

    taosMemoryFree(pTSRow);
    tRowMergerClear(&merge);
  }
1471 1472 1473 1474

  return TSDB_CODE_SUCCESS;
}

1475 1476
static int32_t mergeFileBlockAndLastBlock(STsdbReader* pReader, SLastBlockReader* pLastBlockReader, int64_t key,
                                          STableBlockScanInfo* pBlockScanInfo, SBlockData* pBlockData) {
1477 1478
  SFileBlockDumpInfo* pDumpInfo = &pReader->status.fBlockDumpInfo;

1479 1480
  if (pBlockData->nRow > 0) {
    // no last block available, only data block exists
1481
    if (!hasDataInLastBlock(pLastBlockReader)) {
1482 1483 1484 1485 1486 1487 1488 1489 1490
      return mergeRowsInFileBlocks(pBlockData, pBlockScanInfo, key, pReader);
    }

    // row in last file block
    TSDBROW fRow = tsdbRowFromBlockData(pBlockData, pDumpInfo->rowIndex);
    int64_t ts = getCurrentKeyInLastBlock(pLastBlockReader);
    ASSERT(ts >= key);

    if (ASCENDING_TRAVERSE(pReader->order)) {
1491
      if (key < ts) {  // imem, mem are all empty, file blocks (data blocks and last block) exist
1492 1493 1494 1495
        return mergeRowsInFileBlocks(pBlockData, pBlockScanInfo, key, pReader);
      } else if (key == ts) {
        STSRow*    pTSRow = NULL;
        SRowMerger merge = {0};
1496

1497 1498
        tRowMergerInit(&merge, &fRow, pReader->pSchema);
        doMergeRowsInFileBlocks(pBlockData, pBlockScanInfo, pReader, &merge);
1499 1500 1501 1502

        TSDBROW fRow1 = tMergeTreeGetRow(&pLastBlockReader->mergeTree);
        tRowMerge(&merge, &fRow1);

1503
        doMergeRowsInLastBlock(pLastBlockReader, pBlockScanInfo, ts, &merge);
1504

1505 1506 1507 1508 1509
        int32_t code = tRowMergerGetRow(&merge, &pTSRow);
        if (code != TSDB_CODE_SUCCESS) {
          return code;
        }

1510
        doAppendRowFromTSRow(pReader->pResBlock, pReader, pTSRow, pBlockScanInfo->uid);
1511

1512 1513
        taosMemoryFree(pTSRow);
        tRowMergerClear(&merge);
1514
        return code;
1515
      } else {
1516 1517
        ASSERT(0);
        return TSDB_CODE_SUCCESS;
1518
      }
1519
    } else {  // desc order
1520
      return doMergeFileBlockAndLastBlock(pLastBlockReader, pReader, pBlockScanInfo, pBlockData, true);
1521
    }
1522
  } else {  // only last block exists
1523
    return doMergeFileBlockAndLastBlock(pLastBlockReader, pReader, pBlockScanInfo, NULL, false);
H
Haojun Liao 已提交
1524
  }
1525 1526
}

1527 1528
static int32_t doMergeMultiLevelRows(STsdbReader* pReader, STableBlockScanInfo* pBlockScanInfo, SBlockData* pBlockData,
                                     SLastBlockReader* pLastBlockReader) {
1529 1530 1531 1532 1533 1534
  SRowMerger merge = {0};
  STSRow*    pTSRow = NULL;

  SFileBlockDumpInfo* pDumpInfo = &pReader->status.fBlockDumpInfo;
  SArray*             pDelList = pBlockScanInfo->delSkyline;

1535 1536
  TSDBROW* pRow = getValidMemRow(&pBlockScanInfo->iter, pDelList, pReader);
  TSDBROW* piRow = getValidMemRow(&pBlockScanInfo->iiter, pDelList, pReader);
1537 1538
  ASSERT(pRow != NULL && piRow != NULL);

1539
  int64_t tsLast = INT64_MIN;
1540 1541 1542
  if (hasDataInLastBlock(pLastBlockReader)) {
    tsLast = getCurrentKeyInLastBlock(pLastBlockReader);
  }
1543 1544 1545 1546 1547 1548

  int64_t key = pBlockData->aTSKEY[pDumpInfo->rowIndex];

  TSDBKEY k = TSDBROW_KEY(pRow);
  TSDBKEY ik = TSDBROW_KEY(piRow);

1549
  int64_t minKey = 0;
1550 1551 1552 1553 1554
  if (ASCENDING_TRAVERSE(pReader->order)) {
    minKey = INT64_MAX;  // let's find the minimum
    if (minKey > k.ts) {
      minKey = k.ts;
    }
1555

1556 1557 1558
    if (minKey > ik.ts) {
      minKey = ik.ts;
    }
1559

1560 1561 1562
    if (minKey > key && pBlockData->nRow > 0) {
      minKey = key;
    }
1563

1564
    if (minKey > tsLast && hasDataInLastBlock(pLastBlockReader)) {
1565 1566 1567
      minKey = tsLast;
    }
  } else {
H
Hongze Cheng 已提交
1568
    minKey = INT64_MIN;  // let find the maximum ts value
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
    if (minKey < k.ts) {
      minKey = k.ts;
    }

    if (minKey < ik.ts) {
      minKey = ik.ts;
    }

    if (minKey < key && pBlockData->nRow > 0) {
      minKey = key;
    }

1581
    if (minKey < tsLast && hasDataInLastBlock(pLastBlockReader)) {
1582 1583
      minKey = tsLast;
    }
1584 1585 1586 1587
  }

  bool init = false;

1588 1589 1590 1591
  // ASC: file block -----> last block -----> imem -----> mem
  // DESC: mem -----> imem -----> last block -----> file block
  if (ASCENDING_TRAVERSE(pReader->order)) {
    if (minKey == key) {
1592
      init = true;
1593 1594 1595
      TSDBROW fRow = tsdbRowFromBlockData(pBlockData, pDumpInfo->rowIndex);
      tRowMergerInit(&merge, &fRow, pReader->pSchema);
      doMergeRowsInFileBlocks(pBlockData, pBlockScanInfo, pReader, &merge);
1596 1597
    }

1598
    if (minKey == tsLast) {
1599
      TSDBROW fRow1 = tMergeTreeGetRow(&pLastBlockReader->mergeTree);
H
Haojun Liao 已提交
1600 1601 1602
      if (init) {
        tRowMerge(&merge, &fRow1);
      } else {
1603 1604 1605
        init = true;
        tRowMergerInit(&merge, &fRow1, pReader->pSchema);
      }
H
Haojun Liao 已提交
1606
      doMergeRowsInLastBlock(pLastBlockReader, pBlockScanInfo, tsLast, &merge);
1607 1608 1609
    }

    if (minKey == ik.ts) {
H
Haojun Liao 已提交
1610 1611 1612
      if (init) {
        tRowMerge(&merge, piRow);
      } else {
1613 1614 1615 1616 1617
        init = true;
        STSchema* pSchema = doGetSchemaForTSRow(TSDBROW_SVERSION(piRow), pReader, pBlockScanInfo->uid);
        tRowMergerInit(&merge, piRow, pSchema);
      }
      doMergeRowsInBuf(&pBlockScanInfo->iiter, pBlockScanInfo->uid, ik.ts, pBlockScanInfo->delSkyline, &merge, pReader);
1618 1619
    }

1620
    if (minKey == k.ts) {
H
Haojun Liao 已提交
1621 1622 1623
      if (init) {
        tRowMerge(&merge, pRow);
      } else {
1624 1625 1626 1627 1628 1629 1630 1631
        STSchema* pSchema = doGetSchemaForTSRow(TSDBROW_SVERSION(pRow), pReader, pBlockScanInfo->uid);
        tRowMergerInit(&merge, pRow, pSchema);
      }
      doMergeRowsInBuf(&pBlockScanInfo->iter, pBlockScanInfo->uid, k.ts, pBlockScanInfo->delSkyline, &merge, pReader);
    }
  } else {
    if (minKey == k.ts) {
      init = true;
1632 1633
      STSchema* pSchema = doGetSchemaForTSRow(TSDBROW_SVERSION(pRow), pReader, pBlockScanInfo->uid);
      tRowMergerInit(&merge, pRow, pSchema);
1634 1635 1636 1637
      doMergeRowsInBuf(&pBlockScanInfo->iter, pBlockScanInfo->uid, k.ts, pBlockScanInfo->delSkyline, &merge, pReader);
    }

    if (minKey == ik.ts) {
H
Haojun Liao 已提交
1638 1639 1640
      if (init) {
        tRowMerge(&merge, piRow);
      } else {
1641 1642 1643 1644 1645 1646 1647 1648
        init = true;
        STSchema* pSchema = doGetSchemaForTSRow(TSDBROW_SVERSION(piRow), pReader, pBlockScanInfo->uid);
        tRowMergerInit(&merge, piRow, pSchema);
      }
      doMergeRowsInBuf(&pBlockScanInfo->iiter, pBlockScanInfo->uid, ik.ts, pBlockScanInfo->delSkyline, &merge, pReader);
    }

    if (minKey == tsLast) {
1649
      TSDBROW fRow1 = tMergeTreeGetRow(&pLastBlockReader->mergeTree);
H
Haojun Liao 已提交
1650 1651 1652
      if (init) {
        tRowMerge(&merge, &fRow1);
      } else {
1653 1654 1655
        init = true;
        tRowMergerInit(&merge, &fRow1, pReader->pSchema);
      }
H
Haojun Liao 已提交
1656
      doMergeRowsInLastBlock(pLastBlockReader, pBlockScanInfo, tsLast, &merge);
1657 1658 1659
    }

    if (minKey == key) {
H
Haojun Liao 已提交
1660
      TSDBROW fRow = tsdbRowFromBlockData(pBlockData, pDumpInfo->rowIndex);
1661 1662
      if (!init) {
        tRowMergerInit(&merge, &fRow, pReader->pSchema);
H
Haojun Liao 已提交
1663 1664
      } else {
        tRowMerge(&merge, &fRow);
1665 1666
      }
      doMergeRowsInFileBlocks(pBlockData, pBlockScanInfo, pReader, &merge);
1667 1668 1669
    }
  }

1670 1671 1672 1673 1674
  int32_t code = tRowMergerGetRow(&merge, &pTSRow);
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }

1675 1676 1677 1678
  doAppendRowFromTSRow(pReader->pResBlock, pReader, pTSRow, pBlockScanInfo->uid);

  taosMemoryFree(pTSRow);
  tRowMergerClear(&merge);
1679
  return code;
1680 1681
}

1682
#if 0
1683
static int32_t doMergeThreeLevelRows(STsdbReader* pReader, STableBlockScanInfo* pBlockScanInfo, SBlockData* pBlockData) {
1684 1685 1686
  SRowMerger merge = {0};
  STSRow*    pTSRow = NULL;

1687
  SFileBlockDumpInfo* pDumpInfo = &pReader->status.fBlockDumpInfo;
dengyihao's avatar
dengyihao 已提交
1688
  SArray*             pDelList = pBlockScanInfo->delSkyline;
1689

1690 1691
  TSDBROW* pRow = getValidMemRow(&pBlockScanInfo->iter, pDelList, pReader);
  TSDBROW* piRow = getValidMemRow(&pBlockScanInfo->iiter, pDelList, pReader);
1692
  ASSERT(pRow != NULL && piRow != NULL);
H
Haojun Liao 已提交
1693

1694
  int64_t key = pBlockData->aTSKEY[pDumpInfo->rowIndex];
1695
  bool    freeTSRow = false;
H
Haojun Liao 已提交
1696

1697
  uint64_t uid = pBlockScanInfo->uid;
H
Haojun Liao 已提交
1698

1699 1700 1701
  TSDBKEY k = TSDBROW_KEY(pRow);
  TSDBKEY ik = TSDBROW_KEY(piRow);
  if (ASCENDING_TRAVERSE(pReader->order)) {
1702 1703
    // [1&2] key <= [k.ts && ik.ts]
    if (key <= k.ts && key <= ik.ts) {
1704 1705 1706
      TSDBROW fRow = tsdbRowFromBlockData(pBlockData, pDumpInfo->rowIndex);
      tRowMergerInit(&merge, &fRow, pReader->pSchema);

1707
      doMergeRowsInFileBlocks(pBlockData, pBlockScanInfo, pReader, &merge);
H
Haojun Liao 已提交
1708

1709 1710
      if (ik.ts == key) {
        tRowMerge(&merge, piRow);
1711
        doMergeRowsInBuf(&pBlockScanInfo->iiter, uid, key, pBlockScanInfo->delSkyline, &merge, pReader);
1712 1713
      }

1714 1715
      if (k.ts == key) {
        tRowMerge(&merge, pRow);
1716
        doMergeRowsInBuf(&pBlockScanInfo->iter, uid, key, pBlockScanInfo->delSkyline, &merge, pReader);
1717 1718 1719
      }

      tRowMergerGetRow(&merge, &pTSRow);
H
Haojun Liao 已提交
1720
      doAppendRowFromTSRow(pReader->pResBlock, pReader, pTSRow, uid);
1721
      return TSDB_CODE_SUCCESS;
1722
    } else {  // key > ik.ts || key > k.ts
1723 1724
      ASSERT(key != ik.ts);

1725
      // [3] ik.ts < key <= k.ts
1726
      // [4] ik.ts < k.ts <= key
1727
      if (ik.ts < k.ts) {
1728
        doMergeMemTableMultiRows(piRow, uid, &pBlockScanInfo->iiter, pDelList, &pTSRow, pReader, &freeTSRow);
H
Haojun Liao 已提交
1729
        doAppendRowFromTSRow(pReader->pResBlock, pReader, pTSRow, uid);
1730 1731 1732
        if (freeTSRow) {
          taosMemoryFree(pTSRow);
        }
1733 1734 1735
        return TSDB_CODE_SUCCESS;
      }

1736 1737
      // [5] k.ts < key   <= ik.ts
      // [6] k.ts < ik.ts <= key
1738
      if (k.ts < ik.ts) {
1739
        doMergeMemTableMultiRows(pRow, uid, &pBlockScanInfo->iter, pDelList, &pTSRow, pReader, &freeTSRow);
H
Haojun Liao 已提交
1740
        doAppendRowFromTSRow(pReader->pResBlock, pReader, pTSRow, uid);
1741 1742 1743
        if (freeTSRow) {
          taosMemoryFree(pTSRow);
        }
1744 1745 1746
        return TSDB_CODE_SUCCESS;
      }

1747
      // [7] k.ts == ik.ts < key
1748
      if (k.ts == ik.ts) {
1749 1750
        ASSERT(key > ik.ts && key > k.ts);

1751
        doMergeMemIMemRows(pRow, piRow, pBlockScanInfo, pReader, &pTSRow);
H
Haojun Liao 已提交
1752
        doAppendRowFromTSRow(pReader->pResBlock, pReader, pTSRow, uid);
1753
        taosMemoryFree(pTSRow);
1754 1755 1756
        return TSDB_CODE_SUCCESS;
      }
    }
1757 1758 1759
  } else {  // descending order scan
    // [1/2] k.ts >= ik.ts && k.ts >= key
    if (k.ts >= ik.ts && k.ts >= key) {
H
Haojun Liao 已提交
1760
      STSchema* pSchema = doGetSchemaForTSRow(TSDBROW_SVERSION(pRow), pReader, pBlockScanInfo->uid);
1761

H
Haojun Liao 已提交
1762
      tRowMergerInit(&merge, pRow, pSchema);
1763
      doMergeRowsInBuf(&pBlockScanInfo->iter, uid, key, pBlockScanInfo->delSkyline, &merge, pReader);
1764 1765 1766

      if (ik.ts == k.ts) {
        tRowMerge(&merge, piRow);
1767
        doMergeRowsInBuf(&pBlockScanInfo->iiter, uid, key, pBlockScanInfo->delSkyline, &merge, pReader);
1768 1769 1770 1771 1772 1773 1774 1775 1776
      }

      if (k.ts == key) {
        TSDBROW fRow = tsdbRowFromBlockData(pBlockData, pDumpInfo->rowIndex);
        tRowMerge(&merge, &fRow);
        doMergeRowsInFileBlocks(pBlockData, pBlockScanInfo, pReader, &merge);
      }

      tRowMergerGetRow(&merge, &pTSRow);
H
Haojun Liao 已提交
1777
      doAppendRowFromTSRow(pReader->pResBlock, pReader, pTSRow, uid);
1778 1779
      return TSDB_CODE_SUCCESS;
    } else {
1780
      ASSERT(ik.ts != k.ts);  // this case has been included in the previous if branch
1781 1782 1783 1784

      // [3] ik.ts > k.ts >= Key
      // [4] ik.ts > key >= k.ts
      if (ik.ts > key) {
1785
        doMergeMemTableMultiRows(piRow, uid, &pBlockScanInfo->iiter, pDelList, &pTSRow, pReader, &freeTSRow);
H
Haojun Liao 已提交
1786
        doAppendRowFromTSRow(pReader->pResBlock, pReader, pTSRow, uid);
1787 1788 1789
        if (freeTSRow) {
          taosMemoryFree(pTSRow);
        }
1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800
        return TSDB_CODE_SUCCESS;
      }

      // [5] key > ik.ts > k.ts
      // [6] key > k.ts > ik.ts
      if (key > ik.ts) {
        TSDBROW fRow = tsdbRowFromBlockData(pBlockData, pDumpInfo->rowIndex);
        tRowMergerInit(&merge, &fRow, pReader->pSchema);

        doMergeRowsInFileBlocks(pBlockData, pBlockScanInfo, pReader, &merge);
        tRowMergerGetRow(&merge, &pTSRow);
H
Haojun Liao 已提交
1801
        doAppendRowFromTSRow(pReader->pResBlock, pReader, pTSRow, uid);
1802
        taosMemoryFree(pTSRow);
1803 1804 1805 1806 1807
        return TSDB_CODE_SUCCESS;
      }

      //[7] key = ik.ts > k.ts
      if (key == ik.ts) {
1808
        doMergeMemTableMultiRows(piRow, uid, &pBlockScanInfo->iiter, pDelList, &pTSRow, pReader, &freeTSRow);
1809 1810 1811 1812 1813

        TSDBROW fRow = tsdbRowFromBlockData(pBlockData, pDumpInfo->rowIndex);
        tRowMerge(&merge, &fRow);
        doMergeRowsInFileBlocks(pBlockData, pBlockScanInfo, pReader, &merge);
        tRowMergerGetRow(&merge, &pTSRow);
H
Haojun Liao 已提交
1814
        doAppendRowFromTSRow(pReader->pResBlock, pReader, pTSRow, uid);
1815 1816

        taosMemoryFree(pTSRow);
1817 1818 1819 1820 1821 1822
        return TSDB_CODE_SUCCESS;
      }
    }
  }

  ASSERT(0);
S
Shengliang Guan 已提交
1823
  return -1;
1824
}
1825
#endif
1826

1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851
static int32_t initMemDataIterator(STableBlockScanInfo* pBlockScanInfo, STsdbReader* pReader) {
  if (pBlockScanInfo->iterInit) {
    return TSDB_CODE_SUCCESS;
  }

  int32_t code = TSDB_CODE_SUCCESS;

  TSDBKEY startKey = {0};
  if (ASCENDING_TRAVERSE(pReader->order)) {
    startKey = (TSDBKEY){.ts = pReader->window.skey, .version = pReader->verRange.minVer};
  } else {
    startKey = (TSDBKEY){.ts = pReader->window.ekey, .version = pReader->verRange.maxVer};
  }

  int32_t backward = (!ASCENDING_TRAVERSE(pReader->order));

  STbData* d = NULL;
  if (pReader->pReadSnap->pMem != NULL) {
    d = tsdbGetTbDataFromMemTable(pReader->pReadSnap->pMem, pReader->suid, pBlockScanInfo->uid);
    if (d != NULL) {
      code = tsdbTbDataIterCreate(d, &startKey, backward, &pBlockScanInfo->iter.iter);
      if (code == TSDB_CODE_SUCCESS) {
        pBlockScanInfo->iter.hasVal = (tsdbTbDataIterGet(pBlockScanInfo->iter.iter) != NULL);

        tsdbDebug("%p uid:%" PRId64 ", check data in mem from skey:%" PRId64 ", order:%d, ts range in buf:%" PRId64
H
Hongze Cheng 已提交
1852
                  "-%" PRId64 " %s",
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
                  pReader, pBlockScanInfo->uid, startKey.ts, pReader->order, d->minKey, d->maxKey, pReader->idStr);
      } else {
        tsdbError("%p uid:%" PRId64 ", failed to create iterator for imem, code:%s, %s", pReader, pBlockScanInfo->uid,
                  tstrerror(code), pReader->idStr);
        return code;
      }
    }
  } else {
    tsdbDebug("%p uid:%" PRId64 ", no data in mem, %s", pReader, pBlockScanInfo->uid, pReader->idStr);
  }

  STbData* di = NULL;
  if (pReader->pReadSnap->pIMem != NULL) {
    di = tsdbGetTbDataFromMemTable(pReader->pReadSnap->pIMem, pReader->suid, pBlockScanInfo->uid);
    if (di != NULL) {
      code = tsdbTbDataIterCreate(di, &startKey, backward, &pBlockScanInfo->iiter.iter);
      if (code == TSDB_CODE_SUCCESS) {
        pBlockScanInfo->iiter.hasVal = (tsdbTbDataIterGet(pBlockScanInfo->iiter.iter) != NULL);

        tsdbDebug("%p uid:%" PRId64 ", check data in imem from skey:%" PRId64 ", order:%d, ts range in buf:%" PRId64
H
Hongze Cheng 已提交
1873
                  "-%" PRId64 " %s",
1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890
                  pReader, pBlockScanInfo->uid, startKey.ts, pReader->order, di->minKey, di->maxKey, pReader->idStr);
      } else {
        tsdbError("%p uid:%" PRId64 ", failed to create iterator for mem, code:%s, %s", pReader, pBlockScanInfo->uid,
                  tstrerror(code), pReader->idStr);
        return code;
      }
    }
  } else {
    tsdbDebug("%p uid:%" PRId64 ", no data in imem, %s", pReader, pBlockScanInfo->uid, pReader->idStr);
  }

  initDelSkylineIterator(pBlockScanInfo, pReader, d, di);

  pBlockScanInfo->iterInit = true;
  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
1891 1892
static bool isValidFileBlockRow(SBlockData* pBlockData, SFileBlockDumpInfo* pDumpInfo,
                                STableBlockScanInfo* pBlockScanInfo, STsdbReader* pReader) {
1893 1894 1895 1896 1897 1898 1899 1900
  // it is an multi-table data block
  if (pBlockData->aUid != NULL) {
    uint64_t uid = pBlockData->aUid[pDumpInfo->rowIndex];
    if (uid != pBlockScanInfo->uid) {  // move to next row
      return false;
    }
  }

1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911
  // check for version and time range
  int64_t ver = pBlockData->aVersion[pDumpInfo->rowIndex];
  if (ver > pReader->verRange.maxVer || ver < pReader->verRange.minVer) {
    return false;
  }

  int64_t ts = pBlockData->aTSKEY[pDumpInfo->rowIndex];
  if (ts > pReader->window.ekey || ts < pReader->window.skey) {
    return false;
  }

1912
  TSDBKEY k = {.ts = ts, .version = ver};
1913
  if (hasBeenDropped(pBlockScanInfo->delSkyline, &pBlockScanInfo->fileDelIndex, &k, pReader->order)) {
1914 1915 1916
    return false;
  }

1917 1918 1919
  return true;
}

1920
static bool outOfTimeWindow(int64_t ts, STimeWindow* pWindow) { return (ts > pWindow->ekey) || (ts < pWindow->skey); }
1921

1922
static bool initLastBlockReader(SLastBlockReader* pLBlockReader, STableBlockScanInfo* pScanInfo, STsdbReader* pReader) {
1923
  // the last block reader has been initialized for this table.
1924
  if (pLBlockReader->uid == pScanInfo->uid) {
1925 1926 1927
    return true;
  }

1928 1929
  if (pLBlockReader->uid != 0) {
    tMergeTreeClose(&pLBlockReader->mergeTree);
1930 1931
  }

1932 1933
  initMemDataIterator(pScanInfo, pReader);
  pLBlockReader->uid = pScanInfo->uid;
1934

H
Hongze Cheng 已提交
1935
  int32_t     step = ASCENDING_TRAVERSE(pLBlockReader->order) ? 1 : -1;
1936 1937 1938
  STimeWindow w = pLBlockReader->window;
  if (ASCENDING_TRAVERSE(pLBlockReader->order)) {
    w.skey = pScanInfo->lastKey + step;
1939
  } else {
1940
    w.ekey = pScanInfo->lastKey + step;
1941 1942
  }

1943
  int32_t code =
1944
      tMergeTreeOpen(&pLBlockReader->mergeTree, (pLBlockReader->order == TSDB_ORDER_DESC), pReader->pFileReader,
H
Haojun Liao 已提交
1945
                     pReader->suid, pScanInfo->uid, &w, &pLBlockReader->verRange, pLBlockReader->pInfo);
1946 1947 1948 1949
  if (code != TSDB_CODE_SUCCESS) {
    return false;
  }

1950
  return nextRowFromLastBlocks(pLBlockReader, pScanInfo);
1951 1952
}

1953
static int64_t getCurrentKeyInLastBlock(SLastBlockReader* pLastBlockReader) {
1954
  TSDBROW row = tMergeTreeGetRow(&pLastBlockReader->mergeTree);
1955
  return TSDBROW_TS(&row);
1956 1957
}

H
Hongze Cheng 已提交
1958
static bool hasDataInLastBlock(SLastBlockReader* pLastBlockReader) { return pLastBlockReader->mergeTree.pIter != NULL; }
1959

1960 1961
int32_t mergeRowsInFileBlocks(SBlockData* pBlockData, STableBlockScanInfo* pBlockScanInfo, int64_t key,
                              STsdbReader* pReader) {
1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973
  SFileBlockDumpInfo* pDumpInfo = &pReader->status.fBlockDumpInfo;

  TSDBROW fRow = tsdbRowFromBlockData(pBlockData, pDumpInfo->rowIndex);

  if (tryCopyDistinctRowFromFileBlock(pReader, pBlockData, key, pDumpInfo)) {
    return TSDB_CODE_SUCCESS;
  } else {
    STSRow*    pTSRow = NULL;
    SRowMerger merge = {0};

    tRowMergerInit(&merge, &fRow, pReader->pSchema);
    doMergeRowsInFileBlocks(pBlockData, pBlockScanInfo, pReader, &merge);
1974 1975 1976 1977 1978
    int32_t code = tRowMergerGetRow(&merge, &pTSRow);
    if (code != TSDB_CODE_SUCCESS) {
      return code;
    }

1979 1980 1981 1982 1983 1984
    doAppendRowFromTSRow(pReader->pResBlock, pReader, pTSRow, pBlockScanInfo->uid);

    taosMemoryFree(pTSRow);
    tRowMergerClear(&merge);
    return TSDB_CODE_SUCCESS;
  }
1985

1986 1987 1988
  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
1989 1990
static int32_t buildComposedDataBlockImpl(STsdbReader* pReader, STableBlockScanInfo* pBlockScanInfo,
                                          SBlockData* pBlockData, SLastBlockReader* pLastBlockReader) {
1991 1992
  SFileBlockDumpInfo* pDumpInfo = &pReader->status.fBlockDumpInfo;

H
Hongze Cheng 已提交
1993
  int64_t  key = (pBlockData->nRow > 0) ? pBlockData->aTSKEY[pDumpInfo->rowIndex] : INT64_MIN;
1994 1995
  TSDBROW* pRow = getValidMemRow(&pBlockScanInfo->iter, pBlockScanInfo->delSkyline, pReader);
  TSDBROW* piRow = getValidMemRow(&pBlockScanInfo->iiter, pBlockScanInfo->delSkyline, pReader);
1996

1997
  if (pBlockScanInfo->iter.hasVal && pBlockScanInfo->iiter.hasVal) {
1998
    return doMergeMultiLevelRows(pReader, pBlockScanInfo, pBlockData, pLastBlockReader);
1999
  } else {
2000
    // imem + file + last block
2001
    if (pBlockScanInfo->iiter.hasVal) {
2002
      return doMergeBufAndFileRows(pReader, pBlockScanInfo, piRow, &pBlockScanInfo->iiter, key, pLastBlockReader);
2003 2004
    }

2005
    // mem + file + last block
2006
    if (pBlockScanInfo->iter.hasVal) {
2007
      return doMergeBufAndFileRows(pReader, pBlockScanInfo, pRow, &pBlockScanInfo->iter, key, pLastBlockReader);
H
Haojun Liao 已提交
2008
    }
2009

2010 2011
    // files data blocks + last block
    return mergeFileBlockAndLastBlock(pReader, pLastBlockReader, key, pBlockScanInfo, pBlockData);
2012 2013 2014
  }
}

2015
static int32_t buildComposedDataBlock(STsdbReader* pReader) {
2016 2017
  SSDataBlock* pResBlock = pReader->pResBlock;

H
Hongze Cheng 已提交
2018
  SFileDataBlockInfo* pBlockInfo = getCurrentBlockInfo(&pReader->status.blockIter);
2019 2020 2021 2022 2023 2024 2025 2026

  STableBlockScanInfo* pBlockScanInfo = NULL;
  if (pBlockInfo != NULL) {
    pBlockScanInfo = taosHashGet(pReader->status.pTableMap, &pBlockInfo->uid, sizeof(pBlockInfo->uid));
  } else {
    pBlockScanInfo = pReader->status.pTableIter;
  }

H
Haojun Liao 已提交
2027
  SLastBlockReader*   pLastBlockReader = pReader->status.fileIter.pLastBlockReader;
2028
  SFileBlockDumpInfo* pDumpInfo = &pReader->status.fBlockDumpInfo;
2029 2030
  SBlockData*         pBlockData = &pReader->status.fileBlockData;
  int32_t             step = ASCENDING_TRAVERSE(pReader->order) ? 1 : -1;
2031

2032 2033
  int64_t st = taosGetTimestampUs();

2034
  while (1) {
2035
    // todo check the validate of row in file block
2036
    bool hasBlockData = false;
2037
    {
H
Haojun Liao 已提交
2038
      while (pBlockData->nRow > 0) {  // find the first qualified row in data block
2039 2040 2041 2042 2043
        if (isValidFileBlockRow(pBlockData, pDumpInfo, pBlockScanInfo, pReader)) {
          hasBlockData = true;
          break;
        }

2044 2045
        pDumpInfo->rowIndex += step;

H
Hongze Cheng 已提交
2046
        SDataBlk* pBlock = getCurrentBlock(&pReader->status.blockIter);
2047
        if (pDumpInfo->rowIndex >= pBlock->nRow || pDumpInfo->rowIndex < 0) {
2048
          setBlockAllDumped(pDumpInfo, pBlock->maxKey.ts, pReader->order);
2049 2050 2051
          break;
        }
      }
2052
    }
2053

2054
    bool hasBlockLData = hasDataInLastBlock(pLastBlockReader);
2055

2056 2057 2058
    // no data in last block and block, no need to proceed.
    if ((hasBlockData == false) && (hasBlockLData == false)) {
      break;
2059 2060
    }

2061
    buildComposedDataBlockImpl(pReader, pBlockScanInfo, pBlockData, pLastBlockReader);
2062

2063
    // currently loaded file data block is consumed
2064
    if ((pBlockData->nRow > 0) && (pDumpInfo->rowIndex >= pBlockData->nRow || pDumpInfo->rowIndex < 0)) {
H
Hongze Cheng 已提交
2065
      SDataBlk* pBlock = getCurrentBlock(&pReader->status.blockIter);
2066
      setBlockAllDumped(pDumpInfo, pBlock->maxKey.ts, pReader->order);
2067 2068 2069 2070 2071
      break;
    }

    if (pResBlock->info.rows >= pReader->capacity) {
      break;
2072 2073 2074 2075
    }
  }

  pResBlock->info.uid = pBlockScanInfo->uid;
2076 2077
  blockDataUpdateTsWindow(pResBlock, 0);

2078
  setComposedBlockFlag(pReader, true);
2079
  int64_t et = taosGetTimestampUs();
2080

2081 2082 2083 2084 2085 2086
  if (pResBlock->info.rows > 0) {
    tsdbDebug("%p uid:%" PRIu64 ", composed data block created, brange:%" PRIu64 "-%" PRIu64
              " rows:%d, elapsed time:%.2f ms %s",
              pReader, pBlockScanInfo->uid, pResBlock->info.window.skey, pResBlock->info.window.ekey,
              pResBlock->info.rows, (et - st) / 1000.0, pReader->idStr);
  }
2087

2088 2089 2090 2091 2092
  return TSDB_CODE_SUCCESS;
}

void setComposedBlockFlag(STsdbReader* pReader, bool composed) { pReader->status.composedDataBlock = composed; }

dengyihao's avatar
dengyihao 已提交
2093 2094
int32_t initDelSkylineIterator(STableBlockScanInfo* pBlockScanInfo, STsdbReader* pReader, STbData* pMemTbData,
                               STbData* piMemTbData) {
2095 2096 2097
  if (pBlockScanInfo->delSkyline != NULL) {
    return TSDB_CODE_SUCCESS;
  }
2098

2099 2100 2101
  int32_t code = 0;
  STsdb*  pTsdb = pReader->pTsdb;

2102 2103
  SArray* pDelData = taosArrayInit(4, sizeof(SDelData));

H
Hongze Cheng 已提交
2104
  SDelFile* pDelFile = pReader->pReadSnap->fs.pDelFile;
2105 2106
  if (pDelFile) {
    SDelFReader* pDelFReader = NULL;
H
more  
Hongze Cheng 已提交
2107
    code = tsdbDelFReaderOpen(&pDelFReader, pDelFile, pTsdb);
2108
    if (code != TSDB_CODE_SUCCESS) {
2109 2110 2111 2112 2113
      goto _err;
    }

    SArray* aDelIdx = taosArrayInit(4, sizeof(SDelIdx));
    if (aDelIdx == NULL) {
2114
      tsdbDelFReaderClose(&pDelFReader);
2115 2116 2117
      goto _err;
    }

H
Hongze Cheng 已提交
2118
    code = tsdbReadDelIdx(pDelFReader, aDelIdx);
2119 2120 2121
    if (code != TSDB_CODE_SUCCESS) {
      taosArrayDestroy(aDelIdx);
      tsdbDelFReaderClose(&pDelFReader);
2122 2123
      goto _err;
    }
2124

2125 2126 2127
    SDelIdx  idx = {.suid = pReader->suid, .uid = pBlockScanInfo->uid};
    SDelIdx* pIdx = taosArraySearch(aDelIdx, &idx, tCmprDelIdx, TD_EQ);

H
Haojun Liao 已提交
2128
    if (pIdx != NULL) {
H
Hongze Cheng 已提交
2129
      code = tsdbReadDelData(pDelFReader, pIdx, pDelData);
2130 2131 2132 2133 2134 2135 2136
    }

    taosArrayDestroy(aDelIdx);
    tsdbDelFReaderClose(&pDelFReader);

    if (code != TSDB_CODE_SUCCESS) {
      goto _err;
2137
    }
2138
  }
2139

2140 2141 2142 2143 2144 2145 2146
  SDelData* p = NULL;
  if (pMemTbData != NULL) {
    p = pMemTbData->pHead;
    while (p) {
      taosArrayPush(pDelData, p);
      p = p->pNext;
    }
2147 2148
  }

2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162
  if (piMemTbData != NULL) {
    p = piMemTbData->pHead;
    while (p) {
      taosArrayPush(pDelData, p);
      p = p->pNext;
    }
  }

  if (taosArrayGetSize(pDelData) > 0) {
    pBlockScanInfo->delSkyline = taosArrayInit(4, sizeof(TSDBKEY));
    code = tsdbBuildDeleteSkyline(pDelData, 0, (int32_t)(taosArrayGetSize(pDelData) - 1), pBlockScanInfo->delSkyline);
  }

  taosArrayDestroy(pDelData);
dengyihao's avatar
dengyihao 已提交
2163 2164
  pBlockScanInfo->iter.index =
      ASCENDING_TRAVERSE(pReader->order) ? 0 : taosArrayGetSize(pBlockScanInfo->delSkyline) - 1;
2165 2166
  pBlockScanInfo->iiter.index = pBlockScanInfo->iter.index;
  pBlockScanInfo->fileDelIndex = pBlockScanInfo->iter.index;
2167
  pBlockScanInfo->lastBlockDelIndex = pBlockScanInfo->iter.index;
2168 2169
  return code;

2170 2171 2172
_err:
  taosArrayDestroy(pDelData);
  return code;
2173 2174
}

2175
static TSDBKEY getCurrentKeyInBuf(STableBlockScanInfo* pScanInfo, STsdbReader* pReader) {
H
Hongze Cheng 已提交
2176
  TSDBKEY  key = {.ts = TSKEY_INITIAL_VAL};
2177
  TSDBROW* pRow = getValidMemRow(&pScanInfo->iter, pScanInfo->delSkyline, pReader);
2178
  if (pRow != NULL) {
2179 2180 2181
    key = TSDBROW_KEY(pRow);
  }

2182
  pRow = getValidMemRow(&pScanInfo->iiter, pScanInfo->delSkyline, pReader);
2183
  if (pRow != NULL) {
2184 2185 2186 2187 2188 2189 2190 2191 2192
    TSDBKEY k = TSDBROW_KEY(pRow);
    if (key.ts > k.ts) {
      key = k;
    }
  }

  return key;
}

2193
static int32_t moveToNextFile(STsdbReader* pReader, SBlockNumber* pBlockNum) {
H
Haojun Liao 已提交
2194
  SReaderStatus* pStatus = &pReader->status;
2195
  pBlockNum->numOfBlocks = 0;
2196
  pBlockNum->numOfLastFiles = 0;
2197

2198
  size_t  numOfTables = taosHashGetSize(pReader->status.pTableMap);
2199
  SArray* pIndexList = taosArrayInit(numOfTables, sizeof(SBlockIdx));
H
Haojun Liao 已提交
2200 2201

  while (1) {
2202
    bool hasNext = filesetIteratorNext(&pStatus->fileIter, pReader);
2203
    if (!hasNext) {  // no data files on disk
H
Haojun Liao 已提交
2204 2205 2206
      break;
    }

H
Haojun Liao 已提交
2207
    taosArrayClear(pIndexList);
H
Haojun Liao 已提交
2208 2209
    int32_t code = doLoadBlockIndex(pReader, pReader->pFileReader, pIndexList);
    if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
2210
      taosArrayDestroy(pIndexList);
H
Haojun Liao 已提交
2211 2212 2213
      return code;
    }

H
Hongze Cheng 已提交
2214
    if (taosArrayGetSize(pIndexList) > 0 || pReader->pFileReader->pSet->nSttF > 0) {
2215
      code = doLoadFileBlock(pReader, pIndexList, pBlockNum);
H
Haojun Liao 已提交
2216
      if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
2217
        taosArrayDestroy(pIndexList);
H
Haojun Liao 已提交
2218 2219 2220
        return code;
      }

2221
      if (pBlockNum->numOfBlocks + pBlockNum->numOfLastFiles > 0) {
H
Haojun Liao 已提交
2222 2223 2224
        break;
      }
    }
2225

H
Haojun Liao 已提交
2226 2227 2228
    // no blocks in current file, try next files
  }

H
Haojun Liao 已提交
2229
  taosArrayDestroy(pIndexList);
H
Haojun Liao 已提交
2230 2231 2232
  return TSDB_CODE_SUCCESS;
}

2233
static int32_t uidComparFunc(const void* p1, const void* p2) {
2234 2235
  uint64_t pu1 = *(uint64_t*)p1;
  uint64_t pu2 = *(uint64_t*)p2;
2236 2237 2238
  if (pu1 == pu2) {
    return 0;
  } else {
2239
    return (pu1 < pu2) ? -1 : 1;
2240 2241
  }
}
2242

2243
static void extractOrderedTableUidList(SUidOrderCheckInfo* pOrderCheckInfo, SReaderStatus* pStatus) {
2244 2245 2246 2247
  int32_t index = 0;
  int32_t total = taosHashGetSize(pStatus->pTableMap);

  void* p = taosHashIterate(pStatus->pTableMap, NULL);
2248
  while (p != NULL) {
2249 2250 2251 2252 2253 2254 2255 2256
    STableBlockScanInfo* pScanInfo = p;
    pOrderCheckInfo->tableUidList[index++] = pScanInfo->uid;
    p = taosHashIterate(pStatus->pTableMap, p);
  }

  taosSort(pOrderCheckInfo->tableUidList, total, sizeof(uint64_t), uidComparFunc);
}

2257
static int32_t initOrderCheckInfo(SUidOrderCheckInfo* pOrderCheckInfo, SReaderStatus* pStatus) {
2258 2259 2260 2261
  int32_t total = taosHashGetSize(pStatus->pTableMap);
  if (total == 0) {
    return TSDB_CODE_SUCCESS;
  }
2262

2263
  if (pOrderCheckInfo->tableUidList == NULL) {
2264 2265 2266 2267 2268 2269
    pOrderCheckInfo->currentIndex = 0;
    pOrderCheckInfo->tableUidList = taosMemoryMalloc(total * sizeof(uint64_t));
    if (pOrderCheckInfo->tableUidList == NULL) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }

2270
    extractOrderedTableUidList(pOrderCheckInfo, pStatus);
2271 2272 2273
    uint64_t uid = pOrderCheckInfo->tableUidList[0];
    pStatus->pTableIter = taosHashGet(pStatus->pTableMap, &uid, sizeof(uid));
  } else {
2274 2275
    if (pStatus->pTableIter == NULL) {  // it is the last block of a new file
      pOrderCheckInfo->currentIndex = 0;
2276 2277
      uint64_t uid = pOrderCheckInfo->tableUidList[pOrderCheckInfo->currentIndex];
      pStatus->pTableIter = taosHashGet(pStatus->pTableMap, &uid, sizeof(uid));
2278 2279

      // the tableMap has already updated
2280
      if (pStatus->pTableIter == NULL) {
2281
        void* p = taosMemoryRealloc(pOrderCheckInfo->tableUidList, total * sizeof(uint64_t));
2282 2283 2284 2285 2286 2287 2288 2289 2290
        if (p == NULL) {
          return TSDB_CODE_OUT_OF_MEMORY;
        }

        pOrderCheckInfo->tableUidList = p;
        extractOrderedTableUidList(pOrderCheckInfo, pStatus);

        uid = pOrderCheckInfo->tableUidList[0];
        pStatus->pTableIter = taosHashGet(pStatus->pTableMap, &uid, sizeof(uid));
2291
      }
2292
    }
2293
  }
2294

2295 2296 2297
  return TSDB_CODE_SUCCESS;
}

2298
static bool moveToNextTable(SUidOrderCheckInfo* pOrderedCheckInfo, SReaderStatus* pStatus) {
2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310
  pOrderedCheckInfo->currentIndex += 1;
  if (pOrderedCheckInfo->currentIndex >= taosHashGetSize(pStatus->pTableMap)) {
    pStatus->pTableIter = NULL;
    return false;
  }

  uint64_t uid = pOrderedCheckInfo->tableUidList[pOrderedCheckInfo->currentIndex];
  pStatus->pTableIter = taosHashGet(pStatus->pTableMap, &uid, sizeof(uid));
  ASSERT(pStatus->pTableIter != NULL);
  return true;
}

2311
static int32_t doLoadLastBlockSequentially(STsdbReader* pReader) {
2312
  SReaderStatus*    pStatus = &pReader->status;
2313 2314
  SLastBlockReader* pLastBlockReader = pStatus->fileIter.pLastBlockReader;

2315 2316
  SUidOrderCheckInfo* pOrderedCheckInfo = &pStatus->uidCheckInfo;
  int32_t             code = initOrderCheckInfo(pOrderedCheckInfo, pStatus);
2317
  if (code != TSDB_CODE_SUCCESS || (taosHashGetSize(pStatus->pTableMap) == 0)) {
2318 2319
    return code;
  }
2320

2321
  while (1) {
2322 2323
    // load the last data block of current table
    STableBlockScanInfo* pScanInfo = pStatus->pTableIter;
H
Hongze Cheng 已提交
2324
    bool                 hasVal = initLastBlockReader(pLastBlockReader, pScanInfo, pReader);
2325
    if (!hasVal) {
2326 2327
      bool hasNexTable = moveToNextTable(pOrderedCheckInfo, pStatus);
      if (!hasNexTable) {
2328 2329 2330
        return TSDB_CODE_SUCCESS;
      }
      continue;
2331 2332
    }

2333 2334 2335 2336 2337 2338 2339 2340
    code = doBuildDataBlock(pReader);
    if (code != TSDB_CODE_SUCCESS) {
      return code;
    }

    if (pReader->pResBlock->info.rows > 0) {
      return TSDB_CODE_SUCCESS;
    }
2341

2342
    // current table is exhausted, let's try next table
2343 2344
    bool hasNexTable = moveToNextTable(pOrderedCheckInfo, pStatus);
    if (!hasNexTable) {
2345 2346
      return TSDB_CODE_SUCCESS;
    }
2347 2348 2349
  }
}

2350
static int32_t doBuildDataBlock(STsdbReader* pReader) {
H
Hongze Cheng 已提交
2351 2352
  int32_t   code = TSDB_CODE_SUCCESS;
  SDataBlk* pBlock = NULL;
2353 2354 2355

  SReaderStatus*       pStatus = &pReader->status;
  SDataBlockIter*      pBlockIter = &pStatus->blockIter;
2356 2357 2358
  STableBlockScanInfo* pScanInfo = NULL;
  SFileDataBlockInfo*  pBlockInfo = getCurrentBlockInfo(pBlockIter);
  SLastBlockReader*    pLastBlockReader = pReader->status.fileIter.pLastBlockReader;
2359

2360
  if (pBlockInfo != NULL) {
2361 2362 2363 2364 2365 2366
    pScanInfo = taosHashGet(pReader->status.pTableMap, &pBlockInfo->uid, sizeof(pBlockInfo->uid));
  } else {
    pScanInfo = pReader->status.pTableIter;
  }

  if (pBlockInfo != NULL) {
2367
    pBlock = getCurrentBlock(pBlockIter);
2368 2369
  }

2370
  initLastBlockReader(pLastBlockReader, pScanInfo, pReader);
2371
  TSDBKEY key = getCurrentKeyInBuf(pScanInfo, pReader);
2372

2373 2374 2375 2376 2377 2378 2379
  if (pBlockInfo == NULL) {  // build data block from last data file
    ASSERT(pBlockIter->numOfBlocks == 0);
    code = buildComposedDataBlock(pReader);
  } else if (fileBlockShouldLoad(pReader, pBlockInfo, pBlock, pScanInfo, key, pLastBlockReader)) {
    tBlockDataReset(&pStatus->fileBlockData);
    code = tBlockDataInit(&pStatus->fileBlockData, pReader->suid, pScanInfo->uid, pReader->pSchema);
    if (code != TSDB_CODE_SUCCESS) {
2380
      return code;
2381
    }
2382

2383 2384 2385
    code = doLoadFileBlockData(pReader, pBlockIter, &pStatus->fileBlockData);
    if (code != TSDB_CODE_SUCCESS) {
      return code;
2386 2387 2388
    }

    // build composed data block
2389
    code = buildComposedDataBlock(pReader);
2390 2391
  } else if (bufferDataInFileBlockGap(pReader->order, key, pBlock)) {
    // data in memory that are earlier than current file block
2392
    // todo rows in buffer should be less than the file block in asc, greater than file block in desc
2393
    int64_t endKey = (ASCENDING_TRAVERSE(pReader->order)) ? pBlock->minKey.ts : pBlock->maxKey.ts;
2394
    code = buildDataBlockFromBuf(pReader, pScanInfo, endKey);
2395 2396 2397 2398
  } else {
    if (hasDataInLastBlock(pLastBlockReader) && !ASCENDING_TRAVERSE(pReader->order)) {
      // only return the rows in last block
      int64_t tsLast = getCurrentKeyInLastBlock(pLastBlockReader);
H
Hongze Cheng 已提交
2399
      ASSERT(tsLast >= pBlock->maxKey.ts);
2400 2401 2402
      tBlockDataReset(&pReader->status.fileBlockData);

      code = buildComposedDataBlock(pReader);
H
Hongze Cheng 已提交
2403
    } else {  // whole block is required, return it directly
2404 2405 2406 2407 2408 2409 2410
      SDataBlockInfo* pInfo = &pReader->pResBlock->info;
      pInfo->rows = pBlock->nRow;
      pInfo->uid = pScanInfo->uid;
      pInfo->window = (STimeWindow){.skey = pBlock->minKey.ts, .ekey = pBlock->maxKey.ts};
      setComposedBlockFlag(pReader, false);
      setBlockAllDumped(&pStatus->fBlockDumpInfo, pBlock->maxKey.ts, pReader->order);
    }
2411 2412 2413 2414 2415
  }

  return code;
}

H
Haojun Liao 已提交
2416
static int32_t buildBlockFromBufferSequentially(STsdbReader* pReader) {
2417 2418
  SReaderStatus* pStatus = &pReader->status;

2419
  while (1) {
2420 2421 2422
    if (pStatus->pTableIter == NULL) {
      pStatus->pTableIter = taosHashIterate(pStatus->pTableMap, NULL);
      if (pStatus->pTableIter == NULL) {
H
Haojun Liao 已提交
2423
        return TSDB_CODE_SUCCESS;
2424 2425 2426 2427
      }
    }

    STableBlockScanInfo* pBlockScanInfo = pStatus->pTableIter;
2428
    initMemDataIterator(pBlockScanInfo, pReader);
2429

2430
    int64_t endKey = (ASCENDING_TRAVERSE(pReader->order)) ? INT64_MAX : INT64_MIN;
2431
    int32_t code = buildDataBlockFromBuf(pReader, pBlockScanInfo, endKey);
H
Haojun Liao 已提交
2432 2433 2434 2435
    if (code != TSDB_CODE_SUCCESS) {
      return code;
    }

2436
    if (pReader->pResBlock->info.rows > 0) {
H
Haojun Liao 已提交
2437
      return TSDB_CODE_SUCCESS;
2438 2439 2440 2441 2442
    }

    // current table is exhausted, let's try the next table
    pStatus->pTableIter = taosHashIterate(pStatus->pTableMap, pStatus->pTableIter);
    if (pStatus->pTableIter == NULL) {
H
Haojun Liao 已提交
2443
      return TSDB_CODE_SUCCESS;
2444 2445 2446 2447
    }
  }
}

2448
// set the correct start position in case of the first/last file block, according to the query time window
2449
static void initBlockDumpInfo(STsdbReader* pReader, SDataBlockIter* pBlockIter) {
H
Hongze Cheng 已提交
2450
  SDataBlk* pBlock = getCurrentBlock(pBlockIter);
2451

2452 2453 2454
  SReaderStatus* pStatus = &pReader->status;

  SFileBlockDumpInfo* pDumpInfo = &pStatus->fBlockDumpInfo;
2455 2456 2457

  pDumpInfo->totalRows = pBlock->nRow;
  pDumpInfo->allDumped = false;
2458
  pDumpInfo->rowIndex = ASCENDING_TRAVERSE(pReader->order) ? 0 : pBlock->nRow - 1;
2459 2460
}

2461
static int32_t initForFirstBlockInFile(STsdbReader* pReader, SDataBlockIter* pBlockIter) {
2462 2463
  SBlockNumber num = {0};

2464
  int32_t code = moveToNextFile(pReader, &num);
2465 2466 2467 2468 2469
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }

  // all data files are consumed, try data in buffer
2470
  if (num.numOfBlocks + num.numOfLastFiles == 0) {
2471 2472 2473 2474 2475
    pReader->status.loadFromFile = false;
    return code;
  }

  // initialize the block iterator for a new fileset
2476 2477
  if (num.numOfBlocks > 0) {
    code = initBlockIterator(pReader, pBlockIter, num.numOfBlocks);
H
Hongze Cheng 已提交
2478
  } else {  // no block data, only last block exists
2479
    tBlockDataReset(&pReader->status.fileBlockData);
2480
    resetDataBlockIterator(pBlockIter, pReader->order);
2481
  }
2482 2483

  // set the correct start position according to the query time window
2484
  initBlockDumpInfo(pReader, pBlockIter);
2485 2486 2487
  return code;
}

2488
static bool fileBlockPartiallyRead(SFileBlockDumpInfo* pDumpInfo, bool asc) {
2489 2490
  return (!pDumpInfo->allDumped) &&
         ((pDumpInfo->rowIndex > 0 && asc) || (pDumpInfo->rowIndex < (pDumpInfo->totalRows - 1) && (!asc)));
2491 2492
}

2493
static int32_t buildBlockFromFiles(STsdbReader* pReader) {
H
Haojun Liao 已提交
2494
  int32_t code = TSDB_CODE_SUCCESS;
2495 2496
  bool    asc = ASCENDING_TRAVERSE(pReader->order);

2497 2498
  SDataBlockIter* pBlockIter = &pReader->status.blockIter;

2499
  if (pBlockIter->numOfBlocks == 0) {
H
Hongze Cheng 已提交
2500
  _begin:
2501 2502 2503 2504 2505
    code = doLoadLastBlockSequentially(pReader);
    if (code != TSDB_CODE_SUCCESS) {
      return code;
    }

2506 2507 2508 2509
    if (pReader->pResBlock->info.rows > 0) {
      return TSDB_CODE_SUCCESS;
    }

2510
    // all data blocks are checked in this last block file, now let's try the next file
2511 2512 2513 2514 2515 2516 2517 2518
    if (pReader->status.pTableIter == NULL) {
      code = initForFirstBlockInFile(pReader, pBlockIter);

      // error happens or all the data files are completely checked
      if ((code != TSDB_CODE_SUCCESS) || (pReader->status.loadFromFile == false)) {
        return code;
      }

2519
      // this file does not have data files, let's start check the last block file if exists
2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534
      if (pBlockIter->numOfBlocks == 0) {
        goto _begin;
      }
    }

    code = doBuildDataBlock(pReader);
    if (code != TSDB_CODE_SUCCESS) {
      return code;
    }

    if (pReader->pResBlock->info.rows > 0) {
      return TSDB_CODE_SUCCESS;
    }
  }

2535
  while (1) {
2536 2537
    SFileBlockDumpInfo* pDumpInfo = &pReader->status.fBlockDumpInfo;

2538
    if (fileBlockPartiallyRead(pDumpInfo, asc)) {  // file data block is partially loaded
2539
      code = buildComposedDataBlock(pReader);
2540 2541 2542 2543 2544 2545 2546
    } else {
      // current block are exhausted, try the next file block
      if (pDumpInfo->allDumped) {
        // try next data block in current file
        bool hasNext = blockIteratorNext(&pReader->status.blockIter);
        if (hasNext) {  // check for the next block in the block accessed order list
          initBlockDumpInfo(pReader, pBlockIter);
2547
        } else {
H
Haojun Liao 已提交
2548
          if (pReader->status.pCurrentFileset->nSttF > 0) {
2549 2550 2551 2552 2553 2554
            // data blocks in current file are exhausted, let's try the next file now
            tBlockDataReset(&pReader->status.fileBlockData);
            resetDataBlockIterator(pBlockIter, pReader->order);
            goto _begin;
          } else {
            code = initForFirstBlockInFile(pReader, pBlockIter);
2555

2556 2557 2558 2559
            // error happens or all the data files are completely checked
            if ((code != TSDB_CODE_SUCCESS) || (pReader->status.loadFromFile == false)) {
              return code;
            }
2560

2561 2562 2563 2564
            // this file does not have blocks, let's start check the last block file
            if (pBlockIter->numOfBlocks == 0) {
              goto _begin;
            }
2565
          }
2566
        }
H
Haojun Liao 已提交
2567
      }
2568 2569

      code = doBuildDataBlock(pReader);
2570 2571
    }

2572 2573 2574 2575 2576 2577 2578 2579
    if (code != TSDB_CODE_SUCCESS) {
      return code;
    }

    if (pReader->pResBlock->info.rows > 0) {
      return TSDB_CODE_SUCCESS;
    }
  }
2580
}
H
refact  
Hongze Cheng 已提交
2581

2582 2583
static STsdb* getTsdbByRetentions(SVnode* pVnode, TSKEY winSKey, SRetention* retentions, const char* idStr,
                                  int8_t* pLevel) {
2584
  if (VND_IS_RSMA(pVnode)) {
2585
    int8_t  level = 0;
2586 2587
    int64_t now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);

2588
    for (int8_t i = 0; i < TSDB_RETENTION_MAX; ++i) {
2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601
      SRetention* pRetention = retentions + level;
      if (pRetention->keep <= 0) {
        if (level > 0) {
          --level;
        }
        break;
      }
      if ((now - pRetention->keep) <= winSKey) {
        break;
      }
      ++level;
    }

2602
    const char* str = (idStr != NULL) ? idStr : "";
2603 2604

    if (level == TSDB_RETENTION_L0) {
2605
      *pLevel = TSDB_RETENTION_L0;
C
Cary Xu 已提交
2606
      tsdbDebug("vgId:%d, rsma level %d is selected to query %s", TD_VID(pVnode), TSDB_RETENTION_L0, str);
2607 2608
      return VND_RSMA0(pVnode);
    } else if (level == TSDB_RETENTION_L1) {
2609
      *pLevel = TSDB_RETENTION_L1;
C
Cary Xu 已提交
2610
      tsdbDebug("vgId:%d, rsma level %d is selected to query %s", TD_VID(pVnode), TSDB_RETENTION_L1, str);
2611 2612
      return VND_RSMA1(pVnode);
    } else {
2613
      *pLevel = TSDB_RETENTION_L2;
C
Cary Xu 已提交
2614
      tsdbDebug("vgId:%d, rsma level %d is selected to query %s", TD_VID(pVnode), TSDB_RETENTION_L2, str);
2615 2616 2617 2618 2619 2620 2621
      return VND_RSMA2(pVnode);
    }
  }

  return VND_TSDB(pVnode);
}

H
Haojun Liao 已提交
2622
SVersionRange getQueryVerRange(SVnode* pVnode, SQueryTableDataCond* pCond, int8_t level) {
L
Liu Jicong 已提交
2623
  int64_t startVer = (pCond->startVersion == -1) ? 0 : pCond->startVersion;
H
Haojun Liao 已提交
2624 2625

  int64_t endVer = 0;
L
Liu Jicong 已提交
2626 2627
  if (pCond->endVersion ==
      -1) {  // user not specified end version, set current maximum version of vnode as the endVersion
H
Haojun Liao 已提交
2628 2629
    endVer = pVnode->state.applied;
  } else {
L
Liu Jicong 已提交
2630
    endVer = (pCond->endVersion > pVnode->state.applied) ? pVnode->state.applied : pCond->endVersion;
2631 2632
  }

H
Haojun Liao 已提交
2633
  return (SVersionRange){.minVer = startVer, .maxVer = endVer};
2634 2635
}

2636
bool hasBeenDropped(const SArray* pDelList, int32_t* index, TSDBKEY* pKey, int32_t order) {
2637 2638 2639 2640
  ASSERT(pKey != NULL);
  if (pDelList == NULL) {
    return false;
  }
L
Liu Jicong 已提交
2641 2642 2643
  size_t  num = taosArrayGetSize(pDelList);
  bool    asc = ASCENDING_TRAVERSE(order);
  int32_t step = asc ? 1 : -1;
2644

2645 2646 2647 2648 2649 2650
  if (asc) {
    if (*index >= num - 1) {
      TSDBKEY* last = taosArrayGetLast(pDelList);
      ASSERT(pKey->ts >= last->ts);

      if (pKey->ts > last->ts) {
2651
        return false;
2652 2653 2654
      } else if (pKey->ts == last->ts) {
        TSDBKEY* prev = taosArrayGet(pDelList, num - 2);
        return (prev->version >= pKey->version);
2655 2656
      }
    } else {
2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686
      TSDBKEY* pCurrent = taosArrayGet(pDelList, *index);
      TSDBKEY* pNext = taosArrayGet(pDelList, (*index) + 1);

      if (pKey->ts < pCurrent->ts) {
        return false;
      }

      if (pCurrent->ts <= pKey->ts && pNext->ts >= pKey->ts && pCurrent->version >= pKey->version) {
        return true;
      }

      while (pNext->ts <= pKey->ts && (*index) < num - 1) {
        (*index) += 1;

        if ((*index) < num - 1) {
          pCurrent = taosArrayGet(pDelList, *index);
          pNext = taosArrayGet(pDelList, (*index) + 1);

          // it is not a consecutive deletion range, ignore it
          if (pCurrent->version == 0 && pNext->version > 0) {
            continue;
          }

          if (pCurrent->ts <= pKey->ts && pNext->ts >= pKey->ts && pCurrent->version >= pKey->version) {
            return true;
          }
        }
      }

      return false;
2687 2688
    }
  } else {
2689 2690
    if (*index <= 0) {
      TSDBKEY* pFirst = taosArrayGet(pDelList, 0);
2691

2692 2693 2694 2695 2696 2697 2698
      if (pKey->ts < pFirst->ts) {
        return false;
      } else if (pKey->ts == pFirst->ts) {
        return pFirst->version >= pKey->version;
      } else {
        ASSERT(0);
      }
2699
    } else {
2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726
      TSDBKEY* pCurrent = taosArrayGet(pDelList, *index);
      TSDBKEY* pPrev = taosArrayGet(pDelList, (*index) - 1);

      if (pKey->ts > pCurrent->ts) {
        return false;
      }

      if (pPrev->ts <= pKey->ts && pCurrent->ts >= pKey->ts && pPrev->version >= pKey->version) {
        return true;
      }

      while (pPrev->ts >= pKey->ts && (*index) > 1) {
        (*index) += step;

        if ((*index) >= 1) {
          pCurrent = taosArrayGet(pDelList, *index);
          pPrev = taosArrayGet(pDelList, (*index) - 1);

          // it is not a consecutive deletion range, ignore it
          if (pCurrent->version > 0 && pPrev->version == 0) {
            continue;
          }

          if (pPrev->ts <= pKey->ts && pCurrent->ts >= pKey->ts && pPrev->version >= pKey->version) {
            return true;
          }
        }
2727 2728 2729 2730 2731
      }

      return false;
    }
  }
2732 2733

  return false;
2734 2735
}

2736
TSDBROW* getValidMemRow(SIterInfo* pIter, const SArray* pDelList, STsdbReader* pReader) {
2737
  if (!pIter->hasVal) {
H
Haojun Liao 已提交
2738 2739
    return NULL;
  }
H
Hongze Cheng 已提交
2740

2741
  TSDBROW* pRow = tsdbTbDataIterGet(pIter->iter);
2742
  TSDBKEY  key = {.ts = pRow->pTSRow->ts, .version = pRow->version};
2743
  if (outOfTimeWindow(key.ts, &pReader->window)) {
2744
    pIter->hasVal = false;
H
Haojun Liao 已提交
2745 2746
    return NULL;
  }
H
Hongze Cheng 已提交
2747

2748
  // it is a valid data version
dengyihao's avatar
dengyihao 已提交
2749
  if ((key.version <= pReader->verRange.maxVer && key.version >= pReader->verRange.minVer) &&
2750
      (!hasBeenDropped(pDelList, &pIter->index, &key, pReader->order))) {
H
Haojun Liao 已提交
2751 2752
    return pRow;
  }
H
Hongze Cheng 已提交
2753

2754
  while (1) {
2755 2756
    pIter->hasVal = tsdbTbDataIterNext(pIter->iter);
    if (!pIter->hasVal) {
H
Haojun Liao 已提交
2757 2758
      return NULL;
    }
H
Hongze Cheng 已提交
2759

2760
    pRow = tsdbTbDataIterGet(pIter->iter);
H
Hongze Cheng 已提交
2761

H
Haojun Liao 已提交
2762
    key = TSDBROW_KEY(pRow);
2763
    if (outOfTimeWindow(key.ts, &pReader->window)) {
2764
      pIter->hasVal = false;
H
Haojun Liao 已提交
2765 2766
      return NULL;
    }
H
Hongze Cheng 已提交
2767

dengyihao's avatar
dengyihao 已提交
2768
    if (key.version <= pReader->verRange.maxVer && key.version >= pReader->verRange.minVer &&
2769
        (!hasBeenDropped(pDelList, &pIter->index, &key, pReader->order))) {
H
Haojun Liao 已提交
2770 2771 2772 2773
      return pRow;
    }
  }
}
H
Hongze Cheng 已提交
2774

2775 2776
int32_t doMergeRowsInBuf(SIterInfo* pIter, uint64_t uid, int64_t ts, SArray* pDelList, SRowMerger* pMerger,
                         STsdbReader* pReader) {
H
Haojun Liao 已提交
2777
  while (1) {
2778 2779
    pIter->hasVal = tsdbTbDataIterNext(pIter->iter);
    if (!pIter->hasVal) {
H
Haojun Liao 已提交
2780 2781
      break;
    }
H
Hongze Cheng 已提交
2782

2783
    // data exists but not valid
2784
    TSDBROW* pRow = getValidMemRow(pIter, pDelList, pReader);
2785 2786 2787 2788 2789
    if (pRow == NULL) {
      break;
    }

    // ts is not identical, quit
H
Haojun Liao 已提交
2790
    TSDBKEY k = TSDBROW_KEY(pRow);
2791
    if (k.ts != ts) {
H
Haojun Liao 已提交
2792 2793 2794
      break;
    }

H
Haojun Liao 已提交
2795
    STSchema* pTSchema = doGetSchemaForTSRow(TSDBROW_SVERSION(pRow), pReader, uid);
2796
    tRowMergerAdd(pMerger, pRow, pTSchema);
H
Haojun Liao 已提交
2797 2798 2799 2800 2801
  }

  return TSDB_CODE_SUCCESS;
}

2802
static int32_t doMergeRowsInFileBlockImpl(SBlockData* pBlockData, int32_t rowIndex, int64_t key, SRowMerger* pMerger,
2803
                                          SVersionRange* pVerRange, int32_t step) {
2804 2805
  while (pBlockData->aTSKEY[rowIndex] == key && rowIndex < pBlockData->nRow && rowIndex >= 0) {
    if (pBlockData->aVersion[rowIndex] > pVerRange->maxVer || pBlockData->aVersion[rowIndex] < pVerRange->minVer) {
2806
      rowIndex += step;
2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822
      continue;
    }

    TSDBROW fRow = tsdbRowFromBlockData(pBlockData, rowIndex);
    tRowMerge(pMerger, &fRow);
    rowIndex += step;
  }

  return rowIndex;
}

typedef enum {
  CHECK_FILEBLOCK_CONT = 0x1,
  CHECK_FILEBLOCK_QUIT = 0x2,
} CHECK_FILEBLOCK_STATE;

H
Hongze Cheng 已提交
2823
static int32_t checkForNeighborFileBlock(STsdbReader* pReader, STableBlockScanInfo* pScanInfo, SDataBlk* pBlock,
2824 2825
                                         SFileDataBlockInfo* pFBlock, SRowMerger* pMerger, int64_t key,
                                         CHECK_FILEBLOCK_STATE* state) {
2826
  SFileBlockDumpInfo* pDumpInfo = &pReader->status.fBlockDumpInfo;
2827
  SBlockData*         pBlockData = &pReader->status.fileBlockData;
2828

2829
  *state = CHECK_FILEBLOCK_QUIT;
2830
  int32_t step = ASCENDING_TRAVERSE(pReader->order) ? 1 : -1;
2831

H
Hongze Cheng 已提交
2832 2833
  int32_t   nextIndex = -1;
  SDataBlk* pNeighborBlock = getNeighborBlockOfSameTable(pFBlock, pScanInfo, &nextIndex, pReader->order);
2834
  if (pNeighborBlock == NULL) {  // do nothing
2835 2836 2837 2838
    return 0;
  }

  bool overlap = overlapWithNeighborBlock(pBlock, pNeighborBlock, pReader->order);
2839 2840
  taosMemoryFree(pNeighborBlock);

2841
  if (overlap) {  // load next block
2842
    SReaderStatus*  pStatus = &pReader->status;
2843 2844
    SDataBlockIter* pBlockIter = &pStatus->blockIter;

2845
    // 1. find the next neighbor block in the scan block list
2846
    SFileDataBlockInfo fb = {.uid = pFBlock->uid, .tbBlockIdx = nextIndex};
2847
    int32_t            neighborIndex = findFileBlockInfoIndex(pBlockIter, &fb);
2848

2849
    // 2. remove it from the scan block list
2850
    setFileBlockActiveInBlockIter(pBlockIter, neighborIndex, step);
2851

2852
    // 3. load the neighbor block, and set it to be the currently accessed file data block
H
Haojun Liao 已提交
2853
    tBlockDataReset(&pStatus->fileBlockData);
2854 2855 2856 2857 2858 2859
    int32_t code = tBlockDataInit(&pStatus->fileBlockData, pReader->suid, pFBlock->uid, pReader->pSchema);
    if (code != TSDB_CODE_SUCCESS) {
      return code;
    }

    code = doLoadFileBlockData(pReader, pBlockIter, &pStatus->fileBlockData);
2860 2861 2862 2863
    if (code != TSDB_CODE_SUCCESS) {
      return code;
    }

2864
    // 4. check the data values
2865 2866 2867 2868
    initBlockDumpInfo(pReader, pBlockIter);

    pDumpInfo->rowIndex =
        doMergeRowsInFileBlockImpl(pBlockData, pDumpInfo->rowIndex, key, pMerger, &pReader->verRange, step);
H
Haojun Liao 已提交
2869
    if (pDumpInfo->rowIndex >= pDumpInfo->totalRows) {
2870 2871 2872 2873 2874 2875 2876
      *state = CHECK_FILEBLOCK_CONT;
    }
  }

  return TSDB_CODE_SUCCESS;
}

2877 2878
int32_t doMergeRowsInFileBlocks(SBlockData* pBlockData, STableBlockScanInfo* pScanInfo, STsdbReader* pReader,
                                SRowMerger* pMerger) {
2879 2880
  SFileBlockDumpInfo* pDumpInfo = &pReader->status.fBlockDumpInfo;

2881
  bool    asc = ASCENDING_TRAVERSE(pReader->order);
2882
  int64_t key = pBlockData->aTSKEY[pDumpInfo->rowIndex];
2883
  int32_t step = asc ? 1 : -1;
2884

2885
  pDumpInfo->rowIndex += step;
2886
  if ((pDumpInfo->rowIndex <= pBlockData->nRow - 1 && asc) || (pDumpInfo->rowIndex >= 0 && !asc)) {
2887 2888 2889
    pDumpInfo->rowIndex =
        doMergeRowsInFileBlockImpl(pBlockData, pDumpInfo->rowIndex, key, pMerger, &pReader->verRange, step);
  }
2890

2891 2892 2893 2894
  // all rows are consumed, let's try next file block
  if ((pDumpInfo->rowIndex >= pBlockData->nRow && asc) || (pDumpInfo->rowIndex < 0 && !asc)) {
    while (1) {
      CHECK_FILEBLOCK_STATE st;
2895

2896
      SFileDataBlockInfo* pFileBlockInfo = getCurrentBlockInfo(&pReader->status.blockIter);
H
Hongze Cheng 已提交
2897
      SDataBlk*           pCurrentBlock = getCurrentBlock(&pReader->status.blockIter);
2898 2899 2900
      checkForNeighborFileBlock(pReader, pScanInfo, pCurrentBlock, pFileBlockInfo, pMerger, key, &st);
      if (st == CHECK_FILEBLOCK_QUIT) {
        break;
2901
      }
2902
    }
H
Haojun Liao 已提交
2903
  }
2904

H
Haojun Liao 已提交
2905 2906 2907
  return TSDB_CODE_SUCCESS;
}

H
Hongze Cheng 已提交
2908 2909
int32_t doMergeRowsInLastBlock(SLastBlockReader* pLastBlockReader, STableBlockScanInfo* pScanInfo, int64_t ts,
                               SRowMerger* pMerger) {
H
Haojun Liao 已提交
2910
  pScanInfo->lastKey = ts;
2911
  while (nextRowFromLastBlocks(pLastBlockReader, pScanInfo)) {
2912 2913
    int64_t next1 = getCurrentKeyInLastBlock(pLastBlockReader);
    if (next1 == ts) {
2914
      TSDBROW fRow1 = tMergeTreeGetRow(&pLastBlockReader->mergeTree);
2915 2916 2917 2918 2919 2920 2921 2922 2923
      tRowMerge(pMerger, &fRow1);
    } else {
      break;
    }
  }

  return TSDB_CODE_SUCCESS;
}

2924 2925
int32_t doMergeMemTableMultiRows(TSDBROW* pRow, uint64_t uid, SIterInfo* pIter, SArray* pDelList, STSRow** pTSRow,
                                 STsdbReader* pReader, bool* freeTSRow) {
H
Haojun Liao 已提交
2926
  TSDBROW* pNextRow = NULL;
2927
  TSDBROW  current = *pRow;
2928

2929 2930
  {  // if the timestamp of the next valid row has a different ts, return current row directly
    pIter->hasVal = tsdbTbDataIterNext(pIter->iter);
2931

2932 2933 2934
    if (!pIter->hasVal) {
      *pTSRow = current.pTSRow;
      *freeTSRow = false;
2935
      return TSDB_CODE_SUCCESS;
2936
    } else {  // has next point in mem/imem
2937
      pNextRow = getValidMemRow(pIter, pDelList, pReader);
2938 2939 2940
      if (pNextRow == NULL) {
        *pTSRow = current.pTSRow;
        *freeTSRow = false;
2941
        return TSDB_CODE_SUCCESS;
2942 2943
      }

H
Haojun Liao 已提交
2944
      if (current.pTSRow->ts != pNextRow->pTSRow->ts) {
2945 2946
        *pTSRow = current.pTSRow;
        *freeTSRow = false;
2947
        return TSDB_CODE_SUCCESS;
2948
      }
2949
    }
2950 2951
  }

2952 2953
  SRowMerger merge = {0};

2954
  // get the correct schema for data in memory
H
Haojun Liao 已提交
2955
  STSchema* pTSchema = doGetSchemaForTSRow(TSDBROW_SVERSION(&current), pReader, uid);
H
Haojun Liao 已提交
2956

2957 2958
  if (pReader->pSchema == NULL) {
    pReader->pSchema = pTSchema;
2959
  }
H
Haojun Liao 已提交
2960

H
Haojun Liao 已提交
2961 2962 2963 2964 2965 2966
  tRowMergerInit2(&merge, pReader->pSchema, &current, pTSchema);

  STSchema* pTSchema1 = doGetSchemaForTSRow(TSDBROW_SVERSION(pNextRow), pReader, uid);
  tRowMergerAdd(&merge, pNextRow, pTSchema1);

  doMergeRowsInBuf(pIter, uid, current.pTSRow->ts, pDelList, &merge, pReader);
2967 2968 2969 2970
  int32_t code = tRowMergerGetRow(&merge, pTSRow);
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }
M
Minglei Jin 已提交
2971

2972
  tRowMergerClear(&merge);
2973
  *freeTSRow = true;
2974
  return TSDB_CODE_SUCCESS;
2975 2976
}

2977
int32_t doMergeMemIMemRows(TSDBROW* pRow, TSDBROW* piRow, STableBlockScanInfo* pBlockScanInfo, STsdbReader* pReader,
H
Hongze Cheng 已提交
2978
                           STSRow** pTSRow) {
H
Haojun Liao 已提交
2979 2980
  SRowMerger merge = {0};

2981 2982 2983
  TSDBKEY k = TSDBROW_KEY(pRow);
  TSDBKEY ik = TSDBROW_KEY(piRow);

2984
  if (ASCENDING_TRAVERSE(pReader->order)) {  // ascending order imem --> mem
H
Haojun Liao 已提交
2985
    STSchema* pSchema = doGetSchemaForTSRow(TSDBROW_SVERSION(pRow), pReader, pBlockScanInfo->uid);
2986

H
Haojun Liao 已提交
2987
    tRowMergerInit(&merge, piRow, pSchema);
2988
    doMergeRowsInBuf(&pBlockScanInfo->iiter, pBlockScanInfo->uid, ik.ts, pBlockScanInfo->delSkyline, &merge, pReader);
2989

2990
    tRowMerge(&merge, pRow);
2991
    doMergeRowsInBuf(&pBlockScanInfo->iter, pBlockScanInfo->uid, k.ts, pBlockScanInfo->delSkyline, &merge, pReader);
2992
  } else {
H
Haojun Liao 已提交
2993
    STSchema* pSchema = doGetSchemaForTSRow(TSDBROW_SVERSION(pRow), pReader, pBlockScanInfo->uid);
2994

H
Haojun Liao 已提交
2995
    tRowMergerInit(&merge, pRow, pSchema);
2996
    doMergeRowsInBuf(&pBlockScanInfo->iter, pBlockScanInfo->uid, k.ts, pBlockScanInfo->delSkyline, &merge, pReader);
2997 2998

    tRowMerge(&merge, piRow);
2999
    doMergeRowsInBuf(&pBlockScanInfo->iiter, pBlockScanInfo->uid, k.ts, pBlockScanInfo->delSkyline, &merge, pReader);
3000
  }
3001

3002 3003
  int32_t code = tRowMergerGetRow(&merge, pTSRow);
  return code;
3004 3005
}

3006 3007
int32_t tsdbGetNextRowInMem(STableBlockScanInfo* pBlockScanInfo, STsdbReader* pReader, STSRow** pTSRow, int64_t endKey,
                            bool* freeTSRow) {
3008 3009
  TSDBROW* pRow = getValidMemRow(&pBlockScanInfo->iter, pBlockScanInfo->delSkyline, pReader);
  TSDBROW* piRow = getValidMemRow(&pBlockScanInfo->iiter, pBlockScanInfo->delSkyline, pReader);
dengyihao's avatar
dengyihao 已提交
3010
  SArray*  pDelList = pBlockScanInfo->delSkyline;
3011
  uint64_t uid = pBlockScanInfo->uid;
H
Haojun Liao 已提交
3012

3013 3014
  // todo refactor
  bool asc = ASCENDING_TRAVERSE(pReader->order);
3015
  if (pBlockScanInfo->iter.hasVal) {
3016 3017 3018 3019 3020 3021
    TSDBKEY k = TSDBROW_KEY(pRow);
    if ((k.ts >= endKey && asc) || (k.ts <= endKey && !asc)) {
      pRow = NULL;
    }
  }

3022
  if (pBlockScanInfo->iiter.hasVal) {
3023 3024 3025 3026 3027 3028
    TSDBKEY k = TSDBROW_KEY(piRow);
    if ((k.ts >= endKey && asc) || (k.ts <= endKey && !asc)) {
      piRow = NULL;
    }
  }

3029
  if (pBlockScanInfo->iter.hasVal && pBlockScanInfo->iiter.hasVal && pRow != NULL && piRow != NULL) {
3030
    TSDBKEY k = TSDBROW_KEY(pRow);
3031
    TSDBKEY ik = TSDBROW_KEY(piRow);
H
Haojun Liao 已提交
3032

3033
    int32_t code = TSDB_CODE_SUCCESS;
3034 3035
    if (ik.ts != k.ts) {
      if (((ik.ts < k.ts) && asc) || ((ik.ts > k.ts) && (!asc))) {  // ik.ts < k.ts
3036
        code = doMergeMemTableMultiRows(piRow, uid, &pBlockScanInfo->iiter, pDelList, pTSRow, pReader, freeTSRow);
3037
      } else if (((k.ts < ik.ts) && asc) || ((k.ts > ik.ts) && (!asc))) {
3038
        code = doMergeMemTableMultiRows(pRow, uid, &pBlockScanInfo->iter, pDelList, pTSRow, pReader, freeTSRow);
3039
      }
3040
    } else {  // ik.ts == k.ts
3041
      *freeTSRow = true;
3042 3043 3044 3045
      code = doMergeMemIMemRows(pRow, piRow, pBlockScanInfo, pReader, pTSRow);
      if (code != TSDB_CODE_SUCCESS) {
        return code;
      }
H
Haojun Liao 已提交
3046
    }
3047

3048
    return code;
H
Haojun Liao 已提交
3049 3050
  }

3051
  if (pBlockScanInfo->iter.hasVal && pRow != NULL) {
H
Hongze Cheng 已提交
3052 3053
    return doMergeMemTableMultiRows(pRow, pBlockScanInfo->uid, &pBlockScanInfo->iter, pDelList, pTSRow, pReader,
                                    freeTSRow);
H
Haojun Liao 已提交
3054 3055
  }

3056
  if (pBlockScanInfo->iiter.hasVal && piRow != NULL) {
3057
    return doMergeMemTableMultiRows(piRow, uid, &pBlockScanInfo->iiter, pDelList, pTSRow, pReader, freeTSRow);
H
Haojun Liao 已提交
3058 3059 3060 3061 3062
  }

  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
3063
int32_t doAppendRowFromTSRow(SSDataBlock* pBlock, STsdbReader* pReader, STSRow* pTSRow, uint64_t uid) {
3064 3065 3066
  int32_t numOfRows = pBlock->info.rows;
  int32_t numOfCols = (int32_t)taosArrayGetSize(pBlock->pDataBlock);

3067
  SBlockLoadSuppInfo* pSupInfo = &pReader->suppInfo;
3068
  STSchema*           pSchema = doGetSchemaForTSRow(pTSRow->sver, pReader, uid);
3069

3070
  SColVal colVal = {0};
3071
  int32_t i = 0, j = 0;
H
Haojun Liao 已提交
3072

3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083
  SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
  if (pColInfoData->info.colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
    colDataAppend(pColInfoData, numOfRows, (const char*)&pTSRow->ts, false);
    i += 1;
  }

  while (i < numOfCols && j < pSchema->numOfCols) {
    pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
    col_id_t colId = pColInfoData->info.colId;

    if (colId == pSchema->columns[j].colId) {
H
Haojun Liao 已提交
3084
      tTSRowGetVal(pTSRow, pSchema, j, &colVal);
3085 3086 3087 3088 3089 3090 3091 3092
      doCopyColVal(pColInfoData, numOfRows, i, &colVal, pSupInfo);
      i += 1;
      j += 1;
    } else if (colId < pSchema->columns[j].colId) {
      colDataAppendNULL(pColInfoData, numOfRows);
      i += 1;
    } else if (colId > pSchema->columns[j].colId) {
      j += 1;
3093
    }
3094 3095
  }

3096
  // set null value since current column does not exist in the "pSchema"
3097
  while (i < numOfCols) {
3098 3099 3100 3101 3102
    pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
    colDataAppendNULL(pColInfoData, numOfRows);
    i += 1;
  }

3103 3104 3105 3106
  pBlock->info.rows += 1;
  return TSDB_CODE_SUCCESS;
}

H
Hongze Cheng 已提交
3107 3108
int32_t doAppendRowFromFileBlock(SSDataBlock* pResBlock, STsdbReader* pReader, SBlockData* pBlockData,
                                 int32_t rowIndex) {
3109 3110 3111 3112 3113 3114 3115 3116
  int32_t i = 0, j = 0;
  int32_t outputRowIndex = pResBlock->info.rows;

  SBlockLoadSuppInfo* pSupInfo = &pReader->suppInfo;

  SColumnInfoData* pColData = taosArrayGet(pResBlock->pDataBlock, i);
  if (pColData->info.colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
    colDataAppendInt64(pColData, outputRowIndex, &pBlockData->aTSKEY[rowIndex]);
3117
    i += 1;
3118 3119 3120 3121 3122 3123
  }

  SColVal cv = {0};
  int32_t numOfInputCols = taosArrayGetSize(pBlockData->aIdx);
  int32_t numOfOutputCols = blockDataGetNumOfCols(pResBlock);

3124
  while (i < numOfOutputCols && j < numOfInputCols) {
3125
    SColumnInfoData* pCol = taosArrayGet(pResBlock->pDataBlock, i);
3126
    SColData*        pData = tBlockDataGetColDataByIdx(pBlockData, j);
3127

3128 3129 3130 3131 3132
    if (pData->cid < pCol->info.colId) {
      j += 1;
      continue;
    }

3133
    if (pData->cid == pCol->info.colId) {
3134 3135
      tColDataGetValue(pData, rowIndex, &cv);
      doCopyColVal(pCol, outputRowIndex, i, &cv, pSupInfo);
3136
      j += 1;
3137
    } else if (pData->cid > pCol->info.colId) {  // the specified column does not exist in file block, fill with null data
3138 3139 3140 3141 3142 3143 3144 3145
      colDataAppendNULL(pCol, outputRowIndex);
    }

    i += 1;
  }

  while (i < numOfOutputCols) {
    SColumnInfoData* pCol = taosArrayGet(pResBlock->pDataBlock, i);
3146
    colDataAppendNULL(pCol, outputRowIndex);
3147 3148 3149 3150 3151 3152 3153
    i += 1;
  }

  pResBlock->info.rows += 1;
  return TSDB_CODE_SUCCESS;
}

3154 3155
int32_t buildDataBlockFromBufImpl(STableBlockScanInfo* pBlockScanInfo, int64_t endKey, int32_t capacity,
                                  STsdbReader* pReader) {
H
Haojun Liao 已提交
3156 3157 3158 3159
  SSDataBlock* pBlock = pReader->pResBlock;

  do {
    STSRow* pTSRow = NULL;
3160
    bool    freeTSRow = false;
3161
    tsdbGetNextRowInMem(pBlockScanInfo, pReader, &pTSRow, endKey, &freeTSRow);
3162 3163
    if (pTSRow == NULL) {
      break;
H
Haojun Liao 已提交
3164 3165
    }

H
Haojun Liao 已提交
3166
    doAppendRowFromTSRow(pBlock, pReader, pTSRow, pBlockScanInfo->uid);
3167 3168 3169
    if (freeTSRow) {
      taosMemoryFree(pTSRow);
    }
H
Haojun Liao 已提交
3170 3171

    // no data in buffer, return immediately
3172
    if (!(pBlockScanInfo->iter.hasVal || pBlockScanInfo->iiter.hasVal)) {
H
Haojun Liao 已提交
3173 3174 3175
      break;
    }

3176
    if (pBlock->info.rows >= capacity) {
H
Haojun Liao 已提交
3177 3178 3179 3180
      break;
    }
  } while (1);

3181
  ASSERT(pBlock->info.rows <= capacity);
H
Haojun Liao 已提交
3182 3183
  return TSDB_CODE_SUCCESS;
}
H
Hongze Cheng 已提交
3184

3185
// todo refactor, use arraylist instead
H
Hongze Cheng 已提交
3186
int32_t tsdbSetTableId(STsdbReader* pReader, int64_t uid) {
3187 3188 3189
  ASSERT(pReader != NULL);
  taosHashClear(pReader->status.pTableMap);

3190
  STableBlockScanInfo info = {.lastKey = 0, .uid = uid};
3191
  taosHashPut(pReader->status.pTableMap, &info.uid, sizeof(uint64_t), &info, sizeof(info));
H
Hongze Cheng 已提交
3192 3193 3194
  return TDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
3195 3196 3197 3198 3199 3200
void* tsdbGetIdx(SMeta* pMeta) {
  if (pMeta == NULL) {
    return NULL;
  }
  return metaGetIdx(pMeta);
}
dengyihao's avatar
dengyihao 已提交
3201

dengyihao's avatar
dengyihao 已提交
3202 3203 3204 3205 3206 3207
void* tsdbGetIvtIdx(SMeta* pMeta) {
  if (pMeta == NULL) {
    return NULL;
  }
  return metaGetIvtIdx(pMeta);
}
L
Liu Jicong 已提交
3208

H
Hongze Cheng 已提交
3209
uint64_t getReaderMaxVersion(STsdbReader* pReader) { return pReader->verRange.maxVer; }
3210

H
refact  
Hongze Cheng 已提交
3211
// ====================================== EXPOSED APIs ======================================
3212 3213
int32_t tsdbReaderOpen(SVnode* pVnode, SQueryTableDataCond* pCond, SArray* pTableList, STsdbReader** ppReader,
                       const char* idstr) {
3214 3215
  int32_t code = tsdbReaderCreate(pVnode, pCond, ppReader, 4096, idstr);
  if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
3216 3217
    goto _err;
  }
H
Hongze Cheng 已提交
3218

3219
  // check for query time window
H
Haojun Liao 已提交
3220
  STsdbReader* pReader = *ppReader;
3221
  if (isEmptyQueryTimeWindow(&pReader->window)) {
H
Haojun Liao 已提交
3222 3223 3224
    tsdbDebug("%p query window not overlaps with the data set, no result returned, %s", pReader, pReader->idStr);
    return TSDB_CODE_SUCCESS;
  }
H
Hongze Cheng 已提交
3225

3226 3227 3228
  if (pCond->type == TIMEWINDOW_RANGE_EXTERNAL) {
    // update the SQueryTableDataCond to create inner reader
    STimeWindow w = pCond->twindows;
3229
    int32_t     order = pCond->order;
3230 3231 3232 3233 3234 3235 3236 3237 3238 3239
    if (order == TSDB_ORDER_ASC) {
      pCond->twindows.ekey = pCond->twindows.skey;
      pCond->twindows.skey = INT64_MIN;
      pCond->order = TSDB_ORDER_DESC;
    } else {
      pCond->twindows.skey = pCond->twindows.ekey;
      pCond->twindows.ekey = INT64_MAX;
      pCond->order = TSDB_ORDER_ASC;
    }

3240
    // here we only need one more row, so the capacity is set to be ONE.
3241 3242 3243 3244 3245 3246 3247 3248
    code = tsdbReaderCreate(pVnode, pCond, &pReader->innerReader[0], 1, idstr);
    if (code != TSDB_CODE_SUCCESS) {
      goto _err;
    }

    if (order == TSDB_ORDER_ASC) {
      pCond->twindows.skey = w.ekey;
      pCond->twindows.ekey = INT64_MAX;
3249
    } else {
3250 3251 3252 3253 3254 3255 3256 3257 3258
      pCond->twindows.skey = INT64_MIN;
      pCond->twindows.ekey = w.ekey;
    }
    code = tsdbReaderCreate(pVnode, pCond, &pReader->innerReader[1], 1, idstr);
    if (code != TSDB_CODE_SUCCESS) {
      goto _err;
    }
  }

H
Haojun Liao 已提交
3259
  // NOTE: the endVersion in pCond is the data version not schema version, so pCond->endVersion is not correct here.
3260
  if (pCond->suid != 0) {
H
Haojun Liao 已提交
3261 3262 3263 3264
    pReader->pSchema = metaGetTbTSchema(pReader->pTsdb->pVnode->pMeta, pReader->suid, /*pCond->endVersion*/ -1);
    if (pReader->pSchema == NULL) {
      tsdbError("failed to get table schema, suid:%"PRIu64", ver:%"PRId64" , %s", pReader->suid, -1, pReader->idStr);
    }
3265 3266
  } else if (taosArrayGetSize(pTableList) > 0) {
    STableKeyInfo* pKey = taosArrayGet(pTableList, 0);
H
Haojun Liao 已提交
3267 3268 3269 3270
    pReader->pSchema = metaGetTbTSchema(pReader->pTsdb->pVnode->pMeta, pKey->uid, /*pCond->endVersion*/ -1);
    if (pReader->pSchema == NULL) {
      tsdbError("failed to get table schema, uid:%"PRIu64", ver:%"PRId64" , %s", pKey->uid, -1, pReader->idStr);
    }
3271 3272
  }

3273 3274
  int32_t numOfTables = taosArrayGetSize(pTableList);
  pReader->status.pTableMap = createDataBlockScanInfo(pReader, pTableList->pData, numOfTables);
H
Haojun Liao 已提交
3275 3276 3277
  if (pReader->status.pTableMap == NULL) {
    tsdbReaderClose(pReader);
    *ppReader = NULL;
H
Haojun Liao 已提交
3278

H
Haojun Liao 已提交
3279 3280 3281
    code = TSDB_CODE_TDB_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
3282

H
Hongze Cheng 已提交
3283
  code = tsdbTakeReadSnap(pReader->pTsdb, &pReader->pReadSnap);
3284 3285 3286
  if (code != TSDB_CODE_SUCCESS) {
    goto _err;
  }
H
Hongze Cheng 已提交
3287

3288 3289
  if (pReader->type == TIMEWINDOW_RANGE_CONTAINED) {
    SDataBlockIter* pBlockIter = &pReader->status.blockIter;
3290

3291
    initFilesetIterator(&pReader->status.fileIter, pReader->pReadSnap->fs.aDFileSet, pReader);
3292
    resetDataBlockIterator(&pReader->status.blockIter, pReader->order);
3293 3294 3295 3296 3297 3298 3299 3300 3301 3302

    // no data in files, let's try buffer in memory
    if (pReader->status.fileIter.numOfFiles == 0) {
      pReader->status.loadFromFile = false;
    } else {
      code = initForFirstBlockInFile(pReader, pBlockIter);
      if (code != TSDB_CODE_SUCCESS) {
        return code;
      }
    }
3303
  } else {
3304
    STsdbReader*    pPrevReader = pReader->innerReader[0];
3305 3306
    SDataBlockIter* pBlockIter = &pPrevReader->status.blockIter;

3307 3308 3309 3310 3311
    code = tsdbTakeReadSnap(pPrevReader->pTsdb, &pPrevReader->pReadSnap);
    if (code != TSDB_CODE_SUCCESS) {
      goto _err;
    }

3312
    initFilesetIterator(&pPrevReader->status.fileIter, pPrevReader->pReadSnap->fs.aDFileSet, pPrevReader);
3313
    resetDataBlockIterator(&pPrevReader->status.blockIter, pPrevReader->order);
3314 3315 3316 3317 3318 3319 3320 3321 3322

    // no data in files, let's try buffer in memory
    if (pPrevReader->status.fileIter.numOfFiles == 0) {
      pPrevReader->status.loadFromFile = false;
    } else {
      code = initForFirstBlockInFile(pPrevReader, pBlockIter);
      if (code != TSDB_CODE_SUCCESS) {
        return code;
      }
3323 3324 3325
    }
  }

3326
  tsdbDebug("%p total numOfTable:%d in this query %s", pReader, numOfTables, pReader->idStr);
H
Hongze Cheng 已提交
3327
  return code;
H
Hongze Cheng 已提交
3328 3329

_err:
S
Shengliang Guan 已提交
3330
  tsdbError("failed to create data reader, code:%s %s", tstrerror(code), pReader->idStr);
H
Hongze Cheng 已提交
3331
  return code;
H
refact  
Hongze Cheng 已提交
3332 3333 3334
}

void tsdbReaderClose(STsdbReader* pReader) {
3335 3336
  if (pReader == NULL) {
    return;
3337
  }
H
refact  
Hongze Cheng 已提交
3338

3339
  SBlockLoadSuppInfo* pSupInfo = &pReader->suppInfo;
H
Hongze Cheng 已提交
3340

3341 3342 3343 3344
  taosMemoryFreeClear(pSupInfo->plist);
  taosMemoryFree(pSupInfo->colIds);

  taosArrayDestroy(pSupInfo->pColAgg);
L
Liu Jicong 已提交
3345
  for (int32_t i = 0; i < blockDataGetNumOfCols(pReader->pResBlock); ++i) {
3346 3347 3348 3349
    if (pSupInfo->buildBuf[i] != NULL) {
      taosMemoryFreeClear(pSupInfo->buildBuf[i]);
    }
  }
3350

3351
  taosMemoryFree(pSupInfo->buildBuf);
H
Hongze Cheng 已提交
3352
  tBlockDataDestroy(&pReader->status.fileBlockData, true);
3353 3354

  cleanupDataBlockIterator(&pReader->status.blockIter);
3355 3356

  size_t numOfTables = taosHashGetSize(pReader->status.pTableMap);
3357
  destroyBlockScanInfo(pReader->status.pTableMap);
3358
  blockDataDestroy(pReader->pResBlock);
3359

H
Haojun Liao 已提交
3360 3361 3362
  if (pReader->pFileReader != NULL) {
    tsdbDataFReaderClose(&pReader->pFileReader);
  }
H
refact  
Hongze Cheng 已提交
3363

3364 3365
  tsdbUntakeReadSnap(pReader->pTsdb, pReader->pReadSnap);

3366 3367
  taosMemoryFree(pReader->status.uidCheckInfo.tableUidList);

H
Haojun Liao 已提交
3368 3369
  SFilesetIter* pFilesetIter = &pReader->status.fileIter;
  if (pFilesetIter->pLastBlockReader != NULL) {
3370
    tMergeTreeClose(&pFilesetIter->pLastBlockReader->mergeTree);
3371
    pFilesetIter->pLastBlockReader->pInfo = destroyLastBlockLoadInfo(pFilesetIter->pLastBlockReader->pInfo);
H
Haojun Liao 已提交
3372 3373 3374
    taosMemoryFree(pFilesetIter->pLastBlockReader);
  }

3375
  SIOCostSummary* pCost = &pReader->cost;
H
refact  
Hongze Cheng 已提交
3376

3377
  tsdbDebug("%p :io-cost summary: head-file:%" PRIu64 ", head-file time:%.2f ms, SMA:%" PRId64
3378 3379
            " SMA-time:%.2f ms, fileBlocks:%" PRId64
            ", fileBlocks-time:%.2f ms, "
3380 3381 3382 3383 3384
            "build in-memory-block-time:%.2f ms, lastBlocks:%" PRId64
            ", lastBlocks-time:%.2f ms, STableBlockScanInfo size:%.2f Kb %s",
            pReader, pCost->headFileLoad, pCost->headFileLoadTime, pCost->smaDataLoad, pCost->smaLoadTime,
            pCost->numOfBlocks, pCost->blockLoadTime, pCost->buildmemBlock, pCost->lastBlockLoad,
            pCost->lastBlockLoadTime, numOfTables * sizeof(STableBlockScanInfo) / 1000.0, pReader->idStr);
H
refact  
Hongze Cheng 已提交
3385

3386 3387
  taosMemoryFree(pReader->idStr);
  taosMemoryFree(pReader->pSchema);
3388 3389 3390
  if (pReader->pMemSchema != pReader->pSchema) {
    taosMemoryFree(pReader->pMemSchema);
  }
3391
  taosMemoryFreeClear(pReader);
H
refact  
Hongze Cheng 已提交
3392 3393
}

3394
static bool doTsdbNextDataBlock(STsdbReader* pReader) {
H
Haojun Liao 已提交
3395
  // cleanup the data that belongs to the previous data block
3396 3397
  SSDataBlock* pBlock = pReader->pResBlock;
  blockDataCleanup(pBlock);
H
Hongze Cheng 已提交
3398

3399
  SReaderStatus* pStatus = &pReader->status;
H
Haojun Liao 已提交
3400

3401 3402 3403 3404 3405
  if (pStatus->loadFromFile) {
    int32_t code = buildBlockFromFiles(pReader);
    if (code != TSDB_CODE_SUCCESS) {
      return false;
    }
3406

3407 3408 3409
    if (pBlock->info.rows > 0) {
      return true;
    } else {
H
Haojun Liao 已提交
3410
      buildBlockFromBufferSequentially(pReader);
3411
      return pBlock->info.rows > 0;
H
Haojun Liao 已提交
3412
    }
3413 3414 3415
  } else {  // no data in files, let's try the buffer
    buildBlockFromBufferSequentially(pReader);
    return pBlock->info.rows > 0;
H
Haojun Liao 已提交
3416
  }
3417

3418
  return false;
H
refact  
Hongze Cheng 已提交
3419 3420
}

3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457
bool tsdbNextDataBlock(STsdbReader* pReader) {
  if (isEmptyQueryTimeWindow(&pReader->window)) {
    return false;
  }

  if (pReader->innerReader[0] != NULL) {
    bool ret = doTsdbNextDataBlock(pReader->innerReader[0]);
    if (ret) {
      pReader->step = EXTERNAL_ROWS_PREV;
      return ret;
    }

    tsdbReaderClose(pReader->innerReader[0]);
    pReader->innerReader[0] = NULL;
  }

  pReader->step = EXTERNAL_ROWS_MAIN;
  bool ret = doTsdbNextDataBlock(pReader);
  if (ret) {
    return ret;
  }

  if (pReader->innerReader[1] != NULL) {
    bool ret1 = doTsdbNextDataBlock(pReader->innerReader[1]);
    if (ret1) {
      pReader->step = EXTERNAL_ROWS_NEXT;
      return ret1;
    }

    tsdbReaderClose(pReader->innerReader[1]);
    pReader->innerReader[1] = NULL;
  }

  return false;
}

static void setBlockInfo(STsdbReader* pReader, SDataBlockInfo* pDataBlockInfo) {
3458 3459 3460 3461
  ASSERT(pDataBlockInfo != NULL && pReader != NULL);
  pDataBlockInfo->rows = pReader->pResBlock->info.rows;
  pDataBlockInfo->uid = pReader->pResBlock->info.uid;
  pDataBlockInfo->window = pReader->pResBlock->info.window;
H
Hongze Cheng 已提交
3462 3463
}

3464 3465
void tsdbRetrieveDataBlockInfo(STsdbReader* pReader, SDataBlockInfo* pDataBlockInfo) {
  if (pReader->type == TIMEWINDOW_RANGE_EXTERNAL) {
3466
    if (pReader->step == EXTERNAL_ROWS_MAIN) {
3467
      setBlockInfo(pReader, pDataBlockInfo);
3468
    } else if (pReader->step == EXTERNAL_ROWS_PREV) {
3469 3470 3471 3472 3473 3474 3475 3476 3477
      setBlockInfo(pReader->innerReader[0], pDataBlockInfo);
    } else {
      setBlockInfo(pReader->innerReader[1], pDataBlockInfo);
    }
  } else {
    setBlockInfo(pReader, pDataBlockInfo);
  }
}

3478
int32_t tsdbRetrieveDatablockSMA(STsdbReader* pReader, SColumnDataAgg*** pBlockStatis, bool* allHave) {
H
Hongze Cheng 已提交
3479
  int32_t code = 0;
3480
  *allHave = false;
H
Hongze Cheng 已提交
3481

3482
  if (pReader->type == TIMEWINDOW_RANGE_EXTERNAL) {
3483 3484 3485 3486
    *pBlockStatis = NULL;
    return TSDB_CODE_SUCCESS;
  }

3487
  // there is no statistics data for composed block
3488 3489 3490 3491
  if (pReader->status.composedDataBlock) {
    *pBlockStatis = NULL;
    return TSDB_CODE_SUCCESS;
  }
H
Hongze Cheng 已提交
3492

3493
  SFileDataBlockInfo* pFBlock = getCurrentBlockInfo(&pReader->status.blockIter);
H
Hongze Cheng 已提交
3494

H
Hongze Cheng 已提交
3495 3496
  SDataBlk* pBlock = getCurrentBlock(&pReader->status.blockIter);
  int64_t   stime = taosGetTimestampUs();
H
Hongze Cheng 已提交
3497

3498 3499
  SBlockLoadSuppInfo* pSup = &pReader->suppInfo;

H
Hongze Cheng 已提交
3500
  if (tDataBlkHasSma(pBlock)) {
H
Hongze Cheng 已提交
3501
    code = tsdbReadBlockSma(pReader->pFileReader, pBlock, pSup->pColAgg);
3502
    if (code != TSDB_CODE_SUCCESS) {
3503 3504
      tsdbDebug("vgId:%d, failed to load block SMA for uid %" PRIu64 ", code:%s, %s", 0, pFBlock->uid, tstrerror(code),
                pReader->idStr);
3505 3506
      return code;
    }
3507 3508 3509
  } else {
    *pBlockStatis = NULL;
    return TSDB_CODE_SUCCESS;
3510
  }
H
Hongze Cheng 已提交
3511

3512
  *allHave = true;
H
Hongze Cheng 已提交
3513

3514 3515
  // always load the first primary timestamp column data
  SColumnDataAgg* pTsAgg = &pSup->tsColAgg;
3516

3517 3518
  pTsAgg->numOfNull = 0;
  pTsAgg->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534
  pTsAgg->min = pReader->pResBlock->info.window.skey;
  pTsAgg->max = pReader->pResBlock->info.window.ekey;
  pSup->plist[0] = pTsAgg;

  // update the number of NULL data rows
  size_t numOfCols = blockDataGetNumOfCols(pReader->pResBlock);

  int32_t i = 0, j = 0;
  while (j < numOfCols && i < taosArrayGetSize(pSup->pColAgg)) {
    SColumnDataAgg* pAgg = taosArrayGet(pSup->pColAgg, i);
    if (pAgg->colId == pSup->colIds[j]) {
      if (IS_BSMA_ON(&(pReader->pSchema->columns[i]))) {
        pSup->plist[j] = pAgg;
      } else {
        *allHave = false;
      }
3535 3536
      i += 1;
      j += 1;
3537 3538 3539 3540 3541 3542 3543
    } else if (pAgg->colId < pSup->colIds[j]) {
      i += 1;
    } else if (pSup->colIds[j] < pAgg->colId) {
      j += 1;
    }
  }

3544
  double elapsed = (taosGetTimestampUs() - stime) / 1000.0;
3545
  pReader->cost.smaLoadTime += elapsed;
3546
  pReader->cost.smaDataLoad += 1;
3547 3548 3549

  *pBlockStatis = pSup->plist;

3550
  tsdbDebug("vgId:%d, succeed to load block SMA for uid %" PRIu64 ", elapsed time:%.2f ms, %s", 0, pFBlock->uid,
3551 3552
            elapsed, pReader->idStr);

H
Hongze Cheng 已提交
3553
  return code;
H
Hongze Cheng 已提交
3554 3555
}

3556
static SArray* doRetrieveDataBlock(STsdbReader* pReader) {
H
Haojun Liao 已提交
3557 3558 3559
  SReaderStatus* pStatus = &pReader->status;

  if (pStatus->composedDataBlock) {
3560
    return pReader->pResBlock->pDataBlock;
3561
  }
3562

3563
  SFileDataBlockInfo*  pFBlock = getCurrentBlockInfo(&pStatus->blockIter);
3564
  STableBlockScanInfo* pBlockScanInfo = taosHashGet(pStatus->pTableMap, &pFBlock->uid, sizeof(pFBlock->uid));
3565

H
Haojun Liao 已提交
3566
  tBlockDataReset(&pStatus->fileBlockData);
3567 3568
  int32_t code = tBlockDataInit(&pStatus->fileBlockData, pReader->suid, pBlockScanInfo->uid, pReader->pSchema);
  if (code != TSDB_CODE_SUCCESS) {
3569 3570
    terrno = code;
    return NULL;
3571 3572 3573
  }

  code = doLoadFileBlockData(pReader, &pStatus->blockIter, &pStatus->fileBlockData);
3574
  if (code != TSDB_CODE_SUCCESS) {
H
Hongze Cheng 已提交
3575
    tBlockDataDestroy(&pStatus->fileBlockData, 1);
3576 3577
    terrno = code;
    return NULL;
3578
  }
3579 3580 3581

  copyBlockDataToSDataBlock(pReader, pBlockScanInfo);
  return pReader->pResBlock->pDataBlock;
H
Hongze Cheng 已提交
3582 3583
}

3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595
SArray* tsdbRetrieveDataBlock(STsdbReader* pReader, SArray* pIdList) {
  if (pReader->type == TIMEWINDOW_RANGE_EXTERNAL) {
    if (pReader->step == EXTERNAL_ROWS_PREV) {
      return doRetrieveDataBlock(pReader->innerReader[0]);
    } else if (pReader->step == EXTERNAL_ROWS_NEXT) {
      return doRetrieveDataBlock(pReader->innerReader[1]);
    }
  }

  return doRetrieveDataBlock(pReader);
}

H
Haojun Liao 已提交
3596
int32_t tsdbReaderReset(STsdbReader* pReader, SQueryTableDataCond* pCond) {
3597 3598 3599
  if (isEmptyQueryTimeWindow(&pReader->window)) {
    return TSDB_CODE_SUCCESS;
  }
H
Hongze Cheng 已提交
3600

L
Liu Jicong 已提交
3601
  pReader->order = pCond->order;
3602
  pReader->type = TIMEWINDOW_RANGE_CONTAINED;
3603
  pReader->status.loadFromFile = true;
dengyihao's avatar
dengyihao 已提交
3604
  pReader->status.pTableIter = NULL;
H
Haojun Liao 已提交
3605
  pReader->window = updateQueryTimeWindow(pReader->pTsdb, &pCond->twindows);
H
Hongze Cheng 已提交
3606

3607
  // allocate buffer in order to load data blocks from file
3608
  memset(&pReader->suppInfo.tsColAgg, 0, sizeof(SColumnDataAgg));
3609 3610
  memset(pReader->suppInfo.plist, 0, POINTER_BYTES);

3611
  pReader->suppInfo.tsColAgg.colId = PRIMARYKEY_TIMESTAMP_COL_ID;
3612
  tsdbDataFReaderClose(&pReader->pFileReader);
3613

3614
  int32_t numOfTables = taosHashGetSize(pReader->status.pTableMap);
L
Liu Jicong 已提交
3615

3616
  initFilesetIterator(&pReader->status.fileIter, pReader->pReadSnap->fs.aDFileSet, pReader);
3617
  resetDataBlockIterator(&pReader->status.blockIter, pReader->order);
H
Haojun Liao 已提交
3618

H
Hongze Cheng 已提交
3619
  int64_t ts = ASCENDING_TRAVERSE(pReader->order) ? pReader->window.skey - 1 : pReader->window.ekey + 1;
H
Haojun Liao 已提交
3620
  resetDataBlockScanInfo(pReader->status.pTableMap, ts);
3621

3622
  int32_t         code = 0;
3623 3624
  SDataBlockIter* pBlockIter = &pReader->status.blockIter;

3625 3626 3627 3628 3629 3630
  // no data in files, let's try buffer in memory
  if (pReader->status.fileIter.numOfFiles == 0) {
    pReader->status.loadFromFile = false;
  } else {
    code = initForFirstBlockInFile(pReader, pBlockIter);
    if (code != TSDB_CODE_SUCCESS) {
3631 3632
      tsdbError("%p reset reader failed, numOfTables:%d, query range:%" PRId64 " - %" PRId64 " in query %s", pReader,
                numOfTables, pReader->window.skey, pReader->window.ekey, pReader->idStr);
3633 3634 3635
      return code;
    }
  }
H
Hongze Cheng 已提交
3636

dengyihao's avatar
dengyihao 已提交
3637 3638
  tsdbDebug("%p reset reader, suid:%" PRIu64 ", numOfTables:%d, query range:%" PRId64 " - %" PRId64 " in query %s",
            pReader, pReader->suid, numOfTables, pReader->window.skey, pReader->window.ekey, pReader->idStr);
3639

3640
  return code;
H
Hongze Cheng 已提交
3641
}
H
Hongze Cheng 已提交
3642

3643 3644 3645
static int32_t getBucketIndex(int32_t startRow, int32_t bucketRange, int32_t numOfRows) {
  return (numOfRows - startRow) / bucketRange;
}
H
Hongze Cheng 已提交
3646

3647 3648 3649 3650
int32_t tsdbGetFileBlocksDistInfo(STsdbReader* pReader, STableBlockDistInfo* pTableBlockInfo) {
  int32_t code = TSDB_CODE_SUCCESS;
  pTableBlockInfo->totalSize = 0;
  pTableBlockInfo->totalRows = 0;
H
Hongze Cheng 已提交
3651

3652 3653
  // find the start data block in file
  SReaderStatus* pStatus = &pReader->status;
H
Hongze Cheng 已提交
3654

3655 3656 3657
  STsdbCfg* pc = &pReader->pTsdb->pVnode->config.tsdbCfg;
  pTableBlockInfo->defMinRows = pc->minRows;
  pTableBlockInfo->defMaxRows = pc->maxRows;
H
Hongze Cheng 已提交
3658

3659
  int32_t bucketRange = ceil((pc->maxRows - pc->minRows) / 20.0);
H
Hongze Cheng 已提交
3660

3661
  pTableBlockInfo->numOfFiles += 1;
H
Hongze Cheng 已提交
3662

3663 3664
  int32_t numOfTables = (int32_t)taosHashGetSize(pStatus->pTableMap);
  int     defaultRows = 4096;
H
Hongze Cheng 已提交
3665

3666 3667
  SDataBlockIter* pBlockIter = &pStatus->blockIter;
  pTableBlockInfo->numOfFiles += pStatus->fileIter.numOfFiles;
H
Haojun Liao 已提交
3668

3669 3670
  if (pBlockIter->numOfBlocks > 0) {
    pTableBlockInfo->numOfBlocks += pBlockIter->numOfBlocks;
H
Haojun Liao 已提交
3671
  }
H
Hongze Cheng 已提交
3672

3673
  pTableBlockInfo->numOfTables = numOfTables;
3674
  bool hasNext = (pBlockIter->numOfBlocks > 0);
H
Hongze Cheng 已提交
3675

3676 3677
  while (true) {
    if (hasNext) {
H
Hongze Cheng 已提交
3678
      SDataBlk* pBlock = getCurrentBlock(pBlockIter);
H
Hongze Cheng 已提交
3679

3680 3681
      int32_t numOfRows = pBlock->nRow;
      pTableBlockInfo->totalRows += numOfRows;
H
Hongze Cheng 已提交
3682

3683 3684 3685
      if (numOfRows > pTableBlockInfo->maxRows) {
        pTableBlockInfo->maxRows = numOfRows;
      }
H
refact  
Hongze Cheng 已提交
3686

3687 3688 3689
      if (numOfRows < pTableBlockInfo->minRows) {
        pTableBlockInfo->minRows = numOfRows;
      }
H
refact  
Hongze Cheng 已提交
3690

3691 3692 3693
      if (numOfRows < defaultRows) {
        pTableBlockInfo->numOfSmallBlocks += 1;
      }
H
refact  
Hongze Cheng 已提交
3694

3695 3696
      int32_t bucketIndex = getBucketIndex(pTableBlockInfo->defMinRows, bucketRange, numOfRows);
      pTableBlockInfo->blockRowsHisto[bucketIndex]++;
3697 3698

      hasNext = blockIteratorNext(&pStatus->blockIter);
3699 3700 3701 3702 3703
    } else {
      code = initForFirstBlockInFile(pReader, pBlockIter);
      if ((code != TSDB_CODE_SUCCESS) || (pReader->status.loadFromFile == false)) {
        break;
      }
H
refact  
Hongze Cheng 已提交
3704

3705 3706
      pTableBlockInfo->numOfBlocks += pBlockIter->numOfBlocks;
      hasNext = (pBlockIter->numOfBlocks > 0);
3707
    }
H
refact  
Hongze Cheng 已提交
3708

H
Hongze Cheng 已提交
3709 3710
    //    tsdbDebug("%p %d blocks found in file for %d table(s), fid:%d, %s", pReader, numOfBlocks, numOfTables,
    //              pReader->pFileGroup->fid, pReader->idStr);
3711
  }
H
Hongze Cheng 已提交
3712

H
refact  
Hongze Cheng 已提交
3713 3714
  return code;
}
H
Hongze Cheng 已提交
3715

H
refact  
Hongze Cheng 已提交
3716
int64_t tsdbGetNumOfRowsInMemTable(STsdbReader* pReader) {
3717
  int64_t rows = 0;
H
Hongze Cheng 已提交
3718

3719 3720
  SReaderStatus* pStatus = &pReader->status;
  pStatus->pTableIter = taosHashIterate(pStatus->pTableMap, NULL);
H
Hongze Cheng 已提交
3721

3722 3723 3724 3725 3726
  while (pStatus->pTableIter != NULL) {
    STableBlockScanInfo* pBlockScanInfo = pStatus->pTableIter;

    STbData* d = NULL;
    if (pReader->pTsdb->mem != NULL) {
H
Hongze Cheng 已提交
3727
      d = tsdbGetTbDataFromMemTable(pReader->pReadSnap->pMem, pReader->suid, pBlockScanInfo->uid);
3728 3729 3730 3731 3732 3733 3734
      if (d != NULL) {
        rows += tsdbGetNRowsInTbData(d);
      }
    }

    STbData* di = NULL;
    if (pReader->pTsdb->imem != NULL) {
H
Hongze Cheng 已提交
3735
      di = tsdbGetTbDataFromMemTable(pReader->pReadSnap->pIMem, pReader->suid, pBlockScanInfo->uid);
3736 3737 3738 3739 3740 3741 3742 3743
      if (di != NULL) {
        rows += tsdbGetNRowsInTbData(di);
      }
    }

    // current table is exhausted, let's try the next table
    pStatus->pTableIter = taosHashIterate(pStatus->pTableMap, pStatus->pTableIter);
  }
H
Hongze Cheng 已提交
3744

H
refact  
Hongze Cheng 已提交
3745
  return rows;
H
Hongze Cheng 已提交
3746
}
D
dapan1121 已提交
3747

L
Liu Jicong 已提交
3748
int32_t tsdbGetTableSchema(SVnode* pVnode, int64_t uid, STSchema** pSchema, int64_t* suid) {
D
dapan1121 已提交
3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760
  int32_t sversion = 1;

  SMetaReader mr = {0};
  metaReaderInit(&mr, pVnode->pMeta, 0);
  int32_t code = metaGetTableEntryByUid(&mr, uid);
  if (code != TSDB_CODE_SUCCESS) {
    terrno = TSDB_CODE_TDB_INVALID_TABLE_ID;
    metaReaderClear(&mr);
    return terrno;
  }

  *suid = 0;
L
Liu Jicong 已提交
3761

D
dapan1121 已提交
3762
  if (mr.me.type == TSDB_CHILD_TABLE) {
D
dapan1121 已提交
3763
    tDecoderClear(&mr.coder);
D
dapan1121 已提交
3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778
    *suid = mr.me.ctbEntry.suid;
    code = metaGetTableEntryByUid(&mr, *suid);
    if (code != TSDB_CODE_SUCCESS) {
      terrno = TSDB_CODE_TDB_INVALID_TABLE_ID;
      metaReaderClear(&mr);
      return terrno;
    }
    sversion = mr.me.stbEntry.schemaRow.version;
  } else {
    ASSERT(mr.me.type == TSDB_NORMAL_TABLE);
    sversion = mr.me.ntbEntry.schemaRow.version;
  }

  metaReaderClear(&mr);
  *pSchema = metaGetTbTSchema(pVnode->pMeta, uid, sversion);
L
Liu Jicong 已提交
3779

D
dapan1121 已提交
3780 3781
  return TSDB_CODE_SUCCESS;
}
H
Hongze Cheng 已提交
3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811

int32_t tsdbTakeReadSnap(STsdb* pTsdb, STsdbReadSnap** ppSnap) {
  int32_t code = 0;

  // alloc
  *ppSnap = (STsdbReadSnap*)taosMemoryCalloc(1, sizeof(STsdbReadSnap));
  if (*ppSnap == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

  // lock
  code = taosThreadRwlockRdlock(&pTsdb->rwLock);
  if (code) {
    code = TAOS_SYSTEM_ERROR(code);
    goto _exit;
  }

  // take snapshot
  (*ppSnap)->pMem = pTsdb->mem;
  (*ppSnap)->pIMem = pTsdb->imem;

  if ((*ppSnap)->pMem) {
    tsdbRefMemTable((*ppSnap)->pMem);
  }

  if ((*ppSnap)->pIMem) {
    tsdbRefMemTable((*ppSnap)->pIMem);
  }

H
Hongze Cheng 已提交
3812
  // fs
H
Hongze Cheng 已提交
3813 3814 3815 3816 3817
  code = tsdbFSRef(pTsdb, &(*ppSnap)->fs);
  if (code) {
    taosThreadRwlockUnlock(&pTsdb->rwLock);
    goto _exit;
  }
H
Hongze Cheng 已提交
3818 3819 3820 3821 3822 3823 3824 3825

  // unlock
  code = taosThreadRwlockUnlock(&pTsdb->rwLock);
  if (code) {
    code = TAOS_SYSTEM_ERROR(code);
    goto _exit;
  }

S
Shengliang Guan 已提交
3826
  tsdbTrace("vgId:%d, take read snapshot", TD_VID(pTsdb->pVnode));
H
Hongze Cheng 已提交
3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840
_exit:
  return code;
}

void tsdbUntakeReadSnap(STsdb* pTsdb, STsdbReadSnap* pSnap) {
  if (pSnap) {
    if (pSnap->pMem) {
      tsdbUnrefMemTable(pSnap->pMem);
    }

    if (pSnap->pIMem) {
      tsdbUnrefMemTable(pSnap->pIMem);
    }

H
Hongze Cheng 已提交
3841
    tsdbFSUnref(pTsdb, &pSnap->fs);
H
Hongze Cheng 已提交
3842
    taosMemoryFree(pSnap);
H
Hongze Cheng 已提交
3843
  }
H
Hongze Cheng 已提交
3844

S
Shengliang Guan 已提交
3845
  tsdbTrace("vgId:%d, untake read snapshot", TD_VID(pTsdb->pVnode));
3846
}