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: keys composite clause #2798

Merged
merged 4 commits into from
Dec 12, 2024
Merged
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
50 changes: 39 additions & 11 deletions crates/torii/grpc/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,16 +742,31 @@
let (where_clause, having_clause, join_clause, bind_values) =
build_composite_clause(table, model_relation_table, &composite)?;

let count_query = format!(
r#"
SELECT COUNT(DISTINCT [{table}].id)
FROM [{table}]
JOIN {model_relation_table} ON [{table}].id = {model_relation_table}.entity_id
{join_clause}
{where_clause}
{having_clause}
"#,
);
let count_query = if !having_clause.is_empty() {
format!(
r#"
SELECT COUNT(*) FROM (
SELECT [{table}].id
FROM [{table}]
JOIN {model_relation_table} ON [{table}].id = {model_relation_table}.entity_id
{join_clause}
{where_clause}
GROUP BY [{table}].id
{having_clause}
) as filtered_count
"#
)

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

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L745-L758

Added lines #L745 - L758 were not covered by tests
} else {
format!(
r#"
SELECT COUNT(DISTINCT [{table}].id)
FROM [{table}]
JOIN {model_relation_table} ON [{table}].id = {model_relation_table}.entity_id
{join_clause}
{where_clause}
"#
)

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

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L760-L768

Added lines #L760 - L768 were not covered by tests
};

let mut count_query = sqlx::query_scalar::<_, u32>(&count_query);
for value in &bind_values {
Expand Down Expand Up @@ -1124,7 +1139,7 @@
let mut keys_pattern = format!("^{}", keys.join("/"));

if clause.pattern_matching == proto::types::PatternMatching::VariableLen as i32 {
keys_pattern += &format!("({})*", KEY_PATTERN);
keys_pattern += &format!("/({})*", KEY_PATTERN);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L1142 was not covered by tests
}
keys_pattern += "/$";

Expand Down Expand Up @@ -1162,6 +1177,19 @@
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
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);

having_clauses.push(format!(
"INSTR(group_concat({model_relation_table}.model_id), '{:#x}') > 0",
model_id
));

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

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L1182-L1191

Added lines #L1182 - L1191 were not covered by tests
}
}
ClauseType::Member(member) => {
let comparison_operator = ComparisonOperator::from_repr(member.operator as usize)
Expand Down
Loading