Skip to content

Improve role base rule rewrite (#24393) - #24792

Merged
mergify[bot] merged 20 commits into
matrixorigin:4.0-devfrom
ouyuanning:cp-24393-to-4.0-dev
Jun 4, 2026
Merged

mergify[bot] merged 20 commits into
matrixorigin:4.0-devfrom
ouyuanning:cp-24393-to-4.0-dev

Conversation

@ouyuanning

@ouyuanning ouyuanning commented Jun 2, 2026 •

Copy link
Copy Markdown
Contributor

1、修复多角色合并时改写规则不起效的问题
2、多角色起效时,不同角色对同一个库表都有改写规则的时候,如果列一致的情况下,那么做union distinct合并。否则保留最后一个规则

Approved by: @heni02, @XuPeng-SH

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:

Improve role base rule rewrite (#24393)

1、修复多角色合并时改写规则不起效的问题
2、多角色起效时,不同角色对同一个库表都有改写规则的时候,如果列一致的情况下,那么做union distinct合并。否则保留最后一个规则

Approved by: @heni02, @XuPeng-SH
@ouyuanning
ouyuanning requested a review from XuPeng-SH as a code owner June 2, 2026 12:14
@ouyuanning
ouyuanning requested a review from a user June 2, 2026 12:14
@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 requested a review from aunjgr as a code owner June 2, 2026 12:32

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

发现两个需要先修的阻塞问题:

  1. 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,或者显式拒绝这类无法安全合并的规则,不能悄悄覆盖。

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

两个阻塞问题:

  1. pkg/frontend/rewrite_rule.go:324-326,452-466 现在对 select * / t.* 规则直接判定为不可 merge,然后在 mergeRewriteRules 里静默回退到后一个规则。这样同表多角色场景下最常见的全列规则不会按“列一致则合并”的语义生效,而是直接丢掉前一个角色的 rewrite。这里至少需要做 schema-aware merge,或者在建规则时显式拒绝这类无法安全合并的规则,不能静默覆盖。

  2. pkg/frontend/rewrite_rule.go:367 兼容规则现在用 UNION ALL 合并。只要两个角色的谓词命中同一批底表行,结果就会把同一行重复返回;多角色应该扩大可见集,不应该把重叠行放大成重复元组。现有 BVT 也没有覆盖重叠谓词这个回归面。这里需要改成真正保语义的合并方式,或者识别这类场景并拒绝 merge。

测试角度我没有看到阻塞性的缺口;当前需要先把上面两个语义问题收掉。

@ouyuanning

ouyuanning commented Jun 3, 2026 •

Copy link
Copy Markdown
Contributor Author

两个阻塞问题:

  1. pkg/frontend/rewrite_rule.go:324-326,452-466 现在对 select * / t.* 规则直接判定为不可 merge,然后在 mergeRewriteRules 里静默回退到后一个规则。这样同表多角色场景下最常见的全列规则不会按“列一致则合并”的语义生效,而是直接丢掉前一个角色的 rewrite。这里至少需要做 schema-aware merge,或者在建规则时显式拒绝这类无法安全合并的规则,不能静默覆盖。
  2. 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的部分会使得权限收紧,这个跟产品说明过,只处理能处理的合并规则。
1的部分会使得行数变多,但是数据是用户有权限的数据

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

LGTM after re-reviewing the latest PR state from 3 angles.

  1. 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.
  2. Compatibility/behavior: multi-role rule priority is now deterministic, SET SECONDARY ROLE ALL/NONE invalidates caches correctly, and invalid rewrite SQL is rejected before overwrite.
  3. 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 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.

LGTM after reviewing the current head from 3 angles.

  1. 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.
  2. 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.
  3. Tests: unit + BVT coverage remains sufficient for the touched rewrite paths; I don't see a new uncovered blocker in the current head.

Approve.

@mergify

mergify Bot commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-06-04 06:04 UTC · Rule: release-4.0
  • ✅ Checks passed · in-place
  • ✅ Merged — 2026-06-04 07:04 UTC · at 2859b84fdaa0a362f60d9636478d9afe5a479ea8 · squash

This pull request spent 1 hour 6 seconds in the queue, including 59 minutes 41 seconds running CI.

Required conditions to merge
  • #approved-reviews-by >= 1 [🛡 GitHub branch protection]
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-decision = APPROVED [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-neutral = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-skipped = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Ubuntu/x86
    • check-neutral = Matrixone CI / SCA Test on Ubuntu/x86
    • check-skipped = Matrixone CI / SCA Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-neutral = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-skipped = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86

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/bug Something isn't working kind/feature size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants