Skip to content

[BugFix] Propagate sample options to lake scan#71874

Open
levin-kitty wants to merge 2 commits into
StarRocks:mainfrom
levin-kitty:fix/lake-scan-sample-options
Open

[BugFix] Propagate sample options to lake scan#71874
levin-kitty wants to merge 2 commits into
StarRocks:mainfrom
levin-kitty:fix/lake-scan-sample-options

Conversation

@levin-kitty

@levin-kitty levin-kitty commented Apr 18, 2026

Copy link
Copy Markdown

Why I'm doing:

In shared-data mode, it looks like the TABLESAMPLE / SAMPLE('percent'='N') clause on lake tables doesn't actually take effect. From what I can see, the FE parses the clause correctly, but TLakeScanNode doesn't have a sample_options field, so the info never reaches the BE. So...

  • ANALYZE SAMPLE TABLE seems to do a full scan on each selected tablet
  • SELECT ... TABLET(x) SAMPLE('percent'='N') on lake tables also reads the full tablet
  • The existing block-level sampling code in BE (SegmentIterator::_apply_data_sampling) is never reached for lake tables

In my own test on a large table, ANALYZE SAMPLE went from about 2 minutes to about 8 seconds once sampling actually runs. I think this just brings shared-data to the same behavior that shared-nothing already has — please correct me if I'm missing context here.

What I'm doing:

Trying to complete the FE→BE propagation chain for sample_options on lake scans, mirroring the existing shared-nothing path.

  1. Thrift (gensrc/thrift/PlanNodes.thrift): add sample_options (field 58, optional) to TLakeScanNode, same shape as TOlapScanNode
  2. FE (fe/fe-core/.../planner/OlapScanNode.java): in the lake branch, set sample_options when a TableSampleClause is attached - copied the pattern from the OLAP branch right below it
  3. BE lake connector (be/src/connector/lake_connector.cpp): forward TLakeScanNode.sample_options into TabletReaderParams
  4. BE lake tablet reader (be/src/storage/lake/tablet_reader.cpp): forward from TabletReaderParams into RowsetReadOptions
  5. BE lake rowset (be/src/storage/lake/rowset.cpp): forward from RowsetReadOptions into SegmentReadOptions

As far as I can tell, everything downstream (SegmentIterator::_apply_data_sampling, BlockDataSample, PageDataSample) is already in place and didn't need changes. If I missed any path, please let me know.

This is my first PR to StarRocks — thanks in advance for the review, and happy to fix anything that needs changing.

Fixes #71873

What type of PR is this:

  • BugFix
  • Feature
  • Enhancement
  • Refactor
  • UT
  • Doc
  • Tool

Does this PR entail a change in behavior?

  • Yes, this PR will result in a change in behavior.
  • No, this PR will not result in a change in behavior.

If yes, please specify the type of change:

  • Interface/UI changes: syntax, type conversion, expression evaluation, display information
  • Parameter changes: default values, similar parameters but with different default values
  • Policy changes: use new policy to replace old one, functionality automatically enabled
  • Feature removed
  • Miscellaneous: upgrade & downgrade compatibility, etc.

Checklist:

  • I have added test cases for my bug fix or my new feature
  • This pr needs user documentation (for new or modified features or behaviors)
    • I have added documentation for my new feature or new function
    • This pr needs auto generate documentation
  • This is a backport pr

Bugfix cherry-pick branch check:

  • I have checked the version labels which the pr will be auto-backported to the target branch
    • 4.1
    • 4.0
    • 3.5
    • 3.4

@levin-kitty levin-kitty requested a review from a team as a code owner April 18, 2026 06:47
@wanpengfei-git wanpengfei-git requested a review from a team April 18, 2026 06:48
@levin-kitty levin-kitty force-pushed the fix/lake-scan-sample-options branch from 46fe6b0 to 0ecd3de Compare April 18, 2026 07:03
@github-actions github-actions Bot added the 4.1 label Apr 18, 2026
@levin-kitty levin-kitty force-pushed the fix/lake-scan-sample-options branch from 0ecd3de to cf390c3 Compare April 22, 2026 10:28
@eshishki

Copy link
Copy Markdown
Contributor

@murphyatwork @wyb — could one of you take a look at this? It's been sitting since April with no review.

The bug is real and not niche: ANALYZE SAMPLE is documented as supported (no shared-data caveat in ANALYZE_TABLE.md), and enable_use_table_sample_collect_statistics defaults to true, so the default auto-analyze path builds TABLET(...) SAMPLE('percent'='N'). On lake tables that SAMPLE clause is silently dropped — TLakeScanNode has no sample_options field, so the FE-parsed sample never reaches the BE and SegmentIterator::_apply_data_sampling is never hit. Result: full tablet scan instead of block sampling on every shared-data cluster (author measured ANALYZE SAMPLE going 2 min → 8 s once it actually samples).

The PR just completes the FE→BE propagation chain for lake, mirroring the existing TOlapScanNode path — 13 lines, no downstream changes needed. Tracked in #71873.

Only gap I see is there's no test — @levin-kitty, worth adding one to lock the behavior in.

@levin-kitty

Copy link
Copy Markdown
Author

@eshishki

Thanks for the feedback! I will take a look at how to add tests for this.

@levin-kitty levin-kitty force-pushed the fix/lake-scan-sample-options branch from cf390c3 to 359abe2 Compare July 11, 2026 07:05
@levin-kitty

Copy link
Copy Markdown
Author

@eshishki @murphyatwork @wyb

Hi, I rebased this PR onto the latest main and added two small regression tests as suggested.

  • FE test checks that sample options are serialized into TLakeScanNode.
  • BE test checks that the options reach SegmentReadOptions through the lake scan path.

Both focused tests passed locally.

Could you please take a look when you have time? Thanks.

@levin-kitty levin-kitty force-pushed the fix/lake-scan-sample-options branch 2 times, most recently from 8d56b30 to 5c761a7 Compare July 12, 2026 13:32
@kevincai kevincai self-requested a review July 14, 2026 02:18
@CelerData-Reviewer

Copy link
Copy Markdown

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 5c761a7ddc

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@github-actions

Copy link
Copy Markdown
Contributor

[Java-Extensions Incremental Coverage Report]

pass : 0 / 0 (0%)

@github-actions

Copy link
Copy Markdown
Contributor

[FE Incremental Coverage Report]

pass : 4 / 4 (100.00%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 com/starrocks/planner/OlapScanNode.java 4 4 100.00% []

@sonarqubecloud

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown
Contributor

[BE Incremental Coverage Report]

pass : 4 / 4 (100.00%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 be/src/connector/lake/lake_connector.cpp 2 2 100.00% []
🔵 be/src/storage/lake/tablet_reader.cpp 1 1 100.00% []
🔵 be/src/storage/lake/rowset.cpp 1 1 100.00% []

xiangguangyxg
xiangguangyxg previously approved these changes Jul 14, 2026
Signed-off-by: levin-kitty <levin.de.kitty@gmail.com>
Signed-off-by: levin-kitty <levin.de.kitty@gmail.com>
@github-actions

Copy link
Copy Markdown
Contributor

Module-risk briefing for Codex review

This is review focus, not a finding list. Validate against the diff.

High-confidence risks

@levin-kitty

Copy link
Copy Markdown
Author

@xiangguangyxg @wangshisan @kevincai

Rebased onto the latest main again and resolved the conflicts.

The recent lake reader refactoring already included part of the sample_options propagation, so I removed the duplicated changes and kept only what is still needed.

The focused FE and BE tests pass locally. Thanks!

@CelerData-Reviewer

Copy link
Copy Markdown

@codex review

@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: 0de31e770f

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +1267 to +1271
if (sample != null && sample.isUseSampling()) {
TTableSampleOptions sampleOptions = new TTableSampleOptions();
msg.lake_scan_node.setSample_options(sampleOptions);
sample.toThrift(sampleOptions);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Include sample options in cache key

When enable_query_cache=true for shared-data queries, this makes the lake scan result depend on sample (method/seed/percent), but the query-cache digest is still built from OlapScanNode.toNormalForm, whose TNormalOlapScanNode has no sample fields and sampled scans are not marked uncacheable. Two otherwise identical lake queries with different SAMPLE options can therefore share the same cache entry, so the later query may read rows produced with the earlier sampling options. Please add the sample options to the normal form or disable query cache for sampled lake scans.

Useful? React with 👍 / 👎.

@xiangguangyxg

Copy link
Copy Markdown
Contributor

@levin-kitty Think about the codex review comment

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.

ANALYZE SAMPLE falls back to full scan on lake tables in shared-data mode

5 participants