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

Add support for the postgres multirange type #4159

Merged
merged 9 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ jobs:
if: runner.os == 'Windows' && matrix.backend == 'postgres'
shell: bash
run: |
choco install postgresql12 --force --params '/Password:root'
echo "C:\Program Files\PostgreSQL\12\bin" >> $GITHUB_PATH
echo "C:\Program Files\PostgreSQL\12\lib" >> $GITHUB_PATH
echo "PQ_LIB_DIR=C:\Program Files\PostgreSQL\12\lib" >> $GITHUB_ENV
choco install postgresql14 --force --params '/Password:root'
echo "C:\Program Files\PostgreSQL\14\bin" >> $GITHUB_PATH
echo "C:\Program Files\PostgreSQL\14\lib" >> $GITHUB_PATH
echo "PQ_LIB_DIR=C:\Program Files\PostgreSQL\14\lib" >> $GITHUB_ENV
echo "PG_DATABASE_URL=postgres://postgres:root@localhost/" >> $GITHUB_ENV
echo "PG_EXAMPLE_DATABASE_URL=postgres://postgres:root@localhost/diesel_example" >> $GITHUB_ENV

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Increasing the minimal supported Rust version will always be coupled at least wi
### Added

* Support for libsqlite3-sys 0.29.0
* Support for postgres multirange type

## [2.2.0] 2024-05-31

Expand Down
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<(Bound<T>, Bound<T>)>`][Vec] for any `T` which implements `ToSql<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
132 changes: 132 additions & 0 deletions diesel/src/pg/types/multirange.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
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_u32::<NetworkEndian>()?;

(0..len)
.map(|_| {
let range_size: usize = bytes.read_i32::<NetworkEndian>()?.try_into()?;
let (range_bytes, new_bytes) = bytes.split_at(range_size);
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().try_into()?)?;

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)?;
}
let buffer_len: i32 = buffer.len().try_into()?;
out.write_i32::<NetworkEndian>(buffer_len)?;
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)
}
}

impl<T, ST> ToSql<Nullable<Multirange<ST>>, Pg> for [(Bound<T>, Bound<T>)]
where
ST: 'static,
[(Bound<T>, Bound<T>)]: ToSql<ST, Pg>,
T: ToSql<ST, Pg>,
{
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
ToSql::<Multirange<ST>, Pg>::to_sql(self, out)
}
}

impl<T, ST> ToSql<Nullable<Multirange<ST>>, Pg> for Vec<(Bound<T>, Bound<T>)>
where
ST: 'static,
Vec<(Bound<T>, Bound<T>)>: ToSql<ST, Pg>,
T: ToSql<ST, Pg>,
{
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
ToSql::<Multirange<ST>, Pg>::to_sql(self, out)
}
}
2 changes: 1 addition & 1 deletion diesel/src/pg/types/ranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ where

if !flags.contains(RangeFlags::LB_INF) {
let elem_size = bytes.read_i32::<NetworkEndian>()?;
let (elem_bytes, new_bytes) = bytes.split_at(elem_size as usize);
let (elem_bytes, new_bytes) = bytes.split_at(elem_size.try_into()?);
bytes = new_bytes;
let value = T::from_sql(PgValue::new_internal(elem_bytes, &value))?;

Expand Down
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
18 changes: 17 additions & 1 deletion diesel_tests/tests/schema_inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ 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>)>,
#[allow(clippy::type_complexity)]
tstzmulti: Vec<(Bound<DateTime<Utc>>, Bound<DateTime<Utc>>)>,
datemulti: Vec<(Bound<NaiveDate>, Bound<NaiveDate>)>,
}

#[test]
Expand All @@ -225,13 +232,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
Loading
Loading