Skip to content

fix(stream): handle const boundary in external window range#34669

Open
JinqingKuang wants to merge 1 commit intomainfrom
fix/stream-const-twstart-filter
Open

fix(stream): handle const boundary in external window range#34669
JinqingKuang wants to merge 1 commit intomainfrom
fix/stream-const-twstart-filter

Conversation

@JinqingKuang
Copy link
Contributor

Description

fix(stream): handle const boundary in external window range

Issue(s)

Checklist

Please check the items in the checklist if applicable.

  • Is the user manual updated?
  • Are the test cases passed and automated?
  • Is there no significant decrease in test coverage?

Copilot AI review requested due to automatic review settings March 4, 2026 03:06
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request aims to fix an issue in stream processing by correctly handling constant boundaries in external window ranges, specifically within sclCalcStreamExtWinsTimeRange. However, the implementation introduces a vulnerability to signed integer overflows when processing boundary values near INT64_MAX. These overflows can lead to incorrect window ranges, potentially causing logic bypasses or incorrect data processing in streams. It is recommended to use overflow-safe arithmetic or add explicit checks for boundary conditions. Additionally, there are opportunities to refactor parts of the new logic to reduce code duplication and improve maintainability.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a stream execution bug triggered by external window time-range calculation when a filter uses a constant timestamp boundary together with stream window placeholders (e.g., _twstart), and adds CI coverage to prevent regressions.

Changes:

  • Update external window range calculation to correctly handle constant (VALUE node) timestamp boundaries and broadcast single-row timestamp results across all external windows.
  • Add a new stream regression test covering ts >= <const> combined with ts < _twstart.
  • Register the new test in the CI task list.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
test/ci/cases.task Adds the new trigger-mode stream regression test to CI execution.
test/cases/18-StreamProcessing/03-TriggerMode/test_interval_const_filter_twstart.py New regression test reproducing the const-boundary + _twstart filter scenario and asserting stream tasks don’t fail.
source/libs/scalar/src/scalar.c Fixes external window time range calculation to support constant timestamp boundaries and align row counts with external window count.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@JinqingKuang JinqingKuang force-pushed the fix/stream-const-twstart-filter branch from e3672b3 to f800ce8 Compare March 4, 2026 06:26
Copilot AI review requested due to automatic review settings March 6, 2026 03:38
@JinqingKuang JinqingKuang force-pushed the fix/stream-const-twstart-filter branch from f800ce8 to a01a09f Compare March 6, 2026 03:38
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@JinqingKuang JinqingKuang force-pushed the fix/stream-const-twstart-filter branch from a01a09f to eaa9453 Compare March 6, 2026 06:29
@taosdata taosdata deleted a comment from gemini-code-assist bot Mar 6, 2026
@JinqingKuang
Copy link
Contributor Author

Code review

Found 3 issues:

  1. winOutIdx 语义的潜在混淆 - 条件从 pWin->tw.skey == pExtW->lastSKey 改为 pWin->winOutIdx >= 0,但 winOutIdx 有三种状态(-1: 起始边界已修改, -2: 结束边界已修改, >= 0: 已分配输出缓冲区)。新条件只检查是否已分配缓冲区,不能正确处理 winOutIdx == -1-2 的情况,可能导致空窗口处理逻辑被错误跳过。

if (NULL == pExtW->pEmptyInputBlock || (pWin && pWin->winOutIdx >= 0)) {
goto _exit;

  1. 数组越界风险 - 在 sclCalcStreamExtWinsTimeRange() 中,循环使用 res->numOfRows 访问 ctx->stream.pWins[i],但没有验证 pWins 数组大小是否至少为 res->numOfRowsres->numOfRows 来自表达式计算结果,与原始分配的数组大小没有保证相等,可能导致缓冲区溢出。

ctx->stream.pWins[i].winOutIdx = -1;
}
} else if (node->opType == OP_TYPE_GREATER_EQUAL) {
for (int32_t i = 0; i < winNum; ++i) {
int64_t tsVal = pTsValList[(tsValRows == 1) ? 0 : i];
ctx->stream.pWins[i].tw.skey = (-1 == ctx->stream.pWins[i].winOutIdx) ? TMAX(tsVal, ctx->stream.pWins[i].tw.skey) : tsVal;
ctx->stream.pWins[i].winOutIdx = -1;
}
} else {
qError("invalid op type:%d in ext win range start expr", node->opType);
return TSDB_CODE_STREAM_INTERNAL_ERROR;
}
}
if (2 == ctx->stream.extWinType) {
//if (ctx->stream.pStreamRuntimeFuncInfo->triggerType != STREAM_TRIGGER_SLIDING) {
// consider triggerType and keep the ekey exclude
if (node->opType == OP_TYPE_LOWER_THAN) {
for (int32_t i = 0; i < winNum; ++i) {
int64_t tsVal = pTsValList[(tsValRows == 1) ? 0 : i];
ctx->stream.pWins[i].tw.ekey = (-2 == ctx->stream.pWins[i].winOutIdx) ? TMIN(tsVal, ctx->stream.pWins[i].tw.ekey) : tsVal;
ctx->stream.pWins[i].winOutIdx = -2;
}
} else if (node->opType == OP_TYPE_LOWER_EQUAL) {
for (int32_t i = 0; i < winNum; ++i) {
int64_t tsVal = pTsValList[(tsValRows == 1) ? 0 : i];
ctx->stream.pWins[i].tw.ekey = (-2 == ctx->stream.pWins[i].winOutIdx) ? TMIN(tsVal + 1, ctx->stream.pWins[i].tw.ekey) : (tsVal + 1);
ctx->stream.pWins[i].winOutIdx = -2;
}
} else {
qError("invalid op type:%d in ext win range end expr", node->opType);

  1. 空窗口处理逻辑的条件变更缺少说明 - 在 extWinAggHandleEmptyWins 中,条件从基于时间的 lastSKey 比较改为基于 winOutIdx 的检查,这是重大的语义变更。原逻辑用于避免重复处理相同起始时间的窗口,新逻辑检查窗口输出索引状态。如果 winOutIdx 在某些场景下未正确更新,可能导致空窗口处理逻辑被错误跳过或执行。

if (NULL == pExtW->pEmptyInputBlock || (pWin && pWin->winOutIdx >= 0)) {
goto _exit;

🤖 Generated with Claude Code

- If this code review was useful, please react with 👍. Otherwise, react with 👎.

Copilot AI review requested due to automatic review settings March 6, 2026 08:15
@JinqingKuang JinqingKuang force-pushed the fix/stream-const-twstart-filter branch from eaa9453 to aa8ef32 Compare March 6, 2026 08:15
@JinqingKuang
Copy link
Contributor Author

  1. 问题 1、3 已修复;
  2. 问题 2 不是问题,前面有对行数的校验,校验不通过会报错并提前返回。

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants