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

taosdata / TDengine / #4985

15 Mar 2026 07:43AM UTC coverage: 68.601% (-0.04%) from 68.643%
#4985

push

travis-ci

web-flow
feat(stream): add natural time units support for PERIOD trigger (#34766)

Implement week/month/year units for stream PERIOD trigger with natural
boundary alignment and offset support.

Key changes:
- Parser: Add validation for natural time units (w/n/y) and offset parameter
- Time utilities: Add getDuration() support for week/month/year units
- TriggerTask: Implement window calculation with natural boundary alignment
  - Week: align to Monday 00:00:00
  - Month: align to 1st of month 00:00:00
  - Year: align to Jan 1st 00:00:00
- Add offset support: PERIOD(1w, 1d) shifts window by 1 day
- Unit tests: Parser validation, time utilities, TriggerTask window calculation
- System tests: End-to-end tests for week/month/year units with offset
- Documentation: Update user manual with natural time unit examples

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>

2 of 60 new or added lines in 2 files covered. (3.33%)

448 existing lines in 114 files now uncovered.

212624 of 309941 relevant lines covered (68.6%)

136450774.73 hits per line

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

82.28
/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 "nodes.h"
22
#include "querynodes.h"
23
#include "taos.h"
24
#include "taoserror.h"
25
#include "thash.h"
26
#include "tudf.h"
27

28
typedef struct SFuncMgtService {
29
  SHashObj* pFuncNameHashTable;
30
} SFuncMgtService;
31

32
static SFuncMgtService gFunMgtService;
33
static TdThreadOnce    functionHashTableInit = PTHREAD_ONCE_INIT;
34
static int32_t         initFunctionCode = 0;
35

36
static void doInitFunctionTable() {
1,713,338✔
37
  gFunMgtService.pFuncNameHashTable =
1,713,338✔
38
      taosHashInit(funcMgtBuiltinsNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
1,713,338✔
39
  if (NULL == gFunMgtService.pFuncNameHashTable) {
1,713,338✔
40
    initFunctionCode = terrno;
×
41
    return;
×
42
  }
43

44
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
387,214,388✔
45
    if (TSDB_CODE_SUCCESS != taosHashPut(gFunMgtService.pFuncNameHashTable, funcMgtBuiltins[i].name,
385,501,050✔
46
                                         strlen(funcMgtBuiltins[i].name), &i, sizeof(int32_t))) {
385,501,050✔
47
      initFunctionCode = terrno;
×
48
      return;
×
49
    }
50
  }
51
}
52

53
static bool isSpecificClassifyFunc(int32_t funcId, uint64_t classification) {
2,147,483,647✔
54
  if (fmIsUserDefinedFunc(funcId)) {
2,147,483,647✔
55
    return FUNC_MGT_AGG_FUNC == classification
56
               ? FUNC_AGGREGATE_UDF_ID == funcId
57
               : (FUNC_MGT_SCALAR_FUNC == classification ? FUNC_SCALAR_UDF_ID == funcId : false);
8,044,081✔
58
  }
59
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,147,483,647✔
60
    return false;
2,147,483,647✔
61
  }
62
  return FUNC_MGT_TEST_MASK(funcMgtBuiltins[funcId].classification, classification);
2,147,483,647✔
63
}
64

65
int32_t fmFuncMgtInit() {
1,713,338✔
66
  (void)taosThreadOnce(&functionHashTableInit, doInitFunctionTable);
1,713,338✔
67
  return initFunctionCode;
1,713,338✔
68
}
69

70
int32_t fmGetFuncInfo(SFunctionNode* pFunc, char* pMsg, int32_t msgLen) {
759,096,118✔
71
  if (NULL != gFunMgtService.pFuncNameHashTable) {
759,096,118✔
72
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc->functionName, strlen(pFunc->functionName));
758,940,297✔
73
    if (NULL != pVal) {
758,963,580✔
74
      pFunc->funcId = *(int32_t*)pVal;
758,771,268✔
75
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
758,771,291✔
76
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
758,770,753✔
77
    }
78
    return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
192,312✔
79
  }
80
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
3,092,764✔
81
    if (0 == strcmp(funcMgtBuiltins[i].name, pFunc->functionName)) {
3,092,656✔
82
      pFunc->funcId = i;
166,208✔
83
      pFunc->funcType = funcMgtBuiltins[pFunc->funcId].type;
166,208✔
84
      return funcMgtBuiltins[pFunc->funcId].translateFunc(pFunc, pMsg, msgLen);
166,208✔
85
    }
86
  }
87
  return TSDB_CODE_FUNC_NOT_BUILTIN_FUNTION;
108✔
88
}
89

90
EFuncReturnRows fmGetFuncReturnRows(SFunctionNode* pFunc) {
16,700,044✔
91
  if (NULL != funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc) {
16,700,044✔
92
    return funcMgtBuiltins[pFunc->funcId].estimateReturnRowsFunc(pFunc);
11,801,367✔
93
  }
94
  return (fmIsIndefiniteRowsFunc(pFunc->funcId) || fmIsMultiRowsFunc(pFunc->funcId)) ? FUNC_RETURN_ROWS_INDEFINITE
4,898,181✔
95
                                                                                     : FUNC_RETURN_ROWS_NORMAL;
4,898,181✔
96
}
97

98
bool fmIsBuiltinFunc(const char* pFunc) {
14,102✔
99
  return NULL != taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
14,102✔
100
}
101

102
EFunctionType fmGetFuncType(const char* pFunc) {
554,802,566✔
103
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFunc, strlen(pFunc));
554,802,566✔
104
  if (NULL != pVal) {
554,825,155✔
105
    return funcMgtBuiltins[*(int32_t*)pVal].type;
554,612,227✔
106
  }
107
  return FUNCTION_TYPE_UDF;
212,928✔
108
}
109

110
EFunctionType fmGetFuncTypeFromId(int32_t funcId) {
2,972,497✔
111
  if (funcId < funcMgtBuiltinsNum) {
2,972,497✔
112
    return funcMgtBuiltins[funcId].type;
2,972,497✔
113
  }
114
  return FUNCTION_TYPE_UDF;
×
115
}
116

117
EFuncDataRequired fmFuncDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
35,457,903✔
118
  if (fmIsUserDefinedFunc(pFunc->funcId) || pFunc->funcId < 0 || pFunc->funcId >= funcMgtBuiltinsNum) {
35,457,903✔
UNCOV
119
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
120
  }
