Skip to content

Commit

Permalink
Add DistinctOn preview feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Druue committed Dec 1, 2023
1 parent 66f919a commit 439eeb0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
2 changes: 2 additions & 0 deletions psl/psl-core/src/common/preview_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ features!(
ConnectOrCreate,
CreateMany,
DataProxy,
DistinctOn,
Deno,
Distinct,
DriverAdapters,
Expand Down Expand Up @@ -82,6 +83,7 @@ features!(
pub const ALL_PREVIEW_FEATURES: FeatureMap = FeatureMap {
active: enumflags2::make_bitflags!(PreviewFeature::{
Deno
| DistinctOn
| DriverAdapters
| FullTextIndex
| FullTextSearch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mod distinct {
}

/// Regression test for not selecting the fields the distinct is performed on: https://github.com/prisma/prisma/issues/5969
#[connector_test(exclude(CockroachDb, MongoDb, SqlServer, MySQL, Sqlite))]
#[connector_test(only(Postgres))]
async fn no_panic_pg(runner: Runner) -> TestResult<()> {
test_user(&runner, r#"{ id: 1, first_name: "Joe", last_name: "Doe", email: "1" }"#).await?;
test_user(&runner, r#"{ id: 2, first_name: "Doe", last_name: "Joe", email: "2" }"#).await?;
Expand All @@ -55,7 +55,7 @@ mod distinct {

/// Regression test for not selecting the fields the distinct is performed on: https://github.com/prisma/prisma/issues/5969
#[connector_test(exclude(Postgres))]
async fn no_panic_mem(runner: Runner) -> TestResult<()> {
async fn no_panic_other(runner: Runner) -> TestResult<()> {
test_user(&runner, r#"{ id: 1, first_name: "Joe", last_name: "Doe", email: "1" }"#).await?;
test_user(&runner, r#"{ id: 2, first_name: "Doe", last_name: "Joe", email: "2" }"#).await?;

Expand Down Expand Up @@ -92,7 +92,7 @@ mod distinct {
Ok(())
}

#[connector_test(exclude(Postgres))]
#[connector_test(only(Postgres))]
async fn with_duplicates_pg(runner: Runner) -> TestResult<()> {
test_user(&runner, r#"{ id: 1, first_name: "Joe", last_name: "Doe", email: "1" }"#).await?;
test_user(
Expand All @@ -109,14 +109,14 @@ mod distinct {
{ id, first_name, last_name }
}")
),
@r###"{"data":{"findManyUser":[{"id":1,"first_name":"Joe","last_name":"Doe"},{"id":2,"first_name":"Hans","last_name":"Wurst"}]}}"###
@r###"{"data":{"findManyUser":[{"id":2,"first_name":"Hans","last_name":"Wurst"},{"id":1,"first_name":"Joe","last_name":"Doe"}]}}"###
);

Ok(())
}

#[connector_test(exclude(CockroachDb, MongoDb, SqlServer, MySQL, Sqlite))]
async fn with_duplicates_mem(runner: Runner) -> TestResult<()> {
#[connector_test(exclude(Postgres))]
async fn with_duplicates_other(runner: Runner) -> TestResult<()> {
test_user(&runner, r#"{ id: 1, first_name: "Joe", last_name: "Doe", email: "1" }"#).await?;
test_user(
&runner,
Expand All @@ -132,7 +132,7 @@ mod distinct {
{ id, first_name, last_name }
}")
),
@r###"{"data":{"findManyUser":[{"id":2,"first_name":"Hans","last_name":"Wurst"},{"id":1,"first_name":"Joe","last_name":"Doe"}]}}"###
@r###"{"data":{"findManyUser":[{"id":1,"first_name":"Joe","last_name":"Doe"},{"id":2,"first_name":"Hans","last_name":"Wurst"}]}}"###
);

Ok(())
Expand Down Expand Up @@ -215,7 +215,7 @@ mod distinct {
}

/// Mut return only distinct records for top record, and only for those the distinct relation records.
#[connector_test(exclude(CockroachDb, MongoDb, SqlServer, MySQL, Sqlite))]
#[connector_test(only(Postgres))]
async fn nested_distinct_pg(runner: Runner) -> TestResult<()> {
nested_dataset(&runner).await?;

Expand All @@ -242,7 +242,7 @@ mod distinct {
}

#[connector_test(exclude(Postgres))]
async fn nested_distinct_mem(runner: Runner) -> TestResult<()> {
async fn nested_distinct_other(runner: Runner) -> TestResult<()> {
nested_dataset(&runner).await?;

// Returns Users 1, 3, 4, 5 top
Expand Down
14 changes: 12 additions & 2 deletions query-engine/query-structure/src/query_arguments.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use psl::datamodel_connector::ConnectorCapability;
use psl::{datamodel_connector::ConnectorCapability, PreviewFeature};

use crate::*;

Expand Down Expand Up @@ -76,7 +76,17 @@ impl QueryArguments {
}

pub fn requires_inmemory_distinct(&self) -> bool {
self.has_distinct() && (!self.has_distinct_capability() || self.has_orderby())
self.has_distinct()
&& ((!self.has_distinct_capability() && !self.has_distincton_preview()) || self.has_orderby())
}

fn has_distincton_preview(&self) -> bool {
self.model()
.dm
.schema
.configuration
.preview_features()
.contains(PreviewFeature::DistinctOn)
}

fn has_distinct_capability(&self) -> bool {
Expand Down

0 comments on commit 439eeb0

Please sign in to comment.