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

taosdata / TDengine / #5025

17 Apr 2026 01:15AM UTC coverage: 73.009% (+0.06%) from 72.954%
#5025

push

travis-ci

web-flow
fix: replace \s with [[:space:]] in shell regex for macOS BSD compat (#35162)

6 of 7 new or added lines in 2 files covered. (85.71%)

2432 existing lines in 145 files now uncovered.

273326 of 374375 relevant lines covered (73.01%)

130781443.02 hits per line

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

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

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

52
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
438,546,801✔
53
    if (TSDB_CODE_SUCCESS != taosHashPut(gFunMgtService.pFuncNameHashTable, funcMgtBuiltins[i].name,
436,648,330✔
54
                                         strlen(funcMgtBuiltins[i].name), &i, sizeof(int32_t))) {
436,648,330✔
55
      initFunctionCode = terrno;
×
56
      return;
×
57
    }
58
  }
59
  InitSpecialFuncId();
1,898,471✔
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,580,963✔
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,898,471✔
75
  (void)taosThreadOnce(&functionHashTableInit, doInitFunctionTable);
1,898,471✔
76
  return initFunctionCode;
1,898,471✔
77
}
78

79
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
842,581,419✔
80
  if (NULL != gFunMgtService.pFuncNameHashTable) {
842,581,419✔
81
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
842,418,632✔
82
    if (NULL != pVal) {
842,437,963✔
83
      pFunc->funcId = *(int32_t*)pVal;
842,234,662✔
84
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
842,234,658✔
85
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
842,233,951✔
86
    }
87
    return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
203,301✔
88
  }
89
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
3,251,075✔
90
    if (0 == strcmp(funcMgtBuiltins[i].name, pFunc->functionName)) {
3,251,040✔
91
      pFunc->funcId = i;
174,720✔
92
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
174,720✔
93
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
174,720✔
94
    }
95
  }
96
  return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
35✔
97
}
98

99
EFuncReturnRows fmGetFuncReturnRows(SFunctionNode* pFunc) {
17,734,954✔
100
  if (NULL != funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc) {
17,734,954✔
101
    return funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc(pFunc);
12,520,157✔
102
  }
103
  return (fmIsIndefiniteRowsFunc(pFunc->funcId) || fmIsMultiRowsFunc(pFunc->funcId)) ? FUNC_RETURN_ROWS_INDEFINITE
5,214,797✔
104
                                                                                     : FUNC_RETURN_ROWS_NORMAL;
5,214,797✔
105
}
106

107
bool canCoexistIndefiniteRowsFunc(int32_t funcId1, int32_t funcId2) {
171,289✔
108
  if (fmIsUserDefinedFunc(funcId1) || funcId1 < 0 || funcId1 >= funcMgtBuiltinsNum ||
342,578✔
109
      fmIsUserDefinedFunc(funcId2) || funcId2 < 0 || funcId2 >= funcMgtBuiltinsNum) {
342,578✔
110
    return false;
×
111
  }
112
  if (funcId1 == funcId2) {
171,289✔
113
    return true;
158,590✔
114
  }
115
  if ((funcMgtBuiltins[funcId1].type == FUNCTION_TYPE_LAG || funcMgtBuiltins[funcId1].type == FUNCTION_TYPE_LEAD) &&
12,699✔
116
      (funcMgtBuiltins[funcId2].type == FUNCTION_TYPE_LAG || funcMgtBuiltins[funcId2].type == FUNCTION_TYPE_LEAD)) {
12,699✔
117
    return true;
12,699✔
118
  }
119
  return false;
×
120
}
121

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

126
EFunctionType fmGetFuncType(const char* pFunc) {
612,690,700✔
127
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
612,690,700✔
128
  if (NULL != pVal) {
612,708,577✔
129
    return funcMgtBuiltins[*(int32_t*)pVal].type;
612,502,197✔
130
  }
131
  return FUNCTION_TYPE_UDF;
206,380✔
132
}
133

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

141
EFuncDataRequired fmFuncDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
41,125,162✔
142
  if (fmIsUserDefinedFunc(pFunc->funcId) || pFunc->funcId < 0 || pFunc->funcId >= funcMgtBuiltinsNum) {
41,125,162✔
UNCOV
143
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
144
  }
145
  if (NULL == funcMgtBuiltins[pFunc->funcId].dataRequiredFunc) {
41,130,181✔
146
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
147
  }
148
  return funcMgtBuiltins[pFunc->funcId].dataRequiredFunc(pFunc, pTimeWindow);
41,130,023✔
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;
×
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;
603,337✔
159
    ;
160
  }
161

162
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
2,147,483,647✔
163
    return FUNC_DATA_REQUIRED_DATA_LOAD;
188,810,225✔
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) {
307,687,859✔
170
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
307,687,859✔
171
    return TSDB_CODE_FAILED;
15,661✔
172
  }
173
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
307,673,114✔
174
  pFpSet->init = funcMgtBuiltins[funcId].initFunc;
307,645,355✔
175
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
307,655,943✔
176
  pFpSet->finalize = funcMgtBuiltins[funcId].finalizeFunc;
307,638,679✔
177
  pFpSet->combine = funcMgtBuiltins[funcId].combineFunc;
307,654,557✔
178
  pFpSet->processFuncByRow = funcMgtBuiltins[funcId].processFuncByRow;
307,656,894✔
179
  pFpSet->cleanup = funcMgtBuiltins[funcId].cleanupFunc;
307,687,525✔
180
  return TSDB_CODE_SUCCESS;
307,639,911✔
181
}
182

183
int32_t fmGetUdafExecFuncs(int32_t funcId, SFuncExecFuncs* pFpSet) {
36,967✔
184
#ifdef USE_UDF
185
  if (!fmIsUserDefinedFunc(funcId)) {
36,967✔
186
    return TSDB_CODE_FAILED;
×
187
  }
188
  pFpSet->getEnv = udfAggGetEnv;
36,967✔
189
  pFpSet->init = udfAggInit;
36,967✔
190
  pFpSet->process = udfAggProcess;
36,967✔
191
  pFpSet->finalize = udfAggFinalize;
36,967✔
192
  return TSDB_CODE_SUCCESS;
36,967✔
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,718,215✔
199
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,127,718,215✔
200
    return TSDB_CODE_FAILED;
115,039✔
201
  }
202
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
1,127,788,585✔
203
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
1,127,863,929✔
204
  return TSDB_CODE_SUCCESS;
1,127,904,457✔
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,307,195,040✔
212

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

215
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
1,928,958,797✔
216

217
bool fmIsTimelineFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TIMELINE_FUNC); }
1,179,269,041✔
218

219
bool fmIsDateTimeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_DATETIME_FUNC); }
596,405,729✔
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,462,289,935✔
226

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

229
bool fmIsStreamWindowClauseFunc(int32_t funcId) { return fmIsWindowClauseFunc(funcId) || fmIsPlaceHolderFunc(funcId); }
889,179✔
230

231
bool fmIsStreamVectorFunc(int32_t funcId) { return fmIsVectorFunc(funcId) || fmIsPlaceHolderFunc(funcId); }
10,574✔
232

233
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
1,586,205,297✔
234

235
bool fmIsSpecialDataRequiredFunc(int32_t funcId) {
92,807,907✔
236
  return isSpecificClassifyFunc(funcId, FUNC_MGT_SPECIAL_DATA_REQUIRED);
92,807,907✔
237
}
238

239
bool fmIsDynamicScanOptimizedFunc(int32_t funcId) {
44,402,580✔
240
  return isSpecificClassifyFunc(funcId, FUNC_MGT_DYNAMIC_SCAN_OPTIMIZED);
44,402,580✔
241
}
242

243
bool fmIsMultiResFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_RES_FUNC); }
1,214,679,203✔
244

245
bool fmIsRepeatScanFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_REPEAT_SCAN_FUNC); }
1,191,169,368✔
246

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

249
bool fmIsForbidFillFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_FILL_FUNC); }
596,659,519✔
250

251
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
64,718,280✔
252

253
bool fmIsSystemInfoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SYSTEM_INFO_FUNC); }
599,652,750✔
254

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

257
bool fmIsClientPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CLIENT_PC_FUNC); }
607,240,919✔
258

259
bool fmIsMultiRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_ROWS_FUNC); }
1,188,769,788✔
260

261
static bool fmIsKeepOrderFuncId(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_KEEP_ORDER_FUNC); }
720,539,673✔
262

263
bool fmIsKeepOrderFunc(SFunctionNode* pFunc) {
726,928,992✔
264
  if (pFunc->funcType == FUNCTION_TYPE_TOP || pFunc->funcType == FUNCTION_TYPE_BOTTOM ||
726,928,992✔
265
      pFunc->funcType == FUNCTION_TYPE_SAMPLE) {
722,877,899✔
266
    if (pFunc->pParameterList != NULL && pFunc->pParameterList->length >= 2) {
6,402,575✔
267
      SNode* pParam = nodesListGetNode(pFunc->pParameterList, 1);
6,418,904✔
268
      if (pParam != NULL && nodeType(pParam) == QUERY_NODE_VALUE) {
6,418,904✔
269
        SValueNode* pConst = (SValueNode*)pParam;
6,418,904✔
270
        if (pConst->datum.i == 1) {
6,418,904✔
271
          return true;
748,561✔
272
        }
273
      }
274
    }
275
    return false;
5,660,617✔
276
  }
277
  return fmIsKeepOrderFuncId(pFunc->funcId);
720,549,319✔
278
}
279

280
bool fmIsCumulativeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CUMULATIVE_FUNC); }
157,292,978✔
281

282
bool fmIsForbidSysTableFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_SYSTABLE_FUNC); }
596,596,099✔
283

284
bool fmIsInterpFunc(int32_t funcId) {
1,222,165,272✔
285
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,222,165,272✔
286
    return false;
384,067✔
287
  }
288
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
1,221,783,634✔
289
}
290

291
bool fmIsInterpPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERP_PC_FUNC); }
1,203,112,629✔
292

293
bool fmIsForecastFunc(int32_t funcId) {
1,173,357,424✔
294
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,173,357,424✔
295
    return false;
383,586✔
296
  }
297
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
1,172,976,393✔
298
}
299

300
bool fmIsAnalysisPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_ANALYTICS_PC_FUNC); }
1,206,066,644✔
301

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

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

312
bool fmIsLastRowFunc(int32_t funcId) {
70,943,338✔
313
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
70,943,338✔
UNCOV
314
    return false;
×
315
  }
316
  return FUNCTION_TYPE_LAST_ROW == funcMgtBuiltins[funcId].type;
70,943,973✔
317
}
318

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

326
bool fmIsNotNullOutputFunc(int32_t funcId) {
463,188,357✔
327
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
463,188,357✔
328
    return false;
90,332✔
329
  }
330
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
463,105,452✔
331
         FUNCTION_TYPE_CACHE_LAST == funcMgtBuiltins[funcId].type ||
446,250,914✔
332
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
444,877,442✔
333
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
435,812,239✔
334
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
432,175,026✔
335
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
421,154,130✔
336
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
415,731,901✔
337
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
413,670,177✔
338
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
359,717,104✔
339
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
1,265,285,442✔
340
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
355,927,017✔
341
}
342

343
bool fmIsSelectValueFunc(int32_t funcId) {
357,126,053✔
344
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
357,126,053✔
345
    return false;
×
346
  }
347
  return FUNCTION_TYPE_SELECT_VALUE == funcMgtBuiltins[funcId].type;
357,126,947✔
348
}
349

350
bool fmIsGroupKeyFunc(int32_t funcId) {
276,362,122✔
351
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
276,362,122✔
352
    return false;
×
353
  }
354
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
276,366,899✔
355
}
356

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

364
bool fmisSelectGroupConstValueFunc(int32_t funcId) {
17,366,134✔
365
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
17,366,134✔
366
    return false;
×
367
  }
368
  return FUNCTION_TYPE_GROUP_CONST_VALUE == funcMgtBuiltins[funcId].type;
17,366,134✔
369
}
370

371
bool fmIsElapsedFunc(int32_t funcId) {
10,827,207✔
372
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
10,827,207✔
373
    return false;
×
374
  }
375
  return FUNCTION_TYPE_ELAPSED == funcMgtBuiltins[funcId].type;
10,827,207✔
376
}
377

378
bool fmIsBlockDistFunc(int32_t funcId) {
596,401,017✔
379
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
596,401,017✔
380
    return false;
193,524✔
381
  }
382
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
596,208,230✔
383
}
384

385
bool fmIsDBUsageFunc(int32_t funcId) {
596,404,731✔
386
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
596,404,731✔
387
    return false;
187,874✔
388
  }
389
  return FUNCTION_TYPE_DB_USAGE == funcMgtBuiltins[funcId].type;
596,219,333✔
390
}
391

392
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
797,127,762✔
393

394
bool fmIsIgnoreNullFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IGNORE_NULL_FUNC); }
324,908✔
395

396
void fmFuncMgtDestroy() {
1,898,504✔
397
  void* m = gFunMgtService.pFuncNameHashTable;
1,898,504✔
398
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
1,898,504✔
399
    taosHashCleanup(m);
1,898,458✔
400
  }
401
}
1,898,504✔
402

403
#ifdef BUILD_NO_CALL
404
int32_t fmSetNormalFunc(int32_t funcId, SFuncExecFuncs* pFpSet) {
405
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
406
    return TSDB_CODE_FAILED;
407
  }
408
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
409
  return TSDB_CODE_SUCCESS;
410
}
411
#endif
412

413
// function has same input/output type
414
bool fmIsSameInOutType(int32_t funcId) {
49,436,106✔
415
  bool res = false;
49,436,106✔
416
  switch (funcMgtBuiltins[funcId].type) {
49,436,106✔
417
    case FUNCTION_TYPE_MAX:
19,562,877✔
418
    case FUNCTION_TYPE_MIN:
419
    case FUNCTION_TYPE_TOP:
420
    case FUNCTION_TYPE_BOTTOM:
421
    case FUNCTION_TYPE_FIRST:
422
    case FUNCTION_TYPE_LAST:
423
    case FUNCTION_TYPE_SAMPLE:
424
    case FUNCTION_TYPE_TAIL:
425
    case FUNCTION_TYPE_UNIQUE:
426
      res = true;
19,562,877✔
427
      break;
19,562,877✔
428
    default:
29,873,229✔
429
      break;
29,873,229✔
430
  }
431
  return res;
49,436,106✔
432
}
433

434
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
14,917,748✔
435
  SNode* pNode;
436
  FOREACH(pNode, pFunc->pParameterList) {
15,003,548✔
437
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
154,039✔
438
      return false;
68,239✔
439
    }
440
  }
441
  return true;
14,849,509✔
442
}
443

444
bool fmIsSkipScanCheckFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SKIP_SCAN_CHECK_FUNC); }
44,398,577✔
445

446
bool fmIsPrimaryKeyFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PRIMARY_KEY_FUNC); }
519,622,918✔
447

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

450
bool fmIsPlaceHolderFuncForExternalWin(int32_t funcId) {
524,450✔
451
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
524,450✔
452
    return false;
×
453
  }
454
  return (funcMgtBuiltins[funcId].type == FUNCTION_TYPE_WSTART || funcMgtBuiltins[funcId].type == FUNCTION_TYPE_WEND ||
877,532✔
455
          funcMgtBuiltins[funcId].type == FUNCTION_TYPE_WDURATION);
353,082✔
456
}
457

458
void getLastCacheDataType(SDataType* pType, int32_t pkBytes) {
×
459
  // TODO: do it later.
460
  pType->bytes = getFirstLastInfoSize(pType->bytes, pkBytes) + VARSTR_HEADER_SIZE;
×
461
  pType->type = TSDB_DATA_TYPE_BINARY;
×
462
}
×
463

464
static int32_t getFuncInfo(SFunctionNode* pFunc) {
157,946,566✔
465
  char msg[128] = {0};
157,946,566✔
466
  return fmGetFuncInfo(pFunc, msg, sizeof(msg));
157,946,566✔
467
}
468

469
int32_t createFunction(const char* pName, SNodeList* pParameterList, SFunctionNode** ppFunc) {
10,131,798✔
470
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
10,131,798✔
471
  if (NULL == *ppFunc) {
10,131,798✔
472
    return code;
×
473
  }
474
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
10,131,798✔
475
  (*ppFunc)->pParameterList = pParameterList;
10,131,798✔
476
  code = getFuncInfo((*ppFunc));
10,131,798✔
477
  if (TSDB_CODE_SUCCESS != code) {
10,131,798✔
478
    (*ppFunc)->pParameterList = NULL;
×
479
    nodesDestroyNode((SNode*)*ppFunc);
×
480
    *ppFunc = NULL;
×
481
    return code;
×
482
  }
483
  return code;
10,131,798✔
484
}
485

486
static void resetOutputChangedFunc(SFunctionNode *pFunc, const SFunctionNode* pSrcFunc) {
147,816,682✔
487
  if (funcMgtBuiltins[pFunc->funcId].type == FUNCTION_TYPE_LAST_MERGE) {
147,816,682✔
488
    pFunc->node.resType = pSrcFunc->node.resType;
23,248,097✔
489
    return;
23,248,097✔
490
  }
491
}
492

493
int32_t createFunctionWithSrcFunc(const char* pName, const SFunctionNode* pSrcFunc, SNodeList* pParameterList, SFunctionNode** ppFunc) {
147,816,024✔
494
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
147,816,024✔
495
  if (NULL == *ppFunc) {
147,819,204✔
496
    return code;
×
497
  }
498

499
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
147,819,204✔
500
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
147,819,161✔
501
  (*ppFunc)->pSrcFuncRef = pSrcFunc;
147,819,161✔
502

503
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
147,819,161✔
504
  (*ppFunc)->pParameterList = pParameterList;
147,819,161✔
505
  code = getFuncInfo((*ppFunc));
147,819,161✔
506
  if (TSDB_CODE_SUCCESS != code) {
147,818,234✔
507
    (*ppFunc)->pParameterList = NULL;
×
508
    nodesDestroyNode((SNode*)*ppFunc);
×
509
    *ppFunc = NULL;
×
510
    return code;
×
511
  }
512
  resetOutputChangedFunc(*ppFunc, pSrcFunc);
147,818,234✔
513
  (*ppFunc)->node.relatedTo = pSrcFunc->node.relatedTo;
147,818,244✔
514
  (*ppFunc)->node.bindExprID = pSrcFunc->node.bindExprID;
147,818,244✔
515
  return code;
147,818,244✔
516
}
517

518
static int32_t createColumnByFunc(const SFunctionNode* pFunc, SColumnNode** ppCol) {
98,370,639✔
519
  int32_t code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)ppCol);
98,370,639✔
520
  if (NULL == *ppCol) {
98,377,387✔
521
    return code;
×
522
  }
523
  tstrncpy((*ppCol)->colName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
98,377,387✔
524
  (*ppCol)->node.resType = pFunc->node.resType;
98,377,387✔
525
  return TSDB_CODE_SUCCESS;
98,377,387✔
526
}
527

528
bool fmIsDistExecFunc(int32_t funcId) {
376,221,915✔
529
  if (fmIsUserDefinedFunc(funcId)) {
376,221,915✔
530
    return false;
104,040✔
531
  }
532
  if (!fmIsVectorFunc(funcId)) {
376,143,371✔
533
    return true;
404,501✔
534
  }
535
  return (NULL != funcMgtBuiltins[funcId].pPartialFunc && NULL != funcMgtBuiltins[funcId].pMergeFunc);
375,745,973✔
536
}
537

538
static int32_t createPartialFunction(const SFunctionNode* pSrcFunc, SFunctionNode** pPartialFunc) {
49,437,812✔
539
  SNodeList* pParameterList = NULL;
49,437,812✔
540
  int32_t    code = nodesCloneList(pSrcFunc->pParameterList, &pParameterList);
49,437,812✔
541
  if (NULL == pParameterList) {
49,440,355✔
542
    return code;
×
543
  }
544
  code =
545
      createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pPartialFunc, pSrcFunc, pParameterList, pPartialFunc);
49,440,355✔
546
  if (TSDB_CODE_SUCCESS != code) {
49,439,523✔
547
    nodesDestroyList(pParameterList);
2,435✔
548
    return code;
×
549
  }
550
  (*pPartialFunc)->hasOriginalFunc = true;
49,437,088✔
551
  (*pPartialFunc)->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
49,437,088✔
552
  char name[TSDB_FUNC_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_POINTER_PRINT_BYTES + 1] = {0};
49,437,088✔
553

554
  int32_t len = snprintf(name, sizeof(name), "%s.%p", (*pPartialFunc)->functionName, pSrcFunc);
49,437,088✔
555
  if (taosHashBinary(name, len, sizeof(name)) < 0) {
49,439,305✔
556
    return TSDB_CODE_FAILED;
×
557
  }
558
  tstrncpy((*pPartialFunc)->node.aliasName, name, TSDB_COL_NAME_LEN);
49,439,305✔
559
  return TSDB_CODE_SUCCESS;
49,439,305✔
560
}
561

562
static int32_t createMergeFuncPara(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
98,370,190✔
563
                                   SNodeList** pParameterList) {
564
  SNode*  pRes = NULL;
98,370,190✔
565
  int32_t code = createColumnByFunc(pPartialFunc, (SColumnNode**)&pRes);
98,370,190✔
566
  if (TSDB_CODE_SUCCESS != code) {
98,377,412✔
567
    return code;
×
568
  }
569
  if (NULL != funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc) {
98,377,412✔
570
    return funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc(pSrcFunc->pParameterList, pRes, pParameterList);
858,852✔
571
  } else {
572
    return nodesListMakeStrictAppend(pParameterList, pRes);
97,518,560✔
573
  }
574
}
575

576
static int32_t createMidFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
48,933,556✔
577
                                 SFunctionNode** pMidFunc) {
578
  SNodeList*     pParameterList = NULL;
48,933,556✔
579
  SFunctionNode* pFunc = NULL;
48,933,556✔
580

581
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
48,933,556✔
582
  if (TSDB_CODE_SUCCESS == code) {
48,939,195✔
583
    if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){
48,939,289✔
584
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pSrcFunc, pParameterList, &pFunc);
5,583,156✔
585
    }else{
586
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
43,356,133✔
587
    }
588
  }
589
  if (TSDB_CODE_SUCCESS == code) {
48,938,683✔
590
    tstrncpy(pFunc->node.aliasName, pPartialFunc->node.aliasName, TSDB_COL_NAME_LEN);
48,938,934✔
591
  }
592

593
  if (TSDB_CODE_SUCCESS == code) {
48,938,683✔
594
    *pMidFunc = pFunc;
48,938,683✔
595
  } else {
596
    nodesDestroyList(pParameterList);
×
597
  }
598
  return code;
48,938,896✔
599
}
600

601
static int32_t createMergeFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
49,436,601✔
602
                                   SFunctionNode** pMergeFunc) {
603
  SNodeList*     pParameterList = NULL;
49,436,601✔
604
  SFunctionNode* pFunc = NULL;
49,436,601✔
605

606
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
49,436,601✔
607
  if (TSDB_CODE_SUCCESS == code) {
49,440,611✔
608
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
49,440,611✔
609
  }
610
  if (TSDB_CODE_SUCCESS == code) {
49,442,724✔
611
    pFunc->hasOriginalFunc = true;
49,440,565✔
612
    pFunc->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
49,440,565✔
613
    // overwrite function restype set by translate function
614
    if (fmIsSameInOutType(pSrcFunc->funcId)) {
49,440,565✔
615
      pFunc->node.resType = pSrcFunc->node.resType;
19,562,925✔
616
    }
617
    tstrncpy(pFunc->node.aliasName, pSrcFunc->node.aliasName, TSDB_COL_NAME_LEN);
49,436,248✔
618
  }
619

620
  if (TSDB_CODE_SUCCESS == code) {
49,438,407✔
621
    *pMergeFunc = pFunc;
49,438,407✔
622
  } else {
623
    nodesDestroyList(pParameterList);
×
624
  }
625
  return code;
49,439,335✔
626
}
627

628
int32_t fmGetDistMethod(const SFunctionNode* pFunc, SFunctionNode** pPartialFunc, SFunctionNode** pMidFunc,
49,434,259✔
629
                        SFunctionNode** pMergeFunc) {
630
  if (!fmIsDistExecFunc(pFunc->funcId)) {
49,434,259✔
631
    return TSDB_CODE_FAILED;
×
632
  }
633

634
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
49,440,375✔
635
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
49,437,280✔
636
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
48,935,367✔
637
  }
638
  if (TSDB_CODE_SUCCESS == code) {
49,440,218✔
639
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
49,439,769✔
640
  }
641

642
  if (TSDB_CODE_SUCCESS != code) {
49,440,053✔
643
    nodesDestroyNode((SNode*)*pPartialFunc);
×
644
    if (pMidFunc) nodesDestroyNode((SNode*)*pMidFunc);
×
645
    nodesDestroyNode((SNode*)*pMergeFunc);
×
646
  }
647

648
  return code;
49,439,817✔
649
}
650

651
const char* fmGetFuncName(int32_t funcId) {
342,720✔
652
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
342,720✔
653
    return "invalid function";
×
654
  }
655
  return funcMgtBuiltins[funcId].name;
342,720✔
656
}
657

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

676
bool fmIsTSMASupportedFunc(func_id_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC); }
1,290,000✔
677

678
bool fmIsRsmaSupportedFunc(func_id_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_RSMA_FUNC); }
137,220✔
679

680
int32_t fmCreateStateFuncs(SNodeList* pFuncs) {
3,357✔
681
  int32_t code;
682
  SNode*  pNode;
683
  char    buf[128] = {0};
3,357✔
684
  FOREACH(pNode, pFuncs) {
10,995✔
685
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
7,638✔
686
    code = fmGetFuncInfo(pFunc, buf, 128);
7,638✔
687
    if (code) break;
7,638✔
688
    if (fmIsTSMASupportedFunc(pFunc->funcId)) {
7,638✔
689
      SFunctionNode* pNewFunc = NULL;
7,638✔
690
      code = fmCreateStateFunc(pFunc, &pNewFunc);
7,638✔
691
      if (code) {
7,638✔
692
        // error
693
        break;
×
694
      } else if (!pNewFunc) {
7,638✔
695
        // no need state func
696
        continue;
×
697
      } else {
698
        REPLACE_NODE(pNewFunc);
7,638✔
699
        nodesDestroyNode(pNode);
7,638✔
700
      }
701
    }
702
  }
703
  return code;
3,357✔
704
}
705

706
static int32_t fmCreateStateMergeFunc(SFunctionNode* pFunc, SFunctionNode** pStateMergeFunc) {
1,683✔
707
  if (funcMgtBuiltins[pFunc->funcId].pMergeFunc) {
1,683✔
708
    SNodeList* pParams = NULL;
1,377✔
709
    int32_t    code = nodesCloneList(pFunc->pParameterList, &pParams);
1,377✔
710
    if (!pParams) return code;
1,377✔
711
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pFunc->funcId].pMergeFunc, pFunc, pParams, pStateMergeFunc);
1,377✔
712
    if (TSDB_CODE_SUCCESS != code) {
1,377✔
713
      nodesDestroyList(pParams);
×
714
      return code;
×
715
    }
716
    tstrncpy((*pStateMergeFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
1,377✔
717
    tstrncpy((*pStateMergeFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
1,377✔
718
  }
719
  return TSDB_CODE_SUCCESS;
1,683✔
720
}
721

722
int32_t fmCreateStateMergeFuncs(SNodeList* pFuncs) {
765✔
723
  int32_t code;
724
  SNode*  pNode;
725
  char    buf[128] = {0};
765✔
726
  FOREACH(pNode, pFuncs) {
2,448✔
727
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
1,683✔
728
    if (fmIsTSMASupportedFunc(pFunc->funcId)) {
1,683✔
729
      SFunctionNode* pNewFunc = NULL;
1,683✔
730
      code = fmCreateStateMergeFunc(pFunc, &pNewFunc);
1,683✔
731
      if (code) {
1,683✔
732
        // error
733
        break;
×
734
      } else if (!pNewFunc) {
1,683✔
735
        // no state merge func
736
        continue;
306✔
737
      } else {
738
        REPLACE_NODE(pNewFunc);
1,377✔
739
        nodesDestroyNode(pNode);
1,377✔
740
      }
741
    }
742
  }
743
  return code;
765✔
744
}
745

746
int32_t fmGetFuncId(const char* name) {
9,528,497✔
747
  if (NULL != gFunMgtService.pFuncNameHashTable) {
9,528,497✔
748
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, name, strlen(name));
9,528,497✔
749
    if (NULL != pVal) {
9,528,497✔
750
      return *(int32_t*)pVal;
9,528,497✔
751
    }
752
    return -1;
×
753
  }
754
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
×
755
    if (0 == strcmp(funcMgtBuiltins[i].name, name)) {
×
756
      return i;
×
757
    }
758
  }
759
  return -1;
×
760
}
761

