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

taosdata / TDengine / #4847

11 Nov 2025 05:50AM UTC coverage: 62.651% (+0.3%) from 62.306%
#4847

push

travis-ci

web-flow
Merge e78cd6509 into 47a2ea7a0

542 of 650 new or added lines in 16 files covered. (83.38%)

1515 existing lines in 91 files now uncovered.

113826 of 181682 relevant lines covered (62.65%)

113230552.12 hits per line

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

75.65
/source/libs/function/src/functionMgt.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "functionMgt.h"
17

18
#include "builtins.h"
19
#include "builtinsimpl.h"
20
#include "functionMgtInt.h"
21
#include "taos.h"
22
#include "taoserror.h"
23
#include "thash.h"
24
#include "tudf.h"
25

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

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

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

42
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
362,363,382✔
43
    if (TSDB_CODE_SUCCESS != taosHashPut(gFunMgtService.pFuncNameHashTable, funcMgtBuiltins[i].name,
360,646,020✔
44
                                         strlen(funcMgtBuiltins[i].name), &i, sizeof(int32_t))) {
360,646,020✔
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);
20,593,264✔
56
  }
57
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,147,483,647✔
58
    return false;
2,147,483,647✔
59
  }
60
  return FUNC_MGT_TEST_MASK(funcMgtBuiltins[funcId].classification, classification);
2,147,483,647✔
61
}
62

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

68
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
325,602,314✔
69
  if (NULL != gFunMgtService.pFuncNameHashTable) {
325,602,314✔
70
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
325,258,738✔
71
    if (NULL != pVal) {
325,259,895✔
72
      pFunc->funcId = *(int32_t*)pVal;
324,717,296✔
73
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
324,716,583✔
74
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
324,713,844✔
75
    }
76
    return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
542,599✔
77
  }
78
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
6,397,876✔
79
    if (0 == strcmp(funcMgtBuiltins[i].name, pFunc->functionName)) {
6,398,400✔
80
      pFunc->funcId = i;
344,100✔
81
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
344,100✔
82
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
344,100✔
83
    }
84
  }
85
  return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
×
86
}
87

88
EFuncReturnRows fmGetFuncReturnRows(SFunctionNode* pFunc) {
8,970,770✔
89
  if (NULL != funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc) {
8,970,770✔
90
    return funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc(pFunc);
5,912,070✔
91
  }
92
  return (fmIsIndefiniteRowsFunc(pFunc->funcId) || fmIsMultiRowsFunc(pFunc->funcId)) ? FUNC_RETURN_ROWS_INDEFINITE
3,058,700✔
93
                                                                                     : FUNC_RETURN_ROWS_NORMAL;
3,058,700✔
94
}
95

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

100
EFunctionType fmGetFuncType(const char* pFunc) {
252,386,663✔
101
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
252,386,663✔
102
  if (NULL != pVal) {
252,386,370✔
103
    return funcMgtBuiltins[*(int32_t*)pVal].type;
251,834,869✔
104
  }
105
  return FUNCTION_TYPE_UDF;
551,501✔
106
}
107

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

115
EFuncDataRequired fmFuncDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
10,137,419✔
116
  if (fmIsUserDefinedFunc(pFunc->funcId) || pFunc->funcId < 0 || pFunc->funcId >= funcMgtBuiltinsNum) {
10,137,419✔
117
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
118
  }
119
  if (NULL == funcMgtBuiltins[pFunc->funcId].dataRequiredFunc) {
10,137,165✔
120
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
121
  }
122
  return funcMgtBuiltins[pFunc->funcId].dataRequiredFunc(pFunc, pTimeWindow);
10,137,419✔
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;
8✔
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;
44,215✔
133
    ;
134
  }
135

136
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
2,147,483,647✔
137
    return FUNC_DATA_REQUIRED_DATA_LOAD;
17,150,328✔
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) {
127,722,625✔
144
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
127,722,625✔
145
    return TSDB_CODE_FAILED;
18,453✔
146
  }
147
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
127,706,607✔
148
  pFpSet->init = funcMgtBuiltins[funcId].initFunc;
127,700,346✔
149
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
127,713,778✔
150
  pFpSet->finalize = funcMgtBuiltins[funcId].finalizeFunc;
127,702,258✔
151
  pFpSet->combine = funcMgtBuiltins[funcId].combineFunc;
127,684,412✔
152
  pFpSet->processFuncByRow = funcMgtBuiltins[funcId].processFuncByRow;
127,692,029✔
153
  pFpSet->cleanup = funcMgtBuiltins[funcId].cleanupFunc;
127,719,608✔
154
  return TSDB_CODE_SUCCESS;
127,708,179✔
155
}
156

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

172
int32_t fmGetScalarFuncExecFuncs(int32_t funcId, SScalarFuncExecFuncs* pFpSet) {
601,544,995✔
173
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
601,544,995✔
174
    return TSDB_CODE_FAILED;
100,746✔
175
  }
176
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
601,602,224✔
177
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
601,640,836✔
178
  return TSDB_CODE_SUCCESS;
601,635,927✔
179
}
180

181
bool fmIsAggFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_AGG_FUNC); }
874,710,681✔
182

183
bool fmIsScalarFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCALAR_FUNC); }
1,213,247,553✔
184

185
bool fmIsVectorFunc(int32_t funcId) { return !fmIsScalarFunc(funcId) && !fmIsPseudoColumnFunc(funcId); }
466,437,112✔
186

187
bool fmIsSelectColsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_COLS_FUNC); }
42,396,392✔
188

189
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
936,305,620✔
190

191
bool fmIsTimelineFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TIMELINE_FUNC); }
481,329,398✔
192

193
bool fmIsDateTimeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_DATETIME_FUNC); }
244,802,353✔
194

195
bool fmIsPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PSEUDO_COLUMN_FUNC); }
1,030,079,277✔
196

197
bool fmIsScanPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCAN_PC_FUNC); }
1,048,057,075✔
198

199
bool fmIsWindowPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_WINDOW_PC_FUNC); }
553,574,839✔
200

201
bool fmIsWindowClauseFunc(int32_t funcId) { return fmIsAggFunc(funcId) || fmIsWindowPseudoColumnFunc(funcId); }
16,801,150✔
202

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

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

207
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
650,965,004✔
208

209
bool fmIsSpecialDataRequiredFunc(int32_t funcId) {
31,900,955✔
210
  return isSpecificClassifyFunc(funcId, FUNC_MGT_SPECIAL_DATA_REQUIRED);
31,900,955✔
211
}
212

213
bool fmIsDynamicScanOptimizedFunc(int32_t funcId) {
17,447,824✔
214
  return isSpecificClassifyFunc(funcId, FUNC_MGT_DYNAMIC_SCAN_OPTIMIZED);
17,447,824✔
215
}
216

217
bool fmIsMultiResFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_RES_FUNC); }
502,785,249✔
218

219
bool fmIsRepeatScanFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_REPEAT_SCAN_FUNC); }
488,448,487✔
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); }
244,951,030✔
224

225
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
45,146,520✔
226

227
bool fmIsSystemInfoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SYSTEM_INFO_FUNC); }
246,572,493✔
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); }
246,453,311✔
232

233
bool fmIsMultiRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_ROWS_FUNC); }
486,168,567✔
234

235
bool fmIsKeepOrderFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_KEEP_ORDER_FUNC); }
83,420,634✔
236

237
bool fmIsCumulativeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CUMULATIVE_FUNC); }
43,509,329✔
238

239
bool fmIsForbidSysTableFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_SYSTABLE_FUNC); }
244,914,298✔
240

241
bool fmIsInterpFunc(int32_t funcId) {
500,684,124✔
242
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
500,684,124✔
243
    return false;
1,064,487✔
244
  }
245
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
499,619,637✔
246
}
247

248
bool fmIsInterpPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERP_PC_FUNC); }
487,383,726✔
249

250
bool fmIsForecastFunc(int32_t funcId) {
477,534,774✔
251
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
477,534,774✔
252
    return false;
1,066,221✔
253
  }
254
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
476,468,553✔
255
}
256

257
bool fmIsAnalysisPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_ANALYTICS_PC_FUNC); }
488,747,574✔
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) {
268
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
269
    return false;
27,599,252✔
270
  }
27,599,252✔
271
  return FUNCTION_TYPE_LAST_ROW == funcMgtBuiltins[funcId].type;
20✔
272
}
273

27,599,469✔
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) {
282
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
283
    return false;
206,719,684✔
284
  }
206,719,684✔
285
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
60,184✔
286
         FUNCTION_TYPE_CACHE_LAST == funcMgtBuiltins[funcId].type ||
287
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
206,790,133✔
288
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
195,043,057✔
289
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
194,553,263✔
290
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
187,711,826✔
291
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
184,464,943✔
292
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
178,037,229✔
293
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
174,882,276✔
294
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
173,342,351✔
295
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
148,977,996✔
296
}
549,658,959✔
297

147,939,167✔
298
bool fmIsSelectValueFunc(int32_t funcId) {
299
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
300
    return false;
24,881,909✔
301
  }
24,881,909✔
302
  return FUNCTION_TYPE_SELECT_VALUE == funcMgtBuiltins[funcId].type;
×
303
}
304

24,881,909✔
305
bool fmIsGroupKeyFunc(int32_t funcId) {
306
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
307
    return false;
176,564,721✔
308
  }
176,564,721✔
309
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
×
310
}
311

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

20,262✔
319
bool fmIsElapsedFunc(int32_t funcId) {
320
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
321
    return false;
11,114,759✔
322
  }
11,114,759✔
323
  return FUNCTION_TYPE_ELAPSED == funcMgtBuiltins[funcId].type;
×
324
}
325

11,114,759✔
326
bool fmIsBlockDistFunc(int32_t funcId) {
327
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
328
    return false;
244,801,864✔
329
  }
244,801,864✔
330
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
532,618✔
331
}
332

244,269,246✔
333
bool fmIsDBUsageFunc(int32_t funcId) {
334
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
335
    return false;
244,801,143✔
336
  }
244,801,143✔
337
  return FUNCTION_TYPE_DB_USAGE == funcMgtBuiltins[funcId].type;
532,857✔
338
}
339

244,268,286✔
340
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
341

342
bool fmIsIgnoreNullFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IGNORE_NULL_FUNC); }
258,402,962✔
343

344
void fmFuncMgtDestroy() {
161,046✔
345
  void* m = gFunMgtService.pFuncNameHashTable;
346
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
1,717,638✔
347
    taosHashCleanup(m);
1,717,638✔
348
  }
1,717,638✔
349
}
1,717,362✔
350

351
#ifdef BUILD_NO_CALL
1,717,638✔
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) {
363
  bool res = false;
364
  switch (funcMgtBuiltins[funcId].type) {
13,969,903✔
365
    case FUNCTION_TYPE_MAX:
13,969,903✔
366
    case FUNCTION_TYPE_MIN:
13,969,903✔
367
    case FUNCTION_TYPE_TOP:
4,932,562✔
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;
375
      break;
376
    default:
4,932,562✔
377
      break;
4,932,562✔
378
  }
9,037,608✔
379
  return res;
9,037,608✔
380
}
381

13,970,170✔
382
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
383
  SNode* pNode;
384
  FOREACH(pNode, pFunc->pParameterList) {
2,732,085✔
385
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
386
      return false;
2,748,777✔
387
    }
20,180✔
388
  }
3,488✔
389
  return true;
390
}
391

2,728,597✔
392
bool fmIsSkipScanCheckFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SKIP_SCAN_CHECK_FUNC); }
393

394
bool fmIsPrimaryKeyFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PRIMARY_KEY_FUNC); }
17,448,078✔
395

396
bool fmIsPlaceHolderFunc(int32_t funcId) {return isSpecificClassifyFunc(funcId, FUNC_MGT_PLACE_HOLDER_FUNC); }
242,165,609✔
397

398
void getLastCacheDataType(SDataType* pType, int32_t pkBytes) {
2,147,483,647✔
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) {
×
405
  char msg[128] = {0};
406
  return fmGetFuncInfo(pFunc, msg, sizeof(msg));
45,427,041✔
407
}
45,427,041✔
408

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

3,517,332✔
426
static void resetOutputChangedFunc(SFunctionNode *pFunc, const SFunctionNode* pSrcFunc) {
427
  if (funcMgtBuiltins[pFunc->funcId].type == FUNCTION_TYPE_LAST_MERGE) {
428
    pFunc->node.resType = pSrcFunc->node.resType;
41,910,243✔
429
    return;
41,910,243✔
430
  }
8,173,128✔
431
}
8,172,861✔
432

433
int32_t createFunctionWithSrcFunc(const char* pName, const SFunctionNode* pSrcFunc, SNodeList* pParameterList, SFunctionNode** ppFunc) {
434
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
435
  if (NULL == *ppFunc) {
41,909,442✔
436
    return code;
41,909,442✔
437
  }
41,910,510✔
438

×
439
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
440
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
441
  (*ppFunc)->pSrcFuncRef = pSrcFunc;
41,910,243✔
442

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

41,909,976✔
458
static int32_t createColumnByFunc(const SFunctionNode* pFunc, SColumnNode** ppCol) {
459
  int32_t code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)ppCol);
460
  if (NULL == *ppCol) {
27,940,073✔
461
    return code;
27,940,073✔
462
  }
27,940,340✔
463
  tstrncpy((*ppCol)->colName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
×
464
  (*ppCol)->node.resType = pFunc->node.resType;
465
  return TSDB_CODE_SUCCESS;
27,940,340✔
466
}
27,939,539✔
467

27,939,806✔
468
bool fmIsDistExecFunc(int32_t funcId) {
469
  if (fmIsUserDefinedFunc(funcId)) {
470
    return false;
91,002,795✔
471
  }
91,002,795✔
472
  if (!fmIsVectorFunc(funcId)) {
310,230✔
473
    return true;
474
  }
90,691,887✔
475
  return (NULL != funcMgtBuiltins[funcId].pPartialFunc && NULL != funcMgtBuiltins[funcId].pMergeFunc);
248,446✔
476
}
477

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

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

13,969,903✔
502
static int32_t createMergeFuncPara(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
503
                                   SNodeList** pParameterList) {
504
  SNode*  pRes = NULL;
27,940,073✔
505
  int32_t code = createColumnByFunc(pPartialFunc, (SColumnNode**)&pRes);
506
  if (TSDB_CODE_SUCCESS != code) {
27,940,073✔
507
    return code;
27,940,340✔
508
  }
27,940,073✔
509
  if (NULL != funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc) {
×
510
    return funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc(pSrcFunc->pParameterList, pRes, pParameterList);
511
  } else {
27,940,073✔
512
    return nodesListMakeStrictAppend(pParameterList, pRes);
264,756✔
513
  }
514
}
27,674,516✔
515

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

13,969,903✔
521
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
13,970,170✔
522
  if (TSDB_CODE_SUCCESS == code) {
523
    if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){
13,970,170✔
524
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pSrcFunc, pParameterList, &pFunc);
13,970,170✔
525
    }else{
13,970,170✔
526
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
1,784,932✔
527
    }
528
  }
12,184,971✔
529
  if (TSDB_CODE_SUCCESS == code) {
530
    tstrncpy(pFunc->node.aliasName, pPartialFunc->node.aliasName, TSDB_COL_NAME_LEN);
531
  }
13,969,369✔
532

13,969,636✔
533
  if (TSDB_CODE_SUCCESS == code) {
534
    *pMidFunc = pFunc;
535
  } else {
13,969,636✔
536
    nodesDestroyList(pParameterList);
13,969,636✔
537
  }
538
  return code;
×
539
}
540

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

13,969,903✔
546
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
13,969,903✔
547
  if (TSDB_CODE_SUCCESS == code) {
548
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
13,969,636✔
549
  }
13,970,170✔
550
  if (TSDB_CODE_SUCCESS == code) {
13,969,903✔
551
    pFunc->hasOriginalFunc = true;
552
    pFunc->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
13,970,170✔
553
    // overwrite function restype set by translate function
13,970,170✔
554
    if (fmIsSameInOutType(pSrcFunc->funcId)) {
13,970,170✔
555
      pFunc->node.resType = pSrcFunc->node.resType;
556
    }
13,969,903✔
557
    tstrncpy(pFunc->node.aliasName, pSrcFunc->node.aliasName, TSDB_COL_NAME_LEN);
4,932,562✔
558
  }
559

13,970,170✔
560
  if (TSDB_CODE_SUCCESS == code) {
561
    *pMergeFunc = pFunc;
562
  } else {
13,969,636✔
563
    nodesDestroyList(pParameterList);
13,969,636✔
564
  }
565
  return code;
×
566
}
567

13,969,903✔
568
int32_t fmGetDistMethod(const SFunctionNode* pFunc, SFunctionNode** pPartialFunc, SFunctionNode** pMidFunc,
569
                        SFunctionNode** pMergeFunc) {
570
  if (!fmIsDistExecFunc(pFunc->funcId)) {
13,969,351✔
571
    return TSDB_CODE_FAILED;
572
  }
13,969,351✔
573

×
574
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
575
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
576
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
13,969,903✔
577
  }
13,970,170✔
578
  if (TSDB_CODE_SUCCESS == code) {
13,970,170✔
579
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
580
  }
13,970,170✔
581

13,970,170✔
582
  if (TSDB_CODE_SUCCESS != code) {
583
    nodesDestroyNode((SNode*)*pPartialFunc);
584
    if (pMidFunc) nodesDestroyNode((SNode*)*pMidFunc);
13,970,170✔
585
    nodesDestroyNode((SNode*)*pMergeFunc);
×
586
  }
×
587

×
588
  return code;
589
}
590

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

669,600✔
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) {
601
  if (funcMgtBuiltins[pFunc->funcId].pStateFunc) {
602
    SNodeList* pParams = NULL;
2,816✔
603
    int32_t    code = nodesCloneList(pFunc->pParameterList, &pParams);
2,816✔
604
    if (!pParams) return code;
2,816✔
605
    code = createFunction(funcMgtBuiltins[pFunc->funcId].pStateFunc, pParams, pStateFunc);
2,816✔
606
    if (TSDB_CODE_SUCCESS != code) {
2,816✔
607
      nodesDestroyList(pParams);
2,816✔
608
      return code;
2,816✔
609
    }
×
610
    tstrncpy((*pStateFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
×
611
    tstrncpy((*pStateFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
612
  }
2,816✔
613
  return TSDB_CODE_SUCCESS;
2,816✔
614
}
615

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

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

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

1,408✔
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) {
687
  if (NULL != gFunMgtService.pFuncNameHashTable) {
688
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, name, strlen(name));
1,117,716✔
689
    if (NULL != pVal) {
1,117,716✔
690
      return *(int32_t*)pVal;
1,117,716✔
691
    }
1,117,716✔
692
    return -1;
1,117,716✔
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) {
718
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
719
}
143,748,740✔
720

143,748,740✔
721
bool fmIsRowTsOriginFunc(int32_t funcId) {
722
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
723
    return false;
2,757,454✔
724
  }
2,757,454✔
725
  return FUNCTION_TYPE_IROWTS_ORIGIN == funcMgtBuiltins[funcId].type;
476✔
726
}
727

2,756,978✔
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) {
736
  SSTriggerCalcParam *pParams = taosArrayGet(pStreamRuntimeFuncInfo->pStreamPesudoFuncVals, pStreamRuntimeFuncInfo->curIdx);
737
  switch (funcMgtBuiltins[funcId].type) {
22,062,393✔
738
    case FUNCTION_TYPE_TPREV_TS:
22,062,393✔
739
      return &pParams->prevTs;
22,067,472✔
740
    case FUNCTION_TYPE_TCURRENT_TS:
×
741
      return &pParams->currentTs;
×
742
    case FUNCTION_TYPE_TNEXT_TS:
5,065,604✔
743
      return &pParams->nextTs;
5,065,604✔
744
    case FUNCTION_TYPE_TWSTART:
×
745
      return &pParams->wstart;
×
746
    case FUNCTION_TYPE_TWEND:
11,965,657✔
747
      return &pParams->wend;
11,965,657✔
748
    case FUNCTION_TYPE_TWDURATION:
65,526✔
749
      return &pParams->wduration;
65,526✔
750
    case FUNCTION_TYPE_TWROWNUM:
5,494✔
751
      return &pParams->wrownum;
5,494✔
752
    case FUNCTION_TYPE_TPREV_LOCALTIME:
43,650✔
753
      return &pParams->prevLocalTime;
43,650✔
754
    case FUNCTION_TYPE_TLOCALTIME:
401✔
755
      return &pParams->triggerTime;
401✔
756
    case FUNCTION_TYPE_TNEXT_LOCALTIME:
59,220✔
757
      return &pParams->nextLocalTime;
59,220✔
758
    case FUNCTION_TYPE_TGRPID:
401✔
759
      return &pStreamRuntimeFuncInfo->groupId;
401✔
760
    case FUNCTION_TYPE_PLACEHOLDER_COLUMN:
99,179✔
761
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
99,179✔
762
    case FUNCTION_TYPE_PLACEHOLDER_TBNAME:
656,909✔
763
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
656,909✔
764
    default:
4,100,798✔
765
      break;
4,100,798✔
766
  }
×
767
  return NULL;
×
768
}
769

×
770
bool fmIsStreamPesudoColVal(int32_t funcId) {
771
  return funcMgtBuiltins[funcId].type == FUNCTION_TYPE_PLACEHOLDER_COLUMN
772
         || funcMgtBuiltins[funcId].type == FUNCTION_TYPE_PLACEHOLDER_TBNAME;
126,880✔
773
}
126,880✔
774

126,880✔
775
int32_t fmGetStreamPesudoFuncEnv(int32_t funcId, SNodeList* pParamNodes, SFuncExecEnv *pEnv) {
776
  if (NULL == pParamNodes || pParamNodes->length < 1) {
777
    uError("invalid stream pesudo func param list %p", pParamNodes);
9,763,536✔
778
    return TSDB_CODE_INTERNAL_ERROR;
9,763,536✔
UNCOV
779
  }
×
780

×
781
  int32_t firstParamType = nodeType(nodesListGetNode(pParamNodes, 0));
782
  if (QUERY_NODE_VALUE != firstParamType) {
783
    uError("invalid stream pesudo func first param type %d", firstParamType);
9,763,950✔
784
    return TSDB_CODE_INTERNAL_ERROR;
9,764,360✔
785
  }
×
786

×
787
  SValueNode* pResDesc = (SValueNode*)nodesListGetNode(pParamNodes, 0);
788
  pEnv->calcMemSize = pResDesc->node.resType.bytes;
789

9,764,360✔
790
  return TSDB_CODE_SUCCESS;
9,764,774✔
791
}
792

9,765,339✔
793

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

×
807
  int32_t t = funcMgtBuiltins[funcId].type;
808
  uDebug("set stream pseudo func param val, functype: %d, pStreamRuntimeInfo: %p", t, pStreamRuntimeInfo);
809
  if (FUNCTION_TYPE_TGRPID == t) {
22,060,410✔
810
    SValue v = {0};
22,062,011✔
811
    v.type = TSDB_DATA_TYPE_BIGINT;
22,058,777✔
812
    v.val = *(int64_t*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
99,179✔
813
    
99,179✔
814
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&v, pFirstParam->type));
99,179✔
815
    if (code != 0) {
816
      uError("failed to set value node value: %s", tstrerror(code));
99,179✔
817
      return code;
99,179✔
818
    }
×
819
  } else if (FUNCTION_TYPE_PLACEHOLDER_COLUMN == t) {
×
820
    SNode* pSecondParam = nodesListGetNode(pParamNodes, 1);
821
    if (nodeType(pSecondParam) != QUERY_NODE_VALUE) {
21,959,598✔
822
      uError("invalid param node type: %d for func: %d", nodeType(pSecondParam), funcId);
656,909✔
823
      return TSDB_CODE_INTERNAL_ERROR;
656,909✔
824
    }
×
825
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
×
826
    int32_t idx = ((SValueNode*)pSecondParam)->datum.i;
827
    if (idx - 1 < 0 || idx - 1 >= taosArrayGetSize(pVal)) {
656,909✔
828
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
656,909✔
829
      return TSDB_CODE_INTERNAL_ERROR;
656,909✔
830
    }
×
831
    SStreamGroupValue* pValue = taosArrayGet(pVal, idx - 1);
×
832
    if (pValue == NULL) {
833
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
656,909✔
834
      return TSDB_CODE_INTERNAL_ERROR;
656,909✔
835
    }
×
836
    if (!pValue->isNull){
×
837
      if (pValue->data.type != ((SValueNode*)pFirstParam)->node.resType.type){
838
        uError("invalid value type: %d for func: %d, should be: %d", pValue->data.type, funcId, ((SValueNode*)pFirstParam)->node.resType.type);
656,909✔
839
        return TSDB_CODE_INTERNAL_ERROR;
656,909✔
840
      }
×
841
      if (IS_VAR_DATA_TYPE(((SValueNode*)pFirstParam)->node.resType.type)) {
×
842
        char* tmp = taosMemoryCalloc(1, pValue->data.nData + VARSTR_HEADER_SIZE); 
843
        if (tmp == NULL) {
656,909✔
844
          return TSDB_CODE_OUT_OF_MEMORY;
491,065✔
845
        }
491,065✔
846
        taosMemoryFree(((SValueNode*)pFirstParam)->datum.p);
×
847
        ((SValueNode*)pFirstParam)->datum.p = tmp;
848
        memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
491,065✔
849
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
491,065✔
850
      } else {
491,065✔
851
        code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&pValue->data, pValue->data.type));
491,065✔
852
      }
853
    }
165,844✔
854
    ((SValueNode*)pFirstParam)->isNull = pValue->isNull;
855
  } else if (FUNCTION_TYPE_PLACEHOLDER_TBNAME == t) {
856
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
656,909✔
857
    for (int32_t i = 0; i < taosArrayGetSize(pVal); ++i) {
21,302,689✔
858
      SStreamGroupValue* pValue = taosArrayGet(pVal, i);
4,099,528✔
859
      if (pValue != NULL && pValue->isTbname) {
4,101,226✔
860
        taosMemoryFreeClear(((SValueNode*)pFirstParam)->datum.p);
4,102,077✔
861
        ((SValueNode*)pFirstParam)->datum.p = taosMemoryCalloc(pValue->data.nData + VARSTR_HEADER_SIZE, 1);
4,102,933✔
862
        if (NULL == ((SValueNode*)pFirstParam)->datum.p ) {
4,101,649✔
863
          return terrno;
4,099,514✔
864
        }
4,102,077✔
865

×
866
        (void)memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
867
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
868
        break;
4,101,221✔
869
      }
4,101,649✔
870
    }
4,102,519✔
871
  } else if (LIST_LENGTH(pParamNodes) == 1) {
872
    // twstart, twend
873
    const void* pVal = fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
34,409,975✔
874
    if (!pVal) {
875
      uError("failed to set stream pseudo func param val, NULL val for funcId: %d", funcId);
17,206,311✔
876
      return TSDB_CODE_INTERNAL_ERROR;
17,207,237✔
877
    }
×
878
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, (void*)pVal);
×
879
    if (code != 0) {
880
      uError("failed to set value node value: %s", tstrerror(code));
17,207,237✔
881
      return code;
17,207,201✔
882
    }
387✔
883
  } else {
×
884
    uError("invalid placeholder function type: %d", t);
885
    return TSDB_CODE_INTERNAL_ERROR;
886
  }
×
887
  
×
888
  return code;
889
}
890

22,062,979✔
891

892

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