scanoperator.c 86.1 KB
Newer Older
H
Haojun Liao 已提交
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/>.
 */

16
#include <vnode.h>
17
#include "filter.h"
18
#include "function.h"
19
#include "functionMgt.h"
H
Haojun Liao 已提交
20 21
#include "os.h"
#include "querynodes.h"
22
#include "systable.h"
23
#include "tglobal.h"
H
Haojun Liao 已提交
24
#include "tname.h"
25
#include "ttime.h"
H
Haojun Liao 已提交
26 27 28 29 30 31 32 33 34

#include "tdatablock.h"
#include "tmsg.h"

#include "executorimpl.h"
#include "query.h"
#include "tcompare.h"
#include "thash.h"
#include "ttypes.h"
35
#include "vnode.h"
H
Haojun Liao 已提交
36

wmmhello's avatar
wmmhello 已提交
37 38
#include "executorInt.h"

H
Haojun Liao 已提交
39
#define SET_REVERSE_SCAN_FLAG(_info) ((_info)->scanFlag = REVERSE_SCAN)
40
#define SWITCH_ORDER(n)              (((n) = ((n) == TSDB_ORDER_ASC) ? TSDB_ORDER_DESC : TSDB_ORDER_ASC))
41

42
static int32_t buildSysDbTableInfo(const SSysTableScanInfo* pInfo, int32_t capacity);
L
Liu Jicong 已提交
43 44
static int32_t buildDbTableInfoBlock(const SSDataBlock* p, const SSysTableMeta* pSysDbTableMeta, size_t size,
                                     const char* dbName);
45

46 47 48
static void addTagPseudoColumnData(SReadHandle* pHandle, SExprInfo* pPseudoExpr, int32_t numOfPseudoExpr,
                                   SSDataBlock* pBlock);
static bool processBlockWithProbability(const SSampleExecInfo* pInfo);
49

50
bool processBlockWithProbability(const SSampleExecInfo* pInfo) {
51 52 53 54 55 56 57 58 59 60 61 62
#if 0
  if (pInfo->sampleRatio == 1) {
    return true;
  }

  uint32_t val = taosRandR((uint32_t*) &pInfo->seed);
  return (val % ((uint32_t)(1/pInfo->sampleRatio))) == 0;
#else
  return true;
#endif
}

63
static void switchCtxOrder(SqlFunctionCtx* pCtx, int32_t numOfOutput) {
H
Haojun Liao 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  for (int32_t i = 0; i < numOfOutput; ++i) {
    SWITCH_ORDER(pCtx[i].order);
  }
}

static void setupQueryRangeForReverseScan(STableScanInfo* pTableScanInfo) {
#if 0
  int32_t numOfGroups = (int32_t)(GET_NUM_OF_TABLEGROUP(pRuntimeEnv));
  for(int32_t i = 0; i < numOfGroups; ++i) {
    SArray *group = GET_TABLEGROUP(pRuntimeEnv, i);
    SArray *tableKeyGroup = taosArrayGetP(pQueryAttr->tableGroupInfo.pGroupList, i);

    size_t t = taosArrayGetSize(group);
    for (int32_t j = 0; j < t; ++j) {
      STableQueryInfo *pCheckInfo = taosArrayGetP(group, j);
      updateTableQueryInfoForReverseScan(pCheckInfo);

      // update the last key in tableKeyInfo list, the tableKeyInfo is used to build the tsdbQueryHandle and decide
      // the start check timestamp of tsdbQueryHandle
//      STableKeyInfo *pTableKeyInfo = taosArrayGet(tableKeyGroup, j);
//      pTableKeyInfo->lastKey = pCheckInfo->lastKey;
//
//      assert(pCheckInfo->pTable == pTableKeyInfo->pTable);
    }
  }
#endif
}

92 93 94 95 96 97 98 99 100
static void getNextTimeWindow(SInterval* pInterval, STimeWindow* tw, int32_t order) {
  int32_t factor = GET_FORWARD_DIRECTION_FACTOR(order);
  if (pInterval->intervalUnit != 'n' && pInterval->intervalUnit != 'y') {
    tw->skey += pInterval->sliding * factor;
    tw->ekey = tw->skey + pInterval->interval - 1;
    return;
  }

  int64_t key = tw->skey, interval = pInterval->interval;
101
  // convert key to second
102 103 104 105 106 107 108
  key = convertTimePrecision(key, pInterval->precision, TSDB_TIME_PRECISION_MILLI) / 1000;

  if (pInterval->intervalUnit == 'y') {
    interval *= 12;
  }

  struct tm tm;
109
  time_t    t = (time_t)key;
110 111 112 113 114
  taosLocalTime(&t, &tm);

  int mon = (int)(tm.tm_year * 12 + tm.tm_mon + interval * factor);
  tm.tm_year = mon / 12;
  tm.tm_mon = mon % 12;
wafwerar's avatar
wafwerar 已提交
115
  tw->skey = convertTimePrecision((int64_t)taosMktime(&tm) * 1000LL, TSDB_TIME_PRECISION_MILLI, pInterval->precision);
116 117 118 119

  mon = (int)(mon + interval);
  tm.tm_year = mon / 12;
  tm.tm_mon = mon % 12;
wafwerar's avatar
wafwerar 已提交
120
  tw->ekey = convertTimePrecision((int64_t)taosMktime(&tm) * 1000LL, TSDB_TIME_PRECISION_MILLI, pInterval->precision);
121 122 123 124

  tw->ekey -= 1;
}

125
static bool overlapWithTimeWindow(SInterval* pInterval, SDataBlockInfo* pBlockInfo, int32_t order) {
126 127 128 129 130 131 132
  STimeWindow w = {0};

  // 0 by default, which means it is not a interval operator of the upstream operator.
  if (pInterval->interval == 0) {
    return false;
  }

133
  if (order == TSDB_ORDER_ASC) {
134
    getAlignQueryTimeWindow(pInterval, pInterval->precision, pBlockInfo->window.skey, &w);
135 136 137 138 139 140
    assert(w.ekey >= pBlockInfo->window.skey);

    if (w.ekey < pBlockInfo->window.ekey) {
      return true;
    }

141 142
    while (1) {
      getNextTimeWindow(pInterval, &w, order);
143 144 145 146 147 148 149 150 151 152
      if (w.skey > pBlockInfo->window.ekey) {
        break;
      }

      assert(w.ekey > pBlockInfo->window.ekey);
      if (w.skey <= pBlockInfo->window.ekey && w.skey > pBlockInfo->window.skey) {
        return true;
      }
    }
  } else {
153 154 155 156 157 158 159
    getAlignQueryTimeWindow(pInterval, pInterval->precision, pBlockInfo->window.ekey, &w);
    assert(w.skey <= pBlockInfo->window.ekey);

    if (w.skey > pBlockInfo->window.skey) {
      return true;
    }

160
    while (1) {
161 162 163 164 165 166 167 168 169 170
      getNextTimeWindow(pInterval, &w, order);
      if (w.ekey < pBlockInfo->window.skey) {
        break;
      }

      assert(w.skey < pBlockInfo->window.skey);
      if (w.ekey < pBlockInfo->window.ekey && w.ekey >= pBlockInfo->window.skey) {
        return true;
      }
    }
171 172 173 174 175
  }

  return false;
}

L
Liu Jicong 已提交
176 177
static int32_t loadDataBlock(SOperatorInfo* pOperator, STableScanInfo* pTableScanInfo, SSDataBlock* pBlock,
                             uint32_t* status) {
178
  SExecTaskInfo*  pTaskInfo = pOperator->pTaskInfo;
179 180
  STableScanInfo* pInfo = pOperator->info;

181
  SFileBlockLoadRecorder* pCost = &pTableScanInfo->readRecorder;
H
Haojun Liao 已提交
182 183

  pCost->totalBlocks += 1;
184
  pCost->totalRows += pBlock->info.rows;
H
Haojun Liao 已提交
185

186
  *status = pInfo->dataBlockLoadFlag;
187 188
  if (pTableScanInfo->pFilterNode != NULL ||
      overlapWithTimeWindow(&pTableScanInfo->interval, &pBlock->info, pTableScanInfo->cond.order)) {
189 190 191 192
    (*status) = FUNC_DATA_REQUIRED_DATA_LOAD;
  }

  SDataBlockInfo* pBlockInfo = &pBlock->info;
193
  taosMemoryFreeClear(pBlock->pBlockAgg);
194 195

  if (*status == FUNC_DATA_REQUIRED_FILTEROUT) {
196 197
    qDebug("%s data block filter out, brange:%" PRId64 "-%" PRId64 ", rows:%d", GET_TASKID(pTaskInfo),
           pBlockInfo->window.skey, pBlockInfo->window.ekey, pBlockInfo->rows);
198 199 200
    pCost->filterOutBlocks += 1;
    return TSDB_CODE_SUCCESS;
  } else if (*status == FUNC_DATA_REQUIRED_NOT_LOAD) {
201 202
    qDebug("%s data block skipped, brange:%" PRId64 "-%" PRId64 ", rows:%d", GET_TASKID(pTaskInfo),
           pBlockInfo->window.skey, pBlockInfo->window.ekey, pBlockInfo->rows);
203
    pCost->skipBlocks += 1;
204 205

    // clear all data in pBlock that are set when handing the previous block
206
    for (int32_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); ++i) {
207 208 209 210
      SColumnInfoData* pcol = taosArrayGet(pBlock->pDataBlock, i);
      pcol->pData = NULL;
    }

211 212 213 214
    return TSDB_CODE_SUCCESS;
  } else if (*status == FUNC_DATA_REQUIRED_STATIS_LOAD) {
    pCost->loadBlockStatis += 1;

L
Liu Jicong 已提交
215
    bool             allColumnsHaveAgg = true;
216
    SColumnDataAgg** pColAgg = NULL;
217
    tsdbRetrieveDataBlockStatisInfo(pTableScanInfo->dataReader, &pColAgg, &allColumnsHaveAgg);
218

219
    if (allColumnsHaveAgg == true) {
220
      int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
221 222

      // todo create this buffer during creating operator
223 224 225 226
      if (pBlock->pBlockAgg == NULL) {
        pBlock->pBlockAgg = taosMemoryCalloc(numOfCols, POINTER_BYTES);
      }

227
      for (int32_t i = 0; i < taosArrayGetSize(pTableScanInfo->pColMatchInfo); ++i) {
228 229 230 231 232 233
        SColMatchInfo* pColMatchInfo = taosArrayGet(pTableScanInfo->pColMatchInfo, i);
        if (!pColMatchInfo->output) {
          continue;
        }
        pBlock->pBlockAgg[pColMatchInfo->targetSlotId] = pColAgg[i];
      }
H
Haojun Liao 已提交
234

235
      return TSDB_CODE_SUCCESS;
236
    } else {  // failed to load the block sma data, data block statistics does not exist, load data block instead
H
Haojun Liao 已提交
237
      *status = FUNC_DATA_REQUIRED_DATA_LOAD;
238
    }
H
Haojun Liao 已提交
239
  }
240

241
  ASSERT(*status == FUNC_DATA_REQUIRED_DATA_LOAD);
242

H
Haojun Liao 已提交
243 244 245 246 247 248 249 250 251 252
  // todo filter data block according to the block sma data firstly
#if 0
  if (!doFilterByBlockStatistics(pBlock->pBlockStatis, pTableScanInfo->pCtx, pBlockInfo->rows)) {
    pCost->filterOutBlocks += 1;
    qDebug("%s data block filter out, brange:%" PRId64 "-%" PRId64 ", rows:%d", GET_TASKID(pTaskInfo), pBlockInfo->window.skey,
           pBlockInfo->window.ekey, pBlockInfo->rows);
    (*status) = FUNC_DATA_REQUIRED_FILTEROUT;
    return TSDB_CODE_SUCCESS;
  }
#endif
H
Haojun Liao 已提交
253

H
Haojun Liao 已提交
254 255
  pCost->totalCheckedRows += pBlock->info.rows;
  pCost->loadBlocks += 1;
256

H
Haojun Liao 已提交
257 258 259
  SArray* pCols = tsdbRetrieveDataBlock(pTableScanInfo->dataReader, NULL);
  if (pCols == NULL) {
    return terrno;
H
Haojun Liao 已提交
260 261
  }

262
  relocateColumnData(pBlock, pTableScanInfo->pColMatchInfo, pCols, true);
263 264

  // currently only the tbname pseudo column
265
  if (pTableScanInfo->pseudoSup.numOfExprs > 0) {
266
    SExprSupp* pSup = &pTableScanInfo->pseudoSup;
267
    addTagPseudoColumnData(&pTableScanInfo->readHandle, pSup->pExprInfo, pSup->numOfExprs, pBlock);
268 269
  }

270
  int64_t st = taosGetTimestampMs();
271
  doFilter(pTableScanInfo->pFilterNode, pBlock);
272

273 274 275
  int64_t et = taosGetTimestampMs();
  pTableScanInfo->readRecorder.filterTime += (et - st);

276 277
  if (pBlock->info.rows == 0) {
    pCost->filterOutBlocks += 1;
278 279
    qDebug("%s data block filter out, brange:%" PRId64 "-%" PRId64 ", rows:%d", GET_TASKID(pTaskInfo),
           pBlockInfo->window.skey, pBlockInfo->window.ekey, pBlockInfo->rows);
280 281
  }

H
Haojun Liao 已提交
282 283 284
  return TSDB_CODE_SUCCESS;
}

285
static void prepareForDescendingScan(STableScanInfo* pTableScanInfo, SqlFunctionCtx* pCtx, int32_t numOfOutput) {
H
Haojun Liao 已提交
286 287 288
  SET_REVERSE_SCAN_FLAG(pTableScanInfo);

  switchCtxOrder(pCtx, numOfOutput);
289
  //  setupQueryRangeForReverseScan(pTableScanInfo);
H
Haojun Liao 已提交
290

291
  pTableScanInfo->cond.order = TSDB_ORDER_DESC;
292 293 294 295
  for (int32_t i = 0; i < pTableScanInfo->cond.numOfTWindows; ++i) {
    STimeWindow* pTWindow = &pTableScanInfo->cond.twindows[i];
    TSWAP(pTWindow->skey, pTWindow->ekey);
  }
296 297 298

  SQueryTableDataCond* pCond = &pTableScanInfo->cond;
  taosqsort(pCond->twindows, pCond->numOfTWindows, sizeof(STimeWindow), pCond, compareTimeWindow);
H
Haojun Liao 已提交
299 300
}

301 302
void addTagPseudoColumnData(SReadHandle* pHandle, SExprInfo* pPseudoExpr, int32_t numOfPseudoExpr,
                            SSDataBlock* pBlock) {
303
  // currently only the tbname pseudo column
304
  if (numOfPseudoExpr == 0) {
305 306 307 308
    return;
  }

  SMetaReader mr = {0};
309
  metaReaderInit(&mr, pHandle->meta, 0);
310 311
  metaGetTableEntryByUid(&mr, pBlock->info.uid);

312 313
  for (int32_t j = 0; j < numOfPseudoExpr; ++j) {
    SExprInfo* pExpr = &pPseudoExpr[j];
314 315 316 317

    int32_t dstSlotId = pExpr->base.resSchema.slotId;

    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, dstSlotId);
318

319
    colInfoDataEnsureCapacity(pColInfoData, pBlock->info.rows);
D
dapan1121 已提交
320
    colInfoDataCleanup(pColInfoData, pBlock->info.rows);
321 322 323 324 325

    int32_t functionId = pExpr->pExpr->_function.functionId;

    // this is to handle the tbname
    if (fmIsScanPseudoColumnFunc(functionId)) {
326
      setTbNameColData(pHandle->meta, pBlock, pColInfoData, functionId);
327
    } else {  // these are tags
wmmhello's avatar
wmmhello 已提交
328 329
      STagVal tagVal = {0};
      tagVal.cid = pExpr->base.pParam[0].pCol->colId;
330
      const char* p = metaGetTableTagVal(&mr.me, pColInfoData->info.type, &tagVal);
wmmhello's avatar
wmmhello 已提交
331

332 333 334 335
      char* data = NULL;
      if (pColInfoData->info.type != TSDB_DATA_TYPE_JSON && p != NULL) {
        data = tTagValToData((const STagVal*)p, false);
      } else {
wmmhello's avatar
wmmhello 已提交
336
        data = (char*)p;
wmmhello's avatar
wmmhello 已提交
337
      }
338

339
      for (int32_t i = 0; i < pBlock->info.rows; ++i) {
L
Liu Jicong 已提交
340 341
        colDataAppend(pColInfoData, i, data,
                      (data == NULL) || (pColInfoData->info.type == TSDB_DATA_TYPE_JSON && tTagIsJsonNull(data)));
342
      }
343

344 345
      if (data && (pColInfoData->info.type != TSDB_DATA_TYPE_JSON) && p != NULL &&
          IS_VAR_DATA_TYPE(((const STagVal*)p)->type)) {
wmmhello's avatar
wmmhello 已提交
346
        taosMemoryFree(data);
wmmhello's avatar
wmmhello 已提交
347
      }
348 349 350 351 352 353
    }
  }

  metaReaderClear(&mr);
}

354 355 356 357
void setTbNameColData(void* pMeta, const SSDataBlock* pBlock, SColumnInfoData* pColInfoData, int32_t functionId) {
  struct SScalarFuncExecFuncs fpSet = {0};
  fmGetScalarFuncExecFuncs(functionId, &fpSet);

358 359
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_BIGINT, sizeof(uint64_t), 1);
  colInfoDataEnsureCapacity(&infoData, 1);
360

361
  colDataAppendInt64(&infoData, 0, (int64_t*)&pBlock->info.uid);
362
  SScalarParam srcParam = {.numOfRows = pBlock->info.rows, .param = pMeta, .columnData = &infoData};
363 364 365 366 367

  SScalarParam param = {.columnData = pColInfoData};
  fpSet.process(&srcParam, 1, &param);
}

368
static SSDataBlock* doTableScanImpl(SOperatorInfo* pOperator) {
H
Haojun Liao 已提交
369
  STableScanInfo* pTableScanInfo = pOperator->info;
370
  SExecTaskInfo*  pTaskInfo = pOperator->pTaskInfo;
L
Liu Jicong 已提交
371
  SSDataBlock*    pBlock = pTableScanInfo->pResBlock;
H
Haojun Liao 已提交
372

373 374
  int64_t st = taosGetTimestampUs();

375
  while (tsdbNextDataBlock(pTableScanInfo->dataReader)) {
376 377
    if (isTaskKilled(pTaskInfo)) {
      longjmp(pTaskInfo->env, TSDB_CODE_TSC_QUERY_CANCELLED);
378
    }
H
Haojun Liao 已提交
379

380 381 382 383 384 385
    // process this data block based on the probabilities
    bool processThisBlock = processBlockWithProbability(&pTableScanInfo->sample);
    if (!processThisBlock) {
      continue;
    }

386 387 388 389 390 391 392 393
    blockDataCleanup(pBlock);

    SDataBlockInfo binfo = pBlock->info;
    tsdbRetrieveDataBlockInfo(pTableScanInfo->dataReader, &binfo);

    binfo.capacity = binfo.rows;
    blockDataEnsureCapacity(pBlock, binfo.rows);
    pBlock->info = binfo;
H
Haojun Liao 已提交
394

395 396 397 398 399 400
    uint32_t status = 0;
    int32_t  code = loadDataBlock(pOperator, pTableScanInfo, pBlock, &status);
    //    int32_t  code = loadDataBlockOnDemand(pOperator->pRuntimeEnv, pTableScanInfo, pBlock, &status);
    if (code != TSDB_CODE_SUCCESS) {
      longjmp(pOperator->pTaskInfo->env, code);
    }
401

402 403 404
    // current block is filter out according to filter condition, continue load the next block
    if (status == FUNC_DATA_REQUIRED_FILTEROUT || pBlock->info.rows == 0) {
      continue;
405
    }
406

407
    uint64_t* groupId = taosHashGet(pTaskInfo->tableqinfoList.map, &pBlock->info.uid, sizeof(int64_t));
wmmhello's avatar
wmmhello 已提交
408 409 410 411
    if (groupId) {
      pBlock->info.groupId = *groupId;
    }

412 413 414 415 416
    pOperator->resultInfo.totalRows = pTableScanInfo->readRecorder.totalRows;
    pTableScanInfo->readRecorder.elapsedTime += (taosGetTimestampUs() - st) / 1000.0;

    pOperator->cost.totalCost = pTableScanInfo->readRecorder.elapsedTime;
    return pBlock;
H
Haojun Liao 已提交
417 418 419 420
  }
  return NULL;
}

wmmhello's avatar
wmmhello 已提交
421
static SSDataBlock* doTableScanGroup(SOperatorInfo* pOperator) {
H
Haojun Liao 已提交
422 423 424 425
  STableScanInfo* pTableScanInfo = pOperator->info;
  SExecTaskInfo*  pTaskInfo = pOperator->pTaskInfo;

  // The read handle is not initialized yet, since no qualified tables exists
426
  if (pTableScanInfo->dataReader == NULL || pOperator->status == OP_EXEC_DONE) {
H
Haojun Liao 已提交
427 428 429
    return NULL;
  }

430 431
  // do the ascending order traverse in the first place.
  while (pTableScanInfo->scanTimes < pTableScanInfo->scanInfo.numOfAsc) {
432 433 434 435 436 437 438 439 440
    while (pTableScanInfo->curTWinIdx < pTableScanInfo->cond.numOfTWindows) {
      SSDataBlock* p = doTableScanImpl(pOperator);
      if (p != NULL) {
        return p;
      }
      pTableScanInfo->curTWinIdx += 1;
      if (pTableScanInfo->curTWinIdx < pTableScanInfo->cond.numOfTWindows) {
        tsdbResetReadHandle(pTableScanInfo->dataReader, &pTableScanInfo->cond, pTableScanInfo->curTWinIdx);
      }
H
Haojun Liao 已提交
441 442
    }

443
    pTableScanInfo->scanTimes += 1;
444

445
    if (pTableScanInfo->scanTimes < pTableScanInfo->scanInfo.numOfAsc) {
446 447
      setTaskStatus(pTaskInfo, TASK_NOT_COMPLETED);
      pTableScanInfo->scanFlag = REPEAT_SCAN;
448 449 450 451 452
      qDebug("%s start to repeat ascending order scan data blocks due to query func required", GET_TASKID(pTaskInfo));
      for (int32_t i = 0; i < pTableScanInfo->cond.numOfTWindows; ++i) {
        STimeWindow* pWin = &pTableScanInfo->cond.twindows[i];
        qDebug("%s\t qrange:%" PRId64 "-%" PRId64, GET_TASKID(pTaskInfo), pWin->skey, pWin->ekey);
      }
453 454 455
      // do prepare for the next round table scan operation
      tsdbResetReadHandle(pTableScanInfo->dataReader, &pTableScanInfo->cond, 0);
      pTableScanInfo->curTWinIdx = 0;
H
Haojun Liao 已提交
456
    }
457
  }
H
Haojun Liao 已提交
458

459
  int32_t total = pTableScanInfo->scanInfo.numOfAsc + pTableScanInfo->scanInfo.numOfDesc;
460
  if (pTableScanInfo->scanTimes < total) {
461 462
    if (pTableScanInfo->cond.order == TSDB_ORDER_ASC) {
      prepareForDescendingScan(pTableScanInfo, pTableScanInfo->pCtx, pTableScanInfo->numOfOutput);
463 464
      tsdbResetReadHandle(pTableScanInfo->dataReader, &pTableScanInfo->cond, 0);
      pTableScanInfo->curTWinIdx = 0;
465
    }
H
Haojun Liao 已提交
466

467 468 469 470 471
    qDebug("%s start to descending order scan data blocks due to query func required", GET_TASKID(pTaskInfo));
    for (int32_t i = 0; i < pTableScanInfo->cond.numOfTWindows; ++i) {
      STimeWindow* pWin = &pTableScanInfo->cond.twindows[i];
      qDebug("%s\t qrange:%" PRId64 "-%" PRId64, GET_TASKID(pTaskInfo), pWin->skey, pWin->ekey);
    }
472

473
    while (pTableScanInfo->scanTimes < total) {
474 475 476 477 478 479 480 481 482
      while (pTableScanInfo->curTWinIdx < pTableScanInfo->cond.numOfTWindows) {
        SSDataBlock* p = doTableScanImpl(pOperator);
        if (p != NULL) {
          return p;
        }
        pTableScanInfo->curTWinIdx += 1;
        if (pTableScanInfo->curTWinIdx < pTableScanInfo->cond.numOfTWindows) {
          tsdbResetReadHandle(pTableScanInfo->dataReader, &pTableScanInfo->cond, pTableScanInfo->curTWinIdx);
        }
483
      }
H
Haojun Liao 已提交
484

485
      pTableScanInfo->scanTimes += 1;
H
Haojun Liao 已提交
486

487
      if (pTableScanInfo->scanTimes < total) {
488 489
        setTaskStatus(pTaskInfo, TASK_NOT_COMPLETED);
        pTableScanInfo->scanFlag = REPEAT_SCAN;
H
Haojun Liao 已提交
490

491 492
        qDebug("%s start to repeat descending order scan data blocks due to query func required",
               GET_TASKID(pTaskInfo));
493 494 495 496
        for (int32_t i = 0; i < pTableScanInfo->cond.numOfTWindows; ++i) {
          STimeWindow* pWin = &pTableScanInfo->cond.twindows[i];
          qDebug("%s\t qrange:%" PRId64 "-%" PRId64, GET_TASKID(pTaskInfo), pWin->skey, pWin->ekey);
        }
497
        tsdbResetReadHandle(pTableScanInfo->dataReader, &pTableScanInfo->cond, 0);
498
        pTableScanInfo->curTWinIdx = 0;
499
      }
H
Haojun Liao 已提交
500 501 502
    }
  }

wmmhello's avatar
wmmhello 已提交
503 504 505 506 507 508 509 510 511
  return NULL;
}

static SSDataBlock* doTableScan(SOperatorInfo* pOperator) {
  STableScanInfo* pInfo = pOperator->info;
  SExecTaskInfo*  pTaskInfo = pOperator->pTaskInfo;

  if(pInfo->currentGroupId == -1){
    pInfo->currentGroupId++;
wmmhello's avatar
wmmhello 已提交
512
    if (pInfo->currentGroupId >= taosArrayGetSize(pTaskInfo->tableqinfoList.pGroupList)) {
wmmhello's avatar
wmmhello 已提交
513
      setTaskStatus(pTaskInfo, TASK_COMPLETED);
wmmhello's avatar
wmmhello 已提交
514 515
      return NULL;
    }
wmmhello's avatar
wmmhello 已提交
516
    SArray *tableList = taosArrayGetP(pTaskInfo->tableqinfoList.pGroupList, pInfo->currentGroupId);
wmmhello's avatar
wmmhello 已提交
517
    tsdbCleanupReadHandle(pInfo->dataReader);
wmmhello's avatar
wmmhello 已提交
518 519 520 521 522 523 524 525 526 527 528
    tsdbReaderT* pReader = tsdbReaderOpen(pInfo->readHandle.vnode, &pInfo->cond, tableList, pInfo->queryId, pInfo->taskId);
    pInfo->dataReader = pReader;
  }

  SSDataBlock* result = doTableScanGroup(pOperator);
  if(result){
    return result;
  }

  pInfo->currentGroupId++;
  if (pInfo->currentGroupId >= taosArrayGetSize(pTaskInfo->tableqinfoList.pGroupList)) {
wmmhello's avatar
wmmhello 已提交
529
    setTaskStatus(pTaskInfo, TASK_COMPLETED);
wmmhello's avatar
wmmhello 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
    return NULL;
  }

  SArray *tableList = taosArrayGetP(pTaskInfo->tableqinfoList.pGroupList, pInfo->currentGroupId);
  tsdbSetTableList(pInfo->dataReader, tableList);

  tsdbResetReadHandle(pInfo->dataReader, &pInfo->cond, 0);
  pInfo->curTWinIdx = 0;
  pInfo->scanTimes = 0;

  result = doTableScanGroup(pOperator);
  if(result){
    return result;
  }

wmmhello's avatar
wmmhello 已提交
545
  setTaskStatus(pTaskInfo, TASK_COMPLETED);
546
  return NULL;
H
Haojun Liao 已提交
547 548
}

549 550
static int32_t getTableScannerExecInfo(struct SOperatorInfo* pOptr, void** pOptrExplain, uint32_t* len) {
  SFileBlockLoadRecorder* pRecorder = taosMemoryCalloc(1, sizeof(SFileBlockLoadRecorder));
551
  STableScanInfo*         pTableScanInfo = pOptr->info;
552 553 554 555 556 557
  *pRecorder = pTableScanInfo->readRecorder;
  *pOptrExplain = pRecorder;
  *len = sizeof(SFileBlockLoadRecorder);
  return 0;
}

558 559
static void destroyTableScanOperatorInfo(void* param, int32_t numOfOutput) {
  STableScanInfo* pTableScanInfo = (STableScanInfo*)param;
H
Haojun Liao 已提交
560
  blockDataDestroy(pTableScanInfo->pResBlock);
561
  cleanupQueryTableDataCond(&pTableScanInfo->cond);
H
Haojun Liao 已提交
562

563 564 565 566 567 568 569
  tsdbCleanupReadHandle(pTableScanInfo->dataReader);

  if (pTableScanInfo->pColMatchInfo != NULL) {
    taosArrayDestroy(pTableScanInfo->pColMatchInfo);
  }
}

wmmhello's avatar
wmmhello 已提交
570 571
SOperatorInfo* createTableScanOperatorInfo(STableScanPhysiNode* pTableScanNode, SReadHandle* readHandle,
                                           SExecTaskInfo* pTaskInfo, uint64_t queryId, uint64_t taskId) {
H
Haojun Liao 已提交
572 573 574
  STableScanInfo* pInfo = taosMemoryCalloc(1, sizeof(STableScanInfo));
  SOperatorInfo*  pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
  if (pInfo == NULL || pOperator == NULL) {
575
    goto _error;
H
Haojun Liao 已提交
576 577
  }

L
Liu Jicong 已提交
578
  // taosSsleep(20);
D
dapan1121 已提交
579

580
  SDataBlockDescNode* pDescNode = pTableScanNode->scan.node.pOutputDataBlockDesc;
581
  int32_t             numOfCols = 0;
582
  SArray* pColList = extractColMatchInfo(pTableScanNode->scan.pScanCols, pDescNode, &numOfCols, COL_MATCH_FROM_COL_ID);
L
Liu Jicong 已提交
583

584 585
  int32_t code = initQueryTableDataCond(&pInfo->cond, pTableScanNode);
  if (code != TSDB_CODE_SUCCESS) {
586
    goto _error;
587 588 589
  }

  if (pTableScanNode->scan.pScanPseudoCols != NULL) {
590
    SExprSupp* pSup = &pInfo->pseudoSup;
591 592
    pSup->pExprInfo = createExprInfo(pTableScanNode->scan.pScanPseudoCols, NULL, &pSup->numOfExprs);
    pSup->pCtx = createSqlFunctionCtx(pSup->pExprInfo, pSup->numOfExprs, &pSup->rowEntryInfoOffset);
593 594
  }

595
  pInfo->scanInfo = (SScanInfo){.numOfAsc = pTableScanNode->scanSeq[0], .numOfDesc = pTableScanNode->scanSeq[1]};
596
  //    pInfo->scanInfo = (SScanInfo){.numOfAsc = 0, .numOfDesc = 1}; // for debug purpose
597

598 599 600 601
  pInfo->readHandle = *readHandle;
  pInfo->interval = extractIntervalInfo(pTableScanNode);
  pInfo->sample.sampleRatio = pTableScanNode->ratio;
  pInfo->sample.seed = taosGetTimestampSec();
602

603
  pInfo->dataBlockLoadFlag = pTableScanNode->dataRequired;
604 605 606 607 608
  pInfo->pResBlock = createResDataBlock(pDescNode);
  pInfo->pFilterNode = pTableScanNode->scan.node.pConditions;
  pInfo->scanFlag = MAIN_SCAN;
  pInfo->pColMatchInfo = pColList;
  pInfo->curTWinIdx = 0;
wmmhello's avatar
wmmhello 已提交
609 610 611
  pInfo->queryId = queryId;
  pInfo->taskId = taskId;
  pInfo->currentGroupId = -1;
612 613

  pOperator->name = "TableScanOperator";  // for debug purpose
L
Liu Jicong 已提交
614
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN;
615 616 617
  pOperator->blocking = false;
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
618
  pOperator->exprSupp.numOfExprs = numOfCols;
619
  pOperator->pTaskInfo = pTaskInfo;
620

621 622
  pOperator->fpSet = createOperatorFpSet(operatorDummyOpenFn, doTableScan, NULL, NULL, destroyTableScanOperatorInfo,
                                         NULL, NULL, getTableScannerExecInfo);
623 624 625

  // for non-blocking operator, the open cost is always 0
  pOperator->cost.openCost = 0;
H
Haojun Liao 已提交
626
  return pOperator;
627

628
_error:
629 630 631 632 633
  taosMemoryFreeClear(pInfo);
  taosMemoryFreeClear(pOperator);

  pTaskInfo->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
  return NULL;
H
Haojun Liao 已提交
634 635
}

636
SOperatorInfo* createTableSeqScanOperatorInfo(void* pReadHandle, SExecTaskInfo* pTaskInfo) {
H
Haojun Liao 已提交
637
  STableScanInfo* pInfo = taosMemoryCalloc(1, sizeof(STableScanInfo));
L
Liu Jicong 已提交
638
  SOperatorInfo*  pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
H
Haojun Liao 已提交
639

L
Liu Jicong 已提交
640 641
  pInfo->dataReader = pReadHandle;
  //  pInfo->prevGroupId       = -1;
H
Haojun Liao 已提交
642

643
  pOperator->name = "TableSeqScanOperator";
H
Haojun Liao 已提交
644
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_TABLE_SEQ_SCAN;
645 646 647 648
  pOperator->blocking = false;
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
  pOperator->pTaskInfo = pTaskInfo;
H
Haojun Liao 已提交
649

650
  pOperator->fpSet = createOperatorFpSet(operatorDummyOpenFn, doTableScanImpl, NULL, NULL, NULL, NULL, NULL, NULL);
H
Haojun Liao 已提交
651 652 653
  return pOperator;
}

654 655
static int32_t doGetTableRowSize(void* pMeta, uint64_t uid) {
  int32_t rowLen = 0;
H
Haojun Liao 已提交
656

657
  SMetaReader mr = {0};
658 659
  metaReaderInit(&mr, pMeta, 0);
  metaGetTableEntryByUid(&mr, uid);
660 661
  if (mr.me.type == TSDB_SUPER_TABLE) {
    int32_t numOfCols = mr.me.stbEntry.schemaRow.nCols;
662
    for (int32_t i = 0; i < numOfCols; ++i) {
663 664 665 666 667 668 669
      rowLen += mr.me.stbEntry.schemaRow.pSchema[i].bytes;
    }
  } else if (mr.me.type == TSDB_CHILD_TABLE) {
    uint64_t suid = mr.me.ctbEntry.suid;
    metaGetTableEntryByUid(&mr, suid);
    int32_t numOfCols = mr.me.stbEntry.schemaRow.nCols;

670
    for (int32_t i = 0; i < numOfCols; ++i) {
671 672 673 674
      rowLen += mr.me.stbEntry.schemaRow.pSchema[i].bytes;
    }
  } else if (mr.me.type == TSDB_NORMAL_TABLE) {
    int32_t numOfCols = mr.me.ntbEntry.schemaRow.nCols;
675
    for (int32_t i = 0; i < numOfCols; ++i) {
676 677 678 679 680
      rowLen += mr.me.ntbEntry.schemaRow.pSchema[i].bytes;
    }
  }

  metaReaderClear(&mr);
681 682 683
  return rowLen;
}

684
static SSDataBlock* doBlockInfoScan(SOperatorInfo* pOperator) {
H
Haojun Liao 已提交
685 686 687 688
  if (pOperator->status == OP_EXEC_DONE) {
    return NULL;
  }

689
  SBlockDistInfo* pBlockScanInfo = pOperator->info;
H
Haojun Liao 已提交
690

691 692
  STableBlockDistInfo blockDistInfo = {.minRows = INT_MAX, .maxRows = INT_MIN};
  blockDistInfo.rowSize = doGetTableRowSize(pBlockScanInfo->readHandle.meta, pBlockScanInfo->uid);
H
Haojun Liao 已提交
693

694 695
  tsdbGetFileBlocksDistInfo(pBlockScanInfo->pHandle, &blockDistInfo);
  blockDistInfo.numOfInmemRows = (int32_t)tsdbGetNumOfRowsInMemTable(pBlockScanInfo->pHandle);
H
Haojun Liao 已提交
696

697
  SSDataBlock* pBlock = pBlockScanInfo->pResBlock;
H
Haojun Liao 已提交
698

699
  int32_t          slotId = pOperator->exprSupp.pExprInfo->base.resSchema.slotId;
700
  SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, slotId);
H
Haojun Liao 已提交
701

702
  int32_t len = tSerializeBlockDistInfo(NULL, 0, &blockDistInfo);
703
  char*   p = taosMemoryCalloc(1, len + VARSTR_HEADER_SIZE);
704 705 706
  tSerializeBlockDistInfo(varDataVal(p), len, &blockDistInfo);
  varDataSetLen(p, len);

707
  blockDataEnsureCapacity(pBlock, 1);
708 709
  colDataAppend(pColInfo, 0, p, false);
  taosMemoryFree(p);
H
Haojun Liao 已提交
710

711 712
  pBlock->info.rows = 1;

H
Haojun Liao 已提交
713 714 715 716
  pOperator->status = OP_EXEC_DONE;
  return pBlock;
}

717
static void destroyBlockDistScanOperatorInfo(void* param, int32_t numOfOutput) {
718
  SBlockDistInfo* pDistInfo = (SBlockDistInfo*)param;
719 720 721
  blockDataDestroy(pDistInfo->pResBlock);
}

722 723
SOperatorInfo* createDataBlockInfoScanOperator(void* dataReader, SReadHandle* readHandle, uint64_t uid,
                                               SBlockDistScanPhysiNode* pBlockScanNode, SExecTaskInfo* pTaskInfo) {
724
  SBlockDistInfo* pInfo = taosMemoryCalloc(1, sizeof(SBlockDistInfo));
725
  SOperatorInfo*  pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
H
Haojun Liao 已提交
726 727 728 729 730
  if (pInfo == NULL || pOperator == NULL) {
    pTaskInfo->code = TSDB_CODE_OUT_OF_MEMORY;
    goto _error;
  }

731
  pInfo->pHandle = dataReader;
732
  pInfo->readHandle = *readHandle;
733 734
  pInfo->uid = uid;
  pInfo->pResBlock = createResDataBlock(pBlockScanNode->node.pOutputDataBlockDesc);
735

736
  int32_t    numOfCols = 0;
737
  SExprInfo* pExprInfo = createExprInfo(pBlockScanNode->pScanPseudoCols, NULL, &numOfCols);
738
  int32_t    code = initExprSupp(&pOperator->exprSupp, pExprInfo, numOfCols);
739 740 741
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
H
Haojun Liao 已提交
742

743
  pOperator->name = "DataBlockDistScanOperator";
744
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_BLOCK_DIST_SCAN;
745 746 747
  pOperator->blocking = false;
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
748 749 750 751
  pOperator->pTaskInfo = pTaskInfo;

  pOperator->fpSet = createOperatorFpSet(operatorDummyOpenFn, doBlockInfoScan, NULL, NULL,
                                         destroyBlockDistScanOperatorInfo, NULL, NULL, NULL);
H
Haojun Liao 已提交
752 753
  return pOperator;

754
_error:
H
Haojun Liao 已提交
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
  taosMemoryFreeClear(pInfo);
  taosMemoryFreeClear(pOperator);
  return NULL;
}

static void doClearBufferedBlocks(SStreamBlockScanInfo* pInfo) {
  size_t total = taosArrayGetSize(pInfo->pBlockLists);

  pInfo->validBlockIndex = 0;
  for (int32_t i = 0; i < total; ++i) {
    SSDataBlock* p = taosArrayGetP(pInfo->pBlockLists, i);
    blockDataDestroy(p);
  }
  taosArrayClear(pInfo->pBlockLists);
}

H
Haojun Liao 已提交
771
static bool isSessionWindow(SStreamBlockScanInfo* pInfo) {
772
  return pInfo->sessionSup.parentType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SESSION;
5
54liuyao 已提交
773 774
}

H
Haojun Liao 已提交
775
static bool isStateWindow(SStreamBlockScanInfo* pInfo) {
776
  return pInfo->sessionSup.parentType == QUERY_NODE_PHYSICAL_PLAN_STREAM_STATE;
5
54liuyao 已提交
777
}
5
54liuyao 已提交
778

5
54liuyao 已提交
779 780
static bool prepareDataScan(SStreamBlockScanInfo* pInfo) {
  SSDataBlock* pSDB = pInfo->pUpdateRes;
781
  STimeWindow  win = {
L
Liu Jicong 已提交
782 783
       .skey = INT64_MIN,
       .ekey = INT64_MAX,
784
  };
5
54liuyao 已提交
785 786 787
  bool needRead = false;
  if (!isStateWindow(pInfo) && pInfo->updateResIndex < pSDB->info.rows) {
    SColumnInfoData* pColDataInfo = taosArrayGet(pSDB->pDataBlock, pInfo->primaryTsIndex);
788 789
    TSKEY*           tsCols = (TSKEY*)pColDataInfo->pData;
    SResultRowInfo   dumyInfo;
5
54liuyao 已提交
790
    dumyInfo.cur.pageId = -1;
5
54liuyao 已提交
791 792
    if (isSessionWindow(pInfo)) {
      SStreamAggSupporter* pAggSup = pInfo->sessionSup.pStreamAggSup;
793 794 795
      int64_t              gap = pInfo->sessionSup.gap;
      int32_t              winIndex = 0;
      SResultWindowInfo*   pCurWin =
L
Liu Jicong 已提交
796
          getSessionTimeWindow(pAggSup, tsCols[pInfo->updateResIndex], INT64_MIN, pSDB->info.groupId, gap, &winIndex);
5
54liuyao 已提交
797
      win = pCurWin->win;
798
      pInfo->updateResIndex +=
5
54liuyao 已提交
799
          updateSessionWindowInfo(pCurWin, tsCols, NULL, pSDB->info.rows, pInfo->updateResIndex, gap, NULL);
5
54liuyao 已提交
800
    } else {
801 802 803 804
      win = getActiveTimeWindow(NULL, &dumyInfo, tsCols[pInfo->updateResIndex], &pInfo->interval,
                                pInfo->interval.precision, NULL);
      pInfo->updateResIndex += getNumOfRowsInTimeWindow(&pSDB->info, tsCols, pInfo->updateResIndex, win.ekey,
                                                        binarySearchForKey, NULL, TSDB_ORDER_ASC);
5
54liuyao 已提交
805
    }
5
54liuyao 已提交
806 807 808 809 810
    needRead = true;
  } else if (isStateWindow(pInfo)) {
    SArray* pWins = pInfo->sessionSup.pStreamAggSup->pScanWindow;
    int32_t size = taosArrayGetSize(pWins);
    if (pInfo->scanWinIndex < size) {
811
      win = *(STimeWindow*)taosArrayGet(pWins, pInfo->scanWinIndex);
5
54liuyao 已提交
812 813 814 815 816 817 818 819
      pInfo->scanWinIndex++;
      needRead = true;
    } else {
      pInfo->scanWinIndex = 0;
      taosArrayClear(pWins);
    }
  }
  if (!needRead) {
5
54liuyao 已提交
820 821
    return false;
  }
L
Liu Jicong 已提交
822
  STableScanInfo* pTableScanInfo = pInfo->pSnapshotReadOp->info;
5
54liuyao 已提交
823 824
  pTableScanInfo->cond.twindows[0] = win;
  pTableScanInfo->curTWinIdx = 0;
wmmhello's avatar
wmmhello 已提交
825
//  tsdbResetReadHandle(pTableScanInfo->dataReader, &pTableScanInfo->cond, 0);
5
54liuyao 已提交
826
  pTableScanInfo->scanTimes = 0;
wmmhello's avatar
wmmhello 已提交
827
  pTableScanInfo->currentGroupId = -1;
5
54liuyao 已提交
828
  return true;
5
54liuyao 已提交
829 830
}

831
static void copyOneRow(SSDataBlock* dest, SSDataBlock* source, int32_t sourceRowId) {
832
  for (int32_t j = 0; j < taosArrayGetSize(source->pDataBlock); j++) {
833 834 835 836 837 838
    SColumnInfoData* pDestCol = (SColumnInfoData*)taosArrayGet(dest->pDataBlock, j);
    SColumnInfoData* pSourceCol = (SColumnInfoData*)taosArrayGet(source->pDataBlock, j);
    if (colDataIsNull_s(pSourceCol, sourceRowId)) {
      colDataAppendNULL(pDestCol, dest->info.rows);
    } else {
      colDataAppend(pDestCol, dest->info.rows, colDataGetData(pSourceCol, sourceRowId), false);
5
54liuyao 已提交
839 840
    }
  }
841
  dest->info.rows++;
5
54liuyao 已提交
842 843
}

844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
static uint64_t getGroupId(SOperatorInfo* pOperator, SSDataBlock* pBlock, int32_t rowId) {
  uint64_t* groupId = taosHashGet(pOperator->pTaskInfo->tableqinfoList.map, &pBlock->info.uid, sizeof(int64_t));
  if (groupId) {
    return *groupId;
  }
  return 0;
  /* Todo(liuyao) for partition by column
  recordNewGroupKeys(pTableScanInfo->pGroupCols, pTableScanInfo->pGroupColVals, pBlock, rowId);
  int32_t len = buildGroupKeys(pTableScanInfo->keyBuf, pTableScanInfo->pGroupColVals);
  uint64_t resId = 0;
  uint64_t* groupId = taosHashGet(pTableScanInfo->pGroupSet, pTableScanInfo->keyBuf, len);
  if (groupId) {
    return *groupId;
  } else if (len != 0) {
    resId = calcGroupId(pTableScanInfo->keyBuf, len);
    taosHashPut(pTableScanInfo->pGroupSet, pTableScanInfo->keyBuf, len, &resId, sizeof(uint64_t));
  }
  return resId;
  */
}

static SSDataBlock* doDataScan(SStreamBlockScanInfo* pInfo) {
  while (1) {
    SSDataBlock* pResult = NULL;
L
Liu Jicong 已提交
868
    pResult = doTableScan(pInfo->pSnapshotReadOp);
869 870 871
    if (pResult == NULL) {
      if (prepareDataScan(pInfo)) {
        // scan next window data
L
Liu Jicong 已提交
872
        pResult = doTableScan(pInfo->pSnapshotReadOp);
873 874 875 876 877
      }
    }
    if (!pResult) {
      return NULL;
    }
L
Liu Jicong 已提交
878

879 880
    if (pResult->info.groupId == pInfo->groupId) {
      return pResult;
5
54liuyao 已提交
881 882
    }
  }
883

L
Liu Jicong 已提交
884 885 886 887 888 889 890 891
  /* Todo(liuyao) for partition by column
    SSDataBlock* pBlock = createOneDataBlock(pResult, true);
    blockDataCleanup(pResult);
    for (int32_t i = 0; i < pBlock->info.rows; i++) {
      uint64_t id = getGroupId(pInfo->pOperatorDumy, pBlock, i);
      if (id == pInfo->groupId) {
        copyOneRow(pResult, pBlock, i);
      }
892
    }
L
Liu Jicong 已提交
893 894
    return pResult;
  */
895 896 897 898
}

static void setUpdateData(SStreamBlockScanInfo* pInfo, SSDataBlock* pBlock, SSDataBlock* pUpdateBlock) {
  blockDataCleanup(pUpdateBlock);
5
54liuyao 已提交
899
  int32_t size = taosArrayGetSize(pInfo->tsArray);
900
  if (pInfo->tsArrayIndex < size) {
5
54liuyao 已提交
901
    SColumnInfoData* pCol = (SColumnInfoData*)taosArrayGet(pUpdateBlock->pDataBlock, pInfo->primaryTsIndex);
5
54liuyao 已提交
902
    ASSERT(pCol->info.type == TSDB_DATA_TYPE_TIMESTAMP);
903
    blockDataEnsureCapacity(pUpdateBlock, size);
904 905

    int32_t rowId = *(int32_t*)taosArrayGet(pInfo->tsArray, pInfo->tsArrayIndex);
L
Liu Jicong 已提交
906
    pInfo->groupId = getGroupId(pInfo->pSnapshotReadOp, pBlock, rowId);
907
    int32_t i = 0;
L
Liu Jicong 已提交
908
    for (; i < size; i++) {
909
      rowId = *(int32_t*)taosArrayGet(pInfo->tsArray, i + pInfo->tsArrayIndex);
L
Liu Jicong 已提交
910
      uint64_t id = getGroupId(pInfo->pSnapshotReadOp, pBlock, rowId);
911 912
      if (pInfo->groupId != id) {
        break;
913
      }
914
      copyOneRow(pUpdateBlock, pBlock, rowId);
915
    }
916 917 918
    pUpdateBlock->info.rows = i;
    pInfo->tsArrayIndex += i;
    pUpdateBlock->info.groupId = pInfo->groupId;
5
54liuyao 已提交
919 920
    pUpdateBlock->info.type = STREAM_REPROCESS;
    blockDataUpdateTsWindow(pUpdateBlock, 0);
921 922 923 924
  }
  // all rows have same group id
  ASSERT(pInfo->tsArrayIndex >= size);
  if (size > 0 && pInfo->tsArrayIndex == size) {
5
54liuyao 已提交
925
    taosArrayClear(pInfo->tsArray);
5
54liuyao 已提交
926 927 928
  }
}

929 930 931 932 933 934 935 936 937 938 939
static void getUpdateDataBlock(SStreamBlockScanInfo* pInfo, bool invertible, SSDataBlock* pBlock,
                               SSDataBlock* pUpdateBlock) {
  SColumnInfoData* pColDataInfo = taosArrayGet(pBlock->pDataBlock, pInfo->primaryTsIndex);
  ASSERT(pColDataInfo->info.type == TSDB_DATA_TYPE_TIMESTAMP);
  TSKEY* ts = (TSKEY*)pColDataInfo->pData;
  for (int32_t rowId = 0; rowId < pBlock->info.rows; rowId++) {
    if (updateInfoIsUpdated(pInfo->pUpdateInfo, pBlock->info.uid, ts[rowId])) {
      taosArrayPush(pInfo->tsArray, &rowId);
    }
  }
  if (!pUpdateBlock) {
5
54liuyao 已提交
940
    taosArrayClear(pInfo->tsArray);
941
    return;
5
54liuyao 已提交
942
  }
943 944 945 946 947 948
  setUpdateData(pInfo, pBlock, pUpdateBlock);
  // Todo(liuyao) get from tsdb
  //  SSDataBlock* p = createOneDataBlock(pBlock, true);
  //  p->info.type = STREAM_INVERT;
  //  taosArrayClear(pInfo->tsArray);
  //  return p;
5
54liuyao 已提交
949 950
}

951
static SSDataBlock* doStreamBlockScan(SOperatorInfo* pOperator) {
H
Haojun Liao 已提交
952 953 954 955
  // NOTE: this operator does never check if current status is done or not
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
  SStreamBlockScanInfo* pInfo = pOperator->info;

956
  pTaskInfo->code = pOperator->fpSet._openFn(pOperator);
957
  if (pTaskInfo->code != TSDB_CODE_SUCCESS || pOperator->status == OP_EXEC_DONE) {
H
Haojun Liao 已提交
958 959 960
    return NULL;
  }

5
54liuyao 已提交
961
  size_t total = taosArrayGetSize(pInfo->pBlockLists);
962
  // TODO: refactor
H
Haojun Liao 已提交
963 964
  if (pInfo->blockType == STREAM_DATA_TYPE_SSDATA_BLOCK) {
    if (pInfo->validBlockIndex >= total) {
L
Liu Jicong 已提交
965
      /*doClearBufferedBlocks(pInfo);*/
966
      pOperator->status = OP_EXEC_DONE;
H
Haojun Liao 已提交
967 968 969
      return NULL;
    }

970
    int32_t      current = pInfo->validBlockIndex++;
971 972 973
    SSDataBlock* pBlock = taosArrayGetP(pInfo->pBlockLists, current);
    blockDataUpdateTsWindow(pBlock, 0);
    return pBlock;
L
Liu Jicong 已提交
974
  } else if (pInfo->blockType == STREAM_DATA_TYPE_SUBMIT_BLOCK) {
5
54liuyao 已提交
975 976 977 978 979 980
    if (pInfo->scanMode == STREAM_SCAN_FROM_RES) {
      blockDataDestroy(pInfo->pUpdateRes);
      pInfo->scanMode = STREAM_SCAN_FROM_READERHANDLE;
      return pInfo->pRes;
    } else if (pInfo->scanMode == STREAM_SCAN_FROM_UPDATERES) {
      pInfo->scanMode = STREAM_SCAN_FROM_DATAREADER;
5
54liuyao 已提交
981 982 983
      if (!isStateWindow(pInfo)) {
        prepareDataScan(pInfo);
      }
5
54liuyao 已提交
984
      return pInfo->pUpdateRes;
5
54liuyao 已提交
985
    } else {
986
      if (isStateWindow(pInfo) && taosArrayGetSize(pInfo->sessionSup.pStreamAggSup->pScanWindow) > 0) {
5
54liuyao 已提交
987 988 989 990 991 992 993
        pInfo->scanMode = STREAM_SCAN_FROM_DATAREADER;
        pInfo->updateResIndex = pInfo->pUpdateRes->info.rows;
        prepareDataScan(pInfo);
      }
      if (pInfo->scanMode == STREAM_SCAN_FROM_DATAREADER) {
        SSDataBlock* pSDB = doDataScan(pInfo);
        if (pSDB == NULL) {
994 995 996 997 998 999 1000 1001 1002
          setUpdateData(pInfo, pInfo->pRes, pInfo->pUpdateRes);
          if (pInfo->pUpdateRes->info.rows > 0) {
            if (!isStateWindow(pInfo)) {
              prepareDataScan(pInfo);
            }
            return pInfo->pUpdateRes;
          } else {
            pInfo->scanMode = STREAM_SCAN_FROM_READERHANDLE;
          }
5
54liuyao 已提交
1003 1004 1005 1006
        } else {
          getUpdateDataBlock(pInfo, true, pSDB, NULL);
          return pSDB;
        }
5
54liuyao 已提交
1007
      }
5
54liuyao 已提交
1008
    }
5
54liuyao 已提交
1009

H
Haojun Liao 已提交
1010 1011 1012
    SDataBlockInfo* pBlockInfo = &pInfo->pRes->info;
    blockDataCleanup(pInfo->pRes);

1013
    while (tqNextDataBlock(pInfo->streamBlockReader)) {
1014
      SSDataBlock block = {0};
L
Liu Jicong 已提交
1015 1016 1017
      uint64_t    groupId = 0;
      uint64_t    uid = 0;
      int32_t     numOfRows = 0;
1018

1019
      // todo refactor
1020
      int32_t code = tqRetrieveDataBlock(&block, pInfo->streamBlockReader, &groupId, &uid, &numOfRows);
H
Haojun Liao 已提交
1021

1022 1023 1024
      if (code != TSDB_CODE_SUCCESS || numOfRows == 0) {
        pTaskInfo->code = code;
        return NULL;
H
Haojun Liao 已提交
1025 1026
      }

1027 1028
      pInfo->pRes->info.groupId = groupId;
      pInfo->pRes->info.rows = numOfRows;
1029
      pInfo->pRes->info.uid = uid;
5
54liuyao 已提交
1030
      pInfo->pRes->info.type = STREAM_NORMAL;
1031
      pInfo->pRes->info.capacity = numOfRows;
H
Haojun Liao 已提交
1032

1033 1034 1035 1036
      // for generating rollup SMA result, each time is an independent time serie.
      // TODO temporarily used, when the statement of "partition by tbname" is ready, remove this
      if (pInfo->assignBlockUid) {
        pInfo->pRes->info.groupId = uid;
1037 1038
      } else {
        pInfo->pRes->info.groupId = groupId;
1039 1040
      }

1041 1042 1043 1044 1045
      uint64_t* groupIdPre = taosHashGet(pOperator->pTaskInfo->tableqinfoList.map, &uid, sizeof(int64_t));
      if (groupIdPre) {
        pInfo->pRes->info.groupId = *groupIdPre;
      }

1046
      // todo extract method
1047
      for (int32_t i = 0; i < taosArrayGetSize(pInfo->pColMatchInfo); ++i) {
1048
        SColMatchInfo* pColMatchInfo = taosArrayGet(pInfo->pColMatchInfo, i);
H
Haojun Liao 已提交
1049 1050 1051 1052
        if (!pColMatchInfo->output) {
          continue;
        }

1053
        bool colExists = false;
1054 1055
        for (int32_t j = 0; j < blockDataGetNumOfCols(&block); ++j) {
          SColumnInfoData* pResCol = bdGetColumnInfoData(&block, j);
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
          if (pResCol->info.colId == pColMatchInfo->colId) {
            taosArraySet(pInfo->pRes->pDataBlock, pColMatchInfo->targetSlotId, pResCol);
            colExists = true;
            break;
          }
        }

        // the required column does not exists in submit block, let's set it to be all null value
        if (!colExists) {
          SColumnInfoData* pDst = taosArrayGet(pInfo->pRes->pDataBlock, pColMatchInfo->targetSlotId);
          colDataAppendNNULL(pDst, 0, pBlockInfo->rows);
        }
H
Haojun Liao 已提交
1068 1069
      }

L
Liu Jicong 已提交
1070 1071 1072
      // TODO refactor @liao
      taosArrayDestroy(block.pDataBlock);

H
Haojun Liao 已提交
1073 1074
      if (pInfo->pRes->pDataBlock == NULL) {
        // TODO add log
1075
        pOperator->status = OP_EXEC_DONE;
H
Haojun Liao 已提交
1076 1077 1078
        pTaskInfo->code = terrno;
        return NULL;
      }
1079 1080 1081 1082 1083
      // currently only the tbname pseudo column
      if (pInfo->numOfPseudoExpr > 0) {
        addTagPseudoColumnData(&pInfo->readHandle, pInfo->pPseudoExpr, pInfo->numOfPseudoExpr, pInfo->pRes);
      }

1084
      doFilter(pInfo->pCondition, pInfo->pRes);
1085
      blockDataUpdateTsWindow(pInfo->pRes, pInfo->primaryTsIndex);
5
54liuyao 已提交
1086 1087 1088
      if (pBlockInfo->rows > 0) {
        break;
      }
H
Haojun Liao 已提交
1089 1090 1091 1092
    }

    // record the scan action.
    pInfo->numOfExec++;
1093
    pOperator->resultInfo.totalRows += pBlockInfo->rows;
H
Haojun Liao 已提交
1094

5
54liuyao 已提交
1095
    if (pBlockInfo->rows == 0) {
1096
      pOperator->status = OP_EXEC_DONE;
5
54liuyao 已提交
1097
    } else if (pInfo->pUpdateInfo) {
1098
      pInfo->tsArrayIndex = 0;
5
54liuyao 已提交
1099 1100 1101
      getUpdateDataBlock(pInfo, true, pInfo->pRes, pInfo->pUpdateRes);
      if (pInfo->pUpdateRes->info.rows > 0) {
        if (pInfo->pUpdateRes->info.type == STREAM_REPROCESS) {
5
54liuyao 已提交
1102 1103
          pInfo->updateResIndex = 0;
          pInfo->scanMode = STREAM_SCAN_FROM_UPDATERES;
5
54liuyao 已提交
1104
        } else if (pInfo->pUpdateRes->info.type == STREAM_INVERT) {
5
54liuyao 已提交
1105
          pInfo->scanMode = STREAM_SCAN_FROM_RES;
5
54liuyao 已提交
1106
          return pInfo->pUpdateRes;
5
54liuyao 已提交
1107
        }
5
54liuyao 已提交
1108
      }
1109 1110
    }

5
54liuyao 已提交
1111
    return (pBlockInfo->rows == 0) ? NULL : pInfo->pRes;
L
Liu Jicong 已提交
1112

L
Liu Jicong 已提交
1113
  } else if (pInfo->blockType == STREAM_DATA_TYPE_FROM_SNAPSHOT) {
L
Liu Jicong 已提交
1114
    SSDataBlock* pResult = doTableScan(pInfo->pSnapshotReadOp);
L
Liu Jicong 已提交
1115 1116
    return pResult && pResult->info.rows > 0 ? pResult : NULL;

L
Liu Jicong 已提交
1117 1118 1119
  } else {
    ASSERT(0);
    return NULL;
H
Haojun Liao 已提交
1120 1121 1122
  }
}

1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
static SArray* extractTableIdList(const STableListInfo* pTableGroupInfo) {
  SArray* tableIdList = taosArrayInit(4, sizeof(uint64_t));

  // Transfer the Array of STableKeyInfo into uid list.
  for (int32_t i = 0; i < taosArrayGetSize(pTableGroupInfo->pTableList); ++i) {
    STableKeyInfo* pkeyInfo = taosArrayGet(pTableGroupInfo->pTableList, i);
    taosArrayPush(tableIdList, &pkeyInfo->uid);
  }

  return tableIdList;
}

wmmhello's avatar
wmmhello 已提交
1135
SOperatorInfo* createStreamScanOperatorInfo(SReadHandle* pHandle,
1136
                                            STableScanPhysiNode* pTableScanNode, SExecTaskInfo* pTaskInfo,
wmmhello's avatar
wmmhello 已提交
1137
                                            STimeWindowAggSupp* pTwSup, uint64_t queryId, uint64_t taskId) {
H
Haojun Liao 已提交
1138 1139
  SStreamBlockScanInfo* pInfo = taosMemoryCalloc(1, sizeof(SStreamBlockScanInfo));
  SOperatorInfo*        pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
1140

H
Haojun Liao 已提交
1141 1142
  if (pInfo == NULL || pOperator == NULL) {
    terrno = TSDB_CODE_QRY_OUT_OF_MEMORY;
1143
    goto _error;
H
Haojun Liao 已提交
1144 1145
  }

1146 1147 1148
  SScanPhysiNode* pScanPhyNode = &pTableScanNode->scan;

  SDataBlockDescNode* pDescNode = pScanPhyNode->node.pOutputDataBlockDesc;
H
Haojun Liao 已提交
1149

1150
  int32_t numOfCols = 0;
1151
  pInfo->pColMatchInfo = extractColMatchInfo(pScanPhyNode->pScanCols, pDescNode, &numOfCols, COL_MATCH_FROM_COL_ID);
1152 1153 1154

  int32_t numOfOutput = taosArrayGetSize(pInfo->pColMatchInfo);
  SArray* pColIds = taosArrayInit(numOfOutput, sizeof(int16_t));
1155
  for (int32_t i = 0; i < numOfOutput; ++i) {
1156 1157 1158
    SColMatchInfo* id = taosArrayGet(pInfo->pColMatchInfo, i);

    int16_t colId = id->colId;
1159
    taosArrayPush(pColIds, &colId);
5
54liuyao 已提交
1160 1161 1162
    if (id->colId == pTableScanNode->tsColId) {
      pInfo->primaryTsIndex = id->targetSlotId;
    }
H
Haojun Liao 已提交
1163 1164 1165 1166
  }

  pInfo->pBlockLists = taosArrayInit(4, POINTER_BYTES);
  if (pInfo->pBlockLists == NULL) {
1167 1168
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _error;
H
Haojun Liao 已提交
1169 1170
  }

1171
  pInfo->tsArray = taosArrayInit(4, sizeof(int32_t));
5
54liuyao 已提交
1172
  if (pInfo->tsArray == NULL) {
1173
    goto _error;
5
54liuyao 已提交
1174 1175
  }

L
Liu Jicong 已提交
1176
  if (pHandle) {
wmmhello's avatar
wmmhello 已提交
1177
    SOperatorInfo*  pTableScanDummy = createTableScanOperatorInfo(pTableScanNode, pHandle, pTaskInfo, queryId, taskId);
L
Liu Jicong 已提交
1178 1179 1180 1181 1182 1183
    STableScanInfo* pSTInfo = (STableScanInfo*)pTableScanDummy->info;
    if (pSTInfo->interval.interval > 0) {
      pInfo->pUpdateInfo = updateInfoInitP(&pSTInfo->interval, pTwSup->waterMark);
    } else {
      pInfo->pUpdateInfo = NULL;
    }
L
Liu Jicong 已提交
1184
    pInfo->pSnapshotReadOp = pTableScanDummy;
L
Liu Jicong 已提交
1185 1186
    pInfo->interval = pSTInfo->interval;

L
Liu Jicong 已提交
1187 1188 1189 1190 1191
    pInfo->readHandle = *pHandle;
    ASSERT(pHandle->reader);
    pInfo->streamBlockReader = pHandle->reader;
    pInfo->tableUid = pScanPhyNode->uid;

L
Liu Jicong 已提交
1192 1193 1194 1195 1196 1197 1198 1199 1200
    // set the extract column id to streamHandle
    tqReadHandleSetColIdList((STqReadHandle*)pHandle->reader, pColIds);
    SArray* tableIdList = extractTableIdList(&pTaskInfo->tableqinfoList);
    int32_t code = tqReadHandleSetTbUidList(pHandle->reader, tableIdList);
    if (code != 0) {
      taosArrayDestroy(tableIdList);
      goto _error;
    }
    taosArrayDestroy(tableIdList);
5
54liuyao 已提交
1201 1202
  }

1203 1204 1205 1206 1207
  // create the pseduo columns info
  if (pTableScanNode->scan.pScanPseudoCols != NULL) {
    pInfo->pPseudoExpr = createExprInfo(pTableScanNode->scan.pScanPseudoCols, NULL, &pInfo->numOfPseudoExpr);
  }

1208
  pInfo->pRes = createResDataBlock(pDescNode);
X
Xiaoyu Wang 已提交
1209
  pInfo->pUpdateRes = createResDataBlock(pDescNode);
1210 1211 1212
  pInfo->pCondition = pScanPhyNode->node.pConditions;
  pInfo->scanMode = STREAM_SCAN_FROM_READERHANDLE;
  pInfo->sessionSup = (SessionWindowSupporter){.pStreamAggSup = NULL, .gap = -1};
1213
  pInfo->groupId = 0;
L
Liu Jicong 已提交
1214

1215
  pOperator->name = "StreamBlockScanOperator";
L
Liu Jicong 已提交
1216
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN;
1217 1218 1219
  pOperator->blocking = false;
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
1220
  pOperator->exprSupp.numOfExprs = taosArrayGetSize(pInfo->pRes->pDataBlock);
1221
  pOperator->pTaskInfo = pTaskInfo;
H
Haojun Liao 已提交
1222

1223 1224
  pOperator->fpSet =
      createOperatorFpSet(operatorDummyOpenFn, doStreamBlockScan, NULL, NULL, operatorDummyCloseFn, NULL, NULL, NULL);
1225

H
Haojun Liao 已提交
1226
  return pOperator;
1227

L
Liu Jicong 已提交
1228
_error:
1229 1230 1231
  taosMemoryFreeClear(pInfo);
  taosMemoryFreeClear(pOperator);
  return NULL;
H
Haojun Liao 已提交
1232 1233 1234 1235 1236 1237 1238
}

static void destroySysScanOperator(void* param, int32_t numOfOutput) {
  SSysTableScanInfo* pInfo = (SSysTableScanInfo*)param;
  tsem_destroy(&pInfo->ready);
  blockDataDestroy(pInfo->pRes);

1239
  const char* name = tNameGetTableName(&pInfo->name);
1240
  if (strncasecmp(name, TSDB_INS_TABLE_USER_TABLES, TSDB_TABLE_FNAME_LEN) == 0 || pInfo->pCur != NULL) {
H
Haojun Liao 已提交
1241
    metaCloseTbCursor(pInfo->pCur);
1242
    pInfo->pCur = NULL;
H
Haojun Liao 已提交
1243
  }
H
Haojun Liao 已提交
1244 1245

  taosArrayDestroy(pInfo->scanCols);
H
Haojun Liao 已提交
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 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
}

EDealRes getDBNameFromConditionWalker(SNode* pNode, void* pContext) {
  int32_t   code = TSDB_CODE_SUCCESS;
  ENodeType nType = nodeType(pNode);

  switch (nType) {
    case QUERY_NODE_OPERATOR: {
      SOperatorNode* node = (SOperatorNode*)pNode;
      if (OP_TYPE_EQUAL == node->opType) {
        *(int32_t*)pContext = 1;
        return DEAL_RES_CONTINUE;
      }

      *(int32_t*)pContext = 0;
      return DEAL_RES_IGNORE_CHILD;
    }
    case QUERY_NODE_COLUMN: {
      if (1 != *(int32_t*)pContext) {
        return DEAL_RES_CONTINUE;
      }

      SColumnNode* node = (SColumnNode*)pNode;
      if (TSDB_INS_USER_STABLES_DBNAME_COLID == node->colId) {
        *(int32_t*)pContext = 2;
        return DEAL_RES_CONTINUE;
      }

      *(int32_t*)pContext = 0;
      return DEAL_RES_CONTINUE;
    }
    case QUERY_NODE_VALUE: {
      if (2 != *(int32_t*)pContext) {
        return DEAL_RES_CONTINUE;
      }

      SValueNode* node = (SValueNode*)pNode;
      char*       dbName = nodesGetValueFromNode(node);
      strncpy(pContext, varDataVal(dbName), varDataLen(dbName));
      *((char*)pContext + varDataLen(dbName)) = 0;
1286
      return DEAL_RES_END;  // stop walk
H
Haojun Liao 已提交
1287 1288 1289 1290 1291 1292 1293
    }
    default:
      break;
  }
  return DEAL_RES_CONTINUE;
}

1294
static void getDBNameFromCondition(SNode* pCondition, const char* dbName) {
H
Haojun Liao 已提交
1295 1296 1297
  if (NULL == pCondition) {
    return;
  }
L
Liu Jicong 已提交
1298
  nodesWalkExpr(pCondition, getDBNameFromConditionWalker, (char*)dbName);
H
Haojun Liao 已提交
1299 1300
}

1301
static int32_t loadSysTableCallback(void* param, const SDataBuf* pMsg, int32_t code) {
H
Haojun Liao 已提交
1302 1303 1304 1305 1306 1307 1308
  SOperatorInfo*     operator=(SOperatorInfo*) param;
  SSysTableScanInfo* pScanResInfo = (SSysTableScanInfo*)operator->info;
  if (TSDB_CODE_SUCCESS == code) {
    pScanResInfo->pRsp = pMsg->pData;

    SRetrieveMetaTableRsp* pRsp = pScanResInfo->pRsp;
    pRsp->numOfRows = htonl(pRsp->numOfRows);
1309 1310 1311
    pRsp->useconds = htobe64(pRsp->useconds);
    pRsp->handle = htobe64(pRsp->handle);
    pRsp->compLen = htonl(pRsp->compLen);
H
Haojun Liao 已提交
1312 1313 1314 1315 1316
  } else {
    operator->pTaskInfo->code = code;
  }

  tsem_post(&pScanResInfo->ready);
wmmhello's avatar
wmmhello 已提交
1317
  return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
1318 1319 1320 1321 1322 1323 1324
}

static SSDataBlock* doFilterResult(SSysTableScanInfo* pInfo) {
  if (pInfo->pCondition == NULL) {
    return pInfo->pRes->info.rows == 0 ? NULL : pInfo->pRes;
  }

1325 1326
  doFilter(pInfo->pCondition, pInfo->pRes);
#if 0
H
Haojun Liao 已提交
1327
  SFilterInfo* filter = NULL;
1328 1329

  int32_t code = filterInitFromNode(pInfo->pCondition, &filter, 0);
H
Haojun Liao 已提交
1330 1331 1332 1333 1334

  SFilterColumnParam param1 = {.numOfCols = pInfo->pRes->info.numOfCols, .pDataBlock = pInfo->pRes->pDataBlock};
  code = filterSetDataFromSlotId(filter, &param1);

  int8_t* rowRes = NULL;
L
Liu Jicong 已提交
1335
  bool    keep = filterExecute(filter, pInfo->pRes, &rowRes, NULL, param1.numOfCols);
D
dapan1121 已提交
1336
  filterFreeInfo(filter);
H
Haojun Liao 已提交
1337

1338
  SSDataBlock* px = createOneDataBlock(pInfo->pRes, false);
H
Haojun Liao 已提交
1339 1340 1341 1342 1343 1344 1345 1346
  blockDataEnsureCapacity(px, pInfo->pRes->info.rows);

  // TODO refactor
  int32_t numOfRow = 0;
  for (int32_t i = 0; i < pInfo->pRes->info.numOfCols; ++i) {
    SColumnInfoData* pDest = taosArrayGet(px->pDataBlock, i);
    SColumnInfoData* pSrc = taosArrayGet(pInfo->pRes->pDataBlock, i);

D
dapan1121 已提交
1347
    if (keep) {
1348
      colDataAssign(pDest, pSrc, pInfo->pRes->info.rows, &px->info);
D
dapan1121 已提交
1349 1350 1351 1352 1353 1354 1355
      numOfRow = pInfo->pRes->info.rows;
    } else if (NULL != rowRes) {
      numOfRow = 0;
      for (int32_t j = 0; j < pInfo->pRes->info.rows; ++j) {
        if (rowRes[j] == 0) {
          continue;
        }
1356

1357 1358 1359 1360 1361 1362
        if (colDataIsNull_s(pSrc, j)) {
          colDataAppendNULL(pDest, numOfRow);
        } else {
          colDataAppend(pDest, numOfRow, colDataGetData(pSrc, j), false);
        }

D
dapan1121 已提交
1363
        numOfRow += 1;
H
Haojun Liao 已提交
1364
      }
D
dapan1121 已提交
1365 1366
    } else {
      numOfRow = 0;
H
Haojun Liao 已提交
1367 1368 1369 1370 1371
    }
  }

  px->info.rows = numOfRow;
  pInfo->pRes = px;
1372
#endif
H
Haojun Liao 已提交
1373 1374 1375 1376

  return pInfo->pRes->info.rows == 0 ? NULL : pInfo->pRes;
}

1377
static SSDataBlock* buildSysTableMetaBlock() {
L
Liu Jicong 已提交
1378 1379
  size_t               size = 0;
  const SSysTableMeta* pMeta = NULL;
1380 1381 1382
  getInfosDbMeta(&pMeta, &size);

  int32_t index = 0;
L
Liu Jicong 已提交
1383 1384
  for (int32_t i = 0; i < size; ++i) {
    if (strcmp(pMeta[i].name, TSDB_INS_TABLE_USER_TABLES) == 0) {
1385 1386 1387 1388
      index = i;
      break;
    }
  }
1389

1390
  SSDataBlock* pBlock = createDataBlock();
L
Liu Jicong 已提交
1391
  for (int32_t i = 0; i < pMeta[index].colNum; ++i) {
L
Liu Jicong 已提交
1392 1393
    SColumnInfoData colInfoData =
        createColumnInfoData(pMeta[index].schema[i].type, pMeta[index].schema[i].bytes, i + 1);
1394
    blockDataAppendColInfo(pBlock, &colInfoData);
1395 1396
  }

1397 1398 1399
  return pBlock;
}

1400
static SSDataBlock* doSysTableScan(SOperatorInfo* pOperator) {
H
Haojun Liao 已提交
1401 1402 1403 1404 1405
  // build message and send to mnode to fetch the content of system tables.
  SExecTaskInfo*     pTaskInfo = pOperator->pTaskInfo;
  SSysTableScanInfo* pInfo = pOperator->info;

  // retrieve local table list info from vnode
1406 1407
  const char* name = tNameGetTableName(&pInfo->name);
  if (strncasecmp(name, TSDB_INS_TABLE_USER_TABLES, TSDB_TABLE_FNAME_LEN) == 0) {
1408 1409 1410 1411
    if (pOperator->status == OP_EXEC_DONE) {
      return NULL;
    }

1412 1413
    // the retrieve is executed on the mnode, so return tables that belongs to the information schema database.
    if (pInfo->readHandle.mnd != NULL) {
1414
      buildSysDbTableInfo(pInfo, pOperator->resultInfo.capacity);
1415

1416 1417
      doFilterResult(pInfo);
      pInfo->loadInfo.totalRows += pInfo->pRes->info.rows;
1418

1419
      doSetOperatorCompleted(pOperator);
1420 1421 1422 1423 1424
      return (pInfo->pRes->info.rows == 0) ? NULL : pInfo->pRes;
    } else {
      if (pInfo->pCur == NULL) {
        pInfo->pCur = metaOpenTbCursor(pInfo->readHandle.meta);
      }
1425

1426 1427
      blockDataCleanup(pInfo->pRes);
      int32_t numOfRows = 0;
1428

1429 1430 1431
      const char* db = NULL;
      int32_t     vgId = 0;
      vnodeGetInfo(pInfo->readHandle.vnode, &db, &vgId);
1432

1433 1434 1435
      SName sn = {0};
      char  dbname[TSDB_DB_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
      tNameFromString(&sn, db, T_NAME_ACCT | T_NAME_DB);
1436

1437 1438
      tNameGetDbName(&sn, varDataVal(dbname));
      varDataSetLen(dbname, strlen(varDataVal(dbname)));
1439

1440
      SSDataBlock* p = buildSysTableMetaBlock();
1441
      blockDataEnsureCapacity(p, pOperator->resultInfo.capacity);
1442

1443
      char n[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
1444 1445 1446

      int32_t ret = 0;
      while ((ret = metaTbCursorNext(pInfo->pCur)) == 0) {
1447
        STR_TO_VARSTR(n, pInfo->pCur->mr.me.name);
1448

1449 1450 1451
        // table name
        SColumnInfoData* pColInfoData = taosArrayGet(p->pDataBlock, 0);
        colDataAppend(pColInfoData, numOfRows, n, false);
1452

1453 1454 1455
        // database name
        pColInfoData = taosArrayGet(p->pDataBlock, 1);
        colDataAppend(pColInfoData, numOfRows, dbname, false);
1456

1457 1458 1459
        // vgId
        pColInfoData = taosArrayGet(p->pDataBlock, 6);
        colDataAppend(pColInfoData, numOfRows, (char*)&vgId, false);
1460

1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
        int32_t tableType = pInfo->pCur->mr.me.type;
        if (tableType == TSDB_CHILD_TABLE) {
          // create time
          int64_t ts = pInfo->pCur->mr.me.ctbEntry.ctime;
          pColInfoData = taosArrayGet(p->pDataBlock, 2);
          colDataAppend(pColInfoData, numOfRows, (char*)&ts, false);

          SMetaReader mr = {0};
          metaReaderInit(&mr, pInfo->readHandle.meta, 0);
          metaGetTableEntryByUid(&mr, pInfo->pCur->mr.me.ctbEntry.suid);

          // number of columns
          pColInfoData = taosArrayGet(p->pDataBlock, 3);
1474
          colDataAppend(pColInfoData, numOfRows, (char*)&mr.me.stbEntry.schemaRow.nCols, false);
1475 1476

          // super table name
wmmhello's avatar
wmmhello 已提交
1477
          STR_TO_VARSTR(n, mr.me.name);
1478
          pColInfoData = taosArrayGet(p->pDataBlock, 4);
wmmhello's avatar
wmmhello 已提交
1479
          colDataAppend(pColInfoData, numOfRows, n, false);
1480 1481
          metaReaderClear(&mr);

wmmhello's avatar
wmmhello 已提交
1482 1483
          // table comment
          pColInfoData = taosArrayGet(p->pDataBlock, 8);
L
Liu Jicong 已提交
1484
          if (pInfo->pCur->mr.me.ctbEntry.commentLen > 0) {
wmmhello's avatar
wmmhello 已提交
1485 1486 1487
            char comment[TSDB_TB_COMMENT_LEN + VARSTR_HEADER_SIZE] = {0};
            STR_TO_VARSTR(comment, pInfo->pCur->mr.me.ctbEntry.comment);
            colDataAppend(pColInfoData, numOfRows, comment, false);
L
Liu Jicong 已提交
1488
          } else if (pInfo->pCur->mr.me.ctbEntry.commentLen == 0) {
wmmhello's avatar
wmmhello 已提交
1489 1490 1491
            char comment[VARSTR_HEADER_SIZE + VARSTR_HEADER_SIZE] = {0};
            STR_TO_VARSTR(comment, "");
            colDataAppend(pColInfoData, numOfRows, comment, false);
L
Liu Jicong 已提交
1492
          } else {
wmmhello's avatar
wmmhello 已提交
1493 1494 1495
            colDataAppendNULL(pColInfoData, numOfRows);
          }

1496 1497 1498 1499 1500 1501 1502 1503
          // uid
          pColInfoData = taosArrayGet(p->pDataBlock, 5);
          colDataAppend(pColInfoData, numOfRows, (char*)&pInfo->pCur->mr.me.uid, false);

          // ttl
          pColInfoData = taosArrayGet(p->pDataBlock, 7);
          colDataAppend(pColInfoData, numOfRows, (char*)&pInfo->pCur->mr.me.ctbEntry.ttlDays, false);

wmmhello's avatar
wmmhello 已提交
1504
          STR_TO_VARSTR(n, "CHILD_TABLE");
1505 1506 1507 1508 1509 1510 1511
        } else if (tableType == TSDB_NORMAL_TABLE) {
          // create time
          pColInfoData = taosArrayGet(p->pDataBlock, 2);
          colDataAppend(pColInfoData, numOfRows, (char*)&pInfo->pCur->mr.me.ntbEntry.ctime, false);

          // number of columns
          pColInfoData = taosArrayGet(p->pDataBlock, 3);
1512
          colDataAppend(pColInfoData, numOfRows, (char*)&pInfo->pCur->mr.me.ntbEntry.schemaRow.nCols, false);
1513 1514 1515 1516 1517

          // super table name
          pColInfoData = taosArrayGet(p->pDataBlock, 4);
          colDataAppendNULL(pColInfoData, numOfRows);

wmmhello's avatar
wmmhello 已提交
1518 1519
          // table comment
          pColInfoData = taosArrayGet(p->pDataBlock, 8);
L
Liu Jicong 已提交
1520
          if (pInfo->pCur->mr.me.ntbEntry.commentLen > 0) {
wmmhello's avatar
wmmhello 已提交
1521 1522 1523
            char comment[TSDB_TB_COMMENT_LEN + VARSTR_HEADER_SIZE] = {0};
            STR_TO_VARSTR(comment, pInfo->pCur->mr.me.ntbEntry.comment);
            colDataAppend(pColInfoData, numOfRows, comment, false);
L
Liu Jicong 已提交
1524
          } else if (pInfo->pCur->mr.me.ntbEntry.commentLen == 0) {
wmmhello's avatar
wmmhello 已提交
1525 1526 1527
            char comment[VARSTR_HEADER_SIZE + VARSTR_HEADER_SIZE] = {0};
            STR_TO_VARSTR(comment, "");
            colDataAppend(pColInfoData, numOfRows, comment, false);
L
Liu Jicong 已提交
1528
          } else {
wmmhello's avatar
wmmhello 已提交
1529 1530 1531
            colDataAppendNULL(pColInfoData, numOfRows);
          }

1532 1533 1534 1535 1536 1537 1538 1539
          // uid
          pColInfoData = taosArrayGet(p->pDataBlock, 5);
          colDataAppend(pColInfoData, numOfRows, (char*)&pInfo->pCur->mr.me.uid, false);

          // ttl
          pColInfoData = taosArrayGet(p->pDataBlock, 7);
          colDataAppend(pColInfoData, numOfRows, (char*)&pInfo->pCur->mr.me.ntbEntry.ttlDays, false);

wmmhello's avatar
wmmhello 已提交
1540
          STR_TO_VARSTR(n, "NORMAL_TABLE");
1541
        }
1542

1543
        pColInfoData = taosArrayGet(p->pDataBlock, 9);
wmmhello's avatar
wmmhello 已提交
1544
        colDataAppend(pColInfoData, numOfRows, n, false);
1545

1546
        if (++numOfRows >= pOperator->resultInfo.capacity) {
1547 1548
          break;
        }
H
Haojun Liao 已提交
1549 1550
      }

1551 1552 1553 1554 1555 1556 1557
      // todo temporarily free the cursor here, the true reason why the free is not valid needs to be found
      if (ret != 0) {
        metaCloseTbCursor(pInfo->pCur);
        pInfo->pCur = NULL;
        doSetOperatorCompleted(pOperator);
      }

1558 1559
      p->info.rows = numOfRows;
      pInfo->pRes->info.rows = numOfRows;
H
Haojun Liao 已提交
1560

1561
      relocateColumnData(pInfo->pRes, pInfo->scanCols, p->pDataBlock, false);
1562
      doFilterResult(pInfo);
H
Haojun Liao 已提交
1563

1564 1565
      blockDataDestroy(p);

1566 1567 1568
      pInfo->loadInfo.totalRows += pInfo->pRes->info.rows;
      return (pInfo->pRes->info.rows == 0) ? NULL : pInfo->pRes;
    }
H
Haojun Liao 已提交
1569 1570 1571 1572 1573
  } else {  // load the meta from mnode of the given epset
    if (pOperator->status == OP_EXEC_DONE) {
      return NULL;
    }

1574 1575 1576
    while (1) {
      int64_t startTs = taosGetTimestampUs();
      strncpy(pInfo->req.tb, tNameGetTableName(&pInfo->name), tListLen(pInfo->req.tb));
H
Haojun Liao 已提交
1577

1578 1579 1580 1581 1582
      if (pInfo->showRewrite) {
        char dbName[TSDB_DB_NAME_LEN] = {0};
        getDBNameFromCondition(pInfo->pCondition, dbName);
        sprintf(pInfo->req.db, "%d.%s", pInfo->accountId, dbName);
      }
H
Haojun Liao 已提交
1583

1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
      int32_t contLen = tSerializeSRetrieveTableReq(NULL, 0, &pInfo->req);
      char*   buf1 = taosMemoryCalloc(1, contLen);
      tSerializeSRetrieveTableReq(buf1, contLen, &pInfo->req);

      // send the fetch remote task result reques
      SMsgSendInfo* pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
      if (NULL == pMsgSendInfo) {
        qError("%s prepare message %d failed", GET_TASKID(pTaskInfo), (int32_t)sizeof(SMsgSendInfo));
        pTaskInfo->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
        return NULL;
      }
H
Haojun Liao 已提交
1595

L
Liu Jicong 已提交
1596 1597
      int32_t msgType = (strcasecmp(name, TSDB_INS_TABLE_DNODE_VARIABLES) == 0) ? TDMT_DND_SYSTABLE_RETRIEVE
                                                                                : TDMT_MND_SYSTABLE_RETRIEVE;
D
dapan1121 已提交
1598

1599 1600 1601
      pMsgSendInfo->param = pOperator;
      pMsgSendInfo->msgInfo.pData = buf1;
      pMsgSendInfo->msgInfo.len = contLen;
D
dapan1121 已提交
1602
      pMsgSendInfo->msgType = msgType;
1603
      pMsgSendInfo->fp = loadSysTableCallback;
D
dapan1121 已提交
1604
      pMsgSendInfo->requestId = pTaskInfo->id.queryId;
H
Haojun Liao 已提交
1605

1606
      int64_t transporterId = 0;
1607 1608
      int32_t code =
          asyncSendMsgToServer(pInfo->readHandle.pMsgCb->clientRpc, &pInfo->epSet, &transporterId, pMsgSendInfo);
1609
      tsem_wait(&pInfo->ready);
H
Haojun Liao 已提交
1610

1611 1612 1613 1614 1615
      if (pTaskInfo->code) {
        qDebug("%s load meta data from mnode failed, totalRows:%" PRIu64 ", code:%s", GET_TASKID(pTaskInfo),
               pInfo->loadInfo.totalRows, tstrerror(pTaskInfo->code));
        return NULL;
      }
H
Haojun Liao 已提交
1616

1617 1618
      SRetrieveMetaTableRsp* pRsp = pInfo->pRsp;
      pInfo->req.showId = pRsp->handle;
H
Haojun Liao 已提交
1619

1620 1621
      if (pRsp->numOfRows == 0 || pRsp->completed) {
        pOperator->status = OP_EXEC_DONE;
1622
        qDebug("%s load meta data from mnode completed, rowsOfSource:%d, totalRows:%" PRIu64, GET_TASKID(pTaskInfo),
1623
               pRsp->numOfRows, pInfo->loadInfo.totalRows);
H
Haojun Liao 已提交
1624

1625
        if (pRsp->numOfRows == 0) {
H
Haojun Liao 已提交
1626
          taosMemoryFree(pRsp);
1627 1628 1629
          return NULL;
        }
      }
H
Haojun Liao 已提交
1630

1631
      extractDataBlockFromFetchRsp(pInfo->pRes, &pInfo->loadInfo, pRsp->numOfRows, pRsp->data, pRsp->compLen,
L
Liu Jicong 已提交
1632
                                   pOperator->exprSupp.numOfExprs, startTs, NULL, pInfo->scanCols);
H
Haojun Liao 已提交
1633

1634 1635
      // todo log the filter info
      doFilterResult(pInfo);
H
Haojun Liao 已提交
1636
      taosMemoryFree(pRsp);
1637 1638
      if (pInfo->pRes->info.rows > 0) {
        return pInfo->pRes;
D
dapan1121 已提交
1639 1640
      } else if (pOperator->status == OP_EXEC_DONE) {
        return NULL;
1641
      }
1642
    }
H
Haojun Liao 已提交
1643 1644 1645
  }
}

1646
int32_t buildSysDbTableInfo(const SSysTableScanInfo* pInfo, int32_t capacity) {
1647
  SSDataBlock* p = buildSysTableMetaBlock();
1648
  blockDataEnsureCapacity(p, capacity);
1649

L
Liu Jicong 已提交
1650
  size_t               size = 0;
1651 1652 1653 1654 1655 1656 1657 1658
  const SSysTableMeta* pSysDbTableMeta = NULL;

  getInfosDbMeta(&pSysDbTableMeta, &size);
  p->info.rows = buildDbTableInfoBlock(p, pSysDbTableMeta, size, TSDB_INFORMATION_SCHEMA_DB);

  getPerfDbMeta(&pSysDbTableMeta, &size);
  p->info.rows = buildDbTableInfoBlock(p, pSysDbTableMeta, size, TSDB_PERFORMANCE_SCHEMA_DB);

1659
  relocateColumnData(pInfo->pRes, pInfo->scanCols, p->pDataBlock, false);
1660
  pInfo->pRes->info.rows = p->info.rows;
1661 1662 1663
  blockDataDestroy(p);

  return pInfo->pRes->info.rows;
1664 1665
}

L
Liu Jicong 已提交
1666 1667 1668
int32_t buildDbTableInfoBlock(const SSDataBlock* p, const SSysTableMeta* pSysDbTableMeta, size_t size,
                              const char* dbName) {
  char    n[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
1669 1670
  int32_t numOfRows = p->info.rows;

L
Liu Jicong 已提交
1671
  for (int32_t i = 0; i < size; ++i) {
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
    const SSysTableMeta* pm = &pSysDbTableMeta[i];

    SColumnInfoData* pColInfoData = taosArrayGet(p->pDataBlock, 0);

    STR_TO_VARSTR(n, pm->name);
    colDataAppend(pColInfoData, numOfRows, n, false);

    // database name
    STR_TO_VARSTR(n, dbName);
    pColInfoData = taosArrayGet(p->pDataBlock, 1);
    colDataAppend(pColInfoData, numOfRows, n, false);

    // create time
    pColInfoData = taosArrayGet(p->pDataBlock, 2);
    colDataAppendNULL(pColInfoData, numOfRows);

    // number of columns
    pColInfoData = taosArrayGet(p->pDataBlock, 3);
    colDataAppend(pColInfoData, numOfRows, (char*)&pm->colNum, false);

L
Liu Jicong 已提交
1692
    for (int32_t j = 4; j <= 8; ++j) {
1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707
      pColInfoData = taosArrayGet(p->pDataBlock, j);
      colDataAppendNULL(pColInfoData, numOfRows);
    }

    STR_TO_VARSTR(n, "SYSTEM_TABLE");

    pColInfoData = taosArrayGet(p->pDataBlock, 9);
    colDataAppend(pColInfoData, numOfRows, n, false);

    numOfRows += 1;
  }

  return numOfRows;
}

1708 1709
SOperatorInfo* createSysTableScanOperatorInfo(void* readHandle, SSystemTableScanPhysiNode* pScanPhyNode,
                                              SExecTaskInfo* pTaskInfo) {
H
Haojun Liao 已提交
1710 1711 1712
  SSysTableScanInfo* pInfo = taosMemoryCalloc(1, sizeof(SSysTableScanInfo));
  SOperatorInfo*     pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
  if (pInfo == NULL || pOperator == NULL) {
1713
    goto _error;
H
Haojun Liao 已提交
1714 1715
  }

1716 1717 1718
  SScanPhysiNode* pScanNode = &pScanPhyNode->scan;

  SDataBlockDescNode* pDescNode = pScanNode->node.pOutputDataBlockDesc;
1719
  SSDataBlock*        pResBlock = createResDataBlock(pDescNode);
1720 1721

  int32_t num = 0;
1722
  SArray* colList = extractColMatchInfo(pScanNode->pScanCols, pDescNode, &num, COL_MATCH_FROM_COL_ID);
1723

1724
  pInfo->accountId = pScanPhyNode->accountId;
1725
  pInfo->showRewrite = pScanPhyNode->showRewrite;
1726 1727 1728
  pInfo->pRes = pResBlock;
  pInfo->pCondition = pScanNode->node.pConditions;
  pInfo->scanCols = colList;
1729 1730

  initResultSizeInfo(pOperator, 4096);
H
Haojun Liao 已提交
1731

1732
  tNameAssign(&pInfo->name, &pScanNode->tableName);
1733
  const char* name = tNameGetTableName(&pInfo->name);
1734

1735
  if (strncasecmp(name, TSDB_INS_TABLE_USER_TABLES, TSDB_TABLE_FNAME_LEN) == 0) {
L
Liu Jicong 已提交
1736
    pInfo->readHandle = *(SReadHandle*)readHandle;
1737
    blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
H
Haojun Liao 已提交
1738 1739
  } else {
    tsem_init(&pInfo->ready, 0, 0);
1740
    pInfo->epSet = pScanPhyNode->mgmtEpSet;
1741
    pInfo->readHandle = *(SReadHandle*)readHandle;
H
Haojun Liao 已提交
1742 1743
  }

1744
  pOperator->name = "SysTableScanOperator";
H
Haojun Liao 已提交
1745
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN;
1746 1747 1748
  pOperator->blocking = false;
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
1749
  pOperator->exprSupp.numOfExprs = taosArrayGetSize(pResBlock->pDataBlock);
1750
  pOperator->pTaskInfo = pTaskInfo;
1751

L
Liu Jicong 已提交
1752 1753
  pOperator->fpSet =
      createOperatorFpSet(operatorDummyOpenFn, doSysTableScan, NULL, NULL, destroySysScanOperator, NULL, NULL, NULL);
H
Haojun Liao 已提交
1754 1755

  return pOperator;
1756

1757
_error:
1758 1759 1760 1761
  taosMemoryFreeClear(pInfo);
  taosMemoryFreeClear(pOperator);
  terrno = TSDB_CODE_QRY_OUT_OF_MEMORY;
  return NULL;
H
Haojun Liao 已提交
1762
}
H
Haojun Liao 已提交
1763

1764
static SSDataBlock* doTagScan(SOperatorInfo* pOperator) {
H
Haojun Liao 已提交
1765 1766 1767 1768
  if (pOperator->status == OP_EXEC_DONE) {
    return NULL;
  }

1769 1770 1771
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;

#if 0
H
Haojun Liao 已提交
1772 1773 1774 1775 1776 1777 1778 1779
  int32_t maxNumOfTables = (int32_t)pResultInfo->capacity;

  STagScanInfo *pInfo = pOperator->info;
  SSDataBlock  *pRes = pInfo->pRes;

  int32_t count = 0;
  SArray* pa = GET_TABLEGROUP(pRuntimeEnv, 0);

1780
  int32_t functionId = getExprFunctionId(&pOperator->exprSupp.pExprInfo[0]);
H
Haojun Liao 已提交
1781 1782 1783
  if (functionId == FUNCTION_TID_TAG) { // return the tags & table Id
    assert(pQueryAttr->numOfOutput == 1);

1784
    SExprInfo* pExprInfo = &pOperator->exprSupp.pExprInfo[0];
H
Haojun Liao 已提交
1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
    int32_t rsize = pExprInfo->base.resSchema.bytes;

    count = 0;

    int16_t bytes = pExprInfo->base.resSchema.bytes;
    int16_t type  = pExprInfo->base.resSchema.type;

    for(int32_t i = 0; i < pQueryAttr->numOfTags; ++i) {
      if (pQueryAttr->tagColList[i].colId == pExprInfo->base.pColumns->info.colId) {
        bytes = pQueryAttr->tagColList[i].bytes;
        type = pQueryAttr->tagColList[i].type;
        break;
      }
    }

    SColumnInfoData* pColInfo = taosArrayGet(pRes->pDataBlock, 0);

    while(pInfo->curPos < pInfo->totalTables && count < maxNumOfTables) {
      int32_t i = pInfo->curPos++;
      STableQueryInfo *item = taosArrayGetP(pa, i);

      char *output = pColInfo->pData + count * rsize;
      varDataSetLen(output, rsize - VARSTR_HEADER_SIZE);

      output = varDataVal(output);
      STableId* id = TSDB_TABLEID(item->pTable);

      *(int16_t *)output = 0;
      output += sizeof(int16_t);

      *(int64_t *)output = id->uid;  // memory align problem, todo serialize
      output += sizeof(id->uid);

      *(int32_t *)output = id->tid;
      output += sizeof(id->tid);

      *(int32_t *)output = pQueryAttr->vgId;
      output += sizeof(pQueryAttr->vgId);

      char* data = NULL;
      if (pExprInfo->base.pColumns->info.colId == TSDB_TBNAME_COLUMN_INDEX) {
        data = tsdbGetTableName(item->pTable);
      } else {
        data = tsdbGetTableTagVal(item->pTable, pExprInfo->base.pColumns->info.colId, type, bytes);
      }

      doSetTagValueToResultBuf(output, data, type, bytes);
      count += 1;
    }

    //qDebug("QInfo:0x%"PRIx64" create (tableId, tag) info completed, rows:%d", GET_TASKID(pRuntimeEnv), count);
  } else if (functionId == FUNCTION_COUNT) {// handle the "count(tbname)" query
    SColumnInfoData* pColInfo = taosArrayGet(pRes->pDataBlock, 0);
    *(int64_t*)pColInfo->pData = pInfo->totalTables;
    count = 1;

    pOperator->status = OP_EXEC_DONE;
    //qDebug("QInfo:0x%"PRIx64" create count(tbname) query, res:%d rows:1", GET_TASKID(pRuntimeEnv), count);
  } else {  // return only the tags|table name etc.
1844
#endif
H
Haojun Liao 已提交
1845

1846
  STagScanInfo* pInfo = pOperator->info;
1847
  SExprInfo*    pExprInfo = &pOperator->exprSupp.pExprInfo[0];
1848
  SSDataBlock*  pRes = pInfo->pRes;
H
Haojun Liao 已提交
1849

wmmhello's avatar
wmmhello 已提交
1850 1851
  int32_t size = taosArrayGetSize(pInfo->pTableList->pTableList);
  if (size == 0) {
H
Haojun Liao 已提交
1852 1853 1854 1855
    setTaskStatus(pTaskInfo, TASK_COMPLETED);
    return NULL;
  }

1856 1857 1858
  char        str[512] = {0};
  int32_t     count = 0;
  SMetaReader mr = {0};
1859
  metaReaderInit(&mr, pInfo->readHandle.meta, 0);
H
Haojun Liao 已提交
1860

wmmhello's avatar
wmmhello 已提交
1861 1862
  while (pInfo->curPos < size && count < pOperator->resultInfo.capacity) {
    STableKeyInfo* item = taosArrayGet(pInfo->pTableList->pTableList, pInfo->curPos);
1863
    metaGetTableEntryByUid(&mr, item->uid);
H
Haojun Liao 已提交
1864

1865
    for (int32_t j = 0; j < pOperator->exprSupp.numOfExprs; ++j) {
1866 1867 1868 1869 1870 1871
      SColumnInfoData* pDst = taosArrayGet(pRes->pDataBlock, pExprInfo[j].base.resSchema.slotId);

      // refactor later
      if (fmIsScanPseudoColumnFunc(pExprInfo[j].pExpr->_function.functionId)) {
        STR_TO_VARSTR(str, mr.me.name);
        colDataAppend(pDst, count, str, false);
1872
      } else {  // it is a tag value
wmmhello's avatar
wmmhello 已提交
1873 1874 1875 1876
        STagVal val = {0};
        val.cid = pExprInfo[j].base.pParam[0].pCol->colId;
        const char* p = metaGetTableTagVal(&mr.me, pDst->info.type, &val);

1877 1878 1879 1880
        char* data = NULL;
        if (pDst->info.type != TSDB_DATA_TYPE_JSON && p != NULL) {
          data = tTagValToData((const STagVal*)p, false);
        } else {
wmmhello's avatar
wmmhello 已提交
1881 1882
          data = (char*)p;
        }
L
Liu Jicong 已提交
1883 1884
        colDataAppend(pDst, count, data,
                      (data == NULL) || (pDst->info.type == TSDB_DATA_TYPE_JSON && tTagIsJsonNull(data)));
1885

1886 1887
        if (pDst->info.type != TSDB_DATA_TYPE_JSON && p != NULL && IS_VAR_DATA_TYPE(((const STagVal*)p)->type) &&
            data != NULL) {
wmmhello's avatar
wmmhello 已提交
1888
          taosMemoryFree(data);
wmmhello's avatar
wmmhello 已提交
1889
        }
H
Haojun Liao 已提交
1890 1891 1892
      }
    }

1893
    count += 1;
wmmhello's avatar
wmmhello 已提交
1894
    if (++pInfo->curPos >= size) {
1895
      doSetOperatorCompleted(pOperator);
H
Haojun Liao 已提交
1896 1897 1898
    }
  }

1899 1900
  metaReaderClear(&mr);

1901
  // qDebug("QInfo:0x%"PRIx64" create tag values results completed, rows:%d", GET_TASKID(pRuntimeEnv), count);
H
Haojun Liao 已提交
1902
  if (pOperator->status == OP_EXEC_DONE) {
1903
    setTaskStatus(pTaskInfo, TASK_COMPLETED);
H
Haojun Liao 已提交
1904 1905 1906
  }

  pRes->info.rows = count;
wmmhello's avatar
wmmhello 已提交
1907
  pOperator->resultInfo.totalRows += count;
1908

1909
  return (pRes->info.rows == 0) ? NULL : pInfo->pRes;
H
Haojun Liao 已提交
1910 1911 1912 1913 1914 1915 1916
}

static void destroyTagScanOperatorInfo(void* param, int32_t numOfOutput) {
  STagScanInfo* pInfo = (STagScanInfo*)param;
  pInfo->pRes = blockDataDestroy(pInfo->pRes);
}

1917 1918
SOperatorInfo* createTagScanOperatorInfo(SReadHandle* pReadHandle, STagScanPhysiNode* pPhyNode,
                                         STableListInfo* pTableListInfo, SExecTaskInfo* pTaskInfo) {
1919
  STagScanInfo*  pInfo = taosMemoryCalloc(1, sizeof(STagScanInfo));
H
Haojun Liao 已提交
1920 1921 1922 1923 1924
  SOperatorInfo* pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
  if (pInfo == NULL || pOperator == NULL) {
    goto _error;
  }

1925 1926
  SDataBlockDescNode* pDescNode = pPhyNode->node.pOutputDataBlockDesc;

1927
  int32_t    num = 0;
1928 1929
  int32_t    numOfExprs = 0;
  SExprInfo* pExprInfo = createExprInfo(pPhyNode->pScanPseudoCols, NULL, &numOfExprs);
L
Liu Jicong 已提交
1930
  SArray*    colList = extractColMatchInfo(pPhyNode->pScanPseudoCols, pDescNode, &num, COL_MATCH_FROM_COL_ID);
1931

1932 1933 1934 1935
  int32_t code = initExprSupp(&pOperator->exprSupp, pExprInfo, numOfExprs);
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
1936

1937 1938 1939 1940 1941
  pInfo->pTableList       = pTableListInfo;
  pInfo->pColMatchInfo    = colList;
  pInfo->pRes             = createResDataBlock(pDescNode);
  pInfo->readHandle       = *pReadHandle;
  pInfo->curPos           = 0;
1942

1943
  pOperator->name = "TagScanOperator";
1944
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN;
1945

1946 1947 1948 1949
  pOperator->blocking = false;
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
  pOperator->pTaskInfo = pTaskInfo;
1950

1951 1952 1953
  initResultSizeInfo(pOperator, 4096);
  blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);

1954 1955
  pOperator->fpSet =
      createOperatorFpSet(operatorDummyOpenFn, doTagScan, NULL, NULL, destroyTagScanOperatorInfo, NULL, NULL, NULL);
H
Haojun Liao 已提交
1956 1957

  return pOperator;
1958

1959
_error:
H
Haojun Liao 已提交
1960 1961 1962 1963 1964
  taosMemoryFree(pInfo);
  taosMemoryFree(pOperator);
  terrno = TSDB_CODE_OUT_OF_MEMORY;
  return NULL;
}
1965 1966

typedef struct STableMergeScanInfo {
1967
  STableListInfo* tableListInfo;
wmmhello's avatar
wmmhello 已提交
1968
  int32_t         currentGroupId;
1969

1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981
  SArray*     dataReaders;  // array of tsdbReaderT*
  SReadHandle readHandle;

  int32_t  bufPageSize;
  uint32_t sortBufSize;  // max buffer size for in-memory sort

  SArray*      pSortInfo;
  SSortHandle* pSortHandle;

  SSDataBlock* pSortInputBlock;
  int64_t      startTs;  // sort start time

1982 1983 1984
  SArray*  sortSourceParams;
  uint64_t queryId;
  uint64_t taskId;
1985 1986 1987 1988 1989 1990 1991 1992 1993

  SFileBlockLoadRecorder readRecorder;
  int64_t                numOfRows;
  //  int32_t         prevGroupId;  // previous table group id
  SScanInfo       scanInfo;
  int32_t         scanTimes;
  SNode*          pFilterNode;  // filter info, which is push down by optimizer
  SqlFunctionCtx* pCtx;         // which belongs to the direct upstream operator operator query context
  SResultRowInfo* pResultRowInfo;
1994
  int32_t*        rowEntryInfoOffset;
1995 1996 1997 1998 1999 2000 2001 2002
  SExprInfo*      pExpr;
  SSDataBlock*    pResBlock;
  SArray*         pColMatchInfo;
  int32_t         numOfOutput;

  SExprInfo*      pPseudoExpr;
  int32_t         numOfPseudoExpr;
  SqlFunctionCtx* pPseudoCtx;
2003
  //  int32_t*        rowEntryInfoOffset;
2004 2005 2006 2007 2008 2009 2010 2011 2012 2013

  SQueryTableDataCond cond;
  int32_t             scanFlag;  // table scan flag to denote if it is a repeat/reverse/main scan
  int32_t             dataBlockLoadFlag;
  SInterval interval;  // if the upstream is an interval operator, the interval info is also kept here to get the time
                       // window to check if current data block needs to be loaded.

  SSampleExecInfo sample;  // sample execution info
} STableMergeScanInfo;

2014
int32_t createScanTableListInfo(STableScanPhysiNode* pTableScanNode, SReadHandle* pHandle,
wmmhello's avatar
wmmhello 已提交
2015
                                STableListInfo* pTableListInfo, uint64_t queryId, uint64_t taskId) {
wmmhello's avatar
wmmhello 已提交
2016
  int32_t code = getTableList(pHandle->meta, &pTableScanNode->scan, pTableListInfo);
2017
  if (code != TSDB_CODE_SUCCESS) {
2018
    return code;
2019 2020 2021 2022
  }

  if (taosArrayGetSize(pTableListInfo->pTableList) == 0) {
    qDebug("no table qualified for query, TID:0x%" PRIx64 ", QID:0x%" PRIx64, taskId, queryId);
2023 2024
    return TSDB_CODE_SUCCESS;
  }
wmmhello's avatar
wmmhello 已提交
2025
  code = generateGroupIdMap(pTableListInfo, pHandle, pTableScanNode->pPartitionTags);
2026
  if (code != TSDB_CODE_SUCCESS) {
wmmhello's avatar
wmmhello 已提交
2027
    return code;
2028 2029 2030 2031 2032
  }

  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
2033
// todo refactor
2034 2035
static int32_t loadDataBlockFromOneTable(SOperatorInfo* pOperator, STableMergeScanInfo* pTableScanInfo,
                                         int32_t readerIdx, SSDataBlock* pBlock, uint32_t* status) {
L
Liu Jicong 已提交
2036
  SExecTaskInfo*       pTaskInfo = pOperator->pTaskInfo;
S
shenglian zhou 已提交
2037
  STableMergeScanInfo* pInfo = pOperator->info;
2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063

  SFileBlockLoadRecorder* pCost = &pTableScanInfo->readRecorder;

  pCost->totalBlocks += 1;
  pCost->totalRows += pBlock->info.rows;

  *status = pInfo->dataBlockLoadFlag;
  if (pTableScanInfo->pFilterNode != NULL ||
      overlapWithTimeWindow(&pTableScanInfo->interval, &pBlock->info, pTableScanInfo->cond.order)) {
    (*status) = FUNC_DATA_REQUIRED_DATA_LOAD;
  }

  SDataBlockInfo* pBlockInfo = &pBlock->info;
  taosMemoryFreeClear(pBlock->pBlockAgg);

  if (*status == FUNC_DATA_REQUIRED_FILTEROUT) {
    qDebug("%s data block filter out, brange:%" PRId64 "-%" PRId64 ", rows:%d", GET_TASKID(pTaskInfo),
           pBlockInfo->window.skey, pBlockInfo->window.ekey, pBlockInfo->rows);
    pCost->filterOutBlocks += 1;
    return TSDB_CODE_SUCCESS;
  } else if (*status == FUNC_DATA_REQUIRED_NOT_LOAD) {
    qDebug("%s data block skipped, brange:%" PRId64 "-%" PRId64 ", rows:%d", GET_TASKID(pTaskInfo),
           pBlockInfo->window.skey, pBlockInfo->window.ekey, pBlockInfo->rows);
    pCost->skipBlocks += 1;

    // clear all data in pBlock that are set when handing the previous block
2064
    for (int32_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); ++i) {
2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078
      SColumnInfoData* pcol = taosArrayGet(pBlock->pDataBlock, i);
      pcol->pData = NULL;
    }

    return TSDB_CODE_SUCCESS;
  } else if (*status == FUNC_DATA_REQUIRED_STATIS_LOAD) {
    pCost->loadBlockStatis += 1;

    bool             allColumnsHaveAgg = true;
    SColumnDataAgg** pColAgg = NULL;
    tsdbReaderT*     reader = taosArrayGetP(pTableScanInfo->dataReaders, readerIdx);
    tsdbRetrieveDataBlockStatisInfo(reader, &pColAgg, &allColumnsHaveAgg);

    if (allColumnsHaveAgg == true) {
2079
      int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121

      // todo create this buffer during creating operator
      if (pBlock->pBlockAgg == NULL) {
        pBlock->pBlockAgg = taosMemoryCalloc(numOfCols, POINTER_BYTES);
      }

      for (int32_t i = 0; i < numOfCols; ++i) {
        SColMatchInfo* pColMatchInfo = taosArrayGet(pTableScanInfo->pColMatchInfo, i);
        if (!pColMatchInfo->output) {
          continue;
        }
        pBlock->pBlockAgg[pColMatchInfo->targetSlotId] = pColAgg[i];
      }

      return TSDB_CODE_SUCCESS;
    } else {  // failed to load the block sma data, data block statistics does not exist, load data block instead
      *status = FUNC_DATA_REQUIRED_DATA_LOAD;
    }
  }

  ASSERT(*status == FUNC_DATA_REQUIRED_DATA_LOAD);

  // todo filter data block according to the block sma data firstly
#if 0
  if (!doFilterByBlockStatistics(pBlock->pBlockStatis, pTableScanInfo->pCtx, pBlockInfo->rows)) {
    pCost->filterOutBlocks += 1;
    qDebug("%s data block filter out, brange:%" PRId64 "-%" PRId64 ", rows:%d", GET_TASKID(pTaskInfo), pBlockInfo->window.skey,
           pBlockInfo->window.ekey, pBlockInfo->rows);
    (*status) = FUNC_DATA_REQUIRED_FILTEROUT;
    return TSDB_CODE_SUCCESS;
  }
#endif

  pCost->totalCheckedRows += pBlock->info.rows;
  pCost->loadBlocks += 1;

  tsdbReaderT* reader = taosArrayGetP(pTableScanInfo->dataReaders, readerIdx);
  SArray*      pCols = tsdbRetrieveDataBlock(reader, NULL);
  if (pCols == NULL) {
    return terrno;
  }

2122
  relocateColumnData(pBlock, pTableScanInfo->pColMatchInfo, pCols, true);
2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147

  // currently only the tbname pseudo column
  if (pTableScanInfo->numOfPseudoExpr > 0) {
    addTagPseudoColumnData(&pTableScanInfo->readHandle, pTableScanInfo->pPseudoExpr, pTableScanInfo->numOfPseudoExpr,
                           pBlock);
  }

  int64_t st = taosGetTimestampMs();
  doFilter(pTableScanInfo->pFilterNode, pBlock);

  int64_t et = taosGetTimestampMs();
  pTableScanInfo->readRecorder.filterTime += (et - st);

  if (pBlock->info.rows == 0) {
    pCost->filterOutBlocks += 1;
    qDebug("%s data block filter out, brange:%" PRId64 "-%" PRId64 ", rows:%d", GET_TASKID(pTaskInfo),
           pBlockInfo->window.skey, pBlockInfo->window.ekey, pBlockInfo->rows);
  }

  return TSDB_CODE_SUCCESS;
}

typedef struct STableMergeScanSortSourceParam {
  SOperatorInfo* pOperator;
  int32_t        readerIdx;
2148
  SSDataBlock*   inputBlock;
2149 2150 2151 2152 2153 2154
} STableMergeScanSortSourceParam;

static SSDataBlock* getTableDataBlock(void* param) {
  STableMergeScanSortSourceParam* source = param;
  SOperatorInfo*                  pOperator = source->pOperator;
  int32_t                         readerIdx = source->readerIdx;
2155
  SSDataBlock*                    pBlock = source->inputBlock;
2156 2157 2158 2159
  STableMergeScanInfo*            pTableScanInfo = pOperator->info;

  int64_t st = taosGetTimestampUs();

2160 2161
  blockDataCleanup(pBlock);

2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173
  tsdbReaderT* reader = taosArrayGetP(pTableScanInfo->dataReaders, readerIdx);
  while (tsdbNextDataBlock(reader)) {
    if (isTaskKilled(pOperator->pTaskInfo)) {
      longjmp(pOperator->pTaskInfo->env, TSDB_CODE_TSC_QUERY_CANCELLED);
    }

    // process this data block based on the probabilities
    bool processThisBlock = processBlockWithProbability(&pTableScanInfo->sample);
    if (!processThisBlock) {
      continue;
    }

2174 2175 2176 2177 2178 2179 2180
    blockDataCleanup(pBlock);
    SDataBlockInfo binfo = pBlock->info;
    tsdbRetrieveDataBlockInfo(reader, &binfo);

    binfo.capacity = binfo.rows;
    blockDataEnsureCapacity(pBlock, binfo.capacity);
    pBlock->info = binfo;
2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218

    uint32_t status = 0;
    int32_t  code = loadDataBlockFromOneTable(pOperator, pTableScanInfo, readerIdx, pBlock, &status);
    //    int32_t  code = loadDataBlockOnDemand(pOperator->pRuntimeEnv, pTableScanInfo, pBlock, &status);
    if (code != TSDB_CODE_SUCCESS) {
      longjmp(pOperator->pTaskInfo->env, code);
    }

    // current block is filter out according to filter condition, continue load the next block
    if (status == FUNC_DATA_REQUIRED_FILTEROUT || pBlock->info.rows == 0) {
      continue;
    }

    uint64_t* groupId = taosHashGet(pOperator->pTaskInfo->tableqinfoList.map, &pBlock->info.uid, sizeof(int64_t));
    if (groupId) {
      pBlock->info.groupId = *groupId;
    }

    pOperator->resultInfo.totalRows = pTableScanInfo->readRecorder.totalRows;
    pTableScanInfo->readRecorder.elapsedTime += (taosGetTimestampUs() - st) / 1000.0;

    return pBlock;
  }
  return NULL;
}

SArray* generateSortByTsInfo(int32_t order) {
  SArray*         pList = taosArrayInit(1, sizeof(SBlockOrderInfo));
  SBlockOrderInfo bi = {0};
  bi.order = order;
  bi.slotId = 0;
  bi.nullFirst = NULL_ORDER_FIRST;

  taosArrayPush(pList, &bi);

  return pList;
}

wmmhello's avatar
wmmhello 已提交
2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233
static int32_t createMultipleDataReaders(SQueryTableDataCond* pQueryCond, SReadHandle* pHandle, SArray* tableList, SArray* arrayReader, uint64_t queryId,
                                  uint64_t taskId) {
  for (int32_t i = 0; i < taosArrayGetSize(tableList); ++i) {
    SArray* tmp = taosArrayInit(1, sizeof(STableKeyInfo));
    taosArrayPush(tmp, taosArrayGet(tableList, i));

    tsdbReaderT* pReader = tsdbReaderOpen(pHandle->vnode, pQueryCond, tmp, queryId, taskId);
    taosArrayPush(arrayReader, &pReader);

    taosArrayDestroy(tmp);
  }

  return TSDB_CODE_SUCCESS;
}

2234
int32_t startGroupTableMergeScan(SOperatorInfo* pOperator) {
2235 2236 2237
  STableMergeScanInfo* pInfo = pOperator->info;
  SExecTaskInfo*       pTaskInfo = pOperator->pTaskInfo;

wmmhello's avatar
wmmhello 已提交
2238
  SArray* tableList = taosArrayGetP(pInfo->tableListInfo->pGroupList, pInfo->currentGroupId);
2239

wmmhello's avatar
wmmhello 已提交
2240 2241
  createMultipleDataReaders(&pInfo->cond, &pInfo->readHandle, tableList,
                            pInfo->dataReaders, pInfo->queryId, pInfo->taskId);
2242

2243 2244
  // todo the total available buffer should be determined by total capacity of buffer of this task.
  // the additional one is reserved for merge result
wmmhello's avatar
wmmhello 已提交
2245
  int32_t tableLen = taosArrayGetSize(tableList);
wmmhello's avatar
wmmhello 已提交
2246
  pInfo->sortBufSize = pInfo->bufPageSize * ((tableLen==0?1:tableLen) + 1);
2247
  int32_t numOfBufPage = pInfo->sortBufSize / pInfo->bufPageSize;
L
Liu Jicong 已提交
2248 2249
  pInfo->pSortHandle = tsortCreateSortHandle(pInfo->pSortInfo, SORT_MULTISOURCE_MERGE, pInfo->bufPageSize, numOfBufPage,
                                             pInfo->pSortInputBlock, pTaskInfo->id.str);
2250 2251 2252 2253

  tsortSetFetchRawDataFp(pInfo->pSortHandle, getTableDataBlock, NULL, NULL);

  size_t numReaders = taosArrayGetSize(pInfo->dataReaders);
2254 2255 2256 2257 2258 2259 2260 2261
  for (int32_t i = 0; i < numReaders; ++i) {
    STableMergeScanSortSourceParam param = {0};
    param.readerIdx = i;
    param.pOperator = pOperator;
    param.inputBlock = createOneDataBlock(pInfo->pResBlock, false);
    taosArrayPush(pInfo->sortSourceParams, &param);
  }

2262 2263
  for (int32_t i = 0; i < numReaders; ++i) {
    SSortSource*                    ps = taosMemoryCalloc(1, sizeof(SSortSource));
2264
    STableMergeScanSortSourceParam* param = taosArrayGet(pInfo->sortSourceParams, i);
2265 2266 2267 2268 2269 2270 2271 2272 2273 2274
    ps->param = param;
    tsortAddSource(pInfo->pSortHandle, ps);
  }

  int32_t code = tsortOpen(pInfo->pSortHandle);

  if (code != TSDB_CODE_SUCCESS) {
    longjmp(pTaskInfo->env, terrno);
  }

2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289
  return TSDB_CODE_SUCCESS;
}

int32_t stopGroupTableMergeScan(SOperatorInfo* pOperator) {
  STableMergeScanInfo* pInfo = pOperator->info;
  SExecTaskInfo*       pTaskInfo = pOperator->pTaskInfo;

  tsortDestroySortHandle(pInfo->pSortHandle);
  taosArrayClear(pInfo->sortSourceParams);

  for (int32_t i = 0; i < taosArrayGetSize(pInfo->dataReaders); ++i) {
    tsdbReaderT* reader = taosArrayGetP(pInfo->dataReaders, i);
    tsdbCleanupReadHandle(reader);
  }
  taosArrayDestroy(pInfo->dataReaders);
2290 2291 2292 2293

  return TSDB_CODE_SUCCESS;
}

2294
SSDataBlock* getSortedTableMergeScanBlockData(SSortHandle* pHandle, int32_t capacity, SOperatorInfo* pOperator) {
2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305
  STableMergeScanInfo* pInfo = pOperator->info;
  SExecTaskInfo*       pTaskInfo = pOperator->pTaskInfo;

  SSDataBlock* p = tsortGetSortedDataBlock(pHandle);
  if (p == NULL) {
    return NULL;
  }

  blockDataEnsureCapacity(p, capacity);

  while (1) {
2306
    STupleHandle* pTupleHandle = tsortNextTuple(pHandle);
2307 2308 2309 2310
    if (pTupleHandle == NULL) {
      break;
    }

2311
    appendOneRowToDataBlock(p, pTupleHandle);
2312 2313 2314 2315 2316
    if (p->info.rows >= capacity) {
      break;
    }
  }

2317 2318
  qDebug("%s get sorted row blocks, rows:%d", GET_TASKID(pTaskInfo), p->info.rows);
  return (p->info.rows > 0) ? p : NULL;
2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333
}

SSDataBlock* doTableMergeScan(SOperatorInfo* pOperator) {
  if (pOperator->status == OP_EXEC_DONE) {
    return NULL;
  }

  SExecTaskInfo*       pTaskInfo = pOperator->pTaskInfo;
  STableMergeScanInfo* pInfo = pOperator->info;

  int32_t code = pOperator->fpSet._openFn(pOperator);
  if (code != TSDB_CODE_SUCCESS) {
    longjmp(pTaskInfo->env, code);
  }

wmmhello's avatar
wmmhello 已提交
2334 2335
  if (pInfo->currentGroupId == -1) {
    pInfo->currentGroupId++;
wmmhello's avatar
wmmhello 已提交
2336 2337 2338 2339
    if (pInfo->currentGroupId >= taosArrayGetSize(pInfo->tableListInfo->pGroupList)) {
      doSetOperatorCompleted(pOperator);
      return NULL;
    }
2340 2341
    startGroupTableMergeScan(pOperator);
  }
wmmhello's avatar
wmmhello 已提交
2342 2343 2344 2345 2346 2347 2348
  SSDataBlock* pBlock = getSortedTableMergeScanBlockData(pInfo->pSortHandle, pOperator->resultInfo.capacity, pOperator);
  if (pBlock != NULL) {
    uint64_t* groupId = taosHashGet(pInfo->tableListInfo->map, &(pBlock->info.uid), sizeof(uint64_t));
    if(groupId) pBlock->info.groupId = *groupId;

    pOperator->resultInfo.totalRows += pBlock->info.rows;
    return pBlock;
2349
  }
2350

wmmhello's avatar
wmmhello 已提交
2351
  stopGroupTableMergeScan(pOperator);
wmmhello's avatar
wmmhello 已提交
2352
  pInfo->currentGroupId++;
wmmhello's avatar
wmmhello 已提交
2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369
  if (pInfo->currentGroupId >= taosArrayGetSize(pInfo->tableListInfo->pGroupList)) {
    doSetOperatorCompleted(pOperator);
    return NULL;
  }
  startGroupTableMergeScan(pOperator);

  pBlock = getSortedTableMergeScanBlockData(pInfo->pSortHandle, pOperator->resultInfo.capacity, pOperator);
  if (pBlock != NULL) {
    uint64_t* groupId = taosHashGet(pInfo->tableListInfo->map, &(pBlock->info.uid), sizeof(uint64_t));
    if(groupId) pBlock->info.groupId = *groupId;

    pOperator->resultInfo.totalRows += pBlock->info.rows;
    return pBlock;
  }

  doSetOperatorCompleted(pOperator);

2370 2371 2372 2373 2374
  return pBlock;
}

void destroyTableMergeScanOperatorInfo(void* param, int32_t numOfOutput) {
  STableMergeScanInfo* pTableScanInfo = (STableMergeScanInfo*)param;
2375
  cleanupQueryTableDataCond(&pTableScanInfo->cond);
2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386

  if (pTableScanInfo->pColMatchInfo != NULL) {
    taosArrayDestroy(pTableScanInfo->pColMatchInfo);
  }

  pTableScanInfo->pResBlock = blockDataDestroy(pTableScanInfo->pResBlock);
  pTableScanInfo->pSortInputBlock = blockDataDestroy(pTableScanInfo->pSortInputBlock);

  taosArrayDestroy(pTableScanInfo->pSortInfo);
}

2387 2388
typedef struct STableMergeScanExecInfo {
  SFileBlockLoadRecorder blockRecorder;
L
Liu Jicong 已提交
2389
  SSortExecInfo          sortExecInfo;
2390 2391
} STableMergeScanExecInfo;

2392 2393
int32_t getTableMergeScanExplainExecInfo(SOperatorInfo* pOptr, void** pOptrExplain, uint32_t* len) {
  ASSERT(pOptr != NULL);
2394 2395
  // TODO: merge these two info into one struct
  STableMergeScanExecInfo* execInfo = taosMemoryCalloc(1, sizeof(STableMergeScanExecInfo));
L
Liu Jicong 已提交
2396
  STableMergeScanInfo*     pInfo = pOptr->info;
2397 2398 2399 2400 2401
  execInfo->blockRecorder = pInfo->readRecorder;
  execInfo->sortExecInfo = tsortGetSortExecInfo(pInfo->pSortHandle);

  *pOptrExplain = execInfo;
  *len = sizeof(STableMergeScanExecInfo);
L
Liu Jicong 已提交
2402

2403 2404 2405
  return TSDB_CODE_SUCCESS;
}

2406 2407 2408
SOperatorInfo* createTableMergeScanOperatorInfo(STableScanPhysiNode* pTableScanNode, STableListInfo* pTableListInfo,
                                                SReadHandle* readHandle, SExecTaskInfo* pTaskInfo, uint64_t queryId,
                                                uint64_t taskId) {
2409 2410 2411 2412 2413 2414 2415 2416 2417
  STableMergeScanInfo* pInfo = taosMemoryCalloc(1, sizeof(STableMergeScanInfo));
  SOperatorInfo*       pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
  if (pInfo == NULL || pOperator == NULL) {
    goto _error;
  }

  SDataBlockDescNode* pDescNode = pTableScanNode->scan.node.pOutputDataBlockDesc;

  int32_t numOfCols = 0;
L
Liu Jicong 已提交
2418
  SArray* pColList = extractColMatchInfo(pTableScanNode->scan.pScanCols, pDescNode, &numOfCols, COL_MATCH_FROM_COL_ID);
2419 2420 2421 2422 2423 2424 2425 2426

  int32_t code = initQueryTableDataCond(&pInfo->cond, pTableScanNode);
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }

  if (pTableScanNode->scan.pScanPseudoCols != NULL) {
    pInfo->pPseudoExpr = createExprInfo(pTableScanNode->scan.pScanPseudoCols, NULL, &pInfo->numOfPseudoExpr);
2427
    pInfo->pPseudoCtx = createSqlFunctionCtx(pInfo->pPseudoExpr, pInfo->numOfPseudoExpr, &pInfo->rowEntryInfoOffset);
2428 2429 2430 2431
  }

  pInfo->scanInfo = (SScanInfo){.numOfAsc = pTableScanNode->scanSeq[0], .numOfDesc = pTableScanNode->scanSeq[1]};

L
Liu Jicong 已提交
2432 2433
  pInfo->readHandle = *readHandle;
  pInfo->interval = extractIntervalInfo(pTableScanNode);
2434
  pInfo->sample.sampleRatio = pTableScanNode->ratio;
L
Liu Jicong 已提交
2435 2436 2437
  pInfo->sample.seed = taosGetTimestampSec();
  pInfo->dataBlockLoadFlag = pTableScanNode->dataRequired;
  pInfo->pFilterNode = pTableScanNode->scan.node.pConditions;
2438
  pInfo->tableListInfo = pTableListInfo;
L
Liu Jicong 已提交
2439 2440
  pInfo->scanFlag = MAIN_SCAN;
  pInfo->pColMatchInfo = pColList;
2441 2442

  pInfo->pResBlock = createResDataBlock(pDescNode);
2443 2444 2445
  pInfo->dataReaders = taosArrayInit(64, POINTER_BYTES);
  pInfo->queryId = queryId;
  pInfo->taskId = taskId;
wmmhello's avatar
wmmhello 已提交
2446
  pInfo->currentGroupId = -1;
2447

2448
  pInfo->sortSourceParams = taosArrayInit(64, sizeof(STableMergeScanSortSourceParam));
2449

2450 2451
  pInfo->pSortInfo = generateSortByTsInfo(pInfo->cond.order);
  pInfo->pSortInputBlock = createOneDataBlock(pInfo->pResBlock, false);
2452

2453
  int32_t rowSize = pInfo->pResBlock->info.rowSize;
L
Liu Jicong 已提交
2454
  pInfo->bufPageSize = getProperSortPageSize(rowSize);
2455

L
Liu Jicong 已提交
2456
  pOperator->name = "TableMergeScanOperator";
2457
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN;
L
Liu Jicong 已提交
2458 2459 2460
  pOperator->blocking = false;
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
L
Liu Jicong 已提交
2461
  pOperator->exprSupp.numOfExprs = numOfCols;
L
Liu Jicong 已提交
2462
  pOperator->pTaskInfo = pTaskInfo;
2463 2464 2465
  initResultSizeInfo(pOperator, 1024);

  pOperator->fpSet =
2466 2467
      createOperatorFpSet(operatorDummyOpenFn, doTableMergeScan, NULL, NULL, destroyTableMergeScanOperatorInfo, NULL,
                          NULL, getTableMergeScanExplainExecInfo);
2468 2469 2470 2471 2472 2473 2474 2475 2476
  pOperator->cost.openCost = 0;
  return pOperator;

_error:
  pTaskInfo->code = TSDB_CODE_OUT_OF_MEMORY;
  taosMemoryFree(pInfo);
  taosMemoryFree(pOperator);
  return NULL;
}