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

taosdata / TDengine / #5052

13 May 2026 12:00PM UTC coverage: 73.338% (-0.02%) from 73.358%
#5052

push

travis-ci

web-flow
feat: taosdump support stream backup/restore (#35326)

139 of 170 new or added lines in 3 files covered. (81.76%)

761 existing lines in 163 files now uncovered.

281469 of 383795 relevant lines covered (73.34%)

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

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

52
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
380,482,243✔
53
    if (TSDB_CODE_SUCCESS != taosHashPut(gFunMgtService.pFuncNameHashTable, funcMgtBuiltins[i].name,
378,849,272✔
54
                                         strlen(funcMgtBuiltins[i].name), &i, sizeof(int32_t))) {
378,849,272✔
55
      initFunctionCode = terrno;
×
56
      return;
×
57
    }
58
  }
59
  InitSpecialFuncId();
1,632,971✔
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,926,101✔
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,632,971✔
75
  (void)taosThreadOnce(&functionHashTableInit, doInitFunctionTable);
1,632,971✔
76
  return initFunctionCode;
1,632,971✔
77
}
78

79
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
887,117,212✔
80
  if (NULL != gFunMgtService.pFuncNameHashTable) {
887,117,212✔
81
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
886,950,542✔
82
    if (NULL != pVal) {
886,981,456✔
83
      pFunc->funcId = *(int32_t*)pVal;
886,756,496✔
84
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
886,756,148✔
85
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
886,755,568✔
86
    }
87
    return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
224,960✔
88
  }
89
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
3,446,773✔
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,234,095✔
100
  if (NULL != funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc) {
19,234,095✔
101
    return funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc(pFunc);
13,550,956✔
102
  }
103
  return (fmIsIndefiniteRowsFunc(pFunc->funcId) || fmIsMultiRowsFunc(pFunc->funcId)) ? FUNC_RETURN_ROWS_INDEFINITE
5,682,113✔
104
                                                                                     : FUNC_RETURN_ROWS_NORMAL;
5,682,113✔
105
}
106

107
bool canCoexistIndefiniteRowsFunc(int32_t funcId1, int32_t funcId2) {
194,146✔
108
  if (fmIsUserDefinedFunc(funcId1) || funcId1 < 0 || funcId1 >= funcMgtBuiltinsNum ||
388,292✔
109
      fmIsUserDefinedFunc(funcId2) || funcId2 < 0 || funcId2 >= funcMgtBuiltinsNum) {
388,292✔
110
    return false;
×
111
  }
112
  if (funcId1 == funcId2) {
194,146✔
113
    return true;
177,500✔
114
  }
115
  if ((funcMgtBuiltins[funcId1].type == FUNCTION_TYPE_LAG || funcMgtBuiltins[funcId1].type == FUNCTION_TYPE_LEAD) &&
16,646✔
116
      (funcMgtBuiltins[funcId2].type == FUNCTION_TYPE_LAG || funcMgtBuiltins[funcId2].type == FUNCTION_TYPE_LEAD)) {
16,165✔
117
    return true;
16,165✔
118
  }
119
  return false;
481✔
120
}
121

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

126
EFunctionType fmGetFuncType(const char* pFunc) {
641,195,826✔
127
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
641,195,826✔
128
  if (NULL != pVal) {
641,217,638✔
129
    return funcMgtBuiltins[*(int32_t*)pVal].type;
640,988,482✔
130
  }
131
  return FUNCTION_TYPE_UDF;
229,156✔
132
}
133

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

141
EFuncDataRequired fmFuncDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
42,815,978✔
142
  if (fmIsUserDefinedFunc(pFunc->funcId) || pFunc->funcId < 0 || pFunc->funcId >= funcMgtBuiltinsNum) {
42,815,978✔
143
    return FUNC_DATA_REQUIRED_DATA_LOAD;
3,167✔
144
  }
145
  if (NULL == funcMgtBuiltins[pFunc->funcId].dataRequiredFunc) {
42,820,224✔
146
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
147
  }
148
  return funcMgtBuiltins[pFunc->funcId].dataRequiredFunc(pFunc, pTimeWindow);
42,820,040✔
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;
913,194✔
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;
591,291✔
159
    ;
160
  }
161

162
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
2,147,483,647✔
163
    return FUNC_DATA_REQUIRED_DATA_LOAD;
203,200,294✔
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) {
304,313,774✔
170
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
304,313,774✔
171
    return TSDB_CODE_FAILED;
59,436✔
172
  }
173
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
304,282,076✔
174
  pFpSet->init = funcMgtBuiltins[funcId].initFunc;
304,241,234✔
175
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
304,275,097✔
176
  pFpSet->finalize = funcMgtBuiltins[funcId].finalizeFunc;
304,239,666✔
177
  pFpSet->combine = funcMgtBuiltins[funcId].combineFunc;
304,243,428✔
178
  pFpSet->processFuncByRow = funcMgtBuiltins[funcId].processFuncByRow;
304,243,203✔
179
  pFpSet->cleanup = funcMgtBuiltins[funcId].cleanupFunc;
304,299,357✔
180
  return TSDB_CODE_SUCCESS;
304,280,672✔
181
}
182

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

198
int32_t fmGetScalarFuncExecFuncs(int32_t funcId, SScalarFuncExecFuncs* pFpSet) {
1,205,393,771✔
199
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,205,393,771✔
200
    return TSDB_CODE_FAILED;
168,477✔
201
  }
202
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
1,205,430,082✔
203
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
1,205,459,222✔
204
  return TSDB_CODE_SUCCESS;
1,205,485,221✔
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,376,473,076✔
212

213
bool fmIsSelectColsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_COLS_FUNC); }
161,590,115✔
214

215
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
1,999,014,763✔
216

217
bool fmIsTimelineFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TIMELINE_FUNC); }
1,241,164,922✔
218

219
bool fmIsDateTimeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_DATETIME_FUNC); }
627,871,885✔
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,516,826,770✔
226

227
bool fmIsWindowClauseFunc(int32_t funcId) { return fmIsAggFunc(funcId) || fmIsWindowPseudoColumnFunc(funcId); }
63,621,165✔
228

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

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

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

235
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
1,688,582,927✔
236

237
bool fmIsSpecialDataRequiredFunc(int32_t funcId) {
97,486,178✔
238
  return isSpecificClassifyFunc(funcId, FUNC_MGT_SPECIAL_DATA_REQUIRED);
97,486,178✔
239
}
240

241
bool fmIsDynamicScanOptimizedFunc(int32_t funcId) {
46,825,823✔
242
  return isSpecificClassifyFunc(funcId, FUNC_MGT_DYNAMIC_SCAN_OPTIMIZED);
46,825,823✔
243
}
244

245
bool fmIsMultiResFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_RES_FUNC); }
1,277,662,796✔
246

247
bool fmIsRepeatScanFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_REPEAT_SCAN_FUNC); }
1,254,673,180✔
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); }
628,186,803✔
252

253
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
63,475,592✔
254

255
bool fmIsSystemInfoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SYSTEM_INFO_FUNC); }
631,449,106✔
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); }
639,666,379✔
260

261
bool fmIsMultiRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_ROWS_FUNC); }
1,251,431,991✔
262

263
static bool fmIsKeepOrderFuncId(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_KEEP_ORDER_FUNC); }
755,601,302✔
264

265
bool fmIsKeepOrderFunc(SFunctionNode* pFunc) {
762,516,693✔
266
  if (pFunc->funcType == FUNCTION_TYPE_TOP || pFunc->funcType == FUNCTION_TYPE_BOTTOM ||
762,516,693✔
267
      pFunc->funcType == FUNCTION_TYPE_SAMPLE) {
758,157,423✔
268
    if (pFunc->pParameterList != NULL && pFunc->pParameterList->length >= 2) {
6,909,473✔
269
      SNode* pParam = nodesListGetNode(pFunc->pParameterList, 1);
6,926,487✔
270
      if (pParam != NULL && nodeType(pParam) == QUERY_NODE_VALUE) {
6,926,487✔
271
        SValueNode* pConst = (SValueNode*)pParam;
6,926,487✔
272
        if (pConst->datum.i == 1) {
6,926,487✔
273
          return true;
813,635✔
274
        }
275
      }
276
    }
277
    return false;
6,103,346✔
278
  }
279
  return fmIsKeepOrderFuncId(pFunc->funcId);
755,618,290✔
280
}
281

282
bool fmIsCumulativeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CUMULATIVE_FUNC); }
164,281,521✔
283

284
bool fmIsForbidSysTableFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_SYSTABLE_FUNC); }
628,082,809✔
285

286
bool fmIsInterpFunc(int32_t funcId) {
1,287,244,524✔
287
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,287,244,524✔
288
    return false;
416,531✔
289
  }
290
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
1,286,832,097✔
291
}
292

293
bool fmIsInterpPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERP_PC_FUNC); }
1,265,911,461✔
294

295
bool fmIsForecastFunc(int32_t funcId) {
1,234,669,477✔
296
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,234,669,477✔
297
    return false;
420,588✔
298
  }
299
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
1,234,251,785✔
300
}
301

302
bool fmIsAnalysisPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_ANALYTICS_PC_FUNC); }
1,268,983,519✔
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) {
74,056,809✔
315
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
74,056,809✔
316
    return false;
9✔
317
  }
318
  return FUNCTION_TYPE_LAST_ROW == funcMgtBuiltins[funcId].type;
74,056,948✔
319
}
320

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

328
bool fmIsNotNullOutputFunc(int32_t funcId) {
462,527,324✔
329
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
462,527,324✔
330
    return false;
113,921✔
331
  }
332
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
462,420,234✔
333
         FUNCTION_TYPE_CACHE_LAST == funcMgtBuiltins[funcId].type ||
446,307,105✔
334
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
444,882,226✔
335
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
435,350,236✔
336
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
431,507,306✔
337
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
421,685,792✔
338
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
416,180,682✔
339
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
414,196,865✔
340
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
357,173,173✔
341
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
1,261,762,767✔
342
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
352,976,903✔
343
}
344

345
bool fmIsSelectValueFunc(int32_t funcId) {
378,357,370✔
346
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
378,357,370✔
347
    return false;
×
348
  }
349
  return FUNCTION_TYPE_SELECT_VALUE == funcMgtBuiltins[funcId].type;
378,358,434✔
350
}
351

352
bool fmIsGroupKeyFunc(int32_t funcId) {
291,296,034✔
353
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
291,296,034✔
354
    return false;
×
355
  }
356
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
291,297,945✔
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) {
18,477,978✔
367
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
18,477,978✔
368
    return false;
×
369
  }
370
  return FUNCTION_TYPE_GROUP_CONST_VALUE == funcMgtBuiltins[funcId].type;
18,477,978✔
371
}
372

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

380
bool fmIsBlockDistFunc(int32_t funcId) {
627,858,126✔
381
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
627,858,126✔
382
    return false;
195,067✔
383
  }
384
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
627,668,532✔
385
}
386

387
bool fmIsDBUsageFunc(int32_t funcId) {
627,861,727✔
388
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
627,861,727✔
389
    return false;
204,916✔
390
  }
391
  return FUNCTION_TYPE_DB_USAGE == funcMgtBuiltins[funcId].type;
627,659,923✔
392
}
393

394
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
845,781,013✔
395

396
bool fmIsVolatileFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_VOLATILE_FUNC); }
913,067,937✔
397

398
bool fmIsNoPushdownFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_NO_PUSHDOWN_FUNC); }
29,924,703✔
399

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

402
void fmFuncMgtDestroy() {
1,632,183✔
403
  void* m = gFunMgtService.pFuncNameHashTable;
1,632,183✔
404
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
1,632,183✔
405
    taosHashCleanup(m);
1,632,118✔
406
  }
407
}
1,632,183✔
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) {
51,804,738✔
421
  bool res = false;
51,804,738✔
422
  switch (funcMgtBuiltins[funcId].type) {
51,804,738✔
423
    case FUNCTION_TYPE_MAX:
20,670,118✔
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;
20,670,118✔
433
      break;
20,670,118✔
434
    default:
31,134,620✔
435
      break;
31,134,620✔
436
  }
437
  return res;
51,804,738✔
438
}
439

440
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
15,475,480✔
441
  SNode* pNode;
442
  FOREACH(pNode, pFunc->pParameterList) {
15,568,300✔
443
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
164,451✔
444
      return false;
71,631✔
445
    }
446
  }
447
  return true;
15,403,849✔
448
}
449

450
bool fmIsSkipScanCheckFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SKIP_SCAN_CHECK_FUNC); }
46,821,991✔
451

452
bool fmIsPrimaryKeyFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PRIMARY_KEY_FUNC); }
546,147,760✔
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) {
738,155✔
457
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
738,155✔
458
    return false;
×
459
  }
460
  return (funcMgtBuiltins[funcId].type == FUNCTION_TYPE_WSTART || funcMgtBuiltins[funcId].type == FUNCTION_TYPE_WEND ||
1,239,190✔
461
          funcMgtBuiltins[funcId].type == FUNCTION_TYPE_WDURATION);
501,035✔
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) {
165,790,939✔
471
  char msg[128] = {0};
165,790,939✔
472
  return fmGetFuncInfo(pFunc, msg, sizeof(msg));
165,790,999✔
473
}
474

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

492
static void resetOutputChangedFunc(SFunctionNode *pFunc, const SFunctionNode* pSrcFunc) {
154,917,033✔
493
  if (funcMgtBuiltins[pFunc->funcId].type == FUNCTION_TYPE_LAST_MERGE) {
154,917,033✔
494
    pFunc->node.resType = pSrcFunc->node.resType;
24,757,365✔
495
    return;
24,757,365✔
496
  }
497
}
498

499
int32_t createFunctionWithSrcFunc(const char* pName, const SFunctionNode* pSrcFunc, SNodeList* pParameterList, SFunctionNode** ppFunc) {
154,917,947✔
500
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
154,917,947✔
501
  if (NULL == *ppFunc) {
154,923,770✔
502
    return code;
×
503
  }
504

505
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
154,923,770✔
506
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
154,923,770✔
507
  (*ppFunc)->pSrcFuncRef = pSrcFunc;
154,923,770✔
508

509
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
154,923,770✔
510
  (*ppFunc)->pParameterList = pParameterList;
154,923,710✔
511
  code = getFuncInfo((*ppFunc));
154,923,710✔
512
  if (TSDB_CODE_SUCCESS != code) {
154,918,818✔
513
    (*ppFunc)->pParameterList = NULL;
×
514
    nodesDestroyNode((SNode*)*ppFunc);
×
515
    *ppFunc = NULL;
×
516
    return code;
×
517
  }
518
  resetOutputChangedFunc(*ppFunc, pSrcFunc);
154,918,818✔
519
  (*ppFunc)->node.relatedTo = pSrcFunc->node.relatedTo;
154,920,026✔
520
  (*ppFunc)->node.bindExprID = pSrcFunc->node.bindExprID;
154,920,026✔
521
  return code;
154,920,086✔
522
}
523

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

534
bool fmIsDistExecFunc(int32_t funcId) {
397,123,936✔
535
  if (fmIsUserDefinedFunc(funcId)) {
397,123,936✔
536
    return false;
121,608✔
537
  }
538
  if (!fmIsVectorFunc(funcId)) {
397,031,650✔
539
    return true;
553,167✔
540
  }
541
  return (NULL != funcMgtBuiltins[funcId].pPartialFunc && NULL != funcMgtBuiltins[funcId].pMergeFunc);
396,493,425✔
542
}
543

544
static int32_t createPartialFunction(const SFunctionNode* pSrcFunc, SFunctionNode** pPartialFunc) {
51,806,446✔
545
  SNodeList* pParameterList = NULL;
51,806,446✔
546
  int32_t    code = nodesCloneList(pSrcFunc->pParameterList, &pParameterList);
51,806,446✔
547
  if (NULL == pParameterList) {
51,810,837✔
548
    return code;
×
549
  }
550
  code =
551
      createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pPartialFunc, pSrcFunc, pParameterList, pPartialFunc);
51,810,837✔
552
  if (TSDB_CODE_SUCCESS != code) {
51,809,051✔
553
    nodesDestroyList(pParameterList);
3,692✔
554
    return code;
×
555
  }
556
  (*pPartialFunc)->hasOriginalFunc = true;
51,805,359✔
557
  (*pPartialFunc)->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
51,805,359✔
558
  char name[TSDB_FUNC_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_POINTER_PRINT_BYTES + 1] = {0};
51,805,359✔
559

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

568
static int32_t createMergeFuncPara(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
103,101,411✔
569
                                   SNodeList** pParameterList) {
570
  SNode*  pRes = NULL;
103,101,411✔
571
  int32_t code = createColumnByFunc(pPartialFunc, (SColumnNode**)&pRes);
103,101,411✔
572
  if (TSDB_CODE_SUCCESS != code) {
103,109,258✔
573
    return code;
×
574
  }
575
  if (NULL != funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc) {
103,109,258✔
576
    return funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc(pSrcFunc->pParameterList, pRes, pParameterList);
893,162✔
577
  } else {
578
    return nodesListMakeStrictAppend(pParameterList, pRes);
102,216,036✔
579
  }
580
}
581

582
static int32_t createMidFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
51,293,452✔
583
                                 SFunctionNode** pMidFunc) {
584
  SNodeList*     pParameterList = NULL;
51,293,452✔
585
  SFunctionNode* pFunc = NULL;
51,293,452✔
586

587
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
51,293,452✔
588
  if (TSDB_CODE_SUCCESS == code) {
51,300,293✔
589
    if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){
51,300,362✔
590
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pSrcFunc, pParameterList, &pFunc);
5,640,595✔
591
    }else{
592
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
45,659,702✔
593
    }
594
  }
595
  if (TSDB_CODE_SUCCESS == code) {
51,300,198✔
596
    tstrncpy(pFunc->node.aliasName, pPartialFunc->node.aliasName, TSDB_COL_NAME_LEN);
51,300,707✔
597
  }
598

599
  if (TSDB_CODE_SUCCESS == code) {
51,300,198✔
600
    *pMidFunc = pFunc;
51,300,198✔
601
  } else {
602
    nodesDestroyList(pParameterList);
×
603
  }
604
  return code;
51,300,060✔
605
}
606

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

612
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
51,807,261✔
613
  if (TSDB_CODE_SUCCESS == code) {
51,811,217✔
614
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
51,811,217✔
615
  }
616
  if (TSDB_CODE_SUCCESS == code) {
51,814,995✔
617
    pFunc->hasOriginalFunc = true;
51,810,518✔
618
    pFunc->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
51,810,518✔
619
    // overwrite function restype set by translate function
620
    if (fmIsSameInOutType(pSrcFunc->funcId)) {
51,810,518✔
621
      pFunc->node.resType = pSrcFunc->node.resType;
20,670,187✔
622
    }
623
    tstrncpy(pFunc->node.aliasName, pSrcFunc->node.aliasName, TSDB_COL_NAME_LEN);
51,805,152✔
624
  }
625

626
  if (TSDB_CODE_SUCCESS == code) {
51,809,629✔
627
    *pMergeFunc = pFunc;
51,809,629✔
628
  } else {
629
    nodesDestroyList(pParameterList);
×
630
  }
631
  return code;
51,810,276✔
632
}
633

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

640
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
51,810,863✔
641
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
51,806,619✔
642
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
51,295,937✔
643
  }
644
  if (TSDB_CODE_SUCCESS == code) {
51,810,785✔
645
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
51,810,811✔
646
  }
647

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

654
  return code;
51,809,517✔
655
}
656

657
const char* fmGetFuncName(int32_t funcId) {
363,723✔
658
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
363,723✔
659
    return "invalid function";
×
660
  }
661
  return funcMgtBuiltins[funcId].name;
363,723✔
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) {
7,998✔
667
  if (funcMgtBuiltins[pFunc->funcId].pStateFunc) {
7,998✔
668
    SNodeList* pParams = NULL;
7,998✔
669
    int32_t    code = nodesCloneList(pFunc->pParameterList, &pParams);
7,998✔
670
    if (!pParams) return code;
7,998✔
671
    code = createFunction(funcMgtBuiltins[pFunc->funcId].pStateFunc, pParams, pStateFunc);
7,998✔
672
    if (TSDB_CODE_SUCCESS != code) {
7,998✔
673
      nodesDestroyList(pParams);
×
674
      return code;
×
675
    }
676
    tstrncpy((*pStateFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
7,998✔
677
    tstrncpy((*pStateFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
7,998✔
678
  }
679
  return TSDB_CODE_SUCCESS;
7,998✔
680
}
681

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

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

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

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

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

752
int32_t fmGetFuncId(const char* name) {
8,630,255✔
753
  if (NULL != gFunMgtService.pFuncNameHashTable) {
8,630,255✔
754
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, name, strlen(name));
8,630,255✔
755
    if (NULL != pVal) {
8,630,255✔
756
      return *(int32_t*)pVal;
8,630,255✔
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,835,974✔
769
  static int32_t twstartFuncId = -2;
770
  if (twstartFuncId < 0) {
1,835,974✔
771
    twstartFuncId = fmGetFuncId("_twstart");
1,632,971✔
772
  }
773
  return twstartFuncId;
1,835,974✔
774
}
775

776
int32_t fmGetTwendFuncId() {
1,669,214✔
777
  static int32_t twendFuncId = -2;
778
  if (twendFuncId < 0) {
1,669,214✔
779
    twendFuncId = fmGetFuncId("_twend");
1,632,971✔
780
  }
781
  return twendFuncId;
1,669,214✔
782
}
783

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

792
int32_t fmGetExternalWindowColumnFuncId() {
1,681,200✔
793
  static int32_t externalWindowColumnFuncId = -2;
794
  if (externalWindowColumnFuncId < 0) {
1,681,200✔
795
    externalWindowColumnFuncId = fmGetFuncId("_external_window_column");
1,632,971✔
796
  }
797
  return externalWindowColumnFuncId;
1,681,200✔
798
}
799

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

815
bool fmIsCountLikeFunc(int32_t funcId) {
582,542,007✔
816
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
582,542,007✔
817
}
818

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

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

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

841
const void* fmGetStreamPesudoFuncVal(int32_t funcId, const SStreamRuntimeFuncInfo* pStreamRuntimeFuncInfo) {
434,440,679✔
842
  switch (funcMgtBuiltins[funcId].type) {
434,440,679✔
843
    case FUNCTION_TYPE_TGRPID:
153,815✔
844
      return &pStreamRuntimeFuncInfo->groupId;
153,815✔
845
    case FUNCTION_TYPE_PLACEHOLDER_COLUMN:
993,397✔
846
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
993,397✔
847
    case FUNCTION_TYPE_PLACEHOLDER_TBNAME:
2,138,304✔
848
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
2,138,304✔
849
    default:
431,214,322✔
850
      break;
431,214,322✔
851
  }
852

853
  SSTriggerCalcParam *pParams = taosArrayGet(pStreamRuntimeFuncInfo->pStreamPesudoFuncVals, pStreamRuntimeFuncInfo->curIdx);
431,214,322✔
854
  switch (funcMgtBuiltins[funcId].type) {
431,253,591✔
855
    case FUNCTION_TYPE_TPREV_TS:
×
856
      return &pParams->prevTs;
×
857
    case FUNCTION_TYPE_TCURRENT_TS:
2,250,384✔
858
      return &pParams->currentTs;
2,250,384✔
859
    case FUNCTION_TYPE_TNEXT_TS:
×
860
      return &pParams->nextTs;
×
861
    case FUNCTION_TYPE_TWSTART:
372,359,616✔
862
      return &pParams->wstart;
372,359,616✔
863
    case FUNCTION_TYPE_TWEND:
19,089,222✔
864
      return &pParams->wend;
19,089,222✔
865
    case FUNCTION_TYPE_TWDURATION:
1,160,951✔
866
      return &pParams->wduration;
1,160,951✔
867
    case FUNCTION_TYPE_TWROWNUM:
34,788,304✔
868
      return &pParams->wrownum;
34,788,304✔
869
    case FUNCTION_TYPE_TPREV_LOCALTIME:
305,715✔
870
      return &pParams->prevLocalTime;
305,715✔
871
    case FUNCTION_TYPE_TLOCALTIME:
818,501✔
872
      return &pParams->triggerTime;
818,501✔
873
    case FUNCTION_TYPE_TNEXT_LOCALTIME:
469,461✔
874
      return &pParams->nextLocalTime;
469,461✔
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:
6,831✔
882
      return &pParams->idlestart;
6,831✔
883
    case FUNCTION_TYPE_TIDLEEND:
6,831✔
884
      return &pParams->idleend;
6,831✔
885
    default:
×
886
      break;
×
887
  }
888
  return NULL;
×
889
}
890

891
bool fmIsStreamPesudoColVal(int32_t funcId) {
580,947✔
892
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
580,947✔
893
    return false;
8,910✔
894
  }
895
  return funcMgtBuiltins[funcId].type == FUNCTION_TYPE_PLACEHOLDER_COLUMN
572,037✔
896
         || funcMgtBuiltins[funcId].type == FUNCTION_TYPE_PLACEHOLDER_TBNAME;
572,037✔
897
}
898

899
int32_t fmGetStreamPesudoFuncEnv(int32_t funcId, SNodeList* pParamNodes, SFuncExecEnv *pEnv) {
8,296,724✔
900
  if (NULL == pParamNodes || pParamNodes->length < 1) {
8,296,724✔
UNCOV
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,297,026✔
906
  if (QUERY_NODE_VALUE != firstParamType) {
8,297,026✔
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,297,026✔
912
  pEnv->calcMemSize = pResDesc->node.resType.bytes;
8,297,026✔
913

914
  return TSDB_CODE_SUCCESS;
8,297,471✔
915
}
916

917

918
int32_t fmSetStreamPseudoFuncParamVal(int32_t funcId, SNodeList* pParamNodes, const SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
445,791,574✔
919
  if (!pStreamRuntimeInfo) {
445,791,574✔
920
    uError("internal error, should have pVals for stream pseudo funcs");
×
921
    return TSDB_CODE_INTERNAL_ERROR;
×
922
  }
923
  int32_t code = 0;
445,791,574✔
924
  SArray *pVals1 = NULL;
445,791,574✔
925
  SNode* pFirstParam = nodesListGetNode(pParamNodes, 0);
445,791,574✔
926
  if (nodeType(pFirstParam) != QUERY_NODE_VALUE) {
445,832,285✔
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;
445,791,526✔
932
  uDebug("set stream pseudo func param val, functype: %d, pStreamRuntimeInfo: %p", t, pStreamRuntimeInfo);
445,833,815✔
933
  if (FUNCTION_TYPE_TGRPID == t) {
445,761,689✔
934
    SValue v = {0};
153,815✔
935
    v.type = TSDB_DATA_TYPE_BIGINT;
153,815✔
936
    v.val = *(int64_t*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
153,815✔
937
    
938
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&v, pFirstParam->type));
153,503✔
939
    if (code != 0) {
153,330✔
940
      uError("failed to set value node value: %s", tstrerror(code));
×
941
      return code;
×
942
    }
943
  } else if (FUNCTION_TYPE_PLACEHOLDER_COLUMN == t) {
445,607,874✔
944
    SNode* pSecondParam = nodesListGetNode(pParamNodes, 1);
993,397✔
945
    if (nodeType(pSecondParam) != QUERY_NODE_VALUE) {
993,397✔
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);
993,397✔
950
    int32_t idx = ((SValueNode*)pSecondParam)->datum.i;
993,397✔
951
    if (idx - 1 < 0 || idx - 1 >= taosArrayGetSize(pVal)) {
993,397✔
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);
993,397✔
956
    if (pValue == NULL) {
993,397✔
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){
993,397✔
961
      if (pValue->data.type != ((SValueNode*)pFirstParam)->node.resType.type){
985,767✔
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)) {
985,767✔
966
        char* tmp = taosMemoryCalloc(1, pValue->data.nData + VARSTR_HEADER_SIZE); 
684,100✔
967
        if (tmp == NULL) {
684,100✔
968
          return TSDB_CODE_OUT_OF_MEMORY;
×
969
        }
970
        taosMemoryFree(((SValueNode*)pFirstParam)->datum.p);
684,100✔
971
        ((SValueNode*)pFirstParam)->datum.p = tmp;
684,100✔
972
        memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
684,100✔
973
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
684,100✔
974
      } else {
975
        code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&pValue->data, pValue->data.type));
301,667✔
976
      }
977
    }
978
    ((SValueNode*)pFirstParam)->isNull = pValue->isNull;
993,397✔
979
  } else if (FUNCTION_TYPE_PLACEHOLDER_TBNAME == t) {
444,614,477✔
980
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
2,083,030✔
981
    for (int32_t i = 0; i < taosArrayGetSize(pVal); ++i) {
2,540,570✔
982
      SStreamGroupValue* pValue = taosArrayGet(pVal, i);
2,540,570✔
983
      if (pValue != NULL && pValue->isTbname) {
2,540,570✔
984
        taosMemoryFreeClear(((SValueNode*)pFirstParam)->datum.p);
2,085,530✔
985
        ((SValueNode*)pFirstParam)->datum.p = taosMemoryCalloc(pValue->data.nData + VARSTR_HEADER_SIZE, 1);
2,084,278✔
986
        if (NULL == ((SValueNode*)pFirstParam)->datum.p ) {
2,084,909✔
987
          return terrno;
×
988
        }
989

990
        (void)memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
2,084,593✔
991
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
2,084,593✔
992
        break;
2,085,215✔
993
      }
994
    }
995
  } else if(FUNCTION_TYPE_EXTERNAL_WINDOW_COLUMN == t) {
442,531,447✔
996
    if (NULL == pParamNodes || LIST_LENGTH(pParamNodes) < 2) {
11,337,521✔
997
      uError("invalid stream external window column param list %p, len: %d", pParamNodes,
445✔
998
             pParamNodes ? LIST_LENGTH(pParamNodes) : 0);
999
      return TSDB_CODE_INTERNAL_ERROR;
×
1000
    }
1001

1002
    SNode* pParamNode = nodesListGetNode(pParamNodes, 1);
11,337,521✔
1003
    if (NULL == pParamNode || QUERY_NODE_VALUE != nodeType(pParamNode)) {
11,337,076✔
1004
      uError("invalid stream external window column param node %p type %d for func: %d", pParamNode,
890✔
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,336,186✔
1011
    if (!pVal) {
11,338,856✔
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,338,856✔
1016
      ((SValueNode*)pFirstParam)->isNull = true;
890✔
1017
    } else {
1018
      ((SValueNode*)pFirstParam)->isNull = false;
11,337,966✔
1019
      if (IS_VAR_DATA_TYPE(pVal->data.type)) {
11,338,411✔
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);
9,806✔
1023
        if (tmp == NULL) {
9,806✔
1024
          return TSDB_CODE_OUT_OF_MEMORY;
×
1025
        }
1026
        taosMemoryFree(((SValueNode*)pFirstParam)->datum.p);
9,806✔
1027
        ((SValueNode*)pFirstParam)->datum.p = tmp;
9,806✔
1028
        memcpy(tmp, pVal->data.pData, pVal->data.nData);
9,806✔
1029
      } else {
1030
        code = nodesSetValueNodeValue((SValueNode*)pFirstParam, (void*)&pVal->data.val);
11,328,605✔
1031
      }
1032
    }
1033
    if (code != 0) {
11,336,631✔
1034
      uError("failed to set value node value: %s", tstrerror(code));
×
1035
      return code;
×
1036
    }
1037
  } else if (LIST_LENGTH(pParamNodes) == 1) {
862,451,493✔
1038
    // twstart, twend
1039
    const void* pVal = fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
431,259,800✔
1040
    if (!pVal) {
431,182,940✔
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);
431,182,940✔
1045
    if (code != 0) {
431,251,271✔
1046
      uError("failed to set value node value: %s", tstrerror(code));
342✔
1047
      return code;
×
1048
    }
1049
  } else {
UNCOV
1050
    uError("invalid placeholder function type: %d", t);
×
1051
    return TSDB_CODE_INTERNAL_ERROR;
×
1052
  }
1053
  
1054
  return code;
445,827,159✔
1055
}
1056

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