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: support jiff integration for postgresql #3511

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 27 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ bigdecimal = "0.4.0"
bit-vec = "0.6.3"
chrono = { version = "0.4.34", default-features = false, features = ["std", "clock"] }
ipnetwork = "0.20.0"
jiff = { version = "0.1.13" }
mac_address = "1.1.5"
rust_decimal = { version = "1.26.1", default-features = false, features = ["std"] }
time = { version = "0.3.36", features = ["formatting", "parsing", "macros"] }
Expand Down
1 change: 1 addition & 0 deletions sqlx-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ bit-vec = { workspace = true, optional = true }
bigdecimal = { workspace = true, optional = true }
rust_decimal = { workspace = true, optional = true }
time = { workspace = true, optional = true }
jiff = { workspace = true, optional = true }
ipnetwork = { workspace = true, optional = true }
mac_address = { workspace = true, optional = true }
uuid = { workspace = true, optional = true }
Expand Down
9 changes: 9 additions & 0 deletions sqlx-core/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ pub mod chrono {
};
}

#[cfg(feature = "jiff")]
#[cfg_attr(docsrs, doc(cfg(feature = "jiff")))]
pub mod jiff {
#[doc(no_inline)]
pub use jiff::{
Zoned, Timestamp, tz::TimeZone, tz::Offset, civil::Date, civil::Time, civil::DateTime,
};
}

#[cfg(feature = "bit-vec")]
#[cfg_attr(docsrs, doc(cfg(feature = "bit-vec")))]
#[doc(no_inline)]
Expand Down
4 changes: 3 additions & 1 deletion sqlx-postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ bigdecimal = ["dep:bigdecimal", "dep:num-bigint", "sqlx-core/bigdecimal"]
bit-vec = ["dep:bit-vec", "sqlx-core/bit-vec"]
chrono = ["dep:chrono", "sqlx-core/chrono"]
ipnetwork = ["dep:ipnetwork", "sqlx-core/ipnetwork"]
jiff = ["dep:jiff", "sqlx-core/jiff"]
mac_address = ["dep:mac_address", "sqlx-core/mac_address"]
rust_decimal = ["dep:rust_decimal", "rust_decimal/maths", "sqlx-core/rust_decimal"]
time = ["dep:time", "sqlx-core/time"]
Expand All @@ -35,7 +36,7 @@ futures-util = { version = "0.3.19", default-features = false, features = ["allo
# Cryptographic Primitives
crc = "3.0.0"
hkdf = "0.12.0"
hmac = { version = "0.12.0", default-features = false, features = ["reset"]}
hmac = { version = "0.12.0", default-features = false, features = ["reset"] }
md-5 = { version = "0.10.0", default-features = false }
rand = { version = "0.8.4", default-features = false, features = ["std", "std_rng"] }
sha2 = { version = "0.10.0", default-features = false }
Expand All @@ -45,6 +46,7 @@ bigdecimal = { workspace = true, optional = true }
bit-vec = { workspace = true, optional = true }
chrono = { workspace = true, optional = true }
ipnetwork = { workspace = true, optional = true }
jiff = { workspace = true, optional = true }
mac_address = { workspace = true, optional = true }
rust_decimal = { workspace = true, optional = true }
time = { workspace = true, optional = true }
Expand Down
87 changes: 87 additions & 0 deletions sqlx-postgres/src/types/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,58 @@ impl TryFrom<std::time::Duration> for PgInterval {
}
}

#[cfg(feature = "jiff")]
impl Type<Postgres> for jiff::SignedDuration {
fn type_info() -> PgTypeInfo {
PgTypeInfo::INTERVAL
}
}

#[cfg(feature = "jiff")]
impl PgHasArrayType for jiff::SignedDuration {
fn array_type_info() -> PgTypeInfo {
PgTypeInfo::INTERVAL_ARRAY
}
}

#[cfg(feature = "jiff")]
impl Encode<'_, Postgres> for jiff::SignedDuration {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> {
let pg_interval = PgInterval::try_from(*self)?;
pg_interval.encode_by_ref(buf)
}

fn size_hint(&self) -> usize {
2 * mem::size_of::<i64>()
}
}

#[cfg(feature = "jiff")]
impl TryFrom<jiff::SignedDuration> for PgInterval {
type Error = BoxDynError;

/// Convert a `jiff::SignedDuration` to a `PgInterval`.
///
/// This returns an error if there is a loss of precision using nanoseconds or if there is a
/// microseconds overflow.
fn try_from(value: jiff::SignedDuration) -> Result<Self, BoxDynError> {
if value.subsec_nanos() % 1000 != 0 {
return Err("PostgreSQL `INTERVAL` does not support nanoseconds precision".into());
}

let micros = value.as_micros();
if micros >= i64::MIN as i128 && micros <= i64::MAX as i128 {
Ok(Self {
months: 0,
days: 0,
microseconds: micros as i64,
})
} else {
Err("Overflow has occurred for PostgreSQL `INTERVAL`".into())
}
}
}

#[cfg(feature = "chrono")]
impl Type<Postgres> for chrono::Duration {
fn type_info() -> PgTypeInfo {
Expand Down Expand Up @@ -330,6 +382,41 @@ fn test_pginterval_std() {
assert!(PgInterval::try_from(std::time::Duration::from_secs(20_000_000_000_000)).is_err());
}

#[test]
#[cfg(feature = "jiff")]
fn test_pginterval_jiff() {
// Case for positive duration
let interval = PgInterval {
days: 0,
months: 0,
microseconds: 27_000,
};
assert_eq!(
&PgInterval::try_from(jiff::SignedDuration::from_micros(27_000)).unwrap(),
&interval
);

// Case for negative duration
let interval = PgInterval {
days: 0,
months: 0,
microseconds: -27_000,
};
assert_eq!(
&PgInterval::try_from(jiff::SignedDuration::from_micros(-27_000)).unwrap(),
&interval
);

// Case when precision loss occurs
assert!(PgInterval::try_from(jiff::SignedDuration::from_nanos(27_000_001)).is_err());
assert!(PgInterval::try_from(jiff::SignedDuration::from_nanos(-27_000_001)).is_err());

// Case when microseconds overflow occurs
assert!(PgInterval::try_from(jiff::SignedDuration::from_secs(10_000_000_000_000)).is_err());
assert!(PgInterval::try_from(jiff::SignedDuration::from_secs(-10_000_000_000_000)).is_err());
}


#[test]
#[cfg(feature = "chrono")]
fn test_pginterval_chrono() {
Expand Down