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

taosdata / TDengine / #3541

26 Nov 2024 03:56AM UTC coverage: 60.776% (-0.07%) from 60.846%
#3541

push

travis-ci

web-flow
Merge pull request #28920 from taosdata/fix/TD-33008-3.0

fix(query)[TD-33008]. fix error handling in tsdbCacheRead

120076 of 252763 branches covered (47.51%)

Branch coverage included in aggregate %.

0 of 2 new or added lines in 1 file covered. (0.0%)

1395 existing lines in 154 files now uncovered.

200995 of 275526 relevant lines covered (72.95%)

19612328.37 hits per line

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

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

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

51
static bool isSpecificClassifyFunc(int32_t funcId, uint64_t classification) {
334,951,269✔
52
  if (fmIsUserDefinedFunc(funcId)) {
334,951,269✔
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);
114,925✔
56
  }
57
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
334,838,584!
58
    return false;
88,737,425✔
59
  }
60
  return FUNC_MGT_TEST_MASK(funcMgtBuiltins[funcId].classification, classification);
246,101,159✔
61
}
62

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

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

88
EFuncReturnRows fmGetFuncReturnRows(SFunctionNode* pFunc) {
62,849✔
89
  if (NULL != funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc) {
62,849✔
90
    return funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc(pFunc);
22,072✔
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,204,703✔
101
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
3,204,703✔
102
  if (NULL != pVal) {
3,204,744✔
103
    return funcMgtBuiltins[*(int32_t*)pVal].type;
3,203,149✔
104
  }
105
  return FUNCTION_TYPE_UDF;
1,595✔
106
}
107

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

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

123
  const char* name = funcMgtBuiltins[funcId].name;
1,993,941✔
124
  if ((strcmp(name, "_group_key") == 0) || (strcmp(name, "_select_value") == 0)) {
1,993,941✔
125
    return FUNC_DATA_REQUIRED_NOT_LOAD;;
113,836✔
126
  }
127

128
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
1,880,105✔
129
    return FUNC_DATA_REQUIRED_DATA_LOAD;
1,715,706✔
130
  } else {
131
    return funcMgtBuiltins[funcId].dynDataRequiredFunc(pRes, pBlockInfo);
164,399✔
132
  }
133
}
134

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

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

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

169
bool fmIsAggFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_AGG_FUNC); }
23,431,093✔
170

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

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

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

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

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

181
bool fmIsPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PSEUDO_COLUMN_FUNC); }
36,112,694✔
182

183
bool fmIsScanPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCAN_PC_FUNC); }
24,359,216✔
184

185
bool fmIsWindowPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_WINDOW_PC_FUNC); }
16,150,624✔
186

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

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

191

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

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

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

202
bool fmIsRepeatScanFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_REPEAT_SCAN_FUNC); }
7,631,552✔
203

204
bool fmIsUserDefinedFunc(int32_t funcId) { return funcId > FUNC_UDF_ID_START; }
383,190,481✔
205

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

208
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
28,673,451✔
209

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

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

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

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

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

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

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

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

226
bool fmIsInterpFunc(int32_t funcId) {
6,519,482✔
227
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
6,519,482!
228
    return false;
3,140✔
229
  }
230
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
6,516,342✔
231
}
232

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

235
bool fmIsForecastFunc(int32_t funcId) {
6,365,685✔
236
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
6,365,685!
237
    return false;
3,133✔
238
  }
239
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
6,362,552✔
240
}
241

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

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

251
bool fmIsNotNullOutputFunc(int32_t funcId) {
11,079,348✔
252
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
11,079,348!
253
    return false;
×
254
  }
255
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
21,910,898✔
256
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
10,830,651✔
257
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
10,163,416✔
258
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
9,832,955✔
259
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
9,624,134✔
260
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
9,387,278✔
261
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
9,270,983✔
262
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
7,761,637✔
263
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
29,610,533✔
264
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
7,699,635✔
265
}
266

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

274
bool fmIsGroupKeyFunc(int32_t funcId) {
400,271,390✔
275
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
400,271,390!
276
    return false;
×
277
  }
278
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
400,301,431✔
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,977,029✔
289
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
16,977,029!
290
    return false;
×
291
  }
292
  return FUNCTION_TYPE_ELAPSED == funcMgtBuiltins[funcId].type;
16,977,047✔
293
}
294

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

302
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
5,155,638✔
303

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

499
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
267,620✔
500
  if (TSDB_CODE_SUCCESS == code) {
267,620!
501
    if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){
267,620✔
502
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pSrcFunc, pParameterList, &pFunc);
19,001✔
503
    }else{
504
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
248,619✔
505
    }
506
  }
507
  if (TSDB_CODE_SUCCESS == code) {
267,620!
508
    tstrncpy(pFunc->node.aliasName, pPartialFunc->node.aliasName, TSDB_COL_NAME_LEN);
267,620✔
509
  }
510

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

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

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

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

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

551
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
317,993✔
552
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
317,994!
553
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
267,620✔
554
  }
555
  if (TSDB_CODE_SUCCESS == code) {
317,994!
556
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
317,994✔
557
  }
558

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

565
  return code;
317,994✔
566
}
567

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

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

593
bool fmIsTSMASupportedFunc(func_id_t funcId) {
541,189✔
594
  return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC) &&
1,059,305✔
595
         !isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_STREAM_FUNC);
518,116!
596
}
597

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

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

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

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

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

© 2025 Coveralls, Inc