Skip to content

Allow lowcard key in in direct join with join logical - #105057

Closed
vdimir wants to merge 2 commits into
masterfrom
vdimir/direct_join_lowcard_right_table
Closed

vdimir wants to merge 2 commits into
masterfrom
vdimir/direct_join_lowcard_right_table

Conversation

@vdimir

@vdimir vdimir commented May 15, 2026

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

...

@clickhouse-gh

clickhouse-gh Bot commented May 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [371e061]

Summary: ❌

job_name test_name status info comment
Fast test FAIL
03712_nested_loop_join_merge_tree FAIL cidb
Fast test (arm_darwin) DROPPED
Build (amd_debug) DROPPED
Build (amd_asan_ubsan) DROPPED
Build (amd_tsan) DROPPED
Build (amd_msan) DROPPED
Build (amd_binary) DROPPED
Build (arm_debug) DROPPED
Build (arm_asan_ubsan) DROPPED
Build (arm_tsan) DROPPED

AI Review

Summary

This PR updates logical join planning so LowCardinality key equivalence can still use DirectKeyValueJoin, and it adds coverage for USING/ON paths. The previously flagged nullable-cast correctness issue in JoinStepLogical appears fixed on current head, but regression evidence is still incomplete for the nullable-left/non-null storage key mismatch case.

Tests
  • ⚠️ Add a focused stateless case where the left key is Nullable(String) and the storage PK is LowCardinality(String), and assert both result correctness and that EXPLAIN does not choose Algorithm: DirectKeyValueJoin.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
  1. Add the missing regression coverage (SQL + reference) for nullable-left vs non-null LowCardinality PK mismatch in 04209_rocksdb_direct_join_lowcardinality_pk_equivalence.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label May 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 15, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 90.80% 90.80% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 93.33% (14/15) | lost baseline coverage: 1 line(s) · Uncovered code

Full report · Diff report

@vdimir
vdimir marked this pull request as ready for review May 18, 2026 07:45
@vdimir
vdimir force-pushed the vdimir/direct_join_lowcard_right_table branch from 8c78a28 to 04b1439 Compare May 18, 2026 15:58
)
WHERE explain LIKE '%Algorithm:%';

-- LowCardinality(Nullable(String)) PK: exercises the recursive

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test still misses the nullable-mismatch path from the previous thread: left key Nullable(String) joined to storage PK LowCardinality(String).

That is the case where predicateOperandsToCommonType must force a right-side cast so we do not keep DirectKeyValueJoin; otherwise StorageEmbeddedRocksDB::getByKeys serializes nullable lookup bytes and non-null keys can miss.

Please add a focused regression here that makes the left key nullable against rdb_lc_pk and asserts both correctness of rows and that EXPLAIN does not pick Algorithm: DirectKeyValueJoin for this case.

@vdimir

vdimir commented May 20, 2026

Copy link
Copy Markdown
Member Author

Actually, I don't think we can simply ignore LowCardinality in the right storage, since not all storages handle it gracefully. While RDB does handle it correctly:

// Remove Nullable and LowCardinality wrappers for comparison
DataTypePtr key_type = removeNullable(recursiveRemoveLowCardinality(keys[i].type));
DataTypePtr primary_key_type = removeNullable(recursiveRemoveLowCardinality(primary_key_types[i]));

other storages do not. For example, KeeperMap returns incorrect results if we disable query_plan_use_new_logical_join_step.

So forcing a regular hash join in such cases (as happens with query_plan_use_new_logical_join_step = 1) may actually be desirable behavior until we are sure that all KV storages explicitly handle LowCardinality and Nullable types correctly.

#!/usr/bin/env -S ${HOME}/clickhouse client --queries-file


SET query_plan_use_new_logical_join_step = 0;

DROP TABLE IF EXISTS rdb_lc_pk;
DROP TABLE IF EXISTS t_str_right;

CREATE TABLE rdb_lc_pk (key LowCardinality(String), value String)
-- ENGINE = EmbeddedRocksDB PRIMARY KEY (key);
Engine = KeeperMap('/' || currentDatabase() || '/repro_km_getbykeys') PRIMARY KEY (key);

INSERT INTO rdb_lc_pk VALUES ('a', 'A'), ('b', 'B'), ('c', 'C');
CREATE TABLE t_str_right (k String) ENGINE = TinyLog;
INSERT INTO t_str_right VALUES ('a'), ('b'), ('c'), ('d');

SELECT count() FROM (
SELECT key, value FROM (SELECT k AS key FROM t_str_right) AS t
INNER JOIN rdb_lc_pk USING (key)
) WHERE ignore(*) = 0;

SELECT trim(explain) FROM (
    EXPLAIN actions = 1
    SELECT key, value FROM (SELECT k AS key FROM t_str_right) AS t
    INNER JOIN rdb_lc_pk USING (key)
)
WHERE explain LIKE '%Algorithm:%';


DROP TABLE IF EXISTS rdb_lc_pk;
DROP TABLE IF EXISTS t_str_right;

@vdimir vdimir closed this May 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant