You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
to 3.0: fix non sys account cannot find the branch meta table. - #23220
fix non sys account cannot find the branch meta table.
PR Type
Bug fix
Description
Pass BackgroundExec to branch metadata functions for non-sys account access
Fix constructBranchDAG to use runSql with system context for metadata queries
Add bh parameter to decideLCABranchTSFromBranchDAG function signature
Add comprehensive test cases for branch diff with NULL value handling
Diagram Walkthrough
flowchart LR
A["diffMergeAgency"] -->|pass bh| B["decideLCABranchTSFromBranchDAG"]
B -->|pass bh| C["constructBranchDAG"]
C -->|use runSql with sysCtx| D["Query branch metadata"]
D -->|system account context| E["Access MO_BRANCH_METADATA"]
Loading
File Walkthrough
Relevant files
Bug fix
data_branch.go
Add BackgroundExec parameter for metadata access
pkg/frontend/data_branch.go
Add bh BackgroundExec parameter to decideLCABranchTSFromBranchDAG function
Add bh BackgroundExec parameter to constructBranchDAG function
Replace sqlexec.RunSql with runSql call using system context for metadata queries
Pass bh parameter through call chain from diffMergeAgency to constructBranchDAG
Below is a summary of compliance checks for this PR:
Security Compliance
⚪
Privilege escalation context
Description: Switching the session process context to a system account (AttachAccountId to System_Account) for running metadata queries broadens privileges and could be abused if inputs to the query building path are not strictly controlled; verify that only static SQL against system catalog is executed under elevated context and that the context is always restored. data_branch.go [4338-4345]
Objective: To create a detailed and reliable record of critical system actions for security analysis and compliance.
Status: Missing Auditing: The new code performs privileged metadata queries via system context without any added auditing of who initiated the action or its outcome.
Generic: Robust Error Handling and Edge Case Management
Objective: Ensure comprehensive error handling that provides meaningful context and graceful degradation
Status: Error Context: Errors from privileged metadata query (runSql with sys context) are returned without added contextual information about the operation, which may hinder debugging.
Generic: Security-First Input Validation and Data Handling
Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent vulnerabilities
Status: Privilege Escalation: Switching to system account context to run metadata queries may bypass tenant scoping; ensure authorization is explicitly validated for non-sys accounts.
[To ensure code accuracy, apply this suggestion manually]
Suggestion importance[1-10]: 8
__
Why: The suggestion correctly identifies a potential nil pointer dereference on sqlRet in an error path within the constructBranchDAG function, which would cause a panic. The proposed fix is correct and improves code robustness.
Medium
High-level
Refactor privilege escalation for safer context handling
To improve safety and isolation, refactor the privilege escalation mechanism. Instead of mutating the shared session context (ses.proc.Ctx), pass a temporary system context directly to the metadata query execution layer.
funcconstructBranchDAG(ctx context.Context, ses*Session, bhBackgroundExec) (*databranchutils.DataBranchDAG, error) {
varoldCtx context.ContextvarsysCtx context.Context// Store original contextoldCtx=ses.proc.Ctx// Create system context and overwrite the session's contextsysCtx=defines.AttachAccountId(ctx, catalog.System_Account)
ses.proc.Ctx=sysCtxdeferfunc() {
// Restore original contextses.proc.Ctx=oldCtx
}()
// Run SQL using the modified session, which implicitly uses the mutated contextsqlRet, err:=runSql(sysCtx, ses, bh, "SELECT ... FROM mo_branch_metadata", nil, nil)
// ...
}
After:
funcconstructBranchDAG(ctx context.Context, ses*Session, bhBackgroundExec) (*databranchutils.DataBranchDAG, error) {
// Create a system context for this specific operationsysCtx:=defines.AttachAccountId(ctx, catalog.System_Account)
// Pass the system context directly to the execution function.// This requires `runSql` or the underlying executor to use the// provided context for authorization instead of relying on `ses.proc.Ctx`.sqlRet, err:=runSql(sysCtx, ses, bh, "SELECT ... FROM mo_branch_metadata", nil, nil)
// ...
}
// The implementation of `runSql` and its callees should be adjusted// to create execution options using the passed `sysCtx` for privilege checks,// rather than relying on the context from `ses.proc`.funcrunSql(ctx context.Context, ses*Session, ...) {
// ...opts:= executor.Options{}.
// ...WithContext(ctx) // Explicitly use the passed context for the execution// ...
}
Suggestion importance[1-10]: 7
__
Why: The suggestion correctly identifies a fragile design pattern where the shared session context (ses.proc.Ctx) is mutated for privilege escalation, which is a potential source of subtle bugs and race conditions.
Medium
More
gouhongshen
changed the title
fix non sys account cannot find the branch meta table.
to 3.0: fix non sys account cannot find the branch meta table.
Dec 5, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
User description
What type of PR is this?
Which issue(s) this PR fixes:
issue ##21979
What this PR does / why we need it:
fix non sys account cannot find the branch meta table.
PR Type
Bug fix
Description
Pass
BackgroundExecto branch metadata functions for non-sys account accessFix
constructBranchDAGto userunSqlwith system context for metadata queriesAdd
bhparameter todecideLCABranchTSFromBranchDAGfunction signatureAdd comprehensive test cases for branch diff with NULL value handling
Diagram Walkthrough
File Walkthrough
data_branch.go
Add BackgroundExec parameter for metadata accesspkg/frontend/data_branch.go
bh BackgroundExecparameter todecideLCABranchTSFromBranchDAGfunction
bh BackgroundExecparameter toconstructBranchDAGfunctionsqlexec.RunSqlwithrunSqlcall using system context formetadata queries
bhparameter through call chain fromdiffMergeAgencytoconstructBranchDAGdiff_6.sql
Add branch diff NULL value test casestest/distributed/cases/snapshot/branch/diff/diff_6.sql
acc1with non-sys credentialscolumn types
transitions
NULL handling
diff_6.result
Add expected results for branch diff teststest/distributed/cases/snapshot/branch/diff/diff_6.result
table
types