Skip to content

fix(storage): fix ORDER BY LIMIT pushdown and index misalignment in fuse table functions - #19680

Merged
zhyass merged 2 commits into
databendlabs:mainfrom
zhyass:feat_stream
Apr 8, 2026
Merged

fix(storage): fix ORDER BY LIMIT pushdown and index misalignment in fuse table functions#19680
zhyass merged 2 commits into
databendlabs:mainfrom
zhyass:feat_stream

Conversation

@zhyass

@zhyass zhyass commented Apr 8, 2026

Copy link
Copy Markdown
Member

I hereby agree to the terms of the CLA available at: https://docs.databend.com/dev/policies/cla/

Summary

  • Fix incorrect LIMIT pushdown for table functions (fuse_block, fuse_segment, etc.):
    when order_by is present in PushDownInfo, don't pass limit to apply() since table functions can't do TopN — let the upper Sort operator handle it.
  • Fix fuse_segment chunk-local enumerate() index bug: use global offset instead of chunk-local index when accessing segment_locations[].

After fix for #19679

root@localhost:8000/default/default> set max_threads = 1;

root@localhost:8000/default/default> select row_count from fuse_block('db_09_0102', 't') 
order by row_count asc limit 2;
╭───────────╮
│ row_count │
│   UInt64  │
├───────────┤
│         1 │
│         2 │
╰───────────╯
2 rows read in 0.123 sec. Processed 5 rows, 1.05 KiB (40.65 rows/s, 8.51 KiB/s)

root@localhost:8000/default/default> select row_count from fuse_segment('db_09_0102', 't') 
order by row_count asc limit 2;
╭───────────╮
│ row_count │
│   UInt64  │
├───────────┤
│         1 │
│         2 │
╰───────────╯
2 rows read in 0.063 sec. Processed 5 rows, 887 B (79.37 rows/s, 13.75 KiB/s)

root@localhost:8000/default/default> select row_count from fuse_snapshot('db_09_0102', 't') 
order by row_count asc limit 2;
╭───────────╮
│ row_count │
│   UInt64  │
├───────────┤
│         5 │
│         6 │
╰───────────╯
2 rows read in 0.083 sec. Processed 5 rows, 1.30 KiB (60.24 rows/s, 15.71 KiB/s)

root@localhost:8000/default/default> select count(distinct file_location), count() from fuse_segment('db_09_0102', 't');
╭─────────────────────────────────────────╮
│ count(DISTINCT file_location) │ count() │
│             UInt64            │  UInt64 │
├───────────────────────────────┼─────────┤
│                             55 │
╰─────────────────────────────────────────╯
1 row read in 0.097 sec. Processed 5 row, 887 B (51.55 rows/s, 8.93 KiB/s)

root@localhost:8000/default/default> select file_location from fuse_segment('db_09_0102', 't');
╭──────────────────────────────────────────────────────────╮
│                       file_location                      │
│                          String                          │
├──────────────────────────────────────────────────────────┤
│ 21334/21368/_sg/h019d715ee88e7286b4ef763cba9b63cc_v4.mpk │
│ 21334/21368/_sg/h019d715ed6187dfcbc7a67b80872f340_v4.mpk │
│ 21334/21368/_sg/h019d715ec58f794bb374eb6d2d711bf0_v4.mpk │
│ 21334/21368/_sg/h019d715eb8347534b9bc186a01209e6c_v4.mpk │
│ 21334/21368/_sg/h019d715ea970761f81f2d64397f9899c_v4.mpk │
╰──────────────────────────────────────────────────────────╯
5 rows read in 0.068 sec. Processed 5 rows, 887 B (73.53 rows/s, 12.74 KiB/s)

Tests

  • Unit Test
  • Logic Test
  • Benchmark Test
  • No Test - Explain why

Type of change

  • Bug Fix (non-breaking change which fixes an issue)
  • New Feature (non-breaking change which adds functionality)
  • Breaking Change (fix or feature that could cause existing functionality not to work as expected)
  • Documentation Update
  • Refactoring
  • Performance Improvement
  • Other (please describe):

This change is Reviewable

@github-actions github-actions Bot added the pr-bugfix this PR patches a bug in codebase label Apr 8, 2026
@zhyass
zhyass requested review from SkyFan2002 and dantengsky April 8, 2026 09:05

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 68ae612554

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/query/storages/fuse/src/table_functions/fuse_dump_snapshot.rs Outdated
@zhyass

zhyass commented Apr 8, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread tests/sqllogictests/suites/base/09_fuse_engine/09_0051_fuse_meta_order_limit.test Outdated
Comment thread tests/sqllogictests/suites/base/09_fuse_engine/09_0051_fuse_meta_order_limit.test Outdated
Comment thread tests/sqllogictests/suites/base/09_fuse_engine/09_0051_fuse_meta_order_limit.test Outdated
Comment thread tests/sqllogictests/suites/base/09_fuse_engine/09_0051_fuse_meta_order_limit.test Outdated
@zhyass
zhyass enabled auto-merge April 8, 2026 12:39
@zhyass
zhyass added this pull request to the merge queue Apr 8, 2026
Merged via the queue into databendlabs:main with commit 763520a Apr 8, 2026
91 checks passed
@zhyass
zhyass deleted the feat_stream branch April 8, 2026 12:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix this PR patches a bug in codebase

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Table functions with ORDER BY LIMIT get incorrect results

3 participants