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

taosdata / TDengine / #3522

07 Nov 2024 05:59AM UTC coverage: 58.216% (+1.3%) from 56.943%
#3522

push

travis-ci

web-flow
Merge pull request #28663 from taosdata/fix/3_liaohj

fix(stream): stop the underlying scan operations for stream

111884 of 248391 branches covered (45.04%)

Branch coverage included in aggregate %.

3 of 4 new or added lines in 1 file covered. (75.0%)

1164 existing lines in 134 files now uncovered.

191720 of 273118 relevant lines covered (70.2%)

13088725.13 hits per line

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

78.9
/source/libs/function/src/functionMgt.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "functionMgt.h"
17

18
#include "builtins.h"
19
#include "builtinsimpl.h"
20
#include "functionMgtInt.h"
21
#include "taos.h"
22
#include "taoserror.h"
23
#include "thash.h"
24
#include "tudf.h"
25

26
typedef struct SFuncMgtService {
27
  SHashObj* pFuncNameHashTable;
28
} SFuncMgtService;
29

30
static SFuncMgtService gFunMgtService;
31
static TdThreadOnce    functionHashTableInit = PTHREAD_ONCE_INIT;
32
static int32_t         initFunctionCode = 0;
33

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

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

51
static bool isSpecificClassifyFunc(int32_t funcId, uint64_t classification) {
263,507,059✔
52
  if (fmIsUserDefinedFunc(funcId)) {
263,507,059✔
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);
58,086✔
56
  }
57
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
263,456,423!
58
    return false;
60,915,383✔
59
  }
60
  return FUNC_MGT_TEST_MASK(funcMgtBuiltins[funcId].classification, classification);
202,541,040✔
61
}
62

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

68
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
5,125,710✔
69
  if (NULL != gFunMgtService.pFuncNameHashTable) {
5,125,710✔
70
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
5,122,829✔
71
    if (NULL != pVal) {
5,122,878✔
72
      pFunc->funcId = *(int32_t*)pVal;
5,121,283✔
73
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
5,121,283✔
74
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
5,121,283✔
75
    }
76
    return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
1,595✔
77
  }
78
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
55,887!
79
    if (0 == strcmp(funcMgtBuiltins[i].name, pFunc->functionName)) {
55,917✔
80
      pFunc->funcId = i;
2,911✔
81
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
2,911✔
82
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
2,911✔
83
    }
84
  }
85
  return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
×
86
}
87

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

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

100
EFunctionType fmGetFuncType(const char* pFunc) {
3,423,490✔
101
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
3,423,490✔
102
  if (NULL != pVal) {
3,423,516✔
103
    return funcMgtBuiltins[*(int32_t*)pVal].type;
3,421,918✔
104
  }
105
  return FUNCTION_TYPE_UDF;
1,598✔
106
}
107

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

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

123
  const char* name = funcMgtBuiltins[funcId].name;
235,661✔
124
  if ((strcmp(name, "_group_key") == 0) || (strcmp(name, "_select_value") == 0)) {
235,661✔
125
    return FUNC_DATA_REQUIRED_NOT_LOAD;;
87,778✔
126
  }
127

128
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
147,883✔
129
    return FUNC_DATA_REQUIRED_DATA_LOAD;
130,095✔
130
  } else {
131
    return funcMgtBuiltins[funcId].dynDataRequiredFunc(pRes, pBlockInfo);
17,788✔
132
  }
133
}
134

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

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

160
int32_t fmGetScalarFuncExecFuncs(int32_t funcId, SScalarFuncExecFuncs* pFpSet) {
5,833,568✔
161
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
5,833,568!
162
    return TSDB_CODE_FAILED;
801✔
163
  }
164
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
5,833,573✔
165
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
5,833,573✔
166
  return TSDB_CODE_SUCCESS;
5,833,573✔
167
}
168

169
bool fmIsAggFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_AGG_FUNC); }
16,819,728✔
170

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

173
bool fmIsVectorFunc(int32_t funcId) { return !fmIsScalarFunc(funcId) && !fmIsPseudoColumnFunc(funcId); }
7,091,956✔
174

175
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
25,000,512✔
176

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

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

181
bool fmIsPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PSEUDO_COLUMN_FUNC); }
26,191,065✔
182

183
bool fmIsScanPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCAN_PC_FUNC); }
18,669,273✔
184

185
bool fmIsWindowPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_WINDOW_PC_FUNC); }
12,094,373✔
186

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

189
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
11,823,779✔
190

191

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

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

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

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

204
bool fmIsUserDefinedFunc(int32_t funcId) { return funcId > FUNC_UDF_ID_START; }
288,974,804✔
205

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

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

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

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

214
bool fmIsImplicitTsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IMPLICIT_TS_FUNC); }
37,380,273✔
215

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

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

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

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

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

226
bool fmIsInterpFunc(int32_t funcId) {
6,937,706✔
227
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
6,937,706!
228
    return false;
3,100✔
229
  }
230
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
6,934,606✔
231
}
232

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

235
bool fmIsForecastFunc(int32_t funcId) {
6,791,125✔
236
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
6,791,125!
237
    return false;
2,995✔
238
  }
239
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
6,788,130✔
240
}
241

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

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

251
bool fmIsNotNullOutputFunc(int32_t funcId) {
6,758,146✔
252
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
6,758,146!
UNCOV
253
    return false;
×
254
  }
255
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
13,354,380✔
256
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
6,595,197✔
257
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
5,993,947✔
258
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
5,693,170✔
259
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
5,542,962✔
260
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
5,321,037✔
261
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
5,209,596✔
262
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
4,969,387✔
263
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
18,298,859✔
264
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
4,944,479✔
265
}
266

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

274
bool fmIsGroupKeyFunc(int32_t funcId) {
491,878,756✔
275
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
491,878,756!
276
    return false;
×
277
  }
278
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
491,915,508✔
279
}
280

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

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

295
bool fmIsBlockDistFunc(int32_t funcId) {
3,510,601✔
296
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
3,510,601!
297
    return false;
1,400✔
298
  }
299
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
3,509,201✔
300
}
301

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

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

306
void fmFuncMgtDestroy() {
4,060✔
307
  void* m = gFunMgtService.pFuncNameHashTable;
4,060✔
308
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
4,060!
309
    taosHashCleanup(m);
4,060✔
310
  }
311
}
4,060✔
312

313
#ifdef BUILD_NO_CALL
314
int32_t fmSetInvertFunc(int32_t funcId, SFuncExecFuncs* pFpSet) {
315
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
316
    return TSDB_CODE_FAILED;
317
  }
318
  pFpSet->process = funcMgtBuiltins[funcId].invertFunc;
319
  return TSDB_CODE_SUCCESS;
320
}
321

322
int32_t fmSetNormalFunc(int32_t funcId, SFuncExecFuncs* pFpSet) {
323
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
324
    return TSDB_CODE_FAILED;
325
  }
326
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
327
  return TSDB_CODE_SUCCESS;
328
}
329

330
bool fmIsInvertible(int32_t funcId) {
331
  bool res = false;
332
  switch (funcMgtBuiltins[funcId].type) {
333
    case FUNCTION_TYPE_COUNT:
334
    case FUNCTION_TYPE_SUM:
335
    case FUNCTION_TYPE_STDDEV:
336
    case FUNCTION_TYPE_AVG:
337
    case FUNCTION_TYPE_WSTART:
338
    case FUNCTION_TYPE_WEND:
339
    case FUNCTION_TYPE_WDURATION:
340
      res = true;
341
      break;
342
    default:
343
      break;
344
  }
345
  return res;
346
}
347
#endif
348

349
// function has same input/output type
350
bool fmIsSameInOutType(int32_t funcId) {
315,996✔
351
  bool res = false;
315,996✔
352
  switch (funcMgtBuiltins[funcId].type) {
315,996✔
353
    case FUNCTION_TYPE_MAX:
97,586✔
354
    case FUNCTION_TYPE_MIN:
355
    case FUNCTION_TYPE_TOP:
356
    case FUNCTION_TYPE_BOTTOM:
357
    case FUNCTION_TYPE_FIRST:
358
    case FUNCTION_TYPE_LAST:
359
    case FUNCTION_TYPE_SAMPLE:
360
    case FUNCTION_TYPE_TAIL:
361
    case FUNCTION_TYPE_UNIQUE:
362
      res = true;
97,586✔
363
      break;
97,586✔
364
    default:
218,410✔
365
      break;
218,410✔
366
  }
367
  return res;
315,996✔
368
}
369

370
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
89,561✔
371
  SNode* pNode;
372
  FOREACH(pNode, pFunc->pParameterList) {
90,191✔
373
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
3,678✔
374
      return false;
3,048✔
375
    }
376
  }
377
  return true;
86,513✔
378
}
379

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

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

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

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

415
int32_t createFunctionWithSrcFunc(const char* pName, const SFunctionNode* pSrcFunc, SNodeList* pParameterList, SFunctionNode** ppFunc) {
897,909✔
416
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
897,909✔
417
  if (NULL == *ppFunc) {
897,910!
418
    return code;
×
419
  }
420

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

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

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

446
bool fmIsDistExecFunc(int32_t funcId) {
1,694,079✔
447
  if (fmIsUserDefinedFunc(funcId)) {
1,694,079✔
448
    return false;
889✔
449
  }
450
  if (!fmIsVectorFunc(funcId)) {
1,693,305!
451
    return true;
×
452
  }
453
  return (NULL != funcMgtBuiltins[funcId].pPartialFunc && NULL != funcMgtBuiltins[funcId].pMergeFunc);
1,693,331!
454
}
455

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

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

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

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

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

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

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

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

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

551
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
315,996✔
552
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
315,995!
553
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
265,918✔
554
  }
555
  if (TSDB_CODE_SUCCESS == code) {
315,995!
556
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
315,996✔
557
  }
558

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

565
  return code;
315,996✔
566
}
567

568
char* fmGetFuncName(int32_t funcId) {
2✔
569
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2!
570
    return taosStrdup("invalid function");
×
571
  }
572
  return  taosStrdup(funcMgtBuiltins[funcId].name);
2✔
573
}
574

575
/// @param [out] pStateFunc, not changed if error occured or no need to create state func
576
/// @retval 0 for succ, otherwise err occured
577
static int32_t fmCreateStateFunc(const SFunctionNode* pFunc, SFunctionNode** pStateFunc) {
33,064✔
578
  if (funcMgtBuiltins[pFunc->funcId].pStateFunc) {
33,064!
579
    SNodeList* pParams = NULL;
33,064✔
580
    int32_t code = nodesCloneList(pFunc->pParameterList, &pParams);
33,064✔
581
    if (!pParams) return code;
33,064!
582
    code = createFunction(funcMgtBuiltins[pFunc->funcId].pStateFunc, pParams, pStateFunc);
33,064✔
583
    if (TSDB_CODE_SUCCESS != code) {
33,064!
584
      nodesDestroyList(pParams);
×
585
      return code;
×
586
    }
587
    tstrncpy((*pStateFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
33,064✔
588
    tstrncpy((*pStateFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
33,064✔
589
  }
590
  return TSDB_CODE_SUCCESS;
33,064✔
591
}
592

593
bool fmIsTSMASupportedFunc(func_id_t funcId) {
222,795✔
594
  return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC) &&
428,106✔
595
         !isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_STREAM_FUNC);
205,311!
596
}
597

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

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

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

664
int32_t fmGetFuncId(const char* name) {
63,109✔
665
  if (NULL != gFunMgtService.pFuncNameHashTable) {
63,109!
666
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, name, strlen(name));
63,109✔
667
    if (NULL != pVal) {
63,109!
668
      return *(int32_t*)pVal;
63,109✔
669
    }
670
    return -1;
×
671
  }
672
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
×
673
    if (0 == strcmp(funcMgtBuiltins[i].name, name)) {
×
674
      return i;
×
675
    }
676
  }
677
  return -1;
×
678
}
679

680
bool fmIsMyStateFunc(int32_t funcId, int32_t stateFuncId) {
16,705,737✔
681
  const SBuiltinFuncDefinition* pFunc = &funcMgtBuiltins[funcId];
16,705,737✔
682
  const SBuiltinFuncDefinition* pStateFunc = &funcMgtBuiltins[stateFuncId];
16,705,737✔
683
  if (!pFunc->pStateFunc) {
16,705,737!
684
    return false;
×
685
  }
686
  if (strcmp(pFunc->pStateFunc, pStateFunc->name) == 0) return true;
16,705,737✔
687
  int32_t stateMergeFuncId = fmGetFuncId(pFunc->pStateFunc);
61,415✔
688
  if (stateMergeFuncId == -1) {
61,415!
689
    return false;
×
690
  }
691
  const SBuiltinFuncDefinition* pStateMergeFunc = &funcMgtBuiltins[stateMergeFuncId];
61,415✔
692
  return strcmp(pStateFunc->name, pStateMergeFunc->pMergeFunc) == 0;
61,415✔
693
}
694

695
bool fmIsCountLikeFunc(int32_t funcId) {
1,598,445✔
696
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
1,598,445✔
697
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc