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

taosdata / TDengine / #3519

05 Nov 2024 11:19AM UTC coverage: 57.706% (+8.4%) from 49.32%
#3519

push

travis-ci

web-flow
Merge pull request #28652 from taosdata/fix/3_liaohj

refactor: always successfully put the retrieve msg

109445 of 245179 branches covered (44.64%)

Branch coverage included in aggregate %.

187435 of 269288 relevant lines covered (69.6%)

12869818.21 hits per line

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

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

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

51
static bool isSpecificClassifyFunc(int32_t funcId, uint64_t classification) {
265,605,288✔
52
  if (fmIsUserDefinedFunc(funcId)) {
265,605,288✔
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);
58,086✔
56
  }
57
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
265,552,105!
58
    return false;
60,095,788✔
59
  }
60
  return FUNC_MGT_TEST_MASK(funcMgtBuiltins[funcId].classification, classification);
205,456,317✔
61
}
62

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

68
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
5,438,200✔
69
  if (NULL != gFunMgtService.pFuncNameHashTable) {
5,438,200✔
70
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
5,435,269✔
71
    if (NULL != pVal) {
5,435,360✔
72
      pFunc->funcId = *(int32_t*)pVal;
5,433,765✔
73
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
5,433,765✔
74
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
5,433,765✔
75
    }
76
    return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
1,595✔
77
  }
78
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
57,313!
79
    if (0 == strcmp(funcMgtBuiltins[i].name, pFunc->functionName)) {
57,331✔
80
      pFunc->funcId = i;
2,949✔
81
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
2,949✔
82
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
2,949✔
83
    }
84
  }
85
  return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
×
86
}
87

88
EFuncReturnRows fmGetFuncReturnRows(SFunctionNode* pFunc) {
62,586✔
89
  if (NULL != funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc) {
62,586✔
90
    return funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc(pFunc);
21,822✔
91
  }
92
  return (fmIsIndefiniteRowsFunc(pFunc->funcId) || fmIsMultiRowsFunc(pFunc->funcId)) ? FUNC_RETURN_ROWS_INDEFINITE
40,764!
93
                                                                                     : FUNC_RETURN_ROWS_NORMAL;
40,764!
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,492,563✔
101
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
3,492,563✔
102
  if (NULL != pVal) {
3,492,596✔
103
    return funcMgtBuiltins[*(int32_t*)pVal].type;
3,490,999✔
104
  }
105
  return FUNCTION_TYPE_UDF;
1,597✔
106
}
107

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

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

123
  const char* name = funcMgtBuiltins[funcId].name;
321,574✔
124
  if ((strcmp(name, "_group_key") == 0) || (strcmp(name, "_select_value") == 0)) {
321,574✔
125
    return FUNC_DATA_REQUIRED_NOT_LOAD;;
103,748✔
126
  }
127

128
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
217,826✔
129
    return FUNC_DATA_REQUIRED_DATA_LOAD;
192,376✔
130
  } else {
131
    return funcMgtBuiltins[funcId].dynDataRequiredFunc(pRes, pBlockInfo);
25,450✔
132
  }
133
}
134

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

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

160
int32_t fmGetScalarFuncExecFuncs(int32_t funcId, SScalarFuncExecFuncs* pFpSet) {
5,661,087✔
161
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
5,661,087!
162
    return TSDB_CODE_FAILED;
352✔
163
  }
164
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
5,662,476✔
165
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
5,662,476✔
166
  return TSDB_CODE_SUCCESS;
5,662,476✔
167
}
168

169
bool fmIsAggFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_AGG_FUNC); }
17,179,713✔
170

171
bool fmIsScalarFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCALAR_FUNC); }
15,441,385✔
172

173
bool fmIsVectorFunc(int32_t funcId) { return !fmIsScalarFunc(funcId) && !fmIsPseudoColumnFunc(funcId); }
7,348,699✔
174

175
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
25,633,413✔
176

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

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

181
bool fmIsPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PSEUDO_COLUMN_FUNC); }
26,786,789✔
182

183
bool fmIsScanPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCAN_PC_FUNC); }
19,535,830✔
184

185
bool fmIsWindowPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_WINDOW_PC_FUNC); }
12,561,736✔
186

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

189
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
12,111,517✔
190

191

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

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

200
bool fmIsMultiResFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_RES_FUNC); }
7,106,035✔
201

202
bool fmIsRepeatScanFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_REPEAT_SCAN_FUNC); }
7,052,359✔
203

204
bool fmIsUserDefinedFunc(int32_t funcId) { return funcId > FUNC_UDF_ID_START; }
291,600,633✔
205

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

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

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

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

214
bool fmIsImplicitTsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IMPLICIT_TS_FUNC); }
32,760,156✔
215

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

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

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

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

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

226
bool fmIsInterpFunc(int32_t funcId) {
7,076,436✔
227
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
7,076,436!
228
    return false;
3,089✔
229
  }
230
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
7,073,347✔
231
}
232

233
bool fmIsInterpPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERP_PC_FUNC); }
7,043,049✔
234

235
bool fmIsForecastFunc(int32_t funcId) {
6,931,251✔
236
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
6,931,251!
237
    return false;
2,990✔
238
  }
239
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
6,928,261✔
240
}
241

242
bool fmIsForecastPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORECAST_PC_FUNC); }
7,043,231✔
243

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

251
bool fmIsNotNullOutputFunc(int32_t funcId) {
7,003,797✔
252
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
7,003,797!
253
    return false;
×
254
  }
255
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
13,840,827✔
256
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
6,835,391✔
257
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
6,186,392✔
258
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
5,860,766✔
259
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
5,710,383✔
260
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
5,484,519✔
261
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
5,371,019✔
262
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
5,132,049✔
263
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
18,948,193✔
264
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
5,107,366✔
265
}
266

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

274
bool fmIsGroupKeyFunc(int32_t funcId) {
409,560,355✔
275
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
409,560,355!
276
    return false;
×
277
  }
278
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
409,591,167✔
279
}
280

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

288
bool fmIsElapsedFunc(int32_t funcId) {
16,132,523✔
289
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
16,132,523!
290
    return false;
4✔
291
  }
292
  return FUNCTION_TYPE_ELAPSED == funcMgtBuiltins[funcId].type;
16,132,519✔
293
}
294

295
bool fmIsBlockDistFunc(int32_t funcId) {
3,580,670✔
296
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
3,580,670!
297
    return false;
1,428✔
298
  }
299
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
3,579,242✔
300
}
301

302
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
4,113,489✔
303

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

306
void fmFuncMgtDestroy() {
4,096✔
307
  void* m = gFunMgtService.pFuncNameHashTable;
4,096✔
308
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
4,096!
309
    taosHashCleanup(m);
4,096✔
310
  }
311
}
4,096✔
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) {
397,321✔
351
  bool res = false;
397,321✔
352
  switch (funcMgtBuiltins[funcId].type) {
397,321✔
353
    case FUNCTION_TYPE_MAX:
138,039✔
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;
138,039✔
363
      break;
138,039✔
364
    default:
259,282✔
365
      break;
259,282✔
366
  }
367
  return res;
397,321✔
368
}
369

370
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
89,358✔
371
  SNode* pNode;
372
  FOREACH(pNode, pFunc->pParameterList) {
89,988✔
373
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
3,756✔
374
      return false;
3,126✔
375
    }
376
  }
377
  return true;
86,232✔
378
}
379

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

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

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

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

415
static int32_t createColumnByFunc(const SFunctionNode* pFunc, SColumnNode** ppCol) {
745,002✔
416
  int32_t code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)ppCol);
745,002✔
417
  if (NULL == *ppCol) {
745,002!
418
    return code;
×
419
  }
420
  tstrncpy((*ppCol)->colName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
745,002✔
421
  (*ppCol)->node.resType = pFunc->node.resType;
745,002✔
422
  return TSDB_CODE_SUCCESS;
745,002✔
423
}
424

425
bool fmIsDistExecFunc(int32_t funcId) {
1,849,281✔
426
  if (fmIsUserDefinedFunc(funcId)) {
1,849,281✔
427
    return false;
889✔
428
  }
429
  if (!fmIsVectorFunc(funcId)) {
1,848,495!
430
    return true;
×
431
  }
432
  return (NULL != funcMgtBuiltins[funcId].pPartialFunc && NULL != funcMgtBuiltins[funcId].pMergeFunc);
1,848,514!
433
}
434

435
static int32_t createPartialFunction(const SFunctionNode* pSrcFunc, SFunctionNode** pPartialFunc) {
397,321✔
436
  SNodeList* pParameterList = NULL;
397,321✔
437
  int32_t code = nodesCloneList(pSrcFunc->pParameterList, &pParameterList);
397,321✔
438
  if (NULL == pParameterList) {
397,321!
439
    return code;
×
440
  }
441
  code = createFunction(funcMgtBuiltins[pSrcFunc->funcId].pPartialFunc, pParameterList,pPartialFunc );
397,321✔
442
  if (TSDB_CODE_SUCCESS != code) {
397,321!
443
    nodesDestroyList(pParameterList);
×
444
    return code;
×
445
  }
446
  (*pPartialFunc)->hasOriginalFunc = true;
397,321✔
447
  (*pPartialFunc)->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
397,321✔
448
  char name[TSDB_FUNC_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_POINTER_PRINT_BYTES + 1] = {0};
397,321✔
449
  
450
  int32_t len = tsnprintf(name, sizeof(name), "%s.%p", (*pPartialFunc)->functionName, pSrcFunc);
397,321✔
451
  if (taosHashBinary(name, len) < 0) {
397,321!
452
    return TSDB_CODE_FAILED;
×
453
  }
454
  tstrncpy((*pPartialFunc)->node.aliasName, name, TSDB_COL_NAME_LEN);
397,321✔
455
  (*pPartialFunc)->hasPk = pSrcFunc->hasPk;
397,321✔
456
  (*pPartialFunc)->pkBytes = pSrcFunc->pkBytes;
397,321✔
457
  return TSDB_CODE_SUCCESS;
397,321✔
458
}
459

460
static int32_t createMergeFuncPara(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
745,002✔
461
                                   SNodeList** pParameterList) {
462
  SNode *pRes = NULL;
745,002✔
463
  int32_t code = createColumnByFunc(pPartialFunc, (SColumnNode**)&pRes);
745,002✔
464
  if (TSDB_CODE_SUCCESS != code) {
745,002!
465
    return code;
×
466
  }
467
  if (NULL != funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc) {
745,002✔
468
    return funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc(pSrcFunc->pParameterList, pRes, pParameterList);
4,656✔
469
  } else {
470
    return nodesListMakeStrictAppend(pParameterList, pRes);
740,346✔
471
  }
472
}
473

474
static int32_t createMidFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
347,681✔
475
                                   SFunctionNode** pMidFunc) {
476
  SNodeList*     pParameterList = NULL;
347,681✔
477
  SFunctionNode* pFunc = NULL;
347,681✔
478

479
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
347,681✔
480
  if (TSDB_CODE_SUCCESS == code) {
347,681!
481
    if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){
347,681✔
482
      code = createFunction(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pParameterList, &pFunc);
18,483✔
483
    }else{
484
      code = createFunction(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pParameterList, &pFunc);
329,198✔
485
    }
486
  }
487
  if (TSDB_CODE_SUCCESS == code) {
347,681!
488
    tstrncpy(pFunc->node.aliasName, pPartialFunc->node.aliasName, TSDB_COL_NAME_LEN);
347,681✔
489
  }
490

491
  if (TSDB_CODE_SUCCESS == code) {
347,681!
492
    *pMidFunc = pFunc;
347,681✔
493
  } else {
494
    nodesDestroyList(pParameterList);
×
495
  }
496
  (*pMidFunc)->hasPk = pPartialFunc->hasPk;
347,681✔
497
  (*pMidFunc)->pkBytes = pPartialFunc->pkBytes;
347,681✔
498
  return code;
347,681✔
499
}
500

