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

taosdata / TDengine / #3625

26 Feb 2025 10:19AM UTC coverage: 63.633% (+0.1%) from 63.485%
#3625

push

travis-ci

web-flow
Merge pull request #29914 from taosdata/feat/TS-5613-3.0

feat:[TS-5613]support bool in cast

148738 of 299799 branches covered (49.61%)

Branch coverage included in aggregate %.

233124 of 300297 relevant lines covered (77.63%)

17654074.26 hits per line

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

78.1
/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,
279,732✔
32
                                             SExecTaskInfo* pTaskInfo, SOperatorInfo** pOptrInfo) {
33
  QRY_PARAM_CHECK(pOptrInfo);
279,732!
34

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

44
  pOperator->exprSupp.hasWindowOrGroup = true;
279,733✔
45

46
  SEventWinodwPhysiNode* pEventWindowNode = (SEventWinodwPhysiNode*)physiNode;
279,733✔
47

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

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

55
  if (pEventWindowNode->window.pExprs != NULL) {
279,734✔
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);
279,734✔
66
  QUERY_CHECK_CODE(code, lino, _error);
279,734!
67

68
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
279,734✔
69

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

75
  initResultSizeInfo(&pOperator->resultInfo, 4096);
279,734✔
76

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

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

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

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

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

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

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

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

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

113
  *pOptrInfo = pOperator;
279,734✔
114
  return TSDB_CODE_SUCCESS;
279,734✔
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) {
279,733✔
127
  if (pInfo == NULL || pInfo->pRow == NULL || pOperator == NULL) {
279,733!
128
    return;
77,871✔
129
  }
130
  SExprSupp*       pSup = &pOperator->exprSupp;
201,862✔
131
  for (int32_t j = 0; j < pSup->numOfExprs; ++j) {
501,487✔
132
    pSup->pCtx[j].resultInfo = getResultEntryInfo(pInfo->pRow, j, pSup->rowEntryInfoOffset);
299,625✔
133
    if (pSup->pCtx[j].fpSet.cleanup) {
299,625!
134
      pSup->pCtx[j].fpSet.cleanup(&pSup->pCtx[j]);
×
135
    }
136
  }
137
}
138

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

145
  if (pInfo->pRow != NULL) {
279,734✔
146
    taosMemoryFree(pInfo->pRow);
201,863!
147
  }
148

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

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

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

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

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

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

178
  SSDataBlock* pRes = pInfo->binfo.pRes;
481,642✔
179

180
  blockDataCleanup(pRes);
481,642✔
181

182
  SOperatorInfo* downstream = pOperator->pDownstream[0];
481,643✔
183
  while (1) {
251,586✔
184
    SSDataBlock* pBlock = NULL;
733,229✔
185
    if (pInfo->pPreDataBlock == NULL) {
733,229✔
186
      pBlock = getNextBlockFromDownstream(pOperator, 0);
733,159✔
187
    } else {
188
      pBlock = pInfo->pPreDataBlock;
70✔
189
      pInfo->pPreDataBlock = NULL;
70✔
190
    }
191

192
    if (pBlock == NULL) {
733,229✔
193
      break;
481,572✔
194
    }
195

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

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

203
    // there is an scalar expression that needs to be calculated right before apply the group aggregation.
204
    if (pInfo->scalarSup.pExprInfo != NULL) {
251,657✔
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);
251,657✔
211
    QUERY_CHECK_CODE(code, lino, _end);
251,657!
212

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

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

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

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

244
  (*pResult)->win = *win;
62,634,547✔
245

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

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

255
  int32_t numOfOutput = pSup->numOfExprs;
62,631,308✔
256
  int32_t numOfRows = endIndex - startIndex + 1;
62,631,308✔
257

258
  doKeepTuple(pRowSup, tsList[endIndex], pBlock->info.id.groupId);
62,631,308✔
259

260
  code = setSingleOutputTupleBufv1(&pInfo->binfo.resultRowInfo, &pRowSup->win, &pInfo->pRow, pSup, &pInfo->aggSup);
62,632,672✔
261
  if (code != TSDB_CODE_SUCCESS) {  // null data, too many state code
62,632,482!
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);
62,632,482✔
267
  code = applyAggFunctionOnPartialTuples(pTaskInfo, pSup->pCtx, &pInfo->twAggSup.timeWindowData, startIndex, numOfRows,
62,631,769✔
268
                                         pBlock->info.rows, numOfOutput);
62,631,769✔
269
  return code;
62,636,594✔
270
}
271

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

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

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

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

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

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

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

316
  int32_t startIndex = pInfo->inWindow ? 0 : -1;
251,586✔
317
  while (rowIndex < pBlock->info.rows) {
125,481,794✔
318
    if (pInfo->inWindow) {  // let's find the first end value
125,277,338✔
319
      for (rowIndex = startIndex; rowIndex < pBlock->info.rows; ++rowIndex) {
79,687,193✔
320
        if (((bool*)pe->pData)[rowIndex]) {
79,643,058✔
321
          break;
62,582,701✔
322
        }
323
      }
324

325
      if (rowIndex < pBlock->info.rows) {
62,626,836✔
326
        code = doEventWindowAggImpl(pInfo, pSup, startIndex, rowIndex, pBlock, tsList, pTaskInfo);
62,583,087✔
327
        QUERY_CHECK_CODE(code, lino, _return);
62,591,837!
328
        doUpdateNumOfRows(pSup->pCtx, pInfo->pRow, pSup->numOfExprs, pSup->rowEntryInfoOffset);
62,591,837✔
329

330
        if (pRowSup->win.ekey - pRowSup->win.skey < minWindowSize) {
62,579,257✔
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) {
62,579,245!
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,
62,579,245✔
342
                                          pSup->rowEntryInfoOffset, pTaskInfo);
62,579,245✔
343
          QUERY_CHECK_CODE(code, lino, _return);
62,565,533!
344

345
          pRes->info.rows += pInfo->pRow->numOfRows;
62,565,533✔
346
        }
347
        pInfo->pRow->numOfRows = 0;
62,567,943✔
348

349
        pInfo->inWindow = false;
62,567,943✔
350
        rowIndex += 1;
62,567,943✔
351
      } else {
352
        code = doEventWindowAggImpl(pInfo, pSup, startIndex, pBlock->info.rows - 1, pBlock, tsList, pTaskInfo);
43,749✔
353
        QUERY_CHECK_CODE(code, lino, _return);
44,403!
354
      }
355
    } else {  // find the first start value that is fulfill for the start condition
356
      for (; rowIndex < pBlock->info.rows; ++rowIndex) {
80,141,775✔
357
        if (((bool*)ps->pData)[rowIndex]) {
80,094,864✔
358
          doKeepNewWindowStartInfo(pRowSup, tsList, rowIndex, gid);
62,603,591✔
359
          pInfo->inWindow = true;
62,606,794✔
360
          startIndex = rowIndex;
62,606,794✔
361
          if (pInfo->pRow != NULL) {
62,606,794✔
362
            clearResultRowInitFlag(pSup->pCtx, pSup->numOfExprs);
62,407,959✔
363
          }
364
          break;
62,617,420✔
365
        }
366
      }
367

368
      if (pInfo->inWindow) {
62,664,331✔
369
        continue;  // try to find the end position
62,617,862✔
370
      } else {
371
        break;  // no valid start position, quit
46,469✔
372
      }
373
    }
374
  }
375

376
_return:
204,456✔
377

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

386
  return code;
251,657✔
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