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

taosdata / TDengine / #3523

06 Nov 2024 02:29AM UTC coverage: 55.861% (-2.4%) from 58.216%
#3523

push

travis-ci

web-flow
Merge pull request #28551 from taosdata/feat/TS-5215-2

test(blob): testing & fixes for blob

106075 of 245834 branches covered (43.15%)

Branch coverage included in aggregate %.

0 of 15 new or added lines in 2 files covered. (0.0%)

17003 existing lines in 254 files now uncovered.

181910 of 269703 relevant lines covered (67.45%)

1527639.59 hits per line

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

70.15
/source/libs/executor/src/executorInt.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 "filter.h"
17
#include "function.h"
18
#include "functionMgt.h"
19
#include "os.h"
20
#include "querynodes.h"
21
#include "tfill.h"
22
#include "tname.h"
23

24
#include "tdatablock.h"
25
#include "tmsg.h"
26
#include "ttime.h"
27

28
#include "executorInt.h"
29
#include "index.h"
30
#include "operator.h"
31
#include "query.h"
32
#include "querytask.h"
33
#include "storageapi.h"
34
#include "tcompare.h"
35
#include "thash.h"
36
#include "ttypes.h"
37

38
#define SET_REVERSE_SCAN_FLAG(runtime)    ((runtime)->scanFlag = REVERSE_SCAN)
39
#define GET_FORWARD_DIRECTION_FACTOR(ord) (((ord) == TSDB_ORDER_ASC) ? QUERY_ASC_FORWARD_STEP : QUERY_DESC_FORWARD_STEP)
40

41
#if 0
42
static UNUSED_FUNC void *u_malloc (size_t __size) {
43
  uint32_t v = taosRand();
44

45
  if (v % 1000 <= 0) {
46
    return NULL;
47
  } else {
48
    return taosMemoryMalloc(__size);
49
  }
50
}
51

52
static UNUSED_FUNC void* u_calloc(size_t num, size_t __size) {
53
  uint32_t v = taosRand();
54
  if (v % 1000 <= 0) {
55
    return NULL;
56
  } else {
57
    return taosMemoryCalloc(num, __size);
58
  }
59
}
60

61
static UNUSED_FUNC void* u_realloc(void* p, size_t __size) {
62
  uint32_t v = taosRand();
63
  if (v % 5 <= 1) {
64
    return NULL;
65
  } else {
66
    return taosMemoryRealloc(p, __size);
67
  }
68
}
69

70
#define calloc  u_calloc
71
#define malloc  u_malloc
72
#define realloc u_realloc
73
#endif
74

75
static int32_t setBlockSMAInfo(SqlFunctionCtx* pCtx, SExprInfo* pExpr, SSDataBlock* pBlock);
76

77
static int32_t initCtxOutputBuffer(SqlFunctionCtx* pCtx, int32_t size);
78
static void    doApplyScalarCalculation(SOperatorInfo* pOperator, SSDataBlock* pBlock, int32_t order, int32_t scanFlag);
79

80
static int32_t doSetInputDataBlock(SExprSupp* pExprSup, SSDataBlock* pBlock, int32_t order, int32_t scanFlag,
81
                                   bool createDummyCol);
82
static void    doCopyToSDataBlock(SExecTaskInfo* pTaskInfo, SSDataBlock* pBlock, SExprSupp* pSup, SDiskbasedBuf* pBuf,
83
                                  SGroupResInfo* pGroupResInfo, int32_t threshold, bool ignoreGroup);
84

85
SResultRow* getNewResultRow(SDiskbasedBuf* pResultBuf, int32_t* currentPageId, int32_t interBufSize) {
2,925,212✔
86
  SFilePage* pData = NULL;
2,925,212✔
87

88
  // in the first scan, new space needed for results
89
  int32_t pageId = -1;
2,925,212✔
90
  if (*currentPageId == -1) {
2,925,212✔
91
    pData = getNewBufPage(pResultBuf, &pageId);
133,532✔
92
    if (pData == NULL) {
133,563✔
93
      qError("failed to get buffer, code:%s", tstrerror(terrno));
31✔
94
      return NULL;
26✔
95
    }
96
    pData->num = sizeof(SFilePage);
133,532✔
97
  } else {
98
    pData = getBufPage(pResultBuf, *currentPageId);
2,791,680✔
99
    if (pData == NULL) {
2,791,690!
100
      qError("failed to get buffer, code:%s", tstrerror(terrno));
×
101
      return NULL;
×
102
    }
103

104
    pageId = *currentPageId;
2,791,690✔
105

106
    if (pData->num + interBufSize > getBufPageSize(pResultBuf)) {
2,791,690✔
107
      // release current page first, and prepare the next one
108
      releaseBufPage(pResultBuf, pData);
77,815✔
109

110
      pData = getNewBufPage(pResultBuf, &pageId);
77,817✔
111
      if (pData == NULL) {
77,838✔
112
        qError("failed to get buffer, code:%s", tstrerror(terrno));
18!
113
        return NULL;
18✔
114
      }
115
      pData->num = sizeof(SFilePage);
77,820✔
116
    }
117
  }
118

119
  if (pData == NULL) {
2,925,206!
120
    return NULL;
×
121
  }
122

123
  setBufPageDirty(pData, true);
2,925,206✔
124

125
  // set the number of rows in current disk page
126
  SResultRow* pResultRow = (SResultRow*)((char*)pData + pData->num);
2,925,117✔
127

128
  memset((char*)pResultRow, 0, interBufSize);
2,925,117✔
129
  pResultRow->pageId = pageId;
2,925,117✔
130
  pResultRow->offset = (int32_t)pData->num;
2,925,117✔
131

132
  *currentPageId = pageId;
2,925,117✔
133
  pData->num += interBufSize;
2,925,117✔
134
  return pResultRow;
2,925,117✔
135
}
136

137
/**
138
 * the struct of key in hash table
139
 * +----------+---------------+
140
 * | group id |   key data    |
141
 * | 8 bytes  | actual length |
142
 * +----------+---------------+
143
 */
144
SResultRow* doSetResultOutBufByKey(SDiskbasedBuf* pResultBuf, SResultRowInfo* pResultRowInfo, char* pData,
3,101,189✔
145
                                   int32_t bytes, bool masterscan, uint64_t groupId, SExecTaskInfo* pTaskInfo,
146
                                   bool isIntervalQuery, SAggSupporter* pSup, bool keepGroup) {
147
  SET_RES_WINDOW_KEY(pSup->keyBuf, pData, bytes, groupId);
3,101,189✔
148
  if (!keepGroup) {
3,101,189✔
149
    *(uint64_t*)pSup->keyBuf = calcGroupId(pSup->keyBuf, GET_RES_WINDOW_KEY_LEN(bytes));
327,323✔
150
  }
151

152
  SResultRowPosition* p1 =
153
      (SResultRowPosition*)tSimpleHashGet(pSup->pResultRowHashTable, pSup->keyBuf, GET_RES_WINDOW_KEY_LEN(bytes));
3,101,336✔
154

155
  SResultRow* pResult = NULL;
3,101,452✔
156

157
  // in case of repeat scan/reverse scan, no new time window added.
158
  if (isIntervalQuery) {
3,101,452✔
159
    if (p1 != NULL) {  // the *p1 may be NULL in case of sliding+offset exists.
2,664,129✔
160
      pResult = getResultRowByPos(pResultBuf, p1, true);
62,563✔
161
      if (pResult == NULL) {
62,563!
162
        pTaskInfo->code = terrno;
×
163
        return NULL;
×
164
      }
165

166
      if (pResult->pageId != p1->pageId || pResult->offset != p1->offset) {
62,563!
UNCOV
167
        terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
168
        pTaskInfo->code = terrno;
×
169
        return NULL;
×
170
      }
171
    }
172
  } else {
173
    // In case of group by column query, the required SResultRow object must be existInCurrentResusltRowInfo in the
174
    // pResultRowInfo object.
175
    if (p1 != NULL) {
437,323✔
176
      // todo
177
      pResult = getResultRowByPos(pResultBuf, p1, true);
117,830✔
178
      if (NULL == pResult) {
117,830!
179
        pTaskInfo->code = terrno;
×
180
        return NULL;
×
181
      }
182

183
      if (pResult->pageId != p1->pageId || pResult->offset != p1->offset) {
117,830!
184
        terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
185
        pTaskInfo->code = terrno;
×
UNCOV
186
        return NULL;
×
187
      }
188
    }
189
  }
190

191
  // 1. close current opened time window
192
  if (pResultRowInfo->cur.pageId != -1 && ((pResult == NULL) || (pResult->pageId != pResultRowInfo->cur.pageId))) {
3,101,204✔
193
    SResultRowPosition pos = pResultRowInfo->cur;
2,808,882✔
194
    SFilePage*         pPage = getBufPage(pResultBuf, pos.pageId);
2,808,882✔
195
    if (pPage == NULL) {
2,808,369!
196
      qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
197
      pTaskInfo->code = terrno;
×
198
      return NULL;
×
199
    }
200
    releaseBufPage(pResultBuf, pPage);
2,808,369✔
201
  }
202

203
  // allocate a new buffer page
204
  if (pResult == NULL) {
3,100,456✔
205
    pResult = getNewResultRow(pResultBuf, &pSup->currentPageId, pSup->resultRowSize);
2,920,328✔
206
    if (pResult == NULL) {
2,920,545✔
207
      pTaskInfo->code = terrno;
42✔
208
      return NULL;
145✔
209
    }
210

211
    // add a new result set for a new group
212
    SResultRowPosition pos = {.pageId = pResult->pageId, .offset = pResult->offset};
2,920,503✔
213
    int32_t code = tSimpleHashPut(pSup->pResultRowHashTable, pSup->keyBuf, GET_RES_WINDOW_KEY_LEN(bytes), &pos,
2,920,503✔
214
                                  sizeof(SResultRowPosition));
215
    if (code != TSDB_CODE_SUCCESS) {
2,922,076✔
216
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
117✔
217
      pTaskInfo->code = code;
103✔
218
      return NULL;
103✔
219
    }
220
  }
221

222
  // 2. set the new time window to be the new active time window
223
  pResultRowInfo->cur = (SResultRowPosition){.pageId = pResult->pageId, .offset = pResult->offset};
3,102,087✔
224

225
  // too many time window in query
226
  if (pTaskInfo->execModel == OPTR_EXEC_MODEL_BATCH &&
6,201,946!
227
      tSimpleHashGetSize(pSup->pResultRowHashTable) > MAX_INTERVAL_TIME_WINDOW) {
3,099,941✔
228
    pTaskInfo->code = TSDB_CODE_QRY_TOO_MANY_TIMEWINDOW;
×
229
    return NULL;
×
230
  }
231

232
  return pResult;
3,102,005✔
233
}
234

235
//  query_range_start, query_range_end, window_duration, window_start, window_end
236
int32_t initExecTimeWindowInfo(SColumnInfoData* pColData, STimeWindow* pQueryWindow) {
34,817✔
237
  pColData->info.type = TSDB_DATA_TYPE_TIMESTAMP;
34,817✔
238
  pColData->info.bytes = sizeof(int64_t);
34,817✔
239

240
  int32_t code = colInfoDataEnsureCapacity(pColData, 5, false);
34,817✔
241
  if (code != TSDB_CODE_SUCCESS) {
34,839✔
242
    return code;
2✔
243
  }
244
  colDataSetInt64(pColData, 0, &pQueryWindow->skey);
34,837✔
245
  colDataSetInt64(pColData, 1, &pQueryWindow->ekey);
34,837✔
246

247
  int64_t interval = 0;
34,837✔
248
  colDataSetInt64(pColData, 2, &interval);  // this value may be variable in case of 'n' and 'y'.
249
  colDataSetInt64(pColData, 3, &pQueryWindow->skey);
34,837✔
250
  colDataSetInt64(pColData, 4, &pQueryWindow->ekey);
34,837✔
251
  return TSDB_CODE_SUCCESS;
34,837✔
252
}
253

254
static int32_t doSetInputDataBlockInfo(SExprSupp* pExprSup, SSDataBlock* pBlock, int32_t order, int32_t scanFlag) {
82✔
255
  int32_t         code = TSDB_CODE_SUCCESS;
82✔
256
  int32_t         lino = 0;
82✔
257
  SqlFunctionCtx* pCtx = pExprSup->pCtx;
82✔
258
  for (int32_t i = 0; i < pExprSup->numOfExprs; ++i) {
164✔
259
    pCtx[i].order = order;
82✔
260
    pCtx[i].input.numOfRows = pBlock->info.rows;
82✔
261
    code = setBlockSMAInfo(&pCtx[i], &pExprSup->pExprInfo[i], pBlock);
82✔
262
    QUERY_CHECK_CODE(code, lino, _end);
82!
263
    pCtx[i].pSrcBlock = pBlock;
82✔
264
    pCtx[i].scanFlag = scanFlag;
82✔
265
  }
266

267
_end:
82✔
268
  if (code != TSDB_CODE_SUCCESS) {
82!
269
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
270
  }
271
  return code;
82✔
272
}
273

274
int32_t setInputDataBlock(SExprSupp* pExprSup, SSDataBlock* pBlock, int32_t order, int32_t scanFlag,
398,065✔
275
                          bool createDummyCol) {
276
  if (pBlock->pBlockAgg != NULL) {
398,065✔
277
    return doSetInputDataBlockInfo(pExprSup, pBlock, order, scanFlag);
82✔
278
  } else {
279
    return doSetInputDataBlock(pExprSup, pBlock, order, scanFlag, createDummyCol);
397,983✔
280
  }
281
}
282

283
static int32_t doCreateConstantValColumnInfo(SInputColumnInfoData* pInput, SFunctParam* pFuncParam, int32_t paramIndex,
×
284
                                             int32_t numOfRows) {
285
  int32_t          code = TSDB_CODE_SUCCESS;
×
286
  int32_t          lino = 0;
×
287
  SColumnInfoData* pColInfo = NULL;
×
288
  if (pInput->pData[paramIndex] == NULL) {
×
289
    pColInfo = taosMemoryCalloc(1, sizeof(SColumnInfoData));
×
290
    QUERY_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
291

292
    // Set the correct column info (data type and bytes)
293
    pColInfo->info.type = pFuncParam->param.nType;
×
294
    pColInfo->info.bytes = pFuncParam->param.nLen;
×
295

296
    pInput->pData[paramIndex] = pColInfo;
×
297
  } else {
298
    pColInfo = pInput->pData[paramIndex];
×
299
  }
300

301
  code = colInfoDataEnsureCapacity(pColInfo, numOfRows, false);
×
302
  QUERY_CHECK_CODE(code, lino, _end);
×
303

304
  int8_t type = pFuncParam->param.nType;
×
305
  if (type == TSDB_DATA_TYPE_BIGINT || type == TSDB_DATA_TYPE_UBIGINT) {
×
306
    int64_t v = pFuncParam->param.i;
×
307
    for (int32_t i = 0; i < numOfRows; ++i) {
×
308
      colDataSetInt64(pColInfo, i, &v);
×
309
    }
310
  } else if (type == TSDB_DATA_TYPE_DOUBLE) {
×
311
    double v = pFuncParam->param.d;
×
312
    for (int32_t i = 0; i < numOfRows; ++i) {
×
313
      colDataSetDouble(pColInfo, i, &v);
×
314
    }
315
  } else if (type == TSDB_DATA_TYPE_VARCHAR || type == TSDB_DATA_TYPE_GEOMETRY) {
×
316
    char* tmp = taosMemoryMalloc(pFuncParam->param.nLen + VARSTR_HEADER_SIZE);
×
317
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
318

319
    STR_WITH_SIZE_TO_VARSTR(tmp, pFuncParam->param.pz, pFuncParam->param.nLen);
×
320
    for (int32_t i = 0; i < numOfRows; ++i) {
×
321
      code = colDataSetVal(pColInfo, i, tmp, false);
×
322
      QUERY_CHECK_CODE(code, lino, _end);
×
323
    }
324
    taosMemoryFree(tmp);
×
325
  }
326

327
_end:
×
328
  if (code != TSDB_CODE_SUCCESS) {
×
329
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
330
  }
331
  return code;
×
332
}
333

334
static int32_t doSetInputDataBlock(SExprSupp* pExprSup, SSDataBlock* pBlock, int32_t order, int32_t scanFlag,
397,983✔
335
                                   bool createDummyCol) {
336
  int32_t         code = TSDB_CODE_SUCCESS;
397,983✔
337
  int32_t         lino = 0;
397,983✔
338
  SqlFunctionCtx* pCtx = pExprSup->pCtx;
397,983✔
339

340
  for (int32_t i = 0; i < pExprSup->numOfExprs; ++i) {
2,250,715✔
341
    pCtx[i].order = order;
1,853,174✔
342
    pCtx[i].input.numOfRows = pBlock->info.rows;
1,853,174✔
343

344
    pCtx[i].pSrcBlock = pBlock;
1,853,174✔
345
    pCtx[i].scanFlag = scanFlag;
1,853,174✔
346

347
    SInputColumnInfoData* pInput = &pCtx[i].input;
1,853,174✔
348
    pInput->uid = pBlock->info.id.uid;
1,853,174✔
349
    pInput->colDataSMAIsSet = false;
1,853,174✔
350

351
    SExprInfo* pOneExpr = &pExprSup->pExprInfo[i];
1,853,174✔
352
    bool       hasPk = pOneExpr->pExpr->nodeType == QUERY_NODE_FUNCTION && pOneExpr->pExpr->_function.pFunctNode->hasPk;
1,853,174✔
353
    pCtx[i].hasPrimaryKey = hasPk;
1,853,174✔
354

355
    int16_t tsParamIdx = (!hasPk) ? pOneExpr->base.numOfParams - 1 : pOneExpr->base.numOfParams - 2;
1,853,174✔
356
    int16_t pkParamIdx = pOneExpr->base.numOfParams - 1;
1,853,174✔
357

358
    for (int32_t j = 0; j < pOneExpr->base.numOfParams; ++j) {
3,830,384✔
359
      SFunctParam* pFuncParam = &pOneExpr->base.pParam[j];
1,977,652✔
360
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
1,977,652✔
361
        int32_t slotId = pFuncParam->pCol->slotId;
1,828,011✔
362
        pInput->pData[j] = taosArrayGet(pBlock->pDataBlock, slotId);
1,828,011✔
363
        pInput->totalRows = pBlock->info.rows;
1,827,981✔
364
        pInput->numOfRows = pBlock->info.rows;
1,827,981✔
365
        pInput->startRowIndex = 0;
1,827,981✔
366
        pInput->blankFill = pBlock->info.blankFill;
1,827,981✔
367

368
        // NOTE: the last parameter is the primary timestamp column
369
        // todo: refactor this
370

371
        if (fmIsImplicitTsFunc(pCtx[i].functionId) && (j == tsParamIdx)) {
1,827,981✔
372
          pInput->pPTS = pInput->pData[j];  // in case of merge function, this is not always the ts column data.
121,588✔
373
        }
374
        if (hasPk && (j == pkParamIdx)) {
1,827,569✔
375
          pInput->pPrimaryKey = pInput->pData[j];
916✔
376
        }
377
        QUERY_CHECK_CONDITION((pInput->pData[j] != NULL), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
1,827,569!
378
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
149,641✔
379
        // todo avoid case: top(k, 12), 12 is the value parameter.
380
        // sum(11), 11 is also the value parameter.
381
        if (createDummyCol && pOneExpr->base.numOfParams == 1) {
143,855!
382
          pInput->totalRows = pBlock->info.rows;
×
383
          pInput->numOfRows = pBlock->info.rows;
×
384
          pInput->startRowIndex = 0;
×
385
          pInput->blankFill = pBlock->info.blankFill;
×
386

387
          code = doCreateConstantValColumnInfo(pInput, pFuncParam, j, pBlock->info.rows);
×
388
          QUERY_CHECK_CODE(code, lino, _end);
×
389
        }
390
      }
391
    }
392
  }
393

394
_end:
397,541✔
395
  if (code != TSDB_CODE_SUCCESS) {
397,541!
396
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
397
  }
398
  return code;
398,019✔
399
}
400

401
bool functionNeedToExecute(SqlFunctionCtx* pCtx) {
8,786,379✔
402
  struct SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
8,786,379✔
403

404
  // in case of timestamp column, always generated results.
405
  int32_t functionId = pCtx->functionId;
8,786,379✔
406
  if (functionId == -1) {
8,786,379✔
407
    return false;
1,453,581✔
408
  }
409

410
  if (pCtx->scanFlag == PRE_SCAN) {
7,332,798✔
411
    return fmIsRepeatScanFunc(pCtx->functionId);
5,020✔
412
  }
413

414
  if (isRowEntryCompleted(pResInfo)) {
7,327,778!
415
    return false;
×
416
  }
417

418
  return true;
7,327,778✔
419
}
420

421
static int32_t doCreateConstantValColumnSMAInfo(SInputColumnInfoData* pInput, SFunctParam* pFuncParam, int32_t type,
×
422
                                                int32_t paramIndex, int32_t numOfRows) {
423
  if (pInput->pData[paramIndex] == NULL) {
×
424
    pInput->pData[paramIndex] = taosMemoryCalloc(1, sizeof(SColumnInfoData));
×
425
    if (pInput->pData[paramIndex] == NULL) {
×
426
      return terrno;
×
427
    }
428

429
    // Set the correct column info (data type and bytes)
430
    pInput->pData[paramIndex]->info.type = type;
×
431
    pInput->pData[paramIndex]->info.bytes = tDataTypes[type].bytes;
×
432
  }
433

434
  SColumnDataAgg* da = NULL;
×
435
  if (pInput->pColumnDataAgg[paramIndex] == NULL) {
×
436
    da = taosMemoryCalloc(1, sizeof(SColumnDataAgg));
×
437
    if (!da) {
×
438
      return terrno;
×
439
    }
440
    pInput->pColumnDataAgg[paramIndex] = da;
×
441
    if (da == NULL) {
×
442
      return TSDB_CODE_OUT_OF_MEMORY;
×
443
    }
444
  } else {
445
    da = pInput->pColumnDataAgg[paramIndex];
×
446
  }
447

448
  if (type == TSDB_DATA_TYPE_BIGINT) {
×
449
    int64_t v = pFuncParam->param.i;
×
450
    *da = (SColumnDataAgg){.numOfNull = 0, .min = v, .max = v, .sum = v * numOfRows};
×
451
  } else if (type == TSDB_DATA_TYPE_DOUBLE) {
×
452
    double v = pFuncParam->param.d;
×
453
    *da = (SColumnDataAgg){.numOfNull = 0};
×
454

455
    *(double*)&da->min = v;
×
456
    *(double*)&da->max = v;
×
457
    *(double*)&da->sum = v * numOfRows;
×
458
  } else if (type == TSDB_DATA_TYPE_BOOL) {  // todo validate this data type
×
459
    bool v = pFuncParam->param.i;
×
460

461
    *da = (SColumnDataAgg){.numOfNull = 0};
×
462
    *(bool*)&da->min = 0;
×
463
    *(bool*)&da->max = v;
×
464
    *(bool*)&da->sum = v * numOfRows;
×
465
  } else if (type == TSDB_DATA_TYPE_TIMESTAMP) {
×
466
    // do nothing
467
  } else {
468
    qError("invalid constant type for sma info");
×
469
  }
470

471
  return TSDB_CODE_SUCCESS;
×
472
}
473

474
int32_t setBlockSMAInfo(SqlFunctionCtx* pCtx, SExprInfo* pExprInfo, SSDataBlock* pBlock) {
82✔
475
  int32_t code = TSDB_CODE_SUCCESS;
82✔
476
  int32_t lino = 0;
82✔
477
  int32_t numOfRows = pBlock->info.rows;
82✔
478

479
  SInputColumnInfoData* pInput = &pCtx->input;
82✔
480
  pInput->numOfRows = numOfRows;
82✔
481
  pInput->totalRows = numOfRows;
82✔
482

483
  if (pBlock->pBlockAgg != NULL) {
82!
484
    pInput->colDataSMAIsSet = true;
82✔
485

486
    for (int32_t j = 0; j < pExprInfo->base.numOfParams; ++j) {
164✔
487
      SFunctParam* pFuncParam = &pExprInfo->base.pParam[j];
82✔
488

489
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
82!
490
        int32_t slotId = pFuncParam->pCol->slotId;
82✔
491
        pInput->pColumnDataAgg[j] = &pBlock->pBlockAgg[slotId];
82✔
492
        if (pInput->pColumnDataAgg[j]->colId == -1) {
82!
493
          pInput->colDataSMAIsSet = false;
×
494
        }
495

496
        // Here we set the column info data since the data type for each column data is required, but
497
        // the data in the corresponding SColumnInfoData will not be used.
498
        pInput->pData[j] = taosArrayGet(pBlock->pDataBlock, slotId);
82✔
499
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
×
500
        code = doCreateConstantValColumnSMAInfo(pInput, pFuncParam, pFuncParam->param.nType, j, pBlock->info.rows);
×
501
        QUERY_CHECK_CODE(code, lino, _end);
×
502
      }
503
    }
504
  } else {
505
    pInput->colDataSMAIsSet = false;
×
506
  }
507

508
_end:
82✔
509
  if (code != TSDB_CODE_SUCCESS) {
82!
510
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
511
  }
512
  return code;
82✔
513
}
514

515
/////////////////////////////////////////////////////////////////////////////////////////////
516
STimeWindow getAlignQueryTimeWindow(const SInterval* pInterval, int64_t key) {
26,879✔
517
  STimeWindow win = {0};
26,879✔
518
  win.skey = taosTimeTruncate(key, pInterval);
26,879✔
519

520
  /*
521
   * if the realSkey > INT64_MAX - pInterval->interval, the query duration between
522
   * realSkey and realEkey must be less than one interval.Therefore, no need to adjust the query ranges.
523
   */
524
  win.ekey = taosTimeGetIntervalEnd(win.skey, pInterval);
26,890✔
525
  if (win.ekey < win.skey) {
26,898✔
526
    win.ekey = INT64_MAX;
1,345✔
527
  }
528

529
  return win;
26,898✔
530
}
531

532
int32_t setResultRowInitCtx(SResultRow* pResult, SqlFunctionCtx* pCtx, int32_t numOfOutput,
4,887,784✔
533
                            int32_t* rowEntryInfoOffset) {
534
  bool init = false;
4,887,784✔
535
  for (int32_t i = 0; i < numOfOutput; ++i) {
20,019,511✔
536
    pCtx[i].resultInfo = getResultEntryInfo(pResult, i, rowEntryInfoOffset);
15,134,739✔
537
    if (init) {
15,134,297✔
538
      continue;
805,968✔
539
    }
540

541
    struct SResultRowEntryInfo* pResInfo = pCtx[i].resultInfo;
14,328,329✔
542
    if (isRowEntryCompleted(pResInfo) && isRowEntryInitialized(pResInfo)) {
14,328,329!
543
      continue;
×
544
    }
545

546
    if (pCtx[i].isPseudoFunc) {
14,328,329✔
547
      continue;
6,284,059✔
548
    }
549

550
    if (!pResInfo->initialized) {
8,044,270✔
551
      if (pCtx[i].functionId != -1) {
7,759,203✔
552
        int32_t code = pCtx[i].fpSet.init(&pCtx[i], pResInfo);
6,687,534✔
553
        if (code != TSDB_CODE_SUCCESS && fmIsUserDefinedFunc(pCtx[i].functionId)) {
6,687,251!
554
          pResInfo->initialized = false;
×
555
          return TSDB_CODE_UDF_FUNC_EXEC_FAILURE;
×
556
        } else if (code != TSDB_CODE_SUCCESS) {
6,684,964!
557
          return code;
×
558
        }
559
      } else {
560
        pResInfo->initialized = true;
1,071,669✔
561
      }
562
    } else {
563
      init = true;
285,067✔
564
    }
565
  }
566
  return TSDB_CODE_SUCCESS;
4,884,772✔
567
}
568

569
void clearResultRowInitFlag(SqlFunctionCtx* pCtx, int32_t numOfOutput) {
447,806✔
570
  for (int32_t i = 0; i < numOfOutput; ++i) {
1,684,617✔
571
    SResultRowEntryInfo* pResInfo = pCtx[i].resultInfo;
1,236,811✔
572
    if (pResInfo == NULL) {
1,236,811!
573
      continue;
×
574
    }
575

576
    pResInfo->initialized = false;
1,236,811✔
577
    pResInfo->numOfRes = 0;
1,236,811✔
578
    pResInfo->isNullRes = 0;
1,236,811✔
579
    pResInfo->complete = false;
1,236,811✔
580
  }
581
}
447,806✔
582

583
int32_t doFilter(SSDataBlock* pBlock, SFilterInfo* pFilterInfo, SColMatchInfo* pColMatchInfo) {
634,462✔
584
  int32_t code = TSDB_CODE_SUCCESS;
634,462✔
585
  int32_t lino = 0;
634,462✔
586
  if (pFilterInfo == NULL || pBlock->info.rows == 0) {
634,462✔
587
    return TSDB_CODE_SUCCESS;
531,298✔
588
  }
589

590
  SFilterColumnParam param1 = {.numOfCols = taosArrayGetSize(pBlock->pDataBlock), .pDataBlock = pBlock->pDataBlock};
103,164✔
591
  SColumnInfoData*   p = NULL;
103,167✔
592

593
  code = filterSetDataFromSlotId(pFilterInfo, &param1);
103,167✔
594
  QUERY_CHECK_CODE(code, lino, _err);
103,167!
595

596
  int32_t status = 0;
103,167✔
597
  code = filterExecute(pFilterInfo, pBlock, &p, NULL, param1.numOfCols, &status);
103,167✔
598
  QUERY_CHECK_CODE(code, lino, _err);
103,172✔
599

600
  code = extractQualifiedTupleByFilterResult(pBlock, p, status);
102,677✔
601
  QUERY_CHECK_CODE(code, lino, _err);
102,669✔
602

603
  if (pColMatchInfo != NULL) {
102,665✔
604
    size_t size = taosArrayGetSize(pColMatchInfo->pList);
73,221✔
605
    for (int32_t i = 0; i < size; ++i) {
73,218!
606
      SColMatchItem* pInfo = taosArrayGet(pColMatchInfo->pList, i);
73,218✔
607
      QUERY_CHECK_NULL(pInfo, code, lino, _err, terrno);
73,216!
608
      if (pInfo->colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
73,216!
609
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, pInfo->dstSlotId);
73,216✔
610
        QUERY_CHECK_NULL(pColData, code, lino, _err, terrno);
73,213!
611
        if (pColData->info.type == TSDB_DATA_TYPE_TIMESTAMP) {
73,213!
612
          code = blockDataUpdateTsWindow(pBlock, pInfo->dstSlotId);
73,213✔
613
          QUERY_CHECK_CODE(code, lino, _err);
73,214!
614
          break;
73,214✔
615
        }
616
      }
617
    }
618
  }
619
  code = blockDataCheck(pBlock);
102,658✔
620
  QUERY_CHECK_CODE(code, lino, _err);
102,661!
621
_err:
102,661✔
622
  if (code != TSDB_CODE_SUCCESS) {
103,160✔
623
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
499!
624
  }
625
  colDataDestroy(p);
103,160✔
626
  taosMemoryFree(p);
103,176✔
627
  return code;
103,192✔
628
}
629

630
int32_t extractQualifiedTupleByFilterResult(SSDataBlock* pBlock, const SColumnInfoData* p, int32_t status) {
102,669✔
631
  int32_t code = TSDB_CODE_SUCCESS;
102,669✔
632
  int8_t* pIndicator = (int8_t*)p->pData;
102,669✔
633
  if (status == FILTER_RESULT_ALL_QUALIFIED) {
102,669✔
634
    // here nothing needs to be done
635
  } else if (status == FILTER_RESULT_NONE_QUALIFIED) {
27,969✔
636
    code = trimDataBlock(pBlock, pBlock->info.rows, NULL);
17,771✔
637
    pBlock->info.rows = 0;
17,767✔
638
  } else if (status == FILTER_RESULT_PARTIAL_QUALIFIED) {
10,198!
639
    code = trimDataBlock(pBlock, pBlock->info.rows, (bool*)pIndicator);
10,207✔
640
  } else {
641
    qError("unknown filter result type: %d", status);
×
642
  }
643
  return code;
102,671✔
644
}
645

646
void doUpdateNumOfRows(SqlFunctionCtx* pCtx, SResultRow* pRow, int32_t numOfExprs, const int32_t* rowEntryOffset) {
4,516,258✔
647
  bool returnNotNull = false;
4,516,258✔
648
  for (int32_t j = 0; j < numOfExprs; ++j) {
18,228,906✔
649
    SResultRowEntryInfo* pResInfo = getResultEntryInfo(pRow, j, rowEntryOffset);
13,712,560✔
650
    if (!isRowEntryInitialized(pResInfo)) {
13,712,648✔
651
      continue;
5,956,459✔
652
    } else {
653
    }
654

655
    if (pRow->numOfRows < pResInfo->numOfRes) {
7,756,189✔
656
      pRow->numOfRows = pResInfo->numOfRes;
4,420,200✔
657
    }
658

659
    if (pCtx[j].isNotNullFunc) {
7,756,189✔
660
      returnNotNull = true;
3,354,549✔
661
    }
662
  }
663
  // if all expr skips all blocks, e.g. all null inputs for max function, output one row in final result.
664
  //  except for first/last, which require not null output, output no rows
665
  if (pRow->numOfRows == 0 && !returnNotNull) {
4,516,346✔
666
    pRow->numOfRows = 1;
2,229✔
667
  }
668
}
4,516,346✔
669

670
int32_t copyResultrowToDataBlock(SExprInfo* pExprInfo, int32_t numOfExprs, SResultRow* pRow, SqlFunctionCtx* pCtx,
4,381,376✔
671
                                 SSDataBlock* pBlock, const int32_t* rowEntryOffset, SExecTaskInfo* pTaskInfo) {
672
  int32_t code = TSDB_CODE_SUCCESS;
4,381,376✔
673
  int32_t lino = 0;
4,381,376✔
674
  for (int32_t j = 0; j < numOfExprs; ++j) {
17,476,712✔
675
    int32_t slotId = pExprInfo[j].base.resSchema.slotId;
13,097,981✔
676

677
    pCtx[j].resultInfo = getResultEntryInfo(pRow, j, rowEntryOffset);
13,097,981✔
678
    if (pCtx[j].fpSet.finalize) {
13,096,892✔
679
      if (strcmp(pCtx[j].pExpr->pExpr->_function.functionName, "_group_key") == 0 ||
5,086,622✔
680
          strcmp(pCtx[j].pExpr->pExpr->_function.functionName, "_group_const_value") == 0) {
5,078,701✔
681
        // for groupkey along with functions that output multiple lines(e.g. Histogram)
682
        // need to match groupkey result for each output row of that function.
683
        if (pCtx[j].resultInfo->numOfRes != 0) {
8,130!
684
          pCtx[j].resultInfo->numOfRes = pRow->numOfRows;
8,925✔
685
        }
686
      }
687

688
      code = blockDataEnsureCapacity(pBlock, pBlock->info.rows + pCtx[j].resultInfo->numOfRes);
5,086,622✔
689
      QUERY_CHECK_CODE(code, lino, _end);
5,087,444!
690

691
      code = pCtx[j].fpSet.finalize(&pCtx[j], pBlock);
5,087,444✔
692
      if (TSDB_CODE_SUCCESS != code) {
5,090,321✔
693
        qError("%s build result data block error, code %s", GET_TASKID(pTaskInfo), tstrerror(code));
37!
694
        QUERY_CHECK_CODE(code, lino, _end);
×
695
      }
696
    } else if (strcmp(pCtx[j].pExpr->pExpr->_function.functionName, "_select_value") == 0) {
8,010,270✔
697
      // do nothing
698
    } else {
699
      // expand the result into multiple rows. E.g., _wstart, top(k, 20)
700
      // the _wstart needs to copy to 20 following rows, since the results of top-k expands to 20 different rows.
701
      SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, slotId);
6,893,963✔
702
      QUERY_CHECK_NULL(pColInfoData, code, lino, _end, terrno);
6,893,532✔
703
      char*            in = GET_ROWCELL_INTERBUF(pCtx[j].resultInfo);
6,892,892✔
704
      for (int32_t k = 0; k < pRow->numOfRows; ++k) {
13,799,012✔
705
        code = colDataSetValOrCover(pColInfoData, pBlock->info.rows + k, in, pCtx[j].resultInfo->isNullRes);
6,907,196✔
706
        QUERY_CHECK_CODE(code, lino, _end);
6,906,125✔
707
      }
708
    }
709
  }
710

711
_end:
4,378,731✔
712
  if (code != TSDB_CODE_SUCCESS) {
4,378,773✔
713
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
42!
714
  }
715
  return code;
4,384,763✔
716
}
717

718
// todo refactor. SResultRow has direct pointer in miainfo
719
void finalizeResultRows(SDiskbasedBuf* pBuf, SResultRowPosition* resultRowPosition, SExprSupp* pSup,
1,254,202✔
720
                        SSDataBlock* pBlock, SExecTaskInfo* pTaskInfo) {
721
  SFilePage* page = getBufPage(pBuf, resultRowPosition->pageId);
1,254,202✔
722
  if (page == NULL) {
1,254,156!
723
    qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
724
    T_LONG_JMP(pTaskInfo->env, terrno);
×
725
  }
726

727
  SResultRow* pRow = (SResultRow*)((char*)page + resultRowPosition->offset);
1,254,156✔
728

729
  SqlFunctionCtx* pCtx = pSup->pCtx;
1,254,156✔
730
  SExprInfo*      pExprInfo = pSup->pExprInfo;
1,254,156✔
731
  const int32_t*  rowEntryOffset = pSup->rowEntryInfoOffset;
1,254,156✔
732

733
  doUpdateNumOfRows(pCtx, pRow, pSup->numOfExprs, rowEntryOffset);
1,254,156✔
734
  if (pRow->numOfRows == 0) {
1,254,054!
735
    releaseBufPage(pBuf, page);
×
736
    return;
×
737
  }
738

739
  int32_t size = pBlock->info.capacity;
1,254,060✔
740
  while (pBlock->info.rows + pRow->numOfRows > size) {
1,255,583✔
741
    size = size * 1.25;
1,523✔
742
  }
743

744
  int32_t code = blockDataEnsureCapacity(pBlock, size);
1,254,060✔
745
  if (TAOS_FAILED(code)) {
1,254,071✔
746
    releaseBufPage(pBuf, page);
1✔
747
    qError("%s ensure result data capacity failed, code %s", GET_TASKID(pTaskInfo), tstrerror(code));
1!
748
    T_LONG_JMP(pTaskInfo->env, code);
1!
749
  }
750

751
  code = copyResultrowToDataBlock(pExprInfo, pSup->numOfExprs, pRow, pCtx, pBlock, rowEntryOffset, pTaskInfo);
1,254,070✔
752
  if (TAOS_FAILED(code)) {
1,254,154!
753
    releaseBufPage(pBuf, page);
×
754
    qError("%s copy result row to datablock failed, code %s", GET_TASKID(pTaskInfo), tstrerror(code));
×
755
    T_LONG_JMP(pTaskInfo->env, code);
×
756
  }
757

758
  releaseBufPage(pBuf, page);
1,254,154✔
759
  pBlock->info.rows += pRow->numOfRows;
1,254,154✔
760
}
761

762
void doCopyToSDataBlockByHash(SExecTaskInfo* pTaskInfo, SSDataBlock* pBlock, SExprSupp* pSup, SDiskbasedBuf* pBuf,
39,326✔
763
                              SGroupResInfo* pGroupResInfo, SSHashObj* pHashmap, int32_t threshold, bool ignoreGroup) {
764
  int32_t         code = TSDB_CODE_SUCCESS;
39,326✔
765
  int32_t         lino = 0;
39,326✔
766
  SExprInfo*      pExprInfo = pSup->pExprInfo;
39,326✔
767
  int32_t         numOfExprs = pSup->numOfExprs;
39,326✔
768
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
39,326✔
769
  SqlFunctionCtx* pCtx = pSup->pCtx;
39,326✔
770

771
  size_t  keyLen = 0;
39,326✔
772
  int32_t numOfRows = tSimpleHashGetSize(pHashmap);
39,326✔
773

774
  // begin from last iter
775
  void*   pData = pGroupResInfo->dataPos;
39,320✔
776
  int32_t iter = pGroupResInfo->iter;
39,320✔
777
  while ((pData = tSimpleHashIterate(pHashmap, pData, &iter)) != NULL) {
246,812✔
778
    void*               key = tSimpleHashGetKey(pData, &keyLen);
235,911✔
779
    SResultRowPosition* pos = pData;
235,911✔
780
    uint64_t            groupId = *(uint64_t*)key;
235,911✔
781

782
    SFilePage* page = getBufPage(pBuf, pos->pageId);
235,911✔
783
    if (page == NULL) {
235,868!
784
      qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
785
      T_LONG_JMP(pTaskInfo->env, terrno);
×
786
    }
787

788
    SResultRow* pRow = (SResultRow*)((char*)page + pos->offset);
235,868✔
789

790
    doUpdateNumOfRows(pCtx, pRow, numOfExprs, rowEntryOffset);
235,868✔
791

792
    // no results, continue to check the next one
793
    if (pRow->numOfRows == 0) {
235,700!
794
      pGroupResInfo->index += 1;
×
795
      pGroupResInfo->iter = iter;
×
796
      pGroupResInfo->dataPos = pData;
×
797

798
      releaseBufPage(pBuf, page);
×
799
      continue;
×
800
    }
801

802
    if (!ignoreGroup) {
235,700✔
803
      if (pBlock->info.id.groupId == 0) {
57,679✔
804
        pBlock->info.id.groupId = groupId;
29,428✔
805
      } else {
806
        // current value belongs to different group, it can't be packed into one datablock
807
        if (pBlock->info.id.groupId != groupId) {
28,251!
808
          releaseBufPage(pBuf, page);
28,362✔
809
          break;
28,360✔
810
        }
811
      }
812
    }
813

814
    if (pBlock->info.rows + pRow->numOfRows > pBlock->info.capacity) {
207,338!
815
      uint32_t newSize = pBlock->info.rows + pRow->numOfRows + ((numOfRows - iter) > 1 ? 1 : 0);
×
816
      code = blockDataEnsureCapacity(pBlock, newSize);
×
817
      QUERY_CHECK_CODE(code, lino, _end);
×
818
      qDebug("datablock capacity not sufficient, expand to required:%d, current capacity:%d, %s", newSize,
×
819
             pBlock->info.capacity, GET_TASKID(pTaskInfo));
820
      // todo set the pOperator->resultInfo size
821
    }
822

823
    pGroupResInfo->index += 1;
207,338✔
824
    pGroupResInfo->iter = iter;
207,338✔
825
    pGroupResInfo->dataPos = pData;
207,338✔
826

827
    code = copyResultrowToDataBlock(pExprInfo, numOfExprs, pRow, pCtx, pBlock, rowEntryOffset, pTaskInfo);
207,338✔
828
    releaseBufPage(pBuf, page);
207,501✔
829
    QUERY_CHECK_CODE(code, lino, _end);
207,497✔
830
    pBlock->info.rows += pRow->numOfRows;
207,492✔
831
    if (pBlock->info.rows >= threshold) {
207,492!
UNCOV
832
      break;
×
833
    }
834
  }
835

836
  qDebug("%s result generated, rows:%" PRId64 ", groupId:%" PRIu64, GET_TASKID(pTaskInfo), pBlock->info.rows,
39,227✔
837
         pBlock->info.id.groupId);
838
  pBlock->info.dataLoad = 1;
39,227✔
839
  code = blockDataUpdateTsWindow(pBlock, 0);
39,227✔
840
  QUERY_CHECK_CODE(code, lino, _end);
39,219!
841

842
_end:
39,219✔
843
  if (code != TSDB_CODE_SUCCESS) {
39,224✔
844
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
5!
845
    T_LONG_JMP(pTaskInfo->env, code);
5!
846
  }
847
}
39,219✔
848

849
void doCopyToSDataBlock(SExecTaskInfo* pTaskInfo, SSDataBlock* pBlock, SExprSupp* pSup, SDiskbasedBuf* pBuf,
54,406✔
850
                        SGroupResInfo* pGroupResInfo, int32_t threshold, bool ignoreGroup) {
851
  int32_t         code = TSDB_CODE_SUCCESS;
54,406✔
852
  int32_t         lino = 0;
54,406✔
853
  SExprInfo*      pExprInfo = pSup->pExprInfo;
54,406✔
854
  int32_t         numOfExprs = pSup->numOfExprs;
54,406✔
855
  int32_t*        rowEntryOffset = pSup->rowEntryInfoOffset;
54,406✔
856
  SqlFunctionCtx* pCtx = pSup->pCtx;
54,406✔
857

858
  int32_t numOfRows = getNumOfTotalRes(pGroupResInfo);
54,406✔
859

860
  for (int32_t i = pGroupResInfo->index; i < numOfRows; i += 1) {
2,527,069✔
861
    SResKeyPos* pPos = taosArrayGetP(pGroupResInfo->pRows, i);
2,490,792✔
862
    SFilePage*  page = getBufPage(pBuf, pPos->pos.pageId);
2,490,758✔
863
    if (page == NULL) {
2,490,485!
864
      qError("failed to get buffer, code:%s, %s", tstrerror(terrno), GET_TASKID(pTaskInfo));
×
865
      T_LONG_JMP(pTaskInfo->env, terrno);
×
866
    }
867

868
    SResultRow* pRow = (SResultRow*)((char*)page + pPos->pos.offset);
2,490,485✔
869

870
    doUpdateNumOfRows(pCtx, pRow, numOfExprs, rowEntryOffset);
2,490,485✔
871

872
    // no results, continue to check the next one
873
    if (pRow->numOfRows == 0) {
2,488,680✔
874
      pGroupResInfo->index += 1;
2✔
875
      releaseBufPage(pBuf, page);
2✔
876
      continue;
2✔
877
    }
878

879
    if (!ignoreGroup) {
2,488,678✔
880
      if (pBlock->info.id.groupId == 0) {
1,608,503✔
881
        pBlock->info.id.groupId = pPos->groupId;
1,544,202✔
882
      } else {
883
        // current value belongs to different group, it can't be packed into one datablock
884
        if (pBlock->info.id.groupId != pPos->groupId) {
64,301✔
885
          releaseBufPage(pBuf, page);
14,127✔
886
          break;
14,127✔
887
        }
888
      }
889
    }
890

891
    if (pBlock->info.rows + pRow->numOfRows > pBlock->info.capacity) {
2,474,551!
UNCOV
892
      uint32_t newSize = pBlock->info.rows + pRow->numOfRows + ((numOfRows - i) > 1 ? 1 : 0);
×
UNCOV
893
      code = blockDataEnsureCapacity(pBlock, newSize);
×
UNCOV
894
      QUERY_CHECK_CODE(code, lino, _end);
×
UNCOV
895
      qDebug("datablock capacity not sufficient, expand to required:%d, current capacity:%d, %s", newSize,
×
896
             pBlock->info.capacity, GET_TASKID(pTaskInfo));
897
      // todo set the pOperator->resultInfo size
898
    }
899

900
    pGroupResInfo->index += 1;
2,474,551✔
901
    code = copyResultrowToDataBlock(pExprInfo, numOfExprs, pRow, pCtx, pBlock, rowEntryOffset, pTaskInfo);
2,474,551✔
902
    releaseBufPage(pBuf, page);
2,476,951✔
903
    QUERY_CHECK_CODE(code, lino, _end);
2,476,653✔
904

905
    pBlock->info.rows += pRow->numOfRows;
2,476,616✔
906
    if (pBlock->info.rows >= threshold) {
2,476,616✔
907
      break;
3,955✔
908
    }
909
  }
910

911
  qDebug("%s result generated, rows:%" PRId64 ", groupId:%" PRIu64, GET_TASKID(pTaskInfo), pBlock->info.rows,
54,359✔
912
         pBlock->info.id.groupId);
913
  pBlock->info.dataLoad = 1;
54,360✔
914
  code = blockDataUpdateTsWindow(pBlock, 0);
54,360✔
915
  QUERY_CHECK_CODE(code, lino, _end);
54,367!
916

917
_end:
54,367✔
918
  if (code != TSDB_CODE_SUCCESS) {
54,404✔
919
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
37!
920
    T_LONG_JMP(pTaskInfo->env, code);
37!
921
  }
922
}
54,367✔
923

924
void doBuildResultDatablock(SOperatorInfo* pOperator, SOptrBasicInfo* pbInfo, SGroupResInfo* pGroupResInfo,
65,936✔
925
                            SDiskbasedBuf* pBuf) {
926
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
65,936✔
927
  SSDataBlock*   pBlock = pbInfo->pRes;
65,936✔
928

929
  // set output datablock version
930
  pBlock->info.version = pTaskInfo->version;
65,936✔
931

932
  blockDataCleanup(pBlock);
65,936✔
933
  if (!hasRemainResults(pGroupResInfo)) {
65,935✔
934
    return;
11,527✔
935
  }
936

937
  // clear the existed group id
938
  pBlock->info.id.groupId = 0;
54,407✔
939
  if (!pbInfo->mergeResultBlock) {
54,407✔
940
    doCopyToSDataBlock(pTaskInfo, pBlock, &pOperator->exprSupp, pBuf, pGroupResInfo, pOperator->resultInfo.threshold,
33,373✔
941
                       false);
942
  } else {
943
    while (hasRemainResults(pGroupResInfo)) {
40,351✔
944
      doCopyToSDataBlock(pTaskInfo, pBlock, &pOperator->exprSupp, pBuf, pGroupResInfo, pOperator->resultInfo.threshold,
21,034✔
945
                         true);
946
      if (pBlock->info.rows >= pOperator->resultInfo.threshold) {
21,030✔
947
        break;
1,713✔
948
      }
949

950
      // clearing group id to continue to merge data that belong to different groups
951
      pBlock->info.id.groupId = 0;
19,317✔
952
    }
953

954
    // clear the group id info in SSDataBlock, since the client does not need it
955
    pBlock->info.id.groupId = 0;
21,032✔
956
  }
957
}
958

959
void destroyExprInfo(SExprInfo* pExpr, int32_t numOfExprs) {
241,136✔
960
  for (int32_t i = 0; i < numOfExprs; ++i) {
1,713,205✔
961
    SExprInfo* pExprInfo = &pExpr[i];
1,472,040✔
962
    for (int32_t j = 0; j < pExprInfo->base.numOfParams; ++j) {
2,987,742✔
963
      if (pExprInfo->base.pParam[j].type == FUNC_PARAM_TYPE_COLUMN) {
1,516,357✔
964
        taosMemoryFreeClear(pExprInfo->base.pParam[j].pCol);
1,404,979✔
965
      } else if (pExprInfo->base.pParam[j].type == FUNC_PARAM_TYPE_VALUE) {
111,378✔
966
        taosVariantDestroy(&pExprInfo->base.pParam[j].param);
103,304✔
967
      }
968
    }
969

970
    taosMemoryFree(pExprInfo->base.pParam);
1,471,385✔
971
    taosMemoryFree(pExprInfo->pExpr);
1,471,301✔
972
  }
973
}
241,165✔
974

975
int32_t getBufferPgSize(int32_t rowSize, uint32_t* defaultPgsz, uint32_t* defaultBufsz) {
161,103✔
976
  *defaultPgsz = 4096;
161,103✔
977
  uint32_t last = *defaultPgsz;
161,103✔
978
  while (*defaultPgsz < rowSize * 4) {
223,769✔
979
    *defaultPgsz <<= 1u;
62,666✔
980
    if (*defaultPgsz < last) {
62,666!
981
      return TSDB_CODE_INVALID_PARA;
×
982
    }
983
    last = *defaultPgsz;
62,666✔
984
  }
985

986
  // The default buffer for each operator in query is 10MB.
987
  // at least four pages need to be in buffer
988
  // TODO: make this variable to be configurable.
989
  *defaultBufsz = 4096 * 2560;
161,103✔
990
  if ((*defaultBufsz) <= (*defaultPgsz)) {
161,103!
991
    (*defaultBufsz) = (*defaultPgsz) * 4;
×
992
    if (*defaultBufsz < ((int64_t)(*defaultPgsz)) * 4) {
×
993
      return TSDB_CODE_INVALID_PARA;
×
994
    }
995
  }
996

997
  return 0;
161,103✔
998
}
999

1000
void initResultSizeInfo(SResultInfo* pResultInfo, int32_t numOfRows) {
367,766✔
1001
  if (numOfRows == 0) {
367,766!
1002
    numOfRows = 4096;
×
1003
  }
1004

1005
  pResultInfo->capacity = numOfRows;
367,766✔
1006
  pResultInfo->threshold = numOfRows * 0.75;
367,766✔
1007

1008
  if (pResultInfo->threshold == 0) {
367,766✔
1009
    pResultInfo->threshold = numOfRows;
451✔
1010
  }
1011
}
367,766✔
1012

1013
void initBasicInfo(SOptrBasicInfo* pInfo, SSDataBlock* pBlock) {
157,423✔
1014
  pInfo->pRes = pBlock;
157,423✔
1015
  initResultRowInfo(&pInfo->resultRowInfo);
157,423✔
1016
}
157,420✔
1017

1018
static void destroySqlFunctionCtx(SqlFunctionCtx* pCtx, SExprInfo* pExpr, int32_t numOfOutput) {
720,878✔
1019
  if (pCtx == NULL) {
720,878✔
1020
    return;
457,294✔
1021
  }
1022

1023
  for (int32_t i = 0; i < numOfOutput; ++i) {
1,658,903✔
1024
    if (pExpr != NULL) {
1,395,289✔
1025
      SExprInfo* pExprInfo = &pExpr[i];
1,395,261✔
1026
      for (int32_t j = 0; j < pExprInfo->base.numOfParams; ++j) {
2,836,044✔
1027
        if (pExprInfo->base.pParam[j].type == FUNC_PARAM_TYPE_VALUE) {
1,440,757✔
1028
          taosMemoryFree(pCtx[i].input.pData[j]);
102,079✔
1029
          taosMemoryFree(pCtx[i].input.pColumnDataAgg[j]);
102,075✔
1030
        }
1031
      }
1032
    }
1033
    for (int32_t j = 0; j < pCtx[i].numOfParams; ++j) {
2,835,749✔
1034
      taosVariantDestroy(&pCtx[i].param[j].param);
1,440,440✔
1035
    }
1036

1037
    taosMemoryFreeClear(pCtx[i].subsidiaries.pCtx);
1,395,309✔
1038
    taosMemoryFreeClear(pCtx[i].subsidiaries.buf);
1,395,310✔
1039
    taosMemoryFree(pCtx[i].input.pData);
1,395,309✔
1040
    taosMemoryFree(pCtx[i].input.pColumnDataAgg);
1,395,450✔
1041

1042
    if (pCtx[i].udfName != NULL) {
1,395,325✔
1043
      taosMemoryFree(pCtx[i].udfName);
117✔
1044
    }
1045
  }
1046

1047
  taosMemoryFreeClear(pCtx);
263,614!
1048
  return;
263,615✔
1049
}
1050

1051
int32_t initExprSupp(SExprSupp* pSup, SExprInfo* pExprInfo, int32_t numOfExpr, SFunctionStateStore* pStore) {
222,837✔
1052
  pSup->pExprInfo = pExprInfo;
222,837✔
1053
  pSup->numOfExprs = numOfExpr;
222,837✔
1054
  if (pSup->pExprInfo != NULL) {
222,837✔
1055
    pSup->pCtx = createSqlFunctionCtx(pExprInfo, numOfExpr, &pSup->rowEntryInfoOffset, pStore);
184,238✔
1056
    if (pSup->pCtx == NULL) {
184,205✔
1057
      return TSDB_CODE_OUT_OF_MEMORY;
142✔
1058
    }
1059
  }
1060

1061
  return TSDB_CODE_SUCCESS;
222,662✔
1062
}
1063

1064
void cleanupExprSupp(SExprSupp* pSupp) {
720,882✔
1065
  destroySqlFunctionCtx(pSupp->pCtx, pSupp->pExprInfo, pSupp->numOfExprs);
720,882✔
1066
  if (pSupp->pExprInfo != NULL) {
720,855✔
1067
    destroyExprInfo(pSupp->pExprInfo, pSupp->numOfExprs);
239,264✔
1068
    taosMemoryFreeClear(pSupp->pExprInfo);
239,280!
1069
  }
1070

1071
  if (pSupp->pFilterInfo != NULL) {
720,872✔
1072
    filterFreeInfo(pSupp->pFilterInfo);
74,453✔
1073
    pSupp->pFilterInfo = NULL;
74,458✔
1074
  }
1075

1076
  taosMemoryFree(pSupp->rowEntryInfoOffset);
720,877✔
1077
}
720,828✔
1078

1079
void cleanupBasicInfo(SOptrBasicInfo* pInfo) {
163,008✔
1080
  blockDataDestroy(pInfo->pRes);
163,008✔
1081
  pInfo->pRes = NULL;
163,043✔
1082
}
163,043✔
1083

1084
bool groupbyTbname(SNodeList* pGroupList) {
116,814✔
1085
  bool bytbname = false;
116,814✔
1086
  if (LIST_LENGTH(pGroupList) == 1) {
116,814✔
1087
    SNode* p = nodesListGetNode(pGroupList, 0);
8,308✔
1088
    if (!p) {
8,306!
UNCOV
1089
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1090
      return false;
×
1091
    }
1092
    if (p->type == QUERY_NODE_FUNCTION) {
8,314✔
1093
      // partition by tbname/group by tbname
1094
      bytbname = (strcmp(((struct SFunctionNode*)p)->functionName, "tbname") == 0);
6,713✔
1095
    }
1096
  }
1097

1098
  return bytbname;
116,820✔
1099
}
1100

1101
int32_t createDataSinkParam(SDataSinkNode* pNode, void** pParam, SExecTaskInfo* pTask, SReadHandle* readHandle) {
213,298✔
1102
  switch (pNode->type) {
213,298✔
1103
    case QUERY_NODE_PHYSICAL_PLAN_QUERY_INSERT: {
19✔
1104
      SInserterParam* pInserterParam = taosMemoryCalloc(1, sizeof(SInserterParam));
19✔
1105
      if (NULL == pInserterParam) {
19!
1106
        return terrno;
×
1107
      }
1108
      pInserterParam->readHandle = readHandle;
19✔
1109

1110
      *pParam = pInserterParam;
19✔
1111
      break;
19✔
1112
    }
1113
    case QUERY_NODE_PHYSICAL_PLAN_DELETE: {
1,521✔
1114
      SDeleterParam* pDeleterParam = taosMemoryCalloc(1, sizeof(SDeleterParam));
1,521✔
1115
      if (NULL == pDeleterParam) {
1,521!
1116
        return terrno;
1✔
1117
      }
1118

1119
      SArray* pInfoList = NULL;
1,521✔
1120
      int32_t code = getTableListInfo(pTask, &pInfoList);
1,521✔
1121
      if (code != TSDB_CODE_SUCCESS || pInfoList == NULL) {
1,521!
1122
        taosMemoryFree(pDeleterParam);
×
1123
        return code;
×
1124
      }
1125

1126
      STableListInfo* pTableListInfo = taosArrayGetP(pInfoList, 0);
1,521✔
1127
      taosArrayDestroy(pInfoList);
1,521✔
1128

1129
      pDeleterParam->suid = tableListGetSuid(pTableListInfo);
1,521✔
1130

1131
      // TODO extract uid list
1132
      int32_t numOfTables = 0;
1,520✔
1133
      code = tableListGetSize(pTableListInfo, &numOfTables);
1,520✔
1134
      if (code != TSDB_CODE_SUCCESS) {
1,520!
1135
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1136
        taosMemoryFree(pDeleterParam);
×
1137
        return code;
×
1138
      }
1139

1140
      pDeleterParam->pUidList = taosArrayInit(numOfTables, sizeof(uint64_t));
1,520✔
1141
      if (NULL == pDeleterParam->pUidList) {
1,521✔
1142
        taosMemoryFree(pDeleterParam);
1✔
1143
        return terrno;
1✔
1144
      }
1145

1146
      for (int32_t i = 0; i < numOfTables; ++i) {
3,299✔
1147
        STableKeyInfo* pTable = tableListGetInfo(pTableListInfo, i);
1,779✔
1148
        if (!pTable) {
1,779!
1149
          taosArrayDestroy(pDeleterParam->pUidList);
×
1150
          taosMemoryFree(pDeleterParam);
×
1151
          return TSDB_CODE_OUT_OF_MEMORY;
×
1152
        }
1153
        void*          tmp = taosArrayPush(pDeleterParam->pUidList, &pTable->uid);
1,779✔
1154
        if (!tmp) {
1,779!
1155
          taosArrayDestroy(pDeleterParam->pUidList);
×
1156
          taosMemoryFree(pDeleterParam);
×
1157
          return terrno;
×
1158
        }
1159
      }
1160

1161
      *pParam = pDeleterParam;
1,520✔
1162
      break;
1,520✔
1163
    }
1164
    default:
211,758✔
1165
      break;
211,758✔
1166
  }
1167

1168
  return TSDB_CODE_SUCCESS;
213,297✔
1169
}
1170

1171
void streamOpReleaseState(SOperatorInfo* pOperator) {
156✔
1172
  SOperatorInfo* downstream = pOperator->pDownstream[0];
156✔
1173
  if (downstream->fpSet.releaseStreamStateFn) {
156!
1174
    downstream->fpSet.releaseStreamStateFn(downstream);
156✔
1175
  }
1176
}
155✔
1177

1178
void streamOpReloadState(SOperatorInfo* pOperator) {
156✔
1179
  SOperatorInfo* downstream = pOperator->pDownstream[0];
156✔
1180
  if (downstream->fpSet.reloadStreamStateFn) {
156!
1181
    downstream->fpSet.reloadStreamStateFn(downstream);
156✔
1182
  }
1183
}
156✔
1184

1185
void freeOperatorParamImpl(SOperatorParam* pParam, SOperatorParamType type) {
5,738✔
1186
  int32_t childrenNum = taosArrayGetSize(pParam->pChildren);
5,738✔
1187
  for (int32_t i = 0; i < childrenNum; ++i) {
5,743✔
1188
    SOperatorParam* pChild = taosArrayGetP(pParam->pChildren, i);
5✔
1189
    freeOperatorParam(pChild, type);
5✔
1190
  }
1191

1192
  taosArrayDestroy(pParam->pChildren);
5,738✔
1193

1194
  taosMemoryFree(pParam->value);
5,738✔
1195

1196
  taosMemoryFree(pParam);
5,738✔
1197
}
5,738✔
1198

1199
void freeExchangeGetBasicOperatorParam(void* pParam) {
318✔
1200
  SExchangeOperatorBasicParam* pBasic = (SExchangeOperatorBasicParam*)pParam;
318✔
1201
  taosArrayDestroy(pBasic->uidList);
318✔
1202
}
318✔
1203

1204
void freeExchangeGetOperatorParam(SOperatorParam* pParam) {
184✔
1205
  SExchangeOperatorParam* pExcParam = (SExchangeOperatorParam*)pParam->value;
184✔
1206
  if (pExcParam->multiParams) {
184✔
1207
    SExchangeOperatorBatchParam* pExcBatch = (SExchangeOperatorBatchParam*)pParam->value;
172✔
1208
    tSimpleHashCleanup(pExcBatch->pBatchs);
172✔
1209
  } else {
1210
    freeExchangeGetBasicOperatorParam(&pExcParam->basic);
12✔
1211
  }
1212

1213
  freeOperatorParamImpl(pParam, OP_GET_PARAM);
184✔
1214
}
184✔
1215

1216
void freeExchangeNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1217

1218
void freeGroupCacheGetOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_GET_PARAM); }
3,367✔
1219

1220
void freeGroupCacheNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1221

1222
void freeMergeJoinGetOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_GET_PARAM); }
1,682✔
1223

1224
void freeMergeJoinNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1225

1226
void freeTableScanGetOperatorParam(SOperatorParam* pParam) {
505✔
1227
  STableScanOperatorParam* pTableScanParam = (STableScanOperatorParam*)pParam->value;
505✔
1228
  taosArrayDestroy(pTableScanParam->pUidList);
505✔
1229
  freeOperatorParamImpl(pParam, OP_GET_PARAM);
505✔
1230
}
505✔
1231

1232
void freeTableScanNotifyOperatorParam(SOperatorParam* pParam) { freeOperatorParamImpl(pParam, OP_NOTIFY_PARAM); }
×
1233

1234
void freeOperatorParam(SOperatorParam* pParam, SOperatorParamType type) {
152,996✔
1235
  if (NULL == pParam) {
152,996✔
1236
    return;
147,255✔
1237
  }
1238

1239
  switch (pParam->opType) {
5,741✔
1240
    case QUERY_NODE_PHYSICAL_PLAN_EXCHANGE:
184✔
1241
      type == OP_GET_PARAM ? freeExchangeGetOperatorParam(pParam) : freeExchangeNotifyOperatorParam(pParam);
184!
1242
      break;
184✔
1243
    case QUERY_NODE_PHYSICAL_PLAN_GROUP_CACHE:
3,367✔
1244
      type == OP_GET_PARAM ? freeGroupCacheGetOperatorParam(pParam) : freeGroupCacheNotifyOperatorParam(pParam);
3,367!
1245
      break;
3,367✔
1246
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_JOIN:
1,682✔
1247
      type == OP_GET_PARAM ? freeMergeJoinGetOperatorParam(pParam) : freeMergeJoinNotifyOperatorParam(pParam);
1,682!
1248
      break;
1,682✔
1249
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN:
505✔
1250
      type == OP_GET_PARAM ? freeTableScanGetOperatorParam(pParam) : freeTableScanNotifyOperatorParam(pParam);
505!
1251
      break;
505✔
1252
    default:
3✔
1253
      qError("unsupported op %d param, type %d", pParam->opType, type);
3!
1254
      break;
3✔
1255
  }
1256
}
1257

1258
void freeResetOperatorParams(struct SOperatorInfo* pOperator, SOperatorParamType type, bool allFree) {
974,042✔
1259
  SOperatorParam**  ppParam = NULL;
974,042✔
1260
  SOperatorParam*** pppDownstramParam = NULL;
974,042✔
1261
  switch (type) {
974,042!
1262
    case OP_GET_PARAM:
488,329✔
1263
      ppParam = &pOperator->pOperatorGetParam;
488,329✔
1264
      pppDownstramParam = &pOperator->pDownstreamGetParams;
488,329✔
1265
      break;
488,329✔
1266
    case OP_NOTIFY_PARAM:
486,320✔
1267
      ppParam = &pOperator->pOperatorNotifyParam;
486,320✔
1268
      pppDownstramParam = &pOperator->pDownstreamNotifyParams;
486,320✔
1269
      break;
486,320✔
1270
    default:
×
1271
      return;
×
1272
  }
1273

1274
  if (*ppParam) {
974,649✔
1275
    freeOperatorParam(*ppParam, type);
1,682✔
1276
    *ppParam = NULL;
1,682✔
1277
  }
1278

1279
  if (*pppDownstramParam) {
974,649✔
1280
    for (int32_t i = 0; i < pOperator->numOfDownstream; ++i) {
5,422✔
1281
      if ((*pppDownstramParam)[i]) {
3,364✔
1282
        freeOperatorParam((*pppDownstramParam)[i], type);
24✔
1283
        (*pppDownstramParam)[i] = NULL;
24✔
1284
      }
1285
    }
1286
    if (allFree) {
2,058✔
1287
      taosMemoryFreeClear(*pppDownstramParam);
459!
1288
    }
1289
  }
1290
}
1291

1292
FORCE_INLINE int32_t getNextBlockFromDownstreamImpl(struct SOperatorInfo* pOperator, int32_t idx, bool clearParam,
595,800✔
1293
                                                    SSDataBlock** pResBlock) {
1294
  QRY_PARAM_CHECK(pResBlock);
595,800!
1295

1296
  int32_t code = 0;
595,800✔
1297
  if (pOperator->pDownstreamGetParams && pOperator->pDownstreamGetParams[idx]) {
595,800!
1298
    qDebug("DynOp: op %s start to get block from downstream %s", pOperator->name, pOperator->pDownstream[idx]->name);
5,286!
1299
    code = pOperator->pDownstream[idx]->fpSet.getNextExtFn(pOperator->pDownstream[idx],
5,286✔
1300
                                                           pOperator->pDownstreamGetParams[idx], pResBlock);
5,286✔
1301
    if (clearParam && (code == 0)) {
5,274!
1302
      freeOperatorParam(pOperator->pDownstreamGetParams[idx], OP_GET_PARAM);
×
1303
      pOperator->pDownstreamGetParams[idx] = NULL;
×
1304
    }
1305

1306
    if (code) {
5,274!
1307
      qError("failed to get next data block from upstream at %s, line:%d code:%s", __func__, __LINE__, tstrerror(code));
×
1308
    }
1309
    return code;
5,274✔
1310
  }
1311

1312
  code = pOperator->pDownstream[idx]->fpSet.getNextFn(pOperator->pDownstream[idx], pResBlock);
590,514✔
1313
  if (code) {
587,683!
1314
    qError("failed to get next data block from upstream at %s, %d code:%s", __func__, __LINE__, tstrerror(code));
×
1315
  }
1316
  return code;
587,683✔
1317
}
1318

1319
bool compareVal(const char* v, const SStateKeys* pKey) {
29,422✔
1320
  if (IS_VAR_DATA_TYPE(pKey->type)) {
29,422!
1321
    if (varDataLen(v) != varDataLen(pKey->pData)) {
×
1322
      return false;
×
1323
    } else {
1324
      return memcmp(varDataVal(v), varDataVal(pKey->pData), varDataLen(v)) == 0;
×
1325
    }
1326
  } else {
1327
    return memcmp(pKey->pData, v, pKey->bytes) == 0;
29,422✔
1328
  }
1329
}
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