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
deepdoc/pdf: extend TableBuilder with FinalizeGrid and AttachSpanInfo (#21980)
xugangqiang8cab68a9a3b0
diff --git a/internal/deepdoc/parser/pdf/builder_iface_test.go b/internal/deepdoc/parser/pdf/builder_iface_test.gonew file mode 100644index 0000000000..3f96ec3c38--- /dev/null+++ b/internal/deepdoc/parser/pdf/builder_iface_test.go@@ -0,0 +1,17 @@+package pdf++import (+ "ragflow/internal/deepdoc/parser/pdf/table"+ pdf "ragflow/internal/deepdoc/parser/pdf/type"+)++// Compile-time guarantees that the pdf-package TableBuilder fakes (and the+// production DeepDocTableBuilder reachable from here) satisfy the+// interface after the FinalizeGrid / AttachSpanInfo extension. Missing a+// method on any implementer fails the build (RED). The cgo&&manual fake+// PythonIntermediateTableBuilder is covered by the same extension in+// replay_analyzer_test.go but is not compiled in the default build.+var (+ _ pdf.TableBuilder = (*staticTableBuilder)(nil)+ _ pdf.TableBuilder = (*table.DeepDocTableBuilder)(nil)+)diff --git a/internal/deepdoc/parser/pdf/process_one_table_callsite_test.go b/internal/deepdoc/parser/pdf/process_one_table_callsite_test.gonew file mode 100644index 0000000000..33c806b766--- /dev/null+++ b/internal/deepdoc/parser/pdf/process_one_table_callsite_test.go@@ -0,0 +1,94 @@+package pdf++import (+ "image"+ "testing"++ tbl "ragflow/internal/deepdoc/parser/pdf/table"+ pdf "ragflow/internal/deepdoc/parser/pdf/type"+)++// spyTableBuilder wraps staticTableBuilder and records whether the+// post-grouping extension points (FinalizeGrid / AttachSpanInfo) were+// invoked by processOneTable, and with which arguments. It deliberately+// returns a distinguishable grid / mutates the item so the test can prove+// the caller actually uses the return value and the very item it returns.+type spyTableBuilder struct {+ staticTableBuilder+ finalizeCalled bool+ finalizeInGrid [][]pdf.TSRCell+ finalizeBoxes int+ attachCalled bool+}++// FinalizeGrid records the call and returns a sentinel grid so the test can+// prove processOneTable stores THIS return value as the table grid.+func (b *spyTableBuilder) FinalizeGrid(annotGrid [][]pdf.TSRCell, boxes []pdf.TextBox) [][]pdf.TSRCell {+ b.finalizeCalled = true+ b.finalizeInGrid = annotGrid+ b.finalizeBoxes = len(boxes)+ return [][]pdf.TSRCell{{pdf.TSRCell{Text: "FINALIZED"}}}+}++// AttachSpanInfo records the call and mutates the item so the test can prove+// the mutation survives into the item processOneTable returns.+func (b *spyTableBuilder) AttachSpanInfo(item *pdf.TableItem) {+ b.attachCalled = true+ item.Rows = append(item.Rows, []string{"ATTACHED"})+}++// TestProcessOneTableWiresFinalizeGridAndAttachSpanInfo asserts the extension+// points added to TableBuilder are actually invoked by the shared table+// assembly path. Without the call site, the production DeepDoc builder keeps+// its identity/no-op behavior but the interface methods are dead declarations+// in OSS — this test makes that gap falsifiable.+func TestProcessOneTableWiresFinalizeGridAndAttachSpanInfo(t *testing.T) {+ autoRotate := false+ cfg := pdf.DefaultParserConfig()+ cfg.AutoRotateTables = &autoRotate+ p := NewParser(cfg)++ pageImg := image.NewRGBA(image.Rect(0, 0, 320, 220))+ boxes := []pdf.TextBox{+ {X0: 10, X1: 60, Top: 10, Bottom: 30, Text: "cell", LayoutType: pdf.LayoutTypeTable},+ }+ match := tbl.TableMatch{+ Region: pdf.DLARegion{X0: 10, Y0: 10, X1: 210, Y1: 110, Label: pdf.LayoutTypeTable},+ BoxIdx: []int{0},+ }+ inner := &staticTableBuilder{+ cells: []pdf.TSRCell{{X0: 10, Y0: 20, X1: 60, Y1: 80, Label: "table row"}},+ }+ spy := &spyTableBuilder{staticTableBuilder: *inner}++ item := p.processOneTable(t.Context(), pageImg, boxes, 0, &orientationScoringDoc{}, spy, match, pdf.DlaScale)++ // Call site 1: FinalizeGrid must be invoked with the assembled grid + boxes.+ if !spy.finalizeCalled {+ t.Fatalf("processOneTable did not call TableBuilder.FinalizeGrid")+ }+ if spy.finalizeBoxes == 0 {+ t.Fatalf("FinalizeGrid received 0 boxes; expected the table's crop-space boxes")+ }+ // The assembled grid (pre-FinalizeGrid) must be the one handed to FinalizeGrid.+ if len(spy.finalizeInGrid) == 0 {+ t.Fatalf("FinalizeGrid received an empty grid; expected the assembled grid")+ }+ // The caller must store FinalizeGrid's RETURN value as the item grid, not+ // the pre-FinalizeGrid grid.+ if len(item.Grid) != 1 || len(item.Grid[0]) != 1 || item.Grid[0][0].Text != "FINALIZED" {+ t.Fatalf("item.Grid was not produced by FinalizeGrid: %#v", item.Grid)+ }++ // Call site 2: AttachSpanInfo must be invoked with the assembled item.+ if !spy.attachCalled {+ t.Fatalf("processOneTable did not call TableBuilder.AttachSpanInfo")+ }+ // The mutation to the item inside AttachSpanInfo must surface in the+ // returned item. This is the falsifiable proof that AttachSpanInfo was+ // called on the item processOneTable actually returns (not a throwaway or+ // a pre-copy), since TableItem is a value type copied on return.+ if len(item.Rows) != 1 || item.Rows[0][0] != "ATTACHED" {+ t.Fatalf("AttachSpanInfo did not mutate the assembled item: %#v", item.Rows)+ }+}diff --git a/internal/deepdoc/parser/pdf/replay_analyzer_test.go b/internal/deepdoc/parser/pdf/replay_analyzer_test.goindex a721d17160..73158565e5 100644--- a/internal/deepdoc/parser/pdf/replay_analyzer_test.go+++ b/internal/deepdoc/parser/pdf/replay_analyzer_test.go@@ -221,6 +221,15 @@ func (b *PythonIntermediateTableBuilder) GroupCells(cells []pdf.TSRCell) [][]pdfreturn (&table.DeepDocTableBuilder{}).GroupCells(cells)}+// FinalizeGrid returns annotGrid unchanged (identity). The fake carries no+// deferred OCR-overlay behavior, matching the production DeepDoc builder.+func (b *PythonIntermediateTableBuilder) FinalizeGrid(annotGrid [][]pdf.TSRCell, boxes []pdf.TextBox) [][]pdf.TSRCell {+ return annotGrid+}++// AttachSpanInfo is a no-op for the fake builder.+func (b *PythonIntermediateTableBuilder) AttachSpanInfo(item *pdf.TableItem) {}+func (b *PythonIntermediateTableBuilder) Name() string { return "py-intermediate" }func maxf(a, b float64) float64 {diff --git a/internal/deepdoc/parser/pdf/table/builder_finalize_grid_test.go b/internal/deepdoc/parser/pdf/table/builder_finalize_grid_test.gonew file mode 100644index 0000000000..d73d8bf835--- /dev/null+++ b/internal/deepdoc/parser/pdf/table/builder_finalize_grid_test.go@@ -0,0 +1,61 @@+package table++import (+ "testing"++ pdf "ragflow/internal/deepdoc/parser/pdf/type"+)++// Compile-time guarantees that every TableBuilder implemented inside the+// table package satisfies the interface after the FinalizeGrid /+// AttachSpanInfo extension. If any implementer misses a method, this file+// fails to compile (RED) and the build stays red until GREEN.+var (+ _ pdf.TableBuilder = (*DeepDocTableBuilder)(nil)+ _ pdf.TableBuilder = (*MockTableBuilder)(nil)+)++// TestDeepDocTableBuilderFinalizeGridIdentity verifies the OSS builder is+// the identity function: it returns the annotated grid unchanged and never+// allocates for a nil input.+func TestDeepDocTableBuilderFinalizeGridIdentity(t *testing.T) {+ b := NewDeepDocTableBuilder(nil)++ annotGrid := [][]pdf.TSRCell{+ {{Text: "a"}},+ {{Text: "b"}},+ }+ got := b.FinalizeGrid(annotGrid, nil)+ if len(got) != len(annotGrid) {+ t.Fatalf("FinalizeGrid changed row count: got %d want %d", len(got), len(annotGrid))+ }+ for r := range annotGrid {+ if len(got[r]) != len(annotGrid[r]) {+ t.Fatalf("row %d column count changed", r)+ }+ if got[r][0].Text != annotGrid[r][0].Text {+ t.Errorf("row %d text mutated: got %q want %q", r, got[r][0].Text, annotGrid[r][0].Text)+ }+ }++ // nil grid must round-trip as nil, not a zero-value allocation.+ if out := b.FinalizeGrid(nil, nil); out != nil {+ t.Errorf("FinalizeGrid(nil) = %#v, want nil", out)+ }+}++// TestDeepDocTableBuilderAttachSpanInfoNoop verifies the OSS builder's+// AttachSpanInfo is a safe no-op: it never panics (even on a nil item) and+// never mutates the item.+func TestDeepDocTableBuilderAttachSpanInfoNoop(t *testing.T) {+ b := NewDeepDocTableBuilder(nil)++ // nil item must not panic.+ b.AttachSpanInfo(nil)++ item := &pdf.TableItem{Rows: [][]string{{"x"}}}+ b.AttachSpanInfo(item)+ if len(item.Rows) != 1 || item.Rows[0][0] != "x" {+ t.Errorf("AttachSpanInfo mutated the item: %+v", item.Rows)+ }+}diff --git a/internal/deepdoc/parser/pdf/table/deepdoc_table_builder.go b/internal/deepdoc/parser/pdf/table/deepdoc_table_builder.goindex 343ab80d1f..df21daa22e 100644--- a/internal/deepdoc/parser/pdf/table/deepdoc_table_builder.go+++ b/internal/deepdoc/parser/pdf/table/deepdoc_table_builder.go@@ -177,3 +177,16 @@ func (b *DeepDocTableBuilder) GroupCells(cells []pdf.TSRCell) [][]pdf.TSRCell {func overlapsY(a, b pdf.TSRCell) bool {return a.Y0 < b.Y1 && a.Y1 > b.Y0}++// FinalizeGrid returns annotGrid unchanged. The DeepDoc builder folds OCR+// text into the grid inline during processOneTable's box-RC grouping, so+// there is no deferred work to perform here. An EE V/H builder that defers+// the OCR overlay overrides this with the real overlay.+func (b *DeepDocTableBuilder) FinalizeGrid(annotGrid [][]pdf.TSRCell, boxes []pdf.TextBox) [][]pdf.TSRCell {+ return annotGrid+}++// AttachSpanInfo is a no-op for the DeepDoc builder: its TableItem carries+// no builder-specific span geometry. An EE V/H builder overrides this to+// attach V/H span info.+func (b *DeepDocTableBuilder) AttachSpanInfo(item *pdf.TableItem) {}diff --git a/internal/deepdoc/parser/pdf/table/table_annotate_test.go b/internal/deepdoc/parser/pdf/table/table_annotate_test.goindex a025847827..14d9b28815 100644--- a/internal/deepdoc/parser/pdf/table/table_annotate_test.go+++ b/internal/deepdoc/parser/pdf/table/table_annotate_test.go@@ -585,6 +585,15 @@ func (m *MockTableBuilder) GroupCells(cells []pdf.TSRCell) [][]pdf.TSRCell {return nil}+// FinalizeGrid returns annotGrid unchanged (identity). The fake carries no+// deferred OCR-overlay behavior, matching the production DeepDoc builder.+func (m *MockTableBuilder) FinalizeGrid(annotGrid [][]pdf.TSRCell, boxes []pdf.TextBox) [][]pdf.TSRCell {+ return annotGrid+}++// AttachSpanInfo is a no-op for the fake builder.+func (m *MockTableBuilder) AttachSpanInfo(item *pdf.TableItem) {}+// ── writeTableAnnotations unit tests ──────────────────────────────────func TestWriteTableAnnotations_WriteBack(t *testing.T) {diff --git a/internal/deepdoc/parser/pdf/table_extract.go b/internal/deepdoc/parser/pdf/table_extract.goindex e818aa2ce2..754484bf6f 100644--- a/internal/deepdoc/parser/pdf/table_extract.go+++ b/internal/deepdoc/parser/pdf/table_extract.go@@ -200,6 +200,10 @@ func (p *Parser) processOneTable(ctx context.Context, pageImg image.Image, boxes}}}+ // Let the active builder finalize the grid (e.g. fold in a deferred OCR+ // overlay). The DeepDoc builder is the identity function, so OSS keeps its+ // current behavior unchanged.+ grid = tb.FinalizeGrid(grid, boxInCrop)item := pdf.TableItem{Cells: cells, Grid: grid, Positions: positions,Scale: scale, CropOffX: cropOffX, CropOffY: cropOffY,@@ -208,6 +212,10 @@ func (p *Parser) processOneTable(ctx context.Context, pageImg image.Image, boxesPage: pageNum,}tbl.WriteTableAnnotations(boxes, tm.BoxIdx, cells, scale, cropOffX, cropOffY, tb)+ // Let the active builder attach builder-specific span geometry onto the+ // assembled item. The DeepDoc builder is a no-op, so OSS keeps its current+ // behavior unchanged.+ tb.AttachSpanInfo(&item)return item}diff --git a/internal/deepdoc/parser/pdf/table_extract_test.go b/internal/deepdoc/parser/pdf/table_extract_test.goindex 7e00b7977b..46391034d2 100644--- a/internal/deepdoc/parser/pdf/table_extract_test.go+++ b/internal/deepdoc/parser/pdf/table_extract_test.go@@ -80,6 +80,15 @@ func (b *staticTableBuilder) GroupCells(cells []pdf.TSRCell) [][]pdf.TSRCell {return [][]pdf.TSRCell{{cells[0]}}}+// FinalizeGrid returns annotGrid unchanged (identity). The fake carries no+// deferred OCR-overlay behavior, matching the production DeepDoc builder.+func (b *staticTableBuilder) FinalizeGrid(annotGrid [][]pdf.TSRCell, boxes []pdf.TextBox) [][]pdf.TSRCell {+ return annotGrid+}++// AttachSpanInfo is a no-op for the fake builder.+func (b *staticTableBuilder) AttachSpanInfo(item *pdf.TableItem) {}+func TestProcessOneTable_AutoRotateNormalizesCellBounds(t *testing.T) {autoRotate := truecfg := pdf.DefaultParserConfig()@@ -249,6 +258,15 @@ func (b *twoRowTableBuilder) GroupCells(cells []pdf.TSRCell) [][]pdf.TSRCell {return rows}+// FinalizeGrid returns annotGrid unchanged (identity). The fake carries no+// deferred OCR-overlay behavior, matching the production DeepDoc builder.+func (b *twoRowTableBuilder) FinalizeGrid(annotGrid [][]pdf.TSRCell, boxes []pdf.TextBox) [][]pdf.TSRCell {+ return annotGrid+}++// AttachSpanInfo is a no-op for the fake builder.+func (b *twoRowTableBuilder) AttachSpanInfo(item *pdf.TableItem) {}+func TestProcessOneTable_HandlesTextFragments(t *testing.T) {for _, tc := range []struct {name stringdiff --git a/internal/deepdoc/parser/type/types.go b/internal/deepdoc/parser/type/types.goindex f590d854ec..6f01746e5b 100644--- a/internal/deepdoc/parser/type/types.go+++ b/internal/deepdoc/parser/type/types.go@@ -387,6 +387,18 @@ type TableBuilder interface {Name() stringDetectCells(ctx context.Context, cropped image.Image) ([]TSRCell, error)GroupCells(cells []TSRCell) [][]TSRCell+ // FinalizeGrid turns the annotated structure grid plus the table's+ // OCR/text boxes into the final grid. The DeepDoc builder already+ // folds OCR text into the grid inline during processOneTable's box-RC+ // grouping, so its implementation is the identity function; an EE V/H+ // builder that defers OCR overlay to after structure grouping performs+ // the overlay here.+ FinalizeGrid(annotGrid [][]TSRCell, boxes []TextBox) [][]TSRCell+ // AttachSpanInfo attaches any builder-specific span geometry onto the+ // assembled table item. The DeepDoc builder's TableItem carries no+ // such geometry, so its implementation is a no-op; an EE V/H builder+ // uses this step to attach V/H span info.+ AttachSpanInfo(item *TableItem)}// Rectangular is any 2D axis-aligned rectangle that can report its bounds.