Skip to content

Conversation

Ifechukwudaniel
Copy link

@Ifechukwudaniel Ifechukwudaniel commented Feb 10, 2025

Summary

The CountExpression is designed to count the number of entities returned by a given query. It utilizes run_get_expr to fetch the query result and then determines the count based on the number of items in the result.

Related Issue

#62

Technical details

Uses run_get_expr to fetch results.
Matches result types explicitly.
Converts .len() to a Count result.
Prevents nested count operations.

Topics for Discussion

PR Checklist

  • Added Tests
  • Cargo Tests Passing
  • Added Documentation
  • Breaking changes

Screenshots (if applicable)

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features
    • Added support for performing count queries. Users now see count results displayed in table format along with other query outputs across the CLI and REPL.
    • Enhanced command parsing to recognize and handle the new count command.
  • Refactor
    • Streamlined internal processing and organization to better handle new counting operations.
  • Tests
    • Introduced tests to ensure the accuracy of count query outcomes.

Copy link
Contributor

coderabbitai bot commented Feb 10, 2025

Walkthrough

The changes extend the system's functionality by adding support for count operations. A new Count variant has been introduced in multiple modules, including query results, expressions, and serializers. Modifications in the CLI, REPL, and execution engine now handle and display count results properly. Additionally, improvements in logs parsing and code organization (removing duplicate module declarations, adding newlines) have been made, ensuring consistency and maintainability across the codebase.

Changes

File(s) Change Summary
crates/cli/src/main.rs, crates/cli/src/repl.rs Added support for the Count variant in CLI and REPL. The new match arms use to_table for displaying count results with appropriate formatting.
crates/core/src/common/query_result.rs, crates/core/src/common/types.rs Introduced the Count variant to ExpressionResult and Expression enum; added the CountQueryRes and CountExpression structs with default implementations to support count operations.
crates/core/src/common/serializer.rs Extended serialization functions (CSV and Parquet) to support the new Count variant by adding corresponding match arms invoking specialized serialization logic.
crates/core/src/interpreter/backend/execution_engine.rs Added a new method run_count_expr to process count expressions; updated the run method to handle both count and get expressions; adjusted test cases to validate count functionality.
crates/core/src/common/logs.rs Refined the try_from method in LogFilter by simplifying the checksum logic (removed an extra argument) and reorganized the helper function extract_value for improved readability and maintainability.
crates/core/src/interpreter/backend/mod.rs, crates/macros/src/lib.rs Removed the duplicate module declaration in mod.rs and added a newline at the end of lib.rs.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant REPL
    participant Engine
    participant Serializer

    User->>REPL: Enter query with Count expression
    REPL->>Engine: Forward Count query for processing
    Engine->>Engine: Execute run_count_expr() to compute count result
    Engine->>REPL: Return ExpressionResult::Count
    REPL->>Serializer: Format count result via to_table()
    REPL->>User: Display count result in cyan
Loading

Possibly related PRs

  • refactor: improve entity parsing #48: The changes in the main PR, which add a new variant Count to the ExpressionResult enum and implement handling for it in the ResultHandler and Repl, are directly related to the modifications in the retrieved PR that also involve enhancements to entity parsing, specifically regarding how entities like Count are processed.
  • feat: transaction filters #49: The changes in the main PR, which introduce a new variant Count to the ExpressionResult enum and its handling in the ResultHandler struct, are directly related to the modifications in the retrieved PR that enhance entity parsing, specifically through the introduction of new parsing rules and structures that likely include the Count variant.

Poem

I'm a bunny on a gentle stroll,
Hopping through code with a joyful soul.
Count results now spring up bright and clear,
Numbers and logs dance, oh so dear!
With every hop, my circuits cheer! 🐰✨


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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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
Contributor

@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 (3)
crates/core/src/common/types.rs (1)

16-19: Add documentation for the new struct.

Please add documentation comments explaining the purpose and usage of CountExpression. This will help other developers understand how to use this new feature.

+/// Represents a count operation on a query result.
+/// The count is performed on the results of the contained query.
 #[derive(Debug, PartialEq)]
 pub struct CountExpression {
     pub query: GetExpression,
 }
crates/core/src/common/serializer.rs (1)

111-172: Add test coverage for count serialization.

Please add test cases for serializing count results in all formats (JSON, CSV, and Parquet) to maintain test coverage.

Would you like me to help generate the test cases for count serialization?

crates/core/src/interpreter/backend/execution_engine.rs (1)

44-44: Remove debug print statement.

The debug print statement should be removed before merging.

-println!("Query results: {:#?}", query_results);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9b791a8 and c2722ce.

📒 Files selected for processing (9)
  • crates/cli/src/main.rs (1 hunks)
  • crates/cli/src/repl.rs (1 hunks)
  • crates/core/src/common/logs.rs (1 hunks)
  • crates/core/src/common/query_result.rs (2 hunks)
  • crates/core/src/common/serializer.rs (2 hunks)
  • crates/core/src/common/types.rs (1 hunks)
  • crates/core/src/interpreter/backend/execution_engine.rs (16 hunks)
  • crates/core/src/interpreter/backend/mod.rs (1 hunks)
  • crates/macros/src/lib.rs (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • crates/macros/src/lib.rs
  • crates/core/src/interpreter/backend/mod.rs
🔇 Additional comments (8)
crates/core/src/common/types.rs (1)

13-14: LGTM! New variant added to support count operations.

The Count variant is a clean addition to the Expression enum.

crates/cli/src/main.rs (1)

61-63: LGTM! Consistent handling of count results.

The implementation follows the same pattern as other result types, maintaining consistency in the codebase.

crates/core/src/common/serializer.rs (2)

28-29: LGTM! CSV serialization for count results.

The implementation correctly handles count results using the existing serialize_csv function.


63-64: LGTM! Parquet serialization for count results.

The implementation correctly handles count results using the existing create_parquet_schema_and_data function.

crates/core/src/common/logs.rs (1)

144-147: Review address checksum handling.

The current implementation passes None twice:

  1. First to Address::to_checksum
  2. Then to Address::parse_checksummed

This might not be the intended behavior. Please verify if this is correct or if one of the None parameters should be different.

✅ Verification successful

🏁 Script executed:

#!/bin/bash
# Search for other usages of these methods to verify correct usage
rg -A 2 "to_checksum|parse_checksummed" --type rust

Length of output: 369


Checksum Handling Confirmation: Based on a repository-wide search, the only instance found for calling Address::to_checksum and Address::parse_checksummed is the one in crates/core/src/common/logs.rs, and both functions are consistently invoked with None for their optional parameters. There’s no evidence elsewhere in the codebase suggesting that a non-None value should be provided. Please verify against the function definitions to ensure that these default behaviors are indeed as intended.

crates/cli/src/repl.rs (1)

244-249: LGTM!

The implementation for displaying count results follows the same pattern as other result types and maintains color consistency with block and log results.

crates/core/src/common/query_result.rs (1)

28-30: LGTM!

The implementation of Count variant and CountQueryRes struct follows the same pattern as other result types, with proper serde attributes and default implementation.

Also applies to: 339-351

crates/core/src/interpreter/backend/execution_engine.rs (1)

108-175: LGTM!

The test cases for counting logs and block fields are well-structured and verify the count functionality correctly.

Copy link
Contributor

@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 (3)
crates/core/src/interpreter/backend/execution_engine.rs (3)

44-44: Remove debug print statement.

Debug print statements should not be committed to production code.

-    println!("Query results: {:#?}", query_results);

72-85: Simplify count implementation using match expression.

The count implementation can be simplified by directly returning the count result from the match expression.

 async fn run_count_expr(&self, expr: &CountExpression) -> Result<ExpressionResult> {
     let query_result = self.run_get_expr(&expr.query).await?;
-    let count = match query_result {
+    Ok(ExpressionResult::Count(vec![CountQueryRes { count: match query_result {
         ExpressionResult::Block(data) => data.len(),
         ExpressionResult::Account(data) => data.len(),
         ExpressionResult::Transaction(data) => data.len(),
         ExpressionResult::Log(data) => data.len(),
         ExpressionResult::Count(_) => {
             return Err(anyhow::anyhow!("Unexpected Count result from run_get_expr"))
         }
-    };
-    let result =  ExpressionResult::Count(vec![CountQueryRes{ count }]);
-    Ok(result)
+    }}]))
 }

225-225: Remove debug print statement from test.

Debug print statements in tests can clutter test output.

-    println!("Results: {:#?}", execution_result); 
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c2722ce and 0b5374c.

📒 Files selected for processing (1)
  • crates/core/src/interpreter/backend/execution_engine.rs (16 hunks)
🔇 Additional comments (1)
crates/core/src/interpreter/backend/execution_engine.rs (1)

110-145: Enhance test coverage for count functionality.

The current tests only verify the count=1 case. Consider adding test cases for:

  1. Empty results (count=0)
  2. Multiple results (count>1)
  3. Error cases (e.g., invalid queries)

Also applies to: 148-177

@Ifechukwudaniel Ifechukwudaniel changed the title feat: count with test feat: count query and tests Feb 10, 2025
Copy link
Contributor

@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 (4)
crates/core/src/common/types.rs (1)

27-52: Consider implementing the commented-out error variants.

The commented-out error variants in CountExpressionError seem useful for handling various failure scenarios such as missing entities, chain/RPC issues, and URL parsing errors. These would provide more specific error handling and better error messages.

Apply this diff to implement the error variants:

 #[derive(thiserror::Error, Debug)]
 pub enum CountExpressionError {
     #[error("Unexpected token: {0}")]
     UnexpectedToken(String),
-//     #[error("Missing entity")]
-//     MissingEntity,
-//     #[error("Missing chain or RPC")]
-//     MissingChainOrRpc,
-//     #[error("URL parse error: {0}")]
-//     UrlParseError(String),
-//     #[error(transparent)]
-//     EntityError(#[from] EntityError),
-//     #[error(transparent)]
-//     ChainError(#[from] ChainError),
-//     #[error(transparent)]
-//     DumpError(#[from] DumpError),
+    #[error("Missing entity")]
+    MissingEntity,
+    #[error("Missing chain or RPC")]
+    MissingChainOrRpc,
+    #[error("URL parse error: {0}")]
+    UrlParseError(String),
+    #[error(transparent)]
+    EntityError(#[from] EntityError),
+    #[error(transparent)]
+    ChainError(#[from] ChainError),
+    #[error(transparent)]
+    DumpError(#[from] DumpError),
 }
crates/core/src/interpreter/frontend/productions.pest (1)

3-11: Add documentation for the count rule.

Consider adding a comment explaining the syntax and usage of the count rule for better maintainability.

Add this documentation above the count rule:

+// The count rule defines the syntax for counting entities:
+// COUNT(<query>) where <query> is a valid get expression
+// Example: COUNT(GET * FROM block 1:100 ON eth)
 count = {
     "COUNT" ~
     WHITESPACE* ~
     "(" ~
     WHITESPACE* ~
     query ~
     WHITESPACE* ~
     ")"
 }
crates/core/src/interpreter/backend/execution_engine.rs (2)

109-144: Add more test cases for log counting.

Consider adding test cases for:

  • Empty result (no matching logs)
  • Multiple matching logs
  • Invalid filters

147-176: Add more test cases for block counting.

Consider adding test cases for:

  • Multiple blocks in range
  • Block tag (latest, earliest)
  • Invalid block range
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0b5374c and b093f4d.

📒 Files selected for processing (4)
  • crates/core/src/common/types.rs (1 hunks)
  • crates/core/src/interpreter/backend/execution_engine.rs (15 hunks)
  • crates/core/src/interpreter/frontend/parser.rs (1 hunks)
  • crates/core/src/interpreter/frontend/productions.pest (2 hunks)
🔇 Additional comments (7)
crates/core/src/common/types.rs (3)

11-14: LGTM! The Expression enum is correctly extended.

The addition of the Count variant to handle count expressions aligns well with the existing structure.


16-25: LGTM! Well-structured CountExpression implementation.

The struct design is clean and reuses the existing GetExpression type, promoting code reuse.


54-71: LGTM! Robust TryFrom implementation.

The implementation properly validates the input and handles error cases while converting from parser pairs to CountExpression.

crates/core/src/interpreter/frontend/productions.pest (2)

1-1: LGTM! Program rule correctly extended.

The program rule now properly supports both get and count expressions.


13-13: LGTM! Query rule is correctly defined.

The query rule appropriately references the get rule, maintaining consistency with the CountExpression implementation.

crates/core/src/interpreter/backend/execution_engine.rs (2)

28-46: LGTM! Run method properly handles Count expressions.

The implementation correctly processes both Get and Count expressions, maintaining consistency in result handling.


71-84: LGTM! Improved error handling in run_count_expr.

The implementation now properly handles all cases and includes explicit error handling for unexpected Count results.

Comment on lines +36 to +38
Rule::count => {
expressions.push(Expression::Count(pair.into_inner().try_into()?));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add tests for count expression parsing.

The implementation looks good, but test coverage is needed for the count expression parsing. Consider adding tests for:

  • Basic count syntax
  • Invalid count syntax (missing parentheses, invalid query)
  • Multiple count expressions

Example test to add:

#[test]
fn test_build_ast_with_count_expression() {
    let source = "COUNT(GET * FROM block 1:100 ON eth)";
    let expected = vec![Expression::Count(CountExpression {
        query: GetExpression {
            entity: Entity::Block(Block::new(
                Some(vec![BlockId::Range(BlockRange::new(
                    BlockNumberOrTag::Number(1),
                    Some(BlockNumberOrTag::Number(100)),
                ))]),
                None,
                BlockField::all_variants().to_vec(),
            )),
            chains: vec![ChainOrRpc::Chain(Chain::Ethereum)],
            dump: None,
        }
    })];
    
    match Parser::new(source).parse_expressions() {
        Ok(result) => assert_eq!(result, expected),
        Err(e) => panic!("Error: {}", e),
    }
}

@Ifechukwudaniel Ifechukwudaniel marked this pull request as draft February 12, 2025 05:33
@Ifechukwudaniel Ifechukwudaniel marked this pull request as ready for review February 28, 2025 15:04
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