121
  if (NULL == funcMgtBuiltins[pFunc->funcId].dataRequiredFunc) {
35,462,149✔
122
    return FUNC_DATA_REQUIRED_DATA_LOAD;
×
123
  }
124
  return funcMgtBuiltins[pFunc->funcId].dataRequiredFunc(pFunc, pTimeWindow);
35,462,149✔
125
}
126

127
EFuncDataRequired fmFuncDynDataRequired(int32_t funcId, void* pRes, SDataBlockInfo* pBlockInfo) {
2,147,483,647✔
128
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
2,147,483,647✔
129
    return FUNC_DATA_REQUIRED_DATA_LOAD;
115✔
130
  }
131

132
  const char* name = funcMgtBuiltins[funcId].name;
2,147,483,647✔
133
  if ((strcmp(name, "_group_key") == 0) || (strcmp(name, "_select_value") == 0)) {
2,147,483,647✔
134
    return FUNC_DATA_REQUIRED_NOT_LOAD;
587,594✔
135
    ;
136
  }
137

138
  if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) {
2,147,483,647✔
139
    return FUNC_DATA_REQUIRED_DATA_LOAD;
187,214,317✔
140
  } else {
141
    return funcMgtBuiltins[funcId].dynDataRequiredFunc(pRes, pBlockInfo);
2,147,483,647✔
142
  }
143
}
144

145
int32_t fmGetFuncExecFuncs(int32_t funcId, SFuncExecFuncs* pFpSet) {
332,223,411✔
146
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
332,223,411✔
147
    return TSDB_CODE_FAILED;
4,081✔
148
  }
149
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
332,225,996✔
150
  pFpSet->init = funcMgtBuiltins[funcId].initFunc;
332,162,635✔
151
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
332,184,096✔
152
  pFpSet->finalize = funcMgtBuiltins[funcId].finalizeFunc;
332,171,159✔
153
  pFpSet->combine = funcMgtBuiltins[funcId].combineFunc;
332,181,057✔
154
  pFpSet->processFuncByRow = funcMgtBuiltins[funcId].processFuncByRow;
332,195,819✔
155
  pFpSet->cleanup = funcMgtBuiltins[funcId].cleanupFunc;
332,193,885✔
156
  return TSDB_CODE_SUCCESS;
332,213,623✔
157
}
158

159
int32_t fmGetUdafExecFuncs(int32_t funcId, SFuncExecFuncs* pFpSet) {
34,840✔
160
#ifdef USE_UDF
161
  if (!fmIsUserDefinedFunc(funcId)) {
34,840✔
162
    return TSDB_CODE_FAILED;
×
163
  }
164
  pFpSet->getEnv = udfAggGetEnv;
34,840✔
165
  pFpSet->init = udfAggInit;
34,840✔
166
  pFpSet->process = udfAggProcess;
34,840✔
167
  pFpSet->finalize = udfAggFinalize;
34,840✔
168
  return TSDB_CODE_SUCCESS;
34,840✔
169
#else
170
  TAOS_RETURN(TSDB_CODE_OPS_NOT_SUPPORT);
171
#endif
172
}
173

174
int32_t fmGetScalarFuncExecFuncs(int32_t funcId, SScalarFuncExecFuncs* pFpSet) {
1,021,627,349✔
175
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,021,627,349✔
176
    return TSDB_CODE_FAILED;
36,566✔
177
  }
178
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
1,021,788,542✔
179
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
1,021,755,935✔
180
  return TSDB_CODE_SUCCESS;
1,021,755,085✔
181
}
182

183
bool fmIsAggFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_AGG_FUNC); }
2,097,320,886✔
184

185
bool fmIsScalarFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCALAR_FUNC); }
2,147,483,647✔
186

187
bool fmIsVectorFunc(int32_t funcId) { return !fmIsScalarFunc(funcId) && !fmIsPseudoColumnFunc(funcId); }
1,172,295,895✔
188

189
bool fmIsSelectColsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_COLS_FUNC); }
135,003,114✔
190

191
bool fmIsSelectFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SELECT_FUNC); }
1,870,261,004✔
192

193
bool fmIsTimelineFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TIMELINE_FUNC); }
1,075,453,610✔
194

195
bool fmIsDateTimeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_DATETIME_FUNC); }
544,134,295✔
196

197
bool fmIsPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PSEUDO_COLUMN_FUNC); }
2,047,584,489✔
198

199
bool fmIsScanPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SCAN_PC_FUNC); }
2,147,483,647✔
200

201
bool fmIsWindowPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_WINDOW_PC_FUNC); }
1,381,264,161✔
202

203
bool fmIsWindowClauseFunc(int32_t funcId) { return fmIsAggFunc(funcId) || fmIsWindowPseudoColumnFunc(funcId); }
54,705,951✔
204

205
bool fmIsStreamWindowClauseFunc(int32_t funcId) { return fmIsWindowClauseFunc(funcId) || fmIsPlaceHolderFunc(funcId); }
397,894✔
206

207
bool fmIsStreamVectorFunc(int32_t funcId) { return fmIsVectorFunc(funcId) || fmIsPlaceHolderFunc(funcId); }
10,200✔
208

209
bool fmIsIndefiniteRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INDEFINITE_ROWS_FUNC); }
1,457,545,048✔
210

211
bool fmIsSpecialDataRequiredFunc(int32_t funcId) {
82,406,685✔
212
  return isSpecificClassifyFunc(funcId, FUNC_MGT_SPECIAL_DATA_REQUIRED);
82,406,685✔
213
}
214

215
bool fmIsDynamicScanOptimizedFunc(int32_t funcId) {
40,068,622✔
216
  return isSpecificClassifyFunc(funcId, FUNC_MGT_DYNAMIC_SCAN_OPTIMIZED);
40,068,622✔
217
}
218

219
bool fmIsMultiResFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_RES_FUNC); }
1,101,017,118✔
220

221
bool fmIsRepeatScanFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_REPEAT_SCAN_FUNC); }
1,087,091,949✔
222

223
bool fmIsUserDefinedFunc(int32_t funcId) { return funcId > FUNC_UDF_ID_START; }
2,147,483,647✔
224

225
bool fmIsForbidFillFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_FILL_FUNC); }
544,374,399✔
226

227
bool fmIsIntervalInterpoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERVAL_INTERPO_FUNC); }
59,660,235✔
228

229
bool fmIsSystemInfoFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SYSTEM_INFO_FUNC); }
547,201,194✔
230

231
bool fmIsImplicitTsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IMPLICIT_TS_FUNC); }
2,147,483,647✔
232

233
bool fmIsClientPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CLIENT_PC_FUNC); }
546,951,412✔
234

235
bool fmIsMultiRowsFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_MULTI_ROWS_FUNC); }
1,084,476,302✔
236

237
static bool fmIsKeepOrderFuncId(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_KEEP_ORDER_FUNC); }
648,548,511✔
238

239
bool fmIsKeepOrderFunc(SFunctionNode* pFunc) {
654,414,837✔
240
  if (pFunc->funcType == FUNCTION_TYPE_TOP || pFunc->funcType == FUNCTION_TYPE_BOTTOM ||
654,414,837✔
241
      pFunc->funcType == FUNCTION_TYPE_SAMPLE) {
650,758,481✔
242
    if (pFunc->pParameterList != NULL && pFunc->pParameterList->length >= 2) {
5,864,771✔
243
      SNode* pParam = nodesListGetNode(pFunc->pParameterList, 1);
5,876,891✔
244
      if (pParam != NULL && nodeType(pParam) == QUERY_NODE_VALUE) {
5,876,891✔
245
        SValueNode* pConst = (SValueNode*)pParam;
5,876,891✔
246
        if (pConst->datum.i == 1) {
5,876,891✔
247
          return true;
705,358✔
248
        }
249
      }
250
    }
251
    return false;
5,165,404✔
252
  }
253
  return fmIsKeepOrderFuncId(pFunc->funcId);
648,559,326✔
254
}
255

256
bool fmIsCumulativeFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_CUMULATIVE_FUNC); }
137,225,346✔
257

258
bool fmIsForbidSysTableFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_FORBID_SYSTABLE_FUNC); }
544,320,124✔
259

260
bool fmIsInterpFunc(int32_t funcId) {
1,116,341,653✔
261
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,116,341,653✔
262
    return false;
353,298✔
263
  }
264
  return FUNCTION_TYPE_INTERP == funcMgtBuiltins[funcId].type;
1,115,990,147✔
265
}
266

267
bool fmIsInterpPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_INTERP_PC_FUNC); }
1,096,780,247✔
268

269
bool fmIsForecastFunc(int32_t funcId) {
1,069,968,358✔
270
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
1,069,968,358✔
271
    return false;
364,722✔
272
  }
273
  return FUNCTION_TYPE_FORECAST == funcMgtBuiltins[funcId].type;
1,069,604,330✔
274
}
275

276
bool fmIsAnalysisPseudoColumnFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_ANALYTICS_PC_FUNC); }
1,099,571,854✔
277

278
bool fmIsImputationCorrelationFunc(int32_t funcId) {
×
279
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
280
    return false;
×
281
  }
282

283
  int32_t type = funcMgtBuiltins[funcId].type;
×
284
  return FUNCTION_TYPE_IMPUTATION == type || FUNCTION_TYPE_DTW == type || FUNCTION_TYPE_DTW_PATH == type ||
×
285
         FUNCTION_TYPE_TLCC == type;
286
}
287

288
bool fmIsLastRowFunc(int32_t funcId) {
61,287,411✔
289
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
61,287,411✔
290
    return false;
137✔
291
  }
292
  return FUNCTION_TYPE_LAST_ROW == funcMgtBuiltins[funcId].type;
61,287,274✔
293
}
294

295
bool fmIsLastFunc(int32_t funcId) {
11,930✔
296
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
11,930✔
297
    return false;
×
298
  }
299
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type;
11,930✔
300
}
301

302
bool fmIsNotNullOutputFunc(int32_t funcId) {
491,027,651✔
303
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
491,027,651✔
304
    return false;
73,599✔
305
  }
306
  return FUNCTION_TYPE_LAST == funcMgtBuiltins[funcId].type ||
490,962,644✔
307
         FUNCTION_TYPE_CACHE_LAST == funcMgtBuiltins[funcId].type ||
472,284,195✔
308
         FUNCTION_TYPE_LAST_PARTIAL == funcMgtBuiltins[funcId].type ||
471,736,318✔
309
         FUNCTION_TYPE_LAST_MERGE == funcMgtBuiltins[funcId].type ||
460,527,562✔
310
         FUNCTION_TYPE_FIRST == funcMgtBuiltins[funcId].type ||
455,694,431✔
311
         FUNCTION_TYPE_FIRST_PARTIAL == funcMgtBuiltins[funcId].type ||
442,342,865✔
312
         FUNCTION_TYPE_FIRST_MERGE == funcMgtBuiltins[funcId].type ||
435,866,819✔
313
         FUNCTION_TYPE_COUNT == funcMgtBuiltins[funcId].type ||
433,253,830✔
314
         FUNCTION_TYPE_HYPERLOGLOG == funcMgtBuiltins[funcId].type ||
380,649,974✔
315
         FUNCTION_TYPE_HYPERLOGLOG_PARTIAL == funcMgtBuiltins[funcId].type ||
1,340,196,522✔
316
         FUNCTION_TYPE_HYPERLOGLOG_MERGE == funcMgtBuiltins[funcId].type;
376,914,284✔
317
}
318

319
bool fmIsSelectValueFunc(int32_t funcId) {
326,299,258✔
320
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
326,299,258✔
UNCOV
321
    return false;
×
322
  }
323
  return FUNCTION_TYPE_SELECT_VALUE == funcMgtBuiltins[funcId].type;
326,301,743✔
324
}
325

326
bool fmIsGroupKeyFunc(int32_t funcId) {
249,168,727✔
327
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
249,168,727✔
328
    return false;
277✔
329
  }
330
  return FUNCTION_TYPE_GROUP_KEY == funcMgtBuiltins[funcId].type;
249,169,697✔
331
}
332

333
bool fmIsHasNullFunc(int32_t funcId) {
×
334
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
335
    return false;
×
336
  }
337
  return FUNCTION_TYPE_HAS_NULL == funcMgtBuiltins[funcId].type;
×
338
}
339

340
bool fmisSelectGroupConstValueFunc(int32_t funcId) {
12,353,162✔
341
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
12,353,162✔
342
    return false;
×
343
  }
344
  return FUNCTION_TYPE_GROUP_CONST_VALUE == funcMgtBuiltins[funcId].type;
12,353,162✔
345
}
346

347
bool fmIsElapsedFunc(int32_t funcId) {
10,367,470✔
348
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
10,367,470✔
349
    return false;
×
350
  }
351
  return FUNCTION_TYPE_ELAPSED == funcMgtBuiltins[funcId].type;
10,367,470✔
352
}
353

354
bool fmIsBlockDistFunc(int32_t funcId) {
544,132,148✔
355
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
544,132,148✔
356
    return false;
164,776✔
357
  }
358
  return FUNCTION_TYPE_BLOCK_DIST == funcMgtBuiltins[funcId].type;
543,969,449✔
359
}
360

361
bool fmIsDBUsageFunc(int32_t funcId) {
544,137,256✔
362
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
544,137,256✔
363
    return false;
179,047✔
364
  }
365
  return FUNCTION_TYPE_DB_USAGE == funcMgtBuiltins[funcId].type;
543,958,924✔
366
}
367

368
bool fmIsProcessByRowFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PROCESS_BY_ROW); }
587,214,622✔
369

370
bool fmIsIgnoreNullFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_IGNORE_NULL_FUNC); }
278,077✔
371

372
void fmFuncMgtDestroy() {
1,713,381✔
373
  void* m = gFunMgtService.pFuncNameHashTable;
1,713,381✔
374
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
1,713,381✔
375
    taosHashCleanup(m);
1,713,338✔
376
  }
377
}
1,713,381✔
378

379
#ifdef BUILD_NO_CALL
380
int32_t fmSetNormalFunc(int32_t funcId, SFuncExecFuncs* pFpSet) {
381
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
382
    return TSDB_CODE_FAILED;
383
  }
384
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
385
  return TSDB_CODE_SUCCESS;
386
}
387
#endif
388

389
// function has same input/output type
390
bool fmIsSameInOutType(int32_t funcId) {
42,019,454✔
391
  bool res = false;
42,019,454✔
392
  switch (funcMgtBuiltins[funcId].type) {
42,019,454✔
393
    case FUNCTION_TYPE_MAX:
15,663,048✔
394
    case FUNCTION_TYPE_MIN:
395
    case FUNCTION_TYPE_TOP:
396
    case FUNCTION_TYPE_BOTTOM:
397
    case FUNCTION_TYPE_FIRST:
398
    case FUNCTION_TYPE_LAST:
399
    case FUNCTION_TYPE_SAMPLE:
400
    case FUNCTION_TYPE_TAIL:
401
    case FUNCTION_TYPE_UNIQUE:
402
      res = true;
15,663,048✔
403
      break;
15,663,048✔
404
    default:
26,356,447✔
405
      break;
26,356,447✔
406
  }
407
  return res;
42,019,495✔
408
}
409

410
bool fmIsConstantResFunc(SFunctionNode* pFunc) {
13,534,249✔
411
  SNode* pNode;
412
  FOREACH(pNode, pFunc->pParameterList) {
13,615,993✔
413
    if (nodeType(pNode) != QUERY_NODE_VALUE) {
215,148✔
414
      return false;
133,404✔
415
    }
416
  }
417
  return true;
13,400,845✔
418
}
419

420
bool fmIsSkipScanCheckFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_SKIP_SCAN_CHECK_FUNC); }
40,064,453✔
421

422
bool fmIsPrimaryKeyFunc(int32_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_PRIMARY_KEY_FUNC); }
473,068,717✔
423

424
bool fmIsPlaceHolderFunc(int32_t funcId) {return isSpecificClassifyFunc(funcId, FUNC_MGT_PLACE_HOLDER_FUNC); }
2,147,483,647✔
425

426
void getLastCacheDataType(SDataType* pType, int32_t pkBytes) {
×
427
  // TODO: do it later.
428
  pType->bytes = getFirstLastInfoSize(pType->bytes, pkBytes) + VARSTR_HEADER_SIZE;
×
429
  pType->type = TSDB_DATA_TYPE_BINARY;
×
430
}
×
431

432
static int32_t getFuncInfo(SFunctionNode* pFunc) {
134,736,499✔
433
  char msg[128] = {0};
134,736,499✔
434
  return fmGetFuncInfo(pFunc, msg, sizeof(msg));
134,736,499✔
435
}
436

437
int32_t createFunction(const char* pName, SNodeList* pParameterList, SFunctionNode** ppFunc) {
9,121,282✔
438
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
9,121,282✔
439
  if (NULL == *ppFunc) {
9,121,282✔
440
    return code;
×
441
  }
442
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
9,121,282✔
443
  (*ppFunc)->pParameterList = pParameterList;
9,121,282✔
444
  code = getFuncInfo((*ppFunc));
9,121,282✔
445
  if (TSDB_CODE_SUCCESS != code) {
9,121,282✔
446
    (*ppFunc)->pParameterList = NULL;
×
447
    nodesDestroyNode((SNode*)*ppFunc);
×
448
    *ppFunc = NULL;
×
449
    return code;
×
450
  }
451
  return code;
9,121,282✔
452
}
453

454
static void resetOutputChangedFunc(SFunctionNode *pFunc, const SFunctionNode* pSrcFunc) {
125,616,628✔
455
  if (funcMgtBuiltins[pFunc->funcId].type == FUNCTION_TYPE_LAST_MERGE) {
125,616,628✔
456
    pFunc->node.resType = pSrcFunc->node.resType;
18,701,786✔
457
    return;
18,701,786✔
458
  }
459
}
460

461
int32_t createFunctionWithSrcFunc(const char* pName, const SFunctionNode* pSrcFunc, SNodeList* pParameterList, SFunctionNode** ppFunc) {
125,615,889✔
462
  int32_t code = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)ppFunc);
125,615,889✔
463
  if (NULL == *ppFunc) {
125,619,688✔
464
    return code;
×
465
  }
466

467
  (*ppFunc)->hasPk = pSrcFunc->hasPk;
125,619,688✔
468
  (*ppFunc)->pkBytes = pSrcFunc->pkBytes;
125,619,651✔
469
  (*ppFunc)->pSrcFuncRef = pSrcFunc;
125,619,651✔
470

471
  (void)snprintf((*ppFunc)->functionName, sizeof((*ppFunc)->functionName), "%s", pName);
125,619,651✔
472
  (*ppFunc)->pParameterList = pParameterList;
125,619,647✔
473
  code = getFuncInfo((*ppFunc));
125,619,688✔
474
  if (TSDB_CODE_SUCCESS != code) {
125,618,501✔
475
    (*ppFunc)->pParameterList = NULL;
×
476
    nodesDestroyNode((SNode*)*ppFunc);
×
477
    *ppFunc = NULL;
×
478
    return code;
×
479
  }
480
  resetOutputChangedFunc(*ppFunc, pSrcFunc);
125,618,501✔
481
  (*ppFunc)->node.relatedTo = pSrcFunc->node.relatedTo;
125,617,222✔
482
  (*ppFunc)->node.bindExprID = pSrcFunc->node.bindExprID;
125,617,185✔
483
  return code;
125,617,144✔
484
}
485

486
static int32_t createColumnByFunc(const SFunctionNode* pFunc, SColumnNode** ppCol) {
83,586,446✔
487
  int32_t code = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)ppCol);
83,586,446✔
488
  if (NULL == *ppCol) {
83,593,502✔
489
    return code;
×
490
  }
491
  tstrncpy((*ppCol)->colName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
83,593,502✔
492
  (*ppCol)->node.resType = pFunc->node.resType;
83,593,465✔
493
  return TSDB_CODE_SUCCESS;
83,593,502✔
494
}
495

496
bool fmIsDistExecFunc(int32_t funcId) {
326,247,760✔
497
  if (fmIsUserDefinedFunc(funcId)) {
326,247,760✔
498
    return false;
99,343✔
499
  }
500
  if (!fmIsVectorFunc(funcId)) {
326,170,839✔
501
    return true;
217,251✔
502
  }
503
  return (NULL != funcMgtBuiltins[funcId].pPartialFunc && NULL != funcMgtBuiltins[funcId].pMergeFunc);
325,960,032✔
504
}
505

506
static int32_t createPartialFunction(const SFunctionNode* pSrcFunc, SFunctionNode** pPartialFunc) {
42,021,336✔
507
  SNodeList* pParameterList = NULL;
42,021,336✔
508
  int32_t    code = nodesCloneList(pSrcFunc->pParameterList, &pParameterList);
42,021,336✔
509
  if (NULL == pParameterList) {
42,024,500✔
510
    return code;
×
511
  }
512
  code =
513
      createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pPartialFunc, pSrcFunc, pParameterList, pPartialFunc);
42,024,500✔
514
  if (TSDB_CODE_SUCCESS != code) {
42,021,983✔
515
    nodesDestroyList(pParameterList);
×
516
    return code;
×
517
  }
518
  (*pPartialFunc)->hasOriginalFunc = true;
42,022,239✔
519
  (*pPartialFunc)->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
42,022,280✔
520
  char name[TSDB_FUNC_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_POINTER_PRINT_BYTES + 1] = {0};
42,022,321✔
521

522
  int32_t len = tsnprintf(name, sizeof(name), "%s.%p", (*pPartialFunc)->functionName, pSrcFunc);
42,022,358✔
523
  if (taosHashBinary(name, len) < 0) {
42,022,660✔
524
    return TSDB_CODE_FAILED;
×
525
  }
526
  tstrncpy((*pPartialFunc)->node.aliasName, name, TSDB_COL_NAME_LEN);
42,022,660✔
527
  return TSDB_CODE_SUCCESS;
42,022,623✔
528
}
529

530
static int32_t createMergeFuncPara(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
83,586,682✔
531
                                   SNodeList** pParameterList) {
532
  SNode*  pRes = NULL;
83,586,682✔
533
  int32_t code = createColumnByFunc(pPartialFunc, (SColumnNode**)&pRes);
83,586,682✔
534
  if (TSDB_CODE_SUCCESS != code) {
83,592,606✔
535
    return code;
×
536
  }
537
  if (NULL != funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc) {
83,592,606✔
538
    return funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc(pSrcFunc->pParameterList, pRes, pParameterList);
667,690✔
539
  } else {
540
    return nodesListMakeStrictAppend(pParameterList, pRes);
82,924,916✔
541
  }
542
}
543

544
static int32_t createMidFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
41,565,193✔
545
                                 SFunctionNode** pMidFunc) {
546
  SNodeList*     pParameterList = NULL;
41,565,193✔
547
  SFunctionNode* pFunc = NULL;
41,565,193✔
548

549
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
41,565,156✔
550
  if (TSDB_CODE_SUCCESS == code) {
41,571,557✔
551
    if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){
41,571,749✔
552
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pSrcFunc, pParameterList, &pFunc);
4,940,167✔
553
    }else{
554
      code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
36,631,582✔
555
    }
556
  }
557
  if (TSDB_CODE_SUCCESS == code) {
41,569,474✔
558
    tstrncpy(pFunc->node.aliasName, pPartialFunc->node.aliasName, TSDB_COL_NAME_LEN);
41,570,345✔
559
  }
560

561
  if (TSDB_CODE_SUCCESS == code) {
41,569,433✔
562
    *pMidFunc = pFunc;
41,569,433✔
563
  } else {
564
    nodesDestroyList(pParameterList);
×
565
  }
566
  return code;
41,570,155✔
567
}
568

569
static int32_t createMergeFunction(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc,
42,021,369✔
570
                                   SFunctionNode** pMergeFunc) {
571
  SNodeList*     pParameterList = NULL;
42,021,369✔
572
  SFunctionNode* pFunc = NULL;
42,021,369✔
573

574
  int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList);
42,021,328✔
575
  if (TSDB_CODE_SUCCESS == code) {
42,024,445✔
576
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pSrcFunc, pParameterList, &pFunc);
42,024,509✔
577
  }
578
  if (TSDB_CODE_SUCCESS == code) {
42,027,711✔
579
    pFunc->hasOriginalFunc = true;
42,024,908✔
580
    pFunc->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId;
42,024,908✔
581
    // overwrite function restype set by translate function
582
    if (fmIsSameInOutType(pSrcFunc->funcId)) {
42,024,908✔
583
      pFunc->node.resType = pSrcFunc->node.resType;
15,663,456✔
584
    }
585
    tstrncpy(pFunc->node.aliasName, pSrcFunc->node.aliasName, TSDB_COL_NAME_LEN);
42,018,990✔
586
  }
587

588
  if (TSDB_CODE_SUCCESS == code) {
42,021,834✔
589
    *pMergeFunc = pFunc;
42,021,834✔
590
  } else {
591
    nodesDestroyList(pParameterList);
×
592
  }
593
  return code;
42,021,577✔
594
}
595

596
int32_t fmGetDistMethod(const SFunctionNode* pFunc, SFunctionNode** pPartialFunc, SFunctionNode** pMidFunc,
42,018,391✔
597
                        SFunctionNode** pMergeFunc) {
598
  if (!fmIsDistExecFunc(pFunc->funcId)) {
42,018,391✔
599
    return TSDB_CODE_FAILED;
×
600
  }
601

602
  int32_t code = createPartialFunction(pFunc, pPartialFunc);
42,023,543✔
603
  if (TSDB_CODE_SUCCESS == code && pMidFunc) {
42,020,013✔
604
    code = createMidFunction(pFunc, *pPartialFunc, pMidFunc);
41,570,000✔
605
  }
606
  if (TSDB_CODE_SUCCESS == code) {
42,020,393✔
607
    code = createMergeFunction(pFunc, *pPartialFunc, pMergeFunc);
42,023,629✔
608
  }
609

610
  if (TSDB_CODE_SUCCESS != code) {
42,016,333✔
611
    nodesDestroyNode((SNode*)*pPartialFunc);
×
612
    if (pMidFunc) nodesDestroyNode((SNode*)*pMidFunc);
×
613
    nodesDestroyNode((SNode*)*pMergeFunc);
×
614
  }
615

616
  return code;
42,021,002✔
617
}
618

619
const char* fmGetFuncName(int32_t funcId) {
326,032✔
620
  if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) {
326,032✔
621
    return "invalid function";
×
622
  }
623
  return funcMgtBuiltins[funcId].name;
326,032✔
624
}
625

626
/// @param [out] pStateFunc, not changed if error occured or no need to create state func
627
/// @retval 0 for succ, otherwise err occured
628
static int32_t fmCreateStateFunc(const SFunctionNode* pFunc, SFunctionNode** pStateFunc) {
7,155✔
629
  if (funcMgtBuiltins[pFunc->funcId].pStateFunc) {
7,155✔
630
    SNodeList* pParams = NULL;
7,155✔
631
    int32_t    code = nodesCloneList(pFunc->pParameterList, &pParams);
7,155✔
632
    if (!pParams) return code;
7,155✔
633
    code = createFunction(funcMgtBuiltins[pFunc->funcId].pStateFunc, pParams, pStateFunc);
7,155✔
634
    if (TSDB_CODE_SUCCESS != code) {
7,155✔
635
      nodesDestroyList(pParams);
×
636
      return code;
×
637
    }
638
    tstrncpy((*pStateFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
7,155✔
639
    tstrncpy((*pStateFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
7,155✔
640
  }
641
  return TSDB_CODE_SUCCESS;
7,155✔
642
}
643

644
bool fmIsTSMASupportedFunc(func_id_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_TSMA_FUNC); }
1,163,922✔
645

646
bool fmIsRsmaSupportedFunc(func_id_t funcId) { return isSpecificClassifyFunc(funcId, FUNC_MGT_RSMA_FUNC); }
130,538✔
647

648
int32_t fmCreateStateFuncs(SNodeList* pFuncs) {
3,232✔
649
  int32_t code;
650
  SNode*  pNode;
651
  char    buf[128] = {0};
3,232✔
652
  FOREACH(pNode, pFuncs) {
10,387✔
653
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
7,155✔
654
    code = fmGetFuncInfo(pFunc, buf, 128);
7,155✔
655
    if (code) break;
7,155✔
656
    if (fmIsTSMASupportedFunc(pFunc->funcId)) {
7,155✔
657
      SFunctionNode* pNewFunc = NULL;
7,155✔
658
      code = fmCreateStateFunc(pFunc, &pNewFunc);
7,155✔
659
      if (code) {
7,155✔
660
        // error
661
        break;
×
662
      } else if (!pNewFunc) {
7,155✔
663
        // no need state func
664
        continue;
×
665
      } else {
666
        REPLACE_NODE(pNewFunc);
7,155✔
667
        nodesDestroyNode(pNode);
7,155✔
668
      }
669
    }
670
  }
671
  return code;
3,232✔
672
}
673

674
static int32_t fmCreateStateMergeFunc(SFunctionNode* pFunc, SFunctionNode** pStateMergeFunc) {
1,540✔
675
  if (funcMgtBuiltins[pFunc->funcId].pMergeFunc) {
1,540✔
676
    SNodeList* pParams = NULL;
1,260✔
677
    int32_t    code = nodesCloneList(pFunc->pParameterList, &pParams);
1,260✔
678
    if (!pParams) return code;
1,260✔
679
    code = createFunctionWithSrcFunc(funcMgtBuiltins[pFunc->funcId].pMergeFunc, pFunc, pParams, pStateMergeFunc);
1,260✔
680
    if (TSDB_CODE_SUCCESS != code) {
1,260✔
681
      nodesDestroyList(pParams);
×
682
      return code;
×
683
    }
684
    tstrncpy((*pStateMergeFunc)->node.aliasName, pFunc->node.aliasName, TSDB_COL_NAME_LEN);
1,260✔
685
    tstrncpy((*pStateMergeFunc)->node.userAlias, pFunc->node.userAlias, TSDB_COL_NAME_LEN);
1,260✔
686
  }
687
  return TSDB_CODE_SUCCESS;
1,540✔
688
}
689

690
int32_t fmCreateStateMergeFuncs(SNodeList* pFuncs) {
700✔
691
  int32_t code;
692
  SNode*  pNode;
693
  char    buf[128] = {0};
700✔
694
  FOREACH(pNode, pFuncs) {
2,240✔
695
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
1,540✔
696
    if (fmIsTSMASupportedFunc(pFunc->funcId)) {
1,540✔
697
      SFunctionNode* pNewFunc = NULL;
1,540✔
698
      code = fmCreateStateMergeFunc(pFunc, &pNewFunc);
1,540✔
699
      if (code) {
1,540✔
700
        // error
701
        break;
×
702
      } else if (!pNewFunc) {
1,540✔
703
        // no state merge func
704
        continue;
280✔
705
      } else {
706
        REPLACE_NODE(pNewFunc);
1,260✔
707
        nodesDestroyNode(pNode);
1,260✔
708
      }
709
    }
710
  }
711
  return code;
700✔
712
}
713

714
int32_t fmGetFuncId(const char* name) {
1,909,026✔
715
  if (NULL != gFunMgtService.pFuncNameHashTable) {
1,909,026✔
716
    void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, name, strlen(name));
1,909,026✔
717
    if (NULL != pVal) {
1,909,026✔
718
      return *(int32_t*)pVal;
1,909,026✔
719
    }
720
    return -1;
×
721
  }
722
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
×
723
    if (0 == strcmp(funcMgtBuiltins[i].name, name)) {
×
724
      return i;
×
725
    }
726
  }
727
  return -1;
×
728
}
729

730
bool fmIsMyStateFunc(int32_t funcId, int32_t stateFuncId) {
1,980,580✔
731
  const SBuiltinFuncDefinition* pFunc = &funcMgtBuiltins[funcId];
1,980,580✔
732
  const SBuiltinFuncDefinition* pStateFunc = &funcMgtBuiltins[stateFuncId];
1,980,580✔
733
  if (!pFunc->pStateFunc) {
1,980,580✔
734
    return false;
×
735
  }
736
  if (strcmp(pFunc->pStateFunc, pStateFunc->name) == 0) return true;
1,980,580✔
737
  int32_t stateMergeFuncId = fmGetFuncId(pFunc->pStateFunc);
724,500✔
738
  if (stateMergeFuncId == -1) {
724,500✔
739
    return false;
×
740
  }
741
  const SBuiltinFuncDefinition* pStateMergeFunc = &funcMgtBuiltins[stateMergeFuncId];
724,500✔
742
  return strcmp(pStateFunc->name, pStateMergeFunc->pMergeFunc) == 0;
724,500✔
743
}
744

745
bool fmIsCountLikeFunc(int32_t funcId) {
487,612,191✔
746
  return isSpecificClassifyFunc(funcId, FUNC_MGT_COUNT_LIKE_FUNC);
487,612,191✔
747
}
748

749
bool fmIsRowTsOriginFunc(int32_t funcId) {
5,080,327✔
750
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
5,080,327✔
751
    return false;
×
752
  }
753
  return FUNCTION_TYPE_IROWTS_ORIGIN == funcMgtBuiltins[funcId].type;
5,080,327✔
754
}
755

756
bool fmIsGroupIdFunc(int32_t funcId) {
×
757
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
×
758
    return false;
×
759
  }
760
  return FUNCTION_TYPE_GROUP_ID == funcMgtBuiltins[funcId].type;
×
761
}
762

763
const void* fmGetStreamPesudoFuncVal(int32_t funcId, const SStreamRuntimeFuncInfo* pStreamRuntimeFuncInfo) {
42,886,502✔
764
  SSTriggerCalcParam *pParams = taosArrayGet(pStreamRuntimeFuncInfo->pStreamPesudoFuncVals, pStreamRuntimeFuncInfo->curIdx);
42,886,502✔
765
  switch (funcMgtBuiltins[funcId].type) {
42,889,731✔
766
    case FUNCTION_TYPE_TPREV_TS:
×
767
      return &pParams->prevTs;
×
768
    case FUNCTION_TYPE_TCURRENT_TS:
2,141,088✔
769
      return &pParams->currentTs;
2,141,088✔
770
    case FUNCTION_TYPE_TNEXT_TS:
×
771
      return &pParams->nextTs;
×
772
    case FUNCTION_TYPE_TWSTART:
20,571,666✔
773
      return &pParams->wstart;
20,571,666✔
774
    case FUNCTION_TYPE_TWEND:
15,845,049✔
775
      return &pParams->wend;
15,845,049✔
776
    case FUNCTION_TYPE_TWDURATION:
1,010,458✔
777
      return &pParams->wduration;
1,010,458✔
778
    case FUNCTION_TYPE_TWROWNUM:
93,923✔
779
      return &pParams->wrownum;
93,923✔
780
    case FUNCTION_TYPE_TPREV_LOCALTIME:
122,987✔
781
      return &pParams->prevLocalTime;
122,987✔
782
    case FUNCTION_TYPE_TLOCALTIME:
308,752✔
783
      return &pParams->triggerTime;
308,752✔
784
    case FUNCTION_TYPE_TNEXT_LOCALTIME:
193,233✔
785
      return &pParams->nextLocalTime;
193,233✔
786
    case FUNCTION_TYPE_TGRPID:
114,877✔
787
      return &pStreamRuntimeFuncInfo->groupId;
114,877✔
788
    case FUNCTION_TYPE_PLACEHOLDER_COLUMN:
746,401✔
789
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
746,401✔
790
    case FUNCTION_TYPE_PLACEHOLDER_TBNAME:
1,739,394✔
791
      return pStreamRuntimeFuncInfo->pStreamPartColVals;
1,739,394✔
792
    default:
×
793
      break;
×
794
  }
795
  return NULL;
×
796
}
797

798
bool fmIsStreamPesudoColVal(int32_t funcId) {
499,260✔
799
  return funcMgtBuiltins[funcId].type == FUNCTION_TYPE_PLACEHOLDER_COLUMN
499,260✔
800
         || funcMgtBuiltins[funcId].type == FUNCTION_TYPE_PLACEHOLDER_TBNAME;
499,260✔
801
}
802

803
int32_t fmGetStreamPesudoFuncEnv(int32_t funcId, SNodeList* pParamNodes, SFuncExecEnv *pEnv) {
6,419,586✔
804
  if (NULL == pParamNodes || pParamNodes->length < 1) {
6,419,586✔
UNCOV
805
    uError("invalid stream pesudo func param list %p", pParamNodes);
×
806
    return TSDB_CODE_INTERNAL_ERROR;
×
807
  }
808

809
  int32_t firstParamType = nodeType(nodesListGetNode(pParamNodes, 0));
6,419,586✔
810
  if (QUERY_NODE_VALUE != firstParamType) {
6,420,032✔
811
    uError("invalid stream pesudo func first param type %d", firstParamType);
×
812
    return TSDB_CODE_INTERNAL_ERROR;
×
813
  }
814

815
  SValueNode* pResDesc = (SValueNode*)nodesListGetNode(pParamNodes, 0);
6,420,032✔
816
  pEnv->calcMemSize = pResDesc->node.resType.bytes;
6,419,809✔
817

818
  return TSDB_CODE_SUCCESS;
6,419,809✔
819
}
820

821

822
int32_t fmSetStreamPseudoFuncParamVal(int32_t funcId, SNodeList* pParamNodes, const SStreamRuntimeFuncInfo* pStreamRuntimeInfo) {
42,881,146✔
823
  if (!pStreamRuntimeInfo) {
42,881,146✔
824
    uError("internal error, should have pVals for stream pseudo funcs");
×
825
    return TSDB_CODE_INTERNAL_ERROR;
×
826
  }
827
  int32_t code = 0;
42,881,146✔
828
  SArray *pVals1 = NULL;
42,881,146✔
829
  SNode* pFirstParam = nodesListGetNode(pParamNodes, 0);
42,881,146✔
830
  if (nodeType(pFirstParam) != QUERY_NODE_VALUE) {
42,881,634✔
831
    uError("invalid param node type: %d for func: %d", nodeType(pFirstParam), funcId);
×
832
    return TSDB_CODE_INTERNAL_ERROR;
×
833
  }
834

835
  int32_t t = funcMgtBuiltins[funcId].type;
42,881,457✔
836
  uDebug("set stream pseudo func param val, functype: %d, pStreamRuntimeInfo: %p", t, pStreamRuntimeInfo);
42,883,247✔
837
  if (FUNCTION_TYPE_TGRPID == t) {
42,888,087✔
838
    SValue v = {0};
114,877✔
839
    v.type = TSDB_DATA_TYPE_BIGINT;
114,877✔
840
    v.val = *(int64_t*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
114,877✔
841
    
842
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&v, pFirstParam->type));
114,877✔
843
    if (code != 0) {
114,877✔
844
      uError("failed to set value node value: %s", tstrerror(code));
×
845
      return code;
×
846
    }
847
  } else if (FUNCTION_TYPE_PLACEHOLDER_COLUMN == t) {
42,773,210✔
848
    SNode* pSecondParam = nodesListGetNode(pParamNodes, 1);
746,401✔
849
    if (nodeType(pSecondParam) != QUERY_NODE_VALUE) {
746,401✔
850
      uError("invalid param node type: %d for func: %d", nodeType(pSecondParam), funcId);
×
851
      return TSDB_CODE_INTERNAL_ERROR;
×
852
    }
853
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
746,401✔
854
    int32_t idx = ((SValueNode*)pSecondParam)->datum.i;
746,401✔
855
    if (idx - 1 < 0 || idx - 1 >= taosArrayGetSize(pVal)) {
746,401✔
856
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
×
857
      return TSDB_CODE_INTERNAL_ERROR;
×
858
    }
859
    SStreamGroupValue* pValue = taosArrayGet(pVal, idx - 1);
746,401✔
860
    if (pValue == NULL) {
746,401✔
861
      uError("invalid idx: %d for func: %d, should be in [1, %d]", idx, funcId, (int32_t)taosArrayGetSize(pVal));
×
862
      return TSDB_CODE_INTERNAL_ERROR;
×
863
    }
864
    if (!pValue->isNull){
746,401✔
865
      if (pValue->data.type != ((SValueNode*)pFirstParam)->node.resType.type){
740,241✔
866
        uError("invalid value type: %d for func: %d, should be: %d", pValue->data.type, funcId, ((SValueNode*)pFirstParam)->node.resType.type);
×
867
        return TSDB_CODE_INTERNAL_ERROR;
×
868
      }
869
      if (IS_VAR_DATA_TYPE(((SValueNode*)pFirstParam)->node.resType.type)) {
739,981✔
870
        char* tmp = taosMemoryCalloc(1, pValue->data.nData + VARSTR_HEADER_SIZE); 
522,229✔
871
        if (tmp == NULL) {
522,489✔
872
          return TSDB_CODE_OUT_OF_MEMORY;
×
873
        }
874
        taosMemoryFree(((SValueNode*)pFirstParam)->datum.p);
522,489✔
875
        ((SValueNode*)pFirstParam)->datum.p = tmp;
522,489✔
876
        memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
522,489✔
877
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
522,489✔
878
      } else {
879
        code = nodesSetValueNodeValue((SValueNode*)pFirstParam, VALUE_GET_DATUM(&pValue->data, pValue->data.type));
217,752✔
880
      }
881
    }
882
    ((SValueNode*)pFirstParam)->isNull = pValue->isNull;
745,881✔
883
  } else if (FUNCTION_TYPE_PLACEHOLDER_TBNAME == t) {
42,026,809✔
884
    SArray* pVal = (SArray*)fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
1,739,908✔
885
    for (int32_t i = 0; i < taosArrayGetSize(pVal); ++i) {
2,143,108✔
886
      SStreamGroupValue* pValue = taosArrayGet(pVal, i);
2,143,366✔
887
      if (pValue != NULL && pValue->isTbname) {
2,143,627✔
888
        taosMemoryFreeClear(((SValueNode*)pFirstParam)->datum.p);
1,740,941✔
889
        ((SValueNode*)pFirstParam)->datum.p = taosMemoryCalloc(pValue->data.nData + VARSTR_HEADER_SIZE, 1);
1,740,940✔
890
        if (NULL == ((SValueNode*)pFirstParam)->datum.p ) {
1,740,417✔
891
          return terrno;
×
892
        }
893

894
        (void)memcpy(varDataVal(((SValueNode*)pFirstParam)->datum.p), pValue->data.pData, pValue->data.nData);
1,739,903✔
895
        varDataLen(((SValueNode*)pFirstParam)->datum.p) = pValue->data.nData;
1,740,155✔
896
        break;
1,739,381✔
897
      }
898
    }
899
  } else if (LIST_LENGTH(pParamNodes) == 1) {
80,574,502✔
900
    // twstart, twend
901
    const void* pVal = fmGetStreamPesudoFuncVal(funcId, pStreamRuntimeInfo);
40,287,316✔
902
    if (!pVal) {
40,286,063✔
903
      uError("failed to set stream pseudo func param val, NULL val for funcId: %d", funcId);
×
904
      return TSDB_CODE_INTERNAL_ERROR;
×
905
    }
906
    code = nodesSetValueNodeValue((SValueNode*)pFirstParam, (void*)pVal);
40,286,063✔
907
    if (code != 0) {
40,287,857✔
908
      uError("failed to set value node value: %s", tstrerror(code));
260✔
909
      return code;
×
910
    }
911
  } else {
912
    uError("invalid placeholder function type: %d", t);
1,366✔
913
    return TSDB_CODE_INTERNAL_ERROR;
×
914
  }
915
  
916
  return code;
42,889,535✔
917
}
918

919

920

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