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

taosdata / TDengine / #4717

04 Sep 2025 12:33PM UTC coverage: 58.697% (+0.1%) from 58.593%
#4717

push

travis-ci

web-flow
test: update case description (#32878)

134624 of 291691 branches covered (46.15%)

Branch coverage included in aggregate %.

202924 of 283375 relevant lines covered (71.61%)

26047439.86 hits per line

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

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

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

51
static bool isSpecificClassifyFunc(int32_t funcId, uint64_t classification) {
1,862,730,549✔
52
  if (fmIsUserDefinedFunc(funcId)) {
1,862,730,549✔
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,485✔
56
  }
57
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,862,539,354!
58
    return false;
216,691,603✔
59
  }
60
  return FUNC_MGT_TEST_MASK(funcMgtBuiltins[funcId].classification, classification);
1,645,847,751✔
61
}
62

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

68
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
617,807✔
69
  if (NULL != gFunMgtService.pFuncNameHashTable) {
617,807!
70
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
617,840✔
71
    if (NULL != pVal) {
617,880✔
72
      pFunc->funcId = *(int32_t*)pVal;
617,828✔
73
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
617,828✔
74
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
617,828✔
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) {
977✔
89
  if (NULL != funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc) {
977✔
90
    return funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc(pFunc);
715✔
91
  }
92
  return (fmIsIndefiniteRowsFunc(pFunc->funcId) || fmIsMultiRowsFunc(pFunc->funcId)) ? FUNC_RETURN_ROWS_INDEFINITE
262!
93
                                                                                     : FUNC_RETURN_ROWS_NORMAL;
262!
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) {
239,499✔
101
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
239,499✔
102
  if (NULL != pVal) {
239,448✔
103
    return funcMgtBuiltins[*(int32_t*)pVal].type;
239,434✔
104
  }
105
  return FUNCTION_TYPE_UDF;
14✔
106
}
107

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

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

123
  const char* name = funcMgtBuiltins[funcId].name;
3,558,682✔
124
  if ((strcmp(name, "_group_key") == 0) || (strcmp(name, "_select_value") == 0)) {
3,558,682✔
125
    return FUNC_DATA_REQUIRED_NOT_LOAD;
302,095✔
126
    ;
127
  }
128

129
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
3,256,587✔
130
    return FUNC_DATA_REQUIRED_DATA_LOAD;
3,209,698✔
131
  } else {
132
    return funcMgtBuiltins[funcId].dynDataRequiredFunc(pRes, pBlockInfo);
46,889✔
133
  }
134
}
135

136
int32_t fmGetFuncExecFuncs(int32_t funcId, SFuncExecFuncs* pFpSet) {
11,670,858✔
137
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
11,670,858!
138
    return TSDB_CODE_FAILED;
2,835✔
139
  }
140
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
11,677,140✔
141
  pFpSet->init = funcMgtBuiltins[funcId].initFunc;
11,677,140✔
142
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
11,677,140✔
143
  pFpSet->finalize = funcMgtBuiltins[funcId].finalizeFunc;
11,677,140✔
144
  pFpSet->combine = funcMgtBuiltins[funcId].combineFunc;
11,677,140✔
145
  pFpSet->processFuncByRow = funcMgtBuiltins[funcId].processFuncByRow;
11,677,140✔
146
  pFpSet->cleanup = funcMgtBuiltins[funcId].cleanupFunc;
11,677,140✔
147
  return TSDB_CODE_SUCCESS;
11,677,140✔
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) {
15,183,762✔
166
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
15,183,762!
167
    return TSDB_CODE_FAILED;
193✔
168
  }
169
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
15,186,186✔
170
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
15,186,186✔
171
  return TSDB_CODE_SUCCESS;
15,186,186✔
172
}
173

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

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

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

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

182
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
48,348,592✔
183

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

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

188
bool fmIsPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PSEUDO_COLUMN_FUNC); }
47,757,199✔
189

190
bool fmIsScanPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCAN_PC_FUNC); }
27,517,968✔
191

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

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

196
bool fmIsStreamWindowClauseFunc(int32_t funcId) { return fmIsWindowClauseFunc(funcId) || fmIsPlaceHolderFunc(funcId); }
38!
197

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

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

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

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

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

212
bool fmIsRepeatScanFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_REPEAT_SCAN_FUNC); }
3,276,387✔
213

214
bool fmIsUserDefinedFunc(int32_t funcId) { return funcId > FUNC_UDF_ID_START; }
1,916,968,568✔
215

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

218
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
56,438,725✔
219

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

222
bool fmIsImplicitTsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IMPLICIT_TS_FUNC); }
85,549,344✔
223

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

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

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

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

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

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

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

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

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

252
bool fmIsLastRowFunc(int32_t funcId) {
41,414✔
253
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
41,414!
254
    return false;
×
255
  }
256
  return FUNCTION_TYPE_LAST_ROW == funcMgtBuiltins[funcId].type;
41,416✔
257
}
258

259
bool fmIsLastFunc(int32_t funcId) {
×
260
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
261
    return false;
×
262
  }
263
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type;
×
264
}
265

266
bool fmIsNotNullOutputFunc(int32_t funcId) {
17,135,932✔
267
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
17,135,932!
268
    return false;
×
269
  }
270
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
33,928,028✔
271
         FUNCTION_TYPE_CACHE_LAST == funcMgtBuiltins[funcId].type ||
16,785,924✔
272
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
16,781,605✔
273
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
15,554,016✔
274
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
14,942,455✔
275
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
14,658,685✔
276
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
14,223,989✔
277
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
14,008,164✔
278
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
10,809,920✔
279
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
44,701,482✔
280
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
10,773,454✔
281
}
282

283
bool fmIsSelectValueFunc(int32_t funcId) {
1,004,809✔
284
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,004,809!
285
    return false;
×
286
  }
287
  return FUNCTION_TYPE_SELECT_VALUE == funcMgtBuiltins[funcId].type;
1,004,809✔
288
}
289

290
bool fmIsGroupKeyFunc(int32_t funcId) {
521,655,080✔
291
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
521,655,080!
292
    return false;
×
293
  }
294
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
521,656,798✔
295
}
296

297
bool fmisSelectGroupConstValueFunc(int32_t funcId) {
10✔
298
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
10!
299
    return false;
×
300
  }
301
  return FUNCTION_TYPE_GROUP_CONST_VALUE == funcMgtBuiltins[funcId].type;
10✔
302
}
303

304
bool fmIsElapsedFunc(int32_t funcId) {
34,797,378✔
305
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
34,797,378!
306
    return false;
×
307
  }
308
  return FUNCTION_TYPE_ELAPSED == funcMgtBuiltins[funcId].type;
34,797,836✔
309
}
310

311
bool fmIsBlockDistFunc(int32_t funcId) {
558,934✔
312
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
558,934!
313
    return false;
29✔
314
  }
315
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
558,905✔
316
}
317

318
bool fmIsDBUsageFunc(int32_t funcId) {
558,960✔
319
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
558,960!
320
    return false;
×
321
  }
322
  return FUNCTION_TYPE_DB_USAGE == funcMgtBuiltins[funcId].type;
558,980✔
323
}
324

325
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
3,691,076✔
326

327
bool fmIsIgnoreNullFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IGNORE_NULL_FUNC); }
12✔
328

329
void fmFuncMgtDestroy() {
3,744✔
330
  void* m = gFunMgtService.pFuncNameHashTable;
3,744✔
331
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
3,744!
332
    taosHashCleanup(m);
3,743✔
333
  }
334
}
3,744✔
335

336
#ifdef BUILD_NO_CALL
337
int32_t fmSetInvertFunc(int32_t funcId, SFuncExecFuncs* pFpSet) {
338
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
339
    return TSDB_CODE_FAILED;
340
  }
341
  pFpSet->process = funcMgtBuiltins[funcId].invertFunc;
342
  return TSDB_CODE_SUCCESS;
343
}
344

345
int32_t fmSetNormalFunc(int32_t funcId, SFuncExecFuncs* pFpSet) {
346
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
347
    return TSDB_CODE_FAILED;
348
  }
349
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
350
  return TSDB_CODE_SUCCESS;
351
}
352

353
bool fmIsInvertible(int32_t funcId) {
354
  bool res = false;
355
  switch (funcMgtBuiltins[funcId].type) {
356
    case FUNCTION_TYPE_COUNT:
357
    case FUNCTION_TYPE_SUM:
358
    case FUNCTION_TYPE_STDDEV:
359
    case FUNCTION_TYPE_AVG:
360
    case FUNCTION_TYPE_WSTART:
361
    case FUNCTION_TYPE_WEND:
362
    case FUNCTION_TYPE_WDURATION:
363
      res = true;
364
      break;
365
    default:
366
      break;
367
  }
368
  return res;
369
}
370
#endif
371

372
// function has same input/output type
373
bool fmIsSameInOutType(int32_t funcId) {
14,447✔
374
  bool res = false;
14,447✔
375
  switch (funcMgtBuiltins[funcId].type) {
14,447✔
376
    case FUNCTION_TYPE_MAX:
6,215✔
377
    case FUNCTION_TYPE_MIN:
378
    case FUNCTION_TYPE_TOP:
379
    case FUNCTION_TYPE_BOTTOM:
380
    case FUNCTION_TYPE_FIRST:
381
    case FUNCTION_TYPE_LAST:
382
    case FUNCTION_TYPE_SAMPLE:
383
    case FUNCTION_TYPE_TAIL:
384
    case FUNCTION_TYPE_UNIQUE:
385
      res = true;
6,215✔
386
      break;
6,215✔
387
    default:
8,232✔
388
      break;
8,232✔
389
  }
390
  return res;
14,447✔
391
}
392

393
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
479✔
394
  SNode* pNode;
395
  FOREACH(pNode, pFunc->pParameterList) {
479!
396
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
×
397
      return false;
×
398
    }
399
  }
400
  return true;
479✔
401
}
402

403
bool fmIsSkipScanCheckFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SKIP_SCAN_CHECK_FUNC); }
113,431✔
404

405
bool fmIsPrimaryKeyFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PRIMARY_KEY_FUNC); }
297,738✔
406

407
bool fmIsPlaceHolderFunc(int32_t funcId) {return isSpecificClassifyFunc(funcId, FUNC_MGT_PLACE_HOLDER_FUNC); }
1,528,147,513✔
408

409
void getLastCacheDataType(SDataType* pType, int32_t pkBytes) {
×
410
  // TODO: do it later.
411
  pType->bytes = getFirstLastInfoSize(pType->bytes, pkBytes) + VARSTR_HEADER_SIZE;
×
412
  pType->type = TSDB_DATA_TYPE_BINARY;
×
413
}
×
414

415
static int32_t getFuncInfo(SFunctionNode* pFunc) {
43,662✔
416
  char msg[128] = {0};
43,662✔
417
  return fmGetFuncInfo(pFunc, msg, sizeof(msg));
43,662✔
418
}
419

420
int32_t createFunction(const char* pName, SNodeList* pParameterList, SFunctionNode** ppFunc) {
320✔
421
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
320✔
422
  if (NULL == *ppFunc) {
320!
423
    return code;
×
424
  }
425
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
320✔
426
  (*ppFunc)->pParameterList = pParameterList;
320✔
427
  code = getFuncInfo((*ppFunc));
320✔
428
  if (TSDB_CODE_SUCCESS != code) {
320!
429
    (*ppFunc)->pParameterList = NULL;
×
430
    nodesDestroyNode((SNode*)*ppFunc);
×
431
    *ppFunc = NULL;
×
432
    return code;
×
433
  }
434
  return code;
320✔
435
}
436

437
static void resetOutputChangedFunc(SFunctionNode *pFunc, const SFunctionNode* pSrcFunc) {
43,343✔
438
  if (funcMgtBuiltins[pFunc->funcId].type == FUNCTION_TYPE_LAST_MERGE) {
43,343✔
439
    pFunc->node.resType = pSrcFunc->node.resType;
25,919✔
440
    return;
25,919✔
441
  }
442
}
443

444
int32_t createFunctionWithSrcFunc(const char* pName, const SFunctionNode* pSrcFunc, SNodeList* pParameterList, SFunctionNode** ppFunc) {
43,346✔
445
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
43,346✔
446
  if (NULL == *ppFunc) {
43,348!
447
    return code;
×
448
  }
449

450
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
43,348✔
451
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
43,348✔
452
  (*ppFunc)->pSrcFuncRef = pSrcFunc;
43,348✔
453

454
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
43,348✔
455
  (*ppFunc)->pParameterList = pParameterList;
43,348✔
456
  code = getFuncInfo((*ppFunc));
43,348✔
457
  if (TSDB_CODE_SUCCESS != code) {
43,343!
458
    (*ppFunc)->pParameterList = NULL;
×
459
    nodesDestroyNode((SNode*)*ppFunc);
×
460
    *ppFunc = NULL;
×
461
    return code;
×
462
  }
463
  resetOutputChangedFunc(*ppFunc, pSrcFunc);
43,343✔
464
  (*ppFunc)->node.relatedTo = pSrcFunc->node.relatedTo;
43,342✔
465
  (*ppFunc)->node.bindExprID = pSrcFunc->node.bindExprID;
43,342✔
466
  return code;
43,342✔
467
}
468

469
static int32_t createColumnByFunc(const SFunctionNode* pFunc, SColumnNode** ppCol) {
28,895✔
470
  int32_t code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)ppCol);
28,895✔
471
  if (NULL == *ppCol) {
28,897!
472
    return code;
×
473
  }
474
  tstrncpy((*ppCol)->colName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
28,897✔
475
  (*ppCol)->node.resType = pFunc->node.resType;
28,897✔
476
  return TSDB_CODE_SUCCESS;
28,897✔
477
}
478

479
bool fmIsDistExecFunc(int32_t funcId) {
278,930✔
480
  if (fmIsUserDefinedFunc(funcId)) {
278,930!
481
    return false;
×
482
  }
483
  if (!fmIsVectorFunc(funcId)) {
279,035✔
484
    return true;
32✔
485
  }
486
  return (NULL != funcMgtBuiltins[funcId].pPartialFunc && NULL != funcMgtBuiltins[funcId].pMergeFunc);
279,033✔
487
}
488

489
static int32_t createPartialFunction(const SFunctionNode* pSrcFunc, SFunctionNode** pPartialFunc) {
14,446✔
490
  SNodeList* pParameterList = NULL;
14,446✔
491
  int32_t    code = nodesCloneList(pSrcFunc->pParameterList, &pParameterList);
14,446✔
492
  if (NULL == pParameterList) {
14,448!
493
    return code;
×
494
  }
495
  code =
496
      createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pPartialFunc, pSrcFunc, pParameterList, pPartialFunc);
14,448✔
497
  if (TSDB_CODE_SUCCESS != code) {
14,447!
498
    nodesDestroyList(pParameterList);
×
499
    return code;
×
500
  }
501
  (*pPartialFunc)->hasOriginalFunc = true;
14,447✔
502
  (*pPartialFunc)->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
14,447!
503
  char name[TSDB_FUNC_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_POINTER_PRINT_BYTES + 1] = {0};
14,447✔
504

505
  int32_t len = tsnprintf(name, sizeof(name), "%s.%p", (*pPartialFunc)->functionName, pSrcFunc);
14,447✔
506
  if (taosHashBinary(name, len) < 0) {
14,450!
507
    return TSDB_CODE_FAILED;
×
508
  }
509
  tstrncpy((*pPartialFunc)->node.aliasName, name, TSDB_COL_NAME_LEN);
14,450✔
510
  return TSDB_CODE_SUCCESS;
14,450✔
511
}
512

513
static int32_t createMergeFuncPara(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
28,895✔
514
                                   SNodeList** pParameterList) {
515
  SNode*  pRes = NULL;
28,895✔
516
  int32_t code = createColumnByFunc(pPartialFunc, (SColumnNode**)&pRes);
28,895✔
517
  if (TSDB_CODE_SUCCESS != code) {
28,899!
518
    return code;
×
519
  }
520
  if (NULL != funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc) {
28,899✔
521
    return funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc(pSrcFunc->pParameterList, pRes, pParameterList);
6✔
522
  } else {
523
    return nodesListMakeStrictAppend(pParameterList, pRes);
28,893✔
524
  }
525
}
526

527
static int32_t createMidFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
14,447✔
528
                                 SFunctionNode** pMidFunc) {
529
  SNodeList*     pParameterList = NULL;
14,447✔
530
  SFunctionNode* pFunc = NULL;
14,447✔
531

532
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
14,447✔
533
  if (TSDB_CODE_SUCCESS == code) {
14,450!
534
    if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){
14,450✔
535
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pSrcFunc, pParameterList, &pFunc);
60✔
536
    }else{
537
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
14,390✔
538
    }
539
  }
540
  if (TSDB_CODE_SUCCESS == code) {
14,450✔
541
    tstrncpy(pFunc->node.aliasName, pPartialFunc->node.aliasName, TSDB_COL_NAME_LEN);
14,448✔
542
  }
543

544
  if (TSDB_CODE_SUCCESS == code) {
14,450!
545
    *pMidFunc = pFunc;
14,450✔
546
  } else {
547
    nodesDestroyList(pParameterList);
×
548
  }
549
  return code;
14,448✔
550
}
551

552
static int32_t createMergeFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
14,448✔
553
                                   SFunctionNode** pMergeFunc) {
554
  SNodeList*     pParameterList = NULL;
14,448✔
555
  SFunctionNode* pFunc = NULL;
14,448✔
556

557
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
14,448✔
558
  if (TSDB_CODE_SUCCESS == code) {
14,451!
559
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
14,451✔
560
  }
561
  if (TSDB_CODE_SUCCESS == code) {
14,451✔
562
    pFunc->hasOriginalFunc = true;
14,448✔
563
    pFunc->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
14,448!
564
    // overwrite function restype set by translate function
565
    if (fmIsSameInOutType(pSrcFunc->funcId)) {
14,448✔
566
      pFunc->node.resType = pSrcFunc->node.resType;
6,215✔
567
    }
568
    tstrncpy(pFunc->node.aliasName, pSrcFunc->node.aliasName, TSDB_COL_NAME_LEN);
14,448✔
569
  }
570

571
  if (TSDB_CODE_SUCCESS == code) {
14,451!
572
    *pMergeFunc = pFunc;
14,451✔
573
  } else {
574
    nodesDestroyList(pParameterList);
×
575
  }
576
  return code;
14,451✔
577
}
578

579
int32_t fmGetDistMethod(const SFunctionNode* pFunc, SFunctionNode** pPartialFunc, SFunctionNode** pMidFunc,
14,448✔
580
                        SFunctionNode** pMergeFunc) {
581
  if (!fmIsDistExecFunc(pFunc->funcId)) {
14,448!
582
    return TSDB_CODE_FAILED;
×
583
  }
584

585
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
14,448✔
586
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
14,447!
587
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
14,447✔
588
  }
589
  if (TSDB_CODE_SUCCESS == code) {
14,448!
590
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
14,448✔
591
  }
592

593
  if (TSDB_CODE_SUCCESS != code) {
14,449!
594
    nodesDestroyNode((SNode*)*pPartialFunc);
×
595
    if (pMidFunc) nodesDestroyNode((SNode*)*pMidFunc);
×
596
    nodesDestroyNode((SNode*)*pMergeFunc);
×
597
  }
598

599
  return code;
14,449✔
600
}
601

602
char* fmGetFuncName(int32_t funcId) {
×
603
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
604
    return taosStrdup("invalid function");
×
605
  }
606
  return taosStrdup(funcMgtBuiltins[funcId].name);
×
607
}
608

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

627
bool fmIsTSMASupportedFunc(func_id_t funcId) {
×
628
  return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC);
×
629
}
630

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

657
static int32_t fmCreateStateMergeFunc(SFunctionNode* pFunc, SFunctionNode** pStateMergeFunc) {
×
658
  if (funcMgtBuiltins[pFunc->funcId].pMergeFunc) {
×
659
    SNodeList* pParams = NULL;
×
660
    int32_t    code = nodesCloneList(pFunc->pParameterList, &pParams);
×
661
    if (!pParams) return code;
×
662
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pFunc->funcId].pMergeFunc, pFunc, pParams, pStateMergeFunc);
×
663
    if (TSDB_CODE_SUCCESS != code) {
×
664
      nodesDestroyList(pParams);
×
665
      return code;
×
666
    }
667
    tstrncpy((*pStateMergeFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
×
668
    tstrncpy((*pStateMergeFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
×
669
  }
670
  return TSDB_CODE_SUCCESS;
×
671
}
672

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

697
int32_t fmGetFuncId(const char* name) {
544✔
698
  if (NULL != gFunMgtService.pFuncNameHashTable) {
544!
699
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, name, strlen(name));
544✔
700
    if (NULL != pVal) {
544!
701
      return *(int32_t*)pVal;
544✔
702
    }
703
    return -1;
×
704
  }
705
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
×
706
    if (0 == strcmp(funcMgtBuiltins[i].name, name)) {
×
707
      return i;
×
708
    }
709
  }
710
  return -1;
×
711
}
712

713
bool fmIsMyStateFunc(int32_t funcId, int32_t stateFuncId) {
×
714
  const SBuiltinFuncDefinition* pFunc = &funcMgtBuiltins[funcId];
×
715
  const SBuiltinFuncDefinition* pStateFunc = &funcMgtBuiltins[stateFuncId];
×
716
  if (!pFunc->pStateFunc) {
×
717
    return false;
×
718
  }
719
  if (strcmp(pFunc->pStateFunc, pStateFunc->name) == 0) return true;
×
720
  int32_t stateMergeFuncId = fmGetFuncId(pFunc->pStateFunc);
×
721
  if (stateMergeFuncId == -1) {
×
722
    return false;
×
723
  }
724
  const SBuiltinFuncDefinition* pStateMergeFunc = &funcMgtBuiltins[stateMergeFuncId];
×
725
  return strcmp(pStateFunc->name, pStateMergeFunc->pMergeFunc) == 0;
×
726
}
727

728
bool fmIsCountLikeFunc(int32_t funcId) {
1,083,995✔
729
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
1,083,995✔
730
}
731

732
bool fmIsRowTsOriginFunc(int32_t funcId) {
873✔
733
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
873!
734
    return false;
×
735
  }
736
  return FUNCTION_TYPE_IROWTS_ORIGIN == funcMgtBuiltins[funcId].type;
873✔
737
}
738

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

746
const void* fmGetStreamPesudoFuncVal(int32_t funcId, const SStreamRuntimeFuncInfo* pStreamRuntimeFuncInfo) {
184,617✔
747
  SSTriggerCalcParam *pParams = taosArrayGet(pStreamRuntimeFuncInfo->pStreamPesudoFuncVals, pStreamRuntimeFuncInfo->curIdx);
184,617✔
748
  switch (funcMgtBuiltins[funcId].type) {
184,617!
749
    case FUNCTION_TYPE_TPREV_TS:
11✔
750
      return &pParams->prevTs;
11✔
751
    case FUNCTION_TYPE_TCURRENT_TS:
9,250✔
752
      return &pParams->currentTs;
9,250✔
753
    case FUNCTION_TYPE_TNEXT_TS:
10✔
754
      return &pParams->nextTs;
10✔
755
    case FUNCTION_TYPE_TWSTART:
80,724✔
756
      return &pParams->wstart;
80,724✔
757
    case FUNCTION_TYPE_TWEND:
77,805✔
758
      return &pParams->wend;
77,805✔
759
    case FUNCTION_TYPE_TWDURATION:
198✔
760
      return &pParams->wduration;
198✔
761
    case FUNCTION_TYPE_TWROWNUM:
504✔
762
      return &pParams->wrownum;
504✔
763
    case FUNCTION_TYPE_TPREV_LOCALTIME:
85✔
764
      return &pParams->prevLocalTime;
85✔
765
    case FUNCTION_TYPE_TLOCALTIME:
409✔
766
      return &pParams->triggerTime;
409✔
767
    case FUNCTION_TYPE_TNEXT_LOCALTIME:
179✔
768
      return &pParams->nextLocalTime;
179✔
769
    case FUNCTION_TYPE_TGRPID:
451✔
770
      return &pStreamRuntimeFuncInfo->groupId;
451✔
771
    case FUNCTION_TYPE_PLACEHOLDER_COLUMN:
1,986✔
772
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
1,986✔
773
    case FUNCTION_TYPE_PLACEHOLDER_TBNAME:
13,008✔
774
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
13,008✔
775
    default:
×
776
      break;
×
777
  }
778
  return NULL;
×
779
}
780

781
int32_t fmGetStreamPesudoFuncEnv(int32_t funcId, SNodeList* pParamNodes, SFuncExecEnv *pEnv) {
14,320✔
782
  if (NULL == pParamNodes || pParamNodes->length < 1) {
14,320!
783
    uError("invalid stream pesudo func param list %p", pParamNodes);
×
784
    return TSDB_CODE_INTERNAL_ERROR;
×
785
  }
786

787
  int32_t firstParamType = nodeType(nodesListGetNode(pParamNodes, 0));
14,320✔
788
  if (QUERY_NODE_VALUE != firstParamType) {
14,320!
789
    uError("invalid stream pesudo func first param type %d", firstParamType);
×
790
    return TSDB_CODE_INTERNAL_ERROR;
×
791
  }
792

793
  SValueNode* pResDesc = (SValueNode*)nodesListGetNode(pParamNodes, 0);
14,320✔
794
  pEnv->calcMemSize = pResDesc->node.resType.bytes;
14,320✔
795

796
  return TSDB_CODE_SUCCESS;
14,320✔
797
}
798

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

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

867
        (void)memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
13,008✔
868
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
13,008✔
869
        break;
13,008✔
870
      }
871
    }
872
  } else if (LIST_LENGTH(pParamNodes) == 1) {
338,347!
873
    // twstart, twend
874
    const void* pVal = fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
169,174✔
875
    if (!pVal) {
169,174!
876
      uError("failed to set stream pseudo func param val, NULL val for funcId: %d", funcId);
×
877
      return TSDB_CODE_INTERNAL_ERROR;
×
878
    }
879
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, (void*)pVal);
169,174✔
880
    if (code != 0) {
169,174!
881
      uError("failed to set value node value: %s", tstrerror(code));
×
882
      return code;
×
883
    }
884
  } else {
885
    SNode* pSecondParam = nodesListGetNode(pParamNodes, 1);
×
886
    if (nodeType(pSecondParam) != QUERY_NODE_VALUE) {
×
887
      uError("invalid param node type: %d for func: %d", nodeType(pSecondParam), funcId);
×
888
      return TSDB_CODE_INTERNAL_ERROR;
×
889
    }
890
    int32_t idx = ((SValueNode*)pSecondParam)->datum.i;
×
891
    const SValue* pVal = taosArrayGet(pVals2, idx);
×
892
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(pVal, pFirstParam->type));
×
893
  }
894
  return code;
184,619✔
895
}
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