Skip to content

sheet: add sort command with vim-parity flags; fix rowid-shadow bug i… - #677

Merged
liquidaty merged 2 commits into
mainfrom
sheet-sort-cmd
Aug 9, 2026
Merged

liquidaty merged 2 commits into
mainfrom
sheet-sort-cmd

Conversation

@liquidaty

Copy link
Copy Markdown
Owner

…n where/pivot

Add sort/sortdesc/sortexpr to the interactive zsv sheet viewer, implemented over the existing SQLite path used by where and pivot: expose the buffer's backing CSV as virtual table data, run select ... order by ..., and materialize the result into a new buffer.

Commands and keys:

  • o / :sort <column> [asc|desc] [n|f] ascending column sort
  • O / :sortdesc (alias :sort!) descending
  • :sortexpr <SQL expr> [asc|desc] order by an arbitrary expression

Column sorts group blank/whitespace/missing cells last, sort numeric-looking cells numerically before text, and fold case for ASCII text. Sorting the synthesized Row# column restores original order. n/f flags give vim-style numeric sorts (n = leading integer, f = float); a flag-shaped token yields to a real column of the same name. Sorts are guaranteed stable via a rowid tiebreak, which also makes a re-sort a true secondary sort on a derived buffer.

Fix a pre-existing defect in where and pivot drill-down (and the new sort sites): select ROWID as "Row #", * binds ROWID to a data column literally named rowid, showing that column's values as row numbers. New shared helper zsvsheet_sql_rowid_ref() in sheet-sql.c picks the first unshadowed implicit-rowid spelling (ROWID/ROWID/OID); all three commands use it.

Tested via the tmux-driven test-sheet harness with discriminating fixtures for each clause (numeric grouping, direction, flag/column ambiguity, stability tiebreak, and the rowid-shadow and triple-shadow cases); clean under ASan+UBSan and leak checking.

liquidaty and others added 2 commits August 8, 2026 23:31
…n where/pivot

Add `sort`/`sortdesc`/`sortexpr` to the interactive `zsv sheet` viewer,
implemented over the existing SQLite path used by `where` and `pivot`: expose
the buffer's backing CSV as virtual table `data`, run `select ... order by ...`,
and materialize the result into a new buffer.

Commands and keys:
- `o` / `:sort <column> [asc|desc] [n|f]`  ascending column sort
- `O` / `:sortdesc` (alias `:sort!`)        descending
- `:sortexpr <SQL expr> [asc|desc]`         order by an arbitrary expression

Column sorts group blank/whitespace/missing cells last, sort numeric-looking
cells numerically before text, and fold case for ASCII text. Sorting the
synthesized Row# column restores original order. `n`/`f` flags give vim-style
numeric sorts (n = leading integer, f = float); a flag-shaped token yields to a
real column of the same name. Sorts are guaranteed stable via a rowid tiebreak,
which also makes a re-sort a true secondary sort on a derived buffer.

Fix a pre-existing defect in `where` and pivot drill-down (and the new sort
sites): `select ROWID as "Row #", *` binds ROWID to a data column literally
named `rowid`, showing that column's values as row numbers. New shared helper
zsvsheet_sql_rowid_ref() in sheet-sql.c picks the first unshadowed implicit-rowid
spelling (ROWID/_ROWID_/OID); all three commands use it.

Tested via the tmux-driven test-sheet harness with discriminating fixtures for
each clause (numeric grouping, direction, flag/column ambiguity, stability
tiebreak, and the rowid-shadow and triple-shadow cases); clean under
ASan+UBSan and leak checking.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@liquidaty
liquidaty merged commit 098685e into main Aug 9, 2026
15 checks passed
@liquidaty
liquidaty deleted the sheet-sort-cmd branch August 23, 2026 03:51
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.

1 participant

⌂ 换网址