Skip to content

Implement lazy columns replication in JOIN and ARRAY JOIN - #88752

Merged
Avogar merged 44 commits into
ClickHouse:masterfrom
Avogar:lazy-column-replication
Oct 28, 2025
Merged

Avogar merged 44 commits into
ClickHouse:masterfrom
Avogar:lazy-column-replication

Conversation

@Avogar

@Avogar Avogar commented Oct 17, 2025 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

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

Implement lazy columns replication in JOIN and ARRAY JOIN. Avoid converting special columns representation like Sparse and Replicated to full columns in some output formats. This avoids unnecessary data copy in memory.

Closes #82669.

To use lazy replication enable settings enable_lazy_columns_replication and allow_special_serialization_kinds_in_output_formats (they are disabled by default for now).

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

@clickhouse-gh

clickhouse-gh Bot commented Oct 17, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [57d94ce]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_binary, ParallelReplicas, s3 storage, parallel) failure
03657_gby_overflow_any_sparse FAIL cidb
Stress test (amd_msan) failure
Server died FAIL cidb
Hung check failed, possible deadlock found (see hung_check.log) FAIL cidb
Killed by signal (in clickhouse-server.log) FAIL cidb
Fatal message in clickhouse-server.log (see fatal_messages.txt) FAIL cidb
Killed by signal (output files) FAIL cidb
Found signal in gdb.log FAIL cidb

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Oct 17, 2025
@KochetovNicolai KochetovNicolai self-assigned this Oct 21, 2025
Comment thread src/Columns/ColumnIndex.h
namespace DB
{

/// Wrapper around ColumnVector to store indexes.

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 think this part was ejected from LowCardinality with no change, so I can skip reviewing it.

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.

Yes, exactly

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.

Howewer, I added a few new simple methods that I needed in ColumnReplicated

@Avogar Avogar Oct 21, 2025 •

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.

I found a bug in ColumnVariant::index implementation during testing. I will also create a separate bug-fix PR with this change to backport it

Comment thread src/Columns/ColumnReplicated.cpp Resolved
ColumnReplicated::ColumnReplicated(MutableColumnPtr && nested_column_)
: nested_column(std::move(nested_column_))
{
indexes.insertIndexesRange(0, nested_column->size());

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.

Can we deprecate this ctor? If ColumnReplicated can replace any column, then we can always use the initial one without indexes.

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.

Also, maybe we can check that no sparse/LC is possible inside. (not sure, but at least it does not make a lot of sense to me).

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.

Can we deprecate this ctor? If ColumnReplicated can replace any column, then we can always use the initial one without indexes.

We cannot use initial one if we will need to insert into it from ColumnReplicated. It's needed in MergingSortedTransform. Otherwise we will need to convert ColumnReplicated to full there and loose it's benefits.

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.

Also, maybe we can check that no sparse/LC is possible inside. (not sure, but at least it does not make a lot of sense to me).

Sparse inside Replicated 100% makes sense, Sparse column can contain big values inside that we want to avoid replicating.

LC inside Replicated doesn't make sense, let's avoid it

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.

Sparse inside Replicated 100% makes sense

Sparse is kind of included in replicated. At least you can reuse the same internal column when converting sparse->replicated, and only rebuild indexes

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.

If it's ok, I will add proper Sparse -> Replicated conversion later, maybe even in a separate PR.

Comment thread src/Columns/ColumnReplicated.cpp Resolved
Comment thread src/Functions/IFunction.cpp Resolved
Comment thread src/Functions/IFunction.cpp Resolved
Comment thread src/Functions/IFunction.cpp Resolved
Comment thread src/Columns/IColumn.cpp Resolved
Comment thread src/Columns/IColumn.cpp Resolved
Comment thread src/Columns/IColumn.cpp Resolved
Comment thread src/Core/ProtocolDefines.h Resolved
@Avogar
Avogar added this pull request to the merge queue Oct 28, 2025
Merged via the queue into ClickHouse:master with commit 8cb04b3 Oct 28, 2025
@Avogar
Avogar deleted the lazy-column-replication branch October 28, 2025 21:10
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 28, 2025
@robot-ch-test-poll4 robot-ch-test-poll4 added pr-backports-created-cloud deprecated label, NOOP pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR labels Oct 28, 2025
robot-clickhouse added a commit that referenced this pull request Oct 29, 2025
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 29, 2025
Avogar added a commit that referenced this pull request Oct 29, 2025
Backport #88752 to 25.10: Implement lazy columns replication in JOIN and ARRAY JOIN
azat added a commit to azat/ClickHouse that referenced this pull request Nov 3, 2025
Tuple itself cannot be Sparse, but, some previous ClickHouse version may
write this into the serialization.json, and such table will not be able
to loaded.

Follow-up for: ClickHouse#88752
pull Bot pushed a commit to erickirt/ClickHouse that referenced this pull request Aug 18, 2026
ArrayJoinStep::deserialize read enable_lazy_columns_replication from the
per-step QueryPlanSerializationSettings object, but
ArrayJoinStep::serializeSettings writes only max_block_size, so nothing ever
put that name into an ArrayJoinStep's settings block.

QueryPlan::serialize builds a fresh QueryPlanSerializationSettings for every
step and writeChangedBinary emits only the names that step assigned, while
readBinary leaves absent names at their DECLARE default. A write done by one
step therefore cannot supply a read done by another, and the only tree-wide
writer of this name is JoinSettings::updatePlanSettings, reached only from
JoinStepLogical. A deserialized ArrayJoinStep always got the plan DECLARE
default false, while the core setting defaults to true, so with
serialize_query_plan = 1 the node executing a remote fragment did eager column
replication on an untouched configuration. Rows are identical either way, so
the cost is lost memory and CPU savings, not a wrong result.

Carry the value in the step's existing flags byte (bit 4) and drop the settings
read, instead of adding the missing serializeSettings write. Adding the write
would newly emit a setting name that 25.8 does not declare, and
BaseSettings::readBinary throws UNKNOWN_SETTING on an unknown name; a bare
ARRAY JOIN plan works against 25.8 today because max_block_size is the only
name such a plan emits, so the settings channel would turn a working shape into
a hard error. Version-gating that write is impossible, since serializeSettings
receives no version and runs before the Serialization context exists.

The flags byte degrades gracefully. Measured over master 26.7 26.6 26.5 26.4
26.3 26.2 26.1 25.8: no branch uses bit 4 and no branch validates the byte, so
an older reader ignores the bit and keeps doing eager replication, exactly as
today. No plan-version bump is needed. No byte in serialize is conditioned on a
flag bit, so the stream cannot desynchronize.

This also removes the read-without-write asymmetry rather than balancing it:
the file now reads exactly the one plan setting it writes.

The read was introduced by ClickHouse#88752.

Related: ClickHouse#112079
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-backports-created-cloud deprecated label, NOOP pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-performance Pull request with some performance improvements 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.

Implement ColumnReplicated for the result of IColumn->replicate (similar to ColumnSparse)

6 participants