Skip to content

fix(frontend): align role rule merge semantics - #24929

Merged
mergify[bot] merged 5 commits into
matrixorigin:mainfrom
ouyuanning:improve-mo-from-24792
Jun 12, 2026
Merged

mergify[bot] merged 5 commits into
matrixorigin:mainfrom
ouyuanning:improve-mo-from-24792

Conversation

@ouyuanning

@ouyuanning ouyuanning commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #23271

What this PR does / why we need it:

This PR ports the latest role rewrite-rule and SET ROLE behavior from #24792 to the current branch.

Changes:

  • Merge same-name role rewrite rules as a source-row visibility union for active roles. Compatible simple single-table SELECT rewrites are merged with WHERE (...) OR (...).
  • Clarify that this is intentionally not a UNION DISTINCT optimization for partial projections. If two visible base rows project to the same value, both rows remain visible.
  • Keep row-wise projection expressions mergeable, such as select a + 1 as b ..., while aggregate/window/table-function and other non-compatible shapes still fall back to the later rule.
  • Keep the current primary role unchanged for SET SECONDARY ROLE ALL; it only enables secondary roles.
  • After SET ROLE, switch the current role to the target role and invalidate the privilege/rule cache so subsequent privilege checks use the new role.
  • Update frontend unit tests and distributed access-control cases, including duplicate projected values and row-wise expression projection coverage.

Review note:

  • The partial-projection behavior is a deliberate semantic choice for role rules: multi-role visibility is based on base rows, not deduplicated projected result sets. The new BVT case uses two distinct visible rows that both project to a = 1 and expects count(*) = 2 after SET SECONDARY ROLE ALL.

Validation:

  • gofmt -w pkg/frontend/rewrite_rule.go pkg/frontend/rewrite_rule_test.go
  • git diff --check
  • go test ./pkg/frontend -run 'TestMergeRewriteRules|TestRewriteRuleMergeShapeForRule|TestLoadRuleCacheIncludesSecondaryRoles|TestRewriteSQLPropagatesRuleCacheLoadError|TestValidateRewriteRuleSQL' could not complete locally because vendor metadata is inconsistent with go.mod.
  • go test -mod=mod ./pkg/frontend -run 'TestMergeRewriteRules|TestRewriteRuleMergeShapeForRule|TestLoadRuleCacheIncludesSecondaryRoles|TestRewriteSQLPropagatesRuleCacheLoadError|TestValidateRewriteRuleSQL' reached CGO compilation but could not complete locally because usearch.h and xxhash.h are missing.
  • The role_rule.sql mo-tester case was updated, but not run locally because no MatrixOne server is listening on the configured 127.0.0.1:6001.

@ouyuanning
ouyuanning requested a review from XuPeng-SH as a code owner June 11, 2026 04:03
@ouyuanning
ouyuanning requested a review from a user June 11, 2026 04:03
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@ouyuanning ouyuanning changed the title fix(frontend): improve role rule merge and set role behavior fix(frontend): align role rule merge semantics Jun 11, 2026

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I found two blocking regressions in the frontend changes.

  1. pkg/frontend/rewrite_rule.go:428-446 narrows mergeable rewrite rules to bare column / * projections only. That makes identical row-preserving projections like select a + 1 as b from db1.t1 where ... stop merging, so the later role silently overwrites the earlier one instead of unioning both roles' visibility. The new expectation in pkg/frontend/rewrite_rule_test.go:777-779 already demonstrates this regression.

  2. pkg/frontend/authenticate.go:3503-3531 stops rebinding the current role during SET SECONDARY ROLE ALL, but the privilege/rule loaders still seed from tenant.GetDefaultRoleID() unconditionally (pkg/frontend/rewrite_rule.go:223-239, pkg/frontend/authenticate.go:7273-7287, 8688-8704). If the session's current primary role was revoked after login, SET SECONDARY ROLE ALL now keeps that stale role active and reloads caches with it, which is a privilege-revocation regression. Please either validate/fallback the primary role here or stop treating DefaultRoleID as implicitly valid downstream.

@mergify
mergify Bot merged commit 7d31835 into matrixorigin:main Jun 12, 2026
23 of 24 checks passed
@mergify mergify Bot removed the queued label Jun 12, 2026
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
This PR ports the latest role rewrite-rule and SET ROLE behavior from matrixorigin#24792 to the current branch.

Changes:
- Merge same-name role rewrite rules as a source-row visibility union for active roles. Compatible simple single-table SELECT rewrites are merged with `WHERE (...) OR (...)`.
- Clarify that this is intentionally not a `UNION DISTINCT` optimization for partial projections. If two visible base rows project to the same value, both rows remain visible.
- Keep row-wise projection expressions mergeable, such as `select a + 1 as b ...`, while aggregate/window/table-function and other non-compatible shapes still fall back to the later rule.
- Keep the current primary role unchanged for `SET SECONDARY ROLE ALL`; it only enables secondary roles.
- After `SET ROLE`, switch the current role to the target role and invalidate the privilege/rule cache so subsequent privilege checks use the new role.
- Update frontend unit tests and distributed access-control cases, including duplicate projected values and row-wise expression projection coverage.

Review note:
- The partial-projection behavior is a deliberate semantic choice for role rules: multi-role visibility is based on base rows, not deduplicated projected result sets. The new BVT case uses two distinct visible rows that both project to `a = 1` and expects `count(*) = 2` after `SET SECONDARY ROLE ALL`.

Validation:
- `gofmt -w pkg/frontend/rewrite_rule.go pkg/frontend/rewrite_rule_test.go`
- `git diff --check`
- `go test ./pkg/frontend -run 'TestMergeRewriteRules|TestRewriteRuleMergeShapeForRule|TestLoadRuleCacheIncludesSecondaryRoles|TestRewriteSQLPropagatesRuleCacheLoadError|TestValidateRewriteRuleSQL'` could not complete locally because vendor metadata is inconsistent with `go.mod`.
- `go test -mod=mod ./pkg/frontend -run 'TestMergeRewriteRules|TestRewriteRuleMergeShapeForRule|TestLoadRuleCacheIncludesSecondaryRoles|TestRewriteSQLPropagatesRuleCacheLoadError|TestValidateRewriteRuleSQL'` reached CGO compilation but could not complete locally because `usearch.h` and `xxhash.h` are missing.
- The `role_rule.sql` mo-tester case was updated, but not run locally because no MatrixOne server is listening on the configured `127.0.0.1:6001`.

Approved by: @heni02, @XuPeng-SH
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
This PR ports the latest role rewrite-rule and SET ROLE behavior from matrixorigin#24792 to the current branch.

Changes:
- Merge same-name role rewrite rules as a source-row visibility union for active roles. Compatible simple single-table SELECT rewrites are merged with `WHERE (...) OR (...)`.
- Clarify that this is intentionally not a `UNION DISTINCT` optimization for partial projections. If two visible base rows project to the same value, both rows remain visible.
- Keep row-wise projection expressions mergeable, such as `select a + 1 as b ...`, while aggregate/window/table-function and other non-compatible shapes still fall back to the later rule.
- Keep the current primary role unchanged for `SET SECONDARY ROLE ALL`; it only enables secondary roles.
- After `SET ROLE`, switch the current role to the target role and invalidate the privilege/rule cache so subsequent privilege checks use the new role.
- Update frontend unit tests and distributed access-control cases, including duplicate projected values and row-wise expression projection coverage.

Review note:
- The partial-projection behavior is a deliberate semantic choice for role rules: multi-role visibility is based on base rows, not deduplicated projected result sets. The new BVT case uses two distinct visible rows that both project to `a = 1` and expects `count(*) = 2` after `SET SECONDARY ROLE ALL`.

Validation:
- `gofmt -w pkg/frontend/rewrite_rule.go pkg/frontend/rewrite_rule_test.go`
- `git diff --check`
- `go test ./pkg/frontend -run 'TestMergeRewriteRules|TestRewriteRuleMergeShapeForRule|TestLoadRuleCacheIncludesSecondaryRoles|TestRewriteSQLPropagatesRuleCacheLoadError|TestValidateRewriteRuleSQL'` could not complete locally because vendor metadata is inconsistent with `go.mod`.
- `go test -mod=mod ./pkg/frontend -run 'TestMergeRewriteRules|TestRewriteRuleMergeShapeForRule|TestLoadRuleCacheIncludesSecondaryRoles|TestRewriteSQLPropagatesRuleCacheLoadError|TestValidateRewriteRuleSQL'` reached CGO compilation but could not complete locally because `usearch.h` and `xxhash.h` are missing.
- The `role_rule.sql` mo-tester case was updated, but not run locally because no MatrixOne server is listening on the configured `127.0.0.1:6001`.

Approved by: @heni02, @XuPeng-SH
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
This PR ports the latest role rewrite-rule and SET ROLE behavior from matrixorigin#24792 to the current branch.

Changes:
- Merge same-name role rewrite rules as a source-row visibility union for active roles. Compatible simple single-table SELECT rewrites are merged with `WHERE (...) OR (...)`.
- Clarify that this is intentionally not a `UNION DISTINCT` optimization for partial projections. If two visible base rows project to the same value, both rows remain visible.
- Keep row-wise projection expressions mergeable, such as `select a + 1 as b ...`, while aggregate/window/table-function and other non-compatible shapes still fall back to the later rule.
- Keep the current primary role unchanged for `SET SECONDARY ROLE ALL`; it only enables secondary roles.
- After `SET ROLE`, switch the current role to the target role and invalidate the privilege/rule cache so subsequent privilege checks use the new role.
- Update frontend unit tests and distributed access-control cases, including duplicate projected values and row-wise expression projection coverage.

Review note:
- The partial-projection behavior is a deliberate semantic choice for role rules: multi-role visibility is based on base rows, not deduplicated projected result sets. The new BVT case uses two distinct visible rows that both project to `a = 1` and expects `count(*) = 2` after `SET SECONDARY ROLE ALL`.

Validation:
- `gofmt -w pkg/frontend/rewrite_rule.go pkg/frontend/rewrite_rule_test.go`
- `git diff --check`
- `go test ./pkg/frontend -run 'TestMergeRewriteRules|TestRewriteRuleMergeShapeForRule|TestLoadRuleCacheIncludesSecondaryRoles|TestRewriteSQLPropagatesRuleCacheLoadError|TestValidateRewriteRuleSQL'` could not complete locally because vendor metadata is inconsistent with `go.mod`.
- `go test -mod=mod ./pkg/frontend -run 'TestMergeRewriteRules|TestRewriteRuleMergeShapeForRule|TestLoadRuleCacheIncludesSecondaryRoles|TestRewriteSQLPropagatesRuleCacheLoadError|TestValidateRewriteRuleSQL'` reached CGO compilation but could not complete locally because `usearch.h` and `xxhash.h` are missing.
- The `role_rule.sql` mo-tester case was updated, but not run locally because no MatrixOne server is listening on the configured `127.0.0.1:6001`.

Approved by: @heni02, @XuPeng-SH
XuPeng-SH pushed a commit that referenced this pull request Nov 19, 2026
… rules (#32890)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

Fixes matrixorigin/matrixflow#22006

## What this PR does / why we need it:

On `4.1.6-hotfix`, merging two compatible role rules rebuilds their
SELECT with an OR predicate. The default formatter emits decoded string
values without SQL quotes. When the rewrite hint is read back, a value
such as `'0001'` can become a number, and other values can make the rule
fail to parse.

Use the existing single-quoted SQL literal formatter only when emitting
the merged SELECT. Rule eligibility, fallback, cache invalidation and
the source-row union from #24929 are unchanged.

The unit test compares reparsed trees for Unicode, zero-padded strings
in a subquery, an embedded quote and a backslash. It failed at reparse
before the fix and passes after it. The role-rule BVT now distinguishes
`'0001'` from `'1'`: one role sees one row; two roles see both matching
rows even though their projections are equal. Re-saving the rule and
querying from a new session produces the same result.

Local validation: full `pkg/frontend` unit tests, `go vet`, incremental
golangci-lint, and `zz_accesscontrol/role_rule.sql` via mo-tester
(335/335 passed).

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants