Skip to content

fix(data branch): compare clones against older ancestor snapshots - #32970

Merged
XuPeng-SH merged 8 commits into
matrixorigin:mainfrom
VioletQwQ-0:fix/data-branch-diff-older-ancestor-snapshot
Nov 24, 2026
Merged

XuPeng-SH merged 8 commits into
matrixorigin:mainfrom
VioletQwQ-0:fix/data-branch-diff-older-ancestor-snapshot

Conversation

@VioletQwQ-0

Copy link
Copy Markdown
Collaborator

What type of PR is this?

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

Which issue(s) this PR fixes:

issue #32968

What this PR does / why we need it:

Summary

Fixes DATA BRANCH DIFF (and related snapshot/LCA probe paths) when comparing a cloned branch against an older ancestor snapshot (T_old < CloneTS), along with snapshot handling and probe bugs exposed during that scenario.

Root Cause & Changes

  1. Older-ancestor clone diff & snapshot anchor propagation (pkg/frontend/data_branch.go, pkg/frontend/data_branch_types.go, pkg/frontend/data_branch_hashdiff.go):
    • Replaced separate tarLCASnapshot / baseLCASnapshot heuristics with explicit lcaProbeAnchors and lcaProbeSnapshots so DATA BRANCH DIFF comparing a clone against an older ancestor snapshot collects ancestor writes between T_old and CloneTS properly and preserves PICK conflict behavior when requested.
    • Tracked background transaction snapshot timestamps (tarRelSnapshot, baseRelSnapshot, lcaRelSnapshot, resolvedRelSnapshots) in getRelations, diffOnBase, decideCollectRange, and buildSideCollectRange, propagating per-segment snapshots (collectRange.segmentSnapshot / relSnapshot) into constructChangeHandle and lcaFallbackRangeRel so hydration and compaction stay aligned with each segment's snapshot.
  2. Renamed-column LCA probing & SQL MO_TS formatting (pkg/frontend/data_branch.go, pkg/frontend/data_branch_hashdiff.go):
    • Tracked tarLCAColNames and baseLCAColNames separately in reconcileDataBranchEndpointSchema and selected the active lcaColNames according to the earlier anchor (baseAnchor < tarAnchor), building snapshot-accurate column layouts via buildLCAProbeColumnLayout and lcaProbeTableDefAtSnapshot.
    • Fixed internal LCA probe SQL to format {MO_TS='<TS.ToString()>'} instead of {MO_TS=<PhysicalTime>}.
  3. Fallback reader snapshot binding & MERGE guard (pkg/frontend/data_branch.go, pkg/frontend/data_branch_helpers.go, pkg/frontend/data_branch_hashdiff.go):
    • Bound scanSnapshotRelationByIDWithFallbackImpl / runLCAProbeWithReaderFallbackWithPreopened to the resolved snapshot relation and fixed ownerScopedChangesHandle.Close / historicalDataBranchChangesHandle.Next lifecycle handling.
    • Rejected unsupported destination snapshot options in handleBranchMerge ("destination snapshot option is not supported for DATA BRANCH MERGE").
  4. Tests (pkg/frontend/data_branch_test.go, pkg/frontend/data_branch_hashdiff_test.go, pkg/frontend/data_branch_helpers_test.go, test/distributed/cases/git4data/branch/diff/diff_older_ancestor_snapshot.{sql,result}):
    • Added unit tests and end-to-end BVT coverage for pre-fork ancestor snapshots (both directions, intermediate branches, COPY ALTER, fake/composite PKs, in-place column renames, same-table snapshot DIFF, sibling PICK, and MERGE/PICK snapshot edge cases).

Non-Goals / Deferred (Pre-Existing Out-of-Scope)

  • Pre-existing explicit-txn inherited clone writes (clone.go:699 / data_branch.go:3069), sibling PICK after inherited delete (data_branch_types.go:213 / data_branch_output.go:331), cross-DB same-table-name OUTPUT FILE (data_branch_output.go:1336), COPY ALTER ADD combined with pre-fork in-place rename (data_branch.go:2556), and OUTPUT FILE CSV logical time (data_branch_output.go:1514,1612) are pre-existing baseline items left out of scope.
  • Pre-existing shared mpool 96-byte baseline warning in TestHandleDelsOnLCA_SQLPaths (PR 1's added subtest runs with 0 mpool warnings).

QA Decision

  • QA required: yes (SQL-visible behavior fix in DATA BRANCH DIFF / MERGE with historical snapshots; adds BVT suite test/distributed/cases/git4data/branch/diff/diff_older_ancestor_snapshot.sql).

Verification (Exact Final HEAD ec76fb92283ccca6dca962a8c2daec6c5b9ef2b4)

  • Unit Tests: PASS (104 top-level / 172 subtests in r6-final-unit-focused.log, plus 4/4 subtests in r6-final-unit-pr1-supplement.log, 0 failures, 0 skipped; TestLCAProbeRenamedSnapshotSQLAndReaderParity 0 mpool warnings).
  • Binary: make build-with-prebuilt-native -> vcs.revision=ec76fb92283ccca6dca962a8c2daec6c5b9ef2b4, vcs.modified=false.
  • Static Check: make static-check-selected STATIC_CHECK_PACKAGES=./pkg/frontend GOLANGCI_LINT_CONCURRENCY=4 -> 0 issues..
  • BVT:
    • Focused diff_older_ancestor_snapshot: 103/103 (100%).
    • Regressions (55 scripts across diff, merge, pick, edge/branch_binary_pk): 4429/4429 (100%).

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

@matrix-meow matrix-meow added the size/XL Denotes a PR that changes [1000, 1999] lines label Nov 20, 2026
@mergify mergify Bot added the kind/bug Something isn't working label Nov 20, 2026
@mergify

mergify Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

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

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

Request changes

P2: Historical LCA lookup failure can silently read a different table

lcaProbeTableDefAtSnapshot suppresses any GetRelationById error when allowMismatchedEndpointFallback is true and returns the current endpoint definition. handleDelsOnLCA then uses that definition's current database/table name for time-travel SQL, without verifying that the name resolves to the same table ID at the probe snapshot.

A concrete shape is: create ancestor a with (1, 10) and an unrelated x with (1, 99); branch child from a; rename x away and a to x; delete key 1 in child; then compare child with the current x. At the fork snapshot the LCA is still table a, while the name x refers to the unrelated table. If the historical ID lookup encounters an error at this probe (for example, a transient catalog/storage error), the new fallback issues db.x{MO_TS='<fork>'}; because that SQL can succeed, the unrelated (1, 99) is accepted as the LCA row instead of returning the lookup error. This can silently corrupt DIFF results and conflict decisions in MERGE/PICK. The earlier name-based path already had this identity risk; the new historical-reopen/fallback logic leaves it open in the path this PR is changing.

Please keep the SQL fast path only when its historical table identity is established (or verify the historical name resolves to the expected ID). Otherwise use a relation already proven to be opened at exactly that snapshot, or propagate the lookup error. Add a regression with a reused historical table name and an injected by-ID lookup failure; it should either read the correct LCA by ID or fail closed, never accept the decoy. Also cover the ordinary success path and preserve the existing snapshot-matched reader fallback.

I checked the failure using a temporary exact-head frontend test: with a mocked historical-ID lookup and SQL executor, successful ID lookup generated SQL for original_name and returned the modeled LCA; injected lookup failure generated SQL for reused_name and accepted the modeled decoy. This was a fault-injected frontend test, not a live-storage end-to-end reproduction. The current-head multi-CN BVT artifact reports the new diff_older_ancestor_snapshot.sql case at 103/103.

Cleanup

lcaProbeColumnLayout is now a wrapper used only by tests; production calls buildLCAProbeColumnLayout directly. Please remove the unused production wrapper and have the tests exercise the real builder. I found no prior human review threads on this PR.

@mergify

mergify Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

🚫 Left the queue · queue conditions no longer match

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

So far

  • ✅ Entered the queue Tue 24 Nov, 09:39 · rule main · triggered by rule Automatic queue on approval for main
  • 🚫 Left the queue 09:54 · 4b0c7d6

Reason

Pull request #32970 has been dequeued

Queue conditions are not satisfied:

  • #changes-requested-reviews-by<=0
  • approved-reviews-by=XuPeng-SH

To get back in the queue

  • Push a change if one is needed: the pull request is queued again once its queue conditions match.
  • If no change is needed, tick the box below to queue it again as it is.

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

Times in UTC · dashboard · how the merge queue works

@XuPeng-SH

XuPeng-SH commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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

Commit: 4b0c7d6b52934c830c6a0c82542681bf013ccb22

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: 4b0c7d6b52934c830c6a0c82542681bf013ccb22

Assessment: request_changes

The historical table-name issue is resolved, and the older-snapshot DIFF and sibling PICK checks passed. The reader still resolves the same historical relation twice per batch. Reuse the verified relation for the scan.

Model: gpt-6.2-sol / xhigh

Session: 01a1f0a0-5126-78f3-9ab8-ef4e87f2f119 · slot 0 · generation 3

  • P2 pkg/frontend/data_branch_hashdiff.go:879: A nonempty historical LCA probe or hydration batch takes the reader path after resolving a relation at the required snapshot.
    Impact: The reader creates a second snapshot workspace and repeats the same catalog lookup for every batch. When the historical table version has aged out of the catalog cache, both lookups query catalog storage.
    Evidence: lcaProbeTableDefAtSnapshot resolves the owner, table ID and timestamp, then passes the verified relation to the scanner as a fallback. The scanner opens the same relation again at pkg/frontend/data_branch_helpers.go:423. The focused test at pkg/frontend/data_branch_hashdiff_test.go:1030 passed and explicitly requires two successful by-ID lookups. Use the verified handle for schema and range reads, preserving the existing owner and snapshot checks.

Validation:

  • Verified the frozen checkout and patch. Isolation preflight, native prerequisites and the candidate service build passed.

  • Passed 126 focused frontend tests and 213 subtests, including the updated bounded hydration fixture and historical lookup failure controls.

  • Matched expected rows and errors for all 131 SQL fixture statements on an isolated CN. Additional sibling PICK checks verified SKIP and ACCEPT across COPY ALTER. Frozen CI reports successful proxy and pessimistic multi-CN BVT jobs. Receipts and logs are mapped in review-validation.md.

Limitations:

  • The local SQL probe checked rows and errors on one CN. Multi-CN coverage comes from frozen CI. The duplicate lookup count is verified, while its end-to-end latency cost was not measured.

@mergify mergify Bot added dequeued and removed queued labels Nov 24, 2026
@VioletQwQ-0

Copy link
Copy Markdown
Collaborator Author

Addressed the duplicate historical lookup flagged in review 6037060782 in 3967ea75318. The reader uses the relation already resolved for the requested owner, table ID and exact snapshot; it no longer creates another snapshot workspace or reopens that relation by ID. Catalog scans without a preopened relation still resolve by ID. The reader and SQL-fallback tests now require exactly one snapshot clone and lookup, with the same result rows. Mismatched historical handles are still rejected. Local validation: 206 focused frontend tests, focused atomic coverage, and both SQL BVT scripts (diff_older_ancestor_snapshot and diff_output_as_altered_columns, 131/131 each) passed.

@XuPeng-SH

XuPeng-SH commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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

Commit: 3967ea75318b7397c008d7599909366395571c39

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: 3967ea75318b7397c008d7599909366395571c39

Assessment: approve

DIFF uses the earlier ancestor observation, sibling PICK keeps each fork’s baseline, and MERGE rejects destination snapshots before applying changes. The historical table-name and duplicate lookup findings are resolved. The reader reuses the relation resolved for the requested table ID, owner, and snapshot.

Model: gpt-6.2-sol / xhigh

Session: 01a20d47-b35e-7bd2-9d5c-61978a866730 · slot 0 · generation 6

Validation:

  • Verified the frozen checkout, patch, pinned policy, and all 12 discussion entries. Matched nine check receipts to retained logs.

  • The focused frontend run passed 127 tests and 218 subtests, including all newly added tests and the one-lookup, snapshot-mismatch, and renamed-column cases.

  • Matched all 131 SQL fixture statements to the expected result file and independently checked the core DIFF rows and summary counts in both directions. Frozen CI reported passing unit tests, static analysis, and both multi-CN BVT lanes.

Limitations:

  • Local SQL verification checked the fixture and its expected results. Live SQL coverage came from frozen CI and the author’s report for the reviewed head.

@mergify mergify Bot added the queued label Nov 24, 2026
@mergify

mergify Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

🚫 Left the queue · pull request merged manually

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

So far

  • ✅ Entered the queue Tue 24 Nov, 11:43 · rule main · triggered by rule Automatic queue on approval for main
  • ⏹️ Testing stopped · in place
  • 🚫 Left the queue 12:13 · 1d4fa01

Reason

Pull request #32970 has been merged manually at 835455b

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
ci — 1d4fa013 Deployed Nov 24, 2026 by mergify[bot]
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/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants