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

taosdata / TDengine / #5091

17 May 2026 01:15AM UTC coverage: 73.326% (-0.01%) from 73.34%
#5091

push

travis-ci

web-flow
feat (TDgpt): Dynamic Model Synchronization Enhancements (#35344)

* refactor: do some internal refactor.

* fix: fix multiprocess sync issue.

* feat: add dynamic anomaly detection and forecasting services

* fix: log error message for undeploying model in exception handling

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* fix: handle undeploy when model exists only on disk

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/286aafa0-c3ce-4c27-b803-2707571e9dc1

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: guard dynamic registry concurrent access

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: tighten service list locking scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: restore prophet support and update tests per review feedback

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: improve test name and move copy inside lock scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* Potential fix for pull request finding

Co-au... (continued)

281422 of 383795 relevant lines covered (73.33%)

136839186.86 hits per line

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

82.5
/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 InitSpecialFuncId() {
1,662,019✔
37
  // just to make sure the funcId of these functions are initialized before used.
38
  (void)fmGetTwstartFuncId();
1,662,019✔
39
  (void)fmGetTwendFuncId();
1,662,019✔
40
  (void)fmGetTwdurationFuncId();
1,662,019✔
41
  (void)fmGetExternalWindowColumnFuncId();
1,662,019✔
42
}
1,662,019✔
43

44
static void doInitFunctionTable() {
1,662,019✔
45
  gFunMgtService.pFuncNameHashTable =
1,662,019✔
46
      taosHashInit(funcMgtBuiltinsNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
1,662,019✔
47
  if (NULL == gFunMgtService.pFuncNameHashTable) {
1,662,019✔
48
    initFunctionCode = terrno;
×
49
    return;
×
50
  }
51

52
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
387,250,427✔
53
    if (TSDB_CODE_SUCCESS != taosHashPut(gFunMgtService.pFuncNameHashTable, funcMgtBuiltins[i].name,
385,588,408✔
54
                                         strlen(funcMgtBuiltins[i].name), &i, sizeof(int32_t))) {
385,588,408✔
55
      initFunctionCode = terrno;
×
56
      return;
×
57
    }
58
  }
59
  InitSpecialFuncId();
1,662,019✔
60
}
61

62
static bool isSpecificClassifyFunc(int32_t funcId, uint64_t classification) {
2,147,483,647✔
63
  if (fmIsUserDefinedFunc(funcId)) {
2,147,483,647✔
64
    return FUNC_MGT_AGG_FUNC == classification
65
               ? FUNC_AGGREGATE_UDF_ID == funcId
66
               : (FUNC_MGT_SCALAR_FUNC == classification ? FUNC_SCALAR_UDF_ID == funcId : false);
9,899,649✔
67
  }
68
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,147,483,647✔
69
    return false;
2,147,483,647✔
70
  }
71
  return FUNC_MGT_TEST_MASK(funcMgtBuiltins[funcId].classification, classification);
2,147,483,647✔
72
}
73

74
int32_t fmFuncMgtInit() {
1,662,019✔
75
  (void)taosThreadOnce(&functionHashTableInit, doInitFunctionTable);
1,662,019✔
76
  return initFunctionCode;
1,662,019✔
77
}
78

79
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
902,908,924✔
80
  if (NULL != gFunMgtService.pFuncNameHashTable) {
902,908,924✔
81
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
902,735,170✔
82
    if (NULL != pVal) {
902,758,040✔
83
      pFunc->funcId = *(int32_t*)pVal;
902,533,523✔
84
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
902,533,523✔
85
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
902,531,423✔
86
    }
87
    return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
224,517✔
88
  }
89
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
3,446,410✔
90
    if (0 == strcmp(funcMgtBuiltins[i].name, pFunc->functionName)) {
3,446,936✔
91
      pFunc->funcId = i;
185,248✔
92
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
185,248✔
93
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
185,248✔
94
    }
95
  }
96
  return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
×
97
}
98

99
EFuncReturnRows fmGetFuncReturnRows(SFunctionNode* pFunc) {
19,398,686✔
100
  if (NULL != funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc) {
19,398,686✔
101
    return funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc(pFunc);
13,681,018✔
102
  }
103
  return (fmIsIndefiniteRowsFunc(pFunc->funcId) || fmIsMultiRowsFunc(pFunc->funcId)) ? FUNC_RETURN_ROWS_INDEFINITE
5,717,668✔
104
                                                                                     : FUNC_RETURN_ROWS_NORMAL;
5,717,668✔
105
}
106

107
bool canCoexistIndefiniteRowsFunc(int32_t funcId1, int32_t funcId2) {
193,432✔
108
  if (fmIsUserDefinedFunc(funcId1) || funcId1 < 0 || funcId1 >= funcMgtBuiltinsNum ||
386,864✔
109
      fmIsUserDefinedFunc(funcId2) || funcId2 < 0 || funcId2 >= funcMgtBuiltinsNum) {
386,864✔
110
    return false;
×
111
  }
112
  if (funcId1 == funcId2) {
193,432✔
113
    return true;
176,670✔
114
  }
115
  if ((funcMgtBuiltins[funcId1].type == FUNCTION_TYPE_LAG || funcMgtBuiltins[funcId1].type == FUNCTION_TYPE_LEAD) &&
16,762✔
116
      (funcMgtBuiltins[funcId2].type == FUNCTION_TYPE_LAG || funcMgtBuiltins[funcId2].type == FUNCTION_TYPE_LEAD)) {
16,283✔
117
    return true;
16,283✔
118
  }
119
  return false;
479✔
120
}
121

122
bool fmIsBuiltinFunc(const char* pFunc) {
18,336✔
123
  return NULL != taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
18,336✔
124
}
125

126
EFunctionType fmGetFuncType(const char* pFunc) {
652,257,862✔
127
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
652,257,862✔
128
  if (NULL != pVal) {
652,273,435✔
129
    return funcMgtBuiltins[*(int32_t*)pVal].type;
652,046,316✔
130
  }
131
  return FUNCTION_TYPE_UDF;
227,119✔
132
}
133

134
EFunctionType fmGetFuncTypeFromId(int32_t funcId) {
25,709,221✔
135
  if (funcId >= 0 && funcId < funcMgtBuiltinsNum) {
25,709,221✔
136
    return funcMgtBuiltins[funcId].type;
25,709,221✔
137
  }
138
  return FUNCTION_TYPE_UDF;
×
139
}
140

141
EFuncDataRequired fmFuncDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
43,713,096✔
142
  if (fmIsUserDefinedFunc(pFunc->funcId) || pFunc->funcId < 0 || pFunc->funcId >= funcMgtBuiltinsNum) {
43,713,096✔
143
    return FUNC_DATA_REQUIRED_DATA_LOAD;
1,739✔
144
  }
145
  if (NULL == funcMgtBuiltins[pFunc->funcId].dataRequiredFunc) {
43,714,432✔
146
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
147
  }
148
  return funcMgtBuiltins[pFunc->funcId].dataRequiredFunc(pFunc, pTimeWindow);
43,714,624✔
149
}
150

151
EFuncDataRequired fmFuncDynDataRequired(int32_t funcId, void* pRes, SDataBlockInfo* pBlockInfo) {
2,147,483,647✔
152
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,147,483,647✔
153
    return FUNC_DATA_REQUIRED_DATA_LOAD;
9,256✔
154
  }
155

156
  const char* name = funcMgtBuiltins[funcId].name;
2,147,483,647✔
157
  if ((strcmp(name, "_group_key") == 0) || (strcmp(name, "_select_value") == 0)) {
2,147,483,647✔
158
    return FUNC_DATA_REQUIRED_NOT_LOAD;
540,475✔
159
    ;
160
  }
161

162
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
2,147,483,647✔
163
    return FUNC_DATA_REQUIRED_DATA_LOAD;
202,607,919✔
164
  } else {
165
    return funcMgtBuiltins[funcId].dynDataRequiredFunc(pRes, pBlockInfo);
2,147,483,647✔
166
  }
167
}
168

169
int32_t fmGetFuncExecFuncs(int32_t funcId, SFuncExecFuncs* pFpSet) {
377,474,338✔
170
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
377,474,338✔
171
    return TSDB_CODE_FAILED;
34,706✔
172
  }
173
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
377,459,126✔
174
  pFpSet->init = funcMgtBuiltins[funcId].initFunc;
377,435,580✔
175
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
377,461,850✔
176
  pFpSet->finalize = funcMgtBuiltins[funcId].finalizeFunc;
377,434,735✔
177
  pFpSet->combine = funcMgtBuiltins[funcId].combineFunc;
377,434,119✔
178
  pFpSet->processFuncByRow = funcMgtBuiltins[funcId].processFuncByRow;
377,429,248✔
179
  pFpSet->cleanup = funcMgtBuiltins[funcId].cleanupFunc;
377,471,487✔
180
  return TSDB_CODE_SUCCESS;
377,450,024✔
181
}
182

183
int32_t fmGetUdafExecFuncs(int32_t funcId, SFuncExecFuncs* pFpSet) {
52,489✔
184
#ifdef USE_UDF
185
  if (!fmIsUserDefinedFunc(funcId)) {
52,489✔
186
    return TSDB_CODE_FAILED;
×
187
  }
188
  pFpSet->getEnv = udfAggGetEnv;
52,489✔
189
  pFpSet->init = udfAggInit;
52,489✔
190
  pFpSet->process = udfAggProcess;
52,489✔
191
  pFpSet->finalize = udfAggFinalize;
52,489✔
192
  return TSDB_CODE_SUCCESS;
52,489✔
193
#else
194
  TAOS_RETURN(TSDB_CODE_OPS_NOT_SUPPORT);
195
#endif
196
}
197

198
int32_t fmGetScalarFuncExecFuncs(int32_t funcId, SScalarFuncExecFuncs* pFpSet) {
1,238,885,652✔
199
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,238,885,652✔
200
    return TSDB_CODE_FAILED;
152,963✔
201
  }
202
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
1,238,993,502✔
203
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
1,239,057,254✔
204
  return TSDB_CODE_SUCCESS;
1,239,094,459✔
205
}
206

207
bool fmIsAggFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_AGG_FUNC); }
2,147,483,647✔
208

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

211
bool fmIsVectorFunc(int32_t funcId) { return !fmIsScalarFunc(funcId) && !fmIsPseudoColumnFunc(funcId); }
1,403,035,469✔
212

213
bool fmIsSelectColsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_COLS_FUNC); }
164,706,720✔
214

215
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
2,147,483,647✔
216

217
bool fmIsTimelineFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TIMELINE_FUNC); }
1,262,227,230✔
218

219
bool fmIsDateTimeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_DATETIME_FUNC); }
638,494,240✔
220

221
bool fmIsPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PSEUDO_COLUMN_FUNC); }
2,147,483,647✔
222

223
bool fmIsScanPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCAN_PC_FUNC); }
2,147,483,647✔
224

225
bool fmIsWindowPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_WINDOW_PC_FUNC); }
1,638,531,779✔
226

227
bool fmIsWindowClauseFunc(int32_t funcId) { return fmIsAggFunc(funcId) || fmIsWindowPseudoColumnFunc(funcId); }
64,763,015✔
228

229
bool fmIsWindowIndefRowsFunc(int32_t funcId) { return fmIsVectorFunc(funcId) || fmIsWindowPseudoColumnFunc(funcId); }
428,434✔
230

231
bool fmIsStreamWindowClauseFunc(int32_t funcId) { return fmIsWindowClauseFunc(funcId) || fmIsPlaceHolderFunc(funcId); }
1,222,350✔
232

233
bool fmIsStreamVectorFunc(int32_t funcId) { return fmIsVectorFunc(funcId) || fmIsPlaceHolderFunc(funcId); }
10,727✔
234

235
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
1,740,097,317✔
236

237
bool fmIsSpecialDataRequiredFunc(int32_t funcId) {
99,153,122✔
238
  return isSpecificClassifyFunc(funcId, FUNC_MGT_SPECIAL_DATA_REQUIRED);
99,153,122✔
239
}
240

241
bool fmIsDynamicScanOptimizedFunc(int32_t funcId) {
47,535,311✔
242
  return isSpecificClassifyFunc(funcId, FUNC_MGT_DYNAMIC_SCAN_OPTIMIZED);
47,535,311✔
243
}
244

245
bool fmIsMultiResFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_RES_FUNC); }
1,299,478,676✔
246

247
bool fmIsRepeatScanFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_REPEAT_SCAN_FUNC); }
1,275,674,248✔
248

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

251
bool fmIsForbidFillFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_FILL_FUNC); }
638,810,544✔
252

253
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
71,575,425✔
254

255
bool fmIsSystemInfoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SYSTEM_INFO_FUNC); }
642,090,352✔
256

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

259
bool fmIsClientPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CLIENT_PC_FUNC); }
650,390,135✔
260

261
bool fmIsMultiRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_ROWS_FUNC); }
1,272,580,060✔
262

263
static bool fmIsKeepOrderFuncId(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_KEEP_ORDER_FUNC); }
769,906,717✔
264

265
bool fmIsKeepOrderFunc(SFunctionNode* pFunc) {
776,880,662✔
266
  if (pFunc->funcType == FUNCTION_TYPE_TOP || pFunc->funcType == FUNCTION_TYPE_BOTTOM ||
776,880,662✔
267
      pFunc->funcType == FUNCTION_TYPE_SAMPLE) {
772,473,112✔
268
    if (pFunc->pParameterList != NULL && pFunc->pParameterList->length >= 2) {
6,971,991✔
269
      SNode* pParam = nodesListGetNode(pFunc->pParameterList, 1);
6,987,640✔
270
      if (pParam != NULL && nodeType(pParam) == QUERY_NODE_VALUE) {
6,987,640✔
271
        SValueNode* pConst = (SValueNode*)pParam;
6,987,640✔
272
        if (pConst->datum.i == 1) {
6,987,640✔
273
          return true;
817,468✔
274
        }
275
      }
276
    }
277
    return false;
6,160,642✔
278
  }
279
  return fmIsKeepOrderFuncId(pFunc->funcId);
769,917,209✔
280
}
281

282
bool fmIsCumulativeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CUMULATIVE_FUNC); }
167,404,423✔
283

284
bool fmIsForbidSysTableFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_SYSTABLE_FUNC); }
638,711,469✔
285

286
bool fmIsInterpFunc(int32_t funcId) {
1,308,883,199✔
287
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,308,883,199✔
288
    return false;
418,520✔
289
  }
290
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
1,308,468,791✔
291
}
292

293
bool fmIsInterpPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERP_PC_FUNC); }
1,287,194,746✔
294

295
bool fmIsForecastFunc(int32_t funcId) {
1,255,693,120✔
296
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,255,693,120✔
297
    return false;
422,900✔
298
  }
299
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
1,255,272,362✔
300
}
301

302
bool fmIsAnalysisPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_ANALYTICS_PC_FUNC); }
1,290,309,617✔
303

304
bool fmIsImputationCorrelationFunc(int32_t funcId) {
×
305
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
306
    return false;
×
307
  }
308

309
  int32_t type = funcMgtBuiltins[funcId].type;
×
310
  return FUNCTION_TYPE_IMPUTATION == type || FUNCTION_TYPE_DTW == type || FUNCTION_TYPE_DTW_PATH == type ||
×
311
         FUNCTION_TYPE_TLCC == type;
312
}
313

314
bool fmIsLastRowFunc(int32_t funcId) {
75,436,991✔
315
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
75,436,991✔
316
    return false;
33✔
317
  }
318
  return FUNCTION_TYPE_LAST_ROW == funcMgtBuiltins[funcId].type;
75,437,444✔
319
}
320

321
bool fmIsLastFunc(int32_t funcId) {
13,590✔
322
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
13,590✔
323
    return false;
×
324
  }
325
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type;
13,590✔
326
}
327

328
bool fmIsNotNullOutputFunc(int32_t funcId) {
563,898,606✔
329
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
563,898,606✔
330
    return false;
104,251✔
331
  }
332
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
563,803,100✔
333
         FUNCTION_TYPE_CACHE_LAST == funcMgtBuiltins[funcId].type ||
542,652,385✔
334
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
541,210,739✔
335
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
531,398,411✔
336
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
527,452,174✔
337
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
512,710,479✔
338
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
506,461,230✔
339
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
504,339,312✔
340
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
440,917,828✔
341
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
1,543,224,063✔
342
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
436,730,906✔
343
}
344

345
bool fmIsSelectValueFunc(int32_t funcId) {
381,929,076✔
346
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
381,929,076✔
347
    return false;
×
348
  }
349
  return FUNCTION_TYPE_SELECT_VALUE == funcMgtBuiltins[funcId].type;
381,932,562✔
350
}
351

352
bool fmIsGroupKeyFunc(int32_t funcId) {
293,625,832✔
353
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
293,625,832✔
354
    return false;
×
355
  }
356
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
293,626,822✔
357
}
358

359
bool fmIsHasNullFunc(int32_t funcId) {
×
360
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
361
    return false;
×
362
  }
363
  return FUNCTION_TYPE_HAS_NULL == funcMgtBuiltins[funcId].type;
×
364
}
365

366
bool fmisSelectGroupConstValueFunc(int32_t funcId) {
19,191,995✔
367
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
19,191,995✔
368
    return false;
×
369
  }
370
  return FUNCTION_TYPE_GROUP_CONST_VALUE == funcMgtBuiltins[funcId].type;
19,191,995✔
371
}
372

373
bool fmIsElapsedFunc(int32_t funcId) {
11,698,728✔
374
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
11,698,728✔
375
    return false;
×
376
  }
377
  return FUNCTION_TYPE_ELAPSED == funcMgtBuiltins[funcId].type;
11,698,728✔
378
}
379

380
bool fmIsBlockDistFunc(int32_t funcId) {
638,489,631✔
381
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
638,489,631✔
382
    return false;
201,175✔
383
  }
384
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
638,292,934✔
385
}
386

387
bool fmIsDBUsageFunc(int32_t funcId) {
638,490,543✔
388
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
638,490,543✔
389
    return false;
205,448✔
390
  }
391
  return FUNCTION_TYPE_DB_USAGE == funcMgtBuiltins[funcId].type;
638,287,808✔
392
}
393

394
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
885,339,017✔
395

396
bool fmIsVolatileFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_VOLATILE_FUNC); }
951,929,303✔
397

398
bool fmIsNoPushdownFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_NO_PUSHDOWN_FUNC); }
54,915,797✔
399

400
bool fmIsIgnoreNullFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IGNORE_NULL_FUNC); }
305,403✔
401

402
void fmFuncMgtDestroy() {
1,662,086✔
403
  void* m = gFunMgtService.pFuncNameHashTable;
1,662,086✔
404
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
1,662,086✔
405
    taosHashCleanup(m);
1,662,019✔
406
  }
407
}
1,662,086✔
408

409
#ifdef BUILD_NO_CALL
410
int32_t fmSetNormalFunc(int32_t funcId, SFuncExecFuncs* pFpSet) {
411
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
412
    return TSDB_CODE_FAILED;
413
  }
414
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
415
  return TSDB_CODE_SUCCESS;
416
}
417
#endif
418

419
// function has same input/output type
420
bool fmIsSameInOutType(int32_t funcId) {
53,280,681✔
421
  bool res = false;
53,280,681✔
422
  switch (funcMgtBuiltins[funcId].type) {
53,280,681✔
423
    case FUNCTION_TYPE_MAX:
21,205,778✔
424
    case FUNCTION_TYPE_MIN:
425
    case FUNCTION_TYPE_TOP:
426
    case FUNCTION_TYPE_BOTTOM:
427
    case FUNCTION_TYPE_FIRST:
428
    case FUNCTION_TYPE_LAST:
429
    case FUNCTION_TYPE_SAMPLE:
430
    case FUNCTION_TYPE_TAIL:
431
    case FUNCTION_TYPE_UNIQUE:
432
      res = true;
21,205,778✔
433
      break;
21,205,778✔
434
    default:
32,074,967✔
435
      break;
32,074,967✔
436
  }
437
  return res;
53,280,745✔
438
}
439

440
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
15,696,033✔
441
  SNode* pNode;
442
  FOREACH(pNode, pFunc->pParameterList) {
15,789,009✔
443
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
171,909✔
444
      return false;
78,933✔
445
    }
446
  }
447
  return true;
15,617,100✔
448
}
449

450
bool fmIsSkipScanCheckFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SKIP_SCAN_CHECK_FUNC); }
47,531,682✔
451

452
bool fmIsPrimaryKeyFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PRIMARY_KEY_FUNC); }
553,808,999✔
453

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

456
bool fmIsPlaceHolderFuncForExternalWin(int32_t funcId) {
736,385✔
457
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
736,385✔
458
    return false;
×
459
  }
460
  return (funcMgtBuiltins[funcId].type == FUNCTION_TYPE_WSTART || funcMgtBuiltins[funcId].type == FUNCTION_TYPE_WEND ||
1,236,270✔
461
          funcMgtBuiltins[funcId].type == FUNCTION_TYPE_WDURATION);
499,885✔
462
}
463

464
void getLastCacheDataType(SDataType* pType, int32_t pkBytes) {
×
465
  // TODO: do it later.
466
  pType->bytes = getFirstLastInfoSize(pType->bytes, pkBytes) + VARSTR_HEADER_SIZE;
×
467
  pType->type = TSDB_DATA_TYPE_BINARY;
×
468
}
×
469

470
static int32_t getFuncInfo(SFunctionNode* pFunc) {
170,322,210✔
471
  char msg[128] = {0};
170,322,210✔
472
  return fmGetFuncInfo(pFunc, msg, sizeof(msg));
170,322,274✔
473
}
474

475
int32_t createFunction(const char* pName, SNodeList* pParameterList, SFunctionNode** ppFunc) {
11,045,051✔
476
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
11,045,051✔
477
  if (NULL == *ppFunc) {
11,045,051✔
478
    return code;
×
479
  }
480
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
11,045,051✔
481
  (*ppFunc)->pParameterList = pParameterList;
11,045,051✔
482
  code = getFuncInfo((*ppFunc));
11,045,051✔
483
  if (TSDB_CODE_SUCCESS != code) {
11,045,051✔
484
    (*ppFunc)->pParameterList = NULL;
×
485
    nodesDestroyNode((SNode*)*ppFunc);
×
486
    *ppFunc = NULL;
×
487
    return code;
×
488
  }
489
  return code;
11,045,051✔
490
}
491

492
static void resetOutputChangedFunc(SFunctionNode *pFunc, const SFunctionNode* pSrcFunc) {
159,279,347✔
493
  if (funcMgtBuiltins[pFunc->funcId].type == FUNCTION_TYPE_LAST_MERGE) {
159,279,347✔
494
    pFunc->node.resType = pSrcFunc->node.resType;
25,435,495✔
495
    return;
25,435,431✔
496
  }
497
}
498

499
int32_t createFunctionWithSrcFunc(const char* pName, const SFunctionNode* pSrcFunc, SNodeList* pParameterList, SFunctionNode** ppFunc) {
159,278,284✔
500
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
159,278,284✔
501
  if (NULL == *ppFunc) {
159,283,684✔
502
    return code;
×
503
  }
504

505
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
159,283,684✔
506
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
159,283,620✔
507
  (*ppFunc)->pSrcFuncRef = pSrcFunc;
159,283,684✔
508

509
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
159,283,620✔
510
  (*ppFunc)->pParameterList = pParameterList;
159,283,620✔
511
  code = getFuncInfo((*ppFunc));
159,283,492✔
512
  if (TSDB_CODE_SUCCESS != code) {
159,279,874✔
513
    (*ppFunc)->pParameterList = NULL;
×
514
    nodesDestroyNode((SNode*)*ppFunc);
×
515
    *ppFunc = NULL;
×
516
    return code;
×
517
  }
518
  resetOutputChangedFunc(*ppFunc, pSrcFunc);
159,279,874✔
519
  (*ppFunc)->node.relatedTo = pSrcFunc->node.relatedTo;
159,281,440✔
520
  (*ppFunc)->node.bindExprID = pSrcFunc->node.bindExprID;
159,281,504✔
521
  return code;
159,281,634✔
522
}
523

524
static int32_t createColumnByFunc(const SFunctionNode* pFunc, SColumnNode** ppCol) {
105,988,842✔
525
  int32_t code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)ppCol);
105,988,842✔
526
  if (NULL == *ppCol) {
105,995,829✔
527
    return code;
×
528
  }
529
  tstrncpy((*ppCol)->colName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
105,995,829✔
530
  (*ppCol)->node.resType = pFunc->node.resType;
105,995,699✔
531
  return TSDB_CODE_SUCCESS;
105,995,829✔
532
}
533

534
bool fmIsDistExecFunc(int32_t funcId) {
406,990,416✔
535
  if (fmIsUserDefinedFunc(funcId)) {
406,990,416✔
536
    return false;
121,140✔
537
  }
538
  if (!fmIsVectorFunc(funcId)) {
406,894,626✔
539
    return true;
555,475✔
540
  }
541
  return (NULL != funcMgtBuiltins[funcId].pPartialFunc && NULL != funcMgtBuiltins[funcId].pMergeFunc);
406,345,990✔
542
}
543

544
static int32_t createPartialFunction(const SFunctionNode* pSrcFunc, SFunctionNode** pPartialFunc) {
53,282,799✔
545
  SNodeList* pParameterList = NULL;
53,282,799✔
546
  int32_t    code = nodesCloneList(pSrcFunc->pParameterList, &pParameterList);
53,282,863✔
547
  if (NULL == pParameterList) {
53,285,464✔
548
    return code;
×
549
  }
550
  code =
551
      createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pPartialFunc, pSrcFunc, pParameterList, pPartialFunc);
53,285,464✔
552
  if (TSDB_CODE_SUCCESS != code) {
53,284,659✔
553
    nodesDestroyList(pParameterList);
3,024✔
554
    return code;
×
555
  }
556
  (*pPartialFunc)->hasOriginalFunc = true;
53,281,635✔
557
  (*pPartialFunc)->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
53,281,569✔
558
  char name[TSDB_FUNC_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_POINTER_PRINT_BYTES + 1] = {0};
53,281,635✔
559

560
  int32_t len = snprintf(name, sizeof(name), "%s.%p", (*pPartialFunc)->functionName, pSrcFunc);
53,281,635✔
561
  if (taosHashBinary(name, len, sizeof(name)) < 0) {
53,284,979✔
562
    return TSDB_CODE_FAILED;
×
563
  }
564
  tstrncpy((*pPartialFunc)->node.aliasName, name, TSDB_COL_NAME_LEN);
53,284,979✔
565
  return TSDB_CODE_SUCCESS;
53,284,981✔
566
}
567

568
static int32_t createMergeFuncPara(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
105,987,898✔
569
                                   SNodeList** pParameterList) {
570
  SNode*  pRes = NULL;
105,987,898✔
571
  int32_t code = createColumnByFunc(pPartialFunc, (SColumnNode**)&pRes);
105,987,898✔
572
  if (TSDB_CODE_SUCCESS != code) {
105,994,410✔
573
    return code;
×
574
  }
575
  if (NULL != funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc) {
105,994,410✔
576
    return funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc(pSrcFunc->pParameterList, pRes, pParameterList);
901,926✔
577
  } else {
578
    return nodesListMakeStrictAppend(pParameterList, pRes);
105,092,356✔
579
  }
580
}
581

582
static int32_t createMidFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
52,705,657✔
583
                                 SFunctionNode** pMidFunc) {
584
  SNodeList*     pParameterList = NULL;
52,705,657✔
585
  SFunctionNode* pFunc = NULL;
52,705,721✔
586

587
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
52,705,721✔
588
  if (TSDB_CODE_SUCCESS == code) {
52,710,738✔
589
    if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){
52,710,977✔
590
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pSrcFunc, pParameterList, &pFunc);
5,904,290✔
591
    }else{
592
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
46,806,687✔
593
    }
594
  }
595
  if (TSDB_CODE_SUCCESS == code) {
52,710,656✔
596
    tstrncpy(pFunc->node.aliasName, pPartialFunc->node.aliasName, TSDB_COL_NAME_LEN);
52,711,278✔
597
  }
598

599
  if (TSDB_CODE_SUCCESS == code) {
52,710,658✔
600
    *pMidFunc = pFunc;
52,710,658✔
601
  } else {
602
    nodesDestroyList(pParameterList);
×
603
  }
604
  return code;
52,710,825✔
605
}
606

607
static int32_t createMergeFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
53,282,277✔
608
                                   SFunctionNode** pMergeFunc) {
609
  SNodeList*     pParameterList = NULL;
53,282,277✔
610
  SFunctionNode* pFunc = NULL;
53,282,277✔
611

612
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
53,282,277✔
613
  if (TSDB_CODE_SUCCESS == code) {
53,285,705✔
614
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
53,285,641✔
615
  }
616
  if (TSDB_CODE_SUCCESS == code) {
53,288,285✔
617
    pFunc->hasOriginalFunc = true;
53,284,759✔
618
    pFunc->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
53,284,759✔
619
    // overwrite function restype set by translate function
620
    if (fmIsSameInOutType(pSrcFunc->funcId)) {
53,284,695✔
621
      pFunc->node.resType = pSrcFunc->node.resType;
21,205,755✔
622
    }
623
    tstrncpy(pFunc->node.aliasName, pSrcFunc->node.aliasName, TSDB_COL_NAME_LEN);
53,280,710✔
624
  }
625

626
  if (TSDB_CODE_SUCCESS == code) {
53,284,302✔
627
    *pMergeFunc = pFunc;
53,284,302✔
628
  } else {
629
    nodesDestroyList(pParameterList);
×
630
  }
631
  return code;
53,284,716✔
632
}
633

634
int32_t fmGetDistMethod(const SFunctionNode* pFunc, SFunctionNode** pPartialFunc, SFunctionNode** pMidFunc,
53,279,828✔
635
                        SFunctionNode** pMergeFunc) {
636
  if (!fmIsDistExecFunc(pFunc->funcId)) {
53,279,828✔
637
    return TSDB_CODE_FAILED;
×
638
  }
639

640
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
53,285,577✔
641
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
53,282,210✔
642
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
52,707,772✔
643
  }
644
  if (TSDB_CODE_SUCCESS == code) {
53,285,397✔
645
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
53,285,526✔
646
  }
647

648
  if (TSDB_CODE_SUCCESS != code) {
53,284,760✔
649
    nodesDestroyNode((SNode*)*pPartialFunc);
×
650
    if (pMidFunc) nodesDestroyNode((SNode*)*pMidFunc);
×
651
    nodesDestroyNode((SNode*)*pMergeFunc);
×
652
  }
653

654
  return code;
53,284,955✔
655
}
656

657
const char* fmGetFuncName(int32_t funcId) {
363,546✔
658
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
363,546✔
659
    return "invalid function";
×
660
  }
661
  return funcMgtBuiltins[funcId].name;
363,546✔
662
}
663

664
/// @param [out] pStateFunc, not changed if error occured or no need to create state func
665
/// @retval 0 for succ, otherwise err occured
666
static int32_t fmCreateStateFunc(const SFunctionNode* pFunc, SFunctionNode** pStateFunc) {
9,498✔
667
  if (funcMgtBuiltins[pFunc->funcId].pStateFunc) {
9,498✔
668
    SNodeList* pParams = NULL;
9,498✔
669
    int32_t    code = nodesCloneList(pFunc->pParameterList, &pParams);
9,498✔
670
    if (!pParams) return code;
9,498✔
671
    code = createFunction(funcMgtBuiltins[pFunc->funcId].pStateFunc, pParams, pStateFunc);
9,498✔
672
    if (TSDB_CODE_SUCCESS != code) {
9,498✔
673
      nodesDestroyList(pParams);
×
674
      return code;
×
675
    }
676
    tstrncpy((*pStateFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
9,498✔
677
    tstrncpy((*pStateFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
9,498✔
678
  }
679
  return TSDB_CODE_SUCCESS;
9,498✔
680
}
681

682
bool fmIsTSMASupportedFunc(func_id_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC); }
1,543,906✔
683

684
bool fmIsRsmaSupportedFunc(func_id_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_RSMA_FUNC); }
145,678✔
685

686
int32_t fmCreateStateFuncs(SNodeList* pFuncs) {
4,163✔
687
  int32_t code;
688
  SNode*  pNode;
689
  char    buf[128] = {0};
4,163✔
690
  FOREACH(pNode, pFuncs) {
13,661✔
691
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
9,498✔
692
    code = fmGetFuncInfo(pFunc, buf, 128);
9,498✔
693
    if (code) break;
9,498✔
694
    if (fmIsTSMASupportedFunc(pFunc->funcId)) {
9,498✔
695
      SFunctionNode* pNewFunc = NULL;
9,498✔
696
      code = fmCreateStateFunc(pFunc, &pNewFunc);
9,498✔
697
      if (code) {
9,498✔
698
        // error
699
        break;
×
700
      } else if (!pNewFunc) {
9,498✔
701
        // no need state func
702
        continue;
×
703
      } else {
704
        REPLACE_NODE(pNewFunc);
9,498✔
705
        nodesDestroyNode(pNode);
9,498✔
706
      }
707
    }
708
  }
709
  return code;
4,163✔
710
}
711

712
static int32_t fmCreateStateMergeFunc(SFunctionNode* pFunc, SFunctionNode** pStateMergeFunc) {
2,101✔
713
  if (funcMgtBuiltins[pFunc->funcId].pMergeFunc) {
2,101✔
714
    SNodeList* pParams = NULL;
1,719✔
715
    int32_t    code = nodesCloneList(pFunc->pParameterList, &pParams);
1,719✔
716
    if (!pParams) return code;
1,719✔
717
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pFunc->funcId].pMergeFunc, pFunc, pParams, pStateMergeFunc);
1,719✔
718
    if (TSDB_CODE_SUCCESS != code) {
1,719✔
719
      nodesDestroyList(pParams);
×
720
      return code;
×
721
    }
722
    tstrncpy((*pStateMergeFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
1,719✔
723
    tstrncpy((*pStateMergeFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
1,719✔
724
  }
725
  return TSDB_CODE_SUCCESS;
2,101✔
726
}
727

728
int32_t fmCreateStateMergeFuncs(SNodeList* pFuncs) {
955✔
729
  int32_t code;
730
  SNode*  pNode;
731
  char    buf[128] = {0};
955✔
732
  FOREACH(pNode, pFuncs) {
3,056✔
733
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
2,101✔
734
    if (fmIsTSMASupportedFunc(pFunc->funcId)) {
2,101✔
735
      SFunctionNode* pNewFunc = NULL;
2,101✔
736
      code = fmCreateStateMergeFunc(pFunc, &pNewFunc);
2,101✔
737
      if (code) {
2,101✔
738
        // error
739
        break;
×
740
      } else if (!pNewFunc) {
2,101✔
741
        // no state merge func
742
        continue;
382✔
743
      } else {
744
        REPLACE_NODE(pNewFunc);
1,719✔
745
        nodesDestroyNode(pNode);
1,719✔
746
      }
747
    }
748
  }
749
  return code;
955✔
750
}
751

752
int32_t fmGetFuncId(const char* name) {
8,864,535✔
753
  if (NULL != gFunMgtService.pFuncNameHashTable) {
8,864,535✔
754
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, name, strlen(name));
8,864,535✔
755
    if (NULL != pVal) {
8,864,535✔
756
      return *(int32_t*)pVal;
8,864,535✔
757
    }
758
    return -1;
×
759
  }
760
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
×
761
    if (0 == strcmp(funcMgtBuiltins[i].name, name)) {
×
762
      return i;
×
763
    }
764
  }
765
  return -1;
×
766
}
767

768
int32_t fmGetTwstartFuncId() {
1,864,516✔
769
  static int32_t twstartFuncId = -2;
770
  if (twstartFuncId < 0) {
1,864,516✔
771
    twstartFuncId = fmGetFuncId("_twstart");
1,662,019✔
772
  }
773
  return twstartFuncId;
1,864,516✔
774
}
775

776
int32_t fmGetTwendFuncId() {
1,698,154✔
777
  static int32_t twendFuncId = -2;
778
  if (twendFuncId < 0) {
1,698,154✔
779
    twendFuncId = fmGetFuncId("_twend");
1,662,019✔
780
  }
781
  return twendFuncId;
1,698,154✔
782
}
783

784
int32_t fmGetTwdurationFuncId() {
1,662,471✔
785
  static int32_t twdurationFuncId = -2;
786
  if (twdurationFuncId < 0) {
1,662,471✔
787
    twdurationFuncId = fmGetFuncId("_twduration");
1,662,019✔
788
  }
789
  return twdurationFuncId;
1,662,471✔
790
}
791

792
int32_t fmGetExternalWindowColumnFuncId() {
1,710,101✔
793
  static int32_t externalWindowColumnFuncId = -2;
794
  if (externalWindowColumnFuncId < 0) {
1,710,101✔
795
    externalWindowColumnFuncId = fmGetFuncId("_external_window_column");
1,662,019✔
796
  }
797
  return externalWindowColumnFuncId;
1,710,101✔
798
}
799

800
bool fmIsMyStateFunc(int32_t funcId, int32_t stateFuncId) {
2,552,142✔
801
  const SBuiltinFuncDefinition* pFunc = &funcMgtBuiltins[funcId];
2,552,142✔
802
  const SBuiltinFuncDefinition* pStateFunc = &funcMgtBuiltins[stateFuncId];
2,552,142✔
803
  if (!pFunc->pStateFunc) {
2,552,142✔
804
    return false;
×
805
  }
806
  if (strcmp(pFunc->pStateFunc, pStateFunc->name) == 0) return true;
2,552,142✔
807
  int32_t stateMergeFuncId = fmGetFuncId(pFunc->pStateFunc);
930,743✔
808
  if (stateMergeFuncId == -1) {
930,743✔
809
    return false;
×
810
  }
811
  const SBuiltinFuncDefinition* pStateMergeFunc = &funcMgtBuiltins[stateMergeFuncId];
930,743✔
812
  return strcmp(pStateFunc->name, pStateMergeFunc->pMergeFunc) == 0;
930,743✔
813
}
814

815
bool fmIsCountLikeFunc(int32_t funcId) {
676,105,160✔
816
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
676,105,160✔
817
}
818

819
bool fmIsRowTsOriginFunc(int32_t funcId) {
5,793,314✔
820
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
5,793,314✔
821
    return false;
×
822
  }
823
  return FUNCTION_TYPE_IROWTS_ORIGIN == funcMgtBuiltins[funcId].type;
5,793,314✔
824
}
825

826
bool fmIsGroupIdFunc(int32_t funcId) {
2,541,332✔
827
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,541,332✔
828
    return false;
×
829
  }
830
  return FUNCTION_TYPE_GROUP_ID == funcMgtBuiltins[funcId].type;
2,541,332✔
831
}
832

833
const void* fmGetExternalWindowColumnFuncVal(const SStreamRuntimeFuncInfo* pStreamRuntimeFuncInfo, int32_t index) {
11,329,002✔
834
  SSTriggerCalcParam *pParams = taosArrayGet(pStreamRuntimeFuncInfo->pStreamPesudoFuncVals, pStreamRuntimeFuncInfo->curIdx);
11,329,002✔
835
  if (pParams == NULL || pParams->pExternalWindowData == NULL || index < 0) {
11,339,237✔
836
    return NULL;
×
837
  }
838
  return taosArrayGet(pParams->pExternalWindowData, index);
11,338,792✔
839
}
840

841
const void* fmGetStreamPesudoFuncVal(int32_t funcId, const SStreamRuntimeFuncInfo* pStreamRuntimeFuncInfo) {
438,400,143✔
842
  switch (funcMgtBuiltins[funcId].type) {
438,400,143✔
843
    case FUNCTION_TYPE_TGRPID:
151,541✔
844
      return &pStreamRuntimeFuncInfo->groupId;
151,541✔
845
    case FUNCTION_TYPE_PLACEHOLDER_COLUMN:
1,042,513✔
846
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
1,042,513✔
847
    case FUNCTION_TYPE_PLACEHOLDER_TBNAME:
2,244,064✔
848
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
2,244,064✔
849
    default:
435,015,021✔
850
      break;
435,015,021✔
851
  }
852

853
  SSTriggerCalcParam *pParams = taosArrayGet(pStreamRuntimeFuncInfo->pStreamPesudoFuncVals, pStreamRuntimeFuncInfo->curIdx);
435,015,021✔
854
  switch (funcMgtBuiltins[funcId].type) {
435,062,285✔
855
    case FUNCTION_TYPE_TPREV_TS:
×
856
      return &pParams->prevTs;
×
857
    case FUNCTION_TYPE_TCURRENT_TS:
2,280,327✔
858
      return &pParams->currentTs;
2,280,327✔
859
    case FUNCTION_TYPE_TNEXT_TS:
×
860
      return &pParams->nextTs;
×
861
    case FUNCTION_TYPE_TWSTART:
375,476,610✔
862
      return &pParams->wstart;
375,476,610✔
863
    case FUNCTION_TYPE_TWEND:
20,050,254✔
864
      return &pParams->wend;
20,050,254✔
865
    case FUNCTION_TYPE_TWDURATION:
1,383,943✔
866
      return &pParams->wduration;
1,383,943✔
867
    case FUNCTION_TYPE_TWROWNUM:
34,428,422✔
868
      return &pParams->wrownum;
34,428,422✔
869
    case FUNCTION_TYPE_TPREV_LOCALTIME:
270,379✔
870
      return &pParams->prevLocalTime;
270,379✔
871
    case FUNCTION_TYPE_TLOCALTIME:
793,705✔
872
      return &pParams->triggerTime;
793,705✔
873
    case FUNCTION_TYPE_TNEXT_LOCALTIME:
357,109✔
874
      return &pParams->nextLocalTime;
357,109✔
875
    case FUNCTION_TYPE_TGRPID:
×
876
      return &pStreamRuntimeFuncInfo->groupId;
×
877
    case FUNCTION_TYPE_PLACEHOLDER_COLUMN:
×
878
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
×
879
    case FUNCTION_TYPE_PLACEHOLDER_TBNAME:
×
880
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
×
881
    case FUNCTION_TYPE_TIDLESTART:
5,184✔
882
      return &pParams->idlestart;
5,184✔
883
    case FUNCTION_TYPE_TIDLEEND:
5,184✔
884
      return &pParams->idleend;
5,184✔
885
    default:
×
886
      break;
×
887
  }
888
  return NULL;
×
889
}
890

891
bool fmIsStreamPesudoColVal(int32_t funcId) {
659,775✔
892
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
659,775✔
893
    return false;
8,970✔
894
  }
895
  return funcMgtBuiltins[funcId].type == FUNCTION_TYPE_PLACEHOLDER_COLUMN
650,805✔
896
         || funcMgtBuiltins[funcId].type == FUNCTION_TYPE_PLACEHOLDER_TBNAME;
650,805✔
897
}
898

899
int32_t fmGetStreamPesudoFuncEnv(int32_t funcId, SNodeList* pParamNodes, SFuncExecEnv *pEnv) {
8,440,855✔
900
  if (NULL == pParamNodes || pParamNodes->length < 1) {
8,440,855✔
901
    uError("invalid stream pesudo func param list %p", pParamNodes);
×
902
    return TSDB_CODE_INTERNAL_ERROR;
×
903
  }
904

905
  int32_t firstParamType = nodeType(nodesListGetNode(pParamNodes, 0));
8,441,419✔
906
  if (QUERY_NODE_VALUE != firstParamType) {
8,442,245✔
907
    uError("invalid stream pesudo func first param type %d", firstParamType);
×
908
    return TSDB_CODE_INTERNAL_ERROR;
×
909
  }
910

911
  SValueNode* pResDesc = (SValueNode*)nodesListGetNode(pParamNodes, 0);
8,442,245✔
912
  pEnv->calcMemSize = pResDesc->node.resType.bytes;
8,442,245✔
913

914
  return TSDB_CODE_SUCCESS;
8,442,245✔
915
}
916

917

918
int32_t fmSetStreamPseudoFuncParamVal(int32_t funcId, SNodeList* pParamNodes, const SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
449,636,985✔
919
  if (!pStreamRuntimeInfo) {
449,636,985✔
920
    uError("internal error, should have pVals for stream pseudo funcs");
×
921
    return TSDB_CODE_INTERNAL_ERROR;
×
922
  }
923
  int32_t code = 0;
449,636,985✔
924
  SArray *pVals1 = NULL;
449,636,985✔
925
  SNode* pFirstParam = nodesListGetNode(pParamNodes, 0);
449,636,985✔
926
  if (nodeType(pFirstParam) != QUERY_NODE_VALUE) {
449,784,854✔
927
    uError("invalid param node type: %d for func: %d", nodeType(pFirstParam), funcId);
×
928
    return TSDB_CODE_INTERNAL_ERROR;
×
929
  }
930

931
  int32_t t = funcMgtBuiltins[funcId].type;
449,777,228✔
932
  uDebug("set stream pseudo func param val, functype: %d, pStreamRuntimeInfo: %p", t, pStreamRuntimeInfo);
449,786,373✔
933
  if (FUNCTION_TYPE_TGRPID == t) {
449,781,377✔
934
    SValue v = {0};
151,541✔
935
    v.type = TSDB_DATA_TYPE_BIGINT;
151,541✔
936
    v.val = *(int64_t*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
151,541✔
937
    
938
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&v, pFirstParam->type));
151,541✔
939
    if (code != 0) {
151,541✔
940
      uError("failed to set value node value: %s", tstrerror(code));
×
941
      return code;
×
942
    }
943
  } else if (FUNCTION_TYPE_PLACEHOLDER_COLUMN == t) {
449,629,836✔
944
    SNode* pSecondParam = nodesListGetNode(pParamNodes, 1);
1,042,513✔
945
    if (nodeType(pSecondParam) != QUERY_NODE_VALUE) {
1,042,513✔
946
      uError("invalid param node type: %d for func: %d", nodeType(pSecondParam), funcId);
×
947
      return TSDB_CODE_INTERNAL_ERROR;
×
948
    }
949
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
1,042,513✔
950
    int32_t idx = ((SValueNode*)pSecondParam)->datum.i;
1,042,513✔
951
    if (idx - 1 < 0 || idx - 1 >= taosArrayGetSize(pVal)) {
1,042,513✔
952
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
×
953
      return TSDB_CODE_INTERNAL_ERROR;
×
954
    }
955
    SStreamGroupValue* pValue = taosArrayGet(pVal, idx - 1);
1,042,513✔
956
    if (pValue == NULL) {
1,042,513✔
957
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
×
958
      return TSDB_CODE_INTERNAL_ERROR;
×
959
    }
960
    if (!pValue->isNull){
1,042,513✔
961
      if (pValue->data.type != ((SValueNode*)pFirstParam)->node.resType.type){
1,034,778✔
962
        uError("invalid value type: %d for func: %d, should be: %d", pValue->data.type, funcId, ((SValueNode*)pFirstParam)->node.resType.type);
×
963
        return TSDB_CODE_INTERNAL_ERROR;
×
964
      }
965
      if (IS_VAR_DATA_TYPE(((SValueNode*)pFirstParam)->node.resType.type)) {
1,034,778✔
966
        char* tmp = taosMemoryCalloc(1, pValue->data.nData + VARSTR_HEADER_SIZE); 
706,848✔
967
        if (tmp == NULL) {
706,848✔
968
          return TSDB_CODE_OUT_OF_MEMORY;
×
969
        }
970
        taosMemoryFree(((SValueNode*)pFirstParam)->datum.p);
706,848✔
971
        ((SValueNode*)pFirstParam)->datum.p = tmp;
706,848✔
972
        memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
706,848✔
973
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
706,848✔
974
      } else {
975
        code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&pValue->data, pValue->data.type));
327,930✔
976
      }
