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

taosdata / TDengine / #4832

31 Oct 2025 03:37AM UTC coverage: 58.59% (-0.2%) from 58.771%
#4832

push

travis-ci

SallyHuo-TAOS
Merge remote-tracking branch 'origin/cover/3.0' into cover/3.0

# Conflicts:
#	test/ci/run.sh

149363 of 324176 branches covered (46.07%)

Branch coverage included in aggregate %.

198471 of 269498 relevant lines covered (73.64%)

239014265.14 hits per line

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

63.99
/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() {
9,645,103✔
35
  gFunMgtService.pFuncNameHashTable =
9,645,103✔
36
      taosHashInit(funcMgtBuiltinsNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
9,645,103✔
37
  if (NULL == gFunMgtService.pFuncNameHashTable) {
9,645,103!
38
    initFunctionCode = terrno;
×
39
    return;
×
40
  }
41

42
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
2,006,181,424✔
43
    if (TSDB_CODE_SUCCESS != taosHashPut(gFunMgtService.pFuncNameHashTable, funcMgtBuiltins[i].name,
1,996,536,321!
44
                                         strlen(funcMgtBuiltins[i].name), &i, sizeof(int32_t))) {
1,996,536,321!
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);
15,191,206✔
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() {
9,645,103✔
64
  (void)taosThreadOnce(&functionHashTableInit, doInitFunctionTable);
9,645,103✔
65
  return initFunctionCode;
9,645,103✔
66
}
67

68
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
2,147,483,647✔
69
  if (NULL != gFunMgtService.pFuncNameHashTable) {
2,147,483,647✔
70
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
2,147,483,647!
71
    if (NULL != pVal) {
2,147,483,647✔
72
      pFunc->funcId = *(int32_t*)pVal;
2,147,483,647✔
73
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
2,147,483,647✔
74
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
2,147,483,647✔
75
    }
76
    return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
340,160✔
77
  }
78
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
6,512,975✔
79
    if (0 == strcmp(funcMgtBuiltins[i].name, pFunc->functionName)) {
6,513,386!
80
      pFunc->funcId = i;
347,468✔
81
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
347,468✔
82
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
347,468✔
83
    }
84
  }
85
  return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
300✔
86
}
87

88
EFuncReturnRows fmGetFuncReturnRows(SFunctionNode* pFunc) {
71,327,095✔
89
  if (NULL != funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc) {
71,327,095✔
90
    return funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc(pFunc);
47,044,971✔
91
  }
92
  return (fmIsIndefiniteRowsFunc(pFunc->funcId) || fmIsMultiRowsFunc(pFunc->funcId)) ? FUNC_RETURN_ROWS_INDEFINITE
24,281,413!
93
                                                                                     : FUNC_RETURN_ROWS_NORMAL;
24,281,413!
94
}
95

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

100
EFunctionType fmGetFuncType(const char* pFunc) {
1,620,999,262✔
101
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
1,620,999,262!
102
  if (NULL != pVal) {
1,621,006,102✔
103
    return funcMgtBuiltins[*(int32_t*)pVal].type;
1,620,660,267✔
104
  }
105
  return FUNCTION_TYPE_UDF;
345,835✔
106
}
107

108
EFunctionType fmGetFuncTypeFromId(int32_t funcId) {
9,025,770✔
109
  if (funcId < funcMgtBuiltinsNum) {
9,025,770!
110
    return funcMgtBuiltins[funcId].type;
9,025,770✔
111
  }
112
  return FUNCTION_TYPE_UDF;
×
113
}
114

115
EFuncDataRequired fmFuncDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
106,397,058✔
116
  if (fmIsUserDefinedFunc(pFunc->funcId) || pFunc->funcId < 0 || pFunc->funcId >= funcMgtBuiltinsNum) {
106,397,058!
117
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
118
  }
119
  if (NULL == funcMgtBuiltins[pFunc->funcId].dataRequiredFunc) {
106,397,058!
120
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
121
  }
122
  return funcMgtBuiltins[pFunc->funcId].dataRequiredFunc(pFunc, pTimeWindow);
106,388,945✔
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;
×
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;
57,561✔
133
    ;
134
  }
135

136
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
2,147,483,647✔
137
    return FUNC_DATA_REQUIRED_DATA_LOAD;
169,847,861✔
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) {
1,083,038,990✔
144
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,083,038,990!
145
    return TSDB_CODE_FAILED;
10,631✔
146
  }
147
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
1,083,067,910✔
148
  pFpSet->init = funcMgtBuiltins[funcId].initFunc;
1,082,860,753✔
149
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
1,082,985,727✔
150
  pFpSet->finalize = funcMgtBuiltins[funcId].finalizeFunc;
1,082,859,139✔
151
  pFpSet->combine = funcMgtBuiltins[funcId].combineFunc;
1,082,791,295✔
152
  pFpSet->processFuncByRow = funcMgtBuiltins[funcId].processFuncByRow;
1,082,987,296✔
153
  pFpSet->cleanup = funcMgtBuiltins[funcId].cleanupFunc;
1,082,842,738✔
154
  return TSDB_CODE_SUCCESS;
1,082,939,180✔
155
}
156

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

172
int32_t fmGetScalarFuncExecFuncs(int32_t funcId, SScalarFuncExecFuncs* pFpSet) {
2,147,483,647✔
173
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,147,483,647!
174
    return TSDB_CODE_FAILED;
151,421✔
175
  }
176
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
2,147,483,647✔
177
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
2,147,483,647✔
178
  return TSDB_CODE_SUCCESS;
2,147,483,647✔
179
}
180

181
bool fmIsAggFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_AGG_FUNC); }
2,147,483,647✔
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); }
2,147,483,647✔
186

187
bool fmIsSelectColsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_COLS_FUNC); }
204,075,308✔
188

189
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
2,147,483,647✔
190

191
bool fmIsTimelineFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TIMELINE_FUNC); }
2,147,483,647✔
192

193
bool fmIsDateTimeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_DATETIME_FUNC); }
1,585,268,867✔
194

195
bool fmIsPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PSEUDO_COLUMN_FUNC); }
2,147,483,647✔
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); }
2,147,483,647✔
200

201
bool fmIsWindowClauseFunc(int32_t funcId) { return fmIsAggFunc(funcId) || fmIsWindowPseudoColumnFunc(funcId); }
76,047,657✔
202

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

205
bool fmIsStreamVectorFunc(int32_t funcId) { return fmIsVectorFunc(funcId) || fmIsPlaceHolderFunc(funcId); }
13,446!
206

207
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
2,147,483,647✔
208

209
bool fmIsSpecialDataRequiredFunc(int32_t funcId) {
279,638,610✔
210
  return isSpecificClassifyFunc(funcId, FUNC_MGT_SPECIAL_DATA_REQUIRED);
279,638,610✔
211
}
212

213
bool fmIsDynamicScanOptimizedFunc(int32_t funcId) {
140,859,093✔
214
  return isSpecificClassifyFunc(funcId, FUNC_MGT_DYNAMIC_SCAN_OPTIMIZED);
140,859,093✔
215
}
216

217
bool fmIsMultiResFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_RES_FUNC); }
2,147,483,647✔
218

219
bool fmIsRepeatScanFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_REPEAT_SCAN_FUNC); }
2,147,483,647✔
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); }
1,586,478,232✔
224

225
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
272,651,970✔
226

227
bool fmIsSystemInfoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SYSTEM_INFO_FUNC); }
1,601,328,659✔
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); }
1,599,806,399✔
232

233
bool fmIsMultiRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_ROWS_FUNC); }
2,147,483,647✔
234

235
bool fmIsKeepOrderFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_KEEP_ORDER_FUNC); }
698,635,875✔
236

237
bool fmIsCumulativeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CUMULATIVE_FUNC); }
213,599,182✔
238

239
bool fmIsForbidSysTableFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_SYSTABLE_FUNC); }
1,586,153,588✔
240

241
bool fmIsInterpFunc(int32_t funcId) {
2,147,483,647✔
242
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,147,483,647!
243
    return false;
649,573✔
244
  }
245
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
2,147,483,647✔
246
}
247

248
bool fmIsInterpPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERP_PC_FUNC); }
2,147,483,647✔
249

250
bool fmIsForecastFunc(int32_t funcId) {
2,147,483,647✔
251
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,147,483,647!
252
    return false;
656,471✔
253
  }
254
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
2,147,483,647✔
255
}
256

257
bool fmIsAnalysisPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_ANALYTICS_PC_FUNC); }
2,147,483,647✔
258

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

264
  return FUNCTION_TYPE_IMPUTATION == funcMgtBuiltins[funcId].type;
×
265
}
266

267
bool fmIsLastRowFunc(int32_t funcId) {
114,544,288✔
268
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
114,544,288!
269
    return false;
100✔
270
  }
271
  return FUNCTION_TYPE_LAST_ROW == funcMgtBuiltins[funcId].type;
114,545,317✔
272
}
273

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

281
bool fmIsNotNullOutputFunc(int32_t funcId) {
1,710,601,096✔
282
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,710,601,096!
283
    return false;
167,706✔
284
  }
285
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
1,710,483,126✔
286
         FUNCTION_TYPE_CACHE_LAST == funcMgtBuiltins[funcId].type ||
1,655,714,523✔
287
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
1,653,753,790✔
288
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
1,629,827,824✔
289
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
1,619,378,904✔
290
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
1,595,254,369✔
291
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
1,582,054,730✔
292
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
1,577,066,322✔
293
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
1,292,455,846✔
294
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
2,147,483,647✔
295
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
1,268,435,483✔
296
}
297

298
bool fmIsSelectValueFunc(int32_t funcId) {
385,950,896✔
299
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
385,950,896!
300
    return false;
×
301
  }
302
  return FUNCTION_TYPE_SELECT_VALUE == funcMgtBuiltins[funcId].type;
385,950,896✔
303
}
304

305
bool fmIsGroupKeyFunc(int32_t funcId) {
520,790,377✔
306
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
520,790,377!
307
    return false;
815✔
308
  }
309
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
520,790,772✔
310
}
311

312
bool fmisSelectGroupConstValueFunc(int32_t funcId) {
39,492✔
313
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
39,492!
314
    return false;
×
315
  }
316
  return FUNCTION_TYPE_GROUP_CONST_VALUE == funcMgtBuiltins[funcId].type;
39,492✔
317
}
318

319
bool fmIsElapsedFunc(int32_t funcId) {
27,907,862✔
320
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
27,907,862!
321
    return false;
×
322
  }
323
  return FUNCTION_TYPE_ELAPSED == funcMgtBuiltins[funcId].type;
27,907,862✔
324
}
325

326
bool fmIsBlockDistFunc(int32_t funcId) {
1,585,269,935✔
327
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,585,269,935✔
328
    return false;
332,944✔
329
  }
330
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
1,584,936,991✔
331
}
332

333
bool fmIsDBUsageFunc(int32_t funcId) {
1,585,268,000✔
334
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,585,268,000✔
335
    return false;
333,444✔
336
  }
337
  return FUNCTION_TYPE_DB_USAGE == funcMgtBuiltins[funcId].type;
1,584,934,642✔
338
}
339

340
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
1,794,310,346✔
341

342
bool fmIsIgnoreNullFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IGNORE_NULL_FUNC); }
1,612,785✔
343

344
void fmFuncMgtDestroy() {
9,646,305✔
345
  void* m = gFunMgtService.pFuncNameHashTable;
9,646,305✔
346
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
9,646,305!
347
    taosHashCleanup(m);
9,645,103✔
348
  }
349
}
9,646,305✔
350

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

361
// function has same input/output type
362
bool fmIsSameInOutType(int32_t funcId) {
93,799,639✔
363
  bool res = false;
93,799,639✔
364
  switch (funcMgtBuiltins[funcId].type) {
93,799,639✔
365
    case FUNCTION_TYPE_MAX:
22,151,442✔
366
    case FUNCTION_TYPE_MIN:
367
    case FUNCTION_TYPE_TOP:
368
    case FUNCTION_TYPE_BOTTOM:
369
    case FUNCTION_TYPE_FIRST:
370
    case FUNCTION_TYPE_LAST:
371
    case FUNCTION_TYPE_SAMPLE:
372
    case FUNCTION_TYPE_TAIL:
373
    case FUNCTION_TYPE_UNIQUE:
374
      res = true;
22,151,442✔
375
      break;
22,151,442✔
376
    default:
71,648,197✔
377
      break;
71,648,197✔
378
  }
379
  return res;
93,799,639✔
380
}
381

382
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
46,772,986✔
383
  SNode* pNode;
