Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timestamp comparison in cassandra

As shown in the picture querying with exact timestamp(2013-08-01 15:02:56) is not returning any result though a row with that timestamp exists but it returns results with that row when queried for

timestamps > '2013-08-01 15:02:56'

Is this normal behavior in Cassandra? enter image description here

like image 203
Viswanadh Kumar Reddy Vuggumud Avatar asked Aug 02 '13 12:08

Viswanadh Kumar Reddy Vuggumud


1 Answers

Yes that is expected behavior.

According to the cassandra docs and here here, cassandra is storing timestamps as "milliseconds since the standard base time known as the epoch".

When you insert your data, you insert a millisecond value with higher granularity than your "2013-08-01 15:02:56" (milliseconds value of "now" vs just seconds and 0 milliseconds). A EQ operator will never match UNLESS your inserted timestamp has 0 milliseconds.

This will work

SELECT * FROM myTable WHERE timestamps >= '2013-08-01 15:02:56'
AND timestamps < '2013-08-01 15:02:57' 

So, when you query it through cqlsh your datetime is translated into an integer (of milliseconds) that is just different from the value you inserted originally. Your inserted value will be some milliseconds AFTER "2013-08-01 15:02:56". You query for EXACTLY "2013-08-01 15:02:56" (and 0 milliseconds). Using a GT or LT operator will match, an EQ operator will not.

Hope that helps!

like image 66
John Avatar answered Sep 27 '22 17:09

John