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

taosdata / TDengine / #3626

28 Feb 2025 03:34AM UTC coverage: 63.764% (+0.1%) from 63.633%
#3626

push

travis-ci

web-flow
Merge pull request #29961 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

149233 of 299935 branches covered (49.76%)

Branch coverage included in aggregate %.

53 of 91 new or added lines in 8 files covered. (58.24%)

3267 existing lines in 138 files now uncovered.

233601 of 300457 relevant lines covered (77.75%)

17374158.38 hits per line

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

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

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

51
static bool isSpecificClassifyFunc(int32_t funcId, uint64_t classification) {
334,064,219✔
52
  if (fmIsUserDefinedFunc(funcId)) {
334,064,219✔
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);
56,793✔
56
  }
57
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
334,040,161!
58
    return false;
78,027,624✔
59
  }
60
  return FUNC_MGT_TEST_MASK(funcMgtBuiltins[funcId].classification, classification);
256,012,537✔
61
}
62

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

68
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
4,929,834✔
69
  if (NULL != gFunMgtService.pFuncNameHashTable) {
4,929,834✔
70
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
4,924,914✔
71
    if (NULL != pVal) {
4,924,957✔
72
      pFunc->funcId = *(int32_t*)pVal;
4,924,737✔
73
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
4,924,737✔
74
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
4,924,737✔
75
    }
76
    return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
220✔
77
  }
78
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
99,104!
79
    if (0 == strcmp(funcMgtBuiltins[i].name, pFunc->functionName)) {
99,115✔
80
      pFunc->funcId = i;
4,931✔
81
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
4,931✔
82
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
4,931✔
83
    }
84
  }
85
  return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
×
86
}
87

88
EFuncReturnRows fmGetFuncReturnRows(SFunctionNode* pFunc) {
94,944✔
89
  if (NULL != funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc) {
94,944✔
90
    return funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc(pFunc);
54,125✔
91
  }
92
  return (fmIsIndefiniteRowsFunc(pFunc->funcId) || fmIsMultiRowsFunc(pFunc->funcId)) ? FUNC_RETURN_ROWS_INDEFINITE
40,819!
93
                                                                                     : FUNC_RETURN_ROWS_NORMAL;
40,819!
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) {
3,216,102✔
101
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
3,216,102✔
102
  if (NULL != pVal) {
3,216,141✔
103
    return funcMgtBuiltins[*(int32_t*)pVal].type;
3,215,921✔
104
  }
105
  return FUNCTION_TYPE_UDF;
220✔
106
}
107

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

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

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

129
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
1,610,939✔
130
    return FUNC_DATA_REQUIRED_DATA_LOAD;
1,590,732✔
131
  } else {
132
    return funcMgtBuiltins[funcId].dynDataRequiredFunc(pRes, pBlockInfo);
20,207✔
133
  }
134
}
135

136
int32_t fmGetFuncExecFuncs(int32_t funcId, SFuncExecFuncs* pFpSet) {
7,350,930✔
137
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
7,350,930!
138
    return TSDB_CODE_FAILED;
1,251✔
139
  }
140
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
7,354,450✔
141
  pFpSet->init = funcMgtBuiltins[funcId].initFunc;
7,354,450✔
142
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
7,354,450✔
143
  pFpSet->finalize = funcMgtBuiltins[funcId].finalizeFunc;
7,354,450✔
144
  pFpSet->combine = funcMgtBuiltins[funcId].combineFunc;
7,354,450✔
145
  pFpSet->processFuncByRow = funcMgtBuiltins[funcId].processFuncByRow;
7,354,450✔
146
  pFpSet->cleanup = funcMgtBuiltins[funcId].cleanupFunc;
7,354,450✔
147
  return TSDB_CODE_SUCCESS;
7,354,450✔
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,738,469✔
162
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
11,738,469!
UNCOV
163
    return TSDB_CODE_FAILED;
×
164
  }
165
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
11,741,194✔
166
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
11,741,194✔
167
  return TSDB_CODE_SUCCESS;
11,741,194✔
168
}
169

170
bool fmIsAggFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_AGG_FUNC); }
22,885,362✔
171

172
bool fmIsScalarFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCALAR_FUNC); }
14,774,044✔
173

174
bool fmIsVectorFunc(int32_t funcId) { return !fmIsScalarFunc(funcId) && !fmIsPseudoColumnFunc(funcId); }
6,779,374✔
175

176
bool fmIsSelectColsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_COLS_FUNC); }
587,028✔
177

178
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
32,142,464✔
179

180
bool fmIsTimelineFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TIMELINE_FUNC); }
6,443,452✔
181

182
bool fmIsDateTimeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_DATETIME_FUNC); }
3,340,059✔
183

184
bool fmIsPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PSEUDO_COLUMN_FUNC); }
33,297,077✔
185

186
bool fmIsScanPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCAN_PC_FUNC); }
23,353,550✔
187

188
bool fmIsWindowPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_WINDOW_PC_FUNC); }
16,062,496✔
189

190
bool fmIsWindowClauseFunc(int32_t funcId) { return fmIsAggFunc(funcId) || fmIsWindowPseudoColumnFunc(funcId); }
445,423✔
191

192
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
16,292,136✔
193

194
bool fmIsSpecialDataRequiredFunc(int32_t funcId) {
819,368✔
195
  return isSpecificClassifyFunc(funcId, FUNC_MGT_SPECIAL_DATA_REQUIRED);
819,368✔
196
}
197

198
bool fmIsDynamicScanOptimizedFunc(int32_t funcId) {
277,180✔
199
  return isSpecificClassifyFunc(funcId, FUNC_MGT_DYNAMIC_SCAN_OPTIMIZED);
277,180✔
200
}
201

202
bool fmIsMultiResFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_RES_FUNC); }
6,604,506✔
203

204
bool fmIsRepeatScanFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_REPEAT_SCAN_FUNC); }
7,537,967✔
205

206
bool fmIsUserDefinedFunc(int32_t funcId) { return funcId > FUNC_UDF_ID_START; }
378,257,411✔
207

208
bool fmIsForbidFillFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_FILL_FUNC); }
3,352,782✔
209

210
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
32,547,861✔
211

212
bool fmIsForbidStreamFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_STREAM_FUNC); }
3,341,062✔
213

214
bool fmIsSystemInfoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SYSTEM_INFO_FUNC); }
3,372,549✔
215

216
bool fmIsImplicitTsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IMPLICIT_TS_FUNC); }
68,012,679✔
217

218
bool fmIsClientPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CLIENT_PC_FUNC); }
3,369,828✔
219

220
bool fmIsMultiRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_ROWS_FUNC); }
6,487,920✔
221

222
bool fmIsKeepOrderFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_KEEP_ORDER_FUNC); }
1,277,497✔
223

224
bool fmIsCumulativeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CUMULATIVE_FUNC); }
604,822✔
225

226
bool fmIsForbidSysTableFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_SYSTABLE_FUNC); }
3,341,058✔
227

228
bool fmIsInterpFunc(int32_t funcId) {
6,708,221✔
229
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
6,708,221!
230
    return false;
389✔
231
  }
232
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
6,707,832✔
233
}
234

235
bool fmIsInterpPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERP_PC_FUNC); }
6,524,456✔
236

237
bool fmIsForecastFunc(int32_t funcId) {
6,403,132✔
238
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
6,403,132!
239
    return false;
367✔
240
  }
241
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
6,402,765✔
242
}
243

244
bool fmIsForecastPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORECAST_PC_FUNC); }
6,534,924✔
245

246
bool fmIsLastRowFunc(int32_t funcId) {
222,112✔
247
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
222,112!
248
    return false;
×
249
  }
250
  return FUNCTION_TYPE_LAST_ROW == funcMgtBuiltins[funcId].type;
222,114✔
251
}
252

253
bool fmIsNotNullOutputFunc(int32_t funcId) {
10,846,029✔
254
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
10,846,029!
255
    return false;
×
256
  }
257
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
21,443,002✔
258
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
10,594,063✔
259
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
9,928,998✔
260
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
9,601,263✔
261
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
9,393,772✔
262
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
9,167,532✔
263
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
9,057,123✔
264
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
7,230,087✔
265
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
28,610,893✔
266
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
7,167,891✔
267
}
268

269
bool fmIsSelectValueFunc(int32_t funcId) {
1,011,619✔
270
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,011,619!
271
    return false;
×
272
  }
273
  return FUNCTION_TYPE_SELECT_VALUE == funcMgtBuiltins[funcId].type;
1,011,619✔
274
}
275

276
bool fmIsGroupKeyFunc(int32_t funcId) {
202,367,770✔
277
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
202,367,770!
278
    return false;
×
279
  }
280
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
202,369,676✔
281
}
282

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

290
bool fmIsElapsedFunc(int32_t funcId) {
20,707,910✔
291
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
20,707,910!
292
    return false;
11✔
293
  }
294
  return FUNCTION_TYPE_ELAPSED == funcMgtBuiltins[funcId].type;
20,707,899✔
295
}
296

297
bool fmIsBlockDistFunc(int32_t funcId) {
3,340,048✔
298
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
3,340,048!
299
    return false;
142✔
300
  }
301
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
3,339,906✔
302
}
303

304
bool fmIsDBUsageFunc(int32_t funcId) {
3,340,055✔
305
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
3,340,055!
306
    return false;
188✔
307
  }
308
  return FUNCTION_TYPE_DB_USAGE == funcMgtBuiltins[funcId].type;
3,339,867✔
309
}
310

311
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
5,219,646✔
312

313
bool fmIsIgnoreNullFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IGNORE_NULL_FUNC); }
2,284✔
314

315
void fmFuncMgtDestroy() {
4,482✔
316
  void* m = gFunMgtService.pFuncNameHashTable;
4,482✔
317
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
4,482!
318
    taosHashCleanup(m);
4,481✔
319
  }
320
}
4,482✔
321

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

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

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

358
// function has same input/output type
359
bool fmIsSameInOutType(int32_t funcId) {
329,180✔
360
  bool res = false;
329,180✔
361
  switch (funcMgtBuiltins[funcId].type) {
329,180✔
362
    case FUNCTION_TYPE_MAX:
112,397✔
363
    case FUNCTION_TYPE_MIN:
364
    case FUNCTION_TYPE_TOP:
365
    case FUNCTION_TYPE_BOTTOM:
366
    case FUNCTION_TYPE_FIRST:
367
    case FUNCTION_TYPE_LAST:
368
    case FUNCTION_TYPE_SAMPLE:
369
    case FUNCTION_TYPE_TAIL:
370
    case FUNCTION_TYPE_UNIQUE:
371
      res = true;
112,397✔
372
      break;
112,397✔
373
    default:
216,783✔
374
      break;
216,783✔
375
  }
376
  return res;
329,180✔
377
}
378

379
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
71,873✔
380
  SNode* pNode;
381
  FOREACH(pNode, pFunc->pParameterList) {
72,503✔
382
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
3,733✔
383
      return false;
3,103✔
384
    }
385
  }
386
  return true;
68,770✔
387
}
388

389
bool fmIsSkipScanCheckFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SKIP_SCAN_CHECK_FUNC); }
277,165✔
390

391
bool fmIsPrimaryKeyFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PRIMARY_KEY_FUNC); }
2,947,308✔
392
void getLastCacheDataType(SDataType* pType, int32_t pkBytes) {
21,516✔
393
  // TODO: do it later.
394
  pType->bytes = getFirstLastInfoSize(pType->bytes, pkBytes) + VARSTR_HEADER_SIZE;
21,516✔
395
  pType->type = TSDB_DATA_TYPE_BINARY;
21,516✔
396
}
21,516✔
397

398
static int32_t getFuncInfo(SFunctionNode* pFunc) {
1,048,624✔
399
  char msg[128] = {0};
1,048,624✔
400
  return fmGetFuncInfo(pFunc, msg, sizeof(msg));
1,048,624✔
401
}
402

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

420
static void resetOutputChangedFunc(SFunctionNode *pFunc, const SFunctionNode* pSrcFunc) {
937,675✔
421
  if (funcMgtBuiltins[pFunc->funcId].type == FUNCTION_TYPE_LAST_MERGE) {
937,675✔
422
    pFunc->node.resType = pSrcFunc->node.resType;
51,166✔
423
    return;
51,166✔
424
  }
425
}
426

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

433
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
937,675✔
434
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
937,675✔
435

436
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
937,675✔
437
  (*ppFunc)->pParameterList = pParameterList;
937,675✔
438
  code = getFuncInfo((*ppFunc));
937,675✔
439
  if (TSDB_CODE_SUCCESS != code) {
937,675!
440
    (*ppFunc)->pParameterList = NULL;
×
441
    nodesDestroyNode((SNode*)*ppFunc);
×
442
    *ppFunc = NULL;
×
443
    return code;
×
444
  }
445
  resetOutputChangedFunc(*ppFunc, pSrcFunc);
937,675✔
446
  (*ppFunc)->node.relatedTo = pSrcFunc->node.relatedTo;
937,675✔
447
  (*ppFunc)->node.bindExprID = pSrcFunc->node.bindExprID;
937,675✔
448
  return code;
937,675✔
449
}
450

451
static int32_t createColumnByFunc(const SFunctionNode* pFunc, SColumnNode** ppCol) {
608,495✔
452
  int32_t code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)ppCol);
608,495✔
453
  if (NULL == *ppCol) {
608,495!
454
    return code;
×
455
  }
456
  tstrncpy((*ppCol)->colName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
608,495✔
457
  (*ppCol)->node.resType = pFunc->node.resType;
608,495✔
458
  return TSDB_CODE_SUCCESS;
608,495✔
459
}
460

461
bool fmIsDistExecFunc(int32_t funcId) {
1,718,301✔
462
  if (fmIsUserDefinedFunc(funcId)) {
1,718,301✔
463
    return false;
24✔
464
  }
465
  if (!fmIsVectorFunc(funcId)) {
1,718,333!
466
    return true;
×
467
  }
468
  return (NULL != funcMgtBuiltins[funcId].pPartialFunc && NULL != funcMgtBuiltins[funcId].pMergeFunc);
1,718,331!
469
}
470

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

487
  int32_t len = tsnprintf(name, sizeof(name), "%s.%p", (*pPartialFunc)->functionName, pSrcFunc);
329,180✔
488
  if (taosHashBinary(name, len) < 0) {
329,180!
489
    return TSDB_CODE_FAILED;
×
490
  }
491
  tstrncpy((*pPartialFunc)->node.aliasName, name, TSDB_COL_NAME_LEN);
329,180✔
492
  return TSDB_CODE_SUCCESS;
329,180✔
493
}
494

495
static int32_t createMergeFuncPara(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
608,495✔
496
                                   SNodeList** pParameterList) {
497
  SNode*  pRes = NULL;
608,495✔
498
  int32_t code = createColumnByFunc(pPartialFunc, (SColumnNode**)&pRes);
608,495✔
499
  if (TSDB_CODE_SUCCESS != code) {
608,495!
500
    return code;
×
501
  }
502
  if (NULL != funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc) {
608,495✔
503
    return funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc(pSrcFunc->pParameterList, pRes, pParameterList);
4,988✔
504
  } else {
505
    return nodesListMakeStrictAppend(pParameterList, pRes);
603,507✔
506
  }
507
}
508

509
static int32_t createMidFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
279,315✔
510
                                 SFunctionNode** pMidFunc) {
511
  SNodeList*     pParameterList = NULL;
279,315✔
512
  SFunctionNode* pFunc = NULL;
279,315✔
513

514
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
279,315✔
515
  if (TSDB_CODE_SUCCESS == code) {
279,315!
516
    if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){
279,315✔
517
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pSrcFunc, pParameterList, &pFunc);
18,870✔
518
    }else{
519
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
260,445✔
520
    }
521
  }
522
  if (TSDB_CODE_SUCCESS == code) {
279,315!
523
    tstrncpy(pFunc->node.aliasName, pPartialFunc->node.aliasName, TSDB_COL_NAME_LEN);
279,315✔
524
  }
525

526
  if (TSDB_CODE_SUCCESS == code) {
279,315!
527
    *pMidFunc = pFunc;
279,315✔
528
  } else {
529
    nodesDestroyList(pParameterList);
×
530
  }
531
  return code;
279,315✔
532
}
533

534
static int32_t createMergeFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
329,180✔
535
                                   SFunctionNode** pMergeFunc) {
536
  SNodeList*     pParameterList = NULL;
329,180✔
537
  SFunctionNode* pFunc = NULL;
329,180✔
538

539
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
329,180✔
540
  if (TSDB_CODE_SUCCESS == code) {
329,180!
541
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
329,180✔
542
  }
543
  if (TSDB_CODE_SUCCESS == code) {
329,180!
544
    pFunc->hasOriginalFunc = true;
329,180✔
545
    pFunc->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
329,180✔
546
    // overwrite function restype set by translate function
547
    if (fmIsSameInOutType(pSrcFunc->funcId)) {
329,180✔
548
      pFunc->node.resType = pSrcFunc->node.resType;
112,397✔
549
    }
550
    tstrncpy(pFunc->node.aliasName, pSrcFunc->node.aliasName, TSDB_COL_NAME_LEN);
329,180✔
551
  }
552

553
  if (TSDB_CODE_SUCCESS == code) {
329,180!
554
    *pMergeFunc = pFunc;
329,180✔
555
  } else {
556
    nodesDestroyList(pParameterList);
×
557
  }
558
  return code;
329,180✔
559
}
560

561
int32_t fmGetDistMethod(const SFunctionNode* pFunc, SFunctionNode** pPartialFunc, SFunctionNode** pMidFunc,
329,180✔
562
                        SFunctionNode** pMergeFunc) {
563
  if (!fmIsDistExecFunc(pFunc->funcId)) {
329,180!
564
    return TSDB_CODE_FAILED;
×
565
  }
566

567
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
329,180✔
568
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
329,180!
569
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
279,315✔
570
  }
571
  if (TSDB_CODE_SUCCESS == code) {
329,180!
572
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
329,180✔
573
  }
574

575
  if (TSDB_CODE_SUCCESS != code) {
329,180!
576
    nodesDestroyNode((SNode*)*pPartialFunc);
×
577
    if (pMidFunc) nodesDestroyNode((SNode*)*pMidFunc);
×
578
    nodesDestroyNode((SNode*)*pMergeFunc);
×
579
  }
580

581
  return code;
329,180✔
582
}
583

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

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

609
bool fmIsTSMASupportedFunc(func_id_t funcId) {
564,584✔
610
  return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC) &&
1,106,407✔
611
         !isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_STREAM_FUNC);
541,823!
612
}
613

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

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

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

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

696
bool fmIsMyStateFunc(int32_t funcId, int32_t stateFuncId) {
16,733,902✔
697
  const SBuiltinFuncDefinition* pFunc = &funcMgtBuiltins[funcId];
16,733,902✔
698
  const SBuiltinFuncDefinition* pStateFunc = &funcMgtBuiltins[stateFuncId];
16,733,902✔
699
  if (!pFunc->pStateFunc) {
16,733,902!
700
    return false;
×
701
  }
702
  if (strcmp(pFunc->pStateFunc, pStateFunc->name) == 0) return true;
16,733,902✔
703
  int32_t stateMergeFuncId = fmGetFuncId(pFunc->pStateFunc);
82,827✔
704
  if (stateMergeFuncId == -1) {
82,827!
705
    return false;
×
706
  }
707
  const SBuiltinFuncDefinition* pStateMergeFunc = &funcMgtBuiltins[stateMergeFuncId];
82,827✔
708
  return strcmp(pStateFunc->name, pStateMergeFunc->pMergeFunc) == 0;
82,827✔
709
}
710

711
bool fmIsCountLikeFunc(int32_t funcId) {
1,539,367✔
712
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
1,539,367✔
713
}
714

715
bool fmIsRowTsOriginFunc(int32_t funcId) {
24,580✔
716
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
24,580!
717
    return false;
×
718
  }
719
  return FUNCTION_TYPE_IROWTS_ORIGIN == funcMgtBuiltins[funcId].type;
24,580✔
720
}
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