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

taosdata / TDengine / #5015

03 Apr 2026 03:59PM UTC coverage: 72.289% (+0.03%) from 72.256%
#5015

push

travis-ci

web-flow
merge: from main to 3.0 branch #35067

4055 of 5985 new or added lines in 68 files covered. (67.75%)

13044 existing lines in 149 files now uncovered.

257390 of 356056 relevant lines covered (72.29%)

130247228.09 hits per line

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

81.73
/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 "nodes.h"
22
#include "querynodes.h"
23
#include "taos.h"
24
#include "taoserror.h"
25
#include "thash.h"
26
#include "tudf.h"
27

28
typedef struct SFuncMgtService {
29
  SHashObj* pFuncNameHashTable;
30
} SFuncMgtService;
31

32
static SFuncMgtService gFunMgtService;
33
static TdThreadOnce    functionHashTableInit = PTHREAD_ONCE_INIT;
34
static int32_t         initFunctionCode = 0;
35

36
static void InitSpecialFuncId() {
1,460,825✔
37
  // just to make sure the funcId of these functions are initialized before used.
38
  (void)fmGetTwstartFuncId();
1,460,825✔
39
  (void)fmGetTwendFuncId();
1,460,825✔
40
  (void)fmGetTwdurationFuncId();
1,460,825✔
41
  (void)fmGetExternalWindowColumnFuncId();
1,460,825✔
42
}
1,460,825✔
43

44
static void doInitFunctionTable() {
1,460,825✔
45
  gFunMgtService.pFuncNameHashTable =
1,460,825✔
46
      taosHashInit(funcMgtBuiltinsNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
1,460,825✔
47
  if (NULL == gFunMgtService.pFuncNameHashTable) {
1,460,825✔
UNCOV
48
    initFunctionCode = terrno;
×
UNCOV
49
    return;
×
50
  }
51

52
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
337,450,575✔
53
    if (TSDB_CODE_SUCCESS != taosHashPut(gFunMgtService.pFuncNameHashTable, funcMgtBuiltins[i].name,
335,989,750✔
54
                                         strlen(funcMgtBuiltins[i].name), &i, sizeof(int32_t))) {
335,989,750✔
UNCOV
55
      initFunctionCode = terrno;
×
56
      return;
×
57
    }
58
  }
59
  InitSpecialFuncId();
1,460,825✔
60
}
61

62
static bool isSpecificClassifyFunc(int32_t funcId, uint64_t classification) {
2,147,483,647✔
63
  if (fmIsUserDefinedFunc(funcId)) {
2,147,483,647✔
64
    return FUNC_MGT_AGG_FUNC == classification
65
               ? FUNC_AGGREGATE_UDF_ID == funcId
66
               : (FUNC_MGT_SCALAR_FUNC == classification ? FUNC_SCALAR_UDF_ID == funcId : false);
8,519,452✔
67
  }
68
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,147,483,647✔
69
    return false;
2,147,483,647✔
70
  }
71
  return FUNC_MGT_TEST_MASK(funcMgtBuiltins[funcId].classification, classification);
2,147,483,647✔
72
}
73

74
int32_t fmFuncMgtInit() {
1,460,825✔
75
  (void)taosThreadOnce(&functionHashTableInit, doInitFunctionTable);
1,460,825✔
76
  return initFunctionCode;
1,460,825✔
77
}
78

79
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
812,105,897✔
80
  if (NULL != gFunMgtService.pFuncNameHashTable) {
812,105,897✔
81
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
811,946,832✔
82
    if (NULL != pVal) {
811,976,835✔
83
      pFunc->funcId = *(int32_t*)pVal;
811,775,481✔
84
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
811,775,511✔
85
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
811,774,632✔
86
    }
87
    return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
201,354✔
88
  }
89
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
3,201,077✔
90
    if (0 == strcmp(funcMgtBuiltins[i].name, pFunc->functionName)) {
3,201,024✔
91
      pFunc->funcId = i;
172,032✔
92
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
172,032✔
93
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
172,032✔
94
    }
95
  }
96
  return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
53✔
97
}
98

99
EFuncReturnRows fmGetFuncReturnRows(SFunctionNode* pFunc) {
17,271,668✔
100
  if (NULL != funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc) {
17,271,668✔
101
    return funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc(pFunc);
12,173,680✔
102
  }
103
  return (fmIsIndefiniteRowsFunc(pFunc->funcId) || fmIsMultiRowsFunc(pFunc->funcId)) ? FUNC_RETURN_ROWS_INDEFINITE
5,097,476✔
104
                                                                                     : FUNC_RETURN_ROWS_NORMAL;
5,097,476✔
105
}
106

107
bool canCoexistIndefiniteRowsFunc(int32_t funcId1, int32_t funcId2) {
168,314✔
108
  if (fmIsUserDefinedFunc(funcId1) || funcId1 < 0 || funcId1 >= funcMgtBuiltinsNum ||
336,628✔
109
      fmIsUserDefinedFunc(funcId2) || funcId2 < 0 || funcId2 >= funcMgtBuiltinsNum) {
336,628✔
NEW
110
    return false;
×
111
  }
112
  if (funcId1 == funcId2) {
168,314✔
113
    return true;
155,786✔
114
  }
115
  if ((funcMgtBuiltins[funcId1].type == FUNCTION_TYPE_LAG || funcMgtBuiltins[funcId1].type == FUNCTION_TYPE_LEAD) &&
12,528✔
116
      (funcMgtBuiltins[funcId2].type == FUNCTION_TYPE_LAG || funcMgtBuiltins[funcId2].type == FUNCTION_TYPE_LEAD)) {
12,528✔
117
    return true;
12,528✔
118
  }
NEW
119
  return false;
×
120
}
121

122
bool fmIsBuiltinFunc(const char* pFunc) {
16,253✔
123
  return NULL != taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
16,253✔
124
}
125

126
EFunctionType fmGetFuncType(const char* pFunc) {
589,206,295✔
127
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
589,206,295✔
128
  if (NULL != pVal) {
589,228,639✔
129
    return funcMgtBuiltins[*(int32_t*)pVal].type;
589,024,651✔
130
  }
131
  return FUNCTION_TYPE_UDF;
203,988✔
132
}
133

134
EFunctionType fmGetFuncTypeFromId(int32_t funcId) {
22,693,746✔
135
  if (funcId >= 0 && funcId < funcMgtBuiltinsNum) {
22,693,746✔
136
    return funcMgtBuiltins[funcId].type;
22,693,592✔
137
  }
138
  return FUNCTION_TYPE_UDF;
154✔
139
}
140

141
EFuncDataRequired fmFuncDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
39,583,461✔
142
  if (fmIsUserDefinedFunc(pFunc->funcId) || pFunc->funcId < 0 || pFunc->funcId >= funcMgtBuiltinsNum) {
39,583,461✔
143
    return FUNC_DATA_REQUIRED_DATA_LOAD;
2,464✔
144
  }
145
  if (NULL == funcMgtBuiltins[pFunc->funcId].dataRequiredFunc) {
39,586,258✔
UNCOV
146
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
147
  }
148
  return funcMgtBuiltins[pFunc->funcId].dataRequiredFunc(pFunc, pTimeWindow);
39,586,138✔
149
}
150

151
EFuncDataRequired fmFuncDynDataRequired(int32_t funcId, void* pRes, SDataBlockInfo* pBlockInfo) {
2,147,483,647✔
152
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,147,483,647✔
153
    return FUNC_DATA_REQUIRED_DATA_LOAD;
380✔
154
  }
155

156
  const char* name = funcMgtBuiltins[funcId].name;
2,147,483,647✔
157
  if ((strcmp(name, "_group_key") == 0) || (strcmp(name, "_select_value") == 0)) {
2,147,483,647✔
158
    return FUNC_DATA_REQUIRED_NOT_LOAD;
274,489✔
159
    ;
160
  }
161

162
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
2,147,483,647✔
163
    return FUNC_DATA_REQUIRED_DATA_LOAD;
146,255,929✔
164
  } else {
165
    return funcMgtBuiltins[funcId].dynDataRequiredFunc(pRes, pBlockInfo);
2,147,483,647✔
166
  }
167
}
168

169
int32_t fmGetFuncExecFuncs(int32_t funcId, SFuncExecFuncs* pFpSet) {
289,790,205✔
170
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
289,790,205✔
171
    return TSDB_CODE_FAILED;
45,658✔
172
  }
173
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
289,765,960✔
174
  pFpSet->init = funcMgtBuiltins[funcId].initFunc;
289,753,594✔
175
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
289,756,692✔
176
  pFpSet->finalize = funcMgtBuiltins[funcId].finalizeFunc;
289,739,565✔
177
  pFpSet->combine = funcMgtBuiltins[funcId].combineFunc;
289,760,013✔
178
  pFpSet->processFuncByRow = funcMgtBuiltins[funcId].processFuncByRow;
289,751,043✔
179
  pFpSet->cleanup = funcMgtBuiltins[funcId].cleanupFunc;
289,779,455✔
180
  return TSDB_CODE_SUCCESS;
289,738,877✔
181
}
182

183
int32_t fmGetUdafExecFuncs(int32_t funcId, SFuncExecFuncs* pFpSet) {
36,706✔
184
#ifdef USE_UDF
185
  if (!fmIsUserDefinedFunc(funcId)) {
36,706✔
UNCOV
186
    return TSDB_CODE_FAILED;
×
187
  }
188
  pFpSet->getEnv = udfAggGetEnv;
36,706✔
189
  pFpSet->init = udfAggInit;
36,706✔
190
  pFpSet->process = udfAggProcess;
36,706✔
191
  pFpSet->finalize = udfAggFinalize;
36,706✔
192
  return TSDB_CODE_SUCCESS;
36,706✔
193
#else
194
  TAOS_RETURN(TSDB_CODE_OPS_NOT_SUPPORT);
195
#endif
196
}
197

198
int32_t fmGetScalarFuncExecFuncs(int32_t funcId, SScalarFuncExecFuncs* pFpSet) {
875,989,372✔
199
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
875,989,372✔
200
    return TSDB_CODE_FAILED;
124,383✔
201
  }
202
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
876,053,375✔
203
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
876,088,057✔
204
  return TSDB_CODE_SUCCESS;
876,102,881✔
205
}
206

207
bool fmIsAggFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_AGG_FUNC); }
2,140,599,672✔
208

209
bool fmIsScalarFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCALAR_FUNC); }
2,147,483,647✔
210

211
bool fmIsVectorFunc(int32_t funcId) { return !fmIsScalarFunc(funcId) && !fmIsPseudoColumnFunc(funcId); }
1,259,215,892✔
212

213
bool fmIsSelectColsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_COLS_FUNC); }
148,816,049✔
214

215
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
1,844,443,805✔
216

217
bool fmIsTimelineFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TIMELINE_FUNC); }
1,135,409,608✔
218

219
bool fmIsDateTimeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_DATETIME_FUNC); }
574,317,478✔
220

221
bool fmIsPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PSEUDO_COLUMN_FUNC); }
2,147,483,647✔
222

223
bool fmIsScanPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCAN_PC_FUNC); }
2,147,483,647✔
224

225
bool fmIsWindowPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_WINDOW_PC_FUNC); }
1,400,297,599✔
226

227
bool fmIsWindowClauseFunc(int32_t funcId) { return fmIsAggFunc(funcId) || fmIsWindowPseudoColumnFunc(funcId); }
57,463,866✔
228

229
bool fmIsStreamWindowClauseFunc(int32_t funcId) { return fmIsWindowClauseFunc(funcId) || fmIsPlaceHolderFunc(funcId); }
853,148✔
230

231
bool fmIsStreamVectorFunc(int32_t funcId) { return fmIsVectorFunc(funcId) || fmIsPlaceHolderFunc(funcId); }
11,513✔
232

233
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
1,555,335,394✔
234

235
bool fmIsSpecialDataRequiredFunc(int32_t funcId) {
89,585,597✔
236
  return isSpecificClassifyFunc(funcId, FUNC_MGT_SPECIAL_DATA_REQUIRED);
89,585,597✔
237
}
238

239
bool fmIsDynamicScanOptimizedFunc(int32_t funcId) {
42,837,279✔
240
  return isSpecificClassifyFunc(funcId, FUNC_MGT_DYNAMIC_SCAN_OPTIMIZED);
42,837,279✔
241
}
242

243
bool fmIsMultiResFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_RES_FUNC); }
1,169,446,154✔
244

245
bool fmIsRepeatScanFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_REPEAT_SCAN_FUNC); }
1,147,396,478✔
246

247
bool fmIsUserDefinedFunc(int32_t funcId) { return funcId > FUNC_UDF_ID_START; }
2,147,483,647✔
248

249
bool fmIsForbidFillFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_FILL_FUNC); }
574,573,953✔
250

251
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
62,487,033✔
252

253
bool fmIsSystemInfoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SYSTEM_INFO_FUNC); }
577,501,824✔
254

255
bool fmIsImplicitTsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IMPLICIT_TS_FUNC); }
2,147,483,647✔
256

257
bool fmIsClientPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CLIENT_PC_FUNC); }
584,855,532✔
258

259
bool fmIsMultiRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_ROWS_FUNC); }
1,144,669,321✔
260

261
static bool fmIsKeepOrderFuncId(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_KEEP_ORDER_FUNC); }
693,483,077✔
262

263
bool fmIsKeepOrderFunc(SFunctionNode* pFunc) {
699,694,063✔
264
  if (pFunc->funcType == FUNCTION_TYPE_TOP || pFunc->funcType == FUNCTION_TYPE_BOTTOM ||
699,694,063✔
265
      pFunc->funcType == FUNCTION_TYPE_SAMPLE) {
695,775,390✔
266
    if (pFunc->pParameterList != NULL && pFunc->pParameterList->length >= 2) {
6,216,455✔
267
      SNode* pParam = nodesListGetNode(pFunc->pParameterList, 1);
6,232,057✔
268
      if (pParam != NULL && nodeType(pParam) == QUERY_NODE_VALUE) {
6,232,057✔
269
        SValueNode* pConst = (SValueNode*)pParam;
6,232,057✔
270
        if (pConst->datum.i == 1) {
6,232,057✔
271
          return true;
730,638✔
272
        }
273
      }
274
    }
275
    return false;
5,491,594✔
276
  }
277
  return fmIsKeepOrderFuncId(pFunc->funcId);
693,491,871✔
278
}
279

280
bool fmIsCumulativeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CUMULATIVE_FUNC); }
151,182,202✔
281

282
bool fmIsForbidSysTableFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_SYSTABLE_FUNC); }
574,497,410✔
283

284
bool fmIsInterpFunc(int32_t funcId) {
1,177,032,333✔
285
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,177,032,333✔
286
    return false;
366,214✔
287
  }
288
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
1,176,670,604✔
289
}
290

291
bool fmIsInterpPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERP_PC_FUNC); }
1,158,449,613✔
292

293
bool fmIsForecastFunc(int32_t funcId) {
1,129,624,892✔
294
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,129,624,892✔
295
    return false;
372,919✔
296
  }
297
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
1,129,255,172✔
298
}
299

300
bool fmIsAnalysisPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_ANALYTICS_PC_FUNC); }
1,161,320,428✔
301

UNCOV
302
bool fmIsImputationCorrelationFunc(int32_t funcId) {
×
UNCOV
303
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
UNCOV
304
    return false;
×
305
  }
306

UNCOV
307
  int32_t type = funcMgtBuiltins[funcId].type;
×
UNCOV
308
  return FUNCTION_TYPE_IMPUTATION == type || FUNCTION_TYPE_DTW == type || FUNCTION_TYPE_DTW_PATH == type ||
×
309
         FUNCTION_TYPE_TLCC == type;
310
}
311

312
bool fmIsLastRowFunc(int32_t funcId) {
68,189,004✔
313
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
68,189,004✔
UNCOV
314
    return false;
×
315
  }
316
  return FUNCTION_TYPE_LAST_ROW == funcMgtBuiltins[funcId].type;
68,189,406✔
317
}
318

319
bool fmIsLastFunc(int32_t funcId) {
12,390✔
320
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
12,390✔
UNCOV
321
    return false;
×
322
  }
323
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type;
12,390✔
324
}
325

326
bool fmIsNotNullOutputFunc(int32_t funcId) {
437,674,146✔
327
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
437,674,146✔
328
    return false;
75,858✔
329
  }
330
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
437,614,506✔
331
         FUNCTION_TYPE_CACHE_LAST == funcMgtBuiltins[funcId].type ||
421,702,535✔
332
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
420,466,019✔
333
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
411,754,372✔
334
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
408,245,095✔
335
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
398,034,240✔
336
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
392,920,564✔
337
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
390,896,043✔
338
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
339,660,696✔
339
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
1,195,181,814✔
340
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
335,919,315✔
341
}
342

343
bool fmIsSelectValueFunc(int32_t funcId) {
341,111,121✔
344
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
341,111,121✔
345
    return false;
×
346
  }
347
  return FUNCTION_TYPE_SELECT_VALUE == funcMgtBuiltins[funcId].type;
341,112,568✔
348
}
349

350
bool fmIsGroupKeyFunc(int32_t funcId) {
263,176,280✔
351
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
263,176,280✔
UNCOV
352
    return false;
×
353
  }
354
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
263,178,505✔
355
}
356

UNCOV
357
bool fmIsHasNullFunc(int32_t funcId) {
×
UNCOV
358
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
UNCOV
359
    return false;
×
360
  }
UNCOV
361
  return FUNCTION_TYPE_HAS_NULL == funcMgtBuiltins[funcId].type;
×
362
}
363

364
bool fmisSelectGroupConstValueFunc(int32_t funcId) {
16,818,219✔
365
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
16,818,219✔
UNCOV
366
    return false;
×
367
  }
368
  return FUNCTION_TYPE_GROUP_CONST_VALUE == funcMgtBuiltins[funcId].type;
16,818,219✔
369
}
370

371
bool fmIsElapsedFunc(int32_t funcId) {
10,674,231✔
372
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
10,674,231✔
UNCOV
373
    return false;
×
374
  }
375
  return FUNCTION_TYPE_ELAPSED == funcMgtBuiltins[funcId].type;
10,674,231✔
376
}
377

378
bool fmIsBlockDistFunc(int32_t funcId) {
574,297,561✔
379
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
574,297,561✔
380
    return false;
173,880✔
381
  }
382
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
574,128,463✔
383
}
384

385
bool fmIsDBUsageFunc(int32_t funcId) {
574,306,501✔
386
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
574,306,501✔
387
    return false;
186,604✔
388
  }
389
  return FUNCTION_TYPE_DB_USAGE == funcMgtBuiltins[funcId].type;
574,121,330✔
390
}
391

392
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
765,420,037✔
393

394
bool fmIsIgnoreNullFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IGNORE_NULL_FUNC); }
289,511✔
395

396
void fmFuncMgtDestroy() {
1,460,869✔
397
  void* m = gFunMgtService.pFuncNameHashTable;
1,460,869✔
398
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
1,460,869✔
399
    taosHashCleanup(m);
1,460,825✔
400
  }
401
}
1,460,869✔
402

403
#ifdef BUILD_NO_CALL
404
int32_t fmSetNormalFunc(int32_t funcId, SFuncExecFuncs* pFpSet) {
405
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
406
    return TSDB_CODE_FAILED;
407
  }
408
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
409
  return TSDB_CODE_SUCCESS;
410
}
411
#endif
412

413
// function has same input/output type
414
bool fmIsSameInOutType(int32_t funcId) {
47,568,196✔
415
  bool res = false;
47,568,196✔
416
  switch (funcMgtBuiltins[funcId].type) {
47,568,196✔
417
    case FUNCTION_TYPE_MAX:
18,999,160✔
418
    case FUNCTION_TYPE_MIN:
419
    case FUNCTION_TYPE_TOP:
420
    case FUNCTION_TYPE_BOTTOM:
421
    case FUNCTION_TYPE_FIRST:
422
    case FUNCTION_TYPE_LAST:
423
    case FUNCTION_TYPE_SAMPLE:
424
    case FUNCTION_TYPE_TAIL:
425
    case FUNCTION_TYPE_UNIQUE:
426
      res = true;
18,999,160✔
427
      break;
18,999,160✔
428
    default:
28,569,080✔
429
      break;
28,569,080✔
430
  }
431
  return res;
47,568,240✔
432
}
433

434
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
14,441,646✔
435
  SNode* pNode;
436
  FOREACH(pNode, pFunc->pParameterList) {
14,526,042✔
437
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
189,962✔
438
      return false;
105,566✔
439
    }
440
  }
441
  return true;
14,336,080✔
442
}
443

444
bool fmIsSkipScanCheckFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SKIP_SCAN_CHECK_FUNC); }
42,830,826✔
445

446
bool fmIsPrimaryKeyFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PRIMARY_KEY_FUNC); }
500,216,771✔
447

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

450
bool fmIsPlaceHolderFuncForExternalWin(int32_t funcId) {
491,885✔
451
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
491,885✔
NEW
452
    return false;
×
453
  }
454
  return (funcMgtBuiltins[funcId].type == FUNCTION_TYPE_WSTART || funcMgtBuiltins[funcId].type == FUNCTION_TYPE_WEND ||
822,891✔
455
          funcMgtBuiltins[funcId].type == FUNCTION_TYPE_WDURATION);
331,006✔
456
}
457

UNCOV
458
void getLastCacheDataType(SDataType* pType, int32_t pkBytes) {
×
459
  // TODO: do it later.
UNCOV
460
  pType->bytes = getFirstLastInfoSize(pType->bytes, pkBytes) + VARSTR_HEADER_SIZE;
×
UNCOV
461
  pType->type = TSDB_DATA_TYPE_BINARY;
×
UNCOV
462
}
×
463

464
static int32_t getFuncInfo(SFunctionNode* pFunc) {
152,203,670✔
465
  char msg[128] = {0};
152,203,670✔
466
  return fmGetFuncInfo(pFunc, msg, sizeof(msg));
152,203,670✔
467
}
468

469
int32_t createFunction(const char* pName, SNodeList* pParameterList, SFunctionNode** ppFunc) {
9,866,106✔
470
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
9,866,106✔
471
  if (NULL == *ppFunc) {
9,866,106✔
UNCOV
472
    return code;
×
473
  }
474
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
9,866,106✔
475
  (*ppFunc)->pParameterList = pParameterList;
9,866,106✔
476
  code = getFuncInfo((*ppFunc));
9,866,106✔
477
  if (TSDB_CODE_SUCCESS != code) {
9,866,106✔
UNCOV
478
    (*ppFunc)->pParameterList = NULL;
×
UNCOV
479
    nodesDestroyNode((SNode*)*ppFunc);
×
UNCOV
480
    *ppFunc = NULL;
×
UNCOV
481
    return code;
×
482
  }
483
  return code;
9,866,106✔
484
}
485

486
static void resetOutputChangedFunc(SFunctionNode *pFunc, const SFunctionNode* pSrcFunc) {
142,340,342✔
487
  if (funcMgtBuiltins[pFunc->funcId].type == FUNCTION_TYPE_LAST_MERGE) {
142,340,342✔
488
    pFunc->node.resType = pSrcFunc->node.resType;
22,520,708✔
489
    return;
22,520,751✔
490
  }
491
}
492

493
int32_t createFunctionWithSrcFunc(const char* pName, const SFunctionNode* pSrcFunc, SNodeList* pParameterList, SFunctionNode** ppFunc) {
142,338,173✔
494
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
142,338,173✔
495
  if (NULL == *ppFunc) {
142,343,963✔
UNCOV
496
    return code;
×
497
  }
498

499
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
142,343,963✔
500
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
142,343,920✔
501
  (*ppFunc)->pSrcFuncRef = pSrcFunc;
142,343,963✔
502

503
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
142,343,920✔
504
  (*ppFunc)->pParameterList = pParameterList;
142,343,920✔
505
  code = getFuncInfo((*ppFunc));
142,343,920✔
506
  if (TSDB_CODE_SUCCESS != code) {
142,341,786✔
UNCOV
507
    (*ppFunc)->pParameterList = NULL;
×
UNCOV
508
    nodesDestroyNode((SNode*)*ppFunc);
×
UNCOV
509
    *ppFunc = NULL;
×
510
    return code;
×
511
  }
512
  resetOutputChangedFunc(*ppFunc, pSrcFunc);
142,341,786✔
513
  (*ppFunc)->node.relatedTo = pSrcFunc->node.relatedTo;
142,342,911✔
514
  (*ppFunc)->node.bindExprID = pSrcFunc->node.bindExprID;
142,342,997✔
515
  return code;
142,342,996✔
516
}
517

518
static int32_t createColumnByFunc(const SFunctionNode* pFunc, SColumnNode** ppCol) {
94,761,103✔
519
  int32_t code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)ppCol);
94,761,103✔
520
  if (NULL == *ppCol) {
94,768,768✔
UNCOV
521
    return code;
×
522
  }
523
  tstrncpy((*ppCol)->colName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
94,768,811✔
524
  (*ppCol)->node.resType = pFunc->node.resType;
94,768,683✔
525
  return TSDB_CODE_SUCCESS;
94,768,683✔
526
}
527

528
bool fmIsDistExecFunc(int32_t funcId) {
362,941,235✔
529
  if (fmIsUserDefinedFunc(funcId)) {
362,941,235✔
530
    return false;
102,520✔
531
  }
532
  if (!fmIsVectorFunc(funcId)) {
362,865,210✔
533
    return true;
388,791✔
534
  }
535
  return (NULL != funcMgtBuiltins[funcId].pPartialFunc && NULL != funcMgtBuiltins[funcId].pMergeFunc);
362,485,838✔
536
}
537

538
static int32_t createPartialFunction(const SFunctionNode* pSrcFunc, SFunctionNode** pPartialFunc) {
47,570,624✔
539
  SNodeList* pParameterList = NULL;
47,570,624✔
540
  int32_t    code = nodesCloneList(pSrcFunc->pParameterList, &pParameterList);
47,570,624✔
541
  if (NULL == pParameterList) {
47,573,519✔
542
    return code;
×
543
  }
544
  code =
545
      createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pPartialFunc, pSrcFunc, pParameterList, pPartialFunc);
47,573,519✔
546
  if (TSDB_CODE_SUCCESS != code) {
47,572,151✔
UNCOV
547
    nodesDestroyList(pParameterList);
×
UNCOV
548
    return code;
×
549
  }
550
  (*pPartialFunc)->hasOriginalFunc = true;
47,572,806✔
551
  (*pPartialFunc)->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
47,572,721✔
552
  char name[TSDB_FUNC_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_POINTER_PRINT_BYTES + 1] = {0};
47,572,806✔
553

554
  int32_t len = snprintf(name, sizeof(name), "%s.%p", (*pPartialFunc)->functionName, pSrcFunc);
47,572,849✔
555
  if (taosHashBinary(name, len, sizeof(name)) < 0) {
47,574,023✔
UNCOV
556
    return TSDB_CODE_FAILED;
×
557
  }
558
  tstrncpy((*pPartialFunc)->node.aliasName, name, TSDB_COL_NAME_LEN);
47,574,023✔
559
  return TSDB_CODE_SUCCESS;
47,574,023✔
560
}
561

562
static int32_t createMergeFuncPara(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
94,760,926✔
563
                                   SNodeList** pParameterList) {
564
  SNode*  pRes = NULL;
94,760,926✔
565
  int32_t code = createColumnByFunc(pPartialFunc, (SColumnNode**)&pRes);
94,761,012✔
566
  if (TSDB_CODE_SUCCESS != code) {
94,767,306✔
UNCOV
567
    return code;
×
568
  }
569
  if (NULL != funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc) {
94,767,306✔
570
    return funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc(pSrcFunc->pParameterList, pRes, pParameterList);
835,710✔
571
  } else {
572
    return nodesListMakeStrictAppend(pParameterList, pRes);
93,931,596✔
573
  }
574
}
575

576
static int32_t createMidFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
47,191,203✔
577
                                 SFunctionNode** pMidFunc) {
578
  SNodeList*     pParameterList = NULL;
47,191,203✔
579
  SFunctionNode* pFunc = NULL;
47,191,203✔
580

581
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
47,191,246✔
582
  if (TSDB_CODE_SUCCESS == code) {
47,196,559✔
583
    if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){
47,196,693✔
584
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pSrcFunc, pParameterList, &pFunc);
5,225,523✔
585
    }else{
586
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
41,971,170✔
587
    }
588
  }
589
  if (TSDB_CODE_SUCCESS == code) {
47,196,079✔
590
    tstrncpy(pFunc->node.aliasName, pPartialFunc->node.aliasName, TSDB_COL_NAME_LEN);
47,195,435✔
591
  }
592

593
  if (TSDB_CODE_SUCCESS == code) {
47,196,122✔
594
    *pMidFunc = pFunc;
47,196,122✔
595
  } else {
UNCOV
596
    nodesDestroyList(pParameterList);
×
597
  }
598
  return code;
47,196,215✔
599
}
600

601
static int32_t createMergeFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
47,570,236✔
602
                                   SFunctionNode** pMergeFunc) {
603
  SNodeList*     pParameterList = NULL;
47,570,236✔
604
  SFunctionNode* pFunc = NULL;
47,570,322✔
605

606
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
47,570,322✔
607
  if (TSDB_CODE_SUCCESS == code) {
47,573,865✔
608
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
47,573,958✔
609
  }
610
  if (TSDB_CODE_SUCCESS == code) {
47,575,585✔
611
    pFunc->hasOriginalFunc = true;
47,574,090✔
612
    pFunc->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
47,574,133✔
613
    // overwrite function restype set by translate function
614
    if (fmIsSameInOutType(pSrcFunc->funcId)) {
47,574,133✔
615
      pFunc->node.resType = pSrcFunc->node.resType;
18,999,361✔
616
    }
617
    tstrncpy(pFunc->node.aliasName, pSrcFunc->node.aliasName, TSDB_COL_NAME_LEN);
47,570,110✔
618
  }
619

620
  if (TSDB_CODE_SUCCESS == code) {
47,571,604✔
621
    *pMergeFunc = pFunc;
47,571,604✔
622
  } else {
UNCOV
623
    nodesDestroyList(pParameterList);
×
624
  }
625
  return code;
47,571,915✔
626
}
627

628
int32_t fmGetDistMethod(const SFunctionNode* pFunc, SFunctionNode** pPartialFunc, SFunctionNode** pMidFunc,
47,568,505✔
629
                        SFunctionNode** pMergeFunc) {
630
  if (!fmIsDistExecFunc(pFunc->funcId)) {
47,568,505✔
UNCOV
631
    return TSDB_CODE_FAILED;
×
632
  }
633

634
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
47,573,099✔
635
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
47,570,593✔
636
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
47,195,569✔
637
  }
638
  if (TSDB_CODE_SUCCESS == code) {
47,571,247✔
639
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
47,571,182✔
640
  }
641

642
  if (TSDB_CODE_SUCCESS != code) {
47,569,495✔
UNCOV
643
    nodesDestroyNode((SNode*)*pPartialFunc);
×
UNCOV
644
    if (pMidFunc) nodesDestroyNode((SNode*)*pMidFunc);
×
UNCOV
645
    nodesDestroyNode((SNode*)*pMergeFunc);
×
646
  }
647

648
  return code;
47,570,570✔
649
}
650

651
const char* fmGetFuncName(int32_t funcId) {
336,778✔
652
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
336,778✔
UNCOV
653
    return "invalid function";
×
654
  }
655
  return funcMgtBuiltins[funcId].name;
336,778✔
656
}
657

658
/// @param [out] pStateFunc, not changed if error occured or no need to create state func
659
/// @retval 0 for succ, otherwise err occured
660
static int32_t fmCreateStateFunc(const SFunctionNode* pFunc, SFunctionNode** pStateFunc) {
6,094✔
661
  if (funcMgtBuiltins[pFunc->funcId].pStateFunc) {
6,094✔
662
    SNodeList* pParams = NULL;
6,094✔
663
    int32_t    code = nodesCloneList(pFunc->pParameterList, &pParams);
6,094✔
664
    if (!pParams) return code;
6,094✔
665
    code = createFunction(funcMgtBuiltins[pFunc->funcId].pStateFunc, pParams, pStateFunc);
6,094✔
666
    if (TSDB_CODE_SUCCESS != code) {
6,094✔
UNCOV
667
      nodesDestroyList(pParams);
×
UNCOV
668
      return code;
×
669
    }
670
    tstrncpy((*pStateFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
6,094✔
671
    tstrncpy((*pStateFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
6,094✔
672
  }
673
  return TSDB_CODE_SUCCESS;
6,094✔
674
}
675

676
bool fmIsTSMASupportedFunc(func_id_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC); }
1,026,424✔
677

678
bool fmIsRsmaSupportedFunc(func_id_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_RSMA_FUNC); }
134,820✔
679

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

706
static int32_t fmCreateStateMergeFunc(SFunctionNode* pFunc, SFunctionNode** pStateMergeFunc) {
1,331✔
707
  if (funcMgtBuiltins[pFunc->funcId].pMergeFunc) {
1,331✔
708
    SNodeList* pParams = NULL;
1,089✔
709
    int32_t    code = nodesCloneList(pFunc->pParameterList, &pParams);
1,089✔
710
    if (!pParams) return code;
1,089✔
711
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pFunc->funcId].pMergeFunc, pFunc, pParams, pStateMergeFunc);
1,089✔
712
    if (TSDB_CODE_SUCCESS != code) {
1,089✔
UNCOV
713
      nodesDestroyList(pParams);
×
UNCOV
714
      return code;
×
715
    }
716
    tstrncpy((*pStateMergeFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
1,089✔
717
    tstrncpy((*pStateMergeFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
1,089✔
718
  }
719
  return TSDB_CODE_SUCCESS;
1,331✔
720
}
721

722
int32_t fmCreateStateMergeFuncs(SNodeList* pFuncs) {
605✔
723
  int32_t code;
724
  SNode*  pNode;
725
  char    buf[128] = {0};
605✔
726
  FOREACH(pNode, pFuncs) {
1,936✔
727
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
1,331✔
728
    if (fmIsTSMASupportedFunc(pFunc->funcId)) {
1,331✔
729
      SFunctionNode* pNewFunc = NULL;
1,331✔
730
      code = fmCreateStateMergeFunc(pFunc, &pNewFunc);
1,331✔
731
      if (code) {
1,331✔
732
        // error
UNCOV
733
        break;
×
734
      } else if (!pNewFunc) {
1,331✔
735
        // no state merge func
736
        continue;
242✔
737
      } else {
738
        REPLACE_NODE(pNewFunc);
1,089✔
739
        nodesDestroyNode(pNode);
1,089✔
740
      }
741
    }
742
  }
743
  return code;
605✔
744
}
745

746
int32_t fmGetFuncId(const char* name) {
7,611,254✔
747
  if (NULL != gFunMgtService.pFuncNameHashTable) {
7,611,254✔
748
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, name, strlen(name));
7,611,254✔
749
    if (NULL != pVal) {
7,611,254✔
750
      return *(int32_t*)pVal;
7,611,254✔
751
    }
UNCOV
752
    return -1;
×
753
  }
UNCOV
754
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
×
UNCOV
755
    if (0 == strcmp(funcMgtBuiltins[i].name, name)) {
×
UNCOV
756
      return i;
×
757
    }
758
  }
UNCOV
759
  return -1;
×
760
}
761

762
int32_t fmGetTwstartFuncId() {
1,590,840✔
763
  static int32_t twstartFuncId = -2;
764
  if (twstartFuncId < 0) {
1,590,840✔
765
    twstartFuncId = fmGetFuncId("_twstart");
1,460,825✔
766
  }
767
  return twstartFuncId;
1,590,840✔
768
}
769

770
int32_t fmGetTwendFuncId() {
1,491,689✔
771
  static int32_t twendFuncId = -2;
772
  if (twendFuncId < 0) {
1,491,689✔
773
    twendFuncId = fmGetFuncId("_twend");
1,460,825✔
774
  }
775
  return twendFuncId;
1,491,689✔
776
}
777

778
int32_t fmGetTwdurationFuncId() {
1,461,235✔
779
  static int32_t twdurationFuncId = -2;
780
  if (twdurationFuncId < 0) {
1,461,235✔
781
    twdurationFuncId = fmGetFuncId("_twduration");
1,460,825✔
782
  }
783
  return twdurationFuncId;
1,461,235✔
784
}
785

786
int32_t fmGetExternalWindowColumnFuncId() {
1,496,458✔
787
  static int32_t externalWindowColumnFuncId = -2;
788
  if (externalWindowColumnFuncId < 0) {
1,496,458✔
789
    externalWindowColumnFuncId = fmGetFuncId("_external_window_column");
1,460,825✔
790
  }
791
  return externalWindowColumnFuncId;
1,496,458✔
792
}
793

794
bool fmIsMyStateFunc(int32_t funcId, int32_t stateFuncId) {
1,647,294✔
795
  const SBuiltinFuncDefinition* pFunc = &funcMgtBuiltins[funcId];
1,647,294✔
796
  const SBuiltinFuncDefinition* pStateFunc = &funcMgtBuiltins[stateFuncId];
1,647,294✔
797
  if (!pFunc->pStateFunc) {
1,647,294✔
UNCOV
798
    return false;
×
799
  }
800
  if (strcmp(pFunc->pStateFunc, pStateFunc->name) == 0) return true;
1,647,294✔
801
  int32_t stateMergeFuncId = fmGetFuncId(pFunc->pStateFunc);
599,071✔
802
  if (stateMergeFuncId == -1) {
599,071✔
UNCOV
803
    return false;
×
804
  }
805
  const SBuiltinFuncDefinition* pStateMergeFunc = &funcMgtBuiltins[stateMergeFuncId];
599,071✔
806
  return strcmp(pStateFunc->name, pStateMergeFunc->pMergeFunc) == 0;
599,071✔
807
}
808

809
bool fmIsCountLikeFunc(int32_t funcId) {
567,032,331✔
810
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
567,032,331✔
811
}
812

813
bool fmIsRowTsOriginFunc(int32_t funcId) {
5,247,009✔
814
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
5,247,009✔
UNCOV
815
    return false;
×
816
  }
817
  return FUNCTION_TYPE_IROWTS_ORIGIN == funcMgtBuiltins[funcId].type;
5,247,514✔
818
}
819

820
bool fmIsGroupIdFunc(int32_t funcId) {
×
UNCOV
821
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
UNCOV
822
    return false;
×
823
  }
UNCOV
824
  return FUNCTION_TYPE_GROUP_ID == funcMgtBuiltins[funcId].type;
×
825
}
826

827
const void* fmGetExternalWindowColumnFuncVal(const SStreamRuntimeFuncInfo* pStreamRuntimeFuncInfo, int32_t index) {
10,180,187✔
828
  SSTriggerCalcParam *pParams = taosArrayGet(pStreamRuntimeFuncInfo->pStreamPesudoFuncVals, pStreamRuntimeFuncInfo->curIdx);
10,180,187✔
829
  if (pParams == NULL || pParams->pExternalWindowData == NULL || index < 0) {
10,201,039✔
NEW
830
    return NULL;
×
831
  }
832
  return taosArrayGet(pParams->pExternalWindowData, index);
10,201,440✔
833
}
834

835
const void* fmGetStreamPesudoFuncVal(int32_t funcId, const SStreamRuntimeFuncInfo* pStreamRuntimeFuncInfo) {
325,983,090✔
836
  switch (funcMgtBuiltins[funcId].type) {
325,983,090✔
837
    case FUNCTION_TYPE_TGRPID:
127,501✔
838
      return &pStreamRuntimeFuncInfo->groupId;
127,501✔
839
    case FUNCTION_TYPE_PLACEHOLDER_COLUMN:
825,872✔
840
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
825,872✔
841
    case FUNCTION_TYPE_PLACEHOLDER_TBNAME:
2,010,159✔
842
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
2,010,159✔
843
    default:
323,078,837✔
844
      break;
323,078,837✔
845
  }
846

847
  SSTriggerCalcParam *pParams = taosArrayGet(pStreamRuntimeFuncInfo->pStreamPesudoFuncVals, pStreamRuntimeFuncInfo->curIdx);
323,078,837✔
848
  switch (funcMgtBuiltins[funcId].type) {
323,149,359✔
UNCOV
849
    case FUNCTION_TYPE_TPREV_TS:
×
UNCOV
850
      return &pParams->prevTs;
×
851
    case FUNCTION_TYPE_TCURRENT_TS:
2,001,796✔
852
      return &pParams->currentTs;
2,001,796✔
UNCOV
853
    case FUNCTION_TYPE_TNEXT_TS:
×
UNCOV
854
      return &pParams->nextTs;
×
855
    case FUNCTION_TYPE_TWSTART:
302,717,032✔
856
      return &pParams->wstart;
302,717,032✔
857
    case FUNCTION_TYPE_TWEND:
16,531,559✔
858
      return &pParams->wend;
16,531,559✔
859
    case FUNCTION_TYPE_TWDURATION:
881,621✔
860
      return &pParams->wduration;
881,621✔
861
    case FUNCTION_TYPE_TWROWNUM:
98,857✔
862
      return &pParams->wrownum;
98,857✔
863
    case FUNCTION_TYPE_TPREV_LOCALTIME:
157,728✔
864
      return &pParams->prevLocalTime;
157,728✔
865
    case FUNCTION_TYPE_TLOCALTIME:
449,322✔
866
      return &pParams->triggerTime;
449,322✔
867
    case FUNCTION_TYPE_TNEXT_LOCALTIME:
246,508✔
868
      return &pParams->nextLocalTime;
246,508✔
UNCOV
869
    case FUNCTION_TYPE_TGRPID:
×
UNCOV
870
      return &pStreamRuntimeFuncInfo->groupId;
×
UNCOV
871
    case FUNCTION_TYPE_PLACEHOLDER_COLUMN:
×
UNCOV
872
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
×
UNCOV
873
    case FUNCTION_TYPE_PLACEHOLDER_TBNAME:
×
UNCOV
874
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
×
875
    case FUNCTION_TYPE_TIDLESTART:
5,643✔
876
      return &pParams->idlestart;
5,643✔
877
    case FUNCTION_TYPE_TIDLEEND:
5,643✔
878
      return &pParams->idleend;
5,643✔
UNCOV
879
    default:
×
UNCOV
880
      break;
×
881
  }
UNCOV
882
  return NULL;
×
883
}
884

885
bool fmIsStreamPesudoColVal(int32_t funcId) {
478,956✔
886
  return funcMgtBuiltins[funcId].type == FUNCTION_TYPE_PLACEHOLDER_COLUMN
478,956✔
887
         || funcMgtBuiltins[funcId].type == FUNCTION_TYPE_PLACEHOLDER_TBNAME;
478,956✔
888
}
889

890
int32_t fmGetStreamPesudoFuncEnv(int32_t funcId, SNodeList* pParamNodes, SFuncExecEnv *pEnv) {
7,055,737✔
891
  if (NULL == pParamNodes || pParamNodes->length < 1) {
7,055,737✔
UNCOV
892
    uError("invalid stream pesudo func param list %p", pParamNodes);
×
UNCOV
893
    return TSDB_CODE_INTERNAL_ERROR;
×
894
  }
895

896
  int32_t firstParamType = nodeType(nodesListGetNode(pParamNodes, 0));
7,056,469✔
897
  if (QUERY_NODE_VALUE != firstParamType) {
7,056,265✔
898
    uError("invalid stream pesudo func first param type %d", firstParamType);
×
UNCOV
899
    return TSDB_CODE_INTERNAL_ERROR;
×
900
  }
901

902
  SValueNode* pResDesc = (SValueNode*)nodesListGetNode(pParamNodes, 0);
7,056,265✔
903
  pEnv->calcMemSize = pResDesc->node.resType.bytes;
7,056,469✔
904

905
  return TSDB_CODE_SUCCESS;
7,056,236✔
906
}
907

908

909
int32_t fmSetStreamPseudoFuncParamVal(int32_t funcId, SNodeList* pParamNodes, const SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
336,160,500✔
910
  if (!pStreamRuntimeInfo) {
336,160,500✔
UNCOV
911
    uError("internal error, should have pVals for stream pseudo funcs");
×
UNCOV
912
    return TSDB_CODE_INTERNAL_ERROR;
×
913
  }
914
  int32_t code = 0;
336,160,500✔
915
  SArray *pVals1 = NULL;
336,160,500✔
916
  SNode* pFirstParam = nodesListGetNode(pParamNodes, 0);
336,160,500✔
917
  if (nodeType(pFirstParam) != QUERY_NODE_VALUE) {
336,211,752✔
UNCOV
918
    uError("invalid param node type: %d for func: %d", nodeType(pFirstParam), funcId);
×
UNCOV
919
    return TSDB_CODE_INTERNAL_ERROR;
×
920
  }
921

922
  int32_t t = funcMgtBuiltins[funcId].type;
336,209,333✔
923
  uDebug("set stream pseudo func param val, functype: %d, pStreamRuntimeInfo: %p", t, pStreamRuntimeInfo);
336,185,742✔
924
  if (FUNCTION_TYPE_TGRPID == t) {
336,217,472✔
925
    SValue v = {0};
127,501✔
926
    v.type = TSDB_DATA_TYPE_BIGINT;
127,501✔
927
    v.val = *(int64_t*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
127,501✔
928
    
929
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&v, pFirstParam->type));
127,501✔
930
    if (code != 0) {
127,501✔
UNCOV
931
      uError("failed to set value node value: %s", tstrerror(code));
×
932
      return code;
×
933
    }
934
  } else if (FUNCTION_TYPE_PLACEHOLDER_COLUMN == t) {
336,089,971✔
935
    SNode* pSecondParam = nodesListGetNode(pParamNodes, 1);
825,872✔
936
    if (nodeType(pSecondParam) != QUERY_NODE_VALUE) {
825,872✔
937
      uError("invalid param node type: %d for func: %d", nodeType(pSecondParam), funcId);
×
UNCOV
938
      return TSDB_CODE_INTERNAL_ERROR;
×
939
    }
940
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
825,872✔
941
    int32_t idx = ((SValueNode*)pSecondParam)->datum.i;
825,872✔
942
    if (idx - 1 < 0 || idx - 1 >= taosArrayGetSize(pVal)) {
825,872✔
UNCOV
943
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
×
UNCOV
944
      return TSDB_CODE_INTERNAL_ERROR;
×
945
    }
946
    SStreamGroupValue* pValue = taosArrayGet(pVal, idx - 1);
825,872✔
947
    if (pValue == NULL) {
825,872✔
UNCOV
948
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
×
UNCOV
949
      return TSDB_CODE_INTERNAL_ERROR;
×
950
    }
951
    if (!pValue->isNull){
825,872✔
952
      if (pValue->data.type != ((SValueNode*)pFirstParam)->node.resType.type){
818,087✔
953
        uError("invalid value type: %d for func: %d, should be: %d", pValue->data.type, funcId, ((SValueNode*)pFirstParam)->node.resType.type);
×
954
        return TSDB_CODE_INTERNAL_ERROR;
×
955
      }
956
      if (IS_VAR_DATA_TYPE(((SValueNode*)pFirstParam)->node.resType.type)) {
818,087✔
957
        char* tmp = taosMemoryCalloc(1, pValue->data.nData + VARSTR_HEADER_SIZE); 
568,707✔
958
        if (tmp == NULL) {
568,707✔
UNCOV
959
          return TSDB_CODE_OUT_OF_MEMORY;
×
960
        }
961
        taosMemoryFree(((SValueNode*)pFirstParam)->datum.p);
568,707✔
962
        ((SValueNode*)pFirstParam)->datum.p = tmp;
568,707✔
963
        memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
568,707✔
964
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
568,707✔
965
      } else {
966
        code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&pValue->data, pValue->data.type));
249,380✔
967
      }
968
    }
969
    ((SValueNode*)pFirstParam)->isNull = pValue->isNull;
825,872✔
970
  } else if (FUNCTION_TYPE_PLACEHOLDER_TBNAME == t) {
335,264,099✔
971
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
1,922,337✔
972
    for (int32_t i = 0; i < taosArrayGetSize(pVal); ++i) {
2,272,840✔
973
      SStreamGroupValue* pValue = taosArrayGet(pVal, i);
2,272,826✔
974
      if (pValue != NULL && pValue->isTbname) {
2,273,104✔
975
        taosMemoryFreeClear(((SValueNode*)pFirstParam)->datum.p);
1,924,902✔
976
        ((SValueNode*)pFirstParam)->datum.p = taosMemoryCalloc(pValue->data.nData + VARSTR_HEADER_SIZE, 1);
1,924,612✔
977
        if (NULL == ((SValueNode*)pFirstParam)->datum.p ) {
1,922,995✔
UNCOV
978
          return terrno;
×
979
        }
980

981
        (void)memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
1,922,713✔
982
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
1,924,095✔
983
        break;
1,923,826✔
984
      }
985
    }
986
  } else if(FUNCTION_TYPE_EXTERNAL_WINDOW_COLUMN == t) {
333,341,762✔
987
    if (NULL == pParamNodes || LIST_LENGTH(pParamNodes) < 2) {
10,186,603✔
NEW
988
      uError("invalid stream external window column param list %p, len: %d", pParamNodes,
×
989
             pParamNodes ? LIST_LENGTH(pParamNodes) : 0);
NEW
990
      return TSDB_CODE_INTERNAL_ERROR;
×
991
    }
992

993
    SNode* pParamNode = nodesListGetNode(pParamNodes, 1);
10,192,618✔
994
    if (NULL == pParamNode || QUERY_NODE_VALUE != nodeType(pParamNode)) {
10,196,227✔
995
      uError("invalid stream external window column param node %p type %d for func: %d", pParamNode,
3,208✔
996
             pParamNode ? nodeType(pParamNode) : -1, funcId);
NEW
997
      return TSDB_CODE_INTERNAL_ERROR;
×
998
    }
999

1000
    const SStreamGroupValue* pVal =
1001
        fmGetExternalWindowColumnFuncVal(pStreamRuntimeInfo, ((SValueNode*)pParamNode)->placeholderNo);
10,195,425✔
1002
    if (!pVal) {
10,202,242✔
NEW
1003
      uError("failed to set stream pseudo func param val, NULL val for funcId: %d", funcId);
×
NEW
1004
      return TSDB_CODE_INTERNAL_ERROR;
×
1005
    }
1006
    if (pVal->isNull) {
10,202,242✔
1007
      ((SValueNode*)pFirstParam)->isNull = true;
802✔
1008
    } else {
1009
      ((SValueNode*)pFirstParam)->isNull = false;
10,200,638✔
1010
      if (IS_VAR_DATA_TYPE(pVal->data.type)) {
10,200,638✔
1011
        // pVal->data.pData already includes the VARSTR header from colDataGetData,
1012
        // so copy it directly as datum.p expects VARSTR format [header][raw data].
1013
        char* tmp = taosMemoryMalloc(pVal->data.nData);
8,822✔
1014
        if (tmp == NULL) {
8,020✔
NEW
1015
          return TSDB_CODE_OUT_OF_MEMORY;
×
1016
        }
1017
        taosMemoryFree(((SValueNode*)pFirstParam)->datum.p);
8,020✔
1018
        ((SValueNode*)pFirstParam)->datum.p = tmp;
8,020✔
1019
        memcpy(tmp, pVal->data.pData, pVal->data.nData);
8,020✔
1020
      } else {
1021
        code = nodesSetValueNodeValue((SValueNode*)pFirstParam, (void*)&pVal->data.val);
10,192,618✔
1022
      }
1023
    }
1024
    if (code != 0) {
10,190,212✔
NEW
1025
      uError("failed to set value node value: %s", tstrerror(code));
×
NEW
1026
      return code;
×
1027
    }
1028
  } else if (LIST_LENGTH(pParamNodes) == 1) {
646,293,821✔
1029
    // twstart, twend
1030
    const void* pVal = fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
323,144,383✔
1031
    if (!pVal) {
323,139,671✔
UNCOV
1032
      uError("failed to set stream pseudo func param val, NULL val for funcId: %d", funcId);
×
UNCOV
1033
      return TSDB_CODE_INTERNAL_ERROR;
×
1034
    }
1035
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, (void*)pVal);
323,139,671✔
1036
    if (code != 0) {
323,143,317✔
1037
      uError("failed to set value node value: %s", tstrerror(code));
4,655✔
UNCOV
1038
      return code;
×
1039
    }
1040
  } else {
1041
    uError("invalid placeholder function type: %d", t);
187,917✔
UNCOV
1042
    return TSDB_CODE_INTERNAL_ERROR;
×
1043
  }
1044
  
1045
  return code;
336,202,773✔
1046
}
1047

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