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

taosdata / TDengine / #5028

20 Apr 2026 09:07AM UTC coverage: 72.986% (-0.01%) from 72.996%
#5028

push

travis-ci

web-flow
perf: optimize compact progress query from O(m*n) to O(n+m) (#35115)

104 of 141 new or added lines in 4 files covered. (73.76%)

5170 existing lines in 133 files now uncovered.

273777 of 375111 relevant lines covered (72.99%)

130458474.51 hits per line

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

82.08
/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,935,673✔
37
  // just to make sure the funcId of these functions are initialized before used.
38
  (void)fmGetTwstartFuncId();
1,935,673✔
39
  (void)fmGetTwendFuncId();
1,935,673✔
40
  (void)fmGetTwdurationFuncId();
1,935,673✔
41
  (void)fmGetExternalWindowColumnFuncId();
1,935,673✔
42
}
1,935,673✔
43

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

52
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
447,140,463✔
53
    if (TSDB_CODE_SUCCESS != taosHashPut(gFunMgtService.pFuncNameHashTable, funcMgtBuiltins[i].name,
445,204,790✔
54
                                         strlen(funcMgtBuiltins[i].name), &i, sizeof(int32_t))) {
445,204,790✔
55
      initFunctionCode = terrno;
×
56
      return;
×
57
    }
58
  }
59
  InitSpecialFuncId();
1,935,673✔
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,586,863✔
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,935,673✔
75
  (void)taosThreadOnce(&functionHashTableInit, doInitFunctionTable);
1,935,673✔
76
  return initFunctionCode;
1,935,673✔
77
}
78

79
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
842,904,297✔
80
  if (NULL != gFunMgtService.pFuncNameHashTable) {
842,904,297✔
81
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
842,739,108✔
82
    if (NULL != pVal) {
842,764,028✔
83
      pFunc->funcId = *(int32_t*)pVal;
842,560,626✔
84
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
842,559,839✔
85
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
842,558,667✔
86
    }
87
    return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
203,402✔
88
  }
89
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
3,258,843✔
90
    if (0 == strcmp(funcMgtBuiltins[i].name, pFunc->functionName)) {
3,259,376✔
91
      pFunc->funcId = i;
175,168✔
92
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
175,168✔
93
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
175,168✔
94
    }
95
  }
96
  return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
×
97
}
98

99
EFuncReturnRows fmGetFuncReturnRows(SFunctionNode* pFunc) {
17,973,135✔
100
  if (NULL != funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc) {
17,973,135✔
101
    return funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc(pFunc);
12,693,847✔
102
  }
103
  return (fmIsIndefiniteRowsFunc(pFunc->funcId) || fmIsMultiRowsFunc(pFunc->funcId)) ? FUNC_RETURN_ROWS_INDEFINITE
5,279,288✔
104
                                                                                     : FUNC_RETURN_ROWS_NORMAL;
5,279,288✔
105
}
106

107
bool canCoexistIndefiniteRowsFunc(int32_t funcId1, int32_t funcId2) {
177,763✔
108
  if (fmIsUserDefinedFunc(funcId1) || funcId1 < 0 || funcId1 >= funcMgtBuiltinsNum ||
355,526✔
109
      fmIsUserDefinedFunc(funcId2) || funcId2 < 0 || funcId2 >= funcMgtBuiltinsNum) {
355,526✔
110
    return false;
×
111
  }
112
  if (funcId1 == funcId2) {
177,763✔
113
    return true;
162,374✔
114
  }
115
  if ((funcMgtBuiltins[funcId1].type == FUNCTION_TYPE_LAG || funcMgtBuiltins[funcId1].type == FUNCTION_TYPE_LEAD) &&
15,389✔
116
      (funcMgtBuiltins[funcId2].type == FUNCTION_TYPE_LAG || funcMgtBuiltins[funcId2].type == FUNCTION_TYPE_LEAD)) {
14,949✔
117
    return true;
14,949✔
118
  }
119
  return false;
440✔
120
}
121

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

126
EFunctionType fmGetFuncType(const char* pFunc) {
615,091,893✔
127
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
615,091,893✔
128
  if (NULL != pVal) {
615,105,796✔
129
    return funcMgtBuiltins[*(int32_t*)pVal].type;
614,899,644✔
130
  }
131
  return FUNCTION_TYPE_UDF;
206,152✔
132
}
133

134
EFunctionType fmGetFuncTypeFromId(int32_t funcId) {
23,758,077✔
135
  if (funcId >= 0 && funcId < funcMgtBuiltinsNum) {
23,758,077✔
136
    return funcMgtBuiltins[funcId].type;
23,758,465✔
137
  }
UNCOV
138
  return FUNCTION_TYPE_UDF;
×
139
}
140

141
EFuncDataRequired fmFuncDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
41,552,387✔
142
  if (fmIsUserDefinedFunc(pFunc->funcId) || pFunc->funcId < 0 || pFunc->funcId >= funcMgtBuiltinsNum) {
41,552,387✔
143
    return FUNC_DATA_REQUIRED_DATA_LOAD;
942✔
144
  }
145
  if (NULL == funcMgtBuiltins[pFunc->funcId].dataRequiredFunc) {
41,557,619✔
146
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
147
  }
148
  return funcMgtBuiltins[pFunc->funcId].dataRequiredFunc(pFunc, pTimeWindow);
41,557,541✔
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;
×
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;
1,035,188✔
159
    ;
160
  }
161

162
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
2,147,483,647✔
163
    return FUNC_DATA_REQUIRED_DATA_LOAD;
192,642,247✔
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) {
287,655,593✔
170
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
287,655,593✔
171
    return TSDB_CODE_FAILED;
140✔
172
  }
173
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
287,662,427✔
174
  pFpSet->init = funcMgtBuiltins[funcId].initFunc;
287,599,292✔
175
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
287,619,262✔
176
  pFpSet->finalize = funcMgtBuiltins[funcId].finalizeFunc;
287,602,888✔
177
  pFpSet->combine = funcMgtBuiltins[funcId].combineFunc;
287,627,265✔
178
  pFpSet->processFuncByRow = funcMgtBuiltins[funcId].processFuncByRow;
287,633,068✔
179
  pFpSet->cleanup = funcMgtBuiltins[funcId].cleanupFunc;
287,610,266✔
180
  return TSDB_CODE_SUCCESS;
287,628,123✔
181
}
182

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

198
int32_t fmGetScalarFuncExecFuncs(int32_t funcId, SScalarFuncExecFuncs* pFpSet) {
1,129,947,450✔
199
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,129,947,450✔
200
    return TSDB_CODE_FAILED;
68,901✔
201
  }
202
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
1,130,088,383✔
203
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
1,130,074,962✔
204
  return TSDB_CODE_SUCCESS;
1,130,091,043✔
205
}
206

207
bool fmIsAggFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_AGG_FUNC); }
2,147,483,647✔
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,311,812,366✔
212

213
bool fmIsSelectColsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_COLS_FUNC); }
155,518,350✔
214

215
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
1,871,413,405✔
216

217
bool fmIsTimelineFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TIMELINE_FUNC); }
1,184,256,310✔
218

219
bool fmIsDateTimeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_DATETIME_FUNC); }
598,952,432✔
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,438,781,512✔
226

227
bool fmIsWindowClauseFunc(int32_t funcId) { return fmIsAggFunc(funcId) || fmIsWindowPseudoColumnFunc(funcId); }
60,570,937✔
228

229
bool fmIsWindowIndefRowsFunc(int32_t funcId) { return fmIsVectorFunc(funcId) || fmIsWindowPseudoColumnFunc(funcId); }
393,923✔
230

231
bool fmIsStreamWindowClauseFunc(int32_t funcId) { return fmIsWindowClauseFunc(funcId) || fmIsPlaceHolderFunc(funcId); }
898,255✔
232

233
bool fmIsStreamVectorFunc(int32_t funcId) { return fmIsVectorFunc(funcId) || fmIsPlaceHolderFunc(funcId); }
12,325✔
234

235
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
1,604,383,971✔
236

237
bool fmIsSpecialDataRequiredFunc(int32_t funcId) {
93,550,395✔
238
  return isSpecificClassifyFunc(funcId, FUNC_MGT_SPECIAL_DATA_REQUIRED);
93,550,395✔
239
}
240

241
bool fmIsDynamicScanOptimizedFunc(int32_t funcId) {
44,680,447✔
242
  return isSpecificClassifyFunc(funcId, FUNC_MGT_DYNAMIC_SCAN_OPTIMIZED);
44,680,447✔
243
}
244

245
bool fmIsMultiResFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_RES_FUNC); }
1,220,062,852✔
246

247
bool fmIsRepeatScanFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_REPEAT_SCAN_FUNC); }
1,197,225,459✔
248

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

251
bool fmIsForbidFillFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_FILL_FUNC); }
599,218,497✔
252

253
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
63,922,451✔
254

255
bool fmIsSystemInfoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SYSTEM_INFO_FUNC); }
602,217,305✔
256

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

259
bool fmIsClientPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CLIENT_PC_FUNC); }
609,851,848✔
260

261
bool fmIsMultiRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_ROWS_FUNC); }
1,193,856,383✔
262

263
static bool fmIsKeepOrderFuncId(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_KEEP_ORDER_FUNC); }
723,743,688✔
264

265
bool fmIsKeepOrderFunc(SFunctionNode* pFunc) {
730,133,107✔
266
  if (pFunc->funcType == FUNCTION_TYPE_TOP || pFunc->funcType == FUNCTION_TYPE_BOTTOM ||
730,133,107✔
267
      pFunc->funcType == FUNCTION_TYPE_SAMPLE) {
726,106,797✔
268
    if (pFunc->pParameterList != NULL && pFunc->pParameterList->length >= 2) {
6,394,358✔
269
      SNode* pParam = nodesListGetNode(pFunc->pParameterList, 1);
6,409,080✔
270
      if (pParam != NULL && nodeType(pParam) == QUERY_NODE_VALUE) {
6,409,080✔
271
        SValueNode* pConst = (SValueNode*)pParam;
6,409,080✔
272
        if (pConst->datum.i == 1) {
6,409,080✔
273
          return true;
750,340✔
274
        }
275
      }
276
    }
277
    return false;
5,648,531✔
278
  }
279
  return fmIsKeepOrderFuncId(pFunc->funcId);
723,754,391✔
280
}
281

282
bool fmIsCumulativeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CUMULATIVE_FUNC); }
158,025,143✔
283

284
bool fmIsForbidSysTableFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_SYSTABLE_FUNC); }
599,145,053✔
285

286
bool fmIsInterpFunc(int32_t funcId) {
1,227,430,083✔
287
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,227,430,083✔
288
    return false;
378,655✔
289
  }
290
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
1,227,055,436✔
291
}
292

293
bool fmIsInterpPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERP_PC_FUNC); }
1,208,248,693✔
294

295
bool fmIsForecastFunc(int32_t funcId) {
1,178,221,151✔
296
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,178,221,151✔
297
    return false;
385,191✔
298
  }
299
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
1,177,838,140✔
300
}
301

302
bool fmIsAnalysisPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_ANALYTICS_PC_FUNC); }
1,211,218,629✔
303

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

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

314
bool fmIsLastRowFunc(int32_t funcId) {
71,336,181✔
315
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
71,336,181✔
316
    return false;
2✔
317
  }
318
  return FUNCTION_TYPE_LAST_ROW == funcMgtBuiltins[funcId].type;
71,336,630✔
319
}
320

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

328
bool fmIsNotNullOutputFunc(int32_t funcId) {
436,767,360✔
329
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
436,767,360✔
330
    return false;
92,087✔
331
  }
332
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
436,681,388✔
333
         FUNCTION_TYPE_CACHE_LAST == funcMgtBuiltins[funcId].type ||
421,089,330✔
334
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
419,751,818✔
335
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
410,895,600✔
336
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
407,302,327✔
337
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
397,438,155✔
338
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
392,573,026✔
339
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
390,566,713✔
340
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
340,085,446✔
341
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
1,194,013,062✔
342
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
336,239,634✔
343
}
344

345
bool fmIsSelectValueFunc(int32_t funcId) {
344,934,253✔
346
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
344,934,253✔
UNCOV
347
    return false;
×
348
  }
349
  return FUNCTION_TYPE_SELECT_VALUE == funcMgtBuiltins[funcId].type;
344,935,111✔
350
}
351

352
bool fmIsGroupKeyFunc(int32_t funcId) {
263,182,856✔
353
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
263,182,856✔
UNCOV
354
    return false;
×
355
  }
356
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
263,183,298✔
357
}
358

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

366
bool fmisSelectGroupConstValueFunc(int32_t funcId) {
17,529,491✔
367
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
17,529,491✔
UNCOV
368
    return false;
×
369
  }
370
  return FUNCTION_TYPE_GROUP_CONST_VALUE == funcMgtBuiltins[funcId].type;
17,529,491✔
371
}
372

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

380
bool fmIsBlockDistFunc(int32_t funcId) {
598,946,535✔
381
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
598,946,535✔
382
    return false;
176,739✔
383
  }
384
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
598,775,810✔
385
}
386

387
bool fmIsDBUsageFunc(int32_t funcId) {
598,949,669✔
388
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
598,949,669✔
389
    return false;
188,245✔
390
  }
391
  return FUNCTION_TYPE_DB_USAGE == funcMgtBuiltins[funcId].type;
598,763,928✔
392
}
393

394
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
803,828,144✔
395

396
bool fmIsIgnoreNullFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IGNORE_NULL_FUNC); }
294,900✔
397

398
void fmFuncMgtDestroy() {
1,935,722✔
399
  void* m = gFunMgtService.pFuncNameHashTable;
1,935,722✔
400
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
1,935,722✔
401
    taosHashCleanup(m);
1,935,673✔
402
  }
403
}
1,935,722✔
404

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

415
// function has same input/output type
416
bool fmIsSameInOutType(int32_t funcId) {
48,486,275✔
417
  bool res = false;
48,486,275✔
418
  switch (funcMgtBuiltins[funcId].type) {
48,486,275✔
419
    case FUNCTION_TYPE_MAX:
19,399,946✔
420
    case FUNCTION_TYPE_MIN:
421
    case FUNCTION_TYPE_TOP:
422
    case FUNCTION_TYPE_BOTTOM:
423
    case FUNCTION_TYPE_FIRST:
424
    case FUNCTION_TYPE_LAST:
425
    case FUNCTION_TYPE_SAMPLE:
426
    case FUNCTION_TYPE_TAIL:
427
    case FUNCTION_TYPE_UNIQUE:
428
      res = true;
19,399,946✔
429
      break;
19,399,946✔
430
    default:
29,086,329✔
431
      break;
29,086,329✔
432
  }
433
  return res;
48,486,275✔
434
}
435

436
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
14,973,400✔
437
  SNode* pNode;
438
  FOREACH(pNode, pFunc->pParameterList) {
15,059,512✔
439
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
151,316✔
440
      return false;
65,204✔
441
    }
442
  }
443
  return true;
14,908,196✔
444
}
445

446
bool fmIsSkipScanCheckFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SKIP_SCAN_CHECK_FUNC); }
44,676,495✔
447

448
bool fmIsPrimaryKeyFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PRIMARY_KEY_FUNC); }
522,370,457✔
449

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

452
bool fmIsPlaceHolderFuncForExternalWin(int32_t funcId) {
525,580✔
453
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
525,580✔
UNCOV
454
    return false;
×
455
  }
456
  return (funcMgtBuiltins[funcId].type == FUNCTION_TYPE_WSTART || funcMgtBuiltins[funcId].type == FUNCTION_TYPE_WEND ||
879,553✔
457
          funcMgtBuiltins[funcId].type == FUNCTION_TYPE_WDURATION);
353,973✔
458
}
459

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

466
static int32_t getFuncInfo(SFunctionNode* pFunc) {
155,145,060✔
467
  char msg[128] = {0};
155,145,060✔
468
  return fmGetFuncInfo(pFunc, msg, sizeof(msg));
155,145,060✔
469
}
470

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

488
static void resetOutputChangedFunc(SFunctionNode *pFunc, const SFunctionNode* pSrcFunc) {
144,975,288✔
489
  if (funcMgtBuiltins[pFunc->funcId].type == FUNCTION_TYPE_LAST_MERGE) {
144,975,288✔
490
    pFunc->node.resType = pSrcFunc->node.resType;
23,260,345✔
491
    return;
23,260,345✔
492
  }
493
}
494

495
int32_t createFunctionWithSrcFunc(const char* pName, const SFunctionNode* pSrcFunc, SNodeList* pParameterList, SFunctionNode** ppFunc) {
144,975,773✔
496
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
144,975,773✔
497
  if (NULL == *ppFunc) {
144,979,682✔
UNCOV
498
    return code;
×
499
  }
500

501
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
144,979,682✔
502
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
144,979,641✔
503
  (*ppFunc)->pSrcFuncRef = pSrcFunc;
144,979,641✔
504

505
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
144,979,641✔
506
  (*ppFunc)->pParameterList = pParameterList;
144,979,549✔
507
  code = getFuncInfo((*ppFunc));
144,979,637✔
508
  if (TSDB_CODE_SUCCESS != code) {
144,977,468✔
509
    (*ppFunc)->pParameterList = NULL;
×
510
    nodesDestroyNode((SNode*)*ppFunc);
×
UNCOV
511
    *ppFunc = NULL;
×
UNCOV
512
    return code;
×
513
  }
514
  resetOutputChangedFunc(*ppFunc, pSrcFunc);
144,977,468✔
515
  (*ppFunc)->node.relatedTo = pSrcFunc->node.relatedTo;
144,977,331✔
516
  (*ppFunc)->node.bindExprID = pSrcFunc->node.bindExprID;
144,977,300✔
517
  return code;
144,977,382✔
518
}
519

520
static int32_t createColumnByFunc(const SFunctionNode* pFunc, SColumnNode** ppCol) {
96,481,029✔
521
  int32_t code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)ppCol);
96,481,029✔
522
  if (NULL == *ppCol) {
96,487,544✔
UNCOV
523
    return code;
×
524
  }
525
  tstrncpy((*ppCol)->colName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
96,487,544✔
526
  (*ppCol)->node.resType = pFunc->node.resType;
96,487,544✔
527
  return TSDB_CODE_SUCCESS;
96,487,493✔
528
}
529

530
bool fmIsDistExecFunc(int32_t funcId) {
376,355,643✔
531
  if (fmIsUserDefinedFunc(funcId)) {
376,355,643✔
532
    return false;
104,171✔
533
  }
534
  if (!fmIsVectorFunc(funcId)) {
376,277,723✔
535
    return true;
409,454✔
536
  }
537
  return (NULL != funcMgtBuiltins[funcId].pPartialFunc && NULL != funcMgtBuiltins[funcId].pMergeFunc);
375,873,970✔
538
}
539

540
static int32_t createPartialFunction(const SFunctionNode* pSrcFunc, SFunctionNode** pPartialFunc) {
48,487,124✔
541
  SNodeList* pParameterList = NULL;
48,487,124✔
542
  int32_t    code = nodesCloneList(pSrcFunc->pParameterList, &pParameterList);
48,487,124✔
543
  if (NULL == pParameterList) {
48,490,277✔
UNCOV
544
    return code;
×
545
  }
546
  code =
547
      createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pPartialFunc, pSrcFunc, pParameterList, pPartialFunc);
48,490,277✔
548
  if (TSDB_CODE_SUCCESS != code) {
48,487,998✔
UNCOV
549
    nodesDestroyList(pParameterList);
×
UNCOV
550
    return code;
×
551
  }
552
  (*pPartialFunc)->hasOriginalFunc = true;
48,488,542✔
553
  (*pPartialFunc)->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
48,488,583✔
554
  char name[TSDB_FUNC_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_POINTER_PRINT_BYTES + 1] = {0};
48,488,583✔
555

556
  int32_t len = snprintf(name, sizeof(name), "%s.%p", (*pPartialFunc)->functionName, pSrcFunc);
48,488,583✔
557
  if (taosHashBinary(name, len, sizeof(name)) < 0) {
48,488,959✔
UNCOV
558
    return TSDB_CODE_FAILED;
×
559
  }
560
  tstrncpy((*pPartialFunc)->node.aliasName, name, TSDB_COL_NAME_LEN);
48,488,959✔
561
  return TSDB_CODE_SUCCESS;
48,489,051✔
562
}
563

564
static int32_t createMergeFuncPara(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
96,480,558✔
565
                                   SNodeList** pParameterList) {
566
  SNode*  pRes = NULL;
96,480,558✔
567
  int32_t code = createColumnByFunc(pPartialFunc, (SColumnNode**)&pRes);
96,480,558✔
568
  if (TSDB_CODE_SUCCESS != code) {
96,486,973✔
UNCOV
569
    return code;
×
570
  }
571
  if (NULL != funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc) {
96,486,973✔
572
    return funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc(pSrcFunc->pParameterList, pRes, pParameterList);
759,922✔
573
  } else {
574
    return nodesListMakeStrictAppend(pParameterList, pRes);
95,727,010✔
575
  }
576
}
577

578
static int32_t createMidFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
47,994,846✔
579
                                 SFunctionNode** pMidFunc) {
580
  SNodeList*     pParameterList = NULL;
47,994,846✔
581
  SFunctionNode* pFunc = NULL;
47,994,891✔
582

583
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
47,994,891✔
584
  if (TSDB_CODE_SUCCESS == code) {
47,999,939✔
585
    if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){
47,999,939✔
586
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pSrcFunc, pParameterList, &pFunc);
5,478,280✔
587
    }else{
588
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
42,521,659✔
589
    }
590
  }
591
  if (TSDB_CODE_SUCCESS == code) {
47,998,287✔
592
    tstrncpy(pFunc->node.aliasName, pPartialFunc->node.aliasName, TSDB_COL_NAME_LEN);
47,998,065✔
593
  }
594

595
  if (TSDB_CODE_SUCCESS == code) {
47,998,287✔
596
    *pMidFunc = pFunc;
47,998,287✔
597
  } else {
UNCOV
598
    nodesDestroyList(pParameterList);
×
599
  }
600
  return code;
47,998,841✔
601
}
602

603
static int32_t createMergeFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
48,487,007✔
604
                                   SFunctionNode** pMergeFunc) {
605
  SNodeList*     pParameterList = NULL;
48,487,007✔
606
  SFunctionNode* pFunc = NULL;
48,487,007✔
607

608
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
48,486,966✔
609
  if (TSDB_CODE_SUCCESS == code) {
48,490,115✔
610
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
48,490,115✔
611
  }
612
  if (TSDB_CODE_SUCCESS == code) {
48,492,541✔
613
    pFunc->hasOriginalFunc = true;
48,490,183✔
614
    pFunc->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
48,490,183✔
615
    // overwrite function restype set by translate function
616
    if (fmIsSameInOutType(pSrcFunc->funcId)) {
48,490,183✔
617
      pFunc->node.resType = pSrcFunc->node.resType;
19,400,218✔
618
    }
619
    tstrncpy(pFunc->node.aliasName, pSrcFunc->node.aliasName, TSDB_COL_NAME_LEN);
48,487,006✔
620
  }
621

622
  if (TSDB_CODE_SUCCESS == code) {
48,489,368✔
623
    *pMergeFunc = pFunc;
48,489,368✔
624
  } else {
UNCOV
625
    nodesDestroyList(pParameterList);
×
626
  }
627
  return code;
48,489,435✔
628
}
629

630
int32_t fmGetDistMethod(const SFunctionNode* pFunc, SFunctionNode** pPartialFunc, SFunctionNode** pMidFunc,
48,484,419✔
631
                        SFunctionNode** pMergeFunc) {
632
  if (!fmIsDistExecFunc(pFunc->funcId)) {
48,484,419✔
UNCOV
633
    return TSDB_CODE_FAILED;
×
634
  }
635

636
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
48,489,408✔
637
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
48,486,667✔
638
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
47,998,433✔
639
  }
640
  if (TSDB_CODE_SUCCESS == code) {
48,487,373✔
641
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
48,489,953✔
642
  }
643

644
  if (TSDB_CODE_SUCCESS != code) {
48,484,495✔
645
    nodesDestroyNode((SNode*)*pPartialFunc);
×
UNCOV
646
    if (pMidFunc) nodesDestroyNode((SNode*)*pMidFunc);
×
UNCOV
647
    nodesDestroyNode((SNode*)*pMergeFunc);
×
648
  }
649

650
  return code;
48,488,011✔
651
}
652

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

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

678
bool fmIsTSMASupportedFunc(func_id_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC); }
1,282,141✔
679

680
bool fmIsRsmaSupportedFunc(func_id_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_RSMA_FUNC); }
137,638✔
681

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

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

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

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

764
int32_t fmGetTwstartFuncId() {
2,075,854✔
765
  static int32_t twstartFuncId = -2;
766
  if (twstartFuncId < 0) {
2,075,854✔
767
    twstartFuncId = fmGetFuncId("_twstart");
1,935,673✔
768
  }
769
  return twstartFuncId;
2,075,854✔
770
}
771

772
int32_t fmGetTwendFuncId() {
1,967,099✔
773
  static int32_t twendFuncId = -2;
774
  if (twendFuncId < 0) {
1,967,099✔
775
    twendFuncId = fmGetFuncId("_twend");
1,935,673✔
776
  }
777
  return twendFuncId;
1,967,099✔
778
}
779

780
int32_t fmGetTwdurationFuncId() {
1,936,090✔
781
  static int32_t twdurationFuncId = -2;
782
  if (twdurationFuncId < 0) {
1,936,090✔
783
    twdurationFuncId = fmGetFuncId("_twduration");
1,935,673✔
784
  }
785
  return twdurationFuncId;
1,936,090✔
786
}
787

788
int32_t fmGetExternalWindowColumnFuncId() {
1,971,957✔
789
  static int32_t externalWindowColumnFuncId = -2;
790
  if (externalWindowColumnFuncId < 0) {
1,971,957✔
791
    externalWindowColumnFuncId = fmGetFuncId("_external_window_column");
1,935,673✔
792
  }
793
  return externalWindowColumnFuncId;
1,971,957✔
794
}
795

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

811
bool fmIsCountLikeFunc(int32_t funcId) {
583,365,547✔
812
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
583,365,547✔
813
}
814

815
bool fmIsRowTsOriginFunc(int32_t funcId) {
5,423,533✔
816
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
5,423,533✔
UNCOV
817
    return false;
×
818
  }
819
  return FUNCTION_TYPE_IROWTS_ORIGIN == funcMgtBuiltins[funcId].type;
5,423,533✔
820
}
821

822
bool fmIsGroupIdFunc(int32_t funcId) {
2,333,372✔
823
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,333,372✔
824
    return false;
×
825
  }
826
  return FUNCTION_TYPE_GROUP_ID == funcMgtBuiltins[funcId].type;
2,333,372✔
827
}
828

829
const void* fmGetExternalWindowColumnFuncVal(const SStreamRuntimeFuncInfo* pStreamRuntimeFuncInfo, int32_t index) {
10,350,552✔
830
  SSTriggerCalcParam *pParams = taosArrayGet(pStreamRuntimeFuncInfo->pStreamPesudoFuncVals, pStreamRuntimeFuncInfo->curIdx);
10,350,552✔
831
  if (pParams == NULL || pParams->pExternalWindowData == NULL || index < 0) {
10,379,928✔
UNCOV
832
    return NULL;
×
833
  }
834
  return taosArrayGet(pParams->pExternalWindowData, index);
10,380,336✔
835
}
836

837
const void* fmGetStreamPesudoFuncVal(int32_t funcId, const SStreamRuntimeFuncInfo* pStreamRuntimeFuncInfo) {
389,979,783✔
838
  switch (funcMgtBuiltins[funcId].type) {
389,979,783✔
839
    case FUNCTION_TYPE_TGRPID:
135,870✔
840
      return &pStreamRuntimeFuncInfo->groupId;
135,870✔
841
    case FUNCTION_TYPE_PLACEHOLDER_COLUMN:
895,327✔
842
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
895,327✔
843
    case FUNCTION_TYPE_PLACEHOLDER_TBNAME:
2,075,439✔
844
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
2,075,439✔
845
    default:
386,924,092✔
846
      break;
386,924,092✔
847
  }
848

849
  SSTriggerCalcParam *pParams = taosArrayGet(pStreamRuntimeFuncInfo->pStreamPesudoFuncVals, pStreamRuntimeFuncInfo->curIdx);
386,924,092✔
850
  switch (funcMgtBuiltins[funcId].type) {
386,979,831✔
UNCOV
851
    case FUNCTION_TYPE_TPREV_TS:
×
UNCOV
852
      return &pParams->prevTs;
×
853
    case FUNCTION_TYPE_TCURRENT_TS:
2,243,046✔
854
      return &pParams->currentTs;
2,243,046✔
UNCOV
855
    case FUNCTION_TYPE_TNEXT_TS:
×
UNCOV
856
      return &pParams->nextTs;
×
857
    case FUNCTION_TYPE_TWSTART:
334,447,859✔
858
      return &pParams->wstart;
334,447,859✔
859
    case FUNCTION_TYPE_TWEND:
17,336,036✔
860
      return &pParams->wend;
17,336,036✔
861
    case FUNCTION_TYPE_TWDURATION:
1,083,381✔
862
      return &pParams->wduration;
1,083,381✔
863
    case FUNCTION_TYPE_TWROWNUM:
30,340,700✔
864
      return &pParams->wrownum;
30,340,700✔
865
    case FUNCTION_TYPE_TPREV_LOCALTIME:
288,017✔
866
      return &pParams->prevLocalTime;
288,017✔
867
    case FUNCTION_TYPE_TLOCALTIME:
766,528✔
868
      return &pParams->triggerTime;
766,528✔
869
    case FUNCTION_TYPE_TNEXT_LOCALTIME:
442,071✔
870
      return &pParams->nextLocalTime;
442,071✔
871
    case FUNCTION_TYPE_TGRPID:
×
872
      return &pStreamRuntimeFuncInfo->groupId;
×
873
    case FUNCTION_TYPE_PLACEHOLDER_COLUMN:
×
874
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
×
UNCOV
875
    case FUNCTION_TYPE_PLACEHOLDER_TBNAME:
×
UNCOV
876
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
×
877
    case FUNCTION_TYPE_TIDLESTART:
6,048✔
878
      return &pParams->idlestart;
6,048✔
879
    case FUNCTION_TYPE_TIDLEEND:
6,048✔
880
      return &pParams->idleend;
6,048✔
UNCOV
881
    default:
×
882
      break;
×
883
  }
UNCOV
884
  return NULL;
×
885
}
886

887
bool fmIsStreamPesudoColVal(int32_t funcId) {
524,261✔
888
  return funcMgtBuiltins[funcId].type == FUNCTION_TYPE_PLACEHOLDER_COLUMN
524,261✔
889
         || funcMgtBuiltins[funcId].type == FUNCTION_TYPE_PLACEHOLDER_TBNAME;
524,261✔
890
}
891

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

898
  int32_t firstParamType = nodeType(nodesListGetNode(pParamNodes, 0));
7,361,473✔
899
  if (QUERY_NODE_VALUE != firstParamType) {
7,360,935✔
UNCOV
900
    uError("invalid stream pesudo func first param type %d", firstParamType);
×
UNCOV
901
    return TSDB_CODE_INTERNAL_ERROR;
×
902
  }
903

904
  SValueNode* pResDesc = (SValueNode*)nodesListGetNode(pParamNodes, 0);
7,360,935✔
905
  pEnv->calcMemSize = pResDesc->node.resType.bytes;
7,361,476✔
906

907
  return TSDB_CODE_SUCCESS;
7,361,985✔
908
}
909

910

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

924
  int32_t t = funcMgtBuiltins[funcId].type;
400,384,564✔
925
  uDebug("set stream pseudo func param val, functype: %d, pStreamRuntimeInfo: %p", t, pStreamRuntimeInfo);
400,373,258✔
926
  if (FUNCTION_TYPE_TGRPID == t) {
400,383,709✔
927
    SValue v = {0};
135,870✔
928
    v.type = TSDB_DATA_TYPE_BIGINT;
135,870✔
929
    v.val = *(int64_t*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
135,870✔
930
    
931
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&v, pFirstParam->type));
135,870✔
932
    if (code != 0) {
135,870✔
UNCOV
933
      uError("failed to set value node value: %s", tstrerror(code));
×
UNCOV
934
      return code;
×
935
    }
936
  } else if (FUNCTION_TYPE_PLACEHOLDER_COLUMN == t) {
400,247,839✔
937
    SNode* pSecondParam = nodesListGetNode(pParamNodes, 1);
895,177✔
938
    if (nodeType(pSecondParam) != QUERY_NODE_VALUE) {
895,587✔
UNCOV
939
      uError("invalid param node type: %d for func: %d", nodeType(pSecondParam), funcId);
×
UNCOV
940
      return TSDB_CODE_INTERNAL_ERROR;
×
941
    }
942
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
895,327✔
943
    int32_t idx = ((SValueNode*)pSecondParam)->datum.i;
895,327✔
944
    if (idx - 1 < 0 || idx - 1 >= taosArrayGetSize(pVal)) {
895,327✔
UNCOV
945
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
×
UNCOV
946
      return TSDB_CODE_INTERNAL_ERROR;
×
947
    }
948
    SStreamGroupValue* pValue = taosArrayGet(pVal, idx - 1);
895,327✔
949
    if (pValue == NULL) {
895,587✔
UNCOV
950
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
×
UNCOV
951
      return TSDB_CODE_INTERNAL_ERROR;
×
952
    }
953
    if (!pValue->isNull){
895,587✔
954
      if (pValue->data.type != ((SValueNode*)pFirstParam)->node.resType.type){
889,042✔
UNCOV
955
        uError("invalid value type: %d for func: %d, should be: %d", pValue->data.type, funcId, ((SValueNode*)pFirstParam)->node.resType.type);
×
UNCOV
956
        return TSDB_CODE_INTERNAL_ERROR;
×
957
      }
958
      if (IS_VAR_DATA_TYPE(((SValueNode*)pFirstParam)->node.resType.type)) {
888,782✔
959
        char* tmp = taosMemoryCalloc(1, pValue->data.nData + VARSTR_HEADER_SIZE); 
612,094✔
960
        if (tmp == NULL) {
612,094✔
UNCOV
961
          return TSDB_CODE_OUT_OF_MEMORY;
×
962
        }
963
        taosMemoryFree(((SValueNode*)pFirstParam)->datum.p);
612,094✔
964
        ((SValueNode*)pFirstParam)->datum.p = tmp;
611,834✔
965
        memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
611,834✔
966
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
611,834✔
967
      } else {
968
        code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&pValue->data, pValue->data.type));
276,948✔
969
      }
970
    }
971
    ((SValueNode*)pFirstParam)->isNull = pValue->isNull;
895,067✔
972
  } else if (FUNCTION_TYPE_PLACEHOLDER_TBNAME == t) {
399,352,662✔
973
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
2,002,315✔
974
    for (int32_t i = 0; i < taosArrayGetSize(pVal); ++i) {
2,436,266✔
975
      SStreamGroupValue* pValue = taosArrayGet(pVal, i);
2,435,985✔
976
      if (pValue != NULL && pValue->isTbname) {
2,436,548✔
977
        taosMemoryFreeClear(((SValueNode*)pFirstParam)->datum.p);
2,004,548✔
978
        ((SValueNode*)pFirstParam)->datum.p = taosMemoryCalloc(pValue->data.nData + VARSTR_HEADER_SIZE, 1);
2,003,713✔
979
        if (NULL == ((SValueNode*)pFirstParam)->datum.p ) {
2,002,869✔
UNCOV
980
          return terrno;
×
981
        }
982

983
        (void)memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
2,003,720✔
984
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
2,003,439✔
985
        break;
2,003,985✔
986
      }
987
    }
988
  } else if(FUNCTION_TYPE_EXTERNAL_WINDOW_COLUMN == t) {
397,350,347✔
989
    if (NULL == pParamNodes || LIST_LENGTH(pParamNodes) < 2) {
10,364,424✔
990
      uError("invalid stream external window column param list %p, len: %d", pParamNodes,
×
991
             pParamNodes ? LIST_LENGTH(pParamNodes) : 0);
UNCOV
992
      return TSDB_CODE_INTERNAL_ERROR;
×
993
    }
994

995
    SNode* pParamNode = nodesListGetNode(pParamNodes, 1);
10,367,280✔
996
    if (NULL == pParamNode || QUERY_NODE_VALUE != nodeType(pParamNode)) {
10,372,992✔
997
      uError("invalid stream external window column param node %p type %d for func: %d", pParamNode,
9,384✔
998
             pParamNode ? nodeType(pParamNode) : -1, funcId);
UNCOV
999
      return TSDB_CODE_INTERNAL_ERROR;
×
1000
    }
1001

1002
    const SStreamGroupValue* pVal =
1003
        fmGetExternalWindowColumnFuncVal(pStreamRuntimeInfo, ((SValueNode*)pParamNode)->placeholderNo);
10,367,688✔
1004
    if (!pVal) {
10,378,296✔
UNCOV
1005
      uError("failed to set stream pseudo func param val, NULL val for funcId: %d", funcId);
×
UNCOV
1006
      return TSDB_CODE_INTERNAL_ERROR;
×
1007
    }
1008
    if (pVal->isNull) {
10,378,296✔
1009
      ((SValueNode*)pFirstParam)->isNull = true;
816✔
1010
    } else {
1011
      ((SValueNode*)pFirstParam)->isNull = false;
10,377,072✔
1012
      if (IS_VAR_DATA_TYPE(pVal->data.type)) {
10,379,520✔
1013
        // pVal->data.pData already includes the VARSTR header from colDataGetData,
1014
        // so copy it directly as datum.p expects VARSTR format [header][raw data].
1015
        char* tmp = taosMemoryMalloc(pVal->data.nData);
8,976✔
1016
        if (tmp == NULL) {
8,160✔
UNCOV
1017
          return TSDB_CODE_OUT_OF_MEMORY;
×
1018
        }
1019
        taosMemoryFree(((SValueNode*)pFirstParam)->datum.p);
8,160✔
1020
        ((SValueNode*)pFirstParam)->datum.p = tmp;
8,160✔
1021
        memcpy(tmp, pVal->data.pData, pVal->data.nData);
8,160✔
1022
      } else {
1023
        code = nodesSetValueNodeValue((SValueNode*)pFirstParam, (void*)&pVal->data.val);
10,370,544✔
1024
      }
1025
    }
1026
    if (code != 0) {
10,364,016✔
UNCOV
1027
      uError("failed to set value node value: %s", tstrerror(code));
×
UNCOV
1028
      return code;
×
1029
    }
1030
  } else if (LIST_LENGTH(pParamNodes) == 1) {
773,943,473✔
1031
    // twstart, twend
1032
    const void* pVal = fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
386,984,059✔
1033
    if (!pVal) {
386,950,671✔
UNCOV
1034
      uError("failed to set stream pseudo func param val, NULL val for funcId: %d", funcId);
×
UNCOV
1035
      return TSDB_CODE_INTERNAL_ERROR;
×
1036
    }
1037
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, (void*)pVal);
386,950,671✔
1038
    if (code != 0) {
386,985,797✔
1039
      uError("failed to set value node value: %s", tstrerror(code));
28,247✔
UNCOV
1040
      return code;
×
1041
    }
1042
  } else {
1043
    uError("invalid placeholder function type: %d", t);
42,422✔
UNCOV
1044
    return TSDB_CODE_INTERNAL_ERROR;
×
1045
  }
1046
  
1047
  return code;
400,380,456✔
1048
}
1049

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