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

taosdata / TDengine / #3657

14 Mar 2025 08:10AM UTC coverage: 62.877% (+0.04%) from 62.841%
#3657

push

travis-ci

web-flow
feat(keep): support keep on super table level. (#30097)

* Feat: support use keep while create super table.

* Test(keep): add test for create super table with keep option.

* Feat(keep): Add tmsg for create keep.

* Feat(keep): support alter table option keep.

* Fix(keep): Add baisc test for alter table option.

* Fix(keep): memory leek.

* Feat(keep): add keep to metaEntry&metaCache and fix earliestTs with stn keep.

* Test(keep): add some cases for select with stb keep.

* Fix: fix ci core while alter stb.

* Feat(keep): delete expired data in super table level.

* Feat: remove get stb keep while query.

* Fix : build error.

* Revert "Fix : build error."

This reverts commit 0ed66e4e8.

* Revert "Feat(keep): delete expired data in super table level."

This reverts commit 36330f6b4.

* Fix : build errors.

* Feat : support restart taosd.

* Fix : alter table comment problems.

* Test : add tests for super table keep.

* Fix: change sdb stb reserve size.

* Test: add more tests.

* Feat: Disable normal tables and sub tables from setting the keep parameter

* Fix: add more checks to avoid unknown address.

* Docs: Add docs for stable keep.

* Fix: some review changes.

* Fix: review errors.

147792 of 302527 branches covered (48.85%)

Branch coverage included in aggregate %.

88 of 99 new or added lines in 12 files covered. (88.89%)

3160 existing lines in 17 files now uncovered.

232856 of 302857 relevant lines covered (76.89%)

6602224.33 hits per line

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

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

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

51
static bool isSpecificClassifyFunc(int32_t funcId, uint64_t classification) {
168,718,319✔
52
  if (fmIsUserDefinedFunc(funcId)) {
168,718,319✔
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);
56,666✔
56
  }
57
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
168,665,732!
58
    return false;
19,204,162✔
59
  }
60
  return FUNC_MGT_TEST_MASK(funcMgtBuiltins[funcId].classification, classification);
149,461,570✔
61
}
62

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

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

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

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

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

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

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

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

129
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
107,926✔
130
    return FUNC_DATA_REQUIRED_DATA_LOAD;
105,268✔
131
  } else {
132
    return funcMgtBuiltins[funcId].dynDataRequiredFunc(pRes, pBlockInfo);
2,658✔
133
  }
134
}
135

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

150
int32_t fmGetUdafExecFuncs(int32_t funcId, SFuncExecFuncs* pFpSet) {
32✔
151
#ifdef USE_UDF
152
  if (!fmIsUserDefinedFunc(funcId)) {
32!
153
    return TSDB_CODE_FAILED;
×
154
  }
155
  pFpSet->getEnv = udfAggGetEnv;
32✔
156
  pFpSet->init = udfAggInit;
32✔
157
  pFpSet->process = udfAggProcess;
32✔
158
  pFpSet->finalize = udfAggFinalize;
32✔
159
  return TSDB_CODE_SUCCESS;
32✔
160
#else
161
  TAOS_RETURN(TSDB_CODE_OPS_NOT_SUPPORT);
162
#endif
163
}
164

165
int32_t fmGetScalarFuncExecFuncs(int32_t funcId, SScalarFuncExecFuncs* pFpSet) {
6,298,675✔
166
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
6,298,675!
167
    return TSDB_CODE_FAILED;
×
168
  }
169
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
6,298,499✔
170
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
6,298,499✔
171
  return TSDB_CODE_SUCCESS;
6,298,499✔
172
}
173

174
bool fmIsAggFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_AGG_FUNC); }
12,207,165✔
175

176
bool fmIsScalarFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCALAR_FUNC); }
12,024,137✔
177

178
bool fmIsVectorFunc(int32_t funcId) { return !fmIsScalarFunc(funcId) && !fmIsPseudoColumnFunc(funcId); }
5,969,302✔
179

180
bool fmIsSelectColsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_COLS_FUNC); }
445,794✔
181

182
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
8,837,294✔
183

184
bool fmIsTimelineFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TIMELINE_FUNC); }
5,692,546✔
185

186
bool fmIsDateTimeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_DATETIME_FUNC); }
2,878,691✔
187

188
bool fmIsPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PSEUDO_COLUMN_FUNC); }
11,704,478✔
189

190
bool fmIsScanPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCAN_PC_FUNC); }
10,385,235✔
191

192
bool fmIsWindowPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_WINDOW_PC_FUNC); }
7,113,516✔
193

194
bool fmIsWindowClauseFunc(int32_t funcId) { return fmIsAggFunc(funcId) || fmIsWindowPseudoColumnFunc(funcId); }
414,601✔
195

196
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
9,861,329✔
197

198
bool fmIsSpecialDataRequiredFunc(int32_t funcId) {
569,913✔
199
  return isSpecificClassifyFunc(funcId, FUNC_MGT_SPECIAL_DATA_REQUIRED);
569,913✔
200
}
201

202
bool fmIsDynamicScanOptimizedFunc(int32_t funcId) {
189,075✔
203
  return isSpecificClassifyFunc(funcId, FUNC_MGT_DYNAMIC_SCAN_OPTIMIZED);
189,075✔
204
}
205

206
bool fmIsMultiResFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_RES_FUNC); }
5,734,214✔
207

208
bool fmIsRepeatScanFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_REPEAT_SCAN_FUNC); }
5,753,269✔
209

210
bool fmIsUserDefinedFunc(int32_t funcId) { return funcId > FUNC_UDF_ID_START; }
190,188,796✔
211

212
bool fmIsForbidFillFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_FILL_FUNC); }
2,888,440✔
213

214
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
821,504✔
215

216
bool fmIsForbidStreamFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_STREAM_FUNC); }
2,879,646✔
217

218
bool fmIsSystemInfoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SYSTEM_INFO_FUNC); }
2,908,113✔
219

220
bool fmIsImplicitTsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IMPLICIT_TS_FUNC); }
33,165,634✔
221

222
bool fmIsClientPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CLIENT_PC_FUNC); }
2,905,391✔
223

224
bool fmIsMultiRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_ROWS_FUNC); }
5,735,320✔
225

226
bool fmIsKeepOrderFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_KEEP_ORDER_FUNC); }
1,105,373✔
227

228
bool fmIsCumulativeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CUMULATIVE_FUNC); }
461,091✔
229

230
bool fmIsForbidSysTableFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_SYSTABLE_FUNC); }
2,879,643✔
231

232
bool fmIsInterpFunc(int32_t funcId) {
5,820,461✔
233
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
5,820,461!
234
    return false;
413✔
235
  }
236
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
5,820,048✔
237
}
238

239
bool fmIsInterpPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERP_PC_FUNC); }
5,735,266✔
240

241
bool fmIsForecastFunc(int32_t funcId) {
5,657,294✔
242
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
5,657,294!
243
    return false;
417✔
244
  }
245
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
5,656,877✔
246
}
247

248
bool fmIsForecastPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORECAST_PC_FUNC); }
5,745,518✔
249

250
bool fmIsLastRowFunc(int32_t funcId) {
101,610✔
251
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
101,610!
252
    return false;
×
253
  }
254
  return FUNCTION_TYPE_LAST_ROW == funcMgtBuiltins[funcId].type;
101,613✔
255
}
256

257
bool fmIsNotNullOutputFunc(int32_t funcId) {
2,633,956✔
258
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,633,956!
UNCOV
259
    return false;
×
260
  }
261
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
5,193,566✔
262
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
2,559,579✔
263
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
2,544,660✔
264
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
2,538,565✔
265
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
2,497,486✔
266
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
2,485,918✔
267
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
2,481,516✔
268
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
2,101,728✔
269
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
7,234,332✔
270
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
2,040,766✔
271
}
272

273
bool fmIsSelectValueFunc(int32_t funcId) {
733,016✔
274
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
733,016!
275
    return false;
×
276
  }
277
  return FUNCTION_TYPE_SELECT_VALUE == funcMgtBuiltins[funcId].type;
733,016✔
278
}
279

280
bool fmIsGroupKeyFunc(int32_t funcId) {
612,396✔
281
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
612,396!
282
    return false;
×
283
  }
284
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
612,413✔
285
}
286

287
bool fmisSelectGroupConstValueFunc(int32_t funcId) {
75✔
288
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
75!
289
    return false;
×
290
  }
291
  return FUNCTION_TYPE_GROUP_CONST_VALUE == funcMgtBuiltins[funcId].type;
75✔
292
}
293

294
bool fmIsElapsedFunc(int32_t funcId) {
33,019✔
295
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
33,019!
296
    return false;
×
297
  }
298
  return FUNCTION_TYPE_ELAPSED == funcMgtBuiltins[funcId].type;
33,019✔
299
}
300

301
bool fmIsBlockDistFunc(int32_t funcId) {
2,878,687✔
302
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,878,687!
303
    return false;
204✔
304
  }
305
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
2,878,483✔
306
}
307

308
bool fmIsDBUsageFunc(int32_t funcId) {
2,878,689✔
309
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,878,689!
310
    return false;
206✔
311
  }
312
  return FUNCTION_TYPE_DB_USAGE == funcMgtBuiltins[funcId].type;
2,878,483✔
313
}
314

315
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
3,251,802✔
316

317
bool fmIsIgnoreNullFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IGNORE_NULL_FUNC); }
2,676✔
318

319
void fmFuncMgtDestroy() {
2,792✔
320
  void* m = gFunMgtService.pFuncNameHashTable;
2,792✔
321
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
2,792!
322
    taosHashCleanup(m);
2,791✔
323
  }
324
}
2,792✔
325

326
#ifdef BUILD_NO_CALL
327
int32_t fmSetInvertFunc(int32_t funcId, SFuncExecFuncs* pFpSet) {
328
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
329
    return TSDB_CODE_FAILED;
330
  }
331
  pFpSet->process = funcMgtBuiltins[funcId].invertFunc;
332
  return TSDB_CODE_SUCCESS;
333
}
334

335
int32_t fmSetNormalFunc(int32_t funcId, SFuncExecFuncs* pFpSet) {
336
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
337
    return TSDB_CODE_FAILED;
338
  }
339
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
340
  return TSDB_CODE_SUCCESS;
341
}
342

343
bool fmIsInvertible(int32_t funcId) {
344
  bool res = false;
345
  switch (funcMgtBuiltins[funcId].type) {
346
    case FUNCTION_TYPE_COUNT:
347
    case FUNCTION_TYPE_SUM:
348
    case FUNCTION_TYPE_STDDEV:
349
    case FUNCTION_TYPE_AVG:
350
    case FUNCTION_TYPE_WSTART:
351
    case FUNCTION_TYPE_WEND:
352
    case FUNCTION_TYPE_WDURATION:
353
      res = true;
354
      break;
355
    default:
356
      break;
357
  }
358
  return res;
359
}
360
#endif
361

362
// function has same input/output type
363
bool fmIsSameInOutType(int32_t funcId) {
327,830✔
364
  bool res = false;
327,830✔
365
  switch (funcMgtBuiltins[funcId].type) {
327,830✔
366
    case FUNCTION_TYPE_MAX:
111,381✔
367
    case FUNCTION_TYPE_MIN:
368
    case FUNCTION_TYPE_TOP:
369
    case FUNCTION_TYPE_BOTTOM:
370
    case FUNCTION_TYPE_FIRST:
371
    case FUNCTION_TYPE_LAST:
372
    case FUNCTION_TYPE_SAMPLE:
373
    case FUNCTION_TYPE_TAIL:
374
    case FUNCTION_TYPE_UNIQUE:
375
      res = true;
111,381✔
376
      break;
111,381✔
377
    default:
216,449✔
378
      break;
216,449✔
379
  }
380
  return res;
327,830✔
381
}
382

383
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
63,508✔
384
  SNode* pNode;
385
  FOREACH(pNode, pFunc->pParameterList) {
64,138✔
386
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
3,663✔
387
      return false;
3,033✔
388
    }
389
  }
390
  return true;
60,475✔
391
}
392

393
bool fmIsSkipScanCheckFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SKIP_SCAN_CHECK_FUNC); }
189,067✔
394

395
bool fmIsPrimaryKeyFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PRIMARY_KEY_FUNC); }
2,494,354✔
396
void getLastCacheDataType(SDataType* pType, int32_t pkBytes) {
2,594✔
397
  // TODO: do it later.
398
  pType->bytes = getFirstLastInfoSize(pType->bytes, pkBytes) + VARSTR_HEADER_SIZE;
2,594✔
399
  pType->type = TSDB_DATA_TYPE_BINARY;
2,594✔
400
}
2,594✔
401

402
static int32_t getFuncInfo(SFunctionNode* pFunc) {
1,044,247✔
403
  char msg[128] = {0};
1,044,247✔
404
  return fmGetFuncInfo(pFunc, msg, sizeof(msg));
1,044,247✔
405
}
406

407
int32_t createFunction(const char* pName, SNodeList* pParameterList, SFunctionNode** ppFunc) {
110,910✔
408
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
110,910✔
409
  if (NULL == *ppFunc) {
110,910!
410
    return code;
×
411
  }
412
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
110,910✔
413
  (*ppFunc)->pParameterList = pParameterList;
110,910✔
414
  code = getFuncInfo((*ppFunc));
110,910✔
415
  if (TSDB_CODE_SUCCESS != code) {
110,910!
416
    (*ppFunc)->pParameterList = NULL;
×
417
    nodesDestroyNode((SNode*)*ppFunc);
×
418
    *ppFunc = NULL;
×
419
    return code;
×
420
  }
421
  return code;
110,910✔
422
}
423

424
static void resetOutputChangedFunc(SFunctionNode *pFunc, const SFunctionNode* pSrcFunc) {
933,337✔
425
  if (funcMgtBuiltins[pFunc->funcId].type == FUNCTION_TYPE_LAST_MERGE) {
933,337✔
426
    pFunc->node.resType = pSrcFunc->node.resType;
13,448✔
427
    return;
13,448✔
428
  }
429
}
430

431
int32_t createFunctionWithSrcFunc(const char* pName, const SFunctionNode* pSrcFunc, SNodeList* pParameterList, SFunctionNode** ppFunc) {
933,337✔
432
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
933,337✔
433
  if (NULL == *ppFunc) {
933,337!
434
    return code;
×
435
  }
436

437
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
933,337✔
438
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
933,337✔
439

440
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
933,337✔
441
  (*ppFunc)->pParameterList = pParameterList;
933,337✔
442
  code = getFuncInfo((*ppFunc));
933,337✔
443
  if (TSDB_CODE_SUCCESS != code) {
933,337!
444
    (*ppFunc)->pParameterList = NULL;
×
445
    nodesDestroyNode((SNode*)*ppFunc);
×
446
    *ppFunc = NULL;
×
447
    return code;
×
448
  }
449
  resetOutputChangedFunc(*ppFunc, pSrcFunc);
933,337✔
450
  (*ppFunc)->node.relatedTo = pSrcFunc->node.relatedTo;
933,337✔
451
  (*ppFunc)->node.bindExprID = pSrcFunc->node.bindExprID;
933,337✔
452
  return code;
933,337✔
453
}
454

455
static int32_t createColumnByFunc(const SFunctionNode* pFunc, SColumnNode** ppCol) {
605,507✔
456
  int32_t code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)ppCol);
605,507✔
457
  if (NULL == *ppCol) {
605,507!
458
    return code;
×
459
  }
460
  tstrncpy((*ppCol)->colName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
605,507✔
461
  (*ppCol)->node.resType = pFunc->node.resType;
605,507✔
462
  return TSDB_CODE_SUCCESS;
605,507✔
463
}
464

465
bool fmIsDistExecFunc(int32_t funcId) {
1,509,600✔
466
  if (fmIsUserDefinedFunc(funcId)) {
1,509,600✔
467
    return false;
24✔
468
  }
469
  if (!fmIsVectorFunc(funcId)) {
1,509,579!
470
    return true;
×
471
  }
472
  return (NULL != funcMgtBuiltins[funcId].pPartialFunc && NULL != funcMgtBuiltins[funcId].pMergeFunc);
1,509,581!
473
}
474

475
static int32_t createPartialFunction(const SFunctionNode* pSrcFunc, SFunctionNode** pPartialFunc) {
327,830✔
476
  SNodeList* pParameterList = NULL;
327,830✔
477
  int32_t    code = nodesCloneList(pSrcFunc->pParameterList, &pParameterList);
327,830✔
478
  if (NULL == pParameterList) {
327,830!
479
    return code;
×
480
  }
481
  code =
482
      createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pPartialFunc, pSrcFunc, pParameterList, pPartialFunc);
327,830✔
483
  if (TSDB_CODE_SUCCESS != code) {
327,830!
484
    nodesDestroyList(pParameterList);
×
485
    return code;
×
486
  }
487
  (*pPartialFunc)->hasOriginalFunc = true;
327,830✔
488
  (*pPartialFunc)->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
327,830✔
489
  char name[TSDB_FUNC_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_POINTER_PRINT_BYTES + 1] = {0};
327,830✔
490

491
  int32_t len = tsnprintf(name, sizeof(name), "%s.%p", (*pPartialFunc)->functionName, pSrcFunc);
327,830✔
492
  if (taosHashBinary(name, len) < 0) {
327,830!
493
    return TSDB_CODE_FAILED;
×
494
  }
495
  tstrncpy((*pPartialFunc)->node.aliasName, name, TSDB_COL_NAME_LEN);
327,830✔
496
  return TSDB_CODE_SUCCESS;
327,830✔
497
}
498

499
static int32_t createMergeFuncPara(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
605,507✔
500
                                   SNodeList** pParameterList) {
501
  SNode*  pRes = NULL;
605,507✔
502
  int32_t code = createColumnByFunc(pPartialFunc, (SColumnNode**)&pRes);
605,507✔
503
  if (TSDB_CODE_SUCCESS != code) {
605,507!
504
    return code;
×
505
  }
506
  if (NULL != funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc) {
605,507✔
507
    return funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc(pSrcFunc->pParameterList, pRes, pParameterList);
4,520✔
508
  } else {
509
    return nodesListMakeStrictAppend(pParameterList, pRes);
600,987✔
510
  }
511
}
512

513
static int32_t createMidFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
277,677✔
514
                                 SFunctionNode** pMidFunc) {
515
  SNodeList*     pParameterList = NULL;
277,677✔
516
  SFunctionNode* pFunc = NULL;
277,677✔
517

518
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
277,677✔
519
  if (TSDB_CODE_SUCCESS == code) {
277,677!
520
    if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){
277,677✔
521
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pSrcFunc, pParameterList, &pFunc);
19,696✔
522
    }else{
523
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
257,981✔
524
    }
525
  }
526
  if (TSDB_CODE_SUCCESS == code) {
277,677!
527
    tstrncpy(pFunc->node.aliasName, pPartialFunc->node.aliasName, TSDB_COL_NAME_LEN);
277,677✔
528
  }
529

530
  if (TSDB_CODE_SUCCESS == code) {
277,677!
531
    *pMidFunc = pFunc;
277,677✔
532
  } else {
533
    nodesDestroyList(pParameterList);
×
534
  }
535
  return code;
277,677✔
536
}
537

538
static int32_t createMergeFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
327,830✔
539
                                   SFunctionNode** pMergeFunc) {
540
  SNodeList*     pParameterList = NULL;
327,830✔
541
  SFunctionNode* pFunc = NULL;
327,830✔
542

543
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
327,830✔
544
  if (TSDB_CODE_SUCCESS == code) {
327,830!
545
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
327,830✔
546
  }
547
  if (TSDB_CODE_SUCCESS == code) {
327,830!
548
    pFunc->hasOriginalFunc = true;
327,830✔
549
    pFunc->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
327,830✔
550
    // overwrite function restype set by translate function
551
    if (fmIsSameInOutType(pSrcFunc->funcId)) {
327,830✔
552
      pFunc->node.resType = pSrcFunc->node.resType;
111,381✔
553
    }
554
    tstrncpy(pFunc->node.aliasName, pSrcFunc->node.aliasName, TSDB_COL_NAME_LEN);
327,830✔
555
  }
556

557
  if (TSDB_CODE_SUCCESS == code) {
327,830!
558
    *pMergeFunc = pFunc;
327,830✔
559
  } else {
560
    nodesDestroyList(pParameterList);
×
561
  }
562
  return code;
327,830✔
563
}
564

565
int32_t fmGetDistMethod(const SFunctionNode* pFunc, SFunctionNode** pPartialFunc, SFunctionNode** pMidFunc,
327,830✔
566
                        SFunctionNode** pMergeFunc) {
567
  if (!fmIsDistExecFunc(pFunc->funcId)) {
327,830!
568
    return TSDB_CODE_FAILED;
×
569
  }
570

571
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
327,830✔
572
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
327,830!
573
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
277,677✔
574
  }
575
  if (TSDB_CODE_SUCCESS == code) {
327,830!
576
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
327,830✔
577
  }
578

579
  if (TSDB_CODE_SUCCESS != code) {
327,830!
580
    nodesDestroyNode((SNode*)*pPartialFunc);
×
581
    if (pMidFunc) nodesDestroyNode((SNode*)*pMidFunc);
×
582
    nodesDestroyNode((SNode*)*pMergeFunc);
×
583
  }
584

585
  return code;
327,830✔
586
}
587

588
char* fmGetFuncName(int32_t funcId) {
2✔
589
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2!
590
    return taosStrdup("invalid function");
×
591
  }
592
  return taosStrdup(funcMgtBuiltins[funcId].name);
2!
593
}
594

