ydb-platform / ydb-go-sdk

Pure Go native and database/sql driver for YDB
https://ydb.tech
Apache License 2.0
132 stars 69 forks source link

feat: Add an index of the column into error if `row.Scan` can't scan a column #1272

Closed pelageech closed 3 weeks ago

pelageech commented 3 weeks ago

Feature Request

Describe the Feature Request If I scan a row and get an error connected with column scan, I want to see which column is not scanned properly. Specifically in our project we are moving from v2 to v3. However, we are faced with the problem that the types returned by YDB cannot be mapped to the types that I have defined in the code. I often have a lot of the same types in my code. It is extremely difficult to understand from the query which column returned which type. Because of this, you have to randomly change the types to the types that the database returns.

Example query:

-- ofc in real queries I don't know neither values nor types
SELECT CAST(42 AS Uint32), CAST(42 AS Int32), CAST(42 AS Int64);

Example Go code:

// open db
var a, b, c uint32

row, _ := db.Query().ReadRow(...)
err = row.Scan(&a, &b, &c)
if err != nil {
    log.Fatal(err)
}
fmt.Println(a, b, c)
// ...

I get an error

cannot cast '42' (type 'Int32') to '*uint32' destination at 
'github.com/ydb-platform/ydb-go-sdk/v3/internal/value.int32Value.castTo(value.go:950)' at 
'github.com/ydb-platform/ydb-go-sdk/v3/internal/query/scanner.IndexedScanner.Scan(indexed.go:32)'

Describe Preferred Solution I'd like to see something like that

scan error on column index 1: cannot cast '42' (type 'Int32') to '*uint32' destination at 
'github.com/ydb-platform/ydb-go-sdk/v3/internal/value.int32Value.castTo(value.go:950)' at 
'github.com/ydb-platform/ydb-go-sdk/v3/internal/query/scanner.IndexedScanner.Scan(indexed.go:32)'

Additional Context The issue is inspired by sqlite3 driver, there is something similar there.

If the feature request is approved, would you be willing to submit a PR? Yes

asmyasnikov commented 3 weeks ago

Do you want to check on SDK side the negative value and fail or success if positive value?

pelageech commented 3 weeks ago

Do you want to check on SDK side the negative value and fail or success if positive value?

Sorry, haven't got your question. Can you explain a little bit? :)

pelageech commented 3 weeks ago

So funny to find a bug unexpectedly for myself😁

asmyasnikov commented 3 weeks ago

Fixed in release v3.74.2

asmyasnikov commented 3 weeks ago

From internal meeting: we dont want to allow scan from signed integers to unsigned integers