-
Notifications
You must be signed in to change notification settings - Fork 3.6k
HHH-14711 unsigned types causing out of range on MySQL/MariaDB #10303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say that this issue is only worth fixing if we can fix it in a clean way.
Please see my comments.
protected boolean isUnsigned(String columnTypeName) { | ||
return columnTypeName.contains( "UNSIGNED" ); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unsigned
types are not, AFAIK, a standard or common feature of SQL databases, so this implementation doesn't belong as the default implementation on Dialect
.
case Types.TINYINT: | ||
if ( isUnsigned( columnTypeName ) ) { | ||
return jdbcTypeRegistry.getDescriptor( Types.SMALLINT ); | ||
} | ||
break; | ||
case Types.SMALLINT: | ||
if ( isUnsigned( columnTypeName ) ) { | ||
return columnTypeName.contains( "TINYINT" ) ? jdbcTypeRegistry.getDescriptor( Types.SMALLINT ) | ||
: jdbcTypeRegistry.getDescriptor( Types.INTEGER ); | ||
} | ||
break; | ||
case Types.INTEGER: | ||
if ( isUnsigned( columnTypeName ) ) { | ||
if ( columnTypeName.contains( "SMALLINT" ) ) { | ||
return jdbcTypeRegistry.getDescriptor( Types.INTEGER ); | ||
} | ||
else if ( !columnTypeName.contains( "MEDIUMINT" ) ) { | ||
return jdbcTypeRegistry.getDescriptor( Types.BIGINT ); | ||
} | ||
} | ||
break; | ||
case Types.BIGINT: | ||
if ( isUnsigned( columnTypeName ) ) { | ||
return columnTypeName.contains( "INTEGER" ) ? jdbcTypeRegistry.getDescriptor( Types.BIGINT ) | ||
: jdbcTypeRegistry.getDescriptor( Types.NUMERIC ); | ||
} | ||
break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Woah. This is ... a bit ugly. Surely there must be some more elegant way to do this?
Why not just directly switch on the columnTypeName
, since you're depending on it anyway?
Actually, @peter1123581321, is this a breaking change to existing native SQL queries which are expecting the narrower types? |
@peter1123581321 see my comment here: https://hibernate.atlassian.net/browse/HHH-14711?focusedCommentId=122112 |
Many thanks Gavin for your comments on this PR.
Summarized, I also would suggest to close this bug. |
I'm not referring to what you're doing here, which is, well, OK. I was referring to creating a database schema where you have just one bit of headroom for your numeric types. That's a bad idea, since it's incredibly rare for someone to have such a good estimate of the largest possible value for a column.
And that's probably a pretty common scenario, since numeric columns are typically specified with a lot of headroom (i.e. more than one bit). |
44e6b45
to
d0ce380
Compare
MySQL fails for tinyint, smallint, integer and bigint, but works correctly for mediumint.
MariaDB fails for bigint, but works correctly for all others.
The MariaDB implementation of
ResultSetMetaData#getColumnType()
differs from the MySQL implementation.MariaDB has a more accurate implementation concerning unsigned data types.
As MariaDbDialect extends MySQLDialect, the bugfix was implemented in
MySQLDialect#resolveSqlTypeDescriptor()
.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license
and can be relicensed under the terms of the LGPL v2.1 license in the future at the maintainers' discretion.
For more information on licensing, please check here.
https://hibernate.atlassian.net/browse/HHH-14711