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

taosdata / TDengine / #4923

09 Jan 2026 08:13AM UTC coverage: 65.373% (+0.2%) from 65.161%
#4923

push

travis-ci

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

33 of 56 new or added lines in 8 files covered. (58.93%)

3042 existing lines in 131 files now uncovered.

198273 of 303297 relevant lines covered (65.37%)

118980690.73 hits per line

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

75.65
/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 "taos.h"
22
#include "taoserror.h"
23
#include "thash.h"
24
#include "tudf.h"
25

26
typedef struct SFuncMgtService {
27
  SHashObj* pFuncNameHashTable;
28
} SFuncMgtService;
29

30
static SFuncMgtService gFunMgtService;
31
static TdThreadOnce    functionHashTableInit = PTHREAD_ONCE_INIT;
32
static int32_t         initFunctionCode = 0;
33

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

42
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
373,615,875✔
43
    if (TSDB_CODE_SUCCESS != taosHashPut(gFunMgtService.pFuncNameHashTable, funcMgtBuiltins[i].name,
371,955,360✔
44
                                         strlen(funcMgtBuiltins[i].name), &i, sizeof(int32_t))) {
371,955,360✔
45
      initFunctionCode = terrno;
×
46
      return;
×
47
    }
48
  }
49
}
50

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

63
int32_t fmFuncMgtInit() {
1,660,515✔
64
  (void)taosThreadOnce(&functionHashTableInit, doInitFunctionTable);
1,660,515✔
65
  return initFunctionCode;
1,660,515✔
66
}
67

68
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
561,755,546✔
69
  if (NULL != gFunMgtService.pFuncNameHashTable) {
561,755,546✔
70
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
561,592,061✔
71
    if (NULL != pVal) {
561,593,179✔
72
      pFunc->funcId = *(int32_t*)pVal;
561,057,403✔
73
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
561,057,283✔
74
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
561,056,686✔
75
    }
76
    return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
535,776✔
77
  }
78
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
3,030,489✔
79
    if (0 == strcmp(funcMgtBuiltins[i].name, pFunc->functionName)) {
3,029,952✔
80
      pFunc->funcId = i;
162,948✔
81
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
162,948✔
82
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
162,948✔
83
    }
84
  }
85
  return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
537✔
86
}
87

88
EFuncReturnRows fmGetFuncReturnRows(SFunctionNode* pFunc) {
12,674,261✔
89
  if (NULL != funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc) {
12,674,261✔
90
    return funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc(pFunc);
8,245,583✔
91
  }
92
  return (fmIsIndefiniteRowsFunc(pFunc->funcId) || fmIsMultiRowsFunc(pFunc->funcId)) ? FUNC_RETURN_ROWS_INDEFINITE
4,428,678✔
93
                                                                                     : FUNC_RETURN_ROWS_NORMAL;
4,428,678✔
94
}
95

96
bool fmIsBuiltinFunc(const char* pFunc) {
47,476✔
97
  return NULL != taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
47,476✔
98
}
99

100
EFunctionType fmGetFuncType(const char* pFunc) {
456,822,163✔
101
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
456,822,163✔
102
  if (NULL != pVal) {
456,823,072✔
103
    return funcMgtBuiltins[*(int32_t*)pVal].type;
456,270,559✔
104
  }
105
  return FUNCTION_TYPE_UDF;
552,513✔
106
}
107

108
EFunctionType fmGetFuncTypeFromId(int32_t funcId) {
2,649,432✔
109
  if (funcId < funcMgtBuiltinsNum) {
2,649,432✔
110
    return funcMgtBuiltins[funcId].type;
2,649,671✔
111
  }
112
  return FUNCTION_TYPE_UDF;
×
113
}
114

115
EFuncDataRequired fmFuncDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
29,494,880✔
116
  if (fmIsUserDefinedFunc(pFunc->funcId) || pFunc->funcId < 0 || pFunc->funcId >= funcMgtBuiltinsNum) {
29,494,880✔
117
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
118
  }
119
  if (NULL == funcMgtBuiltins[pFunc->funcId].dataRequiredFunc) {
29,494,836✔
120
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
121
  }
122
  return funcMgtBuiltins[pFunc->funcId].dataRequiredFunc(pFunc, pTimeWindow);
29,494,876✔
123
}
124

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

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

136
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
2,147,483,647✔
137
    return FUNC_DATA_REQUIRED_DATA_LOAD;
74,561,924✔
138
  } else {
139
    return funcMgtBuiltins[funcId].dynDataRequiredFunc(pRes, pBlockInfo);
2,147,483,647✔
140
  }
141
}
142

143
int32_t fmGetFuncExecFuncs(int32_t funcId, SFuncExecFuncs* pFpSet) {
252,008,443✔
144
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
252,008,443✔
145
    return TSDB_CODE_FAILED;
643✔
146
  }
147
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
252,032,383✔
148
  pFpSet->init = funcMgtBuiltins[funcId].initFunc;
252,021,783✔
149
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
252,043,855✔
150
  pFpSet->finalize = funcMgtBuiltins[funcId].finalizeFunc;
252,042,511✔
151
  pFpSet->combine = funcMgtBuiltins[funcId].combineFunc;
252,031,142✔
152
  pFpSet->processFuncByRow = funcMgtBuiltins[funcId].processFuncByRow;
252,038,068✔
153
  pFpSet->cleanup = funcMgtBuiltins[funcId].cleanupFunc;
252,019,752✔
154
  return TSDB_CODE_SUCCESS;
252,004,394✔
155
}
156

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

172
int32_t fmGetScalarFuncExecFuncs(int32_t funcId, SScalarFuncExecFuncs* pFpSet) {
1,022,728,149✔
173
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,022,728,149✔
174
    return TSDB_CODE_FAILED;
165,025✔
175
  }
176
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
1,022,981,725✔
177
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
1,022,975,004✔
178
  return TSDB_CODE_SUCCESS;
1,022,964,925✔
179
}
180

181
bool fmIsAggFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_AGG_FUNC); }
1,617,435,737✔
182

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

185
bool fmIsVectorFunc(int32_t funcId) { return !fmIsScalarFunc(funcId) && !fmIsPseudoColumnFunc(funcId); }
874,943,439✔
186

187
bool fmIsSelectColsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_COLS_FUNC); }
83,902,244✔
188

189
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
1,476,741,945✔
190

191
bool fmIsTimelineFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TIMELINE_FUNC); }
814,911,273✔
192

193
bool fmIsDateTimeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_DATETIME_FUNC); }
412,642,651✔
194

195
bool fmIsPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PSEUDO_COLUMN_FUNC); }
1,544,980,232✔
196

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

199
bool fmIsWindowPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_WINDOW_PC_FUNC); }
1,013,685,569✔
200

201
bool fmIsWindowClauseFunc(int32_t funcId) { return fmIsAggFunc(funcId) || fmIsWindowPseudoColumnFunc(funcId); }
34,302,513✔
202

203
bool fmIsStreamWindowClauseFunc(int32_t funcId) { return fmIsWindowClauseFunc(funcId) || fmIsPlaceHolderFunc(funcId); }
268,935✔
204

205
bool fmIsStreamVectorFunc(int32_t funcId) { return fmIsVectorFunc(funcId) || fmIsPlaceHolderFunc(funcId); }
7,344✔
206

207
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
1,155,681,102✔
208

209
bool fmIsSpecialDataRequiredFunc(int32_t funcId) {
70,052,768✔
210
  return isSpecificClassifyFunc(funcId, FUNC_MGT_SPECIAL_DATA_REQUIRED);
70,052,768✔
211
}
212

213
bool fmIsDynamicScanOptimizedFunc(int32_t funcId) {
33,919,910✔
214
  return isSpecificClassifyFunc(funcId, FUNC_MGT_DYNAMIC_SCAN_OPTIMIZED);
33,919,910✔
215
}
216

217
bool fmIsMultiResFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_RES_FUNC); }
849,334,137✔
218

219
bool fmIsRepeatScanFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_REPEAT_SCAN_FUNC); }
824,320,794✔
220

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

223
bool fmIsForbidFillFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_FILL_FUNC); }
412,854,753✔
224

225
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
66,359,293✔
226

227
bool fmIsSystemInfoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SYSTEM_INFO_FUNC); }
415,472,526✔
228

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

231
bool fmIsClientPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CLIENT_PC_FUNC); }
415,247,445✔
232

233
bool fmIsMultiRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_ROWS_FUNC); }
821,634,726✔
234

235
bool fmIsKeepOrderFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_KEEP_ORDER_FUNC); }
186,954,476✔
236

237
bool fmIsCumulativeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CUMULATIVE_FUNC); }
85,995,613✔
238

239
bool fmIsForbidSysTableFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_SYSTABLE_FUNC); }
412,801,945✔
240

241
bool fmIsInterpFunc(int32_t funcId) {
839,546,341✔
242
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
839,546,341✔
243
    return false;
1,028,440✔
244
  }
245
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
838,518,351✔
246
}
247

248
bool fmIsInterpPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERP_PC_FUNC); }
830,935,608✔
249

250
bool fmIsForecastFunc(int32_t funcId) {
810,104,484✔
251
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
810,104,484✔
252
    return false;
1,029,011✔
253
  }
254
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
809,075,703✔
255
}
256

257
bool fmIsAnalysisPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_ANALYTICS_PC_FUNC); }
832,742,100✔
258

259
bool fmIsImputationCorrelationFunc(int32_t funcId) {
×
260
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
261
    return false;
×
262
  }
263

264
  int32_t type = funcMgtBuiltins[funcId].type;
×
265
  return FUNCTION_TYPE_IMPUTATION == type || FUNCTION_TYPE_DTW == type || FUNCTION_TYPE_DTW_PATH == type ||
×
266
         FUNCTION_TYPE_TLCC == type;
267
}
268

269
bool fmIsLastRowFunc(int32_t funcId) {
49,486,236✔
270
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
49,486,236✔
271
    return false;
×
272
  }
273
  return FUNCTION_TYPE_LAST_ROW == funcMgtBuiltins[funcId].type;
49,486,294✔
274
}
275

276
bool fmIsLastFunc(int32_t funcId) {
×
277
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
278
    return false;
×
279
  }
280
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type;
×
281
}
282

283
bool fmIsNotNullOutputFunc(int32_t funcId) {
384,996,987✔
284
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
384,996,987✔
285
    return false;
291,140✔
286
  }
287
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
384,716,133✔
288
         FUNCTION_TYPE_CACHE_LAST == funcMgtBuiltins[funcId].type ||
370,260,958✔
289
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
369,547,723✔
290
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
363,791,651✔
291
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
361,156,449✔
292
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
351,621,826✔
293
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
347,786,828✔
294
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
346,240,269✔
295
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
294,693,892✔
296
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
1,046,270,021✔
297
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
291,295,426✔
298
}
299

300
bool fmIsSelectValueFunc(int32_t funcId) {
83,710,182✔
301
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
83,710,182✔
302
    return false;
×
303
  }
304
  return FUNCTION_TYPE_SELECT_VALUE == funcMgtBuiltins[funcId].type;
83,710,182✔
305
}
306

307
bool fmIsGroupKeyFunc(int32_t funcId) {
69,165,307✔
308
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
69,165,307✔
UNCOV
309
    return false;
×
310
  }
311
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
69,171,645✔
312
}
313

314
bool fmisSelectGroupConstValueFunc(int32_t funcId) {
6,926,815✔
315
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
6,926,815✔
316
    return false;
×
317
  }
318
  return FUNCTION_TYPE_GROUP_CONST_VALUE == funcMgtBuiltins[funcId].type;
6,926,815✔
319
}
320

321
bool fmIsElapsedFunc(int32_t funcId) {
9,836,855✔
322
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
9,836,855✔
323
    return false;
×
324
  }
325
  return FUNCTION_TYPE_ELAPSED == funcMgtBuiltins[funcId].type;
9,836,855✔
326
}
327

328
bool fmIsBlockDistFunc(int32_t funcId) {
412,642,331✔
329
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
412,642,331✔
330
    return false;
515,069✔
331
  }
332
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
412,127,372✔
333
}
334

335
bool fmIsDBUsageFunc(int32_t funcId) {
412,642,438✔
336
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
412,642,438✔
337
    return false;
514,930✔
338
  }
339
  return FUNCTION_TYPE_DB_USAGE == funcMgtBuiltins[funcId].type;
412,127,618✔
340
}
341

342
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
453,870,992✔
343

344
bool fmIsIgnoreNullFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IGNORE_NULL_FUNC); }
290,909✔
345

346
void fmFuncMgtDestroy() {
1,660,565✔
347
  void* m = gFunMgtService.pFuncNameHashTable;
1,660,565✔
348
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
1,660,565✔
349
    taosHashCleanup(m);
1,660,515✔
350
  }
351
}
1,660,565✔
352

353
#ifdef BUILD_NO_CALL
354
int32_t fmSetNormalFunc(int32_t funcId, SFuncExecFuncs* pFpSet) {
355
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
356
    return TSDB_CODE_FAILED;
357
  }
358
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
359
  return TSDB_CODE_SUCCESS;
360
}
361
#endif
362

363
// function has same input/output type
364
bool fmIsSameInOutType(int32_t funcId) {
25,359,942✔
365
  bool res = false;
25,359,942✔
366
  switch (funcMgtBuiltins[funcId].type) {
25,359,942✔
367
    case FUNCTION_TYPE_MAX:
10,125,885✔
368
    case FUNCTION_TYPE_MIN:
369
    case FUNCTION_TYPE_TOP:
370
    case FUNCTION_TYPE_BOTTOM:
371
    case FUNCTION_TYPE_FIRST:
372
    case FUNCTION_TYPE_LAST:
373
    case FUNCTION_TYPE_SAMPLE:
374
    case FUNCTION_TYPE_TAIL:
375
    case FUNCTION_TYPE_UNIQUE:
376
      res = true;
10,125,885✔
377
      break;
10,125,885✔
378
    default:
15,234,057✔
379
      break;
15,234,057✔
380
  }
381
  return res;
25,359,942✔
382
}
383

384
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
10,741,506✔
385
  SNode* pNode;
386
  FOREACH(pNode, pFunc->pParameterList) {
10,819,038✔
387
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
85,000✔
388
      return false;
7,468✔
389
    }
390
  }
391
  return true;
10,734,038✔
392
}
393

394
bool fmIsSkipScanCheckFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SKIP_SCAN_CHECK_FUNC); }
33,919,957✔
395

396
bool fmIsPrimaryKeyFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PRIMARY_KEY_FUNC); }
403,452,450✔
397

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

400
void getLastCacheDataType(SDataType* pType, int32_t pkBytes) {
×
401
  // TODO: do it later.
402
  pType->bytes = getFirstLastInfoSize(pType->bytes, pkBytes) + VARSTR_HEADER_SIZE;
×
403
  pType->type = TSDB_DATA_TYPE_BINARY;
×
404
}
×
405

406
static int32_t getFuncInfo(SFunctionNode* pFunc) {
84,296,708✔
407
  char msg[128] = {0};
84,296,708✔
408
  return fmGetFuncInfo(pFunc, msg, sizeof(msg));
84,296,708✔
409
}
410

411
int32_t createFunction(const char* pName, SNodeList* pParameterList, SFunctionNode** ppFunc) {
8,216,788✔
412
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
8,216,788✔
413
  if (NULL == *ppFunc) {
8,216,788✔
414
    return code;
×
415
  }
416
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
8,216,788✔
417
  (*ppFunc)->pParameterList = pParameterList;
8,216,788✔
418
  code = getFuncInfo((*ppFunc));
8,216,788✔
419
  if (TSDB_CODE_SUCCESS != code) {
8,216,788✔
420
    (*ppFunc)->pParameterList = NULL;
×
421
    nodesDestroyNode((SNode*)*ppFunc);
×
422
    *ppFunc = NULL;
×
423
    return code;
×
424
  }
425
  return code;
8,216,788✔
426
}
427

428
static void resetOutputChangedFunc(SFunctionNode *pFunc, const SFunctionNode* pSrcFunc) {
76,079,920✔
429
  if (funcMgtBuiltins[pFunc->funcId].type == FUNCTION_TYPE_LAST_MERGE) {
76,079,920✔
430
    pFunc->node.resType = pSrcFunc->node.resType;
14,217,962✔
431
    return;
14,217,962✔
432
  }
433
}
434

435
int32_t createFunctionWithSrcFunc(const char* pName, const SFunctionNode* pSrcFunc, SNodeList* pParameterList, SFunctionNode** ppFunc) {
76,079,920✔
436
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
76,079,920✔
437
  if (NULL == *ppFunc) {
76,079,967✔
438
    return code;
×
439
  }
440

441
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
76,079,967✔
442
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
76,079,967✔
443
  (*ppFunc)->pSrcFuncRef = pSrcFunc;
76,079,967✔
444

445
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
76,079,967✔
446
  (*ppFunc)->pParameterList = pParameterList;
76,079,967✔
447
  code = getFuncInfo((*ppFunc));
76,079,967✔
448
  if (TSDB_CODE_SUCCESS != code) {
76,079,920✔
449
    (*ppFunc)->pParameterList = NULL;
×
450
    nodesDestroyNode((SNode*)*ppFunc);
×
451
    *ppFunc = NULL;
×
452
    return code;
×
453
  }
454
  resetOutputChangedFunc(*ppFunc, pSrcFunc);
76,079,920✔
455
  (*ppFunc)->node.relatedTo = pSrcFunc->node.relatedTo;
76,079,913✔
456
  (*ppFunc)->node.bindExprID = pSrcFunc->node.bindExprID;
76,079,920✔
457
  return code;
76,079,920✔
458
}
459

460
static int32_t createColumnByFunc(const SFunctionNode* pFunc, SColumnNode** ppCol) {
50,719,924✔
461
  int32_t code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)ppCol);
50,719,924✔
462
  if (NULL == *ppCol) {
50,719,978✔
463
    return code;
×
464
  }
465
  tstrncpy((*ppCol)->colName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
50,719,978✔
466
  (*ppCol)->node.resType = pFunc->node.resType;
50,719,978✔
467
  return TSDB_CODE_SUCCESS;
50,719,931✔
468
}
469

470
bool fmIsDistExecFunc(int32_t funcId) {
202,103,908✔
471
  if (fmIsUserDefinedFunc(funcId)) {
202,103,908✔
472
    return false;
285,452✔
473
  }
474
  if (!fmIsVectorFunc(funcId)) {
201,818,537✔
475
    return true;
134,620✔
476
  }
477
  return (NULL != funcMgtBuiltins[funcId].pPartialFunc && NULL != funcMgtBuiltins[funcId].pMergeFunc);
201,683,847✔
478
}
479

480
static int32_t createPartialFunction(const SFunctionNode* pSrcFunc, SFunctionNode** pPartialFunc) {
25,359,989✔
481
  SNodeList* pParameterList = NULL;
25,359,989✔
482
  int32_t    code = nodesCloneList(pSrcFunc->pParameterList, &pParameterList);
25,359,989✔
483
  if (NULL == pParameterList) {
25,359,989✔
484
    return code;
×
485
  }
486
  code =
487
      createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pPartialFunc, pSrcFunc, pParameterList, pPartialFunc);
25,359,989✔
488
  if (TSDB_CODE_SUCCESS != code) {
25,359,942✔
489
    nodesDestroyList(pParameterList);
×
490
    return code;
×
491
  }
492
  (*pPartialFunc)->hasOriginalFunc = true;
25,359,942✔
493
  (*pPartialFunc)->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
25,359,942✔
494
  char name[TSDB_FUNC_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_POINTER_PRINT_BYTES + 1] = {0};
25,359,942✔
495

496
  int32_t len = tsnprintf(name, sizeof(name), "%s.%p", (*pPartialFunc)->functionName, pSrcFunc);
25,359,942✔
497
  if (taosHashBinary(name, len) < 0) {
25,359,989✔
498
    return TSDB_CODE_FAILED;
×
499
  }
500
  tstrncpy((*pPartialFunc)->node.aliasName, name, TSDB_COL_NAME_LEN);
25,359,989✔
501
  return TSDB_CODE_SUCCESS;
25,359,942✔
502
}
503

504
static int32_t createMergeFuncPara(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
50,719,924✔
505
                                   SNodeList** pParameterList) {
506
  SNode*  pRes = NULL;
50,719,924✔
507
  int32_t code = createColumnByFunc(pPartialFunc, (SColumnNode**)&pRes);
50,719,924✔
508
  if (TSDB_CODE_SUCCESS != code) {
50,719,931✔
509
    return code;
×
510
  }
511
  if (NULL != funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc) {
50,719,931✔
512
    return funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc(pSrcFunc->pParameterList, pRes, pParameterList);
498,062✔
513
  } else {
514
    return nodesListMakeStrictAppend(pParameterList, pRes);
50,221,815✔
515
  }
516
}
517

518
static int32_t createMidFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
25,359,942✔
519
                                 SFunctionNode** pMidFunc) {
520
  SNodeList*     pParameterList = NULL;
25,359,942✔
521
  SFunctionNode* pFunc = NULL;
25,359,942✔
522

523
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
25,359,989✔
524
  if (TSDB_CODE_SUCCESS == code) {
25,359,942✔
525
    if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){
25,359,942✔
526
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pSrcFunc, pParameterList, &pFunc);
1,831,980✔
527
    }else{
528
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
23,527,962✔
529
    }
530
  }
531
  if (TSDB_CODE_SUCCESS == code) {
25,359,942✔
532
    tstrncpy(pFunc->node.aliasName, pPartialFunc->node.aliasName, TSDB_COL_NAME_LEN);
25,359,942✔
533
  }
534

535
  if (TSDB_CODE_SUCCESS == code) {
25,359,935✔
536
    *pMidFunc = pFunc;
25,359,935✔
537
  } else {
538
    nodesDestroyList(pParameterList);
×
539
  }
540
  return code;
25,359,935✔
541
}
542

543
static int32_t createMergeFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
25,359,942✔
544
                                   SFunctionNode** pMergeFunc) {
545
  SNodeList*     pParameterList = NULL;
25,359,942✔
546
  SFunctionNode* pFunc = NULL;
25,359,989✔
547

548
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
25,359,989✔
549
  if (TSDB_CODE_SUCCESS == code) {
25,359,989✔
550
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
25,359,989✔
551
  }
552
  if (TSDB_CODE_SUCCESS == code) {
25,359,942✔
553
    pFunc->hasOriginalFunc = true;
25,359,989✔
554
    pFunc->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
25,359,989✔
555
    // overwrite function restype set by translate function
556
    if (fmIsSameInOutType(pSrcFunc->funcId)) {
25,359,942✔
557
      pFunc->node.resType = pSrcFunc->node.resType;
10,125,885✔
558
    }
559
    tstrncpy(pFunc->node.aliasName, pSrcFunc->node.aliasName, TSDB_COL_NAME_LEN);
25,359,989✔
560
  }
561

562
  if (TSDB_CODE_SUCCESS == code) {
25,359,942✔
563
    *pMergeFunc = pFunc;
25,359,942✔
564
  } else {
565
    nodesDestroyList(pParameterList);
×
566
  }
567
  return code;
25,359,942✔
568
}
569

570
int32_t fmGetDistMethod(const SFunctionNode* pFunc, SFunctionNode** pPartialFunc, SFunctionNode** pMidFunc,
25,359,942✔
571
                        SFunctionNode** pMergeFunc) {
572
  if (!fmIsDistExecFunc(pFunc->funcId)) {
25,359,942✔
573
    return TSDB_CODE_FAILED;
×
574
  }
575

576
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
25,359,929✔
577
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
25,359,942✔
578
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
25,359,942✔
579
  }
580
  if (TSDB_CODE_SUCCESS == code) {
25,359,935✔
581
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
25,359,935✔
582
  }
583

584
  if (TSDB_CODE_SUCCESS != code) {
25,359,942✔
585
    nodesDestroyNode((SNode*)*pPartialFunc);
×
586
    if (pMidFunc) nodesDestroyNode((SNode*)*pMidFunc);
×
587
    nodesDestroyNode((SNode*)*pMergeFunc);
×
588
  }
589

590
  return code;
25,359,942✔
591
}
592

593
const char* fmGetFuncName(int32_t funcId) {
317,088✔
594
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
317,088✔
595
    return "invalid function";
×
596
  }
597
  return funcMgtBuiltins[funcId].name;
317,088✔
598
}
599

600
/// @param [out] pStateFunc, not changed if error occured or no need to create state func
601
/// @retval 0 for succ, otherwise err occured
602
static int32_t fmCreateStateFunc(const SFunctionNode* pFunc, SFunctionNode** pStateFunc) {
1,414✔
603
  if (funcMgtBuiltins[pFunc->funcId].pStateFunc) {
1,414✔
604
    SNodeList* pParams = NULL;
1,414✔
605
    int32_t    code = nodesCloneList(pFunc->pParameterList, &pParams);
1,414✔
606
    if (!pParams) return code;
1,414✔
607
    code = createFunction(funcMgtBuiltins[pFunc->funcId].pStateFunc, pParams, pStateFunc);
1,414✔
608
    if (TSDB_CODE_SUCCESS != code) {
1,414✔
609
      nodesDestroyList(pParams);
×
610
      return code;
×
611
    }
612
    tstrncpy((*pStateFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
1,414✔
613
    tstrncpy((*pStateFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
1,414✔
614
  }
615
  return TSDB_CODE_SUCCESS;
1,414✔
616
}
617

618
bool fmIsTSMASupportedFunc(func_id_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC); }
407,232✔
619

620
bool fmIsRsmaSupportedFunc(func_id_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_RSMA_FUNC); }
119,642✔
621

622
int32_t fmCreateStateFuncs(SNodeList* pFuncs) {
707✔
623
  int32_t code;
624
  SNode*  pNode;
625
  char    buf[128] = {0};
707✔
626
  FOREACH(pNode, pFuncs) {
2,121✔
627
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
1,414✔
628
    code = fmGetFuncInfo(pFunc, buf, 128);
1,414✔
629
    if (code) break;
1,414✔
630
    if (fmIsTSMASupportedFunc(pFunc->funcId)) {
1,414✔
631
      SFunctionNode* pNewFunc = NULL;
1,414✔
632
      code = fmCreateStateFunc(pFunc, &pNewFunc);
1,414✔
633
      if (code) {
1,414✔
634
        // error
635
        break;
×
636
      } else if (!pNewFunc) {
1,414✔
637
        // no need state func
638
        continue;
×
639
      } else {
640
        REPLACE_NODE(pNewFunc);
1,414✔
641
        nodesDestroyNode(pNode);
1,414✔
642
      }
643
    }
644
  }
645
  return code;
707✔
646
}
647

648
static int32_t fmCreateStateMergeFunc(SFunctionNode* pFunc, SFunctionNode** pStateMergeFunc) {
×
649
  if (funcMgtBuiltins[pFunc->funcId].pMergeFunc) {
×
650
    SNodeList* pParams = NULL;
×
651
    int32_t    code = nodesCloneList(pFunc->pParameterList, &pParams);
×
652
    if (!pParams) return code;
×
653
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pFunc->funcId].pMergeFunc, pFunc, pParams, pStateMergeFunc);
×
654
    if (TSDB_CODE_SUCCESS != code) {
×
655
      nodesDestroyList(pParams);
×
656
      return code;
×
657
    }
658
    tstrncpy((*pStateMergeFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
×
659
    tstrncpy((*pStateMergeFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
×
660
  }
661
  return TSDB_CODE_SUCCESS;
×
662
}
663

664
int32_t fmCreateStateMergeFuncs(SNodeList* pFuncs) {
×
665
  int32_t code;
666
  SNode*  pNode;
667
  char    buf[128] = {0};
×
668
  FOREACH(pNode, pFuncs) {
×
669
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
×
670
    if (fmIsTSMASupportedFunc(pFunc->funcId)) {
×
671
      SFunctionNode* pNewFunc = NULL;
×
672
      code = fmCreateStateMergeFunc(pFunc, &pNewFunc);
×
673
      if (code) {
×
674
        // error
675
        break;
×
676
      } else if (!pNewFunc) {
×
677
        // no state merge func
678
        continue;
×
679
      } else {
680
        REPLACE_NODE(pNewFunc);
×
681
        nodesDestroyNode(pNode);
×
682
      }
683
    }
684
  }
685
  return code;
×
686
}
687

688
int32_t fmGetFuncId(const char* name) {
366,512✔
689
  if (NULL != gFunMgtService.pFuncNameHashTable) {
366,512✔
690
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, name, strlen(name));
366,512✔
691
    if (NULL != pVal) {
366,512✔
692
      return *(int32_t*)pVal;
366,512✔
693
    }
694
    return -1;
×
695
  }
696
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
×
697
    if (0 == strcmp(funcMgtBuiltins[i].name, name)) {
×
698
      return i;
×
699
    }
700
  }
701
  return -1;
×
702
}
703

704
bool fmIsMyStateFunc(int32_t funcId, int32_t stateFuncId) {
×
705
  const SBuiltinFuncDefinition* pFunc = &funcMgtBuiltins[funcId];
×
706
  const SBuiltinFuncDefinition* pStateFunc = &funcMgtBuiltins[stateFuncId];
×
707
  if (!pFunc->pStateFunc) {
×
708
    return false;
×
709
  }
710
  if (strcmp(pFunc->pStateFunc, pStateFunc->name) == 0) return true;
×
711
  int32_t stateMergeFuncId = fmGetFuncId(pFunc->pStateFunc);
×
712
  if (stateMergeFuncId == -1) {
×
713
    return false;
×
714
  }
715
  const SBuiltinFuncDefinition* pStateMergeFunc = &funcMgtBuiltins[stateMergeFuncId];
×
716
  return strcmp(pStateFunc->name, pStateMergeFunc->pMergeFunc) == 0;
×
717
}
718

719
bool fmIsCountLikeFunc(int32_t funcId) {
325,213,130✔
720
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
325,213,130✔
721
}
722

723
bool fmIsRowTsOriginFunc(int32_t funcId) {
3,430,624✔
724
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
3,430,624✔
725
    return false;
×
726
  }
727
  return FUNCTION_TYPE_IROWTS_ORIGIN == funcMgtBuiltins[funcId].type;
3,430,624✔
728
}
729

730
bool fmIsGroupIdFunc(int32_t funcId) {
×
731
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
732
    return false;
×
733
  }
734
  return FUNCTION_TYPE_GROUP_ID == funcMgtBuiltins[funcId].type;
×
735
}
736

737
const void* fmGetStreamPesudoFuncVal(int32_t funcId, const SStreamRuntimeFuncInfo* pStreamRuntimeFuncInfo) {
36,384,366✔
738
  SSTriggerCalcParam *pParams = taosArrayGet(pStreamRuntimeFuncInfo->pStreamPesudoFuncVals, pStreamRuntimeFuncInfo->curIdx);
36,384,366✔
739
  switch (funcMgtBuiltins[funcId].type) {
36,387,272✔
740
    case FUNCTION_TYPE_TPREV_TS:
×
741
      return &pParams->prevTs;
×
742
    case FUNCTION_TYPE_TCURRENT_TS:
1,803,900✔
743
      return &pParams->currentTs;
1,803,900✔
744
    case FUNCTION_TYPE_TNEXT_TS:
×
745
      return &pParams->nextTs;
×
746
    case FUNCTION_TYPE_TWSTART:
17,960,451✔
747
      return &pParams->wstart;
17,960,451✔
748
    case FUNCTION_TYPE_TWEND:
13,551,777✔
749
      return &pParams->wend;
13,551,777✔
750
    case FUNCTION_TYPE_TWDURATION:
39,432✔
751
      return &pParams->wduration;
39,432✔
752
    case FUNCTION_TYPE_TWROWNUM:
97,433✔
753
      return &pParams->wrownum;
97,433✔
754
    case FUNCTION_TYPE_TPREV_LOCALTIME:
133,066✔
755
      return &pParams->prevLocalTime;
133,066✔
756
    case FUNCTION_TYPE_TLOCALTIME:
376,343✔
757
      return &pParams->triggerTime;
376,343✔
758
    case FUNCTION_TYPE_TNEXT_LOCALTIME:
210,136✔
759
      return &pParams->nextLocalTime;
210,136✔
760
    case FUNCTION_TYPE_TGRPID:
112,168✔
761
      return &pStreamRuntimeFuncInfo->groupId;
112,168✔
762
    case FUNCTION_TYPE_PLACEHOLDER_COLUMN:
506,300✔
763
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
506,300✔
764
    case FUNCTION_TYPE_PLACEHOLDER_TBNAME:
1,595,539✔
765
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
1,595,539✔
766
    default:
×
767
      break;
×
768
  }
769
  return NULL;
×
770
}
771

772
bool fmIsStreamPesudoColVal(int32_t funcId) {
173,338✔
773
  return funcMgtBuiltins[funcId].type == FUNCTION_TYPE_PLACEHOLDER_COLUMN
173,338✔
774
         || funcMgtBuiltins[funcId].type == FUNCTION_TYPE_PLACEHOLDER_TBNAME;
173,338✔
775
}
776

777
int32_t fmGetStreamPesudoFuncEnv(int32_t funcId, SNodeList* pParamNodes, SFuncExecEnv *pEnv) {
4,983,619✔
778
  if (NULL == pParamNodes || pParamNodes->length < 1) {
4,983,619✔
UNCOV
779
    uError("invalid stream pesudo func param list %p", pParamNodes);
×
780
    return TSDB_CODE_INTERNAL_ERROR;
×
781
  }
782

783
  int32_t firstParamType = nodeType(nodesListGetNode(pParamNodes, 0));
4,983,619✔
784
  if (QUERY_NODE_VALUE != firstParamType) {
4,983,619✔
785
    uError("invalid stream pesudo func first param type %d", firstParamType);
×
786
    return TSDB_CODE_INTERNAL_ERROR;
×
787
  }
788

789
  SValueNode* pResDesc = (SValueNode*)nodesListGetNode(pParamNodes, 0);
4,983,619✔
790
  pEnv->calcMemSize = pResDesc->node.resType.bytes;
4,983,380✔
791

792
  return TSDB_CODE_SUCCESS;
4,983,380✔
793
}
794

795

796
int32_t fmSetStreamPseudoFuncParamVal(int32_t funcId, SNodeList* pParamNodes, const SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
36,373,268✔
797
  if (!pStreamRuntimeInfo) {
36,373,268✔
798
    uError("internal error, should have pVals for stream pseudo funcs");
×
799
    return TSDB_CODE_INTERNAL_ERROR;
×
800
  }
801
  int32_t code = 0;
36,373,268✔
802
  SArray *pVals1 = NULL;
36,373,268✔
803
  SNode* pFirstParam = nodesListGetNode(pParamNodes, 0);
36,373,268✔
804
  if (nodeType(pFirstParam) != QUERY_NODE_VALUE) {
36,375,519✔
805
    uError("invalid param node type: %d for func: %d", nodeType(pFirstParam), funcId);
×
806
    return TSDB_CODE_INTERNAL_ERROR;
×
807
  }
808

809
  int32_t t = funcMgtBuiltins[funcId].type;
36,372,063✔
810
  uDebug("set stream pseudo func param val, functype: %d, pStreamRuntimeInfo: %p", t, pStreamRuntimeInfo);
36,376,786✔
811
  if (FUNCTION_TYPE_TGRPID == t) {
36,387,731✔
812
    SValue v = {0};
112,168✔
813
    v.type = TSDB_DATA_TYPE_BIGINT;
112,168✔
814
    v.val = *(int64_t*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
112,168✔
815
    
816
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&v, pFirstParam->type));
112,571✔
817
    if (code != 0) {
112,168✔
818
      uError("failed to set value node value: %s", tstrerror(code));
×
819
      return code;
×
820
    }
821
  } else if (FUNCTION_TYPE_PLACEHOLDER_COLUMN == t) {
36,275,563✔
822
    SNode* pSecondParam = nodesListGetNode(pParamNodes, 1);
505,897✔
823
    if (nodeType(pSecondParam) != QUERY_NODE_VALUE) {
506,300✔
824
      uError("invalid param node type: %d for func: %d", nodeType(pSecondParam), funcId);
×
825
      return TSDB_CODE_INTERNAL_ERROR;
×
826
    }
827
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
505,897✔
828
    int32_t idx = ((SValueNode*)pSecondParam)->datum.i;
506,300✔
829
    if (idx - 1 < 0 || idx - 1 >= taosArrayGetSize(pVal)) {
505,897✔
830
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
×
831
      return TSDB_CODE_INTERNAL_ERROR;
×
832
    }
833
    SStreamGroupValue* pValue = taosArrayGet(pVal, idx - 1);
505,897✔
834
    if (pValue == NULL) {
506,300✔
835
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
×
836
      return TSDB_CODE_INTERNAL_ERROR;
×
837
    }
838
    if (!pValue->isNull){
506,300✔
839
      if (pValue->data.type != ((SValueNode*)pFirstParam)->node.resType.type){
500,765✔
840
        uError("invalid value type: %d for func: %d, should be: %d", pValue->data.type, funcId, ((SValueNode*)pFirstParam)->node.resType.type);
×
841
        return TSDB_CODE_INTERNAL_ERROR;
×
842
      }
843
      if (IS_VAR_DATA_TYPE(((SValueNode*)pFirstParam)->node.resType.type)) {
500,182✔
844
        char* tmp = taosMemoryCalloc(1, pValue->data.nData + VARSTR_HEADER_SIZE); 
389,689✔
845
        if (tmp == NULL) {
390,272✔
846
          return TSDB_CODE_OUT_OF_MEMORY;
×
847
        }
848
        taosMemoryFree(((SValueNode*)pFirstParam)->datum.p);
390,272✔
849
        ((SValueNode*)pFirstParam)->datum.p = tmp;
389,869✔
850
        memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
389,869✔
851
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
389,869✔
852
      } else {
853
        code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&pValue->data, pValue->data.type));
110,493✔
854
      }
855
    }
856
    ((SValueNode*)pFirstParam)->isNull = pValue->isNull;
505,897✔
857
  } else if (FUNCTION_TYPE_PLACEHOLDER_TBNAME == t) {
35,769,666✔
858
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
1,595,778✔
859
    for (int32_t i = 0; i < taosArrayGetSize(pVal); ++i) {
1,596,498✔
860
      SStreamGroupValue* pValue = taosArrayGet(pVal, i);
1,596,020✔
861
      if (pValue != NULL && pValue->isTbname) {
1,595,781✔
862
        taosMemoryFreeClear(((SValueNode*)pFirstParam)->datum.p);
1,596,498✔
863
        ((SValueNode*)pFirstParam)->datum.p = taosMemoryCalloc(pValue->data.nData + VARSTR_HEADER_SIZE, 1);
1,595,781✔
864
        if (NULL == ((SValueNode*)pFirstParam)->datum.p ) {
1,596,497✔
865
          return terrno;
×
866
        }
867

868
        (void)memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
1,596,019✔
869
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
1,596,258✔
870
        break;
1,596,019✔
871
      }
872
    }
873
  } else if (LIST_LENGTH(pParamNodes) == 1) {
68,342,817✔
874
    // twstart, twend
875
    const void* pVal = fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
34,172,028✔
876
    if (!pVal) {
34,171,098✔
877
      uError("failed to set stream pseudo func param val, NULL val for funcId: %d", funcId);
×
878
      return TSDB_CODE_INTERNAL_ERROR;
×
879
    }
880
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, (void*)pVal);
34,171,098✔
881
    if (code != 0) {
34,169,140✔
882
      uError("failed to set value node value: %s", tstrerror(code));
211✔
883
      return code;
×
884
    }
885
  } else {
886
    uError("invalid placeholder function type: %d", t);
4,067✔
887
    return TSDB_CODE_INTERNAL_ERROR;
×
888
  }
889
  
890
  return code;
36,381,691✔
891
}
892

893

894

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