Repository navigation
Write Delta timestamp_ntz columns as unadjusted Parquet timestamps - #120863
alexey-milovidov merged 11 commits into
Conversation
An INSERT into a Delta table whose schema declares a timestamp_ntz column committed a data file that annotated the column Timestamp(isAdjustedToUTC=true, MICROS). Delta defines timestamp_ntz as an unadjusted wall clock timestamp, so Spark refused every later read of the table with java.lang.RuntimeException: Unable to create Parquet converter for data type "timestamp_ntz" whose Parquet type is int64 time(TIMESTAMP(MICROS,true)) Two independent facts produced this. The kernel schema visitor wired visit_timestamp and visit_timestamp_ntz to the same visitor, so both Delta types became DateTime64(6) and the distinction was gone before the writer saw it; ClickHouse has no wall clock type that could carry it either. The Parquet writer then hardcoded isAdjustedToUTC=true at both timestamp branches, which is right for the general Parquet format because a DateTime64 is an instant, but wrong for a column the Delta log declares timestamp_ntz. Rather than consult the DateTime64 timezone parameter, which would change every general-format write and contradict the rule settled in ClickHouse#87872, the distinction is kept where it is produced and delivered where it is needed. The visitor records is_timestamp_ntz on its field, the traversal collects the dotted paths of those leaves, TableSnapshot caches them with the rest of the schema, and the Delta write path hands them to the writer through ColumnMapper, which already carries per-path lake facts that a ClickHouse type cannot express (iceberg_string_paths, iceberg_optional_paths). Nested paths are spelled the way the Parquet writer spells a column path (a struct field's own name, element for an array child, key and value for a map's) rather than reusing the kernel's child names, which are not guaranteed to be those words. Only the annotation changes; the stored int64 is identical, so the ClickHouse round trip stays lossless. This is the inverse of the documented read-side rule that reads an unadjusted timestamp as DateTime64(N, 'UTC'). The mapper is attached only when the table actually has a timestamp_ntz column and only for Parquet, so a Delta table without one keeps its exact current behaviour, and ORC and Avro never see a mapper they would read as Iceberg metadata. ParquetBlockOutputFormat now passes std::nullopt when the mapper's field-id map is empty: an engaged but empty optional makes convertSchema report the first column as having no Iceberg field id, so a mapper carrying only timestamp paths would otherwise break every Delta INSERT. An Iceberg mapper always has an id per field, so Iceberg is unaffected. The shared annotation seam is shaped identically to the in-flight ClickHouse#118601, which adds it for Iceberg but does nothing for Delta, so either merge order is a mechanical rebase. Tests: two integration tests in test_storage_delta covering the flat case (with Nullable, a Delta timestamp control and a char/varchar column) and the nested case (struct field, array element, map value, plus a Delta timestamp sibling inside the same struct), both asserting the _delta_log types first so they cannot pass vacuously, the written annotation, a Spark read and the ClickHouse round trip. 02735_parquet_encoder gains a logical_type assertion because desc file() reports DateTime64(N, 'UTC') for both flag values, so nothing on master would have caught a global flip of the default. Closes ClickHouse#120653.
A Delta field name may contain a dot, and such a table is writable today: the write path rejects column mapping, not dotted names, and the read side is already tested on that shape. So a struct like sc STRUCT<`a.b`: timestamp_ntz, a: STRUCT<b: timestamp>> flattens both of its leaves onto the writer path sc.a.b, and annotating from that path gave the Delta timestamp leaf isAdjustedToUTC=false, which is the same contradiction between data file and Delta log that this branch fixes for timestamp_ntz, only in the other direction. The traversal now also records the writer path of every field that is not a timestamp_ntz leaf, and subtracts that set once the whole tree is known. A path claimed by two fields therefore selects nothing and keeps the default annotation, which is never wrong for a Delta timestamp column and leaves the reported bug unfixed only for a table whose field names contain dots. The alternative, an escaped or otherwise injective path spelling, would have to change the Nested::concatenateName paths that iceberg_optional_paths, iceberg_string_paths and column_field_ids already share, i.e. alter Iceberg write behaviour to serve a pathological Delta schema. Take the annotation from the transaction, not the snapshot The paths were read from the snapshot, which is a cache lookup and can be pinned outright with delta_lake_snapshot_version, while an unpartitioned write shapes its data file with the schema of the transaction, which the kernel always opens on the latest snapshot. validateSchema compares column names only, and everything else in the write path re-derives itself from that fresh write schema; the timestamp_ntz bit was the one fact that could not self-heal, because both Delta types collapse to DateTime64(6). getWriteSchema (one caller) now returns the paths beside the schema it visited, and WriteTransaction mirrors write_schema's provenance exactly: the transaction's own write context when unpartitioned, the snapshot set passed at construction when partitioned, which is the branch that already uses the table schema. Give the partitioned sink an oracle Both new tests created unpartitioned tables, so DeltaLakePartitionedSink's plumbing could be reverted to nullptr with the suite green while a partitioned Delta table kept committing isAdjustedToUTC=true. The flat test is now partitioned on id and the nested one stays unpartitioned, so each sink is covered by a test that exists, and the two annotation provenances above are exercised once each. The nested test carries the dotted-name collision and reads that column in its own Spark projection, because a column annotated as on master is exactly what Spark cannot convert. Co-Authored-By: Claude Opus 5 <[email protected]>
A Delta field name may contain a dot, so two fields can flatten to the same Parquet writer path. The guard that keeps such a path unannotated recorded the path of every non-timestamp_ntz field, containers included. But the annotation is looked up in preparePrimitiveColumn only, so a struct, array or map GROUP that lands on a leaf's path never performs a lookup and is not a competing claim; pruning on it threw away a correct annotation and left the reported interop defect unfixed for STRUCT<`a.b`: timestamp_ntz, a: STRUCT<b: STRUCT<..>>>, a shape the suite already exercises for dotted names without column mapping. Only a leaf now records a claim, which is what the member's own comment says. Also, for a partitioned table the write schema and these paths come from the snapshot, while the transaction always commits against the latest version (delta-kernel-rs exposes no partitioned write context over FFI). With delta_lake_snapshot_version pinned, the snapshot can declare a column that the current schema declares `timestamp`, so annotating from it would write isAdjustedToUTC=false into a table declaring an instant: a new way to produce exactly the unreadable table this change exists to prevent. A pinned snapshot therefore annotates nothing and the file is written as before. Measured first that such an INSERT is accepted rather than rejected, so the case is reachable. The nested test gains the leaf-versus-group column and the partitioned test a pinned second INSERT; each has a build with only its own hunk reverted in which only its own assertion fails. A test comment claiming a Nullable versus non-Nullable writer-path pair was wrong and now states what the pair proves: the write header replaces both columns with the Delta schema's nullable type, so both ClickHouse declarations end up annotated.
…tted The timestamp_ntz annotation was skipped for every INSERT that pinned delta_lake_snapshot_version, not just for one whose pinned schema differs from the current one. A write pinned to the version the transaction commits against is not skewed, so that discarded a correct annotation and committed exactly the data file this fix exists to prevent: int64 micros annotated isAdjustedToUTC=true under a Delta schema declaring timestamp_ntz, which Spark refuses to convert. The transaction always commits against the latest table version, so the predicate now compares the pinned version against a freshly loaded latest snapshot and annotates only when they are equal; an older pin keeps the previous behaviour. The latest snapshot is constructed directly rather than through getTableSnapshot(std::nullopt), which falls back to the cached latest_snapshot_version that update() deliberately does not refresh for a pinned query -- the written annotation would otherwise depend on session history. The extra load introduces no new failure mode, because WriteTransaction::create() already has the kernel open the table at its latest version. The partitioned test's pinned leg now asserts the annotation and reads the whole table back through Spark, proving the file is conforming and not merely annotated; a second leg pins an older version and asserts the unchanged UTC-adjusted default. Two negative controls pin the predicate: removing the guard reddens the older-pin leg, and restoring the previous coarse form reddens the current-pin leg.
Delete comments that narrate this change (what a caller passes, why a guard is here, refuted alternatives) and shorten the rest; keep the property comments a reader cannot derive from the code.
Drop pointer comments duplicated at the definition site and shorten the survivors.
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 4 review rounds, 13 findings across my own
Known limitation, stated rather than fixed (finding 5). For a partitioned table the data file is Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20260918-145000 |
Pre-PR validation gate (click to expand)
Validation: 2 new integration tests x 5 consecutive runs (10/10), Session id: cron:clickhouse-impl-slot-6:20260918-141000 |
|
Workflow [PR], commit [bf63649] Summary: ✅
AI ReviewSummaryThis PR carries Delta Findings❌ Blockers
Final VerdictRequest changes — the partitioned Delta write path still has a correctness hole that can recreate unreadable |
| if (snapshot_version.has_value()) | ||
| { | ||
| auto latest_snapshot = std::make_shared<DeltaLake::TableSnapshot>( | ||
| /* version */ std::nullopt, kernel_helper, object_storage, log); | ||
| if (latest_snapshot->getVersion() == *snapshot_version) | ||
| table_timestamp_ntz_paths = snapshot->getTimestampNtzPaths(); | ||
| } | ||
| else | ||
| table_timestamp_ntz_paths = snapshot->getTimestampNtzPaths(); | ||
|
|
||
| auto delta_transaction = std::make_shared<DeltaLake::WriteTransaction>( | ||
| kernel_helper, snapshot->getTableSchema(), std::move(table_timestamp_ntz_paths)); |
There was a problem hiding this comment.
The new latest_snapshot->getVersion() == *snapshot_version check only proves the pin was current when we looked; it does not bind the transaction to that snapshot. On the partitioned path, WriteTransaction::create still reuses snapshot->getTableSchema() / table_timestamp_ntz_paths, while ffi::commit publishes against whatever schema is latest when the commit actually runs.
That means a schema-changing concurrent overwrite can still make us commit a file under a different schema. In the timestamp_ntz -> timestamp direction this branch is worse than master: we can publish isAdjustedToUTC=false and make Spark reject the new file again. The test added below also locks in the stale-pin case that deliberately writes an unreadable file. I think this path needs to fail closed for partitioned writes when delta_lake_snapshot_version is set, or use a transaction API that is actually bound to the caller's snapshot.
There was a problem hiding this comment.
I checked both halves of this against the pushed tree. The exposure is real, and it is the limitation already stated in the known-limitation paragraph of #120863 (comment), but two of the premises and both proposed remedies do not survive measurement.
Commit time. The generated FFI documents commit as surfacing "conflicted and retryable CommitResult variants as errors today", so a concurrent commit landing after ffi::transaction ("Start a transaction on the latest snapshot of the table", called at WriteTransaction.cpp:175) fails the INSERT rather than re-basing it. The unguarded interval is the one between the snapshot read (DeltaLakeMetadataDeltaKernel.cpp:646-647) and that open, not "whatever schema is latest when the commit actually runs".
The pin does not create that interval. For an unpinned partitioned INSERT the snapshot is the one refreshed earlier in the same query (StorageObjectStorage.cpp:815-819 deliberately skips the refresh for a data lake because getExternalDynamicMetadata already did it), and both the write schema (WriteTransaction.cpp:200, unchanged from master) and the annotation (:201) are read from that same object. Failing closed only when delta_lake_snapshot_version is set would therefore reject an INSERT that succeeds on master today, while leaving the identical interval on the default path.
A transaction bound to the caller's snapshot is not reachable today. transaction_with_committer(snapshot, engine, committer) consumes a MutableCommitter, and the FFI's only producer is get_uc_committer, which needs a Unity Catalog commit client from get_uc_commit_client; a filesystem or S3 Delta table cannot build one. There is also no accessor for an uncommitted transaction's base version (only committed_transaction_version, post-commit), so the tighter check of the pin against the transaction's own version is not expressible either. Either route changes commit and conflict semantics for every Delta write, which is why I reported this residual for a maintainer ruling instead of guessing at it.
The test. The stale-pin leg asserts what master writes for that same INSERT, says so on the line above the assertion, and runs no Spark read afterwards. It pins the fail-open branch so that a later change cannot silently start annotating a stale pin, which is the direction that would make a timestamp_ntz to timestamp schema change produce a file master would have written correctly. Rejecting a pinned INSERT into a table with a timestamp_ntz column is a contract change I will make if the owners want it, but not one I will make unasked.
Triage: the race claim is a real residual, already disclosed, with both named remedies declined on the evidence above; the test claim is a framing point, not a defect. No code change from this round.
CI finish ledger - e9b0d77Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task whose
Neither red is reachable from this diff (DeltaLake Session id: cron:our-pr-ci-monitor:20260919-020058 |
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=30 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The writer reads the local-timestamp annotation only for DateTime and DateTime64 columns, so a colliding leaf of any other type cannot be affected by it. Only a Delta `timestamp` leaf (an instant) is a competing claim on the path. Pruning for every colliding leaf left a `timestamp_ntz` leaf UTC-adjusted, and so unreadable by Spark, when its dotted name collided with, for example, an Int32 leaf. Test: `si STRUCT<`a.b`: timestamp_ntz, a: STRUCT<b: Int32>>` in the nested test; the previous head writes `si."a.b"` as `timestamp[us, tz=UTC]`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A partition column is never written to the data file, so it cannot collide with a data leaf there. A `timestamp` partition column whose name contains a dot (`p.ts`) shares its writer path with the leaf `ts` of a struct `p`, and the collision check counted it, so a `timestamp_ntz` leaf `p.ts` was written UTC-adjusted. Partition columns are now left out of both path sets of the table schema, which also keeps a `timestamp_ntz` partition column from annotating a written `timestamp` leaf at its path. Test: a table partitioned by `p.ts`; the previous head writes `p.ts` as `timestamp[us, tz=UTC]`. The test copies files with argv-only commands because a timestamp partition directory name contains a space. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Head branch was pushed to by a user without write access
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes18 object files changed (+57.16 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units148 translation units recompiled, 1392 s compile time in total, 148 of them have a recent master baseline. |
…ath of the Parquet writer `test_storage_delta/test.py::test_writes_timestamp_ntz_annotation_nested`, added on `master` in ClickHouse#120863, writes a `Delta Lake` table whose struct has both a field literally named `a.b` and a nested `a.b`. That is a legal Delta schema, and the Delta writer handles the colliding path itself. This PR rejected every header with colliding dotted paths whenever a datalake column mapping was present, so the `INSERT` failed with `BAD_ARGUMENTS`. Keep the collision check for the user-facing `output_format_parquet_column_field_ids` / `output_format_parquet_auto_assign_field_ids` settings only, and leave the datalake path as on `master`. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=101783&sha=3fc36efef301c451224b9470ea18204a0bc31c1b&name_0=PR&name_1=Integration%20tests%20%28arm_binary%2C%201%2F4%29 PR: ClickHouse#101783 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
INSERTinto a DeltaLake table with atimestamp_ntzcolumn writing the Parquet data file withisAdjustedToUTC=true, which made the committed table unreadable by Spark and other Delta engines. Such a column is now annotatedTIMESTAMP(isAdjustedToUTC=false), as the Delta protocol requires. Closes #120653.Description
An
INSERTinto a Delta table whose schema declares atimestamp_ntzcolumn succeeded and committed, but stored the column asINT64 / Timestamp(isAdjustedToUTC=true, MICROS). Delta definestimestamp_ntzas an unadjusted (wall clock) Parquet timestamp, so every later Spark read of the table failed:ClickHouse itself still read the table, so the damage was interop-only.
Root cause: the kernel schema visitor wired
visit_timestampandvisit_timestamp_ntzto the same visitor, so both Delta types arrived asDateTime64(6)and the distinction was gone before the writer saw it. The Parquet writer then hardcodedisAdjustedToUTC=true, correct for plainParquet(aDateTime64is an instant) but wrong for a Deltatimestamp_ntzcolumn.The fix keeps the distinction on the schema visitor's field, caches the dotted paths of the
timestamp_ntzleaves with the rest of the snapshot schema, and passes them to the Parquet writer throughColumnMapper, the channel that already carries per-path lake facts that the ClickHouse type cannot express. Only the annotation changes; the storedint64is identical, so the ClickHouse round trip stays lossless. This is the exact inverse of the read-side rule settled in #87872.Nothing else is affected: general-format Parquet writes, Iceberg writes, Delta tables with no
timestamp_ntzcolumn, Delta writes in a non-Parquet format, atimestamp_ntzleaf whose dotted name collides with atimestampleaf, and anINSERTpinned to an older snapshot version all keep their current behaviour. Existing data files are not rewritten, so old tables are not repaired.#118601 adds the same seam for Iceberg. The
PrepareForWritehunks are identical, but it names theColumnMapperaccessorisIcebergLocalTimestampPath, so whichever lands first, the other should reuse the surviving accessor instead of adding a second one.Workflow [PR]
Sync PR [sync-upstream/pr/120863]
Version info
26.10.1.2182-master(included in26.10and later)