Skip to content

fix(format): fix table overflow when column count exceeds available width#576

Merged
apstndb merged 1 commit intomainfrom
issue-575-adjustToSum-overflow
Mar 21, 2026
Merged

fix(format): fix table overflow when column count exceeds available width#576
apstndb merged 1 commit intomainfrom
issue-575-adjustToSum-overflow

Conversation

@apstndb
Copy link
Owner

@apstndb apstndb commented Mar 21, 2026

Summary

Fix table output overflowing screen width when column count is large (e.g., INFORMATION_SCHEMA.COLUMNS with 20 columns). The root cause was adjustToSum being unable to reduce column widths below the minimum unique header width threshold, leaving all columns at that floor even when their sum far exceeds the available width.

Key Changes

  • width.go: Add equal-distribution fallback to adjustToSum when threshold clipping is insufficient. Each column gets at least width 1 to avoid zero-width columns that bypass tabwrap.Wrap. When numCols × 1 > limit, the 1-character overflow per column is unavoidable but minimal.
  • width_strategy_greedy.go: Guard remainder distribution with remainder > 0 check. Previously, when applyColumnFloors caused total width to exceed availableWidth, the negative remainder would subtract width from a column, creating a zero-width column that bypasses wrapping entirely.
  • width_test.go: Add adjustToSum test case for many-column overflow. Add INFORMATION_SCHEMA.COLUMNS-like test case to TestCalculateWidth with total-width assertion that accounts for unavoidable overflow.

Behavior at extreme widths

When numCols × minColumnWidth + overhead > screenWidth (e.g., 20 columns at screenWidth=80), the table will overflow by at most numCols × minColumnWidth + overhead - screenWidth characters (1 char in the COLUMNS case). This is the minimum possible overflow — every column gets exactly 1 character of content width.

Test Plan

  • make check passes
  • TestAdjustToSum/many_columns_overflow_falls_back_to_equal_distribution verifies fallback
  • TestCalculateWidth/many_columns_like_INFORMATION_SCHEMA.COLUMNS verifies end-to-end at screenWidth=120
  • Manual verification with embedded emulator

Fixes #575

…idth

adjustToSum clips column widths using existing value thresholds, but when
many columns have large headers and the screen is narrow, the minimum
threshold times column count still exceeds the available width. Add a
fallback to equal distribution (floor of 1 per column) when threshold
clipping is insufficient.

Also fix GreedyFrequencyStrategy remainder distribution to skip when
remaining space is zero or negative (avoids subtracting width from a column
when the initial allocation already exceeds available width).

Fixes #575

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@apstndb apstndb added the bug Something isn't working label Mar 21, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves an issue where tables with a large number of columns, such as INFORMATION_SCHEMA.COLUMNS, would overflow the screen width due to the column width adjustment logic failing to adequately reduce widths. The changes introduce a more robust fallback mechanism for distributing column widths, ensuring that tables remain within the available screen space while maintaining readability, even in extreme cases where many columns are present.

Highlights

  • Column Width Adjustment: Implemented an equal-distribution fallback in adjustToSum to prevent table overflow when threshold clipping is insufficient, ensuring each column maintains a minimum width of 1.
  • Greedy Width Strategy Refinement: Added a remainder > 0 guard to the remainder distribution logic in width_strategy_greedy.go to prevent the creation of zero-width columns.
  • New Test Cases: Introduced new test cases for adjustToSum to cover many-column overflow and an INFORMATION_SCHEMA.COLUMNS-like scenario for TestCalculateWidth, including an assertion for total table width.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request addresses a table formatting bug where a large number of columns could cause the output to overflow the screen width. The fix involves modifying adjustToSum to fall back to an equal distribution of column widths when the normal clipping mechanism fails, guaranteeing a minimum width of 1 for each column. A related change in width_strategy_greedy.go prevents negative remainders from being applied to column widths. The changes are accompanied by new test cases that cover these overflow scenarios. My review did not identify any issues with the proposed implementation.

@github-actions
Copy link

Code Metrics Report

📊 View detailed coverage report (available for 7 days)

main (88680c9) #576 (fdae9e8) +/-
Coverage 70.8% 70.8% +0.0%
Code to Test Ratio 1:1.3 1:1.3 -0.1
Test Execution Time 1m11s 1m13s +2s
Details
  |                     | main (88680c9) | #576 (fdae9e8) |  +/-  |
  |---------------------|----------------|----------------|-------|
+ | Coverage            |          70.8% |          70.8% | +0.0% |
  |   Files             |             75 |             75 |     0 |
  |   Lines             |           7007 |           7025 |   +18 |
+ |   Covered           |           4967 |           4980 |   +13 |
- | Code to Test Ratio  |          1:1.3 |          1:1.3 |  -0.1 |
  |   Code              |          16274 |          16299 |   +25 |
+ |   Test              |          21183 |          21212 |   +29 |
- | Test Execution Time |          1m11s |          1m13s |   +2s |

Code coverage of files in pull request scope (90.1% → 89.2%)

Files Coverage +/- Status
internal/mycli/execute_sql.go 81.2% -1.4% affected
internal/mycli/format/width.go 96.4% -0.5% modified
internal/mycli/format/width_strategy_greedy.go 97.9% +0.0% modified
internal/mycli/metrics/execution_metrics.go 86.6% -4.5% affected

Reported by octocov

@apstndb apstndb merged commit c40c57d into main Mar 21, 2026
2 checks passed
@apstndb apstndb deleted the issue-575-adjustToSum-overflow branch March 21, 2026 00:24
@apstndb apstndb mentioned this pull request Mar 22, 2026
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Table output overflows screen width when column count is large

1 participant