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

taosdata / TDengine / #4887

16 Dec 2025 08:27AM UTC coverage: 65.289% (-0.003%) from 65.292%
#4887

push

travis-ci

web-flow
feat[TS-7233]: audit (#33850)

377 of 536 new or added lines in 28 files covered. (70.34%)

1025 existing lines in 111 files now uncovered.

178977 of 274129 relevant lines covered (65.29%)

102580217.43 hits per line

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

87.37
/source/libs/executor/src/virtualtablescanoperator.c
1
/*
2
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
*
4
* This program is free software: you can use, redistribute, and/or modify
5
* it under the terms of the GNU Affero General Public License, version 3
6
* or later ("AGPL"), as published by the Free Software Foundation.
7
*
8
* This program is distributed in the hope that it will be useful, but WITHOUT
9
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
* FITNESS FOR A PARTICULAR PURPOSE.
11
*
12
* You should have received a copy of the GNU Affero General Public License
13
* along with this program. If not, see <http://www.gnu.org/licenses/>.
14
*/
15

16
#include "executorInt.h"
17
#include "filter.h"
18
#include "operator.h"
19
#include "querytask.h"
20
#include "tdatablock.h"
21
#include "virtualtablescan.h"
22
#include "tsort.h"
23

24
typedef struct SVirtualTableScanInfo {
25
  STableScanBase base;
26
  SArray*        pSortInfo;
27
  SSortHandle*   pSortHandle;
28
  int32_t        bufPageSize;
29
  uint32_t       sortBufSize;  // max buffer size for in-memory sort
30
  SSDataBlock*   pIntermediateBlock;   // to hold the intermediate result
31
  SSDataBlock*   pInputBlock;
32
  SSHashObj*     dataSlotMap;
33
  int32_t        tsSlotId;
34
  int32_t        orgTsSlotId; // ts slot id of block from origin table, only used for virtual super table to make TS merge key
35
  int32_t        tagBlockId;
36
  int32_t        tagDownStreamId;
37
  bool           scanAllCols;
38
  bool           useOrgTsCol;
39
  SArray*        pSortCtxList;
40
  tb_uid_t       vtableUid;  // virtual table uid, used to identify the vtable scan operator
41
} SVirtualTableScanInfo;
42

43
typedef struct SVirtualScanMergeOperatorInfo {
44
  SOptrBasicInfo        binfo;
45
  EMergeType            type;
46
  SVirtualTableScanInfo virtualScanInfo;
47
  bool                  ignoreGroupId;
48
  uint64_t              groupId;
49
  STupleHandle*         pSavedTuple;
50
  SSDataBlock*          pSavedTagBlock;
51
} SVirtualScanMergeOperatorInfo;
52

53
typedef struct SLoadNextCtx {
54
  SOperatorInfo*  pOperator;
55
  SOperatorParam* pOperatorGetParam;
56
  int32_t         blockId;
57
  STimeWindow     window;
58
  SSDataBlock*    pIntermediateBlock;
59
  col_id_t        tsSlotId;
60
} SLoadNextCtx;
61

62
int32_t virtualScanloadNextDataBlock(void* param, SSDataBlock** ppBlock) {
7,794,851✔
63
  SOperatorInfo* pOperator = (SOperatorInfo*)param;
7,794,851✔
64
  int32_t        code = TSDB_CODE_SUCCESS;
7,794,851✔
65
  int32_t        line = 0;
7,794,851✔
66

67
  VTS_ERR_JRET(pOperator->fpSet.getNextFn(pOperator, ppBlock));
7,794,851✔
68
  VTS_ERR_JRET(blockDataCheck(*ppBlock));
7,794,851✔
69
  if (*ppBlock) {
7,794,851✔
70
    SColumnInfoData* p = taosArrayGet((*ppBlock)->pDataBlock, 0);
5,788,597✔
71
    QUERY_CHECK_NULL(p, code, line, _return, terrno);
5,788,597✔
72
    // ts column will never have null value. set hasNull = false here can accelerate the sort
73
    p->hasNull = false;
5,788,597✔
74
  }
75

76
  return code;
7,794,851✔
77
_return:
×
78
  qError("failed to load data block from downstream, %s code:%s", __func__, tstrerror(code));
×
79
  return code;
×
80
}
81

82
int32_t getTimeWindowOfBlock(SSDataBlock *pBlock, col_id_t tsSlotId, int64_t *startTs, int64_t *endTs) {
208,828✔
83
  int32_t code = TSDB_CODE_SUCCESS;
208,828✔
84
  int32_t lino = 0;
208,828✔
85
  int32_t tsIndex = -1;
208,828✔
86
  for (int32_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); i++) {
245,071✔
87
    if (((SColumnInfoData*)taosArrayGet(pBlock->pDataBlock, i))->info.colId == tsSlotId) {
245,071✔
88
      tsIndex = i;
208,828✔
89
      break;
208,828✔
90
    }
91
  }
92

93
  if (tsIndex == -1) {
208,828✔
94
    tsIndex = (int32_t)taosArrayGetSize(pBlock->pDataBlock) - 1;
×
95
  }
96

97
  SColumnInfoData *pColData = (SColumnInfoData*)taosArrayGet(pBlock->pDataBlock, tsIndex);
208,828✔
98
  QUERY_CHECK_NULL(pColData, code, lino, _return, terrno)
208,828✔
99
  // ts column will never have null value. set hasNull = false here can accelerate the sort
100
  pColData->hasNull = false;
208,828✔
101

102
  GET_TYPED_DATA(*startTs, int64_t, TSDB_DATA_TYPE_TIMESTAMP, colDataGetNumData(pColData, 0), 0);
208,828✔
103
  GET_TYPED_DATA(*endTs, int64_t, TSDB_DATA_TYPE_TIMESTAMP, colDataGetNumData(pColData, pBlock->info.rows - 1), 0);
208,828✔
104

105
  return code;
208,828✔
106
_return:
×
107
  qError("failed to get time window of block, %s code:%s, line:%d", __func__, tstrerror(code), lino);
×
108
  return code;
×
109
}
110

111
int32_t virtualScanloadNextDataBlockFromParam(void* param, SSDataBlock** ppBlock) {
385,147✔
112
  SLoadNextCtx*           pCtx = (SLoadNextCtx*)param;
385,147✔
113
  SOperatorInfo*          pOperator = pCtx->pOperator;
385,147✔
114
  SOperatorParam*         pOperatorGetParam = pCtx->pOperatorGetParam;
385,147✔
115
  int32_t                 code = TSDB_CODE_SUCCESS;
385,147✔
116
  SSDataBlock*            pRes = NULL;
385,147✔
117
  SExchangeOperatorParam* pParam = (SExchangeOperatorParam*)pOperatorGetParam->value;
385,147✔
118

119
  pParam->basic.window = pCtx->window;
385,147✔
120
  pOperator->status = OP_NOT_OPENED;
385,147✔
121
  if (pCtx->pIntermediateBlock) {
385,147✔
122
    blockDataDestroy(pCtx->pIntermediateBlock);
208,828✔
123
    pCtx->pIntermediateBlock = NULL;
208,828✔
124
  }
125

126
  VTS_ERR_JRET(pOperator->fpSet.getNextExtFn(pOperator, pOperatorGetParam, &pRes));
385,147✔
127

128
  pParam->basic.isNewParam = false;
385,147✔
129
  VTS_ERR_JRET(blockDataCheck(pRes));
385,147✔
130
  if ((pRes)) {
385,147✔
131
    qDebug("%s load from downstream, blockId:%d", __func__, pCtx->blockId);
208,828✔
132
    (pRes)->info.id.blockId = pCtx->blockId;
208,828✔
133
    VTS_ERR_JRET(getTimeWindowOfBlock(pRes, pCtx->tsSlotId, &pCtx->window.skey, &pCtx->window.ekey));
208,828✔
134
    VTS_ERR_JRET(createOneDataBlock(pRes, true, &pCtx->pIntermediateBlock));
208,828✔
135
    *ppBlock = pCtx->pIntermediateBlock;
208,828✔
136
  } else {
137
    pCtx->window.ekey = INT64_MAX;
176,319✔
138
    *ppBlock = NULL;
176,319✔
139
  }
140

141
  return code;
385,147✔
142
_return:
×
143
  qError("failed to load data block from downstream, %s code:%s", __func__, tstrerror(code));
×
144
  return code;
×
145
}
146

147
int32_t makeTSMergeKey(SNodeList** pMergeKeys, col_id_t tsSlotId) {
1,034,584✔
148
  int32_t           code = TSDB_CODE_SUCCESS;
1,034,584✔
149
  SNodeList        *pNodeList = NULL;
1,034,584✔
150
  SColumnNode      *pColumnNode = NULL;
1,034,584✔
151
  SOrderByExprNode *pOrderByExprNode = NULL;
1,034,584✔
152

153
  VTS_ERR_JRET(nodesMakeList(&pNodeList));
1,034,584✔
154

155
  VTS_ERR_JRET(nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&pColumnNode));
1,034,584✔
156
  pColumnNode->slotId = tsSlotId;
1,034,584✔
157

158
  VTS_ERR_JRET(nodesMakeNode(QUERY_NODE_ORDER_BY_EXPR, (SNode**)&pOrderByExprNode));
1,034,584✔
159
  pOrderByExprNode->pExpr = (SNode*)pColumnNode;
1,034,584✔
160
  pOrderByExprNode->order = ORDER_ASC;
1,034,584✔
161
  pOrderByExprNode->nullOrder = NULL_ORDER_FIRST;
1,034,584✔
162

163
  VTS_ERR_JRET(nodesListAppend(pNodeList, (SNode*)pOrderByExprNode));
1,034,584✔
164

165
  *pMergeKeys = pNodeList;
1,034,584✔
166
  return code;
1,034,584✔
167
_return:
×
168
  nodesDestroyNode((SNode*)pColumnNode);
×
169
  nodesDestroyNode((SNode*)pOrderByExprNode);
×
170
  nodesDestroyList(pNodeList);
×
171
  return code;
×
172
}
173

174
void cleanUpVirtualScanInfo(SVirtualTableScanInfo* pVirtualScanInfo) {
137,882✔
175
  if (pVirtualScanInfo->pSortInfo) {
137,882✔
176
    taosArrayDestroy(pVirtualScanInfo->pSortInfo);
92,714✔
177
    pVirtualScanInfo->pSortInfo = NULL;
92,714✔
178
  }
179
  if (pVirtualScanInfo->pSortHandle) {
137,882✔
180
    tsortDestroySortHandle(pVirtualScanInfo->pSortHandle);
20,722✔
181
    pVirtualScanInfo->pSortHandle = NULL;
20,722✔
182
  }
183
  if (pVirtualScanInfo->pSortCtxList) {
137,882✔
184
    for (int32_t i = 0; i < taosArrayGetSize(pVirtualScanInfo->pSortCtxList); i++) {
55,574✔
185
      SLoadNextCtx* pCtx = *(SLoadNextCtx**)taosArrayGet(pVirtualScanInfo->pSortCtxList, i);
34,852✔
186
      blockDataDestroy(pCtx->pIntermediateBlock);
34,852✔
187
      taosMemoryFree(pCtx);
34,852✔
188
    }
189
    taosArrayDestroy(pVirtualScanInfo->pSortCtxList);
20,722✔
190
  }
191
}
137,882✔
192

193
int32_t createSortHandleFromParam(SOperatorInfo* pOperator) {
137,882✔
194
  int32_t                         code = TSDB_CODE_SUCCESS;
137,882✔
195
  int32_t                         lino = 0;
137,882✔
196
  SVirtualScanMergeOperatorInfo*  pInfo = pOperator->info;
137,882✔
197
  SVirtualTableScanInfo*          pVirtualScanInfo = &pInfo->virtualScanInfo;
137,882✔
198
  SVTableScanOperatorParam *      pParam = (SVTableScanOperatorParam*)pOperator->pOperatorGetParam->value;
137,882✔
199
  SExecTaskInfo*                  pTaskInfo = pOperator->pTaskInfo;
137,882✔
200
  pVirtualScanInfo->sortBufSize = pVirtualScanInfo->bufPageSize * (taosArrayGetSize((pParam)->pOpParamArray) + 1);
137,882✔
201
  int32_t                         numOfBufPage = (int32_t)pVirtualScanInfo->sortBufSize / pVirtualScanInfo->bufPageSize;
137,882✔
202
  SNodeList*                      pMergeKeys = NULL;
137,882✔
203
  SSortSource*                    ps = NULL;
137,882✔
204
  int32_t                         scanOpIndex = 0;
137,882✔
205

206
  cleanUpVirtualScanInfo(pVirtualScanInfo);
137,882✔
207
  VTS_ERR_JRET(makeTSMergeKey(&pMergeKeys, pVirtualScanInfo->orgTsSlotId));
137,882✔
208
  pVirtualScanInfo->pSortInfo = createSortInfo(pMergeKeys);
137,882✔
209
  TSDB_CHECK_NULL(pVirtualScanInfo->pSortInfo, code, lino, _return, terrno)
137,882✔
210
  nodesDestroyList(pMergeKeys);
137,882✔
211

212
  VTS_ERR_JRET(tsortCreateSortHandle(pVirtualScanInfo->pSortInfo, SORT_MULTISOURCE_TS_MERGE, pVirtualScanInfo->bufPageSize,
137,882✔
213
                                     numOfBufPage, pVirtualScanInfo->pInputBlock, pTaskInfo->id.str, 0, 0, 0, &pVirtualScanInfo->pSortHandle));
214

215
  tsortSetForceUsePQSort(pVirtualScanInfo->pSortHandle);
137,882✔
216
  tsortSetFetchRawDataFp(pVirtualScanInfo->pSortHandle, virtualScanloadNextDataBlockFromParam, NULL, NULL);
137,882✔
217

218
  if (pOperator->numOfDownstream > 2) {
137,882✔
219
    qError("virtual scan operator should not have more than 2 downstreams, current numOfDownstream:%d", pOperator->numOfDownstream);
×
220
    VTS_ERR_JRET(TSDB_CODE_VTABLE_SCAN_INVALID_DOWNSTREAM);
×
221
  }
222

223
  pVirtualScanInfo->tagDownStreamId = -1;
137,882✔
224
  for (int32_t i = 0; i < pOperator->numOfDownstream; ++i) {
306,550✔
225
    SOperatorInfo* pDownstream = pOperator->pDownstream[i];
168,668✔
226
    if (pDownstream->resultDataBlockId == pVirtualScanInfo->tagBlockId) {
168,668✔
227
      // tag block do not need sort
228
      pVirtualScanInfo->tagDownStreamId = i;
30,786✔
229
      pInfo->pSavedTagBlock = NULL;
30,786✔
230
      continue;
30,786✔
231
    }
232
  }
233
  scanOpIndex = pVirtualScanInfo->tagDownStreamId == -1 ? 0 : 1 - pVirtualScanInfo->tagDownStreamId;
137,882✔
234

235
  pOperator->pDownstream[scanOpIndex]->status = OP_NOT_OPENED;
137,882✔
236
  pVirtualScanInfo->pSortCtxList = taosArrayInit(taosArrayGetSize((pParam)->pOpParamArray), POINTER_BYTES);
137,882✔
237
  TSDB_CHECK_NULL(pVirtualScanInfo->pSortCtxList, code, lino, _return, terrno)
137,882✔
238
  for (int32_t i = 0; i < taosArrayGetSize((pParam)->pOpParamArray); i++) {
314,201✔
239
    SOperatorParam* pOpParam = *(SOperatorParam**)taosArrayGet((pParam)->pOpParamArray, i);
176,319✔
240
    SLoadNextCtx*   pCtx = NULL;
176,319✔
241
    ps = NULL;
176,319✔
242

243
    pCtx = taosMemoryMalloc(sizeof(SLoadNextCtx));
176,319✔
244
    QUERY_CHECK_NULL(pCtx, code, lino, _return, terrno)
176,319✔
245
    pCtx->blockId = i;
176,319✔
246
    pCtx->pOperator = pOperator->pDownstream[scanOpIndex];
176,319✔
247
    pCtx->pOperatorGetParam = pOpParam;
176,319✔
248
    pCtx->window = pParam->window;
176,319✔
249
    pCtx->pIntermediateBlock = NULL;
176,319✔
250
    pCtx->tsSlotId = (col_id_t)pVirtualScanInfo->tsSlotId;
176,319✔
251

252
    ps = taosMemoryCalloc(1, sizeof(SSortSource));
176,319✔
253
    QUERY_CHECK_NULL(ps, code, lino, _return, terrno)
176,319✔
254

255
    ps->param = pCtx;
176,319✔
256
    ps->onlyRef = true;
176,319✔
257

258
    VTS_ERR_JRET(tsortAddSource(pVirtualScanInfo->pSortHandle, ps));
176,319✔
259
    QUERY_CHECK_NULL(taosArrayPush(pVirtualScanInfo->pSortCtxList, &pCtx), code, lino, _return, terrno)
352,638✔
260
  }
261

262
  VTS_ERR_JRET(tsortOpen(pVirtualScanInfo->pSortHandle));
137,882✔
263

264
  return code;
137,882✔
265
_return:
×
266
  if (code != 0){
×
267
    qError("%s failed at line %d with msg:%s", __func__, lino, tstrerror(code));
×
268
  }
269
  nodesDestroyList(pMergeKeys);
×
270
  if (ps != NULL) {
×
271
    taosMemoryFree(ps);
×
272
  }
273
  return code;
×
274
}
275

276
int32_t createSortHandle(SOperatorInfo* pOperator) {
890,789✔
277
  SVirtualScanMergeOperatorInfo * pInfo = pOperator->info;
890,789✔
278
  SExecTaskInfo*                  pTaskInfo = pOperator->pTaskInfo;
890,789✔
279
  SVirtualTableScanInfo*          pVirtualScanInfo = &pInfo->virtualScanInfo;
890,789✔
280
  int32_t                         numOfBufPage = (int32_t)pVirtualScanInfo->sortBufSize / pVirtualScanInfo->bufPageSize;
890,789✔
281
  SSortSource*                    ps = NULL;
890,789✔
282
  int32_t                         code = 0;
890,789✔
283
  int32_t                         lino = 0;
890,789✔
284

285
  VTS_ERR_JRET(tsortCreateSortHandle(pVirtualScanInfo->pSortInfo, SORT_MULTISOURCE_TS_MERGE, pVirtualScanInfo->bufPageSize,
890,789✔
286
                                     numOfBufPage, pVirtualScanInfo->pInputBlock, pTaskInfo->id.str, 0, 0, 0, &pVirtualScanInfo->pSortHandle));
287

288
  tsortSetForceUsePQSort(pVirtualScanInfo->pSortHandle);
890,789✔
289
  tsortSetFetchRawDataFp(pVirtualScanInfo->pSortHandle, virtualScanloadNextDataBlock, NULL, NULL);
890,789✔
290

291
  for (int32_t i = 0; i < pOperator->numOfDownstream; ++i) {
2,909,283✔
292
    SOperatorInfo* pDownstream = pOperator->pDownstream[i];
2,018,494✔
293
    if (pDownstream->operatorType == QUERY_NODE_PHYSICAL_PLAN_EXCHANGE) {
2,018,494✔
294
      VTS_ERR_JRET(pDownstream->fpSet._openFn(pDownstream));
2,018,494✔
295
    } else {
296
      VTS_ERR_JRET(TSDB_CODE_VTABLE_SCAN_INVALID_DOWNSTREAM);
×
297
    }
298

299
    if (pDownstream->resultDataBlockId == pVirtualScanInfo->tagBlockId) {
2,018,494✔
300
      // tag block do not need sort
301
      pVirtualScanInfo->tagDownStreamId = i;
12,240✔
302
      continue;
12,240✔
303
    }
304

305
    ps = taosMemoryCalloc(1, sizeof(SSortSource));
2,006,254✔
306
    TSDB_CHECK_NULL(ps, code, lino, _return, terrno)
2,006,254✔
307

308
    ps->param = pDownstream;
2,006,254✔
309
    ps->onlyRef = true;
2,006,254✔
310

311
    VTS_ERR_JRET(tsortAddSource(pVirtualScanInfo->pSortHandle, ps));
2,006,254✔
312
    ps = NULL;
2,006,254✔
313
  }
314

315
  VTS_ERR_JRET(tsortOpen(pVirtualScanInfo->pSortHandle));
890,789✔
316

317
_return:
890,789✔
318
  if (code != 0){
890,789✔
319
    qError("%s failed at line %d with msg:%s", __func__, lino, tstrerror(code));
×
320
  }
321
  if (ps != NULL) {
890,789✔
322
    taosMemoryFree(ps);
×
323
  }
324
  return code;
890,789✔
325
}
326

327
int32_t openVirtualTableScanOperatorImpl(SOperatorInfo* pOperator) {
1,034,584✔
328
  int32_t                         code = 0;
1,034,584✔
329
  int32_t                         lino = 0;
1,034,584✔
330

331
  if (pOperator->numOfDownstream == 0) {
1,034,584✔
332
    return code;
5,913✔
333
  }
334

335
  if (pOperator->pOperatorGetParam) {
1,028,671✔
336
    VTS_ERR_JRET(createSortHandleFromParam(pOperator));
137,882✔
337
  } else {
338
    VTS_ERR_JRET(createSortHandle(pOperator));
890,789✔
339
  }
340

341
  return code;
1,028,671✔
342

343
_return:
×
344
  qError("%s failed at line %d with msg:%s", __func__, lino, tstrerror(code));
×
345
  return code;
×
346
}
347

348
int32_t openVirtualTableScanOperator(SOperatorInfo* pOperator) {
5,205,069✔
349
  int32_t code = 0;
5,205,069✔
350

351
  if (OPTR_IS_OPENED(pOperator)) {
5,205,069✔
352
    return TSDB_CODE_SUCCESS;
4,170,485✔
353
  }
354

355
  int64_t startTs = taosGetTimestampUs();
1,034,584✔
356

357
  code = openVirtualTableScanOperatorImpl(pOperator);
1,034,584✔
358

359
  pOperator->cost.openCost = (double)(taosGetTimestampUs() - startTs) / 1000.0;
1,034,584✔
360
  pOperator->status = OP_RES_TO_RETURN;
1,034,584✔
361

362
  VTS_ERR_RET(code);
1,034,584✔
363

364
  OPTR_SET_OPENED(pOperator);
1,034,584✔
365
  return code;
1,034,584✔
366
}
367

368
static int32_t doGetVtableMergedBlockData(SVirtualScanMergeOperatorInfo* pInfo, SSortHandle* pHandle, int32_t capacity,
5,153,626✔
369
                                          SSDataBlock* p) {
370
  int32_t code = 0;
5,153,626✔
371
  int64_t lastTs = 0;
5,153,626✔
372
  int64_t rowNums = -1;
5,153,626✔
373
  blockDataEmpty(p);
5,153,626✔
374
  for (int32_t j = 0; j < taosArrayGetSize(p->pDataBlock); j++) {
48,919,148✔
375
    colDataSetNItemsNull(taosArrayGet(p->pDataBlock, j), 0, capacity);
43,765,522✔
376
  }
377
  while (1) {
2,147,483,647✔
378
    STupleHandle* pTupleHandle = NULL;
2,147,483,647✔
379
    if (!pInfo->pSavedTuple) {
2,147,483,647✔
380
      code = tsortNextTuple(pHandle, &pTupleHandle);
2,147,483,647✔
381
      if (pTupleHandle == NULL || (code != 0)) {
2,147,483,647✔
382
        break;
383
      }
384
    } else {
385
      pTupleHandle = pInfo->pSavedTuple;
3,396,492✔
386
      pInfo->pSavedTuple = NULL;
3,396,492✔
387
    }
388

389
    int32_t blockId = (int32_t)tsortGetBlockId(pTupleHandle);
2,147,483,647✔
390
    int32_t colNum = pInfo->virtualScanInfo.scanAllCols ? 1 : tsortGetColNum(pTupleHandle);
2,147,483,647✔
391
    for (int32_t i = 0; i < colNum; i++) {
2,147,483,647✔
392
      char* pData = NULL;
2,147,483,647✔
393
      tsortGetValue(pTupleHandle, i, (void**)&pData);
2,147,483,647✔
394
      if (pData != NULL) {
2,147,483,647✔
395
        if (i == 0) {
2,147,483,647✔
396
          if (lastTs != *(int64_t*)pData) {
2,147,483,647✔
397
            if (rowNums >= capacity - 1) {
2,147,483,647✔
398
              pInfo->pSavedTuple = pTupleHandle;
3,396,492✔
399
              goto _return;
3,396,492✔
400
            }
401
            rowNums++;
2,147,483,647✔
402
            if (pInfo->virtualScanInfo.tsSlotId != -1) {
2,147,483,647✔
403
              VTS_ERR_RET(colDataSetVal(taosArrayGet(p->pDataBlock, pInfo->virtualScanInfo.tsSlotId), rowNums, pData, false));
2,147,483,647✔
404
            }
405
            lastTs = *(int64_t*)pData;
2,147,483,647✔
406
          }
407
          if (pInfo->virtualScanInfo.useOrgTsCol) {
2,147,483,647✔
408
            int32_t slotKey = blockId << 16 | i;
6,105,287✔
409
            void*   slotId = tSimpleHashGet(pInfo->virtualScanInfo.dataSlotMap, &slotKey, sizeof(slotKey));
58,967✔
410
            if (slotId) {
58,967✔
411
              VTS_ERR_RET(colDataSetVal(taosArrayGet(p->pDataBlock, *(int32_t *)slotId), rowNums, pData, false));
52,487✔
412
            }
413
          }
414
          continue;
2,147,483,647✔
415
        }
416
        if (tsortIsNullVal(pTupleHandle, i)) {
2,147,483,647✔
417
          continue;
242,544✔
418
        }
419
        int32_t slotKey = blockId << 16 | i;
2,147,483,647✔
420
        void*   slotId = tSimpleHashGet(pInfo->virtualScanInfo.dataSlotMap, &slotKey, sizeof(slotKey));
2,147,483,647✔
421
        if (slotId == NULL) {
2,147,483,647✔
422
          qError("failed to get slotId from dataSlotMap, blockId:%d, slotId:%d", blockId, i);
×
423
          VTS_ERR_RET(TSDB_CODE_VTABLE_SCAN_INTERNAL_ERROR);
×
424
        }
425
        VTS_ERR_RET(colDataSetVal(taosArrayGet(p->pDataBlock, *(int32_t *)slotId), rowNums, pData, false));
2,147,483,647✔
426
      }
427
    }
428
  }
429
_return:
5,153,626✔
430
  p->info.rows = rowNums + 1;
5,153,626✔
431
  p->info.dataLoad = 1;
5,153,626✔
432
  p->info.scanFlag = MAIN_SCAN;
5,153,626✔
433
  return code;
5,153,626✔
434
}
435

436
static int32_t doGetVStableMergedBlockData(SVirtualScanMergeOperatorInfo* pInfo, SSortHandle* pHandle, int32_t capacity,
283,026✔
437
                                           SSDataBlock* p) {
438
  int32_t code = 0;
283,026✔
439
  int64_t lastTs = 0;
283,026✔
440
  int64_t rowNums = -1;
283,026✔
441
  blockDataEmpty(p);
283,026✔
442
  for (int32_t j = 0; j < taosArrayGetSize(p->pDataBlock); j++) {
2,918,590✔
443
    colDataSetNItemsNull(taosArrayGet(p->pDataBlock, j), 0, capacity);
2,635,564✔
444
  }
445
  while (1) {
376,547,268✔
446
    STupleHandle* pTupleHandle = NULL;
376,830,294✔
447
    if (!pInfo->pSavedTuple) {
376,830,294✔
448
      code = tsortNextTuple(pHandle, &pTupleHandle);
376,771,164✔
449
      if (pTupleHandle == NULL || (code != 0)) {
376,771,164✔
450
        break;
451
      }
452
    } else {
453
      pTupleHandle = pInfo->pSavedTuple;
59,130✔
454
      pInfo->pSavedTuple = NULL;
59,130✔
455
    }
456

457
    int32_t tsIndex = -1;
376,606,398✔
458
    int32_t colNum = tsortGetColNum(pTupleHandle);
376,606,398✔
459

460
    for (int32_t i = 0; i < colNum; i++) {
377,070,651✔
461
      SColumnInfoData *pColInfo = NULL;
377,070,651✔
462
      tsortGetColumnInfo(pTupleHandle, i, &pColInfo);
377,070,651✔
463
      if (pColInfo && pColInfo->info.slotId ==  pInfo->virtualScanInfo.tsSlotId) {
377,070,651✔
464
        tsIndex = i;
376,606,398✔
465
        break;
376,606,398✔
466
      }
467
    }
468

469
    if (tsIndex == -1) {
376,606,398✔
470
      tsIndex = colNum - 1;
×
471
    }
472

473
    char* pData = NULL;
376,606,398✔
474
    // first, set ts slot's data
475
    // then, set other slots' data
476
    tsortGetValue(pTupleHandle, tsIndex, (void**)&pData);
376,606,398✔
477

478
    if (pData != NULL) {
376,606,398✔
479
      if (lastTs != *(int64_t*)pData) {
376,606,398✔
480
        if (rowNums >= capacity - 1) {
277,889,178✔
481
          pInfo->pSavedTuple = pTupleHandle;
59,130✔
482
          goto _return;
59,130✔
483
        }
484
        rowNums++;
277,830,048✔
485
        if (pInfo->virtualScanInfo.tsSlotId != -1) {
277,830,048✔
486
          VTS_ERR_RET(colDataSetVal(taosArrayGet(p->pDataBlock, pInfo->virtualScanInfo.tsSlotId), rowNums, pData, false));
277,830,048✔
487
        }
488
        lastTs = *(int64_t*)pData;
277,830,048✔
489
      }
490
    }
491
    if (pInfo->virtualScanInfo.scanAllCols) {
376,547,268✔
UNCOV
492
      continue;
×
493
    }
494

495
    for (int32_t i = 0; i < colNum; i++) {
2,147,483,647✔
496
      if (i == tsIndex || tsortIsNullVal(pTupleHandle, i)) {
2,147,483,647✔
497
        continue;
2,147,483,647✔
498
      }
499

500
      tsortGetValue(pTupleHandle, i, (void**)&pData);
1,521,337,352✔
501

502
      if (pData != NULL) {
1,521,337,352✔
503
        VTS_ERR_RET(colDataSetVal(taosArrayGet(p->pDataBlock, i), rowNums, pData, false));
1,521,337,352✔
504
      }
505
    }
506
  }
507
_return:
283,026✔
508
  p->info.rows = rowNums + 1;
283,026✔
509
  p->info.dataLoad = 1;
283,026✔
510
  p->info.scanFlag = MAIN_SCAN;
283,026✔
511
  return code;
283,026✔
512
}
513

514
int32_t doVirtualTableMerge(SOperatorInfo* pOperator, SSDataBlock** pResBlock) {
5,442,565✔
515
  int32_t                        code = 0;
5,442,565✔
516
  SExecTaskInfo*                 pTaskInfo = pOperator->pTaskInfo;
5,442,565✔
517
  SVirtualScanMergeOperatorInfo* pInfo = pOperator->info;
5,442,565✔
518
  SVirtualTableScanInfo*         pVirtualScanInfo = &pInfo->virtualScanInfo;
5,442,565✔
519
  SSortHandle*                   pHandle = pVirtualScanInfo->pSortHandle;
5,442,565✔
520
  SSDataBlock*                   pDataBlock = pInfo->binfo.pRes;
5,442,565✔
521
  int32_t                        capacity = pOperator->resultInfo.capacity;
5,442,565✔
522

523
  qDebug("start to merge final sorted rows, %s", GET_TASKID(pTaskInfo));
5,442,565✔
524
  blockDataCleanup(pDataBlock);
5,442,565✔
525

526
  if (pHandle == NULL) {
5,442,565✔
527
    return TSDB_CODE_SUCCESS;
5,913✔
528
  }
529

530
  if (pVirtualScanInfo->pIntermediateBlock == NULL) {
5,436,652✔
531
    VTS_ERR_RET(tsortGetSortedDataBlock(pHandle, &pVirtualScanInfo->pIntermediateBlock));
1,007,949✔
532
    if (pVirtualScanInfo->pIntermediateBlock == NULL) {
1,007,949✔
533
      return TSDB_CODE_SUCCESS;
×
534
    }
535

536
    VTS_ERR_RET(blockDataEnsureCapacity(pVirtualScanInfo->pIntermediateBlock, capacity));
1,007,949✔
537
  } else {
538
    blockDataCleanup(pVirtualScanInfo->pIntermediateBlock);
4,428,703✔
539
  }
540

541
  SSDataBlock* p = pVirtualScanInfo->pIntermediateBlock;
5,436,652✔
542
  if (pOperator->pOperatorGetParam) {
5,436,652✔
543
    VTS_ERR_RET(doGetVStableMergedBlockData(pInfo, pHandle, capacity, p));
283,026✔
544
  } else {
545
    VTS_ERR_RET(doGetVtableMergedBlockData(pInfo, pHandle, capacity, p));
5,153,626✔
546
  }
547

548
  VTS_ERR_RET(copyDataBlock(pDataBlock, p));
5,436,652✔
549
  qDebug("%s %s get sorted block, groupId:0x%" PRIx64 " rows:%" PRId64 , GET_TASKID(pTaskInfo), __func__, pDataBlock->info.id.groupId,
5,436,652✔
550
         pDataBlock->info.rows);
551

552
  *pResBlock = (pDataBlock->info.rows > 0) ? pDataBlock : NULL;
5,436,652✔
553
  return code;
5,436,652✔
554
}
555

556
int32_t vtableAddTagPseudoColumnData(const SExprInfo* pExpr, int32_t numOfExpr, SSDataBlock* tagBlock, SSDataBlock* pBlock, int32_t rows) {
85,974✔
557
  int32_t          code = TSDB_CODE_SUCCESS;
85,974✔
558
  int32_t          lino = 0;
85,974✔
559
  int64_t          backupRows;
560
  // currently only the tbname pseudo column
561
  if (numOfExpr <= 0) {
85,974✔
562
    return TSDB_CODE_SUCCESS;
×
563
  }
564

565
  if (tagBlock == NULL) {
85,974✔
566
    return TSDB_CODE_SUCCESS;
×
567
  }
568

569
  if (tagBlock->info.rows != 1) {
85,974✔
570
    qError("tag block should have only one row, current rows:%" PRId64, tagBlock->info.rows);
×
571
    VTS_ERR_JRET(TSDB_CODE_VTABLE_SCAN_INTERNAL_ERROR);
×
572
  }
573

574
  backupRows = pBlock->info.rows;
85,974✔
575
  pBlock->info.rows = rows;
85,974✔
576
  for (int32_t j = 0; j < numOfExpr; ++j) {
533,207✔
577
    const SExprInfo* pExpr1 = &pExpr[j];
447,233✔
578
    int32_t          dstSlotId = pExpr1->base.resSchema.slotId;
447,233✔
579

580
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, dstSlotId);
447,233✔
581
    TSDB_CHECK_NULL(pColInfoData, code, lino, _return, terrno)
447,233✔
582
    colInfoDataCleanup(pColInfoData, pBlock->info.rows);
447,233✔
583

584
    SColumnInfoData* pTagInfoData = taosArrayGet(tagBlock->pDataBlock, j);
447,233✔
585
    TSDB_CHECK_NULL(pTagInfoData, code, lino, _return, terrno)
447,233✔
586

587
    if (colDataIsNull_s(pTagInfoData, 0) || IS_JSON_NULL(pTagInfoData->info.type, colDataGetData(pTagInfoData, 0))) {
447,233✔
588
      colDataSetNNULL(pColInfoData, 0, pBlock->info.rows);
×
589
      continue;
×
590
    }
591

592
    char* data = colDataGetData(pTagInfoData, 0);
447,233✔
593

594
    if (pColInfoData->info.type != TSDB_DATA_TYPE_JSON) {
447,233✔
595
      code = colDataSetNItems(pColInfoData, 0, data, pBlock->info.rows, 1, false);
447,233✔
596
      QUERY_CHECK_CODE(code, lino, _return);
447,233✔
597
    } else {  // todo opt for json tag
598
      for (int32_t i = 0; i < pBlock->info.rows; ++i) {
×
599
        code = colDataSetVal(pColInfoData, i, data, false);
×
600
        QUERY_CHECK_CODE(code, lino, _return);
×
601
      }
602
    }
603
  }
604

605
  // restore the rows
606
  pBlock->info.rows = backupRows;
85,974✔
607

608
_return:
85,974✔
609

610
  if (code != TSDB_CODE_SUCCESS) {
85,974✔
611
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
612
  }
613
  return code;
85,974✔
614
}
615

616
static int32_t doSetTagColumnData(SVirtualTableScanInfo* pInfo, SSDataBlock* pTagBlock, SSDataBlock* pBlock, int32_t rows) {
4,407,981✔
617
  int32_t         code = 0;
4,407,981✔
618
  STableScanBase* pTableScanInfo = &pInfo->base;
4,407,981✔
619
  SExprSupp*      pSup = &pTableScanInfo->pseudoSup;
4,407,981✔
620
  if (pSup->numOfExprs > 0) {
4,407,981✔
621
    VTS_ERR_RET(vtableAddTagPseudoColumnData(pSup->pExprInfo, pSup->numOfExprs, pTagBlock, pBlock, rows));
85,974✔
622
  }
623

624
  return code;
4,407,981✔
625
}
626

627
int32_t   virtualTableGetNext(SOperatorInfo* pOperator, SSDataBlock** pResBlock) {
5,205,069✔
628
  int32_t                        code = TSDB_CODE_SUCCESS;
5,205,069✔
629
  int32_t                        lino = 0;
5,205,069✔
630
  SVirtualScanMergeOperatorInfo* pInfo = pOperator->info;
5,205,069✔
631
  SVirtualTableScanInfo*         pVirtualScanInfo = &pInfo->virtualScanInfo;
5,205,069✔
632
  SExecTaskInfo*                 pTaskInfo = pOperator->pTaskInfo;
5,205,069✔
633

634
  if (pOperator->status == OP_EXEC_DONE && !pOperator->pOperatorGetParam) {
5,205,069✔
635
    pResBlock = NULL;
×
636
    return code;
×
637
  }
638

639
  VTS_ERR_JRET(pOperator->fpSet._openFn(pOperator));
5,205,069✔
640

641
  if (pVirtualScanInfo->tagBlockId != -1 && pVirtualScanInfo->tagDownStreamId != -1 && !pInfo->pSavedTagBlock) {
5,205,069✔
642
    SSDataBlock*   pTagBlock = NULL;
43,026✔
643
    SOperatorInfo *pTagScanOp = pOperator->pDownstream[pVirtualScanInfo->tagDownStreamId];
43,026✔
644
    if (pOperator->pOperatorGetParam) {
43,026✔
645
      SOperatorParam* pTagOp = ((SVTableScanOperatorParam*)pOperator->pOperatorGetParam->value)->pTagScanOp;
30,786✔
646
      VTS_ERR_JRET(pTagScanOp->fpSet.getNextExtFn(pTagScanOp, pTagOp, &pTagBlock));
30,786✔
647
    } else {
648
      VTS_ERR_JRET(pTagScanOp->fpSet.getNextFn(pTagScanOp, &pTagBlock));
12,240✔
649
    }
650

651
    if (pTagBlock == NULL || pTagBlock->info.rows != 1) {
43,026✔
652
      VTS_ERR_JRET(TSDB_CODE_FAILED);
×
653
    }
654
    pInfo->pSavedTagBlock = pTagBlock;
43,026✔
655
  }
656

657
  while(1) {
658
    VTS_ERR_JRET(doVirtualTableMerge(pOperator, pResBlock));
5,442,565✔
659
    if (*pResBlock == NULL) {
5,442,565✔
660
      setOperatorCompleted(pOperator);
1,034,584✔
661
      break;
1,034,584✔
662
    }
663

664
    if (pOperator->pOperatorGetParam) {
4,407,981✔
665
      uint64_t uid = ((SVTableScanOperatorParam*)pOperator->pOperatorGetParam->value)->uid;
145,144✔
666
      (*pResBlock)->info.id.uid = uid;
145,144✔
667

668
      qTrace("vtable scan uid:%" PRIu64, (*pResBlock)->info.id.uid);
145,144✔
669
    } else {
670
      (*pResBlock)->info.id.uid = pInfo->virtualScanInfo.vtableUid;
4,262,837✔
671

672
      qTrace("vtable scan vtb uid:%" PRIu64, (*pResBlock)->info.id.uid);
4,262,837✔
673
    }
674

675
    VTS_ERR_JRET(doSetTagColumnData(pVirtualScanInfo, pInfo->pSavedTagBlock, (*pResBlock), (*pResBlock)->info.rows));
4,407,981✔
676
    VTS_ERR_JRET(doFilter(*pResBlock, pOperator->exprSupp.pFilterInfo, NULL, NULL));
4,407,981✔
677
    if ((*pResBlock)->info.rows > 0) {
4,407,981✔
678
      break;
4,170,485✔
679
    }
680
  }
681

682
  return code;
5,205,069✔
683
_return:
×
684
  qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
685
  pTaskInfo->code = code;
×
686
  T_LONG_JMP(pTaskInfo->env, code);
×
687
}
688

689
static void destroyTableScanBase(STableScanBase* pBase, TsdReader* pAPI) {
941,870✔
690
  cleanupQueryTableDataCond(&pBase->cond);
941,870✔
691

692
  if (pAPI->tsdReaderClose) {
941,870✔
693
    pAPI->tsdReaderClose(pBase->dataReader);
×
694
  }
695
  pBase->dataReader = NULL;
941,870✔
696

697
  if (pBase->matchInfo.pList != NULL) {
941,870✔
698
    taosArrayDestroy(pBase->matchInfo.pList);
×
699
  }
700

701
  taosLRUCacheCleanup(pBase->metaCache.pTableMetaEntryCache);
941,870✔
702
  cleanupExprSupp(&pBase->pseudoSup);
941,870✔
703
}
941,870✔
704

705
void destroyVirtualTableScanOperatorInfo(void* param) {
941,870✔
706
  if (!param) {
941,870✔
707
    return;
×
708
  }
709
  SVirtualScanMergeOperatorInfo* pOperatorInfo = (SVirtualScanMergeOperatorInfo*)param;
941,870✔
710
  SVirtualTableScanInfo* pInfo = &pOperatorInfo->virtualScanInfo;
941,870✔
711
  blockDataDestroy(pOperatorInfo->binfo.pRes);
941,870✔
712
  pOperatorInfo->binfo.pRes = NULL;
941,870✔
713

714
  tsortDestroySortHandle(pInfo->pSortHandle);
941,870✔
715
  pInfo->pSortHandle = NULL;
941,870✔
716
  taosArrayDestroy(pInfo->pSortInfo);
941,870✔
717
  pInfo->pSortInfo = NULL;
941,870✔
718

719
  blockDataDestroy(pInfo->pIntermediateBlock);
941,870✔
720
  pInfo->pIntermediateBlock = NULL;
941,870✔
721

722
  blockDataDestroy(pInfo->pInputBlock);
941,870✔
723
  pInfo->pInputBlock = NULL;
941,870✔
724
  destroyTableScanBase(&pInfo->base, &pInfo->base.readerAPI);
941,870✔
725

726
  tSimpleHashCleanup(pInfo->dataSlotMap);
941,870✔
727

728
  if (pInfo->pSortCtxList) {
941,870✔
729
    for (int32_t i = 0; i < taosArrayGetSize(pInfo->pSortCtxList); i++) {
31,497✔
730
      SLoadNextCtx* pCtx = *(SLoadNextCtx**)taosArrayGet(pInfo->pSortCtxList, i);
22,475✔
731
      blockDataDestroy(pCtx->pIntermediateBlock);
22,475✔
732
      taosMemoryFree(pCtx);
22,475✔
733
    }
734
    taosArrayDestroy(pInfo->pSortCtxList);
9,022✔
735
    pInfo->pSortCtxList = NULL;
9,022✔
736
  }
737
  taosMemoryFreeClear(param);
941,870✔
738
}
739

740
int32_t extractColMap(SNodeList* pNodeList, SSHashObj** pSlotMap, int32_t *tsSlotId, int32_t *orgTsSlotId, int32_t *tagBlockId, bool* useOriginTs) {
941,870✔
741
  size_t  numOfCols = LIST_LENGTH(pNodeList);
941,870✔
742
  int32_t code = TSDB_CODE_SUCCESS;
941,870✔
743
  int32_t lino = 0;
941,870✔
744

745
  if (numOfCols == 0) {
941,870✔
746
    return code;
×
747
  }
748

749
  *tsSlotId = -1;
941,870✔
750
  *orgTsSlotId = numOfCols;
941,870✔
751
  *tagBlockId = -1;
941,870✔
752
  *useOriginTs = false;
941,870✔
753
  *pSlotMap = tSimpleHashInit(numOfCols, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
941,870✔
754
  TSDB_CHECK_NULL(*pSlotMap, code, lino, _return, terrno);
941,870✔
755

756
  for (int32_t i = 0; i < numOfCols; ++i) {
19,767,478✔
757
    SColumnNode* pColNode = (SColumnNode*)nodesListGetNode(pNodeList, i);
18,825,608✔
758
    TSDB_CHECK_NULL(pColNode, code, lino, _return, terrno)
18,825,608✔
759

760
    if (pColNode->isPrimTs || pColNode->colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
18,825,608✔
761
      *tsSlotId = i;
933,710✔
762
      *orgTsSlotId = i;
933,710✔
763
    } else if (pColNode->hasRef) {
17,891,898✔
764
      int32_t slotKey = pColNode->dataBlockId << 16 | pColNode->slotId;
17,555,930✔
765
      if (pColNode->slotId == 0) {
17,555,930✔
766
        *useOriginTs = true;
16,267✔
767
      }
768
      VTS_ERR_JRET(tSimpleHashPut(*pSlotMap, &slotKey, sizeof(slotKey), &i, sizeof(i)));
17,555,930✔
769
    } else if (pColNode->colType == COLUMN_TYPE_TAG || '\0' == pColNode->tableAlias[0]) {
335,968✔
770
      // tag column or pseudo column's function
771
      *tagBlockId = pColNode->dataBlockId;
46,631✔
772
    }
773
  }
774

775
  return code;
941,870✔
776
_return:
×
777
  tSimpleHashCleanup(*pSlotMap);
×
778
  *pSlotMap = NULL;
×
779
  if (code != TSDB_CODE_SUCCESS) {
×
780
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
781
  }
782
  return code;
×
783
}
784

785
int32_t resetVirtualTableMergeOperState(SOperatorInfo* pOper) {
2,010,416✔
786
  int32_t code = 0, lino = 0;
2,010,416✔
787
  SVirtualScanMergeOperatorInfo* pMergeInfo = pOper->info;
2,010,416✔
788
  SVirtualScanPhysiNode* pPhynode = (SVirtualScanPhysiNode*)pOper->pPhyNode;
2,010,416✔
789
  SVirtualTableScanInfo* pInfo = &pMergeInfo->virtualScanInfo;
2,010,416✔
790
  
791
  pOper->status = OP_NOT_OPENED;
2,010,416✔
792
  resetBasicOperatorState(&pMergeInfo->binfo);
2,010,416✔
793

794
  tsortDestroySortHandle(pInfo->pSortHandle);
2,010,416✔
795
  pInfo->pSortHandle = NULL;
2,010,416✔
796
  // taosArrayDestroy(pInfo->pSortInfo);
797
  // pInfo->pSortInfo = NULL;
798

799
  blockDataDestroy(pInfo->pIntermediateBlock);
2,010,416✔
800
  pInfo->pIntermediateBlock = NULL;
2,009,612✔
801

802
  blockDataDestroy(pInfo->pInputBlock);
2,009,612✔
803
  pInfo->pInputBlock = createDataBlockFromDescNode(((SPhysiNode*)pPhynode)->pOutputDataBlockDesc);
2,010,416✔
804
  TSDB_CHECK_NULL(pInfo->pInputBlock, code, lino, _exit, terrno)
2,010,416✔
805

806
  pInfo->tagDownStreamId = -1;
2,010,416✔
807

808
  if (pInfo->pSortCtxList) {
2,010,416✔
809
    for (int32_t i = 0; i < taosArrayGetSize(pInfo->pSortCtxList); i++) {
227,130✔
810
      SLoadNextCtx* pCtx = *(SLoadNextCtx**)taosArrayGet(pInfo->pSortCtxList, i);
118,992✔
811
      blockDataDestroy(pCtx->pIntermediateBlock);
118,992✔
812
      taosMemoryFree(pCtx);
118,992✔
813
    }
814
    taosArrayDestroy(pInfo->pSortCtxList);
108,138✔
815
    pInfo->pSortCtxList = NULL;
108,138✔
816
  }
817

818
  pMergeInfo->pSavedTuple = NULL;
2,010,014✔
819
  pMergeInfo->pSavedTagBlock = NULL;
2,010,014✔
820

821
_exit:
2,010,416✔
822

823
  if (code) {
2,010,416✔
824
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
825
  }
826

827
  return code;
2,010,416✔
828
}
829

830
int32_t createVirtualTableMergeOperatorInfo(SOperatorInfo** pDownstream, int32_t numOfDownstream,
941,870✔
831
                                            SVirtualScanPhysiNode* pVirtualScanPhyNode,
832
                                            SExecTaskInfo* pTaskInfo, SOperatorInfo** pOptrInfo) {
833
  SPhysiNode*                    pPhyNode = (SPhysiNode*)pVirtualScanPhyNode;
941,870✔
834
  int32_t                        lino = 0;
941,870✔
835
  int32_t                        code = TSDB_CODE_SUCCESS;
941,870✔
836
  SVirtualScanMergeOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SVirtualScanMergeOperatorInfo));
941,870✔
837
  SOperatorInfo*                 pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
941,870✔
838
  SDataBlockDescNode*            pDescNode = pPhyNode->pOutputDataBlockDesc;
941,870✔
839
  SNodeList*                     pMergeKeys = NULL;
941,870✔
840

841
  QUERY_CHECK_NULL(pInfo, code, lino, _return, terrno)
941,870✔
842
  QUERY_CHECK_NULL(pOperator, code, lino, _return, terrno)
941,870✔
843

844
  pOperator->pPhyNode = pVirtualScanPhyNode;
941,870✔
845

846
  pInfo->binfo.inputTsOrder = pVirtualScanPhyNode->scan.node.inputTsOrder;
941,870✔
847
  pInfo->binfo.outputTsOrder = pVirtualScanPhyNode->scan.node.outputTsOrder;
941,870✔
848

849
  SVirtualTableScanInfo* pVirtualScanInfo = &pInfo->virtualScanInfo;
941,870✔
850
  pInfo->binfo.pRes = createDataBlockFromDescNode(pDescNode);
941,870✔
851
  TSDB_CHECK_NULL(pInfo->binfo.pRes, code, lino, _return, terrno)
941,870✔
852

853
  SSDataBlock* pInputBlock = createDataBlockFromDescNode(pPhyNode->pOutputDataBlockDesc);
941,870✔
854
  TSDB_CHECK_NULL(pInputBlock, code, lino, _return, terrno)
941,870✔
855
  pVirtualScanInfo->pInputBlock = pInputBlock;
941,870✔
856
  pVirtualScanInfo->tagDownStreamId = -1;
941,870✔
857
  pVirtualScanInfo->vtableUid = (tb_uid_t)pVirtualScanPhyNode->scan.uid;
941,870✔
858
  if (pVirtualScanPhyNode->scan.pScanPseudoCols != NULL) {
941,870✔
859
    SExprSupp* pSup = &pVirtualScanInfo->base.pseudoSup;
22,386✔
860
    pSup->pExprInfo = NULL;
22,386✔
861
    VTS_ERR_JRET(createExprInfo(pVirtualScanPhyNode->scan.pScanPseudoCols, NULL, &pSup->pExprInfo, &pSup->numOfExprs));
22,386✔
862

863
    pSup->pCtx = createSqlFunctionCtx(pSup->pExprInfo, pSup->numOfExprs, &pSup->rowEntryInfoOffset,
22,386✔
864
                                      &pTaskInfo->storageAPI.functionStore);
865
    TSDB_CHECK_NULL(pSup->pCtx, code, lino, _return, terrno)
22,386✔
866
  }
867

868
  initResultSizeInfo(&pOperator->resultInfo, 4096);
941,870✔
869
  TSDB_CHECK_CODE(blockDataEnsureCapacity(pInfo->binfo.pRes, pOperator->resultInfo.capacity), lino, _return);
941,870✔
870

871
  size_t  numOfCols = taosArrayGetSize(pInfo->binfo.pRes->pDataBlock);
941,870✔
872
  int32_t rowSize = pInfo->binfo.pRes->info.rowSize;
941,870✔
873

874
  if (!pVirtualScanPhyNode->scan.node.dynamicOp) {
941,870✔
875
    VTS_ERR_JRET(makeTSMergeKey(&pMergeKeys, 0));
896,702✔
876
    pVirtualScanInfo->pSortInfo = createSortInfo(pMergeKeys);
896,702✔
877
    TSDB_CHECK_NULL(pVirtualScanInfo->pSortInfo, code, lino, _return, terrno)
896,702✔
878
  } else {
879
    pTaskInfo->dynamicTask = true;
45,168✔
880
  }
881
  pVirtualScanInfo->bufPageSize = getProperSortPageSize(rowSize, numOfCols);
941,870✔
882
  pVirtualScanInfo->sortBufSize =
941,870✔
883
      pVirtualScanInfo->bufPageSize * (numOfDownstream + 1);  // one additional is reserved for merged result.
941,870✔
884
  VTS_ERR_JRET(
941,870✔
885
      extractColMap(pVirtualScanPhyNode->pTargets, &pVirtualScanInfo->dataSlotMap, &pVirtualScanInfo->tsSlotId,
886
                    &pVirtualScanInfo->orgTsSlotId, &pVirtualScanInfo->tagBlockId, &pVirtualScanInfo->useOrgTsCol));
887

888
  pVirtualScanInfo->scanAllCols = pVirtualScanPhyNode->scanAllCols;
941,870✔
889

890
  VTS_ERR_JRET(filterInitFromNode((SNode*)pVirtualScanPhyNode->scan.node.pConditions, &pOperator->exprSupp.pFilterInfo,
941,870✔
891
                                  0, pTaskInfo->pStreamRuntimeInfo));
892

893
  pVirtualScanInfo->base.metaCache.pTableMetaEntryCache = taosLRUCacheInit(1024 * 128, -1, .5);
941,870✔
894
  QUERY_CHECK_NULL(pVirtualScanInfo->base.metaCache.pTableMetaEntryCache, code, lino, _return, terrno)
941,870✔
895

896
  setOperatorInfo(pOperator, "VirtualTableScanOperator", QUERY_NODE_PHYSICAL_PLAN_VIRTUAL_TABLE_SCAN, false,
941,870✔
897
                  OP_NOT_OPENED, pInfo, pTaskInfo);
898
  pOperator->fpSet =
899
      createOperatorFpSet(openVirtualTableScanOperator, virtualTableGetNext, NULL, destroyVirtualTableScanOperatorInfo,
941,870✔
900
                          optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
901
  setOperatorResetStateFn(pOperator, resetVirtualTableMergeOperState);
941,870✔
902

903
  if (NULL != pDownstream) {
941,870✔
904
    VTS_ERR_JRET(appendDownstream(pOperator, pDownstream, numOfDownstream));
935,957✔
905
  } else {
906
    pVirtualScanInfo->tagDownStreamId = -1;
5,913✔
907
  }
908

909
  nodesDestroyList(pMergeKeys);
941,870✔
910
  *pOptrInfo = pOperator;
941,870✔
911
  return TSDB_CODE_SUCCESS;
941,870✔
912

913
_return:
×
914
  if (code != TSDB_CODE_SUCCESS) {
×
915
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
916
  }
917
  if (pInfo != NULL) {
×
918
    destroyVirtualTableScanOperatorInfo(pInfo);
×
919
  }
920
  nodesDestroyList(pMergeKeys);
×
921
  pTaskInfo->code = code;
×
922
  destroyOperatorAndDownstreams(pOperator, pDownstream, numOfDownstream);
×
923
  return code;
×
924
}
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