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

taosdata / TDengine / #3534

21 Nov 2024 07:36AM UTC coverage: 60.825% (+2.0%) from 58.848%
#3534

push

travis-ci

web-flow
Merge pull request #28810 from taosdata/ehn/add-sync-heartbeat-sent-time-to-log

ehn:add-sync-heartbeat-sent-time-to-log

120023 of 252376 branches covered (47.56%)

Branch coverage included in aggregate %.

43 of 47 new or added lines in 3 files covered. (91.49%)

2254 existing lines in 162 files now uncovered.

200876 of 275203 relevant lines covered (72.99%)

16110754.39 hits per line

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

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

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

51
static bool isSpecificClassifyFunc(int32_t funcId, uint64_t classification) {
322,683,116✔
52
  if (fmIsUserDefinedFunc(funcId)) {
322,683,116✔
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);
117,320✔
56
  }
57
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
322,577,187!
58
    return false;
85,670,858✔
59
  }
60
  return FUNC_MGT_TEST_MASK(funcMgtBuiltins[funcId].classification, classification);
236,906,329✔
61
}
62

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

68
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
5,003,736✔
69
  if (NULL != gFunMgtService.pFuncNameHashTable) {
5,003,736✔
70
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
4,998,904✔
71
    if (NULL != pVal) {
4,998,954✔
72
      pFunc->funcId = *(int32_t*)pVal;
4,997,359✔
73
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
4,997,359✔
74
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
4,997,359✔
75
    }
76
    return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
1,595✔
77
  }
78
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
92,643!
79
    if (0 == strcmp(funcMgtBuiltins[i].name, pFunc->functionName)) {
92,676✔
80
      pFunc->funcId = i;
4,865✔
81
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
4,865✔
82
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
4,865✔
83
    }
84
  }
85
  return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
×
86
}
87

88
EFuncReturnRows fmGetFuncReturnRows(SFunctionNode* pFunc) {
62,798✔
89
  if (NULL != funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc) {
62,798✔
90
    return funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc(pFunc);
22,021✔
91
  }
92
  return (fmIsIndefiniteRowsFunc(pFunc->funcId) || fmIsMultiRowsFunc(pFunc->funcId)) ? FUNC_RETURN_ROWS_INDEFINITE
40,777!
93
                                                                                     : FUNC_RETURN_ROWS_NORMAL;
40,777!
94
}
95

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

100
EFunctionType fmGetFuncType(const char* pFunc) {
3,245,581✔
101
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
3,245,581✔
102
  if (NULL != pVal) {
3,245,606✔
103
    return funcMgtBuiltins[*(int32_t*)pVal].type;
3,244,010✔
104
  }
105
  return FUNCTION_TYPE_UDF;
1,596✔
106
}
107

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

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

123
  const char* name = funcMgtBuiltins[funcId].name;
571,697✔
124
  if ((strcmp(name, "_group_key") == 0) || (strcmp(name, "_select_value") == 0)) {
571,697✔
125
    return FUNC_DATA_REQUIRED_NOT_LOAD;;
81,231✔
126
  }
127

128
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
490,466✔
129
    return FUNC_DATA_REQUIRED_DATA_LOAD;
428,655✔
130
  } else {
131
    return funcMgtBuiltins[funcId].dynDataRequiredFunc(pRes, pBlockInfo);
61,811✔
132
  }
133
}
134

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

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

160
int32_t fmGetScalarFuncExecFuncs(int32_t funcId, SScalarFuncExecFuncs* pFpSet) {
13,640,547✔
161
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
13,640,547!
UNCOV
162
    return TSDB_CODE_FAILED;
×
163
  }
164
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
13,641,283✔
165
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
13,641,283✔
166
  return TSDB_CODE_SUCCESS;
13,641,283✔
167
}
168

169
bool fmIsAggFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_AGG_FUNC); }
22,605,206✔
170

171
bool fmIsScalarFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCALAR_FUNC); }
14,636,246✔
172

173
bool fmIsVectorFunc(int32_t funcId) { return !fmIsScalarFunc(funcId) && !fmIsPseudoColumnFunc(funcId); }
6,754,630✔
174

175
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
31,094,222✔
176

177
bool fmIsTimelineFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TIMELINE_FUNC); }
6,487,527✔
178

179
bool fmIsDateTimeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_DATETIME_FUNC); }
3,339,330✔
180

181
bool fmIsPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PSEUDO_COLUMN_FUNC); }
33,917,145✔
182

183
bool fmIsScanPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCAN_PC_FUNC); }
24,276,000✔
184

185
bool fmIsWindowPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_WINDOW_PC_FUNC); }
15,215,017✔
186

187
bool fmIsWindowClauseFunc(int32_t funcId) { return fmIsAggFunc(funcId) || fmIsWindowPseudoColumnFunc(funcId); }
458,901✔
188

189
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
15,562,736✔
190

191

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

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

200
bool fmIsMultiResFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_RES_FUNC); }
6,697,360✔
201

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

204
bool fmIsUserDefinedFunc(int32_t funcId) { return funcId > FUNC_UDF_ID_START; }
367,324,496✔
205

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

208
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
33,890,948✔
209

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

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

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

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

218
bool fmIsMultiRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_ROWS_FUNC); }
6,508,994✔
219

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

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

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

226
bool fmIsInterpFunc(int32_t funcId) {
6,600,712✔
227
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
6,600,712!
228
    return false;
3,093✔
229
  }
230
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
6,597,619✔
231
}
232

233
bool fmIsInterpPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERP_PC_FUNC); }
6,557,340✔
234

235
bool fmIsForecastFunc(int32_t funcId) {
6,447,883✔
236
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
6,447,883!
237
    return false;
3,101✔
238
  }
239
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
6,444,782✔
240
}
241

242
bool fmIsForecastPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORECAST_PC_FUNC); }
6,557,365✔
243

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

251
bool fmIsNotNullOutputFunc(int32_t funcId) {
10,025,217✔
252
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
10,025,217!
UNCOV
253
    return false;
×
254
  }
255
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
19,796,798✔
256
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
9,771,335✔
257
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
9,128,501✔
258
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
8,809,589✔
259
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
8,598,196✔
260
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
8,361,368✔
261
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
8,245,246✔
262
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
7,594,535✔
263
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
27,328,756✔
264
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
7,531,958✔
265
}
266

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

274
bool fmIsGroupKeyFunc(int32_t funcId) {
445,966,262✔
275
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
445,966,262!
276
    return false;
×
277
  }
278
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
445,983,623✔
279
}
280

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

288
bool fmIsElapsedFunc(int32_t funcId) {
21,304,157✔
289
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
21,304,157!
290
    return false;
24✔
291
  }
292
  return FUNCTION_TYPE_ELAPSED == funcMgtBuiltins[funcId].type;
21,304,133✔
293
}
294

295
bool fmIsBlockDistFunc(int32_t funcId) {
3,339,249✔
296
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
3,339,249!
297
    return false;
1,546✔
298
  }
299
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
3,337,703✔
300
}
301

302
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
3,765,519✔
303

304
bool fmIsIgnoreNullFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IGNORE_NULL_FUNC); }
2,460✔
305

306
void fmFuncMgtDestroy() {
4,063✔
307
  void* m = gFunMgtService.pFuncNameHashTable;
4,063✔
308
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
4,063!
309
    taosHashCleanup(m);
4,063✔
310
  }
311
}
4,063✔
312

313
#ifdef BUILD_NO_CALL
314
int32_t fmSetInvertFunc(int32_t funcId, SFuncExecFuncs* pFpSet) {
315
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
316
    return TSDB_CODE_FAILED;
317
  }
318
  pFpSet->process = funcMgtBuiltins[funcId].invertFunc;
319
  return TSDB_CODE_SUCCESS;
320
}
321

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

330
bool fmIsInvertible(int32_t funcId) {
331
  bool res = false;
332
  switch (funcMgtBuiltins[funcId].type) {
333
    case FUNCTION_TYPE_COUNT:
334
    case FUNCTION_TYPE_SUM:
335
    case FUNCTION_TYPE_STDDEV:
336
    case FUNCTION_TYPE_AVG:
337
    case FUNCTION_TYPE_WSTART:
338
    case FUNCTION_TYPE_WEND:
339
    case FUNCTION_TYPE_WDURATION:
340
      res = true;
341
      break;
342
    default:
343
      break;
344
  }
345
  return res;
346
}
347
#endif
348

349
// function has same input/output type
350
bool fmIsSameInOutType(int32_t funcId) {
356,577✔
351
  bool res = false;
356,577✔
352
  switch (funcMgtBuiltins[funcId].type) {
356,577✔
353
    case FUNCTION_TYPE_MAX:
124,637✔
354
    case FUNCTION_TYPE_MIN:
355
    case FUNCTION_TYPE_TOP:
356
    case FUNCTION_TYPE_BOTTOM:
357
    case FUNCTION_TYPE_FIRST:
358
    case FUNCTION_TYPE_LAST:
359
    case FUNCTION_TYPE_SAMPLE:
360
    case FUNCTION_TYPE_TAIL:
361
    case FUNCTION_TYPE_UNIQUE:
362
      res = true;
124,637✔
363
      break;
124,637✔
364
    default:
231,940✔
365
      break;
231,940✔
366
  }
367
  return res;
356,577✔
368
}
369

370
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
71,453✔
371
  SNode* pNode;
372
  FOREACH(pNode, pFunc->pParameterList) {
72,083✔
373
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
3,724✔
374
      return false;
3,094✔
375
    }
376
  }
377
  return true;
68,359✔
378
}
379

380
bool fmIsSkipScanCheckFunc(int32_t funcId) {
262,596✔
381
  return isSpecificClassifyFunc(funcId, FUNC_MGT_SKIP_SCAN_CHECK_FUNC);
262,596✔
382
}
383

384
bool fmIsPrimaryKeyFunc(int32_t funcId) {
2,990,004✔
385
  return isSpecificClassifyFunc(funcId, FUNC_MGT_PRIMARY_KEY_FUNC);
2,990,004✔
386
}
387
void getLastCacheDataType(SDataType* pType, int32_t pkBytes) {
33,897✔
388
  //TODO: do it later.
389
  pType->bytes = getFirstLastInfoSize(pType->bytes, pkBytes) + VARSTR_HEADER_SIZE;
33,897✔
390
  pType->type = TSDB_DATA_TYPE_BINARY;
33,897✔
391
}
33,897✔
392

393
static int32_t getFuncInfo(SFunctionNode* pFunc) {
1,129,975✔
394
  char msg[128] = {0};
1,129,975✔
395
  return fmGetFuncInfo(pFunc, msg, sizeof(msg));
1,129,975✔
396
}
397

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

415
int32_t createFunctionWithSrcFunc(const char* pName, const SFunctionNode* pSrcFunc, SNodeList* pParameterList, SFunctionNode** ppFunc) {
1,019,346✔
416
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
1,019,346✔
417
  if (NULL == *ppFunc) {
1,019,345!
418
    return code;
×
419
  }
420

421
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
1,019,345✔
422
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
1,019,345✔
423

424
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
1,019,345✔
425
  (*ppFunc)->pParameterList = pParameterList;
1,019,345✔
426
  code = getFuncInfo((*ppFunc));
1,019,345✔
427
  if (TSDB_CODE_SUCCESS != code) {
1,019,348!
428
    (*ppFunc)->pParameterList = NULL;
×
429
    nodesDestroyNode((SNode*)*ppFunc);
×
430
    *ppFunc = NULL;
×
431
    return code;
×
432
  }
433
  return code;
1,019,348✔
434
}
435

436
static int32_t createColumnByFunc(const SFunctionNode* pFunc, SColumnNode** ppCol) {
662,770✔
437
  int32_t code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)ppCol);
662,770✔
438
  if (NULL == *ppCol) {
662,770!
439
    return code;
×
440
  }
441
  tstrncpy((*ppCol)->colName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
662,770✔
442
  (*ppCol)->node.resType = pFunc->node.resType;
662,770✔
443
  return TSDB_CODE_SUCCESS;
662,770✔
444
}
445

446
bool fmIsDistExecFunc(int32_t funcId) {
1,718,939✔
447
  if (fmIsUserDefinedFunc(funcId)) {
1,718,939✔
448
    return false;
890✔
449
  }
450
  if (!fmIsVectorFunc(funcId)) {
1,718,165!
451
    return true;
×
452
  }
453
  return (NULL != funcMgtBuiltins[funcId].pPartialFunc && NULL != funcMgtBuiltins[funcId].pMergeFunc);
1,718,197!
454
}
455

456
static int32_t createPartialFunction(const SFunctionNode* pSrcFunc, SFunctionNode** pPartialFunc) {
356,577✔
457
  SNodeList* pParameterList = NULL;
356,577✔
458
  int32_t code = nodesCloneList(pSrcFunc->pParameterList, &pParameterList);
356,577✔
459
  if (NULL == pParameterList) {
356,576!
460
    return code;
×
461
  }
462
  code =
463
      createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pPartialFunc, pSrcFunc, pParameterList, pPartialFunc);
356,576✔
464
  if (TSDB_CODE_SUCCESS != code) {
356,577!
465
    nodesDestroyList(pParameterList);
×
466
    return code;
×
467
  }
468
  (*pPartialFunc)->hasOriginalFunc = true;
356,577✔
469
  (*pPartialFunc)->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
356,577✔
470
  char name[TSDB_FUNC_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_POINTER_PRINT_BYTES + 1] = {0};
356,577✔
471
  
472
  int32_t len = tsnprintf(name, sizeof(name), "%s.%p", (*pPartialFunc)->functionName, pSrcFunc);
356,577✔
473
  if (taosHashBinary(name, len) < 0) {
356,577!
474
    return TSDB_CODE_FAILED;
×
475
  }
476
  tstrncpy((*pPartialFunc)->node.aliasName, name, TSDB_COL_NAME_LEN);
356,577✔
477
  return TSDB_CODE_SUCCESS;
356,577✔
478
}
479

480
static int32_t createMergeFuncPara(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
662,770✔
481
                                   SNodeList** pParameterList) {
482
  SNode *pRes = NULL;
662,770✔
483
  int32_t code = createColumnByFunc(pPartialFunc, (SColumnNode**)&pRes);
662,770✔
484
  if (TSDB_CODE_SUCCESS != code) {
662,770!
485
    return code;
×
486
  }
487
  if (NULL != funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc) {
662,770✔
488
    return funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc(pSrcFunc->pParameterList, pRes, pParameterList);
4,798✔
489
  } else {
490
    return nodesListMakeStrictAppend(pParameterList, pRes);
657,972✔
491
  }
492
}
493

494
static int32_t createMidFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
306,194✔
495
                                   SFunctionNode** pMidFunc) {
496
  SNodeList*     pParameterList = NULL;
306,194✔
497
  SFunctionNode* pFunc = NULL;
306,194✔
498

499
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
306,194✔
500
  if (TSDB_CODE_SUCCESS == code) {
306,194!
501
    if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){
306,194✔
502
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pSrcFunc, pParameterList, &pFunc);
18,619✔
503
    }else{
504
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
287,575✔
505
    }
506
  }
507
  if (TSDB_CODE_SUCCESS == code) {
306,194!
508
    tstrncpy(pFunc->node.aliasName, pPartialFunc->node.aliasName, TSDB_COL_NAME_LEN);
306,194✔
509
  }
510

511
  if (TSDB_CODE_SUCCESS == code) {
306,194!
512
    *pMidFunc = pFunc;
306,194✔
513
  } else {
514
    nodesDestroyList(pParameterList);
×
515
  }
516
  return code;
306,194✔
517
}
518

519
static int32_t createMergeFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
356,577✔
520
                                   SFunctionNode** pMergeFunc) {
521
  SNodeList*     pParameterList = NULL;
356,577✔
522
  SFunctionNode* pFunc = NULL;
356,577✔
523

524
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
356,577✔
525
  if (TSDB_CODE_SUCCESS == code) {
356,577!
526
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
356,577✔
527
  }
528
  if (TSDB_CODE_SUCCESS == code) {
356,577!
529
    pFunc->hasOriginalFunc = true;
356,577✔
530
    pFunc->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
356,577✔
531
    // overwrite function restype set by translate function
532
    if (fmIsSameInOutType(pSrcFunc->funcId)) {
356,577✔
533
      pFunc->node.resType = pSrcFunc->node.resType;
124,637✔
534
    }
535
    tstrncpy(pFunc->node.aliasName, pSrcFunc->node.aliasName, TSDB_COL_NAME_LEN);
356,577✔
536
  }
537

538
  if (TSDB_CODE_SUCCESS == code) {
356,577!
539
    *pMergeFunc = pFunc;
356,577✔
540
  } else {
541
    nodesDestroyList(pParameterList);
×
542
  }
543
  return code;
356,577✔
544
}
545

546
int32_t fmGetDistMethod(const SFunctionNode* pFunc, SFunctionNode** pPartialFunc, SFunctionNode** pMidFunc, SFunctionNode** pMergeFunc) {
356,577✔
547
  if (!fmIsDistExecFunc(pFunc->funcId)) {
356,577!
548
    return TSDB_CODE_FAILED;
×
549
  }
550

551
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
356,577✔
552
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
356,577!
553
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
306,194✔
554
  }
555
  if (TSDB_CODE_SUCCESS == code) {
356,577!
556
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
356,577✔
557
  }
558

559
  if (TSDB_CODE_SUCCESS != code) {
356,577!
560
    nodesDestroyNode((SNode*)*pPartialFunc);
×
561
    if (pMidFunc) nodesDestroyNode((SNode*)*pMidFunc);
×
562
    nodesDestroyNode((SNode*)*pMergeFunc);
×
563
  }
564

565
  return code;
356,577✔
566
}
567

568
char* fmGetFuncName(int32_t funcId) {
2✔
569
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2!
570
    return taosStrdup("invalid function");
×
571
  }
572
  return  taosStrdup(funcMgtBuiltins[funcId].name);
2✔
573
}
574

575
/// @param [out] pStateFunc, not changed if error occured or no need to create state func
576
/// @retval 0 for succ, otherwise err occured
577
static int32_t fmCreateStateFunc(const SFunctionNode* pFunc, SFunctionNode** pStateFunc) {
33,072✔
578
  if (funcMgtBuiltins[pFunc->funcId].pStateFunc) {
33,072!
579
    SNodeList* pParams = NULL;
33,072✔
580
    int32_t code = nodesCloneList(pFunc->pParameterList, &pParams);
33,072✔
581
    if (!pParams) return code;
33,072!
582
    code = createFunction(funcMgtBuiltins[pFunc->funcId].pStateFunc, pParams, pStateFunc);
33,072✔
583
    if (TSDB_CODE_SUCCESS != code) {
33,072!
584
      nodesDestroyList(pParams);
×
585
      return code;
×
586
    }
587
    tstrncpy((*pStateFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
33,072✔
588
    tstrncpy((*pStateFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
33,072✔
589
  }
590
  return TSDB_CODE_SUCCESS;
33,072✔
591
}
592

593
bool fmIsTSMASupportedFunc(func_id_t funcId) {
650,644✔
594
  return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC) &&
1,278,325✔
595
         !isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_STREAM_FUNC);
627,681!
596
}
597

598
int32_t fmCreateStateFuncs(SNodeList* pFuncs) {
152✔
599
  int32_t code;
600
  SNode*  pNode;
601
  char    buf[128] = {0};
152✔
602
  FOREACH(pNode, pFuncs) {
33,224!
603
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
33,072✔
604
    code = fmGetFuncInfo(pFunc, buf, 128);
33,072✔
605
    if (code) break;
33,072!
606
    if (fmIsTSMASupportedFunc(pFunc->funcId)) {
33,072!
607
      SFunctionNode* pNewFunc = NULL;
33,072✔
608
      code = fmCreateStateFunc(pFunc, &pNewFunc);
33,072✔
609
      if (code) {
33,072!
610
        // error
611
        break;
×
612
      } else if (!pNewFunc) {
33,072!
613
        // no need state func
614
        continue;
×
615
      } else {
616
        REPLACE_NODE(pNewFunc);
33,072✔
617
        nodesDestroyNode(pNode);
33,072✔
618
      }
619
    }
620
  }
621
  return code;
152✔
622
}
623

624
static int32_t fmCreateStateMergeFunc(SFunctionNode* pFunc, SFunctionNode** pStateMergeFunc) {
428✔
625
  if (funcMgtBuiltins[pFunc->funcId].pMergeFunc) {
428✔
626
    SNodeList* pParams = NULL;
416✔
627
    int32_t code = nodesCloneList(pFunc->pParameterList, &pParams);
416✔
628
    if (!pParams) return code;
416!
629
    code = createFunction(funcMgtBuiltins[pFunc->funcId].pMergeFunc, pParams, pStateMergeFunc);
416✔
630
    if (TSDB_CODE_SUCCESS != code) {
416!
631
      nodesDestroyList(pParams);
×
632
      return code;
×
633
    }
634
    tstrncpy((*pStateMergeFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
416✔
635
    tstrncpy((*pStateMergeFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
416✔
636
  }
637
  return TSDB_CODE_SUCCESS;
428✔
638
}
639

640
int32_t fmCreateStateMergeFuncs(SNodeList* pFuncs) {
92✔
641
  int32_t code;
642
  SNode*  pNode;
643
  char    buf[128] = {0};
92✔
644
  FOREACH(pNode, pFuncs) {
520!
645
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
428✔
646
    if (fmIsTSMASupportedFunc(pFunc->funcId)) {
428!
647
      SFunctionNode* pNewFunc = NULL;
428✔
648
      code = fmCreateStateMergeFunc(pFunc, &pNewFunc);
428✔
649
      if (code) {
428!
650
        // error
651
        break;
×
652
      } else if (!pNewFunc) {
428✔
653
        // no state merge func
654
        continue;
12✔
655
      } else {
656
        REPLACE_NODE(pNewFunc);
416✔
657
        nodesDestroyNode(pNode);
416✔
658
      }
659
    }
660
  }
661
  return code;
92✔
662
}
663

664
int32_t fmGetFuncId(const char* name) {
86,039✔
665
  if (NULL != gFunMgtService.pFuncNameHashTable) {
86,039!
666
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, name, strlen(name));
86,039✔
667
    if (NULL != pVal) {
86,039!
668
      return *(int32_t*)pVal;
86,039✔
669
    }
670
    return -1;
×
671
  }
672
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
×
673
    if (0 == strcmp(funcMgtBuiltins[i].name, name)) {
×
674
      return i;
×
675
    }
676
  }
677
  return -1;
×
678
}
679

680
bool fmIsMyStateFunc(int32_t funcId, int32_t stateFuncId) {
16,729,211✔
681
  const SBuiltinFuncDefinition* pFunc = &funcMgtBuiltins[funcId];
16,729,211✔
682
  const SBuiltinFuncDefinition* pStateFunc = &funcMgtBuiltins[stateFuncId];
16,729,211✔
683
  if (!pFunc->pStateFunc) {
16,729,211!
684
    return false;
×
685
  }
686
  if (strcmp(pFunc->pStateFunc, pStateFunc->name) == 0) return true;
16,729,211✔
687
  int32_t stateMergeFuncId = fmGetFuncId(pFunc->pStateFunc);
84,441✔
688
  if (stateMergeFuncId == -1) {
84,441!
689
    return false;
×
690
  }
691
  const SBuiltinFuncDefinition* pStateMergeFunc = &funcMgtBuiltins[stateMergeFuncId];
84,441✔
692
  return strcmp(pStateFunc->name, pStateMergeFunc->pMergeFunc) == 0;
84,441✔
693
}
694

695
bool fmIsCountLikeFunc(int32_t funcId) {
1,481,869✔
696
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
1,481,869✔
697
}
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

© 2025 Coveralls, Inc