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

taosdata / TDengine / #4935

22 Jan 2026 06:38AM UTC coverage: 66.708% (+0.02%) from 66.691%
#4935

push

travis-ci

web-flow
merge: from main to 3.0 #34371

121 of 271 new or added lines in 17 files covered. (44.65%)

9066 existing lines in 149 files now uncovered.

203884 of 305637 relevant lines covered (66.71%)

125811266.68 hits per line

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

76.68
/source/libs/function/src/functionMgt.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 "functionMgt.h"
17

18
#include "builtins.h"
19
#include "builtinsimpl.h"
20
#include "functionMgtInt.h"
21
#include "nodes.h"
22
#include "querynodes.h"
23
#include "taos.h"
24
#include "taoserror.h"
25
#include "thash.h"
26
#include "tudf.h"
27

28
typedef struct SFuncMgtService {
29
  SHashObj* pFuncNameHashTable;
30
} SFuncMgtService;
31

32
static SFuncMgtService gFunMgtService;
33
static TdThreadOnce    functionHashTableInit = PTHREAD_ONCE_INIT;
34
static int32_t         initFunctionCode = 0;
35

36
static void doInitFunctionTable() {
1,240,017✔
37
  gFunMgtService.pFuncNameHashTable =
1,240,017✔
38
      taosHashInit(funcMgtBuiltinsNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
1,240,017✔
39
  if (NULL == gFunMgtService.pFuncNameHashTable) {
1,240,017✔
UNCOV
40
    initFunctionCode = terrno;
×
UNCOV
41
    return;
×
42
  }
43

44
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
279,003,825✔
45
    if (TSDB_CODE_SUCCESS != taosHashPut(gFunMgtService.pFuncNameHashTable, funcMgtBuiltins[i].name,
277,763,808✔
46
                                         strlen(funcMgtBuiltins[i].name), &i, sizeof(int32_t))) {
277,763,808✔
UNCOV
47
      initFunctionCode = terrno;
×
UNCOV
48
      return;
×
49
    }
50
  }
51
}
52

53
static bool isSpecificClassifyFunc(int32_t funcId, uint64_t classification) {
2,147,483,647✔
54
  if (fmIsUserDefinedFunc(funcId)) {
2,147,483,647✔
55
    return FUNC_MGT_AGG_FUNC == classification
56
               ? FUNC_AGGREGATE_UDF_ID == funcId
57
               : (FUNC_MGT_SCALAR_FUNC == classification ? FUNC_SCALAR_UDF_ID == funcId : false);
16,271,935✔
58
  }
59
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,147,483,647✔
60
    return false;
2,147,483,647✔
61
  }
62
  return FUNC_MGT_TEST_MASK(funcMgtBuiltins[funcId].classification, classification);
2,147,483,647✔
63
}
64

65
int32_t fmFuncMgtInit() {
1,240,017✔
66
  (void)taosThreadOnce(&functionHashTableInit, doInitFunctionTable);
1,240,017✔
67
  return initFunctionCode;
1,240,017✔
68
}
69

70
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
622,213,926✔
71
  if (NULL != gFunMgtService.pFuncNameHashTable) {
622,213,926✔
72
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
622,050,729✔
73
    if (NULL != pVal) {
622,050,475✔
74
      pFunc->funcId = *(int32_t*)pVal;
621,672,578✔
75
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
621,672,587✔
76
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
621,670,336✔
77
    }
78
    return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
377,897✔
79
  }
80
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
3,026,295✔
81
    if (0 == strcmp(funcMgtBuiltins[i].name, pFunc->functionName)) {
3,025,824✔
82
      pFunc->funcId = i;
162,726✔
83
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
162,726✔
84
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
162,726✔
85
    }
86
  }
87
  return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
471✔
88
}
89

90
EFuncReturnRows fmGetFuncReturnRows(SFunctionNode* pFunc) {
15,879,951✔
91
  if (NULL != funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc) {
15,879,951✔
92
    return funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc(pFunc);
11,247,483✔
93
  }
94
  return (fmIsIndefiniteRowsFunc(pFunc->funcId) || fmIsMultiRowsFunc(pFunc->funcId)) ? FUNC_RETURN_ROWS_INDEFINITE
4,632,468✔
95
                                                                                     : FUNC_RETURN_ROWS_NORMAL;
4,632,468✔
96
}
97

98
bool fmIsBuiltinFunc(const char* pFunc) {
32,560✔
99
  return NULL != taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
32,560✔
100
}
101

102
EFunctionType fmGetFuncType(const char* pFunc) {
482,446,008✔
103
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
482,446,008✔
104
  if (NULL != pVal) {
482,445,328✔
105
    return funcMgtBuiltins[*(int32_t*)pVal].type;
482,056,379✔
106
  }
107
  return FUNCTION_TYPE_UDF;
388,949✔
108
}
109

110
EFunctionType fmGetFuncTypeFromId(int32_t funcId) {
2,824,144✔
111
  if (funcId < funcMgtBuiltinsNum) {
2,824,144✔
112
    return funcMgtBuiltins[funcId].type;
2,824,144✔
113
  }
UNCOV
114
  return FUNCTION_TYPE_UDF;
×
115
}
116

117
EFuncDataRequired fmFuncDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
30,215,534✔
118
  if (fmIsUserDefinedFunc(pFunc->funcId) || pFunc->funcId < 0 || pFunc->funcId >= funcMgtBuiltinsNum) {
30,215,534✔
UNCOV
119
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
120
  }
121
  if (NULL == funcMgtBuiltins[pFunc->funcId].dataRequiredFunc) {
30,215,459✔
UNCOV
122
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
123
  }
124
  return funcMgtBuiltins[pFunc->funcId].dataRequiredFunc(pFunc, pTimeWindow);
30,215,415✔
125
}
126

127
EFuncDataRequired fmFuncDynDataRequired(int32_t funcId, void* pRes, SDataBlockInfo* pBlockInfo) {
2,147,483,647✔
128
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,147,483,647✔
129
    return FUNC_DATA_REQUIRED_DATA_LOAD;
16✔
130
  }
131

132
  const char* name = funcMgtBuiltins[funcId].name;
2,147,483,647✔
133
  if ((strcmp(name, "_group_key") == 0) || (strcmp(name, "_select_value") == 0)) {
2,147,483,647✔
134
    return FUNC_DATA_REQUIRED_NOT_LOAD;
577,244✔
135
    ;
136
  }
137

138
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
2,147,483,647✔
139
    return FUNC_DATA_REQUIRED_DATA_LOAD;
38,423,538✔
140
  } else {
141
    return funcMgtBuiltins[funcId].dynDataRequiredFunc(pRes, pBlockInfo);
2,147,483,647✔
142
  }
143
}
144

145
int32_t fmGetFuncExecFuncs(int32_t funcId, SFuncExecFuncs* pFpSet) {
302,921,493✔
146
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
302,921,493✔
147
    return TSDB_CODE_FAILED;
8,999✔
148
  }
149
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
302,887,739✔
150
  pFpSet->init = funcMgtBuiltins[funcId].initFunc;
302,765,999✔
151
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
302,819,438✔
152
  pFpSet->finalize = funcMgtBuiltins[funcId].finalizeFunc;
302,744,106✔
153
  pFpSet->combine = funcMgtBuiltins[funcId].combineFunc;
302,704,422✔
154
  pFpSet->processFuncByRow = funcMgtBuiltins[funcId].processFuncByRow;
302,858,088✔
155
  pFpSet->cleanup = funcMgtBuiltins[funcId].cleanupFunc;
302,752,566✔
156
  return TSDB_CODE_SUCCESS;
302,832,572✔
157
}
158

159
int32_t fmGetUdafExecFuncs(int32_t funcId, SFuncExecFuncs* pFpSet) {
96,967✔
160
#ifdef USE_UDF
161
  if (!fmIsUserDefinedFunc(funcId)) {
96,967✔
UNCOV
162
    return TSDB_CODE_FAILED;
×
163
  }
164
  pFpSet->getEnv = udfAggGetEnv;
96,967✔
165
  pFpSet->init = udfAggInit;
96,967✔
166
  pFpSet->process = udfAggProcess;
96,967✔
167
  pFpSet->finalize = udfAggFinalize;
96,967✔
168
  return TSDB_CODE_SUCCESS;
96,967✔
169
#else
170
  TAOS_RETURN(TSDB_CODE_OPS_NOT_SUPPORT);
171
#endif
172
}
173

174
int32_t fmGetScalarFuncExecFuncs(int32_t funcId, SScalarFuncExecFuncs* pFpSet) {
858,760,881✔
175
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
858,760,881✔
176
    return TSDB_CODE_FAILED;
150,968✔
177
  }
178
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
859,002,873✔
179
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
858,783,508✔
180
  return TSDB_CODE_SUCCESS;
858,825,734✔
181
}
182

183
bool fmIsAggFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_AGG_FUNC); }
1,757,227,001✔
184

185
bool fmIsScalarFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCALAR_FUNC); }
2,147,483,647✔
186

187
bool fmIsVectorFunc(int32_t funcId) { return !fmIsScalarFunc(funcId) && !fmIsPseudoColumnFunc(funcId); }
959,025,764✔
188

189
bool fmIsSelectColsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_COLS_FUNC); }
94,316,159✔
190

191
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
1,608,434,476✔
192

193
bool fmIsTimelineFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TIMELINE_FUNC); }
856,589,777✔
194

195
bool fmIsDateTimeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_DATETIME_FUNC); }
434,421,010✔
196

197
bool fmIsPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PSEUDO_COLUMN_FUNC); }
1,646,965,041✔
198

199
bool fmIsScanPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCAN_PC_FUNC); }
1,846,093,722✔
200

201
bool fmIsWindowPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_WINDOW_PC_FUNC); }
1,140,388,403✔
202

203
bool fmIsWindowClauseFunc(int32_t funcId) { return fmIsAggFunc(funcId) || fmIsWindowPseudoColumnFunc(funcId); }
42,067,444✔
204

205
bool fmIsStreamWindowClauseFunc(int32_t funcId) { return fmIsWindowClauseFunc(funcId) || fmIsPlaceHolderFunc(funcId); }
273,987✔
206

207
bool fmIsStreamVectorFunc(int32_t funcId) { return fmIsVectorFunc(funcId) || fmIsPlaceHolderFunc(funcId); }
9,350✔
208

209
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
1,210,771,147✔
210

211
bool fmIsSpecialDataRequiredFunc(int32_t funcId) {
72,069,426✔
212
  return isSpecificClassifyFunc(funcId, FUNC_MGT_SPECIAL_DATA_REQUIRED);
72,069,426✔
213
}
214

215
bool fmIsDynamicScanOptimizedFunc(int32_t funcId) {
35,347,736✔
216
  return isSpecificClassifyFunc(funcId, FUNC_MGT_DYNAMIC_SCAN_OPTIMIZED);
35,347,736✔
217
}
218

219
bool fmIsMultiResFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_RES_FUNC); }
901,105,177✔
220

221
bool fmIsRepeatScanFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_REPEAT_SCAN_FUNC); }
868,144,342✔
222

223
bool fmIsUserDefinedFunc(int32_t funcId) { return funcId > FUNC_UDF_ID_START; }
2,147,483,647✔
224

225
bool fmIsForbidFillFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_FILL_FUNC); }
434,648,123✔
226

227
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
59,911,181✔
228

229
bool fmIsSystemInfoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SYSTEM_INFO_FUNC); }
437,313,366✔
230

231
bool fmIsImplicitTsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IMPLICIT_TS_FUNC); }
2,147,483,647✔
232

233
bool fmIsClientPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CLIENT_PC_FUNC); }
437,086,291✔
234

235
bool fmIsMultiRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_ROWS_FUNC); }
865,231,920✔
236

237
static bool fmIsKeepOrderFuncId(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_KEEP_ORDER_FUNC); }
486,799,621✔
238

239
bool fmIsKeepOrderFunc(SFunctionNode* pFunc) {
492,157,081✔
240
  if (pFunc->funcType == FUNCTION_TYPE_TOP || pFunc->funcType == FUNCTION_TYPE_BOTTOM ||
492,157,081✔
241
      pFunc->funcType == FUNCTION_TYPE_SAMPLE) {
488,895,856✔
242
    if (pFunc->pParameterList != NULL && pFunc->pParameterList->length >= 2) {
5,363,379✔
243
      SNode* pParam = nodesListGetNode(pFunc->pParameterList, 1);
5,359,873✔
244
      if (pParam != NULL && nodeType(pParam) == QUERY_NODE_VALUE) {
5,359,873✔
245
        SValueNode* pConst = (SValueNode*)pParam;
5,359,873✔
246
        if (pConst->datum.i == 1) {
5,359,873✔
247
          return true;
594,453✔
248
        }
249
      }
250
    }
251
    return false;
4,765,420✔
252
  }
253
  return fmIsKeepOrderFuncId(pFunc->funcId);
486,797,922✔
254
}
255

256
bool fmIsCumulativeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CUMULATIVE_FUNC); }
96,434,711✔
257

258
bool fmIsForbidSysTableFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_SYSTABLE_FUNC); }
434,592,640✔
259

260
bool fmIsInterpFunc(int32_t funcId) {
885,186,845✔
261
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
885,186,845✔
262
    return false;
726,684✔
263
  }
264
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
884,460,379✔
265
}
266

267
bool fmIsInterpPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERP_PC_FUNC); }
874,755,618✔
268

269
bool fmIsForecastFunc(int32_t funcId) {
851,470,203✔
270
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
851,470,203✔
271
    return false;
729,095✔
272
  }
273
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
850,741,112✔
274
}
275

276
bool fmIsAnalysisPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_ANALYTICS_PC_FUNC); }
877,461,216✔
277

278
bool fmIsImputationCorrelationFunc(int32_t funcId) {
×
UNCOV
279
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
280
    return false;
×
281
  }
282

UNCOV
283
  int32_t type = funcMgtBuiltins[funcId].type;
×
UNCOV
284
  return FUNCTION_TYPE_IMPUTATION == type || FUNCTION_TYPE_DTW == type || FUNCTION_TYPE_DTW_PATH == type ||
×
285
         FUNCTION_TYPE_TLCC == type;
286
}
287

288
bool fmIsLastRowFunc(int32_t funcId) {
53,415,026✔
289
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
53,415,026✔
290
    return false;
57✔
291
  }
292
  return FUNCTION_TYPE_LAST_ROW == funcMgtBuiltins[funcId].type;
53,414,969✔
293
}
294

UNCOV
295
bool fmIsLastFunc(int32_t funcId) {
×
UNCOV
296
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
UNCOV
297
    return false;
×
298
  }
UNCOV
299
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type;
×
300
}
301

302
bool fmIsNotNullOutputFunc(int32_t funcId) {
447,664,627✔
303
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
447,664,627✔
304
    return false;
170,789✔
305
  }
306
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
463,231,221✔
307
         FUNCTION_TYPE_CACHE_LAST == funcMgtBuiltins[funcId].type ||
429,033,449✔
308
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
428,604,235✔
309
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
413,639,857✔
310
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
408,586,766✔
311
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
395,447,930✔
312
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
387,222,337✔
313
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
384,619,018✔
314
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
334,131,224✔
315
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
1,206,932,795✔
316
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
330,200,507✔
317
}
318

319
bool fmIsSelectValueFunc(int32_t funcId) {
86,315,373✔
320
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
86,315,373✔
UNCOV
321
    return false;
×
322
  }
323
  return FUNCTION_TYPE_SELECT_VALUE == funcMgtBuiltins[funcId].type;
86,315,373✔
324
}
325

326
bool fmIsGroupKeyFunc(int32_t funcId) {
226,619,186✔
327
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
226,619,186✔
UNCOV
328
    return false;
×
329
  }
330
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
226,621,447✔
331
}
332

333
bool fmisSelectGroupConstValueFunc(int32_t funcId) {
10,718,988✔
334
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
10,718,988✔
UNCOV
335
    return false;
×
336
  }
337
  return FUNCTION_TYPE_GROUP_CONST_VALUE == funcMgtBuiltins[funcId].type;
10,718,988✔
338
}
339

340
bool fmIsElapsedFunc(int32_t funcId) {
9,833,119✔
341
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
9,833,119✔
UNCOV
342
    return false;
×
343
  }
344
  return FUNCTION_TYPE_ELAPSED == funcMgtBuiltins[funcId].type;
9,833,119✔
345
}
346

347
bool fmIsBlockDistFunc(int32_t funcId) {
434,421,754✔
348
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
434,421,754✔
349
    return false;
364,740✔
350
  }
351
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
434,057,014✔
352
}
353

354
bool fmIsDBUsageFunc(int32_t funcId) {
434,421,460✔
355
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
434,421,460✔
356
    return false;
364,738✔
357
  }
358
  return FUNCTION_TYPE_DB_USAGE == funcMgtBuiltins[funcId].type;
434,056,722✔
359
}
360

361
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
475,024,855✔
362

363
bool fmIsIgnoreNullFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IGNORE_NULL_FUNC); }
245,138✔
364

365
void fmFuncMgtDestroy() {
1,240,063✔
366
  void* m = gFunMgtService.pFuncNameHashTable;
1,240,063✔
367
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
1,240,063✔
368
    taosHashCleanup(m);
1,240,017✔
369
  }
370
}
1,240,063✔
371

372
#ifdef BUILD_NO_CALL
373
int32_t fmSetNormalFunc(int32_t funcId, SFuncExecFuncs* pFpSet) {
374
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
375
    return TSDB_CODE_FAILED;
376
  }
377
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
378
  return TSDB_CODE_SUCCESS;
379
}
380
#endif
381

382
// function has same input/output type
383
bool fmIsSameInOutType(int32_t funcId) {
36,199,277✔
384
  bool res = false;
36,199,277✔
385
  switch (funcMgtBuiltins[funcId].type) {
36,199,277✔
386
    case FUNCTION_TYPE_MAX:
13,856,619✔
387
    case FUNCTION_TYPE_MIN:
388
    case FUNCTION_TYPE_TOP:
389
    case FUNCTION_TYPE_BOTTOM:
390
    case FUNCTION_TYPE_FIRST:
391
    case FUNCTION_TYPE_LAST:
392
    case FUNCTION_TYPE_SAMPLE:
393
    case FUNCTION_TYPE_TAIL:
394
    case FUNCTION_TYPE_UNIQUE:
395
      res = true;
13,856,619✔
396
      break;
13,856,619✔
397
    default:
22,342,658✔
398
      break;
22,342,658✔
399
  }
400
  return res;
36,199,277✔
401
}
402

403
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
11,500,377✔
404
  SNode* pNode;
405
  FOREACH(pNode, pFunc->pParameterList) {
11,578,533✔
406
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
204,647✔
407
      return false;
126,491✔
408
    }
409
  }
410
  return true;
11,373,886✔
411
}
412

413
bool fmIsSkipScanCheckFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SKIP_SCAN_CHECK_FUNC); }
35,347,754✔
414

415
bool fmIsPrimaryKeyFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PRIMARY_KEY_FUNC); }
417,210,531✔
416

417
bool fmIsPlaceHolderFunc(int32_t funcId) {return isSpecificClassifyFunc(funcId, FUNC_MGT_PLACE_HOLDER_FUNC); }
2,147,483,647✔
418

UNCOV
419
void getLastCacheDataType(SDataType* pType, int32_t pkBytes) {
×
420
  // TODO: do it later.
421
  pType->bytes = getFirstLastInfoSize(pType->bytes, pkBytes) + VARSTR_HEADER_SIZE;
×
422
  pType->type = TSDB_DATA_TYPE_BINARY;
×
423
}
×
424

425
static int32_t getFuncInfo(SFunctionNode* pFunc) {
117,155,646✔
426
  char msg[128] = {0};
117,155,646✔
427
  return fmGetFuncInfo(pFunc, msg, sizeof(msg));
117,155,646✔
428
}
429

430
int32_t createFunction(const char* pName, SNodeList* pParameterList, SFunctionNode** ppFunc) {
8,557,778✔
431
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
8,557,778✔
432
  if (NULL == *ppFunc) {
8,557,778✔
UNCOV
433
    return code;
×
434
  }
435
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
8,557,778✔
436
  (*ppFunc)->pParameterList = pParameterList;
8,557,778✔
437
  code = getFuncInfo((*ppFunc));
8,557,778✔
438
  if (TSDB_CODE_SUCCESS != code) {
8,557,778✔
UNCOV
439
    (*ppFunc)->pParameterList = NULL;
×
UNCOV
440
    nodesDestroyNode((SNode*)*ppFunc);
×
UNCOV
441
    *ppFunc = NULL;
×
UNCOV
442
    return code;
×
443
  }
444
  return code;
8,557,778✔
445
}
446

447
static void resetOutputChangedFunc(SFunctionNode *pFunc, const SFunctionNode* pSrcFunc) {
108,597,864✔
448
  if (funcMgtBuiltins[pFunc->funcId].type == FUNCTION_TYPE_LAST_MERGE) {
108,597,864✔
449
    pFunc->node.resType = pSrcFunc->node.resType;
16,997,614✔
450
    return;
16,997,569✔
451
  }
452
}
453

454
int32_t createFunctionWithSrcFunc(const char* pName, const SFunctionNode* pSrcFunc, SNodeList* pParameterList, SFunctionNode** ppFunc) {
108,597,827✔
455
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
108,597,827✔
456
  if (NULL == *ppFunc) {
108,597,962✔
UNCOV
457
    return code;
×
458
  }
459

460
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
108,597,962✔
461
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
108,597,872✔
462
  (*ppFunc)->pSrcFuncRef = pSrcFunc;
108,597,827✔
463

464
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
108,597,823✔
465
  (*ppFunc)->pParameterList = pParameterList;
108,597,872✔
466
  code = getFuncInfo((*ppFunc));
108,597,966✔
467
  if (TSDB_CODE_SUCCESS != code) {
108,597,819✔
UNCOV
468
    (*ppFunc)->pParameterList = NULL;
×
UNCOV
469
    nodesDestroyNode((SNode*)*ppFunc);
×
UNCOV
470
    *ppFunc = NULL;
×
UNCOV
471
    return code;
×
472
  }
473
  resetOutputChangedFunc(*ppFunc, pSrcFunc);
108,597,819✔
474
  (*ppFunc)->node.relatedTo = pSrcFunc->node.relatedTo;
108,597,770✔
475
  (*ppFunc)->node.bindExprID = pSrcFunc->node.bindExprID;
108,597,860✔
476
  return code;
108,597,876✔
477
}
478

479
static int32_t createColumnByFunc(const SFunctionNode* pFunc, SColumnNode** ppCol) {
72,398,505✔
480
  int32_t code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)ppCol);
72,398,505✔
481
  if (NULL == *ppCol) {
72,398,640✔
UNCOV
482
    return code;
×
483
  }
484
  tstrncpy((*ppCol)->colName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
72,398,640✔
485
  (*ppCol)->node.resType = pFunc->node.resType;
72,398,595✔
486
  return TSDB_CODE_SUCCESS;
72,398,554✔
487
}
488

489
bool fmIsDistExecFunc(int32_t funcId) {
242,919,018✔
490
  if (fmIsUserDefinedFunc(funcId)) {
242,919,018✔
491
    return false;
200,153✔
492
  }
493
  if (!fmIsVectorFunc(funcId)) {
242,719,027✔
494
    return true;
135,596✔
495
  }
496
  return (NULL != funcMgtBuiltins[funcId].pPartialFunc && NULL != funcMgtBuiltins[funcId].pMergeFunc);
242,583,197✔
497
}
498

499
static int32_t createPartialFunction(const SFunctionNode* pSrcFunc, SFunctionNode** pPartialFunc) {
36,199,232✔
500
  SNodeList* pParameterList = NULL;
36,199,232✔
501
  int32_t    code = nodesCloneList(pSrcFunc->pParameterList, &pParameterList);
36,199,277✔
502
  if (NULL == pParameterList) {
36,199,277✔
UNCOV
503
    return code;
×
504
  }
505
  code =
506
      createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pPartialFunc, pSrcFunc, pParameterList, pPartialFunc);
36,199,277✔
507
  if (TSDB_CODE_SUCCESS != code) {
36,199,232✔
UNCOV
508
    nodesDestroyList(pParameterList);
×
509
    return code;
×
510
  }
511
  (*pPartialFunc)->hasOriginalFunc = true;
36,199,232✔
512
  (*pPartialFunc)->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
36,199,277✔
513
  char name[TSDB_FUNC_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_POINTER_PRINT_BYTES + 1] = {0};
36,199,318✔
514

515
  int32_t len = tsnprintf(name, sizeof(name), "%s.%p", (*pPartialFunc)->functionName, pSrcFunc);
36,199,228✔
516
  if (taosHashBinary(name, len) < 0) {
36,199,142✔
UNCOV
517
    return TSDB_CODE_FAILED;
×
518
  }
519
  tstrncpy((*pPartialFunc)->node.aliasName, name, TSDB_COL_NAME_LEN);
36,199,142✔
520
  return TSDB_CODE_SUCCESS;
36,199,277✔
521
}
522

523
static int32_t createMergeFuncPara(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
72,398,509✔
524
                                   SNodeList** pParameterList) {
525
  SNode*  pRes = NULL;
72,398,509✔
526
  int32_t code = createColumnByFunc(pPartialFunc, (SColumnNode**)&pRes);
72,398,509✔
527
  if (TSDB_CODE_SUCCESS != code) {
72,398,554✔
UNCOV
528
    return code;
×
529
  }
530
  if (NULL != funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc) {
72,398,554✔
531
    return funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc(pSrcFunc->pParameterList, pRes, pParameterList);
559,364✔
532
  } else {
533
    return nodesListMakeStrictAppend(pParameterList, pRes);
71,839,055✔
534
  }
535
}
536

537
static int32_t createMidFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
36,199,232✔
538
                                 SFunctionNode** pMidFunc) {
539
  SNodeList*     pParameterList = NULL;
36,199,232✔
540
  SFunctionNode* pFunc = NULL;
36,199,277✔
541

542
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
36,199,277✔
543
  if (TSDB_CODE_SUCCESS == code) {
36,199,277✔
544
    if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){
36,199,277✔
545
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pSrcFunc, pParameterList, &pFunc);
4,320,424✔
546
    }else{
547
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
31,878,808✔
548
    }
549
  }
550
  if (TSDB_CODE_SUCCESS == code) {
36,199,367✔
551
    tstrncpy(pFunc->node.aliasName, pPartialFunc->node.aliasName, TSDB_COL_NAME_LEN);
36,199,322✔
552
  }
553

554
  if (TSDB_CODE_SUCCESS == code) {
36,199,232✔
555
    *pMidFunc = pFunc;
36,199,232✔
556
  } else {
UNCOV
557
    nodesDestroyList(pParameterList);
×
558
  }
559
  return code;
36,199,277✔
560
}
561

562
static int32_t createMergeFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
36,199,142✔
563
                                   SFunctionNode** pMergeFunc) {
564
  SNodeList*     pParameterList = NULL;
36,199,142✔
565
  SFunctionNode* pFunc = NULL;
36,199,187✔
566

567
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
36,199,187✔
568
  if (TSDB_CODE_SUCCESS == code) {
36,199,322✔
569
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
36,199,277✔
570
  }
571
  if (TSDB_CODE_SUCCESS == code) {
36,199,322✔
572
    pFunc->hasOriginalFunc = true;
36,199,322✔
573
    pFunc->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
36,199,322✔
574
    // overwrite function restype set by translate function
575
    if (fmIsSameInOutType(pSrcFunc->funcId)) {
36,199,322✔
576
      pFunc->node.resType = pSrcFunc->node.resType;
13,856,619✔
577
    }
578
    tstrncpy(pFunc->node.aliasName, pSrcFunc->node.aliasName, TSDB_COL_NAME_LEN);
36,199,232✔
579
  }
580

581
  if (TSDB_CODE_SUCCESS == code) {
36,199,232✔
582
    *pMergeFunc = pFunc;
36,199,232✔
583
  } else {
UNCOV
584
    nodesDestroyList(pParameterList);
×
585
  }
586
  return code;
36,199,322✔
587
}
588

589
int32_t fmGetDistMethod(const SFunctionNode* pFunc, SFunctionNode** pPartialFunc, SFunctionNode** pMidFunc,
36,199,277✔
590
                        SFunctionNode** pMergeFunc) {
591
  if (!fmIsDistExecFunc(pFunc->funcId)) {
36,199,277✔
UNCOV
592
    return TSDB_CODE_FAILED;
×
593
  }
594

595
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
36,199,277✔
596
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
36,199,232✔
597
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
36,199,232✔
598
  }
599
  if (TSDB_CODE_SUCCESS == code) {
36,199,187✔
600
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
36,199,187✔
601
  }
602

603
  if (TSDB_CODE_SUCCESS != code) {
36,199,232✔
UNCOV
604
    nodesDestroyNode((SNode*)*pPartialFunc);
×
UNCOV
605
    if (pMidFunc) nodesDestroyNode((SNode*)*pMidFunc);
×
UNCOV
606
    nodesDestroyNode((SNode*)*pMergeFunc);
×
607
  }
608

609
  return code;
36,199,228✔
610
}
611

612
const char* fmGetFuncName(int32_t funcId) {
316,656✔
613
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
316,656✔
UNCOV
614
    return "invalid function";
×
615
  }
616
  return funcMgtBuiltins[funcId].name;
316,656✔
617
}
618

619
/// @param [out] pStateFunc, not changed if error occured or no need to create state func
620
/// @retval 0 for succ, otherwise err occured
621
static int32_t fmCreateStateFunc(const SFunctionNode* pFunc, SFunctionNode** pStateFunc) {
1,346✔
622
  if (funcMgtBuiltins[pFunc->funcId].pStateFunc) {
1,346✔
623
    SNodeList* pParams = NULL;
1,346✔
624
    int32_t    code = nodesCloneList(pFunc->pParameterList, &pParams);
1,346✔
625
    if (!pParams) return code;
1,346✔
626
    code = createFunction(funcMgtBuiltins[pFunc->funcId].pStateFunc, pParams, pStateFunc);
1,346✔
627
    if (TSDB_CODE_SUCCESS != code) {
1,346✔
UNCOV
628
      nodesDestroyList(pParams);
×
UNCOV
629
      return code;
×
630
    }
631
    tstrncpy((*pStateFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
1,346✔
632
    tstrncpy((*pStateFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
1,346✔
633
  }
634
  return TSDB_CODE_SUCCESS;
1,346✔
635
}
636

637
bool fmIsTSMASupportedFunc(func_id_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC); }
391,686✔
638

639
bool fmIsRsmaSupportedFunc(func_id_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_RSMA_FUNC); }
119,479✔
640

641
int32_t fmCreateStateFuncs(SNodeList* pFuncs) {
673✔
642
  int32_t code;
643
  SNode*  pNode;
644
  char    buf[128] = {0};
673✔
645
  FOREACH(pNode, pFuncs) {
2,019✔
646
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
1,346✔
647
    code = fmGetFuncInfo(pFunc, buf, 128);
1,346✔
648
    if (code) break;
1,346✔
649
    if (fmIsTSMASupportedFunc(pFunc->funcId)) {
1,346✔
650
      SFunctionNode* pNewFunc = NULL;
1,346✔
651
      code = fmCreateStateFunc(pFunc, &pNewFunc);
1,346✔
652
      if (code) {
1,346✔
653
        // error
654
        break;
×
655
      } else if (!pNewFunc) {
1,346✔
656
        // no need state func
UNCOV
657
        continue;
×
658
      } else {
659
        REPLACE_NODE(pNewFunc);
1,346✔
660
        nodesDestroyNode(pNode);
1,346✔
661
      }
662
    }
663
  }
664
  return code;
673✔
665
}
666

667
static int32_t fmCreateStateMergeFunc(SFunctionNode* pFunc, SFunctionNode** pStateMergeFunc) {
×
668
  if (funcMgtBuiltins[pFunc->funcId].pMergeFunc) {
×
669
    SNodeList* pParams = NULL;
×
670
    int32_t    code = nodesCloneList(pFunc->pParameterList, &pParams);
×
671
    if (!pParams) return code;
×
672
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pFunc->funcId].pMergeFunc, pFunc, pParams, pStateMergeFunc);
×
673
    if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
674
      nodesDestroyList(pParams);
×
675
      return code;
×
676
    }
UNCOV
677
    tstrncpy((*pStateMergeFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
×
678
    tstrncpy((*pStateMergeFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
×
679
  }
680
  return TSDB_CODE_SUCCESS;
×
681
}
682

UNCOV
683
int32_t fmCreateStateMergeFuncs(SNodeList* pFuncs) {
×
684
  int32_t code;
685
  SNode*  pNode;
UNCOV
686
  char    buf[128] = {0};
×
UNCOV
687
  FOREACH(pNode, pFuncs) {
×
UNCOV
688
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
×
UNCOV
689
    if (fmIsTSMASupportedFunc(pFunc->funcId)) {
×
UNCOV
690
      SFunctionNode* pNewFunc = NULL;
×
UNCOV
691
      code = fmCreateStateMergeFunc(pFunc, &pNewFunc);
×
UNCOV
692
      if (code) {
×
693
        // error
694
        break;
×
UNCOV
695
      } else if (!pNewFunc) {
×
696
        // no state merge func
697
        continue;
×
698
      } else {
UNCOV
699
        REPLACE_NODE(pNewFunc);
×
UNCOV
700
        nodesDestroyNode(pNode);
×
701
      }
702
    }
703
  }
704
  return code;
×
705
}
706

707
int32_t fmGetFuncId(const char* name) {
815,364✔
708
  if (NULL != gFunMgtService.pFuncNameHashTable) {
815,364✔
709
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, name, strlen(name));
815,364✔
710
    if (NULL != pVal) {
815,364✔
711
      return *(int32_t*)pVal;
815,364✔
712
    }
713
    return -1;
×
714
  }
715
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
×
716
    if (0 == strcmp(funcMgtBuiltins[i].name, name)) {
×
UNCOV
717
      return i;
×
718
    }
719
  }
UNCOV
720
  return -1;
×
721
}
722

UNCOV
723
bool fmIsMyStateFunc(int32_t funcId, int32_t stateFuncId) {
×
UNCOV
724
  const SBuiltinFuncDefinition* pFunc = &funcMgtBuiltins[funcId];
×
725
  const SBuiltinFuncDefinition* pStateFunc = &funcMgtBuiltins[stateFuncId];
×
UNCOV
726
  if (!pFunc->pStateFunc) {
×
UNCOV
727
    return false;
×
728
  }
UNCOV
729
  if (strcmp(pFunc->pStateFunc, pStateFunc->name) == 0) return true;
×
730
  int32_t stateMergeFuncId = fmGetFuncId(pFunc->pStateFunc);
×
731
  if (stateMergeFuncId == -1) {
×
732
    return false;
×
733
  }
734
  const SBuiltinFuncDefinition* pStateMergeFunc = &funcMgtBuiltins[stateMergeFuncId];
×
UNCOV
735
  return strcmp(pStateFunc->name, pStateMergeFunc->pMergeFunc) == 0;
×
736
}
737

738
bool fmIsCountLikeFunc(int32_t funcId) {
395,978,827✔
739
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
395,978,827✔
740
}
741

742
bool fmIsRowTsOriginFunc(int32_t funcId) {
4,747,086✔
743
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
4,747,086✔
744
    return false;
×
745
  }
746
  return FUNCTION_TYPE_IROWTS_ORIGIN == funcMgtBuiltins[funcId].type;
4,747,086✔
747
}
748

UNCOV
749
bool fmIsGroupIdFunc(int32_t funcId) {
×
UNCOV
750
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
UNCOV
751
    return false;
×
752
  }
UNCOV
753
  return FUNCTION_TYPE_GROUP_ID == funcMgtBuiltins[funcId].type;
×
754
}
755

756
const void* fmGetStreamPesudoFuncVal(int32_t funcId, const SStreamRuntimeFuncInfo* pStreamRuntimeFuncInfo) {
36,640,802✔
757
  SSTriggerCalcParam *pParams = taosArrayGet(pStreamRuntimeFuncInfo->pStreamPesudoFuncVals, pStreamRuntimeFuncInfo->curIdx);
36,640,802✔
758
  switch (funcMgtBuiltins[funcId].type) {
36,643,000✔
UNCOV
759
    case FUNCTION_TYPE_TPREV_TS:
×
UNCOV
760
      return &pParams->prevTs;
×
761
    case FUNCTION_TYPE_TCURRENT_TS:
1,716,816✔
762
      return &pParams->currentTs;
1,716,816✔
UNCOV
763
    case FUNCTION_TYPE_TNEXT_TS:
×
UNCOV
764
      return &pParams->nextTs;
×
765
    case FUNCTION_TYPE_TWSTART:
18,147,791✔
766
      return &pParams->wstart;
18,147,791✔
767
    case FUNCTION_TYPE_TWEND:
13,759,019✔
768
      return &pParams->wend;
13,759,019✔
769
    case FUNCTION_TYPE_TWDURATION:
38,690✔
770
      return &pParams->wduration;
38,690✔
771
    case FUNCTION_TYPE_TWROWNUM:
96,342✔
772
      return &pParams->wrownum;
96,342✔
773
    case FUNCTION_TYPE_TPREV_LOCALTIME:
97,548✔
774
      return &pParams->prevLocalTime;
97,548✔
775
    case FUNCTION_TYPE_TLOCALTIME:
286,539✔
776
      return &pParams->triggerTime;
286,539✔
777
    case FUNCTION_TYPE_TNEXT_LOCALTIME:
154,507✔
778
      return &pParams->nextLocalTime;
154,507✔
779
    case FUNCTION_TYPE_TGRPID:
111,980✔
780
      return &pStreamRuntimeFuncInfo->groupId;
111,980✔
781
    case FUNCTION_TYPE_PLACEHOLDER_COLUMN:
523,893✔
782
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
523,893✔
783
    case FUNCTION_TYPE_PLACEHOLDER_TBNAME:
1,710,628✔
784
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
1,710,628✔
785
    default:
×
786
      break;
×
787
  }
UNCOV
788
  return NULL;
×
789
}
790

791
bool fmIsStreamPesudoColVal(int32_t funcId) {
191,735✔
792
  return funcMgtBuiltins[funcId].type == FUNCTION_TYPE_PLACEHOLDER_COLUMN
191,735✔
793
         || funcMgtBuiltins[funcId].type == FUNCTION_TYPE_PLACEHOLDER_TBNAME;
191,571✔
794
}
795

796
int32_t fmGetStreamPesudoFuncEnv(int32_t funcId, SNodeList* pParamNodes, SFuncExecEnv *pEnv) {
5,736,408✔
797
  if (NULL == pParamNodes || pParamNodes->length < 1) {
5,736,408✔
798
    uError("invalid stream pesudo func param list %p", pParamNodes);
568✔
799
    return TSDB_CODE_INTERNAL_ERROR;
×
800
  }
801

802
  int32_t firstParamType = nodeType(nodesListGetNode(pParamNodes, 0));
5,736,258✔
803
  if (QUERY_NODE_VALUE != firstParamType) {
5,737,075✔
UNCOV
804
    uError("invalid stream pesudo func first param type %d", firstParamType);
×
805
    return TSDB_CODE_INTERNAL_ERROR;
×
806
  }
807

808
  SValueNode* pResDesc = (SValueNode*)nodesListGetNode(pParamNodes, 0);
5,737,075✔
809
  pEnv->calcMemSize = pResDesc->node.resType.bytes;
5,737,493✔
810

811
  return TSDB_CODE_SUCCESS;
5,737,546✔
812
}
813

814

815
int32_t fmSetStreamPseudoFuncParamVal(int32_t funcId, SNodeList* pParamNodes, const SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
36,635,915✔
816
  if (!pStreamRuntimeInfo) {
36,635,915✔
UNCOV
817
    uError("internal error, should have pVals for stream pseudo funcs");
×
818
    return TSDB_CODE_INTERNAL_ERROR;
×
819
  }
820
  int32_t code = 0;
36,635,915✔
821
  SArray *pVals1 = NULL;
36,635,915✔
822
  SNode* pFirstParam = nodesListGetNode(pParamNodes, 0);
36,635,915✔
823
  if (nodeType(pFirstParam) != QUERY_NODE_VALUE) {
36,638,293✔
824
    uError("invalid param node type: %d for func: %d", nodeType(pFirstParam), funcId);
×
825
    return TSDB_CODE_INTERNAL_ERROR;
×
826
  }
827

828
  int32_t t = funcMgtBuiltins[funcId].type;
36,634,823✔
829
  uDebug("set stream pseudo func param val, functype: %d, pStreamRuntimeInfo: %p", t, pStreamRuntimeInfo);
36,636,161✔
830
  if (FUNCTION_TYPE_TGRPID == t) {
36,641,725✔
831
    SValue v = {0};
111,980✔
832
    v.type = TSDB_DATA_TYPE_BIGINT;
111,980✔
833
    v.val = *(int64_t*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
111,980✔
834
    
835
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&v, pFirstParam->type));
111,980✔
836
    if (code != 0) {
111,980✔
UNCOV
837
      uError("failed to set value node value: %s", tstrerror(code));
×
UNCOV
838
      return code;
×
839
    }
840
  } else if (FUNCTION_TYPE_PLACEHOLDER_COLUMN == t) {
36,529,745✔
841
    SNode* pSecondParam = nodesListGetNode(pParamNodes, 1);
524,057✔
842
    if (nodeType(pSecondParam) != QUERY_NODE_VALUE) {
524,057✔
UNCOV
843
      uError("invalid param node type: %d for func: %d", nodeType(pSecondParam), funcId);
×
UNCOV
844
      return TSDB_CODE_INTERNAL_ERROR;
×
845
    }
846
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
524,057✔
847
    int32_t idx = ((SValueNode*)pSecondParam)->datum.i;
524,057✔
848
    if (idx - 1 < 0 || idx - 1 >= taosArrayGetSize(pVal)) {
524,057✔
849
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
164✔
UNCOV
850
      return TSDB_CODE_INTERNAL_ERROR;
×
851
    }
852
    SStreamGroupValue* pValue = taosArrayGet(pVal, idx - 1);
523,893✔
853
    if (pValue == NULL) {
523,893✔
UNCOV
854
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
×
UNCOV
855
      return TSDB_CODE_INTERNAL_ERROR;
×
856
    }
857
    if (!pValue->isNull){
523,893✔
858
      if (pValue->data.type != ((SValueNode*)pFirstParam)->node.resType.type){
518,317✔
UNCOV
859
        uError("invalid value type: %d for func: %d, should be: %d", pValue->data.type, funcId, ((SValueNode*)pFirstParam)->node.resType.type);
×
UNCOV
860
        return TSDB_CODE_INTERNAL_ERROR;
×
861
      }
862
      if (IS_VAR_DATA_TYPE(((SValueNode*)pFirstParam)->node.resType.type)) {
517,940✔
863
        char* tmp = taosMemoryCalloc(1, pValue->data.nData + VARSTR_HEADER_SIZE); 
396,444✔
864
        if (tmp == NULL) {
397,526✔
865
          return TSDB_CODE_OUT_OF_MEMORY;
×
866
        }
867
        taosMemoryFree(((SValueNode*)pFirstParam)->datum.p);
397,526✔
868
        ((SValueNode*)pFirstParam)->datum.p = tmp;
397,526✔
869
        memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
397,526✔
870
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
397,526✔
871
      } else {
872
        code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&pValue->data, pValue->data.type));
120,791✔
873
      }
874
    }
875
    ((SValueNode*)pFirstParam)->isNull = pValue->isNull;
524,057✔
876
  } else if (FUNCTION_TYPE_PLACEHOLDER_TBNAME == t) {
36,005,688✔
877
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
1,709,654✔
878
    for (int32_t i = 0; i < taosArrayGetSize(pVal); ++i) {
1,710,874✔
879
      SStreamGroupValue* pValue = taosArrayGet(pVal, i);
1,710,628✔
880
      if (pValue != NULL && pValue->isTbname) {
1,711,119✔
881
        taosMemoryFreeClear(((SValueNode*)pFirstParam)->datum.p);
1,711,365✔
882
        ((SValueNode*)pFirstParam)->datum.p = taosMemoryCalloc(pValue->data.nData + VARSTR_HEADER_SIZE, 1);
1,710,629✔
883
        if (NULL == ((SValueNode*)pFirstParam)->datum.p ) {
1,710,628✔
UNCOV
884
          return terrno;
×
885
        }
886

887
        (void)memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
1,710,873✔
888
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
1,710,629✔
889
        break;
1,711,365✔
890
      }
891
    }
892
  } else if (LIST_LENGTH(pParamNodes) == 1) {
68,589,534✔
893
    // twstart, twend
894
    const void* pVal = fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
34,297,225✔
895
    if (!pVal) {
34,296,214✔
UNCOV
896
      uError("failed to set stream pseudo func param val, NULL val for funcId: %d", funcId);
×
UNCOV
897
      return TSDB_CODE_INTERNAL_ERROR;
×
898
    }
899
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, (void*)pVal);
34,296,214✔
900
    if (code != 0) {
34,296,640✔
901
      uError("failed to set value node value: %s", tstrerror(code));
3,140✔
UNCOV
902
      return code;
×
903
    }
904
  } else {
905
    uError("invalid placeholder function type: %d", t);
109✔
UNCOV
906
    return TSDB_CODE_INTERNAL_ERROR;
×
907
  }
908
  
909
  return code;
36,643,829✔
910
}
911

912

913

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