Skip to content

fix: enforce named snapshot object scope - #27134

Merged
mergify[bot] merged 7 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-27116-4cf5f8d5
Aug 14, 2026
Merged

mergify[bot] merged 7 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-27116-4cf5f8d5

Conversation

@gouhongshen

@gouhongshen gouhongshen commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Fixes #27116

What this PR does / why we need it:

Root cause

Named snapshots retain their declared level and object ID in Snapshot.ExtraInfo, but relation planning and selected SHOW paths applied only their timestamp. A table snapshot could therefore read a different table, including through SHOW CREATE. RESTORE TABLE also omitted the target-database check when its snapshot was database-level.

The initial scope enforcement also exposed two internal metadata readers that must retain historical time travel without presenting themselves as a user-selected named snapshot: DATA BRANCH's synthetic privilege query and SHOW RECOVERY WINDOW's mo_catalog.mo_tables scan. Finally, a copy-table ALTER replaces a physical relation while retaining its logical table ID, which is the identity captured by a table snapshot.

Changes

  • Add shared named-snapshot table/database scope validators and a single logical-table-ID helper.
  • Enforce scope for normal table scans, SHOW forms, clone, RESTORE TABLE, and both DATA BRANCH endpoints.
  • Keep internal catalog and privilege reads on the already-resolved MO_TS; validate the actual user-selected relation separately.
  • Reject cross-database database-snapshot restores before restore work begins.
  • Add regression coverage for the reverse DATA BRANCH endpoint and for table/database snapshot SHOW DATABASES boundaries.

Issue-to-test proof

  • test/distributed/cases/snapshot/snapshot_scope.sql covers [Bug]: Snapshot scope is ignored for cross-table reads and database-snapshot table restore #27116's cross-table SELECT and cross-database RESTORE repros, same-object controls, restore failure atomicity, table/database SHOW boundaries, copy-table ALTER logical identity, and DATA BRANCH rejection with the named snapshot on either endpoint.
  • pkg/sql/plan/query_builder_test.go:TestBuildTableRejectsOutOfScopeSnapshot proves an unrelated relation is rejected during named-snapshot planning.
  • pkg/sql/plan/snapshot_scope_test.go covers named-snapshot scope levels, logical-ID selection, table snapshots rejected by SHOW DATABASES, and database-snapshot catalog plans constrained by dat_id.
  • pkg/frontend/data_branch_helpers_test.go:TestValidateDataBranchNamedSnapshotScope proves DATA BRANCH validates the resolved endpoint's logical table identity.
  • pkg/frontend/snapshot_test.go:TestCheckRestorePrivEnforcesDatabaseSnapshotScope proves the database-snapshot restore target check permits its source database and rejects another database.
  • pkg/frontend/show_recovery_window_test.go:TestSearchTablesReadsCatalogAtSnapshotTimestamp proves the internal recovery-window catalog scan uses MO_TS.
  • Existing CI regressions retain coverage for valid DATA BRANCH snapshots across copy-table ALTER and SHOW RECOVERY WINDOW catalog reads.

Tests run

  • Pre-fix isolated reproduction against the reported baseline: table snapshots read another table and database snapshots restored another database's table.
  • make build — PASS.
  • make err-check — PASS.
  • mo-cgo-test -count=1 -timeout=180s ./pkg/sql/plan — PASS.
  • mo-cgo-test -count=1 -timeout=120s -run '^TestBuildShowDatabases(RejectsTableSnapshot|RestrictsDatabaseSnapshot)$' ./pkg/sql/plan — PASS.
  • mo-cgo-test -list '^TestBuildShowDatabases(RejectsTableSnapshot|RestrictsDatabaseSnapshot)$' ./pkg/sql/plan — both tests enumerated.
  • Focused frontend tests for DATA BRANCH, restore scope, and recovery-window catalog reads — PASS.
  • Local standalone MatrixOne BVT, result files manually inspected: snapshot_scope — 54/54; diff_schema_evolve — 278/278; show_recovery_window — 109/109.

CI follow-up

The prior Proxy BVT failure was PR-caused and fixed in the earlier integration commits; the later Proxy and pessimistic BVT jobs passed. The earlier arm64 SCA failure is green after compliant error construction.

The earlier Coverage failure was PR-caused, not the external download outage: workflow 31755103070 attempt 2 completed its coverage-UT producer successfully, then its final Coverage job reported 61/83 changed statements (73.49%), below the 75% threshold. This commit added direct semantic tests for the previously uncovered table-snapshot rejection and database-dat_id filtering branches. The follow-up workflow 31762472675 passed its Coverage job and all required enabled checks.

Residual risks

No catalog migration, fallback path, concurrency behavior, or resource ownership change is introduced. Database snapshots now expose only their declared database through SHOW DATABASES; table snapshots reject database-wide SHOW operations by design.

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

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

Reviewed exact head d43a6e6. Named-snapshot object identity is enforced before execution across ordinary relation planning, SHOW, CLONE, RESTORE TABLE, and both DATA BRANCH endpoints; internal catalog/privilege probes retain timestamp travel without masquerading as the user-selected scoped snapshot. Logical table identity across copy-table ALTER and restore failure atomicity are covered. Full pkg/sql/plan tests, focused frontend tests, the dependent issue regression, vet, and diff checks passed locally.

@mergify mergify Bot added the queued label Aug 14, 2026
@mergify

mergify Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-14 03:59 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks passed · in-place
  • ✅ Merged — 2026-08-14 05:56 UTC · at 5929249cee5780c9efe770cf936b8c170a1d4677 · squash

This pull request spent 1 hour 56 minutes 17 seconds in the queue, including 42 minutes 14 seconds running CI.

Required conditions to merge
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]
  • 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
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Linux/arm64
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • 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 UT Coverage / UT Coverage on Ubuntu/x86
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)

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

Deep review completed on exact head d43a6e6. Named table/database scope is now enforced at the authoritative relation/database identities for ordinary scans, SHOW CREATE/TABLES/DATABASES, CLONE, RESTORE TABLE, and both DATA BRANCH endpoints. The logical-table-ID fallback correctly preserves table-snapshot identity across copy-table ALTER, while internal catalog/privilege probes intentionally switch to the already-resolved MO_TS so they do not impersonate a user-selected object-scoped snapshot. I also traced inherited view snapshots and sibling table binding: only the explicitly named relation is scope-validated, its dependency snapshot remains usable, and the bind-context snapshot is restored before sibling relations.

The restore mismatch is rejected before FK discovery or destructive restore work, database-level SHOW metadata is narrowed by historical dat_id, and table-level database-wide SHOW operations fail closed. Focused frontend and planner scope regressions passed 10/10 with the required local CGo setup, all exact-head CI checks are terminal and green/skipped, and git diff --check is clean. LGTM.

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 size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Snapshot scope is ignored for cross-table reads and database-snapshot table restore

4 participants