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

Conversation

Larkooo
Copy link
Collaborator

@Larkooo Larkooo commented Feb 7, 2025

Summary by CodeRabbit

  • New Features
    • Enhanced query filtering now allows more refined data retrieval by combining conditions based on keys and related models.
    • Improved query flexibility provides more accurate and targeted results during searches.

Copy link

coderabbitai bot commented Feb 7, 2025

Ohayo sensei!

Walkthrough

This PR refines the querying logic in the DojoWorld struct by modifying the query_by_keys and build_composite_clause methods. A new variable, model_selectors, is introduced to compute selectors from the keys_clause and conditionally adjust the where_clause based on the presence of model selectors. Additionally, build_composite_clause now accepts an extra parameter, model_relation_table, to incorporate model checks into the composite clause. These modifications allow the query to filter based on keys and associated models for more precise SQL query construction.

Changes

File Summary
crates/torii/grpc/src/server/mod.rs Updated query_by_keys to compute model_selectors and conditionally build the where_clause; modified build_composite_clause to include model_relation_table and blend key conditions with model checks

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant DojoWorld
    participant Database
    Client->>DojoWorld: query_by_keys(keys_clause)
    DojoWorld->>DojoWorld: Compute model_selectors from keys_clause
    alt model_selectors not empty
        DojoWorld->>DojoWorld: Build where_clause with model check
    else
        DojoWorld->>DojoWorld: Build default where_clause
    end
    DojoWorld->>Database: Execute query with where_clause
    Database-->>DojoWorld: Return results
    DojoWorld-->>Client: Return results
Loading
sequenceDiagram
    participant DojoWorld
    participant ClauseBuilder
    DojoWorld->>ClauseBuilder: build_composite_clause(keys, model_relation_table)
    ClauseBuilder->>ClauseBuilder: Evaluate key type & model_selectors
    alt model_selectors exist
        ClauseBuilder->>ClauseBuilder: Combine key condition with model check
    else
        ClauseBuilder->>ClauseBuilder: Build clause using keys only
    end
    ClauseBuilder-->>DojoWorld: Return composite clause
Loading

Possibly related PRs

  • refactor: member clauses handle struct fields & operators & recursive #2420: The changes in the main PR directly modify the build_composite_clause method and enhance the query_by_keys method, while the retrieved PR also modifies the build_composite_clause method and introduces a new method that relies on it, indicating a strong connection between the two.
  • opt(torii-grpc): entity updated after on entities table #2810: The changes in the main PR are related to the modifications made to the query_by_keys method in the retrieved PR, as both involve updates to the method's parameters and logic within the same function.
  • fix: keys composite clause #2798: The changes in the main PR are related to the modifications made to the build_composite_clause function in both PRs, as they both involve enhancements for handling model checks within composite queries.

Suggested reviewers

  • glihm

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
crates/torii/grpc/src/server/mod.rs (2)

460-460: Ohayo sensei, watch out for line length warnings.
As flagged in the pipeline failures, lines 460 and 467 exceed recommended length. Consider breaking them for better readability.

Also applies to: 467-467

🧰 Tools
🪛 GitHub Actions: ci

[warning] 460-460: Line exceeds recommended length. Consider breaking the line for better readability.


1207-1207: Ohayo sensei, line length warning.
Line 1207 also exceeds recommended length. Consider splitting it into multiple lines to comply with style guidelines.

🧰 Tools
🪛 GitHub Actions: ci

[warning] 1207-1207: Line exceeds recommended length. Consider breaking the line for better readability.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d11a7ec and 4fc77de.

📒 Files selected for processing (1)
  • crates/torii/grpc/src/server/mod.rs (5 hunks)
🧰 Additional context used
🪛 GitHub Actions: ci
crates/torii/grpc/src/server/mod.rs

[warning] 460-460: Line exceeds recommended length. Consider breaking the line for better readability.


[warning] 467-467: Line exceeds recommended length. Consider breaking the line for better readability.


[warning] 1207-1207: Line exceeds recommended length. Consider breaking the line for better readability.

🔇 Additional comments (4)
crates/torii/grpc/src/server/mod.rs (4)

448-449: Ohayo sensei, nice introduction of model_selectors!
No issues spotted here, it looks neat and well-structured.


725-725: Ohayo sensei, everything looks consistent!
The extra parameter for build_composite_clause is correctly passed, aiding more refined query building.


451-474: Ohayo sensei, verify the OR logic in the WHERE clause.
Because the condition includes "… OR {model_relation_table}.model_id NOT IN ({models})", rows with a model_id outside the specified set might still pass. If that’s intended, great. Otherwise, consider using a stricter check to avoid unintentionally including all models not in the set.

Below is a potential fix if you want to ensure the row only matches when the model belongs to the specified set:

- "({table}.keys REGEXP ? AND {model_relation_table}.model_id IN ({models})) OR {model_relation_table}.model_id NOT IN ({models})"
+ "{table}.keys REGEXP ? AND {model_relation_table}.model_id IN ({models})"
🧰 Tools
🪛 GitHub Actions: ci

[warning] 460-460: Line exceeds recommended length. Consider breaking the line for better readability.


[warning] 467-467: Line exceeds recommended length. Consider breaking the line for better readability.


1204-1216: Ohayo sensei, double-check the OR condition here as well.
Similar to lines 451-474, “OR {model_relation_table}.model_id NOT IN” may inadvertently match all rows that do not belong to the specified models. Confirm if that’s the intended behavior.

- "({table}.keys REGEXP ? AND {model_relation_table}.model_id IN ({models})) OR {model_relation_table}.model_id NOT IN ({models})"
+ "{table}.keys REGEXP ? AND {model_relation_table}.model_id IN ({models})"
🧰 Tools
🪛 GitHub Actions: ci

[warning] 1207-1207: Line exceeds recommended length. Consider breaking the line for better readability.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
crates/torii/grpc/src/server/mod.rs (2)

448-481: Ohayo! The query construction looks good but could be optimized sensei!

The implementation correctly handles model filtering and prevents SQL injection through proper use of bind values. However, we could optimize the model selectors handling:

  1. We're extending bind values twice with the same model selectors
  2. The SQL query could be simplified when there are no model selectors

Consider this optimization:

-        if !model_selectors.is_empty() {
-            bind_values.extend(model_selectors.clone());
-            bind_values.extend(model_selectors);
-        }
+        if !model_selectors.is_empty() {
+            // Add model selectors once and reuse the same bind values
+            let model_placeholders = vec!["?"; model_selectors.len()].join(", ");
+            bind_values.extend(model_selectors);
+        }

1208-1227: Similar optimization opportunity here sensei!

The implementation mirrors the changes in query_by_keys, with the same opportunity for optimization in model selectors handling. The security aspect is well-handled with proper bind value usage.

Consider applying the same optimization as suggested for query_by_keys:

-                    bind_values.extend(model_selectors.clone());
-                    bind_values.extend(model_selectors);
+                    // Add model selectors once and reuse the same bind values
+                    let model_placeholders = vec!["?"; model_selectors.len()].join(", ");
+                    bind_values.extend(model_selectors);
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4fc77de and c9a2401.

📒 Files selected for processing (1)
  • crates/torii/grpc/src/server/mod.rs (5 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: clippy
  • GitHub Check: build

Copy link

codecov bot commented Feb 7, 2025

Codecov Report

Attention: Patch coverage is 24.00000% with 38 lines in your changes missing coverage. Please review.

Project coverage is 57.08%. Comparing base (d11a7ec) to head (c9a2401).

Files with missing lines Patch % Lines
crates/torii/grpc/src/server/mod.rs 24.00% 38 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3000      +/-   ##
==========================================
- Coverage   57.10%   57.08%   -0.03%     
==========================================
  Files         429      429              
  Lines       56868    56910      +42     
==========================================
+ Hits        32475    32487      +12     
- Misses      24393    24423      +30     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant