Repository navigation
fix(frontend): align role rule merge semantics - #24929
Conversation
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
XuPeng-SH
left a comment
There was a problem hiding this comment.
I found two blocking regressions in the frontend changes.
-
pkg/frontend/rewrite_rule.go:428-446narrows mergeable rewrite rules to bare column /*projections only. That makes identical row-preserving projections likeselect 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 inpkg/frontend/rewrite_rule_test.go:777-779already demonstrates this regression. -
pkg/frontend/authenticate.go:3503-3531stops rebinding the current role duringSET SECONDARY ROLE ALL, but the privilege/rule loaders still seed fromtenant.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 ALLnow 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 treatingDefaultRoleIDas implicitly valid downstream.
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
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
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
… 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).
What type of PR is this?
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:
WHERE (...) OR (...).UNION DISTINCToptimization for partial projections. If two visible base rows project to the same value, both rows remain visible.select a + 1 as b ..., while aggregate/window/table-function and other non-compatible shapes still fall back to the later rule.SET SECONDARY ROLE ALL; it only enables secondary roles.SET ROLE, switch the current role to the target role and invalidate the privilege/rule cache so subsequent privilege checks use the new role.Review note:
a = 1and expectscount(*) = 2afterSET SECONDARY ROLE ALL.Validation:
gofmt -w pkg/frontend/rewrite_rule.go pkg/frontend/rewrite_rule_test.gogit diff --checkgo test ./pkg/frontend -run 'TestMergeRewriteRules|TestRewriteRuleMergeShapeForRule|TestLoadRuleCacheIncludesSecondaryRoles|TestRewriteSQLPropagatesRuleCacheLoadError|TestValidateRewriteRuleSQL'could not complete locally because vendor metadata is inconsistent withgo.mod.go test -mod=mod ./pkg/frontend -run 'TestMergeRewriteRules|TestRewriteRuleMergeShapeForRule|TestLoadRuleCacheIncludesSecondaryRoles|TestRewriteSQLPropagatesRuleCacheLoadError|TestValidateRewriteRuleSQL'reached CGO compilation but could not complete locally becauseusearch.handxxhash.hare missing.role_rule.sqlmo-tester case was updated, but not run locally because no MatrixOne server is listening on the configured127.0.0.1:6001.