Skip to content

Build all examples as a single multi-call binary - #100736

Merged
alexey-milovidov merged 40 commits into
masterfrom
fix-amd-compat-disable-examples
Apr 29, 2026
Merged

alexey-milovidov merged 40 commits into
masterfrom
fix-amd-compat-disable-examples

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Mar 25, 2026 •

Copy link
Copy Markdown
Member

The amd_compat CI build was producing ~98 GB of binaries — almost 77 GB of which came from 167 individual example programs, each statically linking the full ClickHouse codebase (~2 GB per binary in release). Other builds produce 22-25 GB.

This was caused by PR #86258 (Aug 2025) which added -DENABLE_EXAMPLES=1 and -k0 all target to amd_compat, and PR #95826 (Feb 2026) which added -DENABLE_EXAMPLES=1 to ARM_BINARY for parser_memory_profiler.

This PR consolidates all 83 example programs into a single clickhouse-examples multi-call binary (like ClickHouse itself uses clickhouse-client, clickhouse-local, etc. via argv[0] dispatch):

$ clickhouse-examples hash_map 10000000 1
$ clickhouse-examples field
$ clickhouse-examples --help
Usage: clickhouse-examples <example-name> [args]
Available examples:
  kerberos_init
  quantile-t-digest
  ...82 examples...

Changes:

  • Each example's main renamed to mainEntryExampleXxx entry point
  • New src/Examples/main.cpp dispatcher and src/Examples/CMakeLists.txt
  • Removed -DENABLE_EXAMPLES=1 from amd_compat and ARM_BINARY CI configs
  • Removed AMD_COMPAT from "build all targets" group (back to clickhouse-bundle)

Changelog category (leave one):

  • Build/Testing/Packaging Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Build all examples as a single multi-call binary.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.166

Previously each example program was built as a separate executable,
each statically linking the full ClickHouse codebase (~2 GB per binary).
With 83 examples, `amd_compat` CI build produced ~77 GB of example
binaries alone (vs ~22 GB for the rest of the build).

This was caused by PR #86258 which added `-DENABLE_EXAMPLES=1` and
`-k0 all` target to `amd_compat`, and PR #95826 which added
`-DENABLE_EXAMPLES=1` to `ARM_BINARY`.

This change:
- Consolidates all 83 example programs into a single `clickhouse-examples`
  multi-call binary (like ClickHouse itself), dispatching by argv[0]
  or first argument
- Each example's `main` is renamed to a unique entry point
  (e.g., `mainEntryExampleHashMap`)
- Removes `-DENABLE_EXAMPLES=1` from `amd_compat` and `ARM_BINARY` CI
  configurations
- Removes `AMD_COMPAT` from the "build all targets" group

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 25, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [88dde57]

Summary: ✅


AI Review

Summary

This PR consolidates example executables into a single clickhouse-examples multi-call binary and adjusts CI/build wiring accordingly. The core direction is good, but there are still correctness/regression issues that should be fixed before merge.

Missing context
  • ⚠️ No CI run logs or artifact validation output were provided in this review context, so runtime/build validation for all target configurations could not be verified here.
Findings

❌ Blockers

  • [src/Examples/main.cpp:272] Subcommand dispatch passes args.data() without guaranteeing the C/C++ argv invariant argv[argc] == nullptr. This can break code paths that rely on sentinel termination and is a correctness risk.
    • Suggested fix: append a trailing nullptr after argument rewriting and before dispatch (or build args with one extra null slot from the start).
  • [src/Examples/main.cpp:115-216, src/Interpreters/examples/jit_example.cpp] jit_example is missing from the new dispatcher/source list, while the PR description states all examples are consolidated.
    • Suggested fix: either include jit_example in clickhouse-examples (with required symbol/ODR adjustments) or explicitly document and justify its intentional removal in PR description/changelog.
ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ⚠️ jit_example executable path was removed from built examples set.
Experimental gate ➖
No magic constants ✅
Backward compatibility ⚠️ argv contract regression in subcommand dispatch can break existing example argument handling assumptions.
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ⚠️ Consolidation is not complete while one example (jit_example) is missing.
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
    1. Preserve argv[argc] == nullptr when dispatching subcommands.
    2. Restore or explicitly justify removal of jit_example to keep behavior aligned with the stated scope of consolidation.

@clickhouse-gh clickhouse-gh Bot added the pr-build Pull request with build/testing/packaging improvement label Mar 25, 2026
Comment thread src/Examples/CMakeLists.txt Resolved
…clude list

The examples dispatcher is not production code and uses `std::cerr` for
help output, similar to `programs/main.cpp`.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

#if defined(OS_LINUX)
int main(int argc, char ** argv)
int mainEntryExampleProcfsMetricsProviderPerf(int argc, char ** argv)

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.

❌ clickhouse-examples now includes this source unconditionally (src/Examples/CMakeLists.txt), but this file still has an #else int main() fallback for non-Linux builds.

That creates multiple main definitions (src/Examples/main.cpp + this file) when OS_LINUX is false, which breaks linking.

Please rename the fallback entry point as well (for example to mainEntryExampleProcfsMetricsProviderPerf) and keep behavior via #if defined(OS_LINUX) inside that function.

The `parser_memory_profiler` is now part of the `clickhouse-examples`
multi-call binary and is no longer built as a separate executable.
Remove its artifact config, S3 upload hook, and references from
CI workflow definitions.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@alexey-milovidov
alexey-milovidov marked this pull request as ready for review March 25, 2026 20:22
alexey-milovidov and others added 3 commits March 27, 2026 00:09
…paths

Rename fallback `main` to `mainEntryExampleProcfsMetricsProviderPerf` in
`procfs_metrics_provider_perf.cpp` (non-Linux path) and to
`mainEntryExampleJitExample` in `jit_example.cpp` (non-`USE_EMBEDDED_COMPILER`
path). These conflict with the `main` in `src/Examples/main.cpp` when building
the consolidated `clickhouse-examples` binary.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Re-add the `-no-pie` and `-Wl,-no-pie` link options that were lost when
`parser_memory_profiler` was consolidated into the multi-call binary.
These flags ensure jemalloc heap profile addresses are stable across
processes, enabling cross-process batch symbolization with `--symbolize-batch`.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Comment thread src/Examples/main.cpp Resolved
alexey-milovidov and others added 4 commits March 27, 2026 22:20
…les` multi-call binary

The `run_profiler.sh` and `rebuild_reports.py` scripts still referenced the old
standalone `parser_memory_profiler` binary path at
`build/src/Parsers/examples/parser_memory_profiler`. Update them to use the new
`clickhouse-examples` multi-call binary at `build/src/Examples/clickhouse-examples`.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
list(APPEND EXAMPLES_SOURCES ../IO/examples/read_buffer_from_hdfs.cpp)
endif()

clickhouse_add_executable(clickhouse-examples ${EXAMPLES_SOURCES})

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.

❌ Consolidating all examples into one clickhouse-examples binary introduces One Definition Rule collisions from duplicated global symbols across different example translation units.

Concrete collisions now in one final executable include duplicate global type/function names like IColumn/ConcreteColumn (cow_columns.cpp vs cow_compositions.cpp) and helper functions like bench in multiple hash_map* examples. With separate binaries this was isolated; in one binary this is ODR-undefined behavior and can produce non-deterministic behavior at link/runtime.

Please isolate file-local declarations in each example (namespace { ... } or static for helpers/types that are not external API), especially in files listed by EXAMPLES_SOURCES.

alexey-milovidov and others added 2 commits March 28, 2026 23:53
Wrap file-local declarations (classes, helper functions, type aliases)
in anonymous namespaces to prevent One Definition Rule violations when
all examples are linked into a single multi-call binary.

Key collisions fixed:
- `IColumn`/`ConcreteColumn`/`print` in cow_columns.cpp vs cow_compositions.cpp
- `SimpleHash`/`Grower` across hash_map3.cpp, hash_map_string.cpp, hash_map_string_3.cpp
- `FastHash64`/`CrapWow` across hash_map_string.cpp and hash_map_string_3.cpp
- `bench` template across hash_map_lookup.cpp, hash_map_string_2.cpp, hash_map_string_3.cpp
- `AlternativeHash`/`CRC32HashTest` in hash_map.cpp
- `HashMapWithDump` in hash_map3.cpp
- `test_function` in jit_example.cpp

For files with `ZeroTraits`/`DefaultHash` template specializations, the
anonymous namespace is closed before the specializations (which must be
in the namespace of the primary template) and reopened afterward.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>


int main(int argc, char ** argv)
int mainEntryExampleParserMemoryProfiler(int argc, char ** argv)

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.

⚠️ The usage examples in this file still show invoking a standalone ./parser_memory_profiler binary, but after this PR the tool is a subcommand of clickhouse-examples.

Please update the examples to avoid user confusion, e.g. ./clickhouse-examples parser_memory_profiler ....

alexey-milovidov and others added 5 commits March 29, 2026 01:59
Suppress `clang-diagnostic-unused-member-function` warnings with NOLINT
in `hash_map3.cpp`, `hash_map_string.cpp`, and `hash_map_string_small.cpp`.
Remove redundant `static` on `mix` inside anonymous namespace in
`hash_map_string.cpp`.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Move global-scope type aliases and helper functions into anonymous
namespaces in `string_hash_map.cpp`, `string_hash_set.cpp`,
`two_level_hash_map.cpp`, and `hash_map_string_small.cpp` to avoid
One Definition Rule violations when all examples are linked into the
single `clickhouse-examples` binary.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Replace references to standalone `./parser_memory_profiler` with
`./clickhouse-examples parser_memory_profiler` in the documentation
comment block to reflect the multi-call binary consolidation.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Wrap file-local declarations (structs, classes, functions, variables)
in anonymous namespaces across all example source files that are now
compiled into the single `clickhouse-examples` binary.

Key fixes:
- `SimpleHash`, `FastHash64`, `CrapWow`, `Grower` were defined in
  multiple hash_map example files with different implementations
- Generic names like `State`, `Key`, `Value`, `Source` in several
  examples could collide at link time
- Template specializations (`ZeroTraits`, `DefaultHash`) kept at
  global scope where required for correct lookup

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
- Remove redundant `static` from functions in anonymous namespace in
  `compact_array.cpp` and `radix_sort.cpp` (readability-static-definition-in-anonymous-namespace)
- Add NOLINT for false positive unused function/member warnings in
  `hash_map_string_3.cpp` (functions are used through template instantiation)

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
* jeprof --text --show_bytes \
* --base=/tmp/query_before.*.heap \
* ./parser_memory_profiler \
* ./clickhouse-examples parser_memory_profiler \

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.

The jeprof examples in this comment block are now incorrect for a multi-call binary.

jeprof expects positional arguments as <program> <profile...>. With the current form:

jeprof ... ./clickhouse-examples parser_memory_profiler /tmp/query_after.*.heap

parser_memory_profiler is treated as a profile path (not a subcommand), so the command fails.

Please keep parser_memory_profiler only when running the profiler, but for jeprof analysis use ./clickhouse-examples as the program path:

jeprof ... ./clickhouse-examples /tmp/query_after.*.heap

alexey-milovidov and others added 2 commits March 29, 2026 07:24
…p`, fix `jeprof` examples

- Remove unused `DecomposedFloat32` struct and `sign` member from `DecomposedFloat64`
  in `dragonbox_test.cpp` (clang-tidy `clang-diagnostic-unused-member-function`)
- Remove redundant `static` from functions in anonymous namespace in
  `int_hashes_perf.cpp` (clang-tidy `readability-static-definition-in-anonymous-namespace`)
- Fix `jeprof` command examples in `parser_memory_profiler.cpp` to use
  `./clickhouse-examples` as the program path without the subcommand name,
  since `jeprof` expects `<program> <profile>` positional arguments

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
targets = "fuzzers"
elif build_type == BuildTypes.ARM_BINARY:
targets = "clickhouse-bundle parser_memory_profiler"
targets = "clickhouse-bundle"

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 drops parser_memory_profiler from ARM_BINARY outputs.

Before this PR, ARM_BINARY explicitly built parser_memory_profiler and CI uploaded it as an artifact (ArtifactConfigs.parser_memory_profiler and build_master_head_hook.py), which was then documented in changelogs as a published tool. After this change, ARM_BINARY only builds clickhouse-bundle, and all corresponding artifact/upload wiring is removed.

That is a functional regression in packaging/distribution, not just a build-size optimization. Please keep publishing this tool (for example, by adding a dedicated lightweight target/job that builds clickhouse-examples and uploads it, or by retaining parser_memory_profiler publishing path) so existing workflows depending on the published profiler do not break.

The `parser_memory_profiler` is now a subcommand of `clickhouse-examples`.
Update the `run_profiler.sh` help text for `--profiler` and the `README.md`
description of `run_profiler.sh` to reflect the multi-call invocation.

# The profiler binary is built from src/Parsers/examples/parser_memory_profiler.cpp
# The profiler binary is part of the clickhouse-examples multi-call binary (src/Examples/)
return {

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 file switches profiler execution to the clickhouse-examples multi-call binary, but the --profiler CLI help still says Path to parser_memory_profiler binary.

That message now points users to a non-existent standalone binary and conflicts with the new path/messaging in this same file.

Please update the --profiler help text to Path to clickhouse-examples binary for consistency.

alexey-milovidov and others added 2 commits April 25, 2026 09:08
The `--profiler` CLI option still said "Path to parser_memory_profiler
binary", but after consolidation the script now executes the
`clickhouse-examples` multi-call binary. Update the help text to match.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Comment thread src/Examples/CMakeLists.txt Resolved
@alexey-milovidov

Copy link
Copy Markdown
Member Author

@groeneai, fix "Logical error: Port is already connected" and send a separate PR. But maybe I've already asked about it recently - in that case, link the corresponding existing PR here.

@groeneai

Copy link
Copy Markdown
Collaborator

@alexey-milovidov yes — STID 0996 family ("Port is already connected" from LazyUnorderedReadFromMergeTreeSource::expandPipeline) is already addressed by PR #103230. Status: OPEN, MERGEABLE. @KochetovNicolai pre-approved on 2026-04-22; the review was dismissed by the 2026-04-24 rebase. Currently awaiting re-approval.

Stress test CI on the fix branch: 143 success / 16 skipped / 1 unrelated. All 11 stress test variants pass with the fix. Includes regression test 04105_lazy_final_unordered_expand_pipeline.

Once #103230 lands, STID 0996 will clear from this PR's CI as well. Tracking task: 2026-04-21-ci-p0-logicalerror-port-is-already-con (umbrella covers all "Port is already connected" variants observed across PRs).

cc @CurtizJ @KochetovNicolai for re-review of #103230.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

@groeneai, fix Unit tests (asan_ubsan, function_prop_fuzzer) and send a separate PR.

@groeneai

Copy link
Copy Markdown
Collaborator

@alexey-milovidov re function_prop_fuzzer — same chronic UBSAN bug at parseDateTimeBestEffort.cpp:79-82 (signed integer overflow in readDecimalNumber). Already addressed by PR #102890 (@Avogar), approved by @nihalzp on 2026-04-17, awaiting maintainer merge.

You earlier LGTM'd this deferral on PR #101034 (2026-04-26). 30+ unrelated PRs hit this in 14 days; Avogar's fix adds a has_fractional_separator flag to reject duplicate . separators with CANNOT_PARSE_DATETIME.

Once #102890 merges, function_prop_fuzzer will clear from this and all other affected PRs.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Apr 29, 2026
Merged via the queue into master with commit 79212ed Apr 29, 2026
165 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-amd-compat-disable-examples branch April 29, 2026 13:02
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 29, 2026
huldarchen pushed a commit to huldarchen/ClickHouse that referenced this pull request Sep 26, 2026
A flaky check hands every worker the *same* changed test, so one heavy test
contends with `--jobs N` copies of itself instead of with N unrelated tests.
`clickhouse-test` already accounts for that: a test tagged `long` is exempt from
the `TEST_MAX_RUN_TIME_IN_SECONDS` "too long" verdict of the check. The per-test
budget was not exempt, though - `proc.wait(args.timeout)` killed such a test at
the `--timeout` default of 600s regardless of the tag - so a pull request that
merely edits one draws a red that says nothing about the change.

Measured on `03927_autopr_input_bytes_estimation_prewhere_filter`, whose p50
under any `asan` lane on master is 33s and whose p90 is 55s: in the
`amd_asan_ubsan` flaky check it runs in 307-383s and is killed at 600.3s. The
inflation is the check's shape rather than a property of the test - in the same
job the other five AutoPR estimation tests are 4.1x to 5.6x their own master
p50 - and it has already produced the same 600.3s kill on pull requests that
have nothing in common: ClickHouse#96844, ClickHouse#119350 and ClickHouse#100736.

`TestCase.get_timeout` now gives a `long` test of a flaky check
`FLAKY_CHECK_LONG_TEST_TIMEOUT_MULTIPLIER` times `--timeout`, and is the single
place the per-test budget is read: the wait, the cleanup budget, the worker's
upper-level alarm, and `CLICKHOUSE_TEST_TIMEOUT` for a `.sh` test that budgets
itself against the harness. Every other test and every other job keeps
`--timeout` unchanged.

The external safety net in `run_tests` has to stay above the graceful alarm it
exists to avoid pre-empting, so it is now derived from the same numbers instead
of being a written-out 900s. For a job that is not a flaky check the derivation
yields exactly the 900s it has always been; for a flaky check it yields 2220s,
still far below the 2.5h job timeout.

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

Labels

pr-build Pull request with build/testing/packaging improvement 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.

5 participants