wren-engine icon indicating copy to clipboard operation
wren-engine copied to clipboard

fix(core): ensure that the RLAC can be applied when the relationship access

Open goldmedal opened this issue 7 months ago • 1 comments

Description

This PR fixes the issue that the RLAC won't be applied when the relationship accesses the model. For example:

  • Given a model orders with the column customer_name which points to the column name in another model customer through the relationship.
  • If customer has the RLAC rules, the SQL trying to query orders.customer_name should also apply the RLAC rules for the model customer.

Changed

Because we apply the RLAC in ModelPlanNode, we can reuse the logic for the relationship accessing. Previously, we built the to-one relationship access through ModelSourceNode that won't be applied RLAC. This PR will build PartialModelPlanNode instead of ModelSourceNode for the relationship accessing to ensure the RLAC can be used.

Summary by CodeRabbit

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of required fields for partial models, ensuring correct application of row-level access control (RLAC) rules across related models and calculated fields.
    • Enhanced error handling during model plan construction.
    • Simplified model plan construction logic by unifying plan node creation for model targets.
  • Tests

    • Added comprehensive tests to verify RLAC rule application on both base and related models, especially when using calculated fields and relationships.
    • Updated test snapshots for better readability.
  • Chores

    • Updated workflow to set a specific environment variable when running Rust tests on multiple platforms.

goldmedal avatar May 08 '25 10:05 goldmedal

Walkthrough

The changes refine the handling of required fields and partial model plans in the logical plan analysis, separating the collection of calculated and non-calculated fields for partial models. The control flow for building partial model nodes is unified, and error handling is improved. Test code is updated to use clearer manifest definitions and verifies row-level access control in more complex model relationships. The Rust workflow is also updated to set a stack size environment variable during tests.

Changes

Files/Paths Change Summary
.github/workflows/rust.yml Modified the Rust test workflow to set RUST_MIN_STACK=8388608 before running cargo test, ensuring a specific stack size during tests across platforms.
wren-core/core/src/logical_plan/analyze/plan.rs Refactored required field collection: split into separate functions for calculated and non-calculated fields, renamed and updated function signatures, improved error propagation, and adjusted control flow to distinctly handle both field types during model plan building. Minor formatting adjustments included.
wren-core/core/src/logical_plan/analyze/relation_chain.rs Simplified the logic for building model target nodes in relation chains: always wraps a ModelPlanNode in a PartialModelPlanNode, removing conditional logic based on calculated columns.
wren-core/core/src/mdl/mod.rs Updated test code: switched to raw string literals for SQL snapshots, replaced a commented test with an active test using a detailed manifest with two related models, calculated columns, and RLAC rules. The new test checks that RLAC filters are applied to both base and related models when transforming SQL involving calculated fields and relationships.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ModelPlanNodeBuilder
    participant Analyzer
    participant PartialModelCollector

    User->>ModelPlanNodeBuilder: build()
    ModelPlanNodeBuilder->>Analyzer: collect_partial_model_plan_for_calculation()
    Analyzer-->>ModelPlanNodeBuilder: (calculated fields)
    ModelPlanNodeBuilder->>PartialModelCollector: collect_partial_model_required_fields()
    PartialModelCollector-->>ModelPlanNodeBuilder: (non-calculated fields)
    ModelPlanNodeBuilder->>Analyzer: collect_model_required_fields()
    Analyzer-->>ModelPlanNodeBuilder: (all required fields)
    ModelPlanNodeBuilder-->>User: ModelPlanNode

Suggested reviewers

  • wwwy3y3
  • douenergy

Poem

In the warren of code, we burrowed deep,
Dividing fields—calculated, plain, and neat.
Tests now prance with RLAC in tow,
While stack size grows for tests to flow.
Models relate, and rabbits cheer—
For clearer plans are finally here! 🐇✨

[!TIP]

⚡️ Faster reviews with caching
  • CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.

Enjoy the performance boost—your workflow just got faster.


📜 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 d2bde801c89bdd4e55c29c66c23c3ea80d6fed04 and a0b1dfb84abd56bfe443be7b7ee71f5ab168d6c7.

📒 Files selected for processing (2)
  • wren-core/core/src/logical_plan/analyze/plan.rs (7 hunks)
  • wren-core/core/src/logical_plan/analyze/relation_chain.rs (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • wren-core/core/src/logical_plan/analyze/relation_chain.rs
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: cargo test (macos-aarch64)
  • GitHub Check: cargo test (macos)
  • GitHub Check: cargo test (win64)
  • GitHub Check: cargo check
  • GitHub Check: ci
  • GitHub Check: test
🔇 Additional comments (4)
wren-core/core/src/logical_plan/analyze/plan.rs (4)

202-216: Code structure improvement: logical separation of field collection.

The refactoring to split the field collection into separate functions improves code organization. The change creates a clear distinction between collecting calculated fields (collect_partial_model_plan_for_calculation) and non-calculated fields (collect_partial_model_required_fields), which helps enforce RLAC properly when accessing related models.


560-600: Improved error handling in new function.

The implementation of collect_partial_model_required_fields properly handles errors with ok_or_else instead of unwrap(), which is an improvement over previous patterns. This function is crucial for enforcing RLAC on related models by ensuring non-calculated fields are properly collected.


602-610: Better function interface for collecting required fields.

The updated function signature with explicit qualified_column parameter makes the function interface clearer and aligns with the same pattern used in other field collection functions. The comments also clarify that it collects both calculated and non-calculated source columns depending on context.


10-11: Added import for better error handling.

The addition of internal_datafusion_err to the imports supports the improved error handling approach used in the refactored functions.

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

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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.

coderabbitai[bot] avatar May 08 '25 10:05 coderabbitai[bot]

Thanks @goldmedal

douenergy avatar May 13 '25 08:05 douenergy