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

taosdata / TDengine / #4925

12 Jan 2026 09:34AM UTC coverage: 66.107% (+0.8%) from 65.354%
#4925

push

travis-ci

web-flow
merge: from main to 3.0 branch #34248

103 of 129 new or added lines in 9 files covered. (79.84%)

891 existing lines in 139 files now uncovered.

200488 of 303278 relevant lines covered (66.11%)

129810096.48 hits per line

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

76.01
/source/libs/executor/src/externalwindowoperator.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 "operator.h"
18
#include "querytask.h"
19
#include "tdatablock.h"
20
#include "stream.h"
21
#include "filter.h"
22
#include "cmdnodes.h"
23

24
typedef struct SBlockList {
25
  const SSDataBlock* pSrcBlock;
26
  SList*             pBlocks;
27
  int32_t            blockRowNumThreshold;
28
} SBlockList;
29

30

31
typedef int32_t (*extWinGetWinFp)(SOperatorInfo*, int64_t*, int32_t*, SDataBlockInfo*, SExtWinTimeWindow**, int32_t*);
32

33
typedef struct SExtWindowStat {
34
  int64_t resBlockCreated;
35
  int64_t resBlockDestroyed;
36
  int64_t resBlockRecycled;
37
  int64_t resBlockReused;
38
  int64_t resBlockAppend;
39
} SExtWindowStat;
40

41
typedef struct SExternalWindowOperator {
42
  SOptrBasicInfo     binfo;
43
  SExprSupp          scalarSupp;
44
  int32_t            primaryTsIndex;
45
  EExtWinMode        mode;
46
  bool               multiTableMode;
47
  bool               inputHasOrder;
48
  SArray*            pWins;           // SArray<SExtWinTimeWindow>
49
  SArray*            pPseudoColInfo;  
50
  STimeRangeNode*    timeRangeExpr;
51
  
52
  extWinGetWinFp     getWinFp;
53

54
  bool               blkWinStartSet;
55
  int32_t            blkWinStartIdx;
56
  int32_t            blkWinIdx;
57
  int32_t            blkRowStartIdx;
58
  int32_t            outputWinId;
59
  int32_t            outputWinNum;
60
  int32_t            outWinIdx;
61

62
  // for project&indefRows
63
  SList*             pFreeBlocks;    // SList<SSDatablock*+SAarray*>
64
  SArray*            pOutputBlocks;  // SArray<SList*>, for each window, we have a list of blocks
65
  SListNode*         pLastBlkNode; 
66
  SSDataBlock*       pTmpBlock;
67
  
68
  // for agg
69
  SAggSupporter      aggSup;
70
  STimeWindowAggSupp twAggSup;
71

72
  int32_t            resultRowCapacity;
73
  SResultRow*        pResultRow;
74

75
  int64_t            lastSKey;
76
  int32_t            lastWinId;
77
  SSDataBlock*       pEmptyInputBlock;
78
  bool               hasCountFunc;
79
  SExtWindowStat     stat;
80
  SArray*            pWinRowIdx;
81

82
  // for vtable window query
83
  bool               isDynWindow;
84
  int32_t            orgTableVgId;
85
  tb_uid_t           orgTableUid;
86
  STimeWindow        orgTableTimeRange;
87
} SExternalWindowOperator;
88

89

90
static int32_t extWinBlockListAddBlock(SExternalWindowOperator* pExtW, SList* pList, int32_t rows, SSDataBlock** ppBlock, SArray** ppIdx) {
511✔
91
  SSDataBlock* pRes = NULL;
511✔
92
  int32_t code = 0, lino = 0;
511✔
93

94
  if (listNEles(pExtW->pFreeBlocks) > 0) {
511✔
95
    SListNode* pNode = tdListPopHead(pExtW->pFreeBlocks);
×
96
    *ppBlock = *(SSDataBlock**)pNode->data;
×
97
    *ppIdx = *(SArray**)((SArray**)pNode->data + 1);
×
98
    tdListAppendNode(pList, pNode);
×
99
    pExtW->stat.resBlockReused++;
×
100
  } else {
101
    TAOS_CHECK_EXIT(createOneDataBlock(pExtW->binfo.pRes, false, &pRes));
511✔
102
    TAOS_CHECK_EXIT(blockDataEnsureCapacity(pRes, TMAX(rows, 4096)));
511✔
103
    SArray* pIdx = taosArrayInit(10, sizeof(int64_t));
511✔
104
    TSDB_CHECK_NULL(pIdx, code, lino, _exit, terrno);
511✔
105
    void* res[2] = {pRes, pIdx};
511✔
106
    TAOS_CHECK_EXIT(tdListAppend(pList, res));
511✔
107

108
    *ppBlock = pRes;
511✔
109
    *ppIdx = pIdx;
511✔
110
    pExtW->stat.resBlockCreated++;
511✔
111
  }
112
  
113
_exit:
511✔
114

115
  if (code) {
511✔
116
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
117
    blockDataDestroy(pRes);
×
118
  }
119
  
120
  return code;
511✔
121
}
122

123
static int32_t extWinGetLastBlockFromList(SExternalWindowOperator* pExtW, SList* pList, int32_t rows, SSDataBlock** ppBlock, SArray** ppIdx) {
1,183✔
124
  int32_t    code = 0, lino = 0;
1,183✔
125
  SSDataBlock* pRes = NULL;
1,183✔
126

127
  SListNode* pNode = TD_DLIST_TAIL(pList);
1,183✔
128
  if (NULL == pNode) {
1,183✔
129
    TAOS_CHECK_EXIT(extWinBlockListAddBlock(pExtW, pList, rows, ppBlock, ppIdx));
511✔
130
    return code;
511✔
131
  }
132

133
  pRes = *(SSDataBlock**)pNode->data;
672✔
134
  if ((pRes->info.rows + rows) > pRes->info.capacity) {
672✔
135
    TAOS_CHECK_EXIT(extWinBlockListAddBlock(pExtW, pList, rows, ppBlock, ppIdx));
×
136
    return code;
×
137
  }
138

139
  *ppIdx = *(SArray**)((SSDataBlock**)pNode->data + 1);
672✔
140
  *ppBlock = pRes;
672✔
141
  pExtW->stat.resBlockAppend++;
672✔
142

143
_exit:
672✔
144

145
  if (code) {
672✔
146
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
147
  }
148
  
149
  return code;
672✔
150
}
151

152
static void extWinDestroyBlockList(void* p) {
11,755,520✔
153
  if (NULL == p) {
11,755,520✔
154
    return;
×
155
  }
156

157
  SListNode* pTmp = NULL;
11,755,520✔
158
  SList** ppList = (SList**)p;
11,755,520✔
159
  if ((*ppList) && TD_DLIST_NELES(*ppList) > 0) {
11,755,520✔
160
    SListNode* pNode = TD_DLIST_HEAD(*ppList);
×
161
    while (pNode) {
×
162
      SSDataBlock* pBlock = *(SSDataBlock**)pNode->data;
×
163
      blockDataDestroy(pBlock);
×
164
      SArray* pIdx = *(SArray**)((SArray**)pNode->data + 1);
×
165
      taosArrayDestroy(pIdx);
×
166
      pTmp = pNode;
×
167
      pNode = pNode->dl_next_;
×
168
      taosMemoryFree(pTmp);
×
169
    }
170
  }
171
  taosMemoryFree(*ppList);
11,755,520✔
172
}
173

174

175
static void extWinRecycleBlkNode(SExternalWindowOperator* pExtW, SListNode** ppNode) {
42,955,768✔
176
  if (NULL == ppNode || NULL == *ppNode) {
42,955,768✔
177
    return;
42,955,962✔
178
  }
179

180
  SSDataBlock* pBlock = *(SSDataBlock**)(*ppNode)->data;
13✔
181
  SArray* pIdx = *(SArray**)((SArray**)(*ppNode)->data + 1);
224✔
182
  
183
  if (listNEles(pExtW->pFreeBlocks) >= 10) {
224✔
184
    blockDataDestroy(pBlock);
×
185
    taosArrayDestroy(pIdx);
×
186
    taosMemoryFreeClear(*ppNode);
×
187
    pExtW->stat.resBlockDestroyed++;
×
188
    return;
×
189
  }
190
  
191
  blockDataCleanup(pBlock);
224✔
192
  taosArrayClear(pIdx);
224✔
193
  tdListPrependNode(pExtW->pFreeBlocks, *ppNode);
224✔
194
  *ppNode = NULL;
224✔
195
  pExtW->stat.resBlockRecycled++;
224✔
196
}
197

198
static void extWinRecycleBlockList(SExternalWindowOperator* pExtW, void* p) {
511✔
199
  if (NULL == p) {
511✔
200
    return;
×
201
  }
202

203
  SListNode* pTmp = NULL;
511✔
204
  SList** ppList = (SList**)p;
511✔
205
  if ((*ppList) && TD_DLIST_NELES(*ppList) > 0) {
511✔
206
    SListNode* pNode = TD_DLIST_HEAD(*ppList);
×
207
    while (pNode) {
×
208
      pTmp = pNode;
×
209
      pNode = pNode->dl_next_;
×
210
      extWinRecycleBlkNode(pExtW, &pTmp);
×
211
    }
212
  }
213
  taosMemoryFree(*ppList);
511✔
214
}
215
static void extWinDestroyBlkNode(SExternalWindowOperator* pInfo, SListNode* pNode) {
451,917✔
216
  if (NULL == pNode) {
451,917✔
217
    return;
451,406✔
218
  }
219

220
  SSDataBlock* pBlock = *(SSDataBlock**)pNode->data;
511✔
221
  SArray* pIdx = *(SArray**)((SArray**)pNode->data + 1);
511✔
222
  
223
  blockDataDestroy(pBlock);
511✔
224
  taosArrayDestroy(pIdx);
511✔
225

226
  taosMemoryFree(pNode);
511✔
227

228
  pInfo->stat.resBlockDestroyed++;
511✔
229
}
230

231

232
void destroyExternalWindowOperatorInfo(void* param) {
451,693✔
233
  if (NULL == param) {
451,693✔
234
    return;
×
235
  }
236
  SExternalWindowOperator* pInfo = (SExternalWindowOperator*)param;
451,693✔
237
  cleanupBasicInfo(&pInfo->binfo);
451,693✔
238

239
  taosArrayDestroyEx(pInfo->pOutputBlocks, extWinDestroyBlockList);
451,693✔
240
  taosArrayDestroy(pInfo->pWins);
451,693✔
241
  colDataDestroy(&pInfo->twAggSup.timeWindowData);
451,693✔
242
  taosArrayDestroy(pInfo->pWinRowIdx);
451,693✔
243
  
244
  taosArrayDestroy(pInfo->pPseudoColInfo);
451,693✔
245
  blockDataDestroy(pInfo->pTmpBlock);
451,693✔
246
  blockDataDestroy(pInfo->pEmptyInputBlock);
451,693✔
247

248
  extWinDestroyBlkNode(pInfo, pInfo->pLastBlkNode);
451,693✔
249
  if (pInfo->pFreeBlocks) {
451,693✔
250
    SListNode *node;
251
    while ((node = TD_DLIST_HEAD(pInfo->pFreeBlocks)) != NULL) {
511✔
252
      TD_DLIST_POP(pInfo->pFreeBlocks, node);
224✔
253
      extWinDestroyBlkNode(pInfo, node);
224✔
254
    }
255
    taosMemoryFree(pInfo->pFreeBlocks);
287✔
256
  }
257
  
258
  cleanupAggSup(&pInfo->aggSup);
451,693✔
259
  cleanupExprSupp(&pInfo->scalarSupp);
451,693✔
260
  taosMemoryFreeClear(pInfo->pResultRow);
451,693✔
261

262
  pInfo->binfo.resultRowInfo.openWindow = tdListFree(pInfo->binfo.resultRowInfo.openWindow);
451,693✔
263

264
  qDebug("ext window stat at destroy, created:%" PRId64 ", destroyed:%" PRId64 ", recycled:%" PRId64 ", reused:%" PRId64 ", append:%" PRId64, 
451,693✔
265
      pInfo->stat.resBlockCreated, pInfo->stat.resBlockDestroyed, pInfo->stat.resBlockRecycled, 
266
      pInfo->stat.resBlockReused, pInfo->stat.resBlockAppend);
267

268
  taosMemoryFreeClear(pInfo);
451,693✔
269
}
270

271
static int32_t extWinOpen(SOperatorInfo* pOperator);
272
static int32_t extWinNext(SOperatorInfo* pOperator, SSDataBlock** ppRes);
273

274
typedef struct SMergeAlignedExternalWindowOperator {
275
  SExternalWindowOperator* pExtW;
276
  int64_t curTs;
277
  SResultRow*  pResultRow;
278
} SMergeAlignedExternalWindowOperator;
279

280
void destroyMergeAlignedExternalWindowOperator(void* pOperator) {
2,149✔
281
  SMergeAlignedExternalWindowOperator* pMlExtInfo = (SMergeAlignedExternalWindowOperator*)pOperator;
2,149✔
282
  destroyExternalWindowOperatorInfo(pMlExtInfo->pExtW);
2,149✔
283
  taosMemoryFreeClear(pMlExtInfo);
2,149✔
284
}
2,149✔
285

286
int64_t* extWinExtractTsCol(SSDataBlock* pBlock, int32_t primaryTsIndex, SExecTaskInfo* pTaskInfo) {
40,327,748✔
287
  TSKEY* tsCols = NULL;
40,327,748✔
288

289
  if (pBlock->pDataBlock != NULL && pBlock->info.dataLoad) {
40,327,748✔
290
    SColumnInfoData* pColDataInfo = taosArrayGet(pBlock->pDataBlock, primaryTsIndex);
40,327,964✔
291
    if (!pColDataInfo) {
40,327,964✔
292
      pTaskInfo->code = terrno;
×
293
      T_LONG_JMP(pTaskInfo->env, terrno);
×
294
    }
295

296
    tsCols = (int64_t*)pColDataInfo->pData;
40,327,964✔
297
    if (pBlock->info.window.skey == 0 && pBlock->info.window.ekey == 0) {
40,327,964✔
298
      int32_t code = blockDataUpdateTsWindow(pBlock, primaryTsIndex);
40,327,964✔
299
      if (code != TSDB_CODE_SUCCESS) {
40,327,964✔
300
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
301
        pTaskInfo->code = code;
×
302
        T_LONG_JMP(pTaskInfo->env, code);
×
303
      }
304
    }
305
  }
306

307
  return tsCols;
40,327,964✔
308
}
309

310
static int32_t extWinGetCurWinIdx(SExecTaskInfo* pTaskInfo) {
2,147,483,647✔
311
  if (!pTaskInfo->pStreamRuntimeInfo) {
2,147,483,647✔
312
    return 0;
2,147,483,647✔
313
  }
314
  return pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
52,665,790✔
315
}
316

317
static void extWinIncCurWinIdx(SOperatorInfo* pOperator) {
×
318
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
×
319
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx++;
×
320
}
×
321

322
static void extWinSetCurWinIdx(SOperatorInfo* pOperator, int32_t idx) {
2,147,483,647✔
323
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
2,147,483,647✔
324
  if (pTaskInfo->pStreamRuntimeInfo) {
2,147,483,647✔
325
    pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = idx;
38,514,914✔
326
  }
327
}
2,147,483,647✔
328

329

330
static void extWinIncCurWinOutIdx(SStreamRuntimeInfo* pStreamRuntimeInfo) {
511✔
331
  pStreamRuntimeInfo->funcInfo.curOutIdx++;
511✔
332
}
511✔
333

334

335
static const STimeWindow* extWinGetNextWin(SExternalWindowOperator* pExtW, SExecTaskInfo* pTaskInfo) {
×
336
  int32_t curIdx = extWinGetCurWinIdx(pTaskInfo);
×
337
  if (curIdx + 1 >= pExtW->pWins->size) return NULL;
×
338
  return taosArrayGet(pExtW->pWins, curIdx + 1);
×
339
}
340

341

342
static int32_t extWinAppendWinIdx(SExecTaskInfo*       pTaskInfo, SArray* pIdx, SSDataBlock* pBlock, int32_t currWinIdx, int32_t rows) {
719,655,884✔
343
  int32_t  code = 0, lino = 0;
719,655,884✔
344
  int64_t* lastRes = taosArrayGetLast(pIdx);
719,655,884✔
345
  int32_t* lastWinIdx = (int32_t*)lastRes;
719,656,306✔
346
  int32_t* lastRowIdx = lastWinIdx ? (lastWinIdx + 1) : NULL;
719,656,306✔
347
  int64_t  res = 0;
719,656,517✔
348
  int32_t* pWinIdx = (int32_t*)&res;
719,656,517✔
349
  int32_t* pRowIdx = pWinIdx + 1;
719,656,517✔
350

351
  if (lastWinIdx && *lastWinIdx == currWinIdx) {
719,656,517✔
352
    return code;
668,189,454✔
353
  }
354

355
  *pWinIdx = currWinIdx;
51,467,063✔
356
  *pRowIdx = pBlock->info.rows - rows;
51,465,797✔
357

358
  TSDB_CHECK_NULL(taosArrayPush(pIdx, &res), code, lino, _exit, terrno);
51,461,999✔
359

360
_exit:
51,461,999✔
361

362
  if (code) {
51,461,999✔
363
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
364
  }
365

366
  return code;
51,461,999✔
367
}
368

369

370
static int32_t mergeAlignExtWinSetOutputBuf(SOperatorInfo* pOperator, SResultRowInfo* pResultRowInfo, const STimeWindow* pWin, SResultRow** pResult,
2,539,674✔
371
                                       SExprSupp* pExprSup, SAggSupporter* pAggSup) {
372
  if (*pResult == NULL) {
2,539,674✔
373
    *pResult = getNewResultRow(pAggSup->pResultBuf, &pAggSup->currentPageId, pAggSup->resultRowSize);
2,571✔
374
    if (!*pResult) {
2,571✔
375
      qError("get new resultRow failed, err:%s", tstrerror(terrno));
×
376
      return terrno;
×
377
    }
378
    pResultRowInfo->cur = (SResultRowPosition){.pageId = (*pResult)->pageId, .offset = (*pResult)->offset};
2,571✔
379
  }
380
  
381
  (*pResult)->win = *pWin;
2,539,674✔
382
  (*pResult)->winIdx = extWinGetCurWinIdx(pOperator->pTaskInfo);
2,539,674✔
383
  
384
  return setResultRowInitCtx((*pResult), pExprSup->pCtx, pExprSup->numOfExprs, pExprSup->rowEntryInfoOffset);
2,539,463✔
385
}
386

387

388
static int32_t mergeAlignExtWinGetWinFromTs(SOperatorInfo* pOperator, SExternalWindowOperator* pExtW, TSKEY ts, STimeWindow** ppWin) {
2,537,353✔
389
  int32_t blkWinIdx = extWinGetCurWinIdx(pOperator->pTaskInfo);
2,537,353✔
390
  
391
  // TODO handle desc order
392
  for (int32_t i = blkWinIdx; i < pExtW->pWins->size; ++i) {
5,067,282✔
393
    STimeWindow* pWin = taosArrayGet(pExtW->pWins, i);
5,061,163✔
394
    if (ts == pWin->skey) {
5,061,163✔
395
      extWinSetCurWinIdx(pOperator, i);
2,535,876✔
396
      *ppWin = pWin;
2,535,876✔
397
      return TSDB_CODE_SUCCESS;
2,536,298✔
398
    } else if (ts < pWin->skey) {
2,530,140✔
399
      qError("invalid ts %" PRId64 " for current window idx %d skey %" PRId64, ts, i, pWin->skey);
211✔
400
      return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
401
    }
402
  }
403
  
404
  qError("invalid ts %" PRId64 " to find merge aligned ext window, size:%d", ts, (int32_t)pExtW->pWins->size);
×
405
  return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
406
}
407

408
static int32_t mergeAlignExtWinFinalizeResult(SOperatorInfo* pOperator, SResultRowInfo* pResultRowInfo, SSDataBlock* pResultBlock) {
2,533,977✔
409
  int32_t        code = 0, lino = 0;
2,533,977✔
410
  SMergeAlignedExternalWindowOperator* pMlExtInfo = pOperator->info;
2,533,977✔
411
  SExternalWindowOperator*             pExtW = pMlExtInfo->pExtW;
2,533,977✔
412
  SExprSupp*     pSup = &pOperator->exprSupp;
2,533,977✔
413
  SResultRow*  pResultRow = pMlExtInfo->pResultRow;
2,533,766✔
414
  
415
  finalizeResultRows(pExtW->aggSup.pResultBuf, &pResultRowInfo->cur, pSup, pResultBlock, pOperator->pTaskInfo);
2,533,766✔
416
  
417
  if (pResultRow->numOfRows > 0) {
2,531,867✔
418
    TAOS_CHECK_EXIT(extWinAppendWinIdx(pOperator->pTaskInfo, pExtW->pWinRowIdx, pResultBlock, pResultRow->winIdx, pResultRow->numOfRows));
2,531,867✔
419
  }
420

421
_exit:
2,529,968✔
422

423
  if (code) {
2,529,968✔
424
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
425
  }
426

427
  return code;
2,529,968✔
428
}
429

430
static int32_t mergeAlignExtWinAggDo(SOperatorInfo* pOperator, SResultRowInfo* pResultRowInfo, SSDataBlock* pBlock, SSDataBlock* pResultBlock) {
9,534✔
431
  SMergeAlignedExternalWindowOperator* pMlExtInfo = pOperator->info;
9,534✔
432
  SExternalWindowOperator*             pExtW = pMlExtInfo->pExtW;
9,534✔
433

434
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
9,534✔
435
  SExprSupp*     pSup = &pOperator->exprSupp;
9,534✔
436
  int32_t        code = 0, lino = 0;
9,534✔
437
  STimeWindow *pWin = NULL;
9,534✔
438

439
  int32_t startPos = 0;
9,534✔
440
  int64_t* tsCols = extWinExtractTsCol(pBlock, pExtW->primaryTsIndex, pTaskInfo);
9,534✔
441
  TSKEY ts = getStartTsKey(&pBlock->info.window, tsCols);
9,534✔
442
  
443
  code = mergeAlignExtWinGetWinFromTs(pOperator, pExtW, ts, &pWin);
9,534✔
444
  if (code) {
9,534✔
445
    qError("failed to get time window for ts:%" PRId64 ", prim ts index:%d, error:%s", ts, pExtW->primaryTsIndex, tstrerror(code));
×
446
    TAOS_CHECK_EXIT(code);
×
447
  }
448

449
  if (pMlExtInfo->curTs != INT64_MIN && pMlExtInfo->curTs != pWin->skey) {
9,534✔
450
    TAOS_CHECK_EXIT(mergeAlignExtWinFinalizeResult(pOperator, pResultRowInfo, pResultBlock));
1,899✔
451
    resetResultRow(pMlExtInfo->pResultRow, pExtW->aggSup.resultRowSize - sizeof(SResultRow));
1,899✔
452
  }
453
  
454
  TAOS_CHECK_EXIT(mergeAlignExtWinSetOutputBuf(pOperator, pResultRowInfo, pWin, &pMlExtInfo->pResultRow, pSup, &pExtW->aggSup));
9,534✔
455

456
  int32_t currPos = startPos;
9,534✔
457
  pMlExtInfo->curTs = pWin->skey;
9,534✔
458
  
459
  while (++currPos < pBlock->info.rows) {
7,599,954✔
460
    if (tsCols[currPos] == pMlExtInfo->curTs) continue;
7,590,420✔
461

462
    qDebug("current ts:%" PRId64 ", startPos:%d, currPos:%d, tsCols[currPos]:%" PRId64,
2,530,140✔
463
      pMlExtInfo->curTs, startPos, currPos, tsCols[currPos]); 
464
    TAOS_CHECK_EXIT(applyAggFunctionOnPartialTuples(pTaskInfo, pSup->pCtx, &pExtW->twAggSup.timeWindowData, startPos,
2,530,140✔
465
                                           currPos - startPos, pBlock->info.rows, pSup->numOfExprs));
466

467
    TAOS_CHECK_EXIT(mergeAlignExtWinFinalizeResult(pOperator, pResultRowInfo, pResultBlock));
2,529,507✔
468
    resetResultRow(pMlExtInfo->pResultRow, pExtW->aggSup.resultRowSize - sizeof(SResultRow));
2,525,498✔
469

470
    TAOS_CHECK_EXIT(mergeAlignExtWinGetWinFromTs(pOperator, pExtW, tsCols[currPos], &pWin));
2,527,819✔
471
    
472
    qDebug("ext window align2 start:%" PRId64 ", end:%" PRId64, pWin->skey, pWin->ekey);
2,526,764✔
473
    startPos = currPos;
2,530,140✔
474
    
475
    TAOS_CHECK_EXIT(mergeAlignExtWinSetOutputBuf(pOperator, pResultRowInfo, pWin, &pMlExtInfo->pResultRow, pSup, &pExtW->aggSup));
2,530,140✔
476

477
    pMlExtInfo->curTs = pWin->skey;
2,530,140✔
478
  }
479

480
  code = applyAggFunctionOnPartialTuples(pTaskInfo, pSup->pCtx, &pExtW->twAggSup.timeWindowData, startPos,
19,068✔
481
                                         currPos - startPos, pBlock->info.rows, pSup->numOfExprs);
9,534✔
482

483
_exit:
9,534✔
484

485
  if (code != 0) {
9,534✔
486
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
487
    T_LONG_JMP(pTaskInfo->env, code);
×
488
  }
489
  
490
  return code;
9,534✔
491
}
492

493
static int32_t mergeAlignExtWinBuildWinRowIdx(SOperatorInfo* pOperator, SSDataBlock* pInput, SSDataBlock* pResult) {
×
494
  SExternalWindowOperator* pExtW = pOperator->info;
×
495
  int64_t* tsCols = extWinExtractTsCol(pInput, pExtW->primaryTsIndex, pOperator->pTaskInfo);
×
496
  STimeWindow* pWin = NULL;
×
497
  int32_t code = 0, lino = 0;
×
498
  int64_t prevTs = INT64_MIN;
×
499
  
500
  for (int32_t i = 0; i < pInput->info.rows; ++i) {
×
501
    if (prevTs == tsCols[i]) {
×
502
      continue;
×
503
    }
504
    
505
    TAOS_CHECK_EXIT(mergeAlignExtWinGetWinFromTs(pOperator, pExtW, tsCols[i], &pWin));
×
506
    TAOS_CHECK_EXIT(extWinAppendWinIdx(pOperator->pTaskInfo, pExtW->pWinRowIdx, pResult, extWinGetCurWinIdx(pOperator->pTaskInfo), pInput->info.rows - i));
×
507

508
    prevTs = tsCols[i];
×
509
  }
510

511
_exit:
×
512

513
  if (code != 0) {
×
514
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
515
  }
516

517
  return code;  
×
518
}
519

520
static int32_t mergeAlignExtWinProjectDo(SOperatorInfo* pOperator, SResultRowInfo* pResultRowInfo, SSDataBlock* pBlock,
×
521
                                            SSDataBlock* pResultBlock) {
522
  SExternalWindowOperator* pExtW = pOperator->info;
×
523
  SExprSupp*               pExprSup = &pExtW->scalarSupp;
×
524
  int32_t                  code = 0, lino = 0;
×
525
  
526
  TAOS_CHECK_EXIT(projectApplyFunctions(pExprSup->pExprInfo, pResultBlock, pBlock, pExprSup->pCtx, pExprSup->numOfExprs, NULL,
×
527
                        GET_STM_RTINFO(pOperator->pTaskInfo)));
528

529
  TAOS_CHECK_EXIT(mergeAlignExtWinBuildWinRowIdx(pOperator, pBlock, pResultBlock));
×
530

531
_exit:
×
532

533
  if (code != 0) {
×
534
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
535
  }
536

537
  return code;
×
538
}
539

540
void mergeAlignExtWinDo(SOperatorInfo* pOperator) {
3,626✔
541
  SExecTaskInfo*                       pTaskInfo = pOperator->pTaskInfo;
3,626✔
542
  SMergeAlignedExternalWindowOperator* pMlExtInfo = pOperator->info;
3,626✔
543
  SExternalWindowOperator*             pExtW = pMlExtInfo->pExtW;
3,626✔
544
  SResultRow*                          pResultRow = NULL;
3,626✔
545
  int32_t                              code = 0;
3,626✔
546
  SSDataBlock*                         pRes = pExtW->binfo.pRes;
3,626✔
547
  SExprSupp*                           pSup = &pOperator->exprSupp;
3,626✔
548
  int32_t                              lino = 0;
3,626✔
549

550
  taosArrayClear(pExtW->pWinRowIdx);
3,626✔
551
  blockDataCleanup(pRes);
3,626✔
552

553
  while (1) {
8,901✔
554
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
12,527✔
555

556
    if (pBlock == NULL) {
12,527✔
557
      // close last time window
558
      if (pMlExtInfo->curTs != INT64_MIN && EEXT_MODE_AGG == pExtW->mode) {
2,993✔
559
        TAOS_CHECK_EXIT(mergeAlignExtWinFinalizeResult(pOperator, &pExtW->binfo.resultRowInfo, pRes));
2,571✔
560
      }
561
      setOperatorCompleted(pOperator);
2,993✔
562
      break;
2,993✔
563
    }
564

565
    pRes->info.scanFlag = pBlock->info.scanFlag;
9,534✔
566
    code = setInputDataBlock(pSup, pBlock, pExtW->binfo.inputTsOrder, pBlock->info.scanFlag, true);
9,534✔
567
    QUERY_CHECK_CODE(code, lino, _exit);
9,534✔
568

569
    printDataBlock(pBlock, __func__, "externalwindowAlign", pTaskInfo->id.queryId);
9,534✔
570
    qDebug("ext windowpExtWAlign->scalarMode:%d", pExtW->mode);
9,534✔
571

572
    if (EEXT_MODE_SCALAR == pExtW->mode) {
9,534✔
573
      TAOS_CHECK_EXIT(mergeAlignExtWinProjectDo(pOperator, &pExtW->binfo.resultRowInfo, pBlock, pRes));
×
574
    } else {
575
      TAOS_CHECK_EXIT(mergeAlignExtWinAggDo(pOperator, &pExtW->binfo.resultRowInfo, pBlock, pRes));
9,534✔
576
    }
577

578
    if (pRes->info.rows >= pOperator->resultInfo.threshold) {
9,534✔
579
      break;
633✔
580
    }
581
  }
582

583
  pOperator->pTaskInfo->pStreamRuntimeInfo->funcInfo.pStreamBlkWinIdx = pExtW->pWinRowIdx;
3,626✔
584
  
585
_exit:
3,626✔
586

587
  if (code != 0) {
3,626✔
588
    qError("%s failed at line %d since:%s", __func__, lino, tstrerror(code));
×
589
    pTaskInfo->code = code;
×
590
    T_LONG_JMP(pTaskInfo->env, code);
×
591
  }
592
}
3,626✔
593

594
static int32_t mergeAlignExtWinNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
6,197✔
595
  SExecTaskInfo*                       pTaskInfo = pOperator->pTaskInfo;
6,197✔
596
  SMergeAlignedExternalWindowOperator* pMlExtInfo = pOperator->info;
6,197✔
597
  SExternalWindowOperator*             pExtW = pMlExtInfo->pExtW;
6,197✔
598
  int32_t                              code = 0;
6,197✔
599
  int32_t lino = 0;
6,197✔
600

601
  if (pOperator->status == OP_EXEC_DONE) {
6,197✔
602
    (*ppRes) = NULL;
2,571✔
603
    return TSDB_CODE_SUCCESS;
2,571✔
604
  }
605

606
  SSDataBlock* pRes = pExtW->binfo.pRes;
3,626✔
607
  blockDataCleanup(pRes);
3,626✔
608

609
  if (taosArrayGetSize(pExtW->pWins) <= 0) {
3,626✔
610
    size_t size = taosArrayGetSize(pTaskInfo->pStreamRuntimeInfo->funcInfo.pStreamPesudoFuncVals);
2,993✔
611
    STimeWindow* pWin = taosArrayReserve(pExtW->pWins, size);
2,993✔
612
    TSDB_CHECK_NULL(pWin, code, lino, _exit, terrno);
2,993✔
613

614
    for (int32_t i = 0; i < size; ++i) {
2,538,025✔
615
      SSTriggerCalcParam* pParam = taosArrayGet(pTaskInfo->pStreamRuntimeInfo->funcInfo.pStreamPesudoFuncVals, i);
2,535,032✔
616
      pWin[i].skey = pParam->wstart;
2,535,032✔
617
      pWin[i].ekey = pParam->wstart + 1;
2,535,032✔
618
    }
619
    
620
    pExtW->outputWinId = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
2,993✔
621
  }
622

623
  mergeAlignExtWinDo(pOperator);
3,626✔
624
  
625
  size_t rows = pRes->info.rows;
3,626✔
626
  pOperator->resultInfo.totalRows += rows;
3,626✔
627
  (*ppRes) = (rows == 0) ? NULL : pRes;
3,626✔
628

629
_exit:
3,626✔
630

631
  if (code != 0) {
3,626✔
632
    qError("%s failed at line %d since:%s", __func__, lino, tstrerror(code));
×
633
    pTaskInfo->code = code;
×
634
    T_LONG_JMP(pTaskInfo->env, code);
×
635
  }
636
  return code;
3,626✔
637
}
638

639
int32_t resetMergeAlignedExtWinOperator(SOperatorInfo* pOperator) {
3,837✔
640
  SMergeAlignedExternalWindowOperator* pMlExtInfo = pOperator->info;
3,837✔
641
  SExternalWindowOperator*             pExtW = pMlExtInfo->pExtW;
3,837✔
642
  SExecTaskInfo*                       pTaskInfo = pOperator->pTaskInfo;
3,837✔
643
  SMergeAlignedIntervalPhysiNode * pPhynode = (SMergeAlignedIntervalPhysiNode*)pOperator->pPhyNode;
3,837✔
644
  pOperator->status = OP_NOT_OPENED;
3,837✔
645

646
  taosArrayClear(pExtW->pWins);
3,837✔
647

648
  resetBasicOperatorState(&pExtW->binfo);
3,837✔
649
  pMlExtInfo->pResultRow = NULL;
3,837✔
650
  pMlExtInfo->curTs = INT64_MIN;
3,837✔
651

652
  int32_t code = resetAggSup(&pOperator->exprSupp, &pExtW->aggSup, pTaskInfo, pPhynode->window.pFuncs, NULL,
7,674✔
653
                             sizeof(int64_t) * 2 + POINTER_BYTES, pTaskInfo->id.str, pTaskInfo->streamInfo.pState,
3,837✔
654
                             &pTaskInfo->storageAPI.functionStore);
655
  if (code == 0) {
3,837✔
656
    colDataDestroy(&pExtW->twAggSup.timeWindowData);
3,837✔
657
    code = initExecTimeWindowInfo(&pExtW->twAggSup.timeWindowData, &pTaskInfo->window);
3,837✔
658
  }
659
  return code;
3,837✔
660
}
661

662
int32_t createMergeAlignedExternalWindowOperator(SOperatorInfo* pDownstream, SPhysiNode* pNode,
2,149✔
663
                                                 SExecTaskInfo* pTaskInfo, SOperatorInfo** ppOptrOut) {
664
  SMergeAlignedIntervalPhysiNode* pPhynode = (SMergeAlignedIntervalPhysiNode*)pNode;
2,149✔
665
  int32_t code = 0;
2,149✔
666
  int32_t lino = 0;
2,149✔
667
  SMergeAlignedExternalWindowOperator* pMlExtInfo = taosMemoryCalloc(1, sizeof(SMergeAlignedExternalWindowOperator));
2,149✔
668
  SOperatorInfo*                       pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
2,149✔
669

670
  if (pTaskInfo->pStreamRuntimeInfo != NULL){
2,149✔
671
    pTaskInfo->pStreamRuntimeInfo->funcInfo.withExternalWindow = true;
2,149✔
672
  }
673
  pOperator->pPhyNode = pNode;
2,149✔
674
  if (!pMlExtInfo || !pOperator) {
2,149✔
675
    code = terrno;
×
676
    goto _error;
×
677
  }
678

679
  pMlExtInfo->pExtW = taosMemoryCalloc(1, sizeof(SExternalWindowOperator));
2,149✔
680
  if (!pMlExtInfo->pExtW) {
2,149✔
681
    code = terrno;
×
682
    goto _error;
×
683
  }
684

685
  SExternalWindowOperator* pExtW = pMlExtInfo->pExtW;
2,149✔
686
  SExprSupp* pSup = &pOperator->exprSupp;
2,149✔
687
  pSup->hasWindowOrGroup = true;
2,149✔
688
  pSup->hasWindow = true;
2,149✔
689
  pMlExtInfo->curTs = INT64_MIN;
2,149✔
690

691
  pExtW->primaryTsIndex = ((SColumnNode*)pPhynode->window.pTspk)->slotId;
2,149✔
692
  pExtW->mode = pPhynode->window.pProjs ? EEXT_MODE_SCALAR : EEXT_MODE_AGG;
2,149✔
693
  pExtW->binfo.inputTsOrder = pPhynode->window.node.inputTsOrder = TSDB_ORDER_ASC;
2,149✔
694
  pExtW->binfo.outputTsOrder = pExtW->binfo.inputTsOrder;
2,149✔
695

696
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
2,149✔
697
  initResultSizeInfo(&pOperator->resultInfo, 4096);
2,149✔
698

699
  int32_t num = 0;
2,149✔
700
  SExprInfo* pExprInfo = NULL;
2,149✔
701
  code = createExprInfo(pPhynode->window.pFuncs, NULL, &pExprInfo, &num);
2,149✔
702
  QUERY_CHECK_CODE(code, lino, _error);
2,149✔
703

704
  if (pExtW->mode == EEXT_MODE_AGG) {
2,149✔
705
    code = initAggSup(pSup, &pExtW->aggSup, pExprInfo, num, keyBufSize, pTaskInfo->id.str, pTaskInfo->streamInfo.pState,
2,149✔
706
                      &pTaskInfo->storageAPI.functionStore);
707
    QUERY_CHECK_CODE(code, lino, _error);
2,149✔
708
  }
709

710
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pPhynode->window.node.pOutputDataBlockDesc);
2,149✔
711
  QUERY_CHECK_NULL(pResBlock, code, lino, _error, terrno);
2,149✔
712
  initBasicInfo(&pExtW->binfo, pResBlock);
2,149✔
713

714
  pExtW->pWins = taosArrayInit(4096, sizeof(STimeWindow));
2,149✔
715
  if (!pExtW->pWins) QUERY_CHECK_CODE(terrno, lino, _error);
2,149✔
716

717
  pExtW->pWinRowIdx = taosArrayInit(4096, sizeof(int64_t));
2,149✔
718
  TSDB_CHECK_NULL(pExtW->pWinRowIdx, code, lino, _error, terrno);
2,149✔
719

720
  initResultRowInfo(&pExtW->binfo.resultRowInfo);
2,149✔
721
  code = blockDataEnsureCapacity(pExtW->binfo.pRes, pOperator->resultInfo.capacity);
2,149✔
722
  QUERY_CHECK_CODE(code, lino, _error);
2,149✔
723
  setOperatorInfo(pOperator, "MergeAlignedExternalWindowOperator", QUERY_NODE_PHYSICAL_PLAN_EXTERNAL_WINDOW, false, OP_NOT_OPENED, pMlExtInfo, pTaskInfo);
2,149✔
724
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, mergeAlignExtWinNext, NULL,
2,149✔
725
                                         destroyMergeAlignedExternalWindowOperator, optrDefaultBufFn, NULL,
726
                                         optrDefaultGetNextExtFn, NULL);
727
  setOperatorResetStateFn(pOperator, resetMergeAlignedExtWinOperator);
2,149✔
728

729
  code = appendDownstream(pOperator, &pDownstream, 1);
2,149✔
730
  QUERY_CHECK_CODE(code, lino, _error);
2,149✔
731
  *ppOptrOut = pOperator;
2,149✔
732
  return code;
2,149✔
733
  
734
_error:
×
735
  if (pMlExtInfo) destroyMergeAlignedExternalWindowOperator(pMlExtInfo);
×
736
  destroyOperatorAndDownstreams(pOperator, &pDownstream, 1);
×
737
  pTaskInfo->code = code;
×
738
  return code;
×
739
}
740

741
static int32_t resetExternalWindowExprSupp(SExternalWindowOperator* pExtW, SExecTaskInfo* pTaskInfo,
338,147✔
742
                                           SExternalWindowPhysiNode* pPhynode) {
743
  int32_t    code = 0, lino = 0, num = 0;
338,147✔
744
  SExprInfo* pExprInfo = NULL;
338,362✔
745
  cleanupExprSuppWithoutFilter(&pExtW->scalarSupp);
338,362✔
746

747
  SNodeList* pNodeList = NULL;
338,362✔
748
  if (pPhynode->window.pProjs) {
338,362✔
749
    pNodeList = pPhynode->window.pProjs;
×
750
  } else {
751
    pNodeList = pPhynode->window.pExprs;
338,362✔
752
  }
753

754
  code = createExprInfo(pNodeList, NULL, &pExprInfo, &num);
338,362✔
755
  QUERY_CHECK_CODE(code, lino, _error);
338,362✔
756
  code = initExprSupp(&pExtW->scalarSupp, pExprInfo, num, &pTaskInfo->storageAPI.functionStore);
338,362✔
757
  QUERY_CHECK_CODE(code, lino, _error);
338,362✔
758
  return code;
338,362✔
759
_error:
×
760
  if (code != TSDB_CODE_SUCCESS) {
×
761
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
762
    pTaskInfo->code = code;
×
763
  }
764
  return code;
×
765
}
766

767
static int32_t resetExternalWindowOperator(SOperatorInfo* pOperator) {
338,362✔
768
  int32_t code = 0, lino = 0;
338,362✔
769
  SExternalWindowOperator* pExtW = pOperator->info;
338,362✔
770
  SExecTaskInfo*           pTaskInfo = pOperator->pTaskInfo;
338,362✔
771
  SExternalWindowPhysiNode* pPhynode = (SExternalWindowPhysiNode*)pOperator->pPhyNode;
338,362✔
772
  pOperator->status = OP_NOT_OPENED;
338,362✔
773

774
  //resetBasicOperatorState(&pExtW->binfo);
775
  initResultRowInfo(&pExtW->binfo.resultRowInfo);
338,362✔
776

777
  pExtW->outputWinId = 0;
338,362✔
778
  pExtW->lastWinId = -1;
338,362✔
779
  pExtW->outputWinNum = 0;
338,362✔
780
  taosArrayClear(pExtW->pWins);
338,362✔
781
  extWinRecycleBlkNode(pExtW, &pExtW->pLastBlkNode);
338,362✔
782

783
/*
784
  int32_t code = blockDataEnsureCapacity(pExtW->binfo.pRes, pOperator->resultInfo.capacity);
785
  if (code == 0) {
786
    code = resetAggSup(&pOperator->exprSupp, &pExtW->aggSup, pTaskInfo, pPhynode->window.pFuncs, NULL,
787
                       sizeof(int64_t) * 2 + POINTER_BYTES, pTaskInfo->id.str, pTaskInfo->streamInfo.pState,
788
                       &pTaskInfo->storageAPI.functionStore);
789
  }
790
*/
791
  TAOS_CHECK_EXIT(resetExternalWindowExprSupp(pExtW, pTaskInfo, pPhynode));
338,362✔
792
  colDataDestroy(&pExtW->twAggSup.timeWindowData);
338,362✔
793
  TAOS_CHECK_EXIT(initExecTimeWindowInfo(&pExtW->twAggSup.timeWindowData, &pTaskInfo->window));
338,362✔
794

795
  pExtW->outWinIdx = 0;
337,586✔
796
  pExtW->lastSKey = INT64_MIN;
337,989✔
797
  pExtW->isDynWindow = false;
338,362✔
798

799
  qDebug("%s ext window stat at reset, created:%" PRId64 ", destroyed:%" PRId64 ", recycled:%" PRId64 ", reused:%" PRId64 ", append:%" PRId64, 
338,362✔
800
      pTaskInfo->id.str, pExtW->stat.resBlockCreated, pExtW->stat.resBlockDestroyed, pExtW->stat.resBlockRecycled, 
801
      pExtW->stat.resBlockReused, pExtW->stat.resBlockAppend);
802

803
_exit:
7,352✔
804

805
  if (code) {
338,362✔
806
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
807
  }
808
  
809
  return code;
338,362✔
810
}
811

812
static EDealRes extWinHasCountLikeFunc(SNode* pNode, void* res) {
2,162,431✔
813
  if (QUERY_NODE_FUNCTION == nodeType(pNode)) {
2,162,431✔
814
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
791,838✔
815
    if (fmIsCountLikeFunc(pFunc->funcId) || (pFunc->hasOriginalFunc && fmIsCountLikeFunc(pFunc->originalFuncId))) {
791,838✔
816
      *(bool*)res = true;
240,321✔
817
      return DEAL_RES_END;
241,165✔
818
    }
819
  }
820
  return DEAL_RES_CONTINUE;
1,925,044✔
821
}
822

823

824
static int32_t extWinCreateEmptyInputBlock(SOperatorInfo* pOperator, SSDataBlock** ppBlock) {
241,165✔
825
  int32_t code = TSDB_CODE_SUCCESS;
241,165✔
826
  int32_t lino = 0;
241,165✔
827
  SSDataBlock* pBlock = NULL;
241,165✔
828
  if (!tsCountAlwaysReturnValue) {
241,376✔
829
    return TSDB_CODE_SUCCESS;
×
830
  }
831

832
  SExternalWindowOperator* pExtW = pOperator->info;
241,376✔
833

834
  if (!pExtW->hasCountFunc) {
241,165✔
835
    return TSDB_CODE_SUCCESS;
×
836
  }
837

838
  code = createDataBlock(&pBlock);
241,165✔
839
  if (code) {
240,954✔
840
    return code;
×
841
  }
842

843
  pBlock->info.rows = 1;
240,954✔
844
  pBlock->info.capacity = 0;
241,165✔
845

846
  for (int32_t i = 0; i < pOperator->exprSupp.numOfExprs; ++i) {
1,045,493✔
847
    SColumnInfoData colInfo = {0};
804,328✔
848
    colInfo.hasNull = true;
804,113✔
849
    colInfo.info.type = TSDB_DATA_TYPE_NULL;
804,113✔
850
    colInfo.info.bytes = 1;
804,113✔
851

852
    SExprInfo* pOneExpr = &pOperator->exprSupp.pExprInfo[i];
804,113✔
853
    for (int32_t j = 0; j < pOneExpr->base.numOfParams; ++j) {
1,622,703✔
854
      SFunctParam* pFuncParam = &pOneExpr->base.pParam[j];
818,168✔
855
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
818,379✔
856
        int32_t slotId = pFuncParam->pCol->slotId;
737,720✔
857
        int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
737,720✔
858
        if (slotId >= numOfCols) {
737,931✔
859
          code = taosArrayEnsureCap(pBlock->pDataBlock, slotId + 1);
568,860✔
860
          QUERY_CHECK_CODE(code, lino, _end);
568,860✔
861

862
          for (int32_t k = numOfCols; k < slotId + 1; ++k) {
1,326,317✔
863
            void* tmp = taosArrayPush(pBlock->pDataBlock, &colInfo);
757,242✔
864
            QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
757,457✔
865
          }
866
        }
867
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
80,870✔
868
        // do nothing
869
      }
870
    }
871
  }
872

873
  code = blockDataEnsureCapacity(pBlock, pBlock->info.rows);
241,376✔
874
  QUERY_CHECK_CODE(code, lino, _end);
241,376✔
875

876
  for (int32_t i = 0; i < blockDataGetNumOfCols(pBlock); ++i) {
998,622✔
877
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
757,035✔
878
    QUERY_CHECK_NULL(pColInfoData, code, lino, _end, terrno);
757,246✔
879
    colDataSetNULL(pColInfoData, 0);
880
  }
881
  *ppBlock = pBlock;
241,376✔
882

883
_end:
240,950✔
884
  if (code != TSDB_CODE_SUCCESS) {
241,376✔
885
    blockDataDestroy(pBlock);
×
886
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
887
  }
888
  return code;
241,165✔
889
}
890

891

892

893
static int extWinTsWinCompare(const void* pLeft, const void* pRight) {
9,734,848✔
894
  int64_t ts = *(int64_t*)pLeft;
9,734,848✔
895
  SExtWinTimeWindow* pWin = (SExtWinTimeWindow*)pRight;
9,734,848✔
896
  if (ts < pWin->tw.skey) {
9,734,848✔
897
    return -1;
2,495,117✔
898
  }
899
  if (ts >= pWin->tw.ekey) {
7,239,731✔
900
    return 1;
4,917,971✔
901
  }
902

903
  return 0;
2,321,760✔
904
}
905

906

907
static int32_t extWinGetMultiTbWinFromTs(SOperatorInfo* pOperator, SExternalWindowOperator* pExtW, int64_t* tsCol, int64_t rowNum, int32_t* startPos) {
1,811,083✔
908
  int32_t idx = taosArraySearchIdx(pExtW->pWins, tsCol, extWinTsWinCompare, TD_EQ);
1,811,083✔
909
  if (idx >= 0) {
1,810,867✔
910
    *startPos = 0;
1,424,815✔
911
    return idx;
1,424,815✔
912
  }
913

914
  SExtWinTimeWindow* pWin = NULL;
386,052✔
915
  int32_t w = 0;
386,052✔
916
  for (int64_t i = 1; i < rowNum; ++i) {
1,328,847,699✔
917
    for (; w < pExtW->pWins->size; ++w) {
2,147,483,647✔
918
      pWin = TARRAY_GET_ELEM(pExtW->pWins, w);
1,161,435,846✔
919
      if (tsCol[i] < pWin->tw.skey) {
1,161,435,846✔
920
        break;
34,028,997✔
921
      }
922
      
923
      if (tsCol[i] < pWin->tw.ekey) {
1,127,406,849✔
924
        *startPos = i;
25,515✔
925
        return w;
25,515✔
926
      }
927
    }
928
  }
929

930
  return -1;
360,537✔
931
}
932

933
static int32_t extWinGetNoOvlpWin(SOperatorInfo* pOperator, int64_t* tsCol, int32_t* startPos, SDataBlockInfo* pInfo, SExtWinTimeWindow** ppWin, int32_t* winRows) {
990,615,084✔
934
  SExternalWindowOperator* pExtW = pOperator->info;
990,615,084✔
935
  if ((*startPos) >= pInfo->rows) {
990,615,084✔
936
    qDebug("%s %s blk rowIdx %d reach the end, size: %d, skip block", 
24,341,057✔
937
        GET_TASKID(pOperator->pTaskInfo), __func__, *startPos, (int32_t)pInfo->rows);
938
    *ppWin = NULL;
24,341,057✔
939
    return TSDB_CODE_SUCCESS;
24,341,057✔
940
  }
941
  
942
  if (pExtW->blkWinIdx < 0) {
966,274,027✔
943
    pExtW->blkWinIdx = extWinGetCurWinIdx(pOperator->pTaskInfo);
37,536,216✔
944
  } else {
945
    pExtW->blkWinIdx++;
928,737,811✔
946
  }
947

948
  if (pExtW->blkWinIdx >= pExtW->pWins->size) {
966,274,027✔
949
    qDebug("%s %s ext win blk idx %d reach the end, size: %d, skip block", 
13,174,105✔
950
        GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, (int32_t)pExtW->pWins->size);
951
    *ppWin = NULL;
13,174,105✔
952
    return TSDB_CODE_SUCCESS;
13,174,105✔
953
  }
954
  
955
  SExtWinTimeWindow* pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
953,099,922✔
956
  if (tsCol[pInfo->rows - 1] < pWin->tw.skey) {
953,099,922✔
957
    qDebug("%s %s block end ts %" PRId64 " is small than curr win %d skey %" PRId64 ", skip block", 
20,340✔
958
        GET_TASKID(pOperator->pTaskInfo), __func__, tsCol[pInfo->rows - 1], pExtW->blkWinIdx, pWin->tw.skey);
959
    *ppWin = NULL;
20,340✔
960
    return TSDB_CODE_SUCCESS;
20,340✔
961
  }
962

963
  int32_t r = *startPos;
953,079,582✔
964

965
  qDebug("%s %s start to get novlp win from winIdx %d rowIdx %d", GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, r);
953,079,582✔
966

967
  // TODO handle desc order
968
  for (; pExtW->blkWinIdx < pExtW->pWins->size; ++pExtW->blkWinIdx) {
953,085,530✔
969
    pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
953,085,530✔
970
    for (; r < pInfo->rows; ++r) {
1,030,650,703✔
971
      if (tsCol[r] < pWin->tw.skey) {
1,030,649,989✔
972
        continue;
77,565,173✔
973
      }
974

975
      if (tsCol[r] < pWin->tw.ekey) {
953,084,816✔
976
        extWinSetCurWinIdx(pOperator, pExtW->blkWinIdx);
504,369,428✔
977
        *ppWin = pWin;
504,369,428✔
978
        *startPos = r;
504,369,428✔
979
        *winRows = getNumOfRowsInTimeWindow(pInfo, tsCol, r, pWin->tw.ekey - 1, binarySearchForKey, NULL, pExtW->binfo.inputTsOrder);
504,369,428✔
980

981
        qDebug("%s %s the %dth ext win TR[%" PRId64 ", %" PRId64 ") got %d rows rowStartidx %d ts[%" PRId64 ", %" PRId64 "] in blk", 
504,369,428✔
982
            GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, pWin->tw.skey, pWin->tw.ekey, *winRows, r, tsCol[r], tsCol[r + *winRows - 1]);
983
        
984
        return TSDB_CODE_SUCCESS;
504,369,428✔
985
      }
986

987
      if (!pOperator->pTaskInfo->pStreamRuntimeInfo && tsCol[r] >= pWin->tw.ekey) {
448,715,388✔
988
        extWinSetCurWinIdx(pOperator, pExtW->blkWinIdx);
448,709,440✔
989
        *ppWin = pWin;
448,709,440✔
990
        *startPos = r;
448,709,440✔
991
        *winRows = getNumOfRowsInTimeWindow(pInfo, tsCol, r, pWin->tw.ekey - 1, binarySearchForKey, NULL, pExtW->binfo.inputTsOrder);
448,709,440✔
992

993
        qDebug("%s %s the %dth ext win TR[%" PRId64 ", %" PRId64 ") got %d rows rowStartidx %d ts[%" PRId64 ", %" PRId64 "] in blk",
448,709,440✔
994
               GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, pWin->tw.skey, pWin->tw.ekey, *winRows, r, tsCol[r], tsCol[r + *winRows - 1]);
995

996
        return TSDB_CODE_SUCCESS;
448,709,440✔
997
      }
998

999
      break;
5,948✔
1000
    }
1001

1002
    if (r == pInfo->rows) {
6,662✔
1003
      break;
714✔
1004
    }
1005
  }
1006

1007
  qDebug("%s %s no more ext win in block, TR[%" PRId64 ", %" PRId64 "), skip it", 
714✔
1008
      GET_TASKID(pOperator->pTaskInfo), __func__, tsCol[0], tsCol[pInfo->rows - 1]);
1009

1010
  *ppWin = NULL;
714✔
1011
  return TSDB_CODE_SUCCESS;
714✔
1012
}
1013

1014
static int32_t extWinGetOvlpWin(SOperatorInfo* pOperator, int64_t* tsCol, int32_t* startPos, SDataBlockInfo* pInfo, SExtWinTimeWindow** ppWin, int32_t* winRows) {
190,278✔
1015
  SExternalWindowOperator* pExtW = pOperator->info;
190,278✔
1016
  if (pExtW->blkWinIdx < 0) {
190,278✔
1017
    pExtW->blkWinIdx = pExtW->blkWinStartIdx;
10,887✔
1018
  } else {
1019
    pExtW->blkWinIdx++;
179,391✔
1020
  }
1021

1022
  if (pExtW->blkWinIdx >= pExtW->pWins->size) {
190,278✔
1023
    qDebug("%s %s ext win blk idx %d reach the end, size: %d, skip block", 
10,173✔
1024
        GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, (int32_t)pExtW->pWins->size);
1025
    *ppWin = NULL;
10,173✔
1026
    return TSDB_CODE_SUCCESS;
10,173✔
1027
  }
1028
  
1029
  SExtWinTimeWindow* pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
180,105✔
1030
  if (tsCol[pInfo->rows - 1] < pWin->tw.skey) {
180,105✔
1031
    qDebug("%s %s block end ts %" PRId64 " is small than curr win %d skey %" PRId64 ", skip block", 
×
1032
        GET_TASKID(pOperator->pTaskInfo), __func__, tsCol[pInfo->rows - 1], pExtW->blkWinIdx, pWin->tw.skey);
1033
    *ppWin = NULL;
×
1034
    return TSDB_CODE_SUCCESS;
×
1035
  }
1036

1037
  int64_t r = 0;
180,105✔
1038

1039
  qDebug("%s %s start to get ovlp win from winIdx %d rowIdx %d", GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, pExtW->blkRowStartIdx);
180,105✔
1040
  
1041
  // TODO handle desc order
1042
  for (; pExtW->blkWinIdx < pExtW->pWins->size; ++pExtW->blkWinIdx) {
180,819✔
1043
    pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
180,819✔
1044
    for (r = pExtW->blkRowStartIdx; r < pInfo->rows; ++r) {
313,623✔
1045
      if (tsCol[r] < pWin->tw.skey) {
312,909✔
1046
        pExtW->blkRowStartIdx = r + 1;
132,804✔
1047
        continue;
132,804✔
1048
      }
1049

1050
      if (tsCol[r] < pWin->tw.ekey) {
180,105✔
1051
        extWinSetCurWinIdx(pOperator, pExtW->blkWinIdx);
179,391✔
1052
        *ppWin = pWin;
179,391✔
1053
        *startPos = r;
179,391✔
1054
        *winRows = getNumOfRowsInTimeWindow(pInfo, tsCol, r, pWin->tw.ekey - 1, binarySearchForKey, NULL, pExtW->binfo.inputTsOrder);
179,391✔
1055

1056
        qDebug("%s %s the %dth ext win TR[%" PRId64 ", %" PRId64 ") got %d rows rowStartidx %d ts[%" PRId64 ", %" PRId64 "] in blk", 
179,391✔
1057
            GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, pWin->tw.skey, pWin->tw.ekey, *winRows, (int32_t)r, tsCol[r], tsCol[r + *winRows - 1]);
1058
        
1059
        if ((r + *winRows) < pInfo->rows) {
179,391✔
1060
          pExtW->blkWinStartIdx = pExtW->blkWinIdx + 1;
169,218✔
1061
          pExtW->blkWinStartSet = true;
169,218✔
1062
        }
1063
        
1064
        return TSDB_CODE_SUCCESS;
179,391✔
1065
      }
1066

1067
      break;
714✔
1068
    }
1069

1070
    if (r >= pInfo->rows) {
1,428✔
1071
      if (!pExtW->blkWinStartSet) {
714✔
1072
        pExtW->blkWinStartIdx = pExtW->blkWinIdx;
714✔
1073
      }
1074
      
1075
      break;
714✔
1076
    }
1077
  }
1078

1079
  qDebug("%s %s no more ext win in block, TR[%" PRId64 ", %" PRId64 "), skip it", 
714✔
1080
      GET_TASKID(pOperator->pTaskInfo), __func__, tsCol[0], tsCol[pInfo->rows - 1]);
1081

1082
  *ppWin = NULL;
714✔
1083
  return TSDB_CODE_SUCCESS;
714✔
1084
}
1085

1086

1087
static int32_t extWinGetMultiTbNoOvlpWin(SOperatorInfo* pOperator, int64_t* tsCol, int32_t* startPos, SDataBlockInfo* pInfo, SExtWinTimeWindow** ppWin, int32_t* winRows) {
165,502,424✔
1088
  SExternalWindowOperator* pExtW = pOperator->info;
165,502,424✔
1089
  if ((*startPos) >= pInfo->rows) {
165,505,001✔
1090
    qDebug("%s %s blk rowIdx %d reach the end, size: %d, skip block", 
1,419,361✔
1091
        GET_TASKID(pOperator->pTaskInfo), __func__, *startPos, (int32_t)pInfo->rows);
1092
    *ppWin = NULL;
1,419,361✔
1093
    return TSDB_CODE_SUCCESS;
1,419,361✔
1094
  }
1095
  
1096
  if (pExtW->blkWinIdx < 0) {
164,087,129✔
1097
    pExtW->blkWinIdx = extWinGetMultiTbWinFromTs(pOperator, pExtW, tsCol, pInfo->rows, startPos);
1,811,083✔
1098
    if (pExtW->blkWinIdx < 0) {
1,810,867✔
1099
      qDebug("%s %s blk TR[%" PRId64 ", %" PRId64 ") not in any win, skip block", 
360,537✔
1100
          GET_TASKID(pOperator->pTaskInfo), __func__, tsCol[0], tsCol[pInfo->rows - 1]);
1101
      *ppWin = NULL;
360,537✔
1102
      return TSDB_CODE_SUCCESS;
360,537✔
1103
    }
1104

1105
    extWinSetCurWinIdx(pOperator, pExtW->blkWinIdx);
1,450,115✔
1106
    *ppWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
1,450,330✔
1107
    *winRows = getNumOfRowsInTimeWindow(pInfo, tsCol, *startPos, (*ppWin)->tw.ekey - 1, binarySearchForKey, NULL, pExtW->binfo.inputTsOrder);
1,450,330✔
1108

1109
    qDebug("%s %s the %dth ext win TR[%" PRId64 ", %" PRId64 ") got %d rows rowStartidx %d ts[%" PRId64 ", %" PRId64 "] in blk", 
1,450,331✔
1110
        GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, (*ppWin)->tw.skey, (*ppWin)->tw.ekey, *winRows, *startPos, tsCol[*startPos], tsCol[*startPos + *winRows - 1]);
1111
    
1112
    return TSDB_CODE_SUCCESS;
1,450,546✔
1113
  } else {
1114
    pExtW->blkWinIdx++;
162,274,119✔
1115
  }
1116

1117
  if (pExtW->blkWinIdx >= pExtW->pWins->size) {
162,276,261✔
1118
    qDebug("%s %s ext win blk idx %d reach the end, size: %d, skip block", 
1,134✔
1119
        GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, (int32_t)pExtW->pWins->size);
1120
    *ppWin = NULL;
1,134✔
1121
    return TSDB_CODE_SUCCESS;
1,134✔
1122
  }
1123
  
1124
  SExtWinTimeWindow* pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
162,273,849✔
1125
  if (tsCol[pInfo->rows - 1] < pWin->tw.skey) {
162,273,630✔
1126
    qDebug("%s %s block end ts %" PRId64 " is small than curr win %d skey %" PRId64 ", skip block", 
30,051✔
1127
        GET_TASKID(pOperator->pTaskInfo), __func__, tsCol[pInfo->rows - 1], pExtW->blkWinIdx, pWin->tw.skey);
1128
    *ppWin = NULL;
30,051✔
1129
    return TSDB_CODE_SUCCESS;
30,051✔
1130
  }
1131

1132
  int32_t r = *startPos;
162,244,224✔
1133

1134
  qDebug("%s %s start to get mnovlp win from winIdx %d rowIdx %d", GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, r);
162,244,869✔
1135

1136
  // TODO handle desc order
1137
  for (; pExtW->blkWinIdx < pExtW->pWins->size; ++pExtW->blkWinIdx) {
162,256,807✔
1138
    pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
162,257,026✔
1139
    for (; r < pInfo->rows; ++r) {
235,271,196✔
1140
      if (tsCol[r] < pWin->tw.skey) {
235,271,407✔
1141
        continue;
73,014,596✔
1142
      }
1143

1144
      if (tsCol[r] < pWin->tw.ekey) {
162,256,815✔
1145
        extWinSetCurWinIdx(pOperator, pExtW->blkWinIdx);
162,245,506✔
1146
        *ppWin = pWin;
162,244,861✔
1147
        *startPos = r;
162,244,861✔
1148
        *winRows = getNumOfRowsInTimeWindow(pInfo, tsCol, r, pWin->tw.ekey - 1, binarySearchForKey, NULL, pExtW->binfo.inputTsOrder);
162,244,861✔
1149

1150
        qDebug("%s %s the %dth ext win TR[%" PRId64 ", %" PRId64 ") got %d rows rowStartidx %d ts[%" PRId64 ", %" PRId64 "] in blk", 
162,243,806✔
1151
            GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, pWin->tw.skey, pWin->tw.ekey, *winRows, r, tsCol[r], tsCol[r + *winRows - 1]);
1152
        
1153
        return TSDB_CODE_SUCCESS;
162,245,713✔
1154
      }
1155

1156
      if (!pOperator->pTaskInfo->pStreamRuntimeInfo && tsCol[r] >= pWin->tw.ekey) {
11,520✔
1157
        extWinSetCurWinIdx(pOperator, pExtW->blkWinIdx);
×
1158
        *ppWin = pWin;
×
1159
        *startPos = r;
×
1160
        *winRows = getNumOfRowsInTimeWindow(pInfo, tsCol, r, pWin->tw.ekey - 1, binarySearchForKey, NULL, pExtW->binfo.inputTsOrder);
×
1161

1162
        qDebug("%s %s the %dth ext win TR[%" PRId64 ", %" PRId64 ") got %d rows rowStartidx %d ts[%" PRId64 ", %" PRId64 "] in blk",
×
1163
               GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, pWin->tw.skey, pWin->tw.ekey, *winRows, r, tsCol[r], tsCol[r + *winRows - 1]);
1164

1165
        return TSDB_CODE_SUCCESS;
×
1166
      }
1167

1168
      break;
11,520✔
1169
    }
1170

1171
    if (r == pInfo->rows) {
11,200✔
1172
      break;
×
1173
    }
1174
  }
1175

UNCOV
1176
  qDebug("%s %s no more ext win in block, TR[%" PRId64 ", %" PRId64 "), skip it", 
×
1177
      GET_TASKID(pOperator->pTaskInfo), __func__, tsCol[0], tsCol[pInfo->rows - 1]);
1178

UNCOV
1179
  *ppWin = NULL;
×
1180
  return TSDB_CODE_SUCCESS;
×
1181
}
1182

1183
static int32_t extWinGetFirstWinFromTs(SOperatorInfo* pOperator, SExternalWindowOperator* pExtW, int64_t* tsCol,
960,244✔
1184
                                       int64_t rowNum, int32_t* startPos) {
1185
  SExtWinTimeWindow* pWin = NULL;
960,244✔
1186
  int32_t            idx = taosArraySearchIdx(pExtW->pWins, tsCol, extWinTsWinCompare, TD_EQ);
960,244✔
1187
  if (idx >= 0) {
960,244✔
1188
    for (int i = idx - 1; i >= 0; --i) {
896,944✔
1189
      pWin = TARRAY_GET_ELEM(pExtW->pWins, i);
×
1190
      if (extWinTsWinCompare(tsCol, pWin) == 0) {
×
1191
        idx = i;
×
1192
      } else {
1193
        break;
×
1194
      }
1195
    }
1196
    *startPos = 0;
896,944✔
1197
    return idx;
896,944✔
1198
  }
1199

1200
  pWin = NULL;
63,300✔
1201
  int32_t w = 0;
63,300✔
1202
  for (int64_t i = 1; i < rowNum; ++i) {
127,022✔
1203
    for (; w < pExtW->pWins->size; ++w) {
148,122✔
1204
      pWin = TARRAY_GET_ELEM(pExtW->pWins, w);
148,122✔
1205
      if (tsCol[i] < pWin->tw.skey) {
148,122✔
1206
        break;
63,722✔
1207
      }
1208

1209
      if (tsCol[i] < pWin->tw.ekey) {
84,400✔
1210
        *startPos = i;
21,100✔
1211
        return w;
21,100✔
1212
      }
1213
    }
1214
  }
1215

1216
  return -1;
42,200✔
1217
}
1218

1219
static int32_t extWinGetMultiTbOvlpWin(SOperatorInfo* pOperator, int64_t* tsCol, int32_t* startPos, SDataBlockInfo* pInfo, SExtWinTimeWindow** ppWin, int32_t* winRows) {
5,668,243✔
1220
  SExternalWindowOperator* pExtW = pOperator->info;
5,668,243✔
1221
  if (pExtW->blkWinIdx < 0) {
5,669,086✔
1222
    pExtW->blkWinIdx = extWinGetFirstWinFromTs(pOperator, pExtW, tsCol, pInfo->rows, startPos);
960,244✔
1223
    if (pExtW->blkWinIdx < 0) {
960,244✔
1224
      qDebug("%s %s blk TR[%" PRId64 ", %" PRId64 ") not in any win, skip block", 
42,200✔
1225
          GET_TASKID(pOperator->pTaskInfo), __func__, tsCol[0], tsCol[pInfo->rows - 1]);
1226
      *ppWin = NULL;
42,200✔
1227
      return TSDB_CODE_SUCCESS;
42,200✔
1228
    }
1229

1230
    extWinSetCurWinIdx(pOperator, pExtW->blkWinIdx);
918,044✔
1231
    *ppWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
918,044✔
1232
    *winRows = getNumOfRowsInTimeWindow(pInfo, tsCol, *startPos, (*ppWin)->tw.ekey - 1, binarySearchForKey, NULL, pExtW->binfo.inputTsOrder);
918,044✔
1233
    
1234
    qDebug("%s %s the %dth ext win TR[%" PRId64 ", %" PRId64 ") got %d rows rowStartidx %d ts[%" PRId64 ", %" PRId64 "] in blk", 
918,044✔
1235
        GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, (*ppWin)->tw.skey, (*ppWin)->tw.ekey, *winRows, *startPos, tsCol[*startPos], tsCol[*startPos + *winRows - 1]);
1236
    
1237
    return TSDB_CODE_SUCCESS;
918,044✔
1238
  } else {
1239
    pExtW->blkWinIdx++;
4,708,838✔
1240
  }
1241

1242
  if (pExtW->blkWinIdx >= pExtW->pWins->size) {
4,708,838✔
1243
    qDebug("%s %s ext win blk idx %d reach the end, size: %d, skip block", 
918,044✔
1244
        GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, (int32_t)pExtW->pWins->size);
1245
    *ppWin = NULL;
918,044✔
1246
    return TSDB_CODE_SUCCESS;
918,044✔
1247
  }
1248
  
1249
  SExtWinTimeWindow* pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
3,791,212✔
1250
  if (tsCol[pInfo->rows - 1] < pWin->tw.skey) {
3,791,002✔
1251
    qDebug("%s %s block end ts %" PRId64 " is small than curr win %d skey %" PRId64 ", skip block", 
×
1252
        GET_TASKID(pOperator->pTaskInfo), __func__, tsCol[pInfo->rows - 1], pExtW->blkWinIdx, pWin->tw.skey);
1253
    *ppWin = NULL;
×
1254
    return TSDB_CODE_SUCCESS;
×
1255
  }
1256

1257
  int64_t r = 0;
3,790,791✔
1258

1259
  qDebug("%s %s start to get movlp win from winIdx %d rowIdx %d", GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, pExtW->blkRowStartIdx);
3,790,791✔
1260

1261
  // TODO handle desc order
1262
  for (; pExtW->blkWinIdx < pExtW->pWins->size; ++pExtW->blkWinIdx) {
3,853,101✔
1263
    pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
3,853,101✔
1264
    for (r = pExtW->blkRowStartIdx; r < pInfo->rows; ++r) {
12,968,008✔
1265
      if (tsCol[r] < pWin->tw.skey) {
12,968,008✔
1266
        pExtW->blkRowStartIdx = r + 1;
9,115,329✔
1267
        continue;
9,115,118✔
1268
      }
1269

1270
      if (tsCol[r] < pWin->tw.ekey) {
3,853,101✔
1271
        extWinSetCurWinIdx(pOperator, pExtW->blkWinIdx);
3,791,001✔
1272
        *ppWin = pWin;
3,791,001✔
1273
        *startPos = r;
3,791,001✔
1274
        *winRows = getNumOfRowsInTimeWindow(pInfo, tsCol, r, pWin->tw.ekey - 1, binarySearchForKey, NULL, pExtW->binfo.inputTsOrder);
3,791,001✔
1275

1276
        qDebug("%s %s the %dth ext win TR[%" PRId64 ", %" PRId64 ") got %d rows rowStartidx %d ts[%" PRId64 ", %" PRId64 "] in blk", 
3,791,001✔
1277
            GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, pWin->tw.skey, pWin->tw.ekey, *winRows, (int32_t)r, tsCol[r], tsCol[r + *winRows - 1]);
1278
        
1279
        return TSDB_CODE_SUCCESS;
3,791,212✔
1280
      }
1281

1282
      break;
62,100✔
1283
    }
1284

1285
    if (r >= pInfo->rows) {
62,100✔
1286
      break;
×
1287
    }
1288
  }
1289

1290
  qDebug("%s %s no more ext win in block, TR[%" PRId64 ", %" PRId64 "), skip it", 
×
1291
      GET_TASKID(pOperator->pTaskInfo), __func__, tsCol[0], tsCol[pInfo->rows - 1]);
1292

1293
  *ppWin = NULL;
×
1294
  return TSDB_CODE_SUCCESS;
×
1295
}
1296

1297

1298
static int32_t extWinGetWinStartPos(STimeWindow win, const SDataBlockInfo* pBlockInfo, int32_t lastEndPos, int32_t order, int32_t* nextPos, int64_t* tsCol) {
×
1299
  bool ascQuery = order == TSDB_ORDER_ASC;
×
1300

1301
  if (win.ekey <= pBlockInfo->window.skey && ascQuery) {
×
1302
    return -2;
×
1303
  }
1304
//if (win.skey > pBlockInfo->window.ekey && !ascQuery) return -2;
1305

1306
  if (win.skey > pBlockInfo->window.ekey && ascQuery) return -1;
×
1307
//if (win.ekey < pBlockInfo->window.skey && !ascQuery) return -1;
1308

1309
  while (true) {
1310
    if (win.ekey <= tsCol[lastEndPos + 1] && ascQuery) return -2;
×
1311
    if (win.skey <= tsCol[lastEndPos + 1] && ascQuery) break;
×
1312
    lastEndPos++;
×
1313
  }
1314

1315
  *nextPos = lastEndPos + 1;
×
1316
  return 0;
×
1317
}
1318

1319
static int32_t extWinAggSetWinOutputBuf(SOperatorInfo* pOperator, SExtWinTimeWindow* win, SExprSupp* pSupp, 
1,488,245,286✔
1320
                                     SAggSupporter* pAggSup, SExecTaskInfo* pTaskInfo) {
1321
  int32_t code = 0, lino = 0;
1,488,245,286✔
1322
  SResultRow* pResultRow = NULL;
1,488,245,286✔
1323
  SExternalWindowOperator* pExtW = (SExternalWindowOperator*)pOperator->info;
1,488,245,286✔
1324
  
1325
#if 0
1326
  SResultRow* pResultRow = doSetResultOutBufByKey(pAggSup->pResultBuf, pResultRowInfo, (char*)&win->skey, TSDB_KEYSIZE,
1327
                                                  true, tableGroupId, pTaskInfo, true, pAggSup, true);
1328
  if (pResultRow == NULL) {
1329
    qError("failed to set result output buffer, error:%s", tstrerror(pTaskInfo->code));
1330
    return pTaskInfo->code;
1331
  }
1332

1333
  qDebug("current result rows num:%d", tSimpleHashGetSize(pAggSup->pResultRowHashTable));
1334

1335
#else
1336
  if (win->winOutIdx >= 0) {
1,488,245,286✔
1337
    pResultRow = (SResultRow*)((char*)pExtW->pResultRow + win->winOutIdx * pAggSup->resultRowSize);
771,118,232✔
1338
  } else {
1339
    win->winOutIdx = pExtW->outWinIdx++;
717,126,843✔
1340
    
1341
    qDebug("set window [%" PRId64 ", %" PRId64 "] outIdx:%d", win->tw.skey, win->tw.ekey, win->winOutIdx);
717,127,054✔
1342

1343
    pResultRow = (SResultRow*)((char*)pExtW->pResultRow + win->winOutIdx * pAggSup->resultRowSize);
717,126,843✔
1344
    
1345
    memset(pResultRow, 0, pAggSup->resultRowSize);
717,127,265✔
1346

1347
    pResultRow->winIdx = extWinGetCurWinIdx(pOperator->pTaskInfo);
717,127,265✔
1348
    TAOS_SET_POBJ_ALIGNED(&pResultRow->win, &win->tw);
717,125,577✔
1349
  }
1350
#endif
1351

1352
  // set time window for current result
1353
  TAOS_CHECK_EXIT(setResultRowInitCtx(pResultRow, pSupp->pCtx, pSupp->numOfExprs, pSupp->rowEntryInfoOffset));
1,488,244,231✔
1354

1355
_exit:
1,488,243,363✔
1356
  
1357
  if (code) {
1,488,243,363✔
1358
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1359
  }
1360

1361
  return code;
1,488,242,519✔
1362
}
1363

1364
static int32_t extWinAggDo(SOperatorInfo* pOperator, int32_t startPos, int32_t forwardRows,
1,489,457,230✔
1365
                                  SSDataBlock* pInputBlock) {
1366
  if (pOperator->pTaskInfo->pStreamRuntimeInfo && forwardRows == 0) {
1,489,457,230✔
1367
    return TSDB_CODE_SUCCESS;
×
1368
  }
1369

1370
  SExprSupp*               pSup = &pOperator->exprSupp;
1,489,457,023✔
1371
  SExternalWindowOperator* pExtW = pOperator->info;
1,489,458,504✔
1372
  return applyAggFunctionOnPartialTuples(pOperator->pTaskInfo, pSup->pCtx, &pExtW->twAggSup.timeWindowData, startPos,
2,147,483,647✔
1373
                                         forwardRows, pInputBlock->info.rows, pSup->numOfExprs);
1,489,458,715✔
1374

1375
}
1376

1377
static bool extWinLastWinClosed(SExternalWindowOperator* pExtW) {
511✔
1378
  if (pExtW->outWinIdx <= 0 || (pExtW->multiTableMode && !pExtW->inputHasOrder)) {
511✔
1379
    return false;
511✔
1380
  }
1381

1382
  if (NULL == pExtW->timeRangeExpr || !pExtW->timeRangeExpr->needCalc) {
×
1383
    return true;
×
1384
  }
1385

1386
  SList* pList = taosArrayGetP(pExtW->pOutputBlocks, pExtW->outWinIdx - 1);
×
1387
  if (0 == listNEles(pList)) {
×
1388
    return true;
×
1389
  }
1390

1391
  SListNode* pNode = listTail(pList);
×
1392
  SArray* pBlkWinIdx = *((SArray**)pNode->data + 1);
×
1393
  int64_t* pIdx = taosArrayGetLast(pBlkWinIdx);
×
1394
  if (pIdx && *(int32_t*)pIdx < pExtW->blkWinStartIdx) {
×
1395
    return true;
×
1396
  }
1397

1398
  return false;
×
1399
}
1400

1401
static int32_t extWinGetWinResBlock(SOperatorInfo* pOperator, int32_t rows, SExtWinTimeWindow* pWin, SSDataBlock** ppRes, SArray** ppIdx) {
1,183✔
1402
  SExternalWindowOperator* pExtW = pOperator->info;
1,183✔
1403
  SList*                   pList = NULL;
1,183✔
1404
  int32_t                  code = TSDB_CODE_SUCCESS, lino = 0;
1,183✔
1405
  
1406
  if (pWin->winOutIdx >= 0) {
1,183✔
1407
    pList = taosArrayGetP(pExtW->pOutputBlocks, pWin->winOutIdx);
672✔
1408
  } else {
1409
    if (extWinLastWinClosed(pExtW)) {
511✔
1410
      pWin->winOutIdx = pExtW->outWinIdx - 1;
×
1411
      pList = taosArrayGetP(pExtW->pOutputBlocks, pWin->winOutIdx);
×
1412
    } else {
1413
      pWin->winOutIdx = pExtW->outWinIdx++;
511✔
1414
      pList = tdListNew(POINTER_BYTES * 2);
511✔
1415
      TSDB_CHECK_NULL(pList, code, lino, _exit, terrno);
511✔
1416
      SList** ppList = taosArrayGet(pExtW->pOutputBlocks, pWin->winOutIdx);
511✔
1417
      extWinRecycleBlockList(pExtW, ppList);
511✔
1418
      *ppList = pList;
511✔
1419
    }
1420
  }
1421
  
1422
  TAOS_CHECK_EXIT(extWinGetLastBlockFromList(pExtW, pList, rows, ppRes, ppIdx));
1,183✔
1423

1424
_exit:
1,183✔
1425

1426
  if (code) {
1,183✔
1427
    qError("%s %s failed at line %d since %s", pOperator->pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1428
  }
1429

1430
  return code;
1,183✔
1431
}
1432

1433
static int32_t extWinProjectDo(SOperatorInfo* pOperator, SSDataBlock* pInputBlock, int32_t startPos, int32_t rows, SExtWinTimeWindow* pWin) {
1,183✔
1434
  SExternalWindowOperator* pExtW = pOperator->info;
1,183✔
1435
  SExprSupp*               pExprSup = &pExtW->scalarSupp;
1,183✔
1436
  SSDataBlock*             pResBlock = NULL;
1,183✔
1437
  SArray*                  pIdx = NULL;
1,183✔
1438
  int32_t                  code = TSDB_CODE_SUCCESS, lino = 0;
1,183✔
1439
  
1440
  TAOS_CHECK_EXIT(extWinGetWinResBlock(pOperator, rows, pWin, &pResBlock, &pIdx));
1,183✔
1441

1442
  qDebug("%s %s win[%" PRId64 ", %" PRId64 "] got res block %p winRowIdx %p, winOutIdx:%d, capacity:%d", 
1,183✔
1443
      pOperator->pTaskInfo->id.str, __func__, pWin->tw.skey, pWin->tw.ekey, pResBlock, pIdx, pWin->winOutIdx, pResBlock->info.capacity);
1444
  
1445
  if (!pExtW->pTmpBlock) {
1,183✔
1446
    TAOS_CHECK_EXIT(createOneDataBlock(pInputBlock, false, &pExtW->pTmpBlock));
287✔
1447
  } else {
1448
    blockDataCleanup(pExtW->pTmpBlock);
896✔
1449
  }
1450
  
1451
  TAOS_CHECK_EXIT(blockDataEnsureCapacity(pExtW->pTmpBlock, TMAX(1, rows)));
1,183✔
1452

1453
  qDebug("%s %s start to copy %d rows to tmp blk", pOperator->pTaskInfo->id.str, __func__, rows);
1,183✔
1454
  TAOS_CHECK_EXIT(blockDataMergeNRows(pExtW->pTmpBlock, pInputBlock, startPos, rows));
1,183✔
1455

1456
  qDebug("%s %s start to apply project to tmp blk", pOperator->pTaskInfo->id.str, __func__);
1,183✔
1457
  TAOS_CHECK_EXIT(projectApplyFunctionsWithSelect(pExprSup->pExprInfo, pResBlock, pExtW->pTmpBlock, pExprSup->pCtx, pExprSup->numOfExprs,
1,183✔
1458
        NULL, GET_STM_RTINFO(pOperator->pTaskInfo), true, pExprSup->hasIndefRowsFunc));
1459

1460
  TAOS_CHECK_EXIT(extWinAppendWinIdx(pOperator->pTaskInfo, pIdx, pResBlock, extWinGetCurWinIdx(pOperator->pTaskInfo), rows));
1,183✔
1461

1462
_exit:
1,183✔
1463

1464
  if (code) {
1,183✔
1465
    qError("%s %s failed at line %d since %s", pOperator->pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1466
  } else {
1467
    qDebug("%s %s project succeed", pOperator->pTaskInfo->id.str, __func__);
1,183✔
1468
  }
1469
  
1470
  return code;
1,183✔
1471
}
1472

1473
static int32_t extWinProjectOpen(SOperatorInfo* pOperator, SSDataBlock* pInputBlock) {
735✔
1474
  SExternalWindowOperator* pExtW = pOperator->info;
735✔
1475
  int64_t*                 tsCol = extWinExtractTsCol(pInputBlock, pExtW->primaryTsIndex, pOperator->pTaskInfo);
735✔
1476
  SExtWinTimeWindow*       pWin = NULL;
735✔
1477
  bool                     ascScan = pExtW->binfo.inputTsOrder == TSDB_ORDER_ASC;
735✔
1478
  int32_t                  startPos = 0, winRows = 0;
735✔
1479
  int32_t                  code = TSDB_CODE_SUCCESS, lino = 0;
735✔
1480
  
1481
  while (true) {
1482
    TAOS_CHECK_EXIT((*pExtW->getWinFp)(pOperator, tsCol, &startPos, &pInputBlock->info, &pWin, &winRows));
1,918✔
1483
    if (pWin == NULL) {
1,918✔
1484
      break;
735✔
1485
    }
1486

1487
    qDebug("%s ext window [%" PRId64 ", %" PRId64 ") project start, ascScan:%d, startPos:%d, winRows:%d",
1,183✔
1488
           GET_TASKID(pOperator->pTaskInfo), pWin->tw.skey, pWin->tw.ekey, ascScan, startPos, winRows);        
1489
    
1490
    TAOS_CHECK_EXIT(extWinProjectDo(pOperator, pInputBlock, startPos, winRows, pWin));
1,183✔
1491
    
1492
    startPos += winRows;
1,183✔
1493
  }
1494
  
1495
_exit:
735✔
1496

1497
  if (code) {
735✔
1498
    qError("%s failed at line %d since:%s", __func__, lino, tstrerror(code));
×
1499
  }
1500

1501
  return code;
735✔
1502
}
1503

1504
static int32_t extWinIndefRowsDoImpl(SOperatorInfo* pOperator, SSDataBlock* pRes, SSDataBlock* pBlock) {
×
1505
  SExternalWindowOperator* pExtW = pOperator->info;
×
1506
  SOptrBasicInfo*     pInfo = &pExtW->binfo;
×
1507
  SExprSupp*          pSup = &pOperator->exprSupp;
×
1508
  SExecTaskInfo*      pTaskInfo = pOperator->pTaskInfo;
×
1509
  int32_t order = pInfo->inputTsOrder;
×
1510
  int32_t scanFlag = pBlock->info.scanFlag;
×
1511
  int32_t code = TSDB_CODE_SUCCESS, lino = 0;
×
1512

1513
  SExprSupp* pScalarSup = &pExtW->scalarSupp;
×
1514
  if (pScalarSup->pExprInfo != NULL) {
×
1515
    TAOS_CHECK_EXIT(projectApplyFunctions(pScalarSup->pExprInfo, pBlock, pBlock, pScalarSup->pCtx, pScalarSup->numOfExprs,
×
1516
                                 pExtW->pPseudoColInfo, GET_STM_RTINFO(pOperator->pTaskInfo)));
1517
  }
1518

1519
  TAOS_CHECK_EXIT(setInputDataBlock(pSup, pBlock, order, scanFlag, false));
×
1520

1521
  TAOS_CHECK_EXIT(blockDataEnsureCapacity(pRes, pRes->info.rows + pBlock->info.rows));
×
1522

1523
  TAOS_CHECK_EXIT(projectApplyFunctions(pSup->pExprInfo, pRes, pBlock, pSup->pCtx, pSup->numOfExprs,
×
1524
                               pExtW->pPseudoColInfo, GET_STM_RTINFO(pOperator->pTaskInfo)));
1525

1526
_exit:
×
1527

1528
  if (code) {
×
1529
    qError("%s %s failed at line %d since %s", pOperator->pTaskInfo->id.str, __FUNCTION__, lino, tstrerror(code));
×
1530
  }
1531

1532
  return code;
×
1533
}
1534

1535
static int32_t extWinIndefRowsSetWinOutputBuf(SExternalWindowOperator* pExtW, SExtWinTimeWindow* win, SExprSupp* pSupp, 
×
1536
                                     SAggSupporter* pAggSup, SExecTaskInfo* pTaskInfo, bool reset) {
1537
  int32_t code = 0, lino = 0;
×
1538
  SResultRow* pResultRow = NULL;
×
1539

1540
  pResultRow = (SResultRow*)((char*)pExtW->pResultRow + win->winOutIdx * pAggSup->resultRowSize);
×
1541
  
1542
  qDebug("set window [%" PRId64 ", %" PRId64 "] outIdx:%d", win->tw.skey, win->tw.ekey, win->winOutIdx);
×
1543

1544
  if (reset) {
×
1545
    memset(pResultRow, 0, pAggSup->resultRowSize);
×
1546
    for (int32_t k = 0; k < pSupp->numOfExprs; ++k) {
×
1547
      SqlFunctionCtx* pCtx = &pSupp->pCtx[k];
×
1548
      pCtx->pOutput = NULL;
×
1549
    }
1550
  }
1551

1552
  TAOS_SET_POBJ_ALIGNED(&pResultRow->win, &win->tw);
×
1553

1554
  // set time window for current result
1555
  TAOS_CHECK_EXIT(setResultRowInitCtx(pResultRow, pSupp->pCtx, pSupp->numOfExprs, pSupp->rowEntryInfoOffset));
×
1556

1557
_exit:
×
1558
  
1559
  if (code) {
×
1560
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1561
  }
1562

1563
  return code;
×
1564
}
1565

1566
static int32_t extWinGetSetWinResBlockBuf(SOperatorInfo* pOperator, int32_t rows, SExtWinTimeWindow* pWin, SSDataBlock** ppRes, SArray** ppIdx) {
×
1567
  SExternalWindowOperator* pExtW = pOperator->info;
×
1568
  SList*                   pList = NULL;
×
1569
  int32_t                  code = TSDB_CODE_SUCCESS, lino = 0;
×
1570
  
1571
  if (pWin->winOutIdx >= 0) {
×
1572
    pList = taosArrayGetP(pExtW->pOutputBlocks, pWin->winOutIdx);
×
1573
    TAOS_CHECK_EXIT(extWinIndefRowsSetWinOutputBuf(pExtW, pWin, &pOperator->exprSupp, &pExtW->aggSup, pOperator->pTaskInfo, false));
×
1574
  } else {
1575
    if (extWinLastWinClosed(pExtW)) {
×
1576
      pWin->winOutIdx = pExtW->outWinIdx - 1;
×
1577
      pList = taosArrayGetP(pExtW->pOutputBlocks, pWin->winOutIdx);
×
1578
    } else {
1579
      pWin->winOutIdx = pExtW->outWinIdx++;
×
1580
      pList = tdListNew(POINTER_BYTES * 2);
×
1581
      TSDB_CHECK_NULL(pList, code, lino, _exit, terrno);
×
1582
      SList** ppList = taosArrayGet(pExtW->pOutputBlocks, pWin->winOutIdx);
×
1583
      extWinRecycleBlockList(pExtW, ppList);
×
1584
      *ppList = pList;
×
1585
    }
1586
    TAOS_CHECK_EXIT(extWinIndefRowsSetWinOutputBuf(pExtW, pWin, &pOperator->exprSupp, &pExtW->aggSup, pOperator->pTaskInfo, true));
×
1587
  }
1588
  
1589
  TAOS_CHECK_EXIT(extWinGetLastBlockFromList(pExtW, pList, rows, ppRes, ppIdx));
×
1590

1591
_exit:
×
1592

1593
  if (code) {
×
1594
    qError("%s %s failed at line %d since %s", pOperator->pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1595
  }
1596

1597
  return code;
×
1598
}
1599

1600

1601
static int32_t extWinIndefRowsDo(SOperatorInfo* pOperator, SSDataBlock* pInputBlock, int32_t startPos, int32_t rows, SExtWinTimeWindow* pWin) {
×
1602
  SExternalWindowOperator* pExtW = pOperator->info;
×
1603
  SSDataBlock*             pResBlock = NULL;
×
1604
  SArray*                  pIdx = NULL;
×
1605
  int32_t                  code = TSDB_CODE_SUCCESS, lino = 0;
×
1606
  
1607
  TAOS_CHECK_EXIT(extWinGetSetWinResBlockBuf(pOperator, rows, pWin, &pResBlock, &pIdx));
×
1608
  
1609
  if (!pExtW->pTmpBlock) {
×
1610
    TAOS_CHECK_EXIT(createOneDataBlock(pInputBlock, false, &pExtW->pTmpBlock));
×
1611
  } else {
1612
    blockDataCleanup(pExtW->pTmpBlock);
×
1613
  }
1614
  
1615
  TAOS_CHECK_EXIT(blockDataEnsureCapacity(pExtW->pTmpBlock, TMAX(1, rows)));
×
1616

1617
  TAOS_CHECK_EXIT(blockDataMergeNRows(pExtW->pTmpBlock, pInputBlock, startPos, rows));
×
1618
  TAOS_CHECK_EXIT(extWinIndefRowsDoImpl(pOperator, pResBlock, pExtW->pTmpBlock));
×
1619

1620
  TAOS_CHECK_EXIT(extWinAppendWinIdx(pOperator->pTaskInfo, pIdx, pResBlock, extWinGetCurWinIdx(pOperator->pTaskInfo), rows));
×
1621

1622
_exit:
×
1623

1624
  if (code) {
×
1625
    qError("%s %s failed at line %d since %s", pOperator->pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1626
  }
1627
  
1628
  return code;
×
1629
}
1630

1631

1632
static int32_t extWinIndefRowsOpen(SOperatorInfo* pOperator, SSDataBlock* pInputBlock) {
×
1633
  SExternalWindowOperator* pExtW = pOperator->info;
×
1634
  int64_t*                 tsCol = extWinExtractTsCol(pInputBlock, pExtW->primaryTsIndex, pOperator->pTaskInfo);
×
1635
  SExtWinTimeWindow*       pWin = NULL;
×
1636
  bool                     ascScan = pExtW->binfo.inputTsOrder == TSDB_ORDER_ASC;
×
1637
  int32_t                  startPos = 0, winRows = 0;
×
1638
  int32_t                  code = TSDB_CODE_SUCCESS, lino = 0;
×
1639
  
1640
  while (true) {
1641
    TAOS_CHECK_EXIT((*pExtW->getWinFp)(pOperator, tsCol, &startPos, &pInputBlock->info, &pWin, &winRows));
×
1642
    if (pWin == NULL) {
×
1643
      break;
×
1644
    }
1645

1646
    qDebug("%s ext window [%" PRId64 ", %" PRId64 ") indefRows start, ascScan:%d, startPos:%d, winRows:%d",
×
1647
           GET_TASKID(pOperator->pTaskInfo), pWin->tw.skey, pWin->tw.ekey, ascScan, startPos, winRows);        
1648
    
1649
    TAOS_CHECK_EXIT(extWinIndefRowsDo(pOperator, pInputBlock, startPos, winRows, pWin));
×
1650
    
1651
    startPos += winRows;
×
1652
  }
1653
  
1654
_exit:
×
1655

1656
  if (code) {
×
1657
    qError("%s failed at line %d since:%s", __func__, lino, tstrerror(code));
×
1658
  }
1659

1660
  return code;
×
1661
}
1662

1663
static int32_t extWinNonAggOutputRes(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
511✔
1664
  SExternalWindowOperator* pExtW = pOperator->info;
511✔
1665
  int32_t                  numOfWin = pExtW->outWinIdx;
511✔
1666
  int32_t                  code = TSDB_CODE_SUCCESS;
511✔
1667
  int32_t                  lino = 0;
511✔
1668
  SSDataBlock*             pRes = NULL;
511✔
1669

1670
  for (; pExtW->outputWinId < numOfWin; pExtW->outputWinId++, extWinIncCurWinOutIdx(pOperator->pTaskInfo->pStreamRuntimeInfo)) {
511✔
1671
    SList* pList = taosArrayGetP(pExtW->pOutputBlocks, pExtW->outputWinId);
511✔
1672
    if (listNEles(pList) <= 0) {
511✔
1673
      continue;
×
1674
    }
1675

1676
    SListNode* pNode = tdListPopHead(pList);
511✔
1677
    pRes = *(SSDataBlock**)pNode->data;
511✔
1678
    pOperator->pTaskInfo->pStreamRuntimeInfo->funcInfo.pStreamBlkWinIdx = *(SArray**)((SArray**)pNode->data + 1);
511✔
1679
    pExtW->pLastBlkNode = pNode;
511✔
1680

1681
    if (listNEles(pList) <= 0) {
511✔
1682
      pExtW->outputWinId++;
511✔
1683
      extWinIncCurWinOutIdx(pOperator->pTaskInfo->pStreamRuntimeInfo);
511✔
1684
    }
1685

1686
    break;
511✔
1687
  }
1688

1689
  if (pRes) {
511✔
1690
    qDebug("%s result generated, rows:%" PRId64 , GET_TASKID(pOperator->pTaskInfo), pRes->info.rows);
511✔
1691
    pRes->info.version = pOperator->pTaskInfo->version;
511✔
1692
    pRes->info.dataLoad = 1;
511✔
1693
  } else {
1694
    pOperator->pTaskInfo->pStreamRuntimeInfo->funcInfo.pStreamBlkWinIdx = NULL;
×
1695
    qDebug("%s ext window done", GET_TASKID(pOperator->pTaskInfo));
×
1696
  }
1697

1698
  *ppRes = (pRes && pRes->info.rows > 0) ? pRes : NULL;
511✔
1699

1700
_exit:
511✔
1701

1702
  if (code != TSDB_CODE_SUCCESS) {
511✔
1703
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1704
  }
1705

1706
  return code;
511✔
1707
}
1708

1709
static int32_t extWinAggHandleEmptyWins(SOperatorInfo* pOperator, SSDataBlock* pBlock, bool allRemains, SExtWinTimeWindow* pWin) {
1,164,059,462✔
1710
  int32_t code = 0, lino = 0;
1,164,059,462✔
1711
  SExternalWindowOperator* pExtW = (SExternalWindowOperator*)pOperator->info;
1,164,059,462✔
1712
  SExprSupp* pSup = &pOperator->exprSupp;
1,164,070,012✔
1713
  int32_t currIdx = extWinGetCurWinIdx(pOperator->pTaskInfo);
1,164,070,442✔
1714

1715
  if (NULL == pExtW->pEmptyInputBlock || (pWin && pWin->tw.skey == pExtW->lastSKey)) {
1,164,070,880✔
1716
    goto _exit;
615,828,379✔
1717
  }
1718

1719
  bool ascScan = pExtW->binfo.inputTsOrder == TSDB_ORDER_ASC;
548,242,501✔
1720
  int32_t endIdx = allRemains ? (pExtW->pWins->size - 1) : (currIdx - 1);
548,242,712✔
1721
  SResultRowInfo* pResultRowInfo = &pExtW->binfo.resultRowInfo;
548,242,712✔
1722
  SSDataBlock* pInput = pExtW->pEmptyInputBlock;
548,242,712✔
1723

1724
  if ((pExtW->lastWinId + 1) <= endIdx) {
548,242,712✔
1725
    TAOS_CHECK_EXIT(setInputDataBlock(pSup, pExtW->pEmptyInputBlock, pExtW->binfo.inputTsOrder, MAIN_SCAN, true));
293,238✔
1726
  }
1727
  
1728
  for (int32_t i = pExtW->lastWinId + 1; i <= endIdx; ++i) {
916,040,110✔
1729
    SExtWinTimeWindow* pWin = taosArrayGet(pExtW->pWins, i);
367,797,398✔
1730

1731
    extWinSetCurWinIdx(pOperator, i);
367,797,398✔
1732
    qDebug("%s %dth ext empty window start:%" PRId64 ", end:%" PRId64 ", ascScan:%d",
367,797,398✔
1733
           GET_TASKID(pOperator->pTaskInfo), i, pWin->tw.skey, pWin->tw.ekey, ascScan);
1734

1735
    TAOS_CHECK_EXIT(extWinAggSetWinOutputBuf(pOperator, pWin, pSup, &pExtW->aggSup, pOperator->pTaskInfo));
367,797,398✔
1736

1737
    updateTimeWindowInfo(&pExtW->twAggSup.timeWindowData, &pWin->tw, 1);
367,797,398✔
1738
    code = extWinAggDo(pOperator, 0, 1, pInput);
367,797,398✔
1739
    pExtW->lastWinId = i;  
367,797,398✔
1740
    TAOS_CHECK_EXIT(code);
367,797,398✔
1741
  }
1742

1743
  
1744
_exit:
548,242,497✔
1745

1746
  if (code) {
1,164,070,665✔
1747
    qError("%s %s failed at line %d since %s", pOperator->pTaskInfo->id.str, __FUNCTION__, lino, tstrerror(code));
×
1748
  } else {
1749
    if (pBlock) {
1,164,070,665✔
1750
      TAOS_CHECK_EXIT(setInputDataBlock(pSup, pBlock, pExtW->binfo.inputTsOrder, pBlock->info.scanFlag, true));
1,121,661,958✔
1751
    }
1752

1753
    if (!allRemains) {
1,164,067,042✔
1754
      extWinSetCurWinIdx(pOperator, currIdx);  
1,121,658,335✔
1755
    }
1756
  }
1757

1758
  return code;
1,164,067,062✔
1759
}
1760

1761
static int32_t extWinAggOpen(SOperatorInfo* pOperator, SSDataBlock* pInputBlock) {
40,317,479✔
1762
  SExternalWindowOperator* pExtW = (SExternalWindowOperator*)pOperator->info;
40,317,479✔
1763
  int32_t                  startPos = 0, winRows = 0;
40,317,695✔
1764
  int64_t*                 tsCol = extWinExtractTsCol(pInputBlock, pExtW->primaryTsIndex, pOperator->pTaskInfo);
40,317,695✔
1765
  bool                     ascScan = pExtW->binfo.inputTsOrder == TSDB_ORDER_ASC;
40,317,695✔
1766
  int32_t                  code = 0, lino = 0;
40,317,695✔
1767
  SExtWinTimeWindow*       pWin = NULL;
40,317,695✔
1768
  bool                     scalarCalc = false;
40,317,695✔
1769

1770
  while (true) {
1771
    TAOS_CHECK_EXIT((*pExtW->getWinFp)(pOperator, tsCol, &startPos, &pInputBlock->info, &pWin, &winRows));
1,161,979,231✔
1772
    if (pWin == NULL) {
1,161,979,219✔
1773
      break;
40,317,480✔
1774
    }
1775

1776
    TAOS_CHECK_EXIT(extWinAggHandleEmptyWins(pOperator, pInputBlock, false, pWin));
1,121,661,739✔
1777

1778
    qDebug("%s ext window [%" PRId64 ", %" PRId64 ") agg start, ascScan:%d, startPos:%d, winRows:%d",
1,121,657,722✔
1779
           GET_TASKID(pOperator->pTaskInfo), pWin->tw.skey, pWin->tw.ekey, ascScan, startPos, winRows);        
1780

1781
    if (!scalarCalc) {
1,121,661,098✔
1782
      if (pExtW->scalarSupp.pExprInfo) {
39,913,814✔
1783
        SExprSupp* pScalarSup = &pExtW->scalarSupp;
5,697✔
1784
        TAOS_CHECK_EXIT(projectApplyFunctions(pScalarSup->pExprInfo, pInputBlock, pInputBlock, pScalarSup->pCtx, pScalarSup->numOfExprs,
5,697✔
1785
                                     pExtW->pPseudoColInfo, GET_STM_RTINFO(pOperator->pTaskInfo)));
1786
      }
1787
      
1788
      scalarCalc = true;
39,914,244✔
1789
    }
1790

1791
    if (pWin->tw.skey != pExtW->lastSKey || pWin->tw.skey == INT64_MIN) {
1,121,661,528✔
1792
      TAOS_CHECK_EXIT(extWinAggSetWinOutputBuf(pOperator, pWin, &pOperator->exprSupp, &pExtW->aggSup, pOperator->pTaskInfo));
1,120,448,099✔
1793
    }
1794
    
1795
    updateTimeWindowInfo(&pExtW->twAggSup.timeWindowData, &pWin->tw, 1);
1,121,659,195✔
1796
    TAOS_CHECK_EXIT(extWinAggDo(pOperator, startPos, winRows, pInputBlock));
1,121,661,110✔
1797
    
1798
    pExtW->lastSKey = pWin->tw.skey;
1,121,658,140✔
1799
    pExtW->lastWinId = extWinGetCurWinIdx(pOperator->pTaskInfo);
1,121,658,992✔
1800
    startPos += winRows;
1,121,661,536✔
1801
  }
1802

1803
_exit:
40,317,480✔
1804

1805
  if (code) {
40,317,695✔
1806
    qError("%s failed at line %d since:%s", __func__, lino, tstrerror(code));
×
1807
  }
1808

1809
  return code;
40,317,695✔
1810
}
1811

1812
static int32_t extWinAggOutputRes(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
42,617,313✔
1813
  SExternalWindowOperator* pExtW = pOperator->info;
42,617,313✔
1814
  SExecTaskInfo*           pTaskInfo = pOperator->pTaskInfo;
42,617,313✔
1815
  SSDataBlock*             pBlock = pExtW->binfo.pRes;
42,617,313✔
1816
  int32_t                  code = TSDB_CODE_SUCCESS;
42,617,313✔
1817
  int32_t                  lino = 0;
42,617,313✔
1818
  SExprInfo*               pExprInfo = pOperator->exprSupp.pExprInfo;
42,617,313✔
1819
  int32_t                  numOfExprs = pOperator->exprSupp.numOfExprs;
42,617,313✔
1820
  int32_t*                 rowEntryOffset = pOperator->exprSupp.rowEntryInfoOffset;
42,617,313✔
1821
  SqlFunctionCtx*          pCtx = pOperator->exprSupp.pCtx;
42,617,313✔
1822
  int32_t                  numOfWin = pExtW->outWinIdx;
42,617,313✔
1823

1824
  pBlock->info.version = pTaskInfo->version;
42,617,313✔
1825
  blockDataCleanup(pBlock);
42,617,313✔
1826
  taosArrayClear(pExtW->pWinRowIdx);
42,617,313✔
1827

1828
  for (; pExtW->outputWinId < pExtW->pWins->size; ++pExtW->outputWinId) {
767,298,900✔
1829
    SExtWinTimeWindow* pWin = taosArrayGet(pExtW->pWins, pExtW->outputWinId);
724,899,835✔
1830
    int32_t            winIdx = pWin->winOutIdx;
724,899,624✔
1831
    if (winIdx < 0) {
724,899,413✔
1832
      continue;
7,774,680✔
1833
    }
1834

1835
    pExtW->outputWinNum++;
717,124,733✔
1836
    SResultRow* pRow = (SResultRow*)((char*)pExtW->pResultRow + winIdx * pExtW->aggSup.resultRowSize);
717,124,733✔
1837

1838
    doUpdateNumOfRows(pCtx, pRow, numOfExprs, rowEntryOffset);
717,124,733✔
1839

1840
    // no results, continue to check the next one
1841
    if (pRow->numOfRows == 0) {
717,123,678✔
1842
      continue;
×
1843
    }
1844

1845
    if (pBlock->info.rows + pRow->numOfRows > pBlock->info.capacity) {
717,123,678✔
1846
      uint32_t newSize = pBlock->info.rows + pRow->numOfRows + numOfWin - pExtW->outputWinNum;
461,429✔
1847
      TAOS_CHECK_EXIT(blockDataEnsureCapacity(pBlock, newSize));
461,429✔
1848
      qDebug("datablock capacity not sufficient, expand to required:%d, current capacity:%d, %s", newSize,
461,429✔
1849
             pBlock->info.capacity, GET_TASKID(pTaskInfo));
1850
    }
1851

1852
    TAOS_CHECK_EXIT(copyResultrowToDataBlock(pExprInfo, numOfExprs, pRow, pCtx, pBlock, rowEntryOffset, pTaskInfo));
717,123,889✔
1853

1854
    pBlock->info.rows += pRow->numOfRows;
717,125,366✔
1855

1856
    TAOS_CHECK_EXIT(extWinAppendWinIdx(pOperator->pTaskInfo, pExtW->pWinRowIdx, pBlock, pRow->winIdx, pRow->numOfRows));
717,125,366✔
1857

1858
    if (pBlock->info.rows >= pOperator->resultInfo.threshold) {
717,124,522✔
1859
      ++pExtW->outputWinId;
217,615✔
1860
      break;
217,615✔
1861
    }
1862
  }
1863

1864
  qDebug("%s result generated, rows:%" PRId64 ", groupId:%" PRIu64, GET_TASKID(pTaskInfo), pBlock->info.rows,
42,616,891✔
1865
         pBlock->info.id.groupId);
1866
         
1867
  pBlock->info.dataLoad = 1;
42,616,891✔
1868

1869
  *ppRes = (pBlock->info.rows > 0) ? pBlock : NULL;
42,617,313✔
1870

1871
  if (*ppRes) {
42,617,313✔
1872
    (*ppRes)->info.window.skey = pExtW->orgTableTimeRange.skey;
37,558,461✔
1873
    (*ppRes)->info.window.ekey = pExtW->orgTableTimeRange.ekey;
37,558,461✔
1874
  }
1875
  if (pOperator->pTaskInfo->pStreamRuntimeInfo) {
42,617,313✔
1876
    pOperator->pTaskInfo->pStreamRuntimeInfo->funcInfo.pStreamBlkWinIdx = pExtW->pWinRowIdx;
416,157✔
1877
  }
1878

1879
_exit:
42,201,156✔
1880

1881
  if (code != TSDB_CODE_SUCCESS) {
42,617,313✔
1882
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1883
  }
1884

1885
  return code;
42,617,313✔
1886
}
1887

1888
static int32_t extWinInitResultRow(SExecTaskInfo*        pTaskInfo, SExternalWindowOperator* pExtW, int32_t winNum) {
42,409,205✔
1889
  if (EEXT_MODE_SCALAR == pExtW->mode) {
42,409,205✔
1890
    return TSDB_CODE_SUCCESS;
287✔
1891
  }
1892

1893
  if (winNum <= pExtW->resultRowCapacity) {
42,408,918✔
1894
    return TSDB_CODE_SUCCESS;
41,946,434✔
1895
  }
1896
  
1897
  taosMemoryFreeClear(pExtW->pResultRow);
462,484✔
1898
  pExtW->resultRowCapacity = -1;
462,484✔
1899

1900
  int32_t code = 0, lino = 0;
462,484✔
1901
  
1902
  pExtW->pResultRow = taosMemoryCalloc(winNum, pExtW->aggSup.resultRowSize);
462,484✔
1903
  QUERY_CHECK_NULL(pExtW->pResultRow, code, lino, _exit, terrno);
462,484✔
1904

1905
  pExtW->resultRowCapacity = winNum;
462,484✔
1906

1907
_exit:
462,484✔
1908

1909
  if (code) {
462,484✔
1910
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1911
  }
1912

1913
  return code;
462,484✔
1914
}
1915

1916
static void extWinFreeResultRow(SExternalWindowOperator* pExtW) {
207,762✔
1917
  if (pExtW->resultRowCapacity * pExtW->aggSup.resultRowSize >= 1048576) {
207,762✔
1918
    taosMemoryFreeClear(pExtW->pResultRow);
1,899✔
1919
    pExtW->resultRowCapacity = -1;
1,899✔
1920
  }
1921
  if (pExtW->binfo.pRes && pExtW->binfo.pRes->info.rows * pExtW->aggSup.resultRowSize >= 1048576) {
207,762✔
1922
    blockDataFreeCols(pExtW->binfo.pRes);
×
1923
  }
1924
}
207,762✔
1925

1926
static int32_t extWinInitWindowList(SExternalWindowOperator* pExtW, SExecTaskInfo*        pTaskInfo) {
207,209✔
1927
  if (taosArrayGetSize(pExtW->pWins) > 0) {
207,209✔
1928
    return TSDB_CODE_SUCCESS;
×
1929
  }
1930
  
1931
  int32_t code = 0, lino = 0;
208,049✔
1932
  SStreamRuntimeFuncInfo* pInfo = &pTaskInfo->pStreamRuntimeInfo->funcInfo;
208,049✔
1933
  size_t size = taosArrayGetSize(pInfo->pStreamPesudoFuncVals);
208,049✔
1934
  SExtWinTimeWindow* pWin = taosArrayReserve(pExtW->pWins, size);
208,049✔
1935
  TSDB_CHECK_NULL(pWin, code, lino, _exit, terrno);
208,049✔
1936

1937
  TAOS_CHECK_EXIT(extWinInitResultRow(pTaskInfo, pExtW, size));
208,049✔
1938

1939
  if (pExtW->timeRangeExpr && pExtW->timeRangeExpr->needCalc) {
208,049✔
1940
    TAOS_CHECK_EXIT(scalarCalculateExtWinsTimeRange(pExtW->timeRangeExpr, pInfo, pWin));
111,159✔
1941
    if (qDebugFlag & DEBUG_DEBUG) {
111,159✔
1942
      for (int32_t i = 0; i < size; ++i) {
315,563✔
1943
        qDebug("%s the %d/%d ext window calced initialized, TR[%" PRId64 ", %" PRId64 ")", 
204,404✔
1944
            pTaskInfo->id.str, i, (int32_t)size, pWin[i].tw.skey, pWin[i].tw.ekey);
1945
      }
1946
    }
1947
  } else {
1948
    for (int32_t i = 0; i < size; ++i) {
11,489,393✔
1949
      SSTriggerCalcParam* pParam = taosArrayGet(pInfo->pStreamPesudoFuncVals, i);
11,392,503✔
1950

1951
      pWin[i].tw.skey = pParam->wstart;
11,392,503✔
1952
      pWin[i].tw.ekey = pParam->wend + ((pInfo->triggerType != STREAM_TRIGGER_SLIDING) ? 1 : 0);
11,392,714✔
1953
      pWin[i].winOutIdx = -1;
11,392,714✔
1954

1955
      qDebug("%s the %d/%d ext window initialized, TR[%" PRId64 ", %" PRId64 ")", 
11,392,714✔
1956
          pTaskInfo->id.str, i, (int32_t)size, pWin[i].tw.skey, pWin[i].tw.ekey);
1957
    }
1958
  }
1959
  
1960
  pExtW->outputWinId = pInfo->curIdx;
208,049✔
1961
  pExtW->lastWinId = -1;
208,049✔
1962
  pExtW->blkWinStartIdx = pInfo->curIdx;
208,049✔
1963

1964
_exit:
208,049✔
1965

1966
  if (code) {
208,049✔
1967
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1968
  }
1969

1970
  return code;
208,049✔
1971
}
1972

1973
static bool extWinNonAggGotResBlock(SExternalWindowOperator* pExtW) {
735✔
1974
  if (pExtW->multiTableMode && !pExtW->inputHasOrder) {
735✔
1975
    return false;
672✔
1976
  }
1977
  int32_t remainWin = pExtW->outWinIdx - pExtW->outputWinId;
63✔
1978
  if (remainWin > 1 && (NULL == pExtW->timeRangeExpr || !pExtW->timeRangeExpr->needCalc)) {
63✔
1979
    return true;
×
1980
  }
1981
  
1982
  SList* pList = taosArrayGetP(pExtW->pOutputBlocks, pExtW->outputWinId);
63✔
1983
  if (!pList || listNEles(pList) <= 0) {
63✔
1984
    return false;
×
1985
  }
1986
  if (listNEles(pList) > 1) {
63✔
1987
    return true;
×
1988
  }
1989

1990
  SListNode* pNode = listHead(pList);
63✔
1991
  SArray* pIdx = *(SArray**)((SArray**)pNode->data + 1);
63✔
1992
  int32_t* winIdx = taosArrayGetLast(pIdx);
63✔
1993
  if (winIdx && *winIdx < pExtW->blkWinStartIdx) {
63✔
1994
    return true;
×
1995
  }
1996

1997
  return false;
63✔
1998
}
1999

2000
static int32_t getTimeWindowOfBlock(SSDataBlock *pBlock, col_id_t tsSlotId, int64_t *startTs, int64_t *endTs) {
38,026,951✔
2001
  int32_t code = TSDB_CODE_SUCCESS;
38,026,951✔
2002
  int32_t lino = 0;
38,026,951✔
2003
  int32_t tsIndex = -1;
38,026,951✔
2004
  for (int32_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); i++) {
38,561,632✔
2005
    SColumnInfoData *pCol = (SColumnInfoData*)taosArrayGet(pBlock->pDataBlock, i);
38,561,632✔
2006
    QUERY_CHECK_NULL(pCol, code, lino, _return, terrno)
38,561,632✔
2007
    if (pCol->info.colId == tsSlotId) {
38,561,632✔
2008
      tsIndex = i;
38,026,951✔
2009
      break;
38,026,951✔
2010
    }
2011
  }
2012

2013
  if (tsIndex == -1) {
38,026,951✔
2014
    tsIndex = (int32_t)taosArrayGetSize(pBlock->pDataBlock) - 1;
×
2015
  }
2016

2017
  SColumnInfoData *pColData = (SColumnInfoData*)taosArrayGet(pBlock->pDataBlock, tsIndex);
38,026,951✔
2018
  QUERY_CHECK_NULL(pColData, code, lino, _return, terrno)
38,026,951✔
2019

2020
  GET_TYPED_DATA(*startTs, int64_t, TSDB_DATA_TYPE_TIMESTAMP, colDataGetNumData(pColData, 0), 0);
38,026,951✔
2021
  GET_TYPED_DATA(*endTs, int64_t, TSDB_DATA_TYPE_TIMESTAMP, colDataGetNumData(pColData, pBlock->info.rows - 1), 0);
38,026,951✔
2022

2023
  return code;
38,026,951✔
2024
_return:
×
2025
  qError("failed to get time window of block, %s code:%s, line:%d", __func__, tstrerror(code), lino);
×
2026
  return code;
×
2027
}
2028

2029
static int32_t extWinOpen(SOperatorInfo* pOperator) {
42,409,205✔
2030
  if (OPTR_IS_OPENED(pOperator) && !pOperator->pOperatorGetParam) {
42,409,205✔
2031
    return TSDB_CODE_SUCCESS;
×
2032
  }
2033
  
2034
  int32_t                  code = 0;
42,409,205✔
2035
  int32_t                  lino = 0;
42,409,205✔
2036
  SExecTaskInfo*           pTaskInfo = pOperator->pTaskInfo;
42,409,205✔
2037
  SOperatorInfo*           pDownstream = pOperator->pDownstream[0];
42,409,205✔
2038
  SExternalWindowOperator* pExtW = pOperator->info;
42,409,205✔
2039
  SExprSupp*               pSup = &pOperator->exprSupp;
42,409,205✔
2040

2041
  if (pOperator->pOperatorGetParam) {
42,409,205✔
2042
    SOperatorParam*               pParam = (SOperatorParam*)(pOperator->pOperatorGetParam);
42,201,156✔
2043
    SOperatorParam*               pDownParam = (SOperatorParam*)(pOperator->pDownstreamGetParams[0]);
42,201,156✔
2044
    SExchangeOperatorParam*       pExecParam = NULL;
42,201,156✔
2045
    SExternalWindowOperatorParam* pExtPram = (SExternalWindowOperatorParam*)pParam->value;
42,201,156✔
2046

2047
    if (pExtW->pWins) {
42,201,156✔
2048
      taosArrayDestroy(pExtW->pWins);
42,201,156✔
2049
    }
2050

2051
    pExtW->pWins = pExtPram->ExtWins;
42,201,156✔
2052

2053
    TAOS_CHECK_EXIT(extWinInitResultRow(pTaskInfo, pExtW, taosArrayGetSize(pExtW->pWins)));
42,201,156✔
2054
    pExtPram->ExtWins = NULL;
42,201,156✔
2055
    pExtW->outputWinId = 0;
42,201,156✔
2056
    pExtW->lastWinId = -1;
42,201,156✔
2057
    pExtW->blkWinStartIdx = 0;
42,201,156✔
2058
    pExtW->outWinIdx = 0;
42,201,156✔
2059
    pExtW->lastSKey = INT64_MIN;
42,201,156✔
2060
    pExtW->isDynWindow = true;
42,201,156✔
2061
    pExtW->orgTableTimeRange.skey = INT64_MAX;
42,201,156✔
2062
    pExtW->orgTableTimeRange.ekey = INT64_MIN;
42,201,156✔
2063

2064
    QUERY_CHECK_CONDITION(pOperator->numOfDownstream == 1, code, lino, _exit, TSDB_CODE_INVALID_PARA)
42,201,156✔
2065

2066
    switch (pDownParam->opType) {
42,201,156✔
2067
      case QUERY_NODE_PHYSICAL_PLAN_DYN_QUERY_CTRL: {
49,896✔
2068
        break;
49,896✔
2069
      }
2070
      case QUERY_NODE_PHYSICAL_PLAN_EXCHANGE: {
42,151,260✔
2071
        pExecParam = (SExchangeOperatorParam*)((SOperatorParam*)(pOperator->pDownstreamGetParams[0]))->value;
42,151,260✔
2072
        pExecParam->basic.vgId = pExtW->orgTableVgId;
42,151,260✔
2073
        taosArrayClear(pExecParam->basic.uidList);
42,151,260✔
2074
        QUERY_CHECK_NULL(taosArrayPush(pExecParam->basic.uidList, &pExtW->orgTableUid), code, lino, _exit, terrno)
84,302,520✔
2075
        break;
42,151,260✔
2076
      }
2077
      default:
×
2078
        QUERY_CHECK_CONDITION(false, code, lino, _exit, TSDB_CODE_INVALID_PARA)
×
2079
    }
2080
  } else {
2081
    TAOS_CHECK_EXIT(extWinInitWindowList(pExtW, pTaskInfo));
208,049✔
2082
  }
2083

2084
  while (1) {
40,318,430✔
2085
    pExtW->blkWinIdx = -1;
82,727,635✔
2086
    pExtW->blkWinStartSet = false;
82,727,635✔
2087
    pExtW->blkRowStartIdx = 0;
82,727,428✔
2088

2089
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
82,727,635✔
2090
    if (pBlock == NULL) {
82,727,635✔
2091
      if (EEXT_MODE_AGG == pExtW->mode) {
42,409,205✔
2092
        TAOS_CHECK_EXIT(extWinAggHandleEmptyWins(pOperator, pBlock, true, NULL));
42,408,918✔
2093
      }
2094
      pExtW->blkWinStartIdx = pExtW->pWins->size;
42,409,205✔
2095
      break;
42,409,205✔
2096
    }
2097

2098
    if (pExtW->isDynWindow) {
40,318,430✔
2099
      TSKEY skey = 0;
38,026,951✔
2100
      TSKEY ekey = 0;
38,026,951✔
2101
      code = getTimeWindowOfBlock(pBlock, pExtW->primaryTsIndex, &skey, &ekey);
38,026,951✔
2102
      QUERY_CHECK_CODE(code, lino, _exit);
38,026,951✔
2103
      pExtW->orgTableTimeRange.skey = TMIN(pExtW->orgTableTimeRange.skey, skey);
38,026,951✔
2104
      pExtW->orgTableTimeRange.ekey = TMAX(pExtW->orgTableTimeRange.ekey, ekey);
38,026,951✔
2105
    }
2106

2107
    printDataBlock(pBlock, __func__, pTaskInfo->id.str, pTaskInfo->id.queryId);
40,318,430✔
2108

2109
    qDebug("ext window mode:%d got %" PRId64 " rows from downstream", pExtW->mode, pBlock->info.rows);
40,318,430✔
2110
    
2111
    switch (pExtW->mode) {
40,318,215✔
2112
      case EEXT_MODE_SCALAR:
735✔
2113
        TAOS_CHECK_EXIT(extWinProjectOpen(pOperator, pBlock));
735✔
2114
        if (extWinNonAggGotResBlock(pExtW)) {
735✔
2115
          return code;
×
2116
        }
2117
        break;
735✔
2118
      case EEXT_MODE_AGG:
40,317,480✔
2119
        TAOS_CHECK_EXIT(extWinAggOpen(pOperator, pBlock));
40,317,480✔
2120
        break;
40,317,695✔
2121
      case EEXT_MODE_INDEFR_FUNC:
×
2122
        TAOS_CHECK_EXIT(extWinIndefRowsOpen(pOperator, pBlock));
×
2123
        if (extWinNonAggGotResBlock(pExtW)) {
×
2124
          return code;
×
2125
        }
2126
        break;
×
2127
      default:
×
2128
        break;
×
2129
    }
2130
  }
2131

2132
  if (pOperator->pOperatorGetParam) {
42,409,205✔
2133
    freeOperatorParam(pOperator->pOperatorGetParam, OP_GET_PARAM);
42,201,156✔
2134
    pOperator->pOperatorGetParam = NULL;
42,201,156✔
2135
  }
2136
  OPTR_SET_OPENED(pOperator);
42,409,205✔
2137

2138
#if 0
2139
  if (pExtW->mode == EEXT_MODE_AGG) {
2140
    qDebug("ext window before dump final rows num:%d", tSimpleHashGetSize(pExtW->aggSup.pResultRowHashTable));
2141

2142
    code = initGroupedResultInfo(&pExtW->groupResInfo, pExtW->aggSup.pResultRowHashTable, pExtW->binfo.inputTsOrder);
2143
    QUERY_CHECK_CODE(code, lino, _exit);
2144

2145
    qDebug("ext window after dump final rows num:%d", tSimpleHashGetSize(pExtW->aggSup.pResultRowHashTable));
2146
  }
2147
#endif
2148

2149
_exit:
42,409,205✔
2150

2151
  if (code != 0) {
42,409,205✔
2152
    qError("%s failed at line %d since:%s", __func__, lino, tstrerror(code));
×
2153
    pTaskInfo->code = code;
×
2154
    T_LONG_JMP(pTaskInfo->env, code);
×
2155
  }
2156
  
2157
  return code;
42,409,205✔
2158
}
2159

2160
static int32_t extWinNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
42,617,824✔
2161
  int32_t                  code = 0;
42,617,824✔
2162
  int32_t                  lino = 0;
42,617,824✔
2163
  SExternalWindowOperator* pExtW = pOperator->info;
42,617,824✔
2164
  SExecTaskInfo*           pTaskInfo = pOperator->pTaskInfo;
42,617,824✔
2165

2166
  if (pOperator->status == OP_EXEC_DONE && !pOperator->pOperatorGetParam) {
42,617,824✔
2167
    *ppRes = NULL;
×
2168
    return code;
×
2169
  }
2170

2171
  if (pOperator->pOperatorGetParam) {
42,617,824✔
2172
    if (pOperator->status == OP_EXEC_DONE) {
42,201,156✔
2173
      pOperator->status = OP_NOT_OPENED;
41,822,442✔
2174
    }
2175
  }
2176

2177
  extWinRecycleBlkNode(pExtW, &pExtW->pLastBlkNode);
42,617,824✔
2178

2179
  if (pOperator->status == OP_NOT_OPENED) {
42,617,824✔
2180
    TAOS_CHECK_EXIT(pOperator->fpSet._openFn(pOperator));
42,409,205✔
2181
  }
2182

2183
  if (pExtW->mode == EEXT_MODE_SCALAR || pExtW->mode == EEXT_MODE_INDEFR_FUNC) {
42,617,824✔
2184
    TAOS_CHECK_EXIT(extWinNonAggOutputRes(pOperator, ppRes));
511✔
2185
    if (NULL == *ppRes) {
511✔
UNCOV
2186
      setOperatorCompleted(pOperator);
×
UNCOV
2187
      extWinFreeResultRow(pExtW);
×
2188
    }
2189
  } else {
2190
#if 0    
2191
    doBuildResultDatablock(pOperator, &pExtW->binfo, &pExtW->groupResInfo, pExtW->aggSup.pResultBuf);
2192
    bool hasRemain = hasRemainResults(&pExtW->groupResInfo);
2193
    if (!hasRemain) {
2194
      setOperatorCompleted(pOperator);
2195
      break;
2196
    }
2197
    if (pExtW->binfo.pRes->info.rows > 0) break;
2198
#else
2199
    TAOS_CHECK_EXIT(extWinAggOutputRes(pOperator, ppRes));
42,617,313✔
2200
    if (NULL == *ppRes) {
42,617,313✔
2201
      setOperatorCompleted(pOperator);
5,058,852✔
2202
      if (pTaskInfo->pStreamRuntimeInfo) {
5,058,852✔
2203
        extWinFreeResultRow(pExtW);
207,762✔
2204
      }
2205
    }
2206
#endif      
2207
  }
2208

2209
  if (*ppRes) {
42,617,824✔
2210
    pOperator->resultInfo.totalRows += (*ppRes)->info.rows;
37,558,972✔
2211
    printDataBlock(*ppRes, __func__, GET_TASKID(pTaskInfo), pTaskInfo->id.queryId);
37,558,972✔
2212
  }
2213
  
2214
_exit:
5,058,852✔
2215

2216
  if (code) {
42,617,824✔
2217
    qError("%s %s failed at line %d since %s", GET_TASKID(pTaskInfo), __func__, lino, tstrerror(code));
×
UNCOV
2218
    pTaskInfo->code = code;
×
UNCOV
2219
    T_LONG_JMP(pTaskInfo->env, code);
×
2220
  }
2221

2222
  if ((*ppRes) && (*ppRes)->info.rows <= 0) {
42,617,824✔
UNCOV
2223
    *ppRes = NULL;
×
2224
  }
2225

2226
  if (pTaskInfo->execModel == OPTR_EXEC_MODEL_STREAM && (*ppRes)) {
42,617,824✔
2227
    printDataBlock(*ppRes, getStreamOpName(pOperator->operatorType), GET_TASKID(pTaskInfo), pTaskInfo->id.queryId);
208,906✔
2228
  }
2229
  
2230
  return code;
42,617,824✔
2231
}
2232

2233

2234
int32_t createExternalWindowOperator(SOperatorInfo* pDownstream, SPhysiNode* pNode, SExecTaskInfo* pTaskInfo,
449,544✔
2235
                                     SOperatorInfo** pOptrOut) {
2236
  SExternalWindowPhysiNode* pPhynode = (SExternalWindowPhysiNode*)pNode;
449,544✔
2237
  QRY_PARAM_CHECK(pOptrOut);
449,544✔
2238
  int32_t                  code = 0;
449,544✔
2239
  int32_t                  lino = 0;
449,544✔
2240
  SExternalWindowOperator* pExtW = taosMemoryCalloc(1, sizeof(SExternalWindowOperator));
449,544✔
2241
  SOperatorInfo*           pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
449,544✔
2242
  pOperator->pPhyNode = pNode;
449,544✔
2243
  if (!pExtW || !pOperator) {
449,544✔
UNCOV
2244
    code = terrno;
×
UNCOV
2245
    lino = __LINE__;
×
UNCOV
2246
    goto _error;
×
2247
  }
2248
  
2249
  setOperatorInfo(pOperator, "ExternalWindowOperator", QUERY_NODE_PHYSICAL_PLAN_EXTERNAL_WINDOW, true, OP_NOT_OPENED,
449,544✔
2250
                  pExtW, pTaskInfo);
2251
                  
2252
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pPhynode->window.node.pOutputDataBlockDesc);
449,544✔
2253
  QUERY_CHECK_NULL(pResBlock, code, lino, _error, terrno);
449,544✔
2254
  initBasicInfo(&pExtW->binfo, pResBlock);
449,544✔
2255

2256
  pExtW->primaryTsIndex = ((SColumnNode*)pPhynode->window.pTspk)->slotId;
449,544✔
2257
  pExtW->mode = pPhynode->window.pProjs ? EEXT_MODE_SCALAR : (pPhynode->window.indefRowsFunc ? EEXT_MODE_INDEFR_FUNC : EEXT_MODE_AGG);
449,544✔
2258
  pExtW->binfo.inputTsOrder = pPhynode->window.node.inputTsOrder = TSDB_ORDER_ASC;
449,544✔
2259
  pExtW->binfo.outputTsOrder = pExtW->binfo.inputTsOrder;
449,544✔
2260
  pExtW->isDynWindow = false;
449,544✔
2261

2262
  if (pTaskInfo->pStreamRuntimeInfo != NULL){
449,544✔
2263
    pTaskInfo->pStreamRuntimeInfo->funcInfo.withExternalWindow = true;
70,830✔
2264
  }
2265

2266
  // pExtW->limitInfo = (SLimitInfo){0};
2267
  // initLimitInfo(pPhynode->window.node.pLimit, pPhynode->window.node.pSlimit, &pExtW->limitInfo);
2268

2269
  if (pPhynode->window.pProjs) {
449,544✔
2270
    int32_t    numOfScalarExpr = 0;
287✔
2271
    SExprInfo* pScalarExprInfo = NULL;
287✔
2272
    code = createExprInfo(pPhynode->window.pProjs, NULL, &pScalarExprInfo, &numOfScalarExpr);
287✔
2273
    QUERY_CHECK_CODE(code, lino, _error);
287✔
2274

2275
    code = initExprSupp(&pExtW->scalarSupp, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
287✔
2276
    QUERY_CHECK_CODE(code, lino, _error);
287✔
2277

2278
  //if (pExtW->multiTableMode) {
2279
    pExtW->pOutputBlocks = taosArrayInit_s(POINTER_BYTES, STREAM_CALC_REQ_MAX_WIN_NUM);
287✔
2280
    if (!pExtW->pOutputBlocks) QUERY_CHECK_CODE(terrno, lino, _error);
287✔
2281
  //}
2282
    pExtW->pFreeBlocks = tdListNew(POINTER_BYTES * 2);
287✔
2283
    QUERY_CHECK_NULL(pExtW->pFreeBlocks, code, lino, _error, terrno);
287✔
2284
  } else if (pExtW->mode == EEXT_MODE_AGG) {
449,257✔
2285
    if (pPhynode->window.pExprs != NULL) {
449,257✔
2286
      int32_t    num = 0;
1,899✔
2287
      SExprInfo* pSExpr = NULL;
1,899✔
2288
      code = createExprInfo(pPhynode->window.pExprs, NULL, &pSExpr, &num);
1,899✔
2289
      QUERY_CHECK_CODE(code, lino, _error);
1,899✔
2290
    
2291
      code = initExprSupp(&pExtW->scalarSupp, pSExpr, num, &pTaskInfo->storageAPI.functionStore);
1,899✔
2292
      if (code != TSDB_CODE_SUCCESS) {
1,899✔
UNCOV
2293
        goto _error;
×
2294
      }
2295
      checkIndefRowsFuncs(&pExtW->scalarSupp);
1,899✔
2296
    }
2297
    
2298
    size_t keyBufSize = sizeof(int64_t) * 2 + POINTER_BYTES;
449,257✔
2299
    initResultSizeInfo(&pOperator->resultInfo, 4096);
449,257✔
2300
    //code = blockDataEnsureCapacity(pExtW->binfo.pRes, pOperator->resultInfo.capacity);
2301
    //QUERY_CHECK_CODE(code, lino, _error);
2302

2303
    pExtW->pWinRowIdx = taosArrayInit(4096, sizeof(int64_t));
449,257✔
2304
    TSDB_CHECK_NULL(pExtW->pWinRowIdx, code, lino, _error, terrno);
449,257✔
2305
    
2306
    int32_t num = 0;
449,257✔
2307
    SExprInfo* pExprInfo = NULL;
449,257✔
2308
    code = createExprInfo(pPhynode->window.pFuncs, NULL, &pExprInfo, &num);
449,257✔
2309
    QUERY_CHECK_CODE(code, lino, _error);
449,257✔
2310
    pOperator->exprSupp.hasWindow = true;
449,257✔
2311
    pOperator->exprSupp.hasWindowOrGroup = true;
449,257✔
2312
    code = initAggSup(&pOperator->exprSupp, &pExtW->aggSup, pExprInfo, num, keyBufSize, pTaskInfo->id.str, 0, 0);
449,257✔
2313
    QUERY_CHECK_CODE(code, lino, _error);
448,831✔
2314

2315
    nodesWalkExprs(pPhynode->window.pFuncs, extWinHasCountLikeFunc, &pExtW->hasCountFunc);
448,831✔
2316
    if (pExtW->hasCountFunc) {
449,042✔
2317
      code = extWinCreateEmptyInputBlock(pOperator, &pExtW->pEmptyInputBlock);
240,950✔
2318
      QUERY_CHECK_CODE(code, lino, _error);
241,376✔
2319
      qDebug("%s ext window has CountLikeFunc", pOperator->pTaskInfo->id.str);
241,376✔
2320
    } else {
2321
      qDebug("%s ext window doesn't have CountLikeFunc", pOperator->pTaskInfo->id.str);
207,881✔
2322
    }
2323

2324
    code = initExecTimeWindowInfo(&pExtW->twAggSup.timeWindowData, &pTaskInfo->window);
449,257✔
2325
    QUERY_CHECK_CODE(code, lino, _error);
449,257✔
2326

2327
    pExtW->lastSKey = INT64_MIN;
449,257✔
2328
  } else {
2329
    size_t  keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
×
2330
    
2331
    if (pPhynode->window.pExprs != NULL) {
×
2332
      int32_t    num = 0;
×
2333
      SExprInfo* pSExpr = NULL;
×
UNCOV
2334
      code = createExprInfo(pPhynode->window.pExprs, NULL, &pSExpr, &num);
×
UNCOV
2335
      QUERY_CHECK_CODE(code, lino, _error);
×
2336
    
2337
      code = initExprSupp(&pExtW->scalarSupp, pSExpr, num, &pTaskInfo->storageAPI.functionStore);
×
2338
      if (code != TSDB_CODE_SUCCESS) {
×
2339
        goto _error;
×
2340
      }
2341
    }
2342
    
2343
    int32_t    numOfExpr = 0;
×
2344
    SExprInfo* pExprInfo = NULL;
×
2345
    code = createExprInfo(pPhynode->window.pFuncs, NULL, &pExprInfo, &numOfExpr);
×
UNCOV
2346
    TSDB_CHECK_CODE(code, lino, _error);
×
2347
    
UNCOV
2348
    code = initAggSup(&pOperator->exprSupp, &pExtW->aggSup, pExprInfo, numOfExpr, keyBufSize, pTaskInfo->id.str,
×
UNCOV
2349
                              pTaskInfo->streamInfo.pState, &pTaskInfo->storageAPI.functionStore);
×
2350
    TSDB_CHECK_CODE(code, lino, _error);
×
2351
    pOperator->exprSupp.hasWindowOrGroup = false;
×
2352
    
2353
    //code = setFunctionResultOutput(pOperator, &pExtW->binfo, &pExtW->aggSup, MAIN_SCAN, numOfExpr);
2354
    //TSDB_CHECK_CODE(code, lino, _error);
2355
    
2356
    code = filterInitFromNode((SNode*)pNode->pConditions, &pOperator->exprSupp.pFilterInfo, 0,
×
2357
                              pTaskInfo->pStreamRuntimeInfo);
×
UNCOV
2358
    TSDB_CHECK_CODE(code, lino, _error);
×
2359
    
2360
    pExtW->binfo.inputTsOrder = pNode->inputTsOrder;
×
2361
    pExtW->binfo.outputTsOrder = pNode->outputTsOrder;
×
UNCOV
2362
    code = setRowTsColumnOutputInfo(pOperator->exprSupp.pCtx, numOfExpr, &pExtW->pPseudoColInfo);
×
2363
    TSDB_CHECK_CODE(code, lino, _error);
×
2364

2365
  //if (pExtW->multiTableMode) {
UNCOV
2366
    pExtW->pOutputBlocks = taosArrayInit_s(POINTER_BYTES, STREAM_CALC_REQ_MAX_WIN_NUM);
×
UNCOV
2367
    if (!pExtW->pOutputBlocks) QUERY_CHECK_CODE(terrno, lino, _error);
×
2368
  //}
UNCOV
2369
    pExtW->pFreeBlocks = tdListNew(POINTER_BYTES * 2);
×
UNCOV
2370
    QUERY_CHECK_NULL(pExtW->pFreeBlocks, code, lino, _error, terrno);  
×
2371
  }
2372

2373
  pExtW->pWins = taosArrayInit(4096, sizeof(SExtWinTimeWindow));
449,544✔
2374
  if (!pExtW->pWins) QUERY_CHECK_CODE(terrno, lino, _error);
449,544✔
2375
  
2376
  //initResultRowInfo(&pExtW->binfo.resultRowInfo);
2377

2378
  pExtW->timeRangeExpr = (STimeRangeNode*)pPhynode->pTimeRange;
449,544✔
2379
  if (pExtW->timeRangeExpr) {
449,544✔
2380
    QUERY_CHECK_NULL(pExtW->timeRangeExpr->pStart, code, lino, _error, TSDB_CODE_STREAM_INTERNAL_ERROR);
70,830✔
2381
    QUERY_CHECK_NULL(pExtW->timeRangeExpr->pEnd, code, lino, _error, TSDB_CODE_STREAM_INTERNAL_ERROR);
70,830✔
2382
  }
2383

2384
  if (pPhynode->isSingleTable) {
449,544✔
2385
    pExtW->getWinFp = (pExtW->timeRangeExpr && (pExtW->timeRangeExpr->needCalc || (pTaskInfo->pStreamRuntimeInfo->funcInfo.addOptions & CALC_SLIDING_OVERLAP))) ? extWinGetOvlpWin : extWinGetNoOvlpWin;
367,008✔
2386
    pExtW->multiTableMode = false;
367,008✔
2387
  } else {
2388
    pExtW->getWinFp = (pExtW->timeRangeExpr && (pExtW->timeRangeExpr->needCalc || (pTaskInfo->pStreamRuntimeInfo->funcInfo.addOptions & CALC_SLIDING_OVERLAP))) ? extWinGetMultiTbOvlpWin : extWinGetMultiTbNoOvlpWin;
82,536✔
2389
    pExtW->multiTableMode = true;
82,536✔
2390
  }
2391
  pExtW->inputHasOrder = pPhynode->inputHasOrder;
449,544✔
2392
  pExtW->orgTableUid = pPhynode->orgTableUid;
449,544✔
2393
  pExtW->orgTableVgId = pPhynode->orgTableVgId;
449,544✔
2394

2395
  pOperator->fpSet = createOperatorFpSet(extWinOpen, extWinNext, NULL, destroyExternalWindowOperatorInfo,
449,544✔
2396
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
2397
  setOperatorResetStateFn(pOperator, resetExternalWindowOperator);
449,544✔
2398
  code = appendDownstream(pOperator, &pDownstream, 1);
449,544✔
2399
  if (code != 0) {
449,544✔
2400
    goto _error;
×
2401
  }
2402

2403
  *pOptrOut = pOperator;
449,544✔
2404
  return code;
449,544✔
2405

2406
_error:
×
2407

2408
  if (pExtW != NULL) {
×
2409
    destroyExternalWindowOperatorInfo(pExtW);
×
2410
  }
2411

UNCOV
2412
  destroyOperatorAndDownstreams(pOperator, &pDownstream, 1);
×
UNCOV
2413
  pTaskInfo->code = code;
×
UNCOV
2414
  qError("error happens at %s %d, code:%s", __func__, lino, tstrerror(code));
×
UNCOV
2415
  return code;
×
2416
}
2417

2418

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