977
    }
978
    ((SValueNode*)pFirstParam)->isNull = pValue->isNull;
1,042,513✔
979
  } else if (FUNCTION_TYPE_PLACEHOLDER_TBNAME == t) {
448,587,323✔
980
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
2,197,256✔
981
    for (int32_t i = 0; i < taosArrayGetSize(pVal); ++i) {
2,745,075✔
982
      SStreamGroupValue* pValue = taosArrayGet(pVal, i);
2,745,393✔
983
      if (pValue != NULL && pValue->isTbname) {
2,745,393✔
984
        taosMemoryFreeClear(((SValueNode*)pFirstParam)->datum.p);
2,198,193✔
985
        ((SValueNode*)pFirstParam)->datum.p = taosMemoryCalloc(pValue->data.nData + VARSTR_HEADER_SIZE, 1);
2,197,560✔
986
        if (NULL == ((SValueNode*)pFirstParam)->datum.p ) {
2,197,876✔
987
          return terrno;
×
988
        }
989

990
        (void)memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
2,196,938✔
991
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
2,197,876✔
992
        break;
2,197,875✔
993
      }
994
    }
995
  } else if(FUNCTION_TYPE_EXTERNAL_WINDOW_COLUMN == t) {
446,390,067✔
996
    if (NULL == pParamNodes || LIST_LENGTH(pParamNodes) < 2) {
11,331,227✔
997
      uError("invalid stream external window column param list %p, len: %d", pParamNodes,
×
998
             pParamNodes ? LIST_LENGTH(pParamNodes) : 0);
999
      return TSDB_CODE_INTERNAL_ERROR;
×
1000
    }
1001

1002
    SNode* pParamNode = nodesListGetNode(pParamNodes, 1);
11,337,902✔
1003
    if (NULL == pParamNode || QUERY_NODE_VALUE != nodeType(pParamNode)) {
11,335,232✔
1004
      uError("invalid stream external window column param node %p type %d for func: %d", pParamNode,
6,230✔
1005
             pParamNode ? nodeType(pParamNode) : -1, funcId);
1006
      return TSDB_CODE_INTERNAL_ERROR;
×
1007
    }
1008

1009
    const SStreamGroupValue* pVal =
1010
        fmGetExternalWindowColumnFuncVal(pStreamRuntimeInfo, ((SValueNode*)pParamNode)->placeholderNo);
11,330,337✔
1011
    if (!pVal) {
11,339,682✔
1012
      uError("failed to set stream pseudo func param val, NULL val for funcId: %d", funcId);
×
1013
      return TSDB_CODE_INTERNAL_ERROR;
×
1014
    }
1015
    if (pVal->isNull) {
11,339,682✔
1016
      ((SValueNode*)pFirstParam)->isNull = true;
890✔
1017
    } else {
1018
      ((SValueNode*)pFirstParam)->isNull = false;
11,338,792✔
1019
      if (IS_VAR_DATA_TYPE(pVal->data.type)) {
11,338,792✔
1020
        // pVal->data.pData already includes the VARSTR header from colDataGetData,
1021
        // so copy it directly as datum.p expects VARSTR format [header][raw data].
1022
        char* tmp = taosMemoryMalloc(pVal->data.nData);
10,247✔
1023
        if (tmp == NULL) {
9,802✔
1024
          return TSDB_CODE_OUT_OF_MEMORY;
×
1025
        }
1026
        taosMemoryFree(((SValueNode*)pFirstParam)->datum.p);
9,802✔
1027
        ((SValueNode*)pFirstParam)->datum.p = tmp;
9,802✔
1028
        memcpy(tmp, pVal->data.pData, pVal->data.nData);
9,802✔
1029
      } else {
1030
        code = nodesSetValueNodeValue((SValueNode*)pFirstParam, (void*)&pVal->data.val);
11,328,545✔
1031
      }
1032
    }
1033
    if (code != 0) {
11,337,457✔
1034
      uError("failed to set value node value: %s", tstrerror(code));
×
1035
      return code;
×
1036
    }
1037
  } else if (LIST_LENGTH(pParamNodes) == 1) {
870,118,825✔
1038
    // twstart, twend
1039
    const void* pVal = fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
435,058,296✔
1040
    if (!pVal) {
435,007,840✔
1041
      uError("failed to set stream pseudo func param val, NULL val for funcId: %d", funcId);
×
1042
      return TSDB_CODE_INTERNAL_ERROR;
×
1043
    }
1044
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, (void*)pVal);
435,007,840✔
1045
    if (code != 0) {
435,065,592✔
1046
      uError("failed to set value node value: %s", tstrerror(code));
5,607✔
1047
      return code;
×
1048
    }
1049
  } else {
1050
    uError("invalid placeholder function type: %d", t);
64,211✔
1051
    return TSDB_CODE_INTERNAL_ERROR;
×
1052
  }
1053
  
1054
  return code;
449,789,299✔
1055
}
1056

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

© 2026 Coveralls, Inc