Skip to content

Fix inconsistent columns on exception (i.e. MEMORY_LIMIT_EXCEEDED) during parsing - #106802

Merged
azat merged 1 commit into
ClickHouse:masterfrom
azat:fix-columns-insert-exception-safety
Jun 9, 2026
Merged

azat merged 1 commit into
ClickHouse:masterfrom
azat:fix-columns-insert-exception-safety

Conversation

@azat

@azat azat commented Jun 9, 2026 •

Copy link
Copy Markdown
Member

Linked issues

Composite columns grow several nested structures in sequence inside insertDefault with no rollback. If one of them throws (for example on a memory limit 1) the already-grown structures are left ahead of the rest, so the column's nested structures end up with different sizes.

This is reachable during deserialization default-filling: a missing named-tuple element (input_format_json_defaults_for_missing_elements_in_named_tuple) or a null with input_format_null_as_default calls insertDefault on a nested column. When an allocation throws in the middle, the column is left desynchronized; the exception then unwinds into a serialization-level restore (SerializationTuple::addElementSafe and friends) which calls popBack to undo the partial row, and popBack over-pops the shorter nested structure:

Cannot pop 1 rows from String: there are only 0 rows

Per type:

  • Tuple: size is columns[0]->size and columns[0] grows first, so any later element that throws leaves size ahead - roll back the grown elements.
  • Nullable: the nested column grows before the null map - roll back the nested column if appending to the null map throws.
  • Variant: local_discriminators grows before offsets - roll it back if appending to offsets throws.
  • Object: typed/dynamic paths and shared_data grow in sequence - roll each back to the previous size.

This should be sufficient (i.e. we do not need to fix IColumn::insert() and others) because usually if some method of IColumn throws, we discard the column, there are only few exceptions:

  • checkpoint-based rollback (getCheckpoint/rollback) - should be safe already
  • manual handling of parsing errors and reverting the state via popBack() - usually safe, but not safe when filling defaults via insertDefault() -- this patch

Note insertManyDefaults is not on this path (parsing fills defaults one row at a time via insertDefault), but the Variant/Object overrides were made exception-safe too, since they do a bulk fill and may be reused elsewhere.

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 inconsistent columns (that leads to LOGICAL_ERROR later) on exception (i.e. MEMORY_LIMIT_EXCEEDED) during parsing

Version info

  • Merged into: 26.6.1.578
  • Backported to: 26.5.3.19, 26.4.5.11, 26.3.14.11

…ring parsing

Composite columns grow several nested structures in sequence inside
`insertDefault` with no rollback. If one of them throws (for example on a
memory limit [1]) the already-grown structures are left ahead of the
rest, so the column's nested structures end up with different sizes.

  [1]: https://pastila.nl/?0005eb6b/e1cd0ca11139b61c65264e368f8e6150#BjWwSA/197t3/SuuGCsqdg==GCM

This is reachable during deserialization default-filling: a missing
named-tuple element (`input_format_json_defaults_for_missing_elements_in_named_tuple`)
or a null with `input_format_null_as_default` calls `insertDefault` on a nested
column. When an allocation throws in the middle, the column is left
desynchronized; the exception then unwinds into a serialization-level restore
(`SerializationTuple::addElementSafe` and friends) which calls `popBack` to
undo the partial row, and `popBack` over-pops the shorter nested structure:

    Cannot pop 1 rows from String: there are only 0 rows

Per type:
- `Tuple`: `size` is `columns[0]->size` and `columns[0]` grows first, so any
  later element that throws leaves `size` ahead - roll back the grown elements.
- `Nullable`: the nested column grows before the null map - roll back the
  nested column if appending to the null map throws.
- `Variant`: `local_discriminators` grows before `offsets` - roll it back if
  appending to `offsets` throws.
- `Object`: typed/dynamic paths and `shared_data` grow in sequence - roll each
  back to the previous size.

This should be sufficient (i.e. we do not need to fix IColumn::insert()
and others) because usually if some method of IColumn throws, we discard
the column, there are only few exceptions:

- checkpoint-based rollback (`getCheckpoint`/`rollback`) - should be safe already
- manual handling of parsing errors and reverting the state via
  popBack() - usually safe, but not safe when filling defaults via
  insertDefault() -- this patch

Note `insertManyDefaults` is not on this path (parsing fills defaults one
row at a time via `insertDefault`), but the `Variant`/`Object` overrides
were made exception-safe too, since they do a bulk fill and may be reused
elsewhere.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@azat
azat requested a review from Avogar June 9, 2026 07:54
@clickhouse-gh

clickhouse-gh Bot commented Jun 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [19b12cf]

Summary: ❌

job_name test_name status info comment
Stateless tests (arm_asan_ubsan, azure, parallel) FAIL
03100_lwu_07_merge_patches FAIL cidb
Unit tests (asan_ubsan) FAIL
UniqueKeyIndexCache.InsertLookupAndHitMissCounters FAIL cidb
AllTests FAIL cidb
Unit tests (tsan) FAIL
UniqueKeyIndexCache.InsertLookupAndHitMissCounters FAIL cidb
AllTests FAIL cidb
Unit tests (msan) FAIL
UniqueKeyIndexCache.InsertLookupAndHitMissCounters FAIL cidb
AllTests FAIL cidb

AI Review

Summary

This PR makes several composite column insertDefault paths roll back partially appended nested state when an exception such as MEMORY_LIMIT_EXCEEDED interrupts parsing. The core direction looks reasonable, but the regression coverage only proves the ColumnTuple path while the patch also changes independent rollback logic in ColumnNullable, ColumnVariant, and ColumnObject.

Missing context / blind spots
  • ⚠️ The linked PR CI report currently has 0 test results, and this checkout has no local build directory, so I could not verify the new gtest locally. A passing unit test run with the added coverage would close this gap.
Findings
⚠️ Majors
  • [src/Columns/tests/gtest_column_tuple.cpp:87] The bug-fix contract is that all modified composite default-insertion paths stay internally synchronized after an exception, but the new test only exercises ColumnTuple. ColumnNullable, ColumnVariant, and ColumnObject use different size carriers (null_map, discriminators/offsets, and shared_data), so a regression in any of those new rollback blocks would still pass. Add focused tests that force the second append to throw for the other modified column types, or cover the corresponding parsing cases, and assert all nested sizes are restored.
Tests
  • ⚠️ Missing focused regression coverage for the non-ColumnTuple paths changed by this PR: ColumnNullable::insertDefault, ColumnVariant::insertDefault, ColumnVariant::insertManyDefaults, ColumnObject::insertDefault, and ColumnObject::insertManyDefaults.
Final Verdict

Status: ⚠️ Request changes

Minimum required action: add focused regression coverage for the other modified composite column rollback paths.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 9, 2026
@Avogar Avogar self-assigned this Jun 9, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.60% 84.60% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.30% 77.20% -0.10%

Changed lines: Changed C/C++ lines covered by tests: 49/81 (60.49%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 2 line(s) · Uncovered code

Full report · Diff report

@azat

azat commented Jun 9, 2026

Copy link
Copy Markdown
Member Author

UniqueKeyIndexCache.InsertLookupAndHitMissCounters

@azat
azat enabled auto-merge June 9, 2026 18:16
@azat
azat added this pull request to the merge queue Jun 9, 2026
@azat azat added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Jun 9, 2026
Merged via the queue into ClickHouse:master with commit 349e653 Jun 9, 2026
161 of 166 checks passed
@azat
azat deleted the fix-columns-insert-exception-safety branch June 9, 2026 18:43
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 9, 2026
robot-clickhouse-ci-1 added a commit that referenced this pull request Jun 9, 2026
Cherry pick #106802 to 26.3: Fix inconsistent columns on exception (i.e. MEMORY_LIMIT_EXCEEDED) during parsing
robot-clickhouse added a commit that referenced this pull request Jun 9, 2026
robot-clickhouse-ci-1 added a commit that referenced this pull request Jun 9, 2026
Cherry pick #106802 to 26.4: Fix inconsistent columns on exception (i.e. MEMORY_LIMIT_EXCEEDED) during parsing
robot-clickhouse added a commit that referenced this pull request Jun 9, 2026
robot-clickhouse-ci-1 added a commit that referenced this pull request Jun 9, 2026
Cherry pick #106802 to 26.5: Fix inconsistent columns on exception (i.e. MEMORY_LIMIT_EXCEEDED) during parsing
robot-clickhouse added a commit that referenced this pull request Jun 9, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jun 9, 2026
alexey-milovidov added a commit that referenced this pull request Jun 10, 2026
Backport #106802 to 26.4: Fix inconsistent columns on exception (i.e. MEMORY_LIMIT_EXCEEDED) during parsing
alexey-milovidov added a commit that referenced this pull request Jun 10, 2026
Backport #106802 to 26.3: Fix inconsistent columns on exception (i.e. MEMORY_LIMIT_EXCEEDED) during parsing
azat added a commit that referenced this pull request Jun 11, 2026
Backport #106802 to 26.5: Fix inconsistent columns on exception (i.e. MEMORY_LIMIT_EXCEEDED) during parsing
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jun 11, 2026
@watson-ch watson-ch Bot added v26.3-must-backport and removed pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR labels Jun 26, 2026
@watson-ch

watson-ch Bot commented Jun 26, 2026

Copy link
Copy Markdown

Backport labels have been automatically added based on https://github.com/clickhouse/clickhouse-core-incidents/issues/1842

Labels added:

  • v26.3-must-backport

These labels ensure backport PRs are properly created for each affected release version. The release automation will create backport PRs as needed based on these labels.

@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jun 26, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jun 26, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…tion (i.e. MEMORY_LIMIT_EXCEEDED) during parsing
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…6802

Backport ClickHouse#106802 to 26.4: Fix inconsistent columns on exception (i.e. MEMORY_LIMIT_EXCEEDED) during parsing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.3-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants