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

taosdata / TDengine / #5033

24 Apr 2026 11:25AM UTC coverage: 73.058% (-0.02%) from 73.076%
#5033

push

travis-ci

web-flow
merge: from main to 3.0 branch #35224

merge: from main to 3.0 branch[manual-only]

1337 of 1975 new or added lines in 48 files covered. (67.7%)

14198 existing lines in 150 files now uncovered.

275895 of 377640 relevant lines covered (73.06%)

132323361.15 hits per line

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

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

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

52
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
358,055,112✔
53
    if (TSDB_CODE_SUCCESS != taosHashPut(gFunMgtService.pFuncNameHashTable, funcMgtBuiltins[i].name,
356,511,771✔
54
                                         strlen(funcMgtBuiltins[i].name), &i, sizeof(int32_t))) {
356,511,771✔
55
      initFunctionCode = terrno;
×
56
      return;
×
57
    }
58
  }
59
  InitSpecialFuncId();
1,543,341✔
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);
8,856,618✔
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,543,341✔
75
  (void)taosThreadOnce(&functionHashTableInit, doInitFunctionTable);
1,543,341✔
76
  return initFunctionCode;
1,543,341✔
77
}
78

79
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
840,610,305✔
80
  if (NULL != gFunMgtService.pFuncNameHashTable) {
840,610,305✔
81
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
840,449,480✔
82
    if (NULL != pVal) {
840,480,854✔
83
      pFunc->funcId = *(int32_t*)pVal;
840,276,333✔
84
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
840,276,297✔
85
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
840,275,284✔
86
    }
87
    return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
204,521✔
88
  }
89
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
3,284,755✔
90
    if (0 == strcmp(funcMgtBuiltins[i].name, pFunc->functionName)) {
3,284,384✔
91
      pFunc->funcId = i;
176,512✔
92
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
176,512✔
93
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
176,512✔
94
    }
95
  }
96
  return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
428✔
97
}
98

99
EFuncReturnRows fmGetFuncReturnRows(SFunctionNode* pFunc) {
17,963,188✔
100
  if (NULL != funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc) {
17,963,188✔
101
    return funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc(pFunc);
12,661,977✔
102
  }
103
  return (fmIsIndefiniteRowsFunc(pFunc->funcId) || fmIsMultiRowsFunc(pFunc->funcId)) ? FUNC_RETURN_ROWS_INDEFINITE
5,301,211✔
104
                                                                                     : FUNC_RETURN_ROWS_NORMAL;
5,301,211✔
105
}
106

107
bool canCoexistIndefiniteRowsFunc(int32_t funcId1, int32_t funcId2) {
181,100✔
108
  if (fmIsUserDefinedFunc(funcId1) || funcId1 < 0 || funcId1 >= funcMgtBuiltinsNum ||
362,200✔
109
      fmIsUserDefinedFunc(funcId2) || funcId2 < 0 || funcId2 >= funcMgtBuiltinsNum) {
362,200✔
110
    return false;
×
111
  }
112
  if (funcId1 == funcId2) {
181,100✔
113
    return true;
165,411✔
114
  }
115
  if ((funcMgtBuiltins[funcId1].type == FUNCTION_TYPE_LAG || funcMgtBuiltins[funcId1].type == FUNCTION_TYPE_LEAD) &&
15,689✔
116
      (funcMgtBuiltins[funcId2].type == FUNCTION_TYPE_LAG || funcMgtBuiltins[funcId2].type == FUNCTION_TYPE_LEAD)) {
15,240✔
117
    return true;
15,240✔
118
  }
119
  return false;
449✔
120
}
121

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

126
EFunctionType fmGetFuncType(const char* pFunc) {
610,999,647✔
127
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
610,999,647✔
128
  if (NULL != pVal) {
611,018,631✔
129
    return funcMgtBuiltins[*(int32_t*)pVal].type;
610,810,781✔
130
  }
131
  return FUNCTION_TYPE_UDF;
207,850✔
132
}
133

134
EFunctionType fmGetFuncTypeFromId(int32_t funcId) {
23,493,699✔
135
  if (funcId >= 0 && funcId < funcMgtBuiltinsNum) {
23,493,699✔
136
    return funcMgtBuiltins[funcId].type;
23,493,153✔
137
  }
138
  return FUNCTION_TYPE_UDF;
546✔
139
}
140

141
EFuncDataRequired fmFuncDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
41,156,486✔
142
  if (fmIsUserDefinedFunc(pFunc->funcId) || pFunc->funcId < 0 || pFunc->funcId >= funcMgtBuiltinsNum) {
41,156,486✔
143
    return FUNC_DATA_REQUIRED_DATA_LOAD;
2,527✔
144
  }
145
  if (NULL == funcMgtBuiltins[pFunc->funcId].dataRequiredFunc) {
41,161,440✔
146
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
147
  }
148
  return funcMgtBuiltins[pFunc->funcId].dataRequiredFunc(pFunc, pTimeWindow);
41,161,278✔
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;
16✔
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;
662,456✔
159
    ;
160
  }
161

162
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
2,147,483,647✔
163
    return FUNC_DATA_REQUIRED_DATA_LOAD;
191,290,237✔
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) {
299,407,738✔
170
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
299,407,738✔
171
    return TSDB_CODE_FAILED;
51,683✔
172
  }
173
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
299,378,315✔
174
  pFpSet->init = funcMgtBuiltins[funcId].initFunc;
299,352,336✔
175
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
299,386,970✔
176
  pFpSet->finalize = funcMgtBuiltins[funcId].finalizeFunc;
299,371,441✔
177
  pFpSet->combine = funcMgtBuiltins[funcId].combineFunc;
299,338,758✔
178
  pFpSet->processFuncByRow = funcMgtBuiltins[funcId].processFuncByRow;
299,355,287✔
179
  pFpSet->cleanup = funcMgtBuiltins[funcId].cleanupFunc;
299,417,670✔
180
  return TSDB_CODE_SUCCESS;
299,378,844✔
181
}
182

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

198
int32_t fmGetScalarFuncExecFuncs(int32_t funcId, SScalarFuncExecFuncs* pFpSet) {
1,127,567,637✔
199
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,127,567,637✔
200
    return TSDB_CODE_FAILED;
148,451✔
201
  }
202
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
1,127,724,792✔
203
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
1,127,743,939✔
204
  return TSDB_CODE_SUCCESS;
1,127,773,067✔
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,306,228,895✔
212

213
bool fmIsSelectColsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_COLS_FUNC); }
154,174,499✔
214

215
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
1,903,376,045✔
216

217
bool fmIsTimelineFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TIMELINE_FUNC); }
1,175,879,726✔
218

219
bool fmIsDateTimeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_DATETIME_FUNC); }
594,769,667✔
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,449,482,257✔
226

227
bool fmIsWindowClauseFunc(int32_t funcId) { return fmIsAggFunc(funcId) || fmIsWindowPseudoColumnFunc(funcId); }
60,168,875✔
228

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

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

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

235
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
1,597,183,389✔
236

237
bool fmIsSpecialDataRequiredFunc(int32_t funcId) {
93,066,149✔
238
  return isSpecificClassifyFunc(funcId, FUNC_MGT_SPECIAL_DATA_REQUIRED);
93,066,149✔
239
}
240

241
bool fmIsDynamicScanOptimizedFunc(int32_t funcId) {
44,540,629✔
242
  return isSpecificClassifyFunc(funcId, FUNC_MGT_DYNAMIC_SCAN_OPTIMIZED);
44,540,629✔
243
}
244

245
bool fmIsMultiResFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_RES_FUNC); }
1,211,461,675✔
246

247
bool fmIsRepeatScanFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_REPEAT_SCAN_FUNC); }
1,188,719,853✔
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); }
595,033,171✔
252

253
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
65,274,306✔
254

255
bool fmIsSystemInfoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SYSTEM_INFO_FUNC); }
598,077,692✔
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); }
605,685,301✔
260

261
bool fmIsMultiRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_ROWS_FUNC); }
1,185,491,961✔
262

263
static bool fmIsKeepOrderFuncId(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_KEEP_ORDER_FUNC); }
718,855,303✔
264

265
bool fmIsKeepOrderFunc(SFunctionNode* pFunc) {
725,323,525✔
266
  if (pFunc->funcType == FUNCTION_TYPE_TOP || pFunc->funcType == FUNCTION_TYPE_BOTTOM ||
725,323,525✔
267
      pFunc->funcType == FUNCTION_TYPE_SAMPLE) {
721,247,957✔
268
    if (pFunc->pParameterList != NULL && pFunc->pParameterList->length >= 2) {
6,460,219✔
269
      SNode* pParam = nodesListGetNode(pFunc->pParameterList, 1);
6,474,453✔
270
      if (pParam != NULL && nodeType(pParam) == QUERY_NODE_VALUE) {
6,474,453✔
271
        SValueNode* pConst = (SValueNode*)pParam;
6,474,453✔
272
        if (pConst->datum.i == 1) {
6,474,453✔
273
          return true;
755,781✔
274
        }
275
      }
276
    }
277
    return false;
5,710,640✔
278
  }
279
  return fmIsKeepOrderFuncId(pFunc->funcId);
718,886,610✔
280
}
281

282
bool fmIsCumulativeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CUMULATIVE_FUNC); }
156,683,414✔
283

284
bool fmIsForbidSysTableFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_SYSTABLE_FUNC); }
594,956,217✔
285

286
bool fmIsInterpFunc(int32_t funcId) {
1,218,993,699✔
287
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,218,993,699✔
288
    return false;
370,838✔
289
  }
290
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
1,218,628,745✔
291
}
292

293
bool fmIsInterpPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERP_PC_FUNC); }
1,199,673,352✔
294

295
bool fmIsForecastFunc(int32_t funcId) {
1,169,845,923✔
296
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,169,845,923✔
297
    return false;
386,376✔
298
  }
299
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
1,169,461,981✔
300
}
301

302
bool fmIsAnalysisPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_ANALYTICS_PC_FUNC); }
1,202,552,959✔
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) {
70,714,850✔
315
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
70,714,850✔
316
    return false;
32✔
317
  }
318
  return FUNCTION_TYPE_LAST_ROW == funcMgtBuiltins[funcId].type;
70,715,895✔
319
}
320

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

328
bool fmIsNotNullOutputFunc(int32_t funcId) {
451,410,486✔
329
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
451,410,486✔
330
    return false;
90,973✔
331
  }
332
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
451,327,038✔
333
         FUNCTION_TYPE_CACHE_LAST == funcMgtBuiltins[funcId].type ||
434,938,192✔
334
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
433,565,689✔
335
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
424,590,054✔
336
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
420,987,421✔
337
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
410,434,597✔
338
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
405,120,908✔
339
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
403,093,805✔
340
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
350,211,838✔
341
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
1,232,576,547✔
342
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
346,326,884✔
343
}
344

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

352
bool fmIsGroupKeyFunc(int32_t funcId) {
629,941,696✔
353
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
629,941,696✔
354
    return false;
35✔
355
  }
356
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
629,943,682✔
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) {
371,677,520✔
367
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
371,677,520✔
368
    return false;
×
369
  }
370
  return FUNCTION_TYPE_GROUP_CONST_VALUE == funcMgtBuiltins[funcId].type;
371,677,520✔
371
}
372

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

380
bool fmIsBlockDistFunc(int32_t funcId) {
594,765,696✔
381
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
594,765,696✔
382
    return false;
175,147✔
383
  }
384
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
594,596,659✔
385
}
386

387
bool fmIsDBUsageFunc(int32_t funcId) {
594,766,051✔
388
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
594,766,051✔
389
    return false;
185,205✔
390
  }
391
  return FUNCTION_TYPE_DB_USAGE == funcMgtBuiltins[funcId].type;
594,584,486✔
392
}
393

394
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
802,204,390✔
395

396
bool fmIsVolatileFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_VOLATILE_FUNC); }
867,832,188✔
397

398
bool fmIsNoPushdownFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_NO_PUSHDOWN_FUNC); }
34,993,659✔
399

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

402
void fmFuncMgtDestroy() {
1,543,384✔
403
  void* m = gFunMgtService.pFuncNameHashTable;
1,543,384✔
404
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
1,543,384✔
405
    taosHashCleanup(m);
1,543,341✔
406
  }
407
}
1,543,384✔
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) {
49,329,185✔
421
  bool res = false;
49,329,185✔
422
  switch (funcMgtBuiltins[funcId].type) {
49,329,185✔
423
    case FUNCTION_TYPE_MAX:
19,728,034✔
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;
19,728,034✔
433
      break;
19,728,034✔
434
    default:
29,601,191✔
435
      break;
29,601,191✔
436
  }
437
  return res;
49,329,225✔
438
}
439

440
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
14,923,246✔
441
  SNode* pNode;
442
  FOREACH(pNode, pFunc->pParameterList) {
15,010,450✔
443
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
196,511✔
444
      return false;
109,307✔
445
    }
446
  }
447
  return true;
14,813,939✔
448
}
449

450
bool fmIsSkipScanCheckFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SKIP_SCAN_CHECK_FUNC); }
44,532,467✔
451

452
bool fmIsPrimaryKeyFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PRIMARY_KEY_FUNC); }
518,609,084✔
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) {
663,233✔
457
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
663,233✔
458
    return false;
×
459
  }
460
  return (funcMgtBuiltins[funcId].type == FUNCTION_TYPE_WSTART || funcMgtBuiltins[funcId].type == FUNCTION_TYPE_WEND ||
1,112,101✔
461
          funcMgtBuiltins[funcId].type == FUNCTION_TYPE_WDURATION);
448,868✔
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) {
157,750,560✔
471
  char msg[128] = {0};
157,750,560✔
472
  return fmGetFuncInfo(pFunc, msg, sizeof(msg));
157,750,560✔
473
}
474

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

492
static void resetOutputChangedFunc(SFunctionNode *pFunc, const SFunctionNode* pSrcFunc) {
147,584,062✔
493
  if (funcMgtBuiltins[pFunc->funcId].type == FUNCTION_TYPE_LAST_MERGE) {
147,584,062✔
494
    pFunc->node.resType = pSrcFunc->node.resType;
23,374,625✔
495
    return;
23,374,625✔
496
  }
497
}
498

499
int32_t createFunctionWithSrcFunc(const char* pName, const SFunctionNode* pSrcFunc, SNodeList* pParameterList, SFunctionNode** ppFunc) {
147,582,296✔
500
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
147,582,296✔
501
  if (NULL == *ppFunc) {
147,589,564✔
502
    return code;
×
503
  }
504

505
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
147,589,564✔
506
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
147,589,525✔
507
  (*ppFunc)->pSrcFuncRef = pSrcFunc;
147,589,397✔
508

509
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
147,589,525✔
510
  (*ppFunc)->pParameterList = pParameterList;
147,589,397✔
511
  code = getFuncInfo((*ppFunc));
147,589,481✔
512
  if (TSDB_CODE_SUCCESS != code) {
147,585,379✔
513
    (*ppFunc)->pParameterList = NULL;
×
514
    nodesDestroyNode((SNode*)*ppFunc);
×
515
    *ppFunc = NULL;
×
516
    return code;
×
517
  }
518
  resetOutputChangedFunc(*ppFunc, pSrcFunc);
147,585,379✔
519
  (*ppFunc)->node.relatedTo = pSrcFunc->node.relatedTo;
147,586,562✔
520
  (*ppFunc)->node.bindExprID = pSrcFunc->node.bindExprID;
147,586,646✔
521
  return code;
147,586,603✔
522
}
523

524
static int32_t createColumnByFunc(const SFunctionNode* pFunc, SColumnNode** ppCol) {
98,243,257✔
525
  int32_t code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)ppCol);
98,243,257✔
526
  if (NULL == *ppCol) {
98,251,401✔
527
    return code;
×
528
  }
529
  tstrncpy((*ppCol)->colName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
98,251,401✔
530
  (*ppCol)->node.resType = pFunc->node.resType;
98,251,361✔
531
  return TSDB_CODE_SUCCESS;
98,251,194✔
532
}
533

534
bool fmIsDistExecFunc(int32_t funcId) {
376,459,472✔
535
  if (fmIsUserDefinedFunc(funcId)) {
376,459,472✔
536
    return false;
105,481✔
537
  }
538
  if (!fmIsVectorFunc(funcId)) {
376,388,664✔
539
    return true;
478,205✔
540
  }
541
  return (NULL != funcMgtBuiltins[funcId].pPartialFunc && NULL != funcMgtBuiltins[funcId].pMergeFunc);
375,924,029✔
542
}
543

544
static int32_t createPartialFunction(const SFunctionNode* pSrcFunc, SFunctionNode** pPartialFunc) {
49,332,530✔
545
  SNodeList* pParameterList = NULL;
49,332,530✔
546
  int32_t    code = nodesCloneList(pSrcFunc->pParameterList, &pParameterList);
49,332,530✔
547
  if (NULL == pParameterList) {
49,335,518✔
548
    return code;
×
549
  }
550
  code =
551
      createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pPartialFunc, pSrcFunc, pParameterList, pPartialFunc);
49,335,518✔
552
  if (TSDB_CODE_SUCCESS != code) {
49,334,565✔
553
    nodesDestroyList(pParameterList);
3,233✔
554
    return code;
×
555
  }
556
  (*pPartialFunc)->hasOriginalFunc = true;
49,331,332✔
557
  (*pPartialFunc)->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
49,331,331✔
558
  char name[TSDB_FUNC_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_POINTER_PRINT_BYTES + 1] = {0};
49,331,539✔
559

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

568
static int32_t createMergeFuncPara(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
98,242,468✔
569
                                   SNodeList** pParameterList) {
570
  SNode*  pRes = NULL;
98,242,468✔
571
  int32_t code = createColumnByFunc(pPartialFunc, (SColumnNode**)&pRes);
98,242,468✔
572
  if (TSDB_CODE_SUCCESS != code) {
98,250,674✔
573
    return code;
×
574
  }
575
  if (NULL != funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc) {
98,250,674✔
576
    return funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc(pSrcFunc->pParameterList, pRes, pParameterList);
871,132✔
577
  } else {
578
    return nodesListMakeStrictAppend(pParameterList, pRes);
97,379,625✔
579
  }
580
}
581

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

587
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
48,909,805✔
588
  if (TSDB_CODE_SUCCESS == code) {
48,916,802✔
589
    if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){
48,917,078✔
590
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pSrcFunc, pParameterList, &pFunc);
5,434,638✔
591
    }else{
592
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
43,482,440✔
593
    }
594
  }
595
  if (TSDB_CODE_SUCCESS == code) {
48,916,386✔
596
    tstrncpy(pFunc->node.aliasName, pPartialFunc->node.aliasName, TSDB_COL_NAME_LEN);
48,917,216✔
597
  }
598

599
  if (TSDB_CODE_SUCCESS == code) {
48,916,386✔
600
    *pMidFunc = pFunc;
48,916,386✔
601
  } else {
602
    nodesDestroyList(pParameterList);
×
603
  }
604
  return code;
48,916,484✔
605
}
606

607
static int32_t createMergeFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
49,331,627✔
608
                                   SFunctionNode** pMergeFunc) {
609
  SNodeList*     pParameterList = NULL;
49,331,627✔
610
  SFunctionNode* pFunc = NULL;
49,331,583✔
611

612
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
49,331,588✔
613
  if (TSDB_CODE_SUCCESS == code) {
49,336,346✔
614
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
49,336,415✔
615
  }
616
  if (TSDB_CODE_SUCCESS == code) {
49,339,848✔
617
    pFunc->hasOriginalFunc = true;
49,335,446✔
618
    pFunc->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
49,335,446✔
619
    // overwrite function restype set by translate function
620
    if (fmIsSameInOutType(pSrcFunc->funcId)) {
49,335,446✔
621
      pFunc->node.resType = pSrcFunc->node.resType;
19,728,291✔
622
    }
623
    tstrncpy(pFunc->node.aliasName, pSrcFunc->node.aliasName, TSDB_COL_NAME_LEN);
49,330,104✔
624
  }
625

626
  if (TSDB_CODE_SUCCESS == code) {
49,334,590✔
627
    *pMergeFunc = pFunc;
49,334,590✔
628
  } else {
629
    nodesDestroyList(pParameterList);
×
630
  }
631
  return code;
49,335,631✔
632
}
633

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

640
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
49,335,980✔
641
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
49,332,291✔
642
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
48,912,177✔
643
  }
644
  if (TSDB_CODE_SUCCESS == code) {
49,336,906✔
645
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
49,335,922✔
646
  }
647

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

654
  return code;
49,335,976✔
655
}
656

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

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

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

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

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

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

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

776
int32_t fmGetTwendFuncId() {
1,576,172✔
777
  static int32_t twendFuncId = -2;
778
  if (twendFuncId < 0) {
1,576,172✔
779
    twendFuncId = fmGetFuncId("_twend");
1,543,341✔
780
  }
781
  return twendFuncId;
1,576,172✔
782
}
783

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

792
int32_t fmGetExternalWindowColumnFuncId() {
1,587,469✔
793
  static int32_t externalWindowColumnFuncId = -2;
794
  if (externalWindowColumnFuncId < 0) {
1,587,469✔
795
    externalWindowColumnFuncId = fmGetFuncId("_external_window_column");
1,543,341✔
796
  }
797
  return externalWindowColumnFuncId;
1,587,469✔
798
}
799

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

815
bool fmIsCountLikeFunc(int32_t funcId) {
589,981,926✔
816
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
589,981,926✔
817
}
818

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

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

833
const void* fmGetExternalWindowColumnFuncVal(const SStreamRuntimeFuncInfo* pStreamRuntimeFuncInfo, int32_t index) {
10,546,204✔
834
  SSTriggerCalcParam *pParams = taosArrayGet(pStreamRuntimeFuncInfo->pStreamPesudoFuncVals, pStreamRuntimeFuncInfo->curIdx);
10,546,204✔
835
  if (pParams == NULL || pParams->pExternalWindowData == NULL || index < 0) {
10,571,104✔
836
    return NULL;
×
837
  }
838
  return taosArrayGet(pParams->pExternalWindowData, index);
10,571,519✔
839
}
840

841
const void* fmGetStreamPesudoFuncVal(int32_t funcId, const SStreamRuntimeFuncInfo* pStreamRuntimeFuncInfo) {
390,393,404✔
842
  switch (funcMgtBuiltins[funcId].type) {
390,393,404✔
843
    case FUNCTION_TYPE_TGRPID:
137,245✔
844
      return &pStreamRuntimeFuncInfo->groupId;
137,245✔
845
    case FUNCTION_TYPE_PLACEHOLDER_COLUMN:
878,536✔
846
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
878,536✔
847
    case FUNCTION_TYPE_PLACEHOLDER_TBNAME:
1,955,648✔
848
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
1,955,648✔
849
    default:
387,512,537✔
850
      break;
387,512,537✔
851
  }
852

853
  SSTriggerCalcParam *pParams = taosArrayGet(pStreamRuntimeFuncInfo->pStreamPesudoFuncVals, pStreamRuntimeFuncInfo->curIdx);
387,512,537✔
854
  switch (funcMgtBuiltins[funcId].type) {
387,575,143✔
855
    case FUNCTION_TYPE_TPREV_TS:
×
856
      return &pParams->prevTs;
×
857
    case FUNCTION_TYPE_TCURRENT_TS:
2,455,010✔
858
      return &pParams->currentTs;
2,455,010✔
859
    case FUNCTION_TYPE_TNEXT_TS:
×
860
      return &pParams->nextTs;
×
861
    case FUNCTION_TYPE_TWSTART:
334,435,805✔
862
      return &pParams->wstart;
334,435,805✔
863
    case FUNCTION_TYPE_TWEND:
16,943,049✔
864
      return &pParams->wend;
16,943,049✔
865
    case FUNCTION_TYPE_TWDURATION:
938,381✔
866
      return &pParams->wduration;
938,381✔
867
    case FUNCTION_TYPE_TWROWNUM:
31,421,671✔
868
      return &pParams->wrownum;
31,421,671✔
869
    case FUNCTION_TYPE_TPREV_LOCALTIME:
261,655✔
870
      return &pParams->prevLocalTime;
261,655✔
871
    case FUNCTION_TYPE_TLOCALTIME:
704,599✔
872
      return &pParams->triggerTime;
704,599✔
873
    case FUNCTION_TYPE_TNEXT_LOCALTIME:
402,886✔
874
      return &pParams->nextLocalTime;
402,886✔
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,886✔
882
      return &pParams->idlestart;
5,886✔
883
    case FUNCTION_TYPE_TIDLEEND:
5,886✔
884
      return &pParams->idleend;
5,886✔
885
    default:
×
886
      break;
×
887
  }
888
  return NULL;
×
889
}
890

891
bool fmIsStreamPesudoColVal(int32_t funcId) {
493,305✔
892
  return funcMgtBuiltins[funcId].type == FUNCTION_TYPE_PLACEHOLDER_COLUMN
493,305✔
893
         || funcMgtBuiltins[funcId].type == FUNCTION_TYPE_PLACEHOLDER_TBNAME;
493,305✔
894
}
895

896
int32_t fmGetStreamPesudoFuncEnv(int32_t funcId, SNodeList* pParamNodes, SFuncExecEnv *pEnv) {
7,000,213✔
897
  if (NULL == pParamNodes || pParamNodes->length < 1) {
7,000,213✔
898
    uError("invalid stream pesudo func param list %p", pParamNodes);
319✔
899
    return TSDB_CODE_INTERNAL_ERROR;
×
900
  }
901

902
  int32_t firstParamType = nodeType(nodesListGetNode(pParamNodes, 0));
6,999,894✔
903
  if (QUERY_NODE_VALUE != firstParamType) {
7,000,213✔
904
    uError("invalid stream pesudo func first param type %d", firstParamType);
×
905
    return TSDB_CODE_INTERNAL_ERROR;
×
906
  }
907

908
  SValueNode* pResDesc = (SValueNode*)nodesListGetNode(pParamNodes, 0);
7,000,213✔
909
  pEnv->calcMemSize = pResDesc->node.resType.bytes;
7,000,179✔
910

911
  return TSDB_CODE_SUCCESS;
7,000,454✔
912
}
913

914

915
int32_t fmSetStreamPseudoFuncParamVal(int32_t funcId, SNodeList* pParamNodes, const SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
400,978,856✔
916
  if (!pStreamRuntimeInfo) {
400,978,856✔
917
    uError("internal error, should have pVals for stream pseudo funcs");
×
918
    return TSDB_CODE_INTERNAL_ERROR;
×
919
  }
920
  int32_t code = 0;
400,978,856✔
921
  SArray *pVals1 = NULL;
400,978,856✔
922
  SNode* pFirstParam = nodesListGetNode(pParamNodes, 0);
400,978,856✔
923
  if (nodeType(pFirstParam) != QUERY_NODE_VALUE) {
401,045,864✔
924
    uError("invalid param node type: %d for func: %d", nodeType(pFirstParam), funcId);
×
925
    return TSDB_CODE_INTERNAL_ERROR;
×
926
  }
927

928
  int32_t t = funcMgtBuiltins[funcId].type;
400,989,664✔
929
  uDebug("set stream pseudo func param val, functype: %d, pStreamRuntimeInfo: %p", t, pStreamRuntimeInfo);
401,046,239✔
930
  if (FUNCTION_TYPE_TGRPID == t) {
401,025,512✔
931
    SValue v = {0};
137,245✔
932
    v.type = TSDB_DATA_TYPE_BIGINT;
137,245✔
933
    v.val = *(int64_t*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
137,245✔
934
    
935
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&v, pFirstParam->type));
137,245✔
936
    if (code != 0) {
137,245✔
UNCOV
937
      uError("failed to set value node value: %s", tstrerror(code));
×
938
      return code;
×
939
    }
940
  } else if (FUNCTION_TYPE_PLACEHOLDER_COLUMN == t) {
400,888,267✔
941
    SNode* pSecondParam = nodesListGetNode(pParamNodes, 1);
878,407✔
942
    if (nodeType(pSecondParam) != QUERY_NODE_VALUE) {
878,665✔
943
      uError("invalid param node type: %d for func: %d", nodeType(pSecondParam), funcId);
×
944
      return TSDB_CODE_INTERNAL_ERROR;
×
945
    }
946
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
878,407✔
947
    int32_t idx = ((SValueNode*)pSecondParam)->datum.i;
878,536✔
948
    if (idx - 1 < 0 || idx - 1 >= taosArrayGetSize(pVal)) {
878,536✔
UNCOV
949
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
×
950
      return TSDB_CODE_INTERNAL_ERROR;
×
951
    }
952
    SStreamGroupValue* pValue = taosArrayGet(pVal, idx - 1);
878,665✔
953
    if (pValue == NULL) {
878,665✔
954
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
×
955
      return TSDB_CODE_INTERNAL_ERROR;
×
956
    }
957
    if (!pValue->isNull){
878,665✔
958
      if (pValue->data.type != ((SValueNode*)pFirstParam)->node.resType.type){
872,190✔
959
        uError("invalid value type: %d for func: %d, should be: %d", pValue->data.type, funcId, ((SValueNode*)pFirstParam)->node.resType.type);
×
960
        return TSDB_CODE_INTERNAL_ERROR;
×
961
      }
962
      if (IS_VAR_DATA_TYPE(((SValueNode*)pFirstParam)->node.resType.type)) {
872,190✔
963
        char* tmp = taosMemoryCalloc(1, pValue->data.nData + VARSTR_HEADER_SIZE); 
607,241✔
964
        if (tmp == NULL) {
607,241✔
965
          return TSDB_CODE_OUT_OF_MEMORY;
×
966
        }
967
        taosMemoryFree(((SValueNode*)pFirstParam)->datum.p);
607,241✔
968
        ((SValueNode*)pFirstParam)->datum.p = tmp;
607,241✔
969
        memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
607,241✔
970
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
607,241✔
971
      } else {
972
        code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&pValue->data, pValue->data.type));
264,949✔
973
      }
974
    }
975
    ((SValueNode*)pFirstParam)->isNull = pValue->isNull;
878,665✔
976
  } else if (FUNCTION_TYPE_PLACEHOLDER_TBNAME == t) {
400,009,860✔
977
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
1,875,623✔
978
    for (int32_t i = 0; i < taosArrayGetSize(pVal); ++i) {
2,248,258✔
979
      SStreamGroupValue* pValue = taosArrayGet(pVal, i);
2,248,533✔
980
      if (pValue != NULL && pValue->isTbname) {
2,248,255✔
981
        taosMemoryFreeClear(((SValueNode*)pFirstParam)->datum.p);
1,877,013✔
982
        ((SValueNode*)pFirstParam)->datum.p = taosMemoryCalloc(pValue->data.nData + VARSTR_HEADER_SIZE, 1);
1,876,738✔
983
        if (NULL == ((SValueNode*)pFirstParam)->datum.p ) {
1,877,013✔
984
          return terrno;
×
985
        }
986

987
        (void)memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
1,876,460✔
988
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
1,876,179✔
989
        break;
1,877,013✔
990
      }
991
    }
992
  } else if(FUNCTION_TYPE_EXTERNAL_WINDOW_COLUMN == t) {
398,134,237✔
993
    if (NULL == pParamNodes || LIST_LENGTH(pParamNodes) < 2) {
10,556,164✔
UNCOV
994
      uError("invalid stream external window column param list %p, len: %d", pParamNodes,
×
995
             pParamNodes ? LIST_LENGTH(pParamNodes) : 0);
996
      return TSDB_CODE_INTERNAL_ERROR;
×
997
    }
998

999
    SNode* pParamNode = nodesListGetNode(pParamNodes, 1);
10,557,409✔
1000
    if (NULL == pParamNode || QUERY_NODE_VALUE != nodeType(pParamNode)) {
10,566,539✔
1001
      uError("invalid stream external window column param node %p type %d for func: %d", pParamNode,
14,940✔
1002
             pParamNode ? nodeType(pParamNode) : -1, funcId);
1003
      return TSDB_CODE_INTERNAL_ERROR;
×
1004
    }
1005

1006
    const SStreamGroupValue* pVal =
1007
        fmGetExternalWindowColumnFuncVal(pStreamRuntimeInfo, ((SValueNode*)pParamNode)->placeholderNo);
10,553,259✔
1008
    if (!pVal) {
10,571,519✔
1009
      uError("failed to set stream pseudo func param val, NULL val for funcId: %d", funcId);
×
1010
      return TSDB_CODE_INTERNAL_ERROR;
×
1011
    }
1012
    if (pVal->isNull) {
10,571,519✔
1013
      ((SValueNode*)pFirstParam)->isNull = true;
830✔
1014
    } else {
1015
      ((SValueNode*)pFirstParam)->isNull = false;
10,570,689✔
1016
      if (IS_VAR_DATA_TYPE(pVal->data.type)) {
10,571,104✔
1017
        // pVal->data.pData already includes the VARSTR header from colDataGetData,
1018
        // so copy it directly as datum.p expects VARSTR format [header][raw data].
1019
        char* tmp = taosMemoryMalloc(pVal->data.nData);
9,559✔
1020
        if (tmp == NULL) {
9,144✔
1021
          return TSDB_CODE_OUT_OF_MEMORY;
×
1022
        }
1023
        taosMemoryFree(((SValueNode*)pFirstParam)->datum.p);
9,144✔
1024
        ((SValueNode*)pFirstParam)->datum.p = tmp;
9,144✔
1025
        memcpy(tmp, pVal->data.pData, pVal->data.nData);
9,144✔
1026
      } else {
1027
        code = nodesSetValueNodeValue((SValueNode*)pFirstParam, (void*)&pVal->data.val);
10,561,545✔
1028
      }
1029
    }
1030
    if (code != 0) {
10,563,634✔
1031
      uError("failed to set value node value: %s", tstrerror(code));
×
1032
      return code;
×
1033
    }
1034
  } else if (LIST_LENGTH(pParamNodes) == 1) {
775,086,498✔
1035
    // twstart, twend
1036
    const void* pVal = fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
387,573,826✔
1037
    if (!pVal) {
387,556,071✔
1038
      uError("failed to set stream pseudo func param val, NULL val for funcId: %d", funcId);
×
1039
      return TSDB_CODE_INTERNAL_ERROR;
×
1040
    }
1041
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, (void*)pVal);
387,556,071✔
1042
    if (code != 0) {
387,570,808✔
1043
      uError("failed to set value node value: %s", tstrerror(code));
62,383✔
1044
      return code;
×
1045
    }
1046
  } else {
1047
    uError("invalid placeholder function type: %d", t);
48,704✔
1048
    return TSDB_CODE_INTERNAL_ERROR;
×
1049
  }
1050
  
1051
  return code;
400,965,465✔
1052
}
1053

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