Skip to content

Add readGeoJSON and geoJSON functions - #129199

Open
pulk17 wants to merge 8 commits into
ClickHouse:masterfrom
pulk17:geojson-functions-v2
Open

pulk17 wants to merge 8 commits into
ClickHouse:masterfrom
pulk17:geojson-functions-v2

Conversation

@pulk17

@pulk17 pulk17 commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Add readGeoJSON and geoJSON functions
Adds readGeoJSON (alias ST_GeomFromGeoJSON) and geoJSON (alias ST_AsGeoJSON), to convert a single GeoJSON geometry object to Geometry and back. GeoJSON geometries often come as a field of JSON Lines or a string column of CSV, and the GeoJSON format only reads a whole FeatureCollection.

The functions reuse the geometry reader and writer of the GeoJSON format (moved out of the format classes into free functions), so they follow the same rules and the same settings: format_geojson_validate_geometry, input_format_geojson_unsupported_geometry_handling and precise_float_parsing.

SELECT readGeoJSON('{"type":"LineString","coordinates":[[0,0],[1,1]]}') AS g, variantType(g), geoJSON(g);
-- [(0,0),(1,1)]	LineString	{"type":"LineString","coordinates":[[0,0],[1,1]]}
  • readGeoJSON takes a String, a JSON value, the named Tuple that schema inference gives for a geometry object in JSON Lines, or Dynamic. A JSON null, a NULL argument and a missing field of a JSON column give NULL. Coordinates after the first two (altitude) are ignored.
  • geoJSON takes any geo type or Geometry. A Ring is written as a Polygon with one ring, because GeoJSON has no ring. For Geometry the result is Nullable(String). Coordinates are written with full precision and never quoted; NaN and infinity are an error, because JSON has no such numbers.

Both functions keep the settings they use and add them to updateHash, as toJSONString does, so the query condition cache tells results for different settings apart. geoJSON of Geometry and readGeoJSON of Dynamic run behind the Variant and Dynamic adaptors, which build the function for each type only when they execute it, and those adaptors dropped the hash. So IFunctionOverloadResolver gets updateHash, and FunctionToOverloadResolverAdaptor and both adaptors forward it (test 05813_geojson_query_condition_cache_adaptors). Resolvers that implement IFunctionOverloadResolver themselves do not add a hash, as before.

Choices a reviewer may want to change:

  • GeometryCollection can't be represented by Geometry, so by default it is an error, as in the format, and NULL with input_format_geojson_unsupported_geometry_handling = 'null'. The issue lists seven types; six are supported.
  • With the default format_geojson_validate_geometry = 1, geoJSON rejects shapes that the reader would reject, such as an empty LineString or an unclosed ring, so the output can always be read back.
  • A geometry object in a JSON column is a sub-object, so it is read as j.^geometry; j.geometry gives NULL. This is documented.
  • The JSON type stores integers outside the Int64/UInt64 range as strings, so a JSON value with such a coordinate (for example 123456789012345680000) is rejected. A String argument reads it fine.

Checked locally: the new test 20/20 with random settings; 1M random Float64 bit patterns round-trip exactly through String and through JSON; 200k random shapes of every type round-trip; geoJSON matches the geometry written by the GeoJSON output format; deep nesting gives TOO_DEEP_RECURSION instead of a stack overflow; clang-tidy is clean.

Closes: #126458
Related: #43941

Changelog category (leave one):

  • New Feature

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

Added functions readGeoJSON (alias ST_GeomFromGeoJSON) and geoJSON (alias ST_AsGeoJSON) to convert between a GeoJSON geometry object and the Geometry type.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

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

They convert one GeoJSON geometry object to `Geometry` and back, with the
aliases `ST_GeomFromGeoJSON` and `ST_AsGeoJSON`. The geometry reader and
writer of the `GeoJSON` format become free functions, so both follow the
same rules and settings.

Closes: ClickHouse#126458
@mintlify

mintlify Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit 09090d5.

Approve preview

@pulk17
pulk17 marked this pull request as ready for review November 4, 2026 07:58
@pulk17

pulk17 commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

@alexey-milovidov ready for review. This implements ClickHouse/ClickHouse#126458 and reuses the geometry reader and writer of the GeoJSON format, so the functions follow its rules and settings. Could you approve CI?

One deviation from the issue: GeometryCollection can’t be stored in Geometry, so it is an error by default (NULL with input_format_geojson_unsupported_geometry_handling = 'null'), as in the format. The other choices are listed in the description.

@clickhouse-gh clickhouse-gh Bot added comp-geo Geospatial functions and related libs (e.g., H3, S2, geo distance). external Author is not a member of the ClickHouse organization labels Nov 4, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Nov 14, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [09090d5]

Summary: ✅


AI Review

Summary

Adds readGeoJSON and geoJSON, with their ST_GeomFromGeoJSON and ST_AsGeoJSON aliases, by sharing the existing GeoJSON reader and writer. The earlier structured-input precision, Variant collision, and adaptor settings-hash findings are resolved. Both added stateless tests match their references on the Fast test binary for 09090d560250047a0cdc18562495faf9d66860a3; additional checks of nonconstant inputs, nested Dynamic values, nulls, malformed geometries, and coordinate round trips pass. No remaining findings.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Nov 15, 2026
@pulk17

pulk17 commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

@alexey-milovidov merged master to resolve the conflict in GeoJSONRowInputFormat.h: the discriminator map is now UnorderedMapWithMemoryTracking, as master changed it, because src/Processors/Formats is now checked for standard containers. The function methods are marked [[nodiscard]] for the new clang-tidy check.

The two red checks from the previous run are unrelated: Code Review failed on access to its API key (ssm:GetParameter not allowed for the runner), and test_read_temporary_tables_on_failure/test.py::test_different_versions fails on master too (DID NOT RAISE). 05497_read_geojson_and_geojson_functions passed all 431 runs, including ASan, UBSan, MSan, TSan and the flaky check.

Comment thread src/Functions/geoJSON.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

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

⚠️ Significant changes: object file sizes.

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.

Object file sizes ⚠️

393 object files changed (+630.85 KiB total), 1 added.

Object file Master PR Δ
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/geoJSON.cpp.o new 551.86 KiB +551.86 KiB

716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

2781 translation units recompiled, 10366 s compile time in total, 2780 of them have a recent master baseline.

Translation units without a recent master baseline:

  • src/Functions/geoJSON.cpp: 9.7 s

Job report

The value was converted to text with the output settings of the session, so
`output_format_float_precision` rounded the coordinates. It now uses default
settings, and `Float32` and `BFloat16` in a `Tuple` are widened to `Float64`
first, as a cast does.
Comment thread src/Functions/geoJSON.cpp Resolved
Rewriting the type and casting failed for `Variant` alternatives that
collide or change, and missed `Dynamic` values. Write each `Float32` and
`BFloat16` as `Float64` while walking the value instead.
Comment thread src/Functions/geoJSON.cpp Resolved
The adaptors hold only the overload resolver, which had no `updateHash`,
so the settings a function captured were lost in the expression hash and
the query condition cache mixed results of different settings.
@pulk17

pulk17 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

@alexey-milovidov CI is green now. I merged master to fix the GeoJSONRowInputFormat.h conflict, and since then I've fixed JSON/Tuple argument reading so it doesn't lose precision. 05497 passed all 431 runs across the ASan/MSan/TSan/UBSan and flaky checks. Could you take a look when you have time? Thanks!

This branch has not been deployed

No deployments
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-geo Geospatial functions and related libs (e.g., H3, S2, geo distance). external Author is not a member of the ClickHouse organization pr-feature Pull request with new product feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Scalar functions to convert between GeoJSON geometry and geo types (ST_GeomFromGeoJSON, ST_AsGeoJSON)

2 participants