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

Add trait for schema construction #96

Merged
merged 7 commits into from
Nov 2, 2023
Merged
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
6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
[workspace]
members = [
"serde_arrow",
"example",
]
members = ["serde_arrow", "example"]
default-members = ["serde_arrow"]

resolver = "2"

Expand Down
12 changes: 8 additions & 4 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ Breaking changes:

Improvements:

- Simpler and streamlined API
- Simpler and streamlined API (`to_arrow` / `from_arrow` and `to_arrow2` /
`from_arrow2`)
- Add `SchemaLike` trait to support direct construction of arrow / arrow2 fields
- Add type based tracing to allow schema tracing without samples
(`SerdeArrowSchema::form_type()`)
(`SchemaLike::form_type()`)
- Allow to build schema objects from serializable objects, e.g.,
`serde_json::Value` (`SerdeArrow::from_value()`)
`serde_json::Value` (`SchemaLike::from_value()`)
- Add support for `arrow=47` and `arrow=48`
- Fix bug in `arrow2=0.16` support
- Fix unused warnings without selected arrow versions

Deprecations (see the documentation of deprecated items for how to migratie):

Expand All @@ -26,7 +30,7 @@ Deprecations (see the documentation of deprecated items for how to migratie):
- Deprecate `serialize_into_arrays`, `deserialize_from_arrays` methods in favor of
`to_arrow` / `to_arrow2` and `from_arrow` / `from_arrow2`
- Deprecate `serialize_into_fields` methods in favor of
`SerdeArrowSchema::from_samples`
`SchemaLike::from_samples`
- Deprecated single item methods in favor of using the `Items` and `Item`
wrappers

Expand Down
2 changes: 1 addition & 1 deletion example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn main() -> Result<(), PanicOnError> {
},
];

use serde_arrow::schema::{SerdeArrowSchema, TracingOptions};
use serde_arrow::schema::{SchemaLike, SerdeArrowSchema, TracingOptions};

let fields: Vec<Field> =
SerdeArrowSchema::from_samples(&examples, TracingOptions::default().guess_dates(true))?
Expand Down
2 changes: 1 addition & 1 deletion serde_arrow/benches/groups/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ macro_rules! define_benchmark {
)?
) => {
pub fn benchmark_serialize(c: &mut criterion::Criterion) {
use serde_arrow::schema::SerdeArrowSchema;
use serde_arrow::schema::{SerdeArrowSchema, SchemaLike};

for n in [$($n),*] {
let mut group = c.benchmark_group(format!("{}_serialize({})", stringify!($name), n));
Expand Down
69 changes: 33 additions & 36 deletions serde_arrow/src/arrow2_impl/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
},
};

/// Build arrow2 arrays record by record (*requires one of the `arrow2-*`
/// Build arrow2 arrays record by record (*requires one of the `arrow2-*`
/// features*)
///
/// The given items should be records (e.g., structs). To serialize items
Expand Down Expand Up @@ -115,8 +115,10 @@ impl Arrow2Builder {
///
/// ```rust
/// # fn main() -> serde_arrow::Result<()> {
/// # use serde_arrow::_impl::arrow2;
/// use arrow2::datatypes::Field;
/// use serde::{Serialize, Deserialize};
/// use serde_arrow::schema::{SerdeArrowSchema, TracingOptions};
/// use serde_arrow::schema::{SchemaLike, TracingOptions};
///
/// ##[derive(Serialize, Deserialize)]
/// struct Record {
Expand All @@ -129,9 +131,7 @@ impl Arrow2Builder {
/// // ...
/// ];
///
/// let fields = SerdeArrowSchema::from_type::<Record>(TracingOptions::default())?.
/// to_arrow2_fields()?;
///
/// let fields = Vec::<Field>::from_type::<Record>(TracingOptions::default())?;
/// let arrays = serde_arrow::to_arrow2(&fields, &items)?;
/// #
/// # assert_eq!(arrays.len(), 2);
Expand Down Expand Up @@ -164,22 +164,21 @@ where
///
/// ```rust
/// # fn main() -> serde_arrow::Result<()> {
/// # use serde_arrow::_impl::arrow2;
/// use arrow2::datatypes::Field;
/// use serde::{Deserialize, Serialize};
/// use serde_arrow::schema::{SerdeArrowSchema, TracingOptions};
/// use serde_arrow::schema::{SchemaLike, TracingOptions};
///
/// ##[derive(Deserialize, Serialize)]
/// struct Record {
/// a: Option<f32>,
/// b: u64,
/// }
///
/// let fields = SerdeArrowSchema::from_type::<Record>(TracingOptions::default())?
/// .to_arrow2_fields()?;
/// let fields = Vec::<Field>::from_type::<Record>(TracingOptions::default())?;
/// # let items = &[Record { a: Some(1.0), b: 2}];
/// # let arrays = serde_arrow::to_arrow2(&fields, &items).unwrap();
/// # let arrays = serde_arrow::to_arrow2(&fields, &items)?;
/// #
///
/// // deserialize the records from arrays
/// let items: Vec<Record> = serde_arrow::from_arrow2(&fields, &arrays)?;
/// # Ok(())
/// # }
Expand Down Expand Up @@ -222,13 +221,15 @@ where
}

/// Replaced by
/// [`SerdeArrowSchema::from_samples`][crate::schema::SerdeArrowSchema::from_samples]
/// [`SchemaLike::from_samples`][crate::schema::SchemaLike::from_samples]
/// (*[example][serialize_into_fields]*)
///
/// ```rust
/// # fn main() -> serde_arrow::Result<()> {
/// # use serde_arrow::_impl::arrow2;
/// use arrow2::datatypes::Field;
/// use serde::Serialize;
/// use serde_arrow::schema::{SerdeArrowSchema, TracingOptions};
/// use serde_arrow::schema::{SchemaLike, TracingOptions};
///
/// ##[derive(Serialize)]
/// struct Record {
Expand All @@ -237,14 +238,13 @@ where
/// }
///
/// let samples = [Record { a: 1, b: 2.0 }, /* ... */ ];
/// let fields = SerdeArrowSchema::from_samples(&samples, TracingOptions::default())?
/// .to_arrow2_fields()?;
/// let fields = Vec::<Field>::from_samples(&samples, TracingOptions::default())?;
/// #
/// # drop(fields);
/// # Ok(())
/// # }
/// ```
#[deprecated = "serde_arrow::arrow2::serialize_into_fields is deprecated. Use serde_arrow::schema::SerdeArrowSchema::from_samples instead"]
#[deprecated = "serde_arrow::arrow2::serialize_into_fields is deprecated. Use serde_arrow::schema::SchemaLike::from_samples instead"]
pub fn serialize_into_fields<T>(items: &T, options: TracingOptions) -> Result<Vec<Field>>
where
T: Serialize + ?Sized,
Expand Down Expand Up @@ -276,25 +276,26 @@ where
}

/// Replaced by
/// [`SerdeArrowSchema::from_samples`][crate::schema::SerdeArrowSchema::from_samples]
/// and [`Items`][crate::utils::Items] (*[example][serialize_into_field]*)
/// [`SchemaLike::from_samples`][crate::schema::SchemaLike::from_samples] and
/// [`Items`][crate::utils::Items] (*[example][serialize_into_field]*)
///
/// ```rust
/// # fn main() -> serde_arrow::Result<()> {
/// # use serde_arrow::_impl::arrow2;
/// use arrow2::datatypes::Field;
/// use serde_arrow::{
/// schema::{SerdeArrowSchema, TracingOptions},
/// schema::{SchemaLike, TracingOptions},
/// utils::Items,
/// };
///
/// let samples: Vec<u32> = vec![1, 2, 3, /* ... */ ];
/// let fields = SerdeArrowSchema::from_samples(&Items(&samples), TracingOptions::default())?
/// .to_arrow2_fields()?;
/// let fields = Vec::<Field>::from_samples(&Items(&samples), TracingOptions::default())?;
/// #
/// # drop(fields);
/// # Ok(())
/// # }
/// ```
#[deprecated = "serde_arrow::arrow2::serialize_into_field is deprecated. Use serde_arrow::schema::SerdeArrowSchema::from_samples instead"]
#[deprecated = "serde_arrow::arrow2::serialize_into_field is deprecated. Use serde_arrow::schema::SchemaLike::from_samples with serde_arrow::utils::Items instead"]
pub fn serialize_into_field<T>(items: &T, name: &str, options: TracingOptions) -> Result<Field>
where
T: Serialize + ?Sized,
Expand All @@ -310,22 +311,17 @@ where
///
/// ```rust
/// # fn main() -> serde_arrow::Result<()> {
/// use serde_arrow::{
/// schema::{SerdeArrowSchema, TracingOptions},
/// utils::Items,
/// };
/// # use serde_arrow::_impl::arrow2::datatypes::Field;
/// # use serde_arrow::schema::{SchemaLike, TracingOptions};
/// use serde_arrow::utils::Items;
///
/// let samples: Vec<u32> = vec![1, 2, 3, /* ... */ ];
/// let fields = SerdeArrowSchema::from_samples(&Items(&samples), TracingOptions::default())?
/// .to_arrow2_fields()?;
///
/// # let fields = Vec::<Field>::from_samples(&Items(&samples), TracingOptions::default())?;
/// let arrays = serde_arrow::to_arrow2(&fields, &Items(&samples))?;
/// #
/// # drop(fields);
/// # Ok(())
/// # }
/// ```
#[deprecated = "serde_arrow::arrow2::serialize_into_array is deprecated. Use serde_arrow::to_arrow2 instead"]
#[deprecated = "serde_arrow::arrow2::serialize_into_array is deprecated. Use serde_arrow::to_arrow2 with serde_arrow::utils::Items instead"]
pub fn serialize_into_array<T>(field: &Field, items: &T) -> Result<Box<dyn Array>>
where
T: Serialize + ?Sized,
Expand All @@ -346,7 +342,7 @@ where
///
/// ```rust
/// # fn main() -> serde_arrow::Result<()> {
/// # use serde_arrow::schema::{SerdeArrowSchema, TracingOptions};
/// # use serde_arrow::schema::{SerdeArrowSchema, SchemaLike, TracingOptions};
/// # let samples: Vec<u32> = vec![1, 2, 3, /* ... */ ];
/// # let fields = SerdeArrowSchema::from_samples(&Items(&samples), TracingOptions::default())?
/// # .to_arrow2_fields()?;
Expand Down Expand Up @@ -378,8 +374,9 @@ where
/// use arrow2::datatypes::{DataType, Field};
/// use serde_arrow::{Arrow2Builder, utils::{Items, Item}};
///
/// let fields = vec![Field::new("item", DataType::UInt8, false)];
/// let mut builder = Arrow2Builder::new(&fields)?;
/// let mut builder = Arrow2Builder::new(&[
/// Field::new("item", DataType::UInt8, false),
/// ])?;
///
/// builder.push(&Item(0))?;
/// builder.push(&Item(1))?;
Expand All @@ -392,7 +389,7 @@ where
/// # Ok(())
/// # }
/// ```
#[deprecated = "serde_arrow::arrow2::ArrayBuilder is deprecated. Use serde_arrow::Arrow2Builder instead"]
#[deprecated = "serde_arrow::arrow2::ArrayBuilder is deprecated. Use serde_arrow::Arrow2Builder with serde_arrow::utils::Items instead"]
pub struct ArrayBuilder(generic::GenericBuilder);

#[allow(deprecated)]
Expand Down
2 changes: 1 addition & 1 deletion serde_arrow/src/arrow2_impl/deserialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ impl BufferExtract for dyn Array {

let validity = get_validity(typed);
let offsets = typed.offsets();
let offsets = offsets.as_slice();

check_supported_list_layout(validity, offsets)?;

Expand Down Expand Up @@ -216,7 +217,6 @@ impl BufferExtract for dyn Array {
let validity = get_validity(typed);

check_supported_list_layout(validity, offsets)?;

let offsets = buffers.push_u32_cast(offsets)?;
let validity = validity.map(|b| buffers.push_u1(b));

Expand Down
29 changes: 26 additions & 3 deletions serde_arrow/src/arrow2_impl/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::{
internal::{
error::{error, fail, Error, Result},
schema::{
GenericDataType, GenericField, GenericTimeUnit, SerdeArrowSchema, Strategy,
STRATEGY_KEY,
GenericDataType, GenericField, GenericTimeUnit, SchemaLike, Sealed, SerdeArrowSchema,
Strategy, STRATEGY_KEY,
},
},
};
Expand All @@ -26,7 +26,7 @@ impl SerdeArrowSchema {
///
/// ```rust
/// # fn main() -> serde_arrow::_impl::PanicOnError<()> {
/// # use serde_arrow::schema::{SerdeArrowSchema, TracingOptions};
/// # use serde_arrow::schema::{SerdeArrowSchema, SchemaLike, TracingOptions};
/// # #[derive(serde::Deserialize)]
/// # struct Item { a: u32 }
/// # let schema = SerdeArrowSchema::from_type::<Item>(TracingOptions::default()).unwrap();
Expand Down Expand Up @@ -55,6 +55,29 @@ impl TryFrom<SerdeArrowSchema> for Vec<Field> {
}
}

impl Sealed for Vec<Field> {}

/// Schema support for `Vec<arrow2::datatype::Field>` (*requires one of the
/// `arrow2-*` features*)
impl SchemaLike for Vec<Field> {
fn from_value<T: serde::Serialize>(value: &T) -> Result<Self> {
SerdeArrowSchema::from_value(value)?.to_arrow2_fields()
}

fn from_type<'de, T: serde::Deserialize<'de>>(
options: crate::schema::TracingOptions,
) -> Result<Self> {
SerdeArrowSchema::from_type::<T>(options)?.to_arrow2_fields()
}

fn from_samples<T: serde::Serialize>(
samples: &T,
options: crate::schema::TracingOptions,
) -> Result<Self> {
SerdeArrowSchema::from_samples(samples, options)?.to_arrow2_fields()
}
}

impl TryFrom<&Field> for GenericField {
type Error = Error;

Expand Down
Loading