scanoperator.c 57.3 KB
Newer Older
H
Haojun Liao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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/>.
 */

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

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

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

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

5
54liuyao 已提交
39 40 41 42 43
typedef struct SWindowPosition {
  int32_t pageId;
  int32_t rowId;
} SWindowPosition;

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

48
static void switchCtxOrder(SqlFunctionCtx* pCtx, int32_t numOfOutput) {
H
Haojun Liao 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  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
}

77 78 79 80 81 82 83 84 85
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;
86
  // convert key to second
87 88 89 90 91 92 93
  key = convertTimePrecision(key, pInterval->precision, TSDB_TIME_PRECISION_MILLI) / 1000;

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

  struct tm tm;
94
  time_t    t = (time_t)key;
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  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;
  tw->skey = convertTimePrecision((int64_t)taosMktime(&tm) * 1000L, TSDB_TIME_PRECISION_MILLI, pInterval->precision);

  mon = (int)(mon + interval);
  tm.tm_year = mon / 12;
  tm.tm_mon = mon % 12;
  tw->ekey = convertTimePrecision((int64_t)taosMktime(&tm) * 1000L, TSDB_TIME_PRECISION_MILLI, pInterval->precision);

  tw->ekey -= 1;
}

110
static bool overlapWithTimeWindow(SInterval* pInterval, SDataBlockInfo* pBlockInfo, int32_t order) {
111 112 113 114 115 116 117
  STimeWindow w = {0};

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

118
  if (order == TSDB_ORDER_ASC) {
119
    getAlignQueryTimeWindow(pInterval, pInterval->precision, pBlockInfo->window.skey, &w);
120 121 122 123 124 125
    assert(w.ekey >= pBlockInfo->window.skey);

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

126 127
    while (1) {
      getNextTimeWindow(pInterval, &w, order);
128 129 130 131 132 133 134 135 136 137
      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 {
138 139 140 141 142 143 144 145 146 147 148 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;
    }

    while(1) {
      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;
      }
    }
156 157 158 159 160
  }

  return false;
}

161 162
static void addTagPseudoColumnData(STableScanInfo* pTableScanInfo, SSDataBlock* pBlock);

L
Liu Jicong 已提交
163 164
static int32_t loadDataBlock(SOperatorInfo* pOperator, STableScanInfo* pTableScanInfo, SSDataBlock* pBlock,
                             uint32_t* status) {
165
  SExecTaskInfo*  pTaskInfo = pOperator->pTaskInfo;
166 167
  STableScanInfo* pInfo = pOperator->info;

168
  SFileBlockLoadRecorder* pCost = &pTableScanInfo->readRecorder;
H
Haojun Liao 已提交
169 170

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

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

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

  if (*status == FUNC_DATA_REQUIRED_FILTEROUT) {
183 184
    qDebug("%s data block filter out, brange:%" PRId64 "-%" PRId64 ", rows:%d", GET_TASKID(pTaskInfo),
           pBlockInfo->window.skey, pBlockInfo->window.ekey, pBlockInfo->rows);
185 186 187
    pCost->filterOutBlocks += 1;
    return TSDB_CODE_SUCCESS;
  } else if (*status == FUNC_DATA_REQUIRED_NOT_LOAD) {
188 189
    qDebug("%s data block skipped, brange:%" PRId64 "-%" PRId64 ", rows:%d", GET_TASKID(pTaskInfo),
           pBlockInfo->window.skey, pBlockInfo->window.ekey, pBlockInfo->rows);
190
    pCost->skipBlocks += 1;
191 192 193 194 195 196 197

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

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

L
Liu Jicong 已提交
202
    bool             allColumnsHaveAgg = true;
203
    SColumnDataAgg** pColAgg = NULL;
204
    tsdbRetrieveDataBlockStatisInfo(pTableScanInfo->dataReader, &pColAgg, &allColumnsHaveAgg);
205

206
    if (allColumnsHaveAgg == true) {
207 208 209
      int32_t numOfCols = pBlock->info.numOfCols;

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

214 215 216 217 218 219 220
      for (int32_t i = 0; i < numOfCols; ++i) {
        SColMatchInfo* pColMatchInfo = taosArrayGet(pTableScanInfo->pColMatchInfo, i);
        if (!pColMatchInfo->output) {
          continue;
        }
        pBlock->pBlockAgg[pColMatchInfo->targetSlotId] = pColAgg[i];
      }
H
Haojun Liao 已提交
221

222
      return TSDB_CODE_SUCCESS;
223
    } else {  // failed to load the block sma data, data block statistics does not exist, load data block instead
H
Haojun Liao 已提交
224
      *status = FUNC_DATA_REQUIRED_DATA_LOAD;
225
    }
H
Haojun Liao 已提交
226
  }
227

228
  ASSERT(*status == FUNC_DATA_REQUIRED_DATA_LOAD);
229

H
Haojun Liao 已提交
230 231 232 233 234 235 236 237 238 239
  // 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 已提交
240

H
Haojun Liao 已提交
241 242
  pCost->totalCheckedRows += pBlock->info.rows;
  pCost->loadBlocks += 1;
243

H
Haojun Liao 已提交
244 245 246
  SArray* pCols = tsdbRetrieveDataBlock(pTableScanInfo->dataReader, NULL);
  if (pCols == NULL) {
    return terrno;
H
Haojun Liao 已提交
247 248
  }

H
Haojun Liao 已提交
249
  relocateColumnData(pBlock, pTableScanInfo->pColMatchInfo, pCols);
250 251 252 253 254 255

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

256
  // todo record the filter time cost
257 258
  doFilter(pTableScanInfo->pFilterNode, pBlock, pTableScanInfo->pColMatchInfo);

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

H
Haojun Liao 已提交
265 266 267
  return TSDB_CODE_SUCCESS;
}

268
static void prepareForDescendingScan(STableScanInfo* pTableScanInfo, SqlFunctionCtx* pCtx, int32_t numOfOutput) {
H
Haojun Liao 已提交
269 270 271
  SET_REVERSE_SCAN_FLAG(pTableScanInfo);

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

274
  STimeWindow* pTWindow = &pTableScanInfo->cond.twindow;
wafwerar's avatar
wafwerar 已提交
275
  TSWAP(pTWindow->skey, pTWindow->ekey);
276
  pTableScanInfo->cond.order = TSDB_ORDER_DESC;
H
Haojun Liao 已提交
277 278
}

279
void addTagPseudoColumnData(STableScanInfo* pTableScanInfo, SSDataBlock* pBlock) {
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
  // currently only the tbname pseudo column
  if (pTableScanInfo->numOfPseudoExpr == 0) {
    return;
  }

  SMetaReader mr = {0};
  metaReaderInit(&mr, pTableScanInfo->readHandle.meta, 0);
  metaGetTableEntryByUid(&mr, pBlock->info.uid);

  for (int32_t j = 0; j < pTableScanInfo->numOfPseudoExpr; ++j) {
    SExprInfo* pExpr = &pTableScanInfo->pPseudoExpr[j];

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

    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, dstSlotId);
    colInfoDataEnsureCapacity(pColInfoData, 0, pBlock->info.rows);

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

    // this is to handle the tbname
    if (fmIsScanPseudoColumnFunc(functionId)) {
301
      setTbNameColData(pTableScanInfo->readHandle.meta, pBlock, pColInfoData, functionId);
302
    } else {  // these are tags
wmmhello's avatar
wmmhello 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316
      const char* p = NULL;
      if(pColInfoData->info.type == TSDB_DATA_TYPE_JSON){
        const uint8_t *tmp = mr.me.ctbEntry.pTags;
        char *data = taosMemoryCalloc(kvRowLen(tmp) + 1, 1);
        if(data == NULL){
          qError("doTagScan calloc error:%d", kvRowLen(tmp) + 1);
          return;
        }
        *data = TSDB_DATA_TYPE_JSON;
        memcpy(data+1, tmp, kvRowLen(tmp));
        p = data;
      }else{
        p = metaGetTableTagVal(&mr.me, pExpr->base.pParam[0].pCol->colId);
      }
317 318 319
      for (int32_t i = 0; i < pBlock->info.rows; ++i) {
        colDataAppend(pColInfoData, i, p, (p == NULL));
      }
wmmhello's avatar
wmmhello 已提交
320 321 322
      if(pColInfoData->info.type == TSDB_DATA_TYPE_JSON){
        taosMemoryFree((void*)p);
      }
323 324 325 326 327 328
    }
  }

  metaReaderClear(&mr);
}

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
void setTbNameColData(void* pMeta, const SSDataBlock* pBlock, SColumnInfoData* pColInfoData, int32_t functionId) {
  struct SScalarFuncExecFuncs fpSet = {0};
  fmGetScalarFuncExecFuncs(functionId, &fpSet);

  SColumnInfoData infoData = {0};
  infoData.info.type = TSDB_DATA_TYPE_BIGINT;
  infoData.info.bytes = sizeof(uint64_t);
  colInfoDataEnsureCapacity(&infoData, 0, 1);

  colDataAppendInt64(&infoData, 0, (int64_t*) &pBlock->info.uid);
  SScalarParam srcParam = {
      .numOfRows = pBlock->info.rows, .param = pMeta, .columnData = &infoData};

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

346
static SSDataBlock* doTableScanImpl(SOperatorInfo* pOperator) {
H
Haojun Liao 已提交
347
  STableScanInfo* pTableScanInfo = pOperator->info;
L
Liu Jicong 已提交
348
  SSDataBlock*    pBlock = pTableScanInfo->pResBlock;
H
Haojun Liao 已提交
349 350 351 352 353 354 355 356

  while (tsdbNextDataBlock(pTableScanInfo->dataReader)) {
    if (isTaskKilled(pOperator->pTaskInfo)) {
      longjmp(pOperator->pTaskInfo->env, TSDB_CODE_TSC_QUERY_CANCELLED);
    }

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

357
    uint32_t status = 0;
H
Haojun Liao 已提交
358
    int32_t  code = loadDataBlock(pOperator, pTableScanInfo, pBlock, &status);
H
Haojun Liao 已提交
359 360 361 362 363
    //    int32_t  code = loadDataBlockOnDemand(pOperator->pRuntimeEnv, pTableScanInfo, pBlock, &status);
    if (code != TSDB_CODE_SUCCESS) {
      longjmp(pOperator->pTaskInfo->env, code);
    }

364 365
    // current block is filter out according to filter condition, continue load the next block
    if (status == FUNC_DATA_REQUIRED_FILTEROUT || pBlock->info.rows == 0) {
H
Haojun Liao 已提交
366 367 368 369 370 371 372 373 374
      continue;
    }

    return pBlock;
  }

  return NULL;
}

375
static SSDataBlock* doTableScan(SOperatorInfo* pOperator) {
H
Haojun Liao 已提交
376 377 378 379
  STableScanInfo* pTableScanInfo = pOperator->info;
  SExecTaskInfo*  pTaskInfo = pOperator->pTaskInfo;

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

384 385
  // do the ascending order traverse in the first place.
  while (pTableScanInfo->scanTimes < pTableScanInfo->scanInfo.numOfAsc) {
386
    SSDataBlock* p = doTableScanImpl(pOperator);
H
Haojun Liao 已提交
387 388 389 390
    if (p != NULL) {
      return p;
    }

391
    pTableScanInfo->scanTimes += 1;
392

393
    if (pTableScanInfo->scanTimes < pTableScanInfo->scanInfo.numOfAsc) {
394 395 396 397
      setTaskStatus(pTaskInfo, TASK_NOT_COMPLETED);
      pTableScanInfo->scanFlag = REPEAT_SCAN;

      STimeWindow* pWin = &pTableScanInfo->cond.twindow;
398
      qDebug("%s start to repeat ascending order scan data blocks due to query func required, qrange:%" PRId64
399
             "-%" PRId64, GET_TASKID(pTaskInfo), pWin->skey, pWin->ekey);
400 401 402

      // do prepare for the next round table scan operation
      tsdbResetReadHandle(pTableScanInfo->dataReader, &pTableScanInfo->cond);
H
Haojun Liao 已提交
403
    }
404
  }
H
Haojun Liao 已提交
405

406
  int32_t total = pTableScanInfo->scanInfo.numOfAsc + pTableScanInfo->scanInfo.numOfDesc;
407
  if (pTableScanInfo->scanTimes < total) {
408 409 410 411
    if (pTableScanInfo->cond.order == TSDB_ORDER_ASC) {
      prepareForDescendingScan(pTableScanInfo, pTableScanInfo->pCtx, pTableScanInfo->numOfOutput);
      tsdbResetReadHandle(pTableScanInfo->dataReader, &pTableScanInfo->cond);
    }
H
Haojun Liao 已提交
412

413 414 415
    STimeWindow* pWin = &pTableScanInfo->cond.twindow;
    qDebug("%s start to descending order scan data blocks due to query func required, qrange:%" PRId64 "-%" PRId64,
           GET_TASKID(pTaskInfo), pWin->skey, pWin->ekey);
H
Haojun Liao 已提交
416

417
    while (pTableScanInfo->scanTimes < total) {
418
      SSDataBlock* p = doTableScanImpl(pOperator);
419 420 421
      if (p != NULL) {
        return p;
      }
H
Haojun Liao 已提交
422

423
      pTableScanInfo->scanTimes += 1;
H
Haojun Liao 已提交
424

425
      if (pTableScanInfo->scanTimes < pTableScanInfo->scanInfo.numOfAsc) {
426 427
        setTaskStatus(pTaskInfo, TASK_NOT_COMPLETED);
        pTableScanInfo->scanFlag = REPEAT_SCAN;
H
Haojun Liao 已提交
428

429
        qDebug("%s start to repeat descending order scan data blocks due to query func required, qrange:%" PRId64
L
Liu Jicong 已提交
430 431
               "-%" PRId64,
               GET_TASKID(pTaskInfo), pTaskInfo->window.skey, pTaskInfo->window.ekey);
H
Haojun Liao 已提交
432

433 434 435
        // do prepare for the next round table scan operation
        tsdbResetReadHandle(pTableScanInfo->dataReader, &pTableScanInfo->cond);
      }
H
Haojun Liao 已提交
436 437 438
    }
  }

439 440
  setTaskStatus(pTaskInfo, TASK_COMPLETED);
  return NULL;
H
Haojun Liao 已提交
441 442
}

443 444 445 446 447 448 449 450 451 452 453 454
SInterval extractIntervalInfo(const STableScanPhysiNode* pTableScanNode) {
  SInterval interval = {
      .interval = pTableScanNode->interval,
      .sliding = pTableScanNode->sliding,
      .intervalUnit = pTableScanNode->intervalUnit,
      .slidingUnit = pTableScanNode->slidingUnit,
      .offset = pTableScanNode->offset,
  };

  return interval;
}

455 456 457 458 459 460 461 462 463 464
static void destroyTableScanOperatorInfo(void* param, int32_t numOfOutput) {
  STableScanInfo* pTableScanInfo = (STableScanInfo*)param;
  taosMemoryFree(pTableScanInfo->pResBlock);
  tsdbCleanupReadHandle(pTableScanInfo->dataReader);

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

465
SOperatorInfo* createTableScanOperatorInfo(STableScanPhysiNode* pTableScanNode, tsdbReaderT pDataReader, SReadHandle* readHandle, SExecTaskInfo* pTaskInfo) {
H
Haojun Liao 已提交
466 467 468 469 470 471 472 473 474 475
  STableScanInfo* pInfo = taosMemoryCalloc(1, sizeof(STableScanInfo));
  SOperatorInfo*  pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
  if (pInfo == NULL || pOperator == NULL) {
    taosMemoryFreeClear(pInfo);
    taosMemoryFreeClear(pOperator);

    pTaskInfo->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
    return NULL;
  }

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

478
  int32_t numOfCols = 0;
479
  SArray* pColList = extractColMatchInfo(pTableScanNode->scan.pScanCols, pDescNode, &numOfCols, pTaskInfo, COL_MATCH_FROM_COL_ID);
L
Liu Jicong 已提交
480

481 482 483 484 485 486 487 488 489 490 491
  int32_t code = initQueryTableDataCond(&pInfo->cond, pTableScanNode);
  if (code != TSDB_CODE_SUCCESS) {
    return NULL;
  }

  if (pTableScanNode->scan.pScanPseudoCols != NULL) {
    pInfo->pPseudoExpr = createExprInfo(pTableScanNode->scan.pScanPseudoCols, NULL, &pInfo->numOfPseudoExpr);
    pInfo->pPseudoCtx  = createSqlFunctionCtx(pInfo->pPseudoExpr, pInfo->numOfPseudoExpr, &pInfo->rowCellInfoOffset);
  }

  pInfo->scanInfo = (SScanInfo){.numOfAsc = pTableScanNode->scanSeq[0], .numOfDesc = pTableScanNode->scanSeq[1]};
492
//  pInfo->scanInfo = (SScanInfo){.numOfAsc = 0, .numOfDesc = 1}; // for debug purpose
493 494 495 496 497 498 499 500 501 502 503 504

  pInfo->readHandle        = *readHandle;
  pInfo->interval          = extractIntervalInfo(pTableScanNode);
  pInfo->sampleRatio       = pTableScanNode->ratio;
  pInfo->dataBlockLoadFlag = pTableScanNode->dataRequired;
  pInfo->pResBlock         = createResDataBlock(pDescNode);
  pInfo->pFilterNode       = pTableScanNode->scan.node.pConditions;
  pInfo->dataReader        = pDataReader;
  pInfo->scanFlag          = MAIN_SCAN;
  pInfo->pColMatchInfo     = pColList;

  pOperator->name         = "TableScanOperator";  // for debug purpose
L
Liu Jicong 已提交
505
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN;
506 507 508 509 510
  pOperator->blocking     = false;
  pOperator->status       = OP_NOT_OPENED;
  pOperator->info         = pInfo;
  pOperator->numOfExprs   = numOfCols;
  pOperator->pTaskInfo    = pTaskInfo;
511

512
  pOperator->fpSet = createOperatorFpSet(operatorDummyOpenFn, doTableScan, NULL, NULL, destroyTableScanOperatorInfo, NULL, NULL, NULL);
H
Haojun Liao 已提交
513

D
dapan1121 已提交
514
  static int32_t cost = 0;
515 516 517

  // for non-blocking operator, the open cost is always 0
  pOperator->cost.openCost = 0;
D
dapan1121 已提交
518 519 520
  pOperator->cost.totalCost = ++cost;
  pOperator->resultInfo.totalRows = ++cost;

H
Haojun Liao 已提交
521 522 523
  return pOperator;
}

524
SOperatorInfo* createTableSeqScanOperatorInfo(void* pReadHandle, SExecTaskInfo* pTaskInfo) {
H
Haojun Liao 已提交
525
  STableScanInfo* pInfo = taosMemoryCalloc(1, sizeof(STableScanInfo));
L
Liu Jicong 已提交
526
  SOperatorInfo*  pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
H
Haojun Liao 已提交
527

L
Liu Jicong 已提交
528 529
  pInfo->dataReader = pReadHandle;
  //  pInfo->prevGroupId       = -1;
H
Haojun Liao 已提交
530

L
Liu Jicong 已提交
531
  pOperator->name = "TableSeqScanOperator";
H
Haojun Liao 已提交
532
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_TABLE_SEQ_SCAN;
L
Liu Jicong 已提交
533 534 535 536
  pOperator->blocking = false;
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
  pOperator->pTaskInfo = pTaskInfo;
H
Haojun Liao 已提交
537

538
  pOperator->fpSet = createOperatorFpSet(operatorDummyOpenFn, doTableScanImpl, NULL, NULL, NULL, NULL, NULL, NULL);
H
Haojun Liao 已提交
539 540 541
  return pOperator;
}

542
static SSDataBlock* doBlockInfoScan(SOperatorInfo* pOperator) {
H
Haojun Liao 已提交
543 544 545 546 547 548 549 550 551
  if (pOperator->status == OP_EXEC_DONE) {
    return NULL;
  }

  STableScanInfo* pTableScanInfo = pOperator->info;

  STableBlockDistInfo tableBlockDist = {0};
  tableBlockDist.numOfTables = 1;  // TODO set the correct number of tables

S
Shengliang Guan 已提交
552 553
  int32_t numRowSteps = TSDB_DEFAULT_MAXROWS_FBLOCK / TSDB_BLOCK_DIST_STEP_ROWS;
  if (TSDB_DEFAULT_MAXROWS_FBLOCK % TSDB_BLOCK_DIST_STEP_ROWS != 0) {
H
Haojun Liao 已提交
554 555 556
    ++numRowSteps;
  }

557
  tableBlockDist.dataBlockInfos = taosArrayInit(numRowSteps, sizeof(SFileBlockInfo));
H
Haojun Liao 已提交
558 559 560 561 562 563
  taosArraySetSize(tableBlockDist.dataBlockInfos, numRowSteps);

  tableBlockDist.maxRows = INT_MIN;
  tableBlockDist.minRows = INT_MAX;

  tsdbGetFileBlocksDistInfo(pTableScanInfo->dataReader, &tableBlockDist);
564
  tableBlockDist.numOfRowsInMemTable = (int32_t)tsdbGetNumOfRowsInMemTable(pTableScanInfo->dataReader);
H
Haojun Liao 已提交
565

566
  SSDataBlock* pBlock = pTableScanInfo->pResBlock;
567
  pBlock->info.rows = 1;
H
Haojun Liao 已提交
568 569
  pBlock->info.numOfCols = 1;

570 571
  //  SBufferWriter bw = tbufInitWriter(NULL, false);
  //  blockDistInfoToBinary(&tableBlockDist, &bw);
H
Haojun Liao 已提交
572 573
  SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, 0);

574 575 576 577 578 579
  //  int32_t len = (int32_t) tbufTell(&bw);
  //  pColInfo->pData = taosMemoryMalloc(len + sizeof(int32_t));
  //  *(int32_t*) pColInfo->pData = len;
  //  memcpy(pColInfo->pData + sizeof(int32_t), tbufGetData(&bw, false), len);
  //
  //  tbufCloseWriter(&bw);
H
Haojun Liao 已提交
580

581 582
  //  SArray* g = GET_TABLEGROUP(pOperator->, 0);
  //  pOperator->pRuntimeEnv->current = taosArrayGetP(g, 0);
H
Haojun Liao 已提交
583 584 585 586 587 588

  pOperator->status = OP_EXEC_DONE;
  return pBlock;
}

SOperatorInfo* createDataBlockInfoScanOperator(void* dataReader, SExecTaskInfo* pTaskInfo) {
589 590
  STableScanInfo* pInfo = taosMemoryCalloc(1, sizeof(STableScanInfo));
  SOperatorInfo*  pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
H
Haojun Liao 已提交
591 592 593 594 595
  if (pInfo == NULL || pOperator == NULL) {
    pTaskInfo->code = TSDB_CODE_OUT_OF_MEMORY;
    goto _error;
  }

596 597
  pInfo->dataReader = dataReader;
  //  pInfo->block.pDataBlock = taosArrayInit(1, sizeof(SColumnInfoData));
H
Haojun Liao 已提交
598 599

  SColumnInfoData infoData = {0};
600
  infoData.info.type = TSDB_DATA_TYPE_BINARY;
H
Haojun Liao 已提交
601 602
  infoData.info.bytes = 1024;
  infoData.info.colId = 0;
603
  //  taosArrayPush(pInfo->block.pDataBlock, &infoData);
H
Haojun Liao 已提交
604

605
  pOperator->name = "DataBlockInfoScanOperator";
H
Haojun Liao 已提交
606
  //  pOperator->operatorType = OP_TableBlockInfoScan;
607
  pOperator->blocking = false;
608
  pOperator->status = OP_NOT_OPENED;
609 610
  pOperator->fpSet._openFn = operatorDummyOpenFn;
  pOperator->fpSet.getNextFn = doBlockInfoScan;
H
Haojun Liao 已提交
611

612 613
  pOperator->info = pInfo;
  pOperator->pTaskInfo = pTaskInfo;
H
Haojun Liao 已提交
614 615 616

  return pOperator;

617
_error:
H
Haojun Liao 已提交
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
  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);
}

5
54liuyao 已提交
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
static bool prepareDataScan(SStreamBlockScanInfo* pInfo) {
  SSDataBlock* pSDB = pInfo->pUpdateRes;
  if (pInfo->updateResIndex < pSDB->info.rows) {
    SColumnInfoData* pColDataInfo = taosArrayGet(pSDB->pDataBlock, 0);
    TSKEY *tsCols = (TSKEY*)pColDataInfo->pData;
    SResultRowInfo dumyInfo;
    dumyInfo.cur.pageId = -1;
    STimeWindow win = getActiveTimeWindow(NULL, &dumyInfo, tsCols[pInfo->updateResIndex], &pInfo->interval,
                                        pInfo->interval.precision, NULL);
    STableScanInfo* pTableScanInfo = pInfo->pOperatorDumy->info;
    pTableScanInfo->cond.twindow = win;
    tsdbResetReadHandle(pTableScanInfo->dataReader, &pTableScanInfo->cond);
    pInfo->updateResIndex += getNumOfRowsInTimeWindow(&pSDB->info, tsCols, pInfo->updateResIndex,
        win.ekey, binarySearchForKey, NULL, TSDB_ORDER_ASC);
    pTableScanInfo->scanTimes = 0;
    return true;
  } else {
    return false;
  }
}

static SSDataBlock* doDataScan(SStreamBlockScanInfo* pInfo) {
  SSDataBlock* pResult = NULL;
  pResult = doTableScan(pInfo->pOperatorDumy);
  if (pResult == NULL) {
    if (prepareDataScan(pInfo)) {
      // scan next window data
      pResult = doTableScan(pInfo->pOperatorDumy);
    }
  }
  return pResult;
}

static SSDataBlock* getUpdateDataBlock(SStreamBlockScanInfo* pInfo, bool invertible) {
5
54liuyao 已提交
668
  SColumnInfoData* pColDataInfo = taosArrayGet(pInfo->pRes->pDataBlock, pInfo->primaryTsIndex);
L
Liu Jicong 已提交
669
  TSKEY*           ts = (TSKEY*)pColDataInfo->pData;
5
54liuyao 已提交
670 671
  for (int32_t i = 0; i < pInfo->pRes->info.rows; i++) {
    if (updateInfoIsUpdated(pInfo->pUpdateInfo, pInfo->pRes->info.uid, ts[i])) {
L
Liu Jicong 已提交
672
      taosArrayPush(pInfo->tsArray, ts + i);
5
54liuyao 已提交
673 674
    }
  }
5
54liuyao 已提交
675 676
  int32_t size = taosArrayGetSize(pInfo->tsArray);
  if (size > 0 && invertible) {
L
Liu Jicong 已提交
677 678 679 680 681
    // TODO(liuyao) get from tsdb
    //  SSDataBlock* p = createOneDataBlock(pInfo->pRes, true);
    //  p->info.type = STREAM_INVERT;
    //  taosArrayClear(pInfo->tsArray);
    //  return p;
5
54liuyao 已提交
682 683 684 685 686 687 688 689 690 691
    SSDataBlock* pDataBlock = createOneDataBlock(pInfo->pRes, false);
    SColumnInfoData* pCol = (SColumnInfoData*) taosArrayGet(pDataBlock->pDataBlock, 0);
    ASSERT(pCol->info.type == TSDB_DATA_TYPE_TIMESTAMP);
    colInfoDataEnsureCapacity(pCol, 0, size);
    for (int32_t i = 0; i < size; i++) {
      TSKEY* pTs = (TSKEY*)taosArrayGet(pInfo->tsArray, i);
      colDataAppend(pCol, i, (char*)pTs, false);
    }
    pDataBlock->info.rows = size;
    pDataBlock->info.type = STREAM_REPROCESS;
5
54liuyao 已提交
692
    blockDataUpdateTsWindow(pDataBlock);
5
54liuyao 已提交
693
    taosArrayClear(pInfo->tsArray);
5
54liuyao 已提交
694
    return pDataBlock;
5
54liuyao 已提交
695 696 697 698
  }
  return NULL;
}

5
54liuyao 已提交
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
void static setSupKeyBuf(SCatchSupporter* pSup, int64_t groupId, int64_t childId, TSKEY ts) {
  int64_t* pKey = (int64_t*)pSup->pKeyBuf;
  pKey[0] = groupId;
  pKey[1] = childId;
  pKey[2] = ts;
}

static int32_t catchWidonwInfo(SSDataBlock* pDataBlock, SCatchSupporter* pSup,
    int32_t pageId, int32_t tsIndex, int64_t childId) {
  SColumnInfoData* pColDataInfo = taosArrayGet(pDataBlock->pDataBlock, tsIndex);
  TSKEY* tsCols = (int64_t*)pColDataInfo->pData;
  for (int32_t i = 0; i < pDataBlock->info.rows; i++) {
    setSupKeyBuf(pSup, pDataBlock->info.groupId, childId, tsCols[i]);
    SWindowPosition* p1 = (SWindowPosition*)taosHashGet(pSup->pWindowHashTable,
        pSup->pKeyBuf, pSup->keySize);
    if (p1 == NULL) {
      SWindowPosition pos = {.pageId = pageId, .rowId = i};
      int32_t code = taosHashPut(pSup->pWindowHashTable, pSup->pKeyBuf, pSup->keySize, &pos,
          sizeof(SWindowPosition));
      if (code != TSDB_CODE_SUCCESS ) {
        return code;
      }
    } else {
      p1->pageId = pageId;
      p1->rowId = i;
    }
  }
  return TSDB_CODE_SUCCESS;
}

static int32_t catchDatablock(SSDataBlock* pDataBlock, SCatchSupporter* pSup,
    int32_t tsIndex, int64_t childId) {
  int32_t start = 0;
  int32_t stop = 0;
  int32_t pageSize = getBufPageSize(pSup->pDataBuf);
  while(start < pDataBlock->info.rows) {
    blockDataSplitRows(pDataBlock, pDataBlock->info.hasVarCol, start, &stop, pageSize);
    SSDataBlock* pDB = blockDataExtractBlock(pDataBlock, start, stop - start + 1);
    if (pDB == NULL) {
      return terrno;
    }
    int32_t pageId = -1;
    void* pPage = getNewBufPage(pSup->pDataBuf, pDataBlock->info.groupId, &pageId);
    if (pPage == NULL) {
      blockDataDestroy(pDB);
      return terrno;
    }
    int32_t size = blockDataGetSize(pDB) + sizeof(int32_t)  + pDB->info.numOfCols * sizeof(int32_t);
    assert(size <= pageSize);
    blockDataToBuf(pPage, pDB);
    setBufPageDirty(pPage, true);
    releaseBufPage(pSup->pDataBuf, pPage);
    blockDataDestroy(pDB);
    start = stop + 1;
    int32_t code = catchWidonwInfo(pDB, pSup, pageId, tsIndex, childId);
    if (code != TSDB_CODE_SUCCESS ) {
      return code;
    }
  }
  return TSDB_CODE_SUCCESS;
}

static SSDataBlock* getDataFromCatch(SStreamBlockScanInfo* pInfo) {
  SSDataBlock* pBlock = pInfo->pUpdateRes;
  if (pInfo->updateResIndex < pBlock->info.rows) {
    blockDataCleanup(pInfo->pRes);
    SCatchSupporter* pCSup = &pInfo->childAggSup;
    SColumnInfoData* pColDataInfo = taosArrayGet(pBlock->pDataBlock, 0);
    TSKEY *tsCols = (TSKEY*)pColDataInfo->pData;
    int32_t size = taosArrayGetSize(pInfo->childIds);
    for (int32_t i = 0; i < size; i++) {
      int64_t id = *(int64_t *)taosArrayGet(pInfo->childIds, i);
      setSupKeyBuf(pCSup, pBlock->info.groupId, id,
          tsCols[pInfo->updateResIndex]);
      SWindowPosition* pos = (SWindowPosition*)taosHashGet(pCSup->pWindowHashTable,
          pCSup->pKeyBuf, pCSup->keySize);
      void* buf = getBufPage(pCSup->pDataBuf, pos->pageId);
      SSDataBlock* pDB = createOneDataBlock(pInfo->pRes, false);
      blockDataFromBuf(pDB, buf);
      SSDataBlock* pSub = blockDataExtractBlock(pDB, pos->rowId, 1);
      blockDataMerge(pInfo->pRes, pSub, NULL);
      blockDataDestroy(pDB);
      blockDataDestroy(pSub);
    }
    pInfo->updateResIndex++;
    return pInfo->pRes;
  }
  return NULL;
}

789
static SSDataBlock* doStreamBlockScan(SOperatorInfo* pOperator) {
H
Haojun Liao 已提交
790 791 792
  // NOTE: this operator does never check if current status is done or not
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
  SStreamBlockScanInfo* pInfo = pOperator->info;
L
Liu Jicong 已提交
793
  int32_t               rows = 0;
H
Haojun Liao 已提交
794

795
  pTaskInfo->code = pOperator->fpSet._openFn(pOperator);
796
  if (pTaskInfo->code != TSDB_CODE_SUCCESS || pOperator->status == OP_EXEC_DONE) {
H
Haojun Liao 已提交
797 798 799
    return NULL;
  }

5
54liuyao 已提交
800
  size_t total = taosArrayGetSize(pInfo->pBlockLists);
H
Haojun Liao 已提交
801
  if (pInfo->blockType == STREAM_DATA_TYPE_SSDATA_BLOCK) {
5
54liuyao 已提交
802 803 804 805 806 807 808 809 810
    if (pInfo->scanMode == STREAM_SCAN_FROM_UPDATERES) {
      SSDataBlock* pDB = getDataFromCatch(pInfo);
      if (pDB != NULL) {
        return pDB;
      } else {
       pInfo->scanMode = STREAM_SCAN_FROM_READERHANDLE;
      }
    }

H
Haojun Liao 已提交
811 812
    if (pInfo->validBlockIndex >= total) {
      doClearBufferedBlocks(pInfo);
813
      pOperator->status = OP_EXEC_DONE;
H
Haojun Liao 已提交
814 815 816 817
      return NULL;
    }

    int32_t current = pInfo->validBlockIndex++;
5
54liuyao 已提交
818 819 820 821 822 823 824 825 826 827 828
    SSDataBlock* pBlock = taosArrayGetP(pInfo->pBlockLists, current);
    if (pBlock->info.type == STREAM_REPROCESS) {
      pInfo->scanMode = STREAM_SCAN_FROM_UPDATERES;
    } else {
      int32_t code = catchDatablock(pBlock, &pInfo->childAggSup, pInfo->primaryTsIndex, 0);
      if (code != TDB_CODE_SUCCESS) {
        pTaskInfo->code = code;
        longjmp(pTaskInfo->env, code);
      }
    }
    return pBlock;
H
Haojun Liao 已提交
829
  } else {
5
54liuyao 已提交
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
    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) {
      blockDataCleanup(pInfo->pRes);
      pInfo->scanMode = STREAM_SCAN_FROM_DATAREADER;
      return pInfo->pUpdateRes;
    } else if (pInfo->scanMode == STREAM_SCAN_FROM_DATAREADER) {
      SSDataBlock* pSDB = doDataScan(pInfo);
      if (pSDB == NULL) {
        pInfo->scanMode = STREAM_SCAN_FROM_READERHANDLE;
      } else {
        return pSDB;
      }
5
54liuyao 已提交
845
    }
5
54liuyao 已提交
846

H
Haojun Liao 已提交
847 848 849
    SDataBlockInfo* pBlockInfo = &pInfo->pRes->info;
    blockDataCleanup(pInfo->pRes);

850
    while (tqNextDataBlock(pInfo->streamBlockReader)) {
851
      SArray*  pCols = NULL;
852 853 854 855 856
      uint64_t groupId = 0;
      uint64_t uid = 0;
      int32_t  numOfRows = 0;
      int16_t  outputCol = 0;

857
      int32_t code = tqRetrieveDataBlock(&pCols, pInfo->streamBlockReader, &groupId, &uid, &numOfRows, &outputCol);
H
Haojun Liao 已提交
858

859 860 861
      if (code != TSDB_CODE_SUCCESS || numOfRows == 0) {
        pTaskInfo->code = code;
        return NULL;
H
Haojun Liao 已提交
862 863
      }

864 865
      pInfo->pRes->info.groupId = groupId;
      pInfo->pRes->info.rows = numOfRows;
866
      pInfo->pRes->info.uid = uid;
5
54liuyao 已提交
867
      pInfo->pRes->info.type = STREAM_NORMAL;
H
Haojun Liao 已提交
868 869 870

      int32_t numOfCols = pInfo->pRes->info.numOfCols;
      for (int32_t i = 0; i < numOfCols; ++i) {
871
        SColMatchInfo* pColMatchInfo = taosArrayGet(pInfo->pColMatchInfo, i);
H
Haojun Liao 已提交
872 873 874 875
        if (!pColMatchInfo->output) {
          continue;
        }

876
        bool colExists = false;
877
        for (int32_t j = 0; j < taosArrayGetSize(pCols); ++j) {
878 879 880 881 882 883 884 885 886 887 888 889 890 891
          SColumnInfoData* pResCol = taosArrayGet(pCols, j);
          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);
          colInfoDataEnsureCapacity(pDst, 0, pBlockInfo->rows);
          colDataAppendNNULL(pDst, 0, pBlockInfo->rows);
        }
H
Haojun Liao 已提交
892 893 894 895
      }

      if (pInfo->pRes->pDataBlock == NULL) {
        // TODO add log
896
        pOperator->status = OP_EXEC_DONE;
H
Haojun Liao 已提交
897 898 899
        pTaskInfo->code = terrno;
        return NULL;
      }
5
54liuyao 已提交
900
      rows = pBlockInfo->rows;
901
      doFilter(pInfo->pCondition, pInfo->pRes, NULL);
5
54liuyao 已提交
902
      blockDataUpdateTsWindow(pInfo->pRes);
H
Haojun Liao 已提交
903 904 905 906 907 908 909 910

      break;
    }

    // record the scan action.
    pInfo->numOfExec++;
    pInfo->numOfRows += pBlockInfo->rows;

5
54liuyao 已提交
911
    if (rows == 0) {
912
      pOperator->status = OP_EXEC_DONE;
5
54liuyao 已提交
913 914
    } else if (pInfo->interval.interval > 0) {
      SSDataBlock* upRes = getUpdateDataBlock(pInfo, true); //TODO(liuyao) get invertible from plan
5
54liuyao 已提交
915
      if (upRes) {
5
54liuyao 已提交
916
        pInfo->pUpdateRes = upRes;
917
        if (upRes->info.type == STREAM_REPROCESS) {
5
54liuyao 已提交
918 919 920
          pInfo->updateResIndex = 0;
          prepareDataScan(pInfo);
          pInfo->scanMode = STREAM_SCAN_FROM_UPDATERES;
921
        } else if (upRes->info.type == STREAM_INVERT) {
5
54liuyao 已提交
922 923 924
          pInfo->scanMode = STREAM_SCAN_FROM_RES;
          return upRes;
        }
5
54liuyao 已提交
925
      }
926 927
    }

5
54liuyao 已提交
928
    return (rows == 0) ? NULL : pInfo->pRes;
H
Haojun Liao 已提交
929 930 931
  }
}

932 933 934 935
SOperatorInfo* createStreamScanOperatorInfo(void* streamReadHandle, void* pDataReader, SReadHandle* pHandle,
                                            uint64_t uid, SSDataBlock* pResBlock, SArray* pColList,
                                            SArray* pTableIdList, SExecTaskInfo* pTaskInfo, SNode* pCondition,
                                            SOperatorInfo* pOperatorDumy) {
H
Haojun Liao 已提交
936 937 938 939
  SStreamBlockScanInfo* pInfo = taosMemoryCalloc(1, sizeof(SStreamBlockScanInfo));
  SOperatorInfo*        pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
  if (pInfo == NULL || pOperator == NULL) {
    terrno = TSDB_CODE_QRY_OUT_OF_MEMORY;
940
    goto _error;
H
Haojun Liao 已提交
941 942
  }

5
54liuyao 已提交
943 944
  STableScanInfo* pSTInfo = (STableScanInfo*)pOperatorDumy->info;

H
Haojun Liao 已提交
945 946 947
  int32_t numOfOutput = taosArrayGetSize(pColList);

  SArray* pColIds = taosArrayInit(4, sizeof(int16_t));
948
  for (int32_t i = 0; i < numOfOutput; ++i) {
949 950 951
    SColMatchInfo* id = taosArrayGet(pColList, i);
    int16_t colId = id->colId;
    taosArrayPush(pColIds, &colId);
H
Haojun Liao 已提交
952 953 954 955 956 957 958 959
  }

  pInfo->pColMatchInfo = pColList;

  // set the extract column id to streamHandle
  tqReadHandleSetColIdList((STqReadHandle*)streamReadHandle, pColIds);
  int32_t code = tqReadHandleSetTbUidList(streamReadHandle, pTableIdList);
  if (code != 0) {
960
    goto _error;
H
Haojun Liao 已提交
961 962 963 964
  }

  pInfo->pBlockLists = taosArrayInit(4, POINTER_BYTES);
  if (pInfo->pBlockLists == NULL) {
965 966
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _error;
H
Haojun Liao 已提交
967 968
  }

5
54liuyao 已提交
969 970
  pInfo->tsArray = taosArrayInit(4, sizeof(TSKEY));
  if (pInfo->tsArray == NULL) {
971
    goto _error;
5
54liuyao 已提交
972 973
  }

L
Liu Jicong 已提交
974
  pInfo->primaryTsIndex = 0;                           // TODO(liuyao) get it from physical plan
5
54liuyao 已提交
975
  pInfo->pUpdateInfo = updateInfoInitP(&pSTInfo->interval, 10000); // TODO(liuyao) get watermark from physical plan
5
54liuyao 已提交
976
  if (pInfo->pUpdateInfo == NULL) {
977
    goto _error;
5
54liuyao 已提交
978 979
  }

980 981 982 983 984 985 986 987 988
  pInfo->readHandle     = *pHandle;
  pInfo->tableUid       = uid;
  pInfo->streamBlockReader = streamReadHandle;
  pInfo->pRes           = pResBlock;
  pInfo->pCondition     = pCondition;
  pInfo->pDataReader    = pDataReader;
  pInfo->scanMode       = STREAM_SCAN_FROM_READERHANDLE;
  pInfo->pOperatorDumy  = pOperatorDumy;
  pInfo->interval       = pSTInfo->interval;
L
Liu Jicong 已提交
989

5
54liuyao 已提交
990 991 992 993
  size_t childKeyBufSize = sizeof(int64_t) + sizeof(int64_t) + sizeof(TSKEY);
  initCatchSupporter(&pInfo->childAggSup, 1024, childKeyBufSize,
      "StreamFinalInterval", "/tmp/"); // TODO(liuyao) get row size from phy plan

994
  pOperator->name       = "StreamBlockScanOperator";
L
Liu Jicong 已提交
995
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN;
996 997 998
  pOperator->blocking   = false;
  pOperator->status     = OP_NOT_OPENED;
  pOperator->info       = pInfo;
L
Liu Jicong 已提交
999
  pOperator->numOfExprs = pResBlock->info.numOfCols;
1000
  pOperator->pTaskInfo  = pTaskInfo;
H
Haojun Liao 已提交
1001

L
Liu Jicong 已提交
1002 1003
  pOperator->fpSet =
      createOperatorFpSet(operatorDummyOpenFn, doStreamBlockScan, NULL, NULL, operatorDummyCloseFn, NULL, NULL, NULL);
1004

H
Haojun Liao 已提交
1005
  return pOperator;
1006

L
Liu Jicong 已提交
1007
_error:
1008 1009 1010
  taosMemoryFreeClear(pInfo);
  taosMemoryFreeClear(pOperator);
  return NULL;
H
Haojun Liao 已提交
1011 1012 1013 1014 1015 1016 1017
}

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

1018 1019
  const char* name = tNameGetTableName(&pInfo->name);
  if (strncasecmp(name, TSDB_INS_TABLE_USER_TABLES, TSDB_TABLE_FNAME_LEN) == 0) {
H
Haojun Liao 已提交
1020 1021
    metaCloseTbCursor(pInfo->pCur);
  }
H
Haojun Liao 已提交
1022 1023

  taosArrayDestroy(pInfo->scanCols);
H
Haojun Liao 已提交
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 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
}

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

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

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

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

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

      SValueNode* node = (SValueNode*)pNode;
      char*       dbName = nodesGetValueFromNode(node);
      strncpy(pContext, varDataVal(dbName), varDataLen(dbName));
      *((char*)pContext + varDataLen(dbName)) = 0;
1064
      return DEAL_RES_END;  // stop walk
H
Haojun Liao 已提交
1065 1066 1067 1068 1069 1070 1071
    }
    default:
      break;
  }
  return DEAL_RES_CONTINUE;
}

1072
static void getDBNameFromCondition(SNode* pCondition, const char* dbName) {
H
Haojun Liao 已提交
1073 1074 1075
  if (NULL == pCondition) {
    return;
  }
L
Liu Jicong 已提交
1076
  nodesWalkExpr(pCondition, getDBNameFromConditionWalker, (char*)dbName);
H
Haojun Liao 已提交
1077 1078
}

1079
static int32_t loadSysTableCallback(void* param, const SDataBuf* pMsg, int32_t code) {
H
Haojun Liao 已提交
1080 1081 1082 1083 1084 1085 1086
  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);
1087 1088 1089
    pRsp->useconds = htobe64(pRsp->useconds);
    pRsp->handle = htobe64(pRsp->handle);
    pRsp->compLen = htonl(pRsp->compLen);
H
Haojun Liao 已提交
1090 1091 1092 1093 1094
  } else {
    operator->pTaskInfo->code = code;
  }

  tsem_post(&pScanResInfo->ready);
wmmhello's avatar
wmmhello 已提交
1095
  return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
1096 1097 1098 1099 1100 1101 1102 1103
}

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

  SFilterInfo* filter = NULL;
1104 1105

  int32_t code = filterInitFromNode(pInfo->pCondition, &filter, 0);
H
Haojun Liao 已提交
1106 1107 1108 1109 1110

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

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

1114
  SSDataBlock* px = createOneDataBlock(pInfo->pRes, false);
H
Haojun Liao 已提交
1115 1116 1117 1118 1119 1120 1121 1122
  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 已提交
1123 1124 1125 1126 1127 1128 1129 1130 1131
    if (keep) {
      colDataAssign(pDest, pSrc, pInfo->pRes->info.rows);
      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;
        }
1132

1133 1134 1135 1136 1137 1138
        if (colDataIsNull_s(pSrc, j)) {
          colDataAppendNULL(pDest, numOfRow);
        } else {
          colDataAppend(pDest, numOfRow, colDataGetData(pSrc, j), false);
        }

D
dapan1121 已提交
1139
        numOfRow += 1;
H
Haojun Liao 已提交
1140
      }
D
dapan1121 已提交
1141 1142
    } else {
      numOfRow = 0;
H
Haojun Liao 已提交
1143 1144 1145 1146 1147 1148 1149 1150 1151
    }
  }

  px->info.rows = numOfRow;
  pInfo->pRes = px;

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

1152 1153
static SSDataBlock* buildSysTableMetaBlock() {
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
1154

L
Liu Jicong 已提交
1155 1156
  size_t               size = 0;
  const SSysTableMeta* pMeta = NULL;
1157 1158 1159
  getInfosDbMeta(&pMeta, &size);

  int32_t index = 0;
L
Liu Jicong 已提交
1160 1161
  for (int32_t i = 0; i < size; ++i) {
    if (strcmp(pMeta[i].name, TSDB_INS_TABLE_USER_TABLES) == 0) {
1162 1163 1164 1165
      index = i;
      break;
    }
  }
1166 1167 1168

  pBlock->pDataBlock = taosArrayInit(pBlock->info.numOfCols, sizeof(SColumnInfoData));

L
Liu Jicong 已提交
1169
  for (int32_t i = 0; i < pMeta[index].colNum; ++i) {
1170 1171 1172 1173 1174 1175 1176 1177 1178
    SColumnInfoData colInfoData = {0};
    colInfoData.info.colId = i + 1;
    colInfoData.info.type = pMeta[index].schema[i].type;
    colInfoData.info.bytes = pMeta[index].schema[i].bytes;
    taosArrayPush(pBlock->pDataBlock, &colInfoData);
  }

  pBlock->info.numOfCols = pMeta[index].colNum;
  pBlock->info.hasVarCol = true;
1179 1180 1181 1182

  return pBlock;
}

1183
static SSDataBlock* doSysTableScan(SOperatorInfo* pOperator) {
H
Haojun Liao 已提交
1184 1185 1186 1187 1188
  // 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
1189 1190
  const char* name = tNameGetTableName(&pInfo->name);
  if (strncasecmp(name, TSDB_INS_TABLE_USER_TABLES, TSDB_TABLE_FNAME_LEN) == 0) {
1191 1192 1193 1194 1195
    // the retrieve is executed on the mnode, so return tables that belongs to the information schema database.
    if (pInfo->readHandle.mnd != NULL) {
      if (pOperator->status == OP_EXEC_DONE) {
        return NULL;
      }
1196

1197
      buildSysDbTableInfo(pInfo, pOperator->resultInfo.capacity);
1198

1199 1200
      doFilterResult(pInfo);
      pInfo->loadInfo.totalRows += pInfo->pRes->info.rows;
1201

1202 1203 1204 1205 1206 1207
      pOperator->status = OP_EXEC_DONE;
      return (pInfo->pRes->info.rows == 0) ? NULL : pInfo->pRes;
    } else {
      if (pInfo->pCur == NULL) {
        pInfo->pCur = metaOpenTbCursor(pInfo->readHandle.meta);
      }
1208

1209
      blockDataCleanup(pInfo->pRes);
1210

1211
      int32_t numOfRows = 0;
1212

1213 1214 1215
      const char* db = NULL;
      int32_t     vgId = 0;
      vnodeGetInfo(pInfo->readHandle.vnode, &db, &vgId);
1216

1217 1218 1219
      SName sn = {0};
      char  dbname[TSDB_DB_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
      tNameFromString(&sn, db, T_NAME_ACCT | T_NAME_DB);
1220

1221 1222
      tNameGetDbName(&sn, varDataVal(dbname));
      varDataSetLen(dbname, strlen(varDataVal(dbname)));
1223

1224
      SSDataBlock* p = buildSysTableMetaBlock();
1225
      blockDataEnsureCapacity(p, pOperator->resultInfo.capacity);
1226

1227 1228 1229
      char n[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
      while (metaTbCursorNext(pInfo->pCur) == 0) {
        STR_TO_VARSTR(n, pInfo->pCur->mr.me.name);
1230

1231 1232 1233
        // table name
        SColumnInfoData* pColInfoData = taosArrayGet(p->pDataBlock, 0);
        colDataAppend(pColInfoData, numOfRows, n, false);
1234

1235 1236 1237
        // database name
        pColInfoData = taosArrayGet(p->pDataBlock, 1);
        colDataAppend(pColInfoData, numOfRows, dbname, false);
1238

1239 1240 1241
        // vgId
        pColInfoData = taosArrayGet(p->pDataBlock, 6);
        colDataAppend(pColInfoData, numOfRows, (char*)&vgId, false);
1242

1243 1244 1245
        // table comment
        // todo: set the correct comment
        pColInfoData = taosArrayGet(p->pDataBlock, 8);
1246 1247
        colDataAppendNULL(pColInfoData, numOfRows);

1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
        char    str[256] = {0};
        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);
          colDataAppend(pColInfoData, numOfRows, (char*)&mr.me.stbEntry.schema.nCols, false);

          // super table name
          STR_TO_VARSTR(str, mr.me.name);
          pColInfoData = taosArrayGet(p->pDataBlock, 4);
          colDataAppend(pColInfoData, numOfRows, str, false);
          metaReaderClear(&mr);

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

          STR_TO_VARSTR(str, "CHILD_TABLE");
        } 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);
          colDataAppend(pColInfoData, numOfRows, (char*)&pInfo->pCur->mr.me.ntbEntry.schema.nCols, false);

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

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

          STR_TO_VARSTR(str, "NORMAL_TABLE");
        }
1302

1303 1304
        pColInfoData = taosArrayGet(p->pDataBlock, 9);
        colDataAppend(pColInfoData, numOfRows, str, false);
1305

1306
        if (++numOfRows >= pOperator->resultInfo.capacity) {
1307 1308
          break;
        }
H
Haojun Liao 已提交
1309 1310
      }

1311 1312
      p->info.rows = numOfRows;
      pInfo->pRes->info.rows = numOfRows;
H
Haojun Liao 已提交
1313

1314 1315
      relocateColumnData(pInfo->pRes, pInfo->scanCols, p->pDataBlock);
      doFilterResult(pInfo);
H
Haojun Liao 已提交
1316

1317 1318 1319
      pInfo->loadInfo.totalRows += pInfo->pRes->info.rows;
      return (pInfo->pRes->info.rows == 0) ? NULL : pInfo->pRes;
    }
H
Haojun Liao 已提交
1320 1321 1322 1323 1324
  } else {  // load the meta from mnode of the given epset
    if (pOperator->status == OP_EXEC_DONE) {
      return NULL;
    }

1325 1326 1327
    while (1) {
      int64_t startTs = taosGetTimestampUs();
      strncpy(pInfo->req.tb, tNameGetTableName(&pInfo->name), tListLen(pInfo->req.tb));
H
Haojun Liao 已提交
1328

1329 1330 1331 1332 1333
      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 已提交
1334

1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
      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 已提交
1346

1347 1348 1349 1350
      pMsgSendInfo->param = pOperator;
      pMsgSendInfo->msgInfo.pData = buf1;
      pMsgSendInfo->msgInfo.len = contLen;
      pMsgSendInfo->msgType = TDMT_MND_SYSTABLE_RETRIEVE;
1351
      pMsgSendInfo->fp = loadSysTableCallback;
H
Haojun Liao 已提交
1352

1353
      int64_t transporterId = 0;
1354 1355
      int32_t code =
          asyncSendMsgToServer(pInfo->readHandle.pMsgCb->clientRpc, &pInfo->epSet, &transporterId, pMsgSendInfo);
1356
      tsem_wait(&pInfo->ready);
H
Haojun Liao 已提交
1357

1358 1359 1360 1361 1362
      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 已提交
1363

1364 1365
      SRetrieveMetaTableRsp* pRsp = pInfo->pRsp;
      pInfo->req.showId = pRsp->handle;
H
Haojun Liao 已提交
1366

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

1372 1373 1374 1375
        if (pRsp->numOfRows == 0) {
          return NULL;
        }
      }
H
Haojun Liao 已提交
1376

1377 1378
      SRetrieveMetaTableRsp* pTableRsp = pInfo->pRsp;
      setSDataBlockFromFetchRsp(pInfo->pRes, &pInfo->loadInfo, pTableRsp->numOfRows, pTableRsp->data,
1379
                                pTableRsp->compLen, pOperator->numOfExprs, startTs, NULL, pInfo->scanCols);
H
Haojun Liao 已提交
1380

1381 1382 1383 1384 1385
      // todo log the filter info
      doFilterResult(pInfo);
      if (pInfo->pRes->info.rows > 0) {
        return pInfo->pRes;
      }
1386
    }
H
Haojun Liao 已提交
1387 1388 1389
  }
}

1390
int32_t buildSysDbTableInfo(const SSysTableScanInfo* pInfo, int32_t capacity) {
1391
  SSDataBlock* p = buildSysTableMetaBlock();
1392
  blockDataEnsureCapacity(p, capacity);
1393

L
Liu Jicong 已提交
1394
  size_t               size = 0;
1395 1396 1397 1398 1399 1400 1401 1402
  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);

L
Liu Jicong 已提交
1403 1404
  relocateColumnData(pInfo->pRes, pInfo->scanCols, p->pDataBlock);
  //  blockDataDestroy(p);  todo handle memory leak
1405 1406 1407 1408 1409

  pInfo->pRes->info.rows = p->info.rows;
  return p->info.rows;
}

L
Liu Jicong 已提交
1410 1411 1412
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};
1413 1414
  int32_t numOfRows = p->info.rows;

L
Liu Jicong 已提交
1415
  for (int32_t i = 0; i < size; ++i) {
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
    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 已提交
1436
    for (int32_t j = 4; j <= 8; ++j) {
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
      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;
}

1452
SOperatorInfo* createSysTableScanOperatorInfo(void* readHandle, SSDataBlock* pResBlock, const SName* pName,
H
Haojun Liao 已提交
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
                                              SNode* pCondition, SEpSet epset, SArray* colList,
                                              SExecTaskInfo* pTaskInfo, bool showRewrite, int32_t accountId) {
  SSysTableScanInfo* pInfo = taosMemoryCalloc(1, sizeof(SSysTableScanInfo));
  SOperatorInfo*     pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
  if (pInfo == NULL || pOperator == NULL) {
    taosMemoryFreeClear(pInfo);
    taosMemoryFreeClear(pOperator);
    terrno = TSDB_CODE_QRY_OUT_OF_MEMORY;
    return NULL;
  }

L
Liu Jicong 已提交
1464
  pInfo->accountId = accountId;
H
Haojun Liao 已提交
1465
  pInfo->showRewrite = showRewrite;
L
Liu Jicong 已提交
1466 1467 1468
  pInfo->pRes = pResBlock;
  pInfo->pCondition = pCondition;
  pInfo->scanCols = colList;
1469 1470

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

  tNameAssign(&pInfo->name, pName);
1473 1474
  const char* name = tNameGetTableName(&pInfo->name);
  if (strncasecmp(name, TSDB_INS_TABLE_USER_TABLES, TSDB_TABLE_FNAME_LEN) == 0) {
L
Liu Jicong 已提交
1475
    pInfo->readHandle = *(SReadHandle*)readHandle;
1476
    blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
H
Haojun Liao 已提交
1477 1478 1479
  } else {
    tsem_init(&pInfo->ready, 0, 0);
    pInfo->epSet = epset;
1480
    pInfo->readHandle = *(SReadHandle*)readHandle;
H
Haojun Liao 已提交
1481 1482
  }

L
Liu Jicong 已提交
1483
  pOperator->name = "SysTableScanOperator";
H
Haojun Liao 已提交
1484
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN;
L
Liu Jicong 已提交
1485 1486 1487 1488 1489 1490
  pOperator->blocking = false;
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
  pOperator->numOfExprs = pResBlock->info.numOfCols;
  pOperator->fpSet =
      createOperatorFpSet(operatorDummyOpenFn, doSysTableScan, NULL, NULL, destroySysScanOperator, NULL, NULL, NULL);
1491
  pOperator->pTaskInfo = pTaskInfo;
H
Haojun Liao 已提交
1492 1493 1494

  return pOperator;
}
H
Haojun Liao 已提交
1495

1496
static SSDataBlock* doTagScan(SOperatorInfo* pOperator) {
H
Haojun Liao 已提交
1497 1498 1499 1500
  if (pOperator->status == OP_EXEC_DONE) {
    return NULL;
  }

1501 1502 1503
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;

#if 0
H
Haojun Liao 已提交
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
  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);

  int32_t functionId = getExprFunctionId(&pOperator->pExpr[0]);
  if (functionId == FUNCTION_TID_TAG) { // return the tags & table Id
    assert(pQueryAttr->numOfOutput == 1);

    SExprInfo* pExprInfo = &pOperator->pExpr[0];
    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.
1576
#endif
H
Haojun Liao 已提交
1577

1578 1579 1580
  STagScanInfo* pInfo = pOperator->info;
  SExprInfo*    pExprInfo = &pOperator->pExpr[0];
  SSDataBlock*  pRes = pInfo->pRes;
H
Haojun Liao 已提交
1581

H
Haojun Liao 已提交
1582 1583 1584 1585 1586
  if (taosArrayGetSize(pInfo->pTableGroups->pGroupList) == 0) {
    setTaskStatus(pTaskInfo, TASK_COMPLETED);
    return NULL;
  }

1587
  SArray* pa = taosArrayGetP(pInfo->pTableGroups->pGroupList, 0);
H
Haojun Liao 已提交
1588

1589 1590 1591
  char        str[512] = {0};
  int32_t     count = 0;
  SMetaReader mr = {0};
1592
  metaReaderInit(&mr, pInfo->readHandle.meta, 0);
H
Haojun Liao 已提交
1593

1594 1595
  while (pInfo->curPos < pInfo->pTableGroups->numOfTables && count < pOperator->resultInfo.capacity) {
    STableKeyInfo* item = taosArrayGet(pa, pInfo->curPos);
1596
    metaGetTableEntryByUid(&mr, item->uid);
H
Haojun Liao 已提交
1597

1598 1599 1600 1601 1602 1603 1604
    for (int32_t j = 0; j < pOperator->numOfExprs; ++j) {
      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);
1605
      } else { // it is a tag value
wmmhello's avatar
wmmhello 已提交
1606
        if(pDst->info.type == TSDB_DATA_TYPE_JSON){
wmmhello's avatar
wmmhello 已提交
1607 1608 1609 1610 1611 1612 1613 1614
          const uint8_t *tmp = mr.me.ctbEntry.pTags;
          char *data = taosMemoryCalloc(kvRowLen(tmp) + 1, 1);
          if(data == NULL){
            qError("doTagScan calloc error:%d", kvRowLen(tmp) + 1);
            return NULL;
          }
          *data = TSDB_DATA_TYPE_JSON;
          memcpy(data+1, tmp, kvRowLen(tmp));
wmmhello's avatar
wmmhello 已提交
1615 1616
          colDataAppend(pDst, count, data, false);
          taosMemoryFree(data);
wmmhello's avatar
wmmhello 已提交
1617
        }else{
wmmhello's avatar
wmmhello 已提交
1618 1619
          const char* p = metaGetTableTagVal(&mr.me, pExprInfo[j].base.pParam[0].pCol->colId);
          colDataAppend(pDst, count, p, (p == NULL));
wmmhello's avatar
wmmhello 已提交
1620
        }
H
Haojun Liao 已提交
1621 1622 1623
      }
    }

1624
    count += 1;
1625
    if (++pInfo->curPos >= pInfo->pTableGroups->numOfTables) {
H
Haojun Liao 已提交
1626 1627 1628 1629
      pOperator->status = OP_EXEC_DONE;
    }
  }

1630 1631
  metaReaderClear(&mr);

1632
  // qDebug("QInfo:0x%"PRIx64" create tag values results completed, rows:%d", GET_TASKID(pRuntimeEnv), count);
H
Haojun Liao 已提交
1633
  if (pOperator->status == OP_EXEC_DONE) {
1634
    setTaskStatus(pTaskInfo, TASK_COMPLETED);
H
Haojun Liao 已提交
1635 1636 1637
  }

  pRes->info.rows = count;
1638
  return (pRes->info.rows == 0) ? NULL : pInfo->pRes;
H
Haojun Liao 已提交
1639 1640 1641 1642 1643 1644 1645
}

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

1646
SOperatorInfo* createTagScanOperatorInfo(SReadHandle* pReadHandle, SExprInfo* pExpr, int32_t numOfOutput,
L
Liu Jicong 已提交
1647 1648
                                         SSDataBlock* pResBlock, SArray* pColMatchInfo,
                                         STableGroupInfo* pTableGroupInfo, SExecTaskInfo* pTaskInfo) {
1649
  STagScanInfo*  pInfo = taosMemoryCalloc(1, sizeof(STagScanInfo));
H
Haojun Liao 已提交
1650 1651 1652 1653 1654
  SOperatorInfo* pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
  if (pInfo == NULL || pOperator == NULL) {
    goto _error;
  }

L
Liu Jicong 已提交
1655 1656 1657 1658 1659 1660
  pInfo->pTableGroups = pTableGroupInfo;
  pInfo->pColMatchInfo = pColMatchInfo;
  pInfo->pRes = pResBlock;
  pInfo->readHandle = *pReadHandle;
  pInfo->curPos = 0;
  pOperator->name = "TagScanOperator";
1661
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN;
L
Liu Jicong 已提交
1662 1663 1664 1665 1666 1667
  pOperator->blocking = false;
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
  pOperator->pExpr = pExpr;
  pOperator->numOfExprs = numOfOutput;
  pOperator->pTaskInfo = pTaskInfo;
1668

1669 1670 1671
  initResultSizeInfo(pOperator, 4096);
  blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);

1672 1673
  pOperator->fpSet =
      createOperatorFpSet(operatorDummyOpenFn, doTagScan, NULL, NULL, destroyTagScanOperatorInfo, NULL, NULL, NULL);
H
Haojun Liao 已提交
1674 1675

  return pOperator;
1676

1677
_error:
H
Haojun Liao 已提交
1678 1679 1680 1681 1682
  taosMemoryFree(pInfo);
  taosMemoryFree(pOperator);
  terrno = TSDB_CODE_OUT_OF_MEMORY;
  return NULL;
}