Skip to content
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: make file metadata serializable #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/metadata/file_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,66 @@ use crate::{error::Error, metadata::get_sort_order};

use super::{column_order::ColumnOrder, schema_descriptor::SchemaDescriptor, RowGroupMetaData};
use parquet_format_safe::ColumnOrder as TColumnOrder;
#[cfg(feature = "serde_types")]
use serde::{Deserialize, Serialize};

pub use crate::thrift_format::KeyValue;

#[cfg(feature = "serde_types")]
pub mod key_value_serde {
use super::*;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
struct SerializableKeyValue {
key: String,
value: Option<String>,
}

impl From<KeyValue> for SerializableKeyValue {
fn from(kv: KeyValue) -> Self {
SerializableKeyValue {
key: kv.key,
value: kv.value,
}
}
}
impl From<SerializableKeyValue> for KeyValue {
fn from(kv: SerializableKeyValue) -> Self {
KeyValue {
key: kv.key,
value: kv.value,
}
}
}

#[allow(unused)]
pub fn serialize<S>(data: &Option<Vec<KeyValue>>, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let data: Option<Vec<SerializableKeyValue>> = data
.as_ref()
.map(|data| data.iter().map(|kv| kv.clone().into()).collect());
match data {
Some(data) => data.serialize(serializer),
None => serializer.serialize_none(),
}
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<Vec<KeyValue>>, D::Error>
where
D: serde::Deserializer<'de>,
{
let data: Option<Vec<SerializableKeyValue>> = Option::deserialize(deserializer)?;
let data = data.map(|data| data.into_iter().map(|kv| kv.into()).collect());
Ok(data)
}
}

/// Metadata for a Parquet file.
// This is almost equal to [`parquet_format_safe::FileMetaData`] but contains the descriptors,
// which are crucial to deserialize pages.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde_types", derive(Deserialize, Serialize))]
pub struct FileMetaData {
/// version of this file.
pub version: i32,
Expand All @@ -26,6 +79,7 @@ pub struct FileMetaData {
/// The row groups of this file
pub row_groups: Vec<RowGroupMetaData>,
/// key_value_metadata of this file.
#[cfg_attr(feature = "serde_types", serde(with = "key_value_serde"))]
pub key_value_metadata: Option<Vec<KeyValue>>,
/// schema descriptor.
pub schema_descr: SchemaDescriptor,
Expand Down
5 changes: 4 additions & 1 deletion src/read/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ pub fn read_metadata<R: Read + Seek>(reader: &mut R) -> Result<FileMetaData> {
}

/// Reads a [`FileMetaData`] from the reader, located at the end of the file, with known file size.
pub fn read_metadata_with_size<R: Read + Seek>(reader: &mut R, file_size: u64) -> Result<FileMetaData> {
pub fn read_metadata_with_size<R: Read + Seek>(
reader: &mut R,
file_size: u64,
) -> Result<FileMetaData> {
if file_size < HEADER_SIZE + FOOTER_SIZE {
return Err(Error::oos(
"A parquet file must containt a header and footer with at least 12 bytes",
Expand Down
4 changes: 2 additions & 2 deletions src/schema/types/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ fn check_decimal_invariants(
precision,
)));
}
if scale >= precision {
if scale > precision {
return Err(Error::oos(format!(
"Invalid DECIMAL: scale ({}) cannot be greater than or equal to precision \
"Invalid DECIMAL: scale ({}) cannot be greater than precision \
({})",
scale, precision
)));
Expand Down