Skip to content

Fix .| transforms for columns that start with many NULLs - #2483

Merged
rolandwalker merged 1 commit into
dbcli:mainfrom
leninworld:fix-polars-sparse-columns
Nov 29, 2026
Merged

rolandwalker merged 1 commit into
dbcli:mainfrom
leninworld:fix-polars-sparse-columns

Conversation

@leninworld

Copy link
Copy Markdown
Contributor

This PR fixes issue #2482.

Description

A .| transform or .> save failed with ComputeError when a column was NULL in the first 100 rows and had a value later, because Polars infers column types from the first 100 rows by default. run_polars_transform() now passes infer_schema_length=None, so all rows are used.

Cost: building the DataFrame scans more rows. For 200,000 rows and 5 columns it went from 0.11 s to 0.13 s on my machine.

Not covered: a column that mixes zero dates (which arrive as strings such as '0000-00-00') with real dates still fails, as it does on main.

The new test uses real Polars with an integer and a string after 100 NULL rows. It fails on main (2 failed) and passes with the change. mycli_test/pytests: 3624 passed, 87 skipped, 7 xfailed. The behave tests, which need a MySQL server, were not run locally.

Checklist

  • I added this contribution to the changelog.md file.
  • I added my name to the mycli/AUTHORS.txt file (or it's already there).
  • To lint and format the code, I ran
    uv run ruff check && uv run ruff format && uv run mypy --install-types .

@rolandwalker

Copy link
Copy Markdown
Contributor

Hi! Thanks!

What are the performance implications?

@rolandwalker rolandwalker self-assigned this Nov 29, 2026
@leninworld

Copy link
Copy Markdown
Contributor Author

What are the performance implications?

The extra cost is in building the DataFrame, it grows with the row count, and the memory part is larger than the PR description suggests (it only gave time).

DataFrame construction only, with rows already fetched (no query or display time). Polars 2.1.0, Python 3.13, Apple M3 Max, median of repeated runs; columns cycle through int, float, str, datetime and nullable Decimal:

rows × columns main this PR
1,000 × 5 0.57 ms 0.62 ms
10,000 × 20 20 ms 22 ms
100,000 × 20 202 ms 234 ms
1,000,000 × 5 535 ms 638 ms
1,000,000 × 20 2.08 s 2.46 s

Peak memory for 1,000,000 × 5: the process holds about 424 MB with the fetched rows; building the DataFrame raises the peak to about 525 MB on main and about 770 MB with this PR.

If that matters, I can change it to try the default inference first and retry with infer_schema_length=None only when that raises. In a quick test, results that work today then cost the same as on main, and the late-NULL case at 1,000,000 × 5 took about the same time as the full scan (376 ms vs 369 ms). The trade-off: whenever the default succeeds you keep main's behavior, including cases where Polars quietly casts a later value (an int column followed by 1.5 becomes 1), which I wouldn't expect from a single MySQL column.

Would you like that version, or keep the simpler full scan?

@rolandwalker rolandwalker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's keep it simple!

@rolandwalker
rolandwalker merged commit dceffdd into dbcli:main Nov 29, 2026
13 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.

.| transform fails when a column is NULL in the first 100 rows

2 participants