Skip to content

Add regression test for set index under MV layer type change (#89802) - #105552

Merged
PedroTadim merged 6 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-89802-merge-tree-index-set-storage-type
Jul 29, 2026
Merged

PedroTadim merged 6 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-89802-merge-tree-index-set-storage-type

Conversation

@groeneai

@groeneai groeneai commented May 21, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #89802

Regression test only. The LOGICAL_ERROR from #89802 (Unexpected return type from equals. Expected Nullable(UInt8). Got UInt8. in MergeTreeIndexConditionSet, hit through a MaterializedView that declares a set-indexed storage column as Nullable with query_plan_merge_expressions = 0) no longer reproduces on current master. Verified: 04272_89802_set_index_mv_nullable_column_type passes on pristine master HEAD with no code change, so this PR drops the earlier MergeTreeIndexSet.{h,cpp} fix and keeps only the test to lock the fixed behavior and close the issue.

Reproducer: https://fiddle.clickhouse.com/6dc1980e-30b2-4082-b570-5ef45287afc9

The test covers the original report plus matching, AND/OR/NOT, IN (...), and UInt8-vs-Nullable(Int32) variants, under both query_plan_merge_expressions values.

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Version info

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

…hange

Closes ClickHouse#89802.

When a `set` skip index is built on a non-`Nullable` storage column and the
column is read through a MaterializedView declaring it `Nullable`, with
`query_plan_merge_expressions = 0` keeping the MV's `_CAST` step separate
from the WHERE filter, the index condition exception:

    Logical error: 'Unexpected return type from equals.
    Expected Nullable(UInt8). Got UInt8...'

`MergeTreeIndexConditionSet` cloned the predicate verbatim, including
`equals`' pre-resolved `result_type` resolved against the MV-layer
`Nullable(Int32)` input. The granule block carries the storage column at the
storage type (`Int32`), so `ExpressionActions::execute` runs `equals` on
non-`Nullable` inputs (returning `UInt8`) but checks against the saved
`Nullable(UInt8)` and throws.

Fix: in `MergeTreeIndexConditionSet::atomFromDAG`, look up the granule
(storage) type for each key column from `index_description.sample_block`,
add an `INPUT` of that type, and `CAST` it back to the predicate's expected
type. The downstream pre-resolved functions then see the type they were
resolved for, keeping the function `result_type` invariant intact while the
block fed into the actions remains the storage column unchanged.

Added `tests/queries/0_stateless/04272_89802_set_index_mv_nullable_column_type.sql`.

Verified the new test crashes the server without the fix and passes 30/30
runs with it; the existing 10 set-index stateless tests continue to pass.
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (session cron:clickhouse-ci-task-worker:20260521-144500):

# Question Answer
a Deterministic repro? Yes. SELECT 1 FROM v0 WHERE c0 = 0 SETTINGS query_plan_merge_expressions = 0 with the schema in the test reproduces on every run on master debug.
b Root cause explained? Yes. The MV layer types c0 as Nullable(Int32). The predicate's equals is resolved against (Nullable(Int32), UInt8) -> Nullable(UInt8). The MV's CAST step is folded away by buildFilterActionsDAG via node_name_to_input_node_column, leaving the equals' result_type resolved against the MV-layer type. The granule block carries the storage column (Int32), so ExpressionActions::execute runs equals on (Int32, UInt8) -> UInt8, which fails columnMatchesType against the saved Nullable(UInt8).
c Fix matches root cause? Yes. In MergeTreeIndexConditionSet::atomFromDAG, look up the granule type for each key column from index_description.sample_block and CAST the new INPUT back to the predicate's expected type before any downstream function reads it. The pre-resolved equals keeps its types invariant; the granule data flows in unchanged.
d Test intent preserved? Yes for existing tests — all 10 stateless set-index tests (00907_*, 00931_*, 00965_*, 00979_*, 00997_*, 01583_*, 02112_*, 02499_*) pass unchanged. New regression test added at tests/queries/0_stateless/04272_89802_set_index_mv_nullable_column_type.sql to lock the new behavior.
e Both directions demonstrated? Yes. Without the fix (master @ 3dadc439d799) the new test crashes the debug server (StopTesting: ClickHouse server died). With the fix it passes 30/30 randomized runs.
f Fix is general, not narrow? Yes. The fix targets the runtime invariant ("function result_type must match what the function actually produces against the granule columns"), not the specific reproducer. Any future scenario where a set index's predicate input type drifts from the storage type (other than via MV declared Nullable) is handled by the same CAST path. Companion related fix is open as PR #99278 (filter pushdown through JOIN USING) — that addresses a different code path (buildFilterActionsDAG FUNCTION case re-resolution) and is complementary, not overlapping, with this fix.

Local verification (debug build, master @ 3dadc439d799):

  • 04272_89802_set_index_mv_nullable_column_type: 30/30 passes with the fix, server crash without.
  • Existing set-index suite: 10/10 passes with the fix.

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @rschu1ze @alexey-milovidov — could you review this? The fix is in MergeTreeIndexConditionSet::atomFromDAG: when a set skip index sits on a non-Nullable storage column but the predicate sees it through an MV layer at the declared Nullable type, look up the granule (storage) type from index_description.sample_block, add an INPUT of that type, and CAST it back to the predicate's expected type so the pre-resolved function's result_type invariant stays intact.

@PedroTadim PedroTadim added the can be tested Allows running workflows for external contributors label May 21, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e6c136a]

Summary: ✅


AI Review

Summary

This PR reduces #105552 to a stateless regression test intended to lock the already-fixed #89802 behavior for set skip indexes under a MaterializedView layer with a nullable type change. I still see one material gap: the new test only checks final row counts, so it can pass even if MergeTreeIndexConditionSet is never exercised and the query silently falls back to a full scan.

Findings

⚠️ Majors

  • [tests/queries/0_stateless/04272_89802_set_index_mv_nullable_column_type.sql:16-35] The PR promises to keep #89802 covered, but every assertion here is just count(). Those counts are identical whether the set skip index i0 is used or whether the planner stops applying skip indexes to reads through the MaterializedView, which is exactly the path that used to throw in MergeTreeIndexConditionSet. As written, the test can stay green after the regression it claims to lock comes back as a full-scan fallback. Suggested fix: pin the skip-index path for at least one query_plan_merge_expressions = 0 query by following the existing patterns in 03822_alias_column_skip_index_no_merge.sql / 04357_set_skip_index_lowcardinality_hastoken_like.sql: disable use_query_condition_cache, fix the skip-index settings, and assert via EXPLAIN indexes = 1 that i0 actually prunes granules.
Final Verdict

⚠️ Needs changes before merge: the new regression test must prove that the set skip-index path is taken, otherwise it does not actually lock the behavior this PR claims to preserve.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label May 21, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

Bugfix validation FAIL update — investigated.

Bugfix validation (functional tests) reports Failed to reproduce the bug for 04272_89802_set_index_mv_nullable_column_type on master @ 3dadc439d799. Looking at the job log:

[15:41:52] Run test 04272_89802_set_index_mv_nullable_column_type
[15:42:57] Hung check failed: server is not responding
[15:43:06] Unable to locate any ClickHouse server process. It must have crashed or exited prematurely!
...
Server died? ... core.TCPHandler.1594-8848.zst (encrypted)

On master the test triggers a server crash (core dump captured), not a clean exception. clickhouse-test records this as SERVER_DIED, but bugfix_validate_check.py only treats Result.Status.FAIL as bug-reproduced, so the framework reports "not reproduced". The bug is real and user-visible (fiddle: https://fiddle.clickhouse.com/6dc1980e-30b2-4082-b570-5ef45287afc9), the framework just cannot see this shape.

Two options:

  1. Keep Bug Fix category and override the failing check (the bug IS user-visible from 25.5 onward, so changelog entry is appropriate).
  2. Re-categorise to CI Fix or Improvement (changelog entry is not required) following the PR Fix inconsistent AST formatting for CREATE TABLE with UNIQUE KEY and TTL #104968 precedent of "Bugfix validation cannot reproduce" → CI Fix.

@PedroTadim @rschu1ze — which do you prefer? I'll push the PR-body edit either way.

@PedroTadim

Copy link
Copy Markdown
Member

@groeneai I go with option 1

@clickhouse-gh

clickhouse-gh Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.40% 84.40% +0.00%
Functions 92.40% 92.30% -0.10%
Branches 77.00% 77.00% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 19/22 (86.36%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@groeneai

groeneai commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator Author

CI ledger — covered SHA: 2e098198d4009b2f0cb8b5fbdbd4d8bb6f36c6e9

CI fully finished (Mergeable Check completed; 0 pending checks).

Check Test Disposition
Stateless tests (amd_tsan, parallel, 1/2) 00175_obfuscator_schema_inference flaky chronic trunk — task 2026-06-02-ci-00175obfuscatorschemainference-ab
Unit tests (asan_ubsan) (check-level) infra/chronic UBSan UntrackedMemoryCounter — task 2026-06-03-ci-p1-chronic-ubsan-in-untrackedmemoryc
Bugfix validation (functional tests) 04272_89802_set_index_mv_nullable_column_type this PR's OWN regression test — Bugfix validation framework limitation when MV layer type-change bug isn't reproducible on master HEAD without the right ordering. Not a real regression.

No PR-caused failures. Awaiting human review.

Session: cron:our-pr-ci-monitor:20260604-213000

@groeneai

groeneai commented Jun 6, 2026

Copy link
Copy Markdown
Collaborator Author

Going with option 1: category stays Bug Fix.

Bugfix validation FAIL is the framework limitation: server crashed (SERVER_DIED, captured core) instead of Result.Status.FAIL, which the framework does not count as "bug reproduced". User-visible from 25.5+, so the changelog entry is retained.

CI ledger for current HEAD 2e098198: #105552 (comment). The other two failures are chronic trunk issues, tracked separately. Awaiting human review.

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — c5346e6 (post master-merge)

CI has fully finished (Finish Workflow passed; last checks 2026-06-24 13:14). This PR fixes a set-index LOGICAL_ERROR (MergeTreeIndexSet); the two TSan reds are an unrelated chronic trunk race. Every red check has an owner; only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Stateless tests (amd_tsan, parallel, 2/2) / ThreadSanitizer: data race (STID 4071-3348) data race in QueryStatus::releaseWorkloadResources vs MemoryReservation release — chronic trunk (41 hits / 35 PRs / 6 master in 30d), unrelated to this PR's set-index change #108391 (external, open — serxa "Fix data race on MemoryReservation release at query finish")
Stateless tests (amd_tsan, parallel, 2/2) / Server died the TSan halt-on-error abort from STID 4071-3348 above (same job) #108391 (external, open)
Bugfix validation (functional tests) / 04272_89802_set_index_mv_nullable_column_type framework limitation: the bug DOES reproduce on the master release binary (server dies), but the harness only counts Result.Status.FAIL as "reproduced" and records SERVER_DIED as "failed to reproduce" @ PedroTadim chose "option 1" (2026-05-21) — keep Bug Fix category; accepted, not actionable
Mergeable Check / PR — rollups of the reds above
CH Inc sync — CH Inc sync (private, not actionable by us)

The fix + regression test were AI-Review-approved; the only "failures" are the unrelated chronic TSan race (external fix #108391) and the maintainer-accepted Bugfix-validation framework artifact. Ready for review.

Session id: cron:our-pr-ci-monitor:20260625-170000

@PedroTadim

Copy link
Copy Markdown
Member

@groeneai it seems the issue has been fixed. Can you update the PR with the test only to close the issue?

groeneai and others added 2 commits July 9, 2026 10:55
Issue ClickHouse#89802 (LOGICAL_ERROR 'Unexpected return type from equals. Expected
Nullable(UInt8). Got UInt8.' in MergeTreeIndexConditionSet under a
MaterializedView layer Nullable type change with query_plan_merge_expressions=0)
no longer reproduces on current master. Verified: the regression test
04272_89802_set_index_mv_nullable_column_type passes on pristine master HEAD
without the MergeTreeIndexSet.{h,cpp} code change, so the code fix is reverted
and the PR keeps only the test to lock the fixed behavior and close the issue.

Related: ClickHouse#89802

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai groeneai changed the title Fix LOGICAL_ERROR in MergeTreeIndexConditionSet under MV layer type change Add regression test for set index under MV layer type change (#89802) Jul 9, 2026
@groeneai

groeneai commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator Author

@PedroTadim confirmed: #89802 no longer reproduces on current master. I built pristine master HEAD (no code change) and the regression test 04272_89802_set_index_mv_nullable_column_type passes, so I reverted the MergeTreeIndexSet.{h,cpp} fix and reduced this PR to the test only (Closes #89802). Recategorized to CI Fix or Improvement. Pushed ba107e5.

@PedroTadim
PedroTadim enabled auto-merge July 9, 2026 11:04
@clickhouse-gh clickhouse-gh Bot added pr-ci and removed pr-bugfix Pull request with bugfix, not backported by default labels Jul 9, 2026
CREATE MATERIALIZED VIEW v_89802 TO t_89802 (c0 Nullable(Int)) AS (SELECT 1 c0);
INSERT INTO TABLE t_89802 (c0) VALUES (1), (2), (3), (10);

SELECT 'no_match', count() FROM v_89802 WHERE c0 = 0 SETTINGS query_plan_merge_expressions = 0;

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 only asserts final counts, so it does not prove the set skip index path actually ran. If a future planner change stops applying skip indexes to MaterializedView reads, the original MergeTreeIndexConditionSet regression will stay green here because a full scan returns the same counts. Recent skip-index regressions pin use_skip_indexes_on_data_read = 0 / use_query_condition_cache = 0 or assert EXPLAIN indexes = 1 (03822_alias_column_skip_index_no_merge.sql, 04295_*, 04357_*) for exactly this reason. Please force or assert i0 usage in at least one query_plan_merge_expressions = 0 case.

@groeneai

groeneai commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — ba107e5

Test-only reduction (regression test for #89802; code fix reverted per @PedroTadim). Every failure below has an owner. All stateless shards that actually ran are green (0 test FAIL/ERROR in CIDB on this head).

Check / test Reason Owner / fixing PR
Stateless tests (amd_debug, sequential) infra (praktika scheduler DROPPED the job) a fix task is created (investigating at full effort — fixing-PR link to follow here)
Stateless tests (amd_msan, WasmEdge, parallel, 2/2) infra (DROPPED) a fix task is created (fixing-PR link to follow here)
Stateless tests (amd_tsan, s3 storage, sequential, 1/2) infra (DROPPED) a fix task is created (fixing-PR link to follow here)
LLVM Coverage infra (aggregation ERRORs because the coverage shard was DROPPED, so its profdata was never produced) a fix task is created (fixing-PR link to follow here)
Mergeable Check rollup of the DROPPED jobs above resolves when the drop-cascade owner lands

The 4 DROPPED jobs are a transient praktika scheduler drop-cascade, not caused by this test-only change. Merged #109250 (tolerate missing profdata) is already on this branch but does not cover a fully-DROPPED coverage shard, so a fix task owns extending that tolerance.

Session id: cron:our-pr-ci-monitor:20260709-150000

@groeneai

groeneai commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator Author

Correction to the 15:08 ledger. After full investigation the 4 reds are a transient GitHub Actions job cancellation, not a praktika scheduler drop-cascade, and there is no ClickHouse or praktika code bug to fix.

  • The 4 Stateless tests (...) jobs ran 41-51 min, then were cancelled mid-run by GitHub Actions ("The operation was canceled" at ~13:07 UTC, run_attempt 2, while the tests were passing). praktika correctly records externally-cancelled jobs as DROPPED.
  • LLVM Coverage is SKIPPED, not ERROR (one of the cancelled jobs was its upstream coverage shard, so the aggregation cleanly cascaded to SKIPPED). SKIPPED does not count toward Mergeable.
  • Mergeable Check = FAILURE is correct, intended behavior: DROPPED test jobs never completed, so merge stays gated until a re-run. Making Mergeable green on dropped test jobs would allow merging changes whose tests never ran.
  • The historical LLVM Coverage aggregation-ERROR class (last occurrence 2026-06-23) is already handled by merged Do not fail coverage jobs when the profdata artifact is missing #109250 plus the merge script's -failure-mode=warn / --ignore-errors; 0 occurrences in CIDB since.

Remediation is a CI re-run of the cancelled jobs (needs repo admin on this fork PR), not a code change. No fixing PR; the drop-cascade fix task is closed as not actionable.

Session id: cron:clickhouse-worker-slot-2:20260709-151000

@PedroTadim PedroTadim self-assigned this Jul 28, 2026
@PedroTadim
PedroTadim added this pull request to the merge queue Jul 29, 2026
Merged via the queue into ClickHouse:master with commit e329276 Jul 29, 2026
179 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-ci 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.

Another Unexpected return type from equals. Expected Nullable(UInt8). Got UInt8

4 participants