Skip to content

Mysql unparser cast targets - #26888

Merged
nuno-faria merged 2 commits into
apache:mainfrom
hozan23:mysql-unparser-cast-targets
Nov 4, 2026
Merged

nuno-faria merged 2 commits into
apache:mainfrom
hozan23:mysql-unparser-cast-targets

Conversation

@hozan23

@hozan23 hozan23 commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

MySQL's CAST does not accept SMALLINT or the ... UNSIGNED integer types as target types, but arrow_dtype_to_ast_dtype hard-codes them for Int16 and UInt8..UInt64. As a result, MySqlDialect cannot override them and the generated SQL fails with ERROR 1064.

See https://dev.mysql.com/doc/refman/8.0/en/cast-functions.html#function_cast

Checked against MySQL 8.0.46 and 8.4.13 with SELECT CAST('1' AS <type>): every type in the issue table fails with ERROR 1064, while SIGNED and UNSIGNED succeed.

What changes are included in this PR?

  • New Dialect hooks int16_cast_dtype and uint8/uint16/uint32/uint64_cast_dtype. Their defaults return the previous types.
  • MySqlDialect returns SIGNED for Int16 and UNSIGNED for the unsigned types.

This builds on #26883.

What is the testing strategy for this PR?

New unit tests in datafusion/sql/src/unparser/expr.rs:

  • mysql_int16_and_unsigned_casts: the new MySQL output
  • default_dialect_int16_and_unsigned_casts: the default output is unchanged
  • custom_dialect_int16_and_unsigned_casts: the builder setters

Are there any user-facing changes?

With MySqlDialect, Int16 casts now unparse as SIGNED and unsigned integer casts as UNSIGNED.

@github-actions github-actions Bot added the sql SQL Planner label Oct 29, 2026

@nuno-faria nuno-faria 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.

Thanks @hozan23, I leave some suggestions below. Postgres also doesn't support unsigned types, so in the future we can use these new overrides to fix it.

Comment thread datafusion/sql/src/unparser/expr.rs Resolved
Comment thread datafusion/sql/src/unparser/expr.rs Resolved
@hozan23
hozan23 force-pushed the mysql-unparser-cast-targets branch from 9b3559c to 5ca2650 Compare October 31, 2026 19:24
@hozan23

hozan23 commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @nuno-faria for the review, I have combined the tests as you suggested.

I agree we can use these new overrides for Postgres as well.

…MySQL

MySQL's CAST does not accept SMALLINT, TINYINT UNSIGNED,
SMALLINT UNSIGNED, INTEGER UNSIGNED or BIGINT UNSIGNED as target types,
but arrow_dtype_to_ast_dtype hard-coded them for Int16 and
UInt8..UInt64, so MySqlDialect could not override them and the
generated SQL failed with ERROR 1064.
@hozan23
hozan23 force-pushed the mysql-unparser-cast-targets branch from 5ca2650 to 4f3d304 Compare November 1, 2026 13:04
@hozan23
hozan23 requested a review from nuno-faria November 4, 2026 09:00
@nuno-faria
nuno-faria added this pull request to the merge queue Nov 4, 2026
Merged via the queue into apache:main with commit 39ad685 Nov 4, 2026
44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sql SQL Planner v57.0.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MySQL unparser emits SMALLINT and unsigned cast types, which MySQL rejects

2 participants