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

taosdata / TDengine / #4513

17 Jul 2025 02:02AM UTC coverage: 31.359% (-31.1%) from 62.446%
#4513

push

travis-ci

web-flow
Merge pull request #31914 from taosdata/fix/3.0/compare-ans-failed

fix:Convert line endings from LF to CRLF for ans file

68541 of 301034 branches covered (22.77%)

Branch coverage included in aggregate %.

117356 of 291771 relevant lines covered (40.22%)

602262.98 hits per line

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

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

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

51
static bool isSpecificClassifyFunc(int32_t funcId, uint64_t classification) {
157,605✔
52
  if (fmIsUserDefinedFunc(funcId)) {
157,605✔
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);
276✔
56
  }
57
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
157,329!
58
    return false;
32,122✔
59
  }
60
  return FUNC_MGT_TEST_MASK(funcMgtBuiltins[funcId].classification, classification);
125,207✔
61
}
62

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

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

88
EFuncReturnRows fmGetFuncReturnRows(SFunctionNode* pFunc) {
255✔
89
  if (NULL != funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc) {
255✔
90
    return funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc(pFunc);
206✔
91
  }
92
  return (fmIsIndefiniteRowsFunc(pFunc->funcId) || fmIsMultiRowsFunc(pFunc->funcId)) ? FUNC_RETURN_ROWS_INDEFINITE
49!
93
                                                                                     : FUNC_RETURN_ROWS_NORMAL;
49!
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) {
2,633✔
101
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
2,633✔
102
  if (NULL != pVal) {
2,633✔
103
    return funcMgtBuiltins[*(int32_t*)pVal].type;
2,629✔
104
  }
105
  return FUNCTION_TYPE_UDF;
4✔
106
}
107

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

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

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

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

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

150
int32_t fmGetUdafExecFuncs(int32_t funcId, SFuncExecFuncs* pFpSet) {
×
151
#ifdef USE_UDF
152
  if (!fmIsUserDefinedFunc(funcId)) {
×
153
    return TSDB_CODE_FAILED;
×
154
  }
155
  pFpSet->getEnv = udfAggGetEnv;
×
156
  pFpSet->init = udfAggInit;
×
157
  pFpSet->process = udfAggProcess;
×
158
  pFpSet->finalize = udfAggFinalize;
×
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) {
2,010✔
166
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,010!
167
    return TSDB_CODE_FAILED;
×
168
  }
169
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
2,010✔
170
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
2,010✔
171
  return TSDB_CODE_SUCCESS;
2,010✔
172
}
173

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

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

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

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

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

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

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

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

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

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

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

196
bool fmIsStreamWindowClauseFunc(int32_t funcId) { return fmIsWindowClauseFunc(funcId) || fmIsPlaceHolderFunc(funcId); }
×
197

198
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
7,764✔
199

200
bool fmIsSpecialDataRequiredFunc(int32_t funcId) {
117✔
201
  return isSpecificClassifyFunc(funcId, FUNC_MGT_SPECIAL_DATA_REQUIRED);
117✔
202
}
203

204
bool fmIsDynamicScanOptimizedFunc(int32_t funcId) {
72✔
205
  return isSpecificClassifyFunc(funcId, FUNC_MGT_DYNAMIC_SCAN_OPTIMIZED);
72✔
206
}
207

208
bool fmIsMultiResFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_RES_FUNC); }
5,402✔
209

210
bool fmIsRepeatScanFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_REPEAT_SCAN_FUNC); }
5,356✔
211

212
bool fmIsUserDefinedFunc(int32_t funcId) { return funcId > FUNC_UDF_ID_START; }
168,559✔
213

214
bool fmIsForbidFillFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_FILL_FUNC); }
2,750✔
215

216
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
210✔
217

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

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

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

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

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

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

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

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

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

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

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

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

257
bool fmIsLastFunc(int32_t funcId) {
×
258
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
259
    return false;
×
260
  }
261
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type;
×
262
}
263

264
bool fmIsNotNullOutputFunc(int32_t funcId) {
2,368✔
265
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,368!
266
    return false;
×
267
  }
268
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
4,706✔
269
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
2,338✔
270
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
2,304✔
271
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
2,287✔
272
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
2,265!
273
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
2,265!
274
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
2,265✔
275
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
1,778!
276
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
6,484!
277
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
1,778!
278
}
279

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

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

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

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

308
bool fmIsBlockDistFunc(int32_t funcId) {
2,695✔
309
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,695!
310
    return false;
8✔
311
  }
312
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
2,687✔
313
}
314

315
bool fmIsDBUsageFunc(int32_t funcId) {
2,695✔
316
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,695!
317
    return false;
8✔
318
  }
319
  return FUNCTION_TYPE_DB_USAGE == funcMgtBuiltins[funcId].type;
2,687✔
320
}
321

322
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
2,843✔
323

324
bool fmIsIgnoreNullFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IGNORE_NULL_FUNC); }
82✔
325

326
void fmFuncMgtDestroy() {
24✔
327
  void* m = gFunMgtService.pFuncNameHashTable;
24✔
328
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
24!
329
    taosHashCleanup(m);
24✔
330
  }
331
}
24✔
332

333
#ifdef BUILD_NO_CALL
334
int32_t fmSetInvertFunc(int32_t funcId, SFuncExecFuncs* pFpSet) {
335
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
336
    return TSDB_CODE_FAILED;
337
  }
338
  pFpSet->process = funcMgtBuiltins[funcId].invertFunc;
339
  return TSDB_CODE_SUCCESS;
340
}
341

342
int32_t fmSetNormalFunc(int32_t funcId, SFuncExecFuncs* pFpSet) {
343
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
344
    return TSDB_CODE_FAILED;
345
  }
346
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
347
  return TSDB_CODE_SUCCESS;
348
}
349

350
bool fmIsInvertible(int32_t funcId) {
351
  bool res = false;
352
  switch (funcMgtBuiltins[funcId].type) {
353
    case FUNCTION_TYPE_COUNT:
354
    case FUNCTION_TYPE_SUM:
355
    case FUNCTION_TYPE_STDDEV:
356
    case FUNCTION_TYPE_AVG:
357
    case FUNCTION_TYPE_WSTART:
358
    case FUNCTION_TYPE_WEND:
359
    case FUNCTION_TYPE_WDURATION:
360
      res = true;
361
      break;
362
    default:
363
      break;
364
  }
365
  return res;
366
}
367
#endif
368

369
// function has same input/output type
370
bool fmIsSameInOutType(int32_t funcId) {
55✔
371
  bool res = false;
55✔
372
  switch (funcMgtBuiltins[funcId].type) {
55✔
373
    case FUNCTION_TYPE_MAX:
23✔
374
    case FUNCTION_TYPE_MIN:
375
    case FUNCTION_TYPE_TOP:
376
    case FUNCTION_TYPE_BOTTOM:
377
    case FUNCTION_TYPE_FIRST:
378
    case FUNCTION_TYPE_LAST:
379
    case FUNCTION_TYPE_SAMPLE:
380
    case FUNCTION_TYPE_TAIL:
381
    case FUNCTION_TYPE_UNIQUE:
382
      res = true;
23✔
383
      break;
23✔
384
    default:
32✔
385
      break;
32✔
386
  }
387
  return res;
55✔
388
}
389

390
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
212✔
391
  SNode* pNode;
392
  FOREACH(pNode, pFunc->pParameterList) {
212!
393
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
×
394
      return false;
×
395
    }
396
  }
397
  return true;
212✔
398
}
399

400
bool fmIsSkipScanCheckFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SKIP_SCAN_CHECK_FUNC); }
72✔
401

402
bool fmIsPrimaryKeyFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PRIMARY_KEY_FUNC); }
2,436✔
403

404
bool fmIsPlaceHolderFunc(int32_t funcId) {return isSpecificClassifyFunc(funcId, FUNC_MGT_PLACE_HOLDER_FUNC); }
16,079✔
405

406
void getLastCacheDataType(SDataType* pType, int32_t pkBytes) {
×
407
  // TODO: do it later.
408
  pType->bytes = getFirstLastInfoSize(pType->bytes, pkBytes) + VARSTR_HEADER_SIZE;
×
409
  pType->type = TSDB_DATA_TYPE_BINARY;
×
410
}
×
411

412
static int32_t getFuncInfo(SFunctionNode* pFunc) {
573✔
413
  char msg[128] = {0};
573✔
414
  return fmGetFuncInfo(pFunc, msg, sizeof(msg));
573✔
415
}
416

417
int32_t createFunction(const char* pName, SNodeList* pParameterList, SFunctionNode** ppFunc) {
408✔
418
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
408✔
419
  if (NULL == *ppFunc) {
408!
420
    return code;
×
421
  }
422
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
408✔
423
  (*ppFunc)->pParameterList = pParameterList;
408✔
424
  code = getFuncInfo((*ppFunc));
408✔
425
  if (TSDB_CODE_SUCCESS != code) {
408!
426
    (*ppFunc)->pParameterList = NULL;
×
427
    nodesDestroyNode((SNode*)*ppFunc);
×
428
    *ppFunc = NULL;
×
429
    return code;
×
430
  }
431
  return code;
408✔
432
}
433

434
static void resetOutputChangedFunc(SFunctionNode *pFunc, const SFunctionNode* pSrcFunc) {
165✔
435
  if (funcMgtBuiltins[pFunc->funcId].type == FUNCTION_TYPE_LAST_MERGE) {
165✔
436
    pFunc->node.resType = pSrcFunc->node.resType;
38✔
437
    return;
38✔
438
  }
439
}
440

441
int32_t createFunctionWithSrcFunc(const char* pName, const SFunctionNode* pSrcFunc, SNodeList* pParameterList, SFunctionNode** ppFunc) {
165✔
442
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
165✔
443
  if (NULL == *ppFunc) {
165!
444
    return code;
×
445
  }
446

447
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
165✔
448
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
165✔
449
  (*ppFunc)->pSrcFuncRef = pSrcFunc;
165✔
450

451
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
165✔
452
  (*ppFunc)->pParameterList = pParameterList;
165✔
453
  code = getFuncInfo((*ppFunc));
165✔
454
  if (TSDB_CODE_SUCCESS != code) {
165!
455
    (*ppFunc)->pParameterList = NULL;
×
456
    nodesDestroyNode((SNode*)*ppFunc);
×
457
    *ppFunc = NULL;
×
458
    return code;
×
459
  }
460
  resetOutputChangedFunc(*ppFunc, pSrcFunc);
165✔
461
  (*ppFunc)->node.relatedTo = pSrcFunc->node.relatedTo;
165✔
462
  (*ppFunc)->node.bindExprID = pSrcFunc->node.bindExprID;
165✔
463
  return code;
165✔
464
}
465

466
static int32_t createColumnByFunc(const SFunctionNode* pFunc, SColumnNode** ppCol) {
110✔
467
  int32_t code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)ppCol);
110✔
468
  if (NULL == *ppCol) {
110!
469
    return code;
×
470
  }
471
  tstrncpy((*ppCol)->colName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
110✔
472
  (*ppCol)->node.resType = pFunc->node.resType;
110✔
473
  return TSDB_CODE_SUCCESS;
110✔
474
}
475

476
bool fmIsDistExecFunc(int32_t funcId) {
1,744✔
477
  if (fmIsUserDefinedFunc(funcId)) {
1,744!
478
    return false;
×
479
  }
480
  if (!fmIsVectorFunc(funcId)) {
1,744!
481
    return true;
×
482
  }
483
  return (NULL != funcMgtBuiltins[funcId].pPartialFunc && NULL != funcMgtBuiltins[funcId].pMergeFunc);
1,744!
484
}
485

486
static int32_t createPartialFunction(const SFunctionNode* pSrcFunc, SFunctionNode** pPartialFunc) {
55✔
487
  SNodeList* pParameterList = NULL;
55✔
488
  int32_t    code = nodesCloneList(pSrcFunc->pParameterList, &pParameterList);
55✔
489
  if (NULL == pParameterList) {
55!
490
    return code;
×
491
  }
492
  code =
493
      createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pPartialFunc, pSrcFunc, pParameterList, pPartialFunc);
55✔
494
  if (TSDB_CODE_SUCCESS != code) {
55!
495
    nodesDestroyList(pParameterList);
×
496
    return code;
×
497
  }
498
  (*pPartialFunc)->hasOriginalFunc = true;
55✔
499
  (*pPartialFunc)->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
55!
500
  char name[TSDB_FUNC_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_POINTER_PRINT_BYTES + 1] = {0};
55✔
501

502
  int32_t len = tsnprintf(name, sizeof(name), "%s.%p", (*pPartialFunc)->functionName, pSrcFunc);
55✔
503
  if (taosHashBinary(name, len) < 0) {
55!
504
    return TSDB_CODE_FAILED;
×
505
  }
506
  tstrncpy((*pPartialFunc)->node.aliasName, name, TSDB_COL_NAME_LEN);
55✔
507
  return TSDB_CODE_SUCCESS;
55✔
508
}
509

510
static int32_t createMergeFuncPara(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
110✔
511
                                   SNodeList** pParameterList) {
512
  SNode*  pRes = NULL;
110✔
513
  int32_t code = createColumnByFunc(pPartialFunc, (SColumnNode**)&pRes);
110✔
514
  if (TSDB_CODE_SUCCESS != code) {
110!
515
    return code;
×
516
  }
517
  if (NULL != funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc) {
110✔
518
    return funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc(pSrcFunc->pParameterList, pRes, pParameterList);
2✔
519
  } else {
520
    return nodesListMakeStrictAppend(pParameterList, pRes);
108✔
521
  }
522
}
523

524
static int32_t createMidFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
55✔
525
                                 SFunctionNode** pMidFunc) {
526
  SNodeList*     pParameterList = NULL;
55✔
527
  SFunctionNode* pFunc = NULL;
55✔
528

529
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
55✔
530
  if (TSDB_CODE_SUCCESS == code) {
55!
531
    if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){
55✔
532
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pSrcFunc, pParameterList, &pFunc);
1✔
533
    }else{
534
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
54✔
535
    }
536
  }
537
  if (TSDB_CODE_SUCCESS == code) {
55!
538
    tstrncpy(pFunc->node.aliasName, pPartialFunc->node.aliasName, TSDB_COL_NAME_LEN);
55✔
539
  }
540

541
  if (TSDB_CODE_SUCCESS == code) {
55!
542
    *pMidFunc = pFunc;
55✔
543
  } else {
544
    nodesDestroyList(pParameterList);
×
545
  }
546
  return code;
55✔
547
}
548

549
static int32_t createMergeFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
55✔
550
                                   SFunctionNode** pMergeFunc) {
551
  SNodeList*     pParameterList = NULL;
55✔
552
  SFunctionNode* pFunc = NULL;
55✔
553

554
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
55✔
555
  if (TSDB_CODE_SUCCESS == code) {
55!
556
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
55✔
557
  }
558
  if (TSDB_CODE_SUCCESS == code) {
55!
559
    pFunc->hasOriginalFunc = true;
55✔
560
    pFunc->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
55!
561
    // overwrite function restype set by translate function
562
    if (fmIsSameInOutType(pSrcFunc->funcId)) {
55✔
563
      pFunc->node.resType = pSrcFunc->node.resType;
23✔
564
    }
565
    tstrncpy(pFunc->node.aliasName, pSrcFunc->node.aliasName, TSDB_COL_NAME_LEN);
55✔
566
  }
567

568
  if (TSDB_CODE_SUCCESS == code) {
55!
569
    *pMergeFunc = pFunc;
55✔
570
  } else {
571
    nodesDestroyList(pParameterList);
×
572
  }
573
  return code;
55✔
574
}
575

576
int32_t fmGetDistMethod(const SFunctionNode* pFunc, SFunctionNode** pPartialFunc, SFunctionNode** pMidFunc,
55✔
577
                        SFunctionNode** pMergeFunc) {
578
  if (!fmIsDistExecFunc(pFunc->funcId)) {
55!
579
    return TSDB_CODE_FAILED;
×
580
  }
581

582
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
55✔
583
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
55!
584
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
55✔
585
  }
586
  if (TSDB_CODE_SUCCESS == code) {
55!
587
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
55✔
588
  }
589

590
  if (TSDB_CODE_SUCCESS != code) {
55!
591
    nodesDestroyNode((SNode*)*pPartialFunc);
×
592
    if (pMidFunc) nodesDestroyNode((SNode*)*pMidFunc);
×
593
    nodesDestroyNode((SNode*)*pMergeFunc);
×
594
  }
595

596
  return code;
55✔
597
}
598

599
char* fmGetFuncName(int32_t funcId) {
×
600
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
601
    return taosStrdup("invalid function");
×
602
  }
603
  return taosStrdup(funcMgtBuiltins[funcId].name);
×
604
}
605

606
/// @param [out] pStateFunc, not changed if error occured or no need to create state func
607
/// @retval 0 for succ, otherwise err occured
608
static int32_t fmCreateStateFunc(const SFunctionNode* pFunc, SFunctionNode** pStateFunc) {
×
609
  if (funcMgtBuiltins[pFunc->funcId].pStateFunc) {
×
610
    SNodeList* pParams = NULL;
×
611
    int32_t    code = nodesCloneList(pFunc->pParameterList, &pParams);
×
612
    if (!pParams) return code;
×
613
    code = createFunction(funcMgtBuiltins[pFunc->funcId].pStateFunc, pParams, pStateFunc);
×
614
    if (TSDB_CODE_SUCCESS != code) {
×
615
      nodesDestroyList(pParams);
×
616
      return code;
×
617
    }
618
    tstrncpy((*pStateFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
×
619
    tstrncpy((*pStateFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
×
620
  }
621
  return TSDB_CODE_SUCCESS;
×
622
}
623

624
bool fmIsTSMASupportedFunc(func_id_t funcId) {
×
625
  return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC);
×
626
}
627

628
int32_t fmCreateStateFuncs(SNodeList* pFuncs) {
×
629
  int32_t code;
630
  SNode*  pNode;
631
  char    buf[128] = {0};
×
632
  FOREACH(pNode, pFuncs) {
×
633
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
×
634
    code = fmGetFuncInfo(pFunc, buf, 128);
×
635
    if (code) break;
×
636
    if (fmIsTSMASupportedFunc(pFunc->funcId)) {
×
637
      SFunctionNode* pNewFunc = NULL;
×
638
      code = fmCreateStateFunc(pFunc, &pNewFunc);
×
639
      if (code) {
×
640
        // error
641
        break;
×
642
      } else if (!pNewFunc) {
×
643
        // no need state func
644
        continue;
×
645
      } else {
646
        REPLACE_NODE(pNewFunc);
×
647
        nodesDestroyNode(pNode);
×
648
      }
649
    }
650
  }
651
  return code;
×
652
}
653

654
static int32_t fmCreateStateMergeFunc(SFunctionNode* pFunc, SFunctionNode** pStateMergeFunc) {
×
655
  if (funcMgtBuiltins[pFunc->funcId].pMergeFunc) {
×
656
    SNodeList* pParams = NULL;
×
657
    int32_t    code = nodesCloneList(pFunc->pParameterList, &pParams);
×
658
    if (!pParams) return code;
×
659
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pFunc->funcId].pMergeFunc, pFunc, pParams, pStateMergeFunc);
×
660
    if (TSDB_CODE_SUCCESS != code) {
×
661
      nodesDestroyList(pParams);
×
662
      return code;
×
663
    }
664
    tstrncpy((*pStateMergeFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
×
665
    tstrncpy((*pStateMergeFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
×
666
  }
667
  return TSDB_CODE_SUCCESS;
×
668
}
669

670
int32_t fmCreateStateMergeFuncs(SNodeList* pFuncs) {
×
671
  int32_t code;
672
  SNode*  pNode;
673
  char    buf[128] = {0};
×
674
  FOREACH(pNode, pFuncs) {
×
675
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
×
676
    if (fmIsTSMASupportedFunc(pFunc->funcId)) {
×
677
      SFunctionNode* pNewFunc = NULL;
×
678
      code = fmCreateStateMergeFunc(pFunc, &pNewFunc);
×
679
      if (code) {
×
680
        // error
681
        break;
×
682
      } else if (!pNewFunc) {
×
683
        // no state merge func
684
        continue;
×
685
      } else {
686
        REPLACE_NODE(pNewFunc);
×
687
        nodesDestroyNode(pNode);
×
688
      }
689
    }
690
  }
691
  return code;
×
692
}
693

694
int32_t fmGetFuncId(const char* name) {
×
695
  if (NULL != gFunMgtService.pFuncNameHashTable) {
×
696
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, name, strlen(name));
×
697
    if (NULL != pVal) {
×
698
      return *(int32_t*)pVal;
×
699
    }
700
    return -1;
×
701
  }
702
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
×
703
    if (0 == strcmp(funcMgtBuiltins[i].name, name)) {
×
704
      return i;
×
705
    }
706
  }
707
  return -1;
×
708
}
709

710
bool fmIsMyStateFunc(int32_t funcId, int32_t stateFuncId) {
×
711
  const SBuiltinFuncDefinition* pFunc = &funcMgtBuiltins[funcId];
×
712
  const SBuiltinFuncDefinition* pStateFunc = &funcMgtBuiltins[stateFuncId];
×
713
  if (!pFunc->pStateFunc) {
×
714
    return false;
×
715
  }
716
  if (strcmp(pFunc->pStateFunc, pStateFunc->name) == 0) return true;
×
717
  int32_t stateMergeFuncId = fmGetFuncId(pFunc->pStateFunc);
×
718
  if (stateMergeFuncId == -1) {
×
719
    return false;
×
720
  }
721
  const SBuiltinFuncDefinition* pStateMergeFunc = &funcMgtBuiltins[stateMergeFuncId];
×
722
  return strcmp(pStateFunc->name, pStateMergeFunc->pMergeFunc) == 0;
×
723
}
724

725
bool fmIsCountLikeFunc(int32_t funcId) {
752✔
726
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
752✔
727
}
728

729
bool fmIsRowTsOriginFunc(int32_t funcId) {
14✔
730
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
14!
731
    return false;
×
732
  }
733
  return FUNCTION_TYPE_IROWTS_ORIGIN == funcMgtBuiltins[funcId].type;
14✔
734
}
735

736
bool fmIsGroupIdFunc(int32_t funcId) {
×
737
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
738
    return false;
×
739
  }
740
  return FUNCTION_TYPE_GROUP_ID == funcMgtBuiltins[funcId].type;
×
741
}
742

743
const void* fmGetStreamPesudoFuncVal(int32_t funcId, const SStreamRuntimeFuncInfo* pStreamRuntimeFuncInfo) {
×
744
  SSTriggerCalcParam *pParams = taosArrayGet(pStreamRuntimeFuncInfo->pStreamPesudoFuncVals, pStreamRuntimeFuncInfo->curIdx);
×
745
  switch (funcMgtBuiltins[funcId].type) {
×
746
    case FUNCTION_TYPE_TPREV_TS:
×
747
      return &pParams->prevTs;
×
748
    case FUNCTION_TYPE_TCURRENT_TS:
×
749
      return &pParams->currentTs;
×
750
    case FUNCTION_TYPE_TNEXT_TS:
×
751
      return &pParams->nextTs;
×
752
    case FUNCTION_TYPE_TWSTART:
×
753
      return &pParams->wstart;
×
754
    case FUNCTION_TYPE_TWEND:
×
755
      return &pParams->wend;
×
756
    case FUNCTION_TYPE_TWDURATION:
×
757
      return &pParams->wduration;
×
758
    case FUNCTION_TYPE_TWROWNUM:
×
759
      return &pParams->wrownum;
×
760
    case FUNCTION_TYPE_TPREV_LOCALTIME:
×
761
      return &pParams->prevLocalTime;
×
762
    case FUNCTION_TYPE_TLOCALTIME:
×
763
      return &pParams->triggerTime;
×
764
    case FUNCTION_TYPE_TNEXT_LOCALTIME:
×
765
      return &pParams->nextLocalTime;
×
766
    case FUNCTION_TYPE_TGRPID:
×
767
      return &pStreamRuntimeFuncInfo->groupId;
×
768
    case FUNCTION_TYPE_PLACEHOLDER_COLUMN:
×
769
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
×
770
    case FUNCTION_TYPE_PLACEHOLDER_TBNAME:
×
771
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
×
772
    default:
×
773
      break;
×
774
  }
775
  return NULL;
×
776
}
777

778
void fmGetStreamPesudoFuncValTbname(int32_t funcId, const SStreamRuntimeFuncInfo* pStreamRuntimeFuncInfo, void** data, int32_t* dataLen) {
×
779
  SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeFuncInfo);
×
780
  for (int32_t i = 0; i < taosArrayGetSize(pVal); ++i) {
×
781
    SStreamGroupValue* pValue = taosArrayGet(pVal, i);
×
782
    if (pValue != NULL && pValue->isTbname) {
×
783
      *data = pValue->data.pData;
×
784
      *dataLen = pValue->data.nData;
×
785
      break;
×
786
    }
787
  }
788
}
×
789

790
int32_t fmGetStreamPesudoFuncEnv(int32_t funcId, SNodeList* pParamNodes, SFuncExecEnv *pEnv) {
×
791
  if (NULL == pParamNodes || pParamNodes->length < 1) {
×
792
    uError("invalid stream pesudo func param list %p", pParamNodes);
×
793
    return TSDB_CODE_INTERNAL_ERROR;
×
794
  }
795

796
  int32_t firstParamType = nodeType(nodesListGetNode(pParamNodes, 0));
×
797
  if (QUERY_NODE_VALUE != firstParamType) {
×
798
    uError("invalid stream pesudo func first param type %d", firstParamType);
×
799
    return TSDB_CODE_INTERNAL_ERROR;
×
800
  }
801

802
  SValueNode* pResDesc = (SValueNode*)nodesListGetNode(pParamNodes, 0);
×
803
  pEnv->calcMemSize = pResDesc->node.resType.bytes;
×
804

805
  return TSDB_CODE_SUCCESS;
×
806
}
807

808
int32_t fmSetStreamPseudoFuncParamVal(int32_t funcId, SNodeList* pParamNodes, const SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
×
809
  if (!pStreamRuntimeInfo) {
×
810
    uError("internal error, should have pVals for stream pseudo funcs");
×
811
    return TSDB_CODE_INTERNAL_ERROR;
×
812
  }
813
  int32_t code = 0;
×
814
  SArray *pVals1 = NULL, *pVals2 = NULL;
×
815
  SNode* pFirstParam = nodesListGetNode(pParamNodes, 0);
×
816
  if (nodeType(pFirstParam) != QUERY_NODE_VALUE) {
×
817
    uError("invalid param node type: %d for func: %d", nodeType(pFirstParam), funcId);
×
818
    return TSDB_CODE_INTERNAL_ERROR;
×
819
  }
820

821
  int32_t t = funcMgtBuiltins[funcId].type;
×
822
  uInfo("set stream pseudo func param val, functype: %d, pStreamRuntimeInfo: %p", t, pStreamRuntimeInfo);
×
823
  if (FUNCTION_TYPE_TGRPID == t) {
×
824
    SValue v = {0};
×
825
    v.type = TSDB_DATA_TYPE_BIGINT;
×
826
    v.val = *(int64_t*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
×
827
    
828
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&v, pFirstParam->type));
×
829
    if (code != 0) {
×
830
      uError("failed to set value node value: %s", tstrerror(code));
×
831
      return code;
×
832
    }
833
  } else if (FUNCTION_TYPE_PLACEHOLDER_COLUMN == t) {
×
834
    SNode* pSecondParam = nodesListGetNode(pParamNodes, 1);
×
835
    if (nodeType(pSecondParam) != QUERY_NODE_VALUE) {
×
836
      uError("invalid param node type: %d for func: %d", nodeType(pSecondParam), funcId);
×
837
      return TSDB_CODE_INTERNAL_ERROR;
×
838
    }
839
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
×
840
    int32_t idx = ((SValueNode*)pSecondParam)->datum.i;
×
841
    if (idx - 1 < 0 || idx - 1 >= taosArrayGetSize(pVal)) {
×
842
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
×
843
      return TSDB_CODE_INTERNAL_ERROR;
×
844
    }
845
    SStreamGroupValue* pValue = taosArrayGet(pVal, idx - 1);
×
846
    if (pValue == NULL) {
×
847
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
×
848
      return TSDB_CODE_INTERNAL_ERROR;
×
849
    }
850
    if (!pValue->isNull){
×
851
      if (pValue->data.type != ((SValueNode*)pFirstParam)->node.resType.type){
×
852
        uError("invalid value type: %d for func: %d, should be: %d", pValue->data.type, funcId, ((SValueNode*)pFirstParam)->node.resType.type);
×
853
        return TSDB_CODE_INTERNAL_ERROR;
×
854
      }
855
      if (IS_VAR_DATA_TYPE(((SValueNode*)pFirstParam)->node.resType.type)) {
×
856
        taosMemoryFree(((SValueNode*)pFirstParam)->datum.p);
×
857
        ((SValueNode*)pFirstParam)->datum.p = taosMemoryCalloc(1, pValue->data.nData + VARSTR_HEADER_SIZE); 
×
858
        memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
×
859
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
×
860
      } else {
861
        code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&pValue->data, pValue->data.type));
×
862
      }
863
    }
864
    ((SValueNode*)pFirstParam)->isNull = pValue->isNull;
×
865
  } else if (FUNCTION_TYPE_PLACEHOLDER_TBNAME == t) {
×
866
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
×
867
    for (int32_t i = 0; i < taosArrayGetSize(pVal); ++i) {
×
868
      SStreamGroupValue* pValue = taosArrayGet(pVal, i);
×
869
      if (pValue != NULL && pValue->isTbname) {
×
870
        taosMemoryFreeClear(((SValueNode*)pFirstParam)->datum.p);
×
871
        ((SValueNode*)pFirstParam)->datum.p = taosMemoryCalloc(pValue->data.nData + VARSTR_HEADER_SIZE, 1);
×
872
        if (NULL == ((SValueNode*)pFirstParam)->datum.p ) {
×
873
          return terrno;
×
874
        }
875

876
        (void)memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
×
877
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
×
878
        break;
×
879
      }
880
    }
881
  } else if (LIST_LENGTH(pParamNodes) == 1) {
×
882
    // twstart, twend
883
    const void* pVal = fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
×
884
    if (!pVal) {
×
885
      uError("failed to set stream pseudo func param val, NULL val for funcId: %d", funcId);
×
886
      return TSDB_CODE_INTERNAL_ERROR;
×
887
    }
888
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, (void*)pVal);
×
889
    if (code != 0) {
×
890
      uError("failed to set value node value: %s", tstrerror(code));
×
891
      return code;
×
892
    }
893
  } else {
894
    SNode* pSecondParam = nodesListGetNode(pParamNodes, 1);
×
895
    if (nodeType(pSecondParam) != QUERY_NODE_VALUE) {
×
896
      uError("invalid param node type: %d for func: %d", nodeType(pSecondParam), funcId);
×
897
      return TSDB_CODE_INTERNAL_ERROR;
×
898
    }
899
    int32_t idx = ((SValueNode*)pSecondParam)->datum.i;
×
900
    const SValue* pVal = taosArrayGet(pVals2, idx);
×
901
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(pVal, pFirstParam->type));
×
902
  }
903
  return code;
×
904
}
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