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

taosdata / TDengine / #3533

20 Nov 2024 07:11AM UTC coverage: 58.848% (-1.9%) from 60.78%
#3533

push

travis-ci

web-flow
Merge pull request #28823 from taosdata/fix/3.0/TD-32587

fix:[TD-32587]fix stmt segmentation fault

115578 of 252434 branches covered (45.79%)

Branch coverage included in aggregate %.

1 of 4 new or added lines in 1 file covered. (25.0%)

8038 existing lines in 233 files now uncovered.

194926 of 275199 relevant lines covered (70.83%)

1494459.59 hits per line

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

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

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

60
  pOperator->exprSupp.hasWindowOrGroup = true;
41✔
61

62
  SEventWinodwPhysiNode* pEventWindowNode = (SEventWinodwPhysiNode*)physiNode;
41✔
63

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

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

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

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

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

84
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
41✔
85

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

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

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

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

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

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

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

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

114
  pInfo->tsSlotId = tsSlotId;
41✔
115
  pInfo->pPreDataBlock = NULL;
41✔
116
  pInfo->pOperator = pOperator;
41✔
117

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

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

128
  *pOptrInfo = pOperator;
41✔
129
  return TSDB_CODE_SUCCESS;
41✔
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) {
41✔
142
  if (pInfo == NULL || pInfo->pRow == NULL || pOperator == NULL) {
41!
143
    return;
4✔
144
  }
145
  SExprSupp*       pSup = &pOperator->exprSupp;
37✔
146
  for (int32_t j = 0; j < pSup->numOfExprs; ++j) {
118✔
147
    pSup->pCtx[j].resultInfo = getResultEntryInfo(pInfo->pRow, j, pSup->rowEntryInfoOffset);
81✔
148
    if (pSup->pCtx[j].fpSet.cleanup) {
81!
149
      pSup->pCtx[j].fpSet.cleanup(&pSup->pCtx[j]);
×
150
    }
151
  }
152
}
153

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

160
  if (pInfo->pRow != NULL) {
41✔
161
    taosMemoryFree(pInfo->pRow);
37✔
162
  }
163

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

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

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

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

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

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

193
  SSDataBlock* pRes = pInfo->binfo.pRes;
99✔
194

195
  blockDataCleanup(pRes);
99✔
196

197
  SOperatorInfo* downstream = pOperator->pDownstream[0];
99✔
198
  while (1) {
235✔
199
    SSDataBlock* pBlock = NULL;
334✔
200
    if (pInfo->pPreDataBlock == NULL) {
334✔
201
      pBlock = getNextBlockFromDownstream(pOperator, 0);
214✔
202
    } else {
203
      pBlock = pInfo->pPreDataBlock;
120✔
204
      pInfo->pPreDataBlock = NULL;
120✔
205
    }
206

207
    if (pBlock == NULL) {
333✔
208
      break;
56✔
209
    }
210

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

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

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

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

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

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

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

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

259
  (*pResult)->win = *win;
387✔
260

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

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

270
  int32_t numOfOutput = pSup->numOfExprs;
387✔
271
  int32_t numOfRows = endIndex - startIndex + 1;
387✔
272

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

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

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

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

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

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

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

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

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

330
  int32_t startIndex = pInfo->inWindow ? 0 : -1;
157!
331
  while (rowIndex < pBlock->info.rows) {
931✔
332
    if (pInfo->inWindow) {  // let's find the first end value
774✔
333
      for (rowIndex = startIndex; rowIndex < pBlock->info.rows; ++rowIndex) {
2,203✔
334
        if (((bool*)pe->pData)[rowIndex]) {
2,101✔
335
          break;
285✔
336
        }
337
      }
338

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

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

355
        pRes->info.rows += pInfo->pRow->numOfRows;
285✔
356
        pInfo->pRow->numOfRows = 0;
285✔
357

358
        pInfo->inWindow = false;
285✔
359
        rowIndex += 1;
285✔
360
      } else {
361
        code = doEventWindowAggImpl(pInfo, pSup, startIndex, pBlock->info.rows - 1, pBlock, tsList, pTaskInfo);
102✔
362
        QUERY_CHECK_CODE(code, lino, _return);
102!
363
      }
364
    } else {  // find the first start value that is fulfill for the start condition
365
      for (; rowIndex < pBlock->info.rows; ++rowIndex) {
387!
366
        if (((bool*)ps->pData)[rowIndex]) {
387!
367
          doKeepNewWindowStartInfo(pRowSup, tsList, rowIndex, gid);
387✔
368
          pInfo->inWindow = true;
387✔
369
          startIndex = rowIndex;
387✔
370
          if (pInfo->pRow != NULL) {
387✔
371
            clearResultRowInitFlag(pSup->pCtx, pSup->numOfExprs);
350✔
372
          }
373
          break;
387✔
374
        }
375
      }
376

377
      if (pInfo->inWindow) {
387!
378
        continue;  // try to find the end position
387✔
379
      } else {
UNCOV
380
        break;  // no valid start position, quit
×
381
      }
382
    }
383
  }
384

385
_return:
157✔
386

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

395
  return code;
277✔
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