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

fix: noop update from selectors should work #4092

Merged
merged 5 commits into from
Jul 31, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ use super::read::get_single_record;

use crate::column_metadata::{self, ColumnMetadata};
use crate::filter_conversion::AliasedCondition;
use crate::model_extensions::AsColumns;
use crate::query_builder::in_conditions;
use crate::query_builder::write::{build_update_and_set_query, chunk_update_with_ids};
use crate::row::ToSqlRow;
use crate::{Context, QueryExt, Queryable};

use connector_interface::*;
use itertools::Itertools;
use prisma_models::*;
use quaint::prelude::ConditionTree;
use std::usize;

/// Performs an update with an explicit selection set.
Expand All @@ -29,11 +26,13 @@ pub(crate) async fn update_one_with_selection(
// If there's nothing to update, just read the record.
// TODO(perf): Technically, if the selectors are fulfilling the field selection, there's no need to perform an additional read.
if args.args.is_empty() {
return get_single_record(conn, model, &record_filter.filter, &selected_fields, &[], ctx).await;
let filter = build_update_one_filter(record_filter);

return get_single_record(conn, model, &filter, &selected_fields, &[], ctx).await;
Comment on lines +29 to +31
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The actual fix. The filter generated for the read in the case of a noop update now takes into account the whole RecordFilter, thus rendering a filter for selectors too if any are present.

}

let update = build_update_and_set_query(model, args, Some(&selected_fields), ctx)
.so_that(build_update_one_filter(record_filter, ctx));
.so_that(build_update_one_filter(record_filter).aliased_condition_from(None, false, ctx));

let field_names: Vec<_> = selected_fields.db_names().collect();
let idents = selected_fields.type_identifiers_with_arities();
Expand Down Expand Up @@ -162,18 +161,9 @@ fn process_result_row(
///
/// Note: This function should only be called for update_one filters. It is not chunking the filters into multiple queries.
/// Note: Using this function to render an update_many filter could exceed the maximum query parameters available for a connector.
fn build_update_one_filter(record_filter: RecordFilter, ctx: &Context<'_>) -> ConditionTree<'static> {
let filter_condition = record_filter.filter.aliased_condition_from(None, false, ctx);

if let Some(selectors) = record_filter.selectors {
let columns = selectors
.iter()
.filter_map(|selection| selection.as_scalar_fields())
.flat_map(|f| f.as_columns(ctx))
.collect_vec();

filter_condition.and(in_conditions(&columns, selectors.iter()))
} else {
filter_condition
fn build_update_one_filter(record_filter: RecordFilter) -> Filter {
match record_filter.selectors {
Some(selectors) => Filter::and(vec![selectors.filter(), record_filter.filter]),
None => record_filter.filter,
}
}
16 changes: 14 additions & 2 deletions query-engine/core/src/query_ast/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ impl WriteQuery {
match self {
Self::CreateRecord(_) => returns_id,
Self::CreateManyRecords(_) => false,
Self::UpdateRecord(_) => returns_id,
Self::UpdateRecord(UpdateRecord::WithExplicitSelection(ur)) => {
ur.selected_fields.is_superset_of(field_selection)
}
Self::UpdateRecord(UpdateRecord::WithImplicitSelection(ur)) => {
ur.selected_fields().is_superset_of(field_selection)
}
Self::UpdateRecord(UpdateRecord::WithoutSelection(_)) => returns_id,
Comment on lines +67 to +73
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit out of scope for this PR.

This method, WriteQuery::returns, is used to compute reload nodes by checking whether a node's field selection fulfills its children data dependencies. It had not been updated after making UpdateRecord nodes return an arbitrary selection set. This means that some reload nodes might've not been added in cases it's needed.

Self::DeleteRecord(_) => returns_id,
Self::UpdateManyRecords(_) => returns_id,
Self::DeleteManyRecords(_) => false,
Expand Down Expand Up @@ -260,7 +266,7 @@ impl UpdateRecord {
pub(crate) fn selected_fields(&self) -> Option<FieldSelection> {
match self {
UpdateRecord::WithExplicitSelection(u) => Some(u.selected_fields.clone()),
UpdateRecord::WithImplicitSelection(u) => Some(u.model.primary_identifier()),
UpdateRecord::WithImplicitSelection(u) => Some(u.selected_fields()),
UpdateRecord::WithoutSelection(_) => None,
}
}
Expand Down Expand Up @@ -291,6 +297,12 @@ pub struct UpdateRecordWithoutSelection {
pub args: WriteArgs,
}

impl UpdateRecordWithoutSelection {
pub(crate) fn selected_fields(&self) -> FieldSelection {
self.model.primary_identifier()
}
}

#[derive(Debug, Clone)]
pub struct UpdateManyRecords {
pub model: Model,
Expand Down
Loading