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

taosdata / TDengine / #3608

12 Feb 2025 05:57AM UTC coverage: 63.066% (+1.4%) from 61.715%
#3608

push

travis-ci

web-flow
Merge pull request #29746 from taosdata/merge/mainto3.02

merge: from main to 3.0 branch

140199 of 286257 branches covered (48.98%)

Branch coverage included in aggregate %.

89 of 161 new or added lines in 18 files covered. (55.28%)

3211 existing lines in 190 files now uncovered.

218998 of 283298 relevant lines covered (77.3%)

5949310.66 hits per line

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

69.4
/source/libs/executor/src/executil.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 "function.h"
17
#include "functionMgt.h"
18
#include "index.h"
19
#include "os.h"
20
#include "query.h"
21
#include "tdatablock.h"
22
#include "thash.h"
23
#include "tmsg.h"
24
#include "ttime.h"
25

26
#include "executil.h"
27
#include "executorInt.h"
28
#include "querytask.h"
29
#include "storageapi.h"
30
#include "tcompression.h"
31

32
typedef struct tagFilterAssist {
33
  SHashObj* colHash;
34
  int32_t   index;
35
  SArray*   cInfoList;
36
  int32_t   code;
37
} tagFilterAssist;
38

39
typedef struct STransTagExprCtx {
40
  int32_t      code;
41
  SMetaReader* pReader;
42
} STransTagExprCtx;
43

44
typedef enum {
45
  FILTER_NO_LOGIC = 1,
46
  FILTER_AND,
47
  FILTER_OTHER,
48
} FilterCondType;
49

50
static FilterCondType checkTagCond(SNode* cond);
51
static int32_t optimizeTbnameInCond(void* metaHandle, int64_t suid, SArray* list, SNode* pTagCond, SStorageAPI* pAPI);
52
static int32_t optimizeTbnameInCondImpl(void* metaHandle, SArray* list, SNode* pTagCond, SStorageAPI* pStoreAPI, uint64_t suid);
53

54
static int32_t getTableList(void* pVnode, SScanPhysiNode* pScanNode, SNode* pTagCond, SNode* pTagIndexCond,
55
                            STableListInfo* pListInfo, uint8_t* digest, const char* idstr, SStorageAPI* pStorageAPI);
56

57
static int64_t getLimit(const SNode* pLimit) { return (NULL == pLimit || NULL == ((SLimitNode*)pLimit)->limit) ? -1 : ((SLimitNode*)pLimit)->limit->datum.i; }
5,477,128!
58
static int64_t getOffset(const SNode* pLimit) { return (NULL == pLimit || NULL == ((SLimitNode*)pLimit)->offset) ? -1 : ((SLimitNode*)pLimit)->offset->datum.i; }
5,478,103✔
59
static void    releaseColInfoData(void* pCol);
60

61
void initResultRowInfo(SResultRowInfo* pResultRowInfo) {
2,133,031✔
62
  pResultRowInfo->size = 0;
2,133,031✔
63
  pResultRowInfo->cur.pageId = -1;
2,133,031✔
64
}
2,133,031✔
65

66
void closeResultRow(SResultRow* pResultRow) { pResultRow->closed = true; }
12,550✔
67

68
void resetResultRow(SResultRow* pResultRow, size_t entrySize) {
12,193,542✔
69
  pResultRow->numOfRows = 0;
12,193,542✔
70
  pResultRow->closed = false;
12,193,542✔
71
  pResultRow->endInterp = false;
12,193,542✔
72
  pResultRow->startInterp = false;
12,193,542✔
73

74
  if (entrySize > 0) {
12,193,542!
75
    memset(pResultRow->pEntryInfo, 0, entrySize);
12,196,077✔
76
  }
77
}
12,193,542✔
78

79
// TODO refactor: use macro
80
SResultRowEntryInfo* getResultEntryInfo(const SResultRow* pRow, int32_t index, const int32_t* offset) {
1,478,765,902✔
81
  return (SResultRowEntryInfo*)((char*)pRow->pEntryInfo + offset[index]);
1,478,765,902✔
82
}
83

84
size_t getResultRowSize(SqlFunctionCtx* pCtx, int32_t numOfOutput) {
1,877,221✔
85
  int32_t rowSize = (numOfOutput * sizeof(SResultRowEntryInfo)) + sizeof(SResultRow);
1,877,221✔
86

87
  for (int32_t i = 0; i < numOfOutput; ++i) {
10,843,727✔
88
    rowSize += pCtx[i].resDataInfo.interBufSize;
8,966,506✔
89
  }
90

91
  return rowSize;
1,877,221✔
92
}
93

94
// Convert buf read from rocksdb to result row
95
int32_t getResultRowFromBuf(SExprSupp *pSup, const char* inBuf, size_t inBufSize, char **outBuf, size_t *outBufSize) {
1,385✔
96
  if (inBuf == NULL || pSup == NULL) {
1,385!
97
    qError("invalid input parameters, inBuf:%p, pSup:%p", inBuf, pSup);
12!
98
    return TSDB_CODE_INVALID_PARA;
12✔
99
  }
100
  SqlFunctionCtx *pCtx = pSup->pCtx;
1,373✔
101
  int32_t        *offset = pSup->rowEntryInfoOffset;
1,373✔
102
  SResultRow     *pResultRow  = NULL;
1,373✔
103
  size_t          processedSize = 0;
1,373✔
104
  int32_t         code = TSDB_CODE_SUCCESS;
1,373✔
105

106
  // calculate the size of output buffer
107
  *outBufSize = getResultRowSize(pCtx, pSup->numOfExprs);
1,373✔
108
  *outBuf = taosMemoryMalloc(*outBufSize);
1,373!
109
  if (*outBuf == NULL) {
1,373!
110
    qError("failed to allocate memory for output buffer, size:%zu", *outBufSize);
×
111
    return terrno;
×
112
  }
113
  pResultRow = (SResultRow*)*outBuf;
1,373✔
114
  (void)memcpy(pResultRow, inBuf, sizeof(SResultRow));
1,373✔
115
  inBuf += sizeof(SResultRow);
1,373✔
116
  processedSize += sizeof(SResultRow);
1,373✔
117

118
  for (int32_t i = 0; i < pSup->numOfExprs; ++i) {
14,312✔
119
    int32_t len = *(int32_t*)inBuf;
12,939✔
120
    inBuf += sizeof(int32_t);
12,939✔
121
    processedSize += sizeof(int32_t);
12,939✔
122
    if (pResultRow->version != FUNCTION_RESULT_INFO_VERSION && pCtx->fpSet.decode) {
12,939!
123
      code = pCtx->fpSet.decode(&pCtx[i], inBuf, getResultEntryInfo(pResultRow, i, offset), pResultRow->version);
×
124
      if (code != TSDB_CODE_SUCCESS) {
×
125
        qError("failed to decode result row, code:%d", code);
×
126
        return code;
×
127
      }
128
    } else {
129
      (void)memcpy(getResultEntryInfo(pResultRow, i, offset), inBuf, len);
12,939✔
130
    }
131
    inBuf += len;
12,939✔
132
    processedSize += len;
12,939✔
133
  }
134

135
  if (processedSize < inBufSize) {
1,373✔
136
    // stream stores extra data after result row
137
    size_t leftLen = inBufSize - processedSize;
604✔
138
    TAOS_MEMORY_REALLOC(*outBuf, *outBufSize + leftLen);
604!
139
    if (*outBuf == NULL) {
604!
140
      qError("failed to reallocate memory for output buffer, size:%zu", *outBufSize + leftLen);
×
141
      return terrno;
×
142
    }
143
    (void)memcpy(*outBuf + *outBufSize, inBuf, leftLen);
604✔
144
    inBuf += leftLen;
604✔
145
    processedSize += leftLen;
604✔
146
    *outBufSize += leftLen;
604✔
147
  }
148
  return TSDB_CODE_SUCCESS;
1,373✔
149
}
150

151
// Convert result row to buf for rocksdb
152
int32_t putResultRowToBuf(SExprSupp *pSup, const char* inBuf, size_t inBufSize, char **outBuf, size_t *outBufSize) {
603,626✔
153
  if (pSup == NULL || inBuf == NULL || outBuf == NULL || outBufSize == NULL) {
603,626!
UNCOV
154
    qError("invalid input parameters, inBuf:%p, pSup:%p, outBufSize:%p, outBuf:%p", inBuf, pSup, outBufSize, outBuf);
×
155
    return TSDB_CODE_INVALID_PARA;
×
156
  }
157

158
  SqlFunctionCtx *pCtx = pSup->pCtx;
603,634✔
159
  int32_t        *offset = pSup->rowEntryInfoOffset;
603,634✔
160
  SResultRow     *pResultRow = (SResultRow*)inBuf;
603,634✔
161
  size_t          rowSize = getResultRowSize(pCtx, pSup->numOfExprs);
603,634✔
162

163
  if (rowSize > inBufSize) {
603,120!
164
    qError("invalid input buffer size, rowSize:%zu, inBufSize:%zu", rowSize, inBufSize);
×
165
    return TSDB_CODE_INVALID_PARA;
×
166
  }
167

168
  // calculate the size of output buffer
169
  *outBufSize = rowSize + sizeof(int32_t) * pSup->numOfExprs;
603,120✔
170
  if (rowSize < inBufSize) {
603,120✔
171
    *outBufSize += inBufSize - rowSize;
530✔
172
  }
173

174
  *outBuf = taosMemoryMalloc(*outBufSize);
603,120!
175
  if (*outBuf == NULL) {
602,831!
176
    qError("failed to allocate memory for output buffer, size:%zu", *outBufSize);
×
177
    return terrno;
×
178
  }
179

180
  char *pBuf = *outBuf;
602,831✔
181
  pResultRow->version = FUNCTION_RESULT_INFO_VERSION;
602,831✔
182
  (void)memcpy(pBuf, pResultRow, sizeof(SResultRow));
602,831✔
183
  pBuf += sizeof(SResultRow);
602,831✔
184
  for (int32_t i = 0; i < pSup->numOfExprs; ++i) {
6,250,650✔
185
    size_t len = sizeof(SResultRowEntryInfo) + pCtx[i].resDataInfo.interBufSize;
5,650,906✔
186
    *(int32_t *) pBuf = (int32_t)len;
5,650,906✔
187
    pBuf += sizeof(int32_t);
5,650,906✔
188
    (void)memcpy(pBuf, getResultEntryInfo(pResultRow, i, offset), len);
5,650,906✔
189
    pBuf += len;
5,647,819✔
190
  }
191

192
  if (rowSize < inBufSize) {
599,744✔
193
    // stream stores extra data after result row
194
    size_t leftLen = inBufSize - rowSize;
530✔
195
    (void)memcpy(pBuf, inBuf + rowSize, leftLen);
530✔
196
    pBuf += leftLen;
530✔
197
  }
198
  return TSDB_CODE_SUCCESS;
599,744✔
199
}
200

201
static void freeEx(void* p) { taosMemoryFree(*(void**)p); }
×
202

203
void cleanupGroupResInfo(SGroupResInfo* pGroupResInfo) {
714,763✔
204
  taosMemoryFreeClear(pGroupResInfo->pBuf);
714,763!
205
  if (pGroupResInfo->freeItem) {
714,794!
206
    //    taosArrayDestroy(pGroupResInfo->pRows);
207
    taosArrayDestroyEx(pGroupResInfo->pRows, freeEx);
×
208
    pGroupResInfo->freeItem = false;
×
209
    pGroupResInfo->pRows = NULL;
×
210
  } else {
211
    taosArrayDestroy(pGroupResInfo->pRows);
714,794✔
212
    pGroupResInfo->pRows = NULL;
714,830✔
213
  }
214
  pGroupResInfo->index = 0;
714,830✔
215
}
714,830✔
216

217
int32_t resultrowComparAsc(const void* p1, const void* p2) {
829,401,778✔
218
  SResKeyPos* pp1 = *(SResKeyPos**)p1;
829,401,778✔
219
  SResKeyPos* pp2 = *(SResKeyPos**)p2;
829,401,778✔
220

221
  if (pp1->groupId == pp2->groupId) {
829,401,778✔
222
    int64_t pts1 = *(int64_t*)pp1->key;
641,607,040✔
223
    int64_t pts2 = *(int64_t*)pp2->key;
641,607,040✔
224

225
    if (pts1 == pts2) {
641,607,040!
226
      return 0;
×
227
    } else {
228
      return pts1 < pts2 ? -1 : 1;
641,607,040✔
229
    }
230
  } else {
231
    return pp1->groupId < pp2->groupId ? -1 : 1;
187,794,738✔
232
  }
233
}
234

235
static int32_t resultrowComparDesc(const void* p1, const void* p2) { return resultrowComparAsc(p2, p1); }
48,767,825✔
236

237
int32_t initGroupedResultInfo(SGroupResInfo* pGroupResInfo, SSHashObj* pHashmap, int32_t order) {
535,969✔
238
  int32_t code = TSDB_CODE_SUCCESS;
535,969✔
239
  int32_t lino = 0;
535,969✔
240
  if (pGroupResInfo->pRows != NULL) {
535,969✔
241
    taosArrayDestroy(pGroupResInfo->pRows);
49,922✔
242
  }
243
  if (pGroupResInfo->pBuf) {
535,969✔
244
    taosMemoryFree(pGroupResInfo->pBuf);
49,922!
245
    pGroupResInfo->pBuf = NULL;
49,922✔
246
  }
247

248
  // extract the result rows information from the hash map
249
  int32_t size = tSimpleHashGetSize(pHashmap);
535,969✔
250

251
  void* pData = NULL;
536,047✔
252
  pGroupResInfo->pRows = taosArrayInit(size, POINTER_BYTES);
536,047✔
253
  QUERY_CHECK_NULL(pGroupResInfo->pRows, code, lino, _end, terrno);
536,233!
254

255
  size_t  keyLen = 0;
536,233✔
256
  int32_t iter = 0;
536,233✔
257
  int64_t bufLen = 0, offset = 0;
536,233✔
258

259
  // todo move away and record this during create window
260
  while ((pData = tSimpleHashIterate(pHashmap, pData, &iter)) != NULL) {
73,817,837✔
261
    /*void* key = */ (void)tSimpleHashGetKey(pData, &keyLen);
262
    bufLen += keyLen + sizeof(SResultRowPosition);
73,281,604✔
263
  }
264

265
  pGroupResInfo->pBuf = taosMemoryMalloc(bufLen);
531,622!
266
  QUERY_CHECK_NULL(pGroupResInfo->pBuf, code, lino, _end, terrno);
536,286!
267

268
  iter = 0;
536,286✔
269
  while ((pData = tSimpleHashIterate(pHashmap, pData, &iter)) != NULL) {
73,688,014✔
270
    void* key = tSimpleHashGetKey(pData, &keyLen);
73,157,142✔
271

272
    SResKeyPos* p = (SResKeyPos*)(pGroupResInfo->pBuf + offset);
73,157,142✔
273

274
    p->groupId = *(uint64_t*)key;
73,157,142✔
275
    p->pos = *(SResultRowPosition*)pData;
73,157,142✔
276
    memcpy(p->key, (char*)key + sizeof(uint64_t), keyLen - sizeof(uint64_t));
73,157,142✔
277
    void* tmp = taosArrayPush(pGroupResInfo->pRows, &p);
73,157,142✔
278
    QUERY_CHECK_NULL(pGroupResInfo->pBuf, code, lino, _end, terrno);
73,151,728!
279

280
    offset += keyLen + sizeof(struct SResultRowPosition);
73,151,728✔
281
  }
282

283
  if (order == TSDB_ORDER_ASC || order == TSDB_ORDER_DESC) {
511,820✔
284
    __compar_fn_t fn = (order == TSDB_ORDER_ASC) ? resultrowComparAsc : resultrowComparDesc;
73,703✔
285
    size = POINTER_BYTES;
73,703✔
286
    taosSort(pGroupResInfo->pRows->pData, taosArrayGetSize(pGroupResInfo->pRows), size, fn);
73,703✔
287
  }
288

289
  pGroupResInfo->index = 0;
536,275✔
290

291
_end:
536,275✔
292
  if (code != TSDB_CODE_SUCCESS) {
536,275!
293
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
294
  }
295
  return code;
536,188✔
296
}
297

298
void initMultiResInfoFromArrayList(SGroupResInfo* pGroupResInfo, SArray* pArrayList) {
19,704✔
299
  if (pGroupResInfo->pRows != NULL) {
19,704✔
300
    taosArrayDestroy(pGroupResInfo->pRows);
16,407✔
301
  }
302

303
  pGroupResInfo->freeItem = true;
19,704✔
304
  pGroupResInfo->pRows = pArrayList;
19,704✔
305
  pGroupResInfo->index = 0;
19,704✔
306
}
19,704✔
307

308
bool hasRemainResults(SGroupResInfo* pGroupResInfo) {
2,291,099✔
309
  if (pGroupResInfo->pRows == NULL) {
2,291,099✔
310
    return false;
635✔
311
  }
312

313
  return pGroupResInfo->index < taosArrayGetSize(pGroupResInfo->pRows);
2,290,464✔
314
}
315

316
int32_t getNumOfTotalRes(SGroupResInfo* pGroupResInfo) {
1,210,140✔
317
  if (pGroupResInfo->pRows == 0) {
1,210,140✔
318
    return 0;
622✔
319
  }
320

321
  return (int32_t)taosArrayGetSize(pGroupResInfo->pRows);
1,209,518✔
322
}
323

324
SArray* createSortInfo(SNodeList* pNodeList) {
272,768✔
325
  size_t numOfCols = 0;
272,768✔
326

327
  if (pNodeList != NULL) {
272,768✔
328
    numOfCols = LIST_LENGTH(pNodeList);
272,588!
329
  } else {
330
    numOfCols = 0;
180✔
331
  }
332

333
  SArray* pList = taosArrayInit(numOfCols, sizeof(SBlockOrderInfo));
272,768✔
334
  if (pList == NULL) {
272,785!
335
    return pList;
×
336
  }
337

338
  for (int32_t i = 0; i < numOfCols; ++i) {
615,041✔
339
    SOrderByExprNode* pSortKey = (SOrderByExprNode*)nodesListGetNode(pNodeList, i);
342,214✔
340
    if (!pSortKey) {
342,239✔
341
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
13!
342
      taosArrayDestroy(pList);
13✔
343
      pList = NULL;
×
344
      terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
345
      break;
×
346
    }
347
    SBlockOrderInfo   bi = {0};
342,226✔
348
    bi.order = (pSortKey->order == ORDER_ASC) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC;
342,226✔
349
    bi.nullFirst = (pSortKey->nullOrder == NULL_ORDER_FIRST);
342,226✔
350

351
    SColumnNode* pColNode = (SColumnNode*)pSortKey->pExpr;
342,226✔
352
    bi.slotId = pColNode->slotId;
342,226✔
353
    void* tmp = taosArrayPush(pList, &bi);
342,256✔
354
    if (!tmp) {
342,256!
355
      taosArrayDestroy(pList);
×
356
      pList = NULL;
×
357
      break;
×
358
    }
359
  }
360

361
  return pList;
272,827✔
362
}
363

364
SSDataBlock* createDataBlockFromDescNode(SDataBlockDescNode* pNode) {
3,724,483✔
365
  int32_t      numOfCols = LIST_LENGTH(pNode->pSlots);
3,724,483!
366
  SSDataBlock* pBlock = NULL;
3,724,483✔
367
  int32_t      code = createDataBlock(&pBlock);
3,724,483✔
368
  if (code) {
3,726,700!
369
    terrno = code;
×
370
    return NULL;
×
371
  }
372

373
  pBlock->info.id.blockId = pNode->dataBlockId;
3,727,167✔
374
  pBlock->info.type = STREAM_INVALID;
3,727,167✔
375
  pBlock->info.calWin = (STimeWindow){.skey = INT64_MIN, .ekey = INT64_MAX};
3,727,167✔
376
  pBlock->info.watermark = INT64_MIN;
3,727,167✔
377

378
  for (int32_t i = 0; i < numOfCols; ++i) {
15,682,349✔
379
    SSlotDescNode*  pDescNode = (SSlotDescNode*)nodesListGetNode(pNode->pSlots, i);
11,953,022✔
380
    if (!pDescNode) {
11,948,094!
381
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
382
      blockDataDestroy(pBlock);
×
383
      pBlock = NULL;
×
384
      terrno = TSDB_CODE_INVALID_PARA;
×
385
      break;
×
386
    }
387
    SColumnInfoData idata =
388
        createColumnInfoData(pDescNode->dataType.type, pDescNode->dataType.bytes, pDescNode->slotId);
11,948,094✔
389
    idata.info.scale = pDescNode->dataType.scale;
11,957,276✔
390
    idata.info.precision = pDescNode->dataType.precision;
11,957,276✔
391
    idata.info.noData = pDescNode->reserve;
11,957,276✔
392

393
    code = blockDataAppendColInfo(pBlock, &idata);
11,957,276✔
394
    if (code != TSDB_CODE_SUCCESS) {
11,955,182!
395
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
396
      blockDataDestroy(pBlock);
×
397
      pBlock = NULL;
×
398
      terrno = code;
×
399
      break;
×
400
    }
401
  }
402

403
  return pBlock;
3,729,327✔
404
}
405

406
int32_t prepareDataBlockBuf(SSDataBlock* pDataBlock, SColMatchInfo* pMatchInfo) {
1,107,785✔
407
  SDataBlockInfo* pBlockInfo = &pDataBlock->info;
1,107,785✔
408

409
  for (int32_t i = 0; i < taosArrayGetSize(pMatchInfo->pList); ++i) {
4,325,180✔
410
    SColMatchItem* pItem = taosArrayGet(pMatchInfo->pList, i);
3,217,478✔
411
    if (!pItem) {
3,218,954✔
412
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
1,211!
413
      return terrno;
1,211✔
414
    }
415

416
    if (pItem->isPk) {
3,217,743✔
417
      SColumnInfoData* pInfoData = taosArrayGet(pDataBlock->pDataBlock, pItem->dstSlotId);
348✔
418
      if (!pInfoData) {
348!
419
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
420
        return terrno;
×
421
      }
422
      pBlockInfo->pks[0].type = pInfoData->info.type;
348✔
423
      pBlockInfo->pks[1].type = pInfoData->info.type;
348✔
424

425
      // allocate enough buffer size, which is pInfoData->info.bytes
426
      if (IS_VAR_DATA_TYPE(pItem->dataType.type)) {
348!
427
        pBlockInfo->pks[0].pData = taosMemoryCalloc(1, pInfoData->info.bytes);
33!
428
        if (pBlockInfo->pks[0].pData == NULL) {
33!
429
          return terrno;
×
430
        }
431

432
        pBlockInfo->pks[1].pData = taosMemoryCalloc(1, pInfoData->info.bytes);
33!
433
        if (pBlockInfo->pks[1].pData == NULL) {
33!
434
          taosMemoryFreeClear(pBlockInfo->pks[0].pData);
×
435
          return terrno;
×
436
        }
437

438
        pBlockInfo->pks[0].nData = pInfoData->info.bytes;
33✔
439
        pBlockInfo->pks[1].nData = pInfoData->info.bytes;
33✔
440
      }
441

442
      break;
348✔
443
    }
444
  }
445

446
  return TSDB_CODE_SUCCESS;
1,108,585✔
447
}
448

449
EDealRes doTranslateTagExpr(SNode** pNode, void* pContext) {
5,968✔
450
  STransTagExprCtx* pCtx = pContext;
5,968✔
451
  SMetaReader*      mr = pCtx->pReader;
5,968✔
452
  bool              isTagCol = false, isTbname = false;
5,968✔
453
  if (nodeType(*pNode) == QUERY_NODE_COLUMN) {
5,968✔
454
    SColumnNode* pCol = (SColumnNode*)*pNode;
1,711✔
455
    if (pCol->colType == COLUMN_TYPE_TBNAME)
1,711!
456
      isTbname = true;
×
457
    else
458
      isTagCol = true;
1,711✔
459
  } else if (nodeType(*pNode) == QUERY_NODE_FUNCTION) {
4,257!
460
    SFunctionNode* pFunc = (SFunctionNode*)*pNode;
×
461
    if (pFunc->funcType == FUNCTION_TYPE_TBNAME) isTbname = true;
×
462
  }
463
  if (isTagCol) {
5,968✔
464
    SColumnNode* pSColumnNode = *(SColumnNode**)pNode;
1,711✔
465

466
    SValueNode* res = NULL;
1,711✔
467
    pCtx->code = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&res);
1,711✔
468
    if (NULL == res) {
1,711!
469
      return DEAL_RES_ERROR;
×
470
    }
471

472
    res->translate = true;
1,711✔
473
    res->node.resType = pSColumnNode->node.resType;
1,711✔
474

475
    STagVal tagVal = {0};
1,711✔
476
    tagVal.cid = pSColumnNode->colId;
1,711✔
477
    const char* p = mr->pAPI->extractTagVal(mr->me.ctbEntry.pTags, pSColumnNode->node.resType.type, &tagVal);
1,711✔
478
    if (p == NULL) {
1,711!
479
      res->node.resType.type = TSDB_DATA_TYPE_NULL;
×
480
    } else if (pSColumnNode->node.resType.type == TSDB_DATA_TYPE_JSON) {
1,711!
481
      int32_t len = ((const STag*)p)->len;
×
482
      res->datum.p = taosMemoryCalloc(len + 1, 1);
×
483
      if (NULL == res->datum.p) {
×
484
        return DEAL_RES_ERROR;
×
485
      }
486
      memcpy(res->datum.p, p, len);
×
487
    } else if (IS_VAR_DATA_TYPE(pSColumnNode->node.resType.type)) {
1,711!
488
      res->datum.p = taosMemoryCalloc(tagVal.nData + VARSTR_HEADER_SIZE + 1, 1);
1,696!
489
      if (NULL == res->datum.p) {
1,696!
490
        return DEAL_RES_ERROR;
×
491
      }
492
      memcpy(varDataVal(res->datum.p), tagVal.pData, tagVal.nData);
1,696✔
493
      varDataSetLen(res->datum.p, tagVal.nData);
1,696✔
494
    } else {
495
      int32_t code = nodesSetValueNodeValue(res, &(tagVal.i64));
15✔
496
      if (code != TSDB_CODE_SUCCESS) {
15!
497
        return DEAL_RES_ERROR;
×
498
      }
499
    }
500
    nodesDestroyNode(*pNode);
1,711✔
501
    *pNode = (SNode*)res;
1,711✔
502
  } else if (isTbname) {
4,257!
503
    SValueNode* res = NULL;
×
504
    pCtx->code = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&res);
×
505
    if (NULL == res) {
×
506
      return DEAL_RES_ERROR;
×
507
    }
508

509
    res->translate = true;
×
510
    res->node.resType = ((SExprNode*)(*pNode))->resType;
×
511

512
    int32_t len = strlen(mr->me.name);
×
513
    res->datum.p = taosMemoryCalloc(len + VARSTR_HEADER_SIZE + 1, 1);
×
514
    if (NULL == res->datum.p) {
×
515
      return DEAL_RES_ERROR;
×
516
    }
517
    memcpy(varDataVal(res->datum.p), mr->me.name, len);
×
518
    varDataSetLen(res->datum.p, len);
×
519
    nodesDestroyNode(*pNode);
×
520
    *pNode = (SNode*)res;
×
521
  }
522

523
  return DEAL_RES_CONTINUE;
5,968✔
524
}
525

526
int32_t isQualifiedTable(STableKeyInfo* info, SNode* pTagCond, void* metaHandle, bool* pQualified, SStorageAPI* pAPI) {
858✔
527
  int32_t     code = TSDB_CODE_SUCCESS;
858✔
528
  SMetaReader mr = {0};
858✔
529

530
  pAPI->metaReaderFn.initReader(&mr, metaHandle, META_READER_LOCK, &pAPI->metaFn);
858✔
531
  code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, info->uid);
858✔
532
  if (TSDB_CODE_SUCCESS != code) {
858!
533
    pAPI->metaReaderFn.clearReader(&mr);
×
534
    *pQualified = false;
×
535

536
    return TSDB_CODE_SUCCESS;
×
537
  }
538

539
  SNode* pTagCondTmp = NULL;
858✔
540
  code = nodesCloneNode(pTagCond, &pTagCondTmp);
858✔
541
  if (TSDB_CODE_SUCCESS != code) {
858!
542
    *pQualified = false;
×
543
    pAPI->metaReaderFn.clearReader(&mr);
×
544
    return code;
×
545
  }
546
  STransTagExprCtx ctx = {.code = 0, .pReader = &mr};
858✔
547
  nodesRewriteExprPostOrder(&pTagCondTmp, doTranslateTagExpr, &ctx);
858✔
548
  pAPI->metaReaderFn.clearReader(&mr);
858✔
549
  if (TSDB_CODE_SUCCESS != ctx.code) {
858!
550
    *pQualified = false;
×
551
    terrno = code;
×
552
    return code;
×
553
  }
554

555
  SNode* pNew = NULL;
858✔
556
  code = scalarCalculateConstants(pTagCondTmp, &pNew);
858✔
557
  if (TSDB_CODE_SUCCESS != code) {
858!
558
    terrno = code;
×
559
    nodesDestroyNode(pTagCondTmp);
×
560
    *pQualified = false;
×
561

562
    return code;
×
563
  }
564

565
  SValueNode* pValue = (SValueNode*)pNew;
858✔
566
  *pQualified = pValue->datum.b;
858✔
567

568
  nodesDestroyNode(pNew);
858✔
569
  return TSDB_CODE_SUCCESS;
858✔
570
}
571

572
static EDealRes getColumn(SNode** pNode, void* pContext) {
381,056✔
573
  tagFilterAssist* pData = (tagFilterAssist*)pContext;
381,056✔
574
  SColumnNode*     pSColumnNode = NULL;
381,056✔
575
  if (QUERY_NODE_COLUMN == nodeType((*pNode))) {
381,056✔
576
    pSColumnNode = *(SColumnNode**)pNode;
137,282✔
577
  } else if (QUERY_NODE_FUNCTION == nodeType((*pNode))) {
243,774✔
578
    SFunctionNode* pFuncNode = *(SFunctionNode**)(pNode);
19,465✔
579
    if (pFuncNode->funcType == FUNCTION_TYPE_TBNAME) {
19,465✔
580
      pData->code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&pSColumnNode);
8,007✔
581
      if (NULL == pSColumnNode) {
8,004!
582
        return DEAL_RES_ERROR;
×
583
      }
584
      pSColumnNode->colId = -1;
8,004✔
585
      pSColumnNode->colType = COLUMN_TYPE_TBNAME;
8,004✔
586
      pSColumnNode->node.resType.type = TSDB_DATA_TYPE_VARCHAR;
8,004✔
587
      pSColumnNode->node.resType.bytes = TSDB_TABLE_FNAME_LEN - 1 + VARSTR_HEADER_SIZE;
8,004✔
588
      nodesDestroyNode(*pNode);
8,004✔
589
      *pNode = (SNode*)pSColumnNode;
8,005✔
590
    } else {
591
      return DEAL_RES_CONTINUE;
11,458✔
592
    }
593
  } else {
594
    return DEAL_RES_CONTINUE;
224,309✔
595
  }
596

597
  void* data = taosHashGet(pData->colHash, &pSColumnNode->colId, sizeof(pSColumnNode->colId));
145,287✔
598
  if (!data) {
145,219✔
599
    int32_t tempRes =
600
        taosHashPut(pData->colHash, &pSColumnNode->colId, sizeof(pSColumnNode->colId), pNode, sizeof((*pNode)));
113,804✔
601
    if (tempRes != TSDB_CODE_SUCCESS && tempRes != TSDB_CODE_DUP_KEY) {
113,916!
602
      return DEAL_RES_ERROR;
×
603
    }
604
    pSColumnNode->slotId = pData->index++;
113,916✔
605
    SColumnInfo cInfo = {.colId = pSColumnNode->colId,
113,916✔
606
                         .type = pSColumnNode->node.resType.type,
113,916✔
607
                         .bytes = pSColumnNode->node.resType.bytes,
113,916✔
608
                         .pk = pSColumnNode->isPk};
113,916✔
609
#if TAG_FILTER_DEBUG
610
    qDebug("tagfilter build column info, slotId:%d, colId:%d, type:%d", pSColumnNode->slotId, cInfo.colId, cInfo.type);
611
#endif
612
    void* tmp = taosArrayPush(pData->cInfoList, &cInfo);
113,916✔
613
    if (!tmp) {
113,954!
614
      return DEAL_RES_ERROR;
×
615
    }
616
  } else {
617
    SColumnNode* col = *(SColumnNode**)data;
31,415✔
618
    pSColumnNode->slotId = col->slotId;
31,415✔
619
  }
620

621
  return DEAL_RES_CONTINUE;
145,369✔
622
}
623

624
static int32_t createResultData(SDataType* pType, int32_t numOfRows, SScalarParam* pParam) {
112,395✔
625
  SColumnInfoData* pColumnData = taosMemoryCalloc(1, sizeof(SColumnInfoData));
112,395!
626
  if (pColumnData == NULL) {
112,489!
627
    return terrno;
×
628
  }
629

630
  pColumnData->info.type = pType->type;
112,489✔
631
  pColumnData->info.bytes = pType->bytes;
112,489✔
632
  pColumnData->info.scale = pType->scale;
112,489✔
633
  pColumnData->info.precision = pType->precision;
112,489✔
634

635
  int32_t code = colInfoDataEnsureCapacity(pColumnData, numOfRows, true);
112,489✔
636
  if (code != TSDB_CODE_SUCCESS) {
112,476!
637
    terrno = code;
×
638
    releaseColInfoData(pColumnData);
×
639
    return terrno;
×
640
  }
641

642
  pParam->columnData = pColumnData;
112,481✔
643
  pParam->colAlloced = true;
112,481✔
644
  return TSDB_CODE_SUCCESS;
112,481✔
645
}
646

647
static void releaseColInfoData(void* pCol) {
53,419✔
648
  if (pCol) {
53,419!
649
    SColumnInfoData* col = (SColumnInfoData*)pCol;
53,419✔
650
    colDataDestroy(col);
53,419✔
651
    taosMemoryFree(col);
53,423!
652
  }
653
}
53,428✔
654

655
void freeItem(void* p) {
535,620✔
656
  STUidTagInfo* pInfo = p;
535,620✔
657
  if (pInfo->pTagVal != NULL) {
535,620✔
658
    taosMemoryFree(pInfo->pTagVal);
532,939!
659
  }
660
}
535,729✔
661

662
static int32_t genTagFilterDigest(const SNode* pTagCond, T_MD5_CTX* pContext) {
×
663
  if (pTagCond == NULL) {
×
664
    return TSDB_CODE_SUCCESS;
×
665
  }
666

667
  char*   payload = NULL;
×
668
  int32_t len = 0;
×
669
  int32_t code = nodesNodeToMsg(pTagCond, &payload, &len);
×
670
  if (code != TSDB_CODE_SUCCESS) {
×
671
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
672
    return code;
×
673
  }
674

675
  tMD5Init(pContext);
×
676
  tMD5Update(pContext, (uint8_t*)payload, (uint32_t)len);
×
677
  tMD5Final(pContext);
×
678

679
  taosMemoryFree(payload);
×
680
  return TSDB_CODE_SUCCESS;
×
681
}
682

683
static int32_t genTbGroupDigest(const SNode* pGroup, uint8_t* filterDigest, T_MD5_CTX* pContext) {
×
684
  int32_t code = TSDB_CODE_SUCCESS;
×
685
  int32_t lino = 0;
×
686
  char*   payload = NULL;
×
687
  int32_t len = 0;
×
688
  code = nodesNodeToMsg(pGroup, &payload, &len);
×
689
  QUERY_CHECK_CODE(code, lino, _end);
×
690

691
  if (filterDigest[0]) {
×
692
    payload = taosMemoryRealloc(payload, len + tListLen(pContext->digest));
×
693
    QUERY_CHECK_NULL(payload, code, lino, _end, terrno);
×
694
    memcpy(payload + len, filterDigest + 1, tListLen(pContext->digest));
×
695
    len += tListLen(pContext->digest);
×
696
  }
697

698
  tMD5Init(pContext);
×
699
  tMD5Update(pContext, (uint8_t*)payload, (uint32_t)len);
×
700
  tMD5Final(pContext);
×
701

702
_end:
×
703
  taosMemoryFree(payload);
×
704
  if (code != TSDB_CODE_SUCCESS) {
×
705
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
706
  }
707
  return code;
×
708
}
709

710
int32_t getColInfoResultForGroupby(void* pVnode, SNodeList* group, STableListInfo* pTableListInfo, uint8_t* digest,
40,546✔
711
                                   SStorageAPI* pAPI, bool initRemainGroups) {
712
  int32_t      code = TSDB_CODE_SUCCESS;
40,546✔
713
  int32_t      lino = 0;
40,546✔
714
  SArray*      pBlockList = NULL;
40,546✔
715
  SSDataBlock* pResBlock = NULL;
40,546✔
716
  void*        keyBuf = NULL;
40,546✔
717
  SArray*      groupData = NULL;
40,546✔
718
  SArray*      pUidTagList = NULL;
40,546✔
719
  SArray*      tableList = NULL;
40,546✔
720

721
  int32_t rows = taosArrayGetSize(pTableListInfo->pTableList);
40,546✔
722
  if (rows == 0) {
40,549!
723
    return TSDB_CODE_SUCCESS;
×
724
  }
725

726
  tagFilterAssist ctx = {0};
40,549✔
727
  ctx.colHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
40,549✔
728
  if (ctx.colHash == NULL) {
40,555!
729
    code = terrno;
×
730
    goto end;
×
731
  }
732

733
  ctx.index = 0;
40,555✔
734
  ctx.cInfoList = taosArrayInit(4, sizeof(SColumnInfo));
40,555✔
735
  if (ctx.cInfoList == NULL) {
40,560!
UNCOV
736
    code = terrno;
×
737
    goto end;
×
738
  }
739

740
  SNode* pNode = NULL;
40,560✔
741
  FOREACH(pNode, group) {
93,976!
742
    nodesRewriteExprPostOrder(&pNode, getColumn, (void*)&ctx);
53,418✔
743
    if (TSDB_CODE_SUCCESS != ctx.code) {
53,416!
744
      code = ctx.code;
×
745
      goto end;
×
746
    }
747
    REPLACE_NODE(pNode);
53,416✔
748
  }
749

750
  T_MD5_CTX context = {0};
40,558✔
751
  if (tsTagFilterCache) {
40,558!
752
    SNodeListNode* listNode = NULL;
×
753
    code = nodesMakeNode(QUERY_NODE_NODE_LIST, (SNode**)&listNode);
×
754
    if (TSDB_CODE_SUCCESS != code) {
×
755
      goto end;
×
756
    }
757
    listNode->pNodeList = group;
×
758
    code = genTbGroupDigest((SNode*)listNode, digest, &context);
×
759
    QUERY_CHECK_CODE(code, lino, end);
×
760

761
    nodesFree(listNode);
×
762

763
    code = pAPI->metaFn.metaGetCachedTbGroup(pVnode, pTableListInfo->idInfo.suid, context.digest,
×
764
                                             tListLen(context.digest), &tableList);
765
    QUERY_CHECK_CODE(code, lino, end);
×
766

767
    if (tableList) {
×
768
      taosArrayDestroy(pTableListInfo->pTableList);
×
769
      pTableListInfo->pTableList = tableList;
×
770
      qDebug("retrieve tb group list from cache, numOfTables:%d",
×
771
             (int32_t)taosArrayGetSize(pTableListInfo->pTableList));
772
      goto end;
×
773
    }
774
  }
775

776
  pUidTagList = taosArrayInit(8, sizeof(STUidTagInfo));
40,558✔
777
  QUERY_CHECK_NULL(pUidTagList, code, lino, end, terrno);
40,537!
778

779
  for (int32_t i = 0; i < rows; ++i) {
228,500✔
780
    STableKeyInfo* pkeyInfo = taosArrayGet(pTableListInfo->pTableList, i);
187,904✔
781
    QUERY_CHECK_NULL(pkeyInfo, code, lino, end, terrno);
187,910!
782
    STUidTagInfo   info = {.uid = pkeyInfo->uid};
187,910✔
783
    void*          tmp = taosArrayPush(pUidTagList, &info);
187,959✔
784
    QUERY_CHECK_NULL(tmp, code, lino, end, terrno);
187,959!
785
  }
786

787
  code = pAPI->metaFn.getTableTags(pVnode, pTableListInfo->idInfo.suid, pUidTagList);
40,596✔
788
  if (code != TSDB_CODE_SUCCESS) {
40,566!
789
    goto end;
×
790
  }
791

792
  int32_t numOfTables = taosArrayGetSize(pUidTagList);
40,566✔
793
  pResBlock = createTagValBlockForFilter(ctx.cInfoList, numOfTables, pUidTagList, pVnode, pAPI);
40,564✔
794
  if (pResBlock == NULL) {
40,555!
795
    code = terrno;
×
796
    goto end;
×
797
  }
798

799
  //  int64_t st1 = taosGetTimestampUs();
800
  //  qDebug("generate tag block rows:%d, cost:%ld us", rows, st1-st);
801

802
  pBlockList = taosArrayInit(2, POINTER_BYTES);
40,555✔
803
  QUERY_CHECK_NULL(pBlockList, code, lino, end, terrno);
40,556!
804

805
  void* tmp = taosArrayPush(pBlockList, &pResBlock);
40,561✔
806
  QUERY_CHECK_NULL(tmp, code, lino, end, terrno);
40,561!
807

808
  groupData = taosArrayInit(2, POINTER_BYTES);
40,561✔
809
  QUERY_CHECK_NULL(groupData, code, lino, end, terrno);
40,563✔
810

811
  FOREACH(pNode, group) {
93,965!
812
    SScalarParam output = {0};
53,408✔
813

814
    switch (nodeType(pNode)) {
53,408!
815
      case QUERY_NODE_VALUE:
×
816
        break;
×
817
      case QUERY_NODE_COLUMN:
53,408✔
818
      case QUERY_NODE_OPERATOR:
819
      case QUERY_NODE_FUNCTION: {
820
        SExprNode* expNode = (SExprNode*)pNode;
53,408✔
821
        code = createResultData(&expNode->resType, rows, &output);
53,408✔
822
        if (code != TSDB_CODE_SUCCESS) {
53,420!
823
          goto end;
×
824
        }
825
        break;
53,420✔
826
      }
827

828
      default:
×
829
        code = TSDB_CODE_OPS_NOT_SUPPORT;
×
830
        goto end;
×
831
    }
832

833
    if (nodeType(pNode) == QUERY_NODE_COLUMN) {
53,420✔
834
      SColumnNode*     pSColumnNode = (SColumnNode*)pNode;
48,896✔
835
      SColumnInfoData* pColInfo = (SColumnInfoData*)taosArrayGet(pResBlock->pDataBlock, pSColumnNode->slotId);
48,896✔
836
      QUERY_CHECK_NULL(pColInfo, code, lino, end, terrno);
48,889!
837
      code = colDataAssign(output.columnData, pColInfo, rows, NULL);
48,889✔
838
    } else if (nodeType(pNode) == QUERY_NODE_VALUE) {
4,524!
839
      continue;
×
840
    } else {
841
      code = scalarCalculate(pNode, pBlockList, &output);
4,524✔
842
    }
843

844
    if (code != TSDB_CODE_SUCCESS) {
53,399!
845
      releaseColInfoData(output.columnData);
×
846
      goto end;
×
847
    }
848

849
    void* tmp = taosArrayPush(groupData, &output.columnData);
53,406✔
850
    QUERY_CHECK_NULL(tmp, code, lino, end, terrno);
53,406!
851
  }
852

853
  int32_t keyLen = 0;
40,557✔
854
  SNode*  node;
855
  FOREACH(node, group) {
93,965✔
856
    SExprNode* pExpr = (SExprNode*)node;
53,408✔
857
    keyLen += pExpr->resType.bytes;
53,408✔
858
  }
859

860
  int32_t nullFlagSize = sizeof(int8_t) * LIST_LENGTH(group);
40,557✔
861
  keyLen += nullFlagSize;
40,557✔
862

863
  keyBuf = taosMemoryCalloc(1, keyLen);
40,557✔
864
  if (keyBuf == NULL) {
40,552!
865
    code = terrno;
×
866
    goto end;
×
867
  }
868

869
  if (initRemainGroups) {
40,552✔
870
    pTableListInfo->remainGroups =
13,095✔
871
        taosHashInit(rows, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
13,095✔
872
    if (pTableListInfo->remainGroups == NULL) {
13,095!
873
      code = terrno;
×
874
      goto end;
×
875
    }
876
  }
877

878
  for (int i = 0; i < rows; i++) {
228,400✔
879
    STableKeyInfo* info = taosArrayGet(pTableListInfo->pTableList, i);
187,829✔
880
    QUERY_CHECK_NULL(info, code, lino, end, terrno);
187,793!
881

882
    char* isNull = (char*)keyBuf;
187,800✔
883
    char* pStart = (char*)keyBuf + sizeof(int8_t) * LIST_LENGTH(group);
187,800!
884
    for (int j = 0; j < taosArrayGetSize(groupData); j++) {
407,278✔
885
      SColumnInfoData* pValue = (SColumnInfoData*)taosArrayGetP(groupData, j);
219,537✔
886

887
      if (colDataIsNull_s(pValue, i)) {
438,984✔
888
        isNull[j] = 1;
486✔
889
      } else {
890
        isNull[j] = 0;
219,006✔
891
        char* data = colDataGetData(pValue, i);
219,006!
892
        if (pValue->info.type == TSDB_DATA_TYPE_JSON) {
219,006✔
893
          if (tTagIsJson(data)) {
264✔
894
            code = TSDB_CODE_QRY_JSON_IN_GROUP_ERROR;
12✔
895
            goto end;
12✔
896
          }
897
          if (tTagIsJsonNull(data)) {
252!
898
            isNull[j] = 1;
×
899
            continue;
×
900
          }
901
          int32_t len = getJsonValueLen(data);
252✔
902
          memcpy(pStart, data, len);
252✔
903
          pStart += len;
252✔
904
        } else if (IS_VAR_DATA_TYPE(pValue->info.type)) {
218,742!
905
          if (varDataTLen(data) > pValue->info.bytes) {
188,621✔
906
            code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
2✔
907
            goto end;
2✔
908
          }
909
          memcpy(pStart, data, varDataTLen(data));
188,619✔
910
          pStart += varDataTLen(data);
188,619✔
911
        } else {
912
          memcpy(pStart, data, pValue->info.bytes);
30,121✔
913
          pStart += pValue->info.bytes;
30,121✔
914
        }
915
      }
916
    }
917

918
    int32_t len = (int32_t)(pStart - (char*)keyBuf);
187,111✔
919
    info->groupId = calcGroupId(keyBuf, len);
187,111✔
920
    if (initRemainGroups) {
187,853✔
921
      // groupId ~ table uid
922
      code = taosHashPut(pTableListInfo->remainGroups, &(info->groupId), sizeof(info->groupId), &(info->uid),
76,594✔
923
                         sizeof(info->uid));
924
      if (code == TSDB_CODE_DUP_KEY) {
76,589✔
925
        code = TSDB_CODE_SUCCESS;
1,246✔
926
      }
927
      QUERY_CHECK_CODE(code, lino, end);
76,589!
928
    }
929
  }
930

931
  if (tsTagFilterCache) {
40,571!
932
    tableList = taosArrayDup(pTableListInfo->pTableList, NULL);
×
933
    QUERY_CHECK_NULL(tableList, code, lino, end, terrno);
×
934

935
    code = pAPI->metaFn.metaPutTbGroupToCache(pVnode, pTableListInfo->idInfo.suid, context.digest,
×
936
                                              tListLen(context.digest), tableList,
937
                                              taosArrayGetSize(tableList) * sizeof(STableKeyInfo));
×
938
    QUERY_CHECK_CODE(code, lino, end);
×
939
  }
940

941
  //  int64_t st2 = taosGetTimestampUs();
942
  //  qDebug("calculate tag block rows:%d, cost:%ld us", rows, st2-st1);
943

944
end:
40,571✔
945
  taosMemoryFreeClear(keyBuf);
40,585!
946
  taosHashCleanup(ctx.colHash);
40,586✔
947
  taosArrayDestroy(ctx.cInfoList);
40,542✔
948
  blockDataDestroy(pResBlock);
40,548✔
949
  taosArrayDestroy(pBlockList);
40,563✔
950
  taosArrayDestroyEx(pUidTagList, freeItem);
40,557✔
951
  taosArrayDestroyP(groupData, releaseColInfoData);
40,561✔
952
  if (code != TSDB_CODE_SUCCESS) {
40,559✔
953
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
14!
954
  }
955
  return code;
40,560✔
956
}
957

958
static int32_t nameComparFn(const void* p1, const void* p2) {
2,448✔
959
  const char* pName1 = *(const char**)p1;
2,448✔
960
  const char* pName2 = *(const char**)p2;
2,448✔
961

962
  int32_t ret = strcmp(pName1, pName2);
2,448✔
963
  if (ret == 0) {
2,448✔
964
    return 0;
16✔
965
  } else {
966
    return (ret > 0) ? 1 : -1;
2,432✔
967
  }
968
}
969

970
static SArray* getTableNameList(const SNodeListNode* pList) {
2,865✔
971
  int32_t    code = TSDB_CODE_SUCCESS;
2,865✔
972
  int32_t    lino = 0;
2,865✔
973
  int32_t    len = LIST_LENGTH(pList->pNodeList);
2,865!
974
  SListCell* cell = pList->pNodeList->pHead;
2,865✔
975

976
  SArray* pTbList = taosArrayInit(len, POINTER_BYTES);
2,865✔
977
  QUERY_CHECK_NULL(pTbList, code, lino, _end, terrno);
2,867✔
978

979
  for (int i = 0; i < pList->pNodeList->length; i++) {
7,481✔
980
    SValueNode* valueNode = (SValueNode*)cell->pNode;
4,614✔
981
    if (!IS_VAR_DATA_TYPE(valueNode->node.resType.type)) {
4,614!
982
      terrno = TSDB_CODE_INVALID_PARA;
×
983
      taosArrayDestroy(pTbList);
×
984
      return NULL;
×
985
    }
986

987
    char* name = varDataVal(valueNode->datum.p);
4,614✔
988
    void* tmp = taosArrayPush(pTbList, &name);
4,615✔
989
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
4,615!
990
    cell = cell->pNext;
4,615✔
991
  }
992

993
  size_t numOfTables = taosArrayGetSize(pTbList);
2,867✔
994

995
  // order the name
996
  taosArraySort(pTbList, nameComparFn);
2,867✔
997

998
  // remove the duplicates
999
  SArray* pNewList = taosArrayInit(taosArrayGetSize(pTbList), sizeof(void*));
2,863✔
1000
  QUERY_CHECK_NULL(pNewList, code, lino, _end, terrno);
2,867!
1001
  void*   tmpTbl = taosArrayGet(pTbList, 0);
2,867✔
1002
  QUERY_CHECK_NULL(tmpTbl, code, lino, _end, terrno);
2,866!
1003
  void*   tmp = taosArrayPush(pNewList, tmpTbl);
2,867✔
1004
  QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
2,867!
1005

1006
  for (int32_t i = 1; i < numOfTables; ++i) {
4,615✔
1007
    char** name = taosArrayGetLast(pNewList);
1,749✔
1008
    char** nameInOldList = taosArrayGet(pTbList, i);
1,749✔
1009
    QUERY_CHECK_NULL(nameInOldList, code, lino, _end, terrno);
1,750!
1010
    if (strcmp(*name, *nameInOldList) == 0) {
1,750✔
1011
      continue;
16✔
1012
    }
1013

1014
    tmp = taosArrayPush(pNewList, nameInOldList);
1,732✔
1015
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,732!
1016
  }
1017

1018
_end:
2,866✔
1019
  taosArrayDestroy(pTbList);
2,866✔
1020
  if (code != TSDB_CODE_SUCCESS) {
2,868✔
1021
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
1!
1022
    return NULL;
×
1023
  }
1024
  return pNewList;
2,867✔
1025
}
1026

1027
static int tableUidCompare(const void* a, const void* b) {
×
1028
  uint64_t u1 = *(uint64_t*)a;
×
1029
  uint64_t u2 = *(uint64_t*)b;
×
1030

1031
  if (u1 == u2) {
×
1032
    return 0;
×
1033
  }
1034

1035
  return u1 < u2 ? -1 : 1;
×
1036
}
1037

1038
static int32_t filterTableInfoCompare(const void* a, const void* b) {
42✔
1039
  STUidTagInfo* p1 = (STUidTagInfo*)a;
42✔
1040
  STUidTagInfo* p2 = (STUidTagInfo*)b;
42✔
1041

1042
  if (p1->uid == p2->uid) {
42!
1043
    return 0;
×
1044
  }
1045

1046
  return p1->uid < p2->uid ? -1 : 1;
42!
1047
}
1048

1049
static FilterCondType checkTagCond(SNode* cond) {
59,460✔
1050
  if (nodeType(cond) == QUERY_NODE_OPERATOR) {
59,460✔
1051
    return FILTER_NO_LOGIC;
31,072✔
1052
  }
1053
  if (nodeType(cond) != QUERY_NODE_LOGIC_CONDITION || ((SLogicConditionNode*)cond)->condType != LOGIC_COND_TYPE_AND) {
28,388!
1054
    return FILTER_AND;
5,141✔
1055
  }
1056
  return FILTER_OTHER;
23,247✔
1057
}
1058

1059
static int32_t optimizeTbnameInCond(void* pVnode, int64_t suid, SArray* list, SNode* cond, SStorageAPI* pAPI) {
59,503✔
1060
  int32_t ret = -1;
59,503✔
1061
  int32_t ntype = nodeType(cond);
59,503✔
1062

1063
  if (ntype == QUERY_NODE_OPERATOR) {
59,503✔
1064
    ret = optimizeTbnameInCondImpl(pVnode, list, cond, pAPI, suid);
31,083✔
1065
  }
1066

1067
  if (ntype != QUERY_NODE_LOGIC_CONDITION || ((SLogicConditionNode*)cond)->condType != LOGIC_COND_TYPE_AND) {
59,619✔
1068
    return ret;
36,362✔
1069
  }
1070

1071
  bool                 hasTbnameCond = false;
23,257✔
1072
  SLogicConditionNode* pNode = (SLogicConditionNode*)cond;
23,257✔
1073
  SNodeList*           pList = (SNodeList*)pNode->pParameterList;
23,257✔
1074

1075
  int32_t len = LIST_LENGTH(pList);
23,257!
1076
  if (len <= 0) {
23,257!
1077
    return ret;
×
1078
  }
1079

1080
  SListCell* cell = pList->pHead;
23,257✔
1081
  for (int i = 0; i < len; i++) {
70,340✔
1082
    if (cell == NULL) break;
47,124!
1083
    if (optimizeTbnameInCondImpl(pVnode, list, cell->pNode, pAPI, suid) == 0) {
47,124✔
1084
      hasTbnameCond = true;
35✔
1085
      break;
35✔
1086
    }
1087
    cell = cell->pNext;
47,083✔
1088
  }
1089

1090
  taosArraySort(list, filterTableInfoCompare);
23,251✔
1091
  taosArrayRemoveDuplicate(list, filterTableInfoCompare, NULL);
23,155✔
1092

1093
  if (hasTbnameCond) {
23,177✔
1094
    ret = pAPI->metaFn.getTableTagsByUid(pVnode, suid, list);
34✔
1095
  }
1096

1097
  return ret;
23,186✔
1098
}
1099

1100
// only return uid that does not contained in pExistedUidList
1101
static int32_t optimizeTbnameInCondImpl(void* pVnode, SArray* pExistedUidList, SNode* pTagCond,
78,064✔
1102
                                        SStorageAPI* pStoreAPI, uint64_t suid) {
1103
  if (nodeType(pTagCond) != QUERY_NODE_OPERATOR) {
78,064✔
1104
    return -1;
18✔
1105
  }
1106

1107
  SOperatorNode* pNode = (SOperatorNode*)pTagCond;
78,046✔
1108
  if (pNode->opType != OP_TYPE_IN) {
78,046✔
1109
    return -1;
71,354✔
1110
  }
1111

1112
  if ((pNode->pLeft != NULL && nodeType(pNode->pLeft) == QUERY_NODE_COLUMN &&
6,692!
1113
       ((SColumnNode*)pNode->pLeft)->colType == COLUMN_TYPE_TBNAME) &&
6,817✔
1114
      (pNode->pRight != NULL && nodeType(pNode->pRight) == QUERY_NODE_NODE_LIST)) {
2,866!
1115
    SNodeListNode* pList = (SNodeListNode*)pNode->pRight;
2,866✔
1116

1117
    int32_t len = LIST_LENGTH(pList->pNodeList);
2,866!
1118
    if (len <= 0) {
2,866!
1119
      return -1;
×
1120
    }
1121

1122
    SArray*   pTbList = getTableNameList(pList);
2,866✔
1123
    int32_t   numOfTables = taosArrayGetSize(pTbList);
2,867✔
1124
    SHashObj* uHash = NULL;
2,867✔
1125

1126
    size_t numOfExisted = taosArrayGetSize(pExistedUidList);  // len > 0 means there already have uids
2,867✔
1127
    if (numOfExisted > 0) {
2,863!
1128
      uHash = taosHashInit(numOfExisted / 0.7, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
×
1129
      if (!uHash) {
×
1130
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1131
        return terrno;
×
1132
      }
1133

1134
      for (int i = 0; i < numOfExisted; i++) {
×
1135
        STUidTagInfo* pTInfo = taosArrayGet(pExistedUidList, i);
×
1136
        if (!pTInfo) {
×
1137
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1138
          return terrno;
×
1139
        }
1140
        int32_t       tempRes = taosHashPut(uHash, &pTInfo->uid, sizeof(uint64_t), &i, sizeof(i));
×
1141
        if (tempRes != TSDB_CODE_SUCCESS && tempRes != TSDB_CODE_DUP_KEY) {
×
1142
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(tempRes));
×
1143
          return tempRes;
×
1144
        }
1145
      }
1146
    }
1147

1148
    for (int i = 0; i < numOfTables; i++) {
7,426✔
1149
      char* name = taosArrayGetP(pTbList, i);
4,579✔
1150

1151
      uint64_t uid = 0, csuid = 0;
4,583✔
1152
      if (pStoreAPI->metaFn.getTableUidByName(pVnode, name, &uid) == 0) {
4,583✔
1153
        ETableType tbType = TSDB_TABLE_MAX;
2,872✔
1154
        if (pStoreAPI->metaFn.getTableTypeSuidByName(pVnode, name, &tbType, &csuid) == 0 && tbType == TSDB_CHILD_TABLE) {
2,872!
1155
          if (suid != csuid) {
2,849✔
1156
            continue;
20✔
1157
          }
1158
          if (NULL == uHash || taosHashGet(uHash, &uid, sizeof(uid)) == NULL) {
2,829!
1159
            STUidTagInfo s = {.uid = uid, .name = name, .pTagVal = NULL};
2,829✔
1160
            void*        tmp = taosArrayPush(pExistedUidList, &s);
2,831✔
1161
            if (!tmp) {
2,831!
1162
              return terrno;
×
1163
            }
1164
          }
1165
        } else {
1166
          taosArrayDestroy(pTbList);
21✔
1167
          taosHashCleanup(uHash);
21✔
1168
          return -1;
21✔
1169
        }
1170
      } else {
1171
        //        qWarn("failed to get tableIds from by table name: %s, reason: %s", name, tstrerror(terrno));
1172
        terrno = 0;
1,712✔
1173
      }
1174
    }
1175

1176
    taosHashCleanup(uHash);
2,847✔
1177
    taosArrayDestroy(pTbList);
2,847✔
1178
    return 0;
2,847✔
1179
  }
1180

1181
  return -1;
3,826✔
1182
}
1183

1184
SSDataBlock* createTagValBlockForFilter(SArray* pColList, int32_t numOfTables, SArray* pUidTagList, void* pVnode,
114,959✔
1185
                                        SStorageAPI* pStorageAPI) {
1186
  int32_t      code = TSDB_CODE_SUCCESS;
114,959✔
1187
  int32_t      lino = 0;
114,959✔
1188
  SSDataBlock* pResBlock = NULL;
114,959✔
1189
  code = createDataBlock(&pResBlock);
114,959✔
1190
  QUERY_CHECK_CODE(code, lino, _end);
114,924!
1191

1192
  for (int32_t i = 0; i < taosArrayGetSize(pColList); ++i) {
243,650✔
1193
    SColumnInfoData colInfo = {0};
128,659✔
1194
    void* tmp = taosArrayGet(pColList, i);
128,659✔
1195
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
128,611!
1196
    colInfo.info = *(SColumnInfo*)tmp;
128,611✔
1197
    code = blockDataAppendColInfo(pResBlock, &colInfo);
128,611✔
1198
    QUERY_CHECK_CODE(code, lino, _end);
128,726!
1199
  }
1200

1201
  code = blockDataEnsureCapacity(pResBlock, numOfTables);
114,896✔
1202
  if (code != TSDB_CODE_SUCCESS) {
114,939!
1203
    terrno = code;
×
1204
    blockDataDestroy(pResBlock);
×
1205
    return NULL;
×
1206
  }
1207

1208
  pResBlock->info.rows = numOfTables;
114,939✔
1209

1210
  int32_t numOfCols = taosArrayGetSize(pResBlock->pDataBlock);
114,939✔
1211

1212
  for (int32_t i = 0; i < numOfTables; i++) {
670,054✔
1213
    STUidTagInfo* p1 = taosArrayGet(pUidTagList, i);
554,717✔
1214
    QUERY_CHECK_NULL(p1, code, lino, _end, terrno);
554,299✔
1215

1216
    for (int32_t j = 0; j < numOfCols; j++) {
1,151,964✔
1217
      SColumnInfoData* pColInfo = (SColumnInfoData*)taosArrayGet(pResBlock->pDataBlock, j);
596,854✔
1218
      QUERY_CHECK_NULL(pColInfo, code, lino, _end, terrno);
596,330!
1219

1220
      if (pColInfo->info.colId == -1) {  // tbname
596,524✔
1221
        char str[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
19,533✔
1222
        if (p1->name != NULL) {
19,533✔
1223
          STR_TO_VARSTR(str, p1->name);
2,830✔
1224
        } else {  // name is not retrieved during filter
1225
          code = pStorageAPI->metaFn.getTableNameByUid(pVnode, p1->uid, str);
16,703✔
1226
          QUERY_CHECK_CODE(code, lino, _end);
16,769!
1227
        }
1228

1229
        code = colDataSetVal(pColInfo, i, str, false);
19,599✔
1230
        QUERY_CHECK_CODE(code, lino, _end);
19,545!
1231
#if TAG_FILTER_DEBUG
1232
        qDebug("tagfilter uid:%ld, tbname:%s", *uid, str + 2);
1233
#endif
1234
      } else {
1235
        STagVal tagVal = {0};
576,991✔
1236
        tagVal.cid = pColInfo->info.colId;
576,991✔
1237
        if (p1->pTagVal == NULL) {
576,991!
1238
          colDataSetNULL(pColInfo, i);
×
1239
        } else {
1240
          const char* p = pStorageAPI->metaFn.extractTagVal(p1->pTagVal, pColInfo->info.type, &tagVal);
576,991✔
1241

1242
          if (p == NULL || (pColInfo->info.type == TSDB_DATA_TYPE_JSON && ((STag*)p)->nTag == 0)) {
578,667✔
1243
            colDataSetNULL(pColInfo, i);
3,059✔
1244
          } else if (pColInfo->info.type == TSDB_DATA_TYPE_JSON) {
575,608✔
1245
            code = colDataSetVal(pColInfo, i, p, false);
4,860✔
1246
            QUERY_CHECK_CODE(code, lino, _end);
4,860!
1247
          } else if (IS_VAR_DATA_TYPE(pColInfo->info.type)) {
923,570!
1248
            char* tmp = taosMemoryMalloc(tagVal.nData + VARSTR_HEADER_SIZE + 1);
353,461✔
1249
            QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
352,548!
1250
            varDataSetLen(tmp, tagVal.nData);
352,548✔
1251
            memcpy(tmp + VARSTR_HEADER_SIZE, tagVal.pData, tagVal.nData);
352,548✔
1252
            code = colDataSetVal(pColInfo, i, tmp, false);
352,548✔
1253
#if TAG_FILTER_DEBUG
1254
            qDebug("tagfilter varch:%s", tmp + 2);
1255
#endif
1256
            taosMemoryFree(tmp);
352,905!
1257
            QUERY_CHECK_CODE(code, lino, _end);
352,822!
1258
          } else {
1259
            code = colDataSetVal(pColInfo, i, (const char*)&tagVal.i64, false);
217,287✔
1260
            QUERY_CHECK_CODE(code, lino, _end);
217,416!
1261
#if TAG_FILTER_DEBUG
1262
            if (pColInfo->info.type == TSDB_DATA_TYPE_INT) {
1263
              qDebug("tagfilter int:%d", *(int*)(&tagVal.i64));
1264
            } else if (pColInfo->info.type == TSDB_DATA_TYPE_DOUBLE) {
1265
              qDebug("tagfilter double:%f", *(double*)(&tagVal.i64));
1266
            }
1267
#endif
1268
          }
1269
        }
1270
      }
1271
    }
1272
  }
1273

1274
_end:
115,337✔
1275
  if (code != TSDB_CODE_SUCCESS) {
115,337!
1276
    blockDataDestroy(pResBlock);
×
1277
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1278
    terrno = code;
×
1279
    return NULL;
×
1280
  }
1281
  return pResBlock;
115,337✔
1282
}
1283

1284
static int32_t doSetQualifiedUid(STableListInfo* pListInfo, SArray* pUidList, const SArray* pUidTagList,
58,741✔
1285
                                 bool* pResultList, bool addUid) {
1286
  taosArrayClear(pUidList);
58,741✔
1287

1288
  STableKeyInfo info = {.uid = 0, .groupId = 0};
58,792✔
1289
  int32_t       numOfTables = taosArrayGetSize(pUidTagList);
58,792✔
1290
  for (int32_t i = 0; i < numOfTables; ++i) {
406,350✔
1291
    if (pResultList[i]) {
347,353✔
1292
      STUidTagInfo* tmpTag = (STUidTagInfo*)taosArrayGet(pUidTagList, i);
228,851✔
1293
      if (!tmpTag) {
228,797!
1294
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1295
        return terrno;
×
1296
      }
1297
      uint64_t uid = tmpTag->uid;
228,797✔
1298
      qDebug("tagfilter get uid:%" PRId64 ", res:%d", uid, pResultList[i]);
228,797✔
1299

1300
      info.uid = uid;
228,814✔
1301
      void* p = taosArrayPush(pListInfo->pTableList, &info);
228,814✔
1302
      if (p == NULL) {
228,983!
1303
        return terrno;
×
1304
      }
1305

1306
      if (addUid) {
228,983!
1307
        void* tmp = taosArrayPush(pUidList, &uid);
×
1308
        if (tmp == NULL) {
×
1309
          return terrno;
×
1310
        }
1311
      }
1312
    }
1313
  }
1314

1315
  return TSDB_CODE_SUCCESS;
58,997✔
1316
}
1317

1318
static int32_t copyExistedUids(SArray* pUidTagList, const SArray* pUidList) {
59,498✔
1319
  int32_t code = TSDB_CODE_SUCCESS;
59,498✔
1320
  int32_t numOfExisted = taosArrayGetSize(pUidList);
59,498✔
1321
  if (numOfExisted == 0) {
59,535✔
1322
    return code;
56,656✔
1323
  }
1324

1325
  for (int32_t i = 0; i < numOfExisted; ++i) {
5,841✔
1326
    uint64_t*    uid = taosArrayGet(pUidList, i);
2,962✔
1327
    if (!uid) {
2,962!
1328
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1329
      return terrno;
×
1330
    }
1331
    STUidTagInfo info = {.uid = *uid};
2,962✔
1332
    void*        tmp = taosArrayPush(pUidTagList, &info);
2,962✔
1333
    if (!tmp) {
2,962!
1334
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1335
      return code;
×
1336
    }
1337
  }
1338
  return code;
2,879✔
1339
}
1340

1341
static int32_t doFilterByTagCond(STableListInfo* pListInfo, SArray* pUidList, SNode* pTagCond, void* pVnode,
1,068,095✔
1342
                                 SIdxFltStatus status, SStorageAPI* pAPI, bool addUid, bool* listAdded) {
1343
  *listAdded = false;
1,068,095✔
1344
  if (pTagCond == NULL) {
1,068,095✔
1345
    return TSDB_CODE_SUCCESS;
1,008,754✔
1346
  }
1347

1348
  terrno = TSDB_CODE_SUCCESS;
59,341✔
1349

1350
  int32_t      lino = 0;
59,581✔
1351
  int32_t      code = TSDB_CODE_SUCCESS;
59,581✔
1352
  SArray*      pBlockList = NULL;
59,581✔
1353
  SSDataBlock* pResBlock = NULL;
59,581✔
1354
  SScalarParam output = {0};
59,581✔
1355
  SArray*      pUidTagList = NULL;
59,581✔
1356

1357
  tagFilterAssist ctx = {0};
59,581✔
1358
  ctx.colHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
59,581✔
1359
  if (ctx.colHash == NULL) {
59,649!
1360
    code = terrno;
×
1361
    QUERY_CHECK_CODE(code, lino, end);
×
1362
  }
1363

1364
  ctx.cInfoList = taosArrayInit(4, sizeof(SColumnInfo));
59,649✔
1365
  if (ctx.cInfoList == NULL) {
59,675!
1366
    code = terrno;
×
1367
    QUERY_CHECK_CODE(code, lino, end);
×
1368
  }
1369

1370
  nodesRewriteExprPostOrder(&pTagCond, getColumn, (void*)&ctx);
59,675✔
1371
  if (TSDB_CODE_SUCCESS != ctx.code) {
59,630!
1372
    terrno = code = ctx.code;
×
1373
    goto end;
×
1374
  }
1375

1376
  SDataType type = {.type = TSDB_DATA_TYPE_BOOL, .bytes = sizeof(bool)};
59,630✔
1377

1378
  //  int64_t stt = taosGetTimestampUs();
1379
  pUidTagList = taosArrayInit(10, sizeof(STUidTagInfo));
59,630✔
1380
  QUERY_CHECK_NULL(pUidTagList, code, lino, end, terrno);
59,546!
1381

1382
  code = copyExistedUids(pUidTagList, pUidList);
59,546✔
1383
  QUERY_CHECK_CODE(code, lino, end);
59,608!
1384

1385
  FilterCondType condType = checkTagCond(pTagCond);
59,608✔
1386

1387
  int32_t filter = optimizeTbnameInCond(pVnode, pListInfo->idInfo.suid, pUidTagList, pTagCond, pAPI);
59,586✔
1388
  if (filter == 0) {  // tbname in filter is activated, do nothing and return
59,479✔
1389
    taosArrayClear(pUidList);
2,847✔
1390

1391
    int32_t numOfRows = taosArrayGetSize(pUidTagList);
2,847✔
1392
    code = taosArrayEnsureCap(pUidList, numOfRows);
2,847✔
1393
    QUERY_CHECK_CODE(code, lino, end);
2,847!
1394

1395
    for (int32_t i = 0; i < numOfRows; ++i) {
5,678✔
1396
      STUidTagInfo* pInfo = taosArrayGet(pUidTagList, i);
2,831✔
1397
      QUERY_CHECK_NULL(pInfo, code, lino, end, terrno);
2,831!
1398
      void*         tmp = taosArrayPush(pUidList, &pInfo->uid);
2,831✔
1399
      QUERY_CHECK_NULL(tmp, code, lino, end, terrno);
2,831!
1400
    }
1401
    terrno = 0;
2,847✔
1402
  } else {
1403
    if ((condType == FILTER_NO_LOGIC || condType == FILTER_AND) && status != SFLT_NOT_INDEX) {
56,632✔
1404
      code = pAPI->metaFn.getTableTagsByUid(pVnode, pListInfo->idInfo.suid, pUidTagList);
10✔
1405
    } else {
1406
      code = pAPI->metaFn.getTableTags(pVnode, pListInfo->idInfo.suid, pUidTagList);
56,622✔
1407
    }
1408
    if (code != TSDB_CODE_SUCCESS) {
56,833!
1409
      qError("failed to get table tags from meta, reason:%s, suid:%" PRIu64, tstrerror(code), pListInfo->idInfo.suid);
×
1410
      terrno = code;
×
1411
      QUERY_CHECK_CODE(code, lino, end);
×
1412
    }
1413
  }
1414

1415
  int32_t numOfTables = taosArrayGetSize(pUidTagList);
59,680✔
1416
  if (numOfTables == 0) {
59,665✔
1417
    goto end;
615✔
1418
  }
1419

1420
  pResBlock = createTagValBlockForFilter(ctx.cInfoList, numOfTables, pUidTagList, pVnode, pAPI);
59,050✔
1421
  if (pResBlock == NULL) {
58,980!
1422
    code = terrno;
×
1423
    QUERY_CHECK_CODE(code, lino, end);
×
1424
  }
1425

1426
  //  int64_t st1 = taosGetTimestampUs();
1427
  //  qDebug("generate tag block rows:%d, cost:%ld us", rows, st1-st);
1428
  pBlockList = taosArrayInit(2, POINTER_BYTES);
58,980✔
1429
  QUERY_CHECK_NULL(pBlockList, code, lino, end, terrno);
59,056!
1430

1431
  void* tmp = taosArrayPush(pBlockList, &pResBlock);
59,039✔
1432
  QUERY_CHECK_NULL(tmp, code, lino, end, terrno);
59,039!
1433

1434
  code = createResultData(&type, numOfTables, &output);
59,039✔
1435
  if (code != TSDB_CODE_SUCCESS) {
59,059!
1436
    terrno = code;
×
1437
    QUERY_CHECK_CODE(code, lino, end);
×
1438
  }
1439

1440
  code = scalarCalculate(pTagCond, pBlockList, &output);
59,059✔
1441
  if (code != TSDB_CODE_SUCCESS) {
58,916✔
1442
    qError("failed to calculate scalar, reason:%s", tstrerror(code));
8!
1443
    terrno = code;
8✔
1444
    QUERY_CHECK_CODE(code, lino, end);
8!
1445
  }
1446

1447
  code = doSetQualifiedUid(pListInfo, pUidList, pUidTagList, (bool*)output.columnData->pData, addUid);
58,908✔
1448
  if (code != TSDB_CODE_SUCCESS) {
58,951✔
1449
    terrno = code;
27✔
1450
    QUERY_CHECK_CODE(code, lino, end);
×
1451
  }
1452
  *listAdded = true;
58,924✔
1453

1454
end:
59,547✔
1455
  if (code != TSDB_CODE_SUCCESS) {
59,547✔
1456
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
8!
1457
  }
1458
  taosHashCleanup(ctx.colHash);
59,547✔
1459
  taosArrayDestroy(ctx.cInfoList);
59,630✔
1460
  blockDataDestroy(pResBlock);
59,671✔
1461
  taosArrayDestroy(pBlockList);
59,667✔
1462
  taosArrayDestroyEx(pUidTagList, freeItem);
59,685✔
1463

1464
  colDataDestroy(output.columnData);
59,685✔
1465
  taosMemoryFreeClear(output.columnData);
59,693!
1466
  return code;
59,690✔
1467
}
1468

1469
int32_t getTableList(void* pVnode, SScanPhysiNode* pScanNode, SNode* pTagCond, SNode* pTagIndexCond,
1,066,457✔
1470
                     STableListInfo* pListInfo, uint8_t* digest, const char* idstr, SStorageAPI* pStorageAPI) {
1471
  int32_t code = TSDB_CODE_SUCCESS;
1,066,457✔
1472
  int32_t lino = 0;
1,066,457✔
1473
  size_t  numOfTables = 0;
1,066,457✔
1474
  bool    listAdded = false;
1,066,457✔
1475

1476
  pListInfo->idInfo.suid = pScanNode->suid;
1,066,457✔
1477
  pListInfo->idInfo.tableType = pScanNode->tableType;
1,066,457✔
1478

1479
  SArray* pUidList = taosArrayInit(8, sizeof(uint64_t));
1,066,457✔
1480
  QUERY_CHECK_NULL(pUidList, code, lino, _error, terrno);
1,068,516✔
1481

1482
  SIdxFltStatus status = SFLT_NOT_INDEX;
1,067,926✔
1483
  if (pScanNode->tableType != TSDB_SUPER_TABLE) {
1,067,926✔
1484
    pListInfo->idInfo.uid = pScanNode->uid;
444,503✔
1485
    if (pStorageAPI->metaFn.isTableExisted(pVnode, pScanNode->uid)) {
444,503✔
1486
      void* tmp = taosArrayPush(pUidList, &pScanNode->uid);
443,770✔
1487
      QUERY_CHECK_NULL(tmp, code, lino, _error, terrno);
443,764!
1488
    }
1489
    code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, status, pStorageAPI, false, &listAdded);
444,513✔
1490
    QUERY_CHECK_CODE(code, lino, _end);
444,507!
1491
  } else {
1492
    T_MD5_CTX context = {0};
623,423✔
1493

1494
    if (tsTagFilterCache) {
623,423!
1495
      // try to retrieve the result from meta cache
1496
      code = genTagFilterDigest(pTagCond, &context);
×
1497
      QUERY_CHECK_CODE(code, lino, _error);
×
1498

1499
      bool acquired = false;
×
1500
      code = pStorageAPI->metaFn.getCachedTableList(pVnode, pScanNode->suid, context.digest, tListLen(context.digest),
×
1501
                                                    pUidList, &acquired);
1502
      QUERY_CHECK_CODE(code, lino, _error);
×
1503

1504
      if (acquired) {
×
1505
        digest[0] = 1;
×
1506
        memcpy(digest + 1, context.digest, tListLen(context.digest));
×
1507
        qDebug("retrieve table uid list from cache, numOfTables:%d", (int32_t)taosArrayGetSize(pUidList));
×
1508
        goto _end;
×
1509
      }
1510
    }
1511

1512
    if (!pTagCond) {  // no tag filter condition exists, let's fetch all tables of this super table
623,423✔
1513
      code = pStorageAPI->metaFn.getChildTableList(pVnode, pScanNode->suid, pUidList);
566,054✔
1514
      QUERY_CHECK_CODE(code, lino, _error);
567,384!
1515
    } else {
1516
      // failed to find the result in the cache, let try to calculate the results
1517
      if (pTagIndexCond) {
57,369✔
1518
        void* pIndex = pStorageAPI->metaFn.getInvertIndex(pVnode);
8,533✔
1519

1520
        SIndexMetaArg metaArg = {.metaEx = pVnode,
17,061✔
1521
                                 .idx = pStorageAPI->metaFn.storeGetIndexInfo(pVnode),
8,532✔
1522
                                 .ivtIdx = pIndex,
1523
                                 .suid = pScanNode->uid};
8,529✔
1524

1525
        status = SFLT_NOT_INDEX;
8,529✔
1526
        code = doFilterTag(pTagIndexCond, &metaArg, pUidList, &status, &pStorageAPI->metaFilter);
8,529✔
1527
        if (code != 0 || status == SFLT_NOT_INDEX) {  // temporarily disable it for performance sake
8,529✔
1528
          qDebug("failed to get tableIds from index, suid:%" PRIu64, pScanNode->uid);
8,511✔
1529
        } else {
1530
          qDebug("succ to get filter result, table num: %d", (int)taosArrayGetSize(pUidList));
18!
1531
        }
1532
      }
1533
    }
1534

1535
    code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, status, pStorageAPI, tsTagFilterCache, &listAdded);
624,752✔
1536
    QUERY_CHECK_CODE(code, lino, _end);
623,799✔
1537

1538
    // let's add the filter results into meta-cache
1539
    numOfTables = taosArrayGetSize(pUidList);
623,791✔
1540

1541
    if (tsTagFilterCache) {
623,756✔
1542
      size_t size = numOfTables * sizeof(uint64_t) + sizeof(int32_t);
46✔
1543
      char*  pPayload = taosMemoryMalloc(size);
46!
1544
      QUERY_CHECK_NULL(pPayload, code, lino, _end, terrno);
×
1545

1546
      *(int32_t*)pPayload = numOfTables;
×
1547
      if (numOfTables > 0) {
×
1548
        void* tmp = taosArrayGet(pUidList, 0);
×
1549
        QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1550
        memcpy(pPayload + sizeof(int32_t), tmp, numOfTables * sizeof(uint64_t));
×
1551
      }
1552

1553
      code = pStorageAPI->metaFn.putCachedTableList(pVnode, pScanNode->suid, context.digest, tListLen(context.digest),
×
1554
                                                    pPayload, size, 1);
1555
      QUERY_CHECK_CODE(code, lino, _error);
×
1556

1557
      digest[0] = 1;
×
1558
      memcpy(digest + 1, context.digest, tListLen(context.digest));
×
1559
    }
1560
  }
1561

1562
_end:
1,068,225✔
1563
  if (!listAdded) {
1,068,225✔
1564
    numOfTables = taosArrayGetSize(pUidList);
1,009,361✔
1565
    for (int i = 0; i < numOfTables; i++) {
3,794,065✔
1566
      void* tmp = taosArrayGet(pUidList, i);
2,784,152✔
1567
      QUERY_CHECK_NULL(tmp, code, lino, _error, terrno);
2,782,729!
1568
      STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
2,782,729✔
1569

1570
      void* p = taosArrayPush(pListInfo->pTableList, &info);
2,782,729✔
1571
      if (p == NULL) {
2,784,533!
1572
        taosArrayDestroy(pUidList);
×
1573
        return terrno;
×
1574
      }
1575

1576
      qTrace("tagfilter get uid:%" PRIu64 ", %s", info.uid, idstr);
2,784,533✔
1577
    }
1578
  }
1579

1580
_error:
1,068,777✔
1581
  taosArrayDestroy(pUidList);
1,068,777✔
1582
  if (code != TSDB_CODE_SUCCESS) {
1,068,638✔
1583
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
8!
1584
  }
1585
  return code;
1,068,236✔
1586
}
1587

1588
int32_t qGetTableList(int64_t suid, void* pVnode, void* node, SArray** tableList, void* pTaskInfo) {
80✔
1589
  int32_t        code = TSDB_CODE_SUCCESS;
80✔
1590
  int32_t        lino = 0;
80✔
1591
  SSubplan*      pSubplan = (SSubplan*)node;
80✔
1592
  SScanPhysiNode pNode = {0};
80✔
1593
  pNode.suid = suid;
80✔
1594
  pNode.uid = suid;
80✔
1595
  pNode.tableType = TSDB_SUPER_TABLE;
80✔
1596

1597
  STableListInfo* pTableListInfo = tableListCreate();
80✔
1598
  QUERY_CHECK_NULL(pTableListInfo, code, lino, _end, terrno);
81!
1599
  uint8_t         digest[17] = {0};
81✔
1600
  code =
1601
      getTableList(pVnode, &pNode, pSubplan ? pSubplan->pTagCond : NULL, pSubplan ? pSubplan->pTagIndexCond : NULL,
81✔
1602
                   pTableListInfo, digest, "qGetTableList", &((SExecTaskInfo*)pTaskInfo)->storageAPI);
1603
  QUERY_CHECK_CODE(code, lino, _end);
80!
1604
  *tableList = pTableListInfo->pTableList;
80✔
1605
  pTableListInfo->pTableList = NULL;
80✔
1606
  tableListDestroy(pTableListInfo);
80✔
1607

1608
_end:
81✔
1609
  if (code != TSDB_CODE_SUCCESS) {
81!
1610
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1611
  }
1612
  return code;
81✔
1613
}
1614

1615
size_t getTableTagsBufLen(const SNodeList* pGroups) {
7,472✔
1616
  size_t keyLen = 0;
7,472✔
1617

1618
  SNode* node;
1619
  FOREACH(node, pGroups) {
49,007!
1620
    SExprNode* pExpr = (SExprNode*)node;
41,535✔
1621
    keyLen += pExpr->resType.bytes;
41,535✔
1622
  }
1623

1624
  keyLen += sizeof(int8_t) * LIST_LENGTH(pGroups);
7,472!
1625
  return keyLen;
7,472✔
1626
}
1627

1628
int32_t getGroupIdFromTagsVal(void* pVnode, uint64_t uid, SNodeList* pGroupNode, char* keyBuf, uint64_t* pGroupId,
6✔
1629
                              SStorageAPI* pAPI) {
1630
  SMetaReader mr = {0};
6✔
1631

1632
  pAPI->metaReaderFn.initReader(&mr, pVnode, META_READER_LOCK, &pAPI->metaFn);
6✔
1633
  if (pAPI->metaReaderFn.getEntryGetUidCache(&mr, uid) != 0) {  // table not exist
6!
1634
    pAPI->metaReaderFn.clearReader(&mr);
×
1635
    return TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
1636
  }
1637

1638
  SNodeList* groupNew = NULL;
6✔
1639
  int32_t    code = nodesCloneList(pGroupNode, &groupNew);
6✔
1640
  if (TSDB_CODE_SUCCESS != code) {
6!
1641
    pAPI->metaReaderFn.clearReader(&mr);
×
1642
    return code;
×
1643
  }
1644

1645
  STransTagExprCtx ctx = {.code = 0, .pReader = &mr};
6✔
1646
  nodesRewriteExprsPostOrder(groupNew, doTranslateTagExpr, &ctx);
6✔
1647
  if (TSDB_CODE_SUCCESS != ctx.code) {
6!
1648
    nodesDestroyList(groupNew);
×
1649
    pAPI->metaReaderFn.clearReader(&mr);
×
1650
    return code;
×
1651
  }
1652
  char* isNull = (char*)keyBuf;
6✔
1653
  char* pStart = (char*)keyBuf + sizeof(int8_t) * LIST_LENGTH(pGroupNode);
6!
1654

1655
  SNode*  pNode;
1656
  int32_t index = 0;
6✔
1657
  FOREACH(pNode, groupNew) {
12!
1658
    SNode*  pNew = NULL;
6✔
1659
    int32_t code = scalarCalculateConstants(pNode, &pNew);
6✔
1660
    if (TSDB_CODE_SUCCESS == code) {
6!
1661
      REPLACE_NODE(pNew);
6✔
1662
    } else {
1663
      nodesDestroyList(groupNew);
×
1664
      pAPI->metaReaderFn.clearReader(&mr);
×
1665
      return code;
×
1666
    }
1667

1668
    if (nodeType(pNew) != QUERY_NODE_VALUE) {
6!
1669
      nodesDestroyList(groupNew);
×
1670
      pAPI->metaReaderFn.clearReader(&mr);
×
1671
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR));
×
1672
      return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1673
    }
1674
    SValueNode* pValue = (SValueNode*)pNew;
6✔
1675

1676
    if (pValue->node.resType.type == TSDB_DATA_TYPE_NULL || pValue->isNull) {
6!
1677
      isNull[index++] = 1;
×
1678
      continue;
×
1679
    } else {
1680
      isNull[index++] = 0;
6✔
1681
      char* data = nodesGetValueFromNode(pValue);
6✔
1682
      if (pValue->node.resType.type == TSDB_DATA_TYPE_JSON) {
6!
1683
        if (tTagIsJson(data)) {
×
1684
          terrno = TSDB_CODE_QRY_JSON_IN_GROUP_ERROR;
×
1685
          nodesDestroyList(groupNew);
×
1686
          pAPI->metaReaderFn.clearReader(&mr);
×
1687
          return terrno;
×
1688
        }
1689
        int32_t len = getJsonValueLen(data);
×
1690
        memcpy(pStart, data, len);
×
1691
        pStart += len;
×
1692
      } else if (IS_VAR_DATA_TYPE(pValue->node.resType.type)) {
6!
1693
        memcpy(pStart, data, varDataTLen(data));
2✔
1694
        pStart += varDataTLen(data);
2✔
1695
      } else {
1696
        memcpy(pStart, data, pValue->node.resType.bytes);
4✔
1697
        pStart += pValue->node.resType.bytes;
4✔
1698
      }
1699
    }
1700
  }
1701

1702
  int32_t len = (int32_t)(pStart - (char*)keyBuf);
6✔
1703
  *pGroupId = calcGroupId(keyBuf, len);
6✔
1704

1705
  nodesDestroyList(groupNew);
6✔
1706
  pAPI->metaReaderFn.clearReader(&mr);
6✔
1707
  
1708
  return TSDB_CODE_SUCCESS;
6✔
1709
}
1710

1711
SArray* makeColumnArrayFromList(SNodeList* pNodeList) {
16,877✔
1712
  if (!pNodeList) {
16,877!
1713
    return NULL;
×
1714
  }
1715

1716
  size_t  numOfCols = LIST_LENGTH(pNodeList);
16,877!
1717
  SArray* pList = taosArrayInit(numOfCols, sizeof(SColumn));
16,877✔
1718
  if (pList == NULL) {
16,879!
1719
    return NULL;
×
1720
  }
1721

1722
  for (int32_t i = 0; i < numOfCols; ++i) {
49,975✔
1723
    SColumnNode* pColNode = (SColumnNode*)nodesListGetNode(pNodeList, i);
33,096✔
1724
    if (!pColNode) {
33,094!
1725
      taosArrayDestroy(pList);
×
1726
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR));
×
1727
      return NULL;
×
1728
    }
1729

1730
    // todo extract method
1731
    SColumn c = {0};
33,094✔
1732
    c.slotId = pColNode->slotId;
33,094✔
1733
    c.colId = pColNode->colId;
33,094✔
1734
    c.type = pColNode->node.resType.type;
33,094✔
1735
    c.bytes = pColNode->node.resType.bytes;
33,094✔
1736
    c.precision = pColNode->node.resType.precision;
33,094✔
1737
    c.scale = pColNode->node.resType.scale;
33,094✔
1738

1739
    void* tmp = taosArrayPush(pList, &c);
33,096✔
1740
    if (!tmp) {
33,096!
1741
      taosArrayDestroy(pList);
×
1742
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1743
      return NULL;
×
1744
    }
1745
  }
1746

1747
  return pList;
16,879✔
1748
}
1749

1750
int32_t extractColMatchInfo(SNodeList* pNodeList, SDataBlockDescNode* pOutputNodeList, int32_t* numOfOutputCols,
1,483,215✔
1751
                            int32_t type, SColMatchInfo* pMatchInfo) {
1752
  size_t  numOfCols = LIST_LENGTH(pNodeList);
1,483,215!
1753
  int32_t code = TSDB_CODE_SUCCESS;
1,483,215✔
1754
  int32_t lino = 0;
1,483,215✔
1755

1756
  pMatchInfo->matchType = type;
1,483,215✔
1757

1758
  SArray* pList = taosArrayInit(numOfCols, sizeof(SColMatchItem));
1,483,215✔
1759
  if (pList == NULL) {
1,483,962!
UNCOV
1760
    code = terrno;
×
1761
    return code;
×
1762
  }
1763

1764
  for (int32_t i = 0; i < numOfCols; ++i) {
6,154,447✔
1765
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pNodeList, i);
4,669,648✔
1766
    QUERY_CHECK_NULL(pNode, code, lino, _end, terrno);
4,668,714!
1767
    if (nodeType(pNode->pExpr) == QUERY_NODE_COLUMN) {
4,668,950✔
1768
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
4,580,232✔
1769

1770
      SColMatchItem c = {.needOutput = true};
4,580,232✔
1771
      c.colId = pColNode->colId;
4,580,232✔
1772
      c.srcSlotId = pColNode->slotId;
4,580,232✔
1773
      c.dstSlotId = pNode->slotId;
4,580,232✔
1774
      c.isPk = pColNode->isPk;
4,580,232✔
1775
      c.dataType = pColNode->node.resType;
4,580,232✔
1776
      void* tmp = taosArrayPush(pList, &c);
4,581,728✔
1777
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
4,581,728!
1778
    }
1779
  }
1780

1781
  // set the output flag for each column in SColMatchInfo, according to the
1782
  *numOfOutputCols = 0;
1,484,799✔
1783
  int32_t num = LIST_LENGTH(pOutputNodeList->pSlots);
1,484,799✔
1784
  for (int32_t i = 0; i < num; ++i) {
7,044,650✔
1785
    SSlotDescNode* pNode = (SSlotDescNode*)nodesListGetNode(pOutputNodeList->pSlots, i);
5,558,477✔
1786
    QUERY_CHECK_NULL(pNode, code, lino, _end, terrno);
5,557,454!
1787

1788
    // todo: add reserve flag check
1789
    // it is a column reserved for the arithmetic expression calculation
1790
    if (pNode->slotId >= numOfCols) {
5,559,353✔
1791
      (*numOfOutputCols) += 1;
891,778✔
1792
      continue;
891,778✔
1793
    }
1794

1795
    SColMatchItem* info = NULL;
4,667,575✔
1796
    for (int32_t j = 0; j < taosArrayGetSize(pList); ++j) {
665,539,164✔
1797
      info = taosArrayGet(pList, j);
666,976,058✔
1798
      QUERY_CHECK_NULL(info, code, lino, _end, terrno);
669,053,812✔
1799
      if (info->dstSlotId == pNode->slotId) {
665,447,813✔
1800
        break;
4,576,224✔
1801
      }
1802
    }
1803

1804
    if (pNode->output) {
4,668,073✔
1805
      (*numOfOutputCols) += 1;
4,597,228✔
1806
    } else if (info != NULL) {
70,845!
1807
      // select distinct tbname from stb where tbname='abc';
1808
      info->needOutput = false;
70,846✔
1809
    }
1810
  }
1811

1812
  pMatchInfo->pList = pList;
1,486,173✔
1813

1814
_end:
1,486,173✔
1815
  if (code != TSDB_CODE_SUCCESS) {
1,486,173!
1816
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1817
  }
1818
  return code;
1,483,936✔
1819
}
1820

1821
static SResSchema createResSchema(int32_t type, int32_t bytes, int32_t slotId, int32_t scale, int32_t precision,
4,590,369✔
1822
                                  const char* name) {
1823
  SResSchema s = {0};
4,590,369✔
1824
  s.scale = scale;
4,590,369✔
1825
  s.type = type;
4,590,369✔
1826
  s.bytes = bytes;
4,590,369✔
1827
  s.slotId = slotId;
4,590,369✔
1828
  s.precision = precision;
4,590,369✔
1829
  tstrncpy(s.name, name, tListLen(s.name));
4,590,369✔
1830

1831
  return s;
4,590,369✔
1832
}
1833

1834
static SColumn* createColumn(int32_t blockId, int32_t slotId, int32_t colId, SDataType* pType, EColumnType colType) {
3,847,191✔
1835
  SColumn* pCol = taosMemoryCalloc(1, sizeof(SColumn));
3,847,191!
1836
  if (pCol == NULL) {
3,847,210!
1837
    return NULL;
×
1838
  }
1839

1840
  pCol->slotId = slotId;
3,847,210✔
1841
  pCol->colId = colId;
3,847,210✔
1842
  pCol->bytes = pType->bytes;
3,847,210✔
1843
  pCol->type = pType->type;
3,847,210✔
1844
  pCol->scale = pType->scale;
3,847,210✔
1845
  pCol->precision = pType->precision;
3,847,210✔
1846
  pCol->dataBlockId = blockId;
3,847,210✔
1847
  pCol->colType = colType;
3,847,210✔
1848
  return pCol;
3,847,210✔
1849
}
1850

1851
int32_t createExprFromOneNode(SExprInfo* pExp, SNode* pNode, int16_t slotId) {
4,587,088✔
1852
  int32_t code = TSDB_CODE_SUCCESS;
4,587,088✔
1853
  int32_t lino = 0;
4,587,088✔
1854
  pExp->base.numOfParams = 0;
4,587,088✔
1855
  pExp->base.pParam = NULL;
4,587,088✔
1856
  pExp->pExpr = taosMemoryCalloc(1, sizeof(tExprNode));
4,587,088!
1857
  QUERY_CHECK_NULL(pExp->pExpr, code, lino, _end, terrno);
4,592,125!
1858

1859
  pExp->pExpr->_function.num = 1;
4,592,125✔
1860
  pExp->pExpr->_function.functionId = -1;
4,592,125✔
1861

1862
  int32_t type = nodeType(pNode);
4,592,125✔
1863
  // it is a project query, or group by column
1864
  if (type == QUERY_NODE_COLUMN) {
4,592,125✔
1865
    pExp->pExpr->nodeType = QUERY_NODE_COLUMN;
1,987,076✔
1866
    SColumnNode* pColNode = (SColumnNode*)pNode;
1,987,076✔
1867

1868
    pExp->base.pParam = taosMemoryCalloc(1, sizeof(SFunctParam));
1,987,076!
1869
    QUERY_CHECK_NULL(pExp->base.pParam, code, lino, _end, terrno);
1,986,844!
1870

1871
    pExp->base.numOfParams = 1;
1,986,844✔
1872

1873
    SDataType* pType = &pColNode->node.resType;
1,986,844✔
1874
    pExp->base.resSchema =
1875
        createResSchema(pType->type, pType->bytes, slotId, pType->scale, pType->precision, pColNode->colName);
1,986,844✔
1876

1877
    pExp->base.pParam[0].pCol =
3,973,967✔
1878
        createColumn(pColNode->dataBlockId, pColNode->slotId, pColNode->colId, pType, pColNode->colType);
1,987,155✔
1879
    QUERY_CHECK_NULL(pExp->base.pParam[0].pCol, code, lino, _end, terrno);
1,986,812!
1880

1881
    pExp->base.pParam[0].type = FUNC_PARAM_TYPE_COLUMN;
1,986,812✔
1882
  } else if (type == QUERY_NODE_VALUE) {
2,605,049✔
1883
    pExp->pExpr->nodeType = QUERY_NODE_VALUE;
143,458✔
1884
    SValueNode* pValNode = (SValueNode*)pNode;
143,458✔
1885

1886
    pExp->base.pParam = taosMemoryCalloc(1, sizeof(SFunctParam));
143,458!
1887
    QUERY_CHECK_NULL(pExp->base.pParam, code, lino, _end, terrno);
143,551!
1888

1889
    pExp->base.numOfParams = 1;
143,551✔
1890

1891
    SDataType* pType = &pValNode->node.resType;
143,551✔
1892
    pExp->base.resSchema =
1893
        createResSchema(pType->type, pType->bytes, slotId, pType->scale, pType->precision, pValNode->node.aliasName);
143,551✔
1894
    pExp->base.pParam[0].type = FUNC_PARAM_TYPE_VALUE;
143,513✔
1895
    code = nodesValueNodeToVariant(pValNode, &pExp->base.pParam[0].param);
143,513✔
1896
    QUERY_CHECK_CODE(code, lino, _end);
143,479!
1897
  } else if (type == QUERY_NODE_FUNCTION) {
2,461,591✔
1898
    pExp->pExpr->nodeType = QUERY_NODE_FUNCTION;
2,314,453✔
1899
    SFunctionNode* pFuncNode = (SFunctionNode*)pNode;
2,314,453✔
1900

1901
    SDataType* pType = &pFuncNode->node.resType;
2,314,453✔
1902
    pExp->base.resSchema =
1903
        createResSchema(pType->type, pType->bytes, slotId, pType->scale, pType->precision, pFuncNode->node.aliasName);
2,314,453✔
1904
    tExprNode* pExprNode = pExp->pExpr;
2,314,914✔
1905

1906
    pExprNode->_function.functionId = pFuncNode->funcId;
2,314,914✔
1907
    pExprNode->_function.pFunctNode = pFuncNode;
2,314,914✔
1908
    pExprNode->_function.functionType = pFuncNode->funcType;
2,314,914✔
1909

1910
    tstrncpy(pExprNode->_function.functionName, pFuncNode->functionName, tListLen(pExprNode->_function.functionName));
2,314,914✔
1911

1912
#if 1
1913
    // todo refactor: add the parameter for tbname function
1914
    const char* name = "tbname";
2,314,914✔
1915
    int32_t     len = strlen(name);
2,314,914✔
1916

1917
    if (!pFuncNode->pParameterList && (memcmp(pExprNode->_function.functionName, name, len) == 0) &&
2,314,914✔
1918
        pExprNode->_function.functionName[len] == 0) {
220,616!
1919
      pFuncNode->pParameterList = NULL;
220,727✔
1920
      int32_t     code = nodesMakeList(&pFuncNode->pParameterList);
220,727✔
1921
      SValueNode* res = NULL;
220,734✔
1922
      if (TSDB_CODE_SUCCESS == code) {
220,734!
1923
        code = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&res);
220,738✔
1924
      }
1925
      QUERY_CHECK_CODE(code, lino, _end);
220,610!
1926
      res->node.resType = (SDataType){.bytes = sizeof(int64_t), .type = TSDB_DATA_TYPE_BIGINT};
220,610✔
1927
      code = nodesListAppend(pFuncNode->pParameterList, (SNode*)res);
220,610✔
1928
      if (code != TSDB_CODE_SUCCESS) {
220,741!
UNCOV
1929
        nodesDestroyNode((SNode*)res);
×
1930
        res = NULL;
×
1931
      }
1932
      QUERY_CHECK_CODE(code, lino, _end);
220,742!
1933
    }
1934
#endif
1935

1936
    int32_t numOfParam = LIST_LENGTH(pFuncNode->pParameterList);
2,314,929✔
1937

1938
    pExp->base.pParam = taosMemoryCalloc(numOfParam, sizeof(SFunctParam));
2,314,929✔
1939
    QUERY_CHECK_NULL(pExp->base.pParam, code, lino, _end, terrno);
2,315,086!
1940
    pExp->base.numOfParams = numOfParam;
2,315,086✔
1941

1942
    for (int32_t j = 0; j < numOfParam && TSDB_CODE_SUCCESS == code; ++j) {
5,323,166!
1943
      SNode* p1 = nodesListGetNode(pFuncNode->pParameterList, j);
3,008,462✔
1944
      QUERY_CHECK_NULL(p1, code, lino, _end, terrno);
3,008,211!
1945
      if (p1->type == QUERY_NODE_COLUMN) {
3,008,466✔
1946
        SColumnNode* pcn = (SColumnNode*)p1;
1,860,518✔
1947

1948
        pExp->base.pParam[j].type = FUNC_PARAM_TYPE_COLUMN;
1,860,518✔
1949
        pExp->base.pParam[j].pCol =
3,721,175✔
1950
            createColumn(pcn->dataBlockId, pcn->slotId, pcn->colId, &pcn->node.resType, pcn->colType);
1,860,518✔
1951
        QUERY_CHECK_NULL(pExp->base.pParam[j].pCol, code, lino, _end, terrno);
1,860,657✔
1952
      } else if (p1->type == QUERY_NODE_VALUE) {
1,147,948✔
1953
        SValueNode* pvn = (SValueNode*)p1;
727,151✔
1954
        pExp->base.pParam[j].type = FUNC_PARAM_TYPE_VALUE;
727,151✔
1955
        code = nodesValueNodeToVariant(pvn, &pExp->base.pParam[j].param);
727,151✔
1956
        QUERY_CHECK_CODE(code, lino, _end);
726,941!
1957
      }
1958
    }
1959
  } else if (type == QUERY_NODE_OPERATOR) {
147,138✔
1960
    pExp->pExpr->nodeType = QUERY_NODE_OPERATOR;
140,554✔
1961
    SOperatorNode* pOpNode = (SOperatorNode*)pNode;
140,554✔
1962

1963
    pExp->base.pParam = taosMemoryCalloc(1, sizeof(SFunctParam));
140,554!
1964
    QUERY_CHECK_NULL(pExp->base.pParam, code, lino, _end, terrno);
140,602!
1965
    pExp->base.numOfParams = 1;
140,602✔
1966

1967
    SDataType* pType = &pOpNode->node.resType;
140,602✔
1968
    pExp->base.resSchema =
1969
        createResSchema(pType->type, pType->bytes, slotId, pType->scale, pType->precision, pOpNode->node.aliasName);
140,602✔
1970
    pExp->pExpr->_optrRoot.pRootNode = pNode;
140,557✔
1971
  } else if (type == QUERY_NODE_CASE_WHEN) {
6,584✔
1972
    pExp->pExpr->nodeType = QUERY_NODE_OPERATOR;
6,178✔
1973
    SCaseWhenNode* pCaseNode = (SCaseWhenNode*)pNode;
6,178✔
1974

1975
    pExp->base.pParam = taosMemoryCalloc(1, sizeof(SFunctParam));
6,178!
1976
    QUERY_CHECK_NULL(pExp->base.pParam, code, lino, _end, terrno);
6,179!
1977
    pExp->base.numOfParams = 1;
6,179✔
1978

1979
    SDataType* pType = &pCaseNode->node.resType;
6,179✔
1980
    pExp->base.resSchema =
1981
        createResSchema(pType->type, pType->bytes, slotId, pType->scale, pType->precision, pCaseNode->node.aliasName);
6,179✔
1982
    pExp->pExpr->_optrRoot.pRootNode = pNode;
6,179✔
1983
  } else if (type == QUERY_NODE_LOGIC_CONDITION) {
406✔
1984
    pExp->pExpr->nodeType = QUERY_NODE_OPERATOR;
2✔
1985
    SLogicConditionNode* pCond = (SLogicConditionNode*)pNode;
2✔
1986
    pExp->base.pParam = taosMemoryCalloc(1, sizeof(SFunctParam));
2!
1987
    QUERY_CHECK_NULL(pExp->base.pParam, code, lino, _end, terrno);
2!
1988
    pExp->base.numOfParams = 1;
2✔
1989
    SDataType* pType = &pCond->node.resType;
2✔
1990
    pExp->base.resSchema = createResSchema(pType->type, pType->bytes, slotId, pType->scale, pType->precision, pCond->node.aliasName);
2✔
1991
    pExp->pExpr->_optrRoot.pRootNode = pNode;
2✔
1992
  } else {
1993
    code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
404✔
1994
    QUERY_CHECK_CODE(code, lino, _end);
404!
1995
  }
