executorimpl.c 126.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
15

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

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

31
#include "executorimpl.h"
dengyihao's avatar
dengyihao 已提交
32
#include "index.h"
33
#include "query.h"
34
#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
        numOfRows = pfCtx->fpSet.process(pfCtx);
H
Haojun Liao 已提交
663
      } else if (fmIsAggFunc(pfCtx->functionId)) {
G
Ganlin Zhao 已提交
664
        // selective value output should be set during corresponding function execution
665 666 667
        if (fmIsSelectValueFunc(pfCtx->functionId)) {
          continue;
        }
668 669
        // _group_key function for "partition by tbname" + csum(col_name) query
        SColumnInfoData* pOutput = taosArrayGet(pResult->pDataBlock, outputSlotId);
670
        int32_t          slotId = pfCtx->param[0].pCol->slotId;
671 672 673

        // todo handle the json tag
        SColumnInfoData* pInput = taosArrayGet(pSrcBlock->pDataBlock, slotId);
674
        for (int32_t f = 0; f < pSrcBlock->info.rows; ++f) {
675 676 677 678 679 680 681 682 683
          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 已提交
684 685 686
      } else {
        SArray* pBlockList = taosArrayInit(4, POINTER_BYTES);
        taosArrayPush(pBlockList, &pSrcBlock);
G
Ganlin Zhao 已提交
687

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

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

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

        numOfRows = dest.numOfRows;
H
Haojun Liao 已提交
704 705
        taosArrayDestroy(pBlockList);
      }
706
    } else {
707
      return TSDB_CODE_OPS_NOT_SUPPORT;
708 709
    }
  }
710

711 712 713
  if (!createNewColModel) {
    pResult->info.rows += numOfRows;
  }
714 715

  return TSDB_CODE_SUCCESS;
716 717
}

5
54liuyao 已提交
718
bool functionNeedToExecute(SqlFunctionCtx* pCtx) {
719
  struct SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
720

721 722 723 724 725
  // in case of timestamp column, always generated results.
  int32_t functionId = pCtx->functionId;
  if (functionId == -1) {
    return false;
  }
726

727 728
  if (pCtx->scanFlag == REPEAT_SCAN) {
    return fmIsRepeatScanFunc(pCtx->functionId);
729 730
  }

731 732
  if (isRowEntryCompleted(pResInfo)) {
    return false;
733 734
  }

735 736 737
  return true;
}

738 739 740 741 742 743 744
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;
    }
745

746 747 748
    // Set the correct column info (data type and bytes)
    pInput->pData[paramIndex]->info.type = type;
    pInput->pData[paramIndex]->info.bytes = tDataTypes[type].bytes;
749
  }
H
Haojun Liao 已提交
750

751 752 753 754 755 756
  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;
757 758
    }
  } else {
759
    da = pInput->pColumnDataAgg[paramIndex];
760 761
  }

762
  ASSERT(!IS_VAR_DATA_TYPE(type));
763

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

771 772 773 774 775 776
    *(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;

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

787 788
  return TSDB_CODE_SUCCESS;
}
789

790
void setBlockSMAInfo(SqlFunctionCtx* pCtx, SExprInfo* pExprInfo, SSDataBlock* pBlock) {
791 792 793 794 795 796 797 798 799
  int32_t numOfRows = pBlock->info.rows;

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

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

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

803 804
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
        int32_t slotId = pFuncParam->pCol->slotId;
805 806 807 808
        pInput->pColumnDataAgg[j] = pBlock->pBlockAgg[slotId];
        if (pInput->pColumnDataAgg[j] == NULL) {
          pInput->colDataAggIsSet = false;
        }
809 810 811 812

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

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

  return false;
}

L
Liu Jicong 已提交
837
void setTaskKilled(SExecTaskInfo* pTaskInfo) { pTaskInfo->code = TSDB_CODE_TSC_QUERY_CANCELLED; }
838 839

/////////////////////////////////////////////////////////////////////////////////////////////
840
STimeWindow getAlignQueryTimeWindow(SInterval* pInterval, int32_t precision, int64_t key) {
L
Liu Jicong 已提交
841
  STimeWindow win = {0};
842
  win.skey = taosTimeTruncate(key, pInterval, precision);
843 844

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

  return win;
854 855
}

L
Liu Jicong 已提交
856 857
int32_t loadDataBlockOnDemand(SExecTaskInfo* pTaskInfo, STableScanInfo* pTableScanInfo, SSDataBlock* pBlock,
                              uint32_t* status) {
858
  *status = BLK_DATA_NOT_LOAD;
859

H
Haojun Liao 已提交
860
  pBlock->pDataBlock = NULL;
L
Liu Jicong 已提交
861
  pBlock->pBlockAgg = NULL;
H
Haojun Liao 已提交
862

L
Liu Jicong 已提交
863 864
  //  int64_t groupId = pRuntimeEnv->current->groupIndex;
  //  bool    ascQuery = QUERY_IS_ASC_QUERY(pQueryAttr);
865

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

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

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

    if (needFilter) {
      (*status) = doFilterByBlockTimeWindow(pTableScanInfo, pBlock);
    } else {
911
      (*status) = BLK_DATA_DATA_LOAD;
912 913 914 915
    }
  }

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

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

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

    // load the data block statistics to perform further filter
    pCost->loadBlockStatis += 1;
936
//    tsdbRetrieveDatablockSMA(pTableScanInfo->pTsdbReadHandle, &pBlock->pBlockAgg);
937 938 939 940 941 942

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

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

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

    pCost->totalCheckedRows += pBlockInfo->rows;
    pCost->loadBlocks += 1;
982
//    pBlock->pDataBlock = tsdbRetrieveDataBlock(pTableScanInfo->pTsdbReadHandle, NULL);
983 984 985 986 987
//    if (pBlock->pDataBlock == NULL) {
//      return terrno;
//    }

//    if (pQueryAttr->pFilters != NULL) {
988
//      filterSetColFieldData(pQueryAttr->pFilters, taosArrayGetSize(pBlock->pDataBlock), pBlock->pDataBlock);
989
//    }
990

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

L
Liu Jicong 已提交
999
static void updateTableQueryInfoForReverseScan(STableQueryInfo* pTableQueryInfo) {
1000 1001 1002 1003 1004
  if (pTableQueryInfo == NULL) {
    return;
  }
}

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

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

    struct SResultRowEntryInfo* pResInfo = pCtx[i].resultInfo;
    if (isRowEntryCompleted(pResInfo) && isRowEntryInitialized(pResInfo)) {
      continue;
    }
1027 1028 1029 1030 1031

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

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

1044 1045
static void extractQualifiedTupleByFilterResult(SSDataBlock* pBlock, const SColumnInfoData* p, bool keep,
                                                int32_t status);
1046

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

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

1055
  SColumnInfoData* p = NULL;
1056
  int32_t          status = 0;
H
Haojun Liao 已提交
1057

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

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

1076 1077
  colDataDestroy(p);
  taosMemoryFree(p);
1078 1079
}

1080
void extractQualifiedTupleByFilterResult(SSDataBlock* pBlock, const SColumnInfoData* p, bool keep, int32_t status) {
1081 1082 1083 1084
  if (keep) {
    return;
  }

H
Haojun Liao 已提交
1085 1086 1087
  int32_t totalRows = pBlock->info.rows;

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

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

1103 1104
      colInfoDataCleanup(pDst, pBlock->info.rows);

1105
      int32_t numOfRows = 0;
1106
      for (int32_t j = 0; j < totalRows; ++j) {
1107
        if (((int8_t*)p->pData)[j] == 0) {
D
dapan1121 已提交
1108 1109
          continue;
        }
1110

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

1119
      // todo this value can be assigned directly
1120 1121 1122 1123 1124
      if (pBlock->info.rows == totalRows) {
        pBlock->info.rows = numOfRows;
      } else {
        ASSERT(pBlock->info.rows == numOfRows);
      }
1125
    }
1126

dengyihao's avatar
dengyihao 已提交
1127
    blockDataDestroy(px);  // fix memory leak
1128 1129 1130
  }
}

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

1136
  SResultRowInfo* pResultRowInfo = &pAggInfo->binfo.resultRowInfo;
1137 1138
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
  int32_t*        rowEntryInfoOffset = pOperator->exprSupp.rowEntryInfoOffset;
1139

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

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

1156
  setResultRowInitCtx(pResultRow, pCtx, numOfOutput, rowEntryInfoOffset);
1157 1158
}

1159 1160 1161
static void setExecutionContext(SOperatorInfo* pOperator, int32_t numOfOutput, uint64_t groupId) {
  SAggOperatorInfo* pAggInfo = pOperator->info;
  if (pAggInfo->groupId != UINT64_MAX && pAggInfo->groupId == groupId) {
1162 1163
    return;
  }
1164 1165

  doSetTableGroupOutputBuf(pOperator, numOfOutput, groupId);
1166 1167

  // record the current active group id
H
Haojun Liao 已提交
1168
  pAggInfo->groupId = groupId;
1169 1170
}

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

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

1184
    if (fmIsNotNullOutputFunc(pCtx[j].functionId)) {
1185 1186
      returnNotNull = true;
    }
1187
  }
S
shenglian zhou 已提交
1188 1189
  // 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
1190
  if (pRow->numOfRows == 0 && !returnNotNull) {
1191
    pRow->numOfRows = 1;
1192 1193 1194
  }
}

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

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

1210 1211 1212
      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));
1213
        T_LONG_JMP(pTaskInfo->env, code);
1214 1215
      }
    } else if (strcmp(pCtx[j].pExpr->pExpr->_function.functionName, "_select_value") == 0) {
1216
      // do nothing
1217
    } else {
1218 1219
      // 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.
1220 1221 1222 1223 1224 1225 1226
      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);
      }
    }
  }
1227 1228
}

1229 1230 1231
// todo refactor. SResultRow has direct pointer in miainfo
int32_t finalizeResultRows(SDiskbasedBuf* pBuf, SResultRowPosition* resultRowPosition, SExprSupp* pSup,
                           SSDataBlock* pBlock, SExecTaskInfo* pTaskInfo) {
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
  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);
1258 1259

  releaseBufPage(pBuf, page);
1260
  pBlock->info.rows += pRow->numOfRows;
1261 1262 1263
  return 0;
}

1264 1265 1266 1267 1268 1269 1270
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;

1271
  int32_t numOfRows = getNumOfTotalRes(pGroupResInfo);
1272

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

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

H
Haojun Liao 已提交
1279
    doUpdateNumOfRows(pCtx, pRow, numOfExprs, rowEntryOffset);
1280 1281

    // no results, continue to check the next one
1282 1283
    if (pRow->numOfRows == 0) {
      pGroupResInfo->index += 1;
1284
      releaseBufPage(pBuf, page);
1285 1286 1287
      continue;
    }

1288 1289 1290 1291 1292
    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) {
1293
        releaseBufPage(pBuf, page);
1294 1295 1296 1297
        break;
      }
    }

1298
    if (pBlock->info.rows + pRow->numOfRows > pBlock->info.capacity) {
1299
      ASSERT(pBlock->info.rows > 0);
1300
      releaseBufPage(pBuf, page);
1301 1302 1303 1304
      break;
    }

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

1307
    releaseBufPage(pBuf, page);
1308
    pBlock->info.rows += pRow->numOfRows;
1309 1310
  }

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

1314
  blockDataUpdateTsWindow(pBlock, 0);
1315 1316 1317
  return 0;
}

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

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

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

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

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

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

X
Xiaoyu Wang 已提交
1358 1359
void doBuildResultDatablock(SOperatorInfo* pOperator, SOptrBasicInfo* pbInfo, SGroupResInfo* pGroupResInfo,
                            SDiskbasedBuf* pBuf) {
1360
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
1361
  SSDataBlock*   pBlock = pbInfo->pRes;
1362

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

1366
  blockDataCleanup(pBlock);
1367
  if (!hasRemainResults(pGroupResInfo)) {
1368 1369 1370
    return;
  }

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

1382 1383
      // clearing group id to continue to merge data that belong to different groups
      pBlock->info.groupId = 0;
1384
    }
1385 1386 1387

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

L
Liu Jicong 已提交
1391 1392
void queryCostStatis(SExecTaskInfo* pTaskInfo) {
  STaskCostInfo* pSummary = &pTaskInfo->cost;
1393

1394 1395
  SFileBlockLoadRecorder* pRecorder = pSummary->pRecoder;
  if (pSummary->pRecoder != NULL) {
1396
    qDebug(
H
Haojun Liao 已提交
1397 1398 1399 1400 1401
        "%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);
1402
  }
1403 1404
}

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

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

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

1646
int32_t appendDownstream(SOperatorInfo* p, SOperatorInfo** pDownstream, int32_t num) {
H
Haojun Liao 已提交
1647
  if (p->pDownstream == NULL) {
H
Haojun Liao 已提交
1648
    assert(p->numOfDownstream == 0);
1649 1650
  }

wafwerar's avatar
wafwerar 已提交
1651
  p->pDownstream = taosMemoryCalloc(1, num * POINTER_BYTES);
1652 1653 1654 1655 1656 1657 1658
  if (p->pDownstream == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

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

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

L
Liu Jicong 已提交
1664
static bool needToMerge(SSDataBlock* pBlock, SArray* groupInfo, char** buf, int32_t rowIndex) {
1665 1666 1667 1668
  size_t size = taosArrayGetSize(groupInfo);
  if (size == 0) {
    return true;
  }
1669

1670 1671
  for (int32_t i = 0; i < size; ++i) {
    int32_t* index = taosArrayGet(groupInfo, i);
1672

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

1676 1677 1678
    if ((isNull && buf[i] != NULL) || (!isNull && buf[i] == NULL)) {
      return false;
    }
1679

1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692
    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;
      }
1693 1694 1695
    }
  }

1696
  return 0;
1697 1698
}

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

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

1706 1707 1708
    char* data = colDataGetData(pColInfo, rowIndex);
    memcpy(rowColData[i], data, colDataGetLength(pColInfo, rowIndex));
  }
1709

1710 1711
  return true;
}
1712

X
Xiaoyu Wang 已提交
1713
int32_t getTableScanInfo(SOperatorInfo* pOperator, int32_t* order, int32_t* scanFlag) {
1714
  // todo add more information about exchange operation
1715
  int32_t type = pOperator->operatorType;
X
Xiaoyu Wang 已提交
1716
  if (type == QUERY_NODE_PHYSICAL_PLAN_EXCHANGE || type == QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN ||
1717
      type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN || type == QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN ||
1718
      type == QUERY_NODE_PHYSICAL_PLAN_BLOCK_DIST_SCAN || type == QUERY_NODE_PHYSICAL_PLAN_LAST_ROW_SCAN) {
1719 1720 1721
    *order = TSDB_ORDER_ASC;
    *scanFlag = MAIN_SCAN;
    return TSDB_CODE_SUCCESS;
1722
  } else if (type == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
1723 1724 1725 1726
    STableScanInfo* pTableScanInfo = pOperator->info;
    *order = pTableScanInfo->cond.order;
    *scanFlag = pTableScanInfo->scanFlag;
    return TSDB_CODE_SUCCESS;
1727 1728 1729 1730 1731
  } 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;
1732
  } else {
H
Haojun Liao 已提交
1733
    if (pOperator->pDownstream == NULL || pOperator->pDownstream[0] == NULL) {
1734
      return TSDB_CODE_INVALID_PARA;
H
Haojun Liao 已提交
1735
    } else {
1736
      return getTableScanInfo(pOperator->pDownstream[0], order, scanFlag);
1737 1738 1739
    }
  }
}
1740

1741
// this is a blocking operator
L
Liu Jicong 已提交
1742
static int32_t doOpenAggregateOptr(SOperatorInfo* pOperator) {
H
Haojun Liao 已提交
1743 1744
  if (OPTR_IS_OPENED(pOperator)) {
    return TSDB_CODE_SUCCESS;
1745 1746
  }

H
Haojun Liao 已提交
1747
  SExecTaskInfo*    pTaskInfo = pOperator->pTaskInfo;
1748
  SAggOperatorInfo* pAggInfo = pOperator->info;
H
Haojun Liao 已提交
1749

1750 1751
  SExprSupp*     pSup = &pOperator->exprSupp;
  SOperatorInfo* downstream = pOperator->pDownstream[0];
1752

1753 1754
  int64_t st = taosGetTimestampUs();

1755 1756 1757
  int32_t order = TSDB_ORDER_ASC;
  int32_t scanFlag = MAIN_SCAN;

H
Haojun Liao 已提交
1758
  while (1) {
1759
    SSDataBlock* pBlock = downstream->fpSet.getNextFn(downstream);
1760 1761 1762 1763
    if (pBlock == NULL) {
      break;
    }

1764 1765
    int32_t code = getTableScanInfo(pOperator, &order, &scanFlag);
    if (code != TSDB_CODE_SUCCESS) {
1766
      T_LONG_JMP(pTaskInfo->env, code);
1767
    }
1768

1769
    // there is an scalar expression that needs to be calculated before apply the group aggregation.
1770 1771 1772
    if (pAggInfo->scalarExprSup.pExprInfo != NULL) {
      SExprSupp* pSup1 = &pAggInfo->scalarExprSup;
      code = projectApplyFunctions(pSup1->pExprInfo, pBlock, pBlock, pSup1->pCtx, pSup1->numOfExprs, NULL);
1773
      if (code != TSDB_CODE_SUCCESS) {
1774
        T_LONG_JMP(pTaskInfo->env, code);
1775
      }
1776 1777
    }

1778
    // the pDataBlock are always the same one, no need to call this again
1779
    setExecutionContext(pOperator, pOperator->exprSupp.numOfExprs, pBlock->info.groupId);
1780
    setInputDataBlock(pSup, pBlock, order, scanFlag, true);
1781
    code = doAggregateImpl(pOperator, pSup->pCtx);
1782
    if (code != 0) {
1783
      T_LONG_JMP(pTaskInfo->env, code);
1784
    }
1785 1786
  }

1787
  initGroupedResultInfo(&pAggInfo->groupResInfo, pAggInfo->aggSup.pResultRowHashTable, 0);
H
Haojun Liao 已提交
1788
  OPTR_SET_OPENED(pOperator);
1789

1790
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
H
Haojun Liao 已提交
1791 1792 1793
  return TSDB_CODE_SUCCESS;
}

1794
static SSDataBlock* getAggregateResult(SOperatorInfo* pOperator) {
L
Liu Jicong 已提交
1795
  SAggOperatorInfo* pAggInfo = pOperator->info;
H
Haojun Liao 已提交
1796 1797 1798 1799 1800 1801
  SOptrBasicInfo*   pInfo = &pAggInfo->binfo;

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

L
Liu Jicong 已提交
1802
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
1803
  pTaskInfo->code = pOperator->fpSet._openFn(pOperator);
H
Haojun Liao 已提交
1804
  if (pTaskInfo->code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
1805
    setOperatorCompleted(pOperator);
H
Haojun Liao 已提交
1806 1807 1808
    return NULL;
  }

H
Haojun Liao 已提交
1809
  blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
S
slzhou 已提交
1810 1811
  while (1) {
    doBuildResultDatablock(pOperator, pInfo, &pAggInfo->groupResInfo, pAggInfo->aggSup.pResultBuf);
H
Haojun Liao 已提交
1812
    doFilter(pInfo->pRes, pOperator->exprSupp.pFilterInfo, NULL);
S
slzhou 已提交
1813

1814
    if (!hasRemainResults(&pAggInfo->groupResInfo)) {
H
Haojun Liao 已提交
1815
      setOperatorCompleted(pOperator);
S
slzhou 已提交
1816 1817
      break;
    }
1818

S
slzhou 已提交
1819 1820 1821 1822
    if (pInfo->pRes->info.rows > 0) {
      break;
    }
  }
1823

1824
  size_t rows = blockDataGetNumOfRows(pInfo->pRes);
1825 1826
  pOperator->resultInfo.totalRows += rows;

1827
  return (rows == 0) ? NULL : pInfo->pRes;
1828 1829
}

wmmhello's avatar
wmmhello 已提交
1830
int32_t aggEncodeResultRow(SOperatorInfo* pOperator, char** result, int32_t* length) {
1831
  if (result == NULL || length == NULL) {
wmmhello's avatar
wmmhello 已提交
1832 1833 1834
    return TSDB_CODE_TSC_INVALID_INPUT;
  }
  SOptrBasicInfo* pInfo = (SOptrBasicInfo*)(pOperator->info);
1835
  SAggSupporter*  pSup = (SAggSupporter*)POINTER_SHIFT(pOperator->info, sizeof(SOptrBasicInfo));
1836
  int32_t         size = tSimpleHashGetSize(pSup->pResultRowHashTable);
1837 1838 1839
  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 已提交
1840

C
Cary Xu 已提交
1841 1842 1843 1844 1845 1846
  // no result
  if (getTotalBufSize(pSup->pResultBuf) == 0) {
    *result = NULL;
    *length = 0;
    return TSDB_CODE_SUCCESS;
  }
1847

wmmhello's avatar
wmmhello 已提交
1848
  *result = (char*)taosMemoryCalloc(1, totalSize);
L
Liu Jicong 已提交
1849
  if (*result == NULL) {
wmmhello's avatar
wmmhello 已提交
1850
    return TSDB_CODE_OUT_OF_MEMORY;
wmmhello's avatar
wmmhello 已提交
1851
  }
wmmhello's avatar
wmmhello 已提交
1852

wmmhello's avatar
wmmhello 已提交
1853
  int32_t offset = sizeof(int32_t);
wmmhello's avatar
wmmhello 已提交
1854 1855
  *(int32_t*)(*result + offset) = size;
  offset += sizeof(int32_t);
1856 1857

  // prepare memory
1858
  SResultRowPosition* pos = &pInfo->resultRowInfo.cur;
dengyihao's avatar
dengyihao 已提交
1859 1860
  void*               pPage = getBufPage(pSup->pResultBuf, pos->pageId);
  SResultRow*         pRow = (SResultRow*)((char*)pPage + pos->offset);
1861 1862
  setBufPageDirty(pPage, true);
  releaseBufPage(pSup->pResultBuf, pPage);
L
Liu Jicong 已提交
1863

1864 1865 1866 1867
  int32_t iter = 0;
  void*   pIter = NULL;
  while ((pIter = tSimpleHashIterate(pSup->pResultRowHashTable, pIter, &iter))) {
    void*               key = tSimpleHashGetKey(pIter, &keyLen);
1868
    SResultRowPosition* p1 = (SResultRowPosition*)pIter;
1869

dengyihao's avatar
dengyihao 已提交
1870
    pPage = (SFilePage*)getBufPage(pSup->pResultBuf, p1->pageId);
1871
    pRow = (SResultRow*)((char*)pPage + p1->offset);
1872 1873
    setBufPageDirty(pPage, true);
    releaseBufPage(pSup->pResultBuf, pPage);
wmmhello's avatar
wmmhello 已提交
1874 1875 1876

    // recalculate the result size
    int32_t realTotalSize = offset + sizeof(int32_t) + keyLen + sizeof(int32_t) + pSup->resultRowSize;
L
Liu Jicong 已提交
1877
    if (realTotalSize > totalSize) {
wmmhello's avatar
wmmhello 已提交
1878
      char* tmp = (char*)taosMemoryRealloc(*result, realTotalSize);
L
Liu Jicong 已提交
1879
      if (tmp == NULL) {
wafwerar's avatar
wafwerar 已提交
1880
        taosMemoryFree(*result);
wmmhello's avatar
wmmhello 已提交
1881
        *result = NULL;
wmmhello's avatar
wmmhello 已提交
1882
        return TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
1883
      } else {
wmmhello's avatar
wmmhello 已提交
1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895
        *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);
1896
    memcpy(*result + offset, pRow, pSup->resultRowSize);
wmmhello's avatar
wmmhello 已提交
1897 1898 1899
    offset += pSup->resultRowSize;
  }

wmmhello's avatar
wmmhello 已提交
1900 1901 1902 1903
  *(int32_t*)(*result) = offset;
  *length = offset;

  return TDB_CODE_SUCCESS;
wmmhello's avatar
wmmhello 已提交
1904 1905
}

1906 1907 1908 1909 1910
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);
1911
      return PROJECT_RETRIEVE_CONTINUE;
1912 1913 1914
    } else if (pLimitInfo->currentGroupId != pBlock->info.groupId) {
      // now it is the data from a new group
      pLimitInfo->remainGroupOffset -= 1;
1915 1916

      // ignore data block in current group
1917 1918
      if (pLimitInfo->remainGroupOffset > 0) {
        blockDataCleanup(pBlock);
1919 1920 1921 1922 1923
        return PROJECT_RETRIEVE_CONTINUE;
      }
    }

    // set current group id of the project operator
1924
    pLimitInfo->currentGroupId = pBlock->info.groupId;
1925 1926
  }

1927
  // here check for a new group data, we need to handle the data of the previous group.
1928 1929 1930
  if (pLimitInfo->currentGroupId != 0 && pLimitInfo->currentGroupId != pBlock->info.groupId) {
    pLimitInfo->numOfOutputGroups += 1;
    if ((pLimitInfo->slimit.limit > 0) && (pLimitInfo->slimit.limit <= pLimitInfo->numOfOutputGroups)) {
1931
      pOperator->status = OP_EXEC_DONE;
1932
      blockDataCleanup(pBlock);
1933 1934 1935 1936 1937

      return PROJECT_RETRIEVE_DONE;
    }

    // reset the value for a new group data
1938 1939
    pLimitInfo->numOfOutputRows = 0;
    pLimitInfo->remainOffset = pLimitInfo->limit.offset;
1940 1941 1942 1943 1944

    // existing rows that belongs to previous group.
    if (pBlock->info.rows > 0) {
      return PROJECT_RETRIEVE_DONE;
    }
1945 1946 1947 1948 1949
  }

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

  // set current group id
1950
  pLimitInfo->currentGroupId = pBlock->info.groupId;
1951

1952 1953 1954
  if (pLimitInfo->remainOffset >= pBlock->info.rows) {
    pLimitInfo->remainOffset -= pBlock->info.rows;
    blockDataCleanup(pBlock);
1955
    return PROJECT_RETRIEVE_CONTINUE;
1956 1957 1958
  } else if (pLimitInfo->remainOffset < pBlock->info.rows && pLimitInfo->remainOffset > 0) {
    blockDataTrimFirstNRows(pBlock, pLimitInfo->remainOffset);
    pLimitInfo->remainOffset = 0;
1959 1960
  }

1961
  // check for the limitation in each group
1962 1963 1964 1965
  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) {
1966 1967 1968
      pOperator->status = OP_EXEC_DONE;
    }

1969
    return PROJECT_RETRIEVE_DONE;
1970
  }
1971

1972
  // todo optimize performance
1973 1974
  // 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.
1975 1976
  if ((!holdDataInBuf) || (pBlock->info.rows >= pOperator->resultInfo.threshold) || pLimitInfo->slimit.offset != -1 ||
      pLimitInfo->slimit.limit != -1) {
1977
    return PROJECT_RETRIEVE_DONE;
L
Liu Jicong 已提交
1978
  } else {  // not full enough, continue to accumulate the output data in the buffer.
1979 1980 1981 1982
    return PROJECT_RETRIEVE_CONTINUE;
  }
}

1983
static void doApplyScalarCalculation(SOperatorInfo* pOperator, SSDataBlock* pBlock, int32_t order, int32_t scanFlag);
L
Liu Jicong 已提交
1984 1985
static void doHandleRemainBlockForNewGroupImpl(SOperatorInfo* pOperator, SFillOperatorInfo* pInfo,
                                               SResultInfo* pResultInfo, SExecTaskInfo* pTaskInfo) {
1986
  pInfo->totalInputRows = pInfo->existNewGroupBlock->info.rows;
1987 1988 1989 1990 1991
  SSDataBlock* pResBlock = pInfo->pFinalRes;

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

L
Liu Jicong 已提交
1993 1994
  int64_t ekey =
      Q_STATUS_EQUAL(pTaskInfo->status, TASK_COMPLETED) ? pInfo->win.ekey : pInfo->existNewGroupBlock->info.window.ekey;
1995 1996
  taosResetFillInfo(pInfo->pFillInfo, getFillInfoStart(pInfo->pFillInfo));

1997
  blockDataCleanup(pInfo->pRes);
1998 1999 2000 2001
  doApplyScalarCalculation(pOperator, pInfo->existNewGroupBlock, order, scanFlag);

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

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

2006
  pInfo->curGroupId = pInfo->existNewGroupBlock->info.groupId;
2007 2008 2009
  pInfo->existNewGroupBlock = NULL;
}

L
Liu Jicong 已提交
2010 2011
static void doHandleRemainBlockFromNewGroup(SOperatorInfo* pOperator, SFillOperatorInfo* pInfo,
                                            SResultInfo* pResultInfo, SExecTaskInfo* pTaskInfo) {
2012
  if (taosFillHasMoreResults(pInfo->pFillInfo)) {
H
Haojun Liao 已提交
2013 2014
    int32_t numOfResultRows = pResultInfo->capacity - pInfo->pFinalRes->info.rows;
    taosFillResultDataBlock(pInfo->pFillInfo, pInfo->pFinalRes, numOfResultRows);
2015 2016
    pInfo->pRes->info.groupId = pInfo->curGroupId;
    return;
2017 2018 2019 2020
  }

  // handle the cached new group data block
  if (pInfo->existNewGroupBlock) {
2021 2022 2023 2024 2025 2026
    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 已提交
2027
  SExprSupp*         pSup = &pOperator->exprSupp;
2028
  setInputDataBlock(pSup, pBlock, order, scanFlag, false);
2029 2030
  projectApplyFunctions(pSup->pExprInfo, pInfo->pRes, pBlock, pSup->pCtx, pSup->numOfExprs, NULL);

2031 2032 2033 2034
  // 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);
2035

2036 2037
  projectApplyFunctions(pNoFillSupp->pExprInfo, pInfo->pRes, pBlock, pNoFillSupp->pCtx, pNoFillSupp->numOfExprs, NULL);
  pInfo->pRes->info.groupId = pBlock->info.groupId;
2038 2039
}

S
slzhou 已提交
2040
static SSDataBlock* doFillImpl(SOperatorInfo* pOperator) {
L
Liu Jicong 已提交
2041 2042
  SFillOperatorInfo* pInfo = pOperator->info;
  SExecTaskInfo*     pTaskInfo = pOperator->pTaskInfo;
2043

H
Haojun Liao 已提交
2044
  SResultInfo* pResultInfo = &pOperator->resultInfo;
H
Haojun Liao 已提交
2045
  SSDataBlock* pResBlock = pInfo->pFinalRes;
2046 2047

  blockDataCleanup(pResBlock);
2048

H
Haojun Liao 已提交
2049 2050
  int32_t order = TSDB_ORDER_ASC;
  int32_t scanFlag = MAIN_SCAN;
2051
  getTableScanInfo(pOperator, &order, &scanFlag);
2052

2053
  doHandleRemainBlockFromNewGroup(pOperator, pInfo, pResultInfo, pTaskInfo);
2054
  if (pResBlock->info.rows > 0) {
2055
    pResBlock->info.groupId = pInfo->curGroupId;
2056
    return pResBlock;
H
Haojun Liao 已提交
2057
  }
2058

H
Haojun Liao 已提交
2059
  SOperatorInfo* pDownstream = pOperator->pDownstream[0];
L
Liu Jicong 已提交
2060
  while (1) {
2061
    SSDataBlock* pBlock = pDownstream->fpSet.getNextFn(pDownstream);
2062 2063
    if (pBlock == NULL) {
      if (pInfo->totalInputRows == 0) {
H
Haojun Liao 已提交
2064
        setOperatorCompleted(pOperator);
2065 2066
        return NULL;
      }
2067

2068
      taosFillSetStartInfo(pInfo->pFillInfo, 0, pInfo->win.ekey);
2069
    } else {
2070
      blockDataUpdateTsWindow(pBlock, pInfo->primarySrcSlotId);
2071 2072

      blockDataCleanup(pInfo->pRes);
2073 2074
      blockDataEnsureCapacity(pInfo->pRes, pBlock->info.rows);
      blockDataEnsureCapacity(pInfo->pFinalRes, pBlock->info.rows);
2075
      doApplyScalarCalculation(pOperator, pBlock, order, scanFlag);
2076

H
Haojun Liao 已提交
2077 2078 2079
      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;
2080

H
Haojun Liao 已提交
2081 2082 2083 2084 2085
        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 已提交
2086
        taosFillSetInputDataBlock(pInfo->pFillInfo, pInfo->pRes);
L
Liu Jicong 已提交
2087
      } else if (pInfo->curGroupId != pBlock->info.groupId) {  // the new group data block
2088 2089 2090 2091 2092
        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);
2093 2094 2095
      }
    }

2096 2097
    int32_t numOfResultRows = pOperator->resultInfo.capacity - pResBlock->info.rows;
    taosFillResultDataBlock(pInfo->pFillInfo, pResBlock, numOfResultRows);
2098 2099

    // current group has no more result to return
2100
    if (pResBlock->info.rows > 0) {
2101 2102
      // 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
2103
      if (pResBlock->info.rows > pResultInfo->threshold || pBlock == NULL || pInfo->existNewGroupBlock != NULL) {
2104
        pResBlock->info.groupId = pInfo->curGroupId;
2105
        return pResBlock;
2106 2107
      }

2108
      doHandleRemainBlockFromNewGroup(pOperator, pInfo, pResultInfo, pTaskInfo);
2109
      if (pResBlock->info.rows >= pOperator->resultInfo.threshold || pBlock == NULL) {
2110
        pResBlock->info.groupId = pInfo->curGroupId;
2111
        return pResBlock;
2112 2113 2114
      }
    } else if (pInfo->existNewGroupBlock) {  // try next group
      assert(pBlock != NULL);
2115 2116 2117 2118

      blockDataCleanup(pResBlock);

      doHandleRemainBlockForNewGroupImpl(pOperator, pInfo, pResultInfo, pTaskInfo);
2119
      if (pResBlock->info.rows > pResultInfo->threshold) {
2120
        pResBlock->info.groupId = pInfo->curGroupId;
2121
        return pResBlock;
2122 2123 2124 2125 2126 2127 2128
      }
    } else {
      return NULL;
    }
  }
}

S
slzhou 已提交
2129 2130 2131 2132 2133 2134 2135 2136
static SSDataBlock* doFill(SOperatorInfo* pOperator) {
  SFillOperatorInfo* pInfo = pOperator->info;
  SExecTaskInfo*     pTaskInfo = pOperator->pTaskInfo;

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

S
slzhou 已提交
2137
  SSDataBlock* fillResult = NULL;
S
slzhou 已提交
2138
  while (true) {
S
slzhou 已提交
2139
    fillResult = doFillImpl(pOperator);
S
slzhou 已提交
2140
    if (fillResult == NULL) {
H
Haojun Liao 已提交
2141
      setOperatorCompleted(pOperator);
S
slzhou 已提交
2142 2143 2144
      break;
    }

H
Haojun Liao 已提交
2145
    doFilter(fillResult, pOperator->exprSupp.pFilterInfo, &pInfo->matchInfo );
S
slzhou 已提交
2146 2147 2148 2149 2150
    if (fillResult->info.rows > 0) {
      break;
    }
  }

S
slzhou 已提交
2151
  if (fillResult != NULL) {
2152
    pOperator->resultInfo.totalRows += fillResult->info.rows;
S
slzhou 已提交
2153
  }
S
slzhou 已提交
2154

S
slzhou 已提交
2155
  return fillResult;
S
slzhou 已提交
2156 2157
}

2158
void destroyExprInfo(SExprInfo* pExpr, int32_t numOfExprs) {
C
Cary Xu 已提交
2159 2160 2161 2162 2163
  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 已提交
2164
      }
2165
    }
C
Cary Xu 已提交
2166 2167 2168

    taosMemoryFree(pExprInfo->base.pParam);
    taosMemoryFree(pExprInfo->pExpr);
H
Haojun Liao 已提交
2169 2170 2171
  }
}

5
54liuyao 已提交
2172
void destroyOperatorInfo(SOperatorInfo* pOperator) {
2173 2174 2175 2176
  if (pOperator == NULL) {
    return;
  }

2177
  if (pOperator->fpSet.closeFn != NULL) {
2178
    pOperator->fpSet.closeFn(pOperator->info);
2179 2180
  }

H
Haojun Liao 已提交
2181
  if (pOperator->pDownstream != NULL) {
L
Liu Jicong 已提交
2182
    for (int32_t i = 0; i < pOperator->numOfDownstream; ++i) {
H
Haojun Liao 已提交
2183
      destroyOperatorInfo(pOperator->pDownstream[i]);
2184 2185
    }

wafwerar's avatar
wafwerar 已提交
2186
    taosMemoryFreeClear(pOperator->pDownstream);
H
Haojun Liao 已提交
2187
    pOperator->numOfDownstream = 0;
2188 2189
  }

2190
  cleanupExprSupp(&pOperator->exprSupp);
wafwerar's avatar
wafwerar 已提交
2191
  taosMemoryFreeClear(pOperator);
2192 2193
}

2194 2195 2196 2197 2198 2199
int32_t getBufferPgSize(int32_t rowSize, uint32_t* defaultPgsz, uint32_t* defaultBufsz) {
  *defaultPgsz = 4096;
  while (*defaultPgsz < rowSize * 4) {
    *defaultPgsz <<= 1u;
  }

2200
  // The default buffer for each operator in query is 10MB.
2201
  // at least four pages need to be in buffer
2202 2203
  // TODO: make this variable to be configurable.
  *defaultBufsz = 4096 * 2560;
2204 2205 2206 2207 2208 2209 2210
  if ((*defaultBufsz) <= (*defaultPgsz)) {
    (*defaultBufsz) = (*defaultPgsz) * 4;
  }

  return 0;
}

dengyihao's avatar
dengyihao 已提交
2211 2212
int32_t doInitAggInfoSup(SAggSupporter* pAggSup, SqlFunctionCtx* pCtx, int32_t numOfOutput, size_t keyBufSize,
                         const char* pKey) {
L
Liu Jicong 已提交
2213
  int32_t    code = 0;
2214 2215
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);

2216
  pAggSup->currentPageId = -1;
dengyihao's avatar
dengyihao 已提交
2217 2218
  pAggSup->resultRowSize = getResultRowSize(pCtx, numOfOutput);
  pAggSup->keyBuf = taosMemoryCalloc(1, keyBufSize + POINTER_BYTES + sizeof(int64_t));
2219
  pAggSup->pResultRowHashTable = tSimpleHashInit(10, hashFn);
2220

H
Haojun Liao 已提交
2221
  if (pAggSup->keyBuf == NULL || pAggSup->pResultRowHashTable == NULL) {
2222 2223 2224
    return TSDB_CODE_OUT_OF_MEMORY;
  }

dengyihao's avatar
dengyihao 已提交
2225
  uint32_t defaultPgsz = 0;
2226 2227
  uint32_t defaultBufsz = 0;
  getBufferPgSize(pAggSup->resultRowSize, &defaultPgsz, &defaultBufsz);
H
Haojun Liao 已提交
2228

wafwerar's avatar
wafwerar 已提交
2229
  if (!osTempSpaceAvailable()) {
H
Haojun Liao 已提交
2230 2231 2232
    code = TSDB_CODE_NO_AVAIL_DISK;
    qError("Init stream agg supporter failed since %s, %s", terrstr(code), pKey);
    return code;
wafwerar's avatar
wafwerar 已提交
2233
  }
2234

H
Haojun Liao 已提交
2235
  code = createDiskbasedBuf(&pAggSup->pResultBuf, defaultPgsz, defaultBufsz, pKey, tsTempDir);
H
Haojun Liao 已提交
2236
  if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
2237
    qError("Create agg result buf failed since %s, %s", tstrerror(code), pKey);
H
Haojun Liao 已提交
2238 2239 2240
    return code;
  }

H
Haojun Liao 已提交
2241
  return code;
2242 2243
}

2244
void cleanupAggSup(SAggSupporter* pAggSup) {
wafwerar's avatar
wafwerar 已提交
2245
  taosMemoryFreeClear(pAggSup->keyBuf);
2246
  tSimpleHashCleanup(pAggSup->pResultRowHashTable);
H
Haojun Liao 已提交
2247
  destroyDiskbasedBuf(pAggSup->pResultBuf);
2248 2249
}

L
Liu Jicong 已提交
2250 2251
int32_t initAggInfo(SExprSupp* pSup, SAggSupporter* pAggSup, SExprInfo* pExprInfo, int32_t numOfCols, size_t keyBufSize,
                    const char* pkey) {
2252 2253 2254 2255 2256
  int32_t code = initExprSupp(pSup, pExprInfo, numOfCols);
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }

2257 2258 2259 2260 2261
  code = doInitAggInfoSup(pAggSup, pSup->pCtx, numOfCols, keyBufSize, pkey);
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }

L
Liu Jicong 已提交
2262
  for (int32_t i = 0; i < numOfCols; ++i) {
2263
    pSup->pCtx[i].saveHandle.pBuf = pAggSup->pResultBuf;
2264 2265
  }

2266
  return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
2267 2268
}

L
Liu Jicong 已提交
2269
void initResultSizeInfo(SResultInfo* pResultInfo, int32_t numOfRows) {
wmmhello's avatar
wmmhello 已提交
2270
  ASSERT(numOfRows != 0);
2271 2272
  pResultInfo->capacity = numOfRows;
  pResultInfo->threshold = numOfRows * 0.75;
2273

2274 2275
  if (pResultInfo->threshold == 0) {
    pResultInfo->threshold = numOfRows;
2276 2277 2278
  }
}

2279 2280 2281 2282 2283
void initBasicInfo(SOptrBasicInfo* pInfo, SSDataBlock* pBlock) {
  pInfo->pRes = pBlock;
  initResultRowInfo(&pInfo->resultRowInfo);
}

5
54liuyao 已提交
2284
void* destroySqlFunctionCtx(SqlFunctionCtx* pCtx, int32_t numOfOutput) {
2285 2286 2287 2288 2289 2290 2291 2292 2293 2294
  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);
2295
    taosMemoryFreeClear(pCtx[i].subsidiaries.buf);
2296 2297 2298 2299 2300 2301 2302 2303
    taosMemoryFree(pCtx[i].input.pData);
    taosMemoryFree(pCtx[i].input.pColumnDataAgg);
  }

  taosMemoryFreeClear(pCtx);
  return NULL;
}

2304
int32_t initExprSupp(SExprSupp* pSup, SExprInfo* pExprInfo, int32_t numOfExpr) {
2305 2306 2307 2308
  pSup->pExprInfo = pExprInfo;
  pSup->numOfExprs = numOfExpr;
  if (pSup->pExprInfo != NULL) {
    pSup->pCtx = createSqlFunctionCtx(pExprInfo, numOfExpr, &pSup->rowEntryInfoOffset);
2309 2310 2311
    if (pSup->pCtx == NULL) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
2312
  }
2313 2314

  return TSDB_CODE_SUCCESS;
2315 2316
}

2317 2318 2319 2320
void cleanupExprSupp(SExprSupp* pSupp) {
  destroySqlFunctionCtx(pSupp->pCtx, pSupp->numOfExprs);
  if (pSupp->pExprInfo != NULL) {
    destroyExprInfo(pSupp->pExprInfo, pSupp->numOfExprs);
C
Cary Xu 已提交
2321
    taosMemoryFreeClear(pSupp->pExprInfo);
2322
  }
H
Haojun Liao 已提交
2323 2324 2325 2326 2327 2328

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

2329 2330 2331
  taosMemoryFree(pSupp->rowEntryInfoOffset);
}

2332 2333
SOperatorInfo* createAggregateOperatorInfo(SOperatorInfo* downstream, SAggPhysiNode* pAggNode,
                                           SExecTaskInfo* pTaskInfo) {
wafwerar's avatar
wafwerar 已提交
2334
  SAggOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SAggOperatorInfo));
L
Liu Jicong 已提交
2335
  SOperatorInfo*    pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
H
Haojun Liao 已提交
2336 2337 2338
  if (pInfo == NULL || pOperator == NULL) {
    goto _error;
  }
H
Haojun Liao 已提交
2339

H
Haojun Liao 已提交
2340 2341 2342 2343
  SSDataBlock* pResBlock = createResDataBlock(pAggNode->node.pOutputDataBlockDesc);
  initBasicInfo(&pInfo->binfo, pResBlock);

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

2346 2347 2348
  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 已提交
2349
  if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
2350 2351
    goto _error;
  }
H
Haojun Liao 已提交
2352

H
Haojun Liao 已提交
2353 2354 2355 2356 2357 2358
  int32_t    numOfScalarExpr = 0;
  SExprInfo* pScalarExprInfo = NULL;
  if (pAggNode->pExprs != NULL) {
    pScalarExprInfo = createExprInfo(pAggNode->pExprs, NULL, &numOfScalarExpr);
  }

2359 2360 2361 2362
  code = initExprSupp(&pInfo->scalarExprSup, pScalarExprInfo, numOfScalarExpr);
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
2363

H
Haojun Liao 已提交
2364 2365 2366 2367 2368
  code = filterInitFromNode((SNode*)pAggNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0);
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }

H
Haojun Liao 已提交
2369
  pInfo->binfo.mergeResultBlock = pAggNode->mergeDataBlock;
2370
  pInfo->groupId = UINT64_MAX;
H
Haojun Liao 已提交
2371

H
Haojun Liao 已提交
2372
  setOperatorInfo(pOperator, "TableAggregate", QUERY_NODE_PHYSICAL_PLAN_HASH_AGG, true, OP_NOT_OPENED, pInfo, pTaskInfo);
5
54liuyao 已提交
2373
  pOperator->fpSet =
H
Haojun Liao 已提交
2374
      createOperatorFpSet(doOpenAggregateOptr, getAggregateResult, NULL, destroyAggOperatorInfo, NULL);
H
Haojun Liao 已提交
2375

2376 2377 2378 2379 2380 2381
  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 已提交
2382 2383 2384 2385
  code = appendDownstream(pOperator, &downstream, 1);
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
2386 2387

  return pOperator;
H
Haojun Liao 已提交
2388

2389
_error:
H
Haojun Liao 已提交
2390 2391 2392 2393
  if (pInfo != NULL) {
    destroyAggOperatorInfo(pInfo);
  }

2394 2395 2396
  if (pOperator != NULL) {
    cleanupExprSupp(&pOperator->exprSupp);
  }
H
Haojun Liao 已提交
2397

2398
  taosMemoryFreeClear(pOperator);
H
Haojun Liao 已提交
2399
  pTaskInfo->code = code;
H
Haojun Liao 已提交
2400
  return NULL;
2401 2402
}

2403
void cleanupBasicInfo(SOptrBasicInfo* pInfo) {
2404
  assert(pInfo != NULL);
H
Haojun Liao 已提交
2405
  pInfo->pRes = blockDataDestroy(pInfo->pRes);
2406 2407
}

H
Haojun Liao 已提交
2408 2409 2410 2411 2412 2413 2414
static void freeItem(void* pItem) {
  void** p = pItem;
  if (*p != NULL) {
    taosMemoryFreeClear(*p);
  }
}

2415
void destroyAggOperatorInfo(void* param) {
L
Liu Jicong 已提交
2416
  SAggOperatorInfo* pInfo = (SAggOperatorInfo*)param;
L
Liu Jicong 已提交
2417 2418
  cleanupBasicInfo(&pInfo->binfo);

H
Haojun Liao 已提交
2419
  cleanupAggSup(&pInfo->aggSup);
S
shenglian zhou 已提交
2420
  cleanupExprSupp(&pInfo->scalarExprSup);
H
Haojun Liao 已提交
2421
  cleanupGroupResInfo(&pInfo->groupResInfo);
D
dapan1121 已提交
2422
  taosMemoryFreeClear(param);
2423
}
2424

2425
void destroyFillOperatorInfo(void* param) {
L
Liu Jicong 已提交
2426
  SFillOperatorInfo* pInfo = (SFillOperatorInfo*)param;
2427
  pInfo->pFillInfo = taosDestroyFillInfo(pInfo->pFillInfo);
H
Haojun Liao 已提交
2428
  pInfo->pRes = blockDataDestroy(pInfo->pRes);
H
Haojun Liao 已提交
2429 2430
  pInfo->pFinalRes = blockDataDestroy(pInfo->pFinalRes);

2431
  cleanupExprSupp(&pInfo->noFillExprSupp);
H
Haojun Liao 已提交
2432

wafwerar's avatar
wafwerar 已提交
2433
  taosMemoryFreeClear(pInfo->p);
H
Haojun Liao 已提交
2434
  taosArrayDestroy(pInfo->matchInfo.pList);
D
dapan1121 已提交
2435
  taosMemoryFreeClear(param);
2436 2437
}

H
Haojun Liao 已提交
2438 2439 2440 2441
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 已提交
2442

H
Haojun Liao 已提交
2443 2444 2445
  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 已提交
2446

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

H
Haojun Liao 已提交
2450 2451 2452 2453 2454 2455 2456
  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 已提交
2457
  pInfo->p = taosMemoryCalloc(numOfCols, POINTER_BYTES);
2458

H
Haojun Liao 已提交
2459
  if (pInfo->pFillInfo == NULL || pInfo->p == NULL) {
H
Haojun Liao 已提交
2460 2461
    taosMemoryFree(pInfo->pFillInfo);
    taosMemoryFree(pInfo->p);
H
Haojun Liao 已提交
2462 2463 2464 2465 2466 2467
    return TSDB_CODE_OUT_OF_MEMORY;
  } else {
    return TSDB_CODE_SUCCESS;
  }
}

2468
static bool isWstartColumnExist(SFillOperatorInfo* pInfo) {
2469
  if (pInfo->noFillExprSupp.numOfExprs == 0) {
2470 2471
    return false;
  }
2472 2473 2474

  for (int32_t i = 0; i < pInfo->noFillExprSupp.numOfExprs; ++i) {
    SExprInfo* exprInfo = pInfo->noFillExprSupp.pExprInfo + i;
2475
    if (exprInfo->pExpr->nodeType == QUERY_NODE_COLUMN && exprInfo->base.numOfParams == 1 &&
2476 2477 2478 2479 2480 2481 2482
        exprInfo->base.pParam[0].pCol->colType == COLUMN_TYPE_WINDOW_START) {
      return true;
    }
  }
  return false;
}

2483 2484
static int32_t createPrimaryTsExprIfNeeded(SFillOperatorInfo* pInfo, SFillPhysiNode* pPhyFillNode, SExprSupp* pExprSupp,
                                           const char* idStr) {
2485
  bool wstartExist = isWstartColumnExist(pInfo);
2486

2487 2488
  if (wstartExist == false) {
    if (pPhyFillNode->pWStartTs->type != QUERY_NODE_TARGET) {
2489
      qError("pWStartTs of fill physical node is not a target node, %s", idStr);
2490 2491 2492
      return TSDB_CODE_QRY_SYS_ERROR;
    }

2493 2494
    SExprInfo* pExpr = taosMemoryRealloc(pExprSupp->pExprInfo, (pExprSupp->numOfExprs + 1) * sizeof(SExprInfo));
    if (pExpr == NULL) {
2495 2496 2497
      return TSDB_CODE_OUT_OF_MEMORY;
    }

2498 2499 2500
    createExprFromTargetNode(&pExpr[pExprSupp->numOfExprs], (STargetNode*)pPhyFillNode->pWStartTs);
    pExprSupp->numOfExprs += 1;
    pExprSupp->pExprInfo = pExpr;
2501
  }
2502

2503 2504 2505
  return TSDB_CODE_SUCCESS;
}

L
Liu Jicong 已提交
2506 2507
SOperatorInfo* createFillOperatorInfo(SOperatorInfo* downstream, SFillPhysiNode* pPhyFillNode,
                                      SExecTaskInfo* pTaskInfo) {
2508 2509 2510 2511 2512 2513
  SFillOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SFillOperatorInfo));
  SOperatorInfo*     pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
  if (pInfo == NULL || pOperator == NULL) {
    goto _error;
  }

H
Haojun Liao 已提交
2514 2515 2516 2517
  pInfo->pRes = createResDataBlock(pPhyFillNode->node.pOutputDataBlockDesc);
  SExprInfo* pExprInfo = createExprInfo(pPhyFillNode->pFillExprs, NULL, &pInfo->numOfExpr);
  pOperator->exprSupp.pExprInfo = pExprInfo;

2518 2519 2520 2521 2522 2523 2524 2525
  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);
2526 2527 2528
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
H
Haojun Liao 已提交
2529

L
Liu Jicong 已提交
2530
  SInterval* pInterval =
2531
      QUERY_NODE_PHYSICAL_PLAN_MERGE_ALIGNED_INTERVAL == downstream->operatorType
2532 2533
          ? &((SMergeAlignedIntervalAggOperatorInfo*)downstream->info)->intervalAggOperatorInfo->interval
          : &((SIntervalAggOperatorInfo*)downstream->info)->interval;
2534

2535
  int32_t order = (pPhyFillNode->inputTsOrder == ORDER_ASC) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC;
2536
  int32_t type = convertFillType(pPhyFillNode->mode);
2537

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

2540
  initResultSizeInfo(&pOperator->resultInfo, 4096);
H
Haojun Liao 已提交
2541 2542 2543 2544 2545
  blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
  code = initExprSupp(&pOperator->exprSupp, pExprInfo, pInfo->numOfExpr);
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
H
Haojun Liao 已提交
2546

H
Haojun Liao 已提交
2547 2548
  pInfo->primaryTsCol = ((STargetNode*)pPhyFillNode->pWStartTs)->slotId;
  pInfo->primarySrcSlotId = ((SColumnNode*)((STargetNode*)pPhyFillNode->pWStartTs)->pExpr)->slotId;
2549

2550
  int32_t numOfOutputCols = 0;
H
Haojun Liao 已提交
2551 2552
  code = extractColMatchInfo(pPhyFillNode->pFillExprs, pPhyFillNode->node.pOutputDataBlockDesc, &numOfOutputCols,
                             COL_MATCH_FROM_SLOT_ID, &pInfo->matchInfo);
2553

2554
  code = initFillInfo(pInfo, pExprInfo, pInfo->numOfExpr, pNoFillSupp->pExprInfo, pNoFillSupp->numOfExprs,
2555 2556
                      (SNodeListNode*)pPhyFillNode->pValues, pPhyFillNode->timeRange, pResultInfo->capacity,
                      pTaskInfo->id.str, pInterval, type, order);
2557 2558 2559
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
2560

H
Haojun Liao 已提交
2561
  pInfo->pFinalRes = createOneDataBlock(pInfo->pRes, false);
H
Haojun Liao 已提交
2562 2563
  blockDataEnsureCapacity(pInfo->pFinalRes, pOperator->resultInfo.capacity);

H
Haojun Liao 已提交
2564 2565 2566 2567 2568
  code = filterInitFromNode((SNode*)pPhyFillNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0);
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }

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

2573
  code = appendDownstream(pOperator, &downstream, 1);
2574
  return pOperator;
H
Haojun Liao 已提交
2575

2576
_error:
H
Haojun Liao 已提交
2577 2578 2579 2580
  if (pInfo != NULL) {
    destroyFillOperatorInfo(pInfo);
  }

2581
  pTaskInfo->code = code;
wafwerar's avatar
wafwerar 已提交
2582
  taosMemoryFreeClear(pOperator);
H
Haojun Liao 已提交
2583
  return NULL;
2584 2585
}

D
dapan1121 已提交
2586
static SExecTaskInfo* createExecTaskInfo(uint64_t queryId, uint64_t taskId, EOPTR_EXEC_MODEL model, char* dbFName) {
wafwerar's avatar
wafwerar 已提交
2587
  SExecTaskInfo* pTaskInfo = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
H
Haojun Liao 已提交
2588 2589 2590 2591 2592
  if (pTaskInfo == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

2593
  setTaskStatus(pTaskInfo, TASK_NOT_COMPLETED);
H
Haojun Liao 已提交
2594

2595
  pTaskInfo->schemaInfo.dbname = strdup(dbFName);
2596
  pTaskInfo->cost.created = taosGetTimestampMs();
H
Haojun Liao 已提交
2597
  pTaskInfo->id.queryId = queryId;
dengyihao's avatar
dengyihao 已提交
2598
  pTaskInfo->execModel = model;
H
Haojun Liao 已提交
2599
  pTaskInfo->pTableInfoList = tableListCreate();
H
Haojun Liao 已提交
2600

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

2605 2606
  return pTaskInfo;
}
H
Haojun Liao 已提交
2607

H
Haojun Liao 已提交
2608 2609
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode);

2610
int32_t extractTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, SExecTaskInfo* pTaskInfo) {
2611 2612
  SMetaReader mr = {0};
  metaReaderInit(&mr, pHandle->meta, 0);
2613
  int32_t code = metaGetTableEntryByUid(&mr, pScanNode->uid);
2614
  if (code != TSDB_CODE_SUCCESS) {
L
Liu Jicong 已提交
2615 2616
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
           GET_TASKID(pTaskInfo));
H
Haojun Liao 已提交
2617

D
dapan1121 已提交
2618
    metaReaderClear(&mr);
2619
    return terrno;
D
dapan1121 已提交
2620
  }
2621

2622 2623
  SSchemaInfo* pSchemaInfo = &pTaskInfo->schemaInfo;
  pSchemaInfo->tablename = strdup(mr.me.name);
2624 2625

  if (mr.me.type == TSDB_SUPER_TABLE) {
2626 2627
    pSchemaInfo->sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
    pSchemaInfo->tversion = mr.me.stbEntry.schemaTag.version;
2628
  } else if (mr.me.type == TSDB_CHILD_TABLE) {
2629 2630
    tDecoderClear(&mr.coder);

2631 2632
    tb_uid_t suid = mr.me.ctbEntry.suid;
    metaGetTableEntryByUid(&mr, suid);
2633 2634
    pSchemaInfo->sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
    pSchemaInfo->tversion = mr.me.stbEntry.schemaTag.version;
2635
  } else {
2636
    pSchemaInfo->sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
2637
  }
2638 2639

  metaReaderClear(&mr);
2640

H
Haojun Liao 已提交
2641 2642 2643 2644 2645
  pSchemaInfo->qsw = extractQueriedColumnSchema(pScanNode);
  return TSDB_CODE_SUCCESS;
}

SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
2646 2647 2648
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);

2649
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
2650
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
2651

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

H
Haojun Liao 已提交
2656 2657 2658
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
    pSchema->colId = pColNode->colId;
    pSchema->type = pColNode->node.resType.type;
H
Haojun Liao 已提交
2659 2660
    pSchema->bytes = pColNode->node.resType.bytes;
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
2661 2662
  }

2663
  // this the tags and pseudo function columns, we only keep the tag columns
2664
  for (int32_t i = 0; i < numOfTags; ++i) {
2665 2666 2667 2668 2669 2670 2671 2672 2673
    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 已提交
2674
      pSchema->bytes = pColNode->node.resType.bytes;
H
Haojun Liao 已提交
2675
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
2676 2677 2678
    }
  }

H
Haojun Liao 已提交
2679
  return pqSw;
2680 2681
}

2682 2683
static void cleanupTableSchemaInfo(SSchemaInfo* pSchemaInfo) {
  taosMemoryFreeClear(pSchemaInfo->dbname);
2684
  taosMemoryFreeClear(pSchemaInfo->tablename);
2685 2686
  tDeleteSSchemaWrapper(pSchemaInfo->sw);
  tDeleteSSchemaWrapper(pSchemaInfo->qsw);
2687 2688
}

2689
static void cleanupStreamInfo(SStreamTaskInfo* pStreamInfo) { tDeleteSSchemaWrapper(pStreamInfo->schema); }
2690

2691
bool groupbyTbname(SNodeList* pGroupList) {
2692
  bool bytbname = false;
2693
  if (LIST_LENGTH(pGroupList) == 1) {
2694 2695 2696 2697 2698 2699 2700 2701 2702 2703
    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;
}

2704 2705
SOperatorInfo* createOperatorTree(SPhysiNode* pPhyNode, SExecTaskInfo* pTaskInfo, SReadHandle* pHandle, SNode* pTagCond,
                                  SNode* pTagIndexCond, const char* pUser) {
2706
  int32_t         type = nodeType(pPhyNode);
2707
  STableListInfo* pTableListInfo = pTaskInfo->pTableInfoList;
2708
  const char*     idstr = GET_TASKID(pTaskInfo);
2709

X
Xiaoyu Wang 已提交
2710
  if (pPhyNode->pChildren == NULL || LIST_LENGTH(pPhyNode->pChildren) == 0) {
2711
    SOperatorInfo* pOperator = NULL;
H
Haojun Liao 已提交
2712
    if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == type) {
dengyihao's avatar
dengyihao 已提交
2713
      STableScanPhysiNode* pTableScanNode = (STableScanPhysiNode*)pPhyNode;
H
Haojun Liao 已提交
2714

2715 2716 2717 2718 2719 2720
      // 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 已提交
2721 2722
      int32_t code =
          createScanTableListInfo(&pTableScanNode->scan, pTableScanNode->pGroupTags, pTableScanNode->groupSort, pHandle,
H
Haojun Liao 已提交
2723
                                  pTableListInfo, pTagCond, pTagIndexCond, pTaskInfo);
2724
      if (code) {
wmmhello's avatar
wmmhello 已提交
2725
        pTaskInfo->code = code;
2726
        qError("failed to createScanTableListInfo, code:%s, %s", tstrerror(code), idstr);
D
dapan1121 已提交
2727 2728
        return NULL;
      }
wmmhello's avatar
wmmhello 已提交
2729

2730
      code = extractTableSchemaInfo(pHandle, &pTableScanNode->scan, pTaskInfo);
S
slzhou 已提交
2731
      if (code) {
2732
        pTaskInfo->code = terrno;
wmmhello's avatar
wmmhello 已提交
2733 2734 2735
        return NULL;
      }

2736
      pOperator = createTableScanOperatorInfo(pTableScanNode, pHandle, pTaskInfo);
D
dapan1121 已提交
2737 2738 2739 2740 2741
      if (NULL == pOperator) {
        pTaskInfo->code = terrno;
        return NULL;
      }

2742 2743
      STableScanInfo* pScanInfo = pOperator->info;
      pTaskInfo->cost.pRecoder = &pScanInfo->readRecorder;
S
slzhou 已提交
2744 2745
    } else if (QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == type) {
      STableMergeScanPhysiNode* pTableScanNode = (STableMergeScanPhysiNode*)pPhyNode;
H
Haojun Liao 已提交
2746 2747 2748

      int32_t code = createScanTableListInfo(&pTableScanNode->scan, pTableScanNode->pGroupTags, true, pHandle,
                                             pTableListInfo, pTagCond, pTagIndexCond, pTaskInfo);
L
Liu Jicong 已提交
2749
      if (code) {
wmmhello's avatar
wmmhello 已提交
2750
        pTaskInfo->code = code;
H
Haojun Liao 已提交
2751
        qError("failed to createScanTableListInfo, code: %s", tstrerror(code));
wmmhello's avatar
wmmhello 已提交
2752 2753
        return NULL;
      }
2754

2755
      code = extractTableSchemaInfo(pHandle, &pTableScanNode->scan, pTaskInfo);
wmmhello's avatar
wmmhello 已提交
2756 2757 2758 2759
      if (code) {
        pTaskInfo->code = terrno;
        return NULL;
      }
wmmhello's avatar
wmmhello 已提交
2760

2761
      pOperator = createTableMergeScanOperatorInfo(pTableScanNode, pTableListInfo, pHandle, pTaskInfo);
D
dapan1121 已提交
2762 2763 2764 2765
      if (NULL == pOperator) {
        pTaskInfo->code = terrno;
        return NULL;
      }
wmmhello's avatar
wmmhello 已提交
2766

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

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

L
Liu Jicong 已提交
2788
        for (int32_t i = 0; i < sz; i++) {
H
Haojun Liao 已提交
2789
          STableKeyInfo* pKeyInfo = tableListGetInfo(pTableListInfo, i);
2790
          qDebug("add table uid:%" PRIu64 ", gid:%" PRIu64, pKeyInfo->uid, pKeyInfo->groupId);
L
Liu Jicong 已提交
2791 2792
        }
#endif
2793
      }
2794

H
Haojun Liao 已提交
2795
      pTaskInfo->schemaInfo.qsw = extractQueriedColumnSchema(&pTableScanNode->scan);
2796
      pOperator = createStreamScanOperatorInfo(pHandle, pTableScanNode, pTagCond, pTaskInfo);
H
Haojun Liao 已提交
2797
    } else if (QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN == type) {
L
Liu Jicong 已提交
2798
      SSystemTableScanPhysiNode* pSysScanPhyNode = (SSystemTableScanPhysiNode*)pPhyNode;
2799
      pOperator = createSysTableScanOperatorInfo(pHandle, pSysScanPhyNode, pUser, pTaskInfo);
2800
    } else if (QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN == type) {
X
Xiaoyu Wang 已提交
2801
      STagScanPhysiNode* pScanPhyNode = (STagScanPhysiNode*)pPhyNode;
2802 2803

      int32_t code = createScanTableListInfo(pScanPhyNode, NULL, false, pHandle, pTableListInfo, pTagCond,
H
Haojun Liao 已提交
2804
                                             pTagIndexCond, pTaskInfo);
2805
      if (code != TSDB_CODE_SUCCESS) {
2806
        pTaskInfo->code = code;
H
Haojun Liao 已提交
2807
        qError("failed to getTableList, code: %s", tstrerror(code));
2808 2809 2810
        return NULL;
      }

2811
      pOperator = createTagScanOperatorInfo(pHandle, pScanPhyNode, pTableListInfo, pTaskInfo);
2812
    } else if (QUERY_NODE_PHYSICAL_PLAN_BLOCK_DIST_SCAN == type) {
2813
      SBlockDistScanPhysiNode* pBlockNode = (SBlockDistScanPhysiNode*)pPhyNode;
2814 2815

      if (pBlockNode->tableType == TSDB_SUPER_TABLE) {
H
Haojun Liao 已提交
2816 2817
        SArray* pList = taosArrayInit(4, sizeof(STableKeyInfo));
        int32_t code = vnodeGetAllTableList(pHandle->vnode, pBlockNode->uid, pList);
2818 2819 2820 2821
        if (code != TSDB_CODE_SUCCESS) {
          pTaskInfo->code = terrno;
          return NULL;
        }
H
Haojun Liao 已提交
2822

2823
        for (int32_t i = 0; i < tableListGetSize(pTableListInfo); ++i) {
H
Haojun Liao 已提交
2824
          STableKeyInfo* p = taosArrayGet(pList, i);
H
Haojun Liao 已提交
2825
          tableListAddTableInfo(pTableListInfo, p->uid, 0);
H
Haojun Liao 已提交
2826 2827
        }
        taosArrayDestroy(pList);
2828
      } else {  // Create group with only one table
H
Haojun Liao 已提交
2829
        tableListAddTableInfo(pTableListInfo, pBlockNode->uid, 0);
2830 2831
      }

2832
      pOperator = createDataBlockInfoScanOperator(pHandle, pBlockNode, pTaskInfo);
H
Haojun Liao 已提交
2833 2834 2835
    } else if (QUERY_NODE_PHYSICAL_PLAN_LAST_ROW_SCAN == type) {
      SLastRowScanPhysiNode* pScanNode = (SLastRowScanPhysiNode*)pPhyNode;

L
Liu Jicong 已提交
2836
      int32_t code = createScanTableListInfo(&pScanNode->scan, pScanNode->pGroupTags, true, pHandle, pTableListInfo,
H
Haojun Liao 已提交
2837
                                             pTagCond, pTagIndexCond, pTaskInfo);
2838 2839 2840 2841
      if (code != TSDB_CODE_SUCCESS) {
        pTaskInfo->code = code;
        return NULL;
      }
2842

2843
      code = extractTableSchemaInfo(pHandle, &pScanNode->scan, pTaskInfo);
2844 2845 2846
      if (code != TSDB_CODE_SUCCESS) {
        pTaskInfo->code = code;
        return NULL;
H
Haojun Liao 已提交
2847 2848
      }

2849
      pOperator = createCacherowsScanOperator(pScanNode, pHandle, pTaskInfo);
2850
    } else if (QUERY_NODE_PHYSICAL_PLAN_PROJECT == type) {
2851
      pOperator = createProjectOperatorInfo(NULL, (SProjectPhysiNode*)pPhyNode, pTaskInfo);
H
Haojun Liao 已提交
2852 2853
    } else {
      ASSERT(0);
H
Haojun Liao 已提交
2854
    }
2855 2856 2857 2858 2859

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

2860
    return pOperator;
H
Haojun Liao 已提交
2861 2862
  }

2863
  size_t          size = LIST_LENGTH(pPhyNode->pChildren);
2864
  SOperatorInfo** ops = taosMemoryCalloc(size, POINTER_BYTES);
2865 2866 2867 2868
  if (ops == NULL) {
    return NULL;
  }

dengyihao's avatar
dengyihao 已提交
2869
  for (int32_t i = 0; i < size; ++i) {
2870
    SPhysiNode* pChildNode = (SPhysiNode*)nodesListGetNode(pPhyNode->pChildren, i);
2871
    ops[i] = createOperatorTree(pChildNode, pTaskInfo, pHandle, pTagCond, pTagIndexCond, pUser);
2872
    if (ops[i] == NULL) {
H
Haojun Liao 已提交
2873
      taosMemoryFree(ops);
2874 2875
      return NULL;
    }
2876
  }
H
Haojun Liao 已提交
2877

2878
  SOperatorInfo* pOptr = NULL;
H
Haojun Liao 已提交
2879
  if (QUERY_NODE_PHYSICAL_PLAN_PROJECT == type) {
2880
    pOptr = createProjectOperatorInfo(ops[0], (SProjectPhysiNode*)pPhyNode, pTaskInfo);
2881
  } else if (QUERY_NODE_PHYSICAL_PLAN_HASH_AGG == type) {
H
Haojun Liao 已提交
2882 2883
    SAggPhysiNode* pAggNode = (SAggPhysiNode*)pPhyNode;
    if (pAggNode->pGroupKeys != NULL) {
H
Haojun Liao 已提交
2884
      pOptr = createGroupOperatorInfo(ops[0], pAggNode, pTaskInfo);
H
Haojun Liao 已提交
2885
    } else {
H
Haojun Liao 已提交
2886
      pOptr = createAggregateOperatorInfo(ops[0], pAggNode, pTaskInfo);
H
Haojun Liao 已提交
2887
    }
2888
  } else if (QUERY_NODE_PHYSICAL_PLAN_HASH_INTERVAL == type) {
H
Haojun Liao 已提交
2889
    SIntervalPhysiNode* pIntervalPhyNode = (SIntervalPhysiNode*)pPhyNode;
H
Haojun Liao 已提交
2890

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

2948
  taosMemoryFree(ops);
2949 2950 2951 2952
  if (pOptr) {
    pOptr->resultDataBlockId = pPhyNode->pOutputDataBlockDesc->dataBlockId;
  }

2953
  return pOptr;
2954
}
H
Haojun Liao 已提交
2955

L
Liu Jicong 已提交
2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968
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 {
2969 2970 2971
    SStreamScanInfo* pInfo = pOperator->info;
    ASSERT(pInfo->pTableScanOp->operatorType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN);
    *ppInfo = pInfo->pTableScanOp->info;
L
Liu Jicong 已提交
2972 2973 2974 2975
    return 0;
  }
}

2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997
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;
}

2998
#if 0
L
Liu Jicong 已提交
2999 3000 3001 3002 3003
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;
  }
3004

L
Liu Jicong 已提交
3005 3006 3007 3008
  STableScanPhysiNode* pNode = NULL;
  if (extractTableScanNode(plan->pNode, &pNode) < 0) {
    ASSERT(0);
  }
3009

H
Haojun Liao 已提交
3010
  tsdbReaderClose(pTableScanInfo->dataReader);
3011

L
Liu Jicong 已提交
3012
  STableListInfo info = {0};
H
Haojun Liao 已提交
3013
  pTableScanInfo->dataReader = doCreateDataReader(pNode, pHandle, &info, NULL);
L
Liu Jicong 已提交
3014 3015 3016 3017
  if (pTableScanInfo->dataReader == NULL) {
    ASSERT(0);
    qError("failed to create data reader");
    return TSDB_CODE_QRY_APP_ERROR;
3018
  }
L
Liu Jicong 已提交
3019
  // TODO: set uid and ts to data reader
3020 3021
  return 0;
}
3022
#endif
3023

C
Cary Xu 已提交
3024
int32_t encodeOperator(SOperatorInfo* ops, char** result, int32_t* length, int32_t* nOptrWithVal) {
wmmhello's avatar
wmmhello 已提交
3025
  int32_t code = TDB_CODE_SUCCESS;
3026
  char*   pCurrent = NULL;
wmmhello's avatar
wmmhello 已提交
3027
  int32_t currLength = 0;
3028
  if (ops->fpSet.encodeResultRow) {
C
Cary Xu 已提交
3029
    if (result == NULL || length == NULL || nOptrWithVal == NULL) {
wmmhello's avatar
wmmhello 已提交
3030 3031 3032
      return TSDB_CODE_TSC_INVALID_INPUT;
    }
    code = ops->fpSet.encodeResultRow(ops, &pCurrent, &currLength);
wmmhello's avatar
wmmhello 已提交
3033

3034 3035
    if (code != TDB_CODE_SUCCESS) {
      if (*result != NULL) {
wmmhello's avatar
wmmhello 已提交
3036 3037 3038 3039
        taosMemoryFree(*result);
        *result = NULL;
      }
      return code;
C
Cary Xu 已提交
3040 3041 3042
    } else if (currLength == 0) {
      ASSERT(!pCurrent);
      goto _downstream;
wmmhello's avatar
wmmhello 已提交
3043
    }
wmmhello's avatar
wmmhello 已提交
3044

C
Cary Xu 已提交
3045 3046
    ++(*nOptrWithVal);

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

3049
    if (*result == NULL) {
wmmhello's avatar
wmmhello 已提交
3050
      *result = (char*)taosMemoryCalloc(1, currLength + sizeof(int32_t));
wmmhello's avatar
wmmhello 已提交
3051 3052 3053 3054 3055 3056
      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);
3057
    } else {
wmmhello's avatar
wmmhello 已提交
3058
      int32_t sizePre = *(int32_t*)(*result);
3059
      char*   tmp = (char*)taosMemoryRealloc(*result, sizePre + currLength);
wmmhello's avatar
wmmhello 已提交
3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071
      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 已提交
3072 3073
  }

3074
_downstream:
wmmhello's avatar
wmmhello 已提交
3075
  for (int32_t i = 0; i < ops->numOfDownstream; ++i) {
C
Cary Xu 已提交
3076
    code = encodeOperator(ops->pDownstream[i], result, length, nOptrWithVal);
3077
    if (code != TDB_CODE_SUCCESS) {
wmmhello's avatar
wmmhello 已提交
3078
      return code;
wmmhello's avatar
wmmhello 已提交
3079 3080
    }
  }
wmmhello's avatar
wmmhello 已提交
3081
  return TDB_CODE_SUCCESS;
wmmhello's avatar
wmmhello 已提交
3082 3083
}

H
Haojun Liao 已提交
3084
int32_t decodeOperator(SOperatorInfo* ops, const char* result, int32_t length) {
wmmhello's avatar
wmmhello 已提交
3085
  int32_t code = TDB_CODE_SUCCESS;
3086 3087
  if (ops->fpSet.decodeResultRow) {
    if (result == NULL) {
wmmhello's avatar
wmmhello 已提交
3088 3089
      return TSDB_CODE_TSC_INVALID_INPUT;
    }
H
Haojun Liao 已提交
3090

3091
    ASSERT(length == *(int32_t*)result);
H
Haojun Liao 已提交
3092 3093

    const char* data = result + sizeof(int32_t);
L
Liu Jicong 已提交
3094
    code = ops->fpSet.decodeResultRow(ops, (char*)data);
3095
    if (code != TDB_CODE_SUCCESS) {
wmmhello's avatar
wmmhello 已提交
3096 3097
      return code;
    }
wmmhello's avatar
wmmhello 已提交
3098

wmmhello's avatar
wmmhello 已提交
3099
    int32_t totalLength = *(int32_t*)result;
3100 3101
    int32_t dataLength = *(int32_t*)data;

3102
    if (totalLength == dataLength + sizeof(int32_t)) {  // the last data
wmmhello's avatar
wmmhello 已提交
3103 3104
      result = NULL;
      length = 0;
3105
    } else {
wmmhello's avatar
wmmhello 已提交
3106 3107 3108 3109
      result += dataLength;
      *(int32_t*)(result) = totalLength - dataLength;
      length = totalLength - dataLength;
    }
wmmhello's avatar
wmmhello 已提交
3110 3111
  }

wmmhello's avatar
wmmhello 已提交
3112 3113
  for (int32_t i = 0; i < ops->numOfDownstream; ++i) {
    code = decodeOperator(ops->pDownstream[i], result, length);
3114
    if (code != TDB_CODE_SUCCESS) {
wmmhello's avatar
wmmhello 已提交
3115
      return code;
wmmhello's avatar
wmmhello 已提交
3116 3117
    }
  }
wmmhello's avatar
wmmhello 已提交
3118
  return TDB_CODE_SUCCESS;
wmmhello's avatar
wmmhello 已提交
3119 3120
}

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

D
dapan1121 已提交
3124
  switch (pNode->type) {
D
dapan1121 已提交
3125 3126 3127 3128 3129 3130
    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 已提交
3131

D
dapan1121 已提交
3132 3133 3134
      *pParam = pInserterParam;
      break;
    }
D
dapan1121 已提交
3135
    case QUERY_NODE_PHYSICAL_PLAN_DELETE: {
3136
      SDeleterParam* pDeleterParam = taosMemoryCalloc(1, sizeof(SDeleterParam));
D
dapan1121 已提交
3137 3138 3139
      if (NULL == pDeleterParam) {
        return TSDB_CODE_OUT_OF_MEMORY;
      }
H
Haojun Liao 已提交
3140 3141 3142 3143
      int32_t tbNum = tableListGetSize(pTask->pTableInfoList);
      pDeleterParam->suid = tableListGetSuid(pTask->pTableInfoList);

      // TODO extract uid list
D
dapan1121 已提交
3144 3145 3146 3147 3148
      pDeleterParam->pUidList = taosArrayInit(tbNum, sizeof(uint64_t));
      if (NULL == pDeleterParam->pUidList) {
        taosMemoryFree(pDeleterParam);
        return TSDB_CODE_OUT_OF_MEMORY;
      }
H
Haojun Liao 已提交
3149

D
dapan1121 已提交
3150
      for (int32_t i = 0; i < tbNum; ++i) {
H
Haojun Liao 已提交
3151
        STableKeyInfo* pTable = tableListGetInfo(pTask->pTableInfoList, i);
D
dapan1121 已提交
3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164
        taosArrayPush(pDeleterParam->pUidList, &pTable->uid);
      }

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

  return TSDB_CODE_SUCCESS;
}

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

D
dapan1121 已提交
3169
  *pTaskInfo = createExecTaskInfo(queryId, taskId, model, pPlan->dbFName);
H
Haojun Liao 已提交
3170 3171 3172
  if (*pTaskInfo == NULL) {
    goto _complete;
  }
H
Haojun Liao 已提交
3173

3174
  if (pHandle) {
L
Liu Jicong 已提交
3175
    /*(*pTaskInfo)->streamInfo.fillHistoryVer1 = pHandle->fillHistoryVer1;*/
3176 3177 3178
    if (pHandle->pStateBackend) {
      (*pTaskInfo)->streamInfo.pState = pHandle->pStateBackend;
    }
H
Haojun Liao 已提交
3179 3180
  }

3181
  (*pTaskInfo)->sql = sql;
D
dapan1121 已提交
3182
  sql = NULL;
H
Haojun Liao 已提交
3183

3184
  (*pTaskInfo)->pSubplan = pPlan;
3185 3186
  (*pTaskInfo)->pRoot =
      createOperatorTree(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user);
L
Liu Jicong 已提交
3187

D
dapan1121 已提交
3188
  if (NULL == (*pTaskInfo)->pRoot) {
H
Haojun Liao 已提交
3189
    terrno = (*pTaskInfo)->code;
D
dapan1121 已提交
3190
    goto _complete;
3191 3192
  }

H
Haojun Liao 已提交
3193
  return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
3194

H
Haojun Liao 已提交
3195
_complete:
D
dapan1121 已提交
3196
  taosMemoryFree(sql);
H
Haojun Liao 已提交
3197
  doDestroyTask(*pTaskInfo);
H
Haojun Liao 已提交
3198
  return terrno;
H
Haojun Liao 已提交
3199 3200
}

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

H
Haojun Liao 已提交
3204
  pTaskInfo->pTableInfoList = tableListDestroy(pTaskInfo->pTableInfoList);
H
Haojun Liao 已提交
3205
  destroyOperatorInfo(pTaskInfo->pRoot);
3206
  cleanupTableSchemaInfo(&pTaskInfo->schemaInfo);
3207
  cleanupStreamInfo(&pTaskInfo->streamInfo);
3208

D
dapan1121 已提交
3209
  if (!pTaskInfo->localFetch.localExec) {
D
dapan1121 已提交
3210 3211
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
  }
3212

wafwerar's avatar
wafwerar 已提交
3213 3214 3215
  taosMemoryFreeClear(pTaskInfo->sql);
  taosMemoryFreeClear(pTaskInfo->id.str);
  taosMemoryFreeClear(pTaskInfo);
3216 3217 3218 3219
}

static int64_t getQuerySupportBufSize(size_t numOfTables) {
  size_t s1 = sizeof(STableQueryInfo);
L
Liu Jicong 已提交
3220 3221
  //  size_t s3 = sizeof(STableCheckInfo);  buffer consumption in tsdb
  return (int64_t)(s1 * 1.5 * numOfTables);
3222 3223 3224 3225 3226 3227 3228
}

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 已提交
3229
    while (1) {
3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255
      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 已提交
3256

H
Haojun Liao 已提交
3257
int32_t getOperatorExplainExecInfo(SOperatorInfo* operatorInfo, SArray* pExecInfoList) {
3258
  SExplainExecInfo  execInfo = {0};
H
Haojun Liao 已提交
3259
  SExplainExecInfo* pExplainInfo = taosArrayPush(pExecInfoList, &execInfo);
3260

H
Haojun Liao 已提交
3261 3262 3263 3264 3265
  pExplainInfo->numOfRows = operatorInfo->resultInfo.totalRows;
  pExplainInfo->startupCost = operatorInfo->cost.openCost;
  pExplainInfo->totalCost = operatorInfo->cost.totalCost;
  pExplainInfo->verboseLen = 0;
  pExplainInfo->verboseInfo = NULL;
D
dapan1121 已提交
3266

3267
  if (operatorInfo->fpSet.getExplainFn) {
3268 3269
    int32_t code =
        operatorInfo->fpSet.getExplainFn(operatorInfo, &pExplainInfo->verboseInfo, &pExplainInfo->verboseLen);
D
dapan1121 已提交
3270
    if (code) {
3271
      qError("%s operator getExplainFn failed, code:%s", GET_TASKID(operatorInfo->pTaskInfo), tstrerror(code));
D
dapan1121 已提交
3272 3273 3274
      return code;
    }
  }
dengyihao's avatar
dengyihao 已提交
3275

D
dapan1121 已提交
3276
  int32_t code = 0;
D
dapan1121 已提交
3277
  for (int32_t i = 0; i < operatorInfo->numOfDownstream; ++i) {
H
Haojun Liao 已提交
3278 3279
    code = getOperatorExplainExecInfo(operatorInfo->pDownstream[i], pExecInfoList);
    if (code != TSDB_CODE_SUCCESS) {
3280
      //      taosMemoryFreeClear(*pRes);
D
dapan1121 已提交
3281 3282 3283 3284 3285
      return TSDB_CODE_QRY_OUT_OF_MEMORY;
    }
  }

  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
3286
}
5
54liuyao 已提交
3287

3288 3289
int32_t setOutputBuf(SStreamState* pState, STimeWindow* win, SResultRow** pResult, int64_t tableGroupId,
                     SqlFunctionCtx* pCtx, int32_t numOfOutput, int32_t* rowEntryInfoOffset, SAggSupporter* pAggSup) {
3290 3291 3292 3293 3294 3295
  SWinKey key = {
      .ts = win->skey,
      .groupId = tableGroupId,
  };
  char*   value = NULL;
  int32_t size = pAggSup->resultRowSize;
5
54liuyao 已提交
3296

3297
  if (streamStateAddIfNotExist(pState, &key, (void**)&value, &size) < 0) {
3298 3299 3300 3301 3302 3303 3304 3305 3306 3307
    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;
}

3308 3309
int32_t releaseOutputBuf(SStreamState* pState, SWinKey* pKey, SResultRow* pResult) {
  streamStateReleaseBuf(pState, pKey, pResult);
3310 3311 3312
  return TSDB_CODE_SUCCESS;
}

3313 3314
int32_t saveOutputBuf(SStreamState* pState, SWinKey* pKey, SResultRow* pResult, int32_t resSize) {
  streamStatePut(pState, pKey, pResult, resSize);
3315 3316 3317
  return TSDB_CODE_SUCCESS;
}

3318
int32_t buildDataBlockFromGroupRes(SOperatorInfo* pOperator, SStreamState* pState, SSDataBlock* pBlock, SExprSupp* pSup,
3319
                                   SGroupResInfo* pGroupResInfo) {
3320
  SExecTaskInfo*  pTaskInfo = pOperator->pTaskInfo;
3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332
  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 = {
3333 3334
            .ts = *(TSKEY*)pPos->key,
            .groupId = pPos->groupId,
3335
    };
3336
    int32_t code = streamStateGet(pState, &key, &pVal, &size);
3337 3338 3339 3340 3341 3342
    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;
3343
      releaseOutputBuf(pState, &key, pRow);
3344 3345 3346 3347 3348
      continue;
    }

    if (pBlock->info.groupId == 0) {
      pBlock->info.groupId = pPos->groupId;
3349 3350 3351 3352
      SStreamIntervalOperatorInfo* pInfo = pOperator->info;
      char* tbname = taosHashGet(pInfo->pGroupIdTbNameMap, &pBlock->info.groupId, sizeof(int64_t));
      if (tbname != NULL) {
        memcpy(pBlock->info.parTbName, tbname, TSDB_TABLE_NAME_LEN);
L
Liu Jicong 已提交
3353 3354
      } else {
        pBlock->info.parTbName[0] = 0;
3355
      }
3356 3357 3358
    } else {
      // current value belongs to different group, it can't be packed into one datablock
      if (pBlock->info.groupId != pPos->groupId) {
3359
        releaseOutputBuf(pState, &key, pRow);
3360 3361 3362 3363 3364 3365
        break;
      }
    }

    if (pBlock->info.rows + pRow->numOfRows > pBlock->info.capacity) {
      ASSERT(pBlock->info.rows > 0);
3366
      releaseOutputBuf(pState, &key, pRow);
3367 3368 3369 3370 3371 3372 3373 3374 3375 3376
      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) {
3377 3378 3379 3380
        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);
3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393
        }
      } 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 已提交
3394

3395
    pBlock->info.rows += pRow->numOfRows;
3396
    releaseOutputBuf(pState, &key, pRow);
3397 3398 3399 3400
  }
  blockDataUpdateTsWindow(pBlock, 0);
  return TSDB_CODE_SUCCESS;
}
5
54liuyao 已提交
3401 3402 3403 3404 3405 3406 3407

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

3408
int32_t buildSessionResultDataBlock(SOperatorInfo* pOperator, SStreamState* pState, SSDataBlock* pBlock,
5
54liuyao 已提交
3409
                                    SExprSupp* pSup, SGroupResInfo* pGroupResInfo) {
3410
  SExecTaskInfo*  pTaskInfo = pOperator->pTaskInfo;
5
54liuyao 已提交
3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423
  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);
3424 3425 3426 3427
    if (code == -1) {
      // coverity scan
      continue;
    }
5
54liuyao 已提交
3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438
    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;
3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463

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

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

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

5
54liuyao 已提交
3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508
    } 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;
3509
}