Skip to content

test and fix for MysSQL float parsing into float64 when placeholders are used #434

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

Merged
merged 2 commits into from
Apr 11, 2016

Conversation

pib
Copy link
Contributor

@pib pib commented Mar 3, 2016

After writing up #433 I realized it would be an easy fix, so I here it is.

@@ -1149,7 +1149,7 @@ func (rows *binaryRows) readRow(dest []driver.Value) error {
continue

case fieldTypeFloat:
dest[i] = float64(math.Float32frombits(binary.LittleEndian.Uint32(data[pos : pos+4])))
dest[i] = float32(math.Float32frombits(binary.LittleEndian.Uint32(data[pos : pos+4])))
Copy link
Member

Choose a reason for hiding this comment

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

Unfortunately the driver spec does not allow to return a float32 as a driver.Value: https://golang.org/pkg/database/sql/driver/#Value

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Huh, it's strange that it works anyway. I suppose what the spec says and the details of the implementation vary a bit.

Looking through some of the standard library sql code, I see that they seem to work around this issue by converting floats to strings and back using strconv.ParseFloat with the bit size of the field it is going into: https://golang.org/src/database/sql/convert.go#L256

Actually, that function is called by sql.Rows.Scan, which means that their spec for Value is actually inaccurate: https://golang.org/src/database/sql/sql.go#L1849

It seems that the doc for driver.Rows https://golang.org/pkg/database/sql/driver/#Rows should be updated to specify that they do handle more types, including float32.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I suppose I will submit a ticket to the golang project asking for clarification.

Copy link
Member

Choose a reason for hiding this comment

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

If it works and we have proper tests for it, I would be fine with ignoring the specification (which is rather flawed anyway). We're testing against all Go versions and tip, so we would notice if it breaks in a future version.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, this change fixes the issue I was running into and the tests I added cover both cases and passed on Travis.

I added an issue to the Go repo so they will hopefully fix the documentation to include the full list of types that are actually allowed in returns from drivers.

@pib
Copy link
Contributor Author

pib commented Apr 10, 2016

They've updated the docs to remove the restriction on types returned, so this fix is even legit by their standards now.

@julienschmidt
Copy link
Member

Perfect, then this PR can be merged.
LGTM

Thanks!

@julienschmidt julienschmidt merged commit 7ebe0a5 into go-sql-driver:master Apr 11, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants