Skip to content

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

Merged
douenergy merged 4 commits intoCanner:mainfrom
goldmedal:fix/rls-cf
May 13, 2025
Merged

fix(core): ensure that the RLAC can be applied when the relationship access#1190
douenergy merged 4 commits intoCanner:mainfrom
goldmedal:fix/rls-cf

Conversation

@goldmedal
Copy link
Copy Markdown
Contributor

@goldmedal goldmedal commented May 8, 2025

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.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented May 8, 2025

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
Loading

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 d2bde80 and a0b1dfb.

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

@github-actions github-actions bot added core rust Pull requests that update Rust code labels May 8, 2025
@github-actions github-actions bot added the ci label May 8, 2025
@goldmedal goldmedal marked this pull request as ready for review May 9, 2025 01:59
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: 3

🔭 Outside diff range comments (1)
wren-core/core/src/logical_plan/analyze/plan.rs (1)

547-553: ⚠️ Potential issue

Alias uses the parent column name – likely a correctness bug

Each nested calculated source column is currently aliased with the outer qualified_column’s name:

expr.alias(qualified_column.name.clone())

If a top-level calculation depends on multiple other calculated fields, they will all share the same alias, producing name collisions in the logical plan and silently masking one another.

-                    expr.alias(qualified_column.name.clone()),
+                    expr.alias(c.name.clone()),   // c is the current required field

Please update the alias to the actual field being added and add a regression test that ensures distinct aliases are preserved.

🧹 Nitpick comments (4)
.github/workflows/rust.yml (1)

60-66: Consider defining RUST_MIN_STACK once instead of repeating it in every test step

Setting the same environment variable four times makes the workflow noisier and harder to maintain.
You can DRY this up by placing the variable under an env: block at the job (or even defaults.run) level, e.g.:

jobs:
  linux-test:
    env:
      RUST_MIN_STACK: 8388608
    steps:
      

All test invocations will then inherit the value automatically.

Also applies to: 78-82, 90-96, 104-107

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

2080-2180: Large inline manifests/tests are becoming unwieldy – factor out helpers

The newly-added RLAC tests reproduce a full manifest (≈ 80 lines) inline.
As these grow they hurt readability and make future changes harder.

Suggestion: extract manifest construction into a helper like
fn build_rlac_manifest() -> Manifest { … } in tests/utils.rs, then keep each test focused on assertions:

let analyzed_mdl = Arc::new(AnalyzedWrenMDL::analyze(build_rlac_manifest())?);

This keeps the test body < 20 lines and removes duplication across similar scenarios.

wren-core/core/src/logical_plan/analyze/plan.rs (2)

448-461: Duplicate logic in create_partial_calculation()

The same pair of collect_* helpers is called here right after the earlier call inside build().
Because partial_model_required_fields starts empty at this point, the first call already seeds it with all relevant entries; the second call therefore repeats the work and, in the worst case, inserts duplicate aliases.

Refactor to a single call (or a small helper that internally decides which branch is needed) to keep the function side-effect-free and O(n) instead of O(2n).


559-562: Typos in documentation

Small spelling issues make the new comments harder to read:

  • “throguh” → “through”
  • “he partial model” → “the partial model”

Fixing them helps future maintainers.

-/// required fields for the partial model used by another model throguh the relationship.
-/// It collects the non-calculated fields for the he partial model used by another model.
+/// Required fields for the partial model used by another model **through** the relationship.
+/// It collects the non-calculated fields for the **partial** model used by another model.
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c0f888d and d2bde80.

📒 Files selected for processing (4)
  • .github/workflows/rust.yml (4 hunks)
  • wren-core/core/src/logical_plan/analyze/plan.rs (6 hunks)
  • wren-core/core/src/logical_plan/analyze/relation_chain.rs (1 hunks)
  • wren-core/core/src/mdl/mod.rs (3 hunks)
🔇 Additional comments (1)
wren-core/core/src/logical_plan/analyze/plan.rs (1)

199-224:

Details

❌ Incorrect review comment

Potentially redundant triple-collection of required fields

Within the else branch you now invoke three collectors back-to-back (collect_partial_model_plan_for_calculation, collect_partial_model_required_fields, and collect_model_required_fields).
All three eventually populate the same model_required_fields map.
Besides the additional hash look-ups this may generate identical OrdExpr instances differing only by aliasing, leading to bloated sets and unexpected duplicates later on (especially because collect_model_required_fields already covers both calculated and non-calculated columns).

Consider:

-collect_partial_model_plan_for_calculation(...)?;
-collect_partial_model_required_fields(...)?;
-collect_model_required_fields(...)?;
+collect_model_required_fields(...)?;            // one pass is enough
+// If you really need the separation, deduplicate the returned sets before merging.

Stream-lining to a single authoritative collector will simplify reasoning and avoid latent ordering / precedence bugs.


🏁 Script executed:

#!/bin/bash
set -e

echo "Searching for collect_partial_model_plan_for_calculation:"
rg "fn collect_partial_model_plan_for_calculation" -n -A20

echo
echo "Searching for collect_partial_model_required_fields:"
rg "fn collect_partial_model_required_fields" -n -A20

echo
echo "Searching for collect_model_required_fields:"
rg "fn collect_model_required_fields" -n -A20

Length of output: 6086


🏁 Script executed:

#!/bin/bash
set -e

echo "=== collect_partial_model_plan_for_calculation body ==="
sed -n '517,560p' wren-core/core/src/logical_plan/analyze/plan.rs || true

echo
echo "=== collect_partial_model_required_fields body ==="
sed -n '561,601p' wren-core/core/src/logical_plan/analyze/plan.rs || true

echo
echo "=== collect_model_required_fields body ==="
sed -n '602,650p' wren-core/core/src/logical_plan/analyze/plan.rs || true

Length of output: 5633


Ignore redundant-collector suggestion

The three “collector” functions serve distinct purposes and even produce different aliases, so they are not interchangeable:

  • collect_partial_model_plan_for_calculation
    • Only when column.is_calculated and only for building the calculation plan
    • Aliases each expression to the qualified_column name
  • collect_partial_model_required_fields
    • Only when !column.is_calculated and only for building related (partial) models
  • collect_model_required_fields
    • Covers both calculated and non-calculated fields, but for the source model plan
    • Uses the column’s own name for aliasing and includes extra debug logic and dataset checks

Merging these into a single pass would lose the clear separation of calculation‐vs-related-model logic and break the aliasing semantics each downstream planner relies on. Leave the three calls as is.

Likely an incorrect or invalid review comment.

rust-version: stable
- name: Run tests (excluding doctests)
run: cargo test --lib --tests --bins
run: RUST_MIN_STACK=8388608 cargo test --lib --tests --bins
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

To avoid the following test error, add the min stack size of rust:

thread 'mdl::test::test_plan_calculation_without_unnamed_subquery' has overflowed its stack.

@douenergy
Copy link
Copy Markdown
Contributor

Thanks @goldmedal

@douenergy douenergy merged commit 238b28f into Canner:main May 13, 2025
14 checks passed
@goldmedal goldmedal deleted the fix/rls-cf branch May 14, 2025 07:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci core rust Pull requests that update Rust code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants