Skip to content

[Bug]: view SELECT grant fails after JOIN index scan rewrite #30785

Closed

Description

@aptend

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

v4.2.1

Commit ID

d239386

Other Environment Information

  • Environment: MatrixOne Cloud (MOC) serverless tenant
  • Server: 8.0.30-MatrixOne-v4.2.1
  • Client: PyMySQL over TLS
  • Source: @TrentShen (feedback dated 2026-10-21)

Actual Behavior

A role granted SELECT only on a view can receive error 20101 when querying it. The GRANT succeeds and the role is active. Granting SELECT on the underlying app.vals table makes the same query succeed; revoking that table grant makes it fail again.

The failure is plan-dependent. It reproduces when the view's JOIN is planned with an index lookup on app.vals.idx_attr_id. This was reproduced with synthetic data on the reported v4.2.1 image and on MatrixOne main (baseline commit 838cc78aa1efe46693876a48c05c202979047b7b).

Expected Behavior

SELECT on a view should be authorized by the view grant when the query accesses its base tables through that view. The caller should not need direct SELECT on those base tables.

Steps to Reproduce

Run as a tenant administrator:

CREATE DATABASE app;
CREATE TABLE app.defs (
  attr_id INT PRIMARY KEY,
  attr_key VARCHAR(64) NOT NULL,
  status VARCHAR(16) NOT NULL,
  UNIQUE KEY uk_attr_key (attr_key),
  KEY idx_status (status)
);
CREATE TABLE app.vals (
  id BIGINT PRIMARY KEY,
  employee_id VARCHAR(32) NOT NULL,
  attr_id INT NOT NULL,
  value_text VARCHAR(32),
  UNIQUE KEY uk_emp_attr (employee_id, attr_id),
  KEY idx_attr_id (attr_id)
);
INSERT INTO app.defs VALUES (1, 'team_member', 'ACTIVE'), (2, 'account_type', 'ACTIVE');
INSERT INTO app.vals VALUES (1, 'synthetic', 1, 'yes'), (2, 'synthetic', 2, 'virtual');

CREATE VIEW app.v AS
  SELECT av.employee_id
  FROM app.vals AS av
  JOIN app.defs AS ad ON ad.attr_id = av.attr_id
  WHERE ad.attr_key = 'team_member'
    AND ad.status = 'ACTIVE'
    AND av.value_text = 'yes';

CREATE ROLE reader;
GRANT CONNECT ON ACCOUNT * TO reader;
CREATE USER observer IDENTIFIED BY 'test-password' DEFAULT ROLE reader;
GRANT reader TO observer;
GRANT SELECT ON VIEW app.v TO reader;

Connect as observer with role reader, then run:

SET enable_privilege_cache = off;
SELECT COUNT(*) FROM app.v;

The initial query can succeed. Back as administrator, add enough non-matching rows to make the optimizer choose the index lookup, then flush/analyze the tables:

INSERT INTO app.vals VALUES
  (3, 'other_1', 1, 'no'), (4, 'other_2', 1, 'no'),
  (5, 'other_3', 1, 'no'), (6, 'other_4', 1, 'no'),
  (7, 'other_5', 1, 'no'), (8, 'other_6', 1, 'no'),
  (9, 'other_7', 1, 'no'), (10, 'other_8', 1, 'no'),
  (11, 'other_9', 1, 'no'), (12, 'other_10', 1, 'no'),
  (13, 'other_11', 1, 'no'), (14, 'other_12', 1, 'no'),
  (15, 'other_13', 1, 'no'), (16, 'other_14', 1, 'no'),
  (17, 'other_15', 1, 'no'), (18, 'other_16', 1, 'no'),
  (19, 'other_17', 1, 'no'), (20, 'other_18', 1, 'no'),
  (21, 'other_19', 1, 'no'), (22, 'other_20', 1, 'no');
ANALYZE TABLE app.vals, app.defs;

In a fresh observer session (with privilege cache disabled), repeat SELECT COUNT(*) FROM app.v. It fails with error 20101. Then verify the base-table grant changes the result:

-- As administrator:
GRANT SELECT ON TABLE app.vals TO reader;

-- As observer: the view query succeeds.

-- As administrator:
REVOKE SELECT ON TABLE app.vals FROM reader;

-- As observer: the view query fails again with error 20101.

Additional information

The likely failure path is the JOIN index rewrite: it creates a synthetic table scan for the index lookup without carrying the original view authorization lineage. The frontend then checks the scan as direct access to the base table. The synthetic scan should retain the view authorization context of the source scan.

Production details and customer data are omitted; the reproduction uses synthetic rows.

Activity

  1. added
    kind/bug
    on Oct 22, 2026
  2. added a commit that references this issue on Oct 22, 2026
    6495ce9
  3. XuPeng-SH commented on Oct 22, 2026

    @XuPeng-SH
    Contributor

    QA follow-up for PR #30820, current head 551de35250d6f52eff054db874c7567f70ba0c1c: the planner change now carries the original scan's view chain/direct-view context into the synthetic index scan and index join, with a planner regression and expanded tenant authorization BVT. The author reports related BVT/SQL and package tests passing on this head; I have not independently rerun them. The current head has no commit status checks; the visible connector workflows are unrelated. Issue remains open and assigned to slyunnn, which I left unchanged. Independent exact-head verification/formal re-review is still pending.

  4. slyunnn commented on Oct 23, 2026

    @slyunnn

    Regression and extended verification for #30785 (2026-10-23)

    Verification Environment

    • Latest fetched MatrixOne origin/main: 91639500503a301742e0c84c8aa6c524f1e70792 — docs(skills): add flexible defect-hunt workflow (#30946). Source and running binary identity matched. Runtime: Go 1.27.1, Linux/arm64, TYPECHECK=1.
    • Independently built and tested the exact backport head requested in the QA follow-up: 551de35250d6f52eff054db874c7567f70ba0c1c — [4.2-dev] fix: initialize logical identity before caching table definitions. Runtime: Go 1.26.4, Linux/arm64, TYPECHECK=1. Binary version and SQL git_version() through all endpoints matched 551de35250; a separate fresh data directory was used. This was exact-head verification, not a claim of testing the later 4.2-dev tip.
    • Each version used the full local topology: 2 CN / 1 TN / 1 Log service / Proxy. LOG/TN/CN1 and the actual Proxy module ran in one container; CN2 ran independently. Proxy 127.0.0.1:6001, CN1 127.0.0.1:16001, and CN2 127.0.0.1:16002 were reachable and actually exercised by the full regression matrix on both versions.
    • Clients: PyMySQL over local TCP using text-protocol COM_QUERY, with separate tenant administrator and fresh observer sessions explicitly using role reader; existing BVT executed through the JDBC mo-tester runner.

    What Was Covered

    • OK — Original synthetic schema and view: the initial 2 rows, addition of 20 nonmatching rows, statistics refresh, and the underlying-table grant/revoke sequence. View-only SELECT COUNT(*) returned 1, and the row was exactly synthetic; removing the direct base-table grant did not break view access.
    • OK — Verified actual target-path reachability rather than relying on query success: plans contained INDEX JOIN and Index Table Scan on vals.idx_attr_id. Main selected this naturally with the 22-row fixture. On the fixed backport, flushing only the fixture's two base tables and re-running ANALYZE enabled the natural target path with the same 22 rows. A FORCE INDEX FOR JOIN(idx_attr_id) control independently exercised that path on both versions.
    • OK — Direct SELECT on each base table and direct INSERT remained denied with 20101. Granting and revoking base-table access produced the expected direct-access controls without changing the granted DEFINER view's result.
    • OK — Nested DEFINER access succeeded with only the outer-view grant; direct access to the revoked inner view remained denied. An INVOKER view failed without base privileges, succeeded after granting both base tables, and failed again after their revocation.
    • OK — Revoking a view grant denied access; regranting restored it. Nonmatching/NULL data did not change the qualifying row; an authorized empty-result view returned 0.
    • OK — Repeated the complete matrix with privilege cache OFF and ON through all three endpoints: 132/132 assertions on main and 132/132 on the exact backport head. Endpoint jobs used separate tenants and ran concurrently within each cache phase. Global-cache changes and BVT runs were serialized, and the previous cache setting was restored.
    • OK — TestIndexJoinPreservesScanAuthorization passed independently with -count=1 on both versions. Its typed checks cover direct scans not borrowing an enclosing JOIN's view authority, a DEFINER view without JOIN context, and nested authorization/snapshot context.
    • OK — Replayed the existing grant_view_non_sys.sql with its unchanged expected results twice on the same instance: main 55/55 in each run; exact backport 65/65 in each run. These cases also cover related view grant preservation, replacement and rollback controls. Tenant teardown was checked between runs; there were no skipped, abnormal or failed statements in the accepted runs.
    • OK — Test tenants were removed, endpoints remained healthy, and the original latest-main topology was restored afterward. Harness concurrency is verified; no distributed engine-parallelism claim is made.
    • Not covered — The original unfixed v4.2.1 image was not independently rerun in this task; its reproduction is documented in the issue. Production MOC/serverless deployment, TLS transport, binary prepared statements, long-lived-session privilege invalidation, later 4.2-dev commits, and performance/soak testing were not exercised.

    Findings

    The relevant main fix is PR #30799, fix: preserve view authorization across index joins and replacement, merged as 6495ce997486c2445994081c1d85c39802f7049b and included in the tested main commit.

    The backport is PR #30820, [4.2-dev] fix: preserve view authorization across index joins and replacement, merged as 4b11cd642008a0fd86ee3087d788f9c057b8a11a. Its requested head 551de35250d6f52eff054db874c7567f70ba0c1c was independently built and verified here.

    The synthetic index scan and index JOIN now retain the source scan's view authorization context. Both natural and forced target-index execution preserve view-only access, while direct-table and INVOKER rejection controls remain intact.

    The backport initially chose a base scan for the in-memory fixture even when its cardinality was increased. Completing the report's FLUSH/statistics prerequisite produced the target index path using the original small fixture; those earlier non-target plans were not counted as target-index verification. No product failure remained in the completed acceptance cases.

    MOTR / BVT Coverage

    Persistent planner and tenant authorization regressions are already included in the fixes. They were executed directly, with additional real three-endpoint tenant/role/cache verification. No duplicate MOTR case, new regression commit, push or weekly PR update was needed. No MatrixOne production or test source was modified; build artifacts and temporary verification files were not committed.

    Final Conclusion

    Fixed for #30785 on latest fetched main and the exact 4.2-dev backport head requested by the QA comment. Independent regression verification is complete; this is not a formal GitHub PR review or a claim that production has been upgraded.

    Large-data regression is not required. The defect depends on authorization lineage across a particular index rewrite. The final 22-row fixture, fixture-only FLUSH/ANALYZE on the backport, explicit target-path assertions and a forced-index control cover that boundary; a single added NULL row covers the result boundary. Increasing volume would add cost without proving another authorization contract.

    The issue is left open for maintainer acceptance; no issue-state change was performed.

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

Metadata

Metadata

Assignees

Labels

kind/bugneeds-triage

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions