gosnowflake icon indicating copy to clipboard operation
gosnowflake copied to clipboard

SNOW-586521: Numeric data to string conversion does not result in the expected number of decimal places.

Open albscui opened this issue 3 years ago • 4 comments

Issue description

When querying numbers with decimal places as strings, the resulting string representation always has 6 decimal places. This seems to affect both NUMBER and FLOAT types.

Example code

create or replace table test_table (
    C_FLOAT FLOAT,
    C_NUMERIC_1 NUMBER(20,4),
    C_NUMERIC_2 NUMBER(20,9)
);
insert into test_table VALUES (123.45 0.1234, 0.123456789);
rows, _ := db.Query("select * from test_table")
var v1, v2, v3 string
for rows.Next() {
    rows.Scan(&v1, &v2, &v3)
    fmt.Println(v1, v2, v3)
}

Results in

123.450000 0.123400 0.123457

But expects

123.45 0.1234 0.123456789

Configuration

Driver version: v1.6.7

Go version: go version go1.17.9 linux/amd64

Server version: 6.14.0

Client OS: Ubuntu 20.04

albscui avatar May 05 '22 21:05 albscui

I'm seeing this too, and I'm not even doing a string conversion. e.g. below

res, err := db.QueryContext(ctx, "select 0.123456789")
for res.Next() {
	var myFloat float64 // same result if this is "string"
	res.Scan(&myFloat)
	log.Println(myFloat)
}

prints 0.123457

clearly there needs to be a way to retrieve more than 6 decimal places of data from a float...

srabraham avatar Sep 09 '22 17:09 srabraham

the problem is even worse for numbers such that 0 < number < 5e-6, since they just get rounded to zero...

res, err := db.QueryContext(ctx, "select 1e-7")
for res.Next() {
	var myFloat float64 // same result if this is "string"
	res.Scan(&myFloat)
	log.Println(myFloat)
}

// prints 0

srabraham avatar Sep 09 '22 18:09 srabraham

any chance the below line is suspicious? For example, fmt.Sprintf("%f", 1e-7) prints 0

https://go.dev/play/p/XkdvbnPBfOm

https://github.com/snowflakedb/gosnowflake/blob/6a152a12d4523d8c40b57426ebfaa8af52b8be02/converter.go#L433

srabraham avatar Sep 09 '22 20:09 srabraham

My colleague John has made a PR to fix this bug. https://github.com/snowflakedb/gosnowflake/pull/655

srabraham avatar Sep 09 '22 22:09 srabraham