501
static int32_t createMergeFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
397,321✔
502
                                   SFunctionNode** pMergeFunc) {
503
  SNodeList*     pParameterList = NULL;
397,321✔
504
  SFunctionNode* pFunc = NULL;
397,321✔
505

506
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
397,321✔
507
  if (TSDB_CODE_SUCCESS == code) {
397,321!
508
    code = createFunction(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pParameterList, &pFunc);
397,321✔
509
  }
510
  if (TSDB_CODE_SUCCESS == code) {
397,321!
511
    pFunc->hasOriginalFunc = true;
397,321✔
512
    pFunc->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
397,321✔
513
    // overwrite function restype set by translate function
514
    if (fmIsSameInOutType(pSrcFunc->funcId)) {
397,321✔
515
      pFunc->node.resType = pSrcFunc->node.resType;
138,039✔
516
    }
517
    tstrncpy(pFunc->node.aliasName, pSrcFunc->node.aliasName, TSDB_COL_NAME_LEN);
397,321✔
518
  }
519

520
  if (TSDB_CODE_SUCCESS == code) {
397,321!
521
    *pMergeFunc = pFunc;
397,321✔
522
  } else {
523
    nodesDestroyList(pParameterList);
×
524
  }
525
  (*pMergeFunc)->hasPk = pPartialFunc->hasPk;
397,321✔
526
  (*pMergeFunc)->pkBytes = pPartialFunc->pkBytes;
397,321✔
527
  return code;
397,321✔
528
}
529

530
int32_t fmGetDistMethod(const SFunctionNode* pFunc, SFunctionNode** pPartialFunc, SFunctionNode** pMidFunc, SFunctionNode** pMergeFunc) {
397,321✔
531
  if (!fmIsDistExecFunc(pFunc->funcId)) {
397,321!
532
    return TSDB_CODE_FAILED;
×
533
  }
534

535
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
397,321✔
536
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
397,321!
537
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
347,681✔
538
  }
539
  if (TSDB_CODE_SUCCESS == code) {
397,321!
540
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
397,321✔
541
  }
542

543
  if (TSDB_CODE_SUCCESS != code) {
397,321!
544
    nodesDestroyNode((SNode*)*pPartialFunc);
×
545
    if (pMidFunc) nodesDestroyNode((SNode*)*pMidFunc);
×
546
    nodesDestroyNode((SNode*)*pMergeFunc);
×
547
  }
548

549
  return code;
397,321✔
550
}
551

552
char* fmGetFuncName(int32_t funcId) {
2✔
553
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2!
554
    return taosStrdup("invalid function");
×
555
  }
556
  return  taosStrdup(funcMgtBuiltins[funcId].name);
2✔
557
}
558

559
/// @param [out] pStateFunc, not changed if error occured or no need to create state func
560
/// @retval 0 for succ, otherwise err occured
561
static int32_t fmCreateStateFunc(const SFunctionNode* pFunc, SFunctionNode** pStateFunc) {
33,064✔
562
  if (funcMgtBuiltins[pFunc->funcId].pStateFunc) {
33,064!
563
    SNodeList* pParams = NULL;
33,064✔
564
    int32_t code = nodesCloneList(pFunc->pParameterList, &pParams);
33,064✔
565
    if (!pParams) return code;
33,064!
566
    code = createFunction(funcMgtBuiltins[pFunc->funcId].pStateFunc, pParams, pStateFunc);
33,064✔
567
    if (TSDB_CODE_SUCCESS != code) {
33,064!
568
      nodesDestroyList(pParams);
×
569
      return code;
×
570
    }
571
    tstrncpy((*pStateFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
33,064✔
572
    tstrncpy((*pStateFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
33,064✔
573
  }
574
  return TSDB_CODE_SUCCESS;
33,064✔
575
}
576

577
bool fmIsTSMASupportedFunc(func_id_t funcId) {
303,406✔
578
  return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC) &&
589,587✔
579
         !isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_STREAM_FUNC);
286,181!
580
}
581

582
int32_t fmCreateStateFuncs(SNodeList* pFuncs) {
152✔
583
  int32_t code;
584
  SNode*  pNode;
585
  char    buf[128] = {0};
152✔
586
  FOREACH(pNode, pFuncs) {
33,216!
587
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
33,064✔
588
    code = fmGetFuncInfo(pFunc, buf, 128);
33,064✔
589
    if (code) break;
33,064!
590
    if (fmIsTSMASupportedFunc(pFunc->funcId)) {
33,064!
591
      SFunctionNode* pNewFunc = NULL;
33,064✔
592
      code = fmCreateStateFunc(pFunc, &pNewFunc);
33,064✔
593
      if (code) {
33,064!
594
        // error
595
        break;
×
596
      } else if (!pNewFunc) {
33,064!
597
        // no need state func
598
        continue;
×
599
      } else {
600
        REPLACE_NODE(pNewFunc);
33,064✔
601
        nodesDestroyNode(pNode);
33,064✔
602
      }
603
    }
604
  }
605
  return code;
152✔
606
}
607

608
static int32_t fmCreateStateMergeFunc(SFunctionNode* pFunc, SFunctionNode** pStateMergeFunc) {
370✔
609
  if (funcMgtBuiltins[pFunc->funcId].pMergeFunc) {
370✔
610
    SNodeList* pParams = NULL;
358✔
611
    int32_t code = nodesCloneList(pFunc->pParameterList, &pParams);
358✔
612
    if (!pParams) return code;
358!
613
    code = createFunction(funcMgtBuiltins[pFunc->funcId].pMergeFunc, pParams, pStateMergeFunc);
358✔
614
    if (TSDB_CODE_SUCCESS != code) {
358!
615
      nodesDestroyList(pParams);
×
616
      return code;
×
617
    }
618
    tstrncpy((*pStateMergeFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
358✔
619
    tstrncpy((*pStateMergeFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
358✔
620
  }
621
  return TSDB_CODE_SUCCESS;
370✔
622
}
623

624
int32_t fmCreateStateMergeFuncs(SNodeList* pFuncs) {
75✔
625
  int32_t code;
626
  SNode*  pNode;
627
  char    buf[128] = {0};
75✔
628
  FOREACH(pNode, pFuncs) {
445!
629
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
370✔
630
    if (fmIsTSMASupportedFunc(pFunc->funcId)) {
370!
631
      SFunctionNode* pNewFunc = NULL;
370✔
632
      code = fmCreateStateMergeFunc(pFunc, &pNewFunc);
370✔
633
      if (code) {
370!
634
        // error
635
        break;
×
636
      } else if (!pNewFunc) {
370✔
637
        // no state merge func
638
        continue;
12✔
639
      } else {
640
        REPLACE_NODE(pNewFunc);
358✔
641
        nodesDestroyNode(pNode);
358✔
642
      }
643
    }
644
  }
645
  return code;
75✔
646
}
647

648
int32_t fmGetFuncId(const char* name) {
62,341✔
649
  if (NULL != gFunMgtService.pFuncNameHashTable) {
62,341!
650
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, name, strlen(name));
62,341✔
651
    if (NULL != pVal) {
62,341!
652
      return *(int32_t*)pVal;
62,341✔
653
    }
654
    return -1;
×
655
  }
656
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
×
657
    if (0 == strcmp(funcMgtBuiltins[i].name, name)) {
×
658
      return i;
×
659
    }
660
  }
661
  return -1;
×
662
}
663

664
bool fmIsMyStateFunc(int32_t funcId, int32_t stateFuncId) {
16,699,079✔
665
  const SBuiltinFuncDefinition* pFunc = &funcMgtBuiltins[funcId];
16,699,079✔
666
  const SBuiltinFuncDefinition* pStateFunc = &funcMgtBuiltins[stateFuncId];
16,699,079✔
667
  if (!pFunc->pStateFunc) {
16,699,079!
668
    return false;
×
669
  }
670
  if (strcmp(pFunc->pStateFunc, pStateFunc->name) == 0) return true;
16,699,079✔
671
  int32_t stateMergeFuncId = fmGetFuncId(pFunc->pStateFunc);
60,505✔
672
  if (stateMergeFuncId == -1) {
60,505!
673
    return false;
×
674
  }
675
  const SBuiltinFuncDefinition* pStateMergeFunc = &funcMgtBuiltins[stateMergeFuncId];
60,505✔
676
  return strcmp(pStateFunc->name, pStateMergeFunc->pMergeFunc) == 0;
60,505✔
677
}
678

679
bool fmIsCountLikeFunc(int32_t funcId) {
1,651,296✔
680
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
1,651,296✔
681
}
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