Skip to content

Type IN (subquery) over LowCardinality the same as IN (literal list) - #114229

Merged
davenger merged 2 commits into
masterfrom
in-subquery-lowcardinality-type
Aug 11, 2026
Merged

davenger merged 2 commits into
masterfrom
in-subquery-lowcardinality-type

Conversation

@davenger

@davenger davenger commented Aug 10, 2026 •

Copy link
Copy Markdown
Member

Related: #113826

Follow-up to the review of the ActionsDAG type-check relaxation in the PR above: revert the relaxation and fix the root cause outside of ActionsDAG instead.

The result type of IN over a LowCardinality argument used to depend on the form of the right-hand side: with a literal list it was LowCardinality(UInt8), with a subquery it was plain UInt8. The general typing rule wraps the result in LowCardinality only when all other arguments are constants, and during analysis the set from a subquery is not built yet and has no column, so it did not count as a constant — while a set is always a constant column in the plan (ColumnSet is always wrapped in ColumnConst). A worker deserializing a distributed plan rebuilt the function with the constant set, got the wrapped type, and the type check failed; the previous fix accepted the difference in the check.

The typing rule now counts a Set-typed argument as a constant. x IN (subquery) over a LowCardinality column returns LowCardinality(UInt8), the same as x IN (literal list), in both analyzers, and the serialized and rebuilt types agree, so the strict type check in ActionsDAG::deserialize is restored exactly as before the PR above. A mixed-version window exists only between that PR and this one (both master-only, no stable release in between, and the affected path is the experimental make_distributed_plan): a not-yet-upgraded initiator sends the unwrapped type and an upgraded worker rejects the plan with a clear INCORRECT_DATA error.

The visible type change: toTypeName(x IN (subquery)), EXPLAIN headers, and column types of CREATE ... AS SELECT over such expressions now show LowCardinality(UInt8) instead of UInt8. The values do not change. A sweep of the stateless suite found no existing test that observes the type. Execution of IN over LowCardinality with a subquery also becomes cheaper: it now takes the same probe-per-distinct-value path as literal lists.

Changelog category (leave one):

  • Improvement

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

x IN (subquery) over a LowCardinality column now returns LowCardinality(UInt8) — the same type as x IN (literal list). Previously the two forms returned different types, and distributed plans with such an IN needed a special case in the plan type check.

Version info

  • Merged into: 26.8.1.1173 (included in 26.8 and later)

@clickhouse-gh

clickhouse-gh Bot commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [853a919]

Summary: ✅


AI Review

Summary

This PR makes IN (subquery) over a LowCardinality left operand resolve to the same LowCardinality(UInt8) result type as IN (literal list), and removes the temporary ActionsDAG::deserialize type-mismatch exception that was compensating for the analyzer-only type gap. After reviewing the current diff, the touched code paths, the existing PR discussion state, and the all-green CI run, I did not find a correctness, compatibility, or test-coverage issue that merits an inline finding.

Final Verdict

✅ No findings.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.70% 86.70% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 79.10% 79.00% -0.10%

Changed lines: Changed C/C++ lines covered: 8/13 (61.54%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Aug 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 853a91930 with master c462bd702 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 691.29 MiB 688.29 MiB -3.00 MiB (-0.43%)

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 units

8 translation units recompiled, 28 s compile time in total, 8 of them have a recent master baseline.

Job report

@KochetovNicolai KochetovNicolai self-assigned this Aug 11, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 37 queries analysed

This is a correctness fix: it makes IN over a LowCardinality argument resolve the same result type whether the right-hand side is a literal list or a subquery set, and relaxes the matching distributed-plan type check. The three flagged ClickBench queries (Q16, Q17, Q18) are all plain GROUP BY aggregations with no IN or subquery, so they never touch the changed code path. The two flagged improvements (Q16 -11.9%, Q17 -16.2%) are therefore downgraded to not_sure as off-path variance, and the Q18 -6.6% suppression to no_change is correct. No real PR-attributable performance change is present in this benchmark set.

clickbench

⚠️ 2 inconclusive

Flagged queries (2 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 16 not_sure 793 698 -11.9% <0.0001 aggregation: Q16 is a GROUP BY UserID/SearchPhrase aggregation; the PR only changes IN-over-LowCardinality type resolution, a path this query never executes, so the -11.9% is run-to-run variance.
⚠️ 17 not_sure 556 466 -16.2% <0.0001 aggregation: Q17 is the same GROUP BY aggregation family with no IN/subquery; nothing in this PR touches its hot path, so the -16.2% cannot be attributed to the change and reads as variance.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 5c19d921-b6da-455c-895b-98659bff7a0b
  • MIRAI run: 78c9454e-8804-4a07-ac61-9bb86a4605d5
  • PR check IDs:
    • clickbench_723446_1786447014
    • clickbench_723452_1786447014
    • clickbench_723463_1786447014
    • tpch_adapted_1_official_723475_1786447014
    • tpch_adapted_1_official_723490_1786447014
    • tpch_adapted_1_official_723518_1786447014

@davenger
davenger added this pull request to the merge queue Aug 11, 2026
Merged via the queue into master with commit b6eb951 Aug 11, 2026
354 of 356 checks passed
@davenger
davenger deleted the in-subquery-lowcardinality-type branch August 11, 2026 15:23
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 11, 2026
pull Bot pushed a commit to SINHASantos/ClickHouse that referenced this pull request Aug 23, 2026
…d analyzer

The old analyzer replaces `in` with `inIgnoreSet` while it only needs the
types, passing the left operand as the stand-in for the set that is not built
yet. The general typing rule wraps the result in `LowCardinality` only when
every other argument is a constant, so two full `LowCardinality` arguments
typed the expression as plain `UInt8`, while executing the real `in` against
the constant `Set` column yields `LowCardinality(UInt8)`.

A query reading such a column across a subquery boundary then recorded
`UInt8` in the outer `ActionsDAG` and got `LowCardinality(UInt8)` in the
header, failing the type check in `ActionsDAG::updateHeader`:

    SET enable_analyzer = 0;
    SELECT tuple(*) FROM (SELECT s, s IN (SELECT 'a') AS f FROM t);
    -- Unexpected return type from tuple. Expected Tuple(LowCardinality(String), UInt8).
    --          Got Tuple(LowCardinality(String), LowCardinality(UInt8)). (LOGICAL_ERROR)

Pass a constant `UInt8` as the stand-in instead. It is indistinguishable from
the real `Set` argument in every dimension the typing rule inspects, so
analysis and execution agree by construction for all eight `in` variants and
every operand shape. A `Set`-typed stand-in would mirror execution more
literally, but the window-definition expressions built by
`getRootActionsNoMakeSet` end up in an executed plan, and a `ColumnSet` with
no set behind it would be dereferenced when such a plan is serialized.

`allow_experimental_analyzer = 1` was fixed for the same root cause in
ClickHouse#114229, whose test is tagged
`no-old-analyzer`; the divergence appeared when `ColumnSet` became always
constant in ClickHouse#100226.
alexey-milovidov added a commit that referenced this pull request Sep 13, 2026
…here

`04894_in_subquery_lowcardinality_type_old_analyzer` fails on this
branch only in its new-analyzer half:

     -- the analyzer
    -LowCardinality(UInt8)	LowCardinality(UInt8)
    -LowCardinality(UInt8)
    +LowCardinality(UInt8)	UInt8
    +UInt8

The old-analyzer half - what this backport fixes - passes. Under
`enable_analyzer = 1` this branch still types `s IN (SELECT 'a')` as
plain `UInt8` while `s IN ('a')` is `LowCardinality(UInt8)`: for a
subquery set the argument is `DataTypeSet` with no column yet, so
`IFunctionOverloadResolver::getReturnType` counts it as a full column
and suppresses the `LowCardinality` wrap. Making the two forms agree
is #114229, which is labelled `pr-improvement` and therefore not a
candidate for a release branch.

So the two `toTypeName` controls are dropped from the analyzer block
rather than re-pinned to this branch's output, which would write a
known inconsistency into the reference. The value assertions stay -
`tuple(*)`, `arrayFilter` and the two `count()`s are the ones that
used to fail `ActionsDAG::updateHeader` with `Unexpected return type
from tuple`, and they pass here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants