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

taosdata / TDengine / #3621

22 Feb 2025 11:44AM UTC coverage: 2.037% (-61.5%) from 63.573%
#3621

push

travis-ci

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

merge: from main to 3.0 branch

4357 of 287032 branches covered (1.52%)

Branch coverage included in aggregate %.

0 of 174 new or added lines in 18 files covered. (0.0%)

213359 existing lines in 469 files now uncovered.

7260 of 283369 relevant lines covered (2.56%)

23737.72 hits per line

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

0.0
/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

UNCOV
34
static void doInitFunctionTable() {
×
UNCOV
35
  gFunMgtService.pFuncNameHashTable =
×
UNCOV
36
      taosHashInit(funcMgtBuiltinsNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
×
UNCOV
37
  if (NULL == gFunMgtService.pFuncNameHashTable) {
×
38
    initFunctionCode = terrno;
×
39
    return;
×
40
  }
41

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

UNCOV
51
static bool isSpecificClassifyFunc(int32_t funcId, uint64_t classification) {
×
UNCOV
52
  if (fmIsUserDefinedFunc(funcId)) {
×
53
    return FUNC_MGT_AGG_FUNC == classification
54
               ? FUNC_AGGREGATE_UDF_ID == funcId
UNCOV
55
               : (FUNC_MGT_SCALAR_FUNC == classification ? FUNC_SCALAR_UDF_ID == funcId : false);
×
56
  }
UNCOV
57
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
UNCOV
58
    return false;
×
59
  }
UNCOV
60
  return FUNC_MGT_TEST_MASK(funcMgtBuiltins[funcId].classification, classification);
×
61
}
62

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

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

UNCOV
88
EFuncReturnRows fmGetFuncReturnRows(SFunctionNode* pFunc) {
×
UNCOV
89
  if (NULL != funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc) {
×
UNCOV
90
    return funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc(pFunc);
×
91
  }
UNCOV
92
  return (fmIsIndefiniteRowsFunc(pFunc->funcId) || fmIsMultiRowsFunc(pFunc->funcId)) ? FUNC_RETURN_ROWS_INDEFINITE
×
UNCOV
93
                                                                                     : FUNC_RETURN_ROWS_NORMAL;
×
94
}
95

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

UNCOV
100
EFunctionType fmGetFuncType(const char* pFunc) {
×
UNCOV
101
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
×
UNCOV
102
  if (NULL != pVal) {
×
UNCOV
103
    return funcMgtBuiltins[*(int32_t*)pVal].type;
×
104
  }
UNCOV
105
  return FUNCTION_TYPE_UDF;
×
106
}
107

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

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

UNCOV
123
  const char* name = funcMgtBuiltins[funcId].name;
×
UNCOV
124
  if ((strcmp(name, "_group_key") == 0) || (strcmp(name, "_select_value") == 0)) {
×
UNCOV
125
    return FUNC_DATA_REQUIRED_NOT_LOAD;
×
126
    ;
127
  }
128

UNCOV
129
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
×
UNCOV
130
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
131
  } else {
UNCOV
132
    return funcMgtBuiltins[funcId].dynDataRequiredFunc(pRes, pBlockInfo);
×
133
  }
134
}
135

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

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

UNCOV
161
int32_t fmGetScalarFuncExecFuncs(int32_t funcId, SScalarFuncExecFuncs* pFpSet) {
×
UNCOV
162
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
UNCOV
163
    return TSDB_CODE_FAILED;
×
164
  }
UNCOV
165
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
×
UNCOV
166
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
×
UNCOV
167
  return TSDB_CODE_SUCCESS;
×
168
}
169

UNCOV
170
bool fmIsAggFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_AGG_FUNC); }
×
171

UNCOV
172
bool fmIsScalarFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCALAR_FUNC); }
×
173

UNCOV
174
bool fmIsVectorFunc(int32_t funcId) { return !fmIsScalarFunc(funcId) && !fmIsPseudoColumnFunc(funcId); }
×
175

UNCOV
176
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
×
177

UNCOV
178
bool fmIsTimelineFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TIMELINE_FUNC); }
×
179

UNCOV
180
bool fmIsDateTimeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_DATETIME_FUNC); }
×
181

UNCOV
182
bool fmIsPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PSEUDO_COLUMN_FUNC); }
×
183

UNCOV
184
bool fmIsScanPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCAN_PC_FUNC); }
×
185

UNCOV
186
bool fmIsWindowPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_WINDOW_PC_FUNC); }
×
187

UNCOV
188
bool fmIsWindowClauseFunc(int32_t funcId) { return fmIsAggFunc(funcId) || fmIsWindowPseudoColumnFunc(funcId); }
×
189

UNCOV
190
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
×
191

UNCOV
192
bool fmIsSpecialDataRequiredFunc(int32_t funcId) {
×
UNCOV
193
  return isSpecificClassifyFunc(funcId, FUNC_MGT_SPECIAL_DATA_REQUIRED);
×
194
}
195

UNCOV
196
bool fmIsDynamicScanOptimizedFunc(int32_t funcId) {
×
UNCOV
197
  return isSpecificClassifyFunc(funcId, FUNC_MGT_DYNAMIC_SCAN_OPTIMIZED);
×
198
}
199

UNCOV
200
bool fmIsMultiResFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_RES_FUNC); }
×
201

UNCOV
202
bool fmIsRepeatScanFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_REPEAT_SCAN_FUNC); }
×
203

UNCOV
204
bool fmIsUserDefinedFunc(int32_t funcId) { return funcId > FUNC_UDF_ID_START; }
×
205

UNCOV
206
bool fmIsForbidFillFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_FILL_FUNC); }
×
207

UNCOV
208
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
×
209

UNCOV
210
bool fmIsForbidStreamFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_STREAM_FUNC); }
×
211

UNCOV
212
bool fmIsSystemInfoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SYSTEM_INFO_FUNC); }
×
213

UNCOV
214
bool fmIsImplicitTsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IMPLICIT_TS_FUNC); }
×
215

UNCOV
216
bool fmIsClientPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CLIENT_PC_FUNC); }
×
217

UNCOV
218
bool fmIsMultiRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_ROWS_FUNC); }
×
219

UNCOV
220
bool fmIsKeepOrderFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_KEEP_ORDER_FUNC); }
×
221

UNCOV
222
bool fmIsCumulativeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CUMULATIVE_FUNC); }
×
223

UNCOV
224
bool fmIsForbidSysTableFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_SYSTABLE_FUNC); }
×
225

UNCOV
226
bool fmIsInterpFunc(int32_t funcId) {
×
UNCOV
227
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
UNCOV
228
    return false;
×
229
  }
UNCOV
230
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
×
231
}
232

UNCOV
233
bool fmIsInterpPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERP_PC_FUNC); }
×
234

UNCOV
235
bool fmIsForecastFunc(int32_t funcId) {
×
UNCOV
236
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
UNCOV
237
    return false;
×
238
  }
UNCOV
239
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
×
240
}
241

UNCOV
242
bool fmIsForecastPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORECAST_PC_FUNC); }
×
243

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

UNCOV
251
bool fmIsNotNullOutputFunc(int32_t funcId) {
×
UNCOV
252
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
253
    return false;
×
254
  }
UNCOV
255
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
×
UNCOV
256
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
×
UNCOV
257
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
×
UNCOV
258
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
×
UNCOV
259
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
×
UNCOV
260
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
×
UNCOV
261
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
×
UNCOV
262
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
×
UNCOV
263
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
×
UNCOV
264
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
×
265
}
266

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

UNCOV
274
bool fmIsGroupKeyFunc(int32_t funcId) {
×
UNCOV
275
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
276
    return false;
×
277
  }
UNCOV
278
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
×
279
}
280

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

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

UNCOV
295
bool fmIsBlockDistFunc(int32_t funcId) {
×
UNCOV
296
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
UNCOV
297
    return false;
×
298
  }
UNCOV
299
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
×
300
}
301

UNCOV
302
bool fmIsDBUsageFunc(int32_t funcId) {
×
UNCOV
303
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
UNCOV
304
    return false;
×
305
  }
UNCOV
306
  return FUNCTION_TYPE_DB_USAGE == funcMgtBuiltins[funcId].type;
×
307
}
308

UNCOV
309
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
×
310

UNCOV
311
bool fmIsIgnoreNullFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IGNORE_NULL_FUNC); }
×
312

UNCOV
313
void fmFuncMgtDestroy() {
×
UNCOV
314
  void* m = gFunMgtService.pFuncNameHashTable;
×
UNCOV
315
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
×
UNCOV
316
    taosHashCleanup(m);
×
317
  }
UNCOV
318
}
×
319

320
#ifdef BUILD_NO_CALL
321
int32_t fmSetInvertFunc(int32_t funcId, SFuncExecFuncs* pFpSet) {
322
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
323
    return TSDB_CODE_FAILED;
324
  }
325
  pFpSet->process = funcMgtBuiltins[funcId].invertFunc;
326
  return TSDB_CODE_SUCCESS;
327
}
328

329
int32_t fmSetNormalFunc(int32_t funcId, SFuncExecFuncs* pFpSet) {
330
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
331
    return TSDB_CODE_FAILED;
332
  }
333
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
334
  return TSDB_CODE_SUCCESS;
335
}
336

337
bool fmIsInvertible(int32_t funcId) {
338
  bool res = false;
339
  switch (funcMgtBuiltins[funcId].type) {
340
    case FUNCTION_TYPE_COUNT:
341
    case FUNCTION_TYPE_SUM:
342
    case FUNCTION_TYPE_STDDEV:
343
    case FUNCTION_TYPE_AVG:
344
    case FUNCTION_TYPE_WSTART:
345
    case FUNCTION_TYPE_WEND:
346
    case FUNCTION_TYPE_WDURATION:
347
      res = true;
348
      break;
349
    default:
350
      break;
351
  }
352
  return res;
353
}
354
#endif
355

356
// function has same input/output type
UNCOV
357
bool fmIsSameInOutType(int32_t funcId) {
×
UNCOV
358
  bool res = false;
×
UNCOV
359
  switch (funcMgtBuiltins[funcId].type) {
×
UNCOV
360
    case FUNCTION_TYPE_MAX:
×
361
    case FUNCTION_TYPE_MIN:
362
    case FUNCTION_TYPE_TOP:
363
    case FUNCTION_TYPE_BOTTOM:
364
    case FUNCTION_TYPE_FIRST:
365
    case FUNCTION_TYPE_LAST:
366
    case FUNCTION_TYPE_SAMPLE:
367
    case FUNCTION_TYPE_TAIL:
368
    case FUNCTION_TYPE_UNIQUE:
UNCOV
369
      res = true;
×
UNCOV
370
      break;
×
UNCOV
371
    default:
×
UNCOV
372
      break;
×
373
  }
UNCOV
374
  return res;
×
375
}
376

UNCOV
377
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
×
378
  SNode* pNode;
UNCOV
379
  FOREACH(pNode, pFunc->pParameterList) {
×
UNCOV
380
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
×
UNCOV
381
      return false;
×
382
    }
383
  }
UNCOV
384
  return true;
×
385
}
386

UNCOV
387
bool fmIsSkipScanCheckFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SKIP_SCAN_CHECK_FUNC); }
×
388

UNCOV
389
bool fmIsPrimaryKeyFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PRIMARY_KEY_FUNC); }
×
UNCOV
390
void getLastCacheDataType(SDataType* pType, int32_t pkBytes) {
×
391
  // TODO: do it later.
UNCOV
392
  pType->bytes = getFirstLastInfoSize(pType->bytes, pkBytes) + VARSTR_HEADER_SIZE;
×
UNCOV
393
  pType->type = TSDB_DATA_TYPE_BINARY;
×
UNCOV
394
}
×
395

UNCOV
396
static int32_t getFuncInfo(SFunctionNode* pFunc) {
×
UNCOV
397
  char msg[128] = {0};
×
UNCOV
398
  return fmGetFuncInfo(pFunc, msg, sizeof(msg));
×
399
}
400

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

UNCOV
418
static void resetOutputChangedFunc(SFunctionNode *pFunc, const SFunctionNode* pSrcFunc) {
×
UNCOV
419
  if (funcMgtBuiltins[pFunc->funcId].type == FUNCTION_TYPE_LAST_MERGE) {
×
UNCOV
420
    pFunc->node.resType = pSrcFunc->node.resType;
×
UNCOV
421
    return;
×
422
  }
423
}
424

UNCOV
425
int32_t createFunctionWithSrcFunc(const char* pName, const SFunctionNode* pSrcFunc, SNodeList* pParameterList, SFunctionNode** ppFunc) {
×
UNCOV
426
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
×
UNCOV
427
  if (NULL == *ppFunc) {
×
428
    return code;
×
429
  }
430

UNCOV
431
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
×
UNCOV
432
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
×
433

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

UNCOV
447
static int32_t createColumnByFunc(const SFunctionNode* pFunc, SColumnNode** ppCol) {
×
UNCOV
448
  int32_t code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)ppCol);
×
UNCOV
449
  if (NULL == *ppCol) {
×
450
    return code;
×
451
  }
UNCOV
452
  tstrncpy((*ppCol)->colName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
×
UNCOV
453
  (*ppCol)->node.resType = pFunc->node.resType;
×
UNCOV
454
  return TSDB_CODE_SUCCESS;
×
455
}
456

UNCOV
457
bool fmIsDistExecFunc(int32_t funcId) {
×
UNCOV
458
  if (fmIsUserDefinedFunc(funcId)) {
×
UNCOV
459
    return false;
×
460
  }
UNCOV
461
  if (!fmIsVectorFunc(funcId)) {
×
462
    return true;
×
463
  }
UNCOV
464
  return (NULL != funcMgtBuiltins[funcId].pPartialFunc && NULL != funcMgtBuiltins[funcId].pMergeFunc);
×
465
}
466

UNCOV
467
static int32_t createPartialFunction(const SFunctionNode* pSrcFunc, SFunctionNode** pPartialFunc) {
×
UNCOV
468
  SNodeList* pParameterList = NULL;
×
UNCOV
469
  int32_t    code = nodesCloneList(pSrcFunc->pParameterList, &pParameterList);
×
UNCOV
470
  if (NULL == pParameterList) {
×
471
    return code;
×
472
  }
473
  code =
UNCOV
474
      createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pPartialFunc, pSrcFunc, pParameterList, pPartialFunc);
×
UNCOV
475
  if (TSDB_CODE_SUCCESS != code) {
×
476
    nodesDestroyList(pParameterList);
×
477
    return code;
×
478
  }
UNCOV
479
  (*pPartialFunc)->hasOriginalFunc = true;
×
UNCOV
480
  (*pPartialFunc)->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
×
UNCOV
481
  char name[TSDB_FUNC_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_POINTER_PRINT_BYTES + 1] = {0};
×
482

UNCOV
483
  int32_t len = tsnprintf(name, sizeof(name), "%s.%p", (*pPartialFunc)->functionName, pSrcFunc);
×
UNCOV
484
  if (taosHashBinary(name, len) < 0) {
×
485
    return TSDB_CODE_FAILED;
×
486
  }
UNCOV
487
  tstrncpy((*pPartialFunc)->node.aliasName, name, TSDB_COL_NAME_LEN);
×
UNCOV
488
  return TSDB_CODE_SUCCESS;
×
489
}
490

UNCOV
491
static int32_t createMergeFuncPara(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
×
492
                                   SNodeList** pParameterList) {
UNCOV
493
  SNode*  pRes = NULL;
×
UNCOV
494
  int32_t code = createColumnByFunc(pPartialFunc, (SColumnNode**)&pRes);
×
UNCOV
495
  if (TSDB_CODE_SUCCESS != code) {
×
496
    return code;
×
497
  }
UNCOV
498
  if (NULL != funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc) {
×
UNCOV
499
    return funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc(pSrcFunc->pParameterList, pRes, pParameterList);
×
500
  } else {
UNCOV
501
    return nodesListMakeStrictAppend(pParameterList, pRes);
×
502
  }
503
}
504

UNCOV
505
static int32_t createMidFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
×
506
                                 SFunctionNode** pMidFunc) {
UNCOV
507
  SNodeList*     pParameterList = NULL;
×
UNCOV
508
  SFunctionNode* pFunc = NULL;
×
509

UNCOV
510
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
×
UNCOV
511
  if (TSDB_CODE_SUCCESS == code) {
×
UNCOV
512
    if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){
×
UNCOV
513
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pSrcFunc, pParameterList, &pFunc);
×
514
    }else{
UNCOV
515
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
×
516
    }
517
  }
UNCOV
518
  if (TSDB_CODE_SUCCESS == code) {
×
UNCOV
519
    tstrncpy(pFunc->node.aliasName, pPartialFunc->node.aliasName, TSDB_COL_NAME_LEN);
×
520
  }
521

UNCOV
522
  if (TSDB_CODE_SUCCESS == code) {
×
UNCOV
523
    *pMidFunc = pFunc;
×
524
  } else {
525
    nodesDestroyList(pParameterList);
×
526
  }
UNCOV
527
  return code;
×
528
}
529

UNCOV
530
static int32_t createMergeFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
×
531
                                   SFunctionNode** pMergeFunc) {
UNCOV
532
  SNodeList*     pParameterList = NULL;
×
UNCOV
533
  SFunctionNode* pFunc = NULL;
×
534

UNCOV
535
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
×
UNCOV
536
  if (TSDB_CODE_SUCCESS == code) {
×
UNCOV
537
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
×
538
  }
UNCOV
539
  if (TSDB_CODE_SUCCESS == code) {
×
UNCOV
540
    pFunc->hasOriginalFunc = true;
×
UNCOV
541
    pFunc->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
×
542
    // overwrite function restype set by translate function
UNCOV
543
    if (fmIsSameInOutType(pSrcFunc->funcId)) {
×
UNCOV
544
      pFunc->node.resType = pSrcFunc->node.resType;
×
545
    }
UNCOV
546
    tstrncpy(pFunc->node.aliasName, pSrcFunc->node.aliasName, TSDB_COL_NAME_LEN);
×
547
  }
548

UNCOV
549
  if (TSDB_CODE_SUCCESS == code) {
×
UNCOV
550
    *pMergeFunc = pFunc;
×
551
  } else {
552
    nodesDestroyList(pParameterList);
×
553
  }
UNCOV
554
  return code;
×
555
}
556

UNCOV
557
int32_t fmGetDistMethod(const SFunctionNode* pFunc, SFunctionNode** pPartialFunc, SFunctionNode** pMidFunc,
×
558
                        SFunctionNode** pMergeFunc) {
UNCOV
559
  if (!fmIsDistExecFunc(pFunc->funcId)) {
×
560
    return TSDB_CODE_FAILED;
×
561
  }
562

UNCOV
563
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
×
UNCOV
564
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
×
UNCOV
565
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
×
566
  }
UNCOV
567
  if (TSDB_CODE_SUCCESS == code) {
×
UNCOV
568
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
×
569
  }
570

UNCOV
571
  if (TSDB_CODE_SUCCESS != code) {
×
572
    nodesDestroyNode((SNode*)*pPartialFunc);
×
573
    if (pMidFunc) nodesDestroyNode((SNode*)*pMidFunc);
×
574
    nodesDestroyNode((SNode*)*pMergeFunc);
×
575
  }
576

UNCOV
577
  return code;
×
578
}
579

UNCOV
580
char* fmGetFuncName(int32_t funcId) {
×
UNCOV
581
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
582
    return taosStrdup("invalid function");
×
583
  }
UNCOV
584
  return taosStrdup(funcMgtBuiltins[funcId].name);
×
585
}
586

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

UNCOV
605
bool fmIsTSMASupportedFunc(func_id_t funcId) {
×
UNCOV
606
  return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC) &&
×
UNCOV
607
         !isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_STREAM_FUNC);
×
608
}
609

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

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

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

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

UNCOV
692
bool fmIsMyStateFunc(int32_t funcId, int32_t stateFuncId) {
×
UNCOV
693
  const SBuiltinFuncDefinition* pFunc = &funcMgtBuiltins[funcId];
×
UNCOV
694
  const SBuiltinFuncDefinition* pStateFunc = &funcMgtBuiltins[stateFuncId];
×
UNCOV
695
  if (!pFunc->pStateFunc) {
×
696
    return false;
×
697
  }
UNCOV
698
  if (strcmp(pFunc->pStateFunc, pStateFunc->name) == 0) return true;
×
UNCOV
699
  int32_t stateMergeFuncId = fmGetFuncId(pFunc->pStateFunc);
×
UNCOV
700
  if (stateMergeFuncId == -1) {
×
701
    return false;
×
702
  }
UNCOV
703
  const SBuiltinFuncDefinition* pStateMergeFunc = &funcMgtBuiltins[stateMergeFuncId];
×
UNCOV
704
  return strcmp(pStateFunc->name, pStateMergeFunc->pMergeFunc) == 0;
×
705
}
706

UNCOV
707
bool fmIsCountLikeFunc(int32_t funcId) {
×
UNCOV
708
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
×
709
}
710

UNCOV
711
bool fmIsRowTsOriginFunc(int32_t funcId) {
×
UNCOV
712
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
713
    return false;
×
714
  }
UNCOV
715
  return FUNCTION_TYPE_IROWTS_ORIGIN == funcMgtBuiltins[funcId].type;
×
716
}
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