executorimpl.c 147.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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/>.
 */
15

H
Haojun Liao 已提交
16 17
#include "filter.h"
#include "function.h"
18 19
#include "functionMgt.h"
#include "os.h"
H
Haojun Liao 已提交
20
#include "querynodes.h"
21
#include "tfill.h"
dengyihao's avatar
dengyihao 已提交
22
#include "tname.h"
X
Xiaoyu Wang 已提交
23
#include "tref.h"
24

H
Haojun Liao 已提交
25
#include "tdatablock.h"
26
#include "tglobal.h"
H
Haojun Liao 已提交
27
#include "tmsg.h"
H
Haojun Liao 已提交
28
#include "tsort.h"
29
#include "ttime.h"
H
Haojun Liao 已提交
30

31
#include "executorimpl.h"
dengyihao's avatar
dengyihao 已提交
32
#include "index.h"
33
#include "query.h"
34 35
#include "tcompare.h"
#include "tcompression.h"
H
Haojun Liao 已提交
36
#include "thash.h"
37
#include "ttypes.h"
dengyihao's avatar
dengyihao 已提交
38
#include "vnode.h"
39

H
Haojun Liao 已提交
40
#define IS_MAIN_SCAN(runtime)          ((runtime)->scanFlag == MAIN_SCAN)
41 42 43 44 45 46
#define SET_REVERSE_SCAN_FLAG(runtime) ((runtime)->scanFlag = REVERSE_SCAN)

#define GET_FORWARD_DIRECTION_FACTOR(ord) (((ord) == TSDB_ORDER_ASC) ? QUERY_ASC_FORWARD_STEP : QUERY_DESC_FORWARD_STEP)

#if 0
static UNUSED_FUNC void *u_malloc (size_t __size) {
wafwerar's avatar
wafwerar 已提交
47
  uint32_t v = taosRand();
48 49 50 51

  if (v % 1000 <= 0) {
    return NULL;
  } else {
wafwerar's avatar
wafwerar 已提交
52
    return taosMemoryMalloc(__size);
53 54 55 56
  }
}

static UNUSED_FUNC void* u_calloc(size_t num, size_t __size) {
wafwerar's avatar
wafwerar 已提交
57
  uint32_t v = taosRand();
58 59 60
  if (v % 1000 <= 0) {
    return NULL;
  } else {
wafwerar's avatar
wafwerar 已提交
61
    return taosMemoryCalloc(num, __size);
62 63 64 65
  }
}

static UNUSED_FUNC void* u_realloc(void* p, size_t __size) {
wafwerar's avatar
wafwerar 已提交
66
  uint32_t v = taosRand();
67 68 69
  if (v % 5 <= 1) {
    return NULL;
  } else {
wafwerar's avatar
wafwerar 已提交
70
    return taosMemoryRealloc(p, __size);
71 72 73 74 75 76 77 78
  }
}

#define calloc  u_calloc
#define malloc  u_malloc
#define realloc u_realloc
#endif

X
Xiaoyu Wang 已提交
79
#define CLEAR_QUERY_STATUS(q, st)   ((q)->status &= (~(st)))
80 81
#define QUERY_IS_INTERVAL_QUERY(_q) ((_q)->interval.interval > 0)

L
Liu Jicong 已提交
82 83
int32_t getMaximumIdleDurationSec() { return tsShellActivityTimer * 2; }

84
static void setBlockSMAInfo(SqlFunctionCtx* pCtx, SExprInfo* pExpr, SSDataBlock* pBlock);
85

X
Xiaoyu Wang 已提交
86
static void releaseQueryBuf(size_t numOfTables);
87

88 89
static void destroyFillOperatorInfo(void* param);
static void destroyProjectOperatorInfo(void* param);
H
Haojun Liao 已提交
90
static void destroySortOperatorInfo(void* param);
91
static void destroyAggOperatorInfo(void* param);
X
Xiaoyu Wang 已提交
92

93 94
static void destroyIntervalOperatorInfo(void* param);
static void destroyExchangeOperatorInfo(void* param);
H
Haojun Liao 已提交
95

96 97
static void destroyOperatorInfo(SOperatorInfo* pOperator);

98
void doSetOperatorCompleted(SOperatorInfo* pOperator) {
99
  pOperator->status = OP_EXEC_DONE;
H
Haojun Liao 已提交
100
  ASSERT(pOperator->pTaskInfo != NULL);
101

102
  pOperator->cost.totalCost = (taosGetTimestampUs() - pOperator->pTaskInfo->cost.start * 1000) / 1000.0;
H
Haojun Liao 已提交
103
  setTaskStatus(pOperator->pTaskInfo, TASK_COMPLETED);
104
}
105

H
Haojun Liao 已提交
106
int32_t operatorDummyOpenFn(SOperatorInfo* pOperator) {
107
  OPTR_SET_OPENED(pOperator);
108
  pOperator->cost.openCost = 0;
H
Haojun Liao 已提交
109
  return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
110 111
}

112
SOperatorFpSet createOperatorFpSet(__optr_open_fn_t openFn, __optr_fn_t nextFn, __optr_fn_t streamFn,
113
                                   __optr_fn_t cleanup, __optr_close_fn_t closeFn, __optr_explain_fn_t explain) {
114 115 116 117 118 119 120 121 122 123 124 125
  SOperatorFpSet fpSet = {
      ._openFn = openFn,
      .getNextFn = nextFn,
      .getStreamResFn = streamFn,
      .cleanupFn = cleanup,
      .closeFn = closeFn,
      .getExplainFn = explain,
  };

  return fpSet;
}

126 127
static int32_t doCopyToSDataBlock(SExecTaskInfo* pTaskInfo, SSDataBlock* pBlock, SExprSupp* pSup, SDiskbasedBuf* pBuf,
                                  SGroupResInfo* pGroupResInfo);
H
Haojun Liao 已提交
128

129
static void initCtxOutputBuffer(SqlFunctionCtx* pCtx, int32_t size);
130
static void doSetTableGroupOutputBuf(SOperatorInfo* pOperator, int32_t numOfOutput, uint64_t groupId);
131

132
#if 0
L
Liu Jicong 已提交
133 134
static bool chkResultRowFromKey(STaskRuntimeEnv* pRuntimeEnv, SResultRowInfo* pResultRowInfo, char* pData,
                                int16_t bytes, bool masterscan, uint64_t uid) {
135 136 137
  bool existed = false;
  SET_RES_WINDOW_KEY(pRuntimeEnv->keyBuf, pData, bytes, uid);

L
Liu Jicong 已提交
138 139
  SResultRow** p1 =
      (SResultRow**)taosHashGet(pRuntimeEnv->pResultRowHashTable, pRuntimeEnv->keyBuf, GET_RES_WINDOW_KEY_LEN(bytes));
140 141 142 143 144 145 146 147 148 149 150

  // in case of repeat scan/reverse scan, no new time window added.
  if (QUERY_IS_INTERVAL_QUERY(pRuntimeEnv->pQueryAttr)) {
    if (!masterscan) {  // the *p1 may be NULL in case of sliding+offset exists.
      return p1 != NULL;
    }

    if (p1 != NULL) {
      if (pResultRowInfo->size == 0) {
        existed = false;
      } else if (pResultRowInfo->size == 1) {
dengyihao's avatar
dengyihao 已提交
151
        //        existed = (pResultRowInfo->pResult[0] == (*p1));
152 153
      } else {  // check if current pResultRowInfo contains the existed pResultRow
        SET_RES_EXT_WINDOW_KEY(pRuntimeEnv->keyBuf, pData, bytes, uid, pResultRowInfo);
L
Liu Jicong 已提交
154 155
        int64_t* index =
            taosHashGet(pRuntimeEnv->pResultRowListSet, pRuntimeEnv->keyBuf, GET_RES_EXT_WINDOW_KEY_LEN(bytes));
156 157 158 159 160 161 162 163 164 165 166 167 168
        if (index != NULL) {
          existed = true;
        } else {
          existed = false;
        }
      }
    }

    return existed;
  }

  return p1 != NULL;
}
169
#endif
170

171
SResultRow* getNewResultRow(SDiskbasedBuf* pResultBuf, int32_t* currentPageId, int32_t interBufSize) {
L
Liu Jicong 已提交
172
  SFilePage* pData = NULL;
173 174 175

  // in the first scan, new space needed for results
  int32_t pageId = -1;
176
  if (*currentPageId == -1) {
177
    pData = getNewBufPage(pResultBuf, &pageId);
178 179
    pData->num = sizeof(SFilePage);
  } else {
180 181
    pData = getBufPage(pResultBuf, *currentPageId);
    pageId = *currentPageId;
182

wmmhello's avatar
wmmhello 已提交
183
    if (pData->num + interBufSize > getBufPageSize(pResultBuf)) {
184
      // release current page first, and prepare the next one
185
      releaseBufPage(pResultBuf, pData);
186

187
      pData = getNewBufPage(pResultBuf, &pageId);
188 189 190 191 192 193 194 195 196 197
      if (pData != NULL) {
        pData->num = sizeof(SFilePage);
      }
    }
  }

  if (pData == NULL) {
    return NULL;
  }

198 199
  setBufPageDirty(pData, true);

200 201 202 203
  // set the number of rows in current disk page
  SResultRow* pResultRow = (SResultRow*)((char*)pData + pData->num);
  pResultRow->pageId = pageId;
  pResultRow->offset = (int32_t)pData->num;
204
  *currentPageId = pageId;
205

wmmhello's avatar
wmmhello 已提交
206
  pData->num += interBufSize;
207 208 209
  return pResultRow;
}

210 211 212 213 214 215 216
/**
 * the struct of key in hash table
 * +----------+---------------+
 * | group id |   key data    |
 * | 8 bytes  | actual length |
 * +----------+---------------+
 */
217 218 219
SResultRow* doSetResultOutBufByKey(SDiskbasedBuf* pResultBuf, SResultRowInfo* pResultRowInfo, char* pData,
                                   int16_t bytes, bool masterscan, uint64_t groupId, SExecTaskInfo* pTaskInfo,
                                   bool isIntervalQuery, SAggSupporter* pSup) {
220
  SET_RES_WINDOW_KEY(pSup->keyBuf, pData, bytes, groupId);
H
Haojun Liao 已提交
221

dengyihao's avatar
dengyihao 已提交
222
  SResultRowPosition* p1 =
223
      (SResultRowPosition*)tSimpleHashGet(pSup->pResultRowHashTable, pSup->keyBuf, GET_RES_WINDOW_KEY_LEN(bytes));
H
Haojun Liao 已提交
224

225 226
  SResultRow* pResult = NULL;

H
Haojun Liao 已提交
227 228
  // in case of repeat scan/reverse scan, no new time window added.
  if (isIntervalQuery) {
229
    if (masterscan && p1 != NULL) {  // the *p1 may be NULL in case of sliding+offset exists.
230
      pResult = getResultRowByPos(pResultBuf, p1, true);
231
      ASSERT(pResult->pageId == p1->pageId && pResult->offset == p1->offset);
H
Haojun Liao 已提交
232 233
    }
  } else {
dengyihao's avatar
dengyihao 已提交
234 235
    // In case of group by column query, the required SResultRow object must be existInCurrentResusltRowInfo in the
    // pResultRowInfo object.
H
Haojun Liao 已提交
236
    if (p1 != NULL) {
237
      // todo
238
      pResult = getResultRowByPos(pResultBuf, p1, true);
239
      ASSERT(pResult->pageId == p1->pageId && pResult->offset == p1->offset);
H
Haojun Liao 已提交
240 241 242
    }
  }

L
Liu Jicong 已提交
243
  // 1. close current opened time window
244
  if (pResultRowInfo->cur.pageId != -1 && ((pResult == NULL) || (pResult->pageId != pResultRowInfo->cur.pageId))) {
245
    SResultRowPosition pos = pResultRowInfo->cur;
X
Xiaoyu Wang 已提交
246
    SFilePage*         pPage = getBufPage(pResultBuf, pos.pageId);
247 248 249 250 251
    releaseBufPage(pResultBuf, pPage);
  }

  // allocate a new buffer page
  if (pResult == NULL) {
H
Haojun Liao 已提交
252
    ASSERT(pSup->resultRowSize > 0);
253
    pResult = getNewResultRow(pResultBuf, &pSup->currentPageId, pSup->resultRowSize);
254

255 256
    // add a new result set for a new group
    SResultRowPosition pos = {.pageId = pResult->pageId, .offset = pResult->offset};
257
    tSimpleHashPut(pSup->pResultRowHashTable, pSup->keyBuf, GET_RES_WINDOW_KEY_LEN(bytes), &pos,
L
Liu Jicong 已提交
258
                   sizeof(SResultRowPosition));
H
Haojun Liao 已提交
259 260
  }

261 262 263
  // 2. set the new time window to be the new active time window
  pResultRowInfo->cur = (SResultRowPosition){.pageId = pResult->pageId, .offset = pResult->offset};

H
Haojun Liao 已提交
264
  // too many time window in query
265
  if (pTaskInfo->execModel == OPTR_EXEC_MODEL_BATCH &&
266
      tSimpleHashGetSize(pSup->pResultRowHashTable) > MAX_INTERVAL_TIME_WINDOW) {
267
    T_LONG_JMP(pTaskInfo->env, TSDB_CODE_QRY_TOO_MANY_TIMEWINDOW);
H
Haojun Liao 已提交
268 269
  }

H
Haojun Liao 已提交
270
  return pResult;
H
Haojun Liao 已提交
271 272
}

273
// a new buffer page for each table. Needs to opt this design
L
Liu Jicong 已提交
274
static int32_t addNewWindowResultBuf(SResultRow* pWindowRes, SDiskbasedBuf* pResultBuf, int32_t tid, uint32_t size) {
275 276 277 278
  if (pWindowRes->pageId != -1) {
    return 0;
  }

L
Liu Jicong 已提交
279
  SFilePage* pData = NULL;
280 281 282

  // in the first scan, new space needed for results
  int32_t pageId = -1;
283
  SIDList list = getDataBufPagesIdList(pResultBuf);
284 285

  if (taosArrayGetSize(list) == 0) {
286
    pData = getNewBufPage(pResultBuf, &pageId);
287
    pData->num = sizeof(SFilePage);
288 289
  } else {
    SPageInfo* pi = getLastPageInfo(list);
290
    pData = getBufPage(pResultBuf, getPageId(pi));
291
    pageId = getPageId(pi);
292

293
    if (pData->num + size > getBufPageSize(pResultBuf)) {
294
      // release current page first, and prepare the next one
295
      releaseBufPageInfo(pResultBuf, pi);
296

297
      pData = getNewBufPage(pResultBuf, &pageId);
298
      if (pData != NULL) {
299
        pData->num = sizeof(SFilePage);
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
      }
    }
  }

  if (pData == NULL) {
    return -1;
  }

  // set the number of rows in current disk page
  if (pWindowRes->pageId == -1) {  // not allocated yet, allocate new buffer
    pWindowRes->pageId = pageId;
    pWindowRes->offset = (int32_t)pData->num;

    pData->num += size;
    assert(pWindowRes->pageId >= 0);
  }

  return 0;
}

320
//  query_range_start, query_range_end, window_duration, window_start, window_end
321
void initExecTimeWindowInfo(SColumnInfoData* pColData, STimeWindow* pQueryWindow) {
322 323 324
  pColData->info.type = TSDB_DATA_TYPE_TIMESTAMP;
  pColData->info.bytes = sizeof(int64_t);

H
Haojun Liao 已提交
325
  colInfoDataEnsureCapacity(pColData, 5, false);
326 327 328 329 330 331 332 333 334
  colDataAppendInt64(pColData, 0, &pQueryWindow->skey);
  colDataAppendInt64(pColData, 1, &pQueryWindow->ekey);

  int64_t interval = 0;
  colDataAppendInt64(pColData, 2, &interval);  // this value may be variable in case of 'n' and 'y'.
  colDataAppendInt64(pColData, 3, &pQueryWindow->skey);
  colDataAppendInt64(pColData, 4, &pQueryWindow->ekey);
}

L
Liu Jicong 已提交
335
void cleanupExecTimeWindowInfo(SColumnInfoData* pColData) { colDataDestroy(pColData); }
H
Haojun Liao 已提交
336

337 338 339 340 341 342 343 344 345 346 347 348 349 350
typedef struct {
  bool    hasAgg;
  int32_t numOfRows;
  int32_t startOffset;
} SFunctionCtxStatus;

static void functionCtxSave(SqlFunctionCtx* pCtx, SFunctionCtxStatus* pStatus) {
  pStatus->hasAgg = pCtx->input.colDataAggIsSet;
  pStatus->numOfRows = pCtx->input.numOfRows;
  pStatus->startOffset = pCtx->input.startRowIndex;
}

static void functionCtxRestore(SqlFunctionCtx* pCtx, SFunctionCtxStatus* pStatus) {
  pCtx->input.colDataAggIsSet = pStatus->hasAgg;
H
Haojun Liao 已提交
351
  pCtx->input.numOfRows = pStatus->numOfRows;
352 353 354 355 356
  pCtx->input.startRowIndex = pStatus->startOffset;
}

void doApplyFunctions(SExecTaskInfo* taskInfo, SqlFunctionCtx* pCtx, SColumnInfoData* pTimeWindowData, int32_t offset,
                      int32_t forwardStep, int32_t numOfTotal, int32_t numOfOutput) {
357
  for (int32_t k = 0; k < numOfOutput; ++k) {
H
Haojun Liao 已提交
358
    // keep it temporarily
359 360
    SFunctionCtxStatus status = {0};
    functionCtxSave(&pCtx[k], &status);
361

362
    pCtx[k].input.startRowIndex = offset;
363
    pCtx[k].input.numOfRows = forwardStep;
364 365 366

    // not a whole block involved in query processing, statistics data can not be used
    // NOTE: the original value of isSet have been changed here
367 368
    if (pCtx[k].input.colDataAggIsSet && forwardStep < numOfTotal) {
      pCtx[k].input.colDataAggIsSet = false;
369 370
    }

371 372
    if (fmIsWindowPseudoColumnFunc(pCtx[k].functionId)) {
      SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(&pCtx[k]);
373 374

      char* p = GET_ROWCELL_INTERBUF(pEntryInfo);
375

376
      SColumnInfoData idata = {0};
dengyihao's avatar
dengyihao 已提交
377
      idata.info.type = TSDB_DATA_TYPE_BIGINT;
378
      idata.info.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
dengyihao's avatar
dengyihao 已提交
379
      idata.pData = p;
380 381 382 383

      SScalarParam out = {.columnData = &idata};
      SScalarParam tw = {.numOfRows = 5, .columnData = pTimeWindowData};
      pCtx[k].sfp.process(&tw, 1, &out);
384
      pEntryInfo->numOfRes = 1;
385 386 387 388 389 390 391 392
    } else {
      int32_t code = TSDB_CODE_SUCCESS;
      if (functionNeedToExecute(&pCtx[k]) && pCtx[k].fpSet.process != NULL) {
        code = pCtx[k].fpSet.process(&pCtx[k]);

        if (code != TSDB_CODE_SUCCESS) {
          qError("%s apply functions error, code: %s", GET_TASKID(taskInfo), tstrerror(code));
          taskInfo->code = code;
393
          T_LONG_JMP(taskInfo->env, code);
394
        }
395
      }
396

397
      // restore it
398
      functionCtxRestore(&pCtx[k], &status);
399
    }
400 401 402
  }
}

403 404
static int32_t doSetInputDataBlock(SExprSupp* pExprSup, SSDataBlock* pBlock, int32_t order, int32_t scanFlag,
                                   bool createDummyCol);
405

406 407 408
static void doSetInputDataBlockInfo(SExprSupp* pExprSup, SSDataBlock* pBlock, int32_t order) {
  SqlFunctionCtx* pCtx = pExprSup->pCtx;
  for (int32_t i = 0; i < pExprSup->numOfExprs; ++i) {
409
    pCtx[i].order = order;
410
    pCtx[i].input.numOfRows = pBlock->info.rows;
411
    setBlockSMAInfo(&pCtx[i], &pExprSup->pExprInfo[i], pBlock);
412
    pCtx[i].pSrcBlock = pBlock;
413 414 415
  }
}

416
void setInputDataBlock(SExprSupp* pExprSup, SSDataBlock* pBlock, int32_t order, int32_t scanFlag, bool createDummyCol) {
417
  if (pBlock->pBlockAgg != NULL) {
418
    doSetInputDataBlockInfo(pExprSup, pBlock, order);
419
  } else {
420
    doSetInputDataBlock(pExprSup, pBlock, order, scanFlag, createDummyCol);
H
Haojun Liao 已提交
421
  }
422 423
}

L
Liu Jicong 已提交
424 425
static int32_t doCreateConstantValColumnInfo(SInputColumnInfoData* pInput, SFunctParam* pFuncParam, int32_t paramIndex,
                                             int32_t numOfRows) {
426 427 428 429 430 431 432 433
  SColumnInfoData* pColInfo = NULL;
  if (pInput->pData[paramIndex] == NULL) {
    pColInfo = taosMemoryCalloc(1, sizeof(SColumnInfoData));
    if (pColInfo == NULL) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }

    // Set the correct column info (data type and bytes)
434 435
    pColInfo->info.type = pFuncParam->param.nType;
    pColInfo->info.bytes = pFuncParam->param.nLen;
436 437

    pInput->pData[paramIndex] = pColInfo;
438 439
  } else {
    pColInfo = pInput->pData[paramIndex];
440 441
  }

H
Haojun Liao 已提交
442
  colInfoDataEnsureCapacity(pColInfo, numOfRows, false);
443

444
  int8_t type = pFuncParam->param.nType;
445 446
  if (type == TSDB_DATA_TYPE_BIGINT || type == TSDB_DATA_TYPE_UBIGINT) {
    int64_t v = pFuncParam->param.i;
dengyihao's avatar
dengyihao 已提交
447
    for (int32_t i = 0; i < numOfRows; ++i) {
448 449 450 451
      colDataAppendInt64(pColInfo, i, &v);
    }
  } else if (type == TSDB_DATA_TYPE_DOUBLE) {
    double v = pFuncParam->param.d;
dengyihao's avatar
dengyihao 已提交
452
    for (int32_t i = 0; i < numOfRows; ++i) {
453 454
      colDataAppendDouble(pColInfo, i, &v);
    }
455
  } else if (type == TSDB_DATA_TYPE_VARCHAR) {
L
Liu Jicong 已提交
456
    char* tmp = taosMemoryMalloc(pFuncParam->param.nLen + VARSTR_HEADER_SIZE);
457
    STR_WITH_SIZE_TO_VARSTR(tmp, pFuncParam->param.pz, pFuncParam->param.nLen);
L
Liu Jicong 已提交
458
    for (int32_t i = 0; i < numOfRows; ++i) {
459 460
      colDataAppend(pColInfo, i, tmp, false);
    }
H
Haojun Liao 已提交
461
    taosMemoryFree(tmp);
462 463 464 465 466
  }

  return TSDB_CODE_SUCCESS;
}

467 468
static int32_t doSetInputDataBlock(SExprSupp* pExprSup, SSDataBlock* pBlock, int32_t order, int32_t scanFlag,
                                   bool createDummyCol) {
469
  int32_t         code = TSDB_CODE_SUCCESS;
470
  SqlFunctionCtx* pCtx = pExprSup->pCtx;
471

472
  for (int32_t i = 0; i < pExprSup->numOfExprs; ++i) {
L
Liu Jicong 已提交
473
    pCtx[i].order = order;
474 475
    pCtx[i].input.numOfRows = pBlock->info.rows;

L
Liu Jicong 已提交
476
    pCtx[i].pSrcBlock = pBlock;
X
Xiaoyu Wang 已提交
477
    pCtx[i].scanFlag = scanFlag;
H
Haojun Liao 已提交
478

479
    SInputColumnInfoData* pInput = &pCtx[i].input;
480
    pInput->uid = pBlock->info.uid;
C
Cary Xu 已提交
481
    pInput->colDataAggIsSet = false;
482

483
    SExprInfo* pOneExpr = &pExprSup->pExprInfo[i];
484
    for (int32_t j = 0; j < pOneExpr->base.numOfParams; ++j) {
dengyihao's avatar
dengyihao 已提交
485
      SFunctParam* pFuncParam = &pOneExpr->base.pParam[j];
G
Ganlin Zhao 已提交
486 487
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
        int32_t slotId = pFuncParam->pCol->slotId;
dengyihao's avatar
dengyihao 已提交
488
        pInput->pData[j] = taosArrayGet(pBlock->pDataBlock, slotId);
489 490 491
        pInput->totalRows = pBlock->info.rows;
        pInput->numOfRows = pBlock->info.rows;
        pInput->startRowIndex = 0;
492

493
        // NOTE: the last parameter is the primary timestamp column
H
Haojun Liao 已提交
494
        // todo: refactor this
495
        if (fmIsImplicitTsFunc(pCtx[i].functionId) && (j == pOneExpr->base.numOfParams - 1)) {
L
Liu Jicong 已提交
496
          pInput->pPTS = pInput->pData[j];  // in case of merge function, this is not always the ts column data.
497
          //          ASSERT(pInput->pPTS->info.type == TSDB_DATA_TYPE_TIMESTAMP);
498
        }
499 500
        ASSERT(pInput->pData[j] != NULL);
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
501 502 503
        // todo avoid case: top(k, 12), 12 is the value parameter.
        // sum(11), 11 is also the value parameter.
        if (createDummyCol && pOneExpr->base.numOfParams == 1) {
504 505 506 507
          pInput->totalRows = pBlock->info.rows;
          pInput->numOfRows = pBlock->info.rows;
          pInput->startRowIndex = 0;

508
          code = doCreateConstantValColumnInfo(pInput, pFuncParam, j, pBlock->info.rows);
509 510 511
          if (code != TSDB_CODE_SUCCESS) {
            return code;
          }
512
        }
G
Ganlin Zhao 已提交
513 514
      }
    }
H
Haojun Liao 已提交
515
  }
516 517

  return code;
H
Haojun Liao 已提交
518 519
}

520
static int32_t doAggregateImpl(SOperatorInfo* pOperator, SqlFunctionCtx* pCtx) {
521
  for (int32_t k = 0; k < pOperator->exprSupp.numOfExprs; ++k) {
H
Haojun Liao 已提交
522
    if (functionNeedToExecute(&pCtx[k])) {
523
      // todo add a dummy funtion to avoid process check
524 525 526
      if (pCtx[k].fpSet.process == NULL) {
        continue;
      }
H
Haojun Liao 已提交
527

528 529 530 531
      int32_t code = pCtx[k].fpSet.process(&pCtx[k]);
      if (code != TSDB_CODE_SUCCESS) {
        qError("%s aggregate function error happens, code: %s", GET_TASKID(pOperator->pTaskInfo), tstrerror(code));
        return code;
532
      }
533 534
    }
  }
535 536

  return TSDB_CODE_SUCCESS;
537 538
}

H
Haojun Liao 已提交
539
static void setPseudoOutputColInfo(SSDataBlock* pResult, SqlFunctionCtx* pCtx, SArray* pPseudoList) {
dengyihao's avatar
dengyihao 已提交
540
  size_t num = (pPseudoList != NULL) ? taosArrayGetSize(pPseudoList) : 0;
H
Haojun Liao 已提交
541 542 543 544 545
  for (int32_t i = 0; i < num; ++i) {
    pCtx[i].pOutput = taosArrayGet(pResult->pDataBlock, i);
  }
}

