Skip to content

fix(core): fix the uppercase table name of the model tableReference#1173

Merged
douenergy merged 3 commits intoCanner:mainfrom
goldmedal:fix/table-ref-case-sensitive
Apr 29, 2025
Merged

fix(core): fix the uppercase table name of the model tableReference#1173
douenergy merged 3 commits intoCanner:mainfrom
goldmedal:fix/table-ref-case-sensitive

Conversation

@goldmedal
Copy link
Copy Markdown
Contributor

@goldmedal goldmedal commented Apr 29, 2025

Description

Previously, the table reference in a manifest JSON was case-insensitive. However, it's not expected behavior. This PR fixes the serialization and deserialization behaviors for the table reference.

Summary by CodeRabbit

  • New Features
    • Improved handling and quoting of SQL identifiers, including support for case sensitivity and Unicode characters in table references.
  • Bug Fixes
    • Enhanced serialization and deserialization logic for table references to ensure correct quoting and normalization.
  • Tests
    • Added tests covering quoted, case-sensitive, and Unicode table references to verify correct SQL transformation and JSON serialization.
    • Expanded model serialization tests with complex quoted table references.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 29, 2025

Walkthrough

This update introduces enhanced handling for SQL identifiers and table references within the codebase. A new utility module provides robust parsing, normalization, and quoting of SQL identifiers, leveraging the sqlparser crate, which is now included as a dependency. Serialization and deserialization logic for table references have been improved to handle quoted and case-sensitive identifiers accurately. Additional tests were added to verify correct behavior with quoted, uppercase, and Unicode table references, ensuring that model roundtrips and SQL transformations preserve identifier casing and special characters as expected.

Changes

File(s) Change Summary
wren-core-base/Cargo.toml Added sqlparser crate version 0.55.0 with the "visitor" feature to dependencies.
wren-core-base/src/mdl/builder.rs Added a test case to test_model_roundtrip for quoted identifiers in table_reference, ensuring correct JSON serialization/deserialization.
wren-core-base/src/mdl/manifest.rs Improved table reference (de)serialization to robustly handle quoted identifiers using new parsing utilities; added a test for case-sensitive quoted identifiers.
wren-core-base/src/mdl/mod.rs Introduced a new internal module declaration: mod utils;.
wren-core-base/src/mdl/utils.rs New utility module for parsing, normalizing, and quoting SQL identifiers; provides functions for identifier parsing, normalization (with case handling), and quoting based on SQL rules.
wren-core/core/src/mdl/mod.rs Added two async tests verifying correct transformation of models with uppercase and Unicode table references, ensuring proper quoting and preservation in generated SQL.

Sequence Diagram(s)

sequenceDiagram
    participant Test as Test Case
    participant ModelBuilder as ModelBuilder
    participant Utils as Identifier Utils
    participant Manifest as Manifest (Serde)
    participant SQLTransformer as SQL Transformer

    Test->>ModelBuilder: Create Model with quoted/Unicode/uppercase table_reference
    ModelBuilder->>Manifest: Serialize Model to JSON
    Manifest->>Utils: Normalize and quote identifiers
    Manifest->>Manifest: Deserialize Model from JSON
    Manifest->>Utils: Parse identifiers with case/quote sensitivity
    Test->>SQLTransformer: Analyze and transform SQL query
    SQLTransformer->>Utils: Quote identifiers for SQL generation
    SQLTransformer->>Test: Return transformed SQL with proper quoting/casing
Loading

Suggested reviewers

  • wwwy3y3
  • douenergy

Poem

In the warren of code, where SQL names dwell,
Quoted and cased, they’re handled quite well!
With Unicode hopping and tests in the field,
Each identifier’s quirks are now fully revealed.
So here’s to new parsing, robust and astute—
The models all roundtrip, their forms resolute!
🐇✨


📜 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 2f1df9a and 33913a7.

📒 Files selected for processing (2)
  • wren-core-base/src/mdl/manifest.rs (4 hunks)
  • wren-core-base/src/mdl/utils.rs (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • wren-core-base/src/mdl/utils.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • wren-core-base/src/mdl/manifest.rs
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: cargo test (macos-aarch64)
  • GitHub Check: cargo test (win64)
  • GitHub Check: cargo test (macos)
  • GitHub Check: test
  • GitHub Check: ci
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in 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.

@github-actions github-actions bot added core dependencies Pull requests that update a dependency file rust Pull requests that update Rust code python Pull requests that update Python code labels Apr 29, 2025
@goldmedal goldmedal marked this pull request as ready for review April 29, 2025 07:02
@goldmedal goldmedal requested a review from douenergy April 29, 2025 07:03
Copy link
Copy Markdown
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 (1)
wren-core-base/src/mdl/utils.rs (1)

31-41: Potential error handling improvement opportunity

The function silently converts parsing errors to empty vectors via unwrap_or_default(). This works for the current use case but could hide actual parsing issues.

Consider propagating errors to callers in cases where they need to know about parsing failures:

-pub(crate) fn parse_identifiers_normalized(s: &str, ignore_case: bool) -> Vec<String> {
+pub(crate) fn parse_identifiers_normalized(s: &str, ignore_case: bool) -> Result<Vec<String>, sqlparser::parser::ParserError> {
-    parse_identifiers(s)
-        .unwrap_or_default()
+    parse_identifiers(s).map(|idents| {
+        idents
         .into_iter()
         .map(|id| match id.quote_style {
             Some(_) => id.value,
             None if ignore_case => id.value,
             _ => id.value.to_ascii_lowercase(),
         })
         .collect::<Vec<_>>()
+    })
}

Or add a comment explaining the reasoning behind using unwrap_or_default().

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between e3d24a1 and 2f1df9a.

⛔ Files ignored due to path filters (1)
  • wren-core-py/Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (6)
  • wren-core-base/Cargo.toml (1 hunks)
  • wren-core-base/src/mdl/builder.rs (1 hunks)
  • wren-core-base/src/mdl/manifest.rs (4 hunks)
  • wren-core-base/src/mdl/mod.rs (1 hunks)
  • wren-core-base/src/mdl/utils.rs (1 hunks)
  • wren-core/core/src/mdl/mod.rs (1 hunks)
🔇 Additional comments (12)
wren-core-base/Cargo.toml (1)

16-16: Appropriate addition of the sqlparser dependency

The sqlparser crate with the "visitor" feature will provide robust SQL identifier parsing capabilities, which aligns with the PR objective of fixing case sensitivity in table references.

wren-core-base/src/mdl/utils.rs (3)

24-29: Well-structured SQL identifier parsing function

This function correctly uses sqlparser to parse multipart identifiers from a string input, leveraging the GenericDialect for broad SQL compatibility.


43-49: Efficient use of Cow for zero-copy optimizations

Great use of Cow<str> to avoid unnecessary allocations when quotes aren't needed, and proper escaping of internal quotes by doubling them.


52-63: Accurate SQL identifier quoting logic

The function correctly determines when identifiers need quotes based on SQL standard rules for the first character.

wren-core-base/src/mdl/mod.rs (1)

24-24: Appropriate module declaration

Correctly adds the new utilities module to the mdl namespace.

wren-core-base/src/mdl/builder.rs (1)

485-492: Good test case for quoted table references

This test case effectively verifies that a model with a quoted table reference ("Wren"."Public"."Source") can be properly serialized and deserialized while preserving case sensitivity.

wren-core-base/src/mdl/manifest.rs (4)

116-117: Good addition of utility imports for SQL identifier handling.

The new imports from the utils module provide essential functions for properly handling SQL identifiers, which is critical for the case-sensitivity fix.


138-141: Improved deserializer now properly quotes identifiers.

The deserializer now correctly handles each component of the table reference by:

  1. Filtering out empty strings
  2. Properly quoting each identifier using the quote_identifier function

This ensures that table references maintain proper case sensitivity during deserialization.


154-154: Robust parsing of identifiers during serialization.

Replacing the naive string splitting with parse_identifiers_normalized provides proper handling of quoted identifiers and case sensitivity during serialization. This is a key improvement that fixes the issue with uppercase table names.


323-333: Good test for case-sensitive identifiers.

This test verifies that quoted identifiers with mixed case are correctly serialized, preserving the original casing. It's an essential addition that validates the fix for the case-sensitivity issue.

wren-core/core/src/mdl/mod.rs (2)

1442-1483: Excellent test for uppercase table references.

This test verifies that models with uppercase table names are correctly transformed into SQL queries with proper quoting. It confirms that the changes to the serialization and deserialization process work correctly with uppercase identifiers.

The test correctly validates that:

  1. The manifest with an uppercase table name can be parsed
  2. The SQL transformation correctly quotes the uppercase table name

This is a crucial test for the fix being implemented.


1485-1526: Great test coverage for Unicode table references.

This test extends the validation to include Unicode characters in table and catalog names, ensuring that:

  1. Unicode identifiers are properly handled during serialization/deserialization
  2. The SQL transformation correctly quotes Unicode identifiers

This provides additional confidence in the robustness of the fix for different character sets.

@douenergy douenergy merged commit 55309d0 into Canner:main Apr 29, 2025
14 checks passed
@goldmedal goldmedal deleted the fix/table-ref-case-sensitive branch April 29, 2025 08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core dependencies Pull requests that update a dependency file python Pull requests that update Python code rust Pull requests that update Rust code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants