• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #3646

12 Mar 2025 12:34PM UTC coverage: 28.375% (-27.8%) from 56.156%
#3646

push

travis-ci

web-flow
Merge pull request #30119 from taosdata/ciup30

ci: Update workflow to fix param issue of run_tdgpt_test

59085 of 286935 branches covered (20.59%)

Branch coverage included in aggregate %.

102775 of 283490 relevant lines covered (36.25%)

55149.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/source/libs/executor/src/streamcountwindowoperator.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15
#include "executorInt.h"
16
#include "function.h"
17
#include "functionMgt.h"
18
#include "operator.h"
19
#include "querytask.h"
20
#include "streamexecutorInt.h"
21
#include "tchecksum.h"
22
#include "tcommon.h"
23
#include "tdatablock.h"
24
#include "tglobal.h"
25
#include "tlog.h"
26
#include "ttime.h"
27

28
#define STREAM_COUNT_OP_STATE_NAME      "StreamCountHistoryState"
29
#define STREAM_COUNT_OP_CHECKPOINT_NAME "StreamCountOperator_Checkpoint"
30

31
typedef struct SCountWindowInfo {
32
  SResultWindowInfo winInfo;
33
  COUNT_TYPE*       pWindowCount;
34
} SCountWindowInfo;
35

36
typedef enum {
37
  NONE_WINDOW = 0,
38
  CREATE_NEW_WINDOW,
39
  MOVE_NEXT_WINDOW,
40
} BuffOp;
41
typedef struct SBuffInfo {
42
  bool             rebuildWindow;
43
  BuffOp           winBuffOp;
44
  SStreamStateCur* pCur;
45
} SBuffInfo;
46

47
void destroyStreamCountAggOperatorInfo(void* param) {
×
48
  if (param == NULL) {
×
49
    return;
×
50
  }
51
  SStreamCountAggOperatorInfo* pInfo = (SStreamCountAggOperatorInfo*)param;
×
52
  cleanupBasicInfo(&pInfo->binfo);
×
53
  if (pInfo->pOperator) {
×
54
    cleanupResultInfoInStream(pInfo->pOperator->pTaskInfo, pInfo->streamAggSup.pState, &pInfo->pOperator->exprSupp,
×
55
                              &pInfo->groupResInfo);
56
    pInfo->pOperator = NULL;
×
57
  }
58

59
  destroyStreamBasicInfo(&pInfo->basic);
×
60
  cleanupExprSupp(&pInfo->scalarSupp);
×
61
  clearGroupResInfo(&pInfo->groupResInfo);
×
62
  taosArrayDestroyP(pInfo->pUpdated, destroyFlusedPos);
×
63
  pInfo->pUpdated = NULL;
×
64
  destroyStreamAggSupporter(&pInfo->streamAggSup);
×
65

66
  colDataDestroy(&pInfo->twAggSup.timeWindowData);
×
67
  blockDataDestroy(pInfo->pDelRes);
×
68
  tSimpleHashCleanup(pInfo->pStUpdated);
×
69
  tSimpleHashCleanup(pInfo->pStDeleted);
×
70
  cleanupGroupResInfo(&pInfo->groupResInfo);
×
71

72
  taosArrayDestroy(pInfo->historyWins);
×
73
  blockDataDestroy(pInfo->pCheckpointRes);
×
74

75
  tSimpleHashCleanup(pInfo->pPkDeleted);
×
76

77
  taosMemoryFreeClear(param);
×
78
}
79

80
bool isSlidingCountWindow(SStreamAggSupporter* pAggSup) { return pAggSup->windowCount != pAggSup->windowSliding; }
×
81

82
int32_t setCountOutputBuf(SStreamAggSupporter* pAggSup, TSKEY ts, uint64_t groupId, SCountWindowInfo* pCurWin,
×
83
                          SBuffInfo* pBuffInfo, int32_t* pWinCode) {
84
  int32_t code = TSDB_CODE_SUCCESS;
×
85
  int32_t lino = 0;
×
86
  int32_t size = pAggSup->resultRowSize;
×
87
  pCurWin->winInfo.sessionWin.groupId = groupId;
×
88
  pCurWin->winInfo.sessionWin.win.skey = ts;
×
89
  pCurWin->winInfo.sessionWin.win.ekey = ts;
×
90

91
  if (isSlidingCountWindow(pAggSup)) {
×
92
    if (pBuffInfo->winBuffOp == CREATE_NEW_WINDOW) {
×
93
      code =
94
          pAggSup->stateStore.streamStateCountWinAdd(pAggSup->pState, &pCurWin->winInfo.sessionWin,
×
95
                                                     pAggSup->windowCount, (void**)&pCurWin->winInfo.pStatePos, &size);
×
96
      QUERY_CHECK_CODE(code, lino, _end);
×
97

98
      *pWinCode = TSDB_CODE_FAILED;
×
99
    } else if (pBuffInfo->winBuffOp == MOVE_NEXT_WINDOW) {
×
100
      QUERY_CHECK_NULL(pBuffInfo->pCur, code, lino, _end, terrno);
×
101
      pAggSup->stateStore.streamStateCurNext(pAggSup->pState, pBuffInfo->pCur);
×
102
      *pWinCode = pAggSup->stateStore.streamStateSessionGetKVByCur(pBuffInfo->pCur, &pCurWin->winInfo.sessionWin,
×
103
                                                                   (void**)&pCurWin->winInfo.pStatePos, &size);
×
104
      if (*pWinCode == TSDB_CODE_FAILED) {
×
105
        code = pAggSup->stateStore.streamStateCountWinAdd(pAggSup->pState, &pCurWin->winInfo.sessionWin,
×
106
                                                          pAggSup->windowCount, (void**)&pCurWin->winInfo.pStatePos,
×
107
                                                          &size);
108
        QUERY_CHECK_CODE(code, lino, _end);
×
109
      } else {
110
        reuseOutputBuf(pAggSup->pState, pCurWin->winInfo.pStatePos, &pAggSup->stateStore);
×
111
      }
112
    } else {
113
      pBuffInfo->pCur = pAggSup->stateStore.streamStateCountSeekKeyPrev(pAggSup->pState, &pCurWin->winInfo.sessionWin,
×
114
                                                                        pAggSup->windowCount);
×
115
      *pWinCode = pAggSup->stateStore.streamStateSessionGetKVByCur(pBuffInfo->pCur, &pCurWin->winInfo.sessionWin,
×
116
                                                                   (void**)&pCurWin->winInfo.pStatePos, &size);
×
117
      if (*pWinCode == TSDB_CODE_FAILED) {
×
118
        code = pAggSup->stateStore.streamStateCountWinAdd(pAggSup->pState, &pCurWin->winInfo.sessionWin,
×
119
                                                          pAggSup->windowCount, (void**)&pCurWin->winInfo.pStatePos,
×
120
                                                          &size);
121
        QUERY_CHECK_CODE(code, lino, _end);
×
122
      } else {
123
        reuseOutputBuf(pAggSup->pState, pCurWin->winInfo.pStatePos, &pAggSup->stateStore);
×
124
      }
125
    }
126
    if (ts < pCurWin->winInfo.sessionWin.win.ekey) {
×
127
      pBuffInfo->rebuildWindow = true;
×
128
    }
129
  } else {
130
    code = pAggSup->stateStore.streamStateCountWinAddIfNotExist(pAggSup->pState, &pCurWin->winInfo.sessionWin,
×
131
                                                                pAggSup->windowCount,
×
132
                                                                (void**)&pCurWin->winInfo.pStatePos, &size, pWinCode);
×
133
    QUERY_CHECK_CODE(code, lino, _end);
×
134
  }
135

136
  if (*pWinCode == TSDB_CODE_SUCCESS) {
×
137
    pCurWin->winInfo.isOutput = true;
×
138
  }
139
  pCurWin->pWindowCount =
×
140
      (COUNT_TYPE*)((char*)pCurWin->winInfo.pStatePos->pRowBuff + (pAggSup->resultRowSize - sizeof(COUNT_TYPE)));
×
141

142
  if (*pCurWin->pWindowCount == pAggSup->windowCount) {
×
143
    pBuffInfo->rebuildWindow = true;
×
144
  }
145

146
_end:
×
147
  if (code != TSDB_CODE_SUCCESS) {
×
148
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
149
  }
150
  return code;
×
151
}
152

153
static void removeCountResult(SSHashObj* pHashMap, SSHashObj* pResMap, SSessionKey* pKey) {
×
154
  SSessionKey key = {0};
×
155
  getSessionHashKey(pKey, &key);
×
156
  int32_t code = tSimpleHashRemove(pHashMap, &key, sizeof(SSessionKey));
×
157
  if (code != TSDB_CODE_SUCCESS) {
×
158
    qInfo("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
159
  }
160

161
  code = tSimpleHashRemove(pResMap, &key, sizeof(SSessionKey));
×
162
  if (code != TSDB_CODE_SUCCESS) {
×
163
    qInfo("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
164
  }
165
}
×
166

167
static int32_t updateCountWindowInfo(SStreamAggSupporter* pAggSup, SCountWindowInfo* pWinInfo, TSKEY* pTs,
×
168
                                     int32_t start, int32_t rows, int32_t maxRows, SSHashObj* pStUpdated,
169
                                     SSHashObj* pStDeleted, bool* pRebuild, int32_t* pWinRows) {
170
  int32_t     code = TSDB_CODE_SUCCESS;
×
171
  int32_t     lino = 0;
×
172
  SSessionKey sWinKey = pWinInfo->winInfo.sessionWin;
×
173
  int32_t     num = 0;
×
174
  for (int32_t i = start; i < rows; i++) {
×
175
    if (pTs[i] < pWinInfo->winInfo.sessionWin.win.ekey) {
×
176
      num++;
×
177
    } else {
178
      break;
×
179
    }
180
  }
181
  int32_t maxNum = TMIN(maxRows - *pWinInfo->pWindowCount, rows - start);
×
182
  if (num > maxNum) {
×
183
    *pRebuild = true;
×
184
  }
185
  *pWinInfo->pWindowCount += maxNum;
×
186
  bool needDelState = false;
×
187
  if (pWinInfo->winInfo.sessionWin.win.skey > pTs[start]) {
×
188
    needDelState = true;
×
189
    if (pStDeleted && pWinInfo->winInfo.isOutput) {
×
190
      code = saveDeleteRes(pStDeleted, pWinInfo->winInfo.sessionWin);
×
191
      QUERY_CHECK_CODE(code, lino, _end);
×
192
    }
193

194
    pWinInfo->winInfo.sessionWin.win.skey = pTs[start];
×
195
  }
196

197
  if (pWinInfo->winInfo.sessionWin.win.ekey < pTs[maxNum + start - 1]) {
×
198
    needDelState = true;
×
199
    pWinInfo->winInfo.sessionWin.win.ekey = pTs[maxNum + start - 1];
×
200
  }
201

202
  if (needDelState) {
×
203
    memcpy(pWinInfo->winInfo.pStatePos->pKey, &pWinInfo->winInfo.sessionWin, sizeof(SSessionKey));
×
204
    removeCountResult(pStUpdated, pAggSup->pResultRows, &sWinKey);
×
205
    if (pWinInfo->winInfo.pStatePos->needFree) {
×
206
      pAggSup->stateStore.streamStateSessionDel(pAggSup->pState, &sWinKey);
×
207
    }
208
  }
209

210
  (*pWinRows) = maxNum;
×
211

212
_end:
×
213
  if (code != TSDB_CODE_SUCCESS) {
×
214
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
215
  }
216
  return code;
×
217
}
218

219
void getCountWinRange(SStreamAggSupporter* pAggSup, const SSessionKey* pKey, EStreamType mode, SSessionKey* pDelRange) {
×
220
  *pDelRange = *pKey;
×
221
  SStreamStateCur* pCur = NULL;
×
222
  if (isSlidingCountWindow(pAggSup)) {
×
223
    pCur = pAggSup->stateStore.streamStateCountSeekKeyPrev(pAggSup->pState, pKey, pAggSup->windowCount);
×
224
  } else {
225
    pCur = pAggSup->stateStore.streamStateSessionSeekKeyCurrentNext(pAggSup->pState, pKey);
×
226
  }
227
  SSessionKey tmpKey = {.groupId = pKey->groupId, .win.ekey = INT64_MIN, .win.skey = INT64_MIN};
×
228
  int32_t     code = pAggSup->stateStore.streamStateSessionGetKVByCur(pCur, &tmpKey, NULL, 0);
×
229
  if (code != TSDB_CODE_SUCCESS) {
×
230
    pAggSup->stateStore.streamStateFreeCur(pCur);
×
231
    return;
×
232
  }
233
  pDelRange->win = tmpKey.win;
×
234
  while (mode == STREAM_DELETE_DATA || mode == STREAM_PARTITION_DELETE_DATA) {
×
235
    pAggSup->stateStore.streamStateCurNext(pAggSup->pState, pCur);
×
236
    code = pAggSup->stateStore.streamStateSessionGetKVByCur(pCur, &tmpKey, NULL, 0);
×
237
    if (code != TSDB_CODE_SUCCESS) {
×
238
      break;
×
239
    }
240
    pDelRange->win.ekey = TMAX(pDelRange->win.ekey, tmpKey.win.ekey);
×
241
  }
242
  pAggSup->stateStore.streamStateFreeCur(pCur);
×
243
}
244

245
static void destroySBuffInfo(SStreamAggSupporter* pAggSup, SBuffInfo* pBuffInfo) {
×
246
  pAggSup->stateStore.streamStateFreeCur(pBuffInfo->pCur);
×
247
}
×
248

249
bool inCountCalSlidingWindow(SStreamAggSupporter* pAggSup, STimeWindow* pWin, TSKEY sKey, TSKEY eKey) {
×
250
  if (pAggSup->windowCount == pAggSup->windowSliding) {
×
251
    return true;
×
252
  }
253
  if (sKey <= pWin->skey && pWin->ekey <= eKey) {
×
254
    return true;
×
255
  }
256
  return false;
×
257
}
258

259
bool inCountSlidingWindow(SStreamAggSupporter* pAggSup, STimeWindow* pWin, SDataBlockInfo* pBlockInfo) {
×
260
  return inCountCalSlidingWindow(pAggSup, pWin, pBlockInfo->calWin.skey, pBlockInfo->calWin.ekey);
×
261
}
262

263
static void doStreamCountAggImpl(SOperatorInfo* pOperator, SSDataBlock* pSDataBlock, SSHashObj* pStUpdated,
×
264
                                 SSHashObj* pStDeleted) {
265
  int32_t                      code = TSDB_CODE_SUCCESS;
×
266
  int32_t                      lino = 0;
×
267
  SExecTaskInfo*               pTaskInfo = pOperator->pTaskInfo;
×
268
  SStreamCountAggOperatorInfo* pInfo = pOperator->info;
×
269
  int32_t                      numOfOutput = pOperator->exprSupp.numOfExprs;
×
270
  uint64_t                     groupId = pSDataBlock->info.id.groupId;
×
271
  SResultRow*                  pResult = NULL;
×
272
  int32_t                      rows = pSDataBlock->info.rows;
×
273
  int32_t                      winRows = 0;
×
274
  SStreamAggSupporter*         pAggSup = &pInfo->streamAggSup;
×
275
  SBuffInfo                    buffInfo = {.rebuildWindow = false, .winBuffOp = NONE_WINDOW, .pCur = NULL};
×
276

277
  pInfo->dataVersion = TMAX(pInfo->dataVersion, pSDataBlock->info.version);
×
278
  pAggSup->winRange = pTaskInfo->streamInfo.fillHistoryWindow;
×
279
  if (pAggSup->winRange.ekey <= 0) {
×
280
    pAggSup->winRange.ekey = INT64_MAX;
×
281
  }
282

283
  SColumnInfoData* pStartTsCol = taosArrayGet(pSDataBlock->pDataBlock, pInfo->primaryTsIndex);
×
284
  if (!pStartTsCol) {
×
285
    code = TSDB_CODE_FAILED;
×
286
    QUERY_CHECK_CODE(code, lino, _end);
×
287
  }
288
  TSKEY* startTsCols = (int64_t*)pStartTsCol->pData;
×
289
  code = blockDataEnsureCapacity(pAggSup->pScanBlock, rows * 2);
×
290
  QUERY_CHECK_CODE(code, lino, _end);
×
291

292
  SStreamStateCur* pCur = NULL;
×
293
  COUNT_TYPE       slidingRows = 0;
×
294

295
  for (int32_t i = 0; i < rows;) {
×
296
    if (pInfo->ignoreExpiredData &&
×
297
        checkExpiredData(&pInfo->streamAggSup.stateStore, pInfo->streamAggSup.pUpdateInfo, &pInfo->twAggSup,
×
298
                         pSDataBlock->info.id.uid, startTsCols[i], NULL, 0)) {
×
299
      i++;
×
300
      continue;
×
301
    }
302
    SCountWindowInfo curWin = {0};
×
303
    int32_t          winCode = TSDB_CODE_SUCCESS;
×
304
    buffInfo.rebuildWindow = false;
×
305
    code = setCountOutputBuf(pAggSup, startTsCols[i], groupId, &curWin, &buffInfo, &winCode);
×
306
    QUERY_CHECK_CODE(code, lino, _end);
×
307

308
    if (winCode != TSDB_CODE_SUCCESS &&
×
309
        BIT_FLAG_TEST_MASK(pTaskInfo->streamInfo.eventTypes, SNOTIFY_EVENT_WINDOW_OPEN)) {
×
310
      code = addCountAggNotifyEvent(SNOTIFY_EVENT_WINDOW_OPEN, &curWin.winInfo.sessionWin, &pInfo->basic.notifyEventSup,
×
311
                                    pTaskInfo->streamInfo.pNotifyEventStat);
312
      QUERY_CHECK_CODE(code, lino, _end);
×
313
    }
314

315
    if (!inCountSlidingWindow(pAggSup, &curWin.winInfo.sessionWin.win, &pSDataBlock->info)) {
×
316
      buffInfo.winBuffOp = MOVE_NEXT_WINDOW;
×
317
      continue;
×
318
    }
319
    setSessionWinOutputInfo(pStUpdated, &curWin.winInfo);
×
320
    slidingRows = *curWin.pWindowCount;
×
321
    if (!buffInfo.rebuildWindow) {
×
322
      code = updateCountWindowInfo(pAggSup, &curWin, startTsCols, i, rows, pAggSup->windowCount, pStUpdated, pStDeleted,
×
323
                                   &buffInfo.rebuildWindow, &winRows);
324
      QUERY_CHECK_CODE(code, lino, _end);
×
325
    }
326
    if (buffInfo.rebuildWindow) {
×
327
      SSessionKey range = {0};
×
328
      if (isSlidingCountWindow(pAggSup)) {
×
329
        curWin.winInfo.sessionWin.win.skey = startTsCols[i];
×
330
        curWin.winInfo.sessionWin.win.ekey = startTsCols[i];
×
331
      }
332
      getCountWinRange(pAggSup, &curWin.winInfo.sessionWin, STREAM_DELETE_DATA, &range);
×
333
      range.win.skey = TMIN(startTsCols[i], range.win.skey);
×
334
      range.win.ekey = TMAX(startTsCols[rows - 1], range.win.ekey);
×
335
      uint64_t uid = 0;
×
336
      code =
337
          appendDataToSpecialBlock(pAggSup->pScanBlock, &range.win.skey, &range.win.ekey, &uid, &range.groupId, NULL);
×
338
      QUERY_CHECK_CODE(code, lino, _end);
×
339
      break;
×
340
    }
341
    code = doOneWindowAggImpl(&pInfo->twAggSup.timeWindowData, &curWin.winInfo, &pResult, i, winRows, rows, numOfOutput,
×
342
                              pOperator, 0);
343
    QUERY_CHECK_CODE(code, lino, _end);
×
344

345
    code = saveSessionOutputBuf(pAggSup, &curWin.winInfo);
×
346
    QUERY_CHECK_CODE(code, lino, _end);
×
347

348
    if (pInfo->destHasPrimaryKey && curWin.winInfo.isOutput && IS_NORMAL_COUNT_OP(pOperator)) {
×
349
      code = saveDeleteRes(pInfo->pPkDeleted, curWin.winInfo.sessionWin);
×
350
      QUERY_CHECK_CODE(code, lino, _end);
×
351
    }
352

353
    if (pInfo->twAggSup.calTrigger == STREAM_TRIGGER_AT_ONCE && pStUpdated) {
×
354
      code = saveResult(curWin.winInfo, pStUpdated);
×
355
      QUERY_CHECK_CODE(code, lino, _end);
×
356
    }
357
    if (pInfo->twAggSup.calTrigger == STREAM_TRIGGER_WINDOW_CLOSE) {
×
358
      curWin.winInfo.pStatePos->beUpdated = true;
×
359
      SSessionKey key = {0};
×
360
      getSessionHashKey(&curWin.winInfo.sessionWin, &key);
×
361
      code =
362
          tSimpleHashPut(pAggSup->pResultRows, &key, sizeof(SSessionKey), &curWin.winInfo, sizeof(SResultWindowInfo));
×
363
      QUERY_CHECK_CODE(code, lino, _end);
×
364
    }
365

366
    if (isSlidingCountWindow(pAggSup)) {
×
367
      if (slidingRows <= pAggSup->windowSliding) {
×
368
        if (slidingRows + winRows > pAggSup->windowSliding) {
×
369
          buffInfo.winBuffOp = CREATE_NEW_WINDOW;
×
370
          winRows = pAggSup->windowSliding - slidingRows;
×
371
        }
372
      } else {
373
        buffInfo.winBuffOp = MOVE_NEXT_WINDOW;
×
374
        winRows = 0;
×
375
      }
376
    }
377
    i += winRows;
×
378
  }
379

380
_end:
×
381
  if (code != TSDB_CODE_SUCCESS) {
×
382
    qError("%s failed at line %d since %s. task:%s", __func__, lino, tstrerror(code), GET_TASKID(pTaskInfo));
×
383
  }
384
  destroySBuffInfo(pAggSup, &buffInfo);
×
385
}
×
386

387
static int32_t buildCountResult(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
×
388
  int32_t                      code = TSDB_CODE_SUCCESS;
×
389
  int32_t                      lino = 0;
×
390
  SStreamCountAggOperatorInfo* pInfo = pOperator->info;
×
391
  SStreamAggSupporter*         pAggSup = &pInfo->streamAggSup;
×
392
  SOptrBasicInfo*              pBInfo = &pInfo->binfo;
×
393
  SExecTaskInfo*               pTaskInfo = pOperator->pTaskInfo;
×
394
  SStreamNotifyEventSupp*      pNotifySup = &pInfo->basic.notifyEventSup;
×
395
  STaskNotifyEventStat*        pNotifyEventStat = pTaskInfo->streamInfo.pNotifyEventStat;
×
396
  bool                         addNotifyEvent = false;
×
397
  addNotifyEvent = BIT_FLAG_TEST_MASK(pTaskInfo->streamInfo.eventTypes, SNOTIFY_EVENT_WINDOW_CLOSE);
×
398
  doBuildDeleteDataBlock(pOperator, pInfo->pStDeleted, pInfo->pDelRes, &pInfo->pDelIterator, &pInfo->groupResInfo);
×
399
  if (pInfo->pDelRes->info.rows > 0) {
×
400
    printDataBlock(pInfo->pDelRes, getStreamOpName(pOperator->operatorType), GET_TASKID(pTaskInfo));
×
401
    if (addNotifyEvent) {
×
402
      code = addAggDeleteNotifyEvent(pInfo->pDelRes, pNotifySup, pNotifyEventStat);
×
403
      QUERY_CHECK_CODE(code, lino, _end);
×
404
    }
405
    (*ppRes) = pInfo->pDelRes;
×
406
    return code;
×
407
  }
408

409
  doBuildSessionResult(pOperator, pAggSup->pState, &pInfo->groupResInfo, pBInfo->pRes,
×
410
                       addNotifyEvent ? pNotifySup->pSessionKeys : NULL);
411
  if (pBInfo->pRes->info.rows > 0) {
×
412
    printDataBlock(pBInfo->pRes, getStreamOpName(pOperator->operatorType), GET_TASKID(pTaskInfo));
×
413
    if (addNotifyEvent) {
×
414
      code = addAggResultNotifyEvent(pBInfo->pRes, pNotifySup->pSessionKeys, pTaskInfo->streamInfo.notifyResultSchema,
×
415
                                     pNotifySup, pNotifyEventStat);
416
      QUERY_CHECK_CODE(code, lino, _end);
×
417
    }
418
    (*ppRes) = pBInfo->pRes;
×
419
    return code;
×
420
  }
421

422
  code = buildNotifyEventBlock(pTaskInfo, pNotifySup, pNotifyEventStat);
×
423
  QUERY_CHECK_CODE(code, lino, _end);
×
424
  if (pNotifySup->pEventBlock && pNotifySup->pEventBlock->info.rows > 0) {
×
425
    printDataBlock(pNotifySup->pEventBlock, getStreamOpName(pOperator->operatorType), GET_TASKID(pTaskInfo));
×
426
    (*ppRes) = pNotifySup->pEventBlock;
×
427
    return code;
×
428
  }
429

430
  code = removeOutdatedNotifyEvents(&pInfo->twAggSup, pNotifySup, pNotifyEventStat);
×
431
  QUERY_CHECK_CODE(code, lino, _end);
×
432

433
_end:
×
434
  if (code != TSDB_CODE_SUCCESS) {
×
435
    qError("%s failed at line %d since %s. task:%s", __func__, lino, tstrerror(code), GET_TASKID(pTaskInfo));
×
436
  }
437
  (*ppRes) = NULL;
×
438
  return code;
×
439
}
440

441
int32_t doStreamCountEncodeOpState(void** buf, int32_t len, SOperatorInfo* pOperator, bool isParent) {
×
442
  SStreamCountAggOperatorInfo* pInfo = pOperator->info;
×
443
  if (!pInfo) {
×
444
    return 0;
×
445
  }
446

447
  void* pData = (buf == NULL) ? NULL : *buf;
×
448

449
  // 1.streamAggSup.pResultRows
450
  int32_t tlen = 0;
×
451
  int32_t mapSize = tSimpleHashGetSize(pInfo->streamAggSup.pResultRows);
×
452
  tlen += taosEncodeFixedI32(buf, mapSize);
×
453
  void*   pIte = NULL;
×
454
  size_t  keyLen = 0;
×
455
  int32_t iter = 0;
×
456
  while ((pIte = tSimpleHashIterate(pInfo->streamAggSup.pResultRows, pIte, &iter)) != NULL) {
×
457
    void* key = tSimpleHashGetKey(pIte, &keyLen);
×
458
    tlen += encodeSSessionKey(buf, key);
×
459
    tlen += encodeSResultWindowInfo(buf, pIte, pInfo->streamAggSup.resultRowSize);
×
460
  }
461

462
  // 2.twAggSup
463
  tlen += encodeSTimeWindowAggSupp(buf, &pInfo->twAggSup);
×
464

465
  // 3.dataVersion
466
  tlen += taosEncodeFixedI32(buf, pInfo->dataVersion);
×
467

468
  // 4.basicInfo
469
  tlen += encodeStreamBasicInfo(buf, &pInfo->basic);
×
470

471
  // 5.checksum
472
  if (isParent) {
×
473
    if (buf) {
×
474
      uint32_t cksum = taosCalcChecksum(0, pData, len - sizeof(uint32_t));
×
475
      tlen += taosEncodeFixedU32(buf, cksum);
×
476
    } else {
477
      tlen += sizeof(uint32_t);
×
478
    }
479
  }
480

481
  return tlen;
×
482
}
483

484
int32_t doStreamCountDecodeOpState(void* buf, int32_t len, SOperatorInfo* pOperator, bool isParent) {
×
485
  int32_t                      code = TSDB_CODE_SUCCESS;
×
486
  int32_t                      lino = 0;
×
487
  SStreamCountAggOperatorInfo* pInfo = pOperator->info;
×
488
  SExecTaskInfo*               pTaskInfo = pOperator->pTaskInfo;
×
489
  void*                        pDataEnd = POINTER_SHIFT(buf, len);
×
490
  if (!pInfo) {
×
491
    code = TSDB_CODE_FAILED;
×
492
    QUERY_CHECK_CODE(code, lino, _end);
×
493
  }
494

495
  // 5.checksum
496
  if (isParent) {
×
497
    int32_t dataLen = len - sizeof(uint32_t);
×
498
    void*   pCksum = POINTER_SHIFT(buf, dataLen);
×
499
    if (taosCheckChecksum(buf, dataLen, *(uint32_t*)pCksum) != TSDB_CODE_SUCCESS) {
×
500
      code = TSDB_CODE_FAILED;
×
501
      QUERY_CHECK_CODE(code, lino, _end);
×
502
    }
503
    pDataEnd = pCksum;
×
504
  }
505

506
  // 1.streamAggSup.pResultRows
507
  int32_t mapSize = 0;
×
508
  buf = taosDecodeFixedI32(buf, &mapSize);
×
509
  for (int32_t i = 0; i < mapSize; i++) {
×
510
    SSessionKey      key = {0};
×
511
    SCountWindowInfo curWin = {0};
×
512
    int32_t          winCode = TSDB_CODE_SUCCESS;
×
513
    buf = decodeSSessionKey(buf, &key);
×
514
    SBuffInfo buffInfo = {.rebuildWindow = false, .winBuffOp = NONE_WINDOW, .pCur = NULL};
×
515
    code = setCountOutputBuf(&pInfo->streamAggSup, key.win.skey, key.groupId, &curWin, &buffInfo, &winCode);
×
516
    QUERY_CHECK_CODE(code, lino, _end);
×
517

518
    buf = decodeSResultWindowInfo(buf, &curWin.winInfo, pInfo->streamAggSup.resultRowSize);
×
519
    code = tSimpleHashPut(pInfo->streamAggSup.pResultRows, &key, sizeof(SSessionKey), &curWin.winInfo,
×
520
                          sizeof(SResultWindowInfo));
521
    QUERY_CHECK_CODE(code, lino, _end);
×
522
  }
523

524
  // 2.twAggSup
525
  buf = decodeSTimeWindowAggSupp(buf, &pInfo->twAggSup);
×
526

527
  // 3.dataVersion
528
  buf = taosDecodeFixedI64(buf, &pInfo->dataVersion);
×
529

530
  // 4.basicInfo
531
  if (buf < pDataEnd) {
×
532
    code = decodeStreamBasicInfo(&buf, &pInfo->basic);
×
533
    QUERY_CHECK_CODE(code, lino, _end);
×
534
  }
535

536
_end:
×
537
  if (code != TSDB_CODE_SUCCESS) {
×
538
    qError("%s failed at line %d since %s. task:%s", __func__, lino, tstrerror(code), GET_TASKID(pTaskInfo));
×
539
  }
540
  return code;
×
541
}
542

543
void doStreamCountSaveCheckpoint(SOperatorInfo* pOperator) {
×
544
  int32_t                      code = TSDB_CODE_SUCCESS;
×
545
  int32_t                      lino = 0;
×
546
  void*                        pBuf = NULL;
×
547
  SStreamCountAggOperatorInfo* pInfo = pOperator->info;
×
548
  SExecTaskInfo*               pTaskInfo = pOperator->pTaskInfo;
×
549
  if (needSaveStreamOperatorInfo(&pInfo->basic)) {
×
550
    int32_t len = doStreamCountEncodeOpState(NULL, 0, pOperator, true);
×
551
    pBuf = taosMemoryCalloc(1, len);
×
552
    if (!pBuf) {
×
553
      code = terrno;
×
554
      QUERY_CHECK_CODE(code, lino, _end);
×
555
    }
556
    void* pTmpBuf = pBuf;
×
557
    len = doStreamCountEncodeOpState(&pTmpBuf, len, pOperator, true);
×
558
    pInfo->streamAggSup.stateStore.streamStateSaveInfo(pInfo->streamAggSup.pState, STREAM_COUNT_OP_CHECKPOINT_NAME,
×
559
                                                       strlen(STREAM_COUNT_OP_CHECKPOINT_NAME), pBuf, len);
560
    saveStreamOperatorStateComplete(&pInfo->basic);
×
561
  }
562

563
_end:
×
564
  taosMemoryFreeClear(pBuf);
×
565
  if (code != TSDB_CODE_SUCCESS) {
×
566
    qError("%s failed at line %d since %s. task:%s", __func__, lino, tstrerror(code), GET_TASKID(pTaskInfo));
×
567
  }
568
}
×
569

570
void doResetCountWindows(SStreamAggSupporter* pAggSup, SSDataBlock* pBlock) {
×
571
  SColumnInfoData* pStartTsCol = taosArrayGet(pBlock->pDataBlock, START_TS_COLUMN_INDEX);
×
572
  TSKEY*           startDatas = (TSKEY*)pStartTsCol->pData;
×
573
  SColumnInfoData* pEndTsCol = taosArrayGet(pBlock->pDataBlock, END_TS_COLUMN_INDEX);
×
574
  TSKEY*           endDatas = (TSKEY*)pEndTsCol->pData;
×
575
  SColumnInfoData* pCalStartTsCol = taosArrayGet(pBlock->pDataBlock, CALCULATE_START_TS_COLUMN_INDEX);
×
576
  TSKEY*           calStartDatas = (TSKEY*)pStartTsCol->pData;
×
577
  SColumnInfoData* pCalEndTsCol = taosArrayGet(pBlock->pDataBlock, CALCULATE_END_TS_COLUMN_INDEX);
×
578
  TSKEY*           calEndDatas = (TSKEY*)pEndTsCol->pData;
×
579
  SColumnInfoData* pGroupCol = taosArrayGet(pBlock->pDataBlock, GROUPID_COLUMN_INDEX);
×
580
  uint64_t*        gpDatas = (uint64_t*)pGroupCol->pData;
×
581

582
  SRowBuffPos* pPos = NULL;
×
583
  int32_t      size = 0;
×
584
  for (int32_t i = 0; i < pBlock->info.rows; i++) {
×
585
    SSessionKey      key = {.groupId = gpDatas[i], .win.skey = startDatas[i], .win.ekey = endDatas[i]};
×
586
    SStreamStateCur* pCur = NULL;
×
587
    if (isSlidingCountWindow(pAggSup)) {
×
588
      pCur = pAggSup->stateStore.streamStateCountSeekKeyPrev(pAggSup->pState, &key, pAggSup->windowCount);
×
589
    } else {
590
      pCur = pAggSup->stateStore.streamStateSessionSeekKeyCurrentNext(pAggSup->pState, &key);
×
591
    }
592
    while (1) {
×
593
      SSessionKey tmpKey = {.groupId = gpDatas[i], .win.skey = INT64_MIN, .win.ekey = INT64_MIN};
×
594
      int32_t     code = pAggSup->stateStore.streamStateSessionGetKVByCur(pCur, &tmpKey, (void**)&pPos, &size);
×
595
      if (code != TSDB_CODE_SUCCESS || tmpKey.win.skey > endDatas[i]) {
×
596
        pAggSup->stateStore.streamStateFreeCur(pCur);
×
597
        break;
×
598
      }
599
      if (!inCountCalSlidingWindow(pAggSup, &tmpKey.win, calStartDatas[i], calEndDatas[i])) {
×
600
        pAggSup->stateStore.streamStateCurNext(pAggSup->pState, pCur);
×
601
        continue;
×
602
      }
603
      pAggSup->stateStore.streamStateSessionReset(pAggSup->pState, pPos->pRowBuff);
×
604
      pAggSup->stateStore.streamStateCurNext(pAggSup->pState, pCur);
×
605
    }
606
  }
607
}
×
608

609
int32_t doDeleteCountWindows(SStreamAggSupporter* pAggSup, SSDataBlock* pBlock, SArray* result) {
×
610
  int32_t          code = TSDB_CODE_SUCCESS;
×
611
  int32_t          lino = 0;
×
612
  SColumnInfoData* pStartTsCol = taosArrayGet(pBlock->pDataBlock, START_TS_COLUMN_INDEX);
×
613
  TSKEY*           startDatas = (TSKEY*)pStartTsCol->pData;
×
614
  SColumnInfoData* pEndTsCol = taosArrayGet(pBlock->pDataBlock, END_TS_COLUMN_INDEX);
×
615
  TSKEY*           endDatas = (TSKEY*)pEndTsCol->pData;
×
616
  SColumnInfoData* pCalStartTsCol = taosArrayGet(pBlock->pDataBlock, CALCULATE_START_TS_COLUMN_INDEX);
×
617
  TSKEY*           calStartDatas = (TSKEY*)pStartTsCol->pData;
×
618
  SColumnInfoData* pCalEndTsCol = taosArrayGet(pBlock->pDataBlock, CALCULATE_END_TS_COLUMN_INDEX);
×
619
  TSKEY*           calEndDatas = (TSKEY*)pEndTsCol->pData;
×
620
  SColumnInfoData* pGroupCol = taosArrayGet(pBlock->pDataBlock, GROUPID_COLUMN_INDEX);
×
621
  uint64_t*        gpDatas = (uint64_t*)pGroupCol->pData;
×
622
  for (int32_t i = 0; i < pBlock->info.rows; i++) {
×
623
    SSessionKey key = {.win.skey = startDatas[i], .win.ekey = endDatas[i], .groupId = gpDatas[i]};
×
624
    while (1) {
×
625
      SSessionKey curWin = {0};
×
626
      int32_t     winCode = pAggSup->stateStore.streamStateCountGetKeyByRange(pAggSup->pState, &key, &curWin);
×
627
      if (winCode != TSDB_CODE_SUCCESS) {
×
628
        break;
×
629
      }
630
      doDeleteSessionWindow(pAggSup, &curWin);
×
631
      if (result) {
×
632
        code = saveDeleteInfo(result, curWin);
×
633
        QUERY_CHECK_CODE(code, lino, _end);
×
634
      }
635
    }
636
  }
637

638
_end:
×
639
  if (code != TSDB_CODE_SUCCESS) {
×
640
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
641
  }
642
  return code;
×
643
}
644

645
int32_t deleteCountWinState(SStreamAggSupporter* pAggSup, SSDataBlock* pBlock, SSHashObj* pMapUpdate,
×
646
                            SSHashObj* pMapDelete, SSHashObj* pPkDelete, bool needAdd) {
647
  int32_t code = TSDB_CODE_SUCCESS;
×
648
  int32_t lino = 0;
×
649
  SArray* pWins = taosArrayInit(16, sizeof(SSessionKey));
×
650
  if (!pWins) {
×
651
    code = terrno;
×
652
    QUERY_CHECK_CODE(code, lino, _end);
×
653
  }
654

655
  if (isSlidingCountWindow(pAggSup)) {
×
656
    code = doDeleteCountWindows(pAggSup, pBlock, pWins);
×
657
    QUERY_CHECK_CODE(code, lino, _end);
×
658
  } else {
659
    code = doDeleteTimeWindows(pAggSup, pBlock, pWins);
×
660
    QUERY_CHECK_CODE(code, lino, _end);
×
661
  }
662
  removeSessionResults(pAggSup, pMapUpdate, pWins);
×
663
  code = copyDeleteWindowInfo(pWins, pMapDelete);
×
664
  QUERY_CHECK_CODE(code, lino, _end);
×
665
  if (needAdd) {
×
666
    code = copyDeleteWindowInfo(pWins, pPkDelete);
×
667
    QUERY_CHECK_CODE(code, lino, _end);
×
668
  }
669
  taosArrayDestroy(pWins);
×
670

671
_end:
×
672
  if (code != TSDB_CODE_SUCCESS) {
×
673
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
674
  }
675
  return code;
×
676
}
677

678
static int32_t doStreamCountAggNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
×
679
  int32_t                      code = TSDB_CODE_SUCCESS;
×
680
  int32_t                      lino = 0;
×
681
  SExprSupp*                   pSup = &pOperator->exprSupp;
×
682
  SStreamCountAggOperatorInfo* pInfo = pOperator->info;
×
683
  SOptrBasicInfo*              pBInfo = &pInfo->binfo;
×
684
  SStreamAggSupporter*         pAggSup = &pInfo->streamAggSup;
×
685
  SExecTaskInfo*               pTaskInfo = pOperator->pTaskInfo;
×
686
  qDebug("stask:%s  %s status: %d", GET_TASKID(pTaskInfo), getStreamOpName(pOperator->operatorType), pOperator->status);
×
687
  if (pOperator->status == OP_EXEC_DONE) {
×
688
    (*ppRes) = NULL;
×
689
    return code;
×
690
  } else if (pOperator->status == OP_RES_TO_RETURN) {
×
691
    SSDataBlock* opRes = NULL;
×
692
    code = buildCountResult(pOperator, &opRes);
×
693
    QUERY_CHECK_CODE(code, lino, _end);
×
694
    if (opRes) {
×
695
      (*ppRes) = opRes;
×
696
      return code;
×
697
    }
698

699
    if (pInfo->recvGetAll) {
×
700
      pInfo->recvGetAll = false;
×
701
      resetUnCloseSessionWinInfo(pInfo->streamAggSup.pResultRows);
×
702
    }
703

704
    if (pInfo->reCkBlock) {
×
705
      pInfo->reCkBlock = false;
×
706
      printDataBlock(pInfo->pCheckpointRes, getStreamOpName(pOperator->operatorType), GET_TASKID(pTaskInfo));
×
707
      (*ppRes) = pInfo->pCheckpointRes;
×
708
      return code;
×
709
    }
710

711
    setStreamOperatorCompleted(pOperator);
×
712
    (*ppRes) = NULL;
×
713
    return code;
×
714
  }
715

716
  SOperatorInfo* downstream = pOperator->pDownstream[0];
×
717
  if (!pInfo->pUpdated) {
×
718
    pInfo->pUpdated = taosArrayInit(16, sizeof(SResultWindowInfo));
×
719
    QUERY_CHECK_NULL(pInfo->pUpdated, code, lino, _end, terrno);
×
720
  }
721
  if (!pInfo->pStUpdated) {
×
722
    _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
×
723
    pInfo->pStUpdated = tSimpleHashInit(64, hashFn);
×
724
    QUERY_CHECK_NULL(pInfo->pStUpdated, code, lino, _end, terrno);
×
725
  }
726
  while (1) {
×
727
    SSDataBlock* pBlock = NULL;
×
728
    code = downstream->fpSet.getNextFn(downstream, &pBlock);
×
729
    QUERY_CHECK_CODE(code, lino, _end);
×
730

731
    if (pBlock == NULL) {
×
732
      break;
×
733
    }
734

735
    printSpecDataBlock(pBlock, getStreamOpName(pOperator->operatorType), "recv", GET_TASKID(pTaskInfo));
×
736
    setStreamOperatorState(&pInfo->basic, pBlock->info.type);
×
737

738
    if (pBlock->info.type == STREAM_DELETE_DATA || pBlock->info.type == STREAM_DELETE_RESULT) {
×
739
      bool add = pInfo->destHasPrimaryKey && IS_NORMAL_COUNT_OP(pOperator);
×
740
      code = deleteCountWinState(&pInfo->streamAggSup, pBlock, pInfo->pStUpdated, pInfo->pStDeleted, pInfo->pPkDeleted,
×
741
                                 add);
742
      QUERY_CHECK_CODE(code, lino, _end);
×
743
      continue;
×
744
    } else if (pBlock->info.type == STREAM_CLEAR) {
×
745
      doResetCountWindows(&pInfo->streamAggSup, pBlock);
×
746
      continue;
×
747
    } else if (pBlock->info.type == STREAM_GET_ALL) {
×
748
      pInfo->recvGetAll = true;
×
749
      code = getAllSessionWindow(pAggSup->pResultRows, pInfo->pStUpdated);
×
750
      QUERY_CHECK_CODE(code, lino, _end);
×
751
      continue;
×
752
    } else if (pBlock->info.type == STREAM_CREATE_CHILD_TABLE) {
×
753
      (*ppRes) = pBlock;
×
754
      return code;
×
755
    } else if (pBlock->info.type == STREAM_CHECKPOINT) {
×
756
      pAggSup->stateStore.streamStateCommit(pAggSup->pState);
×
757
      doStreamCountSaveCheckpoint(pOperator);
×
758
      code = copyDataBlock(pInfo->pCheckpointRes, pBlock);
×
759
      QUERY_CHECK_CODE(code, lino, _end);
×
760
      continue;
×
761
    } else {
762
      if (pBlock->info.type != STREAM_NORMAL && pBlock->info.type != STREAM_INVALID) {
×
763
        code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
764
        QUERY_CHECK_CODE(code, lino, _end);
×
765
      }
766
    }
767

768
    if (pInfo->scalarSupp.pExprInfo != NULL) {
×
769
      SExprSupp* pExprSup = &pInfo->scalarSupp;
×
770
      code = projectApplyFunctions(pExprSup->pExprInfo, pBlock, pBlock, pExprSup->pCtx, pExprSup->numOfExprs, NULL);
×
771
      QUERY_CHECK_CODE(code, lino, _end);
×
772
    }
773
    // the pDataBlock are always the same one, no need to call this again
774
    code = setInputDataBlock(pSup, pBlock, TSDB_ORDER_ASC, MAIN_SCAN, true);
×
775
    QUERY_CHECK_CODE(code, lino, _end);
×
776
    doStreamCountAggImpl(pOperator, pBlock, pInfo->pStUpdated, pInfo->pStDeleted);
×
777
    pInfo->twAggSup.maxTs = TMAX(pInfo->twAggSup.maxTs, pBlock->info.window.ekey);
×
778
    pInfo->twAggSup.maxTs = TMAX(pInfo->twAggSup.maxTs, pBlock->info.watermark);
×
779
  }
780
  // restore the value
781
  pOperator->status = OP_RES_TO_RETURN;
×
782

783
  code = closeSessionWindow(pAggSup->pResultRows, &pInfo->twAggSup, pInfo->pStUpdated);
×
784
  QUERY_CHECK_CODE(code, lino, _end);
×
785

786
  code = copyUpdateResult(&pInfo->pStUpdated, pInfo->pUpdated, sessionKeyCompareAsc);
×
787
  QUERY_CHECK_CODE(code, lino, _end);
×
788

789
  removeSessionDeleteResults(pInfo->pStDeleted, pInfo->pUpdated);
×
790
  initGroupResInfoFromArrayList(&pInfo->groupResInfo, pInfo->pUpdated);
×
791
  pInfo->pUpdated = NULL;
×
792
  code = blockDataEnsureCapacity(pInfo->binfo.pRes, pOperator->resultInfo.capacity);
×
793
  QUERY_CHECK_CODE(code, lino, _end);
×
794

795
  if (pInfo->destHasPrimaryKey && IS_NORMAL_COUNT_OP(pOperator)) {
×
796
    code = copyDeleteSessionKey(pInfo->pPkDeleted, pInfo->pStDeleted);
×
797
    QUERY_CHECK_CODE(code, lino, _end);
×
798
  }
799

800
  SSDataBlock* opRes = NULL;
×
801
  code = buildCountResult(pOperator, &opRes);
×
802
  QUERY_CHECK_CODE(code, lino, _end);
×
803
  if (opRes) {
×
804
    (*ppRes) = opRes;
×
805
    return code;
×
806
  }
807

808
_end:
×
809
  if (code != TSDB_CODE_SUCCESS) {
×
810
    qError("%s failed at line %d since %s. task:%s", __func__, lino, tstrerror(code), GET_TASKID(pTaskInfo));
×
811
    pTaskInfo->code = code;
×
812
    T_LONG_JMP(pTaskInfo->env, code);
×
813
  }
814
  setStreamOperatorCompleted(pOperator);
×
815
  (*ppRes) = NULL;
×
816
  return code;
×
817
}
818

819
void streamCountReleaseState(SOperatorInfo* pOperator) {
×
820
  int32_t                      code = TSDB_CODE_SUCCESS;
×
821
  int32_t                      lino = 0;
×
822
  SStreamEventAggOperatorInfo* pInfo = pOperator->info;
×
823
  SExecTaskInfo*               pTaskInfo = pOperator->pTaskInfo;
×
824
  int32_t                      resSize = sizeof(TSKEY);
×
825
  char*                        pBuff = taosMemoryCalloc(1, resSize);
×
826
  QUERY_CHECK_NULL(pBuff, code, lino, _end, terrno);
×
827

828
  memcpy(pBuff, &pInfo->twAggSup.maxTs, sizeof(TSKEY));
×
829
  qDebug("===stream=== count window operator relase state. ");
×
830
  pInfo->streamAggSup.stateStore.streamStateSaveInfo(pInfo->streamAggSup.pState, STREAM_COUNT_OP_STATE_NAME,
×
831
                                                     strlen(STREAM_COUNT_OP_STATE_NAME), pBuff, resSize);
832
  pInfo->streamAggSup.stateStore.streamStateCommit(pInfo->streamAggSup.pState);
×
833
  taosMemoryFreeClear(pBuff);
×
834
  SOperatorInfo* downstream = pOperator->pDownstream[0];
×
835
  if (downstream->fpSet.releaseStreamStateFn) {
×
836
    downstream->fpSet.releaseStreamStateFn(downstream);
×
837
  }
838
_end:
×
839
  if (code != TSDB_CODE_SUCCESS) {
×
840
    terrno = code;
×
841
    qError("%s failed at line %d since %s. task:%s", __func__, lino, tstrerror(code), GET_TASKID(pTaskInfo));
×
842
  }
843
}
×
844

845
void streamCountReloadState(SOperatorInfo* pOperator) {
×
846
  int32_t                      code = TSDB_CODE_SUCCESS;
×
847
  int32_t                      lino = 0;
×
848
  SStreamCountAggOperatorInfo* pInfo = pOperator->info;
×
849
  SExecTaskInfo*               pTaskInfo = pOperator->pTaskInfo;
×
850
  SStreamAggSupporter*         pAggSup = &pInfo->streamAggSup;
×
851
  int32_t                      size = 0;
×
852
  void*                        pBuf = NULL;
×
853

854
  code = pAggSup->stateStore.streamStateGetInfo(pAggSup->pState, STREAM_COUNT_OP_STATE_NAME,
×
855
                                                strlen(STREAM_COUNT_OP_STATE_NAME), &pBuf, &size);
856
  QUERY_CHECK_CODE(code, lino, _end);
×
857

858
  TSKEY ts = *(TSKEY*)pBuf;
×
859
  pInfo->twAggSup.maxTs = TMAX(pInfo->twAggSup.maxTs, ts);
×
860
  taosMemoryFree(pBuf);
×
861

862
  SOperatorInfo* downstream = pOperator->pDownstream[0];
×
863
  if (downstream->fpSet.reloadStreamStateFn) {
×
864
    downstream->fpSet.reloadStreamStateFn(downstream);
×
865
  }
866
  reloadAggSupFromDownStream(downstream, &pInfo->streamAggSup);
×
867

868
_end:
×
869
  if (code != TSDB_CODE_SUCCESS) {
×
870
    terrno = code;
×
871
    qError("%s failed at line %d since %s. task:%s", __func__, lino, tstrerror(code), GET_TASKID(pTaskInfo));
×
872
  }
873
}
×
874

875
int32_t createStreamCountAggOperatorInfo(SOperatorInfo* downstream, SPhysiNode* pPhyNode, SExecTaskInfo* pTaskInfo,
×
876
                                         SReadHandle* pHandle, SOperatorInfo** pOptrInfo) {
877
  QRY_PARAM_CHECK(pOptrInfo);
×
878

879
  SCountWinodwPhysiNode*       pCountNode = (SCountWinodwPhysiNode*)pPhyNode;
×
880
  int32_t                      numOfCols = 0;
×
881
  int32_t                      code = TSDB_CODE_SUCCESS;
×
882
  int32_t                      lino = 0;
×
883
  SStreamCountAggOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SStreamCountAggOperatorInfo));
×
884
  SOperatorInfo*               pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
×
885
  if (pInfo == NULL || pOperator == NULL) {
×
886
    code = terrno;
×
887
    QUERY_CHECK_CODE(code, lino, _error);
×
888
  }
889

890
  pOperator->pTaskInfo = pTaskInfo;
×
891

892
  initResultSizeInfo(&pOperator->resultInfo, 4096);
×
893
  if (pCountNode->window.pExprs != NULL) {
×
894
    int32_t    numOfScalar = 0;
×
895
    SExprInfo* pScalarExprInfo = NULL;
×
896
    code = createExprInfo(pCountNode->window.pExprs, NULL, &pScalarExprInfo, &numOfScalar);
×
897
    QUERY_CHECK_CODE(code, lino, _error);
×
898

899
    code = initExprSupp(&pInfo->scalarSupp, pScalarExprInfo, numOfScalar, &pTaskInfo->storageAPI.functionStore);
×
900
    QUERY_CHECK_CODE(code, lino, _error);
×
901
  }
902
  SExprSupp* pExpSup = &pOperator->exprSupp;
×
903

904
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pPhyNode->pOutputDataBlockDesc);
×
905
  QUERY_CHECK_NULL(pResBlock, code, lino, _error, terrno);
×
906
  pInfo->binfo.pRes = pResBlock;
×
907

908
  SExprInfo* pExprInfo = NULL;
×
909
  code = createExprInfo(pCountNode->window.pFuncs, NULL, &pExprInfo, &numOfCols);
×
910
  QUERY_CHECK_CODE(code, lino, _error);
×
911

912
  code = initBasicInfoEx(&pInfo->binfo, pExpSup, pExprInfo, numOfCols, pResBlock, &pTaskInfo->storageAPI.functionStore);
×
913
  QUERY_CHECK_CODE(code, lino, _error);
×
914

915
  pInfo->twAggSup = (STimeWindowAggSupp){
×
916
      .waterMark = pCountNode->window.watermark,
×
917
      .calTrigger = pCountNode->window.triggerType,
×
918
      .maxTs = INT64_MIN,
919
      .minTs = INT64_MAX,
920
      .deleteMark = getDeleteMark(&pCountNode->window, 0),
×
921
  };
922

923
  pInfo->primaryTsIndex = ((SColumnNode*)pCountNode->window.pTspk)->slotId;
×
924
  code = initStreamAggSupporter(&pInfo->streamAggSup, pExpSup, numOfCols, 0, pTaskInfo->streamInfo.pState,
×
925
                                sizeof(COUNT_TYPE), 0, &pTaskInfo->storageAPI.stateStore, pHandle, &pInfo->twAggSup,
926
                                GET_TASKID(pTaskInfo), &pTaskInfo->storageAPI, pInfo->primaryTsIndex,
×
927
                                STREAM_STATE_BUFF_SORT, 1);
928
  QUERY_CHECK_CODE(code, lino, _error);
×
929

930
  pInfo->streamAggSup.windowCount = pCountNode->windowCount;
×
931
  pInfo->streamAggSup.windowSliding = pCountNode->windowSliding;
×
932

933
  code = initExecTimeWindowInfo(&pInfo->twAggSup.timeWindowData, &pTaskInfo->window);
×
934
  QUERY_CHECK_CODE(code, lino, _error);
×
935

936
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
×
937
  pInfo->pStDeleted = tSimpleHashInit(64, hashFn);
×
938
  QUERY_CHECK_NULL(pInfo->pStDeleted, code, lino, _error, terrno);
×
939
  pInfo->pDelIterator = NULL;
×
940

941
  code = createSpecialDataBlock(STREAM_DELETE_RESULT, &pInfo->pDelRes);
×
942
  QUERY_CHECK_CODE(code, lino, _error);
×
943

944
  pInfo->ignoreExpiredData = pCountNode->window.igExpired;
×
945
  pInfo->ignoreExpiredDataSaved = false;
×
946
  pInfo->pUpdated = NULL;
×
947
  pInfo->pStUpdated = NULL;
×
948
  pInfo->dataVersion = 0;
×
949
  pInfo->historyWins = taosArrayInit(4, sizeof(SSessionKey));
×
950
  if (!pInfo->historyWins) {
×
951
    code = terrno;
×
952
    QUERY_CHECK_CODE(code, lino, _error);
×
953
  }
954

955
  code = createSpecialDataBlock(STREAM_CHECKPOINT, &pInfo->pCheckpointRes);
×
956
  QUERY_CHECK_CODE(code, lino, _error);
×
957

958
  pInfo->recvGetAll = false;
×
959
  pInfo->pPkDeleted = tSimpleHashInit(64, hashFn);
×
960
  QUERY_CHECK_NULL(pInfo->pPkDeleted, code, lino, _error, terrno);
×
961
  pInfo->destHasPrimaryKey = pCountNode->window.destHasPrimaryKey;
×
962

963
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_STREAM_COUNT;
×
964
  setOperatorInfo(pOperator, getStreamOpName(pOperator->operatorType), QUERY_NODE_PHYSICAL_PLAN_STREAM_COUNT, true,
×
965
                  OP_NOT_OPENED, pInfo, pTaskInfo);
966
  // for stream
967
  void*   buff = NULL;
×
968
  int32_t len = 0;
×
969
  int32_t res =
970
      pInfo->streamAggSup.stateStore.streamStateGetInfo(pInfo->streamAggSup.pState, STREAM_COUNT_OP_CHECKPOINT_NAME,
×
971
                                                        strlen(STREAM_COUNT_OP_CHECKPOINT_NAME), &buff, &len);
972
  if (res == TSDB_CODE_SUCCESS) {
×
973
    code = doStreamCountDecodeOpState(buff, len, pOperator, true);
×
974
    QUERY_CHECK_CODE(code, lino, _error);
×
975
    taosMemoryFree(buff);
×
976
  }
977
  pInfo->pOperator = pOperator;
×
978
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, doStreamCountAggNext, NULL, destroyStreamCountAggOperatorInfo,
×
979
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
980
  setOperatorStreamStateFn(pOperator, streamCountReleaseState, streamCountReloadState);
×
981

982
  code = initStreamBasicInfo(&pInfo->basic, pOperator);
×
983
  QUERY_CHECK_CODE(code, lino, _error);
×
984

985
  if (downstream) {
×
986
    code = initDownStream(downstream, &pInfo->streamAggSup, pOperator->operatorType, pInfo->primaryTsIndex,
×
987
                          &pInfo->twAggSup, &pInfo->basic);
×
988
    QUERY_CHECK_CODE(code, lino, _error);
×
989

990
    code = appendDownstream(pOperator, &downstream, 1);
×
991
    QUERY_CHECK_CODE(code, lino, _error);
×
992
  }
993

994
  *pOptrInfo = pOperator;
×
995
  return TSDB_CODE_SUCCESS;
×
996

997
_error:
×
998
  if (pInfo != NULL) {
×
999
    destroyStreamCountAggOperatorInfo(pInfo);
×
1000
  }
1001

1002
  destroyOperatorAndDownstreams(pOperator, &downstream, 1);
×
1003
  pTaskInfo->code = code;
×
1004
  qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1005
  return code;
×
1006
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc