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

taosdata / TDengine / #3544

30 Nov 2024 03:06AM UTC coverage: 60.88% (+0.04%) from 60.842%
#3544

push

travis-ci

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

merge: from main to 3.0 branch

120724 of 253479 branches covered (47.63%)

Branch coverage included in aggregate %.

407 of 489 new or added lines in 21 files covered. (83.23%)

1148 existing lines in 113 files now uncovered.

201919 of 276488 relevant lines covered (73.03%)

18898587.44 hits per line

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

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

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

51
static bool isSpecificClassifyFunc(int32_t funcId, uint64_t classification) {
351,175,260✔
52
  if (fmIsUserDefinedFunc(funcId)) {
351,175,260✔
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,067✔
56
  }
57
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
351,086,182!
58
    return false;
90,557,442✔
59
  }
60
  return FUNC_MGT_TEST_MASK(funcMgtBuiltins[funcId].classification, classification);
260,528,740✔
61
}
62

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

68
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
4,927,780✔
69
  if (NULL != gFunMgtService.pFuncNameHashTable) {
4,927,780✔
70
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
4,923,137✔
71
    if (NULL != pVal) {
4,923,179✔
72
      pFunc->funcId = *(int32_t*)pVal;
4,921,584✔
73
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
4,921,584✔
74
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
4,921,584✔
75
    }
76
    return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
1,595✔
77
  }
78
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
89,232!
79
    if (0 == strcmp(funcMgtBuiltins[i].name, pFunc->functionName)) {
89,244✔
80
      pFunc->funcId = i;
4,655✔
81
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
4,655✔
82
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
4,655✔
83
    }
84
  }
85
  return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
×
86
}
87

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

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

100
EFunctionType fmGetFuncType(const char* pFunc) {
3,279,781✔
101
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
3,279,781✔
102
  if (NULL != pVal) {
3,279,815✔
103
    return funcMgtBuiltins[*(int32_t*)pVal].type;
3,278,220✔
104
  }
105
  return FUNCTION_TYPE_UDF;
1,595✔
106
}
107

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

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

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

128
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
1,862,507✔
129
    return FUNC_DATA_REQUIRED_DATA_LOAD;
1,825,100✔
130
  } else {
131
    return funcMgtBuiltins[funcId].dynDataRequiredFunc(pRes, pBlockInfo);
37,407✔
132
  }
133
}
134

135
int32_t fmGetFuncExecFuncs(int32_t funcId, SFuncExecFuncs* pFpSet) {
7,155,672✔
136
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
7,155,672!
137
    return TSDB_CODE_FAILED;
×
138
  }
139
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
7,159,462✔
140
  pFpSet->init = funcMgtBuiltins[funcId].initFunc;
7,159,462✔
141
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
7,159,462✔
142
  pFpSet->finalize = funcMgtBuiltins[funcId].finalizeFunc;
7,159,462✔
143
  pFpSet->combine = funcMgtBuiltins[funcId].combineFunc;
7,159,462✔
144
  pFpSet->processFuncByRow = funcMgtBuiltins[funcId].processFuncByRow;
7,159,462✔
145
  pFpSet->cleanup = funcMgtBuiltins[funcId].cleanupFunc;
7,159,462✔
146
  return TSDB_CODE_SUCCESS;
7,159,462✔
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) {
15,505,184✔
161
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
15,505,184!
UNCOV
162
    return TSDB_CODE_FAILED;
×
163
  }
164
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
15,505,070✔
165
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
15,505,070✔
166
  return TSDB_CODE_SUCCESS;
15,505,070✔
167
}
168

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

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

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

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

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

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

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

183
bool fmIsScanPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCAN_PC_FUNC); }
26,468,152✔
184

185
bool fmIsWindowPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_WINDOW_PC_FUNC); }
16,007,448✔
186

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

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

191

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

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

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

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

204
bool fmIsUserDefinedFunc(int32_t funcId) { return funcId > FUNC_UDF_ID_START; }
401,003,077✔
205

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

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

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

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

214
bool fmIsImplicitTsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IMPLICIT_TS_FUNC); }
83,495,815✔
215

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

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

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

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

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

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

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

235
bool fmIsForecastFunc(int32_t funcId) {
6,495,007✔
236
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
6,495,007!
237
    return false;
3,131✔
238
  }
239
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
6,491,876✔
240
}
241

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

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

251
bool fmIsNotNullOutputFunc(int32_t funcId) {
10,853,188✔
252
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
10,853,188!
253
    return false;
×
254
  }
255
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
21,456,111✔
256
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
10,600,752✔
257
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
9,978,746✔
258
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
9,672,865✔
259
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
9,463,069✔
260
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
9,228,302✔
261
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
9,114,093✔
262
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
7,593,942✔
263
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
28,988,423✔
264
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
7,532,312✔
265
}
266

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

274
bool fmIsGroupKeyFunc(int32_t funcId) {
475,254,735✔
275
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
475,254,735!
276
    return false;
×
277
  }
278
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
475,273,402✔
279
}
280

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

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

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

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

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

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

370
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
71,530✔
371
  SNode* pNode;
372
  FOREACH(pNode, pFunc->pParameterList) {
72,160✔
373
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
3,458✔
374
      return false;
2,828✔
375
    }
376
  }
377
  return true;
68,702✔
378
}
379

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

559
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
318,468✔
560
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
318,468!
561
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
268,762✔
562
  }
563
  if (TSDB_CODE_SUCCESS == code) {
318,468!
564
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
318,468✔
565
  }
566

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

573
  return code;
318,468✔
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) {
556,055✔
602
  return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC) &&
1,089,883✔
603
         !isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_STREAM_FUNC);
533,828!
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,194✔
673
  if (NULL != gFunMgtService.pFuncNameHashTable) {
84,194!
674
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, name, strlen(name));
84,194✔
675
    if (NULL != pVal) {
84,194!
676
      return *(int32_t*)pVal;
84,194✔
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,726,602✔
689
  const SBuiltinFuncDefinition* pFunc = &funcMgtBuiltins[funcId];
16,726,602✔
690
  const SBuiltinFuncDefinition* pStateFunc = &funcMgtBuiltins[stateFuncId];
16,726,602✔
691
  if (!pFunc->pStateFunc) {
16,726,602!
692
    return false;
×
693
  }
694
  if (strcmp(pFunc->pStateFunc, pStateFunc->name) == 0) return true;
16,726,602✔
695
  int32_t stateMergeFuncId = fmGetFuncId(pFunc->pStateFunc);
82,732✔
696
  if (stateMergeFuncId == -1) {
82,732!
697
    return false;
×
698
  }
699
  const SBuiltinFuncDefinition* pStateMergeFunc = &funcMgtBuiltins[stateMergeFuncId];
82,732✔
700
  return strcmp(pStateFunc->name, pStateMergeFunc->pMergeFunc) == 0;
82,732✔
701
}
702

703
bool fmIsCountLikeFunc(int32_t funcId) {
1,500,604✔
704
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
1,500,604✔
705
}
706

707
bool fmIsRowTsOriginFunc(int32_t funcId) {
21,863✔
708
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
21,863!
NEW
709
    return false;
×
710
  }
711
  return FUNCTION_TYPE_IROWTS_ORIGIN == funcMgtBuiltins[funcId].type;
21,863✔
712
}
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