Skip to content

Add psql's \crosstabview - #1642

Open
anandghegde wants to merge 1 commit into
dbcli:mainfrom
anandghegde:feat/crosstabview
Open

anandghegde wants to merge 1 commit into
dbcli:mainfrom
anandghegde:feat/crosstabview

Conversation

@anandghegde

Copy link
Copy Markdown

Description

Adds psql's \crosstabview [colV [colH [colD [sortcolH]]]] (#1378, dbcli/pgspecial#142). It runs the query and shows the result as a grid: the values of colV go down the side, the values of colH go across the top, and colD fills the cells.

It follows psql's crosstabview.c:

  • Columns can be given by number or by name. Unquoted names are case-folded, and "Quoted" names are kept as written.
  • Defaults are first column vertical, second horizontal, third data. The data column must be given when the query returns more than three columns.
  • Header values keep their order of first appearance. With sortcolH, the horizontal header is sorted by that integer column: non-integers rank 0 and ties are in name order, as in psql.
  • NULL headers and NULL values print as null_string. A cell with no data is empty.
  • The error messages are psql's: fewer than three columns, the same column for both headers, column number out of range, unknown or ambiguous name, more than one value for a cell, more than 1600 columns. The warning for extra arguments is also psql's.
  • select ... \crosstabview works like \G: PGExecute.run() handles it, and it also ends the buffer in multi-line mode. A bare \crosstabview, or select ...; \crosstabview, re-runs the last query sent to the server, like psql. A statement that returns no rows is shown as usual.

I checked it against psql 17's \crosstabview, using the examples from the psql docs and the error cases, on a Docker Postgres. The grid shape, the header order (including sortcolH) and the error text match. Where pgcli prints differently, it does so by design: SELECT n instead of (n rows), True instead of t, <null> for NULL.

Checklist

  • I've added this contribution to the changelog.rst.
  • I've added my name to the AUTHORS file (or it's already there).
  • I installed pre-commit hooks (pip install pre-commit && pre-commit install).
  • I verified that my changes work as expected (this may include manually testing them in your local environment, or in other available environments).
  • Please squash merge this pull request (uncheck if you'd like us to merge as multiple commits)

\crosstabview [colV [colH [colD [sortcolH]]]] runs the query and shows
the result as a crosstab grid: the distinct values of colV down the
side, of colH across the top, colD in the cells. It follows psql's
crosstabview.c: columns by name (case-folded unless quoted) or number,
the first three columns by default, sortcolH to order the horizontal
header by an integer column (ties in name order), and psql's error
messages for too few columns, ambiguous or unknown columns, and more
than one value per cell.

"<query> \crosstabview" is handled in PGExecute.run(), like \G, and
also ends the buffer in multi-line mode. A bare \crosstabview re-runs
the last query sent to the server, as in psql.

Refs dbcli#1378, dbcli/pgspecial#142
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