Skip to content

Apply role rewrite policies to EXPLAIN statements - #33160

Merged
XuPeng-SH merged 6 commits into
matrixorigin:mainfrom
iamlinjunhong:codex/loom/develop/550cabbb86026a078b7112eaaed1390647cb07e500f66bb191f39529fc6b8656
Nov 30, 2026
Merged

XuPeng-SH merged 6 commits into
matrixorigin:mainfrom
iamlinjunhong:codex/loom/develop/550cabbb86026a078b7112eaaed1390647cb07e500f66bb191f39529fc6b8656

Conversation

@iamlinjunhong

@iamlinjunhong iamlinjunhong commented Nov 24, 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:

What this PR does / why we need it:

Apply the session's effective role rewrite policy to the statement inside EXPLAIN, EXPLAIN ANALYZE, and EXPLAIN PHYPLAN. The parser passes the validated policy to the enclosed query's read sources through the existing rewrite path. EXPLAIN ANALYZE displays the authorized plan. With mandatory role rules active, EXPLAIN ANALYZE and EXPLAIN PHYPLAN keep pre-policy operator statistics out of the client response and report the final SELECT row count. Policy loading errors still terminate the request before planning or execution. EXPLAIN PHYPLAN also visits the wrapped statement during database remapping, keeping the role-rule lookup aligned with the destination database.

Local validation on macOS/arm64 covered the parser's EXPLAIN variants, adjacent prepared-statement paths, and frontend policy-error handling. A single-CN MySQL text-protocol test used a synthetic role rule to assert the ordinary SELECT result, the authorized scan in EXPLAIN ANALYZE, and the root node's actual output count. The test also passed twice on the same fixture. Incremental go vet and lint passed.

The rewrite and output rules operate at statement scope. Local integration coverage was single-CN. The previous head passed the required CI run.

Validation for 67461fa640435848bea5a970b72873967e7676e3 on macOS/arm64

  • The focused MySQL text-protocol regression reproduced the incorrect destination scan before the fix. After the fix, mo-cgo-test -count=2 -run '^TestExplainAnalyzeCarriesRoleRule$' ./pkg/tests/sqlintegration passed twice using the same single-CN fixture. The remapped PHYPLAN ANALYZE scanned the authorized source and reported the same output row count as SELECT.
  • Focused mo-cgo-test checks passed in pkg/sql/parsers and pkg/frontend for rewrite hints, database remapping, prepared EXPLAIN, and policy-loading errors. Incremental go vet, golangci-lint, and the PR artifact check passed.
  • Local integration coverage for the new regression was single-CN. That head passed required CI, including the multi-CN BVT jobs. CI run

Validation for 68a38525ffa6758020d8fb240ccdd7883ef87ee1, rebased onto main@b6f4ca00abd9f8cb9aa5e72d1be830238df22264

  • mo-cgo-test -count=2 -run '^TestExplainAnalyzeCarriesRoleRule$' ./pkg/tests/sqlintegration passed twice on a single CN. The same-table regression checks authorized and zero-row results, adds denied rows, verifies text and native prepared EXPLAIN, and confirms that unrestricted execution still reports operator stats.
  • Focused mo-cgo-test checks in pkg/sql/parsers, pkg/frontend, and pkg/sql/plan/explain passed, including upstream SHOW rewrite attachment and the EXPLAIN rewrite cases. Incremental go vet, golangci-lint, and the PR artifact check passed. That head passed required CI, including both multi-CN BVT jobs. CI run

Validation for a56a88322c3c864165805d67b89dcc61c493a2de, rebased onto main@56989d8ecd07544231bbbde6d1d87552f5a91f20

  • The parser conflict resolution preserves the upstream UPDATE and DELETE role policies and the EXPLAIN read-source rewrite. Focused parser, frontend, EXPLAIN formatter, and planner tests passed with mo-cgo-test.
  • mo-cgo-test -run '^(TestExplainAnalyzeCarriesRoleRule|TestRoleRuleDMLAndIndexVisibility)$' ./pkg/tests/sqlintegration passed on one CN. This checks the EXPLAIN regression, including remapping and native prepared execution, alongside the upstream DML and index-metadata behavior.
  • Incremental go vet, golangci-lint, and the PR artifact check passed. That head passed required CI, including both multi-CN BVT jobs. CI run

Validation for 223c2ae4b2a5b5a3cf951ee6f3ceb1e7804ce4c3

  • Both protected EXPLAIN ANALYZE variants use the planner's main output step for the final SELECT row count. The correlated regression reproduced the old count of zero and now matches the two rows returned by SELECT. mo-cgo-test -count=2 -run '^TestExplainAnalyzeCarriesRoleRule$' ./pkg/tests/sqlintegration passed twice on one CN.
  • The pkg/sql/plan/explain and pkg/pb/plan package tests, incremental go vet, golangci-lint, and the PR artifact check passed. Hosted CI for this commit is pending.

BVT validation for 31af97bcc2e0779119d779395d4ed47a271a1a48

  • Extended test/distributed/cases/zz_accesscontrol/remap_role_rule.sql to compare authorized SELECT results with protected logical and physical EXPLAIN ANALYZE output after database remapping.
  • mo-tester 309a0ca650e0c55e90292767fd61e4976818d66d passed 59/59 statements twice in normal comparison mode with nometa and ignore, on the same single-CN instance. Verified teardown after both runs. The PR artifact check passed.
  • This commit changes the BVT fixture and expected results. Required CI passed on its parent, including both multi-CN BVT jobs. Parent CI run

Validation for ed993e4cd2ece78920cef8c8bef6dbf3b77762d1 on macOS/arm64

  • A native prepared EXPLAIN regression reproduced outputRows=2 when SELECT returned one row. After the fix, both protected ANALYZE variants match SELECT as the session row limit changes on the same prepared handle (2, 1, 0, 2), and the output remains redacted. mo-cgo-test -count=2 -run '^TestExplainAnalyzeCarriesRoleRule$' ./pkg/tests/sqlintegration passed on one CN.
  • Focused EXPLAIN and session-limit tests passed in pkg/frontend, pkg/sql/plan/explain, and pkg/sql/compile. Incremental go vet, golangci-lint, and the PR artifact check passed.
  • Re-ran the remapped-role BVT with mo-tester 309a0ca650e0c55e90292767fd61e4976818d66d in normal comparison mode with nometa and ignore. Both runs passed 59/59 on the same single-CN instance, with cleanup verified after each run. Hosted CI passed on the previous head.

Validation for 52b7f34a8d0ebbddb61fcb9f1c9e41aee10bd5fa, rebased onto main at f1d7f1e4e8b3d0a66ac8516cf571a0a19d64cc08

  • The Mergify branch update failed when its App tried to bring an upstream workflow update into the PR branch. Replayed the six reviewed commits onto official main. git range-diff matched all six patches, and the workflow files match main.
  • The first replay at d73dae9ae5e8ec00c42dd5dcd29ee42b558e4371 used macOS/arm64. mo-cgo-test -count=2 -run '^TestExplainAnalyzeCarriesRoleRule$' ./pkg/tests/sqlintegration passed on one CN. Focused EXPLAIN, role rewrite, remap, and session-limit checks passed in frontend, parser, EXPLAIN formatter, and compiler packages. The upstream native prepared SHOW integration test passed with -tags integration.
  • The remapped-role BVT passed 59/59 twice on the same single-CN instance using mo-tester normal comparison mode with nometa and ignore. Verified fixture cleanup after both runs. Incremental go vet, golangci-lint, and the PR artifact guard passed.
  • The final replay incorporates the upstream allocation-profiling update. Its six PR patches match the first replay. Re-ran the single-CN EXPLAIN integration regression on the final replay and it passed. The other results above are reused for the identical PR patches.
  • The pre-rebase head passed required CI, including the multi-CN BVT jobs. CI attempt 2

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

@mergify

mergify Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Queue requested — the merge queue status continues in this comment ↓.

@XuPeng-SH

XuPeng-SH commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: 388292584568a6c6b575944633ceb442397af063

Review completed. Read the assessment.

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

mo-bot review · mo-bot-local · review

Commit: 388292584568a6c6b575944633ceb442397af063

Assessment: request_changes

The parser reuses the existing role-policy path for EXPLAIN ANALYZE. EXPLAIN PHYPLAN still needs to pass its wrapped statement through the database-remap visitor.

Model: gpt-6.2-sol / xhigh

Session: 01a20d4a-e012-78c2-a9c3-fb5e273c4075 · slot 1 · generation 4

  • P1 pkg/sql/parsers/sqlparse.go:784: EXPLAIN PHYPLAN reads an unqualified table when the session remaps its default database and the role rule is keyed to the source database.
    Impact: PHYPLAN can execute against the destination table without its effective role filter. Its statistics can include rows filtered out by the equivalent SELECT.
    Evidence: The new branch attaches the source-keyed rule, but pkg/frontend/remap_db.go:322 visits only plain EXPLAIN and EXPLAIN ANALYZE. pkg/frontend/compiler_context.go:287 still supplies the remapped default database, so the rewrite lookup at pkg/sql/plan/query_builder.go:13963 misses the source-keyed rule. The existing contract is covered by test/distributed/cases/zz_accesscontrol/remap_role_rule.sql:37. Route PHYPLAN through the same remap visitor and add regression coverage for role-rule parity under remapping.

Validation:

  • Isolation preflight and git diff --check passed on the frozen head. Receipts are checks/1795519547080181007-2.json and checks/1795519779601145828-2.json.

  • The frozen CI snapshot records successful Ubuntu/x86 UT and multi-CN BVT jobs. The PHYPLAN finding was verified by tracing the parser, remapper, and planner.

Limitations:

  • Local product tests did not run. The parser check timed out during dependency downloads. The initial integration run was interrupted before its receipt was written, and the cache-only retry stopped at missing dependencies. See checks/1795519931691727350-2.json, parser.stderr, checks/1795519967776809699-2.json, and sqlintegration-cache.jsonl.

  • The frozen CI snapshot contains job results only, so individual regression-test execution could not be confirmed from CI.

@XuPeng-SH

XuPeng-SH commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: 67461fa640435848bea5a970b72873967e7676e3

Review completed. Read the assessment.

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

mo-bot review · mo-bot-local · review

Commit: 67461fa640435848bea5a970b72873967e7676e3

Assessment: request_changes

The PR reuses the ordinary SELECT rewrite path and resolves the prior PHYPLAN database-remap finding. Issue #31675 remains open at the EXPLAIN output boundary because scan input counts can still include rows denied by the role rule.

Model: gpt-6.2-sol / xhigh

Session: 01a20d4a-e012-78c2-a9c3-fb5e273c4075 · slot 1 · generation 4

  • P2 pkg/sql/parsers/sqlparse.go:783: A restricted session runs EXPLAIN ANALYZE on a table whose effective role rule filters that same table, and a scanned batch contains both allowed and denied rows.
    Impact: The query output honors the role filter, but the plan exposes a scan input count that includes denied rows. The count can change when only denied rows change, disclosing cardinality outside the role's authorized result.
    Evidence: The binder reaches the base table for a self-table rewrite at pkg/sql/plan/query_builder.go:13988. The scan counts input rows before evaluating its filter at pkg/sql/colexec/table_scan/table_scan.go:205. The compiler propagates that count at pkg/sql/compile/analyze_module.go:331, and the public formatter prints it at pkg/sql/plan/explain/explain_node.go:1584. The new regression scans a replacement table and checks root output rows, so it does not cover this case. Protect pre-policy row counts at the public EXPLAIN boundary and add a regression using a filter on the original table.

Validation:

  • Reused the matching frozen-head scope and whitespace check from attempt 1. Receipt 1795528541451518114-2.json exited 0 and matches scope-verified.stdout.

  • Current private /tmp and loopback preflight and native prerequisite verification passed. Receipts 1795529693789563217-2.json and 1795529718729167339-2.json match the retained logs and frozen source.

  • The frozen CI snapshot records success for Ubuntu/x86 unit tests, SCA, two multi-CN BVT jobs, and CI Required. The author reported two passing runs of the single-CN MySQL regression on this head.

  • Saved review-plan.md and discussion-assessment.json and verified coverage of all five discussion entries.

Limitations:

  • The local parser retry timed out before emitting test events. Receipt 1795529899447525105-2.json exited 124 and matches parser.stderr and the empty parser.jsonl.

  • The denied-row count finding is based on the source trace. The supplied execution evidence does not cover a same-table role filter or establish that the new regression ran in multi-CN CI.

@iamlinjunhong
iamlinjunhong force-pushed the codex/loom/develop/550cabbb86026a078b7112eaaed1390647cb07e500f66bb191f39529fc6b8656 branch from 67461fa to 68a3852 Compare November 24, 2026 16:58
@XuPeng-SH

XuPeng-SH commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: b1d8fb3ef0883e0daec8ced8f198f6fff95e9cf1

Review completed. Read the assessment.

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

mo-bot review · mo-bot-local · review

Commit: b1d8fb3ef0883e0daec8ced8f198f6fff95e9cf1

Assessment: request_changes

The implementation addresses the earlier remapping, protected-statistics, and correlated-result-count concerns using the existing policy and EXPLAIN components. The prior approval did not address the repository’s required mo-tester BVT coverage for this public SQL and privilege behavior.

Model: gpt-6.1-terra / max

Session: 01a22087-cef5-7962-9206-10e0932e0fda · slot 1 · generation 34

  • P2 pkg/tests/sqlintegration/explain_analyze_role_rule_test.go:29: The repository’s BVT suite runs against changes to role-governed EXPLAIN ANALYZE behavior.
    Impact: The new Go integration test provides useful public-path coverage, but the required mo-tester suite has no maintained case for this behavior. A regression can therefore escape the BVT coverage required for SQL-visible and privilege changes.
    Evidence: The pinned testing contract requires BVT public-path proof for SQL-visible and privilege behavior at review-policy/.agents/skills/mo-dev/references/testing-contract.md:25 and defines BVT as mo-tester cases under test/distributed/cases at line 141. The existing role-rule BVT exercises ordinary EXPLAIN UPDATE at test/distributed/cases/zz_accesscontrol/role_rule_dml_index.sql:28, not role-governed EXPLAIN ANALYZE. Extend a suitable role-rule BVT fixture with an authorized-result and protected analyzed-output check. This is a required coverage gap, not an observed runtime defect.

Validation:

  • The assigned-head parser, frontend policy and prepared-statement tests, and SQL integration regression passed with the isolated test executor; receipt checks/1795898461295425330-2.json and focused-tests.log.

  • Isolation preflight, native-input provenance verification, and git diff --check passed; receipts checks/1795898244208664990-3.json, checks/1795898244311969881-3.json, and checks/1795898420786619962-3.json.

  • The frozen CI snapshot reports successful x86 unit tests and multi-CN BVT jobs. Earlier same-PR one- and two-CN probes passed on head 223c2ae.

Limitations:

  • No local mo-tester case was run for the changed behavior because the required case is absent; the CI snapshot does not identify individual BVT case selection.

  • The earlier two-CN probe covered cross-CN policy visibility on an older head; it does not establish distributed physical execution on the assigned head.

@XuPeng-SH

XuPeng-SH commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: 31af97bcc2e0779119d779395d4ed47a271a1a48

Review completed. Read the assessment.

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

mo-bot review · mo-bot-local · review

Commit: 31af97bcc2e0779119d779395d4ed47a271a1a48

Assessment: request_changes

The change appropriately reuses the existing rewrite and remapping paths. The earlier remapping, statistics-redaction, correlated-count, and BVT-coverage concerns are addressed. A focused probe reproduced the previously identified row-count error for prepared EXPLAIN statements under a session limit.

Model: gpt-6.1-terra / max

Session: 01a22868-9f3f-7b70-a9d3-bd33c34f8163 · slot 1 · generation 37

  • P2 pkg/sql/plan/explain/explain_query.go:199: A role-governed session prepares a parameterless EXPLAIN ANALYZE or EXPLAIN PHYPLAN ANALYZE, then sets sql_select_limit to 1 and executes it against two authorized rows.
    Impact: Both prepared EXPLAIN variants can report Result: outputRows=2 even though the corresponding SELECT returns one row.
    Evidence: ResultRowsLine reads the logical root's output count. The cached prepared pipeline marks that root as the last operator before adding the dynamic session-limit operator at pkg/sql/compile/compile.go:1700 and :1730, so the count can describe the batch before truncation. A same-head public MySQL probe reproduced 2 for both prepared variants; the limited SELECT and direct EXPLAIN controls returned 1, and unlimited controls returned 2. See model-switch-1795975917629824352/prepared-limit-probe.log and checks/1795976223878303178-3.json. Use a post-limit result count and add a regression for both prepared EXPLAIN variants.

Validation:

  • Isolation preflight passed: model-switch-1795975917629824352/checks/1795975943484499234-3.json.

  • The committed parser and SQL integration regressions passed on the assigned head: focused-tests.log and checks/1795976297555579979-3.json in model-switch-1795975917629824352.

  • The prepared-limit probe failed on both affected variants with passing direct and unlimited controls: prepared-limit-probe.log and checks/1795976223878303178-3.json in model-switch-1795975917629824352.

  • git diff --check passed: model-switch-1795975917629824352/checks/1795976333014667183-3.json.

  • The updated review plan and complete discussion assessment are retained in model-switch-1795975917629824352; discussion key coverage passed.

Limitations:

  • The focused public-path probe used one CN. The frozen CI snapshot reports successful unit and multi-CN BVT jobs but does not identify individual selected cases.

  • The author reports two successful exact-head BVT case runs; their raw logs were not supplied.

@XuPeng-SH

XuPeng-SH commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: ed993e4cd2ece78920cef8c8bef6dbf3b77762d1

Review completed. Read the assessment.

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

mo-bot review · mo-bot-local · review

Commit: ed993e4cd2ece78920cef8c8bef6dbf3b77762d1

Assessment: approve

The change reuses the existing role-policy and database-remapping paths, suppresses pre-policy execution statistics, and derives the reported SELECT count from the coordinator output. The earlier remapping, statistics, correlated-count, BVT-coverage, and prepared-limit concerns are addressed in the current scope.

Model: gpt-6.1-terra / max

Session: 01a22aff-bb68-7e31-ae71-ee4ab48ad9d0 · slot 0 · generation 45

Validation:

  • The private /tmp and loopback preflight passed, and native provenance matched 195 tracked inputs and seven outputs: preflight.log, native-audit.log, and receipts checks/1796020833387009781-3.json and checks/1796020900862660752-6.json.

  • The committed single-CN SQL integration regression passed. An extended probe also passed for aggregates, UNION, recursive CTEs, SQL PREPARE, and hidden-row changes: candidate-integration.log and review-probe.log; receipts checks/1796021110669054461-3.json and checks/1796021191742643003-3.json.

  • A two-CN probe used a restricted client on CN1, forced a remote scan, observed a remote scope, and passed the role-policy and output checks: multicn-probe.log and checks/1796021362363426731-3.json.

  • Focused parser and frontend tests passed. A policy-transition probe confirmed that prepared handles were rejected for re-preparation after a mandatory rule was added: parser-focused.log, frontend-focused.log, and policy-transition-json.log; receipts checks/1796021749615961449-3.json, checks/1796021902827676928-3.json, and checks/1796021731754873882-3.json.

  • Discussion coverage and retained command receipts were checked in evidence-audit.log and checks/1796022043021452072-5.json.

Limitations:

  • The author reports two exact-head mo-tester runs of the changed BVT case with cleanup verified; raw BVT logs were not supplied and the case was not independently rerun.

  • An initial policy-transition run timed out before producing test output; preflight and a subsequent bounded retry passed. An exploratory probe also used an unsupported physical EXPLAIN syntax; the supported prepared-EXECUTE variant passed on retry.

@mergify

mergify Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

🚫 Left the queue · pull request branch update failed

1 min in the queue, with no time running CI.

So far

  • ✅ Entered the queue Mon 30 Nov, 07:02 · rule main · triggered by rule Automatic queue on approval for main
  • 🚫 Left the queue 07:03 · ed993e4

Reason

The pull request can't be updated

GitHub response: refusing to allow a GitHub App to create or update workflow .github/workflows/coverage-identity-contract.yml without workflows permission
GitHub only lets Mergify write to .github/workflows/ with the workflows permission. An organization owner can accept Mergify's pending permissions at https://dashboard.mergify.com/orgs/matrixorigin/repositories.

To get back in the queue

  • Update or rebase the branch yourself: the pull request is queued again once its queue conditions match.
  • If the failure is a flake, tick the box below to queue it again as it is.

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

Times in UTC · dashboard · how the merge queue works

This branch was successfully deployed

No deployments
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/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants