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

taosdata / TDengine / #3615

18 Feb 2025 07:41AM UTC coverage: 62.953% (+1.6%) from 61.4%
#3615

push

travis-ci

web-flow
Merge pull request #29812 from taosdata/doc/analysis

doc: update tdgpt doc.

146885 of 299602 branches covered (49.03%)

Branch coverage included in aggregate %.

230802 of 300346 relevant lines covered (76.85%)

17263824.17 hits per line

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

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

16
#include "function.h"
17
#include "functionMgt.h"
18
#include "index.h"
19
#include "os.h"
20
#include "query.h"
21
#include "tdatablock.h"
22
#include "thash.h"
23
#include "tmsg.h"
24
#include "ttime.h"
25

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

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

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

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

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

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

57
static int64_t getLimit(const SNode* pLimit) { return (NULL == pLimit || NULL == ((SLimitNode*)pLimit)->limit) ? -1 : ((SLimitNode*)pLimit)->limit->datum.i; }
25,651,366!
58
static int64_t getOffset(const SNode* pLimit) { return (NULL == pLimit || NULL == ((SLimitNode*)pLimit)->offset) ? -1 : ((SLimitNode*)pLimit)->offset->datum.i; }
25,660,370✔
59
static void    releaseColInfoData(void* pCol);
60

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

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

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

74
  if (entrySize > 0) {
68,987,505!
75
    memset(pResultRow->pEntryInfo, 0, entrySize);
68,990,551✔
76
  }
77
}
68,987,505✔
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) {
7,478,517✔
85
  int32_t rowSize = (numOfOutput * sizeof(SResultRowEntryInfo)) + sizeof(SResultRow);
7,478,517✔
86

87
  for (int32_t i = 0; i < numOfOutput; ++i) {
29,945,246✔
88
    rowSize += pCtx[i].resDataInfo.interBufSize;
22,466,729✔
89
  }
90

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

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

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

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

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

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

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

180
  char *pBuf = *outBuf;
1,198,757✔
181
  pResultRow->version = FUNCTION_RESULT_INFO_VERSION;
1,198,757✔
182
  (void)memcpy(pBuf, pResultRow, sizeof(SResultRow));
1,198,757✔
183
  pBuf += sizeof(SResultRow);
1,198,757✔
184
  for (int32_t i = 0; i < pSup->numOfExprs; ++i) {
4,859,650✔
185
    size_t len = sizeof(SResultRowEntryInfo) + pCtx[i].resDataInfo.interBufSize;
3,660,894✔
186
    *(int32_t *) pBuf = (int32_t)len;
3,660,894✔
187
    pBuf += sizeof(int32_t);
3,660,894✔
188
    (void)memcpy(pBuf, getResultEntryInfo(pResultRow, i, offset), len);
3,660,894✔
189
    pBuf += len;
3,660,893✔
190
  }
191

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

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

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

217
int32_t resultrowComparAsc(const void* p1, const void* p2) {
2,104,061,798✔
218
  SResKeyPos* pp1 = *(SResKeyPos**)p1;
2,104,061,798✔
219
  SResKeyPos* pp2 = *(SResKeyPos**)p2;
2,104,061,798✔
220

221
  if (pp1->groupId == pp2->groupId) {
2,104,061,798✔
222
    int64_t pts1 = *(int64_t*)pp1->key;
1,920,568,810✔
223
    int64_t pts2 = *(int64_t*)pp2->key;
1,920,568,810✔
224

225
    if (pts1 == pts2) {
1,920,568,810!
226
      return 0;
×
227
    } else {
228
      return pts1 < pts2 ? -1 : 1;
1,920,568,810✔
229
    }
230
  } else {
231
    return pp1->groupId < pp2->groupId ? -1 : 1;
183,492,988✔
232
  }
233
}
234

235
static int32_t resultrowComparDesc(const void* p1, const void* p2) { return resultrowComparAsc(p2, p1); }
47,633,909✔
236

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

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

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

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

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

265
  pGroupResInfo->pBuf = taosMemoryMalloc(bufLen);
3,442,279!
266
  QUERY_CHECK_NULL(pGroupResInfo->pBuf, code, lino, _end, terrno);
3,447,896!
267

268
  iter = 0;
3,447,896✔
269
  while ((pData = tSimpleHashIterate(pHashmap, pData, &iter)) != NULL) {
229,345,283✔
270
    void* key = tSimpleHashGetKey(pData, &keyLen);
225,940,116✔
271

272
    SResKeyPos* p = (SResKeyPos*)(pGroupResInfo->pBuf + offset);
225,940,116✔
273

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

280
    offset += keyLen + sizeof(struct SResultRowPosition);
225,897,387✔
281
  }
282

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

289
  pGroupResInfo->index = 0;
3,448,661✔
290

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

298
void initMultiResInfoFromArrayList(SGroupResInfo* pGroupResInfo, SArray* pArrayList) {
33,301✔
299
  if (pGroupResInfo->pRows != NULL) {
33,301✔
300
    taosArrayDestroy(pGroupResInfo->pRows);
30,503✔
301
  }
302

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

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

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

316
int32_t getNumOfTotalRes(SGroupResInfo* pGroupResInfo) {
6,736,220✔
317
  if (pGroupResInfo->pRows == 0) {
6,736,220✔
318
    return 0;
1,788✔
319
  }
320

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

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

327
  if (pNodeList != NULL) {
2,197,599✔
328
    numOfCols = LIST_LENGTH(pNodeList);
2,197,379!
329
  } else {
330
    numOfCols = 0;
220✔
331
  }
332

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

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

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

361
  return pList;
2,197,721✔
362
}
363

364
SSDataBlock* createDataBlockFromDescNode(SDataBlockDescNode* pNode) {
19,487,866✔
365
  int32_t      numOfCols = LIST_LENGTH(pNode->pSlots);
19,487,866!
366
  SSDataBlock* pBlock = NULL;
19,487,866✔
367
  int32_t      code = createDataBlock(&pBlock);
19,487,866✔
368
  if (code) {
19,494,128!
369
    terrno = code;
×
370
    return NULL;
×
371
  }
372

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

378
  for (int32_t i = 0; i < numOfCols; ++i) {
107,160,136✔
379
    SSlotDescNode*  pDescNode = (SSlotDescNode*)nodesListGetNode(pNode->pSlots, i);
87,639,419✔
380
    if (!pDescNode) {
87,766,828!
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);
87,766,828✔
389
    idata.info.scale = pDescNode->dataType.scale;
87,880,867✔
390
    idata.info.precision = pDescNode->dataType.precision;
87,880,867✔
391
    idata.info.noData = pDescNode->reserve;
87,880,867✔
392

393
    code = blockDataAppendColInfo(pBlock, &idata);
87,880,867✔
394
    if (code != TSDB_CODE_SUCCESS) {
87,663,287!
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;
19,520,717✔
404
}
405

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

409
  for (int32_t i = 0; i < taosArrayGetSize(pMatchInfo->pList); ++i) {
26,951,045✔
410
    SColMatchItem* pItem = taosArrayGet(pMatchInfo->pList, i);
22,599,937✔
411
    if (!pItem) {
22,614,343✔
412
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
487!
413
      return terrno;
487✔
414
    }
415

416
    if (pItem->isPk) {
22,613,856✔
417
      SColumnInfoData* pInfoData = taosArrayGet(pDataBlock->pDataBlock, pItem->dstSlotId);
141,383✔
418
      if (!pInfoData) {
141,385!
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;
141,385✔
423
      pBlockInfo->pks[1].type = pInfoData->info.type;
141,385✔
424

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

446
  return TSDB_CODE_SUCCESS;
4,473,956✔
447
}
448

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

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

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

475
    STagVal tagVal = {0};
1,581✔
476
    tagVal.cid = pSColumnNode->colId;
1,581✔
477
    const char* p = mr->pAPI->extractTagVal(mr->me.ctbEntry.pTags, pSColumnNode->node.resType.type, &tagVal);
1,581✔
478
    if (p == NULL) {
1,581!
479
      res->node.resType.type = TSDB_DATA_TYPE_NULL;
×
480
    } else if (pSColumnNode->node.resType.type == TSDB_DATA_TYPE_JSON) {
1,581!
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,581!
488
      res->datum.p = taosMemoryCalloc(tagVal.nData + VARSTR_HEADER_SIZE + 1, 1);
1,558!
489
      if (NULL == res->datum.p) {
1,558!
490
        return DEAL_RES_ERROR;
×
491
      }
492
      memcpy(varDataVal(res->datum.p), tagVal.pData, tagVal.nData);
1,558✔
493
      varDataSetLen(res->datum.p, tagVal.nData);
1,558✔
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,581✔
501
    *pNode = (SNode*)res;
1,581✔
502
  } else if (isTbname) {
3,912!
503
    SValueNode* res = NULL;
×
504
    pCtx->code = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&res);
×
505
    if (NULL == res) {
×
506
      return DEAL_RES_ERROR;
×
507
    }
508

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

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

523
  return DEAL_RES_CONTINUE;
5,493✔
524
}
525

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

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

536
    return TSDB_CODE_SUCCESS;
×
537
  }
538

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

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

562
    return code;
×
563
  }
564

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

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

572
static EDealRes getColumn(SNode** pNode, void* pContext) {
84,961,386✔
573
  tagFilterAssist* pData = (tagFilterAssist*)pContext;
84,961,386✔
574
  SColumnNode*     pSColumnNode = NULL;
84,961,386✔
575
  if (QUERY_NODE_COLUMN == nodeType((*pNode))) {
84,961,386✔
576
    pSColumnNode = *(SColumnNode**)pNode;
3,521,376✔
577
  } else if (QUERY_NODE_FUNCTION == nodeType((*pNode))) {
81,440,010✔
578
    SFunctionNode* pFuncNode = *(SFunctionNode**)(pNode);
430,965✔
579
    if (pFuncNode->funcType == FUNCTION_TYPE_TBNAME) {
430,965✔
580
      pData->code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&pSColumnNode);
429,715✔
581
      if (NULL == pSColumnNode) {
429,710!
582
        return DEAL_RES_ERROR;
×
583
      }
584
      pSColumnNode->colId = -1;
429,710✔
585
      pSColumnNode->colType = COLUMN_TYPE_TBNAME;
429,710✔
586
      pSColumnNode->node.resType.type = TSDB_DATA_TYPE_VARCHAR;
429,710✔
587
      pSColumnNode->node.resType.bytes = TSDB_TABLE_FNAME_LEN - 1 + VARSTR_HEADER_SIZE;
429,710✔
588
      nodesDestroyNode(*pNode);
429,710✔
589
      *pNode = (SNode*)pSColumnNode;
429,713✔
590
    } else {
591
      return DEAL_RES_CONTINUE;
1,250✔
592
    }
593
  } else {
594
    return DEAL_RES_CONTINUE;
81,009,045✔
595
  }
596

597
  void* data = taosHashGet(pData->colHash, &pSColumnNode->colId, sizeof(pSColumnNode->colId));
3,951,089✔
598
  if (!data) {
3,949,419✔
599
    int32_t tempRes =
600
        taosHashPut(pData->colHash, &pSColumnNode->colId, sizeof(pSColumnNode->colId), pNode, sizeof((*pNode)));
3,273,061✔
601
    if (tempRes != TSDB_CODE_SUCCESS && tempRes != TSDB_CODE_DUP_KEY) {
3,277,523!
602
      return DEAL_RES_ERROR;
×
603
    }
604
    pSColumnNode->slotId = pData->index++;
3,277,523✔
605
    SColumnInfo cInfo = {.colId = pSColumnNode->colId,
3,277,523✔
606
                         .type = pSColumnNode->node.resType.type,
3,277,523✔
607
                         .bytes = pSColumnNode->node.resType.bytes,
3,277,523✔
608
                         .pk = pSColumnNode->isPk};
3,277,523✔
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,277,523✔
613
    if (!tmp) {
3,274,286!
614
      return DEAL_RES_ERROR;
×
615
    }
616
  } else {
617
    SColumnNode* col = *(SColumnNode**)data;
676,358✔
618
    pSColumnNode->slotId = col->slotId;
676,358✔
619
  }
620

621
  return DEAL_RES_CONTINUE;
3,950,644✔
622
}
623

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

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

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

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

647
static void releaseColInfoData(void* pCol) {
54,807✔
648
  if (pCol) {
54,807!
649
    SColumnInfoData* col = (SColumnInfoData*)pCol;
54,811✔
650
    colDataDestroy(col);
54,811✔
651
    taosMemoryFree(col);
54,813!
652
  }
653
}
54,806✔
654

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

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

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

733
  ctx.index = 0;
51,948✔
734
  ctx.cInfoList = taosArrayInit(4, sizeof(SColumnInfo));
51,948✔
735
  if (ctx.cInfoList == NULL) {
51,968✔
736
    code = terrno;
17✔
737
    goto end;
×
738
  }
739

740
  SNode* pNode = NULL;
51,951✔
741
  FOREACH(pNode, group) {
106,728!
742
    nodesRewriteExprPostOrder(&pNode, getColumn, (void*)&ctx);
54,748✔
743
    if (TSDB_CODE_SUCCESS != ctx.code) {
54,777!
744
      code = ctx.code;
×
745
      goto end;
×
746
    }
747
    REPLACE_NODE(pNode);
54,777✔
748
  }
749

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

779
  for (int32_t i = 0; i < rows; ++i) {
285,279✔
780
    STableKeyInfo* pkeyInfo = taosArrayGet(pTableListInfo->pTableList, i);
233,298✔
781
    QUERY_CHECK_NULL(pkeyInfo, code, lino, end, terrno);
233,210!
782
    STUidTagInfo   info = {.uid = pkeyInfo->uid};
233,210✔
783
    void*          tmp = taosArrayPush(pUidTagList, &info);
233,374✔
784
    QUERY_CHECK_NULL(tmp, code, lino, end, terrno);
233,374!
785
  }
786

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

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

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

808
  groupData = taosArrayInit(2, POINTER_BYTES);
51,940✔
809
  QUERY_CHECK_NULL(groupData, code, lino, end, terrno);
51,940!
810

811
  FOREACH(pNode, group) {
106,710!
812
    SScalarParam output = {0};
54,739✔
813

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

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

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

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

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

853
  int32_t keyLen = 0;
51,971✔
854
  SNode*  node;
855
  FOREACH(node, group) {
106,730✔
856
    SExprNode* pExpr = (SExprNode*)node;
54,759✔
857
    keyLen += pExpr->resType.bytes;
54,759✔
858
  }
859

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

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

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

878
  for (int i = 0; i < rows; i++) {
285,313✔
879
    STableKeyInfo* info = taosArrayGet(pTableListInfo->pTableList, i);
233,285✔
880
    QUERY_CHECK_NULL(info, code, lino, end, terrno);
233,150✔
881

882
    char* isNull = (char*)keyBuf;
233,146✔
883
    char* pStart = (char*)keyBuf + sizeof(int8_t) * LIST_LENGTH(group);
233,146!
884
    for (int j = 0; j < taosArrayGetSize(groupData); j++) {
477,193✔
885
      SColumnInfoData* pValue = (SColumnInfoData*)taosArrayGetP(groupData, j);
244,081✔
886

887
      if (colDataIsNull_s(pValue, i)) {
488,126✔
888
        isNull[j] = 1;
486✔
889
      } else {
890
        isNull[j] = 0;
243,577✔
891
        char* data = colDataGetData(pValue, i);
243,577!
892
        if (pValue->info.type == TSDB_DATA_TYPE_JSON) {
243,577✔
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)) {
243,313!
905
          if (varDataTLen(data) > pValue->info.bytes) {
230,811✔
906
            code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
4✔
907
            goto end;
4✔
908
          }
909
          memcpy(pStart, data, varDataTLen(data));
230,807✔
910
          pStart += varDataTLen(data);
230,807✔
911
        } else {
912
          memcpy(pStart, data, pValue->info.bytes);
12,502✔
913
          pStart += pValue->info.bytes;
12,502✔
914
        }
915
      }
916
    }
917

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

931
  if (tsTagFilterCache) {
52,028!
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:
52,028✔
945
  taosMemoryFreeClear(keyBuf);
52,044!
946
  taosHashCleanup(ctx.colHash);
51,990✔
947
  taosArrayDestroy(ctx.cInfoList);
51,978✔
948
  blockDataDestroy(pResBlock);
51,975✔
949
  taosArrayDestroy(pBlockList);
51,980✔
950
  taosArrayDestroyEx(pUidTagList, freeItem);
51,977✔
951
  taosArrayDestroyP(groupData, releaseColInfoData);
51,987✔
952
  if (code != TSDB_CODE_SUCCESS) {
51,984✔
953
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
16!
954
  }
955
  return code;
51,975✔
956
}
957

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

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

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

976
  SArray* pTbList = taosArrayInit(len, POINTER_BYTES);
428,789✔
977
  QUERY_CHECK_NULL(pTbList, code, lino, _end, terrno);
428,792!
978

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

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

993
  size_t numOfTables = taosArrayGetSize(pTbList);
428,795✔
994

995
  // order the name
996
  taosArraySort(pTbList, nameComparFn);
428,790✔
997

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

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

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

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

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

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

1049
static FilterCondType checkTagCond(SNode* cond) {
1,310,405✔
1050
  if (nodeType(cond) == QUERY_NODE_OPERATOR) {
1,310,405✔
1051
    return FILTER_NO_LOGIC;
308,121✔
1052
  }
1053
  if (nodeType(cond) != QUERY_NODE_LOGIC_CONDITION || ((SLogicConditionNode*)cond)->condType != LOGIC_COND_TYPE_AND) {
1,002,284!
1054
    return FILTER_AND;
44,821✔
1055
  }
1056
  return FILTER_OTHER;
957,463✔
1057
}
1058

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

1063
  if (ntype == QUERY_NODE_OPERATOR) {
1,310,482✔
1064
    ret = optimizeTbnameInCondImpl(pVnode, list, cond, pAPI, suid);
308,202✔
1065
  }
1066

1067
  if (ntype != QUERY_NODE_LOGIC_CONDITION || ((SLogicConditionNode*)cond)->condType != LOGIC_COND_TYPE_AND) {
1,311,987✔
1068
    return ret;
354,185✔
1069
  }
1070

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

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

1080
  SListCell* cell = pList->pHead;
957,802✔
1081
  for (int i = 0; i < len; i++) {
3,239,804✔
1082
    if (cell == NULL) break;
2,539,634!
1083
    if (optimizeTbnameInCondImpl(pVnode, list, cell->pNode, pAPI, suid) == 0) {
2,539,634✔
1084
      hasTbnameCond = true;
258,008✔
1085
      break;
258,008✔
1086
    }
1087
    cell = cell->pNext;
2,282,002✔
1088
  }
1089

1090
  taosArraySort(list, filterTableInfoCompare);
958,178✔
1091
  taosArrayRemoveDuplicate(list, filterTableInfoCompare, NULL);
956,262✔
1092

1093
  if (hasTbnameCond) {
956,651✔
1094
    ret = pAPI->metaFn.getTableTagsByUid(pVnode, suid, list);
258,006✔
1095
  }
1096

1097
  return ret;
956,855✔
1098
}
1099

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

1107
  SOperatorNode* pNode = (SOperatorNode*)pTagCond;
2,710,154✔
1108
  if (pNode->opType != OP_TYPE_IN) {
2,710,154✔
1109
    return -1;
1,931,807✔
1110
  }
1111

1112
  if ((pNode->pLeft != NULL && nodeType(pNode->pLeft) == QUERY_NODE_COLUMN &&
778,347!
1113
       ((SColumnNode*)pNode->pLeft)->colType == COLUMN_TYPE_TBNAME) &&
781,365✔
1114
      (pNode->pRight != NULL && nodeType(pNode->pRight) == QUERY_NODE_NODE_LIST)) {
428,792!
1115
    SNodeListNode* pList = (SNodeListNode*)pNode->pRight;
428,791✔
1116

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

1122
    SArray*   pTbList = getTableNameList(pList);
428,791✔
1123
    int32_t   numOfTables = taosArrayGetSize(pTbList);
428,789✔
1124
    SHashObj* uHash = NULL;
428,790✔
1125

1126
    size_t numOfExisted = taosArrayGetSize(pExistedUidList);  // len > 0 means there already have uids
428,790✔
1127
    if (numOfExisted > 0) {
428,788!
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++) {
859,274✔
1149
      char* name = taosArrayGetP(pTbList, i);
430,504✔
1150

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

1176
    taosHashCleanup(uHash);
428,770✔
1177
    taosArrayDestroy(pTbList);
428,768✔
1178
    return 0;
428,770✔
1179
  }
1180

1181
  return -1;
349,556✔
1182
}
1183

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

1192
  for (int32_t i = 0; i < taosArrayGetSize(pColList); ++i) {
4,544,501✔
1193
    SColumnInfoData colInfo = {0};
3,206,330✔
1194
    void* tmp = taosArrayGet(pColList, i);
3,206,330✔
1195
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
3,204,771!
1196
    colInfo.info = *(SColumnInfo*)tmp;
3,204,771✔
1197
    code = blockDataAppendColInfo(pResBlock, &colInfo);
3,204,771✔
1198
    QUERY_CHECK_CODE(code, lino, _end);
3,208,506!
1199
  }
1200

1201
  code = blockDataEnsureCapacity(pResBlock, numOfTables);
1,332,689✔
1202
  if (code != TSDB_CODE_SUCCESS) {
1,336,737!
1203
    terrno = code;
×
1204
    blockDataDestroy(pResBlock);
×
1205
    return NULL;
×
1206
  }
1207

1208
  pResBlock->info.rows = numOfTables;
1,336,737✔
1209

1210
  int32_t numOfCols = taosArrayGetSize(pResBlock->pDataBlock);
1,336,737✔
1211

1212
  for (int32_t i = 0; i < numOfTables; i++) {
4,650,586✔
1213
    STUidTagInfo* p1 = taosArrayGet(pUidTagList, i);
3,304,331✔
1214
    QUERY_CHECK_NULL(p1, code, lino, _end, terrno);
3,301,177!
1215

1216
    for (int32_t j = 0; j < numOfCols; j++) {
10,746,109✔
1217
      SColumnInfoData* pColInfo = (SColumnInfoData*)taosArrayGet(pResBlock->pDataBlock, j);
7,430,699✔
1218
      QUERY_CHECK_NULL(pColInfo, code, lino, _end, terrno);
7,419,551!
1219

1220
      if (pColInfo->info.colId == -1) {  // tbname
7,419,818✔
1221
        char str[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
437,949✔
1222
        if (p1->name != NULL) {
437,949✔
1223
          STR_TO_VARSTR(str, p1->name);
428,755✔
1224
        } else {  // name is not retrieved during filter
1225
          code = pStorageAPI->metaFn.getTableNameByUid(pVnode, p1->uid, str);
9,194✔
1226
          QUERY_CHECK_CODE(code, lino, _end);
9,264!
1227
        }
1228

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

1242
          if (p == NULL || (pColInfo->info.type == TSDB_DATA_TYPE_JSON && ((STag*)p)->nTag == 0)) {
7,019,727✔
1243
            colDataSetNULL(pColInfo, i);
3,024✔
1244
          } else if (pColInfo->info.type == TSDB_DATA_TYPE_JSON) {
7,016,703✔
1245
            code = colDataSetVal(pColInfo, i, p, false);
4,860✔
1246
            QUERY_CHECK_CODE(code, lino, _end);
4,860!
1247
          } else if (IS_VAR_DATA_TYPE(pColInfo->info.type)) {
9,111,604!
1248
            char* tmp = taosMemoryMalloc(tagVal.nData + VARSTR_HEADER_SIZE + 1);
2,101,931✔
1249
            QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
2,099,803!
1250
            varDataSetLen(tmp, tagVal.nData);
2,099,803✔
1251
            memcpy(tmp + VARSTR_HEADER_SIZE, tagVal.pData, tagVal.nData);
2,099,803✔
1252
            code = colDataSetVal(pColInfo, i, tmp, false);
2,099,803✔
1253
#if TAG_FILTER_DEBUG
1254
            qDebug("tagfilter varch:%s", tmp + 2);
1255
#endif
1256
            taosMemoryFree(tmp);
2,097,974!
1257
            QUERY_CHECK_CODE(code, lino, _end);
2,099,761!
1258
          } else {
1259
            code = colDataSetVal(pColInfo, i, (const char*)&tagVal.i64, false);
4,909,912✔
1260
            QUERY_CHECK_CODE(code, lino, _end);
4,898,775!
1261
#if TAG_FILTER_DEBUG
1262
            if (pColInfo->info.type == TSDB_DATA_TYPE_INT) {
1263
              qDebug("tagfilter int:%d", *(int*)(&tagVal.i64));
1264
            } else if (pColInfo->info.type == TSDB_DATA_TYPE_DOUBLE) {
1265
              qDebug("tagfilter double:%f", *(double*)(&tagVal.i64));
1266
            }
1267
#endif
1268
          }
1269
        }
1270
      }
1271
    }
1272
  }
1273

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

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

1288
  STableKeyInfo info = {.uid = 0, .groupId = 0};
1,280,876✔
1289
  int32_t       numOfTables = taosArrayGetSize(pUidTagList);
1,280,876✔
1290
  for (int32_t i = 0; i < numOfTables; ++i) {
4,354,020✔
1291
    if (pResultList[i]) {
3,071,572✔
1292
      STUidTagInfo* tmpTag = (STUidTagInfo*)taosArrayGet(pUidTagList, i);
2,349,623✔
1293
      if (!tmpTag) {
2,348,049!
1294
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1295
        return terrno;
×
1296
      }
1297
      uint64_t uid = tmpTag->uid;
2,348,049✔
1298
      qDebug("tagfilter get uid:%" PRId64 ", res:%d", uid, pResultList[i]);
2,348,049✔
1299

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

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

1315
  return TSDB_CODE_SUCCESS;
1,282,448✔
1316
}
1317

1318
static int32_t copyExistedUids(SArray* pUidTagList, const SArray* pUidList) {
1,311,021✔
1319
  int32_t code = TSDB_CODE_SUCCESS;
1,311,021✔
1320
  int32_t numOfExisted = taosArrayGetSize(pUidList);
1,311,021✔
1321
  if (numOfExisted == 0) {
1,310,886✔
1322
    return code;
1,282,212✔
1323
  }
1324

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

1341
static int32_t doFilterByTagCond(STableListInfo* pListInfo, SArray* pUidList, SNode* pTagCond, void* pVnode,
4,481,658✔
1342
                                 SIdxFltStatus status, SStorageAPI* pAPI, bool addUid, bool* listAdded) {
1343
  *listAdded = false;
4,481,658✔
1344
  if (pTagCond == NULL) {
4,481,658✔
1345
    return TSDB_CODE_SUCCESS;
3,172,152✔
1346
  }
1347

1348
  terrno = TSDB_CODE_SUCCESS;
1,309,506✔
1349

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

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

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

1370
  nodesRewriteExprPostOrder(&pTagCond, getColumn, (void*)&ctx);
1,312,948✔
1371
  if (TSDB_CODE_SUCCESS != ctx.code) {
1,311,357!
1372
    terrno = code = ctx.code;
×
1373
    goto end;
×
1374
  }
1375

1376
  SDataType type = {.type = TSDB_DATA_TYPE_BOOL, .bytes = sizeof(bool)};
1,311,357✔
1377

1378
  //  int64_t stt = taosGetTimestampUs();
1379
  pUidTagList = taosArrayInit(10, sizeof(STUidTagInfo));
1,311,357✔
1380
  QUERY_CHECK_NULL(pUidTagList, code, lino, end, terrno);
1,311,514!
1381

1382
  code = copyExistedUids(pUidTagList, pUidList);
1,311,514✔
1383
  QUERY_CHECK_CODE(code, lino, end);
1,310,939!
1384

1385
  FilterCondType condType = checkTagCond(pTagCond);
1,310,939✔
1386

1387
  int32_t filter = optimizeTbnameInCond(pVnode, pListInfo->idInfo.suid, pUidTagList, pTagCond, pAPI);
1,311,150✔
1388
  if (filter == 0) {  // tbname in filter is activated, do nothing and return
1,310,151✔
1389
    taosArrayClear(pUidList);
428,772✔
1390

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

1395
    for (int32_t i = 0; i < numOfRows; ++i) {
857,527✔
1396
      STUidTagInfo* pInfo = taosArrayGet(pUidTagList, i);
428,756✔
1397
      QUERY_CHECK_NULL(pInfo, code, lino, end, terrno);
428,754!
1398
      void*         tmp = taosArrayPush(pUidList, &pInfo->uid);
428,754✔
1399
      QUERY_CHECK_NULL(tmp, code, lino, end, terrno);
428,755!
1400
    }
1401
    terrno = 0;
428,771✔
1402
  } else {
1403
    if ((condType == FILTER_NO_LOGIC || condType == FILTER_AND) && status != SFLT_NOT_INDEX) {
881,379✔
1404
      code = pAPI->metaFn.getTableTagsByUid(pVnode, pListInfo->idInfo.suid, pUidTagList);
28✔
1405
    } else {
1406
      code = pAPI->metaFn.getTableTags(pVnode, pListInfo->idInfo.suid, pUidTagList);
881,351✔
1407
    }
1408
    if (code != TSDB_CODE_SUCCESS) {
884,485!
1409
      qError("failed to get table tags from meta, reason:%s, suid:%" PRIu64, tstrerror(code), pListInfo->idInfo.suid);
×
1410
      terrno = code;
×
1411
      QUERY_CHECK_CODE(code, lino, end);
×
1412
    }
1413
  }
1414

1415
  int32_t numOfTables = taosArrayGetSize(pUidTagList);
1,313,254✔
1416
  if (numOfTables == 0) {
1,312,033✔
1417
    goto end;
30,617✔
1418
  }
1419

1420
  pResBlock = createTagValBlockForFilter(ctx.cInfoList, numOfTables, pUidTagList, pVnode, pAPI);
1,281,416✔
1421
  if (pResBlock == NULL) {
1,282,349!
1422
    code = terrno;
×
1423
    QUERY_CHECK_CODE(code, lino, end);
×
1424
  }
1425

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

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

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

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

1447
  code = doSetQualifiedUid(pListInfo, pUidList, pUidTagList, (bool*)output.columnData->pData, addUid);
1,281,981✔
1448
  if (code != TSDB_CODE_SUCCESS) {
1,282,221!
1449
    terrno = code;
×
1450
    QUERY_CHECK_CODE(code, lino, end);
×
1451
  }
1452
  *listAdded = true;
1,282,247✔
1453

1454
end:
1,312,872✔
1455
  if (code != TSDB_CODE_SUCCESS) {
1,312,872✔
1456
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
8!
1457
  }
1458
  taosHashCleanup(ctx.colHash);
1,312,872✔
1459
  taosArrayDestroy(ctx.cInfoList);
1,313,159✔
1460
  blockDataDestroy(pResBlock);
1,313,458✔
1461
  taosArrayDestroy(pBlockList);
1,313,455✔
1462
  taosArrayDestroyEx(pUidTagList, freeItem);
1,313,452✔
1463

1464
  colDataDestroy(output.columnData);
1,313,284✔
1465
  taosMemoryFreeClear(output.columnData);
1,313,567!
1466
  return code;
1,313,552✔
1467
}
1468

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

1476
  pListInfo->idInfo.suid = pScanNode->suid;
4,479,043✔
1477
  pListInfo->idInfo.tableType = pScanNode->tableType;
4,479,043✔
1478

1479
  SArray* pUidList = taosArrayInit(8, sizeof(uint64_t));
4,479,043✔
1480
  QUERY_CHECK_NULL(pUidList, code, lino, _error, terrno);
4,484,946✔
1481

1482
  SIdxFltStatus status = SFLT_NOT_INDEX;
4,482,003✔
1483
  if (pScanNode->tableType != TSDB_SUPER_TABLE) {
4,482,003✔
1484
    pListInfo->idInfo.uid = pScanNode->uid;
629,785✔
1485
    if (pStorageAPI->metaFn.isTableExisted(pVnode, pScanNode->uid)) {
629,785✔
1486
      void* tmp = taosArrayPush(pUidList, &pScanNode->uid);
628,091✔
1487
      QUERY_CHECK_NULL(tmp, code, lino, _error, terrno);
628,084!
1488
    }
1489
    code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, status, pStorageAPI, false, &listAdded);
629,787✔
1490
    QUERY_CHECK_CODE(code, lino, _end);
629,770!
1491
  } else {
1492
    T_MD5_CTX context = {0};
3,852,218✔
1493

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

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

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

1512
    if (!pTagCond) {  // no tag filter condition exists, let's fetch all tables of this super table
3,852,218✔
1513
      code = pStorageAPI->metaFn.getChildTableList(pVnode, pScanNode->suid, pUidList);
2,566,315✔
1514
      QUERY_CHECK_CODE(code, lino, _error);
2,573,164!
1515
    } else {
1516
      // failed to find the result in the cache, let try to calculate the results
1517
      if (pTagIndexCond) {
1,285,903✔
1518
        void* pIndex = pStorageAPI->metaFn.getInvertIndex(pVnode);
80,310✔
1519

1520
        SIndexMetaArg metaArg = {.metaEx = pVnode,
160,800✔
1521
                                 .idx = pStorageAPI->metaFn.storeGetIndexInfo(pVnode),
80,409✔
1522
                                 .ivtIdx = pIndex,
1523
                                 .suid = pScanNode->uid};
80,391✔
1524

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

1535
    code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, status, pStorageAPI, tsTagFilterCache, &listAdded);
3,859,036✔
1536
    QUERY_CHECK_CODE(code, lino, _end);
3,854,230✔
1537

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

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

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

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

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

1562
_end:
4,481,534✔
1563
  if (!listAdded) {
4,481,534✔
1564
    numOfTables = taosArrayGetSize(pUidList);
3,200,607✔
1565
    for (int i = 0; i < numOfTables; i++) {
12,466,651✔
1566
      void* tmp = taosArrayGet(pUidList, i);
9,261,880✔
1567
      QUERY_CHECK_NULL(tmp, code, lino, _error, terrno);
9,252,338!
1568
      STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
9,252,338✔
1569

1570
      void* p = taosArrayPush(pListInfo->pTableList, &info);
9,252,338✔
1571
      if (p == NULL) {
9,265,885!
1572
        taosArrayDestroy(pUidList);
×
1573
        return terrno;
×
1574
      }
1575

1576
      qTrace("tagfilter get uid:%" PRIu64 ", %s", info.uid, idstr);
9,265,885✔
1577
    }
1578
  }
1579

1580
_error:
4,485,698✔
1581
  taosArrayDestroy(pUidList);
4,485,698✔
1582
  if (code != TSDB_CODE_SUCCESS) {
4,487,573✔
1583
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
8!
1584
  }
1585
  return code;
4,487,697✔
1586
}
1587

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

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

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

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

1618
  SNode* node;
1619
  FOREACH(node, pGroups) {
28,644!
1620
    SExprNode* pExpr = (SExprNode*)node;
22,733✔
1621
    keyLen += pExpr->resType.bytes;
22,733✔
1622
  }
1623

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1747
  return pList;
20,653✔
1748
}
1749

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

1756
  pMatchInfo->matchType = type;
8,322,175✔
1757

1758
  SArray* pList = taosArrayInit(numOfCols, sizeof(SColMatchItem));
8,322,175✔
1759
  if (pList == NULL) {
8,321,731✔
1760
    code = terrno;
561✔
1761
    return code;
×
1762
  }
1763

1764
  for (int32_t i = 0; i < numOfCols; ++i) {
43,430,483✔
1765
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pNodeList, i);
35,092,996✔
1766
    QUERY_CHECK_NULL(pNode, code, lino, _end, terrno);
35,111,494!
1767
    if (nodeType(pNode->pExpr) == QUERY_NODE_COLUMN) {
35,118,282✔
1768
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
35,098,768✔
1769

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

1781
  // set the output flag for each column in SColMatchInfo, according to the
1782
  *numOfOutputCols = 0;
8,337,487✔
1783
  int32_t num = LIST_LENGTH(pOutputNodeList->pSlots);
8,337,487✔
1784
  for (int32_t i = 0; i < num; ++i) {
50,623,045✔
1785
    SSlotDescNode* pNode = (SSlotDescNode*)nodesListGetNode(pOutputNodeList->pSlots, i);
42,259,372✔
1786
    QUERY_CHECK_NULL(pNode, code, lino, _end, terrno);
42,461,780!
1787

1788
    // todo: add reserve flag check
1789
    // it is a column reserved for the arithmetic expression calculation
1790
    if (pNode->slotId >= numOfCols) {
42,485,488✔
1791
      (*numOfOutputCols) += 1;
7,371,634✔
1792
      continue;
7,371,634✔
1793
    }
1794

1795
    SColMatchItem* info = NULL;
35,113,854✔
1796
    for (int32_t j = 0; j < taosArrayGetSize(pList); ++j) {
251,322,614✔
1797
      info = taosArrayGet(pList, j);
250,370,377✔
1798
      QUERY_CHECK_NULL(info, code, lino, _end, terrno);
250,158,282!
1799
      if (info->dstSlotId == pNode->slotId) {
250,927,491✔
1800
        break;
34,718,731✔
1801
      }
1802
    }
1803

1804
    if (pNode->output) {
34,913,924✔
1805
      (*numOfOutputCols) += 1;
34,888,625✔
1806
    } else if (info != NULL) {
25,299!
1807
      // select distinct tbname from stb where tbname='abc';
1808
      info->needOutput = false;
25,299✔
1809
    }
1810
  }
1811

1812
  pMatchInfo->pList = pList;
8,363,673✔
1813

1814
_end:
8,363,673✔
1815
  if (code != TSDB_CODE_SUCCESS) {
8,363,673!
1816
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1817
  }
1818
  return code;
8,318,634✔
1819
}
1820

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

1831
  return s;
27,224,414✔
1832
}
1833

1834
static SColumn* createColumn(int32_t blockId, int32_t slotId, int32_t colId, SDataType* pType, EColumnType colType) {
25,993,963✔
1835
  SColumn* pCol = taosMemoryCalloc(1, sizeof(SColumn));
25,993,963!
1836
  if (pCol == NULL) {
25,981,288!
1837
    return NULL;
×
1838
  }
1839

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

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

1859
  pExp->pExpr->_function.num = 1;
27,233,636✔
1860
  pExp->pExpr->_function.functionId = -1;
27,233,636✔
1861

1862
  int32_t type = nodeType(pNode);
27,233,636✔
1863
  // it is a project query, or group by column
1864
  if (type == QUERY_NODE_COLUMN) {
27,233,636✔
1865
    pExp->pExpr->nodeType = QUERY_NODE_COLUMN;
17,398,035✔
1866
    SColumnNode* pColNode = (SColumnNode*)pNode;
17,398,035✔
1867

1868
    pExp->base.pParam = taosMemoryCalloc(1, sizeof(SFunctParam));
17,398,035✔
1869
    QUERY_CHECK_NULL(pExp->base.pParam, code, lino, _end, terrno);
17,396,759!
1870

1871
    pExp->base.numOfParams = 1;
17,396,759✔
1872

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

1877
    pExp->base.pParam[0].pCol =
34,793,304✔
1878
        createColumn(pColNode->dataBlockId, pColNode->slotId, pColNode->colId, pType, pColNode->colType);
17,406,820✔
1879
    QUERY_CHECK_NULL(pExp->base.pParam[0].pCol, code, lino, _end, terrno);
17,386,484!
1880

1881
    pExp->base.pParam[0].type = FUNC_PARAM_TYPE_COLUMN;
17,386,484✔
1882
  } else if (type == QUERY_NODE_VALUE) {
9,835,601✔
1883
    pExp->pExpr->nodeType = QUERY_NODE_VALUE;
191,474✔
1884
    SValueNode* pValNode = (SValueNode*)pNode;
191,474✔
1885

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

1889
    pExp->base.numOfParams = 1;
191,511✔
1890

1891
    SDataType* pType = &pValNode->node.resType;
191,511✔
1892
    pExp->base.resSchema =
1893
        createResSchema(pType->type, pType->bytes, slotId, pType->scale, pType->precision, pValNode->node.aliasName);
191,511✔
1894
    pExp->base.pParam[0].type = FUNC_PARAM_TYPE_VALUE;
191,497✔
1895
    code = nodesValueNodeToVariant(pValNode, &pExp->base.pParam[0].param);
191,497✔
1896
    QUERY_CHECK_CODE(code, lino, _end);
191,486!
1897
  } else if (type == QUERY_NODE_FUNCTION) {
9,644,127✔
1898
    pExp->pExpr->nodeType = QUERY_NODE_FUNCTION;
9,447,863✔
1899
    SFunctionNode* pFuncNode = (SFunctionNode*)pNode;
9,447,863✔
1900

1901
    SDataType* pType = &pFuncNode->node.resType;
9,447,863✔
1902
    pExp->base.resSchema =
1903
        createResSchema(pType->type, pType->bytes, slotId, pType->scale, pType->precision, pFuncNode->node.aliasName);
9,447,863✔
1904
    tExprNode* pExprNode = pExp->pExpr;
9,451,224✔
1905

1906
    pExprNode->_function.functionId = pFuncNode->funcId;
9,451,224✔
1907
    pExprNode->_function.pFunctNode = pFuncNode;
9,451,224✔
1908
    pExprNode->_function.functionType = pFuncNode->funcType;
9,451,224✔
1909

1910
    tstrncpy(pExprNode->_function.functionName, pFuncNode->functionName, tListLen(pExprNode->_function.functionName));
9,451,224✔
1911

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

1917
    if (!pFuncNode->pParameterList && (memcmp(pExprNode->_function.functionName, name, len) == 0) &&
9,451,224✔
1918
        pExprNode->_function.functionName[len] == 0) {
674,446✔
1919
      pFuncNode->pParameterList = NULL;
674,275✔
1920
      int32_t     code = nodesMakeList(&pFuncNode->pParameterList);
674,275✔
1921
      SValueNode* res = NULL;
674,567✔
1922
      if (TSDB_CODE_SUCCESS == code) {
674,567!
1923
        code = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&res);
674,572✔
1924
      }
1925
      QUERY_CHECK_CODE(code, lino, _end);
674,461!
1926
      res->node.resType = (SDataType){.bytes = sizeof(int64_t), .type = TSDB_DATA_TYPE_BIGINT};
674,461✔
1927
      code = nodesListAppend(pFuncNode->pParameterList, (SNode*)res);
674,461✔
1928
      if (code != TSDB_CODE_SUCCESS) {
674,601✔
1929
        nodesDestroyNode((SNode*)res);
114✔
1930
        res = NULL;
×
1931
      }
1932
      QUERY_CHECK_CODE(code, lino, _end);
674,487!
1933
    }
1934
#endif
1935

1936
    int32_t numOfParam = LIST_LENGTH(pFuncNode->pParameterList);
9,451,436✔
1937

1938
    pExp->base.pParam = taosMemoryCalloc(numOfParam, sizeof(SFunctParam));
9,451,436✔
1939
    QUERY_CHECK_NULL(pExp->base.pParam, code, lino, _end, terrno);
9,452,012!
1940
    pExp->base.numOfParams = numOfParam;
9,452,012✔
1941

1942
    for (int32_t j = 0; j < numOfParam && TSDB_CODE_SUCCESS == code; ++j) {
20,341,447!
1943
      SNode* p1 = nodesListGetNode(pFuncNode->pParameterList, j);
10,888,652✔
1944
      QUERY_CHECK_NULL(p1, code, lino, _end, terrno);
10,890,278!
1945
      if (p1->type == QUERY_NODE_COLUMN) {
10,891,210✔
1946
        SColumnNode* pcn = (SColumnNode*)p1;
8,595,539✔
1947

1948
        pExp->base.pParam[j].type = FUNC_PARAM_TYPE_COLUMN;
8,595,539✔
1949
        pExp->base.pParam[j].pCol =
17,190,213✔
1950
            createColumn(pcn->dataBlockId, pcn->slotId, pcn->colId, &pcn->node.resType, pcn->colType);
8,595,539✔
1951
        QUERY_CHECK_NULL(pExp->base.pParam[j].pCol, code, lino, _end, terrno);
8,594,674✔
1952
      } else if (p1->type == QUERY_NODE_VALUE) {
2,295,671✔
1953
        SValueNode* pvn = (SValueNode*)p1;
1,912,108✔
1954
        pExp->base.pParam[j].type = FUNC_PARAM_TYPE_VALUE;
1,912,108✔
1955
        code = nodesValueNodeToVariant(pvn, &pExp->base.pParam[j].param);
1,912,108✔
1956
        QUERY_CHECK_CODE(code, lino, _end);
1,911,782!
1957
      }
1958
    }
1959
  } else if (type == QUERY_NODE_OPERATOR) {
196,264✔
1960
    pExp->pExpr->nodeType = QUERY_NODE_OPERATOR;
192,454✔
1961
    SOperatorNode* pOpNode = (SOperatorNode*)pNode;
192,454✔
1962

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

1967
    SDataType* pType = &pOpNode->node.resType;
192,454✔
1968
    pExp->base.resSchema =
1969
        createResSchema(pType->type, pType->bytes, slotId, pType->scale, pType->precision, pOpNode->node.aliasName);
192,454✔
1970
    pExp->pExpr->_optrRoot.pRootNode = pNode;
192,391✔
1971
  } else if (type == QUERY_NODE_CASE_WHEN) {
3,810✔
1972
    pExp->pExpr->nodeType = QUERY_NODE_OPERATOR;
1,262✔
1973
    SCaseWhenNode* pCaseNode = (SCaseWhenNode*)pNode;
1,262✔
1974

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

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

1997
_end:
2,546✔
1998
  if (code != TSDB_CODE_SUCCESS) {
27,226,964!
1999
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
2000
  }
2001
  return code;
27,214,692✔
2002
}
2003

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

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

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

2026
  return pExprs;
1,590✔
2027
}
2028

2029
int32_t createExprInfo(SNodeList* pNodeList, SNodeList* pGroupKeys, SExprInfo** pExprInfo, int32_t* numOfExprs) {
10,777,039✔
2030
  QRY_PARAM_CHECK(pExprInfo);
10,777,039!
2031

2032
  int32_t code = 0;
10,777,039✔
2033
  int32_t numOfFuncs = LIST_LENGTH(pNodeList);
10,777,039✔
2034
  int32_t numOfGroupKeys = 0;
10,777,039✔
2035
  if (pGroupKeys != NULL) {
10,777,039✔
2036
    numOfGroupKeys = LIST_LENGTH(pGroupKeys);
348,539✔
2037
  }
2038

2039
  *numOfExprs = numOfFuncs + numOfGroupKeys;
10,777,039✔
2040
  if (*numOfExprs == 0) {
10,777,039✔
2041
    return code;
1,562,960✔
2042
  }
2043

2044
  SExprInfo* pExprs = taosMemoryCalloc(*numOfExprs, sizeof(SExprInfo));
9,214,079!
2045
  if (pExprs == NULL) {
9,217,498!
2046
    return terrno;
×
2047
  }
2048

2049
  for (int32_t i = 0; i < (*numOfExprs); ++i) {
36,023,261✔
2050
    STargetNode* pTargetNode = NULL;
26,803,552✔
2051
    if (i < numOfFuncs) {
26,803,552✔
2052
      pTargetNode = (STargetNode*)nodesListGetNode(pNodeList, i);
26,360,497✔
2053
    } else {
2054
      pTargetNode = (STargetNode*)nodesListGetNode(pGroupKeys, i - numOfFuncs);
443,055✔
2055
    }
2056
    if (!pTargetNode) {
26,826,842!
2057
      destroyExprInfo(pExprs, *numOfExprs);
×
2058
      taosMemoryFreeClear(pExprs);
×
2059
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
2060
      return terrno;
×
2061
    }
2062

2063
    SExprInfo* pExp = &pExprs[i];
26,826,842✔
2064
    code = createExprFromTargetNode(pExp, pTargetNode);
26,826,842✔
2065
    if (code != TSDB_CODE_SUCCESS) {
26,808,719✔
2066
      destroyExprInfo(pExprs, *numOfExprs);
2,956✔
2067
      taosMemoryFreeClear(pExprs);
×
2068
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
2069
      return code;
×
2070
    }
2071
  }
2072

2073
  *pExprInfo = pExprs;
9,219,709✔
2074
  return code;
9,219,709✔
2075
}
2076

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

2083
  SqlFunctionCtx*  p = NULL;
10,456,748✔
2084
  SqlFunctionCtx** pValCtx = taosMemoryCalloc(numOfOutput, POINTER_BYTES);
10,456,748!
2085
  if (pValCtx == NULL) {
10,459,759!
2086
    return terrno;
×
2087
  }
2088

2089
  SHashObj* pSelectFuncs = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
10,459,759✔
2090
  QUERY_CHECK_NULL(pSelectFuncs, code, lino, _end, terrno);
10,453,302!
2091

2092
  for (int32_t i = 0; i < numOfOutput; ++i) {
36,986,742✔
2093
    const char* pName = pCtx[i].pExpr->pExpr->_function.functionName;
26,626,435✔
2094
    if ((strcmp(pName, "_select_value") == 0) || (strcmp(pName, "_group_key") == 0) ||
26,626,435✔
2095
        (strcmp(pName, "_group_const_value") == 0)) {
25,277,849✔
2096
      pValCtx[num++] = &pCtx[i];
1,348,638✔
2097
    } else if (fmIsSelectFunc(pCtx[i].functionId)) {
25,277,797✔
2098
      void* data = taosHashGet(pSelectFuncs, pName, strlen(pName));
2,227,072✔
2099
      if (taosHashGetSize(pSelectFuncs) != 0 && data == NULL) {
2,222,102✔
2100
        p = NULL;
88,589✔
2101
        break;
88,589✔
2102
      } else {
2103
        int32_t tempRes = taosHashPut(pSelectFuncs, pName, strlen(pName), &num, sizeof(num));
2,134,289✔
2104
        if (tempRes != TSDB_CODE_SUCCESS && tempRes != TSDB_CODE_DUP_KEY) {
2,137,371!
2105
          code = tempRes;
×
2106
          QUERY_CHECK_CODE(code, lino, _end);
×
2107
        }
2108
        p = &pCtx[i];
2,137,371✔
2109
      }
2110
    }
2111
  }
2112
  taosHashCleanup(pSelectFuncs);
10,448,896✔
2113

2114
  if (p != NULL) {
10,457,229✔
2115
    p->subsidiaries.pCtx = pValCtx;
2,011,600✔
2116
    p->subsidiaries.num = num;
2,011,600✔
2117
  } else {
2118
    taosMemoryFreeClear(pValCtx);
8,445,629!
2119
  }
2120

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

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

2139
  *rowEntryInfoOffset = taosMemoryCalloc(numOfOutput, sizeof(int32_t));
10,449,851!
2140
  if (*rowEntryInfoOffset == 0) {
10,459,197!
2141
    taosMemoryFreeClear(pFuncCtx);
×
2142
    return NULL;
×
2143
  }
2144

2145
  for (int32_t i = 0; i < numOfOutput; ++i) {
37,462,365✔
2146
    SExprInfo* pExpr = &pExprInfo[i];
26,996,895✔
2147

2148
    SExprBasicInfo* pFunct = &pExpr->base;
26,996,895✔
2149
    SqlFunctionCtx* pCtx = &pFuncCtx[i];
26,996,895✔
2150

2151
    pCtx->functionId = -1;
26,996,895✔
2152
    pCtx->pExpr = pExpr;
26,996,895✔
2153

2154
    if (pExpr->pExpr->nodeType == QUERY_NODE_FUNCTION) {
26,996,895✔
2155
      SFuncExecEnv env = {0};
9,447,229✔
2156
      pCtx->functionId = pExpr->pExpr->_function.pFunctNode->funcId;
9,447,229✔
2157
      pCtx->isPseudoFunc = fmIsWindowPseudoColumnFunc(pCtx->functionId);
9,447,229✔
2158
      pCtx->isNotNullFunc = fmIsNotNullOutputFunc(pCtx->functionId);
9,442,680✔
2159

2160
      bool isUdaf = fmIsUserDefinedFunc(pCtx->functionId);
9,446,761✔
2161
      if (fmIsAggFunc(pCtx->functionId) || fmIsIndefiniteRowsFunc(pCtx->functionId)) {
15,839,319✔
2162
        if (!isUdaf) {
6,403,914✔
2163
          code = fmGetFuncExecFuncs(pCtx->functionId, &pCtx->fpSet);
6,403,882✔
2164
          QUERY_CHECK_CODE(code, lino, _end);
6,397,559!
2165
        } else {
2166
          char* udfName = pExpr->pExpr->_function.pFunctNode->functionName;
32✔
2167
          pCtx->udfName = taosStrdup(udfName);
32!
2168
          QUERY_CHECK_NULL(pCtx->udfName, code, lino, _end, terrno);
32!
2169

2170
          code = fmGetUdafExecFuncs(pCtx->functionId, &pCtx->fpSet);
32✔
2171
          QUERY_CHECK_CODE(code, lino, _end);
32!
2172
        }
2173
        bool tmp = pCtx->fpSet.getEnv(pExpr->pExpr->_function.pFunctNode, &env);
6,397,591✔
2174
        if (!tmp) {
6,396,108!
2175
          code = terrno;
×
2176
          QUERY_CHECK_CODE(code, lino, _end);
×
2177
        }
2178
      } else {
2179
        code = fmGetScalarFuncExecFuncs(pCtx->functionId, &pCtx->sfp);
3,036,175✔
2180
        if (code != TSDB_CODE_SUCCESS && isUdaf) {
3,036,988!
2181
          code = TSDB_CODE_SUCCESS;
262✔
2182
        }
2183
        QUERY_CHECK_CODE(code, lino, _end);
3,036,988!
2184

2185
        if (pCtx->sfp.getEnv != NULL) {
3,036,988✔
2186
          bool tmp = pCtx->sfp.getEnv(pExpr->pExpr->_function.pFunctNode, &env);
1,786,643✔
2187
          if (!tmp) {
1,784,903!
2188
            code = terrno;
×
2189
            QUERY_CHECK_CODE(code, lino, _end);
×
2190
          }
2191
        }
2192
      }
2193
      pCtx->resDataInfo.interBufSize = env.calcMemSize;
9,433,823✔
2194
    } else if (pExpr->pExpr->nodeType == QUERY_NODE_COLUMN || pExpr->pExpr->nodeType == QUERY_NODE_OPERATOR ||
17,549,666✔
2195
               pExpr->pExpr->nodeType == QUERY_NODE_VALUE) {
191,498!
2196
      // for simple column, the result buffer needs to hold at least one element.
2197
      pCtx->resDataInfo.interBufSize = pFunct->resSchema.bytes;
17,554,446✔
2198
    }
2199

2200
    pCtx->input.numOfInputCols = pFunct->numOfParams;
26,983,489✔
2201
    pCtx->input.pData = taosMemoryCalloc(pFunct->numOfParams, POINTER_BYTES);
26,983,489!
2202
    QUERY_CHECK_NULL(pCtx->input.pData, code, lino, _end, terrno);
27,000,934!
2203
    pCtx->input.pColumnDataAgg = taosMemoryCalloc(pFunct->numOfParams, POINTER_BYTES);
27,000,934!
2204
    QUERY_CHECK_NULL(pCtx->input.pColumnDataAgg, code, lino, _end, terrno);
27,003,168!
2205

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

2220
  for (int32_t i = 1; i < numOfOutput; ++i) {
27,900,679✔
2221
    (*rowEntryInfoOffset)[i] = (int32_t)((*rowEntryInfoOffset)[i - 1] + sizeof(SResultRowEntryInfo) +
17,435,209✔
2222
                                         pFuncCtx[i - 1].resDataInfo.interBufSize);
17,435,209✔
2223
  }
2224

2225
  code = setSelectValueColumnInfo(pFuncCtx, numOfOutput);
10,465,470✔
2226
  QUERY_CHECK_CODE(code, lino, _end);
10,451,876!
2227

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

2238
    terrno = code;
×
2239
    return NULL;
×
2240
  }
2241
  return pFuncCtx;
10,451,876✔
2242
}
2243

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

2250
  int32_t i = 0, j = 0;
1,032,561✔
2251
  while (i < numOfSrcCols && j < taosArrayGetSize(pColMatchInfo)) {
2,230,365✔
2252
    SColumnInfoData* p = taosArrayGet(pCols, i);
1,198,027✔
2253
    if (!p) {
1,197,600!
2254
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
2255
      return terrno;
×
2256
    }
2257
    SColMatchItem* pmInfo = taosArrayGet(pColMatchInfo, j);
1,197,600✔
2258
    if (!pmInfo) {
1,197,431!
2259
      return terrno;
×
2260
    }
2261

2262
    if (p->info.colId == pmInfo->colId) {
1,197,510✔
2263
      SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, pmInfo->dstSlotId);
1,176,600✔
2264
      if (!pDst) {
1,176,318!
2265
        return terrno;
×
2266
      }
2267
      code = colDataAssign(pDst, p, pBlock->info.rows, &pBlock->info);
1,176,318✔
2268
      if (code != TSDB_CODE_SUCCESS) {
1,176,894!
2269
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
2270
        return code;
×
2271
      }
2272
      i++;
1,176,894✔
2273
      j++;
1,176,894✔
2274
    } else if (p->info.colId < pmInfo->colId) {
20,910!
2275
      i++;
20,910✔
2276
    } else {
2277
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR));
×
2278
      return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
2279
    }
2280
  }
2281
  return code;
1,032,558✔
2282
}
2283

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

2296
  return interval;
2,582,347✔
2297
}
2298

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

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

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

2316
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
4,476,335!
2317
  if (!pCond->colList) {
4,484,289!
2318
    return terrno;
×
2319
  }
2320
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
4,484,289✔
2321
  if (pCond->pSlotList == NULL) {
4,480,601!
2322
    taosMemoryFreeClear(pCond->colList);
×
2323
    return terrno;
×
2324
  }
2325

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

2334
  // allowed read stt file optimization mode
2335
  pCond->notLoadData = (pTableScanNode->dataRequired == FUNC_DATA_REQUIRED_NOT_LOAD) &&
9,099,969✔
2336
                       (pTableScanNode->scan.node.pConditions == NULL) && (pTableScanNode->interval == 0);
4,480,601✔
2337

2338
  int32_t j = 0;
4,480,601✔
2339
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
27,164,155✔
2340
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pTableScanNode->scan.pScanCols, i);
22,675,500✔
2341
    if (!pNode) {
22,682,924!
2342
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
2343
      return terrno;
×
2344
    }
2345
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
22,683,554✔
2346
    if (pColNode->colType == COLUMN_TYPE_TAG) {
22,683,554!
2347
      continue;
×
2348
    }
2349

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

2355
    pCond->pSlotList[j] = pNode->slotId;
22,683,554✔
2356
    j += 1;
22,683,554✔
2357
  }
2358

2359
  pCond->numOfCols = j;
4,488,655✔
2360
  return TSDB_CODE_SUCCESS;
4,488,655✔
2361
}
2362

2363
void cleanupQueryTableDataCond(SQueryTableDataCond* pCond) {
6,392,318✔
2364
  taosMemoryFreeClear(pCond->colList);
6,392,318!
2365
  taosMemoryFreeClear(pCond->pSlotList);
6,392,769!
2366
}
6,392,709✔
2367

2368
int32_t convertFillType(int32_t mode) {
549,920✔
2369
  int32_t type = TSDB_FILL_NONE;
549,920✔
2370
  switch (mode) {
549,920!
2371
    case FILL_MODE_PREV:
15,717✔
2372
      type = TSDB_FILL_PREV;
15,717✔
2373
      break;
15,717✔
2374
    case FILL_MODE_NONE:
×
2375
      type = TSDB_FILL_NONE;
×
2376
      break;
×
2377
    case FILL_MODE_NULL:
16,204✔
2378
      type = TSDB_FILL_NULL;
16,204✔
2379
      break;
16,204✔
2380
    case FILL_MODE_NULL_F:
28,601✔
2381
      type = TSDB_FILL_NULL_F;
28,601✔
2382
      break;
28,601✔
2383
    case FILL_MODE_NEXT:
15,295✔
2384
      type = TSDB_FILL_NEXT;
15,295✔
2385
      break;
15,295✔
2386
    case FILL_MODE_VALUE:
152,101✔
2387
      type = TSDB_FILL_SET_VALUE;
152,101✔
2388
      break;
152,101✔
2389
    case FILL_MODE_VALUE_F:
297,635✔
2390
      type = TSDB_FILL_SET_VALUE_F;
297,635✔
2391
      break;
297,635✔
2392
    case FILL_MODE_LINEAR:
14,357✔
2393
      type = TSDB_FILL_LINEAR;
14,357✔
2394
      break;
14,357✔
2395
    case FILL_MODE_NEAR:
10,030✔
2396
      type = TSDB_FILL_NEAR;
10,030✔
2397
      break;
10,030✔
2398
    default:
×
2399
      type = TSDB_FILL_NONE;
×
2400
  }
2401

2402
  return type;
549,920✔
2403
}
2404

