Repository navigation
Fix logical error in ASOF JOIN when the right ASOF key is lazily replicated - #121385
harikrishnan94 merged 5 commits into
Conversation
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.
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex). Two passes and one fix round:
Also checked, no finding: Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-10:20260921-141300 |
|
Workflow [PR], commit [02c6851] Summary: ✅
AI ReviewSummaryPR #121385 fixes nullable Missing context / blind spots
Final Verdict✅ Approve. |
Build profile diff (arm_release)Commit See the job log for details. |
CI finish ledger - 8fd9fcaEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens.
Session id: cron:our-pr-ci-monitor:20260921-213129 |
|
cc @vdimir |
| } | ||
| else | ||
| { | ||
| const auto & asof_column_nullable = assert_cast<const ColumnNullable &>(*asof_column.column).getNullMapData(); |
There was a problem hiding this comment.
Can we keep ColumnNullable as fast path and add additional check here?
There was a problem hiding this comment.
Done in 0ce2499: checkAndGetColumn<ColumnNullable> keeps the direct null map read for the unwrapped column, and only the wrapped case falls back to isNullAt.
I measured which arm runs where instead of assuming it. With the fast path replaced by a throw, it fires only on the enable_lazy_columns_replication = 0 arm of the regression test; with the old assert_cast restored in the fallback, only 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 (hash and parallel_hash, both settings) match the reference.
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]>
CI finish ledger - 0ce2499Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only
Neither row belongs to this diff, which is one One caveat on the first row, so nobody tries to clear it by merging master. #121590 rewrites the previous release's Session id: cron:our-pr-ci-monitor:20260923-010313 |
|
Let's try a simpler fix: |
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]>
|
Done in 02c6851: the The regression test passes 10/10 with identical rows in all four arms ( |
|
@groeneai Fix the CI and make it green |
|
@groeneai Fix CI issues and make it green |
|
@harikrishnan94 All public CI on 02c6851 is green (175 check runs success or skipped, Could you paste the name and error of the new failure here? The only test a private run sees for the first time is this PR's |
Backport #121385 to 26.10: Fix logical error in ASOF JOIN when the right ASOF key is lazily replicated
Backport #121385 to 26.9: Fix logical error in ASOF JOIN when the right ASOF key is lazily replicated
…ght ASOF key is lazily replicated
…ht ASOF key is lazily replicated
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]>
Cherry pick #121385 to 26.8: Fix logical error in ASOF JOIN when the right ASOF key is lazily replicated
…ht ASOF key is lazily replicated
Related: #89997
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a logical error (
Bad cast from type DB::ColumnReplicated to DB::ColumnNullable) in anASOF JOINwhose right side has aNullableASOF key produced through lazy column replication, for example byarrayJoinor a nestedJOIN.enable_lazy_columns_replicationis on by default, and on a release build the cast was unchecked, so the NULL filter on the ASOF key could keep or drop the wrong rows.Description
On released
26.8, anASOF LEFT JOINon aNullable(DateTime)key whose right-side key goes througharrayJoinfails theBad cast from type DB::ColumnReplicated to DB::ColumnNullableassertion, which terminates the server process on a debug or sanitizer build; a release build makes the cast unchecked and reads the null map out of aColumnReplicated, so the filter can keep or drop the wrong rows. CI report.Root cause.
HashJoindeliberately supports the lazy replication wrapper on the right side:materializeColumnsFromRightBlockpeels it, promotes the nested column toNullable, and re-wraps it.isLazyReplicationUsefulis true for everyNullablecolumn, sinceColumnNullable::isFixedAndContiguous()is false. But the ASOF-key NULL filter inaddBlockToJoinguards on the column type, peels onlyColumnConst, and then asserts it is aColumnNullable. The filter predatesColumnReplicated.Fix. Build the prepared key columns before the ASOF-key NULL filter and read the null map from the prepared ASOF key with the original
assert_cast. The existingall_key_columnsloop already strips the wrapper withremoveSpecialRepresentations, and it reads the whole block, so moving it above the filter does not change which rows are stored.Validation. New test for
hashandparallel_hash; its oracle is row equality against theenable_lazy_columns_replication = 0arm, not the absence of an error. It passes 10/10 fixed; reading the null map from the unprepared key again aborts 10/10 with the reported message, on the= 1arms only.grace_hashcannot reach the site: it materializes at its entry point and rejectsASOF.correctNullabilityInplaceis left alone: a wrapped column makes itscheckAndGetColumnprobe miss rather than abort, andremoveColumnNullabilitythen peels the wrapper, so I have no reproduction of a wrong value.Workflow [PR]
Sync PR [sync-upstream/pr/121385]
Version info
26.11.1.209-master(included in26.11and later)