Skip to content

Conversation

@oasisk
Copy link
Contributor

@oasisk oasisk commented Sep 24, 2024

Summary by CodeRabbit

  • New Features

    • Introduced a new SqlQuery structure for improved SQL query handling.
    • Added a per_query_response feature to enhance query response flexibility.
  • Bug Fixes

    • Improved error handling and response clarity in search functions.
    • Enhanced request formatting for better readability and consistency.
  • Refactor

    • Restructured request and response handling in search_multi and _search_partition_multi functions for improved code clarity.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Sep 24, 2024

Walkthrough

The changes introduce a new SqlQuery struct to encapsulate SQL query details within the MultiStreamRequest struct, replacing the previous vector of strings for SQL queries. A custom deserialization function is added to handle both old and new SQL formats. Additionally, the search_multi and _search_partition_multi functions are modified for improved request handling, error management, and clarity in processing query responses based on the new per_query_response flag.

Changes

File Change Summary
src/config/src/meta/search.rs Added SqlQuery struct and modified MultiStreamRequest to include a vector of SqlQuery and a new field per_query_response. Updated to_query_req method.
src/handler/http/request/search/multi_streams.rs Restructured search_multi and _search_partition_multi functions for better readability and error handling. Adjusted logic for processing multi_res based on per_query_response.

Possibly related PRs

  • feat: apply vrl functions over hits as array #4058: The changes involve modifications to how the query_fn is handled during search requests, which is directly relevant to the new query_fn field introduced in the main PR's SqlQuery struct.
  • fix: multi stream search panic #4101: This PR modifies the search_multi function in the same file as the main PR, enhancing how data is aggregated and sorted, which relates to the overall improvements made in the main PR's handling of SQL queries.
  • fix: always return single partition for ResultArray VRL #4300: This PR adds a query_fn field to the SearchPartitionRequest and MultiSearchPartitionRequest structures, which aligns with the introduction of the query_fn in the main PR's SqlQuery struct, indicating a shared focus on query function handling.
  • feat: search_history api #4511: The addition of the SearchHistoryRequest structure includes a query_fn field, which relates to the new struct introduced in the main PR, indicating a broader context of query handling across different functionalities.

Suggested reviewers

  • hengfeiyang
  • Subhra264

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between d8dbcc4 and e40b1b4.

📒 Files selected for processing (2)
  • src/config/src/meta/search.rs (3 hunks)
  • src/handler/http/request/search/multi_streams.rs (9 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/config/src/meta/search.rs
  • src/handler/http/request/search/multi_streams.rs

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

Outside diff range and nitpick comments (3)
src/config/src/meta/search.rs (3)

742-753: Consider making is_old_format field private

The is_old_format field is used internally for deserialization and isn't required to be publicly accessible. Making it private would enhance encapsulation.


742-753: Add documentation to SqlQuery struct and its fields

Adding doc comments to the SqlQuery struct and its fields would improve code readability and help others understand its purpose.


791-792: Add documentation for per_query_response field

Including a doc comment for the per_query_response field would clarify its purpose and usage.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 14cc702 and 124ea07.

Files selected for processing (2)
  • src/config/src/meta/search.rs (3 hunks)
  • src/handler/http/request/search/multi_streams.rs (9 hunks)
Additional context used
Path-based instructions (2)
src/config/src/meta/search.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

src/handler/http/request/search/multi_streams.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

Additional comments not posted (8)
src/handler/http/request/search/multi_streams.rs (5)

134-136: Improved error handling with block statements

The error handling has been refactored to use block statements, which enhances readability and maintainability. This change is consistent across multiple error checks in the function.

Also applies to: 141-143, 149-151


169-170: New feature: Per-query response aggregation

A new flag per_query_resp has been introduced to control how hits are aggregated in the multi-query response. This provides more flexibility in the API, allowing clients to receive either a flat list of hits or separate arrays of hits for each query.

Also applies to: 350-354


476-478: Consistent error handling improvement

The error handling in _search_partition_multi has been updated to use block statements, consistent with the changes in search_multi. This improves code readability and maintainability across the file.

Also applies to: 483-485


622-624: Consistent error handling in around_multi

The error handling in around_multi has been updated to use block statements, maintaining consistency with the other functions in this file. This change improves the overall readability and maintainability of the codebase.

Also applies to: 629-631


Line range hint 1-924: Overall improvements in error handling and response flexibility

The changes in this file demonstrate a consistent approach to improving error handling across all functions. The use of block statements for error handling enhances readability and maintainability. Additionally, the introduction of the per_query_resp flag in the search_multi function provides more flexibility in response aggregation, allowing clients to receive either a flat list of hits or separate arrays of hits for each query.

These changes contribute to a more robust and flexible implementation of multi-stream search operations. The consistency in applying these improvements across all functions is commendable and will likely make future maintenance easier.

src/config/src/meta/search.rs (3)

19-19: LGTM!

The import of Deserializer is appropriate for the custom deserialization function.


758-759: LGTM!

The custom deserialization for the sql field using deserialize_sql is correctly implemented.


837-847: LGTM!

The Query struct is correctly constructed with appropriate handling of sql, start_time, end_time, and query_fn.

Comment on lines +795 to 824
fn deserialize_sql<'de, D>(deserializer: D) -> Result<Vec<SqlQuery>, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum SqlOrSqlQuery {
OldFormat(String),
NewFormat(SqlQuery),
}

let v: Vec<SqlOrSqlQuery> = Vec::deserialize(deserializer)?;

// Convert old format into the new format
let result: Vec<SqlQuery> = v
.into_iter()
.map(|item| match item {
SqlOrSqlQuery::OldFormat(sql) => SqlQuery {
sql,
start_time: None,
end_time: None,
query_fn: None,
is_old_format: true,
},
SqlOrSqlQuery::NewFormat(query) => query,
})
.collect();

Ok(result)
}
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 unit tests for deserialize_sql function

Introducing unit tests for the deserialize_sql function will ensure both old and new SQL formats are correctly handled and prevent future regressions.

Comment on lines +830 to +834
let query_fn = if query.is_old_format {
self.query_fn.clone()
} else {
query.query_fn.clone()
};
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

Encapsulate is_old_format usage within SqlQuery

Accessing is_old_format outside of SqlQuery can break encapsulation. Consider adding a method within SqlQuery to provide the appropriate query_fn, or handle this logic internally.

@oasisk oasisk force-pushed the feat-multi-search-with-multiple-time-ranges branch from 124ea07 to 4931416 Compare September 24, 2024 10:41
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

Outside diff range and nitpick comments (3)
src/config/src/meta/search.rs (3)

758-759: LGTM: Enhanced MultiStreamRequest struct

The changes to the MultiStreamRequest struct improve its functionality:

  1. The sql field now uses Vec<SqlQuery>, allowing for more detailed query information.
  2. The new per_query_response field has been added, which likely controls how responses are structured.
  3. The custom deserialization function for sql allows for backward compatibility.

These changes enhance the flexibility of the struct while maintaining compatibility with older formats.

Consider adding a comment to explain the purpose and usage of the per_query_response field for better documentation.

Also applies to: 791-793


795-824: LGTM: Well-implemented deserialize_sql function

The deserialize_sql function is well-implemented to handle both old and new formats of SQL queries. It uses an untagged enum approach, which is an elegant solution for this scenario. The conversion logic ensures backward compatibility by setting the is_old_format flag for old format queries.

Consider using Vec::with_capacity(v.len()) when creating the result vector to avoid potential reallocations:

-    let result: Vec<SqlQuery> = v
+    let mut result: Vec<SqlQuery> = Vec::with_capacity(v.len());
+    result.extend(v
         .into_iter()
         .map(|item| match item {
             SqlOrSqlQuery::OldFormat(sql) => SqlQuery {
                 sql,
                 start_time: None,
                 end_time: None,
                 query_fn: None,
                 is_old_format: true,
             },
             SqlOrSqlQuery::NewFormat(query) => query,
-        })
-        .collect();
+        }));

This small optimization can improve performance, especially for large input vectors.


829-848: LGTM: Updated to_query_req method handles new SqlQuery structure well

The changes to the to_query_req method correctly handle the new SqlQuery structure while maintaining backward compatibility. The use of the is_old_format flag to determine which query_fn to use is a good approach. The method also properly handles the optional start_time and end_time values from the SqlQuery.

Consider extracting the query_fn logic into a separate method for improved readability:

impl MultiStreamRequest {
    fn get_query_fn(&self, query: &SqlQuery) -> Option<String> {
        if query.is_old_format {
            self.query_fn.clone()
        } else {
            query.query_fn.clone()
        }
    }

    pub fn to_query_req(&mut self) -> Vec<Request> {
        let mut res = vec![];
        for query in &self.sql {
            let query_fn = self.get_query_fn(query);
            res.push(Request {
                query: Query {
                    sql: query.sql.clone(),
                    // ... rest of the fields ...
                    query_fn,
                    // ... rest of the fields ...
                },
                // ... rest of the fields ...
            });
        }
        res
    }
}

This change would make the to_query_req method more concise and easier to understand.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 124ea07 and 4931416.

Files selected for processing (2)
  • src/config/src/meta/search.rs (3 hunks)
  • src/handler/http/request/search/multi_streams.rs (9 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/handler/http/request/search/multi_streams.rs
Additional context used
Path-based instructions (1)
src/config/src/meta/search.rs (1)

Pattern **/*.rs: You are a smart rustlang pull request reviewer.
You are going to review all the rustlang files.
Be concise, and add a brief explanation to your suggestions

Make sure the responses are not too verbose and keep the suggestions to the point i.e. actionable.

Additional comments not posted (2)
src/config/src/meta/search.rs (2)

742-753: LGTM: Well-structured SqlQuery struct

The new SqlQuery struct is well-designed to encapsulate SQL query information. The use of Option<i64> for start_time and end_time allows for nullable values, and the is_old_format flag is useful for backward compatibility.


Line range hint 1-1161: Overall assessment: Well-implemented multi-search with multiple time ranges

The changes introduced in this file successfully implement multi-search functionality with support for multiple time ranges. Key improvements include:

  1. Introduction of the SqlQuery struct to encapsulate query information.
  2. Updates to the MultiStreamRequest struct to use the new SqlQuery type.
  3. Implementation of a custom deserialization function to handle both old and new query formats.
  4. Updates to the to_query_req method to work with the new structures.

These changes enhance the flexibility and functionality of the search system while maintaining backward compatibility. The code is well-structured and the new features integrate seamlessly with the existing codebase.

@oasisk oasisk force-pushed the feat-multi-search-with-multiple-time-ranges branch from 4931416 to d8dbcc4 Compare September 25, 2024 08:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants