Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String column: distinguish between empty string and nil #91

Closed
pesterhazy opened this issue Dec 15, 2014 · 3 comments · Fixed by #92
Closed

String column: distinguish between empty string and nil #91

pesterhazy opened this issue Dec 15, 2014 · 3 comments · Fixed by #92
Assignees
Labels
Milestone

Comments

@pesterhazy
Copy link

When doing a SELECT on a column family with a VARCHAR column, empty strings are returned as nil. AFAICT this is not the expected behavior with C*, and certainly surprising.

The short test program below prints:

[{:id 1, :country nil, :name "a"} {:id 2, :country nil, :name "b"}]

but the following output would be expected:

[{:id 1, :country nil, :name "a"} {:id 2, :country "", :name "b"}]

I'm using the current 2.0 release candidate of cassaforte (dependencies below). I tried to reproduce the problematic behavior using just the Java driver, but wasn't able to. When running the SELECT from Java, one row has null, while the other has "".

drop keyspace if exists tst;
CREATE KEYSPACE tst WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };

use tst;

CREATE TABLE people (
id int,
country text,
name text,
PRIMARY KEY (id)
);

insert into people (id,name,country) values (1,'a',null);
insert into people (id,name,country) values (2,'b','');
(ns casstest.core
  (:require [clojurewerkz.cassaforte.client :as cl]
            [clojurewerkz.cassaforte.cql :as cql]
            ))

(defn test-it
  []
  (let [s (cl/connect ["127.0.0.1"] "tst")]
    (prn (cql/select s :people))))
(defproject casstest "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.6.0"]
                 ;; [com.datastax.cassandra/cassandra-driver-core "2.0.8"]
                 [com.datastax.cassandra/cassandra-driver-core "2.1.3"]
                 [com.datastax.cassandra/cassandra-driver-dse  "2.1.3"]
                 [clojurewerkz/cassaforte "2.0.0-rc2"]
                 ])
@michaelklishin
Copy link
Member

@pesterhazy thank you for the detailed report. We'll take a look what the Java client returns. cqlsh indeed makes a distinction between null and empty strings, as expected.

@michaelklishin michaelklishin self-assigned this Dec 15, 2014
@michaelklishin michaelklishin added this to the 2.0 milestone Dec 15, 2014
@pesterhazy
Copy link
Author

Here's the java test I used to verify that the Java driver properly returns null:

import com.datastax.driver.core.*;

public class GettingStarted {


    public static void main(String[] args) {
        Cluster cluster;
        Session session;

        // Connect to the cluster and keyspace "demo"
    cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
    session = cluster.connect("tst");

    ResultSet results = session.execute("SELECT * FROM people");
    for (Row row : results) {
        System.out.format("%s *%s*\n",
                          row.getString("name") == null ? "NULL" : row.getString("name"),
                          row.getString("country") == null ? "NULL" : row.getString("country"));
        }

        // Clean up the connection by closing it
    cluster.close();
    }
}

Run with this driver: http://downloads.datastax.com/java-driver/cassandra-java-driver-2.0.2.tar.gz like this (I'm obviously not a Java dev):

#/bin/bash

javac -classpath cassandra-driver-core-2.0.2.jar:. GettingStarted.java && java -classpath '*:lib/*:.' GettingStarted

@pesterhazy
Copy link
Author

Git HEAD fixes things for me, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants