Skip to content

Update H3 C library from v3.7.1 to v4.4.1 - #100348

Merged
alexey-milovidov merged 4 commits into
masterfrom
fix-h3-lookup-tables
Mar 25, 2026
Merged

alexey-milovidov merged 4 commits into
masterfrom
fix-h3-lookup-tables

Conversation

@alexey-milovidov

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

Copy link
Copy Markdown
Member

The H3 C library fork (github.com/ClickHouse/h3) was on v3.7.1 with 2 ClickHouse-specific range-check patches. Upstream H3 v4 already includes equivalent validation in _h3ToFaceIjk, so the patches are no longer needed.

Created a clean clickhouse-v4 branch in the fork at upstream tag v4.4.1 with no additional patches.

This fixes two known bugs in the v3 lookup tables:

The v4 API changed most functions to return H3Error with results via output parameters. Updated all 27 wrapper files in src/Functions/ to use the new calling convention. Also updated function renames: exactEdgeLengthM → edgeLengthM, distanceM →
greatCircleDistanceM, etc.

Updated 11 test reference files to reflect the corrected values.

Reference: #100272

Changelog category (leave one):

  • Backward Incompatible Change

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

The H3 library has been updated to v4, which improves the precision of length, area, and other metric calculations. This change is backward incompatible because the new results differ from previous ones.

Version info

  • Merged into: 26.4.1.270

The H3 C library fork (github.com/ClickHouse/h3) was on v3.7.1 with
2 ClickHouse-specific range-check patches. Upstream H3 v4 already
includes equivalent validation in `_h3ToFaceIjk`, so the patches are
no longer needed.

Created a clean `clickhouse-v4` branch in the fork at upstream tag
v4.4.1 with no additional patches.

This fixes two known bugs in the v3 lookup tables:
- Average hexagon edge lengths were actually inradii (apothems), not
  edge lengths (uber/h3#201, fixed in
  uber/h3#749)
- Average hexagon areas naively divided Earth surface by total cell
  count without excluding pentagon areas (fixed in
  uber/h3#529)

The v4 API changed most functions to return `H3Error` with results via
output parameters. Updated all 27 wrapper files in `src/Functions/` to
use the new calling convention. Also updated function renames:
`exactEdgeLengthM` → `edgeLengthM`, `distanceM` →
`greatCircleDistanceM`, etc.

Updated 11 test reference files to reflect the corrected values.

Reference: #100272

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

clickhouse-gh Bot commented Mar 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [066c404]

Summary: ✅


AI Review

Summary

This PR upgrades bundled H3 from v3 to v4 and updates ClickHouse wrappers/tests to the new API and corrected geometric metrics. Most API migration changes look consistent, but I found one user-visible contract mismatch in stringToH3 documentation versus runtime behavior. Also, the Changelog entry is directionally correct but still too generic for the actual behavior changes shown by tests (notably h3Distance output changes).

PR Metadata
  • Changelog category: ✅ Backward Incompatible Change matches the scope.
  • Changelog entry requirement: ✅ required and present.
  • Changelog entry quality: ⚠️ too vague for user impact. It says precision improved, but test updates also show concrete function-result changes (e.g. h3Distance values changed), which should be explicitly called out.

Suggested replacement:
Updated H3 library from v3.7.1 to v4.4.1. Results of multiple H3 functions changed due to upstream bug fixes and API behavior changes, including corrected average edge length/area values and changed h3Distance outputs for some cell pairs. This is backward incompatible for queries that rely on exact previous numeric results.

Findings

💡 Nits

  • [src/Functions/stringToH3.cpp:96,119] stringToH3 now throws on invalid input (H3Error checked), but docs still state it returns 0 for invalid strings. This is a user-facing contract mismatch.
    Suggested fix: update ReturnedValue text to say invalid input raises an exception (or change implementation to return 0 consistently).
ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ⚠️ Backward-incompatible behavior is intended, but changelog text is not specific enough about affected function outputs.
SettingsChangesHistory.cpp ➖
PR metadata quality ⚠️ Changelog entry is present but too generic for concrete output changes (e.g. h3Distance).
Safe rollout ➖
Compilation time ✅
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
    • Fix stringToH3 user-facing contract mismatch (docs or behavior).
    • Make Changelog entry explicit about concrete function output changes (including h3Distance).

@clickhouse-gh clickhouse-gh Bot added pr-backward-incompatible Pull request with backwards incompatible changes submodule changed At least one submodule changed in this PR. labels Mar 22, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The code is good.

res = gridPathCellsSize(start, end);
{
int64_t distance = 0;
if (!gridDistance(start, end, &distance))

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.

h3Distance used to return a negative value when distance cannot be computed (see the function docs below), but this new path initializes res = 0 and only assigns on E_SUCCESS. On any non-success from gridDistance, this now returns 0, which is also a valid distance (start == end).

That silently changes semantics and loses error signaling for valid-but-unreachable pairs.

Please preserve the previous contract by mapping non-success to a negative sentinel (for example -1) or by updating both behavior and docs/tests consistently if you intentionally changed semantics.

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.

@alexey-milovidov Hi, this comment looks meaningful, could you please double check?

alexey-milovidov and others added 2 commits March 22, 2026 17:28
…ntinel

- Zero-initialize `std::array<H3Index>` in `h3GetIndexesFromUnidirectionalEdge`
  and `h3GetUnidirectionalEdgesFromHexagon` to fix MSan
  use-of-uninitialized-value when H3 v4 functions fail on invalid input.

- Change `h3Distance` default result from 0 to -1 so that failure is
  distinguishable from a valid zero distance (start == end), preserving
  the documented contract.

- Restore test reference to expect `(0,0)` for invalid edge inputs.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
The previous commit changed the error sentinel of `h3Distance` from 0
to -1 to distinguish errors from a valid zero-distance result, but
the reference file for `04034_h3_functions_default_if_invalid` was not
updated accordingly.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@@ -1,28 +1,28 @@
6

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 PR changes h3Distance results (see this updated reference: values drop from 7/8/13 to 6/8/12), which is a user-visible backward-incompatible behavior change beyond length/area precision updates. Please update the Changelog entry to mention h3Distance semantic/result changes explicitly so users can anticipate query output differences.

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 is true.
@alexey-milovidov Should we mention this somewhere? We call different function right now

@CurtizJ

CurtizJ commented Mar 23, 2026

Copy link
Copy Markdown
Member

So, do we continue with the C library or migrate to Rust?

@alexey-milovidov

Copy link
Copy Markdown
Member Author

@CurtizJ, first, we will merge the C library update to eat the backward incompatible changes and get the new baseline. Then, merge the Rust library if it is better for us.

@CurtizJ CurtizJ self-assigned this Mar 24, 2026
res_data[row_num] = h3_index;

if (res_data[row_num] == 0)
if (err || res_data[row_num] == 0)

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.

stringToH3 now explicitly checks H3Error and throws on invalid input, but the function docs still claim it returns 0 for invalid strings. This creates a user-visible contract mismatch.

Please update the ReturnedValue text to say it throws an exception for invalid input (or change implementation to match docs).

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.

@alexey-milovidov here as well

@clickhouse-gh

clickhouse-gh Bot commented Mar 24, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 24.40% 24.50% +0.10%
Branches 76.60% 76.60% +0.00%

PR changed lines: PR changed-lines coverage: 94.01% (204/217, 0 noise lines excluded)
Diff coverage report
Uncovered code

@alexey-milovidov
alexey-milovidov merged commit b3aae3c into master Mar 25, 2026
149 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-h3-lookup-tables branch March 25, 2026 22:57
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 25, 2026
Algunenano added a commit to Algunenano/ClickHouse that referenced this pull request Mar 26, 2026
The CI Docker image installs an uninstrumented `fips.so` (built from
stock OpenSSL 3.1.2) that gets loaded during OpenSSL global
initialization. Sanitizers cannot track writes from uninstrumented
shared libraries, causing false positives — currently MSan reports
"use-of-uninitialized-value" in `OPENSSL_LH_retrieve` inside `fips.so`,
crashing the unit test binary before any tests run.

This has been a latent issue since FIPS was added to the Docker image
(June 2025), but only manifested now due to a link order change from
ClickHouse#100348 (H3 library upgrade) which shifted global constructor ordering
so that `sqid.cpp`'s `DEFAULT_BLOCKLIST` initialization poisons stack
memory before `curl.cpp`'s global constructor loads the FIPS provider.

Set `OPENSSL_CONF=/dev/null` for all sanitizer builds to prevent
loading the uninstrumented FIPS provider.

https://s3.amazonaws.com/clickhouse-test-reports/PRs/100798/f39aa9f209bb3760611b2bcb6eff737dad3b6f21/unit_tests_msan/job.log

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

@alexbakharew alexbakharew left a comment

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.

Just few clarifications about incompatible changes

res = gridPathCellsSize(start, end);
{
int64_t distance = 0;
if (!gridDistance(start, end, &distance))

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.

@alexey-milovidov Hi, this comment looks meaningful, could you please double check?

res_data[row_num] = h3_index;

if (res_data[row_num] == 0)
if (err || res_data[row_num] == 0)

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.

@alexey-milovidov here as well

@@ -1,28 +1,28 @@
6

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 is true.
@alexey-milovidov Should we mention this somewhere? We call different function right now

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

Labels

pr-backward-incompatible Pull request with backwards incompatible changes pr-synced-to-cloud The PR is synced to the cloud repo submodule changed At least one submodule changed in this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants