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

taosdata / TDengine / #4885

15 Dec 2025 03:26AM UTC coverage: 65.258% (+4.6%) from 60.617%
#4885

push

travis-ci

web-flow
feat(tmq): [TS-6379]remove limition for table operation in tmq  (#33834)

872 of 1074 new or added lines in 16 files covered. (81.19%)

659 existing lines in 92 files now uncovered.

177890 of 272597 relevant lines covered (65.26%)

103732965.73 hits per line

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

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

42
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
366,039,072✔
43
    if (TSDB_CODE_SUCCESS != taosHashPut(gFunMgtService.pFuncNameHashTable, funcMgtBuiltins[i].name,
364,404,969✔
44
                                         strlen(funcMgtBuiltins[i].name), &i, sizeof(int32_t))) {
364,404,969✔
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);
8,276,617✔
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,634,103✔
64
  (void)taosThreadOnce(&functionHashTableInit, doInitFunctionTable);
1,634,103✔
65
  return initFunctionCode;
1,634,103✔
66
}
67

68
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
310,051,159✔
69
  if (NULL != gFunMgtService.pFuncNameHashTable) {
310,051,159✔
70
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
309,875,428✔
71
    if (NULL != pVal) {
309,876,888✔
72
      pFunc->funcId = *(int32_t*)pVal;
309,675,627✔
73
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
309,674,835✔
74
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
309,672,660✔
75
    }
76
    return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
201,261✔
77
  }
78
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
3,281,001✔
79
    if (0 == strcmp(funcMgtBuiltins[i].name, pFunc->functionName)) {
3,281,760✔
80
      pFunc->funcId = i;
176,490✔
81
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
176,490✔
82
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
176,490✔
83
    }
84
  }
UNCOV
85
  return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
×
86
}
87

88
EFuncReturnRows fmGetFuncReturnRows(SFunctionNode* pFunc) {
8,531,319✔
89
  if (NULL != funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc) {
8,531,319✔
90
    return funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc(pFunc);
5,741,004✔
91
  }
92
  return (fmIsIndefiniteRowsFunc(pFunc->funcId) || fmIsMultiRowsFunc(pFunc->funcId)) ? FUNC_RETURN_ROWS_INDEFINITE
2,789,825✔
93
                                                                                     : FUNC_RETURN_ROWS_NORMAL;
2,789,825✔
94
}
95

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

100
EFunctionType fmGetFuncType(const char* pFunc) {
246,471,674✔
101
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
246,471,674✔
102
  if (NULL != pVal) {
246,470,950✔
103
    return funcMgtBuiltins[*(int32_t*)pVal].type;
246,266,475✔
104
  }
105
  return FUNCTION_TYPE_UDF;
204,475✔
106
}
107

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

115
EFuncDataRequired fmFuncDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
10,046,575✔
116
  if (fmIsUserDefinedFunc(pFunc->funcId) || pFunc->funcId < 0 || pFunc->funcId >= funcMgtBuiltinsNum) {
10,046,575✔
117
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
118
  }
119
  if (NULL == funcMgtBuiltins[pFunc->funcId].dataRequiredFunc) {
10,044,972✔
120
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
121
  }
122
  return funcMgtBuiltins[pFunc->funcId].dataRequiredFunc(pFunc, pTimeWindow);
10,045,768✔
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;
794✔
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,743✔
133
    ;
134
  }
135

136
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
2,147,483,647✔
137
    return FUNC_DATA_REQUIRED_DATA_LOAD;
23,533,595✔
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) {
125,744,939✔
144
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
125,744,939✔
145
    return TSDB_CODE_FAILED;
22✔
146
  }
147
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
125,754,268✔
148
  pFpSet->init = funcMgtBuiltins[funcId].initFunc;
125,715,175✔
149
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
125,721,979✔
150
  pFpSet->finalize = funcMgtBuiltins[funcId].finalizeFunc;
125,714,175✔
151
  pFpSet->combine = funcMgtBuiltins[funcId].combineFunc;
125,715,251✔
152
  pFpSet->processFuncByRow = funcMgtBuiltins[funcId].processFuncByRow;
125,740,124✔
153
  pFpSet->cleanup = funcMgtBuiltins[funcId].cleanupFunc;
125,712,854✔
154
  return TSDB_CODE_SUCCESS;
125,723,238✔
155
}
156

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

172
int32_t fmGetScalarFuncExecFuncs(int32_t funcId, SScalarFuncExecFuncs* pFpSet) {
659,114,664✔
173
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
659,114,664✔
174
    return TSDB_CODE_FAILED;
37,062✔
175
  }
176
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
659,308,803✔
177
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
659,242,318✔
178
  return TSDB_CODE_SUCCESS;
659,258,027✔
179
}
180

181
bool fmIsAggFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_AGG_FUNC); }
850,925,522✔
182

183
bool fmIsScalarFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCALAR_FUNC); }
1,146,998,979✔
184

185
bool fmIsVectorFunc(int32_t funcId) { return !fmIsScalarFunc(funcId) && !fmIsPseudoColumnFunc(funcId); }
446,500,449✔
186

187
bool fmIsSelectColsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_COLS_FUNC); }
34,105,117✔
188

189
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
886,470,521✔
190

191
bool fmIsTimelineFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TIMELINE_FUNC); }
468,209,500✔
192

193
bool fmIsDateTimeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_DATETIME_FUNC); }
238,217,512✔
194

195
bool fmIsPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PSEUDO_COLUMN_FUNC); }
978,317,179✔
196

197
bool fmIsScanPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCAN_PC_FUNC); }
1,065,320,831✔
198

199
bool fmIsWindowPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_WINDOW_PC_FUNC); }
538,092,213✔
200

201
bool fmIsWindowClauseFunc(int32_t funcId) { return fmIsAggFunc(funcId) || fmIsWindowPseudoColumnFunc(funcId); }
12,080,530✔
202

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

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

207
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
634,403,983✔
208

209
bool fmIsSpecialDataRequiredFunc(int32_t funcId) {
30,450,419✔
210
  return isSpecificClassifyFunc(funcId, FUNC_MGT_SPECIAL_DATA_REQUIRED);
30,450,419✔
211
}
212

213
bool fmIsDynamicScanOptimizedFunc(int32_t funcId) {
16,209,499✔
214
  return isSpecificClassifyFunc(funcId, FUNC_MGT_DYNAMIC_SCAN_OPTIMIZED);
16,209,499✔
215
}
216

217
bool fmIsMultiResFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_RES_FUNC); }
480,455,227✔
218

219
bool fmIsRepeatScanFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_REPEAT_SCAN_FUNC); }
475,408,687✔
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); }
238,380,395✔
224

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

227
bool fmIsSystemInfoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SYSTEM_INFO_FUNC); }
240,109,409✔
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); }
239,992,243✔
232

233
bool fmIsMultiRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_ROWS_FUNC); }
473,053,066✔
234

235
bool fmIsKeepOrderFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_KEEP_ORDER_FUNC); }
81,866,508✔
236

237
bool fmIsCumulativeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CUMULATIVE_FUNC); }
35,092,066✔
238

239
bool fmIsForbidSysTableFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_SYSTABLE_FUNC); }
238,331,701✔
240

241
bool fmIsInterpFunc(int32_t funcId) {
487,800,104✔
242
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
487,800,104✔
243
    return false;
393,316✔
244
  }
245
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
487,406,788✔
246
}
247

248
bool fmIsInterpPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERP_PC_FUNC); }
471,686,746✔
249

250
bool fmIsForecastFunc(int32_t funcId) {
464,824,369✔
251
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
464,824,369✔
252
    return false;
395,739✔
253
  }
254
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
464,428,630✔
255
}
256

257
bool fmIsAnalysisPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_ANALYTICS_PC_FUNC); }
473,047,812✔
258

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

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

269
bool fmIsLastRowFunc(int32_t funcId) {
21,657,623✔
270
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
21,657,623✔
UNCOV
271
    return false;
×
272
  }
273
  return FUNCTION_TYPE_LAST_ROW == funcMgtBuiltins[funcId].type;
21,658,091✔
274
}
275

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

283
bool fmIsNotNullOutputFunc(int32_t funcId) {
208,410,266✔
284
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
208,410,266✔
285
    return false;
59,238✔
286
  }
287
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
208,359,171✔
288
         FUNCTION_TYPE_CACHE_LAST == funcMgtBuiltins[funcId].type ||
197,284,599✔
289
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
196,801,171✔
290
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
189,466,331✔
291
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
186,049,503✔
292
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
180,214,781✔
293
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
176,938,938✔
294
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
175,323,987✔
295
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
151,291,583✔
296
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
555,791,527✔
297
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
150,163,808✔
298
}
299

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

307
bool fmIsGroupKeyFunc(int32_t funcId) {
45,177,528✔
308
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
45,177,528✔
309
    return false;
196✔
310
  }
311
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
45,177,823✔
312
}
313

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

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

328
bool fmIsBlockDistFunc(int32_t funcId) {
238,218,748✔
329
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
238,218,748✔
330
    return false;
196,816✔
331
  }
332
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
238,021,932✔
333
}
334

335
bool fmIsDBUsageFunc(int32_t funcId) {
238,217,990✔
336
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
238,217,990✔
337
    return false;
195,943✔
338
  }
339
  return FUNCTION_TYPE_DB_USAGE == funcMgtBuiltins[funcId].type;
238,022,047✔
340
}
341

342
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
252,685,273✔
343

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

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

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

363
// function has same input/output type
364
bool fmIsSameInOutType(int32_t funcId) {
13,427,849✔
365
  bool res = false;
13,427,849✔
366
  switch (funcMgtBuiltins[funcId].type) {
13,427,849✔
367
    case FUNCTION_TYPE_MAX:
4,875,225✔
368
    case FUNCTION_TYPE_MIN:
369
    case FUNCTION_TYPE_TOP:
370
    case FUNCTION_TYPE_BOTTOM:
371
    case FUNCTION_TYPE_FIRST:
372
    case FUNCTION_TYPE_LAST:
373
    case FUNCTION_TYPE_SAMPLE:
374
    case FUNCTION_TYPE_TAIL:
375
    case FUNCTION_TYPE_UNIQUE:
376
      res = true;
4,875,225✔
377
      break;
4,875,225✔
378
    default:
8,552,624✔
379
      break;
8,552,624✔
380
  }
381
  return res;
13,427,849✔
382
}
383

384
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
2,519,617✔
385
  SNode* pNode;
386
  FOREACH(pNode, pFunc->pParameterList) {
2,541,301✔
387
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
25,712✔
388
      return false;
4,028✔
389
    }
390
  }
391
  return true;
2,515,589✔
392
}
393

394
bool fmIsSkipScanCheckFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SKIP_SCAN_CHECK_FUNC); }
16,209,499✔
395

396
bool fmIsPrimaryKeyFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PRIMARY_KEY_FUNC); }
231,237,306✔
397

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

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

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

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

428
static void resetOutputChangedFunc(SFunctionNode *pFunc, const SFunctionNode* pSrcFunc) {
40,283,547✔
429
  if (funcMgtBuiltins[pFunc->funcId].type == FUNCTION_TYPE_LAST_MERGE) {
40,283,547✔
430
    pFunc->node.resType = pSrcFunc->node.resType;
8,116,814✔
431
    return;
8,116,814✔
432
  }
433
}
434

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

441
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
40,283,547✔
442
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
40,283,547✔
443
  (*ppFunc)->pSrcFuncRef = pSrcFunc;
40,283,547✔
444

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

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

470
bool fmIsDistExecFunc(int32_t funcId) {
84,262,975✔
471
  if (fmIsUserDefinedFunc(funcId)) {
84,262,975✔
472
    return false;
121,461✔
473
  }
474
  if (!fmIsVectorFunc(funcId)) {
84,142,786✔
475
    return true;
199,284✔
476
  }
477
  return (NULL != funcMgtBuiltins[funcId].pPartialFunc && NULL != funcMgtBuiltins[funcId].pMergeFunc);
83,942,517✔
478
}
479

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

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

504
static int32_t createMergeFuncPara(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
26,855,698✔
505
                                   SNodeList** pParameterList) {
506
  SNode*  pRes = NULL;
26,855,698✔
507
  int32_t code = createColumnByFunc(pPartialFunc, (SColumnNode**)&pRes);
26,855,698✔
508
  if (TSDB_CODE_SUCCESS != code) {
26,855,429✔
509
    return code;
×
510
  }
511
  if (NULL != funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc) {
26,855,429✔
512
    return funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc(pSrcFunc->pParameterList, pRes, pParameterList);
220,762✔
513
  } else {
514
    return nodesListMakeStrictAppend(pParameterList, pRes);
26,634,936✔
515
  }
516
}
517

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

523
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
13,427,849✔
524
  if (TSDB_CODE_SUCCESS == code) {
13,427,849✔
525
    if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){
13,427,849✔
526
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pSrcFunc, pParameterList, &pFunc);
1,397,793✔
527
    }else{
528
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
12,029,787✔
529
    }
530
  }
531
  if (TSDB_CODE_SUCCESS == code) {
13,427,849✔
532
    tstrncpy(pFunc->node.aliasName, pPartialFunc->node.aliasName, TSDB_COL_NAME_LEN);
13,427,849✔
533
  }
534

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

543
static int32_t createMergeFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
13,427,849✔
544
                                   SFunctionNode** pMergeFunc) {
545
  SNodeList*     pParameterList = NULL;
13,427,849✔
546
  SFunctionNode* pFunc = NULL;
13,427,849✔
547

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

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

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

576
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
13,427,580✔
577
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
13,427,849✔
578
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
13,427,580✔
579
  }
580
  if (TSDB_CODE_SUCCESS == code) {
13,427,849✔
581
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
13,427,849✔
582
  }
583

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

590
  return code;
13,427,849✔
591
}
592

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

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

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

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

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

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

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

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

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

719
bool fmIsCountLikeFunc(int32_t funcId) {
150,656,875✔
720
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
150,656,875✔
721
}
722

723
bool fmIsRowTsOriginFunc(int32_t funcId) {
2,377,860✔
724
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,377,860✔
725
    return false;
490✔
726
  }
727
  return FUNCTION_TYPE_IROWTS_ORIGIN == funcMgtBuiltins[funcId].type;
2,377,370✔
728
}
729

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

737
const void* fmGetStreamPesudoFuncVal(int32_t funcId, const SStreamRuntimeFuncInfo* pStreamRuntimeFuncInfo) {
22,874,715✔
738
  SSTriggerCalcParam *pParams = taosArrayGet(pStreamRuntimeFuncInfo->pStreamPesudoFuncVals, pStreamRuntimeFuncInfo->curIdx);
22,874,715✔
739
  switch (funcMgtBuiltins[funcId].type) {
22,877,884✔
740
    case FUNCTION_TYPE_TPREV_TS:
×
741
      return &pParams->prevTs;
×
742
    case FUNCTION_TYPE_TCURRENT_TS:
3,882,456✔
743
      return &pParams->currentTs;
3,882,456✔
744
    case FUNCTION_TYPE_TNEXT_TS:
×
745
      return &pParams->nextTs;
×
746
    case FUNCTION_TYPE_TWSTART:
13,291,510✔
747
      return &pParams->wstart;
13,291,510✔
748
    case FUNCTION_TYPE_TWEND:
171,622✔
749
      return &pParams->wend;
171,622✔
750
    case FUNCTION_TYPE_TWDURATION:
49,196✔
751
      return &pParams->wduration;
49,196✔
752
    case FUNCTION_TYPE_TWROWNUM:
124,260✔
753
      return &pParams->wrownum;
124,260✔
754
    case FUNCTION_TYPE_TPREV_LOCALTIME:
96,605✔
755
      return &pParams->prevLocalTime;
96,605✔
756
    case FUNCTION_TYPE_TLOCALTIME:
315,252✔
757
      return &pParams->triggerTime;
315,252✔
758
    case FUNCTION_TYPE_TNEXT_LOCALTIME:
153,845✔
759
      return &pParams->nextLocalTime;
153,845✔
760
    case FUNCTION_TYPE_TGRPID:
161,689✔
761
      return &pStreamRuntimeFuncInfo->groupId;
161,689✔
762
    case FUNCTION_TYPE_PLACEHOLDER_COLUMN:
842,894✔
763
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
842,894✔
764
    case FUNCTION_TYPE_PLACEHOLDER_TBNAME:
3,787,896✔
765
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
3,787,896✔
766
    default:
47✔
767
      break;
47✔
768
  }
769
  return NULL;
47✔
770
}
771

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

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

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

789
  SValueNode* pResDesc = (SValueNode*)nodesListGetNode(pParamNodes, 0);
9,555,089✔
790
  pEnv->calcMemSize = pResDesc->node.resType.bytes;
9,555,091✔
791

792
  return TSDB_CODE_SUCCESS;
9,555,496✔
793
}
794

795

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

809
  int32_t t = funcMgtBuiltins[funcId].type;
22,874,799✔
810
  uDebug("set stream pseudo func param val, functype: %d, pStreamRuntimeInfo: %p", t, pStreamRuntimeInfo);
22,879,903✔
811
  if (FUNCTION_TYPE_TGRPID == t) {
22,876,356✔
812
    SValue v = {0};
161,689✔
813
    v.type = TSDB_DATA_TYPE_BIGINT;
161,689✔
814
    v.val = *(int64_t*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
161,689✔
815
    
816
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&v, pFirstParam->type));
161,689✔
817
    if (code != 0) {
161,689✔
818
      uError("failed to set value node value: %s", tstrerror(code));
×
819
      return code;
×
820
    }
821
  } else if (FUNCTION_TYPE_PLACEHOLDER_COLUMN == t) {
22,714,667✔
822
    SNode* pSecondParam = nodesListGetNode(pParamNodes, 1);
842,894✔
823
    if (nodeType(pSecondParam) != QUERY_NODE_VALUE) {
842,894✔
824
      uError("invalid param node type: %d for func: %d", nodeType(pSecondParam), funcId);
×
825
      return TSDB_CODE_INTERNAL_ERROR;
×
826
    }
827
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
842,894✔
828
    int32_t idx = ((SValueNode*)pSecondParam)->datum.i;
842,894✔
829
    if (idx - 1 < 0 || idx - 1 >= taosArrayGetSize(pVal)) {
842,894✔
830
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
×
831
      return TSDB_CODE_INTERNAL_ERROR;
×
832
    }
833
    SStreamGroupValue* pValue = taosArrayGet(pVal, idx - 1);
842,894✔
834
    if (pValue == NULL) {
842,894✔
835
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
×
836
      return TSDB_CODE_INTERNAL_ERROR;
×
837
    }
838
    if (!pValue->isNull){
842,894✔
839
      if (pValue->data.type != ((SValueNode*)pFirstParam)->node.resType.type){
830,364✔
840
        uError("invalid value type: %d for func: %d, should be: %d", pValue->data.type, funcId, ((SValueNode*)pFirstParam)->node.resType.type);
×
841
        return TSDB_CODE_INTERNAL_ERROR;
×
842
      }
843
      if (IS_VAR_DATA_TYPE(((SValueNode*)pFirstParam)->node.resType.type)) {
830,364✔
844
        char* tmp = taosMemoryCalloc(1, pValue->data.nData + VARSTR_HEADER_SIZE); 
621,782✔
845
        if (tmp == NULL) {
621,782✔
846
          return TSDB_CODE_OUT_OF_MEMORY;
×
847
        }
848
        taosMemoryFree(((SValueNode*)pFirstParam)->datum.p);
621,782✔
849
        ((SValueNode*)pFirstParam)->datum.p = tmp;
621,782✔
850
        memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
621,782✔
851
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
621,782✔
852
      } else {
853
        code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&pValue->data, pValue->data.type));
208,582✔
854
      }
855
    }
856
    ((SValueNode*)pFirstParam)->isNull = pValue->isNull;
842,894✔
857
  } else if (FUNCTION_TYPE_PLACEHOLDER_TBNAME == t) {
21,871,773✔
858
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
3,787,443✔
859
    for (int32_t i = 0; i < taosArrayGetSize(pVal); ++i) {
3,790,519✔
860
      SStreamGroupValue* pValue = taosArrayGet(pVal, i);
3,789,648✔
861
      if (pValue != NULL && pValue->isTbname) {
3,790,519✔
862
        taosMemoryFreeClear(((SValueNode*)pFirstParam)->datum.p);
3,791,403✔
863
        ((SValueNode*)pFirstParam)->datum.p = taosMemoryCalloc(pValue->data.nData + VARSTR_HEADER_SIZE, 1);
3,789,197✔
864
        if (NULL == ((SValueNode*)pFirstParam)->datum.p ) {
3,790,948✔
865
          return terrno;
×
866
        }
867

868
        (void)memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
3,788,323✔
869
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
3,790,520✔
870
        break;
3,790,536✔
871
      }
872
    }
873
  } else if (LIST_LENGTH(pParamNodes) == 1) {
36,167,535✔
874
    // twstart, twend
875
    const void* pVal = fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
18,083,548✔
876
    if (!pVal) {
18,082,696✔
877
      uError("failed to set stream pseudo func param val, NULL val for funcId: %d", funcId);
×
878
      return TSDB_CODE_INTERNAL_ERROR;
×
879
    }
880
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, (void*)pVal);
18,082,696✔
881
    if (code != 0) {
18,083,973✔
882
      uError("failed to set value node value: %s", tstrerror(code));
768✔
883
      return code;
×
884
    }
885
  } else {
886
    uError("invalid placeholder function type: %d", t);
1,649✔
887
    return TSDB_CODE_INTERNAL_ERROR;
×
888
  }
889
  
890
  return code;
22,876,532✔
891
}
892

893

894

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc