Skip to content

Add basic support for ISODate("2024-05-29T23:16:12.256Z") JSON parsing - #107650

Open
edugomz wants to merge 61 commits into
ClickHouse:masterfrom
edugomz:feature/mongodb-isodate-json-parsing
Open

edugomz wants to merge 61 commits into
ClickHouse:masterfrom
edugomz:feature/mongodb-isodate-json-parsing

Conversation

@edugomz

@edugomz edugomz commented Jun 16, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #65850

Changelog category:

  • New Feature

Changelog entry

Added support for parsing the MongoDB shell's ISODate("...") and new ISODate("...")
datetime constructor syntax into DateTime64 columns in JSON-based formats such as
JSONEachRow.

The wrapper is accepted only where the target type is already known to be DateTime64
(a declared column, or a DateTime64 arm of Nullable/Variant); it is deliberately not
part of JSON schema inference, so schema inference, the Dynamic and JSON data types and
JSONExtract keep rejecting it. A field that is skipped while reading (an unknown field, or a
declared column that the query does not read) may contain it.

This is the MongoDB shell display syntax, not the output of the official export tools:
mongodump writes BSON (read by BSONEachRow) and mongoexport writes Extended JSON,
where a datetime is an object such as {"$date": "..."}, which is not handled here.
Whether Extended JSON should also be supported is being discussed in the issue.

This PR implements the specific use case described in the issue (parsing
ISODate/new ISODate into DateTime64). The more general new TypeName(...)
constructor-to-type mapping mentioned as an alternative in the issue is left as
future work, as it would need a broader design discussion (constructor registry,
argument parsing for arbitrary types, a new format setting, etc.).

Tests

Stateless tests covering:

  • ISODate(...) and new ISODate(...), with and without a Z suffix
  • Nullable and Variant nested types, including the try parsing path
  • date_time_input_format = 'basic' behavior: Z forces UTC, no suffix stays local, a double Z and a numeric offset are rejected
  • malformed near-misses such as ISODate123, both inside one buffer chunk and split across a tiny read buffer
  • the interaction with a plain numeric timestamp, including a fractional value and input_format_read_datetime_number_as_raw_value

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

@CLAassistant

CLAassistant commented Jun 16, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@edugomz edugomz changed the title Feature/mongodb isodate json parsing Add basic support for ISODate("2024-05-29T23:16:12.256Z") JSON parsing Jun 16, 2026
@edugomz edugomz closed this Jun 16, 2026
@edugomz
edugomz deleted the feature/mongodb-isodate-json-parsing branch June 16, 2026 17:22
@edugomz edugomz reopened this Jun 25, 2026
@yariks5s yariks5s added the can be tested Allows running workflows for external contributors label Jul 1, 2026
@yariks5s yariks5s self-assigned this Jul 1, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [d0478a5]

Summary: ✅


AI Review

Summary

Adds MongoDB shell ISODate and new ISODate parsing for known DateTime64 targets in JSON input, including nested types and discarded fields. Reviewed #107650 at d0478a5e37a28536a53917350648f7dfa742fb31, including all prior discussion and the current implementations. Earlier findings are addressed; no new actionable findings remain. All 48 focused checks against the PR’s Fast test binary behaved as expected, covering buffer boundaries, nested alternatives, strict generic readers, skipped fields, and optimized counts.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Jul 3, 2026
Comment thread src/IO/ReadHelpers.h Resolved
@alexey-milovidov

Copy link
Copy Markdown
Member

Updating the branch to fix the 04001_join_reorder_through_expression and 03532_window_function_and_null_source_max_threads tests, which were broken today in master.

new ISODate in json. Add stateless test to check.
…PeekableReadBuffer`

A non-quoted JSON number never starts with `I` or `n`, so such a token can only be the wrapper and the
checkpoint/rollback to numeric parsing was dead code. Matching the prefix directly keeps a near-miss like
`ISODate123` rejected (now with `CANNOT_PARSE_INPUT_ASSERTION_FAILED`, naming the expected prefix) and
leaves no private `PeekableReadBuffer` memory that could hold unread delimiter bytes.
Merge the 17 one-case tests into `05296_json_isodate_declared_datetime64` (declared `DateTime64`, `Nullable`,
`Variant`, `INSERT`), `05297_json_isodate_basic_input_format` (`date_time_input_format = 'basic'`) and
`05298_json_isodate_malformed`, all with a non-UTC time zone so the `Z` handling is visible.

The removed split-buffer tests used `format` and inline `INSERT` data, which are read from a single in-memory
buffer, so `max_read_buffer_size` never split anything. `05299_json_isodate_small_read_buffer` reads a
multi-row stream through `file` with `max_read_buffer_size` from 1 to 16 instead.
alexey-milovidov and others added 3 commits October 12, 2026 01:59
…`DateTime64` variants

`Variant` enabled `allow_mongodb_isodate_wrapper` unconditionally before choosing a variant, so
an object or an array containing `ISODate(...)` survived tokenization and could then be taken by a
`String` variant through `read_objects_as_strings` / `read_arrays_as_strings`, e.g.
`format(JSONEachRow, 'v Variant(String, DateTime64(3))', '{"v": {"x": ISODate("...")}}')`.

Now the wrapper is tokenized only when some variant contains `DateTime64`, and a value which is
not valid JSON without the wrapper is deserialized with `read_objects_as_strings` and
`read_arrays_as_strings` disabled, so it either reaches a `DateTime64` or is rejected.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`readability-container-contains` and `bugprone-return-const-ref-from-parameter`
fired on `getSettingsForDeserializingJSONField`. Replace it with
`tryDeserializeTextJSONField`, which picks the settings and deserializes the
field itself, so no reference to a parameter is returned.

https://s3.amazonaws.com/clickhouse-test-reports/PRs/107650/34992e03a1bf3b106a4a61295c54d1efe93bcb6d/pr/build_arm_tidy_1_4/build_clickhouse/build_clickhouse.log
PR: ClickHouse#107650

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=107650&sha=caab293f8809c99d1d7fed3d4d80a3c5eb56fb72&name_0=PR&name_1=Stress%20test%20%28arm_asan_ubsan%2C%20s3%29 (Logical error: Cannot call function and because is has 4 arguments but 2 columns were captured in ColumnFunction::reduce via short-circuit and; it has failed in 51 PRs and on master during the last 30 days, and I found no tracking issue) and provide a fix in a separate PR. If the fix is already in progress, link it here.

The other failures on this commit are not related to this PR's ISODate JSON parsing either:

@groeneai

Copy link
Copy Markdown
Collaborator

The fix is #125126 (mine), merged today at 15:34 UTC, after this run. The failed query (from fatal.log) reads system.mutations, whose filter (StorageSystemMutations::fillData -> VirtualColumnUtils::filterBlockWithPredicate) runs on a block with only database and table. splitFilterNodeForAllowedInputs dropped mutation_id and is_done from the inner 4-argument and but kept its 4-argument function, so when the outer short-circuit and evaluated it lazily, ColumnFunction::reduce failed. #125126 rebuilds the reduced and.

I ran the query in clickhouse local with the two MergeTree tables it names: master cb52cc13f18 (before #125126) aborts in ColumnFunction::reduce, and the master CI debug build of the #125126 merge commit 50b17deb63d returns normally. This failure does not come from this PR, and no separate PR is needed.

@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Thanks @groeneai. I merged the latest master (which includes #125126) into this branch, so the next CI run should no longer hit the ColumnFunction::reduce stress failure. The merge built cleanly, and all ten ISODate stateless tests pass locally.

The open CHANGES_REQUESTED review is a product question: should this PR target MongoDB shell ISODate(...) syntax or Extended JSON $date? That is for a maintainer to decide in #65850.

Comment thread src/IO/ReadHelpers.cpp Resolved
Comment thread src/Formats/FormatSettings.h Resolved
alexey-milovidov and others added 2 commits October 30, 2026 17:24
…t` and skipped fields

- `skipJSONField`: choose between `null` and `new ISODate(` by the character after the
  leading `n`, so a failed probe of one alternative no longer consumes a prefix of the other
  (`neull` and `new ISODateull` were accepted as `null`).
- `Variant`: for a value that is valid JSON only with the `ISODate` wrapper, do not disable
  `read_objects_as_strings` / `read_arrays_as_strings` for the whole value. Instead, a new
  internal flag `reject_mongodb_isodate_wrapper_in_strings` makes `String` validate an object
  or array it reads as a raw string, so sibling objects and arrays keep these conversions,
  while a `String` still does not take a value containing the wrapper.
- JSON input formats tolerate the wrapper when skipping a field (`JSONEachRow`,
  `JSONCompactEachRow`, the `count()` path of `JSONColumns`): the format cannot distinguish
  an unknown field from a declared `DateTime64` column pruned from the requested header, and
  `SELECT id FROM file(...)` or `SELECT count()` must not fail on input that
  `SELECT *` reads. Schema inference, `Dynamic`, `JSON` and `JSONExtract` stay strict.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/Formats/JSONUtils.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). and removed comp-datetime Date/DateTime/TimeZone datatypes and date-time semantics. labels Oct 30, 2026
@maxknv maxknv added the external Author is not a member of the ClickHouse organization label Nov 2, 2026
alexey-milovidov and others added 2 commits November 8, 2026 08:51
With `input_format_json_ignore_unknown_keys_in_named_tuple`, an unknown key of a
named `Tuple` is now skipped with the same wrapper-aware settings as an unknown
top-level field (`JSONUtils::getJSONSettingsForSkippingField`), so removing an
unused `ISODate("...")` timestamp from a tuple schema no longer makes the input
unreadable. `Variant` now also tokenizes the wrapper when a variant contains a
named `Tuple` and the setting is enabled; a `String` variant still rejects it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/PRs/107650/57a9d9f4d2f3e03e1011585a7c16e4f8c21239a3/pr/ast_fuzzer_amd_release_oracle/fatal.log (AST fuzzer (amd_release, oracle): "Subquery wrap oracle mismatch" on a FULL OUTER JOIN ... USING of LowCardinality keys with GROUP BY ... WITH ROLLUP and join_use_nulls; unrelated to the JSON ISODate parsing in this PR) and provide a fix in a separate PR. If the fix is already in progress, link it here.

Comment thread src/Formats/JSONUtils.cpp Resolved
@groeneai

groeneai commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator

The fix is in progress in #107127. This is the same bug as #118986: d.id and the USING key id are both output as id, and under SELECT * FROM (...) both columns read d.id, so the right-only row loses its key. LowCardinality, WITH ROLLUP, the windows and join_use_nulls are not needed: SELECT * FROM (SELECT d.id, id FROM (SELECT 100 AS id) AS d FULL OUTER JOIN (SELECT 256 AS id) AS a USING (id)) returns 0 0 on master instead of 0 256.

Measured on the fuzzer's query (join_use_nulls 0 and 1, join_algorithm default and hash, plus the job's settings: join_algorithm = 'direct,parallel_hash,hash', compatibility = '26.3'):

#107127 is open and waiting for review.

alexey-milovidov and others added 2 commits November 17, 2026 21:26
Conflicts: `FormatSettings::JSON` became `JSONFormatSettings` on master, so the
`ISODate` fields moved there; `deserializeTextJSONImpl` keeps the PR's
buffer-boundary-safe null check and now forwards master's `nested_type` to every
`deserializeImpl` call.
…JSON` escaping rule

`CustomSeparated` and `Template` read a declared `DateTime64` through
`deserializeFieldByEscapingRule`, which accepts the MongoDB shell `ISODate("...")`
wrapper, but skipped fields went through `skipFieldByEscapingRule` -> `skipJSONField`
with the strict settings. So an unknown field, `${:JSON}` in `Template`, and the
optimized `count()` failed with `INCORRECT_DATA` on input that reads fine when
the column is declared.

`skipFieldByEscapingRule` now skips `JSON` fields with
`JSONUtils::getJSONSettingsForSkippingField`, like the JSON input formats. The copy
of the settings lives in an out-of-line helper so it does not put a stack canary on
the hot `skipFieldByEscapingRule`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Thanks @groeneai for tracking the AST fuzzer (amd_release, oracle) failure to #107127; it is unrelated to this PR. I merged the latest master (resolving conflicts with the JSONFormatSettings split and the new nested_type parameter in SerializationNullable) and addressed the remaining review thread about skipped fields in the JSON escaping rule.

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 manual approve Manual approve required to run CI pr-feature Pull request with new product feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSON formats: parse new ISODate("2024-05-29T23:16:12.256Z") as DateTime64(3)

8 participants