546
int32_t projectApplyFunctions(SExprInfo* pExpr, SSDataBlock* pResult, SSDataBlock* pSrcBlock, SqlFunctionCtx* pCtx,
X
Xiaoyu Wang 已提交
547
                              int32_t numOfOutput, SArray* pPseudoList) {
H
Haojun Liao 已提交
548
  setPseudoOutputColInfo(pResult, pCtx, pPseudoList);
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568

  if (pSrcBlock == NULL) {
    for (int32_t k = 0; k < numOfOutput; ++k) {
      int32_t outputSlotId = pExpr[k].base.resSchema.slotId;

      ASSERT(pExpr[k].pExpr->nodeType == QUERY_NODE_VALUE);
      SColumnInfoData* pColInfoData = taosArrayGet(pResult->pDataBlock, outputSlotId);

      int32_t type = pExpr[k].base.pParam[0].param.nType;
      if (TSDB_DATA_TYPE_NULL == type) {
        colDataAppendNNULL(pColInfoData, 0, 1);
      } else {
        colDataAppend(pColInfoData, 0, taosVariantGet(&pExpr[k].base.pParam[0].param, type), false);
      }
    }

    pResult->info.rows = 1;
    return TSDB_CODE_SUCCESS;
  }

L
Liu Jicong 已提交
569 570 571 572
  if (pResult != pSrcBlock) {
    pResult->info.groupId = pSrcBlock->info.groupId;
    memcpy(pResult->info.parTbName, pSrcBlock->info.parTbName, TSDB_TABLE_NAME_LEN);
  }
H
Haojun Liao 已提交
573

574 575
  // if the source equals to the destination, it is to create a new column as the result of scalar
  // function or some operators.
576
  bool createNewColModel = (pResult == pSrcBlock);
577 578 579
  if (createNewColModel) {
    blockDataEnsureCapacity(pResult, pResult->info.rows);
  }
580

581 582
  int32_t numOfRows = 0;

583
  for (int32_t k = 0; k < numOfOutput; ++k) {
584 585
    int32_t               outputSlotId = pExpr[k].base.resSchema.slotId;
    SqlFunctionCtx*       pfCtx = &pCtx[k];
586
    SInputColumnInfoData* pInputData = &pfCtx->input;
587

L
Liu Jicong 已提交
588
    if (pExpr[k].pExpr->nodeType == QUERY_NODE_COLUMN) {  // it is a project query
589
      SColumnInfoData* pColInfoData = taosArrayGet(pResult->pDataBlock, outputSlotId);
590
      if (pResult->info.rows > 0 && !createNewColModel) {
591
        colDataMergeCol(pColInfoData, pResult->info.rows, (int32_t*)&pResult->info.capacity, pInputData->pData[0],
592
                        pInputData->numOfRows);
593
      } else {
594
        colDataAssign(pColInfoData, pInputData->pData[0], pInputData->numOfRows, &pResult->info);
595
      }
596

597
      numOfRows = pInputData->numOfRows;
598
    } else if (pExpr[k].pExpr->nodeType == QUERY_NODE_VALUE) {
599
      SColumnInfoData* pColInfoData = taosArrayGet(pResult->pDataBlock, outputSlotId);
600

dengyihao's avatar
dengyihao 已提交
601
      int32_t offset = createNewColModel ? 0 : pResult->info.rows;
602 603 604 605 606 607 608 609

      int32_t type = pExpr[k].base.pParam[0].param.nType;
      if (TSDB_DATA_TYPE_NULL == type) {
        colDataAppendNNULL(pColInfoData, offset, pSrcBlock->info.rows);
      } else {
        for (int32_t i = 0; i < pSrcBlock->info.rows; ++i) {
          colDataAppend(pColInfoData, i + offset, taosVariantGet(&pExpr[k].base.pParam[0].param, type), false);
        }
610
      }
611 612

      numOfRows = pSrcBlock->info.rows;
H
Haojun Liao 已提交
613
    } else if (pExpr[k].pExpr->nodeType == QUERY_NODE_OPERATOR) {
614 615 616
      SArray* pBlockList = taosArrayInit(4, POINTER_BYTES);
      taosArrayPush(pBlockList, &pSrcBlock);

617
      SColumnInfoData* pResColData = taosArrayGet(pResult->pDataBlock, outputSlotId);
618
      SColumnInfoData  idata = {.info = pResColData->info, .hasNull = true};
619

620
      SScalarParam dest = {.columnData = &idata};
X
Xiaoyu Wang 已提交
621
      int32_t      code = scalarCalculate(pExpr[k].pExpr->_optrRoot.pRootNode, pBlockList, &dest);
622 623 624 625
      if (code != TSDB_CODE_SUCCESS) {
        taosArrayDestroy(pBlockList);
        return code;
      }
626

dengyihao's avatar
dengyihao 已提交
627
      int32_t startOffset = createNewColModel ? 0 : pResult->info.rows;
628
      ASSERT(pResult->info.capacity > 0);
629

630
      colDataMergeCol(pResColData, startOffset, (int32_t*)&pResult->info.capacity, &idata, dest.numOfRows);
D
dapan1121 已提交
631
      colDataDestroy(&idata);
L
Liu Jicong 已提交
632

633
      numOfRows = dest.numOfRows;
634 635
      taosArrayDestroy(pBlockList);
    } else if (pExpr[k].pExpr->nodeType == QUERY_NODE_FUNCTION) {
636 637
      // _rowts/_c0, not tbname column
      if (fmIsPseudoColumnFunc(pfCtx->functionId) && (!fmIsScanPseudoColumnFunc(pfCtx->functionId))) {
H
Haojun Liao 已提交
638
        // do nothing
639
      } else if (fmIsIndefiniteRowsFunc(pfCtx->functionId)) {
640 641
        SResultRowEntryInfo* pResInfo = GET_RES_INFO(pfCtx);
        pfCtx->fpSet.init(pfCtx, pResInfo);
642 643 644 645 646 647 648 649 650 651

        pfCtx->pOutput = taosArrayGet(pResult->pDataBlock, outputSlotId);
        pfCtx->offset = createNewColModel ? 0 : pResult->info.rows;  // set the start offset

        // set the timestamp(_rowts) output buffer
        if (taosArrayGetSize(pPseudoList) > 0) {
          int32_t* outputColIndex = taosArrayGet(pPseudoList, 0);
          pfCtx->pTsOutput = (SColumnInfoData*)pCtx[*outputColIndex].pOutput;
        }

652 653 654 655 656
        // link pDstBlock to set selectivity value
        if (pfCtx->subsidiaries.num > 0) {
          pfCtx->pDstBlock = pResult;
        }

657
        numOfRows = pfCtx->fpSet.process(pfCtx);
H
Haojun Liao 已提交
658
      } else if (fmIsAggFunc(pfCtx->functionId)) {
G
Ganlin Zhao 已提交
659
        // selective value output should be set during corresponding function execution
660 661 662
        if (fmIsSelectValueFunc(pfCtx->functionId)) {
          continue;
        }
663 664
        // _group_key function for "partition by tbname" + csum(col_name) query
        SColumnInfoData* pOutput = taosArrayGet(pResult->pDataBlock, outputSlotId);
665
        int32_t          slotId = pfCtx->param[0].pCol->slotId;
666 667 668

        // todo handle the json tag
        SColumnInfoData* pInput = taosArrayGet(pSrcBlock->pDataBlock, slotId);
669
        for (int32_t f = 0; f < pSrcBlock->info.rows; ++f) {
670 671 672 673 674 675 676 677 678
          bool isNull = colDataIsNull_s(pInput, f);
          if (isNull) {
            colDataAppendNULL(pOutput, pResult->info.rows + f);
          } else {
            char* data = colDataGetData(pInput, f);
            colDataAppend(pOutput, pResult->info.rows + f, data, isNull);
          }
        }

H
Haojun Liao 已提交
679 680 681
      } else {
        SArray* pBlockList = taosArrayInit(4, POINTER_BYTES);
        taosArrayPush(pBlockList, &pSrcBlock);
G
Ganlin Zhao 已提交
682

683
        SColumnInfoData* pResColData = taosArrayGet(pResult->pDataBlock, outputSlotId);
684
        SColumnInfoData  idata = {.info = pResColData->info, .hasNull = true};
H
Haojun Liao 已提交
685

686
        SScalarParam dest = {.columnData = &idata};
X
Xiaoyu Wang 已提交
687
        int32_t      code = scalarCalculate((SNode*)pExpr[k].pExpr->_function.pFunctNode, pBlockList, &dest);
688 689 690 691
        if (code != TSDB_CODE_SUCCESS) {
          taosArrayDestroy(pBlockList);
          return code;
        }
692

dengyihao's avatar
dengyihao 已提交
693
        int32_t startOffset = createNewColModel ? 0 : pResult->info.rows;
694
        ASSERT(pResult->info.capacity > 0);
695
        colDataMergeCol(pResColData, startOffset, (int32_t*)&pResult->info.capacity, &idata, dest.numOfRows);
D
dapan1121 已提交
696
        colDataDestroy(&idata);
697 698

        numOfRows = dest.numOfRows;
H
Haojun Liao 已提交
699 700
        taosArrayDestroy(pBlockList);
      }
701
    } else {
702
      return TSDB_CODE_OPS_NOT_SUPPORT;
703 704
    }
  }
705

706 707 708
  if (!createNewColModel) {
    pResult->info.rows += numOfRows;
  }
709 710

  return TSDB_CODE_SUCCESS;
711 712
}

5
54liuyao 已提交
713
bool functionNeedToExecute(SqlFunctionCtx* pCtx) {
714
  struct SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
715

716 717 718 719 720
  // in case of timestamp column, always generated results.
  int32_t functionId = pCtx->functionId;
  if (functionId == -1) {
    return false;
  }
721

722 723
  if (pCtx->scanFlag == REPEAT_SCAN) {
    return fmIsRepeatScanFunc(pCtx->functionId);
724 725
  }

726 727
  if (isRowEntryCompleted(pResInfo)) {
    return false;
728 729
  }

730 731 732
  return true;
}

733 734 735 736 737 738 739
static int32_t doCreateConstantValColumnAggInfo(SInputColumnInfoData* pInput, SFunctParam* pFuncParam, int32_t type,
                                                int32_t paramIndex, int32_t numOfRows) {
  if (pInput->pData[paramIndex] == NULL) {
    pInput->pData[paramIndex] = taosMemoryCalloc(1, sizeof(SColumnInfoData));
    if (pInput->pData[paramIndex] == NULL) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
740

741 742 743
    // Set the correct column info (data type and bytes)
    pInput->pData[paramIndex]->info.type = type;
    pInput->pData[paramIndex]->info.bytes = tDataTypes[type].bytes;
744
  }
H
Haojun Liao 已提交
745

746 747 748 749 750 751
  SColumnDataAgg* da = NULL;
  if (pInput->pColumnDataAgg[paramIndex] == NULL) {
    da = taosMemoryCalloc(1, sizeof(SColumnDataAgg));
    pInput->pColumnDataAgg[paramIndex] = da;
    if (da == NULL) {
      return TSDB_CODE_OUT_OF_MEMORY;
752 753
    }
  } else {
754
    da = pInput->pColumnDataAgg[paramIndex];
755 756
  }

757
  ASSERT(!IS_VAR_DATA_TYPE(type));
758

759 760
  if (type == TSDB_DATA_TYPE_BIGINT) {
    int64_t v = pFuncParam->param.i;
761
    *da = (SColumnDataAgg){.numOfNull = 0, .min = v, .max = v, .sum = v * numOfRows};
762 763
  } else if (type == TSDB_DATA_TYPE_DOUBLE) {
    double v = pFuncParam->param.d;
764
    *da = (SColumnDataAgg){.numOfNull = 0};
765

766 767 768 769 770 771
    *(double*)&da->min = v;
    *(double*)&da->max = v;
    *(double*)&da->sum = v * numOfRows;
  } else if (type == TSDB_DATA_TYPE_BOOL) {  // todo validate this data type
    bool v = pFuncParam->param.i;

772
    *da = (SColumnDataAgg){.numOfNull = 0};
773 774 775 776 777
    *(bool*)&da->min = 0;
    *(bool*)&da->max = v;
    *(bool*)&da->sum = v * numOfRows;
  } else if (type == TSDB_DATA_TYPE_TIMESTAMP) {
    // do nothing
778
  } else {
779
    ASSERT(0);
780 781
  }

782 783
  return TSDB_CODE_SUCCESS;
}
784

785
void setBlockSMAInfo(SqlFunctionCtx* pCtx, SExprInfo* pExprInfo, SSDataBlock* pBlock) {
786 787 788 789 790 791 792 793 794
  int32_t numOfRows = pBlock->info.rows;

  SInputColumnInfoData* pInput = &pCtx->input;
  pInput->numOfRows = numOfRows;
  pInput->totalRows = numOfRows;

  if (pBlock->pBlockAgg != NULL) {
    pInput->colDataAggIsSet = true;

795 796
    for (int32_t j = 0; j < pExprInfo->base.numOfParams; ++j) {
      SFunctParam* pFuncParam = &pExprInfo->base.pParam[j];
797

798 799
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
        int32_t slotId = pFuncParam->pCol->slotId;
800 801 802 803
        pInput->pColumnDataAgg[j] = pBlock->pBlockAgg[slotId];
        if (pInput->pColumnDataAgg[j] == NULL) {
          pInput->colDataAggIsSet = false;
        }
804 805 806 807

        // Here we set the column info data since the data type for each column data is required, but
        // the data in the corresponding SColumnInfoData will not be used.
        pInput->pData[j] = taosArrayGet(pBlock->pDataBlock, slotId);
808 809
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
        doCreateConstantValColumnAggInfo(pInput, pFuncParam, pFuncParam->param.nType, j, pBlock->info.rows);
810 811
      }
    }
812
  } else {
813
    pInput->colDataAggIsSet = false;
814 815 816
  }
}

L
Liu Jicong 已提交
817
bool isTaskKilled(SExecTaskInfo* pTaskInfo) {
818 819
  // query has been executed more than tsShellActivityTimer, and the retrieve has not arrived
  // abort current query execution.
L
Liu Jicong 已提交
820 821
  if (pTaskInfo->owner != 0 &&
      ((taosGetTimestampSec() - pTaskInfo->cost.start / 1000) > 10 * getMaximumIdleDurationSec())
822
    /*(!needBuildResAfterQueryComplete(pTaskInfo))*/) {
823
    assert(pTaskInfo->cost.start != 0);
L
Liu Jicong 已提交
824 825 826
    //    qDebug("QInfo:%" PRIu64 " retrieve not arrive beyond %d ms, abort current query execution, start:%" PRId64
    //           ", current:%d", pQInfo->qId, 1, pQInfo->startExecTs, taosGetTimestampSec());
    //    return true;
827 828 829 830 831
  }

  return false;
}

L
Liu Jicong 已提交
832
void setTaskKilled(SExecTaskInfo* pTaskInfo) { pTaskInfo->code = TSDB_CODE_TSC_QUERY_CANCELLED; }
833 834

/////////////////////////////////////////////////////////////////////////////////////////////
835
STimeWindow getAlignQueryTimeWindow(SInterval* pInterval, int32_t precision, int64_t key) {
L
Liu Jicong 已提交
836
  STimeWindow win = {0};
837
  win.skey = taosTimeTruncate(key, pInterval, precision);
838 839

  /*
H
Haojun Liao 已提交
840
   * if the realSkey > INT64_MAX - pInterval->interval, the query duration between
841 842
   * realSkey and realEkey must be less than one interval.Therefore, no need to adjust the query ranges.
   */
843 844 845
  win.ekey = taosTimeAdd(win.skey, pInterval->interval, pInterval->intervalUnit, precision) - 1;
  if (win.ekey < win.skey) {
    win.ekey = INT64_MAX;
846
  }
847 848

  return win;
849 850
}

L
Liu Jicong 已提交
851 852
int32_t loadDataBlockOnDemand(SExecTaskInfo* pTaskInfo, STableScanInfo* pTableScanInfo, SSDataBlock* pBlock,
                              uint32_t* status) {
853
  *status = BLK_DATA_NOT_LOAD;
854

H
Haojun Liao 已提交
855
  pBlock->pDataBlock = NULL;
L
Liu Jicong 已提交
856
  pBlock->pBlockAgg = NULL;
H
Haojun Liao 已提交
857

L
Liu Jicong 已提交
858 859
  //  int64_t groupId = pRuntimeEnv->current->groupIndex;
  //  bool    ascQuery = QUERY_IS_ASC_QUERY(pQueryAttr);
860

H
Haojun Liao 已提交
861
  STaskCostInfo* pCost = &pTaskInfo->cost;
862

863 864
//  pCost->totalBlocks += 1;
//  pCost->totalRows += pBlock->info.rows;
H
Haojun Liao 已提交
865
#if 0
866 867 868
  // Calculate all time windows that are overlapping or contain current data block.
  // If current data block is contained by all possible time window, do not load current data block.
  if (/*pQueryAttr->pFilters || */pQueryAttr->groupbyColumn || pQueryAttr->sw.gap > 0 ||
H
Haojun Liao 已提交
869
      (QUERY_IS_INTERVAL_QUERY(pQueryAttr) && overlapWithTimeWindow(pTaskInfo, &pBlock->info))) {
870
    (*status) = BLK_DATA_DATA_LOAD;
871 872 873
  }

  // check if this data block is required to load
874
  if ((*status) != BLK_DATA_DATA_LOAD) {
875 876 877 878 879 880 881
    bool needFilter = true;

    // the pCtx[i] result is belonged to previous time window since the outputBuf has not been set yet,
    // the filter result may be incorrect. So in case of interval query, we need to set the correct time output buffer
    if (QUERY_IS_INTERVAL_QUERY(pQueryAttr)) {
      SResultRow* pResult = NULL;

H
Haojun Liao 已提交
882
      bool  masterScan = IS_MAIN_SCAN(pRuntimeEnv);
883 884 885 886 887 888
      TSKEY k = ascQuery? pBlock->info.window.skey : pBlock->info.window.ekey;

      STimeWindow win = getActiveTimeWindow(pTableScanInfo->pResultRowInfo, k, pQueryAttr);
      if (pQueryAttr->pointInterpQuery) {
        needFilter = chkWindowOutputBufByKey(pRuntimeEnv, pTableScanInfo->pResultRowInfo, &win, masterScan, &pResult, groupId,
                                    pTableScanInfo->pCtx, pTableScanInfo->numOfOutput,
889
                                    pTableScanInfo->rowEntryInfoOffset);
890 891 892
      } else {
        if (setResultOutputBufByKey(pRuntimeEnv, pTableScanInfo->pResultRowInfo, pBlock->info.uid, &win, masterScan, &pResult, groupId,
                                    pTableScanInfo->pCtx, pTableScanInfo->numOfOutput,
893
                                    pTableScanInfo->rowEntryInfoOffset) != TSDB_CODE_SUCCESS) {
894
          T_LONG_JMP(pRuntimeEnv->env, TSDB_CODE_QRY_OUT_OF_MEMORY);
895 896 897 898
        }
      }
    } else if (pQueryAttr->stableQuery && (!pQueryAttr->tsCompQuery) && (!pQueryAttr->diffQuery)) { // stable aggregate, not interval aggregate or normal column aggregate
      doSetTableGroupOutputBuf(pRuntimeEnv, pTableScanInfo->pResultRowInfo, pTableScanInfo->pCtx,
899
                               pTableScanInfo->rowEntryInfoOffset, pTableScanInfo->numOfOutput,
900 901 902 903 904 905
                               pRuntimeEnv->current->groupIndex);
    }

    if (needFilter) {
      (*status) = doFilterByBlockTimeWindow(pTableScanInfo, pBlock);
    } else {
906
      (*status) = BLK_DATA_DATA_LOAD;
907 908 909 910
    }
  }

  SDataBlockInfo* pBlockInfo = &pBlock->info;
H
Haojun Liao 已提交
911
//  *status = updateBlockLoadStatus(pRuntimeEnv->pQueryAttr, *status);
912

913
  if ((*status) == BLK_DATA_NOT_LOAD || (*status) == BLK_DATA_FILTEROUT) {
914 915
    //qDebug("QInfo:0x%"PRIx64" data block discard, brange:%" PRId64 "-%" PRId64 ", rows:%d", pQInfo->qId, pBlockInfo->window.skey,
//           pBlockInfo->window.ekey, pBlockInfo->rows);
916
    pCost->skipBlocks += 1;
917
  } else if ((*status) == BLK_DATA_SMA_LOAD) {
918 919
    // this function never returns error?
    pCost->loadBlockStatis += 1;
920
//    tsdbRetrieveDatablockSMA(pTableScanInfo->pTsdbReadHandle, &pBlock->pBlockAgg);
921 922

    if (pBlock->pBlockAgg == NULL) {  // data block statistics does not exist, load data block
923
//      pBlock->pDataBlock = tsdbRetrieveDataBlock(pTableScanInfo->pTsdbReadHandle, NULL);
924 925 926
      pCost->totalCheckedRows += pBlock->info.rows;
    }
  } else {
927
    assert((*status) == BLK_DATA_DATA_LOAD);
928 929 930

    // load the data block statistics to perform further filter
    pCost->loadBlockStatis += 1;
931
//    tsdbRetrieveDatablockSMA(pTableScanInfo->pTsdbReadHandle, &pBlock->pBlockAgg);
932 933 934 935 936 937

    if (pQueryAttr->topBotQuery && pBlock->pBlockAgg != NULL) {
      { // set previous window
        if (QUERY_IS_INTERVAL_QUERY(pQueryAttr)) {
          SResultRow* pResult = NULL;

H
Haojun Liao 已提交
938
          bool  masterScan = IS_MAIN_SCAN(pRuntimeEnv);
939 940 941 942 943
          TSKEY k = ascQuery? pBlock->info.window.skey : pBlock->info.window.ekey;

          STimeWindow win = getActiveTimeWindow(pTableScanInfo->pResultRowInfo, k, pQueryAttr);
          if (setResultOutputBufByKey(pRuntimeEnv, pTableScanInfo->pResultRowInfo, pBlock->info.uid, &win, masterScan, &pResult, groupId,
                                      pTableScanInfo->pCtx, pTableScanInfo->numOfOutput,
944
                                      pTableScanInfo->rowEntryInfoOffset) != TSDB_CODE_SUCCESS) {
945
            T_LONG_JMP(pRuntimeEnv->env, TSDB_CODE_QRY_OUT_OF_MEMORY);
946 947 948 949 950 951 952 953 954 955
          }
        }
      }
      bool load = false;
      for (int32_t i = 0; i < pQueryAttr->numOfOutput; ++i) {
        int32_t functionId = pTableScanInfo->pCtx[i].functionId;
        if (functionId == FUNCTION_TOP || functionId == FUNCTION_BOTTOM) {
//          load = topbot_datablock_filter(&pTableScanInfo->pCtx[i], (char*)&(pBlock->pBlockAgg[i].min),
//                                         (char*)&(pBlock->pBlockAgg[i].max));
          if (!load) { // current block has been discard due to filter applied
956
            pCost->skipBlocks += 1;
957 958
            //qDebug("QInfo:0x%"PRIx64" data block discard, brange:%" PRId64 "-%" PRId64 ", rows:%d", pQInfo->qId,
//                   pBlockInfo->window.skey, pBlockInfo->window.ekey, pBlockInfo->rows);
959
            (*status) = BLK_DATA_FILTEROUT;
960 961 962 963 964 965 966
            return TSDB_CODE_SUCCESS;
          }
        }
      }
    }

    // current block has been discard due to filter applied
H
Haojun Liao 已提交
967
//    if (!doFilterByBlockSMA(pRuntimeEnv, pBlock->pBlockAgg, pTableScanInfo->pCtx, pBlockInfo->rows)) {
968
//      pCost->skipBlocks += 1;
969 970
//      qDebug("QInfo:0x%"PRIx64" data block discard, brange:%" PRId64 "-%" PRId64 ", rows:%d", pQInfo->qId, pBlockInfo->window.skey,
//             pBlockInfo->window.ekey, pBlockInfo->rows);
971
//      (*status) = BLK_DATA_FILTEROUT;
972 973 974 975 976
//      return TSDB_CODE_SUCCESS;
//    }

    pCost->totalCheckedRows += pBlockInfo->rows;
    pCost->loadBlocks += 1;
977
//    pBlock->pDataBlock = tsdbRetrieveDataBlock(pTableScanInfo->pTsdbReadHandle, NULL);
978 979 980 981 982
//    if (pBlock->pDataBlock == NULL) {
//      return terrno;
//    }

//    if (pQueryAttr->pFilters != NULL) {
983
//      filterSetColFieldData(pQueryAttr->pFilters, taosArrayGetSize(pBlock->pDataBlock), pBlock->pDataBlock);
984
//    }
985

986 987 988 989
//    if (pQueryAttr->pFilters != NULL || pRuntimeEnv->pTsBuf != NULL) {
//      filterColRowsInDataBlock(pRuntimeEnv, pBlock, ascQuery);
//    }
  }
H
Haojun Liao 已提交
990
#endif
991 992 993
  return TSDB_CODE_SUCCESS;
}

L
Liu Jicong 已提交
994
static void updateTableQueryInfoForReverseScan(STableQueryInfo* pTableQueryInfo) {
995 996 997 998 999
  if (pTableQueryInfo == NULL) {
    return;
  }
}

L
Liu Jicong 已提交
1000
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
1001
  if (status == TASK_NOT_COMPLETED) {
H
Haojun Liao 已提交
1002
    pTaskInfo->status = status;
1003 1004
  } else {
    // QUERY_NOT_COMPLETED is not compatible with any other status, so clear its position first
1005
    CLEAR_QUERY_STATUS(pTaskInfo, TASK_NOT_COMPLETED);
H
Haojun Liao 已提交
1006
    pTaskInfo->status |= status;
1007 1008 1009
  }
}

1010
void setResultRowInitCtx(SResultRow* pResult, SqlFunctionCtx* pCtx, int32_t numOfOutput, int32_t* rowEntryInfoOffset) {
5
54liuyao 已提交
1011
  bool init = false;
1012
  for (int32_t i = 0; i < numOfOutput; ++i) {
1013
    pCtx[i].resultInfo = getResultEntryInfo(pResult, i, rowEntryInfoOffset);
5
54liuyao 已提交
1014 1015 1016
    if (init) {
      continue;
    }
1017 1018 1019 1020 1021

    struct SResultRowEntryInfo* pResInfo = pCtx[i].resultInfo;
    if (isRowEntryCompleted(pResInfo) && isRowEntryInitialized(pResInfo)) {
      continue;
    }
1022 1023 1024 1025 1026

    if (fmIsWindowPseudoColumnFunc(pCtx[i].functionId)) {
      continue;
    }

1027 1028 1029 1030 1031 1032
    if (!pResInfo->initialized) {
      if (pCtx[i].functionId != -1) {
        pCtx[i].fpSet.init(&pCtx[i], pResInfo);
      } else {
        pResInfo->initialized = true;
      }
5
54liuyao 已提交
1033 1034
    } else {
      init = true;
1035 1036 1037 1038
    }
  }
}

1039 1040
static void extractQualifiedTupleByFilterResult(SSDataBlock* pBlock, const SColumnInfoData* p, bool keep,
                                                int32_t status);
1041

H
Haojun Liao 已提交
1042 1043
void doFilter(SSDataBlock* pBlock, SFilterInfo* pFilterInfo, SColMatchInfo* pColMatchInfo) {
  if (pFilterInfo == NULL || pBlock->info.rows == 0) {
S
shenglian zhou 已提交
1044 1045
    return;
  }
1046

1047
  SFilterColumnParam param1 = {.numOfCols = taosArrayGetSize(pBlock->pDataBlock), .pDataBlock = pBlock->pDataBlock};
H
Haojun Liao 已提交
1048
  int32_t code = filterSetDataFromSlotId(pFilterInfo, &param1);
1049

1050
  SColumnInfoData* p = NULL;
1051
  int32_t          status = 0;
H
Haojun Liao 已提交
1052

1053
  // todo the keep seems never to be True??
H
Haojun Liao 已提交
1054
  bool keep = filterExecute(pFilterInfo, pBlock, &p, NULL, param1.numOfCols, &status);
1055
  extractQualifiedTupleByFilterResult(pBlock, p, keep, status);
H
Haojun Liao 已提交
1056

1057
  if (pColMatchInfo != NULL) {
H
Haojun Liao 已提交
1058 1059
    size_t  size = taosArrayGetSize(pColMatchInfo->pList);
    for (int32_t i = 0; i < size; ++i) {
H
Haojun Liao 已提交
1060
      SColMatchItem* pInfo = taosArrayGet(pColMatchInfo->pList, i);
1061
      if (pInfo->colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
H
Haojun Liao 已提交
1062
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, pInfo->dstSlotId);
1063
        if (pColData->info.type == TSDB_DATA_TYPE_TIMESTAMP) {
H
Haojun Liao 已提交
1064
          blockDataUpdateTsWindow(pBlock, pInfo->dstSlotId);
1065 1066 1067 1068 1069 1070
          break;
        }
      }
    }
  }

1071 1072
  colDataDestroy(p);
  taosMemoryFree(p);
1073 1074
}

1075
void extractQualifiedTupleByFilterResult(SSDataBlock* pBlock, const SColumnInfoData* p, bool keep, int32_t status) {
1076 1077 1078 1079
  if (keep) {
    return;
  }

H
Haojun Liao 已提交
1080 1081 1082
  int32_t totalRows = pBlock->info.rows;

  if (status == FILTER_RESULT_ALL_QUALIFIED) {
1083
    // here nothing needs to be done
H
Haojun Liao 已提交
1084
  } else if (status == FILTER_RESULT_NONE_QUALIFIED) {
1085
    pBlock->info.rows = 0;
H
Haojun Liao 已提交
1086
  } else {
1087
    SSDataBlock* px = createOneDataBlock(pBlock, true);
1088

1089 1090
    size_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
    for (int32_t i = 0; i < numOfCols; ++i) {
1091 1092
      SColumnInfoData* pSrc = taosArrayGet(px->pDataBlock, i);
      SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, i);
1093
      // it is a reserved column for scalar function, and no data in this column yet.
1094
      if (pDst->pData == NULL || pSrc->pData == NULL) {
1095 1096 1097
        continue;
      }

1098 1099
      colInfoDataCleanup(pDst, pBlock->info.rows);

1100
      int32_t numOfRows = 0;
1101
      for (int32_t j = 0; j < totalRows; ++j) {
1102
        if (((int8_t*)p->pData)[j] == 0) {
D
dapan1121 已提交
1103 1104
          continue;
        }
1105

D
dapan1121 已提交
1106
        if (colDataIsNull_s(pSrc, j)) {
1107
          colDataAppendNULL(pDst, numOfRows);
D
dapan1121 已提交
1108
        } else {
1109
          colDataAppend(pDst, numOfRows, colDataGetData(pSrc, j), false);
D
dapan1121 已提交
1110
        }
1111
        numOfRows += 1;
H
Haojun Liao 已提交
1112
      }
1113

1114
      // todo this value can be assigned directly
1115 1116 1117 1118 1119
      if (pBlock->info.rows == totalRows) {
        pBlock->info.rows = numOfRows;
      } else {
        ASSERT(pBlock->info.rows == numOfRows);
      }
1120
    }
1121

dengyihao's avatar
dengyihao 已提交
1122
    blockDataDestroy(px);  // fix memory leak
1123 1124 1125
  }
}

1126
void doSetTableGroupOutputBuf(SOperatorInfo* pOperator, int32_t numOfOutput, uint64_t groupId) {
1127
  // for simple group by query without interval, all the tables belong to one group result.
1128 1129 1130
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
  SAggOperatorInfo* pAggInfo = pOperator->info;

1131
  SResultRowInfo* pResultRowInfo = &pAggInfo->binfo.resultRowInfo;
1132 1133
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
  int32_t*        rowEntryInfoOffset = pOperator->exprSupp.rowEntryInfoOffset;
1134

1135
  SResultRow* pResultRow = doSetResultOutBufByKey(pAggInfo->aggSup.pResultBuf, pResultRowInfo, (char*)&groupId,
L
Liu Jicong 已提交
1136
                                                  sizeof(groupId), true, groupId, pTaskInfo, false, &pAggInfo->aggSup);
L
Liu Jicong 已提交
1137
  assert(pResultRow != NULL);
1138 1139 1140 1141 1142 1143

  /*
   * not assign result buffer yet, add new result buffer
   * all group belong to one result set, and each group result has different group id so set the id to be one
   */
  if (pResultRow->pageId == -1) {
dengyihao's avatar
dengyihao 已提交
1144 1145
    int32_t ret =
        addNewWindowResultBuf(pResultRow, pAggInfo->aggSup.pResultBuf, groupId, pAggInfo->binfo.pRes->info.rowSize);
1146 1147 1148 1149 1150
    if (ret != TSDB_CODE_SUCCESS) {
      return;
    }
  }

1151
  setResultRowInitCtx(pResultRow, pCtx, numOfOutput, rowEntryInfoOffset);
1152 1153
}

1154 1155 1156
static void setExecutionContext(SOperatorInfo* pOperator, int32_t numOfOutput, uint64_t groupId) {
  SAggOperatorInfo* pAggInfo = pOperator->info;
  if (pAggInfo->groupId != UINT64_MAX && pAggInfo->groupId == groupId) {
1157 1158
    return;
  }
1159 1160

  doSetTableGroupOutputBuf(pOperator, numOfOutput, groupId);
1161 1162

  // record the current active group id
H
Haojun Liao 已提交
1163
  pAggInfo->groupId = groupId;
1164 1165
}

dengyihao's avatar
dengyihao 已提交
1166 1167
static void doUpdateNumOfRows(SqlFunctionCtx* pCtx, SResultRow* pRow, int32_t numOfExprs,
                              const int32_t* rowCellOffset) {
1168
  bool returnNotNull = false;
1169
  for (int32_t j = 0; j < numOfExprs; ++j) {
1170
    struct SResultRowEntryInfo* pResInfo = getResultEntryInfo(pRow, j, rowCellOffset);
1171 1172 1173 1174 1175 1176 1177
    if (!isRowEntryInitialized(pResInfo)) {
      continue;
    }

    if (pRow->numOfRows < pResInfo->numOfRes) {
      pRow->numOfRows = pResInfo->numOfRes;
    }
1178

1179
    if (fmIsNotNullOutputFunc(pCtx[j].functionId)) {
1180 1181
      returnNotNull = true;
    }
1182
  }
S
shenglian zhou 已提交
1183 1184
  // if all expr skips all blocks, e.g. all null inputs for max function, output one row in final result.
  //  except for first/last, which require not null output, output no rows
1185
  if (pRow->numOfRows == 0 && !returnNotNull) {
1186
    pRow->numOfRows = 1;
1187 1188 1189
  }
}

1190 1191
static void doCopyResultToDataBlock(SExprInfo* pExprInfo, int32_t numOfExprs, SResultRow* pRow, SqlFunctionCtx* pCtx,
                                    SSDataBlock* pBlock, const int32_t* rowEntryOffset, SExecTaskInfo* pTaskInfo) {
1192 1193 1194
  for (int32_t j = 0; j < numOfExprs; ++j) {
    int32_t slotId = pExprInfo[j].base.resSchema.slotId;

1195
    pCtx[j].resultInfo = getResultEntryInfo(pRow, j, rowEntryOffset);
1196
    if (pCtx[j].fpSet.finalize) {
1197
      if (strcmp(pCtx[j].pExpr->pExpr->_function.functionName, "_group_key") == 0) {
1198 1199
        // for groupkey along with functions that output multiple lines(e.g. Histogram)
        // need to match groupkey result for each output row of that function.
1200 1201 1202 1203 1204
        if (pCtx[j].resultInfo->numOfRes != 0) {
          pCtx[j].resultInfo->numOfRes = pRow->numOfRows;
        }
      }

1205 1206 1207
      int32_t code = pCtx[j].fpSet.finalize(&pCtx[j], pBlock);
      if (TAOS_FAILED(code)) {
        qError("%s build result data block error, code %s", GET_TASKID(pTaskInfo), tstrerror(code));
1208
        T_LONG_JMP(pTaskInfo->env, code);
1209 1210
      }
    } else if (strcmp(pCtx[j].pExpr->pExpr->_function.functionName, "_select_value") == 0) {
1211
      // do nothing
1212
    } else {
1213 1214
      // expand the result into multiple rows. E.g., _wstart, top(k, 20)
      // the _wstart needs to copy to 20 following rows, since the results of top-k expands to 20 different rows.
1215 1216 1217 1218 1219 1220 1221
      SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, slotId);
      char*            in = GET_ROWCELL_INTERBUF(pCtx[j].resultInfo);
      for (int32_t k = 0; k < pRow->numOfRows; ++k) {
        colDataAppend(pColInfoData, pBlock->info.rows + k, in, pCtx[j].resultInfo->isNullRes);
      }
    }
  }
1222 1223
}

1224 1225 1226
// todo refactor. SResultRow has direct pointer in miainfo
int32_t finalizeResultRows(SDiskbasedBuf* pBuf, SResultRowPosition* resultRowPosition, SExprSupp* pSup,
                           SSDataBlock* pBlock, SExecTaskInfo* pTaskInfo) {
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
  SFilePage*  page = getBufPage(pBuf, resultRowPosition->pageId);
  SResultRow* pRow = (SResultRow*)((char*)page + resultRowPosition->offset);

  SqlFunctionCtx* pCtx = pSup->pCtx;
  SExprInfo*      pExprInfo = pSup->pExprInfo;
  const int32_t*  rowEntryOffset = pSup->rowEntryInfoOffset;

  doUpdateNumOfRows(pCtx, pRow, pSup->numOfExprs, rowEntryOffset);
  if (pRow->numOfRows == 0) {
    releaseBufPage(pBuf, page);
    return 0;
  }

  int32_t size = pBlock->info.capacity;
  while (pBlock->info.rows + pRow->numOfRows > size) {
    size = size * 1.25;
  }

  int32_t code = blockDataEnsureCapacity(pBlock, size);
  if (TAOS_FAILED(code)) {
    releaseBufPage(pBuf, page);
    qError("%s ensure result data capacity failed, code %s", GET_TASKID(pTaskInfo), tstrerror(code));
    T_LONG_JMP(pTaskInfo->env, code);
  }

  doCopyResultToDataBlock(pExprInfo, pSup->numOfExprs, pRow, pCtx, pBlock, rowEntryOffset, pTaskInfo);
1253 1254

  releaseBufPage(pBuf, page);
1255
  pBlock->info.rows += pRow->numOfRows;
1256 1257 1258
  return 0;
}

1259 1260 1261 1262 1263 1264 1265
int32_t doCopyToSDataBlock(SExecTaskInfo* pTaskInfo, SSDataBlock* pBlock, SExprSupp* pSup, SDiskbasedBuf* pBuf,
                           SGroupResInfo* pGroupResInfo) {
  SExprInfo*      pExprInfo = pSup->pExprInfo;
  int32_t         numOfExprs = pSup->numOfExprs;
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
  SqlFunctionCtx* pCtx = pSup->pCtx;

1266
  int32_t numOfRows = getNumOfTotalRes(pGroupResInfo);
1267

1268
  for (int32_t i = pGroupResInfo->index; i < numOfRows; i += 1) {
L
Liu Jicong 已提交
1269 1270
    SResKeyPos* pPos = taosArrayGetP(pGroupResInfo->pRows, i);
    SFilePage*  page = getBufPage(pBuf, pPos->pos.pageId);
1271

1272
    SResultRow* pRow = (SResultRow*)((char*)page + pPos->pos.offset);
1273

H
Haojun Liao 已提交
1274
    doUpdateNumOfRows(pCtx, pRow, numOfExprs, rowEntryOffset);
1275 1276

    // no results, continue to check the next one
1277 1278
    if (pRow->numOfRows == 0) {
      pGroupResInfo->index += 1;
1279
      releaseBufPage(pBuf, page);
1280 1281 1282
      continue;
    }

1283 1284 1285 1286 1287
    if (pBlock->info.groupId == 0) {
      pBlock->info.groupId = pPos->groupId;
    } else {
      // current value belongs to different group, it can't be packed into one datablock
      if (pBlock->info.groupId != pPos->groupId) {
1288
        releaseBufPage(pBuf, page);
1289 1290 1291 1292
        break;
      }
    }

1293
    if (pBlock->info.rows + pRow->numOfRows > pBlock->info.capacity) {
1294
      ASSERT(pBlock->info.rows > 0);
1295
      releaseBufPage(pBuf, page);
1296 1297 1298 1299
      break;
    }

    pGroupResInfo->index += 1;
1300
    doCopyResultToDataBlock(pExprInfo, numOfExprs, pRow, pCtx, pBlock, rowEntryOffset, pTaskInfo);
1301

1302
    releaseBufPage(pBuf, page);
1303
    pBlock->info.rows += pRow->numOfRows;
1304 1305
  }

X
Xiaoyu Wang 已提交
1306 1307
  qDebug("%s result generated, rows:%d, groupId:%" PRIu64, GET_TASKID(pTaskInfo), pBlock->info.rows,
         pBlock->info.groupId);
1308

1309
  blockDataUpdateTsWindow(pBlock, 0);
1310 1311 1312
  return 0;
}

1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
void doBuildStreamResBlock(SOperatorInfo* pOperator, SOptrBasicInfo* pbInfo, SGroupResInfo* pGroupResInfo,
                           SDiskbasedBuf* pBuf) {
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
  SSDataBlock*   pBlock = pbInfo->pRes;

  // set output datablock version
  pBlock->info.version = pTaskInfo->version;

  blockDataCleanup(pBlock);
  if (!hasRemainResults(pGroupResInfo)) {
    return;
  }

  // clear the existed group id
  pBlock->info.groupId = 0;
  ASSERT(!pbInfo->mergeResultBlock);
  doCopyToSDataBlock(pTaskInfo, pBlock, &pOperator->exprSupp, pBuf, pGroupResInfo);
  if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_STATE) {
    SStreamStateAggOperatorInfo* pInfo = pOperator->info;

    char* tbname = taosHashGet(pInfo->pGroupIdTbNameMap, &pBlock->info.groupId, sizeof(int64_t));
    if (tbname != NULL) {
      memcpy(pBlock->info.parTbName, tbname, TSDB_TABLE_NAME_LEN);
    } else {
      pBlock->info.parTbName[0] = 0;
    }
  } 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;

    char* tbname = taosHashGet(pInfo->pGroupIdTbNameMap, &pBlock->info.groupId, sizeof(int64_t));
    if (tbname != NULL) {
      memcpy(pBlock->info.parTbName, tbname, TSDB_TABLE_NAME_LEN);
    } else {
      pBlock->info.parTbName[0] = 0;
    }
  }
}

X
Xiaoyu Wang 已提交
1353 1354
void doBuildResultDatablock(SOperatorInfo* pOperator, SOptrBasicInfo* pbInfo, SGroupResInfo* pGroupResInfo,
                            SDiskbasedBuf* pBuf) {
1355
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
1356
  SSDataBlock*   pBlock = pbInfo->pRes;
1357

1358 1359 1360
  // set output datablock version
  pBlock->info.version = pTaskInfo->version;

1361
  blockDataCleanup(pBlock);
1362
  if (!hasRemainResults(pGroupResInfo)) {
1363 1364 1365
    return;
  }

1366 1367
  // clear the existed group id
  pBlock->info.groupId = 0;
1368 1369 1370
  if (!pbInfo->mergeResultBlock) {
    doCopyToSDataBlock(pTaskInfo, pBlock, &pOperator->exprSupp, pBuf, pGroupResInfo);
  } else {
dengyihao's avatar
dengyihao 已提交
1371
    while (hasRemainResults(pGroupResInfo)) {
1372 1373 1374
      doCopyToSDataBlock(pTaskInfo, pBlock, &pOperator->exprSupp, pBuf, pGroupResInfo);
      if (pBlock->info.rows >= pOperator->resultInfo.threshold) {
        break;
1375 1376
      }

1377 1378
      // clearing group id to continue to merge data that belong to different groups
      pBlock->info.groupId = 0;
1379
    }
1380 1381 1382

    // clear the group id info in SSDataBlock, since the client does not need it
    pBlock->info.groupId = 0;
1383 1384 1385
  }
}

L
Liu Jicong 已提交
1386 1387
void queryCostStatis(SExecTaskInfo* pTaskInfo) {
  STaskCostInfo* pSummary = &pTaskInfo->cost;
1388

1389 1390
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
  if (pSummary->pRecoder != NULL) {
1391
    qDebug(
H
Haojun Liao 已提交
1392 1393 1394 1395 1396
        "%s :cost summary: elapsed time:%.2f ms, extract tableList:%.2f ms, createGroupIdMap:%.2f ms, total blocks:%d, "
        "load block SMA:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
        GET_TASKID(pTaskInfo), pSummary->elapsedTime / 1000.0, pSummary->extractListTime, pSummary->groupIdMapTime,
        pRecorder->totalBlocks, pRecorder->loadBlockStatis, pRecorder->loadBlocks, pRecorder->totalRows,
        pRecorder->totalCheckedRows);
1397
  }
1398 1399
}

L
Liu Jicong 已提交
1400 1401 1402
// static void updateOffsetVal(STaskRuntimeEnv *pRuntimeEnv, SDataBlockInfo *pBlockInfo) {
//   STaskAttr *pQueryAttr = pRuntimeEnv->pQueryAttr;
//   STableQueryInfo* pTableQueryInfo = pRuntimeEnv->current;
1403
//
L
Liu Jicong 已提交
1404
//   int32_t step = GET_FORWARD_DIRECTION_FACTOR(pQueryAttr->order.order);
1405
//
L
Liu Jicong 已提交
1406 1407 1408 1409
//   if (pQueryAttr->limit.offset == pBlockInfo->rows) {  // current block will ignore completed
//     pTableQueryInfo->lastKey = QUERY_IS_ASC_QUERY(pQueryAttr) ? pBlockInfo->window.ekey + step :
//     pBlockInfo->window.skey + step; pQueryAttr->limit.offset = 0; return;
//   }
1410
//
L
Liu Jicong 已提交
1411 1412 1413 1414 1415
//   if (QUERY_IS_ASC_QUERY(pQueryAttr)) {
//     pQueryAttr->pos = (int32_t)pQueryAttr->limit.offset;
//   } else {
//     pQueryAttr->pos = pBlockInfo->rows - (int32_t)pQueryAttr->limit.offset - 1;
//   }
1416
//
L
Liu Jicong 已提交
1417
//   assert(pQueryAttr->pos >= 0 && pQueryAttr->pos <= pBlockInfo->rows - 1);
1418
//
L
Liu Jicong 已提交
1419 1420
//   SArray *         pDataBlock = tsdbRetrieveDataBlock(pRuntimeEnv->pTsdbReadHandle, NULL);
//   SColumnInfoData *pColInfoData = taosArrayGet(pDataBlock, 0);
1421
//
L
Liu Jicong 已提交
1422 1423
//   // update the pQueryAttr->limit.offset value, and pQueryAttr->pos value
//   TSKEY *keys = (TSKEY *) pColInfoData->pData;
1424
//
L
Liu Jicong 已提交
1425 1426 1427
//   // update the offset value
//   pTableQueryInfo->lastKey = keys[pQueryAttr->pos];
//   pQueryAttr->limit.offset = 0;
1428
//
L
Liu Jicong 已提交
1429
//   int32_t numOfRes = tableApplyFunctionsOnBlock(pRuntimeEnv, pBlockInfo, NULL, binarySearchForKey, pDataBlock);
1430
//
L
Liu Jicong 已提交
1431 1432 1433 1434
//   //qDebug("QInfo:0x%"PRIx64" check data block, brange:%" PRId64 "-%" PRId64 ", numBlocksOfStep:%d, numOfRes:%d,
//   lastKey:%"PRId64, GET_TASKID(pRuntimeEnv),
//          pBlockInfo->window.skey, pBlockInfo->window.ekey, pBlockInfo->rows, numOfRes, pQuery->current->lastKey);
// }
1435

L
Liu Jicong 已提交
1436 1437
// void skipBlocks(STaskRuntimeEnv *pRuntimeEnv) {
//   STaskAttr *pQueryAttr = pRuntimeEnv->pQueryAttr;
1438
//
L
Liu Jicong 已提交
1439 1440 1441
//   if (pQueryAttr->limit.offset <= 0 || pQueryAttr->numOfFilterCols > 0) {
//     return;
//   }
1442
//
L
Liu Jicong 已提交
1443 1444
//   pQueryAttr->pos = 0;
//   int32_t step = GET_FORWARD_DIRECTION_FACTOR(pQueryAttr->order.order);
1445
//
L
Liu Jicong 已提交
1446 1447
//   STableQueryInfo* pTableQueryInfo = pRuntimeEnv->current;
//   TsdbQueryHandleT pTsdbReadHandle = pRuntimeEnv->pTsdbReadHandle;
1448
//
L
Liu Jicong 已提交
1449 1450 1451
//   SDataBlockInfo blockInfo = SDATA_BLOCK_INITIALIZER;
//   while (tsdbNextDataBlock(pTsdbReadHandle)) {
//     if (isTaskKilled(pRuntimeEnv->qinfo)) {
1452
//       T_LONG_JMP(pRuntimeEnv->env, TSDB_CODE_TSC_QUERY_CANCELLED);
L
Liu Jicong 已提交
1453
//     }
1454
//
L
Liu Jicong 已提交
1455
//     tsdbRetrieveDataBlockInfo(pTsdbReadHandle, &blockInfo);
1456
//
L
Liu Jicong 已提交
1457 1458 1459 1460
//     if (pQueryAttr->limit.offset > blockInfo.rows) {
//       pQueryAttr->limit.offset -= blockInfo.rows;
//       pTableQueryInfo->lastKey = (QUERY_IS_ASC_QUERY(pQueryAttr)) ? blockInfo.window.ekey : blockInfo.window.skey;
//       pTableQueryInfo->lastKey += step;
1461
//
L
Liu Jicong 已提交
1462 1463 1464 1465 1466 1467 1468
//       //qDebug("QInfo:0x%"PRIx64" skip rows:%d, offset:%" PRId64, GET_TASKID(pRuntimeEnv), blockInfo.rows,
//              pQuery->limit.offset);
//     } else {  // find the appropriated start position in current block
//       updateOffsetVal(pRuntimeEnv, &blockInfo);
//       break;
//     }
//   }
1469
//
L
Liu Jicong 已提交
1470
//   if (terrno != TSDB_CODE_SUCCESS) {
1471
//     T_LONG_JMP(pRuntimeEnv->env, terrno);
L
Liu Jicong 已提交
1472 1473 1474 1475 1476 1477 1478
//   }
// }

// static TSKEY doSkipIntervalProcess(STaskRuntimeEnv* pRuntimeEnv, STimeWindow* win, SDataBlockInfo* pBlockInfo,
// STableQueryInfo* pTableQueryInfo) {
//   STaskAttr *pQueryAttr = pRuntimeEnv->pQueryAttr;
//   SResultRowInfo *pWindowResInfo = &pRuntimeEnv->resultRowInfo;
1479
//
L
Liu Jicong 已提交
1480 1481 1482
//   assert(pQueryAttr->limit.offset == 0);
//   STimeWindow tw = *win;
//   getNextTimeWindow(pQueryAttr, &tw);
1483
//
L
Liu Jicong 已提交
1484 1485
//   if ((tw.skey <= pBlockInfo->window.ekey && QUERY_IS_ASC_QUERY(pQueryAttr)) ||
//       (tw.ekey >= pBlockInfo->window.skey && !QUERY_IS_ASC_QUERY(pQueryAttr))) {
1486
//
L
Liu Jicong 已提交
1487 1488 1489 1490
//     // load the data block and check data remaining in current data block
//     // TODO optimize performance
//     SArray *         pDataBlock = tsdbRetrieveDataBlock(pRuntimeEnv->pTsdbReadHandle, NULL);
//     SColumnInfoData *pColInfoData = taosArrayGet(pDataBlock, 0);
1491
//
L
Liu Jicong 已提交
1492 1493 1494 1495
//     tw = *win;
//     int32_t startPos =
//         getNextQualifiedWindow(pQueryAttr, &tw, pBlockInfo, pColInfoData->pData, binarySearchForKey, -1);
//     assert(startPos >= 0);
1496
//
L
Liu Jicong 已提交
1497 1498
//     // set the abort info
//     pQueryAttr->pos = startPos;
1499
//
L
Liu Jicong 已提交
1500 1501 1502 1503
//     // reset the query start timestamp
//     pTableQueryInfo->win.skey = ((TSKEY *)pColInfoData->pData)[startPos];
//     pQueryAttr->window.skey = pTableQueryInfo->win.skey;
//     TSKEY key = pTableQueryInfo->win.skey;
1504
//
L
Liu Jicong 已提交
1505 1506
//     pWindowResInfo->prevSKey = tw.skey;
//     int32_t index = pRuntimeEnv->resultRowInfo.curIndex;
1507
//
L
Liu Jicong 已提交
1508 1509
//     int32_t numOfRes = tableApplyFunctionsOnBlock(pRuntimeEnv, pBlockInfo, NULL, binarySearchForKey, pDataBlock);
//     pRuntimeEnv->resultRowInfo.curIndex = index;  // restore the window index
1510
//
L
Liu Jicong 已提交
1511 1512 1513 1514
//     //qDebug("QInfo:0x%"PRIx64" check data block, brange:%" PRId64 "-%" PRId64 ", numOfRows:%d, numOfRes:%d,
//     lastKey:%" PRId64,
//            GET_TASKID(pRuntimeEnv), pBlockInfo->window.skey, pBlockInfo->window.ekey, pBlockInfo->rows, numOfRes,
//            pQueryAttr->current->lastKey);
1515
//
L
Liu Jicong 已提交
1516 1517 1518 1519 1520
//     return key;
//   } else {  // do nothing
//     pQueryAttr->window.skey      = tw.skey;
//     pWindowResInfo->prevSKey = tw.skey;
//     pTableQueryInfo->lastKey = tw.skey;
1521
//
L
Liu Jicong 已提交
1522 1523
//     return tw.skey;
//   }
1524
//
L
Liu Jicong 已提交
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
//   return true;
// }

// static bool skipTimeInterval(STaskRuntimeEnv *pRuntimeEnv, TSKEY* start) {
//   STaskAttr *pQueryAttr = pRuntimeEnv->pQueryAttr;
//   if (QUERY_IS_ASC_QUERY(pQueryAttr)) {
//     assert(*start <= pRuntimeEnv->current->lastKey);
//   } else {
//     assert(*start >= pRuntimeEnv->current->lastKey);
//   }
1535
//
L
Liu Jicong 已提交
1536 1537 1538 1539 1540
//   // if queried with value filter, do NOT forward query start position
//   if (pQueryAttr->limit.offset <= 0 || pQueryAttr->numOfFilterCols > 0 || pRuntimeEnv->pTsBuf != NULL ||
//   pRuntimeEnv->pFillInfo != NULL) {
//     return true;
//   }
1541
//
L
Liu Jicong 已提交
1542 1543 1544 1545 1546 1547 1548
//   /*
//    * 1. for interval without interpolation query we forward pQueryAttr->interval.interval at a time for
//    *    pQueryAttr->limit.offset times. Since hole exists, pQueryAttr->interval.interval*pQueryAttr->limit.offset
//    value is
//    *    not valid. otherwise, we only forward pQueryAttr->limit.offset number of points
//    */
//   assert(pRuntimeEnv->resultRowInfo.prevSKey == TSKEY_INITIAL_VAL);
1549
//
L
Liu Jicong 已提交
1550 1551
//   STimeWindow w = TSWINDOW_INITIALIZER;
//   bool ascQuery = QUERY_IS_ASC_QUERY(pQueryAttr);
1552
//
L
Liu Jicong 已提交
1553 1554
//   SResultRowInfo *pWindowResInfo = &pRuntimeEnv->resultRowInfo;
//   STableQueryInfo *pTableQueryInfo = pRuntimeEnv->current;
1555
//
L
Liu Jicong 已提交
1556 1557 1558
//   SDataBlockInfo blockInfo = SDATA_BLOCK_INITIALIZER;
//   while (tsdbNextDataBlock(pRuntimeEnv->pTsdbReadHandle)) {
//     tsdbRetrieveDataBlockInfo(pRuntimeEnv->pTsdbReadHandle, &blockInfo);
1559
//
L
Liu Jicong 已提交
1560 1561 1562 1563 1564 1565 1566 1567 1568
//     if (QUERY_IS_ASC_QUERY(pQueryAttr)) {
//       if (pWindowResInfo->prevSKey == TSKEY_INITIAL_VAL) {
//         getAlignQueryTimeWindow(pQueryAttr, blockInfo.window.skey, blockInfo.window.skey, pQueryAttr->window.ekey,
//         &w); pWindowResInfo->prevSKey = w.skey;
//       }
//     } else {
//       getAlignQueryTimeWindow(pQueryAttr, blockInfo.window.ekey, pQueryAttr->window.ekey, blockInfo.window.ekey, &w);
//       pWindowResInfo->prevSKey = w.skey;
//     }
1569
//
L
Liu Jicong 已提交
1570 1571
//     // the first time window
//     STimeWindow win = getActiveTimeWindow(pWindowResInfo, pWindowResInfo->prevSKey, pQueryAttr);
1572
//
L
Liu Jicong 已提交
1573 1574
//     while (pQueryAttr->limit.offset > 0) {
//       STimeWindow tw = win;
1575
//
L
Liu Jicong 已提交
1576 1577 1578
//       if ((win.ekey <= blockInfo.window.ekey && ascQuery) || (win.ekey >= blockInfo.window.skey && !ascQuery)) {
//         pQueryAttr->limit.offset -= 1;
//         pWindowResInfo->prevSKey = win.skey;
1579
//
L
Liu Jicong 已提交
1580 1581 1582 1583 1584 1585
//         // current time window is aligned with blockInfo.window.ekey
//         // restart it from next data block by set prevSKey to be TSKEY_INITIAL_VAL;
//         if ((win.ekey == blockInfo.window.ekey && ascQuery) || (win.ekey == blockInfo.window.skey && !ascQuery)) {
//           pWindowResInfo->prevSKey = TSKEY_INITIAL_VAL;
//         }
//       }
1586
//
L
Liu Jicong 已提交
1587 1588 1589 1590
//       if (pQueryAttr->limit.offset == 0) {
//         *start = doSkipIntervalProcess(pRuntimeEnv, &win, &blockInfo, pTableQueryInfo);
//         return true;
//       }
1591
//
L
Liu Jicong 已提交
1592 1593
//       // current window does not ended in current data block, try next data block
//       getNextTimeWindow(pQueryAttr, &tw);
1594
//
L
Liu Jicong 已提交
1595 1596 1597 1598 1599 1600 1601 1602 1603
//       /*
//        * If the next time window still starts from current data block,
//        * load the primary timestamp column first, and then find the start position for the next queried time window.
//        * Note that only the primary timestamp column is required.
//        * TODO: Optimize for this cases. All data blocks are not needed to be loaded, only if the first actually
//        required
//        * time window resides in current data block.
//        */
//       if ((tw.skey <= blockInfo.window.ekey && ascQuery) || (tw.ekey >= blockInfo.window.skey && !ascQuery)) {
1604
//
L
Liu Jicong 已提交
1605 1606
//         SArray *pDataBlock = tsdbRetrieveDataBlock(pRuntimeEnv->pTsdbReadHandle, NULL);
//         SColumnInfoData *pColInfoData = taosArrayGet(pDataBlock, 0);
1607
//
L
Liu Jicong 已提交
1608 1609 1610
//         if ((win.ekey > blockInfo.window.ekey && ascQuery) || (win.ekey < blockInfo.window.skey && !ascQuery)) {
//           pQueryAttr->limit.offset -= 1;
//         }
1611
//
L
Liu Jicong 已提交
1612 1613 1614 1615 1616 1617 1618 1619
//         if (pQueryAttr->limit.offset == 0) {
//           *start = doSkipIntervalProcess(pRuntimeEnv, &win, &blockInfo, pTableQueryInfo);
//           return true;
//         } else {
//           tw = win;
//           int32_t startPos =
//               getNextQualifiedWindow(pQueryAttr, &tw, &blockInfo, pColInfoData->pData, binarySearchForKey, -1);
//           assert(startPos >= 0);
1620
//
L
Liu Jicong 已提交
1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
//           // set the abort info
//           pQueryAttr->pos = startPos;
//           pTableQueryInfo->lastKey = ((TSKEY *)pColInfoData->pData)[startPos];
//           pWindowResInfo->prevSKey = tw.skey;
//           win = tw;
//         }
//       } else {
//         break;  // offset is not 0, and next time window begins or ends in the next block.
//       }
//     }
//   }
1632
//
L
Liu Jicong 已提交
1633 1634
//   // check for error
//   if (terrno != TSDB_CODE_SUCCESS) {
1635
//     T_LONG_JMP(pRuntimeEnv->env, terrno);
L
Liu Jicong 已提交
1636
//   }
1637
//
L
Liu Jicong 已提交
1638 1639
//   return true;
// }
1640

1641
int32_t appendDownstream(SOperatorInfo* p, SOperatorInfo** pDownstream, int32_t num) {
H
Haojun Liao 已提交
1642
  if (p->pDownstream == NULL) {
H
Haojun Liao 已提交
1643
    assert(p->numOfDownstream == 0);
1644 1645
  }

wafwerar's avatar
wafwerar 已提交
1646
  p->pDownstream = taosMemoryCalloc(1, num * POINTER_BYTES);
1647 1648 1649 1650 1651 1652 1653
  if (p->pDownstream == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  memcpy(p->pDownstream, pDownstream, num * POINTER_BYTES);
  p->numOfDownstream = num;
  return TSDB_CODE_SUCCESS;
1654 1655
}

1656 1657 1658 1659
typedef struct SFetchRspHandleWrapper {
  uint32_t exchangeId;
  int32_t  sourceIndex;
} SFetchRspHandleWrapper;
1660

D
dapan1121 已提交
1661
int32_t loadRemoteDataCallback(void* param, SDataBuf* pMsg, int32_t code) {
X
Xiaoyu Wang 已提交
1662
  SFetchRspHandleWrapper* pWrapper = (SFetchRspHandleWrapper*)param;
1663 1664 1665 1666

  SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pWrapper->exchangeId);
  if (pExchangeInfo == NULL) {
    qWarn("failed to acquire exchange operator, since it may have been released");
1667
    taosMemoryFree(pMsg->pData);
1668 1669 1670
    return TSDB_CODE_SUCCESS;
  }

X
Xiaoyu Wang 已提交
1671
  int32_t          index = pWrapper->sourceIndex;
1672
  SSourceDataInfo* pSourceDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, index);
1673

H
Haojun Liao 已提交
1674 1675
  if (code == TSDB_CODE_SUCCESS) {
    pSourceDataInfo->pRsp = pMsg->pData;
1676

H
Haojun Liao 已提交
1677 1678
    SRetrieveTableRsp* pRsp = pSourceDataInfo->pRsp;
    pRsp->numOfRows = htonl(pRsp->numOfRows);
dengyihao's avatar
dengyihao 已提交
1679
    pRsp->compLen = htonl(pRsp->compLen);
1680
    pRsp->numOfCols = htonl(pRsp->numOfCols);
dengyihao's avatar
dengyihao 已提交
1681
    pRsp->useconds = htobe64(pRsp->useconds);
1682
    pRsp->numOfBlocks = htonl(pRsp->numOfBlocks);
1683

1684
    ASSERT(pRsp != NULL);
H
Haojun Liao 已提交
1685 1686
    qDebug("%s fetch rsp received, index:%d, blocks:%d, rows:%d", pSourceDataInfo->taskId, index, pRsp->numOfBlocks,
           pRsp->numOfRows);
H
Haojun Liao 已提交
1687
  } else {
1688
    taosMemoryFree(pMsg->pData);
H
Haojun Liao 已提交
1689
    pSourceDataInfo->code = code;
1690
    qDebug("%s fetch rsp received, index:%d, error:%s", pSourceDataInfo->taskId, index, tstrerror(code));
H
Haojun Liao 已提交
1691
  }
H
Haojun Liao 已提交
1692

H
Haojun Liao 已提交
1693
  pSourceDataInfo->status = EX_SOURCE_DATA_READY;
1694 1695 1696 1697

  tsem_post(&pExchangeInfo->ready);
  taosReleaseRef(exchangeObjRefPool, pWrapper->exchangeId);

wmmhello's avatar
wmmhello 已提交
1698
  return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
1699 1700
}

D
dapan1121 已提交
1701
void qProcessRspMsg(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
S
Shengliang Guan 已提交
1702 1703
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
  assert(pMsg->info.ahandle != NULL);
H
Haojun Liao 已提交
1704 1705 1706 1707

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

  if (pMsg->contLen > 0) {
wafwerar's avatar
wafwerar 已提交
1708
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
H
Haojun Liao 已提交
1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719
    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);
1720 1721
}

L
Liu Jicong 已提交
1722
static int32_t doSendFetchDataRequest(SExchangeInfo* pExchangeInfo, SExecTaskInfo* pTaskInfo, int32_t sourceIndex) {
1723
  size_t totalSources = taosArrayGetSize(pExchangeInfo->pSources);
1724

L
Liu Jicong 已提交
1725 1726
  SDownstreamSourceNode* pSource = taosArrayGet(pExchangeInfo->pSources, sourceIndex);
  SSourceDataInfo*       pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, sourceIndex);
1727

1728 1729
  ASSERT(pDataInfo->status == EX_SOURCE_DATA_NOT_READY);

1730
  SFetchRspHandleWrapper* pWrapper = taosMemoryCalloc(1, sizeof(SFetchRspHandleWrapper));
X
Xiaoyu Wang 已提交
1731
  pWrapper->exchangeId = pExchangeInfo->self;
1732 1733
  pWrapper->sourceIndex = sourceIndex;

D
dapan1121 已提交
1734 1735
  if (pSource->localExec) {
    SDataBuf pBuf = {0};
1736 1737 1738
    int32_t  code =
        (*pTaskInfo->localFetch.fp)(pTaskInfo->localFetch.handle, pSource->schedId, pTaskInfo->id.queryId,
                                    pSource->taskId, 0, pSource->execId, &pBuf.pData, pTaskInfo->localFetch.explainRes);
D
dapan1121 已提交
1739
    loadRemoteDataCallback(pWrapper, &pBuf, code);
D
dapan1121 已提交
1740
    taosMemoryFree(pWrapper);
D
dapan1121 已提交
1741
  } else {
D
dapan1121 已提交
1742 1743 1744
    SResFetchReq* pMsg = taosMemoryCalloc(1, sizeof(SResFetchReq));
    if (NULL == pMsg) {
      pTaskInfo->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
H
Haojun Liao 已提交
1745
      taosMemoryFree(pWrapper);
D
dapan1121 已提交
1746 1747 1748
      return pTaskInfo->code;
    }

D
dapan1121 已提交
1749
    qDebug("%s build fetch msg and send to vgId:%d, ep:%s, taskId:0x%" PRIx64 ", execId:%d, %d/%" PRIzu,
1750 1751
           GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->addr.epSet.eps[0].fqdn, pSource->taskId,
           pSource->execId, sourceIndex, totalSources);
D
dapan1121 已提交
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762

    pMsg->header.vgId = htonl(pSource->addr.nodeId);
    pMsg->sId = htobe64(pSource->schedId);
    pMsg->taskId = htobe64(pSource->taskId);
    pMsg->queryId = htobe64(pTaskInfo->id.queryId);
    pMsg->execId = htonl(pSource->execId);

    // send the fetch remote task result reques
    SMsgSendInfo* pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
    if (NULL == pMsgSendInfo) {
      taosMemoryFreeClear(pMsg);
H
Haojun Liao 已提交
1763
      taosMemoryFree(pWrapper);
D
dapan1121 已提交
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
      qError("%s prepare message %d failed", GET_TASKID(pTaskInfo), (int32_t)sizeof(SMsgSendInfo));
      pTaskInfo->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
      return pTaskInfo->code;
    }

    pMsgSendInfo->param = pWrapper;
    pMsgSendInfo->paramFreeFp = taosMemoryFree;
    pMsgSendInfo->msgInfo.pData = pMsg;
    pMsgSendInfo->msgInfo.len = sizeof(SResFetchReq);
    pMsgSendInfo->msgType = pSource->fetchMsgType;
    pMsgSendInfo->fp = loadRemoteDataCallback;
1775

D
dapan1121 已提交
1776
    int64_t transporterId = 0;
1777 1778
    int32_t code =
        asyncSendMsgToServer(pExchangeInfo->pTransporter, &pSource->addr.epSet, &transporterId, pMsgSendInfo);
D
dapan1121 已提交
1779
  }
1780

1781 1782 1783
  return TSDB_CODE_SUCCESS;
}

1784 1785 1786 1787 1788 1789 1790 1791
void updateLoadRemoteInfo(SLoadRemoteDataInfo* pInfo, int32_t numOfRows, int32_t dataLen, int64_t startTs,
                          SOperatorInfo* pOperator) {
  pInfo->totalRows += numOfRows;
  pInfo->totalSize += dataLen;
  pInfo->totalElapsed += (taosGetTimestampUs() - startTs);
  pOperator->resultInfo.totalRows += numOfRows;
}

H
Haojun Liao 已提交
1792
int32_t extractDataBlockFromFetchRsp(SSDataBlock* pRes, char* pData, SArray* pColList, char** pNextStart) {
H
Haojun Liao 已提交
1793
  if (pColList == NULL) {  // data from other sources
1794
    blockDataCleanup(pRes);
dengyihao's avatar
dengyihao 已提交
1795
    *pNextStart = (char*)blockDecode(pRes, pData);
H
Haojun Liao 已提交
1796
  } else {  // extract data according to pColList
1797 1798 1799 1800 1801
    char* pStart = pData;

    int32_t numOfCols = htonl(*(int32_t*)pStart);
    pStart += sizeof(int32_t);

1802
    // todo refactor:extract method
1803
    SSysTableSchema* pSchema = (SSysTableSchema*)pStart;
dengyihao's avatar
dengyihao 已提交
1804
    for (int32_t i = 0; i < numOfCols; ++i) {
1805 1806 1807 1808 1809 1810 1811
      SSysTableSchema* p = (SSysTableSchema*)pStart;

      p->colId = htons(p->colId);
      p->bytes = htonl(p->bytes);
      pStart += sizeof(SSysTableSchema);
    }

1812
    SSDataBlock* pBlock = createDataBlock();
dengyihao's avatar
dengyihao 已提交
1813
    for (int32_t i = 0; i < numOfCols; ++i) {
1814 1815
      SColumnInfoData idata = createColumnInfoData(pSchema[i].type, pSchema[i].bytes, pSchema[i].colId);
      blockDataAppendColInfo(pBlock, &idata);
1816 1817
    }

1818
    blockDecode(pBlock, pStart);
1819
    blockDataEnsureCapacity(pRes, pBlock->info.rows);
1820

H
Haojun Liao 已提交
1821
    // data from mnode
1822
    pRes->info.rows = pBlock->info.rows;
1823 1824
    relocateColumnData(pRes, pColList, pBlock->pDataBlock, false);
    blockDataDestroy(pBlock);
1825
  }
1826

1827 1828
  // todo move this to time window aggregator, since the primary timestamp may not be known by exchange operator.
  blockDataUpdateTsWindow(pRes, 0);
1829 1830
  return TSDB_CODE_SUCCESS;
}
1831

L
Liu Jicong 已提交
1832 1833
static void* setAllSourcesCompleted(SOperatorInfo* pOperator, int64_t startTs) {
  SExchangeInfo* pExchangeInfo = pOperator->info;
1834
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
H
Haojun Liao 已提交
1835

1836
  int64_t              el = taosGetTimestampUs() - startTs;
H
Haojun Liao 已提交
1837
  SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
1838

H
Haojun Liao 已提交
1839
  pLoadInfo->totalElapsed += el;
H
Haojun Liao 已提交
1840

1841
  size_t totalSources = taosArrayGetSize(pExchangeInfo->pSources);
L
Liu Jicong 已提交
1842 1843 1844
  qDebug("%s all %" PRIzu " sources are exhausted, total rows: %" PRIu64 " bytes:%" PRIu64 ", elapsed:%.2f ms",
         GET_TASKID(pTaskInfo), totalSources, pLoadInfo->totalRows, pLoadInfo->totalSize,
         pLoadInfo->totalElapsed / 1000.0);
1845 1846 1847 1848 1849

  doSetOperatorCompleted(pOperator);
  return NULL;
}

1850 1851
static void concurrentlyLoadRemoteDataImpl(SOperatorInfo* pOperator, SExchangeInfo* pExchangeInfo,
                                           SExecTaskInfo* pTaskInfo) {
1852 1853 1854 1855 1856
  int32_t code = 0;
  int64_t startTs = taosGetTimestampUs();
  size_t  totalSources = taosArrayGetSize(pExchangeInfo->pSources);

  while (1) {
1857 1858
    tsem_wait(&pExchangeInfo->ready);

1859 1860 1861
    int32_t completed = 0;
    for (int32_t i = 0; i < totalSources; ++i) {
      SSourceDataInfo* pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, i);
1862
      if (pDataInfo->status == EX_SOURCE_DATA_EXHAUSTED) {
1863
        completed += 1;
H
Haojun Liao 已提交
1864 1865
        continue;
      }
1866

1867
      if (pDataInfo->status != EX_SOURCE_DATA_READY) {
1868 1869 1870
        continue;
      }

1871 1872 1873 1874 1875
      if (pDataInfo->code != TSDB_CODE_SUCCESS) {
        code = pDataInfo->code;
        goto _error;
      }

L
Liu Jicong 已提交
1876
      SRetrieveTableRsp*     pRsp = pDataInfo->pRsp;
X
Xiaoyu Wang 已提交
1877
      SDownstreamSourceNode* pSource = taosArrayGet(pExchangeInfo->pSources, i);
1878

1879
      // todo
H
Haojun Liao 已提交
1880
      SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
1881
      if (pRsp->numOfRows == 0) {
1882
        qDebug("%s vgId:%d, taskId:0x%" PRIx64 " execId:%d index:%d completed, rowsOfSource:%" PRIu64
1883
                   ", totalRows:%" PRIu64 ", completed:%d try next %d/%" PRIzu,
D
dapan1121 已提交
1884
               GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->taskId, pSource->execId, i, pDataInfo->totalRows,
1885
               pExchangeInfo->loadInfo.totalRows, completed + 1, i + 1, totalSources);
1886
        pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
1887
        completed += 1;
D
dapan1121 已提交
1888
        taosMemoryFreeClear(pDataInfo->pRsp);
1889 1890
        break;
//        continue;
1891
      }
H
Haojun Liao 已提交
1892

1893
      SRetrieveTableRsp* pRetrieveRsp = pDataInfo->pRsp;
dengyihao's avatar
dengyihao 已提交
1894 1895 1896
      int32_t            index = 0;
      char*              pStart = pRetrieveRsp->data;
      while (index++ < pRetrieveRsp->numOfBlocks) {
1897
        SSDataBlock* pb = createOneDataBlock(pExchangeInfo->pDummyBlock, false);
H
Haojun Liao 已提交
1898
        code = extractDataBlockFromFetchRsp(pb, pStart, NULL, &pStart);
1899 1900 1901 1902 1903 1904
        if (code != 0) {
          taosMemoryFreeClear(pDataInfo->pRsp);
          goto _error;
        }

        taosArrayPush(pExchangeInfo->pResultBlockList, &pb);
1905 1906
      }

1907
      updateLoadRemoteInfo(pLoadInfo, pRetrieveRsp->numOfRows, pRetrieveRsp->compLen, startTs, pOperator);
1908

1909
      if (pRsp->completed == 1) {
dengyihao's avatar
dengyihao 已提交
1910
        qDebug("%s fetch msg rsp from vgId:%d, taskId:0x%" PRIx64
1911 1912
               " execId:%d index:%d completed, blocks:%d, numOfRows:%d, rowsOfSource:%" PRIu64 ", totalRows:%" PRIu64
               ", total:%.2f Kb, completed:%d try next %d/%" PRIzu,
H
Haojun Liao 已提交
1913 1914 1915
               GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->taskId, pSource->execId, i, pRsp->numOfBlocks,
               pRsp->numOfRows, pDataInfo->totalRows, pLoadInfo->totalRows, pLoadInfo->totalSize / 1024.0,
               completed + 1, i + 1, totalSources);
1916
        completed += 1;
1917
        pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
1918
      } else {
dengyihao's avatar
dengyihao 已提交
1919
        qDebug("%s fetch msg rsp from vgId:%d, taskId:0x%" PRIx64
1920
               " execId:%d blocks:%d, numOfRows:%d, totalRows:%" PRIu64 ", total:%.2f Kb",
dengyihao's avatar
dengyihao 已提交
1921 1922
               GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->taskId, pSource->execId, pRsp->numOfBlocks,
               pRsp->numOfRows, pLoadInfo->totalRows, pLoadInfo->totalSize / 1024.0);
1923 1924
      }

1925 1926
      taosMemoryFreeClear(pDataInfo->pRsp);

1927 1928
      if (pDataInfo->status != EX_SOURCE_DATA_EXHAUSTED) {
        pDataInfo->status = EX_SOURCE_DATA_NOT_READY;
1929 1930
        code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, i);
        if (code != TSDB_CODE_SUCCESS) {
1931
          taosMemoryFreeClear(pDataInfo->pRsp);
1932 1933 1934 1935
          goto _error;
        }
      }

1936
      return;
1937 1938
    }

1939
    if (completed == totalSources) {
1940 1941
      setAllSourcesCompleted(pOperator, startTs);
      return;
1942
    }
H
Haojun Liao 已提交
1943

1944
//    sched_yield();
1945 1946
  }

1947
  _error:
1948 1949 1950
  pTaskInfo->code = code;
}

L
Liu Jicong 已提交
1951 1952 1953
static int32_t prepareConcurrentlyLoad(SOperatorInfo* pOperator) {
  SExchangeInfo* pExchangeInfo = pOperator->info;
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
1954

L
Liu Jicong 已提交
1955
  size_t  totalSources = taosArrayGetSize(pExchangeInfo->pSources);
1956 1957 1958
  int64_t startTs = taosGetTimestampUs();

  // Asynchronously send all fetch requests to all sources.
L
Liu Jicong 已提交
1959
  for (int32_t i = 0; i < totalSources; ++i) {
1960 1961
    int32_t code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, i);
    if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
1962 1963
      pTaskInfo->code = code;
      return code;
1964 1965 1966 1967
    }
  }

  int64_t endTs = taosGetTimestampUs();
1968
  qDebug("%s send all fetch requests to %" PRIzu " sources completed, elapsed:%.2fms", GET_TASKID(pTaskInfo),
X
Xiaoyu Wang 已提交
1969
         totalSources, (endTs - startTs) / 1000.0);
1970

1971
  pOperator->status = OP_RES_TO_RETURN;
H
Haojun Liao 已提交
1972
  pOperator->cost.openCost = taosGetTimestampUs() - startTs;
1973

1974
  tsem_wait(&pExchangeInfo->ready);
1975
  tsem_post(&pExchangeInfo->ready);
H
Haojun Liao 已提交
1976
  return TSDB_CODE_SUCCESS;
1977 1978
}

1979
static int32_t seqLoadRemoteData(SOperatorInfo* pOperator) {
L
Liu Jicong 已提交
1980 1981
  SExchangeInfo* pExchangeInfo = pOperator->info;
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
1982

L
Liu Jicong 已提交
1983
  size_t  totalSources = taosArrayGetSize(pExchangeInfo->pSources);
1984
  int64_t startTs = taosGetTimestampUs();
1985

L
Liu Jicong 已提交
1986
  while (1) {
1987
    if (pExchangeInfo->current >= totalSources) {
1988 1989
      setAllSourcesCompleted(pOperator, startTs);
      return TSDB_CODE_SUCCESS;
1990
    }
1991

1992 1993 1994
    doSendFetchDataRequest(pExchangeInfo, pTaskInfo, pExchangeInfo->current);
    tsem_wait(&pExchangeInfo->ready);

dengyihao's avatar
dengyihao 已提交
1995
    SSourceDataInfo*       pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, pExchangeInfo->current);
X
Xiaoyu Wang 已提交
1996
    SDownstreamSourceNode* pSource = taosArrayGet(pExchangeInfo->pSources, pExchangeInfo->current);
1997

H
Haojun Liao 已提交
1998
    if (pDataInfo->code != TSDB_CODE_SUCCESS) {
1999 2000
      qError("%s vgId:%d, taskID:0x%" PRIx64 " execId:%d error happens, code:%s", GET_TASKID(pTaskInfo),
             pSource->addr.nodeId, pSource->taskId, pSource->execId, tstrerror(pDataInfo->code));
H
Haojun Liao 已提交
2001
      pOperator->pTaskInfo->code = pDataInfo->code;
2002
      return pOperator->pTaskInfo->code;
H
Haojun Liao 已提交
2003 2004
    }

L
Liu Jicong 已提交
2005
    SRetrieveTableRsp*   pRsp = pDataInfo->pRsp;
H
Haojun Liao 已提交
2006
    SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
2007
    if (pRsp->numOfRows == 0) {
2008
      qDebug("%s vgId:%d, taskID:0x%" PRIx64 " execId:%d %d of total completed, rowsOfSource:%" PRIu64
2009
                 ", totalRows:%" PRIu64 " try next",
D
dapan1121 已提交
2010
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->taskId, pSource->execId, pExchangeInfo->current + 1,
H
Haojun Liao 已提交
2011
             pDataInfo->totalRows, pLoadInfo->totalRows);
H
Haojun Liao 已提交
2012

2013
      pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
2014
      pExchangeInfo->current += 1;
D
dapan1121 已提交
2015
      taosMemoryFreeClear(pDataInfo->pRsp);
2016 2017
      continue;
    }
H
Haojun Liao 已提交
2018

2019 2020 2021
    SRetrieveTableRsp* pRetrieveRsp = pDataInfo->pRsp;

    char*   pStart = pRetrieveRsp->data;
H
Haojun Liao 已提交
2022
    int32_t code = extractDataBlockFromFetchRsp(NULL, pStart, NULL, &pStart);
2023 2024

    if (pRsp->completed == 1) {
D
dapan1121 已提交
2025
      qDebug("%s fetch msg rsp from vgId:%d, taskId:0x%" PRIx64 " execId:%d numOfRows:%d, rowsOfSource:%" PRIu64
2026
                 ", totalRows:%" PRIu64 ", totalBytes:%" PRIu64 " try next %d/%" PRIzu,
2027
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->taskId, pSource->execId, pRetrieveRsp->numOfRows,
2028 2029
             pDataInfo->totalRows, pLoadInfo->totalRows, pLoadInfo->totalSize, pExchangeInfo->current + 1,
             totalSources);
2030

2031
      pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
2032 2033
      pExchangeInfo->current += 1;
    } else {
D
dapan1121 已提交
2034
      qDebug("%s fetch msg rsp from vgId:%d, taskId:0x%" PRIx64 " execId:%d numOfRows:%d, totalRows:%" PRIu64
2035
                 ", totalBytes:%" PRIu64,
2036
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->taskId, pSource->execId, pRetrieveRsp->numOfRows,
2037
             pLoadInfo->totalRows, pLoadInfo->totalSize);
2038 2039
    }

2040 2041 2042
    updateLoadRemoteInfo(pLoadInfo, pRetrieveRsp->numOfRows, pRetrieveRsp->compLen, startTs, pOperator);
    pDataInfo->totalRows += pRetrieveRsp->numOfRows;

2043
    taosMemoryFreeClear(pDataInfo->pRsp);
2044
    return TSDB_CODE_SUCCESS;
2045
  }
2046 2047
}

L
Liu Jicong 已提交
2048
static int32_t prepareLoadRemoteData(SOperatorInfo* pOperator) {
2049
  if (OPTR_IS_OPENED(pOperator)) {
H
Haojun Liao 已提交
2050 2051 2052
    return TSDB_CODE_SUCCESS;
  }

2053 2054
  int64_t st = taosGetTimestampUs();

L
Liu Jicong 已提交
2055
  SExchangeInfo* pExchangeInfo = pOperator->info;
2056
  if (!pExchangeInfo->seqLoadData) {
H
Haojun Liao 已提交
2057 2058 2059 2060 2061 2062
    int32_t code = prepareConcurrentlyLoad(pOperator);
    if (code != TSDB_CODE_SUCCESS) {
      return code;
    }
  }

2063
  OPTR_SET_OPENED(pOperator);
2064
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
H
Haojun Liao 已提交
2065 2066 2067
  return TSDB_CODE_SUCCESS;
}

2068 2069 2070 2071 2072
static void freeBlock(void* pParam) {
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
  blockDataDestroy(pBlock);
}

2073
static SSDataBlock* doLoadRemoteDataImpl(SOperatorInfo* pOperator) {
L
Liu Jicong 已提交
2074 2075
  SExchangeInfo* pExchangeInfo = pOperator->info;
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
2076

2077
  pTaskInfo->code = pOperator->fpSet._openFn(pOperator);
2078
  if (pTaskInfo->code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
2079 2080
    return NULL;
  }
2081

2082
  size_t totalSources = taosArrayGetSize(pExchangeInfo->pSources);
H
Haojun Liao 已提交
2083

2084
  SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
2085
  if (pOperator->status == OP_EXEC_DONE) {
L
Liu Jicong 已提交
2086 2087 2088
    qDebug("%s all %" PRIzu " source(s) are exhausted, total rows:%" PRIu64 " bytes:%" PRIu64 ", elapsed:%.2f ms",
           GET_TASKID(pTaskInfo), totalSources, pLoadInfo->totalRows, pLoadInfo->totalSize,
           pLoadInfo->totalElapsed / 1000.0);
2089 2090 2091
    return NULL;
  }

2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104
  size_t size = taosArrayGetSize(pExchangeInfo->pResultBlockList);
  if (size == 0 || pExchangeInfo->rspBlockIndex >= size) {
    pExchangeInfo->rspBlockIndex = 0;
    taosArrayClearEx(pExchangeInfo->pResultBlockList, freeBlock);
    if (pExchangeInfo->seqLoadData) {
      seqLoadRemoteData(pOperator);
    } else {
      concurrentlyLoadRemoteDataImpl(pOperator, pExchangeInfo, pTaskInfo);
    }

    if (taosArrayGetSize(pExchangeInfo->pResultBlockList) == 0) {
      return NULL;
    }
2105
  }
2106 2107 2108

  // we have buffered retrieved datablock, return it directly
  return taosArrayGetP(pExchangeInfo->pResultBlockList, pExchangeInfo->rspBlockIndex++);
H
Haojun Liao 已提交
2109
}
2110

2111 2112 2113 2114 2115 2116 2117 2118
static SSDataBlock* doLoadRemoteData(SOperatorInfo* pOperator) {
  SExchangeInfo* pExchangeInfo = pOperator->info;
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;

  if (pOperator->status == OP_EXEC_DONE) {
    return NULL;
  }

L
Liu Jicong 已提交
2119
  while (1) {
2120 2121 2122 2123 2124 2125 2126
    SSDataBlock* pBlock = doLoadRemoteDataImpl(pOperator);
    if (pBlock == NULL) {
      return NULL;
    }

    SLimitInfo* pLimitInfo = &pExchangeInfo->limitInfo;
    if (hasLimitOffsetInfo(pLimitInfo)) {
2127
      int32_t status = handleLimitOffset(pOperator, pLimitInfo, pBlock, false);
2128 2129 2130
      if (status == PROJECT_RETRIEVE_CONTINUE) {
        continue;
      } else if (status == PROJECT_RETRIEVE_DONE) {
2131
        size_t rows = pBlock->info.rows;
2132 2133 2134 2135 2136 2137
        pExchangeInfo->limitInfo.numOfOutputRows += rows;

        if (rows == 0) {
          doSetOperatorCompleted(pOperator);
          return NULL;
        } else {
2138
          return pBlock;
2139 2140 2141
        }
      }
    } else {
2142
      return pBlock;
2143 2144 2145 2146
    }
  }
}

2147
static int32_t initDataSource(int32_t numOfSources, SExchangeInfo* pInfo, const char* id) {
2148
  pInfo->pSourceDataInfo = taosArrayInit(numOfSources, sizeof(SSourceDataInfo));
H
Haojun Liao 已提交
2149 2150
  if (pInfo->pSourceDataInfo == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
2151 2152
  }

L
Liu Jicong 已提交
2153
  for (int32_t i = 0; i < numOfSources; ++i) {
2154
    SSourceDataInfo dataInfo = {0};
H
Haojun Liao 已提交
2155
    dataInfo.status = EX_SOURCE_DATA_NOT_READY;
2156
    dataInfo.taskId = id;
L
Liu Jicong 已提交
2157
    dataInfo.index = i;
X
Xiaoyu Wang 已提交
2158
    SSourceDataInfo* pDs = taosArrayPush(pInfo->pSourceDataInfo, &dataInfo);
2159
    if (pDs == NULL) {
H
Haojun Liao 已提交
2160 2161 2162 2163 2164 2165 2166 2167
      taosArrayDestroy(pInfo->pSourceDataInfo);
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }

  return TSDB_CODE_SUCCESS;
}

2168
static int32_t initExchangeOperator(SExchangePhysiNode* pExNode, SExchangeInfo* pInfo, const char* id) {
2169
  size_t numOfSources = LIST_LENGTH(pExNode->pSrcEndPoints);
H
Haojun Liao 已提交
2170

2171
  if (numOfSources == 0) {
X
Xiaoyu Wang 已提交
2172
    qError("%s invalid number: %d of sources in exchange operator", id, (int32_t)numOfSources);
2173 2174 2175
    return TSDB_CODE_INVALID_PARA;
  }

H
Haojun Liao 已提交
2176
  pInfo->pSources = taosArrayInit(numOfSources, sizeof(SDownstreamSourceNode));
wmmhello's avatar
wmmhello 已提交
2177
  if (pInfo->pSources == NULL) {
2178
    return TSDB_CODE_OUT_OF_MEMORY;
H
Haojun Liao 已提交
2179 2180
  }

L
Liu Jicong 已提交
2181
  for (int32_t i = 0; i < numOfSources; ++i) {
D
dapan1121 已提交
2182
    SDownstreamSourceNode* pNode = (SDownstreamSourceNode*)nodesListGetNode((SNodeList*)pExNode->pSrcEndPoints, i);
H
Haojun Liao 已提交
2183 2184
    taosArrayPush(pInfo->pSources, pNode);
  }
2185

2186
  initLimitInfo(pExNode->node.pLimit, pExNode->node.pSlimit, &pInfo->limitInfo);
2187 2188
  pInfo->self = taosAddRef(exchangeObjRefPool, pInfo);

2189
  return initDataSource(numOfSources, pInfo, id);
2190 2191 2192 2193 2194 2195
}

SOperatorInfo* createExchangeOperatorInfo(void* pTransporter, SExchangePhysiNode* pExNode, SExecTaskInfo* pTaskInfo) {
  SExchangeInfo* pInfo = taosMemoryCalloc(1, sizeof(SExchangeInfo));
  SOperatorInfo* pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
  if (pInfo == NULL || pOperator == NULL) {
H
Haojun Liao 已提交
2196
    goto _error;
2197
  }
H
Haojun Liao 已提交
2198

2199
  int32_t code = initExchangeOperator(pExNode, pInfo, GET_TASKID(pTaskInfo));
2200 2201 2202
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
2203 2204

  tsem_init(&pInfo->ready, 0, 0);
2205 2206
  pInfo->pDummyBlock = createResDataBlock(pExNode->node.pOutputDataBlockDesc);
  pInfo->pResultBlockList = taosArrayInit(1, POINTER_BYTES);
2207

2208
  pInfo->seqLoadData = false;
2209
  pInfo->pTransporter = pTransporter;
2210

2211
  pOperator->name = "ExchangeOperator";
X
Xiaoyu Wang 已提交
2212
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_EXCHANGE;
X
Xiaoyu Wang 已提交
2213
  pOperator->blocking = false;
2214 2215
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
2216
  pOperator->exprSupp.numOfExprs = taosArrayGetSize(pInfo->pDummyBlock->pDataBlock);
X
Xiaoyu Wang 已提交
2217
  pOperator->pTaskInfo = pTaskInfo;
2218

5
54liuyao 已提交
2219 2220
  pOperator->fpSet =
      createOperatorFpSet(prepareLoadRemoteData, doLoadRemoteData, NULL, NULL, destroyExchangeOperatorInfo, NULL);
2221
  return pOperator;
H
Haojun Liao 已提交
2222

2223
  _error:
H
Haojun Liao 已提交
2224
  if (pInfo != NULL) {
2225
    doDestroyExchangeOperatorInfo(pInfo);
H
Haojun Liao 已提交
2226 2227
  }

wafwerar's avatar
wafwerar 已提交
2228
  taosMemoryFreeClear(pOperator);
2229
  pTaskInfo->code = code;
H
Haojun Liao 已提交
2230
  return NULL;
2231 2232
}

dengyihao's avatar
dengyihao 已提交
2233 2234
static int32_t doInitAggInfoSup(SAggSupporter* pAggSup, SqlFunctionCtx* pCtx, int32_t numOfOutput, size_t keyBufSize,
                                const char* pKey);
2235

L
Liu Jicong 已提交
2236
static bool needToMerge(SSDataBlock* pBlock, SArray* groupInfo, char** buf, int32_t rowIndex) {
2237 2238 2239 2240
  size_t size = taosArrayGetSize(groupInfo);
  if (size == 0) {
    return true;
  }
2241

2242 2243
  for (int32_t i = 0; i < size; ++i) {
    int32_t* index = taosArrayGet(groupInfo, i);
2244

2245
    SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, *index);
L
Liu Jicong 已提交
2246
    bool             isNull = colDataIsNull(pColInfo, rowIndex, pBlock->info.rows, NULL);
2247

2248 2249 2250
    if ((isNull && buf[i] != NULL) || (!isNull && buf[i] == NULL)) {
      return false;
    }
2251

2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264
    char* pCell = colDataGetData(pColInfo, rowIndex);
    if (IS_VAR_DATA_TYPE(pColInfo->info.type)) {
      if (varDataLen(pCell) != varDataLen(buf[i])) {
        return false;
      } else {
        if (memcmp(varDataVal(pCell), varDataVal(buf[i]), varDataLen(pCell)) != 0) {
          return false;
        }
      }
    } else {
      if (memcmp(pCell, buf[i], pColInfo->info.bytes) != 0) {
        return false;
      }
2265 2266 2267
    }
  }

2268
  return 0;
2269 2270
}

2271
static bool saveCurrentTuple(char** rowColData, SArray* pColumnList, SSDataBlock* pBlock, int32_t rowIndex) {
L
Liu Jicong 已提交
2272
  int32_t size = (int32_t)taosArrayGetSize(pColumnList);
2273

L
Liu Jicong 已提交
2274 2275
  for (int32_t i = 0; i < size; ++i) {
    int32_t*         index = taosArrayGet(pColumnList, i);
2276
    SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, *index);
H
Haojun Liao 已提交
2277

2278 2279 2280
    char* data = colDataGetData(pColInfo, rowIndex);
    memcpy(rowColData[i], data, colDataGetLength(pColInfo, rowIndex));
  }
2281

2282 2283
  return true;
}
2284

X
Xiaoyu Wang 已提交
2285
int32_t getTableScanInfo(SOperatorInfo* pOperator, int32_t* order, int32_t* scanFlag) {
2286
  // todo add more information about exchange operation
2287
  int32_t type = pOperator->operatorType;
X
Xiaoyu Wang 已提交
2288
  if (type == QUERY_NODE_PHYSICAL_PLAN_EXCHANGE || type == QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN ||
2289
      type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN || type == QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN ||
2290
      type == QUERY_NODE_PHYSICAL_PLAN_BLOCK_DIST_SCAN || type == QUERY_NODE_PHYSICAL_PLAN_LAST_ROW_SCAN) {
2291 2292 2293
    *order = TSDB_ORDER_ASC;
    *scanFlag = MAIN_SCAN;
    return TSDB_CODE_SUCCESS;
2294
  } else if (type == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
2295 2296 2297 2298
    STableScanInfo* pTableScanInfo = pOperator->info;
    *order = pTableScanInfo->cond.order;
    *scanFlag = pTableScanInfo->scanFlag;
    return TSDB_CODE_SUCCESS;
2299 2300 2301 2302 2303
  } else if (type == QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN) {
    STableMergeScanInfo* pTableScanInfo = pOperator->info;
    *order = pTableScanInfo->cond.order;
    *scanFlag = pTableScanInfo->scanFlag;
    return TSDB_CODE_SUCCESS;
2304
  } else {
H
Haojun Liao 已提交
2305
    if (pOperator->pDownstream == NULL || pOperator->pDownstream[0] == NULL) {
2306
      return TSDB_CODE_INVALID_PARA;
H
Haojun Liao 已提交
2307
    } else {
2308
      return getTableScanInfo(pOperator->pDownstream[0], order, scanFlag);
2309 2310 2311
    }
  }
}
2312

2313
// this is a blocking operator
L
Liu Jicong 已提交
2314
static int32_t doOpenAggregateOptr(SOperatorInfo* pOperator) {
H
Haojun Liao 已提交
2315 2316
  if (OPTR_IS_OPENED(pOperator)) {
    return TSDB_CODE_SUCCESS;
2317 2318
  }

H
Haojun Liao 已提交
2319
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
2320
  SAggOperatorInfo* pAggInfo = pOperator->info;
H
Haojun Liao 已提交
2321

2322 2323
  SExprSupp*     pSup = &pOperator->exprSupp;
  SOperatorInfo* downstream = pOperator->pDownstream[0];
2324

2325 2326
  int64_t st = taosGetTimestampUs();

2327 2328 2329
  int32_t order = TSDB_ORDER_ASC;
  int32_t scanFlag = MAIN_SCAN;

H
Haojun Liao 已提交
2330
  while (1) {
2331
    SSDataBlock* pBlock = downstream->fpSet.getNextFn(downstream);
2332 2333 2334 2335
    if (pBlock == NULL) {
      break;
    }

2336 2337
    int32_t code = getTableScanInfo(pOperator, &order, &scanFlag);
    if (code != TSDB_CODE_SUCCESS) {
2338
      T_LONG_JMP(pTaskInfo->env, code);
2339
    }
2340

2341
    // there is an scalar expression that needs to be calculated before apply the group aggregation.
2342 2343 2344
    if (pAggInfo->scalarExprSup.pExprInfo != NULL) {
      SExprSupp* pSup1 = &pAggInfo->scalarExprSup;
      code = projectApplyFunctions(pSup1->pExprInfo, pBlock, pBlock, pSup1->pCtx, pSup1->numOfExprs, NULL);
2345
      if (code != TSDB_CODE_SUCCESS) {
2346
        T_LONG_JMP(pTaskInfo->env, code);
2347
      }
2348 2349
    }

2350
    // the pDataBlock are always the same one, no need to call this again
2351
    setExecutionContext(pOperator, pOperator->exprSupp.numOfExprs, pBlock->info.groupId);
2352
    setInputDataBlock(pSup, pBlock, order, scanFlag, true);
2353
    code = doAggregateImpl(pOperator, pSup->pCtx);
2354
    if (code != 0) {
2355
      T_LONG_JMP(pTaskInfo->env, code);
2356
    }
2357 2358
  }

2359
  initGroupedResultInfo(&pAggInfo->groupResInfo, pAggInfo->aggSup.pResultRowHashTable, 0);
H
Haojun Liao 已提交
2360
  OPTR_SET_OPENED(pOperator);
2361

2362
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
H
Haojun Liao 已提交
2363 2364 2365
  return TSDB_CODE_SUCCESS;
}

2366
static SSDataBlock* getAggregateResult(SOperatorInfo* pOperator) {
L
Liu Jicong 已提交
2367
  SAggOperatorInfo* pAggInfo = pOperator->info;
H
Haojun Liao 已提交
2368 2369 2370 2371 2372 2373
  SOptrBasicInfo*   pInfo = &pAggInfo->binfo;

  if (pOperator->status == OP_EXEC_DONE) {
    return NULL;
  }

L
Liu Jicong 已提交
2374
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
2375
  pTaskInfo->code = pOperator->fpSet._openFn(pOperator);
H
Haojun Liao 已提交
2376
  if (pTaskInfo->code != TSDB_CODE_SUCCESS) {
2377
    doSetOperatorCompleted(pOperator);
H
Haojun Liao 已提交
2378 2379 2380
    return NULL;
  }

H
Haojun Liao 已提交
2381
  blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
S
slzhou 已提交
2382 2383
  while (1) {
    doBuildResultDatablock(pOperator, pInfo, &pAggInfo->groupResInfo, pAggInfo->aggSup.pResultBuf);
H
Haojun Liao 已提交
2384
    doFilter(pInfo->pRes, pOperator->exprSupp.pFilterInfo, NULL);
S
slzhou 已提交
2385

2386
    if (!hasRemainResults(&pAggInfo->groupResInfo)) {
S
slzhou 已提交
2387 2388 2389
      doSetOperatorCompleted(pOperator);
      break;
    }
2390

S
slzhou 已提交
2391 2392 2393 2394
    if (pInfo->pRes->info.rows > 0) {
      break;
    }
  }
2395

2396
  size_t rows = blockDataGetNumOfRows(pInfo->pRes);
2397 2398
  pOperator->resultInfo.totalRows += rows;

2399
  return (rows == 0) ? NULL : pInfo->pRes;
2400 2401
}

wmmhello's avatar
wmmhello 已提交
2402
int32_t aggEncodeResultRow(SOperatorInfo* pOperator, char** result, int32_t* length) {
2403
  if (result == NULL || length == NULL) {
wmmhello's avatar
wmmhello 已提交
2404 2405 2406
    return TSDB_CODE_TSC_INVALID_INPUT;
  }
  SOptrBasicInfo* pInfo = (SOptrBasicInfo*)(pOperator->info);
2407
  SAggSupporter*  pSup = (SAggSupporter*)POINTER_SHIFT(pOperator->info, sizeof(SOptrBasicInfo));
2408
  int32_t         size = tSimpleHashGetSize(pSup->pResultRowHashTable);
2409 2410 2411
  size_t          keyLen = sizeof(uint64_t) * 2;  // estimate the key length
  int32_t         totalSize =
      sizeof(int32_t) + sizeof(int32_t) + size * (sizeof(int32_t) + keyLen + sizeof(int32_t) + pSup->resultRowSize);
wmmhello's avatar
wmmhello 已提交
2412

C
Cary Xu 已提交
2413 2414 2415 2416 2417 2418
  // no result
  if (getTotalBufSize(pSup->pResultBuf) == 0) {
    *result = NULL;
    *length = 0;
    return TSDB_CODE_SUCCESS;
  }
2419

wmmhello's avatar
wmmhello 已提交
2420
  *result = (char*)taosMemoryCalloc(1, totalSize);
L
Liu Jicong 已提交
2421
  if (*result == NULL) {
wmmhello's avatar
wmmhello 已提交
2422
    return TSDB_CODE_OUT_OF_MEMORY;
wmmhello's avatar
wmmhello 已提交
2423
  }
wmmhello's avatar
wmmhello 已提交
2424

wmmhello's avatar
wmmhello 已提交
2425
  int32_t offset = sizeof(int32_t);
wmmhello's avatar
wmmhello 已提交
2426 2427
  *(int32_t*)(*result + offset) = size;
  offset += sizeof(int32_t);
2428 2429

  // prepare memory
2430
  SResultRowPosition* pos = &pInfo->resultRowInfo.cur;
dengyihao's avatar
dengyihao 已提交
2431 2432
  void*               pPage = getBufPage(pSup->pResultBuf, pos->pageId);
  SResultRow*         pRow = (SResultRow*)((char*)pPage + pos->offset);
2433 2434
  setBufPageDirty(pPage, true);
  releaseBufPage(pSup->pResultBuf, pPage);
L
Liu Jicong 已提交
2435

2436 2437 2438 2439
  int32_t iter = 0;
  void*   pIter = NULL;
  while ((pIter = tSimpleHashIterate(pSup->pResultRowHashTable, pIter, &iter))) {
    void*               key = tSimpleHashGetKey(pIter, &keyLen);
2440
    SResultRowPosition* p1 = (SResultRowPosition*)pIter;
2441

dengyihao's avatar
dengyihao 已提交
2442
    pPage = (SFilePage*)getBufPage(pSup->pResultBuf, p1->pageId);
2443
    pRow = (SResultRow*)((char*)pPage + p1->offset);
2444 2445
    setBufPageDirty(pPage, true);
    releaseBufPage(pSup->pResultBuf, pPage);
wmmhello's avatar
wmmhello 已提交
2446 2447 2448

    // recalculate the result size
    int32_t realTotalSize = offset + sizeof(int32_t) + keyLen + sizeof(int32_t) + pSup->resultRowSize;
L
Liu Jicong 已提交
2449
    if (realTotalSize > totalSize) {
wmmhello's avatar
wmmhello 已提交
2450
      char* tmp = (char*)taosMemoryRealloc(*result, realTotalSize);
L
Liu Jicong 已提交
2451
      if (tmp == NULL) {
wafwerar's avatar
wafwerar 已提交
2452
        taosMemoryFree(*result);
wmmhello's avatar
wmmhello 已提交
2453
        *result = NULL;
wmmhello's avatar
wmmhello 已提交
2454
        return TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
2455
      } else {
wmmhello's avatar
wmmhello 已提交
2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467
        *result = tmp;
      }
    }
    // save key
    *(int32_t*)(*result + offset) = keyLen;
    offset += sizeof(int32_t);
    memcpy(*result + offset, key, keyLen);
    offset += keyLen;

    // save value
    *(int32_t*)(*result + offset) = pSup->resultRowSize;
    offset += sizeof(int32_t);
2468
    memcpy(*result + offset, pRow, pSup->resultRowSize);
wmmhello's avatar
wmmhello 已提交
2469 2470 2471
    offset += pSup->resultRowSize;
  }

wmmhello's avatar
wmmhello 已提交
2472 2473 2474 2475
  *(int32_t*)(*result) = offset;
  *length = offset;

  return TDB_CODE_SUCCESS;
wmmhello's avatar
wmmhello 已提交
2476 2477
}

2478 2479 2480 2481 2482
int32_t handleLimitOffset(SOperatorInfo* pOperator, SLimitInfo* pLimitInfo, SSDataBlock* pBlock, bool holdDataInBuf) {
  if (pLimitInfo->remainGroupOffset > 0) {
    if (pLimitInfo->currentGroupId == 0) {  // it is the first group
      pLimitInfo->currentGroupId = pBlock->info.groupId;
      blockDataCleanup(pBlock);
2483
      return PROJECT_RETRIEVE_CONTINUE;
2484 2485 2486
    } else if (pLimitInfo->currentGroupId != pBlock->info.groupId) {
      // now it is the data from a new group
      pLimitInfo->remainGroupOffset -= 1;
2487 2488

      // ignore data block in current group
2489 2490
      if (pLimitInfo->remainGroupOffset > 0) {
        blockDataCleanup(pBlock);
2491 2492 2493 2494 2495
        return PROJECT_RETRIEVE_CONTINUE;
      }
    }

    // set current group id of the project operator
2496
    pLimitInfo->currentGroupId = pBlock->info.groupId;
2497 2498
  }

2499
  // here check for a new group data, we need to handle the data of the previous group.
2500 2501 2502
  if (pLimitInfo->currentGroupId != 0 && pLimitInfo->currentGroupId != pBlock->info.groupId) {
    pLimitInfo->numOfOutputGroups += 1;
    if ((pLimitInfo->slimit.limit > 0) && (pLimitInfo->slimit.limit <= pLimitInfo->numOfOutputGroups)) {
2503
      pOperator->status = OP_EXEC_DONE;
2504
      blockDataCleanup(pBlock);
2505 2506 2507 2508 2509

      return PROJECT_RETRIEVE_DONE;
    }

    // reset the value for a new group data
2510 2511
    pLimitInfo->numOfOutputRows = 0;
    pLimitInfo->remainOffset = pLimitInfo->limit.offset;
2512 2513 2514 2515 2516

    // existing rows that belongs to previous group.
    if (pBlock->info.rows > 0) {
      return PROJECT_RETRIEVE_DONE;
    }
2517 2518 2519 2520 2521
  }

  // here we reach the start position, according to the limit/offset requirements.

  // set current group id
2522
  pLimitInfo->currentGroupId = pBlock->info.groupId;
2523

2524 2525 2526
  if (pLimitInfo->remainOffset >= pBlock->info.rows) {
    pLimitInfo->remainOffset -= pBlock->info.rows;
    blockDataCleanup(pBlock);
2527
    return PROJECT_RETRIEVE_CONTINUE;
2528 2529 2530
  } else if (pLimitInfo->remainOffset < pBlock->info.rows && pLimitInfo->remainOffset > 0) {
    blockDataTrimFirstNRows(pBlock, pLimitInfo->remainOffset);
    pLimitInfo->remainOffset = 0;
2531 2532
  }

2533
  // check for the limitation in each group
2534 2535 2536 2537
  if (pLimitInfo->limit.limit >= 0 && pLimitInfo->numOfOutputRows + pBlock->info.rows >= pLimitInfo->limit.limit) {
    int32_t keepRows = (int32_t)(pLimitInfo->limit.limit - pLimitInfo->numOfOutputRows);
    blockDataKeepFirstNRows(pBlock, keepRows);
    if (pLimitInfo->slimit.limit > 0 && pLimitInfo->slimit.limit <= pLimitInfo->numOfOutputGroups) {
2538 2539 2540
      pOperator->status = OP_EXEC_DONE;
    }

2541
    return PROJECT_RETRIEVE_DONE;
2542
  }
2543

2544
  // todo optimize performance
2545 2546
  // If there are slimit/soffset value exists, multi-round result can not be packed into one group, since the
  // they may not belong to the same group the limit/offset value is not valid in this case.
2547 2548
  if ((!holdDataInBuf) || (pBlock->info.rows >= pOperator->resultInfo.threshold) || pLimitInfo->slimit.offset != -1 ||
      pLimitInfo->slimit.limit != -1) {
2549
    return PROJECT_RETRIEVE_DONE;
L
Liu Jicong 已提交
2550
  } else {  // not full enough, continue to accumulate the output data in the buffer.
2551 2552 2553 2554
    return PROJECT_RETRIEVE_CONTINUE;
  }
}

2555
static void doApplyScalarCalculation(SOperatorInfo* pOperator, SSDataBlock* pBlock, int32_t order, int32_t scanFlag);
L
Liu Jicong 已提交
2556 2557
static void doHandleRemainBlockForNewGroupImpl(SOperatorInfo* pOperator, SFillOperatorInfo* pInfo,
                                               SResultInfo* pResultInfo, SExecTaskInfo* pTaskInfo) {
2558
  pInfo->totalInputRows = pInfo->existNewGroupBlock->info.rows;
2559 2560 2561 2562 2563
  SSDataBlock* pResBlock = pInfo->pFinalRes;

  int32_t order = TSDB_ORDER_ASC;
  int32_t scanFlag = MAIN_SCAN;
  getTableScanInfo(pOperator, &order, &scanFlag);
H
Haojun Liao 已提交
2564

L
Liu Jicong 已提交
2565 2566
  int64_t ekey =
      Q_STATUS_EQUAL(pTaskInfo->status, TASK_COMPLETED) ? pInfo->win.ekey : pInfo->existNewGroupBlock->info.window.ekey;
2567 2568
  taosResetFillInfo(pInfo->pFillInfo, getFillInfoStart(pInfo->pFillInfo));

2569
  blockDataCleanup(pInfo->pRes);
2570 2571 2572 2573
  doApplyScalarCalculation(pOperator, pInfo->existNewGroupBlock, order, scanFlag);

  taosFillSetStartInfo(pInfo->pFillInfo, pInfo->pRes->info.rows, ekey);
  taosFillSetInputDataBlock(pInfo->pFillInfo, pInfo->pRes);
2574

2575 2576
  int32_t numOfResultRows = pResultInfo->capacity - pResBlock->info.rows;
  taosFillResultDataBlock(pInfo->pFillInfo, pResBlock, numOfResultRows);
H
Haojun Liao 已提交
2577

2578
  pInfo->curGroupId = pInfo->existNewGroupBlock->info.groupId;
2579 2580 2581
  pInfo->existNewGroupBlock = NULL;
}

L
Liu Jicong 已提交
2582 2583
static void doHandleRemainBlockFromNewGroup(SOperatorInfo* pOperator, SFillOperatorInfo* pInfo,
                                            SResultInfo* pResultInfo, SExecTaskInfo* pTaskInfo) {
2584
  if (taosFillHasMoreResults(pInfo->pFillInfo)) {
H
Haojun Liao 已提交
2585 2586
    int32_t numOfResultRows = pResultInfo->capacity - pInfo->pFinalRes->info.rows;
    taosFillResultDataBlock(pInfo->pFillInfo, pInfo->pFinalRes, numOfResultRows);
2587 2588
    pInfo->pRes->info.groupId = pInfo->curGroupId;
    return;
2589 2590 2591 2592
  }

  // handle the cached new group data block
  if (pInfo->existNewGroupBlock) {
2593 2594 2595 2596 2597 2598
    doHandleRemainBlockForNewGroupImpl(pOperator, pInfo, pResultInfo, pTaskInfo);
  }
}

static void doApplyScalarCalculation(SOperatorInfo* pOperator, SSDataBlock* pBlock, int32_t order, int32_t scanFlag) {
  SFillOperatorInfo* pInfo = pOperator->info;
L
Liu Jicong 已提交
2599
  SExprSupp*         pSup = &pOperator->exprSupp;
2600
  setInputDataBlock(pSup, pBlock, order, scanFlag, false);
2601 2602
  projectApplyFunctions(pSup->pExprInfo, pInfo->pRes, pBlock, pSup->pCtx, pSup->numOfExprs, NULL);

2603 2604 2605 2606
  // reset the row value before applying the no-fill functions to the input data block, which is "pBlock" in this case.
  pInfo->pRes->info.rows = 0;
  SExprSupp* pNoFillSupp = &pInfo->noFillExprSupp;
  setInputDataBlock(pNoFillSupp, pBlock, order, scanFlag, false);
2607

2608 2609
  projectApplyFunctions(pNoFillSupp->pExprInfo, pInfo->pRes, pBlock, pNoFillSupp->pCtx, pNoFillSupp->numOfExprs, NULL);
  pInfo->pRes->info.groupId = pBlock->info.groupId;
2610 2611
}

S
slzhou 已提交
2612
static SSDataBlock* doFillImpl(SOperatorInfo* pOperator) {
L
Liu Jicong 已提交
2613 2614
  SFillOperatorInfo* pInfo = pOperator->info;
  SExecTaskInfo*     pTaskInfo = pOperator->pTaskInfo;
2615

H
Haojun Liao 已提交
2616
  SResultInfo* pResultInfo = &pOperator->resultInfo;
H
Haojun Liao 已提交
2617
  SSDataBlock* pResBlock = pInfo->pFinalRes;
2618 2619

  blockDataCleanup(pResBlock);
2620

H
Haojun Liao 已提交
2621 2622
  int32_t order = TSDB_ORDER_ASC;
  int32_t scanFlag = MAIN_SCAN;
2623
  getTableScanInfo(pOperator, &order, &scanFlag);
2624

2625
  doHandleRemainBlockFromNewGroup(pOperator, pInfo, pResultInfo, pTaskInfo);
2626
  if (pResBlock->info.rows > 0) {
2627
    pResBlock->info.groupId = pInfo->curGroupId;
2628
    return pResBlock;
H
Haojun Liao 已提交
2629
  }
2630

H
Haojun Liao 已提交
2631
  SOperatorInfo* pDownstream = pOperator->pDownstream[0];
L
Liu Jicong 已提交
2632
  while (1) {
2633
    SSDataBlock* pBlock = pDownstream->fpSet.getNextFn(pDownstream);
2634 2635
    if (pBlock == NULL) {
      if (pInfo->totalInputRows == 0) {
2636
        doSetOperatorCompleted(pOperator);
2637 2638
        return NULL;
      }
2639

2640
      taosFillSetStartInfo(pInfo->pFillInfo, 0, pInfo->win.ekey);
2641
    } else {
2642
      blockDataUpdateTsWindow(pBlock, pInfo->primarySrcSlotId);
2643 2644

      blockDataCleanup(pInfo->pRes);
2645 2646
      blockDataEnsureCapacity(pInfo->pRes, pBlock->info.rows);
      blockDataEnsureCapacity(pInfo->pFinalRes, pBlock->info.rows);
2647
      doApplyScalarCalculation(pOperator, pBlock, order, scanFlag);
2648

H
Haojun Liao 已提交
2649 2650 2651
      if (pInfo->curGroupId == 0 || pInfo->curGroupId == pInfo->pRes->info.groupId) {
        pInfo->curGroupId = pInfo->pRes->info.groupId;  // the first data block
        pInfo->totalInputRows += pInfo->pRes->info.rows;
2652

H
Haojun Liao 已提交
2653 2654 2655 2656 2657
        if (order == pInfo->pFillInfo->order) {
          taosFillSetStartInfo(pInfo->pFillInfo, pInfo->pRes->info.rows, pBlock->info.window.ekey);
        } else {
          taosFillSetStartInfo(pInfo->pFillInfo, pInfo->pRes->info.rows, pBlock->info.window.skey);
        }
H
Haojun Liao 已提交
2658
        taosFillSetInputDataBlock(pInfo->pFillInfo, pInfo->pRes);
L
Liu Jicong 已提交
2659
      } else if (pInfo->curGroupId != pBlock->info.groupId) {  // the new group data block
2660 2661 2662 2663 2664
        pInfo->existNewGroupBlock = pBlock;

        // Fill the previous group data block, before handle the data block of new group.
        // Close the fill operation for previous group data block
        taosFillSetStartInfo(pInfo->pFillInfo, 0, pInfo->win.ekey);
2665 2666 2667
      }
    }

2668 2669
    int32_t numOfResultRows = pOperator->resultInfo.capacity - pResBlock->info.rows;
    taosFillResultDataBlock(pInfo->pFillInfo, pResBlock, numOfResultRows);
2670 2671

    // current group has no more result to return
2672
    if (pResBlock->info.rows > 0) {
2673 2674
      // 1. The result in current group not reach the threshold of output result, continue
      // 2. If multiple group results existing in one SSDataBlock is not allowed, return immediately
2675
      if (pResBlock->info.rows > pResultInfo->threshold || pBlock == NULL || pInfo->existNewGroupBlock != NULL) {
2676
        pResBlock->info.groupId = pInfo->curGroupId;
2677
        return pResBlock;
2678 2679
      }

2680
      doHandleRemainBlockFromNewGroup(pOperator, pInfo, pResultInfo, pTaskInfo);
2681
      if (pResBlock->info.rows >= pOperator->resultInfo.threshold || pBlock == NULL) {
2682
        pResBlock->info.groupId = pInfo->curGroupId;
2683
        return pResBlock;
2684 2685 2686
      }
    } else if (pInfo->existNewGroupBlock) {  // try next group
      assert(pBlock != NULL);
2687 2688 2689 2690

      blockDataCleanup(pResBlock);

      doHandleRemainBlockForNewGroupImpl(pOperator, pInfo, pResultInfo, pTaskInfo);
2691
      if (pResBlock->info.rows > pResultInfo->threshold) {
2692
        pResBlock->info.groupId = pInfo->curGroupId;
2693
        return pResBlock;
2694 2695 2696 2697 2698 2699 2700
      }
    } else {
      return NULL;
    }
  }
}

S
slzhou 已提交
2701 2702 2703 2704 2705 2706 2707 2708
static SSDataBlock* doFill(SOperatorInfo* pOperator) {
  SFillOperatorInfo* pInfo = pOperator->info;
  SExecTaskInfo*     pTaskInfo = pOperator->pTaskInfo;

  if (pOperator->status == OP_EXEC_DONE) {
    return NULL;
  }

S
slzhou 已提交
2709
  SSDataBlock* fillResult = NULL;
S
slzhou 已提交
2710
  while (true) {
S
slzhou 已提交
2711
    fillResult = doFillImpl(pOperator);
S
slzhou 已提交
2712 2713 2714 2715 2716
    if (fillResult == NULL) {
      doSetOperatorCompleted(pOperator);
      break;
    }

H
Haojun Liao 已提交
2717
    doFilter(fillResult, pOperator->exprSupp.pFilterInfo, &pInfo->matchInfo );
S
slzhou 已提交
2718 2719 2720 2721 2722
    if (fillResult->info.rows > 0) {
      break;
    }
  }

S
slzhou 已提交
2723
  if (fillResult != NULL) {
2724
    pOperator->resultInfo.totalRows += fillResult->info.rows;
S
slzhou 已提交
2725
  }
S
slzhou 已提交
2726

S
slzhou 已提交
2727
  return fillResult;
S
slzhou 已提交
2728 2729
}

2730
void destroyExprInfo(SExprInfo* pExpr, int32_t numOfExprs) {
C
Cary Xu 已提交
2731 2732 2733 2734 2735
  for (int32_t i = 0; i < numOfExprs; ++i) {
    SExprInfo* pExprInfo = &pExpr[i];
    for (int32_t j = 0; j < pExprInfo->base.numOfParams; ++j) {
      if (pExprInfo->base.pParam[j].type == FUNC_PARAM_TYPE_COLUMN) {
        taosMemoryFreeClear(pExprInfo->base.pParam[j].pCol);
H
Haojun Liao 已提交
2736
      }
2737
    }
C
Cary Xu 已提交
2738 2739 2740

    taosMemoryFree(pExprInfo->base.pParam);
    taosMemoryFree(pExprInfo->pExpr);
H
Haojun Liao 已提交
2741 2742 2743
  }
}

2744 2745 2746 2747 2748
static void destroyOperatorInfo(SOperatorInfo* pOperator) {
  if (pOperator == NULL) {
    return;
  }

2749
  if (pOperator->fpSet.closeFn != NULL) {
2750
    pOperator->fpSet.closeFn(pOperator->info);
2751 2752
  }

H
Haojun Liao 已提交
2753
  if (pOperator->pDownstream != NULL) {
L
Liu Jicong 已提交
2754
    for (int32_t i = 0; i < pOperator->numOfDownstream; ++i) {
H
Haojun Liao 已提交
2755
      destroyOperatorInfo(pOperator->pDownstream[i]);
2756 2757
    }

wafwerar's avatar
wafwerar 已提交
2758
    taosMemoryFreeClear(pOperator->pDownstream);
H
Haojun Liao 已提交
2759
    pOperator->numOfDownstream = 0;
2760 2761
  }

2762
  cleanupExprSupp(&pOperator->exprSupp);
wafwerar's avatar
wafwerar 已提交
2763
  taosMemoryFreeClear(pOperator);
2764 2765
}

2766 2767 2768 2769 2770 2771
int32_t getBufferPgSize(int32_t rowSize, uint32_t* defaultPgsz, uint32_t* defaultBufsz) {
  *defaultPgsz = 4096;
  while (*defaultPgsz < rowSize * 4) {
    *defaultPgsz <<= 1u;
  }

2772
  // The default buffer for each operator in query is 10MB.
2773
  // at least four pages need to be in buffer
2774 2775
  // TODO: make this variable to be configurable.
  *defaultBufsz = 4096 * 2560;
2776 2777 2778 2779 2780 2781 2782
  if ((*defaultBufsz) <= (*defaultPgsz)) {
    (*defaultBufsz) = (*defaultPgsz) * 4;
  }

  return 0;
}

dengyihao's avatar
dengyihao 已提交
2783 2784
int32_t doInitAggInfoSup(SAggSupporter* pAggSup, SqlFunctionCtx* pCtx, int32_t numOfOutput, size_t keyBufSize,
                         const char* pKey) {
L
Liu Jicong 已提交
2785
  int32_t    code = 0;
2786 2787
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);

2788
  pAggSup->currentPageId = -1;
dengyihao's avatar
dengyihao 已提交
2789 2790
  pAggSup->resultRowSize = getResultRowSize(pCtx, numOfOutput);
  pAggSup->keyBuf = taosMemoryCalloc(1, keyBufSize + POINTER_BYTES + sizeof(int64_t));
2791
  pAggSup->pResultRowHashTable = tSimpleHashInit(10, hashFn);
2792

H
Haojun Liao 已提交
2793
  if (pAggSup->keyBuf == NULL || pAggSup->pResultRowHashTable == NULL) {
2794 2795 2796
    return TSDB_CODE_OUT_OF_MEMORY;
  }

dengyihao's avatar
dengyihao 已提交
2797
  uint32_t defaultPgsz = 0;
2798 2799
  uint32_t defaultBufsz = 0;
  getBufferPgSize(pAggSup->resultRowSize, &defaultPgsz, &defaultBufsz);
H
Haojun Liao 已提交
2800

wafwerar's avatar
wafwerar 已提交
2801
  if (!osTempSpaceAvailable()) {
H
Haojun Liao 已提交
2802 2803 2804
    code = TSDB_CODE_NO_AVAIL_DISK;
    qError("Init stream agg supporter failed since %s, %s", terrstr(code), pKey);
    return code;
wafwerar's avatar
wafwerar 已提交
2805
  }
2806

H
Haojun Liao 已提交
2807
  code = createDiskbasedBuf(&pAggSup->pResultBuf, defaultPgsz, defaultBufsz, pKey, tsTempDir);
H
Haojun Liao 已提交
2808
  if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
2809
    qError("Create agg result buf failed since %s, %s", tstrerror(code), pKey);
H
Haojun Liao 已提交
2810 2811 2812
    return code;
  }

H
Haojun Liao 已提交
2813
  return code;
2814 2815
}

2816
void cleanupAggSup(SAggSupporter* pAggSup) {
wafwerar's avatar
wafwerar 已提交
2817
  taosMemoryFreeClear(pAggSup->keyBuf);
2818
  tSimpleHashCleanup(pAggSup->pResultRowHashTable);
H
Haojun Liao 已提交
2819
  destroyDiskbasedBuf(pAggSup->pResultBuf);
2820 2821
}

L
Liu Jicong 已提交
2822 2823
int32_t initAggInfo(SExprSupp* pSup, SAggSupporter* pAggSup, SExprInfo* pExprInfo, int32_t numOfCols, size_t keyBufSize,
                    const char* pkey) {
2824 2825 2826 2827 2828
  int32_t code = initExprSupp(pSup, pExprInfo, numOfCols);
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }

2829 2830 2831 2832 2833
  code = doInitAggInfoSup(pAggSup, pSup->pCtx, numOfCols, keyBufSize, pkey);
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }

L
Liu Jicong 已提交
2834
  for (int32_t i = 0; i < numOfCols; ++i) {
2835
    pSup->pCtx[i].saveHandle.pBuf = pAggSup->pResultBuf;
2836 2837
  }

2838
  return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
2839 2840
}

L
Liu Jicong 已提交
2841
void initResultSizeInfo(SResultInfo* pResultInfo, int32_t numOfRows) {
wmmhello's avatar
wmmhello 已提交
2842
  ASSERT(numOfRows != 0);
2843 2844
  pResultInfo->capacity = numOfRows;
  pResultInfo->threshold = numOfRows * 0.75;
2845

2846 2847
  if (pResultInfo->threshold == 0) {
    pResultInfo->threshold = numOfRows;
2848 2849 2850
  }
}

2851 2852 2853 2854 2855
void initBasicInfo(SOptrBasicInfo* pInfo, SSDataBlock* pBlock) {
  pInfo->pRes = pBlock;
  initResultRowInfo(&pInfo->resultRowInfo);
}

5
54liuyao 已提交
2856
void* destroySqlFunctionCtx(SqlFunctionCtx* pCtx, int32_t numOfOutput) {
2857 2858 2859 2860 2861 2862 2863 2864 2865 2866
  if (pCtx == NULL) {
    return NULL;
  }

  for (int32_t i = 0; i < numOfOutput; ++i) {
    for (int32_t j = 0; j < pCtx[i].numOfParams; ++j) {
      taosVariantDestroy(&pCtx[i].param[j].param);
    }

    taosMemoryFreeClear(pCtx[i].subsidiaries.pCtx);
2867
    taosMemoryFreeClear(pCtx[i].subsidiaries.buf);
2868 2869 2870 2871 2872 2873 2874 2875
    taosMemoryFree(pCtx[i].input.pData);
    taosMemoryFree(pCtx[i].input.pColumnDataAgg);
  }

  taosMemoryFreeClear(pCtx);
  return NULL;
}

2876
int32_t initExprSupp(SExprSupp* pSup, SExprInfo* pExprInfo, int32_t numOfExpr) {
2877 2878 2879 2880
  pSup->pExprInfo = pExprInfo;
  pSup->numOfExprs = numOfExpr;
  if (pSup->pExprInfo != NULL) {
    pSup->pCtx = createSqlFunctionCtx(pExprInfo, numOfExpr, &pSup->rowEntryInfoOffset);
2881 2882 2883
    if (pSup->pCtx == NULL) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
2884
  }
2885 2886

  return TSDB_CODE_SUCCESS;
2887 2888
}

2889 2890 2891 2892
void cleanupExprSupp(SExprSupp* pSupp) {
  destroySqlFunctionCtx(pSupp->pCtx, pSupp->numOfExprs);
  if (pSupp->pExprInfo != NULL) {
    destroyExprInfo(pSupp->pExprInfo, pSupp->numOfExprs);
C
Cary Xu 已提交
2893
    taosMemoryFreeClear(pSupp->pExprInfo);
2894
  }
H
Haojun Liao 已提交
2895 2896 2897 2898 2899 2900

  if (pSupp->pFilterInfo != NULL) {
    filterFreeInfo(pSupp->pFilterInfo);
    pSupp->pFilterInfo = NULL;
  }

2901 2902 2903
  taosMemoryFree(pSupp->rowEntryInfoOffset);
}

2904 2905
SOperatorInfo* createAggregateOperatorInfo(SOperatorInfo* downstream, SAggPhysiNode* pAggNode,
                                           SExecTaskInfo* pTaskInfo) {
wafwerar's avatar
wafwerar 已提交
2906
  SAggOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SAggOperatorInfo));
L
Liu Jicong 已提交
2907
  SOperatorInfo*    pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
H
Haojun Liao 已提交
2908 2909 2910
  if (pInfo == NULL || pOperator == NULL) {
    goto _error;
  }
H
Haojun Liao 已提交
2911

H
Haojun Liao 已提交
2912 2913 2914 2915
  SSDataBlock* pResBlock = createResDataBlock(pAggNode->node.pOutputDataBlockDesc);
  initBasicInfo(&pInfo->binfo, pResBlock);

  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
2916
  initResultSizeInfo(&pOperator->resultInfo, 4096);
H
Haojun Liao 已提交
2917

2918 2919 2920
  int32_t    num = 0;
  SExprInfo* pExprInfo = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &num);
  int32_t    code = initAggInfo(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, num, keyBufSize, pTaskInfo->id.str);
L
Liu Jicong 已提交
2921
  if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
2922 2923
    goto _error;
  }
H
Haojun Liao 已提交
2924

H
Haojun Liao 已提交
2925 2926 2927 2928 2929 2930
  int32_t    numOfScalarExpr = 0;
  SExprInfo* pScalarExprInfo = NULL;
  if (pAggNode->pExprs != NULL) {
    pScalarExprInfo = createExprInfo(pAggNode->pExprs, NULL, &numOfScalarExpr);
  }

2931 2932 2933 2934
  code = initExprSupp(&pInfo->scalarExprSup, pScalarExprInfo, numOfScalarExpr);
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
2935

H
Haojun Liao 已提交
2936 2937 2938 2939 2940
  code = filterInitFromNode((SNode*)pAggNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0);
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }

H
Haojun Liao 已提交
2941
  pInfo->binfo.mergeResultBlock = pAggNode->mergeDataBlock;
2942
  pInfo->groupId = UINT64_MAX;
dengyihao's avatar
dengyihao 已提交
2943
  pOperator->name = "TableAggregate";
2944
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_HASH_AGG;
2945
  pOperator->blocking = true;
dengyihao's avatar
dengyihao 已提交
2946 2947 2948
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
  pOperator->pTaskInfo = pTaskInfo;
H
Haojun Liao 已提交
2949

5
54liuyao 已提交
2950 2951
  pOperator->fpSet =
      createOperatorFpSet(doOpenAggregateOptr, getAggregateResult, NULL, NULL, destroyAggOperatorInfo, NULL);
H
Haojun Liao 已提交
2952

2953 2954 2955 2956 2957 2958
  if (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
    STableScanInfo* pTableScanInfo = downstream->info;
    pTableScanInfo->pdInfo.pExprSup = &pOperator->exprSupp;
    pTableScanInfo->pdInfo.pAggSup = &pInfo->aggSup;
  }

H
Haojun Liao 已提交
2959 2960 2961 2962
  code = appendDownstream(pOperator, &downstream, 1);
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
2963 2964

  return pOperator;
H
Haojun Liao 已提交
2965

2966
_error:
H
Haojun Liao 已提交
2967 2968 2969 2970
  if (pInfo != NULL) {
    destroyAggOperatorInfo(pInfo);
  }

2971 2972 2973
  if (pOperator != NULL) {
    cleanupExprSupp(&pOperator->exprSupp);
  }
H
Haojun Liao 已提交
2974

2975
  taosMemoryFreeClear(pOperator);
H
Haojun Liao 已提交
2976
  pTaskInfo->code = code;
H
Haojun Liao 已提交
2977
  return NULL;
2978 2979
}

2980
void cleanupBasicInfo(SOptrBasicInfo* pInfo) {
2981
  assert(pInfo != NULL);
H
Haojun Liao 已提交
2982
  pInfo->pRes = blockDataDestroy(pInfo->pRes);
2983 2984
}

H
Haojun Liao 已提交
2985 2986 2987 2988 2989 2990 2991
static void freeItem(void* pItem) {
  void** p = pItem;
  if (*p != NULL) {
    taosMemoryFreeClear(*p);
  }
}

2992
void destroyAggOperatorInfo(void* param) {
L
Liu Jicong 已提交
2993
  SAggOperatorInfo* pInfo = (SAggOperatorInfo*)param;
L
Liu Jicong 已提交
2994 2995
  cleanupBasicInfo(&pInfo->binfo);

H
Haojun Liao 已提交
2996
  cleanupAggSup(&pInfo->aggSup);
S
shenglian zhou 已提交
2997
  cleanupExprSupp(&pInfo->scalarExprSup);
H
Haojun Liao 已提交
2998
  cleanupGroupResInfo(&pInfo->groupResInfo);
D
dapan1121 已提交
2999
  taosMemoryFreeClear(param);
3000
}
3001

3002
void destroyFillOperatorInfo(void* param) {
L
Liu Jicong 已提交
3003
  SFillOperatorInfo* pInfo = (SFillOperatorInfo*)param;
3004
  pInfo->pFillInfo = taosDestroyFillInfo(pInfo->pFillInfo);
H
Haojun Liao 已提交
3005
  pInfo->pRes = blockDataDestroy(pInfo->pRes);
H
Haojun Liao 已提交
3006 3007
  pInfo->pFinalRes = blockDataDestroy(pInfo->pFinalRes);

3008
  cleanupExprSupp(&pInfo->noFillExprSupp);
H
Haojun Liao 已提交
3009

wafwerar's avatar
wafwerar 已提交
3010
  taosMemoryFreeClear(pInfo->p);
H
Haojun Liao 已提交
3011
  taosArrayDestroy(pInfo->matchInfo.pList);
D
dapan1121 已提交
3012
  taosMemoryFreeClear(param);
3013 3014
}

3015
void destroyExchangeOperatorInfo(void* param) {
L
Liu Jicong 已提交
3016
  SExchangeInfo* pExInfo = (SExchangeInfo*)param;
3017 3018 3019
  taosRemoveRef(exchangeObjRefPool, pExInfo->self);
}

L
Liu Jicong 已提交
3020
void freeSourceDataInfo(void* p) {
3021 3022 3023 3024
  SSourceDataInfo* pInfo = (SSourceDataInfo*)p;
  taosMemoryFreeClear(pInfo->pRsp);
}

3025
void doDestroyExchangeOperatorInfo(void* param) {
X
Xiaoyu Wang 已提交
3026
  SExchangeInfo* pExInfo = (SExchangeInfo*)param;
3027

H
Haojun Liao 已提交
3028
  taosArrayDestroy(pExInfo->pSources);
3029
  taosArrayDestroyEx(pExInfo->pSourceDataInfo, freeSourceDataInfo);
3030 3031 3032 3033

  if (pExInfo->pResultBlockList != NULL) {
    taosArrayDestroyEx(pExInfo->pResultBlockList, freeBlock);
    pExInfo->pResultBlockList = NULL;
H
Haojun Liao 已提交
3034 3035
  }

3036
  blockDataDestroy(pExInfo->pDummyBlock);
L
Liu Jicong 已提交
3037

3038
  tsem_destroy(&pExInfo->ready);
D
dapan1121 已提交
3039
  taosMemoryFreeClear(param);
H
Haojun Liao 已提交
3040 3041
}

H
Haojun Liao 已提交
3042 3043 3044 3045
static int32_t initFillInfo(SFillOperatorInfo* pInfo, SExprInfo* pExpr, int32_t numOfCols, SExprInfo* pNotFillExpr,
                            int32_t numOfNotFillCols, SNodeListNode* pValNode, STimeWindow win, int32_t capacity,
                            const char* id, SInterval* pInterval, int32_t fillType, int32_t order) {
  SFillColInfo* pColInfo = createFillColInfo(pExpr, numOfCols, pNotFillExpr, numOfNotFillCols, pValNode);
H
Haojun Liao 已提交
3046

H
Haojun Liao 已提交
3047 3048 3049
  int64_t     startKey = (order == TSDB_ORDER_ASC) ? win.skey : win.ekey;
  STimeWindow w = getAlignQueryTimeWindow(pInterval, pInterval->precision, startKey);
  w = getFirstQualifiedTimeWindow(startKey, &w, pInterval, order);
H
Haojun Liao 已提交
3050

L
Liu Jicong 已提交
3051 3052
  pInfo->pFillInfo = taosCreateFillInfo(w.skey, numOfCols, numOfNotFillCols, capacity, pInterval, fillType, pColInfo,
                                        pInfo->primaryTsCol, order, id);
H
Haojun Liao 已提交
3053

H
Haojun Liao 已提交
3054 3055 3056 3057 3058 3059 3060
  if (order == TSDB_ORDER_ASC) {
    pInfo->win.skey = win.skey;
    pInfo->win.ekey = win.ekey;
  } else {
    pInfo->win.skey = win.ekey;
    pInfo->win.ekey = win.skey;
  }
L
Liu Jicong 已提交
3061
  pInfo->p = taosMemoryCalloc(numOfCols, POINTER_BYTES);
3062

H
Haojun Liao 已提交
3063
  if (pInfo->pFillInfo == NULL || pInfo->p == NULL) {
H
Haojun Liao 已提交
3064 3065
    taosMemoryFree(pInfo->pFillInfo);
    taosMemoryFree(pInfo->p);
H
Haojun Liao 已提交
3066 3067 3068 3069 3070 3071
    return TSDB_CODE_OUT_OF_MEMORY;
  } else {
    return TSDB_CODE_SUCCESS;
  }
}

3072
static bool isWstartColumnExist(SFillOperatorInfo* pInfo) {
3073
  if (pInfo->noFillExprSupp.numOfExprs == 0) {
3074 3075
    return false;
  }
3076 3077 3078

  for (int32_t i = 0; i < pInfo->noFillExprSupp.numOfExprs; ++i) {
    SExprInfo* exprInfo = pInfo->noFillExprSupp.pExprInfo + i;
3079
    if (exprInfo->pExpr->nodeType == QUERY_NODE_COLUMN && exprInfo->base.numOfParams == 1 &&
3080 3081 3082 3083 3084 3085 3086
        exprInfo->base.pParam[0].pCol->colType == COLUMN_TYPE_WINDOW_START) {
      return true;
    }
  }
  return false;
}

3087 3088
static int32_t createPrimaryTsExprIfNeeded(SFillOperatorInfo* pInfo, SFillPhysiNode* pPhyFillNode, SExprSupp* pExprSupp,
                                           const char* idStr) {
3089
  bool wstartExist = isWstartColumnExist(pInfo);
3090

3091 3092
  if (wstartExist == false) {
    if (pPhyFillNode->pWStartTs->type != QUERY_NODE_TARGET) {
3093
      qError("pWStartTs of fill physical node is not a target node, %s", idStr);
3094 3095 3096
      return TSDB_CODE_QRY_SYS_ERROR;
    }

3097 3098
    SExprInfo* pExpr = taosMemoryRealloc(pExprSupp->pExprInfo, (pExprSupp->numOfExprs + 1) * sizeof(SExprInfo));
    if (pExpr == NULL) {
3099 3100 3101
      return TSDB_CODE_OUT_OF_MEMORY;
    }

3102 3103 3104
    createExprFromTargetNode(&pExpr[pExprSupp->numOfExprs], (STargetNode*)pPhyFillNode->pWStartTs);
    pExprSupp->numOfExprs += 1;
    pExprSupp->pExprInfo = pExpr;
3105
  }
3106

3107 3108 3109
  return TSDB_CODE_SUCCESS;
}

L
Liu Jicong 已提交
3110 3111
SOperatorInfo* createFillOperatorInfo(SOperatorInfo* downstream, SFillPhysiNode* pPhyFillNode,
                                      SExecTaskInfo* pTaskInfo) {
3112 3113 3114 3115 3116 3117
  SFillOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SFillOperatorInfo));
  SOperatorInfo*     pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
  if (pInfo == NULL || pOperator == NULL) {
    goto _error;
  }

H
Haojun Liao 已提交
3118 3119 3120 3121
  pInfo->pRes = createResDataBlock(pPhyFillNode->node.pOutputDataBlockDesc);
  SExprInfo* pExprInfo = createExprInfo(pPhyFillNode->pFillExprs, NULL, &pInfo->numOfExpr);
  pOperator->exprSupp.pExprInfo = pExprInfo;

3122 3123 3124 3125 3126 3127 3128 3129
  SExprSupp* pNoFillSupp = &pInfo->noFillExprSupp;
  pNoFillSupp->pExprInfo = createExprInfo(pPhyFillNode->pNotFillExprs, NULL, &pNoFillSupp->numOfExprs);
  int32_t code = createPrimaryTsExprIfNeeded(pInfo, pPhyFillNode, pNoFillSupp, pTaskInfo->id.str);
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }

  code = initExprSupp(pNoFillSupp, pNoFillSupp->pExprInfo, pNoFillSupp->numOfExprs);
3130 3131 3132
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
H
Haojun Liao 已提交
3133

L
Liu Jicong 已提交
3134
  SInterval* pInterval =
3135
      QUERY_NODE_PHYSICAL_PLAN_MERGE_ALIGNED_INTERVAL == downstream->operatorType
3136 3137
      ? &((SMergeAlignedIntervalAggOperatorInfo*)downstream->info)->intervalAggOperatorInfo->interval
      : &((SIntervalAggOperatorInfo*)downstream->info)->interval;
3138

3139
  int32_t order = (pPhyFillNode->inputTsOrder == ORDER_ASC) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC;
3140
  int32_t type = convertFillType(pPhyFillNode->mode);
3141

H
Haojun Liao 已提交
3142
  SResultInfo* pResultInfo = &pOperator->resultInfo;
H
Haojun Liao 已提交
3143

3144
  initResultSizeInfo(&pOperator->resultInfo, 4096);
H
Haojun Liao 已提交
3145 3146 3147 3148 3149
  blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
  code = initExprSupp(&pOperator->exprSupp, pExprInfo, pInfo->numOfExpr);
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
H
Haojun Liao 已提交
3150

H
Haojun Liao 已提交
3151 3152
  pInfo->primaryTsCol = ((STargetNode*)pPhyFillNode->pWStartTs)->slotId;
  pInfo->primarySrcSlotId = ((SColumnNode*)((STargetNode*)pPhyFillNode->pWStartTs)->pExpr)->slotId;
3153

3154
  int32_t numOfOutputCols = 0;
H
Haojun Liao 已提交
3155 3156
  code = extractColMatchInfo(pPhyFillNode->pFillExprs, pPhyFillNode->node.pOutputDataBlockDesc, &numOfOutputCols,
                             COL_MATCH_FROM_SLOT_ID, &pInfo->matchInfo);
3157

3158
  code = initFillInfo(pInfo, pExprInfo, pInfo->numOfExpr, pNoFillSupp->pExprInfo, pNoFillSupp->numOfExprs,
3159 3160
                      (SNodeListNode*)pPhyFillNode->pValues, pPhyFillNode->timeRange, pResultInfo->capacity,
                      pTaskInfo->id.str, pInterval, type, order);
3161 3162 3163
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
3164

H
Haojun Liao 已提交
3165
  pInfo->pFinalRes = createOneDataBlock(pInfo->pRes, false);
H
Haojun Liao 已提交
3166 3167
  blockDataEnsureCapacity(pInfo->pFinalRes, pOperator->resultInfo.capacity);

H
Haojun Liao 已提交
3168 3169 3170 3171 3172
  code = filterInitFromNode((SNode*)pPhyFillNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0);
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }

3173 3174 3175 3176
  pOperator->name = "FillOperator";
  pOperator->blocking = false;
  pOperator->status = OP_NOT_OPENED;
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_FILL;
3177
  pOperator->exprSupp.numOfExprs = pInfo->numOfExpr;
3178 3179
  pOperator->info = pInfo;
  pOperator->pTaskInfo = pTaskInfo;
H
Haojun Liao 已提交
3180

5
54liuyao 已提交
3181
  pOperator->fpSet = createOperatorFpSet(operatorDummyOpenFn, doFill, NULL, NULL, destroyFillOperatorInfo, NULL);
3182

3183
  code = appendDownstream(pOperator, &downstream, 1);
3184
  return pOperator;
H
Haojun Liao 已提交
3185

3186
_error:
H
Haojun Liao 已提交
3187 3188 3189 3190
  if (pInfo != NULL) {
    destroyFillOperatorInfo(pInfo);
  }

3191
  pTaskInfo->code = code;
wafwerar's avatar
wafwerar 已提交
3192
  taosMemoryFreeClear(pOperator);
H
Haojun Liao 已提交
3193
  return NULL;
3194 3195
}

D
dapan1121 已提交
3196
static SExecTaskInfo* createExecTaskInfo(uint64_t queryId, uint64_t taskId, EOPTR_EXEC_MODEL model, char* dbFName) {
wafwerar's avatar
wafwerar 已提交
3197
  SExecTaskInfo* pTaskInfo = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
H
Haojun Liao 已提交
3198 3199 3200 3201 3202
  if (pTaskInfo == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

3203
  setTaskStatus(pTaskInfo, TASK_NOT_COMPLETED);
H
Haojun Liao 已提交
3204

3205
  pTaskInfo->schemaInfo.dbname = strdup(dbFName);
3206
  pTaskInfo->cost.created = taosGetTimestampMs();
H
Haojun Liao 已提交
3207
  pTaskInfo->id.queryId = queryId;
dengyihao's avatar
dengyihao 已提交
3208
  pTaskInfo->execModel = model;
H
Haojun Liao 已提交
3209
  pTaskInfo->pTableInfoList = tableListCreate();
H
Haojun Liao 已提交
3210

wafwerar's avatar
wafwerar 已提交
3211
  char* p = taosMemoryCalloc(1, 128);
L
Liu Jicong 已提交
3212
  snprintf(p, 128, "TID:0x%" PRIx64 " QID:0x%" PRIx64, taskId, queryId);
H
Haojun Liao 已提交
3213
  pTaskInfo->id.str = p;
H
Haojun Liao 已提交
3214

3215 3216
  return pTaskInfo;
}
H
Haojun Liao 已提交
3217

H
Haojun Liao 已提交
3218 3219
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode);

3220
int32_t extractTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, SExecTaskInfo* pTaskInfo) {
3221 3222
  SMetaReader mr = {0};
  metaReaderInit(&mr, pHandle->meta, 0);
3223
  int32_t code = metaGetTableEntryByUid(&mr, pScanNode->uid);
3224
  if (code != TSDB_CODE_SUCCESS) {
L
Liu Jicong 已提交
3225 3226
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
           GET_TASKID(pTaskInfo));
H
Haojun Liao 已提交
3227

D
dapan1121 已提交
3228
    metaReaderClear(&mr);
3229
    return terrno;
D
dapan1121 已提交
3230
  }
3231

3232 3233
  SSchemaInfo* pSchemaInfo = &pTaskInfo->schemaInfo;
  pSchemaInfo->tablename = strdup(mr.me.name);
3234 3235

  if (mr.me.type == TSDB_SUPER_TABLE) {
3236 3237
    pSchemaInfo->sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
    pSchemaInfo->tversion = mr.me.stbEntry.schemaTag.version;
3238
  } else if (mr.me.type == TSDB_CHILD_TABLE) {
3239 3240
    tDecoderClear(&mr.coder);

3241 3242
    tb_uid_t suid = mr.me.ctbEntry.suid;
    metaGetTableEntryByUid(&mr, suid);
3243 3244
    pSchemaInfo->sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
    pSchemaInfo->tversion = mr.me.stbEntry.schemaTag.version;
3245
  } else {
3246
    pSchemaInfo->sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
3247
  }
3248 3249

  metaReaderClear(&mr);
3250

H
Haojun Liao 已提交
3251 3252 3253 3254 3255
  pSchemaInfo->qsw = extractQueriedColumnSchema(pScanNode);
  return TSDB_CODE_SUCCESS;
}

SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
3256 3257 3258
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);

3259
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
3260
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
3261

L
Liu Jicong 已提交
3262
  for (int32_t i = 0; i < numOfCols; ++i) {
H
Haojun Liao 已提交
3263
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
3264 3265
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;

H
Haojun Liao 已提交
3266 3267 3268
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
    pSchema->colId = pColNode->colId;
    pSchema->type = pColNode->node.resType.type;
H
Haojun Liao 已提交
3269 3270
    pSchema->bytes = pColNode->node.resType.bytes;
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
3271 3272
  }

3273
  // this the tags and pseudo function columns, we only keep the tag columns
3274
  for (int32_t i = 0; i < numOfTags; ++i) {
3275 3276 3277 3278 3279 3280 3281 3282 3283
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);

    int32_t type = nodeType(pNode->pExpr);
    if (type == QUERY_NODE_COLUMN) {
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;

      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
      pSchema->colId = pColNode->colId;
      pSchema->type = pColNode->node.resType.type;
H
Haojun Liao 已提交
3284
      pSchema->bytes = pColNode->node.resType.bytes;
H
Haojun Liao 已提交
3285
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
3286 3287 3288
    }
  }

H
Haojun Liao 已提交
3289
  return pqSw;
3290 3291
}

3292 3293
static void cleanupTableSchemaInfo(SSchemaInfo* pSchemaInfo) {
  taosMemoryFreeClear(pSchemaInfo->dbname);
3294
  taosMemoryFreeClear(pSchemaInfo->tablename);
3295 3296
  tDeleteSSchemaWrapper(pSchemaInfo->sw);
  tDeleteSSchemaWrapper(pSchemaInfo->qsw);
3297 3298
}

3299
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) { tDeleteSSchemaWrapper(pStreamInfo->schema); }
3300

3301
bool groupbyTbname(SNodeList* pGroupList) {
3302
  bool bytbname = false;
3303
  if (LIST_LENGTH(pGroupList) == 1) {
3304 3305 3306 3307 3308 3309 3310 3311 3312 3313
    SNode* p = nodesListGetNode(pGroupList, 0);
    if (p->type == QUERY_NODE_FUNCTION) {
      // partition by tbname/group by tbname
      bytbname = (strcmp(((struct SFunctionNode*)p)->functionName, "tbname") == 0);
    }
  }

  return bytbname;
}

3314 3315
SOperatorInfo* createOperatorTree(SPhysiNode* pPhyNode, SExecTaskInfo* pTaskInfo, SReadHandle* pHandle, SNode* pTagCond,
                                  SNode* pTagIndexCond, const char* pUser) {
3316
  int32_t type = nodeType(pPhyNode);
3317 3318
  STableListInfo* pTableListInfo = pTaskInfo->pTableInfoList;
  const char* idstr = GET_TASKID(pTaskInfo);
3319

X
Xiaoyu Wang 已提交
3320
  if (pPhyNode->pChildren == NULL || LIST_LENGTH(pPhyNode->pChildren) == 0) {
3321
    SOperatorInfo* pOperator = NULL;
H
Haojun Liao 已提交
3322
    if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == type) {
dengyihao's avatar
dengyihao 已提交
3323
      STableScanPhysiNode* pTableScanNode = (STableScanPhysiNode*)pPhyNode;
H
Haojun Liao 已提交
3324

3325 3326 3327 3328 3329 3330
      // NOTE: this is an patch to fix the physical plan
      // TODO remove it later
      if (pTableScanNode->scan.node.pLimit != NULL) {
        pTableScanNode->groupSort = true;
      }

L
Liu Jicong 已提交
3331 3332
      int32_t code =
          createScanTableListInfo(&pTableScanNode->scan, pTableScanNode->pGroupTags, pTableScanNode->groupSort, pHandle,
H
Haojun Liao 已提交
3333
                                  pTableListInfo, pTagCond, pTagIndexCond, pTaskInfo);
3334
      if (code) {
wmmhello's avatar
wmmhello 已提交
3335
        pTaskInfo->code = code;
3336
        qError("failed to createScanTableListInfo, code:%s, %s", tstrerror(code), idstr);
D
dapan1121 已提交
3337 3338
        return NULL;
      }
wmmhello's avatar
wmmhello 已提交
3339

3340
      code = extractTableSchemaInfo(pHandle, &pTableScanNode->scan, pTaskInfo);
S
slzhou 已提交
3341
      if (code) {
3342
        pTaskInfo->code = terrno;
wmmhello's avatar
wmmhello 已提交
3343 3344 3345
        return NULL;
      }

3346
      pOperator = createTableScanOperatorInfo(pTableScanNode, pHandle, pTaskInfo);
3347 3348
      STableScanInfo* pScanInfo = pOperator->info;
      pTaskInfo->cost.pRecoder = &pScanInfo->readRecorder;
S
slzhou 已提交
3349 3350
    } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == type) {
      STableMergeScanPhysiNode* pTableScanNode = (STableMergeScanPhysiNode*)pPhyNode;
H
Haojun Liao 已提交
3351 3352 3353

      int32_t code = createScanTableListInfo(&pTableScanNode->scan, pTableScanNode->pGroupTags, true, pHandle,
                                             pTableListInfo, pTagCond, pTagIndexCond, pTaskInfo);
L
Liu Jicong 已提交
3354
      if (code) {
wmmhello's avatar
wmmhello 已提交
3355
        pTaskInfo->code = code;
H
Haojun Liao 已提交
3356
        qError("failed to createScanTableListInfo, code: %s", tstrerror(code));
wmmhello's avatar
wmmhello 已提交
3357 3358
        return NULL;
      }
3359

3360
      code = extractTableSchemaInfo(pHandle, &pTableScanNode->scan, pTaskInfo);
wmmhello's avatar
wmmhello 已提交
3361 3362 3363 3364
      if (code) {
        pTaskInfo->code = terrno;
        return NULL;
      }
wmmhello's avatar
wmmhello 已提交
3365

3366
      pOperator = createTableMergeScanOperatorInfo(pTableScanNode, pTableListInfo, pHandle, pTaskInfo);
wmmhello's avatar
wmmhello 已提交
3367

3368 3369
      STableScanInfo* pScanInfo = pOperator->info;
      pTaskInfo->cost.pRecoder = &pScanInfo->readRecorder;
H
Haojun Liao 已提交
3370
    } else if (QUERY_NODE_PHYSICAL_PLAN_EXCHANGE == type) {
3371 3372
      pOperator = createExchangeOperatorInfo(pHandle ? pHandle->pMsgCb->clientRpc : NULL, (SExchangePhysiNode*)pPhyNode,
                                             pTaskInfo);
H
Haojun Liao 已提交
3373
    } else if (QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN == type) {
5
54liuyao 已提交
3374
      STableScanPhysiNode* pTableScanNode = (STableScanPhysiNode*)pPhyNode;
5
54liuyao 已提交
3375
      if (pHandle->vnode) {
L
Liu Jicong 已提交
3376 3377
        int32_t code =
            createScanTableListInfo(&pTableScanNode->scan, pTableScanNode->pGroupTags, pTableScanNode->groupSort,
H
Haojun Liao 已提交
3378
                                    pHandle, pTableListInfo, pTagCond, pTagIndexCond, pTaskInfo);
L
Liu Jicong 已提交
3379
        if (code) {
wmmhello's avatar
wmmhello 已提交
3380
          pTaskInfo->code = code;
H
Haojun Liao 已提交
3381
          qError("failed to createScanTableListInfo, code: %s", tstrerror(code));
wmmhello's avatar
wmmhello 已提交
3382 3383
          return NULL;
        }
L
Liu Jicong 已提交
3384 3385

#ifndef NDEBUG
H
Haojun Liao 已提交
3386
        int32_t sz = tableListGetSize(pTableListInfo);
H
Haojun Liao 已提交
3387 3388
        qDebug("create stream task, total:%d", sz);

L
Liu Jicong 已提交
3389
        for (int32_t i = 0; i < sz; i++) {
H
Haojun Liao 已提交
3390
          STableKeyInfo* pKeyInfo = tableListGetInfo(pTableListInfo, i);
H
Haojun Liao 已提交
3391
          qDebug("add table uid:%" PRIu64", gid:%"PRIu64, pKeyInfo->uid, pKeyInfo->groupId);
L
Liu Jicong 已提交
3392 3393
        }
#endif
3394
      }
3395

H
Haojun Liao 已提交
3396
      pTaskInfo->schemaInfo.qsw = extractQueriedColumnSchema(&pTableScanNode->scan);
3397
      pOperator = createStreamScanOperatorInfo(pHandle, pTableScanNode, pTagCond, pTaskInfo);
H
Haojun Liao 已提交
3398
    } else if (QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == type) {
L
Liu Jicong 已提交
3399
      SSystemTableScanPhysiNode* pSysScanPhyNode = (SSystemTableScanPhysiNode*)pPhyNode;
3400
      pOperator = createSysTableScanOperatorInfo(pHandle, pSysScanPhyNode, pUser, pTaskInfo);
3401
    } else if (QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN == type) {
X
Xiaoyu Wang 已提交
3402
      STagScanPhysiNode* pScanPhyNode = (STagScanPhysiNode*)pPhyNode;
3403 3404

      int32_t code = createScanTableListInfo(pScanPhyNode, NULL, false, pHandle, pTableListInfo, pTagCond,
H
Haojun Liao 已提交
3405
                                             pTagIndexCond, pTaskInfo);
3406
      if (code != TSDB_CODE_SUCCESS) {
3407
        pTaskInfo->code = code;
H
Haojun Liao 已提交
3408
        qError("failed to getTableList, code: %s", tstrerror(code));
3409 3410 3411
        return NULL;
      }

3412
      pOperator = createTagScanOperatorInfo(pHandle, pScanPhyNode, pTableListInfo, pTaskInfo);
3413
    } else if (QUERY_NODE_PHYSICAL_PLAN_BLOCK_DIST_SCAN == type) {
3414
      SBlockDistScanPhysiNode* pBlockNode = (SBlockDistScanPhysiNode*)pPhyNode;
3415 3416

      if (pBlockNode->tableType == TSDB_SUPER_TABLE) {
H
Haojun Liao 已提交
3417 3418
        SArray* pList = taosArrayInit(4, sizeof(STableKeyInfo));
        int32_t code = vnodeGetAllTableList(pHandle->vnode, pBlockNode->uid, pList);
3419 3420 3421 3422
        if (code != TSDB_CODE_SUCCESS) {
          pTaskInfo->code = terrno;
          return NULL;
        }
H
Haojun Liao 已提交
3423

H
Haojun Liao 已提交
3424
        for(int32_t i = 0; i < tableListGetSize(pTableListInfo); ++i) {
H
Haojun Liao 已提交
3425
          STableKeyInfo* p = taosArrayGet(pList, i);
H
Haojun Liao 已提交
3426
          tableListAddTableInfo(pTableListInfo, p->uid, 0);
H
Haojun Liao 已提交
3427 3428
        }
        taosArrayDestroy(pList);
3429
      } else {  // Create group with only one table
H
Haojun Liao 已提交
3430
        tableListAddTableInfo(pTableListInfo, pBlockNode->uid, 0);
3431 3432
      }

3433
      pOperator = createDataBlockInfoScanOperator(pHandle, pBlockNode, pTaskInfo);
H
Haojun Liao 已提交
3434 3435 3436
    } else if (QUERY_NODE_PHYSICAL_PLAN_LAST_ROW_SCAN == type) {
      SLastRowScanPhysiNode* pScanNode = (SLastRowScanPhysiNode*)pPhyNode;

L
Liu Jicong 已提交
3437
      int32_t code = createScanTableListInfo(&pScanNode->scan, pScanNode->pGroupTags, true, pHandle, pTableListInfo,
H
Haojun Liao 已提交
3438
                                             pTagCond, pTagIndexCond, pTaskInfo);
3439 3440 3441 3442
      if (code != TSDB_CODE_SUCCESS) {
        pTaskInfo->code = code;
        return NULL;
      }
3443

3444
      code = extractTableSchemaInfo(pHandle, &pScanNode->scan, pTaskInfo);
3445 3446 3447
      if (code != TSDB_CODE_SUCCESS) {
        pTaskInfo->code = code;
        return NULL;
H
Haojun Liao 已提交
3448 3449
      }

3450
      pOperator = createCacherowsScanOperator(pScanNode, pHandle, pTaskInfo);
3451
    } else if (QUERY_NODE_PHYSICAL_PLAN_PROJECT == type) {
3452
      pOperator = createProjectOperatorInfo(NULL, (SProjectPhysiNode*)pPhyNode, pTaskInfo);
H
Haojun Liao 已提交
3453 3454
    } else {
      ASSERT(0);
H
Haojun Liao 已提交
3455
    }
3456 3457 3458 3459 3460

    if (pOperator != NULL) {
      pOperator->resultDataBlockId = pPhyNode->pOutputDataBlockDesc->dataBlockId;
    }

3461
    return pOperator;
H
Haojun Liao 已提交
3462 3463
  }

3464
  size_t  size = LIST_LENGTH(pPhyNode->pChildren);
3465
  SOperatorInfo** ops = taosMemoryCalloc(size, POINTER_BYTES);
3466 3467 3468 3469
  if (ops == NULL) {
    return NULL;
  }

dengyihao's avatar
dengyihao 已提交
3470
  for (int32_t i = 0; i < size; ++i) {
3471
    SPhysiNode* pChildNode = (SPhysiNode*)nodesListGetNode(pPhyNode->pChildren, i);
3472
    ops[i] = createOperatorTree(pChildNode, pTaskInfo, pHandle, pTagCond, pTagIndexCond, pUser);
3473
    if (ops[i] == NULL) {
H
Haojun Liao 已提交
3474
      taosMemoryFree(ops);
3475 3476
      return NULL;
    }
3477
  }
H
Haojun Liao 已提交
3478

3479
  SOperatorInfo* pOptr = NULL;
H
Haojun Liao 已提交
3480
  if (QUERY_NODE_PHYSICAL_PLAN_PROJECT == type) {
3481
    pOptr = createProjectOperatorInfo(ops[0], (SProjectPhysiNode*)pPhyNode, pTaskInfo);
3482
  } else if (QUERY_NODE_PHYSICAL_PLAN_HASH_AGG == type) {
H
Haojun Liao 已提交
3483 3484
    SAggPhysiNode* pAggNode = (SAggPhysiNode*)pPhyNode;
    if (pAggNode->pGroupKeys != NULL) {
H
Haojun Liao 已提交
3485
      pOptr = createGroupOperatorInfo(ops[0], pAggNode, pTaskInfo);
H
Haojun Liao 已提交
3486
    } else {
H
Haojun Liao 已提交
3487
      pOptr = createAggregateOperatorInfo(ops[0], pAggNode, pTaskInfo);
H
Haojun Liao 已提交
3488
    }
3489
  } else if (QUERY_NODE_PHYSICAL_PLAN_HASH_INTERVAL == type) {
H
Haojun Liao 已提交
3490
    SIntervalPhysiNode* pIntervalPhyNode = (SIntervalPhysiNode*)pPhyNode;
H
Haojun Liao 已提交
3491

H
Haojun Liao 已提交
3492 3493
    bool isStream = (QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL == type);
    pOptr = createIntervalOperatorInfo(ops[0], pIntervalPhyNode, pTaskInfo, isStream);
3494
  } else if (QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL == type) {
3495
    pOptr = createStreamIntervalOperatorInfo(ops[0], pPhyNode, pTaskInfo);
3496 3497
  } else if (QUERY_NODE_PHYSICAL_PLAN_MERGE_ALIGNED_INTERVAL == type) {
    SMergeAlignedIntervalPhysiNode* pIntervalPhyNode = (SMergeAlignedIntervalPhysiNode*)pPhyNode;
3498
    pOptr = createMergeAlignedIntervalOperatorInfo(ops[0], pIntervalPhyNode, pTaskInfo);
S
shenglian zhou 已提交
3499
  } else if (QUERY_NODE_PHYSICAL_PLAN_MERGE_INTERVAL == type) {
X
Xiaoyu Wang 已提交
3500
    SMergeIntervalPhysiNode* pIntervalPhyNode = (SMergeIntervalPhysiNode*)pPhyNode;
3501
    pOptr = createMergeIntervalOperatorInfo(ops[0], pIntervalPhyNode, pTaskInfo);
5
54liuyao 已提交
3502
  } else if (QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_INTERVAL == type) {
3503
    int32_t children = 0;
5
54liuyao 已提交
3504 3505
    pOptr = createStreamFinalIntervalOperatorInfo(ops[0], pPhyNode, pTaskInfo, children);
  } else if (QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_INTERVAL == type) {
5
54liuyao 已提交
3506
    int32_t children = pHandle->numOfVgroups;
5
54liuyao 已提交
3507
    pOptr = createStreamFinalIntervalOperatorInfo(ops[0], pPhyNode, pTaskInfo, children);
H
Haojun Liao 已提交
3508
  } else if (QUERY_NODE_PHYSICAL_PLAN_SORT == type) {
3509
    pOptr = createSortOperatorInfo(ops[0], (SSortPhysiNode*)pPhyNode, pTaskInfo);
S
shenglian zhou 已提交
3510 3511
  } else if (QUERY_NODE_PHYSICAL_PLAN_GROUP_SORT == type) {
    pOptr = createGroupSortOperatorInfo(ops[0], (SGroupSortPhysiNode*)pPhyNode, pTaskInfo);
X
Xiaoyu Wang 已提交
3512
  } else if (QUERY_NODE_PHYSICAL_PLAN_MERGE == type) {
3513
    SMergePhysiNode* pMergePhyNode = (SMergePhysiNode*)pPhyNode;
3514
    pOptr = createMultiwayMergeOperatorInfo(ops, size, pMergePhyNode, pTaskInfo);
3515
  } else if (QUERY_NODE_PHYSICAL_PLAN_MERGE_SESSION == type) {
H
Haojun Liao 已提交
3516
    SSessionWinodwPhysiNode* pSessionNode = (SSessionWinodwPhysiNode*)pPhyNode;
H
Haojun Liao 已提交
3517
    pOptr = createSessionAggOperatorInfo(ops[0], pSessionNode, pTaskInfo);
3518
  } else if (QUERY_NODE_PHYSICAL_PLAN_STREAM_SESSION == type) {
3519 3520 3521 3522 3523
    pOptr = createStreamSessionAggOperatorInfo(ops[0], pPhyNode, pTaskInfo);
  } else if (QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_SESSION == type) {
    int32_t children = 0;
    pOptr = createStreamFinalSessionAggOperatorInfo(ops[0], pPhyNode, pTaskInfo, children);
  } else if (QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_SESSION == type) {
3524
    int32_t children = pHandle->numOfVgroups;
3525
    pOptr = createStreamFinalSessionAggOperatorInfo(ops[0], pPhyNode, pTaskInfo, children);
H
Haojun Liao 已提交
3526
  } else if (QUERY_NODE_PHYSICAL_PLAN_PARTITION == type) {
3527
    pOptr = createPartitionOperatorInfo(ops[0], (SPartitionPhysiNode*)pPhyNode, pTaskInfo);
3528
  } else if (QUERY_NODE_PHYSICAL_PLAN_STREAM_PARTITION == type) {
3529
    pOptr = createStreamPartitionOperatorInfo(ops[0], (SStreamPartitionPhysiNode*)pPhyNode, pTaskInfo);
3530
  } else if (QUERY_NODE_PHYSICAL_PLAN_MERGE_STATE == type) {
dengyihao's avatar
dengyihao 已提交
3531
    SStateWinodwPhysiNode* pStateNode = (SStateWinodwPhysiNode*)pPhyNode;
3532
    pOptr = createStatewindowOperatorInfo(ops[0], pStateNode, pTaskInfo);
3533
  } else if (QUERY_NODE_PHYSICAL_PLAN_STREAM_STATE == type) {
5
54liuyao 已提交
3534
    pOptr = createStreamStateAggOperatorInfo(ops[0], pPhyNode, pTaskInfo);
3535
  } else if (QUERY_NODE_PHYSICAL_PLAN_MERGE_JOIN == type) {
3536
    pOptr = createMergeJoinOperatorInfo(ops, size, (SSortMergeJoinPhysiNode*)pPhyNode, pTaskInfo);
3537
  } else if (QUERY_NODE_PHYSICAL_PLAN_FILL == type) {
H
Haojun Liao 已提交
3538
    pOptr = createFillOperatorInfo(ops[0], (SFillPhysiNode*)pPhyNode, pTaskInfo);
5
54liuyao 已提交
3539 3540
  } else if (QUERY_NODE_PHYSICAL_PLAN_STREAM_FILL == type) {
    pOptr = createStreamFillOperatorInfo(ops[0], (SStreamFillPhysiNode*)pPhyNode, pTaskInfo);
H
Haojun Liao 已提交
3541 3542
  } else if (QUERY_NODE_PHYSICAL_PLAN_INDEF_ROWS_FUNC == type) {
    pOptr = createIndefinitOutputOperatorInfo(ops[0], pPhyNode, pTaskInfo);
3543 3544
  } else if (QUERY_NODE_PHYSICAL_PLAN_INTERP_FUNC == type) {
    pOptr = createTimeSliceOperatorInfo(ops[0], pPhyNode, pTaskInfo);
H
Haojun Liao 已提交
3545 3546
  } else {
    ASSERT(0);
H
Haojun Liao 已提交
3547
  }
3548

3549
  taosMemoryFree(ops);
3550 3551 3552 3553
  if (pOptr) {
    pOptr->resultDataBlockId = pPhyNode->pOutputDataBlockDesc->dataBlockId;
  }

3554
  return pOptr;
3555
}
H
Haojun Liao 已提交
3556

L
Liu Jicong 已提交
3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569
static int32_t extractTbscanInStreamOpTree(SOperatorInfo* pOperator, STableScanInfo** ppInfo) {
  if (pOperator->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
    if (pOperator->numOfDownstream == 0) {
      qError("failed to find stream scan operator");
      return TSDB_CODE_QRY_APP_ERROR;
    }

    if (pOperator->numOfDownstream > 1) {
      qError("join not supported for stream block scan");
      return TSDB_CODE_QRY_APP_ERROR;
    }
    return extractTbscanInStreamOpTree(pOperator->pDownstream[0], ppInfo);
  } else {
3570 3571 3572
    SStreamScanInfo* pInfo = pOperator->info;
    ASSERT(pInfo->pTableScanOp->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN);
    *ppInfo = pInfo->pTableScanOp->info;
L
Liu Jicong 已提交
3573 3574 3575 3576
    return 0;
  }
}

3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598
int32_t extractTableScanNode(SPhysiNode* pNode, STableScanPhysiNode** ppNode) {
  if (pNode->pChildren == NULL || LIST_LENGTH(pNode->pChildren) == 0) {
    if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == pNode->type) {
      *ppNode = (STableScanPhysiNode*)pNode;
      return 0;
    } else {
      ASSERT(0);
      terrno = TSDB_CODE_QRY_APP_ERROR;
      return -1;
    }
  } else {
    if (LIST_LENGTH(pNode->pChildren) != 1) {
      ASSERT(0);
      terrno = TSDB_CODE_QRY_APP_ERROR;
      return -1;
    }
    SPhysiNode* pChildNode = (SPhysiNode*)nodesListGetNode(pNode->pChildren, 0);
    return extractTableScanNode(pChildNode, ppNode);
  }
  return -1;
}

3599
#if 0
L
Liu Jicong 已提交
3600 3601 3602 3603 3604
int32_t rebuildReader(SOperatorInfo* pOperator, SSubplan* plan, SReadHandle* pHandle, int64_t uid, int64_t ts) {
  STableScanInfo* pTableScanInfo = NULL;
  if (extractTbscanInStreamOpTree(pOperator, &pTableScanInfo) < 0) {
    return -1;
  }
3605

L
Liu Jicong 已提交
3606 3607 3608 3609
  STableScanPhysiNode* pNode = NULL;
  if (extractTableScanNode(plan->pNode, &pNode) < 0) {
    ASSERT(0);
  }
3610

H
Haojun Liao 已提交
3611
  tsdbReaderClose(pTableScanInfo->dataReader);
3612

L
Liu Jicong 已提交
3613
  STableListInfo info = {0};
H
Haojun Liao 已提交
3614
  pTableScanInfo->dataReader = doCreateDataReader(pNode, pHandle, &info, NULL);
L
Liu Jicong 已提交
3615 3616 3617 3618
  if (pTableScanInfo->dataReader == NULL) {
    ASSERT(0);
    qError("failed to create data reader");
    return TSDB_CODE_QRY_APP_ERROR;
3619
  }
L
Liu Jicong 已提交
3620
  // TODO: set uid and ts to data reader
3621 3622
  return 0;
}
3623
#endif
3624

C
Cary Xu 已提交
3625
int32_t encodeOperator(SOperatorInfo* ops, char** result, int32_t* length, int32_t* nOptrWithVal) {
wmmhello's avatar
wmmhello 已提交
3626
  int32_t code = TDB_CODE_SUCCESS;
3627
  char*   pCurrent = NULL;
wmmhello's avatar
wmmhello 已提交
3628
  int32_t currLength = 0;
3629
  if (ops->fpSet.encodeResultRow) {
C
Cary Xu 已提交
3630
    if (result == NULL || length == NULL || nOptrWithVal == NULL) {
wmmhello's avatar
wmmhello 已提交
3631 3632 3633
      return TSDB_CODE_TSC_INVALID_INPUT;
    }
    code = ops->fpSet.encodeResultRow(ops, &pCurrent, &currLength);
wmmhello's avatar
wmmhello 已提交
3634

3635 3636
    if (code != TDB_CODE_SUCCESS) {
      if (*result != NULL) {
wmmhello's avatar
wmmhello 已提交
3637 3638 3639 3640
        taosMemoryFree(*result);
        *result = NULL;
      }
      return code;
C
Cary Xu 已提交
3641 3642 3643
    } else if (currLength == 0) {
      ASSERT(!pCurrent);
      goto _downstream;
wmmhello's avatar
wmmhello 已提交
3644
    }
wmmhello's avatar
wmmhello 已提交
3645

C
Cary Xu 已提交
3646 3647
    ++(*nOptrWithVal);

C
Cary Xu 已提交
3648
    ASSERT(currLength >= 0);
wmmhello's avatar
wmmhello 已提交
3649

3650
    if (*result == NULL) {
wmmhello's avatar
wmmhello 已提交
3651
      *result = (char*)taosMemoryCalloc(1, currLength + sizeof(int32_t));
wmmhello's avatar
wmmhello 已提交
3652 3653 3654 3655 3656 3657
      if (*result == NULL) {
        taosMemoryFree(pCurrent);
        return TSDB_CODE_OUT_OF_MEMORY;
      }
      memcpy(*result + sizeof(int32_t), pCurrent, currLength);
      *(int32_t*)(*result) = currLength + sizeof(int32_t);
3658
    } else {
wmmhello's avatar
wmmhello 已提交
3659
      int32_t sizePre = *(int32_t*)(*result);
3660
      char*   tmp = (char*)taosMemoryRealloc(*result, sizePre + currLength);
wmmhello's avatar
wmmhello 已提交
3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672
      if (tmp == NULL) {
        taosMemoryFree(pCurrent);
        taosMemoryFree(*result);
        *result = NULL;
        return TSDB_CODE_OUT_OF_MEMORY;
      }
      *result = tmp;
      memcpy(*result + sizePre, pCurrent, currLength);
      *(int32_t*)(*result) += currLength;
    }
    taosMemoryFree(pCurrent);
    *length = *(int32_t*)(*result);
wmmhello's avatar
wmmhello 已提交
3673 3674
  }

3675
  _downstream:
wmmhello's avatar
wmmhello 已提交
3676
  for (int32_t i = 0; i < ops->numOfDownstream; ++i) {
C
Cary Xu 已提交
3677
    code = encodeOperator(ops->pDownstream[i], result, length, nOptrWithVal);
3678
    if (code != TDB_CODE_SUCCESS) {
wmmhello's avatar
wmmhello 已提交
3679
      return code;
wmmhello's avatar
wmmhello 已提交
3680 3681
    }
  }
wmmhello's avatar
wmmhello 已提交
3682
  return TDB_CODE_SUCCESS;
wmmhello's avatar
wmmhello 已提交
3683 3684
}

H
Haojun Liao 已提交
3685
int32_t decodeOperator(SOperatorInfo* ops, const char* result, int32_t length) {
wmmhello's avatar
wmmhello 已提交
3686
  int32_t code = TDB_CODE_SUCCESS;
3687 3688
  if (ops->fpSet.decodeResultRow) {
    if (result == NULL) {
wmmhello's avatar
wmmhello 已提交
3689 3690
      return TSDB_CODE_TSC_INVALID_INPUT;
    }
H
Haojun Liao 已提交
3691

3692
    ASSERT(length == *(int32_t*)result);
H
Haojun Liao 已提交
3693 3694

    const char* data = result + sizeof(int32_t);
L
Liu Jicong 已提交
3695
    code = ops->fpSet.decodeResultRow(ops, (char*)data);
3696
    if (code != TDB_CODE_SUCCESS) {
wmmhello's avatar
wmmhello 已提交
3697 3698
      return code;
    }
wmmhello's avatar
wmmhello 已提交
3699

wmmhello's avatar
wmmhello 已提交
3700
    int32_t totalLength = *(int32_t*)result;
3701 3702
    int32_t dataLength = *(int32_t*)data;

3703
    if (totalLength == dataLength + sizeof(int32_t)) {  // the last data
wmmhello's avatar
wmmhello 已提交
3704 3705
      result = NULL;
      length = 0;
3706
    } else {
wmmhello's avatar
wmmhello 已提交
3707 3708 3709 3710
      result += dataLength;
      *(int32_t*)(result) = totalLength - dataLength;
      length = totalLength - dataLength;
    }
wmmhello's avatar
wmmhello 已提交
3711 3712
  }

wmmhello's avatar
wmmhello 已提交
3713 3714
  for (int32_t i = 0; i < ops->numOfDownstream; ++i) {
    code = decodeOperator(ops->pDownstream[i], result, length);
3715
    if (code != TDB_CODE_SUCCESS) {
wmmhello's avatar
wmmhello 已提交
3716
      return code;
wmmhello's avatar
wmmhello 已提交
3717 3718
    }
  }
wmmhello's avatar
wmmhello 已提交
3719
  return TDB_CODE_SUCCESS;
wmmhello's avatar
wmmhello 已提交
3720 3721
}

D
dapan1121 已提交
3722
int32_t createDataSinkParam(SDataSinkNode* pNode, void** pParam, qTaskInfo_t* pTaskInfo, SReadHandle* readHandle) {
D
dapan1121 已提交
3723
  SExecTaskInfo* pTask = *(SExecTaskInfo**)pTaskInfo;
3724

D
dapan1121 已提交
3725
  switch (pNode->type) {
D
dapan1121 已提交
3726 3727 3728 3729 3730 3731
    case QUERY_NODE_PHYSICAL_PLAN_QUERY_INSERT: {
      SInserterParam* pInserterParam = taosMemoryCalloc(1, sizeof(SInserterParam));
      if (NULL == pInserterParam) {
        return TSDB_CODE_OUT_OF_MEMORY;
      }
      pInserterParam->readHandle = readHandle;
L
Liu Jicong 已提交
3732

D
dapan1121 已提交
3733 3734 3735
      *pParam = pInserterParam;
      break;
    }
D
dapan1121 已提交
3736
    case QUERY_NODE_PHYSICAL_PLAN_DELETE: {
3737
      SDeleterParam* pDeleterParam = taosMemoryCalloc(1, sizeof(SDeleterParam));
D
dapan1121 已提交
3738 3739 3740
      if (NULL == pDeleterParam) {
        return TSDB_CODE_OUT_OF_MEMORY;
      }
H
Haojun Liao 已提交
3741 3742 3743 3744
      int32_t tbNum = tableListGetSize(pTask->pTableInfoList);
      pDeleterParam->suid = tableListGetSuid(pTask->pTableInfoList);

      // TODO extract uid list
D
dapan1121 已提交
3745 3746 3747 3748 3749
      pDeleterParam->pUidList = taosArrayInit(tbNum, sizeof(uint64_t));
      if (NULL == pDeleterParam->pUidList) {
        taosMemoryFree(pDeleterParam);
        return TSDB_CODE_OUT_OF_MEMORY;
      }
H
Haojun Liao 已提交
3750

D
dapan1121 已提交
3751
      for (int32_t i = 0; i < tbNum; ++i) {
H
Haojun Liao 已提交
3752
        STableKeyInfo* pTable = tableListGetInfo(pTask->pTableInfoList, i);
D
dapan1121 已提交
3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765
        taosArrayPush(pDeleterParam->pUidList, &pTable->uid);
      }

      *pParam = pDeleterParam;
      break;
    }
    default:
      break;
  }

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
3766
int32_t createExecTaskInfoImpl(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
D
dapan1121 已提交
3767
                               char* sql, EOPTR_EXEC_MODEL model) {
H
Haojun Liao 已提交
3768 3769
  uint64_t queryId = pPlan->id.queryId;

D
dapan1121 已提交
3770
  *pTaskInfo = createExecTaskInfo(queryId, taskId, model, pPlan->dbFName);
H
Haojun Liao 已提交
3771 3772 3773
  if (*pTaskInfo == NULL) {
    goto _complete;
  }
H
Haojun Liao 已提交
3774

3775
  if (pHandle) {
L
Liu Jicong 已提交
3776
    /*(*pTaskInfo)->streamInfo.fillHistoryVer1 = pHandle->fillHistoryVer1;*/
3777 3778 3779
    if (pHandle->pStateBackend) {
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
    }
H
Haojun Liao 已提交
3780 3781
  }

3782
  (*pTaskInfo)->sql = sql;
D
dapan1121 已提交
3783
  sql = NULL;
H
Haojun Liao 已提交
3784

3785
  (*pTaskInfo)->pSubplan = pPlan;
3786 3787
  (*pTaskInfo)->pRoot =
      createOperatorTree(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user);
L
Liu Jicong 已提交
3788

D
dapan1121 已提交
3789
  if (NULL == (*pTaskInfo)->pRoot) {
H
Haojun Liao 已提交
3790
    terrno = (*pTaskInfo)->code;
D
dapan1121 已提交
3791
    goto _complete;
3792 3793
  }

H
Haojun Liao 已提交
3794
  return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
3795

H
Haojun Liao 已提交
3796
_complete:
D
dapan1121 已提交
3797
  taosMemoryFree(sql);
H
Haojun Liao 已提交
3798
  doDestroyTask(*pTaskInfo);
H
Haojun Liao 已提交
3799
  return terrno;
H
Haojun Liao 已提交
3800 3801
}

L
Liu Jicong 已提交
3802
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
H
Haojun Liao 已提交
3803 3804
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));

H
Haojun Liao 已提交
3805
  pTaskInfo->pTableInfoList = tableListDestroy(pTaskInfo->pTableInfoList);
H
Haojun Liao 已提交
3806
  destroyOperatorInfo(pTaskInfo->pRoot);
3807
  cleanupTableSchemaInfo(&pTaskInfo->schemaInfo);
3808
  cleanupStreamInfo(&pTaskInfo->streamInfo);
3809

D
dapan1121 已提交
3810
  if (!pTaskInfo->localFetch.localExec) {
D
dapan1121 已提交
3811 3812
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
  }
3813

wafwerar's avatar
wafwerar 已提交
3814 3815 3816
  taosMemoryFreeClear(pTaskInfo->sql);
  taosMemoryFreeClear(pTaskInfo->id.str);
  taosMemoryFreeClear(pTaskInfo);
3817 3818 3819 3820
}

static int64_t getQuerySupportBufSize(size_t numOfTables) {
  size_t s1 = sizeof(STableQueryInfo);
L
Liu Jicong 已提交
3821 3822
  //  size_t s3 = sizeof(STableCheckInfo);  buffer consumption in tsdb
  return (int64_t)(s1 * 1.5 * numOfTables);
3823 3824 3825 3826 3827 3828 3829
}

int32_t checkForQueryBuf(size_t numOfTables) {
  int64_t t = getQuerySupportBufSize(numOfTables);
  if (tsQueryBufferSizeBytes < 0) {
    return TSDB_CODE_SUCCESS;
  } else if (tsQueryBufferSizeBytes > 0) {
L
Liu Jicong 已提交
3830
    while (1) {
3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856
      int64_t s = tsQueryBufferSizeBytes;
      int64_t remain = s - t;
      if (remain >= 0) {
        if (atomic_val_compare_exchange_64(&tsQueryBufferSizeBytes, s, remain) == s) {
          return TSDB_CODE_SUCCESS;
        }
      } else {
        return TSDB_CODE_QRY_NOT_ENOUGH_BUFFER;
      }
    }
  }

  // disable query processing if the value of tsQueryBufferSize is zero.
  return TSDB_CODE_QRY_NOT_ENOUGH_BUFFER;
}

void releaseQueryBuf(size_t numOfTables) {
  if (tsQueryBufferSizeBytes < 0) {
    return;
  }

  int64_t t = getQuerySupportBufSize(numOfTables);

  // restore value is not enough buffer available
  atomic_add_fetch_64(&tsQueryBufferSizeBytes, t);
}
D
dapan1121 已提交
3857

H
Haojun Liao 已提交
3858
int32_t getOperatorExplainExecInfo(SOperatorInfo* operatorInfo, SArray* pExecInfoList) {
3859
  SExplainExecInfo  execInfo = {0};
H
Haojun Liao 已提交
3860
  SExplainExecInfo* pExplainInfo = taosArrayPush(pExecInfoList, &execInfo);
3861

H
Haojun Liao 已提交
3862 3863 3864 3865 3866
  pExplainInfo->numOfRows = operatorInfo->resultInfo.totalRows;
  pExplainInfo->startupCost = operatorInfo->cost.openCost;
  pExplainInfo->totalCost = operatorInfo->cost.totalCost;
  pExplainInfo->verboseLen = 0;
  pExplainInfo->verboseInfo = NULL;
D
dapan1121 已提交
3867

3868
  if (operatorInfo->fpSet.getExplainFn) {
3869 3870
    int32_t code =
        operatorInfo->fpSet.getExplainFn(operatorInfo, &pExplainInfo->verboseInfo, &pExplainInfo->verboseLen);
D
dapan1121 已提交
3871
    if (code) {
3872
      qError("%s operator getExplainFn failed, code:%s", GET_TASKID(operatorInfo->pTaskInfo), tstrerror(code));
D
dapan1121 已提交
3873 3874 3875
      return code;
    }
  }
dengyihao's avatar
dengyihao 已提交
3876

D
dapan1121 已提交
3877
  int32_t code = 0;
D
dapan1121 已提交
3878
  for (int32_t i = 0; i < operatorInfo->numOfDownstream; ++i) {
H
Haojun Liao 已提交
3879 3880
    code = getOperatorExplainExecInfo(operatorInfo->pDownstream[i], pExecInfoList);
    if (code != TSDB_CODE_SUCCESS) {
3881
      //      taosMemoryFreeClear(*pRes);
D
dapan1121 已提交
3882 3883 3884 3885 3886
      return TSDB_CODE_QRY_OUT_OF_MEMORY;
    }
  }

  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
3887
}
5
54liuyao 已提交
3888

3889 3890
int32_t setOutputBuf(SStreamState* pState, STimeWindow* win, SResultRow** pResult, int64_t tableGroupId,
                     SqlFunctionCtx* pCtx, int32_t numOfOutput, int32_t* rowEntryInfoOffset, SAggSupporter* pAggSup) {
3891 3892 3893 3894 3895 3896
  SWinKey key = {
      .ts = win->skey,
      .groupId = tableGroupId,
  };
  char*   value = NULL;
  int32_t size = pAggSup->resultRowSize;
5
54liuyao 已提交
3897

3898
  if (streamStateAddIfNotExist(pState, &key, (void**)&value, &size) < 0) {
3899 3900 3901 3902 3903 3904 3905 3906 3907 3908
    return TSDB_CODE_QRY_OUT_OF_MEMORY;
  }
  *pResult = (SResultRow*)value;
  ASSERT(*pResult);
  // set time window for current result
  (*pResult)->win = (*win);
  setResultRowInitCtx(*pResult, pCtx, numOfOutput, rowEntryInfoOffset);
  return TSDB_CODE_SUCCESS;
}

3909 3910
int32_t releaseOutputBuf(SStreamState* pState, SWinKey* pKey, SResultRow* pResult) {
  streamStateReleaseBuf(pState, pKey, pResult);
3911 3912 3913
  return TSDB_CODE_SUCCESS;
}

3914 3915
int32_t saveOutputBuf(SStreamState* pState, SWinKey* pKey, SResultRow* pResult, int32_t resSize) {
  streamStatePut(pState, pKey, pResult, resSize);
3916 3917 3918
  return TSDB_CODE_SUCCESS;
}

3919
int32_t buildDataBlockFromGroupRes(SOperatorInfo* pOperator, SStreamState* pState, SSDataBlock* pBlock, SExprSupp* pSup,
3920
                                   SGroupResInfo* pGroupResInfo) {
3921
  SExecTaskInfo*  pTaskInfo = pOperator->pTaskInfo;
3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933
  SExprInfo*      pExprInfo = pSup->pExprInfo;
  int32_t         numOfExprs = pSup->numOfExprs;
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
  SqlFunctionCtx* pCtx = pSup->pCtx;

  int32_t numOfRows = getNumOfTotalRes(pGroupResInfo);

  for (int32_t i = pGroupResInfo->index; i < numOfRows; i += 1) {
    SResKeyPos* pPos = taosArrayGetP(pGroupResInfo->pRows, i);
    int32_t     size = 0;
    void*       pVal = NULL;
    SWinKey     key = {
3934 3935
        .ts = *(TSKEY*)pPos->key,
        .groupId = pPos->groupId,
3936
    };
3937
    int32_t code = streamStateGet(pState, &key, &pVal, &size);
3938 3939 3940 3941 3942 3943
    ASSERT(code == 0);
    SResultRow* pRow = (SResultRow*)pVal;
    doUpdateNumOfRows(pCtx, pRow, numOfExprs, rowEntryOffset);
    // no results, continue to check the next one
    if (pRow->numOfRows == 0) {
      pGroupResInfo->index += 1;
3944
      releaseOutputBuf(pState, &key, pRow);
3945 3946 3947 3948 3949
      continue;
    }

    if (pBlock->info.groupId == 0) {
      pBlock->info.groupId = pPos->groupId;
3950 3951 3952 3953
      SStreamIntervalOperatorInfo* pInfo = pOperator->info;
      char* tbname = taosHashGet(pInfo->pGroupIdTbNameMap, &pBlock->info.groupId, sizeof(int64_t));
      if (tbname != NULL) {
        memcpy(pBlock->info.parTbName, tbname, TSDB_TABLE_NAME_LEN);
L
Liu Jicong 已提交
3954 3955
      } else {
        pBlock->info.parTbName[0] = 0;
3956
      }
3957 3958 3959
    } else {
      // current value belongs to different group, it can't be packed into one datablock
      if (pBlock->info.groupId != pPos->groupId) {
3960
        releaseOutputBuf(pState, &key, pRow);
3961 3962 3963 3964 3965 3966
        break;
      }
    }

    if (pBlock->info.rows + pRow->numOfRows > pBlock->info.capacity) {
      ASSERT(pBlock->info.rows > 0);
3967
      releaseOutputBuf(pState, &key, pRow);
3968 3969 3970 3971 3972 3973 3974 3975 3976 3977
      break;
    }

    pGroupResInfo->index += 1;

    for (int32_t j = 0; j < numOfExprs; ++j) {
      int32_t slotId = pExprInfo[j].base.resSchema.slotId;

      pCtx[j].resultInfo = getResultEntryInfo(pRow, j, rowEntryOffset);
      if (pCtx[j].fpSet.finalize) {
3978 3979 3980 3981
        int32_t code1 = pCtx[j].fpSet.finalize(&pCtx[j], pBlock);
        if (TAOS_FAILED(code1)) {
          qError("%s build result data block error, code %s", GET_TASKID(pTaskInfo), tstrerror(code1));
          T_LONG_JMP(pTaskInfo->env, code1);
3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994
        }
      } else if (strcmp(pCtx[j].pExpr->pExpr->_function.functionName, "_select_value") == 0) {
        // do nothing, todo refactor
      } else {
        // expand the result into multiple rows. E.g., _wstart, top(k, 20)
        // the _wstart needs to copy to 20 following rows, since the results of top-k expands to 20 different rows.
        SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, slotId);
        char*            in = GET_ROWCELL_INTERBUF(pCtx[j].resultInfo);
        for (int32_t k = 0; k < pRow->numOfRows; ++k) {
          colDataAppend(pColInfoData, pBlock->info.rows + k, in, pCtx[j].resultInfo->isNullRes);
        }
      }
    }
5
54liuyao 已提交
3995

3996
    pBlock->info.rows += pRow->numOfRows;
3997
    releaseOutputBuf(pState, &key, pRow);
3998 3999 4000 4001
  }
  blockDataUpdateTsWindow(pBlock, 0);
  return TSDB_CODE_SUCCESS;
}
5
54liuyao 已提交
4002 4003 4004 4005 4006 4007 4008

int32_t saveSessionDiscBuf(SStreamState* pState, SSessionKey* key, void* buf, int32_t size) {
  streamStateSessionPut(pState, key, (const void*)buf, size);
  releaseOutputBuf(pState, NULL, (SResultRow*)buf);
  return TSDB_CODE_SUCCESS;
}

4009
int32_t buildSessionResultDataBlock(SOperatorInfo* pOperator, SStreamState* pState, SSDataBlock* pBlock,
5
54liuyao 已提交
4010
                                    SExprSupp* pSup, SGroupResInfo* pGroupResInfo) {
4011
  SExecTaskInfo*  pTaskInfo = pOperator->pTaskInfo;
5
54liuyao 已提交
4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024
  SExprInfo*      pExprInfo = pSup->pExprInfo;
  int32_t         numOfExprs = pSup->numOfExprs;
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
  SqlFunctionCtx* pCtx = pSup->pCtx;

  int32_t numOfRows = getNumOfTotalRes(pGroupResInfo);

  for (int32_t i = pGroupResInfo->index; i < numOfRows; i += 1) {
    SSessionKey* pKey = taosArrayGet(pGroupResInfo->pRows, i);
    int32_t      size = 0;
    void*        pVal = NULL;
    int32_t      code = streamStateSessionGet(pState, pKey, &pVal, &size);
    ASSERT(code == 0);
4025 4026 4027 4028
    if (code == -1) {
      // coverity scan
      continue;
    }
5
54liuyao 已提交
4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039
    SResultRow* pRow = (SResultRow*)pVal;
    doUpdateNumOfRows(pCtx, pRow, numOfExprs, rowEntryOffset);
    // no results, continue to check the next one
    if (pRow->numOfRows == 0) {
      pGroupResInfo->index += 1;
      releaseOutputBuf(pState, NULL, pRow);
      continue;
    }

    if (pBlock->info.groupId == 0) {
      pBlock->info.groupId = pKey->groupId;
4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064

      if (pOperator->operatorType == QUERY_NODE_PHYSICAL_PLAN_STREAM_STATE) {
        SStreamStateAggOperatorInfo* pInfo = pOperator->info;

        char* tbname = taosHashGet(pInfo->pGroupIdTbNameMap, &pBlock->info.groupId, sizeof(int64_t));
        if (tbname != NULL) {
          memcpy(pBlock->info.parTbName, tbname, TSDB_TABLE_NAME_LEN);
        } else {
          pBlock->info.parTbName[0] = 0;
        }
      } 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;

        char* tbname = taosHashGet(pInfo->pGroupIdTbNameMap, &pBlock->info.groupId, sizeof(int64_t));
        if (tbname != NULL) {
          memcpy(pBlock->info.parTbName, tbname, TSDB_TABLE_NAME_LEN);
        } else {
          pBlock->info.parTbName[0] = 0;
        }
      } else {
        ASSERT(0);
      }

5
54liuyao 已提交
4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109
    } else {
      // current value belongs to different group, it can't be packed into one datablock
      if (pBlock->info.groupId != pKey->groupId) {
        releaseOutputBuf(pState, NULL, pRow);
        break;
      }
    }

    if (pBlock->info.rows + pRow->numOfRows > pBlock->info.capacity) {
      ASSERT(pBlock->info.rows > 0);
      releaseOutputBuf(pState, NULL, pRow);
      break;
    }

    pGroupResInfo->index += 1;

    for (int32_t j = 0; j < numOfExprs; ++j) {
      int32_t slotId = pExprInfo[j].base.resSchema.slotId;

      pCtx[j].resultInfo = getResultEntryInfo(pRow, j, rowEntryOffset);
      if (pCtx[j].fpSet.finalize) {
        int32_t code1 = pCtx[j].fpSet.finalize(&pCtx[j], pBlock);
        if (TAOS_FAILED(code1)) {
          qError("%s build result data block error, code %s", GET_TASKID(pTaskInfo), tstrerror(code1));
          T_LONG_JMP(pTaskInfo->env, code1);
        }
      } else if (strcmp(pCtx[j].pExpr->pExpr->_function.functionName, "_select_value") == 0) {
        // do nothing, todo refactor
      } else {
        // expand the result into multiple rows. E.g., _wstart, top(k, 20)
        // the _wstart needs to copy to 20 following rows, since the results of top-k expands to 20 different rows.
        SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, slotId);
        char*            in = GET_ROWCELL_INTERBUF(pCtx[j].resultInfo);
        for (int32_t k = 0; k < pRow->numOfRows; ++k) {
          colDataAppend(pColInfoData, pBlock->info.rows + k, in, pCtx[j].resultInfo->isNullRes);
        }
      }
    }

    pBlock->info.rows += pRow->numOfRows;
    // saveSessionDiscBuf(pState, pKey, pVal, size);
    releaseOutputBuf(pState, NULL, pRow);
  }
  blockDataUpdateTsWindow(pBlock, 0);
  return TSDB_CODE_SUCCESS;
4110
}