Skip to content

Pass explicit schemas for Polars dataframes based on MySQL field types - #2486

Merged
rolandwalker merged 1 commit into
mainfrom
RW/explicit-polars-schema-from-mysql-types
Dec 2, 2026
Merged

rolandwalker merged 1 commit into
mainfrom
RW/explicit-polars-schema-from-mysql-types

Conversation

@rolandwalker

@rolandwalker rolandwalker commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Description

Pass explicit schemas for Polars dataframes based on MySQL field types.

The general idea is that since MySQL already knows the type of a returned field, we don't need to infer the type when creating dataframes.

This should in principle be faster than always inferring types by scanning the entire result, though handling edge cases such as invalid-date representation also takes compute time, and so far the performance seems to be a wash.

This also solves the bug of throwing a cryptic error when transforming results which included date/datetime values which are unrepresentable in Python, at the cost of stringifying the entire result column, making it intractable to most interesting dataframe operations.

Example of the Polars error:

could not append value: 2026-12-02 07:45:40 of type: datetime[μs] to the builder; make sure that all rows have the same schema or consider increasing `infer_schema_length`

it might also be that a value overflows the data-type's capacity

Unaddressed here: Decimal results still need to be inferred.

@leninworld , do you have any interest in testing this for performance as you did on #2483 ?

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 .

based on MySQL field types.

The general idea is that since MySQL already knows the type of a
returned field, we don't need to _infer_ the type when creating
dataframes.

This should in principle be faster than always inferring types by
scanning the entire result, though handling edge cases such as invalid-
date representation also takes compute time, and so far the performance
seems to be a wash.

This also solves the bug of throwing a cryptic error when transforming
results which included date/datetime values which are unrepresentable in
Python, at the cost of stringifying the entire result column, making it
intractable to most interesting dataframe operations.

Example of the Polars error:

    could not append value: 2026-12-02 07:45:40 of type: datetime[μs] to the builder; make sure that all rows have the same schema or consider increasing infer_schema_length

    it might also be that a value overflows the data-type's capacity

Unaddressed here: Decimal results still need to be inferred.
@rolandwalker
rolandwalker force-pushed the RW/explicit-polars-schema-from-mysql-types branch from dd358fd to a80c626 Compare December 2, 2026 13:53
@rolandwalker

Copy link
Copy Markdown
Contributor Author

Even though this doesn't seem to unlock a performance win, I'm going to merge it for the bugfix.

@rolandwalker
rolandwalker merged commit 3d267e8 into main Dec 2, 2026
13 checks passed
@rolandwalker
rolandwalker deleted the RW/explicit-polars-schema-from-mysql-types branch December 2, 2026 14:01
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