Skip to content

[Bug]: secondary role rewrite rules collapse duplicate projected values from distinct visible rows #29081

Closed

Description

@slyunnn

Description

When a user enables compatible secondary roles, row rewrite rules must combine at the base-row level with OR semantics. On current main, SET SECONDARY ROLE ALL does not add rows made visible by granted secondary roles: the query still returns only the default role's rows.

This is broader than duplicate-projection handling. The original minimal case, where two independently visible base rows both project to a=1, returns one row/count instead of two. The current direct E2E also shows the non-overlapping base-row union itself is missing.

Environment

  • MatrixOne: main@480b91764a94cf9ab49d3f2b43a60cb63ce677c6
  • Earlier minimal reproduction: main@780ef933f2479aa1679faf13d08850ce6c8f7c2f
  • Deployment: macOS arm64, single-node local isolated MatrixOne service
  • Required session setting: enable_remap_hint = 1

Steps to reproduce

CREATE DATABASE rcac_secondary;
CREATE TABLE rcac_secondary.sales(order_id INT PRIMARY KEY, tenant_id INT);
INSERT INTO rcac_secondary.sales VALUES (1,1), (2,2), (3,3), (4,1);

CREATE ROLE rcac_r1, rcac_r2, rcac_r3;
ALTER ROLE rcac_r1 ADD RULE "select * from rcac_secondary.sales where tenant_id = 1" ON TABLE rcac_secondary.sales;
ALTER ROLE rcac_r2 ADD RULE "select * from rcac_secondary.sales where tenant_id = 2" ON TABLE rcac_secondary.sales;
ALTER ROLE rcac_r3 ADD RULE "select * from rcac_secondary.sales where tenant_id = 3" ON TABLE rcac_secondary.sales;

CREATE USER rcac_secondary_u IDENTIFIED BY '<test-password>' DEFAULT ROLE rcac_r1;
GRANT CONNECT ON ACCOUNT * TO rcac_r1;
GRANT SELECT ON TABLE rcac_secondary.sales TO rcac_r1, rcac_r2, rcac_r3;
GRANT rcac_r2, rcac_r3 TO rcac_secondary_u;

Log in as rcac_secondary_u and execute:

SET enable_remap_hint = 1;
SELECT order_id FROM rcac_secondary.sales ORDER BY order_id;

SET SECONDARY ROLE ALL;
SELECT order_id FROM rcac_secondary.sales ORDER BY order_id;

SET ROLE rcac_r2;
SELECT order_id FROM rcac_secondary.sales ORDER BY order_id;
SET ROLE rcac_r3;
SELECT order_id FROM rcac_secondary.sales ORDER BY order_id;

Actual behavior

default rcac_r1:          1, 4
SET SECONDARY ROLE ALL:   1, 4
SET ROLE rcac_r2:         2
SET ROLE rcac_r3:         3

The explicit-role controls show that the individual rules, grants, and row filters are valid. The failure is specific to the SET SECONDARY ROLE ALL combination path.

The original duplicate-projection reproducer has the same defect class: two roles respectively allow distinct base rows (a=1, marker=1) and (a=1, marker=2), but after SET SECONDARY ROLE ALL, SELECT a, COUNT(*) ... GROUP BY a returns 1 | 1 and SELECT COUNT(*) returns 1; both should be 2.

Expected behavior

SET SECONDARY ROLE ALL should make the visible base-row set equivalent to:

(tenant_id = 1) OR (tenant_id = 2) OR (tenant_id = 3)

The combined query must return 1, 2, 3, 4. Distinct base rows must remain distinct even if a later user projection gives them the same value; no UNION DISTINCT-style collapse may occur before the user's query semantics are applied.

Stability / impact

  • Reproduced directly on current main in an isolated service.
  • Incorrectly omits data authorized by secondary roles, affecting ordinary queries, aggregates, pagination, and multi-role workflows.
  • Existing zz_accesscontrol/role_rule.sql expected output matches the default-role-only result after SET SECONDARY ROLE ALL, so its passing result does not validate the required union semantics.

Evidence and initial scope

  • Single-role filtering and explicit SET ROLE rcac_r2 / SET ROLE rcac_r3 controls pass.
  • Evidence limits the fault to secondary-role rule loading, merging, or application in the runtime rewrite path; root cause is not yet confirmed.
  • This issue is independent of [Bug]: ordinary view bypasses row rewrite rule and exposes hidden rows #29093, where a normal view bypasses a base-table row rule.

Regression coverage

After remediation, add or correct an end-to-end regression that verifies:

  1. two non-overlapping role rules return their base-row union;
  2. three compatible role rules return the full OR union;
  3. overlapping rows occur once by base-row identity; and
  4. two distinct authorized base rows with identical projected values remain two rows/count 2.

Activity

  1. added
    kind/bug
    on Sep 18, 2026
  2. added theissue type on Sep 18, 2026
  3. added this to the MO-202609 milestone on Sep 18, 2026
  4. iamlinjunhong commented on Oct 10, 2026

    @iamlinjunhong
    Contributor

    Investigated against main@e5dd4724f782067e1c76c38c9d2f55cebceda4a9. Merged PR #26734 intentionally changed SET SECONDARY ROLE ALL/NONE to successful, state-preserving no-ops: the current active role stays unchanged, and separately granted roles are not automatically combined. Explicit SET ROLE <role> remains supported.

    The current frontend handler and the secondary_role_disabled regression encode this contract. Consequently, the default-role-only result after SET SECONDARY ROLE ALL in this report is expected under the adopted design; the explicit-role controls are consistent with it.

    We are retaining that contract and stopping this fix without a code change or PR. Restoring secondary-role union would require a separate product decision covering authorization, row-rule merging, DDL ownership, and cache behavior. This investigation does not establish correctness of general duplicate-projection merging or address #29093.

  5. slyunnn commented on Oct 13, 2026

    @slyunnn
    Author

    Regression and extended verification for #29081 (2026-10-13)

    Verification Environment

    • MatrixOne source and runtime: origin/main at 175a645a85994f2c95992d3aa8d6dacb6d9e167f (fix(sql): preserve filtered UNION ALL right branch columns (#30015)). The tested mo-service binary was built from that exact commit and embedded the same main branch and commit ID.
    • Topology: isolated 2 CN / 1 TN / 1 Log service / Proxy deployment.
    • Endpoints: Proxy host endpoint 127.0.0.1:6731 (mapped to Proxy listener 6009), CN1 127.0.0.1:16731 (CN listener 16001), and CN2 127.0.0.1:16732 (CN listener 16002). All three accepted SQL connections and executed the scenario.
    • Client protocol: MySQL text protocol through the MySQL CLI. The issue is session-role SQL behavior and does not require a binary prepared-statement path.
    • Related merged change: 09b361f6bdfb5407f51b6648cc964ac96f639839, #26734 remove secondary role. It intentionally defines SET SECONDARY ROLE ALL and SET SECONDARY ROLE NONE as accepted, state-preserving compatibility no-ops.

    What Was Covered

    • OK — Original row-rule shape: default role issue29081_r1 exposed order_id values 1,4; SET SECONDARY ROLE ALL retained role issue29081_r1 and exposed the same values on Proxy, CN1, and CN2.
    • OK — SET SECONDARY ROLE NONE also retained the active role and the same filtered rows on all three endpoints.
    • OK — Explicit-role control: SET ROLE issue29081_r2 returned 2, and SET ROLE issue29081_r3 returned 3, proving individual role switching remains functional.
    • OK — Unhappy path: the default role was denied access to the separately granted duplicate-projection table with ERROR 20101 (HY000); the same session then completed subsequent role operations successfully.
    • OK — Boundary/control for duplicate projected values: roles that each expose one (a = 1) row with distinct markers returned one row per active role. After SET SECONDARY ROLE ALL, role issue29081_dup1 remained active and still returned only marker 1; explicit SET ROLE issue29081_dup2 returned marker 2.
    • Not covered — A union-of-secondary-roles result such as 1,2,3,4 was not asserted as correct because current MatrixOne deliberately does not implement secondary-role merging.

    Findings

    The reported behavior is reproducible on current main, but it is intentional rather than a remaining implementation defect: ALL and NONE preserve the single active role. The source implementation returns immediately for a secondary-role request, and the existing regression test/distributed/cases/zz_accesscontrol/secondary_role_disabled.sql encodes the same contract, including denied secondary privileges and explicit SET ROLE controls.

    No large-data regression is needed. The behavior depends on session role state and privilege/rule selection, not row count, block or batch boundaries, memory/spill, concurrency, skew, transport, or resource scale. The two-row duplicate-projection control is the minimum data shape that distinguishes the reported claim.

    MOTR / BVT Coverage

    No MOTR branch, recording, replay, commit, or push was needed because no new behavior is required. Existing MatrixOne BVT zz_accesscontrol/secondary_role_disabled already covers the supported no-op contract. The three endpoint sessions were launched in parallel because they used isolated client sessions and read-only verification after setup; SQL within each session remained ordered because role state is session-scoped.

    Final Conclusion

    Out of scope. The expected secondary-role union and associated row-rule merge requested by this issue conflict with the deliberate compatibility contract introduced by merged PR #26734. Restoring that behavior requires a new product decision covering authorization semantics, rule merging, and ownership/cache implications; it is not a regression to fix on current main.

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

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions