scylladb / gocql

Package gocql implements a fast and robust ScyllaDB client for the Go programming language.
https://docs.scylladb.com/stable/using-scylla/drivers/cql-drivers/scylla-go-driver.html
BSD 3-Clause "New" or "Revised" License
190 stars 59 forks source link

Marshal `int like` `cql types` into `uint like` `go types` #307

Open illia-li opened 1 month ago

illia-li commented 1 month ago

All int like cql types have negative range and positive range, but uint like go types have only positive range. For example, if cast -1 tinyint []byte("\xff") into uint like go types we got 255.

In the old marhsal/unmarshal functions (before the starts redesign), this problem was solved in different ways. So, my suggest to leave only one way, because if have different ways its not clear. Moreover, this can lead to unexpected consequences, not at the time of testing, but at the moment when the value ofuintfalls into the negative range ofcql type`.

Way 1 - deny any processing with negative range of int like cql types. This way means:

  1. On marshal: 1.1. For uint like go types with less bitsize then the cql type - free encoding into positive range of the cql type 1.2. For uint like go types with same or bigger bitsize then the cql type - encoding only if value of the uint go type in the positive range of the cql type.
  2. On unmarshal: 2.1. For all uint like go types - encoding only if the data from DB in the positive range and in the range of the uint go type.

Way 2 - allow processing with negative range of int like cql types. This way means:

  1. On marshal: 1.1. For uint like go types with less bitsize then the cql type - free encoding into positive range of the cql type 1.1. For uint like go types with same bitsize then the cql type - free encoding into all range of the cql type 1.2. For uint like go types with bigger bitsize then the cql type - encoding only if value of the uint go type in the range of the cql type.
  2. On unmarshal: 2.1. For all uint like go types - encoding only if the data from DB in the range of the uint go type.

In relation to DB users can be sorted into 2 groups:

  1. Users who use DB only with GO.
  2. Users who use DB not only with GO.

For users from group #1 all two ways will be acceptable, but they will not be able to processing with full range of the int like cql types from the box. At the same time, they will always have the opportunity to make their own marshal/unmarshall functions.

For users from group #2 only way 1 will be acceptable, because way 2 can lead to problems due to the different DB and GO representations of the same values. And it may turn out that they will find out only after a while.

So, my suggest to leave only one way - deny the processing with a negative range of the int like cql types for all uint like go types.