Skip to content

GeoJSON input format - #98124

Merged
nihalzp merged 85 commits into
ClickHouse:masterfrom
mneedham:geojson-input-format
Jun 19, 2026
Merged

nihalzp merged 85 commits into
ClickHouse:masterfrom
mneedham:geojson-input-format

Conversation

@mneedham

@mneedham mneedham commented Feb 26, 2026 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • New Feature

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

Add the GeoJSON input format for reading GeoJSON FeatureCollection documents, producing one row per feature with id (String), geometry (Geometry), and properties (Nullable(JSON)) columns. Point, LineString, MultiLineString, Polygon, and MultiPolygon geometries are read into ClickHouse's native Geometry type. GeometryCollection and MultiPoint, which the Geometry type cannot represent, raise an exception by default, or can be stored as NULL via the input_format_geojson_unsupported_geometry_handling setting. Contributes to #91533.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.6.1.1025

mneedham and others added 4 commits February 26, 2026 13:42
Adds a new `GeoJSON` input format that reads a GeoJSON FeatureCollection
and yields one row per feature with a fixed schema:
- `id Nullable(String)` — from `feature.id` if present
- `geometry Geometry` — parsed into ClickHouse's `Geometry` variant type
- `properties JSON` — the feature's properties object

The format is gated behind `allow_experimental_geojson_format = 1`.

Also adds `MultiPoint` to the `Geometry` variant type in `DataTypeCustomGeo`,
since it was a legitimate GeoJSON geometry type that was previously missing.
`GeometryCollection` is not supported and throws an exception with a
descriptive message.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
- Add `done` flag to `GeoJSONRowInputFormat` so that once `]` of the
  features array is consumed, subsequent calls to `readRow` (which
  occur when `IRowInputFormat::read` is called for a second block)
  return `false` immediately instead of attempting to read past the
  end of the `features` array into the FeatureCollection closing `}`,
  which caused a `CANNOT_PARSE_INPUT_ASSERTION_FAILED` exception.
- Pass the required `FormatSettings::JSON` third argument to
  `readJSONString` in `skipJSONValue` and `readGeometry`.
- Update test to use `INSERT INTO … FORMAT GeoJSON` for the empty
  features array case, working around the known ClickHouse limitation
  where `format()` with zero rows raises `NOT_FOUND_COLUMN_IN_BLOCK`.
- Add `.reference` file with expected test output.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Replace the hand-rolled `skipJSONValue` helper (which incorrectly used
`JSONUtils::skipTheRestOfObject` — a function that calls `skipComma`
before the first key-value pair, assuming it is called mid-object) with
the existing `skipJSONField` from `IO/ReadHelpers.h`, which correctly
skips any JSON value type including nested objects and arrays.

This fixes a `CANNOT_PARSE_INPUT_ASSERTION_FAILED` exception when
parsing real-world GeoJSON files that contain extra top-level keys
before `features` (e.g., `crs`, `name`).

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Adds the `input_format_geojson_geometry_collection_handling` setting to
control behaviour when a `GeometryCollection` geometry type is
encountered in `GeoJSON` input. Since `GeometryCollection` cannot be
represented in ClickHouse's `Geometry` type, the options are:

- `'throw'` (default) — throw an `INCORRECT_DATA` exception.
- `'null'` — insert a `NULL` value for the `geometry` column and
  continue parsing.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Feb 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0a3fd0b]

Summary: ✅


AI Review

Summary

This PR adds the GeoJSON input format for FeatureCollection data, with fixed schema inference, strict feature and geometry validation, configurable handling for unsupported geometry types, documentation, and stateless coverage. I rechecked the current parser and the prior bot review threads against the PR head; the earlier concerns around header validation, duplicate members, strict JSON separators and numbers, required feature members, GeometryCollection validation, and unrequested unsupported geometries are addressed. I do not have remaining findings.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-experimental Experimental Feature label Feb 26, 2026
mneedham and others added 7 commits February 26, 2026 17:34
Documents the experimental `GeoJSON` input format: fixed output schema
(`id`, `geometry`, `properties`), example usage with `file()` and
`INSERT`, schema inference, and the
`input_format_geojson_geometry_collection_handling` setting.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
The MATERIALIZED column referenced `properties` which was not defined
in the table. Added `properties JSON` to the schema so the expression
is valid.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Replace generic 'Documentation for the GeoJSON format' with a
meaningful meta description that appears in search results.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Adds words introduced by the GeoJSON input format to the aspell
ignore list to fix the style check:

- `GeoJSON`, `geojson`
- `FeatureCollection`
- `GeometryCollection`
- `MultiPoint`

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
The id field in GeoJSON features is infrequently absent; using
Nullable(String) complicates ORDER BY key usage. Changed the
inferred schema to plain String, with an empty string default for
missing ids. Updated the test and documentation accordingly.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Updates the setting description in Settings.cpp and the class comment
in GeoJSONRowInputFormat.h to reflect that id is String, not
Nullable(String).

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
The custom readRawJSONValue function was doing the same thing as the
existing readJSONField utility in IO/ReadHelpers.h: reading any JSON
value (object, array, string, scalar) into a String. Replace it with
the standard utility and remove the now-unused WriteBufferFromString
include.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@alexey-milovidov alexey-milovidov changed the title Geojson input format GeoJSON input format Feb 26, 2026
@scanhex12 scanhex12 self-assigned this Feb 26, 2026
mneedham and others added 15 commits February 27, 2026 07:27
…N test

Three fixes:

1. Add `allow_experimental_geojson_format` and
   `input_format_geojson_geometry_collection_handling` to
   `SettingsChangesHistory.cpp` under 26.3 to fix
   `02995_new_settings_history`.

2. Remove `MultiPoint` from the `Geometry` variant type and the
   `DataTypeMultiPointName` class. `DataTypeVariant` sorts its sub-types
   alphabetically by name, so adding `MultiPoint` shifted the
   discriminants of `MultiPolygon`, `Point`, `Polygon`, and `Ring` by
   one. This broke `readWkt.cpp` (which hard-codes the old 6-type
   discriminant order) and caused `03444_flip_coordinates` to fail.

3. Remove the dead `MultiPoint` coordinate-parsing branch from
   `GeoJSONRowInputFormat::readGeometry` since the type is no longer
   in the `Geometry` variant and the branch was never reachable.

Fixes: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=98124&sha=eadc51a3b9f7213129f7964aaf826d266096b6da&name_0=PR&name_1=Fast+test&name_2=Tests

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Fix two CI failures in PR ClickHouse#98124:

1. `arm_tidy`: Split `Float64 lon, lat;` into two separate declarations
   in `readGeoJSONPoint` to fix the `readability-isolate-declaration`
   clang-tidy error (treated as `-warnings-as-errors` in the ARM tidy build).

2. Fast test `04003_geojson_input_format`: The reference file was generated
   with `PrettyCompactMonoBlock` output format (due to a local client config
   override), but CI runs `clickhouse-test` with stdout redirected to a file
   which defaults to `TabSeparated`. Regenerated the reference file in
   `TabSeparated` format to match CI output.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
With `async_insert = 1` enabled, the `clickhouse-client --multiquery`
connection terminates silently after an INSERT with inline FORMAT data,
causing all subsequent queries to be skipped. The `SELECT count()` after
the empty GeoJSON insert never ran, so its output was missing from
stdout, producing a "result differs with reference" failure in the
`Stateless tests (amd_debug, AsyncInsert, s3 storage, parallel)` run.

Add `SET async_insert = 0` before the CREATE/INSERT/SELECT block that
tests the empty-features case. This test is about the GeoJSON format
behaviour, not about async inserts, so disabling `async_insert` locally
is the correct fix.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Replace manual token parsing with the utility functions already
available in `JSONUtils`:

- `skipArrayStart` / `checkAndSkipArrayEnd` instead of bare
  `assertChar('[')` and `while (!eof && *pos != ']')` loops
- `skipObjectStart` / `checkAndSkipObjectEnd` instead of bare
  `assertChar('{')` and `while (!eof && *pos != '}')` loops
- `skipComma` / `checkAndSkipComma` instead of
  `if (*pos == ',') ++pos`
- `skipTheRestOfObject` in `readSuffix` instead of the equivalent
  open-coded loop

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Three more simplifications on top of the previous JSONUtils refactor:

- Template the three nested-array coordinate readers
  (`readGeoJSONLinearRing`, `readGeoJSONPolygonCoordinates`,
  `readGeoJSONMultiPolygonCoordinates`) using a single
  `readGeoJSONArray` helper, reducing them to one-liners.

- Replace the manual `'n'`-check + `insertDefault` blocks for the
  `id` and `properties` fields with
  `SerializationNullable::deserializeNullAsDefaultOrNestedTextJSON`,
  which is the standard way other JSON-based formats handle nullable
  values in non-nullable columns.

- Simplify the null check at the top of `readGeometry` by using
  `JSONUtils::checkAndSkipObjectStart`: if `{` is not found the value
  must be `null`, so we assert and insert default; otherwise the `{`
  is already consumed and the field loop follows immediately.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…rmat

Add two local helpers in the anonymous namespace:

- `forEachFieldInJSONObject(buf, settings, handle_field)` — iterates
  every field of an already-opened JSON object, calling
  `handle_field(key)`. If the callback returns true the value was
  consumed; otherwise the value is skipped with `skipJSONField`.
  Used in `readRow` and `readGeometry`.

- `findFieldInJSONObject(buf, settings, key)` — scans a JSON object
  for one specific field, skipping all others, and returns true with
  the read position at the field value (or false after `}`).
  Used in `readPrefix` to locate the `"features"` array.

This removes the three near-identical
`while (!checkAndSkipObjectEnd) { readFieldName; ... else skipJSONField;
checkAndSkipComma; }` loops that previously appeared independently.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
The `GeoJSON` format no longer requires setting `allow_experimental_geojson_format = 1`.
Removes the `allow_experimental_geojson_format` setting, the `SUPPORT_IS_DISABLED` guard
in `GeoJSONRowInputFormat`, and all related test scaffolding.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Adds a shell test that loads a 258-feature GeoJSON FeatureCollection of world
country boundaries (source: https://datahub.io/core/geo-countries/r/0.geojson)
and verifies count, geometry type distribution (`Polygon`: 107, `MultiPolygon`:
151), and country name lookup by ISO 3166-1 alpha-2 code.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
- Remove the experimental note and `allow_experimental_geojson_format` requirement
- Update examples to use `properties.name` instead of `JSONExtractString`
- Add example showing automatic format detection from `.geojson` extension
- Add example querying an ingested table
- Rewrite format settings section as a dedicated `GeometryCollection` section

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Replace the two-Point cities example with a `berlin.geojson` file that
mixes `Point`, `LineString`, and `Polygon` features with richer properties.
This better demonstrates the `Geometry` variant type and `variantType`
function. Adds a `WHERE variantType(geometry) = 'Point'` filter example
and a materialized `feature_type` column in the table ingestion example.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Replace the Berlin example with a London dataset covering Tower of London
(Point), River Thames (LineString), and Hyde Park (Polygon), with richer
properties including `year_built`, `length_km`, and `area_km2`.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Show how to use `variantType` to inspect geometry types per row, and how to
access the underlying geometry data using the `geometry.Point`,
`geometry.LineString`, and `geometry.Polygon` variant sub-column syntax.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
`aspell-dict.txt` was deleted in master along with the `aspell` and
`codespell` style checks. Removing the file here so the PR can merge
cleanly without a modify/delete conflict.
@clickhouse-gh

clickhouse-gh Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.20% 85.20% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.50% 77.50% +0.00%

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

Full report · Diff report

@nihalzp
nihalzp added this pull request to the merge queue Jun 19, 2026
Merged via the queue into ClickHouse:master with commit f3d0c03 Jun 19, 2026
485 of 490 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 19, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi @mneedham @nihalzp — while reviewing this PR I found the following:

Could you review the test PR and add the can be tested label if it looks good?
Happy to discuss — close anything that's wrong or already addressed.

@nihalzp nihalzp mentioned this pull request Jun 21, 2026
pull Bot pushed a commit to Haofei/ClickHouse that referenced this pull request Jun 25, 2026
alexey-milovidov added a commit that referenced this pull request Jul 23, 2026
Query forwarding to external databases (#107740) is based on Denis
Vidiaev's #79652, the GeoJSON output format (#108065) on Mark Needham's
#98124, and SZ3 (#108788) revives Konstantin Vedernikov's #83088.

Co-Authored-By: Claude Fable 5 <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Query forwarding to external databases (ClickHouse#107740) is based on Denis
Vidiaev's ClickHouse#79652, the GeoJSON output format (ClickHouse#108065) on Mark Needham's
ClickHouse#98124, and SZ3 (ClickHouse#108788) revives Konstantin Vedernikov's ClickHouse#83088.

Co-Authored-By: Claude Fable 5 <[email protected]>
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 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.

7 participants