Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: optimizer rule for windowed sort #4874

Merged
merged 37 commits into from
Oct 29, 2024

Conversation

waynexia
Copy link
Member

@waynexia waynexia commented Oct 24, 2024

I hereby agree to the terms of the GreptimeDB CLA.

Refer to a related PR or issue link (optional)

What's changed and what's your intention?

Adds a new optimizer rule to handle windowed sorts.

Before:

explain analyze select * from xxx order by ts limit 1;

|     0 |    0 |  MergeScanExec: peers=[4398046511104(1024, 0), ] metrics=[output_rows: 1, greptime_exec_read_cost: 0, ready_time: 10060933, first_consume_time: 27928469508, finish_time: 27935945806, ]
                                                                                                                                                                                                                                                                                                                                                                                                                     |
|     1 |    0 |  GlobalLimitExec: skip=0, fetch=1 metrics=[output_rows: 1, elapsed_compute: 35178, ]
  SortPreservingMergeExec: [timestamp@9 ASC NULLS LAST] metrics=[output_rows: 16, elapsed_compute: 26392, ]
    SortExec: TopK(fetch=1), expr=[timestamp@9 ASC NULLS LAST], preserve_partitioning=[true] metrics=[output_rows: 16, elapsed_compute: 18577898198, row_replacements: 98, ]
      UnorderedScan: region=4398046511104(1024, 0), partition_count=0 (0 memtable ranges, 0 file 0 ranges) metrics=[output_rows: 806097407, mem_used: 211645809179, elapsed_poll: 358804887399, elapsed_await: 423133641911, ]
 |
|  NULL | NULL | Total rows: 1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |

3 rows in set (27.99 sec)

After

|     0 |    0 |  MergeScanExec: peers=[4398046511104(1024, 0), ] metrics=[output_rows: 1, greptime_exec_read_cost: 0, first_consume_time: 3913059830, ready_time: 35493492, finish_time: 3913469912, ]
                                                                                                                                                                                                                                                                                                                                                                                                                             |
|     1 |    0 |  GlobalLimitExec: skip=0, fetch=1 metrics=[output_rows: 1, elapsed_compute: 17285963, ]
  SortPreservingMergeExec: [timestamp@9 ASC NULLS LAST] metrics=[output_rows: 16, elapsed_compute: 69861, ]
    WindowedSortExec metrics=[output_rows: 16, elapsed_compute: 16, ]
      PartSortExec timestamp@9 ASC NULLS LAST metrics=[output_rows: 46274923, elapsed_compute: 16, ]
        UnorderedScan: region=4398046511104(1024, 0), partition_count=7883 (1 memtable ranges, 24 file 7882 ranges) metrics=[output_rows: 46274923, mem_used: 12144270581, elapsed_poll: 25761366240, elapsed_await: 30407342778, ]
 |
|  NULL | NULL | Total rows: 1                                                                                                                                                           

3 rows in set (3.93 sec)

Checklist

  • I have written the necessary rustdoc comments.
  • I have added the necessary unit tests and integration tests.
  • This PR requires documentation updates.

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Copy link
Contributor

coderabbitai bot commented Oct 24, 2024

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added the docs-not-required This change does not impact docs. label Oct 24, 2024
discord9 and others added 11 commits October 24, 2024 15:09
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
@waynexia waynexia marked this pull request as ready for review October 24, 2024 13:24
@waynexia waynexia requested review from v0y4g3r, evenyag and a team as code owners October 24, 2024 13:24
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Copy link

codecov bot commented Oct 25, 2024

Codecov Report

Attention: Patch coverage is 82.19424% with 99 lines in your changes missing coverage. Please review.

Project coverage is 83.95%. Comparing base (0ee455a) to head (2e2ae98).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4874      +/-   ##
==========================================
- Coverage   84.17%   83.95%   -0.22%     
==========================================
  Files        1136     1137       +1     
  Lines      208600   208997     +397     
==========================================
- Hits       175593   175470     -123     
- Misses      33007    33527     +520     

Copy link
Contributor

@evenyag evenyag left a comment

Choose a reason for hiding this comment

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

Mostly LGTM

src/mito2/src/engine.rs Show resolved Hide resolved
src/query/src/optimizer/windowed_sort.rs Show resolved Hide resolved
src/query/src/part_sort.rs Outdated Show resolved Hide resolved
Comment on lines +144 to +146
let partition_ranges = scanner.properties().partitions.clone();
// set distinguish_partition_range won't fail
let _ = scanner.prepare(partition_ranges, distinguish_partition_range);
Copy link
Contributor

@evenyag evenyag Oct 25, 2024

Choose a reason for hiding this comment

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

non-blocking: We may refactor this to allow only modifying distinguish_partition_range so we don't have to clone the partition ranges.

Cargo.toml Outdated Show resolved Hide resolved
Copy link
Contributor

@discord9 discord9 left a comment

Choose a reason for hiding this comment

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

others LGTM

@evenyag
Copy link
Contributor

evenyag commented Oct 28, 2024

The cause of the failure of fuzz_insert_logical_table is that part sort always return the result of sort_buffer():

// mark end of current PartitionRange
return Poll::Ready(self.sort_buffer().transpose());

If there is an empty PartitionRange, the part sort stream will complete immediately and won't poll the remaining PartRanges. 7baf4ad fixed this.

TODO: Maybe we should add a test case to part_sort.rs

@discord9
Copy link
Contributor

tests added in part_sort.rs and window_sort.rs, also update window_sort's input invariant, a pyo3 ut fail cause coverage to fail will be fixed by #4894

src/query/src/part_sort.rs Outdated Show resolved Hide resolved
src/query/src/part_sort.rs Outdated Show resolved Hide resolved
@waynexia waynexia added this pull request to the merge queue Oct 29, 2024
Merged via the queue into GreptimeTeam:main with commit 03f2fa2 Oct 29, 2024
33 checks passed
@waynexia waynexia deleted the windowed-sort-rule branch October 29, 2024 08:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs-not-required This change does not impact docs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants