Uploaded image for project: 'Cassandra'
  1. Cassandra
  2. CASSANDRA-15064

Wrong ordering for timeuuid fields

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Normal
    • Resolution: Won't Fix
    • None
    • Cluster/Schema
    • None
    • Correctness - API / Semantic Implementation
    • Normal
    • Normal
    • User Report

    Description

      Hi!

      We're seeing some strange behavior for the ordering of timeuuid fields. They seem to be sorted in the wrong order when the clock_seq_low field in a timeuuid goes from 7f to 80. Consider the following example:

      cqlsh:test> show version; 
      [cqlsh 5.0.1 | Cassandra 3.11.4 | CQL spec 3.4.4 | Native protocol v4] 
      cqlsh:test> CREATE TABLE t ( 
              ...     partition   int, 
              ...     t           timeuuid, 
              ...     i           int, 
              ...  
              ...     PRIMARY KEY(partition, t) 
              ... ) 
              ... WITH CLUSTERING ORDER BY(t ASC); 
      cqlsh:test> INSERT INTO t(partition, t, i) VALUES(1, 84e2c963-4ef9-11e9-b57e-f0def1d0755e, 1); 
      cqlsh:test> INSERT INTO t(partition, t, i) VALUES(1, 84e2c963-4ef9-11e9-b57f-f0def1d0755e, 2); 
      cqlsh:test> INSERT INTO t(partition, t, i) VALUES(1, 84e2c963-4ef9-11e9-b580-f0def1d0755e, 3); 
      cqlsh:test> INSERT INTO t(partition, t, i) VALUES(1, 84e2c963-4ef9-11e9-b581-f0def1d0755e, 4); 
      cqlsh:test> INSERT INTO t(partition, t, i) VALUES(1, 84e2c963-4ef9-11e9-b582-f0def1d0755e, 5); 
      cqlsh:test> SELECT * FROM t WHERE partition = 1 ORDER BY t ASC; 
       
       partition | t                                    | i 
      -----------+--------------------------------------+--- 
               1 | 84e2c963-4ef9-11e9-b580-f0def1d0755e | 3 
               1 | 84e2c963-4ef9-11e9-b581-f0def1d0755e | 4 
               1 | 84e2c963-4ef9-11e9-b582-f0def1d0755e | 5 
               1 | 84e2c963-4ef9-11e9-b57e-f0def1d0755e | 1 
               1 | 84e2c963-4ef9-11e9-b57f-f0def1d0755e | 2 
       
      (5 rows) 
      cqlsh:test>
      

      The expected behavior is that the rows are returned in the same order as they were inserted (we inserted them with their clustering key in an ascending order). Instead, the order "wraps" in the middle.

      This issue only arises when the 9th octet (clock_seq_low) in the uuid goes from 7f to 80. A guess would be that the comparison is implemented as a signed integer instead of an unsigned integer, as 0x7f = 127 and 0x80 = -128. According to the RFC, the field should be treated as an unsigned integer: https://tools.ietf.org/html/rfc4122#section-4.1.2

      Changing the field from a timeuuid to a uuid gives the expected correct behavior:

      cqlsh:test> CREATE TABLE t ( 
              ...     partition   int, 
              ...     t           uuid, 
              ...     i           int, 
              ...  
              ...     PRIMARY KEY(partition, t) 
              ... ) 
              ... WITH CLUSTERING ORDER BY(t ASC); 
      cqlsh:test> INSERT INTO t(partition, t, i) VALUES(1, 84e2c963-4ef9-11e9-b57e-f0def1d0755e, 1); 
      cqlsh:test> INSERT INTO t(partition, t, i) VALUES(1, 84e2c963-4ef9-11e9-b57f-f0def1d0755e, 2); 
      cqlsh:test> INSERT INTO t(partition, t, i) VALUES(1, 84e2c963-4ef9-11e9-b580-f0def1d0755e, 3); 
      cqlsh:test> INSERT INTO t(partition, t, i) VALUES(1, 84e2c963-4ef9-11e9-b581-f0def1d0755e, 4); 
      cqlsh:test> INSERT INTO t(partition, t, i) VALUES(1, 84e2c963-4ef9-11e9-b582-f0def1d0755e, 5); 
      cqlsh:test> SELECT * FROM t WHERE partition = 1 ORDER BY t ASC; 
       
       partition | t                                    | i 
      -----------+--------------------------------------+--- 
               1 | 84e2c963-4ef9-11e9-b57e-f0def1d0755e | 1 
               1 | 84e2c963-4ef9-11e9-b57f-f0def1d0755e | 2 
               1 | 84e2c963-4ef9-11e9-b580-f0def1d0755e | 3 
               1 | 84e2c963-4ef9-11e9-b581-f0def1d0755e | 4 
               1 | 84e2c963-4ef9-11e9-b582-f0def1d0755e | 5 
       
      (5 rows) 
      cqlsh:test>

       

       

      Attachments

        1. example.cql
          0.7 kB
          Andreas Andersen

        Activity

          People

            jmeredithco Jon Meredith
            yoshiyaka Andreas Andersen
            Jon Meredith
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: