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

#include "executor.h"
17 18
#include "executorInt.h"
#include "operator.h"
19
#include "planner.h"
20
#include "querytask.h"
L
Liu Jicong 已提交
21
#include "tdatablock.h"
L
Liu Jicong 已提交
22
#include "tref.h"
23
#include "tudf.h"
L
Liu Jicong 已提交
24
#include "vnode.h"
25 26 27 28 29 30 31 32

static TdThreadOnce initPoolOnce = PTHREAD_ONCE_INIT;
int32_t             exchangeObjRefPool = -1;

static void cleanupRefPool() {
  int32_t ref = atomic_val_compare_exchange_32(&exchangeObjRefPool, exchangeObjRefPool, 0);
  taosCloseRef(ref);
}
33

X
Xiaoyu Wang 已提交
34 35
static void initRefPool() {
  exchangeObjRefPool = taosOpenRef(1024, doDestroyExchangeOperatorInfo);
D
dapan1121 已提交
36 37 38
  atexit(cleanupRefPool);
}

L
Liu Jicong 已提交
39 40 41 42
static int32_t doSetSMABlock(SOperatorInfo* pOperator, void* input, size_t numOfBlocks, int32_t type, char* id) {
  if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
    if (pOperator->numOfDownstream == 0) {
      qError("failed to find stream scan operator to set the input data block, %s" PRIx64, id);
S
Shengliang Guan 已提交
43
      return TSDB_CODE_APP_ERROR;
L
Liu Jicong 已提交
44 45 46 47
    }

    if (pOperator->numOfDownstream > 1) {  // not handle this in join query
      qError("join not supported for stream block scan, %s" PRIx64, id);
S
Shengliang Guan 已提交
48
      return TSDB_CODE_APP_ERROR;
L
Liu Jicong 已提交
49 50 51 52 53 54 55 56 57 58
    }
    pOperator->status = OP_NOT_OPENED;
    return doSetSMABlock(pOperator->pDownstream[0], input, numOfBlocks, type, id);
  } else {
    pOperator->status = OP_NOT_OPENED;

    SStreamScanInfo* pInfo = pOperator->info;

    if (type == STREAM_INPUT__MERGED_SUBMIT) {
      for (int32_t i = 0; i < numOfBlocks; i++) {
K
kailixu 已提交
59 60
        SPackedData* pReq = POINTER_SHIFT(input, i * sizeof(SPackedData));
        taosArrayPush(pInfo->pBlockLists, pReq);
L
Liu Jicong 已提交
61 62 63 64 65 66 67 68
      }
      pInfo->blockType = STREAM_INPUT__DATA_SUBMIT;
    } else if (type == STREAM_INPUT__DATA_SUBMIT) {
      taosArrayPush(pInfo->pBlockLists, &input);
      pInfo->blockType = STREAM_INPUT__DATA_SUBMIT;
    } else if (type == STREAM_INPUT__DATA_BLOCK) {
      for (int32_t i = 0; i < numOfBlocks; ++i) {
        SSDataBlock* pDataBlock = &((SSDataBlock*)input)[i];
K
kailixu 已提交
69 70 71 72
        SPackedData  tmp = {
             .pDataBlock = pDataBlock,
        };
        taosArrayPush(pInfo->pBlockLists, &tmp);
L
Liu Jicong 已提交
73 74 75 76 77 78 79 80
      }
      pInfo->blockType = STREAM_INPUT__DATA_BLOCK;
    }

    return TSDB_CODE_SUCCESS;
  }
}

L
Liu Jicong 已提交
81
static int32_t doSetStreamOpOpen(SOperatorInfo* pOperator, char* id) {
H
Haojun Liao 已提交
82 83 84 85 86
  if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
    if (pOperator->numOfDownstream == 0) {
      qError("failed to find stream scan operator to set the input data block, %s" PRIx64, id);
      return TSDB_CODE_APP_ERROR;
    }
L
Liu Jicong 已提交
87

H
Haojun Liao 已提交
88 89 90
    if (pOperator->numOfDownstream > 1) {  // not handle this in join query
      qError("join not supported for stream block scan, %s" PRIx64, id);
      return TSDB_CODE_APP_ERROR;
L
Liu Jicong 已提交
91
    }
H
Haojun Liao 已提交
92 93
    pOperator->status = OP_NOT_OPENED;
    return doSetStreamOpOpen(pOperator->pDownstream[0], id);
L
Liu Jicong 已提交
94 95 96 97
  }
  return 0;
}

98 99 100 101 102 103 104 105 106 107 108
static void clearStreamBlock(SOperatorInfo* pOperator) {
  if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
    if (pOperator->numOfDownstream == 1) {
      return clearStreamBlock(pOperator->pDownstream[0]);
    }
  } else {
    SStreamScanInfo* pInfo = pOperator->info;
    doClearBufferedBlocks(pInfo);
  }
}

5
54liuyao 已提交
109 110 111 112 113 114
void resetTaskInfo(qTaskInfo_t tinfo) {
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
  pTaskInfo->code = 0;
  clearStreamBlock(pTaskInfo->pRoot);
}

L
Liu Jicong 已提交
115
static int32_t doSetStreamBlock(SOperatorInfo* pOperator, void* input, size_t numOfBlocks, int32_t type, char* id) {
X
Xiaoyu Wang 已提交
116
  if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
H
Haojun Liao 已提交
117
    if (pOperator->numOfDownstream == 0) {
118
      qError("failed to find stream scan operator to set the input data block, %s" PRIx64, id);
S
Shengliang Guan 已提交
119
      return TSDB_CODE_APP_ERROR;
H
Haojun Liao 已提交
120
    }
H
Haojun Liao 已提交
121

H
Haojun Liao 已提交
122
    if (pOperator->numOfDownstream > 1) {  // not handle this in join query
123
      qError("join not supported for stream block scan, %s" PRIx64, id);
S
Shengliang Guan 已提交
124
      return TSDB_CODE_APP_ERROR;
H
Haojun Liao 已提交
125
    }
L
Liu Jicong 已提交
126
    pOperator->status = OP_NOT_OPENED;
L
Liu Jicong 已提交
127
    return doSetStreamBlock(pOperator->pDownstream[0], input, numOfBlocks, type, id);
H
Haojun Liao 已提交
128
  } else {
129 130
    pOperator->status = OP_NOT_OPENED;

131
    SStreamScanInfo* pInfo = pOperator->info;
132
    qDebug("s-task set source blocks:%d %s", (int32_t)numOfBlocks, id);
133
    ASSERT(pInfo->validBlockIndex == 0 && taosArrayGetSize(pInfo->pBlockLists) == 0);
134

L
Liu Jicong 已提交
135 136
    if (type == STREAM_INPUT__MERGED_SUBMIT) {
      for (int32_t i = 0; i < numOfBlocks; i++) {
L
Liu Jicong 已提交
137
        SPackedData* pReq = POINTER_SHIFT(input, i * sizeof(SPackedData));
L
Liu Jicong 已提交
138
        taosArrayPush(pInfo->pBlockLists, pReq);
139
      }
L
Liu Jicong 已提交
140 141
      pInfo->blockType = STREAM_INPUT__DATA_SUBMIT;
    } else if (type == STREAM_INPUT__DATA_SUBMIT) {
L
Liu Jicong 已提交
142
      taosArrayPush(pInfo->pBlockLists, input);
L
Liu Jicong 已提交
143
      pInfo->blockType = STREAM_INPUT__DATA_SUBMIT;
L
Liu Jicong 已提交
144
    } else if (type == STREAM_INPUT__DATA_BLOCK) {
H
Haojun Liao 已提交
145
      for (int32_t i = 0; i < numOfBlocks; ++i) {
L
Liu Jicong 已提交
146
        SSDataBlock* pDataBlock = &((SSDataBlock*)input)[i];
L
Liu Jicong 已提交
147 148
        SPackedData  tmp = {
             .pDataBlock = pDataBlock,
L
Liu Jicong 已提交
149 150
        };
        taosArrayPush(pInfo->pBlockLists, &tmp);
H
Haojun Liao 已提交
151
      }
L
Liu Jicong 已提交
152
      pInfo->blockType = STREAM_INPUT__DATA_BLOCK;
153 154
    } else {
      ASSERT(0);
155 156
    }

H
Haojun Liao 已提交
157 158 159 160
    return TSDB_CODE_SUCCESS;
  }
}

161 162 163 164 165 166 167 168 169 170 171 172 173
void doSetTaskId(SOperatorInfo* pOperator) {
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
    SStreamScanInfo* pStreamScanInfo = pOperator->info;
    STableScanInfo*  pScanInfo = pStreamScanInfo->pTableScanOp->info;
    if (pScanInfo->base.dataReader != NULL) {
      tsdbReaderSetId(pScanInfo->base.dataReader, pTaskInfo->id.str);
    }
  } else {
    doSetTaskId(pOperator->pDownstream[0]);
  }
}

H
Haojun Liao 已提交
174 175 176
void qSetTaskId(qTaskInfo_t tinfo, uint64_t taskId, uint64_t queryId) {
  SExecTaskInfo* pTaskInfo = tinfo;
  pTaskInfo->id.queryId = queryId;
H
Haojun Liao 已提交
177
  buildTaskId(taskId, queryId, pTaskInfo->id.str);
178 179 180

  // set the idstr for tsdbReader
  doSetTaskId(pTaskInfo->pRoot);
H
Haojun Liao 已提交
181 182
}

wmmhello's avatar
wmmhello 已提交
183 184 185 186
//void qSetTaskCode(qTaskInfo_t tinfo, int32_t code) {
//  SExecTaskInfo* pTaskInfo = tinfo;
//  pTaskInfo->code = code;
//}
187

L
Liu Jicong 已提交
188 189
int32_t qSetStreamOpOpen(qTaskInfo_t tinfo) {
  if (tinfo == NULL) {
S
Shengliang Guan 已提交
190
    return TSDB_CODE_APP_ERROR;
L
Liu Jicong 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203
  }

  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;

  int32_t code = doSetStreamOpOpen(pTaskInfo->pRoot, GET_TASKID(pTaskInfo));
  if (code != TSDB_CODE_SUCCESS) {
    qError("%s failed to set the stream block data", GET_TASKID(pTaskInfo));
  } else {
    qDebug("%s set the stream block successfully", GET_TASKID(pTaskInfo));
  }

  return code;
}
204

L
liuyao 已提交
205
void qGetCheckpointVersion(qTaskInfo_t tinfo, int64_t* dataVer, int64_t* ckId) {
206
  SExecTaskInfo* pTaskInfo = tinfo;
L
liuyao 已提交
207 208
  *dataVer = pTaskInfo->streamInfo.dataVersion;
  *ckId = pTaskInfo->streamInfo.checkPointId;
209 210 211
}


L
Liu Jicong 已提交
212
int32_t qSetMultiStreamInput(qTaskInfo_t tinfo, const void* pBlocks, size_t numOfBlocks, int32_t type) {
H
Haojun Liao 已提交
213
  if (tinfo == NULL) {
S
Shengliang Guan 已提交
214
    return TSDB_CODE_APP_ERROR;
H
Haojun Liao 已提交
215 216
  }

H
Haojun Liao 已提交
217
  if (pBlocks == NULL || numOfBlocks == 0) {
H
Haojun Liao 已提交
218 219 220
    return TSDB_CODE_SUCCESS;
  }

L
Liu Jicong 已提交
221
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
H
Haojun Liao 已提交
222

C
Cary Xu 已提交
223
  int32_t code = doSetStreamBlock(pTaskInfo->pRoot, (void*)pBlocks, numOfBlocks, type, GET_TASKID(pTaskInfo));
H
Haojun Liao 已提交
224
  if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
225
    qError("%s failed to set the stream block data", GET_TASKID(pTaskInfo));
H
Haojun Liao 已提交
226
  } else {
H
Haojun Liao 已提交
227
    qDebug("%s set the stream block successfully", GET_TASKID(pTaskInfo));
H
Haojun Liao 已提交
228 229 230 231 232
  }

  return code;
}

L
Liu Jicong 已提交
233 234
int32_t qSetSMAInput(qTaskInfo_t tinfo, const void* pBlocks, size_t numOfBlocks, int32_t type) {
  if (tinfo == NULL) {
S
Shengliang Guan 已提交
235
    return TSDB_CODE_APP_ERROR;
L
Liu Jicong 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
  }

  if (pBlocks == NULL || numOfBlocks == 0) {
    return TSDB_CODE_SUCCESS;
  }

  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;

  int32_t code = doSetSMABlock(pTaskInfo->pRoot, (void*)pBlocks, numOfBlocks, type, GET_TASKID(pTaskInfo));
  if (code != TSDB_CODE_SUCCESS) {
    qError("%s failed to set the sma block data", GET_TASKID(pTaskInfo));
  } else {
    qDebug("%s set the sma block successfully", GET_TASKID(pTaskInfo));
  }

  return code;
}

X
Xiaoyu Wang 已提交
254 255 256
qTaskInfo_t qCreateQueueExecTaskInfo(void* msg, SReadHandle* pReaderHandle, int32_t vgId, int32_t* numOfCols,
                                     uint64_t id) {
  if (msg == NULL) {  // create raw scan
H
Haojun Liao 已提交
257
    SExecTaskInfo* pTaskInfo = doCreateTask(0, id, vgId, OPTR_EXEC_MODEL_QUEUE);
258 259 260 261
    if (NULL == pTaskInfo) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return NULL;
    }
262
    pTaskInfo->pRoot = createRawScanOperatorInfo(pReaderHandle, pTaskInfo);
L
Liu Jicong 已提交
263
    if (NULL == pTaskInfo->pRoot) {
264 265 266 267
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      taosMemoryFree(pTaskInfo);
      return NULL;
    }
268 269

    qDebug("create raw scan task info completed, vgId:%d, %s", vgId, GET_TASKID(pTaskInfo));
270
    return pTaskInfo;
L
Liu Jicong 已提交
271 272
  }

H
Haojun Liao 已提交
273
  struct SSubplan* pPlan = NULL;
274 275

  int32_t code = qStringToSubplan(msg, &pPlan);
L
Liu Jicong 已提交
276 277 278 279 280 281
  if (code != TSDB_CODE_SUCCESS) {
    terrno = code;
    return NULL;
  }

  qTaskInfo_t pTaskInfo = NULL;
282
  code = qCreateExecTask(pReaderHandle, vgId, 0, pPlan, &pTaskInfo, NULL, NULL, OPTR_EXEC_MODEL_QUEUE);
L
Liu Jicong 已提交
283
  if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
284 285
    nodesDestroyNode((SNode*)pPlan);
    qDestroyTask(pTaskInfo);
L
Liu Jicong 已提交
286 287 288 289
    terrno = code;
    return NULL;
  }

290
  // extract the number of output columns
H
Haojun Liao 已提交
291
  SDataBlockDescNode* pDescNode = pPlan->pNode->pOutputDataBlockDesc;
wmmhello's avatar
wmmhello 已提交
292
  *numOfCols = 0;
293

L
Liu Jicong 已提交
294
  SNode* pNode;
295 296 297
  FOREACH(pNode, pDescNode->pSlots) {
    SSlotDescNode* pSlotDesc = (SSlotDescNode*)pNode;
    if (pSlotDesc->output) {
wmmhello's avatar
wmmhello 已提交
298
      ++(*numOfCols);
299 300 301
    }
  }

L
Liu Jicong 已提交
302 303 304
  return pTaskInfo;
}

305
qTaskInfo_t qCreateStreamExecTaskInfo(void* msg, SReadHandle* readers, int32_t vgId) {
L
Liu Jicong 已提交
306
  if (msg == NULL) {
307 308 309
    return NULL;
  }

H
Haojun Liao 已提交
310 311
  struct SSubplan* pPlan = NULL;
  int32_t          code = qStringToSubplan(msg, &pPlan);
312 313 314 315 316 317
  if (code != TSDB_CODE_SUCCESS) {
    terrno = code;
    return NULL;
  }

  qTaskInfo_t pTaskInfo = NULL;
318
  code = qCreateExecTask(readers, vgId, 0, pPlan, &pTaskInfo, NULL, NULL, OPTR_EXEC_MODEL_STREAM);
319
  if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
320
    qDestroyTask(pTaskInfo);
321 322 323 324 325 326
    terrno = code;
    return NULL;
  }

  return pTaskInfo;
}
327

328
static SArray* filterUnqualifiedTables(const SStreamScanInfo* pScanInfo, const SArray* tableIdList, const char* idstr) {
329
  SArray* qa = taosArrayInit(4, sizeof(tb_uid_t));
H
Haojun Liao 已提交
330 331 332 333
  int32_t numOfUids = taosArrayGetSize(tableIdList);
  if (numOfUids == 0) {
    return qa;
  }
334

335 336
  STableScanInfo* pTableScanInfo = pScanInfo->pTableScanOp->info;

337
  uint64_t suid = 0;
338
  uint64_t uid = 0;
339
  int32_t type = 0;
340
  tableListGetSourceTableInfo(pTableScanInfo->base.pTableListInfo, &suid, &uid, &type);
341

342 343 344
  // let's discard the tables those are not created according to the queried super table.
  SMetaReader mr = {0};
  metaReaderInit(&mr, pScanInfo->readHandle.meta, 0);
H
Haojun Liao 已提交
345
  for (int32_t i = 0; i < numOfUids; ++i) {
346
    uint64_t* id = (uint64_t*)taosArrayGet(tableIdList, i);
347

348
    int32_t code = metaGetTableEntryByUid(&mr, *id);
349
    if (code != TSDB_CODE_SUCCESS) {
350
      qError("failed to get table meta, uid:%" PRIu64 " code:%s, %s", *id, tstrerror(terrno), idstr);
351 352 353
      continue;
    }

M
Minglei Jin 已提交
354 355
    tDecoderClear(&mr.coder);

356
    if (mr.me.type == TSDB_SUPER_TABLE) {
357
      continue;
358 359 360
    } else {
      if (type == TSDB_SUPER_TABLE) {
        // this new created child table does not belong to the scanned super table.
361
        if (mr.me.type != TSDB_CHILD_TABLE || mr.me.ctbEntry.suid != suid) {
362 363 364 365 366
          continue;
        }
      } else {  // ordinary table
        // In case that the scanned target table is an ordinary table. When replay the WAL during restore the vnode, we
        // should check all newly created ordinary table to make sure that this table isn't the destination table.
367
        if (mr.me.uid != uid) {
368 369 370
          continue;
        }
      }
371
    }
372 373 374

    if (pScanInfo->pTagCond != NULL) {
      bool          qualified = false;
375
      STableKeyInfo info = {.groupId = 0, .uid = mr.me.uid};
H
Haojun Liao 已提交
376
      code = isQualifiedTable(&info, pScanInfo->pTagCond, pScanInfo->readHandle.meta, &qualified);
377 378 379 380 381 382 383 384 385 386
      if (code != TSDB_CODE_SUCCESS) {
        qError("failed to filter new table, uid:0x%" PRIx64 ", %s", info.uid, idstr);
        continue;
      }

      if (!qualified) {
        continue;
      }
    }

387
    // handle multiple partition
388 389 390 391 392 393 394
    taosArrayPush(qa, id);
  }

  metaReaderClear(&mr);
  return qa;
}

395
int32_t qUpdateTableListForStreamScanner(qTaskInfo_t tinfo, const SArray* tableIdList, bool isAdd) {
dengyihao's avatar
dengyihao 已提交
396
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
397 398
  const char*    id = GET_TASKID(pTaskInfo);
  int32_t        code = 0;
H
Haojun Liao 已提交
399 400

  if (isAdd) {
401
    qDebug("add %d tables id into query list, %s", (int32_t)taosArrayGetSize(tableIdList), id);
H
Haojun Liao 已提交
402 403
  }

404
  // traverse to the stream scanner node to add this table id
405
  SOperatorInfo*   pInfo = extractOperatorInTree(pTaskInfo->pRoot, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id);
406
  SStreamScanInfo* pScanInfo = pInfo->info;
407

408
  if (isAdd) {  // add new table id
409
    SArray* qa = filterUnqualifiedTables(pScanInfo, tableIdList, id);
410
    int32_t numOfQualifiedTables = taosArrayGetSize(qa);
411
    qDebug("%d qualified child tables added into stream scanner, %s", numOfQualifiedTables, id);
L
Liu Jicong 已提交
412
    code = tqReaderAddTbUidList(pScanInfo->tqReader, qa);
413
    if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
414
      taosArrayDestroy(qa);
415 416 417
      return code;
    }

M
Minglei Jin 已提交
418
    bool   assignUid = false;
L
Liu Jicong 已提交
419 420
    size_t bufLen = (pScanInfo->pGroupTags != NULL) ? getTableTagsBufLen(pScanInfo->pGroupTags) : 0;
    char*  keyBuf = NULL;
421
    if (bufLen > 0) {
422
      assignUid = groupbyTbname(pScanInfo->pGroupTags);
423 424
      keyBuf = taosMemoryMalloc(bufLen);
      if (keyBuf == NULL) {
H
Haojun Liao 已提交
425
        taosArrayDestroy(qa);
426 427 428
        return TSDB_CODE_OUT_OF_MEMORY;
      }
    }
429

430
    STableListInfo* pTableListInfo = ((STableScanInfo*)pScanInfo->pTableScanOp->info)->base.pTableListInfo;
431
    taosWLockLatch(&pTaskInfo->lock);
432 433

    for (int32_t i = 0; i < numOfQualifiedTables; ++i) {
434
      uint64_t*     uid = taosArrayGet(qa, i);
435
      STableKeyInfo keyInfo = {.uid = *uid, .groupId = 0};
436 437

      if (bufLen > 0) {
438 439 440 441 442 443
        if (assignUid) {
          keyInfo.groupId = keyInfo.uid;
        } else {
          code = getGroupIdFromTagsVal(pScanInfo->readHandle.meta, keyInfo.uid, pScanInfo->pGroupTags, keyBuf,
                                       &keyInfo.groupId);
          if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
444
            taosMemoryFree(keyBuf);
H
Haojun Liao 已提交
445
            taosArrayDestroy(qa);
446
            taosWUnLockLatch(&pTaskInfo->lock);
447 448
            return code;
          }
449 450 451
        }
      }

H
Haojun Liao 已提交
452
      tableListAddTableInfo(pTableListInfo, keyInfo.uid, keyInfo.groupId);
453 454
    }

455
    taosWUnLockLatch(&pTaskInfo->lock);
456 457 458 459
    if (keyBuf != NULL) {
      taosMemoryFree(keyBuf);
    }

460 461
    taosArrayDestroy(qa);
  } else {  // remove the table id in current list
462
    qDebug("%d remove child tables from the stream scanner, %s", (int32_t)taosArrayGetSize(tableIdList), id);
463
    taosWLockLatch(&pTaskInfo->lock);
L
Liu Jicong 已提交
464
    code = tqReaderRemoveTbUidList(pScanInfo->tqReader, tableIdList);
465
    taosWUnLockLatch(&pTaskInfo->lock);
466 467
  }

468
  return code;
L
fix  
Liu Jicong 已提交
469
}
470

471
int32_t qGetQueryTableSchemaVersion(qTaskInfo_t tinfo, char* dbName, char* tableName, int32_t* sversion,
L
Liu Jicong 已提交
472
                                    int32_t* tversion) {
473
  ASSERT(tinfo != NULL && dbName != NULL && tableName != NULL);
474
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
475

476
  if (pTaskInfo->schemaInfo.sw == NULL) {
H
Haojun Liao 已提交
477 478 479
    return TSDB_CODE_SUCCESS;
  }

480 481 482 483
  *sversion = pTaskInfo->schemaInfo.sw->version;
  *tversion = pTaskInfo->schemaInfo.tversion;
  if (pTaskInfo->schemaInfo.dbname) {
    strcpy(dbName, pTaskInfo->schemaInfo.dbname);
484 485 486
  } else {
    dbName[0] = 0;
  }
487 488
  if (pTaskInfo->schemaInfo.tablename) {
    strcpy(tableName, pTaskInfo->schemaInfo.tablename);
489 490 491
  } else {
    tableName[0] = 0;
  }
492 493

  return 0;
494
}
495 496

int32_t qCreateExecTask(SReadHandle* readHandle, int32_t vgId, uint64_t taskId, SSubplan* pSubplan,
D
dapan1121 已提交
497
                        qTaskInfo_t* pTaskInfo, DataSinkHandle* handle, char* sql, EOPTR_EXEC_MODEL model) {
498 499 500
  SExecTaskInfo** pTask = (SExecTaskInfo**)pTaskInfo;
  taosThreadOnce(&initPoolOnce, initRefPool);

501
  qDebug("start to create task, TID:0x%" PRIx64 " QID:0x%" PRIx64 ", vgId:%d", taskId, pSubplan->id.queryId, vgId);
502

503
  int32_t code = createExecTaskInfo(pSubplan, pTask, readHandle, taskId, vgId, sql, model);
504
  if (code != TSDB_CODE_SUCCESS) {
505
    qError("failed to createExecTaskInfo, code: %s", tstrerror(code));
506 507 508
    goto _error;
  }

509
  SDataSinkMgtCfg cfg = {.maxDataBlockNum = 500, .maxDataBlockNumPerQuery = 50};
510 511
  code = dsDataSinkMgtInit(&cfg);
  if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
512
    qError("failed to dsDataSinkMgtInit, code:%s, %s", tstrerror(code), (*pTask)->id.str);
513 514 515 516 517
    goto _error;
  }

  if (handle) {
    void* pSinkParam = NULL;
518
    code = createDataSinkParam(pSubplan->pDataSink, &pSinkParam, (*pTask), readHandle);
519
    if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
520
      qError("failed to createDataSinkParam, vgId:%d, code:%s, %s", vgId, tstrerror(code), (*pTask)->id.str);
521 522 523
      goto _error;
    }

H
Haojun Liao 已提交
524
    // pSinkParam has been freed during create sinker.
H
Haojun Liao 已提交
525
    code = dsCreateDataSinker(pSubplan->pDataSink, handle, pSinkParam, (*pTask)->id.str);
526 527
  }

528
  qDebug("subplan task create completed, TID:0x%" PRIx64 " QID:0x%" PRIx64, taskId, pSubplan->id.queryId);
529

dengyihao's avatar
dengyihao 已提交
530
_error:
531 532 533 534
  // if failed to add ref for all tables in this query, abort current query
  return code;
}

H
Haojun Liao 已提交
535
static void freeBlock(void* param) {
536
  SSDataBlock* pBlock = *(SSDataBlock**)param;
H
Haojun Liao 已提交
537 538 539
  blockDataDestroy(pBlock);
}

540
int32_t qExecTaskOpt(qTaskInfo_t tinfo, SArray* pResList, uint64_t* useconds, bool* hasMore, SLocalFetch* pLocal) {
541 542 543
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
  int64_t        threadId = taosGetSelfPthreadId();

D
dapan1121 已提交
544
  if (pLocal) {
545
    memcpy(&pTaskInfo->localFetch, pLocal, sizeof(*pLocal));
D
dapan1121 已提交
546
  }
L
Liu Jicong 已提交
547

H
Haojun Liao 已提交
548
  taosArrayClear(pResList);
H
Haojun Liao 已提交
549

550 551 552 553 554 555 556 557
  int64_t curOwner = 0;
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
    return pTaskInfo->code;
  }

  if (pTaskInfo->cost.start == 0) {
558
    pTaskInfo->cost.start = taosGetTimestampUs();
559 560 561
  }

  if (isTaskKilled(pTaskInfo)) {
562
    atomic_store_64(&pTaskInfo->owner, 0);
563 564 565 566 567 568 569 570 571
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
    return TSDB_CODE_SUCCESS;
  }

  // error occurs, record the error code and return to client
  int32_t ret = setjmp(pTaskInfo->env);
  if (ret != TSDB_CODE_SUCCESS) {
    pTaskInfo->code = ret;
    cleanUpUdfs();
H
Haojun Liao 已提交
572

573 574 575 576 577 578 579 580
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
    atomic_store_64(&pTaskInfo->owner, 0);

    return pTaskInfo->code;
  }

  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));

581
  int32_t      current = 0;
H
Haojun Liao 已提交
582 583
  SSDataBlock* pRes = NULL;

584 585
  int64_t st = taosGetTimestampUs();

H
Haojun Liao 已提交
586
  int32_t blockIndex = 0;
587
  while ((pRes = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot)) != NULL) {
H
Haojun Liao 已提交
588 589 590 591 592 593
    SSDataBlock* p = NULL;
    if (blockIndex >= taosArrayGetSize(pTaskInfo->pResultBlockList)) {
      SSDataBlock* p1 = createOneDataBlock(pRes, true);
      taosArrayPush(pTaskInfo->pResultBlockList, &p1);
      p = p1;
    } else {
L
Liu Jicong 已提交
594
      p = *(SSDataBlock**)taosArrayGet(pTaskInfo->pResultBlockList, blockIndex);
H
Haojun Liao 已提交
595 596 597 598 599
      copyDataBlock(p, pRes);
    }

    blockIndex += 1;

H
Haojun Liao 已提交
600 601 602 603 604 605 606 607 608
    current += p->info.rows;
    ASSERT(p->info.rows > 0);
    taosArrayPush(pResList, &p);

    if (current >= 4096) {
      break;
    }
  }

609
  *hasMore = (pRes != NULL);
610 611 612
  uint64_t el = (taosGetTimestampUs() - st);

  pTaskInfo->cost.elapsedTime += el;
H
Haojun Liao 已提交
613
  if (NULL == pRes) {
614 615 616 617 618
    *useconds = pTaskInfo->cost.elapsedTime;
  }

  cleanUpUdfs();

H
Haojun Liao 已提交
619
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;
H
Haojun Liao 已提交
620
  qDebug("%s task suspended, %d rows in %d blocks returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
621
         GET_TASKID(pTaskInfo), current, (int32_t)taosArrayGetSize(pResList), total, 0, el / 1000.0);
622 623 624 625 626

  atomic_store_64(&pTaskInfo->owner, 0);
  return pTaskInfo->code;
}

H
Haojun Liao 已提交
627 628
void qCleanExecTaskBlockBuf(qTaskInfo_t tinfo) {
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
L
Liu Jicong 已提交
629 630 631
  SArray*        pList = pTaskInfo->pResultBlockList;
  size_t         num = taosArrayGetSize(pList);
  for (int32_t i = 0; i < num; ++i) {
H
Haojun Liao 已提交
632 633 634 635 636 637 638
    SSDataBlock** p = taosArrayGet(pTaskInfo->pResultBlockList, i);
    blockDataDestroy(*p);
  }

  taosArrayClear(pTaskInfo->pResultBlockList);
}

639 640 641 642 643 644 645 646 647 648 649 650 651
int32_t qExecTask(qTaskInfo_t tinfo, SSDataBlock** pRes, uint64_t* useconds) {
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
  int64_t        threadId = taosGetSelfPthreadId();

  *pRes = NULL;
  int64_t curOwner = 0;
  if ((curOwner = atomic_val_compare_exchange_64(&pTaskInfo->owner, 0, threadId)) != 0) {
    qError("%s-%p execTask is now executed by thread:%p", GET_TASKID(pTaskInfo), pTaskInfo, (void*)curOwner);
    pTaskInfo->code = TSDB_CODE_QRY_IN_EXEC;
    return pTaskInfo->code;
  }

  if (pTaskInfo->cost.start == 0) {
652
    pTaskInfo->cost.start = taosGetTimestampUs();
653 654
  }

5
54liuyao 已提交
655
  if (isTaskKilled(pTaskInfo)) {
656
    clearStreamBlock(pTaskInfo->pRoot);
657
    atomic_store_64(&pTaskInfo->owner, 0);
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
    qDebug("%s already killed, abort", GET_TASKID(pTaskInfo));
    return TSDB_CODE_SUCCESS;
  }

  // error occurs, record the error code and return to client
  int32_t ret = setjmp(pTaskInfo->env);
  if (ret != TSDB_CODE_SUCCESS) {
    pTaskInfo->code = ret;
    cleanUpUdfs();
    qDebug("%s task abort due to error/cancel occurs, code:%s", GET_TASKID(pTaskInfo), tstrerror(pTaskInfo->code));
    atomic_store_64(&pTaskInfo->owner, 0);
    return pTaskInfo->code;
  }

  qDebug("%s execTask is launched", GET_TASKID(pTaskInfo));

  int64_t st = taosGetTimestampUs();

  *pRes = pTaskInfo->pRoot->fpSet.getNextFn(pTaskInfo->pRoot);
  uint64_t el = (taosGetTimestampUs() - st);

  pTaskInfo->cost.elapsedTime += el;
  if (NULL == *pRes) {
    *useconds = pTaskInfo->cost.elapsedTime;
  }

  cleanUpUdfs();

  int32_t  current = (*pRes != NULL) ? (*pRes)->info.rows : 0;
  uint64_t total = pTaskInfo->pRoot->resultInfo.totalRows;

  qDebug("%s task suspended, %d rows returned, total:%" PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms",
         GET_TASKID(pTaskInfo), current, total, 0, el / 1000.0);

  atomic_store_64(&pTaskInfo->owner, 0);
  return pTaskInfo->code;
}

L
Liu Jicong 已提交
696
int32_t qAppendTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
D
dapan1121 已提交
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
  taosArrayPush(pTaskInfo->stopInfo.pStopInfo, pInfo);
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);

  return TSDB_CODE_SUCCESS;
}

int32_t stopInfoComp(void const* lp, void const* rp) {
  SExchangeOpStopInfo* key = (SExchangeOpStopInfo*)lp;
  SExchangeOpStopInfo* pInfo = (SExchangeOpStopInfo*)rp;

  if (key->refId < pInfo->refId) {
    return -1;
  } else if (key->refId > pInfo->refId) {
    return 1;
  }

  return 0;
}

L
Liu Jicong 已提交
717
void qRemoveTaskStopInfo(SExecTaskInfo* pTaskInfo, SExchangeOpStopInfo* pInfo) {
D
dapan1121 已提交
718 719 720 721 722 723 724 725 726 727 728 729 730
  taosWLockLatch(&pTaskInfo->stopInfo.lock);
  int32_t idx = taosArraySearchIdx(pTaskInfo->stopInfo.pStopInfo, pInfo, stopInfoComp, TD_EQ);
  if (idx >= 0) {
    taosArrayRemove(pTaskInfo->stopInfo.pStopInfo, idx);
  }
  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
}

void qStopTaskOperators(SExecTaskInfo* pTaskInfo) {
  taosWLockLatch(&pTaskInfo->stopInfo.lock);

  int32_t num = taosArrayGetSize(pTaskInfo->stopInfo.pStopInfo);
  for (int32_t i = 0; i < num; ++i) {
L
Liu Jicong 已提交
731 732
    SExchangeOpStopInfo* pStop = taosArrayGet(pTaskInfo->stopInfo.pStopInfo, i);
    SExchangeInfo*       pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pStop->refId);
D
dapan1121 已提交
733 734 735 736 737 738 739 740 741
    if (pExchangeInfo) {
      tsem_post(&pExchangeInfo->ready);
      taosReleaseRef(exchangeObjRefPool, pStop->refId);
    }
  }

  taosWUnLockLatch(&pTaskInfo->stopInfo.lock);
}

D
dapan1121 已提交
742
int32_t qAsyncKillTask(qTaskInfo_t qinfo, int32_t rspCode) {
743 744 745 746 747 748
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
  if (pTaskInfo == NULL) {
    return TSDB_CODE_QRY_INVALID_QHANDLE;
  }

  qDebug("%s execTask async killed", GET_TASKID(pTaskInfo));
L
Liu Jicong 已提交
749

D
dapan1121 已提交
750
  setTaskKilled(pTaskInfo, rspCode);
D
dapan1121 已提交
751
  qStopTaskOperators(pTaskInfo);
L
Liu Jicong 已提交
752

753 754 755
  return TSDB_CODE_SUCCESS;
}

756 757 758 759 760 761
int32_t qKillTask(qTaskInfo_t tinfo, int32_t rspCode) {
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
  if (pTaskInfo == NULL) {
    return TSDB_CODE_QRY_INVALID_QHANDLE;
  }

H
Haojun Liao 已提交
762 763
  qDebug("%s sync killed execTask", GET_TASKID(pTaskInfo));
  setTaskKilled(pTaskInfo, TSDB_CODE_TSC_QUERY_KILLED);
764

X
Xiaoyu Wang 已提交
765
  while (qTaskIsExecuting(pTaskInfo)) {
766 767 768 769 770 771 772
    taosMsleep(10);
  }

  pTaskInfo->code = rspCode;
  return TSDB_CODE_SUCCESS;
}

773 774 775 776 777 778 779 780 781
bool qTaskIsExecuting(qTaskInfo_t qinfo) {
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo;
  if (NULL == pTaskInfo) {
    return false;
  }

  return 0 != atomic_load_64(&pTaskInfo->owner);
}

H
Haojun Liao 已提交
782 783
static void printTaskExecCostInLog(SExecTaskInfo* pTaskInfo) {
  STaskCostInfo* pSummary = &pTaskInfo->cost;
784
  int64_t        idleTime = pSummary->start - pSummary->created;
H
Haojun Liao 已提交
785 786 787 788

  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
  if (pSummary->pRecoder != NULL) {
    qDebug(
789
        "%s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
790
        "createGroupIdMap:%.2f ms, total blocks:%d, "
H
Haojun Liao 已提交
791
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
792 793 794 795 796 797
        GET_TASKID(pTaskInfo), idleTime / 1000.0, pSummary->elapsedTime / 1000.0, pSummary->extractListTime,
        pSummary->groupIdMapTime, pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks,
        pRecorder->totalRows, pRecorder->totalCheckedRows);
  } else {
    qDebug("%s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms", GET_TASKID(pTaskInfo), idleTime / 1000.0,
           pSummary->elapsedTime / 1000.0);
H
Haojun Liao 已提交
798 799 800
  }
}

801 802 803 804 805 806
void qDestroyTask(qTaskInfo_t qTaskHandle) {
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle;
  if (pTaskInfo == NULL) {
    return;
  }

H
Haojun Liao 已提交
807 808 809 810 811
  if (pTaskInfo->pRoot != NULL) {
    qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows);
  } else {
    qDebug("%s execTask completed", GET_TASKID(pTaskInfo));
  }
812

H
Haojun Liao 已提交
813
  printTaskExecCostInLog(pTaskInfo);  // print the query cost summary
814 815 816
  doDestroyTask(pTaskInfo);
}

H
Haojun Liao 已提交
817
int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, SArray* pExecInfoList) {
818
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
H
Haojun Liao 已提交
819
  return getOperatorExplainExecInfo(pTaskInfo->pRoot, pExecInfoList);
820 821 822 823 824 825 826 827 828
}

int32_t qSerializeTaskStatus(qTaskInfo_t tinfo, char** pOutput, int32_t* len) {
  SExecTaskInfo* pTaskInfo = (struct SExecTaskInfo*)tinfo;
  if (pTaskInfo->pRoot == NULL) {
    return TSDB_CODE_INVALID_PARA;
  }

  int32_t nOptrWithVal = 0;
L
Liu Jicong 已提交
829 830 831 832 833
  //  int32_t code = encodeOperator(pTaskInfo->pRoot, pOutput, len, &nOptrWithVal);
  //  if ((code == TSDB_CODE_SUCCESS) && (nOptrWithVal == 0)) {
  //    taosMemoryFreeClear(*pOutput);
  //    *len = 0;
  //  }
H
Haojun Liao 已提交
834
  return 0;
835 836 837 838 839 840 841 842 843
}

int32_t qDeserializeTaskStatus(qTaskInfo_t tinfo, const char* pInput, int32_t len) {
  SExecTaskInfo* pTaskInfo = (struct SExecTaskInfo*)tinfo;

  if (pTaskInfo == NULL || pInput == NULL || len == 0) {
    return TSDB_CODE_INVALID_PARA;
  }

H
Haojun Liao 已提交
844
  return 0;
L
Liu Jicong 已提交
845
  //  return decodeOperator(pTaskInfo->pRoot, pInput, len);
846 847 848 849 850 851 852
}

int32_t qExtractStreamScanner(qTaskInfo_t tinfo, void** scanner) {
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
  SOperatorInfo* pOperator = pTaskInfo->pRoot;

  while (1) {
853
    uint16_t type = pOperator->operatorType;
854 855 856 857 858 859 860 861 862 863
    if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
      *scanner = pOperator->info;
      return 0;
    } else {
      ASSERT(pOperator->numOfDownstream == 1);
      pOperator = pOperator->pDownstream[0];
    }
  }
}

864
int32_t qStreamSourceRecoverStep1(qTaskInfo_t tinfo, int64_t ver) {
865 866
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
  ASSERT(pTaskInfo->execModel == OPTR_EXEC_MODEL_STREAM);
L
Liu Jicong 已提交
867
  pTaskInfo->streamInfo.fillHistoryVer1 = ver;
868 869 870 871 872 873 874
  pTaskInfo->streamInfo.recoverStep = STREAM_RECOVER_STEP__PREPARE1;
  return 0;
}

int32_t qStreamSourceRecoverStep2(qTaskInfo_t tinfo, int64_t ver) {
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
  ASSERT(pTaskInfo->execModel == OPTR_EXEC_MODEL_STREAM);
L
Liu Jicong 已提交
875
  pTaskInfo->streamInfo.fillHistoryVer2 = ver;
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
  pTaskInfo->streamInfo.recoverStep = STREAM_RECOVER_STEP__PREPARE2;
  return 0;
}

int32_t qStreamRecoverFinish(qTaskInfo_t tinfo) {
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
  ASSERT(pTaskInfo->execModel == OPTR_EXEC_MODEL_STREAM);
  pTaskInfo->streamInfo.recoverStep = STREAM_RECOVER_STEP__NONE;
  return 0;
}

int32_t qStreamSetParamForRecover(qTaskInfo_t tinfo) {
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
  SOperatorInfo* pOperator = pTaskInfo->pRoot;

  while (1) {
    if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL ||
        pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_INTERVAL ||
        pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_INTERVAL) {
      SStreamIntervalOperatorInfo* pInfo = pOperator->info;
L
Liu Jicong 已提交
896 897
      ASSERT(pInfo->twAggSup.calTrigger == STREAM_TRIGGER_AT_ONCE ||
             pInfo->twAggSup.calTrigger == STREAM_TRIGGER_WINDOW_CLOSE);
H
Haojun Liao 已提交
898
      ASSERT(pInfo->twAggSup.calTriggerSaved == 0 && pInfo->twAggSup.deleteMarkSaved == 0);
L
Liu Jicong 已提交
899 900 901 902 903

      qInfo("save stream param for interval: %d,  %" PRId64, pInfo->twAggSup.calTrigger, pInfo->twAggSup.deleteMark);

      pInfo->twAggSup.calTriggerSaved = pInfo->twAggSup.calTrigger;
      pInfo->twAggSup.deleteMarkSaved = pInfo->twAggSup.deleteMark;
904 905
      pInfo->twAggSup.calTrigger = STREAM_TRIGGER_AT_ONCE;
      pInfo->twAggSup.deleteMark = INT64_MAX;
906 907
      pInfo->ignoreExpiredDataSaved = pInfo->ignoreExpiredData;
      pInfo->ignoreExpiredData = false;
908 909 910 911
    } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SESSION ||
               pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_SESSION ||
               pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_SESSION) {
      SStreamSessionAggOperatorInfo* pInfo = pOperator->info;
L
Liu Jicong 已提交
912 913 914
      ASSERT(pInfo->twAggSup.calTrigger == STREAM_TRIGGER_AT_ONCE ||
             pInfo->twAggSup.calTrigger == STREAM_TRIGGER_WINDOW_CLOSE);

H
Haojun Liao 已提交
915
      ASSERT(pInfo->twAggSup.calTriggerSaved == 0 && pInfo->twAggSup.deleteMarkSaved == 0);
L
Liu Jicong 已提交
916 917 918 919
      qInfo("save stream param for session: %d,  %" PRId64, pInfo->twAggSup.calTrigger, pInfo->twAggSup.deleteMark);

      pInfo->twAggSup.calTriggerSaved = pInfo->twAggSup.calTrigger;
      pInfo->twAggSup.deleteMarkSaved = pInfo->twAggSup.deleteMark;
920 921
      pInfo->twAggSup.calTrigger = STREAM_TRIGGER_AT_ONCE;
      pInfo->twAggSup.deleteMark = INT64_MAX;
922 923
      pInfo->ignoreExpiredDataSaved = pInfo->ignoreExpiredData;
      pInfo->ignoreExpiredData = false;
924 925
    } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_STATE) {
      SStreamStateAggOperatorInfo* pInfo = pOperator->info;
L
Liu Jicong 已提交
926 927
      ASSERT(pInfo->twAggSup.calTrigger == STREAM_TRIGGER_AT_ONCE ||
             pInfo->twAggSup.calTrigger == STREAM_TRIGGER_WINDOW_CLOSE);
H
Haojun Liao 已提交
928
      ASSERT(pInfo->twAggSup.calTriggerSaved == 0 && pInfo->twAggSup.deleteMarkSaved == 0);
L
Liu Jicong 已提交
929 930 931 932 933

      qInfo("save stream param for state: %d,  %" PRId64, pInfo->twAggSup.calTrigger, pInfo->twAggSup.deleteMark);

      pInfo->twAggSup.calTriggerSaved = pInfo->twAggSup.calTrigger;
      pInfo->twAggSup.deleteMarkSaved = pInfo->twAggSup.deleteMark;
934 935
      pInfo->twAggSup.calTrigger = STREAM_TRIGGER_AT_ONCE;
      pInfo->twAggSup.deleteMark = INT64_MAX;
936 937
      pInfo->ignoreExpiredDataSaved = pInfo->ignoreExpiredData;
      pInfo->ignoreExpiredData = false;
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
    }

    // iterate operator tree
    if (pOperator->numOfDownstream != 1 || pOperator->pDownstream[0] == NULL) {
      if (pOperator->numOfDownstream > 1) {
        qError("unexpected stream, multiple downstream");
        ASSERT(0);
        return -1;
      }
      return 0;
    } else {
      pOperator = pOperator->pDownstream[0];
    }
  }

  return 0;
}

int32_t qStreamRestoreParam(qTaskInfo_t tinfo) {
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
  SOperatorInfo* pOperator = pTaskInfo->pRoot;

  while (1) {
    if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL ||
        pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_INTERVAL ||
        pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_INTERVAL) {
      SStreamIntervalOperatorInfo* pInfo = pOperator->info;
L
Liu Jicong 已提交
965 966
      pInfo->twAggSup.calTrigger = pInfo->twAggSup.calTriggerSaved;
      pInfo->twAggSup.deleteMark = pInfo->twAggSup.deleteMarkSaved;
967
      pInfo->ignoreExpiredData = pInfo->ignoreExpiredDataSaved;
L
Liu Jicong 已提交
968
      qInfo("restore stream param for interval: %d,  %" PRId64, pInfo->twAggSup.calTrigger, pInfo->twAggSup.deleteMark);
969 970 971 972
    } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SESSION ||
               pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_SESSION ||
               pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_SESSION) {
      SStreamSessionAggOperatorInfo* pInfo = pOperator->info;
L
Liu Jicong 已提交
973 974
      pInfo->twAggSup.calTrigger = pInfo->twAggSup.calTriggerSaved;
      pInfo->twAggSup.deleteMark = pInfo->twAggSup.deleteMarkSaved;
975
      pInfo->ignoreExpiredData = pInfo->ignoreExpiredDataSaved;
L
Liu Jicong 已提交
976
      qInfo("restore stream param for session: %d,  %" PRId64, pInfo->twAggSup.calTrigger, pInfo->twAggSup.deleteMark);
977 978
    } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_STATE) {
      SStreamStateAggOperatorInfo* pInfo = pOperator->info;
L
Liu Jicong 已提交
979 980
      pInfo->twAggSup.calTrigger = pInfo->twAggSup.calTriggerSaved;
      pInfo->twAggSup.deleteMark = pInfo->twAggSup.deleteMarkSaved;
981
      pInfo->ignoreExpiredData = pInfo->ignoreExpiredDataSaved;
L
Liu Jicong 已提交
982
      qInfo("restore stream param for state: %d,  %" PRId64, pInfo->twAggSup.calTrigger, pInfo->twAggSup.deleteMark);
983 984 985 986 987 988 989 990 991 992 993 994 995
    }

    // iterate operator tree
    if (pOperator->numOfDownstream != 1 || pOperator->pDownstream[0] == NULL) {
      if (pOperator->numOfDownstream > 1) {
        qError("unexpected stream, multiple downstream");
        return -1;
      }
      return 0;
    } else {
      pOperator = pOperator->pDownstream[0];
    }
  }
996 997
  return 0;
}
998

L
Liu Jicong 已提交
999 1000 1001 1002
bool qStreamRecoverScanFinished(qTaskInfo_t tinfo) {
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
  return pTaskInfo->streamInfo.recoverScanFinished;
}
1003 1004 1005 1006 1007 1008

void* qExtractReaderFromStreamScanner(void* scanner) {
  SStreamScanInfo* pInfo = scanner;
  return (void*)pInfo->tqReader;
}

wmmhello's avatar
wmmhello 已提交
1009 1010 1011 1012 1013 1014 1015 1016
const SSchemaWrapper* qExtractSchemaFromTask(qTaskInfo_t tinfo) {
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
  return pTaskInfo->streamInfo.schema;
}

const char* qExtractTbnameFromTask(qTaskInfo_t tinfo) {
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
  return pTaskInfo->streamInfo.tbName;
1017 1018
}

wmmhello's avatar
wmmhello 已提交
1019
SMqMetaRsp* qStreamExtractMetaMsg(qTaskInfo_t tinfo) {
1020
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
wmmhello's avatar
wmmhello 已提交
1021
  return &pTaskInfo->streamInfo.metaRsp;
1022 1023
}

1024
void qStreamExtractOffset(qTaskInfo_t tinfo, STqOffsetVal* pOffset) {
wmmhello's avatar
wmmhello 已提交
1025
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
1026
  memcpy(pOffset, &pTaskInfo->streamInfo.currentOffset, sizeof(STqOffsetVal));
1027 1028
}

H
Haojun Liao 已提交
1029
int32_t initQueryTableDataCondForTmq(SQueryTableDataCond* pCond, SSnapContext* sContext, SMetaTableInfo* pMtInfo) {
1030 1031
  memset(pCond, 0, sizeof(SQueryTableDataCond));
  pCond->order = TSDB_ORDER_ASC;
H
Haojun Liao 已提交
1032
  pCond->numOfCols = pMtInfo->schema->nCols;
1033
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
H
Haojun Liao 已提交
1034 1035 1036 1037
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
  if (pCond->colList == NULL || pCond->pSlotList == NULL) {
    taosMemoryFreeClear(pCond->colList);
    taosMemoryFreeClear(pCond->pSlotList);
S
Shengliang Guan 已提交
1038
    terrno = TSDB_CODE_OUT_OF_MEMORY;
1039 1040 1041
    return terrno;
  }

H
Haojun Liao 已提交
1042
  pCond->twindows = TSWINDOW_INITIALIZER;
H
Haojun Liao 已提交
1043
  pCond->suid = pMtInfo->suid;
1044 1045 1046 1047 1048
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
  pCond->startVersion = -1;
  pCond->endVersion = sContext->snapVersion;

  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
H
Haojun Liao 已提交
1049 1050 1051 1052 1053 1054
    SColumnInfo* pColInfo = &pCond->colList[i];
    pColInfo->type = pMtInfo->schema->pSchema[i].type;
    pColInfo->bytes = pMtInfo->schema->pSchema[i].bytes;
    pColInfo->colId = pMtInfo->schema->pSchema[i].colId;

    pCond->pSlotList[i] = i;
1055 1056 1057 1058 1059
  }

  return TSDB_CODE_SUCCESS;
}

1060 1061
void qStreamSetOpen(qTaskInfo_t tinfo) {
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
X
Xiaoyu Wang 已提交
1062
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
1063 1064 1065
  pOperator->status = OP_NOT_OPENED;
}

1066 1067
void verifyOffset(void *pWalReader, STqOffsetVal* pOffset){
  // if offset version is small than first version , let's seek to first version
wmmhello's avatar
wmmhello 已提交
1068
  taosThreadMutexLock(&((SWalReader*)pWalReader)->pWal->mutex);
1069
  int64_t firstVer = walGetFirstVer(((SWalReader*)pWalReader)->pWal);
wmmhello's avatar
wmmhello 已提交
1070 1071
  taosThreadMutexUnlock(&((SWalReader*)pWalReader)->pWal->mutex);

1072 1073 1074 1075 1076
  if (pOffset->version + 1 < firstVer){
    pOffset->version = firstVer - 1;
  }
}

1077
int32_t qStreamPrepareScan(qTaskInfo_t tinfo, STqOffsetVal* pOffset, int8_t subType) {
1078 1079
  SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
X
Xiaoyu Wang 已提交
1080
  const char*    id = GET_TASKID(pTaskInfo);
1081

wmmhello's avatar
wmmhello 已提交
1082
  // if pOffset equal to current offset, means continue consume
wmmhello's avatar
wmmhello 已提交
1083
  if (tOffsetEqual(pOffset, &pTaskInfo->streamInfo.currentOffset)) {
1084 1085
    return 0;
  }
1086

1087
  if (subType == TOPIC_SUB_TYPE__COLUMN) {
1088
    pOperator = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN, id);
wmmhello's avatar
wmmhello 已提交
1089 1090
    if (pOperator == NULL) {
      return -1;
1091
    }
1092

1093
    SStreamScanInfo* pInfo = pOperator->info;
1094 1095
    STableScanInfo*  pScanInfo = pInfo->pTableScanOp->info;
    STableScanBase*  pScanBaseInfo = &pScanInfo->base;
1096
    STableListInfo*  pTableListInfo = pScanBaseInfo->pTableListInfo;
1097

1098
    if (pOffset->type == TMQ_OFFSET__LOG) {
1099
      // todo refactor: move away
1100 1101 1102
      tsdbReaderClose(pScanBaseInfo->dataReader);
      pScanBaseInfo->dataReader = NULL;

1103
      verifyOffset(pInfo->tqReader->pWalReader, pOffset);
1104
      if (tqSeekVer(pInfo->tqReader, pOffset->version + 1, id) < 0) {
X
Xiaoyu Wang 已提交
1105
        qError("tqSeekVer failed ver:%" PRId64 ", %s", pOffset->version + 1, id);
1106 1107 1108
        return -1;
      }
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
1109 1110
      // iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
      // those data are from the snapshot in tsdb, besides the data in the wal file.
1111 1112
      int64_t uid = pOffset->uid;
      int64_t ts = pOffset->ts;
1113
      int32_t index = 0;
1114

1115 1116 1117 1118
      // this value may be changed if new tables are created
      taosRLockLatch(&pTaskInfo->lock);
      int32_t numOfTables = tableListGetSize(pTableListInfo);

1119
      if (uid == 0) {
1120 1121
        if (numOfTables != 0) {
          STableKeyInfo* pTableInfo = tableListGetInfo(pTableListInfo, 0);
1122 1123
          uid = pTableInfo->uid;
          ts = INT64_MIN;
1124
          pScanInfo->currentTable = 0;
1125
        } else {
1126 1127
          taosRUnLockLatch(&pTaskInfo->lock);
          qError("no table in table list, %s", id);
1128
          terrno = TSDB_CODE_PAR_INTERNAL_ERROR;
L
Liu Jicong 已提交
1129 1130
          return -1;
        }
1131
      }
H
Haojun Liao 已提交
1132

X
Xiaoyu Wang 已提交
1133 1134
      qDebug("switch to table uid:%" PRId64 " ts:%" PRId64 "% " PRId64 " rows returned", uid, ts,
             pInfo->pTableScanOp->resultInfo.totalRows);
1135
      pInfo->pTableScanOp->resultInfo.totalRows = 0;
H
Haojun Liao 已提交
1136

1137
      // start from current accessed position
H
Haojun Liao 已提交
1138 1139 1140
      // we cannot start from the pScanInfo->currentTable, since the commit offset may cause the rollback of the start
      // position, let's find it from the beginning.
      index = tableListFind(pTableListInfo, uid, 0);
1141
      taosRUnLockLatch(&pTaskInfo->lock);
1142

1143 1144 1145
      if (index >= 0) {
        pScanInfo->currentTable = index;
      } else {
H
Haojun Liao 已提交
1146 1147
        qError("vgId:%d uid:%" PRIu64 " not found in table list, total:%d, index:%d %s", pTaskInfo->id.vgId, uid,
               numOfTables, pScanInfo->currentTable, id);
1148
        terrno = TSDB_CODE_PAR_INTERNAL_ERROR;
wmmhello's avatar
wmmhello 已提交
1149 1150
        return -1;
      }
1151

1152
      STableKeyInfo keyInfo = {.uid = uid};
X
Xiaoyu Wang 已提交
1153
      int64_t       oldSkey = pScanBaseInfo->cond.twindows.skey;
1154 1155 1156

      // let's start from the next ts that returned to consumer.
      pScanBaseInfo->cond.twindows.skey = ts + 1;
H
Haojun Liao 已提交
1157
      pScanInfo->scanTimes = 0;
1158

1159 1160
      if (pScanBaseInfo->dataReader == NULL) {
        int32_t code = tsdbReaderOpen(pScanBaseInfo->readHandle.vnode, &pScanBaseInfo->cond, &keyInfo, 1,
1161
                                      pScanInfo->pResBlock, &pScanBaseInfo->dataReader, id, false, NULL);
1162 1163 1164
        if (code != TSDB_CODE_SUCCESS) {
          qError("prepare read tsdb snapshot failed, uid:%" PRId64 ", code:%s %s", pOffset->uid, tstrerror(code), id);
          terrno = code;
wmmhello's avatar
wmmhello 已提交
1165
          return -1;
L
Liu Jicong 已提交
1166
        }
1167

H
Haojun Liao 已提交
1168 1169
        qDebug("tsdb reader created with offset(snapshot) uid:%" PRId64 " ts:%" PRId64 " table index:%d, total:%d, %s",
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1170 1171 1172 1173
      } else {
        tsdbSetTableList(pScanBaseInfo->dataReader, &keyInfo, 1);
        tsdbReaderReset(pScanBaseInfo->dataReader, &pScanBaseInfo->cond);
        qDebug("tsdb reader offset seek snapshot to uid:%" PRId64 " ts %" PRId64 "  table index:%d numOfTable:%d, %s",
H
Haojun Liao 已提交
1174
               uid, pScanBaseInfo->cond.twindows.skey, pScanInfo->currentTable, numOfTables, id);
1175
      }
1176 1177 1178

      // restore the key value
      pScanBaseInfo->cond.twindows.skey = oldSkey;
1179
    } else {
1180
      qError("invalid pOffset->type:%d, %s", pOffset->type, id);
1181
      terrno = TSDB_CODE_PAR_INTERNAL_ERROR;
1182 1183 1184
      return -1;
    }

1185
  } else {  // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
1186

1187 1188 1189
    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
      SStreamRawScanInfo* pInfo = pOperator->info;
      SSnapContext*       sContext = pInfo->sContext;
1190

X
Xiaoyu Wang 已提交
1191
      SOperatorInfo*  p = extractOperatorInTree(pOperator, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN, id);
1192 1193
      STableListInfo* pTableListInfo = ((SStreamRawScanInfo*)(p->info))->pTableListInfo;

1194
      if (setForSnapShot(sContext, pOffset->uid) != 0) {
X
Xiaoyu Wang 已提交
1195
        qError("setDataForSnapShot error. uid:%" PRId64 " , %s", pOffset->uid, id);
1196
        terrno = TSDB_CODE_PAR_INTERNAL_ERROR;
1197 1198
        return -1;
      }
H
Haojun Liao 已提交
1199

1200 1201 1202
      SMetaTableInfo mtInfo = getUidfromSnapShot(sContext);
      tsdbReaderClose(pInfo->dataReader);
      pInfo->dataReader = NULL;
H
Haojun Liao 已提交
1203

1204 1205
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
      tableListClear(pTableListInfo);
1206

1207
      if (mtInfo.uid == 0) {
1208
        goto end;  // no data
1209
      }
1210

1211 1212
      initQueryTableDataCondForTmq(&pTaskInfo->streamInfo.tableCond, sContext, &mtInfo);
      pTaskInfo->streamInfo.tableCond.twindows.skey = pOffset->ts;
H
Haojun Liao 已提交
1213

1214
      tableListAddTableInfo(pTableListInfo, mtInfo.uid, 0);
1215

1216 1217
      STableKeyInfo* pList = tableListGetInfo(pTableListInfo, 0);
      int32_t        size = tableListGetSize(pTableListInfo);
1218

X
Xiaoyu Wang 已提交
1219
      tsdbReaderOpen(pInfo->vnode, &pTaskInfo->streamInfo.tableCond, pList, size, NULL, &pInfo->dataReader, NULL,
1220
                     false, NULL);
L
Liu Jicong 已提交
1221

1222 1223
      cleanupQueryTableDataCond(&pTaskInfo->streamInfo.tableCond);
      strcpy(pTaskInfo->streamInfo.tbName, mtInfo.tbName);
1224
      tDeleteSchemaWrapper(pTaskInfo->streamInfo.schema);
1225 1226
      pTaskInfo->streamInfo.schema = mtInfo.schema;

X
Xiaoyu Wang 已提交
1227
      qDebug("tmqsnap qStreamPrepareScan snapshot data uid:%" PRId64 " ts %" PRId64 " %s", mtInfo.uid, pOffset->ts, id);
1228 1229 1230 1231 1232
    } else if (pOffset->type == TMQ_OFFSET__SNAPSHOT_META) {
      SStreamRawScanInfo* pInfo = pOperator->info;
      SSnapContext*       sContext = pInfo->sContext;
      if (setForSnapShot(sContext, pOffset->uid) != 0) {
        qError("setForSnapShot error. uid:%" PRIu64 " ,version:%" PRId64, pOffset->uid, pOffset->version);
1233
        terrno = TSDB_CODE_PAR_INTERNAL_ERROR;
1234 1235
        return -1;
      }
X
Xiaoyu Wang 已提交
1236 1237
      qDebug("tmqsnap qStreamPrepareScan snapshot meta uid:%" PRId64 " ts %" PRId64 " %s", pOffset->uid, pOffset->ts,
             id);
1238 1239 1240 1241
    } else if (pOffset->type == TMQ_OFFSET__LOG) {
      SStreamRawScanInfo* pInfo = pOperator->info;
      tsdbReaderClose(pInfo->dataReader);
      pInfo->dataReader = NULL;
1242
      qDebug("tmqsnap qStreamPrepareScan snapshot log, %s", id);
wmmhello's avatar
wmmhello 已提交
1243
    }
1244
  }
1245 1246

end:
wmmhello's avatar
wmmhello 已提交
1247
  pTaskInfo->streamInfo.currentOffset = *pOffset;
1248

1249 1250
  return 0;
}
H
Haojun Liao 已提交
1251 1252 1253

void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
X
Xiaoyu Wang 已提交
1254
  if (pMsg->info.ahandle == NULL) {
wmmhello's avatar
wmmhello 已提交
1255 1256 1257
    qError("pMsg->info.ahandle is NULL");
    return;
  }
H
Haojun Liao 已提交
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273

  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};

  if (pMsg->contLen > 0) {
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
    if (buf.pData == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      pMsg->code = TSDB_CODE_OUT_OF_MEMORY;
    } else {
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
    }
  }

  pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
  rpcFreeCont(pMsg->pCont);
  destroySendMsgInfo(pSendInfo);
L
Liu Jicong 已提交
1274
}
L
Liu Jicong 已提交
1275

1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
SArray* qGetQueriedTableListInfo(qTaskInfo_t tinfo) {
  SExecTaskInfo* pTaskInfo = tinfo;
  SArray* plist = getTableListInfo(pTaskInfo);

  // only extract table in the first elements
  STableListInfo* pTableListInfo = taosArrayGetP(plist, 0);

  SArray* pUidList = taosArrayInit(10, sizeof(uint64_t));

  int32_t numOfTables = tableListGetSize(pTableListInfo);
  for(int32_t i = 0; i < numOfTables; ++i) {
    STableKeyInfo* pKeyInfo = tableListGetInfo(pTableListInfo, i);
    taosArrayPush(pUidList, &pKeyInfo->uid);
  }

  taosArrayDestroy(plist);
  return pUidList;
}
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315

static void extractTableList(SArray* pList, const SOperatorInfo* pOperator) {
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
    SStreamScanInfo* pScanInfo = pOperator->info;
    STableScanInfo*  pTableScanInfo = pScanInfo->pTableScanOp->info;
    taosArrayPush(pList, &pTableScanInfo->base.pTableListInfo);
  } else if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
    STableScanInfo* pScanInfo = pOperator->info;
    taosArrayPush(pList, &pScanInfo->base.pTableListInfo);
  } else {
    if (pOperator->pDownstream != NULL && pOperator->pDownstream[0] != NULL) {
      extractTableList(pList, pOperator->pDownstream[0]);
    }
  }
}

SArray* getTableListInfo(const SExecTaskInfo* pTaskInfo) {
  SArray*        pArray = taosArrayInit(0, POINTER_BYTES);
  SOperatorInfo* pOperator = pTaskInfo->pRoot;
  extractTableList(pArray, pOperator);
  return pArray;
}