apache / apache/cassandra-gocql-driver

CASSGO-36 Could MapScan() get nil instead of zero value for null value?

Open
#1,699 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
2.7k
Forks
658
PR merge metrics
No merged PRs in 30d

Description

Hi,
I am new to using Gocql, and I'm currently trying to write a reusable function for selecting data. I want the function to return the query result dynamically. However, when using MapScan, null values are being converted to zero, which is causing confusion for me in distinguishing between zero and null. I have reviewed previous issues and documentation but haven't found a similar solution. Could you please provide some suggestions?

Please answer these questions before submitting your issue. Thanks!

### What version of Cassandra are you using?
4.1.0

### What version of Gocql are you using?
v1.3.2

### What version of Go are you using?
v1.20

### What did you do?
```

func (c *CassandraClient) Select(keyspace string, table string, columns []string, where []Filter) ([]map[string]interface{}, error) {
query := fmt.Sprintf("SELECT %s FROM %s.%s", strings.Join(columns, ","), keyspace, table)
var whereClauses []string
var args []interface{}
for _, v := range where {
whereClauses = append(whereClauses, fmt.Sprintf("%s %s ?", v.Key, v.Operator))
args = append(args, v.Value)
}
if len(whereClauses) > 0 {
query += " WHERE " + strings.Join(whereClauses, " AND ")
}
query += " ALLOW FILTERING"
logrus.Infof("CQL: %s , Args: %s", query, args)
iter := c.session.Query(query, args...).Iter()
results := make([]map[string]interface{}, 0)

for {
m := make(map[string]interface{}, len(columns))

if !iter.MapScan(m) {
break
}
// Handle NaN values
for key, value := range m {
if f, ok := value.(float64); ok && math.IsNaN(f) {
m[key] = "NaN"
}
}
results = append(results, m)
}

if err := iter.Close(); err != nil {
return nil, err
}
return results, nil
}
```

### What did you expect to see?
The null value in the m variable is distinguished from the zero value.

### What did you see instead?
null is converted to zero values.
---

If you are having connectivity related issues please share the following additional information

### Describe your Cassandra cluster
please provide the following information

- output of `nodetool status`
- output of `SELECT peer, rpc_address FROM system.peers`
- rebuild your application with the `gocql_debug` tag and post the output

Contributor guide

Open the contributing guide

Research direction

Start at the MapScan entry point and reproduce the reported behavior with the Cassandra 4.1.0, GoCQL v1.3.2, and Go 1.20 versions listed. Compare how null and zero values are represented in the returned map; done means callers can distinguish the two without breaking existing MapScan behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cassandra, go
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.