Skip to content

Bound the search for the structure of a Freeform row - #122447

Merged
alexey-milovidov merged 5 commits into
ClickHouse:masterfrom
msohail22:fix-freeform-schema-inference-exponential
Oct 1, 2026
Merged

alexey-milovidov merged 5 commits into
ClickHouse:masterfrom
msohail22:fix-freeform-schema-inference-exponential

Conversation

@msohail22

@msohail22 msohail22 commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #122412
Related: #43449

Schema inference for Freeform exhausted memory on a wide row of strings. buildSolutions recursed once per matcher kept by readNextFields, and for a string field every matcher that reads it is kept (best_score <= 1). Raw and Escaped read a word in a tab-separated row alike, so N such fields gave at least 2^N candidate solutions. On master, 16 fields already fail with MEMORY_LIMIT_EXCEEDED under a 150 MB limit.

The search now gives up after input_format_freeform_max_search_steps steps (a new setting, default 4096, 0 means unlimited) with a BAD_ARGUMENTS error that asks for a format with a fixed structure. compatibility below 26.10 restores the previous unbounded search. Under the default, rows of up to 10 tab-separated string fields infer as before, and a row of 24 numbers is unaffected. Merging matchers that read a field alike was rejected: Raw and Escaped can split a later row differently, and validation needs both candidates.

Changelog category (leave one):

  • Improvement

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

Schema inference for the Freeform format no longer exhausts memory on a wide row of strings: the search for the row structure is bounded by the new setting input_format_freeform_max_search_steps (default 4096, 0 means unlimited).


Workflow [PR]
Sync PR [sync-upstream/pr/122447]

Version info

  • Merged into: 26.10.1.1120-master (included in 26.10 and later)

`buildSolutions` recursed once per matcher kept by `readNextFields`, and for a
field that only reads as a string every matcher that reads it is kept. A word
in a tab-separated row is read alike by `Raw` and `Escaped`, so a row of N such
fields produced at least 2^N candidate solutions, each holding N columns, and
schema inference exhausted memory from about 16 fields on.

The search now gives up after `max_search_steps` calls with a `BAD_ARGUMENTS`
error that asks for a format with a fixed structure. The bound keeps every row
of up to 14 such fields inferring as before.

ClickHouse#122412
@msohail22

Copy link
Copy Markdown
Contributor Author

@alexey-milovidov @Avogar Could you review this? It bounds the Freeform structure search from #43449, which exhausted memory on wide rows of strings (#122412). Could you also add can be tested so CI can run?

@clickhouse-gh clickhouse-gh Bot added the comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). label Sep 26, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Sep 26, 2026
@alexey-milovidov alexey-milovidov self-assigned this Sep 26, 2026
@ClickHouse ClickHouse deleted a comment from chatgpt-codex-connector Bot Sep 26, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c69227b]

Summary: ✅


AI Review

Summary

This PR bounds the Freeform row-structure search with input_format_freeform_max_search_steps, threads that setting through FormatSettings, and salts the Freeform schema-cache key with the bound so a schema inferred under permissive settings is not reused under stricter ones. I re-checked the current PR head c69227b44976925130d45b0ebb33e8c3aa6d6727 against the prior clickhouse-gh threads; those fixes are present, and I did not find any remaining correctness or compatibility issues in the changed code.

Final Verdict

✅ No new blocking or major issues found in the current PR head, so I did not post any new inline review comments.

LLVM Coverage Report

Measured on commit c69227b.

Metric Baseline Current Δ
Lines 87.90% 87.90% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.30% 79.30% +0.00%

Changed lines: Changed C/C++ lines covered: 48/48 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Sep 26, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing c69227b44 with master daa7bdbd3 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

3222 translation units recompiled, 16924 s compile time in total, 3222 of them have a recent master baseline.

Job report

@clickhouse-gh clickhouse-gh Bot added the external Author is not a member of the ClickHouse organization label Sep 27, 2026
With 65536 steps, refusing a wide row took minutes when several queries ran at once,
so `05258_freeform_schema_inference_search_bound` timed out in the flaky checks.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122447&sha=26acf79a0413e25aaf4b6288c85bde0cd74fd80f&name_0=PR
PR: ClickHouse#122447
@msohail22

Copy link
Copy Markdown
Contributor Author

@clickgapai Thanks for the report.

The only failing check is Performance Comparison (arm_release, master_head, 6/6) on the multiif test. That runner was flagged under-utilized CPU and disk-almost-full, and this PR does not touch the multiIf code path; it only changes Freeform schema inference. All other checks pass.

@alexey-milovidov @Avogar, it is ready for review.

@clickgapai

Copy link
Copy Markdown
Contributor

@msohail22 PR #122447 isn't merged yet, so there's no review of it here so far.

alexey-milovidov and others added 2 commits September 28, 2026 10:07
…n the test

The bound of the search for the structure of a `Freeform` row is now the
`input_format_freeform_max_search_steps` setting (default 4096, 0 means
unlimited). Its history entry records 0 for versions before 26.10, so
`compatibility` restores the previous unbounded search.

The test now checks the exact boundary under the default (10 tab-separated
strings infer, 11 are refused), that raising or removing the bound, or
`compatibility = '26.9'`, infers the 11-field row, and requires exactly
`BAD_ARGUMENTS` for the refused rows, so a regression back to
`MEMORY_LIMIT_EXCEEDED` fails it.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Pushed b875212: merged master and addressed the three review threads. The Freeform search bound is now the input_format_freeform_max_search_steps setting, and compatibility below 26.10 restores the previous unbounded search. The test pins the exact boundary (10 strings infer, 11 are refused) and requires exactly BAD_ARGUMENTS. I updated the description and changelog entry to match.

@groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122447&sha=7755fb18b4ef219932b2ea1479cd2dd4c47db4af&name_0=PR&name_1=Performance%20Comparison%20%28arm_release%2C%20master_head%2C%206%2F6%29 (multiif #1/#2) and provide a fix in a separate PR. This PR only touches Freeform schema inference. If the fix is already in progress, link it here.

@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements and removed pr-not-for-changelog This PR should not be mentioned in the changelog labels Sep 28, 2026
Comment thread src/Formats/FormatFactory.cpp Resolved
@groeneai

Copy link
Copy Markdown
Collaborator

Acknowledged. multiif #1 and #2 (the two Decimal256 queries) have a second timing mode on ARM that is not caused by this PR:

  • In this run the candidate was slower in all 7 runs of each query (+41% and +39%), all of it user CPU time; rows, bytes, memory and function calls are identical.
  • Since 2026-09-08, 17 runs (this one, 15 other PRs and one master-vs-master run) had one side more than 20% slower on Repair subtree2 #1 or Repair subtree2 #2, in 6 of them the reference side. The four weeks before had one.
  • It is not a fixed property of a build: master build f14685836e49 measured 0.114 s in one job and 0.151 s in another. Within a job it is stable: here it survived the harness's own server restart (confirm_changes: 0 of 2 flagged queries did not reproduce after restart).

Both binaries have the same static TLS size, so the in-progress #122550 is inert here, and the in-tree fallback I asked about on #100330 would not clear it either, because the shard's own report also marks both queries slower. I'm investigating the cause and will follow up here with a fix PR or my findings.

@groeneai

Copy link
Copy Markdown
Collaborator

Investigated: the cause is the position-independent release binary, which came from my #117449 (merged 2026-09-09, reverted in #119279 on 2026-09-10, re-landed in #119284 on 2026-09-15). On arm, a server process started from the PIE binary sometimes runs multiif #1/#2 20-40% slower. It is not caused by this PR, and a change to multiif.xml alone would not fix it, because other queries show the same thing.

Correction to my note above: a restart usually redraws the mode. Since 2026-09-10 the harness's confirmation restart demoted 70 of the 75 such multiif results; this run's two are among the 5 it did not.

Share of arm server processes more than 20% slower than the other server of the same master run:

query before #117449 with #117449 reverted since #119284
multiif #1 0/1261 1/66 1/420 13/949
rand #0 33/1259 15/66 19/420 227/945
vectorize_aggregation_combinators #0 13/1259 16/66 0/418 111/947
optimize_time_filter_with_preimage #6 6/735 7/66 1/418 170/945

amd has 0 to 2 per query in every period.

@alexey-milovidov, which fix do you want?

I recommend (A): (B) filters these runs out of the verdict, but arm servers running the PIE build would keep a start-dependent speed for these queries.

… schema cache key

The search bound decides whether `Freeform` schema inference succeeds, so a
schema cached under a raised bound (or `compatibility = '26.9'`) must not be
reused under the default bound. Test 05258 now checks that with
`schema_inference_use_cache_for_file = 1`.

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

Copy link
Copy Markdown
Collaborator

Correction to my comment above: amd has the same per-process slow mode, in other queries, so (A) as I wrote it would leave the amd reds in place. It showed up as the amd_release 5/6 red on #122184 (job, join_squashing_blocks #0 +17.2%).

Share of amd server processes more than 20% slower than the other server of the same master run (raw samples in query_metric_runs_v1, since 2026-08-29):

query before #117449 with #117449 reverted since #119284
join_squashing_blocks #0 1/767 5/66 1/422 52/995
decimal_parse #0 0/767 5/66 0/422 68/995
hashjoin_low_cardinality_key #0 0/767 2/66 0/422 20/997
timeseries_extract_tag #0 0/503 1/66 0/422 11/997

On arm these queries have at most 1 per period. In a slow process join_squashing_blocks #0 takes a median 37% longer, all of it user CPU time. The harness's restart demoted 76 of the 79 amd moves of join_squashing_blocks #0 above 20% since 2026-09-16, but it only re-runs queries above the shard's own threshold: the +17.2% on #122184 was below its 19%, so only the dashboard judged it, from a single server start.

@alexey-milovidov, so the choice becomes:

I still recommend (A), for the same reason as above.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 1, 2026
Merged via the queue into ClickHouse:master with commit 16b0d2e Oct 1, 2026
149 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 1, 2026
@groeneai

Copy link
Copy Markdown
Collaborator

Fixing PR for the multiif #1/#2 arm_release perf red: #125278

@groeneai

groeneai commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Fixing PR for the multiif #1/#2 arm_release perf red: #125949 (it replaces the closed #125278). Correction: #125949 is not shown to fix this red. Reds like this one existed before PIE (PR side at least 15% slower in 0.2% of PR runs from Jul 1 to Sep 9, 0.4-0.5% since Sep 15), and multiif #1/#2 have too few slow runs per build to test against the load address. Per-query numbers: #125949 (comment)

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 comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). external Author is not a member of the ClickHouse organization pr-improvement Pull request with some product improvements 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.

Freeform schema inference OOMs on a wide all-string row (2^N solution search)

5 participants