Skip to content

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

@fhwangyinan

Description

@fhwangyinan

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions