Skip to content

Commit

Permalink
enable all distinct queries with joins
Browse files Browse the repository at this point in the history
  • Loading branch information
aqrln committed Feb 16, 2024
1 parent 7531d85 commit dc985b0
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 48 deletions.
21 changes: 0 additions & 21 deletions query-engine/core/src/query_ast/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,6 @@ impl ReadQuery {
ReadQuery::AggregateRecordsQuery(_) => false,
}
}

pub(crate) fn requires_inmemory_distinct_with_joins(&self) -> bool {
match self {
ReadQuery::RecordQuery(_) => false,
ReadQuery::ManyRecordsQuery(q) => q.requires_inmemory_distinct_with_joins(),
ReadQuery::RelatedRecordsQuery(q) => q.requires_inmemory_distinct_with_joins(),
ReadQuery::AggregateRecordsQuery(_) => false,
}
}
}

impl FilteredQuery for ReadQuery {
Expand Down Expand Up @@ -207,13 +198,6 @@ pub struct ManyRecordsQuery {
pub relation_load_strategy: RelationLoadStrategy,
}

impl ManyRecordsQuery {
pub fn requires_inmemory_distinct_with_joins(&self) -> bool {
self.args.requires_inmemory_distinct_with_joins()
|| self.nested.iter().any(|q| q.requires_inmemory_distinct_with_joins())
}
}

#[derive(Debug, Clone)]
pub struct RelatedRecordsQuery {
pub name: String,
Expand All @@ -233,11 +217,6 @@ impl RelatedRecordsQuery {
pub fn has_cursor(&self) -> bool {
self.args.cursor.is_some() || self.nested.iter().any(|q| q.has_cursor())
}

pub fn requires_inmemory_distinct_with_joins(&self) -> bool {
self.args.requires_inmemory_distinct_with_joins()
|| self.nested.iter().any(|q| q.requires_inmemory_distinct_with_joins())
}
}

#[derive(Debug, Clone)]
Expand Down
10 changes: 2 additions & 8 deletions query-engine/core/src/query_graph_builder/read/many.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,8 @@ fn find_many_with_options(
let selected_fields = utils::merge_relation_selections(selected_fields, None, &nested);
let selected_fields = utils::merge_cursor_fields(selected_fields, &args.cursor);

let relation_load_strategy = get_relation_load_strategy(
args.relation_load_strategy,
args.cursor.as_ref(),
args.distinct.as_ref(),
&args.order_by,
&nested,
query_schema,
)?;
let relation_load_strategy =
get_relation_load_strategy(args.relation_load_strategy, args.cursor.as_ref(), &nested, query_schema)?;

Ok(ReadQuery::ManyRecordsQuery(ManyRecordsQuery {
name,
Expand Down
3 changes: 1 addition & 2 deletions query-engine/core/src/query_graph_builder/read/one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ fn find_unique_with_options(
let nested = utils::collect_nested_queries(nested_fields, &model, query_schema)?;
let selected_fields = utils::merge_relation_selections(selected_fields, None, &nested);

let relation_load_strategy =
get_relation_load_strategy(requested_rel_load_strategy, None, None, &[], &nested, query_schema)?;
let relation_load_strategy = get_relation_load_strategy(requested_rel_load_strategy, None, &nested, query_schema)?;

Ok(ReadQuery::RecordQuery(RecordQuery {
name,
Expand Down
22 changes: 5 additions & 17 deletions query-engine/core/src/query_graph_builder/read/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;
use crate::{ArgumentListLookup, FieldPair, ParsedField, ReadQuery};
use psl::datamodel_connector::{ConnectorCapability, JoinStrategySupport};
use query_structure::{native_distinct_compatible_with_order_by, prelude::*, RelationLoadStrategy};
use psl::datamodel_connector::JoinStrategySupport;
use query_structure::{prelude::*, RelationLoadStrategy};
use schema::{
constants::{aggregations::*, args},
QuerySchema,
Expand Down Expand Up @@ -254,16 +254,14 @@ pub fn merge_cursor_fields(selected_fields: FieldSelection, cursor: &Option<Sele
pub(crate) fn get_relation_load_strategy(
requested_strategy: Option<RelationLoadStrategy>,
cursor: Option<&SelectionResult>,
distinct: Option<&FieldSelection>,
order_by: &[OrderBy],
nested_queries: &[ReadQuery],
query_schema: &QuerySchema,
) -> QueryGraphBuilderResult<RelationLoadStrategy> {
match query_schema.join_strategy_support() {
// Connector and database version supports the `Join` strategy...
JoinStrategySupport::Yes => match requested_strategy {
// But incoming query cannot be resolved with joins.
_ if !query_can_be_resolved_with_joins(query_schema, cursor, distinct, order_by, nested_queries) => {
_ if !query_can_be_resolved_with_joins(cursor, nested_queries) => {
// So we fallback to the `Query` one.
Ok(RelationLoadStrategy::Query)
}
Expand All @@ -289,20 +287,10 @@ pub(crate) fn get_relation_load_strategy(
}
}

fn query_can_be_resolved_with_joins(
query_schema: &QuerySchema,
cursor: Option<&SelectionResult>,
distinct: Option<&FieldSelection>,
order_by: &[OrderBy],
nested_queries: &[ReadQuery],
) -> bool {
let can_distinct_in_db_with_joins = query_schema.has_capability(ConnectorCapability::DistinctOn)
&& native_distinct_compatible_with_order_by(distinct, order_by);

fn query_can_be_resolved_with_joins(cursor: Option<&SelectionResult>, nested_queries: &[ReadQuery]) -> bool {
cursor.is_none()
&& (distinct.is_none() || can_distinct_in_db_with_joins)
&& !nested_queries.iter().any(|q| match q {
ReadQuery::RelatedRecordsQuery(q) => q.has_cursor() || q.requires_inmemory_distinct_with_joins(),
ReadQuery::RelatedRecordsQuery(q) => q.has_cursor(),
_ => false,
})
}

0 comments on commit dc985b0

Please sign in to comment.