Skip to content

Commit

Permalink
add support for datatypes serde (jorgecarleitao#858)
Browse files Browse the repository at this point in the history
  • Loading branch information
houqp authored and Dexter Duckworth committed Mar 2, 2022
1 parent 21b01fc commit 29e563a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ compute = [
]
benchmarks = ["rand"]
simd = ["packed_simd"]
serde_types = ["serde", "serde_derive"]

[package.metadata.cargo-all-features]
allowlist = ["compute", "compute_sort", "compute_hash", "compute_nullif"]
Expand Down
4 changes: 4 additions & 0 deletions src/datatypes/field.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use super::{DataType, Metadata};

#[cfg(feature = "serde_types")]
use serde_derive::{Deserialize, Serialize};

/// Represents Arrow's metadata of a "column".
///
/// A [`Field`] is the closest representation of the traditional "column": a logical type
Expand All @@ -9,6 +12,7 @@ use super::{DataType, Metadata};
/// Almost all IO in this crate uses [`Field`] to represent logical information about the data
/// to be serialized.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serde_types", derive(Serialize, Deserialize))]
pub struct Field {
/// Its name
pub name: String,
Expand Down
7 changes: 7 additions & 0 deletions src/datatypes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ pub use schema::Schema;
use std::collections::BTreeMap;
use std::sync::Arc;

#[cfg(feature = "serde_types")]
use serde_derive::{Deserialize, Serialize};

/// typedef for [BTreeMap<String, String>] denoting [`Field`]'s and [`Schema`]'s metadata.
pub type Metadata = BTreeMap<String, String>;
/// typedef fpr [Option<(String, Option<String>)>] descr
Expand All @@ -26,6 +29,7 @@ pub(crate) type Extension = Option<(String, Option<String>)>;
/// The [`DataType::Extension`] is special in that it augments a [`DataType`] with metadata to support custom types.
/// Use `to_logical_type` to desugar such type and return its correspoding logical type.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde_types", derive(Serialize, Deserialize))]
pub enum DataType {
/// Null type
Null,
Expand Down Expand Up @@ -156,6 +160,7 @@ pub enum DataType {

/// Mode of [`DataType::Union`]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde_types", derive(Serialize, Deserialize))]
pub enum UnionMode {
/// Dense union
Dense,
Expand Down Expand Up @@ -187,6 +192,7 @@ impl UnionMode {

/// The time units defined in Arrow.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde_types", derive(Serialize, Deserialize))]
pub enum TimeUnit {
/// Time in seconds.
Second,
Expand All @@ -200,6 +206,7 @@ pub enum TimeUnit {

/// Interval units defined in Arrow
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde_types", derive(Serialize, Deserialize))]
pub enum IntervalUnit {
/// The number of elapsed whole months.
YearMonth,
Expand Down
5 changes: 5 additions & 0 deletions src/datatypes/physical_type.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
pub use crate::types::PrimitiveType;

#[cfg(feature = "serde_types")]
use serde_derive::{Deserialize, Serialize};

/// The set of physical types: unique in-memory representations of an Arrow array.
/// A physical type has a one-to-many relationship with a [`crate::datatypes::DataType`] and
/// a one-to-one mapping to each struct in this crate that implements [`crate::array::Array`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde_types", derive(Serialize, Deserialize))]
pub enum PhysicalType {
/// A Null with no allocation.
Null,
Expand Down Expand Up @@ -51,6 +55,7 @@ impl PhysicalType {
/// the set of valid indices types of a dictionary-encoded Array.
/// Each type corresponds to a variant of [`crate::array::DictionaryArray`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde_types", derive(Serialize, Deserialize))]
pub enum IntegerType {
/// A signed 8-bit integer.
Int8,
Expand Down
4 changes: 4 additions & 0 deletions src/datatypes/schema.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use super::{Field, Metadata};

#[cfg(feature = "serde_types")]
use serde_derive::{Deserialize, Serialize};

/// An ordered sequence of [`Field`]s with associated [`Metadata`].
///
/// [`Schema`] is an abstration used to read from, and write to, Arrow IPC format,
/// Apache Parquet, and Apache Avro. All these formats have a concept of a schema
/// with fields and metadata.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde_types", derive(Serialize, Deserialize))]
pub struct Schema {
/// The fields composing this schema.
pub fields: Vec<Field>,
Expand Down
4 changes: 4 additions & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ pub use native::*;
mod offset;
pub use offset::*;

#[cfg(feature = "serde_types")]
use serde_derive::{Deserialize, Serialize};

/// The set of all implementations of the sealed trait [`NativeType`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde_types", derive(Serialize, Deserialize))]
pub enum PrimitiveType {
/// A signed 8-bit integer.
Int8,
Expand Down

0 comments on commit 29e563a

Please sign in to comment.