595
/// @param [out] pStateFunc, not changed if error occured or no need to create state func
596
/// @retval 0 for succ, otherwise err occured
597
static int32_t fmCreateStateFunc(const SFunctionNode* pFunc, SFunctionNode** pStateFunc) {
33,072✔
598
  if (funcMgtBuiltins[pFunc->funcId].pStateFunc) {
33,072!
599
    SNodeList* pParams = NULL;
33,072✔
600
    int32_t    code = nodesCloneList(pFunc->pParameterList, &pParams);
33,072✔
601
    if (!pParams) return code;
33,072!
602
    code = createFunction(funcMgtBuiltins[pFunc->funcId].pStateFunc, pParams, pStateFunc);
33,072✔
603
    if (TSDB_CODE_SUCCESS != code) {
33,072!
604
      nodesDestroyList(pParams);
×
605
      return code;
×
606
    }
607
    tstrncpy((*pStateFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
33,072✔
608
    tstrncpy((*pStateFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
33,072✔
609
  }
610
  return TSDB_CODE_SUCCESS;
33,072✔
611
}
612

613
bool fmIsTSMASupportedFunc(func_id_t funcId) {
629,602✔
614
  return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC) &&
1,236,537✔
615
         !isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_STREAM_FUNC);
606,935!
616
}
617

618
int32_t fmCreateStateFuncs(SNodeList* pFuncs) {
152✔
619
  int32_t code;
620
  SNode*  pNode;
621
  char    buf[128] = {0};
152✔
622
  FOREACH(pNode, pFuncs) {
33,224!
623
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
33,072✔
624
    code = fmGetFuncInfo(pFunc, buf, 128);
33,072✔
625
    if (code) break;
33,072!
626
    if (fmIsTSMASupportedFunc(pFunc->funcId)) {
33,072!
627
      SFunctionNode* pNewFunc = NULL;
33,072✔
628
      code = fmCreateStateFunc(pFunc, &pNewFunc);
33,072✔
629
      if (code) {
33,072!
630
        // error
631
        break;
×
632
      } else if (!pNewFunc) {
33,072!
633
        // no need state func
634
        continue;
×
635
      } else {
636
        REPLACE_NODE(pNewFunc);
33,072✔
637
        nodesDestroyNode(pNode);
33,072✔
638
      }
639
    }
640
  }
641
  return code;
152✔
642
}
643

644
static int32_t fmCreateStateMergeFunc(SFunctionNode* pFunc, SFunctionNode** pStateMergeFunc) {
428✔
645
  if (funcMgtBuiltins[pFunc->funcId].pMergeFunc) {
428✔
646
    SNodeList* pParams = NULL;
416✔
647
    int32_t    code = nodesCloneList(pFunc->pParameterList, &pParams);
416✔
648
    if (!pParams) return code;
416!
649
    code = createFunction(funcMgtBuiltins[pFunc->funcId].pMergeFunc, pParams, pStateMergeFunc);
416✔
650
    if (TSDB_CODE_SUCCESS != code) {
416!
651
      nodesDestroyList(pParams);
×
652
      return code;
×
653
    }
654
    tstrncpy((*pStateMergeFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
416✔
655
    tstrncpy((*pStateMergeFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
416✔
656
  }
657
  return TSDB_CODE_SUCCESS;
428✔
658
}
659

660
int32_t fmCreateStateMergeFuncs(SNodeList* pFuncs) {
92✔
661
  int32_t code;
662
  SNode*  pNode;
663
  char    buf[128] = {0};
92✔
664
  FOREACH(pNode, pFuncs) {
520!
665
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
428✔
666
    if (fmIsTSMASupportedFunc(pFunc->funcId)) {
428!
667
      SFunctionNode* pNewFunc = NULL;
428✔
668
      code = fmCreateStateMergeFunc(pFunc, &pNewFunc);
428✔
669
      if (code) {
428!
670
        // error
671
        break;
×
672
      } else if (!pNewFunc) {
428✔
673
        // no state merge func
674
        continue;
12✔
675
      } else {
676
        REPLACE_NODE(pNewFunc);
416✔
677
        nodesDestroyNode(pNode);
416✔
678
      }
679
    }
680
  }
681
  return code;
92✔
682
}
683

684
int32_t fmGetFuncId(const char* name) {
84,302✔
685
  if (NULL != gFunMgtService.pFuncNameHashTable) {
84,302!
686
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, name, strlen(name));
84,302✔
687
    if (NULL != pVal) {
84,302!
688
      return *(int32_t*)pVal;
84,302✔
689
    }
690
    return -1;
×
691
  }
692
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
×
693
    if (0 == strcmp(funcMgtBuiltins[i].name, name)) {
×
694
      return i;
×
695
    }
696
  }
697
  return -1;
×
698
}
699

700
bool fmIsMyStateFunc(int32_t funcId, int32_t stateFuncId) {
16,724,894✔
701
  const SBuiltinFuncDefinition* pFunc = &funcMgtBuiltins[funcId];
16,724,894✔
702
  const SBuiltinFuncDefinition* pStateFunc = &funcMgtBuiltins[stateFuncId];
16,724,894✔
703
  if (!pFunc->pStateFunc) {
16,724,894!
704
    return false;
×
705
  }
706
  if (strcmp(pFunc->pStateFunc, pStateFunc->name) == 0) return true;
16,724,894✔
707
  int32_t stateMergeFuncId = fmGetFuncId(pFunc->pStateFunc);
82,642✔
708
  if (stateMergeFuncId == -1) {
82,642!
709
    return false;
×
710
  }
711
  const SBuiltinFuncDefinition* pStateMergeFunc = &funcMgtBuiltins[stateMergeFuncId];
82,642✔
712
  return strcmp(pStateFunc->name, pStateMergeFunc->pMergeFunc) == 0;
82,642✔
713
}
714

715
bool fmIsCountLikeFunc(int32_t funcId) {
970,621✔
716
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
970,621✔
717
}
718

719
bool fmIsRowTsOriginFunc(int32_t funcId) {
17,101✔
720
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
17,101!
721
    return false;
×
722
  }
723
  return FUNCTION_TYPE_IROWTS_ORIGIN == funcMgtBuiltins[funcId].type;
17,101✔
724
}
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