Skip to content

Add IPv4/IPv6 auto-inference for JSON shared data insertion - #110766

Open
valerypetrov wants to merge 81 commits into
ClickHouse:masterfrom
valerypetrov:feature/json-infer-ip-types
Open

valerypetrov wants to merge 81 commits into
ClickHouse:masterfrom
valerypetrov:feature/json-infer-ip-types

Conversation

@valerypetrov

@valerypetrov valerypetrov commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Closes #110761

Changelog category (leave one):

  • New Feature

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

Added input_format_try_infer_ipv4 and input_format_try_infer_ipv6 settings (both disabled by default) that allow ClickHouse to automatically infer IPv4/IPv6 types from string fields during schema inference for text formats, following the same pattern as input_format_try_infer_dates.


What does this PR do?

Adds two new opt-in format settings that allow ClickHouse to automatically infer IPv4/IPv6 types from string values during schema inference for text formats, following the same pattern as the existing input_format_try_infer_dates / input_format_try_infer_datetimes settings.

New settings

Setting Default Description
input_format_try_infer_ipv4 false Infer IPv4 from strings matching IPv4 address format
input_format_try_infer_ipv6 false Infer IPv6 from strings matching IPv6 address format

How it works

In insertIntoSharedData (JSONExtractTree.cpp), after the existing Date/DateTime inference chain, two new blocks attempt to parse the string as IPv4/IPv6 using the already-existing IPv4Node::tryParse / IPv6Node::tryParse methods. On a match, the value is encoded with its native binary type (BinaryTypeIndex::IPv4/IPv6) instead of falling back to String.

Storage benefit

Value As String As IPv4
"192.168.100.200" 15 bytes 4 bytes
"10.0.0.1" 8 bytes 4 bytes

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

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jul 17, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b251211]

Summary: ❌

job_name test_name status info comment
Stateless tests (arm_asan_ubsan, targeted) FAIL
Server died FAIL cidb
UndefinedBehaviorSanitizer: undefined behavior (STID: 0993-9a84) FAIL cidb
Stateless tests (arm_binary, parallel) FAIL
05257_update_in_partition_dynamic_expression_pinned FAIL cidb
Stateless tests (amd_asan_ubsan, db disk, distributed plan, targeted) FAIL
Server died FAIL cidb
UndefinedBehaviorSanitizer: undefined behavior (STID: 0993-9a84) FAIL cidb
Fast test (arm_darwin) DROPPED
Build (arm_darwin) DROPPED
Build (wasm64) DROPPED
Build (arm_fuzzers) DROPPED
Integration tests (arm_binary, distributed plan, targeted) DROPPED
Integration tests (amd_tsan, targeted) DROPPED
Integration tests (amd_msan, targeted) DROPPED

AI Review

Summary

Adds opt-in IPv4/IPv6 inference for text formats and JSON paths, with mixed-type merging, cache-key updates, and consistent binary serialization. Reviewed head b25121114cfc599c0210d59c71fd2755793d2da6, all prior threads and replies, and the current code. No actionable findings remain. Consumer tracing covered text schema readers and their type mergers; JSONExtract, dynamic paths, and shared data through the existing IP nodes and serializers; Field, row-wise, and bulk binary serialization; schema and query-condition caches; and typed JSON conversions, whose optimized path deliberately preserves typed strings under the existing accepted conversion semantics. Completed Fast test results confirm the dynamic-path and query-condition-cache regressions pass; broader CI is pending. No local tests were run.

Final Verdict

Status: ✅ Approve

Comment thread src/Core/FormatFactorySettings.h Resolved
@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Jul 17, 2026
Comment thread src/Formats/JSONExtractTree.cpp Resolved
Comment thread src/Formats/JSONExtractTree.cpp Resolved
Comment thread src/Formats/JSONExtractTree.cpp Resolved
Comment thread src/Formats/SchemaInferenceUtils.cpp Resolved
Comment thread src/Formats/FormatFactory.cpp Resolved
Comment thread src/Formats/JSONExtractTree.cpp Resolved
Comment thread src/Formats/JSONExtractTree.cpp Resolved
Comment thread src/Formats/JSONExtractTree.cpp Resolved
@valerypetrov

Copy link
Copy Markdown
Contributor Author

@alexey-milovidov, it seems that some of the flaky tests are failing. Do you want me to retrigger CI?

valerypetrov and others added 19 commits July 26, 2026 19:23
…toDynamicPath

- Add tryInferIPv4OrIPv6FromString to SchemaInferenceUtils so IPv4/IPv6
  detection applies to all text-format schema inference (CSV, TSV,
  JSONEachRow column detection, etc.), not just JSON shared-data insertion.
- Fix early-return condition in insertIntoDynamicPath to include the new
  IP inference flags so the String short-circuit does not bypass them.
- Add IPv4/IPv6 variant handling in insertIntoDynamicPath after the
  DateTime64(9) block, with the colon guard on IPv6 to prevent a plain
  IPv4 address from being coerced into ::ffff: form.
- Wire tryInferIPv4OrIPv6FromString into tryInferDataTypeByEscapingRule
  in EscapingRuleUtils.cpp for all three branches (quoted CSV, unquoted
  CSV, raw/TSV), so input_format_try_infer_ipv4/ipv6 works for all text
  formats as intended.
- Call tryInferIPv4OrIPv6FromString from DynamicNode::elementToDataTypeImpl
  in JSONExtractTree.cpp so new JSON dynamic paths and nested structures
  (e.g. Array of IP strings) infer IPv4/IPv6 instead of String.
- Add tests 9-13 covering dynamic-path inference and CSV/TSV inference.
- Add transformIPTypes in SchemaInferenceUtils: IPv4/IPv6 mixed with
  String or with each other collapses to String during schema inference,
  matching the transformDatesAndDateTimes pattern. Prevents TYPE_MISMATCH
  errors when a sample contains both IP and non-IP string values.
- Include try_infer_ipv4/try_infer_ipv6 in getAdditionalFormatInfoByEscapingRule
  so schema-cache keys vary with the new settings; prevents stale cached
  schemas being reused across different inference configurations.
- Add tests 14-15 covering mixed-type collapse behavior.
…ck serializer

The fast path was using writeBinaryLittleEndian(static_cast<UInt32>(ipv4_value)) while
the fallback dynamic_serialization->serializeBinary path uses writeBinary (host byte order)
via SerializationIP<IPv4>::serializeBinary. On big-endian (s390x) these produce different
bytes. Use writeBinary(ipv4_value, buf) to stay byte-identical with the fallback and
consistent with the IPv6 path on the next line.
valerypetrov and others added 3 commits October 10, 2026 10:08
The example additional_format_info string predates many schema inference
settings, including the new try_infer_ipv4 / try_infer_ipv6 fields. Replace
it with the real output of the documented DESCRIBE query on this branch.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Master's version is 26.11 now, and the settings-history check requires a new
setting to have a record under the current version. With the 26.10 records the
Style check fails and every other CI job is dropped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A Dynamic path first tries the variants it already has, without type
conversion. An IPv6 variant inferred from an earlier row accepted plain IPv4
text, so 192.168.1.1 became ::ffff:192.168.1.1 even with IPv4 inference
disabled, and the result depended on row order and on whether the path was
dynamic or in shared data. IPv6 inference needs a colon; the reuse now does too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/Formats/JSONExtractTree.cpp Resolved
Comment thread src/Formats/SchemaInferenceUtils.cpp Resolved
…fer IPs from escaped text

JSONExtract into Dynamic first tries the variants the column already has. That probe
inherited JSONExtract's "insert a default for an invalid element" mode, so an array such
as ["10.0.0.1","not-an-ip"] matched an existing Array(Nullable(IPv4)) variant as
['10.0.0.1',NULL]. The probe now refuses such lossy matches, and a new type is inferred.

Schema inference decodes string escapes before it looks for an IP address, but IP
columns read the raw JSON text, so "192.168.1.1" was inferred as IPv4 and then
failed to read. Strings that contain escapes are no longer inferred as IPv4 or IPv6.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/Formats/EscapingRuleUtils.cpp Resolved
valerypetrov and others added 3 commits October 19, 2026 14:41
The TSKV schema reader decodes escapes before it infers a type, but IP columns read
the raw bytes, so ip=192.168.1.\x31 was inferred as IPv4 and then failed to read.
Such a value already stays a String when it looks like a number; IP addresses now
get the same treatment.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The TSKV schema reader decodes escapes before inference, but the IPv4/IPv6
text readers inside arrays, tuples, maps and variants parse the original bytes
and stop at the backslash. So a value like ['192.168.1.\x31'] inferred
Array(Nullable(IPv4)) and then failed to read with CANNOT_PARSE_IPV4. When the
field had an escape, nested IP types now fall back to String, which is the
schema the same input gets with IP inference disabled.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…omotion

TSKVSchemaReader previously inherited ISchemaReader::transformTypesIfNeeded, which directly invoked tryGetLeastSupertype. Because IPv4 is represented as UInt32, merging IPv4 with Int64 promoted the column to Nullable(Int64) instead of string, causing subsequent reads to fail parsing dotted IP addresses. The same failure occurred in reverse row order, with floating point, boolean, and arrays of IP and numeric values.

Now TSKVSchemaReader overrides transformTypesIfNeeded to run transformInferredTypesByEscapingRuleIfNeeded before generic numeric promotion, ensuring incompatible IP/numeric values (including nested types) convert to String.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
valerypetrov and others added 3 commits October 25, 2026 17:20
TSKVSchemaReader previously ran transformInferredTypesByEscapingRuleIfNeeded unconditionally, which executed generic numeric transformations like transformIntegers and transformIntegersAndFloatsToFloats even when both IP inference settings were disabled. This coerced incompatible numeric samples (e.g. 9007199254740993 and 0.5, or -1 and 18446744073709551615) into Float64 or UInt64 instead of falling back to Nullable(String), losing precision or rejecting negative input.

Now TSKVSchemaReader only performs IP normalization (via transformIPTypes) when IP inference is enabled and at least one type is IP-bearing, preserving the original lossless numeric fallback for non-IP samples.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/Formats/SchemaInferenceUtils.cpp Resolved
…ed, Raw, TSKV and CSV

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@maxknv maxknv added the external Author is not a member of the ClickHouse organization label Nov 2, 2026
valerypetrov and others added 2 commits November 5, 2026 20:18
…ip-types

Master moved the shared data fast path to inferScalar. An inferred IPv4 or
IPv6 string now leaves that fast path and takes the generic path, which
writes it with the IPv4/IPv6 serialization, as the branch did before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
With the default schema_inference_make_columns_nullable the quoted
scalars infer Nullable(String), which LIKE '%String' does not match, so
the test failed in every CI job.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov alexey-milovidov self-assigned this Nov 7, 2026
Comment thread src/Formats/EscapingRuleUtils.cpp Resolved
valerypetrov and others added 3 commits November 23, 2026 17:57
…` under version 26.12

The settings are added in 26.12 now that master has moved to that version,
so their history records must carry it.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
The fast path for strings in `DynamicNode::insertResultToColumn` inserts a
`String` directly when date and datetime inference is off, and otherwise
reuses an existing variant. Both places must take
`input_format_try_infer_ipv4` and `input_format_try_infer_ipv6` into account,
and no existing test covered them: without those conditions an IP address is
stored as `String` when date inference is off, or once the path has a
`String` variant.

Co-Authored-By: Claude Fable 5.2 <[email protected]>

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-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). 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.

JSON type: auto-infer IPv4/IPv6 types from string values during shared data insertion

3 participants