Skip to content

Commit

Permalink
feat: add chrono feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrobbel committed Mar 25, 2024
1 parent a520c46 commit 352856b
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 9 deletions.
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,17 @@ arrow-rs = [
"dep:arrow-schema",
"narrow-derive?/arrow-rs",
]
chrono = ["dep:chrono"]
derive = ["dep:narrow-derive"]
uuid = ["dep:uuid"]

[dependencies]
arrow-array = { version = "50.0.0", optional = true }
arrow-buffer = { version = "50.0.0", optional = true }
arrow-schema = { version = "50.0.0", optional = true }
arrow-array = { version = "50.0.0", default-features = false, optional = true }
arrow-buffer = { version = "50.0.0", default-features = false, optional = true }
arrow-schema = { version = "50.0.0", default-features = false, optional = true }
chrono = { version = "0.4.35", default-features = false, optional = true }
narrow-derive = { path = "narrow-derive", version = "^0.5.0", optional = true }
uuid = { version = "1.8.0", optional = true }
uuid = { version = "1.8.0", default-features = false, optional = true }

[dev-dependencies]
arrow-cast = { version = "50.0.0", default-features = false, features = [
Expand All @@ -56,9 +58,9 @@ arrow-cast = { version = "50.0.0", default-features = false, features = [
bytes = "1.5.0"
criterion = { version = "0.5.1", default-features = false }
rand = { version = "0.8.5", default-features = false, features = ["small_rng"] }
rustversion = "1.0.14"
rustversion = { versin = "1.0.14", default-features = false }
parquet = { version = "50.0.0", default-features = false, features = ["arrow"] }
uuid = "1.8.0"
uuid = { version = "1.8.0", default-features = false }

[profile.bench]
lto = true
Expand Down
70 changes: 70 additions & 0 deletions src/logical/chrono.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use chrono::{DateTime, TimeZone, Utc};

use crate::{
array::{Array, ArrayType, FixedSizePrimitiveArray, UnionType},
buffer::BufferType,
offset::OffsetElement,
};

use super::{LogicalArray, LogicalArrayType};

impl<T: TimeZone> ArrayType for DateTime<T> {
type Array<Buffer: BufferType, OffsetItem: OffsetElement, UnionLayout: UnionType> =
LogicalArray<DateTime<T>, false, Buffer, OffsetItem, UnionLayout>;
}

impl<T: TimeZone> ArrayType for Option<DateTime<T>> {
type Array<Buffer: BufferType, OffsetItem: OffsetElement, UnionLayout: UnionType> =
LogicalArray<DateTime<T>, true, Buffer, OffsetItem, UnionLayout>;
}

impl<T: TimeZone> LogicalArrayType for DateTime<T> {
type Array<Buffer: BufferType, OffsetItem: OffsetElement, UnionLayout: UnionType> =
FixedSizePrimitiveArray<i64, false, Buffer>;

fn convert<Buffer: BufferType, OffsetItem: OffsetElement, UnionLayout: UnionType>(
self,
) -> <<Self as LogicalArrayType>::Array<Buffer, OffsetItem, UnionLayout> as Array>::Item {
self.timestamp_nanos_opt().expect("out of range")
}
}

impl<T: TimeZone> LogicalArrayType for Option<DateTime<T>> {
type Array<Buffer: BufferType, OffsetItem: OffsetElement, UnionLayout: UnionType> =
FixedSizePrimitiveArray<i64, true, Buffer>;

fn convert<Buffer: BufferType, OffsetItem: OffsetElement, UnionLayout: UnionType>(
self,
) -> <<Self as LogicalArrayType>::Array<Buffer, OffsetItem, UnionLayout> as Array>::Item {
self.map(|date_time| date_time.timestamp_nanos_opt().expect("out of range"))
}
}

impl<T: TimeZone, Buffer: BufferType, OffsetItem: OffsetElement, UnionLayout: UnionType>
From<LogicalArray<DateTime<T>, false, Buffer, OffsetItem, UnionLayout>>
for FixedSizePrimitiveArray<i64, false, Buffer>
{
fn from(value: LogicalArray<DateTime<T>, false, Buffer, OffsetItem, UnionLayout>) -> Self {
value.0
}
}

/// An array for [`Uuid`] items.
#[allow(unused)]
pub type DateTimeArray<T = Utc, const NULLABLE: bool = false, Buffer = crate::buffer::VecBuffer> =
LogicalArray<DateTime<T>, NULLABLE, Buffer, crate::offset::NA, crate::array::union::NA>;

#[cfg(test)]
mod tests {
use super::*;
use crate::Length;

#[test]
fn from_iter() {
let array = [DateTime::<Utc>::UNIX_EPOCH, DateTime::<Utc>::UNIX_EPOCH]
.into_iter()
.collect::<DateTimeArray>();
assert_eq!(array.len(), 2);
assert_eq!(array.0.len(), 2);
}
}
4 changes: 4 additions & 0 deletions src/logical/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ use crate::{
/// Uuid support via logical arrays.
mod uuid;

#[cfg(feature = "chrono")]
/// Chrono support via logical arrays.
mod chrono;

/// Types that can be stored in Arrow arrays, but require mapping via
/// [`LogicalArray`].
///
Expand Down

0 comments on commit 352856b

Please sign in to comment.