-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Scan results are of wrong type when Query has no args #861
Comments
It's a not bug, but designed behavior.
If this driver parse "123" and convert it to int64, Why can't you use |
One more comment. What MySQL returns for |
Inconsistent results are designed behaviour?
This looks contrived: if the database field is of integer type, why would you store it in a string? And if you want to do so, shouldn't you accept that, yes, it must be converted, and yes, this has a performance cost?
Because I'm writing reusable code that tries to be flexible and doesn't have to know the type of each field in advance.
For a statically typed language, I'd argue that type consistency is more important than performance in some edge case. |
Yes. "Return most efficient type for returned data, and
This is just an example why we don't parse text result into other types.
You can use ColumnType to know actual column type.
I can't agree. This topic is about consistent type for "dynamic type" (e.g. |
One way to avoid text protocol is use |
Thank you for your detailed responses. I understand the reasoning, but the end result is that as a user of database/sql I need to be aware of implementation details of the driver, which is less than ideal. Even when using ColumnType, I need to be aware of what datatypes the underlying database can return, and how to map them to go types. In a perfect world, I'd say this is the responsibility of the driver (like how eg. Doctrine DBAL does it), but I understand database/sql is not really intended to be a dbal. Anyway, thanks. |
Hi guys. Context: running db query that has, among db fields, one calculated field |
* fix problems with mysql. * use a prepared statement explicitly to make mysql happy when there are no query parameters * only attempt to use the prepared statement if there are no query parameters, as that's the bug in the mysql driver * simplify code The problem with MySQL's Go driver (https://github.com/go-sql-driver/mysql) is that it won't translate numbers in the response when the text protocol is used (which happens when a query is run without any parameters). Multiple issues have been filed against this for years, but the developers have responded with a wontfix due to the dubious claim that it's too much of a performance hit. See go-sql-driver/mysql#861 for an example. This update checks to see if a query has no query args. If it does, it checks to see if the Querier/ContextQuerier also implements Prepare/PrepareContext and if so, it creates a statement, forcing MySQL to use the binary protocol. (Postgres will also be using a prepared statement unnecessarily, but it probably has minimal performance difference).
Issue description
I tried to use Rows.Scan to read fields of an sql result row into variables of type
interface{}
I expected integer sql types to be read as go type
int64
, or at least, the go type to be the same whatever the query looks like.However:
SELECT 123 WHERE ? = 1
returnsint64: 123
SELECT 123 WHERE 1 = 1
returns[]uint8: []byte{0x31, 0x32, 0x33}
Example code
See https://github.com/ComaVN/gosqltypespoc for a PoC, and golang/go#27828 (comment) for why it's probably a mysql driver issue.
Error log
n/a
Configuration
No idea how to determine the driver version, see the PoC: I just download the latest version from github, I think.
go version go1.11 linux/amd64
mysql:8.0.12, mariadb:10.1
Debian GNU/Linux 9 (stretch)
The text was updated successfully, but these errors were encountered: