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

taosdata / TDengine / #4908

30 Dec 2025 10:52AM UTC coverage: 65.386% (-0.2%) from 65.541%
#4908

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

1330 existing lines in 113 files now uncovered.

193461 of 295877 relevant lines covered (65.39%)

115765274.47 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) {
6,762,426✔
63
  SOperatorInfo* pOperator = (SOperatorInfo*)param;
6,762,426✔
64
  int32_t        code = TSDB_CODE_SUCCESS;
6,762,426✔
65
  int32_t        line = 0;
6,762,426✔
66

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

76
  return code;
6,762,426✔
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) {
171,670✔
83
  int32_t code = TSDB_CODE_SUCCESS;
171,670✔
84
  int32_t lino = 0;
171,670✔
85
  int32_t tsIndex = -1;
171,670✔
86
  for (int32_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); i++) {
200,865✔
87
    if (((SColumnInfoData*)taosArrayGet(pBlock->pDataBlock, i))->info.colId == tsSlotId) {
200,865✔
88
      tsIndex = i;
171,670✔
89
      break;
171,670✔
90
    }
91
  }
92

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

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

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

105
  return code;
171,670✔
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) {
327,412✔
112
  SLoadNextCtx*           pCtx = (SLoadNextCtx*)param;
327,412✔
113
  SOperatorInfo*          pOperator = pCtx->pOperator;
327,412✔
114
  SOperatorParam*         pOperatorGetParam = pCtx->pOperatorGetParam;
327,412✔
115
  int32_t                 code = TSDB_CODE_SUCCESS;
327,412✔
116
  SSDataBlock*            pRes = NULL;
327,412✔
117
  SExchangeOperatorParam* pParam = (SExchangeOperatorParam*)pOperatorGetParam->value;
327,412✔
118

119
  pParam->basic.window = pCtx->window;
327,412✔
120
  pOperator->status = OP_NOT_OPENED;
327,412✔
121
  if (pCtx->pIntermediateBlock) {
327,412✔
122
    blockDataDestroy(pCtx->pIntermediateBlock);
171,670✔
123
    pCtx->pIntermediateBlock = NULL;
171,670✔
124
  }
125

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

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

141
  return code;
326,802✔
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) {
893,001✔
148
  int32_t           code = TSDB_CODE_SUCCESS;
893,001✔
149
  SNodeList        *pNodeList = NULL;
893,001✔
150
  SColumnNode      *pColumnNode = NULL;
893,001✔
151
  SOrderByExprNode *pOrderByExprNode = NULL;
893,001✔
152

153
  VTS_ERR_JRET(nodesMakeList(&pNodeList));
893,001✔
154

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

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

163
  VTS_ERR_JRET(nodesListAppend(pNodeList, (SNode*)pOrderByExprNode));
893,001✔
164

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

174
void cleanUpVirtualScanInfo(SVirtualTableScanInfo* pVirtualScanInfo) {
122,425✔
175
  if (pVirtualScanInfo->pSortInfo) {
122,425✔
176
    taosArrayDestroy(pVirtualScanInfo->pSortInfo);
81,089✔
177
    pVirtualScanInfo->pSortInfo = NULL;
81,089✔
178
  }
179
  if (pVirtualScanInfo->pSortHandle) {
122,425✔
180
    tsortDestroySortHandle(pVirtualScanInfo->pSortHandle);
16,553✔
181
    pVirtualScanInfo->pSortHandle = NULL;
16,553✔
182
  }
183
  if (pVirtualScanInfo->pSortCtxList) {
122,425✔
184
    for (int32_t i = 0; i < taosArrayGetSize(pVirtualScanInfo->pSortCtxList); i++) {
45,568✔
185
      SLoadNextCtx* pCtx = *(SLoadNextCtx**)taosArrayGet(pVirtualScanInfo->pSortCtxList, i);
29,015✔
186
      blockDataDestroy(pCtx->pIntermediateBlock);
29,015✔
187
      taosMemoryFree(pCtx);
29,015✔
188
    }
189
    taosArrayDestroy(pVirtualScanInfo->pSortCtxList);
16,553✔
190
  }
191
}
122,425✔
192

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

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

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

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

218
  if (pOperator->numOfDownstream > 2) {
122,425✔
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;
122,425✔
224
  for (int32_t i = 0; i < pOperator->numOfDownstream; ++i) {
270,112✔
225
    SOperatorInfo* pDownstream = pOperator->pDownstream[i];
147,687✔
226
    if (pDownstream->resultDataBlockId == pVirtualScanInfo->tagBlockId) {
147,687✔
227
      // tag block do not need sort
228
      pVirtualScanInfo->tagDownStreamId = i;
25,262✔
229
      pInfo->pSavedTagBlock = NULL;
25,262✔
230
      continue;
25,262✔
231
    }
232
  }
233
  scanOpIndex = pVirtualScanInfo->tagDownStreamId == -1 ? 0 : 1 - pVirtualScanInfo->tagDownStreamId;
122,425✔
234

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

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

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

255
    ps->param = pCtx;
163,062✔
256
    ps->onlyRef = true;
163,062✔
257

258
    VTS_ERR_JRET(tsortAddSource(pVirtualScanInfo->pSortHandle, ps));
163,062✔
259
    QUERY_CHECK_NULL(taosArrayPush(pVirtualScanInfo->pSortCtxList, &pCtx), code, lino, _return, terrno)
326,124✔
260
  }
261

262
  VTS_ERR_JRET(tsortOpen(pVirtualScanInfo->pSortHandle));
122,425✔
263

264
  return code;
121,815✔
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) {
765,374✔
277
  SVirtualScanMergeOperatorInfo * pInfo = pOperator->info;
765,374✔
278
  SExecTaskInfo*                  pTaskInfo = pOperator->pTaskInfo;
765,374✔
279
  SVirtualTableScanInfo*          pVirtualScanInfo = &pInfo->virtualScanInfo;
765,374✔
280
  int32_t                         numOfBufPage = (int32_t)pVirtualScanInfo->sortBufSize / pVirtualScanInfo->bufPageSize;
765,374✔
281
  SSortSource*                    ps = NULL;
765,374✔
282
  int32_t                         code = 0;
765,374✔
283
  int32_t                         lino = 0;
765,374✔
284

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

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

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

299
    if (pDownstream->resultDataBlockId == pVirtualScanInfo->tagBlockId) {
1,730,163✔
300
      // tag block do not need sort
301
      pVirtualScanInfo->tagDownStreamId = i;
1,818✔
302
      continue;
1,818✔
303
    }
304

305
    ps = taosMemoryCalloc(1, sizeof(SSortSource));
1,728,345✔
306
    TSDB_CHECK_NULL(ps, code, lino, _return, terrno)
1,728,345✔
307

308
    ps->param = pDownstream;
1,728,345✔
309
    ps->onlyRef = true;
1,728,345✔
310

311
    VTS_ERR_JRET(tsortAddSource(pVirtualScanInfo->pSortHandle, ps));
1,728,345✔
312
    ps = NULL;
1,728,345✔
313
  }
314

315
  VTS_ERR_JRET(tsortOpen(pVirtualScanInfo->pSortHandle));
765,374✔
316

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

327
int32_t openVirtualTableScanOperatorImpl(SOperatorInfo* pOperator) {
893,001✔
328
  int32_t                         code = 0;
893,001✔
329
  int32_t                         lino = 0;
893,001✔
330

331
  if (pOperator->numOfDownstream == 0) {
893,001✔
332
    return code;
5,202✔
333
  }
334

335
  if (pOperator->pOperatorGetParam) {
887,799✔
336
    VTS_ERR_JRET(createSortHandleFromParam(pOperator));
122,425✔
337
  } else {
338
    VTS_ERR_JRET(createSortHandle(pOperator));
765,374✔
339
  }
340

341
  return code;
887,189✔
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) {
4,515,557✔
349
  int32_t code = 0;
4,515,557✔
350

351
  if (OPTR_IS_OPENED(pOperator)) {
4,515,557✔
352
    return TSDB_CODE_SUCCESS;
3,622,556✔
353
  }
354

355
  int64_t startTs = taosGetTimestampUs();
893,001✔
356

357
  code = openVirtualTableScanOperatorImpl(pOperator);
893,001✔
358

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

362
  VTS_ERR_RET(code);
892,391✔
363

364
  OPTR_SET_OPENED(pOperator);
892,391✔
365
  return code;
892,391✔
366
}
367

368
static int32_t doGetVtableMergedBlockData(SVirtualScanMergeOperatorInfo* pInfo, SSortHandle* pHandle, int32_t capacity,
4,472,770✔
369
                                          SSDataBlock* p) {
370
  int32_t code = 0;
4,472,770✔
371
  int64_t lastTs = 0;
4,472,770✔
372
  int64_t rowNums = -1;
4,472,770✔
373
  blockDataEmpty(p);
4,472,770✔
374
  for (int32_t j = 0; j < taosArrayGetSize(p->pDataBlock); j++) {
43,633,506✔
375
    colDataSetNItemsNull(taosArrayGet(p->pDataBlock, j), 0, capacity);
39,160,736✔
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;
2,952,168✔
386
      pInfo->pSavedTuple = NULL;
2,952,168✔
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;
2,952,168✔
399
              goto _return;
2,952,168✔
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;
1,618,384✔
409
            void*   slotId = tSimpleHashGet(pInfo->virtualScanInfo.dataSlotMap, &slotKey, sizeof(slotKey));
52,234✔
410
            if (slotId) {
52,234✔
411
              VTS_ERR_RET(colDataSetVal(taosArrayGet(p->pDataBlock, *(int32_t *)slotId), rowNums, pData, false));
46,494✔
412
            }
413
          }
414
          continue;
2,147,483,647✔
415
        }
416
        if (tsortIsNullVal(pTupleHandle, i)) {
2,147,483,647✔
417
          continue;
225,776✔
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:
4,472,770✔
430
  p->info.rows = rowNums + 1;
4,472,770✔
431
  p->info.dataLoad = 1;
4,472,770✔
432
  p->info.scanFlag = MAIN_SCAN;
4,472,770✔
433
  return code;
4,472,770✔
434
}
435

436
static int32_t doGetVStableMergedBlockData(SVirtualScanMergeOperatorInfo* pInfo, SSortHandle* pHandle, int32_t capacity,
242,127✔
437
                                           SSDataBlock* p) {
438
  int32_t code = 0;
242,127✔
439
  int64_t lastTs = 0;
242,127✔
440
  int64_t rowNums = -1;
242,127✔
441
  blockDataEmpty(p);
242,127✔
442
  for (int32_t j = 0; j < taosArrayGetSize(p->pDataBlock); j++) {
2,529,946✔
443
    colDataSetNItemsNull(taosArrayGet(p->pDataBlock, j), 0, capacity);
2,287,819✔
444
  }
445
  while (1) {
331,127,555✔
446
    STupleHandle* pTupleHandle = NULL;
331,369,682✔
447
    if (!pInfo->pSavedTuple) {
331,369,682✔
448
      code = tsortNextTuple(pHandle, &pTupleHandle);
331,317,662✔
449
      if (pTupleHandle == NULL || (code != 0)) {
331,317,662✔
450
        break;
451
      }
452
    } else {
453
      pTupleHandle = pInfo->pSavedTuple;
52,020✔
454
      pInfo->pSavedTuple = NULL;
52,020✔
455
    }
456

457
    int32_t tsIndex = -1;
331,179,575✔
458
    int32_t colNum = tsortGetColNum(pTupleHandle);
331,179,575✔
459

460
    for (int32_t i = 0; i < colNum; i++) {
331,551,000✔
461
      SColumnInfoData *pColInfo = NULL;
331,551,000✔
462
      tsortGetColumnInfo(pTupleHandle, i, &pColInfo);
331,551,000✔
463
      if (pColInfo && pColInfo->info.slotId ==  pInfo->virtualScanInfo.tsSlotId) {
331,551,000✔
464
        tsIndex = i;
331,179,575✔
465
        break;
331,179,575✔
466
      }
467
    }
468

469
    if (tsIndex == -1) {
331,179,575✔
470
      tsIndex = colNum - 1;
×
471
    }
472

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

478
    if (pData != NULL) {
331,179,575✔
479
      if (lastTs != *(int64_t*)pData) {
331,179,575✔
480
        if (rowNums >= capacity - 1) {
244,387,248✔
481
          pInfo->pSavedTuple = pTupleHandle;
52,020✔
482
          goto _return;
52,020✔
483
        }
484
        rowNums++;
244,335,228✔
485
        if (pInfo->virtualScanInfo.tsSlotId != -1) {
244,335,228✔
486
          VTS_ERR_RET(colDataSetVal(taosArrayGet(p->pDataBlock, pInfo->virtualScanInfo.tsSlotId), rowNums, pData, false));
244,335,228✔
487
        }
488
        lastTs = *(int64_t*)pData;
244,335,228✔
489
      }
490
    }
491
    if (pInfo->virtualScanInfo.scanAllCols) {
331,127,555✔
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,338,266,739✔
501

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

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

523
  qDebug("start to merge final sorted rows, %s", GET_TASKID(pTaskInfo));
4,720,099✔
524
  blockDataCleanup(pDataBlock);
4,720,099✔
525

526
  if (pHandle == NULL) {
4,720,099✔
527
    return TSDB_CODE_SUCCESS;
5,202✔
528
  }
529

530
  if (pVirtualScanInfo->pIntermediateBlock == NULL) {
4,714,897✔
531
    VTS_ERR_RET(tsortGetSortedDataBlock(pHandle, &pVirtualScanInfo->pIntermediateBlock));
870,636✔
532
    if (pVirtualScanInfo->pIntermediateBlock == NULL) {
870,636✔
533
      return TSDB_CODE_SUCCESS;
×
534
    }
535

536
    VTS_ERR_RET(blockDataEnsureCapacity(pVirtualScanInfo->pIntermediateBlock, capacity));
870,636✔
537
  } else {
538
    blockDataCleanup(pVirtualScanInfo->pIntermediateBlock);
3,844,261✔
539
  }
540

541
  SSDataBlock* p = pVirtualScanInfo->pIntermediateBlock;
4,714,897✔
542
  if (pOperator->pOperatorGetParam) {
4,714,897✔
543
    VTS_ERR_RET(doGetVStableMergedBlockData(pInfo, pHandle, capacity, p));
242,127✔
544
  } else {
545
    VTS_ERR_RET(doGetVtableMergedBlockData(pInfo, pHandle, capacity, p));
4,472,770✔
546
  }
547

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

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

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

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

569
  if (tagBlock->info.rows != 1) {
73,204✔
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;
73,204✔
575
  pBlock->info.rows = rows;
73,204✔
576
  for (int32_t j = 0; j < numOfExpr; ++j) {
464,268✔
577
    const SExprInfo* pExpr1 = &pExpr[j];
391,064✔
578
    int32_t          dstSlotId = pExpr1->base.resSchema.slotId;
391,064✔
579

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

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

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

592
    char* data = colDataGetData(pTagInfoData, 0);
391,064✔
593

594
    if (pColInfoData->info.type != TSDB_DATA_TYPE_JSON) {
391,064✔
595
      code = colDataSetNItems(pColInfoData, 0, data, pBlock->info.rows, 1, false);
391,064✔
596
      QUERY_CHECK_CODE(code, lino, _return);
391,064✔
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;
73,204✔
607

608
_return:
73,204✔
609

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

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

624
  return code;
3,827,708✔
625
}
626

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

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

639
  VTS_ERR_JRET(pOperator->fpSet._openFn(pOperator));
4,515,557✔
640

641
  if (pVirtualScanInfo->tagBlockId != -1 && pVirtualScanInfo->tagDownStreamId != -1 && !pInfo->pSavedTagBlock) {
4,514,947✔
642
    SSDataBlock*   pTagBlock = NULL;
26,470✔
643
    SOperatorInfo *pTagScanOp = pOperator->pDownstream[pVirtualScanInfo->tagDownStreamId];
26,470✔
644
    if (pOperator->pOperatorGetParam) {
26,470✔
645
      SOperatorParam* pTagOp = ((SVTableScanOperatorParam*)pOperator->pOperatorGetParam->value)->pTagScanOp;
24,652✔
646
      VTS_ERR_JRET(pTagScanOp->fpSet.getNextExtFn(pTagScanOp, pTagOp, &pTagBlock));
24,652✔
647
    } else {
648
      VTS_ERR_JRET(pTagScanOp->fpSet.getNextFn(pTagScanOp, &pTagBlock));
1,818✔
649
    }
650

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

657
  while(1) {
658
    VTS_ERR_JRET(doVirtualTableMerge(pOperator, pResBlock));
4,720,099✔
659
    if (*pResBlock == NULL) {
4,720,099✔
660
      setOperatorCompleted(pOperator);
892,391✔
661
      break;
892,391✔
662
    }
663

664
    if (pOperator->pOperatorGetParam) {
3,827,708✔
665
      uint64_t uid = ((SVTableScanOperatorParam*)pOperator->pOperatorGetParam->value)->uid;
120,312✔
666
      (*pResBlock)->info.id.uid = uid;
120,312✔
667

668
      qTrace("vtable scan uid:%" PRIu64, (*pResBlock)->info.id.uid);
120,312✔
669
    } else {
670
      (*pResBlock)->info.id.uid = pInfo->virtualScanInfo.vtableUid;
3,707,396✔
671

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

675
    VTS_ERR_JRET(doSetTagColumnData(pVirtualScanInfo, pInfo->pSavedTagBlock, (*pResBlock), (*pResBlock)->info.rows));
3,827,708✔
676
    VTS_ERR_JRET(doFilter(*pResBlock, pOperator->exprSupp.pFilterInfo, NULL, NULL));
3,827,708✔
677
    if ((*pResBlock)->info.rows > 0) {
3,827,708✔
678
      break;
3,622,556✔
679
    }
680
  }
681

682
  return code;
4,514,947✔
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) {
811,912✔
690
  cleanupQueryTableDataCond(&pBase->cond);
811,912✔
691

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

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

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

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

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

719
  blockDataDestroy(pInfo->pIntermediateBlock);
811,912✔
720
  pInfo->pIntermediateBlock = NULL;
811,912✔
721

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

726
  tSimpleHashCleanup(pInfo->dataSlotMap);
811,912✔
727

728
  if (pInfo->pSortCtxList) {
811,912✔
729
    for (int32_t i = 0; i < taosArrayGetSize(pInfo->pSortCtxList); i++) {
40,012✔
730
      SLoadNextCtx* pCtx = *(SLoadNextCtx**)taosArrayGet(pInfo->pSortCtxList, i);
31,434✔
731
      blockDataDestroy(pCtx->pIntermediateBlock);
31,434✔
732
      taosMemoryFree(pCtx);
31,434✔
733
    }
734
    taosArrayDestroy(pInfo->pSortCtxList);
8,578✔
735
    pInfo->pSortCtxList = NULL;
8,578✔
736
  }
737
  taosMemoryFreeClear(param);
811,912✔
738
}
739

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

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

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

756
  for (int32_t i = 0; i < numOfCols; ++i) {
17,734,172✔
757
    SColumnNode* pColNode = (SColumnNode*)nodesListGetNode(pNodeList, i);
16,922,260✔
758
    TSDB_CHECK_NULL(pColNode, code, lino, _return, terrno)
16,922,260✔
759

760
    if (pColNode->isPrimTs || pColNode->colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
16,922,260✔
761
      *tsSlotId = i;
810,700✔
762
      *orgTsSlotId = i;
810,700✔
763
    } else if (pColNode->hasRef) {
16,111,560✔
764
      int32_t slotKey = pColNode->dataBlockId << 16 | pColNode->slotId;
15,813,145✔
765
      if (pColNode->slotId == 0) {
15,813,145✔
766
        *useOriginTs = true;
14,174✔
767
      }
768
      VTS_ERR_JRET(tSimpleHashPut(*pSlotMap, &slotKey, sizeof(slotKey), &i, sizeof(i)));
15,813,145✔
769
    } else if (pColNode->colType == COLUMN_TYPE_TAG || '\0' == pColNode->tableAlias[0]) {
298,415✔
770
      // tag column or pseudo column's function
771
      *tagBlockId = pColNode->dataBlockId;
34,961✔
772
    }
773
  }
774

775
  return code;
811,912✔
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) {
1,371,382✔
786
  int32_t code = 0, lino = 0;
1,371,382✔
787
  SVirtualScanMergeOperatorInfo* pMergeInfo = pOper->info;
1,371,382✔
788
  SVirtualScanPhysiNode* pPhynode = (SVirtualScanPhysiNode*)pOper->pPhyNode;
1,371,579✔
789
  SVirtualTableScanInfo* pInfo = &pMergeInfo->virtualScanInfo;
1,371,382✔
790
  
791
  pOper->status = OP_NOT_OPENED;
1,371,382✔
792
  resetBasicOperatorState(&pMergeInfo->binfo);
1,371,382✔
793

794
  tsortDestroySortHandle(pInfo->pSortHandle);
1,371,579✔
795
  pInfo->pSortHandle = NULL;
1,371,579✔
796
  // taosArrayDestroy(pInfo->pSortInfo);
797
  // pInfo->pSortInfo = NULL;
798

799
  blockDataDestroy(pInfo->pIntermediateBlock);
1,371,579✔
800
  pInfo->pIntermediateBlock = NULL;
1,371,382✔
801

802
  blockDataDestroy(pInfo->pInputBlock);
1,371,382✔
803
  pInfo->pInputBlock = createDataBlockFromDescNode(((SPhysiNode*)pPhynode)->pOutputDataBlockDesc);
1,371,382✔
804
  TSDB_CHECK_NULL(pInfo->pInputBlock, code, lino, _exit, terrno)
1,371,579✔
805

806
  pInfo->tagDownStreamId = -1;
1,371,579✔
807

808
  if (pInfo->pSortCtxList) {
1,371,579✔
809
    for (int32_t i = 0; i < taosArrayGetSize(pInfo->pSortCtxList); i++) {
199,907✔
810
      SLoadNextCtx* pCtx = *(SLoadNextCtx**)taosArrayGet(pInfo->pSortCtxList, i);
102,613✔
811
      blockDataDestroy(pCtx->pIntermediateBlock);
102,613✔
812
      taosMemoryFree(pCtx);
102,613✔
813
    }
814
    taosArrayDestroy(pInfo->pSortCtxList);
97,294✔
815
    pInfo->pSortCtxList = NULL;
97,294✔
816
  }
817

818
  pMergeInfo->pSavedTuple = NULL;
1,370,988✔
819
  pMergeInfo->pSavedTagBlock = NULL;
1,370,988✔
820

821
_exit:
1,370,791✔
822

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

827
  return code;
1,370,594✔
828
}
829

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

841
  QUERY_CHECK_NULL(pInfo, code, lino, _return, terrno)
811,912✔
842
  QUERY_CHECK_NULL(pOperator, code, lino, _return, terrno)
811,912✔
843

844
  pOperator->pPhyNode = pVirtualScanPhyNode;
811,912✔
845

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

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

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

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

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

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

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

888
  pVirtualScanInfo->scanAllCols = pVirtualScanPhyNode->scanAllCols;
811,912✔
889

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

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

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

903
  if (NULL != pDownstream) {
811,912✔
904
    VTS_ERR_JRET(appendDownstream(pOperator, pDownstream, numOfDownstream));
806,710✔
905
  } else {
906
    pVirtualScanInfo->tagDownStreamId = -1;
5,202✔
907
  }
908

909
  nodesDestroyList(pMergeKeys);
811,912✔
910
  *pOptrInfo = pOperator;
811,912✔
911
  return TSDB_CODE_SUCCESS;
811,912✔
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