Skip to content

Improve ColumnVector::getExtremes for floating point - #111221

Merged
alexey-milovidov merged 4 commits into
masterfrom
columnvector-getextremes-float64
Aug 12, 2026
Merged

alexey-milovidov merged 4 commits into
masterfrom
columnvector-getextremes-float64

Conversation

@cv4g

@cv4g cv4g commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Improve ColumnVector<T>::getExtremes() performance for floating-point columns by using the existing optimized
findExtremeMin()/findExtremeMax() helpers for Float32/Float64.

This affects column statistics that compute min/max over numeric columns, especially:

  • minmax column statistics,
  • the numeric part of basic column statistics

Motivation

Insert-time materialized statistics showed a large gap between integer and Float64 columns. For 65k rows, Float64
STATISTICS(minmax) was around an order of magnitude slower than UInt64, because ColumnVector::getExtremes() used ascalar NaN-aware loop for floats.

Changes

  • Use optimized findExtremeMin() / findExtremeMax() in ColumnVector::getExtremes() for floating-point vector
    columns.
  • Preserve existing NaN behavior:
    • skip NaNs when non-NaN values exist
    • return NaN when all values are NaN
    • Add unit coverage for:
    • integer extremes
    • Float64 with NaNs skipped
    • all-NaN Float32
  • Extend benchmark_statistics with:
    • Float64 basic / minmax
    • UInt64 / Float64 uniq_v2
  • Extend insert_statistics.xml with Float64-heavy minmax / basic cases so the improvement is visible in performance
    tests.

Results

Microbenchmark, 65,536 rows:

 ┌──────────────────────────────────┬─────────┬────────┐ 
 │ Benchmark                        │ Before  │ After  │
 ├──────────────────────────────────┼─────────┼────────┤ 
 │ Float64 STATISTICS(minmax) build │ ~230 µs │ ~31 µs │ 
 ├──────────────────────────────────┼─────────┼────────┤ 
 │ UInt64 STATISTICS(minmax) build  │ ~24 µs  │ ~24 µs │ 
 └──────────────────────────────────┴─────────┴────────┘ 

uniq_v2 did not show a Float64-specific slowdown; UInt64 and Float64 were comparable.

Testing

Ran:

  ninja -C /workspace/build-default benchmark_statistics
  ninja -C /workspace/build-default clickhouse
  /workspace/build-default/src/unit_tests_dbms --gtest_filter=ColumnVector.GetExtremes

Also validated the performance XML parses successfully.

Changelog category (leave one):

  • Performance Improvement

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

Improve efficiency of minmax and basic column statistics for floating point columns.

Version info

  • Merged into: 26.8.1.1205 (included in 26.8 and later)

@cv4g cv4g changed the title Improve ColumnVector::getExtremes for floating point numbers Improve ColumnVector::getExtremes for floating point Jul 21, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [16b7462]

Summary: ✅


AI Review

Summary

This PR switches ColumnVector::getExtremes for Float32/Float64 over to the existing findExtremeMin / findExtremeMax helpers, adds NaN-focused unit coverage, and extends the statistics benchmarks/performance test inputs. I did not find a runtime correctness issue in the production getExtremes path, but the new benchmark scaffolding has one benchmark-only build problem, so the optional benchmark_statistics target cannot currently be used to reproduce all of the claimed evidence when ENABLE_BENCHMARKS is enabled.

Findings

💡 Nits

  • [src/Storages/benchmarks/benchmark_statistics.cpp:47-50] createBenchmarkUniqCombined64 instantiates AggregateFunctionUniqCombined with only three template arguments, but the actual class template in src/AggregateFunctions/AggregateFunctionUniqCombined.h:131-141 takes four: T, ColumnType, K, and HashValueType. The default CI matrix stays green because it does not build this optional target, but an ENABLE_BENCHMARKS=1 build will fail here, which means the new uniq_v2 benchmark evidence is not reproducible from the tree as-is.
    Suggested fix: either instantiate the production type directly (AggregateFunctionUniqCombined<UInt64, ColumnVector<UInt64>, 12, UInt64> / AggregateFunctionUniqCombined<Float64, ColumnVector<Float64>, 12, UInt64>) or reuse the same helper path as the real registration so the benchmark stays bit-for-bit aligned with production behavior.
Final Verdict

Non-blocking. The production ColumnVector::getExtremes change looks okay, but the new benchmark target should be fixed before relying on it as reproducible performance evidence.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.80% 78.80% +0.00%

Changed lines: Changed C/C++ lines covered: 50/54 (92.59%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Jul 21, 2026
@Algunenano Algunenano self-assigned this Jul 21, 2026
*/
if constexpr (has_find_extreme_implementation<T> && is_floating_point<T>)
{
auto cur_min = findExtremeMin(data.data(), start, end);

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.

Have you tested if other types with has_find_extreme_implementation benefit from using findExtremeMin/findExtremeMax too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not yet, I will check

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spent a moment to get a better understanding for this. Thank you for asking.

Integers do not benefit much from using the find extreme implementation. The main difference between both is that the current implementation here is accumulating min and max in a single pass.
findExtreme() in contrast is doing two passes (one for min, one for max) and it implements a manually unrolled multi-accumulator reduction.
i.e. it sets up n accumulators and makes each accumulator consume only every n-th element. This enables the compiler either to reorder instructions more flexibly (due to the separate data dependencies) or to keep both data and accumulators in simd registers and thus auto-vectorize it.
This cannot be done automatically otherwise by the compiler because of IEEE semantics. It would requre --ffast-math for that.
For integers however the compiler can already do that automatically, so there's no opportunity to be more efficient with a manual implementation.
Big integers unfortunately lack comparison instructions for 128 bit and 256 big it seems, so no gain here as well.

}

size_t i = start;
if constexpr (is_floating_point<T>)

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.

Maybe we can implement has_find_extreme_implementation for BFloat16 in the near future and simplify this code, so we don't have 3 different if constexpr (is_floating_point<T>)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! Let's do it as a follow up and in the mean time briefly investigate already how much effort, complexity that'd be

@clickhouse-gh

clickhouse-gh Bot commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 36 queries analysed

The only runtime change in this PR is a vectorized getExtremes fast-path for floating-point columns; everything else is a new local benchmark tool, build wiring, unit tests, and a perf-test fixture. None of the flagged clickbench or TPC-H queries request extremes or hit that float path, so the +5% to +12% deltas are not plausibly caused by the diff. Q16's flagged regression is downgraded to not_sure on that basis, and the two envelope-suppressed rows (clickbench Q34, tpch_adapted_1_official Q7) are correctly suppressed — both medians sit inside master's current variance band, and the two TPC-H queries additionally ran co-located on a single node, which can inflate apparent deltas. Treat all four as run-to-run variance.

clickbench

⚠️ 3 inconclusive

Flagged queries (3 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 16 not_sure 723 802 +10.9% 0.0002 The only runtime change here is a vectorized getExtremes fast-path for float columns; Q16 is an integer GROUP BY that never requests extremes, so this +10.9% is run-to-run variance, not a PR effect.
⚠️ 32 not_sure 1308 1440 +10.1% 0.0005 Already borderline with mixed signals from the two tests. Q32 doesn't exercise the float getExtremes path this PR changes, so the +10.1% isn't attributable to the diff.
⚠️ 34 not_sure 1230 1292 +5.0% <0.0001 Correctly suppressed: source median 1292 ms sits inside master's current variance band [1193,1465], and Q34 doesn't touch the changed getExtremes float path.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

⚠️ 1 inconclusive

Flagged queries (1 of 22)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 7 not_sure 557 624 +11.9% <0.0001 Correctly suppressed within master's variance band [552,667]; it also ran co-located with another TPC-H query. Q7 requests no extremes, so the +11.9% isn't from this PR.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

Debug info
  • StressHouse run: 863eea3c-0e14-4aff-b22c-f258851b5215
  • MIRAI run: f0898ddc-1203-40b3-a132-0e33264d39b7
  • PR check IDs:
    • clickbench_172523_1786109948
    • clickbench_172529_1786109948
    • clickbench_172545_1786109948
    • tpch_adapted_1_official_172553_1786109948
    • tpch_adapted_1_official_172581_1786109948
    • tpch_adapted_1_official_172595_1786109948

@clickhouse-gh

clickhouse-gh Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.20% 86.20% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 78.20% 78.30% +0.10%

Changed lines: Changed C/C++ lines covered: 50/54 (92.59%) · Uncovered code

Full report · Diff report

@cv4g
cv4g requested a review from Algunenano August 7, 2026 13:50

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

All good.

@alexey-milovidov alexey-milovidov self-assigned this Aug 12, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 12, 2026
Merged via the queue into master with commit 43d3a5a Aug 12, 2026
182 checks passed
@alexey-milovidov
alexey-milovidov deleted the columnvector-getextremes-float64 branch August 12, 2026 03:09
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 12, 2026
pull Bot pushed a commit to erickirt/ClickHouse that referenced this pull request Aug 27, 2026
Building master with -DENABLE_BENCHMARKS=ON failed to compile
src/Storages/benchmarks/benchmark_statistics.cpp with two errors of the
form "template argument for template type parameter must be a type" at
lines 48 and 50.

Two changes landed from branches that never saw each other. ClickHouse#109831
(ea66303) gave AggregateFunctionUniqCombined a ColumnType
template parameter in second position, making it
  template <typename T, typename ColumnType, UInt8 K, typename HashValueType>
ClickHouse#111221 (40da35b, "Reduce binary size for microbenchmark") then
replaced registerAggregateFunctions() in this benchmark with a
benchmark-local factory that hand-instantiates the class using the old
three-argument spelling. The literal 12 therefore binds to the ColumnType
position, where a type is required.

Supply the missing argument at the two call sites, using the spelling the
real factory uses for numeric types (AggregateFunctionUniqCombined.h:373,
ColumnVector<TYPE>). This is behaviour-preserving rather than a choice
among candidates: before ClickHouse#109831 the class hardcoded
assert_cast<const ColumnVector<T> &>(*columns[0]) in its body, and that
commit only hoisted the hardcoded type into the new parameter.

No CI job caught this because ENABLE_BENCHMARKS defaults to OFF
(CMakeLists.txt:159) and grep -rn ENABLE_BENCHMARKS ci/ returns no hits,
so no build compiles this translation unit.

Validated in both directions: on the unmodified tree the target fails with
exactly those two errors and no others; with this change it compiles,
links, and the four UniqV2 benchmark cases run, covering both the UInt64
and Float64 paths. The other eight benchmark targets compile cleanly, so
all nine benchmark executables now build.

Closes: ClickHouse#116251
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-performance Pull request with some performance 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.

5 participants