-
Notifications
You must be signed in to change notification settings - Fork 189
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
feat: support serialize/deserialize DataFile into avro bytes #797
Merged
+179
−49
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -656,6 +656,38 @@ mod _const_schema { | |||||
}) | ||||||
}; | ||||||
|
||||||
fn data_file_fields_v2(partition_type: StructType) -> Vec<NestedFieldRef> { | ||||||
vec![ | ||||||
CONTENT.clone(), | ||||||
FILE_PATH.clone(), | ||||||
FILE_FORMAT.clone(), | ||||||
Arc::new(NestedField::required( | ||||||
102, | ||||||
"partition", | ||||||
Type::Struct(partition_type), | ||||||
)), | ||||||
RECORD_COUNT.clone(), | ||||||
FILE_SIZE_IN_BYTES.clone(), | ||||||
COLUMN_SIZES.clone(), | ||||||
VALUE_COUNTS.clone(), | ||||||
NULL_VALUE_COUNTS.clone(), | ||||||
NAN_VALUE_COUNTS.clone(), | ||||||
LOWER_BOUNDS.clone(), | ||||||
UPPER_BOUNDS.clone(), | ||||||
KEY_METADATA.clone(), | ||||||
SPLIT_OFFSETS.clone(), | ||||||
EQUALITY_IDS.clone(), | ||||||
SORT_ORDER_ID.clone(), | ||||||
] | ||||||
} | ||||||
|
||||||
pub(super) fn data_file_schema_v2(partition_type: StructType) -> Result<AvroSchema, Error> { | ||||||
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.
Suggested change
|
||||||
let schema = Schema::builder() | ||||||
.with_fields(data_file_fields_v2(partition_type)) | ||||||
.build()?; | ||||||
schema_to_avro_schema("data_file", &schema) | ||||||
} | ||||||
|
||||||
pub(super) fn manifest_schema_v2(partition_type: StructType) -> Result<AvroSchema, Error> { | ||||||
let fields = vec![ | ||||||
STATUS.clone(), | ||||||
|
@@ -665,62 +697,52 @@ mod _const_schema { | |||||
Arc::new(NestedField::required( | ||||||
2, | ||||||
"data_file", | ||||||
Type::Struct(StructType::new(vec![ | ||||||
CONTENT.clone(), | ||||||
FILE_PATH.clone(), | ||||||
FILE_FORMAT.clone(), | ||||||
Arc::new(NestedField::required( | ||||||
102, | ||||||
"partition", | ||||||
Type::Struct(partition_type), | ||||||
)), | ||||||
RECORD_COUNT.clone(), | ||||||
FILE_SIZE_IN_BYTES.clone(), | ||||||
COLUMN_SIZES.clone(), | ||||||
VALUE_COUNTS.clone(), | ||||||
NULL_VALUE_COUNTS.clone(), | ||||||
NAN_VALUE_COUNTS.clone(), | ||||||
LOWER_BOUNDS.clone(), | ||||||
UPPER_BOUNDS.clone(), | ||||||
KEY_METADATA.clone(), | ||||||
SPLIT_OFFSETS.clone(), | ||||||
EQUALITY_IDS.clone(), | ||||||
SORT_ORDER_ID.clone(), | ||||||
])), | ||||||
Type::Struct(StructType::new(data_file_fields_v2(partition_type))), | ||||||
)), | ||||||
]; | ||||||
let schema = Schema::builder().with_fields(fields).build()?; | ||||||
schema_to_avro_schema("manifest_entry", &schema) | ||||||
} | ||||||
|
||||||
fn data_file_fields_v1(partition_type: StructType) -> Vec<NestedFieldRef> { | ||||||
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.
Suggested change
|
||||||
vec![ | ||||||
FILE_PATH.clone(), | ||||||
FILE_FORMAT.clone(), | ||||||
Arc::new(NestedField::required( | ||||||
102, | ||||||
"partition", | ||||||
Type::Struct(partition_type), | ||||||
)), | ||||||
RECORD_COUNT.clone(), | ||||||
FILE_SIZE_IN_BYTES.clone(), | ||||||
BLOCK_SIZE_IN_BYTES.clone(), | ||||||
COLUMN_SIZES.clone(), | ||||||
VALUE_COUNTS.clone(), | ||||||
NULL_VALUE_COUNTS.clone(), | ||||||
NAN_VALUE_COUNTS.clone(), | ||||||
LOWER_BOUNDS.clone(), | ||||||
UPPER_BOUNDS.clone(), | ||||||
KEY_METADATA.clone(), | ||||||
SPLIT_OFFSETS.clone(), | ||||||
SORT_ORDER_ID.clone(), | ||||||
] | ||||||
} | ||||||
|
||||||
pub(super) fn data_file_schema_v1(partition_type: StructType) -> Result<AvroSchema, Error> { | ||||||
let schema = Schema::builder() | ||||||
.with_fields(data_file_fields_v1(partition_type)) | ||||||
.build()?; | ||||||
schema_to_avro_schema("data_file", &schema) | ||||||
} | ||||||
|
||||||
pub(super) fn manifest_schema_v1(partition_type: StructType) -> Result<AvroSchema, Error> { | ||||||
let fields = vec![ | ||||||
STATUS.clone(), | ||||||
SNAPSHOT_ID_V1.clone(), | ||||||
Arc::new(NestedField::required( | ||||||
2, | ||||||
"data_file", | ||||||
Type::Struct(StructType::new(vec![ | ||||||
FILE_PATH.clone(), | ||||||
FILE_FORMAT.clone(), | ||||||
Arc::new(NestedField::required( | ||||||
102, | ||||||
"partition", | ||||||
Type::Struct(partition_type), | ||||||
)), | ||||||
RECORD_COUNT.clone(), | ||||||
FILE_SIZE_IN_BYTES.clone(), | ||||||
BLOCK_SIZE_IN_BYTES.clone(), | ||||||
COLUMN_SIZES.clone(), | ||||||
VALUE_COUNTS.clone(), | ||||||
NULL_VALUE_COUNTS.clone(), | ||||||
NAN_VALUE_COUNTS.clone(), | ||||||
LOWER_BOUNDS.clone(), | ||||||
UPPER_BOUNDS.clone(), | ||||||
KEY_METADATA.clone(), | ||||||
SPLIT_OFFSETS.clone(), | ||||||
SORT_ORDER_ID.clone(), | ||||||
])), | ||||||
Type::Struct(StructType::new(data_file_fields_v1(partition_type))), | ||||||
)), | ||||||
]; | ||||||
let schema = Schema::builder().with_fields(fields).build()?; | ||||||
|
@@ -1189,6 +1211,47 @@ impl DataFile { | |||||
self.sort_order_id | ||||||
} | ||||||
} | ||||||
|
||||||
/// Convert data files to avro bytes. | ||||||
Xuanwo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
pub fn data_files_to_avro( | ||||||
Xuanwo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
data_files: impl IntoIterator<Item = DataFile>, | ||||||
partition_type: &StructType, | ||||||
version: FormatVersion, | ||||||
) -> Result<Vec<u8>> { | ||||||
let avro_schema = match version { | ||||||
FormatVersion::V1 => _const_schema::data_file_schema_v1(partition_type.clone()).unwrap(), | ||||||
FormatVersion::V2 => _const_schema::data_file_schema_v2(partition_type.clone()).unwrap(), | ||||||
}; | ||||||
let mut writer = AvroWriter::new(&avro_schema, Vec::new()); | ||||||
|
||||||
for data_file in data_files { | ||||||
let value = to_value(_serde::DataFile::try_from(data_file, partition_type, true)?)? | ||||||
.resolve(&avro_schema)?; | ||||||
writer.append(value)?; | ||||||
} | ||||||
|
||||||
Ok(writer.into_inner()?) | ||||||
} | ||||||
|
||||||
/// Parse data files from avro bytes. | ||||||
pub fn parse_from_avro( | ||||||
Xuanwo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
bytes: &[u8], | ||||||
schema: &Schema, | ||||||
partition_type: &StructType, | ||||||
version: FormatVersion, | ||||||
) -> Result<Vec<DataFile>> { | ||||||
let avro_schema = match version { | ||||||
FormatVersion::V1 => _const_schema::data_file_schema_v1(partition_type.clone()).unwrap(), | ||||||
FormatVersion::V2 => _const_schema::data_file_schema_v2(partition_type.clone()).unwrap(), | ||||||
}; | ||||||
|
||||||
let reader = AvroReader::with_schema(&avro_schema, bytes)?; | ||||||
reader | ||||||
.into_iter() | ||||||
.map(|value| from_value::<_serde::DataFile>(&value?)?.try_into(partition_type, schema)) | ||||||
.collect::<Result<Vec<_>>>() | ||||||
} | ||||||
|
||||||
/// Type of content stored by the data file: data, equality deletes, or | ||||||
/// position deletes (all v1 files are data files) | ||||||
#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)] | ||||||
|
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.