-
Notifications
You must be signed in to change notification settings - Fork 182
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): start rework to use 1 single query #2817
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
8a81615
feat(torii-grpc): start rework to use 1 single query
Larkooo 060cf0e
hashed keys
Larkooo 3a45876
start using build sql query singular with left joins
Larkooo 9a4d414
optimize all queries
Larkooo ae5fc97
fix: count
Larkooo 754805e
f
Larkooo 55ed216
fix count to use having
Larkooo b0d3683
f
Larkooo ee9f43a
print query
Larkooo 496364f
fix keys clause
Larkooo 78bc49e
having clause keys
Larkooo 343d1e0
having to hashed keys
Larkooo 99049e0
having clause for composite
Larkooo 56e4e6d
fmt
Larkooo 39002ac
fmt
Larkooo e05122f
fix: count
Larkooo ef3b12c
Merge branch 'main' into opt-single-query-grpc
Larkooo 124bc8f
value
Larkooo d5c4e74
indices
Larkooo 745cf35
Merge branch 'main' into opt-single-query-grpc
Larkooo 3a1136c
tests: fix tests
glihm 759c5b8
fix: fmt
glihm feecbdb
tests: fix by passing the list of expected models
glihm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,8 +120,10 @@ impl ModelReader<Error> for ModelSQLReader { | |
pub fn build_sql_query( | ||
schemas: &Vec<Ty>, | ||
table_name: &str, | ||
model_relation_table: &str, | ||
entity_relation_column: &str, | ||
where_clause: Option<&str>, | ||
having_clause: Option<&str>, | ||
order_by: Option<&str>, | ||
limit: Option<u32>, | ||
offset: Option<u32>, | ||
|
@@ -172,6 +174,7 @@ pub fn build_sql_query( | |
// Add base table columns | ||
selections.push(format!("{}.id", table_name)); | ||
selections.push(format!("{}.keys", table_name)); | ||
selections.push(format!("group_concat({model_relation_table}.model_id) as model_ids")); | ||
|
||
// Process each model schema | ||
for model in schemas { | ||
|
@@ -185,19 +188,37 @@ pub fn build_sql_query( | |
collect_columns(&model_table, "", model, &mut selections); | ||
} | ||
|
||
joins.push(format!( | ||
"JOIN {model_relation_table} ON {table_name}.id = {model_relation_table}.entity_id" | ||
)); | ||
|
||
let selections_clause = selections.join(", "); | ||
let joins_clause = joins.join(" "); | ||
|
||
let mut query = format!("SELECT {} FROM [{}] {}", selections_clause, table_name, joins_clause); | ||
|
||
let mut count_query = | ||
format!("SELECT COUNT(DISTINCT {}.id) FROM [{}] {}", table_name, table_name, joins_clause); | ||
// Include model_ids in the subquery and put WHERE before GROUP BY | ||
let mut count_query = format!( | ||
"SELECT COUNT(*) FROM (SELECT {}.id, group_concat({}.model_id) as model_ids FROM [{}] {}", | ||
table_name, model_relation_table, table_name, joins_clause | ||
); | ||
|
||
if let Some(where_clause) = where_clause { | ||
query += &format!(" WHERE {}", where_clause); | ||
count_query += &format!(" WHERE {}", where_clause); | ||
} | ||
|
||
query += &format!(" GROUP BY {table_name}.id"); | ||
count_query += &format!(" GROUP BY {table_name}.id"); | ||
|
||
if let Some(having_clause) = having_clause { | ||
query += &format!(" HAVING {}", having_clause); | ||
count_query += &format!(" HAVING {}", having_clause); | ||
} | ||
|
||
// Close the subquery | ||
count_query += ") AS filtered_entities"; | ||
|
||
// Use custom order by if provided, otherwise default to event_id DESC | ||
if let Some(order_clause) = order_by { | ||
query += &format!(" ORDER BY {}", order_clause); | ||
|
@@ -490,25 +511,28 @@ mod tests { | |
let query = build_sql_query( | ||
&vec![position, player_config], | ||
"entities", | ||
"entity_model", | ||
"internal_entity_id", | ||
None, | ||
None, | ||
None, | ||
None, | ||
None, | ||
Comment on lines
+514
to
+520
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Ohayo! Enhance test coverage for new query features. The test case has been updated with the new parameters, but it doesn't verify the behavior of GROUP BY, HAVING clause, or model_ids aggregation. Add test cases to verify:
#[test]
fn test_query_with_aggregation() {
// Test with model_ids aggregation
}
#[test]
fn test_query_with_having() {
// Test HAVING clause filtering
} |
||
) | ||
.unwrap(); | ||
|
||
let expected_query = | ||
"SELECT entities.id, entities.keys, [Test-Position].[player] as \ | ||
\"Test-Position.player\", [Test-Position].[vec.x] as \"Test-Position.vec.x\", \ | ||
[Test-Position].[vec.y] as \"Test-Position.vec.y\", \ | ||
"SELECT entities.id, entities.keys, group_concat(entity_model.model_id) as model_ids, \ | ||
[Test-Position].[player] as \"Test-Position.player\", [Test-Position].[vec.x] as \ | ||
\"Test-Position.vec.x\", [Test-Position].[vec.y] as \"Test-Position.vec.y\", \ | ||
[Test-Position].[test_everything] as \"Test-Position.test_everything\", \ | ||
[Test-PlayerConfig].[favorite_item] as \"Test-PlayerConfig.favorite_item\", \ | ||
[Test-PlayerConfig].[favorite_item.Some] as \ | ||
\"Test-PlayerConfig.favorite_item.Some\", [Test-PlayerConfig].[items] as \ | ||
\"Test-PlayerConfig.items\" FROM [entities] LEFT JOIN [Test-Position] ON entities.id \ | ||
= [Test-Position].internal_entity_id LEFT JOIN [Test-PlayerConfig] ON entities.id = \ | ||
[Test-PlayerConfig].internal_entity_id ORDER BY entities.event_id DESC"; | ||
[Test-PlayerConfig].internal_entity_id JOIN entity_model ON entities.id = \ | ||
entity_model.entity_id GROUP BY entities.id ORDER BY entities.event_id DESC"; | ||
assert_eq!(query.0, expected_query); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using LEFT JOIN for model relations.
Using JOIN might exclude entities without model relations. Consider using LEFT JOIN to ensure all entities are included in the result set.
📝 Committable suggestion