test(agent): add end-to-end DuckDB excel tests and fix sheet enumeration (#1007)

Catching up on a real-world check: when a multi-sheet .xlsx fixture is
fed through LoadFromExcel against an in-memory DuckDB with the spatial
and excel extensions, the first iteration's sheet enumeration quietly
fell back because st_read_meta doesn't expose a scalar 'layer_name'
column — it returns a LIST<STRUCT> column called 'layers'. We now
UNNEST(layers).name so enumeration actually works, and assert the full
data path with three DuckDB-backed tests:

  - multi-sheet workbook yields the sum of all rows and per-sheet
    breakdown via __sheet_name (including schema drift: columns only
    present in one sheet are NULL for the other)
  - single-sheet workbook still works and tags rows with its sheet name
  - sheet name containing a single quote (Q1'24) survives the SQL
    literal round-trip

excelize/v2 is added as a test-time helper to build the fixtures
deterministically. If DuckDB's spatial/excel extensions can't be
installed (offline CI), the tests skip rather than fail.

Refs: https://github.com/Tencent/WeKnora/issues/1007
This commit is contained in:
wizardchen
2026-04-24 19:37:39 +08:00
committed by lyingbug
parent 11811f9bf8
commit 002d98a640
4 changed files with 334 additions and 24 deletions
+8 -1
View File
@@ -364,8 +364,15 @@ func (t *DataAnalysisTool) LoadFromExcel(ctx context.Context, filename string, t
// listExcelSheets returns the names of every sheet (layer) inside the given
// Excel workbook by querying DuckDB's spatial st_read_meta table function.
// The returned slice preserves the on-disk order of sheets.
//
// st_read_meta returns a single row whose `layers` column is a LIST of
// STRUCTs (one per layer / sheet). We UNNEST that list and project the
// struct's `name` field to get a flat list of sheet names.
func (t *DataAnalysisTool) listExcelSheets(ctx context.Context, filename string) ([]string, error) {
metaSQL := fmt.Sprintf("SELECT layer_name FROM st_read_meta('%s')", sqlSingleQuoteEscape(filename))
metaSQL := fmt.Sprintf(
"SELECT UNNEST(layers).name FROM st_read_meta('%s')",
sqlSingleQuoteEscape(filename),
)
rows, err := t.db.QueryContext(ctx, metaSQL)
if err != nil {