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

taosdata / TDengine / #3587

23 Jan 2025 02:42AM UTC coverage: 63.549% (-0.09%) from 63.643%
#3587

push

travis-ci

web-flow
Merge pull request #29637 from taosdata/docs/TS-5944

docs/TS-5944 Correct typos in the descriptions of maximum and minimum values for taosBenchmark

141306 of 285630 branches covered (49.47%)

Branch coverage included in aggregate %.

219951 of 282844 relevant lines covered (77.76%)

19107446.53 hits per line

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

76.92
/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,
279,704✔
48
                                             SExecTaskInfo* pTaskInfo, SOperatorInfo** pOptrInfo) {
49
  QRY_PARAM_CHECK(pOptrInfo);
279,704!
50

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

60
  pOperator->exprSupp.hasWindowOrGroup = true;
279,704✔
61

62
  SEventWinodwPhysiNode* pEventWindowNode = (SEventWinodwPhysiNode*)physiNode;
279,704✔
63

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

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

71
  if (pEventWindowNode->window.pExprs != NULL) {
279,703✔
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);
279,703✔
82
  QUERY_CHECK_CODE(code, lino, _error);
279,701!
83

84
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
279,701✔
85

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

91
  initResultSizeInfo(&pOperator->resultInfo, 4096);
279,704✔
92

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

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

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

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

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

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

114
  pInfo->tsSlotId = tsSlotId;
279,703✔
115
  pInfo->pPreDataBlock = NULL;
279,703✔
116
  pInfo->pOperator = pOperator;
279,703✔
117

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

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

128
  *pOptrInfo = pOperator;
279,703✔
129
  return TSDB_CODE_SUCCESS;
279,703✔
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) {
279,704✔
142
  if (pInfo == NULL || pInfo->pRow == NULL || pOperator == NULL) {
279,704!
143
    return;
77,845✔
144
  }
145
  SExprSupp*       pSup = &pOperator->exprSupp;
201,859✔
146
  for (int32_t j = 0; j < pSup->numOfExprs; ++j) {
501,022✔
147
    pSup->pCtx[j].resultInfo = getResultEntryInfo(pInfo->pRow, j, pSup->rowEntryInfoOffset);
299,163✔
148
    if (pSup->pCtx[j].fpSet.cleanup) {
299,163!
149
      pSup->pCtx[j].fpSet.cleanup(&pSup->pCtx[j]);
×
150
    }
151
  }
152
}
153

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

160
  if (pInfo->pRow != NULL) {
279,704✔
161
    taosMemoryFree(pInfo->pRow);
201,859!
162
  }
163

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

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

174
  cleanupBasicInfo(&pInfo->binfo);
279,705✔
175
  colDataDestroy(&pInfo->twAggSup.timeWindowData);
279,703✔
176

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

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

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

193
  SSDataBlock* pRes = pInfo->binfo.pRes;
481,597✔
194

195
  blockDataCleanup(pRes);
481,597✔
196

197
  SOperatorInfo* downstream = pOperator->pDownstream[0];
481,598✔
198
  while (1) {
273,516✔
199
    SSDataBlock* pBlock = NULL;
755,114✔
200
    if (pInfo->pPreDataBlock == NULL) {
755,114✔
201
      pBlock = getNextBlockFromDownstream(pOperator, 0);
755,055✔
202
    } else {
203
      pBlock = pInfo->pPreDataBlock;
59✔
204
      pInfo->pPreDataBlock = NULL;
59✔
205
    }
206

207
    if (pBlock == NULL) {
755,113✔
208
      break;
481,548✔
209
    }
210

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

215
    code = blockDataUpdateTsWindow(pBlock, pInfo->tsSlotId);
273,565✔
216
    QUERY_CHECK_CODE(code, lino, _end);
273,564!
217

218
    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
219
    if (pInfo->scalarSup.pExprInfo != NULL) {
273,564✔
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);
273,564✔
226
    QUERY_CHECK_CODE(code, lino, _end);
273,565!
227

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

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

238
_end:
481,548✔
239
  if (code != TSDB_CODE_SUCCESS) {
481,548!
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;
481,548✔
245
  return code;
481,548✔
246
}
247

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

259
  (*pResult)->win = *win;
93,483,776✔
260

261
  return setResultRowInitCtx(*pResult, pExprSup->pCtx, pExprSup->numOfExprs, pExprSup->rowEntryInfoOffset);
93,483,776✔
262
}
263

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

270
  int32_t numOfOutput = pSup->numOfExprs;
93,477,570✔
271
  int32_t numOfRows = endIndex - startIndex + 1;
93,477,570✔
272

273
  doKeepTuple(pRowSup, tsList[endIndex], pBlock->info.id.groupId);
93,477,570✔
274

275
  code = setSingleOutputTupleBufv1(&pInfo->binfo.resultRowInfo, &pRowSup->win, &pInfo->pRow, pSup, &pInfo->aggSup);
93,484,604✔
276
  if (code != TSDB_CODE_SUCCESS) {  // null data, too many state code
93,458,675!
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);
93,458,675✔
282
  code = applyAggFunctionOnPartialTuples(pTaskInfo, pSup->pCtx, &pInfo->twAggSup.timeWindowData, startIndex, numOfRows,
93,457,537✔
283
                                         pBlock->info.rows, numOfOutput);
93,457,537✔
284
  return code;
93,476,567✔
285
}
286

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

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

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

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

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

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

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

330
  int32_t startIndex = pInfo->inWindow ? 0 : -1;
273,505✔
331
  while (rowIndex < pBlock->info.rows) {
187,109,704✔
332
    if (pInfo->inWindow) {  // let's find the first end value
186,894,347✔
333
      for (rowIndex = startIndex; rowIndex < pBlock->info.rows; ++rowIndex) {
117,468,683✔
334
        if (((bool*)pe->pData)[rowIndex]) {
117,423,505✔
335
          break;
93,414,660✔
336
        }
337
      }
338

339
      if (rowIndex < pBlock->info.rows) {
93,459,838✔
340
        code = doEventWindowAggImpl(pInfo, pSup, startIndex, rowIndex, pBlock, tsList, pTaskInfo);
93,415,752✔
341
        QUERY_CHECK_CODE(code, lino, _return);
93,428,818!
342
        doUpdateNumOfRows(pSup->pCtx, pInfo->pRow, pSup->numOfExprs, pSup->rowEntryInfoOffset);
93,428,818✔
343

344
        // check buffer size
345
        if (pRes->info.rows + pInfo->pRow->numOfRows >= pRes->info.capacity) {
93,410,680!
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,
93,410,680✔
352
                                        pSup->rowEntryInfoOffset, pTaskInfo);
93,410,680✔
353
        QUERY_CHECK_CODE(code, lino, _return);
93,343,032!
354

355
        pRes->info.rows += pInfo->pRow->numOfRows;
93,343,032✔
356
        pInfo->pRow->numOfRows = 0;
93,343,032✔
357

358
        pInfo->inWindow = false;
93,343,032✔
359
        rowIndex += 1;
93,343,032✔
360
      } else {
361
        code = doEventWindowAggImpl(pInfo, pSup, startIndex, pBlock->info.rows - 1, pBlock, tsList, pTaskInfo);
44,086✔
362
        QUERY_CHECK_CODE(code, lino, _return);
47,021!
363
      }
364
    } else {  // find the first start value that is fulfill for the start condition
365
      for (; rowIndex < pBlock->info.rows; ++rowIndex) {
119,875,896✔
366
        if (((bool*)ps->pData)[rowIndex]) {
119,824,875✔
367
          doKeepNewWindowStartInfo(pRowSup, tsList, rowIndex, gid);
93,383,488✔
368
          pInfo->inWindow = true;
93,399,800✔
369
          startIndex = rowIndex;
93,399,800✔
370
          if (pInfo->pRow != NULL) {
93,399,800✔
371
            clearResultRowInitFlag(pSup->pCtx, pSup->numOfExprs);
93,203,419✔
372
          }
373
          break;
93,443,852✔
374
        }
375
      }
376

377
      if (pInfo->inWindow) {
93,494,873✔
378
        continue;  // try to find the end position
93,446,146✔
379
      } else {
380
        break;  // no valid start position, quit
48,727✔
381
      }
382
    }
383
  }
384

385
_return:
215,357✔
386

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

395
  return code;
273,565✔
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