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

taosdata / TDengine / #3587

23 Jan 2025 02:42AM UTC coverage: 63.549% (-0.09%) from 63.643%
#3587

push

travis-ci

web-flow
Merge pull request #29637 from taosdata/docs/TS-5944

docs/TS-5944 Correct typos in the descriptions of maximum and minimum values for taosBenchmark

141306 of 285630 branches covered (49.47%)

Branch coverage included in aggregate %.

219951 of 282844 relevant lines covered (77.76%)

19107446.53 hits per line

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

69.67
/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);
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 ? -1 : ((SLimitNode*)pLimit)->limit; }
27,497,084✔
58
static int64_t getOffset(const SNode* pLimit) { return NULL == pLimit ? -1 : ((SLimitNode*)pLimit)->offset; }
27,508,516✔
59
static void    releaseColInfoData(void* pCol);
60

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

66
void closeResultRow(SResultRow* pResultRow) { pResultRow->closed = true; }
8,853,417✔
67

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

74
  if (entrySize > 0) {
67,106,030!
75
    memset(pResultRow->pEntryInfo, 0, entrySize);
67,111,141✔
76
  }
77
}
67,106,030✔
78

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

84
size_t getResultRowSize(SqlFunctionCtx* pCtx, int32_t numOfOutput) {
8,456,950✔
85
  int32_t rowSize = (numOfOutput * sizeof(SResultRowEntryInfo)) + sizeof(SResultRow);
8,456,950✔
86

87
  for (int32_t i = 0; i < numOfOutput; ++i) {
37,496,155✔
88
    rowSize += pCtx[i].resDataInfo.interBufSize;
29,039,205✔
89
  }
90

91
  return rowSize;
8,456,950✔
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) {
2,787✔
96
  if (inBuf == NULL || pSup == NULL) {
2,787!
97
    qError("invalid input parameters, inBuf:%p, pSup:%p", inBuf, pSup);
16!
98
    return TSDB_CODE_INVALID_PARA;
16✔
99
  }
100
  SqlFunctionCtx *pCtx = pSup->pCtx;
2,771✔
101
  int32_t        *offset = pSup->rowEntryInfoOffset;
2,771✔
102
  SResultRow     *pResultRow  = NULL;
2,771✔
103
  size_t          processedSize = 0;
2,771✔
104
  int32_t         code = TSDB_CODE_SUCCESS;
2,771✔
105

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

118
  for (int32_t i = 0; i < pSup->numOfExprs; ++i) {
44,070✔
119
    int32_t len = *(int32_t*)inBuf;
41,301✔
120
    inBuf += sizeof(int32_t);
41,301✔
121
    processedSize += sizeof(int32_t);
41,301✔
122
    if (pResultRow->version != FUNCTION_RESULT_INFO_VERSION && pCtx->fpSet.decode) {
41,301!
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);
41,301✔
130
    }
131
    inBuf += len;
41,299✔
132
    processedSize += len;
41,299✔
133
  }
134

135
  if (processedSize < inBufSize) {
2,769✔
136
    // stream stores extra data after result row
137
    size_t leftLen = inBufSize - processedSize;
1,017✔
138
    TAOS_MEMORY_REALLOC(*outBuf, *outBufSize + leftLen);
1,017!
139
    if (*outBuf == NULL) {
1,017!
140
      qError("failed to reallocate memory for output buffer, size:%zu", *outBufSize + leftLen);
×
141
      return terrno;
×
142
    }
143
    (void)memcpy(*outBuf + *outBufSize, inBuf, leftLen);
1,017✔
144
    inBuf += leftLen;
1,017✔
145
    processedSize += leftLen;
1,017✔
146
    *outBufSize += leftLen;
1,017✔
147
  }
148
  return TSDB_CODE_SUCCESS;
2,769✔
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) {
1,800,868✔
153
  if (pSup == NULL || inBuf == NULL || outBuf == NULL || outBufSize == NULL) {
1,800,868!
154
    qError("invalid input parameters, inBuf:%p, pSup:%p, outBufSize:%p, outBuf:%p", inBuf, pSup, outBufSize, outBuf);
4!
155
    return TSDB_CODE_INVALID_PARA;
×
156
  }
157

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

163
  if (rowSize > inBufSize) {
1,800,659!
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;
1,800,659✔
170
  if (rowSize < inBufSize) {
1,800,659✔
171
    *outBufSize += inBufSize - rowSize;
1,408✔
172
  }
173

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

180
  char *pBuf = *outBuf;
1,800,461✔
181
  pResultRow->version = FUNCTION_RESULT_INFO_VERSION;
1,800,461✔
182
  (void)memcpy(pBuf, pResultRow, sizeof(SResultRow));
1,800,461✔
183
  pBuf += sizeof(SResultRow);
1,800,461✔
184
  for (int32_t i = 0; i < pSup->numOfExprs; ++i) {
11,092,277✔
185
    size_t len = sizeof(SResultRowEntryInfo) + pCtx[i].resDataInfo.interBufSize;
9,292,720✔
186
    *(int32_t *) pBuf = (int32_t)len;
9,292,720✔
187
    pBuf += sizeof(int32_t);
9,292,720✔
188
    (void)memcpy(pBuf, getResultEntryInfo(pResultRow, i, offset), len);
9,292,720✔
189
    pBuf += len;
9,291,816✔
190
  }
191

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

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

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

217
int32_t resultrowComparAsc(const void* p1, const void* p2) {
2,001,838,439✔
218
  SResKeyPos* pp1 = *(SResKeyPos**)p1;
2,001,838,439✔
219
  SResKeyPos* pp2 = *(SResKeyPos**)p2;
2,001,838,439✔
220

221
  if (pp1->groupId == pp2->groupId) {
2,001,838,439✔
222
    int64_t pts1 = *(int64_t*)pp1->key;
1,837,477,659✔
223
    int64_t pts2 = *(int64_t*)pp2->key;
1,837,477,659✔
224

225
    if (pts1 == pts2) {
1,837,477,659!
226
      return 0;
×
227
    } else {
228
      return pts1 < pts2 ? -1 : 1;
1,837,477,659✔
229
    }
230
  } else {
231
    return pp1->groupId < pp2->groupId ? -1 : 1;
164,360,780✔
232
  }
233
}
234

235
static int32_t resultrowComparDesc(const void* p1, const void* p2) { return resultrowComparAsc(p2, p1); }
66,664,591✔
236

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

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

251
  void* pData = NULL;
3,597,390✔
252
  pGroupResInfo->pRows = taosArrayInit(size, POINTER_BYTES);
3,597,390✔
253
  QUERY_CHECK_NULL(pGroupResInfo->pRows, code, lino, _end, terrno);
3,598,468!
254

255
  size_t  keyLen = 0;
3,598,468✔
256
  int32_t iter = 0;
3,598,468✔
257
  int64_t bufLen = 0, offset = 0;
3,598,468✔
258

259
  // todo move away and record this during create window
260
  while ((pData = tSimpleHashIterate(pHashmap, pData, &iter)) != NULL) {
222,878,434✔
261
    /*void* key = */ (void)tSimpleHashGetKey(pData, &keyLen);
262
    bufLen += keyLen + sizeof(SResultRowPosition);
219,279,966✔
263
  }
264

265
  pGroupResInfo->pBuf = taosMemoryMalloc(bufLen);
3,590,526!
266
  QUERY_CHECK_NULL(pGroupResInfo->pBuf, code, lino, _end, terrno);
3,598,417!
267

268
  iter = 0;
3,598,417✔
269
  while ((pData = tSimpleHashIterate(pHashmap, pData, &iter)) != NULL) {
222,555,592✔
270
    void* key = tSimpleHashGetKey(pData, &keyLen);
219,052,240✔
271

272
    SResKeyPos* p = (SResKeyPos*)(pGroupResInfo->pBuf + offset);
219,052,240✔
273

274
    p->groupId = *(uint64_t*)key;
219,052,240✔
275
    p->pos = *(SResultRowPosition*)pData;
219,052,240✔
276
    memcpy(p->key, (char*)key + sizeof(uint64_t), keyLen - sizeof(uint64_t));
219,052,240✔
277
    void* tmp = taosArrayPush(pGroupResInfo->pRows, &p);
219,052,240✔
278
    QUERY_CHECK_NULL(pGroupResInfo->pBuf, code, lino, _end, terrno);
218,957,175!
279

280
    offset += keyLen + sizeof(struct SResultRowPosition);
218,957,175✔
281
  }
282

283
  if (order == TSDB_ORDER_ASC || order == TSDB_ORDER_DESC) {
3,607,373✔
284
    __compar_fn_t fn = (order == TSDB_ORDER_ASC) ? resultrowComparAsc : resultrowComparDesc;
1,657,525✔
285
    size = POINTER_BYTES;
1,657,525✔
286
    taosSort(pGroupResInfo->pRows->pData, taosArrayGetSize(pGroupResInfo->pRows), size, fn);
1,657,525✔
287
  }
288

289
  pGroupResInfo->index = 0;
3,598,888✔
290

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

298
void initMultiResInfoFromArrayList(SGroupResInfo* pGroupResInfo, SArray* pArrayList) {
35,152✔
299
  if (pGroupResInfo->pRows != NULL) {
35,152✔
300
    taosArrayDestroy(pGroupResInfo->pRows);
31,163✔
301
  }
302

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

308
bool hasRemainResults(SGroupResInfo* pGroupResInfo) {
10,545,353✔
309
  if (pGroupResInfo->pRows == NULL) {
10,545,353✔
310
    return false;
1,507✔
311
  }
312

313
  return pGroupResInfo->index < taosArrayGetSize(pGroupResInfo->pRows);
10,543,846✔
314
}
315

316
int32_t getNumOfTotalRes(SGroupResInfo* pGroupResInfo) {
7,000,079✔
317
  if (pGroupResInfo->pRows == 0) {
7,000,079✔
318
    return 0;
1,772✔
319
  }
320

321
  return (int32_t)taosArrayGetSize(pGroupResInfo->pRows);
6,998,307✔
322
}
323

324
SArray* createSortInfo(SNodeList* pNodeList) {
2,283,626✔
325
  size_t numOfCols = 0;
2,283,626✔
326

327
  if (pNodeList != NULL) {
2,283,626✔
328
    numOfCols = LIST_LENGTH(pNodeList);
2,283,393!
329
  } else {
330
    numOfCols = 0;
233✔
331
  }
332

333
  SArray* pList = taosArrayInit(numOfCols, sizeof(SBlockOrderInfo));
2,283,626✔
334
  if (pList == NULL) {
2,283,729!
335
    return pList;
×
336
  }
337

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

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

361
  return pList;
2,283,765✔
362
}
363

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

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

378
  for (int32_t i = 0; i < numOfCols; ++i) {
110,935,699✔
379
    SSlotDescNode*  pDescNode = (SSlotDescNode*)nodesListGetNode(pNode->pSlots, i);
90,187,507✔
380
    if (!pDescNode) {
90,305,550!
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);
90,305,550✔
389
    idata.info.scale = pDescNode->dataType.scale;
90,399,717✔
390
    idata.info.precision = pDescNode->dataType.precision;
90,399,717✔
391
    idata.info.noData = pDescNode->reserve;
90,399,717✔
392

393
    code = blockDataAppendColInfo(pBlock, &idata);
90,399,717✔
394
    if (code != TSDB_CODE_SUCCESS) {
90,192,534!
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;
20,748,192✔
404
}
405

406
int32_t prepareDataBlockBuf(SSDataBlock* pDataBlock, SColMatchInfo* pMatchInfo) {
4,800,755✔
407
  SDataBlockInfo* pBlockInfo = &pDataBlock->info;
4,800,755✔
408

409
  for (int32_t i = 0; i < taosArrayGetSize(pMatchInfo->pList); ++i) {
27,789,551✔
410
    SColMatchItem* pItem = taosArrayGet(pMatchInfo->pList, i);
23,120,502✔
411
    if (!pItem) {
23,131,552✔
412
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
1,802!
413
      return terrno;
1,802✔
414
    }
415

416
    if (pItem->isPk) {
23,129,750✔
417
      SColumnInfoData* pInfoData = taosArrayGet(pDataBlock->pDataBlock, pItem->dstSlotId);
140,954✔
418
      if (!pInfoData) {
140,953!
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;
140,953✔
423
      pBlockInfo->pks[1].type = pInfoData->info.type;
140,953✔
424

425
      // allocate enough buffer size, which is pInfoData->info.bytes
426
      if (IS_VAR_DATA_TYPE(pItem->dataType.type)) {
140,953!
427
        pBlockInfo->pks[0].pData = taosMemoryCalloc(1, pInfoData->info.bytes);
41!
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;
140,945✔
443
    }
444
  }
445

446
  return TSDB_CODE_SUCCESS;
4,798,818✔
447
}
448

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

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

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

475
    STagVal tagVal = {0};
1,836✔
476
    tagVal.cid = pSColumnNode->colId;
1,836✔
477
    const char* p = mr->pAPI->extractTagVal(mr->me.ctbEntry.pTags, pSColumnNode->node.resType.type, &tagVal);
1,836✔
478
    if (p == NULL) {
1,836!
479
      res->node.resType.type = TSDB_DATA_TYPE_NULL;
×
480
    } else if (pSColumnNode->node.resType.type == TSDB_DATA_TYPE_JSON) {
1,836!
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,836!
488
      res->datum.p = taosMemoryCalloc(tagVal.nData + VARSTR_HEADER_SIZE + 1, 1);
1,813!
489
      if (NULL == res->datum.p) {
1,813!
490
        return DEAL_RES_ERROR;
×
491
      }
492
      memcpy(varDataVal(res->datum.p), tagVal.pData, tagVal.nData);
1,813✔
493
      varDataSetLen(res->datum.p, tagVal.nData);
1,813✔
494
    } else {
495
      int32_t code = nodesSetValueNodeValue(res, &(tagVal.i64));
23✔
496
      if (code != TSDB_CODE_SUCCESS) {
23!
497
        return DEAL_RES_ERROR;
×
498
      }
499
    }
500
    nodesDestroyNode(*pNode);
1,836✔
501
    *pNode = (SNode*)res;
1,836✔
502
  } else if (isTbname) {
4,552!
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;
6,388✔
524
}
525

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

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

536
    return TSDB_CODE_SUCCESS;
×
537
  }
538

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

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

562
    return code;
×
563
  }
564

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

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

572
static EDealRes getColumn(SNode** pNode, void* pContext) {
139,052,550✔
573
  tagFilterAssist* pData = (tagFilterAssist*)pContext;
139,052,550✔
574
  SColumnNode*     pSColumnNode = NULL;
139,052,550✔
575
  if (QUERY_NODE_COLUMN == nodeType((*pNode))) {
139,052,550✔
576
    pSColumnNode = *(SColumnNode**)pNode;
3,474,646✔
577
  } else if (QUERY_NODE_FUNCTION == nodeType((*pNode))) {
135,577,904✔
578
    SFunctionNode* pFuncNode = *(SFunctionNode**)(pNode);
446,706✔
579
    if (pFuncNode->funcType == FUNCTION_TYPE_TBNAME) {
446,706✔
580
      pData->code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&pSColumnNode);
435,674✔
581
      if (NULL == pSColumnNode) {
435,672!
582
        return DEAL_RES_ERROR;
×
583
      }
584
      pSColumnNode->colId = -1;
435,672✔
585
      pSColumnNode->colType = COLUMN_TYPE_TBNAME;
435,672✔
586
      pSColumnNode->node.resType.type = TSDB_DATA_TYPE_VARCHAR;
435,672✔
587
      pSColumnNode->node.resType.bytes = TSDB_TABLE_FNAME_LEN - 1 + VARSTR_HEADER_SIZE;
435,672✔
588
      nodesDestroyNode(*pNode);
435,672✔
589
      *pNode = (SNode*)pSColumnNode;
435,674✔
590
    } else {
591
      return DEAL_RES_CONTINUE;
11,032✔
592
    }
593
  } else {
594
    return DEAL_RES_CONTINUE;
135,131,198✔
595
  }
596

597
  void* data = taosHashGet(pData->colHash, &pSColumnNode->colId, sizeof(pSColumnNode->colId));
3,910,320✔
598
  if (!data) {
3,909,017✔
599
    int32_t tempRes =
600
        taosHashPut(pData->colHash, &pSColumnNode->colId, sizeof(pSColumnNode->colId), pNode, sizeof((*pNode)));
3,168,544✔
601
    if (tempRes != TSDB_CODE_SUCCESS && tempRes != TSDB_CODE_DUP_KEY) {
3,171,209!
602
      return DEAL_RES_ERROR;
×
603
    }
604
    pSColumnNode->slotId = pData->index++;
3,171,209✔
605
    SColumnInfo cInfo = {.colId = pSColumnNode->colId,
3,171,209✔
606
                         .type = pSColumnNode->node.resType.type,
3,171,209✔
607
                         .bytes = pSColumnNode->node.resType.bytes,
3,171,209✔
608
                         .pk = pSColumnNode->isPk};
3,171,209✔
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);
3,171,209✔
613
    if (!tmp) {
3,168,266!
614
      return DEAL_RES_ERROR;
×
615
    }
616
  } else {
617
    SColumnNode* col = *(SColumnNode**)data;
740,473✔
618
    pSColumnNode->slotId = col->slotId;
740,473✔
619
  }
620

621
  return DEAL_RES_CONTINUE;
3,908,739✔
622
}
623

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

630
  pColumnData->info.type = pType->type;
1,357,504✔
631
  pColumnData->info.bytes = pType->bytes;
1,357,504✔
632
  pColumnData->info.scale = pType->scale;
1,357,504✔
633
  pColumnData->info.precision = pType->precision;
1,357,504✔
634

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

642
  pParam->columnData = pColumnData;
1,357,369✔
643
  pParam->colAlloced = true;
1,357,369✔
644
  return TSDB_CODE_SUCCESS;
1,357,369✔
645
}
646

