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

taosdata / TDengine / #3840

04 Apr 2025 03:35PM UTC coverage: 63.027% (+0.6%) from 62.382%
#3840

push

travis-ci

web-flow
Merge pull request #30653 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

155471 of 315065 branches covered (49.35%)

Branch coverage included in aggregate %.

241637 of 314991 relevant lines covered (76.71%)

18825079.96 hits per line

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

78.35
/source/libs/executor/src/eventwindowoperator.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

16
#include "executorInt.h"
17
#include "filter.h"
18
#include "function.h"
19
#include "functionMgt.h"
20
#include "operator.h"
21
#include "querytask.h"
22
#include "tcommon.h"
23
#include "tcompare.h"
24
#include "tdatablock.h"
25
#include "ttime.h"
26

27
static int32_t eventWindowAggregateNext(SOperatorInfo* pOperator, SSDataBlock** pRes);
28
static void    destroyEWindowOperatorInfo(void* param);
29
static int32_t eventWindowAggImpl(SOperatorInfo* pOperator, SEventWindowOperatorInfo* pInfo, SSDataBlock* pBlock);
30

31
int32_t createEventwindowOperatorInfo(SOperatorInfo* downstream, SPhysiNode* physiNode,
310,804✔
32
                                             SExecTaskInfo* pTaskInfo, SOperatorInfo** pOptrInfo) {
33
  QRY_PARAM_CHECK(pOptrInfo);
310,804!
34

35
  int32_t                   code = TSDB_CODE_SUCCESS;
310,804✔
36
  int32_t                   lino = 0;
310,804✔
37
  SEventWindowOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SEventWindowOperatorInfo));
310,804!
38
  SOperatorInfo*            pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
310,803!
39
  if (pInfo == NULL || pOperator == NULL) {
310,802!
40
    code = terrno;
×
41
    goto _error;
×
42
  }
43

44
  pOperator->exprSupp.hasWindowOrGroup = true;
310,802✔
45

46
  SEventWinodwPhysiNode* pEventWindowNode = (SEventWinodwPhysiNode*)physiNode;
310,802✔
47

48
  int32_t tsSlotId = ((SColumnNode*)pEventWindowNode->window.pTspk)->slotId;
310,802✔
49
  code = filterInitFromNode((SNode*)pEventWindowNode->pStartCond, &pInfo->pStartCondInfo, 0);
310,802✔
50
  QUERY_CHECK_CODE(code, lino, _error);
310,803!
51

52
  code = filterInitFromNode((SNode*)pEventWindowNode->pEndCond, &pInfo->pEndCondInfo, 0);
310,803✔
53
  QUERY_CHECK_CODE(code, lino, _error);
310,803!
54

55
  if (pEventWindowNode->window.pExprs != NULL) {
310,803✔
56
    int32_t    numOfScalarExpr = 0;
2✔
57
    SExprInfo* pScalarExprInfo = NULL;
2✔
58

59
    code = createExprInfo(pEventWindowNode->window.pExprs, NULL, &pScalarExprInfo, &numOfScalarExpr);
2✔
60
    QUERY_CHECK_CODE(code, lino, _error);
2!
61
    code = initExprSupp(&pInfo->scalarSup, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
2✔
62
    QUERY_CHECK_CODE(code, lino, _error);
2!
63
  }
64

65
  code = filterInitFromNode((SNode*)pEventWindowNode->window.node.pConditions, &pOperator->exprSupp.pFilterInfo, 0);
310,803✔
66
  QUERY_CHECK_CODE(code, lino, _error);
310,803!
67

68
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
310,803✔
69

70
  int32_t    num = 0;
310,803✔
71
  SExprInfo* pExprInfo = NULL;
310,803✔
72
  code = createExprInfo(pEventWindowNode->window.pFuncs, NULL, &pExprInfo, &num);
310,803✔
73
  QUERY_CHECK_CODE(code, lino, _error);
310,804!
74

75
  initResultSizeInfo(&pOperator->resultInfo, 4096);
310,804✔
76

77
  code = initAggSup(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, num, keyBufSize, pTaskInfo->id.str,
310,804✔
78
                    pTaskInfo->streamInfo.pState, &pTaskInfo->storageAPI.functionStore);
310,804✔
79
  QUERY_CHECK_CODE(code, lino, _error);
310,803!
80

81
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pEventWindowNode->window.node.pOutputDataBlockDesc);
310,803✔
82
  QUERY_CHECK_NULL(pResBlock, code, lino, _error, terrno);
310,802!
83
  initBasicInfo(&pInfo->binfo, pResBlock);
310,802✔
84

85
  code = blockDataEnsureCapacity(pResBlock, pOperator->resultInfo.capacity);
310,802✔
86
  QUERY_CHECK_CODE(code, lino, _error);
310,804!
87

88
  initResultRowInfo(&pInfo->binfo.resultRowInfo);
310,804✔
89
  pInfo->binfo.inputTsOrder = physiNode->inputTsOrder;
310,804✔
90
  pInfo->binfo.outputTsOrder = physiNode->outputTsOrder;
310,804✔
91

92
  pInfo->twAggSup = (STimeWindowAggSupp){.waterMark = pEventWindowNode->window.watermark,
310,804✔
93
                                         .calTrigger = pEventWindowNode->window.triggerType};
310,804✔
94

95
  code = initExecTimeWindowInfo(&pInfo->twAggSup.timeWindowData, &pTaskInfo->window);
310,804✔
96
  QUERY_CHECK_CODE(code, lino, _error);
310,804!
97

98
  pInfo->tsSlotId = tsSlotId;
310,804✔
99
  pInfo->pPreDataBlock = NULL;
310,804✔
100
  pInfo->pOperator = pOperator;
310,804✔
101
  pInfo->trueForLimit = pEventWindowNode->trueForLimit;
310,804✔
102

103
  setOperatorInfo(pOperator, "EventWindowOperator", QUERY_NODE_PHYSICAL_PLAN_MERGE_EVENT, true, OP_NOT_OPENED, pInfo,
310,804✔
104
                  pTaskInfo);
105
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, eventWindowAggregateNext, NULL, destroyEWindowOperatorInfo,
310,804✔
106
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
107

108
  code = appendDownstream(pOperator, &downstream, 1);
310,804✔
109
  if (code != TSDB_CODE_SUCCESS) {
310,803!
110
    goto _error;
×
111
  }
112

113
  *pOptrInfo = pOperator;
310,803✔
114
  return TSDB_CODE_SUCCESS;
310,803✔
115

116
_error:
×
117
  if (pInfo != NULL) {
×
118
    destroyEWindowOperatorInfo(pInfo);
×
119
  }
120

121
  destroyOperatorAndDownstreams(pOperator, &downstream, 1);
×
122
  pTaskInfo->code = code;
×
123
  return code;
×
124
}
125

126
void cleanupResultInfoInEventWindow(SOperatorInfo* pOperator, SEventWindowOperatorInfo* pInfo) {
310,804✔
127
  if (pInfo == NULL || pInfo->pRow == NULL || pOperator == NULL) {
310,804!
128
    return;
73,349✔
129
  }
130
  SExprSupp*       pSup = &pOperator->exprSupp;
237,455✔
131
  for (int32_t j = 0; j < pSup->numOfExprs; ++j) {
573,379✔
132
    pSup->pCtx[j].resultInfo = getResultEntryInfo(pInfo->pRow, j, pSup->rowEntryInfoOffset);
335,925✔
133
    if (pSup->pCtx[j].fpSet.cleanup) {
335,924!
134
      pSup->pCtx[j].fpSet.cleanup(&pSup->pCtx[j]);
×
135
    }
136
  }
137
}
138

139
void destroyEWindowOperatorInfo(void* param) {
310,803✔
140
  SEventWindowOperatorInfo* pInfo = (SEventWindowOperatorInfo*)param;
310,803✔
141
  if (pInfo == NULL) {
310,803!
142
    return;
×
143
  }
144

145
  if (pInfo->pRow != NULL) {
310,803✔
146
    taosMemoryFree(pInfo->pRow);
237,454!
147
  }
148

149
  if (pInfo->pStartCondInfo != NULL) {
310,804!
150
    filterFreeInfo(pInfo->pStartCondInfo);
310,804✔
151
    pInfo->pStartCondInfo = NULL;
310,804✔
152
  }
153

154
  if (pInfo->pEndCondInfo != NULL) {
310,804!
155
    filterFreeInfo(pInfo->pEndCondInfo);
310,804✔
156
    pInfo->pEndCondInfo = NULL;
310,804✔
157
  }
158

159
  cleanupBasicInfo(&pInfo->binfo);
310,804✔
160
  colDataDestroy(&pInfo->twAggSup.timeWindowData);
310,804✔
161

162
  cleanupResultInfoInEventWindow(pInfo->pOperator, pInfo);
310,804✔
163
  pInfo->pOperator = NULL;
310,804✔
164
  cleanupAggSup(&pInfo->aggSup);
310,804✔
165
  cleanupExprSupp(&pInfo->scalarSup);
310,804✔
166
  taosMemoryFreeClear(param);
310,804!
167
}
168

169
static int32_t eventWindowAggregateNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
548,243✔
170
  int32_t                   code = TSDB_CODE_SUCCESS;
548,243✔
171
  int32_t                   lino = 0;
548,243✔
172
  SEventWindowOperatorInfo* pInfo = pOperator->info;
548,243✔
173
  SExecTaskInfo*            pTaskInfo = pOperator->pTaskInfo;
548,243✔
174

175
  SExprSupp* pSup = &pOperator->exprSupp;
548,243✔
176
  int32_t    order = pInfo->binfo.inputTsOrder;
548,243✔
177

178
  SSDataBlock* pRes = pInfo->binfo.pRes;
548,243✔
179

180
  blockDataCleanup(pRes);
548,243✔
181

182
  SOperatorInfo* downstream = pOperator->pDownstream[0];
548,241✔
183
  while (1) {
340,216✔
184
    SSDataBlock* pBlock = NULL;
888,457✔
185
    if (pInfo->pPreDataBlock == NULL) {
888,457✔
186
      pBlock = getNextBlockFromDownstream(pOperator, 0);
888,427✔
187
    } else {
188
      pBlock = pInfo->pPreDataBlock;
30✔
189
      pInfo->pPreDataBlock = NULL;
30✔
190
    }
191

192
    if (pBlock == NULL) {
888,461✔
193
      break;
548,234✔
194
    }
195

196
    pRes->info.scanFlag = pBlock->info.scanFlag;
340,227✔
197
    code = setInputDataBlock(pSup, pBlock, order, MAIN_SCAN, true);
340,227✔
198
    QUERY_CHECK_CODE(code, lino, _end);
340,228!
199

200
    code = blockDataUpdateTsWindow(pBlock, pInfo->tsSlotId);
340,228✔
201
    QUERY_CHECK_CODE(code, lino, _end);
340,229!
202

203
    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
204
    if (pInfo->scalarSup.pExprInfo != NULL) {
340,229✔
205
      code = projectApplyFunctions(pInfo->scalarSup.pExprInfo, pBlock, pBlock, pInfo->scalarSup.pCtx,
4✔
206
                                   pInfo->scalarSup.numOfExprs, NULL);
207
      QUERY_CHECK_CODE(code, lino, _end);
4!
208
    }
209

210
    code = eventWindowAggImpl(pOperator, pInfo, pBlock);
340,229✔
211
    QUERY_CHECK_CODE(code, lino, _end);
340,229!
212

213
    code = doFilter(pRes, pSup->pFilterInfo, NULL);
340,229✔
214
    QUERY_CHECK_CODE(code, lino, _end);
340,229!
215

216
    if (pRes->info.rows >= pOperator->resultInfo.threshold ||
340,229!
217
        (pRes->info.id.groupId != pInfo->groupId && pRes->info.rows > 0)) {
340,229✔
218
      (*ppRes) = pRes;
13✔
219
      return code;
13✔
220
    }
221
  }
222

223
_end:
548,234✔
224
  if (code != TSDB_CODE_SUCCESS) {
548,234!
225
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
226
    pTaskInfo->code = code;
×
227
    T_LONG_JMP(pTaskInfo->env, code);
×
228
  }
229
  (*ppRes) =  pRes->info.rows == 0 ? NULL : pRes;
548,234✔
230
  return code;
548,234✔
231
}
232

233
static int32_t setSingleOutputTupleBufv1(SResultRowInfo* pResultRowInfo, STimeWindow* win, SResultRow** pResult,
81,339,465✔
234
                                         SExprSupp* pExprSup, SAggSupporter* pAggSup) {
235
  if (*pResult == NULL) {
81,339,465✔
236
    SResultRow* p = taosMemoryCalloc(1, pAggSup->resultRowSize);
237,455!
237
    if (!p) {
237,453!
238
      return terrno;
×
239
    }
240
    pResultRowInfo->cur = (SResultRowPosition){.pageId = p->pageId, .offset = p->offset};
237,453✔
241
    *pResult = p;
237,453✔
242
  }
243

244
  (*pResult)->win = *win;
81,339,463✔
245

246
  return setResultRowInitCtx(*pResult, pExprSup->pCtx, pExprSup->numOfExprs, pExprSup->rowEntryInfoOffset);
81,339,463✔
247
}
248

249
static int32_t doEventWindowAggImpl(SEventWindowOperatorInfo* pInfo, SExprSupp* pSup, int32_t startIndex,
81,344,239✔
250
                                    int32_t endIndex, const SSDataBlock* pBlock, int64_t* tsList,
251
                                    SExecTaskInfo* pTaskInfo) {
252
  int32_t code = TSDB_CODE_SUCCESS;
81,344,239✔
253
  SWindowRowsSup* pRowSup = &pInfo->winSup;
81,344,239✔
254

255
  int32_t numOfOutput = pSup->numOfExprs;
81,344,239✔
256
  int32_t numOfRows = endIndex - startIndex + 1;
81,344,239✔
257

258
  doKeepTuple(pRowSup, tsList[endIndex], pBlock->info.id.groupId);
81,344,239✔
259

260
  code = setSingleOutputTupleBufv1(&pInfo->binfo.resultRowInfo, &pRowSup->win, &pInfo->pRow, pSup, &pInfo->aggSup);
81,341,792✔
261
  if (code != TSDB_CODE_SUCCESS) {  // null data, too many state code
81,338,838!
262
    qError("failed to set single output tuple buffer, code:%d", code);
×
263
    return code;
×
264
  }
265

266
  updateTimeWindowInfo(&pInfo->twAggSup.timeWindowData, &pRowSup->win, 0);
81,338,838✔
267
  code = applyAggFunctionOnPartialTuples(pTaskInfo, pSup->pCtx, &pInfo->twAggSup.timeWindowData, startIndex, numOfRows,
81,338,009✔
268
                                         pBlock->info.rows, numOfOutput);
81,338,009✔
269
  return code;
81,352,527✔
270
}
271

272
int32_t eventWindowAggImpl(SOperatorInfo* pOperator, SEventWindowOperatorInfo* pInfo, SSDataBlock* pBlock) {
340,228✔
273
  int32_t          code = TSDB_CODE_SUCCESS;
340,228✔
274
  int32_t          lino = 0;
340,228✔
275
  SExecTaskInfo*   pTaskInfo = pOperator->pTaskInfo;
340,228✔
276
  SExprSupp*       pSup = &pOperator->exprSupp;
340,228✔
277
  SSDataBlock*     pRes = pInfo->binfo.pRes;
340,228✔
278
  int64_t          gid = pBlock->info.id.groupId;
340,228✔
279
  SColumnInfoData *ps = NULL, *pe = NULL;
340,228✔
280
  SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, pInfo->tsSlotId);
340,228✔
281
  QUERY_CHECK_NULL(pColInfoData, code, lino, _return, terrno);
340,226!
282
  TSKEY*           tsList = (TSKEY*)pColInfoData->pData;
340,226✔
283
  SWindowRowsSup*  pRowSup = &pInfo->winSup;
340,226✔
284
  int32_t          rowIndex = 0;
340,226✔
285
  int64_t          minWindowSize = getMinWindowSize(pOperator);
340,226✔
286

287
  pRowSup->numOfRows = 0;
340,228✔
288
  if (pInfo->groupId == 0) {
340,228✔
289
    pInfo->groupId = gid;
340,166✔
290
  } else if (pInfo->groupId != gid) {
62✔
291
    // this is a new group, reset the info
292
    pInfo->inWindow = false;
31✔
293
    pInfo->groupId = gid;
31✔
294
    pInfo->pPreDataBlock = pBlock;
31✔
295
    goto _return;
31✔
296
  }
297
  pRes->info.id.groupId = pInfo->groupId;
340,197✔
298

299
  SFilterColumnParam param1 = {.numOfCols = taosArrayGetSize(pBlock->pDataBlock), .pDataBlock = pBlock->pDataBlock};
340,197✔
300

301
  code = filterSetDataFromSlotId(pInfo->pStartCondInfo, &param1);
340,194✔
302
  QUERY_CHECK_CODE(code, lino, _return);
340,197!
303

304
  int32_t status1 = 0;
340,197✔
305
  code = filterExecute(pInfo->pStartCondInfo, pBlock, &ps, NULL, param1.numOfCols, &status1);
340,197✔
306
  QUERY_CHECK_CODE(code, lino, _return);
340,197!
307

308
  SFilterColumnParam param2 = {.numOfCols = taosArrayGetSize(pBlock->pDataBlock), .pDataBlock = pBlock->pDataBlock};
340,197✔
309
  code = filterSetDataFromSlotId(pInfo->pEndCondInfo, &param2);
340,198✔
310
  QUERY_CHECK_CODE(code, lino, _return);
340,198!
311

312
  int32_t status2 = 0;
340,198✔
313
  code = filterExecute(pInfo->pEndCondInfo, pBlock, &pe, NULL, param2.numOfCols, &status2);
340,198✔
314
  QUERY_CHECK_CODE(code, lino, _return);
340,198!
315

316
  int32_t startIndex = pInfo->inWindow ? 0 : -1;
340,198✔
317
  while (rowIndex < pBlock->info.rows) {
162,965,860✔
318
    if (pInfo->inWindow) {  // let's find the first end value
162,686,259✔
319
      for (rowIndex = startIndex; rowIndex < pBlock->info.rows; ++rowIndex) {
103,452,636✔
320
        if (((bool*)pe->pData)[rowIndex]) {
103,393,778✔
321
          break;
81,276,046✔
322
        }
323
      }
324

325
      if (rowIndex < pBlock->info.rows) {
81,334,904✔
326
        code = doEventWindowAggImpl(pInfo, pSup, startIndex, rowIndex, pBlock, tsList, pTaskInfo);
81,276,998✔
327
        QUERY_CHECK_CODE(code, lino, _return);
81,291,078!
328
        doUpdateNumOfRows(pSup->pCtx, pInfo->pRow, pSup->numOfExprs, pSup->rowEntryInfoOffset);
81,291,078✔
329

330
        if (pRowSup->win.ekey - pRowSup->win.skey < minWindowSize) {
81,276,774✔
331
          qDebug("skip small window, groupId: %" PRId64 ", windowSize: %" PRId64 ", minWindowSize: %" PRId64,
12!
332
                 pInfo->groupId, pRowSup->win.ekey - pRowSup->win.skey, minWindowSize);
333
        } else {
334
          // check buffer size
335
          if (pRes->info.rows + pInfo->pRow->numOfRows >= pRes->info.capacity) {
81,276,762!
336
            int32_t newSize = pRes->info.rows + pInfo->pRow->numOfRows;
×
337
            code = blockDataEnsureCapacity(pRes, newSize);
×
338
            QUERY_CHECK_CODE(code, lino, _return);
×
339
          }
340

341
          code = copyResultrowToDataBlock(pSup->pExprInfo, pSup->numOfExprs, pInfo->pRow, pSup->pCtx, pRes,
81,276,762✔
342
                                          pSup->rowEntryInfoOffset, pTaskInfo);
81,276,762✔
343
          QUERY_CHECK_CODE(code, lino, _return);
81,245,466!
344

345
          pRes->info.rows += pInfo->pRow->numOfRows;
81,245,466✔
346
        }
347
        pInfo->pRow->numOfRows = 0;
81,246,640✔
348

349
        pInfo->inWindow = false;
81,246,640✔
350
        rowIndex += 1;
81,246,640✔
351
      } else {
352
        code = doEventWindowAggImpl(pInfo, pSup, startIndex, pBlock->info.rows - 1, pBlock, tsList, pTaskInfo);
57,906✔
353
        QUERY_CHECK_CODE(code, lino, _return);
61,020!
354
      }
355
    } else {  // find the first start value that is fulfill for the start condition
356
      for (; rowIndex < pBlock->info.rows; ++rowIndex) {
103,300,525✔
357
        if (((bool*)ps->pData)[rowIndex]) {
103,237,941✔
358
          doKeepNewWindowStartInfo(pRowSup, tsList, rowIndex, gid);
81,288,771✔
359
          pInfo->inWindow = true;
81,293,403✔
360
          startIndex = rowIndex;
81,293,403✔
361
          if (pInfo->pRow != NULL) {
81,293,403✔
362
            clearResultRowInitFlag(pSup->pCtx, pSup->numOfExprs);
81,060,367✔
363
          }
364
          break;
81,317,084✔
365
        }
366
      }
367

368
      if (pInfo->inWindow) {
81,379,668✔
369
        continue;  // try to find the end position
81,318,002✔
370
      } else {
371
        break;  // no valid start position, quit
61,666✔
372
      }
373
    }
374
  }
375

376
_return:
279,601✔
377

378
  if (code != TSDB_CODE_SUCCESS) {
341,298!
379
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
380
  }
381
  colDataDestroy(ps);
341,298✔
382
  taosMemoryFree(ps);
340,229!
383
  colDataDestroy(pe);
340,229✔
384
  taosMemoryFree(pe);
340,229!
385

386
  return code;
340,229✔
387
}
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