executorimpl.c 177.0 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 84
int32_t getMaximumIdleDurationSec() { return tsShellActivityTimer * 2; }

static int32_t getExprFunctionId(SExprInfo* pExprInfo) {
85
  assert(pExprInfo != NULL && pExprInfo->pExpr != NULL && pExprInfo->pExpr->nodeType == TEXPR_UNARYEXPR_NODE);
86
  return 0;
87 88 89 90
}

static void doSetTagValueToResultBuf(char* output, const char* val, int16_t type, int16_t bytes);

91
static void setBlockStatisInfo(SqlFunctionCtx* pCtx, SExprInfo* pExpr, SSDataBlock* pSDataBlock);
92

X
Xiaoyu Wang 已提交
93
static void releaseQueryBuf(size_t numOfTables);
94 95 96 97 98

static void destroySFillOperatorInfo(void* param, int32_t numOfOutput);
static void destroyProjectOperatorInfo(void* param, int32_t numOfOutput);
static void destroyOrderOperatorInfo(void* param, int32_t numOfOutput);
static void destroyAggOperatorInfo(void* param, int32_t numOfOutput);
X
Xiaoyu Wang 已提交
99

H
Haojun Liao 已提交
100
static void destroyIntervalOperatorInfo(void* param, int32_t numOfOutput);
H
Haojun Liao 已提交
101 102
static void destroyExchangeOperatorInfo(void* param, int32_t numOfOutput);

103 104
static void destroyOperatorInfo(SOperatorInfo* pOperator);

105
void doSetOperatorCompleted(SOperatorInfo* pOperator) {
106
  pOperator->status = OP_EXEC_DONE;
107

108
  pOperator->cost.totalCost = (taosGetTimestampUs() - pOperator->pTaskInfo->cost.start * 1000) / 1000.0;
H
Haojun Liao 已提交
109
  if (pOperator->pTaskInfo != NULL) {
110
    setTaskStatus(pOperator->pTaskInfo, TASK_COMPLETED);
111 112
  }
}
113

H
Haojun Liao 已提交
114
int32_t operatorDummyOpenFn(SOperatorInfo* pOperator) {
115
  OPTR_SET_OPENED(pOperator);
116
  pOperator->cost.openCost = 0;
H
Haojun Liao 已提交
117
  return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
118 119
}

120
SOperatorFpSet createOperatorFpSet(__optr_open_fn_t openFn, __optr_fn_t nextFn, __optr_fn_t streamFn,
L
Liu Jicong 已提交
121
                                   __optr_fn_t cleanup, __optr_close_fn_t closeFn, __optr_encode_fn_t encode,
122
                                   __optr_decode_fn_t decode, __optr_explain_fn_t explain) {
123 124 125 126 127 128 129 130 131 132 133 134 135 136
  SOperatorFpSet fpSet = {
      ._openFn = openFn,
      .getNextFn = nextFn,
      .getStreamResFn = streamFn,
      .cleanupFn = cleanup,
      .closeFn = closeFn,
      .encodeResultRow = encode,
      .decodeResultRow = decode,
      .getExplainFn = explain,
  };

  return fpSet;
}

H
Haojun Liao 已提交
137
void operatorDummyCloseFn(void* param, int32_t numOfCols) {}
H
Haojun Liao 已提交
138

X
Xiaoyu Wang 已提交
139 140 141
static int32_t doCopyToSDataBlock(SExecTaskInfo* taskInfo, SSDataBlock* pBlock, SExprInfo* pExprInfo,
                                  SDiskbasedBuf* pBuf, SGroupResInfo* pGroupResInfo, const int32_t* rowCellOffset,
                                  SqlFunctionCtx* pCtx, int32_t numOfExprs);
H
Haojun Liao 已提交
142

143
static void initCtxOutputBuffer(SqlFunctionCtx* pCtx, int32_t size);
L
Liu Jicong 已提交
144 145
static void doSetTableGroupOutputBuf(SOperatorInfo* pOperator, SAggOperatorInfo* pAggInfo, int32_t numOfOutput,
                                     uint64_t groupId);
146

L
Liu Jicong 已提交
147 148
// setup the output buffer for each operator
static bool hasNull(SColumn* pColumn, SColumnDataAgg* pStatis) {
dengyihao's avatar
dengyihao 已提交
149 150
  if (TSDB_COL_IS_TAG(pColumn->flag) || TSDB_COL_IS_UD_COL(pColumn->flag) ||
      pColumn->colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
151 152 153 154 155 156 157 158 159 160
    return false;
  }

  if (pStatis != NULL && pStatis->numOfNull == 0) {
    return false;
  }

  return true;
}

161
#if 0
L
Liu Jicong 已提交
162 163
static bool chkResultRowFromKey(STaskRuntimeEnv* pRuntimeEnv, SResultRowInfo* pResultRowInfo, char* pData,
                                int16_t bytes, bool masterscan, uint64_t uid) {
164 165 166
  bool existed = false;
  SET_RES_WINDOW_KEY(pRuntimeEnv->keyBuf, pData, bytes, uid);

L
Liu Jicong 已提交
167 168
  SResultRow** p1 =
      (SResultRow**)taosHashGet(pRuntimeEnv->pResultRowHashTable, pRuntimeEnv->keyBuf, GET_RES_WINDOW_KEY_LEN(bytes));
169 170 171 172 173 174 175 176 177 178 179

  // 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 已提交
180
        //        existed = (pResultRowInfo->pResult[0] == (*p1));
181 182
      } else {  // check if current pResultRowInfo contains the existed pResultRow
        SET_RES_EXT_WINDOW_KEY(pRuntimeEnv->keyBuf, pData, bytes, uid, pResultRowInfo);
L
Liu Jicong 已提交
183 184
        int64_t* index =
            taosHashGet(pRuntimeEnv->pResultRowListSet, pRuntimeEnv->keyBuf, GET_RES_EXT_WINDOW_KEY_LEN(bytes));
185 186 187 188 189 190 191 192 193 194 195 196 197
        if (index != NULL) {
          existed = true;
        } else {
          existed = false;
        }
      }
    }

    return existed;
  }

  return p1 != NULL;
}
198
#endif
199

200
SResultRow* getNewResultRow(SDiskbasedBuf* pResultBuf, int64_t tableGroupId, int32_t interBufSize) {
L
Liu Jicong 已提交
201
  SFilePage* pData = NULL;
202 203 204 205 206 207 208 209 210 211 212 213 214

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

  if (taosArrayGetSize(list) == 0) {
    pData = getNewBufPage(pResultBuf, tableGroupId, &pageId);
    pData->num = sizeof(SFilePage);
  } else {
    SPageInfo* pi = getLastPageInfo(list);
    pData = getBufPage(pResultBuf, getPageId(pi));
    pageId = getPageId(pi);

wmmhello's avatar
wmmhello 已提交
215
    if (pData->num + interBufSize > getBufPageSize(pResultBuf)) {
216 217 218 219 220 221 222 223 224 225 226 227 228 229
      // release current page first, and prepare the next one
      releaseBufPageInfo(pResultBuf, pi);

      pData = getNewBufPage(pResultBuf, tableGroupId, &pageId);
      if (pData != NULL) {
        pData->num = sizeof(SFilePage);
      }
    }
  }

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

230 231
  setBufPageDirty(pData, true);

232 233 234 235 236
  // 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;

wmmhello's avatar
wmmhello 已提交
237
  pData->num += interBufSize;
238 239 240 241

  return pResultRow;
}

242 243 244 245 246 247 248
/**
 * the struct of key in hash table
 * +----------+---------------+
 * | group id |   key data    |
 * | 8 bytes  | actual length |
 * +----------+---------------+
 */
249 250 251
SResultRow* doSetResultOutBufByKey(SDiskbasedBuf* pResultBuf, SResultRowInfo* pResultRowInfo, char* pData,
                                   int16_t bytes, bool masterscan, uint64_t groupId, SExecTaskInfo* pTaskInfo,
                                   bool isIntervalQuery, SAggSupporter* pSup) {
252
  SET_RES_WINDOW_KEY(pSup->keyBuf, pData, bytes, groupId);
H
Haojun Liao 已提交
253

dengyihao's avatar
dengyihao 已提交
254 255
  SResultRowPosition* p1 =
      (SResultRowPosition*)taosHashGet(pSup->pResultRowHashTable, pSup->keyBuf, GET_RES_WINDOW_KEY_LEN(bytes));
H
Haojun Liao 已提交
256

257 258
  SResultRow* pResult = NULL;

H
Haojun Liao 已提交
259 260
  // in case of repeat scan/reverse scan, no new time window added.
  if (isIntervalQuery) {
261 262
    if (masterscan && p1 != NULL) {  // the *p1 may be NULL in case of sliding+offset exists.
      pResult = getResultRowByPos(pResultBuf, p1);
263
      ASSERT(pResult->pageId == p1->pageId && pResult->offset == p1->offset);
H
Haojun Liao 已提交
264 265
    }
  } else {
dengyihao's avatar
dengyihao 已提交
266 267
    // In case of group by column query, the required SResultRow object must be existInCurrentResusltRowInfo in the
    // pResultRowInfo object.
H
Haojun Liao 已提交
268
    if (p1 != NULL) {
269
      // todo
270
      pResult = getResultRowByPos(pResultBuf, p1);
271
      ASSERT(pResult->pageId == p1->pageId && pResult->offset == p1->offset);
H
Haojun Liao 已提交
272 273 274
    }
  }

L
Liu Jicong 已提交
275
  // 1. close current opened time window
276
  if (pResultRowInfo->cur.pageId != -1 && ((pResult == NULL) || (pResult->pageId != pResultRowInfo->cur.pageId))) {
277
#ifdef BUF_PAGE_DEBUG
wmmhello's avatar
wmmhello 已提交
278
    qDebug("page_1");
279
#endif
280
    SResultRowPosition pos = pResultRowInfo->cur;
X
Xiaoyu Wang 已提交
281
    SFilePage*         pPage = getBufPage(pResultBuf, pos.pageId);
282 283 284 285 286
    releaseBufPage(pResultBuf, pPage);
  }

  // allocate a new buffer page
  if (pResult == NULL) {
287
#ifdef BUF_PAGE_DEBUG
wmmhello's avatar
wmmhello 已提交
288
    qDebug("page_2");
289
#endif
H
Haojun Liao 已提交
290
    ASSERT(pSup->resultRowSize > 0);
291 292
    pResult = getNewResultRow(pResultBuf, groupId, pSup->resultRowSize);

293
    initResultRow(pResult);
H
Haojun Liao 已提交
294

295 296
    // add a new result set for a new group
    SResultRowPosition pos = {.pageId = pResult->pageId, .offset = pResult->offset};
X
Xiaoyu Wang 已提交
297 298
    taosHashPut(pSup->pResultRowHashTable, pSup->keyBuf, GET_RES_WINDOW_KEY_LEN(bytes), &pos,
                sizeof(SResultRowPosition));
H
Haojun Liao 已提交
299 300
  }

301 302 303
  // 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 已提交
304
  // too many time window in query
305
  if (taosHashGetSize(pSup->pResultRowHashTable) > MAX_INTERVAL_TIME_WINDOW) {
H
Haojun Liao 已提交
306 307 308
    longjmp(pTaskInfo->env, TSDB_CODE_QRY_TOO_MANY_TIMEWINDOW);
  }

H
Haojun Liao 已提交
309
  return pResult;
H
Haojun Liao 已提交
310 311
}

312
// a new buffer page for each table. Needs to opt this design
L
Liu Jicong 已提交
313
static int32_t addNewWindowResultBuf(SResultRow* pWindowRes, SDiskbasedBuf* pResultBuf, int32_t tid, uint32_t size) {
314 315 316 317
  if (pWindowRes->pageId != -1) {
    return 0;
  }

L
Liu Jicong 已提交
318
  SFilePage* pData = NULL;
319 320 321 322 323 324

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

  if (taosArrayGetSize(list) == 0) {
H
Haojun Liao 已提交
325
    pData = getNewBufPage(pResultBuf, tid, &pageId);
326
    pData->num = sizeof(SFilePage);
327 328
  } else {
    SPageInfo* pi = getLastPageInfo(list);
329
    pData = getBufPage(pResultBuf, getPageId(pi));
330
    pageId = getPageId(pi);
331

332
    if (pData->num + size > getBufPageSize(pResultBuf)) {
333
      // release current page first, and prepare the next one
334
      releaseBufPageInfo(pResultBuf, pi);
335

H
Haojun Liao 已提交
336
      pData = getNewBufPage(pResultBuf, tid, &pageId);
337
      if (pData != NULL) {
338
        pData->num = sizeof(SFilePage);
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
      }
    }
  }

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

359
//  query_range_start, query_range_end, window_duration, window_start, window_end
360
void initExecTimeWindowInfo(SColumnInfoData* pColData, STimeWindow* pQueryWindow) {
361 362 363
  pColData->info.type = TSDB_DATA_TYPE_TIMESTAMP;
  pColData->info.bytes = sizeof(int64_t);

364
  colInfoDataEnsureCapacity(pColData, 5);
365 366 367 368 369 370 371 372 373
  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);
}

X
Xiaoyu Wang 已提交
374 375 376
void doApplyFunctions(SExecTaskInfo* taskInfo, SqlFunctionCtx* pCtx, STimeWindow* pWin,
                      SColumnInfoData* pTimeWindowData, int32_t offset, int32_t forwardStep, TSKEY* tsCol,
                      int32_t numOfTotal, int32_t numOfOutput, int32_t order) {
377
  for (int32_t k = 0; k < numOfOutput; ++k) {
H
Haojun Liao 已提交
378
    // keep it temporarily
379
    // todo no need this??
dengyihao's avatar
dengyihao 已提交
380 381
    bool    hasAgg = pCtx[k].input.colDataAggIsSet;
    int32_t numOfRows = pCtx[k].input.numOfRows;
H
Haojun Liao 已提交
382
    int32_t startOffset = pCtx[k].input.startRowIndex;
383

384
    pCtx[k].input.startRowIndex = offset;
385
    pCtx[k].input.numOfRows = forwardStep;
386 387 388

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

393 394
    if (fmIsWindowPseudoColumnFunc(pCtx[k].functionId)) {
      SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(&pCtx[k]);
395 396

      char* p = GET_ROWCELL_INTERBUF(pEntryInfo);
397

398
      SColumnInfoData idata = {0};
dengyihao's avatar
dengyihao 已提交
399
      idata.info.type = TSDB_DATA_TYPE_BIGINT;
400
      idata.info.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
dengyihao's avatar
dengyihao 已提交
401
      idata.pData = p;
402 403 404 405

      SScalarParam out = {.columnData = &idata};
      SScalarParam tw = {.numOfRows = 5, .columnData = pTimeWindowData};
      pCtx[k].sfp.process(&tw, 1, &out);
406
      pEntryInfo->numOfRes = 1;
407 408 409 410 411 412 413 414 415 416
    } 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;
          longjmp(taskInfo->env, code);
        }
417
      }
418

419 420 421 422 423
      // restore it
      pCtx[k].input.colDataAggIsSet = hasAgg;
      pCtx[k].input.startRowIndex = startOffset;
      pCtx[k].input.numOfRows = numOfRows;
    }
424 425 426
  }
}

dengyihao's avatar
dengyihao 已提交
427
static int32_t doSetInputDataBlock(SOperatorInfo* pOperator, SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t order,
428
                                   int32_t scanFlag, bool createDummyCol);
429

dengyihao's avatar
dengyihao 已提交
430 431
static void doSetInputDataBlockInfo(SOperatorInfo* pOperator, SqlFunctionCtx* pCtx, SSDataBlock* pBlock,
                                    int32_t order) {
432
  for (int32_t i = 0; i < pOperator->exprSupp.numOfExprs; ++i) {
433
    pCtx[i].order = order;
434
    pCtx[i].input.numOfRows = pBlock->info.rows;
435
    setBlockStatisInfo(&pCtx[i], &pOperator->exprSupp.pExprInfo[i], pBlock);
436 437 438
  }
}

X
Xiaoyu Wang 已提交
439 440
void setInputDataBlock(SOperatorInfo* pOperator, SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t order,
                       int32_t scanFlag, bool createDummyCol) {
441
  if (pBlock->pBlockAgg != NULL) {
H
Haojun Liao 已提交
442
    doSetInputDataBlockInfo(pOperator, pCtx, pBlock, order);
443
  } else {
444
    doSetInputDataBlock(pOperator, pCtx, pBlock, order, scanFlag, createDummyCol);
H
Haojun Liao 已提交
445
  }
446 447
}

L
Liu Jicong 已提交
448 449
static int32_t doCreateConstantValColumnInfo(SInputColumnInfoData* pInput, SFunctParam* pFuncParam, int32_t paramIndex,
                                             int32_t numOfRows) {
450 451 452 453 454 455 456 457
  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)
458 459
    pColInfo->info.type = pFuncParam->param.nType;
    pColInfo->info.bytes = pFuncParam->param.nLen;
460 461

    pInput->pData[paramIndex] = pColInfo;
462 463
  } else {
    pColInfo = pInput->pData[paramIndex];
464 465
  }

466
  colInfoDataEnsureCapacity(pColInfo, numOfRows);
467

468
  int8_t type = pFuncParam->param.nType;
469 470
  if (type == TSDB_DATA_TYPE_BIGINT || type == TSDB_DATA_TYPE_UBIGINT) {
    int64_t v = pFuncParam->param.i;
dengyihao's avatar
dengyihao 已提交
471
    for (int32_t i = 0; i < numOfRows; ++i) {
472 473 474 475
      colDataAppendInt64(pColInfo, i, &v);
    }
  } else if (type == TSDB_DATA_TYPE_DOUBLE) {
    double v = pFuncParam->param.d;
dengyihao's avatar
dengyihao 已提交
476
    for (int32_t i = 0; i < numOfRows; ++i) {
477 478
      colDataAppendDouble(pColInfo, i, &v);
    }
479
  } else if (type == TSDB_DATA_TYPE_VARCHAR) {
L
Liu Jicong 已提交
480
    char* tmp = taosMemoryMalloc(pFuncParam->param.nLen + VARSTR_HEADER_SIZE);
481
    STR_WITH_SIZE_TO_VARSTR(tmp, pFuncParam->param.pz, pFuncParam->param.nLen);
L
Liu Jicong 已提交
482
    for (int32_t i = 0; i < numOfRows; ++i) {
483 484
      colDataAppend(pColInfo, i, tmp, false);
    }
485 486 487 488 489
  }

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
490
static int32_t doSetInputDataBlock(SOperatorInfo* pOperator, SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t order,
X
Xiaoyu Wang 已提交
491
                                   int32_t scanFlag, bool createDummyCol) {
492 493
  int32_t code = TSDB_CODE_SUCCESS;

494
  for (int32_t i = 0; i < pOperator->exprSupp.numOfExprs; ++i) {
L
Liu Jicong 已提交
495
    pCtx[i].order = order;
496 497
    pCtx[i].input.numOfRows = pBlock->info.rows;

L
Liu Jicong 已提交
498
    pCtx[i].pSrcBlock = pBlock;
X
Xiaoyu Wang 已提交
499
    pCtx[i].scanFlag = scanFlag;
H
Haojun Liao 已提交
500

501
    SInputColumnInfoData* pInput = &pCtx[i].input;
502
    pInput->uid = pBlock->info.uid;
C
Cary Xu 已提交
503
    pInput->colDataAggIsSet = false;
504

505
    SExprInfo* pOneExpr = &pOperator->exprSupp.pExprInfo[i];
506
    for (int32_t j = 0; j < pOneExpr->base.numOfParams; ++j) {
dengyihao's avatar
dengyihao 已提交
507
      SFunctParam* pFuncParam = &pOneExpr->base.pParam[j];
G
Ganlin Zhao 已提交
508 509
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
        int32_t slotId = pFuncParam->pCol->slotId;
dengyihao's avatar
dengyihao 已提交
510
        pInput->pData[j] = taosArrayGet(pBlock->pDataBlock, slotId);
511 512 513
        pInput->totalRows = pBlock->info.rows;
        pInput->numOfRows = pBlock->info.rows;
        pInput->startRowIndex = 0;
514

515
        // NOTE: the last parameter is the primary timestamp column
516 517 518
        if (fmIsTimelineFunc(pCtx[i].functionId) && (j == pOneExpr->base.numOfParams - 1)) {
          pInput->pPTS = pInput->pData[j];
        }
519 520
        ASSERT(pInput->pData[j] != NULL);
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
521 522 523
        // 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) {
524 525 526 527
          pInput->totalRows = pBlock->info.rows;
          pInput->numOfRows = pBlock->info.rows;
          pInput->startRowIndex = 0;

528
          code = doCreateConstantValColumnInfo(pInput, pFuncParam, j, pBlock->info.rows);
529 530 531
          if (code != TSDB_CODE_SUCCESS) {
            return code;
          }
532
        }
G
Ganlin Zhao 已提交
533 534
      }
    }
H
Haojun Liao 已提交
535
  }
536 537

  return code;
H
Haojun Liao 已提交
538 539
}

540
static int32_t doAggregateImpl(SOperatorInfo* pOperator, TSKEY startTs, SqlFunctionCtx* pCtx) {
541
  for (int32_t k = 0; k < pOperator->exprSupp.numOfExprs; ++k) {
H
Haojun Liao 已提交
542
    if (functionNeedToExecute(&pCtx[k])) {
543
      // todo add a dummy funtion to avoid process check
544 545 546
      if (pCtx[k].fpSet.process == NULL) {
        continue;
      }
547
#ifdef BUF_PAGE_DEBUG
wmmhello's avatar
wmmhello 已提交
548
      qDebug("page_process");
549
#endif
550 551 552 553
      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;
554
      }
555 556
    }
  }
557 558

  return TSDB_CODE_SUCCESS;
559 560
}

H
Haojun Liao 已提交
561
static void setPseudoOutputColInfo(SSDataBlock* pResult, SqlFunctionCtx* pCtx, SArray* pPseudoList) {
dengyihao's avatar
dengyihao 已提交
562
  size_t num = (pPseudoList != NULL) ? taosArrayGetSize(pPseudoList) : 0;
H
Haojun Liao 已提交
563 564 565 566 567
  for (int32_t i = 0; i < num; ++i) {
    pCtx[i].pOutput = taosArrayGet(pResult->pDataBlock, i);
  }
}

568
int32_t projectApplyFunctions(SExprInfo* pExpr, SSDataBlock* pResult, SSDataBlock* pSrcBlock, SqlFunctionCtx* pCtx,
X
Xiaoyu Wang 已提交
569
                              int32_t numOfOutput, SArray* pPseudoList) {
H
Haojun Liao 已提交
570
  setPseudoOutputColInfo(pResult, pCtx, pPseudoList);
H
Haojun Liao 已提交
571
  pResult->info.groupId = pSrcBlock->info.groupId;
H
Haojun Liao 已提交
572

dengyihao's avatar
dengyihao 已提交
573 574
  // if the source equals to the destination, it is to create a new column as the result of scalar function or some
  // operators.
575 576
  bool createNewColModel = (pResult == pSrcBlock);

577 578
  int32_t numOfRows = 0;

579
  for (int32_t k = 0; k < numOfOutput; ++k) {
dengyihao's avatar
dengyihao 已提交
580
    int32_t         outputSlotId = pExpr[k].base.resSchema.slotId;
581 582
    SqlFunctionCtx* pfCtx = &pCtx[k];

L
Liu Jicong 已提交
583
    if (pExpr[k].pExpr->nodeType == QUERY_NODE_COLUMN) {  // it is a project query
584
      SColumnInfoData* pColInfoData = taosArrayGet(pResult->pDataBlock, outputSlotId);
585
      if (pResult->info.rows > 0 && !createNewColModel) {
X
Xiaoyu Wang 已提交
586 587
        colDataMergeCol(pColInfoData, pResult->info.rows, &pResult->info.capacity, pfCtx->input.pData[0],
                        pfCtx->input.numOfRows);
588
      } else {
589
        colDataAssign(pColInfoData, pfCtx->input.pData[0], pfCtx->input.numOfRows, &pResult->info);
590
      }
591

592
      numOfRows = pfCtx->input.numOfRows;
593
    } else if (pExpr[k].pExpr->nodeType == QUERY_NODE_VALUE) {
594
      SColumnInfoData* pColInfoData = taosArrayGet(pResult->pDataBlock, outputSlotId);
595

dengyihao's avatar
dengyihao 已提交
596
      int32_t offset = createNewColModel ? 0 : pResult->info.rows;
597
      for (int32_t i = 0; i < pSrcBlock->info.rows; ++i) {
dengyihao's avatar
dengyihao 已提交
598 599 600
        colDataAppend(pColInfoData, i + offset,
                      taosVariantGet(&pExpr[k].base.pParam[0].param, pExpr[k].base.pParam[0].param.nType),
                      TSDB_DATA_TYPE_NULL == pExpr[k].base.pParam[0].param.nType);
601
      }
602 603

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

608
      SColumnInfoData* pResColData = taosArrayGet(pResult->pDataBlock, outputSlotId);
609
      SColumnInfoData  idata = {.info = pResColData->info, .hasNull = true};
610

611
      SScalarParam dest = {.columnData = &idata};
X
Xiaoyu Wang 已提交
612
      int32_t      code = scalarCalculate(pExpr[k].pExpr->_optrRoot.pRootNode, pBlockList, &dest);
613 614 615 616
      if (code != TSDB_CODE_SUCCESS) {
        taosArrayDestroy(pBlockList);
        return code;
      }
617

dengyihao's avatar
dengyihao 已提交
618
      int32_t startOffset = createNewColModel ? 0 : pResult->info.rows;
619
      ASSERT(pResult->info.capacity > 0);
620
      colDataMergeCol(pResColData, startOffset, &pResult->info.capacity, &idata, dest.numOfRows);
621 622

      numOfRows = dest.numOfRows;
623 624
      taosArrayDestroy(pBlockList);
    } else if (pExpr[k].pExpr->nodeType == QUERY_NODE_FUNCTION) {
625
      ASSERT(!fmIsAggFunc(pfCtx->functionId));
626

627 628
      // _rowts/_c0, not tbname column
      if (fmIsPseudoColumnFunc(pfCtx->functionId) && (!fmIsScanPseudoColumnFunc(pfCtx->functionId))) {
H
Haojun Liao 已提交
629
        // do nothing
630 631 632 633 634 635 636 637 638 639 640 641 642 643
      } else if (fmIsIndefiniteRowsFunc(pfCtx->functionId)) {
        SResultRowEntryInfo* pResInfo = GET_RES_INFO(&pCtx[k]);
        pfCtx->fpSet.init(&pCtx[k], pResInfo);

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

        numOfRows = pfCtx->fpSet.process(pfCtx);
H
Haojun Liao 已提交
644 645 646
      } else {
        SArray* pBlockList = taosArrayInit(4, POINTER_BYTES);
        taosArrayPush(pBlockList, &pSrcBlock);
G
Ganlin Zhao 已提交
647

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

651
        SScalarParam dest = {.columnData = &idata};
X
Xiaoyu Wang 已提交
652
        int32_t      code = scalarCalculate((SNode*)pExpr[k].pExpr->_function.pFunctNode, pBlockList, &dest);
653 654 655 656
        if (code != TSDB_CODE_SUCCESS) {
          taosArrayDestroy(pBlockList);
          return code;
        }
657

dengyihao's avatar
dengyihao 已提交
658
        int32_t startOffset = createNewColModel ? 0 : pResult->info.rows;
659
        ASSERT(pResult->info.capacity > 0);
660
        colDataMergeCol(pResColData, startOffset, &pResult->info.capacity, &idata, dest.numOfRows);
661 662

        numOfRows = dest.numOfRows;
H
Haojun Liao 已提交
663 664
        taosArrayDestroy(pBlockList);
      }
665
    } else {
666
      ASSERT(0);
667 668
    }
  }
669

670 671 672
  if (!createNewColModel) {
    pResult->info.rows += numOfRows;
  }
673 674

  return TSDB_CODE_SUCCESS;
675 676
}

677 678 679
static void setResultRowKey(SResultRow* pResultRow, char* pData, int16_t type) {
  if (IS_VAR_DATA_TYPE(type)) {
    // todo disable this
680

681 682 683 684 685 686 687 688 689
    //    if (pResultRow->key == NULL) {
    //      pResultRow->key = taosMemoryMalloc(varDataTLen(pData));
    //      varDataCopy(pResultRow->key, pData);
    //    } else {
    //      ASSERT(memcmp(pResultRow->key, pData, varDataTLen(pData)) == 0);
    //    }
  } else {
    int64_t v = -1;
    GET_TYPED_DATA(v, int64_t, type, pData);
690

691 692
    pResultRow->win.skey = v;
    pResultRow->win.ekey = v;
693 694 695
  }
}

5
54liuyao 已提交
696
bool functionNeedToExecute(SqlFunctionCtx* pCtx) {
697
  struct SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
698

699 700 701 702 703
  // in case of timestamp column, always generated results.
  int32_t functionId = pCtx->functionId;
  if (functionId == -1) {
    return false;
  }
704

705 706
  if (pCtx->scanFlag == REPEAT_SCAN) {
    return fmIsRepeatScanFunc(pCtx->functionId);
707 708
  }

709 710
  if (isRowEntryCompleted(pResInfo)) {
    return false;
711 712
  }

713 714 715
  return true;
}

716 717 718 719 720 721 722
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;
    }
723

724 725 726
    // Set the correct column info (data type and bytes)
    pInput->pData[paramIndex]->info.type = type;
    pInput->pData[paramIndex]->info.bytes = tDataTypes[type].bytes;
727
  }
H
Haojun Liao 已提交
728

729 730 731 732 733 734
  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;
735 736
    }
  } else {
737
    da = pInput->pColumnDataAgg[paramIndex];
738 739
  }

740
  ASSERT(!IS_VAR_DATA_TYPE(type));
741

742 743 744 745 746 747
  if (type == TSDB_DATA_TYPE_BIGINT) {
    int64_t v = pFuncParam->param.i;
    *da = (SColumnDataAgg){.numOfNull = 0, .min = v, .max = v, .maxIndex = 0, .minIndex = 0, .sum = v * numOfRows};
  } else if (type == TSDB_DATA_TYPE_DOUBLE) {
    double v = pFuncParam->param.d;
    *da = (SColumnDataAgg){.numOfNull = 0, .maxIndex = 0, .minIndex = 0};
748

749 750 751 752 753 754 755 756 757 758 759 760
    *(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;

    *da = (SColumnDataAgg){.numOfNull = 0, .maxIndex = 0, .minIndex = 0};
    *(bool*)&da->min = 0;
    *(bool*)&da->max = v;
    *(bool*)&da->sum = v * numOfRows;
  } else if (type == TSDB_DATA_TYPE_TIMESTAMP) {
    // do nothing
761
  } else {
762
    ASSERT(0);
763 764
  }

765 766
  return TSDB_CODE_SUCCESS;
}
767 768 769 770 771 772 773 774 775 776 777

void setBlockStatisInfo(SqlFunctionCtx* pCtx, SExprInfo* pExprInfo, SSDataBlock* pBlock) {
  int32_t numOfRows = pBlock->info.rows;

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

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

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

781 782
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
        int32_t slotId = pFuncParam->pCol->slotId;
783 784 785 786
        pInput->pColumnDataAgg[j] = pBlock->pBlockAgg[slotId];
        if (pInput->pColumnDataAgg[j] == NULL) {
          pInput->colDataAggIsSet = false;
        }
787 788 789 790

        // 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);
791 792
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
        doCreateConstantValColumnAggInfo(pInput, pFuncParam, pFuncParam->param.nType, j, pBlock->info.rows);
793 794
      }
    }
795
  } else {
796
    pInput->colDataAggIsSet = false;
797 798 799
  }

  // set the statistics data for primary time stamp column
800 801 802 803 804
  //  if (pCtx->functionId == FUNCTION_SPREAD && pColumn->colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
  //    pCtx->isAggSet = true;
  //    pCtx->agg.min = pBlock->info.window.skey;
  //    pCtx->agg.max = pBlock->info.window.ekey;
  //  }
805 806
}

L
Liu Jicong 已提交
807
bool isTaskKilled(SExecTaskInfo* pTaskInfo) {
808 809
  // query has been executed more than tsShellActivityTimer, and the retrieve has not arrived
  // abort current query execution.
L
Liu Jicong 已提交
810 811
  if (pTaskInfo->owner != 0 &&
      ((taosGetTimestampSec() - pTaskInfo->cost.start / 1000) > 10 * getMaximumIdleDurationSec())
812 813
      /*(!needBuildResAfterQueryComplete(pTaskInfo))*/) {
    assert(pTaskInfo->cost.start != 0);
L
Liu Jicong 已提交
814 815 816
    //    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;
817 818 819 820 821
  }

  return false;
}

L
Liu Jicong 已提交
822
void setTaskKilled(SExecTaskInfo* pTaskInfo) { pTaskInfo->code = TSDB_CODE_TSC_QUERY_CANCELLED; }
823 824

/////////////////////////////////////////////////////////////////////////////////////////////
L
Liu Jicong 已提交
825
// todo refactor : return window
826
void getAlignQueryTimeWindow(SInterval* pInterval, int32_t precision, int64_t key, STimeWindow* win) {
H
Haojun Liao 已提交
827
  win->skey = taosTimeTruncate(key, pInterval, precision);
828 829

  /*
H
Haojun Liao 已提交
830
   * if the realSkey > INT64_MAX - pInterval->interval, the query duration between
831 832
   * realSkey and realEkey must be less than one interval.Therefore, no need to adjust the query ranges.
   */
833 834
  win->ekey = taosTimeAdd(win->skey, pInterval->interval, pInterval->intervalUnit, precision) - 1;
  if (win->ekey < win->skey) {
835 836 837 838
    win->ekey = INT64_MAX;
  }
}

839
#if 0
L
Liu Jicong 已提交
840
static int32_t updateBlockLoadStatus(STaskAttr* pQuery, int32_t status) {
841

842 843 844
  bool hasFirstLastFunc = false;
  bool hasOtherFunc = false;

845
  if (status == BLK_DATA_DATA_LOAD || status == BLK_DATA_FILTEROUT) {
846 847 848 849 850
    return status;
  }

  for (int32_t i = 0; i < pQuery->numOfOutput; ++i) {
    int32_t functionId = getExprFunctionId(&pQuery->pExpr1[i]);
851

852 853 854 855 856 857 858 859 860 861
    if (functionId == FUNCTION_TS || functionId == FUNCTION_TS_DUMMY || functionId == FUNCTION_TAG ||
        functionId == FUNCTION_TAG_DUMMY) {
      continue;
    }

    if (functionId == FUNCTION_FIRST_DST || functionId == FUNCTION_LAST_DST) {
      hasFirstLastFunc = true;
    } else {
      hasOtherFunc = true;
    }
862

863 864
  }

865
  if (hasFirstLastFunc && status == BLK_DATA_NOT_LOAD) {
L
Liu Jicong 已提交
866
    if (!hasOtherFunc) {
867
      return BLK_DATA_FILTEROUT;
868
    } else {
869
      return BLK_DATA_DATA_LOAD;
870 871 872 873 874 875
    }
  }

  return status;
}

876 877
#endif

L
Liu Jicong 已提交
878 879
// static void updateDataCheckOrder(SQInfo *pQInfo, SQueryTableReq* pQueryMsg, bool stableQuery) {
//   STaskAttr* pQueryAttr = pQInfo->runtimeEnv.pQueryAttr;
H
Haojun Liao 已提交
880
//
L
Liu Jicong 已提交
881 882 883 884
//   // in case of point-interpolation query, use asc order scan
//   char msg[] = "QInfo:0x%"PRIx64" scan order changed for %s query, old:%d, new:%d, qrange exchanged, old qrange:%"
//   PRId64
//                "-%" PRId64 ", new qrange:%" PRId64 "-%" PRId64;
H
Haojun Liao 已提交
885
//
L
Liu Jicong 已提交
886 887 888 889 890
//   // todo handle the case the the order irrelevant query type mixed up with order critical query type
//   // descending order query for last_row query
//   if (isFirstLastRowQuery(pQueryAttr)) {
//     //qDebug("QInfo:0x%"PRIx64" scan order changed for last_row query, old:%d, new:%d", pQInfo->qId,
//     pQueryAttr->order.order, TSDB_ORDER_ASC);
H
Haojun Liao 已提交
891
//
L
Liu Jicong 已提交
892 893
//     pQueryAttr->order.order = TSDB_ORDER_ASC;
//     if (pQueryAttr->window.skey > pQueryAttr->window.ekey) {
wafwerar's avatar
wafwerar 已提交
894
//       TSWAP(pQueryAttr->window.skey, pQueryAttr->window.ekey);
L
Liu Jicong 已提交
895
//     }
H
Haojun Liao 已提交
896
//
L
Liu Jicong 已提交
897 898 899
//     pQueryAttr->needReverseScan = false;
//     return;
//   }
H
Haojun Liao 已提交
900
//
L
Liu Jicong 已提交
901 902 903
//   if (pQueryAttr->groupbyColumn && pQueryAttr->order.order == TSDB_ORDER_DESC) {
//     pQueryAttr->order.order = TSDB_ORDER_ASC;
//     if (pQueryAttr->window.skey > pQueryAttr->window.ekey) {
wafwerar's avatar
wafwerar 已提交
904
//       TSWAP(pQueryAttr->window.skey, pQueryAttr->window.ekey);
L
Liu Jicong 已提交
905
//     }
H
Haojun Liao 已提交
906
//
L
Liu Jicong 已提交
907 908 909 910
//     pQueryAttr->needReverseScan = false;
//     doUpdateLastKey(pQueryAttr);
//     return;
//   }
H
Haojun Liao 已提交
911
//
L
Liu Jicong 已提交
912 913 914 915 916 917
//   if (pQueryAttr->pointInterpQuery && pQueryAttr->interval.interval == 0) {
//     if (!QUERY_IS_ASC_QUERY(pQueryAttr)) {
//       //qDebug(msg, pQInfo->qId, "interp", pQueryAttr->order.order, TSDB_ORDER_ASC, pQueryAttr->window.skey,
//       pQueryAttr->window.ekey, pQueryAttr->window.ekey, pQueryAttr->window.skey); TSWAP(pQueryAttr->window.skey,
//       pQueryAttr->window.ekey, TSKEY);
//     }
H
Haojun Liao 已提交
918
//
L
Liu Jicong 已提交
919 920 921
//     pQueryAttr->order.order = TSDB_ORDER_ASC;
//     return;
//   }
H
Haojun Liao 已提交
922
//
L
Liu Jicong 已提交
923 924 925 926
//   if (pQueryAttr->interval.interval == 0) {
//     if (onlyFirstQuery(pQueryAttr)) {
//       if (!QUERY_IS_ASC_QUERY(pQueryAttr)) {
//         //qDebug(msg, pQInfo->qId, "only-first", pQueryAttr->order.order, TSDB_ORDER_ASC, pQueryAttr->window.skey,
H
Haojun Liao 已提交
927 928
////               pQueryAttr->window.ekey, pQueryAttr->window.ekey, pQueryAttr->window.skey);
//
wafwerar's avatar
wafwerar 已提交
929
//        TSWAP(pQueryAttr->window.skey, pQueryAttr->window.ekey);
H
Haojun Liao 已提交
930 931 932 933 934 935 936 937 938 939
//        doUpdateLastKey(pQueryAttr);
//      }
//
//      pQueryAttr->order.order = TSDB_ORDER_ASC;
//      pQueryAttr->needReverseScan = false;
//    } else if (onlyLastQuery(pQueryAttr) && notContainSessionOrStateWindow(pQueryAttr)) {
//      if (QUERY_IS_ASC_QUERY(pQueryAttr)) {
//        //qDebug(msg, pQInfo->qId, "only-last", pQueryAttr->order.order, TSDB_ORDER_DESC, pQueryAttr->window.skey,
////               pQueryAttr->window.ekey, pQueryAttr->window.ekey, pQueryAttr->window.skey);
//
wafwerar's avatar
wafwerar 已提交
940
//        TSWAP(pQueryAttr->window.skey, pQueryAttr->window.ekey);
H
Haojun Liao 已提交
941 942 943 944 945 946 947 948 949 950 951 952
//        doUpdateLastKey(pQueryAttr);
//      }
//
//      pQueryAttr->order.order = TSDB_ORDER_DESC;
//      pQueryAttr->needReverseScan = false;
//    }
//
//  } else {  // interval query
//    if (stableQuery) {
//      if (onlyFirstQuery(pQueryAttr)) {
//        if (!QUERY_IS_ASC_QUERY(pQueryAttr)) {
//          //qDebug(msg, pQInfo->qId, "only-first stable", pQueryAttr->order.order, TSDB_ORDER_ASC,
L
Liu Jicong 已提交
953 954
////                 pQueryAttr->window.skey, pQueryAttr->window.ekey, pQueryAttr->window.ekey,
/// pQueryAttr->window.skey);
H
Haojun Liao 已提交
955
//
wafwerar's avatar
wafwerar 已提交
956
//          TSWAP(pQueryAttr->window.skey, pQueryAttr->window.ekey);
H
Haojun Liao 已提交
957 958 959 960 961 962 963 964
//          doUpdateLastKey(pQueryAttr);
//        }
//
//        pQueryAttr->order.order = TSDB_ORDER_ASC;
//        pQueryAttr->needReverseScan = false;
//      } else if (onlyLastQuery(pQueryAttr)) {
//        if (QUERY_IS_ASC_QUERY(pQueryAttr)) {
//          //qDebug(msg, pQInfo->qId, "only-last stable", pQueryAttr->order.order, TSDB_ORDER_DESC,
L
Liu Jicong 已提交
965 966
////                 pQueryAttr->window.skey, pQueryAttr->window.ekey, pQueryAttr->window.ekey,
/// pQueryAttr->window.skey);
H
Haojun Liao 已提交
967
//
wafwerar's avatar
wafwerar 已提交
968
//          TSWAP(pQueryAttr->window.skey, pQueryAttr->window.ekey);
H
Haojun Liao 已提交
969 970 971 972 973 974 975 976 977
//          doUpdateLastKey(pQueryAttr);
//        }
//
//        pQueryAttr->order.order = TSDB_ORDER_DESC;
//        pQueryAttr->needReverseScan = false;
//      }
//    }
//  }
//}
978

L
Liu Jicong 已提交
979 980 981
// static FORCE_INLINE bool doFilterByBlockStatistics(STaskRuntimeEnv* pRuntimeEnv, SDataStatis *pDataStatis,
// SqlFunctionCtx *pCtx, int32_t numOfRows) {
//   STaskAttr* pQueryAttr = pRuntimeEnv->pQueryAttr;
982
//
L
Liu Jicong 已提交
983 984 985
//   if (pDataStatis == NULL || pQueryAttr->pFilters == NULL) {
//     return true;
//   }
986
//
L
Liu Jicong 已提交
987 988
//   return filterRangeExecute(pQueryAttr->pFilters, pDataStatis, pQueryAttr->numOfCols, numOfRows);
// }
989
#if 0
H
Haojun Liao 已提交
990
static bool overlapWithTimeWindow(STaskAttr* pQueryAttr, SDataBlockInfo* pBlockInfo) {
991 992
  STimeWindow w = {0};

dengyihao's avatar
dengyihao 已提交
993 994
  TSKEY sk = TMIN(pQueryAttr->window.skey, pQueryAttr->window.ekey);
  TSKEY ek = TMAX(pQueryAttr->window.skey, pQueryAttr->window.ekey);
995

996
  if (true) {
L
Liu Jicong 已提交
997
    //    getAlignQueryTimeWindow(pQueryAttr, pBlockInfo->window.skey, sk, ek, &w);
998 999 1000 1001 1002 1003
    assert(w.ekey >= pBlockInfo->window.skey);

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

L
Liu Jicong 已提交
1004 1005
    while (1) {
      //      getNextTimeWindow(pQueryAttr, &w);
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
      if (w.skey > pBlockInfo->window.ekey) {
        break;
      }

      assert(w.ekey > pBlockInfo->window.ekey);
      if (w.skey <= pBlockInfo->window.ekey && w.skey > pBlockInfo->window.skey) {
        return true;
      }
    }
  } else {
L
Liu Jicong 已提交
1016
    //    getAlignQueryTimeWindow(pQueryAttr, pBlockInfo->window.ekey, sk, ek, &w);
1017 1018 1019 1020 1021 1022
    assert(w.skey <= pBlockInfo->window.ekey);

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

L
Liu Jicong 已提交
1023 1024
    while (1) {
      //      getNextTimeWindow(pQueryAttr, &w);
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
      if (w.ekey < pBlockInfo->window.skey) {
        break;
      }

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

  return false;
}
1038
#endif
1039 1040

static uint32_t doFilterByBlockTimeWindow(STableScanInfo* pTableScanInfo, SSDataBlock* pBlock) {
H
Haojun Liao 已提交
1041
  SqlFunctionCtx* pCtx = pTableScanInfo->pCtx;
1042
  uint32_t        status = BLK_DATA_NOT_LOAD;
1043

L
Liu Jicong 已提交
1044
  int32_t numOfOutput = 0;  // pTableScanInfo->numOfOutput;
1045 1046
  for (int32_t i = 0; i < numOfOutput; ++i) {
    int32_t functionId = pCtx[i].functionId;
H
Haojun Liao 已提交
1047
    int32_t colId = pTableScanInfo->pExpr[i].base.pParam[0].pCol->colId;
1048 1049 1050

    // group by + first/last should not apply the first/last block filter
    if (functionId < 0) {
1051
      status |= BLK_DATA_DATA_LOAD;
1052 1053
      return status;
    } else {
L
Liu Jicong 已提交
1054
      //      status |= aAggs[functionId].dataReqFunc(&pTableScanInfo->pCtx[i], &pBlock->info.window, colId);
1055
      //      if ((status & BLK_DATA_DATA_LOAD) == BLK_DATA_DATA_LOAD) {
L
Liu Jicong 已提交
1056 1057
      //        return status;
      //      }
1058 1059 1060 1061 1062 1063
    }
  }

  return status;
}

L
Liu Jicong 已提交
1064 1065
int32_t loadDataBlockOnDemand(SExecTaskInfo* pTaskInfo, STableScanInfo* pTableScanInfo, SSDataBlock* pBlock,
                              uint32_t* status) {
1066
  *status = BLK_DATA_NOT_LOAD;
1067

H
Haojun Liao 已提交
1068
  pBlock->pDataBlock = NULL;
L
Liu Jicong 已提交
1069
  pBlock->pBlockAgg = NULL;
H
Haojun Liao 已提交
1070

L
Liu Jicong 已提交
1071 1072
  //  int64_t groupId = pRuntimeEnv->current->groupIndex;
  //  bool    ascQuery = QUERY_IS_ASC_QUERY(pQueryAttr);
1073

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

1076 1077
//  pCost->totalBlocks += 1;
//  pCost->totalRows += pBlock->info.rows;
H
Haojun Liao 已提交
1078
#if 0
1079 1080 1081
  // 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 已提交
1082
      (QUERY_IS_INTERVAL_QUERY(pQueryAttr) && overlapWithTimeWindow(pTaskInfo, &pBlock->info))) {
1083
    (*status) = BLK_DATA_DATA_LOAD;
1084 1085 1086
  }

  // check if this data block is required to load
1087
  if ((*status) != BLK_DATA_DATA_LOAD) {
1088 1089 1090 1091 1092 1093 1094
    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 已提交
1095
      bool  masterScan = IS_MAIN_SCAN(pRuntimeEnv);
1096 1097 1098 1099 1100 1101
      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,
1102
                                    pTableScanInfo->rowEntryInfoOffset);
1103 1104 1105
      } else {
        if (setResultOutputBufByKey(pRuntimeEnv, pTableScanInfo->pResultRowInfo, pBlock->info.uid, &win, masterScan, &pResult, groupId,
                                    pTableScanInfo->pCtx, pTableScanInfo->numOfOutput,
1106
                                    pTableScanInfo->rowEntryInfoOffset) != TSDB_CODE_SUCCESS) {
1107 1108 1109 1110 1111
          longjmp(pRuntimeEnv->env, TSDB_CODE_QRY_OUT_OF_MEMORY);
        }
      }
    } else if (pQueryAttr->stableQuery && (!pQueryAttr->tsCompQuery) && (!pQueryAttr->diffQuery)) { // stable aggregate, not interval aggregate or normal column aggregate
      doSetTableGroupOutputBuf(pRuntimeEnv, pTableScanInfo->pResultRowInfo, pTableScanInfo->pCtx,
1112
                               pTableScanInfo->rowEntryInfoOffset, pTableScanInfo->numOfOutput,
1113 1114 1115 1116 1117 1118
                               pRuntimeEnv->current->groupIndex);
    }

    if (needFilter) {
      (*status) = doFilterByBlockTimeWindow(pTableScanInfo, pBlock);
    } else {
1119
      (*status) = BLK_DATA_DATA_LOAD;
1120 1121 1122 1123
    }
  }

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

1126
  if ((*status) == BLK_DATA_NOT_LOAD || (*status) == BLK_DATA_FILTEROUT) {
1127 1128
    //qDebug("QInfo:0x%"PRIx64" data block discard, brange:%" PRId64 "-%" PRId64 ", rows:%d", pQInfo->qId, pBlockInfo->window.skey,
//           pBlockInfo->window.ekey, pBlockInfo->rows);
1129
    pCost->skipBlocks += 1;
1130
  } else if ((*status) == BLK_DATA_SMA_LOAD) {
1131 1132
    // this function never returns error?
    pCost->loadBlockStatis += 1;
1133
//    tsdbRetrieveDatablockSMA(pTableScanInfo->pTsdbReadHandle, &pBlock->pBlockAgg);
1134 1135

    if (pBlock->pBlockAgg == NULL) {  // data block statistics does not exist, load data block
1136
//      pBlock->pDataBlock = tsdbRetrieveDataBlock(pTableScanInfo->pTsdbReadHandle, NULL);
1137 1138 1139
      pCost->totalCheckedRows += pBlock->info.rows;
    }
  } else {
1140
    assert((*status) == BLK_DATA_DATA_LOAD);
1141 1142 1143

    // load the data block statistics to perform further filter
    pCost->loadBlockStatis += 1;
1144
//    tsdbRetrieveDatablockSMA(pTableScanInfo->pTsdbReadHandle, &pBlock->pBlockAgg);
1145 1146 1147 1148 1149 1150

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

H
Haojun Liao 已提交
1151
          bool  masterScan = IS_MAIN_SCAN(pRuntimeEnv);
1152 1153 1154 1155 1156
          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,
1157
                                      pTableScanInfo->rowEntryInfoOffset) != TSDB_CODE_SUCCESS) {
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
            longjmp(pRuntimeEnv->env, TSDB_CODE_QRY_OUT_OF_MEMORY);
          }
        }
      }
      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
1169
            pCost->skipBlocks += 1;
1170 1171
            //qDebug("QInfo:0x%"PRIx64" data block discard, brange:%" PRId64 "-%" PRId64 ", rows:%d", pQInfo->qId,
//                   pBlockInfo->window.skey, pBlockInfo->window.ekey, pBlockInfo->rows);
1172
            (*status) = BLK_DATA_FILTEROUT;
1173 1174 1175 1176 1177 1178 1179 1180
            return TSDB_CODE_SUCCESS;
          }
        }
      }
    }

    // current block has been discard due to filter applied
//    if (!doFilterByBlockStatistics(pRuntimeEnv, pBlock->pBlockAgg, pTableScanInfo->pCtx, pBlockInfo->rows)) {
1181
//      pCost->skipBlocks += 1;
1182 1183
//      qDebug("QInfo:0x%"PRIx64" data block discard, brange:%" PRId64 "-%" PRId64 ", rows:%d", pQInfo->qId, pBlockInfo->window.skey,
//             pBlockInfo->window.ekey, pBlockInfo->rows);
1184
//      (*status) = BLK_DATA_FILTEROUT;
1185 1186 1187 1188 1189
//      return TSDB_CODE_SUCCESS;
//    }

    pCost->totalCheckedRows += pBlockInfo->rows;
    pCost->loadBlocks += 1;
1190
//    pBlock->pDataBlock = tsdbRetrieveDataBlock(pTableScanInfo->pTsdbReadHandle, NULL);
1191 1192 1193 1194 1195
//    if (pBlock->pDataBlock == NULL) {
//      return terrno;
//    }

//    if (pQueryAttr->pFilters != NULL) {
1196
//      filterSetColFieldData(pQueryAttr->pFilters, taosArrayGetSize(pBlock->pDataBlock), pBlock->pDataBlock);
1197
//    }
1198

1199 1200 1201 1202
//    if (pQueryAttr->pFilters != NULL || pRuntimeEnv->pTsBuf != NULL) {
//      filterColRowsInDataBlock(pRuntimeEnv, pBlock, ascQuery);
//    }
  }
H
Haojun Liao 已提交
1203
#endif
1204 1205 1206
  return TSDB_CODE_SUCCESS;
}

L
Liu Jicong 已提交
1207
static void updateTableQueryInfoForReverseScan(STableQueryInfo* pTableQueryInfo) {
1208 1209 1210 1211
  if (pTableQueryInfo == NULL) {
    return;
  }

wafwerar's avatar
wafwerar 已提交
1212
  //  TSWAP(pTableQueryInfo->win.skey, pTableQueryInfo->win.ekey);
L
Liu Jicong 已提交
1213
  //  pTableQueryInfo->lastKey = pTableQueryInfo->win.skey;
1214

L
Liu Jicong 已提交
1215 1216
  //  SWITCH_ORDER(pTableQueryInfo->cur.order);
  //  pTableQueryInfo->cur.vgroupIndex = -1;
1217 1218

  // set the index to be the end slot of result rows array
dengyihao's avatar
dengyihao 已提交
1219 1220 1221 1222 1223 1224
  //  SResultRowInfo* pResultRowInfo = &pTableQueryInfo->resInfo;
  //  if (pResultRowInfo->size > 0) {
  //    pResultRowInfo->curPos = pResultRowInfo->size - 1;
  //  } else {
  //    pResultRowInfo->curPos = -1;
  //  }
1225 1226
}

H
Haojun Liao 已提交
1227
void initResultRow(SResultRow* pResultRow) {
X
Xiaoyu Wang 已提交
1228
  //  pResultRow->pEntryInfo = (struct SResultRowEntryInfo*)((char*)pResultRow + sizeof(SResultRow));
1229 1230 1231 1232 1233
}

/*
 * The start of each column SResultRowEntryInfo is denote by RowCellInfoOffset.
 * Note that in case of top/bottom query, the whole multiple rows of result is treated as only one row of results.
H
Haojun Liao 已提交
1234 1235 1236
 * +------------+-----------------result column 1------------+------------------result column 2-----------+
 * | SResultRow | SResultRowEntryInfo | intermediate buffer1 | SResultRowEntryInfo | intermediate buffer 2|
 * +------------+--------------------------------------------+--------------------------------------------+
1237 1238
 *           offset[0]                                  offset[1]                                   offset[2]
 */
1239
// TODO refactor: some function move away
L
Liu Jicong 已提交
1240 1241 1242
void setFunctionResultOutput(SOperatorInfo* pOperator, SOptrBasicInfo* pInfo, SAggSupporter* pSup, int32_t stage,
                             int32_t numOfExprs) {
  SExecTaskInfo*  pTaskInfo = pOperator->pTaskInfo;
1243 1244
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
  int32_t*        rowEntryInfoOffset = pOperator->exprSupp.rowEntryInfoOffset;
H
Haojun Liao 已提交
1245

H
Haojun Liao 已提交
1246
  SResultRowInfo* pResultRowInfo = &pInfo->resultRowInfo;
1247
  initResultRowInfo(pResultRowInfo);
H
Haojun Liao 已提交
1248

L
Liu Jicong 已提交
1249 1250
  int64_t     tid = 0;
  int64_t     groupId = 0;
1251 1252
  SResultRow* pRow = doSetResultOutBufByKey(pSup->pResultBuf, pResultRowInfo, (char*)&tid, sizeof(tid), true, groupId,
                                            pTaskInfo, false, pSup);
H
Haojun Liao 已提交
1253

1254
  for (int32_t i = 0; i < numOfExprs; ++i) {
1255
    struct SResultRowEntryInfo* pEntry = getResultEntryInfo(pRow, i, rowEntryInfoOffset);
H
Haojun Liao 已提交
1256 1257
    cleanupResultRowEntry(pEntry);

L
Liu Jicong 已提交
1258
    pCtx[i].resultInfo = pEntry;
1259
    pCtx[i].scanFlag = stage;
H
Haojun Liao 已提交
1260 1261
  }

1262
  initCtxOutputBuffer(pCtx, numOfExprs);
H
Haojun Liao 已提交
1263 1264
}

H
Haojun Liao 已提交
1265
void initCtxOutputBuffer(SqlFunctionCtx* pCtx, int32_t size) {
1266 1267
  for (int32_t j = 0; j < size; ++j) {
    struct SResultRowEntryInfo* pResInfo = GET_RES_INFO(&pCtx[j]);
dengyihao's avatar
dengyihao 已提交
1268 1269
    if (isRowEntryInitialized(pResInfo) || fmIsPseudoColumnFunc(pCtx[j].functionId) || pCtx[j].functionId == -1 ||
        fmIsScalarFunc(pCtx[j].functionId)) {
1270 1271 1272
      continue;
    }

H
Haojun Liao 已提交
1273
    pCtx[j].fpSet.init(&pCtx[j], pCtx[j].resultInfo);
1274 1275 1276
  }
}

L
Liu Jicong 已提交
1277
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
1278
  if (status == TASK_NOT_COMPLETED) {
H
Haojun Liao 已提交
1279
    pTaskInfo->status = status;
1280 1281
  } else {
    // QUERY_NOT_COMPLETED is not compatible with any other status, so clear its position first
1282
    CLEAR_QUERY_STATUS(pTaskInfo, TASK_NOT_COMPLETED);
H
Haojun Liao 已提交
1283
    pTaskInfo->status |= status;
1284 1285 1286
  }
}

L
Liu Jicong 已提交
1287
void destroyTableQueryInfoImpl(STableQueryInfo* pTableQueryInfo) {
1288 1289 1290 1291
  if (pTableQueryInfo == NULL) {
    return;
  }

L
Liu Jicong 已提交
1292
  //  taosVariantDestroy(&pTableQueryInfo->tag);
dengyihao's avatar
dengyihao 已提交
1293
  //  cleanupResultRowInfo(&pTableQueryInfo->resInfo);
1294 1295
}

1296
void setResultRowInitCtx(SResultRow* pResult, SqlFunctionCtx* pCtx, int32_t numOfOutput, int32_t* rowEntryInfoOffset) {
1297
  for (int32_t i = 0; i < numOfOutput; ++i) {
1298
    pCtx[i].resultInfo = getResultEntryInfo(pResult, i, rowEntryInfoOffset);
1299 1300 1301 1302 1303

    struct SResultRowEntryInfo* pResInfo = pCtx[i].resultInfo;
    if (isRowEntryCompleted(pResInfo) && isRowEntryInitialized(pResInfo)) {
      continue;
    }
1304 1305 1306 1307 1308

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

1309 1310 1311 1312 1313 1314
    if (!pResInfo->initialized) {
      if (pCtx[i].functionId != -1) {
        pCtx[i].fpSet.init(&pCtx[i], pResInfo);
      } else {
        pResInfo->initialized = true;
      }
1315 1316 1317 1318
    }
  }
}

H
Haojun Liao 已提交
1319
static void extractQualifiedTupleByFilterResult(SSDataBlock* pBlock, const int8_t* rowRes, bool keep);
1320

1321
void doFilter(const SNode* pFilterNode, SSDataBlock* pBlock) {
1322 1323 1324 1325 1326
  if (pFilterNode == NULL) {
    return;
  }

  SFilterInfo* filter = NULL;
H
Haojun Liao 已提交
1327

H
Haojun Liao 已提交
1328
  // todo move to the initialization function
H
Haojun Liao 已提交
1329
  int32_t code = filterInitFromNode((SNode*)pFilterNode, &filter, 0);
1330

1331
  size_t             numOfCols = taosArrayGetSize(pBlock->pDataBlock);
1332
  SFilterColumnParam param1 = {.numOfCols = numOfCols, .pDataBlock = pBlock->pDataBlock};
1333 1334 1335
  code = filterSetDataFromSlotId(filter, &param1);

  int8_t* rowRes = NULL;
1336

1337
  // todo the keep seems never to be True??
1338
  bool keep = filterExecute(filter, pBlock, &rowRes, NULL, param1.numOfCols);
D
dapan1121 已提交
1339
  filterFreeInfo(filter);
1340

H
Haojun Liao 已提交
1341
  extractQualifiedTupleByFilterResult(pBlock, rowRes, keep);
1342
  blockDataUpdateTsWindow(pBlock, 0);
H
Haojun Liao 已提交
1343 1344

  taosMemoryFree(rowRes);
1345 1346
}

H
Haojun Liao 已提交
1347
void extractQualifiedTupleByFilterResult(SSDataBlock* pBlock, const int8_t* rowRes, bool keep) {
1348 1349 1350 1351 1352
  if (keep) {
    return;
  }

  if (rowRes != NULL) {
L
Liu Jicong 已提交
1353
    int32_t      totalRows = pBlock->info.rows;
1354
    SSDataBlock* px = createOneDataBlock(pBlock, true);
1355

1356 1357
    size_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
    for (int32_t i = 0; i < numOfCols; ++i) {
1358 1359
      SColumnInfoData* pSrc = taosArrayGet(px->pDataBlock, i);
      SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, i);
1360
      // it is a reserved column for scalar function, and no data in this column yet.
1361
      if (pDst->pData == NULL || pSrc->pData == NULL) {
1362 1363 1364
        continue;
      }

1365 1366
      colInfoDataCleanup(pDst, pBlock->info.rows);

1367
      int32_t numOfRows = 0;
1368
      for (int32_t j = 0; j < totalRows; ++j) {
D
dapan1121 已提交
1369 1370 1371
        if (rowRes[j] == 0) {
          continue;
        }
1372

D
dapan1121 已提交
1373
        if (colDataIsNull_s(pSrc, j)) {
1374
          colDataAppendNULL(pDst, numOfRows);
D
dapan1121 已提交
1375
        } else {
1376
          colDataAppend(pDst, numOfRows, colDataGetData(pSrc, j), false);
D
dapan1121 已提交
1377
        }
1378
        numOfRows += 1;
H
Haojun Liao 已提交
1379
      }
1380

1381 1382 1383 1384 1385
      if (pBlock->info.rows == totalRows) {
        pBlock->info.rows = numOfRows;
      } else {
        ASSERT(pBlock->info.rows == numOfRows);
      }
1386
    }
1387

dengyihao's avatar
dengyihao 已提交
1388
    blockDataDestroy(px);  // fix memory leak
1389 1390 1391
  } else {
    // do nothing
    pBlock->info.rows = 0;
1392 1393 1394
  }
}

L
Liu Jicong 已提交
1395 1396
void doSetTableGroupOutputBuf(SOperatorInfo* pOperator, SAggOperatorInfo* pAggInfo, int32_t numOfOutput,
                              uint64_t groupId) {
1397
  // for simple group by query without interval, all the tables belong to one group result.
L
Liu Jicong 已提交
1398
  SExecTaskInfo*  pTaskInfo = pOperator->pTaskInfo;
1399
  SResultRowInfo* pResultRowInfo = &pAggInfo->binfo.resultRowInfo;
1400 1401
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
  int32_t*        rowEntryInfoOffset = pOperator->exprSupp.rowEntryInfoOffset;
1402

1403
  SResultRow* pResultRow = doSetResultOutBufByKey(pAggInfo->aggSup.pResultBuf, pResultRowInfo, (char*)&groupId,
L
Liu Jicong 已提交
1404
                                                  sizeof(groupId), true, groupId, pTaskInfo, false, &pAggInfo->aggSup);
L
Liu Jicong 已提交
1405
  assert(pResultRow != NULL);
1406 1407 1408 1409 1410 1411

  /*
   * 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 已提交
1412 1413
    int32_t ret =
        addNewWindowResultBuf(pResultRow, pAggInfo->aggSup.pResultBuf, groupId, pAggInfo->binfo.pRes->info.rowSize);
1414 1415 1416 1417 1418
    if (ret != TSDB_CODE_SUCCESS) {
      return;
    }
  }

1419
  setResultRowInitCtx(pResultRow, pCtx, numOfOutput, rowEntryInfoOffset);
1420 1421
}

1422
void setExecutionContext(SOperatorInfo* pOperator, int32_t numOfOutput, uint64_t groupId, SAggOperatorInfo* pAggInfo) {
H
Haojun Liao 已提交
1423
  if (pAggInfo->groupId != INT32_MIN && pAggInfo->groupId == groupId) {
1424 1425
    return;
  }
1426
#ifdef BUF_PAGE_DEBUG
L
Liu Jicong 已提交
1427
  qDebug("page_setbuf, groupId:%" PRIu64, groupId);
1428
#endif
1429
  doSetTableGroupOutputBuf(pOperator, pAggInfo, numOfOutput, groupId);
1430 1431

  // record the current active group id
H
Haojun Liao 已提交
1432
  pAggInfo->groupId = groupId;
1433 1434
}

1435 1436
static void doUpdateNumOfRows(SResultRow* pRow, int32_t numOfExprs, const int32_t* rowCellOffset) {
  for (int32_t j = 0; j < numOfExprs; ++j) {
1437
    struct SResultRowEntryInfo* pResInfo = getResultEntryInfo(pRow, j, rowCellOffset);
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
    if (!isRowEntryInitialized(pResInfo)) {
      continue;
    }

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

1448
int32_t finalizeResultRowIntoResultDataBlock(SDiskbasedBuf* pBuf, SResultRowPosition* resultRowPosition,
S
shenglian zhou 已提交
1449 1450 1451
                                             SqlFunctionCtx* pCtx, SExprInfo* pExprInfo, int32_t numOfExprs,
                                             const int32_t* rowCellOffset, SSDataBlock* pBlock,
                                             SExecTaskInfo* pTaskInfo) {
1452 1453 1454 1455 1456 1457 1458 1459 1460
  SFilePage*  page = getBufPage(pBuf, resultRowPosition->pageId);
  SResultRow* pRow = (SResultRow*)((char*)page + resultRowPosition->offset);

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

1461 1462 1463 1464 1465 1466 1467
  while (pBlock->info.rows + pRow->numOfRows > pBlock->info.capacity) {
    int32_t code = blockDataEnsureCapacity(pBlock, pBlock->info.capacity * 1.25);
    if (TAOS_FAILED(code)) {
      releaseBufPage(pBuf, page);
      qError("%s ensure result data capacity failed, code %s", GET_TASKID(pTaskInfo), tstrerror(code));
      longjmp(pTaskInfo->env, code);
    }
1468 1469 1470 1471 1472
  }

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

1473
    pCtx[j].resultInfo = getResultEntryInfo(pRow, j, rowCellOffset);
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
    if (pCtx[j].fpSet.finalize) {
      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));
        longjmp(pTaskInfo->env, code);
      }
    } 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., _wstartts, top(k, 20)
      // the _wstartts 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);
      }
    }
  }

  releaseBufPage(pBuf, page);
1494
  pBlock->info.rows += pRow->numOfRows;
1495 1496 1497 1498

  return 0;
}

X
Xiaoyu Wang 已提交
1499 1500 1501
int32_t doCopyToSDataBlock(SExecTaskInfo* pTaskInfo, SSDataBlock* pBlock, SExprInfo* pExprInfo, SDiskbasedBuf* pBuf,
                           SGroupResInfo* pGroupResInfo, const int32_t* rowCellOffset, SqlFunctionCtx* pCtx,
                           int32_t numOfExprs) {
1502
  int32_t numOfRows = getNumOfTotalRes(pGroupResInfo);
1503
  int32_t start = pGroupResInfo->index;
1504
#ifdef BUF_PAGE_DEBUG
wmmhello's avatar
wmmhello 已提交
1505
  qDebug("\npage_copytoblock rows:%d", numOfRows);
1506
#endif
1507
  for (int32_t i = start; i < numOfRows; i += 1) {
L
Liu Jicong 已提交
1508 1509
    SResKeyPos* pPos = taosArrayGetP(pGroupResInfo->pRows, i);
    SFilePage*  page = getBufPage(pBuf, pPos->pos.pageId);
1510
#ifdef BUF_PAGE_DEBUG
wmmhello's avatar
wmmhello 已提交
1511
    qDebug("page_copytoblock pos pageId:%d, offset:%d", pPos->pos.pageId, pPos->pos.offset);
1512
#endif
1513
    SResultRow* pRow = (SResultRow*)((char*)page + pPos->pos.offset);
1514 1515

    doUpdateNumOfRows(pRow, numOfExprs, rowCellOffset);
1516 1517
    if (pRow->numOfRows == 0) {
      pGroupResInfo->index += 1;
1518
      releaseBufPage(pBuf, page);
1519 1520 1521
      continue;
    }

1522 1523 1524 1525 1526
    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) {
1527
        releaseBufPage(pBuf, page);
1528 1529 1530 1531
        break;
      }
    }

1532
    if (pBlock->info.rows + pRow->numOfRows > pBlock->info.capacity) {
1533
      releaseBufPage(pBuf, page);
1534 1535 1536 1537 1538
      break;
    }

    pGroupResInfo->index += 1;

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

1542
      pCtx[j].resultInfo = getResultEntryInfo(pRow, j, rowCellOffset);
1543
      if (pCtx[j].fpSet.finalize) {
1544
#ifdef BUF_PAGE_DEBUG
wmmhello's avatar
wmmhello 已提交
1545
        qDebug("\npage_finalize %d", numOfExprs);
1546
#endif
1547
        int32_t code = pCtx[j].fpSet.finalize(&pCtx[j], pBlock);
1548
        if (TAOS_FAILED(code)) {
1549 1550
          qError("%s build result data block error, code %s", GET_TASKID(pTaskInfo), tstrerror(code));
          longjmp(pTaskInfo->env, code);
1551
        }
1552 1553
      } else if (strcmp(pCtx[j].pExpr->pExpr->_function.functionName, "_select_value") == 0) {
        // do nothing, todo refactor
1554
      } else {
1555 1556
        // expand the result into multiple rows. E.g., _wstartts, top(k, 20)
        // the _wstartts needs to copy to 20 following rows, since the results of top-k expands to 20 different rows.
X
Xiaoyu Wang 已提交
1557 1558
        SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, slotId);
        char*            in = GET_ROWCELL_INTERBUF(pCtx[j].resultInfo);
1559
        if (pCtx[j].increase) {
L
Liu Jicong 已提交
1560
          int64_t ts = *(int64_t*)in;
1561
          for (int32_t k = 0; k < pRow->numOfRows; ++k) {
L
Liu Jicong 已提交
1562
            colDataAppend(pColInfoData, pBlock->info.rows + k, (const char*)&ts, pCtx[j].resultInfo->isNullRes);
1563 1564 1565 1566 1567 1568
            ts++;
          }
        } else {
          for (int32_t k = 0; k < pRow->numOfRows; ++k) {
            colDataAppend(pColInfoData, pBlock->info.rows + k, in, pCtx[j].resultInfo->isNullRes);
          }
X
Xiaoyu Wang 已提交
1569
        }
1570
      }
1571 1572
    }

1573
    releaseBufPage(pBuf, page);
1574
    pBlock->info.rows += pRow->numOfRows;
L
Liu Jicong 已提交
1575 1576 1577
    //    if (pBlock->info.rows >= pBlock->info.capacity) {  // output buffer is full
    //      break;
    //    }
1578 1579
  }

X
Xiaoyu Wang 已提交
1580 1581
  qDebug("%s result generated, rows:%d, groupId:%" PRIu64, GET_TASKID(pTaskInfo), pBlock->info.rows,
         pBlock->info.groupId);
1582
  blockDataUpdateTsWindow(pBlock, 0);
1583 1584 1585
  return 0;
}

X
Xiaoyu Wang 已提交
1586 1587
void doBuildResultDatablock(SOperatorInfo* pOperator, SOptrBasicInfo* pbInfo, SGroupResInfo* pGroupResInfo,
                            SDiskbasedBuf* pBuf) {
1588 1589
  SExprInfo*     pExprInfo = pOperator->exprSupp.pExprInfo;
  int32_t        numOfExprs = pOperator->exprSupp.numOfExprs;
1590 1591
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;

1592
  int32_t*        rowCellOffset = pOperator->exprSupp.rowEntryInfoOffset;
X
Xiaoyu Wang 已提交
1593
  SSDataBlock*    pBlock = pbInfo->pRes;
1594
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
1595

1596
  blockDataCleanup(pBlock);
1597
  if (!hasDataInGroupInfo(pGroupResInfo)) {
1598 1599 1600
    return;
  }

1601 1602
  // clear the existed group id
  pBlock->info.groupId = 0;
1603
  doCopyToSDataBlock(pTaskInfo, pBlock, pExprInfo, pBuf, pGroupResInfo, rowCellOffset, pCtx, numOfExprs);
1604 1605
}

L
Liu Jicong 已提交
1606
static void updateNumOfRowsInResultRows(SqlFunctionCtx* pCtx, int32_t numOfOutput, SResultRowInfo* pResultRowInfo,
1607
                                        int32_t* rowEntryInfoOffset) {
1608
  // update the number of result for each, only update the number of rows for the corresponding window result.
L
Liu Jicong 已提交
1609 1610 1611
  //  if (QUERY_IS_INTERVAL_QUERY(pQueryAttr)) {
  //    return;
  //  }
H
Haojun Liao 已提交
1612
#if 0
1613
  for (int32_t i = 0; i < pResultRowInfo->size; ++i) {
L
Liu Jicong 已提交
1614
    SResultRow* pResult = pResultRowInfo->pResult[i];
1615 1616 1617 1618 1619 1620 1621

    for (int32_t j = 0; j < numOfOutput; ++j) {
      int32_t functionId = pCtx[j].functionId;
      if (functionId == FUNCTION_TS || functionId == FUNCTION_TAG || functionId == FUNCTION_TAGPRJ) {
        continue;
      }

1622
      SResultRowEntryInfo* pCell = getResultEntryInfo(pResult, j, rowEntryInfoOffset);
1623
      pResult->numOfRows = (uint16_t)(TMAX(pResult->numOfRows, pCell->numOfRes));
1624 1625
    }
  }
H
Haojun Liao 已提交
1626
#endif
1627 1628
}

L
Liu Jicong 已提交
1629
static int32_t compressQueryColData(SColumnInfoData* pColRes, int32_t numOfRows, char* data, int8_t compressed) {
1630 1631
  int32_t colSize = pColRes->info.bytes * numOfRows;
  return (*(tDataTypes[pColRes->info.type].compFunc))(pColRes->pData, colSize, numOfRows, data,
L
Liu Jicong 已提交
1632
                                                      colSize + COMP_OVERFLOW_BYTES, compressed, NULL, 0);
1633 1634
}

1635 1636 1637
int32_t doFillTimeIntervalGapsInResults(struct SFillInfo* pFillInfo, SSDataBlock* pBlock, int32_t capacity) {
  int32_t numOfRows = (int32_t)taosFillResultDataBlock(pFillInfo, pBlock, capacity - pBlock->info.rows);
  pBlock->info.rows += numOfRows;
1638

1639
  return pBlock->info.rows;
1640 1641
}

L
Liu Jicong 已提交
1642 1643
void queryCostStatis(SExecTaskInfo* pTaskInfo) {
  STaskCostInfo* pSummary = &pTaskInfo->cost;
1644

L
Liu Jicong 已提交
1645 1646 1647
  //  uint64_t hashSize = taosHashGetMemSize(pQInfo->runtimeEnv.pResultRowHashTable);
  //  hashSize += taosHashGetMemSize(pRuntimeEnv->tableqinfoGroupInfo.map);
  //  pSummary->hashSize = hashSize;
1648 1649 1650 1651

  // add the merge time
  pSummary->elapsedTime += pSummary->firstStageMergeTime;

L
Liu Jicong 已提交
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662
  //  SResultRowPool* p = pTaskInfo->pool;
  //  if (p != NULL) {
  //    pSummary->winInfoSize = getResultRowPoolMemSize(p);
  //    pSummary->numOfTimeWindows = getNumOfAllocatedResultRows(p);
  //  } else {
  //    pSummary->winInfoSize = 0;
  //    pSummary->numOfTimeWindows = 0;
  //  }
  //
  //  calculateOperatorProfResults(pQInfo);

1663 1664
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
  if (pSummary->pRecoder != NULL) {
X
Xiaoyu Wang 已提交
1665 1666
    qDebug("%s :cost summary: elapsed time:%" PRId64 " us, first merge:%" PRId64
           " us, total blocks:%d, "
1667 1668 1669 1670
           "load block statis:%d, load data block:%d, total rows:%" PRId64 ", check rows:%" PRId64,
           GET_TASKID(pTaskInfo), pSummary->elapsedTime, pSummary->firstStageMergeTime, pRecorder->totalBlocks,
           pRecorder->loadBlockStatis, pRecorder->loadBlocks, pRecorder->totalRows, pRecorder->totalCheckedRows);
  }
L
Liu Jicong 已提交
1671 1672 1673
  // qDebug("QInfo:0x%"PRIx64" :cost summary: winResPool size:%.2f Kb, numOfWin:%"PRId64", tableInfoSize:%.2f Kb,
  // hashTable:%.2f Kb", pQInfo->qId, pSummary->winInfoSize/1024.0,
  //      pSummary->numOfTimeWindows, pSummary->tableInfoSize/1024.0, pSummary->hashSize/1024.0);
1674 1675
}

L
Liu Jicong 已提交
1676 1677 1678
// static void updateOffsetVal(STaskRuntimeEnv *pRuntimeEnv, SDataBlockInfo *pBlockInfo) {
//   STaskAttr *pQueryAttr = pRuntimeEnv->pQueryAttr;
//   STableQueryInfo* pTableQueryInfo = pRuntimeEnv->current;
1679
//
L
Liu Jicong 已提交
1680
//   int32_t step = GET_FORWARD_DIRECTION_FACTOR(pQueryAttr->order.order);
1681
//
L
Liu Jicong 已提交
1682 1683 1684 1685
//   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;
//   }
1686
//
L
Liu Jicong 已提交
1687 1688 1689 1690 1691
//   if (QUERY_IS_ASC_QUERY(pQueryAttr)) {
//     pQueryAttr->pos = (int32_t)pQueryAttr->limit.offset;
//   } else {
//     pQueryAttr->pos = pBlockInfo->rows - (int32_t)pQueryAttr->limit.offset - 1;
//   }
1692
//
L
Liu Jicong 已提交
1693
//   assert(pQueryAttr->pos >= 0 && pQueryAttr->pos <= pBlockInfo->rows - 1);
1694
//
L
Liu Jicong 已提交
1695 1696
//   SArray *         pDataBlock = tsdbRetrieveDataBlock(pRuntimeEnv->pTsdbReadHandle, NULL);
//   SColumnInfoData *pColInfoData = taosArrayGet(pDataBlock, 0);
1697
//
L
Liu Jicong 已提交
1698 1699
//   // update the pQueryAttr->limit.offset value, and pQueryAttr->pos value
//   TSKEY *keys = (TSKEY *) pColInfoData->pData;
1700
//
L
Liu Jicong 已提交
1701 1702 1703
//   // update the offset value
//   pTableQueryInfo->lastKey = keys[pQueryAttr->pos];
//   pQueryAttr->limit.offset = 0;
1704
//
L
Liu Jicong 已提交
1705
//   int32_t numOfRes = tableApplyFunctionsOnBlock(pRuntimeEnv, pBlockInfo, NULL, binarySearchForKey, pDataBlock);
1706
//
L
Liu Jicong 已提交
1707 1708 1709 1710
//   //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);
// }
1711

L
Liu Jicong 已提交
1712 1713
// void skipBlocks(STaskRuntimeEnv *pRuntimeEnv) {
//   STaskAttr *pQueryAttr = pRuntimeEnv->pQueryAttr;
1714
//
L
Liu Jicong 已提交
1715 1716 1717
//   if (pQueryAttr->limit.offset <= 0 || pQueryAttr->numOfFilterCols > 0) {
//     return;
//   }
1718
//
L
Liu Jicong 已提交
1719 1720
//   pQueryAttr->pos = 0;
//   int32_t step = GET_FORWARD_DIRECTION_FACTOR(pQueryAttr->order.order);
1721
//
L
Liu Jicong 已提交
1722 1723
//   STableQueryInfo* pTableQueryInfo = pRuntimeEnv->current;
//   TsdbQueryHandleT pTsdbReadHandle = pRuntimeEnv->pTsdbReadHandle;
1724
//
L
Liu Jicong 已提交
1725 1726 1727 1728 1729
//   SDataBlockInfo blockInfo = SDATA_BLOCK_INITIALIZER;
//   while (tsdbNextDataBlock(pTsdbReadHandle)) {
//     if (isTaskKilled(pRuntimeEnv->qinfo)) {
//       longjmp(pRuntimeEnv->env, TSDB_CODE_TSC_QUERY_CANCELLED);
//     }
1730
//
L
Liu Jicong 已提交
1731
//     tsdbRetrieveDataBlockInfo(pTsdbReadHandle, &blockInfo);
1732
//
L
Liu Jicong 已提交
1733 1734 1735 1736
//     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;
1737
//
L
Liu Jicong 已提交
1738 1739 1740 1741 1742 1743 1744
//       //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;
//     }
//   }
1745
//
L
Liu Jicong 已提交
1746 1747 1748 1749 1750 1751 1752 1753 1754
//   if (terrno != TSDB_CODE_SUCCESS) {
//     longjmp(pRuntimeEnv->env, terrno);
//   }
// }

// static TSKEY doSkipIntervalProcess(STaskRuntimeEnv* pRuntimeEnv, STimeWindow* win, SDataBlockInfo* pBlockInfo,
// STableQueryInfo* pTableQueryInfo) {
//   STaskAttr *pQueryAttr = pRuntimeEnv->pQueryAttr;
//   SResultRowInfo *pWindowResInfo = &pRuntimeEnv->resultRowInfo;
1755
//
L
Liu Jicong 已提交
1756 1757 1758
//   assert(pQueryAttr->limit.offset == 0);
//   STimeWindow tw = *win;
//   getNextTimeWindow(pQueryAttr, &tw);
1759
//
L
Liu Jicong 已提交
1760 1761
//   if ((tw.skey <= pBlockInfo->window.ekey && QUERY_IS_ASC_QUERY(pQueryAttr)) ||
//       (tw.ekey >= pBlockInfo->window.skey && !QUERY_IS_ASC_QUERY(pQueryAttr))) {
1762
//
L
Liu Jicong 已提交
1763 1764 1765 1766
//     // 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);
1767
//
L
Liu Jicong 已提交
1768 1769 1770 1771
//     tw = *win;
//     int32_t startPos =
//         getNextQualifiedWindow(pQueryAttr, &tw, pBlockInfo, pColInfoData->pData, binarySearchForKey, -1);
//     assert(startPos >= 0);
1772
//
L
Liu Jicong 已提交
1773 1774
//     // set the abort info
//     pQueryAttr->pos = startPos;
1775
//
L
Liu Jicong 已提交
1776 1777 1778 1779
//     // reset the query start timestamp
//     pTableQueryInfo->win.skey = ((TSKEY *)pColInfoData->pData)[startPos];
//     pQueryAttr->window.skey = pTableQueryInfo->win.skey;
//     TSKEY key = pTableQueryInfo->win.skey;
1780
//
L
Liu Jicong 已提交
1781 1782
//     pWindowResInfo->prevSKey = tw.skey;
//     int32_t index = pRuntimeEnv->resultRowInfo.curIndex;
1783
//
L
Liu Jicong 已提交
1784 1785
//     int32_t numOfRes = tableApplyFunctionsOnBlock(pRuntimeEnv, pBlockInfo, NULL, binarySearchForKey, pDataBlock);
//     pRuntimeEnv->resultRowInfo.curIndex = index;  // restore the window index
1786
//
L
Liu Jicong 已提交
1787 1788 1789 1790
//     //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);
1791
//
L
Liu Jicong 已提交
1792 1793 1794 1795 1796
//     return key;
//   } else {  // do nothing
//     pQueryAttr->window.skey      = tw.skey;
//     pWindowResInfo->prevSKey = tw.skey;
//     pTableQueryInfo->lastKey = tw.skey;
1797
//
L
Liu Jicong 已提交
1798 1799
//     return tw.skey;
//   }
1800
//
L
Liu Jicong 已提交
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
//   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);
//   }
1811
//
L
Liu Jicong 已提交
1812 1813 1814 1815 1816
//   // 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;
//   }
1817
//
L
Liu Jicong 已提交
1818 1819 1820 1821 1822 1823 1824
//   /*
//    * 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);
1825
//
L
Liu Jicong 已提交
1826 1827
//   STimeWindow w = TSWINDOW_INITIALIZER;
//   bool ascQuery = QUERY_IS_ASC_QUERY(pQueryAttr);
1828
//
L
Liu Jicong 已提交
1829 1830
//   SResultRowInfo *pWindowResInfo = &pRuntimeEnv->resultRowInfo;
//   STableQueryInfo *pTableQueryInfo = pRuntimeEnv->current;
1831
//
L
Liu Jicong 已提交
1832 1833 1834
//   SDataBlockInfo blockInfo = SDATA_BLOCK_INITIALIZER;
//   while (tsdbNextDataBlock(pRuntimeEnv->pTsdbReadHandle)) {
//     tsdbRetrieveDataBlockInfo(pRuntimeEnv->pTsdbReadHandle, &blockInfo);
1835
//
L
Liu Jicong 已提交
1836 1837 1838 1839 1840 1841 1842 1843 1844
//     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;
//     }
1845
//
L
Liu Jicong 已提交
1846 1847
//     // the first time window
//     STimeWindow win = getActiveTimeWindow(pWindowResInfo, pWindowResInfo->prevSKey, pQueryAttr);
1848
//
L
Liu Jicong 已提交
1849 1850
//     while (pQueryAttr->limit.offset > 0) {
//       STimeWindow tw = win;
1851
//
L
Liu Jicong 已提交
1852 1853 1854
//       if ((win.ekey <= blockInfo.window.ekey && ascQuery) || (win.ekey >= blockInfo.window.skey && !ascQuery)) {
//         pQueryAttr->limit.offset -= 1;
//         pWindowResInfo->prevSKey = win.skey;
1855
//
L
Liu Jicong 已提交
1856 1857 1858 1859 1860 1861
//         // 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;
//         }
//       }
1862
//
L
Liu Jicong 已提交
1863 1864 1865 1866
//       if (pQueryAttr->limit.offset == 0) {
//         *start = doSkipIntervalProcess(pRuntimeEnv, &win, &blockInfo, pTableQueryInfo);
//         return true;
//       }
1867
//
L
Liu Jicong 已提交
1868 1869
//       // current window does not ended in current data block, try next data block
//       getNextTimeWindow(pQueryAttr, &tw);
1870
//
L
Liu Jicong 已提交
1871 1872 1873 1874 1875 1876 1877 1878 1879
//       /*
//        * 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)) {
1880
//
L
Liu Jicong 已提交
1881 1882
//         SArray *pDataBlock = tsdbRetrieveDataBlock(pRuntimeEnv->pTsdbReadHandle, NULL);
//         SColumnInfoData *pColInfoData = taosArrayGet(pDataBlock, 0);
1883
//
L
Liu Jicong 已提交
1884 1885 1886
//         if ((win.ekey > blockInfo.window.ekey && ascQuery) || (win.ekey < blockInfo.window.skey && !ascQuery)) {
//           pQueryAttr->limit.offset -= 1;
//         }
1887
//
L
Liu Jicong 已提交
1888 1889 1890 1891 1892 1893 1894 1895
//         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);
1896
//
L
Liu Jicong 已提交
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907
//           // 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.
//       }
//     }
//   }
1908
//
L
Liu Jicong 已提交
1909 1910 1911 1912
//   // check for error
//   if (terrno != TSDB_CODE_SUCCESS) {
//     longjmp(pRuntimeEnv->env, terrno);
//   }
1913
//
L
Liu Jicong 已提交
1914 1915
//   return true;
// }
1916

1917
int32_t appendDownstream(SOperatorInfo* p, SOperatorInfo** pDownstream, int32_t num) {
H
Haojun Liao 已提交
1918
  if (p->pDownstream == NULL) {
H
Haojun Liao 已提交
1919
    assert(p->numOfDownstream == 0);
1920 1921
  }

wafwerar's avatar
wafwerar 已提交
1922
  p->pDownstream = taosMemoryCalloc(1, num * POINTER_BYTES);
1923 1924 1925 1926 1927 1928 1929
  if (p->pDownstream == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

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

wmmhello's avatar
wmmhello 已提交
1932
static void doDestroyTableList(STableListInfo* pTableqinfoList);
1933

1934
static void doTableQueryInfoTimeWindowCheck(SExecTaskInfo* pTaskInfo, STableQueryInfo* pTableQueryInfo, int32_t order) {
H
Haojun Liao 已提交
1935 1936
#if 0
    if (order == TSDB_ORDER_ASC) {
1937 1938
    assert(
        (pTableQueryInfo->win.skey <= pTableQueryInfo->win.ekey) &&
H
Haojun Liao 已提交
1939 1940
        (pTableQueryInfo->lastKey >= pTaskInfo->window.skey) &&
        (pTableQueryInfo->win.skey >= pTaskInfo->window.skey && pTableQueryInfo->win.ekey <= pTaskInfo->window.ekey));
1941 1942 1943
  } else {
    assert(
        (pTableQueryInfo->win.skey >= pTableQueryInfo->win.ekey) &&
H
Haojun Liao 已提交
1944 1945
        (pTableQueryInfo->lastKey <= pTaskInfo->window.skey) &&
        (pTableQueryInfo->win.skey <= pTaskInfo->window.skey && pTableQueryInfo->win.ekey >= pTaskInfo->window.ekey));
1946
  }
H
Haojun Liao 已提交
1947
#endif
1948 1949
}

1950 1951 1952 1953
typedef struct SFetchRspHandleWrapper {
  uint32_t exchangeId;
  int32_t  sourceIndex;
} SFetchRspHandleWrapper;
1954

D
dapan1121 已提交
1955
int32_t loadRemoteDataCallback(void* param, SDataBuf* pMsg, int32_t code) {
X
Xiaoyu Wang 已提交
1956
  SFetchRspHandleWrapper* pWrapper = (SFetchRspHandleWrapper*)param;
1957 1958 1959 1960 1961 1962 1963

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

X
Xiaoyu Wang 已提交
1964
  int32_t          index = pWrapper->sourceIndex;
1965
  SSourceDataInfo* pSourceDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, index);
1966

H
Haojun Liao 已提交
1967 1968
  if (code == TSDB_CODE_SUCCESS) {
    pSourceDataInfo->pRsp = pMsg->pData;
1969

H
Haojun Liao 已提交
1970 1971
    SRetrieveTableRsp* pRsp = pSourceDataInfo->pRsp;
    pRsp->numOfRows = htonl(pRsp->numOfRows);
dengyihao's avatar
dengyihao 已提交
1972
    pRsp->compLen = htonl(pRsp->compLen);
1973
    pRsp->numOfCols = htonl(pRsp->numOfCols);
dengyihao's avatar
dengyihao 已提交
1974
    pRsp->useconds = htobe64(pRsp->useconds);
1975

1976
    ASSERT(pRsp != NULL);
1977
    qDebug("%s fetch rsp received, index:%d, rows:%d", pSourceDataInfo->taskId, index, pRsp->numOfRows);
H
Haojun Liao 已提交
1978 1979 1980
  } else {
    pSourceDataInfo->code = code;
  }
H
Haojun Liao 已提交
1981

H
Haojun Liao 已提交
1982
  pSourceDataInfo->status = EX_SOURCE_DATA_READY;
1983 1984 1985 1986 1987

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

  taosMemoryFree(pWrapper);
wmmhello's avatar
wmmhello 已提交
1988
  return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
1989 1990 1991 1992
}

static void destroySendMsgInfo(SMsgSendInfo* pMsgBody) {
  assert(pMsgBody != NULL);
wafwerar's avatar
wafwerar 已提交
1993 1994
  taosMemoryFreeClear(pMsgBody->msgInfo.pData);
  taosMemoryFreeClear(pMsgBody);
H
Haojun Liao 已提交
1995 1996
}

S
Shengliang Guan 已提交
1997
void qProcessFetchRsp(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
S
Shengliang Guan 已提交
1998 1999
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
  assert(pMsg->info.ahandle != NULL);
H
Haojun Liao 已提交
2000 2001 2002 2003

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

  if (pMsg->contLen > 0) {
wafwerar's avatar
wafwerar 已提交
2004
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
H
Haojun Liao 已提交
2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
    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);
2016 2017
}

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

wafwerar's avatar
wafwerar 已提交
2021
  SResFetchReq* pMsg = taosMemoryCalloc(1, sizeof(SResFetchReq));
2022 2023 2024 2025
  if (NULL == pMsg) {
    pTaskInfo->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
    return pTaskInfo->code;
  }
2026

L
Liu Jicong 已提交
2027 2028
  SDownstreamSourceNode* pSource = taosArrayGet(pExchangeInfo->pSources, sourceIndex);
  SSourceDataInfo*       pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, sourceIndex);
2029

2030 2031
  ASSERT(pDataInfo->status == EX_SOURCE_DATA_NOT_READY);

2032 2033 2034
  qDebug("%s build fetch msg and send to vgId:%d, ep:%s, taskId:0x%" PRIx64 ", execId:%d, %d/%" PRIzu,
         GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->addr.epSet.eps[0].fqdn, pSource->taskId, pSource->execId,
         sourceIndex, totalSources);
2035 2036 2037 2038 2039

  pMsg->header.vgId = htonl(pSource->addr.nodeId);
  pMsg->sId = htobe64(pSource->schedId);
  pMsg->taskId = htobe64(pSource->taskId);
  pMsg->queryId = htobe64(pTaskInfo->id.queryId);
D
dapan1121 已提交
2040
  pMsg->execId = htonl(pSource->execId);
2041 2042

  // send the fetch remote task result reques
wafwerar's avatar
wafwerar 已提交
2043
  SMsgSendInfo* pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
2044
  if (NULL == pMsgSendInfo) {
wafwerar's avatar
wafwerar 已提交
2045
    taosMemoryFreeClear(pMsg);
2046 2047 2048
    qError("%s prepare message %d failed", GET_TASKID(pTaskInfo), (int32_t)sizeof(SMsgSendInfo));
    pTaskInfo->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
    return pTaskInfo->code;
H
Haojun Liao 已提交
2049 2050
  }

2051
  SFetchRspHandleWrapper* pWrapper = taosMemoryCalloc(1, sizeof(SFetchRspHandleWrapper));
X
Xiaoyu Wang 已提交
2052
  pWrapper->exchangeId = pExchangeInfo->self;
2053 2054 2055
  pWrapper->sourceIndex = sourceIndex;

  pMsgSendInfo->param = pWrapper;
2056 2057
  pMsgSendInfo->msgInfo.pData = pMsg;
  pMsgSendInfo->msgInfo.len = sizeof(SResFetchReq);
D
dapan1121 已提交
2058
  pMsgSendInfo->msgType = pSource->fetchMsgType;
2059
  pMsgSendInfo->fp = loadRemoteDataCallback;
2060

2061
  int64_t transporterId = 0;
L
Liu Jicong 已提交
2062
  int32_t code = asyncSendMsgToServer(pExchangeInfo->pTransporter, &pSource->addr.epSet, &transporterId, pMsgSendInfo);
2063 2064 2065
  return TSDB_CODE_SUCCESS;
}

2066
int32_t extractDataBlockFromFetchRsp(SSDataBlock* pRes, SLoadRemoteDataInfo* pLoadInfo, int32_t numOfRows, char* pData,
L
Liu Jicong 已提交
2067 2068
                                     int32_t compLen, int32_t numOfOutput, int64_t startTs, uint64_t* total,
                                     SArray* pColList) {
H
Haojun Liao 已提交
2069
  if (pColList == NULL) {  // data from other sources
2070
    blockDataCleanup(pRes);
2071
    //    blockDataEnsureCapacity(pRes, numOfRows);
2072
    blockDecode(pRes, numOfOutput, numOfRows, pData);
H
Haojun Liao 已提交
2073
  } else {  // extract data according to pColList
2074
    ASSERT(numOfOutput == taosArrayGetSize(pColList));
2075 2076 2077 2078 2079
    char* pStart = pData;

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

2080
    // todo refactor:extract method
2081
    SSysTableSchema* pSchema = (SSysTableSchema*)pStart;
dengyihao's avatar
dengyihao 已提交
2082
    for (int32_t i = 0; i < numOfCols; ++i) {
2083 2084 2085 2086 2087 2088 2089
      SSysTableSchema* p = (SSysTableSchema*)pStart;

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

2090
    SSDataBlock* pBlock = createDataBlock();
dengyihao's avatar
dengyihao 已提交
2091
    for (int32_t i = 0; i < numOfCols; ++i) {
2092 2093
      SColumnInfoData idata = createColumnInfoData(pSchema[i].type, pSchema[i].bytes, pSchema[i].colId);
      blockDataAppendColInfo(pBlock, &idata);
2094 2095
    }

2096
    blockDecode(pBlock, numOfCols, numOfRows, pStart);
2097 2098
    blockDataEnsureCapacity(pRes, numOfRows);

H
Haojun Liao 已提交
2099
    // data from mnode
2100
    pRes->info.rows = numOfRows;
2101 2102
    relocateColumnData(pRes, pColList, pBlock->pDataBlock, false);
    blockDataDestroy(pBlock);
2103
  }
2104

2105 2106
  // todo move this to time window aggregator, since the primary timestamp may not be known by exchange operator.
  blockDataUpdateTsWindow(pRes, 0);
2107

2108
  int64_t el = taosGetTimestampUs() - startTs;
2109

H
Haojun Liao 已提交
2110 2111
  pLoadInfo->totalRows += numOfRows;
  pLoadInfo->totalSize += compLen;
2112

H
Haojun Liao 已提交
2113 2114 2115
  if (total != NULL) {
    *total += numOfRows;
  }
2116

H
Haojun Liao 已提交
2117
  pLoadInfo->totalElapsed += el;
2118 2119
  return TSDB_CODE_SUCCESS;
}
2120

L
Liu Jicong 已提交
2121 2122
static void* setAllSourcesCompleted(SOperatorInfo* pOperator, int64_t startTs) {
  SExchangeInfo* pExchangeInfo = pOperator->info;
2123
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
H
Haojun Liao 已提交
2124

2125
  int64_t              el = taosGetTimestampUs() - startTs;
H
Haojun Liao 已提交
2126
  SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
2127

H
Haojun Liao 已提交
2128
  pLoadInfo->totalElapsed += el;
H
Haojun Liao 已提交
2129

2130
  size_t totalSources = taosArrayGetSize(pExchangeInfo->pSources);
L
Liu Jicong 已提交
2131 2132 2133
  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);
2134 2135 2136 2137 2138

  doSetOperatorCompleted(pOperator);
  return NULL;
}

L
Liu Jicong 已提交
2139 2140
static SSDataBlock* concurrentlyLoadRemoteDataImpl(SOperatorInfo* pOperator, SExchangeInfo* pExchangeInfo,
                                                   SExecTaskInfo* pTaskInfo) {
2141 2142 2143 2144 2145 2146 2147 2148
  int32_t code = 0;
  int64_t startTs = taosGetTimestampUs();
  size_t  totalSources = taosArrayGetSize(pExchangeInfo->pSources);

  while (1) {
    int32_t completed = 0;
    for (int32_t i = 0; i < totalSources; ++i) {
      SSourceDataInfo* pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, i);
2149
      if (pDataInfo->status == EX_SOURCE_DATA_EXHAUSTED) {
2150
        completed += 1;
H
Haojun Liao 已提交
2151 2152
        continue;
      }
2153

2154
      if (pDataInfo->status != EX_SOURCE_DATA_READY) {
2155 2156 2157
        continue;
      }

2158 2159 2160 2161 2162
      if (pDataInfo->code != TSDB_CODE_SUCCESS) {
        code = pDataInfo->code;
        goto _error;
      }

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

L
Liu Jicong 已提交
2166
      SSDataBlock*         pRes = pExchangeInfo->pResult;
H
Haojun Liao 已提交
2167
      SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
2168
      if (pRsp->numOfRows == 0) {
2169 2170
        qDebug("%s vgId:%d, taskId:0x%" PRIx64 " execId:%d index:%d completed, rowsOfSource:%" PRIu64
               ", totalRows:%" PRIu64 ", completed:%d try next %d/%" PRIzu,
D
dapan1121 已提交
2171
               GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->taskId, pSource->execId, i, pDataInfo->totalRows,
2172
               pExchangeInfo->loadInfo.totalRows, completed + 1, i + 1, totalSources);
2173
        pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
2174
        completed += 1;
D
dapan1121 已提交
2175
        taosMemoryFreeClear(pDataInfo->pRsp);
2176 2177
        continue;
      }
H
Haojun Liao 已提交
2178

H
Haojun Liao 已提交
2179
      SRetrieveTableRsp* pTableRsp = pDataInfo->pRsp;
L
Liu Jicong 已提交
2180 2181 2182
      code =
          extractDataBlockFromFetchRsp(pExchangeInfo->pResult, pLoadInfo, pTableRsp->numOfRows, pTableRsp->data,
                                       pTableRsp->compLen, pTableRsp->numOfCols, startTs, &pDataInfo->totalRows, NULL);
2183
      if (code != 0) {
2184
        taosMemoryFreeClear(pDataInfo->pRsp);
2185 2186 2187
        goto _error;
      }

2188
      if (pRsp->completed == 1) {
2189 2190
        qDebug("%s fetch msg rsp from vgId:%d, taskId:0x%" PRIx64
               " execId:%d"
X
Xiaoyu Wang 已提交
2191 2192
               " index:%d completed, numOfRows:%d, rowsOfSource:%" PRIu64 ", totalRows:%" PRIu64 ", totalBytes:%" PRIu64
               ", completed:%d try next %d/%" PRIzu,
2193 2194
               GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->taskId, pSource->execId, i, pRes->info.rows,
               pDataInfo->totalRows, pLoadInfo->totalRows, pLoadInfo->totalSize, completed + 1, i + 1, totalSources);
2195
        completed += 1;
2196
        pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
2197
      } else {
D
dapan1121 已提交
2198
        qDebug("%s fetch msg rsp from vgId:%d, taskId:0x%" PRIx64 " execId:%d numOfRows:%d, totalRows:%" PRIu64
dengyihao's avatar
dengyihao 已提交
2199
               ", totalBytes:%" PRIu64,
2200 2201
               GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->taskId, pSource->execId, pRes->info.rows,
               pLoadInfo->totalRows, pLoadInfo->totalSize);
2202 2203
      }

2204 2205
      taosMemoryFreeClear(pDataInfo->pRsp);

2206 2207
      if (pDataInfo->status != EX_SOURCE_DATA_EXHAUSTED) {
        pDataInfo->status = EX_SOURCE_DATA_NOT_READY;
2208 2209
        code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, i);
        if (code != TSDB_CODE_SUCCESS) {
2210
          taosMemoryFreeClear(pDataInfo->pRsp);
2211 2212 2213 2214 2215 2216 2217
          goto _error;
        }
      }

      return pExchangeInfo->pResult;
    }

2218
    if (completed == totalSources) {
2219 2220 2221 2222 2223 2224 2225 2226 2227
      return setAllSourcesCompleted(pOperator, startTs);
    }
  }

_error:
  pTaskInfo->code = code;
  return NULL;
}

L
Liu Jicong 已提交
2228 2229 2230
static int32_t prepareConcurrentlyLoad(SOperatorInfo* pOperator) {
  SExchangeInfo* pExchangeInfo = pOperator->info;
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
2231

L
Liu Jicong 已提交
2232
  size_t  totalSources = taosArrayGetSize(pExchangeInfo->pSources);
2233 2234 2235
  int64_t startTs = taosGetTimestampUs();

  // Asynchronously send all fetch requests to all sources.
L
Liu Jicong 已提交
2236
  for (int32_t i = 0; i < totalSources; ++i) {
2237 2238
    int32_t code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, i);
    if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
2239 2240
      pTaskInfo->code = code;
      return code;
2241 2242 2243 2244
    }
  }

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

2248
  pOperator->status = OP_RES_TO_RETURN;
H
Haojun Liao 已提交
2249
  pOperator->cost.openCost = taosGetTimestampUs() - startTs;
2250

2251
  tsem_wait(&pExchangeInfo->ready);
H
Haojun Liao 已提交
2252
  return TSDB_CODE_SUCCESS;
2253 2254
}

L
Liu Jicong 已提交
2255 2256 2257
static SSDataBlock* seqLoadRemoteData(SOperatorInfo* pOperator) {
  SExchangeInfo* pExchangeInfo = pOperator->info;
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
2258

L
Liu Jicong 已提交
2259
  size_t  totalSources = taosArrayGetSize(pExchangeInfo->pSources);
2260
  int64_t startTs = taosGetTimestampUs();
2261

L
Liu Jicong 已提交
2262
  while (1) {
2263 2264
    if (pExchangeInfo->current >= totalSources) {
      return setAllSourcesCompleted(pOperator, startTs);
2265
    }
2266

2267 2268 2269
    doSendFetchDataRequest(pExchangeInfo, pTaskInfo, pExchangeInfo->current);
    tsem_wait(&pExchangeInfo->ready);

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

H
Haojun Liao 已提交
2273
    if (pDataInfo->code != TSDB_CODE_SUCCESS) {
2274 2275
      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 已提交
2276 2277 2278 2279
      pOperator->pTaskInfo->code = pDataInfo->code;
      return NULL;
    }

L
Liu Jicong 已提交
2280
    SRetrieveTableRsp*   pRsp = pDataInfo->pRsp;
H
Haojun Liao 已提交
2281
    SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
2282
    if (pRsp->numOfRows == 0) {
2283 2284
      qDebug("%s vgId:%d, taskID:0x%" PRIx64 " execId:%d %d of total completed, rowsOfSource:%" PRIu64
             ", totalRows:%" PRIu64 " try next",
D
dapan1121 已提交
2285
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->taskId, pSource->execId, pExchangeInfo->current + 1,
H
Haojun Liao 已提交
2286
             pDataInfo->totalRows, pLoadInfo->totalRows);
H
Haojun Liao 已提交
2287

2288
      pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
2289
      pExchangeInfo->current += 1;
D
dapan1121 已提交
2290
      taosMemoryFreeClear(pDataInfo->pRsp);
2291 2292
      continue;
    }
H
Haojun Liao 已提交
2293

L
Liu Jicong 已提交
2294
    SSDataBlock*       pRes = pExchangeInfo->pResult;
H
Haojun Liao 已提交
2295
    SRetrieveTableRsp* pTableRsp = pDataInfo->pRsp;
L
Liu Jicong 已提交
2296
    int32_t            code =
2297
        extractDataBlockFromFetchRsp(pExchangeInfo->pResult, pLoadInfo, pTableRsp->numOfRows, pTableRsp->data,
L
Liu Jicong 已提交
2298
                                     pTableRsp->compLen, pTableRsp->numOfCols, startTs, &pDataInfo->totalRows, NULL);
2299 2300

    if (pRsp->completed == 1) {
D
dapan1121 已提交
2301
      qDebug("%s fetch msg rsp from vgId:%d, taskId:0x%" PRIx64 " execId:%d numOfRows:%d, rowsOfSource:%" PRIu64
L
Liu Jicong 已提交
2302
             ", totalRows:%" PRIu64 ", totalBytes:%" PRIu64 " try next %d/%" PRIzu,
2303 2304 2305
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->taskId, pSource->execId, pRes->info.rows,
             pDataInfo->totalRows, pLoadInfo->totalRows, pLoadInfo->totalSize, pExchangeInfo->current + 1,
             totalSources);
2306

2307
      pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
2308 2309
      pExchangeInfo->current += 1;
    } else {
D
dapan1121 已提交
2310
      qDebug("%s fetch msg rsp from vgId:%d, taskId:0x%" PRIx64 " execId:%d numOfRows:%d, totalRows:%" PRIu64
L
Liu Jicong 已提交
2311
             ", totalBytes:%" PRIu64,
2312 2313
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->taskId, pSource->execId, pRes->info.rows,
             pLoadInfo->totalRows, pLoadInfo->totalSize);
2314 2315
    }

2316
    pOperator->resultInfo.totalRows += pRes->info.rows;
2317
    taosMemoryFreeClear(pDataInfo->pRsp);
2318 2319
    return pExchangeInfo->pResult;
  }
2320 2321
}

L
Liu Jicong 已提交
2322
static int32_t prepareLoadRemoteData(SOperatorInfo* pOperator) {
2323
  if (OPTR_IS_OPENED(pOperator)) {
H
Haojun Liao 已提交
2324 2325 2326
    return TSDB_CODE_SUCCESS;
  }

2327 2328
  int64_t st = taosGetTimestampUs();

L
Liu Jicong 已提交
2329
  SExchangeInfo* pExchangeInfo = pOperator->info;
2330
  if (!pExchangeInfo->seqLoadData) {
H
Haojun Liao 已提交
2331 2332 2333 2334 2335 2336
    int32_t code = prepareConcurrentlyLoad(pOperator);
    if (code != TSDB_CODE_SUCCESS) {
      return code;
    }
  }

2337
  OPTR_SET_OPENED(pOperator);
2338
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
H
Haojun Liao 已提交
2339 2340 2341
  return TSDB_CODE_SUCCESS;
}

2342
static SSDataBlock* doLoadRemoteData(SOperatorInfo* pOperator) {
L
Liu Jicong 已提交
2343 2344
  SExchangeInfo* pExchangeInfo = pOperator->info;
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
2345

2346
  pTaskInfo->code = pOperator->fpSet._openFn(pOperator);
2347
  if (pTaskInfo->code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
2348 2349
    return NULL;
  }
2350

L
Liu Jicong 已提交
2351
  size_t               totalSources = taosArrayGetSize(pExchangeInfo->pSources);
H
Haojun Liao 已提交
2352
  SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
H
Haojun Liao 已提交
2353

2354
  if (pOperator->status == OP_EXEC_DONE) {
L
Liu Jicong 已提交
2355 2356 2357
    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);
2358 2359 2360 2361 2362 2363
    return NULL;
  }

  if (pExchangeInfo->seqLoadData) {
    return seqLoadRemoteData(pOperator);
  } else {
2364
    return concurrentlyLoadRemoteDataImpl(pOperator, pExchangeInfo, pTaskInfo);
2365
  }
H
Haojun Liao 已提交
2366
}
2367

2368
static int32_t initDataSource(int32_t numOfSources, SExchangeInfo* pInfo, const char* id) {
2369
  pInfo->pSourceDataInfo = taosArrayInit(numOfSources, sizeof(SSourceDataInfo));
H
Haojun Liao 已提交
2370 2371
  if (pInfo->pSourceDataInfo == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
2372 2373
  }

L
Liu Jicong 已提交
2374
  for (int32_t i = 0; i < numOfSources; ++i) {
2375
    SSourceDataInfo dataInfo = {0};
H
Haojun Liao 已提交
2376
    dataInfo.status = EX_SOURCE_DATA_NOT_READY;
2377
    dataInfo.taskId = id;
L
Liu Jicong 已提交
2378
    dataInfo.index = i;
X
Xiaoyu Wang 已提交
2379
    SSourceDataInfo* pDs = taosArrayPush(pInfo->pSourceDataInfo, &dataInfo);
2380
    if (pDs == NULL) {
H
Haojun Liao 已提交
2381 2382 2383 2384 2385 2386 2387 2388
      taosArrayDestroy(pInfo->pSourceDataInfo);
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }

  return TSDB_CODE_SUCCESS;
}

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

2392
  if (numOfSources == 0) {
X
Xiaoyu Wang 已提交
2393
    qError("%s invalid number: %d of sources in exchange operator", id, (int32_t)numOfSources);
2394 2395 2396
    return TSDB_CODE_INVALID_PARA;
  }

H
Haojun Liao 已提交
2397
  pInfo->pSources = taosArrayInit(numOfSources, sizeof(SDownstreamSourceNode));
wmmhello's avatar
wmmhello 已提交
2398
  if (pInfo->pSources == NULL) {
2399
    return TSDB_CODE_OUT_OF_MEMORY;
H
Haojun Liao 已提交
2400 2401
  }

L
Liu Jicong 已提交
2402
  for (int32_t i = 0; i < numOfSources; ++i) {
D
dapan1121 已提交
2403
    SDownstreamSourceNode* pNode = (SDownstreamSourceNode*)nodesListGetNode((SNodeList*)pExNode->pSrcEndPoints, i);
H
Haojun Liao 已提交
2404 2405
    taosArrayPush(pInfo->pSources, pNode);
  }
2406

2407 2408
  pInfo->self = taosAddRef(exchangeObjRefPool, pInfo);

2409
  return initDataSource(numOfSources, pInfo, id);
2410 2411 2412 2413 2414 2415
}

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 已提交
2416
    goto _error;
2417
  }
H
Haojun Liao 已提交
2418

2419
  int32_t code = initExchangeOperator(pExNode, pInfo, GET_TASKID(pTaskInfo));
2420 2421 2422
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
2423 2424

  tsem_init(&pInfo->ready, 0, 0);
2425

2426
  pInfo->seqLoadData = false;
2427
  pInfo->pTransporter = pTransporter;
2428 2429
  pInfo->pResult = createResDataBlock(pExNode->node.pOutputDataBlockDesc);
  pOperator->name = "ExchangeOperator";
X
Xiaoyu Wang 已提交
2430
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_EXCHANGE;
X
Xiaoyu Wang 已提交
2431
  pOperator->blocking = false;
2432 2433
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
2434
  pOperator->exprSupp.numOfExprs = taosArrayGetSize(pInfo->pResult->pDataBlock);
X
Xiaoyu Wang 已提交
2435
  pOperator->pTaskInfo = pTaskInfo;
2436

L
Liu Jicong 已提交
2437 2438
  pOperator->fpSet = createOperatorFpSet(prepareLoadRemoteData, doLoadRemoteData, NULL, NULL,
                                         destroyExchangeOperatorInfo, NULL, NULL, NULL);
2439
  return pOperator;
H
Haojun Liao 已提交
2440

L
Liu Jicong 已提交
2441
_error:
H
Haojun Liao 已提交
2442
  if (pInfo != NULL) {
2443
    doDestroyExchangeOperatorInfo(pInfo);
H
Haojun Liao 已提交
2444 2445
  }

wafwerar's avatar
wafwerar 已提交
2446 2447
  taosMemoryFreeClear(pInfo);
  taosMemoryFreeClear(pOperator);
2448
  pTaskInfo->code = code;
H
Haojun Liao 已提交
2449
  return NULL;
2450 2451
}

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

2455
static void destroySortedMergeOperatorInfo(void* param, int32_t numOfOutput) {
L
Liu Jicong 已提交
2456
  SSortedMergeOperatorInfo* pInfo = (SSortedMergeOperatorInfo*)param;
H
Haojun Liao 已提交
2457
  taosArrayDestroy(pInfo->pSortInfo);
2458 2459 2460
  taosArrayDestroy(pInfo->groupInfo);

  if (pInfo->pSortHandle != NULL) {
H
Haojun Liao 已提交
2461
    tsortDestroySortHandle(pInfo->pSortHandle);
2462 2463
  }

H
Haojun Liao 已提交
2464
  blockDataDestroy(pInfo->binfo.pRes);
H
Haojun Liao 已提交
2465
  cleanupAggSup(&pInfo->aggSup);
2466
}
H
Haojun Liao 已提交
2467

L
Liu Jicong 已提交
2468
static bool needToMerge(SSDataBlock* pBlock, SArray* groupInfo, char** buf, int32_t rowIndex) {
2469 2470 2471 2472
  size_t size = taosArrayGetSize(groupInfo);
  if (size == 0) {
    return true;
  }
2473

2474 2475
  for (int32_t i = 0; i < size; ++i) {
    int32_t* index = taosArrayGet(groupInfo, i);
2476

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

2480 2481 2482
    if ((isNull && buf[i] != NULL) || (!isNull && buf[i] == NULL)) {
      return false;
    }
2483

2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496
    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;
      }
2497 2498 2499
    }
  }

2500
  return 0;
2501 2502
}

L
Liu Jicong 已提交
2503 2504 2505
static void doMergeResultImpl(SSortedMergeOperatorInfo* pInfo, SqlFunctionCtx* pCtx, int32_t numOfExpr,
                              int32_t rowIndex) {
  for (int32_t j = 0; j < numOfExpr; ++j) {  // TODO set row index
X
Xiaoyu Wang 已提交
2506
                                             //    pCtx[j].startRow = rowIndex;
2507 2508
  }

2509 2510
  for (int32_t j = 0; j < numOfExpr; ++j) {
    int32_t functionId = pCtx[j].functionId;
L
Liu Jicong 已提交
2511 2512 2513 2514 2515 2516 2517 2518 2519
    //    pCtx[j].fpSet->addInput(&pCtx[j]);

    //    if (functionId < 0) {
    //      SUdfInfo* pUdfInfo = taosArrayGet(pInfo->udfInfo, -1 * functionId - 1);
    //      doInvokeUdf(pUdfInfo, &pCtx[j], 0, TSDB_UDF_FUNC_MERGE);
    //    } else {
    //      assert(!TSDB_FUNC_IS_SCALAR(functionId));
    //      aAggs[functionId].mergeFunc(&pCtx[j]);
    //    }
2520
  }
2521
}
2522

L
Liu Jicong 已提交
2523 2524
static void doFinalizeResultImpl(SqlFunctionCtx* pCtx, int32_t numOfExpr) {
  for (int32_t j = 0; j < numOfExpr; ++j) {
2525 2526 2527 2528
    int32_t functionId = pCtx[j].functionId;
    //    if (functionId == FUNC_TAG_DUMMY || functionId == FUNC_TS_DUMMY) {
    //      continue;
    //    }
2529

2530 2531 2532 2533
    //    if (functionId < 0) {
    //      SUdfInfo* pUdfInfo = taosArrayGet(pInfo->udfInfo, -1 * functionId - 1);
    //      doInvokeUdf(pUdfInfo, &pCtx[j], 0, TSDB_UDF_FUNC_FINALIZE);
    //    } else {
dengyihao's avatar
dengyihao 已提交
2534
    //    pCtx[j].fpSet.finalize(&pCtx[j]);
2535 2536
  }
}
2537

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

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

2545 2546 2547
    char* data = colDataGetData(pColInfo, rowIndex);
    memcpy(rowColData[i], data, colDataGetLength(pColInfo, rowIndex));
  }
2548

2549 2550
  return true;
}
2551

2552 2553
static void doMergeImpl(SOperatorInfo* pOperator, int32_t numOfExpr, SSDataBlock* pBlock) {
  SSortedMergeOperatorInfo* pInfo = pOperator->info;
2554

2555
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
2556

L
Liu Jicong 已提交
2557
  for (int32_t i = 0; i < pBlock->info.rows; ++i) {
2558 2559 2560 2561 2562 2563 2564 2565 2566
    if (!pInfo->hasGroupVal) {
      ASSERT(i == 0);
      doMergeResultImpl(pInfo, pCtx, numOfExpr, i);
      pInfo->hasGroupVal = saveCurrentTuple(pInfo->groupVal, pInfo->groupInfo, pBlock, i);
    } else {
      if (needToMerge(pBlock, pInfo->groupInfo, pInfo->groupVal, i)) {
        doMergeResultImpl(pInfo, pCtx, numOfExpr, i);
      } else {
        doFinalizeResultImpl(pCtx, numOfExpr);
2567 2568
        int32_t numOfRows = getNumOfResult(pOperator->exprSupp.pCtx, pOperator->exprSupp.numOfExprs, NULL);
        //        setTagValueForMultipleRows(pCtx, pOperator->exprSupp.numOfExprs, numOfRows);
2569

2570
        // TODO check for available buffer;
H
Haojun Liao 已提交
2571

2572 2573 2574 2575 2576
        // next group info data
        pInfo->binfo.pRes->info.rows += numOfRows;
        for (int32_t j = 0; j < numOfExpr; ++j) {
          if (pCtx[j].functionId < 0) {
            continue;
2577
          }
2578

H
Haojun Liao 已提交
2579
          pCtx[j].fpSet.process(&pCtx[j]);
2580
        }
2581 2582 2583

        doMergeResultImpl(pInfo, pCtx, numOfExpr, i);
        pInfo->hasGroupVal = saveCurrentTuple(pInfo->groupVal, pInfo->groupInfo, pBlock, i);
H
Haojun Liao 已提交
2584
      }
2585 2586 2587 2588
    }
  }
}

2589 2590
static SSDataBlock* doMerge(SOperatorInfo* pOperator) {
  SSortedMergeOperatorInfo* pInfo = pOperator->info;
L
Liu Jicong 已提交
2591
  SSortHandle*              pHandle = pInfo->pSortHandle;
2592

2593
  SSDataBlock* pDataBlock = createOneDataBlock(pInfo->binfo.pRes, false);
2594
  blockDataEnsureCapacity(pDataBlock, pOperator->resultInfo.capacity);
2595

L
Liu Jicong 已提交
2596
  while (1) {
2597
    blockDataCleanup(pDataBlock);
2598
    while (1) {
H
Haojun Liao 已提交
2599
      STupleHandle* pTupleHandle = tsortNextTuple(pHandle);
2600 2601
      if (pTupleHandle == NULL) {
        break;
2602
      }
2603

2604 2605
      // build datablock for merge for one group
      appendOneRowToDataBlock(pDataBlock, pTupleHandle);
2606
      if (pDataBlock->info.rows >= pOperator->resultInfo.capacity) {
2607 2608
        break;
      }
2609
    }
2610

2611 2612 2613
    if (pDataBlock->info.rows == 0) {
      break;
    }
2614

2615
    setInputDataBlock(pOperator, pOperator->exprSupp.pCtx, pDataBlock, TSDB_ORDER_ASC, MAIN_SCAN, true);
L
Liu Jicong 已提交
2616 2617
    //  updateOutputBuf(&pInfo->binfo, &pAggInfo->bufCapacity, pBlock->info.rows * pAggInfo->resultRowFactor,
    //  pOperator->pRuntimeEnv, true);
2618
    doMergeImpl(pOperator, pOperator->exprSupp.numOfExprs, pDataBlock);
2619 2620
    // flush to tuple store, and after all data have been handled, return to upstream node or sink node
  }
2621

2622 2623 2624
  doFinalizeResultImpl(pOperator->exprSupp.pCtx, pOperator->exprSupp.numOfExprs);
  int32_t numOfRows = getNumOfResult(pOperator->exprSupp.pCtx, pOperator->exprSupp.numOfExprs, NULL);
  //        setTagValueForMultipleRows(pCtx, pOperator->exprSupp.numOfExprs, numOfRows);
2625

2626
  // TODO check for available buffer;
2627

2628 2629
  // next group info data
  pInfo->binfo.pRes->info.rows += numOfRows;
L
Liu Jicong 已提交
2630
  return (pInfo->binfo.pRes->info.rows > 0) ? pInfo->binfo.pRes : NULL;
2631
}
2632

L
Liu Jicong 已提交
2633 2634
SSDataBlock* getSortedMergeBlockData(SSortHandle* pHandle, SSDataBlock* pDataBlock, int32_t capacity,
                                     SArray* pColMatchInfo, SSortedMergeOperatorInfo* pInfo) {
2635 2636 2637 2638 2639 2640 2641 2642 2643 2644
  blockDataCleanup(pDataBlock);

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

  blockDataEnsureCapacity(p, capacity);

  while (1) {
2645
    STupleHandle* pTupleHandle = tsortNextTuple(pHandle);
2646 2647 2648 2649
    if (pTupleHandle == NULL) {
      break;
    }

2650
    appendOneRowToDataBlock(p, pTupleHandle);
2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663
    if (p->info.rows >= capacity) {
      break;
    }
  }

  if (p->info.rows > 0) {
    int32_t numOfCols = taosArrayGetSize(pColMatchInfo);
    for (int32_t i = 0; i < numOfCols; ++i) {
      SColMatchInfo* pmInfo = taosArrayGet(pColMatchInfo, i);
      ASSERT(pmInfo->matchType == COL_MATCH_FROM_SLOT_ID);

      SColumnInfoData* pSrc = taosArrayGet(p->pDataBlock, pmInfo->srcSlotId);
      SColumnInfoData* pDst = taosArrayGet(pDataBlock->pDataBlock, pmInfo->targetSlotId);
2664
      colDataAssign(pDst, pSrc, p->info.rows, &pDataBlock->info);
2665 2666 2667 2668 2669 2670 2671 2672 2673 2674
    }

    pDataBlock->info.rows = p->info.rows;
    pDataBlock->info.capacity = p->info.rows;
  }

  blockDataDestroy(p);
  return (pDataBlock->info.rows > 0) ? pDataBlock : NULL;
}

2675
static SSDataBlock* doSortedMerge(SOperatorInfo* pOperator) {
2676 2677
  if (pOperator->status == OP_EXEC_DONE) {
    return NULL;
2678 2679
  }

L
Liu Jicong 已提交
2680
  SExecTaskInfo*            pTaskInfo = pOperator->pTaskInfo;
2681
  SSortedMergeOperatorInfo* pInfo = pOperator->info;
H
Haojun Liao 已提交
2682
  if (pOperator->status == OP_RES_TO_RETURN) {
2683
    return getSortedMergeBlockData(pInfo->pSortHandle, pInfo->binfo.pRes, pOperator->resultInfo.capacity, NULL, pInfo);
2684 2685
  }

2686
  int32_t numOfBufPage = pInfo->sortBufSize / pInfo->bufPageSize;
L
Liu Jicong 已提交
2687 2688
  pInfo->pSortHandle = tsortCreateSortHandle(pInfo->pSortInfo, SORT_MULTISOURCE_MERGE, pInfo->bufPageSize, numOfBufPage,
                                             pInfo->binfo.pRes, "GET_TASKID(pTaskInfo)");
H
Haojun Liao 已提交
2689

2690
  tsortSetFetchRawDataFp(pInfo->pSortHandle, loadNextDataBlock, NULL, NULL);
2691

L
Liu Jicong 已提交
2692
  for (int32_t i = 0; i < pOperator->numOfDownstream; ++i) {
wmmhello's avatar
wmmhello 已提交
2693
    SSortSource* ps = taosMemoryCalloc(1, sizeof(SSortSource));
H
Haojun Liao 已提交
2694
    ps->param = pOperator->pDownstream[i];
H
Haojun Liao 已提交
2695
    tsortAddSource(pInfo->pSortHandle, ps);
2696 2697
  }

H
Haojun Liao 已提交
2698
  int32_t code = tsortOpen(pInfo->pSortHandle);
2699
  if (code != TSDB_CODE_SUCCESS) {
2700
    longjmp(pTaskInfo->env, terrno);
2701 2702
  }

H
Haojun Liao 已提交
2703
  pOperator->status = OP_RES_TO_RETURN;
2704
  return doMerge(pOperator);
2705
}
2706

L
Liu Jicong 已提交
2707 2708
static int32_t initGroupCol(SExprInfo* pExprInfo, int32_t numOfCols, SArray* pGroupInfo,
                            SSortedMergeOperatorInfo* pInfo) {
2709 2710
  if (pGroupInfo == NULL || taosArrayGetSize(pGroupInfo) == 0) {
    return 0;
H
Haojun Liao 已提交
2711 2712
  }

2713 2714 2715 2716 2717 2718 2719 2720
  int32_t len = 0;
  SArray* plist = taosArrayInit(3, sizeof(SColumn));
  pInfo->groupInfo = taosArrayInit(3, sizeof(int32_t));

  if (plist == NULL || pInfo->groupInfo == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

L
Liu Jicong 已提交
2721 2722
  size_t numOfGroupCol = taosArrayGetSize(pInfo->groupInfo);
  for (int32_t i = 0; i < numOfGroupCol; ++i) {
2723
    SColumn* pCol = taosArrayGet(pGroupInfo, i);
L
Liu Jicong 已提交
2724
    for (int32_t j = 0; j < numOfCols; ++j) {
H
Haojun Liao 已提交
2725
      SExprInfo* pe = &pExprInfo[j];
2726
      if (pe->base.resSchema.slotId == pCol->colId) {
2727 2728
        taosArrayPush(plist, pCol);
        taosArrayPush(pInfo->groupInfo, &j);
H
Haojun Liao 已提交
2729
        len += pCol->bytes;
2730 2731
        break;
      }
H
Haojun Liao 已提交
2732 2733 2734
    }
  }

2735
  ASSERT(taosArrayGetSize(pGroupInfo) == taosArrayGetSize(plist));
H
Haojun Liao 已提交
2736

wafwerar's avatar
wafwerar 已提交
2737
  pInfo->groupVal = taosMemoryCalloc(1, (POINTER_BYTES * numOfGroupCol + len));
2738 2739 2740 2741
  if (pInfo->groupVal == NULL) {
    taosArrayDestroy(plist);
    return TSDB_CODE_OUT_OF_MEMORY;
  }
H
Haojun Liao 已提交
2742

2743
  int32_t offset = 0;
L
Liu Jicong 已提交
2744 2745
  char*   start = (char*)(pInfo->groupVal + (POINTER_BYTES * numOfGroupCol));
  for (int32_t i = 0; i < numOfGroupCol; ++i) {
2746 2747
    pInfo->groupVal[i] = start + offset;
    SColumn* pCol = taosArrayGet(plist, i);
H
Haojun Liao 已提交
2748
    offset += pCol->bytes;
2749
  }
H
Haojun Liao 已提交
2750

2751
  taosArrayDestroy(plist);
H
Haojun Liao 已提交
2752

2753 2754
  return TSDB_CODE_SUCCESS;
}
H
Haojun Liao 已提交
2755

L
Liu Jicong 已提交
2756 2757 2758
SOperatorInfo* createSortedMergeOperatorInfo(SOperatorInfo** downstream, int32_t numOfDownstream, SExprInfo* pExprInfo,
                                             int32_t num, SArray* pSortInfo, SArray* pGroupInfo,
                                             SExecTaskInfo* pTaskInfo) {
wafwerar's avatar
wafwerar 已提交
2759
  SSortedMergeOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SSortedMergeOperatorInfo));
L
Liu Jicong 已提交
2760
  SOperatorInfo*            pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
2761
  if (pInfo == NULL || pOperator == NULL) {
2762
    goto _error;
2763
  }
H
Haojun Liao 已提交
2764

2765 2766 2767 2768 2769
  int32_t code = initExprSupp(&pOperator->exprSupp, pExprInfo, num);
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }

2770
  initResultRowInfo(&pInfo->binfo.resultRowInfo);
H
Haojun Liao 已提交
2771

2772
  if (pOperator->exprSupp.pCtx == NULL || pInfo->binfo.pRes == NULL) {
2773 2774
    goto _error;
  }
H
Haojun Liao 已提交
2775

2776
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
2777
  code = doInitAggInfoSup(&pInfo->aggSup, pOperator->exprSupp.pCtx, num, keyBufSize, pTaskInfo->id.str);
2778 2779 2780
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
H
Haojun Liao 已提交
2781

2782
  setFunctionResultOutput(pOperator, &pInfo->binfo, &pInfo->aggSup, MAIN_SCAN, num);
H
Haojun Liao 已提交
2783
  code = initGroupCol(pExprInfo, num, pGroupInfo, pInfo);
2784 2785 2786
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
H
Haojun Liao 已提交
2787

L
Liu Jicong 已提交
2788 2789 2790 2791 2792
  //  pInfo->resultRowFactor = (int32_t)(getRowNumForMultioutput(pRuntimeEnv->pQueryAttr,
  //      pRuntimeEnv->pQueryAttr->topBotQuery, false));
  pInfo->sortBufSize = 1024 * 16;  // 1MB
  pInfo->bufPageSize = 1024;
  pInfo->pSortInfo = pSortInfo;
H
Haojun Liao 已提交
2793

2794
  pOperator->resultInfo.capacity = blockDataGetCapacityInRow(pInfo->binfo.pRes, pInfo->bufPageSize);
H
Haojun Liao 已提交
2795

L
Liu Jicong 已提交
2796
  pOperator->name = "SortedMerge";
X
Xiaoyu Wang 已提交
2797
  // pOperator->operatorType = OP_SortedMerge;
2798 2799 2800
  pOperator->blocking = true;
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
L
Liu Jicong 已提交
2801
  pOperator->pTaskInfo = pTaskInfo;
H
Haojun Liao 已提交
2802

2803 2804
  pOperator->fpSet = createOperatorFpSet(operatorDummyOpenFn, doSortedMerge, NULL, NULL, destroySortedMergeOperatorInfo,
                                         NULL, NULL, NULL);
2805 2806 2807
  code = appendDownstream(pOperator, downstream, numOfDownstream);
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
2808
  }
H
Haojun Liao 已提交
2809

2810
  return pOperator;
H
Haojun Liao 已提交
2811

L
Liu Jicong 已提交
2812
_error:
2813
  if (pInfo != NULL) {
H
Haojun Liao 已提交
2814
    destroySortedMergeOperatorInfo(pInfo, num);
H
Haojun Liao 已提交
2815 2816
  }

wafwerar's avatar
wafwerar 已提交
2817 2818
  taosMemoryFreeClear(pInfo);
  taosMemoryFreeClear(pOperator);
2819 2820
  terrno = TSDB_CODE_QRY_OUT_OF_MEMORY;
  return NULL;
H
Haojun Liao 已提交
2821 2822
}

X
Xiaoyu Wang 已提交
2823
int32_t getTableScanInfo(SOperatorInfo* pOperator, int32_t* order, int32_t* scanFlag) {
2824
  // todo add more information about exchange operation
2825
  int32_t type = pOperator->operatorType;
X
Xiaoyu Wang 已提交
2826
  if (type == QUERY_NODE_PHYSICAL_PLAN_EXCHANGE || type == QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN ||
2827
      type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN || type == QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN ||
2828
      type == QUERY_NODE_PHYSICAL_PLAN_BLOCK_DIST_SCAN || type == QUERY_NODE_PHYSICAL_PLAN_LAST_ROW_SCAN) {
2829 2830 2831
    *order = TSDB_ORDER_ASC;
    *scanFlag = MAIN_SCAN;
    return TSDB_CODE_SUCCESS;
2832
  } else if (type == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
2833 2834 2835 2836 2837
    STableScanInfo* pTableScanInfo = pOperator->info;
    *order = pTableScanInfo->cond.order;
    *scanFlag = pTableScanInfo->scanFlag;
    return TSDB_CODE_SUCCESS;
  } else {
H
Haojun Liao 已提交
2838
    if (pOperator->pDownstream == NULL || pOperator->pDownstream[0] == NULL) {
2839
      return TSDB_CODE_INVALID_PARA;
H
Haojun Liao 已提交
2840
    } else {
2841
      return getTableScanInfo(pOperator->pDownstream[0], order, scanFlag);
2842 2843 2844
    }
  }
}
2845

L
Liu Jicong 已提交
2846 2847
int32_t doPrepareScan(SOperatorInfo* pOperator, uint64_t uid, int64_t ts) {
  int32_t type = pOperator->operatorType;
2848 2849 2850

  pOperator->status = OP_OPENED;

L
Liu Jicong 已提交
2851 2852
  if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
    SStreamBlockScanInfo* pScanInfo = pOperator->info;
2853 2854
    pScanInfo->blockType = STREAM_INPUT__DATA_SCAN;

2855 2856
    pScanInfo->pSnapshotReadOp->status = OP_OPENED;

2857
    STableScanInfo* pInfo = pScanInfo->pSnapshotReadOp->info;
2858 2859
    ASSERT(pInfo->scanMode == TABLE_SCAN__TABLE_ORDER);

L
Liu Jicong 已提交
2860 2861 2862 2863
    if (uid == 0) {
      pInfo->noTable = 1;
      return TSDB_CODE_SUCCESS;
    }
2864 2865 2866 2867 2868 2869

    /*if (pSnapShotScanInfo->dataReader == NULL) {*/
    /*pSnapShotScanInfo->dataReader = tsdbReaderOpen(pHandle->vnode, &pSTInfo->cond, tableList, 0, 0);*/
    /*pSnapShotScanInfo->scanMode = TABLE_SCAN__TABLE_ORDER;*/
    /*}*/

L
Liu Jicong 已提交
2870 2871
    pInfo->noTable = 0;

2872
    if (pInfo->lastStatus.uid != uid || pInfo->lastStatus.ts != ts) {
L
Liu Jicong 已提交
2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883
      SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;

      int32_t tableSz = taosArrayGetSize(pTaskInfo->tableqinfoList.pTableList);
      bool    found = false;
      for (int32_t i = 0; i < tableSz; i++) {
        STableKeyInfo* pTableInfo = taosArrayGet(pTaskInfo->tableqinfoList.pTableList, i);
        if (pTableInfo->uid == uid) {
          found = true;
          pInfo->currentTable = i;
        }
      }
2884
      // TODO after processing drop, found can be false
L
Liu Jicong 已提交
2885
      ASSERT(found);
2886 2887 2888 2889

      tsdbSetTableId(pInfo->dataReader, uid);
      int64_t oldSkey = pInfo->cond.twindows[0].skey;
      pInfo->cond.twindows[0].skey = ts + 1;
H
Haojun Liao 已提交
2890
      tsdbReaderReset(pInfo->dataReader, &pInfo->cond, 0);
2891 2892 2893 2894
      pInfo->cond.twindows[0].skey = oldSkey;
      pInfo->scanTimes = 0;
      pInfo->curTWinIdx = 0;

L
Liu Jicong 已提交
2895 2896
      qDebug("tsdb reader offset seek to uid %ld ts %ld, table cur set to %d , all table num %d", uid, ts,
             pInfo->currentTable, tableSz);
L
Liu Jicong 已提交
2897
    }
L
Liu Jicong 已提交
2898

L
Liu Jicong 已提交
2899
    return TSDB_CODE_SUCCESS;
2900

L
Liu Jicong 已提交
2901
  } else {
2902 2903 2904 2905 2906
    if (pOperator->numOfDownstream == 1) {
      return doPrepareScan(pOperator->pDownstream[0], uid, ts);
    } else if (pOperator->numOfDownstream == 0) {
      qError("failed to find stream scan operator to set the input data block");
      return TSDB_CODE_QRY_APP_ERROR;
L
Liu Jicong 已提交
2907
    } else {
2908 2909
      qError("join not supported for stream block scan");
      return TSDB_CODE_QRY_APP_ERROR;
L
Liu Jicong 已提交
2910 2911 2912 2913
    }
  }
}

2914 2915 2916 2917
int32_t doGetScanStatus(SOperatorInfo* pOperator, uint64_t* uid, int64_t* ts) {
  int32_t type = pOperator->operatorType;
  if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
    SStreamBlockScanInfo* pScanInfo = pOperator->info;
L
Liu Jicong 已提交
2918 2919 2920
    STableScanInfo*       pSnapShotScanInfo = pScanInfo->pSnapshotReadOp->info;
    *uid = pSnapShotScanInfo->lastStatus.uid;
    *ts = pSnapShotScanInfo->lastStatus.ts;
2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931
  } else {
    if (pOperator->pDownstream[0] == NULL) {
      return TSDB_CODE_INVALID_PARA;
    } else {
      doGetScanStatus(pOperator->pDownstream[0], uid, ts);
    }
  }

  return TSDB_CODE_SUCCESS;
}

2932
// this is a blocking operator
L
Liu Jicong 已提交
2933
static int32_t doOpenAggregateOptr(SOperatorInfo* pOperator) {
H
Haojun Liao 已提交
2934 2935
  if (OPTR_IS_OPENED(pOperator)) {
    return TSDB_CODE_SUCCESS;
2936 2937
  }

H
Haojun Liao 已提交
2938
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
2939
  SAggOperatorInfo* pAggInfo = pOperator->info;
H
Haojun Liao 已提交
2940

2941 2942
  SExprSupp*     pSup = &pOperator->exprSupp;
  SOperatorInfo* downstream = pOperator->pDownstream[0];
2943

2944 2945
  int64_t st = taosGetTimestampUs();

2946 2947 2948
  int32_t order = TSDB_ORDER_ASC;
  int32_t scanFlag = MAIN_SCAN;

H
Haojun Liao 已提交
2949
  while (1) {
2950
    SSDataBlock* pBlock = downstream->fpSet.getNextFn(downstream);
2951 2952 2953 2954
    if (pBlock == NULL) {
      break;
    }

2955 2956 2957 2958
    int32_t code = getTableScanInfo(pOperator, &order, &scanFlag);
    if (code != TSDB_CODE_SUCCESS) {
      longjmp(pTaskInfo->env, code);
    }
2959

2960
    // there is an scalar expression that needs to be calculated before apply the group aggregation.
2961 2962 2963
    if (pAggInfo->scalarExprSup.pExprInfo != NULL) {
      SExprSupp* pSup1 = &pAggInfo->scalarExprSup;
      code = projectApplyFunctions(pSup1->pExprInfo, pBlock, pBlock, pSup1->pCtx, pSup1->numOfExprs, NULL);
2964
      if (code != TSDB_CODE_SUCCESS) {
2965
        longjmp(pTaskInfo->env, code);
2966
      }
2967 2968
    }

2969
    // the pDataBlock are always the same one, no need to call this again
2970 2971 2972
    setExecutionContext(pOperator, pOperator->exprSupp.numOfExprs, pBlock->info.groupId, pAggInfo);
    setInputDataBlock(pOperator, pSup->pCtx, pBlock, order, scanFlag, true);
    code = doAggregateImpl(pOperator, 0, pSup->pCtx);
2973 2974 2975
    if (code != 0) {
      longjmp(pTaskInfo->env, code);
    }
2976

dengyihao's avatar
dengyihao 已提交
2977
#if 0  // test for encode/decode result info
2978
    if(pOperator->fpSet.encodeResultRow){
2979 2980
      char *result = NULL;
      int32_t length = 0;
2981 2982
      pOperator->fpSet.encodeResultRow(pOperator, &result, &length);
      SAggSupporter* pSup = &pAggInfo->aggSup;
2983 2984
      taosHashClear(pSup->pResultRowHashTable);
      pInfo->resultRowInfo.size = 0;
2985
      pOperator->fpSet.decodeResultRow(pOperator, result);
2986 2987 2988
      if(result){
        taosMemoryFree(result);
      }
2989
    }
2990
#endif
2991 2992
  }

H
Haojun Liao 已提交
2993
  closeAllResultRows(&pAggInfo->binfo.resultRowInfo);
2994
  initGroupedResultInfo(&pAggInfo->groupResInfo, pAggInfo->aggSup.pResultRowHashTable, 0);
H
Haojun Liao 已提交
2995
  OPTR_SET_OPENED(pOperator);
2996

2997
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
H
Haojun Liao 已提交
2998 2999 3000
  return TSDB_CODE_SUCCESS;
}

3001
static SSDataBlock* getAggregateResult(SOperatorInfo* pOperator) {
L
Liu Jicong 已提交
3002
  SAggOperatorInfo* pAggInfo = pOperator->info;
H
Haojun Liao 已提交
3003 3004 3005 3006 3007 3008
  SOptrBasicInfo*   pInfo = &pAggInfo->binfo;

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

L
Liu Jicong 已提交
3009
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3010
  pTaskInfo->code = pOperator->fpSet._openFn(pOperator);
H
Haojun Liao 已提交
3011
  if (pTaskInfo->code != TSDB_CODE_SUCCESS) {
3012
    doSetOperatorCompleted(pOperator);
H
Haojun Liao 已提交
3013 3014 3015
    return NULL;
  }

H
Haojun Liao 已提交
3016
  blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
S
slzhou 已提交
3017 3018 3019 3020 3021 3022 3023 3024
  while (1) {
    doBuildResultDatablock(pOperator, pInfo, &pAggInfo->groupResInfo, pAggInfo->aggSup.pResultBuf);
    doFilter(pAggInfo->pCondition, pInfo->pRes);

    if (!hasDataInGroupInfo(&pAggInfo->groupResInfo)) {
      doSetOperatorCompleted(pOperator);
      break;
    }
3025

S
slzhou 已提交
3026 3027 3028 3029
    if (pInfo->pRes->info.rows > 0) {
      break;
    }
  }
3030
  size_t rows = blockDataGetNumOfRows(pInfo->pRes);
3031 3032
  pOperator->resultInfo.totalRows += rows;

3033
  return (rows == 0) ? NULL : pInfo->pRes;
3034 3035
}

wmmhello's avatar
wmmhello 已提交
3036
int32_t aggEncodeResultRow(SOperatorInfo* pOperator, char** result, int32_t* length) {
3037
  if (result == NULL || length == NULL) {
wmmhello's avatar
wmmhello 已提交
3038 3039 3040
    return TSDB_CODE_TSC_INVALID_INPUT;
  }
  SOptrBasicInfo* pInfo = (SOptrBasicInfo*)(pOperator->info);
3041 3042 3043 3044 3045
  SAggSupporter*  pSup = (SAggSupporter*)POINTER_SHIFT(pOperator->info, sizeof(SOptrBasicInfo));
  int32_t         size = taosHashGetSize(pSup->pResultRowHashTable);
  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 已提交
3046

C
Cary Xu 已提交
3047 3048 3049 3050 3051 3052
  // no result
  if (getTotalBufSize(pSup->pResultBuf) == 0) {
    *result = NULL;
    *length = 0;
    return TSDB_CODE_SUCCESS;
  }
3053

wmmhello's avatar
wmmhello 已提交
3054
  *result = (char*)taosMemoryCalloc(1, totalSize);
L
Liu Jicong 已提交
3055
  if (*result == NULL) {
wmmhello's avatar
wmmhello 已提交
3056
    return TSDB_CODE_OUT_OF_MEMORY;
wmmhello's avatar
wmmhello 已提交
3057
  }
wmmhello's avatar
wmmhello 已提交
3058

wmmhello's avatar
wmmhello 已提交
3059
  int32_t offset = sizeof(int32_t);
wmmhello's avatar
wmmhello 已提交
3060 3061
  *(int32_t*)(*result + offset) = size;
  offset += sizeof(int32_t);
3062 3063

  // prepare memory
3064
  SResultRowPosition* pos = &pInfo->resultRowInfo.cur;
dengyihao's avatar
dengyihao 已提交
3065 3066
  void*               pPage = getBufPage(pSup->pResultBuf, pos->pageId);
  SResultRow*         pRow = (SResultRow*)((char*)pPage + pos->offset);
3067 3068 3069
  setBufPageDirty(pPage, true);
  releaseBufPage(pSup->pResultBuf, pPage);

dengyihao's avatar
dengyihao 已提交
3070
  void* pIter = taosHashIterate(pSup->pResultRowHashTable, NULL);
wmmhello's avatar
wmmhello 已提交
3071
  while (pIter) {
dengyihao's avatar
dengyihao 已提交
3072
    void*               key = taosHashGetKey(pIter, &keyLen);
3073
    SResultRowPosition* p1 = (SResultRowPosition*)pIter;
3074

dengyihao's avatar
dengyihao 已提交
3075
    pPage = (SFilePage*)getBufPage(pSup->pResultBuf, p1->pageId);
3076
    pRow = (SResultRow*)((char*)pPage + p1->offset);
3077 3078
    setBufPageDirty(pPage, true);
    releaseBufPage(pSup->pResultBuf, pPage);
wmmhello's avatar
wmmhello 已提交
3079 3080 3081

    // recalculate the result size
    int32_t realTotalSize = offset + sizeof(int32_t) + keyLen + sizeof(int32_t) + pSup->resultRowSize;
L
Liu Jicong 已提交
3082
    if (realTotalSize > totalSize) {
wmmhello's avatar
wmmhello 已提交
3083
      char* tmp = (char*)taosMemoryRealloc(*result, realTotalSize);
L
Liu Jicong 已提交
3084
      if (tmp == NULL) {
wafwerar's avatar
wafwerar 已提交
3085
        taosMemoryFree(*result);
wmmhello's avatar
wmmhello 已提交
3086
        *result = NULL;
wmmhello's avatar
wmmhello 已提交
3087
        return TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
3088
      } else {
wmmhello's avatar
wmmhello 已提交
3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100
        *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);
3101
    memcpy(*result + offset, pRow, pSup->resultRowSize);
wmmhello's avatar
wmmhello 已提交
3102 3103 3104 3105 3106
    offset += pSup->resultRowSize;

    pIter = taosHashIterate(pSup->pResultRowHashTable, pIter);
  }

wmmhello's avatar
wmmhello 已提交
3107 3108 3109 3110
  *(int32_t*)(*result) = offset;
  *length = offset;

  return TDB_CODE_SUCCESS;
wmmhello's avatar
wmmhello 已提交
3111 3112
}

3113
int32_t aggDecodeResultRow(SOperatorInfo* pOperator, char* result) {
3114
  if (result == NULL) {
wmmhello's avatar
wmmhello 已提交
3115
    return TSDB_CODE_TSC_INVALID_INPUT;
wmmhello's avatar
wmmhello 已提交
3116
  }
wmmhello's avatar
wmmhello 已提交
3117
  SOptrBasicInfo* pInfo = (SOptrBasicInfo*)(pOperator->info);
3118
  SAggSupporter*  pSup = (SAggSupporter*)POINTER_SHIFT(pOperator->info, sizeof(SOptrBasicInfo));
wmmhello's avatar
wmmhello 已提交
3119 3120

  //  int32_t size = taosHashGetSize(pSup->pResultRowHashTable);
3121
  int32_t length = *(int32_t*)(result);
wmmhello's avatar
wmmhello 已提交
3122
  int32_t offset = sizeof(int32_t);
3123 3124 3125 3126

  int32_t count = *(int32_t*)(result + offset);
  offset += sizeof(int32_t);

L
Liu Jicong 已提交
3127
  while (count-- > 0 && length > offset) {
wmmhello's avatar
wmmhello 已提交
3128 3129 3130
    int32_t keyLen = *(int32_t*)(result + offset);
    offset += sizeof(int32_t);

L
Liu Jicong 已提交
3131
    uint64_t    tableGroupId = *(uint64_t*)(result + offset);
3132
    SResultRow* resultRow = getNewResultRow(pSup->pResultBuf, tableGroupId, pSup->resultRowSize);
L
Liu Jicong 已提交
3133
    if (!resultRow) {
wmmhello's avatar
wmmhello 已提交
3134
      return TSDB_CODE_TSC_INVALID_INPUT;
wmmhello's avatar
wmmhello 已提交
3135
    }
3136

wmmhello's avatar
wmmhello 已提交
3137
    // add a new result set for a new group
3138 3139
    SResultRowPosition pos = {.pageId = resultRow->pageId, .offset = resultRow->offset};
    taosHashPut(pSup->pResultRowHashTable, result + offset, keyLen, &pos, sizeof(SResultRowPosition));
wmmhello's avatar
wmmhello 已提交
3140 3141 3142

    offset += keyLen;
    int32_t valueLen = *(int32_t*)(result + offset);
L
Liu Jicong 已提交
3143
    if (valueLen != pSup->resultRowSize) {
wmmhello's avatar
wmmhello 已提交
3144
      return TSDB_CODE_TSC_INVALID_INPUT;
wmmhello's avatar
wmmhello 已提交
3145 3146 3147 3148 3149 3150 3151 3152 3153 3154
    }
    offset += sizeof(int32_t);
    int32_t pageId = resultRow->pageId;
    int32_t pOffset = resultRow->offset;
    memcpy(resultRow, result + offset, valueLen);
    resultRow->pageId = pageId;
    resultRow->offset = pOffset;
    offset += valueLen;

    initResultRow(resultRow);
dengyihao's avatar
dengyihao 已提交
3155
    pInfo->resultRowInfo.cur = (SResultRowPosition){.pageId = resultRow->pageId, .offset = resultRow->offset};
wmmhello's avatar
wmmhello 已提交
3156 3157
  }

L
Liu Jicong 已提交
3158
  if (offset != length) {
wmmhello's avatar
wmmhello 已提交
3159
    return TSDB_CODE_TSC_INVALID_INPUT;
wmmhello's avatar
wmmhello 已提交
3160
  }
wmmhello's avatar
wmmhello 已提交
3161
  return TDB_CODE_SUCCESS;
wmmhello's avatar
wmmhello 已提交
3162 3163
}

3164 3165
enum {
  PROJECT_RETRIEVE_CONTINUE = 0x1,
L
Liu Jicong 已提交
3166
  PROJECT_RETRIEVE_DONE = 0x2,
3167 3168 3169 3170 3171
};

static int32_t handleLimitOffset(SOperatorInfo* pOperator, SSDataBlock* pBlock) {
  SProjectOperatorInfo* pProjectInfo = pOperator->info;
  SOptrBasicInfo*       pInfo = &pProjectInfo->binfo;
L
Liu Jicong 已提交
3172
  SSDataBlock*          pRes = pInfo->pRes;
3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220

  if (pProjectInfo->curSOffset > 0) {
    if (pProjectInfo->groupId == 0) {  // it is the first group
      pProjectInfo->groupId = pBlock->info.groupId;
      blockDataCleanup(pInfo->pRes);
      return PROJECT_RETRIEVE_CONTINUE;
    } else if (pProjectInfo->groupId != pBlock->info.groupId) {
      pProjectInfo->curSOffset -= 1;

      // ignore data block in current group
      if (pProjectInfo->curSOffset > 0) {
        blockDataCleanup(pInfo->pRes);
        return PROJECT_RETRIEVE_CONTINUE;
      }
    }

    // set current group id of the project operator
    pProjectInfo->groupId = pBlock->info.groupId;
  }

  if (pProjectInfo->groupId != 0 && pProjectInfo->groupId != pBlock->info.groupId) {
    pProjectInfo->curGroupOutput += 1;
    if ((pProjectInfo->slimit.limit > 0) && (pProjectInfo->slimit.limit <= pProjectInfo->curGroupOutput)) {
      pOperator->status = OP_EXEC_DONE;
      blockDataCleanup(pRes);

      return PROJECT_RETRIEVE_DONE;
    }

    // reset the value for a new group data
    pProjectInfo->curOffset = 0;
    pProjectInfo->curOutput = 0;
  }

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

  // set current group id
  pProjectInfo->groupId = pBlock->info.groupId;

  if (pProjectInfo->curOffset >= pRes->info.rows) {
    pProjectInfo->curOffset -= pRes->info.rows;
    blockDataCleanup(pRes);
    return PROJECT_RETRIEVE_CONTINUE;
  } else if (pProjectInfo->curOffset < pRes->info.rows && pProjectInfo->curOffset > 0) {
    blockDataTrimFirstNRows(pRes, pProjectInfo->curOffset);
    pProjectInfo->curOffset = 0;
  }

3221
  // check for the limitation in each group
wmmhello's avatar
wmmhello 已提交
3222 3223 3224
  if (pProjectInfo->limit.limit >= 0 && pProjectInfo->curOutput + pRes->info.rows >= pProjectInfo->limit.limit) {
    int32_t keepRows = (int32_t)(pProjectInfo->limit.limit - pProjectInfo->curOutput);
    blockDataKeepFirstNRows(pRes, keepRows);
3225
    if (pProjectInfo->slimit.limit > 0 && pProjectInfo->slimit.limit <= pProjectInfo->curGroupOutput) {
3226 3227 3228
      pOperator->status = OP_EXEC_DONE;
    }

3229
    return PROJECT_RETRIEVE_DONE;
3230
  }
3231

3232
  // todo optimize performance
3233 3234
  // 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.
L
Liu Jicong 已提交
3235 3236
  if (pRes->info.rows >= pOperator->resultInfo.threshold || pProjectInfo->slimit.offset != -1 ||
      pProjectInfo->slimit.limit != -1) {
3237
    return PROJECT_RETRIEVE_DONE;
L
Liu Jicong 已提交
3238
  } else {  // not full enough, continue to accumulate the output data in the buffer.
3239 3240 3241 3242
    return PROJECT_RETRIEVE_CONTINUE;
  }
}

3243
static SSDataBlock* doProjectOperation(SOperatorInfo* pOperator) {
3244
  SProjectOperatorInfo* pProjectInfo = pOperator->info;
L
Liu Jicong 已提交
3245
  SOptrBasicInfo*       pInfo = &pProjectInfo->binfo;
3246

L
Liu Jicong 已提交
3247
  SExprSupp*   pSup = &pOperator->exprSupp;
3248
  SSDataBlock* pRes = pInfo->pRes;
3249
  blockDataCleanup(pRes);
3250

3251
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3252 3253 3254
  if (pOperator->status == OP_EXEC_DONE) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
3255

H
Haojun Liao 已提交
3256
#if 0
3257 3258 3259 3260 3261
  if (pProjectInfo->existDataBlock) {  // TODO refactor
    SSDataBlock* pBlock = pProjectInfo->existDataBlock;
    pProjectInfo->existDataBlock = NULL;

    // the pDataBlock are always the same one, no need to call this again
H
Haojun Liao 已提交
3262
    setInputDataBlock(pOperator, pInfo->pCtx, pBlock, TSDB_ORDER_ASC);
3263

H
Haojun Liao 已提交
3264
    blockDataEnsureCapacity(pInfo->pRes, pBlock->info.rows);
3265
    projectApplyFunctions(pOperator->exprSupp.pExprInfo, pInfo->pRes, pBlock, pInfo->pCtx, pOperator->exprSupp.numOfExprs);
L
Liu Jicong 已提交
3266
    if (pRes->info.rows >= pProjectInfo->binfo.capacity * 0.8) {
3267 3268
      copyTsColoum(pRes, pInfo->pCtx, pOperator->exprSupp.numOfExprs);
      resetResultRowEntryResult(pInfo->pCtx, pOperator->exprSupp.numOfExprs);
3269 3270 3271
      return pRes;
    }
  }
H
Haojun Liao 已提交
3272
#endif
3273

3274
  int64_t st = 0;
3275 3276 3277
  int32_t order = 0;
  int32_t scanFlag = 0;

3278 3279 3280 3281
  if (pOperator->cost.openCost == 0) {
    st = taosGetTimestampUs();
  }

H
Haojun Liao 已提交
3282 3283
  SOperatorInfo* downstream = pOperator->pDownstream[0];

L
Liu Jicong 已提交
3284
  while (1) {
H
Haojun Liao 已提交
3285
    // The downstream exec may change the value of the newgroup, so use a local variable instead.
3286
    SSDataBlock* pBlock = downstream->fpSet.getNextFn(downstream);
3287
    if (pBlock == NULL) {
3288
      doSetOperatorCompleted(pOperator);
3289 3290
      break;
    }
3291 3292 3293 3294
    if (pBlock->info.type == STREAM_RETRIEVE) {
      // for stream interval
      return pBlock;
    }
3295 3296

    // the pDataBlock are always the same one, no need to call this again
3297
    int32_t code = getTableScanInfo(pOperator->pDownstream[0], &order, &scanFlag);
3298 3299 3300
    if (code != TSDB_CODE_SUCCESS) {
      longjmp(pTaskInfo->env, code);
    }
3301

3302
    setInputDataBlock(pOperator, pSup->pCtx, pBlock, order, scanFlag, false);
3303 3304
    blockDataEnsureCapacity(pInfo->pRes, pInfo->pRes->info.rows + pBlock->info.rows);

3305
    code = projectApplyFunctions(pSup->pExprInfo, pInfo->pRes, pBlock, pSup->pCtx, pSup->numOfExprs,
X
Xiaoyu Wang 已提交
3306
                                 pProjectInfo->pPseudoColInfo);
3307 3308
    if (code != TSDB_CODE_SUCCESS) {
      longjmp(pTaskInfo->env, code);
3309 3310
    }

3311
    int32_t status = handleLimitOffset(pOperator, pBlock);
3312 3313 3314 3315

    // filter shall be applied after apply functions and limit/offset on the result
    doFilter(pProjectInfo->pFilterNode, pInfo->pRes);

3316
    if (status == PROJECT_RETRIEVE_CONTINUE) {
H
Haojun Liao 已提交
3317
      continue;
L
Liu Jicong 已提交
3318
    } else if (status == PROJECT_RETRIEVE_DONE) {
3319 3320 3321
      break;
    }
  }
dengyihao's avatar
dengyihao 已提交
3322

H
Haojun Liao 已提交
3323
  pProjectInfo->curOutput += pInfo->pRes->info.rows;
H
Haojun Liao 已提交
3324

3325 3326 3327 3328
  size_t rows = pInfo->pRes->info.rows;
  pOperator->resultInfo.totalRows += rows;

  if (pOperator->cost.openCost == 0) {
3329
    pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
3330 3331
  }

3332
  return (rows > 0) ? pInfo->pRes : NULL;
3333 3334
}

L
Liu Jicong 已提交
3335 3336
static void doHandleRemainBlockForNewGroupImpl(SFillOperatorInfo* pInfo, SResultInfo* pResultInfo, bool* newgroup,
                                               SExecTaskInfo* pTaskInfo) {
3337
  pInfo->totalInputRows = pInfo->existNewGroupBlock->info.rows;
H
Haojun Liao 已提交
3338

3339
  int64_t ekey = Q_STATUS_EQUAL(pTaskInfo->status, TASK_COMPLETED) ? pInfo->win.ekey
L
Liu Jicong 已提交
3340
                                                                   : pInfo->existNewGroupBlock->info.window.ekey;
3341 3342
  taosResetFillInfo(pInfo->pFillInfo, getFillInfoStart(pInfo->pFillInfo));

3343
  taosFillSetStartInfo(pInfo->pFillInfo, pInfo->existNewGroupBlock->info.rows, ekey);
3344 3345
  taosFillSetInputDataBlock(pInfo->pFillInfo, pInfo->existNewGroupBlock);

3346
  doFillTimeIntervalGapsInResults(pInfo->pFillInfo, pInfo->pRes, pResultInfo->capacity);
3347 3348 3349 3350
  pInfo->existNewGroupBlock = NULL;
  *newgroup = true;
}

L
Liu Jicong 已提交
3351 3352
static void doHandleRemainBlockFromNewGroup(SFillOperatorInfo* pInfo, SResultInfo* pResultInfo, bool* newgroup,
                                            SExecTaskInfo* pTaskInfo) {
3353 3354
  if (taosFillHasMoreResults(pInfo->pFillInfo)) {
    *newgroup = false;
3355
    doFillTimeIntervalGapsInResults(pInfo->pFillInfo, pInfo->pRes, (int32_t)pResultInfo->capacity);
H
Haojun Liao 已提交
3356
    if (pInfo->pRes->info.rows > pResultInfo->threshold || (!pInfo->multigroupResult)) {
3357 3358 3359 3360 3361 3362
      return;
    }
  }

  // handle the cached new group data block
  if (pInfo->existNewGroupBlock) {
3363
    doHandleRemainBlockForNewGroupImpl(pInfo, pResultInfo, newgroup, pTaskInfo);
3364 3365 3366
  }
}

3367
static SSDataBlock* doFill(SOperatorInfo* pOperator) {
L
Liu Jicong 已提交
3368 3369
  SFillOperatorInfo* pInfo = pOperator->info;
  SExecTaskInfo*     pTaskInfo = pOperator->pTaskInfo;
3370

H
Haojun Liao 已提交
3371
  SResultInfo* pResultInfo = &pOperator->resultInfo;
3372 3373 3374
  SSDataBlock* pResBlock = pInfo->pRes;

  blockDataCleanup(pResBlock);
3375 3376 3377 3378
  if (pOperator->status == OP_EXEC_DONE) {
    return NULL;
  }

3379
  // todo handle different group data interpolation
X
Xiaoyu Wang 已提交
3380 3381
  bool  n = false;
  bool* newgroup = &n;
3382
  doHandleRemainBlockFromNewGroup(pInfo, pResultInfo, newgroup, pTaskInfo);
3383 3384
  if (pResBlock->info.rows > pResultInfo->threshold || (!pInfo->multigroupResult && pResBlock->info.rows > 0)) {
    return pResBlock;
H
Haojun Liao 已提交
3385
  }
3386

H
Haojun Liao 已提交
3387
  SOperatorInfo* pDownstream = pOperator->pDownstream[0];
L
Liu Jicong 已提交
3388
  while (1) {
3389
    SSDataBlock* pBlock = pDownstream->fpSet.getNextFn(pDownstream);
3390 3391 3392 3393 3394 3395 3396 3397 3398 3399
    if (*newgroup) {
      assert(pBlock != NULL);
    }

    if (*newgroup && pInfo->totalInputRows > 0) {  // there are already processed current group data block
      pInfo->existNewGroupBlock = pBlock;
      *newgroup = false;

      // Fill the previous group data block, before handle the data block of new group.
      // Close the fill operation for previous group data block
3400
      taosFillSetStartInfo(pInfo->pFillInfo, 0, pInfo->win.ekey);
3401 3402 3403 3404 3405 3406 3407
    } else {
      if (pBlock == NULL) {
        if (pInfo->totalInputRows == 0) {
          pOperator->status = OP_EXEC_DONE;
          return NULL;
        }

3408
        taosFillSetStartInfo(pInfo->pFillInfo, 0, pInfo->win.ekey);
3409 3410 3411 3412 3413 3414 3415
      } else {
        pInfo->totalInputRows += pBlock->info.rows;
        taosFillSetStartInfo(pInfo->pFillInfo, pBlock->info.rows, pBlock->info.window.ekey);
        taosFillSetInputDataBlock(pInfo->pFillInfo, pBlock);
      }
    }

3416 3417
    blockDataEnsureCapacity(pResBlock, pOperator->resultInfo.capacity);
    doFillTimeIntervalGapsInResults(pInfo->pFillInfo, pResBlock, pOperator->resultInfo.capacity);
3418 3419

    // current group has no more result to return
3420
    if (pResBlock->info.rows > 0) {
3421 3422
      // 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
3423 3424
      if (pResBlock->info.rows > pResultInfo->threshold || pBlock == NULL || (!pInfo->multigroupResult)) {
        return pResBlock;
3425 3426
      }

3427
      doHandleRemainBlockFromNewGroup(pInfo, pResultInfo, newgroup, pTaskInfo);
3428 3429
      if (pResBlock->info.rows > pOperator->resultInfo.threshold || pBlock == NULL) {
        return pResBlock;
3430 3431 3432
      }
    } else if (pInfo->existNewGroupBlock) {  // try next group
      assert(pBlock != NULL);
3433
      doHandleRemainBlockForNewGroupImpl(pInfo, pResultInfo, newgroup, pTaskInfo);
3434 3435
      if (pResBlock->info.rows > pResultInfo->threshold) {
        return pResBlock;
3436 3437 3438 3439 3440 3441 3442
      }
    } else {
      return NULL;
    }
  }
}

H
Haojun Liao 已提交
3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453
static void destroyExprInfo(SExprInfo* pExpr, int32_t numOfExprs) {
  for (int32_t i = 0; i < numOfExprs; ++i) {
    SExprInfo* pExprInfo = &pExpr[i];
    if (pExprInfo->pExpr->nodeType == QUERY_NODE_COLUMN) {
      taosMemoryFree(pExprInfo->base.pParam[0].pCol);
    }
    taosMemoryFree(pExprInfo->base.pParam);
    taosMemoryFree(pExprInfo->pExpr);
  }
}

3454 3455 3456 3457 3458
static void destroyOperatorInfo(SOperatorInfo* pOperator) {
  if (pOperator == NULL) {
    return;
  }

3459
  if (pOperator->fpSet.closeFn != NULL) {
3460
    pOperator->fpSet.closeFn(pOperator->info, pOperator->exprSupp.numOfExprs);
3461 3462
  }

H
Haojun Liao 已提交
3463
  if (pOperator->pDownstream != NULL) {
L
Liu Jicong 已提交
3464
    for (int32_t i = 0; i < pOperator->numOfDownstream; ++i) {
H
Haojun Liao 已提交
3465
      destroyOperatorInfo(pOperator->pDownstream[i]);
3466 3467
    }

wafwerar's avatar
wafwerar 已提交
3468
    taosMemoryFreeClear(pOperator->pDownstream);
H
Haojun Liao 已提交
3469
    pOperator->numOfDownstream = 0;
3470 3471
  }

3472 3473
  if (pOperator->exprSupp.pExprInfo != NULL) {
    destroyExprInfo(pOperator->exprSupp.pExprInfo, pOperator->exprSupp.numOfExprs);
H
Haojun Liao 已提交
3474 3475
  }

3476
  taosMemoryFreeClear(pOperator->exprSupp.pExprInfo);
wafwerar's avatar
wafwerar 已提交
3477 3478
  taosMemoryFreeClear(pOperator->info);
  taosMemoryFreeClear(pOperator);
3479 3480
}

3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495
int32_t getBufferPgSize(int32_t rowSize, uint32_t* defaultPgsz, uint32_t* defaultBufsz) {
  *defaultPgsz = 4096;
  while (*defaultPgsz < rowSize * 4) {
    *defaultPgsz <<= 1u;
  }

  // at least four pages need to be in buffer
  *defaultBufsz = 4096 * 256;
  if ((*defaultBufsz) <= (*defaultPgsz)) {
    (*defaultBufsz) = (*defaultPgsz) * 4;
  }

  return 0;
}

dengyihao's avatar
dengyihao 已提交
3496 3497
int32_t doInitAggInfoSup(SAggSupporter* pAggSup, SqlFunctionCtx* pCtx, int32_t numOfOutput, size_t keyBufSize,
                         const char* pKey) {
3498 3499
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);

dengyihao's avatar
dengyihao 已提交
3500 3501
  pAggSup->resultRowSize = getResultRowSize(pCtx, numOfOutput);
  pAggSup->keyBuf = taosMemoryCalloc(1, keyBufSize + POINTER_BYTES + sizeof(int64_t));
3502 3503
  pAggSup->pResultRowHashTable = taosHashInit(10, hashFn, true, HASH_NO_LOCK);

H
Haojun Liao 已提交
3504
  if (pAggSup->keyBuf == NULL || pAggSup->pResultRowHashTable == NULL) {
3505 3506 3507
    return TSDB_CODE_OUT_OF_MEMORY;
  }

dengyihao's avatar
dengyihao 已提交
3508
  uint32_t defaultPgsz = 0;
3509 3510
  uint32_t defaultBufsz = 0;
  getBufferPgSize(pAggSup->resultRowSize, &defaultPgsz, &defaultBufsz);
H
Haojun Liao 已提交
3511

3512
  int32_t code = createDiskbasedBuf(&pAggSup->pResultBuf, defaultPgsz, defaultBufsz, pKey, TD_TMP_DIR_PATH);
H
Haojun Liao 已提交
3513 3514 3515 3516
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }

3517 3518 3519
  return TSDB_CODE_SUCCESS;
}

3520
void cleanupAggSup(SAggSupporter* pAggSup) {
wafwerar's avatar
wafwerar 已提交
3521
  taosMemoryFreeClear(pAggSup->keyBuf);
3522
  taosHashCleanup(pAggSup->pResultRowHashTable);
H
Haojun Liao 已提交
3523
  destroyDiskbasedBuf(pAggSup->pResultBuf);
3524 3525
}

L
Liu Jicong 已提交
3526 3527
int32_t initAggInfo(SExprSupp* pSup, SAggSupporter* pAggSup, SExprInfo* pExprInfo, int32_t numOfCols, size_t keyBufSize,
                    const char* pkey) {
3528 3529 3530 3531 3532
  int32_t code = initExprSupp(pSup, pExprInfo, numOfCols);
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }

3533
  doInitAggInfoSup(pAggSup, pSup->pCtx, numOfCols, keyBufSize, pkey);
L
Liu Jicong 已提交
3534
  for (int32_t i = 0; i < numOfCols; ++i) {
3535
    pSup->pCtx[i].pBuf = pAggSup->pResultBuf;
3536 3537
  }

3538
  return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
3539 3540
}

3541
void initResultSizeInfo(SOperatorInfo* pOperator, int32_t numOfRows) {
wmmhello's avatar
wmmhello 已提交
3542
  ASSERT(numOfRows != 0);
3543 3544 3545 3546
  pOperator->resultInfo.capacity = numOfRows;
  pOperator->resultInfo.threshold = numOfRows * 0.75;

  if (pOperator->resultInfo.threshold == 0) {
wmmhello's avatar
wmmhello 已提交
3547
    pOperator->resultInfo.threshold = numOfRows;
3548 3549 3550
  }
}

3551 3552 3553 3554 3555
void initBasicInfo(SOptrBasicInfo* pInfo, SSDataBlock* pBlock) {
  pInfo->pRes = pBlock;
  initResultRowInfo(&pInfo->resultRowInfo);
}

3556
int32_t initExprSupp(SExprSupp* pSup, SExprInfo* pExprInfo, int32_t numOfExpr) {
3557 3558 3559 3560
  pSup->pExprInfo = pExprInfo;
  pSup->numOfExprs = numOfExpr;
  if (pSup->pExprInfo != NULL) {
    pSup->pCtx = createSqlFunctionCtx(pExprInfo, numOfExpr, &pSup->rowEntryInfoOffset);
3561 3562 3563
    if (pSup->pCtx == NULL) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
3564
  }
3565 3566

  return TSDB_CODE_SUCCESS;
3567 3568
}

L
Liu Jicong 已提交
3569
SOperatorInfo* createAggregateOperatorInfo(SOperatorInfo* downstream, SExprInfo* pExprInfo, int32_t numOfCols,
S
slzhou 已提交
3570
                                           SSDataBlock* pResultBlock, SNode* pCondition, SExprInfo* pScalarExprInfo,
wmmhello's avatar
wmmhello 已提交
3571
                                           int32_t numOfScalarExpr, SExecTaskInfo* pTaskInfo) {
wafwerar's avatar
wafwerar 已提交
3572
  SAggOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SAggOperatorInfo));
L
Liu Jicong 已提交
3573
  SOperatorInfo*    pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
H
Haojun Liao 已提交
3574 3575 3576
  if (pInfo == NULL || pOperator == NULL) {
    goto _error;
  }
H
Haojun Liao 已提交
3577

3578
  int32_t numOfRows = 1024;
dengyihao's avatar
dengyihao 已提交
3579
  size_t  keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
3580 3581

  initResultSizeInfo(pOperator, numOfRows);
3582
  int32_t code = initAggInfo(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, numOfCols, keyBufSize, pTaskInfo->id.str);
L
Liu Jicong 已提交
3583
  if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
3584 3585
    goto _error;
  }
H
Haojun Liao 已提交
3586

3587
  initBasicInfo(&pInfo->binfo, pResultBlock);
3588 3589 3590 3591
  code = initExprSupp(&pInfo->scalarExprSup, pScalarExprInfo, numOfScalarExpr);
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
3592

L
Liu Jicong 已提交
3593
  pInfo->groupId = INT32_MIN;
S
slzhou 已提交
3594
  pInfo->pCondition = pCondition;
dengyihao's avatar
dengyihao 已提交
3595
  pOperator->name = "TableAggregate";
3596
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_HASH_AGG;
3597
  pOperator->blocking = true;
dengyihao's avatar
dengyihao 已提交
3598 3599 3600
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
  pOperator->pTaskInfo = pTaskInfo;
H
Haojun Liao 已提交
3601

3602 3603
  pOperator->fpSet = createOperatorFpSet(doOpenAggregateOptr, getAggregateResult, NULL, NULL, destroyAggOperatorInfo,
                                         aggEncodeResultRow, aggDecodeResultRow, NULL);
H
Haojun Liao 已提交
3604 3605 3606 3607 3608

  code = appendDownstream(pOperator, &downstream, 1);
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
3609 3610

  return pOperator;
L
Liu Jicong 已提交
3611
_error:
H
Haojun Liao 已提交
3612
  destroyAggOperatorInfo(pInfo, numOfCols);
wafwerar's avatar
wafwerar 已提交
3613 3614
  taosMemoryFreeClear(pInfo);
  taosMemoryFreeClear(pOperator);
H
Haojun Liao 已提交
3615 3616
  pTaskInfo->code = TSDB_CODE_OUT_OF_MEMORY;
  return NULL;
3617 3618
}

3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637
static void* destroySqlFunctionCtx(SqlFunctionCtx* pCtx, int32_t numOfOutput) {
  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);
    taosMemoryFree(pCtx[i].input.pData);
    taosMemoryFree(pCtx[i].input.pColumnDataAgg);
  }

  taosMemoryFreeClear(pCtx);
  return NULL;
}

3638
void cleanupBasicInfo(SOptrBasicInfo* pInfo) {
3639 3640
  assert(pInfo != NULL);
  cleanupResultRowInfo(&pInfo->resultRowInfo);
H
Haojun Liao 已提交
3641
  pInfo->pRes = blockDataDestroy(pInfo->pRes);
3642 3643
}

H
Haojun Liao 已提交
3644
void destroyBasicOperatorInfo(void* param, int32_t numOfOutput) {
L
Liu Jicong 已提交
3645
  SOptrBasicInfo* pInfo = (SOptrBasicInfo*)param;
3646
  cleanupBasicInfo(pInfo);
3647
}
H
Haojun Liao 已提交
3648 3649

void destroyAggOperatorInfo(void* param, int32_t numOfOutput) {
L
Liu Jicong 已提交
3650
  SAggOperatorInfo* pInfo = (SAggOperatorInfo*)param;
3651
  cleanupBasicInfo(&pInfo->binfo);
3652
}
3653

H
Haojun Liao 已提交
3654
void destroySFillOperatorInfo(void* param, int32_t numOfOutput) {
L
Liu Jicong 已提交
3655
  SFillOperatorInfo* pInfo = (SFillOperatorInfo*)param;
3656
  pInfo->pFillInfo = taosDestroyFillInfo(pInfo->pFillInfo);
H
Haojun Liao 已提交
3657
  pInfo->pRes = blockDataDestroy(pInfo->pRes);
wafwerar's avatar
wafwerar 已提交
3658
  taosMemoryFreeClear(pInfo->p);
3659 3660
}

H
Haojun Liao 已提交
3661
static void destroyProjectOperatorInfo(void* param, int32_t numOfOutput) {
D
fix bug  
dapan 已提交
3662 3663 3664
  if (NULL == param) {
    return;
  }
L
Liu Jicong 已提交
3665
  SProjectOperatorInfo* pInfo = (SProjectOperatorInfo*)param;
3666
  cleanupBasicInfo(&pInfo->binfo);
H
Haojun Liao 已提交
3667
  cleanupAggSup(&pInfo->aggSup);
H
Haojun Liao 已提交
3668
  taosArrayDestroy(pInfo->pPseudoColInfo);
3669 3670
}

3671
void cleanupExprSupp(SExprSupp* pSupp) {
3672 3673 3674 3675 3676 3677
  destroySqlFunctionCtx(pSupp->pCtx, pSupp->numOfExprs);
  destroyExprInfo(pSupp->pExprInfo, pSupp->numOfExprs);

  taosMemoryFree(pSupp->rowEntryInfoOffset);
}

H
Haojun Liao 已提交
3678
static void destroyIndefinitOperatorInfo(void* param, int32_t numOfOutput) {
3679
  SIndefOperatorInfo* pInfo = (SIndefOperatorInfo*)param;
3680
  cleanupBasicInfo(&pInfo->binfo);
H
Haojun Liao 已提交
3681 3682 3683

  taosArrayDestroy(pInfo->pPseudoColInfo);
  cleanupAggSup(&pInfo->aggSup);
3684
  cleanupExprSupp(&pInfo->scalarSup);
H
Haojun Liao 已提交
3685 3686
}

H
Haojun Liao 已提交
3687
void destroyExchangeOperatorInfo(void* param, int32_t numOfOutput) {
L
Liu Jicong 已提交
3688
  SExchangeInfo* pExInfo = (SExchangeInfo*)param;
3689 3690 3691 3692
  taosRemoveRef(exchangeObjRefPool, pExInfo->self);
}

void doDestroyExchangeOperatorInfo(void* param) {
X
Xiaoyu Wang 已提交
3693
  SExchangeInfo* pExInfo = (SExchangeInfo*)param;
3694

H
Haojun Liao 已提交
3695 3696 3697 3698 3699 3700 3701 3702 3703
  taosArrayDestroy(pExInfo->pSources);
  taosArrayDestroy(pExInfo->pSourceDataInfo);
  if (pExInfo->pResult != NULL) {
    blockDataDestroy(pExInfo->pResult);
  }

  tsem_destroy(&pExInfo->ready);
}

H
Haojun Liao 已提交
3704 3705
static SArray* setRowTsColumnOutputInfo(SqlFunctionCtx* pCtx, int32_t numOfCols) {
  SArray* pList = taosArrayInit(4, sizeof(int32_t));
dengyihao's avatar
dengyihao 已提交
3706
  for (int32_t i = 0; i < numOfCols; ++i) {
H
Haojun Liao 已提交
3707 3708 3709 3710 3711 3712 3713 3714
    if (fmIsPseudoColumnFunc(pCtx[i].functionId)) {
      taosArrayPush(pList, &i);
    }
  }

  return pList;
}

3715 3716 3717 3718
static int64_t getLimit(SNode* pLimit) { return NULL == pLimit ? -1 : ((SLimitNode*)pLimit)->limit; }

static int64_t getOffset(SNode* pLimit) { return NULL == pLimit ? -1 : ((SLimitNode*)pLimit)->offset; }

L
Liu Jicong 已提交
3719
SOperatorInfo* createProjectOperatorInfo(SOperatorInfo* downstream, SProjectPhysiNode* pProjPhyNode,
dengyihao's avatar
dengyihao 已提交
3720
                                         SExecTaskInfo* pTaskInfo) {
wafwerar's avatar
wafwerar 已提交
3721
  SProjectOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SProjectOperatorInfo));
L
Liu Jicong 已提交
3722
  SOperatorInfo*        pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
H
Haojun Liao 已提交
3723 3724 3725
  if (pInfo == NULL || pOperator == NULL) {
    goto _error;
  }
3726

L
Liu Jicong 已提交
3727
  int32_t    numOfCols = 0;
3728 3729 3730
  SExprInfo* pExprInfo = createExprInfo(pProjPhyNode->pProjections, NULL, &numOfCols);

  SSDataBlock* pResBlock = createResDataBlock(pProjPhyNode->node.pOutputDataBlockDesc);
3731 3732
  SLimit       limit = {.limit = getLimit(pProjPhyNode->node.pLimit), .offset = getOffset(pProjPhyNode->node.pLimit)};
  SLimit slimit = {.limit = getLimit(pProjPhyNode->node.pSlimit), .offset = getOffset(pProjPhyNode->node.pSlimit)};
3733

L
Liu Jicong 已提交
3734 3735 3736 3737
  pInfo->limit = limit;
  pInfo->slimit = slimit;
  pInfo->curOffset = limit.offset;
  pInfo->curSOffset = slimit.offset;
H
Haojun Liao 已提交
3738
  pInfo->binfo.pRes = pResBlock;
3739
  pInfo->pFilterNode = pProjPhyNode->node.pConditions;
H
Haojun Liao 已提交
3740 3741

  int32_t numOfRows = 4096;
dengyihao's avatar
dengyihao 已提交
3742
  size_t  keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
3743

3744 3745 3746 3747 3748
  // Make sure the size of SSDataBlock will never exceed the size of 2MB.
  int32_t TWOMB = 2 * 1024 * 1024;
  if (numOfRows * pResBlock->info.rowSize > TWOMB) {
    numOfRows = TWOMB / pResBlock->info.rowSize;
  }
3749
  initResultSizeInfo(pOperator, numOfRows);
3750

3751 3752
  initAggInfo(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, numOfCols, keyBufSize, pTaskInfo->id.str);
  initBasicInfo(&pInfo->binfo, pResBlock);
3753
  setFunctionResultOutput(pOperator, &pInfo->binfo, &pInfo->aggSup, MAIN_SCAN, numOfCols);
3754

3755
  pInfo->pPseudoColInfo = setRowTsColumnOutputInfo(pOperator->exprSupp.pCtx, numOfCols);
X
Xiaoyu Wang 已提交
3756
  pOperator->name = "ProjectOperator";
H
Haojun Liao 已提交
3757
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_PROJECT;
X
Xiaoyu Wang 已提交
3758 3759 3760 3761
  pOperator->blocking = false;
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
  pOperator->pTaskInfo = pTaskInfo;
3762

L
Liu Jicong 已提交
3763 3764
  pOperator->fpSet = createOperatorFpSet(operatorDummyOpenFn, doProjectOperation, NULL, NULL,
                                         destroyProjectOperatorInfo, NULL, NULL, NULL);
L
Liu Jicong 已提交
3765

3766
  int32_t code = appendDownstream(pOperator, &downstream, 1);
H
Haojun Liao 已提交
3767
  if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
3768 3769
    goto _error;
  }
3770 3771

  return pOperator;
H
Haojun Liao 已提交
3772

L
Liu Jicong 已提交
3773
_error:
H
Haojun Liao 已提交
3774 3775
  pTaskInfo->code = TSDB_CODE_OUT_OF_MEMORY;
  return NULL;
3776 3777
}

H
Haojun Liao 已提交
3778 3779
static SSDataBlock* doApplyIndefinitFunction(SOperatorInfo* pOperator) {
  SIndefOperatorInfo* pIndefInfo = pOperator->info;
3780
  SOptrBasicInfo*     pInfo = &pIndefInfo->binfo;
L
Liu Jicong 已提交
3781
  SExprSupp*          pSup = &pOperator->exprSupp;
H
Haojun Liao 已提交
3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815

  SSDataBlock* pRes = pInfo->pRes;
  blockDataCleanup(pRes);

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

  int64_t st = 0;
  int32_t order = 0;
  int32_t scanFlag = 0;

  if (pOperator->cost.openCost == 0) {
    st = taosGetTimestampUs();
  }

  SOperatorInfo* downstream = pOperator->pDownstream[0];

  while (1) {
    // The downstream exec may change the value of the newgroup, so use a local variable instead.
    SSDataBlock* pBlock = downstream->fpSet.getNextFn(downstream);
    if (pBlock == NULL) {
      doSetOperatorCompleted(pOperator);
      break;
    }

    // the pDataBlock are always the same one, no need to call this again
    int32_t code = getTableScanInfo(pOperator->pDownstream[0], &order, &scanFlag);
    if (code != TSDB_CODE_SUCCESS) {
      longjmp(pTaskInfo->env, code);
    }

    // there is an scalar expression that needs to be calculated before apply the group aggregation.
3816
    SExprSupp* pScalarSup = &pIndefInfo->scalarSup;
3817 3818
    if (pScalarSup->pExprInfo != NULL) {
      code = projectApplyFunctions(pScalarSup->pExprInfo, pBlock, pBlock, pScalarSup->pCtx, pScalarSup->numOfExprs,
L
Liu Jicong 已提交
3819
                                   pIndefInfo->pPseudoColInfo);
H
Haojun Liao 已提交
3820 3821 3822 3823 3824
      if (code != TSDB_CODE_SUCCESS) {
        longjmp(pTaskInfo->env, code);
      }
    }

3825
    setInputDataBlock(pOperator, pSup->pCtx, pBlock, order, scanFlag, false);
H
Haojun Liao 已提交
3826 3827
    blockDataEnsureCapacity(pInfo->pRes, pInfo->pRes->info.rows + pBlock->info.rows);

L
Liu Jicong 已提交
3828 3829
    code = projectApplyFunctions(pOperator->exprSupp.pExprInfo, pInfo->pRes, pBlock, pSup->pCtx,
                                 pOperator->exprSupp.numOfExprs, pIndefInfo->pPseudoColInfo);
H
Haojun Liao 已提交
3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844
    if (code != TSDB_CODE_SUCCESS) {
      longjmp(pTaskInfo->env, code);
    }
  }

  size_t rows = pInfo->pRes->info.rows;
  pOperator->resultInfo.totalRows += rows;

  if (pOperator->cost.openCost == 0) {
    pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
  }

  return (rows > 0) ? pInfo->pRes : NULL;
}

3845 3846
SOperatorInfo* createIndefinitOutputOperatorInfo(SOperatorInfo* downstream, SPhysiNode* pNode,
                                                 SExecTaskInfo* pTaskInfo) {
H
Haojun Liao 已提交
3847
  SIndefOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SIndefOperatorInfo));
3848
  SOperatorInfo*      pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
H
Haojun Liao 已提交
3849 3850 3851 3852
  if (pInfo == NULL || pOperator == NULL) {
    goto _error;
  }

3853 3854
  SExprSupp* pSup = &pOperator->exprSupp;

H
Haojun Liao 已提交
3855 3856 3857
  SIndefRowsFuncPhysiNode* pPhyNode = (SIndefRowsFuncPhysiNode*)pNode;

  int32_t    numOfExpr = 0;
X
Xiaoyu Wang 已提交
3858
  SExprInfo* pExprInfo = createExprInfo(pPhyNode->pFuncs, NULL, &numOfExpr);
H
Haojun Liao 已提交
3859 3860

  if (pPhyNode->pExprs != NULL) {
3861
    int32_t    num = 0;
3862
    SExprInfo* pSExpr = createExprInfo(pPhyNode->pExprs, NULL, &num);
3863
    int32_t    code = initExprSupp(&pInfo->scalarSup, pSExpr, num);
3864 3865 3866
    if (code != TSDB_CODE_SUCCESS) {
      goto _error;
    }
H
Haojun Liao 已提交
3867 3868
  }

3869
  SSDataBlock* pResBlock = createResDataBlock(pPhyNode->node.pOutputDataBlockDesc);
H
Haojun Liao 已提交
3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880

  int32_t numOfRows = 4096;
  size_t  keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;

  // Make sure the size of SSDataBlock will never exceed the size of 2MB.
  int32_t TWOMB = 2 * 1024 * 1024;
  if (numOfRows * pResBlock->info.rowSize > TWOMB) {
    numOfRows = TWOMB / pResBlock->info.rowSize;
  }
  initResultSizeInfo(pOperator, numOfRows);

3881 3882 3883
  initAggInfo(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, numOfExpr, keyBufSize, pTaskInfo->id.str);
  initBasicInfo(&pInfo->binfo, pResBlock);

3884
  setFunctionResultOutput(pOperator, &pInfo->binfo, &pInfo->aggSup, MAIN_SCAN, numOfExpr);
H
Haojun Liao 已提交
3885

3886 3887
  pInfo->binfo.pRes = pResBlock;
  pInfo->pPseudoColInfo = setRowTsColumnOutputInfo(pSup->pCtx, numOfExpr);
H
Haojun Liao 已提交
3888

3889
  pOperator->name = "IndefinitOperator";
H
Haojun Liao 已提交
3890
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_PROJECT;
3891 3892 3893
  pOperator->blocking = false;
  pOperator->status = OP_NOT_OPENED;
  pOperator->info = pInfo;
3894 3895
  pOperator->exprSupp.pExprInfo = pExprInfo;
  pOperator->exprSupp.numOfExprs = numOfExpr;
3896
  pOperator->pTaskInfo = pTaskInfo;
H
Haojun Liao 已提交
3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907

  pOperator->fpSet = createOperatorFpSet(operatorDummyOpenFn, doApplyIndefinitFunction, NULL, NULL,
                                         destroyIndefinitOperatorInfo, NULL, NULL, NULL);

  int32_t code = appendDownstream(pOperator, &downstream, 1);
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }

  return pOperator;

3908
_error:
H
Haojun Liao 已提交
3909 3910 3911 3912 3913 3914
  taosMemoryFree(pInfo);
  taosMemoryFree(pOperator);
  pTaskInfo->code = TSDB_CODE_OUT_OF_MEMORY;
  return NULL;
}

3915
static int32_t initFillInfo(SFillOperatorInfo* pInfo, SExprInfo* pExpr, int32_t numOfCols, SNodeListNode* pValNode,
L
Liu Jicong 已提交
3916
                            STimeWindow win, int32_t capacity, const char* id, SInterval* pInterval, int32_t fillType) {
3917
  SFillColInfo* pColInfo = createFillColInfo(pExpr, numOfCols, pValNode);
H
Haojun Liao 已提交
3918 3919

  STimeWindow w = TSWINDOW_INITIALIZER;
3920
  getAlignQueryTimeWindow(pInterval, pInterval->precision, win.skey, &w);
H
Haojun Liao 已提交
3921 3922

  int32_t order = TSDB_ORDER_ASC;
3923
  pInfo->pFillInfo = taosCreateFillInfo(order, w.skey, 0, capacity, numOfCols, pInterval, fillType, pColInfo, id);
H
Haojun Liao 已提交
3924

3925 3926
  pInfo->win = win;
  pInfo->p   = taosMemoryCalloc(numOfCols, POINTER_BYTES);
H
Haojun Liao 已提交
3927
  if (pInfo->pFillInfo == NULL || pInfo->p == NULL) {
H
Haojun Liao 已提交
3928 3929
    taosMemoryFree(pInfo->pFillInfo);
    taosMemoryFree(pInfo->p);
H
Haojun Liao 已提交
3930 3931 3932 3933 3934 3935
    return TSDB_CODE_OUT_OF_MEMORY;
  } else {
    return TSDB_CODE_SUCCESS;
  }
}

3936 3937 3938 3939 3940 3941 3942 3943
SOperatorInfo* createFillOperatorInfo(SOperatorInfo* downstream, SFillPhysiNode* pPhyFillNode, bool multigroupResult,
                                      SExecTaskInfo* pTaskInfo) {
  SFillOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SFillOperatorInfo));
  SOperatorInfo*     pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
  if (pInfo == NULL || pOperator == NULL) {
    goto _error;
  }

L
Liu Jicong 已提交
3944 3945 3946 3947 3948
  int32_t      num = 0;
  SSDataBlock* pResBlock = createResDataBlock(pPhyFillNode->node.pOutputDataBlockDesc);
  SExprInfo*   pExprInfo = createExprInfo(pPhyFillNode->pTargets, NULL, &num);
  SInterval*   pInterval = &((SIntervalAggOperatorInfo*)downstream->info)->interval;
  int32_t      type = convertFillType(pPhyFillNode->mode);
3949

H
Haojun Liao 已提交
3950
  SResultInfo* pResultInfo = &pOperator->resultInfo;
3951 3952
  initResultSizeInfo(pOperator, 4096);

3953 3954
  int32_t code = initFillInfo(pInfo, pExprInfo, num, (SNodeListNode*)pPhyFillNode->pValues, pPhyFillNode->timeRange,
                              pResultInfo->capacity, pTaskInfo->id.str, pInterval, type);
3955 3956 3957
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
3958

L
Liu Jicong 已提交
3959
  pInfo->pRes = pResBlock;
3960
  pInfo->multigroupResult = multigroupResult;
dengyihao's avatar
dengyihao 已提交
3961
  pOperator->name = "FillOperator";
3962
  pOperator->blocking = false;
dengyihao's avatar
dengyihao 已提交
3963
  pOperator->status = OP_NOT_OPENED;
3964
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_FILL;
3965 3966 3967 3968
  pOperator->exprSupp.pExprInfo = pExprInfo;
  pOperator->exprSupp.numOfExprs = num;
  pOperator->info = pInfo;
  pOperator->pTaskInfo = pTaskInfo;
H
Haojun Liao 已提交
3969

L
Liu Jicong 已提交
3970 3971
  pOperator->fpSet =
      createOperatorFpSet(operatorDummyOpenFn, doFill, NULL, NULL, destroySFillOperatorInfo, NULL, NULL, NULL);
3972

3973
  code = appendDownstream(pOperator, &downstream, 1);
3974
  return pOperator;
H
Haojun Liao 已提交
3975

L
Liu Jicong 已提交
3976
_error:
wafwerar's avatar
wafwerar 已提交
3977 3978
  taosMemoryFreeClear(pOperator);
  taosMemoryFreeClear(pInfo);
H
Haojun Liao 已提交
3979
  return NULL;
3980 3981
}

D
dapan1121 已提交
3982
static SExecTaskInfo* createExecTaskInfo(uint64_t queryId, uint64_t taskId, EOPTR_EXEC_MODEL model, char* dbFName) {
wafwerar's avatar
wafwerar 已提交
3983
  SExecTaskInfo* pTaskInfo = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
3984
  setTaskStatus(pTaskInfo, TASK_NOT_COMPLETED);
H
Haojun Liao 已提交
3985

D
dapan1121 已提交
3986
  pTaskInfo->schemaVer.dbname = strdup(dbFName);
3987
  pTaskInfo->cost.created = taosGetTimestampMs();
H
Haojun Liao 已提交
3988
  pTaskInfo->id.queryId = queryId;
dengyihao's avatar
dengyihao 已提交
3989
  pTaskInfo->execModel = model;
H
Haojun Liao 已提交
3990

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

3995 3996
  return pTaskInfo;
}
H
Haojun Liao 已提交
3997

H
Hongze Cheng 已提交
3998
static STsdbReader* doCreateDataReader(STableScanPhysiNode* pTableScanNode, SReadHandle* pHandle,
H
Haojun Liao 已提交
3999
                                       STableListInfo* pTableListInfo, const char* idstr);
H
Haojun Liao 已提交
4000

H
Haojun Liao 已提交
4001
static SArray* extractColumnInfo(SNodeList* pNodeList);
4002

D
dapan1121 已提交
4003
int32_t extractTableSchemaVersion(SReadHandle* pHandle, uint64_t uid, SExecTaskInfo* pTaskInfo) {
4004 4005
  SMetaReader mr = {0};
  metaReaderInit(&mr, pHandle->meta, 0);
D
dapan1121 已提交
4006
  int32_t code = metaGetTableEntryByUid(&mr, uid);
4007
  if (code != TSDB_CODE_SUCCESS) {
D
dapan1121 已提交
4008
    metaReaderClear(&mr);
4009
    return terrno;
D
dapan1121 已提交
4010
  }
4011 4012 4013 4014

  pTaskInfo->schemaVer.tablename = strdup(mr.me.name);

  if (mr.me.type == TSDB_SUPER_TABLE) {
4015
    pTaskInfo->schemaVer.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
4016
    pTaskInfo->schemaVer.tversion = mr.me.stbEntry.schemaTag.version;
4017 4018 4019
  } else if (mr.me.type == TSDB_CHILD_TABLE) {
    tb_uid_t suid = mr.me.ctbEntry.suid;
    metaGetTableEntryByUid(&mr, suid);
4020
    pTaskInfo->schemaVer.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
4021
    pTaskInfo->schemaVer.tversion = mr.me.stbEntry.schemaTag.version;
4022
  } else {
4023
    pTaskInfo->schemaVer.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
4024
  }
4025 4026

  metaReaderClear(&mr);
D
dapan1121 已提交
4027 4028

  return TSDB_CODE_SUCCESS;
4029 4030
}

4031
static int32_t sortTableGroup(STableListInfo* pTableListInfo, int32_t groupNum) {
wmmhello's avatar
wmmhello 已提交
4032
  taosArrayClear(pTableListInfo->pGroupList);
4033 4034
  SArray* sortSupport = taosArrayInit(groupNum, sizeof(uint64_t));
  if (sortSupport == NULL) return TSDB_CODE_OUT_OF_MEMORY;
wmmhello's avatar
wmmhello 已提交
4035 4036
  for (int32_t i = 0; i < taosArrayGetSize(pTableListInfo->pTableList); i++) {
    STableKeyInfo* info = taosArrayGet(pTableListInfo->pTableList, i);
4037
    uint64_t*      groupId = taosHashGet(pTableListInfo->map, &info->uid, sizeof(uint64_t));
wmmhello's avatar
wmmhello 已提交
4038 4039

    int32_t index = taosArraySearchIdx(sortSupport, groupId, compareUint64Val, TD_EQ);
4040 4041 4042 4043
    if (index == -1) {
      void*   p = taosArraySearch(sortSupport, groupId, compareUint64Val, TD_GT);
      SArray* tGroup = taosArrayInit(8, sizeof(STableKeyInfo));
      if (tGroup == NULL) {
wmmhello's avatar
wmmhello 已提交
4044 4045 4046
        taosArrayDestroy(sortSupport);
        return TSDB_CODE_OUT_OF_MEMORY;
      }
4047
      if (taosArrayPush(tGroup, info) == NULL) {
wmmhello's avatar
wmmhello 已提交
4048 4049 4050 4051
        qError("taos push info array error");
        taosArrayDestroy(sortSupport);
        return TSDB_CODE_QRY_APP_ERROR;
      }
4052
      if (p == NULL) {
wmmhello's avatar
wmmhello 已提交
4053
        if (taosArrayPush(sortSupport, groupId) == NULL) {
wmmhello's avatar
wmmhello 已提交
4054 4055 4056 4057
          qError("taos push support array error");
          taosArrayDestroy(sortSupport);
          return TSDB_CODE_QRY_APP_ERROR;
        }
wmmhello's avatar
wmmhello 已提交
4058
        if (taosArrayPush(pTableListInfo->pGroupList, &tGroup) == NULL) {
wmmhello's avatar
wmmhello 已提交
4059 4060 4061 4062
          qError("taos push group array error");
          taosArrayDestroy(sortSupport);
          return TSDB_CODE_QRY_APP_ERROR;
        }
4063
      } else {
wmmhello's avatar
wmmhello 已提交
4064
        int32_t pos = TARRAY_ELEM_IDX(sortSupport, p);
4065
        if (taosArrayInsert(sortSupport, pos, groupId) == NULL) {
wmmhello's avatar
wmmhello 已提交
4066 4067 4068 4069
          qError("taos insert support array error");
          taosArrayDestroy(sortSupport);
          return TSDB_CODE_QRY_APP_ERROR;
        }
4070
        if (taosArrayInsert(pTableListInfo->pGroupList, pos, &tGroup) == NULL) {
wmmhello's avatar
wmmhello 已提交
4071 4072 4073 4074 4075
          qError("taos insert group array error");
          taosArrayDestroy(sortSupport);
          return TSDB_CODE_QRY_APP_ERROR;
        }
      }
4076
    } else {
wmmhello's avatar
wmmhello 已提交
4077
      SArray* tGroup = (SArray*)taosArrayGetP(pTableListInfo->pGroupList, index);
4078
      if (taosArrayPush(tGroup, info) == NULL) {
wmmhello's avatar
wmmhello 已提交
4079 4080 4081 4082 4083 4084 4085 4086 4087 4088
        qError("taos push uid array error");
        taosArrayDestroy(sortSupport);
        return TSDB_CODE_QRY_APP_ERROR;
      }
    }
  }
  taosArrayDestroy(sortSupport);
  return TDB_CODE_SUCCESS;
}

wmmhello's avatar
wmmhello 已提交
4089 4090
int32_t generateGroupIdMap(STableListInfo* pTableListInfo, SReadHandle* pHandle, SNodeList* group) {
  if (group == NULL) {
wmmhello's avatar
wmmhello 已提交
4091 4092 4093 4094 4095 4096 4097 4098
    return TDB_CODE_SUCCESS;
  }

  pTableListInfo->map = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
  if (pTableListInfo->map == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  int32_t keyLen = 0;
X
Xiaoyu Wang 已提交
4099
  void*   keyBuf = NULL;
wmmhello's avatar
wmmhello 已提交
4100

4101
  SNode* node;
wmmhello's avatar
wmmhello 已提交
4102
  FOREACH(node, group) {
4103
    SExprNode* pExpr = (SExprNode*)node;
wmmhello's avatar
wmmhello 已提交
4104
    keyLen += pExpr->resType.bytes;
wmmhello's avatar
wmmhello 已提交
4105 4106
  }

wmmhello's avatar
wmmhello 已提交
4107
  int32_t nullFlagSize = sizeof(int8_t) * LIST_LENGTH(group);
wmmhello's avatar
wmmhello 已提交
4108 4109 4110 4111 4112 4113 4114
  keyLen += nullFlagSize;

  keyBuf = taosMemoryCalloc(1, keyLen);
  if (keyBuf == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

4115
  int32_t groupNum = 0;
X
Xiaoyu Wang 已提交
4116 4117 4118
  for (int32_t i = 0; i < taosArrayGetSize(pTableListInfo->pTableList); i++) {
    STableKeyInfo* info = taosArrayGet(pTableListInfo->pTableList, i);
    SMetaReader    mr = {0};
wmmhello's avatar
wmmhello 已提交
4119 4120 4121
    metaReaderInit(&mr, pHandle->meta, 0);
    metaGetTableEntryByUid(&mr, info->uid);

4122
    SNodeList* groupNew = nodesCloneList(group);
wmmhello's avatar
wmmhello 已提交
4123

wmmhello's avatar
wmmhello 已提交
4124
    nodesRewriteExprsPostOrder(groupNew, doTranslateTagExpr, &mr);
wmmhello's avatar
wmmhello 已提交
4125
    char* isNull = (char*)keyBuf;
wmmhello's avatar
wmmhello 已提交
4126 4127
    char* pStart = (char*)keyBuf + nullFlagSize;

4128
    SNode*  pNode;
wmmhello's avatar
wmmhello 已提交
4129
    int32_t index = 0;
4130
    FOREACH(pNode, groupNew) {
wmmhello's avatar
wmmhello 已提交
4131 4132 4133 4134
      SNode*  pNew = NULL;
      int32_t code = scalarCalculateConstants(pNode, &pNew);
      if (TSDB_CODE_SUCCESS == code) {
        REPLACE_NODE(pNew);
X
Xiaoyu Wang 已提交
4135
      } else {
4136
        taosMemoryFree(keyBuf);
wmmhello's avatar
wmmhello 已提交
4137
        nodesClearList(groupNew);
4138
        metaReaderClear(&mr);
wmmhello's avatar
wmmhello 已提交
4139
        return code;
wmmhello's avatar
wmmhello 已提交
4140
      }
4141

wmmhello's avatar
wmmhello 已提交
4142
      ASSERT(nodeType(pNew) == QUERY_NODE_VALUE);
4143
      SValueNode* pValue = (SValueNode*)pNew;
4144

wmmhello's avatar
wmmhello 已提交
4145
      if (pValue->node.resType.type == TSDB_DATA_TYPE_NULL || pValue->isNull) {
wmmhello's avatar
wmmhello 已提交
4146 4147 4148 4149
        isNull[index++] = 1;
        continue;
      } else {
        isNull[index++] = 0;
4150
        char* data = nodesGetValueFromNode(pValue);
L
Liu Jicong 已提交
4151 4152
        if (pValue->node.resType.type == TSDB_DATA_TYPE_JSON) {
          if (tTagIsJson(data)) {
wmmhello's avatar
wmmhello 已提交
4153 4154 4155
            terrno = TSDB_CODE_QRY_JSON_IN_GROUP_ERROR;
            taosMemoryFree(keyBuf);
            nodesClearList(groupNew);
4156
            metaReaderClear(&mr);
wmmhello's avatar
wmmhello 已提交
4157 4158
            return terrno;
          }
wmmhello's avatar
wmmhello 已提交
4159
          int32_t len = getJsonValueLen(data);
wmmhello's avatar
wmmhello 已提交
4160 4161 4162
          memcpy(pStart, data, len);
          pStart += len;
        } else if (IS_VAR_DATA_TYPE(pValue->node.resType.type)) {
wmmhello's avatar
wmmhello 已提交
4163 4164
          memcpy(pStart, data, varDataTLen(data));
          pStart += varDataTLen(data);
wmmhello's avatar
wmmhello 已提交
4165
        } else {
wmmhello's avatar
wmmhello 已提交
4166 4167
          memcpy(pStart, data, pValue->node.resType.bytes);
          pStart += pValue->node.resType.bytes;
wmmhello's avatar
wmmhello 已提交
4168 4169 4170
        }
      }
    }
4171
    int32_t  len = (int32_t)(pStart - (char*)keyBuf);
4172 4173
    uint64_t groupId = calcGroupId(keyBuf, len);
    taosHashPut(pTableListInfo->map, &(info->uid), sizeof(uint64_t), &groupId, sizeof(uint64_t));
S
slzhou 已提交
4174
    info->groupId = groupId;
4175
    groupNum++;
wmmhello's avatar
wmmhello 已提交
4176

wmmhello's avatar
wmmhello 已提交
4177
    nodesClearList(groupNew);
wmmhello's avatar
wmmhello 已提交
4178 4179 4180
    metaReaderClear(&mr);
  }
  taosMemoryFree(keyBuf);
4181

4182
  if (pTableListInfo->needSortTableByGroupId) {
wmmhello's avatar
wmmhello 已提交
4183
    return sortTableGroup(pTableListInfo, groupNum);
4184 4185
  }

wmmhello's avatar
wmmhello 已提交
4186 4187 4188
  return TDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
4189
SOperatorInfo* createOperatorTree(SPhysiNode* pPhyNode, SExecTaskInfo* pTaskInfo, SReadHandle* pHandle,
4190 4191
                                  uint64_t queryId, uint64_t taskId, STableListInfo* pTableListInfo,
                                  const char* pUser) {
4192 4193
  int32_t type = nodeType(pPhyNode);

X
Xiaoyu Wang 已提交
4194
  if (pPhyNode->pChildren == NULL || LIST_LENGTH(pPhyNode->pChildren) == 0) {
H
Haojun Liao 已提交
4195
    if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == type) {
dengyihao's avatar
dengyihao 已提交
4196
      STableScanPhysiNode* pTableScanNode = (STableScanPhysiNode*)pPhyNode;
H
Haojun Liao 已提交
4197

wmmhello's avatar
wmmhello 已提交
4198
      int32_t code = createScanTableListInfo(pTableScanNode, pHandle, pTableListInfo, queryId, taskId);
4199
      if (code) {
wmmhello's avatar
wmmhello 已提交
4200
        pTaskInfo->code = code;
D
dapan1121 已提交
4201 4202
        return NULL;
      }
wmmhello's avatar
wmmhello 已提交
4203

wmmhello's avatar
wmmhello 已提交
4204
      code = extractTableSchemaVersion(pHandle, pTableScanNode->scan.uid, pTaskInfo);
S
slzhou 已提交
4205
      if (code) {
4206
        pTaskInfo->code = terrno;
wmmhello's avatar
wmmhello 已提交
4207 4208 4209
        return NULL;
      }

H
Haojun Liao 已提交
4210
      SOperatorInfo*  pOperator = createTableScanOperatorInfo(pTableScanNode, pHandle, pTaskInfo);
4211 4212
      STableScanInfo* pScanInfo = pOperator->info;
      pTaskInfo->cost.pRecoder = &pScanInfo->readRecorder;
S
slzhou 已提交
4213
      return pOperator;
L
Liu Jicong 已提交
4214

S
slzhou 已提交
4215 4216
    } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == type) {
      STableMergeScanPhysiNode* pTableScanNode = (STableMergeScanPhysiNode*)pPhyNode;
wmmhello's avatar
wmmhello 已提交
4217
      int32_t code = createScanTableListInfo(pTableScanNode, pHandle, pTableListInfo, queryId, taskId);
L
Liu Jicong 已提交
4218
      if (code) {
wmmhello's avatar
wmmhello 已提交
4219
        pTaskInfo->code = code;
wmmhello's avatar
wmmhello 已提交
4220 4221 4222 4223 4224 4225 4226
        return NULL;
      }
      code = extractTableSchemaVersion(pHandle, pTableScanNode->scan.uid, pTaskInfo);
      if (code) {
        pTaskInfo->code = terrno;
        return NULL;
      }
wmmhello's avatar
wmmhello 已提交
4227

4228 4229
      SOperatorInfo* pOperator =
          createTableMergeScanOperatorInfo(pTableScanNode, pTableListInfo, pHandle, pTaskInfo, queryId, taskId);
wmmhello's avatar
wmmhello 已提交
4230

4231 4232 4233
      STableScanInfo* pScanInfo = pOperator->info;
      pTaskInfo->cost.pRecoder = &pScanInfo->readRecorder;
      return pOperator;
L
Liu Jicong 已提交
4234

H
Haojun Liao 已提交
4235
    } else if (QUERY_NODE_PHYSICAL_PLAN_EXCHANGE == type) {
4236
      return createExchangeOperatorInfo(pHandle->pMsgCb->clientRpc, (SExchangePhysiNode*)pPhyNode, pTaskInfo);
L
Liu Jicong 已提交
4237

H
Haojun Liao 已提交
4238
    } else if (QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN == type) {
5
54liuyao 已提交
4239
      STableScanPhysiNode* pTableScanNode = (STableScanPhysiNode*)pPhyNode;
4240
      STimeWindowAggSupp   twSup = {
L
Liu Jicong 已提交
4241 4242 4243 4244
            .waterMark = pTableScanNode->watermark,
            .calTrigger = pTableScanNode->triggerType,
            .maxTs = INT64_MIN,
      };
L
Liu Jicong 已提交
4245
      if (pHandle) {
wmmhello's avatar
wmmhello 已提交
4246
        int32_t code = createScanTableListInfo(pTableScanNode, pHandle, pTableListInfo, queryId, taskId);
L
Liu Jicong 已提交
4247
        if (code) {
wmmhello's avatar
wmmhello 已提交
4248 4249 4250
          pTaskInfo->code = code;
          return NULL;
        }
5
54liuyao 已提交
4251
      }
4252

4253 4254
      SOperatorInfo* pOperator =
          createStreamScanOperatorInfo(pHandle, pTableScanNode, pTaskInfo, &twSup, queryId, taskId);
H
Haojun Liao 已提交
4255
      return pOperator;
L
Liu Jicong 已提交
4256

H
Haojun Liao 已提交
4257
    } else if (QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == type) {
L
Liu Jicong 已提交
4258
      SSystemTableScanPhysiNode* pSysScanPhyNode = (SSystemTableScanPhysiNode*)pPhyNode;
4259
      return createSysTableScanOperatorInfo(pHandle, pSysScanPhyNode, pUser, pTaskInfo);
4260
    } else if (QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN == type) {
X
Xiaoyu Wang 已提交
4261
      STagScanPhysiNode* pScanPhyNode = (STagScanPhysiNode*)pPhyNode;
4262

4263
      int32_t code = getTableList(pHandle->meta, pHandle->vnode, pScanPhyNode, pTableListInfo);
4264
      if (code != TSDB_CODE_SUCCESS) {
4265
        pTaskInfo->code = terrno;
4266 4267 4268
        return NULL;
      }

4269
      return createTagScanOperatorInfo(pHandle, pScanPhyNode, pTableListInfo, pTaskInfo);
4270
    } else if (QUERY_NODE_PHYSICAL_PLAN_BLOCK_DIST_SCAN == type) {
4271
      SBlockDistScanPhysiNode* pBlockNode = (SBlockDistScanPhysiNode*)pPhyNode;
4272 4273 4274
      pTableListInfo->pTableList = taosArrayInit(4, sizeof(STableKeyInfo));

      if (pBlockNode->tableType == TSDB_SUPER_TABLE) {
4275
        int32_t code = vnodeGetAllTableList(pHandle->vnode, pBlockNode->uid, pTableListInfo->pTableList);
4276 4277 4278 4279 4280
        if (code != TSDB_CODE_SUCCESS) {
          pTaskInfo->code = terrno;
          return NULL;
        }
      } else {  // Create one table group.
S
slzhou 已提交
4281
        STableKeyInfo info = {.lastKey = 0, .uid = pBlockNode->uid, .groupId = 0};
4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303
        taosArrayPush(pTableListInfo->pTableList, &info);
      }

      SQueryTableDataCond cond = {0};

      {
        cond.order = TSDB_ORDER_ASC;
        cond.numOfCols = 1;
        cond.colList = taosMemoryCalloc(1, sizeof(SColumnInfo));
        if (cond.colList == NULL) {
          terrno = TSDB_CODE_QRY_OUT_OF_MEMORY;
          return NULL;
        }

        cond.colList->colId = 1;
        cond.colList->type = TSDB_DATA_TYPE_TIMESTAMP;
        cond.colList->bytes = sizeof(TSKEY);

        cond.numOfTWindows = 1;
        cond.twindows = taosMemoryCalloc(1, sizeof(STimeWindow));
        cond.twindows[0] = (STimeWindow){.skey = INT64_MIN, .ekey = INT64_MAX};
        cond.suid = pBlockNode->suid;
H
Haojun Liao 已提交
4304
        cond.type = BLOCK_LOAD_OFFSET_ORDER;
4305
      }
H
Haojun Liao 已提交
4306 4307 4308

      STsdbReader* pReader = NULL;
      tsdbReaderOpen(pHandle->vnode, &cond, pTableListInfo->pTableList, &pReader, "");
4309 4310
      cleanupQueryTableDataCond(&cond);

4311
      return createDataBlockInfoScanOperator(pReader, pHandle, cond.suid, pBlockNode, pTaskInfo);
H
Haojun Liao 已提交
4312 4313 4314 4315 4316 4317 4318 4319 4320
    } else if (QUERY_NODE_PHYSICAL_PLAN_LAST_ROW_SCAN == type) {
      SLastRowScanPhysiNode* pScanNode = (SLastRowScanPhysiNode*)pPhyNode;

//      int32_t code = createScanTableListInfo(pTableScanNode, pHandle, pTableListInfo, queryId, taskId);
//      if (code) {
//        pTaskInfo->code = code;
//        return NULL;
//      }

4321
      int32_t code = extractTableSchemaVersion(pHandle, pScanNode->uid, pTaskInfo);
4322 4323 4324 4325
      if (code != TSDB_CODE_SUCCESS) {
        pTaskInfo->code = code;
        return NULL;
      }
4326

H
Haojun Liao 已提交
4327
      pTableListInfo->pTableList = taosArrayInit(4, sizeof(STableKeyInfo));
H
Haojun Liao 已提交
4328
      if (pScanNode->tableType == TSDB_SUPER_TABLE) {
4329
        code = vnodeGetAllTableList(pHandle->vnode, pScanNode->uid, pTableListInfo->pTableList);
H
Haojun Liao 已提交
4330 4331 4332 4333 4334 4335 4336 4337 4338
        if (code != TSDB_CODE_SUCCESS) {
          pTaskInfo->code = terrno;
          return NULL;
        }
      } else {  // Create one table group.
        STableKeyInfo info = {.lastKey = 0, .uid = pScanNode->uid, .groupId = 0};
        taosArrayPush(pTableListInfo->pTableList, &info);
      }

H
Haojun Liao 已提交
4339
      return createLastrowScanOperator(pScanNode, pHandle, pTableListInfo->pTableList, pTaskInfo);
H
Haojun Liao 已提交
4340 4341
    } else {
      ASSERT(0);
H
Haojun Liao 已提交
4342 4343 4344
    }
  }

4345 4346
  int32_t num = 0;
  size_t  size = LIST_LENGTH(pPhyNode->pChildren);
H
Haojun Liao 已提交
4347

4348
  SOperatorInfo** ops = taosMemoryCalloc(size, POINTER_BYTES);
dengyihao's avatar
dengyihao 已提交
4349
  for (int32_t i = 0; i < size; ++i) {
4350
    SPhysiNode* pChildNode = (SPhysiNode*)nodesListGetNode(pPhyNode->pChildren, i);
4351
    ops[i] = createOperatorTree(pChildNode, pTaskInfo, pHandle, queryId, taskId, pTableListInfo, pUser);
4352 4353 4354
    if (ops[i] == NULL) {
      return NULL;
    }
4355
  }
H
Haojun Liao 已提交
4356

4357
  SOperatorInfo* pOptr = NULL;
H
Haojun Liao 已提交
4358
  if (QUERY_NODE_PHYSICAL_PLAN_PROJECT == type) {
4359
    pOptr = createProjectOperatorInfo(ops[0], (SProjectPhysiNode*)pPhyNode, pTaskInfo);
4360
  } else if (QUERY_NODE_PHYSICAL_PLAN_HASH_AGG == type) {
H
Haojun Liao 已提交
4361 4362
    SAggPhysiNode* pAggNode = (SAggPhysiNode*)pPhyNode;
    SExprInfo*     pExprInfo = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &num);
4363
    SSDataBlock*   pResBlock = createResDataBlock(pPhyNode->pOutputDataBlockDesc);
H
Haojun Liao 已提交
4364

dengyihao's avatar
dengyihao 已提交
4365
    int32_t    numOfScalarExpr = 0;
4366 4367 4368 4369 4370
    SExprInfo* pScalarExprInfo = NULL;
    if (pAggNode->pExprs != NULL) {
      pScalarExprInfo = createExprInfo(pAggNode->pExprs, NULL, &numOfScalarExpr);
    }

H
Haojun Liao 已提交
4371 4372
    if (pAggNode->pGroupKeys != NULL) {
      SArray* pColList = extractColumnInfo(pAggNode->pGroupKeys);
dengyihao's avatar
dengyihao 已提交
4373
      pOptr = createGroupOperatorInfo(ops[0], pExprInfo, num, pResBlock, pColList, pAggNode->node.pConditions,
wmmhello's avatar
wmmhello 已提交
4374
                                      pScalarExprInfo, numOfScalarExpr, pTaskInfo);
H
Haojun Liao 已提交
4375
    } else {
dengyihao's avatar
dengyihao 已提交
4376
      pOptr =
S
slzhou 已提交
4377
          createAggregateOperatorInfo(ops[0], pExprInfo, num, pResBlock, pAggNode->node.pConditions, pScalarExprInfo, numOfScalarExpr, pTaskInfo);
H
Haojun Liao 已提交
4378
    }
X
Xiaoyu Wang 已提交
4379
  } else if (QUERY_NODE_PHYSICAL_PLAN_HASH_INTERVAL == type || QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL == type) {
H
Haojun Liao 已提交
4380
    SIntervalPhysiNode* pIntervalPhyNode = (SIntervalPhysiNode*)pPhyNode;
H
Haojun Liao 已提交
4381

H
Haojun Liao 已提交
4382
    SExprInfo*   pExprInfo = createExprInfo(pIntervalPhyNode->window.pFuncs, NULL, &num);
4383
    SSDataBlock* pResBlock = createResDataBlock(pPhyNode->pOutputDataBlockDesc);
H
Haojun Liao 已提交
4384

dengyihao's avatar
dengyihao 已提交
4385 4386 4387 4388 4389 4390
    SInterval interval = {.interval = pIntervalPhyNode->interval,
                          .sliding = pIntervalPhyNode->sliding,
                          .intervalUnit = pIntervalPhyNode->intervalUnit,
                          .slidingUnit = pIntervalPhyNode->slidingUnit,
                          .offset = pIntervalPhyNode->offset,
                          .precision = ((SColumnNode*)pIntervalPhyNode->window.pTspk)->node.resType.precision};
H
Haojun Liao 已提交
4391

X
Xiaoyu Wang 已提交
4392 4393 4394 4395 4396
    STimeWindowAggSupp as = {
        .waterMark = pIntervalPhyNode->window.watermark,
        .calTrigger = pIntervalPhyNode->window.triggerType,
        .maxTs = INT64_MIN,
    };
4397
    ASSERT(as.calTrigger != STREAM_TRIGGER_MAX_DELAY);
4398

4399
    int32_t tsSlotId = ((SColumnNode*)pIntervalPhyNode->window.pTspk)->slotId;
L
Liu Jicong 已提交
4400
    bool    isStream = (QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL == type);
4401 4402
    pOptr = createIntervalOperatorInfo(ops[0], pExprInfo, num, pResBlock, &interval, tsSlotId, &as, pIntervalPhyNode,
                                       pTaskInfo, isStream);
4403

4404 4405
  } else if (QUERY_NODE_PHYSICAL_PLAN_MERGE_ALIGNED_INTERVAL == type) {
    SMergeAlignedIntervalPhysiNode* pIntervalPhyNode = (SMergeAlignedIntervalPhysiNode*)pPhyNode;
S
shenglian zhou 已提交
4406 4407 4408 4409 4410 4411 4412 4413 4414 4415

    SExprInfo*   pExprInfo = createExprInfo(pIntervalPhyNode->window.pFuncs, NULL, &num);
    SSDataBlock* pResBlock = createResDataBlock(pPhyNode->pOutputDataBlockDesc);

    SInterval interval = {.interval = pIntervalPhyNode->interval,
                          .sliding = pIntervalPhyNode->sliding,
                          .intervalUnit = pIntervalPhyNode->intervalUnit,
                          .slidingUnit = pIntervalPhyNode->slidingUnit,
                          .offset = pIntervalPhyNode->offset,
                          .precision = ((SColumnNode*)pIntervalPhyNode->window.pTspk)->node.resType.precision};
4416

S
shenglian zhou 已提交
4417
    int32_t tsSlotId = ((SColumnNode*)pIntervalPhyNode->window.pTspk)->slotId;
4418
    pOptr = createMergeAlignedIntervalOperatorInfo(ops[0], pExprInfo, num, pResBlock, &interval, tsSlotId, pTaskInfo);
S
shenglian zhou 已提交
4419
  } else if (QUERY_NODE_PHYSICAL_PLAN_MERGE_INTERVAL == type) {
X
Xiaoyu Wang 已提交
4420
    SMergeIntervalPhysiNode* pIntervalPhyNode = (SMergeIntervalPhysiNode*)pPhyNode;
S
shenglian zhou 已提交
4421 4422 4423 4424 4425 4426 4427 4428 4429 4430

    SExprInfo*   pExprInfo = createExprInfo(pIntervalPhyNode->window.pFuncs, NULL, &num);
    SSDataBlock* pResBlock = createResDataBlock(pPhyNode->pOutputDataBlockDesc);

    SInterval interval = {.interval = pIntervalPhyNode->interval,
                          .sliding = pIntervalPhyNode->sliding,
                          .intervalUnit = pIntervalPhyNode->intervalUnit,
                          .slidingUnit = pIntervalPhyNode->slidingUnit,
                          .offset = pIntervalPhyNode->offset,
                          .precision = ((SColumnNode*)pIntervalPhyNode->window.pTspk)->node.resType.precision};
4431

S
shenglian zhou 已提交
4432 4433
    int32_t tsSlotId = ((SColumnNode*)pIntervalPhyNode->window.pTspk)->slotId;
    pOptr = createMergeIntervalOperatorInfo(ops[0], pExprInfo, num, pResBlock, &interval, tsSlotId, pTaskInfo);
5
54liuyao 已提交
4434
  } else if (QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_INTERVAL == type) {
4435
    int32_t children = 0;
5
54liuyao 已提交
4436 4437
    pOptr = createStreamFinalIntervalOperatorInfo(ops[0], pPhyNode, pTaskInfo, children);
  } else if (QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_INTERVAL == type) {
4438
    int32_t children = 1;
5
54liuyao 已提交
4439
    pOptr = createStreamFinalIntervalOperatorInfo(ops[0], pPhyNode, pTaskInfo, children);
H
Haojun Liao 已提交
4440
  } else if (QUERY_NODE_PHYSICAL_PLAN_SORT == type) {
4441
    pOptr = createSortOperatorInfo(ops[0], (SSortPhysiNode*)pPhyNode, pTaskInfo);
S
shenglian zhou 已提交
4442 4443
  } else if (QUERY_NODE_PHYSICAL_PLAN_GROUP_SORT == type) {
    pOptr = createGroupSortOperatorInfo(ops[0], (SGroupSortPhysiNode*)pPhyNode, pTaskInfo);
X
Xiaoyu Wang 已提交
4444
  } else if (QUERY_NODE_PHYSICAL_PLAN_MERGE == type) {
4445
    SMergePhysiNode* pMergePhyNode = (SMergePhysiNode*)pPhyNode;
4446
    pOptr = createMultiwayMergeOperatorInfo(ops, size, pMergePhyNode, pTaskInfo);
4447
  } else if (QUERY_NODE_PHYSICAL_PLAN_MERGE_SESSION == type) {
H
Haojun Liao 已提交
4448 4449
    SSessionWinodwPhysiNode* pSessionNode = (SSessionWinodwPhysiNode*)pPhyNode;

X
Xiaoyu Wang 已提交
4450 4451
    STimeWindowAggSupp as = {.waterMark = pSessionNode->window.watermark,
                             .calTrigger = pSessionNode->window.triggerType};
4452

H
Haojun Liao 已提交
4453
    SExprInfo*   pExprInfo = createExprInfo(pSessionNode->window.pFuncs, NULL, &num);
4454
    SSDataBlock* pResBlock = createResDataBlock(pPhyNode->pOutputDataBlockDesc);
4455 4456
    int32_t      tsSlotId = ((SColumnNode*)pSessionNode->window.pTspk)->slotId;

X
Xiaoyu Wang 已提交
4457 4458
    pOptr =
        createSessionAggOperatorInfo(ops[0], pExprInfo, num, pResBlock, pSessionNode->gap, tsSlotId, &as, pTaskInfo);
4459
  } else if (QUERY_NODE_PHYSICAL_PLAN_STREAM_SESSION == type) {
4460 4461 4462 4463 4464 4465 4466
    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) {
    int32_t children = 1;
    pOptr = createStreamFinalSessionAggOperatorInfo(ops[0], pPhyNode, pTaskInfo, children);
H
Haojun Liao 已提交
4467
  } else if (QUERY_NODE_PHYSICAL_PLAN_PARTITION == type) {
4468
    pOptr = createPartitionOperatorInfo(ops[0], (SPartitionPhysiNode*)pPhyNode, pTaskInfo);
4469
  } else if (QUERY_NODE_PHYSICAL_PLAN_MERGE_STATE == type) {
dengyihao's avatar
dengyihao 已提交
4470
    SStateWinodwPhysiNode* pStateNode = (SStateWinodwPhysiNode*)pPhyNode;
4471

4472 4473
    STimeWindowAggSupp as = {.waterMark = pStateNode->window.watermark, .calTrigger = pStateNode->window.triggerType};

dengyihao's avatar
dengyihao 已提交
4474
    SExprInfo*   pExprInfo = createExprInfo(pStateNode->window.pFuncs, NULL, &num);
4475
    SSDataBlock* pResBlock = createResDataBlock(pPhyNode->pOutputDataBlockDesc);
4476 4477
    int32_t      tsSlotId = ((SColumnNode*)pStateNode->window.pTspk)->slotId;

4478
    SColumnNode* pColNode = (SColumnNode*)((STargetNode*)pStateNode->pStateKey)->pExpr;
X
Xiaoyu Wang 已提交
4479
    SColumn      col = extractColumnFromColumnNode(pColNode);
4480
    pOptr = createStatewindowOperatorInfo(ops[0], pExprInfo, num, pResBlock, &as, tsSlotId, &col, pTaskInfo);
4481
  } else if (QUERY_NODE_PHYSICAL_PLAN_STREAM_STATE == type) {
5
54liuyao 已提交
4482
    pOptr = createStreamStateAggOperatorInfo(ops[0], pPhyNode, pTaskInfo);
4483
  } else if (QUERY_NODE_PHYSICAL_PLAN_MERGE_JOIN == type) {
4484
    pOptr = createMergeJoinOperatorInfo(ops, size, (SJoinPhysiNode*)pPhyNode, pTaskInfo);
4485
  } else if (QUERY_NODE_PHYSICAL_PLAN_FILL == type) {
4486
    pOptr = createFillOperatorInfo(ops[0], (SFillPhysiNode*)pPhyNode, false, pTaskInfo);
H
Haojun Liao 已提交
4487 4488
  } else if (QUERY_NODE_PHYSICAL_PLAN_INDEF_ROWS_FUNC == type) {
    pOptr = createIndefinitOutputOperatorInfo(ops[0], pPhyNode, pTaskInfo);
4489 4490
  } else if (QUERY_NODE_PHYSICAL_PLAN_INTERP_FUNC == type) {
    pOptr = createTimeSliceOperatorInfo(ops[0], pPhyNode, pTaskInfo);
H
Haojun Liao 已提交
4491 4492
  } else {
    ASSERT(0);
H
Haojun Liao 已提交
4493
  }
4494 4495 4496

  taosMemoryFree(ops);
  return pOptr;
4497
}
H
Haojun Liao 已提交
4498

4499
int32_t compareTimeWindow(const void* p1, const void* p2, const void* param) {
dengyihao's avatar
dengyihao 已提交
4500 4501 4502
  const SQueryTableDataCond* pCond = param;
  const STimeWindow*         pWin1 = p1;
  const STimeWindow*         pWin2 = p2;
4503 4504 4505 4506 4507 4508 4509 4510
  if (pCond->order == TSDB_ORDER_ASC) {
    return pWin1->skey - pWin2->skey;
  } else if (pCond->order == TSDB_ORDER_DESC) {
    return pWin2->skey - pWin1->skey;
  }
  return 0;
}

H
Haojun Liao 已提交
4511
SArray* extractColumnInfo(SNodeList* pNodeList) {
L
Liu Jicong 已提交
4512
  size_t  numOfCols = LIST_LENGTH(pNodeList);
H
Haojun Liao 已提交
4513 4514 4515 4516 4517 4518
  SArray* pList = taosArrayInit(numOfCols, sizeof(SColumn));
  if (pList == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

L
Liu Jicong 已提交
4519 4520
  for (int32_t i = 0; i < numOfCols; ++i) {
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pNodeList, i);
H
Haojun Liao 已提交
4521

4522 4523 4524
    if (nodeType(pNode->pExpr) == QUERY_NODE_COLUMN) {
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;

4525
      SColumn c = extractColumnFromColumnNode(pColNode);
4526 4527
      taosArrayPush(pList, &c);
    } else if (nodeType(pNode->pExpr) == QUERY_NODE_VALUE) {
L
Liu Jicong 已提交
4528 4529
      SValueNode* pValNode = (SValueNode*)pNode->pExpr;
      SColumn     c = {0};
4530
      c.slotId = pNode->slotId;
L
Liu Jicong 已提交
4531 4532 4533 4534
      c.colId = pNode->slotId;
      c.type = pValNode->node.type;
      c.bytes = pValNode->node.resType.bytes;
      c.scale = pValNode->node.resType.scale;
4535 4536 4537 4538
      c.precision = pValNode->node.resType.precision;

      taosArrayPush(pList, &c);
    }
H
Haojun Liao 已提交
4539 4540 4541 4542 4543
  }

  return pList;
}

H
Haojun Liao 已提交
4544
STsdbReader* doCreateDataReader(STableScanPhysiNode* pTableScanNode, SReadHandle* pHandle, STableListInfo* pTableListInfo, const char* idstr) {
4545
  int32_t code = getTableList(pHandle->meta, pHandle->vnode, &pTableScanNode->scan, pTableListInfo);
wmmhello's avatar
wmmhello 已提交
4546 4547 4548 4549 4550 4551
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }

  if (taosArrayGetSize(pTableListInfo->pTableList) == 0) {
    code = 0;
H
Haojun Liao 已提交
4552
    qDebug("no table qualified for query, %s", idstr);
wmmhello's avatar
wmmhello 已提交
4553 4554 4555
    goto _error;
  }

4556
  SQueryTableDataCond cond = {0};
wmmhello's avatar
wmmhello 已提交
4557
  code = initQueryTableDataCond(&cond, pTableScanNode);
4558
  if (code != TSDB_CODE_SUCCESS) {
wmmhello's avatar
wmmhello 已提交
4559
    goto _error;
X
Xiaoyu Wang 已提交
4560
  }
4561

H
Hongze Cheng 已提交
4562
  STsdbReader* pReader;
H
Haojun Liao 已提交
4563
  code = tsdbReaderOpen(pHandle->vnode, &cond, pTableListInfo->pTableList, &pReader, idstr);
H
Haojun Liao 已提交
4564 4565 4566 4567
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }

4568
  cleanupQueryTableDataCond(&cond);
H
Haojun Liao 已提交
4569 4570

  return pReader;
wmmhello's avatar
wmmhello 已提交
4571 4572 4573 4574

_error:
  terrno = code;
  return NULL;
H
Haojun Liao 已提交
4575 4576
}

L
Liu Jicong 已提交
4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596
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 {
    SStreamBlockScanInfo* pInfo = pOperator->info;
    ASSERT(pInfo->pSnapshotReadOp->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN);
    *ppInfo = pInfo->pSnapshotReadOp->info;
    return 0;
  }
}

4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618
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;
}

L
Liu Jicong 已提交
4619 4620 4621 4622 4623
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;
  }
4624

L
Liu Jicong 已提交
4625 4626 4627 4628
  STableScanPhysiNode* pNode = NULL;
  if (extractTableScanNode(plan->pNode, &pNode) < 0) {
    ASSERT(0);
  }
4629

H
Haojun Liao 已提交
4630
  tsdbReaderClose(pTableScanInfo->dataReader);
4631

L
Liu Jicong 已提交
4632
  STableListInfo info = {0};
H
Haojun Liao 已提交
4633
  pTableScanInfo->dataReader = doCreateDataReader(pNode, pHandle, &info, NULL);
L
Liu Jicong 已提交
4634 4635 4636 4637
  if (pTableScanInfo->dataReader == NULL) {
    ASSERT(0);
    qError("failed to create data reader");
    return TSDB_CODE_QRY_APP_ERROR;
4638
  }
L
Liu Jicong 已提交
4639
  // TODO: set uid and ts to data reader
4640 4641 4642
  return 0;
}

C
Cary Xu 已提交
4643
int32_t encodeOperator(SOperatorInfo* ops, char** result, int32_t* length, int32_t* nOptrWithVal) {
wmmhello's avatar
wmmhello 已提交
4644
  int32_t code = TDB_CODE_SUCCESS;
4645
  char*   pCurrent = NULL;
wmmhello's avatar
wmmhello 已提交
4646
  int32_t currLength = 0;
4647
  if (ops->fpSet.encodeResultRow) {
C
Cary Xu 已提交
4648
    if (result == NULL || length == NULL || nOptrWithVal == NULL) {
wmmhello's avatar
wmmhello 已提交
4649 4650 4651
      return TSDB_CODE_TSC_INVALID_INPUT;
    }
    code = ops->fpSet.encodeResultRow(ops, &pCurrent, &currLength);
wmmhello's avatar
wmmhello 已提交
4652

4653 4654
    if (code != TDB_CODE_SUCCESS) {
      if (*result != NULL) {
wmmhello's avatar
wmmhello 已提交
4655 4656 4657 4658
        taosMemoryFree(*result);
        *result = NULL;
      }
      return code;
C
Cary Xu 已提交
4659 4660 4661
    } else if (currLength == 0) {
      ASSERT(!pCurrent);
      goto _downstream;
wmmhello's avatar
wmmhello 已提交
4662
    }
wmmhello's avatar
wmmhello 已提交
4663

C
Cary Xu 已提交
4664 4665
    ++(*nOptrWithVal);

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

4668
    if (*result == NULL) {
wmmhello's avatar
wmmhello 已提交
4669
      *result = (char*)taosMemoryCalloc(1, currLength + sizeof(int32_t));
wmmhello's avatar
wmmhello 已提交
4670 4671 4672 4673 4674 4675
      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);
4676
    } else {
wmmhello's avatar
wmmhello 已提交
4677
      int32_t sizePre = *(int32_t*)(*result);
4678
      char*   tmp = (char*)taosMemoryRealloc(*result, sizePre + currLength);
wmmhello's avatar
wmmhello 已提交
4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690
      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 已提交
4691 4692
  }

C
Cary Xu 已提交
4693
_downstream:
wmmhello's avatar
wmmhello 已提交
4694
  for (int32_t i = 0; i < ops->numOfDownstream; ++i) {
C
Cary Xu 已提交
4695
    code = encodeOperator(ops->pDownstream[i], result, length, nOptrWithVal);
4696
    if (code != TDB_CODE_SUCCESS) {
wmmhello's avatar
wmmhello 已提交
4697
      return code;
wmmhello's avatar
wmmhello 已提交
4698 4699
    }
  }
wmmhello's avatar
wmmhello 已提交
4700
  return TDB_CODE_SUCCESS;
wmmhello's avatar
wmmhello 已提交
4701 4702
}

H
Haojun Liao 已提交
4703
int32_t decodeOperator(SOperatorInfo* ops, const char* result, int32_t length) {
wmmhello's avatar
wmmhello 已提交
4704
  int32_t code = TDB_CODE_SUCCESS;
4705 4706
  if (ops->fpSet.decodeResultRow) {
    if (result == NULL) {
wmmhello's avatar
wmmhello 已提交
4707 4708
      return TSDB_CODE_TSC_INVALID_INPUT;
    }
H
Haojun Liao 已提交
4709

4710
    ASSERT(length == *(int32_t*)result);
H
Haojun Liao 已提交
4711 4712

    const char* data = result + sizeof(int32_t);
L
Liu Jicong 已提交
4713
    code = ops->fpSet.decodeResultRow(ops, (char*)data);
4714
    if (code != TDB_CODE_SUCCESS) {
wmmhello's avatar
wmmhello 已提交
4715 4716
      return code;
    }
wmmhello's avatar
wmmhello 已提交
4717

wmmhello's avatar
wmmhello 已提交
4718
    int32_t totalLength = *(int32_t*)result;
4719 4720
    int32_t dataLength = *(int32_t*)data;

4721
    if (totalLength == dataLength + sizeof(int32_t)) {  // the last data
wmmhello's avatar
wmmhello 已提交
4722 4723
      result = NULL;
      length = 0;
4724
    } else {
wmmhello's avatar
wmmhello 已提交
4725 4726 4727 4728
      result += dataLength;
      *(int32_t*)(result) = totalLength - dataLength;
      length = totalLength - dataLength;
    }
wmmhello's avatar
wmmhello 已提交
4729 4730
  }

wmmhello's avatar
wmmhello 已提交
4731 4732
  for (int32_t i = 0; i < ops->numOfDownstream; ++i) {
    code = decodeOperator(ops->pDownstream[i], result, length);
4733
    if (code != TDB_CODE_SUCCESS) {
wmmhello's avatar
wmmhello 已提交
4734
      return code;
wmmhello's avatar
wmmhello 已提交
4735 4736
    }
  }
wmmhello's avatar
wmmhello 已提交
4737
  return TDB_CODE_SUCCESS;
wmmhello's avatar
wmmhello 已提交
4738 4739
}

4740
int32_t createDataSinkParam(SDataSinkNode* pNode, void** pParam, qTaskInfo_t* pTaskInfo) {
D
dapan1121 已提交
4741
  SExecTaskInfo* pTask = *(SExecTaskInfo**)pTaskInfo;
4742

D
dapan1121 已提交
4743 4744
  switch (pNode->type) {
    case QUERY_NODE_PHYSICAL_PLAN_DELETE: {
4745
      SDeleterParam* pDeleterParam = taosMemoryCalloc(1, sizeof(SDeleterParam));
D
dapan1121 已提交
4746 4747 4748 4749
      if (NULL == pDeleterParam) {
        return TSDB_CODE_OUT_OF_MEMORY;
      }
      int32_t tbNum = taosArrayGetSize(pTask->tableqinfoList.pTableList);
D
dapan1121 已提交
4750
      pDeleterParam->suid = pTask->tableqinfoList.suid;
D
dapan1121 已提交
4751 4752 4753 4754 4755 4756
      pDeleterParam->pUidList = taosArrayInit(tbNum, sizeof(uint64_t));
      if (NULL == pDeleterParam->pUidList) {
        taosMemoryFree(pDeleterParam);
        return TSDB_CODE_OUT_OF_MEMORY;
      }
      for (int32_t i = 0; i < tbNum; ++i) {
4757
        STableKeyInfo* pTable = taosArrayGet(pTask->tableqinfoList.pTableList, i);
D
dapan1121 已提交
4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770
        taosArrayPush(pDeleterParam->pUidList, &pTable->uid);
      }

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

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
4771
int32_t createExecTaskInfoImpl(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
4772
                               const char* sql, EOPTR_EXEC_MODEL model) {
H
Haojun Liao 已提交
4773 4774
  uint64_t queryId = pPlan->id.queryId;

H
Haojun Liao 已提交
4775
  int32_t code = TSDB_CODE_SUCCESS;
D
dapan1121 已提交
4776
  *pTaskInfo = createExecTaskInfo(queryId, taskId, model, pPlan->dbFName);
H
Haojun Liao 已提交
4777 4778 4779 4780
  if (*pTaskInfo == NULL) {
    code = TSDB_CODE_QRY_OUT_OF_MEMORY;
    goto _complete;
  }
H
Haojun Liao 已提交
4781

4782
  (*pTaskInfo)->sql = sql;
wmmhello's avatar
wmmhello 已提交
4783 4784
  (*pTaskInfo)->tableqinfoList.pTagCond = pPlan->pTagCond;
  (*pTaskInfo)->tableqinfoList.pTagIndexCond = pPlan->pTagIndexCond;
4785 4786
  (*pTaskInfo)->pRoot = createOperatorTree(pPlan->pNode, *pTaskInfo, pHandle, queryId, taskId,
                                           &(*pTaskInfo)->tableqinfoList, pPlan->user);
L
Liu Jicong 已提交
4787

H
Haojun Liao 已提交
4788

D
dapan1121 已提交
4789
  if (NULL == (*pTaskInfo)->pRoot) {
4790
    code = (*pTaskInfo)->code;
D
dapan1121 已提交
4791
    goto _complete;
4792 4793
  }

H
Haojun Liao 已提交
4794 4795
  return code;

H
Haojun Liao 已提交
4796
_complete:
wafwerar's avatar
wafwerar 已提交
4797
  taosMemoryFreeClear(*pTaskInfo);
H
Haojun Liao 已提交
4798 4799
  terrno = code;
  return code;
H
Haojun Liao 已提交
4800 4801
}

wmmhello's avatar
wmmhello 已提交
4802 4803 4804
static void doDestroyTableList(STableListInfo* pTableqinfoList) {
  taosArrayDestroy(pTableqinfoList->pTableList);
  taosHashCleanup(pTableqinfoList->map);
4805 4806
  if (pTableqinfoList->needSortTableByGroupId) {
    for (int32_t i = 0; i < taosArrayGetSize(pTableqinfoList->pGroupList); i++) {
wmmhello's avatar
wmmhello 已提交
4807 4808 4809 4810 4811
      SArray* tmp = taosArrayGetP(pTableqinfoList->pGroupList, i);
      taosArrayDestroy(tmp);
    }
  }
  taosArrayDestroy(pTableqinfoList->pGroupList);
4812

wmmhello's avatar
wmmhello 已提交
4813 4814
  pTableqinfoList->pTableList = NULL;
  pTableqinfoList->map = NULL;
4815 4816
}

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

wmmhello's avatar
wmmhello 已提交
4820
  doDestroyTableList(&pTaskInfo->tableqinfoList);
H
Haojun Liao 已提交
4821
  destroyOperatorInfo(pTaskInfo->pRoot);
L
Liu Jicong 已提交
4822 4823
  //  taosArrayDestroy(pTaskInfo->summary.queryProfEvents);
  //  taosHashCleanup(pTaskInfo->summary.operatorProfResults);
4824

4825 4826
  taosMemoryFree(pTaskInfo->schemaVer.dbname);
  taosMemoryFree(pTaskInfo->schemaVer.tablename);
wafwerar's avatar
wafwerar 已提交
4827 4828 4829
  taosMemoryFreeClear(pTaskInfo->sql);
  taosMemoryFreeClear(pTaskInfo->id.str);
  taosMemoryFreeClear(pTaskInfo);
4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841
}

static void doSetTagValueToResultBuf(char* output, const char* val, int16_t type, int16_t bytes) {
  if (val == NULL) {
    setNull(output, type, bytes);
    return;
  }

  if (IS_VAR_DATA_TYPE(type)) {
    // Binary data overflows for sort of unknown reasons. Let trim the overflow data
    if (varDataTLen(val) > bytes) {
      int32_t maxLen = bytes - VARSTR_HEADER_SIZE;
L
Liu Jicong 已提交
4842
      int32_t len = (varDataLen(val) > maxLen) ? maxLen : varDataLen(val);
4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854
      memcpy(varDataVal(output), varDataVal(val), len);
      varDataSetLen(output, len);
    } else {
      varDataCopy(output, val);
    }
  } else {
    memcpy(output, val, bytes);
  }
}

static int64_t getQuerySupportBufSize(size_t numOfTables) {
  size_t s1 = sizeof(STableQueryInfo);
L
Liu Jicong 已提交
4855 4856
  //  size_t s3 = sizeof(STableCheckInfo);  buffer consumption in tsdb
  return (int64_t)(s1 * 1.5 * numOfTables);
4857 4858 4859 4860 4861 4862 4863
}

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 已提交
4864
    while (1) {
4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890
      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 已提交
4891

dengyihao's avatar
dengyihao 已提交
4892 4893
int32_t getOperatorExplainExecInfo(SOperatorInfo* operatorInfo, SExplainExecInfo** pRes, int32_t* capacity,
                                   int32_t* resNum) {
D
dapan1121 已提交
4894 4895
  if (*resNum >= *capacity) {
    *capacity += 10;
dengyihao's avatar
dengyihao 已提交
4896

D
dapan1121 已提交
4897 4898
    *pRes = taosMemoryRealloc(*pRes, (*capacity) * sizeof(SExplainExecInfo));
    if (NULL == *pRes) {
D
dapan1121 已提交
4899
      qError("malloc %d failed", (*capacity) * (int32_t)sizeof(SExplainExecInfo));
D
dapan1121 已提交
4900 4901 4902 4903
      return TSDB_CODE_QRY_OUT_OF_MEMORY;
    }
  }

4904 4905 4906 4907 4908
  SExplainExecInfo* pInfo = &(*pRes)[*resNum];

  pInfo->numOfRows = operatorInfo->resultInfo.totalRows;
  pInfo->startupCost = operatorInfo->cost.openCost;
  pInfo->totalCost = operatorInfo->cost.totalCost;
D
dapan1121 已提交
4909

4910
  if (operatorInfo->fpSet.getExplainFn) {
4911
    int32_t code = operatorInfo->fpSet.getExplainFn(operatorInfo, &pInfo->verboseInfo, &pInfo->verboseLen);
D
dapan1121 已提交
4912
    if (code) {
4913
      qError("%s operator getExplainFn failed, code:%s", GET_TASKID(operatorInfo->pTaskInfo), tstrerror(code));
D
dapan1121 已提交
4914 4915
      return code;
    }
4916 4917 4918
  } else {
    pInfo->verboseLen = 0;
    pInfo->verboseInfo = NULL;
D
dapan1121 已提交
4919
  }
dengyihao's avatar
dengyihao 已提交
4920

D
dapan1121 已提交
4921
  ++(*resNum);
dengyihao's avatar
dengyihao 已提交
4922

D
dapan1121 已提交
4923
  int32_t code = 0;
D
dapan1121 已提交
4924 4925
  for (int32_t i = 0; i < operatorInfo->numOfDownstream; ++i) {
    code = getOperatorExplainExecInfo(operatorInfo->pDownstream[i], pRes, capacity, resNum);
D
dapan1121 已提交
4926 4927 4928 4929 4930 4931 4932
    if (code) {
      taosMemoryFreeClear(*pRes);
      return TSDB_CODE_QRY_OUT_OF_MEMORY;
    }
  }

  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
4933
}
5
54liuyao 已提交
4934

L
Liu Jicong 已提交
4935
int32_t initStreamAggSupporter(SStreamAggSupporter* pSup, const char* pKey, SqlFunctionCtx* pCtx, int32_t numOfOutput,
4936
                               int32_t size) {
4937
  pSup->resultRowSize = getResultRowSize(pCtx, numOfOutput);
5
54liuyao 已提交
4938 4939
  pSup->keySize = sizeof(int64_t) + sizeof(TSKEY);
  pSup->pKeyBuf = taosMemoryCalloc(1, pSup->keySize);
4940 4941
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
  pSup->pResultRows = taosHashInit(1024, hashFn, false, HASH_NO_LOCK);
5
54liuyao 已提交
4942 4943 4944
  if (pSup->pKeyBuf == NULL || pSup->pResultRows == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
4945
  pSup->valueSize = size;
5
54liuyao 已提交
4946

5
54liuyao 已提交
4947 4948
  pSup->pScanWindow = taosArrayInit(4, sizeof(STimeWindow));

5
54liuyao 已提交
4949 4950 4951 4952 4953 4954 4955 4956 4957
  int32_t pageSize = 4096;
  while (pageSize < pSup->resultRowSize * 4) {
    pageSize <<= 1u;
  }
  // at least four pages need to be in buffer
  int32_t bufSize = 4096 * 256;
  if (bufSize <= pageSize) {
    bufSize = pageSize * 4;
  }
4958
  int32_t code = createDiskbasedBuf(&pSup->pResultBuf, pageSize, bufSize, pKey, TD_TMP_DIR_PATH);
L
Liu Jicong 已提交
4959
  for (int32_t i = 0; i < numOfOutput; ++i) {
4960 4961 4962
    pCtx[i].pBuf = pSup->pResultBuf;
  }
  return code;
5
54liuyao 已提交
4963
}