Repository navigation
Improve role base rule rewrite (#24393) - #24792
Conversation
1、修复多角色合并时改写规则不起效的问题 2、多角色起效时,不同角色对同一个库表都有改写规则的时候,如果列一致的情况下,那么做union distinct合并。否则保留最后一个规则 Approved by: @heni02, @XuPeng-SH
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.
发现两个需要先修的阻塞问题:
-
pkg/frontend/rewrite_rule.go:313-330,437-450现在把SELECT */t.*规则视为不可 merge,并在mergeRewriteRules里直接回退到后一个规则;pkg/frontend/rewrite_rule_test.go:703-714也把这个行为写死了。这样同表多角色下最常见的select * ...规则不会按“列一致则合并”的语义生效,而是静默丢掉前一个角色的规则。这里至少需要做 schema-aware merge,或者显式拒绝这类无法安全合并的规则,不能悄悄覆盖。 -
pkg/frontend/rewrite_rule.go:327-330用UNION DISTINCT合并规则,会把基表的 bag semantics 改成 set semantics。因为改写后的 SQL 会直接作为派生表替换原表(pkg/sql/plan/query_builder.go:4851-4866),只要底表存在合法重复行,或者两个角色命中过同一批行,COUNT(*)/ join / 聚合结果都会被去重后改错。这个 merge 方式本身不保语义,需要换成能保留原始行重数的方案,或者拒绝这种 merge。
XuPeng-SH
left a comment
There was a problem hiding this comment.
两个阻塞问题:
-
pkg/frontend/rewrite_rule.go:324-326,452-466现在对select */t.*规则直接判定为不可 merge,然后在mergeRewriteRules里静默回退到后一个规则。这样同表多角色场景下最常见的全列规则不会按“列一致则合并”的语义生效,而是直接丢掉前一个角色的 rewrite。这里至少需要做 schema-aware merge,或者在建规则时显式拒绝这类无法安全合并的规则,不能静默覆盖。 -
pkg/frontend/rewrite_rule.go:367兼容规则现在用UNION ALL合并。只要两个角色的谓词命中同一批底表行,结果就会把同一行重复返回;多角色应该扩大可见集,不应该把重叠行放大成重复元组。现有 BVT 也没有覆盖重叠谓词这个回归面。这里需要改成真正保语义的合并方式,或者识别这类场景并拒绝 merge。
测试角度我没有看到阻塞性的缺口;当前需要先把上面两个语义问题收掉。
1的部分暂不处理。因为涉及星号展开,这个要binder才能准确做到(如果from是相同单表是可以,如果有派生表或者自然连接就会有问题)。目前规则是基于Parser来做合并,暂时比较难覆盖。 2的部分确实存在,但是parser阶段也确实无法判断它们是否物理上的同一行,除非投影必须有PK列才好判断。没有PK的话,重叠可能有正常的情况,也有异常的情况。尝试过加row_id。但是一样存在from子句出来的结果不一定有row_id的问题。 这2个问题确实存在,短时间可能比较难解决。另外建issue优化或者作为使用多角色权限合并的限制项吧 1的部分会使得权限收紧,这个跟产品说明过,只处理能处理的合并规则。 |
# Conflicts: # pkg/sql/plan/function/function_test.go
XuPeng-SH
left a comment
There was a problem hiding this comment.
LGTM after re-reviewing the latest PR state from 3 angles.
- Logic/correctness: the two previous blockers are addressed in the current diff. Same-shape rules (including
select */t.*) are now merged by OR-ing predicates instead of falling back, and overlapping predicates no longer duplicate rows. - Compatibility/behavior: multi-role rule priority is now deterministic,
SET SECONDARY ROLE ALL/NONEinvalidates caches correctly, and invalid rewrite SQL is rejected before overwrite. - Tests: unit + BVT coverage now covers multi-role merge, inherited roles, wildcard merge, incompatible-rule fallback, cache invalidation, and invalid-rule rejection.
Approve.
XuPeng-SH
left a comment
There was a problem hiding this comment.
LGTM after reviewing the current head from 3 angles.
- Logic/correctness: the current head keeps cache invalidation on role switches, preserves deterministic multi-role rule priority, and the latest rewrite_rule.go delta only broadens safe single-table parsing for merge detection.
- Behavior/compatibility: the current implementation merges same-shape rules by OR-ing predicates without reintroducing the earlier wildcard/overlap issues, and SET SECONDARY ROLE ALL/NONE semantics remain aligned with the updated BVT expectations.
- Tests: unit + BVT coverage remains sufficient for the touched rewrite paths; I don't see a new uncovered blocker in the current head.
Approve.
Merge Queue Status
This pull request spent 1 hour 6 seconds in the queue, including 59 minutes 41 seconds running CI. Required conditions to merge
|
1、修复多角色合并时改写规则不起效的问题
2、多角色起效时,不同角色对同一个库表都有改写规则的时候,如果列一致的情况下,那么做union distinct合并。否则保留最后一个规则
Approved by: @heni02, @XuPeng-SH
What type of PR is this?
Which issue(s) this PR fixes:
issue #23271
What this PR does / why we need it:
Improve role base rule rewrite (#24393)