Repository navigation
fix: accept UTC timestamps in parquet writer - #2477
xanderbailey wants to merge 8 commits into
Conversation
| 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()?); |
There was a problem hiding this comment.
Avoids converting to arrow for each batch
blackmwk
left a comment
There was a problem hiding this comment.
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.
|
Ah very nice, thanks for taking a look! That's very nice, I didn't see that code |
|
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. |
|
Makes sense to me, have moved it over to the arrow crate. Let me know what you think! |
|
there was a separate issue on should we consider changing the default to |
|
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())?; |
There was a problem hiding this comment.
How should a call of coerce_timestamp handle a failure for cast?
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { |
There was a problem hiding this comment.
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()) |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
This replicates a lot of logic with ArrowSchemaVisitor
c48098b to
db43f28
Compare
| } | ||
| } | ||
|
|
||
| impl ArrowSchemaVisitor for TimestampTzCoercionVisitor<'_> { |
There was a problem hiding this comment.
There are two problems to me for this implements:
- It's quite inefficient. It has a lot of useless clone of data type, and field.
- 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:
- Using filter_leaves to filter fields in target schema where the leaves are utc column.
- For each incoming record batch, if source_schema == target_schema, continue.
- 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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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,
}
}
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
|
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? |
|
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. |
|
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. |
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.
+00:00andUTCare 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