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

taosdata / TDengine / #3628

03 Mar 2025 05:52AM UTC coverage: 63.79% (+0.2%) from 63.596%
#3628

push

travis-ci

web-flow
Merge pull request #29946 from taosdata/fix/remove-sync-heartbeat-lock

fix: remove sync heartbeat lock

149328 of 300024 branches covered (49.77%)

Branch coverage included in aggregate %.

100 of 184 new or added lines in 14 files covered. (54.35%)

625 existing lines in 112 files now uncovered.

233817 of 300609 relevant lines covered (77.78%)

17520795.86 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,
259,818✔
32
                                             SExecTaskInfo* pTaskInfo, SOperatorInfo** pOptrInfo) {
33
  QRY_PARAM_CHECK(pOptrInfo);
259,818!
34

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

44
  pOperator->exprSupp.hasWindowOrGroup = true;
259,819✔
45

46
  SEventWinodwPhysiNode* pEventWindowNode = (SEventWinodwPhysiNode*)physiNode;
259,819✔
47

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

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

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

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

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

68
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
259,820✔
69

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

75
  initResultSizeInfo(&pOperator->resultInfo, 4096);
259,820✔
76

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

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

85
  code = blockDataEnsureCapacity(pResBlock, pOperator->resultInfo.capacity);
259,818✔
86
  QUERY_CHECK_CODE(code, lino, _error);
259,819!
87

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

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

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

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

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

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

113
  *pOptrInfo = pOperator;
259,818✔
114
  return TSDB_CODE_SUCCESS;
259,818✔
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) {
259,819✔
127
  if (pInfo == NULL || pInfo->pRow == NULL || pOperator == NULL) {
259,819!
128
    return;
70,526✔
129
  }
130
  SExprSupp*       pSup = &pOperator->exprSupp;
189,293✔
131
  for (int32_t j = 0; j < pSup->numOfExprs; ++j) {
472,056✔
132
    pSup->pCtx[j].resultInfo = getResultEntryInfo(pInfo->pRow, j, pSup->rowEntryInfoOffset);
282,763✔
133
    if (pSup->pCtx[j].fpSet.cleanup) {
282,763!
134
      pSup->pCtx[j].fpSet.cleanup(&pSup->pCtx[j]);
×
135
    }
136
  }
137
}
138

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

145
  if (pInfo->pRow != NULL) {
259,820✔
146
    taosMemoryFree(pInfo->pRow);
189,293!
147
  }
148

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

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

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

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

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

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

178
  SSDataBlock* pRes = pInfo->binfo.pRes;
449,145✔
179

180
  blockDataCleanup(pRes);
449,145✔
181

182
  SOperatorInfo* downstream = pOperator->pDownstream[0];
449,146✔
183
  while (1) {
237,680✔
184
    SSDataBlock* pBlock = NULL;
686,826✔
185
    if (pInfo->pPreDataBlock == NULL) {
686,826✔
186
      pBlock = getNextBlockFromDownstream(pOperator, 0);
686,767✔
187
    } else {
188
      pBlock = pInfo->pPreDataBlock;
59✔
189
      pInfo->pPreDataBlock = NULL;
59✔
190
    }
191

192
    if (pBlock == NULL) {
686,826✔
193
      break;
449,097✔
194
    }
195

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

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

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

210
    code = eventWindowAggImpl(pOperator, pInfo, pBlock);
237,728✔
211
    QUERY_CHECK_CODE(code, lino, _end);
237,729!
212

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

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

223
_end:
449,097✔
224
  if (code != TSDB_CODE_SUCCESS) {
449,097!
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;
449,097✔
230
  return code;
449,097✔
231
}
232

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

244
  (*pResult)->win = *win;
91,155,634✔
245

246
  return setResultRowInitCtx(*pResult, pExprSup->pCtx, pExprSup->numOfExprs, pExprSup->rowEntryInfoOffset);
91,155,634✔
247
}
248

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

255
  int32_t numOfOutput = pSup->numOfExprs;
91,152,912✔
256
  int32_t numOfRows = endIndex - startIndex + 1;
91,152,912✔
257

258
  doKeepTuple(pRowSup, tsList[endIndex], pBlock->info.id.groupId);
91,152,912✔
259

260
  code = setSingleOutputTupleBufv1(&pInfo->binfo.resultRowInfo, &pRowSup->win, &pInfo->pRow, pSup, &pInfo->aggSup);
91,155,605✔
261
  if (code != TSDB_CODE_SUCCESS) {  // null data, too many state code
91,151,227!
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);
91,151,227✔
267
  code = applyAggFunctionOnPartialTuples(pTaskInfo, pSup->pCtx, &pInfo->twAggSup.timeWindowData, startIndex, numOfRows,
91,149,749✔
268
                                         pBlock->info.rows, numOfOutput);
91,149,749✔
269
  return code;
91,164,652✔
270
}
271

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

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

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

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

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

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

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

316
  int32_t startIndex = pInfo->inWindow ? 0 : -1;
237,669✔
317
  while (rowIndex < pBlock->info.rows) {
182,471,394✔
318
    if (pInfo->inWindow) {  // let's find the first end value
182,277,154✔
319
      for (rowIndex = startIndex; rowIndex < pBlock->info.rows; ++rowIndex) {
114,496,904✔
320
        if (((bool*)pe->pData)[rowIndex]) {
114,459,487✔
321
          break;
91,103,615✔
322
        }
323
      }
324

325
      if (rowIndex < pBlock->info.rows) {
91,141,032✔
326
        code = doEventWindowAggImpl(pInfo, pSup, startIndex, rowIndex, pBlock, tsList, pTaskInfo);
91,104,390✔
327
        QUERY_CHECK_CODE(code, lino, _return);
91,125,676!
328
        doUpdateNumOfRows(pSup->pCtx, pInfo->pRow, pSup->numOfExprs, pSup->rowEntryInfoOffset);
91,125,676✔
329

330
        if (pRowSup->win.ekey - pRowSup->win.skey < minWindowSize) {
91,113,312✔
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) {
91,113,300!
UNCOV
336
            int32_t newSize = pRes->info.rows + pInfo->pRow->numOfRows;
×
UNCOV
337
            code = blockDataEnsureCapacity(pRes, newSize);
×
UNCOV
338
            QUERY_CHECK_CODE(code, lino, _return);
×
339
          }
340

341
          code = copyResultrowToDataBlock(pSup->pExprInfo, pSup->numOfExprs, pInfo->pRow, pSup->pCtx, pRes,
91,113,300✔
342
                                          pSup->rowEntryInfoOffset, pTaskInfo);
91,113,300✔
343
          QUERY_CHECK_CODE(code, lino, _return);
91,057,528!
344

345
          pRes->info.rows += pInfo->pRow->numOfRows;
91,057,528✔
346
        }
347
        pInfo->pRow->numOfRows = 0;
91,063,000✔
348

349
        pInfo->inWindow = false;
91,063,000✔
350
        rowIndex += 1;
91,063,000✔
351
      } else {
352
        code = doEventWindowAggImpl(pInfo, pSup, startIndex, pBlock->info.rows - 1, pBlock, tsList, pTaskInfo);
36,642✔
353
        QUERY_CHECK_CODE(code, lino, _return);
38,175!
354
      }
355
    } else {  // find the first start value that is fulfill for the start condition
356
      for (; rowIndex < pBlock->info.rows; ++rowIndex) {
116,290,009✔
357
        if (((bool*)ps->pData)[rowIndex]) {
116,247,003✔
358
          doKeepNewWindowStartInfo(pRowSup, tsList, rowIndex, gid);
91,093,116✔
359
          pInfo->inWindow = true;
91,103,955✔
360
          startIndex = rowIndex;
91,103,955✔
361
          if (pInfo->pRow != NULL) {
91,103,955✔
362
            clearResultRowInitFlag(pSup->pCtx, pSup->numOfExprs);
90,924,384✔
363
          }
364
          break;
91,131,567✔
365
        }
366
      }
367

368
      if (pInfo->inWindow) {
91,174,573✔
369
        continue;  // try to find the end position
91,132,550✔
370
      } else {
371
        break;  // no valid start position, quit
42,023✔
372
      }
373
    }
374
  }
375

376
_return:
194,240✔
377

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

386
  return code;
237,729✔
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