RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
refactor(deepdoc): drop dead covered-slice+sort in GroupCells span injection (#21954)
xugangqiang1958eb8f4a72
diff --git a/internal/deepdoc/parser/pdf/table/deepdoc_table_builder.go b/internal/deepdoc/parser/pdf/table/deepdoc_table_builder.goindex 005a724d4c..343ab80d1f 100644--- a/internal/deepdoc/parser/pdf/table/deepdoc_table_builder.go+++ b/internal/deepdoc/parser/pdf/table/deepdoc_table_builder.go@@ -3,7 +3,6 @@ package tableimport ("context""image"- "sort""strings"pdf "ragflow/internal/deepdoc/parser/pdf/type"@@ -115,16 +114,42 @@ func (b *DeepDocTableBuilder) GroupCells(cells []pdf.TSRCell) [][]pdf.TSRCell {}// 5. Span injection.+ //+ // For each TSR "table spanning cell", locate the top-left grid cell of+ // the covered rectangle (the lexicographically smallest (row, col) whose+ // centre lies inside the span bbox) and extend its bbox to the span's+ // bbox, tagging it with the span label. A cell centre ∈ span bbox iff+ // its column centre ∈ [sp.X0,sp.X1] AND its row centre ∈ [sp.Y0,sp.Y1],+ // because every cell in a row shares the row boundary and every cell in+ // a column shares the column boundary. We track the minimum (r, c)+ // directly instead of allocating a covered slice and sorting it — the+ // covered set is recomputed later by CalSpans from this extended bbox,+ // so that slice was dead work. The origin must stay the covered+ // rectangle's top-left: CalSpans derives colspan/rowspan and the+ // `covered` (hidden) map from it, so a non-top-left origin would make+ // the rendered cell span a different rectangle than the one hidden.+ //+ // The remaining covered cells KEEP their row×column bbox. Python's+ // construct_table assigns every box to a cell by its TSR row/column+ // label (R/C) regardless of spanning cells, then __cal_spans folds the+ // covered cells' text into the span origin cell at render time. If Go+ // zeroed these bboxes here, FillCellTextFromBoxesWithRows would skip+ // them, their text would never reach the span cell, and orphan-row/+ // column cleanup would drop the covered rows/columns — the+ // dell/prodeploy/screenshot real-PDF parity gap (11→6 rows).for _, sp := range spans {- type cellIdx struct{ r, c int }- var covered []cellIdx+ firstR, firstC := -1, -1for ri := range grid {+ cy := (grid[ri][0].Y0 + grid[ri][0].Y1) / 2+ if cy < sp.Y0 || cy > sp.Y1 {+ continue+ }for cj := range grid[ri] {- cell := grid[ri][cj]- cx := (cell.X0 + cell.X1) / 2- cy := (cell.Y0 + cell.Y1) / 2- if cx >= sp.X0 && cx <= sp.X1 && cy >= sp.Y0 && cy <= sp.Y1 {- covered = append(covered, cellIdx{ri, cj})+ cx := (grid[ri][cj].X0 + grid[ri][cj].X1) / 2+ if cx >= sp.X0 && cx <= sp.X1 {+ if firstR == -1 || ri < firstR || (ri == firstR && cj < firstC) {+ firstR, firstC = ri, cj+ }}}}@@ -133,31 +158,16 @@ func (b *DeepDocTableBuilder) GroupCells(cells []pdf.TSRCell) [][]pdf.TSRCell {// model's box edge missed the neighbour row) is still a real TSR// span and must be labelled, not silently dropped. A 1-cell span// renders as rowspan=1/colspan=1 (harmless) but keeps the TSR- // recognition visible downstream. Only skip when the box overlaps- // no grid cell at all (genuine coordinate mismatch).- if len(covered) == 0 {+ // recognition visible downstream. Only skip when no grid cell centre+ // falls inside the box at all (genuine coordinate mismatch).+ if firstR == -1 {continue}- sort.Slice(covered, func(a, b int) bool {- if covered[a].r != covered[b].r {- return covered[a].r < covered[b].r- }- return covered[a].c < covered[b].c- })- first := covered[0]- grid[first.r][first.c].X0 = sp.X0- grid[first.r][first.c].Y0 = sp.Y0- grid[first.r][first.c].X1 = sp.X1- grid[first.r][first.c].Y1 = sp.Y1- grid[first.r][first.c].Label = sp.Label- // The remaining covered cells KEEP their row×column bbox. Python's- // construct_table assigns every box to a cell by its TSR row/column- // label (R/C) regardless of spanning cells, then __cal_spans folds the- // covered cells' text into the span origin cell at render time. If Go- // zeroed these bboxes here, FillCellTextFromBoxesWithRows would skip- // them, their text would never reach the span cell, and orphan-row/- // column cleanup would drop the covered rows/columns — the- // dell/prodeploy/screenshot real-PDF parity gap (11→6 rows).+ grid[firstR][firstC].X0 = sp.X0+ grid[firstR][firstC].Y0 = sp.Y0+ grid[firstR][firstC].X1 = sp.X1+ grid[firstR][firstC].Y1 = sp.Y1+ grid[firstR][firstC].Label = sp.Label}return griddiff --git a/internal/deepdoc/parser/pdf/table/deepdoc_table_builder_test.go b/internal/deepdoc/parser/pdf/table/deepdoc_table_builder_test.goindex 29261a54e1..dd7d644fa9 100644--- a/internal/deepdoc/parser/pdf/table/deepdoc_table_builder_test.go+++ b/internal/deepdoc/parser/pdf/table/deepdoc_table_builder_test.go@@ -161,6 +161,72 @@ func TestDeepDocTableBuildService_GroupCells_NoRows(t *testing.T) {}}+func TestDeepDocTableBuildService_GroupCells_SpanOriginIsTopLeft(t *testing.T) {+ b := &DeepDocTableBuilder{}++ // 3×3 table. Span bbox covers columns {1,2} and rows {1,2} but does+ // NOT touch column 0 or row 0, so the span origin must be the+ // top-left of the *covered* rectangle, i.e. grid[1][1] — never an+ // arbitrary covered cell and never the grid's own corner (0,0).+ //+ // This guards the invariant CalSpans relies on: the labelled origin+ // equals the covered rectangle's top-left, otherwise colspan/rowspan+ // and the `covered` (hidden) map diverge and the rendered HTML breaks.+ cells := []pdf.TSRCell{+ {X0: 0, Y0: 0, X1: 300, Y1: 150, Label: "table"},+ // rows: 0-50, 50-100, 100-150 (centers 25, 75, 125)+ {X0: 0, Y0: 0, X1: 300, Y1: 50, Label: "table row"},+ {X0: 0, Y0: 50, X1: 300, Y1: 100, Label: "table row"},+ {X0: 0, Y0: 100, X1: 300, Y1: 150, Label: "table row"},+ // cols: 0-100, 100-200, 200-300 (centers 50, 150, 250)+ {X0: 0, Y0: 0, X1: 100, Y1: 150, Label: "table column"},+ {X0: 100, Y0: 0, X1: 200, Y1: 150, Label: "table column"},+ {X0: 200, Y0: 0, X1: 300, Y1: 150, Label: "table column"},+ // span bbox X∈[120,280] Y∈[60,140] → covers cols {1,2}, rows {1,2}+ {X0: 120, Y0: 60, X1: 280, Y1: 140, Label: "table spanning cell"},+ }++ grid := b.GroupCells(cells)+ if len(grid) != 3 || len(grid[0]) != 3 {+ t.Fatalf("expected 3×3 grid, got %d×%d", len(grid), len(grid[0]))+ }++ // Exactly one spanning-labelled cell, and it must be [1][1].+ spanCount := 0+ for r := range grid {+ for c := range grid[r] {+ if strings.Contains(strings.ToLower(grid[r][c].Label), "spanning") {+ spanCount+++ if r != 1 || c != 1 {+ t.Errorf("span origin at [%d][%d], want [1][1] (top-left of covered)", r, c)+ }+ }+ }+ }+ if spanCount != 1 {+ t.Fatalf("expected exactly 1 spanning-labelled cell, got %d", spanCount)+ }++ // The origin keeps the span's full bbox.+ o := grid[1][1]+ if o.X0 != 120 || o.X1 != 280 || o.Y0 != 60 || o.Y1 != 140 {+ t.Errorf("origin bbox = (%.0f,%.0f,%.0f,%.0f), want (120,60,280,140)",+ o.X0, o.Y0, o.X1, o.Y1)+ }++ // grid[0][0] is outside the span → must NOT be labelled spanning.+ if strings.Contains(strings.ToLower(grid[0][0].Label), "spanning") {+ t.Error("grid[0][0] is outside the span but got 'spanning' label")+ }++ // Covered cells ([1][2],[2][1],[2][2]) keep their own row×col bbox.+ for _, pos := range [][2]int{{1, 2}, {2, 1}, {2, 2}} {+ if isZeroCell(grid[pos[0]][pos[1]]) {+ t.Errorf("grid[%d][%d] must keep its own bbox (covered, not zeroed)", pos[0], pos[1])+ }+ }+}+func TestDeepDocTableBuildService_GroupCells_NoColumns(t *testing.T) {b := &DeepDocTableBuilder{}// Table + rows but no column cells → each row gets 1 wide column.