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

taosdata / TDengine / #3529

14 Nov 2024 01:56PM UTC coverage: 60.888% (-0.02%) from 60.905%
#3529

push

travis-ci

web-flow
Merge pull request #28764 from taosdata/docs/TS-4937

doc(arch/last): new section for last/last_row cache

119990 of 252020 branches covered (47.61%)

Branch coverage included in aggregate %.

200800 of 274829 relevant lines covered (73.06%)

15624555.39 hits per line

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

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

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

51
static bool isSpecificClassifyFunc(int32_t funcId, uint64_t classification) {
321,695,609✔
52
  if (fmIsUserDefinedFunc(funcId)) {
321,695,609✔
53
    return FUNC_MGT_AGG_FUNC == classification
54
               ? FUNC_AGGREGATE_UDF_ID == funcId
55
               : (FUNC_MGT_SCALAR_FUNC == classification ? FUNC_SCALAR_UDF_ID == funcId : false);
117,263✔
56
  }
57
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
321,589,400!
58
    return false;
89,038,015✔
59
  }
60
  return FUNC_MGT_TEST_MASK(funcMgtBuiltins[funcId].classification, classification);
232,551,385✔
61
}
62

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

68
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
4,951,951✔
69
  if (NULL != gFunMgtService.pFuncNameHashTable) {
4,951,951✔
70
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
4,947,094✔
71
    if (NULL != pVal) {
4,947,175✔
72
      pFunc->funcId = *(int32_t*)pVal;
4,945,580✔
73
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
4,945,580✔
74
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
4,945,580✔
75
    }
76
    return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
1,595✔
77
  }
78
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
92,700!
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,249,510✔
101
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
3,249,510✔
102
  if (NULL != pVal) {
3,249,533✔
103
    return funcMgtBuiltins[*(int32_t*)pVal].type;
3,247,945✔
104
  }
105
  return FUNCTION_TYPE_UDF;
1,588✔
106
}
107

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

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

123
  const char* name = funcMgtBuiltins[funcId].name;
704,586✔
124
  if ((strcmp(name, "_group_key") == 0) || (strcmp(name, "_select_value") == 0)) {
704,586✔
125
    return FUNC_DATA_REQUIRED_NOT_LOAD;;
79,611✔
126
  }
127

128
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
624,975✔
129
    return FUNC_DATA_REQUIRED_DATA_LOAD;
587,894✔
130
  } else {
131
    return funcMgtBuiltins[funcId].dynDataRequiredFunc(pRes, pBlockInfo);
37,081✔
132
  }
133
}
134

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

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

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

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

175
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
29,560,398✔
176

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

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

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

183
bool fmIsScanPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCAN_PC_FUNC); }
25,135,198✔
184

185
bool fmIsWindowPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_WINDOW_PC_FUNC); }
14,708,688✔
186

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

189
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
16,202,611✔
190

191

192
bool fmIsSpecialDataRequiredFunc(int32_t funcId) {
780,499✔
193
  return isSpecificClassifyFunc(funcId, FUNC_MGT_SPECIAL_DATA_REQUIRED);
780,499✔
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,677,127✔
201

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

204
bool fmIsUserDefinedFunc(int32_t funcId) { return funcId > FUNC_UDF_ID_START; }
366,477,746✔
205

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

208
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
24,380,898✔
209

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

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

214
bool fmIsImplicitTsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IMPLICIT_TS_FUNC); }
68,741,290✔
215

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

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

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

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

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

226
bool fmIsInterpFunc(int32_t funcId) {
6,607,379✔
227
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
6,607,379!
228
    return false;
3,079✔
229
  }
230
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
6,604,300✔
231
}
232

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

235
bool fmIsForecastFunc(int32_t funcId) {
6,454,952✔
236
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
6,454,952!
237
    return false;
3,013✔
238
  }
239
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
6,451,939✔
240
}
241

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

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

251
bool fmIsNotNullOutputFunc(int32_t funcId) {
9,548,819✔
252
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
9,548,819!
253
    return false;
154✔
254
  }
255
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
18,841,281✔
256
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
9,292,616✔
257
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
8,679,151✔
258
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
8,375,310✔
259
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
8,159,703✔
260
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
7,904,843✔
261
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
7,779,942✔
262
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
7,121,702✔
263
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
25,900,678✔
264
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
7,059,397✔
265
}
266

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

274
bool fmIsGroupKeyFunc(int32_t funcId) {
345,561,991✔
275
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
345,561,991!
276
    return false;
×
277
  }
278
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
345,596,360✔
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) {
13,679,553✔
289
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
13,679,553!
290
    return false;
×
291
  }
292
  return FUNCTION_TYPE_ELAPSED == funcMgtBuiltins[funcId].type;
13,679,554✔
293
}
294

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

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

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

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

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

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

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

393
static int32_t getFuncInfo(SFunctionNode* pFunc) {
1,072,162✔
394
  char msg[128] = {0};
1,072,162✔
395
  return fmGetFuncInfo(pFunc, msg, sizeof(msg));
1,072,162✔
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
int32_t createFunctionWithSrcFunc(const char* pName, const SFunctionNode* pSrcFunc, SNodeList* pParameterList, SFunctionNode** ppFunc) {
961,538✔
416
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
961,538✔
417
  if (NULL == *ppFunc) {
961,538!
418
    return code;
×
419
  }
420

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

565
  return code;
337,228✔
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) {
598,420✔
594
  return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC) &&
1,173,240✔
595
         !isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_STREAM_FUNC);
574,820!
596
}
597

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

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

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

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

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