Skip to content

Commit

Permalink
implement missing cases
Browse files Browse the repository at this point in the history
  • Loading branch information
omid committed May 24, 2023
1 parent 258d64b commit d31630a
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 19 deletions.
1 change: 0 additions & 1 deletion diesel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ bitflags = { version = "2.0.0", optional = true }
r2d2 = { version = ">= 0.8.2, < 0.9.0", optional = true }
itoa = { version = "1.0.0", optional = true }
time = { version = "0.3.9", optional = true, features = ["macros"] }
paste = "1.0.12"

[dependencies.diesel_derives]
version = "~2.0.0"
Expand Down
15 changes: 13 additions & 2 deletions diesel/src/pg/query_builder/distinct_on.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,24 @@ pub struct DistinctOnClause<T>(pub(crate) T);
impl<T> ValidOrderingForDistinct<DistinctOnClause<T>> for NoOrderClause {}
impl<T> ValidOrderingForDistinct<DistinctOnClause<T>> for OrderClause<(T,)> {}
impl<T> ValidOrderingForDistinct<DistinctOnClause<T>> for OrderClause<T> where T: crate::Column {}

impl<T> ValidOrderingForDistinct<DistinctOnClause<T>> for OrderClause<crate::helper_types::Desc<T>>
where
T: crate::Column,
{}
impl<T> ValidOrderingForDistinct<DistinctOnClause<T>> for OrderClause<crate::helper_types::Asc<T>>
where
T: crate::Column,
{}
macro_rules! valid_ordering {
(@skip: ($ST1: ident, $($ST:ident,)*), $T1:ident, ) => {};
(@skip: ($ST1: ident, $($ST:ident,)*), $T1:ident, $($T:ident,)+) => {
valid_ordering!(($($ST,)*), ($ST1,), $($T,)*);
};
(($ST1: ident,), ($($OT:ident,)*), $T1:ident,) => {
#[allow(unused_parens)]
impl<$T1, $ST1, $($OT,)*> ValidOrderingForDistinct<DistinctOnClause<($ST1, $($OT,)*)>> for OrderClause<($T1)>
where $T1: crate::pg::OrderDecorator<Column = $ST1>,
{}
impl<$T1, $ST1, $($OT,)*> ValidOrderingForDistinct<DistinctOnClause<($ST1, $($OT,)*)>> for OrderClause<($T1,)>
where $T1: crate::pg::OrderDecorator<Column = $ST1>,
{}
Expand All @@ -44,7 +55,7 @@ macro_rules! valid_ordering {
{}
impl<$T1, $($T,)* $ST1, $($ST,)* $($OT,)*> ValidOrderingForDistinct<DistinctOnClause<($T1, $($T,)*)>> for OrderClause<($ST1, $($ST,)* $($OT,)*)>
where $ST1: crate::pg::OrderDecorator<Column = $T1>,
$($ST: crate::pg::OrderDecorator<Column = $T>,)*
$($ST: crate::pg::OrderDecorator<Column = $T>,)*
{}
valid_ordering!(($($ST,)*), ($($OT,)* $ST1,), $($T,)*);
};
Expand Down
83 changes: 67 additions & 16 deletions diesel_tests/tests/distinct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,52 @@ fn distinct_of_multiple_columns() {
.load::<Post>(&mut connection)
.unwrap();

// one order by
// one distinct on
let data = posts::table
.order(posts::body)
.distinct_on(posts::body)
.load(&mut connection);
let expected = vec![
(posts[0].clone()),
(posts[7].clone()),
];

assert_eq!(Ok(expected), data);

// multi order by
// one distinct on
let data = posts::table
.inner_join(users::table)
.order((users::id, posts::body, posts::title))
.distinct_on(users::id)
.load(&mut connection);
let expected = vec![
((posts[0].clone(), sean.clone())),
((posts[4].clone(), tess.clone())),
];

assert_eq!(Ok(expected), data);

// one order by
// multi distinct on
let data = posts::table
.inner_join(users::table)
.order(users::id)
.distinct_on((users::id, posts::body))
.load(&mut connection);
let expected = vec![
((posts[0].clone(), sean.clone())),
((posts[1].clone(), sean.clone())),
((posts[4].clone(), tess.clone())),
((posts[7].clone(), tess.clone())),
];

assert_eq!(Ok(expected), data);

// multi order by
// multi distinct on
// order by > distinct on
let data = posts::table
.inner_join(users::table)
.order((users::id, posts::body, posts::title))
Expand All @@ -181,20 +227,25 @@ fn distinct_of_multiple_columns() {
];

assert_eq!(Ok(expected), data);
}

// #[cfg(feature = "postgres")]
// #[test]
// #[should_panic(expected = "TBD")]
// fn invalid_distinct_on_or_order_detected() {
// use crate::schema::users;
// use crate::schema::posts;
//
// let mut connection = connection();
//
// posts::table
// .inner_join(users::table)
// .order((users::id, posts::title))
// .distinct_on((users::id, posts::body))
// .load(&mut connection);
// }
// multi order by
// multi distinct on
// order by < distinct on
let data = posts::table
.inner_join(users::table)
.order((users::id, posts::body))
.distinct_on((users::id, posts::body, posts::title))
.load(&mut connection);
let expected = vec![
((posts[0].clone(), sean.clone())),
((posts[2].clone(), sean.clone())),
((posts[1].clone(), sean.clone())),
((posts[3].clone(), sean.clone())),
((posts[4].clone(), tess.clone())),
((posts[6].clone(), tess.clone())),
((posts[5].clone(), tess.clone())),
((posts[7].clone(), tess.clone())),
];

assert_eq!(Ok(expected), data);
}

0 comments on commit d31630a

Please sign in to comment.