Skip to content

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

Add RenderVH dispatch hook to ConstructTable for EE V/H tables (#22055)

xugangqiang8e3be03a2b9f
diff --git a/internal/deepdoc/parser/pdf/table/table_construct.go b/internal/deepdoc/parser/pdf/table/table_construct.go
index e56119e2b7..e33272da45 100644
--- a/internal/deepdoc/parser/pdf/table/table_construct.go
+++ b/internal/deepdoc/parser/pdf/table/table_construct.go
@@ -37,7 +37,30 @@ func StripCaptionFromCells(cells []pdf.TSRCell) {
// This pass is intentionally conservative — only clears clearly-non-data text.
}
+// ConstructTable renders a table to HTML. The dispatch to the EE V/H
+// renderer (when item.RenderVH is set) is added by the enterprise build; the
+// OSS build always uses constructTableCore.
func ConstructTable(cells []pdf.TSRCell, boxes []pdf.TextBox, caption string, item *pdf.TableItem) string {
+ if item != nil && item.RenderVH {
+ // Route to the EE V/H renderer. It consumes item.Cells (the builder's
+ // assembled cells), matching the EE tree's prior eeConstructTable
+ // routing, not the page-global cells passed to the OSS core path.
+ return ConstructTableVH(item.Cells, boxes, caption)
+ }
+ return constructTableCore(cells, boxes, caption, item)
+}
+
+// constructTableCore produces an HTML table string from TSR cells and text boxes.
+// Both cells and boxes must be in the same coordinate space (crop pixel space).
+// Fills item.Rows so downstream consumers don't need to re-group cells.
+//
+// Python equivalent: TableStructureRecognizer.construct_table()
+// stripCaptionFromCells clears caption-like text from TSR cells.
+// This catches captions that fillCellTextFromBoxes missed (e.g. text
+// that doesn't match isCaptionBox patterns like "公司差旅费管理办法").
+// Only clears cells whose text matches caption patterns or that contain
+// only number+separator text (pure "1. ", "一、" etc. without data).
+func constructTableCore(cells []pdf.TSRCell, boxes []pdf.TextBox, caption string, item *pdf.TableItem) string {
// Strip caption-like text from cells (defense-in-depth: fillCellTextFromBoxes
// may include caption text that doesn't match isCaptionBox patterns).
StripCaptionFromCells(cells)
diff --git a/internal/deepdoc/parser/pdf/table/table_construct_vh_dispatch_test.go b/internal/deepdoc/parser/pdf/table/table_construct_vh_dispatch_test.go
new file mode 100644
index 0000000000..60977d527b
--- /dev/null
+++ b/internal/deepdoc/parser/pdf/table/table_construct_vh_dispatch_test.go
@@ -0,0 +1,84 @@
+package table
+
+import (
+ "strings"
+ "testing"
+
+ pdf "ragflow/internal/deepdoc/parser/pdf/type"
+)
+
+// vhDispatchGrid is a trivial 2x2 table whose OSS renderer (constructTableCore)
+// produces non-empty HTML. It is used to prove the RenderVH dispatch branch is
+// taken and does not silently fall through to the OSS renderer.
+func vhDispatchGrid() [][]pdf.TSRCell {
+ return [][]pdf.TSRCell{
+ {
+ {Text: "A", X0: 0, Y0: 0, X1: 10, Y1: 10},
+ {Text: "B", X0: 10, Y0: 0, X1: 20, Y1: 10},
+ },
+ {
+ {Text: "C", X0: 0, Y0: 10, X1: 10, Y1: 20},
+ {Text: "D", X0: 10, Y0: 10, X1: 20, Y1: 20},
+ },
+ }
+}
+
+// TestConstructTableRenderVHFalseDelegatesToCore locks that the default
+// (RenderVH == false) path is exactly the OSS dense-grid renderer and keeps
+// its existing behavior, including the cell texts.
+func TestConstructTableRenderVHFalseDelegatesToCore(t *testing.T) {
+ item := &pdf.TableItem{Grid: vhDispatchGrid()}
+ got := ConstructTable(nil, nil, "", item)
+ if got == "" {
+ t.Fatalf("RenderVH=false: expected non-empty HTML, got empty")
+ }
+ for _, want := range []string{"A", "B", "C", "D"} {
+ if !strings.Contains(got, want) {
+ t.Errorf("RenderVH=false: output missing %q\n%s", want, got)
+ }
+ }
+ core := constructTableCore(nil, nil, "", &pdf.TableItem{Grid: vhDispatchGrid()})
+ if got != core {
+ t.Errorf("RenderVH=false did not delegate to constructTableCore:\n got=%s\ncore=%s", got, core)
+ }
+}
+
+// TestConstructTableRenderVHTrueRoutesToVH is the adversarial dispatch check.
+// A non-empty table whose OSS renderer WOULD emit HTML must yield "" when
+// RenderVH is set, because in this build ConstructTableVH is the !ee stub.
+// If the dispatch were missing, ConstructTable would return the non-empty
+// OSS HTML and this test would fail.
+func TestConstructTableRenderVHTrueRoutesToVH(t *testing.T) {
+ item := &pdf.TableItem{Grid: vhDispatchGrid(), RenderVH: true}
+ got := ConstructTable(nil, nil, "", item)
+ if got != "" {
+ t.Fatalf("RenderVH=true must route to ConstructTableVH; expected empty stub output, got %q", got)
+ }
+}
+
+// TestConstructTableVHStubReturnsEmpty guards the OSS/community stub contract:
+// the shared ConstructTable must reference ConstructTableVH so both the ee and
+// !ee builds compile; the !ee build's stub returns empty.
+func TestConstructTableVHStubReturnsEmpty(t *testing.T) {
+ if got := ConstructTableVH(nil, nil, ""); got != "" {
+ t.Errorf("OSS ConstructTableVH stub must return empty; got %q", got)
+ }
+}
+
+// TestTableItemRenderVHZeroByDefault ensures the new field is a no-op signal
+// for the OSS pipeline (nothing sets it).
+func TestTableItemRenderVHZeroByDefault(t *testing.T) {
+ var it pdf.TableItem
+ if it.RenderVH {
+ t.Errorf("RenderVH should default to false")
+ }
+}
+
+// TestTSRCellSpanFieldsZeroByDefault ensures the V/H assembler span fields are
+// a zero-value extension of the shared cell type (OSS never sets them).
+func TestTSRCellSpanFieldsZeroByDefault(t *testing.T) {
+ var c pdf.TSRCell
+ if c.ColSpan != 0 || c.RowSpan != 0 {
+ t.Errorf("ColSpan/RowSpan should default to 0, got %d/%d", c.ColSpan, c.RowSpan)
+ }
+}
diff --git a/internal/deepdoc/parser/pdf/table/table_construct_vh_stub.go b/internal/deepdoc/parser/pdf/table/table_construct_vh_stub.go
new file mode 100644
index 0000000000..37b42d1aff
--- /dev/null
+++ b/internal/deepdoc/parser/pdf/table/table_construct_vh_stub.go
@@ -0,0 +1,15 @@
+//go:build !ee
+
+package table
+
+import (
+ pdf "ragflow/internal/deepdoc/parser/pdf/type"
+)
+
+// ConstructTableVH is the OSS/community stub. The real V/H assembler renderer
+// lives behind the //go:build ee tag in the enterprise tree. No OSS-built
+// TableItem ever sets RenderVH, so this is never reached; it exists only so
+// the shared ConstructTable compiles on both the ee and !ee builds.
+func ConstructTableVH(cells []pdf.TSRCell, boxes []pdf.TextBox, caption string) string {
+ return ""
+}
diff --git a/internal/deepdoc/parser/type/types.go b/internal/deepdoc/parser/type/types.go
index 6f01746e5b..60ba30e21e 100644
--- a/internal/deepdoc/parser/type/types.go
+++ b/internal/deepdoc/parser/type/types.go
@@ -195,6 +195,12 @@ type TableItem struct {
// by the pipeline and used by parity/replay harnesses to map replay
// intermediates (which are keyed by page) back onto the correct table.
Page int
+ // RenderVH marks tables assembled by an EE V/H (vertical/horizontal span)
+ // assembler. It is false for every OSS-built table; the EE tree's
+ // AttachSpanInfo sets it and the EE tree's ConstructTableVH renders those
+ // tables. It is a plain (non-build-tagged) field so the shared
+ // ConstructTable can dispatch without branching on the edition.
+ RenderVH bool
}
// TSRCell represents one table cell from TSR.
@@ -207,6 +213,11 @@ type TSRCell struct {
// so the production table assembly needs it to de-duplicate rows/columns
// the same way Python does.
Score float64
+ // ColSpan / RowSpan record grid span from the v/h edge-clustering
+ // assembler (deepdoc/vision/tsr.py port). 0 means no span. They are set
+ // only by the EE V/H builder; the OSS pipeline leaves them at zero.
+ ColSpan int
+ RowSpan int
}
func (c TSRCell) Bounds() (float64, float64, float64, float64) {