Skip to content

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

refactor(parser): carry spreadsheet merge geometry through chunkers (#21463)

jay77721b5e8a0a2c3ad
diff --git a/internal/entity/table.go b/internal/entity/table.go
index 1d4e2d5ad3..66c2b3b7fb 100644
--- a/internal/entity/table.go
+++ b/internal/entity/table.go
@@ -60,16 +60,19 @@ type TableCell struct {
// empty; readers must skip it (the real content is at the origin slot).
Covered bool `json:"covered,omitempty"`
// OriginRow / OriginCol name the slot whose span produced this Covered
- // marker. They are diagnostic only and are not serialized.
- OriginRow int `json:"-"`
- OriginCol int `json:"-"`
+ // marker. They are serialized because the table splitter clamps a header
+ // span after the parser payload has round-tripped through JSON; zero is a
+ // valid origin, so omitempty is safe (it decodes back to zero).
+ OriginRow int `json:"origin_row,omitempty"`
+ OriginCol int `json:"origin_col,omitempty"`
}
-// CellTexts returns the row-major cell texts, skipping Covered positions
+// CellTexts returns the raw row-major cell texts, skipping Covered positions
// (their content lives at the origin). For a grid with no merges this is
-// exactly the legacy Rows: same strings, same row lengths. Consumers that only
-// care about the textual content use this view; consumers that need merge
-// geometry read Grid directly.
+// exactly the legacy Rows: same strings, same row lengths. Consumers that need
+// merged values projected into the covered slots (the table chunker's
+// column-aligned view, or QA's all-rows-are-data view) should call
+// tableutil.ResolveRows with the appropriate mode instead.
func (td *TableData) CellTexts() [][]string {
if td == nil {
return nil
diff --git a/internal/ingestion/component/chunker/general.go b/internal/ingestion/component/chunker/general.go
index 2eece16f45..fb4028d8f8 100644
--- a/internal/ingestion/component/chunker/general.go
+++ b/internal/ingestion/component/chunker/general.go
@@ -641,7 +641,11 @@ func generalContextSourceText(doc schema.ChunkDoc) (string, bool) {
if td == nil {
return "", false
}
- rows, headerCount := td.CellTexts(), td.HeaderRows
+ // RowsData is the general-context view: data-origin row spans are
+ // resolved at the origin column, header-origin spans stay out of data
+ // rows, and a colspan is not duplicated. CellTexts would skip every
+ // Covered slot, dropping a data rowspan from the context entirely.
+ rows, headerCount := tableutil.ResolveRows(td, tableutil.RowsData), td.HeaderRows
lines := make([]string, 0, len(rows)-headerCount)
for _, row := range rows[headerCount:] {
cells := make([]string, 0, len(row))
diff --git a/internal/ingestion/component/chunker/general_test.go b/internal/ingestion/component/chunker/general_test.go
index a037cd72fb..c28f82460e 100644
--- a/internal/ingestion/component/chunker/general_test.go
+++ b/internal/ingestion/component/chunker/general_test.go
@@ -1417,3 +1417,64 @@ func TestGeneralChunkerSpreadsheetSplitDropsNonSpreadsheetPositions(t *testing.T
})
}
}
+
+// TestGeneralContextSourceTextProjectsMergedRows pins the general-context
+// projection: data-origin row spans stay visible, header-origin spans do not
+// leak into data rows, and a colspan is not duplicated.
+func TestGeneralContextSourceTextProjectsMergedRows(t *testing.T) {
+ cases := []struct {
+ name string
+ td *entity.TableData
+ want string
+ }{
+ {
+ name: "data rowspan",
+ td: &entity.TableData{HeaderRows: 1, Grid: [][]entity.TableCell{
+ {{Text: "Group"}, {Text: "Item"}},
+ {{Text: "G1", RowSpan: 2}, {Text: "x"}},
+ {{Covered: true}, {Text: "y"}},
+ }},
+ want: "G1 x\nG1 y",
+ },
+ {
+ name: "header-origin rowspan",
+ td: &entity.TableData{HeaderRows: 1, Grid: [][]entity.TableCell{
+ {{Text: "H", RowSpan: 2}, {Text: "Name"}},
+ {{Covered: true}, {Text: "Alice"}},
+ }},
+ want: "Alice",
+ },
+ {
+ name: "data colspan",
+ td: &entity.TableData{HeaderRows: 1, Grid: [][]entity.TableCell{
+ {{Text: "A"}, {Text: "B"}, {Text: "C"}},
+ {{Text: "Wide", ColSpan: 2}, {Covered: true}, {Text: "z"}},
+ }},
+ want: "Wide z",
+ },
+ {
+ name: "rowspan and colspan block",
+ td: &entity.TableData{HeaderRows: 1, Grid: [][]entity.TableCell{
+ {{Text: "A"}, {Text: "B"}, {Text: "C"}},
+ {{Text: "Wide", RowSpan: 2, ColSpan: 2}, {Covered: true}, {Text: "x"}},
+ {{Covered: true}, {Covered: true}, {Text: "y"}},
+ }},
+ want: "Wide x\nWide y",
+ },
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ got, ok := generalContextSourceText(schema.ChunkDoc{
+ DocType: "table",
+ Sheet: "Sheet1",
+ TableData: tc.td,
+ })
+ if !ok {
+ t.Fatal("generalContextSourceText did not accept the spreadsheet table")
+ }
+ if got != tc.want {
+ t.Fatalf("context = %q, want %q", got, tc.want)
+ }
+ })
+ }
+}
diff --git a/internal/ingestion/component/chunker/qa.go b/internal/ingestion/component/chunker/qa.go
index e10c1efb6a..88fac3b3cc 100644
--- a/internal/ingestion/component/chunker/qa.go
+++ b/internal/ingestion/component/chunker/qa.go
@@ -428,7 +428,7 @@ func extractQAJSON(items []schema.ChunkDoc, fileType string) []qaPair {
// before this wire must be re-parsed rather than re-chunked.
var rows [][]string
if td != nil {
- rows = td.CellTexts()
+ rows = tableutil.ResolveRows(td, tableutil.RowsQA)
}
if len(rows) > 0 {
tmp = qaPairsFromRows(rows, strictCSV, item.Positions, item.SheetIndex != nil || item.Sheet != "")
@@ -464,7 +464,7 @@ func extractQATable(htmlStr string, strictPairs bool) []qaPair {
if err != nil || td == nil {
return nil
}
- return qaPairsFromRows(td.CellTexts(), strictPairs, nil, false)
+ return qaPairsFromRows(tableutil.ResolveRows(td, tableutil.RowsQA), strictPairs, nil, false)
}
// qaPairsFromRows builds the pairs of one table: the first two non-empty
diff --git a/internal/ingestion/component/chunker/qa_table_test.go b/internal/ingestion/component/chunker/qa_table_test.go
index 4a551b8bbf..69ff8ccdd6 100644
--- a/internal/ingestion/component/chunker/qa_table_test.go
+++ b/internal/ingestion/component/chunker/qa_table_test.go
@@ -27,6 +27,14 @@ func TestExtractQATableFollowsStructure(t *testing.T) {
html: `<table><tr><td colspan="2">q</td><td>a</td></tr></table>`,
want: [][2]string{{"q", "a"}},
},
+ {
+ // A rowspan answer covers the next question too. The QA view
+ // resolves the covered slot instead of dropping the row.
+ name: "rowspan",
+ html: `<table><tr><td>q1</td><td rowspan="2">a1</td></tr>` +
+ `<tr><td>q2</td></tr></table>`,
+ want: [][2]string{{"q1", "a1"}, {"q2", "a1"}},
+ },
{
name: "missing </td>",
html: "<table><tr><td>q<td>a</tr></table>",
diff --git a/internal/ingestion/component/chunker/table.go b/internal/ingestion/component/chunker/table.go
index 3bfae3bc1e..0da8aa90ce 100644
--- a/internal/ingestion/component/chunker/table.go
+++ b/internal/ingestion/component/chunker/table.go
@@ -133,101 +133,20 @@ func tableItems(items, chunks []schema.ChunkDoc) []schema.ChunkDoc {
}
// expandHTMLTableRows turns one table payload into one chunk per data row.
-// tableChunkRows projects a structured TableData to row-major cell texts while
-// preserving one value per logical column. Unlike CellTexts it does NOT drop
-// Covered slots: a slot engulfed by a span is resolved back to the spanning
-// origin's text so each projected row keeps the same column count as the header
-// and stays aligned with it. CellTexts would drop a spanned continuation column
-// and shift the surviving cells under the wrong headers — exactly the mis-match
-// a merged HTML table hits when it is expanded per row.
+// It reads the table's canonical merge geometry through tableutil.RowsTable:
+// data-origin spans are repeated across their covered slots so each projected
+// row keeps the same column count as the header, while a header-origin span
+// stays inside the header block and is not stamped into a data row.
//
-// Resolution works from the grid alone. As rows are scanned top-to-bottom a
-// per-column "active rowspan" remembers the text each ancestor rowspan still
-// contributes, and within a row a running colspan remembers the text of a just
-// seen wide origin. This deliberately avoids OriginRow/OriginCol, which are
-// json:"-" and therefore absent after the TableData is serialized to JSON and
-// read back — the path expandHTMLTableRows actually consumes.
-func tableChunkRows(td *entity.TableData) [][]string {
- if td == nil {
- return nil
- }
- out := make([][]string, 0, len(td.Grid))
- rowspanText := []string{}
- rowspanLeft := []int{}
- for _, row := range td.Grid {
- texts := make([]string, 0, len(row))
- colspanLeft := 0
- colspanText := ""
- for c, cell := range row {
- for len(rowspanText) <= c {
- rowspanText = append(rowspanText, "")
- rowspanLeft = append(rowspanLeft, 0)
- }
- if cell.Covered {
- if rowspanLeft[c] > 0 {
- texts = append(texts, rowspanText[c])
- } else {
- texts = append(texts, colspanText)
- }
- } else {
- texts = append(texts, cell.Text)
- // A merged cell that spans rows AND columns must register its
- // active rowspan across the full [c, c+ColSpan) span, not just
- // the origin column. Its continuation rows own every covered
- // column, so each must resolve back to the origin text; had we
- // only registered the start column, the extra covered columns
- // would read as blank and the row would lose those fields.
- // An unset ColSpan (0) normalizes to 1 so a row-only span still
- // registers its single start column.
- if cell.RowSpan > 1 {
- colSpan := cell.ColSpan
- if colSpan < 1 {
- colSpan = 1
- }
- for cc := c; cc < c+colSpan; cc++ {
- for len(rowspanText) <= cc {
- rowspanText = append(rowspanText, "")
- rowspanLeft = append(rowspanLeft, 0)
- }
- rowspanText[cc] = cell.Text
- rowspanLeft[cc] = cell.RowSpan
- }
- }
- if cell.ColSpan > 1 {
- colspanLeft = cell.ColSpan
- colspanText = cell.Text
- }
- }
- if colspanLeft > 0 {
- colspanLeft--
- }
- }
- for c := range rowspanLeft {
- if rowspanLeft[c] > 0 {
- rowspanLeft[c]--
- }
- }
- out = append(out, texts)
- }
- return out
-}
-
-// expandHTMLTableRows turns one table payload into one chunk per data row.
-// It reads the structured TableData contract the parser emits on
-// ChunkDoc.TableData; items without TableData (pre-upgrade HTML markup or
-// non-table records) pass through unchanged as a single chunk. Every emitted
-// chunk carries its content rendered as an HTML <table> on Text, so the
-// persisted content and the chunk id (a hash of Text) describe the same bytes
-// and the UI/index can display a real table; the structured TableData is
-// dropped here and never serialized to the index. Header-only and fallback
-// chunks keep the whole table's HTML (merge geometry intact); per-row chunks
-// carry a flat header+row sub-table (merge-free, see cellGrid).
+// Items without TableData pass through unchanged as a single chunk. A
+// header-only or fallback chunk keeps the whole table's canonical HTML; each
+// per-row chunk carries a flat header+row sub-table (see cellGrid).
func expandHTMLTableRows(item schema.ChunkDoc) []schema.ChunkDoc {
td := item.TableData
if td == nil {
return []schema.ChunkDoc{item}
}
- rows, headerCount := tableChunkRows(td), td.HeaderRows
+ rows, headerCount := tableutil.ResolveRows(td, tableutil.RowsTable), td.HeaderRows
if len(rows) <= headerCount {
out := item
out.Text = tableutil.RenderTableHTML(td)
diff --git a/internal/ingestion/component/chunker/table_split_test.go b/internal/ingestion/component/chunker/table_split_test.go
index 9cc22e117c..1a33c36fea 100644
--- a/internal/ingestion/component/chunker/table_split_test.go
+++ b/internal/ingestion/component/chunker/table_split_test.go
@@ -15,6 +15,7 @@
package chunker
import (
+ "encoding/json"
"reflect"
"strings"
"testing"
@@ -226,6 +227,48 @@ func TestSplitLargeTableHeaderRowSpanClippedPerPart(t *testing.T) {
}
}
+// TestSplitLargeTableHeaderSpanSurvivesJSON pins the wire contract the table
+// splitter needs: OriginRow/OriginCol must survive the parser payload's JSON
+// round-trip so a header span can be clamped against the rows a part owns.
+func TestSplitLargeTableHeaderSpanSurvivesJSON(t *testing.T) {
+ td, err := tableutil.ParseTableHTML(`<table>
+<tr><th rowspan="3">A</th><th>x</th></tr>
+<tr><td>y</td></tr>
+<tr><td>z</td></tr>
+<tr><td>p</td><td>q</td></tr>
+<tr><td>r</td><td>s</td></tr>
+</table>`)
+ if err != nil || td == nil {
+ t.Fatalf("parse: %v", err)
+ }
+ blob, err := json.Marshal(td)
+ if err != nil {
+ t.Fatalf("marshal: %v", err)
+ }
+ var decoded entity.TableData
+ if err := json.Unmarshal(blob, &decoded); err != nil {
+ t.Fatalf("unmarshal: %v", err)
+ }
+ if got := decoded.Grid[2][0]; !got.Covered || got.OriginRow != 0 || got.OriginCol != 0 {
+ t.Fatalf("header span origin lost in JSON: %+v", got)
+ }
+ headerCost := charTokens(tableutil.RenderTableHTML(&entity.TableData{Grid: decoded.Grid[:1], HeaderRows: 1}))
+ budget := charTokens(tableutil.RenderTableHTML(&entity.TableData{Grid: decoded.Grid[:3], HeaderRows: 1})) - headerCost
+ parts, ranges, headerRows := splitLargeTable(&decoded, budget, charTokens, "")
+ if headerRows != 1 || len(parts) < 2 || ranges[0] != [2]int{0, 2} {
+ t.Fatalf("parts=%d ranges=%v headerRows=%d", len(parts), ranges, headerRows)
+ }
+ for i := range parts {
+ assertPartGridContract(t, &parts[i], i)
+ }
+ if got := parts[0].Grid[0][0].RowSpan; got != 3 {
+ t.Fatalf("part 0 A RowSpan = %d, want 3", got)
+ }
+ if got := parts[1].Grid[0][0].RowSpan; got != 1 {
+ t.Fatalf("part 1 A RowSpan = %d, want 1", got)
+ }
+}
+
// A cut must never fall inside a data row's rowspan: the origin and every
// row it covers belong to the same part. Otherwise the first part's origin
// claims rows outside the part and the next part starts with a Covered slot
diff --git a/internal/ingestion/component/chunker/table_struct_test.go b/internal/ingestion/component/chunker/table_struct_test.go
index 5e3988a00d..703baf14fb 100644
--- a/internal/ingestion/component/chunker/table_struct_test.go
+++ b/internal/ingestion/component/chunker/table_struct_test.go
@@ -84,8 +84,8 @@ func TestExpandHTMLTableRowsNonTablePassesThrough(t *testing.T) {
// continuation row must not lose the spanned column (CellTexts drops Covered
// slots, which would shift its value under the wrong header). The projection
// must recover the spanned value from the grid alone, without relying on
-// OriginRow/OriginCol — those fields are json:"-" and absent after a JSON
-// round-trip, which is the path expandHTMLTableRows actually consumes.
+// OriginRow/OriginCol back-pointers — the projection resolves spans from the
+// grid's RowSpan/Covered geometry alone.
func TestExpandHTMLTableRowsKeepsRowspanColumns(t *testing.T) {
td := &entity.TableData{
HeaderRows: 1,
@@ -95,7 +95,7 @@ func TestExpandHTMLTableRowsKeepsRowspanColumns(t *testing.T) {
{{Covered: true, OriginRow: 0, OriginCol: 0}, {Text: "25"}},
},
}
- // Simulate a JSON round-trip that strips the json:"-" OriginRow/OriginCol.
+ // Clear OriginRow/OriginCol to prove the projection does not depend on them.
for r := range td.Grid {
for c := range td.Grid[r] {
td.Grid[r][c].OriginRow = 0
@@ -114,11 +114,10 @@ func TestExpandHTMLTableRowsKeepsRowspanColumns(t *testing.T) {
}
// A cell that spans BOTH rows and columns (RowSpan>1 && ColSpan>1) must keep
-// every column it covers on the continuation rows, not just the first. The
-// active-rowspan state machine in tableChunkRows used to register the span
-// only at the origin's start column, so a 2x2 block left its second covered
-// column blank on the row below — dropping that field's value and misaligning
-// the row under the header.
+// every column it covers on the continuation rows, not just the first. A
+// projection that registers the active rowspan only at the origin's start
+// column would leave the second covered column blank on the row below,
+// dropping that field's value and misaligning the row under the header.
func TestExpandHTMLTableRowsKeepsRowColSpanBlock(t *testing.T) {
td := &entity.TableData{
HeaderRows: 1,
@@ -128,7 +127,7 @@ func TestExpandHTMLTableRowsKeepsRowColSpanBlock(t *testing.T) {
{{Covered: true, OriginRow: 1, OriginCol: 0}, {Covered: true, OriginRow: 1, OriginCol: 0}, {Text: "y"}},
},
}
- // Simulate a JSON round-trip that strips the json:"-" OriginRow/OriginCol;
+ // Clear OriginRow/OriginCol to prove the projection does not depend on them;
// the projection must recover the spanned value from the grid alone.
for r := range td.Grid {
for c := range td.Grid[r] {
@@ -158,7 +157,7 @@ func TestExpandHTMLTableRowsKeepsColspanColumns(t *testing.T) {
{{Text: "Info", ColSpan: 2}, {Covered: true, OriginRow: 1, OriginCol: 0}},
},
}
- // Simulate a JSON round-trip that strips the json:"-" OriginRow/OriginCol.
+ // Clear OriginRow/OriginCol to prove the projection does not depend on them.
for r := range td.Grid {
for c := range td.Grid[r] {
td.Grid[r][c].OriginRow = 0
diff --git a/internal/ingestion/component/chunker/table_test.go b/internal/ingestion/component/chunker/table_test.go
index 43eb78b510..71eed26f1e 100644
--- a/internal/ingestion/component/chunker/table_test.go
+++ b/internal/ingestion/component/chunker/table_test.go
@@ -297,3 +297,30 @@ func TestTableChunker_HeaderlessHTMLFirstRowIsHeader(t *testing.T) {
t.Errorf("text = %v", chunks[0]["text"])
}
}
+
+// TestTableChunker_HeaderOriginRowspanDoesNotLeak: a vertical merge mastered
+// on the header is a column label, not a value. The table view must leave the
+// data slot empty instead of stamping the header text into the row chunk.
+func TestTableChunker_HeaderOriginRowspanDoesNotLeak(t *testing.T) {
+ table := `<table>` +
+ `<tr><th rowspan="2">H</th><th>Name</th></tr>` +
+ `<tr><td>Alice</td></tr>` +
+ `</table>`
+ chunks := tableChunksOf(t, map[string]any{
+ "name": "h.xlsx",
+ "output_format": "json",
+ "json": []map[string]any{
+ tableItem(t, table),
+ },
+ })
+ if len(chunks) != 1 {
+ t.Fatalf("got %d chunks, want the single data row", len(chunks))
+ }
+ txt, _ := chunks[0]["text"].(string)
+ if strings.Contains(txt, "<td>H</td>") {
+ t.Fatalf("header label leaked into the data row: %v", txt)
+ }
+ if !strings.Contains(txt, "<td></td><td>Alice</td>") {
+ t.Fatalf("data row lost its real value or column alignment: %v", txt)
+ }
+}
diff --git a/internal/ingestion/component/chunker/xlsx_qa_regression_test.go b/internal/ingestion/component/chunker/xlsx_qa_regression_test.go
index 215dfff48d..8064b584ba 100644
--- a/internal/ingestion/component/chunker/xlsx_qa_regression_test.go
+++ b/internal/ingestion/component/chunker/xlsx_qa_regression_test.go
@@ -15,6 +15,13 @@ import (
// xlsxWorkbook renders rows into an in-memory workbook so the tests run
// against the real spreadsheet parser instead of hand-written markup.
func xlsxWorkbook(t *testing.T, rows [][]string) []byte {
+ t.Helper()
+ return xlsxWorkbookWithMerges(t, rows, nil)
+}
+
+// xlsxWorkbookWithMerges is xlsxWorkbook plus merge rectangles expressed as
+// top-left/bottom-right axis pairs (e.g. {"B1", "B2"}).
+func xlsxWorkbookWithMerges(t *testing.T, rows [][]string, merges [][2]string) []byte {
t.Helper()
f := excelize.NewFile()
sh := f.GetSheetName(0)
@@ -26,6 +33,11 @@ func xlsxWorkbook(t *testing.T, rows [][]string) []byte {
}
}
}
+ for _, m := range merges {
+ if err := f.MergeCell(sh, m[0], m[1]); err != nil {
+ t.Fatal(err)
+ }
+ }
var buf bytes.Buffer
if err := f.Write(&buf); err != nil {
t.Fatal(err)
@@ -101,3 +113,47 @@ func TestXLSXQAMultilineCells(t *testing.T) {
}
}
}
+
+// TestXLSXQAMergedAnswerAtFirstRow pins the fix for a vertical merge whose
+// master sits on the first row. A headerless QA sheet treats that row as data,
+// so both questions must receive the merged answer; the parser must not drop
+// the second row because it once mistook the master for a column label.
+func TestXLSXQAMergedAnswerAtFirstRow(t *testing.T) {
+ chunks := qaChunksFromXLSX(t, xlsxWorkbookWithMerges(t, [][]string{
+ {"q1", "a1"},
+ {"q2", ""},
+ }, [][2]string{{"B1", "B2"}}))
+ if len(chunks) != 2 {
+ t.Fatalf("expected two QA chunks, got %d: %#v", len(chunks), chunkTexts(chunks))
+ }
+ texts := chunkTexts(chunks)
+ if !strings.Contains(texts[0], "q1") || !strings.Contains(texts[0], "a1") {
+ t.Fatalf("first pair = %q", texts[0])
+ }
+ if !strings.Contains(texts[1], "q2") || !strings.Contains(texts[1], "a1") {
+ t.Fatalf("merged answer did not reach the second pair: %q", texts[1])
+ }
+ if got, _ := chunks[1]["top_int"].([]any); len(got) != 1 || got[0] != float64(1) {
+ t.Fatalf("second chunk top_int = %v, want [1]", got)
+ }
+}
+
+// TestXLSXQAMergedAnswerBelowFirstRow keeps the existing behavior for a merge
+// whose master is already a data row: every covered row must resolve to the
+// master answer.
+func TestXLSXQAMergedAnswerBelowFirstRow(t *testing.T) {
+ chunks := qaChunksFromXLSX(t, xlsxWorkbookWithMerges(t, [][]string{
+ {"q1", "a1"},
+ {"q2", "a2"},
+ {"q3", ""},
+ {"q4", ""},
+ }, [][2]string{{"B2", "B4"}}))
+ if len(chunks) != 4 {
+ t.Fatalf("expected four QA chunks, got %d: %#v", len(chunks), chunkTexts(chunks))
+ }
+ for i, text := range chunkTexts(chunks)[1:] {
+ if !strings.Contains(text, "a2") {
+ t.Errorf("chunk %d did not inherit the data merge: %q", i+2, text)
+ }
+ }
+}
diff --git a/internal/parser/parser/csv_parser.go b/internal/parser/parser/csv_parser.go
index fbdc50ab4a..92952e50ec 100644
--- a/internal/parser/parser/csv_parser.go
+++ b/internal/parser/parser/csv_parser.go
@@ -139,7 +139,7 @@ func (p *CSVParser) ParseWithResult(ctx context.Context, filename string, data [
}
// One wire shape for csv as for xlsx: a single segmented HTML table with
// row-aligned positions (csv has no in-cell image anchors).
- items := buildSheetItems(records, csvSheetName, 1, 1, dataRows, nil)
+ items := buildSheetItems(records, csvSheetName, 1, 1, dataRows, nil, nil)
return ParseResult{
File: map[string]any{
"name": filename,
diff --git a/internal/parser/parser/office_table_render.go b/internal/parser/parser/office_table_render.go
index f91947a3c4..3013f7b76e 100644
--- a/internal/parser/parser/office_table_render.go
+++ b/internal/parser/parser/office_table_render.go
@@ -29,6 +29,7 @@ import (
"ragflow/internal/common"
"ragflow/internal/entity"
+ "ragflow/internal/parser/tableutil"
)
// tableIllegalCharsRe replaces illegal control characters (everything except
@@ -79,14 +80,20 @@ func trimCells(row []string) []string {
// document order. Every segment repeats the header row (so segment 2+ never
// loses column names) and carries a row-aligned position matrix — one tuple
// per row in strict order, header included — so row-level consumers index
-// positions by row number with no side channel. The chunker reads the rows
-// directly and renders each emitted chunk's Text as an HTML <table> (see
-// tableutil.RenderTableHTML); no HTML markup is emitted here at the parser.
+// positions by row number with no side channel. Each segment's TableData keeps
+// the sheet's merge geometry: an origin cell carries ColSpan/RowSpan and the
+// positions it engulfs are Covered markers. Consumers project those slots with
+// tableutil.ResolveRows; the parser no longer pre-expands merges. The chunker
+// reads the rows directly and renders each emitted chunk's Text as an HTML
+// <table> (see tableutil.RenderTableHTML); no HTML markup is emitted here at
+// the parser.
//
// An image lands after the last row whose (row, colStart) sorts at or before
// its anchor; rows before the header's anchor keep the header in front of
// the image (a header-only lead segment); fully empty rows are skipped from
-// both rows and matrix.
+// both rows and matrix. A segment cut never splits a merge group: an anchor
+// inside a merged block moves after the block, so every segment owns the
+// origins of its Covered slots.
//
// Segmentation is semantic only — sheet boundaries and image anchors, never a
// size budget. The legacy html4excel path pre-cut a fixed 12 rows here, which
@@ -97,7 +104,7 @@ func trimCells(row []string) []string {
// colEnd]. The same `positions` field carries PDF layout boxes
// ([page, left, right, top, bottom]) on PDF items, so a consumer must key the
// vocabulary off sheet_index / doc_type_kwd and never reinterpret one as the other.
-func buildSheetItems(records [][]string, sheet string, sheetIndex, headerRow int, dataRows []int, images []map[string]any) []map[string]any {
+func buildSheetItems(records [][]string, sheet string, sheetIndex, headerRow int, dataRows []int, images []map[string]any, ranges []mergeRange) []map[string]any {
sortImagesByAnchor(images)
if len(records) == 0 {
return images
@@ -105,7 +112,9 @@ func buildSheetItems(records [][]string, sheet string, sheetIndex, headerRow int
if headerRow <= 0 {
headerRow = 1
}
- header := append([]string(nil), records[0]...)
+ rowByNum, rowNums := sheetRowIndex(records, headerRow, dataRows)
+ ranges = materializeDetachedMerges(rowByNum, headerRow, ranges)
+ header := append([]string(nil), rowByNum[headerRow]...)
headerColEnd := len(header)
if headerColEnd == 0 {
for _, row := range records {
@@ -120,16 +129,34 @@ func buildSheetItems(records [][]string, sheet string, sheetIndex, headerRow int
headerTuple := []float64{float64(sheetIndex), float64(headerRow), float64(headerRow), 1, float64(headerColEnd)} // [sheet, rowStart, rowEnd, colStart, colEnd]
rows := make([]spreadsheetSegmentRow, 0, len(records)-1)
- for i, source := range records[1:] {
- cells := append([]string(nil), source...)
+ for i := range records[1:] {
+ rowNum := rowNums[i]
+ cells := append([]string(nil), rowByNum[rowNum]...)
colStart, colEnd := nonEmptyColumnRange(cells)
+ // A merge slave can be the row's only value in a column before the
+ // surviving real cells. The position tuple still has to cover the
+ // merged group's columns, exactly as the pre-geometry parser did when
+ // it copied the master value into the slave cell. Do this before the
+ // empty-row skip: a row with no cells of its own is still a real grid
+ // row when a non-blank merge master covers it.
+ for _, m := range ranges {
+ if m.sr > rowNum || m.er < rowNum {
+ continue
+ }
+ masterRow := rowByNum[m.sr]
+ if m.sc < 1 || m.sc > len(masterRow) || strings.TrimSpace(masterRow[m.sc-1]) == "" {
+ continue
+ }
+ if colStart == 0 || m.sc < colStart {
+ colStart = m.sc
+ }
+ if m.ec > colEnd {
+ colEnd = m.ec
+ }
+ }
if colStart == 0 {
continue
}
- rowNum := headerRow + i + 1
- if len(dataRows) == len(records)-1 {
- rowNum = dataRows[i]
- }
rows = append(rows, spreadsheetSegmentRow{
cells: cells,
tuple: []float64{float64(sheetIndex), float64(rowNum), float64(rowNum), float64(colStart), float64(colEnd)},
@@ -158,6 +185,15 @@ func buildSheetItems(records [][]string, sheet string, sheetIndex, headerRow int
img map[string]any
}{cut, img})
}
+ // A segment must not cut through a merge group: the segment's canonical
+ // grid needs the group's origin to resolve its covered slots. Move an
+ // image anchor forward to the first row after the group instead.
+ for i := range anchored {
+ if anchored[i].cut == 0 {
+ continue
+ }
+ anchored[i].cut = cutAfterMergeGroup(anchored[i].cut, rows, ranges)
+ }
sort.SliceStable(anchored, func(i, j int) bool {
if anchored[i].cut != anchored[j].cut {
return anchored[i].cut < anchored[j].cut
@@ -176,33 +212,10 @@ func buildSheetItems(records [][]string, sheet string, sheetIndex, headerRow int
newSegment := func(body []spreadsheetSegmentRow) map[string]any {
matrix := make([][]float64, 0, len(body)+1)
matrix = append(matrix, headerTuple)
- rowsOut := make([][]string, 0, len(body)+1)
- headerCells := trimCells(header)
- rowsOut = append(rowsOut, headerCells)
for _, r := range body {
- cells := trimCells(r.cells)
- // Pad short rows to the header width so every row renders the
- // full column set with dataless cells left empty. excelize
- // truncates each row at its last valued cell, so without this a
- // trailing dataless column (kept as schema by
- // trimTrailingEmptyColumns) would vanish from data rows while
- // its <th> stays. Padding is empty-only, so the row-aligned
- // position tuple computed above is unaffected.
- for len(cells) < len(headerCells) {
- cells = append(cells, "")
- }
- rowsOut = append(rowsOut, cells)
matrix = append(matrix, r.tuple)
}
- grid := make([][]entity.TableCell, 0, len(rowsOut))
- for _, r := range rowsOut {
- cells := make([]entity.TableCell, 0, len(r))
- for _, c := range r {
- cells = append(cells, entity.TableCell{Text: c})
- }
- grid = append(grid, cells)
- }
- td := &entity.TableData{Grid: grid, HeaderRows: 1, Caption: sheet}
+ td := buildCanonicalTable(headerRow, header, body, ranges, sheet)
ti := NewTableDataJSONItem(td, sheet, SpreadsheetPositions(matrix)).(TableItem)
ti.Extra = map[string]any{"sheet_index": sheetIndex}
return ti.ToMap()
@@ -211,7 +224,16 @@ func buildSheetItems(records [][]string, sheet string, sheetIndex, headerRow int
prev := 0
headerLeadUsed := false
for i := 0; i < len(anchored); {
- cut := anchored[i].cut
+ origCut := anchored[i].cut
+ cut := origCut
+ if cut == 0 && !headerLeadUsed {
+ // A header-origin vertical merge spanning into the data rows must
+ // stay in the lead segment too; otherwise the next segment would
+ // contain a covered slot whose origin is in the previous one.
+ if end := headerMergeEnd(ranges, headerRow); end > headerRow {
+ cut = cutAfterRow(rows, end)
+ }
+ }
if cut == 0 && !headerLeadUsed {
// A cut at 0 keeps the header in front of the image, matching the
// old IR's always-first header record: emit a header-only lead
@@ -222,7 +244,7 @@ func buildSheetItems(records [][]string, sheet string, sheetIndex, headerRow int
items = append(items, newSegment(rows[prev:cut]))
prev = cut
}
- for i < len(anchored) && anchored[i].cut == cut {
+ for i < len(anchored) && anchored[i].cut == origCut {
items = append(items, anchored[i].img)
i++
}
@@ -263,6 +285,268 @@ func nonEmptyColumnRange(row []string) (int, int) {
return start, end
}
+// sheetRowIndex maps absolute sheet row numbers to the reordered records and
+// returns the data rows' numbers in record order. The header row is records[0].
+func sheetRowIndex(records [][]string, headerRow int, dataRows []int) (map[int][]string, []int) {
+ byNum := make(map[int][]string, len(records))
+ nums := make([]int, len(records)-1)
+ byNum[headerRow] = records[0]
+ for i, row := range records[1:] {
+ rowNum := headerRow + i + 1
+ if len(dataRows) == len(records)-1 {
+ rowNum = dataRows[i]
+ }
+ nums[i] = rowNum
+ byNum[rowNum] = row
+ }
+ return byNum, nums
+}
+
+// materializeDetachedMerges handles a merge that starts above the detected
+// header and crosses it. Reordering the header to records[0] puts such a
+// merge's origin after the row it covers, which a grid cannot represent as a
+// span. The legacy parser filled the text into the header (and data rows) with
+// no span, so mirror that shape: write the master text into the rows the range
+// covers and drop the range from the geometry. All other merges stay canonical.
+func materializeDetachedMerges(rowByNum map[int][]string, headerRow int, ranges []mergeRange) []mergeRange {
+ if len(ranges) == 0 {
+ return ranges
+ }
+ kept := make([]mergeRange, 0, len(ranges))
+ for _, m := range ranges {
+ if m.sr >= headerRow || m.er < headerRow {
+ kept = append(kept, m)
+ continue
+ }
+ masterRow := rowByNum[m.sr]
+ if m.sc < 1 || m.sc > len(masterRow) {
+ continue
+ }
+ master := masterRow[m.sc-1]
+ if strings.TrimSpace(master) == "" {
+ continue
+ }
+ // readSpreadsheetRecords caps merge padding at maxMergeExtentCols so a
+ // pathological far merge cannot allocate one cell per worksheet column.
+ // A detached merge bypasses that padding, so apply the same cap here.
+ endCol := m.ec
+ if endCol > maxMergeExtentCols {
+ endCol = maxMergeExtentCols
+ }
+ if m.sc > endCol {
+ continue
+ }
+ for row := m.sr; row <= m.er; row++ {
+ cells, ok := rowByNum[row]
+ if !ok {
+ continue
+ }
+ if row == headerRow {
+ // The header row carried a merged title across the columns
+ // the range covers; the legacy header-inheritance path filled
+ // every one of them.
+ if endCol > len(cells) {
+ grown := make([]string, endCol)
+ copy(grown, cells)
+ cells = grown
+ rowByNum[row] = cells
+ }
+ for c := m.sc; c <= endCol; c++ {
+ if strings.TrimSpace(cells[c-1]) == "" {
+ cells[c-1] = master
+ }
+ }
+ continue
+ }
+ // Data rows only inherit a vertical merge's master. A horizontal
+ // data merge keeps its value on the origin column, matching the
+ // legacy data-inheritance rule.
+ if m.sc == m.ec || row == m.sr {
+ if m.sc > len(cells) {
+ grown := make([]string, m.sc)
+ copy(grown, cells)
+ cells = grown
+ rowByNum[row] = cells
+ }
+ if strings.TrimSpace(cells[m.sc-1]) == "" {
+ cells[m.sc-1] = master
+ }
+ }
+ }
+ }
+ return kept
+}
+
+// buildCanonicalTable assembles one segment's TableData from the segment's
+// source rows and the sheet's merge rectangles. The grid keeps merge geometry
+// instead of pre-expanding it: an origin cell carries ColSpan/RowSpan and the
+// positions it engulfs are Covered markers. Consumers choose how to project
+// those covered slots (see tableutil.ResolveRows), so the parser no longer has
+// to decide whether a merged value is a header label or a data value.
+func buildCanonicalTable(headerRow int, header []string, body []spreadsheetSegmentRow, ranges []mergeRange, sheet string) *entity.TableData {
+ type sourceRow struct {
+ num int
+ cells []string
+ }
+ sources := make([]sourceRow, 0, len(body)+1)
+ sources = append(sources, sourceRow{num: headerRow, cells: header})
+ for _, r := range body {
+ sources = append(sources, sourceRow{num: r.rowNum, cells: r.cells})
+ }
+
+ present := make(map[int]bool, len(sources))
+ for _, src := range sources {
+ present[src.num] = true
+ }
+ localRanges := clampMergeRanges(ranges, present)
+
+ logical := make([][]tableutil.CellInput, 0, len(sources))
+ headerFlags := make([]bool, 0, len(sources))
+ for i, src := range sources {
+ cells := trimCells(src.cells)
+ // A merge origin past the row's truncation point still has to be
+ // emitted so the grid can place the span; GetRows truncates at the
+ // last valued cell.
+ width := len(cells)
+ for _, m := range localRanges {
+ if m.sr == src.num && m.sc > width {
+ width = m.sc
+ }
+ }
+ row := make([]tableutil.CellInput, 0, width)
+ for c := 0; c < width; c++ {
+ m, origin := mergeAt(localRanges, src.num, c+1)
+ if m != nil && !origin {
+ continue
+ }
+ text := ""
+ if c < len(cells) {
+ text = cells[c]
+ }
+ in := tableutil.CellInput{Text: text}
+ if m != nil && origin {
+ in.ColSpan = m.ec - m.sc + 1
+ in.RowSpan = m.er - m.sr + 1
+ }
+ row = append(row, in)
+ }
+ logical = append(logical, row)
+ headerFlags = append(headerFlags, i == 0)
+ }
+
+ td := tableutil.BuildTableData(logical, headerFlags, sheet)
+ if td == nil || len(td.Grid) == 0 {
+ return td
+ }
+ // Preserve the legacy spreadsheet row width: data rows were padded to the
+ // header width so a short trailing column still renders under its <th>.
+ // Only appending is done here, so every existing cell keeps its index.
+ width := len(td.Grid[0])
+ for r := 1; r < len(td.Grid); r++ {
+ for len(td.Grid[r]) < width {
+ td.Grid[r] = append(td.Grid[r], entity.TableCell{})
+ }
+ }
+ return td
+}
+
+// clampMergeRanges keeps only the consecutive rows of each merge that are
+// present in this segment. A header is repeated in every segment, so a header-
+// origin merge whose covered rows live in an earlier segment must not reserve
+// a column in a later one; clamping its RowSpan to 1 (or its ColSpan-only form)
+// prevents the later segment from marking an unrelated body row Covered.
+func clampMergeRanges(ranges []mergeRange, present map[int]bool) []mergeRange {
+ if len(ranges) == 0 {
+ return nil
+ }
+ local := make([]mergeRange, 0, len(ranges))
+ for _, m := range ranges {
+ if !present[m.sr] {
+ continue
+ }
+ end := m.sr
+ for row := m.sr; row <= m.er; row++ {
+ if !present[row] {
+ break
+ }
+ end = row
+ }
+ if end == m.sr && m.sc == m.ec {
+ continue // one-row vertical span is not a merge
+ }
+ local = append(local, mergeRange{sr: m.sr, sc: m.sc, er: end, ec: m.ec})
+ }
+ return local
+}
+
+// mergeTouchesRow reports whether any merge rectangle covers row.
+func mergeTouchesRow(ranges []mergeRange, row int) bool {
+ for _, m := range ranges {
+ if row >= m.sr && row <= m.er {
+ return true
+ }
+ }
+ return false
+}
+
+// mergeAt returns the merge rectangle covering (row, col), both 1-based, and
+// whether that position is the rectangle's origin.
+func mergeAt(ranges []mergeRange, row, col int) (*mergeRange, bool) {
+ for i := range ranges {
+ m := &ranges[i]
+ if row >= m.sr && row <= m.er && col >= m.sc && col <= m.ec {
+ return m, row == m.sr && col == m.sc
+ }
+ }
+ return nil, false
+}
+
+// cutAfterMergeGroup moves a segment cut at index cut to the first row after
+// any merge group it would split. The returned cut is always >= cut.
+func cutAfterMergeGroup(cut int, rows []spreadsheetSegmentRow, ranges []mergeRange) int {
+ if cut <= 0 || cut >= len(rows) {
+ return cut
+ }
+ for cut < len(rows) {
+ moved := false
+ for _, m := range ranges {
+ if m.sr <= rows[cut-1].rowNum && m.er >= rows[cut].rowNum {
+ for cut < len(rows) && rows[cut].rowNum <= m.er {
+ cut++
+ }
+ moved = true
+ break
+ }
+ }
+ if !moved {
+ return cut
+ }
+ }
+ return cut
+}
+
+// cutAfterRow returns the first row index whose absolute sheet row is greater
+// than rowNum.
+func cutAfterRow(rows []spreadsheetSegmentRow, rowNum int) int {
+ cut := 0
+ for cut < len(rows) && rows[cut].rowNum <= rowNum {
+ cut++
+ }
+ return cut
+}
+
+// headerMergeEnd returns the furthest row covered by a merge mastered on the
+// header row, or headerRow when the header has no vertical merge.
+func headerMergeEnd(ranges []mergeRange, headerRow int) int {
+ end := headerRow
+ for _, m := range ranges {
+ if m.sr == headerRow && m.er > end {
+ end = m.er
+ }
+ }
+ return end
+}
+
func numericItemInt(value any) (int, bool) {
switch n := value.(type) {
case int:
@@ -548,101 +832,6 @@ func mergeMaxCol(ranges []mergeRange) int {
return m
}
-// mergeMasterForRow materialises the slave→master map for a single row only.
-// A large merged block (e.g. A1:Z100) would otherwise expand to every one of
-// its cells; we only ever need the header row's slaves, so expanding per row
-// keeps the map O(cols) instead of O(rows×cols).
-func mergeMasterForRow(ranges []mergeRange, row int) map[[2]int][2]int {
- mm := map[[2]int][2]int{}
- for _, r := range ranges {
- if row < r.sr || row > r.er {
- continue
- }
- for c := r.sc; c <= r.ec; c++ {
- mm[[2]int{row, c}] = [2]int{r.sr, r.sc}
- }
- }
- return mm
-}
-
-// inheritMergedHeader fills empty cells in the header row with the value of
-// their merge master (typically a wide horizontally-merged title cell). This
-// keeps a wide merged title from rendering as a row of blank <th> cells.
-func inheritMergedHeader(records [][]string, headerRowIdx int, mm map[[2]int][2]int) {
- if headerRowIdx < 1 || headerRowIdx > len(records) {
- return
- }
- row := records[headerRowIdx-1]
- for c := 0; c < len(row); c++ {
- if strings.TrimSpace(row[c]) != "" {
- continue
- }
- master, ok := mm[[2]int{headerRowIdx, c + 1}]
- if !ok || (master[0] == headerRowIdx && master[1] == c+1) {
- continue
- }
- if master[0] < 1 || master[0] > len(records) || master[1] < 1 || master[1] > len(records[master[0]-1]) {
- continue
- }
- val := records[master[0]-1][master[1]-1]
- if strings.TrimSpace(val) != "" {
- row[c] = val
- }
- }
-}
-
-// inheritMergedDataCells fills empty cells of non-header rows with the value
-// of their merge master, so a vertically merged group label (e.g. A2:A4) is
-// visible on every data row instead of only the first. excelize truncates
-// each row at its last valued cell, so a slave cell past that point is absent
-// from the slice: the row is then extended exactly up to the slave column to
-// host the real inherited value. The extension is bounded by the master row's
-// own width (the master cell must exist and be non-empty), so unlike header
-// padding it cannot chase a far merge across the sheet (see padRowToWidth).
-// Call with absolute sheet row numbers, before records are reordered around
-// the header row.
-func inheritMergedDataCells(rows [][]string, headerRow int, ranges []mergeRange) {
- for _, r := range ranges {
- // Vertical merges only: a horizontally merged data cell is already
- // visible via its master column, while copying its text into every
- // slave column would render the same value twice.
- if r.sc != r.ec {
- continue
- }
- if r.sr == headerRow {
- continue
- }
- // A merge mastered on the header row carries a column label, not
- // data: copying it down would stamp the header text into every
- // data row it spans.
- if r.sr < 1 || r.sr > len(rows) || r.sc < 1 {
- continue
- }
- masterRow := rows[r.sr-1]
- if r.sc > len(masterRow) {
- continue
- }
- master := masterRow[r.sc-1]
- if strings.TrimSpace(master) == "" {
- continue
- }
- // Start past the master row: its own cell already holds the value.
- for row := r.sr + 1; row <= r.er && row <= len(rows); row++ {
- if row == headerRow {
- continue
- }
- // Grow a truncated row exactly up to the slave column; the
- // appended cells stay empty except the inherited value below.
- for len(rows[row-1]) < r.sc {
- rows[row-1] = append(rows[row-1], "")
- }
- if strings.TrimSpace(rows[row-1][r.sc-1]) == "" {
- rows[row-1][r.sc-1] = master
- }
- }
- }
-}
-
// mergeExtentCol returns the furthest merged column across all ranges, capped
// at maxMergeExtentCols so a pathological far merge cannot exhaust parser
// memory when the header row is padded to inherit merged text.
@@ -857,13 +1046,13 @@ func isSubtotalRow(row []string) bool {
return false
}
-func readSpreadsheetRecords(f *excelize.File, sheet string) ([][]string, []int, int, []string, error) {
+func readSpreadsheetRecords(f *excelize.File, sheet string) ([][]string, []int, int, []mergeRange, []string, error) {
rows, err := f.GetRows(sheet)
if err != nil {
- return nil, nil, 0, nil, fmt.Errorf("read XLSX sheet %q rows: %w", sheet, err)
+ return nil, nil, 0, nil, nil, fmt.Errorf("read XLSX sheet %q rows: %w", sheet, err)
}
if len(rows) == 0 {
- return nil, nil, 0, nil, nil
+ return nil, nil, 0, nil, nil, nil
}
rows = cleanIllegalControlChars(rows)
@@ -878,21 +1067,15 @@ func readSpreadsheetRecords(f *excelize.File, sheet string) ([][]string, []int,
}
headerRow := detectHeaderRow(f, sheet, rows, tables)
- // Inherit merged-master text into the header row. excelize's GetRows
- // truncates each row at its last valued cell, so a merged slave beyond that
- // point is absent and cannot inherit its master's text. We therefore pad
- // ONLY the header row (the only row we inherit into) to the furthest merged
- // column — capped by mergeExtentCol so a pathological far merge cannot
- // exhaust memory. Padding runs after detection so a wide merge (e.g. A1:Z1
- // title) does not dilute the styled-majority signal of a narrow header.
- mm := mergeMasterForRow(ranges, headerRow)
- if len(mm) > 0 {
+ // Pad the header row to the furthest merged column when a merge touches
+ // the header. excelize's GetRows truncates each row at its last valued
+ // cell, so a merged slave beyond that point is absent from the slice; the
+ // canonical grid builder needs the header width to include the merge
+ // extent. Padding runs after detection so a wide merge (e.g. A1:Z1 title)
+ // does not dilute the styled-majority signal of a narrow header.
+ if mergeTouchesRow(ranges, headerRow) {
padRowToWidth(&rows[headerRow-1], mergeExtentCol(ranges))
}
- inheritMergedHeader(rows, headerRow, mm)
- // Data rows inherit their own vertical merge masters in place (no
- // widening); see inheritMergedDataCells.
- inheritMergedDataCells(rows, headerRow, ranges)
// Reorder so the detected header row becomes records[0]; every other row is
// data. For the common case (header on row 1) this is a no-op.
@@ -908,5 +1091,5 @@ func readSpreadsheetRecords(f *excelize.File, sheet string) ([][]string, []int,
}
records = trimTrailingEmptyColumns(records)
- return records, absDataRows, headerRow, warnings, nil
+ return records, absDataRows, headerRow, ranges, warnings, nil
}
diff --git a/internal/parser/parser/office_table_render_test.go b/internal/parser/parser/office_table_render_test.go
index b491e30a33..8b487a68c2 100644
--- a/internal/parser/parser/office_table_render_test.go
+++ b/internal/parser/parser/office_table_render_test.go
@@ -186,7 +186,7 @@ func TestXLSXParserEmitsHeaderOnlySegment(t *testing.T) {
// user content can never become markup.
func TestBuildSheetItemsEscapesCellText(t *testing.T) {
const tricky = `<b>bold</b> & "quoted" > 1`
- items := buildSheetItems([][]string{{"Name"}, {tricky}}, "Sheet1", 1, 1, nil, nil)
+ items := buildSheetItems([][]string{{"Name"}, {tricky}}, "Sheet1", 1, 1, nil, nil, nil)
if len(items) != 1 {
t.Fatalf("items = %d, want one segment", len(items))
}
@@ -286,6 +286,95 @@ func TestXLSXParserImageAnchorsSplitSegments(t *testing.T) {
})
}
+// TestXLSXParserImageAnchorDoesNotSplitMergedGroup asserts segmentation keeps
+// a merge group inside one segment. An image anchor inside B2:B3 must move
+// after the group, otherwise row 3 would be a Covered slot whose origin lives
+// in the previous segment.
+func TestXLSXParserImageAnchorDoesNotSplitMergedGroup(t *testing.T) {
+ const pngBase64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+M8AAAMBAQDJ/pLvAAAAAElFTkSuQmCC"
+ data := newTestXLSX(t, func(f *excelize.File) {
+ mustSetCell(t, f, "Sheet1", "A1", "Name")
+ mustSetCell(t, f, "Sheet1", "B1", "Value")
+ mustSetCell(t, f, "Sheet1", "A2", "q2")
+ mustSetCell(t, f, "Sheet1", "B2", "a2")
+ mustMergeCell(t, f, "Sheet1", "B2", "B3")
+ mustSetCell(t, f, "Sheet1", "A3", "q3")
+ mustSetCell(t, f, "Sheet1", "A4", "q4")
+ mustSetCell(t, f, "Sheet1", "B4", "a4")
+ if err := f.AddPictureFromBytes("Sheet1", "A2", &excelize.Picture{
+ Extension: ".png",
+ File: mustDecodeBase64(t, pngBase64),
+ Format: &excelize.GraphicOptions{AltText: "fig"},
+ }); err != nil {
+ t.Fatalf("AddPictureFromBytes: %v", err)
+ }
+ })
+ p, _ := NewXLSXParser("")
+ res := p.ParseWithResult(t.Context(), "merge-anchor.xlsx", data)
+ if res.Err != nil {
+ t.Fatalf("ParseWithResult: %v", res.Err)
+ }
+ items := ItemsToMaps(res.JSON)
+ if len(items) != 3 {
+ t.Fatalf("items = %d, want table, image, table", len(items))
+ }
+ if items[1]["doc_type_kwd"] != "image" {
+ t.Fatalf("middle item = %#v, want image", items[1])
+ }
+ first := items[0]["table"].(*entity.TableData)
+ if len(first.Grid) != 3 {
+ t.Fatalf("first segment rows = %d, want header + two merged rows", len(first.Grid))
+ }
+ if first.Grid[1][1].Text != "a2" || first.Grid[1][1].RowSpan != 2 {
+ t.Fatalf("merge origin = %+v, want a2 rowspan 2", first.Grid[1][1])
+ }
+ if !first.Grid[2][1].Covered {
+ t.Fatalf("merge slave = %+v, want Covered", first.Grid[2][1])
+ }
+ second := items[2]["table"].(*entity.TableData)
+ if len(second.Grid) != 2 {
+ t.Fatalf("second segment rows = %d, want header + row4", len(second.Grid))
+ }
+}
+
+// TestXLSXParserImageAnchorAtLastMergedRow covers the boundary where the merge
+// group ends on the last retained data row. The image cut must advance to
+// len(rows) and stop without indexing past the slice.
+func TestXLSXParserImageAnchorAtLastMergedRow(t *testing.T) {
+ const pngBase64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+M8AAAMBAQDJ/pLvAAAAAElFTkSuQmCC"
+ data := newTestXLSX(t, func(f *excelize.File) {
+ mustSetCell(t, f, "Sheet1", "A1", "Name")
+ mustSetCell(t, f, "Sheet1", "B1", "Value")
+ mustSetCell(t, f, "Sheet1", "A2", "q2")
+ mustSetCell(t, f, "Sheet1", "B2", "a2")
+ mustMergeCell(t, f, "Sheet1", "B2", "B3")
+ mustSetCell(t, f, "Sheet1", "A3", "q3")
+ if err := f.AddPictureFromBytes("Sheet1", "A2", &excelize.Picture{
+ Extension: ".png",
+ File: mustDecodeBase64(t, pngBase64),
+ Format: &excelize.GraphicOptions{AltText: "fig"},
+ }); err != nil {
+ t.Fatalf("AddPictureFromBytes: %v", err)
+ }
+ })
+ p, _ := NewXLSXParser("")
+ res := p.ParseWithResult(t.Context(), "merge-anchor-last.xlsx", data)
+ if res.Err != nil {
+ t.Fatalf("ParseWithResult: %v", res.Err)
+ }
+ items := ItemsToMaps(res.JSON)
+ if len(items) != 2 {
+ t.Fatalf("items = %d, want table then image", len(items))
+ }
+ first := items[0]["table"].(*entity.TableData)
+ if len(first.Grid) != 3 {
+ t.Fatalf("first segment rows = %d, want header + two merged rows", len(first.Grid))
+ }
+ if first.Grid[1][1].RowSpan != 2 || !first.Grid[2][1].Covered {
+ t.Fatalf("last-row merge = %+v / %+v, want span ending on the last row", first.Grid[1][1], first.Grid[2][1])
+ }
+}
+
// markupRows extracts the cell texts of every <tr> in rendered spreadsheet
// markup, unescaped. It is a deliberately minimal test-local reader: these
// tests assert the parser's rendered output, so the markup itself is the
@@ -484,10 +573,11 @@ func TestXLSXParser_HeaderAndCaption(t *testing.T) {
}
}
-// TestXLSXParser_MergedHeaderInheritance asserts a horizontally merged header
-// cell's slave columns inherit the master text, so a wide merged title does not
-// render as a row of blank header cells.
-func TestXLSXParser_MergedHeaderInheritance(t *testing.T) {
+// TestXLSXParser_MergedHeaderKeepsColspan asserts a horizontally merged
+// header keeps its merge geometry (one origin with colspan) instead of being
+// pre-expanded into duplicate header cells. The table view still projects the
+// label across every column.
+func TestXLSXParser_MergedHeaderKeepsColspan(t *testing.T) {
data := newTestXLSX(t, func(f *excelize.File) {
// Row 1 is the header; A1:C1 merged into one wide label "Sales Report".
mustSetCell(t, f, "Sheet1", "A1", "Sales Report")
@@ -508,15 +598,25 @@ func TestXLSXParser_MergedHeaderInheritance(t *testing.T) {
t.Fatalf("ParseWithResult: %v", res.Err)
}
text := spreadsheetText(res)
- if !strings.Contains(text, "<th>Sales Report</th><th>Sales Report</th><th>Sales Report</th>") {
- t.Fatalf("merged master text not inherited into header cells, got:\n%s", text)
+ if !strings.Contains(text, `<th colspan="3">Sales Report</th>`) {
+ t.Fatalf("merged header should keep colspan, got:\n%s", text)
+ }
+ td := res.JSON[0].ToMap()["table"].(*entity.TableData)
+ rows := tableutil.ResolveRows(td, tableutil.RowsTable)
+ if len(rows) == 0 || len(rows[0]) != 3 {
+ t.Fatalf("table view header = %v, want three projected columns", rows)
+ }
+ for i, want := range []string{"Sales Report", "Sales Report", "Sales Report"} {
+ if rows[0][i] != want {
+ t.Fatalf("table view header[%d] = %q, want %q", i, rows[0][i], want)
+ }
}
}
-// TestXLSXParser_MergedDataCellInheritance asserts a vertically merged data
-// cell's slave rows inherit the master text, so every row chunk keeps its
-// group label instead of rendering an empty first cell.
-func TestXLSXParser_MergedDataCellInheritance(t *testing.T) {
+// TestXLSXParser_MergedDataCellKeepsRowspan asserts a vertically merged data
+// cell is stored as rowspan geometry, while the table view still resolves the
+// group label into every projected data row.
+func TestXLSXParser_MergedDataCellKeepsRowspan(t *testing.T) {
data := newTestXLSX(t, func(f *excelize.File) {
mustSetCell(t, f, "Sheet1", "A1", "Group")
mustSetCell(t, f, "Sheet1", "B1", "Item")
@@ -530,9 +630,16 @@ func TestXLSXParser_MergedDataCellInheritance(t *testing.T) {
if res.Err != nil {
t.Fatalf("ParseWithResult: %v", res.Err)
}
- text := spreadsheetText(res)
- if !strings.Contains(text, "<tr><td>G1</td><td>y</td></tr>") {
- t.Fatalf("slave row did not inherit merged master text, got:\n%s", text)
+ td := res.JSON[0].ToMap()["table"].(*entity.TableData)
+ if td.Grid[1][0].Text != "G1" || td.Grid[1][0].RowSpan != 2 {
+ t.Fatalf("origin cell = %+v, want G1 rowspan 2", td.Grid[1][0])
+ }
+ if !td.Grid[2][0].Covered {
+ t.Fatalf("slave cell = %+v, want Covered", td.Grid[2][0])
+ }
+ rows := tableutil.ResolveRows(td, tableutil.RowsTable)
+ if len(rows) != 3 || !reflect.DeepEqual(rows[2], []string{"G1", "y"}) {
+ t.Fatalf("table view row 3 = %v, want [G1 y]", rows)
}
positions := res.JSON[0].ToMap()["positions"].([][]float64)
if len(positions) != 3 {
@@ -568,7 +675,8 @@ func TestXLSXParser_MergedDataCellHorizontalUntouched(t *testing.T) {
// TestXLSXParser_MergedDataCellBeyondTruncation asserts a merged slave cell
// past its row's truncation point (excelize drops trailing empties) is still
-// filled: the row grows exactly up to the slave column to host the value.
+// represented by the origin's rowspan, and the table view resolves it into the
+// truncated row.
func TestXLSXParser_MergedDataCellBeyondTruncation(t *testing.T) {
data := newTestXLSX(t, func(f *excelize.File) {
mustSetCell(t, f, "Sheet1", "A1", "name")
@@ -591,10 +699,11 @@ func TestXLSXParser_MergedDataCellBeyondTruncation(t *testing.T) {
if res.Err != nil {
t.Fatalf("ParseWithResult: %v", res.Err)
}
- text := spreadsheetText(res)
- want := "<tr><td>Mike</td><td>28</td><td>IT</td><td>Important customer</td></tr>"
- if !strings.Contains(text, want) {
- t.Fatalf("truncated slave cells not filled, got:\n%s", text)
+ td := res.JSON[0].ToMap()["table"].(*entity.TableData)
+ rows := tableutil.ResolveRows(td, tableutil.RowsTable)
+ want := []string{"Mike", "28", "IT", "Important customer"}
+ if len(rows) != 3 || !reflect.DeepEqual(rows[2], want) {
+ t.Fatalf("table view row 3 = %v, want %v", rows, want)
}
}
@@ -613,12 +722,140 @@ func TestXLSXParser_MergedHeaderDownIntoDataUntouched(t *testing.T) {
t.Fatalf("ParseWithResult: %v", res.Err)
}
text := spreadsheetText(res)
- if !strings.Contains(text, "<tr><th>H</th><th>Name</th></tr>") {
- t.Fatalf("want header row untouched, got:\n%s", text)
+ if !strings.Contains(text, `<th rowspan="2">H</th>`) {
+ t.Fatalf("want header row to keep its rowspan, got:\n%s", text)
}
if strings.Contains(text, "<td>H</td>") {
t.Fatalf("header merge master leaked into data row, got:\n%s", text)
}
+ td := res.JSON[0].ToMap()["table"].(*entity.TableData)
+ rows := tableutil.ResolveRows(td, tableutil.RowsTable)
+ if len(rows) != 2 || rows[1][0] != "" {
+ t.Fatalf("table view data row must not inherit the header label: %v", rows)
+ }
+}
+
+// TestXLSXParserHeaderBelowMergedTitle asserts a merged title that starts
+// above the detected header and crosses it is materialized into the header
+// cell instead of being lost after the header row is reordered to the front.
+func TestXLSXParserHeaderBelowMergedTitle(t *testing.T) {
+ data := newTestXLSX(t, func(f *excelize.File) {
+ mustSetCell(t, f, "Sheet1", "A1", "Title")
+ mustMergeCell(t, f, "Sheet1", "A1", "A3")
+ mustSetCell(t, f, "Sheet1", "B3", "Name")
+ mustSetCell(t, f, "Sheet1", "C3", "Value")
+ idx := mustNewStyle(t, f, &excelize.Style{Font: &excelize.Font{Bold: true}})
+ mustSetCellStyle(t, f, "Sheet1", "B3", "C3", idx)
+ mustSetCell(t, f, "Sheet1", "B4", "Alice")
+ mustSetCell(t, f, "Sheet1", "C4", "1")
+ })
+ p, _ := NewXLSXParser("")
+ res := p.ParseWithResult(t.Context(), "t.xlsx", data)
+ if res.Err != nil {
+ t.Fatalf("ParseWithResult: %v", res.Err)
+ }
+ text := spreadsheetText(res)
+ if !strings.Contains(text, "<th>Title</th><th>Name</th><th>Value</th>") {
+ t.Fatalf("merged title above the header was lost:\n%s", text)
+ }
+ td := res.JSON[0].ToMap()["table"].(*entity.TableData)
+ if len(td.Grid[0]) != 3 || td.Grid[0][0].Text != "Title" {
+ t.Fatalf("header grid = %+v, want Title in the first column", td.Grid[0])
+ }
+}
+
+// TestXLSXParserFullyCoveredRowRetained asserts a row with no cells of its own
+// is still kept when a non-blank merge master covers it. Dropping the row would
+// let the origin's rowspan continue into the next retained row and misalign it.
+func TestXLSXParserFullyCoveredRowRetained(t *testing.T) {
+ data := newTestXLSX(t, func(f *excelize.File) {
+ mustSetCell(t, f, "Sheet1", "A1", "Group")
+ mustSetCell(t, f, "Sheet1", "B1", "Item")
+ mustSetCell(t, f, "Sheet1", "A2", "G1")
+ mustMergeCell(t, f, "Sheet1", "A2", "A3")
+ mustSetCell(t, f, "Sheet1", "B2", "x")
+ mustSetCell(t, f, "Sheet1", "A4", "G2")
+ mustSetCell(t, f, "Sheet1", "B4", "y")
+ })
+ p, _ := NewXLSXParser("")
+ res := p.ParseWithResult(t.Context(), "t.xlsx", data)
+ if res.Err != nil {
+ t.Fatalf("ParseWithResult: %v", res.Err)
+ }
+ td := res.JSON[0].ToMap()["table"].(*entity.TableData)
+ if len(td.Grid) != 4 {
+ t.Fatalf("grid rows = %d, want header + three data rows: %+v", len(td.Grid), td.Grid)
+ }
+ if !td.Grid[2][0].Covered {
+ t.Fatalf("covered row lost its marker: %+v", td.Grid[2])
+ }
+ rows := tableutil.ResolveRows(td, tableutil.RowsTable)
+ if len(rows) != 4 || rows[2][0] != "G1" || rows[3][0] != "G2" {
+ t.Fatalf("table view rows = %v, want G1 on the covered row and G2 after it", rows)
+ }
+}
+
+// TestXLSXParserHeaderSpanClampedPerSegment asserts a header-origin merge does
+// not reserve its column in a later image segment whose body starts after the
+// merge. Only the segment that actually carries the covered rows owns the span.
+func TestXLSXParserHeaderSpanClampedPerSegment(t *testing.T) {
+ const pngBase64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+M8AAAMBAQDJ/pLvAAAAAElFTkSuQmCC"
+ data := newTestXLSX(t, func(f *excelize.File) {
+ mustSetCell(t, f, "Sheet1", "A1", "Q")
+ mustSetCell(t, f, "Sheet1", "B1", "shared")
+ mustMergeCell(t, f, "Sheet1", "B1", "B2")
+ mustSetCell(t, f, "Sheet1", "A2", "q2")
+ mustSetCell(t, f, "Sheet1", "A3", "q3")
+ mustSetCell(t, f, "Sheet1", "B3", "a3")
+ if err := f.AddPictureFromBytes("Sheet1", "A2", &excelize.Picture{
+ Extension: ".png",
+ File: mustDecodeBase64(t, pngBase64),
+ Format: &excelize.GraphicOptions{AltText: "fig"},
+ }); err != nil {
+ t.Fatalf("AddPictureFromBytes: %v", err)
+ }
+ })
+ p, _ := NewXLSXParser("")
+ res := p.ParseWithResult(t.Context(), "header-span.xlsx", data)
+ if res.Err != nil {
+ t.Fatalf("ParseWithResult: %v", res.Err)
+ }
+ items := ItemsToMaps(res.JSON)
+ if len(items) != 3 {
+ t.Fatalf("items = %d, want table, image, table", len(items))
+ }
+ first := items[0]["table"].(*entity.TableData)
+ if first.Grid[0][1].RowSpan != 2 || !first.Grid[1][1].Covered {
+ t.Fatalf("first segment header span = %+v, want rowspan 2 with a covered row", first.Grid)
+ }
+ second := items[2]["table"].(*entity.TableData)
+ if second.Grid[0][1].RowSpan > 1 {
+ t.Fatalf("second segment header span = %d, want it clamped", second.Grid[0][1].RowSpan)
+ }
+ if second.Grid[1][1].Covered || second.Grid[1][1].Text != "a3" {
+ t.Fatalf("second segment body = %+v, want a3 not covered", second.Grid[1])
+ }
+}
+
+// TestMaterializeDetachedMergesCapsWidth asserts a crafted far merge that
+// crosses the header cannot allocate one cell per worksheet column.
+func TestMaterializeDetachedMergesCapsWidth(t *testing.T) {
+ rows := map[int][]string{
+ 1: {"Title"},
+ 2: {},
+ 3: {"", "Name"},
+ }
+ ranges := []mergeRange{{sr: 1, sc: 1, er: 3, ec: 16384}}
+ kept := materializeDetachedMerges(rows, 3, ranges)
+ if len(kept) != 0 {
+ t.Fatalf("detached merge left geometry: %v", kept)
+ }
+ if len(rows[3]) != maxMergeExtentCols {
+ t.Fatalf("header width = %d, want capped at %d", len(rows[3]), maxMergeExtentCols)
+ }
+ if rows[3][0] != "Title" || rows[3][1] != "Name" {
+ t.Fatalf("header cells = %q, want Title/Name", rows[3][:2])
+ }
}
// TestXLSXParser_StripsInvisibleFormatChars asserts zero-width format
@@ -878,26 +1115,6 @@ func TestDetectHeaderRow_StyledTextHeaderOverNumeric(t *testing.T) {
}
}
-// TestInheritMergedHeader unit-tests the merge inheritance helper directly.
-func TestInheritMergedHeader(t *testing.T) {
- records := [][]string{
- {"MASTER", "", "Q2"}, // header row: B1 blank, merged from master A1
- {"a", "b", "c"},
- }
- mm := map[[2]int][2]int{
- {1, 1}: {1, 1},
- {1, 2}: {1, 1}, // B1's master is A1
- {1, 3}: {1, 3},
- }
- inheritMergedHeader(records, 1, mm)
- if records[0][1] != "MASTER" {
- t.Fatalf("expected B1 to inherit A1 master text, got %q", records[0][1])
- }
- if records[0][2] != "Q2" {
- t.Fatalf("expected non-merged C1 to keep its value, got %q", records[0][2])
- }
-}
-
// TestPadRowToWidth asserts the single-row padder widens with empty strings,
// preserves existing cells, and never shrinks an already-wide row.
func TestPadRowToWidth(t *testing.T) {
diff --git a/internal/parser/parser/xls_parser.go b/internal/parser/parser/xls_parser.go
index 1e424ee517..ed855ffae2 100644
--- a/internal/parser/parser/xls_parser.go
+++ b/internal/parser/parser/xls_parser.go
@@ -131,7 +131,7 @@ func parseBIFF8Bytes(ctx context.Context, data []byte) ([]map[string]any, int, e
if err := ctx.Err(); err != nil {
return nil, sheetCount, err
}
- items = append(items, buildSheetItems(records, reader.SheetName(), sheetIndex+1, headerRow, dataRows, nil)...)
+ items = append(items, buildSheetItems(records, reader.SheetName(), sheetIndex+1, headerRow, dataRows, nil, nil)...)
}
if err := reader.Err(); err != nil {
return nil, sheetCount, fmt.Errorf("read BIFF workbook: %w", err)
@@ -140,6 +140,10 @@ func parseBIFF8Bytes(ctx context.Context, data []byte) ([]map[string]any, int, e
}
func readBIFF8Records(ctx context.Context, reader excel.Reader) ([][]string, []int, int) {
+ // The BIFF reader exposes cell values but not merged ranges, so .xls rows
+ // are emitted without ColSpan/RowSpan geometry. A vertical merge in a
+ // legacy workbook therefore stays a dense row with an empty slave cell;
+ // xlsx is the format that carries full merge geometry into TableData.
type numberedRow struct {
number int
cells []string
diff --git a/internal/parser/parser/xlsx_parser.go b/internal/parser/parser/xlsx_parser.go
index 8e86ffd7d7..c5fde577e1 100644
--- a/internal/parser/parser/xlsx_parser.go
+++ b/internal/parser/parser/xlsx_parser.go
@@ -148,7 +148,7 @@ func parseXLSXBytes(data []byte, mediaBudget *embeddedMediaBudget) ([]map[string
items := make([]map[string]any, 0)
warnings := make([]string, 0)
for sheetIdx, sheet := range sheets {
- records, dataRows, headerRow, sheetWarnings, err := readSpreadsheetRecords(f, sheet)
+ records, dataRows, headerRow, ranges, sheetWarnings, err := readSpreadsheetRecords(f, sheet)
if err != nil {
return nil, warnings, len(sheets), err
}
@@ -162,7 +162,7 @@ func parseXLSXBytes(data []byte, mediaBudget *embeddedMediaBudget) ([]map[string
}
// One wire shape for every spreadsheet sheet: segmented HTML tables
// with row-aligned positions, images interleaved at their anchors.
- items = append(items, buildSheetItems(records, sheet, sheetIdx+1, headerRow, dataRows, images)...)
+ items = append(items, buildSheetItems(records, sheet, sheetIdx+1, headerRow, dataRows, images, ranges)...)
warnings = append(warnings, imageWarnings...)
}
return items, warnings, len(sheets), nil
diff --git a/internal/parser/tableutil/table_rows.go b/internal/parser/tableutil/table_rows.go
new file mode 100644
index 0000000000..33f08e5d72
--- /dev/null
+++ b/internal/parser/tableutil/table_rows.go
@@ -0,0 +1,128 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package tableutil
+
+import "ragflow/internal/entity"
+
+// RowMode selects how ResolveRows interprets a TableData's merge geometry.
+//
+// The grid stores merge geometry once: an origin cell carries ColSpan/RowSpan
+// and the positions it engulfs are Covered markers. How those Covered slots
+// are projected into text is a consumer decision:
+//
+// - RowsTable keeps every logical column filled. A data-origin span is
+// repeated across its covered slots so a row projected under a header stays
+// column-aligned; a header-origin span is repeated only inside the header
+// block and is never stamped into data rows.
+// - RowsData is the general-context view: data-origin row spans are resolved
+// at the origin column, header-origin spans stay out of data rows, and a
+// colspan is not repeated (its value already appears once).
+// - RowsQA treats every row as data. All row spans (including one mastered
+// on the first row) are projected at the origin column, while col spans are
+// not repeated: a question that covers two columns must still pair with the
+// answer in the next non-empty cell, not with a copy of itself.
+type RowMode int
+
+const (
+ // RowsTable is the tabular view consumed by the table chunker.
+ RowsTable RowMode = iota
+ // RowsData is the data-row view consumed by the general chunker.
+ RowsData
+ // RowsQA is the all-rows-are-data view consumed by the QA chunker.
+ RowsQA
+)
+
+// ResolveRows projects a TableData grid into row-major cell text according to
+// mode. It deliberately derives spans from RowSpan/ColSpan/Covered alone so a
+// caller does not depend on OriginRow/OriginCol bookkeeping; those fields are
+// consumed by the table splitter when it clamps a span to a part.
+func ResolveRows(td *entity.TableData, mode RowMode) [][]string {
+ if td == nil {
+ return nil
+ }
+ out := make([][]string, len(td.Grid))
+ type activeSpan struct {
+ text string
+ left int
+ originRow int
+ }
+ active := map[int]*activeSpan{}
+
+ for r, row := range td.Grid {
+ cells := make([]string, len(row))
+ colspanText := ""
+ colspanLeft := 0
+ for c, cell := range row {
+ if cell.Covered {
+ if sp := active[c]; sp != nil && sp.left > 0 {
+ // A header-origin span must not leak into a data row in
+ // the table view: the header is rendered separately and
+ // repeating its label would turn a column name into a
+ // value. QA asks for the opposite projection.
+ if mode == RowsQA || r < td.HeaderRows || sp.originRow >= td.HeaderRows {
+ cells[c] = sp.text
+ }
+ } else if colspanLeft > 0 {
+ // RowsTable keeps the full column count; RowsQA leaves the
+ // engulfed slots empty so first-two-cell pairing is not
+ // confused by a duplicated question.
+ if mode == RowsTable {
+ cells[c] = colspanText
+ }
+ }
+ if colspanLeft > 0 {
+ colspanLeft--
+ }
+ continue
+ }
+
+ cells[c] = cell.Text
+ if cell.RowSpan > 1 {
+ if mode == RowsTable {
+ spanCols := cell.ColSpan
+ if spanCols < 1 {
+ spanCols = 1
+ }
+ for cc := c; cc < c+spanCols; cc++ {
+ active[cc] = &activeSpan{text: cell.Text, left: cell.RowSpan, originRow: r}
+ }
+ } else {
+ // One value belongs in the origin column. RowsData and
+ // RowsQA must not duplicate it across a horizontal span:
+ // general context would repeat the text, and QA would pair
+ // a question with a copy of itself.
+ active[c] = &activeSpan{text: cell.Text, left: cell.RowSpan, originRow: r}
+ }
+ }
+ if cell.ColSpan > 1 {
+ colspanText = cell.Text
+ colspanLeft = cell.ColSpan
+ }
+ if colspanLeft > 0 {
+ colspanLeft--
+ }
+ }
+ for c, sp := range active {
+ sp.left--
+ if sp.left <= 0 {
+ delete(active, c)
+ }
+ }
+ out[r] = cells
+ }
+ return out
+}
diff --git a/internal/parser/tableutil/table_rows_test.go b/internal/parser/tableutil/table_rows_test.go
new file mode 100644
index 0000000000..4b47ec724b
--- /dev/null
+++ b/internal/parser/tableutil/table_rows_test.go
@@ -0,0 +1,109 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package tableutil
+
+import (
+ "reflect"
+ "testing"
+
+ "ragflow/internal/entity"
+)
+
+func TestResolveRowsHeaderOriginSpan(t *testing.T) {
+ td := &entity.TableData{
+ HeaderRows: 1,
+ Grid: [][]entity.TableCell{
+ {{Text: "H", RowSpan: 2}, {Text: "Name"}},
+ {{Covered: true}, {Text: "Alice"}},
+ },
+ }
+ table := ResolveRows(td, RowsTable)
+ if want := [][]string{{"H", "Name"}, {"", "Alice"}}; !reflect.DeepEqual(table, want) {
+ t.Fatalf("table view = %v, want %v", table, want)
+ }
+ data := ResolveRows(td, RowsData)
+ if want := [][]string{{"H", "Name"}, {"", "Alice"}}; !reflect.DeepEqual(data, want) {
+ t.Fatalf("data view = %v, want %v", data, want)
+ }
+ qa := ResolveRows(td, RowsQA)
+ if want := [][]string{{"H", "Name"}, {"H", "Alice"}}; !reflect.DeepEqual(qa, want) {
+ t.Fatalf("QA view = %v, want %v", qa, want)
+ }
+}
+
+func TestResolveRowsDataOriginSpan(t *testing.T) {
+ td := &entity.TableData{
+ HeaderRows: 1,
+ Grid: [][]entity.TableCell{
+ {{Text: "Group"}, {Text: "Item"}},
+ {{Text: "G1", RowSpan: 2}, {Text: "x"}},
+ {{Covered: true}, {Text: "y"}},
+ },
+ }
+ want := [][]string{{"Group", "Item"}, {"G1", "x"}, {"G1", "y"}}
+ if got := ResolveRows(td, RowsTable); !reflect.DeepEqual(got, want) {
+ t.Fatalf("table view = %v, want %v", got, want)
+ }
+ if got := ResolveRows(td, RowsQA); !reflect.DeepEqual(got, want) {
+ t.Fatalf("QA view = %v, want %v", got, want)
+ }
+}
+
+func TestResolveRowsQAKeepsColspanSingle(t *testing.T) {
+ td := &entity.TableData{
+ HeaderRows: 1,
+ Grid: [][]entity.TableCell{
+ {{Text: "q", ColSpan: 2}, {Covered: true}, {Text: "a"}},
+ },
+ }
+ if got, want := ResolveRows(td, RowsTable), [][]string{{"q", "q", "a"}}; !reflect.DeepEqual(got, want) {
+ t.Fatalf("table view = %v, want %v", got, want)
+ }
+ if got, want := ResolveRows(td, RowsQA), [][]string{{"q", "", "a"}}; !reflect.DeepEqual(got, want) {
+ t.Fatalf("QA view = %v, want %v", got, want)
+ }
+}
+
+func TestResolveRowsRowColSpanBlock(t *testing.T) {
+ td := &entity.TableData{
+ HeaderRows: 1,
+ Grid: [][]entity.TableCell{
+ {{Text: "Name"}, {Text: "B"}, {Text: "C"}},
+ {{Text: "Wide", RowSpan: 2, ColSpan: 2}, {Covered: true}, {Text: "x"}},
+ {{Covered: true}, {Covered: true}, {Text: "y"}},
+ },
+ }
+ tableWant := [][]string{
+ {"Name", "B", "C"},
+ {"Wide", "Wide", "x"},
+ {"Wide", "Wide", "y"},
+ }
+ if got := ResolveRows(td, RowsTable); !reflect.DeepEqual(got, tableWant) {
+ t.Fatalf("table view = %v, want %v", got, tableWant)
+ }
+ qaWant := [][]string{
+ {"Name", "B", "C"},
+ {"Wide", "", "x"},
+ {"Wide", "", "y"},
+ }
+ if got := ResolveRows(td, RowsQA); !reflect.DeepEqual(got, qaWant) {
+ t.Fatalf("QA view = %v, want %v", got, qaWant)
+ }
+ if got := ResolveRows(td, RowsData); !reflect.DeepEqual(got, qaWant) {
+ t.Fatalf("data view = %v, want %v", got, qaWant)
+ }
+}