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

taosdata / TDengine / #5003

26 Mar 2026 12:51PM UTC coverage: 72.317% (-0.4%) from 72.671%
#5003

push

travis-ci

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

513 of 851 new or added lines in 47 files covered. (60.28%)

561 existing lines in 135 files now uncovered.

253860 of 351039 relevant lines covered (72.32%)

132153090.56 hits per line

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

82.41
/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,804,702✔
37
  gFunMgtService.pFuncNameHashTable =
1,804,702✔
38
      taosHashInit(funcMgtBuiltinsNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
1,804,702✔
39
  if (NULL == gFunMgtService.pFuncNameHashTable) {
1,804,702✔
40
    initFunctionCode = terrno;
×
41
    return;
×
42
  }
43

44
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
411,472,056✔
45
    if (TSDB_CODE_SUCCESS != taosHashPut(gFunMgtService.pFuncNameHashTable, funcMgtBuiltins[i].name,
409,667,354✔
46
                                         strlen(funcMgtBuiltins[i].name), &i, sizeof(int32_t))) {
409,667,354✔
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);
8,455,152✔
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,804,702✔
66
  (void)taosThreadOnce(&functionHashTableInit, doInitFunctionTable);
1,804,702✔
67
  return initFunctionCode;
1,804,702✔
68
}
69

70
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
821,447,558✔
71
  if (NULL != gFunMgtService.pFuncNameHashTable) {
821,447,558✔
72
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
821,280,844✔
73
    if (NULL != pVal) {
821,307,259✔
74
      pFunc->funcId = *(int32_t*)pVal;
821,105,308✔
75
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
821,105,183✔
76
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
821,104,506✔
77
    }
78
    return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
201,951✔
79
  }
80
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
3,184,406✔
81
    if (0 == strcmp(funcMgtBuiltins[i].name, pFunc->functionName)) {
3,184,352✔
82
      pFunc->funcId = i;
171,136✔
83
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
171,136✔
84
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
171,136✔
85
    }
86
  }
87
  return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
60✔
88
}
89

90
EFuncReturnRows fmGetFuncReturnRows(SFunctionNode* pFunc) {
17,336,724✔
91
  if (NULL != funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc) {
17,336,724✔
92
    return funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc(pFunc);
12,188,154✔
93
  }
94
  return (fmIsIndefiniteRowsFunc(pFunc->funcId) || fmIsMultiRowsFunc(pFunc->funcId)) ? FUNC_RETURN_ROWS_INDEFINITE
5,148,570✔
95
                                                                                     : FUNC_RETURN_ROWS_NORMAL;
5,148,570✔
96
}
97

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

102
EFunctionType fmGetFuncType(const char* pFunc) {
600,906,308✔
103
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
600,906,308✔
104
  if (NULL != pVal) {
600,930,293✔
105
    return funcMgtBuiltins[*(int32_t*)pVal].type;
600,710,162✔
106
  }
107
  return FUNCTION_TYPE_UDF;
220,131✔
108
}
109

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

117
EFuncDataRequired fmFuncDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
40,378,852✔
118
  if (fmIsUserDefinedFunc(pFunc->funcId) || pFunc->funcId < 0 || pFunc->funcId >= funcMgtBuiltinsNum) {
40,378,852✔
UNCOV
119
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
120
  }
121
  if (NULL == funcMgtBuiltins[pFunc->funcId].dataRequiredFunc) {
40,385,664✔
122
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
123
  }
124
  return funcMgtBuiltins[pFunc->funcId].dataRequiredFunc(pFunc, pTimeWindow);
40,385,656✔
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;
53✔
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;
2,115,302✔
135
    ;
136
  }
137

138
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
2,147,483,647✔
139
    return FUNC_DATA_REQUIRED_DATA_LOAD;
185,316,408✔
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) {
305,851,638✔
146
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
305,851,638✔
147
    return TSDB_CODE_FAILED;
76✔
148
  }
149
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
305,865,798✔
150
  pFpSet->init = funcMgtBuiltins[funcId].initFunc;
305,762,919✔
151
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
305,804,778✔
152
  pFpSet->finalize = funcMgtBuiltins[funcId].finalizeFunc;
305,789,098✔
153
  pFpSet->combine = funcMgtBuiltins[funcId].combineFunc;
305,800,486✔
154
  pFpSet->processFuncByRow = funcMgtBuiltins[funcId].processFuncByRow;
305,789,897✔
155
  pFpSet->cleanup = funcMgtBuiltins[funcId].cleanupFunc;
305,783,508✔
156
  return TSDB_CODE_SUCCESS;
305,831,114✔
157
}
158

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

174
int32_t fmGetScalarFuncExecFuncs(int32_t funcId, SScalarFuncExecFuncs* pFpSet) {
1,038,298,681✔
175
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,038,298,681✔
176
    return TSDB_CODE_FAILED;
39,071✔
177
  }
178
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
1,038,420,101✔
179
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
1,038,370,800✔
180
  return TSDB_CODE_SUCCESS;
1,038,351,503✔
181
}
182

183
bool fmIsAggFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_AGG_FUNC); }
2,147,483,647✔
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,279,155,102✔
188

189
bool fmIsSelectColsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_COLS_FUNC); }
150,918,302✔
190

191
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
1,877,185,540✔
192

193
bool fmIsTimelineFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TIMELINE_FUNC); }
1,153,792,286✔
194

195
bool fmIsDateTimeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_DATETIME_FUNC); }
583,509,083✔
196

197
bool fmIsPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PSEUDO_COLUMN_FUNC); }
2,147,483,647✔
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,428,492,930✔
202

203
bool fmIsWindowClauseFunc(int32_t funcId) { return fmIsAggFunc(funcId) || fmIsWindowPseudoColumnFunc(funcId); }
59,555,512✔
204

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

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

209
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
1,538,430,078✔
210

211
bool fmIsSpecialDataRequiredFunc(int32_t funcId) {
90,934,737✔
212
  return isSpecificClassifyFunc(funcId, FUNC_MGT_SPECIAL_DATA_REQUIRED);
90,934,737✔
213
}
214

215
bool fmIsDynamicScanOptimizedFunc(int32_t funcId) {
43,417,734✔
216
  return isSpecificClassifyFunc(funcId, FUNC_MGT_DYNAMIC_SCAN_OPTIMIZED);
43,417,734✔
217
}
218

219
bool fmIsMultiResFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_RES_FUNC); }
1,188,582,697✔
220

221
bool fmIsRepeatScanFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_REPEAT_SCAN_FUNC); }
1,165,943,930✔
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); }
583,761,291✔
226

227
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
61,476,578✔
228

229
bool fmIsSystemInfoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SYSTEM_INFO_FUNC); }
586,700,469✔
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); }
586,438,461✔
234

235
bool fmIsMultiRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_ROWS_FUNC); }
1,163,090,358✔
236

237
static bool fmIsKeepOrderFuncId(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_KEEP_ORDER_FUNC); }
704,452,472✔
238

239
bool fmIsKeepOrderFunc(SFunctionNode* pFunc) {
710,686,922✔
240
  if (pFunc->funcType == FUNCTION_TYPE_TOP || pFunc->funcType == FUNCTION_TYPE_BOTTOM ||
710,686,922✔
241
      pFunc->funcType == FUNCTION_TYPE_SAMPLE) {
706,748,419✔
242
    if (pFunc->pParameterList != NULL && pFunc->pParameterList->length >= 2) {
6,239,720✔
243
      SNode* pParam = nodesListGetNode(pFunc->pParameterList, 1);
6,254,593✔
244
      if (pParam != NULL && nodeType(pParam) == QUERY_NODE_VALUE) {
6,254,593✔
245
        SValueNode* pConst = (SValueNode*)pParam;
6,254,593✔
246
        if (pConst->datum.i == 1) {
6,254,593✔
247
          return true;
737,284✔
248
        }
249
      }
250
    }
251
    return false;
5,507,848✔
252
  }
253
  return fmIsKeepOrderFuncId(pFunc->funcId);
704,456,623✔
254
}
255

256
bool fmIsCumulativeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CUMULATIVE_FUNC); }
153,239,677✔
257

258
bool fmIsForbidSysTableFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_SYSTABLE_FUNC); }
583,694,521✔
259

260
bool fmIsInterpFunc(int32_t funcId) {
1,195,722,267✔
261
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,195,722,267✔
262
    return false;
369,964✔
263
  }
264
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
1,195,357,797✔
265
}
266

267
bool fmIsInterpPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERP_PC_FUNC); }
1,177,069,689✔
268

269
bool fmIsForecastFunc(int32_t funcId) {
1,147,998,972✔
270
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,147,998,972✔
271
    return false;
377,996✔
272
  }
273
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
1,147,624,031✔
274
}
275

276
bool fmIsAnalysisPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_ANALYTICS_PC_FUNC); }
1,179,948,444✔
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) {
69,526,186✔
289
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
69,526,186✔
290
    return false;
×
291
  }
292
  return FUNCTION_TYPE_LAST_ROW == funcMgtBuiltins[funcId].type;
69,526,711✔
293
}
294

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

302
bool fmIsNotNullOutputFunc(int32_t funcId) {
459,949,494✔
303
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
459,949,494✔
304
    return false;
75,373✔
305
  }
306
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
459,883,181✔
307
         FUNCTION_TYPE_CACHE_LAST == funcMgtBuiltins[funcId].type ||
442,391,573✔
308
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
441,212,712✔
309
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
432,446,472✔
310
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
428,880,119✔
311
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
417,191,125✔
312
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
412,159,493✔
313
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
410,173,508✔
314
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
358,802,584✔
315
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
1,257,395,486✔
316
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
355,046,058✔
317
}
318

319
bool fmIsSelectValueFunc(int32_t funcId) {
362,197,379✔
320
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
362,197,379✔
321
    return false;
×
322
  }
323
  return FUNCTION_TYPE_SELECT_VALUE == funcMgtBuiltins[funcId].type;
362,198,921✔
324
}
325

326
bool fmIsGroupKeyFunc(int32_t funcId) {
283,481,747✔
327
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
283,481,747✔
UNCOV
328
    return false;
×
329
  }
330
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
283,482,179✔
331
}
332

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

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

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

354
bool fmIsBlockDistFunc(int32_t funcId) {
583,504,428✔
355
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
583,504,428✔
356
    return false;
175,196✔
357
  }
358
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
583,334,642✔
359
}
360

361
bool fmIsDBUsageFunc(int32_t funcId) {
583,506,185✔
362
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
583,506,185✔
363
    return false;
185,266✔
364
  }
365
  return FUNCTION_TYPE_DB_USAGE == funcMgtBuiltins[funcId].type;
583,323,404✔
366
}
367

368
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
627,429,031✔
369

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

372
void fmFuncMgtDestroy() {
1,804,752✔
373
  void* m = gFunMgtService.pFuncNameHashTable;
1,804,752✔
374
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
1,804,752✔
375
    taosHashCleanup(m);
1,804,702✔
376
  }
377
}
1,804,752✔
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) {
47,524,395✔
391
  bool res = false;
47,524,395✔
392
  switch (funcMgtBuiltins[funcId].type) {
47,524,395✔
393
    case FUNCTION_TYPE_MAX:
18,860,031✔
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;
18,860,031✔
403
      break;
18,860,031✔
404
    default:
28,664,364✔
405
      break;
28,664,364✔
406
  }
407
  return res;
47,524,395✔
408
}
409

410
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
14,593,124✔
411
  SNode* pNode;
412
  FOREACH(pNode, pFunc->pParameterList) {
14,677,052✔
413
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
140,781✔
414
      return false;
56,853✔
415
    }
416
  }
417
  return true;
14,536,271✔
418
}
419

420
bool fmIsSkipScanCheckFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SKIP_SCAN_CHECK_FUNC); }
43,412,241✔
421

422
bool fmIsPrimaryKeyFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PRIMARY_KEY_FUNC); }
508,249,191✔
423

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

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

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

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

454
static void resetOutputChangedFunc(SFunctionNode *pFunc, const SFunctionNode* pSrcFunc) {
142,164,347✔
455
  if (funcMgtBuiltins[pFunc->funcId].type == FUNCTION_TYPE_LAST_MERGE) {
142,164,347✔
456
    pFunc->node.resType = pSrcFunc->node.resType;
22,499,253✔
457
    return;
22,499,253✔
458
  }
459
}
460

461
int32_t createFunctionWithSrcFunc(const char* pName, const SFunctionNode* pSrcFunc, SNodeList* pParameterList, SFunctionNode** ppFunc) {
142,162,087✔
462
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
142,162,087✔
463
  if (NULL == *ppFunc) {
142,167,903✔
464
    return code;
×
465
  }
466

467
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
142,167,903✔
468
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
142,167,849✔
469
  (*ppFunc)->pSrcFuncRef = pSrcFunc;
142,167,849✔
470

471
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
142,167,802✔
472
  (*ppFunc)->pParameterList = pParameterList;
142,167,802✔
473
  code = getFuncInfo((*ppFunc));
142,167,903✔
474
  if (TSDB_CODE_SUCCESS != code) {
142,166,309✔
475
    (*ppFunc)->pParameterList = NULL;
×
476
    nodesDestroyNode((SNode*)*ppFunc);
×
477
    *ppFunc = NULL;
×
478
    return code;
×
479
  }
480
  resetOutputChangedFunc(*ppFunc, pSrcFunc);
142,166,309✔
481
  (*ppFunc)->node.relatedTo = pSrcFunc->node.relatedTo;
142,165,453✔
482
  (*ppFunc)->node.bindExprID = pSrcFunc->node.bindExprID;
142,165,453✔
483
  return code;
142,165,453✔
484
}
485

486
static int32_t createColumnByFunc(const SFunctionNode* pFunc, SColumnNode** ppCol) {
94,629,105✔
487
  int32_t code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)ppCol);
94,629,105✔
488
  if (NULL == *ppCol) {
94,636,974✔
489
    return code;
×
490
  }
491
  tstrncpy((*ppCol)->colName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
94,636,974✔
492
  (*ppCol)->node.resType = pFunc->node.resType;
94,636,974✔
493
  return TSDB_CODE_SUCCESS;
94,636,974✔
494
}
495

496
bool fmIsDistExecFunc(int32_t funcId) {
366,138,749✔
497
  if (fmIsUserDefinedFunc(funcId)) {
366,138,749✔
498
    return false;
103,437✔
499
  }
500
  if (!fmIsVectorFunc(funcId)) {
366,061,954✔
501
    return true;
181,077✔
502
  }
503
  return (NULL != funcMgtBuiltins[funcId].pPartialFunc && NULL != funcMgtBuiltins[funcId].pMergeFunc);
365,890,049✔
504
}
505

506
static int32_t createPartialFunction(const SFunctionNode* pSrcFunc, SFunctionNode** pPartialFunc) {
47,525,545✔
507
  SNodeList* pParameterList = NULL;
47,525,545✔
508
  int32_t    code = nodesCloneList(pSrcFunc->pParameterList, &pParameterList);
47,525,545✔
509
  if (NULL == pParameterList) {
47,529,182✔
510
    return code;
×
511
  }
512
  code =
513
      createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pPartialFunc, pSrcFunc, pParameterList, pPartialFunc);
47,529,182✔
514
  if (TSDB_CODE_SUCCESS != code) {
47,527,143✔
515
    nodesDestroyList(pParameterList);
×
516
    return code;
×
517
  }
518
  (*pPartialFunc)->hasOriginalFunc = true;
47,527,344✔
519
  (*pPartialFunc)->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
47,527,344✔
520
  char name[TSDB_FUNC_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_POINTER_PRINT_BYTES + 1] = {0};
47,527,344✔
521

522
  int32_t len = snprintf(name, sizeof(name), "%s.%p", (*pPartialFunc)->functionName, pSrcFunc);
47,527,344✔
523
  if (taosHashBinary(name, len, sizeof(name)) < 0) {
47,528,754✔
524
    return TSDB_CODE_FAILED;
×
525
  }
526
  tstrncpy((*pPartialFunc)->node.aliasName, name, TSDB_COL_NAME_LEN);
47,528,754✔
527
  return TSDB_CODE_SUCCESS;
47,528,754✔
528
}
529

530
static int32_t createMergeFuncPara(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
94,629,079✔
531
                                   SNodeList** pParameterList) {
532
  SNode*  pRes = NULL;
94,629,079✔
533
  int32_t code = createColumnByFunc(pPartialFunc, (SColumnNode**)&pRes);
94,629,079✔
534
  if (TSDB_CODE_SUCCESS != code) {
94,636,229✔
535
    return code;
×
536
  }
537
  if (NULL != funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc) {
94,636,229✔
538
    return funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc(pSrcFunc->pParameterList, pRes, pParameterList);
814,854✔
539
  } else {
540
    return nodesListMakeStrictAppend(pParameterList, pRes);
93,821,375✔
541
  }
542
}
543

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

549
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
47,103,987✔
550
  if (TSDB_CODE_SUCCESS == code) {
47,108,975✔
551
    if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){
47,108,949✔
552
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pSrcFunc, pParameterList, &pFunc);
5,310,856✔
553
    }else{
554
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
41,798,093✔
555
    }
556
  }
557
  if (TSDB_CODE_SUCCESS == code) {
47,107,725✔
558
    tstrncpy(pFunc->node.aliasName, pPartialFunc->node.aliasName, TSDB_COL_NAME_LEN);
47,107,885✔
559
  }
560

561
  if (TSDB_CODE_SUCCESS == code) {
47,107,725✔
562
    *pMidFunc = pFunc;
47,107,725✔
563
  } else {
564
    nodesDestroyList(pParameterList);
×
565
  }
566
  return code;
47,108,089✔
567
}
568

569
static int32_t createMergeFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
47,525,609✔
570
                                   SFunctionNode** pMergeFunc) {
571
  SNodeList*     pParameterList = NULL;
47,525,609✔
572
  SFunctionNode* pFunc = NULL;
47,525,609✔
573

574
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
47,525,185✔
575
  if (TSDB_CODE_SUCCESS == code) {
47,529,409✔
576
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
47,529,409✔
577
  }
578
  if (TSDB_CODE_SUCCESS == code) {
47,532,245✔
579
    pFunc->hasOriginalFunc = true;
47,529,770✔
580
    pFunc->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
47,529,770✔
581
    // overwrite function restype set by translate function
582
    if (fmIsSameInOutType(pSrcFunc->funcId)) {
47,529,770✔
583
      pFunc->node.resType = pSrcFunc->node.resType;
18,860,258✔
584
    }
585
    tstrncpy(pFunc->node.aliasName, pSrcFunc->node.aliasName, TSDB_COL_NAME_LEN);
47,525,696✔
586
  }
587

588
  if (TSDB_CODE_SUCCESS == code) {
47,528,218✔
589
    *pMergeFunc = pFunc;
47,528,218✔
590
  } else {
591
    nodesDestroyList(pParameterList);
×
592
  }
593
  return code;
47,528,293✔
594
}
595

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

602
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
47,529,187✔
603
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
47,526,713✔
604
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
47,108,058✔
605
  }
606
  if (TSDB_CODE_SUCCESS == code) {
47,526,718✔
607
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
47,528,538✔
608
  }
609

610
  if (TSDB_CODE_SUCCESS != code) {
47,523,905✔
611
    nodesDestroyNode((SNode*)*pPartialFunc);
×
612
    if (pMidFunc) nodesDestroyNode((SNode*)*pMidFunc);
×
613
    nodesDestroyNode((SNode*)*pMergeFunc);
×
614
  }
615

616
  return code;
47,526,518✔
617
}
618

619
const char* fmGetFuncName(int32_t funcId) {
335,294✔
620
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
335,294✔
621
    return "invalid function";
×
622
  }
623
  return funcMgtBuiltins[funcId].name;
335,294✔
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) {
6,598✔
629
  if (funcMgtBuiltins[pFunc->funcId].pStateFunc) {
6,598✔
630
    SNodeList* pParams = NULL;
6,598✔
631
    int32_t    code = nodesCloneList(pFunc->pParameterList, &pParams);
6,598✔
632
    if (!pParams) return code;
6,598✔
633
    code = createFunction(funcMgtBuiltins[pFunc->funcId].pStateFunc, pParams, pStateFunc);
6,598✔
634
    if (TSDB_CODE_SUCCESS != code) {
6,598✔
635
      nodesDestroyList(pParams);
×
636
      return code;
×
637
    }
638
    tstrncpy((*pStateFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
6,598✔
639
    tstrncpy((*pStateFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
6,598✔
640
  }
641
  return TSDB_CODE_SUCCESS;
6,598✔
642
}
643

644
bool fmIsTSMASupportedFunc(func_id_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC); }
1,116,508✔
645

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

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

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

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

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

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

745
bool fmIsCountLikeFunc(int32_t funcId) {
575,734,185✔
746
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
575,734,185✔
747
}
748

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

756
bool fmIsGroupIdFunc(int32_t funcId) {
×
757
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
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) {
300,481,427✔
764
  SSTriggerCalcParam *pParams = taosArrayGet(pStreamRuntimeFuncInfo->pStreamPesudoFuncVals, pStreamRuntimeFuncInfo->curIdx);
300,481,427✔
765
  switch (funcMgtBuiltins[funcId].type) {
300,499,956✔
766
    case FUNCTION_TYPE_TPREV_TS:
×
767
      return &pParams->prevTs;
×
768
    case FUNCTION_TYPE_TCURRENT_TS:
2,229,246✔
769
      return &pParams->currentTs;
2,229,246✔
770
    case FUNCTION_TYPE_TNEXT_TS:
×
771
      return &pParams->nextTs;
×
772
    case FUNCTION_TYPE_TWSTART:
277,043,228✔
773
      return &pParams->wstart;
277,043,228✔
774
    case FUNCTION_TYPE_TWEND:
16,355,775✔
775
      return &pParams->wend;
16,355,775✔
776
    case FUNCTION_TYPE_TWDURATION:
937,119✔
777
      return &pParams->wduration;
937,119✔
778
    case FUNCTION_TYPE_TWROWNUM:
98,998✔
779
      return &pParams->wrownum;
98,998✔
780
    case FUNCTION_TYPE_TPREV_LOCALTIME:
166,133✔
781
      return &pParams->prevLocalTime;
166,133✔
782
    case FUNCTION_TYPE_TLOCALTIME:
455,969✔
783
      return &pParams->triggerTime;
455,969✔
784
    case FUNCTION_TYPE_TNEXT_LOCALTIME:
257,525✔
785
      return &pParams->nextLocalTime;
257,525✔
786
    case FUNCTION_TYPE_TGRPID:
128,755✔
787
      return &pStreamRuntimeFuncInfo->groupId;
128,755✔
788
    case FUNCTION_TYPE_PLACEHOLDER_COLUMN:
854,753✔
789
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
854,753✔
790
    case FUNCTION_TYPE_PLACEHOLDER_TBNAME:
1,970,053✔
791
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
1,970,053✔
792
    case FUNCTION_TYPE_TIDLESTART:
5,832✔
793
      return &pParams->idlestart;
5,832✔
794
    case FUNCTION_TYPE_TIDLEEND:
5,832✔
795
      return &pParams->idleend;
5,832✔
796
    default:
×
797
      break;
×
798
  }
799
  return NULL;
×
800
}
801

802
bool fmIsStreamPesudoColVal(int32_t funcId) {
505,970✔
803
  return funcMgtBuiltins[funcId].type == FUNCTION_TYPE_PLACEHOLDER_COLUMN
505,970✔
804
         || funcMgtBuiltins[funcId].type == FUNCTION_TYPE_PLACEHOLDER_TBNAME;
505,970✔
805
}
806

807
int32_t fmGetStreamPesudoFuncEnv(int32_t funcId, SNodeList* pParamNodes, SFuncExecEnv *pEnv) {
6,917,601✔
808
  if (NULL == pParamNodes || pParamNodes->length < 1) {
6,917,601✔
809
    uError("invalid stream pesudo func param list %p", pParamNodes);
33✔
810
    return TSDB_CODE_INTERNAL_ERROR;
×
811
  }
812

813
  int32_t firstParamType = nodeType(nodesListGetNode(pParamNodes, 0));
6,918,078✔
814
  if (QUERY_NODE_VALUE != firstParamType) {
6,918,245✔
815
    uError("invalid stream pesudo func first param type %d", firstParamType);
×
816
    return TSDB_CODE_INTERNAL_ERROR;
×
817
  }
818

819
  SValueNode* pResDesc = (SValueNode*)nodesListGetNode(pParamNodes, 0);
6,918,245✔
820
  pEnv->calcMemSize = pResDesc->node.resType.bytes;
6,919,011✔
821

822
  return TSDB_CODE_SUCCESS;
6,918,466✔
823
}
824

825

826
int32_t fmSetStreamPseudoFuncParamVal(int32_t funcId, SNodeList* pParamNodes, const SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
300,436,030✔
827
  if (!pStreamRuntimeInfo) {
300,436,030✔
828
    uError("internal error, should have pVals for stream pseudo funcs");
×
829
    return TSDB_CODE_INTERNAL_ERROR;
×
830
  }
831
  int32_t code = 0;
300,436,030✔
832
  SArray *pVals1 = NULL;
300,436,030✔
833
  SNode* pFirstParam = nodesListGetNode(pParamNodes, 0);
300,436,030✔
834
  if (nodeType(pFirstParam) != QUERY_NODE_VALUE) {
300,498,093✔
835
    uError("invalid param node type: %d for func: %d", nodeType(pFirstParam), funcId);
×
836
    return TSDB_CODE_INTERNAL_ERROR;
×
837
  }
838

839
  int32_t t = funcMgtBuiltins[funcId].type;
300,514,150✔
840
  uDebug("set stream pseudo func param val, functype: %d, pStreamRuntimeInfo: %p", t, pStreamRuntimeInfo);
300,487,376✔
841
  if (FUNCTION_TYPE_TGRPID == t) {
300,512,461✔
842
    SValue v = {0};
128,755✔
843
    v.type = TSDB_DATA_TYPE_BIGINT;
128,755✔
844
    v.val = *(int64_t*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
128,755✔
845
    
846
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&v, pFirstParam->type));
128,755✔
847
    if (code != 0) {
128,755✔
848
      uError("failed to set value node value: %s", tstrerror(code));
×
849
      return code;
×
850
    }
851
  } else if (FUNCTION_TYPE_PLACEHOLDER_COLUMN == t) {
300,383,706✔
852
    SNode* pSecondParam = nodesListGetNode(pParamNodes, 1);
854,521✔
853
    if (nodeType(pSecondParam) != QUERY_NODE_VALUE) {
854,753✔
854
      uError("invalid param node type: %d for func: %d", nodeType(pSecondParam), funcId);
×
855
      return TSDB_CODE_INTERNAL_ERROR;
×
856
    }
857
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
854,521✔
858
    int32_t idx = ((SValueNode*)pSecondParam)->datum.i;
854,521✔
859
    if (idx - 1 < 0 || idx - 1 >= taosArrayGetSize(pVal)) {
854,521✔
860
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
38✔
861
      return TSDB_CODE_INTERNAL_ERROR;
×
862
    }
863
    SStreamGroupValue* pValue = taosArrayGet(pVal, idx - 1);
854,251✔
864
    if (pValue == NULL) {
854,483✔
865
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
×
866
      return TSDB_CODE_INTERNAL_ERROR;
×
867
    }
868
    if (!pValue->isNull){
854,483✔
869
      if (pValue->data.type != ((SValueNode*)pFirstParam)->node.resType.type){
846,653✔
870
        uError("invalid value type: %d for func: %d, should be: %d", pValue->data.type, funcId, ((SValueNode*)pFirstParam)->node.resType.type);
×
871
        return TSDB_CODE_INTERNAL_ERROR;
×
872
      }
873
      if (IS_VAR_DATA_TYPE(((SValueNode*)pFirstParam)->node.resType.type)) {
846,151✔
874
        char* tmp = taosMemoryCalloc(1, pValue->data.nData + VARSTR_HEADER_SIZE); 
582,866✔
875
        if (tmp == NULL) {
583,136✔
876
          return TSDB_CODE_OUT_OF_MEMORY;
×
877
        }
878
        taosMemoryFree(((SValueNode*)pFirstParam)->datum.p);
583,136✔
879
        ((SValueNode*)pFirstParam)->datum.p = tmp;
582,904✔
880
        memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
582,634✔
881
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
582,904✔
882
      } else {
883
        code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&pValue->data, pValue->data.type));
263,517✔
884
      }
885
    }
886
    ((SValueNode*)pFirstParam)->isNull = pValue->isNull;
854,289✔
887
  } else if (FUNCTION_TYPE_PLACEHOLDER_TBNAME == t) {
299,529,185✔
888
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
1,969,217✔
889
    for (int32_t i = 0; i < taosArrayGetSize(pVal); ++i) {
2,342,941✔
890
      SStreamGroupValue* pValue = taosArrayGet(pVal, i);
2,341,576✔
891
      if (pValue != NULL && pValue->isTbname) {
2,342,121✔
892
        taosMemoryFreeClear(((SValueNode*)pFirstParam)->datum.p);
1,971,421✔
893
        ((SValueNode*)pFirstParam)->datum.p = taosMemoryCalloc(pValue->data.nData + VARSTR_HEADER_SIZE, 1);
1,971,151✔
894
        if (NULL == ((SValueNode*)pFirstParam)->datum.p ) {
1,970,039✔
895
          return terrno;
×
896
        }
897

898
        (void)memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
1,970,048✔
899
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
1,970,331✔
900
        break;
1,970,603✔
901
      }
902
    }
903
  } else if (LIST_LENGTH(pParamNodes) == 1) {
595,121,450✔
904
    // twstart, twend
905
    const void* pVal = fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
297,536,377✔
906
    if (!pVal) {
297,547,177✔
907
      uError("failed to set stream pseudo func param val, NULL val for funcId: %d", funcId);
×
908
      return TSDB_CODE_INTERNAL_ERROR;
×
909
    }
910
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, (void*)pVal);
297,547,177✔
911
    if (code != 0) {
297,572,411✔
912
      uError("failed to set value node value: %s", tstrerror(code));
10,929✔
913
      return code;
×
914
    }
915
  } else {
916
    uError("invalid placeholder function type: %d", t);
45,958✔
917
    return TSDB_CODE_INTERNAL_ERROR;
×
918
  }
919
  
920
  return code;
300,512,520✔
921
}
922

923

924

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