Repository navigation
Accept leading whitespace in readWKT - #110706
PedroTadim merged 6 commits into
Conversation
readWKT detected the geometry type by matching a lowercased prefix (point, linestring, ...) against the raw string before skipping leading whitespace, so an input like ' POINT(1 2)' matched no prefix and threw BAD_ARGUMENTS, while the typed readWKT* readers and the WKT grammar accept the same input. Strip leading whitespace before the prefix match. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-11:20260716-143400 |
|
cc @scanhex12 @nikitamikhaylov — small geo consistency fix: readWKT now trims leading whitespace before the type-prefix match so it accepts the same inputs as the typed readWKT* readers. |
|
@groeneai can you check if other reading functions have this issue? |
| /// The type prefix may be preceded by whitespace, which the WKT grammar (and the typed | ||
| /// readWKT* readers) accept; strip it before matching so readWKT stays consistent. | ||
| auto lower_data = boost::to_lower_copy(data); | ||
| boost::trim_left(lower_data); |
There was a problem hiding this comment.
@groeneai what about whitespaces after the geometry value? Can you add a test case for that?
|
Workflow [PR], commit [039a740] Summary: ❌
AI ReviewSummaryThis PR correctly fixes the whitespace-related Findings
Tests
Final Verdict
|
parseWKTFormat (ArrowGeoTypes.cpp) reads the geometry type keyword from the raw buffer and only trimmed trailing spaces, so a WKT-encoded GeoParquet value with leading whitespace like ' POINT (1 2)' produced BAD_ARGUMENTS on the Arrow / ArrowIPC / Parquet import path, while the same value now parses via readWKT and the WKT grammar. Skip leading whitespace before accumulating the type keyword. This is the import-path sibling of the readWKT fix in this PR (ClickHouse#110700). Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Audited the geometry readers and the type/format dispatchers in One sibling had the same bug: The others are fine:
|
readWKT already accepts trailing whitespace after the geometry value via the WKT grammar, consistently with the typed readWKT* readers. Add regression coverage per review request. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Trailing whitespace already parses fine and matches the typed readers: boost::geometry::read_wkt skips it, and this fix only touched the leading prefix-match dispatch. Added regression cases to 04545 (POINT(1 2) , LINESTRING trailing tab, leading+trailing, and readWKT == readWKTPoint on the trailing-space input). Commit 9b1ab28. |
|
The premise here does not hold empirically, so switching to I checked what the boost WKT grammar (backing both
So
|
CI finish ledger — 9b1ab28All red checks are pre-existing flaky/infra issues unrelated to this readWKT/GeoParquet whitespace fix, each with an owning fixing PR.
Session id: cron:our-pr-ci-monitor:20260716-213000 |
CI finish ledger — 9b1ab28Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only
Session id: cron:our-pr-ci-monitor:20260717-070000 |
| if (current_symbol == '(') | ||
| break; | ||
| /// Skip leading whitespace before the type keyword, consistent with the WKT grammar and readWKT. | ||
| if (!seen_type_char && (current_symbol == ' ' || current_symbol == '\t' || current_symbol == '\n' || current_symbol == '\r')) |
There was a problem hiding this comment.
@groeneai isn't there a isWhiteSpace check function?
There was a problem hiding this comment.
Yes, but the existing helpers accept a wider set than readWKT does, so none matches exactly.
- isWhitespaceASCII (Common/StringUtils.h:127) = space, \t, \n, \r, \f, \v
- skipWhitespaceIfAny (IO/ReadHelpers.h:1934) uses isWhitespaceASCII (same set)
- isWhitespaceASCIIOneLine = space, \t, \f, \v
All three include \f and \v. But readWKT (and the typed readWKT* readers) reject \f/\v: boost::geometry read_wkt skips only space/\t/\n/\r, and our try_deserialize_type only left-trims the lowercased copy used for the prefix match, so the untouched original still hits the boost grammar. A/B on the debug build: readWKT and readWKTPoint both return Code 6 CANNOT_PARSE_TEXT for a leading \f or \v.
So the explicit space/\t/\n/\r list here is intentional: it matches readWKT exactly. Switching to isWhitespaceASCII/skipWhitespaceIfAny would make GeoParquet WKT import accept \fPOINT(1 2) while readWKT rejects it, i.e. re-introduce the divergence this PR removes.
Happy to swap to a helper if you prefer readability over the \f/\v alignment; there is just no existing helper that matches readWKT's space/\t/\n/\r class, so it would need a small local one.
parseWKTFormat only handled a single space between coordinates and only skipped ' ' when trimming the type keyword, while readWKT (boost WKT grammar) treats ' \t\n\r' as token separators throughout. So GeoParquet WKT imports like 'POINT\t(1 2)', 'POINT(1 2)' threw BAD_ARGUMENTS / CANNOT_PARSE_NUMBER, and 'LINESTRING(1 1,\n2 2)' silently dropped a coordinate, even though readWKT parsed all of them. Normalize the same ' \t\n\r' class (not isWhitespaceASCII, which also matches \f and \v that read_wkt rejects) at the type-keyword and coordinate boundaries. Widen the 04546 regression test with interior whitespace cases.
Head branch was pushed to by a user without write access
…ng readWKT
parseWKTFormat silently accepted inputs that readWKT rejects: non-separator
bytes before '(' (POINT x(1 2)), trailing data after the geometry
(POINT(1 2) trailing, POINT(1 2))), garbage inside the coordinate list
(LINESTRING(1 1 xx, 2 2)), and a missing ')'. readOpenBracket/readItemEnding
skipped arbitrary bytes and the POINT branch never consumed its ')', so
GeoParquet WKT import produced wrong or partial geometries instead of an error.
Make the token boundaries strict (only ' \t\n\r' separators allowed), consume
the closing ')' for POINT, and require the value to end after the geometry.
Whitespace class unchanged; valid values including leading/interior/trailing
separators still parse identically to readWKT.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
| type.pop_back(); | ||
|
|
||
| GeometricObject result; | ||
| if (type == "POINT") |
There was a problem hiding this comment.
This still doesn't fully match the readWKT contract you're aligning to. boost::geometry::read_wkt matches the geometry keyword with boost::iequals and handles EMPTY before the coordinate parser runs, but this path only accepts the exact uppercase bare tags. As a result, values like point(1 2) or LINESTRING EMPTY continue to work through SQL readWKT, while WKT-encoded GeoParquet import still throws BAD_ARGUMENTS here.
Can we canonicalize the type token case-insensitively and teach this entry point the same EMPTY handling, or just reuse readWKT for WKT decoding so the two parsers actually share one grammar?
There was a problem hiding this comment.
The divergence is partly real, but "reuse readWKT" / mirror its EMPTY handling is unsafe here. A/B on the current build (SQL readWKT, which uses boost::geometry::read_wkt, as the oracle vs the GeoParquet parseWKTFormat path):
| input | readWKT (SQL) | parseWKTFormat (GeoParquet) |
|---|---|---|
point(1 2) (lowercase) |
(1,2) |
Code 36 BAD_ARGUMENTS |
LINESTRING EMPTY |
[] |
Code 36 |
POLYGON EMPTY |
[[]] |
Code 36 |
POINT EMPTY |
(6.95e-310, 6.95e-310) [1] |
Code 36 |
[1] readWKT('POINT EMPTY') returns different uninitialized floats on every run (6.9530e-310, 6.9531e-310, 6.9532e-310 across three runs); boost has no defined value for an empty point, so this is an uninitialized read. Mirroring readWKT for POINT EMPTY would import garbage coordinates.
So the two genuine gaps are the case-insensitive type keyword and container EMPTY (LINESTRING / POLYGON / MULTI*). Both are inputs the importer currently rejects with a clear BAD_ARGUMENTS: no silent wrong data, unlike the interior-whitespace and trailing-garbage rounds that were fixed here. Accepting lowercase keywords and EMPTY geometries is a feature expansion of the GeoParquet WKT importer beyond this PR's whitespace-consistency scope (Closes #110700), and it needs the POINT EMPTY uninitialized read in readWKT resolved first. Tracking it as a follow-up rather than widening this already-approved PR. No code change this round.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 65/67 (97.01%) · Uncovered code |
d529ed8
|
Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment. TL;DRAccept leading whitespace in readWKT ClickGap verified: reproduced it with the script below on ReproductionSELECT readWKT(' POINT(1 2)');
-- pre-fix (all supported releases 26.1-26.6): Code: 36 BAD_ARGUMENTS "Incorrect WKT format value: POINT(1 2)".
-- expected (post-fix, matching readWKTPoint(' POINT(1 2)') -> (1,2)):
SELECT readWKT(' POINT(1 2)') = readWKTPoint(' POINT(1 2)'); -- 1Bisect
Affects
Suggested next stepThe fix has landed on master; the affected release branches above still need the backport. Component: cc fix PR author: @groeneai ClickGap · Finding: Triage metadata
|
|
Hi @groeneai @PedroTadim — a couple of suggestions to make this PR easier for reviewers and the changelog reader. Description (currently
Ignore this if the current title/body is intentional. |
|
Thanks. Applied the description suggestion: the PR body now documents the On the backport: this is confirmed long-standing (repros on 26.1-26.6, clean on master), but backport labeling is a maintainer call, so leaving Separately, the GeoParquet WKT importer still diverges from |
parseWKTFormat decodes WKT geometry columns for GeoParquet, Arrow and ArrowStream. Its grammar was missing two productions that the WKT spec allows and that the readWKT function accepts, so valid files failed to import: point(1 2) Code 36: type point LINESTRING EMPTY, POLYGON() Code 36 / Code 72 The empty-container gap also broke a round trip through ClickHouse's own functions, since wkt emits LINESTRING() and POLYGON() for empty geometries. The WKB branch of this same importer already accepted every one of these empty geometries with the same values, so the WKT branch was the only outlier. Three changes: the type keyword is uppercased before dispatch; readWKTEmptyToken accepts EMPTY in place of the coordinate list, only right after the type keyword, so POLYGON(EMPTY) keeps failing as it does in readWKT; checkWKTEmptyList accepts () as an empty element list in each container reader, which is valid at every nesting depth. readWKTEmptyToken reads the whole token and reports an unrecognized one rather than leaving it half-consumed, so LINESTRING E(1 1, 2 2) stays rejected instead of having its stray E swallowed and the rest parsed. POINT EMPTY, POINT() and MULTIPOINT(()) stay rejected: a Point is Tuple(Float64, Float64) with no empty representation, and readWKT returns fabricated coordinates for the latter two, which must not be imported. The change only widens acceptance, every input that imported before still imports with the identical value. Related: ClickHouse#110706 Co-Authored-By: Claude Fable 5 <[email protected]>
parseWKTFormat decodes WKT geometry columns for GeoParquet, Arrow and ArrowStream. Its grammar was missing two productions that the WKT spec allows and that the readWKT function accepts, so valid files failed to import: point(1 2) Code 36: type point LINESTRING EMPTY, POLYGON() Code 36 / Code 72 The empty-container gap also broke a round trip through ClickHouse's own functions, since wkt emits LINESTRING() and POLYGON() for empty geometries. The WKB branch of this same importer already accepted every one of these empty geometries with the same values, so the WKT branch was the only outlier. Three changes: the type keyword is uppercased before dispatch; readWKTEmptyToken accepts EMPTY in place of the coordinate list, only right after the type keyword, so POLYGON(EMPTY) keeps failing as it does in readWKT; checkWKTEmptyList accepts () as an empty element list in each container reader, which is valid at every nesting depth. readWKTEmptyToken reads the whole token and reports an unrecognized one rather than leaving it half-consumed, so LINESTRING E(1 1, 2 2) stays rejected instead of having its stray E swallowed and the rest parsed. POINT EMPTY, POINT() and MULTIPOINT(()) stay rejected: a Point is Tuple(Float64, Float64) with no empty representation, and readWKT returns fabricated coordinates for the latter two, which must not be imported. The change only widens acceptance, every input that imported before still imports with the identical value. Related: ClickHouse#110706 Co-Authored-By: Claude Fable 5 <[email protected]>
…kt-leading-whitespace Accept leading whitespace in readWKT
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
readWKTrejecting WKT strings with leading whitespace (e.g.readWKT(' POINT(1 2)')), which are accepted by the typedreadWKTPoint/readWKTPolygon/... readers and by the WKT grammar.Description
readWKTdetected the geometry type by matching a lowercased prefix (point,linestring, ...) against the raw string before skipping leading whitespace, so' POINT(1 2)'matched no prefix and the loop threwBAD_ARGUMENTS, whilereadWKTPoint(' POINT(1 2)')parses fine. Fix:boost::trim_leftthe lowercased string before the prefix comparison soreadWKTaccepts the same inputs as the typed readers.This PR also rewrites the hand-rolled WKT parser used by the native GeoParquet reader (
parseWKTFormatinArrowGeoTypes.cpp): a shared separator-skipping helper, strict validation of(/)/ item separators (throwingBAD_ARGUMENTSinstead of silently scanning), tolerance for interior whitespace between the type keyword and coordinates, and rejection of trailing data after the geometry, making GeoParquet WKT import consistent withreadWKT. Note: some values previously silently accepted on GeoParquet import (e.g. 3DPOINT(1 2 3),POINT x(1 2), trailing garbage) are now rejected.Closes: #110700