Skip to content

dolthub/dolt#7128 - String to number conversion #3136

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

elianddb
Copy link
Contributor

@elianddb elianddb commented Aug 1, 2025

Fixes dolthub/dolt#7128
Previously, strings like "123.456abc" would throw errors instead of converting to 123.456 as in MySQL. This change adds MySQL-compatible truncation logic that extracts valid numeric prefixes while handling edge cases like whitespace, scientific notation, and pure non-numeric strings.

@nicktobey
Copy link
Contributor

nicktobey commented Aug 3, 2025

A thought: compatibility with MySQL is important, but I also think that throwing an error is probably better behavior (plus having more MySQL-specific behavior in the engine will make it harder to support other dialects like Postgres.)

So I would propose instead to make this behavior configurable, keep the current behavior as the default, and have GMS use the MySQL-specific behavior when a configuration flag is set.

@elianddb elianddb force-pushed the elianddb/7128-fix-string-double-truncation branch from 60cefe8 to a8089e1 Compare August 4, 2025 16:36
@elianddb elianddb force-pushed the elianddb/7128-fix-string-double-truncation branch from a8089e1 to 9a292fc Compare August 4, 2025 20:15
@elianddb elianddb requested a review from nicktobey August 4, 2025 21:05
Name: "mysql_string_to_number system variable",
Assertions: []ScriptTestAssertion{
{
Query: "SELECT @@mysql_string_to_number",
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these copied from another test? Do these different variables interact with each other?

I'm worried this is copy-pasting code and tests. If we have a lot of variables that all have global/session/local variables like this with specific rules, we should probably have a special test for those that doesn't duplicate code.

},
{
Query: "SELECT CAST('123abc' AS SIGNED)",
Expected: []sql.Row{{int64(0)}},
Copy link
Contributor

Choose a reason for hiding this comment

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

The original behavior throws an error, right? This test seems to imply that it returns 0.


// Handle string-to-decimal conversion for mysql_string_to_number mode
if strVal, ok := val.(string); ok && sql.ValidateStringToNumberMode(ctx) {
if convertedVal, _, err := types.Float64.Convert(ctx, strVal); err == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

So if the flag is set, we convert to a decimal by converting to a float first?

It's not clear from looking at this function why this produces the correct behavior.

@nicktobey
Copy link
Contributor

Let's pick a more descriptive name for the variable.

Some possibilities:

  • @@truncate_char_cast_to_number (defaulting to false)
  • @@strict_char_cast_to_number (defaulting to true)

The others might have their own ideas, so maybe we should ask them.

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.

Truncate strings when converting to double
2 participants