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

taosdata / TDengine / #3543

29 Nov 2024 02:58AM UTC coverage: 60.842% (+0.02%) from 60.819%
#3543

push

travis-ci

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

merge: from main to 3.0

120460 of 253224 branches covered (47.57%)

Branch coverage included in aggregate %.

706 of 908 new or added lines in 18 files covered. (77.75%)

2401 existing lines in 137 files now uncovered.

201633 of 276172 relevant lines covered (73.01%)

19045673.23 hits per line

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

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

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

51
static bool isSpecificClassifyFunc(int32_t funcId, uint64_t classification) {
341,355,616✔
52
  if (fmIsUserDefinedFunc(funcId)) {
341,355,616✔
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,706✔
56
  }
57
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
341,245,437!
58
    return false;
87,713,878✔
59
  }
60
  return FUNC_MGT_TEST_MASK(funcMgtBuiltins[funcId].classification, classification);
253,531,559✔
61
}
62

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

68
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
4,859,909✔
69
  if (NULL != gFunMgtService.pFuncNameHashTable) {
4,859,909✔
70
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
4,855,050✔
71
    if (NULL != pVal) {
4,855,072✔
72
      pFunc->funcId = *(int32_t*)pVal;
4,853,477✔
73
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
4,853,477✔
74
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
4,853,477✔
75
    }
76
    return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
1,595✔
77
  }
78
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
92,961!
79
    if (0 == strcmp(funcMgtBuiltins[i].name, pFunc->functionName)) {
92,973✔
80
      pFunc->funcId = i;
4,871✔
81
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
4,871✔
82
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
4,871✔
83
    }
84
  }
85
  return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
×
86
}
87

88
EFuncReturnRows fmGetFuncReturnRows(SFunctionNode* pFunc) {
62,854✔
89
  if (NULL != funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc) {
62,854✔
90
    return funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc(pFunc);
22,083✔
91
  }
92
  return (fmIsIndefiniteRowsFunc(pFunc->funcId) || fmIsMultiRowsFunc(pFunc->funcId)) ? FUNC_RETURN_ROWS_INDEFINITE
40,771!
93
                                                                                     : FUNC_RETURN_ROWS_NORMAL;
40,771!
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,209,384✔
101
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
3,209,384✔
102
  if (NULL != pVal) {
3,209,407✔
103
    return funcMgtBuiltins[*(int32_t*)pVal].type;
3,207,813✔
104
  }
105
  return FUNCTION_TYPE_UDF;
1,594✔
106
}
107

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

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

123
  const char* name = funcMgtBuiltins[funcId].name;
2,228,732✔
124
  if ((strcmp(name, "_group_key") == 0) || (strcmp(name, "_select_value") == 0)) {
2,228,732✔
125
    return FUNC_DATA_REQUIRED_NOT_LOAD;;
199,412✔
126
  }
127

128
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
2,029,320✔
129
    return FUNC_DATA_REQUIRED_DATA_LOAD;
2,000,228✔
130
  } else {
131
    return funcMgtBuiltins[funcId].dynDataRequiredFunc(pRes, pBlockInfo);
29,092✔
132
  }
133
}
134

135
int32_t fmGetFuncExecFuncs(int32_t funcId, SFuncExecFuncs* pFpSet) {
7,236,664✔
136
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
7,236,664!
137
    return TSDB_CODE_FAILED;
×
138
  }
139
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
7,241,106✔
140
  pFpSet->init = funcMgtBuiltins[funcId].initFunc;
7,241,106✔
141
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
7,241,106✔
142
  pFpSet->finalize = funcMgtBuiltins[funcId].finalizeFunc;
7,241,106✔
143
  pFpSet->combine = funcMgtBuiltins[funcId].combineFunc;
7,241,106✔
144
  pFpSet->processFuncByRow = funcMgtBuiltins[funcId].processFuncByRow;
7,241,106✔
145
  pFpSet->cleanup = funcMgtBuiltins[funcId].cleanupFunc;
7,241,106✔
146
  return TSDB_CODE_SUCCESS;
7,241,106✔
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) {
14,676,253✔
161
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
14,676,253!
162
    return TSDB_CODE_FAILED;
615✔
163
  }
164
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
14,675,752✔
165
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
14,675,752✔
166
  return TSDB_CODE_SUCCESS;
14,675,752✔
167
}
168

169
bool fmIsAggFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_AGG_FUNC); }
23,409,626✔
170

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

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

175
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
32,149,468✔
176

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

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

181
bool fmIsPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PSEUDO_COLUMN_FUNC); }
35,866,282✔
182

183
bool fmIsScanPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCAN_PC_FUNC); }
23,537,894✔
184

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

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

189
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
17,039,624✔
190

191

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

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

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

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

204
bool fmIsUserDefinedFunc(int32_t funcId) { return funcId > FUNC_UDF_ID_START; }
390,081,492✔
205

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

208
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
30,696,925✔
209

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

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

214
bool fmIsImplicitTsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IMPLICIT_TS_FUNC); }
74,227,103✔
215

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

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

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

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

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

226
bool fmIsInterpFunc(int32_t funcId) {
6,528,577✔
227
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
6,528,577!
228
    return false;
3,140✔
229
  }
230
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
6,525,437✔
231
}
232

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

235
bool fmIsForecastFunc(int32_t funcId) {
6,375,121✔
236
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
6,375,121!
237
    return false;
3,128✔
238
  }
239
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
6,371,993✔
240
}
241

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

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

251
bool fmIsNotNullOutputFunc(int32_t funcId) {
10,905,600✔
252
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
10,905,600!
253
    return false;
×
254
  }
255
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
21,551,593✔
256
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
10,644,234✔
257
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
10,025,897✔
258
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
9,721,819✔
259
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
9,501,980✔
260
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
9,266,569✔
261
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
9,151,781✔
262
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
7,625,540✔
263
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
29,115,592✔
264
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
7,563,999✔
265
}
266

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

274
bool fmIsGroupKeyFunc(int32_t funcId) {
453,594,115✔
275
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
453,594,115!
276
    return false;
×
277
  }
278
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
453,619,298✔
279
}
280

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

288
bool fmIsElapsedFunc(int32_t funcId) {
18,809,299✔
289
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
18,809,299!
290
    return false;
×
291
  }
292
  return FUNCTION_TYPE_ELAPSED == funcMgtBuiltins[funcId].type;
18,809,318✔
293
}
294

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

302
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
5,269,881✔
303

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

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

370
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
71,358✔
371
  SNode* pNode;
372
  FOREACH(pNode, pFunc->pParameterList) {
71,988✔
373
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
3,645✔
374
      return false;
3,015✔
375
    }
376
  }
377
  return true;
68,343✔
378
}
379

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

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

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

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

415
static void resetOutputChangedFunc(SFunctionNode *pFunc, const SFunctionNode* pSrcFunc) {
908,559✔
416
  if (funcMgtBuiltins[pFunc->funcId].type == FUNCTION_TYPE_LAST_MERGE) {
908,559✔
417
    pFunc->node.resType = pSrcFunc->node.resType;
49,939✔
418
    return;
49,939✔
419
  }
420
}
421

422
int32_t createFunctionWithSrcFunc(const char* pName, const SFunctionNode* pSrcFunc, SNodeList* pParameterList, SFunctionNode** ppFunc) {
908,560✔
423
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
908,560✔
424
  if (NULL == *ppFunc) {
908,560!
425
    return code;
×
426
  }
427

428
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
908,560✔
429
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
908,560✔
430

431
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
908,560✔
432
  (*ppFunc)->pParameterList = pParameterList;
908,560✔
433
  code = getFuncInfo((*ppFunc));
908,560✔
434
  if (TSDB_CODE_SUCCESS != code) {
908,559!
435
    (*ppFunc)->pParameterList = NULL;
×
436
    nodesDestroyNode((SNode*)*ppFunc);
×
437
    *ppFunc = NULL;
×
438
    return code;
×
439
  }
440
  resetOutputChangedFunc(*ppFunc, pSrcFunc);
908,559✔
441
  return code;
908,559✔
442
}
443

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

454
bool fmIsDistExecFunc(int32_t funcId) {
1,649,201✔
455
  if (fmIsUserDefinedFunc(funcId)) {
1,649,201✔
456
    return false;
890✔
457
  }
458
  if (!fmIsVectorFunc(funcId)) {
1,648,357!
459
    return true;
×
460
  }
461
  return (NULL != funcMgtBuiltins[funcId].pPartialFunc && NULL != funcMgtBuiltins[funcId].pMergeFunc);
1,648,369!
462
}
463

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

488
static int32_t createMergeFuncPara(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
589,085✔
489
                                   SNodeList** pParameterList) {
490
  SNode *pRes = NULL;
589,085✔
491
  int32_t code = createColumnByFunc(pPartialFunc, (SColumnNode**)&pRes);
589,085✔
492
  if (TSDB_CODE_SUCCESS != code) {
589,085!
493
    return code;
×
494
  }
495
  if (NULL != funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc) {
589,085✔
496
    return funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc(pSrcFunc->pParameterList, pRes, pParameterList);
4,892✔
497
  } else {
498
    return nodesListMakeStrictAppend(pParameterList, pRes);
584,193✔
499
  }
500
}
501

502
static int32_t createMidFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
269,609✔
503
                                   SFunctionNode** pMidFunc) {
504
  SNodeList*     pParameterList = NULL;
269,609✔
505
  SFunctionNode* pFunc = NULL;
269,609✔
506

507
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
269,609✔
508
  if (TSDB_CODE_SUCCESS == code) {
269,609!
509
    if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){
269,609✔
510
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pSrcFunc, pParameterList, &pFunc);
18,410✔
511
    }else{
512
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
251,199✔
513
    }
514
  }
515
  if (TSDB_CODE_SUCCESS == code) {
269,609!
516
    tstrncpy(pFunc->node.aliasName, pPartialFunc->node.aliasName, TSDB_COL_NAME_LEN);
269,609✔
517
  }
518

519
  if (TSDB_CODE_SUCCESS == code) {
269,609!
520
    *pMidFunc = pFunc;
269,609✔
521
  } else {
522
    nodesDestroyList(pParameterList);
×
523
  }
524
  return code;
269,609✔
525
}
526

527
static int32_t createMergeFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
319,476✔
528
                                   SFunctionNode** pMergeFunc) {
529
  SNodeList*     pParameterList = NULL;
319,476✔
530
  SFunctionNode* pFunc = NULL;
319,476✔
531

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

546
  if (TSDB_CODE_SUCCESS == code) {
319,476!
547
    *pMergeFunc = pFunc;
319,476✔
548
  } else {
549
    nodesDestroyList(pParameterList);
×
550
  }
551
  return code;
319,476✔
552
}
553

554
int32_t fmGetDistMethod(const SFunctionNode* pFunc, SFunctionNode** pPartialFunc, SFunctionNode** pMidFunc, SFunctionNode** pMergeFunc) {
319,475✔
555
  if (!fmIsDistExecFunc(pFunc->funcId)) {
319,475!
556
    return TSDB_CODE_FAILED;
×
557
  }
558

559
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
319,474✔
560
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
319,476!
561
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
269,609✔
562
  }
563
  if (TSDB_CODE_SUCCESS == code) {
319,476!
564
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
319,476✔
565
  }
566

567
  if (TSDB_CODE_SUCCESS != code) {
319,476!
568
    nodesDestroyNode((SNode*)*pPartialFunc);
×
569
    if (pMidFunc) nodesDestroyNode((SNode*)*pMidFunc);
×
570
    nodesDestroyNode((SNode*)*pMergeFunc);
×
571
  }
572

573
  return code;
319,476✔
574
}
575

576
char* fmGetFuncName(int32_t funcId) {
2✔
577
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2!
578
    return taosStrdup("invalid function");
×
579
  }
580
  return  taosStrdup(funcMgtBuiltins[funcId].name);
2✔
581
}
582

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

601
bool fmIsTSMASupportedFunc(func_id_t funcId) {
532,542✔
602
  return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC) &&
1,042,033✔
603
         !isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_STREAM_FUNC);
509,491!
604
}
605

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

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

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

672
int32_t fmGetFuncId(const char* name) {
84,566✔
673
  if (NULL != gFunMgtService.pFuncNameHashTable) {
84,566!
674
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, name, strlen(name));
84,566✔
675
    if (NULL != pVal) {
84,566!
676
      return *(int32_t*)pVal;
84,566✔
677
    }
678
    return -1;
×
679
  }
680
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
×
681
    if (0 == strcmp(funcMgtBuiltins[i].name, name)) {
×
682
      return i;
×
683
    }
684
  }
685
  return -1;
×
686
}
687

688
bool fmIsMyStateFunc(int32_t funcId, int32_t stateFuncId) {
16,727,706✔
689
  const SBuiltinFuncDefinition* pFunc = &funcMgtBuiltins[funcId];
16,727,706✔
690
  const SBuiltinFuncDefinition* pStateFunc = &funcMgtBuiltins[stateFuncId];
16,727,706✔
691
  if (!pFunc->pStateFunc) {
16,727,706!
692
    return false;
×
693
  }
694
  if (strcmp(pFunc->pStateFunc, pStateFunc->name) == 0) return true;
16,727,706✔
695
  int32_t stateMergeFuncId = fmGetFuncId(pFunc->pStateFunc);
82,808✔
696
  if (stateMergeFuncId == -1) {
82,808!
697
    return false;
×
698
  }
699
  const SBuiltinFuncDefinition* pStateMergeFunc = &funcMgtBuiltins[stateMergeFuncId];
82,808✔
700
  return strcmp(pStateFunc->name, pStateMergeFunc->pMergeFunc) == 0;
82,808✔
701
}
702

703
bool fmIsCountLikeFunc(int32_t funcId) {
1,538,751✔
704
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
1,538,751✔
705
}
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