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): IN operator for comparison #2812

Merged
merged 4 commits into from
Dec 17, 2024

Conversation

Larkooo
Copy link
Collaborator

@Larkooo Larkooo commented Dec 16, 2024

Summary by CodeRabbit

  • New Features
    • Enhanced query capabilities for member comparisons with the addition of IN and NOT_IN comparison types.
    • Introduced a new MemberValueList message for handling lists of member values.
  • Bug Fixes
    • Improved error handling across various methods for better robustness.

Copy link

coderabbitai bot commented Dec 16, 2024

Warning

Rate limit exceeded

@Larkooo has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 27 minutes and 0 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 06e557f and 5dc0c8c.

📒 Files selected for processing (1)
  • crates/torii/grpc/src/server/mod.rs (5 hunks)

Walkthrough

Ohayo, sensei! The pull request introduces significant updates to the MemberValue and MemberClause messages in the types.proto file. A new field, MemberValueList, has been added to MemberValue, allowing for a list of values. The MemberClause message has been restructured by removing the ComparisonOperator enum and integrating new comparison types directly into it, including IN and NOT_IN. Additionally, the DojoWorld service has been enhanced with improved query handling and error management.

Changes

File Change Summary
crates/torii/grpc/proto/types.proto - Updated MemberValue to include MemberValueList field
- Added MemberValueList message with repeated MemberValue field
- Modified ComparisonOperator enum to include IN and NOT_IN
crates/torii/grpc/src/types/mod.rs - Updated ComparisonOperator enum to add In and NotIn variants
- Modified fmt::Display for ComparisonOperator to include new variants
- Updated conversion logic for ComparisonOperator
crates/torii/grpc/src/server/mod.rs - Added helper function prepare_comparison for improved query handling
- Refactored query_by_member method to utilize the new helper function
- Updated build_composite_clause for consistency in handling member value comparisons

Possibly Related PRs

Suggested Reviewers

  • glihm

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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: 1

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 8111b6d and 3d6c072.

📒 Files selected for processing (1)
  • crates/torii/grpc/proto/types.proto (1 hunks)
🔇 Additional comments (1)
crates/torii/grpc/proto/types.proto (1)

140-142: LGTM on the addition of 'MemberValueList' for 'in' comparisons

Ohayo, sensei! The addition of MemberValueList to support 'in' comparisons enhances the expressiveness of queries. Nicely done!

Comment on lines 129 to 138
oneof comparison {
MemberValue eq = 4;
MemberValue neq = 5;
MemberValue gt = 6;
MemberValue gte = 7;
MemberValue lt = 8;
MemberValue lte = 9;
MemberValueList in = 10;
}
}
Copy link

Choose a reason for hiding this comment

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

💡 Codebase verification

🛠️ Refactor suggestion

Ohayo! Update needed for MemberClause implementation in Rust code

The proto definition has moved from using operator and value fields to a oneof comparison field, but the Rust implementation in crates/torii/grpc/src/types/mod.rs still uses the old structure. Please update the Rust implementation to match the proto definition:

  • crates/torii/grpc/src/types/mod.rs: Replace operator and value fields with a comparison enum field that matches the proto definition.
🔗 Analysis chain

Ensure backward compatibility and update documentation for 'MemberClause'

Ohayo, sensei! The restructuring of MemberClause introduces a oneof comparison with multiple comparison types. Please verify that this change maintains backward compatibility with existing clients. Also, consider updating any related documentation to reflect the new structure.

Run the following script to check for usages of the old operator and value fields:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find usages of the old 'operator' and 'value' fields in 'MemberClause'
# Expected: No remaining usages of 'operator' or 'value' in 'MemberClause' across the codebase

# Search for 'operator' field in 'MemberClause'
rg 'MemberClause.*operator'

# Search for 'value' field in 'MemberClause'
rg 'MemberClause.*value'

Length of output: 56


Script:

#!/bin/bash
# Let's try a more comprehensive search to understand the changes and their impact

# Check the git history of the proto file to see when these changes were made
git log -p -- crates/torii/grpc/proto/types.proto | head -n 50

# Search for any MemberClause usage patterns in the codebase
rg "MemberClause" -A 3 -B 3

# Look for any documentation files that might need updates
fd "README|CHANGELOG|CONTRIBUTING" --type f --exec cat {}

Length of output: 42574


Script:

#!/bin/bash
# Search for any test files or examples using MemberClause
fd "test|example" --type f --exec rg "MemberClause" {} \;

Length of output: 60


Script:

#!/bin/bash
# Check for any client implementations
rg -g "*.{rs,go,js,py,java}" "MemberClause"

Length of output: 422


Script:

#!/bin/bash
# Let's check the git history of the proto file to see the changes
git log -p crates/torii/grpc/proto/types.proto | grep -A 20 "message MemberClause"

# Check for any tests or implementations that might be affected
rg -g "*.rs" "impl.*MemberClause|test.*MemberClause" -B 3 -A 3

Length of output: 465


Script:

#!/bin/bash
# Look for any client code that might be consuming this proto
fd -e rs -e ts -e js -e py -e go -x rg "MemberClause.*comparison|MemberClause.*operator" {} \;

Length of output: 99


Script:

#!/bin/bash
# Let's check the current implementation of MemberClause in the codebase
rg -g "*.rs" "struct MemberClause" -B 5 -A 10

# And look for any proto message definitions that might be using it
rg -g "*.proto" "message MemberClause" -B 5 -A 15

Length of output: 2274

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: 2

🧹 Nitpick comments (1)
crates/torii/grpc/src/types/mod.rs (1)

165-172: Consider wrapping string values in quotes in fmt::Display for MemberValue

When displaying MemberValue::String, wrapping the string in quotes can improve clarity and distinguish it from other types.

Apply this diff to wrap string values in quotes:

-            MemberValue::String(string) => write!(f, "{}", string),
+            MemberValue::String(string) => write!(f, "\"{}\"", string),
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 5e62115 and 8da21e7.

📒 Files selected for processing (2)
  • crates/torii/grpc/src/server/mod.rs (1 hunks)
  • crates/torii/grpc/src/types/mod.rs (3 hunks)
🔇 Additional comments (4)
crates/torii/grpc/src/server/mod.rs (1)

1294-1294: Ohayo, sensei! The code update aligns with the new MemberClause structure

The change correctly accesses member.comparison.value, reflecting the updated structure. Looks good!

crates/torii/grpc/src/types/mod.rs (3)

196-207: Ohayo, sensei! The Comparison enum addition enhances expressiveness

The introduction of the Comparison enum with various comparison variants improves the flexibility of member comparisons. Nice work!


209-239: All comparison variants are correctly handled in fmt::Display

The fmt::Display implementation for Comparison handles all variants appropriately, ensuring proper formatting. Well done!


434-444: Ohayo, sensei! Correctly updated TryFrom for MemberClause

The TryFrom implementation for MemberClause now properly handles the new comparison field, ensuring accurate conversion.

Comment on lines 154 to 163
impl TryFrom<proto::types::MemberValue> for MemberValue {
type Error = SchemaError;
fn try_from(value: proto::types::MemberValue) -> Result<Self, Self::Error> {
Ok(match value.value_type {
Some(proto::types::member_value::ValueType::Primitive(primitive)) => MemberValue::Primitive(primitive.try_into()?),
Some(proto::types::member_value::ValueType::String(string)) => MemberValue::String(string),
None => return Err(SchemaError::MissingExpectedData("value_type".into())),
})
}
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Ensure consistent error handling in TryFrom for MemberValue

In the implementation of TryFrom<proto::types::MemberValue> for MemberValue, errors from primitive.try_into() might not match the SchemaError type specified. Consider mapping or converting the error types to ensure consistency.

Comment on lines 244 to 260
impl TryFrom<proto::types::member_clause::Comparison> for Comparison {
type Error = FromStrError;
fn try_from(operator: proto::types::member_clause::Comparison) -> Result<Self, Self::Error> {
Ok(match operator {
proto::types::member_clause::Comparison::Eq(value) => Comparison::Eq(value.try_into()?),
proto::types::member_clause::Comparison::Gte(value) => Comparison::Gte(value.try_into()?),
proto::types::member_clause::Comparison::Gt(value) => Comparison::Gt(value.try_into()?),
proto::types::member_clause::Comparison::Lt(value) => Comparison::Lt(value.try_into()?),
proto::types::member_clause::Comparison::Lte(value) => Comparison::Lte(value.try_into()?),
proto::types::member_clause::Comparison::Neq(value) => Comparison::Neq(value.try_into()?),
proto::types::member_clause::Comparison::In(values) => {
Comparison::In(values.values.into_iter().map(|v| v.try_into()).collect::<Result<Vec<_>, _>>()?)
}
proto::types::member_clause::Comparison::NotIn(values) => {
Comparison::NotIn(values.values.into_iter().map(|v| v.try_into()).collect::<Result<Vec<_>, _>>()?)
}
})
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Review error type in TryFrom implementation for Comparison

The impl TryFrom<proto::types::member_clause::Comparison> for Comparison specifies an Error type of FromStrError, but the try_into() calls can produce SchemaError. Consider aligning the error types for consistency.

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: 1

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

845-845: Inconsistent error handling patterns detected, sensei!

The value extraction pattern differs slightly between query_by_member and build_composite_clause:

// In query_by_member
member_clause.value.clone().ok_or(QueryError::MissingParam("value".into()))?

// In build_composite_clause
member.value.clone().ok_or(QueryError::MissingParam("value".into()))?

While both work correctly, maintaining consistent error handling patterns across the codebase improves maintainability.

Consider extracting this common pattern into a helper function to ensure consistent error handling:

fn extract_member_value(value: Option<proto::types::MemberValue>) -> Result<proto::types::MemberValue, Error> {
    value.ok_or_else(|| QueryError::MissingParam("value".into()).into())
}

Also applies to: 1364-1364

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 8da21e7 and 2c18b72.

📒 Files selected for processing (3)
  • crates/torii/grpc/proto/types.proto (2 hunks)
  • crates/torii/grpc/src/server/mod.rs (3 hunks)
  • crates/torii/grpc/src/types/mod.rs (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • crates/torii/grpc/src/types/mod.rs
  • crates/torii/grpc/proto/types.proto
🔇 Additional comments (1)
crates/torii/grpc/src/server/mod.rs (1)

776-788: Ohayo! Nice implementation of the comparison value extraction logic!

The function correctly handles all value types including the new list support for IN operator comparisons.

Comment on lines 1365 to 1377
fn comparison_value(value: &proto::types::MemberValue) -> Result<String, Error> {
match &value.value_type {
Some(ValueType::String(value)) => Ok(value.to_string()),
Some(ValueType::Primitive(value)) => {
let primitive: Primitive = value.try_into()?;
primitive.to_sql_value()
let primitive: Primitive = (value.clone()).try_into()?;
Ok(primitive.to_sql_value())
}
Some(ValueType::List(values)) => {
Ok(format!("({})", values.values.iter().map(|v| comparison_value(v)).collect::<Result<Vec<String>, Error>>()?.join(", ")))
}
None => return Err(QueryError::MissingParam("value_type".into()).into()),
};
bind_values.push(comparison_value);
}
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Code duplication detected, sensei!

The comparison_value function is duplicated. Consider extracting it to a single location, perhaps as a private function at module level, to maintain DRY principles.

Apply this refactor to remove the duplication:

// Add at module level
-fn comparison_value(value: &proto::types::MemberValue) -> Result<String, Error> {
-    match &value.value_type {
-        Some(ValueType::String(value)) => Ok(value.to_string()),
-        Some(ValueType::Primitive(value)) => {
-            let primitive: Primitive = (value.clone()).try_into()?;
-            Ok(primitive.to_sql_value())
-        }
-        Some(ValueType::List(values)) => {
-            Ok(format!("({})", values.values.iter().map(|v| comparison_value(v)).collect::<Result<Vec<String>, Error>>()?.join(", ")))
-        }
-        None => return Err(QueryError::MissingParam("value_type".into()).into()),
-    }
-}

// Remove the first implementation and use the module level function
// Remove the second implementation and use the module level function

Committable suggestion skipped: line range outside the PR's diff.

Copy link

codecov bot commented Dec 17, 2024

Codecov Report

Attention: Patch coverage is 0% with 70 lines in your changes missing coverage. Please review.

Project coverage is 55.62%. Comparing base (3f9c309) to head (5dc0c8c).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
crates/torii/grpc/src/server/mod.rs 0.00% 66 Missing ⚠️
crates/torii/grpc/src/types/mod.rs 0.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2812      +/-   ##
==========================================
- Coverage   55.67%   55.62%   -0.05%     
==========================================
  Files         439      439              
  Lines       55699    55744      +45     
==========================================
- Hits        31009    31008       -1     
- Misses      24690    24736      +46     

☔ 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.

2 participants