executorimpl.c 124.9 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
#include "tcompare.h"
H
Haojun Liao 已提交
35
#include "thash.h"
36
#include "ttypes.h"
dengyihao's avatar
dengyihao 已提交
37
#include "vnode.h"
38

H
Haojun Liao 已提交
39
#define IS_MAIN_SCAN(runtime)          ((runtime)->scanFlag == MAIN_SCAN)
40 41 42 43 44 45
#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 已提交
46
  uint32_t v = taosRand();
47 48 49 50

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

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

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

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

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

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

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

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

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

92
static void destroyIntervalOperatorInfo(void* param);
H
Haojun Liao 已提交
93

H
Haojun Liao 已提交
94
void setOperatorCompleted(SOperatorInfo* pOperator) {
95
  pOperator->status = OP_EXEC_DONE;
H
Haojun Liao 已提交
96
  ASSERT(pOperator->pTaskInfo != NULL);
97

98
  pOperator->cost.totalCost = (taosGetTimestampUs() - pOperator->pTaskInfo->cost.start * 1000) / 1000.0;
H
Haojun Liao 已提交
99
  setTaskStatus(pOperator->pTaskInfo, TASK_COMPLETED);
100
}
101

H
Haojun Liao 已提交
102 103 104 105 106 107 108 109 110 111
void setOperatorInfo(SOperatorInfo* pOperator, const char* name, int32_t type, bool blocking, int32_t status,
                     void* pInfo, SExecTaskInfo* pTaskInfo) {
  pOperator->name = (char*)name;
  pOperator->operatorType = type;
  pOperator->blocking = blocking;
  pOperator->status = status;
  pOperator->info = pInfo;
  pOperator->pTaskInfo = pTaskInfo;
}

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

H
Haojun Liao 已提交
118 119
SOperatorFpSet createOperatorFpSet(__optr_open_fn_t openFn, __optr_fn_t nextFn, __optr_fn_t cleanup,
                                   __optr_close_fn_t closeFn, __optr_explain_fn_t explain) {
120 121 122 123 124 125 126 127 128 129 130
  SOperatorFpSet fpSet = {
      ._openFn = openFn,
      .getNextFn = nextFn,
      .cleanupFn = cleanup,
      .closeFn = closeFn,
      .getExplainFn = explain,
  };

  return fpSet;
}

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

134
static void initCtxOutputBuffer(SqlFunctionCtx* pCtx, int32_t size);
135
static void doSetTableGroupOutputBuf(SOperatorInfo* pOperator, int32_t numOfOutput, uint64_t groupId);
136

137
#if 0
L
Liu Jicong 已提交
138 139
static bool chkResultRowFromKey(STaskRuntimeEnv* pRuntimeEnv, SResultRowInfo* pResultRowInfo, char* pData,
                                int16_t bytes, bool masterscan, uint64_t uid) {
140 141 142
  bool existed = false;
  SET_RES_WINDOW_KEY(pRuntimeEnv->keyBuf, pData, bytes, uid);

L
Liu Jicong 已提交
143 144
  SResultRow** p1 =
      (SResultRow**)taosHashGet(pRuntimeEnv->pResultRowHashTable, pRuntimeEnv->keyBuf, GET_RES_WINDOW_KEY_LEN(bytes));
145 146 147 148 149 150 151 152 153 154 155

  // 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 已提交
156
        //        existed = (pResultRowInfo->pResult[0] == (*p1));
157 158
      } else {  // check if current pResultRowInfo contains the existed pResultRow
        SET_RES_EXT_WINDOW_KEY(pRuntimeEnv->keyBuf, pData, bytes, uid, pResultRowInfo);
L
Liu Jicong 已提交
159 160
        int64_t* index =
            taosHashGet(pRuntimeEnv->pResultRowListSet, pRuntimeEnv->keyBuf, GET_RES_EXT_WINDOW_KEY_LEN(bytes));
161 162 163 164 165 166 167 168 169 170 171 172 173
        if (index != NULL) {
          existed = true;
        } else {
          existed = false;
        }
      }
    }

    return existed;
  }

  return p1 != NULL;
}
174
#endif
175

176
SResultRow* getNewResultRow(SDiskbasedBuf* pResultBuf, int32_t* currentPageId, int32_t interBufSize) {
L
Liu Jicong 已提交
177
  SFilePage* pData = NULL;
178 179 180

  // in the first scan, new space needed for results
  int32_t pageId = -1;
181
  if (*currentPageId == -1) {
182
    pData = getNewBufPage(pResultBuf, &pageId);
183 184
    pData->num = sizeof(SFilePage);
  } else {
185 186
    pData = getBufPage(pResultBuf, *currentPageId);
    pageId = *currentPageId;
187

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

192
      pData = getNewBufPage(pResultBuf, &pageId);
193 194 195 196 197 198 199 200 201 202
      if (pData != NULL) {
        pData->num = sizeof(SFilePage);
      }
    }
  }

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

203 204
  setBufPageDirty(pData, true);

205 206 207 208
  // 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;
209
  *currentPageId = pageId;
210

wmmhello's avatar
wmmhello 已提交
211
  pData->num += interBufSize;
212 213 214
  return pResultRow;
}

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

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

230 231
  SResultRow* pResult = NULL;

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

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

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

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

266 267 268
  // 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 已提交
269
  // too many time window in query
270
  if (pTaskInfo->execModel == OPTR_EXEC_MODEL_BATCH &&
271
      tSimpleHashGetSize(pSup->pResultRowHashTable) > MAX_INTERVAL_TIME_WINDOW) {
272
    T_LONG_JMP(pTaskInfo->env, TSDB_CODE_QRY_TOO_MANY_TIMEWINDOW);
H
Haojun Liao 已提交
273 274
  }

H
Haojun Liao 已提交
275
  return pResult;
H
Haojun Liao 已提交
276 277
}

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

L
Liu Jicong 已提交
284
  SFilePage* pData = NULL;
285 286 287

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

  if (taosArrayGetSize(list) == 0) {
291
    pData = getNewBufPage(pResultBuf, &pageId);
292
    pData->num = sizeof(SFilePage);
293 294
  } else {
    SPageInfo* pi = getLastPageInfo(list);
295
    pData = getBufPage(pResultBuf, getPageId(pi));
296
    pageId = getPageId(pi);
297

298
    if (pData->num + size > getBufPageSize(pResultBuf)) {
299
      // release current page first, and prepare the next one
300
      releaseBufPageInfo(pResultBuf, pi);
301

302
      pData = getNewBufPage(pResultBuf, &pageId);
303
      if (pData != NULL) {
304
        pData->num = sizeof(SFilePage);
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
      }
    }
  }

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

325
//  query_range_start, query_range_end, window_duration, window_start, window_end
326
void initExecTimeWindowInfo(SColumnInfoData* pColData, STimeWindow* pQueryWindow) {
327 328 329
  pColData->info.type = TSDB_DATA_TYPE_TIMESTAMP;
  pColData->info.bytes = sizeof(int64_t);

H
Haojun Liao 已提交
330
  colInfoDataEnsureCapacity(pColData, 5, false);
331 332 333 334 335 336 337 338 339
  colDataAppendInt64(pColData, 0, &pQueryWindow->skey);
  colDataAppendInt64(pColData, 1, &pQueryWindow->ekey);

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

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

342 343 344 345 346 347 348 349 350 351 352 353 354 355
typedef struct {
  bool    hasAgg;
  int32_t numOfRows;
  int32_t startOffset;
} SFunctionCtxStatus;

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

static void functionCtxRestore(SqlFunctionCtx* pCtx, SFunctionCtxStatus* pStatus) {
  pCtx->input.colDataAggIsSet = pStatus->hasAgg;
H
Haojun Liao 已提交
356
  pCtx->input.numOfRows = pStatus->numOfRows;
357 358 359 360 361
  pCtx->input.startRowIndex = pStatus->startOffset;
}

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

367
    pCtx[k].input.startRowIndex = offset;
368
    pCtx[k].input.numOfRows = forwardStep;
369 370 371

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

376 377
    if (fmIsWindowPseudoColumnFunc(pCtx[k].functionId)) {
      SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(&pCtx[k]);
378 379

      char* p = GET_ROWCELL_INTERBUF(pEntryInfo);
380

381
      SColumnInfoData idata = {0};
dengyihao's avatar
dengyihao 已提交
382
      idata.info.type = TSDB_DATA_TYPE_BIGINT;
383
      idata.info.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
dengyihao's avatar
dengyihao 已提交
384
      idata.pData = p;
385 386 387 388

      SScalarParam out = {.columnData = &idata};
      SScalarParam tw = {.numOfRows = 5, .columnData = pTimeWindowData};
      pCtx[k].sfp.process(&tw, 1, &out);
389
      pEntryInfo->numOfRes = 1;
390 391 392 393 394 395 396 397
    } 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;
398
          T_LONG_JMP(taskInfo->env, code);
399
        }
400
      }
401

402
      // restore it
403
      functionCtxRestore(&pCtx[k], &status);
404
    }
405 406 407
  }
}

408 409
static int32_t doSetInputDataBlock(SExprSupp* pExprSup, SSDataBlock* pBlock, int32_t order, int32_t scanFlag,
                                   bool createDummyCol);
410

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

421
void setInputDataBlock(SExprSupp* pExprSup, SSDataBlock* pBlock, int32_t order, int32_t scanFlag, bool createDummyCol) {
422
  if (pBlock->pBlockAgg != NULL) {
423
    doSetInputDataBlockInfo(pExprSup, pBlock, order);
424
  } else {
425
    doSetInputDataBlock(pExprSup, pBlock, order, scanFlag, createDummyCol);
H
Haojun Liao 已提交
426
  }
427 428
}

L
Liu Jicong 已提交
429 430
static int32_t doCreateConstantValColumnInfo(SInputColumnInfoData* pInput, SFunctParam* pFuncParam, int32_t paramIndex,
                                             int32_t numOfRows) {
431 432 433 434 435 436 437 438
  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)
439 440
    pColInfo->info.type = pFuncParam->param.nType;
    pColInfo->info.bytes = pFuncParam->param.nLen;
441 442

    pInput->pData[paramIndex] = pColInfo;
443 444
  } else {
    pColInfo = pInput->pData[paramIndex];
445 446
  }

H
Haojun Liao 已提交
447
  colInfoDataEnsureCapacity(pColInfo, numOfRows, false);
448

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

  return TSDB_CODE_SUCCESS;
}

472 473
static int32_t doSetInputDataBlock(SExprSupp* pExprSup, SSDataBlock* pBlock, int32_t order, int32_t scanFlag,
                                   bool createDummyCol) {
474
  int32_t         code = TSDB_CODE_SUCCESS;
475
  SqlFunctionCtx* pCtx = pExprSup->pCtx;
476

477
  for (int32_t i = 0; i < pExprSup->numOfExprs; ++i) {
L
Liu Jicong 已提交
478
    pCtx[i].order = order;
479 480
    pCtx[i].input.numOfRows = pBlock->info.rows;

L
Liu Jicong 已提交
481
    pCtx[i].pSrcBlock = pBlock;
X
Xiaoyu Wang 已提交
482
    pCtx[i].scanFlag = scanFlag;
H
Haojun Liao 已提交
483

484
    SInputColumnInfoData* pInput = &pCtx[i].input;
485
    pInput->uid = pBlock->info.uid;
C
Cary Xu 已提交
486
    pInput->colDataAggIsSet = false;
487

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

498
        // NOTE: the last parameter is the primary timestamp column
H
Haojun Liao 已提交
499
        // todo: refactor this
500
        if (fmIsImplicitTsFunc(pCtx[i].functionId) && (j == pOneExpr->base.numOfParams - 1)) {
L
Liu Jicong 已提交
501
          pInput->pPTS = pInput->pData[j];  // in case of merge function, this is not always the ts column data.
502
          //          ASSERT(pInput->pPTS->info.type == TSDB_DATA_TYPE_TIMESTAMP);
503
        }
504 505
        ASSERT(pInput->pData[j] != NULL);
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
506 507 508
        // 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) {
509 510 511 512
          pInput->totalRows = pBlock->info.rows;
          pInput->numOfRows = pBlock->info.rows;
          pInput->startRowIndex = 0;

513
          code = doCreateConstantValColumnInfo(pInput, pFuncParam, j, pBlock->info.rows);
514 515 516
          if (code != TSDB_CODE_SUCCESS) {
            return code;
          }
517
        }
G
Ganlin Zhao 已提交
518 519
      }
    }
H
Haojun Liao 已提交
520
  }
521 522

  return code;
H
Haojun Liao 已提交
523 524
}

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

533 534 535 536
      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;
537
      }
538 539
    }
  }
540 541

  return TSDB_CODE_SUCCESS;
542 543
}

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

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

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

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

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

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

L
Liu Jicong 已提交
574 575 576 577
  if (pResult != pSrcBlock) {
    pResult->info.groupId = pSrcBlock->info.groupId;
    memcpy(pResult->info.parTbName, pSrcBlock->info.parTbName, TSDB_TABLE_NAME_LEN);
  }
H
Haojun Liao 已提交
578

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

586 587
  int32_t numOfRows = 0;

588
  for (int32_t k = 0; k < numOfOutput; ++k) {
589 590
    int32_t               outputSlotId = pExpr[k].base.resSchema.slotId;
    SqlFunctionCtx*       pfCtx = &pCtx[k];
591
    SInputColumnInfoData* pInputData = &pfCtx->input;
592

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

602
      numOfRows = pInputData->numOfRows;
603
    } else if (pExpr[k].pExpr->nodeType == QUERY_NODE_VALUE) {
604
      SColumnInfoData* pColInfoData = taosArrayGet(pResult->pDataBlock, outputSlotId);
605

dengyihao's avatar
dengyihao 已提交
606
      int32_t offset = createNewColModel ? 0 : pResult->info.rows;
607 608 609 610 611 612 613 614

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

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

622
      SColumnInfoData* pResColData = taosArrayGet(pResult->pDataBlock, outputSlotId);
623
      SColumnInfoData  idata = {.info = pResColData->info, .hasNull = true};
624

625
      SScalarParam dest = {.columnData = &idata};
X
Xiaoyu Wang 已提交
626
      int32_t      code = scalarCalculate(pExpr[k].pExpr->_optrRoot.pRootNode, pBlockList, &dest);
627 628 629 630
      if (code != TSDB_CODE_SUCCESS) {
        taosArrayDestroy(pBlockList);
        return code;
      }
631

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

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

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

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

657 658 659 660 661
        // link pDstBlock to set selectivity value
        if (pfCtx->subsidiaries.num > 0) {
          pfCtx->pDstBlock = pResult;
        }

662 663 664 665 666
        int32_t code = pfCtx->fpSet.process(pfCtx);
        if (code != TSDB_CODE_SUCCESS) {
          return code;
        }
        numOfRows = pResInfo->numOfRes;
H
Haojun Liao 已提交
667
      } else if (fmIsAggFunc(pfCtx->functionId)) {
G
Ganlin Zhao 已提交
668
        // selective value output should be set during corresponding function execution
669 670 671
        if (fmIsSelectValueFunc(pfCtx->functionId)) {
          continue;
        }
672 673
        // _group_key function for "partition by tbname" + csum(col_name) query
        SColumnInfoData* pOutput = taosArrayGet(pResult->pDataBlock, outputSlotId);
674
        int32_t          slotId = pfCtx->param[0].pCol->slotId;
675 676 677

        // todo handle the json tag
        SColumnInfoData* pInput = taosArrayGet(pSrcBlock->pDataBlock, slotId);
678
        for (int32_t f = 0; f < pSrcBlock->info.rows; ++f) {
679 680 681 682 683 684 685 686 687
          bool isNull = colDataIsNull_s(pInput, f);
          if (isNull) {
            colDataAppendNULL(pOutput, pResult->info.rows + f);
          } else {
            char* data = colDataGetData(pInput, f);
            colDataAppend(pOutput, pResult->info.rows + f, data, isNull);
          }
        }

H
Haojun Liao 已提交
688 689 690
      } else {
        SArray* pBlockList = taosArrayInit(4, POINTER_BYTES);
        taosArrayPush(pBlockList, &pSrcBlock);
G
Ganlin Zhao 已提交
691

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

695
        SScalarParam dest = {.columnData = &idata};
X
Xiaoyu Wang 已提交
696
        int32_t      code = scalarCalculate((SNode*)pExpr[k].pExpr->_function.pFunctNode, pBlockList, &dest);
697 698 699 700
        if (code != TSDB_CODE_SUCCESS) {
          taosArrayDestroy(pBlockList);
          return code;
        }
701

dengyihao's avatar
dengyihao 已提交
702
        int32_t startOffset = createNewColModel ? 0 : pResult->info.rows;
703
        ASSERT(pResult->info.capacity > 0);
704
        colDataMergeCol(pResColData, startOffset, (int32_t*)&pResult->info.capacity, &idata, dest.numOfRows);
D
dapan1121 已提交
705
        colDataDestroy(&idata);
706 707

        numOfRows = dest.numOfRows;
H
Haojun Liao 已提交
708 709
        taosArrayDestroy(pBlockList);
      }
710
    } else {
711
      return TSDB_CODE_OPS_NOT_SUPPORT;
712 713
    }
  }
714

715 716 717
  if (!createNewColModel) {
    pResult->info.rows += numOfRows;
  }
718 719

  return TSDB_CODE_SUCCESS;
720 721
}

5
54liuyao 已提交
722
bool functionNeedToExecute(SqlFunctionCtx* pCtx) {
723
  struct SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
724

725 726 727 728 729
  // in case of timestamp column, always generated results.
  int32_t functionId = pCtx->functionId;
  if (functionId == -1) {
    return false;
  }
730

731 732
  if (pCtx->scanFlag == REPEAT_SCAN) {
    return fmIsRepeatScanFunc(pCtx->functionId);
733 734
  }

735 736
  if (isRowEntryCompleted(pResInfo)) {
    return false;
737 738
  }

739 740 741
  return true;
}

742 743 744 745 746 747 748
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;
    }
749

750 751 752
    // Set the correct column info (data type and bytes)
    pInput->pData[paramIndex]->info.type = type;
    pInput->pData[paramIndex]->info.bytes = tDataTypes[type].bytes;
753
  }
H
Haojun Liao 已提交
754

755 756 757 758 759 760
  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;
761 762
    }
  } else {
763
    da = pInput->pColumnDataAgg[paramIndex];
764 765
  }

766
  ASSERT(!IS_VAR_DATA_TYPE(type));
767

768 769
  if (type == TSDB_DATA_TYPE_BIGINT) {
    int64_t v = pFuncParam->param.i;
770
    *da = (SColumnDataAgg){.numOfNull = 0, .min = v, .max = v, .sum = v * numOfRows};
771 772
  } else if (type == TSDB_DATA_TYPE_DOUBLE) {
    double v = pFuncParam->param.d;
773
    *da = (SColumnDataAgg){.numOfNull = 0};
774

775 776 777 778 779 780
    *(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;

781
    *da = (SColumnDataAgg){.numOfNull = 0};
782 783 784 785 786
    *(bool*)&da->min = 0;
    *(bool*)&da->max = v;
    *(bool*)&da->sum = v * numOfRows;
  } else if (type == TSDB_DATA_TYPE_TIMESTAMP) {
    // do nothing
787
  } else {
788
    ASSERT(0);
789 790
  }

791 792
  return TSDB_CODE_SUCCESS;
}
793

794
void setBlockSMAInfo(SqlFunctionCtx* pCtx, SExprInfo* pExprInfo, SSDataBlock* pBlock) {
795 796 797 798 799 800 801 802 803
  int32_t numOfRows = pBlock->info.rows;

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

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

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

807 808
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
        int32_t slotId = pFuncParam->pCol->slotId;
809 810 811 812
        pInput->pColumnDataAgg[j] = pBlock->pBlockAgg[slotId];
        if (pInput->pColumnDataAgg[j] == NULL) {
          pInput->colDataAggIsSet = false;
        }
813 814 815 816

        // 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);
817 818
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
        doCreateConstantValColumnAggInfo(pInput, pFuncParam, pFuncParam->param.nType, j, pBlock->info.rows);
819 820
      }
    }
821
  } else {
822
    pInput->colDataAggIsSet = false;
823 824 825
  }
}

L
Liu Jicong 已提交
826
bool isTaskKilled(SExecTaskInfo* pTaskInfo) {
827 828
  // query has been executed more than tsShellActivityTimer, and the retrieve has not arrived
  // abort current query execution.
L
Liu Jicong 已提交
829 830
  if (pTaskInfo->owner != 0 &&
      ((taosGetTimestampSec() - pTaskInfo->cost.start / 1000) > 10 * getMaximumIdleDurationSec())
831
      /*(!needBuildResAfterQueryComplete(pTaskInfo))*/) {
832
    assert(pTaskInfo->cost.start != 0);
L
Liu Jicong 已提交
833 834 835
    //    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;
836 837 838 839 840
  }

  return false;
}

L
Liu Jicong 已提交
841
void setTaskKilled(SExecTaskInfo* pTaskInfo) { pTaskInfo->code = TSDB_CODE_TSC_QUERY_CANCELLED; }
842 843

/////////////////////////////////////////////////////////////////////////////////////////////
844
STimeWindow getAlignQueryTimeWindow(SInterval* pInterval, int32_t precision, int64_t key) {
L
Liu Jicong 已提交
845
  STimeWindow win = {0};
846
  win.skey = taosTimeTruncate(key, pInterval, precision);
847 848

  /*
H
Haojun Liao 已提交
849
   * if the realSkey > INT64_MAX - pInterval->interval, the query duration between
850 851
   * realSkey and realEkey must be less than one interval.Therefore, no need to adjust the query ranges.
   */
852 853 854
  win.ekey = taosTimeAdd(win.skey, pInterval->interval, pInterval->intervalUnit, precision) - 1;
  if (win.ekey < win.skey) {
    win.ekey = INT64_MAX;
855
  }
856 857

  return win;
858 859
}

L
Liu Jicong 已提交
860 861
int32_t loadDataBlockOnDemand(SExecTaskInfo* pTaskInfo, STableScanInfo* pTableScanInfo, SSDataBlock* pBlock,
                              uint32_t* status) {
862
  *status = BLK_DATA_NOT_LOAD;
863

H
Haojun Liao 已提交
864
  pBlock->pDataBlock = NULL;
L
Liu Jicong 已提交
865
  pBlock->pBlockAgg = NULL;
H
Haojun Liao 已提交
866

L
Liu Jicong 已提交
867 868
  //  int64_t groupId = pRuntimeEnv->current->groupIndex;
  //  bool    ascQuery = QUERY_IS_ASC_QUERY(pQueryAttr);
869

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

872 873
//  pCost->totalBlocks += 1;
//  pCost->totalRows += pBlock->info.rows;
H
Haojun Liao 已提交
874
#if 0
875 876 877
  // 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 已提交
878
      (QUERY_IS_INTERVAL_QUERY(pQueryAttr) && overlapWithTimeWindow(pTaskInfo, &pBlock->info))) {
879
    (*status) = BLK_DATA_DATA_LOAD;
880 881 882
  }

  // check if this data block is required to load
883
  if ((*status) != BLK_DATA_DATA_LOAD) {
884 885 886 887 888 889 890
    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 已提交
891
      bool  masterScan = IS_MAIN_SCAN(pRuntimeEnv);
892 893 894 895 896 897
      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,
898
                                    pTableScanInfo->rowEntryInfoOffset);
899 900 901
      } else {
        if (setResultOutputBufByKey(pRuntimeEnv, pTableScanInfo->pResultRowInfo, pBlock->info.uid, &win, masterScan, &pResult, groupId,
                                    pTableScanInfo->pCtx, pTableScanInfo->numOfOutput,
902
                                    pTableScanInfo->rowEntryInfoOffset) != TSDB_CODE_SUCCESS) {
903
          T_LONG_JMP(pRuntimeEnv->env, TSDB_CODE_QRY_OUT_OF_MEMORY);
904 905 906 907
        }
      }
    } else if (pQueryAttr->stableQuery && (!pQueryAttr->tsCompQuery) && (!pQueryAttr->diffQuery)) { // stable aggregate, not interval aggregate or normal column aggregate
      doSetTableGroupOutputBuf(pRuntimeEnv, pTableScanInfo->pResultRowInfo, pTableScanInfo->pCtx,
908
                               pTableScanInfo->rowEntryInfoOffset, pTableScanInfo->numOfOutput,
909 910 911 912 913 914
                               pRuntimeEnv->current->groupIndex);
    }

    if (needFilter) {
      (*status) = doFilterByBlockTimeWindow(pTableScanInfo, pBlock);
    } else {
915
      (*status) = BLK_DATA_DATA_LOAD;
916 917 918 919
    }
  }

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

922
  if ((*status) == BLK_DATA_NOT_LOAD || (*status) == BLK_DATA_FILTEROUT) {
923 924
    //qDebug("QInfo:0x%"PRIx64" data block discard, brange:%" PRId64 "-%" PRId64 ", rows:%d", pQInfo->qId, pBlockInfo->window.skey,
//           pBlockInfo->window.ekey, pBlockInfo->rows);
925
    pCost->skipBlocks += 1;
926
  } else if ((*status) == BLK_DATA_SMA_LOAD) {
927 928
    // this function never returns error?
    pCost->loadBlockStatis += 1;
929
//    tsdbRetrieveDatablockSMA(pTableScanInfo->pTsdbReadHandle, &pBlock->pBlockAgg);
930 931

    if (pBlock->pBlockAgg == NULL) {  // data block statistics does not exist, load data block
932
//      pBlock->pDataBlock = tsdbRetrieveDataBlock(pTableScanInfo->pTsdbReadHandle, NULL);
933 934 935
      pCost->totalCheckedRows += pBlock->info.rows;
    }
  } else {
936
    assert((*status) == BLK_DATA_DATA_LOAD);
937 938 939

    // load the data block statistics to perform further filter
    pCost->loadBlockStatis += 1;
940
//    tsdbRetrieveDatablockSMA(pTableScanInfo->pTsdbReadHandle, &pBlock->pBlockAgg);
941 942 943 944 945 946

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

H
Haojun Liao 已提交
947
          bool  masterScan = IS_MAIN_SCAN(pRuntimeEnv);
948 949 950 951 952
          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,
953
                                      pTableScanInfo->rowEntryInfoOffset) != TSDB_CODE_SUCCESS) {
954
            T_LONG_JMP(pRuntimeEnv->env, TSDB_CODE_QRY_OUT_OF_MEMORY);
955 956 957 958 959 960 961 962 963 964
          }
        }
      }
      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
965
            pCost->skipBlocks += 1;
966 967
            //qDebug("QInfo:0x%"PRIx64" data block discard, brange:%" PRId64 "-%" PRId64 ", rows:%d", pQInfo->qId,
//                   pBlockInfo->window.skey, pBlockInfo->window.ekey, pBlockInfo->rows);
968
            (*status) = BLK_DATA_FILTEROUT;
969 970 971 972 973 974 975
            return TSDB_CODE_SUCCESS;
          }
        }
      }
    }

    // current block has been discard due to filter applied
H
Haojun Liao 已提交
976
//    if (!doFilterByBlockSMA(pRuntimeEnv, pBlock->pBlockAgg, pTableScanInfo->pCtx, pBlockInfo->rows)) {
977
//      pCost->skipBlocks += 1;
978 979
//      qDebug("QInfo:0x%"PRIx64" data block discard, brange:%" PRId64 "-%" PRId64 ", rows:%d", pQInfo->qId, pBlockInfo->window.skey,
//             pBlockInfo->window.ekey, pBlockInfo->rows);
980
//      (*status) = BLK_DATA_FILTEROUT;
981 982 983 984 985
//      return TSDB_CODE_SUCCESS;
//    }

    pCost->totalCheckedRows += pBlockInfo->rows;
    pCost->loadBlocks += 1;
986
//    pBlock->pDataBlock = tsdbRetrieveDataBlock(pTableScanInfo->pTsdbReadHandle, NULL);
987 988 989 990 991
//    if (pBlock->pDataBlock == NULL) {
//      return terrno;
//    }

//    if (pQueryAttr->pFilters != NULL) {
992
//      filterSetColFieldData(pQueryAttr->pFilters, taosArrayGetSize(pBlock->pDataBlock), pBlock->pDataBlock);
993
//    }
994

995 996 997 998
//    if (pQueryAttr->pFilters != NULL || pRuntimeEnv->pTsBuf != NULL) {
//      filterColRowsInDataBlock(pRuntimeEnv, pBlock, ascQuery);
//    }
  }
H
Haojun Liao 已提交
999
#endif
1000 1001 1002
  return TSDB_CODE_SUCCESS;
}

L
Liu Jicong 已提交
1003
static void updateTableQueryInfoForReverseScan(STableQueryInfo* pTableQueryInfo) {
1004 1005 1006 1007 1008
  if (pTableQueryInfo == NULL) {
    return;
  }
}

L
Liu Jicong 已提交
1009
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
1010
  if (status == TASK_NOT_COMPLETED) {
H
Haojun Liao 已提交
1011
    pTaskInfo->status = status;
1012 1013
  } else {
    // QUERY_NOT_COMPLETED is not compatible with any other status, so clear its position first
1014
    CLEAR_QUERY_STATUS(pTaskInfo, TASK_NOT_COMPLETED);
H
Haojun Liao 已提交
1015
    pTaskInfo->status |= status;
1016 1017 1018
  }
}

1019
void setResultRowInitCtx(SResultRow* pResult, SqlFunctionCtx* pCtx, int32_t numOfOutput, int32_t* rowEntryInfoOffset) {
5
54liuyao 已提交
1020
  bool init = false;
1021
  for (int32_t i = 0; i < numOfOutput; ++i) {
1022
    pCtx[i].resultInfo = getResultEntryInfo(pResult, i, rowEntryInfoOffset);
5
54liuyao 已提交
1023 1024 1025
    if (init) {
      continue;
    }
1026 1027 1028 1029 1030

    struct SResultRowEntryInfo* pResInfo = pCtx[i].resultInfo;
    if (isRowEntryCompleted(pResInfo) && isRowEntryInitialized(pResInfo)) {
      continue;
    }
1031 1032 1033 1034 1035

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

1036 1037 1038 1039 1040 1041
    if (!pResInfo->initialized) {
      if (pCtx[i].functionId != -1) {
        pCtx[i].fpSet.init(&pCtx[i], pResInfo);
      } else {
        pResInfo->initialized = true;
      }
5
54liuyao 已提交
1042 1043
    } else {
      init = true;
1044 1045 1046 1047
    }
  }
}

1048 1049
static void extractQualifiedTupleByFilterResult(SSDataBlock* pBlock, const SColumnInfoData* p, bool keep,
                                                int32_t status);
1050

H
Haojun Liao 已提交
1051 1052
void doFilter(SSDataBlock* pBlock, SFilterInfo* pFilterInfo, SColMatchInfo* pColMatchInfo) {
  if (pFilterInfo == NULL || pBlock->info.rows == 0) {
S
shenglian zhou 已提交
1053 1054
    return;
  }
1055

1056
  SFilterColumnParam param1 = {.numOfCols = taosArrayGetSize(pBlock->pDataBlock), .pDataBlock = pBlock->pDataBlock};
1057
  int32_t            code = filterSetDataFromSlotId(pFilterInfo, &param1);
1058

1059
  SColumnInfoData* p = NULL;
1060
  int32_t          status = 0;
H
Haojun Liao 已提交
1061

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

1066
  if (pColMatchInfo != NULL) {
1067
    size_t size = taosArrayGetSize(pColMatchInfo->pList);
H
Haojun Liao 已提交
1068
    for (int32_t i = 0; i < size; ++i) {
H
Haojun Liao 已提交
1069
      SColMatchItem* pInfo = taosArrayGet(pColMatchInfo->pList, i);
1070
      if (pInfo->colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
H
Haojun Liao 已提交
1071
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, pInfo->dstSlotId);
1072
        if (pColData->info.type == TSDB_DATA_TYPE_TIMESTAMP) {
H
Haojun Liao 已提交
1073
          blockDataUpdateTsWindow(pBlock, pInfo->dstSlotId);
1074 1075 1076 1077 1078 1079
          break;
        }
      }
    }
  }

1080 1081
  colDataDestroy(p);
  taosMemoryFree(p);
1082 1083
}

1084
void extractQualifiedTupleByFilterResult(SSDataBlock* pBlock, const SColumnInfoData* p, bool keep, int32_t status) {
1085 1086 1087 1088
  if (keep) {
    return;
  }

H
Haojun Liao 已提交
1089 1090 1091
  int32_t totalRows = pBlock->info.rows;

  if (status == FILTER_RESULT_ALL_QUALIFIED) {
1092
    // here nothing needs to be done
H
Haojun Liao 已提交
1093
  } else if (status == FILTER_RESULT_NONE_QUALIFIED) {
1094
    pBlock->info.rows = 0;
H
Haojun Liao 已提交
1095
  } else {
1096
    SSDataBlock* px = createOneDataBlock(pBlock, true);
1097

1098 1099
    size_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
    for (int32_t i = 0; i < numOfCols; ++i) {
1100 1101
      SColumnInfoData* pSrc = taosArrayGet(px->pDataBlock, i);
      SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, i);
1102
      // it is a reserved column for scalar function, and no data in this column yet.
1103
      if (pDst->pData == NULL || pSrc->pData == NULL) {
1104 1105 1106
        continue;
      }

1107 1108
      colInfoDataCleanup(pDst, pBlock->info.rows);

1109
      int32_t numOfRows = 0;
1110
      for (int32_t j = 0; j < totalRows; ++j) {
1111
        if (((int8_t*)p->pData)[j] == 0) {
D
dapan1121 已提交
1112 1113
          continue;
        }
1114

D
dapan1121 已提交
1115
        if (colDataIsNull_s(pSrc, j)) {
1116
          colDataAppendNULL(pDst, numOfRows);
D
dapan1121 已提交
1117
        } else {
1118
          colDataAppend(pDst, numOfRows, colDataGetData(pSrc, j), false);
D
dapan1121 已提交
1119
        }
1120
        numOfRows += 1;
H
Haojun Liao 已提交
1121
      }
1122

1123
      // todo this value can be assigned directly
1124 1125 1126 1127 1128
      if (pBlock->info.rows == totalRows) {
        pBlock->info.rows = numOfRows;
      } else {
        ASSERT(pBlock->info.rows == numOfRows);
      }
1129
    }
1130

dengyihao's avatar
dengyihao 已提交
1131
    blockDataDestroy(px);  // fix memory leak
1132 1133 1134
  }
}

1135
void doSetTableGroupOutputBuf(SOperatorInfo* pOperator, int32_t numOfOutput, uint64_t groupId) {
1136
  // for simple group by query without interval, all the tables belong to one group result.
1137 1138 1139
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
  SAggOperatorInfo* pAggInfo = pOperator->info;

1140
  SResultRowInfo* pResultRowInfo = &pAggInfo->binfo.resultRowInfo;
1141 1142
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
  int32_t*        rowEntryInfoOffset = pOperator->exprSupp.rowEntryInfoOffset;
1143

1144
  SResultRow* pResultRow = doSetResultOutBufByKey(pAggInfo->aggSup.pResultBuf, pResultRowInfo, (char*)&groupId,
L
Liu Jicong 已提交
1145
                                                  sizeof(groupId), true, groupId, pTaskInfo, false, &pAggInfo->aggSup);
L
Liu Jicong 已提交
1146
  assert(pResultRow != NULL);
1147 1148 1149 1150 1151 1152

  /*
   * 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 已提交
1153 1154
    int32_t ret =
        addNewWindowResultBuf(pResultRow, pAggInfo->aggSup.pResultBuf, groupId, pAggInfo->binfo.pRes->info.rowSize);
1155 1156 1157 1158 1159
    if (ret != TSDB_CODE_SUCCESS) {
      return;
    }
  }

1160
  setResultRowInitCtx(pResultRow, pCtx, numOfOutput, rowEntryInfoOffset);
1161 1162
}

1163 1164 1165
static void setExecutionContext(SOperatorInfo* pOperator, int32_t numOfOutput, uint64_t groupId) {
  SAggOperatorInfo* pAggInfo = pOperator->info;
  if (pAggInfo->groupId != UINT64_MAX && pAggInfo->groupId == groupId) {
1166 1167
    return;
  }
1168 1169

  doSetTableGroupOutputBuf(pOperator, numOfOutput, groupId);
1170 1171

  // record the current active group id
H
Haojun Liao 已提交
1172
  pAggInfo->groupId = groupId;
1173 1174
}

dengyihao's avatar
dengyihao 已提交
1175 1176
static void doUpdateNumOfRows(SqlFunctionCtx* pCtx, SResultRow* pRow, int32_t numOfExprs,
                              const int32_t* rowCellOffset) {
1177
  bool returnNotNull = false;
1178
  for (int32_t j = 0; j < numOfExprs; ++j) {
1179
    struct SResultRowEntryInfo* pResInfo = getResultEntryInfo(pRow, j, rowCellOffset);
1180 1181 1182 1183 1184 1185 1186
    if (!isRowEntryInitialized(pResInfo)) {
      continue;
    }

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

1188
    if (fmIsNotNullOutputFunc(pCtx[j].functionId)) {
1189 1190
      returnNotNull = true;
    }
1191
  }
S
shenglian zhou 已提交
1192 1193
  // if all expr skips all blocks, e.g. all null inputs for max function, output one row in final result.
  //  except for first/last, which require not null output, output no rows
1194
  if (pRow->numOfRows == 0 && !returnNotNull) {
1195
    pRow->numOfRows = 1;
1196 1197 1198
  }
}

1199 1200
static void doCopyResultToDataBlock(SExprInfo* pExprInfo, int32_t numOfExprs, SResultRow* pRow, SqlFunctionCtx* pCtx,
                                    SSDataBlock* pBlock, const int32_t* rowEntryOffset, SExecTaskInfo* pTaskInfo) {
1201 1202 1203
  for (int32_t j = 0; j < numOfExprs; ++j) {
    int32_t slotId = pExprInfo[j].base.resSchema.slotId;

1204
    pCtx[j].resultInfo = getResultEntryInfo(pRow, j, rowEntryOffset);
1205
    if (pCtx[j].fpSet.finalize) {
1206
      if (strcmp(pCtx[j].pExpr->pExpr->_function.functionName, "_group_key") == 0) {
1207 1208
        // for groupkey along with functions that output multiple lines(e.g. Histogram)
        // need to match groupkey result for each output row of that function.
1209 1210 1211 1212 1213
        if (pCtx[j].resultInfo->numOfRes != 0) {
          pCtx[j].resultInfo->numOfRes = pRow->numOfRows;
        }
      }

1214 1215 1216
      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));
1217
        T_LONG_JMP(pTaskInfo->env, code);
1218 1219
      }
    } else if (strcmp(pCtx[j].pExpr->pExpr->_function.functionName, "_select_value") == 0) {
1220
      // do nothing
1221
    } else {
1222 1223
      // expand the result into multiple rows. E.g., _wstart, top(k, 20)
      // the _wstart needs to copy to 20 following rows, since the results of top-k expands to 20 different rows.
1224 1225 1226 1227 1228 1229 1230
      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);
      }
    }
  }
1231 1232
}

1233 1234 1235
// todo refactor. SResultRow has direct pointer in miainfo
int32_t finalizeResultRows(SDiskbasedBuf* pBuf, SResultRowPosition* resultRowPosition, SExprSupp* pSup,
                           SSDataBlock* pBlock, SExecTaskInfo* pTaskInfo) {
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
  SFilePage*  page = getBufPage(pBuf, resultRowPosition->pageId);
  SResultRow* pRow = (SResultRow*)((char*)page + resultRowPosition->offset);

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

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

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

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

  doCopyResultToDataBlock(pExprInfo, pSup->numOfExprs, pRow, pCtx, pBlock, rowEntryOffset, pTaskInfo);
1262 1263

  releaseBufPage(pBuf, page);
1264
  pBlock->info.rows += pRow->numOfRows;
1265 1266 1267
  return 0;
}

1268 1269 1270 1271 1272 1273 1274
int32_t doCopyToSDataBlock(SExecTaskInfo* pTaskInfo, SSDataBlock* pBlock, SExprSupp* pSup, SDiskbasedBuf* pBuf,
                           SGroupResInfo* pGroupResInfo) {
  SExprInfo*      pExprInfo = pSup->pExprInfo;
  int32_t         numOfExprs = pSup->numOfExprs;
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
  SqlFunctionCtx* pCtx = pSup->pCtx;

1275
  int32_t numOfRows = getNumOfTotalRes(pGroupResInfo);
1276

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

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

H
Haojun Liao 已提交
1283
    doUpdateNumOfRows(pCtx, pRow, numOfExprs, rowEntryOffset);
1284 1285

    // no results, continue to check the next one
1286 1287
    if (pRow->numOfRows == 0) {
      pGroupResInfo->index += 1;
1288
      releaseBufPage(pBuf, page);
1289 1290 1291
      continue;
    }

1292 1293 1294 1295 1296
    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) {
1297
        releaseBufPage(pBuf, page);
1298 1299 1300 1301
        break;
      }
    }

1302
    if (pBlock->info.rows + pRow->numOfRows > pBlock->info.capacity) {
1303
      ASSERT(pBlock->info.rows > 0);
1304
      releaseBufPage(pBuf, page);
1305 1306 1307 1308
      break;
    }

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

1311
    releaseBufPage(pBuf, page);
1312
    pBlock->info.rows += pRow->numOfRows;
1313 1314
  }

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

1318
  blockDataUpdateTsWindow(pBlock, 0);
1319 1320 1321
  return 0;
}

1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
void doBuildStreamResBlock(SOperatorInfo* pOperator, SOptrBasicInfo* pbInfo, SGroupResInfo* pGroupResInfo,
                           SDiskbasedBuf* pBuf) {
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
  SSDataBlock*   pBlock = pbInfo->pRes;

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

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

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

1340 1341 1342 1343 1344
  void* tbname = NULL;
  if (streamStateGetParName(pTaskInfo->streamInfo.pState, pBlock->info.groupId, &tbname) < 0) {
    pBlock->info.parTbName[0] = 0;
  } else {
    memcpy(pBlock->info.parTbName, tbname, TSDB_TABLE_NAME_LEN);
1345
  }
1346
  tdbFree(tbname);
1347 1348
}

X
Xiaoyu Wang 已提交
1349 1350
void doBuildResultDatablock(SOperatorInfo* pOperator, SOptrBasicInfo* pbInfo, SGroupResInfo* pGroupResInfo,
                            SDiskbasedBuf* pBuf) {
1351
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
1352
  SSDataBlock*   pBlock = pbInfo->pRes;
1353

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

1357
  blockDataCleanup(pBlock);
1358
  if (!hasRemainResults(pGroupResInfo)) {
1359 1360 1361
    return;
  }

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

1373 1374
      // clearing group id to continue to merge data that belong to different groups
      pBlock->info.groupId = 0;
1375
    }
1376 1377 1378

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

L
Liu Jicong 已提交
1382 1383
void queryCostStatis(SExecTaskInfo* pTaskInfo) {
  STaskCostInfo* pSummary = &pTaskInfo->cost;
1384

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

L
Liu Jicong 已提交
1396 1397 1398
// static void updateOffsetVal(STaskRuntimeEnv *pRuntimeEnv, SDataBlockInfo *pBlockInfo) {
//   STaskAttr *pQueryAttr = pRuntimeEnv->pQueryAttr;
//   STableQueryInfo* pTableQueryInfo = pRuntimeEnv->current;
1399
//
L
Liu Jicong 已提交
1400
//   int32_t step = GET_FORWARD_DIRECTION_FACTOR(pQueryAttr->order.order);
1401
//
L
Liu Jicong 已提交
1402 1403 1404 1405
//   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;
//   }
1406
//
L
Liu Jicong 已提交
1407 1408 1409 1410 1411
//   if (QUERY_IS_ASC_QUERY(pQueryAttr)) {
//     pQueryAttr->pos = (int32_t)pQueryAttr->limit.offset;
//   } else {
//     pQueryAttr->pos = pBlockInfo->rows - (int32_t)pQueryAttr->limit.offset - 1;
//   }
1412
//
L
Liu Jicong 已提交
1413
//   assert(pQueryAttr->pos >= 0 && pQueryAttr->pos <= pBlockInfo->rows - 1);
1414
//
L
Liu Jicong 已提交
1415 1416
//   SArray *         pDataBlock = tsdbRetrieveDataBlock(pRuntimeEnv->pTsdbReadHandle, NULL);
//   SColumnInfoData *pColInfoData = taosArrayGet(pDataBlock, 0);
1417
//
L
Liu Jicong 已提交
1418 1419
//   // update the pQueryAttr->limit.offset value, and pQueryAttr->pos value
//   TSKEY *keys = (TSKEY *) pColInfoData->pData;
1420
//
L
Liu Jicong 已提交
1421 1422 1423
//   // update the offset value
//   pTableQueryInfo->lastKey = keys[pQueryAttr->pos];
//   pQueryAttr->limit.offset = 0;
1424
//
L
Liu Jicong 已提交
1425
//   int32_t numOfRes = tableApplyFunctionsOnBlock(pRuntimeEnv, pBlockInfo, NULL, binarySearchForKey, pDataBlock);
1426
//
L
Liu Jicong 已提交
1427 1428 1429 1430
//   //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);
// }
1431

L
Liu Jicong 已提交
1432 1433
// void skipBlocks(STaskRuntimeEnv *pRuntimeEnv) {
//   STaskAttr *pQueryAttr = pRuntimeEnv->pQueryAttr;
1434
//
L
Liu Jicong 已提交
1435 1436 1437
//   if (pQueryAttr->limit.offset <= 0 || pQueryAttr->numOfFilterCols > 0) {
//     return;
//   }
1438
//
L
Liu Jicong 已提交
1439 1440
//   pQueryAttr->pos = 0;
//   int32_t step = GET_FORWARD_DIRECTION_FACTOR(pQueryAttr->order.order);
1441
//
L
Liu Jicong 已提交
1442 1443
//   STableQueryInfo* pTableQueryInfo = pRuntimeEnv->current;
//   TsdbQueryHandleT pTsdbReadHandle = pRuntimeEnv->pTsdbReadHandle;
1444
//
L
Liu Jicong 已提交
1445 1446 1447
//   SDataBlockInfo blockInfo = SDATA_BLOCK_INITIALIZER;
//   while (tsdbNextDataBlock(pTsdbReadHandle)) {
//     if (isTaskKilled(pRuntimeEnv->qinfo)) {
1448
//       T_LONG_JMP(pRuntimeEnv->env, TSDB_CODE_TSC_QUERY_CANCELLED);
L
Liu Jicong 已提交
1449
//     }
1450
//
L
Liu Jicong 已提交
1451
//     tsdbRetrieveDataBlockInfo(pTsdbReadHandle, &blockInfo);
1452
//
L
Liu Jicong 已提交
1453 1454 1455 1456
//     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;
1457
//
L
Liu Jicong 已提交
1458 1459 1460 1461 1462 1463 1464
//       //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;
//     }
//   }
1465
//
L
Liu Jicong 已提交
1466
//   if (terrno != TSDB_CODE_SUCCESS) {
1467
//     T_LONG_JMP(pRuntimeEnv->env, terrno);
L
Liu Jicong 已提交
1468 1469 1470 1471 1472 1473 1474
//   }
// }

// static TSKEY doSkipIntervalProcess(STaskRuntimeEnv* pRuntimeEnv, STimeWindow* win, SDataBlockInfo* pBlockInfo,
// STableQueryInfo* pTableQueryInfo) {
//   STaskAttr *pQueryAttr = pRuntimeEnv->pQueryAttr;
//   SResultRowInfo *pWindowResInfo = &pRuntimeEnv->resultRowInfo;
1475
//
L
Liu Jicong 已提交
1476 1477 1478
//   assert(pQueryAttr->limit.offset == 0);
//   STimeWindow tw = *win;
//   getNextTimeWindow(pQueryAttr, &tw);
1479
//
L
Liu Jicong 已提交
1480 1481
//   if ((tw.skey <= pBlockInfo->window.ekey && QUERY_IS_ASC_QUERY(pQueryAttr)) ||
//       (tw.ekey >= pBlockInfo->window.skey && !QUERY_IS_ASC_QUERY(pQueryAttr))) {
1482
//
L
Liu Jicong 已提交
1483 1484 1485 1486
//     // 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);
1487
//
L
Liu Jicong 已提交
1488 1489 1490 1491
//     tw = *win;
//     int32_t startPos =
//         getNextQualifiedWindow(pQueryAttr, &tw, pBlockInfo, pColInfoData->pData, binarySearchForKey, -1);
//     assert(startPos >= 0);
1492
//
L
Liu Jicong 已提交
1493 1494
//     // set the abort info
//     pQueryAttr->pos = startPos;
1495
//
L
Liu Jicong 已提交
1496 1497 1498 1499
//     // reset the query start timestamp
//     pTableQueryInfo->win.skey = ((TSKEY *)pColInfoData->pData)[startPos];
//     pQueryAttr->window.skey = pTableQueryInfo->win.skey;
//     TSKEY key = pTableQueryInfo->win.skey;
1500
//
L
Liu Jicong 已提交
1501 1502
//     pWindowResInfo->prevSKey = tw.skey;
//     int32_t index = pRuntimeEnv->resultRowInfo.curIndex;
1503
//
L
Liu Jicong 已提交
1504 1505
//     int32_t numOfRes = tableApplyFunctionsOnBlock(pRuntimeEnv, pBlockInfo, NULL, binarySearchForKey, pDataBlock);
//     pRuntimeEnv->resultRowInfo.curIndex = index;  // restore the window index
1506
//
L
Liu Jicong 已提交
1507 1508 1509 1510
//     //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);
1511
//
L
Liu Jicong 已提交
1512 1513 1514 1515 1516
//     return key;
//   } else {  // do nothing
//     pQueryAttr->window.skey      = tw.skey;
//     pWindowResInfo->prevSKey = tw.skey;
//     pTableQueryInfo->lastKey = tw.skey;
1517
//
L
Liu Jicong 已提交
1518 1519
//     return tw.skey;
//   }
1520
//
L
Liu Jicong 已提交
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
//   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);
//   }
1531
//
L
Liu Jicong 已提交
1532 1533 1534 1535 1536
//   // 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;
//   }
1537
//
L
Liu Jicong 已提交
1538 1539 1540 1541 1542 1543 1544
//   /*
//    * 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);
1545
//
L
Liu Jicong 已提交
1546 1547
//   STimeWindow w = TSWINDOW_INITIALIZER;
//   bool ascQuery = QUERY_IS_ASC_QUERY(pQueryAttr);
1548
//
L
Liu Jicong 已提交
1549 1550
//   SResultRowInfo *pWindowResInfo = &pRuntimeEnv->resultRowInfo;
//   STableQueryInfo *pTableQueryInfo = pRuntimeEnv->current;
1551
//
L
Liu Jicong 已提交
1552 1553 1554
//   SDataBlockInfo blockInfo = SDATA_BLOCK_INITIALIZER;
//   while (tsdbNextDataBlock(pRuntimeEnv->pTsdbReadHandle)) {
//     tsdbRetrieveDataBlockInfo(pRuntimeEnv->pTsdbReadHandle, &blockInfo);
1555
//
L
Liu Jicong 已提交
1556 1557 1558 1559 1560 1561 1562 1563 1564
//     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;
//     }
1565
//
L
Liu Jicong 已提交
1566 1567
//     // the first time window
//     STimeWindow win = getActiveTimeWindow(pWindowResInfo, pWindowResInfo->prevSKey, pQueryAttr);
1568
//
L
Liu Jicong 已提交
1569 1570
//     while (pQueryAttr->limit.offset > 0) {
//       STimeWindow tw = win;
1571
//
L
Liu Jicong 已提交
1572 1573 1574
//       if ((win.ekey <= blockInfo.window.ekey && ascQuery) || (win.ekey >= blockInfo.window.skey && !ascQuery)) {
//         pQueryAttr->limit.offset -= 1;
//         pWindowResInfo->prevSKey = win.skey;
1575
//
L
Liu Jicong 已提交
1576 1577 1578 1579 1580 1581
//         // 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;
//         }
//       }
1582
//
L
Liu Jicong 已提交
1583 1584 1585 1586
//       if (pQueryAttr->limit.offset == 0) {
//         *start = doSkipIntervalProcess(pRuntimeEnv, &win, &blockInfo, pTableQueryInfo);
//         return true;
//       }
1587
//
L
Liu Jicong 已提交
1588 1589
//       // current window does not ended in current data block, try next data block
//       getNextTimeWindow(pQueryAttr, &tw);
1590
//
L
Liu Jicong 已提交
1591 1592 1593 1594 1595 1596 1597 1598 1599
//       /*
//        * 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)) {
1600
//
L
Liu Jicong 已提交
1601 1602
//         SArray *pDataBlock = tsdbRetrieveDataBlock(pRuntimeEnv->pTsdbReadHandle, NULL);
//         SColumnInfoData *pColInfoData = taosArrayGet(pDataBlock, 0);
1603
//
L
Liu Jicong 已提交
1604 1605 1606
//         if ((win.ekey > blockInfo.window.ekey && ascQuery) || (win.ekey < blockInfo.window.skey && !ascQuery)) {
//           pQueryAttr->limit.offset -= 1;
//         }
1607
//
L
Liu Jicong 已提交
1608 1609 1610 1611 1612 1613 1614 1615
//         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);
1616
//
L
Liu Jicong 已提交
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
//           // 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.
//       }
//     }
//   }
1628
//
L
Liu Jicong 已提交
1629 1630
//   // check for error
//   if (terrno != TSDB_CODE_SUCCESS) {
1631
//     T_LONG_JMP(pRuntimeEnv->env, terrno);
L
Liu Jicong 已提交
1632
//   }
1633
//
L
Liu Jicong 已提交
1634 1635
//   return true;
// }
1636

1637
int32_t appendDownstream(SOperatorInfo* p, SOperatorInfo** pDownstream, int32_t num) {
H
Haojun Liao 已提交
1638
  if (p->pDownstream == NULL) {
H
Haojun Liao 已提交
1639
    assert(p->numOfDownstream == 0);
1640 1641
  }

wafwerar's avatar
wafwerar 已提交
1642
  p->pDownstream = taosMemoryCalloc(1, num * POINTER_BYTES);
1643 1644 1645 1646 1647 1648 1649
  if (p->pDownstream == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

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

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

L
Liu Jicong 已提交
1655
static bool needToMerge(SSDataBlock* pBlock, SArray* groupInfo, char** buf, int32_t rowIndex) {
1656 1657 1658 1659
  size_t size = taosArrayGetSize(groupInfo);
  if (size == 0) {
    return true;
  }
1660

1661 1662
  for (int32_t i = 0; i < size; ++i) {
    int32_t* index = taosArrayGet(groupInfo, i);
1663

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

1667 1668 1669
    if ((isNull && buf[i] != NULL) || (!isNull && buf[i] == NULL)) {
      return false;
    }
1670

1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
    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;
      }
1684 1685 1686
    }
  }

1687
  return 0;
1688 1689
}

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

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

1697 1698 1699
    char* data = colDataGetData(pColInfo, rowIndex);
    memcpy(rowColData[i], data, colDataGetLength(pColInfo, rowIndex));
  }
1700

1701 1702
  return true;
}
1703

X
Xiaoyu Wang 已提交
1704
int32_t getTableScanInfo(SOperatorInfo* pOperator, int32_t* order, int32_t* scanFlag) {
1705
  // todo add more information about exchange operation
1706
  int32_t type = pOperator->operatorType;
X
Xiaoyu Wang 已提交
1707
  if (type == QUERY_NODE_PHYSICAL_PLAN_EXCHANGE || type == QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN ||
1708
      type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN || type == QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN ||
1709
      type == QUERY_NODE_PHYSICAL_PLAN_BLOCK_DIST_SCAN || type == QUERY_NODE_PHYSICAL_PLAN_LAST_ROW_SCAN) {
1710 1711 1712
    *order = TSDB_ORDER_ASC;
    *scanFlag = MAIN_SCAN;
    return TSDB_CODE_SUCCESS;
1713
  } else if (type == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
1714 1715 1716 1717
    STableScanInfo* pTableScanInfo = pOperator->info;
    *order = pTableScanInfo->cond.order;
    *scanFlag = pTableScanInfo->scanFlag;
    return TSDB_CODE_SUCCESS;
1718 1719 1720 1721 1722
  } else if (type == QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN) {
    STableMergeScanInfo* pTableScanInfo = pOperator->info;
    *order = pTableScanInfo->cond.order;
    *scanFlag = pTableScanInfo->scanFlag;
    return TSDB_CODE_SUCCESS;
1723
  } else {
H
Haojun Liao 已提交
1724
    if (pOperator->pDownstream == NULL || pOperator->pDownstream[0] == NULL) {
1725
      return TSDB_CODE_INVALID_PARA;
H
Haojun Liao 已提交
1726
    } else {
1727
      return getTableScanInfo(pOperator->pDownstream[0], order, scanFlag);
1728 1729 1730
    }
  }
}
1731

1732
// this is a blocking operator
L
Liu Jicong 已提交
1733
static int32_t doOpenAggregateOptr(SOperatorInfo* pOperator) {
H
Haojun Liao 已提交
1734 1735
  if (OPTR_IS_OPENED(pOperator)) {
    return TSDB_CODE_SUCCESS;
1736 1737
  }

H
Haojun Liao 已提交
1738
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
1739
  SAggOperatorInfo* pAggInfo = pOperator->info;
H
Haojun Liao 已提交
1740

1741 1742
  SExprSupp*     pSup = &pOperator->exprSupp;
  SOperatorInfo* downstream = pOperator->pDownstream[0];
1743

1744 1745
  int64_t st = taosGetTimestampUs();

1746 1747 1748
  int32_t order = TSDB_ORDER_ASC;
  int32_t scanFlag = MAIN_SCAN;

H
Haojun Liao 已提交
1749
  while (1) {
1750
    SSDataBlock* pBlock = downstream->fpSet.getNextFn(downstream);
1751 1752 1753 1754
    if (pBlock == NULL) {
      break;
    }

1755 1756
    int32_t code = getTableScanInfo(pOperator, &order, &scanFlag);
    if (code != TSDB_CODE_SUCCESS) {
1757
      T_LONG_JMP(pTaskInfo->env, code);
1758
    }
1759

1760
    // there is an scalar expression that needs to be calculated before apply the group aggregation.
1761 1762 1763
    if (pAggInfo->scalarExprSup.pExprInfo != NULL) {
      SExprSupp* pSup1 = &pAggInfo->scalarExprSup;
      code = projectApplyFunctions(pSup1->pExprInfo, pBlock, pBlock, pSup1->pCtx, pSup1->numOfExprs, NULL);
1764
      if (code != TSDB_CODE_SUCCESS) {
1765
        T_LONG_JMP(pTaskInfo->env, code);
1766
      }
1767 1768
    }

1769
    // the pDataBlock are always the same one, no need to call this again
1770
    setExecutionContext(pOperator, pOperator->exprSupp.numOfExprs, pBlock->info.groupId);
1771
    setInputDataBlock(pSup, pBlock, order, scanFlag, true);
1772
    code = doAggregateImpl(pOperator, pSup->pCtx);
1773
    if (code != 0) {
1774
      T_LONG_JMP(pTaskInfo->env, code);
1775
    }
1776 1777
  }

1778
  initGroupedResultInfo(&pAggInfo->groupResInfo, pAggInfo->aggSup.pResultRowHashTable, 0);
H
Haojun Liao 已提交
1779
  OPTR_SET_OPENED(pOperator);
1780

1781
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
H
Haojun Liao 已提交
1782 1783 1784
  return TSDB_CODE_SUCCESS;
}

1785
static SSDataBlock* getAggregateResult(SOperatorInfo* pOperator) {
L
Liu Jicong 已提交
1786
  SAggOperatorInfo* pAggInfo = pOperator->info;
H
Haojun Liao 已提交
1787 1788 1789 1790 1791 1792
  SOptrBasicInfo*   pInfo = &pAggInfo->binfo;

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

L
Liu Jicong 已提交
1793
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
1794
  pTaskInfo->code = pOperator->fpSet._openFn(pOperator);
H
Haojun Liao 已提交
1795
  if (pTaskInfo->code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
1796
    setOperatorCompleted(pOperator);
H
Haojun Liao 已提交
1797 1798 1799
    return NULL;
  }

H
Haojun Liao 已提交
1800
  blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
S
slzhou 已提交
1801 1802
  while (1) {
    doBuildResultDatablock(pOperator, pInfo, &pAggInfo->groupResInfo, pAggInfo->aggSup.pResultBuf);
H
Haojun Liao 已提交
1803
    doFilter(pInfo->pRes, pOperator->exprSupp.pFilterInfo, NULL);
S
slzhou 已提交
1804

1805
    if (!hasRemainResults(&pAggInfo->groupResInfo)) {
H
Haojun Liao 已提交
1806
      setOperatorCompleted(pOperator);
S
slzhou 已提交
1807 1808
      break;
    }
1809

S
slzhou 已提交
1810 1811 1812 1813
    if (pInfo->pRes->info.rows > 0) {
      break;
    }
  }
1814

1815
  size_t rows = blockDataGetNumOfRows(pInfo->pRes);
1816 1817
  pOperator->resultInfo.totalRows += rows;

1818
  return (rows == 0) ? NULL : pInfo->pRes;
1819 1820
}

wmmhello's avatar
wmmhello 已提交
1821
int32_t aggEncodeResultRow(SOperatorInfo* pOperator, char** result, int32_t* length) {
1822
  if (result == NULL || length == NULL) {
wmmhello's avatar
wmmhello 已提交
1823 1824 1825
    return TSDB_CODE_TSC_INVALID_INPUT;
  }
  SOptrBasicInfo* pInfo = (SOptrBasicInfo*)(pOperator->info);
1826
  SAggSupporter*  pSup = (SAggSupporter*)POINTER_SHIFT(pOperator->info, sizeof(SOptrBasicInfo));
1827
  int32_t         size = tSimpleHashGetSize(pSup->pResultRowHashTable);
1828 1829 1830
  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 已提交
1831

C
Cary Xu 已提交
1832 1833 1834 1835 1836 1837
  // no result
  if (getTotalBufSize(pSup->pResultBuf) == 0) {
    *result = NULL;
    *length = 0;
    return TSDB_CODE_SUCCESS;
  }
1838

wmmhello's avatar
wmmhello 已提交
1839
  *result = (char*)taosMemoryCalloc(1, totalSize);
L
Liu Jicong 已提交
1840
  if (*result == NULL) {
wmmhello's avatar
wmmhello 已提交
1841
    return TSDB_CODE_OUT_OF_MEMORY;
wmmhello's avatar
wmmhello 已提交
1842
  }
wmmhello's avatar
wmmhello 已提交
1843

wmmhello's avatar
wmmhello 已提交
1844
  int32_t offset = sizeof(int32_t);
wmmhello's avatar
wmmhello 已提交
1845 1846
  *(int32_t*)(*result + offset) = size;
  offset += sizeof(int32_t);
1847 1848

  // prepare memory
1849
  SResultRowPosition* pos = &pInfo->resultRowInfo.cur;
dengyihao's avatar
dengyihao 已提交
1850 1851
  void*               pPage = getBufPage(pSup->pResultBuf, pos->pageId);
  SResultRow*         pRow = (SResultRow*)((char*)pPage + pos->offset);
1852 1853
  setBufPageDirty(pPage, true);
  releaseBufPage(pSup->pResultBuf, pPage);
L
Liu Jicong 已提交
1854

1855 1856 1857 1858
  int32_t iter = 0;
  void*   pIter = NULL;
  while ((pIter = tSimpleHashIterate(pSup->pResultRowHashTable, pIter, &iter))) {
    void*               key = tSimpleHashGetKey(pIter, &keyLen);
1859
    SResultRowPosition* p1 = (SResultRowPosition*)pIter;
1860

dengyihao's avatar
dengyihao 已提交
1861
    pPage = (SFilePage*)getBufPage(pSup->pResultBuf, p1->pageId);
1862
    pRow = (SResultRow*)((char*)pPage + p1->offset);
1863 1864
    setBufPageDirty(pPage, true);
    releaseBufPage(pSup->pResultBuf, pPage);
wmmhello's avatar
wmmhello 已提交
1865 1866 1867

    // recalculate the result size
    int32_t realTotalSize = offset + sizeof(int32_t) + keyLen + sizeof(int32_t) + pSup->resultRowSize;
L
Liu Jicong 已提交
1868
    if (realTotalSize > totalSize) {
wmmhello's avatar
wmmhello 已提交
1869
      char* tmp = (char*)taosMemoryRealloc(*result, realTotalSize);
L
Liu Jicong 已提交
1870
      if (tmp == NULL) {
wafwerar's avatar
wafwerar 已提交
1871
        taosMemoryFree(*result);
wmmhello's avatar
wmmhello 已提交
1872
        *result = NULL;
wmmhello's avatar
wmmhello 已提交
1873
        return TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
1874
      } else {
wmmhello's avatar
wmmhello 已提交
1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886
        *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);
1887
    memcpy(*result + offset, pRow, pSup->resultRowSize);
wmmhello's avatar
wmmhello 已提交
1888 1889 1890
    offset += pSup->resultRowSize;
  }

wmmhello's avatar
wmmhello 已提交
1891 1892 1893 1894
  *(int32_t*)(*result) = offset;
  *length = offset;

  return TDB_CODE_SUCCESS;
wmmhello's avatar
wmmhello 已提交
1895 1896
}

1897 1898 1899 1900 1901
int32_t handleLimitOffset(SOperatorInfo* pOperator, SLimitInfo* pLimitInfo, SSDataBlock* pBlock, bool holdDataInBuf) {
  if (pLimitInfo->remainGroupOffset > 0) {
    if (pLimitInfo->currentGroupId == 0) {  // it is the first group
      pLimitInfo->currentGroupId = pBlock->info.groupId;
      blockDataCleanup(pBlock);
1902
      return PROJECT_RETRIEVE_CONTINUE;
1903 1904 1905
    } else if (pLimitInfo->currentGroupId != pBlock->info.groupId) {
      // now it is the data from a new group
      pLimitInfo->remainGroupOffset -= 1;
1906 1907

      // ignore data block in current group
1908 1909
      if (pLimitInfo->remainGroupOffset > 0) {
        blockDataCleanup(pBlock);
1910 1911 1912 1913 1914
        return PROJECT_RETRIEVE_CONTINUE;
      }
    }

    // set current group id of the project operator
1915
    pLimitInfo->currentGroupId = pBlock->info.groupId;
1916 1917
  }

1918
  // here check for a new group data, we need to handle the data of the previous group.
1919 1920 1921
  if (pLimitInfo->currentGroupId != 0 && pLimitInfo->currentGroupId != pBlock->info.groupId) {
    pLimitInfo->numOfOutputGroups += 1;
    if ((pLimitInfo->slimit.limit > 0) && (pLimitInfo->slimit.limit <= pLimitInfo->numOfOutputGroups)) {
1922
      pOperator->status = OP_EXEC_DONE;
1923
      blockDataCleanup(pBlock);
1924 1925 1926 1927 1928

      return PROJECT_RETRIEVE_DONE;
    }

    // reset the value for a new group data
1929 1930
    pLimitInfo->numOfOutputRows = 0;
    pLimitInfo->remainOffset = pLimitInfo->limit.offset;
1931 1932 1933 1934 1935

    // existing rows that belongs to previous group.
    if (pBlock->info.rows > 0) {
      return PROJECT_RETRIEVE_DONE;
    }
1936 1937 1938 1939 1940
  }

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

  // set current group id
1941
  pLimitInfo->currentGroupId = pBlock->info.groupId;
1942

1943 1944 1945
  if (pLimitInfo->remainOffset >= pBlock->info.rows) {
    pLimitInfo->remainOffset -= pBlock->info.rows;
    blockDataCleanup(pBlock);
1946
    return PROJECT_RETRIEVE_CONTINUE;
1947 1948 1949
  } else if (pLimitInfo->remainOffset < pBlock->info.rows && pLimitInfo->remainOffset > 0) {
    blockDataTrimFirstNRows(pBlock, pLimitInfo->remainOffset);
    pLimitInfo->remainOffset = 0;
1950 1951
  }

1952
  // check for the limitation in each group
1953 1954 1955 1956
  if (pLimitInfo->limit.limit >= 0 && pLimitInfo->numOfOutputRows + pBlock->info.rows >= pLimitInfo->limit.limit) {
    int32_t keepRows = (int32_t)(pLimitInfo->limit.limit - pLimitInfo->numOfOutputRows);
    blockDataKeepFirstNRows(pBlock, keepRows);
    if (pLimitInfo->slimit.limit > 0 && pLimitInfo->slimit.limit <= pLimitInfo->numOfOutputGroups) {
1957 1958 1959
      pOperator->status = OP_EXEC_DONE;
    }

1960
    return PROJECT_RETRIEVE_DONE;
1961
  }
1962

1963
  // todo optimize performance
1964 1965
  // 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.
1966 1967
  if ((!holdDataInBuf) || (pBlock->info.rows >= pOperator->resultInfo.threshold) || pLimitInfo->slimit.offset != -1 ||
      pLimitInfo->slimit.limit != -1) {
1968
    return PROJECT_RETRIEVE_DONE;
L
Liu Jicong 已提交
1969
  } else {  // not full enough, continue to accumulate the output data in the buffer.
1970 1971 1972 1973
    return PROJECT_RETRIEVE_CONTINUE;
  }
}

1974
static void doApplyScalarCalculation(SOperatorInfo* pOperator, SSDataBlock* pBlock, int32_t order, int32_t scanFlag);
L
Liu Jicong 已提交
1975 1976
static void doHandleRemainBlockForNewGroupImpl(SOperatorInfo* pOperator, SFillOperatorInfo* pInfo,
                                               SResultInfo* pResultInfo, SExecTaskInfo* pTaskInfo) {
1977
  pInfo->totalInputRows = pInfo->existNewGroupBlock->info.rows;
1978 1979 1980 1981 1982
  SSDataBlock* pResBlock = pInfo->pFinalRes;

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

5
54liuyao 已提交
1984
  int64_t ekey = pInfo->existNewGroupBlock->info.window.ekey;
1985 1986
  taosResetFillInfo(pInfo->pFillInfo, getFillInfoStart(pInfo->pFillInfo));

1987
  blockDataCleanup(pInfo->pRes);
1988 1989 1990 1991
  doApplyScalarCalculation(pOperator, pInfo->existNewGroupBlock, order, scanFlag);

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

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

1996
  pInfo->curGroupId = pInfo->existNewGroupBlock->info.groupId;
1997 1998 1999
  pInfo->existNewGroupBlock = NULL;
}

L
Liu Jicong 已提交
2000 2001
static void doHandleRemainBlockFromNewGroup(SOperatorInfo* pOperator, SFillOperatorInfo* pInfo,
                                            SResultInfo* pResultInfo, SExecTaskInfo* pTaskInfo) {
2002
  if (taosFillHasMoreResults(pInfo->pFillInfo)) {
H
Haojun Liao 已提交
2003 2004
    int32_t numOfResultRows = pResultInfo->capacity - pInfo->pFinalRes->info.rows;
    taosFillResultDataBlock(pInfo->pFillInfo, pInfo->pFinalRes, numOfResultRows);
2005 2006
    pInfo->pRes->info.groupId = pInfo->curGroupId;
    return;
2007 2008 2009 2010
  }

  // handle the cached new group data block
  if (pInfo->existNewGroupBlock) {
2011 2012 2013 2014 2015 2016
    doHandleRemainBlockForNewGroupImpl(pOperator, pInfo, pResultInfo, pTaskInfo);
  }
}

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

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

2026 2027
  projectApplyFunctions(pNoFillSupp->pExprInfo, pInfo->pRes, pBlock, pNoFillSupp->pCtx, pNoFillSupp->numOfExprs, NULL);
  pInfo->pRes->info.groupId = pBlock->info.groupId;
2028 2029
}

S
slzhou 已提交
2030
static SSDataBlock* doFillImpl(SOperatorInfo* pOperator) {
L
Liu Jicong 已提交
2031 2032
  SFillOperatorInfo* pInfo = pOperator->info;
  SExecTaskInfo*     pTaskInfo = pOperator->pTaskInfo;
2033

H
Haojun Liao 已提交
2034
  SResultInfo* pResultInfo = &pOperator->resultInfo;
H
Haojun Liao 已提交
2035
  SSDataBlock* pResBlock = pInfo->pFinalRes;
2036 2037

  blockDataCleanup(pResBlock);
2038

H
Haojun Liao 已提交
2039 2040
  int32_t order = TSDB_ORDER_ASC;
  int32_t scanFlag = MAIN_SCAN;
2041
  getTableScanInfo(pOperator, &order, &scanFlag);
2042

2043
  doHandleRemainBlockFromNewGroup(pOperator, pInfo, pResultInfo, pTaskInfo);
2044
  if (pResBlock->info.rows > 0) {
2045
    pResBlock->info.groupId = pInfo->curGroupId;
2046
    return pResBlock;
H
Haojun Liao 已提交
2047
  }
2048

H
Haojun Liao 已提交
2049
  SOperatorInfo* pDownstream = pOperator->pDownstream[0];
L
Liu Jicong 已提交
2050
  while (1) {
2051
    SSDataBlock* pBlock = pDownstream->fpSet.getNextFn(pDownstream);
2052 2053
    if (pBlock == NULL) {
      if (pInfo->totalInputRows == 0) {
H
Haojun Liao 已提交
2054
        setOperatorCompleted(pOperator);
2055 2056
        return NULL;
      }
2057

2058
      taosFillSetStartInfo(pInfo->pFillInfo, 0, pInfo->win.ekey);
2059
    } else {
2060
      blockDataUpdateTsWindow(pBlock, pInfo->primarySrcSlotId);
2061 2062

      blockDataCleanup(pInfo->pRes);
2063 2064
      blockDataEnsureCapacity(pInfo->pRes, pBlock->info.rows);
      blockDataEnsureCapacity(pInfo->pFinalRes, pBlock->info.rows);
2065
      doApplyScalarCalculation(pOperator, pBlock, order, scanFlag);
2066

H
Haojun Liao 已提交
2067 2068 2069
      if (pInfo->curGroupId == 0 || pInfo->curGroupId == pInfo->pRes->info.groupId) {
        pInfo->curGroupId = pInfo->pRes->info.groupId;  // the first data block
        pInfo->totalInputRows += pInfo->pRes->info.rows;
2070

H
Haojun Liao 已提交
2071 2072 2073 2074 2075
        if (order == pInfo->pFillInfo->order) {
          taosFillSetStartInfo(pInfo->pFillInfo, pInfo->pRes->info.rows, pBlock->info.window.ekey);
        } else {
          taosFillSetStartInfo(pInfo->pFillInfo, pInfo->pRes->info.rows, pBlock->info.window.skey);
        }
H
Haojun Liao 已提交
2076
        taosFillSetInputDataBlock(pInfo->pFillInfo, pInfo->pRes);
L
Liu Jicong 已提交
2077
      } else if (pInfo->curGroupId != pBlock->info.groupId) {  // the new group data block
2078 2079 2080 2081 2082
        pInfo->existNewGroupBlock = pBlock;

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

2086 2087
    int32_t numOfResultRows = pOperator->resultInfo.capacity - pResBlock->info.rows;
    taosFillResultDataBlock(pInfo->pFillInfo, pResBlock, numOfResultRows);
2088 2089

    // current group has no more result to return
2090
    if (pResBlock->info.rows > 0) {
2091 2092
      // 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
2093
      if (pResBlock->info.rows > pResultInfo->threshold || pBlock == NULL || pInfo->existNewGroupBlock != NULL) {
2094
        pResBlock->info.groupId = pInfo->curGroupId;
2095
        return pResBlock;
2096 2097
      }

2098
      doHandleRemainBlockFromNewGroup(pOperator, pInfo, pResultInfo, pTaskInfo);
2099
      if (pResBlock->info.rows >= pOperator->resultInfo.threshold || pBlock == NULL) {
2100
        pResBlock->info.groupId = pInfo->curGroupId;
2101
        return pResBlock;
2102 2103 2104
      }
    } else if (pInfo->existNewGroupBlock) {  // try next group
      assert(pBlock != NULL);
2105 2106 2107 2108

      blockDataCleanup(pResBlock);

      doHandleRemainBlockForNewGroupImpl(pOperator, pInfo, pResultInfo, pTaskInfo);
2109
      if (pResBlock->info.rows > pResultInfo->threshold) {
2110
        pResBlock->info.groupId = pInfo->curGroupId;
2111
        return pResBlock;
2112 2113 2114 2115 2116 2117 2118
      }
    } else {
      return NULL;
    }
  }
}

S
slzhou 已提交
2119 2120 2121 2122 2123 2124 2125 2126
static SSDataBlock* doFill(SOperatorInfo* pOperator) {
  SFillOperatorInfo* pInfo = pOperator->info;
  SExecTaskInfo*     pTaskInfo = pOperator->pTaskInfo;

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

S
slzhou 已提交
2127
  SSDataBlock* fillResult = NULL;
S
slzhou 已提交
2128
  while (true) {
S
slzhou 已提交
2129
    fillResult = doFillImpl(pOperator);
S
slzhou 已提交
2130
    if (fillResult == NULL) {
H
Haojun Liao 已提交
2131
      setOperatorCompleted(pOperator);
S
slzhou 已提交
2132 2133 2134
      break;
    }

2135
    doFilter(fillResult, pOperator->exprSupp.pFilterInfo, &pInfo->matchInfo);
S
slzhou 已提交
2136 2137 2138 2139 2140
    if (fillResult->info.rows > 0) {
      break;
    }
  }

S
slzhou 已提交
2141
  if (fillResult != NULL) {
2142
    pOperator->resultInfo.totalRows += fillResult->info.rows;
S
slzhou 已提交
2143
  }
S
slzhou 已提交
2144

S
slzhou 已提交
2145
  return fillResult;
S
slzhou 已提交
2146 2147
}

2148
void destroyExprInfo(SExprInfo* pExpr, int32_t numOfExprs) {
C
Cary Xu 已提交
2149 2150 2151 2152 2153
  for (int32_t i = 0; i < numOfExprs; ++i) {
    SExprInfo* pExprInfo = &pExpr[i];
    for (int32_t j = 0; j < pExprInfo->base.numOfParams; ++j) {
      if (pExprInfo->base.pParam[j].type == FUNC_PARAM_TYPE_COLUMN) {
        taosMemoryFreeClear(pExprInfo->base.pParam[j].pCol);
H
Haojun Liao 已提交
2154
      }
2155
    }
C
Cary Xu 已提交
2156 2157 2158

    taosMemoryFree(pExprInfo->base.pParam);
    taosMemoryFree(pExprInfo->pExpr);
H
Haojun Liao 已提交
2159 2160 2161
  }
}

5
54liuyao 已提交
2162
void destroyOperatorInfo(SOperatorInfo* pOperator) {
2163 2164 2165 2166
  if (pOperator == NULL) {
    return;
  }

2167
  if (pOperator->fpSet.closeFn != NULL) {
2168
    pOperator->fpSet.closeFn(pOperator->info);
2169 2170
  }

H
Haojun Liao 已提交
2171
  if (pOperator->pDownstream != NULL) {
L
Liu Jicong 已提交
2172
    for (int32_t i = 0; i < pOperator->numOfDownstream; ++i) {
H
Haojun Liao 已提交
2173
      destroyOperatorInfo(pOperator->pDownstream[i]);
2174 2175
    }

wafwerar's avatar
wafwerar 已提交
2176
    taosMemoryFreeClear(pOperator->pDownstream);
H
Haojun Liao 已提交
2177
    pOperator->numOfDownstream = 0;
2178 2179
  }

2180
  cleanupExprSupp(&pOperator->exprSupp);
wafwerar's avatar
wafwerar 已提交
2181
  taosMemoryFreeClear(pOperator);
2182 2183
}

2184 2185 2186 2187 2188 2189
int32_t getBufferPgSize(int32_t rowSize, uint32_t* defaultPgsz, uint32_t* defaultBufsz) {
  *defaultPgsz = 4096;
  while (*defaultPgsz < rowSize * 4) {
    *defaultPgsz <<= 1u;
  }

2190
  // The default buffer for each operator in query is 10MB.
2191
  // at least four pages need to be in buffer
2192 2193
  // TODO: make this variable to be configurable.
  *defaultBufsz = 4096 * 2560;
2194 2195 2196 2197 2198 2199 2200
  if ((*defaultBufsz) <= (*defaultPgsz)) {
    (*defaultBufsz) = (*defaultPgsz) * 4;
  }

  return 0;
}

dengyihao's avatar
dengyihao 已提交
2201 2202
int32_t doInitAggInfoSup(SAggSupporter* pAggSup, SqlFunctionCtx* pCtx, int32_t numOfOutput, size_t keyBufSize,
                         const char* pKey) {
L
Liu Jicong 已提交
2203
  int32_t    code = 0;
2204 2205
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);

2206
  pAggSup->currentPageId = -1;
dengyihao's avatar
dengyihao 已提交
2207 2208
  pAggSup->resultRowSize = getResultRowSize(pCtx, numOfOutput);
  pAggSup->keyBuf = taosMemoryCalloc(1, keyBufSize + POINTER_BYTES + sizeof(int64_t));
2209
  pAggSup->pResultRowHashTable = tSimpleHashInit(10, hashFn);
2210

H
Haojun Liao 已提交
2211
  if (pAggSup->keyBuf == NULL || pAggSup->pResultRowHashTable == NULL) {
2212 2213 2214
    return TSDB_CODE_OUT_OF_MEMORY;
  }

dengyihao's avatar
dengyihao 已提交
2215
  uint32_t defaultPgsz = 0;
2216 2217
  uint32_t defaultBufsz = 0;
  getBufferPgSize(pAggSup->resultRowSize, &defaultPgsz, &defaultBufsz);
H
Haojun Liao 已提交
2218

wafwerar's avatar
wafwerar 已提交
2219
  if (!osTempSpaceAvailable()) {
H
Haojun Liao 已提交
2220 2221 2222
    code = TSDB_CODE_NO_AVAIL_DISK;
    qError("Init stream agg supporter failed since %s, %s", terrstr(code), pKey);
    return code;
wafwerar's avatar
wafwerar 已提交
2223
  }
2224

H
Haojun Liao 已提交
2225
  code = createDiskbasedBuf(&pAggSup->pResultBuf, defaultPgsz, defaultBufsz, pKey, tsTempDir);
H
Haojun Liao 已提交
2226
  if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
2227
    qError("Create agg result buf failed since %s, %s", tstrerror(code), pKey);
H
Haojun Liao 已提交
2228 2229 2230
    return code;
  }

H
Haojun Liao 已提交
2231
  return code;
2232 2233
}

2234
void cleanupAggSup(SAggSupporter* pAggSup) {
wafwerar's avatar
wafwerar 已提交
2235
  taosMemoryFreeClear(pAggSup->keyBuf);
2236
  tSimpleHashCleanup(pAggSup->pResultRowHashTable);
H
Haojun Liao 已提交
2237
  destroyDiskbasedBuf(pAggSup->pResultBuf);
2238 2239
}

L
Liu Jicong 已提交
2240 2241
int32_t initAggInfo(SExprSupp* pSup, SAggSupporter* pAggSup, SExprInfo* pExprInfo, int32_t numOfCols, size_t keyBufSize,
                    const char* pkey) {
2242 2243 2244 2245 2246
  int32_t code = initExprSupp(pSup, pExprInfo, numOfCols);
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }

2247 2248 2249 2250 2251
  code = doInitAggInfoSup(pAggSup, pSup->pCtx, numOfCols, keyBufSize, pkey);
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }

L
Liu Jicong 已提交
2252
  for (int32_t i = 0; i < numOfCols; ++i) {
2253
    pSup->pCtx[i].saveHandle.pBuf = pAggSup->pResultBuf;
2254 2255
  }

2256
  return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
2257 2258
}

L
Liu Jicong 已提交
2259
void initResultSizeInfo(SResultInfo* pResultInfo, int32_t numOfRows) {
wmmhello's avatar
wmmhello 已提交
2260
  ASSERT(numOfRows != 0);
2261 2262
  pResultInfo->capacity = numOfRows;
  pResultInfo->threshold = numOfRows * 0.75;
2263

2264 2265
  if (pResultInfo->threshold == 0) {
    pResultInfo->threshold = numOfRows;
2266 2267 2268
  }
}

2269 2270 2271 2272 2273
void initBasicInfo(SOptrBasicInfo* pInfo, SSDataBlock* pBlock) {
  pInfo->pRes = pBlock;
  initResultRowInfo(&pInfo->resultRowInfo);
}

5
54liuyao 已提交
2274
void* destroySqlFunctionCtx(SqlFunctionCtx* pCtx, int32_t numOfOutput) {
2275 2276 2277 2278 2279 2280 2281 2282 2283 2284
  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);
2285
    taosMemoryFreeClear(pCtx[i].subsidiaries.buf);
2286 2287 2288 2289 2290 2291 2292 2293
    taosMemoryFree(pCtx[i].input.pData);
    taosMemoryFree(pCtx[i].input.pColumnDataAgg);
  }

  taosMemoryFreeClear(pCtx);
  return NULL;
}

2294
int32_t initExprSupp(SExprSupp* pSup, SExprInfo* pExprInfo, int32_t numOfExpr) {
2295 2296 2297 2298
  pSup->pExprInfo = pExprInfo;
  pSup->numOfExprs = numOfExpr;
  if (pSup->pExprInfo != NULL) {
    pSup->pCtx = createSqlFunctionCtx(pExprInfo, numOfExpr, &pSup->rowEntryInfoOffset);
2299 2300 2301
    if (pSup->pCtx == NULL) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
2302
  }
2303 2304

  return TSDB_CODE_SUCCESS;
2305 2306
}

2307 2308 2309 2310
void cleanupExprSupp(SExprSupp* pSupp) {
  destroySqlFunctionCtx(pSupp->pCtx, pSupp->numOfExprs);
  if (pSupp->pExprInfo != NULL) {
    destroyExprInfo(pSupp->pExprInfo, pSupp->numOfExprs);
C
Cary Xu 已提交
2311
    taosMemoryFreeClear(pSupp->pExprInfo);
2312
  }
H
Haojun Liao 已提交
2313 2314 2315 2316 2317 2318

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

2319 2320 2321
  taosMemoryFree(pSupp->rowEntryInfoOffset);
}

2322 2323
SOperatorInfo* createAggregateOperatorInfo(SOperatorInfo* downstream, SAggPhysiNode* pAggNode,
                                           SExecTaskInfo* pTaskInfo) {
wafwerar's avatar
wafwerar 已提交
2324
  SAggOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SAggOperatorInfo));
L
Liu Jicong 已提交
2325
  SOperatorInfo*    pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
H
Haojun Liao 已提交
2326 2327 2328
  if (pInfo == NULL || pOperator == NULL) {
    goto _error;
  }
H
Haojun Liao 已提交
2329

H
Haojun Liao 已提交
2330 2331 2332 2333
  SSDataBlock* pResBlock = createResDataBlock(pAggNode->node.pOutputDataBlockDesc);
  initBasicInfo(&pInfo->binfo, pResBlock);

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

2336 2337 2338
  int32_t    num = 0;
  SExprInfo* pExprInfo = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &num);
  int32_t    code = initAggInfo(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, num, keyBufSize, pTaskInfo->id.str);
L
Liu Jicong 已提交
2339
  if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
2340 2341
    goto _error;
  }
H
Haojun Liao 已提交
2342

H
Haojun Liao 已提交
2343 2344 2345 2346 2347 2348
  int32_t    numOfScalarExpr = 0;
  SExprInfo* pScalarExprInfo = NULL;
  if (pAggNode->pExprs != NULL) {
    pScalarExprInfo = createExprInfo(pAggNode->pExprs, NULL, &numOfScalarExpr);
  }

2349 2350 2351 2352
  code = initExprSupp(&pInfo->scalarExprSup, pScalarExprInfo, numOfScalarExpr);
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
2353

H
Haojun Liao 已提交
2354 2355 2356 2357 2358
  code = filterInitFromNode((SNode*)pAggNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0);
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }

H
Haojun Liao 已提交
2359
  pInfo->binfo.mergeResultBlock = pAggNode->mergeDataBlock;
2360
  pInfo->groupId = UINT64_MAX;
H
Haojun Liao 已提交
2361

2362 2363 2364
  setOperatorInfo(pOperator, "TableAggregate", QUERY_NODE_PHYSICAL_PLAN_HASH_AGG, true, OP_NOT_OPENED, pInfo,
                  pTaskInfo);
  pOperator->fpSet = createOperatorFpSet(doOpenAggregateOptr, getAggregateResult, NULL, destroyAggOperatorInfo, NULL);
H
Haojun Liao 已提交
2365

2366 2367 2368 2369 2370 2371
  if (downstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
    STableScanInfo* pTableScanInfo = downstream->info;
    pTableScanInfo->pdInfo.pExprSup = &pOperator->exprSupp;
    pTableScanInfo->pdInfo.pAggSup = &pInfo->aggSup;
  }

H
Haojun Liao 已提交
2372 2373 2374 2375
  code = appendDownstream(pOperator, &downstream, 1);
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
2376 2377

  return pOperator;
H
Haojun Liao 已提交
2378

2379
_error:
H
Haojun Liao 已提交
2380 2381 2382 2383
  if (pInfo != NULL) {
    destroyAggOperatorInfo(pInfo);
  }

2384 2385 2386
  if (pOperator != NULL) {
    cleanupExprSupp(&pOperator->exprSupp);
  }
H
Haojun Liao 已提交
2387

2388
  taosMemoryFreeClear(pOperator);
H
Haojun Liao 已提交
2389
  pTaskInfo->code = code;
H
Haojun Liao 已提交
2390
  return NULL;
2391 2392
}

2393
void cleanupBasicInfo(SOptrBasicInfo* pInfo) {
2394
  assert(pInfo != NULL);
H
Haojun Liao 已提交
2395
  pInfo->pRes = blockDataDestroy(pInfo->pRes);
2396 2397
}

H
Haojun Liao 已提交
2398 2399 2400 2401 2402 2403 2404
static void freeItem(void* pItem) {
  void** p = pItem;
  if (*p != NULL) {
    taosMemoryFreeClear(*p);
  }
}

2405
void destroyAggOperatorInfo(void* param) {
L
Liu Jicong 已提交
2406
  SAggOperatorInfo* pInfo = (SAggOperatorInfo*)param;
L
Liu Jicong 已提交
2407 2408
  cleanupBasicInfo(&pInfo->binfo);

H
Haojun Liao 已提交
2409
  cleanupAggSup(&pInfo->aggSup);
S
shenglian zhou 已提交
2410
  cleanupExprSupp(&pInfo->scalarExprSup);
H
Haojun Liao 已提交
2411
  cleanupGroupResInfo(&pInfo->groupResInfo);
D
dapan1121 已提交
2412
  taosMemoryFreeClear(param);
2413
}
2414

2415
void destroyFillOperatorInfo(void* param) {
L
Liu Jicong 已提交
2416
  SFillOperatorInfo* pInfo = (SFillOperatorInfo*)param;
2417
  pInfo->pFillInfo = taosDestroyFillInfo(pInfo->pFillInfo);
H
Haojun Liao 已提交
2418
  pInfo->pRes = blockDataDestroy(pInfo->pRes);
H
Haojun Liao 已提交
2419 2420
  pInfo->pFinalRes = blockDataDestroy(pInfo->pFinalRes);

2421
  cleanupExprSupp(&pInfo->noFillExprSupp);
H
Haojun Liao 已提交
2422

wafwerar's avatar
wafwerar 已提交
2423
  taosMemoryFreeClear(pInfo->p);
H
Haojun Liao 已提交
2424
  taosArrayDestroy(pInfo->matchInfo.pList);
D
dapan1121 已提交
2425
  taosMemoryFreeClear(param);
2426 2427
}

H
Haojun Liao 已提交
2428 2429 2430 2431
static int32_t initFillInfo(SFillOperatorInfo* pInfo, SExprInfo* pExpr, int32_t numOfCols, SExprInfo* pNotFillExpr,
                            int32_t numOfNotFillCols, SNodeListNode* pValNode, STimeWindow win, int32_t capacity,
                            const char* id, SInterval* pInterval, int32_t fillType, int32_t order) {
  SFillColInfo* pColInfo = createFillColInfo(pExpr, numOfCols, pNotFillExpr, numOfNotFillCols, pValNode);
H
Haojun Liao 已提交
2432

H
Haojun Liao 已提交
2433 2434 2435
  int64_t     startKey = (order == TSDB_ORDER_ASC) ? win.skey : win.ekey;
  STimeWindow w = getAlignQueryTimeWindow(pInterval, pInterval->precision, startKey);
  w = getFirstQualifiedTimeWindow(startKey, &w, pInterval, order);
H
Haojun Liao 已提交
2436

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

H
Haojun Liao 已提交
2440 2441 2442 2443 2444 2445 2446
  if (order == TSDB_ORDER_ASC) {
    pInfo->win.skey = win.skey;
    pInfo->win.ekey = win.ekey;
  } else {
    pInfo->win.skey = win.ekey;
    pInfo->win.ekey = win.skey;
  }
L
Liu Jicong 已提交
2447
  pInfo->p = taosMemoryCalloc(numOfCols, POINTER_BYTES);
2448

H
Haojun Liao 已提交
2449
  if (pInfo->pFillInfo == NULL || pInfo->p == NULL) {
H
Haojun Liao 已提交
2450 2451
    taosMemoryFree(pInfo->pFillInfo);
    taosMemoryFree(pInfo->p);
H
Haojun Liao 已提交
2452 2453 2454 2455 2456 2457
    return TSDB_CODE_OUT_OF_MEMORY;
  } else {
    return TSDB_CODE_SUCCESS;
  }
}

2458
static bool isWstartColumnExist(SFillOperatorInfo* pInfo) {
2459
  if (pInfo->noFillExprSupp.numOfExprs == 0) {
2460 2461
    return false;
  }
2462 2463 2464

  for (int32_t i = 0; i < pInfo->noFillExprSupp.numOfExprs; ++i) {
    SExprInfo* exprInfo = pInfo->noFillExprSupp.pExprInfo + i;
2465
    if (exprInfo->pExpr->nodeType == QUERY_NODE_COLUMN && exprInfo->base.numOfParams == 1 &&
2466 2467 2468 2469 2470 2471 2472
        exprInfo->base.pParam[0].pCol->colType == COLUMN_TYPE_WINDOW_START) {
      return true;
    }
  }
  return false;
}

2473 2474
static int32_t createPrimaryTsExprIfNeeded(SFillOperatorInfo* pInfo, SFillPhysiNode* pPhyFillNode, SExprSupp* pExprSupp,
                                           const char* idStr) {
2475
  bool wstartExist = isWstartColumnExist(pInfo);
2476

2477 2478
  if (wstartExist == false) {
    if (pPhyFillNode->pWStartTs->type != QUERY_NODE_TARGET) {
2479
      qError("pWStartTs of fill physical node is not a target node, %s", idStr);
2480 2481 2482
      return TSDB_CODE_QRY_SYS_ERROR;
    }

2483 2484
    SExprInfo* pExpr = taosMemoryRealloc(pExprSupp->pExprInfo, (pExprSupp->numOfExprs + 1) * sizeof(SExprInfo));
    if (pExpr == NULL) {
2485 2486 2487
      return TSDB_CODE_OUT_OF_MEMORY;
    }

2488 2489 2490
    createExprFromTargetNode(&pExpr[pExprSupp->numOfExprs], (STargetNode*)pPhyFillNode->pWStartTs);
    pExprSupp->numOfExprs += 1;
    pExprSupp->pExprInfo = pExpr;
2491
  }
2492

2493 2494 2495
  return TSDB_CODE_SUCCESS;
}

L
Liu Jicong 已提交
2496 2497
SOperatorInfo* createFillOperatorInfo(SOperatorInfo* downstream, SFillPhysiNode* pPhyFillNode,
                                      SExecTaskInfo* pTaskInfo) {
2498 2499 2500 2501 2502 2503
  SFillOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SFillOperatorInfo));
  SOperatorInfo*     pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
  if (pInfo == NULL || pOperator == NULL) {
    goto _error;
  }

H
Haojun Liao 已提交
2504 2505 2506 2507
  pInfo->pRes = createResDataBlock(pPhyFillNode->node.pOutputDataBlockDesc);
  SExprInfo* pExprInfo = createExprInfo(pPhyFillNode->pFillExprs, NULL, &pInfo->numOfExpr);
  pOperator->exprSupp.pExprInfo = pExprInfo;

2508 2509 2510 2511 2512 2513 2514 2515
  SExprSupp* pNoFillSupp = &pInfo->noFillExprSupp;
  pNoFillSupp->pExprInfo = createExprInfo(pPhyFillNode->pNotFillExprs, NULL, &pNoFillSupp->numOfExprs);
  int32_t code = createPrimaryTsExprIfNeeded(pInfo, pPhyFillNode, pNoFillSupp, pTaskInfo->id.str);
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }

  code = initExprSupp(pNoFillSupp, pNoFillSupp->pExprInfo, pNoFillSupp->numOfExprs);
2516 2517 2518
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
H
Haojun Liao 已提交
2519

L
Liu Jicong 已提交
2520
  SInterval* pInterval =
2521
      QUERY_NODE_PHYSICAL_PLAN_MERGE_ALIGNED_INTERVAL == downstream->operatorType
2522 2523
          ? &((SMergeAlignedIntervalAggOperatorInfo*)downstream->info)->intervalAggOperatorInfo->interval
          : &((SIntervalAggOperatorInfo*)downstream->info)->interval;
2524

2525
  int32_t order = (pPhyFillNode->inputTsOrder == ORDER_ASC) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC;
2526
  int32_t type = convertFillType(pPhyFillNode->mode);
2527

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

2530
  initResultSizeInfo(&pOperator->resultInfo, 4096);
H
Haojun Liao 已提交
2531 2532 2533 2534 2535
  blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
  code = initExprSupp(&pOperator->exprSupp, pExprInfo, pInfo->numOfExpr);
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
H
Haojun Liao 已提交
2536

H
Haojun Liao 已提交
2537 2538
  pInfo->primaryTsCol = ((STargetNode*)pPhyFillNode->pWStartTs)->slotId;
  pInfo->primarySrcSlotId = ((SColumnNode*)((STargetNode*)pPhyFillNode->pWStartTs)->pExpr)->slotId;
2539

2540
  int32_t numOfOutputCols = 0;
H
Haojun Liao 已提交
2541 2542
  code = extractColMatchInfo(pPhyFillNode->pFillExprs, pPhyFillNode->node.pOutputDataBlockDesc, &numOfOutputCols,
                             COL_MATCH_FROM_SLOT_ID, &pInfo->matchInfo);
2543

2544
  code = initFillInfo(pInfo, pExprInfo, pInfo->numOfExpr, pNoFillSupp->pExprInfo, pNoFillSupp->numOfExprs,
2545 2546
                      (SNodeListNode*)pPhyFillNode->pValues, pPhyFillNode->timeRange, pResultInfo->capacity,
                      pTaskInfo->id.str, pInterval, type, order);
2547 2548 2549
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
2550

H
Haojun Liao 已提交
2551
  pInfo->pFinalRes = createOneDataBlock(pInfo->pRes, false);
H
Haojun Liao 已提交
2552 2553
  blockDataEnsureCapacity(pInfo->pFinalRes, pOperator->resultInfo.capacity);

H
Haojun Liao 已提交
2554 2555 2556 2557 2558
  code = filterInitFromNode((SNode*)pPhyFillNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0);
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }

H
Haojun Liao 已提交
2559
  setOperatorInfo(pOperator, "FillOperator", QUERY_NODE_PHYSICAL_PLAN_FILL, false, OP_NOT_OPENED, pInfo, pTaskInfo);
2560
  pOperator->exprSupp.numOfExprs = pInfo->numOfExpr;
H
Haojun Liao 已提交
2561
  pOperator->fpSet = createOperatorFpSet(operatorDummyOpenFn, doFill, NULL, destroyFillOperatorInfo, NULL);
2562

2563
  code = appendDownstream(pOperator, &downstream, 1);
2564
  return pOperator;
H
Haojun Liao 已提交
2565

2566
_error:
H
Haojun Liao 已提交
2567 2568 2569 2570
  if (pInfo != NULL) {
    destroyFillOperatorInfo(pInfo);
  }

2571
  pTaskInfo->code = code;
wafwerar's avatar
wafwerar 已提交
2572
  taosMemoryFreeClear(pOperator);
H
Haojun Liao 已提交
2573
  return NULL;
2574 2575
}

D
dapan1121 已提交
2576
static SExecTaskInfo* createExecTaskInfo(uint64_t queryId, uint64_t taskId, EOPTR_EXEC_MODEL model, char* dbFName) {
wafwerar's avatar
wafwerar 已提交
2577
  SExecTaskInfo* pTaskInfo = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
H
Haojun Liao 已提交
2578 2579 2580 2581 2582
  if (pTaskInfo == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

2583
  setTaskStatus(pTaskInfo, TASK_NOT_COMPLETED);
H
Haojun Liao 已提交
2584

2585
  pTaskInfo->schemaInfo.dbname = strdup(dbFName);
2586
  pTaskInfo->cost.created = taosGetTimestampMs();
H
Haojun Liao 已提交
2587
  pTaskInfo->id.queryId = queryId;
dengyihao's avatar
dengyihao 已提交
2588
  pTaskInfo->execModel = model;
H
Haojun Liao 已提交
2589
  pTaskInfo->pTableInfoList = tableListCreate();
D
dapan1121 已提交
2590
  pTaskInfo->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
H
Haojun Liao 已提交
2591

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

2596 2597
  return pTaskInfo;
}
H
Haojun Liao 已提交
2598

H
Haojun Liao 已提交
2599 2600
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode);

2601
int32_t extractTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, SExecTaskInfo* pTaskInfo) {
2602 2603
  SMetaReader mr = {0};
  metaReaderInit(&mr, pHandle->meta, 0);
2604
  int32_t code = metaGetTableEntryByUid(&mr, pScanNode->uid);
2605
  if (code != TSDB_CODE_SUCCESS) {
L
Liu Jicong 已提交
2606 2607
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
           GET_TASKID(pTaskInfo));
H
Haojun Liao 已提交
2608

D
dapan1121 已提交
2609
    metaReaderClear(&mr);
2610
    return terrno;
D
dapan1121 已提交
2611
  }
2612

2613 2614
  SSchemaInfo* pSchemaInfo = &pTaskInfo->schemaInfo;
  pSchemaInfo->tablename = strdup(mr.me.name);
2615 2616

  if (mr.me.type == TSDB_SUPER_TABLE) {
2617 2618
    pSchemaInfo->sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
    pSchemaInfo->tversion = mr.me.stbEntry.schemaTag.version;
2619
  } else if (mr.me.type == TSDB_CHILD_TABLE) {
2620 2621
    tDecoderClear(&mr.coder);

2622 2623
    tb_uid_t suid = mr.me.ctbEntry.suid;
    metaGetTableEntryByUid(&mr, suid);
2624 2625
    pSchemaInfo->sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
    pSchemaInfo->tversion = mr.me.stbEntry.schemaTag.version;
2626
  } else {
2627
    pSchemaInfo->sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
2628
  }
2629 2630

  metaReaderClear(&mr);
2631

H
Haojun Liao 已提交
2632 2633 2634 2635 2636
  pSchemaInfo->qsw = extractQueriedColumnSchema(pScanNode);
  return TSDB_CODE_SUCCESS;
}

SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
2637 2638 2639
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);

2640
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
2641
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
2642

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

H
Haojun Liao 已提交
2647 2648 2649
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
    pSchema->colId = pColNode->colId;
    pSchema->type = pColNode->node.resType.type;
H
Haojun Liao 已提交
2650 2651
    pSchema->bytes = pColNode->node.resType.bytes;
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
2652 2653
  }

2654
  // this the tags and pseudo function columns, we only keep the tag columns
2655
  for (int32_t i = 0; i < numOfTags; ++i) {
2656 2657 2658 2659 2660 2661 2662 2663 2664
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);

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

      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
      pSchema->colId = pColNode->colId;
      pSchema->type = pColNode->node.resType.type;
H
Haojun Liao 已提交
2665
      pSchema->bytes = pColNode->node.resType.bytes;
H
Haojun Liao 已提交
2666
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
2667 2668 2669
    }
  }

H
Haojun Liao 已提交
2670
  return pqSw;
2671 2672
}

2673 2674
static void cleanupTableSchemaInfo(SSchemaInfo* pSchemaInfo) {
  taosMemoryFreeClear(pSchemaInfo->dbname);
2675
  taosMemoryFreeClear(pSchemaInfo->tablename);
2676 2677
  tDeleteSSchemaWrapper(pSchemaInfo->sw);
  tDeleteSSchemaWrapper(pSchemaInfo->qsw);
2678 2679
}

2680
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) { tDeleteSSchemaWrapper(pStreamInfo->schema); }
2681

2682
bool groupbyTbname(SNodeList* pGroupList) {
2683
  bool bytbname = false;
2684
  if (LIST_LENGTH(pGroupList) == 1) {
2685 2686 2687 2688 2689 2690 2691 2692 2693 2694
    SNode* p = nodesListGetNode(pGroupList, 0);
    if (p->type == QUERY_NODE_FUNCTION) {
      // partition by tbname/group by tbname
      bytbname = (strcmp(((struct SFunctionNode*)p)->functionName, "tbname") == 0);
    }
  }

  return bytbname;
}

2695 2696
SOperatorInfo* createOperatorTree(SPhysiNode* pPhyNode, SExecTaskInfo* pTaskInfo, SReadHandle* pHandle, SNode* pTagCond,
                                  SNode* pTagIndexCond, const char* pUser) {
2697
  int32_t         type = nodeType(pPhyNode);
2698
  STableListInfo* pTableListInfo = pTaskInfo->pTableInfoList;
2699
  const char*     idstr = GET_TASKID(pTaskInfo);
2700

X
Xiaoyu Wang 已提交
2701
  if (pPhyNode->pChildren == NULL || LIST_LENGTH(pPhyNode->pChildren) == 0) {
2702
    SOperatorInfo* pOperator = NULL;
H
Haojun Liao 已提交
2703
    if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == type) {
dengyihao's avatar
dengyihao 已提交
2704
      STableScanPhysiNode* pTableScanNode = (STableScanPhysiNode*)pPhyNode;
H
Haojun Liao 已提交
2705

2706 2707 2708 2709 2710 2711
      // NOTE: this is an patch to fix the physical plan
      // TODO remove it later
      if (pTableScanNode->scan.node.pLimit != NULL) {
        pTableScanNode->groupSort = true;
      }

L
Liu Jicong 已提交
2712 2713
      int32_t code =
          createScanTableListInfo(&pTableScanNode->scan, pTableScanNode->pGroupTags, pTableScanNode->groupSort, pHandle,
H
Haojun Liao 已提交
2714
                                  pTableListInfo, pTagCond, pTagIndexCond, pTaskInfo);
2715
      if (code) {
wmmhello's avatar
wmmhello 已提交
2716
        pTaskInfo->code = code;
2717
        qError("failed to createScanTableListInfo, code:%s, %s", tstrerror(code), idstr);
D
dapan1121 已提交
2718 2719
        return NULL;
      }
wmmhello's avatar
wmmhello 已提交
2720

2721
      code = extractTableSchemaInfo(pHandle, &pTableScanNode->scan, pTaskInfo);
S
slzhou 已提交
2722
      if (code) {
2723
        pTaskInfo->code = terrno;
wmmhello's avatar
wmmhello 已提交
2724 2725 2726
        return NULL;
      }

2727
      pOperator = createTableScanOperatorInfo(pTableScanNode, pHandle, pTaskInfo);
D
dapan1121 已提交
2728 2729 2730 2731 2732
      if (NULL == pOperator) {
        pTaskInfo->code = terrno;
        return NULL;
      }

2733 2734
      STableScanInfo* pScanInfo = pOperator->info;
      pTaskInfo->cost.pRecoder = &pScanInfo->readRecorder;
S
slzhou 已提交
2735 2736
    } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == type) {
      STableMergeScanPhysiNode* pTableScanNode = (STableMergeScanPhysiNode*)pPhyNode;
H
Haojun Liao 已提交
2737 2738 2739

      int32_t code = createScanTableListInfo(&pTableScanNode->scan, pTableScanNode->pGroupTags, true, pHandle,
                                             pTableListInfo, pTagCond, pTagIndexCond, pTaskInfo);
L
Liu Jicong 已提交
2740
      if (code) {
wmmhello's avatar
wmmhello 已提交
2741
        pTaskInfo->code = code;
H
Haojun Liao 已提交
2742
        qError("failed to createScanTableListInfo, code: %s", tstrerror(code));
wmmhello's avatar
wmmhello 已提交
2743 2744
        return NULL;
      }
2745

2746
      code = extractTableSchemaInfo(pHandle, &pTableScanNode->scan, pTaskInfo);
wmmhello's avatar
wmmhello 已提交
2747 2748 2749 2750
      if (code) {
        pTaskInfo->code = terrno;
        return NULL;
      }
wmmhello's avatar
wmmhello 已提交
2751

2752
      pOperator = createTableMergeScanOperatorInfo(pTableScanNode, pTableListInfo, pHandle, pTaskInfo);
D
dapan1121 已提交
2753 2754 2755 2756
      if (NULL == pOperator) {
        pTaskInfo->code = terrno;
        return NULL;
      }
wmmhello's avatar
wmmhello 已提交
2757

2758 2759
      STableScanInfo* pScanInfo = pOperator->info;
      pTaskInfo->cost.pRecoder = &pScanInfo->readRecorder;
H
Haojun Liao 已提交
2760
    } else if (QUERY_NODE_PHYSICAL_PLAN_EXCHANGE == type) {
2761 2762
      pOperator = createExchangeOperatorInfo(pHandle ? pHandle->pMsgCb->clientRpc : NULL, (SExchangePhysiNode*)pPhyNode,
                                             pTaskInfo);
H
Haojun Liao 已提交
2763
    } else if (QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN == type) {
5
54liuyao 已提交
2764
      STableScanPhysiNode* pTableScanNode = (STableScanPhysiNode*)pPhyNode;
5
54liuyao 已提交
2765
      if (pHandle->vnode) {
L
Liu Jicong 已提交
2766 2767
        int32_t code =
            createScanTableListInfo(&pTableScanNode->scan, pTableScanNode->pGroupTags, pTableScanNode->groupSort,
H
Haojun Liao 已提交
2768
                                    pHandle, pTableListInfo, pTagCond, pTagIndexCond, pTaskInfo);
L
Liu Jicong 已提交
2769
        if (code) {
wmmhello's avatar
wmmhello 已提交
2770
          pTaskInfo->code = code;
H
Haojun Liao 已提交
2771
          qError("failed to createScanTableListInfo, code: %s", tstrerror(code));
wmmhello's avatar
wmmhello 已提交
2772 2773
          return NULL;
        }
L
Liu Jicong 已提交
2774 2775

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

L
Liu Jicong 已提交
2779
        for (int32_t i = 0; i < sz; i++) {
H
Haojun Liao 已提交
2780
          STableKeyInfo* pKeyInfo = tableListGetInfo(pTableListInfo, i);
2781
          qDebug("add table uid:%" PRIu64 ", gid:%" PRIu64, pKeyInfo->uid, pKeyInfo->groupId);
L
Liu Jicong 已提交
2782 2783
        }
#endif
2784
      }
2785

H
Haojun Liao 已提交
2786
      pTaskInfo->schemaInfo.qsw = extractQueriedColumnSchema(&pTableScanNode->scan);
2787
      pOperator = createStreamScanOperatorInfo(pHandle, pTableScanNode, pTagCond, pTaskInfo);
H
Haojun Liao 已提交
2788
    } else if (QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == type) {
L
Liu Jicong 已提交
2789
      SSystemTableScanPhysiNode* pSysScanPhyNode = (SSystemTableScanPhysiNode*)pPhyNode;
2790
      pOperator = createSysTableScanOperatorInfo(pHandle, pSysScanPhyNode, pUser, pTaskInfo);
2791
    } else if (QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN == type) {
X
Xiaoyu Wang 已提交
2792
      STagScanPhysiNode* pScanPhyNode = (STagScanPhysiNode*)pPhyNode;
2793 2794

      int32_t code = createScanTableListInfo(pScanPhyNode, NULL, false, pHandle, pTableListInfo, pTagCond,
H
Haojun Liao 已提交
2795
                                             pTagIndexCond, pTaskInfo);
2796
      if (code != TSDB_CODE_SUCCESS) {
2797
        pTaskInfo->code = code;
H
Haojun Liao 已提交
2798
        qError("failed to getTableList, code: %s", tstrerror(code));
2799 2800 2801
        return NULL;
      }

2802
      pOperator = createTagScanOperatorInfo(pHandle, pScanPhyNode, pTableListInfo, pTaskInfo);
2803
    } else if (QUERY_NODE_PHYSICAL_PLAN_BLOCK_DIST_SCAN == type) {
2804
      SBlockDistScanPhysiNode* pBlockNode = (SBlockDistScanPhysiNode*)pPhyNode;
2805 2806

      if (pBlockNode->tableType == TSDB_SUPER_TABLE) {
H
Haojun Liao 已提交
2807 2808
        SArray* pList = taosArrayInit(4, sizeof(STableKeyInfo));
        int32_t code = vnodeGetAllTableList(pHandle->vnode, pBlockNode->uid, pList);
2809 2810 2811 2812
        if (code != TSDB_CODE_SUCCESS) {
          pTaskInfo->code = terrno;
          return NULL;
        }
H
Haojun Liao 已提交
2813

2814
        for (int32_t i = 0; i < tableListGetSize(pTableListInfo); ++i) {
H
Haojun Liao 已提交
2815
          STableKeyInfo* p = taosArrayGet(pList, i);
H
Haojun Liao 已提交
2816
          tableListAddTableInfo(pTableListInfo, p->uid, 0);
H
Haojun Liao 已提交
2817 2818
        }
        taosArrayDestroy(pList);
2819
      } else {  // Create group with only one table
H
Haojun Liao 已提交
2820
        tableListAddTableInfo(pTableListInfo, pBlockNode->uid, 0);
2821 2822
      }

2823
      pOperator = createDataBlockInfoScanOperator(pHandle, pBlockNode, pTaskInfo);
H
Haojun Liao 已提交
2824 2825 2826
    } else if (QUERY_NODE_PHYSICAL_PLAN_LAST_ROW_SCAN == type) {
      SLastRowScanPhysiNode* pScanNode = (SLastRowScanPhysiNode*)pPhyNode;

L
Liu Jicong 已提交
2827
      int32_t code = createScanTableListInfo(&pScanNode->scan, pScanNode->pGroupTags, true, pHandle, pTableListInfo,
H
Haojun Liao 已提交
2828
                                             pTagCond, pTagIndexCond, pTaskInfo);
2829 2830 2831 2832
      if (code != TSDB_CODE_SUCCESS) {
        pTaskInfo->code = code;
        return NULL;
      }
2833

2834
      code = extractTableSchemaInfo(pHandle, &pScanNode->scan, pTaskInfo);
2835 2836 2837
      if (code != TSDB_CODE_SUCCESS) {
        pTaskInfo->code = code;
        return NULL;
H
Haojun Liao 已提交
2838 2839
      }

2840
      pOperator = createCacherowsScanOperator(pScanNode, pHandle, pTaskInfo);
2841
    } else if (QUERY_NODE_PHYSICAL_PLAN_PROJECT == type) {
2842
      pOperator = createProjectOperatorInfo(NULL, (SProjectPhysiNode*)pPhyNode, pTaskInfo);
H
Haojun Liao 已提交
2843 2844
    } else {
      ASSERT(0);
H
Haojun Liao 已提交
2845
    }
2846 2847 2848 2849 2850

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

2851
    return pOperator;
H
Haojun Liao 已提交
2852 2853
  }

2854
  size_t          size = LIST_LENGTH(pPhyNode->pChildren);
2855
  SOperatorInfo** ops = taosMemoryCalloc(size, POINTER_BYTES);
2856 2857 2858 2859
  if (ops == NULL) {
    return NULL;
  }

dengyihao's avatar
dengyihao 已提交
2860
  for (int32_t i = 0; i < size; ++i) {
2861
    SPhysiNode* pChildNode = (SPhysiNode*)nodesListGetNode(pPhyNode->pChildren, i);
2862
    ops[i] = createOperatorTree(pChildNode, pTaskInfo, pHandle, pTagCond, pTagIndexCond, pUser);
2863
    if (ops[i] == NULL) {
H
Haojun Liao 已提交
2864
      taosMemoryFree(ops);
2865 2866
      return NULL;
    }
2867
  }
H
Haojun Liao 已提交
2868

2869
  SOperatorInfo* pOptr = NULL;
H
Haojun Liao 已提交
2870
  if (QUERY_NODE_PHYSICAL_PLAN_PROJECT == type) {
2871
    pOptr = createProjectOperatorInfo(ops[0], (SProjectPhysiNode*)pPhyNode, pTaskInfo);
2872
  } else if (QUERY_NODE_PHYSICAL_PLAN_HASH_AGG == type) {
H
Haojun Liao 已提交
2873 2874
    SAggPhysiNode* pAggNode = (SAggPhysiNode*)pPhyNode;
    if (pAggNode->pGroupKeys != NULL) {
H
Haojun Liao 已提交
2875
      pOptr = createGroupOperatorInfo(ops[0], pAggNode, pTaskInfo);
H
Haojun Liao 已提交
2876
    } else {
H
Haojun Liao 已提交
2877
      pOptr = createAggregateOperatorInfo(ops[0], pAggNode, pTaskInfo);
H
Haojun Liao 已提交
2878
    }
2879
  } else if (QUERY_NODE_PHYSICAL_PLAN_HASH_INTERVAL == type) {
H
Haojun Liao 已提交
2880
    SIntervalPhysiNode* pIntervalPhyNode = (SIntervalPhysiNode*)pPhyNode;
H
Haojun Liao 已提交
2881

H
Haojun Liao 已提交
2882 2883
    bool isStream = (QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL == type);
    pOptr = createIntervalOperatorInfo(ops[0], pIntervalPhyNode, pTaskInfo, isStream);
2884
  } else if (QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL == type) {
2885
    pOptr = createStreamIntervalOperatorInfo(ops[0], pPhyNode, pTaskInfo);
2886 2887
  } else if (QUERY_NODE_PHYSICAL_PLAN_MERGE_ALIGNED_INTERVAL == type) {
    SMergeAlignedIntervalPhysiNode* pIntervalPhyNode = (SMergeAlignedIntervalPhysiNode*)pPhyNode;
2888
    pOptr = createMergeAlignedIntervalOperatorInfo(ops[0], pIntervalPhyNode, pTaskInfo);
S
shenglian zhou 已提交
2889
  } else if (QUERY_NODE_PHYSICAL_PLAN_MERGE_INTERVAL == type) {
X
Xiaoyu Wang 已提交
2890
    SMergeIntervalPhysiNode* pIntervalPhyNode = (SMergeIntervalPhysiNode*)pPhyNode;
2891
    pOptr = createMergeIntervalOperatorInfo(ops[0], pIntervalPhyNode, pTaskInfo);
5
54liuyao 已提交
2892
  } else if (QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_INTERVAL == type) {
2893
    int32_t children = 0;
5
54liuyao 已提交
2894 2895
    pOptr = createStreamFinalIntervalOperatorInfo(ops[0], pPhyNode, pTaskInfo, children);
  } else if (QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_INTERVAL == type) {
5
54liuyao 已提交
2896
    int32_t children = pHandle->numOfVgroups;
5
54liuyao 已提交
2897
    pOptr = createStreamFinalIntervalOperatorInfo(ops[0], pPhyNode, pTaskInfo, children);
H
Haojun Liao 已提交
2898
  } else if (QUERY_NODE_PHYSICAL_PLAN_SORT == type) {
2899
    pOptr = createSortOperatorInfo(ops[0], (SSortPhysiNode*)pPhyNode, pTaskInfo);
S
shenglian zhou 已提交
2900 2901
  } else if (QUERY_NODE_PHYSICAL_PLAN_GROUP_SORT == type) {
    pOptr = createGroupSortOperatorInfo(ops[0], (SGroupSortPhysiNode*)pPhyNode, pTaskInfo);
X
Xiaoyu Wang 已提交
2902
  } else if (QUERY_NODE_PHYSICAL_PLAN_MERGE == type) {
2903
    SMergePhysiNode* pMergePhyNode = (SMergePhysiNode*)pPhyNode;
2904
    pOptr = createMultiwayMergeOperatorInfo(ops, size, pMergePhyNode, pTaskInfo);
2905
  } else if (QUERY_NODE_PHYSICAL_PLAN_MERGE_SESSION == type) {
H
Haojun Liao 已提交
2906
    SSessionWinodwPhysiNode* pSessionNode = (SSessionWinodwPhysiNode*)pPhyNode;
H
Haojun Liao 已提交
2907
    pOptr = createSessionAggOperatorInfo(ops[0], pSessionNode, pTaskInfo);
2908
  } else if (QUERY_NODE_PHYSICAL_PLAN_STREAM_SESSION == type) {
2909 2910 2911 2912 2913
    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) {
2914
    int32_t children = pHandle->numOfVgroups;
2915
    pOptr = createStreamFinalSessionAggOperatorInfo(ops[0], pPhyNode, pTaskInfo, children);
H
Haojun Liao 已提交
2916
  } else if (QUERY_NODE_PHYSICAL_PLAN_PARTITION == type) {
2917
    pOptr = createPartitionOperatorInfo(ops[0], (SPartitionPhysiNode*)pPhyNode, pTaskInfo);
2918
  } else if (QUERY_NODE_PHYSICAL_PLAN_STREAM_PARTITION == type) {
2919
    pOptr = createStreamPartitionOperatorInfo(ops[0], (SStreamPartitionPhysiNode*)pPhyNode, pTaskInfo);
2920
  } else if (QUERY_NODE_PHYSICAL_PLAN_MERGE_STATE == type) {
dengyihao's avatar
dengyihao 已提交
2921
    SStateWinodwPhysiNode* pStateNode = (SStateWinodwPhysiNode*)pPhyNode;
2922
    pOptr = createStatewindowOperatorInfo(ops[0], pStateNode, pTaskInfo);
2923
  } else if (QUERY_NODE_PHYSICAL_PLAN_STREAM_STATE == type) {
5
54liuyao 已提交
2924
    pOptr = createStreamStateAggOperatorInfo(ops[0], pPhyNode, pTaskInfo);
2925
  } else if (QUERY_NODE_PHYSICAL_PLAN_MERGE_JOIN == type) {
2926
    pOptr = createMergeJoinOperatorInfo(ops, size, (SSortMergeJoinPhysiNode*)pPhyNode, pTaskInfo);
2927
  } else if (QUERY_NODE_PHYSICAL_PLAN_FILL == type) {
H
Haojun Liao 已提交
2928
    pOptr = createFillOperatorInfo(ops[0], (SFillPhysiNode*)pPhyNode, pTaskInfo);
5
54liuyao 已提交
2929 2930
  } else if (QUERY_NODE_PHYSICAL_PLAN_STREAM_FILL == type) {
    pOptr = createStreamFillOperatorInfo(ops[0], (SStreamFillPhysiNode*)pPhyNode, pTaskInfo);
H
Haojun Liao 已提交
2931 2932
  } else if (QUERY_NODE_PHYSICAL_PLAN_INDEF_ROWS_FUNC == type) {
    pOptr = createIndefinitOutputOperatorInfo(ops[0], pPhyNode, pTaskInfo);
2933 2934
  } else if (QUERY_NODE_PHYSICAL_PLAN_INTERP_FUNC == type) {
    pOptr = createTimeSliceOperatorInfo(ops[0], pPhyNode, pTaskInfo);
H
Haojun Liao 已提交
2935 2936
  } else {
    ASSERT(0);
H
Haojun Liao 已提交
2937
  }
2938

2939
  taosMemoryFree(ops);
2940 2941 2942 2943
  if (pOptr) {
    pOptr->resultDataBlockId = pPhyNode->pOutputDataBlockDesc->dataBlockId;
  }

2944
  return pOptr;
2945
}
H
Haojun Liao 已提交
2946

L
Liu Jicong 已提交
2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959
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 {
2960 2961 2962
    SStreamScanInfo* pInfo = pOperator->info;
    ASSERT(pInfo->pTableScanOp->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN);
    *ppInfo = pInfo->pTableScanOp->info;
L
Liu Jicong 已提交
2963 2964 2965 2966
    return 0;
  }
}

2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988
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;
}

2989
#if 0
L
Liu Jicong 已提交
2990 2991 2992 2993 2994
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;
  }
2995

L
Liu Jicong 已提交
2996 2997 2998 2999
  STableScanPhysiNode* pNode = NULL;
  if (extractTableScanNode(plan->pNode, &pNode) < 0) {
    ASSERT(0);
  }
3000

H
Haojun Liao 已提交
3001
  tsdbReaderClose(pTableScanInfo->dataReader);
3002

L
Liu Jicong 已提交
3003
  STableListInfo info = {0};
H
Haojun Liao 已提交
3004
  pTableScanInfo->dataReader = doCreateDataReader(pNode, pHandle, &info, NULL);
L
Liu Jicong 已提交
3005 3006 3007 3008
  if (pTableScanInfo->dataReader == NULL) {
    ASSERT(0);
    qError("failed to create data reader");
    return TSDB_CODE_QRY_APP_ERROR;
3009
  }
L
Liu Jicong 已提交
3010
  // TODO: set uid and ts to data reader
3011 3012
  return 0;
}
3013
#endif
3014

C
Cary Xu 已提交
3015
int32_t encodeOperator(SOperatorInfo* ops, char** result, int32_t* length, int32_t* nOptrWithVal) {
wmmhello's avatar
wmmhello 已提交
3016
  int32_t code = TDB_CODE_SUCCESS;
3017
  char*   pCurrent = NULL;
wmmhello's avatar
wmmhello 已提交
3018
  int32_t currLength = 0;
3019
  if (ops->fpSet.encodeResultRow) {
C
Cary Xu 已提交
3020
    if (result == NULL || length == NULL || nOptrWithVal == NULL) {
wmmhello's avatar
wmmhello 已提交
3021 3022 3023
      return TSDB_CODE_TSC_INVALID_INPUT;
    }
    code = ops->fpSet.encodeResultRow(ops, &pCurrent, &currLength);
wmmhello's avatar
wmmhello 已提交
3024

3025 3026
    if (code != TDB_CODE_SUCCESS) {
      if (*result != NULL) {
wmmhello's avatar
wmmhello 已提交
3027 3028 3029 3030
        taosMemoryFree(*result);
        *result = NULL;
      }
      return code;
C
Cary Xu 已提交
3031 3032 3033
    } else if (currLength == 0) {
      ASSERT(!pCurrent);
      goto _downstream;
wmmhello's avatar
wmmhello 已提交
3034
    }
wmmhello's avatar
wmmhello 已提交
3035

C
Cary Xu 已提交
3036 3037
    ++(*nOptrWithVal);

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

3040
    if (*result == NULL) {
wmmhello's avatar
wmmhello 已提交
3041
      *result = (char*)taosMemoryCalloc(1, currLength + sizeof(int32_t));
wmmhello's avatar
wmmhello 已提交
3042 3043 3044 3045 3046 3047
      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);
3048
    } else {
wmmhello's avatar
wmmhello 已提交
3049
      int32_t sizePre = *(int32_t*)(*result);
3050
      char*   tmp = (char*)taosMemoryRealloc(*result, sizePre + currLength);
wmmhello's avatar
wmmhello 已提交
3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062
      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 已提交
3063 3064
  }

3065
_downstream:
wmmhello's avatar
wmmhello 已提交
3066
  for (int32_t i = 0; i < ops->numOfDownstream; ++i) {
C
Cary Xu 已提交
3067
    code = encodeOperator(ops->pDownstream[i], result, length, nOptrWithVal);
3068
    if (code != TDB_CODE_SUCCESS) {
wmmhello's avatar
wmmhello 已提交
3069
      return code;
wmmhello's avatar
wmmhello 已提交
3070 3071
    }
  }
wmmhello's avatar
wmmhello 已提交
3072
  return TDB_CODE_SUCCESS;
wmmhello's avatar
wmmhello 已提交
3073 3074
}

H
Haojun Liao 已提交
3075
int32_t decodeOperator(SOperatorInfo* ops, const char* result, int32_t length) {
wmmhello's avatar
wmmhello 已提交
3076
  int32_t code = TDB_CODE_SUCCESS;
3077 3078
  if (ops->fpSet.decodeResultRow) {
    if (result == NULL) {
wmmhello's avatar
wmmhello 已提交
3079 3080
      return TSDB_CODE_TSC_INVALID_INPUT;
    }
H
Haojun Liao 已提交
3081

3082
    ASSERT(length == *(int32_t*)result);
H
Haojun Liao 已提交
3083 3084

    const char* data = result + sizeof(int32_t);
L
Liu Jicong 已提交
3085
    code = ops->fpSet.decodeResultRow(ops, (char*)data);
3086
    if (code != TDB_CODE_SUCCESS) {
wmmhello's avatar
wmmhello 已提交
3087 3088
      return code;
    }
wmmhello's avatar
wmmhello 已提交
3089

wmmhello's avatar
wmmhello 已提交
3090
    int32_t totalLength = *(int32_t*)result;
3091 3092
    int32_t dataLength = *(int32_t*)data;

3093
    if (totalLength == dataLength + sizeof(int32_t)) {  // the last data
wmmhello's avatar
wmmhello 已提交
3094 3095
      result = NULL;
      length = 0;
3096
    } else {
wmmhello's avatar
wmmhello 已提交
3097 3098 3099 3100
      result += dataLength;
      *(int32_t*)(result) = totalLength - dataLength;
      length = totalLength - dataLength;
    }
wmmhello's avatar
wmmhello 已提交
3101 3102
  }

wmmhello's avatar
wmmhello 已提交
3103 3104
  for (int32_t i = 0; i < ops->numOfDownstream; ++i) {
    code = decodeOperator(ops->pDownstream[i], result, length);
3105
    if (code != TDB_CODE_SUCCESS) {
wmmhello's avatar
wmmhello 已提交
3106
      return code;
wmmhello's avatar
wmmhello 已提交
3107 3108
    }
  }
wmmhello's avatar
wmmhello 已提交
3109
  return TDB_CODE_SUCCESS;
wmmhello's avatar
wmmhello 已提交
3110 3111
}

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

D
dapan1121 已提交
3115
  switch (pNode->type) {
D
dapan1121 已提交
3116 3117 3118 3119 3120 3121
    case QUERY_NODE_PHYSICAL_PLAN_QUERY_INSERT: {
      SInserterParam* pInserterParam = taosMemoryCalloc(1, sizeof(SInserterParam));
      if (NULL == pInserterParam) {
        return TSDB_CODE_OUT_OF_MEMORY;
      }
      pInserterParam->readHandle = readHandle;
L
Liu Jicong 已提交
3122

D
dapan1121 已提交
3123 3124 3125
      *pParam = pInserterParam;
      break;
    }
D
dapan1121 已提交
3126
    case QUERY_NODE_PHYSICAL_PLAN_DELETE: {
3127
      SDeleterParam* pDeleterParam = taosMemoryCalloc(1, sizeof(SDeleterParam));
D
dapan1121 已提交
3128 3129 3130
      if (NULL == pDeleterParam) {
        return TSDB_CODE_OUT_OF_MEMORY;
      }
H
Haojun Liao 已提交
3131 3132 3133 3134
      int32_t tbNum = tableListGetSize(pTask->pTableInfoList);
      pDeleterParam->suid = tableListGetSuid(pTask->pTableInfoList);

      // TODO extract uid list
D
dapan1121 已提交
3135 3136 3137 3138 3139
      pDeleterParam->pUidList = taosArrayInit(tbNum, sizeof(uint64_t));
      if (NULL == pDeleterParam->pUidList) {
        taosMemoryFree(pDeleterParam);
        return TSDB_CODE_OUT_OF_MEMORY;
      }
H
Haojun Liao 已提交
3140

D
dapan1121 已提交
3141
      for (int32_t i = 0; i < tbNum; ++i) {
H
Haojun Liao 已提交
3142
        STableKeyInfo* pTable = tableListGetInfo(pTask->pTableInfoList, i);
D
dapan1121 已提交
3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155
        taosArrayPush(pDeleterParam->pUidList, &pTable->uid);
      }

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

  return TSDB_CODE_SUCCESS;
}

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

D
dapan1121 已提交
3160
  *pTaskInfo = createExecTaskInfo(queryId, taskId, model, pPlan->dbFName);
H
Haojun Liao 已提交
3161 3162 3163
  if (*pTaskInfo == NULL) {
    goto _complete;
  }
H
Haojun Liao 已提交
3164

3165
  if (pHandle) {
L
Liu Jicong 已提交
3166
    /*(*pTaskInfo)->streamInfo.fillHistoryVer1 = pHandle->fillHistoryVer1;*/
3167 3168 3169
    if (pHandle->pStateBackend) {
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
    }
H
Haojun Liao 已提交
3170 3171
  }

3172
  (*pTaskInfo)->sql = sql;
D
dapan1121 已提交
3173
  sql = NULL;
H
Haojun Liao 已提交
3174

3175
  (*pTaskInfo)->pSubplan = pPlan;
3176 3177
  (*pTaskInfo)->pRoot =
      createOperatorTree(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user);
L
Liu Jicong 已提交
3178

D
dapan1121 已提交
3179
  if (NULL == (*pTaskInfo)->pRoot) {
H
Haojun Liao 已提交
3180
    terrno = (*pTaskInfo)->code;
D
dapan1121 已提交
3181
    goto _complete;
3182 3183
  }

H
Haojun Liao 已提交
3184
  return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
3185

H
Haojun Liao 已提交
3186
_complete:
D
dapan1121 已提交
3187
  taosMemoryFree(sql);
H
Haojun Liao 已提交
3188
  doDestroyTask(*pTaskInfo);
H
Haojun Liao 已提交
3189
  return terrno;
H
Haojun Liao 已提交
3190 3191
}

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

H
Haojun Liao 已提交
3195
  pTaskInfo->pTableInfoList = tableListDestroy(pTaskInfo->pTableInfoList);
H
Haojun Liao 已提交
3196
  destroyOperatorInfo(pTaskInfo->pRoot);
3197
  cleanupTableSchemaInfo(&pTaskInfo->schemaInfo);
3198
  cleanupStreamInfo(&pTaskInfo->streamInfo);
3199

D
dapan1121 已提交
3200
  if (!pTaskInfo->localFetch.localExec) {
D
dapan1121 已提交
3201 3202
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
  }
3203

D
dapan1121 已提交
3204
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
wafwerar's avatar
wafwerar 已提交
3205 3206 3207
  taosMemoryFreeClear(pTaskInfo->sql);
  taosMemoryFreeClear(pTaskInfo->id.str);
  taosMemoryFreeClear(pTaskInfo);
3208 3209 3210 3211
}

static int64_t getQuerySupportBufSize(size_t numOfTables) {
  size_t s1 = sizeof(STableQueryInfo);
L
Liu Jicong 已提交
3212 3213
  //  size_t s3 = sizeof(STableCheckInfo);  buffer consumption in tsdb
  return (int64_t)(s1 * 1.5 * numOfTables);
3214 3215 3216 3217 3218 3219 3220
}

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 已提交
3221
    while (1) {
3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247
      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 已提交
3248

H
Haojun Liao 已提交
3249
int32_t getOperatorExplainExecInfo(SOperatorInfo* operatorInfo, SArray* pExecInfoList) {
3250
  SExplainExecInfo  execInfo = {0};
H
Haojun Liao 已提交
3251
  SExplainExecInfo* pExplainInfo = taosArrayPush(pExecInfoList, &execInfo);
3252

H
Haojun Liao 已提交
3253 3254 3255 3256 3257
  pExplainInfo->numOfRows = operatorInfo->resultInfo.totalRows;
  pExplainInfo->startupCost = operatorInfo->cost.openCost;
  pExplainInfo->totalCost = operatorInfo->cost.totalCost;
  pExplainInfo->verboseLen = 0;
  pExplainInfo->verboseInfo = NULL;
D
dapan1121 已提交
3258

3259
  if (operatorInfo->fpSet.getExplainFn) {
3260 3261
    int32_t code =
        operatorInfo->fpSet.getExplainFn(operatorInfo, &pExplainInfo->verboseInfo, &pExplainInfo->verboseLen);
D
dapan1121 已提交
3262
    if (code) {
3263
      qError("%s operator getExplainFn failed, code:%s", GET_TASKID(operatorInfo->pTaskInfo), tstrerror(code));
D
dapan1121 已提交
3264 3265 3266
      return code;
    }
  }
dengyihao's avatar
dengyihao 已提交
3267

D
dapan1121 已提交
3268
  int32_t code = 0;
D
dapan1121 已提交
3269
  for (int32_t i = 0; i < operatorInfo->numOfDownstream; ++i) {
H
Haojun Liao 已提交
3270 3271
    code = getOperatorExplainExecInfo(operatorInfo->pDownstream[i], pExecInfoList);
    if (code != TSDB_CODE_SUCCESS) {
3272
      //      taosMemoryFreeClear(*pRes);
D
dapan1121 已提交
3273 3274 3275 3276 3277
      return TSDB_CODE_QRY_OUT_OF_MEMORY;
    }
  }

  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
3278
}
5
54liuyao 已提交
3279

3280 3281
int32_t setOutputBuf(SStreamState* pState, STimeWindow* win, SResultRow** pResult, int64_t tableGroupId,
                     SqlFunctionCtx* pCtx, int32_t numOfOutput, int32_t* rowEntryInfoOffset, SAggSupporter* pAggSup) {
3282 3283 3284 3285 3286 3287
  SWinKey key = {
      .ts = win->skey,
      .groupId = tableGroupId,
  };
  char*   value = NULL;
  int32_t size = pAggSup->resultRowSize;
5
54liuyao 已提交
3288

3289
  if (streamStateAddIfNotExist(pState, &key, (void**)&value, &size) < 0) {
3290 3291 3292 3293 3294 3295 3296 3297 3298 3299
    return TSDB_CODE_QRY_OUT_OF_MEMORY;
  }
  *pResult = (SResultRow*)value;
  ASSERT(*pResult);
  // set time window for current result
  (*pResult)->win = (*win);
  setResultRowInitCtx(*pResult, pCtx, numOfOutput, rowEntryInfoOffset);
  return TSDB_CODE_SUCCESS;
}

3300 3301
int32_t releaseOutputBuf(SStreamState* pState, SWinKey* pKey, SResultRow* pResult) {
  streamStateReleaseBuf(pState, pKey, pResult);
3302 3303 3304
  return TSDB_CODE_SUCCESS;
}

3305 3306
int32_t saveOutputBuf(SStreamState* pState, SWinKey* pKey, SResultRow* pResult, int32_t resSize) {
  streamStatePut(pState, pKey, pResult, resSize);
3307 3308 3309
  return TSDB_CODE_SUCCESS;
}

3310
int32_t buildDataBlockFromGroupRes(SOperatorInfo* pOperator, SStreamState* pState, SSDataBlock* pBlock, SExprSupp* pSup,
3311
                                   SGroupResInfo* pGroupResInfo) {
3312
  SExecTaskInfo*  pTaskInfo = pOperator->pTaskInfo;
3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324
  SExprInfo*      pExprInfo = pSup->pExprInfo;
  int32_t         numOfExprs = pSup->numOfExprs;
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
  SqlFunctionCtx* pCtx = pSup->pCtx;

  int32_t numOfRows = getNumOfTotalRes(pGroupResInfo);

  for (int32_t i = pGroupResInfo->index; i < numOfRows; i += 1) {
    SResKeyPos* pPos = taosArrayGetP(pGroupResInfo->pRows, i);
    int32_t     size = 0;
    void*       pVal = NULL;
    SWinKey     key = {
3325 3326
            .ts = *(TSKEY*)pPos->key,
            .groupId = pPos->groupId,
3327
    };
3328
    int32_t code = streamStateGet(pState, &key, &pVal, &size);
3329 3330 3331 3332 3333 3334
    ASSERT(code == 0);
    SResultRow* pRow = (SResultRow*)pVal;
    doUpdateNumOfRows(pCtx, pRow, numOfExprs, rowEntryOffset);
    // no results, continue to check the next one
    if (pRow->numOfRows == 0) {
      pGroupResInfo->index += 1;
3335
      releaseOutputBuf(pState, &key, pRow);
3336 3337 3338 3339 3340
      continue;
    }

    if (pBlock->info.groupId == 0) {
      pBlock->info.groupId = pPos->groupId;
3341 3342
      void* tbname = NULL;
      if (streamStateGetParName(pTaskInfo->streamInfo.pState, pBlock->info.groupId, &tbname) < 0) {
L
Liu Jicong 已提交
3343
        pBlock->info.parTbName[0] = 0;
3344 3345
      } else {
        memcpy(pBlock->info.parTbName, tbname, TSDB_TABLE_NAME_LEN);
3346
      }
3347
      tdbFree(tbname);
3348 3349 3350
    } else {
      // current value belongs to different group, it can't be packed into one datablock
      if (pBlock->info.groupId != pPos->groupId) {
3351
        releaseOutputBuf(pState, &key, pRow);
3352 3353 3354 3355 3356 3357
        break;
      }
    }

    if (pBlock->info.rows + pRow->numOfRows > pBlock->info.capacity) {
      ASSERT(pBlock->info.rows > 0);
3358
      releaseOutputBuf(pState, &key, pRow);
3359 3360 3361 3362 3363 3364 3365 3366 3367 3368
      break;
    }

    pGroupResInfo->index += 1;

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

      pCtx[j].resultInfo = getResultEntryInfo(pRow, j, rowEntryOffset);
      if (pCtx[j].fpSet.finalize) {
3369 3370 3371 3372
        int32_t code1 = pCtx[j].fpSet.finalize(&pCtx[j], pBlock);
        if (TAOS_FAILED(code1)) {
          qError("%s build result data block error, code %s", GET_TASKID(pTaskInfo), tstrerror(code1));
          T_LONG_JMP(pTaskInfo->env, code1);
3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385
        }
      } else if (strcmp(pCtx[j].pExpr->pExpr->_function.functionName, "_select_value") == 0) {
        // do nothing, todo refactor
      } else {
        // expand the result into multiple rows. E.g., _wstart, top(k, 20)
        // the _wstart needs to copy to 20 following rows, since the results of top-k expands to 20 different rows.
        SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, slotId);
        char*            in = GET_ROWCELL_INTERBUF(pCtx[j].resultInfo);
        for (int32_t k = 0; k < pRow->numOfRows; ++k) {
          colDataAppend(pColInfoData, pBlock->info.rows + k, in, pCtx[j].resultInfo->isNullRes);
        }
      }
    }
5
54liuyao 已提交
3386

3387
    pBlock->info.rows += pRow->numOfRows;
3388
    releaseOutputBuf(pState, &key, pRow);
3389 3390 3391 3392
  }
  blockDataUpdateTsWindow(pBlock, 0);
  return TSDB_CODE_SUCCESS;
}
5
54liuyao 已提交
3393 3394 3395 3396 3397 3398 3399

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

3400
int32_t buildSessionResultDataBlock(SOperatorInfo* pOperator, SStreamState* pState, SSDataBlock* pBlock,
5
54liuyao 已提交
3401
                                    SExprSupp* pSup, SGroupResInfo* pGroupResInfo) {
3402
  SExecTaskInfo*  pTaskInfo = pOperator->pTaskInfo;
5
54liuyao 已提交
3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415
  SExprInfo*      pExprInfo = pSup->pExprInfo;
  int32_t         numOfExprs = pSup->numOfExprs;
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
  SqlFunctionCtx* pCtx = pSup->pCtx;

  int32_t numOfRows = getNumOfTotalRes(pGroupResInfo);

  for (int32_t i = pGroupResInfo->index; i < numOfRows; i += 1) {
    SSessionKey* pKey = taosArrayGet(pGroupResInfo->pRows, i);
    int32_t      size = 0;
    void*        pVal = NULL;
    int32_t      code = streamStateSessionGet(pState, pKey, &pVal, &size);
    ASSERT(code == 0);
3416 3417
    if (code == -1) {
      // coverity scan
5
54liuyao 已提交
3418
      pGroupResInfo->index += 1;
3419 3420
      continue;
    }
5
54liuyao 已提交
3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431
    SResultRow* pRow = (SResultRow*)pVal;
    doUpdateNumOfRows(pCtx, pRow, numOfExprs, rowEntryOffset);
    // no results, continue to check the next one
    if (pRow->numOfRows == 0) {
      pGroupResInfo->index += 1;
      releaseOutputBuf(pState, NULL, pRow);
      continue;
    }

    if (pBlock->info.groupId == 0) {
      pBlock->info.groupId = pKey->groupId;
3432

3433 3434 3435
      void* tbname = NULL;
      if (streamStateGetParName(pTaskInfo->streamInfo.pState, pBlock->info.groupId, &tbname) < 0) {
        pBlock->info.parTbName[0] = 0;
3436
      } else {
3437
        memcpy(pBlock->info.parTbName, tbname, TSDB_TABLE_NAME_LEN);
3438
      }
3439
      tdbFree(tbname);
5
54liuyao 已提交
3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484
    } else {
      // current value belongs to different group, it can't be packed into one datablock
      if (pBlock->info.groupId != pKey->groupId) {
        releaseOutputBuf(pState, NULL, pRow);
        break;
      }
    }

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

    pGroupResInfo->index += 1;

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

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

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