20

How do I query in cassandra for != null columns.

Select * from tableA where id != null;
Select * from tableA where name != null;

Then I wanted to store these values and insert these into different table.

7
  • where id is null? null is a special value in databases, and is contagious. any operations done with a null get "infected" and the results become null themsleves. you cannot do null = null, and have to use the special is null syntax.
    – Marc B
    Jun 27, 2014 at 16:04
  • Will Select * from tableA where id = null; Jun 27, 2014 at 16:06
  • where id is not null as well.
    – Marc B
    Jun 27, 2014 at 16:07
  • 2
    select * from tableA where name is not null; Bad Request: line 1:42 no viable alternative at input 'is' Jun 27, 2014 at 16:11
  • 5
    This is in cassandra and not MYSQL just a reminder Jun 27, 2014 at 16:14

2 Answers 2

14

I don't think this is possible with Cassandra. First of all, Cassandra CQL doesn't support the use of NOT or not equal to operators in the WHERE clause. Secondly, your WHERE clause can only contain primary key columns, and primary key columns will not allow null values to be inserted. I wasn't sure about secondary indexes though, so I ran this quick test:

create table nullTest (id text PRIMARY KEY, name text);
INSERT INTO nullTest (id,name) VALUES ('1','bob');
INSERT INTO nullTest (id,name) VALUES ('2',null);

I now have a table and two rows (one with null data):

SELECT * FROM nullTest;

 id | name
----+------
  2 | null
  1 |  bob

(2 rows)

I then try to create a secondary index on name, which I know contains null values.

CREATE INDEX nullTestIdx ON nullTest(name);

It lets me do it. Now, I'll run a query on that index.

SELECT * FROM nullTest WHERE name=null;
Bad Request: Unsupported null value for indexed column name

And again, this is done under the premise that you can't query for not null, if you can't even query for column values that may actually be null.

So, I'm thinking this can't be done. Also, if null values are a possibility in your primary key, then you may want to re-evaluate your data model. Again, I know the OP's question is about querying where data is not null. But as I mentioned before, Cassandra CQL doesn't have a NOT or != operator, so that's going to be a problem right there.

Another option, is to insert an empty string instead of a null. You would then be able to query on an empty string. But that still doesn't get you past the fundamental design flaw of having a null in a primary key field. Perhaps if you had a composite primary key, and only part of it (the clustering columns) had the possibility of being empty (certainly not part of the partitioning key). But you'd still be stuck with the problem of not being able to query for rows that are "not empty" (instead of not null).

NOTE: Inserting null values was done here for demonstration purposes only. It is something you should do your best to avoid, as inserting a null column value WILL create a tombstone. Likewise, inserting lots of null values will create lots of tombstones.

3
  • I think this is now outdated... issues.apache.org/jira/browse/CASSANDRA-3783 added support for NULL values. Aug 2, 2020 at 18:55
  • @JeffWidman That ticket is misleading. Cassandra 4.0(beta) does not support INSERTing null values, nor does it yet support a not-equals operator.
    – Aaron
    Aug 3, 2020 at 14:07
  • 1
    Ah, I'd missed this final comment on that ticket: This ticket contracted from its original scope and turned into just support for upserting a null actually performing a delete operation on the cell. There is currently no select support for indexed nulls, and given the design of Cassandra, is considered a difficult/prohibitive problem. Aug 11, 2020 at 3:33
3

1) select * from test;

name             | id | address
------------------+----+------------------
    bangalore |  3 |       ramyam_lab
    bangalore |  4 | bangalore_ramyam
    bangalore |  5 |        jasgdjgkj
       prasad | 11 |             null
       prasad | 12 |             null
        india |  6 |          karnata
        india |  7 |          karnata
ramyam-bangalore |  3 |        jasgdjgkj
ramyam-bangalore |  5 |        jasgdjgkj

2)cassandra does't support null values selection.It is showing null for our understanding.

3) For handling null values use another strings like "not-available","null",then we can select data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.