Skip to content

Cherry pick #121385 to 26.8: Fix logical error in ASOF JOIN when the right ASOF key is lazily replicated - #130500

Merged
robot-clickhouse-ci-2 merged 7 commits into
backport/26.8/121385from
cherrypick/26.8/121385
Nov 13, 2026
Merged

robot-clickhouse-ci-2 merged 7 commits into
backport/26.8/121385from
cherrypick/26.8/121385

Conversation

@robot-clickhouse-ci-2

Copy link
Copy Markdown
Contributor

Original pull-request #121385

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

groeneai and others added 6 commits September 21, 2026 12:35
HashJoin deliberately supports the lazy column replication wrapper on the
right side. materializeColumnsFromRightBlock peels it, promotes the nested
column to Nullable when the sample column is Nullable, and re-wraps it, and
isLazyReplicationUseful is true for every Nullable column because
ColumnNullable::isFixedAndContiguous() returns false. So addBlockToJoin
legitimately receives an ASOF key of type Nullable(T) whose column is a
ColumnReplicated(ColumnNullable(...)).

The ASOF-key NULL filter guarded on asof_column.type->isNullable(), which
says nothing about the wrapper, peeled exactly one representation
(ColumnConst), and then asserted the column was a ColumnNullable in order to
hoist its null map. The branch that cannot handle the wrapper was therefore
the only branch that ran for the column class that is always wrapped. The
filter predates ColumnReplicated and was never revisited for it.

Read nullness through IColumn::isNullAt instead. ColumnReplicated::isNullAt
resolves the logical row through its indexes, so no peel and no
materialization is needed, and the same call three lines above already reads
a const key's nullness this way. Indexing the nested column with the logical
row would not have been equivalent: ColumnReplicated::size() is the indexes
size, so under replication the nested column is smaller and that read is out
of bounds rather than merely wrong.

Deleting the assertion loses no coverage. The "type says Nullable, column is
not a ColumnNullable" invariant is still enforced in the same call by
assertBlocksHaveEqualStructureAllowReplicated, which compares the concrete
column class after peeling one replication level on both sides, and which
runs before the block is stored.

The regression test asserts row equality between the
enable_lazy_columns_replication = 0 and = 1 arms rather than the absence of
an error, because a fix that peeled the wrapper but indexed the nested null
map would not fail, it would silently keep or drop the wrong rows. It covers
the hash and parallel_hash entry points; grace_hash cannot reach the site,
since it materializes the block at its entry point and rejects ASOF.

Co-Authored-By: Claude Opus 5 <[email protected]>
One stress worker in three runs with join_use_nulls=1
(ci/jobs/scripts/stress/stress.py:383). Under that setting an unmatched
ASOF LEFT JOIN row returns NULL for the right-side columns instead of the
type default, so the k = 2 and k = 4 rows disagreed with the reference and
the test would have failed in Stress test and Upgrade check.

A query-local SETTINGS clause overrides a client option only for the
settings it names, so pinning it in the query is what makes the oracle
stable. Local runs could not have caught this: join_use_nulls is not in
SettingsRandomizer, only stress.py injects it.

Co-Authored-By: Claude Opus 5 <[email protected]>
Collapse the added ASOF-key comment to the one non-obvious property: the wrapper resolves the logical row.
Requested in review: read the null map directly when the ASOF key column
really is a ColumnNullable, and only fall back to IColumn::isNullAt for the
representations that the direct read cannot serve.

checkAndGetColumn<ColumnNullable> replaces the assert_cast that aborted, so
the wrapped ColumnReplicated(ColumnNullable(...)) case takes the fallback
instead of being asserted away, while the common unwrapped case keeps the
original per-row null map indexing and no longer pays a virtual call.

Measured with a mutation binary built from this tree, both join algorithms of
the regression test: with the fast path replaced by a throw, the
enable_lazy_columns_replication = 0 arm raises it (size=12) and the = 1 arm
does not, so the fast path is taken exactly for the unwrapped column; with
the fallback replaced by the old assert_cast, the = 1 arm aborts with the
reported 'Bad cast from type DB::ColumnReplicated to DB::ColumnNullable' at
the same two addBlockToJoin frames. Unmutated, all four arms return output
byte-identical to the committed reference.

Co-Authored-By: Claude Opus 5 <[email protected]>
Build the prepared key columns before the ASOF NULL filter and read the
null map from the prepared ASOF key, so the filter keeps the original
assert_cast and no longer sees the lazy replication wrapper.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…azy-replication

Fix logical error in ASOF JOIN when the right ASOF key is lazily replicated
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Nov 10, 2026
Resolve the only conflict, in HashJoin::addBlockToJoin: 26.8 still
increments data->rows_to_join right after computing the row count
(master moved it later in #116991, which is not backported). Keep that
line and take #121385's move of the key-column loop above the ASOF
NULL filter unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator

The only conflict is in HashJoin::addBlockToJoin: 26.8 still increments data->rows_to_join right after const size_t rows = selector.size(); (master moved that line in #116991, which is not on 26.8). I kept that line and took the move of the key-column loop above the ASOF NULL filter as is. Against backport/26.8/121385 the merge changes only HashJoin.cpp and the two 05233_asof_join_nullable_key_lazy_replication test files, and the changed lines are identical to master.

26.8 is affected: the official 26.8.35.29 returns a wrong row for k = 3 in the test's enable_lazy_columns_replication = 1 queries (NULL instead of r3b), and a debug build of the 26.8 tree without the HashJoin.cpp change fails with the original Logical error: 'Bad cast from type DB::ColumnReplicated to DB::ColumnNullable'. With the change (clang-22, debug) the test passes, 50 of 50 with random settings.

I cannot push to cherrypick/26.8/121385. The merge (9de9c850fc6c) is on my fork and its first parent is this PR's head bdca15a49a1d, so applying it is a fast-forward:

git fetch https://github.com/groeneai/ClickHouse.git groeneai/resolve-26.8-cherrypick-121385
git push origin FETCH_HEAD:cherrypick/26.8/121385

@robot-clickhouse-ci-2
robot-clickhouse-ci-2 merged commit 63d6878 into backport/26.8/121385 Nov 13, 2026
2 checks passed
@robot-clickhouse-ci-2
robot-clickhouse-ci-2 deleted the cherrypick/26.8/121385 branch November 13, 2026 13:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants