Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(pdf): do not cross-page merge tables whose anchor is not at page bottom (#18789)

xugangqiangd82ab2eced70
diff --git a/internal/deepdoc/parser/pdf/table/table_merge.go b/internal/deepdoc/parser/pdf/table/table_merge.go
index 72303ce03c..6ce1e7596f 100644
--- a/internal/deepdoc/parser/pdf/table/table_merge.go
+++ b/internal/deepdoc/parser/pdf/table/table_merge.go
@@ -117,9 +117,26 @@ func MergeTablesAcrossPages(tables []pdf.TableItem, medianHeights, pageHeights m
if anchorPageH, ok := pageHeights[anchorPg]; ok && anchorPageH > 0 {
yDis += anchorPageH
}
- }
- if yDis > mh*23 {
- continue
+ if yDis > mh*23 {
+ continue
+ }
+ } else {
+ // A NEGATIVE page-local yDis means the continuation sits at the
+ // TOP of the next page (in page-local coordinates it is "above"
+ // the anchor). A genuine cross-page split is cut off at the page
+ // boundary, so its anchor must END NEAR THE BOTTOM of its page.
+ // Two independent tables that merely both start near the top of
+ // consecutive pages (e.g. ZoomNeXt's R3→R5) also produce a
+ // negative yDis but their anchor ends high on its page — merging
+ // them wrongly collapses the second table into the first and
+ // silently drops it. Reject the merge unless the anchor bottom is
+ // within mh*23 of the page bottom (the same proximity used for
+ // the Y gate), so only real page-boundary continuations merge.
+ if anchorPageH, ok := pageHeights[anchorPg]; ok && anchorPageH > 0 {
+ if maxBottomOnPage(anchor.Positions, anchorPg) < anchorPageH-mh*23 {
+ continue
+ }
+ }
}
// Merge: combine cells and positions.
anchor.Cells = append(anchor.Cells, tables[jt.idx].Cells...)
@@ -215,6 +232,31 @@ func MergeTablesAcrossPages(tables []pdf.TableItem, medianHeights, pageHeights m
return result
}
+// maxBottomOnPage returns the largest Bottom among the table's positions that
+// carry page number pg. Page-local Y resets to 0 at each page top, so a
+// multi-page anchor's positions across different pages are not directly
+// comparable; this isolates the anchor's extent on the specific page it is
+// being tested against for a cross-page continuation.
+func maxBottomOnPage(positions []pdf.Position, pg int) float64 {
+ var mb float64
+ for _, p := range positions {
+ onPage := false
+ for _, pn := range p.PageNumbers {
+ if pn == pg {
+ onPage = true
+ break
+ }
+ }
+ if !onPage {
+ continue
+ }
+ if p.Bottom > mb {
+ mb = p.Bottom
+ }
+ }
+ return mb
+}
+
// stackGrids concatenates per-page grids (each already built correctly by
// processOneTable) into one grid for a cross-page-merged table. Continuation
// pages are shifted in Y so their rows sit strictly below the anchor rows,
diff --git a/internal/deepdoc/parser/pdf/table/table_merge_top_table_test.go b/internal/deepdoc/parser/pdf/table/table_merge_top_table_test.go
new file mode 100644
index 0000000000..e6d9500063
--- /dev/null
+++ b/internal/deepdoc/parser/pdf/table/table_merge_top_table_test.go
@@ -0,0 +1,55 @@
+package table
+
+import (
+ "testing"
+
+ pdf "ragflow/internal/deepdoc/parser/pdf/type"
+)
+
+// TestMergeTablesAcrossPages_TopTablesOnConsecutivePagesNotMerged locks the
+// ZoomNeXt regression: Table 3 sits at the TOP of page 11 and Table 5 sits at
+// the TOP of page 12. Both are complete, top-of-page tables whose page-local Y
+// repeats near 0, so the continuation's page-local yDis is NEGATIVE. #18740's
+// "negative yDis ⇒ merge" rule wrongly merges them, which (a) gives Table 3 a
+// false cross-page span (pages 11-12) and (b) drops Table 5 entirely from the
+// output. A genuine cross-page continuation only exists when the ANCHOR table
+// reaches near the BOTTOM of its page; a top-of-page anchor is a complete
+// table, and the next page's top table is a NEW table.
+//
+// Geometry mirrors ZoomNeXt: anchor first box bottom ~81 (near the table top),
+// but the table's true bottom extent ~198; continuation is at the very top of
+// the next page (top 52). Page height 842, median char height 10.
+func TestMergeTablesAcrossPages_TopTablesOnConsecutivePagesNotMerged(t *testing.T) {
+ anchor := pdf.TableItem{
+ Page: 10,
+ Positions: []pdf.Position{
+ {PageNumbers: []int{10}, Left: 226, Right: 457, Top: 61, Bottom: 81}, // first OCR box, near table top
+ {PageNumbers: []int{10}, Left: 117, Right: 473, Top: 189, Bottom: 198}, // true bottom extent of the table
+ },
+ Cells: []pdf.TSRCell{{Text: "No. Model"}},
+ }
+ cont := pdf.TableItem{
+ Page: 11,
+ Positions: []pdf.Position{
+ {PageNumbers: []int{11}, Left: 143, Right: 444, Top: 52, Bottom: 61},
+ },
+ Cells: []pdf.TSRCell{{Text: "Input Scale"}},
+ }
+ pageH := map[int]float64{10: 842, 11: 842}
+ medianH := map[int]float64{10: 10, 11: 10}
+
+ merged := MergeTablesAcrossPages([]pdf.TableItem{anchor, cont}, medianH, pageH)
+ if len(merged) != 2 {
+ t.Fatalf("top-of-page tables on consecutive pages: expected 2 SEPARATE tables (no over-merge), got %d (Table 5 dropped)", len(merged))
+ }
+ // The anchor must NOT have gained page 11.
+ pages := map[int]bool{}
+ for _, p := range merged[0].Positions {
+ for _, pn := range p.PageNumbers {
+ pages[pn] = true
+ }
+ }
+ if pages[11] {
+ t.Errorf("anchor wrongly merged across pages; pages=%v (want only page 10)", pages)
+ }
+}