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

taosdata / TDengine / #5050

12 May 2026 05:36AM UTC coverage: 73.398% (+0.09%) from 73.313%
#5050

push

travis-ci

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

90 of 101 new or added lines in 2 files covered. (89.11%)

489 existing lines in 125 files now uncovered.

281602 of 383662 relevant lines covered (73.4%)

138099127.08 hits per line

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

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

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

52
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
378,886,659✔
53
    if (TSDB_CODE_SUCCESS != taosHashPut(gFunMgtService.pFuncNameHashTable, funcMgtBuiltins[i].name,
377,260,536✔
54
                                         strlen(funcMgtBuiltins[i].name), &i, sizeof(int32_t))) {
377,260,536✔
55
      initFunctionCode = terrno;
×
56
      return;
×
57
    }
58
  }
59
  InitSpecialFuncId();
1,626,123✔
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);
9,841,967✔
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,626,123✔
75
  (void)taosThreadOnce(&functionHashTableInit, doInitFunctionTable);
1,626,123✔
76
  return initFunctionCode;
1,626,123✔
77
}
78

79
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
902,463,399✔
80
  if (NULL != gFunMgtService.pFuncNameHashTable) {
902,463,399✔
81
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
902,300,064✔
82
    if (NULL != pVal) {
902,350,085✔
83
      pFunc->funcId = *(int32_t*)pVal;
902,126,772✔
84
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
902,123,649✔
85
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
902,123,704✔
86
    }
87
    return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
223,313✔
88
  }
89
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
3,475,537✔
90
    if (0 == strcmp(funcMgtBuiltins[i].name, pFunc->functionName)) {
3,476,112✔
91
      pFunc->funcId = i;
186,816✔
92
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
186,816✔
93
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
186,816✔
94
    }
95
  }
UNCOV
96
  return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
×
97
}
98

99
EFuncReturnRows fmGetFuncReturnRows(SFunctionNode* pFunc) {
19,508,726✔
100
  if (NULL != funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc) {
19,508,726✔
101
    return funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc(pFunc);
13,797,887✔
102
  }
103
  return (fmIsIndefiniteRowsFunc(pFunc->funcId) || fmIsMultiRowsFunc(pFunc->funcId)) ? FUNC_RETURN_ROWS_INDEFINITE
5,711,993✔
104
                                                                                     : FUNC_RETURN_ROWS_NORMAL;
5,711,993✔
105
}
106

107
bool canCoexistIndefiniteRowsFunc(int32_t funcId1, int32_t funcId2) {
194,531✔
108
  if (fmIsUserDefinedFunc(funcId1) || funcId1 < 0 || funcId1 >= funcMgtBuiltinsNum ||
389,062✔
109
      fmIsUserDefinedFunc(funcId2) || funcId2 < 0 || funcId2 >= funcMgtBuiltinsNum) {
389,062✔
110
    return false;
×
111
  }
112
  if (funcId1 == funcId2) {
194,531✔
113
    return true;
177,702✔
114
  }
115
  if ((funcMgtBuiltins[funcId1].type == FUNCTION_TYPE_LAG || funcMgtBuiltins[funcId1].type == FUNCTION_TYPE_LEAD) &&
16,829✔
116
      (funcMgtBuiltins[funcId2].type == FUNCTION_TYPE_LAG || funcMgtBuiltins[funcId2].type == FUNCTION_TYPE_LEAD)) {
16,347✔
117
    return true;
16,347✔
118
  }
119
  return false;
482✔
120
}
121

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

126
EFunctionType fmGetFuncType(const char* pFunc) {
666,961,447✔
127
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
666,961,447✔
128
  if (NULL != pVal) {
667,003,181✔
129
    return funcMgtBuiltins[*(int32_t*)pVal].type;
666,775,246✔
130
  }
131
  return FUNCTION_TYPE_UDF;
227,935✔
132
}
133

134
EFunctionType fmGetFuncTypeFromId(int32_t funcId) {
26,200,589✔
135
  if (funcId >= 0 && funcId < funcMgtBuiltinsNum) {
26,200,589✔
136
    return funcMgtBuiltins[funcId].type;
26,201,599✔
137
  }
UNCOV
138
  return FUNCTION_TYPE_UDF;
×
139
}
140

141
EFuncDataRequired fmFuncDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
43,482,618✔
142
  if (fmIsUserDefinedFunc(pFunc->funcId) || pFunc->funcId < 0 || pFunc->funcId >= funcMgtBuiltinsNum) {
43,482,618✔
143
    return FUNC_DATA_REQUIRED_DATA_LOAD;
362✔
144
  }
145
  if (NULL == funcMgtBuiltins[pFunc->funcId].dataRequiredFunc) {
43,489,800✔
146
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
147
  }
148
  return funcMgtBuiltins[pFunc->funcId].dataRequiredFunc(pFunc, pTimeWindow);
43,489,166✔
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,353,364✔
159
    ;
160
  }
161

162
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
2,147,483,647✔
163
    return FUNC_DATA_REQUIRED_DATA_LOAD;
210,587,420✔
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) {
374,618,514✔
170
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
374,618,514✔
171
    return TSDB_CODE_FAILED;
21,126✔
172
  }
173
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
374,680,034✔
174
  pFpSet->init = funcMgtBuiltins[funcId].initFunc;
374,178,682✔
175
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
374,427,539✔
176
  pFpSet->finalize = funcMgtBuiltins[funcId].finalizeFunc;
374,168,919✔
177
  pFpSet->combine = funcMgtBuiltins[funcId].combineFunc;
374,247,250✔
178
  pFpSet->processFuncByRow = funcMgtBuiltins[funcId].processFuncByRow;
374,291,659✔
179
  pFpSet->cleanup = funcMgtBuiltins[funcId].cleanupFunc;
374,586,703✔
180
  return TSDB_CODE_SUCCESS;
374,484,324✔
181
}
182

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

198
int32_t fmGetScalarFuncExecFuncs(int32_t funcId, SScalarFuncExecFuncs* pFpSet) {
1,035,067,783✔
199
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,035,067,783✔
200
    return TSDB_CODE_FAILED;
294✔
201
  }
202
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
1,036,052,147✔
203
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
1,036,177,818✔
204
  return TSDB_CODE_SUCCESS;
1,036,350,884✔
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,399,125,003✔
212

213
bool fmIsSelectColsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_COLS_FUNC); }
160,948,610✔
214

215
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
2,127,767,772✔
216

217
bool fmIsTimelineFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TIMELINE_FUNC); }
1,281,518,887✔
218

219
bool fmIsDateTimeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_DATETIME_FUNC); }
648,179,580✔
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,616,781,799✔
226

227
bool fmIsWindowClauseFunc(int32_t funcId) { return fmIsAggFunc(funcId) || fmIsWindowPseudoColumnFunc(funcId); }
67,371,827✔
228

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

231
bool fmIsStreamWindowClauseFunc(int32_t funcId) { return fmIsWindowClauseFunc(funcId) || fmIsPlaceHolderFunc(funcId); }
1,228,212✔
232

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

235
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
1,749,097,740✔
236

237
bool fmIsSpecialDataRequiredFunc(int32_t funcId) {
99,929,643✔
238
  return isSpecificClassifyFunc(funcId, FUNC_MGT_SPECIAL_DATA_REQUIRED);
99,929,643✔
239
}
240

241
bool fmIsDynamicScanOptimizedFunc(int32_t funcId) {
48,495,149✔
242
  return isSpecificClassifyFunc(funcId, FUNC_MGT_DYNAMIC_SCAN_OPTIMIZED);
48,495,149✔
243
}
244

245
bool fmIsMultiResFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_RES_FUNC); }
1,313,350,156✔
246

247
bool fmIsRepeatScanFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_REPEAT_SCAN_FUNC); }
1,294,845,861✔
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); }
648,490,408✔
252

253
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
69,879,070✔
254

255
bool fmIsSystemInfoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SYSTEM_INFO_FUNC); }
651,785,865✔
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); }
660,120,597✔
260

261
bool fmIsMultiRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_ROWS_FUNC); }
1,291,958,913✔
262

263
static bool fmIsKeepOrderFuncId(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_KEEP_ORDER_FUNC); }
777,347,761✔
264

265
bool fmIsKeepOrderFunc(SFunctionNode* pFunc) {
784,162,486✔
266
  if (pFunc->funcType == FUNCTION_TYPE_TOP || pFunc->funcType == FUNCTION_TYPE_BOTTOM ||
784,162,486✔
267
      pFunc->funcType == FUNCTION_TYPE_SAMPLE) {
779,951,157✔
268
    if (pFunc->pParameterList != NULL && pFunc->pParameterList->length >= 2) {
6,833,463✔
269
      SNode* pParam = nodesListGetNode(pFunc->pParameterList, 1);
6,844,279✔
270
      if (pParam != NULL && nodeType(pParam) == QUERY_NODE_VALUE) {
6,844,279✔
271
        SValueNode* pConst = (SValueNode*)pParam;
6,844,279✔
272
        if (pConst->datum.i == 1) {
6,844,279✔
273
          return true;
813,729✔
274
        }
275
      }
276
    }
277
    return false;
6,022,298✔
278
  }
279
  return fmIsKeepOrderFuncId(pFunc->funcId);
777,382,668✔
280
}
281

282
bool fmIsCumulativeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CUMULATIVE_FUNC); }
163,660,135✔
283

284
bool fmIsForbidSysTableFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_SYSTABLE_FUNC); }
648,384,101✔
285

286
bool fmIsInterpFunc(int32_t funcId) {
1,328,631,536✔
287
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,328,631,536✔
288
    return false;
388,500✔
289
  }
290
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
1,328,254,691✔
291
}
292

293
bool fmIsInterpPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERP_PC_FUNC); }
1,307,307,890✔
294

295
bool fmIsForecastFunc(int32_t funcId) {
1,274,991,935✔
296
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,274,991,935✔
297
    return false;
400,785✔
298
  }
299
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
1,274,599,484✔
300
}
301

302
bool fmIsAnalysisPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_ANALYTICS_PC_FUNC); }
1,310,453,956✔
303

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

309
  int32_t type = funcMgtBuiltins[funcId].type;
×
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) {
73,490,120✔
315
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
73,490,120✔
UNCOV
316
    return false;
×
317
  }
318
  return FUNCTION_TYPE_LAST_ROW == funcMgtBuiltins[funcId].type;
73,491,708✔
319
}
320

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

328
bool fmIsNotNullOutputFunc(int32_t funcId) {
548,051,978✔
329
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
548,051,978✔
330
    return false;
31,694✔
331
  }
332
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
548,053,689✔
333
         FUNCTION_TYPE_CACHE_LAST == funcMgtBuiltins[funcId].type ||
529,997,335✔
334
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
528,527,995✔
335
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
504,644,842✔
336
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
496,930,949✔
337
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
485,637,892✔
338
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
472,639,757✔
339
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
469,484,135✔
340
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
412,337,543✔
341
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
1,486,535,707✔
342
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
408,362,524✔
343
}
344

345
bool fmIsSelectValueFunc(int32_t funcId) {
371,477,416✔
346
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
371,477,416✔
347
    return false;
×
348
  }
349
  return FUNCTION_TYPE_SELECT_VALUE == funcMgtBuiltins[funcId].type;
371,511,522✔
350
}
351

352
bool fmIsGroupKeyFunc(int32_t funcId) {
284,096,493✔
353
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
284,096,493✔
354
    return false;
12,542✔
355
  }
356
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
284,083,951✔
357
}
358

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

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

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

380
bool fmIsBlockDistFunc(int32_t funcId) {
648,159,070✔
381
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
648,159,070✔
382
    return false;
180,922✔
383
  }
384
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
647,989,250✔
385
}
386

387
bool fmIsDBUsageFunc(int32_t funcId) {
648,156,782✔
388
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
648,156,782✔
389
    return false;
179,098✔
390
  }
391
  return FUNCTION_TYPE_DB_USAGE == funcMgtBuiltins[funcId].type;
647,986,578✔
392
}
393

394
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
881,733,845✔
395

396
bool fmIsVolatileFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_VOLATILE_FUNC); }
948,025,775✔
397

398
bool fmIsNoPushdownFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_NO_PUSHDOWN_FUNC); }
38,580,480✔
399

400
bool fmIsIgnoreNullFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IGNORE_NULL_FUNC); }
323,339✔
401

402
void fmFuncMgtDestroy() {
1,626,192✔
403
  void* m = gFunMgtService.pFuncNameHashTable;
1,626,192✔
404
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
1,626,192✔
405
    taosHashCleanup(m);
1,626,123✔
406
  }
407
}
1,626,192✔
408

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

419
// function has same input/output type
420
bool fmIsSameInOutType(int32_t funcId) {
49,531,780✔
421
  bool res = false;
49,531,780✔
422
  switch (funcMgtBuiltins[funcId].type) {
49,531,780✔
423
    case FUNCTION_TYPE_MAX:
17,794,401✔
424
    case FUNCTION_TYPE_MIN:
425
    case FUNCTION_TYPE_TOP:
426
    case FUNCTION_TYPE_BOTTOM:
427
    case FUNCTION_TYPE_FIRST:
428
    case FUNCTION_TYPE_LAST:
429
    case FUNCTION_TYPE_SAMPLE:
430
    case FUNCTION_TYPE_TAIL:
431
    case FUNCTION_TYPE_UNIQUE:
432
      res = true;
17,794,401✔
433
      break;
17,794,401✔
434
    default:
31,737,379✔
435
      break;
31,737,379✔
436
  }
437
  return res;
49,531,780✔
438
}
439

440
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
16,263,265✔
441
  SNode* pNode;
442
  FOREACH(pNode, pFunc->pParameterList) {
16,357,489✔
443
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
339,474✔
444
      return false;
245,250✔
445
    }
446
  }
447
  return true;
16,018,015✔
448
}
449

450
bool fmIsSkipScanCheckFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SKIP_SCAN_CHECK_FUNC); }
48,488,122✔
451

452
bool fmIsPrimaryKeyFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PRIMARY_KEY_FUNC); }
571,018,326✔
453

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

456
bool fmIsPlaceHolderFuncForExternalWin(int32_t funcId) {
746,832✔
457
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
746,832✔
458
    return false;
×
459
  }
460
  return (funcMgtBuiltins[funcId].type == FUNCTION_TYPE_WSTART || funcMgtBuiltins[funcId].type == FUNCTION_TYPE_WEND ||
1,253,912✔
461
          funcMgtBuiltins[funcId].type == FUNCTION_TYPE_WDURATION);
507,080✔
462
}
463

464
void getLastCacheDataType(SDataType* pType, int32_t pkBytes) {
×
465
  // TODO: do it later.
466
  pType->bytes = getFirstLastInfoSize(pType->bytes, pkBytes) + VARSTR_HEADER_SIZE;
×
467
  pType->type = TSDB_DATA_TYPE_BINARY;
×
468
}
×
469

470
static int32_t getFuncInfo(SFunctionNode* pFunc) {
158,531,831✔
471
  char msg[128] = {0};
158,531,831✔
472
  return fmGetFuncInfo(pFunc, msg, sizeof(msg));
158,531,890✔
473
}
474

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

492
static void resetOutputChangedFunc(SFunctionNode *pFunc, const SFunctionNode* pSrcFunc) {
148,025,453✔
493
  if (funcMgtBuiltins[pFunc->funcId].type == FUNCTION_TYPE_LAST_MERGE) {
148,025,453✔
494
    pFunc->node.resType = pSrcFunc->node.resType;
21,106,086✔
495
    return;
21,105,968✔
496
  }
497
}
498

499
int32_t createFunctionWithSrcFunc(const char* pName, const SFunctionNode* pSrcFunc, SNodeList* pParameterList, SFunctionNode** ppFunc) {
148,020,617✔
500
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
148,020,617✔
501
  if (NULL == *ppFunc) {
148,030,434✔
502
    return code;
×
503
  }
504

505
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
148,030,434✔
506
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
148,030,434✔
507
  (*ppFunc)->pSrcFuncRef = pSrcFunc;
148,030,434✔
508

509
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
148,030,253✔
510
  (*ppFunc)->pParameterList = pParameterList;
148,030,131✔
511
  code = getFuncInfo((*ppFunc));
148,030,253✔
512
  if (TSDB_CODE_SUCCESS != code) {
148,026,761✔
513
    (*ppFunc)->pParameterList = NULL;
×
514
    nodesDestroyNode((SNode*)*ppFunc);
×
515
    *ppFunc = NULL;
×
516
    return code;
×
517
  }
518
  resetOutputChangedFunc(*ppFunc, pSrcFunc);
148,026,761✔
519
  (*ppFunc)->node.relatedTo = pSrcFunc->node.relatedTo;
148,028,416✔
520
  (*ppFunc)->node.bindExprID = pSrcFunc->node.bindExprID;
148,028,656✔
521
  return code;
148,028,711✔
522
}
523

524
static int32_t createColumnByFunc(const SFunctionNode* pFunc, SColumnNode** ppCol) {
98,479,923✔
525
  int32_t code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)ppCol);
98,479,923✔
526
  if (NULL == *ppCol) {
98,487,548✔
527
    return code;
×
528
  }
529
  tstrncpy((*ppCol)->colName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
98,487,548✔
530
  (*ppCol)->node.resType = pFunc->node.resType;
98,487,489✔
531
  return TSDB_CODE_SUCCESS;
98,487,304✔
532
}
533

534
bool fmIsDistExecFunc(int32_t funcId) {
384,239,104✔
535
  if (fmIsUserDefinedFunc(funcId)) {
384,239,104✔
536
    return false;
121,140✔
537
  }
538
  if (!fmIsVectorFunc(funcId)) {
384,163,187✔
539
    return true;
554,923✔
540
  }
541
  return (NULL != funcMgtBuiltins[funcId].pPartialFunc && NULL != funcMgtBuiltins[funcId].pMergeFunc);
383,622,140✔
542
}
543

544
static int32_t createPartialFunction(const SFunctionNode* pSrcFunc, SFunctionNode** pPartialFunc) {
49,535,021✔
545
  SNodeList* pParameterList = NULL;
49,535,021✔
546
  int32_t    code = nodesCloneList(pSrcFunc->pParameterList, &pParameterList);
49,535,021✔
547
  if (NULL == pParameterList) {
49,539,288✔
548
    return code;
×
549
  }
550
  code =
551
      createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pPartialFunc, pSrcFunc, pParameterList, pPartialFunc);
49,539,288✔
552
  if (TSDB_CODE_SUCCESS != code) {
49,537,917✔
553
    nodesDestroyList(pParameterList);
3,728✔
554
    return code;
×
555
  }
556
  (*pPartialFunc)->hasOriginalFunc = true;
49,534,189✔
557
  (*pPartialFunc)->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
49,534,252✔
558
  char name[TSDB_FUNC_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_POINTER_PRINT_BYTES + 1] = {0};
49,534,307✔
559

560
  int32_t len = snprintf(name, sizeof(name), "%s.%p", (*pPartialFunc)->functionName, pSrcFunc);
49,534,181✔
561
  if (taosHashBinary(name, len, sizeof(name)) < 0) {
49,538,279✔
562
    return TSDB_CODE_FAILED;
×
563
  }
564
  tstrncpy((*pPartialFunc)->node.aliasName, name, TSDB_COL_NAME_LEN);
49,538,279✔
565
  return TSDB_CODE_SUCCESS;
49,538,397✔
566
}
567

568
static int32_t createMergeFuncPara(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
98,478,978✔
569
                                   SNodeList** pParameterList) {
570
  SNode*  pRes = NULL;
98,478,978✔
571
  int32_t code = createColumnByFunc(pPartialFunc, (SColumnNode**)&pRes);
98,478,978✔
572
  if (TSDB_CODE_SUCCESS != code) {
98,486,942✔
573
    return code;
×
574
  }
575
  if (NULL != funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc) {
98,486,942✔
576
    return funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc(pSrcFunc->pParameterList, pRes, pParameterList);
833,288✔
577
  } else {
578
    return nodesListMakeStrictAppend(pParameterList, pRes);
97,653,532✔
579
  }
580
}
581

582
static int32_t createMidFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
48,941,735✔
583
                                 SFunctionNode** pMidFunc) {
584
  SNodeList*     pParameterList = NULL;
48,941,735✔
585
  SFunctionNode* pFunc = NULL;
48,941,550✔
586

587
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
48,941,735✔
588
  if (TSDB_CODE_SUCCESS == code) {
48,950,967✔
589
    if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){
48,951,311✔
590
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pSrcFunc, pParameterList, &pFunc);
6,121,041✔
591
    }else{
592
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
42,830,085✔
593
    }
594
  }
595
  if (TSDB_CODE_SUCCESS == code) {
48,951,636✔
596
    tstrncpy(pFunc->node.aliasName, pPartialFunc->node.aliasName, TSDB_COL_NAME_LEN);
48,952,324✔
597
  }
598

599
  if (TSDB_CODE_SUCCESS == code) {
48,951,695✔
600
    *pMidFunc = pFunc;
48,951,695✔
601
  } else {
602
    nodesDestroyList(pParameterList);
×
603
  }
604
  return code;
48,951,179✔
605
}
606

607
static int32_t createMergeFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
49,538,551✔
608
                                   SFunctionNode** pMergeFunc) {
609
  SNodeList*     pParameterList = NULL;
49,538,551✔
610
  SFunctionNode* pFunc = NULL;
49,538,551✔
611

612
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
49,538,614✔
613
  if (TSDB_CODE_SUCCESS == code) {
49,538,949✔
614
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
49,539,035✔
615
  }
616
  if (TSDB_CODE_SUCCESS == code) {
49,546,466✔
617
    pFunc->hasOriginalFunc = true;
49,540,424✔
618
    pFunc->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
49,540,424✔
619
    // overwrite function restype set by translate function
620
    if (fmIsSameInOutType(pSrcFunc->funcId)) {
49,540,365✔
621
      pFunc->node.resType = pSrcFunc->node.resType;
17,794,745✔
622
    }
623
    tstrncpy(pFunc->node.aliasName, pSrcFunc->node.aliasName, TSDB_COL_NAME_LEN);
49,532,477✔
624
  }
625

626
  if (TSDB_CODE_SUCCESS == code) {
49,538,338✔
627
    *pMergeFunc = pFunc;
49,538,338✔
628
  } else {
629
    nodesDestroyList(pParameterList);
×
630
  }
631
  return code;
49,539,066✔
632
}
633

634
int32_t fmGetDistMethod(const SFunctionNode* pFunc, SFunctionNode** pPartialFunc, SFunctionNode** pMidFunc,
49,529,309✔
635
                        SFunctionNode** pMergeFunc) {
636
  if (!fmIsDistExecFunc(pFunc->funcId)) {
49,529,309✔
637
    return TSDB_CODE_FAILED;
×
638
  }
639

640
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
49,540,058✔
641
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
49,534,365✔
642
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
48,944,681✔
643
  }
644
  if (TSDB_CODE_SUCCESS == code) {
49,540,379✔
645
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
49,539,338✔
646
  }
647

648
  if (TSDB_CODE_SUCCESS != code) {
49,540,796✔
649
    nodesDestroyNode((SNode*)*pPartialFunc);
×
650
    if (pMidFunc) nodesDestroyNode((SNode*)*pMidFunc);
×
651
    nodesDestroyNode((SNode*)*pMergeFunc);
×
652
  }
653

654
  return code;
49,538,809✔
655
}
656

657
const char* fmGetFuncName(int32_t funcId) {
366,533✔
658
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
366,533✔
659
    return "invalid function";
×
660
  }
661
  return funcMgtBuiltins[funcId].name;
366,533✔
662
}
663

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

682
bool fmIsTSMASupportedFunc(func_id_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC); }
1,548,108✔
683

684
bool fmIsRsmaSupportedFunc(func_id_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_RSMA_FUNC); }
147,074✔
685

686
int32_t fmCreateStateFuncs(SNodeList* pFuncs) {
3,954✔
687
  int32_t code;
688
  SNode*  pNode;
689
  char    buf[128] = {0};
3,954✔
690
  FOREACH(pNode, pFuncs) {
12,946✔
691
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
8,992✔
692
    code = fmGetFuncInfo(pFunc, buf, 128);
8,992✔
693
    if (code) break;
8,992✔
694
    if (fmIsTSMASupportedFunc(pFunc->funcId)) {
8,992✔
695
      SFunctionNode* pNewFunc = NULL;
8,992✔
696
      code = fmCreateStateFunc(pFunc, &pNewFunc);
8,992✔
697
      if (code) {
8,992✔
698
        // error
699
        break;
×
700
      } else if (!pNewFunc) {
8,992✔
701
        // no need state func
702
        continue;
×
703
      } else {
704
        REPLACE_NODE(pNewFunc);
8,992✔
705
        nodesDestroyNode(pNode);
8,992✔
706
      }
707
    }
708
  }
709
  return code;
3,954✔
710
}
711

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

728
int32_t fmCreateStateMergeFuncs(SNodeList* pFuncs) {
900✔
729
  int32_t code;
730
  SNode*  pNode;
731
  char    buf[128] = {0};
900✔
732
  FOREACH(pNode, pFuncs) {
2,880✔
733
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
1,980✔
734
    if (fmIsTSMASupportedFunc(pFunc->funcId)) {
1,980✔
735
      SFunctionNode* pNewFunc = NULL;
1,980✔
736
      code = fmCreateStateMergeFunc(pFunc, &pNewFunc);
1,980✔
737
      if (code) {
1,980✔
738
        // error
739
        break;
×
740
      } else if (!pNewFunc) {
1,980✔
741
        // no state merge func
742
        continue;
360✔
743
      } else {
744
        REPLACE_NODE(pNewFunc);
1,620✔
745
        nodesDestroyNode(pNode);
1,620✔
746
      }
747
    }
748
  }
749
  return code;
900✔
750
}
751

752
int32_t fmGetFuncId(const char* name) {
8,696,200✔
753
  if (NULL != gFunMgtService.pFuncNameHashTable) {
8,696,200✔
754
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, name, strlen(name));
8,696,200✔
755
    if (NULL != pVal) {
8,696,200✔
756
      return *(int32_t*)pVal;
8,696,200✔
757
    }
758
    return -1;
×
759
  }
760
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
×
761
    if (0 == strcmp(funcMgtBuiltins[i].name, name)) {
×
762
      return i;
×
763
    }
764
  }
765
  return -1;
×
766
}
767

768
int32_t fmGetTwstartFuncId() {
1,831,048✔
769
  static int32_t twstartFuncId = -2;
770
  if (twstartFuncId < 0) {
1,831,048✔
771
    twstartFuncId = fmGetFuncId("_twstart");
1,626,123✔
772
  }
773
  return twstartFuncId;
1,831,048✔
774
}
775

776
int32_t fmGetTwendFuncId() {
1,662,320✔
777
  static int32_t twendFuncId = -2;
778
  if (twendFuncId < 0) {
1,662,320✔
779
    twendFuncId = fmGetFuncId("_twend");
1,626,123✔
780
  }
781
  return twendFuncId;
1,662,320✔
782
}
783

784
int32_t fmGetTwdurationFuncId() {
1,626,579✔
785
  static int32_t twdurationFuncId = -2;
786
  if (twdurationFuncId < 0) {
1,626,579✔
787
    twdurationFuncId = fmGetFuncId("_twduration");
1,626,123✔
788
  }
789
  return twdurationFuncId;
1,626,579✔
790
}
791

792
int32_t fmGetExternalWindowColumnFuncId() {
1,674,814✔
793
  static int32_t externalWindowColumnFuncId = -2;
794
  if (externalWindowColumnFuncId < 0) {
1,674,814✔
795
    externalWindowColumnFuncId = fmGetFuncId("_external_window_column");
1,626,123✔
796
  }
797
  return externalWindowColumnFuncId;
1,674,814✔
798
}
799

800
bool fmIsMyStateFunc(int32_t funcId, int32_t stateFuncId) {
2,544,480✔
801
  const SBuiltinFuncDefinition* pFunc = &funcMgtBuiltins[funcId];
2,544,480✔
802
  const SBuiltinFuncDefinition* pStateFunc = &funcMgtBuiltins[stateFuncId];
2,544,480✔
803
  if (!pFunc->pStateFunc) {
2,544,480✔
804
    return false;
×
805
  }
806
  if (strcmp(pFunc->pStateFunc, pStateFunc->name) == 0) return true;
2,544,480✔
807
  int32_t stateMergeFuncId = fmGetFuncId(pFunc->pStateFunc);
927,900✔
808
  if (stateMergeFuncId == -1) {
927,900✔
809
    return false;
×
810
  }
811
  const SBuiltinFuncDefinition* pStateMergeFunc = &funcMgtBuiltins[stateMergeFuncId];
927,900✔
812
  return strcmp(pStateFunc->name, pStateMergeFunc->pMergeFunc) == 0;
927,900✔
813
}
814

815
bool fmIsCountLikeFunc(int32_t funcId) {
654,188,592✔
816
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
654,188,592✔
817
}
818

819
bool fmIsRowTsOriginFunc(int32_t funcId) {
5,884,519✔
820
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
5,884,519✔
821
    return false;
×
822
  }
823
  return FUNCTION_TYPE_IROWTS_ORIGIN == funcMgtBuiltins[funcId].type;
5,884,992✔
824
}
825

826
bool fmIsGroupIdFunc(int32_t funcId) {
2,555,156✔
827
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,555,156✔
828
    return false;
×
829
  }
830
  return FUNCTION_TYPE_GROUP_ID == funcMgtBuiltins[funcId].type;
2,555,156✔
831
}
832

833
const void* fmGetExternalWindowColumnFuncVal(const SStreamRuntimeFuncInfo* pStreamRuntimeFuncInfo, int32_t index) {
11,567,044✔
834
  SSTriggerCalcParam *pParams = taosArrayGet(pStreamRuntimeFuncInfo->pStreamPesudoFuncVals, pStreamRuntimeFuncInfo->curIdx);
11,567,044✔
835
  if (pParams == NULL || pParams->pExternalWindowData == NULL || index < 0) {
11,567,044✔
836
    return NULL;
×
837
  }
838
  return taosArrayGet(pParams->pExternalWindowData, index);
11,566,590✔
839
}
840

841
const void* fmGetStreamPesudoFuncVal(int32_t funcId, const SStreamRuntimeFuncInfo* pStreamRuntimeFuncInfo) {
440,547,446✔
842
  switch (funcMgtBuiltins[funcId].type) {
440,547,446✔
843
    case FUNCTION_TYPE_TGRPID:
156,398✔
844
      return &pStreamRuntimeFuncInfo->groupId;
156,398✔
845
    case FUNCTION_TYPE_PLACEHOLDER_COLUMN:
1,039,600✔
846
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
1,039,600✔
847
    case FUNCTION_TYPE_PLACEHOLDER_TBNAME:
2,303,852✔
848
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
2,303,852✔
849
    default:
437,120,863✔
850
      break;
437,120,863✔
851
  }
852

853
  SSTriggerCalcParam *pParams = taosArrayGet(pStreamRuntimeFuncInfo->pStreamPesudoFuncVals, pStreamRuntimeFuncInfo->curIdx);
437,120,863✔
854
  switch (funcMgtBuiltins[funcId].type) {
437,127,772✔
855
    case FUNCTION_TYPE_TPREV_TS:
×
856
      return &pParams->prevTs;
×
857
    case FUNCTION_TYPE_TCURRENT_TS:
2,976,267✔
858
      return &pParams->currentTs;
2,976,267✔
859
    case FUNCTION_TYPE_TNEXT_TS:
×
860
      return &pParams->nextTs;
×
861
    case FUNCTION_TYPE_TWSTART:
376,960,523✔
862
      return &pParams->wstart;
376,960,523✔
863
    case FUNCTION_TYPE_TWEND:
19,654,082✔
864
      return &pParams->wend;
19,654,082✔
865
    case FUNCTION_TYPE_TWDURATION:
1,301,337✔
866
      return &pParams->wduration;
1,301,337✔
867
    case FUNCTION_TYPE_TWROWNUM:
34,908,797✔
868
      return &pParams->wrownum;
34,908,797✔
869
    case FUNCTION_TYPE_TPREV_LOCALTIME:
250,771✔
870
      return &pParams->prevLocalTime;
250,771✔
871
    case FUNCTION_TYPE_TLOCALTIME:
681,436✔
872
      return &pParams->triggerTime;
681,436✔
873
    case FUNCTION_TYPE_TNEXT_LOCALTIME:
388,546✔
874
      return &pParams->nextLocalTime;
388,546✔
875
    case FUNCTION_TYPE_TGRPID:
×
876
      return &pStreamRuntimeFuncInfo->groupId;
×
877
    case FUNCTION_TYPE_PLACEHOLDER_COLUMN:
×
878
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
×
879
    case FUNCTION_TYPE_PLACEHOLDER_TBNAME:
×
880
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
×
881
    case FUNCTION_TYPE_TIDLESTART:
6,804✔
882
      return &pParams->idlestart;
6,804✔
883
    case FUNCTION_TYPE_TIDLEEND:
6,804✔
884
      return &pParams->idleend;
6,804✔
885
    default:
×
886
      break;
×
887
  }
888
  return NULL;
×
889
}
890

891
bool fmIsStreamPesudoColVal(int32_t funcId) {
624,040✔
892
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
624,040✔
893
    return false;
8,910✔
894
  }
895
  return funcMgtBuiltins[funcId].type == FUNCTION_TYPE_PLACEHOLDER_COLUMN
615,130✔
896
         || funcMgtBuiltins[funcId].type == FUNCTION_TYPE_PLACEHOLDER_TBNAME;
615,130✔
897
}
898

899
int32_t fmGetStreamPesudoFuncEnv(int32_t funcId, SNodeList* pParamNodes, SFuncExecEnv *pEnv) {
9,501,130✔
900
  if (NULL == pParamNodes || pParamNodes->length < 1) {
9,501,130✔
901
    uError("invalid stream pesudo func param list %p", pParamNodes);
381✔
902
    return TSDB_CODE_INTERNAL_ERROR;
×
903
  }
904

905
  int32_t firstParamType = nodeType(nodesListGetNode(pParamNodes, 0));
9,501,063✔
906
  if (QUERY_NODE_VALUE != firstParamType) {
9,501,889✔
907
    uError("invalid stream pesudo func first param type %d", firstParamType);
×
908
    return TSDB_CODE_INTERNAL_ERROR;
×
909
  }
910

911
  SValueNode* pResDesc = (SValueNode*)nodesListGetNode(pParamNodes, 0);
9,501,889✔
912
  pEnv->calcMemSize = pResDesc->node.resType.bytes;
9,502,205✔
913

914
  return TSDB_CODE_SUCCESS;
9,502,205✔
915
}
916

917

918
int32_t fmSetStreamPseudoFuncParamVal(int32_t funcId, SNodeList* pParamNodes, const SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
452,120,143✔
919
  if (!pStreamRuntimeInfo) {
452,120,143✔
920
    uError("internal error, should have pVals for stream pseudo funcs");
×
921
    return TSDB_CODE_INTERNAL_ERROR;
×
922
  }
923
  int32_t code = 0;
452,120,143✔
924
  SArray *pVals1 = NULL;
452,120,143✔
925
  SNode* pFirstParam = nodesListGetNode(pParamNodes, 0);
452,120,143✔
926
  if (nodeType(pFirstParam) != QUERY_NODE_VALUE) {
452,174,175✔
927
    uError("invalid param node type: %d for func: %d", nodeType(pFirstParam), funcId);
×
928
    return TSDB_CODE_INTERNAL_ERROR;
×
929
  }
930

931
  int32_t t = funcMgtBuiltins[funcId].type;
452,165,462✔
932
  uDebug("set stream pseudo func param val, functype: %d, pStreamRuntimeInfo: %p", t, pStreamRuntimeInfo);
452,178,373✔
933
  if (FUNCTION_TYPE_TGRPID == t) {
452,172,044✔
934
    SValue v = {0};
156,136✔
935
    v.type = TSDB_DATA_TYPE_BIGINT;
156,136✔
936
    v.val = *(int64_t*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
156,136✔
937
    
938
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&v, pFirstParam->type));
156,398✔
939
    if (code != 0) {
156,398✔
940
      uError("failed to set value node value: %s", tstrerror(code));
×
941
      return code;
×
942
    }
943
  } else if (FUNCTION_TYPE_PLACEHOLDER_COLUMN == t) {
452,015,908✔
944
    SNode* pSecondParam = nodesListGetNode(pParamNodes, 1);
1,039,600✔
945
    if (nodeType(pSecondParam) != QUERY_NODE_VALUE) {
1,039,600✔
946
      uError("invalid param node type: %d for func: %d", nodeType(pSecondParam), funcId);
×
947
      return TSDB_CODE_INTERNAL_ERROR;
×
948
    }
949
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
1,039,600✔
950
    int32_t idx = ((SValueNode*)pSecondParam)->datum.i;
1,039,600✔
951
    if (idx - 1 < 0 || idx - 1 >= taosArrayGetSize(pVal)) {
1,039,377✔
UNCOV
952
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
×
953
      return TSDB_CODE_INTERNAL_ERROR;
×
954
    }
955
    SStreamGroupValue* pValue = taosArrayGet(pVal, idx - 1);
1,039,377✔
956
    if (pValue == NULL) {
1,039,377✔
957
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
×
958
      return TSDB_CODE_INTERNAL_ERROR;
×
959
    }
960
    if (!pValue->isNull){
1,039,377✔
961
      if (pValue->data.type != ((SValueNode*)pFirstParam)->node.resType.type){
1,031,712✔
962
        uError("invalid value type: %d for func: %d, should be: %d", pValue->data.type, funcId, ((SValueNode*)pFirstParam)->node.resType.type);
×
963
        return TSDB_CODE_INTERNAL_ERROR;
×
964
      }
965
      if (IS_VAR_DATA_TYPE(((SValueNode*)pFirstParam)->node.resType.type)) {
1,031,489✔
966
        char* tmp = taosMemoryCalloc(1, pValue->data.nData + VARSTR_HEADER_SIZE); 
703,012✔
967
        if (tmp == NULL) {
703,270✔
968
          return TSDB_CODE_OUT_OF_MEMORY;
×
969
        }
970
        taosMemoryFree(((SValueNode*)pFirstParam)->datum.p);
703,270✔
971
        ((SValueNode*)pFirstParam)->datum.p = tmp;
703,270✔
972
        memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
703,270✔
973
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
703,270✔
974
      } else {
975
        code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&pValue->data, pValue->data.type));
328,700✔
976
      }
977
    }
978
    ((SValueNode*)pFirstParam)->isNull = pValue->isNull;
1,039,858✔
979
  } else if (FUNCTION_TYPE_PLACEHOLDER_TBNAME == t) {
450,976,308✔
980
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
2,268,006✔
981
    for (int32_t i = 0; i < taosArrayGetSize(pVal); ++i) {
2,781,557✔
982
      SStreamGroupValue* pValue = taosArrayGet(pVal, i);
2,781,873✔
983
      if (pValue != NULL && pValue->isTbname) {
2,782,187✔
984
        taosMemoryFreeClear(((SValueNode*)pFirstParam)->datum.p);
2,269,854✔
985
        ((SValueNode*)pFirstParam)->datum.p = taosMemoryCalloc(pValue->data.nData + VARSTR_HEADER_SIZE, 1);
2,269,863✔
986
        if (NULL == ((SValueNode*)pFirstParam)->datum.p ) {
2,269,537✔
987
          return terrno;
×
988
        }
989

990
        (void)memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
2,268,616✔
991
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
2,268,616✔
992
        break;
2,269,230✔
993
      }
994
    }
995
  } else if(FUNCTION_TYPE_EXTERNAL_WINDOW_COLUMN == t) {
448,708,302✔
996
    if (NULL == pParamNodes || LIST_LENGTH(pParamNodes) < 2) {
11,564,320✔
UNCOV
997
      uError("invalid stream external window column param list %p, len: %d", pParamNodes,
×
998
             pParamNodes ? LIST_LENGTH(pParamNodes) : 0);
999
      return TSDB_CODE_INTERNAL_ERROR;
×
1000
    }
1001

1002
    SNode* pParamNode = nodesListGetNode(pParamNodes, 1);
11,564,320✔
1003
    if (NULL == pParamNode || QUERY_NODE_VALUE != nodeType(pParamNode)) {
11,567,498✔
1004
      uError("invalid stream external window column param node %p type %d for func: %d", pParamNode,
908✔
1005
             pParamNode ? nodeType(pParamNode) : -1, funcId);
1006
      return TSDB_CODE_INTERNAL_ERROR;
×
1007
    }
1008

1009
    const SStreamGroupValue* pVal =
1010
        fmGetExternalWindowColumnFuncVal(pStreamRuntimeInfo, ((SValueNode*)pParamNode)->placeholderNo);
11,566,590✔
1011
    if (!pVal) {
11,565,682✔
1012
      uError("failed to set stream pseudo func param val, NULL val for funcId: %d", funcId);
×
1013
      return TSDB_CODE_INTERNAL_ERROR;
×
1014
    }
1015
    if (pVal->isNull) {
11,565,682✔
1016
      ((SValueNode*)pFirstParam)->isNull = true;
908✔
1017
    } else {
1018
      ((SValueNode*)pFirstParam)->isNull = false;
11,564,774✔
1019
      if (IS_VAR_DATA_TYPE(pVal->data.type)) {
11,563,866✔
1020
        // pVal->data.pData already includes the VARSTR header from colDataGetData,
1021
        // so copy it directly as datum.p expects VARSTR format [header][raw data].
1022
        char* tmp = taosMemoryMalloc(pVal->data.nData);
9,990✔
1023
        if (tmp == NULL) {
9,990✔
1024
          return TSDB_CODE_OUT_OF_MEMORY;
×
1025
        }
1026
        taosMemoryFree(((SValueNode*)pFirstParam)->datum.p);
9,990✔
1027
        ((SValueNode*)pFirstParam)->datum.p = tmp;
9,990✔
1028
        memcpy(tmp, pVal->data.pData, pVal->data.nData);
9,990✔
1029
      } else {
1030
        code = nodesSetValueNodeValue((SValueNode*)pFirstParam, (void*)&pVal->data.val);
11,554,330✔
1031
      }
1032
    }
1033
    if (code != 0) {
11,564,774✔
1034
      uError("failed to set value node value: %s", tstrerror(code));
×
1035
      return code;
×
1036
    }
1037
  } else if (LIST_LENGTH(pParamNodes) == 1) {
874,286,108✔
1038
    // twstart, twend
1039
    const void* pVal = fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
437,136,629✔
1040
    if (!pVal) {
437,121,694✔
1041
      uError("failed to set stream pseudo func param val, NULL val for funcId: %d", funcId);
×
1042
      return TSDB_CODE_INTERNAL_ERROR;
×
1043
    }
1044
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, (void*)pVal);
437,121,694✔
1045
    if (code != 0) {
437,154,256✔
1046
      uError("failed to set value node value: %s", tstrerror(code));
12,130✔
1047
      return code;
×
1048
    }
1049
  } else {
1050
    uError("invalid placeholder function type: %d", t);
52,128✔
1051
    return TSDB_CODE_INTERNAL_ERROR;
×
1052
  }
1053
  
1054
  return code;
452,146,495✔
1055
}
1056

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