Skip to content

Detect Object/JSON in join runtime filter null-capable check - #107651

Closed
groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:fix-runtime-filter-json-object-key
Closed

groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:fix-runtime-filter-json-object-key

Conversation

@groeneai

Copy link
Copy Markdown
Collaborator

Closes: #107646
Related: #106931

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix silently dropped rows for an equi-join on a JSON/Object key (or a JSON nested in Tuple/Array/Map) when the right side has a single distinct value and enable_join_runtime_filters = 1. The join runtime filter treated a JSON-containing key as null-free and chose the single-element "equals" path; equals over a JSON value returns Nullable(UInt8), so the matching row was dropped. JSON/Object is now detected as null-capable, forcing the null-safe lookup path.

Description

Follow-up to #106931 (Closes #101415), which generalized the runtime-filter null-capable type check to hasTypeThatCanContainNulls() and was found, in review of that PR, to still miss Object/JSON.

hasTypeThatCanContainNulls() enumerates null-capable types via canContainNull() (Nullable / LowCardinality(Nullable) / Variant / Dynamic) and recurses into Array/Tuple/Map. canContainNull() omits Object/JSON, whose paths are dynamically typed and can be NULL like Variant/Dynamic. So a JSON-containing join key was reported null-free and routed to the single-element "equals" runtime-filter path (RuntimeFilterLookup.h), which produces Nullable(UInt8) for a JSON argument and silently drops the matching left row.

Repro on master (returns 0; with enable_join_runtime_filters = 0 returns 1):

SET enable_analyzer = 1, allow_experimental_json_type = 1;
SELECT count() FROM (SELECT '{"a":1}'::JSON AS k) AS t1
JOIN (SELECT '{"a":1}'::JSON AS k) AS t2 USING (k)
SETTINGS enable_join_runtime_filters = 1;

Fix: add isObject() to hasTypeThatCanContainNulls() so JSON-containing keys take the null-safe Set path. The helper has a single caller (the join runtime filter); canContainNull() is left unchanged because it has many other callers (CAST, comparison, MergeTree key conditions). The existing Array/Tuple/Map recursion covers nested JSON.

Regression coverage added to 04327_runtime_filter_nested_variant (top-level JSON key, Tuple(JSON), Array(JSON), Map(String, JSON) value, Map(JSON, String) key, plus LEFT ANTI JOIN runtime-filters off-vs-on self-validation). The inner-join cases return no rows without the fix and the expected row with it.

hasTypeThatCanContainNulls() enumerates null-capable types via
canContainNull() (Nullable / LowCardinality(Nullable) / Variant / Dynamic)
and recurses into Array/Tuple/Map. canContainNull() omits Object/JSON, whose
paths are dynamically typed and can be NULL like Variant/Dynamic, so a
JSON-containing join key was reported null-free and routed to the
single-element "equals" runtime-filter path. equals over a JSON value returns
Nullable(UInt8), so a matching left row was silently dropped: an equi-join on
a JSON (or nested Tuple/Array/Map JSON) key with one distinct right-side value
returned no rows under the default enable_join_runtime_filters=1, while the
same query with the filter disabled returned the matching row.

Add isObject() to hasTypeThatCanContainNulls() so JSON-containing keys take
the null-safe Set path. The helper has a single caller (the join runtime
filter); canContainNull() is left unchanged (it has many other callers).
The existing Array/Tuple/Map recursion covers nested JSON.

Closes: ClickHouse#107646

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

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? Yes. On master HEAD: SET enable_analyzer=1, allow_experimental_json_type=1; SELECT count() FROM (SELECT '{"a":1}'::JSON AS k) t1 JOIN (SELECT '{"a":1}'::JSON AS k) t2 USING (k) SETTINGS enable_join_runtime_filters=1; returns 0 every time; the same query with enable_join_runtime_filters=0 returns 1. Also deterministic for Tuple(JSON), Array(JSON), Map(String,JSON) value, and Map(JSON,String) key.
b Root cause explained? RuntimeFilterBase sets argument_can_have_nulls = hasTypeThatCanContainNulls(key_type). That helper checks canContainNull() (= Nullable/LowCardinality(Nullable)/Variant/Dynamic) and recurses into Array/Tuple/Map. canContainNull() omits Object/JSON, so a JSON key reads as null-free, argument_can_have_nulls=false, and finishInsertImpl() picks the single-element "equals" path. equals over a JSON value yields Nullable(UInt8), so the matching left row is dropped before the join.
c Fix matches root cause? Yes. Add isObject(*type) to hasTypeThatCanContainNulls() so JSON-containing keys are reported null-capable and take the null-safe Set path. This targets the exact predicate that mis-classified the key. canContainNull() is intentionally NOT touched (it has ~30 other callers: CAST, comparison, MergeTree key conditions); hasTypeThatCanContainNulls() has a single caller (the join runtime filter), so the change is scoped to where the bug lives.
d Test intent preserved / new tests added? New regression added to 04327_runtime_filter_nested_variant: top-level JSON key, Tuple(JSON), Array(JSON), Map(String,JSON) value, Map(JSON,String) key (exercises the helper's Map-key recursion), plus two LEFT ANTI JOIN runtime-filters off-vs-on self-validation cases. Existing Variant/Dynamic/LC(Nullable) coverage unchanged.
e Both directions demonstrated? Yes. Built the unfixed binary (Build ID 9dd0b6d3...): the new JSON inner-join cases return no rows, test FAILS (diff shows the 5 equals-path rows missing). Built the fixed binary (Build ID 011cfae4...): test PASSES, all JSON cases return the matching row, off==on for LEFT ANTI.
f Fix is general, not a narrow patch? Yes. The fix is at the source (the type-classification helper), not a guard at the crash/drop site. The existing Array/Tuple/Map recursion in the helper means nested JSON (Tuple(JSON), Array(JSON), Map key/value) is covered by the single isObject addition; verified for all of those shapes. This completes the dynamically-typed family (Variant/Dynamic were already covered by #106931; Object was the remaining gap).

Original #101415 crash repro (Tuple(Variant) single distinct value) still returns the row with no crash. Related suite 03709_anti_join_runtime_filters, 03580_join_runtime_filter_{column_type,prewhere,pushdown} all pass.

Session id: cron:clickhouse-worker-slot-3:20260616-172500

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @Avogar — could you review this? It is the follow-up to #106931: hasTypeThatCanContainNulls() missed Object/JSON, so a JSON join key with one distinct right-side value took the single-element "equals" runtime-filter path and silently dropped the matching row under the default enable_join_runtime_filters=1. The fix adds isObject() to that helper (single caller; canContainNull() left untouched), and the existing Array/Tuple/Map recursion covers nested JSON.

@@ -33,7 +33,8 @@ bool hasNullable(const DataTypePtr & type)

bool hasTypeThatCanContainNulls(const DataTypePtr & type)
{
if (canContainNull(*type))
// canContainNull omits Object/JSON, but its dynamically typed paths can be NULL like Variant/Dynamic.
if (canContainNull(*type) || isObject(*type))

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.

Why? JSON cannot contain NULLs. If JOIN key would be JSON key, it would be Dynamic or any other type if it has type hint

@Avogar

Avogar commented Jun 16, 2026

Copy link
Copy Markdown
Member

This is wrong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants