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

taosdata / TDengine / #3613

14 Feb 2025 09:14AM UTC coverage: 63.499% (-0.01%) from 63.513%
#3613

push

travis-ci

web-flow
Merge pull request #29781 from taosdata/doc/internal

docs: minor changes

141396 of 286269 branches covered (49.39%)

Branch coverage included in aggregate %.

220278 of 283307 relevant lines covered (77.75%)

19138445.45 hits per line

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

68.86
/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; }
27,867,126!
58
static int64_t getOffset(const SNode* pLimit) { return (NULL == pLimit || NULL == ((SLimitNode*)pLimit)->offset) ? -1 : ((SLimitNode*)pLimit)->offset->datum.i; }
27,874,402✔
59
static void    releaseColInfoData(void* pCol);
60

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

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

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

74
  if (entrySize > 0) {
72,172,756!
75
    memset(pResultRow->pEntryInfo, 0, entrySize);
72,178,893✔
76
  }
77
}
72,172,756✔
78

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

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

87
  for (int32_t i = 0; i < numOfOutput; ++i) {
38,186,735✔
88
    rowSize += pCtx[i].resDataInfo.interBufSize;
29,629,129✔
89
  }
90

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

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

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

135
  if (processedSize < inBufSize) {
2,793✔
136
    // stream stores extra data after result row
137
    size_t leftLen = inBufSize - processedSize;
998✔
138
    TAOS_MEMORY_REALLOC(*outBuf, *outBufSize + leftLen);
998!
139
    if (*outBuf == NULL) {
998!
140
      qError("failed to reallocate memory for output buffer, size:%zu", *outBufSize + leftLen);
×
141
      return terrno;
×
142
    }
143
    (void)memcpy(*outBuf + *outBufSize, inBuf, leftLen);
998✔
144
    inBuf += leftLen;
998✔
145
    processedSize += leftLen;
998✔
146
    *outBufSize += leftLen;
998✔
147
  }
148
  return TSDB_CODE_SUCCESS;
2,793✔
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,822,408✔
153
  if (pSup == NULL || inBuf == NULL || outBuf == NULL || outBufSize == NULL) {
1,822,408!
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,822,418✔
159
  int32_t        *offset = pSup->rowEntryInfoOffset;
1,822,418✔
160
  SResultRow     *pResultRow = (SResultRow*)inBuf;
1,822,418✔
161
  size_t          rowSize = getResultRowSize(pCtx, pSup->numOfExprs);
1,822,418✔
162

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

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

180
  char *pBuf = *outBuf;
1,822,295✔
181
  pResultRow->version = FUNCTION_RESULT_INFO_VERSION;
1,822,295✔
182
  (void)memcpy(pBuf, pResultRow, sizeof(SResultRow));
1,822,295✔
183
  pBuf += sizeof(SResultRow);
1,822,295✔
184
  for (int32_t i = 0; i < pSup->numOfExprs; ++i) {
11,193,950✔
185
    size_t len = sizeof(SResultRowEntryInfo) + pCtx[i].resDataInfo.interBufSize;
9,372,443✔
186
    *(int32_t *) pBuf = (int32_t)len;
9,372,443✔
187
    pBuf += sizeof(int32_t);
9,372,443✔
188
    (void)memcpy(pBuf, getResultEntryInfo(pResultRow, i, offset), len);
9,372,443✔
189
    pBuf += len;
9,371,655✔
190
  }
191

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

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

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

217
int32_t resultrowComparAsc(const void* p1, const void* p2) {
2,147,483,647✔
218
  SResKeyPos* pp1 = *(SResKeyPos**)p1;
2,147,483,647✔
219
  SResKeyPos* pp2 = *(SResKeyPos**)p2;
2,147,483,647✔
220

221
  if (pp1->groupId == pp2->groupId) {
2,147,483,647✔
222
    int64_t pts1 = *(int64_t*)pp1->key;
2,013,061,964✔
223
    int64_t pts2 = *(int64_t*)pp2->key;
2,013,061,964✔
224

225
    if (pts1 == pts2) {
2,013,061,964!
226
      return 0;
×
227
    } else {
228
      return pts1 < pts2 ? -1 : 1;
2,013,061,964✔
229
    }
230
  } else {
231
    return pp1->groupId < pp2->groupId ? -1 : 1;
188,489,951✔
232
  }
233
}
234

235
static int32_t resultrowComparDesc(const void* p1, const void* p2) { return resultrowComparAsc(p2, p1); }
91,937,863✔
236

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

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

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

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

259
  // todo move away and record this during create window
260
  while ((pData = tSimpleHashIterate(pHashmap, pData, &iter)) != NULL) {
242,273,590✔
261
    /*void* key = */ (void)tSimpleHashGetKey(pData, &keyLen);
262
    bufLen += keyLen + sizeof(SResultRowPosition);
238,652,042✔
263
  }
264

265
  pGroupResInfo->pBuf = taosMemoryMalloc(bufLen);
3,609,873!
266
  QUERY_CHECK_NULL(pGroupResInfo->pBuf, code, lino, _end, terrno);
3,621,678!
267

268
  iter = 0;
3,621,678✔
269
  while ((pData = tSimpleHashIterate(pHashmap, pData, &iter)) != NULL) {
242,011,468✔
270
    void* key = tSimpleHashGetKey(pData, &keyLen);
238,442,694✔
271

272
    SResKeyPos* p = (SResKeyPos*)(pGroupResInfo->pBuf + offset);
238,442,694✔
273

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

280
    offset += keyLen + sizeof(struct SResultRowPosition);
238,389,790✔
281
  }
282

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

289
  pGroupResInfo->index = 0;
3,621,984✔
290

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

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

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

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

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

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

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

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

327
  if (pNodeList != NULL) {
2,309,667✔
328
    numOfCols = LIST_LENGTH(pNodeList);
2,309,392!
329
  } else {
330
    numOfCols = 0;
275✔
331
  }
332

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

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

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

361
  return pList;
2,309,787✔
362
}
363

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

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

378
  for (int32_t i = 0; i < numOfCols; ++i) {
115,611,478✔
379
    SSlotDescNode*  pDescNode = (SSlotDescNode*)nodesListGetNode(pNode->pSlots, i);
94,591,046✔
380
    if (!pDescNode) {
94,560,174!
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);
94,560,174✔
389
    idata.info.scale = pDescNode->dataType.scale;
94,781,601✔
390
    idata.info.precision = pDescNode->dataType.precision;
94,781,601✔
391
    idata.info.noData = pDescNode->reserve;
94,781,601✔
392

393
    code = blockDataAppendColInfo(pBlock, &idata);
94,781,601✔
394
    if (code != TSDB_CODE_SUCCESS) {
94,606,004!
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;
21,020,432✔
404
}
405

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

409
  for (int32_t i = 0; i < taosArrayGetSize(pMatchInfo->pList); ++i) {
29,252,870✔
410
    SColMatchItem* pItem = taosArrayGet(pMatchInfo->pList, i);
24,505,888✔
411
    if (!pItem) {
24,517,887✔
412
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
2,818!
413
      return terrno;
2,818✔
414
    }
415

416
    if (pItem->isPk) {
24,515,069✔
417
      SColumnInfoData* pInfoData = taosArrayGet(pDataBlock->pDataBlock, pItem->dstSlotId);
141,096✔
418
      if (!pInfoData) {
141,097!
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,097✔
423
      pBlockInfo->pks[1].type = pInfoData->info.type;
141,097✔
424

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

446
  return TSDB_CODE_SUCCESS;
4,876,627✔
447
}
448

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

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

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

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

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

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

523
  return DEAL_RES_CONTINUE;
6,816✔
524
}
525

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

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

536
    return TSDB_CODE_SUCCESS;
×
537
  }
538

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

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

562
    return code;
×
563
  }
564

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

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

572
static EDealRes getColumn(SNode** pNode, void* pContext) {
77,737,182✔
573
  tagFilterAssist* pData = (tagFilterAssist*)pContext;
77,737,182✔
574
  SColumnNode*     pSColumnNode = NULL;
77,737,182✔
575
  if (QUERY_NODE_COLUMN == nodeType((*pNode))) {
77,737,182✔
576
    pSColumnNode = *(SColumnNode**)pNode;
3,788,955✔
577
  } else if (QUERY_NODE_FUNCTION == nodeType((*pNode))) {
73,948,227✔
578
    SFunctionNode* pFuncNode = *(SFunctionNode**)(pNode);
477,413✔
579
    if (pFuncNode->funcType == FUNCTION_TYPE_TBNAME) {
477,413✔
580
      pData->code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&pSColumnNode);
465,776✔
581
      if (NULL == pSColumnNode) {
465,775!
582
        return DEAL_RES_ERROR;
×
583
      }
584
      pSColumnNode->colId = -1;
465,775✔
585
      pSColumnNode->colType = COLUMN_TYPE_TBNAME;
465,775✔
586
      pSColumnNode->node.resType.type = TSDB_DATA_TYPE_VARCHAR;
465,775✔
587
      pSColumnNode->node.resType.bytes = TSDB_TABLE_FNAME_LEN - 1 + VARSTR_HEADER_SIZE;
465,775✔
588
      nodesDestroyNode(*pNode);
465,775✔
589
      *pNode = (SNode*)pSColumnNode;
465,774✔
590
    } else {
591
      return DEAL_RES_CONTINUE;
11,637✔
592
    }
593
  } else {
594
    return DEAL_RES_CONTINUE;
73,470,814✔
595
  }
596

597
  void* data = taosHashGet(pData->colHash, &pSColumnNode->colId, sizeof(pSColumnNode->colId));
4,254,729✔
598
  if (!data) {
4,252,225✔
599
    int32_t tempRes =
600
        taosHashPut(pData->colHash, &pSColumnNode->colId, sizeof(pSColumnNode->colId), pNode, sizeof((*pNode)));
3,409,722✔
601
    if (tempRes != TSDB_CODE_SUCCESS && tempRes != TSDB_CODE_DUP_KEY) {
3,413,330!
602
      return DEAL_RES_ERROR;
×
603
    }
604
    pSColumnNode->slotId = pData->index++;
3,413,330✔
605
    SColumnInfo cInfo = {.colId = pSColumnNode->colId,
3,413,330✔
606
                         .type = pSColumnNode->node.resType.type,
3,413,330✔
607
                         .bytes = pSColumnNode->node.resType.bytes,
3,413,330✔
608
                         .pk = pSColumnNode->isPk};
3,413,330✔
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,413,330✔
613
    if (!tmp) {
3,411,770!
614
      return DEAL_RES_ERROR;
×
615
    }
616
  } else {
617
    SColumnNode* col = *(SColumnNode**)data;
842,503✔
618
    pSColumnNode->slotId = col->slotId;
842,503✔
619
  }
620

621
  return DEAL_RES_CONTINUE;
4,254,273✔
622
}
623

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

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

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

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

647
static void releaseColInfoData(void* pCol) {
76,418✔
648
  if (pCol) {
76,418✔
649
    SColumnInfoData* col = (SColumnInfoData*)pCol;
76,416✔
650
    colDataDestroy(col);
76,416✔
651
    taosMemoryFree(col);
76,420!
652
  }
653
}
76,424✔
654

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

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

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

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

740
  SNode* pNode = NULL;
64,379✔
741
  FOREACH(pNode, group) {
140,768!
742
    nodesRewriteExprPostOrder(&pNode, getColumn, (void*)&ctx);
76,382✔
743
    if (TSDB_CODE_SUCCESS != ctx.code) {
76,389!
744
      code = ctx.code;
×
745
      goto end;
×
746
    }
747
    REPLACE_NODE(pNode);
76,389✔
748
  }
749

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

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

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

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

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

808
  groupData = taosArrayInit(2, POINTER_BYTES);
64,396✔
809
  QUERY_CHECK_NULL(groupData, code, lino, end, terrno);
64,399✔
810

811
  FOREACH(pNode, group) {
140,795!
812
    SScalarParam output = {0};
76,403✔
813

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

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

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

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

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

853
  int32_t keyLen = 0;
64,392✔
854
  SNode*  node;
855
  FOREACH(node, group) {
140,804✔
856
    SExprNode* pExpr = (SExprNode*)node;
76,412✔
857
    keyLen += pExpr->resType.bytes;
76,412✔
858
  }
859

860
  int32_t nullFlagSize = sizeof(int8_t) * LIST_LENGTH(group);
64,392!
861
  keyLen += nullFlagSize;
64,392✔
862

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

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

878
  for (int i = 0; i < rows; i++) {
319,858✔
879
    STableKeyInfo* info = taosArrayGet(pTableListInfo->pTableList, i);
255,432✔
880
    QUERY_CHECK_NULL(info, code, lino, end, terrno);
255,365!
881

882
    char* isNull = (char*)keyBuf;
255,377✔
883
    char* pStart = (char*)keyBuf + sizeof(int8_t) * LIST_LENGTH(group);
255,377!
884
    for (int j = 0; j < taosArrayGetSize(groupData); j++) {
540,028✔
885
      SColumnInfoData* pValue = (SColumnInfoData*)taosArrayGetP(groupData, j);
284,696✔
886

887
      if (colDataIsNull_s(pValue, i)) {
569,332✔
888
        isNull[j] = 1;
486✔
889
      } else {
890
        isNull[j] = 0;
284,180✔
891
        char* data = colDataGetData(pValue, i);
284,180!
892
        if (pValue->info.type == TSDB_DATA_TYPE_JSON) {
284,180✔
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)) {
283,916!
905
          if (varDataTLen(data) > pValue->info.bytes) {
253,440✔
906
            code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
3✔
907
            goto end;
3✔
908
          }
909
          memcpy(pStart, data, varDataTLen(data));
253,437✔
910
          pStart += varDataTLen(data);
253,437✔
911
        } else {
912
          memcpy(pStart, data, pValue->info.bytes);
30,476✔
913
          pStart += pValue->info.bytes;
30,476✔
914
        }
915
      }
916
    }
917

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

931
  if (tsTagFilterCache) {
64,426!
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:
64,426✔
945
  taosMemoryFreeClear(keyBuf);
64,441!
946
  taosHashCleanup(ctx.colHash);
64,434✔
947
  taosArrayDestroy(ctx.cInfoList);
64,395✔
948
  blockDataDestroy(pResBlock);
64,391✔
949
  taosArrayDestroy(pBlockList);
64,404✔
950
  taosArrayDestroyEx(pUidTagList, freeItem);
64,398✔
951
  taosArrayDestroyP(groupData, releaseColInfoData);
64,405✔
952
  if (code != TSDB_CODE_SUCCESS) {
64,402✔
953
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
15!
954
  }
955
  return code;
64,397✔
956
}
957

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

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

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

976
  SArray* pTbList = taosArrayInit(len, POINTER_BYTES);
460,219✔
977
  QUERY_CHECK_NULL(pTbList, code, lino, _end, terrno);
460,220✔
978

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

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

993
  size_t numOfTables = taosArrayGetSize(pTbList);
460,220✔
994

995
  // order the name
996
  taosArraySort(pTbList, nameComparFn);
460,217✔
997

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

1006
  for (int32_t i = 1; i < numOfTables; ++i) {
461,970✔
1007
    char** name = taosArrayGetLast(pNewList);
1,750✔
1008
    char** nameInOldList = taosArrayGet(pTbList, i);
1,750✔
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:
460,220✔
1019
  taosArrayDestroy(pTbList);
460,220✔
1020
  if (code != TSDB_CODE_SUCCESS) {
460,217!
1021
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1022
    return NULL;
×
1023
  }
1024
  return pNewList;
460,217✔
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,300✔
1039
  STUidTagInfo* p1 = (STUidTagInfo*)a;
3,300✔
1040
  STUidTagInfo* p2 = (STUidTagInfo*)b;
3,300✔
1041

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

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

1049
static FilterCondType checkTagCond(SNode* cond) {
1,421,186✔
1050
  if (nodeType(cond) == QUERY_NODE_OPERATOR) {
1,421,186✔
1051
    return FILTER_NO_LOGIC;
352,800✔
1052
  }
1053
  if (nodeType(cond) != QUERY_NODE_LOGIC_CONDITION || ((SLogicConditionNode*)cond)->condType != LOGIC_COND_TYPE_AND) {
1,068,386!
1054
    return FILTER_AND;
47,617✔
1055
  }
1056
  return FILTER_OTHER;
1,020,769✔
1057
}
1058

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

1063
  if (ntype == QUERY_NODE_OPERATOR) {
1,421,389✔
1064
    ret = optimizeTbnameInCondImpl(pVnode, list, cond, pAPI, suid);
352,911✔
1065
  }
1066

1067
  if (ntype != QUERY_NODE_LOGIC_CONDITION || ((SLogicConditionNode*)cond)->condType != LOGIC_COND_TYPE_AND) {
1,423,313✔
1068
    return ret;
402,370✔
1069
  }
1070

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

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

1080
  SListCell* cell = pList->pHead;
1,020,943✔
1081
  for (int i = 0; i < len; i++) {
3,451,489✔
1082
    if (cell == NULL) break;
2,702,621!
1083
    if (optimizeTbnameInCondImpl(pVnode, list, cell->pNode, pAPI, suid) == 0) {
2,702,621✔
1084
      hasTbnameCond = true;
272,061✔
1085
      break;
272,061✔
1086
    }
1087
    cell = cell->pNext;
2,430,546✔
1088
  }
1089

1090
  taosArraySort(list, filterTableInfoCompare);
1,020,929✔
1091
  taosArrayRemoveDuplicate(list, filterTableInfoCompare, NULL);
1,018,520✔
1092

1093
  if (hasTbnameCond) {
1,019,068✔
1094
    ret = pAPI->metaFn.getTableTagsByUid(pVnode, suid, list);
272,060✔
1095
  }
1096

1097
  return ret;
1,019,176✔
1098
}
1099

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

1107
  SOperatorNode* pNode = (SOperatorNode*)pTagCond;
2,909,557✔
1108
  if (pNode->opType != OP_TYPE_IN) {
2,909,557✔
1109
    return -1;
2,055,063✔
1110
  }
1111

1112
  if ((pNode->pLeft != NULL && nodeType(pNode->pLeft) == QUERY_NODE_COLUMN &&
854,494!
1113
       ((SColumnNode*)pNode->pLeft)->colType == COLUMN_TYPE_TBNAME) &&
857,254✔
1114
      (pNode->pRight != NULL && nodeType(pNode->pRight) == QUERY_NODE_NODE_LIST)) {
460,221!
1115
    SNodeListNode* pList = (SNodeListNode*)pNode->pRight;
460,220✔
1116

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

1122
    SArray*   pTbList = getTableNameList(pList);
460,220✔
1123
    int32_t   numOfTables = taosArrayGetSize(pTbList);
460,216✔
1124
    SHashObj* uHash = NULL;
460,214✔
1125

1126
    size_t numOfExisted = taosArrayGetSize(pExistedUidList);  // len > 0 means there already have uids
460,214✔
1127
    if (numOfExisted > 0) {
460,214!
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++) {
922,128✔
1149
      char* name = taosArrayGetP(pTbList, i);
461,925✔
1150

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

1176
    taosHashCleanup(uHash);
460,203✔
1177
    taosArrayDestroy(pTbList);
460,197✔
1178
    return 0;
460,198✔
1179
  }
1180

1181
  return -1;
394,274✔
1182
}
1183

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

1192
  for (int32_t i = 0; i < taosArrayGetSize(pColList); ++i) {
4,754,474✔
1193
    SColumnInfoData colInfo = {0};
3,304,182✔
1194
    void* tmp = taosArrayGet(pColList, i);
3,304,182✔
1195
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
3,301,902!
1196
    colInfo.info = *(SColumnInfo*)tmp;
3,301,902✔
1197
    code = blockDataAppendColInfo(pResBlock, &colInfo);
3,301,902✔
1198
    QUERY_CHECK_CODE(code, lino, _end);
3,306,790!
1199
  }
1200

1201
  code = blockDataEnsureCapacity(pResBlock, numOfTables);
1,444,991✔
1202
  if (code != TSDB_CODE_SUCCESS) {
1,448,195!
1203
    terrno = code;
×
1204
    blockDataDestroy(pResBlock);
×
1205
    return NULL;
×
1206
  }
1207

1208
  pResBlock->info.rows = numOfTables;
1,448,195✔
1209

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

1212
  for (int32_t i = 0; i < numOfTables; i++) {
4,946,982✔
1213
    STUidTagInfo* p1 = taosArrayGet(pUidTagList, i);
3,488,844✔
1214
    QUERY_CHECK_NULL(p1, code, lino, _end, terrno);
3,485,967✔
1215

1216
    for (int32_t j = 0; j < numOfCols; j++) {
11,022,150✔
1217
      SColumnInfoData* pColInfo = (SColumnInfoData*)taosArrayGet(pResBlock->pDataBlock, j);
7,522,535✔
1218
      QUERY_CHECK_NULL(pColInfo, code, lino, _end, terrno);
7,513,864!
1219

1220
      if (pColInfo->info.colId == -1) {  // tbname
7,515,285✔
1221
        char str[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
477,249✔
1222
        if (p1->name != NULL) {
477,249✔
1223
          STR_TO_VARSTR(str, p1->name);
460,185✔
1224
        } else {  // name is not retrieved during filter
1225
          code = pStorageAPI->metaFn.getTableNameByUid(pVnode, p1->uid, str);
17,064✔
1226
          QUERY_CHECK_CODE(code, lino, _end);
17,095!
1227
        }
1228

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

1242
          if (p == NULL || (pColInfo->info.type == TSDB_DATA_TYPE_JSON && ((STag*)p)->nTag == 0)) {
7,073,228✔
1243
            colDataSetNULL(pColInfo, i);
3,024✔
1244
          } else if (pColInfo->info.type == TSDB_DATA_TYPE_JSON) {
7,070,204✔
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,101,062!
1248
            char* tmp = taosMemoryMalloc(tagVal.nData + VARSTR_HEADER_SIZE + 1);
2,037,468✔
1249
            QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
2,035,281!
1250
            varDataSetLen(tmp, tagVal.nData);
2,035,281✔
1251
            memcpy(tmp + VARSTR_HEADER_SIZE, tagVal.pData, tagVal.nData);
2,035,281✔
1252
            code = colDataSetVal(pColInfo, i, tmp, false);
2,035,281✔
1253
#if TAG_FILTER_DEBUG
1254
            qDebug("tagfilter varch:%s", tmp + 2);
1255
#endif
1256
            taosMemoryFree(tmp);
2,033,181!
1257
            QUERY_CHECK_CODE(code, lino, _end);
2,035,718!
1258
          } else {
1259
            code = colDataSetVal(pColInfo, i, (const char*)&tagVal.i64, false);
5,027,876✔
1260
            QUERY_CHECK_CODE(code, lino, _end);
5,015,438!
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,458,138✔
1275
  if (code != TSDB_CODE_SUCCESS) {
1,458,138!
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,458,138✔
1282
}
1283

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

1288
  STableKeyInfo info = {.uid = 0, .groupId = 0};
1,366,260✔
1289
  int32_t       numOfTables = taosArrayGetSize(pUidTagList);
1,366,260✔
1290
  for (int32_t i = 0; i < numOfTables; ++i) {
4,582,081✔
1291
    if (pResultList[i]) {
3,213,368✔
1292
      STUidTagInfo* tmpTag = (STUidTagInfo*)taosArrayGet(pUidTagList, i);
2,493,232✔
1293
      if (!tmpTag) {
2,491,382!
1294
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1295
        return terrno;
×
1296
      }
1297
      uint64_t uid = tmpTag->uid;
2,491,382✔
1298
      qDebug("tagfilter get uid:%" PRId64 ", res:%d", uid, pResultList[i]);
2,491,382✔
1299

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

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

1315
  return TSDB_CODE_SUCCESS;
1,368,713✔
1316
}
1317

1318
static int32_t copyExistedUids(SArray* pUidTagList, const SArray* pUidList) {
1,421,611✔
1319
  int32_t code = TSDB_CODE_SUCCESS;
1,421,611✔
1320
  int32_t numOfExisted = taosArrayGetSize(pUidList);
1,421,611✔
1321
  if (numOfExisted == 0) {
1,421,833✔
1322
    return code;
1,392,138✔
1323
  }
1324

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

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

1348
  terrno = TSDB_CODE_SUCCESS;
1,419,420✔
1349

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

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

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

1370
  nodesRewriteExprPostOrder(&pTagCond, getColumn, (void*)&ctx);
1,424,029✔
1371
  if (TSDB_CODE_SUCCESS != ctx.code) {
1,423,368!
1372
    terrno = code = ctx.code;
×
1373
    goto end;
×
1374
  }
1375

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

1378
  //  int64_t stt = taosGetTimestampUs();
1379
  pUidTagList = taosArrayInit(10, sizeof(STUidTagInfo));
1,423,368✔
1380
  QUERY_CHECK_NULL(pUidTagList, code, lino, end, terrno);
1,422,222!
1381

1382
  code = copyExistedUids(pUidTagList, pUidList);
1,422,222✔
1383
  QUERY_CHECK_CODE(code, lino, end);
1,422,327!
1384

1385
  FilterCondType condType = checkTagCond(pTagCond);
1,422,327✔
1386

1387
  int32_t filter = optimizeTbnameInCond(pVnode, pListInfo->idInfo.suid, pUidTagList, pTagCond, pAPI);
1,422,953✔
1388
  if (filter == 0) {  // tbname in filter is activated, do nothing and return
1,420,781✔
1389
    taosArrayClear(pUidList);
460,197✔
1390

1391
    int32_t numOfRows = taosArrayGetSize(pUidTagList);
460,196✔
1392
    code = taosArrayEnsureCap(pUidList, numOfRows);
460,195✔
1393
    QUERY_CHECK_CODE(code, lino, end);
460,193!
1394

1395
    for (int32_t i = 0; i < numOfRows; ++i) {
920,370✔
1396
      STUidTagInfo* pInfo = taosArrayGet(pUidTagList, i);
460,177✔
1397
      QUERY_CHECK_NULL(pInfo, code, lino, end, terrno);
460,175!
1398
      void*         tmp = taosArrayPush(pUidList, &pInfo->uid);
460,175✔
1399
      QUERY_CHECK_NULL(tmp, code, lino, end, terrno);
460,177!
1400
    }
1401
    terrno = 0;
460,193✔
1402
  } else {
1403
    if ((condType == FILTER_NO_LOGIC || condType == FILTER_AND) && status != SFLT_NOT_INDEX) {
960,584✔
1404
      code = pAPI->metaFn.getTableTagsByUid(pVnode, pListInfo->idInfo.suid, pUidTagList);
28✔
1405
    } else {
1406
      code = pAPI->metaFn.getTableTags(pVnode, pListInfo->idInfo.suid, pUidTagList);
960,556✔
1407
    }
1408
    if (code != TSDB_CODE_SUCCESS) {
963,550!
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,423,743✔
1416
  if (numOfTables == 0) {
1,422,697✔
1417
    goto end;
55,288✔
1418
  }
1419

1420
  pResBlock = createTagValBlockForFilter(ctx.cInfoList, numOfTables, pUidTagList, pVnode, pAPI);
1,367,409✔
1421
  if (pResBlock == NULL) {
1,368,394!
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,368,394✔
1429
  QUERY_CHECK_NULL(pBlockList, code, lino, end, terrno);
1,368,825!
1430

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

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

1440
  code = scalarCalculate(pTagCond, pBlockList, &output);
1,368,807✔
1441
  if (code != TSDB_CODE_SUCCESS) {
1,368,273✔
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,368,265✔
1448
  if (code != TSDB_CODE_SUCCESS) {
1,368,306✔
1449
    terrno = code;
150✔
1450
    QUERY_CHECK_CODE(code, lino, end);
×
1451
  }
1452
  *listAdded = true;
1,368,156✔
1453

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

1464
  colDataDestroy(output.columnData);
1,424,179✔
1465
  taosMemoryFreeClear(output.columnData);
1,424,293!
1466
  return code;
1,424,262✔
1467
}
1468

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

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

1479
  SArray* pUidList = taosArrayInit(8, sizeof(uint64_t));
4,838,455✔
1480
  QUERY_CHECK_NULL(pUidList, code, lino, _error, terrno);
4,847,950✔
1481

1482
  SIdxFltStatus status = SFLT_NOT_INDEX;
4,845,649✔
1483
  if (pScanNode->tableType != TSDB_SUPER_TABLE) {
4,845,649✔
1484
    pListInfo->idInfo.uid = pScanNode->uid;
685,448✔
1485
    if (pStorageAPI->metaFn.isTableExisted(pVnode, pScanNode->uid)) {
685,448✔
1486
      void* tmp = taosArrayPush(pUidList, &pScanNode->uid);
683,674✔
1487
      QUERY_CHECK_NULL(tmp, code, lino, _error, terrno);
683,670!
1488
    }
1489
    code = doFilterByTagCond(pListInfo, pUidList, pTagCond, pVnode, status, pStorageAPI, false, &listAdded);
685,445✔
1490
    QUERY_CHECK_CODE(code, lino, _end);
685,432!
1491
  } else {
1492
    T_MD5_CTX context = {0};
4,160,201✔
1493

1494
    if (tsTagFilterCache) {
4,160,201!
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
4,160,201✔
1513
      code = pStorageAPI->metaFn.getChildTableList(pVnode, pScanNode->suid, pUidList);
2,761,585✔
1514
      QUERY_CHECK_CODE(code, lino, _error);
2,769,763!
1515
    } else {
1516
      // failed to find the result in the cache, let try to calculate the results
1517
      if (pTagIndexCond) {
1,398,616✔
1518
        void* pIndex = pStorageAPI->metaFn.getInvertIndex(pVnode);
55,575✔
1519

1520
        SIndexMetaArg metaArg = {.metaEx = pVnode,
111,248✔
1521
                                 .idx = pStorageAPI->metaFn.storeGetIndexInfo(pVnode),
55,645✔
1522
                                 .ivtIdx = pIndex,
1523
                                 .suid = pScanNode->uid};
55,603✔
1524

1525
        status = SFLT_NOT_INDEX;
55,603✔
1526
        code = doFilterTag(pTagIndexCond, &metaArg, pUidList, &status, &pStorageAPI->metaFilter);
55,603✔
1527
        if (code != 0 || status == SFLT_NOT_INDEX) {  // temporarily disable it for performance sake
55,600✔
1528
          qDebug("failed to get tableIds from index, suid:%" PRIu64, pScanNode->uid);
55,528✔
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);
4,168,366✔
1536
    QUERY_CHECK_CODE(code, lino, _end);
4,159,127✔
1537

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

1541
    if (tsTagFilterCache) {
4,157,395✔
1542
      size_t size = numOfTables * sizeof(uint64_t) + sizeof(int32_t);
471✔
1543
      char*  pPayload = taosMemoryMalloc(size);
471!
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,842,364✔
1563
  if (!listAdded) {
4,842,364✔
1564
    numOfTables = taosArrayGetSize(pUidList);
3,475,899✔
1565
    for (int i = 0; i < numOfTables; i++) {
13,327,129✔
1566
      void* tmp = taosArrayGet(pUidList, i);
9,846,051✔
1567
      QUERY_CHECK_NULL(tmp, code, lino, _error, terrno);
9,831,558!
1568
      STableKeyInfo info = {.uid = *(uint64_t*)tmp, .groupId = 0};
9,831,558✔
1569

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

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

1580
_error:
4,847,543✔
1581
  taosArrayDestroy(pUidList);
4,847,543✔
1582
  if (code != TSDB_CODE_SUCCESS) {
4,849,493✔
1583
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
8!
1584
  }
1585
  return code;
4,846,756✔
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);
80!
1599
  uint8_t         digest[17] = {0};
80✔
1600
  code =
1601
      getTableList(pVnode, &pNode, pSubplan ? pSubplan->pTagCond : NULL, pSubplan ? pSubplan->pTagIndexCond : NULL,
80✔
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) {
9,419✔
1616
  size_t keyLen = 0;
9,419✔
1617

1618
  SNode* node;
1619
  FOREACH(node, pGroups) {
54,369!
1620
    SExprNode* pExpr = (SExprNode*)node;
44,950✔
1621
    keyLen += pExpr->resType.bytes;
44,950✔
1622
  }
1623

1624
  keyLen += sizeof(int8_t) * LIST_LENGTH(pGroups);
9,419!
1625
  return keyLen;
9,419✔
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) {
27,694✔
1712
  if (!pNodeList) {
27,694!
1713
    return NULL;
×
1714
  }
1715

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

1722
  for (int32_t i = 0; i < numOfCols; ++i) {
103,276✔
1723
    SColumnNode* pColNode = (SColumnNode*)nodesListGetNode(pNodeList, i);
75,561✔
1724
    if (!pColNode) {
75,472!
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};
75,472✔
1732
    c.slotId = pColNode->slotId;
75,472✔
1733
    c.colId = pColNode->colId;
75,472✔
1734
    c.type = pColNode->node.resType.type;
75,472✔
1735
    c.bytes = pColNode->node.resType.bytes;
75,472✔
1736
    c.precision = pColNode->node.resType.precision;
75,472✔
1737
    c.scale = pColNode->node.resType.scale;
75,472✔
1738

1739
    void* tmp = taosArrayPush(pList, &c);
75,585✔
1740
    if (!tmp) {
75,585!
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;
27,715✔
1748
}
1749

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

1756
  pMatchInfo->matchType = type;
8,902,657✔
1757

1758
  SArray* pList = taosArrayInit(numOfCols, sizeof(SColMatchItem));
8,902,657✔
1759
  if (pList == NULL) {
8,903,876!
1760
    code = terrno;
×
1761
    return code;
×
1762
  }
1763

1764
  for (int32_t i = 0; i < numOfCols; ++i) {
46,885,121✔
1765
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pNodeList, i);
37,966,352✔
1766
    QUERY_CHECK_NULL(pNode, code, lino, _end, terrno);
37,930,164!
1767
    if (nodeType(pNode->pExpr) == QUERY_NODE_COLUMN) {
37,936,507✔
1768
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
37,841,928✔
1769

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

1781
  // set the output flag for each column in SColMatchInfo, according to the
1782
  *numOfOutputCols = 0;
8,918,769✔
1783
  int32_t num = LIST_LENGTH(pOutputNodeList->pSlots);
8,918,769✔
1784
  for (int32_t i = 0; i < num; ++i) {
54,513,206✔
1785
    SSlotDescNode* pNode = (SSlotDescNode*)nodesListGetNode(pOutputNodeList->pSlots, i);
45,563,921✔
1786
    QUERY_CHECK_NULL(pNode, code, lino, _end, terrno);
45,660,060!
1787

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

1795
    SColMatchItem* info = NULL;
37,930,099✔
1796
    for (int32_t j = 0; j < taosArrayGetSize(pList); ++j) {
892,295,980✔
1797
      info = taosArrayGet(pList, j);
892,040,764✔
1798
      QUERY_CHECK_NULL(info, code, lino, _end, terrno);
892,673,043✔
1799
      if (info->dstSlotId == pNode->slotId) {
891,918,286✔
1800
        break;
37,552,405✔
1801
      }
1802
    }
1803

1804
    if (pNode->output) {
37,825,559✔
1805
      (*numOfOutputCols) += 1;
37,758,940✔
1806
    } else if (info != NULL) {
66,619!
1807
      // select distinct tbname from stb where tbname='abc';
1808
      info->needOutput = false;
66,621✔
1809
    }
1810
  }
1811

1812
  pMatchInfo->pList = pList;
8,949,285✔
1813

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

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

1831
  return s;
29,269,867✔
1832
}
1833

1834
static SColumn* createColumn(int32_t blockId, int32_t slotId, int32_t colId, SDataType* pType, EColumnType colType) {
27,802,303✔
1835
  SColumn* pCol = taosMemoryCalloc(1, sizeof(SColumn));
27,802,303!
1836
  if (pCol == NULL) {
27,803,236!
1837
    return NULL;
×
1838
  }
1839

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

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

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

1862
  int32_t type = nodeType(pNode);
29,284,125✔
1863
  // it is a project query, or group by column
1864
  if (type == QUERY_NODE_COLUMN) {
29,284,125✔
1865
    pExp->pExpr->nodeType = QUERY_NODE_COLUMN;
18,416,141✔
1866
    SColumnNode* pColNode = (SColumnNode*)pNode;
18,416,141✔
1867

1868
    pExp->base.pParam = taosMemoryCalloc(1, sizeof(SFunctParam));
18,416,141!
1869
    QUERY_CHECK_NULL(pExp->base.pParam, code, lino, _end, terrno);
18,414,198!
1870

1871
    pExp->base.numOfParams = 1;
18,414,198✔
1872

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

1877
    pExp->base.pParam[0].pCol =
36,826,990✔
1878
        createColumn(pColNode->dataBlockId, pColNode->slotId, pColNode->colId, pType, pColNode->colType);
18,415,172✔
1879
    QUERY_CHECK_NULL(pExp->base.pParam[0].pCol, code, lino, _end, terrno);
18,411,818!
1880

1881
    pExp->base.pParam[0].type = FUNC_PARAM_TYPE_COLUMN;
18,411,818✔
1882
  } else if (type == QUERY_NODE_VALUE) {
10,867,984✔
1883
    pExp->pExpr->nodeType = QUERY_NODE_VALUE;
259,526✔
1884
    SValueNode* pValNode = (SValueNode*)pNode;
259,526✔
1885

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

1889
    pExp->base.numOfParams = 1;
259,624✔
1890

1891
    SDataType* pType = &pValNode->node.resType;
259,624✔
1892
    pExp->base.resSchema =
1893
        createResSchema(pType->type, pType->bytes, slotId, pType->scale, pType->precision, pValNode->node.aliasName);
259,624✔
1894
    pExp->base.pParam[0].type = FUNC_PARAM_TYPE_VALUE;
259,572✔
1895
    code = nodesValueNodeToVariant(pValNode, &pExp->base.pParam[0].param);
259,572✔
1896
    QUERY_CHECK_CODE(code, lino, _end);
259,528!
1897
  } else if (type == QUERY_NODE_FUNCTION) {
10,608,458✔
1898
    pExp->pExpr->nodeType = QUERY_NODE_FUNCTION;
10,387,312✔
1899
    SFunctionNode* pFuncNode = (SFunctionNode*)pNode;
10,387,312✔
1900

1901
    SDataType* pType = &pFuncNode->node.resType;
10,387,312✔
1902
    pExp->base.resSchema =
1903
        createResSchema(pType->type, pType->bytes, slotId, pType->scale, pType->precision, pFuncNode->node.aliasName);
10,387,312✔
1904
    tExprNode* pExprNode = pExp->pExpr;
10,390,990✔
1905

1906
    pExprNode->_function.functionId = pFuncNode->funcId;
10,390,990✔
1907
    pExprNode->_function.pFunctNode = pFuncNode;
10,390,990✔
1908
    pExprNode->_function.functionType = pFuncNode->funcType;
10,390,990✔
1909

1910
    tstrncpy(pExprNode->_function.functionName, pFuncNode->functionName, tListLen(pExprNode->_function.functionName));
10,390,990✔
1911

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

1917
    if (!pFuncNode->pParameterList && (memcmp(pExprNode->_function.functionName, name, len) == 0) &&
10,390,990✔
1918
        pExprNode->_function.functionName[len] == 0) {
735,293!
1919
      pFuncNode->pParameterList = NULL;
735,378✔
1920
      int32_t     code = nodesMakeList(&pFuncNode->pParameterList);
735,378✔
1921
      SValueNode* res = NULL;
735,410✔
1922
      if (TSDB_CODE_SUCCESS == code) {
735,410!
1923
        code = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&res);
735,426✔
1924
      }
1925
      QUERY_CHECK_CODE(code, lino, _end);
735,196!
1926
      res->node.resType = (SDataType){.bytes = sizeof(int64_t), .type = TSDB_DATA_TYPE_BIGINT};
735,196✔
1927
      code = nodesListAppend(pFuncNode->pParameterList, (SNode*)res);
735,196✔
1928
      if (code != TSDB_CODE_SUCCESS) {
735,472✔
1929
        nodesDestroyNode((SNode*)res);
9✔
1930
        res = NULL;
×
1931
      }
1932
      QUERY_CHECK_CODE(code, lino, _end);
735,463!
1933
    }
1934
#endif
1935

1936
    int32_t numOfParam = LIST_LENGTH(pFuncNode->pParameterList);
10,391,075✔
1937

1938
    pExp->base.pParam = taosMemoryCalloc(numOfParam, sizeof(SFunctParam));
10,391,075✔
1939
    QUERY_CHECK_NULL(pExp->base.pParam, code, lino, _end, terrno);
10,392,654!
1940
    pExp->base.numOfParams = numOfParam;
10,392,654✔
1941

1942
    for (int32_t j = 0; j < numOfParam && TSDB_CODE_SUCCESS == code; ++j) {
22,269,468!
1943
      SNode* p1 = nodesListGetNode(pFuncNode->pParameterList, j);
11,875,676✔
1944
      QUERY_CHECK_NULL(p1, code, lino, _end, terrno);
11,875,966!
1945
      if (p1->type == QUERY_NODE_COLUMN) {
11,877,191✔
1946
        SColumnNode* pcn = (SColumnNode*)p1;
9,393,160✔
1947

1948
        pExp->base.pParam[j].type = FUNC_PARAM_TYPE_COLUMN;
9,393,160✔
1949
        pExp->base.pParam[j].pCol =
18,787,141✔
1950
            createColumn(pcn->dataBlockId, pcn->slotId, pcn->colId, &pcn->node.resType, pcn->colType);
9,393,160✔
1951
        QUERY_CHECK_NULL(pExp->base.pParam[j].pCol, code, lino, _end, terrno);
9,393,981✔
1952
      } else if (p1->type == QUERY_NODE_VALUE) {
2,484,031✔
1953
        SValueNode* pvn = (SValueNode*)p1;
2,066,489✔
1954
        pExp->base.pParam[j].type = FUNC_PARAM_TYPE_VALUE;
2,066,489✔
1955
        code = nodesValueNodeToVariant(pvn, &pExp->base.pParam[j].param);
2,066,489✔
1956
        QUERY_CHECK_CODE(code, lino, _end);
2,065,941!
1957
      }
1958
    }
1959
  } else if (type == QUERY_NODE_OPERATOR) {
221,146✔
1960
    pExp->pExpr->nodeType = QUERY_NODE_OPERATOR;
212,060✔
1961
    SOperatorNode* pOpNode = (SOperatorNode*)pNode;
212,060✔
1962

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

1967
    SDataType* pType = &pOpNode->node.resType;
212,120✔
1968
    pExp->base.resSchema =
1969
        createResSchema(pType->type, pType->bytes, slotId, pType->scale, pType->precision, pOpNode->node.aliasName);
212,120✔
1970
    pExp->pExpr->_optrRoot.pRootNode = pNode;
212,013✔
1971
  } else if (type == QUERY_NODE_CASE_WHEN) {
9,086✔
1972
    pExp->pExpr->nodeType = QUERY_NODE_OPERATOR;
5,682✔
1973
    SCaseWhenNode* pCaseNode = (SCaseWhenNode*)pNode;
5,682✔
1974

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

1979
    SDataType* pType = &pCaseNode->node.resType;
5,687✔
1980
    pExp->base.resSchema =
1981
        createResSchema(pType->type, pType->bytes, slotId, pType->scale, pType->precision, pCaseNode->node.aliasName);
5,687✔
1982
    pExp->pExpr->_optrRoot.pRootNode = pNode;
5,681✔
1983
  } else if (type == QUERY_NODE_LOGIC_CONDITION) {
3,404✔
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;
3,402✔
1994
    QUERY_CHECK_CODE(code, lino, _end);
3,402!
1995
  }
1996

1997
_end:
3,402✔
1998
  if (code != TSDB_CODE_SUCCESS) {
29,286,236!
1999
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
2000
  }
2001
  return code;
29,269,681✔
2002
}
2003

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

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

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

2026
  return pExprs;
2,670✔
2027
}
2028

2029
int32_t createExprInfo(SNodeList* pNodeList, SNodeList* pGroupKeys, SExprInfo** pExprInfo, int32_t* numOfExprs) {
11,568,441✔
2030
  QRY_PARAM_CHECK(pExprInfo);
11,568,441!
2031

2032
  int32_t code = 0;
11,568,441✔
2033
  int32_t numOfFuncs = LIST_LENGTH(pNodeList);
11,568,441✔
2034
  int32_t numOfGroupKeys = 0;
11,568,441✔
2035
  if (pGroupKeys != NULL) {
11,568,441✔
2036
    numOfGroupKeys = LIST_LENGTH(pGroupKeys);
361,352!
2037
  }
2038

2039
  *numOfExprs = numOfFuncs + numOfGroupKeys;
11,568,441✔
2040
  if (*numOfExprs == 0) {
11,568,441✔
2041
    return code;
1,614,936✔
2042
  }
2043

2044
  SExprInfo* pExprs = taosMemoryCalloc(*numOfExprs, sizeof(SExprInfo));
9,953,505!
2045
  if (pExprs == NULL) {
9,958,274!
2046
    return terrno;
×
2047
  }
2048

2049
  for (int32_t i = 0; i < (*numOfExprs); ++i) {
38,802,337✔
2050
    STargetNode* pTargetNode = NULL;
28,840,340✔
2051
    if (i < numOfFuncs) {
28,840,340✔
2052
      pTargetNode = (STargetNode*)nodesListGetNode(pNodeList, i);
28,363,083✔
2053
    } else {
2054
      pTargetNode = (STargetNode*)nodesListGetNode(pGroupKeys, i - numOfFuncs);
477,257✔
2055
    }
2056
    if (!pTargetNode) {
28,813,504!
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];
28,813,504✔
2064
    code = createExprFromTargetNode(pExp, pTargetNode);
28,813,504✔
2065
    if (code != TSDB_CODE_SUCCESS) {
28,848,432✔
2066
      destroyExprInfo(pExprs, *numOfExprs);
4,369✔
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,961,997✔
2074
  return code;
9,961,997✔
2075
}
2076

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

2083
  SqlFunctionCtx*  p = NULL;
11,228,837✔
2084
  SqlFunctionCtx** pValCtx = taosMemoryCalloc(numOfOutput, POINTER_BYTES);
11,228,837!
2085
  if (pValCtx == NULL) {
11,231,844!
2086
    return terrno;
×
2087
  }
2088

2089
  SHashObj* pSelectFuncs = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
11,231,844✔
2090
  QUERY_CHECK_NULL(pSelectFuncs, code, lino, _end, terrno);
11,227,210!
2091

2092
  for (int32_t i = 0; i < numOfOutput; ++i) {
39,706,556✔
2093
    const char* pName = pCtx[i].pExpr->pExpr->_function.functionName;
28,580,377✔
2094
    if ((strcmp(pName, "_select_value") == 0) || (strcmp(pName, "_group_key") == 0) ||
28,580,377✔
2095
        (strcmp(pName, "_group_const_value") == 0)) {
27,192,664!
2096
      pValCtx[num++] = &pCtx[i];
1,384,197✔
2097
    } else if (fmIsSelectFunc(pCtx[i].functionId)) {
27,196,180✔
2098
      void* data = taosHashGet(pSelectFuncs, pName, strlen(pName));
2,387,753✔
2099
      if (taosHashGetSize(pSelectFuncs) != 0 && data == NULL) {
2,384,698✔
2100
        p = NULL;
96,910✔
2101
        break;
96,910✔
2102
      } else {
2103
        int32_t tempRes = taosHashPut(pSelectFuncs, pName, strlen(pName), &num, sizeof(num));
2,287,541✔
2104
        if (tempRes != TSDB_CODE_SUCCESS && tempRes != TSDB_CODE_DUP_KEY) {
2,290,073!
2105
          code = tempRes;
×
2106
          QUERY_CHECK_CODE(code, lino, _end);
×
2107
        }
2108
        p = &pCtx[i];
2,290,073✔
2109
      }
2110
    }
2111
  }
2112
  taosHashCleanup(pSelectFuncs);
11,223,089✔
2113

2114
  if (p != NULL) {
11,229,581✔
2115
    p->subsidiaries.pCtx = pValCtx;
2,046,124✔
2116
    p->subsidiaries.num = num;
2,046,124✔
2117
  } else {
2118
    taosMemoryFreeClear(pValCtx);
9,183,457!
2119
  }
2120

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

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

2139
  *rowEntryInfoOffset = taosMemoryCalloc(numOfOutput, sizeof(int32_t));
11,221,775!
2140
  if (*rowEntryInfoOffset == 0) {
11,232,553!
2141
    taosMemoryFreeClear(pFuncCtx);
×
2142
    return NULL;
×
2143
  }
2144

2145
  for (int32_t i = 0; i < numOfOutput; ++i) {
40,261,784✔
2146
    SExprInfo* pExpr = &pExprInfo[i];
29,022,739✔
2147

2148
    SExprBasicInfo* pFunct = &pExpr->base;
29,022,739✔
2149
    SqlFunctionCtx* pCtx = &pFuncCtx[i];
29,022,739✔
2150

2151
    pCtx->functionId = -1;
29,022,739✔
2152
    pCtx->pExpr = pExpr;
29,022,739✔
2153

2154
    if (pExpr->pExpr->nodeType == QUERY_NODE_FUNCTION) {
29,022,739✔
2155
      SFuncExecEnv env = {0};
10,387,884✔
2156
      pCtx->functionId = pExpr->pExpr->_function.pFunctNode->funcId;
10,387,884✔
2157
      pCtx->isPseudoFunc = fmIsWindowPseudoColumnFunc(pCtx->functionId);
10,387,884✔
2158
      pCtx->isNotNullFunc = fmIsNotNullOutputFunc(pCtx->functionId);
10,381,599✔
2159

2160
      bool isUdaf = fmIsUserDefinedFunc(pCtx->functionId);
10,385,830✔
2161
      if (fmIsAggFunc(pCtx->functionId) || fmIsIndefiniteRowsFunc(pCtx->functionId)) {
17,361,013✔
2162
        if (!isUdaf) {
6,978,440✔
2163
          code = fmGetFuncExecFuncs(pCtx->functionId, &pCtx->fpSet);
6,978,408✔
2164
          QUERY_CHECK_CODE(code, lino, _end);
6,976,131!
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,976,163✔
2174
        if (!tmp) {
6,974,420!
2175
          code = terrno;
×
2176
          QUERY_CHECK_CODE(code, lino, _end);
×
2177
        }
2178
      } else {
2179
        code = fmGetScalarFuncExecFuncs(pCtx->functionId, &pCtx->sfp);
3,400,280✔
2180
        if (code != TSDB_CODE_SUCCESS && isUdaf) {
3,394,495!
2181
          code = TSDB_CODE_SUCCESS;
262✔
2182
        }
2183
        QUERY_CHECK_CODE(code, lino, _end);
3,394,495!
2184

2185
        if (pCtx->sfp.getEnv != NULL) {
3,394,495✔
2186
          bool tmp = pCtx->sfp.getEnv(pExpr->pExpr->_function.pFunctNode, &env);
1,864,759✔
2187
          if (!tmp) {
1,861,198!
2188
            code = terrno;
×
2189
            QUERY_CHECK_CODE(code, lino, _end);
×
2190
          }
2191
        }
2192
      }
2193
      pCtx->resDataInfo.interBufSize = env.calcMemSize;
10,368,356✔
2194
    } else if (pExpr->pExpr->nodeType == QUERY_NODE_COLUMN || pExpr->pExpr->nodeType == QUERY_NODE_OPERATOR ||
18,634,855✔
2195
               pExpr->pExpr->nodeType == QUERY_NODE_VALUE) {
259,598!
2196
      // for simple column, the result buffer needs to hold at least one element.
2197
      pCtx->resDataInfo.interBufSize = pFunct->resSchema.bytes;
18,637,101✔
2198
    }
2199

2200
    pCtx->input.numOfInputCols = pFunct->numOfParams;
29,003,211✔
2201
    pCtx->input.pData = taosMemoryCalloc(pFunct->numOfParams, POINTER_BYTES);
29,003,211!
2202
    QUERY_CHECK_NULL(pCtx->input.pData, code, lino, _end, terrno);
29,022,385!
2203
    pCtx->input.pColumnDataAgg = taosMemoryCalloc(pFunct->numOfParams, POINTER_BYTES);
29,022,385!
2204
    QUERY_CHECK_NULL(pCtx->input.pColumnDataAgg, code, lino, _end, terrno);
29,029,231!
2205

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

2220
  for (int32_t i = 1; i < numOfOutput; ++i) {
29,942,224✔
2221
    (*rowEntryInfoOffset)[i] = (int32_t)((*rowEntryInfoOffset)[i - 1] + sizeof(SResultRowEntryInfo) +
18,703,179✔
2222
                                         pFuncCtx[i - 1].resDataInfo.interBufSize);
18,703,179✔
2223
  }
2224

2225
  code = setSelectValueColumnInfo(pFuncCtx, numOfOutput);
11,239,045✔
2226
  QUERY_CHECK_CODE(code, lino, _end);
11,223,331!
2227

2228
_end:
11,223,331✔
2229
  if (code != TSDB_CODE_SUCCESS) {
11,223,331!
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;
11,223,331✔
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,037,626✔
2247
  int32_t code = TSDB_CODE_SUCCESS;
1,037,626✔
2248
  size_t  numOfSrcCols = taosArrayGetSize(pCols);
1,037,626✔
2249

2250
  int32_t i = 0, j = 0;
1,037,677✔
2251
  while (i < numOfSrcCols && j < taosArrayGetSize(pColMatchInfo)) {
2,245,898✔
2252
    SColumnInfoData* p = taosArrayGet(pCols, i);
1,208,629✔
2253
    if (!p) {
1,208,164!
2254
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
2255
      return terrno;
×
2256
    }
2257
    SColMatchItem* pmInfo = taosArrayGet(pColMatchInfo, j);
1,208,164✔
2258
    if (!pmInfo) {
1,207,932!
2259
      return terrno;
×
2260
    }
2261

2262
    if (p->info.colId == pmInfo->colId) {
1,208,141✔
2263
      SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, pmInfo->dstSlotId);
1,180,746✔
2264
      if (!pDst) {
1,180,484!
2265
        return terrno;
×
2266
      }
2267
      code = colDataAssign(pDst, p, pBlock->info.rows, &pBlock->info);
1,180,484✔
2268
      if (code != TSDB_CODE_SUCCESS) {
1,180,779!
2269
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
2270
        return code;
×
2271
      }
2272
      i++;
1,180,827✔
2273
      j++;
1,180,827✔
2274
    } else if (p->info.colId < pmInfo->colId) {
27,395✔
2275
      i++;
27,394✔
2276
    } else {
2277
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR));
1!
2278
      return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
2279
    }
2280
  }
2281
  return code;
1,037,522✔
2282
}
2283

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

2296
  return interval;
2,793,766✔
2297
}
2298

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

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

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

2316
  pCond->colList = taosMemoryCalloc(pCond->numOfCols, sizeof(SColumnInfo));
4,875,591!
2317
  if (!pCond->colList) {
4,885,716!
2318
    return terrno;
×
2319
  }
2320
  pCond->pSlotList = taosMemoryMalloc(sizeof(int32_t) * pCond->numOfCols);
4,885,716!
2321
  if (pCond->pSlotList == NULL) {
4,880,733!
2322
    taosMemoryFreeClear(pCond->colList);
×
2323
    return terrno;
×
2324
  }
2325

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

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

2338
  int32_t j = 0;
4,880,733✔
2339
  for (int32_t i = 0; i < pCond->numOfCols; ++i) {
29,432,947✔
2340
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pTableScanNode->scan.pScanCols, i);
24,549,970✔
2341
    if (!pNode) {
24,552,253✔
2342
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
39!
2343
      return terrno;
39✔
2344
    }
2345
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
24,552,214✔
2346
    if (pColNode->colType == COLUMN_TYPE_TAG) {
24,552,214!
2347
      continue;
×
2348
    }
2349

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

2355
    pCond->pSlotList[j] = pNode->slotId;
24,552,214✔
2356
    j += 1;
24,552,214✔
2357
  }
2358

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

2363
void cleanupQueryTableDataCond(SQueryTableDataCond* pCond) {
6,981,412✔
2364
  taosMemoryFreeClear(pCond->colList);
6,981,412!
2365
  taosMemoryFreeClear(pCond->pSlotList);
6,982,073!
2366
}
6,982,007✔
2367

2368
int32_t convertFillType(int32_t mode) {
566,974✔
2369
  int32_t type = TSDB_FILL_NONE;
566,974✔
2370
  switch (mode) {
566,974!
2371
    case FILL_MODE_PREV:
17,677✔
2372
      type = TSDB_FILL_PREV;
17,677✔
2373
      break;
17,677✔
2374
    case FILL_MODE_NONE:
×
2375
      type = TSDB_FILL_NONE;
×
2376
      break;
×
2377
    case FILL_MODE_NULL:
30,009✔
2378
      type = TSDB_FILL_NULL;
30,009✔
2379
      break;
30,009✔
2380
    case FILL_MODE_NULL_F:
26,478✔
2381
      type = TSDB_FILL_NULL_F;
26,478✔
2382
      break;
26,478✔
2383
    case FILL_MODE_NEXT:
16,824✔
2384
      type = TSDB_FILL_NEXT;
16,824✔
2385
      break;
16,824✔
2386
    case FILL_MODE_VALUE:
156,146✔
2387
      type = TSDB_FILL_SET_VALUE;
156,146✔
2388
      break;
156,146✔
2389
    case FILL_MODE_VALUE_F:
294,015✔
2390
      type = TSDB_FILL_SET_VALUE_F;
294,015✔
2391
      break;
294,015✔
2392
    case FILL_MODE_LINEAR:
15,800✔
2393
      type = TSDB_FILL_LINEAR;
15,800✔
2394
      break;
15,800✔
2395
    case FILL_MODE_NEAR:
10,029✔
2396
      type = TSDB_FILL_NEAR;
10,029✔
2397
      break;
10,029✔
2398
    default:
×
2399
      type = TSDB_FILL_NONE;
×
2400
  }
2401

2402
  return type;
566,974✔
2403
}
2404

2405
void getInitialStartTimeWindow(SInterval* pInterval, TSKEY ts, STimeWindow* w, bool ascQuery) {
30,148,018✔
2406
  if (ascQuery) {
30,148,018✔
2407
    *w = getAlignQueryTimeWindow(pInterval, ts);
29,842,563✔
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);
305,455✔
2411

2412
    int64_t key = w->skey;
305,463✔
2413
    while (key < ts) {  // moving towards end
935,477✔
2414
      key = getNextTimeWindowStart(pInterval, key, TSDB_ORDER_ASC);
913,414✔
2415
      if (key > ts) {
913,420✔
2416
        break;
283,406✔
2417
      }
2418

2419
      w->skey = key;
630,014✔
2420
    }
2421
    w->ekey = taosTimeAdd(w->skey, pInterval->interval, pInterval->intervalUnit, pInterval->precision, NULL) - 1;
305,469✔
2422
  }
2423
}
30,147,122✔
2424

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

2428
  w.skey = taosTimeTruncate(ts, pInterval);
1,181,035✔
2429
  w.ekey = taosTimeGetIntervalEnd(w.skey, pInterval);
1,181,168✔
2430
  return w;
1,181,118✔
2431
}
2432

2433
STimeWindow getFirstQualifiedTimeWindow(int64_t ts, STimeWindow* pWindow, SInterval* pInterval, int32_t order) {
508,689✔
2434
  STimeWindow win = *pWindow;
508,689✔
2435
  STimeWindow save = win;
508,689✔
2436
  while (win.skey <= ts && win.ekey >= ts) {
2,260,774✔
2437
    save = win;
1,752,087✔
2438
    // get previous time window
2439
    getNextTimeWindow(pInterval, &win, order == TSDB_ORDER_ASC ? TSDB_ORDER_DESC : TSDB_ORDER_ASC);
1,752,087✔
2440
  }
2441

2442
  return save;
508,687✔
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,
31,988,334✔
2448
                                int32_t order) {
2449
  STimeWindow w = {0};
31,988,334✔
2450
  if (pResultRowInfo->cur.pageId == -1) {  // the first window, from the previous stored value
31,988,334✔
2451
    getInitialStartTimeWindow(pInterval, ts, &w, (order == TSDB_ORDER_ASC));
30,148,035✔
2452
    return w;
30,147,040✔
2453
  }
2454

2455
  SResultRow* pRow = getResultRowByPos(pBuf, &pResultRowInfo->cur, false);
1,840,299✔
2456
  if (pRow) {
1,840,216!
2457
    w = pRow->win;
1,840,227✔
2458
  }
2459

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

2465
  if (pInterval->interval != pInterval->sliding) {
1,840,304✔
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);
508,689✔
2469
  }
2470

2471
  return w;
1,840,309✔
2472
}
2473

2474
TSKEY getNextTimeWindowStart(const SInterval* pInterval, TSKEY start, int32_t order) {
266,675,375✔
2475
  int32_t factor = GET_FORWARD_DIRECTION_FACTOR(order);
266,675,375✔
2476
  TSKEY   nextStart = taosTimeAdd(start, -1 * pInterval->offset, pInterval->offsetUnit, pInterval->precision, NULL);
266,675,375✔
2477
  nextStart = taosTimeAdd(nextStart, factor * pInterval->sliding, pInterval->slidingUnit, pInterval->precision, NULL);
266,497,756✔
2478
  nextStart = taosTimeAdd(nextStart, pInterval->offset, pInterval->offsetUnit, pInterval->precision, NULL);
266,343,103✔
2479
  return nextStart;
265,852,946✔
2480
}
2481

2482
void getNextTimeWindow(const SInterval* pInterval, STimeWindow* tw, int32_t order) {
265,791,796✔
2483
  tw->skey = getNextTimeWindowStart(pInterval, tw->skey, order);
265,791,796✔
2484
  tw->ekey = taosTimeAdd(tw->skey, pInterval->interval, pInterval->intervalUnit, pInterval->precision, NULL) - 1;
264,934,444✔
2485
}
264,800,200✔
2486

2487
bool hasLimitOffsetInfo(SLimitInfo* pLimitInfo) {
13,134,219✔
2488
  return (pLimitInfo->limit.limit != -1 || pLimitInfo->limit.offset != -1 || pLimitInfo->slimit.limit != -1 ||
26,245,670!
2489
          pLimitInfo->slimit.offset != -1);
13,111,451!
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) {
13,933,837✔
2497
  SLimit limit = {.limit = getLimit(pLimit), .offset = getOffset(pLimit)};
13,933,837✔
2498
  SLimit slimit = {.limit = getLimit(pSLimit), .offset = getOffset(pSLimit)};
13,938,769✔
2499

2500
  pLimitInfo->limit = limit;
13,938,854✔
2501
  pLimitInfo->slimit = slimit;
13,938,854✔
2502
  pLimitInfo->remainOffset = limit.offset;
13,938,854✔
2503
  pLimitInfo->remainGroupOffset = slimit.offset;
13,938,854✔
2504
}
13,938,854✔
2505

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

2511
int32_t tableListGetSize(const STableListInfo* pTableList, int32_t* pRes) {
17,809,947✔
2512
  if (taosArrayGetSize(pTableList->pTableList) != taosHashGetSize(pTableList->map)) {
17,809,947!
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);
17,823,794✔
2517
  return TSDB_CODE_SUCCESS;
17,820,690✔
2518
}
2519

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

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

2527
  return taosArrayGet(pTableList->pTableList, index);
7,483,138✔
2528
}
2529

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

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

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

2555
uint64_t tableListGetTableGroupId(const STableListInfo* pTableList, uint64_t tableUid) {
16,740,808✔
2556
  int32_t* slot = taosHashGet(pTableList->map, &tableUid, sizeof(tableUid));
16,740,808✔
2557
  if (slot == NULL) {
16,743,592!
2558
    return -1;
×
2559
  }
2560

2561
  STableKeyInfo* pKeyInfo = taosArrayGet(pTableList->pTableList, *slot);
16,743,592✔
2562

2563
  return pKeyInfo->groupId;
16,741,968✔
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) {
10,117✔
2568
  int32_t code = TSDB_CODE_SUCCESS;
10,117✔
2569
  int32_t lino = 0;
10,117✔
2570
  if (pTableList->map == NULL) {
10,117!
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};
10,117✔
2576
  void*         p = taosHashGet(pTableList->map, &uid, sizeof(uid));
10,117✔
2577
  if (p != NULL) {
10,150✔
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,147✔
2583
  QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
10,132!
2584

2585
  int32_t slot = (int32_t)taosArrayGetSize(pTableList->pTableList) - 1;
10,132✔
2586
  code = taosHashPut(pTableList->map, &uid, sizeof(uid), &slot, sizeof(slot));
10,125✔
2587
  if (code != TSDB_CODE_SUCCESS) {
10,166!
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,166✔
2594
  if (code != TSDB_CODE_SUCCESS) {
10,152!
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,152✔
2598
  }
2599

2600
  return code;
10,151✔
2601
}
2602

2603
int32_t tableListGetGroupList(const STableListInfo* pTableList, int32_t ordinalGroupIndex, STableKeyInfo** pKeyInfo,
2,759,428✔
2604
                              int32_t* size) {
2605
  int32_t totalGroups = tableListGetOutputGroups(pTableList);
2,759,428✔
2606
  int32_t numOfTables = 0;
2,762,532✔
2607
  int32_t code = tableListGetSize(pTableList, &numOfTables);
2,762,532✔
2608
  if (code != TSDB_CODE_SUCCESS) {
2,759,366!
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,759,366!
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,759,366!
2620
    *size = numOfTables;
2,760,286✔
2621
    *pKeyInfo = (*size == 0) ? NULL : taosArrayGet(pTableList->pTableList, 0);
2,760,286✔
2622
    return TSDB_CODE_SUCCESS;
2,756,889✔
2623
  } else if (totalGroups == numOfTables) {
×
2624
    *size = 1;
1,008✔
2625
    *pKeyInfo = taosArrayGet(pTableList->pTableList, ordinalGroupIndex);
1,008✔
2626
    return TSDB_CODE_SUCCESS;
1,008✔
2627
  }
2628

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

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

2640
int32_t tableListGetOutputGroups(const STableListInfo* pTableList) { return pTableList->numOfOuputGroups; }
8,236,481✔
2641

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

2644
STableListInfo* tableListCreate() {
4,929,415✔
2645
  STableListInfo* pListInfo = taosMemoryCalloc(1, sizeof(STableListInfo));
4,929,415!
2646
  if (pListInfo == NULL) {
4,941,781!
2647
    return NULL;
×
2648
  }
2649

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

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

2661
  pListInfo->numOfOuputGroups = 1;
4,941,757✔
2662
  return pListInfo;
4,941,757✔
2663

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

2669
void tableListDestroy(STableListInfo* pTableListInfo) {
4,943,198✔
2670
  if (pTableListInfo == NULL) {
4,943,198!
2671
    return;
×
2672
  }
2673

2674
  taosArrayDestroy(pTableListInfo->pTableList);
4,943,198✔
2675
  taosMemoryFreeClear(pTableListInfo->groupOffset);
4,944,186!
2676

2677
  taosHashCleanup(pTableListInfo->map);
4,944,186✔
2678
  taosHashCleanup(pTableListInfo->remainGroups);
4,944,054✔
2679
  pTableListInfo->pTableList = NULL;
4,944,180✔
2680
  pTableListInfo->map = NULL;
4,944,180✔
2681
  taosMemoryFree(pTableListInfo);
4,944,180!
2682
}
2683

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

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

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

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

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

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

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

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

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

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

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

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

2763
  bool   groupByTbname = groupbyTbname(group);
4,615,043✔
2764
  size_t numOfTables = taosArrayGetSize(pTableListInfo->pTableList);
4,614,000✔
2765
  if (!numOfTables) {
4,612,409!
2766
    return code;
×
2767
  }
2768
  if (group == NULL || groupByTbname) {
4,612,409✔
2769
    if (tsCountAlwaysReturnValue && QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pScanNode) &&
4,548,085✔
2770
        ((STableScanPhysiNode*)pScanNode)->needCountEmptyTable) {
2,518,723✔
2771
      pTableListInfo->remainGroups =
173,183✔
2772
          taosHashInit(numOfTables, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
173,191✔
2773
      if (pTableListInfo->remainGroups == NULL) {
173,183!
2774
        return terrno;
×
2775
      }
2776

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

2785
        int32_t tempRes = taosHashPut(pTableListInfo->remainGroups, &(info->groupId), sizeof(info->groupId),
454,692✔
2786
                                      &(info->uid), sizeof(info->uid));
454,692✔
2787
        if (tempRes != TSDB_CODE_SUCCESS && tempRes != TSDB_CODE_DUP_KEY) {
454,798!
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++) {
15,994,103✔
2794
        STableKeyInfo* info = taosArrayGet(pTableListInfo->pTableList, i);
11,617,113✔
2795
        if (!info) {
11,618,917!
2796
          qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
2797
          return terrno;
×
2798
        }
2799
        info->groupId = groupByTbname ? info->uid : 0;
11,619,209✔
2800
      }
2801
    }
2802

2803
    pTableListInfo->oneTableForEachGroup = groupByTbname;
4,550,269✔
2804
    if (numOfTables == 1 && pTableListInfo->idInfo.tableType == TSDB_CHILD_TABLE) {
4,550,269✔
2805
      pTableListInfo->oneTableForEachGroup = true;
412,239✔
2806
    }
2807

2808
    if (groupSort && groupByTbname) {
4,550,269✔
2809
      taosArraySort(pTableListInfo->pTableList, orderbyGroupIdComparFn);
26,953✔
2810
      pTableListInfo->numOfOuputGroups = numOfTables;
26,944✔
2811
    } else if (groupByTbname && pScanNode->groupOrderScan) {
4,523,316✔
2812
      pTableListInfo->numOfOuputGroups = numOfTables;
145✔
2813
    } else {
2814
      pTableListInfo->numOfOuputGroups = 1;
4,523,171✔
2815
    }
2816
  } else {
2817
    bool initRemainGroups = false;
64,324✔
2818
    if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(pScanNode)) {
64,324✔
2819
      STableScanPhysiNode* pTableScanNode = (STableScanPhysiNode*)pScanNode;
63,700✔
2820
      if (tsCountAlwaysReturnValue && pTableScanNode->needCountEmptyTable &&
63,700✔
2821
          !(groupSort || pScanNode->groupOrderScan)) {
13,182!
2822
        initRemainGroups = true;
13,137✔
2823
      }
2824
    }
2825

2826
    code = getColInfoResultForGroupby(pHandle->vnode, group, pTableListInfo, digest, pAPI, initRemainGroups);
64,324✔
2827
    if (code != TSDB_CODE_SUCCESS) {
64,391✔
2828
      return code;
15✔
2829
    }
2830

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

2833
    if (groupSort || pScanNode->groupOrderScan) {
64,376✔
2834
      code = sortTableGroup(pTableListInfo);
911✔
2835
    }
2836
  }
2837

2838
  // add all table entry in the hash map
2839
  size_t size = taosArrayGetSize(pTableListInfo->pTableList);
4,614,644✔
2840
  for (int32_t i = 0; i < size; ++i) {
16,967,781✔
2841
    STableKeyInfo* p = taosArrayGet(pTableListInfo->pTableList, i);
12,340,051✔
2842
    if (!p) {
12,321,738!
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));
12,321,738✔
2847
    if (tempRes != TSDB_CODE_SUCCESS && tempRes != TSDB_CODE_DUP_KEY) {
12,354,532!
2848
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(tempRes));
×
2849
      return tempRes;
×
2850
    }
2851
  }
2852

2853
  return code;
4,627,730✔
2854
}
2855

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

2862
  if (pHandle == NULL) {
4,843,395!
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,843,395✔
2868
  int32_t code = getTableList(pHandle->vnode, pScanNode, pTagCond, pTagIndexCond, pTableListInfo, digest, idStr,
4,843,395✔
2869
                              &pTaskInfo->storageAPI);
2870
  if (code != TSDB_CODE_SUCCESS) {
4,844,448✔
2871
    qError("failed to getTableList, code: %s", tstrerror(code));
8!
2872
    return code;
8✔
2873
  }
2874

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

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

2882
  if (numOfTables == 0) {
4,848,075✔
2883
    qDebug("no table qualified for query, %s" PRIx64, idStr);
227,510✔
2884
    return TSDB_CODE_SUCCESS;
227,526✔
2885
  }
2886

2887
  code = buildGroupIdMapForAllTables(pTableListInfo, pHandle, pScanNode, pGroupTags, groupSort, digest,
4,620,565✔
2888
                                     &pTaskInfo->storageAPI);
2889
  if (code != TSDB_CODE_SUCCESS) {
4,623,967✔
2890
    return code;
15✔
2891
  }
2892

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

2896
  return TSDB_CODE_SUCCESS;
4,623,562✔
2897
}
2898

2899
char* getStreamOpName(uint16_t opType) {
2,617,611✔
2900
  switch (opType) {
2,617,611!
2901
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN:
465,199✔
2902
      return "stream scan";
465,199✔
2903
    case QUERY_NODE_PHYSICAL_PLAN_PROJECT:
31,523✔
2904
      return "project";
31,523✔
2905
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL:
1,297,652✔
2906
      return "interval single";
1,297,652✔
2907
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_INTERVAL:
12,957✔
2908
      return "interval final";
12,957✔
2909
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_INTERVAL:
32,483✔
2910
      return "interval semi";
32,483✔
2911
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_MID_INTERVAL:
343✔
2912
      return "interval mid";
343✔
2913
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FILL:
5,959✔
2914
      return "stream fill";
5,959✔
2915
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SESSION:
10,436✔
2916
      return "session single";
10,436✔
2917
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_SESSION:
1,570✔
2918
      return "session semi";
1,570✔
2919
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_SESSION:
1,280✔
2920
      return "session final";
1,280✔
2921
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_STATE:
7,445✔
2922
      return "state single";
7,445✔
2923
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_PARTITION:
741,657✔
2924
      return "stream partitionby";
741,657✔
2925
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_EVENT:
834✔
2926
      return "stream event";
834✔
2927
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_COUNT:
10,224✔
2928
      return "stream count";
10,224✔
2929
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERP_FUNC:
7,701✔
2930
      return "stream interp";
7,701✔
2931
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_CONTINUE_INTERVAL:
4,706✔
2932
      return "interval continue";
4,706✔
2933
  }
2934
  return "";
×
2935
}
2936

2937
void printDataBlock(SSDataBlock* pBlock, const char* flag, const char* taskIdStr) {
1,232,520✔
2938
  if (!pBlock) {
1,232,520!
2939
    qDebug("%s===stream===%s: Block is Null", taskIdStr, flag);
×
2940
    return;
×
2941
  } else if (pBlock->info.rows == 0) {
1,232,520✔
2942
    qDebug("%s===stream===%s: Block is Empty. block type %d", taskIdStr, flag, pBlock->info.type);
23,078✔
2943
    return;
23,079✔
2944
  }
2945
  if (qDebugFlag & DEBUG_DEBUG) {
1,209,442✔
2946
    char*   pBuf = NULL;
48,462✔
2947
    int32_t code = dumpBlockData(pBlock, flag, &pBuf, taskIdStr);
48,462✔
2948
    if (code == 0) {
48,465!
2949
      qDebug("%s", pBuf);
48,465!
2950
      taosMemoryFree(pBuf);
48,465!
2951
    }
2952
  }
2953
}
2954

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

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

2979
void updateTimeWindowInfo(SColumnInfoData* pColData, STimeWindow* pWin, int64_t delta) {
472,162,949✔
2980
  int64_t* ts = (int64_t*)pColData->pData;
472,162,949✔
2981

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

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

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

2999
    if (colDataIsNull(pColInfoData, pBlock->info.rows, rowIndex, pColAgg)) {
65,622,810!
3000
      if (isNull[i] != 1) return 1;
2,182,834✔
3001
    } else {
3002
      if (isNull[i] != 0) return 1;
30,628,571✔
3003
      const char* val = colDataGetData(pColInfoData, rowIndex);
30,614,347!
3004
      if (pCol->type == TSDB_DATA_TYPE_JSON) {
30,614,347!
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,614,347!
3009
        if (memcmp(p, val, varDataTLen(val)) != 0) return 1;
10,404,473✔
3010
        p += varDataTLen(val);
10,268,162✔
3011
      } else {
3012
        if (0 != memcmp(p, val, pCol->bytes)) return 1;
20,209,874✔
3013
        p += pCol->bytes;
19,877,089✔
3014
      }
3015
    }
3016
  }
3017
  if ((int32_t)(p - oldkeyBuf) != oldKeysLen) return 1;
20,893,118✔
3018
  return 0;
20,892,822✔
3019
}
3020

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

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

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

3034
    if (colDataIsNull(pColInfoData, pBlock->info.rows, rowIndex, pColAgg)) {
1,912,780!
3035
      isNull[i] = 1;
34,561✔
3036
    } else {
3037
      isNull[i] = 0;
921,829✔
3038
      const char* val = colDataGetData(pColInfoData, rowIndex);
921,829!
3039
      if (pCol->type == TSDB_DATA_TYPE_JSON) {
921,829!
3040
        int32_t len = getJsonValueLen(val);
×
3041
        memcpy(p, val, len);
×
3042
        p += len;
×
3043
      } else if (IS_VAR_DATA_TYPE(pCol->type)) {
921,829!
3044
        varDataCopy(p, val);
139,557✔
3045
        p += varDataTLen(val);
139,557✔
3046
      } else {
3047
        memcpy(p, val, pCol->bytes);
782,272✔
3048
        p += pCol->bytes;
782,272✔
3049
      }
3050
    }
3051
  }
3052
  return (int32_t)(p - keyBuf);
483,549✔
3053
}
3054

3055
uint64_t calcGroupId(char* pData, int32_t len) {
17,266,530✔
3056
  T_MD5_CTX context;
3057
  tMD5Init(&context);
17,266,530✔
3058
  tMD5Update(&context, (uint8_t*)pData, len);
17,284,596✔
3059
  tMD5Final(&context);
17,391,899✔
3060

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

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

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

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

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

© 2026 Coveralls, Inc