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

taosdata / TDengine / #3526

10 Nov 2024 03:50AM UTC coverage: 60.225% (-0.6%) from 60.818%
#3526

push

travis-ci

web-flow
Merge pull request #28709 from taosdata/main

merge: from main to 3.0 branch

117031 of 249004 branches covered (47.0%)

Branch coverage included in aggregate %.

130 of 169 new or added lines in 23 files covered. (76.92%)

4149 existing lines in 176 files now uncovered.

197577 of 273386 relevant lines covered (72.27%)

5840219.36 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

195
  blockDataCleanup(pRes);
197✔
196

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

207
    if (pBlock == NULL) {
614✔
208
      break;
117✔
209
    }
210

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

330
  int32_t startIndex = pInfo->inWindow ? 0 : -1;
286!
331
  while (rowIndex < pBlock->info.rows) {
1,388✔
332
    if (pInfo->inWindow) {  // let's find the first end value
1,106✔
333
      for (rowIndex = startIndex; rowIndex < pBlock->info.rows; ++rowIndex) {
3,417✔
334
        if (((bool*)pe->pData)[rowIndex]) {
3,243✔
335
          break;
377✔
336
        }
337
      }
338

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

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

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

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

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

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

385
_return:
282✔
386

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

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