From 13935602f1d2a6ae703554947f01182298998d2f Mon Sep 17 00:00:00 2001 From: Larko <59736843+Larkooo@users.noreply.github.com> Date: Thu, 12 Dec 2024 23:02:17 +0700 Subject: [PATCH] fix(torii-grpc): address sql precedence in composite (#2800) * fix(torii-grpc): address sql precedence in composite * add distincts * fmt --- crates/torii/grpc/src/server/mod.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/crates/torii/grpc/src/server/mod.rs b/crates/torii/grpc/src/server/mod.rs index 113832b1e8..020f209614 100644 --- a/crates/torii/grpc/src/server/mod.rs +++ b/crates/torii/grpc/src/server/mod.rs @@ -768,7 +768,7 @@ impl DojoWorld { format!( r#" SELECT COUNT(*) FROM ( - SELECT [{table}].id + SELECT DISTINCT [{table}].id FROM [{table}] JOIN {model_relation_table} ON [{table}].id = {model_relation_table}.entity_id {join_clause} @@ -802,7 +802,7 @@ impl DojoWorld { let query = format!( r#" - SELECT [{table}].id, group_concat({model_relation_table}.model_id) as model_ids + SELECT DISTINCT [{table}].id, group_concat({model_relation_table}.model_id) as model_ids FROM [{table}] JOIN {model_relation_table} ON [{table}].id = {model_relation_table}.entity_id {join_clause} @@ -1193,12 +1193,12 @@ fn build_composite_clause( }) .collect::>() .join(", "); - where_clauses.push(format!("{table}.id IN ({})", ids)); + where_clauses.push(format!("({table}.id IN ({}))", ids)); } ClauseType::Keys(keys) => { let keys_pattern = build_keys_pattern(keys)?; bind_values.push(keys_pattern); - where_clauses.push(format!("{table}.keys REGEXP ?")); + where_clauses.push(format!("({table}.keys REGEXP ?)")); // Add model checks for specified models for model in &keys.models { @@ -1254,7 +1254,7 @@ fn build_composite_clause( // Use the column name directly since it's already flattened where_clauses - .push(format!("[{alias}].[{}] {comparison_operator} ?", member.member)); + .push(format!("([{alias}].[{}] {comparison_operator} ?)", member.member)); } ClauseType::Composite(nested) => { // Handle nested composite by recursively building the clause @@ -1265,7 +1265,8 @@ fn build_composite_clause( where_clauses.push(format!("({})", nested_where.trim_start_matches("WHERE "))); } if !nested_having.is_empty() { - having_clauses.push(nested_having.trim_start_matches("HAVING ").to_string()); + having_clauses + .push(format!("({})", nested_having.trim_start_matches("HAVING "))); } join_clauses.extend( nested_join