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

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

     [ https://issues.apache.org/jira/browse/CASSANDRA-9609?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Tyler Hobbs resolved CASSANDRA-9609.
------------------------------------
    Resolution: Duplicate

We fixed this in CASSANDRA-5723 by introducing the TimestampType and switching the CQL {{timestamp}} type to that by default.

> 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 org.apache.cassandra.db.marshal.DateType;
> import org.apache.cassandra.db.marshal.LongType;
> import org.apache.cassandra.serializers.LongSerializer;
> import org.apache.cassandra.serializers.TimestampSerializer;
> import java.util.Date;
> import java.util.Locale;
> import java.util.TimeZone;
> public class TestCassandraCompare {
>     static TimestampSerializer dateSerializer = TimestampSerializer.instance;
>     static LongSerializer longSerializer = LongSerializer.instance;
>     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.serialize(l1), longSerializer.serialize(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.serialize(d1), dateSerializer.serialize(d2));
>         int javaCoreCompare = d1.compareTo(d2);
>         if (cassandraCompare != javaCoreCompare) {
>             System.err.println("[" + testNum + "]got incorrect result from DateType 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 DateType 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 DateType 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 DateType 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 DateType 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)