You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by "Alexander Troshanin (JIRA)" <ji...@apache.org> on 2015/06/17 18:12:00 UTC

[jira] [Created] (CASSANDRA-9609) org.apache.cassandra.db.marshal.DateType compares dates with negative timestamp incorrectly

Alexander Troshanin created CASSANDRA-9609:
----------------------------------------------

             Summary: org.apache.cassandra.db.marshal.DateType compares dates with negative timestamp incorrectly
                 Key: CASSANDRA-9609
                 URL: https://issues.apache.org/jira/browse/CASSANDRA-9609
             Project: Cassandra
          Issue Type: Bug
            Reporter: Alexander Troshanin


We got issues in our application.
We have column family with column names as dates.
when we try to search these columns with some criteria like 
{code}
// pseudocode
colName < '01.01.2015'
{code}
then we dont receive columns whos names are less then 01.01.1970, i.e. with negative timestamp.

After a small research we found out, that probably DateType class has a bug in compateTo method.
Here is very small example shat shows incorrect work of DateType 

{code}
import com.netflix.astyanax.serializers.DateSerializer;
import com.netflix.astyanax.serializers.LongSerializer;
import org.apache.cassandra.db.marshal.DateType;
import org.apache.cassandra.db.marshal.LongType;

import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class TestCassandraCompare {

    static DateSerializer dateSerializer = DateSerializer.get();
    static LongSerializer longSerializer = LongSerializer.get();
    static DateType dateType = DateType.instance;
    static LongType longType = LongType.instance;

    public static void main(String[] args) {

        Locale.setDefault(Locale.ENGLISH);
        TimeZone.setDefault(TimeZone.getTimeZone("GMT0"));

        // check longs
        doLongsCheck(10, 20);
        doLongsCheck(20, 10);
        doLongsCheck(-10, -20);
        doLongsCheck(-20, -10);
        doLongsCheck(-10, 20);
        doLongsCheck(20, -10);
        doLongsCheck(10, -20);
        doLongsCheck(-20, 10);

        // check dates
        doDatesCheck(new Date(100000),  new Date(200000),   1);
        doDatesCheck(new Date(200000),  new Date(100000),   2);
        doDatesCheck(new Date(-100000), new Date(-200000),  3);
        doDatesCheck(new Date(-200000), new Date(-100000),  4);
        doDatesCheck(new Date(-100000), new Date(200000),   5);
        doDatesCheck(new Date(200000),  new Date(-100000),  6);
        doDatesCheck(new Date(100000),  new Date(-200000),  7);
        doDatesCheck(new Date(-200000), new Date(100000),   8);

    }

    private static void doLongsCheck(long l1, long l2) {
        int cassandraCompare = longType.compare(longSerializer.toByteBuffer(l1), longSerializer.toByteBuffer(l2));
        int javaCoreCompare = Long.compare(l1, l2);
        if (cassandraCompare != javaCoreCompare) {
            System.err.println("got incorrect result from LongType compare method." +
                    "\n\tlong1: " + l1 +
                    "\n\tlong2: " + l2 +
                    "\n\tcassandraCompare: " + cassandraCompare +
                    "\n\tjavaCoreCompare: " + javaCoreCompare
            );
        }
    }

    private static void doDatesCheck(Date d1, Date d2, int testNum) {
        int cassandraCompare = dateType.compare(dateSerializer.toByteBuffer(d1), dateSerializer.toByteBuffer(d2));
        int javaCoreCompare = d1.compareTo(d2);
        if (cassandraCompare != javaCoreCompare) {
            System.err.println("[" + testNum + "]got incorrect result from LongType compare method." +
                    "\n\tdate1: " + d1 +
                    "\n\tdate2: " + d2 +
                    "\n\tcassandraCompare: " + cassandraCompare +
                    "\n\tjavaCoreCompare: " + javaCoreCompare
            );
        }
    }

}
{code}

If you will run the code you will se next output:
{code}
[5]got incorrect result from LongType compare method.
	date1: Wed Dec 31 23:58:20 GMT 1969
	date2: Thu Jan 01 00:03:20 GMT 1970
	cassandraCompare: 1
	javaCoreCompare: -1
[6]got incorrect result from LongType compare method.
	date1: Thu Jan 01 00:03:20 GMT 1970
	date2: Wed Dec 31 23:58:20 GMT 1969
	cassandraCompare: -1
	javaCoreCompare: 1
[7]got incorrect result from LongType compare method.
	date1: Thu Jan 01 00:01:40 GMT 1970
	date2: Wed Dec 31 23:56:40 GMT 1969
	cassandraCompare: -1
	javaCoreCompare: 1
[8]got incorrect result from LongType compare method.
	date1: Wed Dec 31 23:56:40 GMT 1969
	date2: Thu Jan 01 00:01:40 GMT 1970
	cassandraCompare: 1
	javaCoreCompare: -1
{code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)