Skip to content

fix: state window handles partititon by error#33782

Merged
guanshengliang merged 2 commits intomainfrom
fix/main/stateWinPartBy
Dec 5, 2025
Merged

fix: state window handles partititon by error#33782
guanshengliang merged 2 commits intomainfrom
fix/main/stateWinPartBy

Conversation

@Tony2h
Copy link
Member

@Tony2h Tony2h commented Dec 2, 2025

Description

Please briefly describe the code changes in this pull request.

Jira: https://jira.taosdata.com:18080/browse/TD-

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?

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Tony2h, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses and fixes an error in how state window functions handle data partitioning, particularly concerning the accurate definition of window boundaries and the processing of null values across different data blocks and groups. The changes ensure that state windows are correctly formed and aggregated, even when data contains nulls or spans multiple partitions, leading to more reliable query results.

Highlights

  • Refined State Window Boundary Calculations: Adjusted the initialization of prevTs to INT64_MIN and refined win.skey and win.ekey assignments for more accurate window definition, especially when handling null values and transitions between data blocks.
  • Improved Null Value Handling: Enhanced the processing of null rows by storing their timestamps and preventing the processing of empty state windows, ensuring more robust aggregation results.
  • Enhanced Group Change Management: Implemented explicit logic to handle transitions between data groups within the aggregation process, ensuring proper closure of previous windows and correct initialization for new groups.
  • Added New Test Cases: Introduced a dedicated test check_partition_and_null to thoroughly validate state window behavior with partitioned data and null values, improving test coverage.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

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 addresses a bug in how state_window handles partition by clauses. The changes correctly manage group boundaries within data blocks by processing the unclosed window of the previous group before starting a new one. Key fixes include updating the group ID of merged blocks, correctly initializing timestamps, and adjusting window start/end key calculations. The addition of a new test case for this scenario is a great improvement. I've found one potential bug related to a type mismatch and have a suggestion to improve code readability.

pTaskInfo->code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
T_LONG_JMP(pTaskInfo->env, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
}
int64_t gid = pBlock->info.id.groupId;
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The variable gid is declared as int64_t, but it is assigned from pBlock->info.id.groupId, which is of type uint64_t. This can lead to incorrect behavior for large group IDs due to potential overflow or incorrect sign interpretation. It should be declared as uint64_t to match the type of pBlock->info.id.groupId and pRowSup->groupId.

  uint64_t gid = pBlock->info.id.groupId;

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

Comment on lines +706 to +707
pRowSup->startRowIndex = pRowSup->groupId = 0;
pRowSup->numOfRows = pRowSup->numNullRows = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For better readability and to avoid potential subtle issues, consider splitting these chained assignments into separate lines.

pRowSup->startRowIndex = 0;
pRowSup->groupId = 0;
pRowSup->numOfRows = 0;
pRowSup->numNullRows = 0;

Copilot AI review requested due to automatic review settings December 3, 2025 16:43
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 an error in state window handling when used with PARTITION BY clauses, specifically addressing issues where partition boundaries and null values in data blocks were not being handled correctly.

Key Changes

  • Added group change detection and handling logic to properly close state windows when partition groups change
  • Fixed null row tracking to use timestamps instead of row indices for correct end window calculation
  • Enhanced window initialization logic to correctly determine start keys when handling null rows across partition boundaries

Reviewed changes

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

Show a summary per file
File Description
test/cases/13-TimeSeriesExt/04-StateWindow/test_state_window_null_block.py Added comprehensive test case check_partition_and_null() to verify state window behavior with partition by and null values
test/cases/05-VirtualTables/in/test_vstable_select_test_partition.in Updated test query from subquery approach to direct partition by with state_window, adding partition column to output
test/cases/05-VirtualTables/ans/test_vstable_select_test_partition.ans Updated expected results to match new query format with partition column included
test/cases/05-VirtualTables/ans/test_vctable_select_test_partition.ans Updated expected results for child table variant to match new query format
source/libs/executor/src/timewindowoperator.c Core fix: Added group change detection, fixed null row timestamp tracking, improved window boundary calculations, and added empty window guard
source/libs/executor/inc/executorInt.h Updated prevTs initialization to INT64_MIN and improved reset logic for window state tracking

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

@guanshengliang guanshengliang merged commit 24af00c into main Dec 5, 2025
19 of 20 checks passed
@minhuinie minhuinie deleted the fix/main/stateWinPartBy branch February 4, 2026 02:51
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.

4 participants