1996

1997
_end:
404✔
1998
  if (code != TSDB_CODE_SUCCESS) {
4,592,137!
1999
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
2000
  }
2001
  return code;
4,590,194✔
2002
}
2003

2004
int32_t createExprFromTargetNode(SExprInfo* pExp, STargetNode* pTargetNode) {
4,569,093✔
2005
  return createExprFromOneNode(pExp, pTargetNode->pExpr, pTargetNode->slotId);
4,569,093✔
2006
}
2007

2008
SExprInfo* createExpr(SNodeList* pNodeList, int32_t* numOfExprs) {
2,252✔
2009
  *numOfExprs = LIST_LENGTH(pNodeList);
2,252!
2010
  SExprInfo* pExprs = taosMemoryCalloc(*numOfExprs, sizeof(SExprInfo));
2,252!
2011
  if (!pExprs) {
2,252!
2012
    return NULL;
×
2013
  }
2014

2015
  for (int32_t i = 0; i < (*numOfExprs); ++i) {
16,832✔
2016
    SExprInfo* pExp = &pExprs[i];
14,580✔
2017
    int32_t    code = createExprFromOneNode(pExp, nodesListGetNode(pNodeList, i), i + UD_TAG_COLUMN_INDEX);
14,580✔
2018
    if (code != TSDB_CODE_SUCCESS) {
14,580!
2019
      taosMemoryFreeClear(pExprs);
×
2020
      terrno = code;
×
2021
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
2022
      return NULL;
×
2023
    }
2024
  }
2025

2026
  return pExprs;
2,252✔
2027
}
2028

2029
int32_t createExprInfo(SNodeList* pNodeList, SNodeList* pGroupKeys, SExprInfo** pExprInfo, int32_t* numOfExprs) {
2,206,162✔
2030
  QRY_PARAM_CHECK(pExprInfo);
2,206,162!
2031

2032
  int32_t code = 0;
2,206,162✔
2033
  int32_t numOfFuncs = LIST_LENGTH(pNodeList);
2,206,162✔
2034
  int32_t numOfGroupKeys = 0;
2,206,162✔
2035
  if (pGroupKeys != NULL) {
2,206,162✔
2036
    numOfGroupKeys = LIST_LENGTH(pGroupKeys);
215,513!
2037
  }
2038

2039
  *numOfExprs = numOfFuncs + numOfGroupKeys;
2,206,162✔
2040
  if (*numOfExprs == 0) {
2,206,162✔
2041
    return code;
198,958✔
2042
  }
2043

2044
  SExprInfo* pExprs = taosMemoryCalloc(*numOfExprs, sizeof(SExprInfo));
2,007,204!
2045
  if (pExprs == NULL) {
2,008,130!
2046
    return terrno;
×
2047
  }
2048

2049
  for (int32_t i = 0; i < (*numOfExprs); ++i) {
6,580,047✔
2050
    STargetNode* pTargetNode = NULL;
4,572,052✔
2051
    if (i < numOfFuncs) {
4,572,052✔
2052
      pTargetNode = (STargetNode*)nodesListGetNode(pNodeList, i);
4,314,952✔
2053
    } else {
2054
      pTargetNode = (STargetNode*)nodesListGetNode(pGroupKeys, i - numOfFuncs);
257,100✔
2055
    }
2056
    if (!pTargetNode) {
4,569,011!
2057
      destroyExprInfo(pExprs, *numOfExprs);
×
2058
      taosMemoryFreeClear(pExprs);
×
2059
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
2060
      return terrno;
×
2061
    }
2062

2063
    SExprInfo* pExp = &pExprs[i];
4,569,011✔
2064
    code = createExprFromTargetNode(pExp, pTargetNode);
4,569,011✔
2065
    if (code != TSDB_CODE_SUCCESS) {
4,572,468✔
2066
      destroyExprInfo(pExprs, *numOfExprs);
551✔
2067
      taosMemoryFreeClear(pExprs);
×
2068
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
2069
      return code;
×
2070
    }
2071
  }
2072

2073
  *pExprInfo = pExprs;
2,007,995✔
2074
  return code;
2,007,995✔
2075
}
2076

2077
// set the output buffer for the selectivity + tag query
2078
static int32_t setSelectValueColumnInfo(SqlFunctionCtx* pCtx, int32_t numOfOutput) {
2,180,393✔
2079
  int32_t num = 0;
2,180,393✔
2080
  int32_t code = TSDB_CODE_SUCCESS;
2,180,393✔
2081
  int32_t lino = 0;
2,180,393✔
2082

2083
  SqlFunctionCtx*  p = NULL;
2,180,393✔
2084
  SqlFunctionCtx** pValCtx = taosMemoryCalloc(numOfOutput, POINTER_BYTES);
2,180,393!
2085
  if (pValCtx == NULL) {
2,180,889!
2086
    return terrno;
×
2087
  }
2088

2089
  SHashObj* pSelectFuncs = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
2,180,889✔
2090
  QUERY_CHECK_NULL(pSelectFuncs, code, lino, _end, terrno);
2,180,141!
2091

2092
  for (int32_t i = 0; i < numOfOutput; ++i) {
6,484,711✔
2093
    const char* pName = pCtx[i].pExpr->pExpr->_function.functionName;
4,334,619✔
2094
    if ((strcmp(pName, "_select_value") == 0) || (strcmp(pName, "_group_key") == 0) ||
4,334,619✔
2095
        (strcmp(pName, "_group_const_value") == 0)) {
4,069,861!
2096
      pValCtx[num++] = &pCtx[i];
264,456✔
2097
    } else if (fmIsSelectFunc(pCtx[i].functionId)) {
4,070,163✔
2098
      void* data = taosHashGet(pSelectFuncs, pName, strlen(pName));
230,443✔
2099
      if (taosHashGetSize(pSelectFuncs) != 0 && data == NULL) {
230,137✔
2100
        p = NULL;
29,190✔
2101
        break;
29,190✔
2102
      } else {
2103
        int32_t tempRes = taosHashPut(pSelectFuncs, pName, strlen(pName), &num, sizeof(num));
200,942✔
2104
        if (tempRes != TSDB_CODE_SUCCESS && tempRes != TSDB_CODE_DUP_KEY) {
200,984!
2105
          code = tempRes;
×
2106
          QUERY_CHECK_CODE(code, lino, _end);
×
2107
        }
2108
        p = &pCtx[i];
200,984✔
2109
      }
2110
    }
2111
  }
2112
  taosHashCleanup(pSelectFuncs);
2,179,282✔
2113

2114
  if (p != NULL) {
2,180,195✔
2115
    p->subsidiaries.pCtx = pValCtx;
94,161✔
2116
    p->subsidiaries.num = num;
94,161✔
2117
  } else {
2118
    taosMemoryFreeClear(pValCtx);
2,086,034!
2119
  }
2120

2121
_end:
×
2122
  if (code != TSDB_CODE_SUCCESS) {
2,180,379!
2123
    taosMemoryFreeClear(pValCtx);
×
2124
    taosHashCleanup(pSelectFuncs);
×
2125
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
2126
  }
2127
  return code;
2,179,160✔
2128
}
2129

2130
SqlFunctionCtx* createSqlFunctionCtx(SExprInfo* pExprInfo, int32_t numOfOutput, int32_t** rowEntryInfoOffset,
2,179,076✔
2131
                                     SFunctionStateStore* pStore) {
2132
  int32_t         code = TSDB_CODE_SUCCESS;
2,179,076✔
2133
  int32_t         lino = 0;
2,179,076✔
2134
  SqlFunctionCtx* pFuncCtx = (SqlFunctionCtx*)taosMemoryCalloc(numOfOutput, sizeof(SqlFunctionCtx));
2,179,076!
2135
  if (pFuncCtx == NULL) {
2,179,270!
2136
    return NULL;
×
2137
  }
2138

2139
  *rowEntryInfoOffset = taosMemoryCalloc(numOfOutput, sizeof(int32_t));
2,179,270!
2140
  if (*rowEntryInfoOffset == 0) {
2,180,950!
2141
    taosMemoryFreeClear(pFuncCtx);
×
2142
    return NULL;
×
2143
  }
2144

2145
  for (int32_t i = 0; i < numOfOutput; ++i) {
6,680,143✔
2146
    SExprInfo* pExpr = &pExprInfo[i];
4,498,612✔
2147

2148
    SExprBasicInfo* pFunct = &pExpr->base;
4,498,612✔
2149
    SqlFunctionCtx* pCtx = &pFuncCtx[i];
4,498,612✔
2150

2151
    pCtx->functionId = -1;
4,498,612✔
2152
    pCtx->pExpr = pExpr;
4,498,612✔
2153

2154
    if (pExpr->pExpr->nodeType == QUERY_NODE_FUNCTION) {
4,498,612✔
2155
      SFuncExecEnv env = {0};
2,312,206✔
2156
      pCtx->functionId = pExpr->pExpr->_function.pFunctNode->funcId;
2,312,206✔
2157
      pCtx->isPseudoFunc = fmIsWindowPseudoColumnFunc(pCtx->functionId);
2,312,206✔
2158
      pCtx->isNotNullFunc = fmIsNotNullOutputFunc(pCtx->functionId);
2,311,218✔
2159

2160
      bool isUdaf = fmIsUserDefinedFunc(pCtx->functionId);
2,311,733✔
2161
      if (fmIsAggFunc(pCtx->functionId) || fmIsIndefiniteRowsFunc(pCtx->functionId)) {
3,666,944✔
2162
        if (!isUdaf) {
1,356,218✔
2163
          code = fmGetFuncExecFuncs(pCtx->functionId, &pCtx->fpSet);
1,356,186✔
2164
          QUERY_CHECK_CODE(code, lino, _end);
1,355,948!
2165
        } else {
2166
          char* udfName = pExpr->pExpr->_function.pFunctNode->functionName;
32✔
2167
          pCtx->udfName = taosStrdup(udfName);
32!
2168
          QUERY_CHECK_NULL(pCtx->udfName, code, lino, _end, terrno);
32!
2169

2170
          code = fmGetUdafExecFuncs(pCtx->functionId, &pCtx->fpSet);
32✔
2171
          QUERY_CHECK_CODE(code, lino, _end);
32!
2172
        }
2173
        bool tmp = pCtx->fpSet.getEnv(pExpr->pExpr->_function.pFunctNode, &env);
1,355,980✔
2174
        if (!tmp) {
1,355,262!
2175
          code = terrno;
×
2176
          QUERY_CHECK_CODE(code, lino, _end);
×
2177
        }
2178
      } else {
2179
        code = fmGetScalarFuncExecFuncs(pCtx->functionId, &pCtx->sfp);
954,519✔
2180
        if (code != TSDB_CODE_SUCCESS && isUdaf) {
954,182!
2181
          code = TSDB_CODE_SUCCESS;
261✔
2182
        }
2183
        QUERY_CHECK_CODE(code, lino, _end);
954,182!
2184

2185
        if (pCtx->sfp.getEnv != NULL) {
954,182✔
2186
          bool tmp = pCtx->sfp.getEnv(pExpr->pExpr->_function.pFunctNode, &env);
145,085✔
2187
          if (!tmp) {
145,072✔
2188
            code = terrno;
373✔
2189
            QUERY_CHECK_CODE(code, lino, _end);
×
2190
          }
2191
        }
2192
      }
2193
      pCtx->resDataInfo.interBufSize = env.calcMemSize;
2,309,324✔
2194
    } else if (pExpr->pExpr->nodeType == QUERY_NODE_COLUMN || pExpr->pExpr->nodeType == QUERY_NODE_OPERATOR ||
2,186,406✔
2195
               pExpr->pExpr->nodeType == QUERY_NODE_VALUE) {
143,542✔
2196
      // for simple column, the result buffer needs to hold at least one element.
2197
      pCtx->resDataInfo.interBufSize = pFunct->resSchema.bytes;
2,186,325✔
2198
    }
2199

2200
    pCtx->input.numOfInputCols = pFunct->numOfParams;
4,495,730✔
2201
    pCtx->input.pData = taosMemoryCalloc(pFunct->numOfParams, POINTER_BYTES);
4,495,730✔
2202
    QUERY_CHECK_NULL(pCtx->input.pData, code, lino, _end, terrno);
4,498,807!
2203
    pCtx->input.pColumnDataAgg = taosMemoryCalloc(pFunct->numOfParams, POINTER_BYTES);
4,498,807!
2204
    QUERY_CHECK_NULL(pCtx->input.pColumnDataAgg, code, lino, _end, terrno);
4,499,193!
2205

2206
    pCtx->pTsOutput = NULL;
4,499,193✔
2207
    pCtx->resDataInfo.bytes = pFunct->resSchema.bytes;
4,499,193✔
2208
    pCtx->resDataInfo.type = pFunct->resSchema.type;
4,499,193✔
2209
    pCtx->order = TSDB_ORDER_ASC;
4,499,193✔
2210
    pCtx->start.key = INT64_MIN;
4,499,193✔
2211
    pCtx->end.key = INT64_MIN;
4,499,193✔
2212
    pCtx->numOfParams = pExpr->base.numOfParams;
4,499,193✔
2213
    pCtx->param = pFunct->pParam;
4,499,193✔
2214
    pCtx->saveHandle.currentPage = -1;
4,499,193✔
2215
    pCtx->pStore = pStore;
4,499,193✔
2216
    pCtx->hasWindowOrGroup = false;
4,499,193✔
2217
    pCtx->needCleanup = false;
4,499,193✔
2218
  }
2219

2220
  for (int32_t i = 1; i < numOfOutput; ++i) {
4,687,014✔
2221
    (*rowEntryInfoOffset)[i] = (int32_t)((*rowEntryInfoOffset)[i - 1] + sizeof(SResultRowEntryInfo) +
2,505,483✔
2222
                                         pFuncCtx[i - 1].resDataInfo.interBufSize);
2,505,483✔
2223
  }
2224

2225
  code = setSelectValueColumnInfo(pFuncCtx, numOfOutput);
2,181,531✔
2226
  QUERY_CHECK_CODE(code, lino, _end);
2,179,267!
2227

2228
_end:
2,179,267✔
2229
  if (code != TSDB_CODE_SUCCESS) {
2,179,267!
2230
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
2231
    for (int32_t i = 0; i < numOfOutput; ++i) {
×
2232
      taosMemoryFree(pFuncCtx[i].input.pData);
×
2233
      taosMemoryFree(pFuncCtx[i].input.pColumnDataAgg);
×
2234
    }
2235
    taosMemoryFreeClear(*rowEntryInfoOffset);
×
2236
    taosMemoryFreeClear(pFuncCtx);
×
2237

2238
    terrno = code;
×
2239
    return NULL;
×
2240
  }
2241
  return pFuncCtx;
2,179,267✔
2242
}
2243

2244
// NOTE: sources columns are more than the destination SSDatablock columns.
2245
// doFilter in table scan needs every column even its output is false
2246
int32_t relocateColumnData(SSDataBlock* pBlock, const SArray* pColMatchInfo, SArray* pCols, bool outputEveryColumn) {
35,448✔
2247
  int32_t code = TSDB_CODE_SUCCESS;
35,448✔
2248
  size_t  numOfSrcCols = taosArrayGetSize(pCols);
35,448✔
2249

2250
  int32_t i = 0, j = 0;
35,446✔
2251
  while (i < numOfSrcCols && j < taosArrayGetSize(pColMatchInfo)) {
218,803✔
2252
    SColumnInfoData* p = taosArrayGet(pCols, i);
183,360✔
2253
    if (!p) {
183,365!
2254
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
2255
      return terrno;
×
2256
    }
2257
    SColMatchItem* pmInfo = taosArrayGet(pColMatchInfo, j);
183,365✔
2258
    if (!pmInfo) {
183,359!
2259
      return terrno;
×
2260
    }
2261

2262
    if (p->info.colId == pmInfo->colId) {
183,361✔
2263
      SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, pmInfo->dstSlotId);
158,790✔
2264
      if (!pDst) {
158,781!
2265
        return terrno;
×
2266
      }
2267
      code = colDataAssign(pDst, p, pBlock->info.rows, &pBlock->info);
158,781✔
2268
      if (code != TSDB_CODE_SUCCESS) {
158,786!
UNCOV
2269
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
2270
        return code;
×
2271
      }
2272
      i++;
158,786✔
2273
      j++;
158,786✔
2274
    } else if (p->info.colId < pmInfo->colId) {
24,571!
2275
      i++;
24,571✔
2276
    } else {
2277
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR));
×
2278
      return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
2279
    }
2280
  }
2281
  return code;
35,447✔
2282
}
2283

2284
SInterval extractIntervalInfo(const STableScanPhysiNode* pTableScanNode) {
946,481✔
2285
  SInterval interval = {
946,481✔
2286
      .interval = pTableScanNode->interval,
946,481✔
2287
      .sliding = pTableScanNode->sliding,
946,481✔
2288
      .intervalUnit = pTableScanNode->intervalUnit,
946,481✔
2289
      .slidingUnit = pTableScanNode->slidingUnit,
946,481✔
2290
      .offset = pTableScanNode->offset,
946,481✔
2291
      .precision = pTableScanNode->scan.node.pOutputDataBlockDesc->precision,
946,481✔
2292
      .timeRange = pTableScanNode->scanRange,
2293
  };
2294
  calcIntervalAutoOffset(&interval);
946,481✔
2295

2296
  return interval;
946,238✔
2297
}
2298

2299
SColumn extractColumnFromColumnNode(SColumnNode* pColNode) {
276,114✔
2300
  SColumn c = {0};
276,114✔
2301

2302
  c.slotId = pColNode->slotId;
276,114✔
2303
  c.colId = pColNode->colId;
276,114✔
2304
  c.type = pColNode->node.resType.type;
276,114✔
2305
  c.bytes = pColNode->node.resType.bytes;
276,114✔
2306
  c.scale = pColNode->node.resType.scale;
276,114✔
2307
  c.precision = pColNode->node.resType.precision;
276,114✔
2308
  return c;
276,114✔
2309
}
2310

2311
int32_t initQueryTableDataCond(SQueryTableDataCond* pCond, const STableScanPhysiNode* pTableScanNode,
1,107,187✔
2312
                               const SReadHandle* readHandle) {
2313
  pCond->order = pTableScanNode->scanSeq[0] > 0 ? TSDB_ORDER_ASC : TSDB_ORDER_DESC;
1,107,187✔
2314
  pCond->numOfCols = LIST_LENGTH(pTableScanNode->scan.pScanCols);
1,107,187!
2315

2316
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
1,107,187!
2317
  if (!pCond->colList) {
1,109,400!
2318
    return terrno;
×
2319
  }
2320
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
1,109,400!
2321
  if (pCond->pSlotList == NULL) {
1,108,977!
2322
    taosMemoryFreeClear(pCond->colList);
×
2323
    return terrno;
×
2324
  }
2325

2326
  // TODO: get it from stable scan node
2327
  pCond->twindows = pTableScanNode->scanRange;
1,108,977✔
2328
  pCond->suid = pTableScanNode->scan.suid;
1,108,977✔
2329
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
1,108,977✔
2330
  pCond->startVersion = -1;
1,108,977✔
2331
  pCond->endVersion = -1;
1,108,977✔
2332
  pCond->skipRollup = readHandle->skipRollup;
1,108,977✔
2333

2334
  // allowed read stt file optimization mode
2335
  pCond->notLoadData = (pTableScanNode->dataRequired == FUNC_DATA_REQUIRED_NOT_LOAD) &&
2,274,255✔
2336
                       (pTableScanNode->scan.node.pConditions == NULL) && (pTableScanNode->interval == 0);
1,108,977✔
2337

2338
  int32_t j = 0;
1,108,977✔
2339
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
4,328,518✔
2340
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pTableScanNode->scan.pScanCols, i);
3,218,679✔
2341
    if (!pNode) {
3,219,771✔
2342
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
230!
2343
      return terrno;
230✔
2344
    }
2345
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
3,219,541✔
2346
    if (pColNode->colType == COLUMN_TYPE_TAG) {
3,219,541!
2347
      continue;
×
2348
    }
2349

2350
    pCond->colList[j].type = pColNode->node.resType.type;
3,219,541✔
2351
    pCond->colList[j].bytes = pColNode->node.resType.bytes;
3,219,541✔
2352
    pCond->colList[j].colId = pColNode->colId;
3,219,541✔
2353
    pCond->colList[j].pk = pColNode->isPk;
3,219,541✔
2354

2355
    pCond->pSlotList[j] = pNode->slotId;
3,219,541✔
2356
    j += 1;
3,219,541✔
2357
  }
2358

2359
  pCond->numOfCols = j;
1,109,839✔
2360
  return TSDB_CODE_SUCCESS;
1,109,839✔
2361
}
2362

2363
void cleanupQueryTableDataCond(SQueryTableDataCond* pCond) {
1,275,302✔
2364
  taosMemoryFreeClear(pCond->colList);
1,275,302!
2365
  taosMemoryFreeClear(pCond->pSlotList);
1,275,481!
2366
}
1,275,469✔
2367

2368
int32_t convertFillType(int32_t mode) {
24,341✔
2369
  int32_t type = TSDB_FILL_NONE;
24,341✔
2370
  switch (mode) {
24,341!
2371
    case FILL_MODE_PREV:
740✔
2372
      type = TSDB_FILL_PREV;
740✔
2373
      break;
740✔
2374
    case FILL_MODE_NONE:
×
2375
      type = TSDB_FILL_NONE;
×
2376
      break;
×
2377
    case FILL_MODE_NULL:
10,981✔
2378
      type = TSDB_FILL_NULL;
10,981✔
2379
      break;
10,981✔
2380
    case FILL_MODE_NULL_F:
10✔
2381
      type = TSDB_FILL_NULL_F;
10✔
2382
      break;
10✔
2383
    case FILL_MODE_NEXT:
425✔
2384
      type = TSDB_FILL_NEXT;
425✔
2385
      break;
425✔
2386
    case FILL_MODE_VALUE:
956✔
2387
      type = TSDB_FILL_SET_VALUE;
956✔
2388
      break;
956✔
2389
    case FILL_MODE_VALUE_F:
19✔
2390
      type = TSDB_FILL_SET_VALUE_F;
19✔
2391
      break;
19✔
2392
    case FILL_MODE_LINEAR:
1,180✔
2393
      type = TSDB_FILL_LINEAR;
1,180✔
2394
      break;
1,180✔
2395
    case FILL_MODE_NEAR:
10,030✔
2396
      type = TSDB_FILL_NEAR;
10,030✔
2397
      break;
10,030✔
2398
    default:
×
2399
      type = TSDB_FILL_NONE;
×
2400
  }
2401

2402
  return type;
24,341✔
2403
}
2404

2405
void getInitialStartTimeWindow(SInterval* pInterval, TSKEY ts, STimeWindow* w, bool ascQuery) {
785,307✔
2406
  if (ascQuery) {
785,307✔
2407
    *w = getAlignQueryTimeWindow(pInterval, ts);
782,024✔
2408
  } else {
2409
    // the start position of the first time window in the endpoint that spreads beyond the queried last timestamp
2410
    *w = getAlignQueryTimeWindow(pInterval, ts);
3,283✔
2411

2412
    int64_t key = w->skey;
3,294✔
2413
    while (key < ts) {  // moving towards end
3,360✔
2414
      key = getNextTimeWindowStart(pInterval, key, TSDB_ORDER_ASC);
1,835✔
2415
      if (key > ts) {
1,835✔
2416
        break;
1,769✔
2417
      }
2418

2419
      w->skey = key;
66✔
2420
    }
2421
    w->ekey = taosTimeAdd(w->skey, pInterval->interval, pInterval->intervalUnit, pInterval->precision, NULL) - 1;
3,294✔
2422
  }
2423
}
783,167✔
2424

2425
static STimeWindow doCalculateTimeWindow(int64_t ts, SInterval* pInterval) {
659,300✔
2426
  STimeWindow w = {0};
659,300✔
2427

2428
  w.skey = taosTimeTruncate(ts, pInterval);
659,300✔
2429
  w.ekey = taosTimeGetIntervalEnd(w.skey, pInterval);
659,422✔
2430
  return w;
659,373✔
2431
}
2432

2433
STimeWindow getFirstQualifiedTimeWindow(int64_t ts, STimeWindow* pWindow, SInterval* pInterval, int32_t order) {
10,653✔
2434
  STimeWindow win = *pWindow;
10,653✔
2435
  STimeWindow save = win;
10,653✔
2436
  while (win.skey <= ts && win.ekey >= ts) {
83,953✔
2437
    save = win;
73,298✔
2438
    // get previous time window
2439
    getNextTimeWindow(pInterval, &win, order == TSDB_ORDER_ASC ? TSDB_ORDER_DESC : TSDB_ORDER_ASC);
73,298✔
2440
  }
2441

2442
  return save;
10,655✔
2443
}
2444

2445
// get the correct time window according to the handled timestamp
2446
// todo refactor
2447
STimeWindow getActiveTimeWindow(SDiskbasedBuf* pBuf, SResultRowInfo* pResultRowInfo, int64_t ts, SInterval* pInterval,
1,701,764✔
2448
                                int32_t order) {
2449
  STimeWindow w = {0};
1,701,764✔
2450
  if (pResultRowInfo->cur.pageId == -1) {  // the first window, from the previous stored value
1,701,764✔
2451
    getInitialStartTimeWindow(pInterval, ts, &w, (order == TSDB_ORDER_ASC));
785,317✔
2452
    return w;
783,108✔
2453
  }
2454

2455
  SResultRow* pRow = getResultRowByPos(pBuf, &pResultRowInfo->cur, false);
916,447✔
2456
  if (pRow) {
916,253!
2457
    w = pRow->win;
916,264✔
2458
  }
2459

2460
  // in case of typical time window, we can calculate time window directly.
2461
  if (w.skey > ts || w.ekey < ts) {
916,253✔
2462
    w = doCalculateTimeWindow(ts, pInterval);
659,296✔
2463
  }
2464

2465
  if (pInterval->interval != pInterval->sliding) {
916,326✔
2466
    // it is an sliding window query, in which sliding value is not equalled to
2467
    // interval value, and we need to find the first qualified time window.
2468
    w = getFirstQualifiedTimeWindow(ts, &w, pInterval, order);
10,653✔
2469
  }
2470

2471
  return w;
916,333✔
2472
}
2473

2474
TSKEY getNextTimeWindowStart(const SInterval* pInterval, TSKEY start, int32_t order) {
104,223,272✔
2475
  int32_t factor = GET_FORWARD_DIRECTION_FACTOR(order);
104,223,272✔
2476
  TSKEY   nextStart = taosTimeAdd(start, -1 * pInterval->offset, pInterval->offsetUnit, pInterval->precision, NULL);
104,223,272✔
2477
  nextStart = taosTimeAdd(nextStart, factor * pInterval->sliding, pInterval->slidingUnit, pInterval->precision, NULL);
104,145,093✔
2478
  nextStart = taosTimeAdd(nextStart, pInterval->offset, pInterval->offsetUnit, pInterval->precision, NULL);
104,165,324✔
2479
  return nextStart;
103,868,710✔
2480
}
2481

2482
void getNextTimeWindow(const SInterval* pInterval, STimeWindow* tw, int32_t order) {
104,261,361✔
2483
  tw->skey = getNextTimeWindowStart(pInterval, tw->skey, order);
104,261,361✔
2484
  tw->ekey = taosTimeAdd(tw->skey, pInterval->interval, pInterval->intervalUnit, pInterval->precision, NULL) - 1;
103,856,894✔
2485
}
103,716,909✔
2486

2487
bool hasLimitOffsetInfo(SLimitInfo* pLimitInfo) {
2,863,053✔
2488
  return (pLimitInfo->limit.limit != -1 || pLimitInfo->limit.offset != -1 || pLimitInfo->slimit.limit != -1 ||
5,703,627!
2489
          pLimitInfo->slimit.offset != -1);
2,840,574✔
2490
}
2491

2492
bool hasSlimitOffsetInfo(SLimitInfo* pLimitInfo) {
×
2493
  return (pLimitInfo->slimit.limit != -1 || pLimitInfo->slimit.offset != -1);
×
2494
}
2495

2496
void initLimitInfo(const SNode* pLimit, const SNode* pSLimit, SLimitInfo* pLimitInfo) {
2,738,388✔
2497
  SLimit limit = {.limit = getLimit(pLimit), .offset = getOffset(pLimit)};
2,738,388✔
2498
  SLimit slimit = {.limit = getLimit(pSLimit), .offset = getOffset(pSLimit)};
2,739,063✔
2499

2500
  pLimitInfo->limit = limit;
2,739,093✔
2501
  pLimitInfo->slimit = slimit;
2,739,093✔
2502
  pLimitInfo->remainOffset = limit.offset;
2,739,093✔
2503
  pLimitInfo->remainGroupOffset = slimit.offset;
2,739,093✔
2504
}
2,739,093✔
2505

2506
void resetLimitInfoForNextGroup(SLimitInfo* pLimitInfo) {
495,964✔
2507
  pLimitInfo->numOfOutputRows = 0;
495,964✔
2508
  pLimitInfo->remainOffset = pLimitInfo->limit.offset;
495,964✔
2509
}
495,964✔
2510

2511
int32_t tableListGetSize(const STableListInfo* pTableList, int32_t* pRes) {
2,781,124✔
2512
  if (taosArrayGetSize(pTableList->pTableList) != taosHashGetSize(pTableList->map)) {
2,781,124!
2513
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR));
×
2514
    return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
2515
  }
2516
  (*pRes) = taosArrayGetSize(pTableList->pTableList);
2,783,649✔
2517
  return TSDB_CODE_SUCCESS;
2,783,922✔
2518
}
2519

2520
uint64_t tableListGetSuid(const STableListInfo* pTableList) { return pTableList->idInfo.suid; }
12,751✔
2521

2522
STableKeyInfo* tableListGetInfo(const STableListInfo* pTableList, int32_t index) {
900,853✔
2523
  if (taosArrayGetSize(pTableList->pTableList) == 0) {
900,853✔
2524
    return NULL;
13✔
2525
  }
2526

2527
  return taosArrayGet(pTableList->pTableList, index);
901,391✔
2528
}
2529

2530
int32_t tableListFind(const STableListInfo* pTableList, uint64_t uid, int32_t startIndex) {
180✔
2531
  int32_t numOfTables = taosArrayGetSize(pTableList->pTableList);
180✔
2532
  if (startIndex >= numOfTables) {
180!
2533
    return -1;
×
2534
  }
2535

2536
  for (int32_t i = startIndex; i < numOfTables; ++i) {
2,338!
2537
    STableKeyInfo* p = taosArrayGet(pTableList->pTableList, i);
2,360✔
2538
    if (!p) {
2,368✔
2539
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
29!
2540
      return -1;
×
2541
    }
2542
    if (p->uid == uid) {
2,339✔
2543
      return i;
181✔
2544
    }
2545
  }
UNCOV
2546
  return -1;
×
2547
}
2548

2549
void tableListGetSourceTableInfo(const STableListInfo* pTableList, uint64_t* psuid, uint64_t* uid, int32_t* type) {
11,304✔
2550
  *psuid = pTableList->idInfo.suid;
11,304✔
2551
  *uid = pTableList->idInfo.uid;
11,304✔
2552
  *type = pTableList->idInfo.tableType;
11,304✔
2553
}
11,304✔
2554

2555
uint64_t tableListGetTableGroupId(const STableListInfo* pTableList, uint64_t tableUid) {
3,487,617✔
2556
  int32_t* slot = taosHashGet(pTableList->map, &tableUid, sizeof(tableUid));
3,487,617✔
2557
  if (slot == NULL) {
3,490,110!
2558
    return -1;
×
2559
  }
2560

2561
  STableKeyInfo* pKeyInfo = taosArrayGet(pTableList->pTableList, *slot);
3,490,110✔
2562

2563
  return pKeyInfo->groupId;
3,488,945✔
2564
}
2565

2566
// TODO handle the group offset info, fix it, the rule of group output will be broken by this function
2567
int32_t tableListAddTableInfo(STableListInfo* pTableList, uint64_t uid, uint64_t gid) {
2,898✔
2568
  int32_t code = TSDB_CODE_SUCCESS;
2,898✔
2569
  int32_t lino = 0;
2,898✔
2570
  if (pTableList->map == NULL) {
2,898!
2571
    pTableList->map = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
×
2572
    QUERY_CHECK_NULL(pTableList->map, code, lino, _end, terrno);
×
2573
  }
2574

2575
  STableKeyInfo keyInfo = {.uid = uid, .groupId = gid};
2,898✔
2576
  void*         p = taosHashGet(pTableList->map, &uid, sizeof(uid));
2,898✔
2577
  if (p != NULL) {
2,897✔
2578
    qInfo("table:%" PRId64 " already in tableIdList, ignore it", uid);
3!
2579
    goto _end;
3✔
2580
  }
2581

2582
  void* tmp = taosArrayPush(pTableList->pTableList, &keyInfo);
2,894✔
2583
  QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
2,894!
2584

2585
  int32_t slot = (int32_t)taosArrayGetSize(pTableList->pTableList) - 1;
2,894✔
2586
  code = taosHashPut(pTableList->map, &uid, sizeof(uid), &slot, sizeof(slot));
2,894✔
2587
  if (code != TSDB_CODE_SUCCESS) {
2,894!
2588
    // we have checked the existence of uid in hash map above
2589
    QUERY_CHECK_CONDITION((code != TSDB_CODE_DUP_KEY), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
×
2590
    taosArrayPopTailBatch(pTableList->pTableList, 1);  // let's pop the last element in the array list
×
2591
  }
2592

2593
_end:
2,894✔
2594
  if (code != TSDB_CODE_SUCCESS) {
2,897!
2595
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
2596
  } else {
2597
    qDebug("uid:%" PRIu64 ", groupId:%" PRIu64 " added into table list, slot:%d, total:%d", uid, gid, slot, slot + 1);
2,897✔
2598
  }
2599

2600
  return code;
2,897✔
2601
}
2602

2603
int32_t tableListGetGroupList(const STableListInfo* pTableList, int32_t ordinalGroupIndex, STableKeyInfo** pKeyInfo,
912,054✔
2604
                              int32_t* size) {
2605
  int32_t totalGroups = tableListGetOutputGroups(pTableList);
912,054✔
2606
  int32_t numOfTables = 0;
912,490✔
2607
  int32_t code = tableListGetSize(pTableList, &numOfTables);
912,490✔
2608
  if (code != TSDB_CODE_SUCCESS) {
912,828!
2609
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
2610
    return code;
×
2611
  }
2612

2613
  if (ordinalGroupIndex < 0 || ordinalGroupIndex >= totalGroups) {
912,828!
2614
    return TSDB_CODE_INVALID_PARA;
×
2615
  }
2616

2617
  // here handle two special cases:
2618
  // 1. only one group exists, and 2. one table exists for each group.
2619
  if (totalGroups == 1) {
912,828✔
2620
    *size = numOfTables;
911,628✔
2621
    *pKeyInfo = (*size == 0) ? NULL : taosArrayGet(pTableList->pTableList, 0);
911,628✔
2622
    return TSDB_CODE_SUCCESS;
911,136✔
2623
  } else if (totalGroups == numOfTables) {
1,200✔
2624
    *size = 1;
988✔
2625
    *pKeyInfo = taosArrayGet(pTableList->pTableList, ordinalGroupIndex);
988✔
2626
    return TSDB_CODE_SUCCESS;
988✔
2627
  }
2628

2629
  int32_t offset = pTableList->groupOffset[ordinalGroupIndex];
212✔
2630
  if (ordinalGroupIndex < totalGroups - 1) {
212!
2631
    *size = pTableList->groupOffset[ordinalGroupIndex + 1] - offset;
532✔
2632
  } else {
2633
    *size = numOfTables - offset;
×
2634
  }
2635

2636
  *pKeyInfo = taosArrayGet(pTableList->pTableList, offset);
212✔
2637
  return TSDB_CODE_SUCCESS;
664✔
2638
}
2639

2640
int32_t tableListGetOutputGroups(const STableListInfo* pTableList) { return pTableList->numOfOuputGroups; }
2,690,025✔
2641

2642
bool oneTableForEachGroup(const STableListInfo* pTableList) { return pTableList->oneTableForEachGroup; }
2,708✔
2643

2644
STableListInfo* tableListCreate() {
1,155,140✔
2645
  STableListInfo* pListInfo = taosMemoryCalloc(1, sizeof(STableListInfo));
1,155,140!
2646
  if (pListInfo == NULL) {
1,157,525!
2647
    return NULL;
×
2648
  }
2649

2650
  pListInfo->remainGroups = NULL;
1,157,525✔
2651
  pListInfo->pTableList = taosArrayInit(4, sizeof(STableKeyInfo));
1,157,525✔
2652
  if (pListInfo->pTableList == NULL) {
1,157,367!
2653
    goto _error;
×
2654
  }
2655

2656
  pListInfo->map = taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
1,157,367✔
2657
  if (pListInfo->map == NULL) {
1,157,204!
2658
    goto _error;
×
2659
  }
2660

2661
  pListInfo->numOfOuputGroups = 1;
1,157,204✔
2662
  return pListInfo;
1,157,204✔
2663

2664
_error:
×
2665
  tableListDestroy(pListInfo);
×
2666
  return NULL;
×
2667
}
2668

2669
void tableListDestroy(STableListInfo* pTableListInfo) {
1,157,867✔
2670
  if (pTableListInfo == NULL) {
1,157,867✔
2671
    return;
2✔
2672
  }
2673

2674
  taosArrayDestroy(pTableListInfo->pTableList);
1,157,865✔
2675
  taosMemoryFreeClear(pTableListInfo->groupOffset);
1,158,013!
2676

2677
  taosHashCleanup(pTableListInfo->map);
1,158,013✔
2678
  taosHashCleanup(pTableListInfo->remainGroups);
1,158,026✔
2679
  pTableListInfo->pTableList = NULL;
1,158,030✔
2680
  pTableListInfo->map = NULL;
1,158,030✔
2681
  taosMemoryFree(pTableListInfo);
1,158,030✔
2682
}
2683

2684
void tableListClear(STableListInfo* pTableListInfo) {
2,114✔
2685
  if (pTableListInfo == NULL) {
2,114!
2686
    return;
×
2687
  }
2688

2689
  taosArrayClear(pTableListInfo->pTableList);
2,114✔
2690
  taosHashClear(pTableListInfo->map);
2,114✔
2691
  taosHashClear(pTableListInfo->remainGroups);
2,116✔
2692
  taosMemoryFree(pTableListInfo->groupOffset);
2,116!
2693
  pTableListInfo->numOfOuputGroups = 1;
2,116✔
2694
  pTableListInfo->oneTableForEachGroup = false;
2,116✔
2695
}
2696

2697
static int32_t orderbyGroupIdComparFn(const void* p1, const void* p2) {
79,258✔
2698
  STableKeyInfo* pInfo1 = (STableKeyInfo*)p1;
79,258✔
2699
  STableKeyInfo* pInfo2 = (STableKeyInfo*)p2;
79,258✔
2700

2701
  if (pInfo1->groupId == pInfo2->groupId) {
79,258✔
2702
    return 0;
169✔
2703
  } else {
2704
    return pInfo1->groupId < pInfo2->groupId ? -1 : 1;
79,089✔
2705
  }
2706
}
2707

2708
static int32_t sortTableGroup(STableListInfo* pTableListInfo) {
868✔
2709
  taosArraySort(pTableListInfo->pTableList, orderbyGroupIdComparFn);
868✔
2710
  int32_t size = taosArrayGetSize(pTableListInfo->pTableList);
868✔
2711

2712
  SArray* pList = taosArrayInit(4, sizeof(int32_t));
871✔
2713
  if (!pList) {
871!
2714
    return terrno;
×
2715
  }
2716

2717
  STableKeyInfo* pInfo = taosArrayGet(pTableListInfo->pTableList, 0);
871✔
2718
  if (!pInfo) {
871!
2719
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
2720
    return terrno;
×
2721
  }
2722
  uint64_t       gid = pInfo->groupId;
871✔
2723

2724
  int32_t start = 0;
871✔
2725
  void*   tmp = taosArrayPush(pList, &start);
868✔
2726
  if (!tmp) {
868!
2727
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
2728
    return terrno;
×
2729
  }
2730

2731
  for (int32_t i = 1; i < size; ++i) {
2,771✔
2732
    pInfo = taosArrayGet(pTableListInfo->pTableList, i);
1,902✔
2733
    if (!pInfo) {
1,903✔
2734
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
1!
2735
      return terrno;
1✔
2736
    }
2737
    if (pInfo->groupId != gid) {
1,902✔
2738
      tmp = taosArrayPush(pList, &i);
1,735✔
2739
      if (!tmp) {
1,735!
2740
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
2741
        return terrno;
×
2742
      }
2743
      gid = pInfo->groupId;
1,735✔
2744
    }
2745
  }
2746

2747
  pTableListInfo->numOfOuputGroups = taosArrayGetSize(pList);
869✔
2748
  pTableListInfo->groupOffset = taosMemoryMalloc(sizeof(int32_t) * pTableListInfo->numOfOuputGroups);
868!
2749
  if (pTableListInfo->groupOffset == NULL) {
870!
2750
    taosArrayDestroy(pList);
×
2751
    return terrno;
×
2752
  }
2753

2754
  memcpy(pTableListInfo->groupOffset, taosArrayGet(pList, 0), sizeof(int32_t) * pTableListInfo->numOfOuputGroups);
870✔
2755
  taosArrayDestroy(pList);
870✔
2756
  return TSDB_CODE_SUCCESS;
871✔
2757
}
2758

2759
int32_t buildGroupIdMapForAllTables(STableListInfo* pTableListInfo, SReadHandle* pHandle, SScanPhysiNode* pScanNode,
1,027,997✔
2760
                                    SNodeList* group, bool groupSort, uint8_t* digest, SStorageAPI* pAPI) {
2761
  int32_t code = TSDB_CODE_SUCCESS;
1,027,997✔
2762

2763
  bool   groupByTbname = groupbyTbname(group);
1,027,997✔
2764
  size_t numOfTables = taosArrayGetSize(pTableListInfo->pTableList);
1,027,804✔
2765
  if (!numOfTables) {
1,027,641!
2766
    return code;
×
2767
  }
2768
  if (group == NULL || groupByTbname) {
1,027,641✔
2769
    if (tsCountAlwaysReturnValue && QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pScanNode) &&
987,091✔
2770
        ((STableScanPhysiNode*)pScanNode)->needCountEmptyTable) {
758,711✔
2771
      pTableListInfo->remainGroups =
71,839✔
2772
          taosHashInit(numOfTables, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
71,850✔
2773
      if (pTableListInfo->remainGroups == NULL) {
71,839!
2774
        return terrno;
×
2775
      }
2776

2777
      for (int i = 0; i < numOfTables; i++) {
423,652✔
2778
        STableKeyInfo* info = taosArrayGet(pTableListInfo->pTableList, i);
351,705✔
2779
        if (!info) {
351,679!
UNCOV
2780
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
UNCOV
2781
          return terrno;
×
2782
        }
2783
        info->groupId = groupByTbname ? info->uid : 0;
351,689✔
2784

2785
        int32_t tempRes = taosHashPut(pTableListInfo->remainGroups, &(info->groupId), sizeof(info->groupId),
351,689✔
2786
                                      &(info->uid), sizeof(info->uid));
351,689✔
2787
        if (tempRes != TSDB_CODE_SUCCESS && tempRes != TSDB_CODE_DUP_KEY) {
351,813!
2788
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(tempRes));
×
2789
          return tempRes;
×
2790
        }
2791
      }
2792
    } else {
2793
      for (int32_t i = 0; i < numOfTables; i++) {
3,386,318✔
2794
        STableKeyInfo* info = taosArrayGet(pTableListInfo->pTableList, i);
2,470,824✔
2795
        if (!info) {
2,471,243✔
2796
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
166!
2797
          return terrno;
166✔
2798
        }
2799
        info->groupId = groupByTbname ? info->uid : 0;
2,471,077✔
2800
      }
2801
    }
2802

2803
    pTableListInfo->oneTableForEachGroup = groupByTbname;
987,441✔
2804
    if (numOfTables == 1 && pTableListInfo->idInfo.tableType == TSDB_CHILD_TABLE) {
987,441✔
2805
      pTableListInfo->oneTableForEachGroup = true;
241,435✔
2806
    }
2807

2808
    if (groupSort && groupByTbname) {
987,441✔
2809
      taosArraySort(pTableListInfo->pTableList, orderbyGroupIdComparFn);
17,993✔
2810
      pTableListInfo->numOfOuputGroups = numOfTables;
17,981✔
2811
    } else if (groupByTbname && pScanNode->groupOrderScan) {
969,448✔
2812
      pTableListInfo->numOfOuputGroups = numOfTables;
129✔
2813
    } else {
2814
      pTableListInfo->numOfOuputGroups = 1;
969,319✔
2815
    }
2816
  } else {
2817
    bool initRemainGroups = false;
40,550✔
2818
    if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pScanNode)) {
40,550✔
2819
      STableScanPhysiNode* pTableScanNode = (STableScanPhysiNode*)pScanNode;
39,999✔
2820
      if (tsCountAlwaysReturnValue && pTableScanNode->needCountEmptyTable &&
39,999✔
2821
          !(groupSort || pScanNode->groupOrderScan)) {
13,146!
2822
        initRemainGroups = true;
13,101✔
2823
      }
2824
    }
2825

2826
    code = getColInfoResultForGroupby(pHandle->vnode, group, pTableListInfo, digest, pAPI, initRemainGroups);
40,550✔
2827
    if (code != TSDB_CODE_SUCCESS) {
40,552✔
2828
      return code;
14✔
2829
    }
2830

2831
    if (pScanNode->groupOrderScan) pTableListInfo->numOfOuputGroups = taosArrayGetSize(pTableListInfo->pTableList);
40,538✔
2832

2833
    if (groupSort || pScanNode->groupOrderScan) {
40,538✔
2834
      code = sortTableGroup(pTableListInfo);
864✔
2835
    }
2836
  }
2837

2838
  // add all table entry in the hash map
2839
  size_t size = taosArrayGetSize(pTableListInfo->pTableList);
1,027,974✔
2840
  for (int32_t i = 0; i < size; ++i) {
4,043,252✔
2841
    STableKeyInfo* p = taosArrayGet(pTableListInfo->pTableList, i);
3,013,687✔
2842
    if (!p) {
3,012,556!
2843
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
2844
      return terrno;
×
2845
    }
2846
    int32_t        tempRes = taosHashPut(pTableListInfo->map, &p->uid, sizeof(uint64_t), &i, sizeof(int32_t));
3,012,556✔
2847
    if (tempRes != TSDB_CODE_SUCCESS && tempRes != TSDB_CODE_DUP_KEY) {
3,015,413!
2848
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(tempRes));
×
2849
      return tempRes;
×
2850
    }
2851
  }
2852

2853
  return code;
1,029,565✔
2854
}
2855

2856
int32_t createScanTableListInfo(SScanPhysiNode* pScanNode, SNodeList* pGroupTags, bool groupSort, SReadHandle* pHandle,
1,066,579✔
2857
                                STableListInfo* pTableListInfo, SNode* pTagCond, SNode* pTagIndexCond,
2858
                                SExecTaskInfo* pTaskInfo) {
2859
  int64_t     st = taosGetTimestampUs();
1,067,948✔
2860
  const char* idStr = GET_TASKID(pTaskInfo);
1,067,948✔
2861

2862
  if (pHandle == NULL) {
1,067,948!
2863
    qError("invalid handle, in creating operator tree, %s", idStr);
×
2864
    return TSDB_CODE_INVALID_PARA;
×
2865
  }
2866

2867
  uint8_t digest[17] = {0};
1,067,948✔
2868
  int32_t code = getTableList(pHandle->vnode, pScanNode, pTagCond, pTagIndexCond, pTableListInfo, digest, idStr,
1,067,948✔
2869
                              &pTaskInfo->storageAPI);
2870
  if (code != TSDB_CODE_SUCCESS) {
1,068,018✔
2871
    qError("failed to getTableList, code: %s", tstrerror(code));
8!
2872
    return code;
8✔
2873
  }
2874

2875
  int32_t numOfTables = taosArrayGetSize(pTableListInfo->pTableList);
1,068,010✔
2876

2877
  int64_t st1 = taosGetTimestampUs();
1,068,396✔
2878
  pTaskInfo->cost.extractListTime = (st1 - st) / 1000.0;
1,068,396✔
2879
  qDebug("extract queried table list completed, %d tables, elapsed time:%.2f ms %s", numOfTables,
1,068,396✔
2880
         pTaskInfo->cost.extractListTime, idStr);
2881

2882
  if (numOfTables == 0) {
1,068,476✔
2883
    qDebug("no table qualified for query, %s" PRIx64, idStr);
39,681✔
2884
    return TSDB_CODE_SUCCESS;
39,682✔
2885
  }
2886

2887
  code = buildGroupIdMapForAllTables(pTableListInfo, pHandle, pScanNode, pGroupTags, groupSort, digest,
1,028,795✔
2888
                                     &pTaskInfo->storageAPI);
2889
  if (code != TSDB_CODE_SUCCESS) {
1,029,056✔
2890
    return code;
14✔
2891
  }
2892

2893
  pTaskInfo->cost.groupIdMapTime = (taosGetTimestampUs() - st1) / 1000.0;
1,029,048✔
2894
  qDebug("generate group id map completed, elapsed time:%.2f ms %s", pTaskInfo->cost.groupIdMapTime, idStr);
1,029,048✔
2895

2896
  return TSDB_CODE_SUCCESS;
1,028,999✔
2897
}
2898

2899
char* getStreamOpName(uint16_t opType) {
1,621,853✔
2900
  switch (opType) {
1,621,853!
2901
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN:
49,455✔
2902
      return "stream scan";
49,455✔
2903
    case QUERY_NODE_PHYSICAL_PLAN_PROJECT:
13,113✔
2904
      return "project";
13,113✔
2905
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL:
788,641✔
2906
      return "interval single";
788,641✔
2907
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_INTERVAL:
9,889✔
2908
      return "interval final";
9,889✔
2909
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_INTERVAL:
28,050✔
2910
      return "interval semi";
28,050✔
2911
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_MID_INTERVAL:
338✔
2912
      return "interval mid";
338✔
2913
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FILL:
5,001✔
2914
      return "stream fill";
5,001✔
2915
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SESSION:
6,948✔
2916
      return "session single";
6,948✔
2917
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_SESSION:
861✔
2918
      return "session semi";
861✔
2919
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_SESSION:
611✔
2920
      return "session final";
611✔
2921
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_STATE:
4,407✔
2922
      return "state single";
4,407✔
2923
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_PARTITION:
730,290✔
2924
      return "stream partitionby";
730,290✔
2925
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_EVENT:
246✔
2926
      return "stream event";
246✔
2927
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_COUNT:
627✔
2928
      return "stream count";
627✔
2929
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERP_FUNC:
7,435✔
2930
      return "stream interp";
7,435✔
2931
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_CONTINUE_INTERVAL:
4,494✔
2932
      return "interval continue";
4,494✔
2933
  }
2934
  return "";
×
2935
}
2936

2937
void printDataBlock(SSDataBlock* pBlock, const char* flag, const char* taskIdStr) {
785,711✔
2938
  if (!pBlock) {
785,711!
2939
    qDebug("%s===stream===%s: Block is Null", taskIdStr, flag);
×
2940
    return;
×
2941
  } else if (pBlock->info.rows == 0) {
785,711✔
2942
    qDebug("%s===stream===%s: Block is Empty. block type %d", taskIdStr, flag, pBlock->info.type);
10,229!
2943
    return;
10,229✔
2944
  }
2945
  if (qDebugFlag & DEBUG_DEBUG) {
775,482✔
2946
    char*   pBuf = NULL;
48,590✔
2947
    int32_t code = dumpBlockData(pBlock, flag, &pBuf, taskIdStr);
48,590✔
2948
    if (code == 0) {
48,589!
2949
      qDebug("%s", pBuf);
48,589!
2950
      taosMemoryFree(pBuf);
48,590!
2951
    }
2952
  }
2953
}
2954

2955
void printSpecDataBlock(SSDataBlock* pBlock, const char* flag, const char* opStr, const char* taskIdStr) {
795,799✔
2956
  if (!pBlock) {
795,799!
2957
    qDebug("%s===stream===%s %s: Block is Null", taskIdStr, flag, opStr);
×
2958
    return;
×
2959
  } else if (pBlock->info.rows == 0) {
795,799✔
2960
    qDebug("%s===stream===%s %s: Block is Empty. block type %d.skey:%" PRId64 ",ekey:%" PRId64 ",version%" PRId64,
9,383✔
2961
           taskIdStr, flag, opStr, pBlock->info.type, pBlock->info.window.skey, pBlock->info.window.ekey,
2962
           pBlock->info.version);
2963
    return;
9,383✔
2964
  }
2965
  if (qDebugFlag & DEBUG_DEBUG) {
786,416✔
2966
    char* pBuf = NULL;
60,270✔
2967
    char  flagBuf[64];
2968
    snprintf(flagBuf, sizeof(flagBuf), "%s %s", flag, opStr);
60,270✔
2969
    int32_t code = dumpBlockData(pBlock, flagBuf, &pBuf, taskIdStr);
60,270✔
2970
    if (code == 0) {
60,271!
2971
      qDebug("%s", pBuf);
60,271!
2972
      taosMemoryFree(pBuf);
60,271!
2973
    }
2974
  }
2975
}
2976

2977
TSKEY getStartTsKey(STimeWindow* win, const TSKEY* tsCols) { return tsCols == NULL ? win->skey : tsCols[0]; }
1,742,954!
2978

2979
void updateTimeWindowInfo(SColumnInfoData* pColData, STimeWindow* pWin, int64_t delta) {
115,608,996✔
2980
  int64_t* ts = (int64_t*)pColData->pData;
115,608,996✔
2981

2982
  int64_t duration = pWin->ekey - pWin->skey + delta;
115,608,996✔
2983
  ts[2] = duration;            // set the duration
115,608,996✔
2984
  ts[3] = pWin->skey;          // window start key
115,608,996✔
2985
  ts[4] = pWin->ekey + delta;  // window end key
115,608,996✔
2986
}
115,608,996✔
2987

2988
int32_t compKeys(const SArray* pSortGroupCols, const char* oldkeyBuf, int32_t oldKeysLen, const SSDataBlock* pBlock,
21,376,259✔
2989
                 int32_t rowIndex) {
2990
  SColumnDataAgg* pColAgg = NULL;
21,376,259✔
2991
  const char*     isNull = oldkeyBuf;
21,376,259✔
2992
  const char*     p = oldkeyBuf + sizeof(int8_t) * pSortGroupCols->size;
21,376,259✔
2993

2994
  for (int32_t i = 0; i < pSortGroupCols->size; ++i) {
53,710,968✔
2995
    const SColumn*         pCol = (SColumn*)TARRAY_GET_ELEM(pSortGroupCols, i);
32,817,650✔
2996
    const SColumnInfoData* pColInfoData = TARRAY_GET_ELEM(pBlock->pDataBlock, pCol->slotId);
32,817,650✔
2997
    if (pBlock->pBlockAgg) pColAgg = &pBlock->pBlockAgg[pCol->slotId];
32,817,650!
2998

2999
    if (colDataIsNull(pColInfoData, pBlock->info.rows, rowIndex, pColAgg)) {
65,635,300!
3000
      if (isNull[i] != 1) return 1;
2,186,345✔
3001
    } else {
3002
      if (isNull[i] != 0) return 1;
30,631,305✔
3003
      const char* val = colDataGetData(pColInfoData, rowIndex);
30,617,082!
3004
      if (pCol->type == TSDB_DATA_TYPE_JSON) {
30,617,082!
3005
        int32_t len = getJsonValueLen(val);
×
3006
        if (memcmp(p, val, len) != 0) return 1;
×
3007
        p += len;
×
3008
      } else if (IS_VAR_DATA_TYPE(pCol->type)) {
30,617,082!
3009
        if (memcmp(p, val, varDataTLen(val)) != 0) return 1;
10,413,749✔
3010
        p += varDataTLen(val);
10,277,390✔
3011
      } else {
3012
        if (0 != memcmp(p, val, pCol->bytes)) return 1;
20,203,333✔
3013
        p += pCol->bytes;
19,871,166✔
3014
      }
3015
    }
3016
  }
3017
  if ((int32_t)(p - oldkeyBuf) != oldKeysLen) return 1;
20,893,318✔
3018
  return 0;
20,893,022✔
3019
}
3020

3021
int32_t buildKeys(char* keyBuf, const SArray* pSortGroupCols, const SSDataBlock* pBlock, int32_t rowIndex) {
482,353✔
3022
  uint32_t        colNum = pSortGroupCols->size;
482,353✔
3023
  SColumnDataAgg* pColAgg = NULL;
482,353✔
3024
  char*           isNull = keyBuf;
482,353✔
3025
  char*           p = keyBuf + sizeof(int8_t) * colNum;
482,353✔
3026

3027
  for (int32_t i = 0; i < colNum; ++i) {
1,437,299✔
3028
    const SColumn*         pCol = (SColumn*)TARRAY_GET_ELEM(pSortGroupCols, i);
954,946✔
3029
    const SColumnInfoData* pColInfoData = TARRAY_GET_ELEM(pBlock->pDataBlock, pCol->slotId);
954,946✔
3030
    if (pCol->slotId > pBlock->pDataBlock->size) continue;
954,946!
3031

3032
    if (pBlock->pBlockAgg) pColAgg = &pBlock->pBlockAgg[pCol->slotId];
954,946!
3033

3034
    if (colDataIsNull(pColInfoData, pBlock->info.rows, rowIndex, pColAgg)) {
1,909,892!
3035
      isNull[i] = 1;
34,575✔
3036
    } else {
3037
      isNull[i] = 0;
920,371✔
3038
      const char* val = colDataGetData(pColInfoData, rowIndex);
920,371!
3039
      if (pCol->type == TSDB_DATA_TYPE_JSON) {
920,371!
3040
        int32_t len = getJsonValueLen(val);
×
3041
        memcpy(p, val, len);
×
3042
        p += len;
×
3043
      } else if (IS_VAR_DATA_TYPE(pCol->type)) {
920,371!
3044
        varDataCopy(p, val);
138,000✔
3045
        p += varDataTLen(val);
138,000✔
3046
      } else {
3047
        memcpy(p, val, pCol->bytes);
782,371✔
3048
        p += pCol->bytes;
782,371✔
3049
      }
3050
    }
3051
  }
3052
  return (int32_t)(p - keyBuf);
482,353✔
3053
}
3054

3055
uint64_t calcGroupId(char* pData, int32_t len) {
12,086,561✔
3056
  T_MD5_CTX context;
3057
  tMD5Init(&context);
12,086,561✔
3058
  tMD5Update(&context, (uint8_t*)pData, len);
12,114,040✔
3059
  tMD5Final(&context);
12,220,277✔
3060

3061
  // NOTE: only extract the initial 8 bytes of the final MD5 digest
3062
  uint64_t id = 0;
12,357,664✔
3063
  memcpy(&id, context.digest, sizeof(uint64_t));
12,357,664✔
3064
  if (0 == id) memcpy(&id, context.digest + 8, sizeof(uint64_t));
12,357,664!
3065
  return id;
12,357,664✔
3066
}
3067

3068
SNodeList* makeColsNodeArrFromSortKeys(SNodeList* pSortKeys) {
941✔
3069
  SNode*     node;
3070
  SNodeList* ret = NULL;
941✔
3071
  FOREACH(node, pSortKeys) {
2,876!
3072
    SOrderByExprNode* pSortKey = (SOrderByExprNode*)node;
1,933✔
3073
    int32_t           code = nodesListMakeAppend(&ret, pSortKey->pExpr);
1,933✔
3074
    if (code != TSDB_CODE_SUCCESS) {
1,935!
3075
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
3076
      terrno = code;
×
3077
      return NULL;
×
3078
    }
3079
  }
3080
  return ret;
943✔
3081
}
3082

3083
int32_t extractKeysLen(const SArray* keys, int32_t* pLen) {
944✔
3084
  int32_t code = TSDB_CODE_SUCCESS;
944✔
3085
  int32_t lino = 0;
944✔
3086
  int32_t len = 0;
944✔
3087
  int32_t keyNum = taosArrayGetSize(keys);
944✔
3088
  for (int32_t i = 0; i < keyNum; ++i) {
2,400✔
3089
    SColumn* pCol = (SColumn*)taosArrayGet(keys, i);
1,456✔
3090
    QUERY_CHECK_NULL(pCol, code, lino, _end, terrno);
1,456!
3091
    len += pCol->bytes;
1,456✔
3092
  }
3093
  len += sizeof(int8_t) * keyNum;  // null flag
944✔
3094
  *pLen = len;
944✔
3095

3096
_end:
944✔
3097
  if (code != TSDB_CODE_SUCCESS) {
944!
3098
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
3099
  }
3100
  return code;
944✔
3101
}
3102

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