Skip to content
\n
mysql> select name from users;\n+--------------------------------------------------------+\n| name                                                   |\n+--------------------------------------------------------+\n| {\"hoge1\": {\"fuga1\": 100000}}                           |\n| {\"hoge2\": {\"fuga2\": 1000000}}                          |\n+--------------------------------------------------------+
\n

In Go, I'm executing the following SQL queries and scanning the results into int64 variables:

\n
import (\n  ...\n  \"github.com/volatiletech/sqlboiler/queries\"\n  \"database/sql\"\n)\n...\nvar ( \n  v1 v2 int64\n  db *sql.DB\n)\n...\nrow1 := queries.Raw(`\n  select sum(JSON_EXTRACT(name, '$.hoge1.fuga1')) as piyo from users;\n`).QueryRow(db)\n\nerr := row1.Scan(&v1)\nfmt.Printf(\"%v, err\\n\", v1, err) // output: 100000, nil\n\nrow2 := queries.Raw(`\n  select sum(JSON_EXTRACT(name, '$.hoge2.fuga2')) as piyo from users;\n`).QueryRow(db)\n\nerr = row2.Scan(&v2)\nfmt.Printf(\"%v, err\\n\", v2, err) // output: 0, Scan error on column index 0, name \"piyo\": converting driver.Value type float64 (\"1e+06\") to a int64: invalid syntax
\n

The first query works as expected, but the second query returns the following error:

\n
Scan error on column index 0, name \"piyo\": converting driver.Value type float64 (\"1e+06\") to a int64: invalid syntax
\n

I thought that 1,000,000 is expected to be of type int64, but for some reason, it was retrieved as float64.

\n

So what criteria determine whether a value is sent as int64 or double.
\nSpecifically, for values like 100,000 and 1,000,000 which data type (int64 or double) would be used?

","upvoteCount":1,"answerCount":2,"acceptedAnswer":{"@type":"Answer","text":"

I managed to run your code and I can reproduce it.
\nhttps://gist.github.com/methane/8f7bf5c84705246ab7c8da3eb5889820

\n

I confirm that MySQL returns DOUBLE (float64) for both queries.

\n
$ mysql -h 127.0.0.1 -u root test --column-type-info\n\nmysql> select sum(JSON_EXTRACT(name, '$.hoge1.fuga1')) as piyo from users;\nField   1:  `piyo`\nCatalog:    `def`\nDatabase:   ``\nTable:      ``\nOrg_table:  ``\nType:       DOUBLE\nCollation:  binary (63)\nLength:     23\nMax_length: 6\nDecimals:   31\nFlags:      BINARY NUM\n\n\n+--------+\n| piyo   |\n+--------+\n| 100000 |\n+--------+\n1 row in set (0.00 sec)\n\nmysql> select sum(JSON_EXTRACT(name, '$.hoge2.fuga2')) as piyo from users;\nField   1:  `piyo`\nCatalog:    `def`\nDatabase:   ``\nTable:      ``\nOrg_table:  ``\nType:       DOUBLE\nCollation:  binary (63)\nLength:     23\nMax_length: 7\nDecimals:   31\nFlags:      BINARY NUM\n\n\n+---------+\n| piyo    |\n+---------+\n| 1000000 |\n+---------+\n1 row in set (0.01 sec)\n
\n

The difference is came from here.

\n

https://github.com/golang/go/blob/96d8ff00c2d6a88384863a656fb5e53716b614d3/src/database/sql/convert.go#L440-L451

\n

When dest type (scanning into) is int and source type (the driver returns) is not int, database/sql stringify source value and use ParseInt() to get integer.

\n

See and run this sample code. https://go.dev/play/p/-raUl94MbZW

\n

fmt.Println(100000.0) becomes 100000 so ParseInt succeeds.
\nfmt.Println(1000000.0) becomes 1e+06 so ParseInt fails.

\n

That's why two queries have different results.

","upvoteCount":1,"url":"https://github.com/go-sql-driver/mysql/discussions/1627#discussioncomment-10436091"}}}

Clarification on Data Type Conversion in Text Protocol for v1.8.0 #1627

Closed Answered by methane
mimaken3 asked this question in Q&A
Discussion options

You must be logged in to vote

I managed to run your code and I can reproduce it.
https://gist.github.com/methane/8f7bf5c84705246ab7c8da3eb5889820

I confirm that MySQL returns DOUBLE (float64) for both queries.

$ mysql -h 127.0.0.1 -u root test --column-type-info

mysql> select sum(JSON_EXTRACT(name, '$.hoge1.fuga1')) as piyo from users;
Field   1:  `piyo`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       DOUBLE
Collation:  binary (63)
Length:     23
Max_length: 6
Decimals:   31
Flags:      BINARY NUM


+--------+
| piyo   |
+--------+
| 100000 |
+--------+
1 row in set (0.00 sec)

mysql> select sum(JSON_EXTRACT(name, '$.hoge2.fuga2')) as piyo from users;
Field   1:  `piyo`
Catalog:    `def`
Da…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@methane
Comment options

Comment options

You must be logged in to vote
1 reply
@mimaken3
Comment options

Answer selected by mimaken3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants