From 630731de5bfbc7782a6d46be0049bbc40793dde5 Mon Sep 17 00:00:00 2001 From: Omid Rad Date: Thu, 25 May 2023 09:24:19 +0200 Subject: [PATCH] add more tests + cs --- diesel/src/pg/query_builder/distinct_on.rs | 16 +- ...tinct_on_requires_matching_order_clause.rs | 27 +++ ...t_on_requires_matching_order_clause.stderr | 190 +++++++++--------- diesel_tests/tests/distinct.rs | 26 ++- 4 files changed, 150 insertions(+), 109 deletions(-) diff --git a/diesel/src/pg/query_builder/distinct_on.rs b/diesel/src/pg/query_builder/distinct_on.rs index 0ac2856ce223..0177c8652cb3 100644 --- a/diesel/src/pg/query_builder/distinct_on.rs +++ b/diesel/src/pg/query_builder/distinct_on.rs @@ -18,14 +18,14 @@ pub struct DistinctOnClause(pub(crate) T); impl ValidOrderingForDistinct> for NoOrderClause {} impl ValidOrderingForDistinct> for OrderClause<(T,)> {} impl ValidOrderingForDistinct> for OrderClause where T: crate::Column {} -impl ValidOrderingForDistinct> for OrderClause> - where - T: crate::Column, -{} -impl ValidOrderingForDistinct> for OrderClause> - where - T: crate::Column, -{} +impl ValidOrderingForDistinct> for OrderClause> where + T: crate::Column +{ +} +impl ValidOrderingForDistinct> for OrderClause> where + T: crate::Column +{ +} macro_rules! valid_ordering { (@skip: ($ST1: ident, $($ST:ident,)*), $T1:ident, ) => {}; (@skip: ($ST1: ident, $($ST:ident,)*), $T1:ident, $($T:ident,)+) => { diff --git a/diesel_compile_tests/tests/fail/distinct_on_requires_matching_order_clause.rs b/diesel_compile_tests/tests/fail/distinct_on_requires_matching_order_clause.rs index a1374f4e4206..b6fbbbb30cb6 100644 --- a/diesel_compile_tests/tests/fail/distinct_on_requires_matching_order_clause.rs +++ b/diesel_compile_tests/tests/fail/distinct_on_requires_matching_order_clause.rs @@ -42,6 +42,33 @@ fn main() { .then_order_by(users::id) .distinct_on(users::name); + // same as above, with asc/desc + let _ = users::table + .order_by(users::name.asc()) + .distinct_on(users::name) + .into_boxed(); + let _ = users::table + .order_by((users::name.asc(), users::id.desc())) + .distinct_on(users::name) + .into_boxed(); + let _ = users::table + .order_by((users::name.asc(), users::id.desc())) + .distinct_on((users::name, users::id)) + .into_boxed(); + let _ = users::table + .order_by((users::name.asc(), users::id.desc())) + .distinct_on((users::name, users::id, users::hair_color)) + .into_boxed(); + let _ = users::table + .order_by((users::name.asc(), users::id.desc(), users::hair_color)) + .distinct_on((users::name, users::id)) + .into_boxed(); + let _ = users::table + .order_by(users::name.asc()) + .then_order_by(users::id) + .distinct_on(users::name) + .into_boxed(); + // verify that this all works with boxed queries let _ = users::table.distinct_on(users::name).into_boxed(); let _ = users::table diff --git a/diesel_compile_tests/tests/fail/distinct_on_requires_matching_order_clause.stderr b/diesel_compile_tests/tests/fail/distinct_on_requires_matching_order_clause.stderr index d79dbf2a3756..780196688dbc 100644 --- a/diesel_compile_tests/tests/fail/distinct_on_requires_matching_order_clause.stderr +++ b/diesel_compile_tests/tests/fail/distinct_on_requires_matching_order_clause.stderr @@ -1,109 +1,109 @@ error[E0277]: the trait bound `diesel::query_builder::order_clause::OrderClause: query_dsl::order_dsl::ValidOrderingForDistinct>` is not satisfied - --> tests/fail/distinct_on_requires_matching_order_clause.rs:76:58 - | -76 | let _ = users::table.order_by(users::id).distinct_on(users::name); - | ----------- ^^^^^^^^^^^ the trait `query_dsl::order_dsl::ValidOrderingForDistinct>` is not implemented for `diesel::query_builder::order_clause::OrderClause` - | | - | required by a bound introduced by this call - | - = help: the following other types implement trait `query_dsl::order_dsl::ValidOrderingForDistinct`: - as query_dsl::order_dsl::ValidOrderingForDistinct>> - as query_dsl::order_dsl::ValidOrderingForDistinct>> - as query_dsl::order_dsl::ValidOrderingForDistinct>> - as query_dsl::order_dsl::ValidOrderingForDistinct>> - as query_dsl::order_dsl::ValidOrderingForDistinct>> - as query_dsl::order_dsl::ValidOrderingForDistinct>> - as query_dsl::order_dsl::ValidOrderingForDistinct>> - as query_dsl::order_dsl::ValidOrderingForDistinct>> - and $N others - = note: required for `SelectStatement, DefaultSelectClause>, NoDistinctClause, ..., ...>` to implement `DistinctOnDsl` + --> tests/fail/distinct_on_requires_matching_order_clause.rs:103:58 + | +103 | let _ = users::table.order_by(users::id).distinct_on(users::name); + | ----------- ^^^^^^^^^^^ the trait `query_dsl::order_dsl::ValidOrderingForDistinct>` is not implemented for `diesel::query_builder::order_clause::OrderClause` + | | + | required by a bound introduced by this call + | + = help: the following other types implement trait `query_dsl::order_dsl::ValidOrderingForDistinct`: + as query_dsl::order_dsl::ValidOrderingForDistinct>> + as query_dsl::order_dsl::ValidOrderingForDistinct>> + as query_dsl::order_dsl::ValidOrderingForDistinct>> + as query_dsl::order_dsl::ValidOrderingForDistinct>> + as query_dsl::order_dsl::ValidOrderingForDistinct>> + as query_dsl::order_dsl::ValidOrderingForDistinct>> + as query_dsl::order_dsl::ValidOrderingForDistinct>> + as query_dsl::order_dsl::ValidOrderingForDistinct>> + and $N others + = note: required for `SelectStatement, DefaultSelectClause>, NoDistinctClause, ..., ...>` to implement `DistinctOnDsl` note: required by a bound in `diesel::QueryDsl::distinct_on` - --> $DIESEL/src/query_dsl/mod.rs - | - | Self: methods::DistinctOnDsl, - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `QueryDsl::distinct_on` + --> $DIESEL/src/query_dsl/mod.rs + | + | Self: methods::DistinctOnDsl, + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `QueryDsl::distinct_on` error[E0271]: type mismatch resolving `::Column == name` - --> tests/fail/distinct_on_requires_matching_order_clause.rs:80:61 - | -80 | let _ = users::table.order_by((users::id, users::name)).distinct_on(users::name); - | ^^^^^^^^^^^ expected struct `id`, found struct `name` - | - = note: required for `diesel::query_builder::order_clause::OrderClause<(columns::id, columns::name)>` to implement `query_dsl::order_dsl::ValidOrderingForDistinct>` - = note: required for `SelectStatement, DefaultSelectClause>, NoDistinctClause, ..., ...>` to implement `DistinctOnDsl` + --> tests/fail/distinct_on_requires_matching_order_clause.rs:107:61 + | +107 | let _ = users::table.order_by((users::id, users::name)).distinct_on(users::name); + | ^^^^^^^^^^^ expected struct `id`, found struct `name` + | + = note: required for `diesel::query_builder::order_clause::OrderClause<(columns::id, columns::name)>` to implement `query_dsl::order_dsl::ValidOrderingForDistinct>` + = note: required for `SelectStatement, DefaultSelectClause>, NoDistinctClause, ..., ...>` to implement `DistinctOnDsl` error[E0271]: type mismatch resolving `::Column == name` - --> tests/fail/distinct_on_requires_matching_order_clause.rs:86:10 - | -86 | .distinct_on(users::name); - | ^^^^^^^^^^^ expected struct `id`, found struct `name` - | - = note: required for `diesel::query_builder::order_clause::OrderClause<(columns::id, columns::name)>` to implement `query_dsl::order_dsl::ValidOrderingForDistinct>` - = note: required for `SelectStatement, DefaultSelectClause>, NoDistinctClause, ..., ...>` to implement `DistinctOnDsl` + --> tests/fail/distinct_on_requires_matching_order_clause.rs:113:10 + | +113 | .distinct_on(users::name); + | ^^^^^^^^^^^ expected struct `id`, found struct `name` + | + = note: required for `diesel::query_builder::order_clause::OrderClause<(columns::id, columns::name)>` to implement `query_dsl::order_dsl::ValidOrderingForDistinct>` + = note: required for `SelectStatement, DefaultSelectClause>, NoDistinctClause, ..., ...>` to implement `DistinctOnDsl` error[E0277]: the trait bound `diesel::query_builder::order_clause::OrderClause: query_dsl::order_dsl::ValidOrderingForDistinct>` is not satisfied - --> tests/fail/distinct_on_requires_matching_order_clause.rs:90:60 - | -90 | let _ = users::table.distinct_on(users::name).order_by(users::id); - | -------- ^^^^^^^^^ the trait `query_dsl::order_dsl::ValidOrderingForDistinct>` is not implemented for `diesel::query_builder::order_clause::OrderClause` - | | - | required by a bound introduced by this call - | - = help: the following other types implement trait `query_dsl::order_dsl::ValidOrderingForDistinct`: - as query_dsl::order_dsl::ValidOrderingForDistinct>> - as query_dsl::order_dsl::ValidOrderingForDistinct>> - as query_dsl::order_dsl::ValidOrderingForDistinct>> - as query_dsl::order_dsl::ValidOrderingForDistinct>> - as query_dsl::order_dsl::ValidOrderingForDistinct>> - as query_dsl::order_dsl::ValidOrderingForDistinct>> - as query_dsl::order_dsl::ValidOrderingForDistinct>> - as query_dsl::order_dsl::ValidOrderingForDistinct>> - and $N others - = note: required for `SelectStatement, DefaultSelectClause>, DistinctOnClause>` to implement `OrderDsl` + --> tests/fail/distinct_on_requires_matching_order_clause.rs:117:60 + | +117 | let _ = users::table.distinct_on(users::name).order_by(users::id); + | -------- ^^^^^^^^^ the trait `query_dsl::order_dsl::ValidOrderingForDistinct>` is not implemented for `diesel::query_builder::order_clause::OrderClause` + | | + | required by a bound introduced by this call + | + = help: the following other types implement trait `query_dsl::order_dsl::ValidOrderingForDistinct`: + as query_dsl::order_dsl::ValidOrderingForDistinct>> + as query_dsl::order_dsl::ValidOrderingForDistinct>> + as query_dsl::order_dsl::ValidOrderingForDistinct>> + as query_dsl::order_dsl::ValidOrderingForDistinct>> + as query_dsl::order_dsl::ValidOrderingForDistinct>> + as query_dsl::order_dsl::ValidOrderingForDistinct>> + as query_dsl::order_dsl::ValidOrderingForDistinct>> + as query_dsl::order_dsl::ValidOrderingForDistinct>> + and $N others + = note: required for `SelectStatement, DefaultSelectClause>, DistinctOnClause>` to implement `OrderDsl` note: required by a bound in `order_by` - --> $DIESEL/src/query_dsl/mod.rs - | - | Self: methods::OrderDsl, - | ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `QueryDsl::order_by` + --> $DIESEL/src/query_dsl/mod.rs + | + | Self: methods::OrderDsl, + | ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `QueryDsl::order_by` error[E0277]: the trait bound `diesel::query_builder::order_clause::OrderClause: query_dsl::order_dsl::ValidOrderingForDistinct>` is not satisfied - --> tests/fail/distinct_on_requires_matching_order_clause.rs:93:58 - | -93 | let _ = users::table.order_by(users::id).distinct_on(users::name).into_boxed(); - | ----------- ^^^^^^^^^^^ the trait `query_dsl::order_dsl::ValidOrderingForDistinct>` is not implemented for `diesel::query_builder::order_clause::OrderClause` - | | - | required by a bound introduced by this call - | - = help: the following other types implement trait `query_dsl::order_dsl::ValidOrderingForDistinct`: - as query_dsl::order_dsl::ValidOrderingForDistinct>> - as query_dsl::order_dsl::ValidOrderingForDistinct>> - as query_dsl::order_dsl::ValidOrderingForDistinct>> - as query_dsl::order_dsl::ValidOrderingForDistinct>> - as query_dsl::order_dsl::ValidOrderingForDistinct>> - as query_dsl::order_dsl::ValidOrderingForDistinct>> - as query_dsl::order_dsl::ValidOrderingForDistinct>> - as query_dsl::order_dsl::ValidOrderingForDistinct>> - and $N others - = note: required for `SelectStatement, DefaultSelectClause>, NoDistinctClause, ..., ...>` to implement `DistinctOnDsl` + --> tests/fail/distinct_on_requires_matching_order_clause.rs:120:58 + | +120 | let _ = users::table.order_by(users::id).distinct_on(users::name).into_boxed(); + | ----------- ^^^^^^^^^^^ the trait `query_dsl::order_dsl::ValidOrderingForDistinct>` is not implemented for `diesel::query_builder::order_clause::OrderClause` + | | + | required by a bound introduced by this call + | + = help: the following other types implement trait `query_dsl::order_dsl::ValidOrderingForDistinct`: + as query_dsl::order_dsl::ValidOrderingForDistinct>> + as query_dsl::order_dsl::ValidOrderingForDistinct>> + as query_dsl::order_dsl::ValidOrderingForDistinct>> + as query_dsl::order_dsl::ValidOrderingForDistinct>> + as query_dsl::order_dsl::ValidOrderingForDistinct>> + as query_dsl::order_dsl::ValidOrderingForDistinct>> + as query_dsl::order_dsl::ValidOrderingForDistinct>> + as query_dsl::order_dsl::ValidOrderingForDistinct>> + and $N others + = note: required for `SelectStatement, DefaultSelectClause>, NoDistinctClause, ..., ...>` to implement `DistinctOnDsl` note: required by a bound in `diesel::QueryDsl::distinct_on` - --> $DIESEL/src/query_dsl/mod.rs - | - | Self: methods::DistinctOnDsl, - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `QueryDsl::distinct_on` + --> $DIESEL/src/query_dsl/mod.rs + | + | Self: methods::DistinctOnDsl, + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `QueryDsl::distinct_on` error[E0271]: type mismatch resolving `::Column == name` - --> tests/fail/distinct_on_requires_matching_order_clause.rs:99:10 - | -99 | .distinct_on(users::name) - | ^^^^^^^^^^^ expected struct `id`, found struct `name` - | - = note: required for `diesel::query_builder::order_clause::OrderClause<(columns::id, columns::name)>` to implement `query_dsl::order_dsl::ValidOrderingForDistinct>` - = note: required for `SelectStatement, DefaultSelectClause>, NoDistinctClause, ..., ...>` to implement `DistinctOnDsl` + --> tests/fail/distinct_on_requires_matching_order_clause.rs:126:10 + | +126 | .distinct_on(users::name) + | ^^^^^^^^^^^ expected struct `id`, found struct `name` + | + = note: required for `diesel::query_builder::order_clause::OrderClause<(columns::id, columns::name)>` to implement `query_dsl::order_dsl::ValidOrderingForDistinct>` + = note: required for `SelectStatement, DefaultSelectClause>, NoDistinctClause, ..., ...>` to implement `DistinctOnDsl` error[E0277]: the trait bound `diesel::query_builder::order_clause::OrderClause<(columns::id, columns::name)>: query_dsl::order_dsl::ValidOrderingForDistinct>` is not satisfied - --> tests/fail/distinct_on_requires_matching_order_clause.rs:106:22 + --> tests/fail/distinct_on_requires_matching_order_clause.rs:133:22 | -106 | .distinct_on((users::name, users::id)) +133 | .distinct_on((users::name, users::id)) | ----------- ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `query_dsl::order_dsl::ValidOrderingForDistinct>` is not implemented for `diesel::query_builder::order_clause::OrderClause<(columns::id, columns::name)>` | | | required by a bound introduced by this call @@ -126,27 +126,27 @@ note: required by a bound in `diesel::QueryDsl::distinct_on` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `QueryDsl::distinct_on` error[E0271]: type mismatch resolving `::Column == name` - --> tests/fail/distinct_on_requires_matching_order_clause.rs:113:10 + --> tests/fail/distinct_on_requires_matching_order_clause.rs:140:10 | -113 | .distinct_on((users::name, users::id)) +140 | .distinct_on((users::name, users::id)) | ^^^^^^^^^^^ expected struct `id`, found struct `name` | = note: required for `diesel::query_builder::order_clause::OrderClause` to implement `query_dsl::order_dsl::ValidOrderingForDistinct>` = note: required for `SelectStatement, DefaultSelectClause>, NoDistinctClause, ..., ...>` to implement `DistinctOnDsl<(columns::name, columns::id)>` error[E0271]: type mismatch resolving `::Column == name` - --> tests/fail/distinct_on_requires_matching_order_clause.rs:120:10 + --> tests/fail/distinct_on_requires_matching_order_clause.rs:147:10 | -120 | .distinct_on(users::name) +147 | .distinct_on(users::name) | ^^^^^^^^^^^ expected struct `id`, found struct `name` | = note: required for `diesel::query_builder::order_clause::OrderClause<(columns::id, columns::name)>` to implement `query_dsl::order_dsl::ValidOrderingForDistinct>` = note: required for `SelectStatement, DefaultSelectClause>, NoDistinctClause, ..., ...>` to implement `DistinctOnDsl` error[E0277]: the trait bound `diesel::query_builder::order_clause::OrderClause: query_dsl::order_dsl::ValidOrderingForDistinct>` is not satisfied - --> tests/fail/distinct_on_requires_matching_order_clause.rs:127:19 + --> tests/fail/distinct_on_requires_matching_order_clause.rs:154:19 | -127 | .order_by(users::id) +154 | .order_by(users::id) | -------- ^^^^^^^^^ the trait `query_dsl::order_dsl::ValidOrderingForDistinct>` is not implemented for `diesel::query_builder::order_clause::OrderClause` | | | required by a bound introduced by this call diff --git a/diesel_tests/tests/distinct.rs b/diesel_tests/tests/distinct.rs index 3682f5f34dfb..a400b241f44c 100644 --- a/diesel_tests/tests/distinct.rs +++ b/diesel_tests/tests/distinct.rs @@ -174,10 +174,7 @@ fn distinct_of_multiple_columns() { .order(posts::body) .distinct_on(posts::body) .load(&mut connection); - let expected = vec![ - (posts[0].clone()), - (posts[7].clone()), - ]; + let expected = vec![(posts[0].clone()), (posts[7].clone())]; assert_eq!(Ok(expected), data); @@ -240,11 +237,28 @@ fn distinct_of_multiple_columns() { (posts[0].clone(), sean.clone()), (posts[2].clone(), sean.clone()), (posts[1].clone(), sean.clone()), - (posts[3].clone(), sean), + (posts[3].clone(), sean.clone()), (posts[4].clone(), tess.clone()), (posts[6].clone(), tess.clone()), (posts[5].clone(), tess.clone()), - (posts[7].clone(), tess), + (posts[7].clone(), tess.clone()), + ]; + + assert_eq!(Ok(expected), data); + + // multi order by + // multi distinct on + // including asc and desc + let data = posts::table + .inner_join(users::table) + .order((users::id.asc(), posts::body.desc(), posts::title)) + .distinct_on((users::id, posts::body)) + .load(&mut connection); + let expected = vec![ + (posts[1].clone(), sean.clone()), + (posts[0].clone(), sean), + (posts[5].clone(), tess.clone()), + (posts[4].clone(), tess), ]; assert_eq!(Ok(expected), data);