Skip to content

refactor: schema mismatch check #1259

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

de-sh
Copy link
Contributor

@de-sh de-sh commented Mar 21, 2025

Fixes #XXXX.

Description

ref: #1218 (comment)


This PR has:

  • been tested to ensure log ingestion and log query works.
  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
  • added documentation for new or modified features or behaviors.

Summary by CodeRabbit

  • Refactor
    • Streamlined event data validation by consolidating checks for data format compatibility. This update improves error handling by providing clearer feedback when event data does not match the expected schema.
    • Introduced a new standalone function for schema matching, simplifying the validation process.

Copy link

coderabbitai bot commented Mar 21, 2025

Walkthrough

The changes modify the into_recordbatch method in the EventFormat trait by removing the previous implementation of is_schema_matching as a method of the trait and introducing a new standalone function. This new function simplifies the schema matching logic by taking a slice of fields and checking compatibility with the storage_schema. The creation of the new_schema variable has been adjusted to occur after schema validation, streamlining the control flow and ensuring correctness through added unit tests.

Changes

File Change Summary
src/event/.../mod.rs Removed is_schema_matching method from EventFormat trait; added standalone is_schema_matching function; updated logic for new_schema creation and validation.

Sequence Diagram(s)

sequenceDiagram
    participant C as Caller
    participant E as EventFormat::into_recordbatch
    participant V as Schema Validator

    C->>E: Invoke into_recordbatch(new_schema, storage_schema, static_schema_flag)
    E->>V: For each field, check using new is_schema_matching
    alt Schema mismatch found
        V-->>E: Return mismatch error
        E-->>C: Return error response ("Schema mismatch")
    else All fields valid
        V-->>E: Validation passed
        E->>E: Prepare new_schema
        E-->>C: Return record batch
    end
Loading

Suggested reviewers

  • nikhilsinhaparseable

Poem

In coding fields I hop with glee,
Tightening checks, as swift as can be.
I nibbled through the schema maze,
Leaving errors lost in a haze.
A joyful hop for code so neat,
Rabbit cheers make our work complete!
🐰✨


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 35ad745 and f159344.

📒 Files selected for processing (1)
  • src/event/format/mod.rs (3 hunks)
🔇 Additional comments (4)
src/event/format/mod.rs (4)

165-165: Logic improved in schema matching check

The code now uses a standalone is_schema_matching function rather than a trait method, which simplifies the API and allows for better testing. This change helps separate concerns and improves the overall code organization.


169-176: Improved control flow for schema handling

The restructuring creates the new schema only after validating that the incoming schema matches the storage schema, which is a logical improvement to the control flow. This ensures we only perform schema updates when necessary.


199-215: Well-documented and simplified schema matching implementation

The standalone function implementation is cleaner with:

  1. Comprehensive documentation explaining the purpose and behavior
  2. Simplified parameter interface (using &[Arc<Field>] instead of Arc<Schema>)
  3. More concise logic using is_none_or to check field compatibility

This refactoring aligns with the PR's objective of improving the schema mismatch check while making the code more maintainable.


361-476: Excellent test coverage for the new implementation

The comprehensive test suite covers all critical scenarios for schema matching:

  • Empty schemas (both sides)
  • Schema type mismatches
  • Missing fields
  • Extra fields in storage
  • Different data types
  • Flag behavior with static_schema_flag

This ensures robust validation of the refactored functionality and prevents future regressions.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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.
  • @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

@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 (1)
src/event/format/mod.rs (1)

174-183: Improved schema validation logic, but consider enhancing the error message.

The refactored code effectively integrates the schema validation directly into the into_recordbatch method, which simplifies the control flow. The use of is_none_or with a predicate is elegant for checking both missing fields and field mismatches in a single operation.

However, the error message "Schema mismatch" is quite generic and doesn't provide details about which fields failed to match, making debugging potentially challenging.

Consider enhancing the error message to include specific field information:

- return Err(anyhow!("Schema mismatch"));
+ let mismatched_fields: Vec<String> = schema
+     .iter()
+     .filter_map(|field| {
+         let name = field.name();
+         storage_schema
+             .get(name)
+             .filter(|storage_field| **storage_field != *field)
+             .map(|_| name.clone())
+     })
+     .collect();
+ return Err(anyhow!("Schema mismatch on fields: {}", mismatched_fields.join(", ")));
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 1afa318 and b6c9e66.

📒 Files selected for processing (1)
  • src/event/format/mod.rs (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (10)
  • GitHub Check: coverage
  • GitHub Check: Quest Smoke and Load Tests for Distributed deployments
  • GitHub Check: Quest Smoke and Load Tests for Standalone deployments
  • GitHub Check: Build Default aarch64-apple-darwin
  • GitHub Check: Build Default x86_64-unknown-linux-gnu
  • GitHub Check: Build Default aarch64-unknown-linux-gnu
  • GitHub Check: Build Default x86_64-apple-darwin
  • GitHub Check: Build Kafka x86_64-unknown-linux-gnu
  • GitHub Check: Build Default x86_64-pc-windows-msvc
  • GitHub Check: Build Kafka aarch64-apple-darwin
🔇 Additional comments (1)
src/event/format/mod.rs (1)

184-185: Good addition of explanatory comment.

The added comment "prepare the record batch and new fields to be added" clearly explains the purpose of this code block, which aids in code readability and maintainability.

coderabbitai[bot]
coderabbitai bot previously approved these changes Mar 21, 2025
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 (1)
src/event/format/mod.rs (1)

174-177: Consider adding more descriptive context to the error message.
By returning a generic "Schema mismatch" error, it might be harder to debug exactly where or why the mismatch occurs. Providing the mismatched field name or type difference in the error could speed up troubleshooting.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between b6c9e66 and 35ad745.

📒 Files selected for processing (1)
  • src/event/format/mod.rs (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: Build Default aarch64-unknown-linux-gnu
  • GitHub Check: Build Kafka x86_64-unknown-linux-gnu
  • GitHub Check: coverage
  • GitHub Check: Quest Smoke and Load Tests for Standalone deployments
  • GitHub Check: Quest Smoke and Load Tests for Distributed deployments
🔇 Additional comments (3)
src/event/format/mod.rs (3)

178-185: Verify ignoring the existing schema during type updates.
Here, existing_schema is passed as None, which means any logic that relies on the actual stored schema for type inferences or timestamp overrides will be skipped. Double-check that this is intentional and doesn't hide mismatch or override opportunities that might be relevant for your schema.


212-229: Great clarity and seamless logic for is_schema_matching.
The inline documentation aligns well with the implementation. The usage of is_none_or succinctly captures the condition where either the storage schema entry is absent or different. Overall, this improves maintainability and readability of the static vs. flexible schema checks.


374-490: Commendable test coverage for is_schema_matching.
The tests thoroughly cover corner cases, including static vs. flexible schema modes, missing fields, type mismatches, and empty schemas. This robust suite will greatly reduce the risk of regressions.

coderabbitai[bot]
coderabbitai bot previously approved these changes Mar 21, 2025
Signed-off-by: Devdutt Shenoi <devdutt@parseable.com>
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