Skip to content

fix: accept UTC timestamps in parquet writer - #2477

Closed
xanderbailey wants to merge 8 commits into
apache:mainfrom
xanderbailey:xb/parquet_writer_accepts_UTC
Closed

xanderbailey wants to merge 8 commits into
apache:mainfrom
xanderbailey:xb/parquet_writer_accepts_UTC

Conversation

@xanderbailey

@xanderbailey xanderbailey commented May 20, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

When writing to an iceberg table with an arrow-batch that has timestamp UTC we fail with the following error.

 ArrowError(
    "Incompatible type. Field 'timestamp' has type Timestamp(µs, \"+00:00\"), array has type Timestamp(µs, \"UTC\")",
 ),

+00:00 and UTC are semantically identical so we should just arrow cast (essentially a no-op) before we write.

I've tried to make this as cheap as possible without introducing the invariant that we assume all incoming arrow batches have the same schema. If we did make that assumption we could store the index that need to be cast and make this a little cheaper but in reality this is an O(n_cols) per batch which I think.. is okay?

DataFusion has a similar mechanism for semantically identical timestamps but at the planning phase they do the cast which is a little cheaper

Opened a PR in arrow-rs to fix this lower in the stack but it was suggested that this should be handled by the application code so I'm open a PR here to open up that discussion.

What changes are included in this PR?

Are these changes tested?

Yes

type R = ParquetWriter;

async fn build(&self, output_file: OutputFile) -> Result<Self::R> {
let arrow_schema: ArrowSchemaRef = Arc::new(self.schema.as_ref().try_into()?);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Avoids converting to arrow for each batch

@blackmwk blackmwk 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.

Thanks @xanderbailey for this pr! I think the feature is reasonable, but we should not maintain it in parquet write module. I think the root cause is misalignment of arrow data types with iceberg schema. We did sth similar in here, and I think we should merge with it.

@xanderbailey

Copy link
Copy Markdown
Contributor Author

Ah very nice, thanks for taking a look! That's very nice, I didn't see that code

@xanderbailey

Copy link
Copy Markdown
Contributor Author

Looked at this a little more closely. The coercion has to happen per-batch since we don't know the incoming timezone until we see data, but I can move the utility into the arrow module to keep it consistent with the int96.rs pattern. Would that address your concern? The code linked only seems to be used on the read-side?

@blackmwk

Copy link
Copy Markdown
Contributor

Looked at this a little more closely. The coercion has to happen per-batch since we don't know the incoming timezone until we see data, but I can move the utility into the arrow module to keep it consistent with the int96.rs pattern. Would that address your concern? The code linked only seems to be used on the read-side?

Yes, let's move it arrow module. The actually problem is the misalignment between arrow and iceberg schema. The only difference is that int96 is about schema only, but we need to convert arrow data here. This is somehow similar to dealing with schema evolution when reading from parquet.
Anyway, let's make it a crate private standalone module, and we can refine the api and processing logic in future.

@xanderbailey

Copy link
Copy Markdown
Contributor Author

Makes sense to me, have moved it over to the arrow crate. Let me know what you think!

@kevinjqliu

Copy link
Copy Markdown
Contributor

there was a separate issue on +00:00 vs UTC: #2167

should we consider changing the default to UTC to align with spark's semantics?

@xanderbailey

Copy link
Copy Markdown
Contributor Author

Hey, thanks for taking a look! I'm not opposed to that but I think we'd still want this PR to accept it either way around!

if col.data_type() != target_field.data_type()
&& differs_only_by_utc_timezone(col.data_type(), target_field.data_type())
{
cols[idx] = cast(col, target_field.data_type())?;

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.

How should a call of coerce_timestamp handle a failure for cast?

}

#[cfg(test)]
mod tests {

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.

Maybe I overlooked it, but is there a test that passes in a bad schema at the top layer to make sure it gets down to the differs method as expected?

.enumerate()
{
if col.data_type() != target_field.data_type()
&& differs_only_by_utc_timezone(col.data_type(), target_field.data_type())

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 is actually error prone and wasteful. You need to detect the schema recursively, and cast needs to do it again. Ideally, we could use visit_schema_with_partner to handle it.


/// Returns true if `source` and `target` differ only by UTC-equivalent timezone aliases
/// at any nesting depth. Recurses into List, LargeList, FixedSizeList, Struct, and Map.
fn differs_only_by_utc_timezone(

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 replicates a lot of logic with ArrowSchemaVisitor

@xanderbailey
xanderbailey force-pushed the xb/parquet_writer_accepts_UTC branch from c48098b to db43f28 Compare June 15, 2026 07:55
}
}

impl ArrowSchemaVisitor for TimestampTzCoercionVisitor<'_> {

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.

There are two problems to me for this implements:

  1. It's quite inefficient. It has a lot of useless clone of data type, and field.
  2. Also I don't think we should silently ignore schema difference except timezone difference.

I just learned from this pr that arrow supports casting from struct to struct, then we could simplify it with a much more efficient algorithm:

  1. Using filter_leaves to filter fields in target schema where the leaves are utc column.
  2. For each incoming record batch, if source_schema == target_schema, continue.
  3. If it's not, we check the filtered fields in step 1 in source_schema to see if we need to do conversion. If so, we do a cast.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think the issue here is that if you have a nested struct with a +00:00 timestamp then filter leaves has thrown away the information needed to point at the column and then the field name, similarly for arrays etc. So you need to traverse the two together.

You also can't blindly use the struct cast in case you have say {int, utc} and {string, +00:00} because you risk casting things you didn't want to, i.e. the int is cast to a string.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Which I think means you might end up with something similar to what we had at the start:

fn is_utc_tz_alias_difference(source: &DataType, target: &DataType) -> bool {
    match (source, target) {
        (
            DataType::Timestamp(src_unit, Some(src_tz)),
            DataType::Timestamp(tgt_unit, Some(tgt_tz)),
        ) => src_unit == tgt_unit && src_tz != tgt_tz && is_utc(src_tz) && is_utc(tgt_tz),
        (DataType::Struct(src_fields), DataType::Struct(tgt_fields)) => {
            src_fields.len() == tgt_fields.len()
                && src_fields
                    .iter()
                    .zip(tgt_fields)
                    .all(|(s, t)| fields_coercible(s, t))
        }
        (DataType::List(src), DataType::List(tgt))
        | (DataType::LargeList(src), DataType::LargeList(tgt)) => fields_coercible(src, tgt),
        (DataType::FixedSizeList(src, src_len), DataType::FixedSizeList(tgt, tgt_len)) => {
            src_len == tgt_len && fields_coercible(src, tgt)
        }
        (DataType::Map(src, src_sorted), DataType::Map(tgt, tgt_sorted)) => {
            src_sorted == tgt_sorted && fields_coercible(src, tgt)
        }
        _ => false,
    }
}

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.

I think the issue here is that if you have a nested struct with a +00:00 timestamp then filter leaves has thrown away the information needed to point at the column and then the field name, similarly for arrays etc. So you need to traverse the two together.

Ah, actually, no, if you look at its example carefully, the filtered nested type are kept, so you could still fetch the top fields.

You also can't blindly use the struct cast in case you have say {int, utc} and {string, +00:00} because you risk casting things you didn't want to, i.e. the int is cast to a string.

That's a good point, but it's odd to do this in the utc check. I think the final solution should be a function to do it in one pass.

@blackmwk blackmwk 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.

After a second thought, although I think the requirement is valuable, I don't think parquet_writer is the right place to solve the problem. Iceberg's parquet writer is a low level primitive to produce iceberg related data files, it should be the caller's responsibility to do the conversion.

@xanderbailey

Copy link
Copy Markdown
Contributor Author

I wonder if makes more sense to have this in our datafusion integration specifically then? I believe datafusion does this handling at the logical plan layer which perhaps makes more sense?

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been marked as stale due to 30 days of inactivity. It will be closed in 1 week if no further activity occurs. If you think that’s incorrect or this pull request requires a review, please simply write any comment. If closed, you can revive the PR at any time and @mention a reviewer or discuss it on the [email protected] list. Thank you for your contributions.

@github-actions github-actions Bot added the stale label Jul 19, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been closed due to lack of activity. This is not a judgement on the merit of the PR in any way. It is just a way of keeping the PR queue manageable. If you think that is incorrect, or the pull request requires review, you can revive the PR at any time.

@github-actions github-actions Bot closed this Jul 26, 2026
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.

Writing UTC timestamp to an iceberg table fails

4 participants