From 436f60f03b6421666cfb882cf393a493620db75c Mon Sep 17 00:00:00 2001 From: dracarys18 Date: Wed, 6 Nov 2024 13:11:52 +0530 Subject: [PATCH 1/3] feat: implment FromCqlVal and SerializeValue for PrimitiveDatetime --- scylla-cql/src/frame/response/cql_to_rust.rs | 9 +++++++++ scylla-cql/src/frame/value.rs | 7 +++++++ scylla-cql/src/types/serialize/value.rs | 9 +++++++++ 3 files changed, 25 insertions(+) diff --git a/scylla-cql/src/frame/response/cql_to_rust.rs b/scylla-cql/src/frame/response/cql_to_rust.rs index 8f199b83eb..6f6e348bb4 100644 --- a/scylla-cql/src/frame/response/cql_to_rust.rs +++ b/scylla-cql/src/frame/response/cql_to_rust.rs @@ -228,6 +228,15 @@ impl FromCqlVal for time_03::OffsetDateTime { } } +#[cfg(feature = "time-03")] +impl FromCqlVal for time_03::PrimitiveDateTime { + fn from_cql(cql_val: CqlValue) -> Result { + let datetime = time_03::OffsetDateTime::from_cql(cql_val)?; + + Ok(Self::new(datetime.date(), datetime.time())) + } +} + #[cfg(feature = "secrecy-08")] impl + secrecy_08::Zeroize> FromCqlVal for secrecy_08::Secret { fn from_cql(cql_val: CqlValue) -> Result { diff --git a/scylla-cql/src/frame/value.rs b/scylla-cql/src/frame/value.rs index c479b64d01..d66efdd847 100644 --- a/scylla-cql/src/frame/value.rs +++ b/scylla-cql/src/frame/value.rs @@ -608,6 +608,13 @@ impl From for CqlTimestamp { } } +#[cfg(feature = "time-03")] +impl From for CqlTimestamp { + fn from(value: time_03::PrimitiveDateTime) -> Self { + value.assume_utc().into() + } +} + #[cfg(feature = "time-03")] impl TryInto for CqlTimestamp { type Error = ValueOverflow; diff --git a/scylla-cql/src/types/serialize/value.rs b/scylla-cql/src/types/serialize/value.rs index 22c3372c6b..e15c02adc8 100644 --- a/scylla-cql/src/types/serialize/value.rs +++ b/scylla-cql/src/types/serialize/value.rs @@ -196,6 +196,15 @@ impl SerializeValue for time_03::OffsetDateTime { ::serialize(&(*me).into(), typ, writer)? }); } + +#[cfg(feature = "time-03")] +impl SerializeValue for time_03::PrimitiveDateTime { + impl_serialize_via_writer!(|me, typ, writer| { + exact_type_check!(typ, Timestamp); + ::serialize(&(*me).into(), typ, writer)? + }); +} + #[cfg(feature = "time-03")] impl SerializeValue for time_03::Time { impl_serialize_via_writer!(|me, typ, writer| { From a03c61d49d02affe8a5a817e16fed8f5bcf3a51b Mon Sep 17 00:00:00 2001 From: dracarys18 Date: Wed, 13 Nov 2024 10:32:19 +0530 Subject: [PATCH 2/3] feat: impl deserializevalue for primitivedatetime --- scylla-cql/src/types/deserialize/value.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/scylla-cql/src/types/deserialize/value.rs b/scylla-cql/src/types/deserialize/value.rs index b07c6eb478..204214987b 100644 --- a/scylla-cql/src/types/deserialize/value.rs +++ b/scylla-cql/src/types/deserialize/value.rs @@ -577,6 +577,16 @@ impl_emptiable_strict_type!( } ); +#[cfg(feature = "time-03")] +impl_emptiable_strict_type!( + time_03::PrimitiveDateTime, + Timestamp, + |typ: &'metadata ColumnType<'metadata>, v: Option>| { + let time = time_03::OffsetDateTime::deserialize(typ, v)?; + Ok(Self::new(time.date(), time.time())) + } +); + // inet impl_emptiable_strict_type!( From 47c89d514722dfeae506b5fc2f67e4c4ba7f69e9 Mon Sep 17 00:00:00 2001 From: dracarys18 Date: Wed, 13 Nov 2024 13:38:49 +0530 Subject: [PATCH 3/3] fix: add unit test --- scylla-cql/src/types/deserialize/value_tests.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/scylla-cql/src/types/deserialize/value_tests.rs b/scylla-cql/src/types/deserialize/value_tests.rs index 2ce5d0ea44..1921e04b39 100644 --- a/scylla-cql/src/types/deserialize/value_tests.rs +++ b/scylla-cql/src/types/deserialize/value_tests.rs @@ -258,6 +258,20 @@ fn test_date_time_types() { &time_03::OffsetDateTime::from_unix_timestamp(0xdead_cafe).unwrap(), &mut Bytes::new(), ); + + #[cfg(feature = "time-03")] + assert_ser_de_identity( + &ColumnType::Timestamp, + &time_03::PrimitiveDateTime::new( + time_03::OffsetDateTime::from_unix_timestamp(0xdead_cafe) + .unwrap() + .date(), + time_03::OffsetDateTime::from_unix_timestamp(0xdead_cafe) + .unwrap() + .time(), + ), + &mut Bytes::new(), + ); } #[test]