Skip to content

fix(frontend): retain primary keys in data branch projections - #28133

Merged
XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-25244-2f160761
Sep 4, 2026
Merged

XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-25244-2f160761

Conversation

@gouhongshen

@gouhongshen gouhongshen commented Sep 4, 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:

Related to #25244

What this PR does / why we need it:

Root cause

When DATA BRANCH DIFF used COLUMNS, resolveProjectedIdxes returned only the requested visible columns. The row path temporarily extracted primary-key values for OUTPUT LIMIT sorting, but final row compaction and result-schema construction still used the requested-only list. Composite primary-key values were therefore absent from both the result rows and their metadata. OUTPUT AS reused the same incomplete list.

The older worker-pool leak finding recorded in #25244 is already addressed on current main by #26369 (09855ae9b1) and is not changed here.

Changes

  • Make the shared projection resolver emit explicit primary-key columns first in primary-key definition order, then requested non-PK visible columns in request order, with deduplication.
  • Reuse that effective projection for ordinary diff result rows, result metadata, and OUTPUT AS table schemas.
  • Preserve existing fake-PK/no-PK behavior and leave COUNT, SUMMARY, and OUTPUT FILE behavior unchanged.
  • Add focused unit coverage, an exact composite-PK regression BVT, related boundary assertions, and update the documented OUTPUT AS contract.

Issue-to-test proof

  • TestDataBranchOutputColumnsIncludeCompositePrimaryKey asserts the exact five-column metadata (diff, flag, both PK columns, and val) and the exact update row values.
  • TestDataBranchOutputResolveProjectedIdxes covers normal and composite PKs, definition order different from physical order, duplicate/case-insensitive requests, unknown columns, and the fake-PK control case. Table-schema tests cover ordinary output and OUTPUT AS ordering.
  • diff_composite_pk_columns.sql covers the issue reproduction, partial/duplicate PK requests, UPDATE/DELETE/INSERT rows, projected OUTPUT LIMIT, unrelated tables, and OUTPUT AS metadata plus rows.
  • Existing diff_11, diff_13, diff_17, branch_output_limit_order, and merge_generated_column cases cover the broader data types, lineage depth, materialized schema, limit ordering, and generated-column interactions affected by the shared resolver.

Tests run

  • make build
  • Full pkg/frontend CGo test package: passed.
  • Focused frontend regression tests: passed.
  • New composite-PK BVT twice with metadata comparison enabled: 28/28 each run.
  • Existing related BVTs: diff_11 158/158; diff_13 137/137; diff_17 154/154; branch_output_limit_order 21/21; merge_generated_column 80/80; remaining branch edge cases 314/314.

The existing diff_13, diff_17, and edge/merge result files use historical metadata-width encodings that do not compare cleanly with the current tester. Those runs used -n, which still compared all SQL output, column names/order, and row values. The new regression result was checked twice with metadata comparison enabled.

Residual risks

For explicit-PK tables, callers using COLUMNS will now receive additional PK columns, in PK definition order, as required for row identity. Fake-PK/no-PK tables and non-row output modes retain their prior behavior. This change does not alter concurrency, storage, or resource-lifecycle code; #25244 remains open for manual validation.

CI follow-up

  • Updated the existing pkg/tests/dml output-limit regression assertion to expect the retained primary-key column introduced by this fix; focused TestDataBranchDiffAsFile passes locally.

Rebase follow-up

  • Rebased the issue branch onto origin/main at fa829e5 without conflicts or scope changes.
  • Re-ran the focused frontend regression tests and TestDataBranchDiffAsFile after the rebase; both passed locally.

@gouhongshen-gmail

gouhongshen-gmail commented Sep 4, 2026 •

Copy link
Copy Markdown

Unrelated CI blocker update for MatrixOne ALL CI run 33846244114: attempt 1 had three producer jobs fail on separate self-hosted runners with the identical runner-communication annotation; the final coverage merge job then failed as their downstream prerequisite check.

  • UT Coverage on Ubuntu/x86 stopped during Checkout CI coverage helper on amd64-mo-guangzhou-2xlarge16.
  • UT Test on Ubuntu/x86 stopped during Unit Testing on amd64-mo-shanghai-8c16g.
  • SCA on Linux/arm64 stopped during Static Code Analysis on arm64-mo-shanghai-4c8g.
  • Coverage merge failed on Verify coverage prerequisites with coverage merge skipped because at least one required producer was skipped or failed; its log contains no source or test diagnostic.

The three producer check-runs contain only The self-hosted runner lost communication with the server; the annotation targets .github, and their job logs are unavailable (BlobNotFound). The failures match existing infrastructure records #27784 (AMD64) and #27742 (ARM64).

Attempt 1 failed jobs were rerun once as attempt 2. Attempt 2 coverage completed its tests but exposed a PR-owned stale assertion in pkg/tests/dml/dml_test.go (TestDataBranchDiffAsFile/output_limit_subset) that still expected four columns; both assertions now expect the retained primary-key column, the focused test passes locally, and commit 7f9da301c4 is pushed. The old attempt 2 was canceled automatically when the new head triggered MatrixOne ALL CI run 33852883820; SCA and UT had not reached terminal state, so no repeated infrastructure failure was recorded. No PR blocker issue was added for the test assertion.

The rerun 33852883820 completed all required jobs successfully; no unrelated CI blocker remains on that PR head.

@gouhongshen
gouhongshen force-pushed the agent/issue-25244-2f160761 branch from 7f9da30 to 5b69d8d Compare September 4, 2026 09:56
@gouhongshen
gouhongshen marked this pull request as ready for review September 4, 2026 09:57
@gouhongshen
gouhongshen force-pushed the agent/issue-25244-2f160761 branch from f65e79e to 1120ce0 Compare September 4, 2026 11:47
@gouhongshen
gouhongshen force-pushed the agent/issue-25244-2f160761 branch from 1120ce0 to 3e596fb Compare September 4, 2026 12:11
@gouhongshen
gouhongshen force-pushed the agent/issue-25244-2f160761 branch from 3e596fb to 4a35a36 Compare September 4, 2026 12:34

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

Deep review completed at exact head 7c5b2c7 (associated issue #25244).

Design/correctness: the shared effective projection now has one explicit contract across result metadata, ordinary rows, projected LIMIT, and OUTPUT AS: explicit PK components first in PK-definition order, then requested non-PK columns in request order, with deduplication. This directly restores row identity for the issue reproduction. No-COLUMNS, fake-PK/no-PK, COUNT, SUMMARY, and OUTPUT FILE behavior remains unchanged; OUTPUT FILE plus COLUMNS still fails before worker creation.

Counterexamples checked: composite PK order differing from physical order; requested/duplicate/case-insensitive PK names; UPDATE, DELETE, INSERT, unrelated endpoint tables, target/base schema drift, LIMIT 0 and positive LIMIT, generated columns, metadata-name collisions, and OUTPUT AS rollback/error paths. Endpoint schema reconciliation requires explicit PKs to remain compatible and common, so the newly projected key cannot become a target-only/null identity. Result rows and schema consume the same resolved index order.

Performance/lifecycle: resolution is bounded O(visible columns + requested columns + PK columns) with schema-width allocations only. Diff scanning and PK ordering complexity are unchanged. The projection validation executes before pool creation and producer goroutines; this patch adds no goroutine, wait, storage, or cleanup ownership.

Validation: diff-check is clean. The pre-rebase exact patch completed SCA, UT, coverage, build, BVT, and connector checks successfully; the rebase is patch-equivalent and the only new main delta is outside these paths. Focused UT/BVT coverage exercises the exact composite-PK reproduction, output shape and values, LIMIT, all row kinds, OUTPUT AS, fake PK, and error validation.

No blocking correctness, performance, or unhappy-path issue found.

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

Deep review completed at exact head 7c5b2c77ab8607c93ae42575eeb29f2c8b9d8e77 (related issue #25244).

The design direction is sound: an explicit primary key is row identity, so COLUMNS may prune payload columns but must not prune any PK component. Centralizing that rule in resolveProjectedIdxes keeps ordinary row output, result metadata, LIMIT compaction/sorting, and OUTPUT AS schemas on one effective projection contract. PK definition order is stable; requested non-PK order remains stable; requested/duplicate/case-insensitive PK names are deduplicated. Fake-PK tables intentionally retain the prior behavior.

Counterexamples reviewed: composite PK order differing from physical column order; partial and duplicate PK requests; normal and fake PKs; renamed/schema-reconciled endpoints; UPDATE/DELETE/INSERT and unrelated-table rows; LIMIT 0 and bounded top-K; OUTPUT AS types/nullability/name collisions; generated and target-only columns; unknown columns and cancellation/cleanup paths. The production table binding validates PK indices, and base/historical rows are projected into target layout before this output layer, so the added indices do not create row/schema skew.

Performance impact is proportional only to the required PK values returned/materialized. LIMIT already extracted PK values for ordering before this change; there is no new scan, worker, lock, or lifecycle path. Exact-head substantive CI on the prior identical PR diff was green; this head only rebases onto main, and focused frontend output tests pass locally, including a 100-run repetition and 20-run race repetition on the previous identical diff. At this head, go test -count=1 ./pkg/frontend -run Test(DataBranchOutput|NewApplyBatchInfoExcludesGeneratedColumns|DataBranchDiffAsFile) and git diff --check pass.

No P0/P1/P2/P3 finding remains. APPROVE.

@mergify

mergify Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

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

  • Queue this pull request

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.

4 participants