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

taosdata / TDengine / #3525

10 Nov 2024 03:50AM UTC coverage: 60.818% (-0.08%) from 60.898%
#3525

push

travis-ci

web-flow
Merge pull request #28709 from taosdata/main

merge: from main to 3.0 branch

118634 of 249004 branches covered (47.64%)

Branch coverage included in aggregate %.

136 of 169 new or added lines in 23 files covered. (80.47%)

542 existing lines in 129 files now uncovered.

199071 of 273386 relevant lines covered (72.82%)

15691647.46 hits per line

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

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

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

51
static bool isSpecificClassifyFunc(int32_t funcId, uint64_t classification) {
327,728,412✔
52
  if (fmIsUserDefinedFunc(funcId)) {
327,728,412✔
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);
116,765✔
56
  }
57
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
327,623,798!
58
    return false;
85,522,411✔
59
  }
60
  return FUNC_MGT_TEST_MASK(funcMgtBuiltins[funcId].classification, classification);
242,101,387✔
61
}
62

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

68
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
5,215,001✔
69
  if (NULL != gFunMgtService.pFuncNameHashTable) {
5,215,001✔
70
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
5,210,155✔
71
    if (NULL != pVal) {
5,210,250✔
72
      pFunc->funcId = *(int32_t*)pVal;
5,208,655✔
73
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
5,208,655✔
74
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
5,208,655✔
75
    }
76
    return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
1,595✔
77
  }
78
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
92,689!
79
    if (0 == strcmp(funcMgtBuiltins[i].name, pFunc->functionName)) {
92,723✔
80
      pFunc->funcId = i;
4,880✔
81
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
4,880✔
82
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
4,880✔
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,024✔
91
  }
92
  return (fmIsIndefiniteRowsFunc(pFunc->funcId) || fmIsMultiRowsFunc(pFunc->funcId)) ? FUNC_RETURN_ROWS_INDEFINITE
40,774!
93
                                                                                     : FUNC_RETURN_ROWS_NORMAL;
40,774!
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,445,221✔
101
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
3,445,221✔
102
  if (NULL != pVal) {
3,445,254✔
103
    return funcMgtBuiltins[*(int32_t*)pVal].type;
3,443,657✔
104
  }
105
  return FUNCTION_TYPE_UDF;
1,597✔
106
}
107

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

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

123
  const char* name = funcMgtBuiltins[funcId].name;
733,335✔
124
  if ((strcmp(name, "_group_key") == 0) || (strcmp(name, "_select_value") == 0)) {
733,335✔
125
    return FUNC_DATA_REQUIRED_NOT_LOAD;;
156,482✔
126
  }
127

128
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
576,853✔
129
    return FUNC_DATA_REQUIRED_DATA_LOAD;
497,837✔
130
  } else {
131
    return funcMgtBuiltins[funcId].dynDataRequiredFunc(pRes, pBlockInfo);
79,016✔
132
  }
133
}
134

135
int32_t fmGetFuncExecFuncs(int32_t funcId, SFuncExecFuncs* pFpSet) {
6,504,769✔
136
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
6,504,769!
UNCOV
137
    return TSDB_CODE_FAILED;
×
138
  }
139
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
6,507,065✔
140
  pFpSet->init = funcMgtBuiltins[funcId].initFunc;
6,507,065✔
141
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
6,507,065✔
142
  pFpSet->finalize = funcMgtBuiltins[funcId].finalizeFunc;
6,507,065✔
143
  pFpSet->combine = funcMgtBuiltins[funcId].combineFunc;
6,507,065✔
144
  pFpSet->processFuncByRow = funcMgtBuiltins[funcId].processFuncByRow;
6,507,065✔
145
  pFpSet->cleanup = funcMgtBuiltins[funcId].cleanupFunc;
6,507,065✔
146
  return TSDB_CODE_SUCCESS;
6,507,065✔
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,390,302✔
161
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
13,390,302!
162
    return TSDB_CODE_FAILED;
1,279✔
163
  }
164
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
13,388,651✔
165
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
13,388,651✔
166
  return TSDB_CODE_SUCCESS;
13,388,651✔
167
}
168

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

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

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

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

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

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

181
bool fmIsPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PSEUDO_COLUMN_FUNC); }
34,314,823✔
182

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

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

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

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

191

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

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

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

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

204
bool fmIsUserDefinedFunc(int32_t funcId) { return funcId > FUNC_UDF_ID_START; }
372,283,965✔
205

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

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

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

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

214
bool fmIsImplicitTsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IMPLICIT_TS_FUNC); }
58,082,817✔
215

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

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

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

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

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

226
bool fmIsInterpFunc(int32_t funcId) {
6,990,177✔
227
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
6,990,177!
228
    return false;
3,091✔
229
  }
230
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
6,987,086✔
231
}
232

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

235
bool fmIsForecastFunc(int32_t funcId) {
6,835,353✔
236
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
6,835,353!
237
    return false;
3,020✔
238
  }
239
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
6,832,333✔
240
}
241

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

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

251
bool fmIsNotNullOutputFunc(int32_t funcId) {
10,231,508✔
252
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
10,231,508!
253
    return false;
×
254
  }
255
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
20,210,548✔
256
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
9,977,909✔
257
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
9,331,054✔
258
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
9,011,480✔
259
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
8,805,517✔
260
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
8,574,020✔
261
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
8,460,408✔
262
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
7,733,472✔
263
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
27,871,070✔
264
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
7,660,522✔
265
}
266

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

274
bool fmIsGroupKeyFunc(int32_t funcId) {
406,183,685✔
275
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
406,183,685!
276
    return false;
×
277
  }
278
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
406,199,263✔
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,488,982✔
289
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
21,488,982!
290
    return false;
×
291
  }
292
  return FUNCTION_TYPE_ELAPSED == funcMgtBuiltins[funcId].type;
21,489,001✔
293
}
294

295
bool fmIsBlockDistFunc(int32_t funcId) {
3,532,828✔
296
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
3,532,828!
297
    return false;
1,422✔
298
  }
299
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
3,531,406✔
300
}
301

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

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

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

370
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
89,871✔
371
  SNode* pNode;
372
  FOREACH(pNode, pFunc->pParameterList) {
90,501✔
373
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
3,727✔
374
      return false;
3,097✔
375
    }
376
  }
377
  return true;
86,774✔
378
}
379

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

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

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

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

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

421
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
963,190✔
422
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
963,190✔
423

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

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

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

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

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

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

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

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

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

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

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

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

551
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
337,764✔
552
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
337,762!
553
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
287,663✔
554
  }
555
  if (TSDB_CODE_SUCCESS == code) {
337,761!
556
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
337,763✔
557
  }
558

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

565
  return code;
337,763✔
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) {
549,574✔
594
  return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC) &&
1,075,635✔
595
         !isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_STREAM_FUNC);
526,061!
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) {
422✔
625
  if (funcMgtBuiltins[pFunc->funcId].pMergeFunc) {
422✔
626
    SNodeList* pParams = NULL;
410✔
627
    int32_t code = nodesCloneList(pFunc->pParameterList, &pParams);
410✔
628
    if (!pParams) return code;
410!
629
    code = createFunction(funcMgtBuiltins[pFunc->funcId].pMergeFunc, pParams, pStateMergeFunc);
410✔
630
    if (TSDB_CODE_SUCCESS != code) {
410!
631
      nodesDestroyList(pParams);
×
632
      return code;
×
633
    }
634
    tstrncpy((*pStateMergeFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
410✔
635
    tstrncpy((*pStateMergeFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
410✔
636
  }
637
  return TSDB_CODE_SUCCESS;
422✔
638
}
639

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

664
int32_t fmGetFuncId(const char* name) {
85,042✔
665
  if (NULL != gFunMgtService.pFuncNameHashTable) {
85,042!
666
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, name, strlen(name));
85,042✔
667
    if (NULL != pVal) {
85,042!
668
      return *(int32_t*)pVal;
85,042✔
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,723,311✔
681
  const SBuiltinFuncDefinition* pFunc = &funcMgtBuiltins[funcId];
16,723,311✔
682
  const SBuiltinFuncDefinition* pStateFunc = &funcMgtBuiltins[stateFuncId];
16,723,311✔
683
  if (!pFunc->pStateFunc) {
16,723,311!
684
    return false;
×
685
  }
686
  if (strcmp(pFunc->pStateFunc, pStateFunc->name) == 0) return true;
16,723,311✔
687
  int32_t stateMergeFuncId = fmGetFuncId(pFunc->pStateFunc);
83,202✔
688
  if (stateMergeFuncId == -1) {
83,202!
689
    return false;
×
690
  }
691
  const SBuiltinFuncDefinition* pStateMergeFunc = &funcMgtBuiltins[stateMergeFuncId];
83,202✔
692
  return strcmp(pStateFunc->name, pStateMergeFunc->pMergeFunc) == 0;
83,202✔
693
}
694

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

© 2026 Coveralls, Inc