Repository navigation
Fix wrong value for a virtual column on the right side of direct JOIN - #113698
PedroTadim merged 2 commits into
Conversation
`SELECT dd._table FROM t LEFT JOIN <key-value table> AS dd ON t.pref = dd.k` returned the contents of the join key column where the table name belongs, or failed with `LOGICAL_ERROR` `Bad cast from type DB::ColumnNullable to DB::ColumnLowCardinality`, depending on the key type. Both happen at stock settings, because `join_algorithm` defaults to `direct,parallel_hash,hash`. `DirectKeyValueJoin::joinBlock` builds the right-side columns in two steps that disagree about indexing. `convertBlockStructure` skips any header column the storage does not report and returns a compacted column vector, then the insertion loop re-indexes that vector positionally against the uncompacted header. A virtual column such as `_table` or `_database` is materialized at plan time and is therefore in the header and never in the storage sample block, so it is exactly the skipped case and a data column ends up published under its name and declared type. For a dictionary the misalignment is guaranteed, because `IDictionary::getByKeys` filters out non-attributes and then prepends the key columns, so the key occupies the slot reserved for the virtual column. Decline `DirectKeyValueJoin` in `tryDirectJoin` when a mapped header column is absent from the storage sample block, so the existing `HashJoin` fallback, whose right subtree does materialize virtual columns, answers the query. `tryDirectJoin` already declines for the join key under the same two conditions a few lines above; this applies that rule to non-key columns. The guard is about the storage being unable to supply the column, not about a column name, because `_database` misbehaves identically. `DirectJoin.cpp` is deliberately unchanged. Repairing the positional index there would stop publishing another column's data but still leave the query unanswerable, because the filled-join plan has no right subtree that could materialize the unbound slot. Direct join remains selected for every normal-column shape, and no existing test loses coverage. With `join_algorithm = 'direct'` pinned alone, such a query now returns `NOT_IMPLEMENTED` rather than a wrong value, which is the pre-existing behaviour of the two bail-outs already present for a mixed `ON` condition and for `RIGHT JOIN`.
The test creates an EmbeddedRocksDB table, so it must not run in a build without rocksdb. `use-rocksdb` is a required-build-flag gate, not only a replicated-database skip: tests/clickhouse-test:3080-3083 returns FailureReason.BUILD for any `use-*` tag absent from the build flags, which collect_build_flags fills from `system.build_options WHERE name LIKE 'USE_%'`. Fast test configures -DENABLE_LIBRARIES=0 and never re-enables rocksdb, so USE_ROCKSDB is unset, registerStorageEmbeddedRocksDB is compiled out, and an untagged test does not skip -- it runs and its first CREATE TABLE fails with UNKNOWN_STORAGE. The same tag also covers the two stateless `old analyzer` ParamSets, which pair `old analyzer` with DBReplicated, where these queries are UNKNOWN_IDENTIFIER and never reach the code under test. Of the 35 stateless tests that create such a table, this was the only one carrying neither `use-rocksdb` nor `no-fasttest`. `no-fasttest` is deliberately not added: both gates sit in the same should_skip_test and both return a FailureReason, so it would cover no job that `use-rocksdb` does not. `no-parallel-replicas` is deliberately not added either, unlike the two sibling direct-join tests: measured against a server configured the way `install.sh --parallel-rep` does it, all twelve arms behave identically to the plain flavour, the EXPLAIN arm still shows DirectKeyValueJoin being chosen, and the whole file passes. Those siblings join a real left table, whereas every arm here has a constant subquery on the left, which is not a parallel-replicas-eligible reading step.
Internal second-model review (click to expand)Two review rounds by an independent reviewer that never edits the code, plus two automated
Also examined and found sound, independently of the fix plan: the guard's placement inside the One finding was opened by the reviewer and then withdrawn by the reviewer: an apparent |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-review-slot-53:20260806-160300 |
|
cc @vdimir @nickitat, could you review this? |
|
This one looks worth backporting: the wrong value is returned at stock settings, since @PedroTadim, could you add |
|
cc @vdimir easy to review |
|
Workflow [PR], commit [12e926f] Summary: ✅
AI ReviewSummaryThis PR teaches Final Verdict✅ No findings. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 6/7 (85.71%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units7 translation units recompiled, 13 s compile time in total, 7 of them have a recent master baseline. |
… right side of direct JOIN
Backport #113698 to 26.7: Fix wrong value for a virtual column on the right side of direct JOIN
Backport #113698 to 26.5: Fix wrong value for a virtual column on the right side of direct JOIN
Backport #113698 to 26.6: Fix wrong value for a virtual column on the right side of direct JOIN
…umn on the right side of direct JOIN
…umn on the right side of direct JOIN
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a wrong value returned for a virtual column such as
_tableor_databaseselected from the right side of a JOIN served byDirectKeyValueJoin(a key-value storage such asEmbeddedRocksDB,KeeperMap,Redis, or a dictionary). The query returned the contents of a data column instead, or failed withLOGICAL_ERROR.Description
SELECT dd.v, dd._table FROM t LEFT JOIN <key-value table> AS dd ON t.pref = dd.kreturnedVALA KEYAwhereVALA s2is correct: the join key column's value appeared where the tablename belongs. With a key type other than
LowCardinality(String)it failed withLOGICAL_ERROR(
Bad cast ... to ColumnLowCardinality). Both occur at stock settings, sincejoin_algorithmdefaults to
direct,parallel_hash,hash.Root cause:
DirectKeyValueJoin::joinBlockbuilds the right-side columns in two steps thatdisagree about indexing.
convertBlockStructureskips any header column the storage does notreport (
DirectJoin.cpp:36-37), returning a compacted column vector, and the insertion loopre-indexes it positionally against the uncompacted header (
:188-192). Aplan-materialized virtual column is exactly the skipped case, so a data column gets published
under the virtual column's name and declared type. For a dictionary
getByKeysalso prependsthe key columns (
IDictionary.h:460-466), so the key lands in the slot reserved for_table.The fix declines
DirectKeyValueJoinintryDirectJoinwhen a mapped header column is absentfrom the storage's sample block, letting the existing
HashJoinfallback, whose right subtreedoes materialize virtual columns, answer the query. That function already carries two bail-outs
of this shape for the join key (
PlannerJoins.cpp:1108-1113); this is the same rule for non-keycolumns.
DirectJoin.cppis unchanged, so its positional re-index becomes unreachable hererather than being removed.
Direct join is still chosen for every normal-column shape, and no existing test loses coverage.
Dictionaries and
EmbeddedRocksDBare verified affected by measurement;KeeperMapandRedisby construction, having an identical
getSampleBlock. One deliberate behaviour change, pinned bya test arm: with
join_algorithm = 'direct'alone such a query now returnsNOT_IMPLEMENTEDrather than a wrong value, as the existing bail-outs for a mixed
ONcondition and forRIGHT JOINalready do.Version info
26.7.4.8,26.6.3.18,26.5.7.18