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

taosdata / TDengine / #4976

06 Mar 2026 09:48AM UTC coverage: 68.446% (+0.08%) from 68.37%
#4976

push

travis-ci

web-flow
feat(TDgpt): support multiple input data columns for anomaly detection. (#34606)

0 of 93 new or added lines in 9 files covered. (0.0%)

5718 existing lines in 144 files now uncovered.

211146 of 308486 relevant lines covered (68.45%)

136170362.0 hits per line

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

82.46
/source/libs/function/src/functionMgt.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "functionMgt.h"
17

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

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

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

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

44
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
297,238,590✔
45
    if (TSDB_CODE_SUCCESS != taosHashPut(gFunMgtService.pFuncNameHashTable, funcMgtBuiltins[i].name,
295,923,375✔
46
                                         strlen(funcMgtBuiltins[i].name), &i, sizeof(int32_t))) {
295,923,375✔
47
      initFunctionCode = terrno;
×
48
      return;
×
49
    }
50
  }
51
}
52

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

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

70
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
745,412,759✔
71
  if (NULL != gFunMgtService.pFuncNameHashTable) {
745,412,759✔
72
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
745,268,448✔
73
    if (NULL != pVal) {
745,294,442✔
74
      pFunc->funcId = *(int32_t*)pVal;
745,104,124✔
75
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
745,104,929✔
76
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
745,104,441✔
77
    }
78
    return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
190,318✔
79
  }
80
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
3,029,973✔
81
    if (0 == strcmp(funcMgtBuiltins[i].name, pFunc->functionName)) {
3,030,136✔
82
      pFunc->funcId = i;
162,848✔
83
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
162,848✔
84
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
162,848✔
85
    }
86
  }
UNCOV
87
  return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
×
88
}
89

90
EFuncReturnRows fmGetFuncReturnRows(SFunctionNode* pFunc) {
16,386,045✔
91
  if (NULL != funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc) {
16,386,045✔
92
    return funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc(pFunc);
11,479,640✔
93
  }
94
  return (fmIsIndefiniteRowsFunc(pFunc->funcId) || fmIsMultiRowsFunc(pFunc->funcId)) ? FUNC_RETURN_ROWS_INDEFINITE
4,906,889✔
95
                                                                                     : FUNC_RETURN_ROWS_NORMAL;
4,906,889✔
96
}
97

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

102
EFunctionType fmGetFuncType(const char* pFunc) {
545,657,759✔
103
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
545,657,759✔
104
  if (NULL != pVal) {
545,687,699✔
105
    return funcMgtBuiltins[*(int32_t*)pVal].type;
545,492,161✔
106
  }
107
  return FUNCTION_TYPE_UDF;
195,538✔
108
}
109

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

117
EFuncDataRequired fmFuncDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
34,758,273✔
118
  if (fmIsUserDefinedFunc(pFunc->funcId) || pFunc->funcId < 0 || pFunc->funcId >= funcMgtBuiltinsNum) {
34,758,273✔
119
    return FUNC_DATA_REQUIRED_DATA_LOAD;
11✔
120
  }
121
  if (NULL == funcMgtBuiltins[pFunc->funcId].dataRequiredFunc) {
34,766,222✔
122
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
123
  }
124
  return funcMgtBuiltins[pFunc->funcId].dataRequiredFunc(pFunc, pTimeWindow);
34,766,296✔
125
}
126

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

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

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

145
int32_t fmGetFuncExecFuncs(int32_t funcId, SFuncExecFuncs* pFpSet) {
311,811,309✔
146
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
311,811,309✔
147
    return TSDB_CODE_FAILED;
41,634✔
148
  }
149
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
311,786,552✔
150
  pFpSet->init = funcMgtBuiltins[funcId].initFunc;
311,788,984✔
151
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
311,782,550✔
152
  pFpSet->finalize = funcMgtBuiltins[funcId].finalizeFunc;
311,772,067✔
153
  pFpSet->combine = funcMgtBuiltins[funcId].combineFunc;
311,782,024✔
154
  pFpSet->processFuncByRow = funcMgtBuiltins[funcId].processFuncByRow;
311,775,390✔
155
  pFpSet->cleanup = funcMgtBuiltins[funcId].cleanupFunc;
311,796,660✔
156
  return TSDB_CODE_SUCCESS;
311,780,270✔
157
}
158

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

174
int32_t fmGetScalarFuncExecFuncs(int32_t funcId, SScalarFuncExecFuncs* pFpSet) {
1,000,435,888✔
175
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,000,435,888✔
176
    return TSDB_CODE_FAILED;
197,121✔
177
  }
178
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
1,000,477,775✔
179
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
1,000,609,495✔
180
  return TSDB_CODE_SUCCESS;
1,000,667,842✔
181
}
182

183
bool fmIsAggFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_AGG_FUNC); }
2,031,780,154✔
184

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

187
bool fmIsVectorFunc(int32_t funcId) { return !fmIsScalarFunc(funcId) && !fmIsPseudoColumnFunc(funcId); }
1,156,784,668✔
188

189
bool fmIsSelectColsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_COLS_FUNC); }
132,496,814✔
190

191
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
1,799,758,981✔
192

193
bool fmIsTimelineFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TIMELINE_FUNC); }
1,055,779,553✔
194

195
bool fmIsDateTimeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_DATETIME_FUNC); }
534,138,888✔
196

197
bool fmIsPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PSEUDO_COLUMN_FUNC); }
2,039,356,903✔
198

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

201
bool fmIsWindowPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_WINDOW_PC_FUNC); }
1,337,927,844✔
202

203
bool fmIsWindowClauseFunc(int32_t funcId) { return fmIsAggFunc(funcId) || fmIsWindowPseudoColumnFunc(funcId); }
54,513,171✔
204

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

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

209
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
1,419,468,077✔
210

211
bool fmIsSpecialDataRequiredFunc(int32_t funcId) {
80,675,592✔
212
  return isSpecificClassifyFunc(funcId, FUNC_MGT_SPECIAL_DATA_REQUIRED);
80,675,592✔
213
}
214

215
bool fmIsDynamicScanOptimizedFunc(int32_t funcId) {
39,160,993✔
216
  return isSpecificClassifyFunc(funcId, FUNC_MGT_DYNAMIC_SCAN_OPTIMIZED);
39,160,993✔
217
}
218

219
bool fmIsMultiResFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_RES_FUNC); }
1,081,282,684✔
220

221
bool fmIsRepeatScanFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_REPEAT_SCAN_FUNC); }
1,067,539,264✔
222

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

225
bool fmIsForbidFillFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_FILL_FUNC); }
534,364,943✔
226

227
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
58,380,348✔
228

229
bool fmIsSystemInfoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SYSTEM_INFO_FUNC); }
537,139,143✔
230

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

233
bool fmIsClientPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CLIENT_PC_FUNC); }
536,901,478✔
234

235
bool fmIsMultiRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_ROWS_FUNC); }
1,064,542,919✔
236

237
static bool fmIsKeepOrderFuncId(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_KEEP_ORDER_FUNC); }
637,780,981✔
238

239
bool fmIsKeepOrderFunc(SFunctionNode* pFunc) {
643,473,502✔
240
  if (pFunc->funcType == FUNCTION_TYPE_TOP || pFunc->funcType == FUNCTION_TYPE_BOTTOM ||
643,473,502✔
241
      pFunc->funcType == FUNCTION_TYPE_SAMPLE) {
639,939,898✔
242
    if (pFunc->pParameterList != NULL && pFunc->pParameterList->length >= 2) {
5,717,264✔
243
      SNode* pParam = nodesListGetNode(pFunc->pParameterList, 1);
5,727,350✔
244
      if (pParam != NULL && nodeType(pParam) == QUERY_NODE_VALUE) {
5,727,350✔
245
        SValueNode* pConst = (SValueNode*)pParam;
5,727,350✔
246
        if (pConst->datum.i == 1) {
5,727,350✔
247
          return true;
689,009✔
248
        }
249
      }
250
    }
251
    return false;
5,032,070✔
252
  }
253
  return fmIsKeepOrderFuncId(pFunc->funcId);
637,795,945✔
254
}
255

256
bool fmIsCumulativeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CUMULATIVE_FUNC); }
134,669,691✔
257

258
bool fmIsForbidSysTableFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_SYSTABLE_FUNC); }
534,308,771✔
259

260
bool fmIsInterpFunc(int32_t funcId) {
1,095,287,537✔
261
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,095,287,537✔
262
    return false;
346,755✔
263
  }
264
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
1,094,943,133✔
265
}
266

267
bool fmIsInterpPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERP_PC_FUNC); }
1,076,733,981✔
268

269
bool fmIsForecastFunc(int32_t funcId) {
1,050,288,587✔
270
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,050,288,587✔
271
    return false;
355,791✔
272
  }
273
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
1,049,934,156✔
274
}
275

276
bool fmIsAnalysisPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_ANALYTICS_PC_FUNC); }
1,079,447,528✔
277

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

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

288
bool fmIsLastRowFunc(int32_t funcId) {
60,068,878✔
289
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
60,068,878✔
290
    return false;
×
291
  }
292
  return FUNCTION_TYPE_LAST_ROW == funcMgtBuiltins[funcId].type;
60,069,616✔
293
}
294

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

302
bool fmIsNotNullOutputFunc(int32_t funcId) {
458,034,264✔
303
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
458,034,264✔
304
    return false;
74,075✔
305
  }
306
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
457,969,483✔
307
         FUNCTION_TYPE_CACHE_LAST == funcMgtBuiltins[funcId].type ||
441,794,413✔
308
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
441,301,388✔
309
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
423,977,151✔
310
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
418,191,176✔
311
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
407,441,910✔
312
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
398,160,931✔
313
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
395,143,674✔
314
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
344,708,857✔
315
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
1,240,987,974✔
316
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
341,238,840✔
317
}
318

319
bool fmIsSelectValueFunc(int32_t funcId) {
393,696,601✔
320
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
393,696,601✔
321
    return false;
×
322
  }
323
  return FUNCTION_TYPE_SELECT_VALUE == funcMgtBuiltins[funcId].type;
393,697,001✔
324
}
325

326
bool fmIsGroupKeyFunc(int32_t funcId) {
318,811,197✔
327
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
318,811,197✔
328
    return false;
35✔
329
  }
330
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
318,812,362✔
331
}
332

UNCOV
333
bool fmIsHasNullFunc(int32_t funcId) {
×
UNCOV
334
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
335
    return false;
×
336
  }
UNCOV
337
  return FUNCTION_TYPE_HAS_NULL == funcMgtBuiltins[funcId].type;
×
338
}
339

340
bool fmisSelectGroupConstValueFunc(int32_t funcId) {
12,313,292✔
341
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
12,313,292✔
342
    return false;
×
343
  }
344
  return FUNCTION_TYPE_GROUP_CONST_VALUE == funcMgtBuiltins[funcId].type;
12,313,292✔
345
}
346

347
bool fmIsElapsedFunc(int32_t funcId) {
10,100,788✔
348
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
10,100,788✔
UNCOV
349
    return false;
×
350
  }
351
  return FUNCTION_TYPE_ELAPSED == funcMgtBuiltins[funcId].type;
10,100,788✔
352
}
353

354
bool fmIsBlockDistFunc(int32_t funcId) {
534,130,215✔
355
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
534,130,215✔
356
    return false;
181,595✔
357
  }
358
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
533,949,074✔
359
}
360

361
bool fmIsDBUsageFunc(int32_t funcId) {
534,131,713✔
362
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
534,131,713✔
363
    return false;
169,123✔
364
  }
365
  return FUNCTION_TYPE_DB_USAGE == funcMgtBuiltins[funcId].type;
533,963,781✔
366
}
367

368
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
575,888,118✔
369

370
bool fmIsIgnoreNullFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IGNORE_NULL_FUNC); }
278,753✔
371

372
void fmFuncMgtDestroy() {
1,315,259✔
373
  void* m = gFunMgtService.pFuncNameHashTable;
1,315,259✔
374
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
1,315,259✔
375
    taosHashCleanup(m);
1,315,215✔
376
  }
377
}
1,315,259✔
378

379
#ifdef BUILD_NO_CALL
380
int32_t fmSetNormalFunc(int32_t funcId, SFuncExecFuncs* pFpSet) {
381
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
382
    return TSDB_CODE_FAILED;
383
  }
384
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
385
  return TSDB_CODE_SUCCESS;
386
}
387
#endif
388

389
// function has same input/output type
390
bool fmIsSameInOutType(int32_t funcId) {
41,347,886✔
391
  bool res = false;
41,347,886✔
392
  switch (funcMgtBuiltins[funcId].type) {
41,347,886✔
393
    case FUNCTION_TYPE_MAX:
15,465,284✔
394
    case FUNCTION_TYPE_MIN:
395
    case FUNCTION_TYPE_TOP:
396
    case FUNCTION_TYPE_BOTTOM:
397
    case FUNCTION_TYPE_FIRST:
398
    case FUNCTION_TYPE_LAST:
399
    case FUNCTION_TYPE_SAMPLE:
400
    case FUNCTION_TYPE_TAIL:
401
    case FUNCTION_TYPE_UNIQUE:
402
      res = true;
15,465,284✔
403
      break;
15,465,284✔
404
    default:
25,882,563✔
405
      break;
25,882,563✔
406
  }
407
  return res;
41,347,847✔
408
}
409

410
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
13,326,288✔
411
  SNode* pNode;
412
  FOREACH(pNode, pFunc->pParameterList) {
13,406,628✔
413
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
243,883✔
414
      return false;
163,543✔
415
    }
416
  }
417
  return true;
13,162,745✔
418
}
419

420
bool fmIsSkipScanCheckFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SKIP_SCAN_CHECK_FUNC); }
39,154,303✔
421

422
bool fmIsPrimaryKeyFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PRIMARY_KEY_FUNC); }
463,203,131✔
423

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

UNCOV
426
void getLastCacheDataType(SDataType* pType, int32_t pkBytes) {
×
427
  // TODO: do it later.
UNCOV
428
  pType->bytes = getFirstLastInfoSize(pType->bytes, pkBytes) + VARSTR_HEADER_SIZE;
×
UNCOV
429
  pType->type = TSDB_DATA_TYPE_BINARY;
×
UNCOV
430
}
×
431

432
static int32_t getFuncInfo(SFunctionNode* pFunc) {
132,618,171✔
433
  char msg[128] = {0};
132,618,171✔
434
  return fmGetFuncInfo(pFunc, msg, sizeof(msg));
132,618,171✔
435
}
436

437
int32_t createFunction(const char* pName, SNodeList* pParameterList, SFunctionNode** ppFunc) {
8,919,535✔
438
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
8,919,535✔
439
  if (NULL == *ppFunc) {
8,919,535✔
440
    return code;
×
441
  }
442
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
8,919,535✔
443
  (*ppFunc)->pParameterList = pParameterList;
8,919,535✔
444
  code = getFuncInfo((*ppFunc));
8,919,535✔
445
  if (TSDB_CODE_SUCCESS != code) {
8,919,535✔
UNCOV
446
    (*ppFunc)->pParameterList = NULL;
×
UNCOV
447
    nodesDestroyNode((SNode*)*ppFunc);
×
UNCOV
448
    *ppFunc = NULL;
×
UNCOV
449
    return code;
×
450
  }
451
  return code;
8,919,535✔
452
}
453

454
static void resetOutputChangedFunc(SFunctionNode *pFunc, const SFunctionNode* pSrcFunc) {
123,701,172✔
455
  if (funcMgtBuiltins[pFunc->funcId].type == FUNCTION_TYPE_LAST_MERGE) {
123,701,172✔
456
    pFunc->node.resType = pSrcFunc->node.resType;
18,593,791✔
457
    return;
18,593,752✔
458
  }
459
}
460

461
int32_t createFunctionWithSrcFunc(const char* pName, const SFunctionNode* pSrcFunc, SNodeList* pParameterList, SFunctionNode** ppFunc) {
123,699,776✔
462
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
123,699,776✔
463
  if (NULL == *ppFunc) {
123,707,299✔
UNCOV
464
    return code;
×
465
  }
466

467
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
123,707,338✔
468
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
123,707,299✔
469
  (*ppFunc)->pSrcFuncRef = pSrcFunc;
123,707,338✔
470

471
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
123,707,338✔
472
  (*ppFunc)->pParameterList = pParameterList;
123,707,338✔
473
  code = getFuncInfo((*ppFunc));
123,707,377✔
474
  if (TSDB_CODE_SUCCESS != code) {
123,704,194✔
UNCOV
475
    (*ppFunc)->pParameterList = NULL;
×
UNCOV
476
    nodesDestroyNode((SNode*)*ppFunc);
×
UNCOV
477
    *ppFunc = NULL;
×
UNCOV
478
    return code;
×
479
  }
480
  resetOutputChangedFunc(*ppFunc, pSrcFunc);
123,704,194✔
481
  (*ppFunc)->node.relatedTo = pSrcFunc->node.relatedTo;
123,703,079✔
482
  (*ppFunc)->node.bindExprID = pSrcFunc->node.bindExprID;
123,703,196✔
483
  return code;
123,703,118✔
484
}
485

486
static int32_t createColumnByFunc(const SFunctionNode* pFunc, SColumnNode** ppCol) {
82,342,260✔
487
  int32_t code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)ppCol);
82,342,260✔
488
  if (NULL == *ppCol) {
82,349,504✔
UNCOV
489
    return code;
×
490
  }
491
  tstrncpy((*ppCol)->colName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
82,349,504✔
492
  (*ppCol)->node.resType = pFunc->node.resType;
82,349,465✔
493
  return TSDB_CODE_SUCCESS;
82,349,384✔
494
}
495

496
bool fmIsDistExecFunc(int32_t funcId) {
324,333,505✔
497
  if (fmIsUserDefinedFunc(funcId)) {
324,333,505✔
498
    return false;
98,295✔
499
  }
500
  if (!fmIsVectorFunc(funcId)) {
324,265,309✔
501
    return true;
163,291✔
502
  }
503
  return (NULL != funcMgtBuiltins[funcId].pPartialFunc && NULL != funcMgtBuiltins[funcId].pMergeFunc);
324,108,608✔
504
}
505

506
static int32_t createPartialFunction(const SFunctionNode* pSrcFunc, SFunctionNode** pPartialFunc) {
41,349,972✔
507
  SNodeList* pParameterList = NULL;
41,349,972✔
508
  int32_t    code = nodesCloneList(pSrcFunc->pParameterList, &pParameterList);
41,350,011✔
509
  if (NULL == pParameterList) {
41,355,419✔
UNCOV
510
    return code;
×
511
  }
512
  code =
513
      createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pPartialFunc, pSrcFunc, pParameterList, pPartialFunc);
41,355,419✔
514
  if (TSDB_CODE_SUCCESS != code) {
41,354,036✔
515
    nodesDestroyList(pParameterList);
2,918✔
UNCOV
516
    return code;
×
517
  }
518
  (*pPartialFunc)->hasOriginalFunc = true;
41,351,118✔
519
  (*pPartialFunc)->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
41,351,118✔
520
  char name[TSDB_FUNC_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_POINTER_PRINT_BYTES + 1] = {0};
41,351,118✔
521

522
  int32_t len = tsnprintf(name, sizeof(name), "%s.%p", (*pPartialFunc)->functionName, pSrcFunc);
41,351,157✔
523
  if (taosHashBinary(name, len) < 0) {
41,353,998✔
UNCOV
524
    return TSDB_CODE_FAILED;
×
525
  }
526
  tstrncpy((*pPartialFunc)->node.aliasName, name, TSDB_COL_NAME_LEN);
41,353,998✔
527
  return TSDB_CODE_SUCCESS;
41,353,959✔
528
}
529

530
static int32_t createMergeFuncPara(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
82,341,643✔
531
                                   SNodeList** pParameterList) {
532
  SNode*  pRes = NULL;
82,341,643✔
533
  int32_t code = createColumnByFunc(pPartialFunc, (SColumnNode**)&pRes);
82,341,682✔
534
  if (TSDB_CODE_SUCCESS != code) {
82,347,354✔
UNCOV
535
    return code;
×
536
  }
537
  if (NULL != funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc) {
82,347,354✔
538
    return funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc(pSrcFunc->pParameterList, pRes, pParameterList);
672,620✔
539
  } else {
540
    return nodesListMakeStrictAppend(pParameterList, pRes);
81,674,734✔
541
  }
542
}
543

544
static int32_t createMidFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
40,988,931✔
545
                                 SFunctionNode** pMidFunc) {
546
  SNodeList*     pParameterList = NULL;
40,988,931✔
547
  SFunctionNode* pFunc = NULL;
40,988,931✔
548

549
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
40,988,928✔
550
  if (TSDB_CODE_SUCCESS == code) {
40,996,188✔
551
    if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){
40,996,169✔
552
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pSrcFunc, pParameterList, &pFunc);
4,805,886✔
553
    }else{
554
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
36,190,283✔
555
    }
556
  }
557
  if (TSDB_CODE_SUCCESS == code) {
40,995,937✔
558
    tstrncpy(pFunc->node.aliasName, pPartialFunc->node.aliasName, TSDB_COL_NAME_LEN);
40,996,372✔
559
  }
560

561
  if (TSDB_CODE_SUCCESS == code) {
40,995,859✔
562
    *pMidFunc = pFunc;
40,995,859✔
563
  } else {
UNCOV
564
    nodesDestroyList(pParameterList);
×
565
  }
566
  return code;
40,995,859✔
567
}
568

569
static int32_t createMergeFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
41,351,372✔
570
                                   SFunctionNode** pMergeFunc) {
571
  SNodeList*     pParameterList = NULL;
41,351,372✔
572
  SFunctionNode* pFunc = NULL;
41,351,411✔
573

574
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
41,351,408✔
575
  if (TSDB_CODE_SUCCESS == code) {
41,355,253✔
576
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
41,355,317✔
577
  }
578
  if (TSDB_CODE_SUCCESS == code) {
41,358,710✔
579
    pFunc->hasOriginalFunc = true;
41,354,824✔
580
    pFunc->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
41,354,863✔
581
    // overwrite function restype set by translate function
582
    if (fmIsSameInOutType(pSrcFunc->funcId)) {
41,354,785✔
583
      pFunc->node.resType = pSrcFunc->node.resType;
15,465,502✔
584
    }
585
    tstrncpy(pFunc->node.aliasName, pSrcFunc->node.aliasName, TSDB_COL_NAME_LEN);
41,348,107✔
586
  }
587

588
  if (TSDB_CODE_SUCCESS == code) {
41,352,152✔
589
    *pMergeFunc = pFunc;
41,352,152✔
590
  } else {
UNCOV
591
    nodesDestroyList(pParameterList);
×
592
  }
593
  return code;
41,353,452✔
594
}
595

596
int32_t fmGetDistMethod(const SFunctionNode* pFunc, SFunctionNode** pPartialFunc, SFunctionNode** pMidFunc,
41,346,615✔
597
                        SFunctionNode** pMergeFunc) {
598
  if (!fmIsDistExecFunc(pFunc->funcId)) {
41,346,615✔
UNCOV
599
    return TSDB_CODE_FAILED;
×
600
  }
601

602
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
41,355,279✔
603
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
41,350,024✔
604
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
40,992,151✔
605
  }
606
  if (TSDB_CODE_SUCCESS == code) {
41,354,104✔
607
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
41,355,256✔
608
  }
609

610
  if (TSDB_CODE_SUCCESS != code) {
41,352,990✔
UNCOV
611
    nodesDestroyNode((SNode*)*pPartialFunc);
×
UNCOV
612
    if (pMidFunc) nodesDestroyNode((SNode*)*pMidFunc);
×
UNCOV
613
    nodesDestroyNode((SNode*)*pMergeFunc);
×
614
  }
615

616
  return code;
41,354,756✔
617
}
618

619
const char* fmGetFuncName(int32_t funcId) {
315,693✔
620
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
315,693✔
UNCOV
621
    return "invalid function";
×
622
  }
623
  return funcMgtBuiltins[funcId].name;
315,693✔
624
}
625

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

644
bool fmIsTSMASupportedFunc(func_id_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC); }
962,076✔
645

646
bool fmIsRsmaSupportedFunc(func_id_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_RSMA_FUNC); }
126,834✔
647

648
int32_t fmCreateStateFuncs(SNodeList* pFuncs) {
2,220✔
649
  int32_t code;
650
  SNode*  pNode;
651
  char    buf[128] = {0};
2,220✔
652
  FOREACH(pNode, pFuncs) {
7,536✔
653
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
5,316✔
654
    code = fmGetFuncInfo(pFunc, buf, 128);
5,316✔
655
    if (code) break;
5,316✔
656
    if (fmIsTSMASupportedFunc(pFunc->funcId)) {
5,316✔
657
      SFunctionNode* pNewFunc = NULL;
5,316✔
658
      code = fmCreateStateFunc(pFunc, &pNewFunc);
5,316✔
659
      if (code) {
5,316✔
660
        // error
UNCOV
661
        break;
×
662
      } else if (!pNewFunc) {
5,316✔
663
        // no need state func
UNCOV
664
        continue;
×
665
      } else {
666
        REPLACE_NODE(pNewFunc);
5,316✔
667
        nodesDestroyNode(pNode);
5,316✔
668
      }
669
    }
670
  }
671
  return code;
2,220✔
672
}
673

674
static int32_t fmCreateStateMergeFunc(SFunctionNode* pFunc, SFunctionNode** pStateMergeFunc) {
1,254✔
675
  if (funcMgtBuiltins[pFunc->funcId].pMergeFunc) {
1,254✔
676
    SNodeList* pParams = NULL;
1,026✔
677
    int32_t    code = nodesCloneList(pFunc->pParameterList, &pParams);
1,026✔
678
    if (!pParams) return code;
1,026✔
679
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pFunc->funcId].pMergeFunc, pFunc, pParams, pStateMergeFunc);
1,026✔
680
    if (TSDB_CODE_SUCCESS != code) {
1,026✔
UNCOV
681
      nodesDestroyList(pParams);
×
UNCOV
682
      return code;
×
683
    }
684
    tstrncpy((*pStateMergeFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
1,026✔
685
    tstrncpy((*pStateMergeFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
1,026✔
686
  }
687
  return TSDB_CODE_SUCCESS;
1,254✔
688
}
689

690
int32_t fmCreateStateMergeFuncs(SNodeList* pFuncs) {
570✔
691
  int32_t code;
692
  SNode*  pNode;
693
  char    buf[128] = {0};
570✔
694
  FOREACH(pNode, pFuncs) {
1,824✔
695
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
1,254✔
696
    if (fmIsTSMASupportedFunc(pFunc->funcId)) {
1,254✔
697
      SFunctionNode* pNewFunc = NULL;
1,254✔
698
      code = fmCreateStateMergeFunc(pFunc, &pNewFunc);
1,254✔
699
      if (code) {
1,254✔
700
        // error
UNCOV
701
        break;
×
702
      } else if (!pNewFunc) {
1,254✔
703
        // no state merge func
704
        continue;
228✔
705
      } else {
706
        REPLACE_NODE(pNewFunc);
1,026✔
707
        nodesDestroyNode(pNode);
1,026✔
708
      }
709
    }
710
  }
711
  return code;
570✔
712
}
713

714
int32_t fmGetFuncId(const char* name) {
1,627,818✔
715
  if (NULL != gFunMgtService.pFuncNameHashTable) {
1,627,818✔
716
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, name, strlen(name));
1,627,818✔
717
    if (NULL != pVal) {
1,627,818✔
718
      return *(int32_t*)pVal;
1,627,818✔
719
    }
720
    return -1;
×
721
  }
UNCOV
722
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
×
UNCOV
723
    if (0 == strcmp(funcMgtBuiltins[i].name, name)) {
×
UNCOV
724
      return i;
×
725
    }
726
  }
727
  return -1;
×
728
}
729

730
bool fmIsMyStateFunc(int32_t funcId, int32_t stateFuncId) {
1,554,504✔
731
  const SBuiltinFuncDefinition* pFunc = &funcMgtBuiltins[funcId];
1,554,504✔
732
  const SBuiltinFuncDefinition* pStateFunc = &funcMgtBuiltins[stateFuncId];
1,554,504✔
733
  if (!pFunc->pStateFunc) {
1,554,504✔
UNCOV
734
    return false;
×
735
  }
736
  if (strcmp(pFunc->pStateFunc, pStateFunc->name) == 0) return true;
1,554,504✔
737
  int32_t stateMergeFuncId = fmGetFuncId(pFunc->pStateFunc);
570,342✔
738
  if (stateMergeFuncId == -1) {
570,342✔
UNCOV
739
    return false;
×
740
  }
741
  const SBuiltinFuncDefinition* pStateMergeFunc = &funcMgtBuiltins[stateMergeFuncId];
570,342✔
742
  return strcmp(pStateFunc->name, pStateMergeFunc->pMergeFunc) == 0;
570,342✔
743
}
744

745
bool fmIsCountLikeFunc(int32_t funcId) {
504,573,351✔
746
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
504,573,351✔
747
}
748

749
bool fmIsRowTsOriginFunc(int32_t funcId) {
4,948,635✔
750
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
4,948,635✔
751
    return false;
×
752
  }
753
  return FUNCTION_TYPE_IROWTS_ORIGIN == funcMgtBuiltins[funcId].type;
4,948,635✔
754
}
755

UNCOV
756
bool fmIsGroupIdFunc(int32_t funcId) {
×
UNCOV
757
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
UNCOV
758
    return false;
×
759
  }
760
  return FUNCTION_TYPE_GROUP_ID == funcMgtBuiltins[funcId].type;
×
761
}
762

763
const void* fmGetStreamPesudoFuncVal(int32_t funcId, const SStreamRuntimeFuncInfo* pStreamRuntimeFuncInfo) {
40,180,439✔
764
  SSTriggerCalcParam *pParams = taosArrayGet(pStreamRuntimeFuncInfo->pStreamPesudoFuncVals, pStreamRuntimeFuncInfo->curIdx);
40,180,439✔
765
  switch (funcMgtBuiltins[funcId].type) {
40,182,235✔
UNCOV
766
    case FUNCTION_TYPE_TPREV_TS:
×
UNCOV
767
      return &pParams->prevTs;
×
768
    case FUNCTION_TYPE_TCURRENT_TS:
1,807,684✔
769
      return &pParams->currentTs;
1,807,684✔
UNCOV
770
    case FUNCTION_TYPE_TNEXT_TS:
×
UNCOV
771
      return &pParams->nextTs;
×
772
    case FUNCTION_TYPE_TWSTART:
19,497,909✔
773
      return &pParams->wstart;
19,497,909✔
774
    case FUNCTION_TYPE_TWEND:
14,639,864✔
775
      return &pParams->wend;
14,639,864✔
776
    case FUNCTION_TYPE_TWDURATION:
829,160✔
777
      return &pParams->wduration;
829,160✔
778
    case FUNCTION_TYPE_TWROWNUM:
98,585✔
779
      return &pParams->wrownum;
98,585✔
780
    case FUNCTION_TYPE_TPREV_LOCALTIME:
123,360✔
781
      return &pParams->prevLocalTime;
123,360✔
782
    case FUNCTION_TYPE_TLOCALTIME:
355,501✔
783
      return &pParams->triggerTime;
355,501✔
784
    case FUNCTION_TYPE_TNEXT_LOCALTIME:
192,544✔
785
      return &pParams->nextLocalTime;
192,544✔
786
    case FUNCTION_TYPE_TGRPID:
119,244✔
787
      return &pStreamRuntimeFuncInfo->groupId;
119,244✔
788
    case FUNCTION_TYPE_PLACEHOLDER_COLUMN:
712,577✔
789
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
712,577✔
790
    case FUNCTION_TYPE_PLACEHOLDER_TBNAME:
1,805,656✔
791
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
1,805,656✔
UNCOV
792
    default:
×
UNCOV
793
      break;
×
794
  }
UNCOV
795
  return NULL;
×
796
}
797

798
bool fmIsStreamPesudoColVal(int32_t funcId) {
437,806✔
799
  return funcMgtBuiltins[funcId].type == FUNCTION_TYPE_PLACEHOLDER_COLUMN
437,806✔
800
         || funcMgtBuiltins[funcId].type == FUNCTION_TYPE_PLACEHOLDER_TBNAME;
437,806✔
801
}
802

803
int32_t fmGetStreamPesudoFuncEnv(int32_t funcId, SNodeList* pParamNodes, SFuncExecEnv *pEnv) {
6,124,988✔
804
  if (NULL == pParamNodes || pParamNodes->length < 1) {
6,124,988✔
805
    uError("invalid stream pesudo func param list %p", pParamNodes);
×
UNCOV
806
    return TSDB_CODE_INTERNAL_ERROR;
×
807
  }
808

809
  int32_t firstParamType = nodeType(nodesListGetNode(pParamNodes, 0));
6,125,464✔
810
  if (QUERY_NODE_VALUE != firstParamType) {
6,125,321✔
UNCOV
811
    uError("invalid stream pesudo func first param type %d", firstParamType);
×
UNCOV
812
    return TSDB_CODE_INTERNAL_ERROR;
×
813
  }
814

815
  SValueNode* pResDesc = (SValueNode*)nodesListGetNode(pParamNodes, 0);
6,125,321✔
816
  pEnv->calcMemSize = pResDesc->node.resType.bytes;
6,124,598✔
817

818
  return TSDB_CODE_SUCCESS;
6,124,598✔
819
}
820

821

822
int32_t fmSetStreamPseudoFuncParamVal(int32_t funcId, SNodeList* pParamNodes, const SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
40,170,938✔
823
  if (!pStreamRuntimeInfo) {
40,170,938✔
824
    uError("internal error, should have pVals for stream pseudo funcs");
×
825
    return TSDB_CODE_INTERNAL_ERROR;
×
826
  }
827
  int32_t code = 0;
40,170,938✔
828
  SArray *pVals1 = NULL;
40,170,938✔
829
  SNode* pFirstParam = nodesListGetNode(pParamNodes, 0);
40,170,938✔
830
  if (nodeType(pFirstParam) != QUERY_NODE_VALUE) {
40,167,365✔
UNCOV
831
    uError("invalid param node type: %d for func: %d", nodeType(pFirstParam), funcId);
×
UNCOV
832
    return TSDB_CODE_INTERNAL_ERROR;
×
833
  }
834

835
  int32_t t = funcMgtBuiltins[funcId].type;
40,173,137✔
836
  uDebug("set stream pseudo func param val, functype: %d, pStreamRuntimeInfo: %p", t, pStreamRuntimeInfo);
40,172,416✔
837
  if (FUNCTION_TYPE_TGRPID == t) {
40,181,760✔
838
    SValue v = {0};
119,244✔
839
    v.type = TSDB_DATA_TYPE_BIGINT;
119,244✔
840
    v.val = *(int64_t*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
119,244✔
841
    
842
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&v, pFirstParam->type));
119,244✔
843
    if (code != 0) {
119,244✔
844
      uError("failed to set value node value: %s", tstrerror(code));
×
UNCOV
845
      return code;
×
846
    }
847
  } else if (FUNCTION_TYPE_PLACEHOLDER_COLUMN == t) {
40,062,516✔
848
    SNode* pSecondParam = nodesListGetNode(pParamNodes, 1);
712,796✔
849
    if (nodeType(pSecondParam) != QUERY_NODE_VALUE) {
712,796✔
850
      uError("invalid param node type: %d for func: %d", nodeType(pSecondParam), funcId);
×
UNCOV
851
      return TSDB_CODE_INTERNAL_ERROR;
×
852
    }
853
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
712,577✔
854
    int32_t idx = ((SValueNode*)pSecondParam)->datum.i;
712,577✔
855
    if (idx - 1 < 0 || idx - 1 >= taosArrayGetSize(pVal)) {
712,577✔
856
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
219✔
UNCOV
857
      return TSDB_CODE_INTERNAL_ERROR;
×
858
    }
859
    SStreamGroupValue* pValue = taosArrayGet(pVal, idx - 1);
712,577✔
860
    if (pValue == NULL) {
712,577✔
UNCOV
861
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
×
UNCOV
862
      return TSDB_CODE_INTERNAL_ERROR;
×
863
    }
864
    if (!pValue->isNull){
712,577✔
865
      if (pValue->data.type != ((SValueNode*)pFirstParam)->node.resType.type){
705,101✔
UNCOV
866
        uError("invalid value type: %d for func: %d, should be: %d", pValue->data.type, funcId, ((SValueNode*)pFirstParam)->node.resType.type);
×
UNCOV
867
        return TSDB_CODE_INTERNAL_ERROR;
×
868
      }
869
      if (IS_VAR_DATA_TYPE(((SValueNode*)pFirstParam)->node.resType.type)) {
704,882✔
870
        char* tmp = taosMemoryCalloc(1, pValue->data.nData + VARSTR_HEADER_SIZE); 
501,975✔
871
        if (tmp == NULL) {
501,975✔
UNCOV
872
          return TSDB_CODE_OUT_OF_MEMORY;
×
873
        }
874
        taosMemoryFree(((SValueNode*)pFirstParam)->datum.p);
501,975✔
875
        ((SValueNode*)pFirstParam)->datum.p = tmp;
501,975✔
876
        memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
501,975✔
877
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
501,756✔
878
      } else {
879
        code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&pValue->data, pValue->data.type));
203,126✔
880
      }
881
    }
882
    ((SValueNode*)pFirstParam)->isNull = pValue->isNull;
712,358✔
883
  } else if (FUNCTION_TYPE_PLACEHOLDER_TBNAME == t) {
39,349,720✔
884
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
1,805,917✔
885
    for (int32_t i = 0; i < taosArrayGetSize(pVal); ++i) {
2,134,739✔
886
      SStreamGroupValue* pValue = taosArrayGet(pVal, i);
2,134,739✔
887
      if (pValue != NULL && pValue->isTbname) {
2,134,739✔
888
        taosMemoryFreeClear(((SValueNode*)pFirstParam)->datum.p);
1,806,677✔
889
        ((SValueNode*)pFirstParam)->datum.p = taosMemoryCalloc(pValue->data.nData + VARSTR_HEADER_SIZE, 1);
1,806,423✔
890
        if (NULL == ((SValueNode*)pFirstParam)->datum.p ) {
1,806,419✔
UNCOV
891
          return terrno;
×
892
        }
893

894
        (void)memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
1,805,412✔
895
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
1,806,433✔
896
        break;
1,806,186✔
897
      }
898
    }
899
  } else if (LIST_LENGTH(pParamNodes) == 1) {
75,084,824✔
900
    // twstart, twend
901
    const void* pVal = fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
37,543,147✔
902
    if (!pVal) {
37,543,415✔
UNCOV
903
      uError("failed to set stream pseudo func param val, NULL val for funcId: %d", funcId);
×
UNCOV
904
      return TSDB_CODE_INTERNAL_ERROR;
×
905
    }
906
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, (void*)pVal);
37,543,415✔
907
    if (code != 0) {
37,542,339✔
908
      uError("failed to set value node value: %s", tstrerror(code));
1,318✔
UNCOV
909
      return code;
×
910
    }
911
  } else {
912
    uError("invalid placeholder function type: %d", t);
967✔
UNCOV
913
    return TSDB_CODE_INTERNAL_ERROR;
×
914
  }
915
  
916
  return code;
40,179,017✔
917
}
918

919

920

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