groupoperator.c 48.3 KB
Newer Older
H
Haojun Liao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

16
#include "filter.h"
H
Haojun Liao 已提交
17
#include "function.h"
18
#include "os.h"
H
Haojun Liao 已提交
19 20 21 22 23
#include "tname.h"

#include "tdatablock.h"
#include "tmsg.h"

24
#include "executorInt.h"
25 26
#include "operator.h"
#include "querytask.h"
H
Haojun Liao 已提交
27 28 29 30
#include "tcompare.h"
#include "thash.h"
#include "ttypes.h"

H
Haojun Liao 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
typedef struct SGroupbyOperatorInfo {
  SOptrBasicInfo binfo;
  SAggSupporter  aggSup;
  SArray*        pGroupCols;     // group by columns, SArray<SColumn>
  SArray*        pGroupColVals;  // current group column values, SArray<SGroupKeys>
  bool           isInit;         // denote if current val is initialized or not
  char*          keyBuf;         // group by keys for hash
  int32_t        groupKeyLen;    // total group by column width
  SGroupResInfo  groupResInfo;
  SExprSupp      scalarSup;
} SGroupbyOperatorInfo;

// The sort in partition may be needed later.
typedef struct SPartitionOperatorInfo {
  SOptrBasicInfo binfo;
  SArray*        pGroupCols;
  SArray*        pGroupColVals;  // current group column values, SArray<SGroupKeys>
  char*          keyBuf;         // group by keys for hash
  int32_t        groupKeyLen;    // total group by column width
  SHashObj*      pGroupSet;      // quick locate the window object for each result

  SDiskbasedBuf* pBuf;              // query result buffer based on blocked-wised disk file
  int32_t        rowCapacity;       // maximum number of rows for each buffer page
  int32_t*       columnOffset;      // start position for each column data
  SArray*        sortedGroupArray;  // SDataGroupInfo sorted by group id
  int32_t        groupIndex;        // group index
  int32_t        pageIndex;         // page index of current group
  SExprSupp      scalarSup;
} SPartitionOperatorInfo;

61
static void*    getCurrentDataGroupInfo(const SPartitionOperatorInfo* pInfo, SDataGroupInfo** pGroupInfo, int32_t len);
H
Haojun Liao 已提交
62
static int32_t* setupColumnOffset(const SSDataBlock* pBlock, int32_t rowCapacity);
63 64
static int32_t  setGroupResultOutputBuf(SOperatorInfo* pOperator, SOptrBasicInfo* binfo, int32_t numOfCols, char* pData,
                                        int16_t bytes, uint64_t groupId, SDiskbasedBuf* pBuf, SAggSupporter* pAggSup);
H
Haojun Liao 已提交
65
static SArray*  extractColumnInfo(SNodeList* pNodeList);
H
Haojun Liao 已提交
66

H
Haojun Liao 已提交
67
static void freeGroupKey(void* param) {
68
  SGroupKeys* pKey = (SGroupKeys*)param;
H
Haojun Liao 已提交
69 70 71
  taosMemoryFree(pKey->pData);
}

72
static void destroyGroupOperatorInfo(void* param) {
H
Haojun Liao 已提交
73
  SGroupbyOperatorInfo* pInfo = (SGroupbyOperatorInfo*)param;
74 75 76 77
  if (pInfo == NULL) {
    return;
  }

78
  cleanupBasicInfo(&pInfo->binfo);
H
Haojun Liao 已提交
79 80
  taosMemoryFreeClear(pInfo->keyBuf);
  taosArrayDestroy(pInfo->pGroupCols);
H
Haojun Liao 已提交
81
  taosArrayDestroyEx(pInfo->pGroupColVals, freeGroupKey);
82
  cleanupExprSupp(&pInfo->scalarSup);
H
Haojun Liao 已提交
83 84 85

  cleanupGroupResInfo(&pInfo->groupResInfo);
  cleanupAggSup(&pInfo->aggSup);
D
dapan1121 已提交
86
  taosMemoryFreeClear(param);
H
Haojun Liao 已提交
87 88
}

wmmhello's avatar
wmmhello 已提交
89
static int32_t initGroupOptrInfo(SArray** pGroupColVals, int32_t* keyLen, char** keyBuf, const SArray* pGroupColList) {
H
Haojun Liao 已提交
90 91
  *pGroupColVals = taosArrayInit(4, sizeof(SGroupKeys));
  if ((*pGroupColVals) == NULL) {
H
Haojun Liao 已提交
92 93 94 95 96
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  int32_t numOfGroupCols = taosArrayGetSize(pGroupColList);
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
5
54liuyao 已提交
97
    SColumn* pCol = (SColumn*)taosArrayGet(pGroupColList, i);
98
    (*keyLen) += pCol->bytes;  // actual data + null_flag
H
Haojun Liao 已提交
99

100
    SGroupKeys key = {0};
101 102
    key.bytes = pCol->bytes;
    key.type = pCol->type;
H
Haojun Liao 已提交
103
    key.isNull = false;
104
    key.pData = taosMemoryCalloc(1, pCol->bytes);
H
Haojun Liao 已提交
105 106 107 108
    if (key.pData == NULL) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }

H
Haojun Liao 已提交
109
    taosArrayPush((*pGroupColVals), &key);
H
Haojun Liao 已提交
110 111 112
  }

  int32_t nullFlagSize = sizeof(int8_t) * numOfGroupCols;
113
  (*keyLen) += nullFlagSize;
H
Haojun Liao 已提交
114

115
  (*keyBuf) = taosMemoryCalloc(1, (*keyLen));
H
Haojun Liao 已提交
116
  if ((*keyBuf) == NULL) {
H
Haojun Liao 已提交
117 118 119 120 121 122
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  return TSDB_CODE_SUCCESS;
}

123 124
static bool groupKeyCompare(SArray* pGroupCols, SArray* pGroupColVals, SSDataBlock* pBlock, int32_t rowIndex,
                            int32_t numOfGroupCols) {
H
Haojun Liao 已提交
125 126
  SColumnDataAgg* pColAgg = NULL;
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
H
Haojun Liao 已提交
127
    SColumn*         pCol = taosArrayGet(pGroupCols, i);
H
Haojun Liao 已提交
128 129
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, pCol->slotId);
    if (pBlock->pBlockAgg != NULL) {
130
      pColAgg = pBlock->pBlockAgg[pCol->slotId];  // TODO is agg data matched?
H
Haojun Liao 已提交
131 132 133 134
    }

    bool isNull = colDataIsNull(pColInfoData, pBlock->info.rows, rowIndex, pColAgg);

H
Haojun Liao 已提交
135
    SGroupKeys* pkey = taosArrayGet(pGroupColVals, i);
H
Haojun Liao 已提交
136 137 138 139 140 141 142 143 144 145
    if (pkey->isNull && isNull) {
      continue;
    }

    if (isNull || pkey->isNull) {
      return false;
    }

    char* val = colDataGetData(pColInfoData, rowIndex);

wmmhello's avatar
wmmhello 已提交
146 147 148
    if (pkey->type == TSDB_DATA_TYPE_JSON) {
      int32_t dataLen = getJsonValueLen(val);

149
      if (memcmp(pkey->pData, val, dataLen) == 0) {
wmmhello's avatar
wmmhello 已提交
150 151 152 153 154
        continue;
      } else {
        return false;
      }
    } else if (IS_VAR_DATA_TYPE(pkey->type)) {
H
Haojun Liao 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
      int32_t len = varDataLen(val);
      if (len == varDataLen(pkey->pData) && memcmp(varDataVal(pkey->pData), varDataVal(val), len) == 0) {
        continue;
      } else {
        return false;
      }
    } else {
      if (memcmp(pkey->pData, val, pkey->bytes) != 0) {
        return false;
      }
    }
  }

  return true;
}

wmmhello's avatar
wmmhello 已提交
171
static void recordNewGroupKeys(SArray* pGroupCols, SArray* pGroupColVals, SSDataBlock* pBlock, int32_t rowIndex) {
H
Haojun Liao 已提交
172 173
  SColumnDataAgg* pColAgg = NULL;

174 175
  size_t numOfGroupCols = taosArrayGetSize(pGroupCols);

H
Haojun Liao 已提交
176
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
D
dapan1121 已提交
177
    SColumn*         pCol = (SColumn*) taosArrayGet(pGroupCols, i);
H
Haojun Liao 已提交
178 179
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, pCol->slotId);

D
dapan1121 已提交
180 181 182 183 184
    // valid range check. todo: return error code.
    if (pCol->slotId > taosArrayGetSize(pBlock->pDataBlock)) {
      continue;
    }

H
Haojun Liao 已提交
185
    if (pBlock->pBlockAgg != NULL) {
186
      pColAgg = pBlock->pBlockAgg[pCol->slotId];  // TODO is agg data matched?
H
Haojun Liao 已提交
187 188
    }

H
Haojun Liao 已提交
189
    SGroupKeys* pkey = taosArrayGet(pGroupColVals, i);
H
Haojun Liao 已提交
190 191 192
    if (colDataIsNull(pColInfoData, pBlock->info.rows, rowIndex, pColAgg)) {
      pkey->isNull = true;
    } else {
193
      pkey->isNull = false;
H
Haojun Liao 已提交
194
      char* val = colDataGetData(pColInfoData, rowIndex);
wmmhello's avatar
wmmhello 已提交
195
      if (pkey->type == TSDB_DATA_TYPE_JSON) {
196
        if (tTagIsJson(val)) {
wmmhello's avatar
wmmhello 已提交
197 198 199
          terrno = TSDB_CODE_QRY_JSON_IN_GROUP_ERROR;
          return;
        }
wmmhello's avatar
wmmhello 已提交
200 201 202
        int32_t dataLen = getJsonValueLen(val);
        memcpy(pkey->pData, val, dataLen);
      } else if (IS_VAR_DATA_TYPE(pkey->type)) {
H
Haojun Liao 已提交
203
        memcpy(pkey->pData, val, varDataTLen(val));
204
        ASSERT(varDataTLen(val) <= pkey->bytes);
H
Haojun Liao 已提交
205 206 207 208 209 210 211
      } else {
        memcpy(pkey->pData, val, pkey->bytes);
      }
    }
  }
}

wmmhello's avatar
wmmhello 已提交
212
static int32_t buildGroupKeys(void* pKey, const SArray* pGroupColVals) {
H
Haojun Liao 已提交
213 214 215 216 217 218 219 220 221 222 223 224
  size_t numOfGroupCols = taosArrayGetSize(pGroupColVals);

  char* isNull = (char*)pKey;
  char* pStart = (char*)pKey + sizeof(int8_t) * numOfGroupCols;
  for (int32_t i = 0; i < numOfGroupCols; ++i) {
    SGroupKeys* pkey = taosArrayGet(pGroupColVals, i);
    if (pkey->isNull) {
      isNull[i] = 1;
      continue;
    }

    isNull[i] = 0;
wmmhello's avatar
wmmhello 已提交
225 226 227 228 229
    if (pkey->type == TSDB_DATA_TYPE_JSON) {
      int32_t dataLen = getJsonValueLen(pkey->pData);
      memcpy(pStart, (pkey->pData), dataLen);
      pStart += dataLen;
    } else if (IS_VAR_DATA_TYPE(pkey->type)) {
H
Haojun Liao 已提交
230 231
      varDataCopy(pStart, pkey->pData);
      pStart += varDataTLen(pkey->pData);
232
      ASSERT(varDataTLen(pkey->pData) <= pkey->bytes);
H
Haojun Liao 已提交
233 234 235 236 237 238
    } else {
      memcpy(pStart, pkey->pData, pkey->bytes);
      pStart += pkey->bytes;
    }
  }

239
  return (int32_t)(pStart - (char*)pKey);
H
Haojun Liao 已提交
240 241 242 243 244
}

// assign the group keys or user input constant values if required
static void doAssignGroupKeys(SqlFunctionCtx* pCtx, int32_t numOfOutput, int32_t totalRows, int32_t rowIndex) {
  for (int32_t i = 0; i < numOfOutput; ++i) {
245
    if (pCtx[i].functionId == -1) {  // select count(*),key from t group by key.
H
Haojun Liao 已提交
246 247 248
      SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(&pCtx[i]);

      SColumnInfoData* pColInfoData = pCtx[i].input.pData[0];
249
      // todo OPT all/all not NULL
H
Haojun Liao 已提交
250 251 252 253
      if (!colDataIsNull(pColInfoData, totalRows, rowIndex, NULL)) {
        char* dest = GET_ROWCELL_INTERBUF(pEntryInfo);
        char* data = colDataGetData(pColInfoData, rowIndex);

wmmhello's avatar
wmmhello 已提交
254 255 256 257
        if (pColInfoData->info.type == TSDB_DATA_TYPE_JSON) {
          int32_t dataLen = getJsonValueLen(data);
          memcpy(dest, data, dataLen);
        } else if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
258 259 260 261
          varDataCopy(dest, data);
        } else {
          memcpy(dest, data, pColInfoData->info.bytes);
        }
262
      } else {  // it is a NULL value
H
Haojun Liao 已提交
263
        pEntryInfo->isNullRes = 1;
H
Haojun Liao 已提交
264
      }
H
Haojun Liao 已提交
265 266

      pEntryInfo->numOfRes = 1;
H
Haojun Liao 已提交
267 268 269 270 271 272 273 274
    }
  }
}

static void doHashGroupbyAgg(SOperatorInfo* pOperator, SSDataBlock* pBlock) {
  SExecTaskInfo*        pTaskInfo = pOperator->pTaskInfo;
  SGroupbyOperatorInfo* pInfo = pOperator->info;

275
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
H
Haojun Liao 已提交
276 277 278 279 280 281
  int32_t         numOfGroupCols = taosArrayGetSize(pInfo->pGroupCols);
  //  if (type == TSDB_DATA_TYPE_FLOAT || type == TSDB_DATA_TYPE_DOUBLE) {
  // qError("QInfo:0x%"PRIx64" group by not supported on double/float columns, abort", GET_TASKID(pRuntimeEnv));
  //    return;
  //  }

H
Haojun Liao 已提交
282
  int32_t len = 0;
wmmhello's avatar
wmmhello 已提交
283
  terrno = TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
284

H
Haojun Liao 已提交
285
  int32_t num = 0;
D
dapan1121 已提交
286
  uint64_t groupId = 0;
H
Haojun Liao 已提交
287 288 289
  for (int32_t j = 0; j < pBlock->info.rows; ++j) {
    // Compare with the previous row of this column, and do not set the output buffer again if they are identical.
    if (!pInfo->isInit) {
290
      recordNewGroupKeys(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j);
wmmhello's avatar
wmmhello 已提交
291
      if (terrno != TSDB_CODE_SUCCESS) {  // group by json error
292
        T_LONG_JMP(pTaskInfo->env, terrno);
wmmhello's avatar
wmmhello 已提交
293
      }
H
Haojun Liao 已提交
294 295 296 297 298
      pInfo->isInit = true;
      num++;
      continue;
    }

H
Haojun Liao 已提交
299
    bool equal = groupKeyCompare(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j, numOfGroupCols);
H
Haojun Liao 已提交
300 301 302 303 304
    if (equal) {
      num++;
      continue;
    }

H
Haojun Liao 已提交
305
    // The first row of a new block does not belongs to the previous existed group
306
    if (j == 0) {
H
Haojun Liao 已提交
307
      num++;
308
      recordNewGroupKeys(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j);
wmmhello's avatar
wmmhello 已提交
309
      if (terrno != TSDB_CODE_SUCCESS) {  // group by json error
310
        T_LONG_JMP(pTaskInfo->env, terrno);
wmmhello's avatar
wmmhello 已提交
311
      }
H
Haojun Liao 已提交
312 313 314
      continue;
    }

H
Haojun Liao 已提交
315
    len = buildGroupKeys(pInfo->keyBuf, pInfo->pGroupColVals);
316
    int32_t ret = setGroupResultOutputBuf(pOperator, &(pInfo->binfo), pOperator->exprSupp.numOfExprs, pInfo->keyBuf,
H
Haojun Liao 已提交
317
                                          len, pBlock->info.id.groupId, pInfo->aggSup.pResultBuf, &pInfo->aggSup);
H
Haojun Liao 已提交
318
    if (ret != TSDB_CODE_SUCCESS) {  // null data, too many state code
S
Shengliang Guan 已提交
319
      T_LONG_JMP(pTaskInfo->env, TSDB_CODE_APP_ERROR);
H
Haojun Liao 已提交
320 321 322
    }

    int32_t rowIndex = j - num;
dengyihao's avatar
dengyihao 已提交
323 324
    applyAggFunctionOnPartialTuples(pTaskInfo, pCtx, NULL, rowIndex, num, pBlock->info.rows,
                                    pOperator->exprSupp.numOfExprs);
H
Haojun Liao 已提交
325 326

    // assign the group keys or user input constant values if required
327
    doAssignGroupKeys(pCtx, pOperator->exprSupp.numOfExprs, pBlock->info.rows, rowIndex);
328
    recordNewGroupKeys(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j);
H
Haojun Liao 已提交
329 330 331 332
    num = 1;
  }

  if (num > 0) {
H
Haojun Liao 已提交
333
    len = buildGroupKeys(pInfo->keyBuf, pInfo->pGroupColVals);
334
    int32_t ret = setGroupResultOutputBuf(pOperator, &(pInfo->binfo), pOperator->exprSupp.numOfExprs, pInfo->keyBuf,
H
Haojun Liao 已提交
335
                                          len, pBlock->info.id.groupId, pInfo->aggSup.pResultBuf, &pInfo->aggSup);
H
Haojun Liao 已提交
336
    if (ret != TSDB_CODE_SUCCESS) {
S
Shengliang Guan 已提交
337
      T_LONG_JMP(pTaskInfo->env, TSDB_CODE_APP_ERROR);
H
Haojun Liao 已提交
338 339 340
    }

    int32_t rowIndex = pBlock->info.rows - num;
dengyihao's avatar
dengyihao 已提交
341 342
    applyAggFunctionOnPartialTuples(pTaskInfo, pCtx, NULL, rowIndex, num, pBlock->info.rows,
                                    pOperator->exprSupp.numOfExprs);
343
    doAssignGroupKeys(pCtx, pOperator->exprSupp.numOfExprs, pBlock->info.rows, rowIndex);
H
Haojun Liao 已提交
344 345 346
  }
}

347 348 349 350
static SSDataBlock* buildGroupResultDataBlock(SOperatorInfo* pOperator) {
  SGroupbyOperatorInfo* pInfo = pOperator->info;

  SSDataBlock* pRes = pInfo->binfo.pRes;
351
  while (1) {
352
    doBuildResultDatablock(pOperator, &pInfo->binfo, &pInfo->groupResInfo, pInfo->aggSup.pResultBuf);
H
Haojun Liao 已提交
353
    doFilter(pRes, pOperator->exprSupp.pFilterInfo, NULL);
354

355
    if (!hasRemainResults(&pInfo->groupResInfo)) {
H
Haojun Liao 已提交
356
      setOperatorCompleted(pOperator);
357 358 359 360 361 362 363 364 365
      break;
    }

    if (pRes->info.rows > 0) {
      break;
    }
  }

  pOperator->resultInfo.totalRows += pRes->info.rows;
366
  return (pRes->info.rows == 0) ? NULL : pRes;
367 368
}

369
static SSDataBlock* hashGroupbyAggregate(SOperatorInfo* pOperator) {
H
Haojun Liao 已提交
370 371 372 373
  if (pOperator->status == OP_EXEC_DONE) {
    return NULL;
  }

374 375
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;

H
Haojun Liao 已提交
376 377
  SGroupbyOperatorInfo* pInfo = pOperator->info;
  if (pOperator->status == OP_RES_TO_RETURN) {
378
    return buildGroupResultDataBlock(pOperator);
H
Haojun Liao 已提交
379 380
  }

381 382 383
  int32_t order = TSDB_ORDER_ASC;
  int32_t scanFlag = MAIN_SCAN;

384
  int64_t        st = taosGetTimestampUs();
H
Haojun Liao 已提交
385 386 387
  SOperatorInfo* downstream = pOperator->pDownstream[0];

  while (1) {
388
    SSDataBlock* pBlock = downstream->fpSet.getNextFn(downstream);
H
Haojun Liao 已提交
389 390 391 392
    if (pBlock == NULL) {
      break;
    }

393
    int32_t code = getTableScanInfo(pOperator, &order, &scanFlag, false);
394
    if (code != TSDB_CODE_SUCCESS) {
395
      T_LONG_JMP(pTaskInfo->env, code);
396 397
    }

H
Haojun Liao 已提交
398
    // the pDataBlock are always the same one, no need to call this again
399
    setInputDataBlock(&pOperator->exprSupp, pBlock, order, scanFlag, true);
400

401
    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
402
    if (pInfo->scalarSup.pExprInfo != NULL) {
403 404
      pTaskInfo->code = projectApplyFunctions(pInfo->scalarSup.pExprInfo, pBlock, pBlock, pInfo->scalarSup.pCtx,
                                              pInfo->scalarSup.numOfExprs, NULL);
405
      if (pTaskInfo->code != TSDB_CODE_SUCCESS) {
406
        T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
407
      }
408 409
    }

H
Haojun Liao 已提交
410 411 412 413
    doHashGroupbyAgg(pOperator, pBlock);
  }

  pOperator->status = OP_RES_TO_RETURN;
H
Haojun Liao 已提交
414

415 416 417 418 419 420 421 422 423 424 425 426 427 428
#if 0
  if(pOperator->fpSet.encodeResultRow){
    char *result = NULL;
    int32_t length = 0;
    pOperator->fpSet.encodeResultRow(pOperator, &result, &length);
    SAggSupporter* pSup = &pInfo->aggSup;
    taosHashClear(pSup->pResultRowHashTable);
    pInfo->binfo.resultRowInfo.size = 0;
    pOperator->fpSet.decodeResultRow(pOperator, result);
    if(result){
      taosMemoryFree(result);
    }
  }
#endif
429
  initGroupedResultInfo(&pInfo->groupResInfo, pInfo->aggSup.pResultRowHashTable, 0);
430

431
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
432
  return buildGroupResultDataBlock(pOperator);
H
Haojun Liao 已提交
433 434
}

5
54liuyao 已提交
435
SOperatorInfo* createGroupOperatorInfo(SOperatorInfo* downstream, SAggPhysiNode* pAggNode, SExecTaskInfo* pTaskInfo) {
436
  int32_t               code = TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
437 438 439
  SGroupbyOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SGroupbyOperatorInfo));
  SOperatorInfo*        pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
  if (pInfo == NULL || pOperator == NULL) {
440
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Haojun Liao 已提交
441 442 443
    goto _error;
  }

H
Haojun Liao 已提交
444
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pAggNode->node.pOutputDataBlockDesc);
H
Haojun Liao 已提交
445 446 447 448 449 450 451 452 453
  initBasicInfo(&pInfo->binfo, pResBlock);

  int32_t    numOfScalarExpr = 0;
  SExprInfo* pScalarExprInfo = NULL;
  if (pAggNode->pExprs != NULL) {
    pScalarExprInfo = createExprInfo(pAggNode->pExprs, NULL, &numOfScalarExpr);
  }

  pInfo->pGroupCols = extractColumnInfo(pAggNode->pGroupKeys);
454
  code = initExprSupp(&pInfo->scalarSup, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
455 456 457
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
458

H
Haojun Liao 已提交
459
  initResultSizeInfo(&pOperator->resultInfo, 4096);
H
Haojun Liao 已提交
460 461
  blockDataEnsureCapacity(pInfo->binfo.pRes, pOperator->resultInfo.capacity);

H
Haojun Liao 已提交
462
  code = initGroupOptrInfo(&pInfo->pGroupColVals, &pInfo->groupKeyLen, &pInfo->keyBuf, pInfo->pGroupCols);
H
Haojun Liao 已提交
463 464 465 466
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }

H
Haojun Liao 已提交
467 468
  int32_t    num = 0;
  SExprInfo* pExprInfo = createExprInfo(pAggNode->pAggFuncs, pAggNode->pGroupKeys, &num);
L
Liu Jicong 已提交
469
  code = initAggSup(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, num, pInfo->groupKeyLen, pTaskInfo->id.str,
470
                    pTaskInfo->streamInfo.pState, &pTaskInfo->storageAPI.functionStore);
471 472 473 474
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }

475 476 477 478 479
  code = filterInitFromNode((SNode*)pAggNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0);
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }

480
  initResultRowInfo(&pInfo->binfo.resultRowInfo);
481
  setOperatorInfo(pOperator, "GroupbyAggOperator", 0, true, OP_NOT_OPENED, pInfo, pTaskInfo);
H
Haojun Liao 已提交
482

D
dapan1121 已提交
483 484
  pInfo->binfo.mergeResultBlock = pAggNode->mergeDataBlock;

dengyihao's avatar
dengyihao 已提交
485 486
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, hashGroupbyAggregate, NULL, destroyGroupOperatorInfo,
                                         optrDefaultBufFn, NULL);
H
Haojun Liao 已提交
487
  code = appendDownstream(pOperator, &downstream, 1);
488 489 490 491
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }

H
Haojun Liao 已提交
492 493
  return pOperator;

494
_error:
495
  pTaskInfo->code = code;
H
Haojun Liao 已提交
496 497 498
  if (pInfo != NULL) {
    destroyGroupOperatorInfo(pInfo);
  }
H
Haojun Liao 已提交
499 500
  taosMemoryFreeClear(pOperator);
  return NULL;
501 502
}

H
Haojun Liao 已提交
503 504
static void doHashPartition(SOperatorInfo* pOperator, SSDataBlock* pBlock) {
  SPartitionOperatorInfo* pInfo = pOperator->info;
dengyihao's avatar
dengyihao 已提交
505 506
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;

H
Haojun Liao 已提交
507
  for (int32_t j = 0; j < pBlock->info.rows; ++j) {
508
    recordNewGroupKeys(pInfo->pGroupCols, pInfo->pGroupColVals, pBlock, j);
H
Haojun Liao 已提交
509 510
    int32_t len = buildGroupKeys(pInfo->keyBuf, pInfo->pGroupColVals);

511
    SDataGroupInfo* pGroupInfo = NULL;
512
    void*           pPage = getCurrentDataGroupInfo(pInfo, &pGroupInfo, len);
513 514 515
    if (pPage == NULL) {
      T_LONG_JMP(pTaskInfo->env, terrno);
    }
H
Haojun Liao 已提交
516

517 518 519 520 521
    pGroupInfo->numOfRows += 1;

    // group id
    if (pGroupInfo->groupId == 0) {
      pGroupInfo->groupId = calcGroupId(pInfo->keyBuf, len);
H
Haojun Liao 已提交
522 523
    }

524
    // number of rows
525
    int32_t* rows = (int32_t*)pPage;
H
Haojun Liao 已提交
526

527
    size_t numOfCols = pOperator->exprSupp.numOfExprs;
528
    for (int32_t i = 0; i < numOfCols; ++i) {
529
      SExprInfo* pExpr = &pOperator->exprSupp.pExprInfo[i];
530
      int32_t    slotId = pExpr->base.pParam[0].pCol->slotId;
531 532

      SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, slotId);
H
Haojun Liao 已提交
533

H
Haojun Liao 已提交
534 535
      int32_t bytes = pColInfoData->info.bytes;
      int32_t startOffset = pInfo->columnOffset[i];
H
Haojun Liao 已提交
536

537
      int32_t* columnLen = NULL;
538
      int32_t  contentLen = 0;
H
Haojun Liao 已提交
539 540

      if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
wafwerar's avatar
wafwerar 已提交
541
        int32_t* offset = (int32_t*)((char*)pPage + startOffset);
542 543
        columnLen = (int32_t*)((char*)pPage + startOffset + sizeof(int32_t) * pInfo->rowCapacity);
        char* data = (char*)((char*)columnLen + sizeof(int32_t));
H
Haojun Liao 已提交
544 545 546 547

        if (colDataIsNull_s(pColInfoData, j)) {
          offset[(*rows)] = -1;
          contentLen = 0;
548
        } else if (pColInfoData->info.type == TSDB_DATA_TYPE_JSON) {
wmmhello's avatar
wmmhello 已提交
549
          offset[*rows] = (*columnLen);
550
          char*   src = colDataGetData(pColInfoData, j);
wmmhello's avatar
wmmhello 已提交
551 552 553 554
          int32_t dataLen = getJsonValueLen(src);

          memcpy(data + (*columnLen), src, dataLen);
          int32_t v = (data + (*columnLen) + dataLen - (char*)pPage);
555
          ASSERT(v > 0);
wmmhello's avatar
wmmhello 已提交
556 557

          contentLen = dataLen;
H
Haojun Liao 已提交
558 559 560 561
        } else {
          offset[*rows] = (*columnLen);
          char* src = colDataGetData(pColInfoData, j);
          memcpy(data + (*columnLen), src, varDataTLen(src));
562
          int32_t v = (data + (*columnLen) + varDataTLen(src) - (char*)pPage);
563
          ASSERT(v > 0);
564

H
Haojun Liao 已提交
565 566
          contentLen = varDataTLen(src);
        }
H
Haojun Liao 已提交
567
      } else {
wafwerar's avatar
wafwerar 已提交
568
        char* bitmap = (char*)pPage + startOffset;
569 570
        columnLen = (int32_t*)((char*)pPage + startOffset + BitmapLen(pInfo->rowCapacity));
        char* data = (char*)columnLen + sizeof(int32_t);
H
Haojun Liao 已提交
571 572 573

        bool isNull = colDataIsNull_f(pColInfoData->nullbitmap, j);
        if (isNull) {
H
Haojun Liao 已提交
574
          colDataSetNull_f(bitmap, (*rows));
H
Haojun Liao 已提交
575
        } else {
H
Haojun Liao 已提交
576
          memcpy(data + (*columnLen), colDataGetData(pColInfoData, j), bytes);
577
          ASSERT((data + (*columnLen) + bytes - (char*)pPage) <= getBufPageSize(pInfo->pBuf));
H
Haojun Liao 已提交
578
        }
H
Haojun Liao 已提交
579
        contentLen = bytes;
H
Haojun Liao 已提交
580
      }
H
Haojun Liao 已提交
581 582

      (*columnLen) += contentLen;
H
Haojun Liao 已提交
583 584
    }

H
Haojun Liao 已提交
585 586
    (*rows) += 1;

H
Haojun Liao 已提交
587 588 589
    setBufPageDirty(pPage, true);
    releaseBufPage(pInfo->pBuf, pPage);
  }
H
Haojun Liao 已提交
590 591 592 593 594 595
}

void* getCurrentDataGroupInfo(const SPartitionOperatorInfo* pInfo, SDataGroupInfo** pGroupInfo, int32_t len) {
  SDataGroupInfo* p = taosHashGet(pInfo->pGroupSet, pInfo->keyBuf, len);

  void* pPage = NULL;
596
  if (p == NULL) {  // it is a new group
H
Haojun Liao 已提交
597 598 599 600 601 602 603
    SDataGroupInfo gi = {0};
    gi.pPageList = taosArrayInit(100, sizeof(int32_t));
    taosHashPut(pInfo->pGroupSet, pInfo->keyBuf, len, &gi, sizeof(SDataGroupInfo));

    p = taosHashGet(pInfo->pGroupSet, pInfo->keyBuf, len);

    int32_t pageId = 0;
604
    pPage = getNewBufPage(pInfo->pBuf, &pageId);
605 606 607
    if (pPage == NULL) {
      return pPage;
    }
H
Haojun Liao 已提交
608

609
    taosArrayPush(p->pPageList, &pageId);
610
    *(int32_t*)pPage = 0;
H
Haojun Liao 已提交
611 612 613
  } else {
    int32_t* curId = taosArrayGetLast(p->pPageList);
    pPage = getBufPage(pInfo->pBuf, *curId);
614 615 616 617
    if (pPage == NULL) {
      qError("failed to get buffer, code:%s", tstrerror(terrno));
      return pPage;
    }
H
Haojun Liao 已提交
618

619
    int32_t* rows = (int32_t*)pPage;
H
Haojun Liao 已提交
620
    if (*rows >= pInfo->rowCapacity) {
621 622 623
      // release buffer
      releaseBufPage(pInfo->pBuf, pPage);

H
Haojun Liao 已提交
624 625
      // add a new page for current group
      int32_t pageId = 0;
626
      pPage = getNewBufPage(pInfo->pBuf, &pageId);
627 628 629 630 631
      if (pPage == NULL) {
        qError("failed to get new buffer, code:%s", tstrerror(terrno));
        return NULL;
      }

H
Haojun Liao 已提交
632
      taosArrayPush(p->pPageList, &pageId);
633
      memset(pPage, 0, getBufPageSize(pInfo->pBuf));
H
Haojun Liao 已提交
634 635
    }
  }
H
Haojun Liao 已提交
636

H
Haojun Liao 已提交
637 638 639 640 641 642 643 644 645 646 647 648 649 650
  *pGroupInfo = p;
  return pPage;
}

uint64_t calcGroupId(char* pData, int32_t len) {
  T_MD5_CTX context;
  tMD5Init(&context);
  tMD5Update(&context, (uint8_t*)pData, len);
  tMD5Final(&context);

  // NOTE: only extract the initial 8 bytes of the final MD5 digest
  uint64_t id = 0;
  memcpy(&id, context.digest, sizeof(uint64_t));
  return id;
H
Haojun Liao 已提交
651 652
}

H
Haojun Liao 已提交
653
int32_t* setupColumnOffset(const SSDataBlock* pBlock, int32_t rowCapacity) {
654
  size_t   numOfCols = taosArrayGetSize(pBlock->pDataBlock);
655
  int32_t* offset = taosMemoryCalloc(numOfCols, sizeof(int32_t));
H
Haojun Liao 已提交
656

657 658
  offset[0] = sizeof(int32_t) +
              sizeof(uint64_t);  // the number of rows in current page, ref to SSDataBlock paged serialization format
H
Haojun Liao 已提交
659

660
  for (int32_t i = 0; i < numOfCols - 1; ++i) {
H
Haojun Liao 已提交
661 662 663 664
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);

    int32_t bytes = pColInfoData->info.bytes;
    int32_t payloadLen = bytes * rowCapacity;
665

H
Haojun Liao 已提交
666 667 668 669 670 671 672 673 674 675 676 677
    if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
      // offset segment + content length + payload
      offset[i + 1] = rowCapacity * sizeof(int32_t) + sizeof(int32_t) + payloadLen + offset[i];
    } else {
      // bitmap + content length + payload
      offset[i + 1] = BitmapLen(rowCapacity) + sizeof(int32_t) + payloadLen + offset[i];
    }
  }

  return offset;
}

5
54liuyao 已提交
678
static void clearPartitionOperator(SPartitionOperatorInfo* pInfo) {
679 680 681 682
  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
  for (int32_t i = 0; i < size; i++) {
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
    taosArrayDestroy(pGp->pPageList);
5
54liuyao 已提交
683
  }
684
  taosArrayClear(pInfo->sortedGroupArray);
5
54liuyao 已提交
685 686 687
  clearDiskbasedBuf(pInfo->pBuf);
}

688 689 690
static int compareDataGroupInfo(const void* group1, const void* group2) {
  const SDataGroupInfo* pGroupInfo1 = group1;
  const SDataGroupInfo* pGroupInfo2 = group2;
691 692 693 694 695

  if (pGroupInfo1->groupId == pGroupInfo2->groupId) {
    return 0;
  }

696
  return (pGroupInfo1->groupId < pGroupInfo2->groupId) ? -1 : 1;
697 698
}

H
Haojun Liao 已提交
699 700
static SSDataBlock* buildPartitionResult(SOperatorInfo* pOperator) {
  SPartitionOperatorInfo* pInfo = pOperator->info;
dengyihao's avatar
dengyihao 已提交
701 702
  SExecTaskInfo*          pTaskInfo = pOperator->pTaskInfo;

703 704
  SDataGroupInfo* pGroupInfo =
      (pInfo->groupIndex != -1) ? taosArrayGet(pInfo->sortedGroupArray, pInfo->groupIndex) : NULL;
705
  if (pInfo->groupIndex == -1 || pInfo->pageIndex >= taosArrayGetSize(pGroupInfo->pPageList)) {
H
Haojun Liao 已提交
706
    // try next group data
707 708
    ++pInfo->groupIndex;
    if (pInfo->groupIndex >= taosArrayGetSize(pInfo->sortedGroupArray)) {
H
Haojun Liao 已提交
709
      setOperatorCompleted(pOperator);
5
54liuyao 已提交
710
      clearPartitionOperator(pInfo);
H
Haojun Liao 已提交
711 712 713
      return NULL;
    }

714
    pGroupInfo = taosArrayGet(pInfo->sortedGroupArray, pInfo->groupIndex);
H
Haojun Liao 已提交
715 716 717 718
    pInfo->pageIndex = 0;
  }

  int32_t* pageId = taosArrayGet(pGroupInfo->pPageList, pInfo->pageIndex);
719
  void*    page = getBufPage(pInfo->pBuf, *pageId);
720 721 722 723
  if (page == NULL) {
    qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
    T_LONG_JMP(pTaskInfo->env, terrno);
  }
dengyihao's avatar
dengyihao 已提交
724

725
  blockDataEnsureCapacity(pInfo->binfo.pRes, pInfo->rowCapacity);
H
Haojun Liao 已提交
726
  blockDataFromBuf1(pInfo->binfo.pRes, page, pInfo->rowCapacity);
H
Haojun Liao 已提交
727 728

  pInfo->pageIndex += 1;
729
  releaseBufPage(pInfo->pBuf, page);
H
Haojun Liao 已提交
730

731
  pInfo->binfo.pRes->info.dataLoad = 1;
732
  blockDataUpdateTsWindow(pInfo->binfo.pRes, 0);
H
Haojun Liao 已提交
733
  pInfo->binfo.pRes->info.id.groupId = pGroupInfo->groupId;
734 735

  pOperator->resultInfo.totalRows += pInfo->binfo.pRes->info.rows;
H
Haojun Liao 已提交
736 737 738
  return pInfo->binfo.pRes;
}

739
static SSDataBlock* hashPartition(SOperatorInfo* pOperator) {
H
Haojun Liao 已提交
740 741
  if (pOperator->status == OP_EXEC_DONE) {
    return NULL;
742 743
  }

744 745 746
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;

  SPartitionOperatorInfo* pInfo = pOperator->info;
747
  SSDataBlock*            pRes = pInfo->binfo.pRes;
748

H
Haojun Liao 已提交
749
  if (pOperator->status == OP_RES_TO_RETURN) {
H
Haojun Liao 已提交
750 751
    blockDataCleanup(pRes);
    return buildPartitionResult(pOperator);
H
Haojun Liao 已提交
752 753
  }

754
  int64_t        st = taosGetTimestampUs();
H
Haojun Liao 已提交
755
  SOperatorInfo* downstream = pOperator->pDownstream[0];
H
Haojun Liao 已提交
756

H
Haojun Liao 已提交
757
  while (1) {
758
    SSDataBlock* pBlock = downstream->fpSet.getNextFn(downstream);
H
Haojun Liao 已提交
759 760 761
    if (pBlock == NULL) {
      break;
    }
H
Haojun Liao 已提交
762

763
    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
764
    if (pInfo->scalarSup.pExprInfo != NULL) {
765 766
      pTaskInfo->code = projectApplyFunctions(pInfo->scalarSup.pExprInfo, pBlock, pBlock, pInfo->scalarSup.pCtx,
                                              pInfo->scalarSup.numOfExprs, NULL);
767
      if (pTaskInfo->code != TSDB_CODE_SUCCESS) {
768
        T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
769 770 771
      }
    }

wmmhello's avatar
wmmhello 已提交
772
    terrno = TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
773
    doHashPartition(pOperator, pBlock);
wmmhello's avatar
wmmhello 已提交
774
    if (terrno != TSDB_CODE_SUCCESS) {  // group by json error
775
      T_LONG_JMP(pTaskInfo->env, terrno);
wmmhello's avatar
wmmhello 已提交
776
    }
H
Haojun Liao 已提交
777 778
  }

779
  SArray* groupArray = taosArrayInit(taosHashGetSize(pInfo->pGroupSet), sizeof(SDataGroupInfo));
780 781

  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
782 783 784 785 786 787 788 789 790 791 792
  while (pGroupIter != NULL) {
    SDataGroupInfo* pGroupInfo = pGroupIter;
    taosArrayPush(groupArray, pGroupInfo);
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
  }

  taosArraySort(groupArray, compareDataGroupInfo);
  pInfo->sortedGroupArray = groupArray;
  pInfo->groupIndex = -1;
  taosHashClear(pInfo->pGroupSet);

793 794
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;

H
Haojun Liao 已提交
795
  pOperator->status = OP_RES_TO_RETURN;
H
Haojun Liao 已提交
796 797 798 799
  blockDataEnsureCapacity(pRes, 4096);
  return buildPartitionResult(pOperator);
}

800
static void destroyPartitionOperatorInfo(void* param) {
H
Haojun Liao 已提交
801
  SPartitionOperatorInfo* pInfo = (SPartitionOperatorInfo*)param;
802
  cleanupBasicInfo(&pInfo->binfo);
H
Haojun Liao 已提交
803
  taosArrayDestroy(pInfo->pGroupCols);
804

805
  for (int i = 0; i < taosArrayGetSize(pInfo->pGroupColVals); i++) {
wmmhello's avatar
wmmhello 已提交
806 807 808
    SGroupKeys key = *(SGroupKeys*)taosArrayGet(pInfo->pGroupColVals, i);
    taosMemoryFree(key.pData);
  }
809

H
Haojun Liao 已提交
810
  taosArrayDestroy(pInfo->pGroupColVals);
H
Haojun Liao 已提交
811
  taosMemoryFree(pInfo->keyBuf);
dengyihao's avatar
dengyihao 已提交
812 813 814 815 816 817

  int32_t size = taosArrayGetSize(pInfo->sortedGroupArray);
  for (int32_t i = 0; i < size; i++) {
    SDataGroupInfo* pGp = taosArrayGet(pInfo->sortedGroupArray, i);
    taosArrayDestroy(pGp->pPageList);
  }
818
  taosArrayDestroy(pInfo->sortedGroupArray);
D
dapan1121 已提交
819 820 821 822 823 824 825 826

  void* pGroupIter = taosHashIterate(pInfo->pGroupSet, NULL);
  while (pGroupIter != NULL) {
    SDataGroupInfo* pGroupInfo = pGroupIter;
    taosArrayDestroy(pGroupInfo->pPageList);
    pGroupIter = taosHashIterate(pInfo->pGroupSet, pGroupIter);
  }

wmmhello's avatar
wmmhello 已提交
827
  taosHashCleanup(pInfo->pGroupSet);
H
Haojun Liao 已提交
828
  taosMemoryFree(pInfo->columnOffset);
829

830
  cleanupExprSupp(&pInfo->scalarSup);
H
Haojun Liao 已提交
831
  destroyDiskbasedBuf(pInfo->pBuf);
D
dapan1121 已提交
832
  taosMemoryFreeClear(param);
H
Haojun Liao 已提交
833 834
}

835 836
SOperatorInfo* createPartitionOperatorInfo(SOperatorInfo* downstream, SPartitionPhysiNode* pPartNode,
                                           SExecTaskInfo* pTaskInfo) {
H
Haojun Liao 已提交
837
  SPartitionOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SPartitionOperatorInfo));
838
  SOperatorInfo*          pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
H
Haojun Liao 已提交
839
  if (pInfo == NULL || pOperator == NULL) {
dengyihao's avatar
dengyihao 已提交
840 841
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    pTaskInfo->code = terrno;
H
Haojun Liao 已提交
842 843
    goto _error;
  }
844

845
  int32_t    numOfCols = 0;
846 847 848 849
  SExprInfo* pExprInfo = createExprInfo(pPartNode->pTargets, NULL, &numOfCols);
  pInfo->pGroupCols = extractPartitionColInfo(pPartNode->pPartitionKeys);

  if (pPartNode->pExprs != NULL) {
850
    int32_t    num = 0;
851
    SExprInfo* pExprInfo1 = createExprInfo(pPartNode->pExprs, NULL, &num);
852
    int32_t    code = initExprSupp(&pInfo->scalarSup, pExprInfo1, num, &pTaskInfo->storageAPI.functionStore);
853
    if (code != TSDB_CODE_SUCCESS) {
dengyihao's avatar
dengyihao 已提交
854 855
      terrno = code;
      pTaskInfo->code = terrno;
856 857
      goto _error;
    }
858
  }
H
Haojun Liao 已提交
859 860 861 862

  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
  pInfo->pGroupSet = taosHashInit(100, hashFn, false, HASH_NO_LOCK);
  if (pInfo->pGroupSet == NULL) {
dengyihao's avatar
dengyihao 已提交
863 864
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    pTaskInfo->code = terrno;
H
Haojun Liao 已提交
865 866 867
    goto _error;
  }

868
  uint32_t defaultPgsz = 0;
869
  uint32_t defaultBufsz = 0;
H
Haojun Liao 已提交
870

H
Haojun Liao 已提交
871
  pInfo->binfo.pRes = createDataBlockFromDescNode(pPartNode->node.pOutputDataBlockDesc);
D
dapan1121 已提交
872 873 874 875 876 877
  int32_t code = getBufferPgSize(pInfo->binfo.pRes->info.rowSize, &defaultPgsz, &defaultBufsz);
  if (code != TSDB_CODE_SUCCESS) {
    terrno = code;
    pTaskInfo->code = code;
    goto _error;
  }
878

wafwerar's avatar
wafwerar 已提交
879
  if (!osTempSpaceAvailable()) {
880
    terrno = TSDB_CODE_NO_DISKSPACE;
wafwerar's avatar
wafwerar 已提交
881
    pTaskInfo->code = terrno;
882
    qError("Create partition operator info failed since %s, tempDir:%s", terrstr(), tsTempDir);
wafwerar's avatar
wafwerar 已提交
883 884
    goto _error;
  }
H
Haojun Liao 已提交
885

D
dapan1121 已提交
886
  code = createDiskbasedBuf(&pInfo->pBuf, defaultPgsz, defaultBufsz, pTaskInfo->id.str, tsTempDir);
H
Haojun Liao 已提交
887
  if (code != TSDB_CODE_SUCCESS) {
dengyihao's avatar
dengyihao 已提交
888 889
    terrno = code;
    pTaskInfo->code = code;
H
Haojun Liao 已提交
890 891 892
    goto _error;
  }

G
Ganlin Zhao 已提交
893 894
  pInfo->rowCapacity = blockDataGetCapacityInRow(pInfo->binfo.pRes, getBufPageSize(pInfo->pBuf),
                                                 blockDataGetSerialMetaSize(taosArrayGetSize(pInfo->binfo.pRes->pDataBlock)));
H
Haojun Liao 已提交
895
  pInfo->columnOffset = setupColumnOffset(pInfo->binfo.pRes, pInfo->rowCapacity);
896
  code = initGroupOptrInfo(&pInfo->pGroupColVals, &pInfo->groupKeyLen, &pInfo->keyBuf, pInfo->pGroupCols);
H
Haojun Liao 已提交
897
  if (code != TSDB_CODE_SUCCESS) {
dengyihao's avatar
dengyihao 已提交
898 899
    terrno = code;
    pTaskInfo->code = code;
H
Haojun Liao 已提交
900 901
    goto _error;
  }
H
Haojun Liao 已提交
902

L
Liu Jicong 已提交
903 904
  setOperatorInfo(pOperator, "PartitionOperator", QUERY_NODE_PHYSICAL_PLAN_PARTITION, false, OP_NOT_OPENED, pInfo,
                  pTaskInfo);
905 906
  pOperator->exprSupp.numOfExprs = numOfCols;
  pOperator->exprSupp.pExprInfo = pExprInfo;
907

dengyihao's avatar
dengyihao 已提交
908 909
  pOperator->fpSet =
      createOperatorFpSet(optrDummyOpenFn, hashPartition, NULL, destroyPartitionOperatorInfo, optrDefaultBufFn, NULL);
H
Haojun Liao 已提交
910

H
Haojun Liao 已提交
911
  code = appendDownstream(pOperator, &downstream, 1);
dengyihao's avatar
dengyihao 已提交
912 913 914 915 916 917
  if (code != TSDB_CODE_SUCCESS) {
    terrno = code;
    pTaskInfo->code = code;
    goto _error;
  }

918 919
  return pOperator;

920
_error:
H
Haojun Liao 已提交
921 922 923
  if (pInfo != NULL) {
    destroyPartitionOperatorInfo(pInfo);
  }
H
Haojun Liao 已提交
924
  taosMemoryFreeClear(pOperator);
925
  return NULL;
926 927
}

928 929 930
int32_t setGroupResultOutputBuf(SOperatorInfo* pOperator, SOptrBasicInfo* binfo, int32_t numOfCols, char* pData,
                                int16_t bytes, uint64_t groupId, SDiskbasedBuf* pBuf, SAggSupporter* pAggSup) {
  SExecTaskInfo*  pTaskInfo = pOperator->pTaskInfo;
931
  SResultRowInfo* pResultRowInfo = &binfo->resultRowInfo;
932
  SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
933 934

  SResultRow* pResultRow =
D
dapan1121 已提交
935
      doSetResultOutBufByKey(pBuf, pResultRowInfo, (char*)pData, bytes, true, groupId, pTaskInfo, false, pAggSup, false);
936

937
  setResultRowInitCtx(pResultRow, pCtx, numOfCols, pOperator->exprSupp.rowEntryInfoOffset);
938
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
939
}
940 941 942

uint64_t calGroupIdByData(SPartitionBySupporter* pParSup, SExprSupp* pExprSup, SSDataBlock* pBlock, int32_t rowId) {
  if (pExprSup->pExprInfo != NULL) {
943 944
    int32_t code =
        projectApplyFunctions(pExprSup->pExprInfo, pBlock, pBlock, pExprSup->pCtx, pExprSup->numOfExprs, NULL);
945 946 947 948 949
    if (code != TSDB_CODE_SUCCESS) {
      qError("calaculate group id error, code:%d", code);
    }
  }
  recordNewGroupKeys(pParSup->pGroupCols, pParSup->pGroupColVals, pBlock, rowId);
950
  int32_t  len = buildGroupKeys(pParSup->keyBuf, pParSup->pGroupColVals);
951 952 953 954
  uint64_t groupId = calcGroupId(pParSup->keyBuf, len);
  return groupId;
}

955
static bool hasRemainPartion(SStreamPartitionOperatorInfo* pInfo) { return pInfo->parIte != NULL; }
5
54liuyao 已提交
956
static bool hasRemainTbName(SStreamPartitionOperatorInfo* pInfo) { return pInfo->pTbNameIte != NULL; }
957 958

static SSDataBlock* buildStreamPartitionResult(SOperatorInfo* pOperator) {
959 960
  SStorageAPI* pAPI = &pOperator->pTaskInfo->storageAPI;

961
  SStreamPartitionOperatorInfo* pInfo = pOperator->info;
962
  SSDataBlock*                  pDest = pInfo->binfo.pRes;
963
  ASSERT(hasRemainPartion(pInfo));
964 965
  SPartitionDataInfo* pParInfo = (SPartitionDataInfo*)pInfo->parIte;
  blockDataCleanup(pDest);
966
  int32_t      rows = taosArrayGetSize(pParInfo->rowIds);
967 968 969 970
  SSDataBlock* pSrc = pInfo->pInputDataBlock;
  for (int32_t i = 0; i < rows; i++) {
    int32_t rowIndex = *(int32_t*)taosArrayGet(pParInfo->rowIds, i);
    for (int32_t j = 0; j < pOperator->exprSupp.numOfExprs; j++) {
971
      int32_t          slotId = pOperator->exprSupp.pExprInfo[j].base.pParam[0].pCol->slotId;
972 973
      SColumnInfoData* pSrcCol = taosArrayGet(pSrc->pDataBlock, slotId);
      SColumnInfoData* pDestCol = taosArrayGet(pDest->pDataBlock, j);
974 975
      bool             isNull = colDataIsNull(pSrcCol, pSrc->info.rows, rowIndex, NULL);
      char*            pSrcData = colDataGetData(pSrcCol, rowIndex);
976
      colDataSetVal(pDestCol, pDest->info.rows, pSrcData, isNull);
977 978
    }
    pDest->info.rows++;
5
54liuyao 已提交
979 980 981 982
  }
  pDest->info.parTbName[0] = 0;
  if (pInfo->tbnameCalSup.numOfExprs > 0) {
    void* tbname = NULL;
983
    if (pAPI->stateStore.streamStateGetParName(pOperator->pTaskInfo->streamInfo.pState, pParInfo->groupId, &tbname) == 0) {
5
54liuyao 已提交
984
      memcpy(pDest->info.parTbName, tbname, TSDB_TABLE_NAME_LEN);
985
      pAPI->stateStore.streamStateFreeVal(tbname);
986
    }
987
  }
988 989
  taosArrayDestroy(pParInfo->rowIds);
  pParInfo->rowIds = NULL;
H
Haojun Liao 已提交
990 991
  pDest->info.dataLoad = 1;

992
  blockDataUpdateTsWindow(pDest, pInfo->tsColIndex);
H
Haojun Liao 已提交
993
  pDest->info.id.groupId = pParInfo->groupId;
994 995
  pOperator->resultInfo.totalRows += pDest->info.rows;
  pInfo->parIte = taosHashIterate(pInfo->pPartitions, pInfo->parIte);
996
  ASSERT(pDest->info.rows > 0);
997 998 999 1000
  printDataBlock(pDest, "stream partitionby");
  return pDest;
}

1001 1002
void appendCreateTableRow(void* pState, SExprSupp* pTableSup, SExprSupp* pTagSup, uint64_t groupId,
                          SSDataBlock* pSrcBlock, int32_t rowId, SSDataBlock* pDestBlock, SStateStore* pAPI) {
5
54liuyao 已提交
1003
  void* pValue = NULL;
1004
  if (pAPI->streamStateGetParName(pState, groupId, &pValue) != 0) {
5
54liuyao 已提交
1005
    SSDataBlock* pTmpBlock = blockCopyOneRow(pSrcBlock, rowId);
5
54liuyao 已提交
1006 1007
    memset(pTmpBlock->info.parTbName, 0, TSDB_TABLE_NAME_LEN);
    pTmpBlock->info.id.groupId = groupId;
5
54liuyao 已提交
1008
    char* tbName = pSrcBlock->info.parTbName;
5
54liuyao 已提交
1009 1010 1011 1012
    if (pTableSup->numOfExprs > 0) {
      projectApplyFunctions(pTableSup->pExprInfo, pDestBlock, pTmpBlock, pTableSup->pCtx, pTableSup->numOfExprs, NULL);
      SColumnInfoData* pTbCol = taosArrayGet(pDestBlock->pDataBlock, UD_TABLE_NAME_COLUMN_INDEX);
      memset(tbName, 0, TSDB_TABLE_NAME_LEN);
5
54liuyao 已提交
1013 1014
      int32_t len = 0;
      if (colDataIsNull_s(pTbCol, pDestBlock->info.rows - 1)) {
5
54liuyao 已提交
1015 1016
        len = 1;
        tbName[0] = 0;
5
54liuyao 已提交
1017 1018 1019 1020
      } else {
        void* pData = colDataGetData(pTbCol, pDestBlock->info.rows - 1);
        len = TMIN(varDataLen(pData), TSDB_TABLE_NAME_LEN - 1);
        memcpy(tbName, varDataVal(pData), len);
1021
        pAPI->streamStatePutParName(pState, groupId, tbName);
5
54liuyao 已提交
1022
      }
5
54liuyao 已提交
1023
      memcpy(pTmpBlock->info.parTbName, tbName, len);
5
54liuyao 已提交
1024 1025 1026
      pDestBlock->info.rows--;
    } else {
      void* pTbNameCol = taosArrayGet(pDestBlock->pDataBlock, UD_TABLE_NAME_COLUMN_INDEX);
X
Xiaoyu Wang 已提交
1027
      colDataSetNULL(pTbNameCol, pDestBlock->info.rows);
5
54liuyao 已提交
1028
      tbName[0] = 0;
5
54liuyao 已提交
1029 1030 1031 1032 1033
    }

    if (pTagSup->numOfExprs > 0) {
      projectApplyFunctions(pTagSup->pExprInfo, pDestBlock, pTmpBlock, pTagSup->pCtx, pTagSup->numOfExprs, NULL);
      pDestBlock->info.rows--;
5
54liuyao 已提交
1034 1035
    } else {
      memcpy(pDestBlock->info.parTbName, pTmpBlock->info.parTbName, TSDB_TABLE_NAME_LEN);
5
54liuyao 已提交
1036 1037 1038 1039 1040 1041
    }

    void* pGpIdCol = taosArrayGet(pDestBlock->pDataBlock, UD_GROUPID_COLUMN_INDEX);
    colDataAppend(pGpIdCol, pDestBlock->info.rows, (const char*)&groupId, false);
    pDestBlock->info.rows++;
    blockDataDestroy(pTmpBlock);
5
54liuyao 已提交
1042 1043
  } else {
    memcpy(pSrcBlock->info.parTbName, pValue, TSDB_TABLE_NAME_LEN);
5
54liuyao 已提交
1044
  }
1045
  pAPI->streamStateReleaseBuf(pState, NULL, pValue);
5
54liuyao 已提交
1046 1047 1048
}

static SSDataBlock* buildStreamCreateTableResult(SOperatorInfo* pOperator) {
1049 1050
  SExecTaskInfo* pTask = pOperator->pTaskInfo;

5
54liuyao 已提交
1051
  SStreamPartitionOperatorInfo* pInfo = pOperator->info;
1052 1053
  if ((pInfo->tbnameCalSup.numOfExprs == 0 && pInfo->tagCalSup.numOfExprs == 0) ||
      taosHashGetSize(pInfo->pPartitions) == 0) {
5
54liuyao 已提交
1054 1055 1056 1057 1058 1059
    return NULL;
  }
  blockDataCleanup(pInfo->pCreateTbRes);
  blockDataEnsureCapacity(pInfo->pCreateTbRes, taosHashGetSize(pInfo->pPartitions));
  SSDataBlock* pSrc = pInfo->pInputDataBlock;

5
54liuyao 已提交
1060
  if (pInfo->pTbNameIte != NULL) {
5
54liuyao 已提交
1061
    SPartitionDataInfo* pParInfo = (SPartitionDataInfo*)pInfo->pTbNameIte;
1062
    int32_t             rowId = *(int32_t*)taosArrayGet(pParInfo->rowIds, 0);
1063 1064
    appendCreateTableRow(pTask->streamInfo.pState, &pInfo->tbnameCalSup, &pInfo->tagCalSup,
                         pParInfo->groupId, pSrc, rowId, pInfo->pCreateTbRes, &pTask->storageAPI.stateStore);
5
54liuyao 已提交
1065 1066 1067 1068 1069
    pInfo->pTbNameIte = taosHashIterate(pInfo->pPartitions, pInfo->pTbNameIte);
  }
  return pInfo->pCreateTbRes->info.rows > 0 ? pInfo->pCreateTbRes : NULL;
}

1070 1071 1072 1073
static void doStreamHashPartitionImpl(SStreamPartitionOperatorInfo* pInfo, SSDataBlock* pBlock) {
  pInfo->pInputDataBlock = pBlock;
  for (int32_t i = 0; i < pBlock->info.rows; ++i) {
    recordNewGroupKeys(pInfo->partitionSup.pGroupCols, pInfo->partitionSup.pGroupColVals, pBlock, i);
1074 1075 1076
    int32_t             keyLen = buildGroupKeys(pInfo->partitionSup.keyBuf, pInfo->partitionSup.pGroupColVals);
    SPartitionDataInfo* pParData =
        (SPartitionDataInfo*)taosHashGet(pInfo->pPartitions, pInfo->partitionSup.keyBuf, keyLen);
1077 1078 1079 1080 1081 1082 1083
    if (pParData) {
      taosArrayPush(pParData->rowIds, &i);
    } else {
      SPartitionDataInfo newParData = {0};
      newParData.groupId = calcGroupId(pInfo->partitionSup.keyBuf, keyLen);
      newParData.rowIds = taosArrayInit(64, sizeof(int32_t));
      taosArrayPush(newParData.rowIds, &i);
1084
      taosHashPut(pInfo->pPartitions, pInfo->partitionSup.keyBuf, keyLen, &newParData, sizeof(SPartitionDataInfo));
1085 1086 1087 1088 1089 1090 1091 1092 1093
    }
  }
}

static SSDataBlock* doStreamHashPartition(SOperatorInfo* pOperator) {
  if (pOperator->status == OP_EXEC_DONE) {
    return NULL;
  }

1094
  SExecTaskInfo*                pTaskInfo = pOperator->pTaskInfo;
1095
  SStreamPartitionOperatorInfo* pInfo = pOperator->info;
5
54liuyao 已提交
1096 1097 1098 1099 1100 1101 1102 1103 1104
  SSDataBlock*                  pCtRes = NULL;

  if (hasRemainTbName(pInfo)) {
    pCtRes = buildStreamCreateTableResult(pOperator);
    if (pCtRes != NULL) {
      return pCtRes;
    }
  }

1105 1106 1107 1108
  if (hasRemainPartion(pInfo)) {
    return buildStreamPartitionResult(pOperator);
  }

1109
  int64_t        st = taosGetTimestampUs();
1110 1111 1112 1113 1114
  SOperatorInfo* downstream = pOperator->pDownstream[0];
  {
    pInfo->pInputDataBlock = NULL;
    SSDataBlock* pBlock = downstream->fpSet.getNextFn(downstream);
    if (pBlock == NULL) {
H
Haojun Liao 已提交
1115
      setOperatorCompleted(pOperator);
1116 1117 1118 1119 1120 1121 1122 1123 1124
      return NULL;
    }
    printDataBlock(pBlock, "stream partitionby recv");
    switch (pBlock->info.type) {
      case STREAM_NORMAL:
      case STREAM_PULL_DATA:
      case STREAM_INVALID:
        pInfo->binfo.pRes->info.type = pBlock->info.type;
        break;
1125 1126 1127
      case STREAM_DELETE_DATA: {
        copyDataBlock(pInfo->pDelRes, pBlock);
        pInfo->pDelRes->info.type = STREAM_DELETE_RESULT;
5
54liuyao 已提交
1128
        printDataBlock(pInfo->pDelRes, "stream partitionby delete");
1129
        return pInfo->pDelRes;
1130
      } break;
1131
      default:
L
liuyao 已提交
1132
        ASSERTS(pBlock->info.type == STREAM_CREATE_CHILD_TABLE || pBlock->info.type == STREAM_RETRIEVE, "invalid SSDataBlock type");
1133 1134 1135 1136 1137
        return pBlock;
    }

    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
    if (pInfo->scalarSup.pExprInfo != NULL) {
5
54liuyao 已提交
1138
      projectApplyFunctions(pInfo->scalarSup.pExprInfo, pBlock, pBlock, pInfo->scalarSup.pCtx,
dengyihao's avatar
dengyihao 已提交
1139
                            pInfo->scalarSup.numOfExprs, NULL);
1140 1141 1142 1143 1144
    }
    taosHashClear(pInfo->pPartitions);
    doStreamHashPartitionImpl(pInfo, pBlock);
  }
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
1145

1146
  pInfo->parIte = taosHashIterate(pInfo->pPartitions, NULL);
5
54liuyao 已提交
1147 1148 1149 1150 1151
  pInfo->pTbNameIte = taosHashIterate(pInfo->pPartitions, NULL);
  pCtRes = buildStreamCreateTableResult(pOperator);
  if (pCtRes != NULL) {
    return pCtRes;
  }
1152 1153 1154 1155 1156 1157 1158 1159
  return buildStreamPartitionResult(pOperator);
}

static void destroyStreamPartitionOperatorInfo(void* param) {
  SStreamPartitionOperatorInfo* pInfo = (SStreamPartitionOperatorInfo*)param;
  cleanupBasicInfo(&pInfo->binfo);
  taosArrayDestroy(pInfo->partitionSup.pGroupCols);

1160
  for (int i = 0; i < taosArrayGetSize(pInfo->partitionSup.pGroupColVals); i++) {
1161 1162 1163 1164 1165 1166 1167
    SGroupKeys key = *(SGroupKeys*)taosArrayGet(pInfo->partitionSup.pGroupColVals, i);
    taosMemoryFree(key.pData);
  }
  taosArrayDestroy(pInfo->partitionSup.pGroupColVals);

  taosMemoryFree(pInfo->partitionSup.keyBuf);
  cleanupExprSupp(&pInfo->scalarSup);
L
Liu Jicong 已提交
1168 1169
  cleanupExprSupp(&pInfo->tbnameCalSup);
  cleanupExprSupp(&pInfo->tagCalSup);
1170
  blockDataDestroy(pInfo->pDelRes);
1171
  taosHashCleanup(pInfo->pPartitions);
5
54liuyao 已提交
1172
  blockDataDestroy(pInfo->pCreateTbRes);
1173 1174 1175 1176
  taosMemoryFreeClear(param);
}

void initParDownStream(SOperatorInfo* downstream, SPartitionBySupporter* pParSup, SExprSupp* pExpr) {
1177 1178
  SStorageAPI* pAPI = &downstream->pTaskInfo->storageAPI;

1179 1180 1181
  if (downstream->operatorType != QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) {
    return;
  }
1182

1183 1184 1185
  SStreamScanInfo* pScanInfo = downstream->info;
  pScanInfo->partitionSup = *pParSup;
  pScanInfo->pPartScalarSup = pExpr;
5
54liuyao 已提交
1186
  if (!pScanInfo->igCheckUpdate && !pScanInfo->pUpdateInfo) {
1187
    pScanInfo->pUpdateInfo = pAPI->stateStore.updateInfoInit(60000, TSDB_TIME_PRECISION_MILLI, 0);
5
54liuyao 已提交
1188
  }
1189 1190
}

5
54liuyao 已提交
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
SSDataBlock* buildCreateTableBlock(SExprSupp* tbName, SExprSupp* tag) {
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
  pBlock->info.hasVarCol = false;
  pBlock->info.id.groupId = 0;
  pBlock->info.rows = 0;
  pBlock->info.type = STREAM_CREATE_CHILD_TABLE;
  pBlock->info.watermark = INT64_MIN;

  pBlock->pDataBlock = taosArrayInit(4, sizeof(SColumnInfoData));
  SColumnInfoData infoData = {0};
  infoData.info.type = TSDB_DATA_TYPE_VARCHAR;
  if (tbName->numOfExprs > 0) {
    infoData.info.bytes = tbName->pExprInfo->base.resSchema.bytes;
  } else {
    infoData.info.bytes = 1;
  }
  pBlock->info.rowSize += infoData.info.bytes;
  // sub table name
  taosArrayPush(pBlock->pDataBlock, &infoData);

  SColumnInfoData gpIdData = {0};
  gpIdData.info.type = TSDB_DATA_TYPE_UBIGINT;
  gpIdData.info.bytes = 8;
  pBlock->info.rowSize += gpIdData.info.bytes;
  // group id
  taosArrayPush(pBlock->pDataBlock, &gpIdData);

  for (int32_t i = 0; i < tag->numOfExprs; i++) {
    SColumnInfoData tagCol = {0};
    tagCol.info.type = tag->pExprInfo[i].base.resSchema.type;
    tagCol.info.bytes = tag->pExprInfo[i].base.resSchema.bytes;
    tagCol.info.precision = tag->pExprInfo[i].base.resSchema.precision;
    // tag info
    taosArrayPush(pBlock->pDataBlock, &tagCol);
    pBlock->info.rowSize += tagCol.info.bytes;
  }

  return pBlock;
}

L
liuyao 已提交
1231 1232 1233 1234 1235
void freePartItem(void* ptr) {
  SPartitionDataInfo* pPart = (SPartitionDataInfo*)ptr;
  taosArrayDestroy(pPart->rowIds);
}

1236 1237
SOperatorInfo* createStreamPartitionOperatorInfo(SOperatorInfo* downstream, SStreamPartitionPhysiNode* pPartNode,
                                                 SExecTaskInfo* pTaskInfo) {
1238
  int32_t                       code = TSDB_CODE_SUCCESS;
1239 1240 1241
  SStreamPartitionOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SStreamPartitionOperatorInfo));
  SOperatorInfo*                pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
  if (pInfo == NULL || pOperator == NULL) {
1242
    code = TSDB_CODE_OUT_OF_MEMORY;
1243 1244
    goto _error;
  }
1245

1246
  pInfo->partitionSup.pGroupCols = extractPartitionColInfo(pPartNode->part.pPartitionKeys);
1247

1248
  if (pPartNode->part.pExprs != NULL) {
1249
    int32_t    num = 0;
1250
    SExprInfo* pCalExprInfo = createExprInfo(pPartNode->part.pExprs, NULL, &num);
1251
    code = initExprSupp(&pInfo->scalarSup, pCalExprInfo, num, &pTaskInfo->storageAPI.functionStore);
1252 1253 1254 1255 1256
    if (code != TSDB_CODE_SUCCESS) {
      goto _error;
    }
  }

5
54liuyao 已提交
1257
  pInfo->tbnameCalSup.numOfExprs = 0;
1258 1259 1260 1261 1262 1263 1264 1265
  if (pPartNode->pSubtable != NULL) {
    SExprInfo* pSubTableExpr = taosMemoryCalloc(1, sizeof(SExprInfo));
    if (pSubTableExpr == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _error;
    }
    pInfo->tbnameCalSup.pExprInfo = pSubTableExpr;
    createExprFromOneNode(pSubTableExpr, pPartNode->pSubtable, 0);
1266
    code = initExprSupp(&pInfo->tbnameCalSup, pSubTableExpr, 1, &pTaskInfo->storageAPI.functionStore);
1267 1268 1269 1270 1271
    if (code != TSDB_CODE_SUCCESS) {
      goto _error;
    }
  }

5
54liuyao 已提交
1272
  pInfo->tagCalSup.numOfExprs = 0;
L
Liu Jicong 已提交
1273 1274
  if (pPartNode->pTags != NULL) {
    int32_t    numOfTags;
5
54liuyao 已提交
1275
    SExprInfo* pTagExpr = createExpr(pPartNode->pTags, &numOfTags);
L
Liu Jicong 已提交
1276
    if (pTagExpr == NULL) {
1277
      code = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
1278 1279
      goto _error;
    }
1280
    if (initExprSupp(&pInfo->tagCalSup, pTagExpr, numOfTags, &pTaskInfo->storageAPI.functionStore) != 0) {
1281
      code = TSDB_CODE_OUT_OF_MEMORY;
L
Liu Jicong 已提交
1282 1283 1284 1285
      goto _error;
    }
  }

5
54liuyao 已提交
1286 1287 1288 1289 1290 1291
  if (pInfo->tbnameCalSup.numOfExprs != 0 || pInfo->tagCalSup.numOfExprs != 0) {
    pInfo->pCreateTbRes = buildCreateTableBlock(&pInfo->tbnameCalSup, &pInfo->tagCalSup);
  } else {
    pInfo->pCreateTbRes = NULL;
  }

1292
  int32_t keyLen = 0;
1293 1294
  code = initGroupOptrInfo(&pInfo->partitionSup.pGroupColVals, &keyLen, &pInfo->partitionSup.keyBuf,
                           pInfo->partitionSup.pGroupCols);
1295 1296 1297 1298 1299
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }
  pInfo->partitionSup.needCalc = true;

H
Haojun Liao 已提交
1300
  pInfo->binfo.pRes = createDataBlockFromDescNode(pPartNode->part.node.pOutputDataBlockDesc);
1301
  if (pInfo->binfo.pRes == NULL) {
1302
    code = TSDB_CODE_OUT_OF_MEMORY;
1303 1304
    goto _error;
  }
1305 1306 1307

  blockDataEnsureCapacity(pInfo->binfo.pRes, 4096);

1308
  pInfo->parIte = NULL;
5
54liuyao 已提交
1309
  pInfo->pTbNameIte = NULL;
1310
  pInfo->pInputDataBlock = NULL;
1311

1312
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
1313
  pInfo->pPartitions = taosHashInit(1024, hashFn, false, HASH_NO_LOCK);
L
liuyao 已提交
1314
  taosHashSetFreeFp(pInfo->pPartitions, freePartItem);
1315 1316
  pInfo->tsColIndex = 0;
  pInfo->pDelRes = createSpecialDataBlock(STREAM_DELETE_RESULT);
1317

1318
  int32_t    numOfCols = 0;
1319
  SExprInfo* pExprInfo = createExprInfo(pPartNode->part.pTargets, NULL, &numOfCols);
1320

L
Liu Jicong 已提交
1321 1322
  setOperatorInfo(pOperator, "StreamPartitionOperator", QUERY_NODE_PHYSICAL_PLAN_STREAM_PARTITION, false, OP_NOT_OPENED,
                  pInfo, pTaskInfo);
1323 1324
  pOperator->exprSupp.numOfExprs = numOfCols;
  pOperator->exprSupp.pExprInfo = pExprInfo;
dengyihao's avatar
dengyihao 已提交
1325 1326
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, doStreamHashPartition, NULL,
                                         destroyStreamPartitionOperatorInfo, optrDefaultBufFn, NULL);
1327 1328 1329 1330 1331

  initParDownStream(downstream, &pInfo->partitionSup, &pInfo->scalarSup);
  code = appendDownstream(pOperator, &downstream, 1);
  return pOperator;

1332
_error:
1333
  pTaskInfo->code = code;
1334
  destroyStreamPartitionOperatorInfo(pInfo);
1335 1336 1337
  taosMemoryFreeClear(pOperator);
  return NULL;
}
H
Haojun Liao 已提交
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370

SArray* extractColumnInfo(SNodeList* pNodeList) {
  size_t  numOfCols = LIST_LENGTH(pNodeList);
  SArray* pList = taosArrayInit(numOfCols, sizeof(SColumn));
  if (pList == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

  for (int32_t i = 0; i < numOfCols; ++i) {
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pNodeList, i);

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

      SColumn c = extractColumnFromColumnNode(pColNode);
      taosArrayPush(pList, &c);
    } else if (nodeType(pNode->pExpr) == QUERY_NODE_VALUE) {
      SValueNode* pValNode = (SValueNode*)pNode->pExpr;
      SColumn     c = {0};
      c.slotId = pNode->slotId;
      c.colId = pNode->slotId;
      c.type = pValNode->node.type;
      c.bytes = pValNode->node.resType.bytes;
      c.scale = pValNode->node.resType.scale;
      c.precision = pValNode->node.resType.precision;

      taosArrayPush(pList, &c);
    }
  }

  return pList;
}