Skip to content

[VL][Iceberg] Fix timestamptz columns written with isAdjustedToUTC=false in Parquet footer - #13684

Open
Anirban-Chand wants to merge 12 commits into
apache:mainfrom
Anirban-Chand:fix/iceberg-timestamptz-isAdjustedToUTC
Open

Anirban-Chand wants to merge 12 commits into
apache:mainfrom
Anirban-Chand:fix/iceberg-timestamptz-isAdjustedToUTC

Conversation

@Anirban-Chand

@Anirban-Chand Anirban-Chand commented Nov 19, 2026 •

Copy link
Copy Markdown

What changes are proposed in this pull request?

Fixes #13429.

When writing Iceberg tables via the Velox backend, columns with Spark TimestampType (which map to Iceberg's timestamptz) were written into Parquet files with isAdjustedToUTC=false.

According to the Iceberg Table Specification, timestamptz must be written as Parquet Timestamp(isAdjustedToUTC=true, unit=micros). Because the flag was missing/false, strict Iceberg readers such as apache/iceberg-go fail during operations like table compaction with: cannot promote timestamp to timestamptz.

Root Cause

  1. In Velox (velox/dwio/parquet/writer/arrow/ArrowSchema.cpp:240), isAdjustedToUTC is derived purely from whether the Arrow timestamp type carries a non-empty timezone string:
    const bool utc = !(timestampType.timezone().empty());
  2. Velox requires the serde parameter parquet.writer.timestamp.timezone (ParquetConfig.h:261) to attach the timezone string during Arrow schema construction (Bridge.cpp:371).
  3. In AbstractIcebergWriteExec.scala, this serde property was not passed into icebergProperties. As a result, Velox constructed the Arrow schema with an empty timezone string (""), causing the Parquet footer to default to isAdjustedToUTC=false.

Proposed Fix

In AbstractIcebergWriteExec.scala, inspect the Iceberg write schema (writeSchema). If any column is a timestamptz (col.type().typeId() == TypeID.TIMESTAMP && col.type().asInstanceOf[Types.TimestampType].shouldAdjustToUTC()), inject "parquet.writer.timestamp.timezone" -> "UTC" into icebergProperties so Velox sets isAdjustedToUTC=true in the Parquet footer.

How was this patch tested?

  • Added unit test GLUTEN-13429: timestamptz columns written with isAdjustedToUTC=true in VeloxIcebergSuite.scala.
  • The test verifies:
    1. The Iceberg write is offloaded to native Velox (VeloxIcebergAppendDataExec).
    2. The written .parquet file footer metadata contains isAdjustedToUTC=true via ParquetFileReader inspection.
    3. Reading back the written table data yields correct results.
  • Validated against GitHub Actions CI.

Was this patch authored or co-authored using generative AI tooling?

Generated-by: Antigravity

@github-actions github-actions Bot added the VELOX label Nov 19, 2026
@zhouyuan

Copy link
Copy Markdown
Member

Cc: @infvg

@zhouyuan
zhouyuan requested a balanced review from Copilot November 19, 2026 09:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@Anirban-Chand
Anirban-Chand marked this pull request as ready for review November 19, 2026 09:42
col.`type`().asInstanceOf[Types.TimestampType].shouldAdjustToUTC()
}
if (hasTimestampTz) {
icebergProperties.put("parquet.writer.timestamp.timezone", "UTC")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This never reaches velox, gluten passes empty serde parameters, and Velox sets the timezone to "" here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks @infvg! You're right—Velox's WriterOptionsAdapter.cpp explicitly overrides kWriterSerdeTimestampTimezone = "" in applyPreConfigs, which causes ArrowSchema.cpp:240 to write isAdjustedToUTC=false.

A proper fix will require updating Velox's WriterOptionsAdapter.cpp to set "UTC" for timestamptz. I'll open a PR in Velox for this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in cpp/velox/compute/iceberg/IcebergWriter.cc — createSerdeParameters now extracts parquet::ParquetConfig::kWriterSerdeTimestampTimezone from sparkConfs and forwards it into IcebergInsertTableHandle::serdeParameters. Upstream facebookincubator/velox#20487 (9c6c9667b2c) is also merged to preserve caller-provided timezone in ParquetWriterOptionsAdapter.

// path (validation in IcebergWriteExec blocks them). If per-column support is needed in
// future, consider Option 2/3 discussed in https://github.com/apache/gluten/issues/13429.
val hasTimestampTz = writeSchema
.columns()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only checks top-level columns, can we see if we can either add support for nested columns, and only if it's not possible without a velox change, raise the velox PR + verify fallback for nested case.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I'll update the check using TypeUtil.visit(...) to cover nested fields recursively. I will also verify fallback behavior if Velox can't support mixed/nested types without deeper changes.

}
}

test("GLUTEN-13429: timestamptz columns written with isAdjustedToUTC=true") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests might not be necessary, please check if this functionality is tested via our upstream iceberg tests, and if it is, we can remove this.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I'll check iceberg_spark_ut.yml coverage, and if this is already tested upstream, I'll remove this test.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@infvg, I verified the upstream Iceberg test suite: the upstream tests perform query-level round-trip validations (SELECT ... and comparing row values). Because Java/Spark-based Iceberg readers silently coerce the timestamp regardless of isAdjustedToUTC, those upstream tests pass even when isAdjustedToUTC=false (which is why #13429 was only caught by strict readers like iceberg-go).

The test in VeloxIcebergSuite.scala explicitly inspects ParquetFileReader.getFooter.getFileMetaData to assert isAdjustedToUTC=true directly on the physical Parquet metadata (matching the pattern used elsewhere in VeloxIcebergSuite for page version and codec footer verification).

I propose keeping this test so we have regression coverage verifying the physical Parquet metadata flag itself. What do you think?

@infvg infvg Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move this out of the enhanced test class (i think this isn't being run)? I think this test will actually fail CI due to the above issue

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to active backends-velox/src-iceberg/test/scala/org/apache/gluten/execution/VeloxIcebergSuite.scala and extended to verify isAdjustedToUTC=true for both top-level (ts TIMESTAMP) and nested (nested STRUCT<ts: TIMESTAMP>) columns.

@Anirban-Chand
Anirban-Chand requested a review from infvg November 19, 2026 17:37
@Anirban-Chand

Anirban-Chand commented Nov 19, 2026 •

Copy link
Copy Markdown
Author

Hi @infvg, quick update on the review comments:

  1. Velox Serde Parameter Flow:

    • Opened Velox PR facebookincubator/velox#20487: fix(iceberg): preserve caller timestamp timezone in ParquetWriterOptionsAdapter.
    • It updates WriterOptionsAdapter.cpp to preserve the caller-provided parquet.writer.timestamp.timezone (e.g., "UTC") if present rather than unconditionally overwriting it to "". Unit tests have been added and all checks are passing upstream.
  2. Nested Columns Support:

    • Updated AbstractIcebergWriteExec.scala to use TypeUtil.indexById(writeSchema.asStruct()).values() to recursively inspect all fields (both top-level and nested structs, maps, and lists) for timestamptz (col.shouldAdjustToUTC() == true).
  3. Test Coverage:

    • Retained the unit test in VeloxIcebergSuite.scala. As noted earlier, upstream Iceberg tests only perform logical SELECT round-trips with Spark's reader, which ignores isAdjustedToUTC. This unit test directly inspects the physical ParquetFileReader footer to ensure the metadata flag is preserved.
  4. CI Status:

    • Velox Backend (ARM) passed completely.
    • Velox Backend (x86) passed completely.
    • Velox Kafka passed completely.
    • All 5 chunks of our PR's Iceberg test suite (iceberg-upstream-test (3.5, head, 0-4)) passed 100%.

@Anirban-Chand

Copy link
Copy Markdown
Author

Hi @infvg, gentle ping on this when you have a moment.

All CI checks across x86, ARM, Kafka, and Iceberg suites have finished and are now fully green. In parallel, we also added the end-to-end Iceberg write test verifying Parquet footer annotations on the upstream Velox PR (facebookincubator/velox#20487).

Could you take another look when you get a chance? Thanks!

1 similar comment
@Anirban-Chand

Copy link
Copy Markdown
Author

Hi @infvg, gentle ping on this when you have a moment.

All CI checks across x86, ARM, Kafka, and Iceberg suites have finished and are now fully green. In parallel, we also added the end-to-end Iceberg write test verifying Parquet footer annotations on the upstream Velox PR (facebookincubator/velox#20487).

Could you take another look when you get a chance? Thanks!

pedrumj2 pushed a commit to pedrumj2/gluten that referenced this pull request Nov 25, 2026
## What changes are proposed in this pull request?

[`withNativeStatistics`](https://github.com/apache/gluten/blob/85c89803ed2d050db4052f1813d76711ebe29fb8/backends-velox/src-delta33/test/scala/org/apache/spark/sql/delta/GlutenDeltaStatsLifecycleSuite.scala#L105) waits up to 15 seconds for the statistics worker threads to exit, while two other checks in [`GlutenDeltaStatsLifecycleSuite`](https://github.com/apache/gluten/blob/85c89803ed2d050db4052f1813d76711ebe29fb8/backends-velox/src-delta33/test/scala/org/apache/spark/sql/delta/GlutenDeltaStatsLifecycleSuite.scala#L156) did not wait. This change makes those checks wait the same way. The change also writes the aborted-write test's rows from a single partition, so that test always creates its native statistics.

| Test | Before | After |
|---|---|---|
| "aborted Delta writes close native statistics before task runtimes" | statistics sometimes never created | statistics always created |
| "newRow releases the batch when statistics fail during submission" | checks worker threads without waiting | waits up to 15 seconds |
| "newRow propagates a statistics worker failure while consuming input" | checks worker threads without waiting | waits up to 15 seconds |

[`GlutenDeltaJobStatsTracker`](https://github.com/apache/gluten/blob/85c89803ed2d050db4052f1813d76711ebe29fb8/backends-velox/src-delta33/main/scala/org/apache/spark/sql/delta/stats/GlutenDeltaJobStatsTracker.scala#L128) runs each statistics worker on a single-thread pool that the task's completion listener [shuts down and awaits](https://github.com/apache/gluten/blob/85c89803ed2d050db4052f1813d76711ebe29fb8/backends-velox/src-delta33/main/scala/org/apache/spark/sql/delta/stats/GlutenDeltaJobStatsTracker.scala#L237). The pool's thread can still be exiting when a test lists the live threads right after the task ends, which fails with `Set(331) was not empty Statistics workers survived task completion`.

[`executeTask`](https://github.com/apache/gluten/blob/85c89803ed2d050db4052f1813d76711ebe29fb8/backends-velox/src-delta33/main/scala/org/apache/spark/sql/delta/files/GlutenDeltaFileFormatWriter.scala#L473) reads a row before it creates the writer in every partition except partition 0. The writer's statistics tracker reports its plan to the test's observer [when the tracker is created](https://github.com/apache/gluten/blob/85c89803ed2d050db4052f1813d76711ebe29fb8/backends-velox/src-delta33/main/scala/org/apache/spark/sql/delta/stats/GlutenDeltaJobStatsTracker.scala#L192). The aborted-write test wrote its valid row from partition 0 and its null row from partition 1, so partition 1 failed the NOT NULL check before creating a tracker. The job abort could then kill partition 0's task before that task created its tracker, which fails with `[] was empty The write did not exercise native Delta statistics`.

The two tests failed 10 times across 7 pull requests between 2026-11-19 and 2026-11-24, for example on apache#13684 and apache#13728. A re-run of the same commit passed for 7 of the 10 failures. The suite was added under apache#12743. Removing both races keeps unrelated Delta pull requests from going red on them.

## How was this patch tested?

### Automated tests

The `spark-test-spark40 (3)`, `spark-test-spark41 (3)` and `spark-test-spark42 (3)` jobs of the [Velox Backend (x86) workflow](https://github.com/apache/gluten/blob/85c89803ed2d050db4052f1813d76711ebe29fb8/.github/workflows/velox_backend_x86.yml) run [`GlutenDeltaStatsLifecycleSuite`](https://github.com/apache/gluten/blob/85c89803ed2d050db4052f1813d76711ebe29fb8/backends-velox/src-delta33/test/scala/org/apache/spark/sql/delta/GlutenDeltaStatsLifecycleSuite.scala#L111).

## Was this patch authored or co-authored using generative AI tooling?

Generated-by: Co-authored with Claude Code (claude-opus-5-5)
@Anirban-Chand

Copy link
Copy Markdown
Author

Hi @infvg, addressed both review threads inline:

  1. Forwarded parquet.writer.timestamp.timezone into IcebergInsertTableHandle::serdeParameters in cpp/velox/compute/iceberg/IcebergWriter.cc (upstream fix(iceberg): Preserve caller timestamp timezone in ParquetWriterOptionsAdapter facebookincubator/velox#20487 is now merged at 9c6c9667b2c).
  2. Moved the test out of enhanced/ into active execution/VeloxIcebergSuite.scala (covering top-level and nested timestamptz).

Note: ep/build-velox/src/get-velox.sh currently pins IBM/velox dft-2026_11_19, which does not yet include velox#20487, so this test is expected to fail CI until IBM/velox syncs (pinged @zhouyuan on triggering the sync).

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[VL] Iceberg write: timestamptz columns written with isAdjustedToUTC=false

4 participants