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

taosdata / TDengine / #3559

18 Dec 2024 12:59AM UTC coverage: 59.805% (+0.03%) from 59.778%
#3559

push

travis-ci

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

merge: main to 3.0 branch

132705 of 287544 branches covered (46.15%)

Branch coverage included in aggregate %.

87 of 95 new or added lines in 19 files covered. (91.58%)

1132 existing lines in 133 files now uncovered.

209591 of 284807 relevant lines covered (73.59%)

8125235.78 hits per line

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

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

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

51
static bool isSpecificClassifyFunc(int32_t funcId, uint64_t classification) {
158,764,617✔
52
  if (fmIsUserDefinedFunc(funcId)) {
158,764,617✔
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,146✔
56
  }
57
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
158,738,110!
58
    return false;
17,531,847✔
59
  }
60
  return FUNC_MGT_TEST_MASK(funcMgtBuiltins[funcId].classification, classification);
141,206,263✔
61
}
62

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

68
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
4,303,067✔
69
  if (NULL != gFunMgtService.pFuncNameHashTable) {
4,303,067✔
70
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
4,298,293✔
71
    if (NULL != pVal) {
4,298,303✔
72
      pFunc->funcId = *(int32_t*)pVal;
4,298,083✔
73
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
4,298,083✔
74
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
4,298,083✔
75
    }
76
    return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
220✔
77
  }
78
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
90,807!
79
    if (0 == strcmp(funcMgtBuiltins[i].name, pFunc->functionName)) {
90,811✔
80
      pFunc->funcId = i;
4,778✔
81
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
4,778✔
82
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
4,778✔
83
    }
84
  }
85
  return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
×
86
}
87

88
EFuncReturnRows fmGetFuncReturnRows(SFunctionNode* pFunc) {
81,955✔
89
  if (NULL != funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc) {
81,955✔
90
    return funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc(pFunc);
46,752✔
91
  }
92
  return (fmIsIndefiniteRowsFunc(pFunc->funcId) || fmIsMultiRowsFunc(pFunc->funcId)) ? FUNC_RETURN_ROWS_INDEFINITE
35,203!
93
                                                                                     : FUNC_RETURN_ROWS_NORMAL;
35,204!
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,770,709✔
101
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
2,770,709✔
102
  if (NULL != pVal) {
2,770,717✔
103
    return funcMgtBuiltins[*(int32_t*)pVal].type;
2,770,500✔
104
  }
105
  return FUNCTION_TYPE_UDF;
217✔
106
}
107

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

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

123
  const char* name = funcMgtBuiltins[funcId].name;
107,796✔
124
  if ((strcmp(name, "_group_key") == 0) || (strcmp(name, "_select_value") == 0)) {
107,796✔
125
    return FUNC_DATA_REQUIRED_NOT_LOAD;
3,562✔
126
    ;
127
  }
128

129
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
104,234✔
130
    return FUNC_DATA_REQUIRED_DATA_LOAD;
101,787✔
131
  } else {
132
    return funcMgtBuiltins[funcId].dynDataRequiredFunc(pRes, pBlockInfo);
2,447✔
133
  }
134
}
135

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

170
bool fmIsAggFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_AGG_FUNC); }
11,206,504✔
171

172
bool fmIsScalarFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCALAR_FUNC); }
11,619,352✔
173

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

176
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
7,806,285✔
177

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

180
bool fmIsDateTimeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_DATETIME_FUNC); }
2,774,034✔
181

182
bool fmIsPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PSEUDO_COLUMN_FUNC); }
10,588,937✔
183

184
bool fmIsScanPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCAN_PC_FUNC); }
9,626,040✔
185

186
bool fmIsWindowPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_WINDOW_PC_FUNC); }
6,916,056✔
187

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

190
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
8,796,603✔
191

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

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

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

202
bool fmIsRepeatScanFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_REPEAT_SCAN_FUNC); }
5,512,712✔
203

204
bool fmIsUserDefinedFunc(int32_t funcId) { return funcId > FUNC_UDF_ID_START; }
178,944,087✔
205

206
bool fmIsForbidFillFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_FILL_FUNC); }
2,783,675✔
207

208
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
771,053✔
209

210
bool fmIsForbidStreamFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_STREAM_FUNC); }
2,774,907✔
211

212
bool fmIsSystemInfoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SYSTEM_INFO_FUNC); }
2,801,831✔
213

214
bool fmIsImplicitTsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IMPLICIT_TS_FUNC); }
31,569,879✔
215

216
bool fmIsClientPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CLIENT_PC_FUNC); }
2,799,263✔
217

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

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

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

224
bool fmIsForbidSysTableFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_SYSTABLE_FUNC); }
2,774,892✔
225

226
bool fmIsInterpFunc(int32_t funcId) {
5,580,511✔
227
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
5,580,511!
228
    return false;
407✔
229
  }
230
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
5,580,104✔
231
}
232

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

235
bool fmIsForecastFunc(int32_t funcId) {
5,422,760✔
236
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
5,422,760!
237
    return false;
403✔
238
  }
239
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
5,422,357✔
240
}
241

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

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

251
bool fmIsNotNullOutputFunc(int32_t funcId) {
2,547,174✔
252
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,547,174!
253
    return false;
17✔
254
  }
255
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
5,026,046✔
256
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
2,478,889✔
257
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
2,463,743✔
258
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
2,457,600✔
259
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
2,407,995✔
260
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
2,395,746✔
261
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
2,391,099✔
262
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
1,982,385✔
263
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
6,948,109✔
264
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
1,922,063✔
265
}
266

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

274
bool fmIsGroupKeyFunc(int32_t funcId) {
835,812✔
275
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
835,812!
276
    return false;
×
277
  }
278
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
835,857✔
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) {
32,522✔
289
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
32,522!
290
    return false;
×
291
  }
292
  return FUNCTION_TYPE_ELAPSED == funcMgtBuiltins[funcId].type;
32,522✔
293
}
294

295
bool fmIsBlockDistFunc(int32_t funcId) {
2,774,034✔
296
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,774,034!
297
    return false;
196✔
298
  }
299
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
2,773,838✔
300
}
301

302
bool fmIsDBUsageFunc(int32_t funcId) {
2,774,029✔
303
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,774,029!
304
    return false;
197✔
305
  }
306
  return FUNCTION_TYPE_DB_USAGE == funcMgtBuiltins[funcId].type;
2,773,832✔
307
}
308

309
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
2,962,714✔
310

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

313
void fmFuncMgtDestroy() {
2,016✔
314
  void* m = gFunMgtService.pFuncNameHashTable;
2,016✔
315
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
2,016!
316
    taosHashCleanup(m);
2,016✔
317
  }
318
}
2,016✔
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) {
357,098✔
358
  bool res = false;
357,098✔
359
  switch (funcMgtBuiltins[funcId].type) {
357,098✔
360
    case FUNCTION_TYPE_MAX:
127,140✔
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;
127,140✔
370
      break;
127,140✔
371
    default:
229,958✔
372
      break;
229,958✔
373
  }
374
  return res;
357,098✔
375
}
376

377
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
62,010✔
378
  SNode* pNode;
379
  FOREACH(pNode, pFunc->pParameterList) {
62,640✔
380
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
3,754✔
381
      return false;
3,124✔
382
    }
383
  }
384
  return true;
58,886✔
385
}
386

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

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

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

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

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

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

431
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
1,020,877✔
432
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
1,020,877✔
433

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

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

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

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

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

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

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

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

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

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

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

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

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

563
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
357,098✔
564
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
357,098!
565
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
306,681✔
566
  }
567
  if (TSDB_CODE_SUCCESS == code) {
357,098!
568
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
357,098✔
569
  }
570

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

577
  return code;
357,098✔
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,064✔
590
  if (funcMgtBuiltins[pFunc->funcId].pStateFunc) {
33,064!
591
    SNodeList* pParams = NULL;
33,064✔
592
    int32_t    code = nodesCloneList(pFunc->pParameterList, &pParams);
33,064✔
593
    if (!pParams) return code;
33,064!
594
    code = createFunction(funcMgtBuiltins[pFunc->funcId].pStateFunc, pParams, pStateFunc);
33,064✔
595
    if (TSDB_CODE_SUCCESS != code) {
33,064!
596
      nodesDestroyList(pParams);
×
597
      return code;
×
598
    }
599
    tstrncpy((*pStateFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
33,064✔
600
    tstrncpy((*pStateFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
33,064✔
601
  }
602
  return TSDB_CODE_SUCCESS;
33,064✔
603
}
604

605
bool fmIsTSMASupportedFunc(func_id_t funcId) {
724,612✔
606
  return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC) &&
1,425,925✔
607
         !isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_STREAM_FUNC);
701,313!
608
}
609

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

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

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

676
int32_t fmGetFuncId(const char* name) {
85,495✔
677
  if (NULL != gFunMgtService.pFuncNameHashTable) {
85,495!
678
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, name, strlen(name));
85,495✔
679
    if (NULL != pVal) {
85,495!
680
      return *(int32_t*)pVal;
85,495✔
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,725,478✔
693
  const SBuiltinFuncDefinition* pFunc = &funcMgtBuiltins[funcId];
16,725,478✔
694
  const SBuiltinFuncDefinition* pStateFunc = &funcMgtBuiltins[stateFuncId];
16,725,478✔
695
  if (!pFunc->pStateFunc) {
16,725,478!
696
    return false;
×
697
  }
698
  if (strcmp(pFunc->pStateFunc, pStateFunc->name) == 0) return true;
16,725,478✔
699
  int32_t stateMergeFuncId = fmGetFuncId(pFunc->pStateFunc);
83,721✔
700
  if (stateMergeFuncId == -1) {
83,721!
701
    return false;
×
702
  }
703
  const SBuiltinFuncDefinition* pStateMergeFunc = &funcMgtBuiltins[stateMergeFuncId];
83,721✔
704
  return strcmp(pStateFunc->name, pStateMergeFunc->pMergeFunc) == 0;
83,721✔
705
}
706

707
bool fmIsCountLikeFunc(int32_t funcId) {
934,158✔
708
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
934,158✔
709
}
710

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