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

taosdata / TDengine / #3798

31 Mar 2025 10:39AM UTC coverage: 9.424% (-20.9%) from 30.372%
#3798

push

travis-ci

happyguoxy
test:add test cases

21549 of 307601 branches covered (7.01%)

Branch coverage included in aggregate %.

36084 of 303967 relevant lines covered (11.87%)

58620.7 hits per line

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

0.0
/source/libs/executor/src/forecastoperator.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 "executorInt.h"
17
#include "filter.h"
18
#include "functionMgt.h"
19
#include "operator.h"
20
#include "querytask.h"
21
#include "tanalytics.h"
22
#include "tcommon.h"
23
#include "tcompare.h"
24
#include "tdatablock.h"
25
#include "tfill.h"
26
#include "ttime.h"
27

28
#ifdef USE_ANALYTICS
29

30
typedef struct {
31
  char         algoName[TSDB_ANALYTIC_ALGO_NAME_LEN];
32
  char         algoUrl[TSDB_ANALYTIC_ALGO_URL_LEN];
33
  char         algoOpt[TSDB_ANALYTIC_ALGO_OPTION_LEN];
34
  int64_t      maxTs;
35
  int64_t      minTs;
36
  int64_t      numOfRows;
37
  uint64_t     groupId;
38
  int64_t      optRows;
39
  int64_t      cachedRows;
40
  int32_t      numOfBlocks;
41
  int64_t      timeout;
42
  int16_t      resTsSlot;
43
  int16_t      resValSlot;
44
  int16_t      resLowSlot;
45
  int16_t      resHighSlot;
46
  int16_t      inputTsSlot;
47
  int16_t      inputValSlot;
48
  int8_t       inputValType;
49
  int8_t       inputPrecision;
50
  SAnalyticBuf analyBuf;
51
} SForecastSupp;
52

53
typedef struct SForecastOperatorInfo {
54
  SSDataBlock*  pRes;
55
  SExprSupp     scalarSup;  // scalar calculation
56
  SForecastSupp forecastSupp;
57
} SForecastOperatorInfo;
58

59
static void destroyForecastInfo(void* param);
60

61
static FORCE_INLINE int32_t forecastEnsureBlockCapacity(SSDataBlock* pBlock, int32_t newRowsNum) {
62
  if (pBlock->info.rows < pBlock->info.capacity) {
×
63
    return TSDB_CODE_SUCCESS;
×
64
  }
65

66
  int32_t code = blockDataEnsureCapacity(pBlock, newRowsNum);
×
67
  if (code != TSDB_CODE_SUCCESS) {
×
68
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
69
    return code;
×
70
  }
71

72
  return TSDB_CODE_SUCCESS;
×
73
}
74

75
static int32_t forecastCacheBlock(SForecastSupp* pSupp, SSDataBlock* pBlock, const char* id) {
×
76
  int32_t       code = TSDB_CODE_SUCCESS;
×
77
  int32_t       lino = 0;
×
78
  SAnalyticBuf* pBuf = &pSupp->analyBuf;
×
79

80
  if (pSupp->cachedRows > ANALY_FORECAST_MAX_ROWS) {
×
81
    code = TSDB_CODE_ANA_ANODE_TOO_MANY_ROWS;
×
82
    qError("%s rows:%" PRId64 " for forecast cache, error happens, code:%s, upper limit:%d", id, pSupp->cachedRows,
×
83
           tstrerror(code), ANALY_FORECAST_MAX_ROWS);
84
    return code;
×
85
  }
86

87
  pSupp->numOfBlocks++;
×
88
  qDebug("%s block:%d, %p rows:%" PRId64, id, pSupp->numOfBlocks, pBlock, pBlock->info.rows);
×
89

90
  for (int32_t j = 0; j < pBlock->info.rows; ++j) {
×
91
    SColumnInfoData* pValCol = taosArrayGet(pBlock->pDataBlock, pSupp->inputValSlot);
×
92
    SColumnInfoData* pTsCol = taosArrayGet(pBlock->pDataBlock, pSupp->inputTsSlot);
×
93
    if (pTsCol == NULL || pValCol == NULL) break;
×
94

95
    int64_t ts = ((TSKEY*)pTsCol->pData)[j];
×
96
    char*   val = colDataGetData(pValCol, j);
×
97
    int16_t valType = pValCol->info.type;
×
98

99
    pSupp->minTs = MIN(pSupp->minTs, ts);
×
100
    pSupp->maxTs = MAX(pSupp->maxTs, ts);
×
101
    pSupp->numOfRows++;
×
102

103
    code = taosAnalyBufWriteColData(pBuf, 0, TSDB_DATA_TYPE_TIMESTAMP, &ts);
×
104
    if (TSDB_CODE_SUCCESS != code) {
×
105
      qError("%s failed to write ts in buf, code:%s", id, tstrerror(code));
×
106
      return code;
×
107
    }
108

109
    code = taosAnalyBufWriteColData(pBuf, 1, valType, val);
×
110
    if (TSDB_CODE_SUCCESS != code) {
×
111
      qError("%s failed to write val in buf, code:%s", id, tstrerror(code));
×
112
      return code;
×
113
    }
114
  }
115

116
  return 0;
×
117
}
118

119
static int32_t forecastCloseBuf(SForecastSupp* pSupp, const char* id) {
×
120
  SAnalyticBuf* pBuf = &pSupp->analyBuf;
×
121
  int32_t       code = 0;
×
122

123
  for (int32_t i = 0; i < 2; ++i) {
×
124
    code = taosAnalyBufWriteColEnd(pBuf, i);
×
125
    if (code != 0) return code;
×
126
  }
127

128
  code = taosAnalyBufWriteDataEnd(pBuf);
×
129
  if (code != 0) return code;
×
130

131
  code = taosAnalyBufWriteOptStr(pBuf, "option", pSupp->algoOpt);
×
132
  if (code != 0) return code;
×
133

134
  code = taosAnalyBufWriteOptStr(pBuf, "algo", pSupp->algoName);
×
135
  if (code != 0) return code;
×
136

137
  const char* prec = TSDB_TIME_PRECISION_MILLI_STR;
×
138
  if (pSupp->inputPrecision == TSDB_TIME_PRECISION_MICRO) prec = TSDB_TIME_PRECISION_MICRO_STR;
×
139
  if (pSupp->inputPrecision == TSDB_TIME_PRECISION_NANO) prec = TSDB_TIME_PRECISION_NANO_STR;
×
140
  code = taosAnalyBufWriteOptStr(pBuf, "prec", prec);
×
141
  if (code != 0) return code;
×
142

143
  int64_t wncheck = ANALY_FORECAST_DEFAULT_WNCHECK;
×
144
  bool    hasWncheck = taosAnalyGetOptInt(pSupp->algoOpt, "wncheck", &wncheck);
×
145
  if (!hasWncheck) {
×
146
    qDebug("%s forecast wncheck not found from %s, use default:%" PRId64, id, pSupp->algoOpt, wncheck);
×
147
  }
148

149
  code = taosAnalyBufWriteOptInt(pBuf, "wncheck", wncheck);
×
150
  if (code != 0) return code;
×
151

152
  bool noConf = (pSupp->resHighSlot == -1 && pSupp->resLowSlot == -1);
×
153
  code = taosAnalyBufWriteOptInt(pBuf, "return_conf", !noConf);
×
154
  if (code != 0) return code;
×
155

156
  pSupp->optRows = ANALY_FORECAST_DEFAULT_ROWS;
×
157
  bool hasRows = taosAnalyGetOptInt(pSupp->algoOpt, "rows", &pSupp->optRows);
×
158
  if (!hasRows) {
×
159
    qDebug("%s forecast rows not found from %s, use default:%" PRId64, id, pSupp->algoOpt, pSupp->optRows);
×
160
  }
161

162
  if (pSupp->optRows > ANALY_FORECAST_RES_MAX_ROWS) {
×
163
    qError("%s required too many forecast rows, max allowed:%d, required:%" PRId64, id, ANALY_FORECAST_RES_MAX_ROWS,
×
164
           pSupp->optRows);
165
    return TSDB_CODE_ANA_ANODE_TOO_MANY_ROWS;
×
166
  }
167

168
  code = taosAnalyBufWriteOptInt(pBuf, "forecast_rows", pSupp->optRows);
×
169
  if (code != 0) return code;
×
170

171
  int64_t conf = ANALY_FORECAST_DEFAULT_CONF;
×
172
  bool    hasConf = taosAnalyGetOptInt(pSupp->algoOpt, "conf", &conf);
×
173
  if (!hasConf) {
×
174
    qDebug("%s forecast conf not found from %s, use default:%" PRId64, id, pSupp->algoOpt, conf);
×
175
  }
176
  code = taosAnalyBufWriteOptInt(pBuf, "conf", conf);
×
177
  if (code != 0) return code;
×
178

179
  int32_t len = strlen(pSupp->algoOpt);
×
180
  int64_t every = (pSupp->maxTs - pSupp->minTs) / (pSupp->numOfRows - 1);
×
181
  int64_t start = pSupp->maxTs + every;
×
182
  bool    hasStart = taosAnalyGetOptInt(pSupp->algoOpt, "start", &start);
×
183
  if (!hasStart) {
×
184
    qDebug("%s forecast start not found from %s, use %" PRId64, id, pSupp->algoOpt, start);
×
185
  }
186
  code = taosAnalyBufWriteOptInt(pBuf, "start", start);
×
187
  if (code != 0) return code;
×
188

189
  bool hasEvery = taosAnalyGetOptInt(pSupp->algoOpt, "every", &every);
×
190
  if (!hasEvery) {
×
191
    qDebug("%s forecast every not found from %s, use %" PRId64, id, pSupp->algoOpt, every);
×
192
  }
193
  code = taosAnalyBufWriteOptInt(pBuf, "every", every);
×
194
  if (code != 0) return code;
×
195

196
  code = taosAnalyBufClose(pBuf);
×
197
  return code;
×
198
}
199

200
static int32_t forecastAnalysis(SForecastSupp* pSupp, SSDataBlock* pBlock, const char* pId) {
×
201
  SAnalyticBuf* pBuf = &pSupp->analyBuf;
×
202
  int32_t       resCurRow = pBlock->info.rows;
×
203
  int8_t        tmpI8 = 0;
×
204
  int16_t       tmpI16 = 0;
×
205
  int32_t       tmpI32 = 0;
×
206
  int64_t       tmpI64 = 0;
×
207
  float         tmpFloat = 0;
×
208
  double        tmpDouble = 0;
×
209
  int32_t       code = 0;
×
210

211
  SColumnInfoData* pResValCol = taosArrayGet(pBlock->pDataBlock, pSupp->resValSlot);
×
212
  if (NULL == pResValCol) {
×
213
    return terrno;
×
214
  }
215

216
  SColumnInfoData* pResTsCol = ((pSupp->resTsSlot != -1) ? taosArrayGet(pBlock->pDataBlock, pSupp->resTsSlot) : NULL);
×
217
  SColumnInfoData* pResLowCol =
×
218
      ((pSupp->resLowSlot != -1) ? taosArrayGet(pBlock->pDataBlock, pSupp->resLowSlot) : NULL);
×
219
  SColumnInfoData* pResHighCol =
×
220
      (pSupp->resHighSlot != -1 ? taosArrayGet(pBlock->pDataBlock, pSupp->resHighSlot) : NULL);
×
221

222
  SJson* pJson = taosAnalySendReqRetJson(pSupp->algoUrl, ANALYTICS_HTTP_TYPE_POST, pBuf, pSupp->timeout * 1000);
×
223
  if (pJson == NULL) {
×
224
    return terrno;
×
225
  }
226

227
  int32_t rows = 0;
×
228
  tjsonGetInt32ValueFromDouble(pJson, "rows", rows, code);
×
229
  if (rows < 0 && code == 0) {
×
230
    char pMsg[1024] = {0};
×
231
    code = tjsonGetStringValue(pJson, "msg", pMsg);
×
232
    if (code != 0) {
×
233
      qError("%s failed to get msg from rsp, unknown error", pId);
×
234
    } else {
235
      qError("%s failed to exec forecast, msg:%s", pId, pMsg);
×
236
    }
237

238
    tjsonDelete(pJson);
×
239
    return TSDB_CODE_ANA_ANODE_RETURN_ERROR;
×
240
  }
241

242
  if (code < 0) {
×
243
    goto _OVER;
×
244
  }
245

246
  SJson* res = tjsonGetObjectItem(pJson, "res");
×
247
  if (res == NULL) goto _OVER;
×
248
  int32_t ressize = tjsonGetArraySize(res);
×
249
  bool    returnConf = (pSupp->resHighSlot != -1 || pSupp->resLowSlot != -1);
×
250

251
  if ((returnConf && (ressize != 4)) || ((!returnConf) && (ressize != 2))) {
×
252
    goto _OVER;
×
253
  }
254

255
  if (pResTsCol != NULL) {
×
256
    resCurRow = pBlock->info.rows;
×
257
    SJson* tsJsonArray = tjsonGetArrayItem(res, 0);
×
258
    if (tsJsonArray == NULL) goto _OVER;
×
259
    int32_t tsSize = tjsonGetArraySize(tsJsonArray);
×
260
    if (tsSize != rows) goto _OVER;
×
261
    for (int32_t i = 0; i < tsSize; ++i) {
×
262
      SJson* tsJson = tjsonGetArrayItem(tsJsonArray, i);
×
263
      tjsonGetObjectValueBigInt(tsJson, &tmpI64);
×
264
      colDataSetInt64(pResTsCol, resCurRow, &tmpI64);
×
265
      resCurRow++;
×
266
    }
267
  }
268

269
  if (pResLowCol != NULL) {
×
270
    resCurRow = pBlock->info.rows;
×
271
    SJson* lowJsonArray = tjsonGetArrayItem(res, 2);
×
272
    if (lowJsonArray == NULL) goto _OVER;
×
273
    int32_t lowSize = tjsonGetArraySize(lowJsonArray);
×
274
    if (lowSize != rows) goto _OVER;
×
275
    for (int32_t i = 0; i < lowSize; ++i) {
×
276
      SJson* lowJson = tjsonGetArrayItem(lowJsonArray, i);
×
277
      tjsonGetObjectValueDouble(lowJson, &tmpDouble);
×
278
      tmpFloat = (float)tmpDouble;
×
279
      colDataSetFloat(pResLowCol, resCurRow, &tmpFloat);
×
280
      resCurRow++;
×
281
    }
282
  }
283

284
  if (pResHighCol != NULL) {
×
285
    resCurRow = pBlock->info.rows;
×
286
    SJson* highJsonArray = tjsonGetArrayItem(res, 3);
×
287
    if (highJsonArray == NULL) goto _OVER;
×
288
    int32_t highSize = tjsonGetArraySize(highJsonArray);
×
289
    if (highSize != rows) goto _OVER;
×
290
    for (int32_t i = 0; i < highSize; ++i) {
×
291
      SJson* highJson = tjsonGetArrayItem(highJsonArray, i);
×
292
      tjsonGetObjectValueDouble(highJson, &tmpDouble);
×
293
      tmpFloat = (float)tmpDouble;
×
294
      colDataSetFloat(pResHighCol, resCurRow, &tmpFloat);
×
295
      resCurRow++;
×
296
    }
297
  }
298

299
  resCurRow = pBlock->info.rows;
×
300
  SJson* valJsonArray = tjsonGetArrayItem(res, 1);
×
301
  if (valJsonArray == NULL) goto _OVER;
×
302
  int32_t valSize = tjsonGetArraySize(valJsonArray);
×
303
  if (valSize != rows) goto _OVER;
×
304
  for (int32_t i = 0; i < valSize; ++i) {
×
305
    SJson* valJson = tjsonGetArrayItem(valJsonArray, i);
×
306
    tjsonGetObjectValueDouble(valJson, &tmpDouble);
×
307

308
    switch (pSupp->inputValType) {
×
309
      case TSDB_DATA_TYPE_BOOL:
×
310
      case TSDB_DATA_TYPE_UTINYINT:
311
      case TSDB_DATA_TYPE_TINYINT: {
312
        tmpI8 = (int8_t)tmpDouble;
×
313
        colDataSetInt8(pResValCol, resCurRow, &tmpI8);
×
314
        break;
×
315
      }
316
      case TSDB_DATA_TYPE_USMALLINT:
×
317
      case TSDB_DATA_TYPE_SMALLINT: {
318
        tmpI16 = (int16_t)tmpDouble;
×
319
        colDataSetInt16(pResValCol, resCurRow, &tmpI16);
×
320
        break;
×
321
      }
322
      case TSDB_DATA_TYPE_INT:
×
323
      case TSDB_DATA_TYPE_UINT: {
324
        tmpI32 = (int32_t)tmpDouble;
×
325
        colDataSetInt32(pResValCol, resCurRow, &tmpI32);
×
326
        break;
×
327
      }
328
      case TSDB_DATA_TYPE_TIMESTAMP:
×
329
      case TSDB_DATA_TYPE_UBIGINT:
330
      case TSDB_DATA_TYPE_BIGINT: {
331
        tmpI64 = (int64_t)tmpDouble;
×
332
        colDataSetInt64(pResValCol, resCurRow, &tmpI64);
×
333
        break;
×
334
      }
335
      case TSDB_DATA_TYPE_FLOAT: {
×
336
        tmpFloat = (float)tmpDouble;
×
337
        colDataSetFloat(pResValCol, resCurRow, &tmpFloat);
×
338
        break;
×
339
      }
340
      case TSDB_DATA_TYPE_DOUBLE: {
×
341
        colDataSetDouble(pResValCol, resCurRow, &tmpDouble);
×
342
        break;
×
343
      }
344
      default:
×
345
        code = TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
346
        goto _OVER;
×
347
    }
348
    resCurRow++;
×
349
  }
350

351
  pBlock->info.rows += rows;
×
352

353
  if (pJson != NULL) tjsonDelete(pJson);
×
354
  return 0;
×
355

356
_OVER:
×
357
  tjsonDelete(pJson);
×
358
  if (code == 0) {
×
359
    code = TSDB_CODE_INVALID_JSON_FORMAT;
×
360
  }
361

362
  qError("%s failed to perform forecast finalize since %s", pId, tstrerror(code));
×
363
  return code;
×
364
}
365

366
static int32_t forecastAggregateBlocks(SForecastSupp* pSupp, SSDataBlock* pResBlock, const char* pId) {
×
367
  int32_t       code = TSDB_CODE_SUCCESS;
×
368
  int32_t       lino = 0;
×
369
  SAnalyticBuf* pBuf = &pSupp->analyBuf;
×
370

371
  code = forecastCloseBuf(pSupp, pId);
×
372
  QUERY_CHECK_CODE(code, lino, _end);
×
373

374
  code = forecastEnsureBlockCapacity(pResBlock, 1);
×
375
  QUERY_CHECK_CODE(code, lino, _end);
×
376

377
  code = forecastAnalysis(pSupp, pResBlock, pId);
×
378
  QUERY_CHECK_CODE(code, lino, _end);
×
379

380
  uInfo("%s block:%d, forecast finalize", pId, pSupp->numOfBlocks);
×
381

382
_end:
×
383
  pSupp->numOfBlocks = 0;
×
384
  taosAnalyBufDestroy(&pSupp->analyBuf);
×
385
  return code;
×
386
}
387

388
static int32_t forecastNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
×
389
  int32_t                code = TSDB_CODE_SUCCESS;
×
390
  int32_t                lino = 0;
×
391
  SExecTaskInfo*         pTaskInfo = pOperator->pTaskInfo;
×
392
  SForecastOperatorInfo* pInfo = pOperator->info;
×
393
  SSDataBlock*           pResBlock = pInfo->pRes;
×
394
  SForecastSupp*         pSupp = &pInfo->forecastSupp;
×
395
  SAnalyticBuf*          pBuf = &pSupp->analyBuf;
×
396
  int64_t                st = taosGetTimestampUs();
×
397
  int32_t                numOfBlocks = pSupp->numOfBlocks;
×
398
  const char*            pId = GET_TASKID(pOperator->pTaskInfo);
×
399

400
  blockDataCleanup(pResBlock);
×
401

402
  while (1) {
×
403
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
×
404
    if (pBlock == NULL) {
×
405
      break;
×
406
    }
407

408
    if (pSupp->groupId == 0 || pSupp->groupId == pBlock->info.id.groupId) {
×
409
      pSupp->groupId = pBlock->info.id.groupId;
×
410
      numOfBlocks++;
×
411
      pSupp->cachedRows += pBlock->info.rows;
×
412
      qDebug("%s group:%" PRId64 ", blocks:%d, rows:%" PRId64 ", total rows:%" PRId64, pId, pSupp->groupId, numOfBlocks,
×
413
             pBlock->info.rows, pSupp->cachedRows);
414
      code = forecastCacheBlock(pSupp, pBlock, pId);
×
415
      QUERY_CHECK_CODE(code, lino, _end);
×
416
    } else {
417
      qDebug("%s group:%" PRId64 ", read finish for new group coming, blocks:%d", pId, pSupp->groupId, numOfBlocks);
×
418
      code = forecastAggregateBlocks(pSupp, pResBlock, pId);
×
419
      QUERY_CHECK_CODE(code, lino, _end);
×
420
      pSupp->groupId = pBlock->info.id.groupId;
×
421
      numOfBlocks = 1;
×
422
      pSupp->cachedRows = pBlock->info.rows;
×
423
      qDebug("%s group:%" PRId64 ", new group, rows:%" PRId64 ", total rows:%" PRId64, pId, pSupp->groupId,
×
424
             pBlock->info.rows, pSupp->cachedRows);
425
      code = forecastCacheBlock(pSupp, pBlock, pId);
×
426
      QUERY_CHECK_CODE(code, lino, _end);
×
427
    }
428

429
    if (pResBlock->info.rows > 0) {
×
430
      (*ppRes) = pResBlock;
×
431
      qDebug("%s group:%" PRId64 ", return to upstream, blocks:%d", pId, pResBlock->info.id.groupId, numOfBlocks);
×
432
      return code;
×
433
    }
434
  }
435

436
  if (numOfBlocks > 0) {
×
437
    qDebug("%s group:%" PRId64 ", read finish, blocks:%d", pId, pSupp->groupId, numOfBlocks);
×
438
    code = forecastAggregateBlocks(pSupp, pResBlock, pId);
×
439
    QUERY_CHECK_CODE(code, lino, _end);
×
440
  }
441

442
  int64_t cost = taosGetTimestampUs() - st;
×
443
  qDebug("%s all groups finished, cost:%" PRId64 "us", pId, cost);
×
444

445
_end:
×
446
  if (code != TSDB_CODE_SUCCESS) {
×
447
    qError("%s %s failed at line %d since %s", pId, __func__, lino, tstrerror(code));
×
448
    pTaskInfo->code = code;
×
449
    T_LONG_JMP(pTaskInfo->env, code);
×
450
  }
451

452
  (*ppRes) = (pResBlock->info.rows == 0) ? NULL : pResBlock;
×
453
  return code;
×
454
}
455

456
static int32_t forecastParseOutput(SForecastSupp* pSupp, SExprSupp* pExprSup) {
×
457
  pSupp->resLowSlot = -1;
×
458
  pSupp->resHighSlot = -1;
×
459
  pSupp->resTsSlot = -1;
×
460
  pSupp->resValSlot = -1;
×
461

462
  for (int32_t j = 0; j < pExprSup->numOfExprs; ++j) {
×
463
    SExprInfo* pExprInfo = &pExprSup->pExprInfo[j];
×
464
    int32_t    dstSlot = pExprInfo->base.resSchema.slotId;
×
465
    if (pExprInfo->pExpr->_function.functionType == FUNCTION_TYPE_FORECAST) {
×
466
      pSupp->resValSlot = dstSlot;
×
467
    } else if (pExprInfo->pExpr->_function.functionType == FUNCTION_TYPE_FORECAST_ROWTS) {
×
468
      pSupp->resTsSlot = dstSlot;
×
469
    } else if (pExprInfo->pExpr->_function.functionType == FUNCTION_TYPE_FORECAST_LOW) {
×
470
      pSupp->resLowSlot = dstSlot;
×
471
    } else if (pExprInfo->pExpr->_function.functionType == FUNCTION_TYPE_FORECAST_HIGH) {
×
472
      pSupp->resHighSlot = dstSlot;
×
473
    } else {
474
    }
475
  }
476

477
  return 0;
×
478
}
479

480
static int32_t forecastParseInput(SForecastSupp* pSupp, SNodeList* pFuncs) {
×
481
  SNode* pNode = NULL;
×
482

483
  pSupp->inputTsSlot = -1;
×
484
  pSupp->inputValSlot = -1;
×
485
  pSupp->inputValType = -1;
×
486
  pSupp->inputPrecision = -1;
×
487

488
  FOREACH(pNode, pFuncs) {
×
489
    if ((nodeType(pNode) == QUERY_NODE_TARGET) && (nodeType(((STargetNode*)pNode)->pExpr) == QUERY_NODE_FUNCTION)) {
×
490
      SFunctionNode* pFunc = (SFunctionNode*)((STargetNode*)pNode)->pExpr;
×
491
      int32_t        numOfParam = LIST_LENGTH(pFunc->pParameterList);
×
492

493
      if (pFunc->funcType == FUNCTION_TYPE_FORECAST) {
×
494
        if (numOfParam == 3) {
×
495
          SNode* p1 = nodesListGetNode(pFunc->pParameterList, 0);
×
496
          SNode* p2 = nodesListGetNode(pFunc->pParameterList, 1);
×
497
          SNode* p3 = nodesListGetNode(pFunc->pParameterList, 2);
×
498
          if (p1 == NULL || p2 == NULL || p3 == NULL) return TSDB_CODE_PLAN_INTERNAL_ERROR;
×
499
          if (p1->type != QUERY_NODE_COLUMN) return TSDB_CODE_PLAN_INTERNAL_ERROR;
×
500
          if (p2->type != QUERY_NODE_VALUE) return TSDB_CODE_PLAN_INTERNAL_ERROR;
×
501
          if (p3->type != QUERY_NODE_COLUMN) return TSDB_CODE_PLAN_INTERNAL_ERROR;
×
502
          SColumnNode* pValNode = (SColumnNode*)p1;
×
503
          SValueNode*  pOptNode = (SValueNode*)p2;
×
504
          SColumnNode* pTsNode = (SColumnNode*)p3;
×
505
          pSupp->inputTsSlot = pTsNode->slotId;
×
506
          pSupp->inputPrecision = pTsNode->node.resType.precision;
×
507
          pSupp->inputValSlot = pValNode->slotId;
×
508
          pSupp->inputValType = pValNode->node.resType.type;
×
509
          tstrncpy(pSupp->algoOpt, pOptNode->literal, sizeof(pSupp->algoOpt));
×
510
        } else if (numOfParam == 2) {
×
511
          SNode* p1 = nodesListGetNode(pFunc->pParameterList, 0);
×
512
          SNode* p2 = nodesListGetNode(pFunc->pParameterList, 1);
×
513
          if (p1 == NULL || p2 == NULL) return TSDB_CODE_PLAN_INTERNAL_ERROR;
×
514
          if (p1->type != QUERY_NODE_COLUMN) return TSDB_CODE_PLAN_INTERNAL_ERROR;
×
515
          if (p2->type != QUERY_NODE_COLUMN) return TSDB_CODE_PLAN_INTERNAL_ERROR;
×
516
          SColumnNode* pValNode = (SColumnNode*)p1;
×
517
          SColumnNode* pTsNode = (SColumnNode*)p2;
×
518
          pSupp->inputTsSlot = pTsNode->slotId;
×
519
          pSupp->inputPrecision = pTsNode->node.resType.precision;
×
520
          pSupp->inputValSlot = pValNode->slotId;
×
521
          pSupp->inputValType = pValNode->node.resType.type;
×
522
          tstrncpy(pSupp->algoOpt, "algo=arima", TSDB_ANALYTIC_ALGO_OPTION_LEN);
×
523
        } else {
524
          return TSDB_CODE_PLAN_INTERNAL_ERROR;
×
525
        }
526
      }
527
    }
528
  }
529

530
  return 0;
×
531
}
532

533
static int32_t forecastParseAlgo(SForecastSupp* pSupp, const char* id) {
×
534
  pSupp->maxTs = 0;
×
535
  pSupp->minTs = INT64_MAX;
×
536
  pSupp->numOfRows = 0;
×
537

538
  if (!taosAnalyGetOptStr(pSupp->algoOpt, "algo", pSupp->algoName, sizeof(pSupp->algoName))) {
×
539
    qError("%s failed to get forecast algorithm name from %s", id, pSupp->algoOpt);
×
540
    return TSDB_CODE_ANA_ALGO_NOT_FOUND;
×
541
  }
542

543
  bool hasTimeout = taosAnalyGetOptInt(pSupp->algoOpt, "timeout", &pSupp->timeout);
×
544
  if (!hasTimeout) {
×
545
    qDebug("%s not set the timeout val, set default:%d", id, ANALY_DEFAULT_TIMEOUT);
×
546
    pSupp->timeout = ANALY_DEFAULT_TIMEOUT;
×
547
  } else {
548
    if (pSupp->timeout <= 0 || pSupp->timeout > ANALY_MAX_TIMEOUT) {
×
549
      qDebug("%s timeout val:%" PRId64 "s is invalid (greater than 10min or less than 1s), use default:%dms",
×
550
             id, pSupp->timeout, ANALY_DEFAULT_TIMEOUT);
551
      pSupp->timeout = ANALY_DEFAULT_TIMEOUT;
×
552
    } else {
553
      qDebug("%s timeout val is set to: %" PRId64 "s", id, pSupp->timeout);
×
554
    }
555
  }
556

557
  if (taosAnalyGetAlgoUrl(pSupp->algoName, ANALY_ALGO_TYPE_FORECAST, pSupp->algoUrl, sizeof(pSupp->algoUrl)) != 0) {
×
558
    qError("%s failed to get forecast algorithm url from %s", id, pSupp->algoName);
×
559
    return TSDB_CODE_ANA_ALGO_NOT_LOAD;
×
560
  }
561

562
  return 0;
×
563
}
564

565
static int32_t forecastCreateBuf(SForecastSupp* pSupp) {
×
566
  SAnalyticBuf* pBuf = &pSupp->analyBuf;
×
567
  int64_t       ts = 0;  // taosGetTimestampMs();
×
568

569
  pBuf->bufType = ANALYTICS_BUF_TYPE_JSON_COL;
×
570
  snprintf(pBuf->fileName, sizeof(pBuf->fileName), "%s/tdengine-forecast-%" PRId64, tsTempDir, ts);
×
571
  int32_t code = tsosAnalyBufOpen(pBuf, 2);
×
572
  if (code != 0) goto _OVER;
×
573

574
  code = taosAnalyBufWriteColMeta(pBuf, 0, TSDB_DATA_TYPE_TIMESTAMP, "ts");
×
575
  if (code != 0) goto _OVER;
×
576

577
  code = taosAnalyBufWriteColMeta(pBuf, 1, pSupp->inputValType, "val");
×
578
  if (code != 0) goto _OVER;
×
579

580
  code = taosAnalyBufWriteDataBegin(pBuf);
×
581
  if (code != 0) goto _OVER;
×
582

583
  for (int32_t i = 0; i < 2; ++i) {
×
584
    code = taosAnalyBufWriteColBegin(pBuf, i);
×
585
    if (code != 0) goto _OVER;
×
586
  }
587

588
_OVER:
×
589
  if (code != 0) {
×
590
    (void)taosAnalyBufClose(pBuf);
×
591
    taosAnalyBufDestroy(pBuf);
×
592
  }
593
  return code;
×
594
}
595

596
int32_t createForecastOperatorInfo(SOperatorInfo* downstream, SPhysiNode* pPhyNode, SExecTaskInfo* pTaskInfo,
×
597
                                   SOperatorInfo** pOptrInfo) {
598
  QRY_PARAM_CHECK(pOptrInfo);
×
599

600
  int32_t                code = 0;
×
601
  int32_t                lino = 0;
×
602
  SForecastOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SForecastOperatorInfo));
×
603
  SOperatorInfo*         pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
×
604
  if (pOperator == NULL || pInfo == NULL) {
×
605
    code = terrno;
×
606
    goto _error;
×
607
  }
608

609
  const char*             pId = pTaskInfo->id.str;
×
610
  SForecastSupp*          pSupp = &pInfo->forecastSupp;
×
611
  SForecastFuncPhysiNode* pForecastPhyNode = (SForecastFuncPhysiNode*)pPhyNode;
×
612
  SExprSupp*              pExprSup = &pOperator->exprSupp;
×
613
  int32_t                 numOfExprs = 0;
×
614
  SExprInfo*              pExprInfo = NULL;
×
615

616
  code = createExprInfo(pForecastPhyNode->pFuncs, NULL, &pExprInfo, &numOfExprs);
×
617
  QUERY_CHECK_CODE(code, lino, _error);
×
618

619
  code = initExprSupp(pExprSup, pExprInfo, numOfExprs, &pTaskInfo->storageAPI.functionStore);
×
620
  QUERY_CHECK_CODE(code, lino, _error);
×
621

622
  if (pForecastPhyNode->pExprs != NULL) {
×
623
    int32_t    num = 0;
×
624
    SExprInfo* pScalarExprInfo = NULL;
×
625
    code = createExprInfo(pForecastPhyNode->pExprs, NULL, &pScalarExprInfo, &num);
×
626
    QUERY_CHECK_CODE(code, lino, _error);
×
627

628
    code = initExprSupp(&pInfo->scalarSup, pScalarExprInfo, num, &pTaskInfo->storageAPI.functionStore);
×
629
    QUERY_CHECK_CODE(code, lino, _error);
×
630
  }
631

632
  code = filterInitFromNode((SNode*)pForecastPhyNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0);
×
633
  QUERY_CHECK_CODE(code, lino, _error);
×
634

635
  code = forecastParseInput(pSupp, pForecastPhyNode->pFuncs);
×
636
  QUERY_CHECK_CODE(code, lino, _error);
×
637

638
  code = forecastParseOutput(pSupp, pExprSup);
×
639
  QUERY_CHECK_CODE(code, lino, _error);
×
640

641
  code = forecastParseAlgo(pSupp, pId);
×
642
  QUERY_CHECK_CODE(code, lino, _error);
×
643

644
  code = forecastCreateBuf(pSupp);
×
645
  QUERY_CHECK_CODE(code, lino, _error);
×
646

647
  initResultSizeInfo(&pOperator->resultInfo, 4096);
×
648

649
  pInfo->pRes = createDataBlockFromDescNode(pPhyNode->pOutputDataBlockDesc);
×
650
  QUERY_CHECK_NULL(pInfo->pRes, code, lino, _error, terrno);
×
651

652
  setOperatorInfo(pOperator, "ForecastOperator", QUERY_NODE_PHYSICAL_PLAN_FORECAST_FUNC, false, OP_NOT_OPENED, pInfo,
×
653
                  pTaskInfo);
654
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, forecastNext, NULL, destroyForecastInfo, optrDefaultBufFn,
×
655
                                         NULL, optrDefaultGetNextExtFn, NULL);
656

657
  code = blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity);
×
658
  QUERY_CHECK_CODE(code, lino, _error);
×
659

660
  code = appendDownstream(pOperator, &downstream, 1);
×
661
  QUERY_CHECK_CODE(code, lino, _error);
×
662

663
  *pOptrInfo = pOperator;
×
664

665
  qDebug("%s forecast env is initialized, option:%s", pId, pSupp->algoOpt);
×
666
  return TSDB_CODE_SUCCESS;
×
667

668
_error:
×
669
  if (code != TSDB_CODE_SUCCESS) {
×
670
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
671
  }
672
  if (pInfo != NULL) destroyForecastInfo(pInfo);
×
673
  destroyOperatorAndDownstreams(pOperator, &downstream, 1);
×
674
  pTaskInfo->code = code;
×
675
  return code;
×
676
}
677

678
static void destroyForecastInfo(void* param) {
×
679
  SForecastOperatorInfo* pInfo = (SForecastOperatorInfo*)param;
×
680

681
  blockDataDestroy(pInfo->pRes);
×
682
  pInfo->pRes = NULL;
×
683
  cleanupExprSupp(&pInfo->scalarSup);
×
684
  taosAnalyBufDestroy(&pInfo->forecastSupp.analyBuf);
×
685
  taosMemoryFreeClear(param);
×
686
}
×
687

688
#else
689

690
int32_t createForecastOperatorInfo(SOperatorInfo* downstream, SPhysiNode* pPhyNode, SExecTaskInfo* pTaskInfo,
691
                                   SOperatorInfo** pOptrInfo) {
692
  return TSDB_CODE_OPS_NOT_SUPPORT;
693
}
694

695
#endif
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