• 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

81.7
/source/libs/executor/src/querytask.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 "filter.h"
17
#include "function.h"
18
#include "functionMgt.h"
19
#include "os.h"
20
#include "querynodes.h"
21
#include "tfill.h"
22
#include "tname.h"
23

24
#include "tdatablock.h"
25
#include "tmsg.h"
26

27
#include "executorInt.h"
28
#include "index.h"
29
#include "operator.h"
30
#include "query.h"
31
#include "querytask.h"
32
#include "storageapi.h"
33
#include "thash.h"
34
#include "ttypes.h"
35
#include "tref.h"
36

37
#define CLEAR_QUERY_STATUS(q, st) ((q)->status &= (~(st)))
38

39
int32_t doCreateTask(uint64_t queryId, uint64_t taskId, int32_t vgId, EOPTR_EXEC_MODEL model, SStorageAPI* pAPI,
349,650,491✔
40
                     SExecTaskInfo** pTaskInfo) {
41
  if (pTaskInfo == NULL) {
349,650,491✔
42
    return TSDB_CODE_SUCCESS;
×
43
  }
44

45
  SExecTaskInfo* p = taosMemoryCalloc(1, sizeof(SExecTaskInfo));
349,650,491✔
46
  if (p == NULL) {
349,628,356✔
47
    return terrno;
×
48
  }
49

50
  setTaskStatus(p, TASK_NOT_COMPLETED);
349,628,356✔
51
  p->cost.created = taosGetTimestampUs();
349,664,743✔
52

53
  p->execModel = model;
349,651,970✔
54
  p->stopInfo.pStopInfo = taosArrayInit(4, sizeof(SExchangeOpStopInfo));
349,659,025✔
55
  p->pResultBlockList = taosArrayInit(128, POINTER_BYTES);
349,636,154✔
56
  if (p->stopInfo.pStopInfo == NULL || p->pResultBlockList == NULL) {
349,635,948✔
57
    doDestroyTask(p);
18✔
58
    return terrno;
×
59
  }
60

61
  p->storageAPI = *pAPI;
349,649,743✔
62
  taosInitRWLatch(&p->lock);
349,657,567✔
63

64
  p->id.vgId = vgId;
349,642,042✔
65
  p->id.queryId = queryId;
349,638,598✔
66
  p->id.taskId = taskId;
349,652,851✔
67
  p->id.str = taosMemoryMalloc(64);
349,654,447✔
68
  if (p->id.str == NULL) {
349,611,607✔
69
    doDestroyTask(p);
×
70
    return terrno;
×
71
  }
72

73
  buildTaskId(taskId, queryId, p->id.str, 64);
349,637,739✔
74
  p->schemaInfos = taosArrayInit(1, sizeof(SSchemaInfo));
349,617,179✔
75
  if (p->id.str == NULL || p->schemaInfos == NULL) {
349,602,573✔
76
    doDestroyTask(p);
443✔
77
    return terrno;
×
78
  }
79

80
  *pTaskInfo = p;
349,621,817✔
81
  return TSDB_CODE_SUCCESS;
349,626,092✔
82
}
83

84
int32_t getTaskCode(void* pTaskInfo) { return ((SExecTaskInfo*)pTaskInfo)->code; }
8,656✔
85

86
bool isTaskKilled(void* pTaskInfo) { return (0 != ((SExecTaskInfo*)pTaskInfo)->code); }
1,820,873,130✔
87

88
void setTaskKilled(SExecTaskInfo* pTaskInfo, int32_t rspCode) {
99,095✔
89
  pTaskInfo->code = rspCode;
99,095✔
90
  (void)stopTableScanOperator(pTaskInfo->pRoot, pTaskInfo->id.str, &pTaskInfo->storageAPI);
99,095✔
91
}
99,095✔
92

93
void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) {
1,102,575,304✔
94
  if (status == TASK_NOT_COMPLETED) {
1,102,575,304✔
95
    pTaskInfo->status = status;
349,812,176✔
96
  } else {
97
    // QUERY_NOT_COMPLETED is not compatible with any other status, so clear its position first
98
    CLEAR_QUERY_STATUS(pTaskInfo, TASK_NOT_COMPLETED);
752,763,128✔
99
    pTaskInfo->status |= status;
752,787,601✔
100
  }
101
}
1,102,622,352✔
102

103

104
int32_t initTaskSubJobCtx(SExecTaskInfo* pTaskInfo, SArray** subEndPoints, SReadHandle* readHandle) {
38,784,488✔
105
  int32_t code = 0, lino = 0;
38,784,488✔
106
  int32_t subJobNum = taosArrayGetSize(*subEndPoints);
38,784,488✔
107
  
108
  pTaskInfo->pSubJobCtx = taosMemoryCalloc(1, sizeof(*pTaskInfo->pSubJobCtx));
38,785,581✔
109
  TSDB_CHECK_NULL(pTaskInfo->pSubJobCtx, code, lino, _exit, terrno);
38,784,521✔
110
  
111
  STaskSubJobCtx* ctx = pTaskInfo->pSubJobCtx;
38,784,499✔
112

113
  ctx->queryId = pTaskInfo->id.queryId;
38,785,570✔
114
  ctx->taskId = pTaskInfo->id.taskId;
38,786,111✔
115
  ctx->idStr = pTaskInfo->id.str;
38,784,499✔
116
  ctx->pTaskInfo = pTaskInfo;
38,784,499✔
117
  ctx->subEndPoints = *subEndPoints;
38,783,417✔
118
  ctx->rpcHandle = (readHandle && readHandle->pMsgCb) ? readHandle->pMsgCb->clientRpc : NULL;
38,783,947✔
119

120
  *subEndPoints = NULL;
38,783,958✔
121
  
122
  ctx->subResNodes = taosArrayInit_s(POINTER_BYTES, subJobNum);
38,783,417✔
123
  if (NULL == ctx->subResNodes) {
38,785,040✔
124
    qError("%s taosArrayInit_s %d subResNodes failed, error:%s", GET_TASKID(pTaskInfo), subJobNum, tstrerror(terrno));
×
125
    TSDB_CHECK_NULL(ctx->subResNodes, code, lino, _exit, terrno);
×
126
  }
127
  
128
  TAOS_CHECK_EXIT(tsem_init(&ctx->ready, 0, 0));
38,784,488✔
129

130
  int64_t refId = taosAddRef(fetchObjRefPool, ctx);
38,782,346✔
131
  if (refId < 0) {
38,785,570✔
132
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
133
    TAOS_CHECK_EXIT(terrno);
×
134
  }
135
  
136
  ctx->subJobRefId = refId;
38,785,570✔
137

138
  qDebug("%s subJobCtx %" PRIu64 " with %d endPoints inited", pTaskInfo->id.str, (uint64_t)refId, (int32_t)taosArrayGetSize(ctx->subEndPoints));
38,784,488✔
139

140
_exit:
38,571,670✔
141

142
  if (code) {
38,783,947✔
143
    destroySubJobCtx(pTaskInfo->pSubJobCtx);
×
144
    pTaskInfo->pSubJobCtx = NULL;
×
145
    
146
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
147
  }
148

149
  return code;
38,782,887✔
150
}
151

152

153

154
int32_t createExecTaskInfo(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId,
349,524,374✔
155
                           int32_t vgId, char* sql, EOPTR_EXEC_MODEL model, SArray** subEndPoints) {
156
  int32_t code = doCreateTask(pPlan->id.queryId, taskId, vgId, model, &pHandle->api, pTaskInfo);
349,524,374✔
157
  if (*pTaskInfo == NULL || code != 0) {
349,490,048✔
158
    nodesDestroyNode((SNode*)pPlan);
78✔
159
    return code;
×
160
  }
161

162
  (*pTaskInfo)->pSubplan = pPlan;
349,494,348✔
163

164
  if (NULL != sql) {
349,508,746✔
165
    (*pTaskInfo)->sql = taosStrdup(sql);
325,889,598✔
166
    if (NULL == (*pTaskInfo)->sql) {
325,895,877✔
167
      code = terrno;
×
168
      doDestroyTask(*pTaskInfo);
×
169
      (*pTaskInfo) = NULL;
×
170
      return code;
×
171
    }
172
  }
173

174
  (*pTaskInfo)->pWorkerCb = pHandle->pWorkerCb;
349,499,627✔
175
  (*pTaskInfo)->pStreamRuntimeInfo = pHandle->streamRtInfo;
349,507,373✔
176

177
  if (subEndPoints && taosArrayGetSize(*subEndPoints) > 0) {
349,490,777✔
178
    code = initTaskSubJobCtx(*pTaskInfo, subEndPoints, pHandle);
38,785,581✔
179
    if (code != TSDB_CODE_SUCCESS) {
38,783,428✔
180
      doDestroyTask(*pTaskInfo);
×
181
      (*pTaskInfo) = NULL;
×
182
      return code;
×
183
    }
184
  }
185
  
186
  setTaskScalarExtraInfo(*pTaskInfo);
349,461,451✔
187
  
188
  code = createOperator(pPlan->pNode, *pTaskInfo, pHandle, pPlan->pTagCond, pPlan->pTagIndexCond, pPlan->user,
349,504,042✔
189
                        pPlan->dbFName, &((*pTaskInfo)->pRoot), model);
349,486,443✔
190

191
  if (NULL == (*pTaskInfo)->pRoot || code != 0) {
349,353,515✔
192
    doDestroyTask(*pTaskInfo);
454,575✔
193
    (*pTaskInfo) = NULL;
462,744✔
194
  }
195
  return code;
349,383,732✔
196
}
197

198
void cleanupQueriedTableScanInfo(void* p) {
265,498,361✔
199
  SSchemaInfo* pSchemaInfo = p;
265,498,361✔
200

201
  taosMemoryFreeClear(pSchemaInfo->dbname);
265,498,361✔
202
  taosMemoryFreeClear(pSchemaInfo->tablename);
265,466,016✔
203
  tDeleteSchemaWrapper(pSchemaInfo->sw);
265,416,439✔
204
  tDeleteSchemaWrapper(pSchemaInfo->qsw);
265,429,593✔
205
}
265,430,230✔
206

207
int32_t initQueriedTableSchemaInfo(SReadHandle* pHandle, SScanPhysiNode* pScanNode, const char* dbName,
265,526,711✔
208
                                   SExecTaskInfo* pTaskInfo) {
209
  SMetaReader mr = {0};
265,526,711✔
210
  if (pHandle == NULL) {
265,531,162✔
211
    return TSDB_CODE_INVALID_PARA;
×
212
  }
213

214
  SStorageAPI* pAPI = &pTaskInfo->storageAPI;
265,531,162✔
215

216
  pAPI->metaReaderFn.initReader(&mr, pHandle->vnode, META_READER_LOCK, &pAPI->metaFn);
265,528,389✔
217
  int32_t code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, pScanNode->uid);
265,532,831✔
218
  if (code != TSDB_CODE_SUCCESS) {
265,401,370✔
UNCOV
219
    qError("failed to get the table meta, uid:0x%" PRIx64 ", suid:0x%" PRIx64 ", %s", pScanNode->uid, pScanNode->suid,
×
220
           GET_TASKID(pTaskInfo));
221

UNCOV
222
    pAPI->metaReaderFn.clearReader(&mr);
×
UNCOV
223
    return code;
×
224
  }
225

226
  SSchemaInfo schemaInfo = {0};
265,401,370✔
227

228
  schemaInfo.tablename = taosStrdup(mr.me.name);
265,400,295✔
229
  schemaInfo.dbname = taosStrdup(dbName);
265,365,799✔
230
  if (schemaInfo.tablename == NULL || schemaInfo.dbname == NULL) {
265,417,646✔
231
    pAPI->metaReaderFn.clearReader(&mr);
183,445✔
232
    cleanupQueriedTableScanInfo(&schemaInfo);
×
233
    return terrno;
×
234
  }
235

236
  if (mr.me.type == TSDB_VIRTUAL_NORMAL_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
265,234,201✔
237
    schemaInfo.rversion = mr.me.colRef.version;
286,858✔
238
  }
239

240
  if (mr.me.type == TSDB_SUPER_TABLE) {
265,234,201✔
241
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
120,038,659✔
242
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
120,038,659✔
243
  } else if (mr.me.type == TSDB_CHILD_TABLE || mr.me.type == TSDB_VIRTUAL_CHILD_TABLE) {
145,260,202✔
244
    tDecoderClear(&mr.coder);
101,844,352✔
245

246
    tb_uid_t suid = mr.me.ctbEntry.suid;
101,937,410✔
247
    code = pAPI->metaReaderFn.getEntryGetUidCache(&mr, suid);
101,937,410✔
248
    if (code != TSDB_CODE_SUCCESS) {
101,934,806✔
249
      pAPI->metaReaderFn.clearReader(&mr);
×
250
      cleanupQueriedTableScanInfo(&schemaInfo);
×
251
      return code;
×
252
    }
253

254
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.stbEntry.schemaRow);
101,936,316✔
255
    schemaInfo.tversion = mr.me.stbEntry.schemaTag.version;
101,936,316✔
256
  } else {
257
    schemaInfo.sw = tCloneSSchemaWrapper(&mr.me.ntbEntry.schemaRow);
43,427,957✔
258
  }
259

260
  pAPI->metaReaderFn.clearReader(&mr);
265,402,932✔
261

262
  if (schemaInfo.sw == NULL) {
265,306,503✔
263
    cleanupQueriedTableScanInfo(&schemaInfo);
×
264
    return terrno;
×
265
  }
266

267
  schemaInfo.qsw = extractQueriedColumnSchema(pScanNode);
265,306,503✔
268
  if (schemaInfo.qsw == NULL) {
265,414,168✔
269
    cleanupQueriedTableScanInfo(&schemaInfo);
×
270
    return terrno;
×
271
  }
272

273
  void* p = taosArrayPush(pTaskInfo->schemaInfos, &schemaInfo);
265,414,168✔
274
  if (p == NULL) {
265,458,211✔
275
    cleanupQueriedTableScanInfo(&schemaInfo);
×
276
    return terrno;
×
277
  }
278

279
  return code;
265,458,211✔
280
}
281

282
SSchemaWrapper* extractQueriedColumnSchema(SScanPhysiNode* pScanNode) {
265,367,228✔
283
  int32_t numOfCols = LIST_LENGTH(pScanNode->pScanCols);
265,367,228✔
284
  int32_t numOfTags = LIST_LENGTH(pScanNode->pScanPseudoCols);
265,451,916✔
285

286
  SSchemaWrapper* pqSw = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
265,410,945✔
287
  if (pqSw == NULL) {
265,338,886✔
288
    return NULL;
×
289
  }
290

291
  pqSw->pSchema = taosMemoryCalloc(numOfCols + numOfTags, sizeof(SSchema));
265,338,886✔
292
  if (pqSw->pSchema == NULL) {
265,202,517✔
293
    taosMemoryFree(pqSw);
×
294
    return NULL;
×
295
  }
296

297
  for (int32_t i = 0; i < numOfCols; ++i) {
1,187,536,242✔
298
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanCols, i);
922,073,069✔
299
    SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
922,148,582✔
300

301
    SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
922,150,510✔
302
    pSchema->colId = pColNode->colId;
922,186,832✔
303
    pSchema->type = pColNode->node.resType.type;
922,244,792✔
304
    pSchema->bytes = pColNode->node.resType.bytes;
922,240,490✔
305
    tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
922,189,905✔
306
  }
307

308
  // this the tags and pseudo function columns, we only keep the tag columns
309
  for (int32_t i = 0; i < numOfTags; ++i) {
456,835,872✔
310
    STargetNode* pNode = (STargetNode*)nodesListGetNode(pScanNode->pScanPseudoCols, i);
191,387,679✔
311

312
    int32_t type = nodeType(pNode->pExpr);
191,446,915✔
313
    if (type == QUERY_NODE_COLUMN) {
191,456,619✔
314
      SColumnNode* pColNode = (SColumnNode*)pNode->pExpr;
148,890,367✔
315

316
      SSchema* pSchema = &pqSw->pSchema[pqSw->nCols++];
148,882,746✔
317
      pSchema->colId = pColNode->colId;
148,832,311✔
318
      pSchema->type = pColNode->node.resType.type;
148,900,448✔
319
      pSchema->bytes = pColNode->node.resType.bytes;
148,844,232✔
320
      tstrncpy(pSchema->name, pColNode->colName, tListLen(pSchema->name));
148,874,689✔
321
    }
322
  }
323

324
  return pqSw;
265,448,193✔
325
}
326

327
static void cleanupTmqInfo(STmqTaskInfo* pTmqInfo) {
349,580,459✔
328
  tDeleteSchemaWrapper(pTmqInfo->schema);
349,580,459✔
329
  tOffsetDestroy(&pTmqInfo->currentOffset);
349,614,211✔
330
}
349,520,773✔
331

332
static void freeBlock(void* pParam) {
641,409,220✔
333
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
641,409,220✔
334
  blockDataDestroy(pBlock);
641,415,984✔
335
}
641,413,710✔
336

337

338
void destroySubJobCtx(STaskSubJobCtx* pCtx) {
38,785,029✔
339
  if (pCtx->transporterId > 0) {
38,785,029✔
340
    int32_t ret = asyncFreeConnById(pCtx->rpcHandle, pCtx->transporterId);
3,246✔
341
    if (ret != 0) {
3,246✔
342
      qDebug("%s failed to free subQ rpc handle, code:%s", pCtx->idStr, tstrerror(ret));
×
343
    }
344
    pCtx->transporterId = -1;
3,246✔
345
  }
346

347
  if (pCtx->subEndPoints != NULL) {
38,785,570✔
348
    size_t size = taosArrayGetSize(pCtx->subEndPoints);
38,785,040✔
349
    if (size > 0) {
38,783,428✔
350
      int32_t code = tsem_destroy(&pCtx->ready);
38,784,510✔
351
      if (code != TSDB_CODE_SUCCESS) {
38,785,029✔
352
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
353
      }
354
      taosArrayDestroy(pCtx->subResNodes);
38,785,029✔
355
    }
356
    taosArrayDestroyP(pCtx->subEndPoints, NULL);
38,782,346✔
357
    pCtx->subEndPoints = NULL;
38,781,805✔
358
  }
359
  
360
  taosMemoryFreeClear(pCtx);  
38,782,346✔
361
}
38,780,193✔
362

363
void doDestroyTask(SExecTaskInfo* pTaskInfo) {
349,643,361✔
364
  qDebug("%s execTask is freed", GET_TASKID(pTaskInfo));
349,643,361✔
365
  destroyOperator(pTaskInfo->pRoot);
349,643,361✔
366
  pTaskInfo->pRoot = NULL;
349,571,175✔
367

368
  if (pTaskInfo->pSubJobCtx) {
349,590,869✔
369
    int32_t  code = taosRemoveRef(fetchObjRefPool, pTaskInfo->pSubJobCtx->subJobRefId);
38,785,581✔
370
    if (code != TSDB_CODE_SUCCESS) {
38,785,570✔
371
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
372
    }
373
  }
374

375
  taosArrayDestroyEx(pTaskInfo->schemaInfos, cleanupQueriedTableScanInfo);
349,596,527✔
376
  cleanupTmqInfo(&pTaskInfo->tmqInfo);
349,549,265✔
377

378
  if (!pTaskInfo->localFetch.localExec) {
349,513,064✔
379
    nodesDestroyNode((SNode*)pTaskInfo->pSubplan);
349,592,352✔
380
    pTaskInfo->pSubplan = NULL;
349,585,803✔
381
  }
382

383
  taosArrayDestroyEx(pTaskInfo->pResultBlockList, freeBlock);
349,524,199✔
384
  taosArrayDestroy(pTaskInfo->stopInfo.pStopInfo);
349,572,244✔
385
  if (!pTaskInfo->paramSet) {
349,589,626✔
386
    freeOperatorParam(pTaskInfo->pOpParam, OP_GET_PARAM);
342,174,223✔
387
    pTaskInfo->pOpParam = NULL;
342,135,499✔
388
  }
389
  taosMemoryFreeClear(pTaskInfo->sql);
349,570,521✔
390
  taosMemoryFreeClear(pTaskInfo->id.str);
349,617,491✔
391
  taosMemoryFreeClear(pTaskInfo);
349,570,714✔
392
}
349,546,813✔
393

394
void buildTaskId(uint64_t taskId, uint64_t queryId, char* dst, int32_t len) {
379,552,103✔
395
  int32_t ret = snprintf(dst, len, "TID:0x%" PRIx64 " QID:0x%" PRIx64, taskId, queryId);
379,552,103✔
396
  if (ret < 0) {
379,552,103✔
397
    qError("TID:0x%"PRIx64" QID:0x%"PRIx64" create task id failed,  ignore and continue", taskId, queryId);
×
398
  }
399
}
379,552,103✔
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