Skip to content

Fix Bad cast on data-lake SETTINGS disk = '...' with wrapped object storage - #104258

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-89300-bad-cast-wrapped-object-storage
May 6, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-89300-bad-cast-wrapped-object-storage

Conversation

@groeneai

@groeneai groeneai commented May 6, 2026

Copy link
Copy Markdown
Collaborator

Fixes #89300.

Motivation

A user reported via #89300 that

SELECT 1 FROM icebergS3(s3, format = 'Parquet', filename = '...', SETTINGS disk = 'disk0');

against a disk configured with metadata_type = plain aborted with
Logical error: 'Bad cast from type DB::PlainObjectStorage<DB::S3ObjectStorage> to DB::S3ObjectStorage'.

The PlainObjectStorage<> decorator was deleted by #90580, so the
exact crash from the original report is gone, but the underlying bug
pattern remains: S3StorageParsedArguments::fromDisk (and the Azure
sibling) call assert_cast<const S3ObjectStorage &>(*disk->getObjectStorage())
directly. assert_cast performs an exact typeid match in
debug/sanitizer builds, so any decorator that wraps the underlying
storage instead of being it (most importantly CachedObjectStorage,
returned by DiskObjectStorage::wrapWithCache) still crashes argument
parsing with Logical error: 'Bad cast from type DB::CachedObjectStorage to DB::S3ObjectStorage'.

I confirmed this is currently reachable in trunk:

  • The BuzzHouse fuzzer hit it on PR Introducing a new fuzzer check in the ClickHouse CI for my dear friend #94148 with
    CREATE TABLE d1.t28 ENGINE = DeltaLakeS3(s3, filename = 't28', format = 'Parquet') SETTINGS disk = 'disk1', iceberg_format_version = 3.
  • Reproduced locally: a debug ClickHouse with a cache-typed disk over
    s3 and SETTINGS disk = '<cache_disk>' aborts the server in the
    exact same way.

Approach

IObjectStorage already exposes getUnderlying() which returns the
inner storage for decorator types and nullptr otherwise. Walk that
chain before the cast in both S3StorageParsedArguments::fromDisk and
AzureStorageParsedArguments::fromDisk. After unwrapping, the cast
sees the concrete S3ObjectStorage / AzureObjectStorage and
succeeds.

Validation

Pre-PR validation gate:

  • Deterministic repro? Yes — confirmed locally with a debug build:
    SELECT 1 FROM icebergS3('no_such_table', 'Parquet', 'a Int',
                            SETTINGS disk = 's3_cache_test')
    
    on master HEAD aborts with
    Logical error: 'Bad cast from type DB::CachedObjectStorage to DB::S3ObjectStorage'
    and kills the server.
  • Root cause explained? Yes — assert_cast in
    DEBUG_OR_SANITIZER_BUILD does a strict typeid(from) == typeid(To)
    check (see comment at src/Common/assert_cast.h:19). CachedObjectStorage
    inherits only from IObjectStorage, not from S3ObjectStorage, so
    the equality fails and the helper throws LOGICAL_ERROR. In release
    builds the underlying static_cast is undefined behaviour because
    there is no inheritance relationship at all.
  • Fix matches root cause? Yes — unwrap to the concrete storage so
    the typeid matches. No assert_cast semantics are weakened.
  • Test intent preserved? N/A; this PR adds a new regression test.
  • Both directions demonstrated? Yes — same query on the fixed
    binary returns FILE_DOESNT_EXIST (as expected for a missing iceberg
    table) instead of crashing. Verified for s3_disk (raw),
    s3_cache (cached), and s3_plain_disk (plain metadata).
  • Fix is general? Yes — applied to both the S3 and Azure parsers,
    and uses the existing getUnderlying() virtual so any future
    decorator added to IObjectStorage is handled automatically.

The new stateless test
tests/queries/0_stateless/04208_iceberg_disk_with_cache_no_bad_cast.sql
exercises all three disk shapes (raw, cached, plain metadata) and
asserts that the query fails with an S3/iceberg-level error rather
than a LOGICAL_ERROR. The existing test config is extended with a
narrow allowed_disks_for_table_engines so the data-lake disk gate
lets the queries through to the real check.

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 a logical error (Bad cast from type DB::CachedObjectStorage to DB::S3ObjectStorage) that aborted argument parsing of data-lake table functions and engines (icebergS3, deltaLakeS3, etc.) when called with SETTINGS disk = '...' against a disk whose underlying object storage is wrapped by a decorator such as a filesystem cache. Closes #89300.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

… storage

`StorageS3Configuration::fromDisk` (and the Azure parallel) called
`assert_cast<const S3ObjectStorage &>(*disk->getObjectStorage())` directly.
`assert_cast` checks `typeid` exactly, so any decorator that wraps the
underlying `S3ObjectStorage` — most importantly `CachedObjectStorage` for
disks created with `wrapWithCache(...)` — caused
`Logical error: 'Bad cast from type DB::CachedObjectStorage to DB::S3ObjectStorage'`
during argument parsing.

Historically this also surfaced as
`Bad cast from type DB::PlainObjectStorage<DB::S3ObjectStorage> to DB::S3ObjectStorage`
(reported in ClickHouse#89300). The `PlainObjectStorage<>` decorator was removed in
ClickHouse#90580, but the same pattern still bites for `CachedObjectStorage`. CIDB
shows the cached-disk variant being hit by the BuzzHouse fuzzer (e.g. a
`CREATE TABLE ... ENGINE = DeltaLakeS3(...) SETTINGS disk = '...'` query
on PR ClickHouse#94148) and by `test_storage_iceberg_disks` runs.

Use the existing `IObjectStorage::getUnderlying()` virtual to walk past
any decorator layers before the cast. Apply the same fix to the Azure
path which has the identical pattern.

Add a stateless regression test that exercises `icebergS3` with three
disk shapes (`s3_disk` raw, `s3_cache` wrapped, `s3_plain_disk` plain
metadata). The test does not need a real iceberg table — it asserts that
argument parsing no longer throws a logical error and the query fails
with an expected S3/iceberg error code instead. The CI test config now
allows-lists those four disks for use with table engines so the data
lake disk-validation gate lets the queries through to the real check.

Closes ClickHouse#89300
@groeneai

groeneai commented May 6, 2026

Copy link
Copy Markdown
Collaborator Author

cc @kssenii @Michicosun @PedroTadim — could you review this? The fix unwraps decorator object storages (notably CachedObjectStorage) before the assert_cast<const S3ObjectStorage&> in S3StorageParsedArguments::fromDisk (and the Azure parallel). Without the unwrap, calling a data-lake table function with SETTINGS disk = '<cached_disk>' aborted argument parsing with Logical error: 'Bad cast from type DB::CachedObjectStorage to DB::S3ObjectStorage' — the same bug shape as #89300 (PedroTadim's report) plus the BuzzHouse fuzzer hit on PR #94148. Reproduced and verified locally in both directions: master HEAD aborts the server, fixed binary returns FILE_DOESNT_EXIST. New stateless test 04208 covers raw / cached / plain-metadata disks.

@clickhouse-gh

clickhouse-gh Bot commented May 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c39f742]

Summary: ✅


AI Review

Summary

This PR fixes argument parsing for data-lake table functions/engines when SETTINGS disk = '...' points to object-storage disks wrapped by decorators (for example, cache layers): both S3 and Azure paths now unwrap via IObjectStorage::getUnderlying before assert_cast, and a stateless regression test was added. High-level verdict: the fix is targeted, symmetric across S3/Azure, and the test covers wrapped + raw S3 disk shapes without introducing new correctness or compatibility risks.

Missing context
  • ⚠️ CI is still in progress (several checks pending), so final runtime signal is not yet available.
ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ✅ Approve

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

@alexey-milovidov alexey-milovidov left a comment

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.

Okay...

@alexey-milovidov alexey-milovidov self-assigned this May 6, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.60% 76.60% +0.00%

Changed lines: 50.00% (6/12) · Uncovered code

Full report · Diff report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue May 6, 2026
Merged via the queue into ClickHouse:master with commit 3631bd3 May 6, 2026
165 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label May 7, 2026
Algunenano pushed a commit to Algunenano/ClickHouse that referenced this pull request May 7, 2026
The stress-test log scanner (`ci/jobs/scripts/log_parser.py`) uses
`rg --text -A 10 -o 'Logical error.*'` against the server log. Test
`04208_iceberg_disk_with_cache_no_bad_cast.sql` previously included
the literal exception string in its SQL comments. When such a query
fails (it is expected to fail at table-resolution time), the full
query — including those comments — is echoed into the
`<Error> executeQuery` log entry. If a later, completely unrelated
fatal error occurs in the same stress-test run, the scanner picks up
the SQL-comment occurrence first and uses it as the failure name,
even though the actual stack trace (and `STID`) belong to a different
bug.

Three master/PR runs on 2026-05-07 hit this false positive:

- `Stress test (experimental, serverfuzz, arm_tsan)` on master
  (commit 69167ef) — actual abort was a `chassert(false)` in
  `paranoidCheckForCoveredPartsInZooKeeperOnStart`,
  reported as `STID 2508-5dc3`.
- `Stress test (arm_asan_ubsan)` on PR ClickHouse#100377 — actual abort was
  `Not-ready Set is passed as the second argument for function 'in'`
  in `FunctionIn::executeImpl`, reported as `STID 0250-4e52`.
- `Stress test (amd_msan)` on PR ClickHouse#93037 — same `Not-ready Set`
  family, reported as `STID 0250-41a5`.

The original `Bad cast from CachedObjectStorage to S3ObjectStorage`
bug that this test guards against is genuinely fixed by ClickHouse#104258 — the
unwrap loop in `S3StorageParsedArguments::fromDisk` is in place and
none of these three runs actually crashed in that code path.

This commit reworks the comments in 04208 so they describe the bug
without containing the verbatim `Logical error: '...'` pattern that
the scanner greps for, and adds a `NOTE` explaining why the verbatim
text is intentionally avoided. The SQL queries and expected
`serverError` codes are unchanged.

CI report (master arm_tsan):
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=69167ef25d21358fd8f171e593849ea73195dd1b&name_0=MasterCI&name_1=Stress%20test%20%28experimental%2C%20serverfuzz%2C%20arm_tsan%29

Co-Authored-By: Claude Opus 4.7 <[email protected]>
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-bugfix Pull request with bugfix, not backported by default 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.

Logical error: 'Bad cast from type DB::PlainObjectStorage<DB::S3ObjectStorage> to DB::S3ObjectStorage'

3 participants