Skip to content

Compose transform schemas with Polars DataTypes, not native Python types. - #2493

Merged
rolandwalker merged 1 commit into
mainfrom
RW/transform-shemas-as-polars-types
Dec 3, 2026
Merged

rolandwalker merged 1 commit into
mainfrom
RW/transform-shemas-as-polars-types

Conversation

@rolandwalker

@rolandwalker rolandwalker commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Description

A closer read of

suggests that even though some wording says

a dict of {name:type} pairs

a Python type is not intended by the word "type", but a Polars DataType.

The correspondences between MySQL field types and Polars schema DataTypes can no longer be mapped out coherently as a dictionary, since

  • there are more and more special cases such as signed/unsigned integers
  • the polars library is conditionally imported, so the logic containing references to its types must be deferred

Therefore, the mapping system is written out as a series of cluttered if blocks, though some meaningful type collections are grouped at the top of the file.

This change incidentally fixes a bug in which NEWDATE was cast to str.

Limitations around GEOMETRY and SET result fields are documented.

TEXT_BINARY_TYPES is recast to STRING_BINARY_FIELD_TYPES to match the name of the Polars types.

A couple of todo comments are added. Interval temporal types do not seem to be covered. Should they be?

It is still not clear that there is any measurable performance gain for writing schemas over letting Polars infer types, but this method definitely solves some edge cases which led to sometimes-confusing errors in the REPL.

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 rolandwalker self-assigned this Dec 3, 2026
@rolandwalker

Copy link
Copy Markdown
Contributor Author

cc @leninworld

not native Python types.

A closer read of

 * https://docs.pola.rs/api/python/stable/reference/dataframe/index.html

suggests that even though some wording says

    a dict of {name:type} pairs

a Python "type" is not intended by the word "type", but a Polars
"DataType".

The correspondences between MySQL field types and Polars schema
DataTypes can no longer be mapped out coherently as a dictionary, since

 * there are more and more special cases such as signed/unsigned
   integers
 * the polars library is conditionally imported, so the logic containing
   references to its types must be deferred

Therefore, the mapping system is written out as a series of cluttered
"if" blocks, though some meaningful type collections are grouped at the
top of the file.

This change incidentally fixes a bug in which NEWDATE was cast to str.

Limitations around GEOMETRY and SET result fields are documented.

TEXT_BINARY_TYPES is recast to STRING_BINARY_FIELD_TYPES to match the
name of the Polars types.

A couple of todo comments are added.  Interval temporal types do not
seem to be covered.  Should they be?

It is still not clear that there is any measurable performance gain for
writing schemas over letting Polars infer types, but this method
definitely solves some edge cases which led to sometimes-confusing
errors in the REPL.
@rolandwalker
rolandwalker force-pushed the RW/transform-shemas-as-polars-types branch from 0ef7c33 to 8e48a88 Compare December 3, 2026 14:16
@rolandwalker
rolandwalker merged commit 6e6f230 into main Dec 3, 2026
13 checks passed
@rolandwalker
rolandwalker deleted the RW/transform-shemas-as-polars-types branch December 3, 2026 16:09
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