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

taosdata / TDengine / #4759

25 Sep 2025 05:58AM UTC coverage: 58.866% (+0.03%) from 58.832%
#4759

push

travis-ci

web-flow
enh: taos command line support '-uroot' on windows (#33055)

135700 of 293169 branches covered (46.29%)

Branch coverage included in aggregate %.

204479 of 284720 relevant lines covered (71.82%)

25399510.59 hits per line

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

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

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

51
static bool isSpecificClassifyFunc(int32_t funcId, uint64_t classification) {
1,889,553,164✔
52
  if (fmIsUserDefinedFunc(funcId)) {
1,889,553,164✔
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);
5,483✔
56
  }
57
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,889,284,530!
58
    return false;
204,246,851✔
59
  }
60
  return FUNC_MGT_TEST_MASK(funcMgtBuiltins[funcId].classification, classification);
1,685,037,679✔
61
}
62

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

68
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
664,458✔
69
  if (NULL != gFunMgtService.pFuncNameHashTable) {
664,458!
70
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
664,508✔
71
    if (NULL != pVal) {
664,540✔
72
      pFunc->funcId = *(int32_t*)pVal;
664,488✔
73
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
664,488✔
74
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
664,488✔
75
    }
76
    return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
52✔
77
  }
78
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
×
79
    if (0 == strcmp(funcMgtBuiltins[i].name, pFunc->functionName)) {
×
80
      pFunc->funcId = i;
×
81
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
×
82
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
×
83
    }
84
  }
85
  return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
×
86
}
87

88
EFuncReturnRows fmGetFuncReturnRows(SFunctionNode* pFunc) {
1,243✔
89
  if (NULL != funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc) {
1,243✔
90
    return funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc(pFunc);
885✔
91
  }
92
  return (fmIsIndefiniteRowsFunc(pFunc->funcId) || fmIsMultiRowsFunc(pFunc->funcId)) ? FUNC_RETURN_ROWS_INDEFINITE
358!
93
                                                                                     : FUNC_RETURN_ROWS_NORMAL;
358!
94
}
95

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

100
EFunctionType fmGetFuncType(const char* pFunc) {
264,491✔
101
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
264,491✔
102
  if (NULL != pVal) {
264,475✔
103
    return funcMgtBuiltins[*(int32_t*)pVal].type;
264,435✔
104
  }
105
  return FUNCTION_TYPE_UDF;
40✔
106
}
107

108
EFuncDataRequired fmFuncDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
216,227✔
109
  if (fmIsUserDefinedFunc(pFunc->funcId) || pFunc->funcId < 0 || pFunc->funcId >= funcMgtBuiltinsNum) {
216,227!
110
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
111
  }
112
  if (NULL == funcMgtBuiltins[pFunc->funcId].dataRequiredFunc) {
216,343!
113
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
114
  }
115
  return funcMgtBuiltins[pFunc->funcId].dataRequiredFunc(pFunc, pTimeWindow);
216,343✔
116
}
117

118
EFuncDataRequired fmFuncDynDataRequired(int32_t funcId, void* pRes, SDataBlockInfo* pBlockInfo) {
4,159,107✔
119
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
4,159,107!
120
    return FUNC_DATA_REQUIRED_DATA_LOAD;
4✔
121
  }
122

123
  const char* name = funcMgtBuiltins[funcId].name;
4,159,108✔
124
  if ((strcmp(name, "_group_key") == 0) || (strcmp(name, "_select_value") == 0)) {
4,159,108✔
125
    return FUNC_DATA_REQUIRED_NOT_LOAD;
207,678✔
126
    ;
127
  }
128

129
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
3,951,430✔
130
    return FUNC_DATA_REQUIRED_DATA_LOAD;
3,893,421✔
131
  } else {
132
    return funcMgtBuiltins[funcId].dynDataRequiredFunc(pRes, pBlockInfo);
58,009✔
133
  }
134
}
135

136
int32_t fmGetFuncExecFuncs(int32_t funcId, SFuncExecFuncs* pFpSet) {
11,156,438✔
137
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
11,156,438!
138
    return TSDB_CODE_FAILED;
1,592✔
139
  }
140
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
11,161,529✔
141
  pFpSet->init = funcMgtBuiltins[funcId].initFunc;
11,161,529✔
142
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
11,161,529✔
143
  pFpSet->finalize = funcMgtBuiltins[funcId].finalizeFunc;
11,161,529✔
144
  pFpSet->combine = funcMgtBuiltins[funcId].combineFunc;
11,161,529✔
145
  pFpSet->processFuncByRow = funcMgtBuiltins[funcId].processFuncByRow;
11,161,529✔
146
  pFpSet->cleanup = funcMgtBuiltins[funcId].cleanupFunc;
11,161,529✔
147
  return TSDB_CODE_SUCCESS;
11,161,529✔
148
}
149

150
int32_t fmGetUdafExecFuncs(int32_t funcId, SFuncExecFuncs* pFpSet) {
85✔
151
#ifdef USE_UDF
152
  if (!fmIsUserDefinedFunc(funcId)) {
85!
153
    return TSDB_CODE_FAILED;
×
154
  }
155
  pFpSet->getEnv = udfAggGetEnv;
85✔
156
  pFpSet->init = udfAggInit;
85✔
157
  pFpSet->process = udfAggProcess;
85✔
158
  pFpSet->finalize = udfAggFinalize;
85✔
159
  return TSDB_CODE_SUCCESS;
85✔
160
#else
161
  TAOS_RETURN(TSDB_CODE_OPS_NOT_SUPPORT);
162
#endif
163
}
164

165
int32_t fmGetScalarFuncExecFuncs(int32_t funcId, SScalarFuncExecFuncs* pFpSet) {
14,877,156✔
166
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
14,877,156!
167
    return TSDB_CODE_FAILED;
×
168
  }
169
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
14,879,956✔
170
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
14,879,956✔
171
  return TSDB_CODE_SUCCESS;
14,879,956✔
172
}
173

174
bool fmIsAggFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_AGG_FUNC); }
21,016,450✔
175

176
bool fmIsScalarFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCALAR_FUNC); }
2,908,643✔
177

178
bool fmIsVectorFunc(int32_t funcId) { return !fmIsScalarFunc(funcId) && !fmIsPseudoColumnFunc(funcId); }
861,125✔
179

180
bool fmIsSelectColsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_COLS_FUNC); }
45,901✔
181

182
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
46,076,897✔
183

184
bool fmIsTimelineFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TIMELINE_FUNC); }
852,012✔
185

186
bool fmIsDateTimeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_DATETIME_FUNC); }
584,792✔
187

188
bool fmIsPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PSEUDO_COLUMN_FUNC); }
45,856,455✔
189

190
bool fmIsScanPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCAN_PC_FUNC); }
22,400,946✔
191

192
bool fmIsWindowPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_WINDOW_PC_FUNC); }
17,258,367✔
193

194
bool fmIsWindowClauseFunc(int32_t funcId) { return fmIsAggFunc(funcId) || fmIsWindowPseudoColumnFunc(funcId); }
10,666✔
195

196
bool fmIsStreamWindowClauseFunc(int32_t funcId) { return fmIsWindowClauseFunc(funcId) || fmIsPlaceHolderFunc(funcId); }
896✔
197

198
bool fmIsStreamVectorFunc(int32_t funcId) { return fmIsVectorFunc(funcId) || fmIsPlaceHolderFunc(funcId); }
16!
199

200
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
13,592,010✔
201

202
bool fmIsSpecialDataRequiredFunc(int32_t funcId) {
437,031✔
203
  return isSpecificClassifyFunc(funcId, FUNC_MGT_SPECIAL_DATA_REQUIRED);
437,031✔
204
}
205

206
bool fmIsDynamicScanOptimizedFunc(int32_t funcId) {
114,995✔
207
  return isSpecificClassifyFunc(funcId, FUNC_MGT_DYNAMIC_SCAN_OPTIMIZED);
114,995✔
208
}
209

210
bool fmIsMultiResFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_RES_FUNC); }
900,732✔
211

212
bool fmIsRepeatScanFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_REPEAT_SCAN_FUNC); }
4,167,155✔
213

214
bool fmIsUserDefinedFunc(int32_t funcId) { return funcId > FUNC_UDF_ID_START; }
1,943,160,664✔
215

216
bool fmIsForbidFillFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_FILL_FUNC); }
584,922✔
217

218
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
45,079,277✔
219

220
bool fmIsSystemInfoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SYSTEM_INFO_FUNC); }
585,186✔
221

222
bool fmIsImplicitTsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IMPLICIT_TS_FUNC); }
87,670,263✔
223

224
bool fmIsClientPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CLIENT_PC_FUNC); }
585,172✔
225

226
bool fmIsMultiRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_ROWS_FUNC); }
852,646✔
227

228
bool fmIsKeepOrderFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_KEEP_ORDER_FUNC); }
241,749✔
229

230
bool fmIsCumulativeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CUMULATIVE_FUNC); }
46,175✔
231

232
bool fmIsForbidSysTableFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_SYSTABLE_FUNC); }
584,855✔
233

234
bool fmIsInterpFunc(int32_t funcId) {
1,167,700✔
235
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,167,700!
236
    return false;
42✔
237
  }
238
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
1,167,658✔
239
}
240

241
bool fmIsInterpPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERP_PC_FUNC); }
852,879✔
242

243
bool fmIsForecastFunc(int32_t funcId) {
851,478✔
244
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
851,478!
245
    return false;
53✔
246
  }
247
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
851,425✔
248
}
249

250
bool fmIsForecastPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORECAST_PC_FUNC); }
852,734✔
251

252
bool fmIsImputationFunc(int32_t funcId) {
×
253
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
254
    return false;
×
255
  }
256

257
  return FUNCTION_TYPE_IMPUTATION == funcMgtBuiltins[funcId].type;
×
258
}
259

260
bool fmIsLastRowFunc(int32_t funcId) {
44,077✔
261
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
44,077!
262
    return false;
×
263
  }
264
  return FUNCTION_TYPE_LAST_ROW == funcMgtBuiltins[funcId].type;
44,081✔
265
}
266

267
bool fmIsLastFunc(int32_t funcId) {
10✔
268
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
10!
269
    return false;
×
270
  }
271
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type;
10✔
272
}
273

274
bool fmIsNotNullOutputFunc(int32_t funcId) {
16,366,325✔
275
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
16,366,325!
276
    return false;
×
277
  }
278
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
32,394,112✔
279
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
16,021,994✔
280
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
14,806,229✔
281
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
14,202,558✔
282
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
13,928,401✔
283
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
13,517,337✔
284
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
13,310,599✔
285
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
10,018,041✔
286
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
42,375,444✔
287
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
9,981,332✔
288
}
289

290
bool fmIsSelectValueFunc(int32_t funcId) {
930,468✔
291
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
930,468!
292
    return false;
×
293
  }
294
  return FUNCTION_TYPE_SELECT_VALUE == funcMgtBuiltins[funcId].type;
930,468✔
295
}
296

297
bool fmIsGroupKeyFunc(int32_t funcId) {
407,738,362✔
298
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
407,738,362!
299
    return false;
×
300
  }
301
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
407,748,851✔
302
}
303

304
bool fmisSelectGroupConstValueFunc(int32_t funcId) {
10✔
305
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
10!
306
    return false;
×
307
  }
308
  return FUNCTION_TYPE_GROUP_CONST_VALUE == funcMgtBuiltins[funcId].type;
10✔
309
}
310

311
bool fmIsElapsedFunc(int32_t funcId) {
26,712,626✔
312
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
26,712,626!
313
    return false;
×
314
  }
315
  return FUNCTION_TYPE_ELAPSED == funcMgtBuiltins[funcId].type;
26,713,325✔
316
}
317

318
bool fmIsBlockDistFunc(int32_t funcId) {
584,752✔
319
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
584,752!
320
    return false;
31✔
321
  }
322
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
584,721✔
323
}
324

325
bool fmIsDBUsageFunc(int32_t funcId) {
584,802✔
326
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
584,802!
327
    return false;
×
328
  }
329
  return FUNCTION_TYPE_DB_USAGE == funcMgtBuiltins[funcId].type;
584,820✔
330
}
331

332
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
4,142,303✔
333

334
bool fmIsIgnoreNullFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IGNORE_NULL_FUNC); }
12✔
335

336
void fmFuncMgtDestroy() {
3,775✔
337
  void* m = gFunMgtService.pFuncNameHashTable;
3,775✔
338
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
3,775!
339
    taosHashCleanup(m);
3,774✔
340
  }
341
}
3,775✔
342

343
#ifdef BUILD_NO_CALL
344
int32_t fmSetInvertFunc(int32_t funcId, SFuncExecFuncs* pFpSet) {
345
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
346
    return TSDB_CODE_FAILED;
347
  }
348
  pFpSet->process = funcMgtBuiltins[funcId].invertFunc;
349
  return TSDB_CODE_SUCCESS;
350
}
351

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

360
bool fmIsInvertible(int32_t funcId) {
361
  bool res = false;
362
  switch (funcMgtBuiltins[funcId].type) {
363
    case FUNCTION_TYPE_COUNT:
364
    case FUNCTION_TYPE_SUM:
365
    case FUNCTION_TYPE_STDDEV:
366
    case FUNCTION_TYPE_AVG:
367
    case FUNCTION_TYPE_WSTART:
368
    case FUNCTION_TYPE_WEND:
369
    case FUNCTION_TYPE_WDURATION:
370
      res = true;
371
      break;
372
    default:
373
      break;
374
  }
375
  return res;
376
}
377
#endif
378

379
// function has same input/output type
380
bool fmIsSameInOutType(int32_t funcId) {
14,665✔
381
  bool res = false;
14,665✔
382
  switch (funcMgtBuiltins[funcId].type) {
14,665✔
383
    case FUNCTION_TYPE_MAX:
6,239✔
384
    case FUNCTION_TYPE_MIN:
385
    case FUNCTION_TYPE_TOP:
386
    case FUNCTION_TYPE_BOTTOM:
387
    case FUNCTION_TYPE_FIRST:
388
    case FUNCTION_TYPE_LAST:
389
    case FUNCTION_TYPE_SAMPLE:
390
    case FUNCTION_TYPE_TAIL:
391
    case FUNCTION_TYPE_UNIQUE:
392
      res = true;
6,239✔
393
      break;
6,239✔
394
    default:
8,426✔
395
      break;
8,426✔
396
  }
397
  return res;
14,665✔
398
}
399

400
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
891✔
401
  SNode* pNode;
402
  FOREACH(pNode, pFunc->pParameterList) {
891!
403
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
×
404
      return false;
×
405
    }
406
  }
407
  return true;
891✔
408
}
409

410
bool fmIsSkipScanCheckFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SKIP_SCAN_CHECK_FUNC); }
114,975✔
411

412
bool fmIsPrimaryKeyFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PRIMARY_KEY_FUNC); }
320,068✔
413

414
bool fmIsPlaceHolderFunc(int32_t funcId) {return isSpecificClassifyFunc(funcId, FUNC_MGT_PLACE_HOLDER_FUNC); }
1,573,003,056✔
415

416
void getLastCacheDataType(SDataType* pType, int32_t pkBytes) {
10,464✔
417
  // TODO: do it later.
418
  pType->bytes = getFirstLastInfoSize(pType->bytes, pkBytes) + VARSTR_HEADER_SIZE;
10,464✔
419
  pType->type = TSDB_DATA_TYPE_BINARY;
10,464✔
420
}
10,464✔
421

422
static int32_t getFuncInfo(SFunctionNode* pFunc) {
46,571✔
423
  char msg[128] = {0};
46,571✔
424
  return fmGetFuncInfo(pFunc, msg, sizeof(msg));
46,571✔
425
}
426

427
int32_t createFunction(const char* pName, SNodeList* pParameterList, SFunctionNode** ppFunc) {
2,573✔
428
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
2,573✔
429
  if (NULL == *ppFunc) {
2,573!
430
    return code;
×
431
  }
432
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
2,573✔
433
  (*ppFunc)->pParameterList = pParameterList;
2,573✔
434
  code = getFuncInfo((*ppFunc));
2,573✔
435
  if (TSDB_CODE_SUCCESS != code) {
2,573!
436
    (*ppFunc)->pParameterList = NULL;
×
437
    nodesDestroyNode((SNode*)*ppFunc);
×
438
    *ppFunc = NULL;
×
439
    return code;
×
440
  }
441
  return code;
2,573✔
442
}
443

444
static void resetOutputChangedFunc(SFunctionNode *pFunc, const SFunctionNode* pSrcFunc) {
43,991✔
445
  if (funcMgtBuiltins[pFunc->funcId].type == FUNCTION_TYPE_LAST_MERGE) {
43,991✔
446
    pFunc->node.resType = pSrcFunc->node.resType;
25,936✔
447
    return;
25,936✔
448
  }
449
}
450

451
int32_t createFunctionWithSrcFunc(const char* pName, const SFunctionNode* pSrcFunc, SNodeList* pParameterList, SFunctionNode** ppFunc) {
43,996✔
452
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
43,996✔
453
  if (NULL == *ppFunc) {
43,998!
454
    return code;
×
455
  }
456

457
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
43,998✔
458
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
43,998✔
459
  (*ppFunc)->pSrcFuncRef = pSrcFunc;
43,998✔
460

461
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
43,998✔
462
  (*ppFunc)->pParameterList = pParameterList;
43,998✔
463
  code = getFuncInfo((*ppFunc));
43,998✔
464
  if (TSDB_CODE_SUCCESS != code) {
43,992!
465
    (*ppFunc)->pParameterList = NULL;
×
466
    nodesDestroyNode((SNode*)*ppFunc);
×
467
    *ppFunc = NULL;
×
468
    return code;
×
469
  }
470
  resetOutputChangedFunc(*ppFunc, pSrcFunc);
43,992✔
471
  (*ppFunc)->node.relatedTo = pSrcFunc->node.relatedTo;
43,991✔
472
  (*ppFunc)->node.bindExprID = pSrcFunc->node.bindExprID;
43,991✔
473
  return code;
43,991✔
474
}
475

476
static int32_t createColumnByFunc(const SFunctionNode* pFunc, SColumnNode** ppCol) {
29,330✔
477
  int32_t code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)ppCol);
29,330✔
478
  if (NULL == *ppCol) {
29,330!
479
    return code;
×
480
  }
481
  tstrncpy((*ppCol)->colName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
29,330✔
482
  (*ppCol)->node.resType = pFunc->node.resType;
29,330✔
483
  return TSDB_CODE_SUCCESS;
29,330✔
484
}
485

486
bool fmIsDistExecFunc(int32_t funcId) {
288,511✔
487
  if (fmIsUserDefinedFunc(funcId)) {
288,511!
488
    return false;
×
489
  }
490
  if (!fmIsVectorFunc(funcId)) {
288,625✔
491
    return true;
376✔
492
  }
493
  return (NULL != funcMgtBuiltins[funcId].pPartialFunc && NULL != funcMgtBuiltins[funcId].pMergeFunc);
288,269!
494
}
495

496
static int32_t createPartialFunction(const SFunctionNode* pSrcFunc, SFunctionNode** pPartialFunc) {
14,662✔
497
  SNodeList* pParameterList = NULL;
14,662✔
498
  int32_t    code = nodesCloneList(pSrcFunc->pParameterList, &pParameterList);
14,662✔
499
  if (NULL == pParameterList) {
14,666!
500
    return code;
×
501
  }
502
  code =
503
      createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pPartialFunc, pSrcFunc, pParameterList, pPartialFunc);
14,666✔
504
  if (TSDB_CODE_SUCCESS != code) {
14,665!
505
    nodesDestroyList(pParameterList);
×
506
    return code;
×
507
  }
508
  (*pPartialFunc)->hasOriginalFunc = true;
14,665✔
509
  (*pPartialFunc)->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
14,665!
510
  char name[TSDB_FUNC_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_POINTER_PRINT_BYTES + 1] = {0};
14,665✔
511

512
  int32_t len = tsnprintf(name, sizeof(name), "%s.%p", (*pPartialFunc)->functionName, pSrcFunc);
14,665✔
513
  if (taosHashBinary(name, len) < 0) {
14,666!
514
    return TSDB_CODE_FAILED;
×
515
  }
516
  tstrncpy((*pPartialFunc)->node.aliasName, name, TSDB_COL_NAME_LEN);
14,666✔
517
  return TSDB_CODE_SUCCESS;
14,666✔
518
}
519

520
static int32_t createMergeFuncPara(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
29,330✔
521
                                   SNodeList** pParameterList) {
522
  SNode*  pRes = NULL;
29,330✔
523
  int32_t code = createColumnByFunc(pPartialFunc, (SColumnNode**)&pRes);
29,330✔
524
  if (TSDB_CODE_SUCCESS != code) {
29,331!
525
    return code;
×
526
  }
527
  if (NULL != funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc) {
29,331✔
528
    return funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc(pSrcFunc->pParameterList, pRes, pParameterList);
6✔
529
  } else {
530
    return nodesListMakeStrictAppend(pParameterList, pRes);
29,325✔
531
  }
532
}
533

534
static int32_t createMidFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
14,665✔
535
                                 SFunctionNode** pMidFunc) {
536
  SNodeList*     pParameterList = NULL;
14,665✔
537
  SFunctionNode* pFunc = NULL;
14,665✔
538

539
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
14,665✔
540
  if (TSDB_CODE_SUCCESS == code) {
14,666!
541
    if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){
14,666✔
542
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pSrcFunc, pParameterList, &pFunc);
62✔
543
    }else{
544
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
14,604✔
545
    }
546
  }
547
  if (TSDB_CODE_SUCCESS == code) {
14,665!
548
    tstrncpy(pFunc->node.aliasName, pPartialFunc->node.aliasName, TSDB_COL_NAME_LEN);
14,665✔
549
  }
550

551
  if (TSDB_CODE_SUCCESS == code) {
14,665!
552
    *pMidFunc = pFunc;
14,665✔
553
  } else {
554
    nodesDestroyList(pParameterList);
×
555
  }
556
  return code;
14,665✔
557
}
558

559
static int32_t createMergeFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
14,665✔
560
                                   SFunctionNode** pMergeFunc) {
561
  SNodeList*     pParameterList = NULL;
14,665✔
562
  SFunctionNode* pFunc = NULL;
14,665✔
563

564
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
14,665✔
565
  if (TSDB_CODE_SUCCESS == code) {
14,665!
566
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
14,665✔
567
  }
568
  if (TSDB_CODE_SUCCESS == code) {
14,664!
569
    pFunc->hasOriginalFunc = true;
14,664✔
570
    pFunc->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
14,664!
571
    // overwrite function restype set by translate function
572
    if (fmIsSameInOutType(pSrcFunc->funcId)) {
14,664✔
573
      pFunc->node.resType = pSrcFunc->node.resType;
6,239✔
574
    }
575
    tstrncpy(pFunc->node.aliasName, pSrcFunc->node.aliasName, TSDB_COL_NAME_LEN);
14,665✔
576
  }
577

578
  if (TSDB_CODE_SUCCESS == code) {
14,665!
579
    *pMergeFunc = pFunc;
14,665✔
580
  } else {
581
    nodesDestroyList(pParameterList);
×
582
  }
583
  return code;
14,666✔
584
}
585

586
int32_t fmGetDistMethod(const SFunctionNode* pFunc, SFunctionNode** pPartialFunc, SFunctionNode** pMidFunc,
14,664✔
587
                        SFunctionNode** pMergeFunc) {
588
  if (!fmIsDistExecFunc(pFunc->funcId)) {
14,664!
589
    return TSDB_CODE_FAILED;
×
590
  }
591

592
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
14,661✔
593
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
14,665!
594
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
14,665✔
595
  }
596
  if (TSDB_CODE_SUCCESS == code) {
14,665!
597
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
14,665✔
598
  }
599

600
  if (TSDB_CODE_SUCCESS != code) {
14,666!
601
    nodesDestroyNode((SNode*)*pPartialFunc);
×
602
    if (pMidFunc) nodesDestroyNode((SNode*)*pMidFunc);
×
603
    nodesDestroyNode((SNode*)*pMergeFunc);
×
604
  }
605

606
  return code;
14,666✔
607
}
608

609
char* fmGetFuncName(int32_t funcId) {
×
610
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
611
    return taosStrdup("invalid function");
×
612
  }
613
  return taosStrdup(funcMgtBuiltins[funcId].name);
×
614
}
615

616
/// @param [out] pStateFunc, not changed if error occured or no need to create state func
617
/// @retval 0 for succ, otherwise err occured
618
static int32_t fmCreateStateFunc(const SFunctionNode* pFunc, SFunctionNode** pStateFunc) {
×
619
  if (funcMgtBuiltins[pFunc->funcId].pStateFunc) {
×
620
    SNodeList* pParams = NULL;
×
621
    int32_t    code = nodesCloneList(pFunc->pParameterList, &pParams);
×
622
    if (!pParams) return code;
×
623
    code = createFunction(funcMgtBuiltins[pFunc->funcId].pStateFunc, pParams, pStateFunc);
×
624
    if (TSDB_CODE_SUCCESS != code) {
×
625
      nodesDestroyList(pParams);
×
626
      return code;
×
627
    }
628
    tstrncpy((*pStateFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
×
629
    tstrncpy((*pStateFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
×
630
  }
631
  return TSDB_CODE_SUCCESS;
×
632
}
633

634
bool fmIsTSMASupportedFunc(func_id_t funcId) {
×
635
  return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC);
×
636
}
637

638
int32_t fmCreateStateFuncs(SNodeList* pFuncs) {
×
639
  int32_t code;
640
  SNode*  pNode;
641
  char    buf[128] = {0};
×
642
  FOREACH(pNode, pFuncs) {
×
643
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
×
644
    code = fmGetFuncInfo(pFunc, buf, 128);
×
645
    if (code) break;
×
646
    if (fmIsTSMASupportedFunc(pFunc->funcId)) {
×
647
      SFunctionNode* pNewFunc = NULL;
×
648
      code = fmCreateStateFunc(pFunc, &pNewFunc);
×
649
      if (code) {
×
650
        // error
651
        break;
×
652
      } else if (!pNewFunc) {
×
653
        // no need state func
654
        continue;
×
655
      } else {
656
        REPLACE_NODE(pNewFunc);
×
657
        nodesDestroyNode(pNode);
×
658
      }
659
    }
660
  }
661
  return code;
×
662
}
663

664
static int32_t fmCreateStateMergeFunc(SFunctionNode* pFunc, SFunctionNode** pStateMergeFunc) {
×
665
  if (funcMgtBuiltins[pFunc->funcId].pMergeFunc) {
×
666
    SNodeList* pParams = NULL;
×
667
    int32_t    code = nodesCloneList(pFunc->pParameterList, &pParams);
×
668
    if (!pParams) return code;
×
669
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pFunc->funcId].pMergeFunc, pFunc, pParams, pStateMergeFunc);
×
670
    if (TSDB_CODE_SUCCESS != code) {
×
671
      nodesDestroyList(pParams);
×
672
      return code;
×
673
    }
674
    tstrncpy((*pStateMergeFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
×
675
    tstrncpy((*pStateMergeFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
×
676
  }
677
  return TSDB_CODE_SUCCESS;
×
678
}
679

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

704
int32_t fmGetFuncId(const char* name) {
6,108✔
705
  if (NULL != gFunMgtService.pFuncNameHashTable) {
6,108!
706
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, name, strlen(name));
6,108✔
707
    if (NULL != pVal) {
6,108!
708
      return *(int32_t*)pVal;
6,108✔
709
    }
710
    return -1;
×
711
  }
712
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
×
713
    if (0 == strcmp(funcMgtBuiltins[i].name, name)) {
×
714
      return i;
×
715
    }
716
  }
717
  return -1;
×
718
}
719

720
bool fmIsMyStateFunc(int32_t funcId, int32_t stateFuncId) {
×
721
  const SBuiltinFuncDefinition* pFunc = &funcMgtBuiltins[funcId];
×
722
  const SBuiltinFuncDefinition* pStateFunc = &funcMgtBuiltins[stateFuncId];
×
723
  if (!pFunc->pStateFunc) {
×
724
    return false;
×
725
  }
726
  if (strcmp(pFunc->pStateFunc, pStateFunc->name) == 0) return true;
×
727
  int32_t stateMergeFuncId = fmGetFuncId(pFunc->pStateFunc);
×
728
  if (stateMergeFuncId == -1) {
×
729
    return false;
×
730
  }
731
  const SBuiltinFuncDefinition* pStateMergeFunc = &funcMgtBuiltins[stateMergeFuncId];
×
732
  return strcmp(pStateFunc->name, pStateMergeFunc->pMergeFunc) == 0;
×
733
}
734

735
bool fmIsCountLikeFunc(int32_t funcId) {
1,272,799✔
736
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
1,272,799✔
737
}
738

739
bool fmIsRowTsOriginFunc(int32_t funcId) {
873✔
740
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
873!
741
    return false;
×
742
  }
743
  return FUNCTION_TYPE_IROWTS_ORIGIN == funcMgtBuiltins[funcId].type;
873✔
744
}
745

746
bool fmIsGroupIdFunc(int32_t funcId) {
×
747
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
748
    return false;
×
749
  }
750
  return FUNCTION_TYPE_GROUP_ID == funcMgtBuiltins[funcId].type;
×
751
}
752

753
const void* fmGetStreamPesudoFuncVal(int32_t funcId, const SStreamRuntimeFuncInfo* pStreamRuntimeFuncInfo) {
813,837✔
754
  SSTriggerCalcParam *pParams = taosArrayGet(pStreamRuntimeFuncInfo->pStreamPesudoFuncVals, pStreamRuntimeFuncInfo->curIdx);
813,837✔
755
  switch (funcMgtBuiltins[funcId].type) {
813,837!
756
    case FUNCTION_TYPE_TPREV_TS:
×
757
      return &pParams->prevTs;
×
758
    case FUNCTION_TYPE_TCURRENT_TS:
12,198✔
759
      return &pParams->currentTs;
12,198✔
760
    case FUNCTION_TYPE_TNEXT_TS:
×
761
      return &pParams->nextTs;
×
762
    case FUNCTION_TYPE_TWSTART:
527,553✔
763
      return &pParams->wstart;
527,553✔
764
    case FUNCTION_TYPE_TWEND:
263,345✔
765
      return &pParams->wend;
263,345✔
766
    case FUNCTION_TYPE_TWDURATION:
×
767
      return &pParams->wduration;
×
768
    case FUNCTION_TYPE_TWROWNUM:
303✔
769
      return &pParams->wrownum;
303✔
770
    case FUNCTION_TYPE_TPREV_LOCALTIME:
1✔
771
      return &pParams->prevLocalTime;
1✔
772
    case FUNCTION_TYPE_TLOCALTIME:
145✔
773
      return &pParams->triggerTime;
145✔
774
    case FUNCTION_TYPE_TNEXT_LOCALTIME:
1✔
775
      return &pParams->nextLocalTime;
1✔
776
    case FUNCTION_TYPE_TGRPID:
169✔
777
      return &pStreamRuntimeFuncInfo->groupId;
169✔
778
    case FUNCTION_TYPE_PLACEHOLDER_COLUMN:
1,450✔
779
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
1,450✔
780
    case FUNCTION_TYPE_PLACEHOLDER_TBNAME:
8,672✔
781
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
8,672✔
782
    default:
×
783
      break;
×
784
  }
785
  return NULL;
×
786
}
787

788
int32_t fmGetStreamPesudoFuncEnv(int32_t funcId, SNodeList* pParamNodes, SFuncExecEnv *pEnv) {
8,646✔
789
  if (NULL == pParamNodes || pParamNodes->length < 1) {
8,646!
790
    uError("invalid stream pesudo func param list %p", pParamNodes);
×
791
    return TSDB_CODE_INTERNAL_ERROR;
×
792
  }
793

794
  int32_t firstParamType = nodeType(nodesListGetNode(pParamNodes, 0));
8,646✔
795
  if (QUERY_NODE_VALUE != firstParamType) {
8,647!
796
    uError("invalid stream pesudo func first param type %d", firstParamType);
×
797
    return TSDB_CODE_INTERNAL_ERROR;
×
798
  }
799

800
  SValueNode* pResDesc = (SValueNode*)nodesListGetNode(pParamNodes, 0);
8,647✔
801
  pEnv->calcMemSize = pResDesc->node.resType.bytes;
8,647✔
802

803
  return TSDB_CODE_SUCCESS;
8,647✔
804
}
805

806
int32_t fmSetStreamPseudoFuncParamVal(int32_t funcId, SNodeList* pParamNodes, const SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
813,845✔
807
  if (!pStreamRuntimeInfo) {
813,845✔
808
    uError("internal error, should have pVals for stream pseudo funcs");
8!
809
    return TSDB_CODE_INTERNAL_ERROR;
8✔
810
  }
811
  int32_t code = 0;
813,837✔
812
  SArray *pVals1 = NULL, *pVals2 = NULL;
813,837✔
813
  SNode* pFirstParam = nodesListGetNode(pParamNodes, 0);
813,837✔
814
  if (nodeType(pFirstParam) != QUERY_NODE_VALUE) {
813,837!
815
    uError("invalid param node type: %d for func: %d", nodeType(pFirstParam), funcId);
×
816
    return TSDB_CODE_INTERNAL_ERROR;
×
817
  }
818

819
  int32_t t = funcMgtBuiltins[funcId].type;
813,837✔
820
  uDebug("set stream pseudo func param val, functype: %d, pStreamRuntimeInfo: %p", t, pStreamRuntimeInfo);
813,837!
821
  if (FUNCTION_TYPE_TGRPID == t) {
813,837✔
822
    SValue v = {0};
169✔
823
    v.type = TSDB_DATA_TYPE_BIGINT;
169✔
824
    v.val = *(int64_t*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
169✔
825
    
826
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&v, pFirstParam->type));
169!
827
    if (code != 0) {
169!
828
      uError("failed to set value node value: %s", tstrerror(code));
×
829
      return code;
×
830
    }
831
  } else if (FUNCTION_TYPE_PLACEHOLDER_COLUMN == t) {
813,668✔
832
    SNode* pSecondParam = nodesListGetNode(pParamNodes, 1);
1,450✔
833
    if (nodeType(pSecondParam) != QUERY_NODE_VALUE) {
1,450!
834
      uError("invalid param node type: %d for func: %d", nodeType(pSecondParam), funcId);
×
835
      return TSDB_CODE_INTERNAL_ERROR;
×
836
    }
837
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
1,450✔
838
    int32_t idx = ((SValueNode*)pSecondParam)->datum.i;
1,450✔
839
    if (idx - 1 < 0 || idx - 1 >= taosArrayGetSize(pVal)) {
1,450!
840
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
×
841
      return TSDB_CODE_INTERNAL_ERROR;
×
842
    }
843
    SStreamGroupValue* pValue = taosArrayGet(pVal, idx - 1);
1,450✔
844
    if (pValue == NULL) {
1,450!
845
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
×
846
      return TSDB_CODE_INTERNAL_ERROR;
×
847
    }
848
    if (!pValue->isNull){
1,450!
849
      if (pValue->data.type != ((SValueNode*)pFirstParam)->node.resType.type){
1,450!
850
        uError("invalid value type: %d for func: %d, should be: %d", pValue->data.type, funcId, ((SValueNode*)pFirstParam)->node.resType.type);
×
851
        return TSDB_CODE_INTERNAL_ERROR;
×
852
      }
853
      if (IS_VAR_DATA_TYPE(((SValueNode*)pFirstParam)->node.resType.type)) {
1,450!
854
        taosMemoryFree(((SValueNode*)pFirstParam)->datum.p);
1,018!
855
        ((SValueNode*)pFirstParam)->datum.p = taosMemoryCalloc(1, pValue->data.nData + VARSTR_HEADER_SIZE); 
1,018!
856
        memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
1,018✔
857
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
1,018✔
858
      } else {
859
        code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&pValue->data, pValue->data.type));
432!
860
      }
861
    }
862
    ((SValueNode*)pFirstParam)->isNull = pValue->isNull;
1,450✔
863
  } else if (FUNCTION_TYPE_PLACEHOLDER_TBNAME == t) {
812,218✔
864
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
8,672✔
865
    for (int32_t i = 0; i < taosArrayGetSize(pVal); ++i) {
8,672!
866
      SStreamGroupValue* pValue = taosArrayGet(pVal, i);
8,672✔
867
      if (pValue != NULL && pValue->isTbname) {
8,672!
868
        taosMemoryFreeClear(((SValueNode*)pFirstParam)->datum.p);
8,672!
869
        ((SValueNode*)pFirstParam)->datum.p = taosMemoryCalloc(pValue->data.nData + VARSTR_HEADER_SIZE, 1);
8,672!
870
        if (NULL == ((SValueNode*)pFirstParam)->datum.p ) {
8,672!
871
          return terrno;
×
872
        }
873

874
        (void)memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
8,672✔
875
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
8,672✔
876
        break;
8,672✔
877
      }
878
    }
879
  } else if (LIST_LENGTH(pParamNodes) == 1) {
1,607,092!
880
    // twstart, twend
881
    const void* pVal = fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
803,546✔
882
    if (!pVal) {
803,546!
883
      uError("failed to set stream pseudo func param val, NULL val for funcId: %d", funcId);
×
884
      return TSDB_CODE_INTERNAL_ERROR;
×
885
    }
886
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, (void*)pVal);
803,546✔
887
    if (code != 0) {
803,546!
888
      uError("failed to set value node value: %s", tstrerror(code));
×
889
      return code;
×
890
    }
891
  } else {
892
    SNode* pSecondParam = nodesListGetNode(pParamNodes, 1);
×
893
    if (nodeType(pSecondParam) != QUERY_NODE_VALUE) {
×
894
      uError("invalid param node type: %d for func: %d", nodeType(pSecondParam), funcId);
×
895
      return TSDB_CODE_INTERNAL_ERROR;
×
896
    }
897
    int32_t idx = ((SValueNode*)pSecondParam)->datum.i;
×
898
    const SValue* pVal = taosArrayGet(pVals2, idx);
×
899
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(pVal, pFirstParam->type));
×
900
  }
901
  return code;
813,837✔
902
}
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