Skip to content

feat(explain): numeric cost and gap columns in the EXPLAIN CSV export - #1237

Merged
debba merged 2 commits into
TabularisDB:mainfrom
itsmunzir:feat/1231-explain-csv-polish
Oct 23, 2026
Merged

debba merged 2 commits into
TabularisDB:mainfrom
itsmunzir:feat/1231-explain-csv-polish

Conversation

@itsmunzir

@itsmunzir itsmunzir commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Follow-ups from the review of #1184, as listed in #1231. The EXPLAIN table CSV now has numeric cost and estimate gap columns, the table view and the CSV build their rows with the same code, a successful export shows a toast, and a failed one shows the reason.

What changed

Numeric columns

  • cost ("0 - 150.25") is split into startup_cost and total_cost, both plain numbers.
  • estimate_gap is a plain number (3, not "3.00x").
  • Unavailable values stay empty, never 0, as before. A real startup cost of 0 is still written as 0. Non-finite values are written as empty cells.
  • Costs and row counts are written exactly as the database reported them. Self time and estimate gap, which Tabularis calculates, are rounded to 3 decimals so the file does not carry float noise like 3.3333333333333335.
  • Numbers never use exponent notation: 1e21 is written as 1000000000000000000000 and 1.5e-7 as 0.00000015, with the same digits String() gives, so no precision is lost.

New header:

index,depth,node_type,relation,startup_cost,total_cost,est_rows,actual_rows,self_time_ms,estimate_gap,filter

One formatting source

  • New getExplainTableRow(node, hasAnalyzeData, nodeMetrics) in packages/explain/src/plan.ts returns the raw values for one row: costs, rows, self time, estimate gap and filter. It also hides measured values when the plan has no ANALYZE data.
  • getExplainTableRows (used by the CSV) and ExplainTableView's TreeRows (the screen) both call it. Each side only decides how to display the numbers (abbreviated on screen, plain in the file).
  • New getRowEstimateGap(node) replaces the private getMismatchMagnitude and the two inline copies of the "ratio or its inverse" maths. The amber highlight in the table now reads gap >= 4, which is the same condition as the old ratio >= 4 || ratio <= 0.25.
  • Neither helper is added to the package's public index.ts, so the public API does not grow. ExplainTableRowData changes shape (cost becomes startupCost and totalCost). It was added in feat(explain): export complete EXPLAIN table as CSV (#1163) #1184 and has not been published to npm yet (the latest release, 0.2.0, came before it).

Feedback

  • Success: showToast(..., { kind: "success" }) replaces the modal showAlert.
  • Failure: an error toast titled with the existing exportCsvError string, with the reason from toErrorMessage(err) as the message (for example permission denied, or a plain string rejected by Tauri). It uses duration: 0, so it stays until dismissed and the reason can be read and reported. Connections.tsx does the same for its error toasts. The error is still logged to the console.
  • No new strings and no new styling. The existing toast component and i18n keys are reused.
  • The export goes through main's downloadTextFile with the platform from usePlatformCapabilities, so it works in the desktop app and in tabularis web.

CRLF

  • Every record, the last one included, ends with \r\n. Line breaks inside quoted fields (for example a multi-line filter) are left as they are, which RFC 4180 allows.

Cleanups

How tested

  • pnpm typecheck and pnpm typecheck:explain: clean.
  • pnpm lint: clean (ESLint, Prettier check and the theme token check).
  • pnpm test run: 456 files, 7190 tests. All pass except 5 in web-ui-project/tests/runWebUiTasks.test.mjs, which fail the same way on main locally.
  • packages/explain, npx vitest run: 11 files, 183 tests passed.

New and updated tests:

  • packages/web-ui/tests/utils/explainTableExport.test.ts: the new header, CRLF on every record with no bare LF, numeric cost and gap values for estimated and analyzed plans, a startup cost of 0 kept as 0, missing costs (SQLite-style), a total cost with no startup cost (MySQL-style), NaN and Infinity costs, the estimate gap left empty when estimates or actual rows are missing or zero, an overestimate rounded to 3.333, quoting, the semicolon delimiter, very large, very small and negative costs written without exponent notation, and a cost of 2^53 + 2 keeping every digit.
  • packages/explain/tests/plan.test.ts: getRowEstimateGap in both directions plus the null cases, and getExplainTableRow (raw costs, ANALYZE gating, index and depth fallback, and that it matches the row from getExplainTableRows).
  • packages/web-ui/tests/components/explain/ExplainTableView.test.tsx: the table shows the cost, rows, time and gap from the shared row, and - instead of 0 when a value is missing.
  • packages/web-ui/tests/components/explain/VisualExplainView.test.tsx: through the platform download and writeFile, as main's raw-export tests do. Success opens the save dialog with explain-plan.csv, writes the CSV once and shows a toast, not an alert. Cancelling the dialog writes nothing and shows nothing. A write error shows a persistent error toast with the reason, and the button can be used again. A plain string rejection is shown as the reason.

Checked in tabularis web (server built with --no-default-features, local PostgreSQL 17, EXPLAIN ANALYZE, Table view → Export CSV): success shows the toast and downloads explain-plan.csv as text/csv with CRLF records and plain numbers. A download error forced from the page shows the persistent error toast with the reason. In web mode the browser handles the save, so a cancel in the browser's own prompt can't be seen by the page (same as the raw-plan export). I haven't checked the desktop window by hand.

Closes #1231

@kilo-code-bot

kilo-code-bot Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Notes from review:

  • New since the last review: plainDecimal in explainTableExport.ts expands exponent notation (1e+21, 1.5e-7) into plain decimals, so spreadsheets don't read those cells as text. I traced it by hand for 1e21, 1.2345e25, -2.5e22, 1.5e-7 and 1.234e-10. Each gives the expected string with no lost digits, and the new tests cover those values.
  • Unchanged and still correct: getExplainTableRow feeds both the table view and the CSV, null and non-finite values stay empty, a real startup cost of 0 is kept, and every record ends with CRLF.
  • The error toast keeps the underlying reason and stays until dismissed.
  • Assumption: this was a read-only review, so I did not run the tests or the app. The PR notes that the toast and saved file were not checked by hand in the desktop window, so a quick manual check is worthwhile.
Files Reviewed (9 files)
  • packages/explain/src/components/ExplainTableView.tsx
  • packages/explain/src/plan.ts
  • packages/explain/tests/plan.test.ts
  • packages/web-ui/src/components/explain/VisualExplainView.tsx
  • packages/web-ui/src/i18n/locales/ja.json
  • packages/web-ui/src/utils/explainTableExport.ts
  • packages/web-ui/tests/components/explain/ExplainTableView.test.tsx
  • packages/web-ui/tests/components/explain/VisualExplainView.test.tsx
  • packages/web-ui/tests/utils/explainTableExport.test.ts
Previous Review Summaries (2 snapshots, latest commit 2f0298b)

Current summary above is authoritative. Previous snapshots are kept for context only.

Previous review (commit 2f0298b)

Status: No Issues Found | Recommendation: Merge

Notes from review:

  • The PR was rebased onto the Prettier-formatted main. The reviewed logic is unchanged: getExplainTableRow still feeds both the table view and the CSV, and the estimateGap >= 4 amber highlight still matches the old ratio >= 4 || ratio <= 0.25.
  • CSV output keeps the separate startup_cost and total_cost columns. Null and non-finite values stay empty, a real startup cost of 0 is kept, and self time and estimate gap are rounded to 3 decimals.
  • Every record ends with CRLF, including the last one.
  • The error toast keeps the underlying reason through toErrorMessage and stays until dismissed.
  • Assumption: this was a read-only review, so I did not run the tests or the app. The PR notes that the toast and saved file were not checked by hand in the desktop window, so a quick manual check is worthwhile.
Files Reviewed (9 files)
  • packages/explain/src/components/ExplainTableView.tsx
  • packages/explain/src/plan.ts
  • packages/explain/tests/plan.test.ts
  • src/components/explain/VisualExplainView.tsx
  • src/i18n/locales/ja.json
  • src/utils/explainTableExport.ts
  • tests/components/explain/ExplainTableView.test.tsx
  • tests/components/explain/VisualExplainView.test.tsx
  • tests/utils/explainTableExport.test.ts

Previous review (commit fd7ce92)

Status: No Issues Found | Recommendation: Merge

Notes from review:

  • getExplainTableRow is now the single source for the table view and the CSV. The amber highlight condition estimateGap >= 4 is equivalent to the previous ratio >= 4 || ratio <= 0.25.
  • The ExplainTableRowData shape change (cost replaced by startupCost/totalCost) has no other consumers in the repo. The package is at 0.2.0, which predates the type.
  • CSV numeric columns leave null and non-finite values as empty cells. A startup cost of 0 is kept as 0. Self time and estimate gap are rounded to 3 decimals.
  • Every CSV record ends with CRLF. Line breaks inside quoted fields are preserved.
  • The error toast uses toErrorMessage, which handles strings, Error objects and Tauri-style {message} objects.
  • Assumption: I did not run the tests or the app (read-only review). The PR notes that the toast and saved file were not checked by hand in the desktop window, so a quick manual check is worthwhile.
Files Reviewed (9 files)
  • packages/explain/src/components/ExplainTableView.tsx
  • packages/explain/src/plan.ts
  • packages/explain/tests/plan.test.ts
  • src/components/explain/VisualExplainView.tsx
  • src/i18n/locales/ja.json
  • src/utils/explainTableExport.ts
  • tests/components/explain/ExplainTableView.test.tsx
  • tests/components/explain/VisualExplainView.test.tsx
  • tests/utils/explainTableExport.test.ts

Reviewed by claude-sonnet-5.5 · Input: 248 · Output: 2K · Cached: 296.5K

Review guidance: REVIEW.md from base branch main

@itsmunzir
itsmunzir force-pushed the feat/1231-explain-csv-polish branch from fd7ce92 to 2f0298b Compare October 21, 2026 15:30

@debba debba left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! Having a single raw-value row builder (getExplainTableRow) feed both the tree view and the CSV, with each side doing only its display formatting, is a really nice cleanup. Replacing getMismatchMagnitude and the inline copies with getRowEstimateGap keeps the amber highlight exactly equivalent (gap >= 4). Every point in #1231 is covered.

The code itself is fine. What blocks it is the rebase after #676:

  • packages/web-ui/src/utils/explainTableExport.ts: saveExplainTableCsv now takes platform and uses downloadTextFile instead of save + writeTextFile.
  • VisualExplainView.tsx: a platform argument and dependency were added.

Both are easy: keep platform and add your toast code on top. One trap to watch for: git auto-merges packages/web-ui/tests/components/explain/VisualExplainView.test.tsx, but the result is broken. Main only mocks writeFile, while your new CSV tests still use mockWriteTextFile, which is no longer declared, so the whole suite fails. Those tests need rewriting against writeFile / downloadTextFile, like the raw-export tests on main.

Small nits:

  • csvNumber uses String(value), so very large or very small values end up as 1e+21. Unlikely for costs.
  • The export now goes through downloadTextFile, so it would be good to also check success, cancel and error in tabularis web, not only in the desktop app.

Follow-ups from the review of TabularisDB#1184.

- The CSV now has separate startup_cost and total_cost columns and writes
  estimate_gap as a plain number, so all three sort and chart in a
  spreadsheet. Unavailable values stay empty, never 0.
- The table view and the CSV both build their rows with getExplainTableRow,
  so the values they show cannot drift apart. getRowEstimateGap replaces the
  private mismatch helper and the two inline copies of the same maths.
- Every record ends with CRLF, as RFC 4180 describes.
- A successful export shows a success toast instead of a modal alert. A
  failed one shows an error toast that keeps the underlying reason and stays
  until dismissed.
- Re-indent the table block in ExplainTableView and use a more natural ja
  translation for the table title.

Closes TabularisDB#1231
String(value) turns very large or very small numbers into 1e+21 or
1.5e-7. The CSV now expands them to plain decimals, keeping the digits
String() picks, so normal values are unchanged and no precision is lost.
@itsmunzir
itsmunzir force-pushed the feat/1231-explain-csv-polish branch from 2f0298b to 0ebf89d Compare October 23, 2026 18:40
@itsmunzir

Copy link
Copy Markdown
Contributor Author

Thanks for the review, and for flagging the test file trap. It would have been easy to miss.

Rebased onto main:

  • explainTableExport.ts: kept main's saveExplainTableCsv(platform, ...) with downloadTextFile. The save / writeTextFile imports are gone. The numeric columns and CRLF changes sit on top.
  • VisualExplainView.tsx: kept usePlatformCapabilities() and the platform argument and dependency, with the toast code on top. The callback depends on [plan, metrics, platform, showToast, t].
  • VisualExplainView.test.tsx: rewrote the CSV tests against writeFile, like the raw-export tests on main. They go through the Tauri-backed platform, so downloadTextFile → downloadFile → save dialog → writeFile all runs:
    • success: the dialog gets explain-plan.csv with the CSV filter, writeFile is called once with that path, the decoded bytes start with the new header, and a success toast shows instead of an alert;
    • cancel: the dialog returns null, so there's no write, no toast and no alert;
    • error: writeFile rejects, so a persistent error toast shows the reason and the button can be used again. A plain string rejection is shown as the reason too.

Nits:

csvNumber. Numbers are now written without exponent notation. A small plainDecimal helper takes the digits String() picks (the shortest that round-trip) and only moves the decimal point. So 1e21 → 1000000000000000000000 and 1.5e-7 → 0.00000015, normal values such as 123456.789 stay the same, and no precision is lost or made up. Tests cover large, small and negative values, and a cost of 2^53 + 2 keeping every digit.

tabularis web. I built the server with cargo build --no-default-features and the bundle with pnpm --filter @tabularis/web-ui build, then ran tabularis web --web-root packages/web-ui/dist against a local PostgreSQL 17. I ran Visual Explain with ANALYZE on a GROUP BY ... ORDER BY ... LIMIT query and used Export CSV in the Table view:

  • Success: the "Explain table exported as CSV" success toast shows. The download is explain-plan.csv, text/csv, with CRLF records and plain numbers:
    index,depth,node_type,relation,startup_cost,total_cost,est_rows,actual_rows,self_time_ms,estimate_gap,filter
    1,0,Limit,,284.43,284.45,10,10,0.001,1,
    2,1,Sort,,284.43,296.76,4933,10,0.587,493.3,
    3,2,Aggregate,,116.16,177.83,4933,4934,1.994,1,
    4,3,Seq Scan,orders,0,91.5,4933,4934,0.585,1,(total > '100'::numeric)
    
    (I read the Blob in the page rather than letting the browser save the file.)
  • Error: I made the browser download throw from the page (URL.createObjectURL). The error toast shows "Failed to export EXPLAIN table as CSV" with the reason, it stays until dismissed, and the button works again.
  • Cancel: in web mode the page has nothing to cancel. BrowserPlatformCapabilities.downloadFile hands the Blob to the browser through an <a download> click and always returns true. If the browser asks where to save and the user cancels there, the page can't tell, and the success toast has already shown. The raw-plan export on main behaves the same way. If you'd prefer different wording in web mode (for example "download started"), I can add it here or in a follow-up.

Typecheck (web-ui and explain), pnpm lint (ESLint, Prettier, theme tokens) and both test suites pass. The only failures are the five in web-ui-project/tests/runWebUiTasks.test.mjs, which fail the same way on main on my machine.

@debba debba left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the careful rebase, and for rewriting the CSV tests against writeFile instead of letting the broken auto-merge through. Going through the real Tauri platform path (dialog, downloadTextFile, writeFile) is exactly what I had in mind, and the plain string rejection case is a nice touch. The plainDecimal fix is also cleaner than I expected: keeping the digits String() picks and only moving the decimal point means normal values don't change at all.

I checked it out locally. The explain package tests (183), the three web-ui test files (36) and pnpm build all pass, and CI is green. Every point from my previous review is covered, so I'm approving.

Two things that aren't for this PR, just possible follow-ups if you feel like it:

  • The raw plan export in VisualExplainView.tsx still uses the modal showAlert, so the two export buttons in the same view now give different feedback. Moving it to toasts too would make them consistent.
  • In web mode BrowserPlatformCapabilities.downloadFile always returns true, so the success toast shows even if the user cancels the browser's save prompt. Your "download started" idea makes sense, but I'd handle it once for every download rather than here.

I'll run a few manual checks in the desktop app before merging, since that's the one path you couldn't try by hand. Thanks again!

@debba
debba merged commit 5caeaf7 into TabularisDB:main Oct 23, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Explain CSV export: numeric cost/gap columns, shared formatting and toast feedback

2 participants