Skip to content

Conversation

@peasee
Copy link
Contributor

@peasee peasee commented Oct 13, 2024

Which issue does this PR close?

Closes #12915.

Rationale for this change

  • To fix a bug with casting integers in MySQL

What changes are included in this PR?

  • Adds an option to the Dialect to specify the cast type for Int32 integers, replicating the existing behavior for Int64 integers.

Are these changes tested?

Yes.

Are there any user-facing changes?

No.

@github-actions github-actions bot added the sql SQL Planner label Oct 13, 2024
}

fn int32_cast_dtype(&self) -> ast::DataType {
ast::DataType::Custom(ObjectName(vec![Ident::new("SIGNED")]), vec![])
Copy link
Member

Choose a reason for hiding this comment

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

MySQL integer is signed by default

Also where do we specify it's integer and not eg bigint or tinyint?
https://dev.mysql.com/doc/refman/8.4/en/integer-types.html

Copy link
Member

Choose a reason for hiding this comment

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

perhaps SIGNED INTEGER would work (it does in my MySQL)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

MySQL integer is signed by default

I'm not sure what you mean by this. There's already a rule to rewrite BIGINT as SIGNED, but not for INTEGER. On the current version, it generates CAST(.. INTEGER) which isn't supported.

Also where do we specify it's integer and not eg bigint or tinyint?

you cannot specify the size of an integer in cast with MySQL - it always casts to BIGINT. SIGNED INTEGER does work, but it's syntactic sugar and is identical to SIGNED.

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe @phillipleblanc or @sgrebnov can offer some help here

Copy link
Member

@sgrebnov sgrebnov Oct 17, 2024

Choose a reason for hiding this comment

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

The change LGTM

  1. MySQL cast does not support Integer or BigInt which are currently used by default to unparse Int32 and Int64 unparsing. List of supported types for MySQL cast:
    https://dev.mysql.com/doc/refman/8.4/en/cast-functions.html#function_cast
  2. So this PR fallbacks to using SIGNED [INTEGER] similar to int64_cast_dtype which will produce a signed BIGINT. I prefer just SIGNED to be consistent with int64_cast_dtype (or update both)

Possible alternative is to see if we can get rid of casting at all when it is unnecessary as Int32 cast intention and BigInt are different

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thank. you @peasee , @findepi and @sgrebnov

@alamb alamb merged commit 3d1d28d into apache:main Oct 17, 2024
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sql SQL Planner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CAST(.. AS INTEGER) is not supported in MySQL

4 participants