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

taosdata / TDengine / #3593

24 Jan 2025 08:57AM UTC coverage: 63.239% (-0.3%) from 63.546%
#3593

push

travis-ci

web-flow
Merge pull request #29638 from taosdata/docs/TS-5846-3.0

enh: TDengine modify taosBenchmark new query rule cases and add doc

140619 of 285630 branches covered (49.23%)

Branch coverage included in aggregate %.

218877 of 282844 relevant lines covered (77.38%)

19647377.39 hits per line

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

76.43
/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
typedef struct SEventWindowOperatorInfo {
28
  SOptrBasicInfo     binfo;
29
  SAggSupporter      aggSup;
30
  SExprSupp          scalarSup;
31
  SWindowRowsSup     winSup;
32
  int32_t            tsSlotId;  // primary timestamp column slot id
33
  STimeWindowAggSupp twAggSup;
34
  uint64_t           groupId;  // current group id, used to identify the data block from different groups
35
  SFilterInfo*       pStartCondInfo;
36
  SFilterInfo*       pEndCondInfo;
37
  bool               inWindow;
38
  SResultRow*        pRow;
39
  SSDataBlock*       pPreDataBlock;
40
  SOperatorInfo*     pOperator;
41
} SEventWindowOperatorInfo;
42

43
static int32_t eventWindowAggregateNext(SOperatorInfo* pOperator, SSDataBlock** pRes);
44
static void    destroyEWindowOperatorInfo(void* param);
45
static int32_t eventWindowAggImpl(SOperatorInfo* pOperator, SEventWindowOperatorInfo* pInfo, SSDataBlock* pBlock);
46

47
int32_t createEventwindowOperatorInfo(SOperatorInfo* downstream, SPhysiNode* physiNode,
269,788✔
48
                                             SExecTaskInfo* pTaskInfo, SOperatorInfo** pOptrInfo) {
49
  QRY_PARAM_CHECK(pOptrInfo);
269,788!
50

51
  int32_t                   code = TSDB_CODE_SUCCESS;
269,788✔
52
  int32_t                   lino = 0;
269,788✔
53
  SEventWindowOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SEventWindowOperatorInfo));
269,788!
54
  SOperatorInfo*            pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
269,788!
55
  if (pInfo == NULL || pOperator == NULL) {
269,788!
56
    code = terrno;
×
57
    goto _error;
×
58
  }
59

60
  pOperator->exprSupp.hasWindowOrGroup = true;
269,788✔
61

62
  SEventWinodwPhysiNode* pEventWindowNode = (SEventWinodwPhysiNode*)physiNode;
269,788✔
63

64
  int32_t tsSlotId = ((SColumnNode*)pEventWindowNode->window.pTspk)->slotId;
269,788✔
65
  code = filterInitFromNode((SNode*)pEventWindowNode->pStartCond, &pInfo->pStartCondInfo, 0);
269,788✔
66
  QUERY_CHECK_CODE(code, lino, _error);
269,788!
67

68
  code = filterInitFromNode((SNode*)pEventWindowNode->pEndCond, &pInfo->pEndCondInfo, 0);
269,788✔
69
  QUERY_CHECK_CODE(code, lino, _error);
269,788!
70

71
  if (pEventWindowNode->window.pExprs != NULL) {
269,788✔
72
    int32_t    numOfScalarExpr = 0;
2✔
73
    SExprInfo* pScalarExprInfo = NULL;
2✔
74

75
    code = createExprInfo(pEventWindowNode->window.pExprs, NULL, &pScalarExprInfo, &numOfScalarExpr);
2✔
76
    QUERY_CHECK_CODE(code, lino, _error);
2!
77
    code = initExprSupp(&pInfo->scalarSup, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
2✔
78
    QUERY_CHECK_CODE(code, lino, _error);
2!
79
  }
80

81
  code = filterInitFromNode((SNode*)pEventWindowNode->window.node.pConditions, &pOperator->exprSupp.pFilterInfo, 0);
269,788✔
82
  QUERY_CHECK_CODE(code, lino, _error);
269,788!
83

84
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
269,788✔
85

86
  int32_t    num = 0;
269,788✔
87
  SExprInfo* pExprInfo = NULL;
269,788✔
88
  code = createExprInfo(pEventWindowNode->window.pFuncs, NULL, &pExprInfo, &num);
269,788✔
89
  QUERY_CHECK_CODE(code, lino, _error);
269,788!
90

91
  initResultSizeInfo(&pOperator->resultInfo, 4096);
269,788✔
92

93
  code = initAggSup(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, num, keyBufSize, pTaskInfo->id.str,
269,788✔
94
                    pTaskInfo->streamInfo.pState, &pTaskInfo->storageAPI.functionStore);
269,788✔
95
  QUERY_CHECK_CODE(code, lino, _error);
269,788!
96

97
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pEventWindowNode->window.node.pOutputDataBlockDesc);
269,788✔
98
  QUERY_CHECK_NULL(pResBlock, code, lino, _error, terrno);
269,788!
99
  initBasicInfo(&pInfo->binfo, pResBlock);
269,788✔
100

101
  code = blockDataEnsureCapacity(pResBlock, pOperator->resultInfo.capacity);
269,788✔
102
  QUERY_CHECK_CODE(code, lino, _error);
269,788!
103

104
  initResultRowInfo(&pInfo->binfo.resultRowInfo);
269,788✔
105
  pInfo->binfo.inputTsOrder = physiNode->inputTsOrder;
269,788✔
106
  pInfo->binfo.outputTsOrder = physiNode->outputTsOrder;
269,788✔
107

108
  pInfo->twAggSup = (STimeWindowAggSupp){.waterMark = pEventWindowNode->window.watermark,
269,788✔
109
                                         .calTrigger = pEventWindowNode->window.triggerType};
269,788✔
110

111
  code = initExecTimeWindowInfo(&pInfo->twAggSup.timeWindowData, &pTaskInfo->window);
269,788✔
112
  QUERY_CHECK_CODE(code, lino, _error);
269,787!
113

114
  pInfo->tsSlotId = tsSlotId;
269,787✔
115
  pInfo->pPreDataBlock = NULL;
269,787✔
116
  pInfo->pOperator = pOperator;
269,787✔
117

118
  setOperatorInfo(pOperator, "EventWindowOperator", QUERY_NODE_PHYSICAL_PLAN_MERGE_STATE, true, OP_NOT_OPENED, pInfo,
269,787✔
119
                  pTaskInfo);
120
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, eventWindowAggregateNext, NULL, destroyEWindowOperatorInfo,
269,787✔
121
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
122

123
  code = appendDownstream(pOperator, &downstream, 1);
269,787✔
124
  if (code != TSDB_CODE_SUCCESS) {
269,788!
125
    goto _error;
×
126
  }
127

128
  *pOptrInfo = pOperator;
269,788✔
129
  return TSDB_CODE_SUCCESS;
269,788✔
130

131
_error:
×
132
  if (pInfo != NULL) {
×
133
    destroyEWindowOperatorInfo(pInfo);
×
134
  }
135

136
  destroyOperatorAndDownstreams(pOperator, &downstream, 1);
×
137
  pTaskInfo->code = code;
×
138
  return code;
×
139
}
140

141
void cleanupResultInfoInEventWindow(SOperatorInfo* pOperator, SEventWindowOperatorInfo* pInfo) {
269,788✔
142
  if (pInfo == NULL || pInfo->pRow == NULL || pOperator == NULL) {
269,788!
143
    return;
75,253✔
144
  }
145
  SExprSupp*       pSup = &pOperator->exprSupp;
194,535✔
146
  for (int32_t j = 0; j < pSup->numOfExprs; ++j) {
478,722✔
147
    pSup->pCtx[j].resultInfo = getResultEntryInfo(pInfo->pRow, j, pSup->rowEntryInfoOffset);
284,187✔
148
    if (pSup->pCtx[j].fpSet.cleanup) {
284,187!
149
      pSup->pCtx[j].fpSet.cleanup(&pSup->pCtx[j]);
×
150
    }
151
  }
152
}
153

154
void destroyEWindowOperatorInfo(void* param) {
269,788✔
155
  SEventWindowOperatorInfo* pInfo = (SEventWindowOperatorInfo*)param;
269,788✔
156
  if (pInfo == NULL) {
269,788!
157
    return;
×
158
  }
159

160
  if (pInfo->pRow != NULL) {
269,788✔
161
    taosMemoryFree(pInfo->pRow);
194,535!
162
  }
163

164
  if (pInfo->pStartCondInfo != NULL) {
269,788!
165
    filterFreeInfo(pInfo->pStartCondInfo);
269,788✔
166
    pInfo->pStartCondInfo = NULL;
269,788✔
167
  }
168

169
  if (pInfo->pEndCondInfo != NULL) {
269,788!
170
    filterFreeInfo(pInfo->pEndCondInfo);
269,788✔
171
    pInfo->pEndCondInfo = NULL;
269,788✔
172
  }
173

174
  cleanupBasicInfo(&pInfo->binfo);
269,788✔
175
  colDataDestroy(&pInfo->twAggSup.timeWindowData);
269,788✔
176

177
  cleanupResultInfoInEventWindow(pInfo->pOperator, pInfo);
269,788✔
178
  pInfo->pOperator = NULL;
269,788✔
179
  cleanupAggSup(&pInfo->aggSup);
269,788✔
180
  cleanupExprSupp(&pInfo->scalarSup);
269,788✔
181
  taosMemoryFreeClear(param);
269,788!
182
}
183

184
static int32_t eventWindowAggregateNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
464,306✔
185
  int32_t                   code = TSDB_CODE_SUCCESS;
464,306✔
186
  int32_t                   lino = 0;
464,306✔
187
  SEventWindowOperatorInfo* pInfo = pOperator->info;
464,306✔
188
  SExecTaskInfo*            pTaskInfo = pOperator->pTaskInfo;
464,306✔
189

190
  SExprSupp* pSup = &pOperator->exprSupp;
464,306✔
191
  int32_t    order = pInfo->binfo.inputTsOrder;
464,306✔
192

193
  SSDataBlock* pRes = pInfo->binfo.pRes;
464,306✔
194

195
  blockDataCleanup(pRes);
464,306✔
196

197
  SOperatorInfo* downstream = pOperator->pDownstream[0];
464,307✔
198
  while (1) {
270,834✔
199
    SSDataBlock* pBlock = NULL;
735,141✔
200
    if (pInfo->pPreDataBlock == NULL) {
735,141✔
201
      pBlock = getNextBlockFromDownstream(pOperator, 0);
735,114✔
202
    } else {
203
      pBlock = pInfo->pPreDataBlock;
27✔
204
      pInfo->pPreDataBlock = NULL;
27✔
205
    }
206

207
    if (pBlock == NULL) {
735,140✔
208
      break;
464,298✔
209
    }
210

211
    pRes->info.scanFlag = pBlock->info.scanFlag;
270,842✔
212
    code = setInputDataBlock(pSup, pBlock, order, MAIN_SCAN, true);
270,842✔
213
    QUERY_CHECK_CODE(code, lino, _end);
270,841!
214

215
    code = blockDataUpdateTsWindow(pBlock, pInfo->tsSlotId);
270,841✔
216
    QUERY_CHECK_CODE(code, lino, _end);
270,842!
217

218
    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
219
    if (pInfo->scalarSup.pExprInfo != NULL) {
270,842✔
220
      code = projectApplyFunctions(pInfo->scalarSup.pExprInfo, pBlock, pBlock, pInfo->scalarSup.pCtx,
4✔
221
                                   pInfo->scalarSup.numOfExprs, NULL);
222
      QUERY_CHECK_CODE(code, lino, _end);
4!
223
    }
224

225
    code = eventWindowAggImpl(pOperator, pInfo, pBlock);
270,842✔
226
    QUERY_CHECK_CODE(code, lino, _end);
270,844!
227

228
    code = doFilter(pRes, pSup->pFilterInfo, NULL);
270,844✔
229
    QUERY_CHECK_CODE(code, lino, _end);
270,844!
230

231
    if (pRes->info.rows >= pOperator->resultInfo.threshold ||
270,844!
232
        (pRes->info.id.groupId != pInfo->groupId && pRes->info.rows > 0)) {
270,844✔
233
      (*ppRes) = pRes;
10✔
234
      return code;
10✔
235
    }
236
  }
237

238
_end:
464,298✔
239
  if (code != TSDB_CODE_SUCCESS) {
464,298!
240
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
241
    pTaskInfo->code = code;
×
242
    T_LONG_JMP(pTaskInfo->env, code);
×
243
  }
244
  (*ppRes) =  pRes->info.rows == 0 ? NULL : pRes;
464,298✔
245
  return code;
464,298✔
246
}
247

248
static int32_t setSingleOutputTupleBufv1(SResultRowInfo* pResultRowInfo, STimeWindow* win, SResultRow** pResult,
82,227,293✔
249
                                         SExprSupp* pExprSup, SAggSupporter* pAggSup) {
250
  if (*pResult == NULL) {
82,227,293✔
251
    SResultRow* p = taosMemoryCalloc(1, pAggSup->resultRowSize);
194,534!
252
    if (!p) {
194,534!
253
      return terrno;
×
254
    }
255
    pResultRowInfo->cur = (SResultRowPosition){.pageId = p->pageId, .offset = p->offset};
194,534✔
256
    *pResult = p;
194,534✔
257
  }
258

259
  (*pResult)->win = *win;
82,227,293✔
260

261
  return setResultRowInitCtx(*pResult, pExprSup->pCtx, pExprSup->numOfExprs, pExprSup->rowEntryInfoOffset);
82,227,293✔
262
}
263

264
static int32_t doEventWindowAggImpl(SEventWindowOperatorInfo* pInfo, SExprSupp* pSup, int32_t startIndex,
82,224,079✔
265
                                    int32_t endIndex, const SSDataBlock* pBlock, int64_t* tsList,
266
                                    SExecTaskInfo* pTaskInfo) {
267
  int32_t code = TSDB_CODE_SUCCESS;
82,224,079✔
268
  SWindowRowsSup* pRowSup = &pInfo->winSup;
82,224,079✔
269

270
  int32_t numOfOutput = pSup->numOfExprs;
82,224,079✔
271
  int32_t numOfRows = endIndex - startIndex + 1;
82,224,079✔
272

273
  doKeepTuple(pRowSup, tsList[endIndex], pBlock->info.id.groupId);
82,224,079✔
274

275
  code = setSingleOutputTupleBufv1(&pInfo->binfo.resultRowInfo, &pRowSup->win, &pInfo->pRow, pSup, &pInfo->aggSup);
82,228,075✔
276
  if (code != TSDB_CODE_SUCCESS) {  // null data, too many state code
82,218,138!
277
    qError("failed to set single output tuple buffer, code:%d", code);
×
278
    return code;
×
279
  }
280

281
  updateTimeWindowInfo(&pInfo->twAggSup.timeWindowData, &pRowSup->win, 0);
82,218,138✔
282
  code = applyAggFunctionOnPartialTuples(pTaskInfo, pSup->pCtx, &pInfo->twAggSup.timeWindowData, startIndex, numOfRows,
82,217,078✔
283
                                         pBlock->info.rows, numOfOutput);
82,217,078✔
284
  return code;
82,224,754✔
285
}
286

287
int32_t eventWindowAggImpl(SOperatorInfo* pOperator, SEventWindowOperatorInfo* pInfo, SSDataBlock* pBlock) {
270,842✔
288
  int32_t          code = TSDB_CODE_SUCCESS;
270,842✔
289
  int32_t          lino = 0;
270,842✔
290
  SExecTaskInfo*   pTaskInfo = pOperator->pTaskInfo;
270,842✔
291
  SExprSupp*       pSup = &pOperator->exprSupp;
270,842✔
292
  SSDataBlock*     pRes = pInfo->binfo.pRes;
270,842✔
293
  int64_t          gid = pBlock->info.id.groupId;
270,842✔
294
  SColumnInfoData *ps = NULL, *pe = NULL;
270,842✔
295
  SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, pInfo->tsSlotId);
270,842✔
296
  QUERY_CHECK_NULL(pColInfoData, code, lino, _return, terrno);
270,842!
297
  TSKEY*           tsList = (TSKEY*)pColInfoData->pData;
270,842✔
298
  SWindowRowsSup*  pRowSup = &pInfo->winSup;
270,842✔
299
  int32_t          rowIndex = 0;
270,842✔
300

301
  pRowSup->numOfRows = 0;
270,842✔
302
  if (pInfo->groupId == 0) {
270,842✔
303
    pInfo->groupId = gid;
270,787✔
304
  } else if (pInfo->groupId != gid) {
55✔
305
    // this is a new group, reset the info
306
    pInfo->inWindow = false;
28✔
307
    pInfo->groupId = gid;
28✔
308
    pInfo->pPreDataBlock = pBlock;
28✔
309
    goto _return;
28✔
310
  }
311
  pRes->info.id.groupId = pInfo->groupId;
270,814✔
312

313
  SFilterColumnParam param1 = {.numOfCols = taosArrayGetSize(pBlock->pDataBlock), .pDataBlock = pBlock->pDataBlock};
270,814✔
314

315
  code = filterSetDataFromSlotId(pInfo->pStartCondInfo, &param1);
270,814✔
316
  QUERY_CHECK_CODE(code, lino, _return);
270,814!
317

318
  int32_t status1 = 0;
270,814✔
319
  code = filterExecute(pInfo->pStartCondInfo, pBlock, &ps, NULL, param1.numOfCols, &status1);
270,814✔
320
  QUERY_CHECK_CODE(code, lino, _return);
270,816!
321

322
  SFilterColumnParam param2 = {.numOfCols = taosArrayGetSize(pBlock->pDataBlock), .pDataBlock = pBlock->pDataBlock};
270,816✔
323
  code = filterSetDataFromSlotId(pInfo->pEndCondInfo, &param2);
270,816✔
324
  QUERY_CHECK_CODE(code, lino, _return);
270,816!
325

326
  int32_t status2 = 0;
270,816✔
327
  code = filterExecute(pInfo->pEndCondInfo, pBlock, &pe, NULL, param2.numOfCols, &status2);
270,816✔
328
  QUERY_CHECK_CODE(code, lino, _return);
270,817!
329

330
  int32_t startIndex = pInfo->inWindow ? 0 : -1;
270,817✔
331
  while (rowIndex < pBlock->info.rows) {
164,621,949✔
332
    if (pInfo->inWindow) {  // let's find the first end value
164,405,667✔
333
      for (rowIndex = startIndex; rowIndex < pBlock->info.rows; ++rowIndex) {
104,551,424✔
334
        if (((bool*)pe->pData)[rowIndex]) {
104,504,907✔
335
          break;
82,164,061✔
336
        }
337
      }
338

339
      if (rowIndex < pBlock->info.rows) {
82,210,578✔
340
        code = doEventWindowAggImpl(pInfo, pSup, startIndex, rowIndex, pBlock, tsList, pTaskInfo);
82,164,808✔
341
        QUERY_CHECK_CODE(code, lino, _return);
82,175,977!
342
        doUpdateNumOfRows(pSup->pCtx, pInfo->pRow, pSup->numOfExprs, pSup->rowEntryInfoOffset);
82,175,977✔
343

344
        // check buffer size
345
        if (pRes->info.rows + pInfo->pRow->numOfRows >= pRes->info.capacity) {
82,165,001!
346
          int32_t newSize = pRes->info.rows + pInfo->pRow->numOfRows;
×
347
          code = blockDataEnsureCapacity(pRes, newSize);
×
348
          QUERY_CHECK_CODE(code, lino, _return);
×
349
        }
350

351
        code = copyResultrowToDataBlock(pSup->pExprInfo, pSup->numOfExprs, pInfo->pRow, pSup->pCtx, pRes,
82,165,001✔
352
                                        pSup->rowEntryInfoOffset, pTaskInfo);
82,165,001✔
353
        QUERY_CHECK_CODE(code, lino, _return);
82,106,334!
354

355
        pRes->info.rows += pInfo->pRow->numOfRows;
82,106,334✔
356
        pInfo->pRow->numOfRows = 0;
82,106,334✔
357

358
        pInfo->inWindow = false;
82,106,334✔
359
        rowIndex += 1;
82,106,334✔
360
      } else {
361
        code = doEventWindowAggImpl(pInfo, pSup, startIndex, pBlock->info.rows - 1, pBlock, tsList, pTaskInfo);
45,770✔
362
        QUERY_CHECK_CODE(code, lino, _return);
48,213!
363
      }
364
    } else {  // find the first start value that is fulfill for the start condition
365
      for (; rowIndex < pBlock->info.rows; ++rowIndex) {
106,228,637✔
366
        if (((bool*)ps->pData)[rowIndex]) {
106,179,339✔
367
          doKeepNewWindowStartInfo(pRowSup, tsList, rowIndex, gid);
82,145,791✔
368
          pInfo->inWindow = true;
82,160,086✔
369
          startIndex = rowIndex;
82,160,086✔
370
          if (pInfo->pRow != NULL) {
82,160,086✔
371
            clearResultRowInitFlag(pSup->pCtx, pSup->numOfExprs);
81,970,359✔
372
          }
373
          break;
82,195,197✔
374
        }
375
      }
376

377
      if (pInfo->inWindow) {
82,244,495✔
378
        continue;  // try to find the end position
82,196,585✔
379
      } else {
380
        break;  // no valid start position, quit
47,910✔
381
      }
382
    }
383
  }
384

385
_return:
216,282✔
386

387
  if (code != TSDB_CODE_SUCCESS) {
264,220!
388
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
389
  }
390
  colDataDestroy(ps);
264,220✔
391
  taosMemoryFree(ps);
270,845!
392
  colDataDestroy(pe);
270,845✔
393
  taosMemoryFree(pe);
270,845!
394

395
  return code;
270,844✔
396
}
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