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

taosdata / TDengine / #3572

02 Jan 2025 08:57AM UTC coverage: 63.077% (-0.2%) from 63.276%
#3572

push

travis-ci

web-flow
Merge pull request #29450 from taosdata/fix/TS-5651-skip-sync-heartbeat

fix:[TS-5651]skip-sync-heartbeat

139525 of 284348 branches covered (49.07%)

Branch coverage included in aggregate %.

217427 of 281548 relevant lines covered (77.23%)

18571459.85 hits per line

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

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

16
#include "functionMgt.h"
17

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

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

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

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

42
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
467,285✔
43
    if (TSDB_CODE_SUCCESS != taosHashPut(gFunMgtService.pFuncNameHashTable, funcMgtBuiltins[i].name,
464,520!
44
                                         strlen(funcMgtBuiltins[i].name), &i, sizeof(int32_t))) {
464,520✔
45
      initFunctionCode = terrno;
×
46
      return;
×
47
    }
48
  }
49
}
50

51
static bool isSpecificClassifyFunc(int32_t funcId, uint64_t classification) {
292,807,002✔
52
  if (fmIsUserDefinedFunc(funcId)) {
292,807,002✔
53
    return FUNC_MGT_AGG_FUNC == classification
54
               ? FUNC_AGGREGATE_UDF_ID == funcId
55
               : (FUNC_MGT_SCALAR_FUNC == classification ? FUNC_SCALAR_UDF_ID == funcId : false);
57,304✔
56
  }
57
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
292,781,915!
58
    return false;
70,473,022✔
59
  }
60
  return FUNC_MGT_TEST_MASK(funcMgtBuiltins[funcId].classification, classification);
222,308,893✔
61
}
62

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

68
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
4,326,628✔
69
  if (NULL != gFunMgtService.pFuncNameHashTable) {
4,326,628✔
70
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
4,321,750✔
71
    if (NULL != pVal) {
4,321,777✔
72
      pFunc->funcId = *(int32_t*)pVal;
4,321,557✔
73
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
4,321,557✔
74
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
4,321,557✔
75
    }
76
    return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
220✔
77
  }
78
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
98,960!
79
    if (0 == strcmp(funcMgtBuiltins[i].name, pFunc->functionName)) {
98,974✔
80
      pFunc->funcId = i;
4,892✔
81
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
4,892✔
82
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
4,892✔
83
    }
84
  }
85
  return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
×
86
}
87

88
EFuncReturnRows fmGetFuncReturnRows(SFunctionNode* pFunc) {
84,977✔
89
  if (NULL != funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc) {
84,977✔
90
    return funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc(pFunc);
48,931✔
91
  }
92
  return (fmIsIndefiniteRowsFunc(pFunc->funcId) || fmIsMultiRowsFunc(pFunc->funcId)) ? FUNC_RETURN_ROWS_INDEFINITE
36,046!
93
                                                                                     : FUNC_RETURN_ROWS_NORMAL;
36,048!
94
}
95

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

100
EFunctionType fmGetFuncType(const char* pFunc) {
2,890,576✔
101
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
2,890,576✔
102
  if (NULL != pVal) {
2,890,602✔
103
    return funcMgtBuiltins[*(int32_t*)pVal].type;
2,890,381✔
104
  }
105
  return FUNCTION_TYPE_UDF;
221✔
106
}
107

108
EFuncDataRequired fmFuncDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
351,959✔
109
  if (fmIsUserDefinedFunc(pFunc->funcId) || pFunc->funcId < 0 || pFunc->funcId >= funcMgtBuiltinsNum) {
351,959!
110
    return FUNC_DATA_REQUIRED_DATA_LOAD;
5✔
111
  }
112
  if (NULL == funcMgtBuiltins[pFunc->funcId].dataRequiredFunc) {
351,982!
113
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
114
  }
115
  return funcMgtBuiltins[pFunc->funcId].dataRequiredFunc(pFunc, pTimeWindow);
351,982✔
116
}
117

118
EFuncDataRequired fmFuncDynDataRequired(int32_t funcId, void* pRes, SDataBlockInfo* pBlockInfo) {
1,533,842✔
119
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,533,842!
120
    return FUNC_DATA_REQUIRED_DATA_LOAD;
7✔
121
  }
122

123
  const char* name = funcMgtBuiltins[funcId].name;
1,533,841✔
124
  if ((strcmp(name, "_group_key") == 0) || (strcmp(name, "_select_value") == 0)) {
1,533,841✔
125
    return FUNC_DATA_REQUIRED_NOT_LOAD;
141,813✔
126
    ;
127
  }
128

129
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
1,392,028✔
130
    return FUNC_DATA_REQUIRED_DATA_LOAD;
1,377,869✔
131
  } else {
132
    return funcMgtBuiltins[funcId].dynDataRequiredFunc(pRes, pBlockInfo);
14,159✔
133
  }
134
}
135

136
int32_t fmGetFuncExecFuncs(int32_t funcId, SFuncExecFuncs* pFpSet) {
6,784,389✔
137
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
6,784,389!
138
    return TSDB_CODE_FAILED;
641✔
139
  }
140
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
6,788,289✔
141
  pFpSet->init = funcMgtBuiltins[funcId].initFunc;
6,788,289✔
142
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
6,788,289✔
143
  pFpSet->finalize = funcMgtBuiltins[funcId].finalizeFunc;
6,788,289✔
144
  pFpSet->combine = funcMgtBuiltins[funcId].combineFunc;
6,788,289✔
145
  pFpSet->processFuncByRow = funcMgtBuiltins[funcId].processFuncByRow;
6,788,289✔
146
  pFpSet->cleanup = funcMgtBuiltins[funcId].cleanupFunc;
6,788,289✔
147
  return TSDB_CODE_SUCCESS;
6,788,289✔
148
}
149

150
int32_t fmGetUdafExecFuncs(int32_t funcId, SFuncExecFuncs* pFpSet) {
32✔
151
  if (!fmIsUserDefinedFunc(funcId)) {
32!
152
    return TSDB_CODE_FAILED;
×
153
  }
154
  pFpSet->getEnv = udfAggGetEnv;
32✔
155
  pFpSet->init = udfAggInit;
32✔
156
  pFpSet->process = udfAggProcess;
32✔
157
  pFpSet->finalize = udfAggFinalize;
32✔
158
  return TSDB_CODE_SUCCESS;
32✔
159
}
160

161
int32_t fmGetScalarFuncExecFuncs(int32_t funcId, SScalarFuncExecFuncs* pFpSet) {
11,392,142✔
162
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
11,392,142!
163
    return TSDB_CODE_FAILED;
×
164
  }
165
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
11,394,486✔
166
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
11,394,486✔
167
  return TSDB_CODE_SUCCESS;
11,394,486✔
168
}
169

170
bool fmIsAggFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_AGG_FUNC); }
19,835,320✔
171

172
bool fmIsScalarFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCALAR_FUNC); }
12,247,049✔
173

174
bool fmIsVectorFunc(int32_t funcId) { return !fmIsScalarFunc(funcId) && !fmIsPseudoColumnFunc(funcId); }
5,936,669✔
175

176
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
29,307,958✔
177

178
bool fmIsTimelineFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TIMELINE_FUNC); }
5,802,633✔
179

180
bool fmIsDateTimeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_DATETIME_FUNC); }
3,004,621✔
181

182
bool fmIsPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PSEUDO_COLUMN_FUNC); }
30,359,776✔
183

184
bool fmIsScanPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCAN_PC_FUNC); }
21,296,410✔
185

186
bool fmIsWindowPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_WINDOW_PC_FUNC); }
14,632,402✔
187

188
bool fmIsWindowClauseFunc(int32_t funcId) { return fmIsAggFunc(funcId) || fmIsWindowPseudoColumnFunc(funcId); }
365,098✔
189

190
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
13,734,052✔
191

192
bool fmIsSpecialDataRequiredFunc(int32_t funcId) {
707,715✔
193
  return isSpecificClassifyFunc(funcId, FUNC_MGT_SPECIAL_DATA_REQUIRED);
707,715✔
194
}
195

196
bool fmIsDynamicScanOptimizedFunc(int32_t funcId) {
223,903✔
197
  return isSpecificClassifyFunc(funcId, FUNC_MGT_DYNAMIC_SCAN_OPTIMIZED);
223,903✔
198
}
199

200
bool fmIsMultiResFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_RES_FUNC); }
5,802,841✔
201

202
bool fmIsRepeatScanFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_REPEAT_SCAN_FUNC); }
6,904,762✔
203

204
bool fmIsUserDefinedFunc(int32_t funcId) { return funcId > FUNC_UDF_ID_START; }
333,330,805✔
205

206
bool fmIsForbidFillFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_FILL_FUNC); }
3,014,370✔
207

208
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
26,101,475✔
209

210
bool fmIsForbidStreamFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_STREAM_FUNC); }
3,005,598✔
211

212
bool fmIsSystemInfoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SYSTEM_INFO_FUNC); }
3,032,826✔
213

214
bool fmIsImplicitTsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IMPLICIT_TS_FUNC); }
59,452,424✔
215

216
bool fmIsClientPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CLIENT_PC_FUNC); }
3,030,171✔
217

218
bool fmIsMultiRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_ROWS_FUNC); }
5,843,066✔
219

220
bool fmIsKeepOrderFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_KEEP_ORDER_FUNC); }
1,168,632✔
221

222
bool fmIsCumulativeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CUMULATIVE_FUNC); }
406,060✔
223

224
bool fmIsForbidSysTableFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_SYSTABLE_FUNC); }
3,005,581✔
225

226
bool fmIsInterpFunc(int32_t funcId) {
6,060,152✔
227
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
6,060,152!
228
    return false;
398✔
229
  }
230
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
6,059,754✔
231
}
232

233
bool fmIsInterpPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERP_PC_FUNC); }
5,843,506✔
234

235
bool fmIsForecastFunc(int32_t funcId) {
5,770,314✔
236
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
5,770,314!
237
    return false;
399✔
238
  }
239
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
5,769,915✔
240
}
241

242
bool fmIsForecastPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORECAST_PC_FUNC); }
5,853,729✔
243

244
bool fmIsLastRowFunc(int32_t funcId) {
86,816✔
245
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
86,816!
246
    return false;
×
247
  }
248
  return FUNCTION_TYPE_LAST_ROW == funcMgtBuiltins[funcId].type;
86,818✔
249
}
250

251
bool fmIsNotNullOutputFunc(int32_t funcId) {
10,082,083✔
252
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
10,082,083!
253
    return false;
×
254
  }
255
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
19,973,876✔
256
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
9,887,462✔
257
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
9,272,020✔
258
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
8,964,609✔
259
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
8,797,046✔
260
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
8,563,120✔
261
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
8,446,810✔
262
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
6,669,595✔
263
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
26,580,602✔
264
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
6,606,726✔
265
}
266

267
bool fmIsSelectValueFunc(int32_t funcId) {
604,345✔
268
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
604,345!
269
    return false;
×
270
  }
271
  return FUNCTION_TYPE_SELECT_VALUE == funcMgtBuiltins[funcId].type;
604,345✔
272
}
273

274
bool fmIsGroupKeyFunc(int32_t funcId) {
377,529,314✔
275
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
377,529,314!
276
    return false;
×
277
  }
278
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
377,542,556✔
279
}
280

281
bool fmisSelectGroupConstValueFunc(int32_t funcId) {
70✔
282
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
70!
283
    return false;
×
284
  }
285
  return FUNCTION_TYPE_GROUP_CONST_VALUE == funcMgtBuiltins[funcId].type;
70✔
286
}
287

288
bool fmIsElapsedFunc(int32_t funcId) {
15,368,911✔
289
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
15,368,911!
290
    return false;
×
291
  }
292
  return FUNCTION_TYPE_ELAPSED == funcMgtBuiltins[funcId].type;
15,368,930✔
293
}
294

295
bool fmIsBlockDistFunc(int32_t funcId) {
3,004,615✔
296
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
3,004,615!
297
    return false;
195✔
298
  }
299
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
3,004,420✔
300
}
301

302
bool fmIsDBUsageFunc(int32_t funcId) {
3,004,639✔
303
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
3,004,639!
304
    return false;
198✔
305
  }
306
  return FUNCTION_TYPE_DB_USAGE == funcMgtBuiltins[funcId].type;
3,004,441✔
307
}
308

309
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
4,570,233✔
310

311
bool fmIsIgnoreNullFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IGNORE_NULL_FUNC); }
2,535✔
312

313
void fmFuncMgtDestroy() {
2,765✔
314
  void* m = gFunMgtService.pFuncNameHashTable;
2,765✔
315
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
2,765!
316
    taosHashCleanup(m);
2,765✔
317
  }
318
}
2,765✔
319

320
#ifdef BUILD_NO_CALL
321
int32_t fmSetInvertFunc(int32_t funcId, SFuncExecFuncs* pFpSet) {
322
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
323
    return TSDB_CODE_FAILED;
324
  }
325
  pFpSet->process = funcMgtBuiltins[funcId].invertFunc;
326
  return TSDB_CODE_SUCCESS;
327
}
328

329
int32_t fmSetNormalFunc(int32_t funcId, SFuncExecFuncs* pFpSet) {
330
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
331
    return TSDB_CODE_FAILED;
332
  }
333
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
334
  return TSDB_CODE_SUCCESS;
335
}
336

337
bool fmIsInvertible(int32_t funcId) {
338
  bool res = false;
339
  switch (funcMgtBuiltins[funcId].type) {
340
    case FUNCTION_TYPE_COUNT:
341
    case FUNCTION_TYPE_SUM:
342
    case FUNCTION_TYPE_STDDEV:
343
    case FUNCTION_TYPE_AVG:
344
    case FUNCTION_TYPE_WSTART:
345
    case FUNCTION_TYPE_WEND:
346
    case FUNCTION_TYPE_WDURATION:
347
      res = true;
348
      break;
349
    default:
350
      break;
351
  }
352
  return res;
353
}
354
#endif
355

356
// function has same input/output type
357
bool fmIsSameInOutType(int32_t funcId) {
283,559✔
358
  bool res = false;
283,559✔
359
  switch (funcMgtBuiltins[funcId].type) {
283,559✔
360
    case FUNCTION_TYPE_MAX:
90,884✔
361
    case FUNCTION_TYPE_MIN:
362
    case FUNCTION_TYPE_TOP:
363
    case FUNCTION_TYPE_BOTTOM:
364
    case FUNCTION_TYPE_FIRST:
365
    case FUNCTION_TYPE_LAST:
366
    case FUNCTION_TYPE_SAMPLE:
367
    case FUNCTION_TYPE_TAIL:
368
    case FUNCTION_TYPE_UNIQUE:
369
      res = true;
90,884✔
370
      break;
90,884✔
371
    default:
192,675✔
372
      break;
192,675✔
373
  }
374
  return res;
283,559✔
375
}
376

377
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
63,150✔
378
  SNode* pNode;
379
  FOREACH(pNode, pFunc->pParameterList) {
63,780✔
380
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
3,550✔
381
      return false;
2,920✔
382
    }
383
  }
384
  return true;
60,230✔
385
}
386

387
bool fmIsSkipScanCheckFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SKIP_SCAN_CHECK_FUNC); }
223,887✔
388

389
bool fmIsPrimaryKeyFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PRIMARY_KEY_FUNC); }
2,463,053✔
390
void getLastCacheDataType(SDataType* pType, int32_t pkBytes) {
2,612✔
391
  // TODO: do it later.
392
  pType->bytes = getFirstLastInfoSize(pType->bytes, pkBytes) + VARSTR_HEADER_SIZE;
2,612✔
393
  pType->type = TSDB_DATA_TYPE_BINARY;
2,612✔
394
}
2,612✔
395

396
static int32_t getFuncInfo(SFunctionNode* pFunc) {
911,196✔
397
  char msg[128] = {0};
911,196✔
398
  return fmGetFuncInfo(pFunc, msg, sizeof(msg));
911,196✔
399
}
400

401
int32_t createFunction(const char* pName, SNodeList* pParameterList, SFunctionNode** ppFunc) {
110,732✔
402
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
110,732✔
403
  if (NULL == *ppFunc) {
110,732!
404
    return code;
×
405
  }
406
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
110,732✔
407
  (*ppFunc)->pParameterList = pParameterList;
110,732✔
408
  code = getFuncInfo((*ppFunc));
110,732✔
409
  if (TSDB_CODE_SUCCESS != code) {
110,732!
410
    (*ppFunc)->pParameterList = NULL;
×
411
    nodesDestroyNode((SNode*)*ppFunc);
×
412
    *ppFunc = NULL;
×
413
    return code;
×
414
  }
415
  return code;
110,732✔
416
}
417

418
static void resetOutputChangedFunc(SFunctionNode *pFunc, const SFunctionNode* pSrcFunc) {
800,464✔
419
  if (funcMgtBuiltins[pFunc->funcId].type == FUNCTION_TYPE_LAST_MERGE) {
800,464✔
420
    pFunc->node.resType = pSrcFunc->node.resType;
12,708✔
421
    return;
12,708✔
422
  }
423
}
424

425
int32_t createFunctionWithSrcFunc(const char* pName, const SFunctionNode* pSrcFunc, SNodeList* pParameterList, SFunctionNode** ppFunc) {
800,464✔
426
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
800,464✔
427
  if (NULL == *ppFunc) {
800,464!
428
    return code;
×
429
  }
430

431
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
800,464✔
432
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
800,464✔
433

434
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
800,464✔
435
  (*ppFunc)->pParameterList = pParameterList;
800,464✔
436
  code = getFuncInfo((*ppFunc));
800,464✔
437
  if (TSDB_CODE_SUCCESS != code) {
800,464!
438
    (*ppFunc)->pParameterList = NULL;
×
439
    nodesDestroyNode((SNode*)*ppFunc);
×
440
    *ppFunc = NULL;
×
441
    return code;
×
442
  }
443
  resetOutputChangedFunc(*ppFunc, pSrcFunc);
800,464✔
444
  return code;
800,464✔
445
}
446

447
static int32_t createColumnByFunc(const SFunctionNode* pFunc, SColumnNode** ppCol) {
516,905✔
448
  int32_t code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)ppCol);
516,905✔
449
  if (NULL == *ppCol) {
516,905!
450
    return code;
×
451
  }
452
  tstrncpy((*ppCol)->colName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
516,905✔
453
  (*ppCol)->node.resType = pFunc->node.resType;
516,905✔
454
  return TSDB_CODE_SUCCESS;
516,905✔
455
}
456

457
bool fmIsDistExecFunc(int32_t funcId) {
1,470,289✔
458
  if (fmIsUserDefinedFunc(funcId)) {
1,470,289✔
459
    return false;
24✔
460
  }
461
  if (!fmIsVectorFunc(funcId)) {
1,470,313!
462
    return true;
×
463
  }
464
  return (NULL != funcMgtBuiltins[funcId].pPartialFunc && NULL != funcMgtBuiltins[funcId].pMergeFunc);
1,470,325!
465
}
466

467
static int32_t createPartialFunction(const SFunctionNode* pSrcFunc, SFunctionNode** pPartialFunc) {
283,559✔
468
  SNodeList* pParameterList = NULL;
283,559✔
469
  int32_t    code = nodesCloneList(pSrcFunc->pParameterList, &pParameterList);
283,559✔
470
  if (NULL == pParameterList) {
283,559!
471
    return code;
×
472
  }
473
  code =
474
      createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pPartialFunc, pSrcFunc, pParameterList, pPartialFunc);
283,559✔
475
  if (TSDB_CODE_SUCCESS != code) {
283,559!
476
    nodesDestroyList(pParameterList);
×
477
    return code;
×
478
  }
479
  (*pPartialFunc)->hasOriginalFunc = true;
283,559✔
480
  (*pPartialFunc)->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
283,559✔
481
  char name[TSDB_FUNC_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_POINTER_PRINT_BYTES + 1] = {0};
283,559✔
482

483
  int32_t len = tsnprintf(name, sizeof(name), "%s.%p", (*pPartialFunc)->functionName, pSrcFunc);
283,559✔
484
  if (taosHashBinary(name, len) < 0) {
283,559!
485
    return TSDB_CODE_FAILED;
×
486
  }
487
  tstrncpy((*pPartialFunc)->node.aliasName, name, TSDB_COL_NAME_LEN);
283,559✔
488
  return TSDB_CODE_SUCCESS;
283,559✔
489
}
490

491
static int32_t createMergeFuncPara(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
516,905✔
492
                                   SNodeList** pParameterList) {
493
  SNode*  pRes = NULL;
516,905✔
494
  int32_t code = createColumnByFunc(pPartialFunc, (SColumnNode**)&pRes);
516,905✔
495
  if (TSDB_CODE_SUCCESS != code) {
516,905!
496
    return code;
×
497
  }
498
  if (NULL != funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc) {
516,905✔
499
    return funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc(pSrcFunc->pParameterList, pRes, pParameterList);
4,436✔
500
  } else {
501
    return nodesListMakeStrictAppend(pParameterList, pRes);
512,469✔
502
  }
503
}
504

505
static int32_t createMidFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
233,346✔
506
                                 SFunctionNode** pMidFunc) {
507
  SNodeList*     pParameterList = NULL;
233,346✔
508
  SFunctionNode* pFunc = NULL;
233,346✔
509

510
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
233,346✔
511
  if (TSDB_CODE_SUCCESS == code) {
233,346!
512
    if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){
233,346✔
513
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pSrcFunc, pParameterList, &pFunc);
18,606✔
514
    }else{
515
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
214,740✔
516
    }
517
  }
518
  if (TSDB_CODE_SUCCESS == code) {
233,346!
519
    tstrncpy(pFunc->node.aliasName, pPartialFunc->node.aliasName, TSDB_COL_NAME_LEN);
233,346✔
520
  }
521

522
  if (TSDB_CODE_SUCCESS == code) {
233,346!
523
    *pMidFunc = pFunc;
233,346✔
524
  } else {
525
    nodesDestroyList(pParameterList);
×
526
  }
527
  return code;
233,346✔
528
}
529

530
static int32_t createMergeFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
283,559✔
531
                                   SFunctionNode** pMergeFunc) {
532
  SNodeList*     pParameterList = NULL;
283,559✔
533
  SFunctionNode* pFunc = NULL;
283,559✔
534

535
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
283,559✔
536
  if (TSDB_CODE_SUCCESS == code) {
283,559!
537
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
283,559✔
538
  }
539
  if (TSDB_CODE_SUCCESS == code) {
283,559!
540
    pFunc->hasOriginalFunc = true;
283,559✔
541
    pFunc->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
283,559✔
542
    // overwrite function restype set by translate function
543
    if (fmIsSameInOutType(pSrcFunc->funcId)) {
283,559✔
544
      pFunc->node.resType = pSrcFunc->node.resType;
90,884✔
545
    }
546
    tstrncpy(pFunc->node.aliasName, pSrcFunc->node.aliasName, TSDB_COL_NAME_LEN);
283,559✔
547
  }
548

549
  if (TSDB_CODE_SUCCESS == code) {
283,559!
550
    *pMergeFunc = pFunc;
283,559✔
551
  } else {
552
    nodesDestroyList(pParameterList);
×
553
  }
554
  return code;
283,559✔
555
}
556

557
int32_t fmGetDistMethod(const SFunctionNode* pFunc, SFunctionNode** pPartialFunc, SFunctionNode** pMidFunc,
283,559✔
558
                        SFunctionNode** pMergeFunc) {
559
  if (!fmIsDistExecFunc(pFunc->funcId)) {
283,559!
560
    return TSDB_CODE_FAILED;
×
561
  }
562

563
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
283,559✔
564
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
283,559!
565
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
233,346✔
566
  }
567
  if (TSDB_CODE_SUCCESS == code) {
283,559!
568
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
283,559✔
569
  }
570

571
  if (TSDB_CODE_SUCCESS != code) {
283,559!
572
    nodesDestroyNode((SNode*)*pPartialFunc);
×
573
    if (pMidFunc) nodesDestroyNode((SNode*)*pMidFunc);
×
574
    nodesDestroyNode((SNode*)*pMergeFunc);
×
575
  }
576

577
  return code;
283,559✔
578
}
579

580
char* fmGetFuncName(int32_t funcId) {
2✔
581
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2!
582
    return taosStrdup("invalid function");
×
583
  }
584
  return taosStrdup(funcMgtBuiltins[funcId].name);
2!
585
}
586

587
/// @param [out] pStateFunc, not changed if error occured or no need to create state func
588
/// @retval 0 for succ, otherwise err occured
589
static int32_t fmCreateStateFunc(const SFunctionNode* pFunc, SFunctionNode** pStateFunc) {
33,072✔
590
  if (funcMgtBuiltins[pFunc->funcId].pStateFunc) {
33,072!
591
    SNodeList* pParams = NULL;
33,072✔
592
    int32_t    code = nodesCloneList(pFunc->pParameterList, &pParams);
33,072✔
593
    if (!pParams) return code;
33,072!
594
    code = createFunction(funcMgtBuiltins[pFunc->funcId].pStateFunc, pParams, pStateFunc);
33,072✔
595
    if (TSDB_CODE_SUCCESS != code) {
33,072!
596
      nodesDestroyList(pParams);
×
597
      return code;
×
598
    }
599
    tstrncpy((*pStateFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
33,072✔
600
    tstrncpy((*pStateFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
33,072✔
601
  }
602
  return TSDB_CODE_SUCCESS;
33,072✔
603
}
604

605
bool fmIsTSMASupportedFunc(func_id_t funcId) {
524,178✔
606
  return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC) &&
1,025,694✔
607
         !isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_STREAM_FUNC);
501,516!
608
}
609

610
int32_t fmCreateStateFuncs(SNodeList* pFuncs) {
152✔
611
  int32_t code;
612
  SNode*  pNode;
613
  char    buf[128] = {0};
152✔
614
  FOREACH(pNode, pFuncs) {
33,224!
615
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
33,072✔
616
    code = fmGetFuncInfo(pFunc, buf, 128);
33,072✔
617
    if (code) break;
33,072!
618
    if (fmIsTSMASupportedFunc(pFunc->funcId)) {
33,072!
619
      SFunctionNode* pNewFunc = NULL;
33,072✔
620
      code = fmCreateStateFunc(pFunc, &pNewFunc);
33,072✔
621
      if (code) {
33,072!
622
        // error
623
        break;
×
624
      } else if (!pNewFunc) {
33,072!
625
        // no need state func
626
        continue;
×
627
      } else {
628
        REPLACE_NODE(pNewFunc);
33,072✔
629
        nodesDestroyNode(pNode);
33,072✔
630
      }
631
    }
632
  }
633
  return code;
152✔
634
}
635

636
static int32_t fmCreateStateMergeFunc(SFunctionNode* pFunc, SFunctionNode** pStateMergeFunc) {
422✔
637
  if (funcMgtBuiltins[pFunc->funcId].pMergeFunc) {
422✔
638
    SNodeList* pParams = NULL;
410✔
639
    int32_t    code = nodesCloneList(pFunc->pParameterList, &pParams);
410✔
640
    if (!pParams) return code;
410!
641
    code = createFunction(funcMgtBuiltins[pFunc->funcId].pMergeFunc, pParams, pStateMergeFunc);
410✔
642
    if (TSDB_CODE_SUCCESS != code) {
410!
643
      nodesDestroyList(pParams);
×
644
      return code;
×
645
    }
646
    tstrncpy((*pStateMergeFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
410✔
647
    tstrncpy((*pStateMergeFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
410✔
648
  }
649
  return TSDB_CODE_SUCCESS;
422✔
650
}
651

652
int32_t fmCreateStateMergeFuncs(SNodeList* pFuncs) {
91✔
653
  int32_t code;
654
  SNode*  pNode;
655
  char    buf[128] = {0};
91✔
656
  FOREACH(pNode, pFuncs) {
513!
657
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
422✔
658
    if (fmIsTSMASupportedFunc(pFunc->funcId)) {
422!
659
      SFunctionNode* pNewFunc = NULL;
422✔
660
      code = fmCreateStateMergeFunc(pFunc, &pNewFunc);
422✔
661
      if (code) {
422!
662
        // error
663
        break;
×
664
      } else if (!pNewFunc) {
422✔
665
        // no state merge func
666
        continue;
12✔
667
      } else {
668
        REPLACE_NODE(pNewFunc);
410✔
669
        nodesDestroyNode(pNode);
410✔
670
      }
671
    }
672
  }
673
  return code;
91✔
674
}
675

676
int32_t fmGetFuncId(const char* name) {
85,080✔
677
  if (NULL != gFunMgtService.pFuncNameHashTable) {
85,080!
678
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, name, strlen(name));
85,080✔
679
    if (NULL != pVal) {
85,080!
680
      return *(int32_t*)pVal;
85,080✔
681
    }
682
    return -1;
×
683
  }
684
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
×
685
    if (0 == strcmp(funcMgtBuiltins[i].name, name)) {
×
686
      return i;
×
687
    }
688
  }
689
  return -1;
×
690
}
691

692
bool fmIsMyStateFunc(int32_t funcId, int32_t stateFuncId) {
16,726,100✔
693
  const SBuiltinFuncDefinition* pFunc = &funcMgtBuiltins[funcId];
16,726,100✔
694
  const SBuiltinFuncDefinition* pStateFunc = &funcMgtBuiltins[stateFuncId];
16,726,100✔
695
  if (!pFunc->pStateFunc) {
16,726,100!
696
    return false;
×
697
  }
698
  if (strcmp(pFunc->pStateFunc, pStateFunc->name) == 0) return true;
16,726,100✔
699
  int32_t stateMergeFuncId = fmGetFuncId(pFunc->pStateFunc);
83,334✔
700
  if (stateMergeFuncId == -1) {
83,334!
701
    return false;
×
702
  }
703
  const SBuiltinFuncDefinition* pStateMergeFunc = &funcMgtBuiltins[stateMergeFuncId];
83,334✔
704
  return strcmp(pStateFunc->name, pStateMergeFunc->pMergeFunc) == 0;
83,334✔
705
}
706

707
bool fmIsCountLikeFunc(int32_t funcId) {
1,055,322✔
708
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
1,055,322✔
709
}
710

711
bool fmIsRowTsOriginFunc(int32_t funcId) {
17,043✔
712
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
17,043!
713
    return false;
×
714
  }
715
  return FUNCTION_TYPE_IROWTS_ORIGIN == funcMgtBuiltins[funcId].type;
17,043✔
716
}
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