2405
void getInitialStartTimeWindow(SInterval* pInterval, TSKEY ts, STimeWindow* w, bool ascQuery) {
34,369,473✔
2406
  if (ascQuery) {
34,369,473✔
2407
    *w = getAlignQueryTimeWindow(pInterval, ts);
34,037,649✔
2408
  } else {
2409
    // the start position of the first time window in the endpoint that spreads beyond the queried last timestamp
2410
    *w = getAlignQueryTimeWindow(pInterval, ts);
331,824✔
2411

2412
    int64_t key = w->skey;
331,985✔
2413
    while (key < ts) {  // moving towards end
1,101,235✔
2414
      key = getNextTimeWindowStart(pInterval, key, TSDB_ORDER_ASC);
1,079,985✔
2415
      if (key > ts) {
1,079,949✔
2416
        break;
310,699✔
2417
      }
2418

2419
      w->skey = key;
769,250✔
2420
    }
2421
    w->ekey = taosTimeAdd(w->skey, pInterval->interval, pInterval->intervalUnit, pInterval->precision, NULL) - 1;
331,949✔
2422
  }
2423
}
34,365,172✔
2424

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

2428
  w.skey = taosTimeTruncate(ts, pInterval);
1,072,451✔
2429
  w.ekey = taosTimeGetIntervalEnd(w.skey, pInterval);
1,072,601✔
2430
  return w;
1,072,531✔
2431
}
2432

2433
STimeWindow getFirstQualifiedTimeWindow(int64_t ts, STimeWindow* pWindow, SInterval* pInterval, int32_t order) {
431,675✔
2434
  STimeWindow win = *pWindow;
431,675✔
2435
  STimeWindow save = win;
431,675✔
2436
  while (win.skey <= ts && win.ekey >= ts) {
1,840,308✔
2437
    save = win;
1,408,630✔
2438
    // get previous time window
2439
    getNextTimeWindow(pInterval, &win, order == TSDB_ORDER_ASC ? TSDB_ORDER_DESC : TSDB_ORDER_ASC);
1,408,630✔
2440
  }
2441

2442
  return save;
431,678✔
2443
}
2444

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

2455
  SResultRow* pRow = getResultRowByPos(pBuf, &pResultRowInfo->cur, false);
1,698,810✔
2456
  if (pRow) {
1,699,159✔
2457
    w = pRow->win;
1,699,157✔
2458
  }
2459

2460
  // in case of typical time window, we can calculate time window directly.
2461
  if (w.skey > ts || w.ekey < ts) {
1,699,159✔
2462
    w = doCalculateTimeWindow(ts, pInterval);
1,072,452✔
2463
  }
2464

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

2471
  return w;
1,699,233✔
2472
}
2473

2474
TSKEY getNextTimeWindowStart(const SInterval* pInterval, TSKEY start, int32_t order) {
257,047,241✔
2475
  int32_t factor = GET_FORWARD_DIRECTION_FACTOR(order);
257,047,241✔
2476
  TSKEY   nextStart = taosTimeAdd(start, -1 * pInterval->offset, pInterval->offsetUnit, pInterval->precision, NULL);
257,047,241✔
2477
  nextStart = taosTimeAdd(nextStart, factor * pInterval->sliding, pInterval->slidingUnit, pInterval->precision, NULL);
256,902,399✔
2478
  nextStart = taosTimeAdd(nextStart, pInterval->offset, pInterval->offsetUnit, pInterval->precision, NULL);
256,717,482✔
2479
  return nextStart;
256,099,881✔
2480
}
2481

2482
void getNextTimeWindow(const SInterval* pInterval, STimeWindow* tw, int32_t order) {
256,000,770✔
2483
  tw->skey = getNextTimeWindowStart(pInterval, tw->skey, order);
256,000,770✔
2484
  tw->ekey = taosTimeAdd(tw->skey, pInterval->interval, pInterval->intervalUnit, pInterval->precision, NULL) - 1;
255,084,795✔
2485
}
255,129,170✔
2486

2487
bool hasLimitOffsetInfo(SLimitInfo* pLimitInfo) {
11,090,157✔
2488
  return (pLimitInfo->limit.limit != -1 || pLimitInfo->limit.offset != -1 || pLimitInfo->slimit.limit != -1 ||
22,169,435!
2489
          pLimitInfo->slimit.offset != -1);
11,079,278✔
2490
}
2491

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

2496
void initLimitInfo(const SNode* pLimit, const SNode* pSLimit, SLimitInfo* pLimitInfo) {
12,824,381✔
2497
  SLimit limit = {.limit = getLimit(pLimit), .offset = getOffset(pLimit)};
12,824,381✔
2498
  SLimit slimit = {.limit = getLimit(pSLimit), .offset = getOffset(pSLimit)};
12,831,650✔
2499

2500
  pLimitInfo->limit = limit;
12,834,678✔
2501
  pLimitInfo->slimit = slimit;
12,834,678✔
2502
  pLimitInfo->remainOffset = limit.offset;
12,834,678✔
2503
  pLimitInfo->remainGroupOffset = slimit.offset;
12,834,678✔
2504
}
12,834,678✔
2505

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

2511
int32_t tableListGetSize(const STableListInfo* pTableList, int32_t* pRes) {
15,929,420✔
2512
  if (taosArrayGetSize(pTableList->pTableList) != taosHashGetSize(pTableList->map)) {
15,929,420!
2513
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR));
×
2514
    return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
2515
  }
2516
  (*pRes) = taosArrayGetSize(pTableList->pTableList);
15,934,912✔
2517
  return TSDB_CODE_SUCCESS;
15,930,981✔
2518
}
2519

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

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

2527
  return taosArrayGet(pTableList->pTableList, index);
6,907,265✔
2528
}
2529

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

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

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

2555
uint64_t tableListGetTableGroupId(const STableListInfo* pTableList, uint64_t tableUid) {
14,397,510✔
2556
  int32_t* slot = taosHashGet(pTableList->map, &tableUid, sizeof(tableUid));
14,397,510✔
2557
  if (slot == NULL) {
14,400,030!
2558
    return -1;
×
2559
  }
2560

2561
  STableKeyInfo* pKeyInfo = taosArrayGet(pTableList->pTableList, *slot);
14,400,030✔
2562

2563
  return pKeyInfo->groupId;
14,398,834✔
2564
}
2565

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

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

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

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

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

2600
  return code;
10,021✔
2601
}
2602

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

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

2617
  // here handle two special cases:
2618
  // 1. only one group exists, and 2. one table exists for each group.
2619
  if (totalGroups == 1) {
2,583,743✔
2620
    *size = numOfTables;
2,583,178✔
2621
    *pKeyInfo = (*size == 0) ? NULL : taosArrayGet(pTableList->pTableList, 0);
2,583,178✔
2622
    return TSDB_CODE_SUCCESS;
2,582,579✔
2623
  } else if (totalGroups == numOfTables) {
565!
2624
    *size = 1;
759✔
2625
    *pKeyInfo = taosArrayGet(pTableList->pTableList, ordinalGroupIndex);
759✔
2626
    return TSDB_CODE_SUCCESS;
759✔
2627
  }
2628

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

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

2640
int32_t tableListGetOutputGroups(const STableListInfo* pTableList) { return pTableList->numOfOuputGroups; }
7,720,558✔
2641

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

2644
STableListInfo* tableListCreate() {
4,495,399✔
2645
  STableListInfo* pListInfo = taosMemoryCalloc(1, sizeof(STableListInfo));
4,495,399!
2646
  if (pListInfo == NULL) {
4,505,112!
2647
    return NULL;
×
2648
  }
2649

2650
  pListInfo->remainGroups = NULL;
4,505,112✔
2651
  pListInfo->pTableList = taosArrayInit(4, sizeof(STableKeyInfo));
4,505,112✔
2652
  if (pListInfo->pTableList == NULL) {
4,503,297!
2653
    goto _error;
×
2654
  }
2655

2656
  pListInfo->map = taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
4,503,297✔
2657
  if (pListInfo->map == NULL) {
4,505,000!
2658
    goto _error;
×
2659
  }
2660

2661
  pListInfo->numOfOuputGroups = 1;
4,505,000✔
2662
  return pListInfo;
4,505,000✔
2663

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

2669
void tableListDestroy(STableListInfo* pTableListInfo) {
4,506,402✔
2670
  if (pTableListInfo == NULL) {
4,506,402✔
2671
    return;
2✔
2672
  }
2673

2674
  taosArrayDestroy(pTableListInfo->pTableList);
4,506,400✔
2675
  taosMemoryFreeClear(pTableListInfo->groupOffset);
4,507,089!
2676

2677
  taosHashCleanup(pTableListInfo->map);
4,507,089✔
2678
  taosHashCleanup(pTableListInfo->remainGroups);
4,506,818✔
2679
  pTableListInfo->pTableList = NULL;
4,506,951✔
2680
  pTableListInfo->map = NULL;
4,506,951✔
2681
  taosMemoryFree(pTableListInfo);
4,506,951!
2682
}
2683

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

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

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

2701
  if (pInfo1->groupId == pInfo2->groupId) {
86,198✔
2702
    return 0;
123✔
2703
  } else {
2704
    return pInfo1->groupId < pInfo2->groupId ? -1 : 1;
86,075✔
2705
  }
2706
}
2707

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

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

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

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

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

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

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

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

2763
  bool   groupByTbname = groupbyTbname(group);
4,250,385✔
2764
  size_t numOfTables = taosArrayGetSize(pTableListInfo->pTableList);
4,250,185✔
2765
  if (!numOfTables) {
4,248,617!
2766
    return code;
×
2767
  }
2768
  if (group == NULL || groupByTbname) {
4,248,617✔
2769
    if (tsCountAlwaysReturnValue && QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pScanNode) &&
4,196,698✔
2770
        ((STableScanPhysiNode*)pScanNode)->needCountEmptyTable) {
2,338,237✔
2771
      pTableListInfo->remainGroups =
172,227✔
2772
          taosHashInit(numOfTables, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
172,244✔
2773
      if (pTableListInfo->remainGroups == NULL) {
172,227!
2774
        return terrno;
×
2775
      }
2776

2777
      for (int i = 0; i < numOfTables; i++) {
625,389✔
2778
        STableKeyInfo* info = taosArrayGet(pTableListInfo->pTableList, i);
453,023✔
2779
        if (!info) {
452,964!
2780
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
2781
          return terrno;
×
2782
        }
2783
        info->groupId = groupByTbname ? info->uid : 0;
452,967✔
2784

2785
        int32_t tempRes = taosHashPut(pTableListInfo->remainGroups, &(info->groupId), sizeof(info->groupId),
452,967✔
2786
                                      &(info->uid), sizeof(info->uid));
452,967✔
2787
        if (tempRes != TSDB_CODE_SUCCESS && tempRes != TSDB_CODE_DUP_KEY) {
453,162!
2788
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(tempRes));
×
2789
          return tempRes;
×
2790
        }
2791
      }
2792
    } else {
2793
      for (int32_t i = 0; i < numOfTables; i++) {
14,945,418✔
2794
        STableKeyInfo* info = taosArrayGet(pTableListInfo->pTableList, i);
10,919,999✔
2795
        if (!info) {
10,920,766!
2796
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
2797
          return terrno;
×
2798
        }
2799
        info->groupId = groupByTbname ? info->uid : 0;
10,920,964✔
2800
      }
2801
    }
2802

2803
    pTableListInfo->oneTableForEachGroup = groupByTbname;
4,197,785✔
2804
    if (numOfTables == 1 && pTableListInfo->idInfo.tableType == TSDB_CHILD_TABLE) {
4,197,785✔
2805
      pTableListInfo->oneTableForEachGroup = true;
414,249✔
2806
    }
2807

2808
    if (groupSort && groupByTbname) {
4,197,785✔
2809
      taosArraySort(pTableListInfo->pTableList, orderbyGroupIdComparFn);
24,426✔
2810
      pTableListInfo->numOfOuputGroups = numOfTables;
24,439✔
2811
    } else if (groupByTbname && pScanNode->groupOrderScan) {
4,173,359✔
2812
      pTableListInfo->numOfOuputGroups = numOfTables;
63✔
2813
    } else {
2814
      pTableListInfo->numOfOuputGroups = 1;
4,173,296✔
2815
    }
2816
  } else {
2817
    bool initRemainGroups = false;
51,919✔
2818
    if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pScanNode)) {
51,919✔
2819
      STableScanPhysiNode* pTableScanNode = (STableScanPhysiNode*)pScanNode;
51,611✔
2820
      if (tsCountAlwaysReturnValue && pTableScanNode->needCountEmptyTable &&
51,611✔
2821
          !(groupSort || pScanNode->groupOrderScan)) {
13,088!
2822
        initRemainGroups = true;
13,044✔
2823
      }
2824
    }
2825

2826
    code = getColInfoResultForGroupby(pHandle->vnode, group, pTableListInfo, digest, pAPI, initRemainGroups);
51,919✔
2827
    if (code != TSDB_CODE_SUCCESS) {
51,947✔
2828
      return code;
16✔
2829
    }
2830

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

2833
    if (groupSort || pScanNode->groupOrderScan) {
51,931✔
2834
      code = sortTableGroup(pTableListInfo);
654✔
2835
    }
2836
  }
2837

2838
  // add all table entry in the hash map
2839
  size_t size = taosArrayGetSize(pTableListInfo->pTableList);
4,249,713✔
2840
  for (int32_t i = 0; i < size; ++i) {
15,879,266✔
2841
    STableKeyInfo* p = taosArrayGet(pTableListInfo->pTableList, i);
11,618,159✔
2842
    if (!p) {
11,601,950!
2843
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
2844
      return terrno;
×
2845
    }
2846
    int32_t        tempRes = taosHashPut(pTableListInfo->map, &p->uid, sizeof(uint64_t), &i, sizeof(int32_t));
11,601,950✔
2847
    if (tempRes != TSDB_CODE_SUCCESS && tempRes != TSDB_CODE_DUP_KEY) {
11,631,574!
2848
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(tempRes));
×
2849
      return tempRes;
×
2850
    }
2851
  }
2852

2853
  return code;
4,261,107✔
2854
}
2855

2856
int32_t createScanTableListInfo(SScanPhysiNode* pScanNode, SNodeList* pGroupTags, bool groupSort, SReadHandle* pHandle,
4,480,200✔
2857
                                STableListInfo* pTableListInfo, SNode* pTagCond, SNode* pTagIndexCond,
2858
                                SExecTaskInfo* pTaskInfo) {
2859
  int64_t     st = taosGetTimestampUs();
4,482,642✔
2860
  const char* idStr = GET_TASKID(pTaskInfo);
4,482,642✔
2861

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

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

2875
  int32_t numOfTables = taosArrayGetSize(pTableListInfo->pTableList);
4,485,810✔
2876

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

2882
  if (numOfTables == 0) {
4,484,512✔
2883
    qDebug("no table qualified for query, %s" PRIx64, idStr);
232,183✔
2884
    return TSDB_CODE_SUCCESS;
232,161✔
2885
  }
2886

2887
  code = buildGroupIdMapForAllTables(pTableListInfo, pHandle, pScanNode, pGroupTags, groupSort, digest,
4,252,329✔
2888
                                     &pTaskInfo->storageAPI);
2889
  if (code != TSDB_CODE_SUCCESS) {
4,256,230✔
2890
    return code;
16✔
2891
  }
2892

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

2896
  return TSDB_CODE_SUCCESS;
4,256,447✔
2897
}
2898

2899
char* getStreamOpName(uint16_t opType) {
1,498,856✔
2900
  switch (opType) {
1,498,856!
2901
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN:
616,690✔
2902
      return "stream scan";
616,690✔
2903
    case QUERY_NODE_PHYSICAL_PLAN_PROJECT:
31,679✔
2904
      return "project";
31,679✔
2905
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL:
759,090✔
2906
      return "interval single";
759,090✔
2907
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_INTERVAL:
10,308✔
2908
      return "interval final";
10,308✔
2909
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_INTERVAL:
16,970✔
2910
      return "interval semi";
16,970✔
2911
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_MID_INTERVAL:
143✔
2912
      return "interval mid";
143✔
2913
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FILL:
5,913✔
2914
      return "stream fill";
5,913✔
2915
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SESSION:
10,375✔
2916
      return "session single";
10,375✔
2917
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_SESSION:
1,563✔
2918
      return "session semi";
1,563✔
2919
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_SESSION:
1,299✔
2920
      return "session final";
1,299✔
2921
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_STATE:
7,450✔
2922
      return "state single";
7,450✔
2923
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_PARTITION:
14,265✔
2924
      return "stream partitionby";
14,265✔
2925
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_EVENT:
799✔
2926
      return "stream event";
799✔
2927
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_COUNT:
10,144✔
2928
      return "stream count";
10,144✔
2929
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERP_FUNC:
7,614✔
2930
      return "stream interp";
7,614✔
2931
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_CONTINUE_INTERVAL:
4,633✔
2932
      return "interval continue";
4,633✔
2933
  }
2934
  return "";
×
2935
}
2936

2937
void printDataBlock(SSDataBlock* pBlock, const char* flag, const char* taskIdStr) {
561,350✔
2938
  if (!pBlock) {
561,350!
2939
    qDebug("%s===stream===%s: Block is Null", taskIdStr, flag);
×
2940
    return;
×
2941
  } else if (pBlock->info.rows == 0) {
561,350✔
2942
    qDebug("%s===stream===%s: Block is Empty. block type %d", taskIdStr, flag, pBlock->info.type);
22,644✔
2943
    return;
22,644✔
2944
  }
2945
  if (qDebugFlag & DEBUG_DEBUG) {
538,706✔
2946
    char*   pBuf = NULL;
30,318✔
2947
    int32_t code = dumpBlockData(pBlock, flag, &pBuf, taskIdStr);
30,318✔
2948
    if (code == 0) {
30,319!
2949
      qDebug("%s", pBuf);
30,319!
2950
      taosMemoryFree(pBuf);
30,319!
2951
    }
2952
  }
2953
}
2954

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

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

2979
void updateTimeWindowInfo(SColumnInfoData* pColData, STimeWindow* pWin, int64_t delta) {
452,706,187✔
2980
  int64_t* ts = (int64_t*)pColData->pData;
452,706,187✔
2981

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

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

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

2999
    if (colDataIsNull(pColInfoData, pBlock->info.rows, rowIndex, pColAgg)) {
65,629,708!
3000
      if (isNull[i] != 1) return 1;
2,182,226✔
3001
    } else {
3002
      if (isNull[i] != 0) return 1;
30,632,628✔
3003
      const char* val = colDataGetData(pColInfoData, rowIndex);
30,618,406!
3004
      if (pCol->type == TSDB_DATA_TYPE_JSON) {
30,618,406!
3005
        int32_t len = getJsonValueLen(val);
×
3006
        if (memcmp(p, val, len) != 0) return 1;
×
3007
        p += len;
×
3008
      } else if (IS_VAR_DATA_TYPE(pCol->type)) {
30,618,406!
3009
        if (memcmp(p, val, varDataTLen(val)) != 0) return 1;
10,427,536✔
3010
        p += varDataTLen(val);
10,291,316✔
3011
      } else {
3012
        if (0 != memcmp(p, val, pCol->bytes)) return 1;
20,190,870✔
3013
        p += pCol->bytes;
19,859,267✔
3014
      }
3015
    }
3016
  }
3017
  if ((int32_t)(p - oldkeyBuf) != oldKeysLen) return 1;
20,890,424✔
3018
  return 0;
20,890,128✔
3019
}
3020

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

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

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

3034
    if (colDataIsNull(pColInfoData, pBlock->info.rows, rowIndex, pColAgg)) {
1,907,796!
3035
      isNull[i] = 1;
34,573✔
3036
    } else {
3037
      isNull[i] = 0;
919,325✔
3038
      const char* val = colDataGetData(pColInfoData, rowIndex);
919,325!
3039
      if (pCol->type == TSDB_DATA_TYPE_JSON) {
919,325!
3040
        int32_t len = getJsonValueLen(val);
×
3041
        memcpy(p, val, len);
×
3042
        p += len;
×
3043
      } else if (IS_VAR_DATA_TYPE(pCol->type)) {
919,325!
3044
        varDataCopy(p, val);
139,523✔
3045
        p += varDataTLen(val);
139,523✔
3046
      } else {
3047
        memcpy(p, val, pCol->bytes);
779,802✔
3048
        p += pCol->bytes;
779,802✔
3049
      }
3050
    }
3051
  }
3052
  return (int32_t)(p - keyBuf);
482,125✔
3053
}
3054

3055
uint64_t calcGroupId(char* pData, int32_t len) {
15,227,496✔
3056
  T_MD5_CTX context;
3057
  tMD5Init(&context);
15,227,496✔
3058
  tMD5Update(&context, (uint8_t*)pData, len);
15,247,270✔
3059
  tMD5Final(&context);
15,336,518✔
3060

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

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

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

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

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc