Skip to content

Take the column types from the cursor, not from the values - #1655

Open
DiegoDAF wants to merge 2 commits into
dbcli:mainfrom
DiegoDAF:upstream/number-formats-from-the-cursor
Open

DiegoDAF wants to merge 2 commits into
dbcli:mainfrom
DiegoDAF:upstream/number-formats-from-the-cursor

Conversation

@DiegoDAF

Copy link
Copy Markdown
Contributor

The float format of [data_formats] has never applied, and float and numeric columns were never aligned on the decimal point. Two things combine:

  • format_output() computes column_types from the cursor description only after cur = list(cur) has dropped it, so they are always None and cli_helpers infers the types from the values.
  • cli_helpers' TYPES ranks float and Decimal both as 3, and _get_column_type() inverts that dict, so every float or numeric column comes back as Decimal, which neither format_numbers nor align_decimals treats as a number (both test column_type is float). This goes back to cli_helpers 1.0.0.

The types now come from the cursor description before the rows are materialised (float4/float8/numeric as float, int2/int4/int8 as int) and go to both the table and the auto-vertical fallback. With float = ",.2f" in the config 123456.78 prints as 123,456.78; with no float format, the numbers of a float or numeric column line up on the decimal point. The first commit also fixes the type list itself, which appended two entries for a numeric column.

Four tests against a live server, all failing on main. The whole suite passes except test_format_array_output_expanded, red on main before this change (cli-helpers 2.15.2, see #1653).

A numeric column appended float and then, through the fall-through else,
str as well, so every column after it carried the wrong type and the
integer format skipped the ints that followed a numeric. Only the
auto-vertical fallback with float_format or integer_format set was affected.
cli_helpers infers a column's type from its values when none is given,
and its ranking files float and Decimal alike under Decimal, which neither
format_numbers nor align_decimals treats as a number. So the float format
of [data_formats] never applied, and float and numeric columns were never
aligned on the decimal point; the types pgcli did compute came after
list(cur) had dropped the description, so they were always None.

The types now come from the cursor description before the rows are
materialised, and go to both the table and the auto-vertical fallback.

This branch has not been deployed

No deployments
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