-
Notifications
You must be signed in to change notification settings - Fork 331
fix: Add support for unsigned Arrow datatypes in schema conversion #1617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
81907e7
fix: Add support for unsigned Arrow datatypes in schema conversion
gkpanda4 cd78247
Fix formatting issues
gkpanda4 948f25d
Address comments and change approach
gkpanda4 014790c
Fix formatting issues
gkpanda4 b457602
Make conversion logic simpler and add integration test
gkpanda4 a352b84
Address comments
gkpanda4 44c62a5
Remove local changes
gkpanda4 31894ad
nit:Update formats
gkpanda4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -378,7 +378,16 @@ impl ArrowSchemaVisitor for ArrowSchemaConverter { | |
DataType::Int8 | DataType::Int16 | DataType::Int32 => { | ||
Ok(Type::Primitive(PrimitiveType::Int)) | ||
} | ||
DataType::UInt8 | DataType::UInt16 => Ok(Type::Primitive(PrimitiveType::Int)), | ||
DataType::UInt32 => Ok(Type::Primitive(PrimitiveType::Long)), | ||
DataType::Int64 => Ok(Type::Primitive(PrimitiveType::Long)), | ||
DataType::UInt64 => { | ||
// Block uint64 - no safe casting option | ||
Err(Error::new( | ||
ErrorKind::DataInvalid, | ||
"UInt64 is not supported. Use Int64 for values ≤ 9,223,372,036,854,775,807 or Decimal(20,0) for full uint64 range.", | ||
)) | ||
} | ||
DataType::Float32 => Ok(Type::Primitive(PrimitiveType::Float)), | ||
DataType::Float64 => Ok(Type::Primitive(PrimitiveType::Double)), | ||
DataType::Decimal128(p, s) => Type::decimal(*p as u32, *s as u32).map_err(|e| { | ||
|
@@ -1717,6 +1726,49 @@ mod tests { | |
} | ||
} | ||
|
||
#[test] | ||
fn test_unsigned_integer_type_conversion() { | ||
let test_cases = vec![ | ||
(DataType::UInt8, PrimitiveType::Int), | ||
(DataType::UInt16, PrimitiveType::Int), | ||
(DataType::UInt32, PrimitiveType::Long), | ||
]; | ||
|
||
for (arrow_type, expected_iceberg_type) in test_cases { | ||
let arrow_field = Field::new("test", arrow_type.clone(), false).with_metadata( | ||
HashMap::from([(PARQUET_FIELD_ID_META_KEY.to_string(), "1".to_string())]), | ||
); | ||
let arrow_schema = ArrowSchema::new(vec![arrow_field]); | ||
|
||
let iceberg_schema = arrow_schema_to_schema(&arrow_schema).unwrap(); | ||
let iceberg_field = iceberg_schema.as_struct().fields().first().unwrap(); | ||
|
||
assert!( | ||
matches!(iceberg_field.field_type.as_ref(), Type::Primitive(t) if *t == expected_iceberg_type), | ||
"Expected {:?} to map to {:?}", | ||
arrow_type, | ||
expected_iceberg_type | ||
); | ||
} | ||
|
||
// Test UInt64 blocking | ||
{ | ||
let arrow_field = Field::new("test", DataType::UInt64, false).with_metadata( | ||
HashMap::from([(PARQUET_FIELD_ID_META_KEY.to_string(), "1".to_string())]), | ||
); | ||
let arrow_schema = ArrowSchema::new(vec![arrow_field]); | ||
|
||
let result = arrow_schema_to_schema(&arrow_schema); | ||
assert!(result.is_err()); | ||
assert!( | ||
result | ||
.unwrap_err() | ||
.to_string() | ||
.contains("UInt64 is not supported") | ||
); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I think the brackets here and L1755 are excessive |
||
} | ||
|
||
#[test] | ||
fn test_datum_conversion() { | ||
{ | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this probably isn't the right module, but it would probably be nice to have a test that actually exercises writing these types and then reading them back again?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I implemented an integration test for unsigned type roundtrip, but discovered that ParquetWriter also requires modification to handle unsigned data conversion. The issue stems from a type mismatch between schema and data.
The problem occurs because schema conversion (
arrow_schema_to_schema
) transforms the metadata but leaves the actual data unchanged. When writing, Arrow validation fails due to this mismatch.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think writing record batches that contain unsigned types is out of the scope of the original issue and can be tricky:
ParquetWriter
usesAsyncArrowWriter
under the hoodAsyncArrowWriter
uses an arrow schema that got converted from the Iceberg table schemaThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, from the original issue it seems scope is ambiguous. It seems like this change it makes it possible to create a schema from arrow with unsigned types which might be helpful by itself, but imagine the next thing the user would want to do is actually the write the data?
It seems fine to check this in separately as long as there is a clean failure for the unsigned types (i.e. we don't silently lose data).