use jiff::SignedDuration;
use jiff::civil::{Date, DateTime, Time, date};
use sqlx::decode::Decode;
use sqlx::encode::{Encode, IsNull};
use sqlx::error::BoxDynError;
use sqlx::postgres::types::Oid;
use sqlx::postgres::{PgArgumentBuffer, PgTypeInfo, PgValueFormat, PgValueRef, Postgres};
const PG_EPOCH_DATE: Date = date(2000, 1, 1);
const PG_EPOCH_DATETIME: DateTime = PG_EPOCH_DATE.at(0, 0, 0, 0);
const PG_EPOCH_UNIX_NANOS: i128 = 946_684_800_000_000_000;
const DAY_MICROS: i64 = 86_400_000_000;
const PG_MIN_MICROS: i64 = -211_813_488_000_000_000;
const PG_MIN_DAYS: i32 = -2_451_545;
pub(super) struct PgTimestamp(pub(super) jiff::Timestamp);
pub(super) struct PgDateTime(pub(super) DateTime);
pub(super) struct PgDate(pub(super) Date);
pub(super) struct PgTime(pub(super) Time);
fn saturate(v: i128) -> i64 {
i64::try_from(v).unwrap_or(if v < 0 { i64::MIN } else { i64::MAX })
}
fn timestamp_micros(ts: jiff::Timestamp) -> i64 {
saturate((ts.as_nanosecond() - PG_EPOCH_UNIX_NANOS).div_euclid(1_000))
}
fn datetime_micros(dt: DateTime) -> i64 {
saturate(
dt.duration_since(PG_EPOCH_DATETIME)
.as_nanos()
.div_euclid(1_000),
)
}
fn date_days(d: Date) -> i64 {
d.duration_since(PG_EPOCH_DATE).as_secs() / 86_400
}
fn checked_micros(micros: i64, what: &dyn std::fmt::Display) -> Result<i64, String> {
if micros < PG_MIN_MICROS {
return Err(format!(
"{what} is before PostgreSQL's earliest, 4714-11-24 00:00:00 BC"
));
}
Ok(micros)
}
fn timestamp_to_wire(ts: jiff::Timestamp) -> Result<i64, String> {
checked_micros(timestamp_micros(ts), &ts)
}
fn datetime_to_wire(dt: DateTime) -> Result<i64, String> {
checked_micros(datetime_micros(dt), &dt)
}
fn date_to_wire(d: Date) -> Result<i32, String> {
i32::try_from(date_days(d))
.ok()
.filter(|days| *days >= PG_MIN_DAYS)
.ok_or_else(|| format!("{d} is before PostgreSQL's earliest date, 4714-11-24 BC"))
}
fn time_to_wire(t: Time) -> Result<i64, String> {
i64::try_from(t.duration_since(Time::midnight()).as_micros()).map_err(|e| e.to_string())
}
fn representable<T: PartialOrd + std::fmt::Display>(
ty: &str,
wire: T,
(min, max): (T, T),
jiff: (T, T),
) -> Result<(), String> {
let infinity = match &wire {
w if *w == max => "infinity",
w if *w == min => "-infinity",
w if *w < jiff.0 || *w > jiff.1 => {
return Err(format!(
"{ty} {wire} on the wire is beyond jiff's years ±9999"
));
}
_ => return Ok(()),
};
Err(format!("{ty} is {infinity}, which has no jiff value"))
}
fn timestamp_from_wire(micros: i64) -> Result<jiff::Timestamp, String> {
let jiff = (
timestamp_micros(jiff::Timestamp::MIN),
timestamp_micros(jiff::Timestamp::MAX),
);
representable("timestamptz", micros, (i64::MIN, i64::MAX), jiff)?;
jiff::Timestamp::from_nanosecond(i128::from(micros) * 1_000 + PG_EPOCH_UNIX_NANOS)
.map_err(|e| e.to_string())
}
fn datetime_from_wire(micros: i64) -> Result<DateTime, String> {
let jiff = (
datetime_micros(DateTime::MIN),
datetime_micros(DateTime::MAX),
);
representable("timestamp", micros, (i64::MIN, i64::MAX), jiff)?;
PG_EPOCH_DATETIME
.checked_add(SignedDuration::from_micros(micros))
.map_err(|e| e.to_string())
}
fn date_from_wire(days: i32) -> Result<Date, String> {
let jiff = (date_days(Date::MIN), date_days(Date::MAX));
representable(
"date",
i64::from(days),
(i32::MIN.into(), i32::MAX.into()),
jiff,
)?;
PG_EPOCH_DATE
.checked_add(SignedDuration::from_hours(i64::from(days) * 24))
.map_err(|e| e.to_string())
}
fn time_from_wire(micros: i64) -> Result<Time, String> {
if !(0..DAY_MICROS).contains(µs) {
return Err(format!("time of day {micros}µs is not before 24:00"));
}
Time::midnight()
.checked_add(SignedDuration::from_micros(micros))
.map_err(|e| e.to_string())
}
impl PgTimestamp {
pub(super) fn checked(ts: jiff::Timestamp) -> Result<Self, String> {
timestamp_to_wire(ts).map(|_| Self(ts))
}
}
impl PgDateTime {
pub(super) fn checked(dt: DateTime) -> Result<Self, String> {
datetime_to_wire(dt).map(|_| Self(dt))
}
}
impl PgDate {
pub(super) fn checked(d: Date) -> Result<Self, String> {
date_to_wire(d).map(|_| Self(d))
}
}
fn wire_bytes<const N: usize>(value: &PgValueRef<'_>, ty: &str) -> Result<[u8; N], BoxDynError> {
if value.format() == PgValueFormat::Text {
return Err(format!("{ty} arrived in text format; only the binary format is decoded").into());
}
let bytes = value.as_bytes()?;
<[u8; N]>::try_from(bytes)
.map_err(|_| format!("{ty} has {} bytes on the wire, not {N}", bytes.len()).into())
}
macro_rules! pg_codec {
($wrapper:ident, $oid:literal, $name:literal, $int:ty, $to_wire:expr, $from_wire:expr) => {
impl sqlx::Type<Postgres> for $wrapper {
fn type_info() -> PgTypeInfo {
PgTypeInfo::with_oid(Oid($oid))
}
}
impl Encode<'_, Postgres> for $wrapper {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> {
let wire: $int = $to_wire(self.0)?;
Encode::<Postgres>::encode_by_ref(&wire, buf)
}
fn size_hint(&self) -> usize {
size_of::<$int>()
}
}
impl<'r> Decode<'r, Postgres> for $wrapper {
fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
let wire = <$int>::from_be_bytes(wire_bytes(&value, $name)?);
Ok(Self($from_wire(wire)?))
}
}
};
}
pg_codec!(
PgTimestamp,
1184,
"timestamptz",
i64,
timestamp_to_wire,
timestamp_from_wire
);
pg_codec!(
PgDateTime,
1114,
"timestamp",
i64,
datetime_to_wire,
datetime_from_wire
);
pg_codec!(PgDate, 1082, "date", i32, date_to_wire, date_from_wire);
pg_codec!(PgTime, 1083, "time", i64, time_to_wire, time_from_wire);
#[cfg(test)]
mod tests {
use id_effect::{Cause, Effect, Exit};
use id_effect_tokio::TokioRuntime;
use jiff::civil::{date, time};
use super::*;
use crate::pg::{PgPoolConfig, PgSqlClient, pg_pool_from_config};
use crate::{SqlClient, SqlError, SqlValue};
#[test]
fn binary_temporals_decode_with_checked_arithmetic() {
assert_eq!(
timestamp_from_wire(0).unwrap(),
"2000-01-01T00:00:00Z".parse::<jiff::Timestamp>().unwrap()
);
assert_eq!(
datetime_from_wire(-1).unwrap(),
date(1999, 12, 31).at(23, 59, 59, 999_999_000)
);
assert_eq!(date_from_wire(366).unwrap(), date(2001, 1, 1));
assert_eq!(time_from_wire(3_600_000_001).unwrap(), time(1, 0, 0, 1_000));
assert!(time_from_wire(DAY_MICROS).unwrap_err().contains("24:00"));
assert!(time_from_wire(-1).is_err());
}
#[test]
fn encoding_truncates_sub_microseconds_towards_the_past() {
let before_epoch = date(1999, 12, 31).at(23, 59, 59, 999_999_999);
assert_eq!(datetime_to_wire(before_epoch), Ok(-1));
let ts = "1999-12-31T23:59:59.9999995Z"
.parse::<jiff::Timestamp>()
.unwrap();
assert_eq!(timestamp_to_wire(ts), Ok(-1));
assert_eq!(time_to_wire(time(0, 0, 0, 1_999)), Ok(1));
}
#[test]
fn saturate_clamps_beyond_i64() {
assert_eq!(saturate(i128::from(i64::MAX) + 1), i64::MAX);
assert_eq!(saturate(i128::from(i64::MIN) - 1), i64::MIN);
assert_eq!(saturate(-42), -42);
}
fn encoded<T: for<'q> Encode<'q, Postgres>>(value: &T) -> (Result<bool, String>, Vec<u8>) {
let mut buf = PgArgumentBuffer::default();
let result = value
.encode_by_ref(&mut buf)
.map(|is_null| matches!(is_null, IsNull::Yes))
.map_err(|e| e.to_string());
(result, buf.to_vec())
}
fn oid_of<T: sqlx::Type<Postgres>>() -> Option<Oid> {
T::type_info().oid()
}
#[test]
fn codecs_declare_their_oids_and_sizes() {
assert_eq!(oid_of::<PgTimestamp>(), Some(Oid(1184)));
assert_eq!(oid_of::<PgDateTime>(), Some(Oid(1114)));
assert_eq!(oid_of::<PgDate>(), Some(Oid(1082)));
assert_eq!(oid_of::<PgTime>(), Some(Oid(1083)));
let epoch = date(2000, 1, 1);
assert_eq!(PgTimestamp(jiff::Timestamp::UNIX_EPOCH).size_hint(), 8);
assert_eq!(PgDateTime(epoch.at(0, 0, 0, 0)).size_hint(), 8);
assert_eq!(PgDate(epoch).size_hint(), 4);
assert_eq!(PgTime(Time::midnight()).size_hint(), 8);
}
#[test]
fn codecs_encode_the_big_endian_wire_integer() {
let ts = "2000-01-01T00:00:01Z".parse::<jiff::Timestamp>().unwrap();
let (result, bytes) = encoded(&PgTimestamp(ts));
assert_eq!(result, Ok(false));
assert_eq!(bytes, 1_000_000i64.to_be_bytes());
let (result, bytes) = encoded(&PgDateTime(date(1999, 12, 31).at(23, 59, 59, 999_999_000)));
assert_eq!(result, Ok(false));
assert_eq!(bytes, (-1i64).to_be_bytes());
let (result, bytes) = encoded(&PgDate(date(2000, 1, 2)));
assert_eq!(result, Ok(false));
assert_eq!(bytes, 1i32.to_be_bytes());
let (result, bytes) = encoded(&PgTime(time(0, 0, 1, 0)));
assert_eq!(result, Ok(false));
assert_eq!(bytes, 1_000_000i64.to_be_bytes());
}
#[test]
fn encoding_before_pg_earliest_fails_without_writing() {
let (result, bytes) = encoded(&PgTimestamp(jiff::Timestamp::MIN));
assert!(result.unwrap_err().contains("4714-11-24"));
assert!(bytes.is_empty());
let (result, _) = encoded(&PgDateTime(DateTime::MIN));
assert!(result.unwrap_err().contains("earliest"));
let (result, _) = encoded(&PgDate(Date::MIN));
assert!(result.unwrap_err().contains("earliest date"));
}
#[test]
fn checked_wrappers_accept_values_in_range() {
let ts = "2024-02-29T12:34:56Z".parse::<jiff::Timestamp>().unwrap();
assert_eq!(PgTimestamp::checked(ts).map(|w| w.0), Ok(ts));
let dt = date(2024, 2, 29).at(12, 34, 56, 0);
assert_eq!(PgDateTime::checked(dt).map(|w| w.0), Ok(dt));
assert_eq!(
PgDate::checked(date(2024, 2, 29)).map(|w| w.0),
Ok(date(2024, 2, 29))
);
assert!(
PgDateTime::checked(DateTime::MIN)
.err()
.is_some_and(|e| e.contains("BC"))
);
}
#[test]
fn wire_round_trips_at_jiff_edges() {
let max = datetime_micros(DateTime::MAX);
assert_eq!(datetime_from_wire(max).map(datetime_micros), Ok(max));
assert!(datetime_from_wire(max + 1).unwrap_err().contains("beyond"));
let ts_max = timestamp_micros(jiff::Timestamp::MAX);
assert_eq!(
timestamp_from_wire(ts_max).map(timestamp_micros),
Ok(ts_max)
);
let days = date_days(Date::MAX);
let days = i32::try_from(days).unwrap();
assert_eq!(date_from_wire(days), Ok(Date::MAX));
assert!(date_from_wire(days + 1).unwrap_err().contains("beyond"));
assert!(date_from_wire(-5_000_000).unwrap_err().contains("beyond"));
assert_eq!(
time_to_wire(time(23, 59, 59, 999_999_999)),
Ok(DAY_MICROS - 1)
);
assert_eq!(
time_from_wire(DAY_MICROS - 1),
Ok(time(23, 59, 59, 999_999_000))
);
assert!(
representable("date", 3i64, (i64::MIN, i64::MAX), (0, 2))
.unwrap_err()
.contains("date 3 on the wire")
);
assert_eq!(
representable("date", 2i64, (i64::MIN, i64::MAX), (0, 2)),
Ok(())
);
}
async fn run<A: Send + 'static>(eff: Effect<A, SqlError>) -> Result<A, SqlError> {
match TokioRuntime::current().expect("tokio").run(eff).await {
Exit::Success(a) => Ok(a),
Exit::Failure(Cause::Fail(e)) => Err(e),
Exit::Failure(c) => panic!("unexpected cause: {c:?}"),
}
}
async fn live_pg(test: &str) -> Option<PgSqlClient> {
let unavailable = |why: String| {
assert!(
!std::env::var("ID_EFFECT_REQUIRE_PG").is_ok_and(|v| v == "1"),
"{test}: ID_EFFECT_REQUIRE_PG=1 but Postgres is unavailable: {why}"
);
eprintln!("SKIP {test}: {why} (set DATABASE_URL, and ID_EFFECT_REQUIRE_PG=1 to require it)");
None
};
let Ok(url) = std::env::var("DATABASE_URL") else {
return unavailable("DATABASE_URL is not set".into());
};
let pool = match pg_pool_from_config(PgPoolConfig::from_url(url).with_max_size(1)).await {
Ok(pool) => pool,
Err(e) => return unavailable(format!("cannot build pool: {}", e.0)),
};
let client = PgSqlClient::new(pool);
match run(client.connect()).await {
Ok(()) => Some(client),
Err(e) => unavailable(format!("cannot connect: {e:?}")),
}
}
async fn one(client: &PgSqlClient, sql: &str, params: &[SqlValue]) -> Result<SqlValue, SqlError> {
let rows = run(client.query(sql, params)).await?;
Ok(rows[0].values()[0].clone())
}
#[tokio::test]
async fn pg_wire_datetime_round_trip_bounds() {
let jiff_max = jiff::Timestamp::from_microsecond(jiff::Timestamp::MAX.as_microsecond()).ok();
let pg_min_ts = "-004713-11-24T00:00:00Z"
.parse::<jiff::Timestamp>()
.unwrap();
let mut values = vec![
SqlValue::Timestamp(pg_min_ts),
SqlValue::Timestamp("1999-12-31T23:59:59.999999Z".parse().unwrap()),
SqlValue::Timestamp("2024-02-29T12:34:56.123456Z".parse().unwrap()),
SqlValue::DateTime(date(-4713, 11, 24).at(0, 0, 0, 0)),
SqlValue::DateTime(date(9999, 12, 31).at(23, 59, 59, 999_999_000)),
SqlValue::Date(date(-4713, 11, 24)),
SqlValue::Date(date(1, 1, 1)),
SqlValue::Date(date(9999, 12, 31)),
SqlValue::Time(time(0, 0, 0, 0)),
SqlValue::Time(time(23, 59, 59, 999_999_000)),
];
values.extend(jiff_max.map(SqlValue::Timestamp));
assert_eq!(timestamp_to_wire(pg_min_ts), Ok(PG_MIN_MICROS));
assert_eq!(date_to_wire(date(-4713, 11, 24)), Ok(PG_MIN_DAYS));
assert_eq!(timestamp_from_wire(PG_MIN_MICROS), Ok(pg_min_ts));
assert_eq!(date_from_wire(PG_MIN_DAYS), Ok(date(-4713, 11, 24)));
assert!(PgTimestamp::checked(jiff::Timestamp::MIN).is_err());
assert!(PgDateTime::checked(date(-4713, 11, 23).at(23, 59, 59, 0)).is_err());
assert!(PgDate::checked(date(-9999, 1, 1)).is_err());
let Some(client) = live_pg("pg_wire_datetime_round_trip_bounds").await else {
return;
};
for (n, value) in values.iter().enumerate() {
let sql = format!("SELECT $1 AS v{n}");
let back = one(&client, &sql, std::slice::from_ref(value)).await;
assert_eq!(back.as_ref(), Ok(value), "{sql}");
}
for (literal, value) in [
(
"'2000-01-01 00:00:00+00'::timestamptz",
SqlValue::Timestamp("2000-01-01T00:00:00Z".parse().unwrap()),
),
(
"'4714-11-24 00:00:00+00 BC'::timestamptz",
SqlValue::Timestamp(pg_min_ts),
),
(
"'1970-01-01 00:00:00.000001'::timestamp",
SqlValue::DateTime(date(1970, 1, 1).at(0, 0, 0, 1_000)),
),
("'4714-11-24 BC'::date", SqlValue::Date(date(-4713, 11, 24))),
("'2024-02-29'::date", SqlValue::Date(date(2024, 2, 29))),
(
"'23:59:59.999999'::time",
SqlValue::Time(time(23, 59, 59, 999_999_000)),
),
] {
assert_eq!(
one(&client, &format!("SELECT {literal}"), &[]).await,
Ok(value.clone()),
"{literal}"
);
let same = one(&client, &format!("SELECT $1 = {literal}"), &[value]).await;
assert_eq!(same, Ok(SqlValue::Bool(true)), "{literal}");
}
let err = one(
&client,
"SELECT $1::date",
&[SqlValue::Date(date(-9999, 1, 1))],
)
.await;
assert!(
matches!(&err, Err(SqlError::ValueOutOfRange { column, .. }) if column == "$1"),
"{err:?}"
);
}
#[tokio::test]
async fn pg_infinity_is_an_error() {
assert!(
timestamp_from_wire(i64::MAX)
.unwrap_err()
.contains("is infinity")
);
assert!(
timestamp_from_wire(i64::MIN)
.unwrap_err()
.contains("-infinity")
);
assert!(
datetime_from_wire(i64::MAX)
.unwrap_err()
.contains("infinity")
);
assert!(date_from_wire(i32::MAX).unwrap_err().contains("infinity"));
assert!(date_from_wire(i32::MIN).unwrap_err().contains("-infinity"));
assert!(date_from_wire(5_000_000).unwrap_err().contains("beyond"));
assert!(
timestamp_from_wire(i64::MAX - 1)
.unwrap_err()
.contains("beyond")
);
assert!(
datetime_from_wire(i64::MIN + 1)
.unwrap_err()
.contains("beyond")
);
let Some(client) = live_pg("pg_infinity_is_an_error").await else {
return;
};
for (sql, needle) in [
("SELECT 'infinity'::timestamptz AS v", "infinity"),
("SELECT '-infinity'::timestamptz AS v", "-infinity"),
("SELECT 'infinity'::timestamp AS v", "infinity"),
("SELECT '-infinity'::timestamp AS v", "-infinity"),
("SELECT 'infinity'::date AS v", "infinity"),
("SELECT '-infinity'::date AS v", "-infinity"),
("SELECT '294276-12-31'::date AS v", "beyond"),
(
"SELECT '294276-12-31 00:00:00+00'::timestamptz AS v",
"beyond",
),
("SELECT '24:00:00'::time AS v", "24:00"),
] {
match one(&client, sql, &[]).await {
Err(SqlError::ValueOutOfRange { column, message }) => {
assert_eq!(column, "v", "{sql}");
assert!(message.contains(needle), "{sql}: {message}");
}
other => panic!("{sql}: {other:?}"),
}
}
}
}