Skip to content

Kafka: map columns with special names to message metadata on INSERT - #103243

Merged
alexey-milovidov merged 8 commits into
masterfrom
kafka-headers-producer
Apr 26, 2026
Merged

alexey-milovidov merged 8 commits into
masterfrom
kafka-headers-producer

Conversation

@alexey-milovidov

Copy link
Copy Markdown
Member

Adds a new Kafka table setting kafka_map_virtual_columns_on_write
(default false). When enabled, columns in the Kafka table schema named
_key, _timestamp, _headers.name and _headers.value are produced
as the corresponding Kafka message key, timestamp and headers, and are
excluded from the message payload. Default behavior is unchanged.

This lets consume-process-produce pipelines reconstruct Kafka messages
with the original key, timestamp and headers without polluting the
payload with extra fields like _key.

Closes #85610.

Changelog category (leave one):

  • New Feature

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

Added a new Kafka table setting kafka_map_virtual_columns_on_write. When enabled, columns named _key, _timestamp, _headers.name and _headers.value in the Kafka table schema are produced as the corresponding Kafka message key, timestamp and headers on INSERT, and are excluded from the message payload.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Add a new Kafka table setting `kafka_map_virtual_columns_on_write`
(default `false`). When enabled, columns in the Kafka table schema named
`_key`, `_timestamp`, `_headers.name` and `_headers.value` are produced
as the corresponding Kafka message key, timestamp and headers, and are
excluded from the message payload.

This lets producers reconstruct a Kafka message with the original key,
timestamp and headers (for consume-process-produce pipelines) without
the payload picking up extra fields such as `_key`.

Closes #85610

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Apr 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a3fe42d]

Summary: ✅


AI Review

Summary

This PR adds kafka_map_virtual_columns_on_write for Kafka tables so _key, _timestamp, and paired _headers.* columns can be emitted as Kafka metadata and excluded from payload when types match, while preserving payload for one-sided or non-matching header schemas. The final implementation aligns payload filtering with effective producer mapping and includes integration coverage for both full mapping and one-sided header behavior.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ✅
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Apr 21, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

#103248

Only exclude a column from the Kafka message payload when its type
matches what `KafkaProducer` actually maps (String for `_key`, DateTime
for `_timestamp`, `Array(String)` for `_headers.name`/`_headers.value`).
Otherwise keep the column in the payload so schemas reusing these
names with different types keep working — addresses reviewer feedback
from #103243.

Also fix `test_kafka_produce_virtual_columns_mapping` which tried to
insert `['only_name']`/`[]` into a Nested `_headers.*` pair — that row
is rejected by the standard `NestedElementsValidationTransform` before
it reaches the producer, so remove it and drop the unreachable
`std::min` fallback in `KafkaProducer::produce`.

The Nested-size constraint is now reflected in `kafka.md`.
alexey-milovidov and others added 3 commits April 21, 2026 22:18
`splitPayloadColumns` previously excluded `_headers.name` and
`_headers.value` from the payload independently as long as each
column had `Array(String)` type. But `KafkaProducer` only emits Kafka
headers when both columns are present, so a schema containing just one
of them would silently lose that column from the payload.

Compute the header-mapping decision once (both columns present and
typed `Array(String)`) and use it for both columns. The reading and
writing predicates now stay in sync with `KafkaProducer::KafkaProducer`.

Adds an integration test for the one-sided case that verifies the
remaining header column is preserved in the JSON payload.

Addresses reviewer feedback from
#103243.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
# Conflicts:
#	tests/queries/0_stateless/02818_memory_profiler_sample_min_max_allocation_size.sh
@clickhouse-gh

clickhouse-gh Bot commented Apr 26, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.70% 83.80% +0.10%
Functions 91.10% 91.10% +0.00%
Branches 76.20% 76.20% +0.00%

Changed lines: 97.44% (152/156) | lost baseline coverage: 5 line(s) · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov left a comment

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.

Looks good.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Apr 26, 2026
Merged via the queue into master with commit 3de15f1 Apr 26, 2026
165 checks passed
@alexey-milovidov
alexey-milovidov deleted the kafka-headers-producer branch April 26, 2026 11:16
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 26, 2026
@alexey-milovidov alexey-milovidov mentioned this pull request Sep 20, 2026
47 of 72 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-feature Pull request with new product feature 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.

Kafka Engine - Granular control to map columns to Kafka message Key, Value, and Headers when producing

2 participants