762
int32_t fmGetTwstartFuncId() {
2,038,471✔
763
  static int32_t twstartFuncId = -2;
764
  if (twstartFuncId < 0) {
2,038,471✔
765
    twstartFuncId = fmGetFuncId("_twstart");
1,898,471✔
766
  }
767
  return twstartFuncId;
2,038,471✔
768
}
769

770
int32_t fmGetTwendFuncId() {
1,929,839✔
771
  static int32_t twendFuncId = -2;
772
  if (twendFuncId < 0) {
1,929,839✔
773
    twendFuncId = fmGetFuncId("_twend");
1,898,471✔
774
  }
775
  return twendFuncId;
1,929,839✔
776
}
777

778
int32_t fmGetTwdurationFuncId() {
1,898,887✔
779
  static int32_t twdurationFuncId = -2;
780
  if (twdurationFuncId < 0) {
1,898,887✔
781
    twdurationFuncId = fmGetFuncId("_twduration");
1,898,471✔
782
  }
783
  return twdurationFuncId;
1,898,887✔
784
}
785

786
int32_t fmGetExternalWindowColumnFuncId() {
1,934,696✔
787
  static int32_t externalWindowColumnFuncId = -2;
788
  if (externalWindowColumnFuncId < 0) {
1,934,696✔
789
    externalWindowColumnFuncId = fmGetFuncId("_external_window_column");
1,898,471✔
790
  }
791
  return externalWindowColumnFuncId;
1,934,696✔
792
}
793

794
bool fmIsMyStateFunc(int32_t funcId, int32_t stateFuncId) {
2,160,513✔
795
  const SBuiltinFuncDefinition* pFunc = &funcMgtBuiltins[funcId];
2,160,513✔
796
  const SBuiltinFuncDefinition* pStateFunc = &funcMgtBuiltins[stateFuncId];
2,160,513✔
797
  if (!pFunc->pStateFunc) {
2,160,513✔
798
    return false;
×
799
  }
800
  if (strcmp(pFunc->pStateFunc, pStateFunc->name) == 0) return true;
2,160,513✔
801
  int32_t stateMergeFuncId = fmGetFuncId(pFunc->pStateFunc);
782,289✔
802
  if (stateMergeFuncId == -1) {
782,289✔
803
    return false;
×
804
  }
805
  const SBuiltinFuncDefinition* pStateMergeFunc = &funcMgtBuiltins[stateMergeFuncId];
782,289✔
806
  return strcmp(pStateFunc->name, pStateMergeFunc->pMergeFunc) == 0;
782,289✔
807
}
808

809
bool fmIsCountLikeFunc(int32_t funcId) {
583,081,930✔
810
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
583,081,930✔
811
}
812

813
bool fmIsRowTsOriginFunc(int32_t funcId) {
5,381,226✔
814
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
5,381,226✔
815
    return false;
×
816
  }
817
  return FUNCTION_TYPE_IROWTS_ORIGIN == funcMgtBuiltins[funcId].type;
5,381,226✔
818
}
819

820
bool fmIsGroupIdFunc(int32_t funcId) {
×
821
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
822
    return false;
×
823
  }
824
  return FUNCTION_TYPE_GROUP_ID == funcMgtBuiltins[funcId].type;
×
825
}
826

827
const void* fmGetExternalWindowColumnFuncVal(const SStreamRuntimeFuncInfo* pStreamRuntimeFuncInfo, int32_t index) {
10,354,080✔
828
  SSTriggerCalcParam *pParams = taosArrayGet(pStreamRuntimeFuncInfo->pStreamPesudoFuncVals, pStreamRuntimeFuncInfo->curIdx);
10,354,080✔
829
  if (pParams == NULL || pParams->pExternalWindowData == NULL || index < 0) {
10,354,894✔
830
    return NULL;
×
831
  }
832
  return taosArrayGet(pParams->pExternalWindowData, index);
10,354,894✔
833
}
834

835
const void* fmGetStreamPesudoFuncVal(int32_t funcId, const SStreamRuntimeFuncInfo* pStreamRuntimeFuncInfo) {
387,689,539✔
836
  switch (funcMgtBuiltins[funcId].type) {
387,689,539✔
837
    case FUNCTION_TYPE_TGRPID:
137,038✔
838
      return &pStreamRuntimeFuncInfo->groupId;
137,038✔
839
    case FUNCTION_TYPE_PLACEHOLDER_COLUMN:
901,760✔
840
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
901,760✔
841
    case FUNCTION_TYPE_PLACEHOLDER_TBNAME:
2,132,895✔
842
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
2,132,895✔
843
    default:
384,598,895✔
844
      break;
384,598,895✔
845
  }
846

847
  SSTriggerCalcParam *pParams = taosArrayGet(pStreamRuntimeFuncInfo->pStreamPesudoFuncVals, pStreamRuntimeFuncInfo->curIdx);
384,598,895✔
848
  switch (funcMgtBuiltins[funcId].type) {
384,646,737✔
849
    case FUNCTION_TYPE_TPREV_TS:
×
850
      return &pParams->prevTs;
×
851
    case FUNCTION_TYPE_TCURRENT_TS:
2,159,328✔
852
      return &pParams->currentTs;
2,159,328✔
853
    case FUNCTION_TYPE_TNEXT_TS:
×
854
      return &pParams->nextTs;
×
855
    case FUNCTION_TYPE_TWSTART:
332,232,080✔
856
      return &pParams->wstart;
332,232,080✔
857
    case FUNCTION_TYPE_TWEND:
17,232,936✔
858
      return &pParams->wend;
17,232,936✔
859
    case FUNCTION_TYPE_TWDURATION:
1,104,156✔
860
      return &pParams->wduration;
1,104,156✔
861
    case FUNCTION_TYPE_TWROWNUM:
30,819,412✔
862
      return &pParams->wrownum;
30,819,412✔
863
    case FUNCTION_TYPE_TPREV_LOCALTIME:
168,937✔
864
      return &pParams->prevLocalTime;
168,937✔
865
    case FUNCTION_TYPE_TLOCALTIME:
581,114✔
866
      return &pParams->triggerTime;
581,114✔
867
    case FUNCTION_TYPE_TNEXT_LOCALTIME:
287,649✔
868
      return &pParams->nextLocalTime;
287,649✔
869
    case FUNCTION_TYPE_TGRPID:
×
870
      return &pStreamRuntimeFuncInfo->groupId;
×
871
    case FUNCTION_TYPE_PLACEHOLDER_COLUMN:
×
872
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
×
873
    case FUNCTION_TYPE_PLACEHOLDER_TBNAME:
×
874
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
×
875
    case FUNCTION_TYPE_TIDLESTART:
6,048✔
876
      return &pParams->idlestart;
6,048✔
877
    case FUNCTION_TYPE_TIDLEEND:
6,048✔
878
      return &pParams->idleend;
6,048✔
879
    default:
×
880
      break;
×
881
  }
882
  return NULL;
×
883
}
884

885
bool fmIsStreamPesudoColVal(int32_t funcId) {
540,245✔
886
  return funcMgtBuiltins[funcId].type == FUNCTION_TYPE_PLACEHOLDER_COLUMN
540,245✔
887
         || funcMgtBuiltins[funcId].type == FUNCTION_TYPE_PLACEHOLDER_TBNAME;
540,245✔
888
}
889

890
int32_t fmGetStreamPesudoFuncEnv(int32_t funcId, SNodeList* pParamNodes, SFuncExecEnv *pEnv) {
7,296,330✔
891
  if (NULL == pParamNodes || pParamNodes->length < 1) {
7,296,330✔
892
    uError("invalid stream pesudo func param list %p", pParamNodes);
×
893
    return TSDB_CODE_INTERNAL_ERROR;
×
894
  }
895

896
  int32_t firstParamType = nodeType(nodesListGetNode(pParamNodes, 0));
7,297,353✔
897
  if (QUERY_NODE_VALUE != firstParamType) {
7,297,072✔
898
    uError("invalid stream pesudo func first param type %d", firstParamType);
×
899
    return TSDB_CODE_INTERNAL_ERROR;
×
900
  }
901

902
  SValueNode* pResDesc = (SValueNode*)nodesListGetNode(pParamNodes, 0);
7,297,072✔
903
  pEnv->calcMemSize = pResDesc->node.resType.bytes;
7,295,389✔
904

905
  return TSDB_CODE_SUCCESS;
7,296,673✔
906
}
907

908

909
int32_t fmSetStreamPseudoFuncParamVal(int32_t funcId, SNodeList* pParamNodes, const SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
398,027,969✔
910
  if (!pStreamRuntimeInfo) {
398,027,969✔
911
    uError("internal error, should have pVals for stream pseudo funcs");
×
912
    return TSDB_CODE_INTERNAL_ERROR;
×
913
  }
914
  int32_t code = 0;
398,027,969✔
915
  SArray *pVals1 = NULL;
398,027,969✔
916
  SNode* pFirstParam = nodesListGetNode(pParamNodes, 0);
398,027,969✔
917
  if (nodeType(pFirstParam) != QUERY_NODE_VALUE) {
398,094,505✔
918
    uError("invalid param node type: %d for func: %d", nodeType(pFirstParam), funcId);
×
919
    return TSDB_CODE_INTERNAL_ERROR;
×
920
  }
921

922
  int32_t t = funcMgtBuiltins[funcId].type;
398,119,327✔
923
  uDebug("set stream pseudo func param val, functype: %d, pStreamRuntimeInfo: %p", t, pStreamRuntimeInfo);
398,099,588✔
924
  if (FUNCTION_TYPE_TGRPID == t) {
398,087,754✔
925
    SValue v = {0};
137,038✔
926
    v.type = TSDB_DATA_TYPE_BIGINT;
137,038✔
927
    v.val = *(int64_t*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
137,038✔
928
    
929
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&v, pFirstParam->type));
137,038✔
930
    if (code != 0) {
137,038✔
931
      uError("failed to set value node value: %s", tstrerror(code));
×
932
      return code;
×
933
    }
934
  } else if (FUNCTION_TYPE_PLACEHOLDER_COLUMN == t) {
397,950,716✔
935
    SNode* pSecondParam = nodesListGetNode(pParamNodes, 1);
901,760✔
936
    if (nodeType(pSecondParam) != QUERY_NODE_VALUE) {
901,520✔
937
      uError("invalid param node type: %d for func: %d", nodeType(pSecondParam), funcId);
×
938
      return TSDB_CODE_INTERNAL_ERROR;
×
939
    }
940
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
901,760✔
941
    int32_t idx = ((SValueNode*)pSecondParam)->datum.i;
901,279✔
942
    if (idx - 1 < 0 || idx - 1 >= taosArrayGetSize(pVal)) {
901,279✔
UNCOV
943
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
×
944
      return TSDB_CODE_INTERNAL_ERROR;
×
945
    }
946
    SStreamGroupValue* pValue = taosArrayGet(pVal, idx - 1);
901,520✔
947
    if (pValue == NULL) {
901,279✔
948
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
×
949
      return TSDB_CODE_INTERNAL_ERROR;
×
950
    }
951
    if (!pValue->isNull){
901,279✔
952
      if (pValue->data.type != ((SValueNode*)pFirstParam)->node.resType.type){
894,940✔
953
        uError("invalid value type: %d for func: %d, should be: %d", pValue->data.type, funcId, ((SValueNode*)pFirstParam)->node.resType.type);
×
954
        return TSDB_CODE_INTERNAL_ERROR;
×
955
      }
956
      if (IS_VAR_DATA_TYPE(((SValueNode*)pFirstParam)->node.resType.type)) {
894,940✔
957
        char* tmp = taosMemoryCalloc(1, pValue->data.nData + VARSTR_HEADER_SIZE); 
617,012✔
958
        if (tmp == NULL) {
617,252✔
959
          return TSDB_CODE_OUT_OF_MEMORY;
×
960
        }
961
        taosMemoryFree(((SValueNode*)pFirstParam)->datum.p);
617,252✔
962
        ((SValueNode*)pFirstParam)->datum.p = tmp;
617,252✔
963
        memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
617,252✔
964
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
617,252✔
965
      } else {
966
        code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&pValue->data, pValue->data.type));
277,928✔
967
      }
968
    }
969
    ((SValueNode*)pFirstParam)->isNull = pValue->isNull;
901,760✔
970
  } else if (FUNCTION_TYPE_PLACEHOLDER_TBNAME == t) {
397,048,956✔
971
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
2,072,090✔
972
    for (int32_t i = 0; i < taosArrayGetSize(pVal); ++i) {
2,515,211✔
973
      SStreamGroupValue* pValue = taosArrayGet(pVal, i);
2,514,669✔
974
      if (pValue != NULL && pValue->isTbname) {
2,515,211✔
975
        taosMemoryFreeClear(((SValueNode*)pFirstParam)->datum.p);
2,074,571✔
976
        ((SValueNode*)pFirstParam)->datum.p = taosMemoryCalloc(pValue->data.nData + VARSTR_HEADER_SIZE, 1);
2,074,571✔
977
        if (NULL == ((SValueNode*)pFirstParam)->datum.p ) {
2,073,756✔
978
          return terrno;
×
979
        }
980

981
        (void)memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
2,073,466✔
982
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
2,074,019✔
983
        break;
2,073,745✔
984
      }
985
    }
986
  } else if(FUNCTION_TYPE_EXTERNAL_WINDOW_COLUMN == t) {
394,976,866✔
987
    if (NULL == pParamNodes || LIST_LENGTH(pParamNodes) < 2) {
10,353,673✔
988
      uError("invalid stream external window column param list %p, len: %d", pParamNodes,
×
989
             pParamNodes ? LIST_LENGTH(pParamNodes) : 0);
990
      return TSDB_CODE_INTERNAL_ERROR;
×
991
    }
992

993
    SNode* pParamNode = nodesListGetNode(pParamNodes, 1);
10,354,487✔
994
    if (NULL == pParamNode || QUERY_NODE_VALUE != nodeType(pParamNode)) {
10,354,894✔
995
      uError("invalid stream external window column param node %p type %d for func: %d", pParamNode,
407✔
996
             pParamNode ? nodeType(pParamNode) : -1, funcId);
997
      return TSDB_CODE_INTERNAL_ERROR;
×
998
    }
999

1000
    const SStreamGroupValue* pVal =
1001
        fmGetExternalWindowColumnFuncVal(pStreamRuntimeInfo, ((SValueNode*)pParamNode)->placeholderNo);
10,354,487✔
1002
    if (!pVal) {
10,354,894✔
1003
      uError("failed to set stream pseudo func param val, NULL val for funcId: %d", funcId);
×
1004
      return TSDB_CODE_INTERNAL_ERROR;
×
1005
    }
1006
    if (pVal->isNull) {
10,354,894✔
1007
      ((SValueNode*)pFirstParam)->isNull = true;
814✔
1008
    } else {
1009
      ((SValueNode*)pFirstParam)->isNull = false;
10,354,080✔
1010
      if (IS_VAR_DATA_TYPE(pVal->data.type)) {
10,354,080✔
1011
        // pVal->data.pData already includes the VARSTR header from colDataGetData,
1012
        // so copy it directly as datum.p expects VARSTR format [header][raw data].
1013
        char* tmp = taosMemoryMalloc(pVal->data.nData);
8,140✔
1014
        if (tmp == NULL) {
8,140✔
1015
          return TSDB_CODE_OUT_OF_MEMORY;
×
1016
        }
1017
        taosMemoryFree(((SValueNode*)pFirstParam)->datum.p);
8,140✔
1018
        ((SValueNode*)pFirstParam)->datum.p = tmp;
8,140✔
1019
        memcpy(tmp, pVal->data.pData, pVal->data.nData);
8,140✔
1020
      } else {
1021
        code = nodesSetValueNodeValue((SValueNode*)pFirstParam, (void*)&pVal->data.val);
10,345,940✔
1022
      }
1023
    }
1024
    if (code != 0) {
10,354,080✔
1025
      uError("failed to set value node value: %s", tstrerror(code));
×
1026
      return code;
×
1027
    }
1028
  } else if (LIST_LENGTH(pParamNodes) == 1) {
769,269,283✔
1029
    // twstart, twend
1030
    const void* pVal = fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
384,639,211✔
1031
    if (!pVal) {
384,626,900✔
1032
      uError("failed to set stream pseudo func param val, NULL val for funcId: %d", funcId);
×
1033
      return TSDB_CODE_INTERNAL_ERROR;
×
1034
    }
1035
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, (void*)pVal);
384,626,900✔
1036
    if (code != 0) {
384,650,923✔
1037
      uError("failed to set value node value: %s", tstrerror(code));
4,833✔
1038
      return code;
×
1039
    }
1040
  } else {
1041
    uError("invalid placeholder function type: %d", t);
42,641✔
1042
    return TSDB_CODE_INTERNAL_ERROR;
×
1043
  }
1044
  
1045
  return code;
398,116,554✔
1046
}
1047

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