Skip to content

Commit

Permalink
Add support for the postgres multirange type
Browse files Browse the repository at this point in the history
  • Loading branch information
guissalustiano committed Aug 12, 2024
1 parent bce4881 commit 5eed8e3
Show file tree
Hide file tree
Showing 8 changed files with 324 additions and 1 deletion.
36 changes: 36 additions & 0 deletions diesel/src/pg/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod json;
mod mac_addr;
#[doc(hidden)]
pub(in crate::pg) mod money;
mod multirange;
#[cfg(feature = "network-address")]
mod network_address;
mod numeric;
Expand Down Expand Up @@ -171,6 +172,41 @@ pub mod sql_types {
#[doc(hidden)]
pub type Tstzrange = Range<crate::sql_types::Timestamptz>;

/// The [`Multirange`] SQL type.
///
/// This wraps another type to represent a SQL range of that type.
///
/// ### [`ToSql`] impls
///
/// - [`Vec<T>`][Vec] for any `T` which implements `ToSql<Range<ST>>`
/// - [`&[T]`][slice] for any `T` which implements `ToSql<ST>`
///
/// ### [`FromSql`] impls
///
/// - [`Vec<T>`][Vec] for any `T` which implements `ToSql<Range<ST>>`
///
/// [`ToSql`]: crate::serialize::ToSql
/// [`FromSql`]: crate::deserialize::FromSql
/// [Vec]: std::vec::Vec
/// [slice]: https://doc.rust-lang.org/nightly/std/primitive.slice.html
/// [`Multirange`]: https://www.postgresql.org/docs/current/rangetypes.html
#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)]
#[cfg(feature = "postgres_backend")]
pub struct Multirange<ST: 'static>(ST);

#[doc(hidden)]
pub type Int4multirange = Multirange<crate::sql_types::Int4>;
#[doc(hidden)]
pub type Int8multirange = Multirange<crate::sql_types::Int8>;
#[doc(hidden)]
pub type Datemultirange = Multirange<crate::sql_types::Date>;
#[doc(hidden)]
pub type Nummultirange = Multirange<crate::sql_types::Numeric>;
#[doc(hidden)]
pub type Tsmultirange = Multirange<crate::sql_types::Timestamp>;
#[doc(hidden)]
pub type Tstzmultirange = Multirange<crate::sql_types::Timestamptz>;

/// This is a wrapper for [`RangeBound`] to represent range bounds: '[]', '(]', '[)', '()',
/// used in functions int4range, int8range, numrange, tsrange, tstzrange, daterange.
#[derive(Debug, Clone, Copy, QueryId, SqlType)]
Expand Down
111 changes: 111 additions & 0 deletions diesel/src/pg/types/multirange.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use byteorder::{NetworkEndian, ReadBytesExt, WriteBytesExt};
use std::io::Write;
use std::ops::Bound;

use crate::deserialize::{self, FromSql};
use crate::expression::bound::Bound as SqlBound;
use crate::expression::AsExpression;
use crate::pg::{Pg, PgTypeMetadata, PgValue};
use crate::query_builder::bind_collector::ByteWrapper;
use crate::serialize::{self, IsNull, Output, ToSql};
use crate::sql_types::*;

// from `SELECT oid, typname FROM pg_catalog.pg_type where typname LIKE '%multirange'`;
macro_rules! multirange_has_sql_type {
($ty:ty, $oid:expr, $array_oid:expr) => {
#[cfg(feature = "postgres_backend")]
impl HasSqlType<$ty> for Pg {
fn metadata(_: &mut Self::MetadataLookup) -> PgTypeMetadata {
PgTypeMetadata::new($oid, $array_oid)
}
}
};
}
multirange_has_sql_type!(Datemultirange, 4535, 6155);
multirange_has_sql_type!(Int4multirange, 4451, 6150);
multirange_has_sql_type!(Int8multirange, 4536, 6157);
multirange_has_sql_type!(Nummultirange, 4532, 6151);
multirange_has_sql_type!(Tsmultirange, 4533, 6152);
multirange_has_sql_type!(Tstzmultirange, 4534, 6153);

macro_rules! multirange_as_expression {
($ty:ty, $sql_type:ty) => {
#[cfg(feature = "postgres_backend")]
// this simplifies the macro implementation
// as some macro calls use this lifetime
#[allow(clippy::extra_unused_lifetimes)]
impl<'a, 'b, ST: 'static, T> AsExpression<$sql_type> for $ty {
type Expression = SqlBound<$sql_type, Self>;
fn as_expression(self) -> Self::Expression {
SqlBound::new(self)
}
}
};
}

multirange_as_expression!(&'a [(Bound<T>, Bound<T>)], Multirange<ST>);
multirange_as_expression!(&'a [(Bound<T>, Bound<T>)], Nullable<Multirange<ST>>);
multirange_as_expression!(&'a &'b [(Bound<T>, Bound<T>)], Multirange<ST>);
multirange_as_expression!(&'a &'b [(Bound<T>, Bound<T>)], Nullable<Multirange<ST>>);
multirange_as_expression!(Vec<(Bound<T>, Bound<T>)>, Multirange<ST>);
multirange_as_expression!(Vec<(Bound<T>, Bound<T>)>, Nullable<Multirange<ST>>);
multirange_as_expression!(&'a Vec<(Bound<T>, Bound<T>)>, Multirange<ST>);
multirange_as_expression!(&'a Vec<(Bound<T>, Bound<T>)>, Nullable<Multirange<ST>>);
multirange_as_expression!(&'a &'b Vec<(Bound<T>, Bound<T>)>, Multirange<ST>);
multirange_as_expression!(&'a &'b Vec<(Bound<T>, Bound<T>)>, Nullable<Multirange<ST>>);

#[cfg(feature = "postgres_backend")]
impl<T, ST> FromSql<Multirange<ST>, Pg> for Vec<(Bound<T>, Bound<T>)>
where
T: FromSql<ST, Pg>,
{
fn from_sql(value: PgValue<'_>) -> deserialize::Result<Self> {
let mut bytes = value.as_bytes();
let len = bytes.read_i32::<NetworkEndian>()?;

(0..len)
.map(|_| {
let range_size = bytes.read_i32::<NetworkEndian>()?;
let (range_bytes, new_bytes) = bytes.split_at(range_size as usize);
bytes = new_bytes;
FromSql::from_sql(PgValue::new_internal(range_bytes, &value))
})
.collect()
}
}

#[cfg(feature = "postgres_backend")]
impl<T, ST> ToSql<Multirange<ST>, Pg> for [(Bound<T>, Bound<T>)]
where
T: ToSql<ST, Pg>,
{
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
out.write_u32::<NetworkEndian>(self.len() as u32)?;

let mut buffer = Vec::new();
for value in self {
{
let mut inner_buffer = Output::new(ByteWrapper(&mut buffer), out.metadata_lookup());
ToSql::<Range<ST>, Pg>::to_sql(&value, &mut inner_buffer)?;
}
out.write_u32::<NetworkEndian>(buffer.len() as u32)?;
out.write_all(&buffer)?;
buffer.clear();
}

Ok(IsNull::No)
}
}

#[cfg(feature = "postgres_backend")]
impl<T, ST> ToSql<Multirange<ST>, Pg> for Vec<(Bound<T>, Bound<T>)>
where
T: ToSql<ST, Pg>,
[(Bound<T>, Bound<T>)]: ToSql<Multirange<ST>, Pg>,
{
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
ToSql::<Multirange<ST>, Pg>::to_sql(self.as_slice(), out)
}
}

// TODO: nullable impl
6 changes: 6 additions & 0 deletions diesel_tests/tests/schema/pg_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ table! {
ts -> Tsrange,
tstz -> Tstzrange,
date -> Daterange,
int4multi -> Int4multirange,
int8multi -> Int8multirange,
nummulti -> Nummultirange,
tsmulti -> Tsmultirange,
tstzmulti -> Tstzmultirange,
datemulti -> Datemultirange,
}
}

Expand Down
17 changes: 16 additions & 1 deletion diesel_tests/tests/schema_inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ mod postgres {
ts: (Bound<NaiveDateTime>, Bound<NaiveDateTime>),
tstz: (Bound<DateTime<Utc>>, Bound<DateTime<Utc>>),
date: (Bound<NaiveDate>, Bound<NaiveDate>),
int4multi: Vec<(Bound<i32>, Bound<i32>)>,
int8multi: Vec<(Bound<i64>, Bound<i64>)>,
nummulti: Vec<(Bound<PgNumeric>, Bound<PgNumeric>)>,
tsmulti: Vec<(Bound<NaiveDateTime>, Bound<NaiveDateTime>)>,
tstzmulti: Vec<(Bound<DateTime<Utc>>, Bound<DateTime<Utc>>)>,
datemulti: Vec<(Bound<NaiveDate>, Bound<NaiveDate>)>,
}

#[test]
Expand All @@ -225,13 +231,22 @@ mod postgres {
let inferred_ranges = InferredRanges {
int4: (Bound::Included(5), Bound::Excluded(12)),
int8: (Bound::Included(5), Bound::Excluded(13)),
num: (Bound::Included(numeric), Bound::Unbounded),
num: (Bound::Included(numeric.clone()), Bound::Unbounded),
ts: (Bound::Included(dt), Bound::Unbounded),
tstz: (
Bound::Unbounded,
Bound::Excluded(Utc.from_utc_datetime(&dt)),
),
date: (Bound::Included(dt.date()), Bound::Unbounded),
int4multi: vec![(Bound::Included(5), Bound::Excluded(12))],
int8multi: vec![(Bound::Included(5), Bound::Excluded(13))],
nummulti: vec![(Bound::Included(numeric), Bound::Unbounded)],
tsmulti: vec![(Bound::Included(dt), Bound::Unbounded)],
tstzmulti: vec![(
Bound::Unbounded,
Bound::Excluded(Utc.from_utc_datetime(&dt)),
)],
datemulti: vec![(Bound::Included(dt.date()), Bound::Unbounded)],
};

insert_into(all_the_ranges::table)
Expand Down
38 changes: 38 additions & 0 deletions diesel_tests/tests/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1498,6 +1498,44 @@ fn test_range_bound_enum_to_sql() {
));
}

#[cfg(feature = "postgres")]
#[test]
fn test_multirange_from_sql() {
use diesel::dsl::sql;
use std::collections::Bound;

let connection = &mut connection();

let query = "'{(,1), [5,8), [10,)}'::int4multirange";
let expected_value = vec![
(Bound::Unbounded, Bound::Excluded(1)),
(Bound::Included(5), Bound::Excluded(8)),
(Bound::Included(10), Bound::Unbounded),
];
assert_eq!(
expected_value,
query_single_value::<Multirange<Int4>, Vec<(Bound<i32>, Bound<i32>)>>(query)
);
}

#[cfg(feature = "postgres")]
#[test]
fn test_multirange_to_sql() {
use diesel::dsl::sql;
use std::collections::Bound;

let expected_value = "'{(,1), [5,8), [10,)}'::int4multirange";
let value = vec![
(Bound::Unbounded, Bound::Excluded(1)),
(Bound::Included(5), Bound::Excluded(8)),
(Bound::Included(10), Bound::Unbounded),
];
assert!(query_to_sql_equality::<
Multirange<Int4>,
Vec<(Bound<i32>, Bound<i32>)>,
>(expected_value, value));
}

#[cfg(feature = "postgres")]
#[test]
fn test_inserting_ranges() {
Expand Down
103 changes: 103 additions & 0 deletions diesel_tests/tests/types_roundtrip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,42 @@ mod pg_types {
(i64, u32, i64, u32),
mk_tstz_bounds
);
test_round_trip!(
int4multirange_roundtrips,
Multirange<Int4>,
Vec<(i32, i32)>,
mk_bound_list
);
test_round_trip!(
int8multirange_roundtrips,
Multirange<Int8>,
Vec<(i64, i64)>,
mk_bound_list
);
test_round_trip!(
datemultirange_roundtrips,
Multirange<Date>,
Vec<(u32, u32)>,
mk_date_bound_lists
);
test_round_trip!(
numrange_multirangetrips,
Multirange<Numeric>,
Vec<(i64, u64, i64, u64)>,
mk_num_bound_lists
);
test_round_trip!(
tsrange_multirangetrips,
Multirange<Timestamp>,
Vec<(i64, u32, i64, u32)>,
mk_ts_bound_lists
);
test_round_trip!(
tstzrange_multirangetrips,
Multirange<Timestamptz>,
(i64, u32, i64, u32), // if we use Vec here we receive a weird values which postgres don't accept
mk_tstz_bound_lists
);

test_round_trip!(json_roundtrips, Json, SerdeWrapper, mk_serde_json);
test_round_trip!(jsonb_roundtrips, Jsonb, SerdeWrapper, mk_serde_json);
Expand Down Expand Up @@ -333,6 +369,73 @@ mod pg_types {
mk_bounds((tstz1, tstz2))
}

fn is_sorted2<T>(data: &[(T, T)]) -> bool
where
T: Ord,
{
data.windows(2).all(|w| w[0].1 < w[1].0)
}

fn is_sorted4<T, U>(data: &[(T, U, T, U)]) -> bool
where
T: Ord,
{
data.windows(2).all(|w| w[0].2 < w[1].0)
}

fn mk_bound_list<T: Ord + PartialEq>(data: Vec<(T, T)>) -> Vec<(Bound<T>, Bound<T>)> {
let data: Vec<_> = data.into_iter().filter(|d| d.0 < d.1).collect();

if !is_sorted2(&data) {
// This is invalid but we don't have a way to say that to quickcheck
return vec![];
}

data.into_iter().map(mk_bounds).collect()
}

fn mk_date_bound_lists(data: Vec<(u32, u32)>) -> Vec<(Bound<NaiveDate>, Bound<NaiveDate>)> {
let data: Vec<_> = data.into_iter().filter(|d| d.0 < d.1).collect();

if !is_sorted2(&data) {
// This is invalid but we don't have a way to say that to quickcheck
return vec![];
}

data.into_iter().map(mk_date_bounds).collect()
}
fn mk_num_bound_lists(
data: Vec<(i64, u64, i64, u64)>,
) -> Vec<(Bound<BigDecimal>, Bound<BigDecimal>)> {
let data: Vec<_> = data.into_iter().filter(|d| d.0 < d.2).collect();

if !is_sorted4(&data) {
// This is invalid but we don't have a way to say that to quickcheck
return vec![];
}

data.into_iter().map(mk_num_bounds).collect()
}

fn mk_ts_bound_lists(
data: Vec<(i64, u32, i64, u32)>,
) -> Vec<(Bound<NaiveDateTime>, Bound<NaiveDateTime>)> {
let data: Vec<_> = data.into_iter().filter(|d| d.0 < d.2).collect();

if !is_sorted4(&data) {
// This is invalid but we don't have a way to say that to quickcheck
return vec![];
}

data.into_iter().map(mk_ts_bounds).collect()
}

fn mk_tstz_bound_lists(
data: (i64, u32, i64, u32),
) -> Vec<(Bound<DateTime<Utc>>, Bound<DateTime<Utc>>)> {
vec![mk_tstz_bounds(data)]
}

pub fn mk_pg_naive_datetime(data: (i64, u32)) -> NaiveDateTime {
// https://www.postgresql.org/docs/current/datatype-datetime.html
let earliest_pg_date = NaiveDate::from_ymd_opt(-4713, 1, 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ALTER TABLE all_the_ranges
DROP COLUMN int4multi,
DROP COLUMN int8multi,
DROP COLUMN nummulti,
DROP COLUMN tsmulti,
DROP COLUMN tstzmulti,
DROP COLUMN datemulti
7 changes: 7 additions & 0 deletions migrations/postgres/2024-08-10-143453_add_multiranges/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ALTER TABLE all_the_ranges
ADD COLUMN int4multi INT4MULTIRANGE NOT NULL,
ADD COLUMN int8multi INT8MULTIRANGE NOT NULL,
ADD COLUMN nummulti NUMMULTIRANGE NOT NULL,
ADD COLUMN tsmulti TSMULTIRANGE NOT NULL,
ADD COLUMN tstzmulti TSTZMULTIRANGE NOT NULL,
ADD COLUMN datemulti DATEMULTIRANGE NOT NULL

0 comments on commit 5eed8e3

Please sign in to comment.