Skip to content

test(parser): pin figure-aware reading order in PDF post-process - #18178

Merged
yuzhichang merged 1 commit into
infiniflow:mainfrom
xugangqiang:fix/pdf-column-reading-order
Aug 12, 2026
Merged

yuzhichang merged 1 commit into
infiniflow:mainfrom
xugangqiang:fix/pdf-column-reading-order

Conversation

@xugangqiang

Copy link
Copy Markdown
Contributor

Summary

Adds a characterization / regression test (pdf_postprocess_measure_test.go) that pins the figure-aware reading order produced by the PDF adapter layer (internal/parser/parser/pdf_postprocess.go).

While planning "Borrow B" (an image-anchored reading-order DAG inspired by xugangqiang/pdf-inspector), measurement showed the adapter already produces correct order for pages where a figure spans one or both columns:

  • sortSectionsByPosition (page → top → left, run unconditionally) places a column-spanning figure between the upper and lower text bands.
  • reorderPDFMultiColumn (left → top, when enableMultiColumn is set) wraps the figure correctly inside its own column.

Both derive order from section geometry, not from AssignColumn's FinalReadingOrderMerge (column-major) output — which these sorts overwrite anyway. So the planned "figure-anchored DAG" would have no end-to-end effect, and Borrow B is not needed. The only residual limitation (a merged section whose vertical span straddles a figure is ordered by its first top) is inherent to sorting already-merged sections and is out of scope.

This test locks the current correct behavior so a future refactor of the adapter cannot silently regress figure handling.

Test plan

  • bash build.sh --test ./internal/parser/parser/ — new TestMeasure_* cases pass; they assert a spanning figure is placed between the upper and lower text in both adapter modes.

Note

This PR is intentionally independent of PR #18150 (column detection). It only adds a test; no production code is changed.

Characterization/regression test for Borrow B (image-anchored reading order).
Measurement shows the adapter layer already produces correct figure-aware
order: sortSectionsByPosition (page->top->left, run unconditionally at
pdf_postprocess.go:29) and reorderPDFMultiColumn (left->top, when
enableMultiColumn) both place a column-spanning figure between the upper
and lower text bands. So Borrow B as a separate feature is NOT needed —
the detector's FinalReadingOrderMerge output is overwritten by these
geometry-based sorts anyway. The only residual is a merged section whose
vertical span straddles a figure (placed by its first top); that is
inherent to sorting already-merged sections and out of scope.

Co-Authored-by: CodeBuddy <[email protected]>
@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Aug 12, 2026
@xugangqiang xugangqiang added the ci Continue Integration label Aug 12, 2026
@xugangqiang
xugangqiang requested a review from yuzhichang August 12, 2026 11:39
@coderabbitai

coderabbitai Bot commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds PDF post-processing measurement tests for figure ordering across two columns and for paragraphs that span a figure.

Changes

PDF reading-order tests

Layer / File(s) Summary
Figure spanning-column measurements
internal/parser/parser/pdf_postprocess_measure_test.go
Adds shared layout helpers and tests for figure placement with the default and multi-column adapters.
Paragraph and figure ordering characterization
internal/parser/parser/pdf_postprocess_measure_test.go
Verifies that a paragraph spanning a figure is ordered by its first top coordinate.

Estimated code review effort: 2 (Simple) | ~10 minutes

Poem

I’m a rabbit with a measured pen,
Testing columns again and again.
Figures hop between text in line,
Paragraphs sort by their top design.
The PDF path now leaves a trace!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly identifies the parser test change and its purpose of preserving figure-aware PDF reading order.
Description check ✅ Passed The description includes the required Summary section and provides clear purpose, context, test coverage, and scope.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/parser/parser/pdf_postprocess_measure_test.go`:
- Around line 60-73: Update the ordering assertions in
internal/parser/parser/pdf_postprocess_measure_test.go lines 60-73 to record
each expected upper and lower section index and fail if any required section is
missing before comparing positions; also update lines 122-131 to fail when
either para-col0 or figure has index -1 before asserting their order, using the
existing test symbols and preserving the intended ordering checks.
- Around line 3-14: Remove the obsolete planning commentary above the
measurement tests, including references to “Borrow B,” future figure-anchored
fixes, and implementation-planning details. Retain only concise documentation
describing the current behavior covered by the tests.
- Around line 94-101: Extend the reading-order assertions in the parser test to
verify the spanning figure is after both upper sections and before both lower
sections, including col1-upper and col1-lower alongside the existing col0
checks. If the implementation fails this contract, update reorderPDFMultiColumn
to produce that ordering while preserving the existing section-presence
validation.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 74498e41-374b-4fed-b6a5-f4b53b3ef1ce

📥 Commits

Reviewing files that changed from the base of the PR and between 91fd114 and c12f156.

📒 Files selected for processing (1)
  • internal/parser/parser/pdf_postprocess_measure_test.go

Comment on lines +3 to +14
// Measurement / characterization tests for Borrow B (image-anchored reading
// order). These pin the CURRENT behavior of the adapter layer
// (pdf_postprocess.go) on pages where a figure spans one or both columns, so
// we can decide WHERE a figure-anchored reading-order fix (if any) must live.
//
// Key fact established during planning: AssignColumn's FinalReadingOrderMerge
// (column-major) is OVERWRITTEN by sortSectionsByPosition (page -> top -> left,
// run unconditionally at pdf_postprocess.go:29) and, when enableMultiColumn, by
// reorderPDFMultiColumn (left -> top). Both derive order from section geometry,
// not from ColID, and are already figure-aware. These tests verify that and
// surface the one residual limitation (a merged section whose vertical span
// straddles a figure is placed by its FIRST top).

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.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Remove obsolete planning commentary.

This comment records the earlier “Borrow B” evaluation and a possible future fix. The PR objective states that the image-anchored DAG is unnecessary. Keep only documentation of the current test behavior.

As per coding guidelines, remove stale documentation.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/parser/parser/pdf_postprocess_measure_test.go` around lines 3 - 14,
Remove the obsolete planning commentary above the measurement tests, including
references to “Borrow B,” future figure-anchored fixes, and
implementation-planning details. Retain only concise documentation describing
the current behavior covered by the tests.

Source: Coding guidelines

Comment on lines +60 to +73
for _, name := range []string{"col0-upper", "col1-upper"} {
for i, s := range result.Sections {
if s.Text == name && i > figIdx {
t.Errorf("%s (idx %d) is after figure (idx %d); want before", name, i, figIdx)
}
}
}
for _, name := range []string{"col0-lower", "col1-lower"} {
for i, s := range result.Sections {
if s.Text == name && i < figIdx {
t.Errorf("%s (idx %d) is before figure (idx %d); want after", name, i, figIdx)
}
}
}

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.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Fail when a required fixture section is missing.

Both tests use ordering checks that can pass when required sections are absent.

  • internal/parser/parser/pdf_postprocess_measure_test.go#L60-L73: Record the index of every upper and lower section, then fail before comparing indices if any expected section is missing.
  • internal/parser/parser/pdf_postprocess_measure_test.go#L122-L131: Fail if para-col0 or figure has index -1 before asserting their order.
📍 Affects 1 file
  • internal/parser/parser/pdf_postprocess_measure_test.go#L60-L73 (this comment)
  • internal/parser/parser/pdf_postprocess_measure_test.go#L122-L131
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/parser/parser/pdf_postprocess_measure_test.go` around lines 60 - 73,
Update the ordering assertions in
internal/parser/parser/pdf_postprocess_measure_test.go lines 60-73 to record
each expected upper and lower section index and fail if any required section is
missing before comparing positions; also update lines 122-131 to fail when
either para-col0 or figure has index -1 before asserting their order, using the
existing test symbols and preserving the intended ordering checks.

Comment on lines +94 to +101
f := idx("figure")
c0u, c0l := idx("col0-upper"), idx("col0-lower")
if f < 0 || c0u < 0 || c0l < 0 {
t.Fatal("expected sections missing")
}
if !(c0u < f && f < c0l) {
t.Errorf("figure not wrapped inside col0: want col0-upper(%d) < figure(%d) < col0-lower(%d)", c0u, f, c0l)
}

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.

🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift

Assert the stated multi-column reading order.

The test only checks column 0. For this fixture, reorderPDFMultiColumn places col1-upper after figure. The test therefore does not verify that the spanning figure is between the upper and lower text bands in both columns.

Define the required reading-order contract. If both upper sections must precede the figure, assert col0-upper and col1-upper before it, and both lower sections after it. Update reorderPDFMultiColumn if that assertion is the required behavior.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/parser/parser/pdf_postprocess_measure_test.go` around lines 94 -
101, Extend the reading-order assertions in the parser test to verify the
spanning figure is after both upper sections and before both lower sections,
including col1-upper and col1-lower alongside the existing col0 checks. If the
implementation fails this contract, update reorderPDFMultiColumn to produce that
ordering while preserving the existing section-presence validation.

@xugangqiang
xugangqiang marked this pull request as draft August 12, 2026 12:04
@xugangqiang
xugangqiang marked this pull request as ready for review August 12, 2026 12:22
@yuzhichang
yuzhichang merged commit c7db20b into infiniflow:main Aug 12, 2026
6 checks passed
@xugangqiang
xugangqiang deleted the fix/pdf-column-reading-order branch September 23, 2026 07:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants