Skip to content

Native Common Virtuals for Buffer - #101537

Merged
Michicosun merged 9 commits into
masterfrom
native_common_virtuals/Buffer
Apr 4, 2026
Merged

Michicosun merged 9 commits into
masterfrom
native_common_virtuals/Buffer

Conversation

@Michicosun

@Michicosun Michicosun commented Apr 1, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Part of #100766

Version info

  • Merged into: 26.4.1.565

@clickhouse-gh

clickhouse-gh Bot commented Apr 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5e5e543]

Summary: ❌

job_name test_name status info comment
Stress test (arm_msan) failure
Server died FAIL cidb
MemorySanitizer: use-of-uninitialized-value (STID: 2410-543a) FAIL cidb

AI Review

Summary

This PR extends support for virtual column _table in MergeTree and Buffer, updates Distributed virtual-column registration accordingly, and adjusts stateless tests. I found one blocker: StorageBuffer::read can drop destination rows for SELECT _table when the destination engine does not provide _table (e.g. Memory). This is a correctness issue because results become dependent on destination virtual-column support rather than Buffer semantics.

Missing context
  • ⚠️ No CI logs/results were provided in this task context.
Findings

❌ Blockers

  • [src/Storages/StorageBuffer.cpp:363-380] SELECT _table FROM <buffer_table> can silently skip flushed rows when destination does not expose _table. In the mismatch branch, _table is removed from columns_intersection; if that becomes empty, destination read is skipped entirely.
    Suggested fix: keep reading destination rows and synthesize _table for them as the queried Buffer table name (or ensure _table remains materialized via post-read expression/default path).
Tests
  • ⚠️ Add a stateless test where Buffer has a destination engine without _table (e.g. Memory), with rows both flushed and in-memory, and verify SELECT _table and SELECT *, _table return all rows.
ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ⚠️ Behavior of _table for Buffer is user-visible and changed; plus blocker causes row omission in some setups.
SettingsChangesHistory.cpp ➖
PR metadata quality ⚠️ Not for changelog is questionable for a user-visible virtual-column behavior change.
Safe rollout ⚠️ Current blocker can produce incorrect query results on specific destination engines.
Compilation time ✅
Performance & Safety
  • Correctness concern only: destination rows may be omitted for _table-only reads in Buffer with destinations lacking _table.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
    • Fix StorageBuffer::read so destination rows are never dropped when _table is requested.
    • Add a regression test for Buffer + destination without _table virtual column support.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Apr 1, 2026
Comment thread src/Storages/StorageBuffer.cpp Resolved
VirtualColumnsDescription StorageBuffer::createVirtuals()
{
VirtualColumnsDescription desc;
desc.addEphemeral("_table", std::make_shared<DataTypeLowCardinality>(std::make_shared<DataTypeString>()), "");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This PR also adds a new user-visible virtual column (_table) for Buffer, but there is no corresponding stateless test coverage.

Please add a tests/queries/0_stateless test that selects _table from a Buffer table (ideally with rows both in the in-memory buffer and in the destination table) and verifies the expected table name is returned.

@KochetovNicolai KochetovNicolai self-assigned this Apr 1, 2026
/// Reads from one buffer (from one block) under its mutex.
class BufferSource : public ISource
{
ColumnPtr fillVirtualColumn(const String & name, const DataTypePtr & type, size_t num_rows) const

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.

I feel like we can have this code shared for many sources by default. Maybe make some base class, like SourceWithVirtuals?

@Michicosun Michicosun Apr 1, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Actually, I thought about it, and IMO it will require more effort in the future if we unite implementations of different storages. I like the idea that storage must always provide a way to customize its virtual columns, and this should not be tied to other storage engines.

wdyt?

Comment thread src/Storages/StorageBuffer.cpp Resolved
Comment thread src/Storages/StorageBuffer.cpp Resolved

@KochetovNicolai KochetovNicolai 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.

Overall ok

Comment thread src/Storages/StorageBuffer.cpp Resolved
{
if (!dest_columns.hasPhysical(column_name))
auto dest_column = destination_columns.tryGetByName(column_name);
if (!dest_column)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

❌ SELECT _table FROM <buffer_table> can silently drop flushed rows when the destination engine has no _table virtual column.

In the mismatch branch, _table is removed from columns_intersection; if it is the only requested column, columns_intersection becomes empty and destination rows are skipped entirely ("Block of data is skipped."). That makes results depend on destination engine support instead of Buffer semantics.

Please keep destination rows in this case and synthesize _table for them from the queried Buffer table name (or otherwise ensure destination rows are still returned when _table is requested).

Comment on lines +55 to +56
d5 7 70
d5 8 80

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

this is a behavior change

@clickhouse-gh

clickhouse-gh Bot commented Apr 1, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.00% 84.00% +0.00%
Functions 90.90% 90.90% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 100.00% (91/91) | lost baseline coverage: 7 line(s) · Uncovered code

Full report · Diff report

@Michicosun
Michicosun added this pull request to the merge queue Apr 4, 2026
Merged via the queue into master with commit f2b05ea Apr 4, 2026
158 of 159 checks passed
@Michicosun
Michicosun deleted the native_common_virtuals/Buffer branch April 4, 2026 20:42
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 4, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi @Michicosun @CheSema @KochetovNicolai — this PR appears to contain a backward incompatible change but is categorized as Not for changelog (changelog entry is not required).

Why: The _table virtual column for Buffer previously returned the buffer table name for ALL rows. After this PR, it returns the destination table name for flushed data. This changes observable SQL semantics (test 03094 reference shows buffer1->d5 for flushed rows). Users filtering WHERE _table='buffer_name' will silently get fewer rows. Additionally, the PR introduces a crash for non-MergeTree destinations.

Suggested category: Backward Incompatible Change

Could you check if the changelog category should be updated? This helps users know what to expect when upgrading. Ignore this if the change is intentionally non-breaking.

@clickgapai

Copy link
Copy Markdown
Contributor

Hi @Michicosun @CheSema @KochetovNicolai — while reviewing this PR I found the following:

Happy to discuss — close anything that's wrong or already addressed.

Mip182 pushed a commit to Mip182/ClickHouse that referenced this pull request May 9, 2026
…rtuals/Buffer

Native Common Virtuals for Buffer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants