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

taosdata / TDengine / #3653

14 Mar 2025 08:10AM UTC coverage: 22.565% (-41.0%) from 63.596%
#3653

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.

49248 of 302527 branches covered (16.28%)

Branch coverage included in aggregate %.

53 of 99 new or added lines in 12 files covered. (53.54%)

155872 existing lines in 443 files now uncovered.

87359 of 302857 relevant lines covered (28.84%)

570004.22 hits per line

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

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

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

51
static bool isSpecificClassifyFunc(int32_t funcId, uint64_t classification) {
47,264✔
52
  if (fmIsUserDefinedFunc(funcId)) {
47,264✔
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);
268✔
56
  }
57
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
46,996!
UNCOV
58
    return false;
×
59
  }
60
  return FUNC_MGT_TEST_MASK(funcMgtBuiltins[funcId].classification, classification);
46,996✔
61
}
62

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

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

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

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

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

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

UNCOV
118
EFuncDataRequired fmFuncDynDataRequired(int32_t funcId, void* pRes, SDataBlockInfo* pBlockInfo) {
×
UNCOV
119
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
UNCOV
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) {
×
UNCOV
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) {
×
151
#ifdef USE_UDF
152
  if (!fmIsUserDefinedFunc(funcId)) {
×
UNCOV
153
    return TSDB_CODE_FAILED;
×
154
  }
UNCOV
155
  pFpSet->getEnv = udfAggGetEnv;
×
UNCOV
156
  pFpSet->init = udfAggInit;
×
UNCOV
157
  pFpSet->process = udfAggProcess;
×
UNCOV
158
  pFpSet->finalize = udfAggFinalize;
×
UNCOV
159
  return TSDB_CODE_SUCCESS;
×
160
#else
161
  TAOS_RETURN(TSDB_CODE_OPS_NOT_SUPPORT);
162
#endif
163
}
164

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

174
bool fmIsAggFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_AGG_FUNC); }
3,075✔
175

176
bool fmIsScalarFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCALAR_FUNC); }
4,297✔
177

178
bool fmIsVectorFunc(int32_t funcId) { return !fmIsScalarFunc(funcId) && !fmIsPseudoColumnFunc(funcId); }
2,378✔
179

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

182
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
1,639✔
183

184
bool fmIsTimelineFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TIMELINE_FUNC); }
2,406✔
185

186
bool fmIsDateTimeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_DATETIME_FUNC); }
1,242✔
187

188
bool fmIsPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PSEUDO_COLUMN_FUNC); }
2,289✔
189

190
bool fmIsScanPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCAN_PC_FUNC); }
1,970✔
191

192
bool fmIsWindowPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_WINDOW_PC_FUNC); }
1,612✔
193

194
bool fmIsWindowClauseFunc(int32_t funcId) { return fmIsAggFunc(funcId) || fmIsWindowPseudoColumnFunc(funcId); }
111!
195

196
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
2,582✔
197

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

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

206
bool fmIsMultiResFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_RES_FUNC); }
2,941✔
207

208
bool fmIsRepeatScanFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_REPEAT_SCAN_FUNC); }
2,411✔
209

210
bool fmIsUserDefinedFunc(int32_t funcId) { return funcId > FUNC_UDF_ID_START; }
49,222✔
211

212
bool fmIsForbidFillFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_FILL_FUNC); }
1,262✔
213

UNCOV
214
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
×
215

216
bool fmIsForbidStreamFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_STREAM_FUNC); }
1,254✔
217

218
bool fmIsSystemInfoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SYSTEM_INFO_FUNC); }
1,345✔
219

220
bool fmIsImplicitTsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IMPLICIT_TS_FUNC); }
1,481✔
221

222
bool fmIsClientPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CLIENT_PC_FUNC); }
1,319✔
223

224
bool fmIsMultiRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_ROWS_FUNC); }
2,412✔
225

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

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

230
bool fmIsForbidSysTableFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_SYSTABLE_FUNC); }
1,250✔
231

232
bool fmIsInterpFunc(int32_t funcId) {
2,342✔
233
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,342!
234
    return false;
16✔
235
  }
236
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
2,326✔
237
}
238

239
bool fmIsInterpPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERP_PC_FUNC); }
2,423✔
240

241
bool fmIsForecastFunc(int32_t funcId) {
2,325✔
242
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,325!
243
    return false;
16✔
244
  }
245
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
2,309✔
246
}
247

248
bool fmIsForecastPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORECAST_PC_FUNC); }
2,422✔
249

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

UNCOV
257
bool fmIsNotNullOutputFunc(int32_t funcId) {
×
UNCOV
258
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
UNCOV
259
    return false;
×
260
  }
UNCOV
261
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
×
UNCOV
262
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
×
UNCOV
263
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
×
UNCOV
264
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
×
UNCOV
265
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
×
UNCOV
266
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
×
UNCOV
267
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
×
UNCOV
268
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
×
UNCOV
269
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
×
UNCOV
270
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
×
271
}
272

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

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

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

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

301
bool fmIsBlockDistFunc(int32_t funcId) {
1,242✔
302
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,242!
303
    return false;
8✔
304
  }
305
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
1,234✔
306
}
307

308
bool fmIsDBUsageFunc(int32_t funcId) {
1,242✔
309
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,242!
310
    return false;
8✔
311
  }
312
  return FUNCTION_TYPE_DB_USAGE == funcMgtBuiltins[funcId].type;
1,234✔
313
}
314

315
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
1,166✔
316

UNCOV
317
bool fmIsIgnoreNullFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IGNORE_NULL_FUNC); }
×
318

319
void fmFuncMgtDestroy() {
5✔
320
  void* m = gFunMgtService.pFuncNameHashTable;
5✔
321
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
5!
322
    taosHashCleanup(m);
5✔
323
  }
324
}
5✔
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) {
41✔
364
  bool res = false;
41✔
365
  switch (funcMgtBuiltins[funcId].type) {
41✔
366
    case FUNCTION_TYPE_MAX:
8✔
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;
8✔
376
      break;
8✔
377
    default:
33✔
378
      break;
33✔
379
  }
380
  return res;
41✔
381
}
382

383
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
57✔
384
  SNode* pNode;
385
  FOREACH(pNode, pFunc->pParameterList) {
57!
UNCOV
386
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
×
UNCOV
387
      return false;
×
388
    }
389
  }
390
  return true;
57✔
391
}
392

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

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

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

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

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

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

437
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
123✔
438
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
123✔
439

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

585
  return code;
41✔
586
}
587

UNCOV
588
char* fmGetFuncName(int32_t funcId) {
×
UNCOV
589
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
UNCOV
590
    return taosStrdup("invalid function");
×
591
  }
UNCOV
592
  return taosStrdup(funcMgtBuiltins[funcId].name);
×
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
UNCOV
597
static int32_t fmCreateStateFunc(const SFunctionNode* pFunc, SFunctionNode** pStateFunc) {
×
UNCOV
598
  if (funcMgtBuiltins[pFunc->funcId].pStateFunc) {
×
UNCOV
599
    SNodeList* pParams = NULL;
×
600
    int32_t    code = nodesCloneList(pFunc->pParameterList, &pParams);
×
601
    if (!pParams) return code;
×
UNCOV
602
    code = createFunction(funcMgtBuiltins[pFunc->funcId].pStateFunc, pParams, pStateFunc);
×
UNCOV
603
    if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
604
      nodesDestroyList(pParams);
×
UNCOV
605
      return code;
×
606
    }
UNCOV
607
    tstrncpy((*pStateFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
×
UNCOV
608
    tstrncpy((*pStateFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
×
609
  }
UNCOV
610
  return TSDB_CODE_SUCCESS;
×
611
}
612

UNCOV
613
bool fmIsTSMASupportedFunc(func_id_t funcId) {
×
UNCOV
614
  return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC) &&
×
UNCOV
615
         !isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_STREAM_FUNC);
×
616
}
617

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

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

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

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

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

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

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