384
  FOREACH(pNode, pFunc->pParameterList) {
47,311,810✔
385
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
573,027✔
386
      return false;
34,203✔
387
    }
388
  }
389
  return true;
46,738,783✔
390
}
391

392
bool fmIsSkipScanCheckFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SKIP_SCAN_CHECK_FUNC); }
140,860,266✔
393

394
bool fmIsPrimaryKeyFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PRIMARY_KEY_FUNC); }
1,452,736,837✔
395

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

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

404
static int32_t getFuncInfo(SFunctionNode* pFunc) {
330,617,645✔
405
  char msg[128] = {0};
330,617,645✔
406
  return fmGetFuncInfo(pFunc, msg, sizeof(msg));
330,617,645✔
407
}
408

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

426
static void resetOutputChangedFunc(SFunctionNode *pFunc, const SFunctionNode* pSrcFunc) {
281,398,917✔
427
  if (funcMgtBuiltins[pFunc->funcId].type == FUNCTION_TYPE_LAST_MERGE) {
281,398,917✔
428
    pFunc->node.resType = pSrcFunc->node.resType;
46,704,400✔
429
    return;
46,704,400✔
430
  }
431
}
432

433
int32_t createFunctionWithSrcFunc(const char* pName, const SFunctionNode* pSrcFunc, SNodeList* pParameterList, SFunctionNode** ppFunc) {
281,398,917✔
434
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
281,398,917✔
435
  if (NULL == *ppFunc) {
281,398,917!
436
    return code;
×
437
  }
438

439
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
281,398,917!
440
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
281,398,917✔
441
  (*ppFunc)->pSrcFuncRef = pSrcFunc;
281,398,917✔
442

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

458
static int32_t createColumnByFunc(const SFunctionNode* pFunc, SColumnNode** ppCol) {
187,599,278✔
459
  int32_t code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)ppCol);
187,599,278✔
460
  if (NULL == *ppCol) {
187,599,278!
461
    return code;
×
462
  }
463
  tstrncpy((*ppCol)->colName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
187,599,278!
464
  (*ppCol)->node.resType = pFunc->node.resType;
187,599,278✔
465
  return TSDB_CODE_SUCCESS;
187,599,278✔
466
}
467

468
bool fmIsDistExecFunc(int32_t funcId) {
798,496,780✔
469
  if (fmIsUserDefinedFunc(funcId)) {
798,496,780✔
470
    return false;
166,249✔
471
  }
472
  if (!fmIsVectorFunc(funcId)) {
798,329,731✔
473
    return true;
424,891✔
474
  }
475
  return (NULL != funcMgtBuiltins[funcId].pPartialFunc && NULL != funcMgtBuiltins[funcId].pMergeFunc);
797,905,060!
476
}
477

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

494
  int32_t len = tsnprintf(name, sizeof(name), "%s.%p", (*pPartialFunc)->functionName, pSrcFunc);
93,799,639✔
495
  if (taosHashBinary(name, len) < 0) {
93,799,639!
496
    return TSDB_CODE_FAILED;
×
497
  }
498
  tstrncpy((*pPartialFunc)->node.aliasName, name, TSDB_COL_NAME_LEN);
93,799,639!
499
  return TSDB_CODE_SUCCESS;
93,799,639✔
500
}
501

502
static int32_t createMergeFuncPara(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
187,599,278✔
503
                                   SNodeList** pParameterList) {
504
  SNode*  pRes = NULL;
187,599,278✔
505
  int32_t code = createColumnByFunc(pPartialFunc, (SColumnNode**)&pRes);
187,599,278✔
506
  if (TSDB_CODE_SUCCESS != code) {
187,599,278!
507
    return code;
×
508
  }
509
  if (NULL != funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc) {
187,599,278✔
510
    return funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc(pSrcFunc->pParameterList, pRes, pParameterList);
1,195,598✔
511
  } else {
512
    return nodesListMakeStrictAppend(pParameterList, pRes);
186,403,680✔
513
  }
514
}
515

516
static int32_t createMidFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
93,799,639✔
517
                                 SFunctionNode** pMidFunc) {
518
  SNodeList*     pParameterList = NULL;
93,799,639✔
519
  SFunctionNode* pFunc = NULL;
93,799,639✔
520

521
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
93,799,639✔
522
  if (TSDB_CODE_SUCCESS == code) {
93,799,639!
523
    if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){
93,799,639✔
524
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pSrcFunc, pParameterList, &pFunc);
5,079,020✔
525
    }else{
526
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
88,720,619✔
527
    }
528
  }
529
  if (TSDB_CODE_SUCCESS == code) {
93,799,639!
530
    tstrncpy(pFunc->node.aliasName, pPartialFunc->node.aliasName, TSDB_COL_NAME_LEN);
93,799,639!
531
  }
532

533
  if (TSDB_CODE_SUCCESS == code) {
93,799,639!
534
    *pMidFunc = pFunc;
93,799,639✔
535
  } else {
536
    nodesDestroyList(pParameterList);
×
537
  }
538
  return code;
93,799,639✔
539
}
540

541
static int32_t createMergeFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
93,799,639✔
542
                                   SFunctionNode** pMergeFunc) {
543
  SNodeList*     pParameterList = NULL;
93,799,639✔
544
  SFunctionNode* pFunc = NULL;
93,799,639✔
545

546
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
93,799,639✔
547
  if (TSDB_CODE_SUCCESS == code) {
93,799,639!
548
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
93,799,639✔
549
  }
550
  if (TSDB_CODE_SUCCESS == code) {
93,799,639!
551
    pFunc->hasOriginalFunc = true;
93,799,639✔
552
    pFunc->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
93,799,639!
553
    // overwrite function restype set by translate function
554
    if (fmIsSameInOutType(pSrcFunc->funcId)) {
93,799,639✔
555
      pFunc->node.resType = pSrcFunc->node.resType;
22,151,442✔
556
    }
557
    tstrncpy(pFunc->node.aliasName, pSrcFunc->node.aliasName, TSDB_COL_NAME_LEN);
93,799,639!
558
  }
559

560
  if (TSDB_CODE_SUCCESS == code) {
93,799,639!
561
    *pMergeFunc = pFunc;
93,799,639✔
562
  } else {
563
    nodesDestroyList(pParameterList);
×
564
  }
565
  return code;
93,799,639✔
566
}
567

568
int32_t fmGetDistMethod(const SFunctionNode* pFunc, SFunctionNode** pPartialFunc, SFunctionNode** pMidFunc,
93,799,639✔
569
                        SFunctionNode** pMergeFunc) {
570
  if (!fmIsDistExecFunc(pFunc->funcId)) {
93,799,639!
571
    return TSDB_CODE_FAILED;
×
572
  }
573

574
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
93,799,639✔
575
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
93,799,639!
576
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
93,799,639✔
577
  }
578
  if (TSDB_CODE_SUCCESS == code) {
93,799,639!
579
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
93,799,639✔
580
  }
581

582
  if (TSDB_CODE_SUCCESS != code) {
93,799,639!
583
    nodesDestroyNode((SNode*)*pPartialFunc);
×
584
    if (pMidFunc) nodesDestroyNode((SNode*)*pMidFunc);
×
585
    nodesDestroyNode((SNode*)*pMergeFunc);
×
586
  }
587

588
  return code;
93,799,639✔
589
}
590

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

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

616
bool fmIsTSMASupportedFunc(func_id_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC); }
911,760✔
617

618
bool fmIsRsmaSupportedFunc(func_id_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_RSMA_FUNC); }
258,962✔
619

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

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

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

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

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

717
bool fmIsCountLikeFunc(int32_t funcId) {
1,561,071,914✔
718
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
1,561,071,914✔
719
}
720

721
bool fmIsRowTsOriginFunc(int32_t funcId) {
14,807,448✔
722
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
14,807,448!
723
    return false;
×
724
  }
725
  return FUNCTION_TYPE_IROWTS_ORIGIN == funcMgtBuiltins[funcId].type;
14,807,448✔
726
}
727

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

735
const void* fmGetStreamPesudoFuncVal(int32_t funcId, const SStreamRuntimeFuncInfo* pStreamRuntimeFuncInfo) {
70,136,210✔
736
  SSTriggerCalcParam *pParams = taosArrayGet(pStreamRuntimeFuncInfo->pStreamPesudoFuncVals, pStreamRuntimeFuncInfo->curIdx);
70,136,210✔
737
  switch (funcMgtBuiltins[funcId].type) {
70,151,923!
738
    case FUNCTION_TYPE_TPREV_TS:
×
739
      return &pParams->prevTs;
×
740
    case FUNCTION_TYPE_TCURRENT_TS:
13,908,172✔
741
      return &pParams->currentTs;
13,908,172✔
742
    case FUNCTION_TYPE_TNEXT_TS:
×
743
      return &pParams->nextTs;
×
744
    case FUNCTION_TYPE_TWSTART:
40,168,750✔
745
      return &pParams->wstart;
40,168,750✔
746
    case FUNCTION_TYPE_TWEND:
810,986✔
747
      return &pParams->wend;
810,986✔
748
    case FUNCTION_TYPE_TWDURATION:
51,844✔
749
      return &pParams->wduration;
51,844✔
750
    case FUNCTION_TYPE_TWROWNUM:
266,884✔
751
      return &pParams->wrownum;
266,884✔
752
    case FUNCTION_TYPE_TPREV_LOCALTIME:
105,585✔
753
      return &pParams->prevLocalTime;
105,585✔
754
    case FUNCTION_TYPE_TLOCALTIME:
475,008✔
755
      return &pParams->triggerTime;
475,008✔
756
    case FUNCTION_TYPE_TNEXT_LOCALTIME:
161,553✔
757
      return &pParams->nextLocalTime;
161,553✔
758
    case FUNCTION_TYPE_TGRPID:
387,092✔
759
      return &pStreamRuntimeFuncInfo->groupId;
387,092✔
760
    case FUNCTION_TYPE_PLACEHOLDER_COLUMN:
2,156,464✔
761
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
2,156,464✔
762
    case FUNCTION_TYPE_PLACEHOLDER_TBNAME:
11,650,496✔
763
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
11,650,496✔
764
    default:
×
765
      break;
×
766
  }
767
  return NULL;
×
768
}
769

770
int32_t fmGetStreamPesudoFuncEnv(int32_t funcId, SNodeList* pParamNodes, SFuncExecEnv *pEnv) {
36,639,162✔
771
  if (NULL == pParamNodes || pParamNodes->length < 1) {
36,639,162!
772
    uError("invalid stream pesudo func param list %p", pParamNodes);
×
773
    return TSDB_CODE_INTERNAL_ERROR;
×
774
  }
775

776
  int32_t firstParamType = nodeType(nodesListGetNode(pParamNodes, 0));
36,640,436✔
777
  if (QUERY_NODE_VALUE != firstParamType) {
36,640,438!
778
    uError("invalid stream pesudo func first param type %d", firstParamType);
×
779
    return TSDB_CODE_INTERNAL_ERROR;
×
780
  }
781

782
  SValueNode* pResDesc = (SValueNode*)nodesListGetNode(pParamNodes, 0);
36,640,438✔
783
  pEnv->calcMemSize = pResDesc->node.resType.bytes;
36,640,443✔
784

785
  return TSDB_CODE_SUCCESS;
36,641,688✔
786
}
787

788

789
int32_t fmSetStreamPseudoFuncParamVal(int32_t funcId, SNodeList* pParamNodes, const SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
70,140,147✔
790
  if (!pStreamRuntimeInfo) {
70,140,147!
791
    uError("internal error, should have pVals for stream pseudo funcs");
×
792
    return TSDB_CODE_INTERNAL_ERROR;
×
793
  }
794
  int32_t code = 0;
70,140,147✔
795
  SArray *pVals1 = NULL;
70,140,147✔
796
  SNode* pFirstParam = nodesListGetNode(pParamNodes, 0);
70,140,147✔
797
  if (nodeType(pFirstParam) != QUERY_NODE_VALUE) {
70,144,007!
798
    uError("invalid param node type: %d for func: %d", nodeType(pFirstParam), funcId);
×
799
    return TSDB_CODE_INTERNAL_ERROR;
×
800
  }
801

802
  int32_t t = funcMgtBuiltins[funcId].type;
70,144,076✔
803
  uDebug("set stream pseudo func param val, functype: %d, pStreamRuntimeInfo: %p", t, pStreamRuntimeInfo);
70,138,836✔
804
  if (FUNCTION_TYPE_TGRPID == t) {
70,134,849✔
805
    SValue v = {0};
385,722✔
806
    v.type = TSDB_DATA_TYPE_BIGINT;
385,722✔
807
    v.val = *(int64_t*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
385,722✔
808
    
809
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&v, pFirstParam->type));
387,092!
810
    if (code != 0) {
387,092✔
811
      uError("failed to set value node value: %s", tstrerror(code));
1,370!
812
      return code;
×
813
    }
814
  } else if (FUNCTION_TYPE_PLACEHOLDER_COLUMN == t) {
69,749,127✔
815
    SNode* pSecondParam = nodesListGetNode(pParamNodes, 1);
2,157,712✔
816
    if (nodeType(pSecondParam) != QUERY_NODE_VALUE) {
2,156,415!
817
      uError("invalid param node type: %d for func: %d", nodeType(pSecondParam), funcId);
×
818
      return TSDB_CODE_INTERNAL_ERROR;
×
819
    }
820
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
2,156,415✔
821
    int32_t idx = ((SValueNode*)pSecondParam)->datum.i;
2,157,712✔
822
    if (idx - 1 < 0 || idx - 1 >= taosArrayGetSize(pVal)) {
2,157,712!
823
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
1,248!
824
      return TSDB_CODE_INTERNAL_ERROR;
×
825
    }
826
    SStreamGroupValue* pValue = taosArrayGet(pVal, idx - 1);
2,156,464✔
827
    if (pValue == NULL) {
2,156,464!
828
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
×
829
      return TSDB_CODE_INTERNAL_ERROR;
×
830
    }
831
    if (!pValue->isNull){
2,156,464!
832
      if (pValue->data.type != ((SValueNode*)pFirstParam)->node.resType.type){
2,157,712!
833
        uError("invalid value type: %d for func: %d, should be: %d", pValue->data.type, funcId, ((SValueNode*)pFirstParam)->node.resType.type);
×
834
        return TSDB_CODE_INTERNAL_ERROR;
×
835
      }
836
      if (IS_VAR_DATA_TYPE(((SValueNode*)pFirstParam)->node.resType.type)) {
2,156,464!
837
        taosMemoryFree(((SValueNode*)pFirstParam)->datum.p);
1,607,598!
838
        ((SValueNode*)pFirstParam)->datum.p = taosMemoryCalloc(1, pValue->data.nData + VARSTR_HEADER_SIZE); 
1,602,459!
839
        memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
1,603,707!
840
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
1,603,707✔
841
      } else {
842
        code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&pValue->data, pValue->data.type));
552,708!
843
      }
844
    }
845
    ((SValueNode*)pFirstParam)->isNull = pValue->isNull;
2,156,415!
846
  } else if (FUNCTION_TYPE_PLACEHOLDER_TBNAME == t) {
67,591,415✔
847
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
11,641,340✔
848
    for (int32_t i = 0; i < taosArrayGetSize(pVal); ++i) {
11,651,817!
849
      SStreamGroupValue* pValue = taosArrayGet(pVal, i);
11,655,718✔
850
      if (pValue != NULL && pValue->isTbname) {
11,655,718!
851
        taosMemoryFreeClear(((SValueNode*)pFirstParam)->datum.p);
11,657,029!
852
        ((SValueNode*)pFirstParam)->datum.p = taosMemoryCalloc(pValue->data.nData + VARSTR_HEADER_SIZE, 1);
11,653,108!
853
        if (NULL == ((SValueNode*)pFirstParam)->datum.p ) {
11,650,488!
854
          return terrno;
×
855
        }
856

857
        (void)memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
11,643,952!
858
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
11,651,801✔
859
        break;
11,658,340✔
860
      }
861
    }
862
  } else if (LIST_LENGTH(pParamNodes) == 1) {
111,900,194!
863
    // twstart, twend
864
    const void* pVal = fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
55,953,944✔
865
    if (!pVal) {
55,951,400!
866
      uError("failed to set stream pseudo func param val, NULL val for funcId: %d", funcId);
×
867
      return TSDB_CODE_INTERNAL_ERROR;
×
868
    }
869
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, (void*)pVal);
55,951,400✔
870
    if (code != 0) {
55,950,119!
871
      uError("failed to set value node value: %s", tstrerror(code));
×
872
      return code;
×
873
    }
874
  } else {
875
    uError("invalid placeholder function type: %d", t);
×
876
    return TSDB_CODE_INTERNAL_ERROR;
×
877
  }
878
  
879
  return code;
70,137,613✔
880
}
881

882

883

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