Files
NianJiu 60dbbc80b2 fix(data-analysis): auto-detect Excel header rows, check result consistency, and surface analysis failures
Why:
- `LoadFromExcel` unconditionally executed `read_xlsx(..., header=true, all_varchar=true)` starting at row 1. For workbooks with a leading cross-column merged title banner, subtitle/metadata notes, or blank rows above the real header, DuckDB locked onto row 1 (even with `header=false`), collapsing the entire sheet to 1 column (`[2026 年度销售统计]`) and propagating the broken schema into `DataTableSummaryService` (`table_summary`/`table_column` chunks), `data_schema`, and SQL execution (#5627).
- `DataAnalysisTool.Execute` returned query rows without checking whether grand-total rows reconciled with detail sums or whether unfiltered partition share columns summed to ~1 / ~100%.
- When `PluginDataAnalysis` failed to load or execute SQL in the chat pipeline, it logged a warning and continued silently without informing the answer prompt, and the `data_analyst` system prompt did not explicitly forbid falling back to manual LLM arithmetic over raw table excerpts on failure.

What:
- Add streaming OOXML header detection (`internal/agent/tools/excel_header.go`, sharing workbook relationship resolution with `excel_sheets.go`) that samples leading non-empty rows, resolves only needed shared strings, inspects `<mergeCells>` and contiguous column bounds only when row 1 is narrower than subsequent rows (O(1) fast path for normal workbooks), and guards against pathological wide columns (e.g. stray `XFD` cells) that would OOM DuckDB.
- Update `LoadFromExcel` and `buildExcelCreateTableSQLWithRanges` to pass per-sheet `range = '<startCol><headerRow>:<endCol>'` with `stop_at_empty=true` (preventing DuckDB from materializing 1M trailing NULL rows when `range` is explicit) only for sheets with preamble rows, keeping legacy SQL byte-for-byte identical for normal sheets, CSVs, and multi-sheet `UNION ALL BY NAME` workbooks. Because `DataTableSummaryService` reuses `LoadFromKnowledge`, ingestion schema chunks and `data_schema` automatically receive the true columns.
- Add advisory SQL result consistency checks (`internal/agent/tools/data_analysis_consistency.go`) to warn when a single grand-total row disagrees with the sum of additive detail rows or when an unfiltered, non-limited partition share column fails to sum to ~1 / ~100%, skipping filtered/limited subsets and non-share rates/weights to avoid false alarms.
- Record `PipelineState.DataAnalysisFailure` in `PluginDataAnalysis.OnEvent` on load/SQL failure and append a sanitized `[Data Analysis Notice: ...]` in `prepareMessagesWithModelContext` (without polluting `MergeResult` citations or leaking UUIDs), and harden the `data_analyst` prompt template to require SQL computation for statistics and explicit failure reporting instead of manual arithmetic.
- Add end-to-end tests against real DuckDB covering merged banners, unmerged notes, multi-row/corner metadata preambles, offset tables, pivot headers, footer exclusion, mixed multi-sheet workbooks, renamed `.xls`, `DataTableSummaryService.Handle`, `PluginDataAnalysis` -> `prepareMessagesWithModelContext`, and consistency warnings.
2026-11-19 15:27:21 +08:00
..