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

feat(torii-grpc): correct keys clause models predicate #3000

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
82 changes: 58 additions & 24 deletions crates/torii/grpc/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,17 +445,40 @@
entity_updated_after: Option<String>,
) -> Result<(Vec<proto::types::Entity>, u32), Error> {
let keys_pattern = build_keys_pattern(keys_clause)?;
let model_selectors: Vec<String> = keys_clause
.models
.iter()
.map(|model| format!("{:#x}", compute_selector_from_tag(model)))
.collect();

let where_clause = format!(
"{table}.keys REGEXP ? {}",
if entity_updated_after.is_some() {
format!("AND {table}.updated_at >= ?")
} else {
String::new()
}
);
let where_clause = if model_selectors.is_empty() {
format!(
"{table}.keys REGEXP ? {}",
if entity_updated_after.is_some() {
format!("AND {table}.updated_at >= ?")

Check warning on line 458 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L458

Added line #L458 was not covered by tests
} else {
String::new()
}
)
} else {
format!(
"({table}.keys REGEXP ? AND {model_relation_table}.model_id IN ({})) OR \
{model_relation_table}.model_id NOT IN ({}) {}",
vec!["?"; model_selectors.len()].join(", "),
vec!["?"; model_selectors.len()].join(", "),
if entity_updated_after.is_some() {
format!("AND {table}.updated_at >= ?")

Check warning on line 470 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L464-L470

Added lines #L464 - L470 were not covered by tests
} else {
String::new()

Check warning on line 472 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L472

Added line #L472 was not covered by tests
}
)
};

let mut bind_values = vec![keys_pattern];
if !model_selectors.is_empty() {
bind_values.extend(model_selectors.clone());
bind_values.extend(model_selectors);

Check warning on line 480 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L479-L480

Added lines #L479 - L480 were not covered by tests
}
if let Some(entity_updated_after) = entity_updated_after.clone() {
bind_values.push(entity_updated_after);
}
Expand Down Expand Up @@ -704,7 +727,7 @@
entity_updated_after: Option<String>,
) -> Result<(Vec<proto::types::Entity>, u32), Error> {
let (where_clause, bind_values) =
build_composite_clause(table, &composite, entity_updated_after)?;
build_composite_clause(table, model_relation_table, &composite, entity_updated_after)?;

Check warning on line 730 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L730

Added line #L730 was not covered by tests

let entity_models =
entity_models.iter().map(|model| compute_selector_from_tag(model)).collect::<Vec<_>>();
Expand Down Expand Up @@ -1157,6 +1180,7 @@
// builds a composite clause for a query
fn build_composite_clause(
table: &str,
model_relation_table: &str,

Check warning on line 1183 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L1183

Added line #L1183 was not covered by tests
composite: &proto::types::CompositeClause,
entity_updated_after: Option<String>,
) -> Result<(String, Vec<String>), Error> {
Expand All @@ -1181,20 +1205,26 @@
ClauseType::Keys(keys) => {
let keys_pattern = build_keys_pattern(keys)?;
bind_values.push(keys_pattern);
where_clauses.push(format!("({table}.keys REGEXP ?)"));

// Add model checks for specified models

// NOTE: disabled since we are now using the top level entity models

// for model in &keys.models {
// let (namespace, model_name) = model
// .split_once('-')
// .ok_or(QueryError::InvalidNamespacedModel(model.clone()))?;
// let model_id = compute_selector_from_names(namespace, model_name);
let model_selectors: Vec<String> = keys
.models
.iter()
.map(|model| format!("{:#x}", compute_selector_from_tag(model)))
.collect();

Check warning on line 1212 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L1208-L1212

Added lines #L1208 - L1212 were not covered by tests

// having_clauses.push(format!("INSTR(model_ids, '{:#x}') > 0", model_id));
// }
if model_selectors.is_empty() {
where_clauses.push(format!("({table}.keys REGEXP ?)"));
} else {
// Add bind value placeholders for each model selector
let placeholders = vec!["?"; model_selectors.len()].join(", ");
where_clauses.push(format!(
"({table}.keys REGEXP ? AND {model_relation_table}.model_id IN ({})) OR \
{model_relation_table}.model_id NOT IN ({})",
placeholders, placeholders
));
// Add each model selector twice (once for IN and once for NOT IN)
bind_values.extend(model_selectors.clone());
bind_values.extend(model_selectors);
}

Check warning on line 1227 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L1214-L1227

Added lines #L1214 - L1227 were not covered by tests
}
ClauseType::Member(member) => {
let comparison_operator = ComparisonOperator::from_repr(member.operator as usize)
Expand Down Expand Up @@ -1236,8 +1266,12 @@
}
ClauseType::Composite(nested) => {
// Handle nested composite by recursively building the clause
let (nested_where, nested_values) =
build_composite_clause(table, nested, entity_updated_after.clone())?;
let (nested_where, nested_values) = build_composite_clause(
table,
model_relation_table,
nested,
entity_updated_after.clone(),
)?;

Check warning on line 1274 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L1269-L1274

Added lines #L1269 - L1274 were not covered by tests

if !nested_where.is_empty() {
where_clauses.push(nested_where);
Expand Down
Loading