Search code examples
pythontimestampcassandracql3

Bugged timestamps on cqlengine (Cassandra 2.0)


I'm trying to select timestamps columns from Cassandra 2.0 using cqlengine or cql(python), and i'm getting wrong results.

This is what i get from cqlsh ( or thrift ): "2013-09-23 00:00:00-0700"

This is what i get from cqlengine and cql itself: "\x00\x00\x01AG\x0b\xd5\xe0"

If you wanna reproduce the error, try this:

  • open cqlsh
  • create table test (name varchar primary key, dt timestamp)
  • insert into table test ('Test', '2013-09-23 12:00') <<< Yes, i have tried to add by another ways....
  • select * from test ( Here it's everything fine )
  • Now go on cqlengine or cql itself and select that table and you will get a broken hexadecimal.

Thanks !


Solution

  • Unfortunately, cqlengine is not currently compatible with cassandra 2.0

    There were some new types introduced with Cassandra 2.0, and we haven't had a chance to make cqlengine compatible with them. I'm also aware of a problem with blob columns.

    This particular issue is caused by the cql driver returning the timestamp as a raw string of bytes, as opposed to an integer.

    Since cqlengine does not support Cassandra 2.0 yet, your best bet is to use Cassandra 1.2.x until we can get it updated, cqlengine doesn't support any of the new 2.0 features anyway. If you really need to use 2.0, you can work around this problem by subclassing the DateTime column like so:

    class NewDateTime(DateTime):
        def to_python(self, val):
            if isinstance(val, basestring):
                val = struct.unpack('!Q', val)[0] / 1000.0
            return super(NewDateTime, self).to_python(val)