Skip to content

Serializations and Statistics Refactoring - #85145

Draft
CurtizJ wants to merge 37 commits into
ClickHouse:masterfrom
CurtizJ:statistics-for-serialization
Draft

CurtizJ wants to merge 37 commits into
ClickHouse:masterfrom
CurtizJ:statistics-for-serialization

Conversation

@CurtizJ

@CurtizJ CurtizJ commented Aug 6, 2025 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

This PR is a draft for developing automatic LowCardinality serialization. It contains several tasks that will be extracted and merged in separate PRs, but it's more convenient for me to develop them in one branch.

This PR includes:

@clickhouse-gh

clickhouse-gh Bot commented Aug 6, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [7e6878c]

Summary: ❌
15 failures out of 104 shown:

job_name test_name status info comment
Fast test failure
02125_query_views_log FAIL
02560_null_as_default FAIL
03315_query_log_privileges_backup_restore FAIL
02669_alter_modify_to_nullable FAIL
01355_defaultValueOfArgumentType_bug FAIL
03148_async_queries_in_query_log_errors FAIL
01943_query_id_check FAIL
00079_defaulted_columns FAIL
01931_storage_merge_no_columns FAIL
02423_ddl_for_opentelemetry FAIL
02842_vertical_merge_after_add_drop_column FAIL
03254_pr_join_on_dups FAIL
02346_additional_filters_index FAIL
01780_column_sparse_tuple FAIL
03199_merge_filters_bug FAIL
03403_mv_errors FAIL
03262_const_adaptive_index_granularity FAIL
03311_constantnode FAIL
01881_join_on_conditions_hash FAIL
02703_row_policy_for_database FAIL
02179_sparse_columns_detach FAIL
02864_statistics_delayed_materialization_in_merge FAIL
02286_vertical_merges_missed_column FAIL
02543_alter_rename_modify_stuck FAIL
00804_test_alter_compression_codecs FAIL
03208_buffer_over_distributed_type_mismatch FAIL
01788_update_nested_type_subcolumn_check FAIL
03530_insert_into_distributed_different_types_sparseness FAIL
01281_alter_rename_and_other_renames FAIL
02840_merge__table_or_filter FAIL
03519_analyzer_tuple_cast FAIL
00717_merge_and_distributed FAIL
02884_async_insert_native_protocol_4 FAIL
03164_materialize_skip_index_on_merge FAIL
01780_column_sparse_full FAIL
03143_benchmark_query_id_prefix FAIL
00030_alter_table FAIL
00421_storage_merge__table_index FAIL
01881_join_on_conditions_merge FAIL
02675_sparse_columns_clear_column FAIL
03600_auto_lc_01_basics FAIL
02947_merge_tree_index_table_1 FAIL
01414_freeze_does_not_prevent_alters FAIL
01055_compact_parts FAIL
01455_opentelemetry_distributed FAIL
01343_min_bytes_to_use_mmap_io FAIL
02662_sparse_columns_mutations_2 FAIL
03286_serialization_hint_system_columns FAIL
00753_alter_destination_for_storage_buffer FAIL
02538_alter_rename_sequence FAIL
00652_mutations_alter_update FAIL
02439_merge_selecting_partitions FAIL
01388_clear_all_columns FAIL
02574_suspicious_low_cardinality_msan FAIL
02882_replicated_fetch_checksums_doesnt_match FAIL
03154_recursive_cte_distributed FAIL
02662_sparse_columns_mutations_5 FAIL
01114_clear_column_compact_parts FAIL
03094_virtual_column_table_name FAIL
02714_async_inserts_empty_data FAIL
03231_pr_reverse_in_order FAIL
01780_column_sparse_materialize FAIL
03257_async_insert_native_empty_block FAIL
02862_sorted_distinct_sparse_fix FAIL
03040_dynamic_type_alters_2_compact_merge_tree FAIL
00804_test_delta_codec_compression FAIL
02662_sparse_columns_mutations_3 FAIL
03254_part_log_partition_column_is_set FAIL
02003_bug_from_23515 FAIL
03357_block_structure_union_step FAIL
01611_string_to_low_cardinality_key_alter FAIL
03217_filtering_in_storage_merge FAIL
03519_storage_url FAIL
01278_alter_rename_combination FAIL
01387_clear_column_default_depends FAIL
02946_materialize_column_must_not_override_past_values FAIL
02884_async_insert_native_protocol_2 FAIL
01780_column_sparse_alter FAIL
02662_sparse_columns_mutations_1 FAIL
02126_dist_desc FAIL
02346_text_index_bug47393 FAIL
02117_show_create_table_system FAIL
02319_lightweight_delete_on_merge_tree FAIL
02987_logical_optimizer_pass_lowcardinality FAIL
01915_merge_prewhere_virtual_column_rand_chao_wang FAIL
00578_merge_table_and_table_virtual_column FAIL
00061_merge_tree_alter FAIL
02381_compress_marks_and_primary_key FAIL
03526_columns_substreams_in_wide_parts FAIL
03579_mergeTreeIndex_params FAIL
02131_used_row_policies_in_query_log FAIL
02008_materialize_column FAIL
03079_analyzer_numeric_literals_as_column_names FAIL
02361_fsync_profile_events FAIL
03040_dynamic_type_alters_1_wide_merge_tree FAIL
02421_simple_queries_for_opentelemetry FAIL
02478_projection_and_alter_low_cardinality FAIL
02995_new_settings_history FAIL
02294_nothing_arguments_in_functions FAIL
01151_storage_merge_filter_tables_by_virtual_column FAIL
00961_checksums_in_system_parts_columns_table FAIL
02481_low_cardinality_with_short_circuit_functins FAIL
02399_merge_tree_mutate_in_partition FAIL
01824_move_to_prewhere_many_columns FAIL
02763_mutate_compact_part_with_skip_indices_and_projections FAIL
03040_dynamic_type_alters_1_compact_merge_tree FAIL
03261_minmax_indices_by_default FAIL
01780_column_sparse_filter FAIL
01576_alter_low_cardinality_and_select FAIL
01344_min_bytes_to_use_mmap_io_index FAIL
01661_referer FAIL
03600_auto_lc_03_auto_stats FAIL
02842_move_pk_to_end_of_prewhere FAIL
00926_adaptive_index_granularity_replacing_merge_tree FAIL
02461_mullable_pk_monotonicity_bug FAIL
02882_formatQuery FAIL
01780_column_sparse FAIL
02461_alter_update_respect_part_column_type_bug FAIL
03312_sparse_column_tuple FAIL
03240_insert_select_named_tuple FAIL
02884_async_insert_native_protocol_3 FAIL
02346_additional_filters_distr FAIL
01902_table_function_merge_db_repr FAIL
03047_on_fly_mutations_basics FAIL
03273_primary_index_cache_low_cardinality FAIL
01658_read_file_to_stringcolumn FAIL
03310_index_hints_read_columns FAIL
03400_merge_storage_underlying_tables_access_validation FAIL
01375_compact_parts_codecs FAIL
03229_async_insert_alter_http FAIL
02884_async_insert_skip_settings FAIL
02871_peak_threads_usage FAIL
03229_async_insert_alter FAIL
02884_async_insert_native_protocol_1 FAIL
03047_on_fly_mutations_multiple_updates FAIL
03540_system_dashboards FAIL
Build (arm_tidy) failure
Build ClickHouse failure
Build (amd_debug) dropped
Build (amd_release) dropped
Build (amd_asan) dropped
Build (amd_tsan) dropped
Build (amd_msan) dropped
Build (amd_ubsan) dropped
Build (amd_binary) dropped
Build (arm_release) dropped
Build (arm_asan) dropped
Build (arm_coverage) dropped
Build (arm_binary) dropped
Build (amd_darwin) dropped
Build (arm_darwin) dropped

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Aug 6, 2025
@EmeraldShift

Copy link
Copy Markdown
Contributor

This is an excellent set of features, thank you! Should I subscribe to this PR for updates, or will it be split into many smaller PRs later for merging?

@CurtizJ

CurtizJ commented Oct 14, 2025 •

Copy link
Copy Markdown
Member Author

@EmeraldShift

This PR will be split into smaller ones. The first one #87241 will be merged soon. You can subscribe to this PR for updates, because I'll post links to other PRs with separate features.

@clickhouse-gh

clickhouse-gh Bot commented Dec 16, 2025

Copy link
Copy Markdown
Contributor

Dear @CurtizJ, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

4 similar comments
@clickhouse-gh

clickhouse-gh Bot commented Mar 10, 2026

Copy link
Copy Markdown
Contributor

Dear @CurtizJ, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

Dear @CurtizJ, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Mar 24, 2026

Copy link
Copy Markdown
Contributor

Dear @CurtizJ, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented May 26, 2026

Copy link
Copy Markdown
Contributor

Dear @CurtizJ, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

CurtizJ added a commit to CurtizJ/ClickHouse that referenced this pull request Jun 14, 2026
…om-statistics

Resolve conflicts and adapt the "serialization infos from statistics" work
to current master.

Statistics:
- Remove the standalone `StatisticDefaults` statistics type and fold the
  per-column default count into `StatisticsBasic` (new `DefaultsCount`
  feature-mask bit; `getNumDefaults`/`estimateDefaults`, tracked only for
  types that support sparse serialization). `ColumnStatistics::estimateDefaults`
  and `getEstimate` now read the default count from `Basic`.

SerializationInfo:
- Reconcile master's streaming `serialization.json` writer with the
  statistics-based design: `SerializationInfo` carries only the serialization
  kind, while row/default counts come from statistics. Reading stays Poco-based
  (`fromJSON`/`fromJSONWithStats`).
- Add streaming `writeJSONWithStats` and the version-aware
  `writeSerializationInfosJSON`, so versions before `WITHOUT_DATA` still store
  the row/default counts (synthesized from the chosen kinds) and stay readable
  by servers that derive the kind from those counts. Wired into
  `MergedBlockOutputStream`, `MutateTask` and the serialization-hints path.
- Fix `loadSerializationInfosFromString`: the `stats` result was initialized for
  the wrong version and dereferenced as an empty optional when reading a
  pre-`WITHOUT_DATA` part.

MergeTree:
- `serialization_info_version` default stays `without_data`, with a fallback to
  implicit statistics when none are available (so sparse serialization keeps
  working on insert).
- `MergeTask`: choose the merged part's serialization by carrying over the
  serialization kinds of the source parts.
- `MergedBlockOutputStream`: re-enable empty/expired column pruning for
  mutations and merges.
- Projection parts derive serialization from implicit statistics.

Part of ClickHouse#85145

Co-Authored-By: Claude Opus 4.8 <[email protected]>
CurtizJ added a commit to CurtizJ/ClickHouse that referenced this pull request Jun 17, 2026
`updateSerializationHintsForPart` used to aggregate per-row default counts via
the removed `SerializationInfo::add`/`remove`. Carry over the serialization kind
of the source parts instead: hint a column as sparse once an active part
serializes it sparse. Sparse is always a safe representation and the on-disk
serialization is re-decided from statistics at write time, so an over-eager hint
only affects how external data is parsed on `INSERT ... SELECT FROM` a file-like
source, never correctness. Removing a part cannot un-aggregate a kind, so it is a
no-op; `resetSerializationHints` rebuilds the hints from the active parts.

This resolves the last commented-out block left over from removing the
data-accumulation API of `SerializationInfo`.

Part of ClickHouse#85145

Co-Authored-By: Claude Opus 4.8 <[email protected]>
CurtizJ added a commit to CurtizJ/ClickHouse that referenced this pull request Jun 17, 2026
`writeSerializationInfosJSON` derived the per-column `num_defaults` written for
serialization-info versions before `WITHOUT_DATA` from the chosen serialization
kind (sparse => all rows are defaults, otherwise none), discarding the real
default counts that `getImplicitStatisticsForSparseSerialization` had already
computed over the written block. That encoding is lossy: a dense column's real
defaults were recorded as 0, which corrupts the aggregate default ratio an older
server computes during its next merge on a mixed-version cluster.

Carry the implicit statistics that chose the serialization to part finalization
through the (previously unused) `GatheredData::statistics_for_serializations`
field, and add `getEstimatesForSerializationInfos` to select the counts to
persist: the real counts from those statistics for `INSERT` and projection
writes, and a kind-based encoding only for the paths that decide the
serialization without counting defaults (merges and mutations carry the kind
forward from the source parts; serialization hints carry no row data).

`writeSerializationInfosJSON` now only writes the estimates it is given.

Part of ClickHouse#85145

Co-Authored-By: Claude Opus 4.8 <[email protected]>
CurtizJ added a commit to CurtizJ/ClickHouse that referenced this pull request Jun 20, 2026
For serialization-info versions before `WITHOUT_DATA` (`basic`/`with_types`),
`serialization.json` stores the per-column `num_rows`/`num_defaults`. After the
serialization-from-statistics refactoring only the `INSERT` path recomputed those
counts; merges, mutations and the serialization-hints query-plan path passed empty
statistics to `writeSerializationInfosJSON`, which threw
`LOGICAL_ERROR: Missed statistics for column ...` for any sparse-capable column.
`serialization_info_version` is randomized across `basic`/`with_types` in CI, so
this affected most merges and mutations, not only rolling upgrades.

Recompute the counts by adding implicit `Basic` statistics for sparse-capable columns
into the part statistics (only for columns that do not already have `Basic`), so they
are built by the regular statistics machinery over the written data (handling
row-reducing merges) and recorded in `GatheredData::implicit_serialization_statistics`.
The implicit statistics are removed again (`getStatisticsToPersist`) before the
statistics files are written, so they are not persisted as user statistics; the
per-column counts for `serialization.json` are taken from them beforehand. In a
vertical mutation the counts of columns that are not rewritten are carried over from
the source part. `writeSerializationInfosJSON` now takes the per-column `Estimates`.

Part of ClickHouse#85145

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Dear @CurtizJ, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@abashkeev abashkeev added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Sep 3, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Dear @CurtizJ, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@alexey-milovidov alexey-milovidov added the close in a month if not active This will be closed in case of no information label Nov 15, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

close in a month if not active This will be closed in case of no information comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. pr-not-for-changelog This PR should not be mentioned in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants