scanoperator.c 100.9 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 "executorimpl.h"
17
#include "function.h"
18
#include "functionMgt.h"
H
Haojun Liao 已提交
19 20
#include "os.h"
#include "querynodes.h"
21
#include "systable.h"
H
Haojun Liao 已提交
22
#include "tname.h"
23
#include "ttime.h"
H
Haojun Liao 已提交
24 25 26 27 28 29 30 31 32

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

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

#define SET_REVERSE_SCAN_FLAG(_info) ((_info)->scanFlag = REVERSE_SCAN)
36
#define SWITCH_ORDER(n)              (((n) = ((n) == TSDB_ORDER_ASC) ? TSDB_ORDER_DESC : TSDB_ORDER_ASC))
37

38
static int32_t buildSysDbTableInfo(const SSysTableScanInfo* pInfo, int32_t capacity);
L
Liu Jicong 已提交
39 40
static int32_t buildDbTableInfoBlock(const SSDataBlock* p, const SSysTableMeta* pSysDbTableMeta, size_t size,
                                     const char* dbName);
41

42 43 44
static void addTagPseudoColumnData(SReadHandle* pHandle, SExprInfo* pPseudoExpr, int32_t numOfPseudoExpr,
                                   SSDataBlock* pBlock);
static bool processBlockWithProbability(const SSampleExecInfo* pInfo);
45

46
bool processBlockWithProbability(const SSampleExecInfo* pInfo) {
47 48 49 50 51 52 53 54 55 56 57 58
#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
}

59
static void switchCtxOrder(SqlFunctionCtx* pCtx, int32_t numOfOutput) {
H
Haojun Liao 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  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
}

88 89 90 91 92 93 94 95 96
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;
97
  // convert key to second
98 99 100 101 102 103 104
  key = convertTimePrecision(key, pInterval->precision, TSDB_TIME_PRECISION_MILLI) / 1000;

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

  struct tm tm;
105
  time_t    t = (time_t)key;
106 107 108 109 110
  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 已提交
111
  tw->skey = convertTimePrecision((int64_t)taosMktime(&tm) * 1000LL, TSDB_TIME_PRECISION_MILLI, pInterval->precision);
112 113 114 115

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

  tw->ekey -= 1;
}

121
static bool overlapWithTimeWindow(SInterval* pInterval, SDataBlockInfo* pBlockInfo, int32_t order) {
122 123 124 125 126 127 128
  STimeWindow w = {0};

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

129
  if (order == TSDB_ORDER_ASC) {
130
    getAlignQueryTimeWindow(pInterval, pInterval->precision, pBlockInfo->window.skey, &w);
131 132 133 134 135 136
    assert(w.ekey >= pBlockInfo->window.skey);

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

137 138
    while (1) {
      getNextTimeWindow(pInterval, &w, order);
139 140 141 142 143 144 145 146 147 148
      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 {
149 150 151 152 153 154 155
    getAlignQueryTimeWindow(pInterval, pInterval->precision, pBlockInfo->window.ekey, &w);
    assert(w.skey <= pBlockInfo->window.ekey);

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

156
    while (1) {
157 158 159 160 161 162 163 164 165 166
      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;
      }
    }
167 168 169 170 171
  }

  return false;
}

L
Liu Jicong 已提交
172 173
static int32_t loadDataBlock(SOperatorInfo* pOperator, STableScanInfo* pTableScanInfo, SSDataBlock* pBlock,
                             uint32_t* status) {
174
  SExecTaskInfo*  pTaskInfo = pOperator->pTaskInfo;
175 176
  STableScanInfo* pInfo = pOperator->info;

177
  SFileBlockLoadRecorder* pCost = &pTableScanInfo->readRecorder;
H
Haojun Liao 已提交
178 179

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

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

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

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

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

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

L
Liu Jicong 已提交
211
    bool             allColumnsHaveAgg = true;
212
    SColumnDataAgg** pColAgg = NULL;
213
    tsdbRetrieveDataBlockStatisInfo(pTableScanInfo->dataReader, &pColAgg, &allColumnsHaveAgg);
214

215
    if (allColumnsHaveAgg == true) {
216
      int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
217 218

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

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

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

237
  ASSERT(*status == FUNC_DATA_REQUIRED_DATA_LOAD);
238

H
Haojun Liao 已提交
239 240 241 242 243 244 245 246 247 248
  // 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 已提交
249

H
Haojun Liao 已提交
250 251
  pCost->totalCheckedRows += pBlock->info.rows;
  pCost->loadBlocks += 1;
252

H
Haojun Liao 已提交
253 254 255
  SArray* pCols = tsdbRetrieveDataBlock(pTableScanInfo->dataReader, NULL);
  if (pCols == NULL) {
    return terrno;
H
Haojun Liao 已提交
256 257
  }

258
  relocateColumnData(pBlock, pTableScanInfo->pColMatchInfo, pCols, true);
259 260

  // currently only the tbname pseudo column
261
  if (pTableScanInfo->pseudoSup.numOfExprs > 0) {
262
    SExprSupp* pSup = &pTableScanInfo->pseudoSup;
263
    addTagPseudoColumnData(&pTableScanInfo->readHandle, pSup->pExprInfo, pSup->numOfExprs, pBlock);
264 265
  }

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

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

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

H
Haojun Liao 已提交
278 279 280
  return TSDB_CODE_SUCCESS;
}

281
static void prepareForDescendingScan(STableScanInfo* pTableScanInfo, SqlFunctionCtx* pCtx, int32_t numOfOutput) {
H
Haojun Liao 已提交
282 283 284
  SET_REVERSE_SCAN_FLAG(pTableScanInfo);

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

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

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

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

  SMetaReader mr = {0};
305
  metaReaderInit(&mr, pHandle->meta, 0);
306 307
  metaGetTableEntryByUid(&mr, pBlock->info.uid);

308 309
  for (int32_t j = 0; j < numOfPseudoExpr; ++j) {
    SExprInfo* pExpr = &pPseudoExpr[j];
310 311 312 313

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

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

315
    colInfoDataEnsureCapacity(pColInfoData, pBlock->info.rows);
D
dapan1121 已提交
316
    colInfoDataCleanup(pColInfoData, pBlock->info.rows);
317 318 319 320 321

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

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

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

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

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

  metaReaderClear(&mr);
}

350 351 352 353
void setTbNameColData(void* pMeta, const SSDataBlock* pBlock, SColumnInfoData* pColInfoData, int32_t functionId) {
  struct SScalarFuncExecFuncs fpSet = {0};
  fmGetScalarFuncExecFuncs(functionId, &fpSet);

354 355
  SColumnInfoData infoData = createColumnInfoData(TSDB_DATA_TYPE_BIGINT, sizeof(uint64_t), 1);
  colInfoDataEnsureCapacity(&infoData, 1);
356

357
  colDataAppendInt64(&infoData, 0, (int64_t*)&pBlock->info.uid);
358
  SScalarParam srcParam = {.numOfRows = pBlock->info.rows, .param = pMeta, .columnData = &infoData};
359 360 361 362 363

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

364
static SSDataBlock* doTableScanImpl(SOperatorInfo* pOperator) {
H
Haojun Liao 已提交
365
  STableScanInfo* pTableScanInfo = pOperator->info;
366
  SExecTaskInfo*  pTaskInfo = pOperator->pTaskInfo;
L
Liu Jicong 已提交
367
  SSDataBlock*    pBlock = pTableScanInfo->pResBlock;
H
Haojun Liao 已提交
368

369 370
  int64_t st = taosGetTimestampUs();

371
  while (tsdbNextDataBlock(pTableScanInfo->dataReader)) {
372 373
    if (isTaskKilled(pTaskInfo)) {
      longjmp(pTaskInfo->env, TSDB_CODE_TSC_QUERY_CANCELLED);
374
    }
H
Haojun Liao 已提交
375

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

382 383 384 385 386 387 388 389
    blockDataCleanup(pBlock);

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

    binfo.capacity = binfo.rows;
    blockDataEnsureCapacity(pBlock, binfo.rows);
    pBlock->info = binfo;
L
Liu Jicong 已提交
390
    ASSERT(binfo.uid != 0);
H
Haojun Liao 已提交
391

392 393 394 395 396 397
    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);
    }
398

399 400 401
    // 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;
402
    }
403

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

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

    pOperator->cost.totalCost = pTableScanInfo->readRecorder.elapsedTime;
413 414

    // todo refactor
L
Liu Jicong 已提交
415 416
    pTableScanInfo->lastStatus.uid = pBlock->info.uid;
    pTableScanInfo->lastStatus.ts = pBlock->info.window.ekey;
417

L
Liu Jicong 已提交
418
    ASSERT(pBlock->info.uid != 0);
419
    return pBlock;
H
Haojun Liao 已提交
420 421 422 423
  }
  return NULL;
}

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

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

433 434
  // do the ascending order traverse in the first place.
  while (pTableScanInfo->scanTimes < pTableScanInfo->scanInfo.numOfAsc) {
435 436 437
    while (pTableScanInfo->curTWinIdx < pTableScanInfo->cond.numOfTWindows) {
      SSDataBlock* p = doTableScanImpl(pOperator);
      if (p != NULL) {
L
Liu Jicong 已提交
438
        ASSERT(p->info.uid != 0);
439 440 441 442
        return p;
      }
      pTableScanInfo->curTWinIdx += 1;
      if (pTableScanInfo->curTWinIdx < pTableScanInfo->cond.numOfTWindows) {
443
        tsdbReaderReset(pTableScanInfo->dataReader, &pTableScanInfo->cond, pTableScanInfo->curTWinIdx);
444
      }
H
Haojun Liao 已提交
445 446
    }

447
    pTableScanInfo->scanTimes += 1;
448

449
    if (pTableScanInfo->scanTimes < pTableScanInfo->scanInfo.numOfAsc) {
450 451
      setTaskStatus(pTaskInfo, TASK_NOT_COMPLETED);
      pTableScanInfo->scanFlag = REPEAT_SCAN;
452 453 454
      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];
455
        qDebug("%s qrange:%" PRId64 "-%" PRId64, GET_TASKID(pTaskInfo), pWin->skey, pWin->ekey);
456
      }
457
      // do prepare for the next round table scan operation
458
      tsdbReaderReset(pTableScanInfo->dataReader, &pTableScanInfo->cond, 0);
459
      pTableScanInfo->curTWinIdx = 0;
H
Haojun Liao 已提交
460
    }
461
  }
H
Haojun Liao 已提交
462

463
  int32_t total = pTableScanInfo->scanInfo.numOfAsc + pTableScanInfo->scanInfo.numOfDesc;
464
  if (pTableScanInfo->scanTimes < total) {
465
    if (pTableScanInfo->cond.order == TSDB_ORDER_ASC) {
466
      prepareForDescendingScan(pTableScanInfo, pTableScanInfo->pCtx, 0);
467
      tsdbReaderReset(pTableScanInfo->dataReader, &pTableScanInfo->cond, 0);
468
      pTableScanInfo->curTWinIdx = 0;
469
    }
H
Haojun Liao 已提交
470

471 472 473
    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];
474
      qDebug("%s qrange:%" PRId64 "-%" PRId64, GET_TASKID(pTaskInfo), pWin->skey, pWin->ekey);
475
    }
476

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

489
      pTableScanInfo->scanTimes += 1;
H
Haojun Liao 已提交
490

491
      if (pTableScanInfo->scanTimes < total) {
492 493
        setTaskStatus(pTaskInfo, TASK_NOT_COMPLETED);
        pTableScanInfo->scanFlag = REPEAT_SCAN;
H
Haojun Liao 已提交
494

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

wmmhello's avatar
wmmhello 已提交
507 508 509 510 511 512 513
  return NULL;
}

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

L
Liu Jicong 已提交
514 515
  // if scan table by table
  if (pInfo->scanMode == TABLE_SCAN__TABLE_ORDER) {
L
Liu Jicong 已提交
516
    if (pInfo->noTable) return NULL;
L
Liu Jicong 已提交
517
    while (1) {
L
Liu Jicong 已提交
518 519 520 521 522 523 524 525 526 527 528
      SSDataBlock* result = doTableScanGroup(pOperator);
      if (result) {
        return result;
      }
      // if no data, switch to next table and continue scan
      pInfo->currentTable++;
      if (pInfo->currentTable >= taosArrayGetSize(pTaskInfo->tableqinfoList.pTableList)) {
        return NULL;
      }
      STableKeyInfo* pTableInfo = taosArrayGet(pTaskInfo->tableqinfoList.pTableList, pInfo->currentTable);
      tsdbSetTableId(pInfo->dataReader, pTableInfo->uid);
529
      tsdbReaderReset(pInfo->dataReader, &pInfo->cond, 0);
L
Liu Jicong 已提交
530 531 532 533 534
      pInfo->scanTimes = 0;
      pInfo->curTWinIdx = 0;
    }
  }

535
  if (pInfo->currentGroupId == -1) {
wmmhello's avatar
wmmhello 已提交
536
    pInfo->currentGroupId++;
wmmhello's avatar
wmmhello 已提交
537
    if (pInfo->currentGroupId >= taosArrayGetSize(pTaskInfo->tableqinfoList.pGroupList)) {
wmmhello's avatar
wmmhello 已提交
538
      setTaskStatus(pTaskInfo, TASK_COMPLETED);
wmmhello's avatar
wmmhello 已提交
539 540
      return NULL;
    }
H
Haojun Liao 已提交
541

542
    SArray* tableList = taosArrayGetP(pTaskInfo->tableqinfoList.pGroupList, pInfo->currentGroupId);
H
Haojun Liao 已提交
543 544
    tsdbReaderClose(pInfo->dataReader);

545 546
    int32_t code = tsdbReaderOpen(pInfo->readHandle.vnode, &pInfo->cond, tableList, (STsdbReader**)&pInfo->dataReader,
                                  GET_TASKID(pTaskInfo));
wmmhello's avatar
wmmhello 已提交
547 548 549
  }

  SSDataBlock* result = doTableScanGroup(pOperator);
550
  if (result) {
wmmhello's avatar
wmmhello 已提交
551 552 553 554 555
    return result;
  }

  pInfo->currentGroupId++;
  if (pInfo->currentGroupId >= taosArrayGetSize(pTaskInfo->tableqinfoList.pGroupList)) {
wmmhello's avatar
wmmhello 已提交
556
    setTaskStatus(pTaskInfo, TASK_COMPLETED);
wmmhello's avatar
wmmhello 已提交
557 558 559
    return NULL;
  }

560
  SArray* tableList = taosArrayGetP(pTaskInfo->tableqinfoList.pGroupList, pInfo->currentGroupId);
561
  //  tsdbSetTableList(pInfo->dataReader, tableList);
wmmhello's avatar
wmmhello 已提交
562

563
  tsdbReaderReset(pInfo->dataReader, &pInfo->cond, 0);
wmmhello's avatar
wmmhello 已提交
564 565 566 567
  pInfo->curTWinIdx = 0;
  pInfo->scanTimes = 0;

  result = doTableScanGroup(pOperator);
568
  if (result) {
wmmhello's avatar
wmmhello 已提交
569 570 571
    return result;
  }

572 573
  setTaskStatus(pTaskInfo, TASK_COMPLETED);
  return NULL;
H
Haojun Liao 已提交
574 575
}

576 577
static int32_t getTableScannerExecInfo(struct SOperatorInfo* pOptr, void** pOptrExplain, uint32_t* len) {
  SFileBlockLoadRecorder* pRecorder = taosMemoryCalloc(1, sizeof(SFileBlockLoadRecorder));
578
  STableScanInfo*         pTableScanInfo = pOptr->info;
579 580 581 582 583 584
  *pRecorder = pTableScanInfo->readRecorder;
  *pOptrExplain = pRecorder;
  *len = sizeof(SFileBlockLoadRecorder);
  return 0;
}

585 586
static void destroyTableScanOperatorInfo(void* param, int32_t numOfOutput) {
  STableScanInfo* pTableScanInfo = (STableScanInfo*)param;
H
Haojun Liao 已提交
587
  blockDataDestroy(pTableScanInfo->pResBlock);
588
  cleanupQueryTableDataCond(&pTableScanInfo->cond);
H
Haojun Liao 已提交
589

H
refact  
Hongze Cheng 已提交
590
  tsdbReaderClose(pTableScanInfo->dataReader);
591 592 593 594 595 596

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

wmmhello's avatar
wmmhello 已提交
597
SOperatorInfo* createTableScanOperatorInfo(STableScanPhysiNode* pTableScanNode, SReadHandle* readHandle,
598
                                           SExecTaskInfo* pTaskInfo) {
H
Haojun Liao 已提交
599 600 601
  STableScanInfo* pInfo = taosMemoryCalloc(1, sizeof(STableScanInfo));
  SOperatorInfo*  pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
  if (pInfo == NULL || pOperator == NULL) {
602
    goto _error;
H
Haojun Liao 已提交
603 604
  }

605
  SDataBlockDescNode* pDescNode = pTableScanNode->scan.node.pOutputDataBlockDesc;
606
  int32_t             numOfCols = 0;
607
  SArray* pColList = extractColMatchInfo(pTableScanNode->scan.pScanCols, pDescNode, &numOfCols, COL_MATCH_FROM_COL_ID);
L
Liu Jicong 已提交
608

609 610
  int32_t code = initQueryTableDataCond(&pInfo->cond, pTableScanNode);
  if (code != TSDB_CODE_SUCCESS) {
611
    goto _error;
612 613 614
  }

  if (pTableScanNode->scan.pScanPseudoCols != NULL) {
615
    SExprSupp* pSup = &pInfo->pseudoSup;
616 617
    pSup->pExprInfo = createExprInfo(pTableScanNode->scan.pScanPseudoCols, NULL, &pSup->numOfExprs);
    pSup->pCtx = createSqlFunctionCtx(pSup->pExprInfo, pSup->numOfExprs, &pSup->rowEntryInfoOffset);
618 619
  }

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

623 624 625 626
  pInfo->readHandle = *readHandle;
  pInfo->interval = extractIntervalInfo(pTableScanNode);
  pInfo->sample.sampleRatio = pTableScanNode->ratio;
  pInfo->sample.seed = taosGetTimestampSec();
627

628
  pInfo->dataBlockLoadFlag = pTableScanNode->dataRequired;
629 630 631 632 633
  pInfo->pResBlock = createResDataBlock(pDescNode);
  pInfo->pFilterNode = pTableScanNode->scan.node.pConditions;
  pInfo->scanFlag = MAIN_SCAN;
  pInfo->pColMatchInfo = pColList;
  pInfo->curTWinIdx = 0;
wmmhello's avatar
wmmhello 已提交
634
  pInfo->currentGroupId = -1;
635 636

  pOperator->name = "TableScanOperator";  // for debug purpose
L
Liu Jicong 已提交
637
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN;
638 639 640
  pOperator->blocking = false;
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
641
  pOperator->exprSupp.numOfExprs = numOfCols;
642
  pOperator->pTaskInfo = pTaskInfo;
643

644 645
  pOperator->fpSet = createOperatorFpSet(operatorDummyOpenFn, doTableScan, NULL, NULL, destroyTableScanOperatorInfo,
                                         NULL, NULL, getTableScannerExecInfo);
646 647 648

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

651
_error:
652 653 654 655 656
  taosMemoryFreeClear(pInfo);
  taosMemoryFreeClear(pOperator);

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

659
SOperatorInfo* createTableSeqScanOperatorInfo(void* pReadHandle, SExecTaskInfo* pTaskInfo) {
H
Haojun Liao 已提交
660
  STableScanInfo* pInfo = taosMemoryCalloc(1, sizeof(STableScanInfo));
L
Liu Jicong 已提交
661
  SOperatorInfo*  pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
H
Haojun Liao 已提交
662

L
Liu Jicong 已提交
663 664
  pInfo->dataReader = pReadHandle;
  //  pInfo->prevGroupId       = -1;
H
Haojun Liao 已提交
665

L
Liu Jicong 已提交
666
  pOperator->name = "TableSeqScanOperator";
H
Haojun Liao 已提交
667
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_TABLE_SEQ_SCAN;
L
Liu Jicong 已提交
668 669 670 671
  pOperator->blocking = false;
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
  pOperator->pTaskInfo = pTaskInfo;
H
Haojun Liao 已提交
672

673
  pOperator->fpSet = createOperatorFpSet(operatorDummyOpenFn, doTableScanImpl, NULL, NULL, NULL, NULL, NULL, NULL);
H
Haojun Liao 已提交
674 675 676
  return pOperator;
}

677 678
static int32_t doGetTableRowSize(void* pMeta, uint64_t uid) {
  int32_t rowLen = 0;
H
Haojun Liao 已提交
679

680
  SMetaReader mr = {0};
681 682
  metaReaderInit(&mr, pMeta, 0);
  metaGetTableEntryByUid(&mr, uid);
683 684
  if (mr.me.type == TSDB_SUPER_TABLE) {
    int32_t numOfCols = mr.me.stbEntry.schemaRow.nCols;
685
    for (int32_t i = 0; i < numOfCols; ++i) {
686 687 688 689 690 691 692
      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;

693
    for (int32_t i = 0; i < numOfCols; ++i) {
694 695 696 697
      rowLen += mr.me.stbEntry.schemaRow.pSchema[i].bytes;
    }
  } else if (mr.me.type == TSDB_NORMAL_TABLE) {
    int32_t numOfCols = mr.me.ntbEntry.schemaRow.nCols;
698
    for (int32_t i = 0; i < numOfCols; ++i) {
699 700 701 702 703
      rowLen += mr.me.ntbEntry.schemaRow.pSchema[i].bytes;
    }
  }

  metaReaderClear(&mr);
704 705 706
  return rowLen;
}

707
static SSDataBlock* doBlockInfoScan(SOperatorInfo* pOperator) {
H
Haojun Liao 已提交
708 709 710 711
  if (pOperator->status == OP_EXEC_DONE) {
    return NULL;
  }

712
  SBlockDistInfo* pBlockScanInfo = pOperator->info;
H
Haojun Liao 已提交
713

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

717 718
  tsdbGetFileBlocksDistInfo(pBlockScanInfo->pHandle, &blockDistInfo);
  blockDistInfo.numOfInmemRows = (int32_t)tsdbGetNumOfRowsInMemTable(pBlockScanInfo->pHandle);
H
Haojun Liao 已提交
719

720
  SSDataBlock* pBlock = pBlockScanInfo->pResBlock;
H
Haojun Liao 已提交
721

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

725
  int32_t len = tSerializeBlockDistInfo(NULL, 0, &blockDistInfo);
726
  char*   p = taosMemoryCalloc(1, len + VARSTR_HEADER_SIZE);
727 728 729
  tSerializeBlockDistInfo(varDataVal(p), len, &blockDistInfo);
  varDataSetLen(p, len);

730
  blockDataEnsureCapacity(pBlock, 1);
731 732
  colDataAppend(pColInfo, 0, p, false);
  taosMemoryFree(p);
H
Haojun Liao 已提交
733

734 735
  pBlock->info.rows = 1;

H
Haojun Liao 已提交
736 737 738 739
  pOperator->status = OP_EXEC_DONE;
  return pBlock;
}

740
static void destroyBlockDistScanOperatorInfo(void* param, int32_t numOfOutput) {
741
  SBlockDistInfo* pDistInfo = (SBlockDistInfo*)param;
742 743 744
  blockDataDestroy(pDistInfo->pResBlock);
}

745 746
SOperatorInfo* createDataBlockInfoScanOperator(void* dataReader, SReadHandle* readHandle, uint64_t uid,
                                               SBlockDistScanPhysiNode* pBlockScanNode, SExecTaskInfo* pTaskInfo) {
747
  SBlockDistInfo* pInfo = taosMemoryCalloc(1, sizeof(SBlockDistInfo));
748
  SOperatorInfo*  pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
H
Haojun Liao 已提交
749 750 751 752 753
  if (pInfo == NULL || pOperator == NULL) {
    pTaskInfo->code = TSDB_CODE_OUT_OF_MEMORY;
    goto _error;
  }

754
  pInfo->pHandle = dataReader;
755
  pInfo->readHandle = *readHandle;
756 757
  pInfo->uid = uid;
  pInfo->pResBlock = createResDataBlock(pBlockScanNode->node.pOutputDataBlockDesc);
758

759
  int32_t    numOfCols = 0;
760
  SExprInfo* pExprInfo = createExprInfo(pBlockScanNode->pScanPseudoCols, NULL, &numOfCols);
761
  int32_t    code = initExprSupp(&pOperator->exprSupp, pExprInfo, numOfCols);
762 763 764
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
H
Haojun Liao 已提交
765

766
  pOperator->name = "DataBlockDistScanOperator";
767
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_BLOCK_DIST_SCAN;
768 769 770 771 772 773 774
  pOperator->blocking = false;
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
  pOperator->pTaskInfo = pTaskInfo;

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

777
_error:
H
Haojun Liao 已提交
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
  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 已提交
794
static bool isSessionWindow(SStreamBlockScanInfo* pInfo) {
5
54liuyao 已提交
795 796
  return pInfo->sessionSup.parentType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SESSION ||
      pInfo->sessionSup.parentType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SESSION;
5
54liuyao 已提交
797 798
}

H
Haojun Liao 已提交
799
static bool isStateWindow(SStreamBlockScanInfo* pInfo) {
800
  return pInfo->sessionSup.parentType == QUERY_NODE_PHYSICAL_PLAN_STREAM_STATE;
5
54liuyao 已提交
801
}
5
54liuyao 已提交
802

5
54liuyao 已提交
803 804 805 806 807 808
static bool isIntervalWindow(SStreamBlockScanInfo* pInfo) {
  return pInfo->sessionSup.parentType == QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL ||
      pInfo->sessionSup.parentType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_INTERVAL;
}


809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
static uint64_t getGroupId(SOperatorInfo* pOperator, uint64_t uid) {
  uint64_t* groupId = taosHashGet(pOperator->pTaskInfo->tableqinfoList.map, &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;
  */
}

830 831 832 833
static void setGroupId(SStreamBlockScanInfo* pInfo, SSDataBlock* pBlock, int32_t groupColIndex, int32_t rowIndex) {
  ASSERT(rowIndex < pBlock->info.rows);
  switch (pBlock->info.type)
  {
834
  case STREAM_DELETE_DATA:
835 836 837 838 839 840 841 842 843 844 845
  case STREAM_RETRIEVE: {
    SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, groupColIndex);
    uint64_t* groupCol = (uint64_t*)pColInfo->pData;
    pInfo->groupId = groupCol[rowIndex];
  }
    break;
  default:
    break;
  }
}

5
54liuyao 已提交
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
void resetTableScanInfo(STableScanInfo* pTableScanInfo, STimeWindow* pWin) {
  pTableScanInfo->cond.twindows[0] = *pWin;
  pTableScanInfo->curTWinIdx = 0;
  //  tsdbResetReadHandle(pTableScanInfo->dataReader, &pTableScanInfo->cond, 0);
  // if (!pTableScanInfo->dataReader) {
  //   return false;
  // }
  pTableScanInfo->scanTimes = 0;
  pTableScanInfo->currentGroupId = -1;
}

static bool prepareRangeScan(SStreamBlockScanInfo* pInfo, SSDataBlock* pBlock, int32_t* pRowIndex) {
  if ((*pRowIndex) == pBlock->info.rows) {
    return false;
  }

  ASSERT(taosArrayGetSize(pBlock->pDataBlock) >= 3);
  SColumnInfoData* pStartTsCol = taosArrayGet(pBlock->pDataBlock, START_TS_COLUMN_INDEX);
  TSKEY* startData = (TSKEY*)pStartTsCol->pData;
  SColumnInfoData* pEndTsCol = taosArrayGet(pBlock->pDataBlock, END_TS_COLUMN_INDEX);
  TSKEY* endData = (TSKEY*)pEndTsCol->pData;
  STimeWindow win = {.skey = startData[*pRowIndex], .ekey = endData[*pRowIndex]};
  setGroupId(pInfo, pBlock, GROUPID_COLUMN_INDEX, *pRowIndex);
  (*pRowIndex)++;

  for (; *pRowIndex < pBlock->info.rows; (*pRowIndex)++) {
    if (win.skey == startData[*pRowIndex]) {
      win.ekey = TMAX(win.ekey, endData[*pRowIndex]);
      continue;
    }
    if (win.skey == endData[*pRowIndex]) {
      win.skey = TMIN(win.skey, startData[*pRowIndex]);
      continue;
    }
    ASSERT( (win.skey > startData[*pRowIndex] && win.ekey < endData[*pRowIndex]) ||
            ( isInTimeWindow(&win, startData[*pRowIndex], 0) || isInTimeWindow(&win, endData[*pRowIndex], 0) ) );
    break;
  }

  resetTableScanInfo(pInfo->pSnapshotReadOp->info, &win);
  return true;
}

5
54liuyao 已提交
889
static bool prepareDataScan(SStreamBlockScanInfo* pInfo, SSDataBlock* pSDB, int32_t tsColIndex, int32_t* pRowIndex) {
890 891 892
  STimeWindow win = {
      .skey = INT64_MIN,
      .ekey = INT64_MAX,
893
  };
5
54liuyao 已提交
894
  bool needRead = false;
5
54liuyao 已提交
895 896
  if (!isStateWindow(pInfo) && (*pRowIndex) < pSDB->info.rows) {
    SColumnInfoData* pColDataInfo = taosArrayGet(pSDB->pDataBlock, tsColIndex);
897 898
    TSKEY*           tsCols = (TSKEY*)pColDataInfo->pData;
    SResultRowInfo   dumyInfo;
5
54liuyao 已提交
899
    dumyInfo.cur.pageId = -1;
5
54liuyao 已提交
900 901
    if (isSessionWindow(pInfo)) {
      SStreamAggSupporter* pAggSup = pInfo->sessionSup.pStreamAggSup;
902 903 904
      int64_t              gap = pInfo->sessionSup.gap;
      int32_t              winIndex = 0;
      SResultWindowInfo*   pCurWin =
905
          getSessionTimeWindow(pAggSup, tsCols[*pRowIndex], INT64_MIN, pSDB->info.groupId, gap, &winIndex);
5
54liuyao 已提交
906
      win = pCurWin->win;
5
54liuyao 已提交
907
      setGroupId(pInfo, pSDB, GROUPID_COLUMN_INDEX, *pRowIndex);
908
      (*pRowIndex) += updateSessionWindowInfo(pCurWin, tsCols, NULL, pSDB->info.rows, *pRowIndex, gap, NULL);
5
54liuyao 已提交
909
    } else {
910
      win =
911 912 913
          getActiveTimeWindow(NULL, &dumyInfo, tsCols[*pRowIndex], &pInfo->interval, pInfo->interval.precision, NULL);
      setGroupId(pInfo, pSDB, GROUPID_COLUMN_INDEX, *pRowIndex);
      (*pRowIndex) += getNumOfRowsInTimeWindow(&pSDB->info, tsCols, *pRowIndex, win.ekey, binarySearchForKey, NULL,
914
                                               TSDB_ORDER_ASC);
5
54liuyao 已提交
915
    }
5
54liuyao 已提交
916 917 918 919 920
    needRead = true;
  } else if (isStateWindow(pInfo)) {
    SArray* pWins = pInfo->sessionSup.pStreamAggSup->pScanWindow;
    int32_t size = taosArrayGetSize(pWins);
    if (pInfo->scanWinIndex < size) {
921
      win = *(STimeWindow*)taosArrayGet(pWins, pInfo->scanWinIndex);
5
54liuyao 已提交
922 923 924 925 926 927 928 929
      pInfo->scanWinIndex++;
      needRead = true;
    } else {
      pInfo->scanWinIndex = 0;
      taosArrayClear(pWins);
    }
  }
  if (!needRead) {
5
54liuyao 已提交
930 931
    return false;
  }
5
54liuyao 已提交
932
  resetTableScanInfo(pInfo->pSnapshotReadOp->info, &win);
5
54liuyao 已提交
933
  return true;
5
54liuyao 已提交
934 935
}

936
static void copyOneRow(SSDataBlock* dest, SSDataBlock* source, int32_t sourceRowId) {
937
  for (int32_t j = 0; j < taosArrayGetSize(source->pDataBlock); j++) {
938 939 940 941 942 943
    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 已提交
944 945
    }
  }
946
  dest->info.rows++;
5
54liuyao 已提交
947 948
}

5
54liuyao 已提交
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
static SSDataBlock* doRangeScan(SStreamBlockScanInfo* pInfo, SSDataBlock* pSDB, int32_t tsColIndex, int32_t* pRowIndex) {
  while (1) {
    SSDataBlock* pResult = NULL;
    pResult = doTableScan(pInfo->pSnapshotReadOp);
    if (!pResult && prepareRangeScan(pInfo, pSDB, pRowIndex)) {
      // scan next window data
      pResult = doTableScan(pInfo->pSnapshotReadOp);
    }
    if (!pResult) {
      blockDataCleanup(pSDB);
      *pRowIndex = 0;
      return NULL;
    }

    if (pResult->info.groupId == pInfo->groupId) {
      return pResult;
    }
  }
}

5
54liuyao 已提交
969
static SSDataBlock* doDataScan(SStreamBlockScanInfo* pInfo, SSDataBlock* pSDB, int32_t tsColIndex, int32_t* pRowIndex) {
970 971
  while (1) {
    SSDataBlock* pResult = NULL;
L
Liu Jicong 已提交
972
    pResult = doTableScan(pInfo->pSnapshotReadOp);
973
    if (pResult == NULL) {
5
54liuyao 已提交
974
      if (prepareDataScan(pInfo, pSDB, tsColIndex, pRowIndex)) {
975
        // scan next window data
L
Liu Jicong 已提交
976
        pResult = doTableScan(pInfo->pSnapshotReadOp);
977 978 979 980 981
      }
    }
    if (!pResult) {
      return NULL;
    }
H
Hongze Cheng 已提交
982

983 984
    if (pResult->info.groupId == pInfo->groupId) {
      return pResult;
5
54liuyao 已提交
985 986
    }
  }
987

H
Hongze Cheng 已提交
988 989 990 991
  /* Todo(liuyao) for partition by column
    SSDataBlock* pBlock = createOneDataBlock(pResult, true);
    blockDataCleanup(pResult);
    for (int32_t i = 0; i < pBlock->info.rows; i++) {
992
      uint64_t id = getGroupId(pInfo->pOperatorDumy, pBlock->info.uid);
H
Hongze Cheng 已提交
993 994 995
      if (id == pInfo->groupId) {
        copyOneRow(pResult, pBlock, i);
      }
996
    }
H
Hongze Cheng 已提交
997 998
    return pResult;
  */
999 1000
}

5
54liuyao 已提交
1001
static void generateIntervalTs(SStreamBlockScanInfo* pInfo, SSDataBlock* pDelBlock, SOperatorInfo* pOperator, SSDataBlock* pUpdateRes) {
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
  if (pDelBlock->info.rows == 0) {
    return;
  }
  blockDataCleanup(pUpdateRes);
  blockDataEnsureCapacity(pUpdateRes, 64);
  ASSERT(taosArrayGetSize(pDelBlock->pDataBlock) >= 3);
  SColumnInfoData* pStartTsCol = taosArrayGet(pDelBlock->pDataBlock, START_TS_COLUMN_INDEX);
  TSKEY* startData = (TSKEY*)pStartTsCol->pData;
  SColumnInfoData* pEndTsCol = taosArrayGet(pDelBlock->pDataBlock, END_TS_COLUMN_INDEX);
  TSKEY* endData = (TSKEY*)pEndTsCol->pData;
  SColumnInfoData* pGpCol = taosArrayGet(pDelBlock->pDataBlock, UID_COLUMN_INDEX);
  uint64_t* uidCol = (uint64_t*)pGpCol->pData;

  SColumnInfoData* pDestTsCol = taosArrayGet(pUpdateRes->pDataBlock, START_TS_COLUMN_INDEX);
5
54liuyao 已提交
1016
  SColumnInfoData* pDestGpCol = taosArrayGet(pUpdateRes->pDataBlock, GROUPID_COLUMN_INDEX);
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
  for (int32_t i = pInfo->deleteDataIndex ; i < pDelBlock->info.rows &&
      i < pDelBlock->info.capacity - (endData[i] - startData[i])/pInfo->interval.interval - 1; i++) {
    uint64_t groupId = getGroupId(pOperator, uidCol[i]);
    for (TSKEY startTs = startData[i]; startTs <= endData[i]; ) {
      colDataAppend(pDestTsCol, pUpdateRes->info.rows, (const char*)&startTs, false);
      colDataAppend(pDestGpCol, pUpdateRes->info.rows, (const char*)&groupId, false);
      pUpdateRes->info.rows++;
      startTs = taosTimeAdd(startTs, pInfo->interval.interval, pInfo->interval.intervalUnit, pInfo->interval.precision);
    }
    pInfo->deleteDataIndex++;
  }

  if (pInfo->deleteDataIndex > 0 && pInfo->deleteDataIndex == pDelBlock->info.rows) {
    blockDataCleanup(pDelBlock);
    pInfo->deleteDataIndex = 0;
  }
}

5
54liuyao 已提交
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
static void generateScanRange(SStreamBlockScanInfo* pInfo, SSDataBlock* pBlock, SOperatorInfo* pOperator, SSDataBlock* pUpdateRes) {
   if (pBlock->info.rows == 0) {
    return;
  }
  blockDataCleanup(pUpdateRes);
  blockDataEnsureCapacity(pUpdateRes, pBlock->info.rows);
  ASSERT(taosArrayGetSize(pBlock->pDataBlock) >= 3);
  SColumnInfoData* pStartTsCol = taosArrayGet(pBlock->pDataBlock, START_TS_COLUMN_INDEX);
  TSKEY* startData = (TSKEY*)pStartTsCol->pData;
  SColumnInfoData* pEndTsCol = taosArrayGet(pBlock->pDataBlock, END_TS_COLUMN_INDEX);
  TSKEY* endData = (TSKEY*)pEndTsCol->pData;
  SColumnInfoData* pGpCol = taosArrayGet(pBlock->pDataBlock, UID_COLUMN_INDEX);
  uint64_t* uidCol = (uint64_t*)pGpCol->pData;

  SColumnInfoData* pDestStartCol = taosArrayGet(pUpdateRes->pDataBlock, START_TS_COLUMN_INDEX);
  SColumnInfoData* pDestEndCol = taosArrayGet(pUpdateRes->pDataBlock, END_TS_COLUMN_INDEX);
  SColumnInfoData* pDestGpCol = taosArrayGet(pUpdateRes->pDataBlock, GROUPID_COLUMN_INDEX);
  int32_t dummy = 0;
  for (int32_t i = 0 ; i < pBlock->info.rows; i++) {
    uint64_t groupId = getGroupId(pOperator, uidCol[i]);
    //gap must be 0.
    SResultWindowInfo* pStartWin = getCurSessionWindow(pInfo->sessionSup.pStreamAggSup, startData[i], endData[i], groupId, 0, &dummy);
    if (!pStartWin) {
      // window has been closed.
      continue;
    }
    SResultWindowInfo* pEndWin = getCurSessionWindow(pInfo->sessionSup.pStreamAggSup, endData[i], endData[i], groupId, 0, &dummy);
    ASSERT(pEndWin);
    colDataAppend(pDestStartCol, i, (const char*)&pStartWin->win.skey, false);
    colDataAppend(pDestEndCol, i, (const char*)&pEndWin->win.ekey, false);
    colDataAppend(pDestGpCol, i, (const char*)&groupId, false);
    pUpdateRes->info.rows++;
  }
}
1069 1070
static void setUpdateData(SStreamBlockScanInfo* pInfo, SSDataBlock* pBlock, SSDataBlock* pUpdateBlock) {
  blockDataCleanup(pUpdateBlock);
5
54liuyao 已提交
1071
  int32_t size = taosArrayGetSize(pInfo->tsArray);
1072
  if (pInfo->tsArrayIndex < size) {
5
54liuyao 已提交
1073
    SColumnInfoData* pCol = (SColumnInfoData*)taosArrayGet(pUpdateBlock->pDataBlock, pInfo->primaryTsIndex);
5
54liuyao 已提交
1074
    ASSERT(pCol->info.type == TSDB_DATA_TYPE_TIMESTAMP);
1075
    blockDataEnsureCapacity(pUpdateBlock, size);
1076 1077

    int32_t rowId = *(int32_t*)taosArrayGet(pInfo->tsArray, pInfo->tsArrayIndex);
1078
    pInfo->groupId = getGroupId(pInfo->pSnapshotReadOp, pBlock->info.uid);
1079
    int32_t i = 0;
H
Hongze Cheng 已提交
1080
    for (; i < size; i++) {
1081
      rowId = *(int32_t*)taosArrayGet(pInfo->tsArray, i + pInfo->tsArrayIndex);
1082
      uint64_t id = getGroupId(pInfo->pSnapshotReadOp, pBlock->info.uid);
1083 1084
      if (pInfo->groupId != id) {
        break;
1085
      }
1086
      copyOneRow(pUpdateBlock, pBlock, rowId);
1087
    }
1088 1089 1090
    pUpdateBlock->info.rows = i;
    pInfo->tsArrayIndex += i;
    pUpdateBlock->info.groupId = pInfo->groupId;
5
54liuyao 已提交
1091
    pUpdateBlock->info.type = STREAM_CLEAR;
5
54liuyao 已提交
1092
    blockDataUpdateTsWindow(pUpdateBlock, 0);
1093 1094 1095 1096
  }
  // all rows have same group id
  ASSERT(pInfo->tsArrayIndex >= size);
  if (size > 0 && pInfo->tsArrayIndex == size) {
5
54liuyao 已提交
1097
    taosArrayClear(pInfo->tsArray);
5
54liuyao 已提交
1098
  }
1099 1100

  if (size == 0) {
5
54liuyao 已提交
1101
    generateIntervalTs(pInfo, pInfo->pDeleteDataRes, pInfo->pSnapshotReadOp, pUpdateBlock);
1102
  }
5
54liuyao 已提交
1103 1104
}

1105 1106
static void checkUpdateData(SStreamBlockScanInfo* pInfo, bool invertible, SSDataBlock* pBlock,
    bool out) {
1107 1108 1109 1110
  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++) {
1111
    if (updateInfoIsUpdated(pInfo->pUpdateInfo, pBlock->info.uid, ts[rowId]) && out) {
1112 1113 1114
      taosArrayPush(pInfo->tsArray, &rowId);
    }
  }
1115 1116 1117 1118 1119 1120 1121 1122 1123
}

static void setBlockGroupId(SOperatorInfo* pOperator, SSDataBlock* pBlock, int32_t uidColIndex) {
  ASSERT(taosArrayGetSize(pBlock->pDataBlock) >= 3);
  SColumnInfoData* pColDataInfo = taosArrayGet(pBlock->pDataBlock, uidColIndex);
  uint64_t*        uidCol = (uint64_t*)pColDataInfo->pData;
  ASSERT(pBlock->info.rows > 0);
  for (int32_t i = 0 ; i < pBlock->info.rows; i++) {
    uidCol[i] = getGroupId(pOperator, uidCol[i]);
1124 1125 1126
  }
}

1127
static SSDataBlock* doStreamBlockScan(SOperatorInfo* pOperator) {
H
Haojun Liao 已提交
1128 1129 1130 1131
  // NOTE: this operator does never check if current status is done or not
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
  SStreamBlockScanInfo* pInfo = pOperator->info;

1132
  pTaskInfo->code = pOperator->fpSet._openFn(pOperator);
1133
  if (pTaskInfo->code != TSDB_CODE_SUCCESS || pOperator->status == OP_EXEC_DONE) {
H
Haojun Liao 已提交
1134 1135 1136
    return NULL;
  }

5
54liuyao 已提交
1137
  size_t total = taosArrayGetSize(pInfo->pBlockLists);
1138
  // TODO: refactor
L
Liu Jicong 已提交
1139
  if (pInfo->blockType == STREAM_INPUT__DATA_BLOCK) {
H
Haojun Liao 已提交
1140
    if (pInfo->validBlockIndex >= total) {
L
Liu Jicong 已提交
1141
      /*doClearBufferedBlocks(pInfo);*/
1142
      pOperator->status = OP_EXEC_DONE;
H
Haojun Liao 已提交
1143 1144 1145
      return NULL;
    }

1146
    int32_t      current = pInfo->validBlockIndex++;
1147 1148
    SSDataBlock* pBlock = taosArrayGetP(pInfo->pBlockLists, current);
    blockDataUpdateTsWindow(pBlock, 0);
1149 1150
    switch (pBlock->info.type) {
    case STREAM_RETRIEVE:{
L
Liu Jicong 已提交
1151
      pInfo->blockType = STREAM_INPUT__DATA_SUBMIT;
5
54liuyao 已提交
1152 1153 1154
      pInfo->scanMode = STREAM_SCAN_FROM_DATAREADER_RETRIEVE;
      copyDataBlock(pInfo->pPullDataRes, pBlock);
      pInfo->pullDataResIndex = 0;
1155
      prepareDataScan(pInfo, pInfo->pPullDataRes, START_TS_COLUMN_INDEX, &pInfo->pullDataResIndex);
5
54liuyao 已提交
1156
      updateInfoAddCloseWindowSBF(pInfo->pUpdateInfo);
1157 1158 1159 1160 1161
    }
      break;
    case STREAM_DELETE_DATA: {
      pInfo->blockType = STREAM_INPUT__DATA_SUBMIT;
      pInfo->updateResIndex = 0;
5
54liuyao 已提交
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
      if (isIntervalWindow(pInfo)) {
        copyDataBlock(pInfo->pDeleteDataRes, pBlock);
        generateIntervalTs(pInfo, pInfo->pDeleteDataRes, pInfo->pSnapshotReadOp, pInfo->pUpdateRes);
        prepareDataScan(pInfo, pInfo->pUpdateRes, START_TS_COLUMN_INDEX, &pInfo->updateResIndex);
        pInfo->scanMode = STREAM_SCAN_FROM_DATAREADER;
      } else {
        generateScanRange(pInfo, pBlock, pInfo->pSnapshotReadOp, pInfo->pUpdateRes);
        prepareRangeScan(pInfo, pInfo->pUpdateRes, &pInfo->updateResIndex);
        pInfo->scanMode = STREAM_SCAN_FROM_DATAREADER_RANGE;
      }
1172 1173 1174 1175 1176 1177
      pInfo->pUpdateRes->info.type = STREAM_DELETE_DATA;
      return pInfo->pUpdateRes;
    }
      break;
    default:
      break;
5
54liuyao 已提交
1178
    }
1179
    return pBlock;
L
Liu Jicong 已提交
1180
  } else if (pInfo->blockType == STREAM_INPUT__DATA_SUBMIT) {
5
54liuyao 已提交
1181 1182 1183 1184 1185
    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) {
5
54liuyao 已提交
1186 1187 1188 1189
      if (isStateWindow(pInfo)) {
        pInfo->scanMode = STREAM_SCAN_FROM_READERHANDLE;
      } else {
        pInfo->scanMode = STREAM_SCAN_FROM_DATAREADER;
5
54liuyao 已提交
1190
        prepareDataScan(pInfo, pInfo->pUpdateRes, pInfo->primaryTsIndex, &pInfo->updateResIndex);
5
54liuyao 已提交
1191
      }
5
54liuyao 已提交
1192
      return pInfo->pUpdateRes;
5
54liuyao 已提交
1193 1194 1195
    } else if (pInfo->scanMode == STREAM_SCAN_FROM_DATAREADER_RETRIEVE) {
      SSDataBlock* pSDB = doDataScan(pInfo, pInfo->pPullDataRes, 0, &pInfo->pullDataResIndex);
      if (pSDB != NULL) {
1196
        checkUpdateData(pInfo, true, pSDB, false);
L
Liu Jicong 已提交
1197
        pSDB->info.type = STREAM_PULL_DATA;
5
54liuyao 已提交
1198 1199 1200
        return pSDB;
      }
      pInfo->scanMode = STREAM_SCAN_FROM_DATAREADER;
1201 1202 1203 1204 1205 1206
    } else if (pInfo->scanMode == STREAM_SCAN_FROM_DATAREADER) {
      SSDataBlock* pSDB = doDataScan(pInfo, pInfo->pUpdateRes, pInfo->primaryTsIndex, &pInfo->updateResIndex);
      if (pSDB) {
        pSDB->info.type = STREAM_NORMAL;
        checkUpdateData(pInfo, true, pSDB, false);
        return pSDB;
5
54liuyao 已提交
1207
      }
1208 1209 1210 1211
      setUpdateData(pInfo, pInfo->pRes, pInfo->pUpdateRes);
      if (pInfo->pUpdateRes->info.rows > 0) {
        prepareDataScan(pInfo, pInfo->pUpdateRes, pInfo->primaryTsIndex, &pInfo->updateResIndex);
        return pInfo->pUpdateRes;
5
54liuyao 已提交
1212
      }
1213
      pInfo->scanMode = STREAM_SCAN_FROM_READERHANDLE;
5
54liuyao 已提交
1214 1215 1216 1217 1218 1219 1220 1221
    } else if (pInfo->scanMode == STREAM_SCAN_FROM_DATAREADER_RANGE) {
      SSDataBlock* pSDB = doRangeScan(pInfo, pInfo->pUpdateRes, pInfo->primaryTsIndex, &pInfo->updateResIndex);
      if (pSDB) {
        pSDB->info.type = STREAM_NORMAL;
        checkUpdateData(pInfo, true, pSDB, false);
        return pSDB;
      }
      pInfo->scanMode = STREAM_SCAN_FROM_READERHANDLE;
1222 1223 1224 1225
    } else if (isStateWindow(pInfo)) {
      pInfo->scanMode = STREAM_SCAN_FROM_DATAREADER;
      pInfo->updateResIndex = pInfo->pUpdateRes->info.rows;
      if (prepareDataScan(pInfo, pInfo->pUpdateRes, pInfo->primaryTsIndex, &pInfo->updateResIndex)) {
5
54liuyao 已提交
1226
        blockDataCleanup(pInfo->pUpdateRes);
1227 1228 1229 1230
        // return empty data blcok
        return pInfo->pUpdateRes;
      }
      pInfo->scanMode = STREAM_SCAN_FROM_READERHANDLE;
5
54liuyao 已提交
1231
    }
5
54liuyao 已提交
1232

H
Haojun Liao 已提交
1233 1234 1235
    SDataBlockInfo* pBlockInfo = &pInfo->pRes->info;
    blockDataCleanup(pInfo->pRes);

1236
    while (tqNextDataBlock(pInfo->streamBlockReader)) {
1237
      SSDataBlock block = {0};
1238

1239
      // todo refactor
1240
      int32_t code = tqRetrieveDataBlock(&block, pInfo->streamBlockReader);
1241

1242 1243 1244
      uint64_t groupId = block.info.groupId;
      uint64_t uid = block.info.uid;
      int32_t  numOfRows = block.info.rows;
H
Haojun Liao 已提交
1245

1246 1247 1248
      if (code != TSDB_CODE_SUCCESS || numOfRows == 0) {
        pTaskInfo->code = code;
        return NULL;
H
Haojun Liao 已提交
1249 1250
      }

1251 1252
      pInfo->pRes->info.groupId = groupId;
      pInfo->pRes->info.rows = numOfRows;
1253
      pInfo->pRes->info.uid = uid;
5
54liuyao 已提交
1254
      pInfo->pRes->info.type = STREAM_NORMAL;
1255
      pInfo->pRes->info.capacity = numOfRows;
H
Haojun Liao 已提交
1256

1257 1258 1259 1260
      // 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;
1261 1262
      } else {
        pInfo->pRes->info.groupId = groupId;
1263 1264
      }

1265 1266 1267 1268 1269
      uint64_t* groupIdPre = taosHashGet(pOperator->pTaskInfo->tableqinfoList.map, &uid, sizeof(int64_t));
      if (groupIdPre) {
        pInfo->pRes->info.groupId = *groupIdPre;
      }

1270
      // todo extract method
1271
      for (int32_t i = 0; i < taosArrayGetSize(pInfo->pColMatchInfo); ++i) {
1272
        SColMatchInfo* pColMatchInfo = taosArrayGet(pInfo->pColMatchInfo, i);
H
Haojun Liao 已提交
1273 1274 1275 1276
        if (!pColMatchInfo->output) {
          continue;
        }

1277
        bool colExists = false;
1278 1279
        for (int32_t j = 0; j < blockDataGetNumOfCols(&block); ++j) {
          SColumnInfoData* pResCol = bdGetColumnInfoData(&block, j);
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
          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 已提交
1292 1293
      }

H
Haojun Liao 已提交
1294
      taosArrayDestroy(block.pDataBlock);
H
Haojun Liao 已提交
1295 1296
      if (pInfo->pRes->pDataBlock == NULL) {
        // TODO add log
5
54liuyao 已提交
1297
        updateInfoDestoryColseWinSBF(pInfo->pUpdateInfo);
1298
        pOperator->status = OP_EXEC_DONE;
H
Haojun Liao 已提交
1299 1300 1301
        pTaskInfo->code = terrno;
        return NULL;
      }
1302 1303 1304 1305 1306 1307

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

1308
      doFilter(pInfo->pCondition, pInfo->pRes);
1309
      blockDataUpdateTsWindow(pInfo->pRes, pInfo->primaryTsIndex);
5
54liuyao 已提交
1310 1311 1312
      if (pBlockInfo->rows > 0) {
        break;
      }
H
Haojun Liao 已提交
1313 1314 1315 1316
    }

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

5
54liuyao 已提交
1319
    if (pBlockInfo->rows == 0) {
5
54liuyao 已提交
1320
      updateInfoDestoryColseWinSBF(pInfo->pUpdateInfo);
1321
      pOperator->status = OP_EXEC_DONE;
5
54liuyao 已提交
1322
    } else if (pInfo->pUpdateInfo) {
1323
      pInfo->tsArrayIndex = 0;
1324 1325
      checkUpdateData(pInfo, true, pInfo->pRes, true);
      setUpdateData(pInfo, pInfo->pRes, pInfo->pUpdateRes);
5
54liuyao 已提交
1326
      if (pInfo->pUpdateRes->info.rows > 0) {
5
54liuyao 已提交
1327
        if (pInfo->pUpdateRes->info.type == STREAM_CLEAR) {
5
54liuyao 已提交
1328 1329
          pInfo->updateResIndex = 0;
          pInfo->scanMode = STREAM_SCAN_FROM_UPDATERES;
5
54liuyao 已提交
1330
        } else if (pInfo->pUpdateRes->info.type == STREAM_INVERT) {
5
54liuyao 已提交
1331
          pInfo->scanMode = STREAM_SCAN_FROM_RES;
5
54liuyao 已提交
1332
          return pInfo->pUpdateRes;
5
54liuyao 已提交
1333
        }
5
54liuyao 已提交
1334
      }
1335
    }
5
54liuyao 已提交
1336
    return (pBlockInfo->rows == 0) ? NULL : pInfo->pRes;
L
Liu Jicong 已提交
1337 1338 1339
  } else if (pInfo->blockType == STREAM_INPUT__DATA_SCAN) {
    // check reader last status
    // if not match, reset status
L
Liu Jicong 已提交
1340
    SSDataBlock* pResult = doTableScan(pInfo->pSnapshotReadOp);
L
Liu Jicong 已提交
1341 1342
    return pResult && pResult->info.rows > 0 ? pResult : NULL;

L
Liu Jicong 已提交
1343 1344 1345
  } else {
    ASSERT(0);
    return NULL;
H
Haojun Liao 已提交
1346 1347 1348
  }
}

1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
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;
}

1361 1362 1363
SOperatorInfo* createStreamScanOperatorInfo(SReadHandle* pHandle, STableScanPhysiNode* pTableScanNode,
                                            SExecTaskInfo* pTaskInfo, STimeWindowAggSupp* pTwSup, uint64_t queryId,
                                            uint64_t taskId) {
H
Haojun Liao 已提交
1364 1365
  SStreamBlockScanInfo* pInfo = taosMemoryCalloc(1, sizeof(SStreamBlockScanInfo));
  SOperatorInfo*        pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
1366

H
Haojun Liao 已提交
1367 1368
  if (pInfo == NULL || pOperator == NULL) {
    terrno = TSDB_CODE_QRY_OUT_OF_MEMORY;
1369
    goto _error;
H
Haojun Liao 已提交
1370 1371
  }

1372 1373 1374
  SScanPhysiNode* pScanPhyNode = &pTableScanNode->scan;

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

1376
  int32_t numOfCols = 0;
1377
  pInfo->pColMatchInfo = extractColMatchInfo(pScanPhyNode->pScanCols, pDescNode, &numOfCols, COL_MATCH_FROM_COL_ID);
1378 1379 1380

  int32_t numOfOutput = taosArrayGetSize(pInfo->pColMatchInfo);
  SArray* pColIds = taosArrayInit(numOfOutput, sizeof(int16_t));
1381
  for (int32_t i = 0; i < numOfOutput; ++i) {
1382 1383 1384
    SColMatchInfo* id = taosArrayGet(pInfo->pColMatchInfo, i);

    int16_t colId = id->colId;
1385
    taosArrayPush(pColIds, &colId);
1386
    if (id->colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
5
54liuyao 已提交
1387 1388
      pInfo->primaryTsIndex = id->targetSlotId;
    }
H
Haojun Liao 已提交
1389 1390 1391 1392
  }

  pInfo->pBlockLists = taosArrayInit(4, POINTER_BYTES);
  if (pInfo->pBlockLists == NULL) {
1393 1394
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _error;
H
Haojun Liao 已提交
1395 1396
  }

1397
  pInfo->tsArray = taosArrayInit(4, sizeof(int32_t));
5
54liuyao 已提交
1398
  if (pInfo->tsArray == NULL) {
1399
    goto _error;
5
54liuyao 已提交
1400 1401
  }

L
Liu Jicong 已提交
1402
  if (pHandle) {
H
Haojun Liao 已提交
1403
    SOperatorInfo*  pTableScanDummy = createTableScanOperatorInfo(pTableScanNode, pHandle, pTaskInfo);
L
Liu Jicong 已提交
1404
    STableScanInfo* pSTInfo = (STableScanInfo*)pTableScanDummy->info;
L
Liu Jicong 已提交
1405 1406 1407 1408

    SArray* tableList = taosArrayGetP(pTaskInfo->tableqinfoList.pGroupList, 0);
    if (pHandle->tqReader) {
      pSTInfo->scanMode = TABLE_SCAN__TABLE_ORDER;
H
Hongze Cheng 已提交
1409
      tsdbReaderOpen(pHandle->vnode, &pSTInfo->cond, tableList, &pSTInfo->dataReader, 0);
L
Liu Jicong 已提交
1410 1411
    }

L
Liu Jicong 已提交
1412 1413 1414 1415 1416
    if (pSTInfo->interval.interval > 0) {
      pInfo->pUpdateInfo = updateInfoInitP(&pSTInfo->interval, pTwSup->waterMark);
    } else {
      pInfo->pUpdateInfo = NULL;
    }
L
Liu Jicong 已提交
1417
    pInfo->pSnapshotReadOp = pTableScanDummy;
L
Liu Jicong 已提交
1418 1419
    pInfo->interval = pSTInfo->interval;

L
Liu Jicong 已提交
1420 1421 1422 1423 1424
    pInfo->readHandle = *pHandle;
    ASSERT(pHandle->reader);
    pInfo->streamBlockReader = pHandle->reader;
    pInfo->tableUid = pScanPhyNode->uid;

L
Liu Jicong 已提交
1425
    // set the extract column id to streamHandle
L
Liu Jicong 已提交
1426
    tqReadHandleSetColIdList((SStreamReader*)pHandle->reader, pColIds);
L
Liu Jicong 已提交
1427 1428 1429 1430 1431 1432 1433
    SArray* tableIdList = extractTableIdList(&pTaskInfo->tableqinfoList);
    int32_t code = tqReadHandleSetTbUidList(pHandle->reader, tableIdList);
    if (code != 0) {
      taosArrayDestroy(tableIdList);
      goto _error;
    }
    taosArrayDestroy(tableIdList);
5
54liuyao 已提交
1434 1435
  }

1436 1437 1438 1439 1440
  // create the pseduo columns info
  if (pTableScanNode->scan.pScanPseudoCols != NULL) {
    pInfo->pPseudoExpr = createExprInfo(pTableScanNode->scan.pScanPseudoCols, NULL, &pInfo->numOfPseudoExpr);
  }

1441
  pInfo->pRes = createResDataBlock(pDescNode);
X
Xiaoyu Wang 已提交
1442
  pInfo->pUpdateRes = createResDataBlock(pDescNode);
1443 1444 1445
  pInfo->pCondition = pScanPhyNode->node.pConditions;
  pInfo->scanMode = STREAM_SCAN_FROM_READERHANDLE;
  pInfo->sessionSup = (SessionWindowSupporter){.pStreamAggSup = NULL, .gap = -1};
1446
  pInfo->groupId = 0;
5
54liuyao 已提交
1447
  pInfo->pPullDataRes = createPullDataBlock();
1448
  pInfo->pStreamScanOp = pOperator;
1449 1450
  pInfo->deleteDataIndex = 0;
  pInfo->pDeleteDataRes = createPullDataBlock();
H
Hongze Cheng 已提交
1451

1452
  pOperator->name = "StreamBlockScanOperator";
L
Liu Jicong 已提交
1453
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN;
1454 1455 1456
  pOperator->blocking = false;
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
1457
  pOperator->exprSupp.numOfExprs = taosArrayGetSize(pInfo->pRes->pDataBlock);
1458
  pOperator->pTaskInfo = pTaskInfo;
H
Haojun Liao 已提交
1459

1460 1461
  pOperator->fpSet =
      createOperatorFpSet(operatorDummyOpenFn, doStreamBlockScan, NULL, NULL, operatorDummyCloseFn, NULL, NULL, NULL);
1462

H
Haojun Liao 已提交
1463
  return pOperator;
1464

L
Liu Jicong 已提交
1465
_error:
1466 1467 1468
  taosMemoryFreeClear(pInfo);
  taosMemoryFreeClear(pOperator);
  return NULL;
H
Haojun Liao 已提交
1469 1470 1471 1472 1473 1474 1475
}

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

1476
  const char* name = tNameGetTableName(&pInfo->name);
1477
  if (strncasecmp(name, TSDB_INS_TABLE_USER_TABLES, TSDB_TABLE_FNAME_LEN) == 0 || pInfo->pCur != NULL) {
H
Haojun Liao 已提交
1478
    metaCloseTbCursor(pInfo->pCur);
1479
    pInfo->pCur = NULL;
H
Haojun Liao 已提交
1480
  }
H
Haojun Liao 已提交
1481 1482

  taosArrayDestroy(pInfo->scanCols);
1483
  taosMemoryFreeClear(pInfo->pUser);
H
Haojun Liao 已提交
1484 1485
}

X
Xiaoyu Wang 已提交
1486
static int32_t getSysTableDbNameColId(const char* pTable) {
X
Xiaoyu Wang 已提交
1487 1488 1489
  // if (0 == strcmp(TSDB_INS_TABLE_USER_INDEXES, pTable)) {
  //   return 1;
  // }
X
Xiaoyu Wang 已提交
1490 1491 1492
  return TSDB_INS_USER_STABLES_DBNAME_COLID;
}

H
Haojun Liao 已提交
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
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;
X
Xiaoyu Wang 已提交
1514
      if (getSysTableDbNameColId(node->tableName) == node->colId) {
H
Haojun Liao 已提交
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
        *(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;
1531
      return DEAL_RES_END;  // stop walk
H
Haojun Liao 已提交
1532 1533 1534 1535 1536 1537 1538
    }
    default:
      break;
  }
  return DEAL_RES_CONTINUE;
}

1539
static void getDBNameFromCondition(SNode* pCondition, const char* dbName) {
H
Haojun Liao 已提交
1540 1541 1542
  if (NULL == pCondition) {
    return;
  }
L
Liu Jicong 已提交
1543
  nodesWalkExpr(pCondition, getDBNameFromConditionWalker, (char*)dbName);
H
Haojun Liao 已提交
1544 1545
}

D
dapan1121 已提交
1546
static int32_t loadSysTableCallback(void* param, SDataBuf* pMsg, int32_t code) {
H
Haojun Liao 已提交
1547 1548 1549 1550 1551 1552 1553
  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);
1554 1555 1556
    pRsp->useconds = htobe64(pRsp->useconds);
    pRsp->handle = htobe64(pRsp->handle);
    pRsp->compLen = htonl(pRsp->compLen);
H
Haojun Liao 已提交
1557 1558 1559 1560 1561
  } else {
    operator->pTaskInfo->code = code;
  }

  tsem_post(&pScanResInfo->ready);
wmmhello's avatar
wmmhello 已提交
1562
  return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
1563 1564 1565 1566 1567 1568 1569
}

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

1570 1571
  doFilter(pInfo->pCondition, pInfo->pRes);
#if 0
H
Haojun Liao 已提交
1572
  SFilterInfo* filter = NULL;
1573 1574

  int32_t code = filterInitFromNode(pInfo->pCondition, &filter, 0);
H
Haojun Liao 已提交
1575 1576 1577 1578 1579

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

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

1583
  SSDataBlock* px = createOneDataBlock(pInfo->pRes, false);
H
Haojun Liao 已提交
1584 1585 1586 1587 1588 1589 1590 1591
  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 已提交
1592
    if (keep) {
1593
      colDataAssign(pDest, pSrc, pInfo->pRes->info.rows, &px->info);
D
dapan1121 已提交
1594 1595 1596 1597 1598 1599 1600
      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;
        }
1601

1602 1603 1604 1605 1606 1607
        if (colDataIsNull_s(pSrc, j)) {
          colDataAppendNULL(pDest, numOfRow);
        } else {
          colDataAppend(pDest, numOfRow, colDataGetData(pSrc, j), false);
        }

D
dapan1121 已提交
1608
        numOfRow += 1;
H
Haojun Liao 已提交
1609
      }
D
dapan1121 已提交
1610 1611
    } else {
      numOfRow = 0;
H
Haojun Liao 已提交
1612 1613 1614 1615 1616
    }
  }

  px->info.rows = numOfRow;
  pInfo->pRes = px;
1617
#endif
H
Haojun Liao 已提交
1618 1619 1620 1621

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

1622
static SSDataBlock* buildSysTableMetaBlock() {
L
Liu Jicong 已提交
1623 1624
  size_t               size = 0;
  const SSysTableMeta* pMeta = NULL;
1625 1626 1627
  getInfosDbMeta(&pMeta, &size);

  int32_t index = 0;
L
Liu Jicong 已提交
1628 1629
  for (int32_t i = 0; i < size; ++i) {
    if (strcmp(pMeta[i].name, TSDB_INS_TABLE_USER_TABLES) == 0) {
1630 1631 1632 1633
      index = i;
      break;
    }
  }
1634

1635
  SSDataBlock* pBlock = createDataBlock();
L
Liu Jicong 已提交
1636
  for (int32_t i = 0; i < pMeta[index].colNum; ++i) {
L
Liu Jicong 已提交
1637 1638
    SColumnInfoData colInfoData =
        createColumnInfoData(pMeta[index].schema[i].type, pMeta[index].schema[i].bytes, i + 1);
1639
    blockDataAppendColInfo(pBlock, &colInfoData);
1640 1641
  }

1642 1643 1644
  return pBlock;
}

1645
static SSDataBlock* doSysTableScan(SOperatorInfo* pOperator) {
H
Haojun Liao 已提交
1646 1647 1648 1649 1650
  // 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
1651 1652
  const char* name = tNameGetTableName(&pInfo->name);
  if (strncasecmp(name, TSDB_INS_TABLE_USER_TABLES, TSDB_TABLE_FNAME_LEN) == 0) {
1653 1654 1655 1656
    if (pOperator->status == OP_EXEC_DONE) {
      return NULL;
    }

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

1661 1662
      doFilterResult(pInfo);
      pInfo->loadInfo.totalRows += pInfo->pRes->info.rows;
1663

1664
      doSetOperatorCompleted(pOperator);
1665 1666 1667 1668 1669
      return (pInfo->pRes->info.rows == 0) ? NULL : pInfo->pRes;
    } else {
      if (pInfo->pCur == NULL) {
        pInfo->pCur = metaOpenTbCursor(pInfo->readHandle.meta);
      }
1670

1671 1672
      blockDataCleanup(pInfo->pRes);
      int32_t numOfRows = 0;
1673

1674 1675 1676
      const char* db = NULL;
      int32_t     vgId = 0;
      vnodeGetInfo(pInfo->readHandle.vnode, &db, &vgId);
1677

1678 1679 1680
      SName sn = {0};
      char  dbname[TSDB_DB_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
      tNameFromString(&sn, db, T_NAME_ACCT | T_NAME_DB);
1681

1682 1683
      tNameGetDbName(&sn, varDataVal(dbname));
      varDataSetLen(dbname, strlen(varDataVal(dbname)));
1684

1685
      SSDataBlock* p = buildSysTableMetaBlock();
1686
      blockDataEnsureCapacity(p, pOperator->resultInfo.capacity);
1687

1688
      char n[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
1689 1690 1691

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

1694 1695 1696
        // table name
        SColumnInfoData* pColInfoData = taosArrayGet(p->pDataBlock, 0);
        colDataAppend(pColInfoData, numOfRows, n, false);
1697

1698 1699 1700
        // database name
        pColInfoData = taosArrayGet(p->pDataBlock, 1);
        colDataAppend(pColInfoData, numOfRows, dbname, false);
1701

1702 1703 1704
        // vgId
        pColInfoData = taosArrayGet(p->pDataBlock, 6);
        colDataAppend(pColInfoData, numOfRows, (char*)&vgId, false);
1705

1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
        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);
1719
          colDataAppend(pColInfoData, numOfRows, (char*)&mr.me.stbEntry.schemaRow.nCols, false);
1720 1721

          // super table name
wmmhello's avatar
wmmhello 已提交
1722
          STR_TO_VARSTR(n, mr.me.name);
1723
          pColInfoData = taosArrayGet(p->pDataBlock, 4);
wmmhello's avatar
wmmhello 已提交
1724
          colDataAppend(pColInfoData, numOfRows, n, false);
1725 1726
          metaReaderClear(&mr);

wmmhello's avatar
wmmhello 已提交
1727 1728
          // table comment
          pColInfoData = taosArrayGet(p->pDataBlock, 8);
L
Liu Jicong 已提交
1729
          if (pInfo->pCur->mr.me.ctbEntry.commentLen > 0) {
wmmhello's avatar
wmmhello 已提交
1730 1731 1732
            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 已提交
1733
          } else if (pInfo->pCur->mr.me.ctbEntry.commentLen == 0) {
wmmhello's avatar
wmmhello 已提交
1734 1735 1736
            char comment[VARSTR_HEADER_SIZE + VARSTR_HEADER_SIZE] = {0};
            STR_TO_VARSTR(comment, "");
            colDataAppend(pColInfoData, numOfRows, comment, false);
L
Liu Jicong 已提交
1737
          } else {
wmmhello's avatar
wmmhello 已提交
1738 1739 1740
            colDataAppendNULL(pColInfoData, numOfRows);
          }

1741 1742 1743 1744 1745 1746 1747 1748
          // 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 已提交
1749
          STR_TO_VARSTR(n, "CHILD_TABLE");
1750 1751 1752 1753 1754 1755 1756
        } 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);
1757
          colDataAppend(pColInfoData, numOfRows, (char*)&pInfo->pCur->mr.me.ntbEntry.schemaRow.nCols, false);
1758 1759 1760 1761 1762

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

wmmhello's avatar
wmmhello 已提交
1763 1764
          // table comment
          pColInfoData = taosArrayGet(p->pDataBlock, 8);
L
Liu Jicong 已提交
1765
          if (pInfo->pCur->mr.me.ntbEntry.commentLen > 0) {
wmmhello's avatar
wmmhello 已提交
1766 1767 1768
            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 已提交
1769
          } else if (pInfo->pCur->mr.me.ntbEntry.commentLen == 0) {
wmmhello's avatar
wmmhello 已提交
1770 1771 1772
            char comment[VARSTR_HEADER_SIZE + VARSTR_HEADER_SIZE] = {0};
            STR_TO_VARSTR(comment, "");
            colDataAppend(pColInfoData, numOfRows, comment, false);
L
Liu Jicong 已提交
1773
          } else {
wmmhello's avatar
wmmhello 已提交
1774 1775 1776
            colDataAppendNULL(pColInfoData, numOfRows);
          }

1777 1778 1779 1780 1781 1782 1783 1784
          // 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 已提交
1785
          STR_TO_VARSTR(n, "NORMAL_TABLE");
1786
        }
1787

1788
        pColInfoData = taosArrayGet(p->pDataBlock, 9);
wmmhello's avatar
wmmhello 已提交
1789
        colDataAppend(pColInfoData, numOfRows, n, false);
1790

1791
        if (++numOfRows >= pOperator->resultInfo.capacity) {
1792 1793
          break;
        }
H
Haojun Liao 已提交
1794 1795
      }

1796 1797 1798 1799 1800 1801 1802
      // 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);
      }

1803 1804
      p->info.rows = numOfRows;
      pInfo->pRes->info.rows = numOfRows;
H
Haojun Liao 已提交
1805

1806
      relocateColumnData(pInfo->pRes, pInfo->scanCols, p->pDataBlock, false);
1807
      doFilterResult(pInfo);
H
Haojun Liao 已提交
1808

1809 1810
      blockDataDestroy(p);

1811 1812 1813
      pInfo->loadInfo.totalRows += pInfo->pRes->info.rows;
      return (pInfo->pRes->info.rows == 0) ? NULL : pInfo->pRes;
    }
H
Haojun Liao 已提交
1814 1815 1816 1817 1818
  } else {  // load the meta from mnode of the given epset
    if (pOperator->status == OP_EXEC_DONE) {
      return NULL;
    }

1819 1820 1821
    while (1) {
      int64_t startTs = taosGetTimestampUs();
      strncpy(pInfo->req.tb, tNameGetTableName(&pInfo->name), tListLen(pInfo->req.tb));
1822
      strcpy(pInfo->req.user, pInfo->pUser);
H
Haojun Liao 已提交
1823

1824 1825 1826 1827 1828
      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 已提交
1829

1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840
      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 已提交
1841

L
Liu Jicong 已提交
1842 1843
      int32_t msgType = (strcasecmp(name, TSDB_INS_TABLE_DNODE_VARIABLES) == 0) ? TDMT_DND_SYSTABLE_RETRIEVE
                                                                                : TDMT_MND_SYSTABLE_RETRIEVE;
D
dapan1121 已提交
1844

1845 1846 1847
      pMsgSendInfo->param = pOperator;
      pMsgSendInfo->msgInfo.pData = buf1;
      pMsgSendInfo->msgInfo.len = contLen;
D
dapan1121 已提交
1848
      pMsgSendInfo->msgType = msgType;
1849
      pMsgSendInfo->fp = loadSysTableCallback;
D
dapan1121 已提交
1850
      pMsgSendInfo->requestId = pTaskInfo->id.queryId;
H
Haojun Liao 已提交
1851

1852
      int64_t transporterId = 0;
1853 1854
      int32_t code =
          asyncSendMsgToServer(pInfo->readHandle.pMsgCb->clientRpc, &pInfo->epSet, &transporterId, pMsgSendInfo);
1855
      tsem_wait(&pInfo->ready);
H
Haojun Liao 已提交
1856

1857 1858 1859 1860 1861
      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 已提交
1862

1863 1864
      SRetrieveMetaTableRsp* pRsp = pInfo->pRsp;
      pInfo->req.showId = pRsp->handle;
H
Haojun Liao 已提交
1865

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

1871
        if (pRsp->numOfRows == 0) {
H
Haojun Liao 已提交
1872
          taosMemoryFree(pRsp);
1873 1874 1875
          return NULL;
        }
      }
H
Haojun Liao 已提交
1876

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

1880 1881
      // todo log the filter info
      doFilterResult(pInfo);
H
Haojun Liao 已提交
1882
      taosMemoryFree(pRsp);
1883 1884
      if (pInfo->pRes->info.rows > 0) {
        return pInfo->pRes;
D
dapan1121 已提交
1885 1886
      } else if (pOperator->status == OP_EXEC_DONE) {
        return NULL;
1887
      }
1888
    }
H
Haojun Liao 已提交
1889 1890 1891
  }
}

1892
int32_t buildSysDbTableInfo(const SSysTableScanInfo* pInfo, int32_t capacity) {
1893
  SSDataBlock* p = buildSysTableMetaBlock();
1894
  blockDataEnsureCapacity(p, capacity);
1895

L
Liu Jicong 已提交
1896
  size_t               size = 0;
1897 1898 1899 1900 1901 1902 1903 1904 1905
  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);

  pInfo->pRes->info.rows = p->info.rows;
1906
  relocateColumnData(pInfo->pRes, pInfo->scanCols, p->pDataBlock, false);
1907 1908 1909
  blockDataDestroy(p);

  return pInfo->pRes->info.rows;
1910 1911
}

L
Liu Jicong 已提交
1912 1913 1914
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};
1915 1916
  int32_t numOfRows = p->info.rows;

L
Liu Jicong 已提交
1917
  for (int32_t i = 0; i < size; ++i) {
1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937
    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 已提交
1938
    for (int32_t j = 4; j <= 8; ++j) {
1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953
      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;
}

1954
SOperatorInfo* createSysTableScanOperatorInfo(void* readHandle, SSystemTableScanPhysiNode* pScanPhyNode,
1955
                                              const char* pUser, SExecTaskInfo* pTaskInfo) {
H
Haojun Liao 已提交
1956 1957 1958
  SSysTableScanInfo* pInfo = taosMemoryCalloc(1, sizeof(SSysTableScanInfo));
  SOperatorInfo*     pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
  if (pInfo == NULL || pOperator == NULL) {
1959
    goto _error;
H
Haojun Liao 已提交
1960 1961
  }

1962 1963 1964
  SScanPhysiNode* pScanNode = &pScanPhyNode->scan;

  SDataBlockDescNode* pDescNode = pScanNode->node.pOutputDataBlockDesc;
1965
  SSDataBlock*        pResBlock = createResDataBlock(pDescNode);
1966 1967

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

1970
  pInfo->accountId = pScanPhyNode->accountId;
1971
  pInfo->pUser = taosMemoryStrDup((void*)pUser);
1972
  pInfo->showRewrite = pScanPhyNode->showRewrite;
1973 1974 1975
  pInfo->pRes = pResBlock;
  pInfo->pCondition = pScanNode->node.pConditions;
  pInfo->scanCols = colList;
1976 1977

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

1979
  tNameAssign(&pInfo->name, &pScanNode->tableName);
1980
  const char* name = tNameGetTableName(&pInfo->name);
1981

1982
  if (strncasecmp(name, TSDB_INS_TABLE_USER_TABLES, TSDB_TABLE_FNAME_LEN) == 0) {
L
Liu Jicong 已提交
1983
    pInfo->readHandle = *(SReadHandle*)readHandle;
1984
    blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
H
Haojun Liao 已提交
1985 1986
  } else {
    tsem_init(&pInfo->ready, 0, 0);
1987
    pInfo->epSet = pScanPhyNode->mgmtEpSet;
1988
    pInfo->readHandle = *(SReadHandle*)readHandle;
H
Haojun Liao 已提交
1989 1990
  }

1991
  pOperator->name = "SysTableScanOperator";
H
Haojun Liao 已提交
1992
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN;
1993 1994 1995
  pOperator->blocking = false;
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
1996
  pOperator->exprSupp.numOfExprs = taosArrayGetSize(pResBlock->pDataBlock);
1997
  pOperator->pTaskInfo = pTaskInfo;
1998

L
Liu Jicong 已提交
1999 2000
  pOperator->fpSet =
      createOperatorFpSet(operatorDummyOpenFn, doSysTableScan, NULL, NULL, destroySysScanOperator, NULL, NULL, NULL);
H
Haojun Liao 已提交
2001 2002

  return pOperator;
2003

2004
_error:
2005 2006 2007 2008
  taosMemoryFreeClear(pInfo);
  taosMemoryFreeClear(pOperator);
  terrno = TSDB_CODE_QRY_OUT_OF_MEMORY;
  return NULL;
H
Haojun Liao 已提交
2009
}
H
Haojun Liao 已提交
2010

2011
static SSDataBlock* doTagScan(SOperatorInfo* pOperator) {
H
Haojun Liao 已提交
2012 2013 2014 2015
  if (pOperator->status == OP_EXEC_DONE) {
    return NULL;
  }

2016 2017 2018
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;

#if 0
H
Haojun Liao 已提交
2019 2020 2021 2022 2023 2024 2025 2026
  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);

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

2031
    SExprInfo* pExprInfo = &pOperator->exprSupp.pExprInfo[0];
H
Haojun Liao 已提交
2032 2033 2034 2035 2036 2037 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 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090
    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.
2091
#endif
H
Haojun Liao 已提交
2092

2093
  STagScanInfo* pInfo = pOperator->info;
2094
  SExprInfo*    pExprInfo = &pOperator->exprSupp.pExprInfo[0];
2095
  SSDataBlock*  pRes = pInfo->pRes;
H
Haojun Liao 已提交
2096

wmmhello's avatar
wmmhello 已提交
2097 2098
  int32_t size = taosArrayGetSize(pInfo->pTableList->pTableList);
  if (size == 0) {
H
Haojun Liao 已提交
2099 2100 2101 2102
    setTaskStatus(pTaskInfo, TASK_COMPLETED);
    return NULL;
  }

2103 2104 2105
  char        str[512] = {0};
  int32_t     count = 0;
  SMetaReader mr = {0};
2106
  metaReaderInit(&mr, pInfo->readHandle.meta, 0);
H
Haojun Liao 已提交
2107

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

2112
    for (int32_t j = 0; j < pOperator->exprSupp.numOfExprs; ++j) {
2113 2114 2115 2116 2117 2118
      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);
2119
      } else {  // it is a tag value
wmmhello's avatar
wmmhello 已提交
2120 2121 2122 2123
        STagVal val = {0};
        val.cid = pExprInfo[j].base.pParam[0].pCol->colId;
        const char* p = metaGetTableTagVal(&mr.me, pDst->info.type, &val);

2124 2125 2126 2127
        char* data = NULL;
        if (pDst->info.type != TSDB_DATA_TYPE_JSON && p != NULL) {
          data = tTagValToData((const STagVal*)p, false);
        } else {
wmmhello's avatar
wmmhello 已提交
2128 2129
          data = (char*)p;
        }
L
Liu Jicong 已提交
2130 2131
        colDataAppend(pDst, count, data,
                      (data == NULL) || (pDst->info.type == TSDB_DATA_TYPE_JSON && tTagIsJsonNull(data)));
2132

2133 2134
        if (pDst->info.type != TSDB_DATA_TYPE_JSON && p != NULL && IS_VAR_DATA_TYPE(((const STagVal*)p)->type) &&
            data != NULL) {
wmmhello's avatar
wmmhello 已提交
2135
          taosMemoryFree(data);
wmmhello's avatar
wmmhello 已提交
2136
        }
H
Haojun Liao 已提交
2137 2138 2139
      }
    }

2140
    count += 1;
wmmhello's avatar
wmmhello 已提交
2141
    if (++pInfo->curPos >= size) {
2142
      doSetOperatorCompleted(pOperator);
H
Haojun Liao 已提交
2143 2144 2145
    }
  }

2146 2147
  metaReaderClear(&mr);

2148
  // qDebug("QInfo:0x%"PRIx64" create tag values results completed, rows:%d", GET_TASKID(pRuntimeEnv), count);
H
Haojun Liao 已提交
2149
  if (pOperator->status == OP_EXEC_DONE) {
2150
    setTaskStatus(pTaskInfo, TASK_COMPLETED);
H
Haojun Liao 已提交
2151 2152 2153
  }

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

2156
  return (pRes->info.rows == 0) ? NULL : pInfo->pRes;
H
Haojun Liao 已提交
2157 2158 2159 2160 2161 2162 2163
}

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

2164 2165
SOperatorInfo* createTagScanOperatorInfo(SReadHandle* pReadHandle, STagScanPhysiNode* pPhyNode,
                                         STableListInfo* pTableListInfo, SExecTaskInfo* pTaskInfo) {
2166
  STagScanInfo*  pInfo = taosMemoryCalloc(1, sizeof(STagScanInfo));
H
Haojun Liao 已提交
2167 2168 2169 2170 2171
  SOperatorInfo* pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
  if (pInfo == NULL || pOperator == NULL) {
    goto _error;
  }

2172 2173
  SDataBlockDescNode* pDescNode = pPhyNode->node.pOutputDataBlockDesc;

2174
  int32_t    num = 0;
2175 2176
  int32_t    numOfExprs = 0;
  SExprInfo* pExprInfo = createExprInfo(pPhyNode->pScanPseudoCols, NULL, &numOfExprs);
L
Liu Jicong 已提交
2177
  SArray*    colList = extractColMatchInfo(pPhyNode->pScanPseudoCols, pDescNode, &num, COL_MATCH_FROM_COL_ID);
2178

2179 2180 2181 2182
  int32_t code = initExprSupp(&pOperator->exprSupp, pExprInfo, numOfExprs);
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
2183

2184 2185 2186 2187 2188
  pInfo->pTableList = pTableListInfo;
  pInfo->pColMatchInfo = colList;
  pInfo->pRes = createResDataBlock(pDescNode);
  pInfo->readHandle = *pReadHandle;
  pInfo->curPos = 0;
2189

2190
  pOperator->name = "TagScanOperator";
2191
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN;
2192

2193 2194 2195 2196
  pOperator->blocking = false;
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
  pOperator->pTaskInfo = pTaskInfo;
2197

2198 2199 2200
  initResultSizeInfo(pOperator, 4096);
  blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);

2201 2202
  pOperator->fpSet =
      createOperatorFpSet(operatorDummyOpenFn, doTagScan, NULL, NULL, destroyTagScanOperatorInfo, NULL, NULL, NULL);
H
Haojun Liao 已提交
2203 2204

  return pOperator;
2205

2206
_error:
H
Haojun Liao 已提交
2207 2208 2209 2210 2211
  taosMemoryFree(pInfo);
  taosMemoryFree(pOperator);
  terrno = TSDB_CODE_OUT_OF_MEMORY;
  return NULL;
}
2212 2213

typedef struct STableMergeScanInfo {
2214
  STableListInfo* tableListInfo;
S
slzhou 已提交
2215 2216 2217 2218
  int32_t         tableStartIndex;
  int32_t         tableEndIndex;
  bool            hasGroupId;
  uint64_t        groupId;
2219

2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231
  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

2232 2233 2234
  SArray*  sortSourceParams;
  uint64_t queryId;
  uint64_t taskId;
2235 2236 2237 2238 2239 2240 2241 2242 2243

  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;
2244
  int32_t*        rowEntryInfoOffset;
2245 2246 2247 2248 2249 2250 2251 2252
  SExprInfo*      pExpr;
  SSDataBlock*    pResBlock;
  SArray*         pColMatchInfo;
  int32_t         numOfOutput;

  SExprInfo*      pPseudoExpr;
  int32_t         numOfPseudoExpr;
  SqlFunctionCtx* pPseudoCtx;
2253
  //  int32_t*        rowEntryInfoOffset;
2254 2255 2256 2257 2258 2259 2260 2261 2262 2263

  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;

2264
int32_t createScanTableListInfo(STableScanPhysiNode* pTableScanNode, SReadHandle* pHandle,
H
Haojun Liao 已提交
2265
                                STableListInfo* pTableListInfo, uint64_t queryId, uint64_t taskId) {
2266
  int32_t code = getTableList(pHandle->meta, pHandle->vnode, &pTableScanNode->scan, pTableListInfo);
2267
  if (code != TSDB_CODE_SUCCESS) {
2268
    return code;
2269 2270 2271 2272
  }

  if (taosArrayGetSize(pTableListInfo->pTableList) == 0) {
    qDebug("no table qualified for query, TID:0x%" PRIx64 ", QID:0x%" PRIx64, taskId, queryId);
2273
    return TSDB_CODE_SUCCESS;
2274
  }
wmmhello's avatar
wmmhello 已提交
2275
  pTableListInfo->needSortTableByGroupId = pTableScanNode->groupSort;
2276
  code = generateGroupIdMap(pTableListInfo, pHandle, pTableScanNode->pGroupTags);
2277
  if (code != TSDB_CODE_SUCCESS) {
wmmhello's avatar
wmmhello 已提交
2278
    return code;
2279
  }
2280

2281 2282 2283
  return TSDB_CODE_SUCCESS;
}

S
slzhou 已提交
2284
int32_t createMultipleDataReaders(SQueryTableDataCond* pQueryCond, SReadHandle* pHandle, STableListInfo* pTableListInfo,
H
Haojun Liao 已提交
2285
                                  int32_t tableStartIdx, int32_t tableEndIdx, SArray* arrayReader, const char* idstr) {
S
slzhou 已提交
2286 2287 2288 2289
  for (int32_t i = tableStartIdx; i <= tableEndIdx; ++i) {
    SArray* subTableList = taosArrayInit(1, sizeof(STableKeyInfo));
    taosArrayPush(subTableList, taosArrayGet(pTableListInfo->pTableList, i));

H
Haojun Liao 已提交
2290 2291
    STsdbReader* pReader = NULL;
    tsdbReaderOpen(pHandle->vnode, pQueryCond, subTableList, &pReader, idstr);
2292
    taosArrayPush(arrayReader, &pReader);
2293

S
slzhou 已提交
2294
    taosArrayDestroy(subTableList);
2295 2296
  }

S
slzhou 已提交
2297
  return TSDB_CODE_SUCCESS;
2298 2299
}

H
Haojun Liao 已提交
2300
// todo refactor
2301 2302
static int32_t loadDataBlockFromOneTable(SOperatorInfo* pOperator, STableMergeScanInfo* pTableScanInfo,
                                         int32_t readerIdx, SSDataBlock* pBlock, uint32_t* status) {
H
Hongze Cheng 已提交
2303
  SExecTaskInfo*       pTaskInfo = pOperator->pTaskInfo;
S
shenglian zhou 已提交
2304
  STableMergeScanInfo* pInfo = pOperator->info;
2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330

  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
2331
    for (int32_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); ++i) {
2332 2333 2334 2335 2336 2337 2338 2339 2340 2341
      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;
H
Hongze Cheng 已提交
2342
    STsdbReader*     reader = taosArrayGetP(pTableScanInfo->dataReaders, readerIdx);
2343 2344 2345
    tsdbRetrieveDataBlockStatisInfo(reader, &pColAgg, &allColumnsHaveAgg);

    if (allColumnsHaveAgg == true) {
2346
      int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382

      // 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;

H
Hongze Cheng 已提交
2383
  STsdbReader* reader = taosArrayGetP(pTableScanInfo->dataReaders, readerIdx);
2384 2385 2386 2387 2388
  SArray*      pCols = tsdbRetrieveDataBlock(reader, NULL);
  if (pCols == NULL) {
    return terrno;
  }

2389
  relocateColumnData(pBlock, pTableScanInfo->pColMatchInfo, pCols, true);
2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414

  // 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;
2415
  SSDataBlock*   inputBlock;
2416 2417 2418 2419 2420 2421
} STableMergeScanSortSourceParam;

static SSDataBlock* getTableDataBlock(void* param) {
  STableMergeScanSortSourceParam* source = param;
  SOperatorInfo*                  pOperator = source->pOperator;
  int32_t                         readerIdx = source->readerIdx;
2422
  SSDataBlock*                    pBlock = source->inputBlock;
2423 2424 2425 2426
  STableMergeScanInfo*            pTableScanInfo = pOperator->info;

  int64_t st = taosGetTimestampUs();

2427 2428
  blockDataCleanup(pBlock);

H
Hongze Cheng 已提交
2429
  STsdbReader* reader = taosArrayGetP(pTableScanInfo->dataReaders, readerIdx);
2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440
  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;
    }

2441 2442 2443 2444 2445 2446 2447
    blockDataCleanup(pBlock);
    SDataBlockInfo binfo = pBlock->info;
    tsdbRetrieveDataBlockInfo(reader, &binfo);

    binfo.capacity = binfo.rows;
    blockDataEnsureCapacity(pBlock, binfo.capacity);
    pBlock->info = binfo;
2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485

    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;
}

2486
int32_t startGroupTableMergeScan(SOperatorInfo* pOperator) {
2487 2488 2489
  STableMergeScanInfo* pInfo = pOperator->info;
  SExecTaskInfo*       pTaskInfo = pOperator->pTaskInfo;

S
slzhou 已提交
2490 2491 2492 2493 2494 2495 2496 2497 2498 2499
  {
    size_t  tableListSize = taosArrayGetSize(pInfo->tableListInfo->pTableList);
    int32_t i = pInfo->tableStartIndex + 1;
    for (; i < tableListSize; ++i) {
      STableKeyInfo* tableKeyInfo = taosArrayGet(pInfo->tableListInfo->pTableList, i);
      if (tableKeyInfo->groupId != pInfo->groupId) {
        break;
      }
    }
    pInfo->tableEndIndex = i - 1;
2500 2501
  }

S
slzhou 已提交
2502 2503
  int32_t tableStartIdx = pInfo->tableStartIndex;
  int32_t tableEndIdx = pInfo->tableEndIndex;
2504

S
slzhou 已提交
2505 2506
  STableListInfo* tableListInfo = pInfo->tableListInfo;
  createMultipleDataReaders(&pInfo->cond, &pInfo->readHandle, tableListInfo, tableStartIdx, tableEndIdx,
2507
                            pInfo->dataReaders, GET_TASKID(pTaskInfo));
2508

2509 2510
  // todo the total available buffer should be determined by total capacity of buffer of this task.
  // the additional one is reserved for merge result
S
slzhou 已提交
2511
  pInfo->sortBufSize = pInfo->bufPageSize * (tableEndIdx - tableStartIdx + 1 + 1);
2512
  int32_t numOfBufPage = pInfo->sortBufSize / pInfo->bufPageSize;
L
Liu Jicong 已提交
2513 2514
  pInfo->pSortHandle = tsortCreateSortHandle(pInfo->pSortInfo, SORT_MULTISOURCE_MERGE, pInfo->bufPageSize, numOfBufPage,
                                             pInfo->pSortInputBlock, pTaskInfo->id.str);
2515 2516 2517 2518

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

  size_t numReaders = taosArrayGetSize(pInfo->dataReaders);
2519 2520 2521 2522 2523 2524 2525 2526
  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);
  }

2527 2528
  for (int32_t i = 0; i < numReaders; ++i) {
    SSortSource*                    ps = taosMemoryCalloc(1, sizeof(SSortSource));
2529
    STableMergeScanSortSourceParam* param = taosArrayGet(pInfo->sortSourceParams, i);
2530 2531 2532 2533 2534 2535 2536 2537 2538 2539
    ps->param = param;
    tsortAddSource(pInfo->pSortHandle, ps);
  }

  int32_t code = tsortOpen(pInfo->pSortHandle);

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

2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550
  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) {
H
Haojun Liao 已提交
2551 2552
    STsdbReader* reader = taosArrayGetP(pInfo->dataReaders, i);
    tsdbReaderClose(reader);
2553
  }
2554

2555 2556
  taosArrayDestroy(pInfo->dataReaders);
  pInfo->dataReaders = NULL;
2557 2558 2559
  return TSDB_CODE_SUCCESS;
}

2560
SSDataBlock* getSortedTableMergeScanBlockData(SSortHandle* pHandle, int32_t capacity, SOperatorInfo* pOperator) {
2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571
  STableMergeScanInfo* pInfo = pOperator->info;
  SExecTaskInfo*       pTaskInfo = pOperator->pTaskInfo;

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

  blockDataEnsureCapacity(p, capacity);

  while (1) {
2572
    STupleHandle* pTupleHandle = tsortNextTuple(pHandle);
2573 2574 2575 2576
    if (pTupleHandle == NULL) {
      break;
    }

2577
    appendOneRowToDataBlock(p, pTupleHandle);
2578 2579 2580 2581 2582
    if (p->info.rows >= capacity) {
      break;
    }
  }

2583 2584
  qDebug("%s get sorted row blocks, rows:%d", GET_TASKID(pTaskInfo), p->info.rows);
  return (p->info.rows > 0) ? p : NULL;
2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598
}

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);
  }
S
slzhou 已提交
2599 2600 2601
  size_t tableListSize = taosArrayGetSize(pInfo->tableListInfo->pTableList);
  if (!pInfo->hasGroupId) {
    pInfo->hasGroupId = true;
2602

S
slzhou 已提交
2603
    if (tableListSize == 0) {
2604 2605 2606
      doSetOperatorCompleted(pOperator);
      return NULL;
    }
S
slzhou 已提交
2607 2608
    pInfo->tableStartIndex = 0;
    pInfo->groupId = ((STableKeyInfo*)taosArrayGet(pInfo->tableListInfo->pTableList, pInfo->tableStartIndex))->groupId;
2609 2610
    startGroupTableMergeScan(pOperator);
  }
S
slzhou 已提交
2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628
  SSDataBlock* pBlock = NULL;
  while (pInfo->tableStartIndex < tableListSize) {
    pBlock = getSortedTableMergeScanBlockData(pInfo->pSortHandle, pOperator->resultInfo.capacity, pOperator);
    if (pBlock != NULL) {
      pBlock->info.groupId = pInfo->groupId;
      pOperator->resultInfo.totalRows += pBlock->info.rows;
      return pBlock;
    } else {
      stopGroupTableMergeScan(pOperator);
      if (pInfo->tableEndIndex >= tableListSize - 1) {
        doSetOperatorCompleted(pOperator);
        break;
      }
      pInfo->tableStartIndex = pInfo->tableEndIndex + 1;
      pInfo->groupId =
          ((STableKeyInfo*)taosArrayGet(pInfo->tableListInfo->pTableList, pInfo->tableStartIndex))->groupId;
      startGroupTableMergeScan(pOperator);
    }
2629
  }
wmmhello's avatar
wmmhello 已提交
2630

2631 2632 2633 2634 2635
  return pBlock;
}

void destroyTableMergeScanOperatorInfo(void* param, int32_t numOfOutput) {
  STableMergeScanInfo* pTableScanInfo = (STableMergeScanInfo*)param;
2636
  cleanupQueryTableDataCond(&pTableScanInfo->cond);
2637 2638

  for (int32_t i = 0; i < taosArrayGetSize(pTableScanInfo->dataReaders); ++i) {
H
Hongze Cheng 已提交
2639
    STsdbReader* reader = taosArrayGetP(pTableScanInfo->dataReaders, i);
H
refact  
Hongze Cheng 已提交
2640
    tsdbReaderClose(reader);
2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653
  }
  taosArrayDestroy(pTableScanInfo->dataReaders);

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

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

  taosArrayDestroy(pTableScanInfo->pSortInfo);
}

2654 2655
typedef struct STableMergeScanExecInfo {
  SFileBlockLoadRecorder blockRecorder;
H
Hongze Cheng 已提交
2656
  SSortExecInfo          sortExecInfo;
2657 2658
} STableMergeScanExecInfo;

2659 2660
int32_t getTableMergeScanExplainExecInfo(SOperatorInfo* pOptr, void** pOptrExplain, uint32_t* len) {
  ASSERT(pOptr != NULL);
2661 2662
  // TODO: merge these two info into one struct
  STableMergeScanExecInfo* execInfo = taosMemoryCalloc(1, sizeof(STableMergeScanExecInfo));
H
Hongze Cheng 已提交
2663
  STableMergeScanInfo*     pInfo = pOptr->info;
2664 2665 2666 2667 2668
  execInfo->blockRecorder = pInfo->readRecorder;
  execInfo->sortExecInfo = tsortGetSortExecInfo(pInfo->pSortHandle);

  *pOptrExplain = execInfo;
  *len = sizeof(STableMergeScanExecInfo);
H
Hongze Cheng 已提交
2669

2670 2671 2672
  return TSDB_CODE_SUCCESS;
}

S
slzhou 已提交
2673 2674 2675 2676 2677 2678
int32_t compareTableKeyInfoByGid(const void* p1, const void* p2) {
  const STableKeyInfo* info1 = p1;
  const STableKeyInfo* info2 = p2;
  return info1->groupId - info2->groupId;
}

2679 2680 2681
SOperatorInfo* createTableMergeScanOperatorInfo(STableScanPhysiNode* pTableScanNode, STableListInfo* pTableListInfo,
                                                SReadHandle* readHandle, SExecTaskInfo* pTaskInfo, uint64_t queryId,
                                                uint64_t taskId) {
2682 2683 2684 2685 2686
  STableMergeScanInfo* pInfo = taosMemoryCalloc(1, sizeof(STableMergeScanInfo));
  SOperatorInfo*       pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
  if (pInfo == NULL || pOperator == NULL) {
    goto _error;
  }
2687
  if (pTableScanNode->pGroupTags) {
S
slzhou 已提交
2688 2689
    taosArraySort(pTableListInfo->pTableList, compareTableKeyInfoByGid);
  }
2690 2691 2692 2693

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

  int32_t numOfCols = 0;
L
Liu Jicong 已提交
2694
  SArray* pColList = extractColMatchInfo(pTableScanNode->scan.pScanCols, pDescNode, &numOfCols, COL_MATCH_FROM_COL_ID);
2695 2696 2697 2698 2699 2700 2701 2702

  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);
2703
    pInfo->pPseudoCtx = createSqlFunctionCtx(pInfo->pPseudoExpr, pInfo->numOfPseudoExpr, &pInfo->rowEntryInfoOffset);
2704 2705 2706 2707
  }

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

L
Liu Jicong 已提交
2708 2709
  pInfo->readHandle = *readHandle;
  pInfo->interval = extractIntervalInfo(pTableScanNode);
2710
  pInfo->sample.sampleRatio = pTableScanNode->ratio;
L
Liu Jicong 已提交
2711 2712 2713
  pInfo->sample.seed = taosGetTimestampSec();
  pInfo->dataBlockLoadFlag = pTableScanNode->dataRequired;
  pInfo->pFilterNode = pTableScanNode->scan.node.pConditions;
2714
  pInfo->tableListInfo = pTableListInfo;
L
Liu Jicong 已提交
2715 2716
  pInfo->scanFlag = MAIN_SCAN;
  pInfo->pColMatchInfo = pColList;
2717 2718

  pInfo->pResBlock = createResDataBlock(pDescNode);
2719 2720 2721
  pInfo->dataReaders = taosArrayInit(64, POINTER_BYTES);
  pInfo->queryId = queryId;
  pInfo->taskId = taskId;
2722

2723
  pInfo->sortSourceParams = taosArrayInit(64, sizeof(STableMergeScanSortSourceParam));
2724

2725 2726
  pInfo->pSortInfo = generateSortByTsInfo(pInfo->cond.order);
  pInfo->pSortInputBlock = createOneDataBlock(pInfo->pResBlock, false);
2727

2728
  int32_t rowSize = pInfo->pResBlock->info.rowSize;
L
Liu Jicong 已提交
2729
  pInfo->bufPageSize = getProperSortPageSize(rowSize);
2730

L
Liu Jicong 已提交
2731
  pOperator->name = "TableMergeScanOperator";
2732
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN;
L
Liu Jicong 已提交
2733 2734 2735
  pOperator->blocking = false;
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
L
Liu Jicong 已提交
2736
  pOperator->exprSupp.numOfExprs = numOfCols;
L
Liu Jicong 已提交
2737
  pOperator->pTaskInfo = pTaskInfo;
2738 2739 2740
  initResultSizeInfo(pOperator, 1024);

  pOperator->fpSet =
2741 2742
      createOperatorFpSet(operatorDummyOpenFn, doTableMergeScan, NULL, NULL, destroyTableMergeScanOperatorInfo, NULL,
                          NULL, getTableMergeScanExplainExecInfo);
2743 2744 2745 2746 2747 2748 2749 2750 2751
  pOperator->cost.openCost = 0;
  return pOperator;

_error:
  pTaskInfo->code = TSDB_CODE_OUT_OF_MEMORY;
  taosMemoryFree(pInfo);
  taosMemoryFree(pOperator);
  return NULL;
}
2752 2753 2754 2755 2756 2757 2758

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

  SLastrowScanInfo* pInfo = pOperator->info;
2759
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
2760

H
Haojun Liao 已提交
2761
  int32_t size = taosArrayGetSize(pInfo->pTableList);
2762 2763 2764 2765 2766 2767
  if (size == 0) {
    setTaskStatus(pTaskInfo, TASK_COMPLETED);
    return NULL;
  }

  // check if it is a group by tbname
H
Haojun Liao 已提交
2768
  if (size == taosArrayGetSize(pInfo->pTableList)) {
2769 2770
    blockDataCleanup(pInfo->pRes);
    tsdbRetrieveLastRow(pInfo->pLastrowReader, pInfo->pRes, pInfo->pSlotIds);
2771
    return (pInfo->pRes->info.rows == 0) ? NULL : pInfo->pRes;
2772
  } else {
2773
    // todo fetch the result for each group
2774 2775
  }

2776
  return pInfo->pRes->info.rows == 0 ? NULL : pInfo->pRes;
2777 2778 2779
}

static void destroyLastrowScanOperator(void* param, int32_t numOfOutput) {
2780
  SLastrowScanInfo* pInfo = (SLastrowScanInfo*)param;
2781
  blockDataDestroy(pInfo->pRes);
2782
  tsdbLastrowReaderClose(pInfo->pLastrowReader);
2783 2784
}

2785 2786
SOperatorInfo* createLastrowScanOperator(SLastRowScanPhysiNode* pScanNode, SReadHandle* readHandle, SArray* pTableList,
                                         SExecTaskInfo* pTaskInfo) {
2787 2788 2789 2790 2791 2792 2793 2794
  SLastrowScanInfo* pInfo = taosMemoryCalloc(1, sizeof(SLastrowScanInfo));
  SOperatorInfo*    pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
  if (pInfo == NULL || pOperator == NULL) {
    goto _error;
  }

  pInfo->pTableList = pTableList;
  pInfo->readHandle = *readHandle;
2795 2796 2797
  pInfo->pRes = createResDataBlock(pScanNode->node.pOutputDataBlockDesc);

  int32_t numOfCols = 0;
2798 2799
  pInfo->pColMatchInfo = extractColMatchInfo(pScanNode->pScanCols, pScanNode->node.pOutputDataBlockDesc, &numOfCols,
                                             COL_MATCH_FROM_COL_ID);
2800
  int32_t* pCols = taosMemoryMalloc(numOfCols * sizeof(int32_t));
2801
  for (int32_t i = 0; i < numOfCols; ++i) {
2802 2803 2804 2805 2806
    SColMatchInfo* pColMatch = taosArrayGet(pInfo->pColMatchInfo, i);
    pCols[i] = pColMatch->colId;
  }

  pInfo->pSlotIds = taosMemoryMalloc(numOfCols * sizeof(pInfo->pSlotIds[0]));
2807
  for (int32_t i = 0; i < numOfCols; ++i) {
2808
    SColMatchInfo* pColMatch = taosArrayGet(pInfo->pColMatchInfo, i);
2809 2810 2811
    for (int32_t j = 0; j < pTaskInfo->schemaVer.sw->nCols; ++j) {
      if (pColMatch->colId == pTaskInfo->schemaVer.sw->pSchema[j].colId &&
          pColMatch->colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822
        pInfo->pSlotIds[pColMatch->targetSlotId] = -1;
        break;
      }

      if (pColMatch->colId == pTaskInfo->schemaVer.sw->pSchema[j].colId) {
        pInfo->pSlotIds[pColMatch->targetSlotId] = j;
        break;
      }
    }
  }

2823 2824
  tsdbLastRowReaderOpen(readHandle->vnode, LASTROW_RETRIEVE_TYPE_ALL, pTableList, pCols, numOfCols,
                        &pInfo->pLastrowReader);
2825
  taosMemoryFree(pCols);
2826

2827
  pOperator->name = "LastrowScanOperator";
H
Haojun Liao 已提交
2828
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_LAST_ROW_SCAN;
2829 2830 2831 2832
  pOperator->blocking = false;
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
  pOperator->pTaskInfo = pTaskInfo;
2833
  pOperator->exprSupp.numOfExprs = taosArrayGetSize(pInfo->pRes->pDataBlock);
2834 2835

  initResultSizeInfo(pOperator, 1024);
2836
  blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
2837 2838

  pOperator->fpSet =
2839
      createOperatorFpSet(operatorDummyOpenFn, doScanLastrow, NULL, NULL, destroyLastrowScanOperator, NULL, NULL, NULL);
2840 2841 2842 2843 2844 2845 2846 2847 2848
  pOperator->cost.openCost = 0;
  return pOperator;

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