Skip to content

Memory tracking containers for columns - #98145

Merged
nickitat merged 32 commits into
masterfrom
memory_tracking_containers_for_columns
Mar 21, 2026
Merged

nickitat merged 32 commits into
masterfrom
memory_tracking_containers_for_columns

Conversation

@nickitat

@nickitat nickitat commented Feb 26, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Note

Medium Risk
Touches core IColumn APIs (scatter, prepareForSquashing, dynamic-structure helpers) and many column implementations, so regressions would surface broadly at runtime or as subtle performance/memory-limit behavior changes.

Overview
Primary change: replaces std::vector/std::unordered_map/std::unordered_set (and related aliases) used throughout src/Columns with VectorWithMemoryTracking, UnorderedMapWithMemoryTracking, UnorderedSetWithMemoryTracking, and SetWithMemoryTracking to ensure allocations participate in throwing memory tracking.

API surface updates: updates IColumn and many column overrides to use these memory-tracked containers for scatter return values, prepareForSquashing inputs, dynamic-structure plumbing, and related helpers (e.g., countColumnsSizeInSelector, ColumnVariant discriminator mappings, ColumnObject path maps/sets).

Tooling: extends the style check to lint src/Columns for direct std::* container usage.

Written by Cursor Bugbot for commit 9f33db3. This will update automatically on new commits. Configure here.

Version info

  • Merged into: 26.4.1.88

nickitat and others added 22 commits February 26, 2026 23:34
…mns`

Changed the `scatter` virtual method signature in `IColumn` and all overrides to return
`VectorWithMemoryTracking<MutableColumnPtr>` instead of `MutableColumns`
(`std::vector<MutableColumnPtr>`), so scatter results are tracked by the memory tracker.

Updated all call sites outside `src/Columns` to use `auto` to capture the result, or
updated container element types accordingly (`arrayFold.cpp`, `HashedDictionaryParallelLoader.h`).

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…vectors

Changed `IColumn::Patch::sources` from `std::vector<Source>` to
`VectorWithMemoryTracking<Source>` so patch application is tracked by the memory tracker.

Changed the inner `std::vector<size_t>` inside `VectorWithMemoryTracking<>` in
`ColumnLowCardinality::getPermutation` to `VectorWithMemoryTracking<size_t>`.

Updated the single external builder in `applyPatches.cpp`.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
… `ColumnString`

Changed `batchSerializeValueIntoMemory` parameter from `std::vector<char *>` to
`VectorWithMemoryTracking<char *>` in `ColumnString`, matching the virtual method
signature in `IColumn`. Updated the call site in `ColumnsHashing.h` to use
`VectorWithMemoryTracking<char *>` for the local `memories` variable.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Changed the parameter type of `getLeastSuperColumn` from `std::vector<const ColumnWithTypeAndName *>`
to `VectorWithMemoryTracking<const ColumnWithTypeAndName *>`, and updated the three
call sites in `Planner/Utils.cpp`, `InterpreterSelectWithUnionQuery.cpp`, and
`InterpreterSelectIntersectExceptQuery.cpp` to use `VectorWithMemoryTracking` for
their local `columns` vectors.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…riminator>` directly

Changed all 3 private constructors of `ColumnVariant` to take `VectorWithMemoryTracking<Discriminator>`
by value instead of `const std::vector<Discriminator> &`. The member assignment in the full
constructor is now a direct move instead of a cross-allocator `.assign(begin, end)`.

Updated all `MutablePtr create` inline overloads: `std::vector<Discriminator>` overloads now
convert to `VectorWithMemoryTracking<Discriminator>` before calling `Base::create`;
`VectorWithMemoryTracking<Discriminator>` overloads now pass directly via move.

Updated the `Ptr create` `VectorWithMemoryTracking<Discriminator>` overloads in the `.cpp` to
delegate directly to the corresponding `VectorWithMemoryTracking<Discriminator>` `MutablePtr create`
overloads, eliminating the previous double-conversion through `std::vector<Discriminator>`.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Add `std::vector` forwarding overloads in `ColumnObject.h` for
backward compatibility with external callers in `src/DataTypes`.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…eate` and `setDynamicPaths`

Remove `std::vector` compatibility overloads and update all callers directly:
- `ColumnVariant::create` overloads taking `const std::vector<Discriminator> &` removed from `ColumnVariant.h`/`.cpp`; `FunctionsConversion.h` caller updated
- `setDynamicPaths` `std::vector` forwarding overloads removed from `ColumnObject.h`; callers updated:
  - `DeserializeBinaryBulkStateSubObject::dynamic_sub_paths` → `VectorWithMemoryTracking<String>`
  - `DeserializeBinaryBulkStateObjectStructure::sorted_dynamic_paths` inner type → `VectorWithMemoryTracking<String>`
  - `result_dynamic_paths` in `DataTypeObject.cpp` → `VectorWithMemoryTracking<std::pair<String, ColumnPtr>>`

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…WithMemoryTracking`

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
… to `VectorWithMemoryTracking`

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Update all 7 separate alias definitions to use `VectorWithMemoryTracking<ColumnWithTypeAndName>`,
fix explicit `std::vector<ColumnWithTypeAndName>` local variables in call sites,
and templatize `applyPermutation` in `ColumnPermuteTransform` to accept any allocator.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…MemoryTracking<ColumnPtr>\`

Update the virtual method signature in \`IColumn\`, all 11 override declarations,
all 9 override implementations, and both callers in \`MergedData\` and
\`ColumnGathererTransform\`. Local \`Columns\` temporaries inside the
implementations are converted to \`VectorWithMemoryTracking<ColumnPtr>\` as well.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…umnPtr>\`

Update the virtual method signature in \`IColumn\`, all 9 override declarations
(including the private \`prepareVariantsForSquashing\` in \`ColumnDynamic\`),
all 9 override implementations, and the caller in \`Squashing\`. Local
\`Columns\` temporaries inside the implementations are converted to
\`VectorWithMemoryTracking<ColumnPtr>\` as well. The initializer-list call
in \`StreamingFormatExecutor\` requires no change.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…lumns/tests\`

Full replacement across all test files, except:
- \`Columns\`, \`MutableColumns\`, \`ColumnRawPtrs\` aliases (preserved per project convention)
- \`std::vector<String>\` used in \`checkInsertFrom\`/\`checkInsertManyFrom\`/\`checkInsertRangeFrom\`
  helper parameters and corresponding local variables — these are compared via \`ASSERT_EQ\`
  against \`variant_names\` (type \`Names = std::vector<String>\`) and \`operator==\` requires
  matching allocators
- \`std::vector<std::thread>\` in \`gtest_column_dump_structure\` is replaced as it is unrelated
  to column API boundaries

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…ynamic\`

Replace remaining \`std::vector<String>\` usages (\`expected_names\` parameters
and local variables) with \`VectorWithMemoryTracking<String>\`. Fix the 8
\`ASSERT_EQ\` comparison sites against \`variant_names\` (type \`Names =
std::vector<String>\`) by converting \`expected_names\` via range constructor:
\`Names(expected_names.begin(), expected_names.end())\`.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Since \`variant_names\` is of type \`Names\` (= \`std::vector<String>\`),
replace \`VectorWithMemoryTracking<String>\` with \`Names\` for
\`expected_names\` variables and parameters. This avoids the need
for range-constructor conversions at \`ASSERT_EQ\` sites and keeps
the test types consistent with the API types they compare against.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…/Columns

- ColumnDynamic.h: member `variants_with_failed_combination`
- ColumnObject.cpp: local `new_dynamic_paths` in `serialize`
- ColumnReplicated.cpp: local `indexes_of_default_values` in `getNumberOfDefaultRows`

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
- member `sorted_dynamic_paths` and its iterator pair

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…/Columns

Internal struct members and type aliases:
- ColumnDynamic.h: `VariantInfo::variant_name_to_discriminator`,
  `variant_mappings_cache`, `serialization_cache`,
  `DynamicColumnCheckpoint::variants_checkpoints`; `Statistics::variants_statistics`
  and `shared_variants_statistics` kept as `std::unordered_map` (external API boundary
  via SerializationVariant)
- ColumnObject.h: `Statistics::dynamic_paths_statistics`,
  `Statistics::shared_data_paths_statistics`, `PathToColumnMap`,
  `PathToDynamicColumnPtrMap`
- ColumnReplicated.h: `insertion_cache`

Updated `extendVariantColumn` signature and `getCheckpoint` local variable
in ColumnDynamic.cpp, and test helper in gtest_column_dynamic.cpp.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…ing in src/Columns

- ColumnObject.h: private constructors, public create methods
- ColumnObject.cpp: all local variables, constructor/create definitions,
  CheckpointsMap type alias; also updates VectorWithMemoryTracking element types
- ColumnDynamic.cpp: local vars in takeDynamicStructureFromSourceColumns
  and prepareVariantsForSquashing
- Collator.h: AvailableLocalesMap type alias
- Updated external callers: DataTypeObject.cpp (createColumn local var)
  and gtest_column_object.cpp (test local var)
- Updated tests: gtest_column_dynamic.cpp, gtest_column_unique.cpp,
  gtest_weak_hash_32.cpp

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…rderedMapWithMemoryTracking

- ColumnDynamic.h: Statistics::variants_statistics, Statistics::shared_variants_statistics
- SerializationVariant.h/.cpp: updated serializeBinaryBulkWithMultipleStreamsAndUpdateVariantStatistics
  parameter and internal tmp_statistics local variable

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@nickitat
nickitat force-pushed the memory_tracking_containers_for_columns branch from 740e91d to afa999e Compare February 26, 2026 22:46
@clickhouse-gh

clickhouse-gh Bot commented Feb 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [88a896d]

Summary: ✅

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Feb 26, 2026
claude --resume 87e09014-ce44-4519-87d1-b6c2f60bee33
@nickitat
nickitat requested a review from Copilot February 27, 2026 13:02

Copilot AI left a comment

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.

Pull request overview

This PR introduces systematic memory tracking for column-related containers in the ClickHouse codebase. The main goal is to replace standard containers (std::vector, std::unordered_map, std::set) with memory-tracking variants to enable throwing memory tracking and prevent out-of-memory (OOM) kills in production environments.

Changes:

  • Replaced standard container types with memory-tracking variants (VectorWithMemoryTracking, UnorderedMapWithMemoryTracking, SetWithMemoryTracking) for column-related data structures
  • Updated IColumn::scatter return type and all implementations to return VectorWithMemoryTracking
  • Changed ColumnsWithTypeAndName typedef to use VectorWithMemoryTracking
  • Updated CI style checks to enforce memory-tracking containers in src/Columns directory
  • Special exemptions for Columns, MutableColumns, and ColumnRawPtrs which remain as std::vector with STYLE_CHECK_ALLOW_STD_CONTAINERS markers

Reviewed changes

Copilot reviewed 89 out of 89 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Columns/IColumn.h Updated scatter method signature and prepareForSquashing to use VectorWithMemoryTracking
src/Columns/IColumn_fwd.h Added VectorWithMemoryTracking for EqualRanges and ColumnCheckpoints; kept Columns/MutableColumns/ColumnRawPtrs as std::vector with exemption markers
src/Core/ColumnsWithTypeAndName.h Changed typedef to use VectorWithMemoryTracking
src/Columns/ColumnVariant.h Updated internal data structures and method signatures to use memory-tracking containers
src/Columns/ColumnObject.h Replaced std::unordered_map with UnorderedMapWithMemoryTracking for path mappings
src/Columns/ColumnDynamic.h Updated variant info structures and caches to use memory-tracking containers
src/Storages/*.cpp Updated scatter call sites to use auto instead of explicit MutableColumns type
src/Processors/Transforms/*.cpp Updated to work with new container types
src/Functions/*.cpp Adapted to new ColumnsWithTypeAndName type
src/Columns/tests/*.cpp Updated test code to use memory-tracking containers
ci/jobs/scripts/check_style/check_cpp.sh Added src/Columns to directories enforcing memory-tracking container usage

Comment thread src/Core/ColumnsWithTypeAndName.h Resolved
@nickitat
nickitat marked this pull request as ready for review February 27, 2026 13:48
@nickitat

nickitat commented Mar 3, 2026

Copy link
Copy Markdown
Member Author

Stateless tests - attempting here #98612

@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.

Do not merge before investigating and fixing:

Stateless tests (arm_asan, targeted) failure
00633_materialized_view_and_too_many_parts_zookeeper FAIL cidb
Integration tests (amd_asan, db disk, old analyzer, 4/6) failure
test_ttl_replicated/test.py::test_ttl_compatibility[node_left1-node_right1-1] FAIL cidb, #98166

@nickitat

nickitat commented Mar 3, 2026

Copy link
Copy Markdown
Member Author

Do not merge before investigating and fixing:

Stateless tests (arm_asan, targeted) failure 00633_materialized_view_and_too_many_parts_zookeeper FAIL cidb Integration tests (amd_asan, db disk, old analyzer, 4/6) failure test_ttl_replicated/test.py::test_ttl_compatibility[node_left1-node_right1-1] FAIL cidb, #98166

@alexey-milovidov, please make your automatics more accurate.

@alexey-milovidov

Copy link
Copy Markdown
Member

Waiting while these tests will be fixed in master.

@nickitat

Copy link
Copy Markdown
Member Author

@alexey-milovidov, would you like to take another look?

@alexey-milovidov alexey-milovidov self-assigned this Mar 16, 2026
@nickitat

Copy link
Copy Markdown
Member Author

AST fuzzer (amd_debug, targeted) - #99920

@clickhouse-gh

clickhouse-gh Bot commented Mar 20, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.70% 83.80% +0.10%
Functions 24.00% 24.00% +0.00%
Branches 76.30% 76.40% +0.10%

PR changed lines: PR changed-lines coverage: 83.33% (920/1104, 7 noise lines excluded)
Diff coverage report
Uncovered code

@nickitat
nickitat added this pull request to the merge queue Mar 21, 2026
Merged via the queue into master with commit 19a4282 Mar 21, 2026
148 checks passed
@nickitat
nickitat deleted the memory_tracking_containers_for_columns branch March 21, 2026 16:15
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 21, 2026
PedroTadim added a commit that referenced this pull request Oct 2, 2026
`26.3` has neither the untrusted-count hardening that this change rewords on master (the
`reserveOrThrowTooMany*` helpers and the `MAX_DYNAMIC_TYPES_LIMIT` check) nor master's newer
`SerializationObjectHelpers` signatures. Keep the `26.3` code and apply only the switch to
memory-tracked containers, with `unflattenAndInsertPaths` instantiated for `std::vector<ColumnPtr> &&`.

The version 2 query in the test expects the non-flat path list to be memory-tracked, which came
with #98145 and is not in `26.3`, so the test keeps only the version 3 (FLATTENED) query that
this change fixes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog 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.

4 participants