Skip to content

Add output_format_float_precision setting to limit decimal digits in float output - #99721

Merged
rienath merged 44 commits into
ClickHouse:masterfrom
phulv94:add-output-format-float-precision-setting
Jun 4, 2026
Merged

rienath merged 44 commits into
ClickHouse:masterfrom
phulv94:add-output-format-float-precision-setting

Conversation

@phulv94

@phulv94 phulv94 commented Mar 17, 2026 •

Copy link
Copy Markdown
Contributor

When set to 0 (the default), the existing shortest round-trip representation (dragonbox) is used. When set to N, values are rounded to N decimal places using the double-conversion library's ToFixed. Closes #99199

Changelog category (leave one):

  • New Feature

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

Add output_format_float_precision setting to control the number of decimal digits in floating-point text output.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Note

Medium Risk
Changes core float serialization for text/JSON outputs and adds a new setting that can affect query results’ textual representations; mistakes could impact client parsing or tests but are bounded to formatting behavior.

Overview
Adds a new output_format_float_precision format setting to cap fractional digits in floating-point text output (with range checking) while keeping the default shortest round-trip representation when set to 0.

Plumbs the setting through FormatSettings/FormatFactory and switches numeric serialization to use a new writeFloatText(..., settings) path for Float32/Float64/BFloat16, including JSON number output, with special handling for non-finite values and scientific-notation fallbacks.

Updates settings history and CI doc-change detection to recognize DECLARE(...) blocks in settings headers, and adds a stateless test covering rounding, stripping trailing zeros, edge cases, and error handling.

Reviewed by Cursor Bugbot for commit d27d66d. Bugbot is set up for automated code reviews on this repo. Configure here.

Version info

  • Merged into: 26.6.1.376

@phulv94

phulv94 commented Mar 17, 2026

Copy link
Copy Markdown
Contributor Author

@rienath Can you take a look when you have freetime?

@rienath rienath self-assigned this Mar 17, 2026
@rienath rienath added the can be tested Allows running workflows for external contributors label Mar 17, 2026
@clickhouse-gh

clickhouse-gh Bot commented Mar 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0821ab6]

Summary: ✅


AI Review

Summary

This PR adds output_format_float_precision and routes floating-point text/JSON serialization through a precision-aware formatter while keeping 0 as the shortest round-trip default. The main implementation issues raised earlier are fixed, but one user-facing documentation/behavior mismatch remains around the scientific-notation fallback.

Missing context / blind spots
  • ⚠️ The latest S3 report for commit 0821ab6f9a650376071c9677a8b52261720d0dee still had performance jobs pending or skipped, and this checkout has no local clickhouse binary for fetch_perf_report.py. That leaves the earlier default-path overhead concern unclosed: every floating-point serializeText now enters the out-of-line writeFloatText overload even when output_format_float_precision = 0.
Findings

⚠️ Majors

  • [src/Core/FormatFactorySettings.h:1242] [dismissed by author -- https://github.com/Add output_format_float_precision setting to limit decimal digits in float output #99721#discussion_r3329116672] The setting docs still promise that values whose magnitude is so small that rounding to the requested precision would lose all significant digits are emitted in scientific notation, but the implementation only does that when the shortest representation was already scientific. src/IO/WriteHelpers.cpp:444 gates the ±0 fallback on default_decimals < 0, so a fixed-notation value such as 0.0001 with output_format_float_precision = 3 is formatted through ToFixed, stripped to 0, and not emitted scientifically. Please either qualify the docs with the missing “when the shortest representation is already scientific” condition, or remove the guard if every value rounded to ±0 should preserve significance.
Final Verdict

Status: ⚠️ Request changes

Minimum required action: fix the output_format_float_precision fallback documentation so it matches the current default_decimals < 0 behavior, or change the implementation to match the broader documented promise.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Mar 17, 2026
Comment thread src/IO/WriteHelpers.cpp Resolved
Comment thread src/Core/FormatFactorySettings.h Resolved
Comment thread src/IO/WriteHelpers.cpp Resolved
Comment thread src/Formats/FormatFactory.cpp Resolved
@phulv94

phulv94 commented Mar 19, 2026

Copy link
Copy Markdown
Contributor Author

@rienath seem like the CI look good, can you take a look?

@rienath

rienath commented Mar 19, 2026

Copy link
Copy Markdown
Member

@phulv94 please resolve conflicts

@phulv94

phulv94 commented Mar 20, 2026

Copy link
Copy Markdown
Contributor Author

@phulv94 please resolve conflicts

I've resolved conflicts.

@rienath rienath changed the title Add output format float precision setting Add output_format_float_precision setting to limit decimal digits in float output Mar 27, 2026

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

Thanks for working on this! The overall approach looks good. There are a couple of issues to address before merge.

With the current implementation, ToFixed always pads with trailing zeroes up to the requested precision, even when they carry no information. For example:

:) SELECT (1.1::BFloat16) settings output_format_float_precision=0;
-- 1.09375

:) SELECT (1.1::BFloat16) settings output_format_float_precision=30;
-- 1.093750000000000000000000000000

The exact representation has only 6 meaningful digits, but we get 24 extra zeroes. Similarly for negative zero:

:) SELECT (-0.0::BFloat16) settings output_format_float_precision=1;
-- -0.0

:) SELECT (-0.0::BFloat16) settings output_format_float_precision=0;
-- -0

The setting should mean "round to at most N decimal places", not "always pad to N decimal places". It would be great to strip trailing zeroes (and the trailing decimal point if it becomes unnecessary), so that output_format_float_precision = 30 produces 1.09375, not 1.093750000000000000000000000000.

Another important problem is numbers with magnitude >= 10^60 throw because ToFixed can't represent them. A format setting should never cause previously-working queries to fail on valid data.

:) SELECT 1e61 settings output_format_float_precision=0;
-- 1e61

:) SELECT 1e61 settings output_format_float_precision=1;
-- Code: 28. DB::Exception: Cannot print floating point number. (CANNOT_PRINT_FLOAT_OR_DOUBLE_NUMBER)

When you fixe these, please add regression tests. You could use the examples above

Comment thread src/Core/SettingsChangesHistory.cpp Resolved
Comment thread src/Formats/FormatSettings.h Resolved
Comment thread src/Core/FormatFactorySettings.h Resolved
Comment thread src/IO/WriteHelpers.cpp Resolved
Comment thread src/IO/WriteHelpers.cpp Resolved
alexey-milovidov and others added 3 commits May 23, 2026 14:29
…ings.h in feature_docs hook

Address review feedback on `output_format_float_precision`:

- Clarify in the setting description that values whose shortest representation
  is in scientific notation are emitted as-is (the mantissa may exceed the
  requested precision) so callers know what to expect for very large or very
  small magnitudes. Also document the valid range explicitly.

- Extend `ci/jobs/scripts/workflow_hooks/feature_docs.py` to recognize
  `*Settings.h` files containing `DECLARE(` macros as embedded documentation,
  in the same way `*Settings.cpp` files containing `DECLARE_SETTING` are
  already accepted. Format settings put their per-setting `DECLARE(...)`
  blocks (with description strings) in `FormatFactorySettings.h`, so the
  previous hook treated them as undocumented even when the description was
  written inline.

ClickHouse#99721

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
The CI tool's gh token does not have the workflow scope, so the master merge
brought in many `.github/workflows/*.yml` updates that the remote rejects on
push. Restore those workflow files to their pre-merge state. The workflows
on master will apply once this PR is merged.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the manual approve Manual approve required to run CI label May 23, 2026
alexey-milovidov and others added 6 commits May 23, 2026 22:44
Workflow files in this branch were stale or had been deleted as a
workaround for GitHub's workflow-scope check. Restoring them to match
`master` so the PR diff no longer contains those unrelated changes.
…ry.cpp`

Master is now 26.6, so new settings introduced under this branch must be
registered in the 26.6 block. The 26.5 placement caused Fast test
`02995_new_settings_history` to fail with "PLEASE ADD THE SETTING VALUE
CHANGE TO SettingsChangesHistory.cpp" because the test filters for
versions strictly greater than 26.5.

Also clarify the setting description in `FormatFactorySettings.h` to
match the actual fallback behavior: scientific notation is used only as
a fallback when fixed notation cannot represent the value (large
magnitude makes `ToFixed` fail) or when rounding to the requested
precision would lose all significant digits (mantissa would become
`±0`). The previous wording read as if all scientific-notation
defaults were emitted as-is, which is not true for moderate magnitudes
where `ToFixed` succeeds.
The master merge updated .github/workflows/{master,pull_request}.yml; restore
them to the fork branch state so the push does not require the workflow OAuth
scope. These generated files do not affect the PR's CI.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
These changes to CI workflow / infrastructure files were unrelated to the
purpose of this pull request and were introduced accidentally (stale base
branch). Restoring them to match `master`.
@alexey-milovidov alexey-milovidov removed the manual approve Manual approve required to run CI label May 31, 2026
@rienath

rienath commented Jun 1, 2026

Copy link
Copy Markdown
Member

Let's see what the perf tests show here. The three-argument writeFloatText is defined out-of-line in WriteHelpers.cpp, so every float in serializeText / writeJSONNumber now takes an out-of-line call even when output_format_float_precision == 0 (the default).

Might be negligible (ThinLTO can fold it back in), so perf decides. If it shows up, we can keep precision == 0 inline in the header and only delegate the rare case:

template <typename T> requires is_floating_point<T>
void writeFloatTextWithPrecision(T x, WriteBuffer & buf, const FormatSettings & settings);
extern template void writeFloatTextWithPrecision(Float64, WriteBuffer &, const FormatSettings &);
extern template void writeFloatTextWithPrecision(Float32, WriteBuffer &, const FormatSettings &);
extern template void writeFloatTextWithPrecision(BFloat16, WriteBuffer &, const FormatSettings &);

template <typename T> requires is_floating_point<T>
inline void writeFloatText(T x, WriteBuffer & buf, const FormatSettings & settings)
{
    if (settings.float_precision == 0 || !isFinite(x))
        writeFloatText(x, buf);
    else
        writeFloatTextWithPrecision(x, buf, settings);
}

@clickhouse-gh clickhouse-gh Bot added the manual approve Manual approve required to run CI label Jun 1, 2026
Comment thread src/IO/WriteHelpers.cpp Resolved
Comment thread src/IO/WriteHelpers.cpp Resolved
Comment thread src/IO/WriteHelpers.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.40% 84.50% +0.10%
Functions 92.40% 92.40% +0.00%
Branches 77.00% 77.00% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 75/75 (100.00%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@rienath
rienath added this pull request to the merge queue Jun 4, 2026
Merged via the queue into ClickHouse:master with commit 9515e67 Jun 4, 2026
166 checks passed
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 4, 2026
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 manual approve Manual approve required to run CI pr-feature Pull request with new product feature 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.

Add output_format_float_precision setting to control decimal digits in float output

4 participants