647
static void releaseColInfoData(void* pCol) {
75,906✔
648
  if (pCol) {
75,906!
649
    SColumnInfoData* col = (SColumnInfoData*)pCol;
75,906✔
650
    colDataDestroy(col);
75,906✔
651
    taosMemoryFree(col);
75,911!
652
  }
653
}
75,912✔
654

655
void freeItem(void* p) {
3,256,139✔
656
  STUidTagInfo* pInfo = p;
3,256,139✔
657
  if (pInfo->pTagVal != NULL) {
3,256,139✔
658
    taosMemoryFree(pInfo->pTagVal);
3,080,357!
659
  }
660
}
3,257,023✔
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,
63,746✔
711
                                   SStorageAPI* pAPI, bool initRemainGroups) {
712
  int32_t      code = TSDB_CODE_SUCCESS;
63,746✔
713
  int32_t      lino = 0;
63,746✔
714
  SArray*      pBlockList = NULL;
63,746✔
715
  SSDataBlock* pResBlock = NULL;
63,746✔
716
  void*        keyBuf = NULL;
63,746✔
717
  SArray*      groupData = NULL;
63,746✔
718
  SArray*      pUidTagList = NULL;
63,746✔
719
  SArray*      tableList = NULL;
63,746✔
720

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

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

733
  ctx.index = 0;
63,796✔
734
  ctx.cInfoList = taosArrayInit(4, sizeof(SColumnInfo));
63,796✔
735
  if (ctx.cInfoList == NULL) {
63,819✔
736
    code = terrno;
21✔
737
    goto end;
×
738
  }
739

740
  SNode* pNode = NULL;
63,798✔
741
  FOREACH(pNode, group) {
139,667!
742
    nodesRewriteExprPostOrder(&pNode, getColumn, (void*)&ctx);
75,827✔
743
    if (TSDB_CODE_SUCCESS != ctx.code) {
75,869!
744
      code = ctx.code;
×
745
      goto end;
×
746
    }
747
    REPLACE_NODE(pNode);
75,869✔
748
  }
749

750
  T_MD5_CTX context = {0};
63,840✔
751
  if (tsTagFilterCache) {
63,840!
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));
63,840✔
777
  QUERY_CHECK_NULL(pUidTagList, code, lino, end, terrno);
63,738!
778

779
  for (int32_t i = 0; i < rows; ++i) {
316,711✔
780
    STableKeyInfo* pkeyInfo = taosArrayGet(pTableListInfo->pTableList, i);
252,876✔
781
    QUERY_CHECK_NULL(pkeyInfo, code, lino, end, terrno);
252,814!
782
    STUidTagInfo   info = {.uid = pkeyInfo->uid};
252,814✔
783
    void*          tmp = taosArrayPush(pUidTagList, &info);
252,961✔
784
    QUERY_CHECK_NULL(tmp, code, lino, end, terrno);
252,961!
785
  }
786

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

792
  int32_t numOfTables = taosArrayGetSize(pUidTagList);
63,847✔
793
  pResBlock = createTagValBlockForFilter(ctx.cInfoList, numOfTables, pUidTagList, pVnode, pAPI);
63,839✔
794
  if (pResBlock == NULL) {
63,834!
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);
63,834✔
803
  QUERY_CHECK_NULL(pBlockList, code, lino, end, terrno);
63,832!
804

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

808
  groupData = taosArrayInit(2, POINTER_BYTES);
63,832✔
809
  QUERY_CHECK_NULL(groupData, code, lino, end, terrno);
63,833!
810

811
  FOREACH(pNode, group) {
139,723!
812
    SScalarParam output = {0};
75,891✔
813

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

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

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

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

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

853
  int32_t keyLen = 0;
63,832✔
854
  SNode*  node;
855
  FOREACH(node, group) {
139,727✔
856
    SExprNode* pExpr = (SExprNode*)node;
75,895✔
857
    keyLen += pExpr->resType.bytes;
75,895✔
858
  }
859

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

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

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

878
  for (int i = 0; i < rows; i++) {
316,724✔
879
    STableKeyInfo* info = taosArrayGet(pTableListInfo->pTableList, i);
252,860✔
880
    QUERY_CHECK_NULL(info, code, lino, end, terrno);
252,772✔
881

882
    char* isNull = (char*)keyBuf;
252,770✔
883
    char* pStart = (char*)keyBuf + sizeof(int8_t) * LIST_LENGTH(group);
252,770!
884
    for (int j = 0; j < taosArrayGetSize(groupData); j++) {
533,402✔
885
      SColumnInfoData* pValue = (SColumnInfoData*)taosArrayGetP(groupData, j);
280,762✔
886

887
      if (colDataIsNull_s(pValue, i)) {
561,294✔
888
        isNull[j] = 1;
426✔
889
      } else {
890
        isNull[j] = 0;
280,221✔
891
        char* data = colDataGetData(pValue, i);
280,221!
892
        if (pValue->info.type == TSDB_DATA_TYPE_JSON) {
280,221✔
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)) {
279,957!
905
          if (varDataTLen(data) > pValue->info.bytes) {
249,377✔
906
            code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
3✔
907
            goto end;
3✔
908
          }
909
          memcpy(pStart, data, varDataTLen(data));
249,374✔
910
          pStart += varDataTLen(data);
249,374✔
911
        } else {
912
          memcpy(pStart, data, pValue->info.bytes);
30,580✔
913
          pStart += pValue->info.bytes;
30,580✔
914
        }
915
      }
916
    }
917

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

931
  if (tsTagFilterCache) {
63,864!
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:
63,864✔
945
  taosMemoryFreeClear(keyBuf);
63,879!
946
  taosHashCleanup(ctx.colHash);
63,878✔
947
  taosArrayDestroy(ctx.cInfoList);
63,832✔
948
  blockDataDestroy(pResBlock);
63,835✔
949
  taosArrayDestroy(pBlockList);
63,839✔
950
  taosArrayDestroyEx(pUidTagList, freeItem);
63,837✔
951
  taosArrayDestroyP(groupData, releaseColInfoData);
63,843✔
952
  if (code != TSDB_CODE_SUCCESS) {
63,832✔
953
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
15!
954
  }
955
  return code;
63,811✔
956
}
957

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

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

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

976
  SArray* pTbList = taosArrayInit(len, POINTER_BYTES);
430,616✔
977
  QUERY_CHECK_NULL(pTbList, code, lino, _end, terrno);
430,620!
978

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

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

993
  size_t numOfTables = taosArrayGetSize(pTbList);
430,618✔
994

995
  // order the name
996
  taosArraySort(pTbList, nameComparFn);
430,616✔
997

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

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

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

1018
_end:
430,617✔
1019
  taosArrayDestroy(pTbList);
430,617✔
1020
  if (code != TSDB_CODE_SUCCESS) {
430,616✔
1021
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
1!
1022
    return NULL;
×
1023
  }
1024
  return pNewList;
430,615✔
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) {
3,290✔
1039
  STUidTagInfo* p1 = (STUidTagInfo*)a;
3,290✔
1040
  STUidTagInfo* p2 = (STUidTagInfo*)b;
3,290✔
1041

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

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

1049
static FilterCondType checkTagCond(SNode* cond) {
1,322,461✔
1050
  if (nodeType(cond) == QUERY_NODE_OPERATOR) {
1,322,461✔
1051
    return FILTER_NO_LOGIC;
323,326✔
1052
  }
1053
  if (nodeType(cond) != QUERY_NODE_LOGIC_CONDITION || ((SLogicConditionNode*)cond)->condType != LOGIC_COND_TYPE_AND) {
999,135!
1054
    return FILTER_AND;
9,517✔
1055
  }
1056
  return FILTER_OTHER;
989,618✔
1057
}
1058

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

1063
  if (ntype == QUERY_NODE_OPERATOR) {
1,322,686✔
1064
    ret = optimizeTbnameInCondImpl(pVnode, list, cond, pAPI);
323,368✔
1065
  }
1066

1067
  if (ntype != QUERY_NODE_LOGIC_CONDITION || ((SLogicConditionNode*)cond)->condType != LOGIC_COND_TYPE_AND) {
1,323,861✔
1068
    return ret;
334,319✔
1069
  }
1070

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

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

1080
  SListCell* cell = pList->pHead;
989,542✔
1081
  for (int i = 0; i < len; i++) {
3,344,530✔
1082
    if (cell == NULL) break;
2,609,081!
1083
    if (optimizeTbnameInCondImpl(pVnode, list, cell->pNode, pAPI) == 0) {
2,609,081✔
1084
      hasTbnameCond = true;
254,081✔
1085
      break;
254,081✔
1086
    }
1087
    cell = cell->pNext;
2,354,988✔
1088
  }
1089

1090
  taosArraySort(list, filterTableInfoCompare);
989,530✔
1091
  taosArrayRemoveDuplicate(list, filterTableInfoCompare, NULL);
987,752✔
1092

1093
  if (hasTbnameCond) {
988,264✔
1094
    ret = pAPI->metaFn.getTableTagsByUid(pVnode, suid, list);
254,081✔
1095
  }
1096

1097
  return ret;
988,349✔
1098
}
1099

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

1107
  SOperatorNode* pNode = (SOperatorNode*)pTagCond;
2,797,822✔
1108
  if (pNode->opType != OP_TYPE_IN) {
2,797,822✔
1109
    return -1;
1,881,613✔
1110
  }
1111

1112
  if ((pNode->pLeft != NULL && nodeType(pNode->pLeft) == QUERY_NODE_COLUMN &&
916,209!
1113
       ((SColumnNode*)pNode->pLeft)->colType == COLUMN_TYPE_TBNAME) &&
918,506✔
1114
      (pNode->pRight != NULL && nodeType(pNode->pRight) == QUERY_NODE_NODE_LIST)) {
430,618!
1115
    SNodeListNode* pList = (SNodeListNode*)pNode->pRight;
430,618✔
1116

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

1122
    SArray*   pTbList = getTableNameList(pList);
430,618✔
1123
    int32_t   numOfTables = taosArrayGetSize(pTbList);
430,615✔
1124
    SHashObj* uHash = NULL;
430,614✔
1125

1126
    size_t numOfExisted = taosArrayGetSize(pExistedUidList);  // len > 0 means there already have uids
430,614✔
1127
    if (numOfExisted > 0) {
430,612!
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++) {
862,896✔
1149
      char* name = taosArrayGetP(pTbList, i);
432,285✔
1150

1151
      uint64_t uid = 0;
432,284✔
1152
      if (pStoreAPI->metaFn.getTableUidByName(pVnode, name, &uid) == 0) {
432,284✔
1153
        ETableType tbType = TSDB_TABLE_MAX;
430,619✔
1154
        if (pStoreAPI->metaFn.getTableTypeByName(pVnode, name, &tbType) == 0 && tbType == TSDB_CHILD_TABLE) {
430,619!
1155
          if (NULL == uHash || taosHashGet(uHash, &uid, sizeof(uid)) == NULL) {
430,611!
1156
            STUidTagInfo s = {.uid = uid, .name = name, .pTagVal = NULL};
430,612✔
1157
            void*        tmp = taosArrayPush(pExistedUidList, &s);
430,612✔
1158
            if (!tmp) {
430,612!
1159
              return terrno;
×
1160
            }
1161
          }
1162
        } else {
1163
          taosArrayDestroy(pTbList);
11✔
1164
          taosHashCleanup(uHash);
11✔
1165
          return -1;
11✔
1166
        }
1167
      } else {
1168
        //        qWarn("failed to get tableIds from by table name: %s, reason: %s", name, tstrerror(terrno));
1169
        terrno = 0;
1,673✔
1170
      }
1171
    }
1172

1173
    taosHashCleanup(uHash);
430,611✔
1174
    taosArrayDestroy(pTbList);
430,606✔
1175
    return 0;
430,605✔
1176
  }
1177

1178
  return -1;
485,591✔
1179
}
1180

1181
SSDataBlock* createTagValBlockForFilter(SArray* pColList, int32_t numOfTables, SArray* pUidTagList, void* pVnode,
1,359,549✔
1182
                                        SStorageAPI* pStorageAPI) {
1183
  int32_t      code = TSDB_CODE_SUCCESS;
1,359,549✔
1184
  int32_t      lino = 0;
1,359,549✔
1185
  SSDataBlock* pResBlock = NULL;
1,359,549✔
1186
  code = createDataBlock(&pResBlock);
1,359,549✔
1187
  QUERY_CHECK_CODE(code, lino, _end);
1,360,097!
1188

1189
  for (int32_t i = 0; i < taosArrayGetSize(pColList); ++i) {
4,443,687✔
1190
    SColumnInfoData colInfo = {0};
3,082,731✔
1191
    void* tmp = taosArrayGet(pColList, i);
3,082,731✔
1192
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
3,080,855!
1193
    colInfo.info = *(SColumnInfo*)tmp;
3,080,855✔
1194
    code = blockDataAppendColInfo(pResBlock, &colInfo);
3,080,855✔
1195
    QUERY_CHECK_CODE(code, lino, _end);
3,083,590!
1196
  }
1197

1198
  code = blockDataEnsureCapacity(pResBlock, numOfTables);
1,357,872✔
1199
  if (code != TSDB_CODE_SUCCESS) {
1,360,437!
1200
    terrno = code;
×
1201
    blockDataDestroy(pResBlock);
×
1202
    return NULL;
×
1203
  }
1204

1205
  pResBlock->info.rows = numOfTables;
1,360,437✔
1206

1207
  int32_t numOfCols = taosArrayGetSize(pResBlock->pDataBlock);
1,360,437✔
1208

1209
  for (int32_t i = 0; i < numOfTables; i++) {
4,639,556✔
1210
    STUidTagInfo* p1 = taosArrayGet(pUidTagList, i);
3,273,194✔
1211
    QUERY_CHECK_NULL(p1, code, lino, _end, terrno);
3,270,874!
1212

1213
    for (int32_t j = 0; j < numOfCols; j++) {
10,266,982✔
1214
      SColumnInfoData* pColInfo = (SColumnInfoData*)taosArrayGet(pResBlock->pDataBlock, j);
6,986,924✔
1215
      QUERY_CHECK_NULL(pColInfo, code, lino, _end, terrno);
6,979,340✔
1216

1217
      if (pColInfo->info.colId == -1) {  // tbname
6,979,114✔
1218
        char str[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
447,286✔
1219
        if (p1->name != NULL) {
447,286✔
1220
          STR_TO_VARSTR(str, p1->name);
430,610✔
1221
        } else {  // name is not retrieved during filter
1222
          code = pStorageAPI->metaFn.getTableNameByUid(pVnode, p1->uid, str);
16,676✔
1223
          QUERY_CHECK_CODE(code, lino, _end);
16,821!
1224
        }
1225

1226
        code = colDataSetVal(pColInfo, i, str, false);
447,431✔
1227
        QUERY_CHECK_CODE(code, lino, _end);
447,340!
1228
#if TAG_FILTER_DEBUG
1229
        qDebug("tagfilter uid:%ld, tbname:%s", *uid, str + 2);
1230
#endif
1231
      } else {
1232
        STagVal tagVal = {0};
6,531,828✔
1233
        tagVal.cid = pColInfo->info.colId;
6,531,828✔
1234
        if (p1->pTagVal == NULL) {
6,531,828!
1235
          colDataSetNULL(pColInfo, i);
×
1236
        } else {
1237
          const char* p = pStorageAPI->metaFn.extractTagVal(p1->pTagVal, pColInfo->info.type, &tagVal);
6,531,828✔
1238

1239
          if (p == NULL || (pColInfo->info.type == TSDB_DATA_TYPE_JSON && ((STag*)p)->nTag == 0)) {
6,561,777!
1240
            colDataSetNULL(pColInfo, i);
2,962✔
1241
          } else if (pColInfo->info.type == TSDB_DATA_TYPE_JSON) {
6,558,815✔
1242
            code = colDataSetVal(pColInfo, i, p, false);
4,860✔
1243
            QUERY_CHECK_CODE(code, lino, _end);
4,860!
1244
          } else if (IS_VAR_DATA_TYPE(pColInfo->info.type)) {
8,293,476!
1245
            char* tmp = taosMemoryMalloc(tagVal.nData + VARSTR_HEADER_SIZE + 1);
1,740,874✔
1246
            QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,738,771!
1247
            varDataSetLen(tmp, tagVal.nData);
1,738,771✔
1248
            memcpy(tmp + VARSTR_HEADER_SIZE, tagVal.pData, tagVal.nData);
1,738,771✔
1249
            code = colDataSetVal(pColInfo, i, tmp, false);
1,738,771✔
1250
#if TAG_FILTER_DEBUG
1251
            qDebug("tagfilter varch:%s", tmp + 2);
1252
#endif
1253
            taosMemoryFree(tmp);
1,737,685!
1254
            QUERY_CHECK_CODE(code, lino, _end);
1,739,521!
1255
          } else {
1256
            code = colDataSetVal(pColInfo, i, (const char*)&tagVal.i64, false);
4,813,081✔
1257
            QUERY_CHECK_CODE(code, lino, _end);
4,801,066!
1258
#if TAG_FILTER_DEBUG
1259
            if (pColInfo->info.type == TSDB_DATA_TYPE_INT) {
1260
              qDebug("tagfilter int:%d", *(int*)(&tagVal.i64));
1261
            } else if (pColInfo->info.type == TSDB_DATA_TYPE_DOUBLE) {
1262
              qDebug("tagfilter double:%f", *(double*)(&tagVal.i64));
1263
            }
1264
#endif
1265
          }
1266
        }
1267
      }
1268
    }
1269
  }
1270

1271
_end:
1,366,362✔
1272
  if (code != TSDB_CODE_SUCCESS) {
1,366,362!
1273
    blockDataDestroy(pResBlock);
×
1274
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1275
    terrno = code;
×
1276
    return NULL;
×
1277
  }
1278
  return pResBlock;
1,366,362✔
1279
}
1280

1281
static int32_t doSetQualifiedUid(STableListInfo* pListInfo, SArray* pUidList, const SArray* pUidTagList,
1,279,646✔
1282
                                 bool* pResultList, bool addUid) {
1283
  taosArrayClear(pUidList);
1,279,646✔
1284

1285
  STableKeyInfo info = {.uid = 0, .groupId = 0};
1,279,860✔
1286
  int32_t       numOfTables = taosArrayGetSize(pUidTagList);
1,279,860✔
1287
  for (int32_t i = 0; i < numOfTables; ++i) {
4,283,317✔
1288
    if (pResultList[i]) {
3,002,119✔
1289
      STUidTagInfo* tmpTag = (STUidTagInfo*)taosArrayGet(pUidTagList, i);
2,420,758✔
1290
      if (!tmpTag) {
2,419,487!
1291
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1292
        return terrno;
×
1293
      }
1294
      uint64_t uid = tmpTag->uid;
2,419,487✔
1295
      qDebug("tagfilter get uid:%" PRId64 ", res:%d", uid, pResultList[i]);
2,419,487✔
1296

1297
      info.uid = uid;
2,419,501✔
1298
      void* p = taosArrayPush(pListInfo->pTableList, &info);
2,419,501✔
1299
      if (p == NULL) {
2,420,808!
1300
        return terrno;
×
1301
      }
1302

1303
      if (addUid) {
2,420,808!
1304
        void* tmp = taosArrayPush(pUidList, &uid);
×
1305
        if (tmp == NULL) {
×
1306
          return terrno;
×
1307
        }
1308
      }
1309
    }
1310
  }
1311

1312
  return TSDB_CODE_SUCCESS;
1,281,198✔
1313
}
1314

1315
static int32_t copyExistedUids(SArray* pUidTagList, const SArray* pUidList) {
1,322,973✔
1316
  int32_t code = TSDB_CODE_SUCCESS;
1,322,973✔
1317
  int32_t numOfExisted = taosArrayGetSize(pUidList);
1,322,973✔
1318
  if (numOfExisted == 0) {
1,323,186✔
1319
    return code;
1,293,217✔
1320
  }
1321

1322
  for (int32_t i = 0; i < numOfExisted; ++i) {
61,668✔
1323
    uint64_t*    uid = taosArrayGet(pUidList, i);
31,699✔
1324
    if (!uid) {
31,698!
1325
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1326
      return terrno;
×
1327
    }
1328
    STUidTagInfo info = {.uid = *uid};
31,698✔
1329
    void*        tmp = taosArrayPush(pUidTagList, &info);
31,699✔
1330
    if (!tmp) {
31,699!
1331
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1332
      return code;
×
1333
    }
1334
  }
1335
  return code;
29,969✔
1336
}
1337

1338
static int32_t doFilterByTagCond(STableListInfo* pListInfo, SArray* pUidList, SNode* pTagCond, void* pVnode,
4,767,755✔
1339
                                 SIdxFltStatus status, SStorageAPI* pAPI, bool addUid, bool* listAdded) {
1340
  *listAdded = false;
4,767,755✔
1341
  if (pTagCond == NULL) {
4,767,755✔
1342
    return TSDB_CODE_SUCCESS;
3,446,664✔
1343
  }
1344

1345
  terrno = TSDB_CODE_SUCCESS;
1,321,091✔
1346

1347
  int32_t      lino = 0;
1,323,675✔
1348
  int32_t      code = TSDB_CODE_SUCCESS;
1,323,675✔
1349
  SArray*      pBlockList = NULL;
1,323,675✔
1350
  SSDataBlock* pResBlock = NULL;
1,323,675✔
1351
  SScalarParam output = {0};
1,323,675✔
1352
  SArray*      pUidTagList = NULL;
1,323,675✔
1353

1354
  tagFilterAssist ctx = {0};
1,323,675✔
1355
  ctx.colHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT), false, HASH_NO_LOCK);
1,323,675✔
1356
  if (ctx.colHash == NULL) {
1,323,936!
1357
    code = terrno;
×
1358
    QUERY_CHECK_CODE(code, lino, end);
×
1359
  }
1360

1361
  ctx.cInfoList = taosArrayInit(4, sizeof(SColumnInfo));
1,323,936✔
1362
  if (ctx.cInfoList == NULL) {
1,324,666!
1363
    code = terrno;
×
1364
    QUERY_CHECK_CODE(code, lino, end);
×
1365
  }
1366

1367
  nodesRewriteExprPostOrder(&pTagCond, getColumn, (void*)&ctx);
1,324,666✔
1368
  if (TSDB_CODE_SUCCESS != ctx.code) {
1,322,975!
1369
    terrno = code = ctx.code;
×
1370
    goto end;
×
1371
  }
1372

1373
  SDataType type = {.type = TSDB_DATA_TYPE_BOOL, .bytes = sizeof(bool)};
1,322,975✔
1374

1375
  //  int64_t stt = taosGetTimestampUs();
1376
  pUidTagList = taosArrayInit(10, sizeof(STUidTagInfo));
1,322,975✔
1377
  QUERY_CHECK_NULL(pUidTagList, code, lino, end, terrno);
1,323,461!
1378

1379
  code = copyExistedUids(pUidTagList, pUidList);
1,323,461✔
1380
  QUERY_CHECK_CODE(code, lino, end);
1,323,460!
1381

1382
  FilterCondType condType = checkTagCond(pTagCond);
1,323,460✔
1383

1384
  int32_t filter = optimizeTbnameInCond(pVnode, pListInfo->idInfo.suid, pUidTagList, pTagCond, pAPI);
1,323,373✔
1385
  if (filter == 0) {  // tbname in filter is activated, do nothing and return
1,322,172✔
1386
    taosArrayClear(pUidList);
430,606✔
1387

1388
    int32_t numOfRows = taosArrayGetSize(pUidTagList);
430,607✔
1389
    code = taosArrayEnsureCap(pUidList, numOfRows);
430,607✔
1390
    QUERY_CHECK_CODE(code, lino, end);
430,605!
1391

1392
    for (int32_t i = 0; i < numOfRows; ++i) {
861,208✔
1393
      STUidTagInfo* pInfo = taosArrayGet(pUidTagList, i);
430,608✔
1394
      QUERY_CHECK_NULL(pInfo, code, lino, end, terrno);
430,605!
1395
      void*         tmp = taosArrayPush(pUidList, &pInfo->uid);
430,605✔
1396
      QUERY_CHECK_NULL(tmp, code, lino, end, terrno);
430,603!
1397
    }
1398
    terrno = 0;
430,600✔
1399
  } else {
1400
    if ((condType == FILTER_NO_LOGIC || condType == FILTER_AND) && status != SFLT_NOT_INDEX) {
891,566✔
1401
      code = pAPI->metaFn.getTableTagsByUid(pVnode, pListInfo->idInfo.suid, pUidTagList);
28✔
1402
    } else {
1403
      code = pAPI->metaFn.getTableTags(pVnode, pListInfo->idInfo.suid, pUidTagList);
891,538✔
1404
    }
1405
    if (code != TSDB_CODE_SUCCESS) {
893,914!
1406
      qError("failed to get table tags from meta, reason:%s, suid:%" PRIu64, tstrerror(code), pListInfo->idInfo.suid);
×
1407
      terrno = code;
×
1408
      QUERY_CHECK_CODE(code, lino, end);
×
1409
    }
1410
  }
1411

1412
  int32_t numOfTables = taosArrayGetSize(pUidTagList);
1,324,514✔
1413
  if (numOfTables == 0) {
1,323,626✔
1414
    goto end;
43,161✔
1415
  }
1416

1417
  pResBlock = createTagValBlockForFilter(ctx.cInfoList, numOfTables, pUidTagList, pVnode, pAPI);
1,280,465✔
1418
  if (pResBlock == NULL) {
1,280,941!
1419
    code = terrno;
×
1420
    QUERY_CHECK_CODE(code, lino, end);
×
1421
  }
1422

1423
  //  int64_t st1 = taosGetTimestampUs();
1424
  //  qDebug("generate tag block rows:%d, cost:%ld us", rows, st1-st);
1425
  pBlockList = taosArrayInit(2, POINTER_BYTES);
1,280,941✔
1426
  QUERY_CHECK_NULL(pBlockList, code, lino, end, terrno);
1,281,431!
1427

1428
  void* tmp = taosArrayPush(pBlockList, &pResBlock);
1,281,225✔
1429
  QUERY_CHECK_NULL(tmp, code, lino, end, terrno);
1,281,225!
1430

1431
  code = createResultData(&type, numOfTables, &output);
1,281,225✔
1432
  if (code != TSDB_CODE_SUCCESS) {
1,281,478!
1433
    terrno = code;
×
1434
    QUERY_CHECK_CODE(code, lino, end);
×
1435
  }
1436

1437
  code = scalarCalculate(pTagCond, pBlockList, &output);
1,281,478✔
1438
  if (code != TSDB_CODE_SUCCESS) {
1,280,484✔
1439
    qError("failed to calculate scalar, reason:%s", tstrerror(code));
8!
1440
    terrno = code;
8✔
1441
    QUERY_CHECK_CODE(code, lino, end);
8!
1442
  }
1443

1444
  code = doSetQualifiedUid(pListInfo, pUidList, pUidTagList, (bool*)output.columnData->pData, addUid);
1,280,476✔
1445
  if (code != TSDB_CODE_SUCCESS) {
1,280,951!
1446
    terrno = code;
×
1447
    QUERY_CHECK_CODE(code, lino, end);
×
1448
  }
1449
  *listAdded = true;
1,281,019✔
1450

1451
end:
1,324,188✔
1452
  if (code != TSDB_CODE_SUCCESS) {
1,324,188✔
1453
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
8!
1454
  }
1455
  taosHashCleanup(ctx.colHash);
1,324,188✔
1456
  taosArrayDestroy(ctx.cInfoList);
1,324,527✔
1457
  blockDataDestroy(pResBlock);
1,324,743✔
1458
  taosArrayDestroy(pBlockList);
1,324,775✔
1459
  taosArrayDestroyEx(pUidTagList, freeItem);
1,324,790✔
1460

1461
  colDataDestroy(output.columnData);
1,324,298✔
1462
  taosMemoryFreeClear(output.columnData);
1,324,791!
1463
  return code;
1,324,816✔
1464
}
1465

1466
int32_t getTableList(void* pVnode, SScanPhysiNode* pScanNode, SNode* pTagCond, SNode* pTagIndexCond,
4,764,204✔
1467
                     STableListInfo* pListInfo, uint8_t* digest, const char* idstr, SStorageAPI* pStorageAPI) {
1468
  int32_t code = TSDB_CODE_SUCCESS;
4,764,204✔
1469
  int32_t lino = 0;
4,764,204✔
1470
  size_t  numOfTables = 0;
4,764,204✔
1471
  bool    listAdded = false;
4,764,204✔
1472

1473
  pListInfo->idInfo.suid = pScanNode->suid;
4,764,204✔
1474
  pListInfo->idInfo.tableType = pScanNode->tableType;
4,764,204✔
1475

1476
  SArray* pUidList = taosArrayInit(8, sizeof(uint64_t));
4,764,204✔
1477
  QUERY_CHECK_NULL(pUidList, code, lino, _error, terrno);
4,773,470✔
1478

1479
  SIdxFltStatus status = SFLT_NOT_INDEX;
4,767,394✔
1480
  if (pScanNode->tableType != TSDB_SUPER_TABLE) {
4,767,394✔
1481
    pListInfo->idInfo.uid = pScanNode->uid;
693,710✔
1482
    if (pStorageAPI->metaFn.isTableExisted(pVnode, pScanNode->uid)) {
693,710✔
1483
      void* tmp = taosArrayPush(pUidList, &pScanNode->uid);
691,953✔
1484
      QUERY_CHECK_NULL(tmp, code, lino, _error, terrno);
691,942!
1485
    }
1486
    code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, status, pStorageAPI, false, &listAdded);
693,714✔
1487
    QUERY_CHECK_CODE(code, lino, _end);
693,707!
1488
  } else {
1489
    T_MD5_CTX context = {0};
4,073,684✔
1490

1491
    if (tsTagFilterCache) {
4,073,684!
1492
      // try to retrieve the result from meta cache
1493
      code = genTagFilterDigest(pTagCond, &context);
×
1494
      QUERY_CHECK_CODE(code, lino, _error);
×
1495

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

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

1509
    if (!pTagCond) {  // no tag filter condition exists, let's fetch all tables of this super table
4,073,684✔
1510
      code = pStorageAPI->metaFn.getChildTableList(pVnode, pScanNode->suid, pUidList);
2,778,400✔
1511
      QUERY_CHECK_CODE(code, lino, _error);
2,787,028!
1512
    } else {
1513
      // failed to find the result in the cache, let try to calculate the results
1514
      if (pTagIndexCond) {
1,295,284✔
1515
        void* pIndex = pStorageAPI->metaFn.getInvertIndex(pVnode);
43,363✔
1516

1517
        SIndexMetaArg metaArg = {.metaEx = pVnode,
86,836✔
1518
                                 .idx = pStorageAPI->metaFn.storeGetIndexInfo(pVnode),
43,421✔
1519
                                 .ivtIdx = pIndex,
1520
                                 .suid = pScanNode->uid};
43,415✔
1521

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

1532
    code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, status, pStorageAPI, tsTagFilterCache, &listAdded);
4,082,279✔
1533
    QUERY_CHECK_CODE(code, lino, _end);
4,077,121✔
1534

1535
    // let's add the filter results into meta-cache
1536
    numOfTables = taosArrayGetSize(pUidList);
4,077,113✔
1537

1538
    if (tsTagFilterCache) {
4,074,657✔
1539
      size_t size = numOfTables * sizeof(uint64_t) + sizeof(int32_t);
536✔
1540
      char*  pPayload = taosMemoryMalloc(size);
536!
1541
      QUERY_CHECK_NULL(pPayload, code, lino, _end, terrno);
×
1542

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

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

1554
      digest[0] = 1;
×
1555
      memcpy(digest + 1, context.digest, tListLen(context.digest));
×
1556
    }
1557
  }
1558

1559
_end:
4,767,836✔
1560
  if (!listAdded) {
4,767,836✔
1561
    numOfTables = taosArrayGetSize(pUidList);
3,488,414✔
1562
    for (int i = 0; i < numOfTables; i++) {
13,616,172✔
1563
      void* tmp = taosArrayGet(pUidList, i);
10,125,894✔
1564
      QUERY_CHECK_NULL(tmp, code, lino, _error, terrno);
10,115,584!
1565
      STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
10,115,584✔
1566

1567
      void* p = taosArrayPush(pListInfo->pTableList, &info);
10,115,584✔
1568
      if (p == NULL) {
10,126,901!
1569
        taosArrayDestroy(pUidList);
×
1570
        return terrno;
×
1571
      }
1572

1573
      qTrace("tagfilter get uid:%" PRIu64 ", %s", info.uid, idstr);
10,126,901✔
1574
    }
1575
  }
1576

1577
_error:
4,769,700✔
1578
  taosArrayDestroy(pUidList);
4,769,700✔
1579
  if (code != TSDB_CODE_SUCCESS) {
4,773,935✔
1580
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
8!
1581
  }
1582
  return code;
4,774,073✔
1583
}
1584

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

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

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

1612
size_t getTableTagsBufLen(const SNodeList* pGroups) {
9,234✔
1613
  size_t keyLen = 0;
9,234✔
1614

1615
  SNode* node;
1616
  FOREACH(node, pGroups) {
53,874!
1617
    SExprNode* pExpr = (SExprNode*)node;
44,640✔
1618
    keyLen += pExpr->resType.bytes;
44,640✔
1619
  }
1620

1621
  keyLen += sizeof(int8_t) * LIST_LENGTH(pGroups);
9,234!
1622
  return keyLen;
9,234✔
1623
}
1624

1625
int32_t getGroupIdFromTagsVal(void* pVnode, uint64_t uid, SNodeList* pGroupNode, char* keyBuf, uint64_t* pGroupId,
13✔
1626
                              SStorageAPI* pAPI) {
1627
  SMetaReader mr = {0};
13✔
1628

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

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

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

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

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

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

1699
  int32_t len = (int32_t)(pStart - (char*)keyBuf);
13✔
1700
  *pGroupId = calcGroupId(keyBuf, len);
13✔
1701

1702
  nodesDestroyList(groupNew);
13✔
1703
  pAPI->metaReaderFn.clearReader(&mr);
13✔
1704
  
1705
  return TSDB_CODE_SUCCESS;
13✔
1706
}
1707

1708
SArray* makeColumnArrayFromList(SNodeList* pNodeList) {
27,438✔
1709
  if (!pNodeList) {
27,438!
1710
    return NULL;
×
1711
  }
1712

1713
  size_t  numOfCols = LIST_LENGTH(pNodeList);
27,438✔
1714
  SArray* pList = taosArrayInit(numOfCols, sizeof(SColumn));
27,438✔
1715
  if (pList == NULL) {
27,429!
1716
    return NULL;
×
1717
  }
1718

1719
  for (int32_t i = 0; i < numOfCols; ++i) {
102,050✔
1720
    SColumnNode* pColNode = (SColumnNode*)nodesListGetNode(pNodeList, i);
74,593✔
1721
    if (!pColNode) {
74,565!
1722
      taosArrayDestroy(pList);
×
1723
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR));
×
1724
      return NULL;
×
1725
    }
1726

1727
    // todo extract method
1728
    SColumn c = {0};
74,565✔
1729
    c.slotId = pColNode->slotId;
74,565✔
1730
    c.colId = pColNode->colId;
74,565✔
1731
    c.type = pColNode->node.resType.type;
74,565✔
1732
    c.bytes = pColNode->node.resType.bytes;
74,565✔
1733
    c.precision = pColNode->node.resType.precision;
74,565✔
1734
    c.scale = pColNode->node.resType.scale;
74,565✔
1735

1736
    void* tmp = taosArrayPush(pList, &c);
74,621✔
1737
    if (!tmp) {
74,621!
1738
      taosArrayDestroy(pList);
×
1739
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1740
      return NULL;
×
1741
    }
1742
  }
1743

1744
  return pList;
27,457✔
1745
}
1746

1747
int32_t extractColMatchInfo(SNodeList* pNodeList, SDataBlockDescNode* pOutputNodeList, int32_t* numOfOutputCols,
8,788,448✔
1748
                            int32_t type, SColMatchInfo* pMatchInfo) {
1749
  size_t  numOfCols = LIST_LENGTH(pNodeList);
8,788,448!
1750
  int32_t code = TSDB_CODE_SUCCESS;
8,788,448✔
1751
  int32_t lino = 0;
8,788,448✔
1752

1753
  pMatchInfo->matchType = type;
8,788,448✔
1754

1755
  SArray* pList = taosArrayInit(numOfCols, sizeof(SColMatchItem));
8,788,448✔
1756
  if (pList == NULL) {
8,791,537✔
1757
    code = terrno;
1,196✔
1758
    return code;
×
1759
  }
1760

1761
  for (int32_t i = 0; i < numOfCols; ++i) {
44,820,840✔
1762
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pNodeList, i);
36,018,626✔
1763
    QUERY_CHECK_NULL(pNode, code, lino, _end, terrno);
36,040,697!
1764
    if (nodeType(pNode->pExpr) == QUERY_NODE_COLUMN) {
36,047,564✔
1765
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
35,952,825✔
1766

1767
      SColMatchItem c = {.needOutput = true};
35,952,825✔
1768
      c.colId = pColNode->colId;
35,952,825✔
1769
      c.srcSlotId = pColNode->slotId;
35,952,825✔
1770
      c.dstSlotId = pNode->slotId;
35,952,825✔
1771
      c.isPk = pColNode->isPk;
35,952,825✔
1772
      c.dataType = pColNode->node.resType;
35,952,825✔
1773
      void* tmp = taosArrayPush(pList, &c);
35,935,760✔
1774
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
35,935,760!
1775
    }
1776
  }
1777

1778
  // set the output flag for each column in SColMatchInfo, according to the
1779
  *numOfOutputCols = 0;
8,802,214✔
1780
  int32_t num = LIST_LENGTH(pOutputNodeList->pSlots);
8,802,214✔
1781
  for (int32_t i = 0; i < num; ++i) {
52,095,312✔
1782
    SSlotDescNode* pNode = (SSlotDescNode*)nodesListGetNode(pOutputNodeList->pSlots, i);
43,267,072✔
1783
    QUERY_CHECK_NULL(pNode, code, lino, _end, terrno);
43,445,002!
1784

1785
    // todo: add reserve flag check
1786
    // it is a column reserved for the arithmetic expression calculation
1787
    if (pNode->slotId >= numOfCols) {
43,466,783✔
1788
      (*numOfOutputCols) += 1;
7,420,382✔
1789
      continue;
7,420,382✔
1790
    }
1791

1792
    SColMatchItem* info = NULL;
36,046,401✔
1793
    for (int32_t j = 0; j < taosArrayGetSize(pList); ++j) {
859,628,048✔
1794
      info = taosArrayGet(pList, j);
862,329,423✔
1795
      QUERY_CHECK_NULL(info, code, lino, _end, terrno);
864,988,733✔
1796
      if (info->dstSlotId == pNode->slotId) {
859,179,125✔
1797
        break;
35,597,478✔
1798
      }
1799
    }
1800

1801
    if (pNode->output) {
35,872,716✔
1802
      (*numOfOutputCols) += 1;
35,806,852✔
1803
    } else if (info != NULL) {
65,864!
1804
      // select distinct tbname from stb where tbname='abc';
1805
      info->needOutput = false;
65,868✔
1806
    }
1807
  }
1808

1809
  pMatchInfo->pList = pList;
8,828,240✔
1810

1811
_end:
8,828,240✔
1812
  if (code != TSDB_CODE_SUCCESS) {
8,828,240!
1813
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1814
  }
1815
  return code;
8,786,521✔
1816
}
1817

1818
static SResSchema createResSchema(int32_t type, int32_t bytes, int32_t slotId, int32_t scale, int32_t precision,
28,393,837✔
1819
                                  const char* name) {
1820
  SResSchema s = {0};
28,393,837✔
1821
  s.scale = scale;
28,393,837✔
1822
  s.type = type;
28,393,837✔
1823
  s.bytes = bytes;
28,393,837✔
1824
  s.slotId = slotId;
28,393,837✔
1825
  s.precision = precision;
28,393,837✔
1826
  tstrncpy(s.name, name, tListLen(s.name));
28,393,837✔
1827

1828
  return s;
28,393,837✔
1829
}
1830

1831
static SColumn* createColumn(int32_t blockId, int32_t slotId, int32_t colId, SDataType* pType, EColumnType colType) {
26,923,147✔
1832
  SColumn* pCol = taosMemoryCalloc(1, sizeof(SColumn));
26,923,147!
1833
  if (pCol == NULL) {
26,924,056!
1834
    return NULL;
×
1835
  }
1836

1837
  pCol->slotId = slotId;
26,924,056✔
1838
  pCol->colId = colId;
26,924,056✔
1839
  pCol->bytes = pType->bytes;
26,924,056✔
1840
  pCol->type = pType->type;
26,924,056✔
1841
  pCol->scale = pType->scale;
26,924,056✔
1842
  pCol->precision = pType->precision;
26,924,056✔
1843
  pCol->dataBlockId = blockId;
26,924,056✔
1844
  pCol->colType = colType;
26,924,056✔
1845
  return pCol;
26,924,056✔
1846
}
1847

1848
int32_t createExprFromOneNode(SExprInfo* pExp, SNode* pNode, int16_t slotId) {
28,379,634✔
1849
  int32_t code = TSDB_CODE_SUCCESS;
28,379,634✔
1850
  int32_t lino = 0;
28,379,634✔
1851
  pExp->base.numOfParams = 0;
28,379,634✔
1852
  pExp->base.pParam = NULL;
28,379,634✔
1853
  pExp->pExpr = taosMemoryCalloc(1, sizeof(tExprNode));
28,379,634!
1854
  QUERY_CHECK_NULL(pExp->pExpr, code, lino, _end, terrno);
28,402,501!
1855

1856
  pExp->pExpr->_function.num = 1;
28,402,501✔
1857
  pExp->pExpr->_function.functionId = -1;
28,402,501✔
1858

1859
  int32_t type = nodeType(pNode);
28,402,501✔
1860
  // it is a project query, or group by column
1861
  if (type == QUERY_NODE_COLUMN) {
28,402,501✔
1862
    pExp->pExpr->nodeType = QUERY_NODE_COLUMN;
17,668,893✔
1863
    SColumnNode* pColNode = (SColumnNode*)pNode;
17,668,893✔
1864

1865
    pExp->base.pParam = taosMemoryCalloc(1, sizeof(SFunctParam));
17,668,893✔
1866
    QUERY_CHECK_NULL(pExp->base.pParam, code, lino, _end, terrno);
17,669,437!
1867

1868
    pExp->base.numOfParams = 1;
17,669,437✔
1869

1870
    SDataType* pType = &pColNode->node.resType;
17,669,437✔
1871
    pExp->base.resSchema =
1872
        createResSchema(pType->type, pType->bytes, slotId, pType->scale, pType->precision, pColNode->colName);
17,669,437✔
1873

1874
    pExp->base.pParam[0].pCol =
35,325,049✔
1875
        createColumn(pColNode->dataBlockId, pColNode->slotId, pColNode->colId, pType, pColNode->colType);
17,666,786✔
1876
    QUERY_CHECK_NULL(pExp->base.pParam[0].pCol, code, lino, _end, terrno);
17,658,263!
1877

1878
    pExp->base.pParam[0].type = FUNC_PARAM_TYPE_COLUMN;
17,658,263✔
1879
  } else if (type == QUERY_NODE_VALUE) {
10,733,608✔
1880
    pExp->pExpr->nodeType = QUERY_NODE_VALUE;
272,304✔
1881
    SValueNode* pValNode = (SValueNode*)pNode;
272,304✔
1882

1883
    pExp->base.pParam = taosMemoryCalloc(1, sizeof(SFunctParam));
272,304!
1884
    QUERY_CHECK_NULL(pExp->base.pParam, code, lino, _end, terrno);
272,393!
1885

1886
    pExp->base.numOfParams = 1;
272,393✔
1887

1888
    SDataType* pType = &pValNode->node.resType;
272,393✔
1889
    pExp->base.resSchema =
1890
        createResSchema(pType->type, pType->bytes, slotId, pType->scale, pType->precision, pValNode->node.aliasName);
272,393✔
1891
    pExp->base.pParam[0].type = FUNC_PARAM_TYPE_VALUE;
272,338✔
1892
    code = nodesValueNodeToVariant(pValNode, &pExp->base.pParam[0].param);
272,338✔
1893
    QUERY_CHECK_CODE(code, lino, _end);
272,324!
1894
  } else if (type == QUERY_NODE_FUNCTION) {
10,461,304✔
1895
    pExp->pExpr->nodeType = QUERY_NODE_FUNCTION;
10,247,351✔
1896
    SFunctionNode* pFuncNode = (SFunctionNode*)pNode;
10,247,351✔
1897

1898
    SDataType* pType = &pFuncNode->node.resType;
10,247,351✔
1899
    pExp->base.resSchema =
1900
        createResSchema(pType->type, pType->bytes, slotId, pType->scale, pType->precision, pFuncNode->node.aliasName);
10,247,351✔
1901
    tExprNode* pExprNode = pExp->pExpr;
10,248,744✔
1902

1903
    pExprNode->_function.functionId = pFuncNode->funcId;
10,248,744✔
1904
    pExprNode->_function.pFunctNode = pFuncNode;
10,248,744✔
1905
    pExprNode->_function.functionType = pFuncNode->funcType;
10,248,744✔
1906

1907
    tstrncpy(pExprNode->_function.functionName, pFuncNode->functionName, tListLen(pExprNode->_function.functionName));
10,248,744✔
1908

1909
#if 1
1910
    // todo refactor: add the parameter for tbname function
1911
    const char* name = "tbname";
10,248,744✔
1912
    int32_t     len = strlen(name);
10,248,744✔
1913

1914
    if (!pFuncNode->pParameterList && (memcmp(pExprNode->_function.functionName, name, len) == 0) &&
10,248,744✔
1915
        pExprNode->_function.functionName[len] == 0) {
704,160✔
1916
      pFuncNode->pParameterList = NULL;
704,082✔
1917
      int32_t     code = nodesMakeList(&pFuncNode->pParameterList);
704,082✔
1918
      SValueNode* res = NULL;
704,259✔
1919
      if (TSDB_CODE_SUCCESS == code) {
704,259!
1920
        code = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&res);
704,276✔
1921
      }
1922
      QUERY_CHECK_CODE(code, lino, _end);
704,206!
1923
      res->node.resType = (SDataType){.bytes = sizeof(int64_t), .type = TSDB_DATA_TYPE_BIGINT};
704,206✔
1924
      code = nodesListAppend(pFuncNode->pParameterList, (SNode*)res);
704,206✔
1925
      if (code != TSDB_CODE_SUCCESS) {
704,303✔
1926
        nodesDestroyNode((SNode*)res);
176✔
1927
        res = NULL;
×
1928
      }
1929
      QUERY_CHECK_CODE(code, lino, _end);
704,127!
1930
    }
1931
#endif
1932

1933
    int32_t numOfParam = LIST_LENGTH(pFuncNode->pParameterList);
10,248,789✔
1934

1935
    pExp->base.pParam = taosMemoryCalloc(numOfParam, sizeof(SFunctParam));
10,248,789✔
1936
    QUERY_CHECK_NULL(pExp->base.pParam, code, lino, _end, terrno);
10,252,558!
1937
    pExp->base.numOfParams = numOfParam;
10,252,558✔
1938

1939
    for (int32_t j = 0; j < numOfParam && TSDB_CODE_SUCCESS == code; ++j) {
22,009,661!
1940
      SNode* p1 = nodesListGetNode(pFuncNode->pParameterList, j);
11,757,280✔
1941
      QUERY_CHECK_NULL(p1, code, lino, _end, terrno);
11,758,895!
1942
      if (p1->type == QUERY_NODE_COLUMN) {
11,760,067✔
1943
        SColumnNode* pcn = (SColumnNode*)p1;
9,267,120✔
1944

1945
        pExp->base.pParam[j].type = FUNC_PARAM_TYPE_COLUMN;
9,267,120✔
1946
        pExp->base.pParam[j].pCol =
18,533,178✔
1947
            createColumn(pcn->dataBlockId, pcn->slotId, pcn->colId, &pcn->node.resType, pcn->colType);
9,267,120✔
1948
        QUERY_CHECK_NULL(pExp->base.pParam[j].pCol, code, lino, _end, terrno);
9,266,058✔
1949
      } else if (p1->type == QUERY_NODE_VALUE) {
2,492,947✔
1950
        SValueNode* pvn = (SValueNode*)p1;
2,074,610✔
1951
        pExp->base.pParam[j].type = FUNC_PARAM_TYPE_VALUE;
2,074,610✔
1952
        code = nodesValueNodeToVariant(pvn, &pExp->base.pParam[j].param);
2,074,610✔
1953
        QUERY_CHECK_CODE(code, lino, _end);
2,074,231!
1954
      }
1955
    }
1956
  } else if (type == QUERY_NODE_OPERATOR) {
213,953✔
1957
    pExp->pExpr->nodeType = QUERY_NODE_OPERATOR;
204,521✔
1958
    SOperatorNode* pOpNode = (SOperatorNode*)pNode;
204,521✔
1959

1960
    pExp->base.pParam = taosMemoryCalloc(1, sizeof(SFunctParam));
204,521!
1961
    QUERY_CHECK_NULL(pExp->base.pParam, code, lino, _end, terrno);
204,533!
1962
    pExp->base.numOfParams = 1;
204,533✔
1963

1964
    SDataType* pType = &pOpNode->node.resType;
204,533✔
1965
    pExp->base.resSchema =
1966
        createResSchema(pType->type, pType->bytes, slotId, pType->scale, pType->precision, pOpNode->node.aliasName);
204,533✔
1967
    pExp->pExpr->_optrRoot.pRootNode = pNode;
204,460✔
1968
  } else if (type == QUERY_NODE_CASE_WHEN) {
9,432✔
1969
    pExp->pExpr->nodeType = QUERY_NODE_OPERATOR;
5,581✔
1970
    SCaseWhenNode* pCaseNode = (SCaseWhenNode*)pNode;
5,581✔
1971

1972
    pExp->base.pParam = taosMemoryCalloc(1, sizeof(SFunctParam));
5,581!
1973
    QUERY_CHECK_NULL(pExp->base.pParam, code, lino, _end, terrno);
5,583!
1974
    pExp->base.numOfParams = 1;
5,583✔
1975

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

1994
_end:
3,849✔
1995
  if (code != TSDB_CODE_SUCCESS) {
28,396,859!
1996
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1997
  }
1998
  return code;
28,381,291✔
1999
}
2000

2001
int32_t createExprFromTargetNode(SExprInfo* pExp, STargetNode* pTargetNode) {
28,356,046✔
2002
  return createExprFromOneNode(pExp, pTargetNode->pExpr, pTargetNode->slotId);
28,356,046✔
2003
}
2004

2005
SExprInfo* createExpr(SNodeList* pNodeList, int32_t* numOfExprs) {
2,669✔
2006
  *numOfExprs = LIST_LENGTH(pNodeList);
2,669!
2007
  SExprInfo* pExprs = taosMemoryCalloc(*numOfExprs, sizeof(SExprInfo));
2,669!
2008
  if (!pExprs) {
2,669!
2009
    return NULL;
×
2010
  }
2011

2012
  for (int32_t i = 0; i < (*numOfExprs); ++i) {
22,700✔
2013
    SExprInfo* pExp = &pExprs[i];
20,032✔
2014
    int32_t    code = createExprFromOneNode(pExp, nodesListGetNode(pNodeList, i), i + UD_TAG_COLUMN_INDEX);
20,032✔
2015
    if (code != TSDB_CODE_SUCCESS) {
20,031!
2016
      taosMemoryFreeClear(pExprs);
×
2017
      terrno = code;
×
2018
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
2019
      return NULL;
×
2020
    }
2021
  }
2022

2023
  return pExprs;
2,668✔
2024
}
2025

2026
int32_t createExprInfo(SNodeList* pNodeList, SNodeList* pGroupKeys, SExprInfo** pExprInfo, int32_t* numOfExprs) {
11,386,964✔
2027
  QRY_PARAM_CHECK(pExprInfo);
11,386,964!
2028

2029
  int32_t code = 0;
11,386,964✔
2030
  int32_t numOfFuncs = LIST_LENGTH(pNodeList);
11,386,964✔
2031
  int32_t numOfGroupKeys = 0;
11,386,964✔
2032
  if (pGroupKeys != NULL) {
11,386,964✔
2033
    numOfGroupKeys = LIST_LENGTH(pGroupKeys);
358,147✔
2034
  }
2035

2036
  *numOfExprs = numOfFuncs + numOfGroupKeys;
11,386,964✔
2037
  if (*numOfExprs == 0) {
11,386,964✔
2038
    return code;
1,613,619✔
2039
  }
2040

2041
  SExprInfo* pExprs = taosMemoryCalloc(*numOfExprs, sizeof(SExprInfo));
9,773,345!
2042
  if (pExprs == NULL) {
9,778,191!
2043
    return terrno;
×
2044
  }
2045

2046
  for (int32_t i = 0; i < (*numOfExprs); ++i) {
37,733,606✔
2047
    STargetNode* pTargetNode = NULL;
27,951,105✔
2048
    if (i < numOfFuncs) {
27,951,105✔
2049
      pTargetNode = (STargetNode*)nodesListGetNode(pNodeList, i);
27,477,110✔
2050
    } else {
2051
      pTargetNode = (STargetNode*)nodesListGetNode(pGroupKeys, i - numOfFuncs);
473,995✔
2052
    }
2053
    if (!pTargetNode) {
27,965,987!
2054
      destroyExprInfo(pExprs, *numOfExprs);
×
2055
      taosMemoryFreeClear(pExprs);
×
2056
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
2057
      return terrno;
×
2058
    }
2059

2060
    SExprInfo* pExp = &pExprs[i];
27,965,987✔
2061
    code = createExprFromTargetNode(pExp, pTargetNode);
27,965,987✔
2062
    if (code != TSDB_CODE_SUCCESS) {
27,956,540✔
2063
      destroyExprInfo(pExprs, *numOfExprs);
1,125✔
2064
      taosMemoryFreeClear(pExprs);
×
2065
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
2066
      return code;
×
2067
    }
2068
  }
2069

2070
  *pExprInfo = pExprs;
9,782,501✔
2071
  return code;
9,782,501✔
2072
}
2073

2074
// set the output buffer for the selectivity + tag query
2075
static int32_t setSelectValueColumnInfo(SqlFunctionCtx* pCtx, int32_t numOfOutput) {
11,047,473✔
2076
  int32_t num = 0;
11,047,473✔
2077
  int32_t code = TSDB_CODE_SUCCESS;
11,047,473✔
2078
  int32_t lino = 0;
11,047,473✔
2079

2080
  SqlFunctionCtx*  p = NULL;
11,047,473✔
2081
  SqlFunctionCtx** pValCtx = taosMemoryCalloc(numOfOutput, POINTER_BYTES);
11,047,473!
2082
  if (pValCtx == NULL) {
11,050,645!
2083
    return terrno;
×
2084
  }
2085

2086
  SHashObj* pSelectFuncs = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
11,050,645✔
2087
  QUERY_CHECK_NULL(pSelectFuncs, code, lino, _end, terrno);
11,042,831!
2088

2089
  for (int32_t i = 0; i < numOfOutput; ++i) {
38,643,142✔
2090
    const char* pName = pCtx[i].pExpr->pExpr->_function.functionName;
27,709,043✔
2091
    if ((strcmp(pName, "_select_value") == 0) || (strcmp(pName, "_group_key") == 0) ||
27,709,043✔
2092
        (strcmp(pName, "_group_const_value") == 0)) {
26,327,743!
2093
      pValCtx[num++] = &pCtx[i];
1,379,925✔
2094
    } else if (fmIsSelectFunc(pCtx[i].functionId)) {
26,329,118✔
2095
      void* data = taosHashGet(pSelectFuncs, pName, strlen(pName));
2,368,648✔
2096
      if (taosHashGetSize(pSelectFuncs) != 0 && data == NULL) {
2,360,377✔
2097
        p = NULL;
98,536✔
2098
        break;
98,536✔
2099
      } else {
2100
        int32_t tempRes = taosHashPut(pSelectFuncs, pName, strlen(pName), &num, sizeof(num));
2,262,315✔
2101
        if (tempRes != TSDB_CODE_SUCCESS && tempRes != TSDB_CODE_DUP_KEY) {
2,265,295!
2102
          code = tempRes;
×
2103
          QUERY_CHECK_CODE(code, lino, _end);
×
2104
        }
2105
        p = &pCtx[i];
2,265,295✔
2106
      }
2107
    }
2108
  }
2109
  taosHashCleanup(pSelectFuncs);
11,032,635✔
2110

2111
  if (p != NULL) {
11,047,516✔
2112
    p->subsidiaries.pCtx = pValCtx;
2,046,172✔
2113
    p->subsidiaries.num = num;
2,046,172✔
2114
  } else {
2115
    taosMemoryFreeClear(pValCtx);
9,001,344!
2116
  }
2117

2118
_end:
×
2119
  if (code != TSDB_CODE_SUCCESS) {
11,048,982!
2120
    taosMemoryFreeClear(pValCtx);
×
2121
    taosHashCleanup(pSelectFuncs);
×
2122
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
2123
  }
2124
  return code;
11,041,091✔
2125
}
2126

2127
SqlFunctionCtx* createSqlFunctionCtx(SExprInfo* pExprInfo, int32_t numOfOutput, int32_t** rowEntryInfoOffset,
11,039,585✔
2128
                                     SFunctionStateStore* pStore) {
2129
  int32_t         code = TSDB_CODE_SUCCESS;
11,039,585✔
2130
  int32_t         lino = 0;
11,039,585✔
2131
  SqlFunctionCtx* pFuncCtx = (SqlFunctionCtx*)taosMemoryCalloc(numOfOutput, sizeof(SqlFunctionCtx));
11,039,585!
2132
  if (pFuncCtx == NULL) {
11,040,203!
2133
    return NULL;
×
2134
  }
2135

2136
  *rowEntryInfoOffset = taosMemoryCalloc(numOfOutput, sizeof(int32_t));
11,040,203!
2137
  if (*rowEntryInfoOffset == 0) {
11,050,237!
2138
    taosMemoryFreeClear(pFuncCtx);
×
2139
    return NULL;
×
2140
  }
2141

2142
  for (int32_t i = 0; i < numOfOutput; ++i) {
39,197,621✔
2143
    SExprInfo* pExpr = &pExprInfo[i];
28,139,885✔
2144

2145
    SExprBasicInfo* pFunct = &pExpr->base;
28,139,885✔
2146
    SqlFunctionCtx* pCtx = &pFuncCtx[i];
28,139,885✔
2147

2148
    pCtx->functionId = -1;
28,139,885✔
2149
    pCtx->pExpr = pExpr;
28,139,885✔
2150

2151
    if (pExpr->pExpr->nodeType == QUERY_NODE_FUNCTION) {
28,139,885✔
2152
      SFuncExecEnv env = {0};
10,246,776✔
2153
      pCtx->functionId = pExpr->pExpr->_function.pFunctNode->funcId;
10,246,776✔
2154
      pCtx->isPseudoFunc = fmIsWindowPseudoColumnFunc(pCtx->functionId);
10,246,776✔
2155
      pCtx->isNotNullFunc = fmIsNotNullOutputFunc(pCtx->functionId);
10,239,421✔
2156

2157
      bool isUdaf = fmIsUserDefinedFunc(pCtx->functionId);
10,244,977✔
2158
      if (fmIsAggFunc(pCtx->functionId) || fmIsIndefiniteRowsFunc(pCtx->functionId)) {
17,123,392✔
2159
        if (!isUdaf) {
6,888,279✔
2160
          code = fmGetFuncExecFuncs(pCtx->functionId, &pCtx->fpSet);
6,888,247✔
2161
          QUERY_CHECK_CODE(code, lino, _end);
6,881,478!
2162
        } else {
2163
          char* udfName = pExpr->pExpr->_function.pFunctNode->functionName;
32✔
2164
          pCtx->udfName = taosStrdup(udfName);
32!
2165
          QUERY_CHECK_NULL(pCtx->udfName, code, lino, _end, terrno);
32!
2166

2167
          code = fmGetUdafExecFuncs(pCtx->functionId, &pCtx->fpSet);
32✔
2168
          QUERY_CHECK_CODE(code, lino, _end);
32!
2169
        }
2170
        bool tmp = pCtx->fpSet.getEnv(pExpr->pExpr->_function.pFunctNode, &env);
6,881,510✔
2171
        if (!tmp) {
6,879,734!
2172
          code = terrno;
×
2173
          QUERY_CHECK_CODE(code, lino, _end);
×
2174
        }
2175
      } else {
2176
        code = fmGetScalarFuncExecFuncs(pCtx->functionId, &pCtx->sfp);
3,349,023✔
2177
        if (code != TSDB_CODE_SUCCESS && isUdaf) {
3,348,887!
2178
          code = TSDB_CODE_SUCCESS;
262✔
2179
        }
2180
        QUERY_CHECK_CODE(code, lino, _end);
3,348,887!
2181

2182
        if (pCtx->sfp.getEnv != NULL) {
3,348,887✔
2183
          bool tmp = pCtx->sfp.getEnv(pExpr->pExpr->_function.pFunctNode, &env);
1,858,196✔
2184
          if (!tmp) {
1,856,099!
2185
            code = terrno;
×
2186
            QUERY_CHECK_CODE(code, lino, _end);
×
2187
          }
2188
        }
2189
      }
2190
      pCtx->resDataInfo.interBufSize = env.calcMemSize;
10,230,434✔
2191
    } else if (pExpr->pExpr->nodeType == QUERY_NODE_COLUMN || pExpr->pExpr->nodeType == QUERY_NODE_OPERATOR ||
17,893,109✔
2192
               pExpr->pExpr->nodeType == QUERY_NODE_VALUE) {
272,378!
2193
      // for simple column, the result buffer needs to hold at least one element.
2194
      pCtx->resDataInfo.interBufSize = pFunct->resSchema.bytes;
17,895,590✔
2195
    }
2196

2197
    pCtx->input.numOfInputCols = pFunct->numOfParams;
28,123,543✔
2198
    pCtx->input.pData = taosMemoryCalloc(pFunct->numOfParams, POINTER_BYTES);
28,123,543✔
2199
    QUERY_CHECK_NULL(pCtx->input.pData, code, lino, _end, terrno);
28,145,107!
2200
    pCtx->input.pColumnDataAgg = taosMemoryCalloc(pFunct->numOfParams, POINTER_BYTES);
28,145,107!
2201
    QUERY_CHECK_NULL(pCtx->input.pColumnDataAgg, code, lino, _end, terrno);
28,147,384!
2202

2203
    pCtx->pTsOutput = NULL;
28,147,384✔
2204
    pCtx->resDataInfo.bytes = pFunct->resSchema.bytes;
28,147,384✔
2205
    pCtx->resDataInfo.type = pFunct->resSchema.type;
28,147,384✔
2206
    pCtx->order = TSDB_ORDER_ASC;
28,147,384✔
2207
    pCtx->start.key = INT64_MIN;
28,147,384✔
2208
    pCtx->end.key = INT64_MIN;
28,147,384✔
2209
    pCtx->numOfParams = pExpr->base.numOfParams;
28,147,384✔
2210
    pCtx->param = pFunct->pParam;
28,147,384✔
2211
    pCtx->saveHandle.currentPage = -1;
28,147,384✔
2212
    pCtx->pStore = pStore;
28,147,384✔
2213
    pCtx->hasWindowOrGroup = false;
28,147,384✔
2214
    pCtx->needCleanup = false;
28,147,384✔
2215
  }
2216

2217
  for (int32_t i = 1; i < numOfOutput; ++i) {
29,061,863✔
2218
    (*rowEntryInfoOffset)[i] = (int32_t)((*rowEntryInfoOffset)[i - 1] + sizeof(SResultRowEntryInfo) +
18,004,127✔
2219
                                         pFuncCtx[i - 1].resDataInfo.interBufSize);
18,004,127✔
2220
  }
2221

2222
  code = setSelectValueColumnInfo(pFuncCtx, numOfOutput);
11,057,736✔
2223
  QUERY_CHECK_CODE(code, lino, _end);
11,041,307!
2224

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

2235
    terrno = code;
×
2236
    return NULL;
×
2237
  }
2238
  return pFuncCtx;
11,041,307✔
2239
}
2240

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

2247
  int32_t i = 0, j = 0;
1,033,637✔
2248
  while (i < numOfSrcCols && j < taosArrayGetSize(pColMatchInfo)) {
2,239,796✔
2249
    SColumnInfoData* p = taosArrayGet(pCols, i);
1,206,426✔
2250
    if (!p) {
1,206,050!
2251
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
2252
      return terrno;
×
2253
    }
2254
    SColMatchItem* pmInfo = taosArrayGet(pColMatchInfo, j);
1,206,050✔
2255
    if (!pmInfo) {
1,205,856!
2256
      return terrno;
×
2257
    }
2258

2259
    if (p->info.colId == pmInfo->colId) {
1,205,964✔
2260
      SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, pmInfo->dstSlotId);
1,178,587✔
2261
      if (!pDst) {
1,178,339!
2262
        return terrno;
×
2263
      }
2264
      code = colDataAssign(pDst, p, pBlock->info.rows, &pBlock->info);
1,178,339✔
2265
      if (code != TSDB_CODE_SUCCESS) {
1,178,868✔
2266
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
85!
2267
        return code;
×
2268
      }
2269
      i++;
1,178,783✔
2270
      j++;
1,178,783✔
2271
    } else if (p->info.colId < pmInfo->colId) {
27,377✔
2272
      i++;
27,376✔
2273
    } else {
2274
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR));
1!
2275
      return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
2276
    }
2277
  }
2278
  return code;
1,033,640✔
2279
}
2280

2281
SInterval extractIntervalInfo(const STableScanPhysiNode* pTableScanNode) {
2,795,063✔
2282
  SInterval interval = {
2,795,063✔
2283
      .interval = pTableScanNode->interval,
2,795,063✔
2284
      .sliding = pTableScanNode->sliding,
2,795,063✔
2285
      .intervalUnit = pTableScanNode->intervalUnit,
2,795,063✔
2286
      .slidingUnit = pTableScanNode->slidingUnit,
2,795,063✔
2287
      .offset = pTableScanNode->offset,
2,795,063✔
2288
      .precision = pTableScanNode->scan.node.pOutputDataBlockDesc->precision,
2,795,063✔
2289
      .timeRange = pTableScanNode->scanRange,
2290
  };
2291
  calcIntervalAutoOffset(&interval);
2,795,063✔
2292

2293
  return interval;
2,795,289✔
2294
}
2295

2296
SColumn extractColumnFromColumnNode(SColumnNode* pColNode) {
624,788✔
2297
  SColumn c = {0};
624,788✔
2298

2299
  c.slotId = pColNode->slotId;
624,788✔
2300
  c.colId = pColNode->colId;
624,788✔
2301
  c.type = pColNode->node.resType.type;
624,788✔
2302
  c.bytes = pColNode->node.resType.bytes;
624,788✔
2303
  c.scale = pColNode->node.resType.scale;
624,788✔
2304
  c.precision = pColNode->node.resType.precision;
624,788✔
2305
  return c;
624,788✔
2306
}
2307

2308
int32_t initQueryTableDataCond(SQueryTableDataCond* pCond, const STableScanPhysiNode* pTableScanNode,
4,798,306✔
2309
                               const SReadHandle* readHandle) {
2310
  pCond->order = pTableScanNode->scanSeq[0] > 0 ? TSDB_ORDER_ASC : TSDB_ORDER_DESC;
4,798,306✔
2311
  pCond->numOfCols = LIST_LENGTH(pTableScanNode->scan.pScanCols);
4,798,306!
2312

2313
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
4,798,306!
2314
  if (!pCond->colList) {
4,807,972!
2315
    return terrno;
×
2316
  }
2317
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
4,807,972!
2318
  if (pCond->pSlotList == NULL) {
4,803,815!
2319
    taosMemoryFreeClear(pCond->colList);
×
2320
    return terrno;
×
2321
  }
2322

2323
  // TODO: get it from stable scan node
2324
  pCond->twindows = pTableScanNode->scanRange;
4,803,815✔
2325
  pCond->suid = pTableScanNode->scan.suid;
4,803,815✔
2326
  pCond->type = TIMEWINDOW_RANGE_CONTAINED;
4,803,815✔
2327
  pCond->startVersion = -1;
4,803,815✔
2328
  pCond->endVersion = -1;
4,803,815✔
2329
  pCond->skipRollup = readHandle->skipRollup;
4,803,815✔
2330

2331
  // allowed read stt file optimization mode
2332
  pCond->notLoadData = (pTableScanNode->dataRequired == FUNC_DATA_REQUIRED_NOT_LOAD) &&
9,773,802✔
2333
                       (pTableScanNode->scan.node.pConditions == NULL) && (pTableScanNode->interval == 0);
4,803,815✔
2334

2335
  int32_t j = 0;
4,803,815✔
2336
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
27,987,609✔
2337
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pTableScanNode->scan.pScanCols, i);
23,174,768✔
2338
    if (!pNode) {
23,184,500✔
2339
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
706!
2340
      return terrno;
706✔
2341
    }
2342
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
23,183,794✔
2343
    if (pColNode->colType == COLUMN_TYPE_TAG) {
23,183,794!
2344
      continue;
×
2345
    }
2346

2347
    pCond->colList[j].type = pColNode->node.resType.type;
23,183,794✔
2348
    pCond->colList[j].bytes = pColNode->node.resType.bytes;
23,183,794✔
2349
    pCond->colList[j].colId = pColNode->colId;
23,183,794✔
2350
    pCond->colList[j].pk = pColNode->isPk;
23,183,794✔
2351

2352
    pCond->pSlotList[j] = pNode->slotId;
23,183,794✔
2353
    j += 1;
23,183,794✔
2354
  }
2355

2356
  pCond->numOfCols = j;
4,812,841✔
2357
  return TSDB_CODE_SUCCESS;
4,812,841✔
2358
}
2359

2360
void cleanupQueryTableDataCond(SQueryTableDataCond* pCond) {
6,825,546✔
2361
  taosMemoryFreeClear(pCond->colList);
6,825,546!
2362
  taosMemoryFreeClear(pCond->pSlotList);
6,826,065!
2363
}
6,826,019✔
2364

2365
int32_t convertFillType(int32_t mode) {
569,549✔
2366
  int32_t type = TSDB_FILL_NONE;
569,549✔
2367
  switch (mode) {
569,549!
2368
    case FILL_MODE_PREV:
16,499✔
2369
      type = TSDB_FILL_PREV;
16,499✔
2370
      break;
16,499✔
2371
    case FILL_MODE_NONE:
×
2372
      type = TSDB_FILL_NONE;
×
2373
      break;
×
2374
    case FILL_MODE_NULL:
27,397✔
2375
      type = TSDB_FILL_NULL;
27,397✔
2376
      break;
27,397✔
2377
    case FILL_MODE_NULL_F:
33,621✔
2378
      type = TSDB_FILL_NULL_F;
33,621✔
2379
      break;
33,621✔
2380
    case FILL_MODE_NEXT:
13,068✔
2381
      type = TSDB_FILL_NEXT;
13,068✔
2382
      break;
13,068✔
2383
    case FILL_MODE_VALUE:
150,117✔
2384
      type = TSDB_FILL_SET_VALUE;
150,117✔
2385
      break;
150,117✔
2386
    case FILL_MODE_VALUE_F:
301,207✔
2387
      type = TSDB_FILL_SET_VALUE_F;
301,207✔
2388
      break;
301,207✔
2389
    case FILL_MODE_LINEAR:
17,624✔
2390
      type = TSDB_FILL_LINEAR;
17,624✔
2391
      break;
17,624✔
2392
    case FILL_MODE_NEAR:
10,030✔
2393
      type = TSDB_FILL_NEAR;
10,030✔
2394
      break;
10,030✔
2395
    default:
×
2396
      type = TSDB_FILL_NONE;
×
2397
  }
2398

2399
  return type;
569,549✔
2400
}
2401

2402
void getInitialStartTimeWindow(SInterval* pInterval, TSKEY ts, STimeWindow* w, bool ascQuery) {
30,526,996✔
2403
  if (ascQuery) {
30,526,996✔
2404
    *w = getAlignQueryTimeWindow(pInterval, ts);
30,173,955✔
2405
  } else {
2406
    // the start position of the first time window in the endpoint that spreads beyond the queried last timestamp
2407
    *w = getAlignQueryTimeWindow(pInterval, ts);
353,041✔
2408

2409
    int64_t key = w->skey;
353,071✔
2410
    while (key < ts) {  // moving towards end
1,145,686✔
2411
      key = getNextTimeWindowStart(pInterval, key, TSDB_ORDER_ASC);
1,131,660✔
2412
      if (key > ts) {
1,131,658✔
2413
        break;
339,043✔
2414
      }
2415

2416
      w->skey = key;
792,615✔
2417
    }
2418
    w->ekey = taosTimeAdd(w->skey, pInterval->interval, pInterval->intervalUnit, pInterval->precision, NULL) - 1;
353,069✔
2419
  }
2420
}
30,524,774✔
2421

2422
static STimeWindow doCalculateTimeWindow(int64_t ts, SInterval* pInterval) {
1,115,011✔
2423
  STimeWindow w = {0};
1,115,011✔
2424

2425
  w.skey = taosTimeTruncate(ts, pInterval);
1,115,011✔
2426
  w.ekey = taosTimeGetIntervalEnd(w.skey, pInterval);
1,115,168✔
2427
  return w;
1,115,086✔
2428
}
2429

2430
STimeWindow getFirstQualifiedTimeWindow(int64_t ts, STimeWindow* pWindow, SInterval* pInterval, int32_t order) {
488,660✔
2431
  STimeWindow win = *pWindow;
488,660✔
2432
  STimeWindow save = win;
488,660✔
2433
  while (win.skey <= ts && win.ekey >= ts) {
2,136,628✔
2434
    save = win;
1,647,966✔
2435
    // get previous time window
2436
    getNextTimeWindow(pInterval, &win, order == TSDB_ORDER_ASC ? TSDB_ORDER_DESC : TSDB_ORDER_ASC);
1,647,966✔
2437
  }
2438

2439
  return save;
488,662✔
2440
}
2441

2442
// get the correct time window according to the handled timestamp
2443
// todo refactor
2444
STimeWindow getActiveTimeWindow(SDiskbasedBuf* pBuf, SResultRowInfo* pResultRowInfo, int64_t ts, SInterval* pInterval,
32,284,484✔
2445
                                int32_t order) {
2446
  STimeWindow w = {0};
32,284,484✔
2447
  if (pResultRowInfo->cur.pageId == -1) {  // the first window, from the previous stored value
32,284,484✔
2448
    getInitialStartTimeWindow(pInterval, ts, &w, (order == TSDB_ORDER_ASC));
30,527,071✔
2449
    return w;
30,524,741✔
2450
  }
2451

2452
  SResultRow* pRow = getResultRowByPos(pBuf, &pResultRowInfo->cur, false);
1,757,413✔
2453
  if (pRow) {
1,757,207!
2454
    w = pRow->win;
1,757,219✔
2455
  }
2456

2457
  // in case of typical time window, we can calculate time window directly.
2458
  if (w.skey > ts || w.ekey < ts) {
1,757,207✔
2459
    w = doCalculateTimeWindow(ts, pInterval);
1,115,002✔
2460
  }
2461

2462
  if (pInterval->interval != pInterval->sliding) {
1,757,293✔
2463
    // it is an sliding window query, in which sliding value is not equalled to
2464
    // interval value, and we need to find the first qualified time window.
2465
    w = getFirstQualifiedTimeWindow(ts, &w, pInterval, order);
488,660✔
2466
  }
2467

2468
  return w;
1,757,301✔
2469
}
2470

2471
TSKEY getNextTimeWindowStart(const SInterval* pInterval, TSKEY start, int32_t order) {
261,173,280✔
2472
  int32_t factor = GET_FORWARD_DIRECTION_FACTOR(order);
261,173,280✔
2473
  TSKEY   nextStart = taosTimeAdd(start, -1 * pInterval->offset, pInterval->offsetUnit, pInterval->precision, NULL);
261,173,280✔
2474
  nextStart = taosTimeAdd(nextStart, factor * pInterval->sliding, pInterval->slidingUnit, pInterval->precision, NULL);
261,011,561✔
2475
  nextStart = taosTimeAdd(nextStart, pInterval->offset, pInterval->offsetUnit, pInterval->precision, NULL);
261,010,662✔
2476
  return nextStart;
260,763,780✔
2477
}
2478

2479
void getNextTimeWindow(const SInterval* pInterval, STimeWindow* tw, int32_t order) {
260,136,631✔
2480
  tw->skey = getNextTimeWindowStart(pInterval, tw->skey, order);
260,136,631✔
2481
  tw->ekey = taosTimeAdd(tw->skey, pInterval->interval, pInterval->intervalUnit, pInterval->precision, NULL) - 1;
259,544,001✔
2482
}
259,826,446✔
2483

2484
bool hasLimitOffsetInfo(SLimitInfo* pLimitInfo) {
12,784,271✔
2485
  return (pLimitInfo->limit.limit != -1 || pLimitInfo->limit.offset != -1 || pLimitInfo->slimit.limit != -1 ||
25,546,204!
2486
          pLimitInfo->slimit.offset != -1);
12,761,933✔
2487
}
2488

2489
bool hasSlimitOffsetInfo(SLimitInfo* pLimitInfo) {
×
2490
  return (pLimitInfo->slimit.limit != -1 || pLimitInfo->slimit.offset != -1);
×
2491
}
2492

2493
void initLimitInfo(const SNode* pLimit, const SNode* pSLimit, SLimitInfo* pLimitInfo) {
13,748,216✔
2494
  SLimit limit = {.limit = getLimit(pLimit), .offset = getOffset(pLimit)};
13,748,216✔
2495
  SLimit slimit = {.limit = getLimit(pSLimit), .offset = getOffset(pSLimit)};
13,754,281✔
2496

2497
  pLimitInfo->limit = limit;
13,760,602✔
2498
  pLimitInfo->slimit = slimit;
13,760,602✔
2499
  pLimitInfo->remainOffset = limit.offset;
13,760,602✔
2500
  pLimitInfo->remainGroupOffset = slimit.offset;
13,760,602✔
2501
}
13,760,602✔
2502

2503
void resetLimitInfoForNextGroup(SLimitInfo* pLimitInfo) {
2,328,819✔
2504
  pLimitInfo->numOfOutputRows = 0;
2,328,819✔
2505
  pLimitInfo->remainOffset = pLimitInfo->limit.offset;
2,328,819✔
2506
}
2,328,819✔
2507

2508
int32_t tableListGetSize(const STableListInfo* pTableList, int32_t* pRes) {
17,058,936✔
2509
  if (taosArrayGetSize(pTableList->pTableList) != taosHashGetSize(pTableList->map)) {
17,058,936!
2510
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR));
×
2511
    return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
2512
  }
2513
  (*pRes) = taosArrayGetSize(pTableList->pTableList);
17,071,026✔
2514
  return TSDB_CODE_SUCCESS;
17,069,053✔
2515
}
2516

2517
uint64_t tableListGetSuid(const STableListInfo* pTableList) { return pTableList->idInfo.suid; }
73,674✔
2518

2519
STableKeyInfo* tableListGetInfo(const STableListInfo* pTableList, int32_t index) {
7,645,150✔
2520
  if (taosArrayGetSize(pTableList->pTableList) == 0) {
7,645,150✔
2521
    return NULL;
554✔
2522
  }
2523

2524
  return taosArrayGet(pTableList->pTableList, index);
7,645,467✔
2525
}
2526

2527
int32_t tableListFind(const STableListInfo* pTableList, uint64_t uid, int32_t startIndex) {
181✔
2528
  int32_t numOfTables = taosArrayGetSize(pTableList->pTableList);
181✔
2529
  if (startIndex >= numOfTables) {
181!
2530
    return -1;
×
2531
  }
2532

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

2546
void tableListGetSourceTableInfo(const STableListInfo* pTableList, uint64_t* psuid, uint64_t* uid, int32_t* type) {
18,807✔
2547
  *psuid = pTableList->idInfo.suid;
18,807✔
2548
  *uid = pTableList->idInfo.uid;
18,807✔
2549
  *type = pTableList->idInfo.tableType;
18,807✔
2550
}
18,807✔
2551

2552
uint64_t tableListGetTableGroupId(const STableListInfo* pTableList, uint64_t tableUid) {
15,573,705✔
2553
  int32_t* slot = taosHashGet(pTableList->map, &tableUid, sizeof(tableUid));
15,573,705✔
2554
  if (slot == NULL) {
15,576,806!
2555
    return -1;
×
2556
  }
2557

2558
  STableKeyInfo* pKeyInfo = taosArrayGet(pTableList->pTableList, *slot);
15,576,806✔
2559

2560
  return pKeyInfo->groupId;
15,575,079✔
2561
}
2562

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

2572
  STableKeyInfo keyInfo = {.uid = uid, .groupId = gid};
10,071✔
2573
  void*         p = taosHashGet(pTableList->map, &uid, sizeof(uid));
10,071✔
2574
  if (p != NULL) {
10,084✔
2575
    qInfo("table:%" PRId64 " already in tableIdList, ignore it", uid);
3!
2576
    goto _end;
3✔
2577
  }
2578

2579
  void* tmp = taosArrayPush(pTableList->pTableList, &keyInfo);
10,081✔
2580
  QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
10,070!
2581

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

2590
_end:
10,086✔
2591
  if (code != TSDB_CODE_SUCCESS) {
10,087!
2592
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
2593
  } else {
2594
    qDebug("uid:%" PRIu64 ", groupId:%" PRIu64 " added into table list, slot:%d, total:%d", uid, gid, slot, slot + 1);
10,087✔
2595
  }
2596

2597
  return code;
10,087✔
2598
}
2599

2600
int32_t tableListGetGroupList(const STableListInfo* pTableList, int32_t ordinalGroupIndex, STableKeyInfo** pKeyInfo,
2,759,612✔
2601
                              int32_t* size) {
2602
  int32_t totalGroups = tableListGetOutputGroups(pTableList);
2,759,612✔
2603
  int32_t numOfTables = 0;
2,766,091✔
2604
  int32_t code = tableListGetSize(pTableList, &numOfTables);
2,766,091✔
2605
  if (code != TSDB_CODE_SUCCESS) {
2,762,333!
2606
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
2607
    return code;
×
2608
  }
2609

2610
  if (ordinalGroupIndex < 0 || ordinalGroupIndex >= totalGroups) {
2,762,333!
2611
    return TSDB_CODE_INVALID_PARA;
×
2612
  }
2613

2614
  // here handle two special cases:
2615
  // 1. only one group exists, and 2. one table exists for each group.
2616
  if (totalGroups == 1) {
2,762,333✔
2617
    *size = numOfTables;
2,761,712✔
2618
    *pKeyInfo = (*size == 0) ? NULL : taosArrayGet(pTableList->pTableList, 0);
2,761,712✔
2619
    return TSDB_CODE_SUCCESS;
2,758,828✔
2620
  } else if (totalGroups == numOfTables) {
621!
2621
    *size = 1;
986✔
2622
    *pKeyInfo = taosArrayGet(pTableList->pTableList, ordinalGroupIndex);
986✔
2623
    return TSDB_CODE_SUCCESS;
986✔
2624
  }
2625

2626
  int32_t offset = pTableList->groupOffset[ordinalGroupIndex];
×
2627
  if (ordinalGroupIndex < totalGroups - 1) {
×
2628
    *size = pTableList->groupOffset[ordinalGroupIndex + 1] - offset;
532✔
2629
  } else {
2630
    *size = numOfTables - offset;
×
2631
  }
2632

2633
  *pKeyInfo = taosArrayGet(pTableList->pTableList, offset);
×
2634
  return TSDB_CODE_SUCCESS;
665✔
2635
}
2636

2637
int32_t tableListGetOutputGroups(const STableListInfo* pTableList) { return pTableList->numOfOuputGroups; }
8,240,207✔
2638

2639
bool oneTableForEachGroup(const STableListInfo* pTableList) { return pTableList->oneTableForEachGroup; }
6,416✔
2640

2641
STableListInfo* tableListCreate() {
4,855,953✔
2642
  STableListInfo* pListInfo = taosMemoryCalloc(1, sizeof(STableListInfo));
4,855,953!
2643
  if (pListInfo == NULL) {
4,866,986!
2644
    return NULL;
×
2645
  }
2646

2647
  pListInfo->remainGroups = NULL;
4,866,986✔
2648
  pListInfo->pTableList = taosArrayInit(4, sizeof(STableKeyInfo));
4,866,986✔
2649
  if (pListInfo->pTableList == NULL) {
4,867,472!
2650
    goto _error;
×
2651
  }
2652

2653
  pListInfo->map = taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
4,867,472✔
2654
  if (pListInfo->map == NULL) {
4,865,580!
2655
    goto _error;
×
2656
  }
2657

2658
  pListInfo->numOfOuputGroups = 1;
4,865,580✔
2659
  return pListInfo;
4,865,580✔
2660

2661
_error:
×
2662
  tableListDestroy(pListInfo);
×
2663
  return NULL;
×
2664
}
2665

2666
void tableListDestroy(STableListInfo* pTableListInfo) {
4,867,891✔
2667
  if (pTableListInfo == NULL) {
4,867,891!
2668
    return;
×
2669
  }
2670

2671
  taosArrayDestroy(pTableListInfo->pTableList);
4,867,891✔
2672
  taosMemoryFreeClear(pTableListInfo->groupOffset);
4,868,697!
2673

2674
  taosHashCleanup(pTableListInfo->map);
4,868,697✔
2675
  taosHashCleanup(pTableListInfo->remainGroups);
4,868,190✔
2676
  pTableListInfo->pTableList = NULL;
4,868,484✔
2677
  pTableListInfo->map = NULL;
4,868,484✔
2678
  taosMemoryFree(pTableListInfo);
4,868,484!
2679
}
2680

2681
void tableListClear(STableListInfo* pTableListInfo) {
2,115✔
2682
  if (pTableListInfo == NULL) {
2,115!
2683
    return;
×
2684
  }
2685

2686
  taosArrayClear(pTableListInfo->pTableList);
2,115✔
2687
  taosHashClear(pTableListInfo->map);
2,115✔
2688
  taosHashClear(pTableListInfo->remainGroups);
2,116✔
2689
  taosMemoryFree(pTableListInfo->groupOffset);
2,116!
2690
  pTableListInfo->numOfOuputGroups = 1;
2,116✔
2691
  pTableListInfo->oneTableForEachGroup = false;
2,116✔
2692
}
2693

2694
static int32_t orderbyGroupIdComparFn(const void* p1, const void* p2) {
93,955✔
2695
  STableKeyInfo* pInfo1 = (STableKeyInfo*)p1;
93,955✔
2696
  STableKeyInfo* pInfo2 = (STableKeyInfo*)p2;
93,955✔
2697

2698
  if (pInfo1->groupId == pInfo2->groupId) {
93,955✔
2699
    return 0;
169✔
2700
  } else {
2701
    return pInfo1->groupId < pInfo2->groupId ? -1 : 1;
93,786✔
2702
  }
2703
}
2704

2705
static int32_t sortTableGroup(STableListInfo* pTableListInfo) {
919✔
2706
  taosArraySort(pTableListInfo->pTableList, orderbyGroupIdComparFn);
919✔
2707
  int32_t size = taosArrayGetSize(pTableListInfo->pTableList);
919✔
2708

2709
  SArray* pList = taosArrayInit(4, sizeof(int32_t));
919✔
2710
  if (!pList) {
919!
2711
    return terrno;
×
2712
  }
2713

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

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

2728
  for (int32_t i = 1; i < size; ++i) {
2,892✔
2729
    pInfo = taosArrayGet(pTableListInfo->pTableList, i);
1,974✔
2730
    if (!pInfo) {
1,973✔
2731
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
2!
2732
      return terrno;
2✔
2733
    }
2734
    if (pInfo->groupId != gid) {
1,971✔
2735
      tmp = taosArrayPush(pList, &i);
1,806✔
2736
      if (!tmp) {
1,806!
2737
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
2738
        return terrno;
×
2739
      }
2740
      gid = pInfo->groupId;
1,806✔
2741
    }
2742
  }
2743

2744
  pTableListInfo->numOfOuputGroups = taosArrayGetSize(pList);
918✔
2745
  pTableListInfo->groupOffset = taosMemoryMalloc(sizeof(int32_t) * pTableListInfo->numOfOuputGroups);
919!
2746
  if (pTableListInfo->groupOffset == NULL) {
919!
2747
    taosArrayDestroy(pList);
×
2748
    return terrno;
×
2749
  }
2750

2751
  memcpy(pTableListInfo->groupOffset, taosArrayGet(pList, 0), sizeof(int32_t) * pTableListInfo->numOfOuputGroups);
919✔
2752
  taosArrayDestroy(pList);
919✔
2753
  return TSDB_CODE_SUCCESS;
919✔
2754
}
2755

2756
int32_t buildGroupIdMapForAllTables(STableListInfo* pTableListInfo, SReadHandle* pHandle, SScanPhysiNode* pScanNode,
4,559,492✔
2757
                                    SNodeList* group, bool groupSort, uint8_t* digest, SStorageAPI* pAPI) {
2758
  int32_t code = TSDB_CODE_SUCCESS;
4,559,492✔
2759

2760
  bool   groupByTbname = groupbyTbname(group);
4,559,492✔
2761
  size_t numOfTables = taosArrayGetSize(pTableListInfo->pTableList);
4,560,056✔
2762
  if (!numOfTables) {
4,557,909!
2763
    return code;
×
2764
  }
2765
  if (group == NULL || groupByTbname) {
4,557,909✔
2766
    if (tsCountAlwaysReturnValue && QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pScanNode) &&
4,494,132✔
2767
        ((STableScanPhysiNode*)pScanNode)->needCountEmptyTable) {
2,515,251✔
2768
      pTableListInfo->remainGroups =
178,153✔
2769
          taosHashInit(numOfTables, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
178,156✔
2770
      if (pTableListInfo->remainGroups == NULL) {
178,153!
2771
        return terrno;
×
2772
      }
2773

2774
      for (int i = 0; i < numOfTables; i++) {
638,922✔
2775
        STableKeyInfo* info = taosArrayGet(pTableListInfo->pTableList, i);
460,643✔
2776
        if (!info) {
460,604✔
2777
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
3!
2778
          return terrno;
3✔
2779
        }
2780
        info->groupId = groupByTbname ? info->uid : 0;
460,601✔
2781

2782
        int32_t tempRes = taosHashPut(pTableListInfo->remainGroups, &(info->groupId), sizeof(info->groupId),
460,601✔
2783
                                      &(info->uid), sizeof(info->uid));
460,601✔
2784
        if (tempRes != TSDB_CODE_SUCCESS && tempRes != TSDB_CODE_DUP_KEY) {
460,769!
2785
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(tempRes));
×
2786
          return tempRes;
×
2787
        }
2788
      }
2789
    } else {
2790
      for (int32_t i = 0; i < numOfTables; i++) {
16,148,215✔
2791
        STableKeyInfo* info = taosArrayGet(pTableListInfo->pTableList, i);
11,830,746✔
2792
        if (!info) {
11,832,356✔
2793
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
117!
2794
          return terrno;
117✔
2795
        }
2796
        info->groupId = groupByTbname ? info->uid : 0;
11,832,239✔
2797
      }
2798
    }
2799

2800
    pTableListInfo->oneTableForEachGroup = groupByTbname;
4,495,748✔
2801
    if (numOfTables == 1 && pTableListInfo->idInfo.tableType == TSDB_CHILD_TABLE) {
4,495,748✔
2802
      pTableListInfo->oneTableForEachGroup = true;
419,235✔
2803
    }
2804

2805
    if (groupSort && groupByTbname) {
4,495,748✔
2806
      taosArraySort(pTableListInfo->pTableList, orderbyGroupIdComparFn);
28,578✔
2807
      pTableListInfo->numOfOuputGroups = numOfTables;
28,582✔
2808
    } else if (groupByTbname && pScanNode->groupOrderScan) {
4,467,170✔
2809
      pTableListInfo->numOfOuputGroups = numOfTables;
133✔
2810
    } else {
2811
      pTableListInfo->numOfOuputGroups = 1;
4,467,037✔
2812
    }
2813
  } else {
2814
    bool initRemainGroups = false;
63,777✔
2815
    if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pScanNode)) {
63,777✔
2816
      STableScanPhysiNode* pTableScanNode = (STableScanPhysiNode*)pScanNode;
63,149✔
2817
      if (tsCountAlwaysReturnValue && pTableScanNode->needCountEmptyTable &&
63,149✔
2818
          !(groupSort || pScanNode->groupOrderScan)) {
13,087!
2819
        initRemainGroups = true;
13,043✔
2820
      }
2821
    }
2822

2823
    code = getColInfoResultForGroupby(pHandle->vnode, group, pTableListInfo, digest, pAPI, initRemainGroups);
63,777✔
2824
    if (code != TSDB_CODE_SUCCESS) {
63,811✔
2825
      return code;
15✔
2826
    }
2827

2828
    if (pScanNode->groupOrderScan) pTableListInfo->numOfOuputGroups = taosArrayGetSize(pTableListInfo->pTableList);
63,796✔
2829

2830
    if (groupSort || pScanNode->groupOrderScan) {
63,796✔
2831
      code = sortTableGroup(pTableListInfo);
910✔
2832
    }
2833
  }
2834

2835
  // add all table entry in the hash map
2836
  size_t size = taosArrayGetSize(pTableListInfo->pTableList);
4,559,557✔
2837
  for (int32_t i = 0; i < size; ++i) {
17,126,195✔
2838
    STableKeyInfo* p = taosArrayGet(pTableListInfo->pTableList, i);
12,554,003✔
2839
    if (!p) {
12,538,244!
2840
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
2841
      return terrno;
×
2842
    }
2843
    int32_t        tempRes = taosHashPut(pTableListInfo->map, &p->uid, sizeof(uint64_t), &i, sizeof(int32_t));
12,538,244✔
2844
    if (tempRes != TSDB_CODE_SUCCESS && tempRes != TSDB_CODE_DUP_KEY) {
12,568,803!
2845
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(tempRes));
×
2846
      return tempRes;
×
2847
    }
2848
  }
2849

2850
  return code;
4,572,192✔
2851
}
2852

2853
int32_t createScanTableListInfo(SScanPhysiNode* pScanNode, SNodeList* pGroupTags, bool groupSort, SReadHandle* pHandle,
4,765,022✔
2854
                                STableListInfo* pTableListInfo, SNode* pTagCond, SNode* pTagIndexCond,
2855
                                SExecTaskInfo* pTaskInfo) {
2856
  int64_t     st = taosGetTimestampUs();
4,768,637✔
2857
  const char* idStr = GET_TASKID(pTaskInfo);
4,768,637✔
2858

2859
  if (pHandle == NULL) {
4,768,637!
2860
    qError("invalid handle, in creating operator tree, %s", idStr);
×
2861
    return TSDB_CODE_INVALID_PARA;
×
2862
  }
2863

2864
  uint8_t digest[17] = {0};
4,768,637✔
2865
  int32_t code = getTableList(pHandle->vnode, pScanNode, pTagCond, pTagIndexCond, pTableListInfo, digest, idStr,
4,768,637✔
2866
                              &pTaskInfo->storageAPI);
2867
  if (code != TSDB_CODE_SUCCESS) {
4,772,082✔
2868
    qError("failed to getTableList, code: %s", tstrerror(code));
8!
2869
    return code;
8✔
2870
  }
2871

2872
  int32_t numOfTables = taosArrayGetSize(pTableListInfo->pTableList);
4,772,074✔
2873

2874
  int64_t st1 = taosGetTimestampUs();
4,771,716✔
2875
  pTaskInfo->cost.extractListTime = (st1 - st) / 1000.0;
4,771,716✔
2876
  qDebug("extract queried table list completed, %d tables, elapsed time:%.2f ms %s", numOfTables,
4,771,716✔
2877
         pTaskInfo->cost.extractListTime, idStr);
2878

2879
  if (numOfTables == 0) {
4,771,680✔
2880
    qDebug("no table qualified for query, %s" PRIx64, idStr);
208,785✔
2881
    return TSDB_CODE_SUCCESS;
208,788✔
2882
  }
2883

2884
  code = buildGroupIdMapForAllTables(pTableListInfo, pHandle, pScanNode, pGroupTags, groupSort, digest,
4,562,895✔
2885
                                     &pTaskInfo->storageAPI);
2886
  if (code != TSDB_CODE_SUCCESS) {
4,567,096✔
2887
    return code;
15✔
2888
  }
2889

2890
  pTaskInfo->cost.groupIdMapTime = (taosGetTimestampUs() - st1) / 1000.0;
4,566,745✔
2891
  qDebug("generate group id map completed, elapsed time:%.2f ms %s", pTaskInfo->cost.groupIdMapTime, idStr);
4,566,745✔
2892

2893
  return TSDB_CODE_SUCCESS;
4,566,787✔
2894
}
2895

2896
char* getStreamOpName(uint16_t opType) {
2,996,425✔
2897
  switch (opType) {
2,996,425!
2898
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN:
634,633✔
2899
      return "stream scan";
634,633✔
2900
    case QUERY_NODE_PHYSICAL_PLAN_PROJECT:
31,658✔
2901
      return "project";
31,658✔
2902
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL:
1,517,066✔
2903
      return "interval single";
1,517,066✔
2904
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_INTERVAL:
12,305✔
2905
      return "interval final";
12,305✔
2906
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_INTERVAL:
29,776✔
2907
      return "interval semi";
29,776✔
2908
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_MID_INTERVAL:
345✔
2909
      return "interval mid";
345✔
2910
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FILL:
5,713✔
2911
      return "stream fill";
5,713✔
2912
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SESSION:
10,428✔
2913
      return "session single";
10,428✔
2914
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_SESSION:
1,537✔
2915
      return "session semi";
1,537✔
2916
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_SESSION:
1,283✔
2917
      return "session final";
1,283✔
2918
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_STATE:
7,524✔
2919
      return "state single";
7,524✔
2920
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_PARTITION:
740,291✔
2921
      return "stream partitionby";
740,291✔
2922
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_EVENT:
831✔
2923
      return "stream event";
831✔
2924
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_COUNT:
10,044✔
2925
      return "stream count";
10,044✔
2926
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERP_FUNC:
7,442✔
2927
      return "stream interp";
7,442✔
2928
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_CONTINUE_INTERVAL:
4,382✔
2929
      return "interval continue";
4,382✔
2930
  }
2931
  return "";
×
2932
}
2933

2934
void printDataBlock(SSDataBlock* pBlock, const char* flag, const char* taskIdStr) {
1,279,775✔
2935
  if (!pBlock) {
1,279,775!
2936
    qDebug("%s===stream===%s: Block is Null", taskIdStr, flag);
×
2937
    return;
×
2938
  } else if (pBlock->info.rows == 0) {
1,279,775✔
2939
    qDebug("%s===stream===%s: Block is Empty. block type %d", taskIdStr, flag, pBlock->info.type);
23,289✔
2940
    return;
23,288✔
2941
  }
2942
  if (qDebugFlag & DEBUG_DEBUG) {
1,256,486✔
2943
    char*   pBuf = NULL;
48,163✔
2944
    int32_t code = dumpBlockData(pBlock, flag, &pBuf, taskIdStr);
48,163✔
2945
    if (code == 0) {
48,162!
2946
      qDebug("%s", pBuf);
48,162!
2947
      taosMemoryFree(pBuf);
48,163!
2948
    }
2949
  }
2950
}
2951

2952
void printSpecDataBlock(SSDataBlock* pBlock, const char* flag, const char* opStr, const char* taskIdStr) {
1,667,908✔
2953
  if (!pBlock) {
1,667,908!
2954
    qDebug("%s===stream===%s %s: Block is Null", taskIdStr, flag, opStr);
×
2955
    return;
×
2956
  } else if (pBlock->info.rows == 0) {
1,667,908✔
2957
    qDebug("%s===stream===%s %s: Block is Empty. block type %d.skey:%" PRId64 ",ekey:%" PRId64 ",version%" PRId64,
12,562✔
2958
           taskIdStr, flag, opStr, pBlock->info.type, pBlock->info.window.skey, pBlock->info.window.ekey,
2959
           pBlock->info.version);
2960
    return;
12,563✔
2961
  }
2962
  if (qDebugFlag & DEBUG_DEBUG) {
1,655,346✔
2963
    char* pBuf = NULL;
59,874✔
2964
    char  flagBuf[64];
2965
    snprintf(flagBuf, sizeof(flagBuf), "%s %s", flag, opStr);
59,874✔
2966
    int32_t code = dumpBlockData(pBlock, flagBuf, &pBuf, taskIdStr);
59,874✔
2967
    if (code == 0) {
59,873!
2968
      qDebug("%s", pBuf);
59,873!
2969
      taosMemoryFree(pBuf);
59,874!
2970
    }
2971
  }
2972
}
2973

2974
TSKEY getStartTsKey(STimeWindow* win, const TSKEY* tsCols) { return tsCols == NULL ? win->skey : tsCols[0]; }
4,262,340!
2975

2976
void updateTimeWindowInfo(SColumnInfoData* pColData, STimeWindow* pWin, int64_t delta) {
443,861,647✔
2977
  int64_t* ts = (int64_t*)pColData->pData;
443,861,647✔
2978

2979
  int64_t duration = pWin->ekey - pWin->skey + delta;
443,861,647✔
2980
  ts[2] = duration;            // set the duration
443,861,647✔
2981
  ts[3] = pWin->skey;          // window start key
443,861,647✔
2982
  ts[4] = pWin->ekey + delta;  // window end key
443,861,647✔
2983
}
443,861,647✔
2984

2985
int32_t compKeys(const SArray* pSortGroupCols, const char* oldkeyBuf, int32_t oldKeysLen, const SSDataBlock* pBlock,
21,394,157✔
2986
                 int32_t rowIndex) {
2987
  SColumnDataAgg* pColAgg = NULL;
21,394,157✔
2988
  const char*     isNull = oldkeyBuf;
21,394,157✔
2989
  const char*     p = oldkeyBuf + sizeof(int8_t) * pSortGroupCols->size;
21,394,157✔
2990

2991
  for (int32_t i = 0; i < pSortGroupCols->size; ++i) {
53,766,598✔
2992
    const SColumn*         pCol = (SColumn*)TARRAY_GET_ELEM(pSortGroupCols, i);
32,855,353✔
2993
    const SColumnInfoData* pColInfoData = TARRAY_GET_ELEM(pBlock->pDataBlock, pCol->slotId);
32,855,353✔
2994
    if (pBlock->pBlockAgg) pColAgg = &pBlock->pBlockAgg[pCol->slotId];
32,855,353!
2995

2996
    if (colDataIsNull(pColInfoData, pBlock->info.rows, rowIndex, pColAgg)) {
65,710,706!
2997
      if (isNull[i] != 1) return 1;
2,185,934✔
2998
    } else {
2999
      if (isNull[i] != 0) return 1;
30,669,419✔
3000
      const char* val = colDataGetData(pColInfoData, rowIndex);
30,655,197!
3001
      if (pCol->type == TSDB_DATA_TYPE_JSON) {
30,655,197!
3002
        int32_t len = getJsonValueLen(val);
×
3003
        if (memcmp(p, val, len) != 0) return 1;
×
3004
        p += len;
×
3005
      } else if (IS_VAR_DATA_TYPE(pCol->type)) {
30,655,197!
3006
        if (memcmp(p, val, varDataTLen(val)) != 0) return 1;
10,462,113✔
3007
        p += varDataTLen(val);
10,326,089✔
3008
      } else {
3009
        if (0 != memcmp(p, val, pCol->bytes)) return 1;
20,193,084✔
3010
        p += pCol->bytes;
19,860,610✔
3011
      }
3012
    }
3013
  }
3014
  if ((int32_t)(p - oldkeyBuf) != oldKeysLen) return 1;
20,911,245✔
3015
  return 0;
20,910,949✔
3016
}
3017

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

3024
  for (int32_t i = 0; i < colNum; ++i) {
1,438,909✔
3025
    const SColumn*         pCol = (SColumn*)TARRAY_GET_ELEM(pSortGroupCols, i);
956,173✔
3026
    const SColumnInfoData* pColInfoData = TARRAY_GET_ELEM(pBlock->pDataBlock, pCol->slotId);
956,173✔
3027
    if (pCol->slotId > pBlock->pDataBlock->size) continue;
956,173!
3028

3029
    if (pBlock->pBlockAgg) pColAgg = &pBlock->pBlockAgg[pCol->slotId];
956,173!
3030

3031
    if (colDataIsNull(pColInfoData, pBlock->info.rows, rowIndex, pColAgg)) {
1,912,346!
3032
      isNull[i] = 1;
34,562✔
3033
    } else {
3034
      isNull[i] = 0;
921,611✔
3035
      const char* val = colDataGetData(pColInfoData, rowIndex);
921,611!
3036
      if (pCol->type == TSDB_DATA_TYPE_JSON) {
921,611!
3037
        int32_t len = getJsonValueLen(val);
×
3038
        memcpy(p, val, len);
×
3039
        p += len;
×
3040
      } else if (IS_VAR_DATA_TYPE(pCol->type)) {
921,611✔
3041
        varDataCopy(p, val);
138,989✔
3042
        p += varDataTLen(val);
138,989✔
3043
      } else {
3044
        memcpy(p, val, pCol->bytes);
782,622✔
3045
        p += pCol->bytes;
782,622✔
3046
      }
3047
    }
3048
  }
3049
  return (int32_t)(p - keyBuf);
482,736✔
3050
}
3051

3052
uint64_t calcGroupId(char* pData, int32_t len) {
17,253,030✔
3053
  T_MD5_CTX context;
3054
  tMD5Init(&context);
17,253,030✔
3055
  tMD5Update(&context, (uint8_t*)pData, len);
17,275,563✔
3056
  tMD5Final(&context);
17,368,319✔
3057

3058
  // NOTE: only extract the initial 8 bytes of the final MD5 digest
3059
  uint64_t id = 0;
17,523,896✔
3060
  memcpy(&id, context.digest, sizeof(uint64_t));
17,523,896✔
3061
  if (0 == id) memcpy(&id, context.digest + 8, sizeof(uint64_t));
17,523,896!
3062
  return id;
17,523,896✔
3063
}
3064

3065
SNodeList* makeColsNodeArrFromSortKeys(SNodeList* pSortKeys) {
944✔
3066
  SNode*     node;
3067
  SNodeList* ret = NULL;
944✔
3068
  FOREACH(node, pSortKeys) {
2,880!
3069
    SOrderByExprNode* pSortKey = (SOrderByExprNode*)node;
1,934✔
3070
    int32_t           code = nodesListMakeAppend(&ret, pSortKey->pExpr);
1,934✔
3071
    if (code != TSDB_CODE_SUCCESS) {
1,936!
3072
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
3073
      terrno = code;
×
3074
      return NULL;
×
3075
    }
3076
  }
3077
  return ret;
946✔
3078
}
3079

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

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

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