Skip to content

feat(query): refresh TopN statistics on append - #20091

Merged
KKould merged 17 commits into
databendlabs:mainfrom
KKould:feat/topn-append-freshness
Jul 20, 2026
Merged

feat(query): refresh TopN statistics on append#20091
KKould merged 17 commits into
databendlabs:mainfrom
KKould:feat/topn-append-freshness

Conversation

@KKould

@KKould KKould commented Jun 30, 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

  • Improve TopN freshness for append workloads. For non-transactional append commits, Databend now collects column TopN in the write path and refreshes table snapshot statistics when the previous snapshot statistics are still fresh.
  • This uses the existing table TopN options: configure analyze_top_n_columns and set analyze_top_n_size > 0 to enable collection for selected columns. If TopN options are disabled or cannot be safely refreshed, the optimizer falls back to the existing statistics behavior until a full ANALYZE.
  • Keep append TopN refresh out of explicit transactions for now. Transaction commits can collapse intermediate snapshot lineage, so transaction-aware TopN invalidation/regeneration will be handled separately instead of expanding this PR.
  • Reuse the existing statistics metadata flow instead of creating a separate TopN path: block/segment metadata carries column_top_n, commit meta aggregates it, and snapshot statistics are written together with the new snapshot.
  • Skip oversized string scalar values with LARGE_STRING_BYTES_THRESHOLD so one abnormal string value does not drop the whole TopN column.
  • Part of Track TopN freshness and runtime skew join follow-ups #20087.
  • Related to Handle HLL and TopN staleness after DELETE #20100.

Implementation

  • Let ColumnTopN carry its own capacity, which avoids passing top_n_size through every merge/add call.
  • Extend append block/segment/commit metadata to carry BlockTopN alongside HLL.
  • Merge append TopN into TableSnapshotStatistics only when the existing snapshot statistics match the current snapshot row count and predecessor snapshot id.
  • Limit append TopN refresh to autocommit append commits; explicit transactions skip refresh until table snapshot stats invalidation semantics are defined for transaction-local row-changing mutations.
  • Keep analyze and append collection on the same TopN builder path.

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-feature this PR introduces a new feature to the codebase label Jun 30, 2026
@KKould
KKould marked this pull request as draft June 30, 2026 21:08

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

ℹ️ 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/common/table_meta/src/meta/statistics.rs Outdated
@KKould
KKould force-pushed the feat/topn-append-freshness branch from 0e098ba to 6064b76 Compare July 2, 2026 01:14
@KKould

KKould commented Jul 2, 2026

Copy link
Copy Markdown
Member Author

@codex review

@KKould
KKould requested review from SkyFan2002, zhang2014 and zhyass July 2, 2026 01:25
@KKould KKould self-assigned this Jul 2, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 6064b76024

ℹ️ 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".

@KKould
KKould marked this pull request as ready for review July 2, 2026 05:11
@KKould
KKould marked this pull request as draft July 2, 2026 05:40
@KKould
KKould force-pushed the feat/topn-append-freshness branch 3 times, most recently from a9d49cd to ef82699 Compare July 2, 2026 06:26
@KKould

KKould commented Jul 2, 2026

Copy link
Copy Markdown
Member Author

@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: ef82699cc7

ℹ️ 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/operations/commit.rs Outdated
@KKould
KKould force-pushed the feat/topn-append-freshness branch from ef82699 to 24e9fb2 Compare July 2, 2026 07:16
KKould added 2 commits July 2, 2026 18:57
…eshness

# Conflicts:
#	src/query/storages/fuse/src/fuse_table.rs
#	src/query/storages/fuse/src/io/write/block_statistics_writer.rs
#	src/query/storages/fuse/src/io/write/stream/block_builder.rs
#	src/query/storages/fuse/src/operations/analyze/analyze_ndv_meta.rs
#	src/query/storages/fuse/src/operations/analyze/analyze_state_sink.rs
#	src/query/storages/fuse/src/operations/analyze/collect_ndv_source.rs
#	src/query/storages/fuse/src/operations/commit.rs
#	src/query/storages/fuse/src/operations/common/processors/sink_commit.rs
#	src/query/storages/fuse/src/operations/common/processors/transform_mutation_aggregator.rs
#	tests/sqllogictests/suites/base/09_fuse_engine/09_0053_analyze_top_n.test
@KKould
KKould marked this pull request as ready for review July 3, 2026 01: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: 01acc8535c

ℹ️ 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/operations/commit.rs Outdated
@KKould

KKould commented Jul 3, 2026

Copy link
Copy Markdown
Member Author

@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: 49905f7d45

ℹ️ 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/operations/commit.rs
@KKould

KKould commented Jul 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. Nice work!

Reviewed commit: 0a29de2fe0

ℹ️ 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".

@zhang2014

Copy link
Copy Markdown
Member

@zhyass please help review.

@zhang2014
zhang2014 requested a review from forsaken628 July 13, 2026 02:36
@sundy-li

Copy link
Copy Markdown
Member

mutation will affect the statistics

@zhyass zhyass left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For Block HLL, when a new block is created, a block-level HLL is generated. These block HLLs are stored in a segment-level file with the prefix _hs. This way, when analyzing the table, we only need to read and aggregate these statistics files.
For TOPN, the table statistics file is updated whenever a write occurs. Which is somewhat different from the previous approach.

Comment thread src/query/storages/fuse/src/operations/common/processors/sink_commit.rs Outdated
Comment thread src/query/storages/fuse/src/io/write/block_statistics_writer.rs
Comment thread src/query/storages/fuse/src/operations/commit.rs Outdated
@KKould

KKould commented Jul 15, 2026

Copy link
Copy Markdown
Member Author

@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: 25b0715d00

ℹ️ 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/operations/commit.rs Outdated
@KKould

KKould commented Jul 15, 2026

Copy link
Copy Markdown
Member Author

@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: 34ff36e058

ℹ️ 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/io/write/block_statistics_writer.rs
@KKould
KKould requested a review from zhyass July 16, 2026 06:20
…eshness

# Conflicts:
#	src/query/storages/fuse/src/operations/commit.rs
#	src/query/storages/fuse/src/operations/common/processors/sink_commit.rs
#	src/query/storages/fuse/src/operations/common/processors/transform_mutation_aggregator.rs
@KKould
KKould added this pull request to the merge queue Jul 20, 2026
Merged via the queue into databendlabs:main with commit d1681a7 Jul 20, 2026
91 checks passed
@KKould
KKould deleted the feat/topn-append-freshness branch July 20, 2026 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-feature this PR introduces a new feature to the codebase

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants